ipath_qp.c 24 KB

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