core-cdev.c 46 KB

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