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