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