ipath_qp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. All rights reserved.
  3. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/err.h>
  34. #include <linux/vmalloc.h>
  35. #include "ipath_verbs.h"
  36. #include "ipath_common.h"
  37. #define BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE)
  38. #define BITS_PER_PAGE_MASK (BITS_PER_PAGE-1)
  39. #define mk_qpn(qpt, map, off) (((map) - (qpt)->map) * BITS_PER_PAGE + \
  40. (off))
  41. #define find_next_offset(map, off) find_next_zero_bit((map)->page, \
  42. BITS_PER_PAGE, off)
  43. #define TRANS_INVALID 0
  44. #define TRANS_ANY2RST 1
  45. #define TRANS_RST2INIT 2
  46. #define TRANS_INIT2INIT 3
  47. #define TRANS_INIT2RTR 4
  48. #define TRANS_RTR2RTS 5
  49. #define TRANS_RTS2RTS 6
  50. #define TRANS_SQERR2RTS 7
  51. #define TRANS_ANY2ERR 8
  52. #define TRANS_RTS2SQD 9 /* XXX Wait for expected ACKs & signal event */
  53. #define TRANS_SQD2SQD 10 /* error if not drained & parameter change */
  54. #define TRANS_SQD2RTS 11 /* error if not drained */
  55. /*
  56. * Convert the AETH credit code into the number of credits.
  57. */
  58. static u32 credit_table[31] = {
  59. 0, /* 0 */
  60. 1, /* 1 */
  61. 2, /* 2 */
  62. 3, /* 3 */
  63. 4, /* 4 */
  64. 6, /* 5 */
  65. 8, /* 6 */
  66. 12, /* 7 */
  67. 16, /* 8 */
  68. 24, /* 9 */
  69. 32, /* A */
  70. 48, /* B */
  71. 64, /* C */
  72. 96, /* D */
  73. 128, /* E */
  74. 192, /* F */
  75. 256, /* 10 */
  76. 384, /* 11 */
  77. 512, /* 12 */
  78. 768, /* 13 */
  79. 1024, /* 14 */
  80. 1536, /* 15 */
  81. 2048, /* 16 */
  82. 3072, /* 17 */
  83. 4096, /* 18 */
  84. 6144, /* 19 */
  85. 8192, /* 1A */
  86. 12288, /* 1B */
  87. 16384, /* 1C */
  88. 24576, /* 1D */
  89. 32768 /* 1E */
  90. };
  91. static u32 alloc_qpn(struct ipath_qp_table *qpt)
  92. {
  93. u32 i, offset, max_scan, qpn;
  94. struct qpn_map *map;
  95. u32 ret;
  96. qpn = qpt->last + 1;
  97. if (qpn >= QPN_MAX)
  98. qpn = 2;
  99. offset = qpn & BITS_PER_PAGE_MASK;
  100. map = &qpt->map[qpn / BITS_PER_PAGE];
  101. max_scan = qpt->nmaps - !offset;
  102. for (i = 0;;) {
  103. if (unlikely(!map->page)) {
  104. unsigned long page = get_zeroed_page(GFP_KERNEL);
  105. unsigned long flags;
  106. /*
  107. * Free the page if someone raced with us
  108. * installing it:
  109. */
  110. spin_lock_irqsave(&qpt->lock, flags);
  111. if (map->page)
  112. free_page(page);
  113. else
  114. map->page = (void *)page;
  115. spin_unlock_irqrestore(&qpt->lock, flags);
  116. if (unlikely(!map->page))
  117. break;
  118. }
  119. if (likely(atomic_read(&map->n_free))) {
  120. do {
  121. if (!test_and_set_bit(offset, map->page)) {
  122. atomic_dec(&map->n_free);
  123. qpt->last = qpn;
  124. ret = qpn;
  125. goto bail;
  126. }
  127. offset = find_next_offset(map, offset);
  128. qpn = mk_qpn(qpt, map, offset);
  129. /*
  130. * This test differs from alloc_pidmap().
  131. * If find_next_offset() does find a zero
  132. * bit, we don't need to check for QPN
  133. * wrapping around past our starting QPN.
  134. * We just need to be sure we don't loop
  135. * forever.
  136. */
  137. } while (offset < BITS_PER_PAGE && qpn < QPN_MAX);
  138. }
  139. /*
  140. * In order to keep the number of pages allocated to a
  141. * minimum, we scan the all existing pages before increasing
  142. * the size of the bitmap table.
  143. */
  144. if (++i > max_scan) {
  145. if (qpt->nmaps == QPNMAP_ENTRIES)
  146. break;
  147. map = &qpt->map[qpt->nmaps++];
  148. offset = 0;
  149. } else if (map < &qpt->map[qpt->nmaps]) {
  150. ++map;
  151. offset = 0;
  152. } else {
  153. map = &qpt->map[0];
  154. offset = 2;
  155. }
  156. qpn = mk_qpn(qpt, map, offset);
  157. }
  158. ret = 0;
  159. bail:
  160. return ret;
  161. }
  162. static void free_qpn(struct ipath_qp_table *qpt, u32 qpn)
  163. {
  164. struct qpn_map *map;
  165. map = qpt->map + qpn / BITS_PER_PAGE;
  166. if (map->page)
  167. clear_bit(qpn & BITS_PER_PAGE_MASK, map->page);
  168. atomic_inc(&map->n_free);
  169. }
  170. /**
  171. * ipath_alloc_qpn - allocate a QP number
  172. * @qpt: the QP table
  173. * @qp: the QP
  174. * @type: the QP type (IB_QPT_SMI and IB_QPT_GSI are special)
  175. *
  176. * Allocate the next available QPN and put the QP into the hash table.
  177. * The hash table holds a reference to the QP.
  178. */
  179. static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp,
  180. enum ib_qp_type type)
  181. {
  182. unsigned long flags;
  183. u32 qpn;
  184. int ret;
  185. if (type == IB_QPT_SMI)
  186. qpn = 0;
  187. else if (type == IB_QPT_GSI)
  188. qpn = 1;
  189. else {
  190. /* Allocate the next available QPN */
  191. qpn = alloc_qpn(qpt);
  192. if (qpn == 0) {
  193. ret = -ENOMEM;
  194. goto bail;
  195. }
  196. }
  197. qp->ibqp.qp_num = qpn;
  198. /* Add the QP to the hash table. */
  199. spin_lock_irqsave(&qpt->lock, flags);
  200. qpn %= qpt->max;
  201. qp->next = qpt->table[qpn];
  202. qpt->table[qpn] = qp;
  203. atomic_inc(&qp->refcount);
  204. spin_unlock_irqrestore(&qpt->lock, flags);
  205. ret = 0;
  206. bail:
  207. return ret;
  208. }
  209. /**
  210. * ipath_free_qp - remove a QP from the QP table
  211. * @qpt: the QP table
  212. * @qp: the QP to remove
  213. *
  214. * Remove the QP from the table so it can't be found asynchronously by
  215. * the receive interrupt routine.
  216. */
  217. static void ipath_free_qp(struct ipath_qp_table *qpt, struct ipath_qp *qp)
  218. {
  219. struct ipath_qp *q, **qpp;
  220. unsigned long flags;
  221. int fnd = 0;
  222. spin_lock_irqsave(&qpt->lock, flags);
  223. /* Remove QP from the hash table. */
  224. qpp = &qpt->table[qp->ibqp.qp_num % qpt->max];
  225. for (; (q = *qpp) != NULL; qpp = &q->next) {
  226. if (q == qp) {
  227. *qpp = qp->next;
  228. qp->next = NULL;
  229. atomic_dec(&qp->refcount);
  230. fnd = 1;
  231. break;
  232. }
  233. }
  234. spin_unlock_irqrestore(&qpt->lock, flags);
  235. if (!fnd)
  236. return;
  237. /* If QPN is not reserved, mark QPN free in the bitmap. */
  238. if (qp->ibqp.qp_num > 1)
  239. free_qpn(qpt, qp->ibqp.qp_num);
  240. wait_event(qp->wait, !atomic_read(&qp->refcount));
  241. }
  242. /**
  243. * ipath_free_all_qps - remove all QPs from the table
  244. * @qpt: the QP table to empty
  245. */
  246. void ipath_free_all_qps(struct ipath_qp_table *qpt)
  247. {
  248. unsigned long flags;
  249. struct ipath_qp *qp, *nqp;
  250. u32 n;
  251. for (n = 0; n < qpt->max; n++) {
  252. spin_lock_irqsave(&qpt->lock, flags);
  253. qp = qpt->table[n];
  254. qpt->table[n] = NULL;
  255. spin_unlock_irqrestore(&qpt->lock, flags);
  256. while (qp) {
  257. nqp = qp->next;
  258. if (qp->ibqp.qp_num > 1)
  259. free_qpn(qpt, qp->ibqp.qp_num);
  260. if (!atomic_dec_and_test(&qp->refcount) ||
  261. !ipath_destroy_qp(&qp->ibqp))
  262. _VERBS_INFO("QP memory leak!\n");
  263. qp = nqp;
  264. }
  265. }
  266. for (n = 0; n < ARRAY_SIZE(qpt->map); n++) {
  267. if (qpt->map[n].page)
  268. free_page((unsigned long)qpt->map[n].page);
  269. }
  270. }
  271. /**
  272. * ipath_lookup_qpn - return the QP with the given QPN
  273. * @qpt: the QP table
  274. * @qpn: the QP number to look up
  275. *
  276. * The caller is responsible for decrementing the QP reference count
  277. * when done.
  278. */
  279. struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn)
  280. {
  281. unsigned long flags;
  282. struct ipath_qp *qp;
  283. spin_lock_irqsave(&qpt->lock, flags);
  284. for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) {
  285. if (qp->ibqp.qp_num == qpn) {
  286. atomic_inc(&qp->refcount);
  287. break;
  288. }
  289. }
  290. spin_unlock_irqrestore(&qpt->lock, flags);
  291. return qp;
  292. }
  293. /**
  294. * ipath_reset_qp - initialize the QP state to the reset state
  295. * @qp: the QP to reset
  296. */
  297. static void ipath_reset_qp(struct ipath_qp *qp)
  298. {
  299. qp->remote_qpn = 0;
  300. qp->qkey = 0;
  301. qp->qp_access_flags = 0;
  302. clear_bit(IPATH_S_BUSY, &qp->s_flags);
  303. qp->s_hdrwords = 0;
  304. qp->s_psn = 0;
  305. qp->r_psn = 0;
  306. qp->r_msn = 0;
  307. if (qp->ibqp.qp_type == IB_QPT_RC) {
  308. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  309. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  310. } else {
  311. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  312. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  313. }
  314. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  315. qp->r_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  316. qp->r_nak_state = 0;
  317. qp->s_rnr_timeout = 0;
  318. qp->s_head = 0;
  319. qp->s_tail = 0;
  320. qp->s_cur = 0;
  321. qp->s_last = 0;
  322. qp->s_ssn = 1;
  323. qp->s_lsn = 0;
  324. qp->r_rq.head = 0;
  325. qp->r_rq.tail = 0;
  326. qp->r_reuse_sge = 0;
  327. }
  328. /**
  329. * ipath_error_qp - put a QP into an error state
  330. * @qp: the QP to put into an error state
  331. *
  332. * Flushes both send and receive work queues.
  333. * QP s_lock should be held and interrupts disabled.
  334. */
  335. void ipath_error_qp(struct ipath_qp *qp)
  336. {
  337. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  338. struct ib_wc wc;
  339. _VERBS_INFO("QP%d/%d in error state\n",
  340. qp->ibqp.qp_num, qp->remote_qpn);
  341. spin_lock(&dev->pending_lock);
  342. /* XXX What if its already removed by the timeout code? */
  343. if (!list_empty(&qp->timerwait))
  344. list_del_init(&qp->timerwait);
  345. if (!list_empty(&qp->piowait))
  346. list_del_init(&qp->piowait);
  347. spin_unlock(&dev->pending_lock);
  348. wc.status = IB_WC_WR_FLUSH_ERR;
  349. wc.vendor_err = 0;
  350. wc.byte_len = 0;
  351. wc.imm_data = 0;
  352. wc.qp_num = qp->ibqp.qp_num;
  353. wc.src_qp = 0;
  354. wc.wc_flags = 0;
  355. wc.pkey_index = 0;
  356. wc.slid = 0;
  357. wc.sl = 0;
  358. wc.dlid_path_bits = 0;
  359. wc.port_num = 0;
  360. while (qp->s_last != qp->s_head) {
  361. struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
  362. wc.wr_id = wqe->wr.wr_id;
  363. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  364. if (++qp->s_last >= qp->s_size)
  365. qp->s_last = 0;
  366. ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
  367. }
  368. qp->s_cur = qp->s_tail = qp->s_head;
  369. qp->s_hdrwords = 0;
  370. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  371. wc.opcode = IB_WC_RECV;
  372. spin_lock(&qp->r_rq.lock);
  373. while (qp->r_rq.tail != qp->r_rq.head) {
  374. wc.wr_id = get_rwqe_ptr(&qp->r_rq, qp->r_rq.tail)->wr_id;
  375. if (++qp->r_rq.tail >= qp->r_rq.size)
  376. qp->r_rq.tail = 0;
  377. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
  378. }
  379. spin_unlock(&qp->r_rq.lock);
  380. }
  381. /**
  382. * ipath_modify_qp - modify the attributes of a queue pair
  383. * @ibqp: the queue pair who's attributes we're modifying
  384. * @attr: the new attributes
  385. * @attr_mask: the mask of attributes to modify
  386. *
  387. * Returns 0 on success, otherwise returns an errno.
  388. */
  389. int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  390. int attr_mask)
  391. {
  392. struct ipath_ibdev *dev = to_idev(ibqp->device);
  393. struct ipath_qp *qp = to_iqp(ibqp);
  394. enum ib_qp_state cur_state, new_state;
  395. unsigned long flags;
  396. int ret;
  397. spin_lock_irqsave(&qp->s_lock, flags);
  398. cur_state = attr_mask & IB_QP_CUR_STATE ?
  399. attr->cur_qp_state : qp->state;
  400. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  401. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  402. attr_mask))
  403. goto inval;
  404. if (attr_mask & IB_QP_AV)
  405. if (attr->ah_attr.dlid == 0 ||
  406. attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE)
  407. goto inval;
  408. if (attr_mask & IB_QP_PKEY_INDEX)
  409. if (attr->pkey_index >= ipath_layer_get_npkeys(dev->dd))
  410. goto inval;
  411. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  412. if (attr->min_rnr_timer > 31)
  413. goto inval;
  414. switch (new_state) {
  415. case IB_QPS_RESET:
  416. ipath_reset_qp(qp);
  417. break;
  418. case IB_QPS_ERR:
  419. ipath_error_qp(qp);
  420. break;
  421. default:
  422. break;
  423. }
  424. if (attr_mask & IB_QP_PKEY_INDEX)
  425. qp->s_pkey_index = attr->pkey_index;
  426. if (attr_mask & IB_QP_DEST_QPN)
  427. qp->remote_qpn = attr->dest_qp_num;
  428. if (attr_mask & IB_QP_SQ_PSN) {
  429. qp->s_next_psn = attr->sq_psn;
  430. qp->s_last_psn = qp->s_next_psn - 1;
  431. }
  432. if (attr_mask & IB_QP_RQ_PSN)
  433. qp->r_psn = attr->rq_psn;
  434. if (attr_mask & IB_QP_ACCESS_FLAGS)
  435. qp->qp_access_flags = attr->qp_access_flags;
  436. if (attr_mask & IB_QP_AV)
  437. qp->remote_ah_attr = attr->ah_attr;
  438. if (attr_mask & IB_QP_PATH_MTU)
  439. qp->path_mtu = attr->path_mtu;
  440. if (attr_mask & IB_QP_RETRY_CNT)
  441. qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
  442. if (attr_mask & IB_QP_RNR_RETRY) {
  443. qp->s_rnr_retry = attr->rnr_retry;
  444. if (qp->s_rnr_retry > 7)
  445. qp->s_rnr_retry = 7;
  446. qp->s_rnr_retry_cnt = qp->s_rnr_retry;
  447. }
  448. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  449. qp->r_min_rnr_timer = attr->min_rnr_timer;
  450. if (attr_mask & IB_QP_QKEY)
  451. qp->qkey = attr->qkey;
  452. qp->state = new_state;
  453. spin_unlock_irqrestore(&qp->s_lock, flags);
  454. ret = 0;
  455. goto bail;
  456. inval:
  457. spin_unlock_irqrestore(&qp->s_lock, flags);
  458. ret = -EINVAL;
  459. bail:
  460. return ret;
  461. }
  462. int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  463. int attr_mask, struct ib_qp_init_attr *init_attr)
  464. {
  465. struct ipath_qp *qp = to_iqp(ibqp);
  466. attr->qp_state = qp->state;
  467. attr->cur_qp_state = attr->qp_state;
  468. attr->path_mtu = qp->path_mtu;
  469. attr->path_mig_state = 0;
  470. attr->qkey = qp->qkey;
  471. attr->rq_psn = qp->r_psn;
  472. attr->sq_psn = qp->s_next_psn;
  473. attr->dest_qp_num = qp->remote_qpn;
  474. attr->qp_access_flags = qp->qp_access_flags;
  475. attr->cap.max_send_wr = qp->s_size - 1;
  476. attr->cap.max_recv_wr = qp->r_rq.size - 1;
  477. attr->cap.max_send_sge = qp->s_max_sge;
  478. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  479. attr->cap.max_inline_data = 0;
  480. attr->ah_attr = qp->remote_ah_attr;
  481. memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
  482. attr->pkey_index = qp->s_pkey_index;
  483. attr->alt_pkey_index = 0;
  484. attr->en_sqd_async_notify = 0;
  485. attr->sq_draining = 0;
  486. attr->max_rd_atomic = 1;
  487. attr->max_dest_rd_atomic = 1;
  488. attr->min_rnr_timer = qp->r_min_rnr_timer;
  489. attr->port_num = 1;
  490. attr->timeout = 0;
  491. attr->retry_cnt = qp->s_retry_cnt;
  492. attr->rnr_retry = qp->s_rnr_retry;
  493. attr->alt_port_num = 0;
  494. attr->alt_timeout = 0;
  495. init_attr->event_handler = qp->ibqp.event_handler;
  496. init_attr->qp_context = qp->ibqp.qp_context;
  497. init_attr->send_cq = qp->ibqp.send_cq;
  498. init_attr->recv_cq = qp->ibqp.recv_cq;
  499. init_attr->srq = qp->ibqp.srq;
  500. init_attr->cap = attr->cap;
  501. init_attr->sq_sig_type =
  502. (qp->s_flags & (1 << IPATH_S_SIGNAL_REQ_WR))
  503. ? IB_SIGNAL_REQ_WR : 0;
  504. init_attr->qp_type = qp->ibqp.qp_type;
  505. init_attr->port_num = 1;
  506. return 0;
  507. }
  508. /**
  509. * ipath_compute_aeth - compute the AETH (syndrome + MSN)
  510. * @qp: the queue pair to compute the AETH for
  511. *
  512. * Returns the AETH.
  513. */
  514. __be32 ipath_compute_aeth(struct ipath_qp *qp)
  515. {
  516. u32 aeth = qp->r_msn & IPATH_MSN_MASK;
  517. if (qp->ibqp.srq) {
  518. /*
  519. * Shared receive queues don't generate credits.
  520. * Set the credit field to the invalid value.
  521. */
  522. aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT;
  523. } else {
  524. u32 min, max, x;
  525. u32 credits;
  526. /*
  527. * Compute the number of credits available (RWQEs).
  528. * XXX Not holding the r_rq.lock here so there is a small
  529. * chance that the pair of reads are not atomic.
  530. */
  531. credits = qp->r_rq.head - qp->r_rq.tail;
  532. if ((int)credits < 0)
  533. credits += qp->r_rq.size;
  534. /*
  535. * Binary search the credit table to find the code to
  536. * use.
  537. */
  538. min = 0;
  539. max = 31;
  540. for (;;) {
  541. x = (min + max) / 2;
  542. if (credit_table[x] == credits)
  543. break;
  544. if (credit_table[x] > credits)
  545. max = x;
  546. else if (min == x)
  547. break;
  548. else
  549. min = x;
  550. }
  551. aeth |= x << IPATH_AETH_CREDIT_SHIFT;
  552. }
  553. return cpu_to_be32(aeth);
  554. }
  555. /**
  556. * ipath_create_qp - create a queue pair for a device
  557. * @ibpd: the protection domain who's device we create the queue pair for
  558. * @init_attr: the attributes of the queue pair
  559. * @udata: unused by InfiniPath
  560. *
  561. * Returns the queue pair on success, otherwise returns an errno.
  562. *
  563. * Called by the ib_create_qp() core verbs function.
  564. */
  565. struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
  566. struct ib_qp_init_attr *init_attr,
  567. struct ib_udata *udata)
  568. {
  569. struct ipath_qp *qp;
  570. int err;
  571. struct ipath_swqe *swq = NULL;
  572. struct ipath_ibdev *dev;
  573. size_t sz;
  574. struct ib_qp *ret;
  575. if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
  576. init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
  577. init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs ||
  578. init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
  579. ret = ERR_PTR(-ENOMEM);
  580. goto bail;
  581. }
  582. if (init_attr->cap.max_send_sge +
  583. init_attr->cap.max_recv_sge +
  584. init_attr->cap.max_send_wr +
  585. init_attr->cap.max_recv_wr == 0) {
  586. ret = ERR_PTR(-EINVAL);
  587. goto bail;
  588. }
  589. switch (init_attr->qp_type) {
  590. case IB_QPT_UC:
  591. case IB_QPT_RC:
  592. sz = sizeof(struct ipath_sge) *
  593. init_attr->cap.max_send_sge +
  594. sizeof(struct ipath_swqe);
  595. swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
  596. if (swq == NULL) {
  597. ret = ERR_PTR(-ENOMEM);
  598. goto bail;
  599. }
  600. /* FALLTHROUGH */
  601. case IB_QPT_UD:
  602. case IB_QPT_SMI:
  603. case IB_QPT_GSI:
  604. qp = kmalloc(sizeof(*qp), GFP_KERNEL);
  605. if (!qp) {
  606. vfree(swq);
  607. ret = ERR_PTR(-ENOMEM);
  608. goto bail;
  609. }
  610. if (init_attr->srq) {
  611. qp->r_rq.size = 0;
  612. qp->r_rq.max_sge = 0;
  613. qp->r_rq.wq = NULL;
  614. } else {
  615. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  616. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  617. sz = (sizeof(struct ipath_sge) * qp->r_rq.max_sge) +
  618. sizeof(struct ipath_rwqe);
  619. qp->r_rq.wq = vmalloc(qp->r_rq.size * sz);
  620. if (!qp->r_rq.wq) {
  621. kfree(qp);
  622. vfree(swq);
  623. ret = ERR_PTR(-ENOMEM);
  624. goto bail;
  625. }
  626. }
  627. /*
  628. * ib_create_qp() will initialize qp->ibqp
  629. * except for qp->ibqp.qp_num.
  630. */
  631. spin_lock_init(&qp->s_lock);
  632. spin_lock_init(&qp->r_rq.lock);
  633. atomic_set(&qp->refcount, 0);
  634. init_waitqueue_head(&qp->wait);
  635. tasklet_init(&qp->s_task, ipath_do_ruc_send,
  636. (unsigned long)qp);
  637. INIT_LIST_HEAD(&qp->piowait);
  638. INIT_LIST_HEAD(&qp->timerwait);
  639. qp->state = IB_QPS_RESET;
  640. qp->s_wq = swq;
  641. qp->s_size = init_attr->cap.max_send_wr + 1;
  642. qp->s_max_sge = init_attr->cap.max_send_sge;
  643. qp->s_flags = init_attr->sq_sig_type == IB_SIGNAL_REQ_WR ?
  644. 1 << IPATH_S_SIGNAL_REQ_WR : 0;
  645. dev = to_idev(ibpd->device);
  646. err = ipath_alloc_qpn(&dev->qp_table, qp,
  647. init_attr->qp_type);
  648. if (err) {
  649. vfree(swq);
  650. vfree(qp->r_rq.wq);
  651. kfree(qp);
  652. ret = ERR_PTR(err);
  653. goto bail;
  654. }
  655. ipath_reset_qp(qp);
  656. /* Tell the core driver that the kernel SMA is present. */
  657. if (init_attr->qp_type == IB_QPT_SMI)
  658. ipath_layer_set_verbs_flags(dev->dd,
  659. IPATH_VERBS_KERNEL_SMA);
  660. break;
  661. default:
  662. /* Don't support raw QPs */
  663. ret = ERR_PTR(-ENOSYS);
  664. goto bail;
  665. }
  666. init_attr->cap.max_inline_data = 0;
  667. ret = &qp->ibqp;
  668. bail:
  669. return ret;
  670. }
  671. /**
  672. * ipath_destroy_qp - destroy a queue pair
  673. * @ibqp: the queue pair to destroy
  674. *
  675. * Returns 0 on success.
  676. *
  677. * Note that this can be called while the QP is actively sending or
  678. * receiving!
  679. */
  680. int ipath_destroy_qp(struct ib_qp *ibqp)
  681. {
  682. struct ipath_qp *qp = to_iqp(ibqp);
  683. struct ipath_ibdev *dev = to_idev(ibqp->device);
  684. unsigned long flags;
  685. /* Tell the core driver that the kernel SMA is gone. */
  686. if (qp->ibqp.qp_type == IB_QPT_SMI)
  687. ipath_layer_set_verbs_flags(dev->dd, 0);
  688. spin_lock_irqsave(&qp->r_rq.lock, flags);
  689. spin_lock(&qp->s_lock);
  690. qp->state = IB_QPS_ERR;
  691. spin_unlock(&qp->s_lock);
  692. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  693. /* Stop the sending tasklet. */
  694. tasklet_kill(&qp->s_task);
  695. /* Make sure the QP isn't on the timeout list. */
  696. spin_lock_irqsave(&dev->pending_lock, flags);
  697. if (!list_empty(&qp->timerwait))
  698. list_del_init(&qp->timerwait);
  699. if (!list_empty(&qp->piowait))
  700. list_del_init(&qp->piowait);
  701. spin_unlock_irqrestore(&dev->pending_lock, flags);
  702. /*
  703. * Make sure that the QP is not in the QPN table so receive
  704. * interrupts will discard packets for this QP. XXX Also remove QP
  705. * from multicast table.
  706. */
  707. if (atomic_read(&qp->refcount) != 0)
  708. ipath_free_qp(&dev->qp_table, qp);
  709. vfree(qp->s_wq);
  710. vfree(qp->r_rq.wq);
  711. kfree(qp);
  712. return 0;
  713. }
  714. /**
  715. * ipath_init_qp_table - initialize the QP table for a device
  716. * @idev: the device who's QP table we're initializing
  717. * @size: the size of the QP table
  718. *
  719. * Returns 0 on success, otherwise returns an errno.
  720. */
  721. int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
  722. {
  723. int i;
  724. int ret;
  725. idev->qp_table.last = 1; /* QPN 0 and 1 are special. */
  726. idev->qp_table.max = size;
  727. idev->qp_table.nmaps = 1;
  728. idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
  729. GFP_KERNEL);
  730. if (idev->qp_table.table == NULL) {
  731. ret = -ENOMEM;
  732. goto bail;
  733. }
  734. for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
  735. atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
  736. idev->qp_table.map[i].page = NULL;
  737. }
  738. ret = 0;
  739. bail:
  740. return ret;
  741. }
  742. /**
  743. * ipath_sqerror_qp - put a QP's send queue into an error state
  744. * @qp: QP who's send queue will be put into an error state
  745. * @wc: the WC responsible for putting the QP in this state
  746. *
  747. * Flushes the send work queue.
  748. * The QP s_lock should be held.
  749. */
  750. void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
  751. {
  752. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  753. struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
  754. _VERBS_INFO("Send queue error on QP%d/%d: err: %d\n",
  755. qp->ibqp.qp_num, qp->remote_qpn, wc->status);
  756. spin_lock(&dev->pending_lock);
  757. /* XXX What if its already removed by the timeout code? */
  758. if (!list_empty(&qp->timerwait))
  759. list_del_init(&qp->timerwait);
  760. if (!list_empty(&qp->piowait))
  761. list_del_init(&qp->piowait);
  762. spin_unlock(&dev->pending_lock);
  763. ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
  764. if (++qp->s_last >= qp->s_size)
  765. qp->s_last = 0;
  766. wc->status = IB_WC_WR_FLUSH_ERR;
  767. while (qp->s_last != qp->s_head) {
  768. wc->wr_id = wqe->wr.wr_id;
  769. wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  770. ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
  771. if (++qp->s_last >= qp->s_size)
  772. qp->s_last = 0;
  773. wqe = get_swqe_ptr(qp, qp->s_last);
  774. }
  775. qp->s_cur = qp->s_tail = qp->s_head;
  776. qp->state = IB_QPS_SQE;
  777. }
  778. /**
  779. * ipath_get_credit - flush the send work queue of a QP
  780. * @qp: the qp who's send work queue to flush
  781. * @aeth: the Acknowledge Extended Transport Header
  782. *
  783. * The QP s_lock should be held.
  784. */
  785. void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
  786. {
  787. u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK;
  788. /*
  789. * If the credit is invalid, we can send
  790. * as many packets as we like. Otherwise, we have to
  791. * honor the credit field.
  792. */
  793. if (credit == IPATH_AETH_CREDIT_INVAL)
  794. qp->s_lsn = (u32) -1;
  795. else if (qp->s_lsn != (u32) -1) {
  796. /* Compute new LSN (i.e., MSN + credit) */
  797. credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK;
  798. if (ipath_cmp24(credit, qp->s_lsn) > 0)
  799. qp->s_lsn = credit;
  800. }
  801. /* Restart sending if it was blocked due to lack of credits. */
  802. if (qp->s_cur != qp->s_head &&
  803. (qp->s_lsn == (u32) -1 ||
  804. ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
  805. qp->s_lsn + 1) <= 0))
  806. tasklet_hi_schedule(&qp->s_task);
  807. }