ipath_qp.c 26 KB

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