xillybus_core.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  1. /*
  2. * linux/drivers/misc/xillybus_core.c
  3. *
  4. * Copyright 2011 Xillybus Ltd, http://xillybus.com
  5. *
  6. * Driver for the Xillybus FPGA/host framework.
  7. *
  8. * This driver interfaces with a special IP core in an FPGA, setting up
  9. * a pipe between a hardware FIFO in the programmable logic and a device
  10. * file in the host. The number of such pipes and their attributes are
  11. * set up on the logic. This driver detects these automatically and
  12. * creates the device files accordingly.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the smems of the GNU General Public License as published by
  16. * the Free Software Foundation; version 2 of the License.
  17. */
  18. #include <linux/list.h>
  19. #include <linux/device.h>
  20. #include <linux/module.h>
  21. #include <linux/io.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/sched.h>
  25. #include <linux/fs.h>
  26. #include <linux/cdev.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/mutex.h>
  29. #include <linux/crc32.h>
  30. #include <linux/poll.h>
  31. #include <linux/delay.h>
  32. #include <linux/slab.h>
  33. #include <linux/workqueue.h>
  34. #include "xillybus.h"
  35. MODULE_DESCRIPTION("Xillybus core functions");
  36. MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
  37. MODULE_VERSION("1.07");
  38. MODULE_ALIAS("xillybus_core");
  39. MODULE_LICENSE("GPL v2");
  40. /* General timeout is 100 ms, rx timeout is 10 ms */
  41. #define XILLY_RX_TIMEOUT (10*HZ/1000)
  42. #define XILLY_TIMEOUT (100*HZ/1000)
  43. #define fpga_msg_ctrl_reg 0x0008
  44. #define fpga_dma_control_reg 0x0020
  45. #define fpga_dma_bufno_reg 0x0024
  46. #define fpga_dma_bufaddr_lowaddr_reg 0x0028
  47. #define fpga_dma_bufaddr_highaddr_reg 0x002c
  48. #define fpga_buf_ctrl_reg 0x0030
  49. #define fpga_buf_offset_reg 0x0034
  50. #define fpga_endian_reg 0x0040
  51. #define XILLYMSG_OPCODE_RELEASEBUF 1
  52. #define XILLYMSG_OPCODE_QUIESCEACK 2
  53. #define XILLYMSG_OPCODE_FIFOEOF 3
  54. #define XILLYMSG_OPCODE_FATAL_ERROR 4
  55. #define XILLYMSG_OPCODE_NONEMPTY 5
  56. static const char xillyname[] = "xillybus";
  57. static struct class *xillybus_class;
  58. /*
  59. * ep_list_lock is the last lock to be taken; No other lock requests are
  60. * allowed while holding it. It merely protects list_of_endpoints, and not
  61. * the endpoints listed in it.
  62. */
  63. static LIST_HEAD(list_of_endpoints);
  64. static struct mutex ep_list_lock;
  65. static struct workqueue_struct *xillybus_wq;
  66. /*
  67. * Locking scheme: Mutexes protect invocations of character device methods.
  68. * If both locks are taken, wr_mutex is taken first, rd_mutex second.
  69. *
  70. * wr_spinlock protects wr_*_buf_idx, wr_empty, wr_sleepy, wr_ready and the
  71. * buffers' end_offset fields against changes made by IRQ handler (and in
  72. * theory, other file request handlers, but the mutex handles that). Nothing
  73. * else.
  74. * They are held for short direct memory manipulations. Needless to say,
  75. * no mutex locking is allowed when a spinlock is held.
  76. *
  77. * rd_spinlock does the same with rd_*_buf_idx, rd_empty and end_offset.
  78. *
  79. * register_mutex is endpoint-specific, and is held when non-atomic
  80. * register operations are performed. wr_mutex and rd_mutex may be
  81. * held when register_mutex is taken, but none of the spinlocks. Note that
  82. * register_mutex doesn't protect against sporadic buf_ctrl_reg writes
  83. * which are unrelated to buf_offset_reg, since they are harmless.
  84. *
  85. * Blocking on the wait queues is allowed with mutexes held, but not with
  86. * spinlocks.
  87. *
  88. * Only interruptible blocking is allowed on mutexes and wait queues.
  89. *
  90. * All in all, the locking order goes (with skips allowed, of course):
  91. * wr_mutex -> rd_mutex -> register_mutex -> wr_spinlock -> rd_spinlock
  92. */
  93. static void malformed_message(struct xilly_endpoint *endpoint, u32 *buf)
  94. {
  95. int opcode;
  96. int msg_channel, msg_bufno, msg_data, msg_dir;
  97. opcode = (buf[0] >> 24) & 0xff;
  98. msg_dir = buf[0] & 1;
  99. msg_channel = (buf[0] >> 1) & 0x7ff;
  100. msg_bufno = (buf[0] >> 12) & 0x3ff;
  101. msg_data = buf[1] & 0xfffffff;
  102. dev_warn(endpoint->dev,
  103. "Malformed message (skipping): opcode=%d, channel=%03x, dir=%d, bufno=%03x, data=%07x\n",
  104. opcode, msg_channel, msg_dir, msg_bufno, msg_data);
  105. }
  106. /*
  107. * xillybus_isr assumes the interrupt is allocated exclusively to it,
  108. * which is the natural case MSI and several other hardware-oriented
  109. * interrupts. Sharing is not allowed.
  110. */
  111. irqreturn_t xillybus_isr(int irq, void *data)
  112. {
  113. struct xilly_endpoint *ep = data;
  114. u32 *buf;
  115. unsigned int buf_size;
  116. int i;
  117. int opcode;
  118. unsigned int msg_channel, msg_bufno, msg_data, msg_dir;
  119. struct xilly_channel *channel;
  120. buf = ep->msgbuf_addr;
  121. buf_size = ep->msg_buf_size/sizeof(u32);
  122. ep->ephw->hw_sync_sgl_for_cpu(ep,
  123. ep->msgbuf_dma_addr,
  124. ep->msg_buf_size,
  125. DMA_FROM_DEVICE);
  126. for (i = 0; i < buf_size; i += 2) {
  127. if (((buf[i+1] >> 28) & 0xf) != ep->msg_counter) {
  128. malformed_message(ep, &buf[i]);
  129. dev_warn(ep->dev,
  130. "Sending a NACK on counter %x (instead of %x) on entry %d\n",
  131. ((buf[i+1] >> 28) & 0xf),
  132. ep->msg_counter,
  133. i/2);
  134. if (++ep->failed_messages > 10) {
  135. dev_err(ep->dev,
  136. "Lost sync with interrupt messages. Stopping.\n");
  137. } else {
  138. ep->ephw->hw_sync_sgl_for_device(
  139. ep,
  140. ep->msgbuf_dma_addr,
  141. ep->msg_buf_size,
  142. DMA_FROM_DEVICE);
  143. iowrite32(0x01, /* Message NACK */
  144. ep->registers + fpga_msg_ctrl_reg);
  145. }
  146. return IRQ_HANDLED;
  147. } else if (buf[i] & (1 << 22)) /* Last message */
  148. break;
  149. }
  150. if (i >= buf_size) {
  151. dev_err(ep->dev, "Bad interrupt message. Stopping.\n");
  152. return IRQ_HANDLED;
  153. }
  154. buf_size = i + 2;
  155. for (i = 0; i < buf_size; i += 2) { /* Scan through messages */
  156. opcode = (buf[i] >> 24) & 0xff;
  157. msg_dir = buf[i] & 1;
  158. msg_channel = (buf[i] >> 1) & 0x7ff;
  159. msg_bufno = (buf[i] >> 12) & 0x3ff;
  160. msg_data = buf[i+1] & 0xfffffff;
  161. switch (opcode) {
  162. case XILLYMSG_OPCODE_RELEASEBUF:
  163. if ((msg_channel > ep->num_channels) ||
  164. (msg_channel == 0)) {
  165. malformed_message(ep, &buf[i]);
  166. break;
  167. }
  168. channel = ep->channels[msg_channel];
  169. if (msg_dir) { /* Write channel */
  170. if (msg_bufno >= channel->num_wr_buffers) {
  171. malformed_message(ep, &buf[i]);
  172. break;
  173. }
  174. spin_lock(&channel->wr_spinlock);
  175. channel->wr_buffers[msg_bufno]->end_offset =
  176. msg_data;
  177. channel->wr_fpga_buf_idx = msg_bufno;
  178. channel->wr_empty = 0;
  179. channel->wr_sleepy = 0;
  180. spin_unlock(&channel->wr_spinlock);
  181. wake_up_interruptible(&channel->wr_wait);
  182. } else {
  183. /* Read channel */
  184. if (msg_bufno >= channel->num_rd_buffers) {
  185. malformed_message(ep, &buf[i]);
  186. break;
  187. }
  188. spin_lock(&channel->rd_spinlock);
  189. channel->rd_fpga_buf_idx = msg_bufno;
  190. channel->rd_full = 0;
  191. spin_unlock(&channel->rd_spinlock);
  192. wake_up_interruptible(&channel->rd_wait);
  193. if (!channel->rd_synchronous)
  194. queue_delayed_work(
  195. xillybus_wq,
  196. &channel->rd_workitem,
  197. XILLY_RX_TIMEOUT);
  198. }
  199. break;
  200. case XILLYMSG_OPCODE_NONEMPTY:
  201. if ((msg_channel > ep->num_channels) ||
  202. (msg_channel == 0) || (!msg_dir) ||
  203. !ep->channels[msg_channel]->wr_supports_nonempty) {
  204. malformed_message(ep, &buf[i]);
  205. break;
  206. }
  207. channel = ep->channels[msg_channel];
  208. if (msg_bufno >= channel->num_wr_buffers) {
  209. malformed_message(ep, &buf[i]);
  210. break;
  211. }
  212. spin_lock(&channel->wr_spinlock);
  213. if (msg_bufno == channel->wr_host_buf_idx)
  214. channel->wr_ready = 1;
  215. spin_unlock(&channel->wr_spinlock);
  216. wake_up_interruptible(&channel->wr_ready_wait);
  217. break;
  218. case XILLYMSG_OPCODE_QUIESCEACK:
  219. ep->idtlen = msg_data;
  220. wake_up_interruptible(&ep->ep_wait);
  221. break;
  222. case XILLYMSG_OPCODE_FIFOEOF:
  223. if ((msg_channel > ep->num_channels) ||
  224. (msg_channel == 0) || (!msg_dir) ||
  225. !ep->channels[msg_channel]->num_wr_buffers) {
  226. malformed_message(ep, &buf[i]);
  227. break;
  228. }
  229. channel = ep->channels[msg_channel];
  230. spin_lock(&channel->wr_spinlock);
  231. channel->wr_eof = msg_bufno;
  232. channel->wr_sleepy = 0;
  233. channel->wr_hangup = channel->wr_empty &&
  234. (channel->wr_host_buf_idx == msg_bufno);
  235. spin_unlock(&channel->wr_spinlock);
  236. wake_up_interruptible(&channel->wr_wait);
  237. break;
  238. case XILLYMSG_OPCODE_FATAL_ERROR:
  239. ep->fatal_error = 1;
  240. wake_up_interruptible(&ep->ep_wait); /* For select() */
  241. dev_err(ep->dev,
  242. "FPGA reported a fatal error. This means that the low-level communication with the device has failed. This hardware problem is most likely unrelated to Xillybus (neither kernel module nor FPGA core), but reports are still welcome. All I/O is aborted.\n");
  243. break;
  244. default:
  245. malformed_message(ep, &buf[i]);
  246. break;
  247. }
  248. }
  249. ep->ephw->hw_sync_sgl_for_device(ep,
  250. ep->msgbuf_dma_addr,
  251. ep->msg_buf_size,
  252. DMA_FROM_DEVICE);
  253. ep->msg_counter = (ep->msg_counter + 1) & 0xf;
  254. ep->failed_messages = 0;
  255. iowrite32(0x03, ep->registers + fpga_msg_ctrl_reg); /* Message ACK */
  256. return IRQ_HANDLED;
  257. }
  258. EXPORT_SYMBOL(xillybus_isr);
  259. /*
  260. * A few trivial memory management functions.
  261. * NOTE: These functions are used only on probe and remove, and therefore
  262. * no locks are applied!
  263. */
  264. static void xillybus_autoflush(struct work_struct *work);
  265. struct xilly_alloc_state {
  266. void *salami;
  267. int left_of_salami;
  268. int nbuffer;
  269. enum dma_data_direction direction;
  270. u32 regdirection;
  271. };
  272. static int xilly_get_dma_buffers(struct xilly_endpoint *ep,
  273. struct xilly_alloc_state *s,
  274. struct xilly_buffer **buffers,
  275. int bufnum, int bytebufsize)
  276. {
  277. int i, rc;
  278. dma_addr_t dma_addr;
  279. struct device *dev = ep->dev;
  280. struct xilly_buffer *this_buffer = NULL; /* Init to silence warning */
  281. if (buffers) { /* Not the message buffer */
  282. this_buffer = devm_kcalloc(dev, bufnum,
  283. sizeof(struct xilly_buffer),
  284. GFP_KERNEL);
  285. if (!this_buffer)
  286. return -ENOMEM;
  287. }
  288. for (i = 0; i < bufnum; i++) {
  289. /*
  290. * Buffers are expected in descending size order, so there
  291. * is either enough space for this buffer or none at all.
  292. */
  293. if ((s->left_of_salami < bytebufsize) &&
  294. (s->left_of_salami > 0)) {
  295. dev_err(ep->dev,
  296. "Corrupt buffer allocation in IDT. Aborting.\n");
  297. return -ENODEV;
  298. }
  299. if (s->left_of_salami == 0) {
  300. int allocorder, allocsize;
  301. allocsize = PAGE_SIZE;
  302. allocorder = 0;
  303. while (bytebufsize > allocsize) {
  304. allocsize *= 2;
  305. allocorder++;
  306. }
  307. s->salami = (void *) devm_get_free_pages(
  308. dev,
  309. GFP_KERNEL | __GFP_DMA32 | __GFP_ZERO,
  310. allocorder);
  311. if (!s->salami)
  312. return -ENOMEM;
  313. s->left_of_salami = allocsize;
  314. }
  315. rc = ep->ephw->map_single(ep, s->salami,
  316. bytebufsize, s->direction,
  317. &dma_addr);
  318. if (rc)
  319. return rc;
  320. iowrite32((u32) (dma_addr & 0xffffffff),
  321. ep->registers + fpga_dma_bufaddr_lowaddr_reg);
  322. iowrite32(((u32) ((((u64) dma_addr) >> 32) & 0xffffffff)),
  323. ep->registers + fpga_dma_bufaddr_highaddr_reg);
  324. if (buffers) { /* Not the message buffer */
  325. this_buffer->addr = s->salami;
  326. this_buffer->dma_addr = dma_addr;
  327. buffers[i] = this_buffer++;
  328. iowrite32(s->regdirection | s->nbuffer++,
  329. ep->registers + fpga_dma_bufno_reg);
  330. } else {
  331. ep->msgbuf_addr = s->salami;
  332. ep->msgbuf_dma_addr = dma_addr;
  333. ep->msg_buf_size = bytebufsize;
  334. iowrite32(s->regdirection,
  335. ep->registers + fpga_dma_bufno_reg);
  336. }
  337. s->left_of_salami -= bytebufsize;
  338. s->salami += bytebufsize;
  339. }
  340. return 0;
  341. }
  342. static int xilly_setupchannels(struct xilly_endpoint *ep,
  343. unsigned char *chandesc,
  344. int entries)
  345. {
  346. struct device *dev = ep->dev;
  347. int i, entry, rc;
  348. struct xilly_channel *channel;
  349. int channelnum, bufnum, bufsize, format, is_writebuf;
  350. int bytebufsize;
  351. int synchronous, allowpartial, exclusive_open, seekable;
  352. int supports_nonempty;
  353. int msg_buf_done = 0;
  354. struct xilly_alloc_state rd_alloc = {
  355. .salami = NULL,
  356. .left_of_salami = 0,
  357. .nbuffer = 1,
  358. .direction = DMA_TO_DEVICE,
  359. .regdirection = 0,
  360. };
  361. struct xilly_alloc_state wr_alloc = {
  362. .salami = NULL,
  363. .left_of_salami = 0,
  364. .nbuffer = 1,
  365. .direction = DMA_FROM_DEVICE,
  366. .regdirection = 0x80000000,
  367. };
  368. channel = devm_kcalloc(dev, ep->num_channels,
  369. sizeof(struct xilly_channel), GFP_KERNEL);
  370. if (!channel)
  371. return -ENOMEM;
  372. ep->channels = devm_kcalloc(dev, ep->num_channels + 1,
  373. sizeof(struct xilly_channel *),
  374. GFP_KERNEL);
  375. if (!ep->channels)
  376. return -ENOMEM;
  377. ep->channels[0] = NULL; /* Channel 0 is message buf. */
  378. /* Initialize all channels with defaults */
  379. for (i = 1; i <= ep->num_channels; i++) {
  380. channel->wr_buffers = NULL;
  381. channel->rd_buffers = NULL;
  382. channel->num_wr_buffers = 0;
  383. channel->num_rd_buffers = 0;
  384. channel->wr_fpga_buf_idx = -1;
  385. channel->wr_host_buf_idx = 0;
  386. channel->wr_host_buf_pos = 0;
  387. channel->wr_empty = 1;
  388. channel->wr_ready = 0;
  389. channel->wr_sleepy = 1;
  390. channel->rd_fpga_buf_idx = 0;
  391. channel->rd_host_buf_idx = 0;
  392. channel->rd_host_buf_pos = 0;
  393. channel->rd_full = 0;
  394. channel->wr_ref_count = 0;
  395. channel->rd_ref_count = 0;
  396. spin_lock_init(&channel->wr_spinlock);
  397. spin_lock_init(&channel->rd_spinlock);
  398. mutex_init(&channel->wr_mutex);
  399. mutex_init(&channel->rd_mutex);
  400. init_waitqueue_head(&channel->rd_wait);
  401. init_waitqueue_head(&channel->wr_wait);
  402. init_waitqueue_head(&channel->wr_ready_wait);
  403. INIT_DELAYED_WORK(&channel->rd_workitem, xillybus_autoflush);
  404. channel->endpoint = ep;
  405. channel->chan_num = i;
  406. channel->log2_element_size = 0;
  407. ep->channels[i] = channel++;
  408. }
  409. for (entry = 0; entry < entries; entry++, chandesc += 4) {
  410. struct xilly_buffer **buffers = NULL;
  411. is_writebuf = chandesc[0] & 0x01;
  412. channelnum = (chandesc[0] >> 1) | ((chandesc[1] & 0x0f) << 7);
  413. format = (chandesc[1] >> 4) & 0x03;
  414. allowpartial = (chandesc[1] >> 6) & 0x01;
  415. synchronous = (chandesc[1] >> 7) & 0x01;
  416. bufsize = 1 << (chandesc[2] & 0x1f);
  417. bufnum = 1 << (chandesc[3] & 0x0f);
  418. exclusive_open = (chandesc[2] >> 7) & 0x01;
  419. seekable = (chandesc[2] >> 6) & 0x01;
  420. supports_nonempty = (chandesc[2] >> 5) & 0x01;
  421. if ((channelnum > ep->num_channels) ||
  422. ((channelnum == 0) && !is_writebuf)) {
  423. dev_err(ep->dev,
  424. "IDT requests channel out of range. Aborting.\n");
  425. return -ENODEV;
  426. }
  427. channel = ep->channels[channelnum]; /* NULL for msg channel */
  428. if (!is_writebuf || channelnum > 0) {
  429. channel->log2_element_size = ((format > 2) ?
  430. 2 : format);
  431. bytebufsize = bufsize *
  432. (1 << channel->log2_element_size);
  433. buffers = devm_kcalloc(dev, bufnum,
  434. sizeof(struct xilly_buffer *),
  435. GFP_KERNEL);
  436. if (!buffers)
  437. return -ENOMEM;
  438. } else {
  439. bytebufsize = bufsize << 2;
  440. }
  441. if (!is_writebuf) {
  442. channel->num_rd_buffers = bufnum;
  443. channel->rd_buf_size = bytebufsize;
  444. channel->rd_allow_partial = allowpartial;
  445. channel->rd_synchronous = synchronous;
  446. channel->rd_exclusive_open = exclusive_open;
  447. channel->seekable = seekable;
  448. channel->rd_buffers = buffers;
  449. rc = xilly_get_dma_buffers(ep, &rd_alloc, buffers,
  450. bufnum, bytebufsize);
  451. } else if (channelnum > 0) {
  452. channel->num_wr_buffers = bufnum;
  453. channel->wr_buf_size = bytebufsize;
  454. channel->seekable = seekable;
  455. channel->wr_supports_nonempty = supports_nonempty;
  456. channel->wr_allow_partial = allowpartial;
  457. channel->wr_synchronous = synchronous;
  458. channel->wr_exclusive_open = exclusive_open;
  459. channel->wr_buffers = buffers;
  460. rc = xilly_get_dma_buffers(ep, &wr_alloc, buffers,
  461. bufnum, bytebufsize);
  462. } else {
  463. rc = xilly_get_dma_buffers(ep, &wr_alloc, NULL,
  464. bufnum, bytebufsize);
  465. msg_buf_done++;
  466. }
  467. if (rc)
  468. return -ENOMEM;
  469. }
  470. if (!msg_buf_done) {
  471. dev_err(ep->dev,
  472. "Corrupt IDT: No message buffer. Aborting.\n");
  473. return -ENODEV;
  474. }
  475. return 0;
  476. }
  477. static int xilly_scan_idt(struct xilly_endpoint *endpoint,
  478. struct xilly_idt_handle *idt_handle)
  479. {
  480. int count = 0;
  481. unsigned char *idt = endpoint->channels[1]->wr_buffers[0]->addr;
  482. unsigned char *end_of_idt = idt + endpoint->idtlen - 4;
  483. unsigned char *scan;
  484. int len;
  485. scan = idt;
  486. idt_handle->idt = idt;
  487. scan++; /* Skip version number */
  488. while ((scan <= end_of_idt) && *scan) {
  489. while ((scan <= end_of_idt) && *scan++)
  490. /* Do nothing, just scan thru string */;
  491. count++;
  492. }
  493. scan++;
  494. if (scan > end_of_idt) {
  495. dev_err(endpoint->dev,
  496. "IDT device name list overflow. Aborting.\n");
  497. return -ENODEV;
  498. }
  499. idt_handle->chandesc = scan;
  500. len = endpoint->idtlen - (3 + ((int) (scan - idt)));
  501. if (len & 0x03) {
  502. dev_err(endpoint->dev,
  503. "Corrupt IDT device name list. Aborting.\n");
  504. return -ENODEV;
  505. }
  506. idt_handle->entries = len >> 2;
  507. endpoint->num_channels = count;
  508. return 0;
  509. }
  510. static int xilly_obtain_idt(struct xilly_endpoint *endpoint)
  511. {
  512. struct xilly_channel *channel;
  513. unsigned char *version;
  514. long t;
  515. channel = endpoint->channels[1]; /* This should be generated ad-hoc */
  516. channel->wr_sleepy = 1;
  517. iowrite32(1 |
  518. (3 << 24), /* Opcode 3 for channel 0 = Send IDT */
  519. endpoint->registers + fpga_buf_ctrl_reg);
  520. t = wait_event_interruptible_timeout(channel->wr_wait,
  521. (!channel->wr_sleepy),
  522. XILLY_TIMEOUT);
  523. if (t <= 0) {
  524. dev_err(endpoint->dev, "Failed to obtain IDT. Aborting.\n");
  525. if (endpoint->fatal_error)
  526. return -EIO;
  527. return -ENODEV;
  528. }
  529. endpoint->ephw->hw_sync_sgl_for_cpu(
  530. channel->endpoint,
  531. channel->wr_buffers[0]->dma_addr,
  532. channel->wr_buf_size,
  533. DMA_FROM_DEVICE);
  534. if (channel->wr_buffers[0]->end_offset != endpoint->idtlen) {
  535. dev_err(endpoint->dev,
  536. "IDT length mismatch (%d != %d). Aborting.\n",
  537. channel->wr_buffers[0]->end_offset, endpoint->idtlen);
  538. return -ENODEV;
  539. }
  540. if (crc32_le(~0, channel->wr_buffers[0]->addr,
  541. endpoint->idtlen+1) != 0) {
  542. dev_err(endpoint->dev, "IDT failed CRC check. Aborting.\n");
  543. return -ENODEV;
  544. }
  545. version = channel->wr_buffers[0]->addr;
  546. /* Check version number. Reject anything above 0x82. */
  547. if (*version > 0x82) {
  548. dev_err(endpoint->dev,
  549. "No support for IDT version 0x%02x. Maybe the xillybus driver needs an upgrade. Aborting.\n",
  550. *version);
  551. return -ENODEV;
  552. }
  553. return 0;
  554. }
  555. static ssize_t xillybus_read(struct file *filp, char __user *userbuf,
  556. size_t count, loff_t *f_pos)
  557. {
  558. ssize_t rc;
  559. unsigned long flags;
  560. int bytes_done = 0;
  561. int no_time_left = 0;
  562. long deadline, left_to_sleep;
  563. struct xilly_channel *channel = filp->private_data;
  564. int empty, reached_eof, exhausted, ready;
  565. /* Initializations are there only to silence warnings */
  566. int howmany = 0, bufpos = 0, bufidx = 0, bufferdone = 0;
  567. int waiting_bufidx;
  568. if (channel->endpoint->fatal_error)
  569. return -EIO;
  570. deadline = jiffies + 1 + XILLY_RX_TIMEOUT;
  571. rc = mutex_lock_interruptible(&channel->wr_mutex);
  572. if (rc)
  573. return rc;
  574. while (1) { /* Note that we may drop mutex within this loop */
  575. int bytes_to_do = count - bytes_done;
  576. spin_lock_irqsave(&channel->wr_spinlock, flags);
  577. empty = channel->wr_empty;
  578. ready = !empty || channel->wr_ready;
  579. if (!empty) {
  580. bufidx = channel->wr_host_buf_idx;
  581. bufpos = channel->wr_host_buf_pos;
  582. howmany = ((channel->wr_buffers[bufidx]->end_offset
  583. + 1) << channel->log2_element_size)
  584. - bufpos;
  585. /* Update wr_host_* to its post-operation state */
  586. if (howmany > bytes_to_do) {
  587. bufferdone = 0;
  588. howmany = bytes_to_do;
  589. channel->wr_host_buf_pos += howmany;
  590. } else {
  591. bufferdone = 1;
  592. channel->wr_host_buf_pos = 0;
  593. if (bufidx == channel->wr_fpga_buf_idx) {
  594. channel->wr_empty = 1;
  595. channel->wr_sleepy = 1;
  596. channel->wr_ready = 0;
  597. }
  598. if (bufidx >= (channel->num_wr_buffers - 1))
  599. channel->wr_host_buf_idx = 0;
  600. else
  601. channel->wr_host_buf_idx++;
  602. }
  603. }
  604. /*
  605. * Marking our situation after the possible changes above,
  606. * for use after releasing the spinlock.
  607. *
  608. * empty = empty before change
  609. * exhasted = empty after possible change
  610. */
  611. reached_eof = channel->wr_empty &&
  612. (channel->wr_host_buf_idx == channel->wr_eof);
  613. channel->wr_hangup = reached_eof;
  614. exhausted = channel->wr_empty;
  615. waiting_bufidx = channel->wr_host_buf_idx;
  616. spin_unlock_irqrestore(&channel->wr_spinlock, flags);
  617. if (!empty) { /* Go on, now without the spinlock */
  618. if (bufpos == 0) /* Position zero means it's virgin */
  619. channel->endpoint->ephw->hw_sync_sgl_for_cpu(
  620. channel->endpoint,
  621. channel->wr_buffers[bufidx]->dma_addr,
  622. channel->wr_buf_size,
  623. DMA_FROM_DEVICE);
  624. if (copy_to_user(
  625. userbuf,
  626. channel->wr_buffers[bufidx]->addr
  627. + bufpos, howmany))
  628. rc = -EFAULT;
  629. userbuf += howmany;
  630. bytes_done += howmany;
  631. if (bufferdone) {
  632. channel->endpoint->ephw->hw_sync_sgl_for_device(
  633. channel->endpoint,
  634. channel->wr_buffers[bufidx]->dma_addr,
  635. channel->wr_buf_size,
  636. DMA_FROM_DEVICE);
  637. /*
  638. * Tell FPGA the buffer is done with. It's an
  639. * atomic operation to the FPGA, so what
  640. * happens with other channels doesn't matter,
  641. * and the certain channel is protected with
  642. * the channel-specific mutex.
  643. */
  644. iowrite32(1 | (channel->chan_num << 1) |
  645. (bufidx << 12),
  646. channel->endpoint->registers +
  647. fpga_buf_ctrl_reg);
  648. }
  649. if (rc) {
  650. mutex_unlock(&channel->wr_mutex);
  651. return rc;
  652. }
  653. }
  654. /* This includes a zero-count return = EOF */
  655. if ((bytes_done >= count) || reached_eof)
  656. break;
  657. if (!exhausted)
  658. continue; /* More in RAM buffer(s)? Just go on. */
  659. if ((bytes_done > 0) &&
  660. (no_time_left ||
  661. (channel->wr_synchronous && channel->wr_allow_partial)))
  662. break;
  663. /*
  664. * Nonblocking read: The "ready" flag tells us that the FPGA
  665. * has data to send. In non-blocking mode, if it isn't on,
  666. * just return. But if there is, we jump directly to the point
  667. * where we ask for the FPGA to send all it has, and wait
  668. * until that data arrives. So in a sense, we *do* block in
  669. * nonblocking mode, but only for a very short time.
  670. */
  671. if (!no_time_left && (filp->f_flags & O_NONBLOCK)) {
  672. if (bytes_done > 0)
  673. break;
  674. if (ready)
  675. goto desperate;
  676. rc = -EAGAIN;
  677. break;
  678. }
  679. if (!no_time_left || (bytes_done > 0)) {
  680. /*
  681. * Note that in case of an element-misaligned read
  682. * request, offsetlimit will include the last element,
  683. * which will be partially read from.
  684. */
  685. int offsetlimit = ((count - bytes_done) - 1) >>
  686. channel->log2_element_size;
  687. int buf_elements = channel->wr_buf_size >>
  688. channel->log2_element_size;
  689. /*
  690. * In synchronous mode, always send an offset limit.
  691. * Just don't send a value too big.
  692. */
  693. if (channel->wr_synchronous) {
  694. /* Don't request more than one buffer */
  695. if (channel->wr_allow_partial &&
  696. (offsetlimit >= buf_elements))
  697. offsetlimit = buf_elements - 1;
  698. /* Don't request more than all buffers */
  699. if (!channel->wr_allow_partial &&
  700. (offsetlimit >=
  701. (buf_elements * channel->num_wr_buffers)))
  702. offsetlimit = buf_elements *
  703. channel->num_wr_buffers - 1;
  704. }
  705. /*
  706. * In asynchronous mode, force early flush of a buffer
  707. * only if that will allow returning a full count. The
  708. * "offsetlimit < ( ... )" rather than "<=" excludes
  709. * requesting a full buffer, which would obviously
  710. * cause a buffer transmission anyhow
  711. */
  712. if (channel->wr_synchronous ||
  713. (offsetlimit < (buf_elements - 1))) {
  714. mutex_lock(&channel->endpoint->register_mutex);
  715. iowrite32(offsetlimit,
  716. channel->endpoint->registers +
  717. fpga_buf_offset_reg);
  718. iowrite32(1 | (channel->chan_num << 1) |
  719. (2 << 24) | /* 2 = offset limit */
  720. (waiting_bufidx << 12),
  721. channel->endpoint->registers +
  722. fpga_buf_ctrl_reg);
  723. mutex_unlock(&channel->endpoint->
  724. register_mutex);
  725. }
  726. }
  727. /*
  728. * If partial completion is disallowed, there is no point in
  729. * timeout sleeping. Neither if no_time_left is set and
  730. * there's no data.
  731. */
  732. if (!channel->wr_allow_partial ||
  733. (no_time_left && (bytes_done == 0))) {
  734. /*
  735. * This do-loop will run more than once if another
  736. * thread reasserted wr_sleepy before we got the mutex
  737. * back, so we try again.
  738. */
  739. do {
  740. mutex_unlock(&channel->wr_mutex);
  741. if (wait_event_interruptible(
  742. channel->wr_wait,
  743. (!channel->wr_sleepy)))
  744. goto interrupted;
  745. if (mutex_lock_interruptible(
  746. &channel->wr_mutex))
  747. goto interrupted;
  748. } while (channel->wr_sleepy);
  749. continue;
  750. interrupted: /* Mutex is not held if got here */
  751. if (channel->endpoint->fatal_error)
  752. return -EIO;
  753. if (bytes_done)
  754. return bytes_done;
  755. if (filp->f_flags & O_NONBLOCK)
  756. return -EAGAIN; /* Don't admit snoozing */
  757. return -EINTR;
  758. }
  759. left_to_sleep = deadline - ((long) jiffies);
  760. /*
  761. * If our time is out, skip the waiting. We may miss wr_sleepy
  762. * being deasserted but hey, almost missing the train is like
  763. * missing it.
  764. */
  765. if (left_to_sleep > 0) {
  766. left_to_sleep =
  767. wait_event_interruptible_timeout(
  768. channel->wr_wait,
  769. (!channel->wr_sleepy),
  770. left_to_sleep);
  771. if (left_to_sleep > 0) /* wr_sleepy deasserted */
  772. continue;
  773. if (left_to_sleep < 0) { /* Interrupt */
  774. mutex_unlock(&channel->wr_mutex);
  775. if (channel->endpoint->fatal_error)
  776. return -EIO;
  777. if (bytes_done)
  778. return bytes_done;
  779. return -EINTR;
  780. }
  781. }
  782. desperate:
  783. no_time_left = 1; /* We're out of sleeping time. Desperate! */
  784. if (bytes_done == 0) {
  785. /*
  786. * Reaching here means that we allow partial return,
  787. * that we've run out of time, and that we have
  788. * nothing to return.
  789. * So tell the FPGA to send anything it has or gets.
  790. */
  791. iowrite32(1 | (channel->chan_num << 1) |
  792. (3 << 24) | /* Opcode 3, flush it all! */
  793. (waiting_bufidx << 12),
  794. channel->endpoint->registers +
  795. fpga_buf_ctrl_reg);
  796. }
  797. /*
  798. * Reaching here means that we *do* have data in the buffer,
  799. * but the "partial" flag disallows returning less than
  800. * required. And we don't have as much. So loop again,
  801. * which is likely to end up blocking indefinitely until
  802. * enough data has arrived.
  803. */
  804. }
  805. mutex_unlock(&channel->wr_mutex);
  806. if (channel->endpoint->fatal_error)
  807. return -EIO;
  808. if (rc)
  809. return rc;
  810. return bytes_done;
  811. }
  812. /*
  813. * The timeout argument takes values as follows:
  814. * >0 : Flush with timeout
  815. * ==0 : Flush, and wait idefinitely for the flush to complete
  816. * <0 : Autoflush: Flush only if there's a single buffer occupied
  817. */
  818. static int xillybus_myflush(struct xilly_channel *channel, long timeout)
  819. {
  820. int rc;
  821. unsigned long flags;
  822. int end_offset_plus1;
  823. int bufidx, bufidx_minus1;
  824. int i;
  825. int empty;
  826. int new_rd_host_buf_pos;
  827. if (channel->endpoint->fatal_error)
  828. return -EIO;
  829. rc = mutex_lock_interruptible(&channel->rd_mutex);
  830. if (rc)
  831. return rc;
  832. /*
  833. * Don't flush a closed channel. This can happen when the work queued
  834. * autoflush thread fires off after the file has closed. This is not
  835. * an error, just something to dismiss.
  836. */
  837. if (!channel->rd_ref_count)
  838. goto done;
  839. bufidx = channel->rd_host_buf_idx;
  840. bufidx_minus1 = (bufidx == 0) ?
  841. channel->num_rd_buffers - 1 :
  842. bufidx - 1;
  843. end_offset_plus1 = channel->rd_host_buf_pos >>
  844. channel->log2_element_size;
  845. new_rd_host_buf_pos = channel->rd_host_buf_pos -
  846. (end_offset_plus1 << channel->log2_element_size);
  847. /* Submit the current buffer if it's nonempty */
  848. if (end_offset_plus1) {
  849. unsigned char *tail = channel->rd_buffers[bufidx]->addr +
  850. (end_offset_plus1 << channel->log2_element_size);
  851. /* Copy unflushed data, so we can put it in next buffer */
  852. for (i = 0; i < new_rd_host_buf_pos; i++)
  853. channel->rd_leftovers[i] = *tail++;
  854. spin_lock_irqsave(&channel->rd_spinlock, flags);
  855. /* Autoflush only if a single buffer is occupied */
  856. if ((timeout < 0) &&
  857. (channel->rd_full ||
  858. (bufidx_minus1 != channel->rd_fpga_buf_idx))) {
  859. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  860. /*
  861. * A new work item may be queued by the ISR exactly
  862. * now, since the execution of a work item allows the
  863. * queuing of a new one while it's running.
  864. */
  865. goto done;
  866. }
  867. /* The 4th element is never needed for data, so it's a flag */
  868. channel->rd_leftovers[3] = (new_rd_host_buf_pos != 0);
  869. /* Set up rd_full to reflect a certain moment's state */
  870. if (bufidx == channel->rd_fpga_buf_idx)
  871. channel->rd_full = 1;
  872. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  873. if (bufidx >= (channel->num_rd_buffers - 1))
  874. channel->rd_host_buf_idx = 0;
  875. else
  876. channel->rd_host_buf_idx++;
  877. channel->endpoint->ephw->hw_sync_sgl_for_device(
  878. channel->endpoint,
  879. channel->rd_buffers[bufidx]->dma_addr,
  880. channel->rd_buf_size,
  881. DMA_TO_DEVICE);
  882. mutex_lock(&channel->endpoint->register_mutex);
  883. iowrite32(end_offset_plus1 - 1,
  884. channel->endpoint->registers + fpga_buf_offset_reg);
  885. iowrite32((channel->chan_num << 1) | /* Channel ID */
  886. (2 << 24) | /* Opcode 2, submit buffer */
  887. (bufidx << 12),
  888. channel->endpoint->registers + fpga_buf_ctrl_reg);
  889. mutex_unlock(&channel->endpoint->register_mutex);
  890. } else if (bufidx == 0) {
  891. bufidx = channel->num_rd_buffers - 1;
  892. } else {
  893. bufidx--;
  894. }
  895. channel->rd_host_buf_pos = new_rd_host_buf_pos;
  896. if (timeout < 0)
  897. goto done; /* Autoflush */
  898. /*
  899. * bufidx is now the last buffer written to (or equal to
  900. * rd_fpga_buf_idx if buffer was never written to), and
  901. * channel->rd_host_buf_idx the one after it.
  902. *
  903. * If bufidx == channel->rd_fpga_buf_idx we're either empty or full.
  904. */
  905. while (1) { /* Loop waiting for draining of buffers */
  906. spin_lock_irqsave(&channel->rd_spinlock, flags);
  907. if (bufidx != channel->rd_fpga_buf_idx)
  908. channel->rd_full = 1; /*
  909. * Not really full,
  910. * but needs waiting.
  911. */
  912. empty = !channel->rd_full;
  913. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  914. if (empty)
  915. break;
  916. /*
  917. * Indefinite sleep with mutex taken. With data waiting for
  918. * flushing user should not be surprised if open() for write
  919. * sleeps.
  920. */
  921. if (timeout == 0)
  922. wait_event_interruptible(channel->rd_wait,
  923. (!channel->rd_full));
  924. else if (wait_event_interruptible_timeout(
  925. channel->rd_wait,
  926. (!channel->rd_full),
  927. timeout) == 0) {
  928. dev_warn(channel->endpoint->dev,
  929. "Timed out while flushing. Output data may be lost.\n");
  930. rc = -ETIMEDOUT;
  931. break;
  932. }
  933. if (channel->rd_full) {
  934. rc = -EINTR;
  935. break;
  936. }
  937. }
  938. done:
  939. mutex_unlock(&channel->rd_mutex);
  940. if (channel->endpoint->fatal_error)
  941. return -EIO;
  942. return rc;
  943. }
  944. static int xillybus_flush(struct file *filp, fl_owner_t id)
  945. {
  946. if (!(filp->f_mode & FMODE_WRITE))
  947. return 0;
  948. return xillybus_myflush(filp->private_data, HZ); /* 1 second timeout */
  949. }
  950. static void xillybus_autoflush(struct work_struct *work)
  951. {
  952. struct delayed_work *workitem = container_of(
  953. work, struct delayed_work, work);
  954. struct xilly_channel *channel = container_of(
  955. workitem, struct xilly_channel, rd_workitem);
  956. int rc;
  957. rc = xillybus_myflush(channel, -1);
  958. if (rc == -EINTR)
  959. dev_warn(channel->endpoint->dev,
  960. "Autoflush failed because work queue thread got a signal.\n");
  961. else if (rc)
  962. dev_err(channel->endpoint->dev,
  963. "Autoflush failed under weird circumstances.\n");
  964. }
  965. static ssize_t xillybus_write(struct file *filp, const char __user *userbuf,
  966. size_t count, loff_t *f_pos)
  967. {
  968. ssize_t rc;
  969. unsigned long flags;
  970. int bytes_done = 0;
  971. struct xilly_channel *channel = filp->private_data;
  972. int full, exhausted;
  973. /* Initializations are there only to silence warnings */
  974. int howmany = 0, bufpos = 0, bufidx = 0, bufferdone = 0;
  975. int end_offset_plus1 = 0;
  976. if (channel->endpoint->fatal_error)
  977. return -EIO;
  978. rc = mutex_lock_interruptible(&channel->rd_mutex);
  979. if (rc)
  980. return rc;
  981. while (1) {
  982. int bytes_to_do = count - bytes_done;
  983. spin_lock_irqsave(&channel->rd_spinlock, flags);
  984. full = channel->rd_full;
  985. if (!full) {
  986. bufidx = channel->rd_host_buf_idx;
  987. bufpos = channel->rd_host_buf_pos;
  988. howmany = channel->rd_buf_size - bufpos;
  989. /*
  990. * Update rd_host_* to its state after this operation.
  991. * count=0 means committing the buffer immediately,
  992. * which is like flushing, but not necessarily block.
  993. */
  994. if ((howmany > bytes_to_do) &&
  995. (count ||
  996. ((bufpos >> channel->log2_element_size) == 0))) {
  997. bufferdone = 0;
  998. howmany = bytes_to_do;
  999. channel->rd_host_buf_pos += howmany;
  1000. } else {
  1001. bufferdone = 1;
  1002. if (count) {
  1003. end_offset_plus1 =
  1004. channel->rd_buf_size >>
  1005. channel->log2_element_size;
  1006. channel->rd_host_buf_pos = 0;
  1007. } else {
  1008. unsigned char *tail;
  1009. int i;
  1010. howmany = 0;
  1011. end_offset_plus1 = bufpos >>
  1012. channel->log2_element_size;
  1013. channel->rd_host_buf_pos -=
  1014. end_offset_plus1 <<
  1015. channel->log2_element_size;
  1016. tail = channel->
  1017. rd_buffers[bufidx]->addr +
  1018. (end_offset_plus1 <<
  1019. channel->log2_element_size);
  1020. for (i = 0;
  1021. i < channel->rd_host_buf_pos;
  1022. i++)
  1023. channel->rd_leftovers[i] =
  1024. *tail++;
  1025. }
  1026. if (bufidx == channel->rd_fpga_buf_idx)
  1027. channel->rd_full = 1;
  1028. if (bufidx >= (channel->num_rd_buffers - 1))
  1029. channel->rd_host_buf_idx = 0;
  1030. else
  1031. channel->rd_host_buf_idx++;
  1032. }
  1033. }
  1034. /*
  1035. * Marking our situation after the possible changes above,
  1036. * for use after releasing the spinlock.
  1037. *
  1038. * full = full before change
  1039. * exhasted = full after possible change
  1040. */
  1041. exhausted = channel->rd_full;
  1042. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  1043. if (!full) { /* Go on, now without the spinlock */
  1044. unsigned char *head =
  1045. channel->rd_buffers[bufidx]->addr;
  1046. int i;
  1047. if ((bufpos == 0) || /* Zero means it's virgin */
  1048. (channel->rd_leftovers[3] != 0)) {
  1049. channel->endpoint->ephw->hw_sync_sgl_for_cpu(
  1050. channel->endpoint,
  1051. channel->rd_buffers[bufidx]->dma_addr,
  1052. channel->rd_buf_size,
  1053. DMA_TO_DEVICE);
  1054. /* Virgin, but leftovers are due */
  1055. for (i = 0; i < bufpos; i++)
  1056. *head++ = channel->rd_leftovers[i];
  1057. channel->rd_leftovers[3] = 0; /* Clear flag */
  1058. }
  1059. if (copy_from_user(
  1060. channel->rd_buffers[bufidx]->addr + bufpos,
  1061. userbuf, howmany))
  1062. rc = -EFAULT;
  1063. userbuf += howmany;
  1064. bytes_done += howmany;
  1065. if (bufferdone) {
  1066. channel->endpoint->ephw->hw_sync_sgl_for_device(
  1067. channel->endpoint,
  1068. channel->rd_buffers[bufidx]->dma_addr,
  1069. channel->rd_buf_size,
  1070. DMA_TO_DEVICE);
  1071. mutex_lock(&channel->endpoint->register_mutex);
  1072. iowrite32(end_offset_plus1 - 1,
  1073. channel->endpoint->registers +
  1074. fpga_buf_offset_reg);
  1075. iowrite32((channel->chan_num << 1) |
  1076. (2 << 24) | /* 2 = submit buffer */
  1077. (bufidx << 12),
  1078. channel->endpoint->registers +
  1079. fpga_buf_ctrl_reg);
  1080. mutex_unlock(&channel->endpoint->
  1081. register_mutex);
  1082. channel->rd_leftovers[3] =
  1083. (channel->rd_host_buf_pos != 0);
  1084. }
  1085. if (rc) {
  1086. mutex_unlock(&channel->rd_mutex);
  1087. if (channel->endpoint->fatal_error)
  1088. return -EIO;
  1089. if (!channel->rd_synchronous)
  1090. queue_delayed_work(
  1091. xillybus_wq,
  1092. &channel->rd_workitem,
  1093. XILLY_RX_TIMEOUT);
  1094. return rc;
  1095. }
  1096. }
  1097. if (bytes_done >= count)
  1098. break;
  1099. if (!exhausted)
  1100. continue; /* If there's more space, just go on */
  1101. if ((bytes_done > 0) && channel->rd_allow_partial)
  1102. break;
  1103. /*
  1104. * Indefinite sleep with mutex taken. With data waiting for
  1105. * flushing, user should not be surprised if open() for write
  1106. * sleeps.
  1107. */
  1108. if (filp->f_flags & O_NONBLOCK) {
  1109. rc = -EAGAIN;
  1110. break;
  1111. }
  1112. if (wait_event_interruptible(channel->rd_wait,
  1113. (!channel->rd_full))) {
  1114. mutex_unlock(&channel->rd_mutex);
  1115. if (channel->endpoint->fatal_error)
  1116. return -EIO;
  1117. if (bytes_done)
  1118. return bytes_done;
  1119. return -EINTR;
  1120. }
  1121. }
  1122. mutex_unlock(&channel->rd_mutex);
  1123. if (!channel->rd_synchronous)
  1124. queue_delayed_work(xillybus_wq,
  1125. &channel->rd_workitem,
  1126. XILLY_RX_TIMEOUT);
  1127. if (channel->endpoint->fatal_error)
  1128. return -EIO;
  1129. if (rc)
  1130. return rc;
  1131. if ((channel->rd_synchronous) && (bytes_done > 0)) {
  1132. rc = xillybus_myflush(filp->private_data, 0); /* No timeout */
  1133. if (rc && (rc != -EINTR))
  1134. return rc;
  1135. }
  1136. return bytes_done;
  1137. }
  1138. static int xillybus_open(struct inode *inode, struct file *filp)
  1139. {
  1140. int rc = 0;
  1141. unsigned long flags;
  1142. int minor = iminor(inode);
  1143. int major = imajor(inode);
  1144. struct xilly_endpoint *ep_iter, *endpoint = NULL;
  1145. struct xilly_channel *channel;
  1146. mutex_lock(&ep_list_lock);
  1147. list_for_each_entry(ep_iter, &list_of_endpoints, ep_list) {
  1148. if ((ep_iter->major == major) &&
  1149. (minor >= ep_iter->lowest_minor) &&
  1150. (minor < (ep_iter->lowest_minor +
  1151. ep_iter->num_channels))) {
  1152. endpoint = ep_iter;
  1153. break;
  1154. }
  1155. }
  1156. mutex_unlock(&ep_list_lock);
  1157. if (!endpoint) {
  1158. pr_err("xillybus: open() failed to find a device for major=%d and minor=%d\n",
  1159. major, minor);
  1160. return -ENODEV;
  1161. }
  1162. if (endpoint->fatal_error)
  1163. return -EIO;
  1164. channel = endpoint->channels[1 + minor - endpoint->lowest_minor];
  1165. filp->private_data = channel;
  1166. /*
  1167. * It gets complicated because:
  1168. * 1. We don't want to take a mutex we don't have to
  1169. * 2. We don't want to open one direction if the other will fail.
  1170. */
  1171. if ((filp->f_mode & FMODE_READ) && (!channel->num_wr_buffers))
  1172. return -ENODEV;
  1173. if ((filp->f_mode & FMODE_WRITE) && (!channel->num_rd_buffers))
  1174. return -ENODEV;
  1175. if ((filp->f_mode & FMODE_READ) && (filp->f_flags & O_NONBLOCK) &&
  1176. (channel->wr_synchronous || !channel->wr_allow_partial ||
  1177. !channel->wr_supports_nonempty)) {
  1178. dev_err(endpoint->dev,
  1179. "open() failed: O_NONBLOCK not allowed for read on this device\n");
  1180. return -ENODEV;
  1181. }
  1182. if ((filp->f_mode & FMODE_WRITE) && (filp->f_flags & O_NONBLOCK) &&
  1183. (channel->rd_synchronous || !channel->rd_allow_partial)) {
  1184. dev_err(endpoint->dev,
  1185. "open() failed: O_NONBLOCK not allowed for write on this device\n");
  1186. return -ENODEV;
  1187. }
  1188. /*
  1189. * Note: open() may block on getting mutexes despite O_NONBLOCK.
  1190. * This shouldn't occur normally, since multiple open of the same
  1191. * file descriptor is almost always prohibited anyhow
  1192. * (*_exclusive_open is normally set in real-life systems).
  1193. */
  1194. if (filp->f_mode & FMODE_READ) {
  1195. rc = mutex_lock_interruptible(&channel->wr_mutex);
  1196. if (rc)
  1197. return rc;
  1198. }
  1199. if (filp->f_mode & FMODE_WRITE) {
  1200. rc = mutex_lock_interruptible(&channel->rd_mutex);
  1201. if (rc)
  1202. goto unlock_wr;
  1203. }
  1204. if ((filp->f_mode & FMODE_READ) &&
  1205. (channel->wr_ref_count != 0) &&
  1206. (channel->wr_exclusive_open)) {
  1207. rc = -EBUSY;
  1208. goto unlock;
  1209. }
  1210. if ((filp->f_mode & FMODE_WRITE) &&
  1211. (channel->rd_ref_count != 0) &&
  1212. (channel->rd_exclusive_open)) {
  1213. rc = -EBUSY;
  1214. goto unlock;
  1215. }
  1216. if (filp->f_mode & FMODE_READ) {
  1217. if (channel->wr_ref_count == 0) { /* First open of file */
  1218. /* Move the host to first buffer */
  1219. spin_lock_irqsave(&channel->wr_spinlock, flags);
  1220. channel->wr_host_buf_idx = 0;
  1221. channel->wr_host_buf_pos = 0;
  1222. channel->wr_fpga_buf_idx = -1;
  1223. channel->wr_empty = 1;
  1224. channel->wr_ready = 0;
  1225. channel->wr_sleepy = 1;
  1226. channel->wr_eof = -1;
  1227. channel->wr_hangup = 0;
  1228. spin_unlock_irqrestore(&channel->wr_spinlock, flags);
  1229. iowrite32(1 | (channel->chan_num << 1) |
  1230. (4 << 24) | /* Opcode 4, open channel */
  1231. ((channel->wr_synchronous & 1) << 23),
  1232. channel->endpoint->registers +
  1233. fpga_buf_ctrl_reg);
  1234. }
  1235. channel->wr_ref_count++;
  1236. }
  1237. if (filp->f_mode & FMODE_WRITE) {
  1238. if (channel->rd_ref_count == 0) { /* First open of file */
  1239. /* Move the host to first buffer */
  1240. spin_lock_irqsave(&channel->rd_spinlock, flags);
  1241. channel->rd_host_buf_idx = 0;
  1242. channel->rd_host_buf_pos = 0;
  1243. channel->rd_leftovers[3] = 0; /* No leftovers. */
  1244. channel->rd_fpga_buf_idx = channel->num_rd_buffers - 1;
  1245. channel->rd_full = 0;
  1246. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  1247. iowrite32((channel->chan_num << 1) |
  1248. (4 << 24), /* Opcode 4, open channel */
  1249. channel->endpoint->registers +
  1250. fpga_buf_ctrl_reg);
  1251. }
  1252. channel->rd_ref_count++;
  1253. }
  1254. unlock:
  1255. if (filp->f_mode & FMODE_WRITE)
  1256. mutex_unlock(&channel->rd_mutex);
  1257. unlock_wr:
  1258. if (filp->f_mode & FMODE_READ)
  1259. mutex_unlock(&channel->wr_mutex);
  1260. if (!rc && (!channel->seekable))
  1261. return nonseekable_open(inode, filp);
  1262. return rc;
  1263. }
  1264. static int xillybus_release(struct inode *inode, struct file *filp)
  1265. {
  1266. unsigned long flags;
  1267. struct xilly_channel *channel = filp->private_data;
  1268. int buf_idx;
  1269. int eof;
  1270. if (channel->endpoint->fatal_error)
  1271. return -EIO;
  1272. if (filp->f_mode & FMODE_WRITE) {
  1273. mutex_lock(&channel->rd_mutex);
  1274. channel->rd_ref_count--;
  1275. if (channel->rd_ref_count == 0) {
  1276. /*
  1277. * We rely on the kernel calling flush()
  1278. * before we get here.
  1279. */
  1280. iowrite32((channel->chan_num << 1) | /* Channel ID */
  1281. (5 << 24), /* Opcode 5, close channel */
  1282. channel->endpoint->registers +
  1283. fpga_buf_ctrl_reg);
  1284. }
  1285. mutex_unlock(&channel->rd_mutex);
  1286. }
  1287. if (filp->f_mode & FMODE_READ) {
  1288. mutex_lock(&channel->wr_mutex);
  1289. channel->wr_ref_count--;
  1290. if (channel->wr_ref_count == 0) {
  1291. iowrite32(1 | (channel->chan_num << 1) |
  1292. (5 << 24), /* Opcode 5, close channel */
  1293. channel->endpoint->registers +
  1294. fpga_buf_ctrl_reg);
  1295. /*
  1296. * This is crazily cautious: We make sure that not
  1297. * only that we got an EOF (be it because we closed
  1298. * the channel or because of a user's EOF), but verify
  1299. * that it's one beyond the last buffer arrived, so
  1300. * we have no leftover buffers pending before wrapping
  1301. * up (which can only happen in asynchronous channels,
  1302. * BTW)
  1303. */
  1304. while (1) {
  1305. spin_lock_irqsave(&channel->wr_spinlock,
  1306. flags);
  1307. buf_idx = channel->wr_fpga_buf_idx;
  1308. eof = channel->wr_eof;
  1309. channel->wr_sleepy = 1;
  1310. spin_unlock_irqrestore(&channel->wr_spinlock,
  1311. flags);
  1312. /*
  1313. * Check if eof points at the buffer after
  1314. * the last one the FPGA submitted. Note that
  1315. * no EOF is marked by negative eof.
  1316. */
  1317. buf_idx++;
  1318. if (buf_idx == channel->num_wr_buffers)
  1319. buf_idx = 0;
  1320. if (buf_idx == eof)
  1321. break;
  1322. /*
  1323. * Steal extra 100 ms if awaken by interrupt.
  1324. * This is a simple workaround for an
  1325. * interrupt pending when entering, which would
  1326. * otherwise result in declaring the hardware
  1327. * non-responsive.
  1328. */
  1329. if (wait_event_interruptible(
  1330. channel->wr_wait,
  1331. (!channel->wr_sleepy)))
  1332. msleep(100);
  1333. if (channel->wr_sleepy) {
  1334. mutex_unlock(&channel->wr_mutex);
  1335. dev_warn(channel->endpoint->dev,
  1336. "Hardware failed to respond to close command, therefore left in messy state.\n");
  1337. return -EINTR;
  1338. }
  1339. }
  1340. }
  1341. mutex_unlock(&channel->wr_mutex);
  1342. }
  1343. return 0;
  1344. }
  1345. static loff_t xillybus_llseek(struct file *filp, loff_t offset, int whence)
  1346. {
  1347. struct xilly_channel *channel = filp->private_data;
  1348. loff_t pos = filp->f_pos;
  1349. int rc = 0;
  1350. /*
  1351. * Take both mutexes not allowing interrupts, since it seems like
  1352. * common applications don't expect an -EINTR here. Besides, multiple
  1353. * access to a single file descriptor on seekable devices is a mess
  1354. * anyhow.
  1355. */
  1356. if (channel->endpoint->fatal_error)
  1357. return -EIO;
  1358. mutex_lock(&channel->wr_mutex);
  1359. mutex_lock(&channel->rd_mutex);
  1360. switch (whence) {
  1361. case SEEK_SET:
  1362. pos = offset;
  1363. break;
  1364. case SEEK_CUR:
  1365. pos += offset;
  1366. break;
  1367. case SEEK_END:
  1368. pos = offset; /* Going to the end => to the beginning */
  1369. break;
  1370. default:
  1371. rc = -EINVAL;
  1372. goto end;
  1373. }
  1374. /* In any case, we must finish on an element boundary */
  1375. if (pos & ((1 << channel->log2_element_size) - 1)) {
  1376. rc = -EINVAL;
  1377. goto end;
  1378. }
  1379. mutex_lock(&channel->endpoint->register_mutex);
  1380. iowrite32(pos >> channel->log2_element_size,
  1381. channel->endpoint->registers + fpga_buf_offset_reg);
  1382. iowrite32((channel->chan_num << 1) |
  1383. (6 << 24), /* Opcode 6, set address */
  1384. channel->endpoint->registers + fpga_buf_ctrl_reg);
  1385. mutex_unlock(&channel->endpoint->register_mutex);
  1386. end:
  1387. mutex_unlock(&channel->rd_mutex);
  1388. mutex_unlock(&channel->wr_mutex);
  1389. if (rc) /* Return error after releasing mutexes */
  1390. return rc;
  1391. filp->f_pos = pos;
  1392. /*
  1393. * Since seekable devices are allowed only when the channel is
  1394. * synchronous, we assume that there is no data pending in either
  1395. * direction (which holds true as long as no concurrent access on the
  1396. * file descriptor takes place).
  1397. * The only thing we may need to throw away is leftovers from partial
  1398. * write() flush.
  1399. */
  1400. channel->rd_leftovers[3] = 0;
  1401. return pos;
  1402. }
  1403. static unsigned int xillybus_poll(struct file *filp, poll_table *wait)
  1404. {
  1405. struct xilly_channel *channel = filp->private_data;
  1406. unsigned int mask = 0;
  1407. unsigned long flags;
  1408. poll_wait(filp, &channel->endpoint->ep_wait, wait);
  1409. /*
  1410. * poll() won't play ball regarding read() channels which
  1411. * aren't asynchronous and support the nonempty message. Allowing
  1412. * that will create situations where data has been delivered at
  1413. * the FPGA, and users expecting select() to wake up, which it may
  1414. * not.
  1415. */
  1416. if (!channel->wr_synchronous && channel->wr_supports_nonempty) {
  1417. poll_wait(filp, &channel->wr_wait, wait);
  1418. poll_wait(filp, &channel->wr_ready_wait, wait);
  1419. spin_lock_irqsave(&channel->wr_spinlock, flags);
  1420. if (!channel->wr_empty || channel->wr_ready)
  1421. mask |= POLLIN | POLLRDNORM;
  1422. if (channel->wr_hangup)
  1423. /*
  1424. * Not POLLHUP, because its behavior is in the
  1425. * mist, and POLLIN does what we want: Wake up
  1426. * the read file descriptor so it sees EOF.
  1427. */
  1428. mask |= POLLIN | POLLRDNORM;
  1429. spin_unlock_irqrestore(&channel->wr_spinlock, flags);
  1430. }
  1431. /*
  1432. * If partial data write is disallowed on a write() channel,
  1433. * it's pointless to ever signal OK to write, because is could
  1434. * block despite some space being available.
  1435. */
  1436. if (channel->rd_allow_partial) {
  1437. poll_wait(filp, &channel->rd_wait, wait);
  1438. spin_lock_irqsave(&channel->rd_spinlock, flags);
  1439. if (!channel->rd_full)
  1440. mask |= POLLOUT | POLLWRNORM;
  1441. spin_unlock_irqrestore(&channel->rd_spinlock, flags);
  1442. }
  1443. if (channel->endpoint->fatal_error)
  1444. mask |= POLLERR;
  1445. return mask;
  1446. }
  1447. static const struct file_operations xillybus_fops = {
  1448. .owner = THIS_MODULE,
  1449. .read = xillybus_read,
  1450. .write = xillybus_write,
  1451. .open = xillybus_open,
  1452. .flush = xillybus_flush,
  1453. .release = xillybus_release,
  1454. .llseek = xillybus_llseek,
  1455. .poll = xillybus_poll,
  1456. };
  1457. static int xillybus_init_chrdev(struct xilly_endpoint *endpoint,
  1458. const unsigned char *idt)
  1459. {
  1460. int rc;
  1461. dev_t dev;
  1462. int devnum, i, minor, major;
  1463. char devname[48];
  1464. struct device *device;
  1465. rc = alloc_chrdev_region(&dev, 0, /* minor start */
  1466. endpoint->num_channels,
  1467. xillyname);
  1468. if (rc) {
  1469. dev_warn(endpoint->dev, "Failed to obtain major/minors");
  1470. return rc;
  1471. }
  1472. endpoint->major = major = MAJOR(dev);
  1473. endpoint->lowest_minor = minor = MINOR(dev);
  1474. cdev_init(&endpoint->cdev, &xillybus_fops);
  1475. endpoint->cdev.owner = endpoint->ephw->owner;
  1476. rc = cdev_add(&endpoint->cdev, MKDEV(major, minor),
  1477. endpoint->num_channels);
  1478. if (rc) {
  1479. dev_warn(endpoint->dev, "Failed to add cdev. Aborting.\n");
  1480. goto unregister_chrdev;
  1481. }
  1482. idt++;
  1483. for (i = minor, devnum = 0;
  1484. devnum < endpoint->num_channels;
  1485. devnum++, i++) {
  1486. snprintf(devname, sizeof(devname)-1, "xillybus_%s", idt);
  1487. devname[sizeof(devname)-1] = 0; /* Should never matter */
  1488. while (*idt++)
  1489. /* Skip to next */;
  1490. device = device_create(xillybus_class,
  1491. NULL,
  1492. MKDEV(major, i),
  1493. NULL,
  1494. "%s", devname);
  1495. if (IS_ERR(device)) {
  1496. dev_warn(endpoint->dev,
  1497. "Failed to create %s device. Aborting.\n",
  1498. devname);
  1499. rc = -ENODEV;
  1500. goto unroll_device_create;
  1501. }
  1502. }
  1503. dev_info(endpoint->dev, "Created %d device files.\n",
  1504. endpoint->num_channels);
  1505. return 0; /* succeed */
  1506. unroll_device_create:
  1507. devnum--; i--;
  1508. for (; devnum >= 0; devnum--, i--)
  1509. device_destroy(xillybus_class, MKDEV(major, i));
  1510. cdev_del(&endpoint->cdev);
  1511. unregister_chrdev:
  1512. unregister_chrdev_region(MKDEV(major, minor), endpoint->num_channels);
  1513. return rc;
  1514. }
  1515. static void xillybus_cleanup_chrdev(struct xilly_endpoint *endpoint)
  1516. {
  1517. int minor;
  1518. for (minor = endpoint->lowest_minor;
  1519. minor < (endpoint->lowest_minor + endpoint->num_channels);
  1520. minor++)
  1521. device_destroy(xillybus_class, MKDEV(endpoint->major, minor));
  1522. cdev_del(&endpoint->cdev);
  1523. unregister_chrdev_region(MKDEV(endpoint->major,
  1524. endpoint->lowest_minor),
  1525. endpoint->num_channels);
  1526. dev_info(endpoint->dev, "Removed %d device files.\n",
  1527. endpoint->num_channels);
  1528. }
  1529. struct xilly_endpoint *xillybus_init_endpoint(struct pci_dev *pdev,
  1530. struct device *dev,
  1531. struct xilly_endpoint_hardware
  1532. *ephw)
  1533. {
  1534. struct xilly_endpoint *endpoint;
  1535. endpoint = devm_kzalloc(dev, sizeof(*endpoint), GFP_KERNEL);
  1536. if (!endpoint)
  1537. return NULL;
  1538. endpoint->pdev = pdev;
  1539. endpoint->dev = dev;
  1540. endpoint->ephw = ephw;
  1541. endpoint->msg_counter = 0x0b;
  1542. endpoint->failed_messages = 0;
  1543. endpoint->fatal_error = 0;
  1544. init_waitqueue_head(&endpoint->ep_wait);
  1545. mutex_init(&endpoint->register_mutex);
  1546. return endpoint;
  1547. }
  1548. EXPORT_SYMBOL(xillybus_init_endpoint);
  1549. static int xilly_quiesce(struct xilly_endpoint *endpoint)
  1550. {
  1551. long t;
  1552. endpoint->idtlen = -1;
  1553. iowrite32((u32) (endpoint->dma_using_dac & 0x0001),
  1554. endpoint->registers + fpga_dma_control_reg);
  1555. t = wait_event_interruptible_timeout(endpoint->ep_wait,
  1556. (endpoint->idtlen >= 0),
  1557. XILLY_TIMEOUT);
  1558. if (t <= 0) {
  1559. dev_err(endpoint->dev,
  1560. "Failed to quiesce the device on exit.\n");
  1561. return -ENODEV;
  1562. }
  1563. return 0;
  1564. }
  1565. int xillybus_endpoint_discovery(struct xilly_endpoint *endpoint)
  1566. {
  1567. int rc;
  1568. long t;
  1569. void *bootstrap_resources;
  1570. int idtbuffersize = (1 << PAGE_SHIFT);
  1571. struct device *dev = endpoint->dev;
  1572. /*
  1573. * The bogus IDT is used during bootstrap for allocating the initial
  1574. * message buffer, and then the message buffer and space for the IDT
  1575. * itself. The initial message buffer is of a single page's size, but
  1576. * it's soon replaced with a more modest one (and memory is freed).
  1577. */
  1578. unsigned char bogus_idt[8] = { 1, 224, (PAGE_SHIFT)-2, 0,
  1579. 3, 192, PAGE_SHIFT, 0 };
  1580. struct xilly_idt_handle idt_handle;
  1581. /*
  1582. * Writing the value 0x00000001 to Endianness register signals which
  1583. * endianness this processor is using, so the FPGA can swap words as
  1584. * necessary.
  1585. */
  1586. iowrite32(1, endpoint->registers + fpga_endian_reg);
  1587. /* Bootstrap phase I: Allocate temporary message buffer */
  1588. bootstrap_resources = devres_open_group(dev, NULL, GFP_KERNEL);
  1589. if (!bootstrap_resources)
  1590. return -ENOMEM;
  1591. endpoint->num_channels = 0;
  1592. rc = xilly_setupchannels(endpoint, bogus_idt, 1);
  1593. if (rc)
  1594. return rc;
  1595. /* Clear the message subsystem (and counter in particular) */
  1596. iowrite32(0x04, endpoint->registers + fpga_msg_ctrl_reg);
  1597. endpoint->idtlen = -1;
  1598. /*
  1599. * Set DMA 32/64 bit mode, quiesce the device (?!) and get IDT
  1600. * buffer size.
  1601. */
  1602. iowrite32((u32) (endpoint->dma_using_dac & 0x0001),
  1603. endpoint->registers + fpga_dma_control_reg);
  1604. t = wait_event_interruptible_timeout(endpoint->ep_wait,
  1605. (endpoint->idtlen >= 0),
  1606. XILLY_TIMEOUT);
  1607. if (t <= 0) {
  1608. dev_err(endpoint->dev, "No response from FPGA. Aborting.\n");
  1609. return -ENODEV;
  1610. }
  1611. /* Enable DMA */
  1612. iowrite32((u32) (0x0002 | (endpoint->dma_using_dac & 0x0001)),
  1613. endpoint->registers + fpga_dma_control_reg);
  1614. /* Bootstrap phase II: Allocate buffer for IDT and obtain it */
  1615. while (endpoint->idtlen >= idtbuffersize) {
  1616. idtbuffersize *= 2;
  1617. bogus_idt[6]++;
  1618. }
  1619. endpoint->num_channels = 1;
  1620. rc = xilly_setupchannels(endpoint, bogus_idt, 2);
  1621. if (rc)
  1622. goto failed_idt;
  1623. rc = xilly_obtain_idt(endpoint);
  1624. if (rc)
  1625. goto failed_idt;
  1626. rc = xilly_scan_idt(endpoint, &idt_handle);
  1627. if (rc)
  1628. goto failed_idt;
  1629. devres_close_group(dev, bootstrap_resources);
  1630. /* Bootstrap phase III: Allocate buffers according to IDT */
  1631. rc = xilly_setupchannels(endpoint,
  1632. idt_handle.chandesc,
  1633. idt_handle.entries);
  1634. if (rc)
  1635. goto failed_idt;
  1636. /*
  1637. * endpoint is now completely configured. We put it on the list
  1638. * available to open() before registering the char device(s)
  1639. */
  1640. mutex_lock(&ep_list_lock);
  1641. list_add_tail(&endpoint->ep_list, &list_of_endpoints);
  1642. mutex_unlock(&ep_list_lock);
  1643. rc = xillybus_init_chrdev(endpoint, idt_handle.idt);
  1644. if (rc)
  1645. goto failed_chrdevs;
  1646. devres_release_group(dev, bootstrap_resources);
  1647. return 0;
  1648. failed_chrdevs:
  1649. mutex_lock(&ep_list_lock);
  1650. list_del(&endpoint->ep_list);
  1651. mutex_unlock(&ep_list_lock);
  1652. failed_idt:
  1653. xilly_quiesce(endpoint);
  1654. flush_workqueue(xillybus_wq);
  1655. return rc;
  1656. }
  1657. EXPORT_SYMBOL(xillybus_endpoint_discovery);
  1658. void xillybus_endpoint_remove(struct xilly_endpoint *endpoint)
  1659. {
  1660. xillybus_cleanup_chrdev(endpoint);
  1661. mutex_lock(&ep_list_lock);
  1662. list_del(&endpoint->ep_list);
  1663. mutex_unlock(&ep_list_lock);
  1664. xilly_quiesce(endpoint);
  1665. /*
  1666. * Flushing is done upon endpoint release to prevent access to memory
  1667. * just about to be released. This makes the quiesce complete.
  1668. */
  1669. flush_workqueue(xillybus_wq);
  1670. }
  1671. EXPORT_SYMBOL(xillybus_endpoint_remove);
  1672. static int __init xillybus_init(void)
  1673. {
  1674. mutex_init(&ep_list_lock);
  1675. xillybus_class = class_create(THIS_MODULE, xillyname);
  1676. if (IS_ERR(xillybus_class))
  1677. return PTR_ERR(xillybus_class);
  1678. xillybus_wq = alloc_workqueue(xillyname, 0, 0);
  1679. if (!xillybus_wq) {
  1680. class_destroy(xillybus_class);
  1681. return -ENOMEM;
  1682. }
  1683. return 0;
  1684. }
  1685. static void __exit xillybus_exit(void)
  1686. {
  1687. /* flush_workqueue() was called for each endpoint released */
  1688. destroy_workqueue(xillybus_wq);
  1689. class_destroy(xillybus_class);
  1690. }
  1691. module_init(xillybus_init);
  1692. module_exit(xillybus_exit);