qib_qp.c 34 KB

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