firewire: cdev: add PHY packet transmission
[deliverable/linux.git] / drivers / firewire / core-cdev.c
CommitLineData
c781c06d
KH
1/*
2 * Char device for device raw access
19a15b93 3 *
c781c06d 4 * Copyright (C) 2005-2007 Kristian Hoegsberg <krh@bitplanet.net>
19a15b93
KH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
eb5b35a5 21#include <linux/bug.h>
be5bbd67
SR
22#include <linux/compat.h>
23#include <linux/delay.h>
24#include <linux/device.h>
25#include <linux/errno.h>
77c9a5da 26#include <linux/firewire.h>
be5bbd67
SR
27#include <linux/firewire-cdev.h>
28#include <linux/idr.h>
4a9bde9b 29#include <linux/irqflags.h>
b1bda4cd 30#include <linux/jiffies.h>
19a15b93 31#include <linux/kernel.h>
fb443036 32#include <linux/kref.h>
be5bbd67
SR
33#include <linux/mm.h>
34#include <linux/module.h>
d67cfb96 35#include <linux/mutex.h>
19a15b93 36#include <linux/poll.h>
a99bbaf5 37#include <linux/sched.h>
cf417e54 38#include <linux/spinlock.h>
281e2032 39#include <linux/string.h>
be5bbd67 40#include <linux/time.h>
e034d242 41#include <linux/uaccess.h>
be5bbd67
SR
42#include <linux/vmalloc.h>
43#include <linux/wait.h>
b1bda4cd 44#include <linux/workqueue.h>
be5bbd67 45
a64408b9 46#include <asm/system.h>
be5bbd67 47
77c9a5da 48#include "core.h"
19a15b93 49
604f4516
SR
50/*
51 * ABI version history is documented in linux/firewire-cdev.h.
52 */
e205597d
SR
53#define FW_CDEV_KERNEL_VERSION 4
54#define FW_CDEV_VERSION_EVENT_REQUEST2 4
604f4516 55
19a15b93 56struct client {
344bbc4d 57 u32 version;
19a15b93 58 struct fw_device *device;
45ee3199 59
19a15b93 60 spinlock_t lock;
45ee3199
JF
61 bool in_shutdown;
62 struct idr resource_idr;
19a15b93 63 struct list_head event_list;
19a15b93 64 wait_queue_head_t wait;
da8ecffa 65 u64 bus_reset_closure;
9aad8125 66
19a15b93 67 struct fw_iso_context *iso_context;
abaa5743 68 u64 iso_closure;
9aad8125
KH
69 struct fw_iso_buffer buffer;
70 unsigned long vm_start;
97bd9efa
KH
71
72 struct list_head link;
fb443036 73 struct kref kref;
19a15b93
KH
74};
75
fb443036
SR
76static inline void client_get(struct client *client)
77{
78 kref_get(&client->kref);
79}
80
81static void client_release(struct kref *kref)
82{
83 struct client *client = container_of(kref, struct client, kref);
84
85 fw_device_put(client->device);
86 kfree(client);
87}
88
89static void client_put(struct client *client)
90{
91 kref_put(&client->kref, client_release);
92}
93
97c18b7f
SR
94struct client_resource;
95typedef void (*client_resource_release_fn_t)(struct client *,
96 struct client_resource *);
97struct client_resource {
98 client_resource_release_fn_t release;
99 int handle;
100};
101
102struct address_handler_resource {
103 struct client_resource resource;
104 struct fw_address_handler handler;
105 __u64 closure;
106 struct client *client;
107};
108
109struct outbound_transaction_resource {
110 struct client_resource resource;
111 struct fw_transaction transaction;
112};
113
114struct inbound_transaction_resource {
115 struct client_resource resource;
08bd34c9 116 struct fw_card *card;
97c18b7f
SR
117 struct fw_request *request;
118 void *data;
119 size_t length;
120};
121
122struct descriptor_resource {
123 struct client_resource resource;
124 struct fw_descriptor descriptor;
125 u32 data[0];
126};
127
b1bda4cd
JFSR
128struct iso_resource {
129 struct client_resource resource;
130 struct client *client;
131 /* Schedule work and access todo only with client->lock held. */
132 struct delayed_work work;
1ec3c026
SR
133 enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
134 ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
b1bda4cd
JFSR
135 int generation;
136 u64 channels;
137 s32 bandwidth;
6fdc0370 138 __be32 transaction_data[2];
b1bda4cd
JFSR
139 struct iso_resource_event *e_alloc, *e_dealloc;
140};
141
b1bda4cd
JFSR
142static void release_iso_resource(struct client *, struct client_resource *);
143
9fb551bf
SR
144static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
145{
146 client_get(r->client);
147 if (!schedule_delayed_work(&r->work, delay))
148 client_put(r->client);
149}
150
151static void schedule_if_iso_resource(struct client_resource *resource)
152{
153 if (resource->release == release_iso_resource)
154 schedule_iso_resource(container_of(resource,
155 struct iso_resource, resource), 0);
156}
157
97c18b7f
SR
158/*
159 * dequeue_event() just kfree()'s the event, so the event has to be
160 * the first field in a struct XYZ_event.
161 */
162struct event {
163 struct { void *data; size_t size; } v[2];
164 struct list_head link;
165};
166
167struct bus_reset_event {
168 struct event event;
169 struct fw_cdev_event_bus_reset reset;
170};
171
172struct outbound_transaction_event {
173 struct event event;
174 struct client *client;
175 struct outbound_transaction_resource r;
176 struct fw_cdev_event_response response;
177};
178
179struct inbound_transaction_event {
180 struct event event;
e205597d
SR
181 union {
182 struct fw_cdev_event_request request;
183 struct fw_cdev_event_request2 request2;
184 } req;
97c18b7f
SR
185};
186
187struct iso_interrupt_event {
188 struct event event;
189 struct fw_cdev_event_iso_interrupt interrupt;
190};
191
b1bda4cd
JFSR
192struct iso_resource_event {
193 struct event event;
e21fcf79 194 struct fw_cdev_event_iso_resource iso_resource;
b1bda4cd
JFSR
195};
196
850bb6f2
SR
197struct outbound_phy_packet_event {
198 struct event event;
199 struct client *client;
200 struct fw_packet p;
201 struct fw_cdev_event_phy_packet phy_packet;
202};
203
53dca511 204static inline void __user *u64_to_uptr(__u64 value)
19a15b93
KH
205{
206 return (void __user *)(unsigned long)value;
207}
208
53dca511 209static inline __u64 uptr_to_u64(void __user *ptr)
19a15b93
KH
210{
211 return (__u64)(unsigned long)ptr;
212}
213
214static int fw_device_op_open(struct inode *inode, struct file *file)
215{
216 struct fw_device *device;
217 struct client *client;
218
96b19062 219 device = fw_device_get_by_devt(inode->i_rdev);
a3aca3da
KH
220 if (device == NULL)
221 return -ENODEV;
19a15b93 222
551f4cb9
JF
223 if (fw_device_is_shutdown(device)) {
224 fw_device_put(device);
225 return -ENODEV;
226 }
227
2d826cc5 228 client = kzalloc(sizeof(*client), GFP_KERNEL);
96b19062
SR
229 if (client == NULL) {
230 fw_device_put(device);
19a15b93 231 return -ENOMEM;
96b19062 232 }
19a15b93 233
96b19062 234 client->device = device;
19a15b93 235 spin_lock_init(&client->lock);
45ee3199
JF
236 idr_init(&client->resource_idr);
237 INIT_LIST_HEAD(&client->event_list);
19a15b93 238 init_waitqueue_head(&client->wait);
fb443036 239 kref_init(&client->kref);
19a15b93
KH
240
241 file->private_data = client;
242
d67cfb96 243 mutex_lock(&device->client_list_mutex);
97bd9efa 244 list_add_tail(&client->link, &device->client_list);
d67cfb96 245 mutex_unlock(&device->client_list_mutex);
97bd9efa 246
3ac26b2e 247 return nonseekable_open(inode, file);
19a15b93
KH
248}
249
250static void queue_event(struct client *client, struct event *event,
251 void *data0, size_t size0, void *data1, size_t size1)
252{
253 unsigned long flags;
254
255 event->v[0].data = data0;
256 event->v[0].size = size0;
257 event->v[1].data = data1;
258 event->v[1].size = size1;
259
260 spin_lock_irqsave(&client->lock, flags);
45ee3199
JF
261 if (client->in_shutdown)
262 kfree(event);
263 else
264 list_add_tail(&event->link, &client->event_list);
19a15b93 265 spin_unlock_irqrestore(&client->lock, flags);
83431cba
JF
266
267 wake_up_interruptible(&client->wait);
19a15b93
KH
268}
269
53dca511
SR
270static int dequeue_event(struct client *client,
271 char __user *buffer, size_t count)
19a15b93 272{
19a15b93
KH
273 struct event *event;
274 size_t size, total;
2dbd7d7e 275 int i, ret;
19a15b93 276
2dbd7d7e
SR
277 ret = wait_event_interruptible(client->wait,
278 !list_empty(&client->event_list) ||
279 fw_device_is_shutdown(client->device));
280 if (ret < 0)
281 return ret;
19a15b93 282
2603bf21
KH
283 if (list_empty(&client->event_list) &&
284 fw_device_is_shutdown(client->device))
285 return -ENODEV;
19a15b93 286
3ba94986 287 spin_lock_irq(&client->lock);
a459b8ab 288 event = list_first_entry(&client->event_list, struct event, link);
19a15b93 289 list_del(&event->link);
3ba94986 290 spin_unlock_irq(&client->lock);
19a15b93 291
19a15b93
KH
292 total = 0;
293 for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
294 size = min(event->v[i].size, count - total);
2603bf21 295 if (copy_to_user(buffer + total, event->v[i].data, size)) {
2dbd7d7e 296 ret = -EFAULT;
19a15b93 297 goto out;
2603bf21 298 }
19a15b93
KH
299 total += size;
300 }
2dbd7d7e 301 ret = total;
19a15b93
KH
302
303 out:
304 kfree(event);
305
2dbd7d7e 306 return ret;
19a15b93
KH
307}
308
53dca511
SR
309static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
310 size_t count, loff_t *offset)
19a15b93
KH
311{
312 struct client *client = file->private_data;
313
314 return dequeue_event(client, buffer, count);
315}
316
53dca511
SR
317static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
318 struct client *client)
344bbc4d 319{
da8ecffa 320 struct fw_card *card = client->device->card;
cf417e54 321
3ba94986 322 spin_lock_irq(&card->lock);
344bbc4d 323
da8ecffa 324 event->closure = client->bus_reset_closure;
344bbc4d 325 event->type = FW_CDEV_EVENT_BUS_RESET;
cf5a56ac 326 event->generation = client->device->generation;
da8ecffa 327 event->node_id = client->device->node_id;
344bbc4d 328 event->local_node_id = card->local_node->node_id;
250b2b6d 329 event->bm_node_id = card->bm_node_id;
344bbc4d
KH
330 event->irm_node_id = card->irm_node->node_id;
331 event->root_node_id = card->root_node->node_id;
cf417e54 332
3ba94986 333 spin_unlock_irq(&card->lock);
344bbc4d
KH
334}
335
53dca511
SR
336static void for_each_client(struct fw_device *device,
337 void (*callback)(struct client *client))
2603bf21 338{
2603bf21 339 struct client *c;
2603bf21 340
d67cfb96 341 mutex_lock(&device->client_list_mutex);
2603bf21
KH
342 list_for_each_entry(c, &device->client_list, link)
343 callback(c);
d67cfb96 344 mutex_unlock(&device->client_list_mutex);
2603bf21
KH
345}
346
b1bda4cd
JFSR
347static int schedule_reallocations(int id, void *p, void *data)
348{
9fb551bf 349 schedule_if_iso_resource(p);
b1bda4cd 350
b1bda4cd
JFSR
351 return 0;
352}
353
53dca511 354static void queue_bus_reset_event(struct client *client)
97bd9efa 355{
97c18b7f 356 struct bus_reset_event *e;
97bd9efa 357
97c18b7f
SR
358 e = kzalloc(sizeof(*e), GFP_KERNEL);
359 if (e == NULL) {
97bd9efa
KH
360 fw_notify("Out of memory when allocating bus reset event\n");
361 return;
362 }
363
97c18b7f 364 fill_bus_reset_event(&e->reset, client);
97bd9efa 365
97c18b7f
SR
366 queue_event(client, &e->event,
367 &e->reset, sizeof(e->reset), NULL, 0);
b1bda4cd
JFSR
368
369 spin_lock_irq(&client->lock);
370 idr_for_each(&client->resource_idr, schedule_reallocations, client);
371 spin_unlock_irq(&client->lock);
97bd9efa
KH
372}
373
374void fw_device_cdev_update(struct fw_device *device)
375{
2603bf21
KH
376 for_each_client(device, queue_bus_reset_event);
377}
97bd9efa 378
2603bf21
KH
379static void wake_up_client(struct client *client)
380{
381 wake_up_interruptible(&client->wait);
382}
97bd9efa 383
2603bf21
KH
384void fw_device_cdev_remove(struct fw_device *device)
385{
386 for_each_client(device, wake_up_client);
97bd9efa
KH
387}
388
6e95dea7
SR
389union ioctl_arg {
390 struct fw_cdev_get_info get_info;
391 struct fw_cdev_send_request send_request;
392 struct fw_cdev_allocate allocate;
393 struct fw_cdev_deallocate deallocate;
394 struct fw_cdev_send_response send_response;
395 struct fw_cdev_initiate_bus_reset initiate_bus_reset;
396 struct fw_cdev_add_descriptor add_descriptor;
397 struct fw_cdev_remove_descriptor remove_descriptor;
398 struct fw_cdev_create_iso_context create_iso_context;
399 struct fw_cdev_queue_iso queue_iso;
400 struct fw_cdev_start_iso start_iso;
401 struct fw_cdev_stop_iso stop_iso;
402 struct fw_cdev_get_cycle_timer get_cycle_timer;
403 struct fw_cdev_allocate_iso_resource allocate_iso_resource;
404 struct fw_cdev_send_stream_packet send_stream_packet;
405 struct fw_cdev_get_cycle_timer2 get_cycle_timer2;
850bb6f2 406 struct fw_cdev_send_phy_packet send_phy_packet;
6e95dea7
SR
407};
408
409static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
19a15b93 410{
6e95dea7 411 struct fw_cdev_get_info *a = &arg->get_info;
344bbc4d 412 struct fw_cdev_event_bus_reset bus_reset;
c9755e14 413 unsigned long ret = 0;
344bbc4d 414
6e95dea7 415 client->version = a->version;
604f4516 416 a->version = FW_CDEV_KERNEL_VERSION;
6e95dea7 417 a->card = client->device->card->index;
344bbc4d 418
c9755e14
SR
419 down_read(&fw_device_rwsem);
420
6e95dea7
SR
421 if (a->rom != 0) {
422 size_t want = a->rom_length;
d84702a5 423 size_t have = client->device->config_rom_length * 4;
344bbc4d 424
6e95dea7
SR
425 ret = copy_to_user(u64_to_uptr(a->rom),
426 client->device->config_rom, min(want, have));
344bbc4d 427 }
6e95dea7 428 a->rom_length = client->device->config_rom_length * 4;
344bbc4d 429
c9755e14
SR
430 up_read(&fw_device_rwsem);
431
432 if (ret != 0)
433 return -EFAULT;
434
6e95dea7
SR
435 client->bus_reset_closure = a->bus_reset_closure;
436 if (a->bus_reset != 0) {
da8ecffa 437 fill_bus_reset_event(&bus_reset, client);
6e95dea7
SR
438 if (copy_to_user(u64_to_uptr(a->bus_reset),
439 &bus_reset, sizeof(bus_reset)))
344bbc4d
KH
440 return -EFAULT;
441 }
19a15b93 442
19a15b93
KH
443 return 0;
444}
445
53dca511
SR
446static int add_client_resource(struct client *client,
447 struct client_resource *resource, gfp_t gfp_mask)
3964a449
KH
448{
449 unsigned long flags;
45ee3199
JF
450 int ret;
451
452 retry:
453 if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
454 return -ENOMEM;
3964a449
KH
455
456 spin_lock_irqsave(&client->lock, flags);
45ee3199
JF
457 if (client->in_shutdown)
458 ret = -ECANCELED;
459 else
460 ret = idr_get_new(&client->resource_idr, resource,
461 &resource->handle);
b1bda4cd 462 if (ret >= 0) {
fb443036 463 client_get(client);
9fb551bf 464 schedule_if_iso_resource(resource);
b1bda4cd 465 }
3964a449 466 spin_unlock_irqrestore(&client->lock, flags);
45ee3199
JF
467
468 if (ret == -EAGAIN)
469 goto retry;
470
471 return ret < 0 ? ret : 0;
3964a449
KH
472}
473
53dca511
SR
474static int release_client_resource(struct client *client, u32 handle,
475 client_resource_release_fn_t release,
e21fcf79 476 struct client_resource **return_resource)
3964a449 477{
e21fcf79 478 struct client_resource *resource;
3964a449 479
3ba94986 480 spin_lock_irq(&client->lock);
45ee3199 481 if (client->in_shutdown)
e21fcf79 482 resource = NULL;
45ee3199 483 else
e21fcf79
SR
484 resource = idr_find(&client->resource_idr, handle);
485 if (resource && resource->release == release)
45ee3199 486 idr_remove(&client->resource_idr, handle);
3ba94986 487 spin_unlock_irq(&client->lock);
3964a449 488
e21fcf79 489 if (!(resource && resource->release == release))
3964a449
KH
490 return -EINVAL;
491
e21fcf79
SR
492 if (return_resource)
493 *return_resource = resource;
3964a449 494 else
e21fcf79 495 resource->release(client, resource);
3964a449 496
fb443036
SR
497 client_put(client);
498
3964a449
KH
499 return 0;
500}
501
53dca511
SR
502static void release_transaction(struct client *client,
503 struct client_resource *resource)
3964a449 504{
97c18b7f
SR
505 struct outbound_transaction_resource *r = container_of(resource,
506 struct outbound_transaction_resource, resource);
3964a449 507
97c18b7f 508 fw_cancel_transaction(client->device->card, &r->transaction);
3964a449
KH
509}
510
53dca511
SR
511static void complete_transaction(struct fw_card *card, int rcode,
512 void *payload, size_t length, void *data)
19a15b93 513{
97c18b7f
SR
514 struct outbound_transaction_event *e = data;
515 struct fw_cdev_event_response *rsp = &e->response;
516 struct client *client = e->client;
28cf6a04 517 unsigned long flags;
19a15b93 518
97c18b7f
SR
519 if (length < rsp->length)
520 rsp->length = length;
19a15b93 521 if (rcode == RCODE_COMPLETE)
97c18b7f 522 memcpy(rsp->data, payload, rsp->length);
19a15b93 523
28cf6a04 524 spin_lock_irqsave(&client->lock, flags);
45ee3199 525 /*
fb443036
SR
526 * 1. If called while in shutdown, the idr tree must be left untouched.
527 * The idr handle will be removed and the client reference will be
528 * dropped later.
529 * 2. If the call chain was release_client_resource ->
530 * release_transaction -> complete_transaction (instead of a normal
531 * conclusion of the transaction), i.e. if this resource was already
532 * unregistered from the idr, the client reference will be dropped
533 * by release_client_resource and we must not drop it here.
45ee3199 534 */
fb443036 535 if (!client->in_shutdown &&
97c18b7f
SR
536 idr_find(&client->resource_idr, e->r.resource.handle)) {
537 idr_remove(&client->resource_idr, e->r.resource.handle);
fb443036
SR
538 /* Drop the idr's reference */
539 client_put(client);
540 }
28cf6a04
KH
541 spin_unlock_irqrestore(&client->lock, flags);
542
97c18b7f
SR
543 rsp->type = FW_CDEV_EVENT_RESPONSE;
544 rsp->rcode = rcode;
8401d92b
DM
545
546 /*
97c18b7f 547 * In the case that sizeof(*rsp) doesn't align with the position of the
8401d92b
DM
548 * data, and the read is short, preserve an extra copy of the data
549 * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
550 * for short reads and some apps depended on it, this is both safe
551 * and prudent for compatibility.
552 */
97c18b7f
SR
553 if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
554 queue_event(client, &e->event, rsp, sizeof(*rsp),
555 rsp->data, rsp->length);
8401d92b 556 else
97c18b7f 557 queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
8401d92b 558 NULL, 0);
fb443036
SR
559
560 /* Drop the transaction callback's reference */
561 client_put(client);
19a15b93
KH
562}
563
acfe8333
JFSR
564static int init_request(struct client *client,
565 struct fw_cdev_send_request *request,
566 int destination_id, int speed)
19a15b93 567{
97c18b7f 568 struct outbound_transaction_event *e;
1f3125af 569 int ret;
19a15b93 570
18e9b10f
SR
571 if (request->tcode != TCODE_STREAM_DATA &&
572 (request->length > 4096 || request->length > 512 << speed))
5d3fd692 573 return -EIO;
19a15b93 574
a8e93f3d
CL
575 if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
576 request->length < 4)
577 return -EINVAL;
578
97c18b7f
SR
579 e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
580 if (e == NULL)
19a15b93
KH
581 return -ENOMEM;
582
97c18b7f
SR
583 e->client = client;
584 e->response.length = request->length;
585 e->response.closure = request->closure;
19a15b93 586
4f259223 587 if (request->data &&
97c18b7f 588 copy_from_user(e->response.data,
4f259223 589 u64_to_uptr(request->data), request->length)) {
1f3125af 590 ret = -EFAULT;
45ee3199 591 goto failed;
1f3125af
SR
592 }
593
97c18b7f
SR
594 e->r.resource.release = release_transaction;
595 ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
45ee3199
JF
596 if (ret < 0)
597 goto failed;
28cf6a04 598
fb443036
SR
599 /* Get a reference for the transaction callback */
600 client_get(client);
601
acfe8333 602 fw_send_request(client->device->card, &e->r.transaction,
664d8010
SR
603 request->tcode, destination_id, request->generation,
604 speed, request->offset, e->response.data,
605 request->length, complete_transaction, e);
606 return 0;
19a15b93 607
45ee3199 608 failed:
97c18b7f 609 kfree(e);
1f3125af
SR
610
611 return ret;
19a15b93
KH
612}
613
6e95dea7 614static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
acfe8333 615{
6e95dea7 616 switch (arg->send_request.tcode) {
acfe8333
JFSR
617 case TCODE_WRITE_QUADLET_REQUEST:
618 case TCODE_WRITE_BLOCK_REQUEST:
619 case TCODE_READ_QUADLET_REQUEST:
620 case TCODE_READ_BLOCK_REQUEST:
621 case TCODE_LOCK_MASK_SWAP:
622 case TCODE_LOCK_COMPARE_SWAP:
623 case TCODE_LOCK_FETCH_ADD:
624 case TCODE_LOCK_LITTLE_ADD:
625 case TCODE_LOCK_BOUNDED_ADD:
626 case TCODE_LOCK_WRAP_ADD:
627 case TCODE_LOCK_VENDOR_DEPENDENT:
628 break;
629 default:
630 return -EINVAL;
631 }
632
6e95dea7 633 return init_request(client, &arg->send_request, client->device->node_id,
acfe8333
JFSR
634 client->device->max_speed);
635}
636
281e2032
SR
637static inline bool is_fcp_request(struct fw_request *request)
638{
639 return request == NULL;
640}
641
53dca511
SR
642static void release_request(struct client *client,
643 struct client_resource *resource)
3964a449 644{
97c18b7f
SR
645 struct inbound_transaction_resource *r = container_of(resource,
646 struct inbound_transaction_resource, resource);
3964a449 647
281e2032
SR
648 if (is_fcp_request(r->request))
649 kfree(r->data);
650 else
08bd34c9 651 fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
0244f573
SR
652
653 fw_card_put(r->card);
97c18b7f 654 kfree(r);
3964a449
KH
655}
656
97c18b7f 657static void handle_request(struct fw_card *card, struct fw_request *request,
53dca511 658 int tcode, int destination, int source,
33e553fe 659 int generation, unsigned long long offset,
53dca511 660 void *payload, size_t length, void *callback_data)
19a15b93 661{
97c18b7f
SR
662 struct address_handler_resource *handler = callback_data;
663 struct inbound_transaction_resource *r;
664 struct inbound_transaction_event *e;
e205597d 665 size_t event_size0;
281e2032 666 void *fcp_frame = NULL;
45ee3199 667 int ret;
19a15b93 668
0244f573
SR
669 /* card may be different from handler->client->device->card */
670 fw_card_get(card);
671
97c18b7f 672 r = kmalloc(sizeof(*r), GFP_ATOMIC);
2d826cc5 673 e = kmalloc(sizeof(*e), GFP_ATOMIC);
97c18b7f 674 if (r == NULL || e == NULL)
45ee3199 675 goto failed;
19a15b93 676
08bd34c9 677 r->card = card;
97c18b7f
SR
678 r->request = request;
679 r->data = payload;
680 r->length = length;
19a15b93 681
281e2032
SR
682 if (is_fcp_request(request)) {
683 /*
684 * FIXME: Let core-transaction.c manage a
685 * single reference-counted copy?
686 */
687 fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
688 if (fcp_frame == NULL)
689 goto failed;
690
691 r->data = fcp_frame;
692 }
693
97c18b7f
SR
694 r->resource.release = release_request;
695 ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
45ee3199
JF
696 if (ret < 0)
697 goto failed;
19a15b93 698
e205597d
SR
699 if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
700 struct fw_cdev_event_request *req = &e->req.request;
701
702 if (tcode & 0x10)
703 tcode = TCODE_LOCK_REQUEST;
704
705 req->type = FW_CDEV_EVENT_REQUEST;
706 req->tcode = tcode;
707 req->offset = offset;
708 req->length = length;
709 req->handle = r->resource.handle;
710 req->closure = handler->closure;
711 event_size0 = sizeof(*req);
712 } else {
713 struct fw_cdev_event_request2 *req = &e->req.request2;
714
715 req->type = FW_CDEV_EVENT_REQUEST2;
716 req->tcode = tcode;
717 req->offset = offset;
718 req->source_node_id = source;
719 req->destination_node_id = destination;
720 req->card = card->index;
721 req->generation = generation;
722 req->length = length;
723 req->handle = r->resource.handle;
724 req->closure = handler->closure;
725 event_size0 = sizeof(*req);
726 }
19a15b93 727
97c18b7f 728 queue_event(handler->client, &e->event,
e205597d 729 &e->req, event_size0, r->data, length);
45ee3199
JF
730 return;
731
732 failed:
97c18b7f 733 kfree(r);
45ee3199 734 kfree(e);
281e2032
SR
735 kfree(fcp_frame);
736
737 if (!is_fcp_request(request))
db5d247a 738 fw_send_response(card, request, RCODE_CONFLICT_ERROR);
0244f573
SR
739
740 fw_card_put(card);
19a15b93
KH
741}
742
53dca511
SR
743static void release_address_handler(struct client *client,
744 struct client_resource *resource)
3964a449 745{
97c18b7f
SR
746 struct address_handler_resource *r =
747 container_of(resource, struct address_handler_resource, resource);
3964a449 748
97c18b7f
SR
749 fw_core_remove_address_handler(&r->handler);
750 kfree(r);
3964a449
KH
751}
752
6e95dea7 753static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
19a15b93 754{
6e95dea7 755 struct fw_cdev_allocate *a = &arg->allocate;
97c18b7f 756 struct address_handler_resource *r;
19a15b93 757 struct fw_address_region region;
45ee3199 758 int ret;
19a15b93 759
97c18b7f
SR
760 r = kmalloc(sizeof(*r), GFP_KERNEL);
761 if (r == NULL)
19a15b93
KH
762 return -ENOMEM;
763
6e95dea7
SR
764 region.start = a->offset;
765 region.end = a->offset + a->length;
766 r->handler.length = a->length;
97c18b7f 767 r->handler.address_callback = handle_request;
6e95dea7
SR
768 r->handler.callback_data = r;
769 r->closure = a->closure;
770 r->client = client;
19a15b93 771
97c18b7f 772 ret = fw_core_add_address_handler(&r->handler, &region);
3e0b5f0d 773 if (ret < 0) {
97c18b7f 774 kfree(r);
3e0b5f0d 775 return ret;
19a15b93
KH
776 }
777
97c18b7f
SR
778 r->resource.release = release_address_handler;
779 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
45ee3199 780 if (ret < 0) {
97c18b7f 781 release_address_handler(client, &r->resource);
45ee3199
JF
782 return ret;
783 }
6e95dea7 784 a->handle = r->resource.handle;
19a15b93
KH
785
786 return 0;
787}
788
6e95dea7 789static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
9472316b 790{
6e95dea7 791 return release_client_resource(client, arg->deallocate.handle,
45ee3199 792 release_address_handler, NULL);
9472316b
KH
793}
794
6e95dea7 795static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
19a15b93 796{
6e95dea7 797 struct fw_cdev_send_response *a = &arg->send_response;
3964a449 798 struct client_resource *resource;
97c18b7f 799 struct inbound_transaction_resource *r;
7e44c0b5 800 int ret = 0;
19a15b93 801
6e95dea7 802 if (release_client_resource(client, a->handle,
45ee3199 803 release_request, &resource) < 0)
19a15b93 804 return -EINVAL;
45ee3199 805
97c18b7f
SR
806 r = container_of(resource, struct inbound_transaction_resource,
807 resource);
281e2032
SR
808 if (is_fcp_request(r->request))
809 goto out;
810
a10c0ce7
CL
811 if (a->length != fw_get_response_length(r->request)) {
812 ret = -EINVAL;
813 kfree(r->request);
814 goto out;
815 }
816 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
281e2032
SR
817 ret = -EFAULT;
818 kfree(r->request);
819 goto out;
7e44c0b5 820 }
08bd34c9 821 fw_send_response(r->card, r->request, a->rcode);
7e44c0b5 822 out:
0244f573 823 fw_card_put(r->card);
19a15b93
KH
824 kfree(r);
825
7e44c0b5 826 return ret;
19a15b93
KH
827}
828
6e95dea7 829static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
5371842b 830{
02d37bed 831 fw_schedule_bus_reset(client->device->card, true,
6e95dea7 832 arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
02d37bed 833 return 0;
5371842b
KH
834}
835
3964a449
KH
836static void release_descriptor(struct client *client,
837 struct client_resource *resource)
838{
97c18b7f
SR
839 struct descriptor_resource *r =
840 container_of(resource, struct descriptor_resource, resource);
3964a449 841
97c18b7f
SR
842 fw_core_remove_descriptor(&r->descriptor);
843 kfree(r);
3964a449
KH
844}
845
6e95dea7 846static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
66dea3e5 847{
6e95dea7 848 struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
97c18b7f 849 struct descriptor_resource *r;
45ee3199 850 int ret;
66dea3e5 851
de487da8 852 /* Access policy: Allow this ioctl only on local nodes' device files. */
92368890 853 if (!client->device->is_local)
de487da8
SR
854 return -ENOSYS;
855
6e95dea7 856 if (a->length > 256)
66dea3e5
KH
857 return -EINVAL;
858
6e95dea7 859 r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
97c18b7f 860 if (r == NULL)
66dea3e5
KH
861 return -ENOMEM;
862
6e95dea7 863 if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
45ee3199
JF
864 ret = -EFAULT;
865 goto failed;
66dea3e5
KH
866 }
867
6e95dea7
SR
868 r->descriptor.length = a->length;
869 r->descriptor.immediate = a->immediate;
870 r->descriptor.key = a->key;
97c18b7f 871 r->descriptor.data = r->data;
66dea3e5 872
97c18b7f 873 ret = fw_core_add_descriptor(&r->descriptor);
45ee3199
JF
874 if (ret < 0)
875 goto failed;
66dea3e5 876
97c18b7f
SR
877 r->resource.release = release_descriptor;
878 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
45ee3199 879 if (ret < 0) {
97c18b7f 880 fw_core_remove_descriptor(&r->descriptor);
45ee3199
JF
881 goto failed;
882 }
6e95dea7 883 a->handle = r->resource.handle;
66dea3e5
KH
884
885 return 0;
45ee3199 886 failed:
97c18b7f 887 kfree(r);
45ee3199
JF
888
889 return ret;
66dea3e5
KH
890}
891
6e95dea7 892static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
66dea3e5 893{
6e95dea7 894 return release_client_resource(client, arg->remove_descriptor.handle,
45ee3199 895 release_descriptor, NULL);
66dea3e5
KH
896}
897
53dca511
SR
898static void iso_callback(struct fw_iso_context *context, u32 cycle,
899 size_t header_length, void *header, void *data)
19a15b93
KH
900{
901 struct client *client = data;
97c18b7f 902 struct iso_interrupt_event *e;
19a15b93 903
56d04cb1 904 e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
97c18b7f 905 if (e == NULL)
19a15b93
KH
906 return;
907
97c18b7f
SR
908 e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
909 e->interrupt.closure = client->iso_closure;
910 e->interrupt.cycle = cycle;
911 e->interrupt.header_length = header_length;
912 memcpy(e->interrupt.header, header, header_length);
913 queue_event(client, &e->event, &e->interrupt,
914 sizeof(e->interrupt) + header_length, NULL, 0);
19a15b93
KH
915}
916
6e95dea7 917static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
19a15b93 918{
6e95dea7 919 struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
24315c5e 920 struct fw_iso_context *context;
19a15b93 921
eb5b35a5
SR
922 BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT ||
923 FW_CDEV_ISO_CONTEXT_RECEIVE != FW_ISO_CONTEXT_RECEIVE);
924
6e95dea7 925 if (a->channel > 63)
21efb3cf
KH
926 return -EINVAL;
927
6e95dea7 928 switch (a->type) {
c70dc788 929 case FW_ISO_CONTEXT_RECEIVE:
6e95dea7 930 if (a->header_size < 4 || (a->header_size & 3))
c70dc788 931 return -EINVAL;
c70dc788
KH
932 break;
933
934 case FW_ISO_CONTEXT_TRANSMIT:
6e95dea7 935 if (a->speed > SCODE_3200)
c70dc788 936 return -EINVAL;
c70dc788
KH
937 break;
938
939 default:
21efb3cf 940 return -EINVAL;
c70dc788
KH
941 }
942
6e95dea7
SR
943 context = fw_iso_context_create(client->device->card, a->type,
944 a->channel, a->speed, a->header_size,
945 iso_callback, client);
24315c5e
KH
946 if (IS_ERR(context))
947 return PTR_ERR(context);
948
bdfe273e
CL
949 /* We only support one context at this time. */
950 spin_lock_irq(&client->lock);
951 if (client->iso_context != NULL) {
952 spin_unlock_irq(&client->lock);
953 fw_iso_context_destroy(context);
954 return -EBUSY;
955 }
6e95dea7 956 client->iso_closure = a->closure;
24315c5e 957 client->iso_context = context;
bdfe273e 958 spin_unlock_irq(&client->lock);
19a15b93 959
6e95dea7 960 a->handle = 0;
abaa5743 961
19a15b93
KH
962 return 0;
963}
964
1ca31ae7
KH
965/* Macros for decoding the iso packet control header. */
966#define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
967#define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
968#define GET_SKIP(v) (((v) >> 17) & 0x01)
7a100344
SR
969#define GET_TAG(v) (((v) >> 18) & 0x03)
970#define GET_SY(v) (((v) >> 20) & 0x0f)
1ca31ae7
KH
971#define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
972
6e95dea7 973static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
19a15b93 974{
6e95dea7 975 struct fw_cdev_queue_iso *a = &arg->queue_iso;
19a15b93 976 struct fw_cdev_iso_packet __user *p, *end, *next;
9b32d5f3 977 struct fw_iso_context *ctx = client->iso_context;
ef370ee7 978 unsigned long payload, buffer_end, header_length;
1ca31ae7 979 u32 control;
19a15b93
KH
980 int count;
981 struct {
982 struct fw_iso_packet packet;
983 u8 header[256];
984 } u;
985
6e95dea7 986 if (ctx == NULL || a->handle != 0)
19a15b93 987 return -EINVAL;
19a15b93 988
c781c06d
KH
989 /*
990 * If the user passes a non-NULL data pointer, has mmap()'ed
19a15b93
KH
991 * the iso buffer, and the pointer points inside the buffer,
992 * we setup the payload pointers accordingly. Otherwise we
9aad8125 993 * set them both to 0, which will still let packets with
19a15b93
KH
994 * payload_length == 0 through. In other words, if no packets
995 * use the indirect payload, the iso buffer need not be mapped
6e95dea7 996 * and the a->data pointer is ignored.
c781c06d 997 */
19a15b93 998
6e95dea7 999 payload = (unsigned long)a->data - client->vm_start;
ef370ee7 1000 buffer_end = client->buffer.page_count << PAGE_SHIFT;
6e95dea7 1001 if (a->data == 0 || client->buffer.pages == NULL ||
ef370ee7 1002 payload >= buffer_end) {
9aad8125 1003 payload = 0;
ef370ee7 1004 buffer_end = 0;
19a15b93
KH
1005 }
1006
6e95dea7 1007 p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
1ccc9147 1008
6e95dea7 1009 if (!access_ok(VERIFY_READ, p, a->size))
19a15b93
KH
1010 return -EFAULT;
1011
6e95dea7 1012 end = (void __user *)p + a->size;
19a15b93
KH
1013 count = 0;
1014 while (p < end) {
1ca31ae7 1015 if (get_user(control, &p->control))
19a15b93 1016 return -EFAULT;
1ca31ae7
KH
1017 u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
1018 u.packet.interrupt = GET_INTERRUPT(control);
1019 u.packet.skip = GET_SKIP(control);
1020 u.packet.tag = GET_TAG(control);
1021 u.packet.sy = GET_SY(control);
1022 u.packet.header_length = GET_HEADER_LENGTH(control);
295e3feb 1023
9b32d5f3 1024 if (ctx->type == FW_ISO_CONTEXT_TRANSMIT) {
385ab5bc
CL
1025 if (u.packet.header_length % 4 != 0)
1026 return -EINVAL;
295e3feb
KH
1027 header_length = u.packet.header_length;
1028 } else {
c781c06d
KH
1029 /*
1030 * We require that header_length is a multiple of
1031 * the fixed header size, ctx->header_size.
1032 */
9b32d5f3
KH
1033 if (ctx->header_size == 0) {
1034 if (u.packet.header_length > 0)
1035 return -EINVAL;
4ba1d9c0
CL
1036 } else if (u.packet.header_length == 0 ||
1037 u.packet.header_length % ctx->header_size != 0) {
295e3feb 1038 return -EINVAL;
9b32d5f3 1039 }
295e3feb
KH
1040 header_length = 0;
1041 }
1042
19a15b93 1043 next = (struct fw_cdev_iso_packet __user *)
295e3feb 1044 &p->header[header_length / 4];
19a15b93
KH
1045 if (next > end)
1046 return -EINVAL;
1047 if (__copy_from_user
295e3feb 1048 (u.packet.header, p->header, header_length))
19a15b93 1049 return -EFAULT;
98b6cbe8 1050 if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
19a15b93
KH
1051 u.packet.header_length + u.packet.payload_length > 0)
1052 return -EINVAL;
ef370ee7 1053 if (payload + u.packet.payload_length > buffer_end)
19a15b93
KH
1054 return -EINVAL;
1055
9b32d5f3
KH
1056 if (fw_iso_context_queue(ctx, &u.packet,
1057 &client->buffer, payload))
19a15b93
KH
1058 break;
1059
1060 p = next;
1061 payload += u.packet.payload_length;
1062 count++;
1063 }
1064
6e95dea7
SR
1065 a->size -= uptr_to_u64(p) - a->packets;
1066 a->packets = uptr_to_u64(p);
1067 a->data = client->vm_start + payload;
19a15b93
KH
1068
1069 return count;
1070}
1071
6e95dea7 1072static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
19a15b93 1073{
6e95dea7 1074 struct fw_cdev_start_iso *a = &arg->start_iso;
19a15b93 1075
eb5b35a5
SR
1076 BUILD_BUG_ON(
1077 FW_CDEV_ISO_CONTEXT_MATCH_TAG0 != FW_ISO_CONTEXT_MATCH_TAG0 ||
1078 FW_CDEV_ISO_CONTEXT_MATCH_TAG1 != FW_ISO_CONTEXT_MATCH_TAG1 ||
1079 FW_CDEV_ISO_CONTEXT_MATCH_TAG2 != FW_ISO_CONTEXT_MATCH_TAG2 ||
1080 FW_CDEV_ISO_CONTEXT_MATCH_TAG3 != FW_ISO_CONTEXT_MATCH_TAG3 ||
1081 FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS != FW_ISO_CONTEXT_MATCH_ALL_TAGS);
1082
6e95dea7 1083 if (client->iso_context == NULL || a->handle != 0)
abaa5743 1084 return -EINVAL;
fae60312 1085
6e95dea7
SR
1086 if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1087 (a->tags == 0 || a->tags > 15 || a->sync > 15))
1088 return -EINVAL;
eb0306ea 1089
6e95dea7
SR
1090 return fw_iso_context_start(client->iso_context,
1091 a->cycle, a->sync, a->tags);
19a15b93
KH
1092}
1093
6e95dea7 1094static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
b8295668 1095{
6e95dea7 1096 struct fw_cdev_stop_iso *a = &arg->stop_iso;
abaa5743 1097
6e95dea7 1098 if (client->iso_context == NULL || a->handle != 0)
abaa5743
KH
1099 return -EINVAL;
1100
b8295668
KH
1101 return fw_iso_context_stop(client->iso_context);
1102}
1103
6e95dea7 1104static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
a64408b9 1105{
6e95dea7 1106 struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
a64408b9 1107 struct fw_card *card = client->device->card;
abfe5a01 1108 struct timespec ts = {0, 0};
4a9bde9b 1109 u32 cycle_time;
abfe5a01 1110 int ret = 0;
a64408b9 1111
4a9bde9b 1112 local_irq_disable();
a64408b9 1113
0fcff4e3 1114 cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
abfe5a01 1115
6e95dea7 1116 switch (a->clk_id) {
abfe5a01
SR
1117 case CLOCK_REALTIME: getnstimeofday(&ts); break;
1118 case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
1119 case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
1120 default:
1121 ret = -EINVAL;
1122 }
a64408b9 1123
4a9bde9b 1124 local_irq_enable();
a64408b9 1125
6e95dea7
SR
1126 a->tv_sec = ts.tv_sec;
1127 a->tv_nsec = ts.tv_nsec;
1128 a->cycle_timer = cycle_time;
abfe5a01
SR
1129
1130 return ret;
1131}
1132
6e95dea7 1133static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
abfe5a01 1134{
6e95dea7 1135 struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
abfe5a01
SR
1136 struct fw_cdev_get_cycle_timer2 ct2;
1137
1138 ct2.clk_id = CLOCK_REALTIME;
6e95dea7 1139 ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
abfe5a01 1140
6e95dea7
SR
1141 a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1142 a->cycle_timer = ct2.cycle_timer;
4a9bde9b 1143
a64408b9
SR
1144 return 0;
1145}
1146
b1bda4cd
JFSR
1147static void iso_resource_work(struct work_struct *work)
1148{
1149 struct iso_resource_event *e;
1150 struct iso_resource *r =
1151 container_of(work, struct iso_resource, work.work);
1152 struct client *client = r->client;
1153 int generation, channel, bandwidth, todo;
1154 bool skip, free, success;
1155
1156 spin_lock_irq(&client->lock);
1157 generation = client->device->generation;
1158 todo = r->todo;
1159 /* Allow 1000ms grace period for other reallocations. */
1160 if (todo == ISO_RES_ALLOC &&
1161 time_is_after_jiffies(client->device->card->reset_jiffies + HZ)) {
9fb551bf 1162 schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
b1bda4cd
JFSR
1163 skip = true;
1164 } else {
1165 /* We could be called twice within the same generation. */
1166 skip = todo == ISO_RES_REALLOC &&
1167 r->generation == generation;
1168 }
1ec3c026
SR
1169 free = todo == ISO_RES_DEALLOC ||
1170 todo == ISO_RES_ALLOC_ONCE ||
1171 todo == ISO_RES_DEALLOC_ONCE;
b1bda4cd
JFSR
1172 r->generation = generation;
1173 spin_unlock_irq(&client->lock);
1174
1175 if (skip)
1176 goto out;
1177
1178 bandwidth = r->bandwidth;
1179
1180 fw_iso_resource_manage(client->device->card, generation,
1181 r->channels, &channel, &bandwidth,
1ec3c026
SR
1182 todo == ISO_RES_ALLOC ||
1183 todo == ISO_RES_REALLOC ||
6fdc0370
SR
1184 todo == ISO_RES_ALLOC_ONCE,
1185 r->transaction_data);
b1bda4cd
JFSR
1186 /*
1187 * Is this generation outdated already? As long as this resource sticks
1188 * in the idr, it will be scheduled again for a newer generation or at
1189 * shutdown.
1190 */
1191 if (channel == -EAGAIN &&
1192 (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
1193 goto out;
1194
1195 success = channel >= 0 || bandwidth > 0;
1196
1197 spin_lock_irq(&client->lock);
1198 /*
1199 * Transit from allocation to reallocation, except if the client
1200 * requested deallocation in the meantime.
1201 */
1202 if (r->todo == ISO_RES_ALLOC)
1203 r->todo = ISO_RES_REALLOC;
1204 /*
1205 * Allocation or reallocation failure? Pull this resource out of the
1206 * idr and prepare for deletion, unless the client is shutting down.
1207 */
1208 if (r->todo == ISO_RES_REALLOC && !success &&
1209 !client->in_shutdown &&
1210 idr_find(&client->resource_idr, r->resource.handle)) {
1211 idr_remove(&client->resource_idr, r->resource.handle);
1212 client_put(client);
1213 free = true;
1214 }
1215 spin_unlock_irq(&client->lock);
1216
1217 if (todo == ISO_RES_ALLOC && channel >= 0)
5d9cb7d2 1218 r->channels = 1ULL << channel;
b1bda4cd
JFSR
1219
1220 if (todo == ISO_RES_REALLOC && success)
1221 goto out;
1222
1ec3c026 1223 if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
b1bda4cd
JFSR
1224 e = r->e_alloc;
1225 r->e_alloc = NULL;
1226 } else {
1227 e = r->e_dealloc;
1228 r->e_dealloc = NULL;
1229 }
e21fcf79
SR
1230 e->iso_resource.handle = r->resource.handle;
1231 e->iso_resource.channel = channel;
1232 e->iso_resource.bandwidth = bandwidth;
b1bda4cd
JFSR
1233
1234 queue_event(client, &e->event,
e21fcf79 1235 &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
b1bda4cd
JFSR
1236
1237 if (free) {
1238 cancel_delayed_work(&r->work);
1239 kfree(r->e_alloc);
1240 kfree(r->e_dealloc);
1241 kfree(r);
1242 }
1243 out:
1244 client_put(client);
1245}
1246
b1bda4cd
JFSR
1247static void release_iso_resource(struct client *client,
1248 struct client_resource *resource)
1249{
1250 struct iso_resource *r =
1251 container_of(resource, struct iso_resource, resource);
1252
1253 spin_lock_irq(&client->lock);
1254 r->todo = ISO_RES_DEALLOC;
9fb551bf 1255 schedule_iso_resource(r, 0);
b1bda4cd
JFSR
1256 spin_unlock_irq(&client->lock);
1257}
1258
1ec3c026
SR
1259static int init_iso_resource(struct client *client,
1260 struct fw_cdev_allocate_iso_resource *request, int todo)
b1bda4cd 1261{
b1bda4cd
JFSR
1262 struct iso_resource_event *e1, *e2;
1263 struct iso_resource *r;
1264 int ret;
1265
1266 if ((request->channels == 0 && request->bandwidth == 0) ||
1267 request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
1268 request->bandwidth < 0)
1269 return -EINVAL;
1270
1271 r = kmalloc(sizeof(*r), GFP_KERNEL);
1272 e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
1273 e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
1274 if (r == NULL || e1 == NULL || e2 == NULL) {
1275 ret = -ENOMEM;
1276 goto fail;
1277 }
1278
1279 INIT_DELAYED_WORK(&r->work, iso_resource_work);
1280 r->client = client;
1ec3c026 1281 r->todo = todo;
b1bda4cd
JFSR
1282 r->generation = -1;
1283 r->channels = request->channels;
1284 r->bandwidth = request->bandwidth;
1285 r->e_alloc = e1;
1286 r->e_dealloc = e2;
1287
e21fcf79
SR
1288 e1->iso_resource.closure = request->closure;
1289 e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
1290 e2->iso_resource.closure = request->closure;
1291 e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
b1bda4cd 1292
1ec3c026
SR
1293 if (todo == ISO_RES_ALLOC) {
1294 r->resource.release = release_iso_resource;
1295 ret = add_client_resource(client, &r->resource, GFP_KERNEL);
81610b8f
SR
1296 if (ret < 0)
1297 goto fail;
1ec3c026
SR
1298 } else {
1299 r->resource.release = NULL;
1300 r->resource.handle = -1;
9fb551bf 1301 schedule_iso_resource(r, 0);
1ec3c026 1302 }
b1bda4cd
JFSR
1303 request->handle = r->resource.handle;
1304
1305 return 0;
1306 fail:
1307 kfree(r);
1308 kfree(e1);
1309 kfree(e2);
1310
1311 return ret;
1312}
1313
6e95dea7
SR
1314static int ioctl_allocate_iso_resource(struct client *client,
1315 union ioctl_arg *arg)
1ec3c026 1316{
6e95dea7
SR
1317 return init_iso_resource(client,
1318 &arg->allocate_iso_resource, ISO_RES_ALLOC);
1ec3c026
SR
1319}
1320
6e95dea7
SR
1321static int ioctl_deallocate_iso_resource(struct client *client,
1322 union ioctl_arg *arg)
b1bda4cd 1323{
6e95dea7
SR
1324 return release_client_resource(client,
1325 arg->deallocate.handle, release_iso_resource, NULL);
b1bda4cd
JFSR
1326}
1327
6e95dea7
SR
1328static int ioctl_allocate_iso_resource_once(struct client *client,
1329 union ioctl_arg *arg)
1ec3c026 1330{
6e95dea7
SR
1331 return init_iso_resource(client,
1332 &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
1ec3c026
SR
1333}
1334
6e95dea7
SR
1335static int ioctl_deallocate_iso_resource_once(struct client *client,
1336 union ioctl_arg *arg)
1ec3c026 1337{
6e95dea7
SR
1338 return init_iso_resource(client,
1339 &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
1ec3c026
SR
1340}
1341
c8a25900
SR
1342/*
1343 * Returns a speed code: Maximum speed to or from this device,
1344 * limited by the device's link speed, the local node's link speed,
1345 * and all PHY port speeds between the two links.
1346 */
6e95dea7 1347static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
33580a3e 1348{
c8a25900 1349 return client->device->max_speed;
33580a3e
SR
1350}
1351
6e95dea7
SR
1352static int ioctl_send_broadcast_request(struct client *client,
1353 union ioctl_arg *arg)
acfe8333 1354{
6e95dea7 1355 struct fw_cdev_send_request *a = &arg->send_request;
acfe8333 1356
6e95dea7 1357 switch (a->tcode) {
acfe8333
JFSR
1358 case TCODE_WRITE_QUADLET_REQUEST:
1359 case TCODE_WRITE_BLOCK_REQUEST:
1360 break;
1361 default:
1362 return -EINVAL;
1363 }
1364
1566f3dc 1365 /* Security policy: Only allow accesses to Units Space. */
6e95dea7 1366 if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
1566f3dc
SR
1367 return -EACCES;
1368
6e95dea7 1369 return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
acfe8333
JFSR
1370}
1371
6e95dea7 1372static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
f8c2287c 1373{
6e95dea7 1374 struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
18e9b10f
SR
1375 struct fw_cdev_send_request request;
1376 int dest;
f8c2287c 1377
6e95dea7
SR
1378 if (a->speed > client->device->card->link_speed ||
1379 a->length > 1024 << a->speed)
18e9b10f 1380 return -EIO;
f8c2287c 1381
6e95dea7 1382 if (a->tag > 3 || a->channel > 63 || a->sy > 15)
18e9b10f
SR
1383 return -EINVAL;
1384
6e95dea7 1385 dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
18e9b10f 1386 request.tcode = TCODE_STREAM_DATA;
6e95dea7
SR
1387 request.length = a->length;
1388 request.closure = a->closure;
1389 request.data = a->data;
1390 request.generation = a->generation;
18e9b10f 1391
6e95dea7 1392 return init_request(client, &request, dest, a->speed);
f8c2287c
JF
1393}
1394
850bb6f2
SR
1395static void outbound_phy_packet_callback(struct fw_packet *packet,
1396 struct fw_card *card, int status)
1397{
1398 struct outbound_phy_packet_event *e =
1399 container_of(packet, struct outbound_phy_packet_event, p);
1400
1401 switch (status) {
1402 /* expected: */
1403 case ACK_COMPLETE: e->phy_packet.rcode = RCODE_COMPLETE; break;
1404 /* should never happen with PHY packets: */
1405 case ACK_PENDING: e->phy_packet.rcode = RCODE_COMPLETE; break;
1406 case ACK_BUSY_X:
1407 case ACK_BUSY_A:
1408 case ACK_BUSY_B: e->phy_packet.rcode = RCODE_BUSY; break;
1409 case ACK_DATA_ERROR: e->phy_packet.rcode = RCODE_DATA_ERROR; break;
1410 case ACK_TYPE_ERROR: e->phy_packet.rcode = RCODE_TYPE_ERROR; break;
1411 /* stale generation; cancelled; on certain controllers: no ack */
1412 default: e->phy_packet.rcode = status; break;
1413 }
1414
1415 queue_event(e->client, &e->event,
1416 &e->phy_packet, sizeof(e->phy_packet), NULL, 0);
1417 client_put(e->client);
1418}
1419
1420static int ioctl_send_phy_packet(struct client *client, union ioctl_arg *arg)
1421{
1422 struct fw_cdev_send_phy_packet *a = &arg->send_phy_packet;
1423 struct fw_card *card = client->device->card;
1424 struct outbound_phy_packet_event *e;
1425
1426 /* Access policy: Allow this ioctl only on local nodes' device files. */
1427 if (!client->device->is_local)
1428 return -ENOSYS;
1429
1430 e = kzalloc(sizeof(*e), GFP_KERNEL);
1431 if (e == NULL)
1432 return -ENOMEM;
1433
1434 client_get(client);
1435 e->client = client;
1436 e->p.speed = SCODE_100;
1437 e->p.generation = a->generation;
1438 e->p.header[0] = a->data[0];
1439 e->p.header[1] = a->data[1];
1440 e->p.header_length = 8;
1441 e->p.callback = outbound_phy_packet_callback;
1442 e->phy_packet.closure = a->closure;
1443 e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_SENT;
1444
1445 card->driver->send_request(card, &e->p);
1446
1447 return 0;
1448}
1449
6e95dea7 1450static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
b9dc61cf
SR
1451 [0x00] = ioctl_get_info,
1452 [0x01] = ioctl_send_request,
1453 [0x02] = ioctl_allocate,
1454 [0x03] = ioctl_deallocate,
1455 [0x04] = ioctl_send_response,
1456 [0x05] = ioctl_initiate_bus_reset,
1457 [0x06] = ioctl_add_descriptor,
1458 [0x07] = ioctl_remove_descriptor,
1459 [0x08] = ioctl_create_iso_context,
1460 [0x09] = ioctl_queue_iso,
1461 [0x0a] = ioctl_start_iso,
1462 [0x0b] = ioctl_stop_iso,
1463 [0x0c] = ioctl_get_cycle_timer,
1464 [0x0d] = ioctl_allocate_iso_resource,
1465 [0x0e] = ioctl_deallocate_iso_resource,
1466 [0x0f] = ioctl_allocate_iso_resource_once,
1467 [0x10] = ioctl_deallocate_iso_resource_once,
1468 [0x11] = ioctl_get_speed,
1469 [0x12] = ioctl_send_broadcast_request,
1470 [0x13] = ioctl_send_stream_packet,
1471 [0x14] = ioctl_get_cycle_timer2,
850bb6f2 1472 [0x15] = ioctl_send_phy_packet,
4f259223
KH
1473};
1474
53dca511
SR
1475static int dispatch_ioctl(struct client *client,
1476 unsigned int cmd, void __user *arg)
19a15b93 1477{
6e95dea7 1478 union ioctl_arg buffer;
2dbd7d7e 1479 int ret;
4f259223 1480
64582298
SR
1481 if (fw_device_is_shutdown(client->device))
1482 return -ENODEV;
1483
4f259223 1484 if (_IOC_TYPE(cmd) != '#' ||
9cac00b8
SR
1485 _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1486 _IOC_SIZE(cmd) > sizeof(buffer))
19a15b93 1487 return -EINVAL;
4f259223 1488
9cac00b8
SR
1489 if (_IOC_DIR(cmd) == _IOC_READ)
1490 memset(&buffer, 0, _IOC_SIZE(cmd));
1491
1492 if (_IOC_DIR(cmd) & _IOC_WRITE)
1493 if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
4f259223 1494 return -EFAULT;
4f259223 1495
6e95dea7 1496 ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
2dbd7d7e
SR
1497 if (ret < 0)
1498 return ret;
4f259223 1499
9cac00b8
SR
1500 if (_IOC_DIR(cmd) & _IOC_READ)
1501 if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
4f259223 1502 return -EFAULT;
4f259223 1503
2dbd7d7e 1504 return ret;
19a15b93
KH
1505}
1506
53dca511
SR
1507static long fw_device_op_ioctl(struct file *file,
1508 unsigned int cmd, unsigned long arg)
19a15b93 1509{
64582298 1510 return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
19a15b93
KH
1511}
1512
1513#ifdef CONFIG_COMPAT
53dca511
SR
1514static long fw_device_op_compat_ioctl(struct file *file,
1515 unsigned int cmd, unsigned long arg)
19a15b93 1516{
64582298 1517 return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
19a15b93
KH
1518}
1519#endif
1520
1521static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1522{
1523 struct client *client = file->private_data;
9aad8125
KH
1524 enum dma_data_direction direction;
1525 unsigned long size;
2dbd7d7e 1526 int page_count, ret;
9aad8125 1527
551f4cb9
JF
1528 if (fw_device_is_shutdown(client->device))
1529 return -ENODEV;
1530
9aad8125
KH
1531 /* FIXME: We could support multiple buffers, but we don't. */
1532 if (client->buffer.pages != NULL)
1533 return -EBUSY;
1534
1535 if (!(vma->vm_flags & VM_SHARED))
1536 return -EINVAL;
19a15b93 1537
9aad8125 1538 if (vma->vm_start & ~PAGE_MASK)
19a15b93
KH
1539 return -EINVAL;
1540
1541 client->vm_start = vma->vm_start;
9aad8125
KH
1542 size = vma->vm_end - vma->vm_start;
1543 page_count = size >> PAGE_SHIFT;
1544 if (size & ~PAGE_MASK)
1545 return -EINVAL;
1546
1547 if (vma->vm_flags & VM_WRITE)
1548 direction = DMA_TO_DEVICE;
1549 else
1550 direction = DMA_FROM_DEVICE;
1551
2dbd7d7e
SR
1552 ret = fw_iso_buffer_init(&client->buffer, client->device->card,
1553 page_count, direction);
1554 if (ret < 0)
1555 return ret;
19a15b93 1556
2dbd7d7e
SR
1557 ret = fw_iso_buffer_map(&client->buffer, vma);
1558 if (ret < 0)
9aad8125
KH
1559 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1560
2dbd7d7e 1561 return ret;
19a15b93
KH
1562}
1563
45ee3199
JF
1564static int shutdown_resource(int id, void *p, void *data)
1565{
e21fcf79 1566 struct client_resource *resource = p;
45ee3199
JF
1567 struct client *client = data;
1568
e21fcf79 1569 resource->release(client, resource);
fb443036 1570 client_put(client);
45ee3199
JF
1571
1572 return 0;
1573}
1574
19a15b93
KH
1575static int fw_device_op_release(struct inode *inode, struct file *file)
1576{
1577 struct client *client = file->private_data;
e21fcf79 1578 struct event *event, *next_event;
19a15b93 1579
97811e34
SR
1580 mutex_lock(&client->device->client_list_mutex);
1581 list_del(&client->link);
1582 mutex_unlock(&client->device->client_list_mutex);
1583
19a15b93
KH
1584 if (client->iso_context)
1585 fw_iso_context_destroy(client->iso_context);
1586
36a755cf
SR
1587 if (client->buffer.pages)
1588 fw_iso_buffer_destroy(&client->buffer, client->device->card);
1589
45ee3199 1590 /* Freeze client->resource_idr and client->event_list */
3ba94986 1591 spin_lock_irq(&client->lock);
45ee3199 1592 client->in_shutdown = true;
3ba94986 1593 spin_unlock_irq(&client->lock);
66dea3e5 1594
45ee3199
JF
1595 idr_for_each(&client->resource_idr, shutdown_resource, client);
1596 idr_remove_all(&client->resource_idr);
1597 idr_destroy(&client->resource_idr);
28cf6a04 1598
e21fcf79
SR
1599 list_for_each_entry_safe(event, next_event, &client->event_list, link)
1600 kfree(event);
19a15b93 1601
fb443036 1602 client_put(client);
19a15b93
KH
1603
1604 return 0;
1605}
1606
1607static unsigned int fw_device_op_poll(struct file *file, poll_table * pt)
1608{
1609 struct client *client = file->private_data;
2603bf21 1610 unsigned int mask = 0;
19a15b93
KH
1611
1612 poll_wait(file, &client->wait, pt);
1613
2603bf21
KH
1614 if (fw_device_is_shutdown(client->device))
1615 mask |= POLLHUP | POLLERR;
19a15b93 1616 if (!list_empty(&client->event_list))
2603bf21
KH
1617 mask |= POLLIN | POLLRDNORM;
1618
1619 return mask;
19a15b93
KH
1620}
1621
21ebcd12 1622const struct file_operations fw_device_ops = {
19a15b93 1623 .owner = THIS_MODULE,
3ac26b2e 1624 .llseek = no_llseek,
19a15b93
KH
1625 .open = fw_device_op_open,
1626 .read = fw_device_op_read,
1627 .unlocked_ioctl = fw_device_op_ioctl,
19a15b93 1628 .mmap = fw_device_op_mmap,
3ac26b2e
SR
1629 .release = fw_device_op_release,
1630 .poll = fw_device_op_poll,
19a15b93 1631#ifdef CONFIG_COMPAT
5af4e5ea 1632 .compat_ioctl = fw_device_op_compat_ioctl,
19a15b93
KH
1633#endif
1634};
This page took 0.53032 seconds and 5 git commands to generate.