qib_user_sdma.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /*
  2. * Copyright (c) 2007, 2008, 2009 QLogic Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/mm.h>
  33. #include <linux/types.h>
  34. #include <linux/device.h>
  35. #include <linux/dmapool.h>
  36. #include <linux/slab.h>
  37. #include <linux/list.h>
  38. #include <linux/highmem.h>
  39. #include <linux/io.h>
  40. #include <linux/uio.h>
  41. #include <linux/rbtree.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/delay.h>
  44. #include "qib.h"
  45. #include "qib_user_sdma.h"
  46. /* minimum size of header */
  47. #define QIB_USER_SDMA_MIN_HEADER_LENGTH 64
  48. /* expected size of headers (for dma_pool) */
  49. #define QIB_USER_SDMA_EXP_HEADER_LENGTH 64
  50. /* attempt to drain the queue for 5secs */
  51. #define QIB_USER_SDMA_DRAIN_TIMEOUT 500
  52. struct qib_user_sdma_pkt {
  53. struct list_head list; /* list element */
  54. u8 tiddma; /* if this is NEW tid-sdma */
  55. u8 largepkt; /* this is large pkt from kmalloc */
  56. u16 frag_size; /* frag size used by PSM */
  57. u16 index; /* last header index or push index */
  58. u16 naddr; /* dimension of addr (1..3) ... */
  59. u16 addrlimit; /* addr array size */
  60. u16 tidsmidx; /* current tidsm index */
  61. u16 tidsmcount; /* tidsm array item count */
  62. u16 payload_size; /* payload size so far for header */
  63. u32 bytes_togo; /* bytes for processing */
  64. u32 counter; /* sdma pkts queued counter for this entry */
  65. struct qib_tid_session_member *tidsm; /* tid session member array */
  66. struct qib_user_sdma_queue *pq; /* which pq this pkt belongs to */
  67. u64 added; /* global descq number of entries */
  68. struct {
  69. u16 offset; /* offset for kvaddr, addr */
  70. u16 length; /* length in page */
  71. u16 first_desc; /* first desc */
  72. u16 last_desc; /* last desc */
  73. u16 put_page; /* should we put_page? */
  74. u16 dma_mapped; /* is page dma_mapped? */
  75. u16 dma_length; /* for dma_unmap_page() */
  76. u16 padding;
  77. struct page *page; /* may be NULL (coherent mem) */
  78. void *kvaddr; /* FIXME: only for pio hack */
  79. dma_addr_t addr;
  80. } addr[4]; /* max pages, any more and we coalesce */
  81. };
  82. struct qib_user_sdma_queue {
  83. /*
  84. * pkts sent to dma engine are queued on this
  85. * list head. the type of the elements of this
  86. * list are struct qib_user_sdma_pkt...
  87. */
  88. struct list_head sent;
  89. /*
  90. * Because above list will be accessed by both process and
  91. * signal handler, we need a spinlock for it.
  92. */
  93. spinlock_t sent_lock ____cacheline_aligned_in_smp;
  94. /* headers with expected length are allocated from here... */
  95. char header_cache_name[64];
  96. struct dma_pool *header_cache;
  97. /* packets are allocated from the slab cache... */
  98. char pkt_slab_name[64];
  99. struct kmem_cache *pkt_slab;
  100. /* as packets go on the queued queue, they are counted... */
  101. u32 counter;
  102. u32 sent_counter;
  103. /* pending packets, not sending yet */
  104. u32 num_pending;
  105. /* sending packets, not complete yet */
  106. u32 num_sending;
  107. /* global descq number of entry of last sending packet */
  108. u64 added;
  109. /* dma page table */
  110. struct rb_root dma_pages_root;
  111. /* protect everything above... */
  112. struct mutex lock;
  113. };
  114. struct qib_user_sdma_queue *
  115. qib_user_sdma_queue_create(struct device *dev, int unit, int ctxt, int sctxt)
  116. {
  117. struct qib_user_sdma_queue *pq =
  118. kmalloc(sizeof(struct qib_user_sdma_queue), GFP_KERNEL);
  119. if (!pq)
  120. goto done;
  121. pq->counter = 0;
  122. pq->sent_counter = 0;
  123. pq->num_pending = 0;
  124. pq->num_sending = 0;
  125. pq->added = 0;
  126. INIT_LIST_HEAD(&pq->sent);
  127. spin_lock_init(&pq->sent_lock);
  128. mutex_init(&pq->lock);
  129. snprintf(pq->pkt_slab_name, sizeof(pq->pkt_slab_name),
  130. "qib-user-sdma-pkts-%u-%02u.%02u", unit, ctxt, sctxt);
  131. pq->pkt_slab = kmem_cache_create(pq->pkt_slab_name,
  132. sizeof(struct qib_user_sdma_pkt),
  133. 0, 0, NULL);
  134. if (!pq->pkt_slab)
  135. goto err_kfree;
  136. snprintf(pq->header_cache_name, sizeof(pq->header_cache_name),
  137. "qib-user-sdma-headers-%u-%02u.%02u", unit, ctxt, sctxt);
  138. pq->header_cache = dma_pool_create(pq->header_cache_name,
  139. dev,
  140. QIB_USER_SDMA_EXP_HEADER_LENGTH,
  141. 4, 0);
  142. if (!pq->header_cache)
  143. goto err_slab;
  144. pq->dma_pages_root = RB_ROOT;
  145. goto done;
  146. err_slab:
  147. kmem_cache_destroy(pq->pkt_slab);
  148. err_kfree:
  149. kfree(pq);
  150. pq = NULL;
  151. done:
  152. return pq;
  153. }
  154. static void qib_user_sdma_init_frag(struct qib_user_sdma_pkt *pkt,
  155. int i, u16 offset, u16 len,
  156. u16 first_desc, u16 last_desc,
  157. u16 put_page, u16 dma_mapped,
  158. struct page *page, void *kvaddr,
  159. dma_addr_t dma_addr, u16 dma_length)
  160. {
  161. pkt->addr[i].offset = offset;
  162. pkt->addr[i].length = len;
  163. pkt->addr[i].first_desc = first_desc;
  164. pkt->addr[i].last_desc = last_desc;
  165. pkt->addr[i].put_page = put_page;
  166. pkt->addr[i].dma_mapped = dma_mapped;
  167. pkt->addr[i].page = page;
  168. pkt->addr[i].kvaddr = kvaddr;
  169. pkt->addr[i].addr = dma_addr;
  170. pkt->addr[i].dma_length = dma_length;
  171. }
  172. static void *qib_user_sdma_alloc_header(struct qib_user_sdma_queue *pq,
  173. size_t len, dma_addr_t *dma_addr)
  174. {
  175. void *hdr;
  176. if (len == QIB_USER_SDMA_EXP_HEADER_LENGTH)
  177. hdr = dma_pool_alloc(pq->header_cache, GFP_KERNEL,
  178. dma_addr);
  179. else
  180. hdr = NULL;
  181. if (!hdr) {
  182. hdr = kmalloc(len, GFP_KERNEL);
  183. if (!hdr)
  184. return NULL;
  185. *dma_addr = 0;
  186. }
  187. return hdr;
  188. }
  189. static int qib_user_sdma_page_to_frags(const struct qib_devdata *dd,
  190. struct qib_user_sdma_queue *pq,
  191. struct qib_user_sdma_pkt *pkt,
  192. struct page *page, u16 put,
  193. u16 offset, u16 len, void *kvaddr)
  194. {
  195. __le16 *pbc16;
  196. void *pbcvaddr;
  197. struct qib_message_header *hdr;
  198. u16 newlen, pbclen, lastdesc, dma_mapped;
  199. u32 vcto;
  200. union qib_seqnum seqnum;
  201. dma_addr_t pbcdaddr;
  202. dma_addr_t dma_addr =
  203. dma_map_page(&dd->pcidev->dev,
  204. page, offset, len, DMA_TO_DEVICE);
  205. int ret = 0;
  206. if (dma_mapping_error(&dd->pcidev->dev, dma_addr)) {
  207. /*
  208. * dma mapping error, pkt has not managed
  209. * this page yet, return the page here so
  210. * the caller can ignore this page.
  211. */
  212. if (put) {
  213. put_page(page);
  214. } else {
  215. /* coalesce case */
  216. kunmap(page);
  217. __free_page(page);
  218. }
  219. ret = -ENOMEM;
  220. goto done;
  221. }
  222. offset = 0;
  223. dma_mapped = 1;
  224. next_fragment:
  225. /*
  226. * In tid-sdma, the transfer length is restricted by
  227. * receiver side current tid page length.
  228. */
  229. if (pkt->tiddma && len > pkt->tidsm[pkt->tidsmidx].length)
  230. newlen = pkt->tidsm[pkt->tidsmidx].length;
  231. else
  232. newlen = len;
  233. /*
  234. * Then the transfer length is restricted by MTU.
  235. * the last descriptor flag is determined by:
  236. * 1. the current packet is at frag size length.
  237. * 2. the current tid page is done if tid-sdma.
  238. * 3. there is no more byte togo if sdma.
  239. */
  240. lastdesc = 0;
  241. if ((pkt->payload_size + newlen) >= pkt->frag_size) {
  242. newlen = pkt->frag_size - pkt->payload_size;
  243. lastdesc = 1;
  244. } else if (pkt->tiddma) {
  245. if (newlen == pkt->tidsm[pkt->tidsmidx].length)
  246. lastdesc = 1;
  247. } else {
  248. if (newlen == pkt->bytes_togo)
  249. lastdesc = 1;
  250. }
  251. /* fill the next fragment in this page */
  252. qib_user_sdma_init_frag(pkt, pkt->naddr, /* index */
  253. offset, newlen, /* offset, len */
  254. 0, lastdesc, /* first last desc */
  255. put, dma_mapped, /* put page, dma mapped */
  256. page, kvaddr, /* struct page, virt addr */
  257. dma_addr, len); /* dma addr, dma length */
  258. pkt->bytes_togo -= newlen;
  259. pkt->payload_size += newlen;
  260. pkt->naddr++;
  261. if (pkt->naddr == pkt->addrlimit) {
  262. ret = -EFAULT;
  263. goto done;
  264. }
  265. /* If there is no more byte togo. (lastdesc==1) */
  266. if (pkt->bytes_togo == 0) {
  267. /* The packet is done, header is not dma mapped yet.
  268. * it should be from kmalloc */
  269. if (!pkt->addr[pkt->index].addr) {
  270. pkt->addr[pkt->index].addr =
  271. dma_map_single(&dd->pcidev->dev,
  272. pkt->addr[pkt->index].kvaddr,
  273. pkt->addr[pkt->index].dma_length,
  274. DMA_TO_DEVICE);
  275. if (dma_mapping_error(&dd->pcidev->dev,
  276. pkt->addr[pkt->index].addr)) {
  277. ret = -ENOMEM;
  278. goto done;
  279. }
  280. pkt->addr[pkt->index].dma_mapped = 1;
  281. }
  282. goto done;
  283. }
  284. /* If tid-sdma, advance tid info. */
  285. if (pkt->tiddma) {
  286. pkt->tidsm[pkt->tidsmidx].length -= newlen;
  287. if (pkt->tidsm[pkt->tidsmidx].length) {
  288. pkt->tidsm[pkt->tidsmidx].offset += newlen;
  289. } else {
  290. pkt->tidsmidx++;
  291. if (pkt->tidsmidx == pkt->tidsmcount) {
  292. ret = -EFAULT;
  293. goto done;
  294. }
  295. }
  296. }
  297. /*
  298. * If this is NOT the last descriptor. (newlen==len)
  299. * the current packet is not done yet, but the current
  300. * send side page is done.
  301. */
  302. if (lastdesc == 0)
  303. goto done;
  304. /*
  305. * If running this driver under PSM with message size
  306. * fitting into one transfer unit, it is not possible
  307. * to pass this line. otherwise, it is a buggggg.
  308. */
  309. /*
  310. * Since the current packet is done, and there are more
  311. * bytes togo, we need to create a new sdma header, copying
  312. * from previous sdma header and modify both.
  313. */
  314. pbclen = pkt->addr[pkt->index].length;
  315. pbcvaddr = qib_user_sdma_alloc_header(pq, pbclen, &pbcdaddr);
  316. if (!pbcvaddr) {
  317. ret = -ENOMEM;
  318. goto done;
  319. }
  320. /* Copy the previous sdma header to new sdma header */
  321. pbc16 = (__le16 *)pkt->addr[pkt->index].kvaddr;
  322. memcpy(pbcvaddr, pbc16, pbclen);
  323. /* Modify the previous sdma header */
  324. hdr = (struct qib_message_header *)&pbc16[4];
  325. /* New pbc length */
  326. pbc16[0] = cpu_to_le16(le16_to_cpu(pbc16[0])-(pkt->bytes_togo>>2));
  327. /* New packet length */
  328. hdr->lrh[2] = cpu_to_be16(le16_to_cpu(pbc16[0]));
  329. if (pkt->tiddma) {
  330. /* turn on the header suppression */
  331. hdr->iph.pkt_flags =
  332. cpu_to_le16(le16_to_cpu(hdr->iph.pkt_flags)|0x2);
  333. /* turn off ACK_REQ: 0x04 and EXPECTED_DONE: 0x20 */
  334. hdr->flags &= ~(0x04|0x20);
  335. } else {
  336. /* turn off extra bytes: 20-21 bits */
  337. hdr->bth[0] = cpu_to_be32(be32_to_cpu(hdr->bth[0])&0xFFCFFFFF);
  338. /* turn off ACK_REQ: 0x04 */
  339. hdr->flags &= ~(0x04);
  340. }
  341. /* New kdeth checksum */
  342. vcto = le32_to_cpu(hdr->iph.ver_ctxt_tid_offset);
  343. hdr->iph.chksum = cpu_to_le16(QIB_LRH_BTH +
  344. be16_to_cpu(hdr->lrh[2]) -
  345. ((vcto>>16)&0xFFFF) - (vcto&0xFFFF) -
  346. le16_to_cpu(hdr->iph.pkt_flags));
  347. /* The packet is done, header is not dma mapped yet.
  348. * it should be from kmalloc */
  349. if (!pkt->addr[pkt->index].addr) {
  350. pkt->addr[pkt->index].addr =
  351. dma_map_single(&dd->pcidev->dev,
  352. pkt->addr[pkt->index].kvaddr,
  353. pkt->addr[pkt->index].dma_length,
  354. DMA_TO_DEVICE);
  355. if (dma_mapping_error(&dd->pcidev->dev,
  356. pkt->addr[pkt->index].addr)) {
  357. ret = -ENOMEM;
  358. goto done;
  359. }
  360. pkt->addr[pkt->index].dma_mapped = 1;
  361. }
  362. /* Modify the new sdma header */
  363. pbc16 = (__le16 *)pbcvaddr;
  364. hdr = (struct qib_message_header *)&pbc16[4];
  365. /* New pbc length */
  366. pbc16[0] = cpu_to_le16(le16_to_cpu(pbc16[0])-(pkt->payload_size>>2));
  367. /* New packet length */
  368. hdr->lrh[2] = cpu_to_be16(le16_to_cpu(pbc16[0]));
  369. if (pkt->tiddma) {
  370. /* Set new tid and offset for new sdma header */
  371. hdr->iph.ver_ctxt_tid_offset = cpu_to_le32(
  372. (le32_to_cpu(hdr->iph.ver_ctxt_tid_offset)&0xFF000000) +
  373. (pkt->tidsm[pkt->tidsmidx].tid<<QLOGIC_IB_I_TID_SHIFT) +
  374. (pkt->tidsm[pkt->tidsmidx].offset>>2));
  375. } else {
  376. /* Middle protocol new packet offset */
  377. hdr->uwords[2] += pkt->payload_size;
  378. }
  379. /* New kdeth checksum */
  380. vcto = le32_to_cpu(hdr->iph.ver_ctxt_tid_offset);
  381. hdr->iph.chksum = cpu_to_le16(QIB_LRH_BTH +
  382. be16_to_cpu(hdr->lrh[2]) -
  383. ((vcto>>16)&0xFFFF) - (vcto&0xFFFF) -
  384. le16_to_cpu(hdr->iph.pkt_flags));
  385. /* Next sequence number in new sdma header */
  386. seqnum.val = be32_to_cpu(hdr->bth[2]);
  387. if (pkt->tiddma)
  388. seqnum.seq++;
  389. else
  390. seqnum.pkt++;
  391. hdr->bth[2] = cpu_to_be32(seqnum.val);
  392. /* Init new sdma header. */
  393. qib_user_sdma_init_frag(pkt, pkt->naddr, /* index */
  394. 0, pbclen, /* offset, len */
  395. 1, 0, /* first last desc */
  396. 0, 0, /* put page, dma mapped */
  397. NULL, pbcvaddr, /* struct page, virt addr */
  398. pbcdaddr, pbclen); /* dma addr, dma length */
  399. pkt->index = pkt->naddr;
  400. pkt->payload_size = 0;
  401. pkt->naddr++;
  402. if (pkt->naddr == pkt->addrlimit) {
  403. ret = -EFAULT;
  404. goto done;
  405. }
  406. /* Prepare for next fragment in this page */
  407. if (newlen != len) {
  408. if (dma_mapped) {
  409. put = 0;
  410. dma_mapped = 0;
  411. page = NULL;
  412. kvaddr = NULL;
  413. }
  414. len -= newlen;
  415. offset += newlen;
  416. goto next_fragment;
  417. }
  418. done:
  419. return ret;
  420. }
  421. /* we've too many pages in the iovec, coalesce to a single page */
  422. static int qib_user_sdma_coalesce(const struct qib_devdata *dd,
  423. struct qib_user_sdma_queue *pq,
  424. struct qib_user_sdma_pkt *pkt,
  425. const struct iovec *iov,
  426. unsigned long niov)
  427. {
  428. int ret = 0;
  429. struct page *page = alloc_page(GFP_KERNEL);
  430. void *mpage_save;
  431. char *mpage;
  432. int i;
  433. int len = 0;
  434. if (!page) {
  435. ret = -ENOMEM;
  436. goto done;
  437. }
  438. mpage = kmap(page);
  439. mpage_save = mpage;
  440. for (i = 0; i < niov; i++) {
  441. int cfur;
  442. cfur = copy_from_user(mpage,
  443. iov[i].iov_base, iov[i].iov_len);
  444. if (cfur) {
  445. ret = -EFAULT;
  446. goto free_unmap;
  447. }
  448. mpage += iov[i].iov_len;
  449. len += iov[i].iov_len;
  450. }
  451. ret = qib_user_sdma_page_to_frags(dd, pq, pkt,
  452. page, 0, 0, len, mpage_save);
  453. goto done;
  454. free_unmap:
  455. kunmap(page);
  456. __free_page(page);
  457. done:
  458. return ret;
  459. }
  460. /*
  461. * How many pages in this iovec element?
  462. */
  463. static int qib_user_sdma_num_pages(const struct iovec *iov)
  464. {
  465. const unsigned long addr = (unsigned long) iov->iov_base;
  466. const unsigned long len = iov->iov_len;
  467. const unsigned long spage = addr & PAGE_MASK;
  468. const unsigned long epage = (addr + len - 1) & PAGE_MASK;
  469. return 1 + ((epage - spage) >> PAGE_SHIFT);
  470. }
  471. static void qib_user_sdma_free_pkt_frag(struct device *dev,
  472. struct qib_user_sdma_queue *pq,
  473. struct qib_user_sdma_pkt *pkt,
  474. int frag)
  475. {
  476. const int i = frag;
  477. if (pkt->addr[i].page) {
  478. /* only user data has page */
  479. if (pkt->addr[i].dma_mapped)
  480. dma_unmap_page(dev,
  481. pkt->addr[i].addr,
  482. pkt->addr[i].dma_length,
  483. DMA_TO_DEVICE);
  484. if (pkt->addr[i].kvaddr)
  485. kunmap(pkt->addr[i].page);
  486. if (pkt->addr[i].put_page)
  487. put_page(pkt->addr[i].page);
  488. else
  489. __free_page(pkt->addr[i].page);
  490. } else if (pkt->addr[i].kvaddr) {
  491. /* for headers */
  492. if (pkt->addr[i].dma_mapped) {
  493. /* from kmalloc & dma mapped */
  494. dma_unmap_single(dev,
  495. pkt->addr[i].addr,
  496. pkt->addr[i].dma_length,
  497. DMA_TO_DEVICE);
  498. kfree(pkt->addr[i].kvaddr);
  499. } else if (pkt->addr[i].addr) {
  500. /* free coherent mem from cache... */
  501. dma_pool_free(pq->header_cache,
  502. pkt->addr[i].kvaddr, pkt->addr[i].addr);
  503. } else {
  504. /* from kmalloc but not dma mapped */
  505. kfree(pkt->addr[i].kvaddr);
  506. }
  507. }
  508. }
  509. /* return number of pages pinned... */
  510. static int qib_user_sdma_pin_pages(const struct qib_devdata *dd,
  511. struct qib_user_sdma_queue *pq,
  512. struct qib_user_sdma_pkt *pkt,
  513. unsigned long addr, int tlen, int npages)
  514. {
  515. struct page *pages[8];
  516. int i, j;
  517. int ret = 0;
  518. while (npages) {
  519. if (npages > 8)
  520. j = 8;
  521. else
  522. j = npages;
  523. ret = get_user_pages_fast(addr, j, 0, pages);
  524. if (ret != j) {
  525. i = 0;
  526. j = ret;
  527. ret = -ENOMEM;
  528. goto free_pages;
  529. }
  530. for (i = 0; i < j; i++) {
  531. /* map the pages... */
  532. unsigned long fofs = addr & ~PAGE_MASK;
  533. int flen = ((fofs + tlen) > PAGE_SIZE) ?
  534. (PAGE_SIZE - fofs) : tlen;
  535. ret = qib_user_sdma_page_to_frags(dd, pq, pkt,
  536. pages[i], 1, fofs, flen, NULL);
  537. if (ret < 0) {
  538. /* current page has beed taken
  539. * care of inside above call.
  540. */
  541. i++;
  542. goto free_pages;
  543. }
  544. addr += flen;
  545. tlen -= flen;
  546. }
  547. npages -= j;
  548. }
  549. goto done;
  550. /* if error, return all pages not managed by pkt */
  551. free_pages:
  552. while (i < j)
  553. put_page(pages[i++]);
  554. done:
  555. return ret;
  556. }
  557. static int qib_user_sdma_pin_pkt(const struct qib_devdata *dd,
  558. struct qib_user_sdma_queue *pq,
  559. struct qib_user_sdma_pkt *pkt,
  560. const struct iovec *iov,
  561. unsigned long niov)
  562. {
  563. int ret = 0;
  564. unsigned long idx;
  565. for (idx = 0; idx < niov; idx++) {
  566. const int npages = qib_user_sdma_num_pages(iov + idx);
  567. const unsigned long addr = (unsigned long) iov[idx].iov_base;
  568. ret = qib_user_sdma_pin_pages(dd, pq, pkt, addr,
  569. iov[idx].iov_len, npages);
  570. if (ret < 0)
  571. goto free_pkt;
  572. }
  573. goto done;
  574. free_pkt:
  575. /* we need to ignore the first entry here */
  576. for (idx = 1; idx < pkt->naddr; idx++)
  577. qib_user_sdma_free_pkt_frag(&dd->pcidev->dev, pq, pkt, idx);
  578. /* need to dma unmap the first entry, this is to restore to
  579. * the original state so that caller can free the memory in
  580. * error condition. Caller does not know if dma mapped or not*/
  581. if (pkt->addr[0].dma_mapped) {
  582. dma_unmap_single(&dd->pcidev->dev,
  583. pkt->addr[0].addr,
  584. pkt->addr[0].dma_length,
  585. DMA_TO_DEVICE);
  586. pkt->addr[0].addr = 0;
  587. pkt->addr[0].dma_mapped = 0;
  588. }
  589. done:
  590. return ret;
  591. }
  592. static int qib_user_sdma_init_payload(const struct qib_devdata *dd,
  593. struct qib_user_sdma_queue *pq,
  594. struct qib_user_sdma_pkt *pkt,
  595. const struct iovec *iov,
  596. unsigned long niov, int npages)
  597. {
  598. int ret = 0;
  599. if (pkt->frag_size == pkt->bytes_togo &&
  600. npages >= ARRAY_SIZE(pkt->addr))
  601. ret = qib_user_sdma_coalesce(dd, pq, pkt, iov, niov);
  602. else
  603. ret = qib_user_sdma_pin_pkt(dd, pq, pkt, iov, niov);
  604. return ret;
  605. }
  606. /* free a packet list -- return counter value of last packet */
  607. static void qib_user_sdma_free_pkt_list(struct device *dev,
  608. struct qib_user_sdma_queue *pq,
  609. struct list_head *list)
  610. {
  611. struct qib_user_sdma_pkt *pkt, *pkt_next;
  612. list_for_each_entry_safe(pkt, pkt_next, list, list) {
  613. int i;
  614. for (i = 0; i < pkt->naddr; i++)
  615. qib_user_sdma_free_pkt_frag(dev, pq, pkt, i);
  616. if (pkt->largepkt)
  617. kfree(pkt);
  618. else
  619. kmem_cache_free(pq->pkt_slab, pkt);
  620. }
  621. INIT_LIST_HEAD(list);
  622. }
  623. /*
  624. * copy headers, coalesce etc -- pq->lock must be held
  625. *
  626. * we queue all the packets to list, returning the
  627. * number of bytes total. list must be empty initially,
  628. * as, if there is an error we clean it...
  629. */
  630. static int qib_user_sdma_queue_pkts(const struct qib_devdata *dd,
  631. struct qib_pportdata *ppd,
  632. struct qib_user_sdma_queue *pq,
  633. const struct iovec *iov,
  634. unsigned long niov,
  635. struct list_head *list,
  636. int *maxpkts, int *ndesc)
  637. {
  638. unsigned long idx = 0;
  639. int ret = 0;
  640. int npkts = 0;
  641. __le32 *pbc;
  642. dma_addr_t dma_addr;
  643. struct qib_user_sdma_pkt *pkt = NULL;
  644. size_t len;
  645. size_t nw;
  646. u32 counter = pq->counter;
  647. u16 frag_size;
  648. while (idx < niov && npkts < *maxpkts) {
  649. const unsigned long addr = (unsigned long) iov[idx].iov_base;
  650. const unsigned long idx_save = idx;
  651. unsigned pktnw;
  652. unsigned pktnwc;
  653. int nfrags = 0;
  654. int npages = 0;
  655. int bytes_togo = 0;
  656. int tiddma = 0;
  657. int cfur;
  658. len = iov[idx].iov_len;
  659. nw = len >> 2;
  660. if (len < QIB_USER_SDMA_MIN_HEADER_LENGTH ||
  661. len > PAGE_SIZE || len & 3 || addr & 3) {
  662. ret = -EINVAL;
  663. goto free_list;
  664. }
  665. pbc = qib_user_sdma_alloc_header(pq, len, &dma_addr);
  666. if (!pbc) {
  667. ret = -ENOMEM;
  668. goto free_list;
  669. }
  670. cfur = copy_from_user(pbc, iov[idx].iov_base, len);
  671. if (cfur) {
  672. ret = -EFAULT;
  673. goto free_pbc;
  674. }
  675. /*
  676. * This assignment is a bit strange. it's because the
  677. * the pbc counts the number of 32 bit words in the full
  678. * packet _except_ the first word of the pbc itself...
  679. */
  680. pktnwc = nw - 1;
  681. /*
  682. * pktnw computation yields the number of 32 bit words
  683. * that the caller has indicated in the PBC. note that
  684. * this is one less than the total number of words that
  685. * goes to the send DMA engine as the first 32 bit word
  686. * of the PBC itself is not counted. Armed with this count,
  687. * we can verify that the packet is consistent with the
  688. * iovec lengths.
  689. */
  690. pktnw = le32_to_cpu(*pbc) & 0xFFFF;
  691. if (pktnw < pktnwc) {
  692. ret = -EINVAL;
  693. goto free_pbc;
  694. }
  695. idx++;
  696. while (pktnwc < pktnw && idx < niov) {
  697. const size_t slen = iov[idx].iov_len;
  698. const unsigned long faddr =
  699. (unsigned long) iov[idx].iov_base;
  700. if (slen & 3 || faddr & 3 || !slen) {
  701. ret = -EINVAL;
  702. goto free_pbc;
  703. }
  704. npages += qib_user_sdma_num_pages(&iov[idx]);
  705. bytes_togo += slen;
  706. pktnwc += slen >> 2;
  707. idx++;
  708. nfrags++;
  709. }
  710. if (pktnwc != pktnw) {
  711. ret = -EINVAL;
  712. goto free_pbc;
  713. }
  714. frag_size = ((le32_to_cpu(*pbc))>>16) & 0xFFFF;
  715. if (((frag_size ? frag_size : bytes_togo) + len) >
  716. ppd->ibmaxlen) {
  717. ret = -EINVAL;
  718. goto free_pbc;
  719. }
  720. if (frag_size) {
  721. int pktsize, tidsmsize, n;
  722. n = npages*((2*PAGE_SIZE/frag_size)+1);
  723. pktsize = sizeof(*pkt) + sizeof(pkt->addr[0])*n;
  724. /*
  725. * Determine if this is tid-sdma or just sdma.
  726. */
  727. tiddma = (((le32_to_cpu(pbc[7])>>
  728. QLOGIC_IB_I_TID_SHIFT)&
  729. QLOGIC_IB_I_TID_MASK) !=
  730. QLOGIC_IB_I_TID_MASK);
  731. if (tiddma)
  732. tidsmsize = iov[idx].iov_len;
  733. else
  734. tidsmsize = 0;
  735. pkt = kmalloc(pktsize+tidsmsize, GFP_KERNEL);
  736. if (!pkt) {
  737. ret = -ENOMEM;
  738. goto free_pbc;
  739. }
  740. pkt->largepkt = 1;
  741. pkt->frag_size = frag_size;
  742. pkt->addrlimit = n + ARRAY_SIZE(pkt->addr);
  743. if (tiddma) {
  744. char *tidsm = (char *)pkt + pktsize;
  745. cfur = copy_from_user(tidsm,
  746. iov[idx].iov_base, tidsmsize);
  747. if (cfur) {
  748. ret = -EFAULT;
  749. goto free_pkt;
  750. }
  751. pkt->tidsm =
  752. (struct qib_tid_session_member *)tidsm;
  753. pkt->tidsmcount = tidsmsize/
  754. sizeof(struct qib_tid_session_member);
  755. pkt->tidsmidx = 0;
  756. idx++;
  757. }
  758. /*
  759. * pbc 'fill1' field is borrowed to pass frag size,
  760. * we need to clear it after picking frag size, the
  761. * hardware requires this field to be zero.
  762. */
  763. *pbc = cpu_to_le32(le32_to_cpu(*pbc) & 0x0000FFFF);
  764. } else {
  765. pkt = kmem_cache_alloc(pq->pkt_slab, GFP_KERNEL);
  766. if (!pkt) {
  767. ret = -ENOMEM;
  768. goto free_pbc;
  769. }
  770. pkt->largepkt = 0;
  771. pkt->frag_size = bytes_togo;
  772. pkt->addrlimit = ARRAY_SIZE(pkt->addr);
  773. }
  774. pkt->bytes_togo = bytes_togo;
  775. pkt->payload_size = 0;
  776. pkt->counter = counter;
  777. pkt->tiddma = tiddma;
  778. /* setup the first header */
  779. qib_user_sdma_init_frag(pkt, 0, /* index */
  780. 0, len, /* offset, len */
  781. 1, 0, /* first last desc */
  782. 0, 0, /* put page, dma mapped */
  783. NULL, pbc, /* struct page, virt addr */
  784. dma_addr, len); /* dma addr, dma length */
  785. pkt->index = 0;
  786. pkt->naddr = 1;
  787. if (nfrags) {
  788. ret = qib_user_sdma_init_payload(dd, pq, pkt,
  789. iov + idx_save + 1,
  790. nfrags, npages);
  791. if (ret < 0)
  792. goto free_pkt;
  793. } else {
  794. /* since there is no payload, mark the
  795. * header as the last desc. */
  796. pkt->addr[0].last_desc = 1;
  797. if (dma_addr == 0) {
  798. /*
  799. * the header is not dma mapped yet.
  800. * it should be from kmalloc.
  801. */
  802. dma_addr = dma_map_single(&dd->pcidev->dev,
  803. pbc, len, DMA_TO_DEVICE);
  804. if (dma_mapping_error(&dd->pcidev->dev,
  805. dma_addr)) {
  806. ret = -ENOMEM;
  807. goto free_pkt;
  808. }
  809. pkt->addr[0].addr = dma_addr;
  810. pkt->addr[0].dma_mapped = 1;
  811. }
  812. }
  813. counter++;
  814. npkts++;
  815. pkt->pq = pq;
  816. pkt->index = 0; /* reset index for push on hw */
  817. *ndesc += pkt->naddr;
  818. list_add_tail(&pkt->list, list);
  819. }
  820. *maxpkts = npkts;
  821. ret = idx;
  822. goto done;
  823. free_pkt:
  824. if (pkt->largepkt)
  825. kfree(pkt);
  826. else
  827. kmem_cache_free(pq->pkt_slab, pkt);
  828. free_pbc:
  829. if (dma_addr)
  830. dma_pool_free(pq->header_cache, pbc, dma_addr);
  831. else
  832. kfree(pbc);
  833. free_list:
  834. qib_user_sdma_free_pkt_list(&dd->pcidev->dev, pq, list);
  835. done:
  836. return ret;
  837. }
  838. static void qib_user_sdma_set_complete_counter(struct qib_user_sdma_queue *pq,
  839. u32 c)
  840. {
  841. pq->sent_counter = c;
  842. }
  843. /* try to clean out queue -- needs pq->lock */
  844. static int qib_user_sdma_queue_clean(struct qib_pportdata *ppd,
  845. struct qib_user_sdma_queue *pq)
  846. {
  847. struct qib_devdata *dd = ppd->dd;
  848. struct list_head free_list;
  849. struct qib_user_sdma_pkt *pkt;
  850. struct qib_user_sdma_pkt *pkt_prev;
  851. unsigned long flags;
  852. int ret = 0;
  853. if (!pq->num_sending)
  854. return 0;
  855. INIT_LIST_HEAD(&free_list);
  856. /*
  857. * We need this spin lock here because interrupt handler
  858. * might modify this list in qib_user_sdma_send_desc(), also
  859. * we can not get interrupted, otherwise it is a deadlock.
  860. */
  861. spin_lock_irqsave(&pq->sent_lock, flags);
  862. list_for_each_entry_safe(pkt, pkt_prev, &pq->sent, list) {
  863. s64 descd = ppd->sdma_descq_removed - pkt->added;
  864. if (descd < 0)
  865. break;
  866. list_move_tail(&pkt->list, &free_list);
  867. /* one more packet cleaned */
  868. ret++;
  869. pq->num_sending--;
  870. }
  871. spin_unlock_irqrestore(&pq->sent_lock, flags);
  872. if (!list_empty(&free_list)) {
  873. u32 counter;
  874. pkt = list_entry(free_list.prev,
  875. struct qib_user_sdma_pkt, list);
  876. counter = pkt->counter;
  877. qib_user_sdma_free_pkt_list(&dd->pcidev->dev, pq, &free_list);
  878. qib_user_sdma_set_complete_counter(pq, counter);
  879. }
  880. return ret;
  881. }
  882. void qib_user_sdma_queue_destroy(struct qib_user_sdma_queue *pq)
  883. {
  884. if (!pq)
  885. return;
  886. kmem_cache_destroy(pq->pkt_slab);
  887. dma_pool_destroy(pq->header_cache);
  888. kfree(pq);
  889. }
  890. /* clean descriptor queue, returns > 0 if some elements cleaned */
  891. static int qib_user_sdma_hwqueue_clean(struct qib_pportdata *ppd)
  892. {
  893. int ret;
  894. unsigned long flags;
  895. spin_lock_irqsave(&ppd->sdma_lock, flags);
  896. ret = qib_sdma_make_progress(ppd);
  897. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  898. return ret;
  899. }
  900. /* we're in close, drain packets so that we can cleanup successfully... */
  901. void qib_user_sdma_queue_drain(struct qib_pportdata *ppd,
  902. struct qib_user_sdma_queue *pq)
  903. {
  904. struct qib_devdata *dd = ppd->dd;
  905. unsigned long flags;
  906. int i;
  907. if (!pq)
  908. return;
  909. for (i = 0; i < QIB_USER_SDMA_DRAIN_TIMEOUT; i++) {
  910. mutex_lock(&pq->lock);
  911. if (!pq->num_pending && !pq->num_sending) {
  912. mutex_unlock(&pq->lock);
  913. break;
  914. }
  915. qib_user_sdma_hwqueue_clean(ppd);
  916. qib_user_sdma_queue_clean(ppd, pq);
  917. mutex_unlock(&pq->lock);
  918. msleep(10);
  919. }
  920. if (pq->num_pending || pq->num_sending) {
  921. struct qib_user_sdma_pkt *pkt;
  922. struct qib_user_sdma_pkt *pkt_prev;
  923. struct list_head free_list;
  924. mutex_lock(&pq->lock);
  925. spin_lock_irqsave(&ppd->sdma_lock, flags);
  926. /*
  927. * Since we hold sdma_lock, it is safe without sent_lock.
  928. */
  929. if (pq->num_pending) {
  930. list_for_each_entry_safe(pkt, pkt_prev,
  931. &ppd->sdma_userpending, list) {
  932. if (pkt->pq == pq) {
  933. list_move_tail(&pkt->list, &pq->sent);
  934. pq->num_pending--;
  935. pq->num_sending++;
  936. }
  937. }
  938. }
  939. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  940. qib_dev_err(dd, "user sdma lists not empty: forcing!\n");
  941. INIT_LIST_HEAD(&free_list);
  942. list_splice_init(&pq->sent, &free_list);
  943. pq->num_sending = 0;
  944. qib_user_sdma_free_pkt_list(&dd->pcidev->dev, pq, &free_list);
  945. mutex_unlock(&pq->lock);
  946. }
  947. }
  948. static inline __le64 qib_sdma_make_desc0(u8 gen,
  949. u64 addr, u64 dwlen, u64 dwoffset)
  950. {
  951. return cpu_to_le64(/* SDmaPhyAddr[31:0] */
  952. ((addr & 0xfffffffcULL) << 32) |
  953. /* SDmaGeneration[1:0] */
  954. ((gen & 3ULL) << 30) |
  955. /* SDmaDwordCount[10:0] */
  956. ((dwlen & 0x7ffULL) << 16) |
  957. /* SDmaBufOffset[12:2] */
  958. (dwoffset & 0x7ffULL));
  959. }
  960. static inline __le64 qib_sdma_make_first_desc0(__le64 descq)
  961. {
  962. return descq | cpu_to_le64(1ULL << 12);
  963. }
  964. static inline __le64 qib_sdma_make_last_desc0(__le64 descq)
  965. {
  966. /* last */ /* dma head */
  967. return descq | cpu_to_le64(1ULL << 11 | 1ULL << 13);
  968. }
  969. static inline __le64 qib_sdma_make_desc1(u64 addr)
  970. {
  971. /* SDmaPhyAddr[47:32] */
  972. return cpu_to_le64(addr >> 32);
  973. }
  974. static void qib_user_sdma_send_frag(struct qib_pportdata *ppd,
  975. struct qib_user_sdma_pkt *pkt, int idx,
  976. unsigned ofs, u16 tail, u8 gen)
  977. {
  978. const u64 addr = (u64) pkt->addr[idx].addr +
  979. (u64) pkt->addr[idx].offset;
  980. const u64 dwlen = (u64) pkt->addr[idx].length / 4;
  981. __le64 *descqp;
  982. __le64 descq0;
  983. descqp = &ppd->sdma_descq[tail].qw[0];
  984. descq0 = qib_sdma_make_desc0(gen, addr, dwlen, ofs);
  985. if (pkt->addr[idx].first_desc)
  986. descq0 = qib_sdma_make_first_desc0(descq0);
  987. if (pkt->addr[idx].last_desc) {
  988. descq0 = qib_sdma_make_last_desc0(descq0);
  989. if (ppd->sdma_intrequest) {
  990. descq0 |= cpu_to_le64(1ULL << 15);
  991. ppd->sdma_intrequest = 0;
  992. }
  993. }
  994. descqp[0] = descq0;
  995. descqp[1] = qib_sdma_make_desc1(addr);
  996. }
  997. void qib_user_sdma_send_desc(struct qib_pportdata *ppd,
  998. struct list_head *pktlist)
  999. {
  1000. struct qib_devdata *dd = ppd->dd;
  1001. u16 nfree, nsent;
  1002. u16 tail, tail_c;
  1003. u8 gen, gen_c;
  1004. nfree = qib_sdma_descq_freecnt(ppd);
  1005. if (!nfree)
  1006. return;
  1007. retry:
  1008. nsent = 0;
  1009. tail_c = tail = ppd->sdma_descq_tail;
  1010. gen_c = gen = ppd->sdma_generation;
  1011. while (!list_empty(pktlist)) {
  1012. struct qib_user_sdma_pkt *pkt =
  1013. list_entry(pktlist->next, struct qib_user_sdma_pkt,
  1014. list);
  1015. int i, j, c = 0;
  1016. unsigned ofs = 0;
  1017. u16 dtail = tail;
  1018. for (i = pkt->index; i < pkt->naddr && nfree; i++) {
  1019. qib_user_sdma_send_frag(ppd, pkt, i, ofs, tail, gen);
  1020. ofs += pkt->addr[i].length >> 2;
  1021. if (++tail == ppd->sdma_descq_cnt) {
  1022. tail = 0;
  1023. ++gen;
  1024. ppd->sdma_intrequest = 1;
  1025. } else if (tail == (ppd->sdma_descq_cnt>>1)) {
  1026. ppd->sdma_intrequest = 1;
  1027. }
  1028. nfree--;
  1029. if (pkt->addr[i].last_desc == 0)
  1030. continue;
  1031. /*
  1032. * If the packet is >= 2KB mtu equivalent, we
  1033. * have to use the large buffers, and have to
  1034. * mark each descriptor as part of a large
  1035. * buffer packet.
  1036. */
  1037. if (ofs > dd->piosize2kmax_dwords) {
  1038. for (j = pkt->index; j <= i; j++) {
  1039. ppd->sdma_descq[dtail].qw[0] |=
  1040. cpu_to_le64(1ULL << 14);
  1041. if (++dtail == ppd->sdma_descq_cnt)
  1042. dtail = 0;
  1043. }
  1044. }
  1045. c += i + 1 - pkt->index;
  1046. pkt->index = i + 1; /* index for next first */
  1047. tail_c = dtail = tail;
  1048. gen_c = gen;
  1049. ofs = 0; /* reset for next packet */
  1050. }
  1051. ppd->sdma_descq_added += c;
  1052. nsent += c;
  1053. if (pkt->index == pkt->naddr) {
  1054. pkt->added = ppd->sdma_descq_added;
  1055. pkt->pq->added = pkt->added;
  1056. pkt->pq->num_pending--;
  1057. spin_lock(&pkt->pq->sent_lock);
  1058. pkt->pq->num_sending++;
  1059. list_move_tail(&pkt->list, &pkt->pq->sent);
  1060. spin_unlock(&pkt->pq->sent_lock);
  1061. }
  1062. if (!nfree || (nsent<<2) > ppd->sdma_descq_cnt)
  1063. break;
  1064. }
  1065. /* advance the tail on the chip if necessary */
  1066. if (ppd->sdma_descq_tail != tail_c) {
  1067. ppd->sdma_generation = gen_c;
  1068. dd->f_sdma_update_tail(ppd, tail_c);
  1069. }
  1070. if (nfree && !list_empty(pktlist))
  1071. goto retry;
  1072. return;
  1073. }
  1074. /* pq->lock must be held, get packets on the wire... */
  1075. static int qib_user_sdma_push_pkts(struct qib_pportdata *ppd,
  1076. struct qib_user_sdma_queue *pq,
  1077. struct list_head *pktlist, int count)
  1078. {
  1079. int ret = 0;
  1080. unsigned long flags;
  1081. if (unlikely(!(ppd->lflags & QIBL_LINKACTIVE)))
  1082. return -ECOMM;
  1083. spin_lock_irqsave(&ppd->sdma_lock, flags);
  1084. if (unlikely(!__qib_sdma_running(ppd))) {
  1085. ret = -ECOMM;
  1086. goto unlock;
  1087. }
  1088. pq->num_pending += count;
  1089. list_splice_tail_init(pktlist, &ppd->sdma_userpending);
  1090. qib_user_sdma_send_desc(ppd, &ppd->sdma_userpending);
  1091. unlock:
  1092. spin_unlock_irqrestore(&ppd->sdma_lock, flags);
  1093. return ret;
  1094. }
  1095. int qib_user_sdma_writev(struct qib_ctxtdata *rcd,
  1096. struct qib_user_sdma_queue *pq,
  1097. const struct iovec *iov,
  1098. unsigned long dim)
  1099. {
  1100. struct qib_devdata *dd = rcd->dd;
  1101. struct qib_pportdata *ppd = rcd->ppd;
  1102. int ret = 0;
  1103. struct list_head list;
  1104. int npkts = 0;
  1105. INIT_LIST_HEAD(&list);
  1106. mutex_lock(&pq->lock);
  1107. /* why not -ECOMM like qib_user_sdma_push_pkts() below? */
  1108. if (!qib_sdma_running(ppd))
  1109. goto done_unlock;
  1110. /* if I have packets not complete yet */
  1111. if (pq->added > ppd->sdma_descq_removed)
  1112. qib_user_sdma_hwqueue_clean(ppd);
  1113. /* if I have complete packets to be freed */
  1114. if (pq->num_sending)
  1115. qib_user_sdma_queue_clean(ppd, pq);
  1116. while (dim) {
  1117. int mxp = 8;
  1118. int ndesc = 0;
  1119. ret = qib_user_sdma_queue_pkts(dd, ppd, pq,
  1120. iov, dim, &list, &mxp, &ndesc);
  1121. if (ret < 0)
  1122. goto done_unlock;
  1123. else {
  1124. dim -= ret;
  1125. iov += ret;
  1126. }
  1127. /* force packets onto the sdma hw queue... */
  1128. if (!list_empty(&list)) {
  1129. /*
  1130. * Lazily clean hw queue.
  1131. */
  1132. if (qib_sdma_descq_freecnt(ppd) < ndesc) {
  1133. qib_user_sdma_hwqueue_clean(ppd);
  1134. if (pq->num_sending)
  1135. qib_user_sdma_queue_clean(ppd, pq);
  1136. }
  1137. ret = qib_user_sdma_push_pkts(ppd, pq, &list, mxp);
  1138. if (ret < 0)
  1139. goto done_unlock;
  1140. else {
  1141. npkts += mxp;
  1142. pq->counter += mxp;
  1143. }
  1144. }
  1145. }
  1146. done_unlock:
  1147. if (!list_empty(&list))
  1148. qib_user_sdma_free_pkt_list(&dd->pcidev->dev, pq, &list);
  1149. mutex_unlock(&pq->lock);
  1150. return (ret < 0) ? ret : npkts;
  1151. }
  1152. int qib_user_sdma_make_progress(struct qib_pportdata *ppd,
  1153. struct qib_user_sdma_queue *pq)
  1154. {
  1155. int ret = 0;
  1156. mutex_lock(&pq->lock);
  1157. qib_user_sdma_hwqueue_clean(ppd);
  1158. ret = qib_user_sdma_queue_clean(ppd, pq);
  1159. mutex_unlock(&pq->lock);
  1160. return ret;
  1161. }
  1162. u32 qib_user_sdma_complete_counter(const struct qib_user_sdma_queue *pq)
  1163. {
  1164. return pq ? pq->sent_counter : 0;
  1165. }
  1166. u32 qib_user_sdma_inflight_counter(struct qib_user_sdma_queue *pq)
  1167. {
  1168. return pq ? pq->counter : 0;
  1169. }