core-cdev.c 47 KB

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