xref: /linux-6.15/drivers/firewire/core-cdev.c (revision 044ce581)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Char device for device raw access
4  *
5  * Copyright (C) 2005-2007  Kristian Hoegsberg <[email protected]>
6  */
7 
8 #include <linux/bug.h>
9 #include <linux/compat.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/firewire.h>
16 #include <linux/firewire-cdev.h>
17 #include <linux/idr.h>
18 #include <linux/irqflags.h>
19 #include <linux/jiffies.h>
20 #include <linux/kernel.h>
21 #include <linux/kref.h>
22 #include <linux/mm.h>
23 #include <linux/module.h>
24 #include <linux/mutex.h>
25 #include <linux/poll.h>
26 #include <linux/sched.h> /* required for linux/wait.h */
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/string.h>
30 #include <linux/time.h>
31 #include <linux/uaccess.h>
32 #include <linux/vmalloc.h>
33 #include <linux/wait.h>
34 #include <linux/workqueue.h>
35 
36 
37 #include "core.h"
38 #include <trace/events/firewire.h>
39 
40 #include "packet-header-definitions.h"
41 
42 /*
43  * ABI version history is documented in linux/firewire-cdev.h.
44  */
45 #define FW_CDEV_KERNEL_VERSION			5
46 #define FW_CDEV_VERSION_EVENT_REQUEST2		4
47 #define FW_CDEV_VERSION_ALLOCATE_REGION_END	4
48 #define FW_CDEV_VERSION_AUTO_FLUSH_ISO_OVERFLOW	5
49 #define FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP	6
50 
51 struct client {
52 	u32 version;
53 	struct fw_device *device;
54 
55 	spinlock_t lock;
56 	bool in_shutdown;
57 	struct idr resource_idr;
58 	struct list_head event_list;
59 	wait_queue_head_t wait;
60 	wait_queue_head_t tx_flush_wait;
61 	u64 bus_reset_closure;
62 
63 	struct fw_iso_context *iso_context;
64 	u64 iso_closure;
65 	struct fw_iso_buffer buffer;
66 	unsigned long vm_start;
67 	bool buffer_is_mapped;
68 
69 	struct list_head phy_receiver_link;
70 	u64 phy_receiver_closure;
71 
72 	struct list_head link;
73 	struct kref kref;
74 };
75 
76 static inline void client_get(struct client *client)
77 {
78 	kref_get(&client->kref);
79 }
80 
81 static void client_release(struct kref *kref)
82 {
83 	struct client *client = container_of(kref, struct client, kref);
84 
85 	fw_device_put(client->device);
86 	kfree(client);
87 }
88 
89 static void client_put(struct client *client)
90 {
91 	kref_put(&client->kref, client_release);
92 }
93 
94 struct client_resource;
95 typedef void (*client_resource_release_fn_t)(struct client *,
96 					     struct client_resource *);
97 struct client_resource {
98 	client_resource_release_fn_t release;
99 	int handle;
100 };
101 
102 struct address_handler_resource {
103 	struct client_resource resource;
104 	struct fw_address_handler handler;
105 	__u64 closure;
106 	struct client *client;
107 };
108 
109 struct outbound_transaction_resource {
110 	struct client_resource resource;
111 	struct fw_transaction transaction;
112 };
113 
114 struct inbound_transaction_resource {
115 	struct client_resource resource;
116 	struct fw_card *card;
117 	struct fw_request *request;
118 	bool is_fcp;
119 	void *data;
120 	size_t length;
121 };
122 
123 struct descriptor_resource {
124 	struct client_resource resource;
125 	struct fw_descriptor descriptor;
126 	u32 data[];
127 };
128 
129 struct iso_resource {
130 	struct client_resource resource;
131 	struct client *client;
132 	/* Schedule work and access todo only with client->lock held. */
133 	struct delayed_work work;
134 	enum {ISO_RES_ALLOC, ISO_RES_REALLOC, ISO_RES_DEALLOC,
135 	      ISO_RES_ALLOC_ONCE, ISO_RES_DEALLOC_ONCE,} todo;
136 	int generation;
137 	u64 channels;
138 	s32 bandwidth;
139 	struct iso_resource_event *e_alloc, *e_dealloc;
140 };
141 
142 static void release_iso_resource(struct client *, struct client_resource *);
143 
144 static void schedule_iso_resource(struct iso_resource *r, unsigned long delay)
145 {
146 	client_get(r->client);
147 	if (!queue_delayed_work(fw_workqueue, &r->work, delay))
148 		client_put(r->client);
149 }
150 
151 static void schedule_if_iso_resource(struct client_resource *resource)
152 {
153 	if (resource->release == release_iso_resource)
154 		schedule_iso_resource(container_of(resource,
155 					struct iso_resource, resource), 0);
156 }
157 
158 /*
159  * dequeue_event() just kfree()'s the event, so the event has to be
160  * the first field in a struct XYZ_event.
161  */
162 struct event {
163 	struct { void *data; size_t size; } v[2];
164 	struct list_head link;
165 };
166 
167 struct bus_reset_event {
168 	struct event event;
169 	struct fw_cdev_event_bus_reset reset;
170 };
171 
172 struct outbound_transaction_event {
173 	struct event event;
174 	struct client *client;
175 	struct outbound_transaction_resource r;
176 	union {
177 		struct fw_cdev_event_response without_tstamp;
178 		struct fw_cdev_event_response2 with_tstamp;
179 	} rsp;
180 };
181 
182 struct inbound_transaction_event {
183 	struct event event;
184 	union {
185 		struct fw_cdev_event_request request;
186 		struct fw_cdev_event_request2 request2;
187 		struct fw_cdev_event_request3 with_tstamp;
188 	} req;
189 };
190 
191 struct iso_interrupt_event {
192 	struct event event;
193 	struct fw_cdev_event_iso_interrupt interrupt;
194 };
195 
196 struct iso_interrupt_mc_event {
197 	struct event event;
198 	struct fw_cdev_event_iso_interrupt_mc interrupt;
199 };
200 
201 struct iso_resource_event {
202 	struct event event;
203 	struct fw_cdev_event_iso_resource iso_resource;
204 };
205 
206 struct outbound_phy_packet_event {
207 	struct event event;
208 	struct client *client;
209 	struct fw_packet p;
210 	union {
211 		struct fw_cdev_event_phy_packet without_tstamp;
212 		struct fw_cdev_event_phy_packet2 with_tstamp;
213 	} phy_packet;
214 };
215 
216 struct inbound_phy_packet_event {
217 	struct event event;
218 	union {
219 		struct fw_cdev_event_phy_packet without_tstamp;
220 		struct fw_cdev_event_phy_packet2 with_tstamp;
221 	} phy_packet;
222 };
223 
224 #ifdef CONFIG_COMPAT
225 static void __user *u64_to_uptr(u64 value)
226 {
227 	if (in_compat_syscall())
228 		return compat_ptr(value);
229 	else
230 		return (void __user *)(unsigned long)value;
231 }
232 
233 static u64 uptr_to_u64(void __user *ptr)
234 {
235 	if (in_compat_syscall())
236 		return ptr_to_compat(ptr);
237 	else
238 		return (u64)(unsigned long)ptr;
239 }
240 #else
241 static inline void __user *u64_to_uptr(u64 value)
242 {
243 	return (void __user *)(unsigned long)value;
244 }
245 
246 static inline u64 uptr_to_u64(void __user *ptr)
247 {
248 	return (u64)(unsigned long)ptr;
249 }
250 #endif /* CONFIG_COMPAT */
251 
252 static int fw_device_op_open(struct inode *inode, struct file *file)
253 {
254 	struct fw_device *device;
255 	struct client *client;
256 
257 	device = fw_device_get_by_devt(inode->i_rdev);
258 	if (device == NULL)
259 		return -ENODEV;
260 
261 	if (fw_device_is_shutdown(device)) {
262 		fw_device_put(device);
263 		return -ENODEV;
264 	}
265 
266 	client = kzalloc(sizeof(*client), GFP_KERNEL);
267 	if (client == NULL) {
268 		fw_device_put(device);
269 		return -ENOMEM;
270 	}
271 
272 	client->device = device;
273 	spin_lock_init(&client->lock);
274 	idr_init(&client->resource_idr);
275 	INIT_LIST_HEAD(&client->event_list);
276 	init_waitqueue_head(&client->wait);
277 	init_waitqueue_head(&client->tx_flush_wait);
278 	INIT_LIST_HEAD(&client->phy_receiver_link);
279 	INIT_LIST_HEAD(&client->link);
280 	kref_init(&client->kref);
281 
282 	file->private_data = client;
283 
284 	return nonseekable_open(inode, file);
285 }
286 
287 static void queue_event(struct client *client, struct event *event,
288 			void *data0, size_t size0, void *data1, size_t size1)
289 {
290 	unsigned long flags;
291 
292 	event->v[0].data = data0;
293 	event->v[0].size = size0;
294 	event->v[1].data = data1;
295 	event->v[1].size = size1;
296 
297 	spin_lock_irqsave(&client->lock, flags);
298 	if (client->in_shutdown)
299 		kfree(event);
300 	else
301 		list_add_tail(&event->link, &client->event_list);
302 	spin_unlock_irqrestore(&client->lock, flags);
303 
304 	wake_up_interruptible(&client->wait);
305 }
306 
307 static int dequeue_event(struct client *client,
308 			 char __user *buffer, size_t count)
309 {
310 	struct event *event;
311 	size_t size, total;
312 	int i, ret;
313 
314 	ret = wait_event_interruptible(client->wait,
315 			!list_empty(&client->event_list) ||
316 			fw_device_is_shutdown(client->device));
317 	if (ret < 0)
318 		return ret;
319 
320 	if (list_empty(&client->event_list) &&
321 		       fw_device_is_shutdown(client->device))
322 		return -ENODEV;
323 
324 	spin_lock_irq(&client->lock);
325 	event = list_first_entry(&client->event_list, struct event, link);
326 	list_del(&event->link);
327 	spin_unlock_irq(&client->lock);
328 
329 	total = 0;
330 	for (i = 0; i < ARRAY_SIZE(event->v) && total < count; i++) {
331 		size = min(event->v[i].size, count - total);
332 		if (copy_to_user(buffer + total, event->v[i].data, size)) {
333 			ret = -EFAULT;
334 			goto out;
335 		}
336 		total += size;
337 	}
338 	ret = total;
339 
340  out:
341 	kfree(event);
342 
343 	return ret;
344 }
345 
346 static ssize_t fw_device_op_read(struct file *file, char __user *buffer,
347 				 size_t count, loff_t *offset)
348 {
349 	struct client *client = file->private_data;
350 
351 	return dequeue_event(client, buffer, count);
352 }
353 
354 static void fill_bus_reset_event(struct fw_cdev_event_bus_reset *event,
355 				 struct client *client)
356 {
357 	struct fw_card *card = client->device->card;
358 
359 	spin_lock_irq(&card->lock);
360 
361 	event->closure	     = client->bus_reset_closure;
362 	event->type          = FW_CDEV_EVENT_BUS_RESET;
363 	event->generation    = client->device->generation;
364 	event->node_id       = client->device->node_id;
365 	event->local_node_id = card->local_node->node_id;
366 	event->bm_node_id    = card->bm_node_id;
367 	event->irm_node_id   = card->irm_node->node_id;
368 	event->root_node_id  = card->root_node->node_id;
369 
370 	spin_unlock_irq(&card->lock);
371 }
372 
373 static void for_each_client(struct fw_device *device,
374 			    void (*callback)(struct client *client))
375 {
376 	struct client *c;
377 
378 	guard(mutex)(&device->client_list_mutex);
379 
380 	list_for_each_entry(c, &device->client_list, link)
381 		callback(c);
382 }
383 
384 static int schedule_reallocations(int id, void *p, void *data)
385 {
386 	schedule_if_iso_resource(p);
387 
388 	return 0;
389 }
390 
391 static void queue_bus_reset_event(struct client *client)
392 {
393 	struct bus_reset_event *e;
394 
395 	e = kzalloc(sizeof(*e), GFP_KERNEL);
396 	if (e == NULL)
397 		return;
398 
399 	fill_bus_reset_event(&e->reset, client);
400 
401 	queue_event(client, &e->event,
402 		    &e->reset, sizeof(e->reset), NULL, 0);
403 
404 	spin_lock_irq(&client->lock);
405 	idr_for_each(&client->resource_idr, schedule_reallocations, client);
406 	spin_unlock_irq(&client->lock);
407 }
408 
409 void fw_device_cdev_update(struct fw_device *device)
410 {
411 	for_each_client(device, queue_bus_reset_event);
412 }
413 
414 static void wake_up_client(struct client *client)
415 {
416 	wake_up_interruptible(&client->wait);
417 }
418 
419 void fw_device_cdev_remove(struct fw_device *device)
420 {
421 	for_each_client(device, wake_up_client);
422 }
423 
424 union ioctl_arg {
425 	struct fw_cdev_get_info			get_info;
426 	struct fw_cdev_send_request		send_request;
427 	struct fw_cdev_allocate			allocate;
428 	struct fw_cdev_deallocate		deallocate;
429 	struct fw_cdev_send_response		send_response;
430 	struct fw_cdev_initiate_bus_reset	initiate_bus_reset;
431 	struct fw_cdev_add_descriptor		add_descriptor;
432 	struct fw_cdev_remove_descriptor	remove_descriptor;
433 	struct fw_cdev_create_iso_context	create_iso_context;
434 	struct fw_cdev_queue_iso		queue_iso;
435 	struct fw_cdev_start_iso		start_iso;
436 	struct fw_cdev_stop_iso			stop_iso;
437 	struct fw_cdev_get_cycle_timer		get_cycle_timer;
438 	struct fw_cdev_allocate_iso_resource	allocate_iso_resource;
439 	struct fw_cdev_send_stream_packet	send_stream_packet;
440 	struct fw_cdev_get_cycle_timer2		get_cycle_timer2;
441 	struct fw_cdev_send_phy_packet		send_phy_packet;
442 	struct fw_cdev_receive_phy_packets	receive_phy_packets;
443 	struct fw_cdev_set_iso_channels		set_iso_channels;
444 	struct fw_cdev_flush_iso		flush_iso;
445 };
446 
447 static int ioctl_get_info(struct client *client, union ioctl_arg *arg)
448 {
449 	struct fw_cdev_get_info *a = &arg->get_info;
450 	struct fw_cdev_event_bus_reset bus_reset;
451 	unsigned long ret = 0;
452 
453 	client->version = a->version;
454 	a->version = FW_CDEV_KERNEL_VERSION;
455 	a->card = client->device->card->index;
456 
457 	down_read(&fw_device_rwsem);
458 
459 	if (a->rom != 0) {
460 		size_t want = a->rom_length;
461 		size_t have = client->device->config_rom_length * 4;
462 
463 		ret = copy_to_user(u64_to_uptr(a->rom),
464 				   client->device->config_rom, min(want, have));
465 	}
466 	a->rom_length = client->device->config_rom_length * 4;
467 
468 	up_read(&fw_device_rwsem);
469 
470 	if (ret != 0)
471 		return -EFAULT;
472 
473 	guard(mutex)(&client->device->client_list_mutex);
474 
475 	client->bus_reset_closure = a->bus_reset_closure;
476 	if (a->bus_reset != 0) {
477 		fill_bus_reset_event(&bus_reset, client);
478 		/* unaligned size of bus_reset is 36 bytes */
479 		ret = copy_to_user(u64_to_uptr(a->bus_reset), &bus_reset, 36);
480 	}
481 	if (ret == 0 && list_empty(&client->link))
482 		list_add_tail(&client->link, &client->device->client_list);
483 
484 	return ret ? -EFAULT : 0;
485 }
486 
487 static int add_client_resource(struct client *client,
488 			       struct client_resource *resource, gfp_t gfp_mask)
489 {
490 	bool preload = gfpflags_allow_blocking(gfp_mask);
491 	unsigned long flags;
492 	int ret;
493 
494 	if (preload)
495 		idr_preload(gfp_mask);
496 	spin_lock_irqsave(&client->lock, flags);
497 
498 	if (client->in_shutdown)
499 		ret = -ECANCELED;
500 	else
501 		ret = idr_alloc(&client->resource_idr, resource, 0, 0,
502 				GFP_NOWAIT);
503 	if (ret >= 0) {
504 		resource->handle = ret;
505 		client_get(client);
506 		schedule_if_iso_resource(resource);
507 	}
508 
509 	spin_unlock_irqrestore(&client->lock, flags);
510 	if (preload)
511 		idr_preload_end();
512 
513 	return ret < 0 ? ret : 0;
514 }
515 
516 static int release_client_resource(struct client *client, u32 handle,
517 				   client_resource_release_fn_t release,
518 				   struct client_resource **return_resource)
519 {
520 	struct client_resource *resource;
521 
522 	spin_lock_irq(&client->lock);
523 	if (client->in_shutdown)
524 		resource = NULL;
525 	else
526 		resource = idr_find(&client->resource_idr, handle);
527 	if (resource && resource->release == release)
528 		idr_remove(&client->resource_idr, handle);
529 	spin_unlock_irq(&client->lock);
530 
531 	if (!(resource && resource->release == release))
532 		return -EINVAL;
533 
534 	if (return_resource)
535 		*return_resource = resource;
536 	else
537 		resource->release(client, resource);
538 
539 	client_put(client);
540 
541 	return 0;
542 }
543 
544 static void release_transaction(struct client *client,
545 				struct client_resource *resource)
546 {
547 }
548 
549 static void complete_transaction(struct fw_card *card, int rcode, u32 request_tstamp,
550 				 u32 response_tstamp, void *payload, size_t length, void *data)
551 {
552 	struct outbound_transaction_event *e = data;
553 	struct client *client = e->client;
554 	unsigned long flags;
555 
556 	spin_lock_irqsave(&client->lock, flags);
557 	idr_remove(&client->resource_idr, e->r.resource.handle);
558 	if (client->in_shutdown)
559 		wake_up(&client->tx_flush_wait);
560 	spin_unlock_irqrestore(&client->lock, flags);
561 
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 	}
607 
608 	/* Drop the idr's reference */
609 	client_put(client);
610 }
611 
612 static int init_request(struct client *client,
613 			struct fw_cdev_send_request *request,
614 			int destination_id, int speed)
615 {
616 	struct outbound_transaction_event *e;
617 	void *payload;
618 	int ret;
619 
620 	if (request->tcode != TCODE_STREAM_DATA &&
621 	    (request->length > 4096 || request->length > 512 << speed))
622 		return -EIO;
623 
624 	if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
625 	    request->length < 4)
626 		return -EINVAL;
627 
628 	e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
629 	if (e == NULL)
630 		return -ENOMEM;
631 	e->client = client;
632 
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 	}
648 
649 	if (request->data && copy_from_user(payload, u64_to_uptr(request->data), request->length)) {
650 		ret = -EFAULT;
651 		goto failed;
652 	}
653 
654 	e->r.resource.release = release_transaction;
655 	ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
656 	if (ret < 0)
657 		goto failed;
658 
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);
662 	return 0;
663 
664  failed:
665 	kfree(e);
666 
667 	return ret;
668 }
669 
670 static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
671 {
672 	switch (arg->send_request.tcode) {
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 
689 	return init_request(client, &arg->send_request, client->device->node_id,
690 			    client->device->max_speed);
691 }
692 
693 static void release_request(struct client *client,
694 			    struct client_resource *resource)
695 {
696 	struct inbound_transaction_resource *r = container_of(resource,
697 			struct inbound_transaction_resource, resource);
698 
699 	if (r->is_fcp)
700 		fw_request_put(r->request);
701 	else
702 		fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
703 
704 	fw_card_put(r->card);
705 	kfree(r);
706 }
707 
708 static void handle_request(struct fw_card *card, struct fw_request *request,
709 			   int tcode, int destination, int source,
710 			   int generation, unsigned long long offset,
711 			   void *payload, size_t length, void *callback_data)
712 {
713 	struct address_handler_resource *handler = callback_data;
714 	bool is_fcp = is_in_fcp_region(offset, length);
715 	struct inbound_transaction_resource *r;
716 	struct inbound_transaction_event *e;
717 	size_t event_size0;
718 	int ret;
719 
720 	/* card may be different from handler->client->device->card */
721 	fw_card_get(card);
722 
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 
728 	r = kmalloc(sizeof(*r), GFP_ATOMIC);
729 	e = kmalloc(sizeof(*e), GFP_ATOMIC);
730 	if (r == NULL || e == NULL)
731 		goto failed;
732 
733 	r->card    = card;
734 	r->request = request;
735 	r->is_fcp  = is_fcp;
736 	r->data    = payload;
737 	r->length  = length;
738 
739 	r->resource.release = release_request;
740 	ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
741 	if (ret < 0)
742 		goto failed;
743 
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);
757 	} else if (handler->client->version < FW_CDEV_VERSION_EVENT_ASYNC_TSTAMP) {
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);
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);
786 	}
787 
788 	queue_event(handler->client, &e->event,
789 		    &e->req, event_size0, r->data, length);
790 	return;
791 
792  failed:
793 	kfree(r);
794 	kfree(e);
795 
796 	if (!is_fcp)
797 		fw_send_response(card, request, RCODE_CONFLICT_ERROR);
798 	else
799 		fw_request_put(request);
800 
801 	fw_card_put(card);
802 }
803 
804 static void release_address_handler(struct client *client,
805 				    struct client_resource *resource)
806 {
807 	struct address_handler_resource *r =
808 	    container_of(resource, struct address_handler_resource, resource);
809 
810 	fw_core_remove_address_handler(&r->handler);
811 	kfree(r);
812 }
813 
814 static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
815 {
816 	struct fw_cdev_allocate *a = &arg->allocate;
817 	struct address_handler_resource *r;
818 	struct fw_address_region region;
819 	int ret;
820 
821 	r = kmalloc(sizeof(*r), GFP_KERNEL);
822 	if (r == NULL)
823 		return -ENOMEM;
824 
825 	region.start = a->offset;
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 
831 	r->handler.length           = a->length;
832 	r->handler.address_callback = handle_request;
833 	r->handler.callback_data    = r;
834 	r->closure   = a->closure;
835 	r->client    = client;
836 
837 	ret = fw_core_add_address_handler(&r->handler, &region);
838 	if (ret < 0) {
839 		kfree(r);
840 		return ret;
841 	}
842 	a->offset = r->handler.offset;
843 
844 	r->resource.release = release_address_handler;
845 	ret = add_client_resource(client, &r->resource, GFP_KERNEL);
846 	if (ret < 0) {
847 		release_address_handler(client, &r->resource);
848 		return ret;
849 	}
850 	a->handle = r->resource.handle;
851 
852 	return 0;
853 }
854 
855 static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
856 {
857 	return release_client_resource(client, arg->deallocate.handle,
858 				       release_address_handler, NULL);
859 }
860 
861 static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
862 {
863 	struct fw_cdev_send_response *a = &arg->send_response;
864 	struct client_resource *resource;
865 	struct inbound_transaction_resource *r;
866 	int ret = 0;
867 
868 	if (release_client_resource(client, a->handle,
869 				    release_request, &resource) < 0)
870 		return -EINVAL;
871 
872 	r = container_of(resource, struct inbound_transaction_resource,
873 			 resource);
874 	if (r->is_fcp) {
875 		fw_request_put(r->request);
876 		goto out;
877 	}
878 
879 	if (a->length != fw_get_response_length(r->request)) {
880 		ret = -EINVAL;
881 		fw_request_put(r->request);
882 		goto out;
883 	}
884 	if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
885 		ret = -EFAULT;
886 		fw_request_put(r->request);
887 		goto out;
888 	}
889 	fw_send_response(r->card, r->request, a->rcode);
890  out:
891 	fw_card_put(r->card);
892 	kfree(r);
893 
894 	return ret;
895 }
896 
897 static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
898 {
899 	fw_schedule_bus_reset(client->device->card, true,
900 			arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
901 	return 0;
902 }
903 
904 static void release_descriptor(struct client *client,
905 			       struct client_resource *resource)
906 {
907 	struct descriptor_resource *r =
908 		container_of(resource, struct descriptor_resource, resource);
909 
910 	fw_core_remove_descriptor(&r->descriptor);
911 	kfree(r);
912 }
913 
914 static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
915 {
916 	struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
917 	struct descriptor_resource *r;
918 	int ret;
919 
920 	/* Access policy: Allow this ioctl only on local nodes' device files. */
921 	if (!client->device->is_local)
922 		return -ENOSYS;
923 
924 	if (a->length > 256)
925 		return -EINVAL;
926 
927 	r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
928 	if (r == NULL)
929 		return -ENOMEM;
930 
931 	if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
932 		ret = -EFAULT;
933 		goto failed;
934 	}
935 
936 	r->descriptor.length    = a->length;
937 	r->descriptor.immediate = a->immediate;
938 	r->descriptor.key       = a->key;
939 	r->descriptor.data      = r->data;
940 
941 	ret = fw_core_add_descriptor(&r->descriptor);
942 	if (ret < 0)
943 		goto failed;
944 
945 	r->resource.release = release_descriptor;
946 	ret = add_client_resource(client, &r->resource, GFP_KERNEL);
947 	if (ret < 0) {
948 		fw_core_remove_descriptor(&r->descriptor);
949 		goto failed;
950 	}
951 	a->handle = r->resource.handle;
952 
953 	return 0;
954  failed:
955 	kfree(r);
956 
957 	return ret;
958 }
959 
960 static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
961 {
962 	return release_client_resource(client, arg->remove_descriptor.handle,
963 				       release_descriptor, NULL);
964 }
965 
966 static void iso_callback(struct fw_iso_context *context, u32 cycle,
967 			 size_t header_length, void *header, void *data)
968 {
969 	struct client *client = data;
970 	struct iso_interrupt_event *e;
971 
972 	e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
973 	if (e == NULL)
974 		return;
975 
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);
983 }
984 
985 static 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);
992 	if (e == NULL)
993 		return;
994 
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 
1003 static 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 
1011 static 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 
1025 static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
1026 {
1027 	struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
1028 	struct fw_iso_context *context;
1029 	union fw_iso_callback cb;
1030 	int ret;
1031 
1032 	BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT ||
1033 		     FW_CDEV_ISO_CONTEXT_RECEIVE  != FW_ISO_CONTEXT_RECEIVE  ||
1034 		     FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL !=
1035 					FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL);
1036 
1037 	switch (a->type) {
1038 	case FW_ISO_CONTEXT_TRANSMIT:
1039 		if (a->speed > SCODE_3200 || a->channel > 63)
1040 			return -EINVAL;
1041 
1042 		cb.sc = iso_callback;
1043 		break;
1044 
1045 	case FW_ISO_CONTEXT_RECEIVE:
1046 		if (a->header_size < 4 || (a->header_size & 3) ||
1047 		    a->channel > 63)
1048 			return -EINVAL;
1049 
1050 		cb.sc = iso_callback;
1051 		break;
1052 
1053 	case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
1054 		cb.mc = iso_mc_callback;
1055 		break;
1056 
1057 	default:
1058 		return -EINVAL;
1059 	}
1060 
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);
1068 	if (IS_ERR(context))
1069 		return PTR_ERR(context);
1070 	if (client->version < FW_CDEV_VERSION_AUTO_FLUSH_ISO_OVERFLOW)
1071 		context->drop_overflow_headers = true;
1072 
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);
1078 
1079 		return -EBUSY;
1080 	}
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 	}
1093 	client->iso_closure = a->closure;
1094 	client->iso_context = context;
1095 	spin_unlock_irq(&client->lock);
1096 
1097 	a->handle = 0;
1098 
1099 	return 0;
1100 }
1101 
1102 static 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 
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)
1117 #define GET_TAG(v)		(((v) >> 18) & 0x03)
1118 #define GET_SY(v)		(((v) >> 20) & 0x0f)
1119 #define GET_HEADER_LENGTH(v)	(((v) >> 24) & 0xff)
1120 
1121 static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
1122 {
1123 	struct fw_cdev_queue_iso *a = &arg->queue_iso;
1124 	struct fw_cdev_iso_packet __user *p, *end, *next;
1125 	struct fw_iso_context *ctx = client->iso_context;
1126 	unsigned long payload, buffer_end, transmit_header_bytes = 0;
1127 	u32 control;
1128 	int count;
1129 	struct {
1130 		struct fw_iso_packet packet;
1131 		u8 header[256];
1132 	} u;
1133 
1134 	if (ctx == NULL || a->handle != 0)
1135 		return -EINVAL;
1136 
1137 	/*
1138 	 * If the user passes a non-NULL data pointer, has mmap()'ed
1139 	 * the iso buffer, and the pointer points inside the buffer,
1140 	 * we setup the payload pointers accordingly.  Otherwise we
1141 	 * set them both to 0, which will still let packets with
1142 	 * payload_length == 0 through.  In other words, if no packets
1143 	 * use the indirect payload, the iso buffer need not be mapped
1144 	 * and the a->data pointer is ignored.
1145 	 */
1146 	payload = (unsigned long)a->data - client->vm_start;
1147 	buffer_end = client->buffer.page_count << PAGE_SHIFT;
1148 	if (a->data == 0 || client->buffer.pages == NULL ||
1149 	    payload >= buffer_end) {
1150 		payload = 0;
1151 		buffer_end = 0;
1152 	}
1153 
1154 	if (ctx->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL && payload & 3)
1155 		return -EINVAL;
1156 
1157 	p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
1158 
1159 	end = (void __user *)p + a->size;
1160 	count = 0;
1161 	while (p < end) {
1162 		if (get_user(control, &p->control))
1163 			return -EFAULT;
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);
1170 
1171 		switch (ctx->type) {
1172 		case FW_ISO_CONTEXT_TRANSMIT:
1173 			if (u.packet.header_length & 3)
1174 				return -EINVAL;
1175 			transmit_header_bytes = u.packet.header_length;
1176 			break;
1177 
1178 		case FW_ISO_CONTEXT_RECEIVE:
1179 			if (u.packet.header_length == 0 ||
1180 			    u.packet.header_length % ctx->header_size != 0)
1181 				return -EINVAL;
1182 			break;
1183 
1184 		case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
1185 			if (u.packet.payload_length == 0 ||
1186 			    u.packet.payload_length & 3)
1187 				return -EINVAL;
1188 			break;
1189 		}
1190 
1191 		next = (struct fw_cdev_iso_packet __user *)
1192 			&p->header[transmit_header_bytes / 4];
1193 		if (next > end)
1194 			return -EINVAL;
1195 		if (copy_from_user
1196 		    (u.packet.header, p->header, transmit_header_bytes))
1197 			return -EFAULT;
1198 		if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
1199 		    u.packet.header_length + u.packet.payload_length > 0)
1200 			return -EINVAL;
1201 		if (payload + u.packet.payload_length > buffer_end)
1202 			return -EINVAL;
1203 
1204 		if (fw_iso_context_queue(ctx, &u.packet,
1205 					 &client->buffer, payload))
1206 			break;
1207 
1208 		p = next;
1209 		payload += u.packet.payload_length;
1210 		count++;
1211 	}
1212 	fw_iso_context_queue_flush(ctx);
1213 
1214 	a->size    -= uptr_to_u64(p) - a->packets;
1215 	a->packets  = uptr_to_u64(p);
1216 	a->data     = client->vm_start + payload;
1217 
1218 	return count;
1219 }
1220 
1221 static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
1222 {
1223 	struct fw_cdev_start_iso *a = &arg->start_iso;
1224 
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 
1232 	if (client->iso_context == NULL || a->handle != 0)
1233 		return -EINVAL;
1234 
1235 	if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
1236 	    (a->tags == 0 || a->tags > 15 || a->sync > 15))
1237 		return -EINVAL;
1238 
1239 	return fw_iso_context_start(client->iso_context,
1240 				    a->cycle, a->sync, a->tags);
1241 }
1242 
1243 static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
1244 {
1245 	struct fw_cdev_stop_iso *a = &arg->stop_iso;
1246 
1247 	if (client->iso_context == NULL || a->handle != 0)
1248 		return -EINVAL;
1249 
1250 	return fw_iso_context_stop(client->iso_context);
1251 }
1252 
1253 static 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 
1263 static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
1264 {
1265 	struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
1266 	struct fw_card *card = client->device->card;
1267 	struct timespec64 ts = {0, 0};
1268 	u32 cycle_time = 0;
1269 	int ret = 0;
1270 
1271 	local_irq_disable();
1272 
1273 	ret = fw_card_read_cycle_time(card, &cycle_time);
1274 	if (ret < 0)
1275 		goto end;
1276 
1277 	switch (a->clk_id) {
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;
1281 	default:
1282 		ret = -EINVAL;
1283 	}
1284 end:
1285 	local_irq_enable();
1286 
1287 	a->tv_sec      = ts.tv_sec;
1288 	a->tv_nsec     = ts.tv_nsec;
1289 	a->cycle_timer = cycle_time;
1290 
1291 	return ret;
1292 }
1293 
1294 static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
1295 {
1296 	struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
1297 	struct fw_cdev_get_cycle_timer2 ct2;
1298 
1299 	ct2.clk_id = CLOCK_REALTIME;
1300 	ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
1301 
1302 	a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
1303 	a->cycle_timer = ct2.cycle_timer;
1304 
1305 	return 0;
1306 }
1307 
1308 static 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 &&
1322 	    time_before64(get_jiffies_64(),
1323 			  client->device->card->reset_jiffies + HZ)) {
1324 		schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
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 	}
1331 	free = todo == ISO_RES_DEALLOC ||
1332 	       todo == ISO_RES_ALLOC_ONCE ||
1333 	       todo == ISO_RES_DEALLOC_ONCE;
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,
1344 			todo == ISO_RES_ALLOC ||
1345 			todo == ISO_RES_REALLOC ||
1346 			todo == ISO_RES_ALLOC_ONCE);
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 &&
1371 	    idr_remove(&client->resource_idr, r->resource.handle)) {
1372 		client_put(client);
1373 		free = true;
1374 	}
1375 	spin_unlock_irq(&client->lock);
1376 
1377 	if (todo == ISO_RES_ALLOC && channel >= 0)
1378 		r->channels = 1ULL << channel;
1379 
1380 	if (todo == ISO_RES_REALLOC && success)
1381 		goto out;
1382 
1383 	if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
1384 		e = r->e_alloc;
1385 		r->e_alloc = NULL;
1386 	} else {
1387 		e = r->e_dealloc;
1388 		r->e_dealloc = NULL;
1389 	}
1390 	e->iso_resource.handle    = r->resource.handle;
1391 	e->iso_resource.channel   = channel;
1392 	e->iso_resource.bandwidth = bandwidth;
1393 
1394 	queue_event(client, &e->event,
1395 		    &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
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 
1407 static 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;
1415 	schedule_iso_resource(r, 0);
1416 	spin_unlock_irq(&client->lock);
1417 }
1418 
1419 static int init_iso_resource(struct client *client,
1420 		struct fw_cdev_allocate_iso_resource *request, int todo)
1421 {
1422 	struct iso_resource_event *e1, *e2;
1423 	struct iso_resource *r;
1424 	int ret;
1425 
1426 	if ((request->channels == 0 && request->bandwidth == 0) ||
1427 	    request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL)
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;
1440 	r->todo		= todo;
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 
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;
1451 
1452 	if (todo == ISO_RES_ALLOC) {
1453 		r->resource.release = release_iso_resource;
1454 		ret = add_client_resource(client, &r->resource, GFP_KERNEL);
1455 		if (ret < 0)
1456 			goto fail;
1457 	} else {
1458 		r->resource.release = NULL;
1459 		r->resource.handle = -1;
1460 		schedule_iso_resource(r, 0);
1461 	}
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 
1473 static int ioctl_allocate_iso_resource(struct client *client,
1474 				       union ioctl_arg *arg)
1475 {
1476 	return init_iso_resource(client,
1477 			&arg->allocate_iso_resource, ISO_RES_ALLOC);
1478 }
1479 
1480 static int ioctl_deallocate_iso_resource(struct client *client,
1481 					 union ioctl_arg *arg)
1482 {
1483 	return release_client_resource(client,
1484 			arg->deallocate.handle, release_iso_resource, NULL);
1485 }
1486 
1487 static int ioctl_allocate_iso_resource_once(struct client *client,
1488 					    union ioctl_arg *arg)
1489 {
1490 	return init_iso_resource(client,
1491 			&arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
1492 }
1493 
1494 static int ioctl_deallocate_iso_resource_once(struct client *client,
1495 					      union ioctl_arg *arg)
1496 {
1497 	return init_iso_resource(client,
1498 			&arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
1499 }
1500 
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  */
1506 static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
1507 {
1508 	return client->device->max_speed;
1509 }
1510 
1511 static int ioctl_send_broadcast_request(struct client *client,
1512 					union ioctl_arg *arg)
1513 {
1514 	struct fw_cdev_send_request *a = &arg->send_request;
1515 
1516 	switch (a->tcode) {
1517 	case TCODE_WRITE_QUADLET_REQUEST:
1518 	case TCODE_WRITE_BLOCK_REQUEST:
1519 		break;
1520 	default:
1521 		return -EINVAL;
1522 	}
1523 
1524 	/* Security policy: Only allow accesses to Units Space. */
1525 	if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
1526 		return -EACCES;
1527 
1528 	return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
1529 }
1530 
1531 static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
1532 {
1533 	struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
1534 	struct fw_cdev_send_request request;
1535 	int dest;
1536 
1537 	if (a->speed > client->device->card->link_speed ||
1538 	    a->length > 1024 << a->speed)
1539 		return -EIO;
1540 
1541 	if (a->tag > 3 || a->channel > 63 || a->sy > 15)
1542 		return -EINVAL;
1543 
1544 	dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
1545 	request.tcode		= TCODE_STREAM_DATA;
1546 	request.length		= a->length;
1547 	request.closure		= a->closure;
1548 	request.data		= a->data;
1549 	request.generation	= a->generation;
1550 
1551 	return init_request(client, &request, dest, a->speed);
1552 }
1553 
1554 static 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);
1559 	struct client *e_client = e->client;
1560 	u32 rcode;
1561 
1562 	trace_async_phy_outbound_complete((uintptr_t)packet, card->index, status, packet->generation,
1563 					  packet->timestamp);
1564 
1565 	switch (status) {
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;
1574 	case ACK_BUSY_X:
1575 	case ACK_BUSY_A:
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;
1589 	}
1590 
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 	}
1616 
1617 	client_put(e_client);
1618 }
1619 
1620 static 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 
1630 	e = kzalloc(sizeof(*e) + sizeof(a->data), GFP_KERNEL);
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;
1638 	async_header_set_tcode(e->p.header, TCODE_LINK_INTERNAL);
1639 	e->p.header[1]		= a->data[0];
1640 	e->p.header[2]		= a->data[1];
1641 	e->p.header_length	= 12;
1642 	e->p.callback		= outbound_phy_packet_callback;
1643 
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 	}
1661 
1662 	trace_async_phy_outbound_initiate((uintptr_t)&e->p, card->index, e->p.generation,
1663 					  e->p.header[1], e->p.header[2]);
1664 
1665 	card->driver->send_request(card, &e->p);
1666 
1667 	return 0;
1668 }
1669 
1670 static 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 
1689 void 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);
1699 		if (e == NULL)
1700 			break;
1701 
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 		}
1725 	}
1726 
1727 	spin_unlock_irqrestore(&card->lock, flags);
1728 }
1729 
1730 static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
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,
1752 	[0x15] = ioctl_send_phy_packet,
1753 	[0x16] = ioctl_receive_phy_packets,
1754 	[0x17] = ioctl_set_iso_channels,
1755 	[0x18] = ioctl_flush_iso,
1756 };
1757 
1758 static int dispatch_ioctl(struct client *client,
1759 			  unsigned int cmd, void __user *arg)
1760 {
1761 	union ioctl_arg buffer;
1762 	int ret;
1763 
1764 	if (fw_device_is_shutdown(client->device))
1765 		return -ENODEV;
1766 
1767 	if (_IOC_TYPE(cmd) != '#' ||
1768 	    _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
1769 	    _IOC_SIZE(cmd) > sizeof(buffer))
1770 		return -ENOTTY;
1771 
1772 	memset(&buffer, 0, sizeof(buffer));
1773 
1774 	if (_IOC_DIR(cmd) & _IOC_WRITE)
1775 		if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
1776 			return -EFAULT;
1777 
1778 	ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
1779 	if (ret < 0)
1780 		return ret;
1781 
1782 	if (_IOC_DIR(cmd) & _IOC_READ)
1783 		if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
1784 			return -EFAULT;
1785 
1786 	return ret;
1787 }
1788 
1789 static long fw_device_op_ioctl(struct file *file,
1790 			       unsigned int cmd, unsigned long arg)
1791 {
1792 	return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
1793 }
1794 
1795 static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
1796 {
1797 	struct client *client = file->private_data;
1798 	unsigned long size;
1799 	int page_count, ret;
1800 
1801 	if (fw_device_is_shutdown(client->device))
1802 		return -ENODEV;
1803 
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;
1810 
1811 	if (vma->vm_start & ~PAGE_MASK)
1812 		return -EINVAL;
1813 
1814 	client->vm_start = vma->vm_start;
1815 	size = vma->vm_end - vma->vm_start;
1816 	page_count = size >> PAGE_SHIFT;
1817 	if (size & ~PAGE_MASK)
1818 		return -EINVAL;
1819 
1820 	ret = fw_iso_buffer_alloc(&client->buffer, page_count);
1821 	if (ret < 0)
1822 		return ret;
1823 
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);
1832 	if (ret < 0)
1833 		goto fail;
1834 
1835 	ret = vm_map_pages_zero(vma, client->buffer.pages,
1836 				client->buffer.page_count);
1837 	if (ret < 0)
1838 		goto fail;
1839 
1840 	return 0;
1841  fail:
1842 	fw_iso_buffer_destroy(&client->buffer, client->device->card);
1843 	return ret;
1844 }
1845 
1846 static 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 
1853 static 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 
1865 static int shutdown_resource(int id, void *p, void *data)
1866 {
1867 	struct client_resource *resource = p;
1868 	struct client *client = data;
1869 
1870 	resource->release(client, resource);
1871 	client_put(client);
1872 
1873 	return 0;
1874 }
1875 
1876 static int fw_device_op_release(struct inode *inode, struct file *file)
1877 {
1878 	struct client *client = file->private_data;
1879 	struct event *event, *next_event;
1880 
1881 	spin_lock_irq(&client->device->card->lock);
1882 	list_del(&client->phy_receiver_link);
1883 	spin_unlock_irq(&client->device->card->lock);
1884 
1885 	scoped_guard(mutex, &client->device->client_list_mutex)
1886 		list_del(&client->link);
1887 
1888 	if (client->iso_context)
1889 		fw_iso_context_destroy(client->iso_context);
1890 
1891 	if (client->buffer.pages)
1892 		fw_iso_buffer_destroy(&client->buffer, client->device->card);
1893 
1894 	/* Freeze client->resource_idr and client->event_list */
1895 	spin_lock_irq(&client->lock);
1896 	client->in_shutdown = true;
1897 	spin_unlock_irq(&client->lock);
1898 
1899 	wait_event(client->tx_flush_wait, !has_outbound_transactions(client));
1900 
1901 	idr_for_each(&client->resource_idr, shutdown_resource, client);
1902 	idr_destroy(&client->resource_idr);
1903 
1904 	list_for_each_entry_safe(event, next_event, &client->event_list, link)
1905 		kfree(event);
1906 
1907 	client_put(client);
1908 
1909 	return 0;
1910 }
1911 
1912 static __poll_t fw_device_op_poll(struct file *file, poll_table * pt)
1913 {
1914 	struct client *client = file->private_data;
1915 	__poll_t mask = 0;
1916 
1917 	poll_wait(file, &client->wait, pt);
1918 
1919 	if (fw_device_is_shutdown(client->device))
1920 		mask |= EPOLLHUP | EPOLLERR;
1921 	if (!list_empty(&client->event_list))
1922 		mask |= EPOLLIN | EPOLLRDNORM;
1923 
1924 	return mask;
1925 }
1926 
1927 const struct file_operations fw_device_ops = {
1928 	.owner		= THIS_MODULE,
1929 	.llseek		= no_llseek,
1930 	.open		= fw_device_op_open,
1931 	.read		= fw_device_op_read,
1932 	.unlocked_ioctl	= fw_device_op_ioctl,
1933 	.mmap		= fw_device_op_mmap,
1934 	.release	= fw_device_op_release,
1935 	.poll		= fw_device_op_poll,
1936 	.compat_ioctl	= compat_ptr_ioctl,
1937 };
1938