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. unsigned long flags;
  408. int ret;
  409. retry:
  410. if (idr_pre_get(&client->resource_idr, gfp_mask) == 0)
  411. return -ENOMEM;
  412. spin_lock_irqsave(&client->lock, flags);
  413. if (client->in_shutdown)
  414. ret = -ECANCELED;
  415. else
  416. ret = idr_get_new(&client->resource_idr, resource,
  417. &resource->handle);
  418. if (ret >= 0) {
  419. client_get(client);
  420. schedule_if_iso_resource(resource);
  421. }
  422. spin_unlock_irqrestore(&client->lock, flags);
  423. if (ret == -EAGAIN)
  424. goto retry;
  425. return ret < 0 ? ret : 0;
  426. }
  427. static int release_client_resource(struct client *client, u32 handle,
  428. client_resource_release_fn_t release,
  429. struct client_resource **return_resource)
  430. {
  431. struct client_resource *resource;
  432. spin_lock_irq(&client->lock);
  433. if (client->in_shutdown)
  434. resource = NULL;
  435. else
  436. resource = idr_find(&client->resource_idr, handle);
  437. if (resource && resource->release == release)
  438. idr_remove(&client->resource_idr, handle);
  439. spin_unlock_irq(&client->lock);
  440. if (!(resource && resource->release == release))
  441. return -EINVAL;
  442. if (return_resource)
  443. *return_resource = resource;
  444. else
  445. resource->release(client, resource);
  446. client_put(client);
  447. return 0;
  448. }
  449. static void release_transaction(struct client *client,
  450. struct client_resource *resource)
  451. {
  452. }
  453. static void complete_transaction(struct fw_card *card, int rcode,
  454. void *payload, size_t length, void *data)
  455. {
  456. struct outbound_transaction_event *e = data;
  457. struct fw_cdev_event_response *rsp = &e->response;
  458. struct client *client = e->client;
  459. unsigned long flags;
  460. if (length < rsp->length)
  461. rsp->length = length;
  462. if (rcode == RCODE_COMPLETE)
  463. memcpy(rsp->data, payload, rsp->length);
  464. spin_lock_irqsave(&client->lock, flags);
  465. idr_remove(&client->resource_idr, e->r.resource.handle);
  466. if (client->in_shutdown)
  467. wake_up(&client->tx_flush_wait);
  468. spin_unlock_irqrestore(&client->lock, flags);
  469. rsp->type = FW_CDEV_EVENT_RESPONSE;
  470. rsp->rcode = rcode;
  471. /*
  472. * In the case that sizeof(*rsp) doesn't align with the position of the
  473. * data, and the read is short, preserve an extra copy of the data
  474. * to stay compatible with a pre-2.6.27 bug. Since the bug is harmless
  475. * for short reads and some apps depended on it, this is both safe
  476. * and prudent for compatibility.
  477. */
  478. if (rsp->length <= sizeof(*rsp) - offsetof(typeof(*rsp), data))
  479. queue_event(client, &e->event, rsp, sizeof(*rsp),
  480. rsp->data, rsp->length);
  481. else
  482. queue_event(client, &e->event, rsp, sizeof(*rsp) + rsp->length,
  483. NULL, 0);
  484. /* Drop the idr's reference */
  485. client_put(client);
  486. }
  487. static int init_request(struct client *client,
  488. struct fw_cdev_send_request *request,
  489. int destination_id, int speed)
  490. {
  491. struct outbound_transaction_event *e;
  492. int ret;
  493. if (request->tcode != TCODE_STREAM_DATA &&
  494. (request->length > 4096 || request->length > 512 << speed))
  495. return -EIO;
  496. if (request->tcode == TCODE_WRITE_QUADLET_REQUEST &&
  497. request->length < 4)
  498. return -EINVAL;
  499. e = kmalloc(sizeof(*e) + request->length, GFP_KERNEL);
  500. if (e == NULL)
  501. return -ENOMEM;
  502. e->client = client;
  503. e->response.length = request->length;
  504. e->response.closure = request->closure;
  505. if (request->data &&
  506. copy_from_user(e->response.data,
  507. u64_to_uptr(request->data), request->length)) {
  508. ret = -EFAULT;
  509. goto failed;
  510. }
  511. e->r.resource.release = release_transaction;
  512. ret = add_client_resource(client, &e->r.resource, GFP_KERNEL);
  513. if (ret < 0)
  514. goto failed;
  515. fw_send_request(client->device->card, &e->r.transaction,
  516. request->tcode, destination_id, request->generation,
  517. speed, request->offset, e->response.data,
  518. request->length, complete_transaction, e);
  519. return 0;
  520. failed:
  521. kfree(e);
  522. return ret;
  523. }
  524. static int ioctl_send_request(struct client *client, union ioctl_arg *arg)
  525. {
  526. switch (arg->send_request.tcode) {
  527. case TCODE_WRITE_QUADLET_REQUEST:
  528. case TCODE_WRITE_BLOCK_REQUEST:
  529. case TCODE_READ_QUADLET_REQUEST:
  530. case TCODE_READ_BLOCK_REQUEST:
  531. case TCODE_LOCK_MASK_SWAP:
  532. case TCODE_LOCK_COMPARE_SWAP:
  533. case TCODE_LOCK_FETCH_ADD:
  534. case TCODE_LOCK_LITTLE_ADD:
  535. case TCODE_LOCK_BOUNDED_ADD:
  536. case TCODE_LOCK_WRAP_ADD:
  537. case TCODE_LOCK_VENDOR_DEPENDENT:
  538. break;
  539. default:
  540. return -EINVAL;
  541. }
  542. return init_request(client, &arg->send_request, client->device->node_id,
  543. client->device->max_speed);
  544. }
  545. static inline bool is_fcp_request(struct fw_request *request)
  546. {
  547. return request == NULL;
  548. }
  549. static void release_request(struct client *client,
  550. struct client_resource *resource)
  551. {
  552. struct inbound_transaction_resource *r = container_of(resource,
  553. struct inbound_transaction_resource, resource);
  554. if (is_fcp_request(r->request))
  555. kfree(r->data);
  556. else
  557. fw_send_response(r->card, r->request, RCODE_CONFLICT_ERROR);
  558. fw_card_put(r->card);
  559. kfree(r);
  560. }
  561. static void handle_request(struct fw_card *card, struct fw_request *request,
  562. int tcode, int destination, int source,
  563. int generation, unsigned long long offset,
  564. void *payload, size_t length, void *callback_data)
  565. {
  566. struct address_handler_resource *handler = callback_data;
  567. struct inbound_transaction_resource *r;
  568. struct inbound_transaction_event *e;
  569. size_t event_size0;
  570. void *fcp_frame = NULL;
  571. int ret;
  572. /* card may be different from handler->client->device->card */
  573. fw_card_get(card);
  574. r = kmalloc(sizeof(*r), GFP_ATOMIC);
  575. e = kmalloc(sizeof(*e), GFP_ATOMIC);
  576. if (r == NULL || e == NULL) {
  577. fw_notice(card, "out of memory when allocating event\n");
  578. goto failed;
  579. }
  580. r->card = card;
  581. r->request = request;
  582. r->data = payload;
  583. r->length = length;
  584. if (is_fcp_request(request)) {
  585. /*
  586. * FIXME: Let core-transaction.c manage a
  587. * single reference-counted copy?
  588. */
  589. fcp_frame = kmemdup(payload, length, GFP_ATOMIC);
  590. if (fcp_frame == NULL)
  591. goto failed;
  592. r->data = fcp_frame;
  593. }
  594. r->resource.release = release_request;
  595. ret = add_client_resource(handler->client, &r->resource, GFP_ATOMIC);
  596. if (ret < 0)
  597. goto failed;
  598. if (handler->client->version < FW_CDEV_VERSION_EVENT_REQUEST2) {
  599. struct fw_cdev_event_request *req = &e->req.request;
  600. if (tcode & 0x10)
  601. tcode = TCODE_LOCK_REQUEST;
  602. req->type = FW_CDEV_EVENT_REQUEST;
  603. req->tcode = tcode;
  604. req->offset = offset;
  605. req->length = length;
  606. req->handle = r->resource.handle;
  607. req->closure = handler->closure;
  608. event_size0 = sizeof(*req);
  609. } else {
  610. struct fw_cdev_event_request2 *req = &e->req.request2;
  611. req->type = FW_CDEV_EVENT_REQUEST2;
  612. req->tcode = tcode;
  613. req->offset = offset;
  614. req->source_node_id = source;
  615. req->destination_node_id = destination;
  616. req->card = card->index;
  617. req->generation = generation;
  618. req->length = length;
  619. req->handle = r->resource.handle;
  620. req->closure = handler->closure;
  621. event_size0 = sizeof(*req);
  622. }
  623. queue_event(handler->client, &e->event,
  624. &e->req, event_size0, r->data, length);
  625. return;
  626. failed:
  627. kfree(r);
  628. kfree(e);
  629. kfree(fcp_frame);
  630. if (!is_fcp_request(request))
  631. fw_send_response(card, request, RCODE_CONFLICT_ERROR);
  632. fw_card_put(card);
  633. }
  634. static void release_address_handler(struct client *client,
  635. struct client_resource *resource)
  636. {
  637. struct address_handler_resource *r =
  638. container_of(resource, struct address_handler_resource, resource);
  639. fw_core_remove_address_handler(&r->handler);
  640. kfree(r);
  641. }
  642. static int ioctl_allocate(struct client *client, union ioctl_arg *arg)
  643. {
  644. struct fw_cdev_allocate *a = &arg->allocate;
  645. struct address_handler_resource *r;
  646. struct fw_address_region region;
  647. int ret;
  648. r = kmalloc(sizeof(*r), GFP_KERNEL);
  649. if (r == NULL)
  650. return -ENOMEM;
  651. region.start = a->offset;
  652. if (client->version < FW_CDEV_VERSION_ALLOCATE_REGION_END)
  653. region.end = a->offset + a->length;
  654. else
  655. region.end = a->region_end;
  656. r->handler.length = a->length;
  657. r->handler.address_callback = handle_request;
  658. r->handler.callback_data = r;
  659. r->closure = a->closure;
  660. r->client = client;
  661. ret = fw_core_add_address_handler(&r->handler, &region);
  662. if (ret < 0) {
  663. kfree(r);
  664. return ret;
  665. }
  666. a->offset = r->handler.offset;
  667. r->resource.release = release_address_handler;
  668. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  669. if (ret < 0) {
  670. release_address_handler(client, &r->resource);
  671. return ret;
  672. }
  673. a->handle = r->resource.handle;
  674. return 0;
  675. }
  676. static int ioctl_deallocate(struct client *client, union ioctl_arg *arg)
  677. {
  678. return release_client_resource(client, arg->deallocate.handle,
  679. release_address_handler, NULL);
  680. }
  681. static int ioctl_send_response(struct client *client, union ioctl_arg *arg)
  682. {
  683. struct fw_cdev_send_response *a = &arg->send_response;
  684. struct client_resource *resource;
  685. struct inbound_transaction_resource *r;
  686. int ret = 0;
  687. if (release_client_resource(client, a->handle,
  688. release_request, &resource) < 0)
  689. return -EINVAL;
  690. r = container_of(resource, struct inbound_transaction_resource,
  691. resource);
  692. if (is_fcp_request(r->request))
  693. goto out;
  694. if (a->length != fw_get_response_length(r->request)) {
  695. ret = -EINVAL;
  696. kfree(r->request);
  697. goto out;
  698. }
  699. if (copy_from_user(r->data, u64_to_uptr(a->data), a->length)) {
  700. ret = -EFAULT;
  701. kfree(r->request);
  702. goto out;
  703. }
  704. fw_send_response(r->card, r->request, a->rcode);
  705. out:
  706. fw_card_put(r->card);
  707. kfree(r);
  708. return ret;
  709. }
  710. static int ioctl_initiate_bus_reset(struct client *client, union ioctl_arg *arg)
  711. {
  712. fw_schedule_bus_reset(client->device->card, true,
  713. arg->initiate_bus_reset.type == FW_CDEV_SHORT_RESET);
  714. return 0;
  715. }
  716. static void release_descriptor(struct client *client,
  717. struct client_resource *resource)
  718. {
  719. struct descriptor_resource *r =
  720. container_of(resource, struct descriptor_resource, resource);
  721. fw_core_remove_descriptor(&r->descriptor);
  722. kfree(r);
  723. }
  724. static int ioctl_add_descriptor(struct client *client, union ioctl_arg *arg)
  725. {
  726. struct fw_cdev_add_descriptor *a = &arg->add_descriptor;
  727. struct descriptor_resource *r;
  728. int ret;
  729. /* Access policy: Allow this ioctl only on local nodes' device files. */
  730. if (!client->device->is_local)
  731. return -ENOSYS;
  732. if (a->length > 256)
  733. return -EINVAL;
  734. r = kmalloc(sizeof(*r) + a->length * 4, GFP_KERNEL);
  735. if (r == NULL)
  736. return -ENOMEM;
  737. if (copy_from_user(r->data, u64_to_uptr(a->data), a->length * 4)) {
  738. ret = -EFAULT;
  739. goto failed;
  740. }
  741. r->descriptor.length = a->length;
  742. r->descriptor.immediate = a->immediate;
  743. r->descriptor.key = a->key;
  744. r->descriptor.data = r->data;
  745. ret = fw_core_add_descriptor(&r->descriptor);
  746. if (ret < 0)
  747. goto failed;
  748. r->resource.release = release_descriptor;
  749. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  750. if (ret < 0) {
  751. fw_core_remove_descriptor(&r->descriptor);
  752. goto failed;
  753. }
  754. a->handle = r->resource.handle;
  755. return 0;
  756. failed:
  757. kfree(r);
  758. return ret;
  759. }
  760. static int ioctl_remove_descriptor(struct client *client, union ioctl_arg *arg)
  761. {
  762. return release_client_resource(client, arg->remove_descriptor.handle,
  763. release_descriptor, NULL);
  764. }
  765. static void iso_callback(struct fw_iso_context *context, u32 cycle,
  766. size_t header_length, void *header, void *data)
  767. {
  768. struct client *client = data;
  769. struct iso_interrupt_event *e;
  770. e = kmalloc(sizeof(*e) + header_length, GFP_ATOMIC);
  771. if (e == NULL) {
  772. fw_notice(context->card, "out of memory when allocating event\n");
  773. return;
  774. }
  775. e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
  776. e->interrupt.closure = client->iso_closure;
  777. e->interrupt.cycle = cycle;
  778. e->interrupt.header_length = header_length;
  779. memcpy(e->interrupt.header, header, header_length);
  780. queue_event(client, &e->event, &e->interrupt,
  781. sizeof(e->interrupt) + header_length, NULL, 0);
  782. }
  783. static void iso_mc_callback(struct fw_iso_context *context,
  784. dma_addr_t completed, void *data)
  785. {
  786. struct client *client = data;
  787. struct iso_interrupt_mc_event *e;
  788. e = kmalloc(sizeof(*e), GFP_ATOMIC);
  789. if (e == NULL) {
  790. fw_notice(context->card, "out of memory when allocating event\n");
  791. return;
  792. }
  793. e->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT_MULTICHANNEL;
  794. e->interrupt.closure = client->iso_closure;
  795. e->interrupt.completed = fw_iso_buffer_lookup(&client->buffer,
  796. completed);
  797. queue_event(client, &e->event, &e->interrupt,
  798. sizeof(e->interrupt), NULL, 0);
  799. }
  800. static enum dma_data_direction iso_dma_direction(struct fw_iso_context *context)
  801. {
  802. if (context->type == FW_ISO_CONTEXT_TRANSMIT)
  803. return DMA_TO_DEVICE;
  804. else
  805. return DMA_FROM_DEVICE;
  806. }
  807. static int ioctl_create_iso_context(struct client *client, union ioctl_arg *arg)
  808. {
  809. struct fw_cdev_create_iso_context *a = &arg->create_iso_context;
  810. struct fw_iso_context *context;
  811. fw_iso_callback_t cb;
  812. int ret;
  813. BUILD_BUG_ON(FW_CDEV_ISO_CONTEXT_TRANSMIT != FW_ISO_CONTEXT_TRANSMIT ||
  814. FW_CDEV_ISO_CONTEXT_RECEIVE != FW_ISO_CONTEXT_RECEIVE ||
  815. FW_CDEV_ISO_CONTEXT_RECEIVE_MULTICHANNEL !=
  816. FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL);
  817. switch (a->type) {
  818. case FW_ISO_CONTEXT_TRANSMIT:
  819. if (a->speed > SCODE_3200 || a->channel > 63)
  820. return -EINVAL;
  821. cb = iso_callback;
  822. break;
  823. case FW_ISO_CONTEXT_RECEIVE:
  824. if (a->header_size < 4 || (a->header_size & 3) ||
  825. a->channel > 63)
  826. return -EINVAL;
  827. cb = iso_callback;
  828. break;
  829. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  830. cb = (fw_iso_callback_t)iso_mc_callback;
  831. break;
  832. default:
  833. return -EINVAL;
  834. }
  835. context = fw_iso_context_create(client->device->card, a->type,
  836. a->channel, a->speed, a->header_size, cb, client);
  837. if (IS_ERR(context))
  838. return PTR_ERR(context);
  839. /* We only support one context at this time. */
  840. spin_lock_irq(&client->lock);
  841. if (client->iso_context != NULL) {
  842. spin_unlock_irq(&client->lock);
  843. fw_iso_context_destroy(context);
  844. return -EBUSY;
  845. }
  846. if (!client->buffer_is_mapped) {
  847. ret = fw_iso_buffer_map_dma(&client->buffer,
  848. client->device->card,
  849. iso_dma_direction(context));
  850. if (ret < 0) {
  851. spin_unlock_irq(&client->lock);
  852. fw_iso_context_destroy(context);
  853. return ret;
  854. }
  855. client->buffer_is_mapped = true;
  856. }
  857. client->iso_closure = a->closure;
  858. client->iso_context = context;
  859. spin_unlock_irq(&client->lock);
  860. a->handle = 0;
  861. return 0;
  862. }
  863. static int ioctl_set_iso_channels(struct client *client, union ioctl_arg *arg)
  864. {
  865. struct fw_cdev_set_iso_channels *a = &arg->set_iso_channels;
  866. struct fw_iso_context *ctx = client->iso_context;
  867. if (ctx == NULL || a->handle != 0)
  868. return -EINVAL;
  869. return fw_iso_context_set_channels(ctx, &a->channels);
  870. }
  871. /* Macros for decoding the iso packet control header. */
  872. #define GET_PAYLOAD_LENGTH(v) ((v) & 0xffff)
  873. #define GET_INTERRUPT(v) (((v) >> 16) & 0x01)
  874. #define GET_SKIP(v) (((v) >> 17) & 0x01)
  875. #define GET_TAG(v) (((v) >> 18) & 0x03)
  876. #define GET_SY(v) (((v) >> 20) & 0x0f)
  877. #define GET_HEADER_LENGTH(v) (((v) >> 24) & 0xff)
  878. static int ioctl_queue_iso(struct client *client, union ioctl_arg *arg)
  879. {
  880. struct fw_cdev_queue_iso *a = &arg->queue_iso;
  881. struct fw_cdev_iso_packet __user *p, *end, *next;
  882. struct fw_iso_context *ctx = client->iso_context;
  883. unsigned long payload, buffer_end, transmit_header_bytes = 0;
  884. u32 control;
  885. int count;
  886. struct {
  887. struct fw_iso_packet packet;
  888. u8 header[256];
  889. } u;
  890. if (ctx == NULL || a->handle != 0)
  891. return -EINVAL;
  892. /*
  893. * If the user passes a non-NULL data pointer, has mmap()'ed
  894. * the iso buffer, and the pointer points inside the buffer,
  895. * we setup the payload pointers accordingly. Otherwise we
  896. * set them both to 0, which will still let packets with
  897. * payload_length == 0 through. In other words, if no packets
  898. * use the indirect payload, the iso buffer need not be mapped
  899. * and the a->data pointer is ignored.
  900. */
  901. payload = (unsigned long)a->data - client->vm_start;
  902. buffer_end = client->buffer.page_count << PAGE_SHIFT;
  903. if (a->data == 0 || client->buffer.pages == NULL ||
  904. payload >= buffer_end) {
  905. payload = 0;
  906. buffer_end = 0;
  907. }
  908. if (ctx->type == FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL && payload & 3)
  909. return -EINVAL;
  910. p = (struct fw_cdev_iso_packet __user *)u64_to_uptr(a->packets);
  911. if (!access_ok(VERIFY_READ, p, a->size))
  912. return -EFAULT;
  913. end = (void __user *)p + a->size;
  914. count = 0;
  915. while (p < end) {
  916. if (get_user(control, &p->control))
  917. return -EFAULT;
  918. u.packet.payload_length = GET_PAYLOAD_LENGTH(control);
  919. u.packet.interrupt = GET_INTERRUPT(control);
  920. u.packet.skip = GET_SKIP(control);
  921. u.packet.tag = GET_TAG(control);
  922. u.packet.sy = GET_SY(control);
  923. u.packet.header_length = GET_HEADER_LENGTH(control);
  924. switch (ctx->type) {
  925. case FW_ISO_CONTEXT_TRANSMIT:
  926. if (u.packet.header_length & 3)
  927. return -EINVAL;
  928. transmit_header_bytes = u.packet.header_length;
  929. break;
  930. case FW_ISO_CONTEXT_RECEIVE:
  931. if (u.packet.header_length == 0 ||
  932. u.packet.header_length % ctx->header_size != 0)
  933. return -EINVAL;
  934. break;
  935. case FW_ISO_CONTEXT_RECEIVE_MULTICHANNEL:
  936. if (u.packet.payload_length == 0 ||
  937. u.packet.payload_length & 3)
  938. return -EINVAL;
  939. break;
  940. }
  941. next = (struct fw_cdev_iso_packet __user *)
  942. &p->header[transmit_header_bytes / 4];
  943. if (next > end)
  944. return -EINVAL;
  945. if (__copy_from_user
  946. (u.packet.header, p->header, transmit_header_bytes))
  947. return -EFAULT;
  948. if (u.packet.skip && ctx->type == FW_ISO_CONTEXT_TRANSMIT &&
  949. u.packet.header_length + u.packet.payload_length > 0)
  950. return -EINVAL;
  951. if (payload + u.packet.payload_length > buffer_end)
  952. return -EINVAL;
  953. if (fw_iso_context_queue(ctx, &u.packet,
  954. &client->buffer, payload))
  955. break;
  956. p = next;
  957. payload += u.packet.payload_length;
  958. count++;
  959. }
  960. fw_iso_context_queue_flush(ctx);
  961. a->size -= uptr_to_u64(p) - a->packets;
  962. a->packets = uptr_to_u64(p);
  963. a->data = client->vm_start + payload;
  964. return count;
  965. }
  966. static int ioctl_start_iso(struct client *client, union ioctl_arg *arg)
  967. {
  968. struct fw_cdev_start_iso *a = &arg->start_iso;
  969. BUILD_BUG_ON(
  970. FW_CDEV_ISO_CONTEXT_MATCH_TAG0 != FW_ISO_CONTEXT_MATCH_TAG0 ||
  971. FW_CDEV_ISO_CONTEXT_MATCH_TAG1 != FW_ISO_CONTEXT_MATCH_TAG1 ||
  972. FW_CDEV_ISO_CONTEXT_MATCH_TAG2 != FW_ISO_CONTEXT_MATCH_TAG2 ||
  973. FW_CDEV_ISO_CONTEXT_MATCH_TAG3 != FW_ISO_CONTEXT_MATCH_TAG3 ||
  974. FW_CDEV_ISO_CONTEXT_MATCH_ALL_TAGS != FW_ISO_CONTEXT_MATCH_ALL_TAGS);
  975. if (client->iso_context == NULL || a->handle != 0)
  976. return -EINVAL;
  977. if (client->iso_context->type == FW_ISO_CONTEXT_RECEIVE &&
  978. (a->tags == 0 || a->tags > 15 || a->sync > 15))
  979. return -EINVAL;
  980. return fw_iso_context_start(client->iso_context,
  981. a->cycle, a->sync, a->tags);
  982. }
  983. static int ioctl_stop_iso(struct client *client, union ioctl_arg *arg)
  984. {
  985. struct fw_cdev_stop_iso *a = &arg->stop_iso;
  986. if (client->iso_context == NULL || a->handle != 0)
  987. return -EINVAL;
  988. return fw_iso_context_stop(client->iso_context);
  989. }
  990. static int ioctl_flush_iso(struct client *client, union ioctl_arg *arg)
  991. {
  992. struct fw_cdev_flush_iso *a = &arg->flush_iso;
  993. if (client->iso_context == NULL || a->handle != 0)
  994. return -EINVAL;
  995. return fw_iso_context_flush_completions(client->iso_context);
  996. }
  997. static int ioctl_get_cycle_timer2(struct client *client, union ioctl_arg *arg)
  998. {
  999. struct fw_cdev_get_cycle_timer2 *a = &arg->get_cycle_timer2;
  1000. struct fw_card *card = client->device->card;
  1001. struct timespec ts = {0, 0};
  1002. u32 cycle_time;
  1003. int ret = 0;
  1004. local_irq_disable();
  1005. cycle_time = card->driver->read_csr(card, CSR_CYCLE_TIME);
  1006. switch (a->clk_id) {
  1007. case CLOCK_REALTIME: getnstimeofday(&ts); break;
  1008. case CLOCK_MONOTONIC: do_posix_clock_monotonic_gettime(&ts); break;
  1009. case CLOCK_MONOTONIC_RAW: getrawmonotonic(&ts); break;
  1010. default:
  1011. ret = -EINVAL;
  1012. }
  1013. local_irq_enable();
  1014. a->tv_sec = ts.tv_sec;
  1015. a->tv_nsec = ts.tv_nsec;
  1016. a->cycle_timer = cycle_time;
  1017. return ret;
  1018. }
  1019. static int ioctl_get_cycle_timer(struct client *client, union ioctl_arg *arg)
  1020. {
  1021. struct fw_cdev_get_cycle_timer *a = &arg->get_cycle_timer;
  1022. struct fw_cdev_get_cycle_timer2 ct2;
  1023. ct2.clk_id = CLOCK_REALTIME;
  1024. ioctl_get_cycle_timer2(client, (union ioctl_arg *)&ct2);
  1025. a->local_time = ct2.tv_sec * USEC_PER_SEC + ct2.tv_nsec / NSEC_PER_USEC;
  1026. a->cycle_timer = ct2.cycle_timer;
  1027. return 0;
  1028. }
  1029. static void iso_resource_work(struct work_struct *work)
  1030. {
  1031. struct iso_resource_event *e;
  1032. struct iso_resource *r =
  1033. container_of(work, struct iso_resource, work.work);
  1034. struct client *client = r->client;
  1035. int generation, channel, bandwidth, todo;
  1036. bool skip, free, success;
  1037. spin_lock_irq(&client->lock);
  1038. generation = client->device->generation;
  1039. todo = r->todo;
  1040. /* Allow 1000ms grace period for other reallocations. */
  1041. if (todo == ISO_RES_ALLOC &&
  1042. time_before64(get_jiffies_64(),
  1043. client->device->card->reset_jiffies + HZ)) {
  1044. schedule_iso_resource(r, DIV_ROUND_UP(HZ, 3));
  1045. skip = true;
  1046. } else {
  1047. /* We could be called twice within the same generation. */
  1048. skip = todo == ISO_RES_REALLOC &&
  1049. r->generation == generation;
  1050. }
  1051. free = todo == ISO_RES_DEALLOC ||
  1052. todo == ISO_RES_ALLOC_ONCE ||
  1053. todo == ISO_RES_DEALLOC_ONCE;
  1054. r->generation = generation;
  1055. spin_unlock_irq(&client->lock);
  1056. if (skip)
  1057. goto out;
  1058. bandwidth = r->bandwidth;
  1059. fw_iso_resource_manage(client->device->card, generation,
  1060. r->channels, &channel, &bandwidth,
  1061. todo == ISO_RES_ALLOC ||
  1062. todo == ISO_RES_REALLOC ||
  1063. todo == ISO_RES_ALLOC_ONCE);
  1064. /*
  1065. * Is this generation outdated already? As long as this resource sticks
  1066. * in the idr, it will be scheduled again for a newer generation or at
  1067. * shutdown.
  1068. */
  1069. if (channel == -EAGAIN &&
  1070. (todo == ISO_RES_ALLOC || todo == ISO_RES_REALLOC))
  1071. goto out;
  1072. success = channel >= 0 || bandwidth > 0;
  1073. spin_lock_irq(&client->lock);
  1074. /*
  1075. * Transit from allocation to reallocation, except if the client
  1076. * requested deallocation in the meantime.
  1077. */
  1078. if (r->todo == ISO_RES_ALLOC)
  1079. r->todo = ISO_RES_REALLOC;
  1080. /*
  1081. * Allocation or reallocation failure? Pull this resource out of the
  1082. * idr and prepare for deletion, unless the client is shutting down.
  1083. */
  1084. if (r->todo == ISO_RES_REALLOC && !success &&
  1085. !client->in_shutdown &&
  1086. idr_find(&client->resource_idr, r->resource.handle)) {
  1087. idr_remove(&client->resource_idr, r->resource.handle);
  1088. client_put(client);
  1089. free = true;
  1090. }
  1091. spin_unlock_irq(&client->lock);
  1092. if (todo == ISO_RES_ALLOC && channel >= 0)
  1093. r->channels = 1ULL << channel;
  1094. if (todo == ISO_RES_REALLOC && success)
  1095. goto out;
  1096. if (todo == ISO_RES_ALLOC || todo == ISO_RES_ALLOC_ONCE) {
  1097. e = r->e_alloc;
  1098. r->e_alloc = NULL;
  1099. } else {
  1100. e = r->e_dealloc;
  1101. r->e_dealloc = NULL;
  1102. }
  1103. e->iso_resource.handle = r->resource.handle;
  1104. e->iso_resource.channel = channel;
  1105. e->iso_resource.bandwidth = bandwidth;
  1106. queue_event(client, &e->event,
  1107. &e->iso_resource, sizeof(e->iso_resource), NULL, 0);
  1108. if (free) {
  1109. cancel_delayed_work(&r->work);
  1110. kfree(r->e_alloc);
  1111. kfree(r->e_dealloc);
  1112. kfree(r);
  1113. }
  1114. out:
  1115. client_put(client);
  1116. }
  1117. static void release_iso_resource(struct client *client,
  1118. struct client_resource *resource)
  1119. {
  1120. struct iso_resource *r =
  1121. container_of(resource, struct iso_resource, resource);
  1122. spin_lock_irq(&client->lock);
  1123. r->todo = ISO_RES_DEALLOC;
  1124. schedule_iso_resource(r, 0);
  1125. spin_unlock_irq(&client->lock);
  1126. }
  1127. static int init_iso_resource(struct client *client,
  1128. struct fw_cdev_allocate_iso_resource *request, int todo)
  1129. {
  1130. struct iso_resource_event *e1, *e2;
  1131. struct iso_resource *r;
  1132. int ret;
  1133. if ((request->channels == 0 && request->bandwidth == 0) ||
  1134. request->bandwidth > BANDWIDTH_AVAILABLE_INITIAL ||
  1135. request->bandwidth < 0)
  1136. return -EINVAL;
  1137. r = kmalloc(sizeof(*r), GFP_KERNEL);
  1138. e1 = kmalloc(sizeof(*e1), GFP_KERNEL);
  1139. e2 = kmalloc(sizeof(*e2), GFP_KERNEL);
  1140. if (r == NULL || e1 == NULL || e2 == NULL) {
  1141. ret = -ENOMEM;
  1142. goto fail;
  1143. }
  1144. INIT_DELAYED_WORK(&r->work, iso_resource_work);
  1145. r->client = client;
  1146. r->todo = todo;
  1147. r->generation = -1;
  1148. r->channels = request->channels;
  1149. r->bandwidth = request->bandwidth;
  1150. r->e_alloc = e1;
  1151. r->e_dealloc = e2;
  1152. e1->iso_resource.closure = request->closure;
  1153. e1->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED;
  1154. e2->iso_resource.closure = request->closure;
  1155. e2->iso_resource.type = FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED;
  1156. if (todo == ISO_RES_ALLOC) {
  1157. r->resource.release = release_iso_resource;
  1158. ret = add_client_resource(client, &r->resource, GFP_KERNEL);
  1159. if (ret < 0)
  1160. goto fail;
  1161. } else {
  1162. r->resource.release = NULL;
  1163. r->resource.handle = -1;
  1164. schedule_iso_resource(r, 0);
  1165. }
  1166. request->handle = r->resource.handle;
  1167. return 0;
  1168. fail:
  1169. kfree(r);
  1170. kfree(e1);
  1171. kfree(e2);
  1172. return ret;
  1173. }
  1174. static int ioctl_allocate_iso_resource(struct client *client,
  1175. union ioctl_arg *arg)
  1176. {
  1177. return init_iso_resource(client,
  1178. &arg->allocate_iso_resource, ISO_RES_ALLOC);
  1179. }
  1180. static int ioctl_deallocate_iso_resource(struct client *client,
  1181. union ioctl_arg *arg)
  1182. {
  1183. return release_client_resource(client,
  1184. arg->deallocate.handle, release_iso_resource, NULL);
  1185. }
  1186. static int ioctl_allocate_iso_resource_once(struct client *client,
  1187. union ioctl_arg *arg)
  1188. {
  1189. return init_iso_resource(client,
  1190. &arg->allocate_iso_resource, ISO_RES_ALLOC_ONCE);
  1191. }
  1192. static int ioctl_deallocate_iso_resource_once(struct client *client,
  1193. union ioctl_arg *arg)
  1194. {
  1195. return init_iso_resource(client,
  1196. &arg->allocate_iso_resource, ISO_RES_DEALLOC_ONCE);
  1197. }
  1198. /*
  1199. * Returns a speed code: Maximum speed to or from this device,
  1200. * limited by the device's link speed, the local node's link speed,
  1201. * and all PHY port speeds between the two links.
  1202. */
  1203. static int ioctl_get_speed(struct client *client, union ioctl_arg *arg)
  1204. {
  1205. return client->device->max_speed;
  1206. }
  1207. static int ioctl_send_broadcast_request(struct client *client,
  1208. union ioctl_arg *arg)
  1209. {
  1210. struct fw_cdev_send_request *a = &arg->send_request;
  1211. switch (a->tcode) {
  1212. case TCODE_WRITE_QUADLET_REQUEST:
  1213. case TCODE_WRITE_BLOCK_REQUEST:
  1214. break;
  1215. default:
  1216. return -EINVAL;
  1217. }
  1218. /* Security policy: Only allow accesses to Units Space. */
  1219. if (a->offset < CSR_REGISTER_BASE + CSR_CONFIG_ROM_END)
  1220. return -EACCES;
  1221. return init_request(client, a, LOCAL_BUS | 0x3f, SCODE_100);
  1222. }
  1223. static int ioctl_send_stream_packet(struct client *client, union ioctl_arg *arg)
  1224. {
  1225. struct fw_cdev_send_stream_packet *a = &arg->send_stream_packet;
  1226. struct fw_cdev_send_request request;
  1227. int dest;
  1228. if (a->speed > client->device->card->link_speed ||
  1229. a->length > 1024 << a->speed)
  1230. return -EIO;
  1231. if (a->tag > 3 || a->channel > 63 || a->sy > 15)
  1232. return -EINVAL;
  1233. dest = fw_stream_packet_destination_id(a->tag, a->channel, a->sy);
  1234. request.tcode = TCODE_STREAM_DATA;
  1235. request.length = a->length;
  1236. request.closure = a->closure;
  1237. request.data = a->data;
  1238. request.generation = a->generation;
  1239. return init_request(client, &request, dest, a->speed);
  1240. }
  1241. static void outbound_phy_packet_callback(struct fw_packet *packet,
  1242. struct fw_card *card, int status)
  1243. {
  1244. struct outbound_phy_packet_event *e =
  1245. container_of(packet, struct outbound_phy_packet_event, p);
  1246. switch (status) {
  1247. /* expected: */
  1248. case ACK_COMPLETE: e->phy_packet.rcode = RCODE_COMPLETE; break;
  1249. /* should never happen with PHY packets: */
  1250. case ACK_PENDING: e->phy_packet.rcode = RCODE_COMPLETE; break;
  1251. case ACK_BUSY_X:
  1252. case ACK_BUSY_A:
  1253. case ACK_BUSY_B: e->phy_packet.rcode = RCODE_BUSY; break;
  1254. case ACK_DATA_ERROR: e->phy_packet.rcode = RCODE_DATA_ERROR; break;
  1255. case ACK_TYPE_ERROR: e->phy_packet.rcode = RCODE_TYPE_ERROR; break;
  1256. /* stale generation; cancelled; on certain controllers: no ack */
  1257. default: e->phy_packet.rcode = status; break;
  1258. }
  1259. e->phy_packet.data[0] = packet->timestamp;
  1260. queue_event(e->client, &e->event, &e->phy_packet,
  1261. sizeof(e->phy_packet) + e->phy_packet.length, NULL, 0);
  1262. client_put(e->client);
  1263. }
  1264. static int ioctl_send_phy_packet(struct client *client, union ioctl_arg *arg)
  1265. {
  1266. struct fw_cdev_send_phy_packet *a = &arg->send_phy_packet;
  1267. struct fw_card *card = client->device->card;
  1268. struct outbound_phy_packet_event *e;
  1269. /* Access policy: Allow this ioctl only on local nodes' device files. */
  1270. if (!client->device->is_local)
  1271. return -ENOSYS;
  1272. e = kzalloc(sizeof(*e) + 4, GFP_KERNEL);
  1273. if (e == NULL)
  1274. return -ENOMEM;
  1275. client_get(client);
  1276. e->client = client;
  1277. e->p.speed = SCODE_100;
  1278. e->p.generation = a->generation;
  1279. e->p.header[0] = TCODE_LINK_INTERNAL << 4;
  1280. e->p.header[1] = a->data[0];
  1281. e->p.header[2] = a->data[1];
  1282. e->p.header_length = 12;
  1283. e->p.callback = outbound_phy_packet_callback;
  1284. e->phy_packet.closure = a->closure;
  1285. e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_SENT;
  1286. if (is_ping_packet(a->data))
  1287. e->phy_packet.length = 4;
  1288. card->driver->send_request(card, &e->p);
  1289. return 0;
  1290. }
  1291. static int ioctl_receive_phy_packets(struct client *client, union ioctl_arg *arg)
  1292. {
  1293. struct fw_cdev_receive_phy_packets *a = &arg->receive_phy_packets;
  1294. struct fw_card *card = client->device->card;
  1295. /* Access policy: Allow this ioctl only on local nodes' device files. */
  1296. if (!client->device->is_local)
  1297. return -ENOSYS;
  1298. spin_lock_irq(&card->lock);
  1299. list_move_tail(&client->phy_receiver_link, &card->phy_receiver_list);
  1300. client->phy_receiver_closure = a->closure;
  1301. spin_unlock_irq(&card->lock);
  1302. return 0;
  1303. }
  1304. void fw_cdev_handle_phy_packet(struct fw_card *card, struct fw_packet *p)
  1305. {
  1306. struct client *client;
  1307. struct inbound_phy_packet_event *e;
  1308. unsigned long flags;
  1309. spin_lock_irqsave(&card->lock, flags);
  1310. list_for_each_entry(client, &card->phy_receiver_list, phy_receiver_link) {
  1311. e = kmalloc(sizeof(*e) + 8, GFP_ATOMIC);
  1312. if (e == NULL) {
  1313. fw_notice(card, "out of memory when allocating event\n");
  1314. break;
  1315. }
  1316. e->phy_packet.closure = client->phy_receiver_closure;
  1317. e->phy_packet.type = FW_CDEV_EVENT_PHY_PACKET_RECEIVED;
  1318. e->phy_packet.rcode = RCODE_COMPLETE;
  1319. e->phy_packet.length = 8;
  1320. e->phy_packet.data[0] = p->header[1];
  1321. e->phy_packet.data[1] = p->header[2];
  1322. queue_event(client, &e->event,
  1323. &e->phy_packet, sizeof(e->phy_packet) + 8, NULL, 0);
  1324. }
  1325. spin_unlock_irqrestore(&card->lock, flags);
  1326. }
  1327. static int (* const ioctl_handlers[])(struct client *, union ioctl_arg *) = {
  1328. [0x00] = ioctl_get_info,
  1329. [0x01] = ioctl_send_request,
  1330. [0x02] = ioctl_allocate,
  1331. [0x03] = ioctl_deallocate,
  1332. [0x04] = ioctl_send_response,
  1333. [0x05] = ioctl_initiate_bus_reset,
  1334. [0x06] = ioctl_add_descriptor,
  1335. [0x07] = ioctl_remove_descriptor,
  1336. [0x08] = ioctl_create_iso_context,
  1337. [0x09] = ioctl_queue_iso,
  1338. [0x0a] = ioctl_start_iso,
  1339. [0x0b] = ioctl_stop_iso,
  1340. [0x0c] = ioctl_get_cycle_timer,
  1341. [0x0d] = ioctl_allocate_iso_resource,
  1342. [0x0e] = ioctl_deallocate_iso_resource,
  1343. [0x0f] = ioctl_allocate_iso_resource_once,
  1344. [0x10] = ioctl_deallocate_iso_resource_once,
  1345. [0x11] = ioctl_get_speed,
  1346. [0x12] = ioctl_send_broadcast_request,
  1347. [0x13] = ioctl_send_stream_packet,
  1348. [0x14] = ioctl_get_cycle_timer2,
  1349. [0x15] = ioctl_send_phy_packet,
  1350. [0x16] = ioctl_receive_phy_packets,
  1351. [0x17] = ioctl_set_iso_channels,
  1352. [0x18] = ioctl_flush_iso,
  1353. };
  1354. static int dispatch_ioctl(struct client *client,
  1355. unsigned int cmd, void __user *arg)
  1356. {
  1357. union ioctl_arg buffer;
  1358. int ret;
  1359. if (fw_device_is_shutdown(client->device))
  1360. return -ENODEV;
  1361. if (_IOC_TYPE(cmd) != '#' ||
  1362. _IOC_NR(cmd) >= ARRAY_SIZE(ioctl_handlers) ||
  1363. _IOC_SIZE(cmd) > sizeof(buffer))
  1364. return -ENOTTY;
  1365. if (_IOC_DIR(cmd) == _IOC_READ)
  1366. memset(&buffer, 0, _IOC_SIZE(cmd));
  1367. if (_IOC_DIR(cmd) & _IOC_WRITE)
  1368. if (copy_from_user(&buffer, arg, _IOC_SIZE(cmd)))
  1369. return -EFAULT;
  1370. ret = ioctl_handlers[_IOC_NR(cmd)](client, &buffer);
  1371. if (ret < 0)
  1372. return ret;
  1373. if (_IOC_DIR(cmd) & _IOC_READ)
  1374. if (copy_to_user(arg, &buffer, _IOC_SIZE(cmd)))
  1375. return -EFAULT;
  1376. return ret;
  1377. }
  1378. static long fw_device_op_ioctl(struct file *file,
  1379. unsigned int cmd, unsigned long arg)
  1380. {
  1381. return dispatch_ioctl(file->private_data, cmd, (void __user *)arg);
  1382. }
  1383. #ifdef CONFIG_COMPAT
  1384. static long fw_device_op_compat_ioctl(struct file *file,
  1385. unsigned int cmd, unsigned long arg)
  1386. {
  1387. return dispatch_ioctl(file->private_data, cmd, compat_ptr(arg));
  1388. }
  1389. #endif
  1390. static int fw_device_op_mmap(struct file *file, struct vm_area_struct *vma)
  1391. {
  1392. struct client *client = file->private_data;
  1393. unsigned long size;
  1394. int page_count, ret;
  1395. if (fw_device_is_shutdown(client->device))
  1396. return -ENODEV;
  1397. /* FIXME: We could support multiple buffers, but we don't. */
  1398. if (client->buffer.pages != NULL)
  1399. return -EBUSY;
  1400. if (!(vma->vm_flags & VM_SHARED))
  1401. return -EINVAL;
  1402. if (vma->vm_start & ~PAGE_MASK)
  1403. return -EINVAL;
  1404. client->vm_start = vma->vm_start;
  1405. size = vma->vm_end - vma->vm_start;
  1406. page_count = size >> PAGE_SHIFT;
  1407. if (size & ~PAGE_MASK)
  1408. return -EINVAL;
  1409. ret = fw_iso_buffer_alloc(&client->buffer, page_count);
  1410. if (ret < 0)
  1411. return ret;
  1412. spin_lock_irq(&client->lock);
  1413. if (client->iso_context) {
  1414. ret = fw_iso_buffer_map_dma(&client->buffer,
  1415. client->device->card,
  1416. iso_dma_direction(client->iso_context));
  1417. client->buffer_is_mapped = (ret == 0);
  1418. }
  1419. spin_unlock_irq(&client->lock);
  1420. if (ret < 0)
  1421. goto fail;
  1422. ret = fw_iso_buffer_map_vma(&client->buffer, vma);
  1423. if (ret < 0)
  1424. goto fail;
  1425. return 0;
  1426. fail:
  1427. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  1428. return ret;
  1429. }
  1430. static int is_outbound_transaction_resource(int id, void *p, void *data)
  1431. {
  1432. struct client_resource *resource = p;
  1433. return resource->release == release_transaction;
  1434. }
  1435. static int has_outbound_transactions(struct client *client)
  1436. {
  1437. int ret;
  1438. spin_lock_irq(&client->lock);
  1439. ret = idr_for_each(&client->resource_idr,
  1440. is_outbound_transaction_resource, NULL);
  1441. spin_unlock_irq(&client->lock);
  1442. return ret;
  1443. }
  1444. static int shutdown_resource(int id, void *p, void *data)
  1445. {
  1446. struct client_resource *resource = p;
  1447. struct client *client = data;
  1448. resource->release(client, resource);
  1449. client_put(client);
  1450. return 0;
  1451. }
  1452. static int fw_device_op_release(struct inode *inode, struct file *file)
  1453. {
  1454. struct client *client = file->private_data;
  1455. struct event *event, *next_event;
  1456. spin_lock_irq(&client->device->card->lock);
  1457. list_del(&client->phy_receiver_link);
  1458. spin_unlock_irq(&client->device->card->lock);
  1459. mutex_lock(&client->device->client_list_mutex);
  1460. list_del(&client->link);
  1461. mutex_unlock(&client->device->client_list_mutex);
  1462. if (client->iso_context)
  1463. fw_iso_context_destroy(client->iso_context);
  1464. if (client->buffer.pages)
  1465. fw_iso_buffer_destroy(&client->buffer, client->device->card);
  1466. /* Freeze client->resource_idr and client->event_list */
  1467. spin_lock_irq(&client->lock);
  1468. client->in_shutdown = true;
  1469. spin_unlock_irq(&client->lock);
  1470. wait_event(client->tx_flush_wait, !has_outbound_transactions(client));
  1471. idr_for_each(&client->resource_idr, shutdown_resource, client);
  1472. idr_remove_all(&client->resource_idr);
  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. };