ipath_qp.c 25 KB

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