ipath_qp.c 22 KB

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