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