core-cdev.c 49 KB

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