core-cdev.c 46 KB

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