ipath_qp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. /*
  2. * Copyright (c) 2006, 2007 QLogic Corporation. 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_wqe = NULL;
  303. qp->s_psn = 0;
  304. qp->r_psn = 0;
  305. qp->r_msn = 0;
  306. if (qp->ibqp.qp_type == IB_QPT_RC) {
  307. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  308. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  309. } else {
  310. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  311. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  312. }
  313. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  314. qp->r_nak_state = 0;
  315. qp->r_wrid_valid = 0;
  316. qp->s_rnr_timeout = 0;
  317. qp->s_head = 0;
  318. qp->s_tail = 0;
  319. qp->s_cur = 0;
  320. qp->s_last = 0;
  321. qp->s_ssn = 1;
  322. qp->s_lsn = 0;
  323. qp->s_wait_credit = 0;
  324. memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
  325. qp->r_head_ack_queue = 0;
  326. qp->s_tail_ack_queue = 0;
  327. qp->s_num_rd_atomic = 0;
  328. if (qp->r_rq.wq) {
  329. qp->r_rq.wq->head = 0;
  330. qp->r_rq.wq->tail = 0;
  331. }
  332. qp->r_reuse_sge = 0;
  333. }
  334. /**
  335. * ipath_error_qp - put a QP into an error state
  336. * @qp: the QP to put into an error state
  337. * @err: the receive completion error to signal if a RWQE is active
  338. *
  339. * Flushes both send and receive work queues.
  340. * Returns true if last WQE event should be generated.
  341. * The QP s_lock should be held and interrupts disabled.
  342. */
  343. int ipath_error_qp(struct ipath_qp *qp, enum ib_wc_status err)
  344. {
  345. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  346. struct ib_wc wc;
  347. int ret = 0;
  348. ipath_dbg("QP%d/%d in error state\n",
  349. qp->ibqp.qp_num, qp->remote_qpn);
  350. spin_lock(&dev->pending_lock);
  351. /* XXX What if its already removed by the timeout code? */
  352. if (!list_empty(&qp->timerwait))
  353. list_del_init(&qp->timerwait);
  354. if (!list_empty(&qp->piowait))
  355. list_del_init(&qp->piowait);
  356. spin_unlock(&dev->pending_lock);
  357. wc.vendor_err = 0;
  358. wc.byte_len = 0;
  359. wc.imm_data = 0;
  360. wc.qp = &qp->ibqp;
  361. wc.src_qp = 0;
  362. wc.wc_flags = 0;
  363. wc.pkey_index = 0;
  364. wc.slid = 0;
  365. wc.sl = 0;
  366. wc.dlid_path_bits = 0;
  367. wc.port_num = 0;
  368. if (qp->r_wrid_valid) {
  369. qp->r_wrid_valid = 0;
  370. wc.wr_id = qp->r_wr_id;
  371. wc.opcode = IB_WC_RECV;
  372. wc.status = err;
  373. ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
  374. }
  375. wc.status = IB_WC_WR_FLUSH_ERR;
  376. while (qp->s_last != qp->s_head) {
  377. struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
  378. wc.wr_id = wqe->wr.wr_id;
  379. wc.opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  380. if (++qp->s_last >= qp->s_size)
  381. qp->s_last = 0;
  382. ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
  383. }
  384. qp->s_cur = qp->s_tail = qp->s_head;
  385. qp->s_hdrwords = 0;
  386. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  387. if (qp->r_rq.wq) {
  388. struct ipath_rwq *wq;
  389. u32 head;
  390. u32 tail;
  391. spin_lock(&qp->r_rq.lock);
  392. /* sanity check pointers before trusting them */
  393. wq = qp->r_rq.wq;
  394. head = wq->head;
  395. if (head >= qp->r_rq.size)
  396. head = 0;
  397. tail = wq->tail;
  398. if (tail >= qp->r_rq.size)
  399. tail = 0;
  400. wc.opcode = IB_WC_RECV;
  401. while (tail != head) {
  402. wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  403. if (++tail >= qp->r_rq.size)
  404. tail = 0;
  405. ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
  406. }
  407. wq->tail = tail;
  408. spin_unlock(&qp->r_rq.lock);
  409. } else if (qp->ibqp.event_handler)
  410. ret = 1;
  411. return ret;
  412. }
  413. /**
  414. * ipath_modify_qp - modify the attributes of a queue pair
  415. * @ibqp: the queue pair who's attributes we're modifying
  416. * @attr: the new attributes
  417. * @attr_mask: the mask of attributes to modify
  418. * @udata: user data for ipathverbs.so
  419. *
  420. * Returns 0 on success, otherwise returns an errno.
  421. */
  422. int ipath_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  423. int attr_mask, struct ib_udata *udata)
  424. {
  425. struct ipath_ibdev *dev = to_idev(ibqp->device);
  426. struct ipath_qp *qp = to_iqp(ibqp);
  427. enum ib_qp_state cur_state, new_state;
  428. unsigned long flags;
  429. int lastwqe = 0;
  430. int ret;
  431. spin_lock_irqsave(&qp->s_lock, flags);
  432. cur_state = attr_mask & IB_QP_CUR_STATE ?
  433. attr->cur_qp_state : qp->state;
  434. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  435. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  436. attr_mask))
  437. goto inval;
  438. if (attr_mask & IB_QP_AV) {
  439. if (attr->ah_attr.dlid == 0 ||
  440. attr->ah_attr.dlid >= IPATH_MULTICAST_LID_BASE)
  441. goto inval;
  442. if ((attr->ah_attr.ah_flags & IB_AH_GRH) &&
  443. (attr->ah_attr.grh.sgid_index > 1))
  444. goto inval;
  445. }
  446. if (attr_mask & IB_QP_PKEY_INDEX)
  447. if (attr->pkey_index >= ipath_get_npkeys(dev->dd))
  448. goto inval;
  449. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  450. if (attr->min_rnr_timer > 31)
  451. goto inval;
  452. if (attr_mask & IB_QP_PORT)
  453. if (attr->port_num == 0 ||
  454. attr->port_num > ibqp->device->phys_port_cnt)
  455. goto inval;
  456. /*
  457. * Note: the chips support a maximum MTU of 4096, but the driver
  458. * hasn't implemented this feature yet, so don't allow Path MTU
  459. * values greater than 2048.
  460. */
  461. if (attr_mask & IB_QP_PATH_MTU)
  462. if (attr->path_mtu > IB_MTU_2048)
  463. goto inval;
  464. if (attr_mask & IB_QP_PATH_MIG_STATE)
  465. if (attr->path_mig_state != IB_MIG_MIGRATED &&
  466. attr->path_mig_state != IB_MIG_REARM)
  467. goto inval;
  468. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  469. if (attr->max_dest_rd_atomic > IPATH_MAX_RDMA_ATOMIC)
  470. goto inval;
  471. switch (new_state) {
  472. case IB_QPS_RESET:
  473. ipath_reset_qp(qp);
  474. break;
  475. case IB_QPS_ERR:
  476. lastwqe = ipath_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  477. break;
  478. default:
  479. break;
  480. }
  481. if (attr_mask & IB_QP_PKEY_INDEX)
  482. qp->s_pkey_index = attr->pkey_index;
  483. if (attr_mask & IB_QP_DEST_QPN)
  484. qp->remote_qpn = attr->dest_qp_num;
  485. if (attr_mask & IB_QP_SQ_PSN) {
  486. qp->s_psn = qp->s_next_psn = attr->sq_psn;
  487. qp->s_last_psn = qp->s_next_psn - 1;
  488. }
  489. if (attr_mask & IB_QP_RQ_PSN)
  490. qp->r_psn = attr->rq_psn;
  491. if (attr_mask & IB_QP_ACCESS_FLAGS)
  492. qp->qp_access_flags = attr->qp_access_flags;
  493. if (attr_mask & IB_QP_AV)
  494. qp->remote_ah_attr = attr->ah_attr;
  495. if (attr_mask & IB_QP_PATH_MTU)
  496. qp->path_mtu = attr->path_mtu;
  497. if (attr_mask & IB_QP_RETRY_CNT)
  498. qp->s_retry = qp->s_retry_cnt = attr->retry_cnt;
  499. if (attr_mask & IB_QP_RNR_RETRY) {
  500. qp->s_rnr_retry = attr->rnr_retry;
  501. if (qp->s_rnr_retry > 7)
  502. qp->s_rnr_retry = 7;
  503. qp->s_rnr_retry_cnt = qp->s_rnr_retry;
  504. }
  505. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  506. qp->r_min_rnr_timer = attr->min_rnr_timer;
  507. if (attr_mask & IB_QP_TIMEOUT)
  508. qp->timeout = attr->timeout;
  509. if (attr_mask & IB_QP_QKEY)
  510. qp->qkey = attr->qkey;
  511. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  512. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  513. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  514. qp->s_max_rd_atomic = attr->max_rd_atomic;
  515. qp->state = new_state;
  516. spin_unlock_irqrestore(&qp->s_lock, flags);
  517. if (lastwqe) {
  518. struct ib_event ev;
  519. ev.device = qp->ibqp.device;
  520. ev.element.qp = &qp->ibqp;
  521. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  522. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  523. }
  524. ret = 0;
  525. goto bail;
  526. inval:
  527. spin_unlock_irqrestore(&qp->s_lock, flags);
  528. ret = -EINVAL;
  529. bail:
  530. return ret;
  531. }
  532. int ipath_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  533. int attr_mask, struct ib_qp_init_attr *init_attr)
  534. {
  535. struct ipath_qp *qp = to_iqp(ibqp);
  536. attr->qp_state = qp->state;
  537. attr->cur_qp_state = attr->qp_state;
  538. attr->path_mtu = qp->path_mtu;
  539. attr->path_mig_state = 0;
  540. attr->qkey = qp->qkey;
  541. attr->rq_psn = qp->r_psn;
  542. attr->sq_psn = qp->s_next_psn;
  543. attr->dest_qp_num = qp->remote_qpn;
  544. attr->qp_access_flags = qp->qp_access_flags;
  545. attr->cap.max_send_wr = qp->s_size - 1;
  546. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  547. attr->cap.max_send_sge = qp->s_max_sge;
  548. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  549. attr->cap.max_inline_data = 0;
  550. attr->ah_attr = qp->remote_ah_attr;
  551. memset(&attr->alt_ah_attr, 0, sizeof(attr->alt_ah_attr));
  552. attr->pkey_index = qp->s_pkey_index;
  553. attr->alt_pkey_index = 0;
  554. attr->en_sqd_async_notify = 0;
  555. attr->sq_draining = 0;
  556. attr->max_rd_atomic = qp->s_max_rd_atomic;
  557. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  558. attr->min_rnr_timer = qp->r_min_rnr_timer;
  559. attr->port_num = 1;
  560. attr->timeout = qp->timeout;
  561. attr->retry_cnt = qp->s_retry_cnt;
  562. attr->rnr_retry = qp->s_rnr_retry;
  563. attr->alt_port_num = 0;
  564. attr->alt_timeout = 0;
  565. init_attr->event_handler = qp->ibqp.event_handler;
  566. init_attr->qp_context = qp->ibqp.qp_context;
  567. init_attr->send_cq = qp->ibqp.send_cq;
  568. init_attr->recv_cq = qp->ibqp.recv_cq;
  569. init_attr->srq = qp->ibqp.srq;
  570. init_attr->cap = attr->cap;
  571. if (qp->s_flags & IPATH_S_SIGNAL_REQ_WR)
  572. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  573. else
  574. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  575. init_attr->qp_type = qp->ibqp.qp_type;
  576. init_attr->port_num = 1;
  577. return 0;
  578. }
  579. /**
  580. * ipath_compute_aeth - compute the AETH (syndrome + MSN)
  581. * @qp: the queue pair to compute the AETH for
  582. *
  583. * Returns the AETH.
  584. */
  585. __be32 ipath_compute_aeth(struct ipath_qp *qp)
  586. {
  587. u32 aeth = qp->r_msn & IPATH_MSN_MASK;
  588. if (qp->ibqp.srq) {
  589. /*
  590. * Shared receive queues don't generate credits.
  591. * Set the credit field to the invalid value.
  592. */
  593. aeth |= IPATH_AETH_CREDIT_INVAL << IPATH_AETH_CREDIT_SHIFT;
  594. } else {
  595. u32 min, max, x;
  596. u32 credits;
  597. struct ipath_rwq *wq = qp->r_rq.wq;
  598. u32 head;
  599. u32 tail;
  600. /* sanity check pointers before trusting them */
  601. head = wq->head;
  602. if (head >= qp->r_rq.size)
  603. head = 0;
  604. tail = wq->tail;
  605. if (tail >= qp->r_rq.size)
  606. tail = 0;
  607. /*
  608. * Compute the number of credits available (RWQEs).
  609. * XXX Not holding the r_rq.lock here so there is a small
  610. * chance that the pair of reads are not atomic.
  611. */
  612. credits = head - tail;
  613. if ((int)credits < 0)
  614. credits += qp->r_rq.size;
  615. /*
  616. * Binary search the credit table to find the code to
  617. * use.
  618. */
  619. min = 0;
  620. max = 31;
  621. for (;;) {
  622. x = (min + max) / 2;
  623. if (credit_table[x] == credits)
  624. break;
  625. if (credit_table[x] > credits)
  626. max = x;
  627. else if (min == x)
  628. break;
  629. else
  630. min = x;
  631. }
  632. aeth |= x << IPATH_AETH_CREDIT_SHIFT;
  633. }
  634. return cpu_to_be32(aeth);
  635. }
  636. /**
  637. * ipath_create_qp - create a queue pair for a device
  638. * @ibpd: the protection domain who's device we create the queue pair for
  639. * @init_attr: the attributes of the queue pair
  640. * @udata: unused by InfiniPath
  641. *
  642. * Returns the queue pair on success, otherwise returns an errno.
  643. *
  644. * Called by the ib_create_qp() core verbs function.
  645. */
  646. struct ib_qp *ipath_create_qp(struct ib_pd *ibpd,
  647. struct ib_qp_init_attr *init_attr,
  648. struct ib_udata *udata)
  649. {
  650. struct ipath_qp *qp;
  651. int err;
  652. struct ipath_swqe *swq = NULL;
  653. struct ipath_ibdev *dev;
  654. size_t sz;
  655. struct ib_qp *ret;
  656. if (init_attr->cap.max_send_sge > ib_ipath_max_sges ||
  657. init_attr->cap.max_recv_sge > ib_ipath_max_sges ||
  658. init_attr->cap.max_send_wr > ib_ipath_max_qp_wrs ||
  659. init_attr->cap.max_recv_wr > ib_ipath_max_qp_wrs) {
  660. ret = ERR_PTR(-ENOMEM);
  661. goto bail;
  662. }
  663. if (init_attr->cap.max_send_sge +
  664. init_attr->cap.max_recv_sge +
  665. init_attr->cap.max_send_wr +
  666. init_attr->cap.max_recv_wr == 0) {
  667. ret = ERR_PTR(-EINVAL);
  668. goto bail;
  669. }
  670. switch (init_attr->qp_type) {
  671. case IB_QPT_UC:
  672. case IB_QPT_RC:
  673. case IB_QPT_UD:
  674. case IB_QPT_SMI:
  675. case IB_QPT_GSI:
  676. sz = sizeof(struct ipath_sge) *
  677. init_attr->cap.max_send_sge +
  678. sizeof(struct ipath_swqe);
  679. swq = vmalloc((init_attr->cap.max_send_wr + 1) * sz);
  680. if (swq == NULL) {
  681. ret = ERR_PTR(-ENOMEM);
  682. goto bail;
  683. }
  684. sz = sizeof(*qp);
  685. if (init_attr->srq) {
  686. struct ipath_srq *srq = to_isrq(init_attr->srq);
  687. sz += sizeof(*qp->r_sg_list) *
  688. srq->rq.max_sge;
  689. } else
  690. sz += sizeof(*qp->r_sg_list) *
  691. init_attr->cap.max_recv_sge;
  692. qp = kmalloc(sz, GFP_KERNEL);
  693. if (!qp) {
  694. ret = ERR_PTR(-ENOMEM);
  695. goto bail_swq;
  696. }
  697. if (init_attr->srq) {
  698. sz = 0;
  699. qp->r_rq.size = 0;
  700. qp->r_rq.max_sge = 0;
  701. qp->r_rq.wq = NULL;
  702. init_attr->cap.max_recv_wr = 0;
  703. init_attr->cap.max_recv_sge = 0;
  704. } else {
  705. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  706. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  707. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  708. sizeof(struct ipath_rwqe);
  709. qp->r_rq.wq = vmalloc_user(sizeof(struct ipath_rwq) +
  710. qp->r_rq.size * sz);
  711. if (!qp->r_rq.wq) {
  712. ret = ERR_PTR(-ENOMEM);
  713. goto bail_qp;
  714. }
  715. }
  716. /*
  717. * ib_create_qp() will initialize qp->ibqp
  718. * except for qp->ibqp.qp_num.
  719. */
  720. spin_lock_init(&qp->s_lock);
  721. spin_lock_init(&qp->r_rq.lock);
  722. atomic_set(&qp->refcount, 0);
  723. init_waitqueue_head(&qp->wait);
  724. tasklet_init(&qp->s_task, ipath_do_send, (unsigned long)qp);
  725. INIT_LIST_HEAD(&qp->piowait);
  726. INIT_LIST_HEAD(&qp->timerwait);
  727. qp->state = IB_QPS_RESET;
  728. qp->s_wq = swq;
  729. qp->s_size = init_attr->cap.max_send_wr + 1;
  730. qp->s_max_sge = init_attr->cap.max_send_sge;
  731. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  732. qp->s_flags = IPATH_S_SIGNAL_REQ_WR;
  733. else
  734. qp->s_flags = 0;
  735. dev = to_idev(ibpd->device);
  736. err = ipath_alloc_qpn(&dev->qp_table, qp,
  737. init_attr->qp_type);
  738. if (err) {
  739. ret = ERR_PTR(err);
  740. goto bail_rwq;
  741. }
  742. qp->ip = NULL;
  743. ipath_reset_qp(qp);
  744. break;
  745. default:
  746. /* Don't support raw QPs */
  747. ret = ERR_PTR(-ENOSYS);
  748. goto bail;
  749. }
  750. init_attr->cap.max_inline_data = 0;
  751. /*
  752. * Return the address of the RWQ as the offset to mmap.
  753. * See ipath_mmap() for details.
  754. */
  755. if (udata && udata->outlen >= sizeof(__u64)) {
  756. int err;
  757. if (!qp->r_rq.wq) {
  758. __u64 offset = 0;
  759. err = ib_copy_to_udata(udata, &offset,
  760. sizeof(offset));
  761. if (err) {
  762. ret = ERR_PTR(err);
  763. goto bail_rwq;
  764. }
  765. } else {
  766. u32 s = sizeof(struct ipath_rwq) +
  767. qp->r_rq.size * sz;
  768. qp->ip =
  769. ipath_create_mmap_info(dev, s,
  770. ibpd->uobject->context,
  771. qp->r_rq.wq);
  772. if (!qp->ip) {
  773. ret = ERR_PTR(-ENOMEM);
  774. goto bail_rwq;
  775. }
  776. err = ib_copy_to_udata(udata, &(qp->ip->offset),
  777. sizeof(qp->ip->offset));
  778. if (err) {
  779. ret = ERR_PTR(err);
  780. goto bail_ip;
  781. }
  782. }
  783. }
  784. spin_lock(&dev->n_qps_lock);
  785. if (dev->n_qps_allocated == ib_ipath_max_qps) {
  786. spin_unlock(&dev->n_qps_lock);
  787. ret = ERR_PTR(-ENOMEM);
  788. goto bail_ip;
  789. }
  790. dev->n_qps_allocated++;
  791. spin_unlock(&dev->n_qps_lock);
  792. if (qp->ip) {
  793. spin_lock_irq(&dev->pending_lock);
  794. list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
  795. spin_unlock_irq(&dev->pending_lock);
  796. }
  797. ret = &qp->ibqp;
  798. goto bail;
  799. bail_ip:
  800. kfree(qp->ip);
  801. bail_rwq:
  802. vfree(qp->r_rq.wq);
  803. bail_qp:
  804. kfree(qp);
  805. bail_swq:
  806. vfree(swq);
  807. bail:
  808. return ret;
  809. }
  810. /**
  811. * ipath_destroy_qp - destroy a queue pair
  812. * @ibqp: the queue pair to destroy
  813. *
  814. * Returns 0 on success.
  815. *
  816. * Note that this can be called while the QP is actively sending or
  817. * receiving!
  818. */
  819. int ipath_destroy_qp(struct ib_qp *ibqp)
  820. {
  821. struct ipath_qp *qp = to_iqp(ibqp);
  822. struct ipath_ibdev *dev = to_idev(ibqp->device);
  823. unsigned long flags;
  824. spin_lock_irqsave(&qp->s_lock, flags);
  825. qp->state = IB_QPS_ERR;
  826. spin_unlock_irqrestore(&qp->s_lock, flags);
  827. spin_lock(&dev->n_qps_lock);
  828. dev->n_qps_allocated--;
  829. spin_unlock(&dev->n_qps_lock);
  830. /* Stop the sending tasklet. */
  831. tasklet_kill(&qp->s_task);
  832. /* Make sure the QP isn't on the timeout list. */
  833. spin_lock_irqsave(&dev->pending_lock, flags);
  834. if (!list_empty(&qp->timerwait))
  835. list_del_init(&qp->timerwait);
  836. if (!list_empty(&qp->piowait))
  837. list_del_init(&qp->piowait);
  838. spin_unlock_irqrestore(&dev->pending_lock, flags);
  839. /*
  840. * Make sure that the QP is not in the QPN table so receive
  841. * interrupts will discard packets for this QP. XXX Also remove QP
  842. * from multicast table.
  843. */
  844. if (atomic_read(&qp->refcount) != 0)
  845. ipath_free_qp(&dev->qp_table, qp);
  846. if (qp->ip)
  847. kref_put(&qp->ip->ref, ipath_release_mmap_info);
  848. else
  849. vfree(qp->r_rq.wq);
  850. vfree(qp->s_wq);
  851. kfree(qp);
  852. return 0;
  853. }
  854. /**
  855. * ipath_init_qp_table - initialize the QP table for a device
  856. * @idev: the device who's QP table we're initializing
  857. * @size: the size of the QP table
  858. *
  859. * Returns 0 on success, otherwise returns an errno.
  860. */
  861. int ipath_init_qp_table(struct ipath_ibdev *idev, int size)
  862. {
  863. int i;
  864. int ret;
  865. idev->qp_table.last = 1; /* QPN 0 and 1 are special. */
  866. idev->qp_table.max = size;
  867. idev->qp_table.nmaps = 1;
  868. idev->qp_table.table = kzalloc(size * sizeof(*idev->qp_table.table),
  869. GFP_KERNEL);
  870. if (idev->qp_table.table == NULL) {
  871. ret = -ENOMEM;
  872. goto bail;
  873. }
  874. for (i = 0; i < ARRAY_SIZE(idev->qp_table.map); i++) {
  875. atomic_set(&idev->qp_table.map[i].n_free, BITS_PER_PAGE);
  876. idev->qp_table.map[i].page = NULL;
  877. }
  878. ret = 0;
  879. bail:
  880. return ret;
  881. }
  882. /**
  883. * ipath_sqerror_qp - put a QP's send queue into an error state
  884. * @qp: QP who's send queue will be put into an error state
  885. * @wc: the WC responsible for putting the QP in this state
  886. *
  887. * Flushes the send work queue.
  888. * The QP s_lock should be held and interrupts disabled.
  889. */
  890. void ipath_sqerror_qp(struct ipath_qp *qp, struct ib_wc *wc)
  891. {
  892. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  893. struct ipath_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
  894. ipath_dbg("Send queue error on QP%d/%d: err: %d\n",
  895. qp->ibqp.qp_num, qp->remote_qpn, wc->status);
  896. spin_lock(&dev->pending_lock);
  897. /* XXX What if its already removed by the timeout code? */
  898. if (!list_empty(&qp->timerwait))
  899. list_del_init(&qp->timerwait);
  900. if (!list_empty(&qp->piowait))
  901. list_del_init(&qp->piowait);
  902. spin_unlock(&dev->pending_lock);
  903. ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
  904. if (++qp->s_last >= qp->s_size)
  905. qp->s_last = 0;
  906. wc->status = IB_WC_WR_FLUSH_ERR;
  907. while (qp->s_last != qp->s_head) {
  908. wqe = get_swqe_ptr(qp, qp->s_last);
  909. wc->wr_id = wqe->wr.wr_id;
  910. wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
  911. ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 1);
  912. if (++qp->s_last >= qp->s_size)
  913. qp->s_last = 0;
  914. }
  915. qp->s_cur = qp->s_tail = qp->s_head;
  916. qp->state = IB_QPS_SQE;
  917. }
  918. /**
  919. * ipath_get_credit - flush the send work queue of a QP
  920. * @qp: the qp who's send work queue to flush
  921. * @aeth: the Acknowledge Extended Transport Header
  922. *
  923. * The QP s_lock should be held.
  924. */
  925. void ipath_get_credit(struct ipath_qp *qp, u32 aeth)
  926. {
  927. u32 credit = (aeth >> IPATH_AETH_CREDIT_SHIFT) & IPATH_AETH_CREDIT_MASK;
  928. /*
  929. * If the credit is invalid, we can send
  930. * as many packets as we like. Otherwise, we have to
  931. * honor the credit field.
  932. */
  933. if (credit == IPATH_AETH_CREDIT_INVAL)
  934. qp->s_lsn = (u32) -1;
  935. else if (qp->s_lsn != (u32) -1) {
  936. /* Compute new LSN (i.e., MSN + credit) */
  937. credit = (aeth + credit_table[credit]) & IPATH_MSN_MASK;
  938. if (ipath_cmp24(credit, qp->s_lsn) > 0)
  939. qp->s_lsn = credit;
  940. }
  941. /* Restart sending if it was blocked due to lack of credits. */
  942. if (qp->s_cur != qp->s_head &&
  943. (qp->s_lsn == (u32) -1 ||
  944. ipath_cmp24(get_swqe_ptr(qp, qp->s_cur)->ssn,
  945. qp->s_lsn + 1) <= 0))
  946. tasklet_hi_schedule(&qp->s_task);
  947. }