ipath_qp.c 26 KB

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