ipath_qp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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 "ips_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. qp->s_hdrwords = 0;
  303. qp->s_psn = 0;
  304. qp->r_psn = 0;
  305. atomic_set(&qp->msn, 0);
  306. if (qp->ibqp.qp_type == IB_QPT_RC) {
  307. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  308. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  309. } else {
  310. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  311. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  312. }
  313. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  314. qp->s_nak_state = 0;
  315. qp->s_rnr_timeout = 0;
  316. qp->s_head = 0;
  317. qp->s_tail = 0;
  318. qp->s_cur = 0;
  319. qp->s_last = 0;
  320. qp->s_ssn = 1;
  321. qp->s_lsn = 0;
  322. qp->r_rq.head = 0;
  323. qp->r_rq.tail = 0;
  324. qp->r_reuse_sge = 0;
  325. }
  326. /**
  327. * ipath_error_qp - put a QP into an error state
  328. * @qp: the QP to put into an error state
  329. *
  330. * Flushes both send and receive work queues.
  331. * QP r_rq.lock and s_lock should be held.
  332. */
  333. static void ipath_error_qp(struct ipath_qp *qp)
  334. {
  335. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  336. struct ib_wc wc;
  337. _VERBS_INFO("QP%d/%d in error state\n",
  338. qp->ibqp.qp_num, qp->remote_qpn);
  339. spin_lock(&dev->pending_lock);
  340. /* XXX What if its already removed by the timeout code? */
  341. if (!list_empty(&qp->timerwait))
  342. list_del_init(&qp->timerwait);
  343. if (!list_empty(&qp->piowait))
  344. list_del_init(&qp->piowait);
  345. spin_unlock(&dev->pending_lock);
  346. wc.status = IB_WC_WR_FLUSH_ERR;
  347. wc.vendor_err = 0;
  348. wc.byte_len = 0;
  349. wc.imm_data = 0;
  350. wc.qp_num = qp->ibqp.qp_num;
  351. wc.src_qp = 0;
  352. wc.wc_flags = 0;
  353. wc.pkey_index = 0;
  354. wc.slid = 0;
  355. wc.sl = 0;
  356. wc.dlid_path_bits = 0;
  357. wc.port_num = 0;
  358. while (qp->s_last != qp->s_head) {
  359. struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
  360. wc.wr_id = wqe->wr.wr_id;
  361. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  362. if (++qp->s_last >= qp->s_size)
  363. qp->s_last = 0;
  364. ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
  365. }
  366. qp->s_cur = qp->s_tail = qp->s_head;
  367. qp->s_hdrwords = 0;
  368. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  369. wc.opcode = IB_WC_RECV;
  370. while (qp->r_rq.tail != qp->r_rq.head) {
  371. wc.wr_id = get_rwqe_ptr(&qp->r_rq, qp->r_rq.tail)->wr_id;
  372. if (++qp->r_rq.tail >= qp->r_rq.size)
  373. qp->r_rq.tail = 0;
  374. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
  375. }
  376. }
  377. /**
  378. * ipath_modify_qp - modify the attributes of a queue pair
  379. * @ibqp: the queue pair who's attributes we're modifying
  380. * @attr: the new attributes
  381. * @attr_mask: the mask of attributes to modify
  382. *
  383. * Returns 0 on success, otherwise returns an errno.
  384. */
  385. int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  386. int attr_mask)
  387. {
  388. struct ipath_ibdev *dev = to_idev(ibqp->device);
  389. struct ipath_qp *qp = to_iqp(ibqp);
  390. enum ib_qp_state cur_state, new_state;
  391. unsigned long flags;
  392. int ret;
  393. spin_lock_irqsave(&qp->r_rq.lock, flags);
  394. spin_lock(&qp->s_lock);
  395. cur_state = attr_mask & IB_QP_CUR_STATE ?
  396. attr->cur_qp_state : qp->state;
  397. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  398. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  399. attr_mask))
  400. goto inval;
  401. if (attr_mask & IB_QP_AV)
  402. if (attr->ah_attr.dlid == 0 ||
  403. attr->ah_attr.dlid >= IPS_MULTICAST_LID_BASE)
  404. goto inval;
  405. if (attr_mask & IB_QP_PKEY_INDEX)
  406. if (attr->pkey_index >= ipath_layer_get_npkeys(dev->dd))
  407. goto inval;
  408. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  409. if (attr->min_rnr_timer > 31)
  410. goto inval;
  411. switch (new_state) {
  412. case IB_QPS_RESET:
  413. ipath_reset_qp(qp);
  414. break;
  415. case IB_QPS_ERR:
  416. ipath_error_qp(qp);
  417. break;
  418. default:
  419. break;
  420. }
  421. if (attr_mask & IB_QP_PKEY_INDEX)
  422. qp->s_pkey_index = attr->pkey_index;
  423. if (attr_mask & IB_QP_DEST_QPN)
  424. qp->remote_qpn = attr->dest_qp_num;
  425. if (attr_mask & IB_QP_SQ_PSN) {
  426. qp->s_next_psn = attr->sq_psn;
  427. qp->s_last_psn = qp->s_next_psn - 1;
  428. }
  429. if (attr_mask & IB_QP_RQ_PSN)
  430. qp->r_psn = attr->rq_psn;
  431. if (attr_mask & IB_QP_ACCESS_FLAGS)
  432. qp->qp_access_flags = attr->qp_access_flags;
  433. if (attr_mask & IB_QP_AV)
  434. qp->remote_ah_attr = attr->ah_attr;
  435. if (attr_mask & IB_QP_PATH_MTU)
  436. qp->path_mtu = attr->path_mtu;
  437. if (attr_mask & IB_QP_RETRY_CNT)
  438. qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
  439. if (attr_mask & IB_QP_RNR_RETRY) {
  440. qp->s_rnr_retry = attr->rnr_retry;
  441. if (qp->s_rnr_retry > 7)
  442. qp->s_rnr_retry = 7;
  443. qp->s_rnr_retry_cnt = qp->s_rnr_retry;
  444. }
  445. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  446. qp->s_min_rnr_timer = attr->min_rnr_timer;
  447. if (attr_mask & IB_QP_QKEY)
  448. qp->qkey = attr->qkey;
  449. qp->state = new_state;
  450. spin_unlock(&qp->s_lock);
  451. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  452. /*
  453. * If QP1 changed to the RTS state, try to move to the link to INIT
  454. * even if it was ACTIVE so the SM will reinitialize the SMA's
  455. * state.
  456. */
  457. if (qp->ibqp.qp_num == 1 && new_state == IB_QPS_RTS) {
  458. struct ipath_ibdev *dev = to_idev(ibqp->device);
  459. ipath_layer_set_linkstate(dev->dd, IPATH_IB_LINKDOWN);
  460. }
  461. ret = 0;
  462. goto bail;
  463. inval:
  464. spin_unlock(&qp->s_lock);
  465. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  466. ret = -EINVAL;
  467. bail:
  468. return ret;
  469. }
  470. int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  471. int attr_mask, struct ib_qp_init_attr *init_attr)
  472. {
  473. struct ipath_qp *qp = to_iqp(ibqp);
  474. attr->qp_state = qp->state;
  475. attr->cur_qp_state = attr->qp_state;
  476. attr->path_mtu = qp->path_mtu;
  477. attr->path_mig_state = 0;
  478. attr->qkey = qp->qkey;
  479. attr->rq_psn = qp->r_psn;
  480. attr->sq_psn = qp->s_next_psn;
  481. attr->dest_qp_num = qp->remote_qpn;
  482. attr->qp_access_flags = qp->qp_access_flags;
  483. attr->cap.max_send_wr = qp->s_size - 1;
  484. attr->cap.max_recv_wr = qp->r_rq.size - 1;
  485. attr->cap.max_send_sge = qp->s_max_sge;
  486. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  487. attr->cap.max_inline_data = 0;
  488. attr->ah_attr = qp->remote_ah_attr;
  489. memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
  490. attr->pkey_index = qp->s_pkey_index;
  491. attr->alt_pkey_index = 0;
  492. attr->en_sqd_async_notify = 0;
  493. attr->sq_draining = 0;
  494. attr->max_rd_atomic = 1;
  495. attr->max_dest_rd_atomic = 1;
  496. attr->min_rnr_timer = qp->s_min_rnr_timer;
  497. attr->port_num = 1;
  498. attr->timeout = 0;
  499. attr->retry_cnt = qp->s_retry_cnt;
  500. attr->rnr_retry = qp->s_rnr_retry;
  501. attr->alt_port_num = 0;
  502. attr->alt_timeout = 0;
  503. init_attr->event_handler = qp->ibqp.event_handler;
  504. init_attr->qp_context = qp->ibqp.qp_context;
  505. init_attr->send_cq = qp->ibqp.send_cq;
  506. init_attr->recv_cq = qp->ibqp.recv_cq;
  507. init_attr->srq = qp->ibqp.srq;
  508. init_attr->cap = attr->cap;
  509. init_attr->sq_sig_type =
  510. (qp->s_flags & (1 << IPATH_S_SIGNAL_REQ_WR))
  511. ? IB_SIGNAL_REQ_WR : 0;
  512. init_attr->qp_type = qp->ibqp.qp_type;
  513. init_attr->port_num = 1;
  514. return 0;
  515. }
  516. /**
  517. * ipath_compute_aeth - compute the AETH (syndrome + MSN)
  518. * @qp: the queue pair to compute the AETH for
  519. *
  520. * Returns the AETH.
  521. *
  522. * The QP s_lock should be held.
  523. */
  524. __be32 ipath_compute_aeth(struct ipath_qp *qp)
  525. {
  526. u32 aeth = atomic_read(&qp->msn) & IPS_MSN_MASK;
  527. if (qp->s_nak_state) {
  528. aeth |= qp->s_nak_state << IPS_AETH_CREDIT_SHIFT;
  529. } else if (qp->ibqp.srq) {
  530. /*
  531. * Shared receive queues don't generate credits.
  532. * Set the credit field to the invalid value.
  533. */
  534. aeth |= IPS_AETH_CREDIT_INVAL << IPS_AETH_CREDIT_SHIFT;
  535. } else {
  536. u32 min, max, x;
  537. u32 credits;
  538. /*
  539. * Compute the number of credits available (RWQEs).
  540. * XXX Not holding the r_rq.lock here so there is a small
  541. * chance that the pair of reads are not atomic.
  542. */
  543. credits = qp->r_rq.head - qp->r_rq.tail;
  544. if ((int)credits < 0)
  545. credits += qp->r_rq.size;
  546. /*
  547. * Binary search the credit table to find the code to
  548. * use.
  549. */
  550. min = 0;
  551. max = 31;
  552. for (;;) {
  553. x = (min + max) / 2;
  554. if (credit_table[x] == credits)
  555. break;
  556. if (credit_table[x] > credits)
  557. max = x;
  558. else if (min == x)
  559. break;
  560. else
  561. min = x;
  562. }
  563. aeth |= x << IPS_AETH_CREDIT_SHIFT;
  564. }
  565. return cpu_to_be32(aeth);
  566. }
  567. /**
  568. * ipath_create_qp - create a queue pair for a device
  569. * @ibpd: the protection domain who's device we create the queue pair for
  570. * @init_attr: the attributes of the queue pair
  571. * @udata: unused by InfiniPath
  572. *
  573. * Returns the queue pair on success, otherwise returns an errno.
  574. *
  575. * Called by the ib_create_qp() core verbs function.
  576. */
  577. struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
  578. struct ib_qp_init_attr *init_attr,
  579. struct ib_udata *udata)
  580. {
  581. struct ipath_qp *qp;
  582. int err;
  583. struct ipath_swqe *swq = NULL;
  584. struct ipath_ibdev *dev;
  585. size_t sz;
  586. struct ib_qp *ret;
  587. if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
  588. init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
  589. init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs ||
  590. init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
  591. ret = ERR_PTR(-ENOMEM);
  592. goto bail;
  593. }
  594. if (init_attr->cap.max_send_sge +
  595. init_attr->cap.max_recv_sge +
  596. init_attr->cap.max_send_wr +
  597. init_attr->cap.max_recv_wr == 0) {
  598. ret = ERR_PTR(-EINVAL);
  599. goto bail;
  600. }
  601. switch (init_attr->qp_type) {
  602. case IB_QPT_UC:
  603. case IB_QPT_RC:
  604. sz = sizeof(struct ipath_sge) *
  605. init_attr->cap.max_send_sge +
  606. sizeof(struct ipath_swqe);
  607. swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
  608. if (swq == NULL) {
  609. ret = ERR_PTR(-ENOMEM);
  610. goto bail;
  611. }
  612. /* FALLTHROUGH */
  613. case IB_QPT_UD:
  614. case IB_QPT_SMI:
  615. case IB_QPT_GSI:
  616. qp = kmalloc(sizeof(*qp), GFP_KERNEL);
  617. if (!qp) {
  618. vfree(swq);
  619. ret = ERR_PTR(-ENOMEM);
  620. goto bail;
  621. }
  622. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  623. sz = sizeof(struct ipath_sge) *
  624. init_attr->cap.max_recv_sge +
  625. sizeof(struct ipath_rwqe);
  626. qp->r_rq.wq = vmalloc(qp->r_rq.size * sz);
  627. if (!qp->r_rq.wq) {
  628. kfree(qp);
  629. vfree(swq);
  630. ret = ERR_PTR(-ENOMEM);
  631. goto bail;
  632. }
  633. /*
  634. * ib_create_qp() will initialize qp->ibqp
  635. * except for qp->ibqp.qp_num.
  636. */
  637. spin_lock_init(&qp->s_lock);
  638. spin_lock_init(&qp->r_rq.lock);
  639. atomic_set(&qp->refcount, 0);
  640. init_waitqueue_head(&qp->wait);
  641. tasklet_init(&qp->s_task, ipath_do_ruc_send,
  642. (unsigned long)qp);
  643. INIT_LIST_HEAD(&qp->piowait);
  644. INIT_LIST_HEAD(&qp->timerwait);
  645. qp->state = IB_QPS_RESET;
  646. qp->s_wq = swq;
  647. qp->s_size = init_attr->cap.max_send_wr + 1;
  648. qp->s_max_sge = init_attr->cap.max_send_sge;
  649. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  650. qp->s_flags = init_attr->sq_sig_type == IB_SIGNAL_REQ_WR ?
  651. 1 << IPATH_S_SIGNAL_REQ_WR : 0;
  652. dev = to_idev(ibpd->device);
  653. err = ipath_alloc_qpn(&dev->qp_table, qp,
  654. init_attr->qp_type);
  655. if (err) {
  656. vfree(swq);
  657. vfree(qp->r_rq.wq);
  658. kfree(qp);
  659. ret = ERR_PTR(err);
  660. goto bail;
  661. }
  662. ipath_reset_qp(qp);
  663. /* Tell the core driver that the kernel SMA is present. */
  664. if (init_attr->qp_type == IB_QPT_SMI)
  665. ipath_layer_set_verbs_flags(dev->dd,
  666. IPATH_VERBS_KERNEL_SMA);
  667. break;
  668. default:
  669. /* Don't support raw QPs */
  670. ret = ERR_PTR(-ENOSYS);
  671. goto bail;
  672. }
  673. init_attr->cap.max_inline_data = 0;
  674. ret = &qp->ibqp;
  675. bail:
  676. return ret;
  677. }
  678. /**
  679. * ipath_destroy_qp - destroy a queue pair
  680. * @ibqp: the queue pair to destroy
  681. *
  682. * Returns 0 on success.
  683. *
  684. * Note that this can be called while the QP is actively sending or
  685. * receiving!
  686. */
  687. int ipath_destroy_qp(struct ib_qp *ibqp)
  688. {
  689. struct ipath_qp *qp = to_iqp(ibqp);
  690. struct ipath_ibdev *dev = to_idev(ibqp->device);
  691. unsigned long flags;
  692. /* Tell the core driver that the kernel SMA is gone. */
  693. if (qp->ibqp.qp_type == IB_QPT_SMI)
  694. ipath_layer_set_verbs_flags(dev->dd, 0);
  695. spin_lock_irqsave(&qp->r_rq.lock, flags);
  696. spin_lock(&qp->s_lock);
  697. qp->state = IB_QPS_ERR;
  698. spin_unlock(&qp->s_lock);
  699. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  700. /* Stop the sending tasklet. */
  701. tasklet_kill(&qp->s_task);
  702. /* Make sure the QP isn't on the timeout list. */
  703. spin_lock_irqsave(&dev->pending_lock, flags);
  704. if (!list_empty(&qp->timerwait))
  705. list_del_init(&qp->timerwait);
  706. if (!list_empty(&qp->piowait))
  707. list_del_init(&qp->piowait);
  708. spin_unlock_irqrestore(&dev->pending_lock, flags);
  709. /*
  710. * Make sure that the QP is not in the QPN table so receive
  711. * interrupts will discard packets for this QP. XXX Also remove QP
  712. * from multicast table.
  713. */
  714. if (atomic_read(&qp->refcount) != 0)
  715. ipath_free_qp(&dev->qp_table, qp);
  716. vfree(qp->s_wq);
  717. vfree(qp->r_rq.wq);
  718. kfree(qp);
  719. return 0;
  720. }
  721. /**
  722. * ipath_init_qp_table - initialize the QP table for a device
  723. * @idev: the device who's QP table we're initializing
  724. * @size: the size of the QP table
  725. *
  726. * Returns 0 on success, otherwise returns an errno.
  727. */
  728. int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
  729. {
  730. int i;
  731. int ret;
  732. idev->qp_table.last = 1; /* QPN 0 and 1 are special. */
  733. idev->qp_table.max = size;
  734. idev->qp_table.nmaps = 1;
  735. idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
  736. GFP_KERNEL);
  737. if (idev->qp_table.table == NULL) {
  738. ret = -ENOMEM;
  739. goto bail;
  740. }
  741. for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
  742. atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
  743. idev->qp_table.map[i].page = NULL;
  744. }
  745. ret = 0;
  746. bail:
  747. return ret;
  748. }
  749. /**
  750. * ipath_sqerror_qp - put a QP's send queue into an error state
  751. * @qp: QP who's send queue will be put into an error state
  752. * @wc: the WC responsible for putting the QP in this state
  753. *
  754. * Flushes the send work queue.
  755. * The QP s_lock should be held.
  756. */
  757. void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
  758. {
  759. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  760. struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
  761. _VERBS_INFO("Send queue error on QP%d/%d: err: %d\n",
  762. qp->ibqp.qp_num, qp->remote_qpn, wc->status);
  763. spin_lock(&dev->pending_lock);
  764. /* XXX What if its already removed by the timeout code? */
  765. if (!list_empty(&qp->timerwait))
  766. list_del_init(&qp->timerwait);
  767. if (!list_empty(&qp->piowait))
  768. list_del_init(&qp->piowait);
  769. spin_unlock(&dev->pending_lock);
  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. wc->status = IB_WC_WR_FLUSH_ERR;
  774. while (qp->s_last != qp->s_head) {
  775. wc->wr_id = wqe->wr.wr_id;
  776. wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  777. ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
  778. if (++qp->s_last >= qp->s_size)
  779. qp->s_last = 0;
  780. wqe = get_swqe_ptr(qp, qp->s_last);
  781. }
  782. qp->s_cur = qp->s_tail = qp->s_head;
  783. qp->state = IB_QPS_SQE;
  784. }
  785. /**
  786. * ipath_get_credit - flush the send work queue of a QP
  787. * @qp: the qp who's send work queue to flush
  788. * @aeth: the Acknowledge Extended Transport Header
  789. *
  790. * The QP s_lock should be held.
  791. */
  792. void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
  793. {
  794. u32 credit = (aeth >> IPS_AETH_CREDIT_SHIFT) & IPS_AETH_CREDIT_MASK;
  795. /*
  796. * If the credit is invalid, we can send
  797. * as many packets as we like. Otherwise, we have to
  798. * honor the credit field.
  799. */
  800. if (credit == IPS_AETH_CREDIT_INVAL)
  801. qp->s_lsn = (u32) -1;
  802. else if (qp->s_lsn != (u32) -1) {
  803. /* Compute new LSN (i.e., MSN + credit) */
  804. credit = (aeth + credit_table[credit]) & IPS_MSN_MASK;
  805. if (ipath_cmp24(credit, qp->s_lsn) > 0)
  806. qp->s_lsn = credit;
  807. }
  808. /* Restart sending if it was blocked due to lack of credits. */
  809. if (qp->s_cur != qp->s_head &&
  810. (qp->s_lsn == (u32) -1 ||
  811. ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
  812. qp->s_lsn + 1) <= 0))
  813. tasklet_hi_schedule(&qp->s_task);
  814. }