ipath_qp.c 22 KB

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