ipath_qp.c 25 KB

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