ipath_qp.c 26 KB

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