ipath_qp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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_kernel.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. /*
  44. * Convert the AETH credit code into the number of credits.
  45. */
  46. static u32 credit_table[31] = {
  47. 0, /* 0 */
  48. 1, /* 1 */
  49. 2, /* 2 */
  50. 3, /* 3 */
  51. 4, /* 4 */
  52. 6, /* 5 */
  53. 8, /* 6 */
  54. 12, /* 7 */
  55. 16, /* 8 */
  56. 24, /* 9 */
  57. 32, /* A */
  58. 48, /* B */
  59. 64, /* C */
  60. 96, /* D */
  61. 128, /* E */
  62. 192, /* F */
  63. 256, /* 10 */
  64. 384, /* 11 */
  65. 512, /* 12 */
  66. 768, /* 13 */
  67. 1024, /* 14 */
  68. 1536, /* 15 */
  69. 2048, /* 16 */
  70. 3072, /* 17 */
  71. 4096, /* 18 */
  72. 6144, /* 19 */
  73. 8192, /* 1A */
  74. 12288, /* 1B */
  75. 16384, /* 1C */
  76. 24576, /* 1D */
  77. 32768 /* 1E */
  78. };
  79. static void get_map_page(struct ipath_qp_table *qpt, struct qpn_map *map)
  80. {
  81. unsigned long page = get_zeroed_page(GFP_KERNEL);
  82. unsigned long flags;
  83. /*
  84. * Free the page if someone raced with us installing it.
  85. */
  86. spin_lock_irqsave(&qpt->lock, flags);
  87. if (map->page)
  88. free_page(page);
  89. else
  90. map->page = (void *)page;
  91. spin_unlock_irqrestore(&qpt->lock, flags);
  92. }
  93. static int alloc_qpn(struct ipath_qp_table *qpt, enum ib_qp_type type)
  94. {
  95. u32 i, offset, max_scan, qpn;
  96. struct qpn_map *map;
  97. u32 ret = -1;
  98. if (type == IB_QPT_SMI)
  99. ret = 0;
  100. else if (type == IB_QPT_GSI)
  101. ret = 1;
  102. if (ret != -1) {
  103. map = &qpt->map[0];
  104. if (unlikely(!map->page)) {
  105. get_map_page(qpt, map);
  106. if (unlikely(!map->page)) {
  107. ret = -ENOMEM;
  108. goto bail;
  109. }
  110. }
  111. if (!test_and_set_bit(ret, map->page))
  112. atomic_dec(&map->n_free);
  113. else
  114. ret = -EBUSY;
  115. goto bail;
  116. }
  117. qpn = qpt->last + 1;
  118. if (qpn >= QPN_MAX)
  119. qpn = 2;
  120. offset = qpn & BITS_PER_PAGE_MASK;
  121. map = &qpt->map[qpn / BITS_PER_PAGE];
  122. max_scan = qpt->nmaps - !offset;
  123. for (i = 0;;) {
  124. if (unlikely(!map->page)) {
  125. get_map_page(qpt, map);
  126. if (unlikely(!map->page))
  127. break;
  128. }
  129. if (likely(atomic_read(&map->n_free))) {
  130. do {
  131. if (!test_and_set_bit(offset, map->page)) {
  132. atomic_dec(&map->n_free);
  133. qpt->last = qpn;
  134. ret = qpn;
  135. goto bail;
  136. }
  137. offset = find_next_offset(map, offset);
  138. qpn = mk_qpn(qpt, map, offset);
  139. /*
  140. * This test differs from alloc_pidmap().
  141. * If find_next_offset() does find a zero
  142. * bit, we don't need to check for QPN
  143. * wrapping around past our starting QPN.
  144. * We just need to be sure we don't loop
  145. * forever.
  146. */
  147. } while (offset < BITS_PER_PAGE && qpn < QPN_MAX);
  148. }
  149. /*
  150. * In order to keep the number of pages allocated to a
  151. * minimum, we scan the all existing pages before increasing
  152. * the size of the bitmap table.
  153. */
  154. if (++i > max_scan) {
  155. if (qpt->nmaps == QPNMAP_ENTRIES)
  156. break;
  157. map = &qpt->map[qpt->nmaps++];
  158. offset = 0;
  159. } else if (map < &qpt->map[qpt->nmaps]) {
  160. ++map;
  161. offset = 0;
  162. } else {
  163. map = &qpt->map[0];
  164. offset = 2;
  165. }
  166. qpn = mk_qpn(qpt, map, offset);
  167. }
  168. ret = -ENOMEM;
  169. bail:
  170. return ret;
  171. }
  172. static void free_qpn(struct ipath_qp_table *qpt, u32 qpn)
  173. {
  174. struct qpn_map *map;
  175. map = qpt->map + qpn / BITS_PER_PAGE;
  176. if (map->page)
  177. clear_bit(qpn & BITS_PER_PAGE_MASK, map->page);
  178. atomic_inc(&map->n_free);
  179. }
  180. /**
  181. * ipath_alloc_qpn - allocate a QP number
  182. * @qpt: the QP table
  183. * @qp: the QP
  184. * @type: the QP type (IB_QPT_SMI and IB_QPT_GSI are special)
  185. *
  186. * Allocate the next available QPN and put the QP into the hash table.
  187. * The hash table holds a reference to the QP.
  188. */
  189. static int ipath_alloc_qpn(struct ipath_qp_table *qpt, struct ipath_qp *qp,
  190. enum ib_qp_type type)
  191. {
  192. unsigned long flags;
  193. int ret;
  194. ret = alloc_qpn(qpt, type);
  195. if (ret < 0)
  196. goto bail;
  197. qp->ibqp.qp_num = ret;
  198. /* Add the QP to the hash table. */
  199. spin_lock_irqsave(&qpt->lock, flags);
  200. ret %= qpt->max;
  201. qp->next = qpt->table[ret];
  202. qpt->table[ret] = 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. free_qpn(qpt, qp->ibqp.qp_num);
  238. wait_event(qp->wait, !atomic_read(&qp->refcount));
  239. }
  240. /**
  241. * ipath_free_all_qps - remove all QPs from the table
  242. * @qpt: the QP table to empty
  243. */
  244. void ipath_free_all_qps(struct ipath_qp_table *qpt)
  245. {
  246. unsigned long flags;
  247. struct ipath_qp *qp, *nqp;
  248. u32 n;
  249. for (n = 0; n < qpt->max; n++) {
  250. spin_lock_irqsave(&qpt->lock, flags);
  251. qp = qpt->table[n];
  252. qpt->table[n] = NULL;
  253. spin_unlock_irqrestore(&qpt->lock, flags);
  254. while (qp) {
  255. nqp = qp->next;
  256. free_qpn(qpt, qp->ibqp.qp_num);
  257. if (!atomic_dec_and_test(&qp->refcount) ||
  258. !ipath_destroy_qp(&qp->ibqp))
  259. ipath_dbg("QP memory leak!\n");
  260. qp = nqp;
  261. }
  262. }
  263. for (n = 0; n < ARRAY_SIZE(qpt->map); n++) {
  264. if (qpt->map[n].page)
  265. free_page((unsigned long)qpt->map[n].page);
  266. }
  267. }
  268. /**
  269. * ipath_lookup_qpn - return the QP with the given QPN
  270. * @qpt: the QP table
  271. * @qpn: the QP number to look up
  272. *
  273. * The caller is responsible for decrementing the QP reference count
  274. * when done.
  275. */
  276. struct ipath_qp *ipath_lookup_qpn(struct ipath_qp_table *qpt, u32 qpn)
  277. {
  278. unsigned long flags;
  279. struct ipath_qp *qp;
  280. spin_lock_irqsave(&qpt->lock, flags);
  281. for (qp = qpt->table[qpn % qpt->max]; qp; qp = qp->next) {
  282. if (qp->ibqp.qp_num == qpn) {
  283. atomic_inc(&qp->refcount);
  284. break;
  285. }
  286. }
  287. spin_unlock_irqrestore(&qpt->lock, flags);
  288. return qp;
  289. }
  290. /**
  291. * ipath_reset_qp - initialize the QP state to the reset state
  292. * @qp: the QP to reset
  293. */
  294. static void ipath_reset_qp(struct ipath_qp *qp)
  295. {
  296. qp->remote_qpn = 0;
  297. qp->qkey = 0;
  298. qp->qp_access_flags = 0;
  299. qp->s_busy = 0;
  300. qp->s_flags &= ~IPATH_S_SIGNAL_REQ_WR;
  301. qp->s_hdrwords = 0;
  302. qp->s_psn = 0;
  303. qp->r_psn = 0;
  304. qp->r_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->r_nak_state = 0;
  314. qp->r_wrid_valid = 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->s_wait_credit = 0;
  323. memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
  324. qp->r_head_ack_queue = 0;
  325. qp->s_tail_ack_queue = 0;
  326. qp->s_num_rd_atomic = 0;
  327. if (qp->r_rq.wq) {
  328. qp->r_rq.wq->head = 0;
  329. qp->r_rq.wq->tail = 0;
  330. }
  331. qp->r_reuse_sge = 0;
  332. }
  333. /**
  334. * ipath_error_qp - put a QP into an error state
  335. * @qp: the QP to put into an error state
  336. * @err: the receive completion error to signal if a RWQE is active
  337. *
  338. * Flushes both send and receive work queues.
  339. * The QP s_lock should be held and interrupts disabled.
  340. */
  341. void ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err)
  342. {
  343. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  344. struct ib_wc wc;
  345. ipath_dbg("QP%d/%d in error state\n",
  346. qp->ibqp.qp_num, qp->remote_qpn);
  347. spin_lock(&dev->pending_lock);
  348. /* XXX What if its already removed by the timeout code? */
  349. if (!list_empty(&qp->timerwait))
  350. list_del_init(&qp->timerwait);
  351. if (!list_empty(&qp->piowait))
  352. list_del_init(&qp->piowait);
  353. spin_unlock(&dev->pending_lock);
  354. wc.vendor_err = 0;
  355. wc.byte_len = 0;
  356. wc.imm_data = 0;
  357. wc.qp = &qp->ibqp;
  358. wc.src_qp = 0;
  359. wc.wc_flags = 0;
  360. wc.pkey_index = 0;
  361. wc.slid = 0;
  362. wc.sl = 0;
  363. wc.dlid_path_bits = 0;
  364. wc.port_num = 0;
  365. if (qp->r_wrid_valid) {
  366. qp->r_wrid_valid = 0;
  367. wc.wr_id = qp->r_wr_id;
  368. wc.opcode = IB_WC_RECV;
  369. wc.status = err;
  370. ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
  371. }
  372. wc.status = IB_WC_WR_FLUSH_ERR;
  373. while (qp->s_last != qp->s_head) {
  374. struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
  375. wc.wr_id = wqe->wr.wr_id;
  376. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  377. if (++qp->s_last >= qp->s_size)
  378. qp->s_last = 0;
  379. ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
  380. }
  381. qp->s_cur = qp->s_tail = qp->s_head;
  382. qp->s_hdrwords = 0;
  383. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  384. if (qp->r_rq.wq) {
  385. struct ipath_rwq *wq;
  386. u32 head;
  387. u32 tail;
  388. spin_lock(&qp->r_rq.lock);
  389. /* sanity check pointers before trusting them */
  390. wq = qp->r_rq.wq;
  391. head = wq->head;
  392. if (head >= qp->r_rq.size)
  393. head = 0;
  394. tail = wq->tail;
  395. if (tail >= qp->r_rq.size)
  396. tail = 0;
  397. wc.opcode = IB_WC_RECV;
  398. while (tail != head) {
  399. wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  400. if (++tail >= qp->r_rq.size)
  401. tail = 0;
  402. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
  403. }
  404. wq->tail = tail;
  405. spin_unlock(&qp->r_rq.lock);
  406. }
  407. }
  408. /**
  409. * ipath_modify_qp - modify the attributes of a queue pair
  410. * @ibqp: the queue pair who's attributes we're modifying
  411. * @attr: the new attributes
  412. * @attr_mask: the mask of attributes to modify
  413. * @udata: user data for ipathverbs.so
  414. *
  415. * Returns 0 on success, otherwise returns an errno.
  416. */
  417. int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  418. int attr_mask, struct ib_udata *udata)
  419. {
  420. struct ipath_ibdev *dev = to_idev(ibqp->device);
  421. struct ipath_qp *qp = to_iqp(ibqp);
  422. enum ib_qp_state cur_state, new_state;
  423. unsigned long flags;
  424. int ret;
  425. spin_lock_irqsave(&qp->s_lock, flags);
  426. cur_state = attr_mask & IB_QP_CUR_STATE ?
  427. attr->cur_qp_state : qp->state;
  428. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  429. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  430. attr_mask))
  431. goto inval;
  432. if (attr_mask & IB_QP_AV) {
  433. if (attr->ah_attr.dlid == 0 ||
  434. attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE)
  435. goto inval;
  436. if ((attr->ah_attr.ah_flags & IB_AH_GRH) &&
  437. (attr->ah_attr.grh.sgid_index > 1))
  438. goto inval;
  439. }
  440. if (attr_mask & IB_QP_PKEY_INDEX)
  441. if (attr->pkey_index >= ipath_get_npkeys(dev->dd))
  442. goto inval;
  443. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  444. if (attr->min_rnr_timer > 31)
  445. goto inval;
  446. if (attr_mask & IB_QP_PORT)
  447. if (attr->port_num == 0 ||
  448. attr->port_num > ibqp->device->phys_port_cnt)
  449. goto inval;
  450. if (attr_mask & IB_QP_PATH_MTU)
  451. if (attr->path_mtu > IB_MTU_4096)
  452. goto inval;
  453. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  454. if (attr->max_dest_rd_atomic > 1)
  455. goto inval;
  456. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  457. if (attr->max_rd_atomic > 1)
  458. goto inval;
  459. if (attr_mask & IB_QP_PATH_MIG_STATE)
  460. if (attr->path_mig_state != IB_MIG_MIGRATED &&
  461. attr->path_mig_state != IB_MIG_REARM)
  462. goto inval;
  463. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  464. if (attr->max_dest_rd_atomic > IPATH_MAX_RDMA_ATOMIC)
  465. goto inval;
  466. switch (new_state) {
  467. case IB_QPS_RESET:
  468. ipath_reset_qp(qp);
  469. break;
  470. case IB_QPS_ERR:
  471. ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  472. break;
  473. default:
  474. break;
  475. }
  476. if (attr_mask & IB_QP_PKEY_INDEX)
  477. qp->s_pkey_index = attr->pkey_index;
  478. if (attr_mask & IB_QP_DEST_QPN)
  479. qp->remote_qpn = attr->dest_qp_num;
  480. if (attr_mask & IB_QP_SQ_PSN) {
  481. qp->s_psn = qp->s_next_psn = attr->sq_psn;
  482. qp->s_last_psn = qp->s_next_psn - 1;
  483. }
  484. if (attr_mask & IB_QP_RQ_PSN)
  485. qp->r_psn = attr->rq_psn;
  486. if (attr_mask & IB_QP_ACCESS_FLAGS)
  487. qp->qp_access_flags = attr->qp_access_flags;
  488. if (attr_mask & IB_QP_AV)
  489. qp->remote_ah_attr = attr->ah_attr;
  490. if (attr_mask & IB_QP_PATH_MTU)
  491. qp->path_mtu = attr->path_mtu;
  492. if (attr_mask & IB_QP_RETRY_CNT)
  493. qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
  494. if (attr_mask & IB_QP_RNR_RETRY) {
  495. qp->s_rnr_retry = attr->rnr_retry;
  496. if (qp->s_rnr_retry > 7)
  497. qp->s_rnr_retry = 7;
  498. qp->s_rnr_retry_cnt = qp->s_rnr_retry;
  499. }
  500. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  501. qp->r_min_rnr_timer = attr->min_rnr_timer;
  502. if (attr_mask & IB_QP_TIMEOUT)
  503. qp->timeout = attr->timeout;
  504. if (attr_mask & IB_QP_QKEY)
  505. qp->qkey = attr->qkey;
  506. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  507. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  508. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  509. qp->s_max_rd_atomic = attr->max_rd_atomic;
  510. qp->state = new_state;
  511. spin_unlock_irqrestore(&qp->s_lock, flags);
  512. ret = 0;
  513. goto bail;
  514. inval:
  515. spin_unlock_irqrestore(&qp->s_lock, flags);
  516. ret = -EINVAL;
  517. bail:
  518. return ret;
  519. }
  520. int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  521. int attr_mask, struct ib_qp_init_attr *init_attr)
  522. {
  523. struct ipath_qp *qp = to_iqp(ibqp);
  524. attr->qp_state = qp->state;
  525. attr->cur_qp_state = attr->qp_state;
  526. attr->path_mtu = qp->path_mtu;
  527. attr->path_mig_state = 0;
  528. attr->qkey = qp->qkey;
  529. attr->rq_psn = qp->r_psn;
  530. attr->sq_psn = qp->s_next_psn;
  531. attr->dest_qp_num = qp->remote_qpn;
  532. attr->qp_access_flags = qp->qp_access_flags;
  533. attr->cap.max_send_wr = qp->s_size - 1;
  534. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  535. attr->cap.max_send_sge = qp->s_max_sge;
  536. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  537. attr->cap.max_inline_data = 0;
  538. attr->ah_attr = qp->remote_ah_attr;
  539. memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
  540. attr->pkey_index = qp->s_pkey_index;
  541. attr->alt_pkey_index = 0;
  542. attr->en_sqd_async_notify = 0;
  543. attr->sq_draining = 0;
  544. attr->max_rd_atomic = qp->s_max_rd_atomic;
  545. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  546. attr->min_rnr_timer = qp->r_min_rnr_timer;
  547. attr->port_num = 1;
  548. attr->timeout = qp->timeout;
  549. attr->retry_cnt = qp->s_retry_cnt;
  550. attr->rnr_retry = qp->s_rnr_retry;
  551. attr->alt_port_num = 0;
  552. attr->alt_timeout = 0;
  553. init_attr->event_handler = qp->ibqp.event_handler;
  554. init_attr->qp_context = qp->ibqp.qp_context;
  555. init_attr->send_cq = qp->ibqp.send_cq;
  556. init_attr->recv_cq = qp->ibqp.recv_cq;
  557. init_attr->srq = qp->ibqp.srq;
  558. init_attr->cap = attr->cap;
  559. if (qp->s_flags & IPATH_S_SIGNAL_REQ_WR)
  560. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  561. else
  562. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  563. init_attr->qp_type = qp->ibqp.qp_type;
  564. init_attr->port_num = 1;
  565. return 0;
  566. }
  567. /**
  568. * ipath_compute_aeth - compute the AETH (syndrome + MSN)
  569. * @qp: the queue pair to compute the AETH for
  570. *
  571. * Returns the AETH.
  572. */
  573. __be32 ipath_compute_aeth(struct ipath_qp *qp)
  574. {
  575. u32 aeth = qp->r_msn & IPATH_MSN_MASK;
  576. if (qp->ibqp.srq) {
  577. /*
  578. * Shared receive queues don't generate credits.
  579. * Set the credit field to the invalid value.
  580. */
  581. aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT;
  582. } else {
  583. u32 min, max, x;
  584. u32 credits;
  585. struct ipath_rwq *wq = qp->r_rq.wq;
  586. u32 head;
  587. u32 tail;
  588. /* sanity check pointers before trusting them */
  589. head = wq->head;
  590. if (head >= qp->r_rq.size)
  591. head = 0;
  592. tail = wq->tail;
  593. if (tail >= qp->r_rq.size)
  594. tail = 0;
  595. /*
  596. * Compute the number of credits available (RWQEs).
  597. * XXX Not holding the r_rq.lock here so there is a small
  598. * chance that the pair of reads are not atomic.
  599. */
  600. credits = head - tail;
  601. if ((int)credits < 0)
  602. credits += qp->r_rq.size;
  603. /*
  604. * Binary search the credit table to find the code to
  605. * use.
  606. */
  607. min = 0;
  608. max = 31;
  609. for (;;) {
  610. x = (min + max) / 2;
  611. if (credit_table[x] == credits)
  612. break;
  613. if (credit_table[x] > credits)
  614. max = x;
  615. else if (min == x)
  616. break;
  617. else
  618. min = x;
  619. }
  620. aeth |= x << IPATH_AETH_CREDIT_SHIFT;
  621. }
  622. return cpu_to_be32(aeth);
  623. }
  624. /**
  625. * ipath_create_qp - create a queue pair for a device
  626. * @ibpd: the protection domain who's device we create the queue pair for
  627. * @init_attr: the attributes of the queue pair
  628. * @udata: unused by InfiniPath
  629. *
  630. * Returns the queue pair on success, otherwise returns an errno.
  631. *
  632. * Called by the ib_create_qp() core verbs function.
  633. */
  634. struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
  635. struct ib_qp_init_attr *init_attr,
  636. struct ib_udata *udata)
  637. {
  638. struct ipath_qp *qp;
  639. int err;
  640. struct ipath_swqe *swq = NULL;
  641. struct ipath_ibdev *dev;
  642. size_t sz;
  643. struct ib_qp *ret;
  644. if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
  645. init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
  646. init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs ||
  647. init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
  648. ret = ERR_PTR(-ENOMEM);
  649. goto bail;
  650. }
  651. if (init_attr->cap.max_send_sge +
  652. init_attr->cap.max_recv_sge +
  653. init_attr->cap.max_send_wr +
  654. init_attr->cap.max_recv_wr == 0) {
  655. ret = ERR_PTR(-EINVAL);
  656. goto bail;
  657. }
  658. switch (init_attr->qp_type) {
  659. case IB_QPT_UC:
  660. case IB_QPT_RC:
  661. sz = sizeof(struct ipath_sge) *
  662. init_attr->cap.max_send_sge +
  663. sizeof(struct ipath_swqe);
  664. swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
  665. if (swq == NULL) {
  666. ret = ERR_PTR(-ENOMEM);
  667. goto bail;
  668. }
  669. /* FALLTHROUGH */
  670. case IB_QPT_UD:
  671. case IB_QPT_SMI:
  672. case IB_QPT_GSI:
  673. sz = sizeof(*qp);
  674. if (init_attr->srq) {
  675. struct ipath_srq *srq = to_isrq(init_attr->srq);
  676. sz += sizeof(*qp->r_sg_list) *
  677. srq->rq.max_sge;
  678. } else
  679. sz += sizeof(*qp->r_sg_list) *
  680. init_attr->cap.max_recv_sge;
  681. qp = kmalloc(sz, GFP_KERNEL);
  682. if (!qp) {
  683. ret = ERR_PTR(-ENOMEM);
  684. goto bail_swq;
  685. }
  686. if (init_attr->srq) {
  687. sz = 0;
  688. qp->r_rq.size = 0;
  689. qp->r_rq.max_sge = 0;
  690. qp->r_rq.wq = NULL;
  691. init_attr->cap.max_recv_wr = 0;
  692. init_attr->cap.max_recv_sge = 0;
  693. } else {
  694. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  695. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  696. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  697. sizeof(struct ipath_rwqe);
  698. qp->r_rq.wq = vmalloc_user(sizeof(struct ipath_rwq) +
  699. qp->r_rq.size * sz);
  700. if (!qp->r_rq.wq) {
  701. ret = ERR_PTR(-ENOMEM);
  702. goto bail_qp;
  703. }
  704. }
  705. /*
  706. * ib_create_qp() will initialize qp->ibqp
  707. * except for qp->ibqp.qp_num.
  708. */
  709. spin_lock_init(&qp->s_lock);
  710. spin_lock_init(&qp->r_rq.lock);
  711. atomic_set(&qp->refcount, 0);
  712. init_waitqueue_head(&qp->wait);
  713. tasklet_init(&qp->s_task, ipath_do_ruc_send,
  714. (unsigned long)qp);
  715. INIT_LIST_HEAD(&qp->piowait);
  716. INIT_LIST_HEAD(&qp->timerwait);
  717. qp->state = IB_QPS_RESET;
  718. qp->s_wq = swq;
  719. qp->s_size = init_attr->cap.max_send_wr + 1;
  720. qp->s_max_sge = init_attr->cap.max_send_sge;
  721. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  722. qp->s_flags = IPATH_S_SIGNAL_REQ_WR;
  723. else
  724. qp->s_flags = 0;
  725. dev = to_idev(ibpd->device);
  726. err = ipath_alloc_qpn(&dev->qp_table, qp,
  727. init_attr->qp_type);
  728. if (err) {
  729. ret = ERR_PTR(err);
  730. goto bail_rwq;
  731. }
  732. qp->ip = NULL;
  733. ipath_reset_qp(qp);
  734. break;
  735. default:
  736. /* Don't support raw QPs */
  737. ret = ERR_PTR(-ENOSYS);
  738. goto bail;
  739. }
  740. init_attr->cap.max_inline_data = 0;
  741. /*
  742. * Return the address of the RWQ as the offset to mmap.
  743. * See ipath_mmap() for details.
  744. */
  745. if (udata && udata->outlen >= sizeof(__u64)) {
  746. int err;
  747. if (!qp->r_rq.wq) {
  748. __u64 offset = 0;
  749. err = ib_copy_to_udata(udata, &offset,
  750. sizeof(offset));
  751. if (err) {
  752. ret = ERR_PTR(err);
  753. goto bail_rwq;
  754. }
  755. } else {
  756. u32 s = sizeof(struct ipath_rwq) +
  757. qp->r_rq.size * sz;
  758. qp->ip =
  759. ipath_create_mmap_info(dev, s,
  760. ibpd->uobject->context,
  761. qp->r_rq.wq);
  762. if (!qp->ip) {
  763. ret = ERR_PTR(-ENOMEM);
  764. goto bail_rwq;
  765. }
  766. err = ib_copy_to_udata(udata, &(qp->ip->offset),
  767. sizeof(qp->ip->offset));
  768. if (err) {
  769. ret = ERR_PTR(err);
  770. goto bail_ip;
  771. }
  772. }
  773. }
  774. spin_lock(&dev->n_qps_lock);
  775. if (dev->n_qps_allocated == ib_ipath_max_qps) {
  776. spin_unlock(&dev->n_qps_lock);
  777. ret = ERR_PTR(-ENOMEM);
  778. goto bail_ip;
  779. }
  780. dev->n_qps_allocated++;
  781. spin_unlock(&dev->n_qps_lock);
  782. if (qp->ip) {
  783. spin_lock_irq(&dev->pending_lock);
  784. list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
  785. spin_unlock_irq(&dev->pending_lock);
  786. }
  787. ret = &qp->ibqp;
  788. goto bail;
  789. bail_ip:
  790. kfree(qp->ip);
  791. bail_rwq:
  792. vfree(qp->r_rq.wq);
  793. bail_qp:
  794. kfree(qp);
  795. bail_swq:
  796. vfree(swq);
  797. bail:
  798. return ret;
  799. }
  800. /**
  801. * ipath_destroy_qp - destroy a queue pair
  802. * @ibqp: the queue pair to destroy
  803. *
  804. * Returns 0 on success.
  805. *
  806. * Note that this can be called while the QP is actively sending or
  807. * receiving!
  808. */
  809. int ipath_destroy_qp(struct ib_qp *ibqp)
  810. {
  811. struct ipath_qp *qp = to_iqp(ibqp);
  812. struct ipath_ibdev *dev = to_idev(ibqp->device);
  813. unsigned long flags;
  814. spin_lock_irqsave(&qp->s_lock, flags);
  815. qp->state = IB_QPS_ERR;
  816. spin_unlock_irqrestore(&qp->s_lock, flags);
  817. spin_lock(&dev->n_qps_lock);
  818. dev->n_qps_allocated--;
  819. spin_unlock(&dev->n_qps_lock);
  820. /* Stop the sending tasklet. */
  821. tasklet_kill(&qp->s_task);
  822. /* Make sure the QP isn't on the timeout list. */
  823. spin_lock_irqsave(&dev->pending_lock, flags);
  824. if (!list_empty(&qp->timerwait))
  825. list_del_init(&qp->timerwait);
  826. if (!list_empty(&qp->piowait))
  827. list_del_init(&qp->piowait);
  828. spin_unlock_irqrestore(&dev->pending_lock, flags);
  829. /*
  830. * Make sure that the QP is not in the QPN table so receive
  831. * interrupts will discard packets for this QP. XXX Also remove QP
  832. * from multicast table.
  833. */
  834. if (atomic_read(&qp->refcount) != 0)
  835. ipath_free_qp(&dev->qp_table, qp);
  836. if (qp->ip)
  837. kref_put(&qp->ip->ref, ipath_release_mmap_info);
  838. else
  839. vfree(qp->r_rq.wq);
  840. vfree(qp->s_wq);
  841. kfree(qp);
  842. return 0;
  843. }
  844. /**
  845. * ipath_init_qp_table - initialize the QP table for a device
  846. * @idev: the device who's QP table we're initializing
  847. * @size: the size of the QP table
  848. *
  849. * Returns 0 on success, otherwise returns an errno.
  850. */
  851. int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
  852. {
  853. int i;
  854. int ret;
  855. idev->qp_table.last = 1; /* QPN 0 and 1 are special. */
  856. idev->qp_table.max = size;
  857. idev->qp_table.nmaps = 1;
  858. idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
  859. GFP_KERNEL);
  860. if (idev->qp_table.table == NULL) {
  861. ret = -ENOMEM;
  862. goto bail;
  863. }
  864. for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
  865. atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
  866. idev->qp_table.map[i].page = NULL;
  867. }
  868. ret = 0;
  869. bail:
  870. return ret;
  871. }
  872. /**
  873. * ipath_sqerror_qp - put a QP's send queue into an error state
  874. * @qp: QP who's send queue will be put into an error state
  875. * @wc: the WC responsible for putting the QP in this state
  876. *
  877. * Flushes the send work queue.
  878. * The QP s_lock should be held and interrupts disabled.
  879. */
  880. void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
  881. {
  882. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  883. struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
  884. ipath_dbg("Send queue error on QP%d/%d: err: %d\n",
  885. qp->ibqp.qp_num, qp->remote_qpn, wc->status);
  886. spin_lock(&dev->pending_lock);
  887. /* XXX What if its already removed by the timeout code? */
  888. if (!list_empty(&qp->timerwait))
  889. list_del_init(&qp->timerwait);
  890. if (!list_empty(&qp->piowait))
  891. list_del_init(&qp->piowait);
  892. spin_unlock(&dev->pending_lock);
  893. ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
  894. if (++qp->s_last >= qp->s_size)
  895. qp->s_last = 0;
  896. wc->status = IB_WC_WR_FLUSH_ERR;
  897. while (qp->s_last != qp->s_head) {
  898. wqe = get_swqe_ptr(qp, qp->s_last);
  899. wc->wr_id = wqe->wr.wr_id;
  900. wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  901. ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
  902. if (++qp->s_last >= qp->s_size)
  903. qp->s_last = 0;
  904. }
  905. qp->s_cur = qp->s_tail = qp->s_head;
  906. qp->state = IB_QPS_SQE;
  907. }
  908. /**
  909. * ipath_get_credit - flush the send work queue of a QP
  910. * @qp: the qp who's send work queue to flush
  911. * @aeth: the Acknowledge Extended Transport Header
  912. *
  913. * The QP s_lock should be held.
  914. */
  915. void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
  916. {
  917. u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK;
  918. /*
  919. * If the credit is invalid, we can send
  920. * as many packets as we like. Otherwise, we have to
  921. * honor the credit field.
  922. */
  923. if (credit == IPATH_AETH_CREDIT_INVAL)
  924. qp->s_lsn = (u32) -1;
  925. else if (qp->s_lsn != (u32) -1) {
  926. /* Compute new LSN (i.e., MSN + credit) */
  927. credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK;
  928. if (ipath_cmp24(credit, qp->s_lsn) > 0)
  929. qp->s_lsn = credit;
  930. }
  931. /* Restart sending if it was blocked due to lack of credits. */
  932. if (qp->s_cur != qp->s_head &&
  933. (qp->s_lsn == (u32) -1 ||
  934. ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
  935. qp->s_lsn + 1) <= 0))
  936. tasklet_hi_schedule(&qp->s_task);
  937. }