core-cdev.c 46 KB

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