ipath_qp.c 25 KB

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