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