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