ipath_qp.c 25 KB

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