qib_qp.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
  3. * All rights reserved.
  4. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/err.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/jhash.h>
  37. #include "qib.h"
  38. #define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE)
  39. #define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1)
  40. static inline unsigned mk_qpn(struct qib_qpn_table *qpt,
  41. struct qpn_map *map, unsigned off)
  42. {
  43. return (map - qpt->map) * BITS_PER_PAGE + off;
  44. }
  45. static inline unsigned find_next_offset(struct qib_qpn_table *qpt,
  46. struct qpn_map *map, unsigned off,
  47. unsigned n)
  48. {
  49. if (qpt->mask) {
  50. off++;
  51. if (((off & qpt->mask) >> 1) >= n)
  52. off = (off | qpt->mask) + 2;
  53. } else
  54. off = find_next_zero_bit(map->page, BITS_PER_PAGE, off);
  55. return off;
  56. }
  57. /*
  58. * Convert the AETH credit code into the number of credits.
  59. */
  60. static u32 credit_table[31] = {
  61. 0, /* 0 */
  62. 1, /* 1 */
  63. 2, /* 2 */
  64. 3, /* 3 */
  65. 4, /* 4 */
  66. 6, /* 5 */
  67. 8, /* 6 */
  68. 12, /* 7 */
  69. 16, /* 8 */
  70. 24, /* 9 */
  71. 32, /* A */
  72. 48, /* B */
  73. 64, /* C */
  74. 96, /* D */
  75. 128, /* E */
  76. 192, /* F */
  77. 256, /* 10 */
  78. 384, /* 11 */
  79. 512, /* 12 */
  80. 768, /* 13 */
  81. 1024, /* 14 */
  82. 1536, /* 15 */
  83. 2048, /* 16 */
  84. 3072, /* 17 */
  85. 4096, /* 18 */
  86. 6144, /* 19 */
  87. 8192, /* 1A */
  88. 12288, /* 1B */
  89. 16384, /* 1C */
  90. 24576, /* 1D */
  91. 32768 /* 1E */
  92. };
  93. static void get_map_page(struct qib_qpn_table *qpt, struct qpn_map *map)
  94. {
  95. unsigned long page = get_zeroed_page(GFP_KERNEL);
  96. /*
  97. * Free the page if someone raced with us installing it.
  98. */
  99. spin_lock(&qpt->lock);
  100. if (map->page)
  101. free_page(page);
  102. else
  103. map->page = (void *)page;
  104. spin_unlock(&qpt->lock);
  105. }
  106. /*
  107. * Allocate the next available QPN or
  108. * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
  109. */
  110. static int alloc_qpn(struct qib_devdata *dd, struct qib_qpn_table *qpt,
  111. enum ib_qp_type type, u8 port)
  112. {
  113. u32 i, offset, max_scan, qpn;
  114. struct qpn_map *map;
  115. u32 ret;
  116. if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
  117. unsigned n;
  118. ret = type == IB_QPT_GSI;
  119. n = 1 << (ret + 2 * (port - 1));
  120. spin_lock(&qpt->lock);
  121. if (qpt->flags & n)
  122. ret = -EINVAL;
  123. else
  124. qpt->flags |= n;
  125. spin_unlock(&qpt->lock);
  126. goto bail;
  127. }
  128. qpn = qpt->last + 2;
  129. if (qpn >= QPN_MAX)
  130. qpn = 2;
  131. if (qpt->mask && ((qpn & qpt->mask) >> 1) >= dd->n_krcv_queues)
  132. qpn = (qpn | qpt->mask) + 2;
  133. offset = qpn & BITS_PER_PAGE_MASK;
  134. map = &qpt->map[qpn / BITS_PER_PAGE];
  135. max_scan = qpt->nmaps - !offset;
  136. for (i = 0;;) {
  137. if (unlikely(!map->page)) {
  138. get_map_page(qpt, map);
  139. if (unlikely(!map->page))
  140. break;
  141. }
  142. do {
  143. if (!test_and_set_bit(offset, map->page)) {
  144. qpt->last = qpn;
  145. ret = qpn;
  146. goto bail;
  147. }
  148. offset = find_next_offset(qpt, map, offset,
  149. dd->n_krcv_queues);
  150. qpn = mk_qpn(qpt, map, offset);
  151. /*
  152. * This test differs from alloc_pidmap().
  153. * If find_next_offset() does find a zero
  154. * bit, we don't need to check for QPN
  155. * wrapping around past our starting QPN.
  156. * We just need to be sure we don't loop
  157. * forever.
  158. */
  159. } while (offset < BITS_PER_PAGE && qpn < QPN_MAX);
  160. /*
  161. * In order to keep the number of pages allocated to a
  162. * minimum, we scan the all existing pages before increasing
  163. * the size of the bitmap table.
  164. */
  165. if (++i > max_scan) {
  166. if (qpt->nmaps == QPNMAP_ENTRIES)
  167. break;
  168. map = &qpt->map[qpt->nmaps++];
  169. offset = 0;
  170. } else if (map < &qpt->map[qpt->nmaps]) {
  171. ++map;
  172. offset = 0;
  173. } else {
  174. map = &qpt->map[0];
  175. offset = 2;
  176. }
  177. qpn = mk_qpn(qpt, map, offset);
  178. }
  179. ret = -ENOMEM;
  180. bail:
  181. return ret;
  182. }
  183. static void free_qpn(struct qib_qpn_table *qpt, u32 qpn)
  184. {
  185. struct qpn_map *map;
  186. map = qpt->map + qpn / BITS_PER_PAGE;
  187. if (map->page)
  188. clear_bit(qpn & BITS_PER_PAGE_MASK, map->page);
  189. }
  190. static inline unsigned qpn_hash(struct qib_ibdev *dev, u32 qpn)
  191. {
  192. return jhash_1word(qpn, dev->qp_rnd) &
  193. (dev->qp_table_size - 1);
  194. }
  195. /*
  196. * Put the QP into the hash table.
  197. * The hash table holds a reference to the QP.
  198. */
  199. static void insert_qp(struct qib_ibdev *dev, struct qib_qp *qp)
  200. {
  201. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  202. unsigned long flags;
  203. unsigned n = qpn_hash(dev, qp->ibqp.qp_num);
  204. spin_lock_irqsave(&dev->qpt_lock, flags);
  205. atomic_inc(&qp->refcount);
  206. if (qp->ibqp.qp_num == 0)
  207. rcu_assign_pointer(ibp->qp0, qp);
  208. else if (qp->ibqp.qp_num == 1)
  209. rcu_assign_pointer(ibp->qp1, qp);
  210. else {
  211. qp->next = dev->qp_table[n];
  212. rcu_assign_pointer(dev->qp_table[n], qp);
  213. }
  214. spin_unlock_irqrestore(&dev->qpt_lock, flags);
  215. synchronize_rcu();
  216. }
  217. /*
  218. * Remove the QP from the table so it can't be found asynchronously by
  219. * the receive interrupt routine.
  220. */
  221. static void remove_qp(struct qib_ibdev *dev, struct qib_qp *qp)
  222. {
  223. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  224. unsigned n = qpn_hash(dev, qp->ibqp.qp_num);
  225. unsigned long flags;
  226. spin_lock_irqsave(&dev->qpt_lock, flags);
  227. if (ibp->qp0 == qp) {
  228. atomic_dec(&qp->refcount);
  229. rcu_assign_pointer(ibp->qp0, NULL);
  230. } else if (ibp->qp1 == qp) {
  231. atomic_dec(&qp->refcount);
  232. rcu_assign_pointer(ibp->qp1, NULL);
  233. } else {
  234. struct qib_qp *q, **qpp;
  235. qpp = &dev->qp_table[n];
  236. for (; (q = *qpp) != NULL; qpp = &q->next)
  237. if (q == qp) {
  238. atomic_dec(&qp->refcount);
  239. rcu_assign_pointer(*qpp, qp->next);
  240. qp->next = NULL;
  241. break;
  242. }
  243. }
  244. spin_unlock_irqrestore(&dev->qpt_lock, flags);
  245. synchronize_rcu();
  246. }
  247. /**
  248. * qib_free_all_qps - check for QPs still in use
  249. * @qpt: the QP table to empty
  250. *
  251. * There should not be any QPs still in use.
  252. * Free memory for table.
  253. */
  254. unsigned qib_free_all_qps(struct qib_devdata *dd)
  255. {
  256. struct qib_ibdev *dev = &dd->verbs_dev;
  257. unsigned long flags;
  258. struct qib_qp *qp;
  259. unsigned n, qp_inuse = 0;
  260. for (n = 0; n < dd->num_pports; n++) {
  261. struct qib_ibport *ibp = &dd->pport[n].ibport_data;
  262. if (!qib_mcast_tree_empty(ibp))
  263. qp_inuse++;
  264. rcu_read_lock();
  265. if (rcu_dereference(ibp->qp0))
  266. qp_inuse++;
  267. if (rcu_dereference(ibp->qp1))
  268. qp_inuse++;
  269. rcu_read_unlock();
  270. }
  271. spin_lock_irqsave(&dev->qpt_lock, flags);
  272. for (n = 0; n < dev->qp_table_size; n++) {
  273. qp = dev->qp_table[n];
  274. rcu_assign_pointer(dev->qp_table[n], NULL);
  275. for (; qp; qp = qp->next)
  276. qp_inuse++;
  277. }
  278. spin_unlock_irqrestore(&dev->qpt_lock, flags);
  279. synchronize_rcu();
  280. return qp_inuse;
  281. }
  282. /**
  283. * qib_lookup_qpn - return the QP with the given QPN
  284. * @qpt: the QP table
  285. * @qpn: the QP number to look up
  286. *
  287. * The caller is responsible for decrementing the QP reference count
  288. * when done.
  289. */
  290. struct qib_qp *qib_lookup_qpn(struct qib_ibport *ibp, u32 qpn)
  291. {
  292. struct qib_qp *qp = NULL;
  293. if (unlikely(qpn <= 1)) {
  294. rcu_read_lock();
  295. if (qpn == 0)
  296. qp = rcu_dereference(ibp->qp0);
  297. else
  298. qp = rcu_dereference(ibp->qp1);
  299. } else {
  300. struct qib_ibdev *dev = &ppd_from_ibp(ibp)->dd->verbs_dev;
  301. unsigned n = qpn_hash(dev, qpn);
  302. rcu_read_lock();
  303. for (qp = dev->qp_table[n]; rcu_dereference(qp); qp = qp->next)
  304. if (qp->ibqp.qp_num == qpn)
  305. break;
  306. }
  307. if (qp)
  308. if (unlikely(!atomic_inc_not_zero(&qp->refcount)))
  309. qp = NULL;
  310. rcu_read_unlock();
  311. return qp;
  312. }
  313. /**
  314. * qib_reset_qp - initialize the QP state to the reset state
  315. * @qp: the QP to reset
  316. * @type: the QP type
  317. */
  318. static void qib_reset_qp(struct qib_qp *qp, enum ib_qp_type type)
  319. {
  320. qp->remote_qpn = 0;
  321. qp->qkey = 0;
  322. qp->qp_access_flags = 0;
  323. atomic_set(&qp->s_dma_busy, 0);
  324. qp->s_flags &= QIB_S_SIGNAL_REQ_WR;
  325. qp->s_hdrwords = 0;
  326. qp->s_wqe = NULL;
  327. qp->s_draining = 0;
  328. qp->s_next_psn = 0;
  329. qp->s_last_psn = 0;
  330. qp->s_sending_psn = 0;
  331. qp->s_sending_hpsn = 0;
  332. qp->s_psn = 0;
  333. qp->r_psn = 0;
  334. qp->r_msn = 0;
  335. if (type == IB_QPT_RC) {
  336. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  337. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  338. } else {
  339. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  340. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  341. }
  342. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  343. qp->r_nak_state = 0;
  344. qp->r_aflags = 0;
  345. qp->r_flags = 0;
  346. qp->s_head = 0;
  347. qp->s_tail = 0;
  348. qp->s_cur = 0;
  349. qp->s_acked = 0;
  350. qp->s_last = 0;
  351. qp->s_ssn = 1;
  352. qp->s_lsn = 0;
  353. qp->s_mig_state = IB_MIG_MIGRATED;
  354. memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
  355. qp->r_head_ack_queue = 0;
  356. qp->s_tail_ack_queue = 0;
  357. qp->s_num_rd_atomic = 0;
  358. if (qp->r_rq.wq) {
  359. qp->r_rq.wq->head = 0;
  360. qp->r_rq.wq->tail = 0;
  361. }
  362. qp->r_sge.num_sge = 0;
  363. }
  364. static void clear_mr_refs(struct qib_qp *qp, int clr_sends)
  365. {
  366. unsigned n;
  367. if (test_and_clear_bit(QIB_R_REWIND_SGE, &qp->r_aflags))
  368. qib_put_ss(&qp->s_rdma_read_sge);
  369. qib_put_ss(&qp->r_sge);
  370. if (clr_sends) {
  371. while (qp->s_last != qp->s_head) {
  372. struct qib_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
  373. unsigned i;
  374. for (i = 0; i < wqe->wr.num_sge; i++) {
  375. struct qib_sge *sge = &wqe->sg_list[i];
  376. qib_put_mr(sge->mr);
  377. }
  378. if (qp->ibqp.qp_type == IB_QPT_UD ||
  379. qp->ibqp.qp_type == IB_QPT_SMI ||
  380. qp->ibqp.qp_type == IB_QPT_GSI)
  381. atomic_dec(&to_iah(wqe->wr.wr.ud.ah)->refcount);
  382. if (++qp->s_last >= qp->s_size)
  383. qp->s_last = 0;
  384. }
  385. if (qp->s_rdma_mr) {
  386. qib_put_mr(qp->s_rdma_mr);
  387. qp->s_rdma_mr = NULL;
  388. }
  389. }
  390. if (qp->ibqp.qp_type != IB_QPT_RC)
  391. return;
  392. for (n = 0; n < ARRAY_SIZE(qp->s_ack_queue); n++) {
  393. struct qib_ack_entry *e = &qp->s_ack_queue[n];
  394. if (e->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST &&
  395. e->rdma_sge.mr) {
  396. qib_put_mr(e->rdma_sge.mr);
  397. e->rdma_sge.mr = NULL;
  398. }
  399. }
  400. }
  401. /**
  402. * qib_error_qp - put a QP into the error state
  403. * @qp: the QP to put into the error state
  404. * @err: the receive completion error to signal if a RWQE is active
  405. *
  406. * Flushes both send and receive work queues.
  407. * Returns true if last WQE event should be generated.
  408. * The QP r_lock and s_lock should be held and interrupts disabled.
  409. * If we are already in error state, just return.
  410. */
  411. int qib_error_qp(struct qib_qp *qp, enum ib_wc_status err)
  412. {
  413. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  414. struct ib_wc wc;
  415. int ret = 0;
  416. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  417. goto bail;
  418. qp->state = IB_QPS_ERR;
  419. if (qp->s_flags & (QIB_S_TIMER | QIB_S_WAIT_RNR)) {
  420. qp->s_flags &= ~(QIB_S_TIMER | QIB_S_WAIT_RNR);
  421. del_timer(&qp->s_timer);
  422. }
  423. if (qp->s_flags & QIB_S_ANY_WAIT_SEND)
  424. qp->s_flags &= ~QIB_S_ANY_WAIT_SEND;
  425. spin_lock(&dev->pending_lock);
  426. if (!list_empty(&qp->iowait) && !(qp->s_flags & QIB_S_BUSY)) {
  427. qp->s_flags &= ~QIB_S_ANY_WAIT_IO;
  428. list_del_init(&qp->iowait);
  429. }
  430. spin_unlock(&dev->pending_lock);
  431. if (!(qp->s_flags & QIB_S_BUSY)) {
  432. qp->s_hdrwords = 0;
  433. if (qp->s_rdma_mr) {
  434. qib_put_mr(qp->s_rdma_mr);
  435. qp->s_rdma_mr = NULL;
  436. }
  437. if (qp->s_tx) {
  438. qib_put_txreq(qp->s_tx);
  439. qp->s_tx = NULL;
  440. }
  441. }
  442. /* Schedule the sending tasklet to drain the send work queue. */
  443. if (qp->s_last != qp->s_head)
  444. qib_schedule_send(qp);
  445. clear_mr_refs(qp, 0);
  446. memset(&wc, 0, sizeof(wc));
  447. wc.qp = &qp->ibqp;
  448. wc.opcode = IB_WC_RECV;
  449. if (test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags)) {
  450. wc.wr_id = qp->r_wr_id;
  451. wc.status = err;
  452. qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
  453. }
  454. wc.status = IB_WC_WR_FLUSH_ERR;
  455. if (qp->r_rq.wq) {
  456. struct qib_rwq *wq;
  457. u32 head;
  458. u32 tail;
  459. spin_lock(&qp->r_rq.lock);
  460. /* sanity check pointers before trusting them */
  461. wq = qp->r_rq.wq;
  462. head = wq->head;
  463. if (head >= qp->r_rq.size)
  464. head = 0;
  465. tail = wq->tail;
  466. if (tail >= qp->r_rq.size)
  467. tail = 0;
  468. while (tail != head) {
  469. wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  470. if (++tail >= qp->r_rq.size)
  471. tail = 0;
  472. qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
  473. }
  474. wq->tail = tail;
  475. spin_unlock(&qp->r_rq.lock);
  476. } else if (qp->ibqp.event_handler)
  477. ret = 1;
  478. bail:
  479. return ret;
  480. }
  481. /**
  482. * qib_modify_qp - modify the attributes of a queue pair
  483. * @ibqp: the queue pair who's attributes we're modifying
  484. * @attr: the new attributes
  485. * @attr_mask: the mask of attributes to modify
  486. * @udata: user data for libibverbs.so
  487. *
  488. * Returns 0 on success, otherwise returns an errno.
  489. */
  490. int qib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  491. int attr_mask, struct ib_udata *udata)
  492. {
  493. struct qib_ibdev *dev = to_idev(ibqp->device);
  494. struct qib_qp *qp = to_iqp(ibqp);
  495. enum ib_qp_state cur_state, new_state;
  496. struct ib_event ev;
  497. int lastwqe = 0;
  498. int mig = 0;
  499. int ret;
  500. u32 pmtu = 0; /* for gcc warning only */
  501. spin_lock_irq(&qp->r_lock);
  502. spin_lock(&qp->s_lock);
  503. cur_state = attr_mask & IB_QP_CUR_STATE ?
  504. attr->cur_qp_state : qp->state;
  505. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  506. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  507. attr_mask))
  508. goto inval;
  509. if (attr_mask & IB_QP_AV) {
  510. if (attr->ah_attr.dlid >= QIB_MULTICAST_LID_BASE)
  511. goto inval;
  512. if (qib_check_ah(qp->ibqp.device, &attr->ah_attr))
  513. goto inval;
  514. }
  515. if (attr_mask & IB_QP_ALT_PATH) {
  516. if (attr->alt_ah_attr.dlid >= QIB_MULTICAST_LID_BASE)
  517. goto inval;
  518. if (qib_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
  519. goto inval;
  520. if (attr->alt_pkey_index >= qib_get_npkeys(dd_from_dev(dev)))
  521. goto inval;
  522. }
  523. if (attr_mask & IB_QP_PKEY_INDEX)
  524. if (attr->pkey_index >= qib_get_npkeys(dd_from_dev(dev)))
  525. goto inval;
  526. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  527. if (attr->min_rnr_timer > 31)
  528. goto inval;
  529. if (attr_mask & IB_QP_PORT)
  530. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  531. qp->ibqp.qp_type == IB_QPT_GSI ||
  532. attr->port_num == 0 ||
  533. attr->port_num > ibqp->device->phys_port_cnt)
  534. goto inval;
  535. if (attr_mask & IB_QP_DEST_QPN)
  536. if (attr->dest_qp_num > QIB_QPN_MASK)
  537. goto inval;
  538. if (attr_mask & IB_QP_RETRY_CNT)
  539. if (attr->retry_cnt > 7)
  540. goto inval;
  541. if (attr_mask & IB_QP_RNR_RETRY)
  542. if (attr->rnr_retry > 7)
  543. goto inval;
  544. /*
  545. * Don't allow invalid path_mtu values. OK to set greater
  546. * than the active mtu (or even the max_cap, if we have tuned
  547. * that to a small mtu. We'll set qp->path_mtu
  548. * to the lesser of requested attribute mtu and active,
  549. * for packetizing messages.
  550. * Note that the QP port has to be set in INIT and MTU in RTR.
  551. */
  552. if (attr_mask & IB_QP_PATH_MTU) {
  553. struct qib_devdata *dd = dd_from_dev(dev);
  554. int mtu, pidx = qp->port_num - 1;
  555. mtu = ib_mtu_enum_to_int(attr->path_mtu);
  556. if (mtu == -1)
  557. goto inval;
  558. if (mtu > dd->pport[pidx].ibmtu) {
  559. switch (dd->pport[pidx].ibmtu) {
  560. case 4096:
  561. pmtu = IB_MTU_4096;
  562. break;
  563. case 2048:
  564. pmtu = IB_MTU_2048;
  565. break;
  566. case 1024:
  567. pmtu = IB_MTU_1024;
  568. break;
  569. case 512:
  570. pmtu = IB_MTU_512;
  571. break;
  572. case 256:
  573. pmtu = IB_MTU_256;
  574. break;
  575. default:
  576. pmtu = IB_MTU_2048;
  577. }
  578. } else
  579. pmtu = attr->path_mtu;
  580. }
  581. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  582. if (attr->path_mig_state == IB_MIG_REARM) {
  583. if (qp->s_mig_state == IB_MIG_ARMED)
  584. goto inval;
  585. if (new_state != IB_QPS_RTS)
  586. goto inval;
  587. } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
  588. if (qp->s_mig_state == IB_MIG_REARM)
  589. goto inval;
  590. if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
  591. goto inval;
  592. if (qp->s_mig_state == IB_MIG_ARMED)
  593. mig = 1;
  594. } else
  595. goto inval;
  596. }
  597. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  598. if (attr->max_dest_rd_atomic > QIB_MAX_RDMA_ATOMIC)
  599. goto inval;
  600. switch (new_state) {
  601. case IB_QPS_RESET:
  602. if (qp->state != IB_QPS_RESET) {
  603. qp->state = IB_QPS_RESET;
  604. spin_lock(&dev->pending_lock);
  605. if (!list_empty(&qp->iowait))
  606. list_del_init(&qp->iowait);
  607. spin_unlock(&dev->pending_lock);
  608. qp->s_flags &= ~(QIB_S_TIMER | QIB_S_ANY_WAIT);
  609. spin_unlock(&qp->s_lock);
  610. spin_unlock_irq(&qp->r_lock);
  611. /* Stop the sending work queue and retry timer */
  612. cancel_work_sync(&qp->s_work);
  613. del_timer_sync(&qp->s_timer);
  614. wait_event(qp->wait_dma, !atomic_read(&qp->s_dma_busy));
  615. if (qp->s_tx) {
  616. qib_put_txreq(qp->s_tx);
  617. qp->s_tx = NULL;
  618. }
  619. remove_qp(dev, qp);
  620. wait_event(qp->wait, !atomic_read(&qp->refcount));
  621. spin_lock_irq(&qp->r_lock);
  622. spin_lock(&qp->s_lock);
  623. clear_mr_refs(qp, 1);
  624. qib_reset_qp(qp, ibqp->qp_type);
  625. }
  626. break;
  627. case IB_QPS_RTR:
  628. /* Allow event to retrigger if QP set to RTR more than once */
  629. qp->r_flags &= ~QIB_R_COMM_EST;
  630. qp->state = new_state;
  631. break;
  632. case IB_QPS_SQD:
  633. qp->s_draining = qp->s_last != qp->s_cur;
  634. qp->state = new_state;
  635. break;
  636. case IB_QPS_SQE:
  637. if (qp->ibqp.qp_type == IB_QPT_RC)
  638. goto inval;
  639. qp->state = new_state;
  640. break;
  641. case IB_QPS_ERR:
  642. lastwqe = qib_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  643. break;
  644. default:
  645. qp->state = new_state;
  646. break;
  647. }
  648. if (attr_mask & IB_QP_PKEY_INDEX)
  649. qp->s_pkey_index = attr->pkey_index;
  650. if (attr_mask & IB_QP_PORT)
  651. qp->port_num = attr->port_num;
  652. if (attr_mask & IB_QP_DEST_QPN)
  653. qp->remote_qpn = attr->dest_qp_num;
  654. if (attr_mask & IB_QP_SQ_PSN) {
  655. qp->s_next_psn = attr->sq_psn & QIB_PSN_MASK;
  656. qp->s_psn = qp->s_next_psn;
  657. qp->s_sending_psn = qp->s_next_psn;
  658. qp->s_last_psn = qp->s_next_psn - 1;
  659. qp->s_sending_hpsn = qp->s_last_psn;
  660. }
  661. if (attr_mask & IB_QP_RQ_PSN)
  662. qp->r_psn = attr->rq_psn & QIB_PSN_MASK;
  663. if (attr_mask & IB_QP_ACCESS_FLAGS)
  664. qp->qp_access_flags = attr->qp_access_flags;
  665. if (attr_mask & IB_QP_AV) {
  666. qp->remote_ah_attr = attr->ah_attr;
  667. qp->s_srate = attr->ah_attr.static_rate;
  668. }
  669. if (attr_mask & IB_QP_ALT_PATH) {
  670. qp->alt_ah_attr = attr->alt_ah_attr;
  671. qp->s_alt_pkey_index = attr->alt_pkey_index;
  672. }
  673. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  674. qp->s_mig_state = attr->path_mig_state;
  675. if (mig) {
  676. qp->remote_ah_attr = qp->alt_ah_attr;
  677. qp->port_num = qp->alt_ah_attr.port_num;
  678. qp->s_pkey_index = qp->s_alt_pkey_index;
  679. }
  680. }
  681. if (attr_mask & IB_QP_PATH_MTU) {
  682. qp->path_mtu = pmtu;
  683. qp->pmtu = ib_mtu_enum_to_int(pmtu);
  684. }
  685. if (attr_mask & IB_QP_RETRY_CNT) {
  686. qp->s_retry_cnt = attr->retry_cnt;
  687. qp->s_retry = attr->retry_cnt;
  688. }
  689. if (attr_mask & IB_QP_RNR_RETRY) {
  690. qp->s_rnr_retry_cnt = attr->rnr_retry;
  691. qp->s_rnr_retry = attr->rnr_retry;
  692. }
  693. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  694. qp->r_min_rnr_timer = attr->min_rnr_timer;
  695. if (attr_mask & IB_QP_TIMEOUT) {
  696. qp->timeout = attr->timeout;
  697. qp->timeout_jiffies =
  698. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  699. 1000UL);
  700. }
  701. if (attr_mask & IB_QP_QKEY)
  702. qp->qkey = attr->qkey;
  703. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  704. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  705. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  706. qp->s_max_rd_atomic = attr->max_rd_atomic;
  707. spin_unlock(&qp->s_lock);
  708. spin_unlock_irq(&qp->r_lock);
  709. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  710. insert_qp(dev, qp);
  711. if (lastwqe) {
  712. ev.device = qp->ibqp.device;
  713. ev.element.qp = &qp->ibqp;
  714. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  715. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  716. }
  717. if (mig) {
  718. ev.device = qp->ibqp.device;
  719. ev.element.qp = &qp->ibqp;
  720. ev.event = IB_EVENT_PATH_MIG;
  721. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  722. }
  723. ret = 0;
  724. goto bail;
  725. inval:
  726. spin_unlock(&qp->s_lock);
  727. spin_unlock_irq(&qp->r_lock);
  728. ret = -EINVAL;
  729. bail:
  730. return ret;
  731. }
  732. int qib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  733. int attr_mask, struct ib_qp_init_attr *init_attr)
  734. {
  735. struct qib_qp *qp = to_iqp(ibqp);
  736. attr->qp_state = qp->state;
  737. attr->cur_qp_state = attr->qp_state;
  738. attr->path_mtu = qp->path_mtu;
  739. attr->path_mig_state = qp->s_mig_state;
  740. attr->qkey = qp->qkey;
  741. attr->rq_psn = qp->r_psn & QIB_PSN_MASK;
  742. attr->sq_psn = qp->s_next_psn & QIB_PSN_MASK;
  743. attr->dest_qp_num = qp->remote_qpn;
  744. attr->qp_access_flags = qp->qp_access_flags;
  745. attr->cap.max_send_wr = qp->s_size - 1;
  746. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  747. attr->cap.max_send_sge = qp->s_max_sge;
  748. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  749. attr->cap.max_inline_data = 0;
  750. attr->ah_attr = qp->remote_ah_attr;
  751. attr->alt_ah_attr = qp->alt_ah_attr;
  752. attr->pkey_index = qp->s_pkey_index;
  753. attr->alt_pkey_index = qp->s_alt_pkey_index;
  754. attr->en_sqd_async_notify = 0;
  755. attr->sq_draining = qp->s_draining;
  756. attr->max_rd_atomic = qp->s_max_rd_atomic;
  757. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  758. attr->min_rnr_timer = qp->r_min_rnr_timer;
  759. attr->port_num = qp->port_num;
  760. attr->timeout = qp->timeout;
  761. attr->retry_cnt = qp->s_retry_cnt;
  762. attr->rnr_retry = qp->s_rnr_retry_cnt;
  763. attr->alt_port_num = qp->alt_ah_attr.port_num;
  764. attr->alt_timeout = qp->alt_timeout;
  765. init_attr->event_handler = qp->ibqp.event_handler;
  766. init_attr->qp_context = qp->ibqp.qp_context;
  767. init_attr->send_cq = qp->ibqp.send_cq;
  768. init_attr->recv_cq = qp->ibqp.recv_cq;
  769. init_attr->srq = qp->ibqp.srq;
  770. init_attr->cap = attr->cap;
  771. if (qp->s_flags & QIB_S_SIGNAL_REQ_WR)
  772. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  773. else
  774. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  775. init_attr->qp_type = qp->ibqp.qp_type;
  776. init_attr->port_num = qp->port_num;
  777. return 0;
  778. }
  779. /**
  780. * qib_compute_aeth - compute the AETH (syndrome + MSN)
  781. * @qp: the queue pair to compute the AETH for
  782. *
  783. * Returns the AETH.
  784. */
  785. __be32 qib_compute_aeth(struct qib_qp *qp)
  786. {
  787. u32 aeth = qp->r_msn & QIB_MSN_MASK;
  788. if (qp->ibqp.srq) {
  789. /*
  790. * Shared receive queues don't generate credits.
  791. * Set the credit field to the invalid value.
  792. */
  793. aeth |= QIB_AETH_CREDIT_INVAL << QIB_AETH_CREDIT_SHIFT;
  794. } else {
  795. u32 min, max, x;
  796. u32 credits;
  797. struct qib_rwq *wq = qp->r_rq.wq;
  798. u32 head;
  799. u32 tail;
  800. /* sanity check pointers before trusting them */
  801. head = wq->head;
  802. if (head >= qp->r_rq.size)
  803. head = 0;
  804. tail = wq->tail;
  805. if (tail >= qp->r_rq.size)
  806. tail = 0;
  807. /*
  808. * Compute the number of credits available (RWQEs).
  809. * XXX Not holding the r_rq.lock here so there is a small
  810. * chance that the pair of reads are not atomic.
  811. */
  812. credits = head - tail;
  813. if ((int)credits < 0)
  814. credits += qp->r_rq.size;
  815. /*
  816. * Binary search the credit table to find the code to
  817. * use.
  818. */
  819. min = 0;
  820. max = 31;
  821. for (;;) {
  822. x = (min + max) / 2;
  823. if (credit_table[x] == credits)
  824. break;
  825. if (credit_table[x] > credits)
  826. max = x;
  827. else if (min == x)
  828. break;
  829. else
  830. min = x;
  831. }
  832. aeth |= x << QIB_AETH_CREDIT_SHIFT;
  833. }
  834. return cpu_to_be32(aeth);
  835. }
  836. /**
  837. * qib_create_qp - create a queue pair for a device
  838. * @ibpd: the protection domain who's device we create the queue pair for
  839. * @init_attr: the attributes of the queue pair
  840. * @udata: user data for libibverbs.so
  841. *
  842. * Returns the queue pair on success, otherwise returns an errno.
  843. *
  844. * Called by the ib_create_qp() core verbs function.
  845. */
  846. struct ib_qp *qib_create_qp(struct ib_pd *ibpd,
  847. struct ib_qp_init_attr *init_attr,
  848. struct ib_udata *udata)
  849. {
  850. struct qib_qp *qp;
  851. int err;
  852. struct qib_swqe *swq = NULL;
  853. struct qib_ibdev *dev;
  854. struct qib_devdata *dd;
  855. size_t sz;
  856. size_t sg_list_sz;
  857. struct ib_qp *ret;
  858. if (init_attr->cap.max_send_sge > ib_qib_max_sges ||
  859. init_attr->cap.max_send_wr > ib_qib_max_qp_wrs) {
  860. ret = ERR_PTR(-EINVAL);
  861. goto bail;
  862. }
  863. /* Check receive queue parameters if no SRQ is specified. */
  864. if (!init_attr->srq) {
  865. if (init_attr->cap.max_recv_sge > ib_qib_max_sges ||
  866. init_attr->cap.max_recv_wr > ib_qib_max_qp_wrs) {
  867. ret = ERR_PTR(-EINVAL);
  868. goto bail;
  869. }
  870. if (init_attr->cap.max_send_sge +
  871. init_attr->cap.max_send_wr +
  872. init_attr->cap.max_recv_sge +
  873. init_attr->cap.max_recv_wr == 0) {
  874. ret = ERR_PTR(-EINVAL);
  875. goto bail;
  876. }
  877. }
  878. switch (init_attr->qp_type) {
  879. case IB_QPT_SMI:
  880. case IB_QPT_GSI:
  881. if (init_attr->port_num == 0 ||
  882. init_attr->port_num > ibpd->device->phys_port_cnt) {
  883. ret = ERR_PTR(-EINVAL);
  884. goto bail;
  885. }
  886. case IB_QPT_UC:
  887. case IB_QPT_RC:
  888. case IB_QPT_UD:
  889. sz = sizeof(struct qib_sge) *
  890. init_attr->cap.max_send_sge +
  891. sizeof(struct qib_swqe);
  892. swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
  893. if (swq == NULL) {
  894. ret = ERR_PTR(-ENOMEM);
  895. goto bail;
  896. }
  897. sz = sizeof(*qp);
  898. sg_list_sz = 0;
  899. if (init_attr->srq) {
  900. struct qib_srq *srq = to_isrq(init_attr->srq);
  901. if (srq->rq.max_sge > 1)
  902. sg_list_sz = sizeof(*qp->r_sg_list) *
  903. (srq->rq.max_sge - 1);
  904. } else if (init_attr->cap.max_recv_sge > 1)
  905. sg_list_sz = sizeof(*qp->r_sg_list) *
  906. (init_attr->cap.max_recv_sge - 1);
  907. qp = kzalloc(sz + sg_list_sz, GFP_KERNEL);
  908. if (!qp) {
  909. ret = ERR_PTR(-ENOMEM);
  910. goto bail_swq;
  911. }
  912. RCU_INIT_POINTER(qp->next, NULL);
  913. qp->s_hdr = kzalloc(sizeof(*qp->s_hdr), GFP_KERNEL);
  914. if (!qp->s_hdr) {
  915. ret = ERR_PTR(-ENOMEM);
  916. goto bail_qp;
  917. }
  918. qp->timeout_jiffies =
  919. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  920. 1000UL);
  921. if (init_attr->srq)
  922. sz = 0;
  923. else {
  924. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  925. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  926. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  927. sizeof(struct qib_rwqe);
  928. qp->r_rq.wq = vmalloc_user(sizeof(struct qib_rwq) +
  929. qp->r_rq.size * sz);
  930. if (!qp->r_rq.wq) {
  931. ret = ERR_PTR(-ENOMEM);
  932. goto bail_qp;
  933. }
  934. }
  935. /*
  936. * ib_create_qp() will initialize qp->ibqp
  937. * except for qp->ibqp.qp_num.
  938. */
  939. spin_lock_init(&qp->r_lock);
  940. spin_lock_init(&qp->s_lock);
  941. spin_lock_init(&qp->r_rq.lock);
  942. atomic_set(&qp->refcount, 0);
  943. init_waitqueue_head(&qp->wait);
  944. init_waitqueue_head(&qp->wait_dma);
  945. init_timer(&qp->s_timer);
  946. qp->s_timer.data = (unsigned long)qp;
  947. INIT_WORK(&qp->s_work, qib_do_send);
  948. INIT_LIST_HEAD(&qp->iowait);
  949. INIT_LIST_HEAD(&qp->rspwait);
  950. qp->state = IB_QPS_RESET;
  951. qp->s_wq = swq;
  952. qp->s_size = init_attr->cap.max_send_wr + 1;
  953. qp->s_max_sge = init_attr->cap.max_send_sge;
  954. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  955. qp->s_flags = QIB_S_SIGNAL_REQ_WR;
  956. dev = to_idev(ibpd->device);
  957. dd = dd_from_dev(dev);
  958. err = alloc_qpn(dd, &dev->qpn_table, init_attr->qp_type,
  959. init_attr->port_num);
  960. if (err < 0) {
  961. ret = ERR_PTR(err);
  962. vfree(qp->r_rq.wq);
  963. goto bail_qp;
  964. }
  965. qp->ibqp.qp_num = err;
  966. qp->port_num = init_attr->port_num;
  967. qib_reset_qp(qp, init_attr->qp_type);
  968. break;
  969. default:
  970. /* Don't support raw QPs */
  971. ret = ERR_PTR(-ENOSYS);
  972. goto bail;
  973. }
  974. init_attr->cap.max_inline_data = 0;
  975. /*
  976. * Return the address of the RWQ as the offset to mmap.
  977. * See qib_mmap() for details.
  978. */
  979. if (udata && udata->outlen >= sizeof(__u64)) {
  980. if (!qp->r_rq.wq) {
  981. __u64 offset = 0;
  982. err = ib_copy_to_udata(udata, &offset,
  983. sizeof(offset));
  984. if (err) {
  985. ret = ERR_PTR(err);
  986. goto bail_ip;
  987. }
  988. } else {
  989. u32 s = sizeof(struct qib_rwq) + qp->r_rq.size * sz;
  990. qp->ip = qib_create_mmap_info(dev, s,
  991. ibpd->uobject->context,
  992. qp->r_rq.wq);
  993. if (!qp->ip) {
  994. ret = ERR_PTR(-ENOMEM);
  995. goto bail_ip;
  996. }
  997. err = ib_copy_to_udata(udata, &(qp->ip->offset),
  998. sizeof(qp->ip->offset));
  999. if (err) {
  1000. ret = ERR_PTR(err);
  1001. goto bail_ip;
  1002. }
  1003. }
  1004. }
  1005. spin_lock(&dev->n_qps_lock);
  1006. if (dev->n_qps_allocated == ib_qib_max_qps) {
  1007. spin_unlock(&dev->n_qps_lock);
  1008. ret = ERR_PTR(-ENOMEM);
  1009. goto bail_ip;
  1010. }
  1011. dev->n_qps_allocated++;
  1012. spin_unlock(&dev->n_qps_lock);
  1013. if (qp->ip) {
  1014. spin_lock_irq(&dev->pending_lock);
  1015. list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
  1016. spin_unlock_irq(&dev->pending_lock);
  1017. }
  1018. ret = &qp->ibqp;
  1019. goto bail;
  1020. bail_ip:
  1021. if (qp->ip)
  1022. kref_put(&qp->ip->ref, qib_release_mmap_info);
  1023. else
  1024. vfree(qp->r_rq.wq);
  1025. free_qpn(&dev->qpn_table, qp->ibqp.qp_num);
  1026. bail_qp:
  1027. kfree(qp->s_hdr);
  1028. kfree(qp);
  1029. bail_swq:
  1030. vfree(swq);
  1031. bail:
  1032. return ret;
  1033. }
  1034. /**
  1035. * qib_destroy_qp - destroy a queue pair
  1036. * @ibqp: the queue pair to destroy
  1037. *
  1038. * Returns 0 on success.
  1039. *
  1040. * Note that this can be called while the QP is actively sending or
  1041. * receiving!
  1042. */
  1043. int qib_destroy_qp(struct ib_qp *ibqp)
  1044. {
  1045. struct qib_qp *qp = to_iqp(ibqp);
  1046. struct qib_ibdev *dev = to_idev(ibqp->device);
  1047. /* Make sure HW and driver activity is stopped. */
  1048. spin_lock_irq(&qp->s_lock);
  1049. if (qp->state != IB_QPS_RESET) {
  1050. qp->state = IB_QPS_RESET;
  1051. spin_lock(&dev->pending_lock);
  1052. if (!list_empty(&qp->iowait))
  1053. list_del_init(&qp->iowait);
  1054. spin_unlock(&dev->pending_lock);
  1055. qp->s_flags &= ~(QIB_S_TIMER | QIB_S_ANY_WAIT);
  1056. spin_unlock_irq(&qp->s_lock);
  1057. cancel_work_sync(&qp->s_work);
  1058. del_timer_sync(&qp->s_timer);
  1059. wait_event(qp->wait_dma, !atomic_read(&qp->s_dma_busy));
  1060. if (qp->s_tx) {
  1061. qib_put_txreq(qp->s_tx);
  1062. qp->s_tx = NULL;
  1063. }
  1064. remove_qp(dev, qp);
  1065. wait_event(qp->wait, !atomic_read(&qp->refcount));
  1066. clear_mr_refs(qp, 1);
  1067. } else
  1068. spin_unlock_irq(&qp->s_lock);
  1069. /* all user's cleaned up, mark it available */
  1070. free_qpn(&dev->qpn_table, qp->ibqp.qp_num);
  1071. spin_lock(&dev->n_qps_lock);
  1072. dev->n_qps_allocated--;
  1073. spin_unlock(&dev->n_qps_lock);
  1074. if (qp->ip)
  1075. kref_put(&qp->ip->ref, qib_release_mmap_info);
  1076. else
  1077. vfree(qp->r_rq.wq);
  1078. vfree(qp->s_wq);
  1079. kfree(qp->s_hdr);
  1080. kfree(qp);
  1081. return 0;
  1082. }
  1083. /**
  1084. * qib_init_qpn_table - initialize the QP number table for a device
  1085. * @qpt: the QPN table
  1086. */
  1087. void qib_init_qpn_table(struct qib_devdata *dd, struct qib_qpn_table *qpt)
  1088. {
  1089. spin_lock_init(&qpt->lock);
  1090. qpt->last = 1; /* start with QPN 2 */
  1091. qpt->nmaps = 1;
  1092. qpt->mask = dd->qpn_mask;
  1093. }
  1094. /**
  1095. * qib_free_qpn_table - free the QP number table for a device
  1096. * @qpt: the QPN table
  1097. */
  1098. void qib_free_qpn_table(struct qib_qpn_table *qpt)
  1099. {
  1100. int i;
  1101. for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
  1102. if (qpt->map[i].page)
  1103. free_page((unsigned long) qpt->map[i].page);
  1104. }
  1105. /**
  1106. * qib_get_credit - flush the send work queue of a QP
  1107. * @qp: the qp who's send work queue to flush
  1108. * @aeth: the Acknowledge Extended Transport Header
  1109. *
  1110. * The QP s_lock should be held.
  1111. */
  1112. void qib_get_credit(struct qib_qp *qp, u32 aeth)
  1113. {
  1114. u32 credit = (aeth >> QIB_AETH_CREDIT_SHIFT) & QIB_AETH_CREDIT_MASK;
  1115. /*
  1116. * If the credit is invalid, we can send
  1117. * as many packets as we like. Otherwise, we have to
  1118. * honor the credit field.
  1119. */
  1120. if (credit == QIB_AETH_CREDIT_INVAL) {
  1121. if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT)) {
  1122. qp->s_flags |= QIB_S_UNLIMITED_CREDIT;
  1123. if (qp->s_flags & QIB_S_WAIT_SSN_CREDIT) {
  1124. qp->s_flags &= ~QIB_S_WAIT_SSN_CREDIT;
  1125. qib_schedule_send(qp);
  1126. }
  1127. }
  1128. } else if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT)) {
  1129. /* Compute new LSN (i.e., MSN + credit) */
  1130. credit = (aeth + credit_table[credit]) & QIB_MSN_MASK;
  1131. if (qib_cmp24(credit, qp->s_lsn) > 0) {
  1132. qp->s_lsn = credit;
  1133. if (qp->s_flags & QIB_S_WAIT_SSN_CREDIT) {
  1134. qp->s_flags &= ~QIB_S_WAIT_SSN_CREDIT;
  1135. qib_schedule_send(qp);
  1136. }
  1137. }
  1138. }
  1139. }