rio.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. /*
  2. * RapidIO interconnect services
  3. * (RapidIO Interconnect Specification, http://www.rapidio.org)
  4. *
  5. * Copyright 2005 MontaVista Software, Inc.
  6. * Matt Porter <mporter@kernel.crashing.org>
  7. *
  8. * Copyright 2009 Integrated Device Technology, Inc.
  9. * Alex Bounine <alexandre.bounine@idt.com>
  10. * - Added Port-Write/Error Management initialization and handling
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/delay.h>
  20. #include <linux/init.h>
  21. #include <linux/rio.h>
  22. #include <linux/rio_drv.h>
  23. #include <linux/rio_ids.h>
  24. #include <linux/rio_regs.h>
  25. #include <linux/module.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/slab.h>
  28. #include <linux/interrupt.h>
  29. #include "rio.h"
  30. static LIST_HEAD(rio_mports);
  31. static unsigned char next_portid;
  32. static DEFINE_SPINLOCK(rio_mmap_lock);
  33. /**
  34. * rio_local_get_device_id - Get the base/extended device id for a port
  35. * @port: RIO master port from which to get the deviceid
  36. *
  37. * Reads the base/extended device id from the local device
  38. * implementing the master port. Returns the 8/16-bit device
  39. * id.
  40. */
  41. u16 rio_local_get_device_id(struct rio_mport *port)
  42. {
  43. u32 result;
  44. rio_local_read_config_32(port, RIO_DID_CSR, &result);
  45. return (RIO_GET_DID(port->sys_size, result));
  46. }
  47. /**
  48. * rio_request_inb_mbox - request inbound mailbox service
  49. * @mport: RIO master port from which to allocate the mailbox resource
  50. * @dev_id: Device specific pointer to pass on event
  51. * @mbox: Mailbox number to claim
  52. * @entries: Number of entries in inbound mailbox queue
  53. * @minb: Callback to execute when inbound message is received
  54. *
  55. * Requests ownership of an inbound mailbox resource and binds
  56. * a callback function to the resource. Returns %0 on success.
  57. */
  58. int rio_request_inb_mbox(struct rio_mport *mport,
  59. void *dev_id,
  60. int mbox,
  61. int entries,
  62. void (*minb) (struct rio_mport * mport, void *dev_id, int mbox,
  63. int slot))
  64. {
  65. int rc = -ENOSYS;
  66. struct resource *res;
  67. if (mport->ops->open_inb_mbox == NULL)
  68. goto out;
  69. res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  70. if (res) {
  71. rio_init_mbox_res(res, mbox, mbox);
  72. /* Make sure this mailbox isn't in use */
  73. if ((rc =
  74. request_resource(&mport->riores[RIO_INB_MBOX_RESOURCE],
  75. res)) < 0) {
  76. kfree(res);
  77. goto out;
  78. }
  79. mport->inb_msg[mbox].res = res;
  80. /* Hook the inbound message callback */
  81. mport->inb_msg[mbox].mcback = minb;
  82. rc = mport->ops->open_inb_mbox(mport, dev_id, mbox, entries);
  83. } else
  84. rc = -ENOMEM;
  85. out:
  86. return rc;
  87. }
  88. /**
  89. * rio_release_inb_mbox - release inbound mailbox message service
  90. * @mport: RIO master port from which to release the mailbox resource
  91. * @mbox: Mailbox number to release
  92. *
  93. * Releases ownership of an inbound mailbox resource. Returns 0
  94. * if the request has been satisfied.
  95. */
  96. int rio_release_inb_mbox(struct rio_mport *mport, int mbox)
  97. {
  98. if (mport->ops->close_inb_mbox) {
  99. mport->ops->close_inb_mbox(mport, mbox);
  100. /* Release the mailbox resource */
  101. return release_resource(mport->inb_msg[mbox].res);
  102. } else
  103. return -ENOSYS;
  104. }
  105. /**
  106. * rio_request_outb_mbox - request outbound mailbox service
  107. * @mport: RIO master port from which to allocate the mailbox resource
  108. * @dev_id: Device specific pointer to pass on event
  109. * @mbox: Mailbox number to claim
  110. * @entries: Number of entries in outbound mailbox queue
  111. * @moutb: Callback to execute when outbound message is sent
  112. *
  113. * Requests ownership of an outbound mailbox resource and binds
  114. * a callback function to the resource. Returns 0 on success.
  115. */
  116. int rio_request_outb_mbox(struct rio_mport *mport,
  117. void *dev_id,
  118. int mbox,
  119. int entries,
  120. void (*moutb) (struct rio_mport * mport, void *dev_id, int mbox, int slot))
  121. {
  122. int rc = -ENOSYS;
  123. struct resource *res;
  124. if (mport->ops->open_outb_mbox == NULL)
  125. goto out;
  126. res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  127. if (res) {
  128. rio_init_mbox_res(res, mbox, mbox);
  129. /* Make sure this outbound mailbox isn't in use */
  130. if ((rc =
  131. request_resource(&mport->riores[RIO_OUTB_MBOX_RESOURCE],
  132. res)) < 0) {
  133. kfree(res);
  134. goto out;
  135. }
  136. mport->outb_msg[mbox].res = res;
  137. /* Hook the inbound message callback */
  138. mport->outb_msg[mbox].mcback = moutb;
  139. rc = mport->ops->open_outb_mbox(mport, dev_id, mbox, entries);
  140. } else
  141. rc = -ENOMEM;
  142. out:
  143. return rc;
  144. }
  145. /**
  146. * rio_release_outb_mbox - release outbound mailbox message service
  147. * @mport: RIO master port from which to release the mailbox resource
  148. * @mbox: Mailbox number to release
  149. *
  150. * Releases ownership of an inbound mailbox resource. Returns 0
  151. * if the request has been satisfied.
  152. */
  153. int rio_release_outb_mbox(struct rio_mport *mport, int mbox)
  154. {
  155. if (mport->ops->close_outb_mbox) {
  156. mport->ops->close_outb_mbox(mport, mbox);
  157. /* Release the mailbox resource */
  158. return release_resource(mport->outb_msg[mbox].res);
  159. } else
  160. return -ENOSYS;
  161. }
  162. /**
  163. * rio_setup_inb_dbell - bind inbound doorbell callback
  164. * @mport: RIO master port to bind the doorbell callback
  165. * @dev_id: Device specific pointer to pass on event
  166. * @res: Doorbell message resource
  167. * @dinb: Callback to execute when doorbell is received
  168. *
  169. * Adds a doorbell resource/callback pair into a port's
  170. * doorbell event list. Returns 0 if the request has been
  171. * satisfied.
  172. */
  173. static int
  174. rio_setup_inb_dbell(struct rio_mport *mport, void *dev_id, struct resource *res,
  175. void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src, u16 dst,
  176. u16 info))
  177. {
  178. int rc = 0;
  179. struct rio_dbell *dbell;
  180. if (!(dbell = kmalloc(sizeof(struct rio_dbell), GFP_KERNEL))) {
  181. rc = -ENOMEM;
  182. goto out;
  183. }
  184. dbell->res = res;
  185. dbell->dinb = dinb;
  186. dbell->dev_id = dev_id;
  187. list_add_tail(&dbell->node, &mport->dbells);
  188. out:
  189. return rc;
  190. }
  191. /**
  192. * rio_request_inb_dbell - request inbound doorbell message service
  193. * @mport: RIO master port from which to allocate the doorbell resource
  194. * @dev_id: Device specific pointer to pass on event
  195. * @start: Doorbell info range start
  196. * @end: Doorbell info range end
  197. * @dinb: Callback to execute when doorbell is received
  198. *
  199. * Requests ownership of an inbound doorbell resource and binds
  200. * a callback function to the resource. Returns 0 if the request
  201. * has been satisfied.
  202. */
  203. int rio_request_inb_dbell(struct rio_mport *mport,
  204. void *dev_id,
  205. u16 start,
  206. u16 end,
  207. void (*dinb) (struct rio_mport * mport, void *dev_id, u16 src,
  208. u16 dst, u16 info))
  209. {
  210. int rc = 0;
  211. struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  212. if (res) {
  213. rio_init_dbell_res(res, start, end);
  214. /* Make sure these doorbells aren't in use */
  215. if ((rc =
  216. request_resource(&mport->riores[RIO_DOORBELL_RESOURCE],
  217. res)) < 0) {
  218. kfree(res);
  219. goto out;
  220. }
  221. /* Hook the doorbell callback */
  222. rc = rio_setup_inb_dbell(mport, dev_id, res, dinb);
  223. } else
  224. rc = -ENOMEM;
  225. out:
  226. return rc;
  227. }
  228. /**
  229. * rio_release_inb_dbell - release inbound doorbell message service
  230. * @mport: RIO master port from which to release the doorbell resource
  231. * @start: Doorbell info range start
  232. * @end: Doorbell info range end
  233. *
  234. * Releases ownership of an inbound doorbell resource and removes
  235. * callback from the doorbell event list. Returns 0 if the request
  236. * has been satisfied.
  237. */
  238. int rio_release_inb_dbell(struct rio_mport *mport, u16 start, u16 end)
  239. {
  240. int rc = 0, found = 0;
  241. struct rio_dbell *dbell;
  242. list_for_each_entry(dbell, &mport->dbells, node) {
  243. if ((dbell->res->start == start) && (dbell->res->end == end)) {
  244. found = 1;
  245. break;
  246. }
  247. }
  248. /* If we can't find an exact match, fail */
  249. if (!found) {
  250. rc = -EINVAL;
  251. goto out;
  252. }
  253. /* Delete from list */
  254. list_del(&dbell->node);
  255. /* Release the doorbell resource */
  256. rc = release_resource(dbell->res);
  257. /* Free the doorbell event */
  258. kfree(dbell);
  259. out:
  260. return rc;
  261. }
  262. /**
  263. * rio_request_outb_dbell - request outbound doorbell message range
  264. * @rdev: RIO device from which to allocate the doorbell resource
  265. * @start: Doorbell message range start
  266. * @end: Doorbell message range end
  267. *
  268. * Requests ownership of a doorbell message range. Returns a resource
  269. * if the request has been satisfied or %NULL on failure.
  270. */
  271. struct resource *rio_request_outb_dbell(struct rio_dev *rdev, u16 start,
  272. u16 end)
  273. {
  274. struct resource *res = kmalloc(sizeof(struct resource), GFP_KERNEL);
  275. if (res) {
  276. rio_init_dbell_res(res, start, end);
  277. /* Make sure these doorbells aren't in use */
  278. if (request_resource(&rdev->riores[RIO_DOORBELL_RESOURCE], res)
  279. < 0) {
  280. kfree(res);
  281. res = NULL;
  282. }
  283. }
  284. return res;
  285. }
  286. /**
  287. * rio_release_outb_dbell - release outbound doorbell message range
  288. * @rdev: RIO device from which to release the doorbell resource
  289. * @res: Doorbell resource to be freed
  290. *
  291. * Releases ownership of a doorbell message range. Returns 0 if the
  292. * request has been satisfied.
  293. */
  294. int rio_release_outb_dbell(struct rio_dev *rdev, struct resource *res)
  295. {
  296. int rc = release_resource(res);
  297. kfree(res);
  298. return rc;
  299. }
  300. /**
  301. * rio_request_inb_pwrite - request inbound port-write message service
  302. * @rdev: RIO device to which register inbound port-write callback routine
  303. * @pwcback: Callback routine to execute when port-write is received
  304. *
  305. * Binds a port-write callback function to the RapidIO device.
  306. * Returns 0 if the request has been satisfied.
  307. */
  308. int rio_request_inb_pwrite(struct rio_dev *rdev,
  309. int (*pwcback)(struct rio_dev *rdev, union rio_pw_msg *msg, int step))
  310. {
  311. int rc = 0;
  312. spin_lock(&rio_global_list_lock);
  313. if (rdev->pwcback != NULL)
  314. rc = -ENOMEM;
  315. else
  316. rdev->pwcback = pwcback;
  317. spin_unlock(&rio_global_list_lock);
  318. return rc;
  319. }
  320. EXPORT_SYMBOL_GPL(rio_request_inb_pwrite);
  321. /**
  322. * rio_release_inb_pwrite - release inbound port-write message service
  323. * @rdev: RIO device which registered for inbound port-write callback
  324. *
  325. * Removes callback from the rio_dev structure. Returns 0 if the request
  326. * has been satisfied.
  327. */
  328. int rio_release_inb_pwrite(struct rio_dev *rdev)
  329. {
  330. int rc = -ENOMEM;
  331. spin_lock(&rio_global_list_lock);
  332. if (rdev->pwcback) {
  333. rdev->pwcback = NULL;
  334. rc = 0;
  335. }
  336. spin_unlock(&rio_global_list_lock);
  337. return rc;
  338. }
  339. EXPORT_SYMBOL_GPL(rio_release_inb_pwrite);
  340. /**
  341. * rio_map_inb_region -- Map inbound memory region.
  342. * @mport: Master port.
  343. * @local: physical address of memory region to be mapped
  344. * @rbase: RIO base address assigned to this window
  345. * @size: Size of the memory region
  346. * @rflags: Flags for mapping.
  347. *
  348. * Return: 0 -- Success.
  349. *
  350. * This function will create the mapping from RIO space to local memory.
  351. */
  352. int rio_map_inb_region(struct rio_mport *mport, dma_addr_t local,
  353. u64 rbase, u32 size, u32 rflags)
  354. {
  355. int rc = 0;
  356. unsigned long flags;
  357. if (!mport->ops->map_inb)
  358. return -1;
  359. spin_lock_irqsave(&rio_mmap_lock, flags);
  360. rc = mport->ops->map_inb(mport, local, rbase, size, rflags);
  361. spin_unlock_irqrestore(&rio_mmap_lock, flags);
  362. return rc;
  363. }
  364. EXPORT_SYMBOL_GPL(rio_map_inb_region);
  365. /**
  366. * rio_unmap_inb_region -- Unmap the inbound memory region
  367. * @mport: Master port
  368. * @lstart: physical address of memory region to be unmapped
  369. */
  370. void rio_unmap_inb_region(struct rio_mport *mport, dma_addr_t lstart)
  371. {
  372. unsigned long flags;
  373. if (!mport->ops->unmap_inb)
  374. return;
  375. spin_lock_irqsave(&rio_mmap_lock, flags);
  376. mport->ops->unmap_inb(mport, lstart);
  377. spin_unlock_irqrestore(&rio_mmap_lock, flags);
  378. }
  379. EXPORT_SYMBOL_GPL(rio_unmap_inb_region);
  380. /**
  381. * rio_mport_get_physefb - Helper function that returns register offset
  382. * for Physical Layer Extended Features Block.
  383. * @port: Master port to issue transaction
  384. * @local: Indicate a local master port or remote device access
  385. * @destid: Destination ID of the device
  386. * @hopcount: Number of switch hops to the device
  387. */
  388. u32
  389. rio_mport_get_physefb(struct rio_mport *port, int local,
  390. u16 destid, u8 hopcount)
  391. {
  392. u32 ext_ftr_ptr;
  393. u32 ftr_header;
  394. ext_ftr_ptr = rio_mport_get_efb(port, local, destid, hopcount, 0);
  395. while (ext_ftr_ptr) {
  396. if (local)
  397. rio_local_read_config_32(port, ext_ftr_ptr,
  398. &ftr_header);
  399. else
  400. rio_mport_read_config_32(port, destid, hopcount,
  401. ext_ftr_ptr, &ftr_header);
  402. ftr_header = RIO_GET_BLOCK_ID(ftr_header);
  403. switch (ftr_header) {
  404. case RIO_EFB_SER_EP_ID_V13P:
  405. case RIO_EFB_SER_EP_REC_ID_V13P:
  406. case RIO_EFB_SER_EP_FREE_ID_V13P:
  407. case RIO_EFB_SER_EP_ID:
  408. case RIO_EFB_SER_EP_REC_ID:
  409. case RIO_EFB_SER_EP_FREE_ID:
  410. case RIO_EFB_SER_EP_FREC_ID:
  411. return ext_ftr_ptr;
  412. default:
  413. break;
  414. }
  415. ext_ftr_ptr = rio_mport_get_efb(port, local, destid,
  416. hopcount, ext_ftr_ptr);
  417. }
  418. return ext_ftr_ptr;
  419. }
  420. /**
  421. * rio_get_comptag - Begin or continue searching for a RIO device by component tag
  422. * @comp_tag: RIO component tag to match
  423. * @from: Previous RIO device found in search, or %NULL for new search
  424. *
  425. * Iterates through the list of known RIO devices. If a RIO device is
  426. * found with a matching @comp_tag, a pointer to its device
  427. * structure is returned. Otherwise, %NULL is returned. A new search
  428. * is initiated by passing %NULL to the @from argument. Otherwise, if
  429. * @from is not %NULL, searches continue from next device on the global
  430. * list.
  431. */
  432. struct rio_dev *rio_get_comptag(u32 comp_tag, struct rio_dev *from)
  433. {
  434. struct list_head *n;
  435. struct rio_dev *rdev;
  436. spin_lock(&rio_global_list_lock);
  437. n = from ? from->global_list.next : rio_devices.next;
  438. while (n && (n != &rio_devices)) {
  439. rdev = rio_dev_g(n);
  440. if (rdev->comp_tag == comp_tag)
  441. goto exit;
  442. n = n->next;
  443. }
  444. rdev = NULL;
  445. exit:
  446. spin_unlock(&rio_global_list_lock);
  447. return rdev;
  448. }
  449. /**
  450. * rio_set_port_lockout - Sets/clears LOCKOUT bit (RIO EM 1.3) for a switch port.
  451. * @rdev: Pointer to RIO device control structure
  452. * @pnum: Switch port number to set LOCKOUT bit
  453. * @lock: Operation : set (=1) or clear (=0)
  454. */
  455. int rio_set_port_lockout(struct rio_dev *rdev, u32 pnum, int lock)
  456. {
  457. u32 regval;
  458. rio_read_config_32(rdev,
  459. rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
  460. &regval);
  461. if (lock)
  462. regval |= RIO_PORT_N_CTL_LOCKOUT;
  463. else
  464. regval &= ~RIO_PORT_N_CTL_LOCKOUT;
  465. rio_write_config_32(rdev,
  466. rdev->phys_efptr + RIO_PORT_N_CTL_CSR(pnum),
  467. regval);
  468. return 0;
  469. }
  470. /**
  471. * rio_chk_dev_route - Validate route to the specified device.
  472. * @rdev: RIO device failed to respond
  473. * @nrdev: Last active device on the route to rdev
  474. * @npnum: nrdev's port number on the route to rdev
  475. *
  476. * Follows a route to the specified RIO device to determine the last available
  477. * device (and corresponding RIO port) on the route.
  478. */
  479. static int
  480. rio_chk_dev_route(struct rio_dev *rdev, struct rio_dev **nrdev, int *npnum)
  481. {
  482. u32 result;
  483. int p_port, rc = -EIO;
  484. struct rio_dev *prev = NULL;
  485. /* Find switch with failed RIO link */
  486. while (rdev->prev && (rdev->prev->pef & RIO_PEF_SWITCH)) {
  487. if (!rio_read_config_32(rdev->prev, RIO_DEV_ID_CAR, &result)) {
  488. prev = rdev->prev;
  489. break;
  490. }
  491. rdev = rdev->prev;
  492. }
  493. if (prev == NULL)
  494. goto err_out;
  495. p_port = prev->rswitch->route_table[rdev->destid];
  496. if (p_port != RIO_INVALID_ROUTE) {
  497. pr_debug("RIO: link failed on [%s]-P%d\n",
  498. rio_name(prev), p_port);
  499. *nrdev = prev;
  500. *npnum = p_port;
  501. rc = 0;
  502. } else
  503. pr_debug("RIO: failed to trace route to %s\n", rio_name(rdev));
  504. err_out:
  505. return rc;
  506. }
  507. /**
  508. * rio_mport_chk_dev_access - Validate access to the specified device.
  509. * @mport: Master port to send transactions
  510. * @destid: Device destination ID in network
  511. * @hopcount: Number of hops into the network
  512. */
  513. int
  514. rio_mport_chk_dev_access(struct rio_mport *mport, u16 destid, u8 hopcount)
  515. {
  516. int i = 0;
  517. u32 tmp;
  518. while (rio_mport_read_config_32(mport, destid, hopcount,
  519. RIO_DEV_ID_CAR, &tmp)) {
  520. i++;
  521. if (i == RIO_MAX_CHK_RETRY)
  522. return -EIO;
  523. mdelay(1);
  524. }
  525. return 0;
  526. }
  527. /**
  528. * rio_chk_dev_access - Validate access to the specified device.
  529. * @rdev: Pointer to RIO device control structure
  530. */
  531. static int rio_chk_dev_access(struct rio_dev *rdev)
  532. {
  533. return rio_mport_chk_dev_access(rdev->net->hport,
  534. rdev->destid, rdev->hopcount);
  535. }
  536. /**
  537. * rio_get_input_status - Sends a Link-Request/Input-Status control symbol and
  538. * returns link-response (if requested).
  539. * @rdev: RIO devive to issue Input-status command
  540. * @pnum: Device port number to issue the command
  541. * @lnkresp: Response from a link partner
  542. */
  543. static int
  544. rio_get_input_status(struct rio_dev *rdev, int pnum, u32 *lnkresp)
  545. {
  546. u32 regval;
  547. int checkcount;
  548. if (lnkresp) {
  549. /* Read from link maintenance response register
  550. * to clear valid bit */
  551. rio_read_config_32(rdev,
  552. rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
  553. &regval);
  554. udelay(50);
  555. }
  556. /* Issue Input-status command */
  557. rio_write_config_32(rdev,
  558. rdev->phys_efptr + RIO_PORT_N_MNT_REQ_CSR(pnum),
  559. RIO_MNT_REQ_CMD_IS);
  560. /* Exit if the response is not expected */
  561. if (lnkresp == NULL)
  562. return 0;
  563. checkcount = 3;
  564. while (checkcount--) {
  565. udelay(50);
  566. rio_read_config_32(rdev,
  567. rdev->phys_efptr + RIO_PORT_N_MNT_RSP_CSR(pnum),
  568. &regval);
  569. if (regval & RIO_PORT_N_MNT_RSP_RVAL) {
  570. *lnkresp = regval;
  571. return 0;
  572. }
  573. }
  574. return -EIO;
  575. }
  576. /**
  577. * rio_clr_err_stopped - Clears port Error-stopped states.
  578. * @rdev: Pointer to RIO device control structure
  579. * @pnum: Switch port number to clear errors
  580. * @err_status: port error status (if 0 reads register from device)
  581. */
  582. static int rio_clr_err_stopped(struct rio_dev *rdev, u32 pnum, u32 err_status)
  583. {
  584. struct rio_dev *nextdev = rdev->rswitch->nextdev[pnum];
  585. u32 regval;
  586. u32 far_ackid, far_linkstat, near_ackid;
  587. if (err_status == 0)
  588. rio_read_config_32(rdev,
  589. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  590. &err_status);
  591. if (err_status & RIO_PORT_N_ERR_STS_PW_OUT_ES) {
  592. pr_debug("RIO_EM: servicing Output Error-Stopped state\n");
  593. /*
  594. * Send a Link-Request/Input-Status control symbol
  595. */
  596. if (rio_get_input_status(rdev, pnum, &regval)) {
  597. pr_debug("RIO_EM: Input-status response timeout\n");
  598. goto rd_err;
  599. }
  600. pr_debug("RIO_EM: SP%d Input-status response=0x%08x\n",
  601. pnum, regval);
  602. far_ackid = (regval & RIO_PORT_N_MNT_RSP_ASTAT) >> 5;
  603. far_linkstat = regval & RIO_PORT_N_MNT_RSP_LSTAT;
  604. rio_read_config_32(rdev,
  605. rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
  606. &regval);
  607. pr_debug("RIO_EM: SP%d_ACK_STS_CSR=0x%08x\n", pnum, regval);
  608. near_ackid = (regval & RIO_PORT_N_ACK_INBOUND) >> 24;
  609. pr_debug("RIO_EM: SP%d far_ackID=0x%02x far_linkstat=0x%02x" \
  610. " near_ackID=0x%02x\n",
  611. pnum, far_ackid, far_linkstat, near_ackid);
  612. /*
  613. * If required, synchronize ackIDs of near and
  614. * far sides.
  615. */
  616. if ((far_ackid != ((regval & RIO_PORT_N_ACK_OUTSTAND) >> 8)) ||
  617. (far_ackid != (regval & RIO_PORT_N_ACK_OUTBOUND))) {
  618. /* Align near outstanding/outbound ackIDs with
  619. * far inbound.
  620. */
  621. rio_write_config_32(rdev,
  622. rdev->phys_efptr + RIO_PORT_N_ACK_STS_CSR(pnum),
  623. (near_ackid << 24) |
  624. (far_ackid << 8) | far_ackid);
  625. /* Align far outstanding/outbound ackIDs with
  626. * near inbound.
  627. */
  628. far_ackid++;
  629. if (nextdev)
  630. rio_write_config_32(nextdev,
  631. nextdev->phys_efptr +
  632. RIO_PORT_N_ACK_STS_CSR(RIO_GET_PORT_NUM(nextdev->swpinfo)),
  633. (far_ackid << 24) |
  634. (near_ackid << 8) | near_ackid);
  635. else
  636. pr_debug("RIO_EM: Invalid nextdev pointer (NULL)\n");
  637. }
  638. rd_err:
  639. rio_read_config_32(rdev,
  640. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  641. &err_status);
  642. pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
  643. }
  644. if ((err_status & RIO_PORT_N_ERR_STS_PW_INP_ES) && nextdev) {
  645. pr_debug("RIO_EM: servicing Input Error-Stopped state\n");
  646. rio_get_input_status(nextdev,
  647. RIO_GET_PORT_NUM(nextdev->swpinfo), NULL);
  648. udelay(50);
  649. rio_read_config_32(rdev,
  650. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(pnum),
  651. &err_status);
  652. pr_debug("RIO_EM: SP%d_ERR_STS_CSR=0x%08x\n", pnum, err_status);
  653. }
  654. return (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
  655. RIO_PORT_N_ERR_STS_PW_INP_ES)) ? 1 : 0;
  656. }
  657. /**
  658. * rio_inb_pwrite_handler - process inbound port-write message
  659. * @pw_msg: pointer to inbound port-write message
  660. *
  661. * Processes an inbound port-write message. Returns 0 if the request
  662. * has been satisfied.
  663. */
  664. int rio_inb_pwrite_handler(union rio_pw_msg *pw_msg)
  665. {
  666. struct rio_dev *rdev;
  667. u32 err_status, em_perrdet, em_ltlerrdet;
  668. int rc, portnum;
  669. rdev = rio_get_comptag((pw_msg->em.comptag & RIO_CTAG_UDEVID), NULL);
  670. if (rdev == NULL) {
  671. /* Device removed or enumeration error */
  672. pr_debug("RIO: %s No matching device for CTag 0x%08x\n",
  673. __func__, pw_msg->em.comptag);
  674. return -EIO;
  675. }
  676. pr_debug("RIO: Port-Write message from %s\n", rio_name(rdev));
  677. #ifdef DEBUG_PW
  678. {
  679. u32 i;
  680. for (i = 0; i < RIO_PW_MSG_SIZE/sizeof(u32);) {
  681. pr_debug("0x%02x: %08x %08x %08x %08x\n",
  682. i*4, pw_msg->raw[i], pw_msg->raw[i + 1],
  683. pw_msg->raw[i + 2], pw_msg->raw[i + 3]);
  684. i += 4;
  685. }
  686. }
  687. #endif
  688. /* Call an external service function (if such is registered
  689. * for this device). This may be the service for endpoints that send
  690. * device-specific port-write messages. End-point messages expected
  691. * to be handled completely by EP specific device driver.
  692. * For switches rc==0 signals that no standard processing required.
  693. */
  694. if (rdev->pwcback != NULL) {
  695. rc = rdev->pwcback(rdev, pw_msg, 0);
  696. if (rc == 0)
  697. return 0;
  698. }
  699. portnum = pw_msg->em.is_port & 0xFF;
  700. /* Check if device and route to it are functional:
  701. * Sometimes devices may send PW message(s) just before being
  702. * powered down (or link being lost).
  703. */
  704. if (rio_chk_dev_access(rdev)) {
  705. pr_debug("RIO: device access failed - get link partner\n");
  706. /* Scan route to the device and identify failed link.
  707. * This will replace device and port reported in PW message.
  708. * PW message should not be used after this point.
  709. */
  710. if (rio_chk_dev_route(rdev, &rdev, &portnum)) {
  711. pr_err("RIO: Route trace for %s failed\n",
  712. rio_name(rdev));
  713. return -EIO;
  714. }
  715. pw_msg = NULL;
  716. }
  717. /* For End-point devices processing stops here */
  718. if (!(rdev->pef & RIO_PEF_SWITCH))
  719. return 0;
  720. if (rdev->phys_efptr == 0) {
  721. pr_err("RIO_PW: Bad switch initialization for %s\n",
  722. rio_name(rdev));
  723. return 0;
  724. }
  725. /*
  726. * Process the port-write notification from switch
  727. */
  728. if (rdev->rswitch->em_handle)
  729. rdev->rswitch->em_handle(rdev, portnum);
  730. rio_read_config_32(rdev,
  731. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
  732. &err_status);
  733. pr_debug("RIO_PW: SP%d_ERR_STS_CSR=0x%08x\n", portnum, err_status);
  734. if (err_status & RIO_PORT_N_ERR_STS_PORT_OK) {
  735. if (!(rdev->rswitch->port_ok & (1 << portnum))) {
  736. rdev->rswitch->port_ok |= (1 << portnum);
  737. rio_set_port_lockout(rdev, portnum, 0);
  738. /* Schedule Insertion Service */
  739. pr_debug("RIO_PW: Device Insertion on [%s]-P%d\n",
  740. rio_name(rdev), portnum);
  741. }
  742. /* Clear error-stopped states (if reported).
  743. * Depending on the link partner state, two attempts
  744. * may be needed for successful recovery.
  745. */
  746. if (err_status & (RIO_PORT_N_ERR_STS_PW_OUT_ES |
  747. RIO_PORT_N_ERR_STS_PW_INP_ES)) {
  748. if (rio_clr_err_stopped(rdev, portnum, err_status))
  749. rio_clr_err_stopped(rdev, portnum, 0);
  750. }
  751. } else { /* if (err_status & RIO_PORT_N_ERR_STS_PORT_UNINIT) */
  752. if (rdev->rswitch->port_ok & (1 << portnum)) {
  753. rdev->rswitch->port_ok &= ~(1 << portnum);
  754. rio_set_port_lockout(rdev, portnum, 1);
  755. rio_write_config_32(rdev,
  756. rdev->phys_efptr +
  757. RIO_PORT_N_ACK_STS_CSR(portnum),
  758. RIO_PORT_N_ACK_CLEAR);
  759. /* Schedule Extraction Service */
  760. pr_debug("RIO_PW: Device Extraction on [%s]-P%d\n",
  761. rio_name(rdev), portnum);
  762. }
  763. }
  764. rio_read_config_32(rdev,
  765. rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), &em_perrdet);
  766. if (em_perrdet) {
  767. pr_debug("RIO_PW: RIO_EM_P%d_ERR_DETECT=0x%08x\n",
  768. portnum, em_perrdet);
  769. /* Clear EM Port N Error Detect CSR */
  770. rio_write_config_32(rdev,
  771. rdev->em_efptr + RIO_EM_PN_ERR_DETECT(portnum), 0);
  772. }
  773. rio_read_config_32(rdev,
  774. rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, &em_ltlerrdet);
  775. if (em_ltlerrdet) {
  776. pr_debug("RIO_PW: RIO_EM_LTL_ERR_DETECT=0x%08x\n",
  777. em_ltlerrdet);
  778. /* Clear EM L/T Layer Error Detect CSR */
  779. rio_write_config_32(rdev,
  780. rdev->em_efptr + RIO_EM_LTL_ERR_DETECT, 0);
  781. }
  782. /* Clear remaining error bits and Port-Write Pending bit */
  783. rio_write_config_32(rdev,
  784. rdev->phys_efptr + RIO_PORT_N_ERR_STS_CSR(portnum),
  785. err_status);
  786. return 0;
  787. }
  788. EXPORT_SYMBOL_GPL(rio_inb_pwrite_handler);
  789. /**
  790. * rio_mport_get_efb - get pointer to next extended features block
  791. * @port: Master port to issue transaction
  792. * @local: Indicate a local master port or remote device access
  793. * @destid: Destination ID of the device
  794. * @hopcount: Number of switch hops to the device
  795. * @from: Offset of current Extended Feature block header (if 0 starts
  796. * from ExtFeaturePtr)
  797. */
  798. u32
  799. rio_mport_get_efb(struct rio_mport *port, int local, u16 destid,
  800. u8 hopcount, u32 from)
  801. {
  802. u32 reg_val;
  803. if (from == 0) {
  804. if (local)
  805. rio_local_read_config_32(port, RIO_ASM_INFO_CAR,
  806. &reg_val);
  807. else
  808. rio_mport_read_config_32(port, destid, hopcount,
  809. RIO_ASM_INFO_CAR, &reg_val);
  810. return reg_val & RIO_EXT_FTR_PTR_MASK;
  811. } else {
  812. if (local)
  813. rio_local_read_config_32(port, from, &reg_val);
  814. else
  815. rio_mport_read_config_32(port, destid, hopcount,
  816. from, &reg_val);
  817. return RIO_GET_BLOCK_ID(reg_val);
  818. }
  819. }
  820. /**
  821. * rio_mport_get_feature - query for devices' extended features
  822. * @port: Master port to issue transaction
  823. * @local: Indicate a local master port or remote device access
  824. * @destid: Destination ID of the device
  825. * @hopcount: Number of switch hops to the device
  826. * @ftr: Extended feature code
  827. *
  828. * Tell if a device supports a given RapidIO capability.
  829. * Returns the offset of the requested extended feature
  830. * block within the device's RIO configuration space or
  831. * 0 in case the device does not support it. Possible
  832. * values for @ftr:
  833. *
  834. * %RIO_EFB_PAR_EP_ID LP/LVDS EP Devices
  835. *
  836. * %RIO_EFB_PAR_EP_REC_ID LP/LVDS EP Recovery Devices
  837. *
  838. * %RIO_EFB_PAR_EP_FREE_ID LP/LVDS EP Free Devices
  839. *
  840. * %RIO_EFB_SER_EP_ID LP/Serial EP Devices
  841. *
  842. * %RIO_EFB_SER_EP_REC_ID LP/Serial EP Recovery Devices
  843. *
  844. * %RIO_EFB_SER_EP_FREE_ID LP/Serial EP Free Devices
  845. */
  846. u32
  847. rio_mport_get_feature(struct rio_mport * port, int local, u16 destid,
  848. u8 hopcount, int ftr)
  849. {
  850. u32 asm_info, ext_ftr_ptr, ftr_header;
  851. if (local)
  852. rio_local_read_config_32(port, RIO_ASM_INFO_CAR, &asm_info);
  853. else
  854. rio_mport_read_config_32(port, destid, hopcount,
  855. RIO_ASM_INFO_CAR, &asm_info);
  856. ext_ftr_ptr = asm_info & RIO_EXT_FTR_PTR_MASK;
  857. while (ext_ftr_ptr) {
  858. if (local)
  859. rio_local_read_config_32(port, ext_ftr_ptr,
  860. &ftr_header);
  861. else
  862. rio_mport_read_config_32(port, destid, hopcount,
  863. ext_ftr_ptr, &ftr_header);
  864. if (RIO_GET_BLOCK_ID(ftr_header) == ftr)
  865. return ext_ftr_ptr;
  866. if (!(ext_ftr_ptr = RIO_GET_BLOCK_PTR(ftr_header)))
  867. break;
  868. }
  869. return 0;
  870. }
  871. /**
  872. * rio_get_asm - Begin or continue searching for a RIO device by vid/did/asm_vid/asm_did
  873. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  874. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  875. * @asm_vid: RIO asm_vid to match or %RIO_ANY_ID to match all asm_vids
  876. * @asm_did: RIO asm_did to match or %RIO_ANY_ID to match all asm_dids
  877. * @from: Previous RIO device found in search, or %NULL for new search
  878. *
  879. * Iterates through the list of known RIO devices. If a RIO device is
  880. * found with a matching @vid, @did, @asm_vid, @asm_did, the reference
  881. * count to the device is incrememted and a pointer to its device
  882. * structure is returned. Otherwise, %NULL is returned. A new search
  883. * is initiated by passing %NULL to the @from argument. Otherwise, if
  884. * @from is not %NULL, searches continue from next device on the global
  885. * list. The reference count for @from is always decremented if it is
  886. * not %NULL.
  887. */
  888. struct rio_dev *rio_get_asm(u16 vid, u16 did,
  889. u16 asm_vid, u16 asm_did, struct rio_dev *from)
  890. {
  891. struct list_head *n;
  892. struct rio_dev *rdev;
  893. WARN_ON(in_interrupt());
  894. spin_lock(&rio_global_list_lock);
  895. n = from ? from->global_list.next : rio_devices.next;
  896. while (n && (n != &rio_devices)) {
  897. rdev = rio_dev_g(n);
  898. if ((vid == RIO_ANY_ID || rdev->vid == vid) &&
  899. (did == RIO_ANY_ID || rdev->did == did) &&
  900. (asm_vid == RIO_ANY_ID || rdev->asm_vid == asm_vid) &&
  901. (asm_did == RIO_ANY_ID || rdev->asm_did == asm_did))
  902. goto exit;
  903. n = n->next;
  904. }
  905. rdev = NULL;
  906. exit:
  907. rio_dev_put(from);
  908. rdev = rio_dev_get(rdev);
  909. spin_unlock(&rio_global_list_lock);
  910. return rdev;
  911. }
  912. /**
  913. * rio_get_device - Begin or continue searching for a RIO device by vid/did
  914. * @vid: RIO vid to match or %RIO_ANY_ID to match all vids
  915. * @did: RIO did to match or %RIO_ANY_ID to match all dids
  916. * @from: Previous RIO device found in search, or %NULL for new search
  917. *
  918. * Iterates through the list of known RIO devices. If a RIO device is
  919. * found with a matching @vid and @did, the reference count to the
  920. * device is incrememted and a pointer to its device structure is returned.
  921. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  922. * to the @from argument. Otherwise, if @from is not %NULL, searches
  923. * continue from next device on the global list. The reference count for
  924. * @from is always decremented if it is not %NULL.
  925. */
  926. struct rio_dev *rio_get_device(u16 vid, u16 did, struct rio_dev *from)
  927. {
  928. return rio_get_asm(vid, did, RIO_ANY_ID, RIO_ANY_ID, from);
  929. }
  930. /**
  931. * rio_std_route_add_entry - Add switch route table entry using standard
  932. * registers defined in RIO specification rev.1.3
  933. * @mport: Master port to issue transaction
  934. * @destid: Destination ID of the device
  935. * @hopcount: Number of switch hops to the device
  936. * @table: routing table ID (global or port-specific)
  937. * @route_destid: destID entry in the RT
  938. * @route_port: destination port for specified destID
  939. */
  940. int rio_std_route_add_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  941. u16 table, u16 route_destid, u8 route_port)
  942. {
  943. if (table == RIO_GLOBAL_TABLE) {
  944. rio_mport_write_config_32(mport, destid, hopcount,
  945. RIO_STD_RTE_CONF_DESTID_SEL_CSR,
  946. (u32)route_destid);
  947. rio_mport_write_config_32(mport, destid, hopcount,
  948. RIO_STD_RTE_CONF_PORT_SEL_CSR,
  949. (u32)route_port);
  950. }
  951. udelay(10);
  952. return 0;
  953. }
  954. /**
  955. * rio_std_route_get_entry - Read switch route table entry (port number)
  956. * associated with specified destID using standard registers defined in RIO
  957. * specification rev.1.3
  958. * @mport: Master port to issue transaction
  959. * @destid: Destination ID of the device
  960. * @hopcount: Number of switch hops to the device
  961. * @table: routing table ID (global or port-specific)
  962. * @route_destid: destID entry in the RT
  963. * @route_port: returned destination port for specified destID
  964. */
  965. int rio_std_route_get_entry(struct rio_mport *mport, u16 destid, u8 hopcount,
  966. u16 table, u16 route_destid, u8 *route_port)
  967. {
  968. u32 result;
  969. if (table == RIO_GLOBAL_TABLE) {
  970. rio_mport_write_config_32(mport, destid, hopcount,
  971. RIO_STD_RTE_CONF_DESTID_SEL_CSR, route_destid);
  972. rio_mport_read_config_32(mport, destid, hopcount,
  973. RIO_STD_RTE_CONF_PORT_SEL_CSR, &result);
  974. *route_port = (u8)result;
  975. }
  976. return 0;
  977. }
  978. /**
  979. * rio_std_route_clr_table - Clear swotch route table using standard registers
  980. * defined in RIO specification rev.1.3.
  981. * @mport: Master port to issue transaction
  982. * @destid: Destination ID of the device
  983. * @hopcount: Number of switch hops to the device
  984. * @table: routing table ID (global or port-specific)
  985. */
  986. int rio_std_route_clr_table(struct rio_mport *mport, u16 destid, u8 hopcount,
  987. u16 table)
  988. {
  989. u32 max_destid = 0xff;
  990. u32 i, pef, id_inc = 1, ext_cfg = 0;
  991. u32 port_sel = RIO_INVALID_ROUTE;
  992. if (table == RIO_GLOBAL_TABLE) {
  993. rio_mport_read_config_32(mport, destid, hopcount,
  994. RIO_PEF_CAR, &pef);
  995. if (mport->sys_size) {
  996. rio_mport_read_config_32(mport, destid, hopcount,
  997. RIO_SWITCH_RT_LIMIT,
  998. &max_destid);
  999. max_destid &= RIO_RT_MAX_DESTID;
  1000. }
  1001. if (pef & RIO_PEF_EXT_RT) {
  1002. ext_cfg = 0x80000000;
  1003. id_inc = 4;
  1004. port_sel = (RIO_INVALID_ROUTE << 24) |
  1005. (RIO_INVALID_ROUTE << 16) |
  1006. (RIO_INVALID_ROUTE << 8) |
  1007. RIO_INVALID_ROUTE;
  1008. }
  1009. for (i = 0; i <= max_destid;) {
  1010. rio_mport_write_config_32(mport, destid, hopcount,
  1011. RIO_STD_RTE_CONF_DESTID_SEL_CSR,
  1012. ext_cfg | i);
  1013. rio_mport_write_config_32(mport, destid, hopcount,
  1014. RIO_STD_RTE_CONF_PORT_SEL_CSR,
  1015. port_sel);
  1016. i += id_inc;
  1017. }
  1018. }
  1019. udelay(10);
  1020. return 0;
  1021. }
  1022. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  1023. static bool rio_chan_filter(struct dma_chan *chan, void *arg)
  1024. {
  1025. struct rio_dev *rdev = arg;
  1026. /* Check that DMA device belongs to the right MPORT */
  1027. return (rdev->net->hport ==
  1028. container_of(chan->device, struct rio_mport, dma));
  1029. }
  1030. /**
  1031. * rio_request_dma - request RapidIO capable DMA channel that supports
  1032. * specified target RapidIO device.
  1033. * @rdev: RIO device control structure
  1034. *
  1035. * Returns pointer to allocated DMA channel or NULL if failed.
  1036. */
  1037. struct dma_chan *rio_request_dma(struct rio_dev *rdev)
  1038. {
  1039. dma_cap_mask_t mask;
  1040. struct dma_chan *dchan;
  1041. dma_cap_zero(mask);
  1042. dma_cap_set(DMA_SLAVE, mask);
  1043. dchan = dma_request_channel(mask, rio_chan_filter, rdev);
  1044. return dchan;
  1045. }
  1046. EXPORT_SYMBOL_GPL(rio_request_dma);
  1047. /**
  1048. * rio_release_dma - release specified DMA channel
  1049. * @dchan: DMA channel to release
  1050. */
  1051. void rio_release_dma(struct dma_chan *dchan)
  1052. {
  1053. dma_release_channel(dchan);
  1054. }
  1055. EXPORT_SYMBOL_GPL(rio_release_dma);
  1056. /**
  1057. * rio_dma_prep_slave_sg - RapidIO specific wrapper
  1058. * for device_prep_slave_sg callback defined by DMAENGINE.
  1059. * @rdev: RIO device control structure
  1060. * @dchan: DMA channel to configure
  1061. * @data: RIO specific data descriptor
  1062. * @direction: DMA data transfer direction (TO or FROM the device)
  1063. * @flags: dmaengine defined flags
  1064. *
  1065. * Initializes RapidIO capable DMA channel for the specified data transfer.
  1066. * Uses DMA channel private extension to pass information related to remote
  1067. * target RIO device.
  1068. * Returns pointer to DMA transaction descriptor or NULL if failed.
  1069. */
  1070. struct dma_async_tx_descriptor *rio_dma_prep_slave_sg(struct rio_dev *rdev,
  1071. struct dma_chan *dchan, struct rio_dma_data *data,
  1072. enum dma_transfer_direction direction, unsigned long flags)
  1073. {
  1074. struct dma_async_tx_descriptor *txd = NULL;
  1075. struct rio_dma_ext rio_ext;
  1076. if (dchan->device->device_prep_slave_sg == NULL) {
  1077. pr_err("%s: prep_rio_sg == NULL\n", __func__);
  1078. return NULL;
  1079. }
  1080. rio_ext.destid = rdev->destid;
  1081. rio_ext.rio_addr_u = data->rio_addr_u;
  1082. rio_ext.rio_addr = data->rio_addr;
  1083. rio_ext.wr_type = data->wr_type;
  1084. txd = dmaengine_prep_rio_sg(dchan, data->sg, data->sg_len,
  1085. direction, flags, &rio_ext);
  1086. return txd;
  1087. }
  1088. EXPORT_SYMBOL_GPL(rio_dma_prep_slave_sg);
  1089. #endif /* CONFIG_RAPIDIO_DMA_ENGINE */
  1090. static void rio_fixup_device(struct rio_dev *dev)
  1091. {
  1092. }
  1093. static int rio_init(void)
  1094. {
  1095. struct rio_dev *dev = NULL;
  1096. while ((dev = rio_get_device(RIO_ANY_ID, RIO_ANY_ID, dev)) != NULL) {
  1097. rio_fixup_device(dev);
  1098. }
  1099. return 0;
  1100. }
  1101. static struct workqueue_struct *rio_wq;
  1102. struct rio_disc_work {
  1103. struct work_struct work;
  1104. struct rio_mport *mport;
  1105. };
  1106. static void disc_work_handler(struct work_struct *_work)
  1107. {
  1108. struct rio_disc_work *work;
  1109. work = container_of(_work, struct rio_disc_work, work);
  1110. pr_debug("RIO: discovery work for mport %d %s\n",
  1111. work->mport->id, work->mport->name);
  1112. rio_disc_mport(work->mport);
  1113. }
  1114. int rio_init_mports(void)
  1115. {
  1116. struct rio_mport *port;
  1117. struct rio_disc_work *work;
  1118. int n = 0;
  1119. if (!next_portid)
  1120. return -ENODEV;
  1121. /*
  1122. * First, run enumerations and check if we need to perform discovery
  1123. * on any of the registered mports.
  1124. */
  1125. list_for_each_entry(port, &rio_mports, node) {
  1126. if (port->host_deviceid >= 0)
  1127. rio_enum_mport(port);
  1128. else
  1129. n++;
  1130. }
  1131. if (!n)
  1132. goto no_disc;
  1133. /*
  1134. * If we have mports that require discovery schedule a discovery work
  1135. * for each of them. If the code below fails to allocate needed
  1136. * resources, exit without error to keep results of enumeration
  1137. * process (if any).
  1138. * TODO: Implement restart of dicovery process for all or
  1139. * individual discovering mports.
  1140. */
  1141. rio_wq = alloc_workqueue("riodisc", 0, 0);
  1142. if (!rio_wq) {
  1143. pr_err("RIO: unable allocate rio_wq\n");
  1144. goto no_disc;
  1145. }
  1146. work = kcalloc(n, sizeof *work, GFP_KERNEL);
  1147. if (!work) {
  1148. pr_err("RIO: no memory for work struct\n");
  1149. destroy_workqueue(rio_wq);
  1150. goto no_disc;
  1151. }
  1152. n = 0;
  1153. list_for_each_entry(port, &rio_mports, node) {
  1154. if (port->host_deviceid < 0) {
  1155. work[n].mport = port;
  1156. INIT_WORK(&work[n].work, disc_work_handler);
  1157. queue_work(rio_wq, &work[n].work);
  1158. n++;
  1159. }
  1160. }
  1161. flush_workqueue(rio_wq);
  1162. pr_debug("RIO: destroy discovery workqueue\n");
  1163. destroy_workqueue(rio_wq);
  1164. kfree(work);
  1165. no_disc:
  1166. rio_init();
  1167. return 0;
  1168. }
  1169. device_initcall_sync(rio_init_mports);
  1170. static int hdids[RIO_MAX_MPORTS + 1];
  1171. static int rio_get_hdid(int index)
  1172. {
  1173. if (!hdids[0] || hdids[0] <= index || index >= RIO_MAX_MPORTS)
  1174. return -1;
  1175. return hdids[index + 1];
  1176. }
  1177. static int rio_hdid_setup(char *str)
  1178. {
  1179. (void)get_options(str, ARRAY_SIZE(hdids), hdids);
  1180. return 1;
  1181. }
  1182. __setup("riohdid=", rio_hdid_setup);
  1183. int rio_register_mport(struct rio_mport *port)
  1184. {
  1185. if (next_portid >= RIO_MAX_MPORTS) {
  1186. pr_err("RIO: reached specified max number of mports\n");
  1187. return 1;
  1188. }
  1189. port->id = next_portid++;
  1190. port->host_deviceid = rio_get_hdid(port->id);
  1191. list_add_tail(&port->node, &rio_mports);
  1192. return 0;
  1193. }
  1194. EXPORT_SYMBOL_GPL(rio_local_get_device_id);
  1195. EXPORT_SYMBOL_GPL(rio_get_device);
  1196. EXPORT_SYMBOL_GPL(rio_get_asm);
  1197. EXPORT_SYMBOL_GPL(rio_request_inb_dbell);
  1198. EXPORT_SYMBOL_GPL(rio_release_inb_dbell);
  1199. EXPORT_SYMBOL_GPL(rio_request_outb_dbell);
  1200. EXPORT_SYMBOL_GPL(rio_release_outb_dbell);
  1201. EXPORT_SYMBOL_GPL(rio_request_inb_mbox);
  1202. EXPORT_SYMBOL_GPL(rio_release_inb_mbox);
  1203. EXPORT_SYMBOL_GPL(rio_request_outb_mbox);
  1204. EXPORT_SYMBOL_GPL(rio_release_outb_mbox);