qib_qp.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /*
  2. * Copyright (c) 2012, 2013 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2006 - 2012 QLogic Corporation. * All rights reserved.
  4. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/err.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/jhash.h>
  37. #ifdef CONFIG_DEBUG_FS
  38. #include <linux/seq_file.h>
  39. #endif
  40. #include "qib.h"
  41. #define RVT_BITS_PER_PAGE (PAGE_SIZE*BITS_PER_BYTE)
  42. #define RVT_BITS_PER_PAGE_MASK (RVT_BITS_PER_PAGE-1)
  43. static inline unsigned mk_qpn(struct qib_qpn_table *qpt,
  44. struct qpn_map *map, unsigned off)
  45. {
  46. return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
  47. }
  48. static inline unsigned find_next_offset(struct qib_qpn_table *qpt,
  49. struct qpn_map *map, unsigned off,
  50. unsigned n)
  51. {
  52. if (qpt->mask) {
  53. off++;
  54. if (((off & qpt->mask) >> 1) >= n)
  55. off = (off | qpt->mask) + 2;
  56. } else
  57. off = find_next_zero_bit(map->page, RVT_BITS_PER_PAGE, off);
  58. return off;
  59. }
  60. /*
  61. * Convert the AETH credit code into the number of credits.
  62. */
  63. static u32 credit_table[31] = {
  64. 0, /* 0 */
  65. 1, /* 1 */
  66. 2, /* 2 */
  67. 3, /* 3 */
  68. 4, /* 4 */
  69. 6, /* 5 */
  70. 8, /* 6 */
  71. 12, /* 7 */
  72. 16, /* 8 */
  73. 24, /* 9 */
  74. 32, /* A */
  75. 48, /* B */
  76. 64, /* C */
  77. 96, /* D */
  78. 128, /* E */
  79. 192, /* F */
  80. 256, /* 10 */
  81. 384, /* 11 */
  82. 512, /* 12 */
  83. 768, /* 13 */
  84. 1024, /* 14 */
  85. 1536, /* 15 */
  86. 2048, /* 16 */
  87. 3072, /* 17 */
  88. 4096, /* 18 */
  89. 6144, /* 19 */
  90. 8192, /* 1A */
  91. 12288, /* 1B */
  92. 16384, /* 1C */
  93. 24576, /* 1D */
  94. 32768 /* 1E */
  95. };
  96. static void get_map_page(struct qib_qpn_table *qpt, struct qpn_map *map,
  97. gfp_t gfp)
  98. {
  99. unsigned long page = get_zeroed_page(gfp);
  100. /*
  101. * Free the page if someone raced with us installing it.
  102. */
  103. spin_lock(&qpt->lock);
  104. if (map->page)
  105. free_page(page);
  106. else
  107. map->page = (void *)page;
  108. spin_unlock(&qpt->lock);
  109. }
  110. /*
  111. * Allocate the next available QPN or
  112. * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
  113. */
  114. static int alloc_qpn(struct qib_devdata *dd, struct qib_qpn_table *qpt,
  115. enum ib_qp_type type, u8 port, gfp_t gfp)
  116. {
  117. u32 i, offset, max_scan, qpn;
  118. struct qpn_map *map;
  119. u32 ret;
  120. if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
  121. unsigned n;
  122. ret = type == IB_QPT_GSI;
  123. n = 1 << (ret + 2 * (port - 1));
  124. spin_lock(&qpt->lock);
  125. if (qpt->flags & n)
  126. ret = -EINVAL;
  127. else
  128. qpt->flags |= n;
  129. spin_unlock(&qpt->lock);
  130. goto bail;
  131. }
  132. qpn = qpt->last + 2;
  133. if (qpn >= QPN_MAX)
  134. qpn = 2;
  135. if (qpt->mask && ((qpn & qpt->mask) >> 1) >= dd->n_krcv_queues)
  136. qpn = (qpn | qpt->mask) + 2;
  137. offset = qpn & RVT_BITS_PER_PAGE_MASK;
  138. map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
  139. max_scan = qpt->nmaps - !offset;
  140. for (i = 0;;) {
  141. if (unlikely(!map->page)) {
  142. get_map_page(qpt, map, gfp);
  143. if (unlikely(!map->page))
  144. break;
  145. }
  146. do {
  147. if (!test_and_set_bit(offset, map->page)) {
  148. qpt->last = qpn;
  149. ret = qpn;
  150. goto bail;
  151. }
  152. offset = find_next_offset(qpt, map, offset,
  153. dd->n_krcv_queues);
  154. qpn = mk_qpn(qpt, map, offset);
  155. /*
  156. * This test differs from alloc_pidmap().
  157. * If find_next_offset() does find a zero
  158. * bit, we don't need to check for QPN
  159. * wrapping around past our starting QPN.
  160. * We just need to be sure we don't loop
  161. * forever.
  162. */
  163. } while (offset < RVT_BITS_PER_PAGE && qpn < QPN_MAX);
  164. /*
  165. * In order to keep the number of pages allocated to a
  166. * minimum, we scan the all existing pages before increasing
  167. * the size of the bitmap table.
  168. */
  169. if (++i > max_scan) {
  170. if (qpt->nmaps == QPNMAP_ENTRIES)
  171. break;
  172. map = &qpt->map[qpt->nmaps++];
  173. offset = 0;
  174. } else if (map < &qpt->map[qpt->nmaps]) {
  175. ++map;
  176. offset = 0;
  177. } else {
  178. map = &qpt->map[0];
  179. offset = 2;
  180. }
  181. qpn = mk_qpn(qpt, map, offset);
  182. }
  183. ret = -ENOMEM;
  184. bail:
  185. return ret;
  186. }
  187. static void free_qpn(struct qib_qpn_table *qpt, u32 qpn)
  188. {
  189. struct qpn_map *map;
  190. map = qpt->map + qpn / RVT_BITS_PER_PAGE;
  191. if (map->page)
  192. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  193. }
  194. static inline unsigned qpn_hash(struct qib_ibdev *dev, u32 qpn)
  195. {
  196. return jhash_1word(qpn, dev->qp_rnd) &
  197. (dev->qp_table_size - 1);
  198. }
  199. /*
  200. * Put the QP into the hash table.
  201. * The hash table holds a reference to the QP.
  202. */
  203. static void insert_qp(struct qib_ibdev *dev, struct qib_qp *qp)
  204. {
  205. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  206. unsigned long flags;
  207. unsigned n = qpn_hash(dev, qp->ibqp.qp_num);
  208. atomic_inc(&qp->refcount);
  209. spin_lock_irqsave(&dev->qpt_lock, flags);
  210. if (qp->ibqp.qp_num == 0)
  211. rcu_assign_pointer(ibp->qp0, qp);
  212. else if (qp->ibqp.qp_num == 1)
  213. rcu_assign_pointer(ibp->qp1, qp);
  214. else {
  215. qp->next = dev->qp_table[n];
  216. rcu_assign_pointer(dev->qp_table[n], qp);
  217. }
  218. spin_unlock_irqrestore(&dev->qpt_lock, flags);
  219. }
  220. /*
  221. * Remove the QP from the table so it can't be found asynchronously by
  222. * the receive interrupt routine.
  223. */
  224. static void remove_qp(struct qib_ibdev *dev, struct qib_qp *qp)
  225. {
  226. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  227. unsigned n = qpn_hash(dev, qp->ibqp.qp_num);
  228. unsigned long flags;
  229. int removed = 1;
  230. spin_lock_irqsave(&dev->qpt_lock, flags);
  231. if (rcu_dereference_protected(ibp->qp0,
  232. lockdep_is_held(&dev->qpt_lock)) == qp) {
  233. RCU_INIT_POINTER(ibp->qp0, NULL);
  234. } else if (rcu_dereference_protected(ibp->qp1,
  235. lockdep_is_held(&dev->qpt_lock)) == qp) {
  236. RCU_INIT_POINTER(ibp->qp1, NULL);
  237. } else {
  238. struct qib_qp *q;
  239. struct qib_qp __rcu **qpp;
  240. removed = 0;
  241. qpp = &dev->qp_table[n];
  242. for (; (q = rcu_dereference_protected(*qpp,
  243. lockdep_is_held(&dev->qpt_lock))) != NULL;
  244. qpp = &q->next)
  245. if (q == qp) {
  246. RCU_INIT_POINTER(*qpp,
  247. rcu_dereference_protected(qp->next,
  248. lockdep_is_held(&dev->qpt_lock)));
  249. removed = 1;
  250. break;
  251. }
  252. }
  253. spin_unlock_irqrestore(&dev->qpt_lock, flags);
  254. if (removed) {
  255. synchronize_rcu();
  256. atomic_dec(&qp->refcount);
  257. }
  258. }
  259. /**
  260. * qib_free_all_qps - check for QPs still in use
  261. * @qpt: the QP table to empty
  262. *
  263. * There should not be any QPs still in use.
  264. * Free memory for table.
  265. */
  266. unsigned qib_free_all_qps(struct qib_devdata *dd)
  267. {
  268. struct qib_ibdev *dev = &dd->verbs_dev;
  269. unsigned long flags;
  270. struct qib_qp *qp;
  271. unsigned n, qp_inuse = 0;
  272. for (n = 0; n < dd->num_pports; n++) {
  273. struct qib_ibport *ibp = &dd->pport[n].ibport_data;
  274. if (!qib_mcast_tree_empty(ibp))
  275. qp_inuse++;
  276. rcu_read_lock();
  277. if (rcu_dereference(ibp->qp0))
  278. qp_inuse++;
  279. if (rcu_dereference(ibp->qp1))
  280. qp_inuse++;
  281. rcu_read_unlock();
  282. }
  283. spin_lock_irqsave(&dev->qpt_lock, flags);
  284. for (n = 0; n < dev->qp_table_size; n++) {
  285. qp = rcu_dereference_protected(dev->qp_table[n],
  286. lockdep_is_held(&dev->qpt_lock));
  287. RCU_INIT_POINTER(dev->qp_table[n], NULL);
  288. for (; qp; qp = rcu_dereference_protected(qp->next,
  289. lockdep_is_held(&dev->qpt_lock)))
  290. qp_inuse++;
  291. }
  292. spin_unlock_irqrestore(&dev->qpt_lock, flags);
  293. synchronize_rcu();
  294. return qp_inuse;
  295. }
  296. /**
  297. * qib_lookup_qpn - return the QP with the given QPN
  298. * @qpt: the QP table
  299. * @qpn: the QP number to look up
  300. *
  301. * The caller is responsible for decrementing the QP reference count
  302. * when done.
  303. */
  304. struct qib_qp *qib_lookup_qpn(struct qib_ibport *ibp, u32 qpn)
  305. {
  306. struct qib_qp *qp = NULL;
  307. rcu_read_lock();
  308. if (unlikely(qpn <= 1)) {
  309. if (qpn == 0)
  310. qp = rcu_dereference(ibp->qp0);
  311. else
  312. qp = rcu_dereference(ibp->qp1);
  313. if (qp)
  314. atomic_inc(&qp->refcount);
  315. } else {
  316. struct qib_ibdev *dev = &ppd_from_ibp(ibp)->dd->verbs_dev;
  317. unsigned n = qpn_hash(dev, qpn);
  318. for (qp = rcu_dereference(dev->qp_table[n]); qp;
  319. qp = rcu_dereference(qp->next))
  320. if (qp->ibqp.qp_num == qpn) {
  321. atomic_inc(&qp->refcount);
  322. break;
  323. }
  324. }
  325. rcu_read_unlock();
  326. return qp;
  327. }
  328. /**
  329. * qib_reset_qp - initialize the QP state to the reset state
  330. * @qp: the QP to reset
  331. * @type: the QP type
  332. */
  333. static void qib_reset_qp(struct qib_qp *qp, enum ib_qp_type type)
  334. {
  335. qp->remote_qpn = 0;
  336. qp->qkey = 0;
  337. qp->qp_access_flags = 0;
  338. atomic_set(&qp->s_dma_busy, 0);
  339. qp->s_flags &= QIB_S_SIGNAL_REQ_WR;
  340. qp->s_hdrwords = 0;
  341. qp->s_wqe = NULL;
  342. qp->s_draining = 0;
  343. qp->s_next_psn = 0;
  344. qp->s_last_psn = 0;
  345. qp->s_sending_psn = 0;
  346. qp->s_sending_hpsn = 0;
  347. qp->s_psn = 0;
  348. qp->r_psn = 0;
  349. qp->r_msn = 0;
  350. if (type == IB_QPT_RC) {
  351. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  352. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  353. } else {
  354. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  355. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  356. }
  357. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  358. qp->r_nak_state = 0;
  359. qp->r_aflags = 0;
  360. qp->r_flags = 0;
  361. qp->s_head = 0;
  362. qp->s_tail = 0;
  363. qp->s_cur = 0;
  364. qp->s_acked = 0;
  365. qp->s_last = 0;
  366. qp->s_ssn = 1;
  367. qp->s_lsn = 0;
  368. qp->s_mig_state = IB_MIG_MIGRATED;
  369. memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
  370. qp->r_head_ack_queue = 0;
  371. qp->s_tail_ack_queue = 0;
  372. qp->s_num_rd_atomic = 0;
  373. if (qp->r_rq.wq) {
  374. qp->r_rq.wq->head = 0;
  375. qp->r_rq.wq->tail = 0;
  376. }
  377. qp->r_sge.num_sge = 0;
  378. }
  379. static void clear_mr_refs(struct qib_qp *qp, int clr_sends)
  380. {
  381. unsigned n;
  382. if (test_and_clear_bit(QIB_R_REWIND_SGE, &qp->r_aflags))
  383. qib_put_ss(&qp->s_rdma_read_sge);
  384. qib_put_ss(&qp->r_sge);
  385. if (clr_sends) {
  386. while (qp->s_last != qp->s_head) {
  387. struct qib_swqe *wqe = get_swqe_ptr(qp, qp->s_last);
  388. unsigned i;
  389. for (i = 0; i < wqe->wr.num_sge; i++) {
  390. struct qib_sge *sge = &wqe->sg_list[i];
  391. qib_put_mr(sge->mr);
  392. }
  393. if (qp->ibqp.qp_type == IB_QPT_UD ||
  394. qp->ibqp.qp_type == IB_QPT_SMI ||
  395. qp->ibqp.qp_type == IB_QPT_GSI)
  396. atomic_dec(&to_iah(wqe->ud_wr.ah)->refcount);
  397. if (++qp->s_last >= qp->s_size)
  398. qp->s_last = 0;
  399. }
  400. if (qp->s_rdma_mr) {
  401. qib_put_mr(qp->s_rdma_mr);
  402. qp->s_rdma_mr = NULL;
  403. }
  404. }
  405. if (qp->ibqp.qp_type != IB_QPT_RC)
  406. return;
  407. for (n = 0; n < ARRAY_SIZE(qp->s_ack_queue); n++) {
  408. struct qib_ack_entry *e = &qp->s_ack_queue[n];
  409. if (e->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST &&
  410. e->rdma_sge.mr) {
  411. qib_put_mr(e->rdma_sge.mr);
  412. e->rdma_sge.mr = NULL;
  413. }
  414. }
  415. }
  416. /**
  417. * qib_error_qp - put a QP into the error state
  418. * @qp: the QP to put into the error state
  419. * @err: the receive completion error to signal if a RWQE is active
  420. *
  421. * Flushes both send and receive work queues.
  422. * Returns true if last WQE event should be generated.
  423. * The QP r_lock and s_lock should be held and interrupts disabled.
  424. * If we are already in error state, just return.
  425. */
  426. int qib_error_qp(struct qib_qp *qp, enum ib_wc_status err)
  427. {
  428. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  429. struct ib_wc wc;
  430. int ret = 0;
  431. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  432. goto bail;
  433. qp->state = IB_QPS_ERR;
  434. if (qp->s_flags & (QIB_S_TIMER | QIB_S_WAIT_RNR)) {
  435. qp->s_flags &= ~(QIB_S_TIMER | QIB_S_WAIT_RNR);
  436. del_timer(&qp->s_timer);
  437. }
  438. if (qp->s_flags & QIB_S_ANY_WAIT_SEND)
  439. qp->s_flags &= ~QIB_S_ANY_WAIT_SEND;
  440. spin_lock(&dev->pending_lock);
  441. if (!list_empty(&qp->iowait) && !(qp->s_flags & QIB_S_BUSY)) {
  442. qp->s_flags &= ~QIB_S_ANY_WAIT_IO;
  443. list_del_init(&qp->iowait);
  444. }
  445. spin_unlock(&dev->pending_lock);
  446. if (!(qp->s_flags & QIB_S_BUSY)) {
  447. qp->s_hdrwords = 0;
  448. if (qp->s_rdma_mr) {
  449. qib_put_mr(qp->s_rdma_mr);
  450. qp->s_rdma_mr = NULL;
  451. }
  452. if (qp->s_tx) {
  453. qib_put_txreq(qp->s_tx);
  454. qp->s_tx = NULL;
  455. }
  456. }
  457. /* Schedule the sending tasklet to drain the send work queue. */
  458. if (qp->s_last != qp->s_head)
  459. qib_schedule_send(qp);
  460. clear_mr_refs(qp, 0);
  461. memset(&wc, 0, sizeof(wc));
  462. wc.qp = &qp->ibqp;
  463. wc.opcode = IB_WC_RECV;
  464. if (test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags)) {
  465. wc.wr_id = qp->r_wr_id;
  466. wc.status = err;
  467. qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
  468. }
  469. wc.status = IB_WC_WR_FLUSH_ERR;
  470. if (qp->r_rq.wq) {
  471. struct qib_rwq *wq;
  472. u32 head;
  473. u32 tail;
  474. spin_lock(&qp->r_rq.lock);
  475. /* sanity check pointers before trusting them */
  476. wq = qp->r_rq.wq;
  477. head = wq->head;
  478. if (head >= qp->r_rq.size)
  479. head = 0;
  480. tail = wq->tail;
  481. if (tail >= qp->r_rq.size)
  482. tail = 0;
  483. while (tail != head) {
  484. wc.wr_id = get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  485. if (++tail >= qp->r_rq.size)
  486. tail = 0;
  487. qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
  488. }
  489. wq->tail = tail;
  490. spin_unlock(&qp->r_rq.lock);
  491. } else if (qp->ibqp.event_handler)
  492. ret = 1;
  493. bail:
  494. return ret;
  495. }
  496. /**
  497. * qib_modify_qp - modify the attributes of a queue pair
  498. * @ibqp: the queue pair who's attributes we're modifying
  499. * @attr: the new attributes
  500. * @attr_mask: the mask of attributes to modify
  501. * @udata: user data for libibverbs.so
  502. *
  503. * Returns 0 on success, otherwise returns an errno.
  504. */
  505. int qib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  506. int attr_mask, struct ib_udata *udata)
  507. {
  508. struct qib_ibdev *dev = to_idev(ibqp->device);
  509. struct qib_qp *qp = to_iqp(ibqp);
  510. enum ib_qp_state cur_state, new_state;
  511. struct ib_event ev;
  512. int lastwqe = 0;
  513. int mig = 0;
  514. int ret;
  515. u32 pmtu = 0; /* for gcc warning only */
  516. spin_lock_irq(&qp->r_lock);
  517. spin_lock(&qp->s_lock);
  518. cur_state = attr_mask & IB_QP_CUR_STATE ?
  519. attr->cur_qp_state : qp->state;
  520. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  521. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  522. attr_mask, IB_LINK_LAYER_UNSPECIFIED))
  523. goto inval;
  524. if (attr_mask & IB_QP_AV) {
  525. if (attr->ah_attr.dlid >= QIB_MULTICAST_LID_BASE)
  526. goto inval;
  527. if (qib_check_ah(qp->ibqp.device, &attr->ah_attr))
  528. goto inval;
  529. }
  530. if (attr_mask & IB_QP_ALT_PATH) {
  531. if (attr->alt_ah_attr.dlid >= QIB_MULTICAST_LID_BASE)
  532. goto inval;
  533. if (qib_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
  534. goto inval;
  535. if (attr->alt_pkey_index >= qib_get_npkeys(dd_from_dev(dev)))
  536. goto inval;
  537. }
  538. if (attr_mask & IB_QP_PKEY_INDEX)
  539. if (attr->pkey_index >= qib_get_npkeys(dd_from_dev(dev)))
  540. goto inval;
  541. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  542. if (attr->min_rnr_timer > 31)
  543. goto inval;
  544. if (attr_mask & IB_QP_PORT)
  545. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  546. qp->ibqp.qp_type == IB_QPT_GSI ||
  547. attr->port_num == 0 ||
  548. attr->port_num > ibqp->device->phys_port_cnt)
  549. goto inval;
  550. if (attr_mask & IB_QP_DEST_QPN)
  551. if (attr->dest_qp_num > QIB_QPN_MASK)
  552. goto inval;
  553. if (attr_mask & IB_QP_RETRY_CNT)
  554. if (attr->retry_cnt > 7)
  555. goto inval;
  556. if (attr_mask & IB_QP_RNR_RETRY)
  557. if (attr->rnr_retry > 7)
  558. goto inval;
  559. /*
  560. * Don't allow invalid path_mtu values. OK to set greater
  561. * than the active mtu (or even the max_cap, if we have tuned
  562. * that to a small mtu. We'll set qp->path_mtu
  563. * to the lesser of requested attribute mtu and active,
  564. * for packetizing messages.
  565. * Note that the QP port has to be set in INIT and MTU in RTR.
  566. */
  567. if (attr_mask & IB_QP_PATH_MTU) {
  568. struct qib_devdata *dd = dd_from_dev(dev);
  569. int mtu, pidx = qp->port_num - 1;
  570. mtu = ib_mtu_enum_to_int(attr->path_mtu);
  571. if (mtu == -1)
  572. goto inval;
  573. if (mtu > dd->pport[pidx].ibmtu) {
  574. switch (dd->pport[pidx].ibmtu) {
  575. case 4096:
  576. pmtu = IB_MTU_4096;
  577. break;
  578. case 2048:
  579. pmtu = IB_MTU_2048;
  580. break;
  581. case 1024:
  582. pmtu = IB_MTU_1024;
  583. break;
  584. case 512:
  585. pmtu = IB_MTU_512;
  586. break;
  587. case 256:
  588. pmtu = IB_MTU_256;
  589. break;
  590. default:
  591. pmtu = IB_MTU_2048;
  592. }
  593. } else
  594. pmtu = attr->path_mtu;
  595. }
  596. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  597. if (attr->path_mig_state == IB_MIG_REARM) {
  598. if (qp->s_mig_state == IB_MIG_ARMED)
  599. goto inval;
  600. if (new_state != IB_QPS_RTS)
  601. goto inval;
  602. } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
  603. if (qp->s_mig_state == IB_MIG_REARM)
  604. goto inval;
  605. if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
  606. goto inval;
  607. if (qp->s_mig_state == IB_MIG_ARMED)
  608. mig = 1;
  609. } else
  610. goto inval;
  611. }
  612. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  613. if (attr->max_dest_rd_atomic > QIB_MAX_RDMA_ATOMIC)
  614. goto inval;
  615. switch (new_state) {
  616. case IB_QPS_RESET:
  617. if (qp->state != IB_QPS_RESET) {
  618. qp->state = IB_QPS_RESET;
  619. spin_lock(&dev->pending_lock);
  620. if (!list_empty(&qp->iowait))
  621. list_del_init(&qp->iowait);
  622. spin_unlock(&dev->pending_lock);
  623. qp->s_flags &= ~(QIB_S_TIMER | QIB_S_ANY_WAIT);
  624. spin_unlock(&qp->s_lock);
  625. spin_unlock_irq(&qp->r_lock);
  626. /* Stop the sending work queue and retry timer */
  627. cancel_work_sync(&qp->s_work);
  628. del_timer_sync(&qp->s_timer);
  629. wait_event(qp->wait_dma, !atomic_read(&qp->s_dma_busy));
  630. if (qp->s_tx) {
  631. qib_put_txreq(qp->s_tx);
  632. qp->s_tx = NULL;
  633. }
  634. remove_qp(dev, qp);
  635. wait_event(qp->wait, !atomic_read(&qp->refcount));
  636. spin_lock_irq(&qp->r_lock);
  637. spin_lock(&qp->s_lock);
  638. clear_mr_refs(qp, 1);
  639. qib_reset_qp(qp, ibqp->qp_type);
  640. }
  641. break;
  642. case IB_QPS_RTR:
  643. /* Allow event to retrigger if QP set to RTR more than once */
  644. qp->r_flags &= ~QIB_R_COMM_EST;
  645. qp->state = new_state;
  646. break;
  647. case IB_QPS_SQD:
  648. qp->s_draining = qp->s_last != qp->s_cur;
  649. qp->state = new_state;
  650. break;
  651. case IB_QPS_SQE:
  652. if (qp->ibqp.qp_type == IB_QPT_RC)
  653. goto inval;
  654. qp->state = new_state;
  655. break;
  656. case IB_QPS_ERR:
  657. lastwqe = qib_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  658. break;
  659. default:
  660. qp->state = new_state;
  661. break;
  662. }
  663. if (attr_mask & IB_QP_PKEY_INDEX)
  664. qp->s_pkey_index = attr->pkey_index;
  665. if (attr_mask & IB_QP_PORT)
  666. qp->port_num = attr->port_num;
  667. if (attr_mask & IB_QP_DEST_QPN)
  668. qp->remote_qpn = attr->dest_qp_num;
  669. if (attr_mask & IB_QP_SQ_PSN) {
  670. qp->s_next_psn = attr->sq_psn & QIB_PSN_MASK;
  671. qp->s_psn = qp->s_next_psn;
  672. qp->s_sending_psn = qp->s_next_psn;
  673. qp->s_last_psn = qp->s_next_psn - 1;
  674. qp->s_sending_hpsn = qp->s_last_psn;
  675. }
  676. if (attr_mask & IB_QP_RQ_PSN)
  677. qp->r_psn = attr->rq_psn & QIB_PSN_MASK;
  678. if (attr_mask & IB_QP_ACCESS_FLAGS)
  679. qp->qp_access_flags = attr->qp_access_flags;
  680. if (attr_mask & IB_QP_AV) {
  681. qp->remote_ah_attr = attr->ah_attr;
  682. qp->s_srate = attr->ah_attr.static_rate;
  683. }
  684. if (attr_mask & IB_QP_ALT_PATH) {
  685. qp->alt_ah_attr = attr->alt_ah_attr;
  686. qp->s_alt_pkey_index = attr->alt_pkey_index;
  687. }
  688. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  689. qp->s_mig_state = attr->path_mig_state;
  690. if (mig) {
  691. qp->remote_ah_attr = qp->alt_ah_attr;
  692. qp->port_num = qp->alt_ah_attr.port_num;
  693. qp->s_pkey_index = qp->s_alt_pkey_index;
  694. }
  695. }
  696. if (attr_mask & IB_QP_PATH_MTU) {
  697. qp->path_mtu = pmtu;
  698. qp->pmtu = ib_mtu_enum_to_int(pmtu);
  699. }
  700. if (attr_mask & IB_QP_RETRY_CNT) {
  701. qp->s_retry_cnt = attr->retry_cnt;
  702. qp->s_retry = attr->retry_cnt;
  703. }
  704. if (attr_mask & IB_QP_RNR_RETRY) {
  705. qp->s_rnr_retry_cnt = attr->rnr_retry;
  706. qp->s_rnr_retry = attr->rnr_retry;
  707. }
  708. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  709. qp->r_min_rnr_timer = attr->min_rnr_timer;
  710. if (attr_mask & IB_QP_TIMEOUT) {
  711. qp->timeout = attr->timeout;
  712. qp->timeout_jiffies =
  713. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  714. 1000UL);
  715. }
  716. if (attr_mask & IB_QP_QKEY)
  717. qp->qkey = attr->qkey;
  718. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  719. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  720. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  721. qp->s_max_rd_atomic = attr->max_rd_atomic;
  722. spin_unlock(&qp->s_lock);
  723. spin_unlock_irq(&qp->r_lock);
  724. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  725. insert_qp(dev, qp);
  726. if (lastwqe) {
  727. ev.device = qp->ibqp.device;
  728. ev.element.qp = &qp->ibqp;
  729. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  730. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  731. }
  732. if (mig) {
  733. ev.device = qp->ibqp.device;
  734. ev.element.qp = &qp->ibqp;
  735. ev.event = IB_EVENT_PATH_MIG;
  736. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  737. }
  738. ret = 0;
  739. goto bail;
  740. inval:
  741. spin_unlock(&qp->s_lock);
  742. spin_unlock_irq(&qp->r_lock);
  743. ret = -EINVAL;
  744. bail:
  745. return ret;
  746. }
  747. int qib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  748. int attr_mask, struct ib_qp_init_attr *init_attr)
  749. {
  750. struct qib_qp *qp = to_iqp(ibqp);
  751. attr->qp_state = qp->state;
  752. attr->cur_qp_state = attr->qp_state;
  753. attr->path_mtu = qp->path_mtu;
  754. attr->path_mig_state = qp->s_mig_state;
  755. attr->qkey = qp->qkey;
  756. attr->rq_psn = qp->r_psn & QIB_PSN_MASK;
  757. attr->sq_psn = qp->s_next_psn & QIB_PSN_MASK;
  758. attr->dest_qp_num = qp->remote_qpn;
  759. attr->qp_access_flags = qp->qp_access_flags;
  760. attr->cap.max_send_wr = qp->s_size - 1;
  761. attr->cap.max_recv_wr = qp->ibqp.srq ? 0 : qp->r_rq.size - 1;
  762. attr->cap.max_send_sge = qp->s_max_sge;
  763. attr->cap.max_recv_sge = qp->r_rq.max_sge;
  764. attr->cap.max_inline_data = 0;
  765. attr->ah_attr = qp->remote_ah_attr;
  766. attr->alt_ah_attr = qp->alt_ah_attr;
  767. attr->pkey_index = qp->s_pkey_index;
  768. attr->alt_pkey_index = qp->s_alt_pkey_index;
  769. attr->en_sqd_async_notify = 0;
  770. attr->sq_draining = qp->s_draining;
  771. attr->max_rd_atomic = qp->s_max_rd_atomic;
  772. attr->max_dest_rd_atomic = qp->r_max_rd_atomic;
  773. attr->min_rnr_timer = qp->r_min_rnr_timer;
  774. attr->port_num = qp->port_num;
  775. attr->timeout = qp->timeout;
  776. attr->retry_cnt = qp->s_retry_cnt;
  777. attr->rnr_retry = qp->s_rnr_retry_cnt;
  778. attr->alt_port_num = qp->alt_ah_attr.port_num;
  779. attr->alt_timeout = qp->alt_timeout;
  780. init_attr->event_handler = qp->ibqp.event_handler;
  781. init_attr->qp_context = qp->ibqp.qp_context;
  782. init_attr->send_cq = qp->ibqp.send_cq;
  783. init_attr->recv_cq = qp->ibqp.recv_cq;
  784. init_attr->srq = qp->ibqp.srq;
  785. init_attr->cap = attr->cap;
  786. if (qp->s_flags & QIB_S_SIGNAL_REQ_WR)
  787. init_attr->sq_sig_type = IB_SIGNAL_REQ_WR;
  788. else
  789. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  790. init_attr->qp_type = qp->ibqp.qp_type;
  791. init_attr->port_num = qp->port_num;
  792. return 0;
  793. }
  794. /**
  795. * qib_compute_aeth - compute the AETH (syndrome + MSN)
  796. * @qp: the queue pair to compute the AETH for
  797. *
  798. * Returns the AETH.
  799. */
  800. __be32 qib_compute_aeth(struct qib_qp *qp)
  801. {
  802. u32 aeth = qp->r_msn & QIB_MSN_MASK;
  803. if (qp->ibqp.srq) {
  804. /*
  805. * Shared receive queues don't generate credits.
  806. * Set the credit field to the invalid value.
  807. */
  808. aeth |= QIB_AETH_CREDIT_INVAL << QIB_AETH_CREDIT_SHIFT;
  809. } else {
  810. u32 min, max, x;
  811. u32 credits;
  812. struct qib_rwq *wq = qp->r_rq.wq;
  813. u32 head;
  814. u32 tail;
  815. /* sanity check pointers before trusting them */
  816. head = wq->head;
  817. if (head >= qp->r_rq.size)
  818. head = 0;
  819. tail = wq->tail;
  820. if (tail >= qp->r_rq.size)
  821. tail = 0;
  822. /*
  823. * Compute the number of credits available (RWQEs).
  824. * XXX Not holding the r_rq.lock here so there is a small
  825. * chance that the pair of reads are not atomic.
  826. */
  827. credits = head - tail;
  828. if ((int)credits < 0)
  829. credits += qp->r_rq.size;
  830. /*
  831. * Binary search the credit table to find the code to
  832. * use.
  833. */
  834. min = 0;
  835. max = 31;
  836. for (;;) {
  837. x = (min + max) / 2;
  838. if (credit_table[x] == credits)
  839. break;
  840. if (credit_table[x] > credits)
  841. max = x;
  842. else if (min == x)
  843. break;
  844. else
  845. min = x;
  846. }
  847. aeth |= x << QIB_AETH_CREDIT_SHIFT;
  848. }
  849. return cpu_to_be32(aeth);
  850. }
  851. /**
  852. * qib_create_qp - create a queue pair for a device
  853. * @ibpd: the protection domain who's device we create the queue pair for
  854. * @init_attr: the attributes of the queue pair
  855. * @udata: user data for libibverbs.so
  856. *
  857. * Returns the queue pair on success, otherwise returns an errno.
  858. *
  859. * Called by the ib_create_qp() core verbs function.
  860. */
  861. struct ib_qp *qib_create_qp(struct ib_pd *ibpd,
  862. struct ib_qp_init_attr *init_attr,
  863. struct ib_udata *udata)
  864. {
  865. struct qib_qp *qp;
  866. int err;
  867. struct qib_swqe *swq = NULL;
  868. struct qib_ibdev *dev;
  869. struct qib_devdata *dd;
  870. size_t sz;
  871. size_t sg_list_sz;
  872. struct ib_qp *ret;
  873. gfp_t gfp;
  874. if (init_attr->cap.max_send_sge > ib_qib_max_sges ||
  875. init_attr->cap.max_send_wr > ib_qib_max_qp_wrs ||
  876. init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
  877. return ERR_PTR(-EINVAL);
  878. /* GFP_NOIO is applicable in RC QPs only */
  879. if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
  880. init_attr->qp_type != IB_QPT_RC)
  881. return ERR_PTR(-EINVAL);
  882. gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
  883. GFP_NOIO : GFP_KERNEL;
  884. /* Check receive queue parameters if no SRQ is specified. */
  885. if (!init_attr->srq) {
  886. if (init_attr->cap.max_recv_sge > ib_qib_max_sges ||
  887. init_attr->cap.max_recv_wr > ib_qib_max_qp_wrs) {
  888. ret = ERR_PTR(-EINVAL);
  889. goto bail;
  890. }
  891. if (init_attr->cap.max_send_sge +
  892. init_attr->cap.max_send_wr +
  893. init_attr->cap.max_recv_sge +
  894. init_attr->cap.max_recv_wr == 0) {
  895. ret = ERR_PTR(-EINVAL);
  896. goto bail;
  897. }
  898. }
  899. switch (init_attr->qp_type) {
  900. case IB_QPT_SMI:
  901. case IB_QPT_GSI:
  902. if (init_attr->port_num == 0 ||
  903. init_attr->port_num > ibpd->device->phys_port_cnt) {
  904. ret = ERR_PTR(-EINVAL);
  905. goto bail;
  906. }
  907. case IB_QPT_UC:
  908. case IB_QPT_RC:
  909. case IB_QPT_UD:
  910. sz = sizeof(struct qib_sge) *
  911. init_attr->cap.max_send_sge +
  912. sizeof(struct qib_swqe);
  913. swq = __vmalloc((init_attr->cap.max_send_wr + 1) * sz,
  914. gfp, PAGE_KERNEL);
  915. if (swq == NULL) {
  916. ret = ERR_PTR(-ENOMEM);
  917. goto bail;
  918. }
  919. sz = sizeof(*qp);
  920. sg_list_sz = 0;
  921. if (init_attr->srq) {
  922. struct qib_srq *srq = to_isrq(init_attr->srq);
  923. if (srq->rq.max_sge > 1)
  924. sg_list_sz = sizeof(*qp->r_sg_list) *
  925. (srq->rq.max_sge - 1);
  926. } else if (init_attr->cap.max_recv_sge > 1)
  927. sg_list_sz = sizeof(*qp->r_sg_list) *
  928. (init_attr->cap.max_recv_sge - 1);
  929. qp = kzalloc(sz + sg_list_sz, gfp);
  930. if (!qp) {
  931. ret = ERR_PTR(-ENOMEM);
  932. goto bail_swq;
  933. }
  934. RCU_INIT_POINTER(qp->next, NULL);
  935. qp->s_hdr = kzalloc(sizeof(*qp->s_hdr), gfp);
  936. if (!qp->s_hdr) {
  937. ret = ERR_PTR(-ENOMEM);
  938. goto bail_qp;
  939. }
  940. qp->timeout_jiffies =
  941. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  942. 1000UL);
  943. if (init_attr->srq)
  944. sz = 0;
  945. else {
  946. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  947. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  948. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  949. sizeof(struct qib_rwqe);
  950. if (gfp != GFP_NOIO)
  951. qp->r_rq.wq = vmalloc_user(
  952. sizeof(struct qib_rwq) +
  953. qp->r_rq.size * sz);
  954. else
  955. qp->r_rq.wq = __vmalloc(
  956. sizeof(struct qib_rwq) +
  957. qp->r_rq.size * sz,
  958. gfp, PAGE_KERNEL);
  959. if (!qp->r_rq.wq) {
  960. ret = ERR_PTR(-ENOMEM);
  961. goto bail_qp;
  962. }
  963. }
  964. /*
  965. * ib_create_qp() will initialize qp->ibqp
  966. * except for qp->ibqp.qp_num.
  967. */
  968. spin_lock_init(&qp->r_lock);
  969. spin_lock_init(&qp->s_lock);
  970. spin_lock_init(&qp->r_rq.lock);
  971. atomic_set(&qp->refcount, 0);
  972. init_waitqueue_head(&qp->wait);
  973. init_waitqueue_head(&qp->wait_dma);
  974. init_timer(&qp->s_timer);
  975. qp->s_timer.data = (unsigned long)qp;
  976. INIT_WORK(&qp->s_work, qib_do_send);
  977. INIT_LIST_HEAD(&qp->iowait);
  978. INIT_LIST_HEAD(&qp->rspwait);
  979. qp->state = IB_QPS_RESET;
  980. qp->s_wq = swq;
  981. qp->s_size = init_attr->cap.max_send_wr + 1;
  982. qp->s_max_sge = init_attr->cap.max_send_sge;
  983. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  984. qp->s_flags = QIB_S_SIGNAL_REQ_WR;
  985. dev = to_idev(ibpd->device);
  986. dd = dd_from_dev(dev);
  987. err = alloc_qpn(dd, &dev->qpn_table, init_attr->qp_type,
  988. init_attr->port_num, gfp);
  989. if (err < 0) {
  990. ret = ERR_PTR(err);
  991. vfree(qp->r_rq.wq);
  992. goto bail_qp;
  993. }
  994. qp->ibqp.qp_num = err;
  995. qp->port_num = init_attr->port_num;
  996. qib_reset_qp(qp, init_attr->qp_type);
  997. break;
  998. default:
  999. /* Don't support raw QPs */
  1000. ret = ERR_PTR(-ENOSYS);
  1001. goto bail;
  1002. }
  1003. init_attr->cap.max_inline_data = 0;
  1004. /*
  1005. * Return the address of the RWQ as the offset to mmap.
  1006. * See qib_mmap() for details.
  1007. */
  1008. if (udata && udata->outlen >= sizeof(__u64)) {
  1009. if (!qp->r_rq.wq) {
  1010. __u64 offset = 0;
  1011. err = ib_copy_to_udata(udata, &offset,
  1012. sizeof(offset));
  1013. if (err) {
  1014. ret = ERR_PTR(err);
  1015. goto bail_ip;
  1016. }
  1017. } else {
  1018. u32 s = sizeof(struct qib_rwq) + qp->r_rq.size * sz;
  1019. qp->ip = qib_create_mmap_info(dev, s,
  1020. ibpd->uobject->context,
  1021. qp->r_rq.wq);
  1022. if (!qp->ip) {
  1023. ret = ERR_PTR(-ENOMEM);
  1024. goto bail_ip;
  1025. }
  1026. err = ib_copy_to_udata(udata, &(qp->ip->offset),
  1027. sizeof(qp->ip->offset));
  1028. if (err) {
  1029. ret = ERR_PTR(err);
  1030. goto bail_ip;
  1031. }
  1032. }
  1033. }
  1034. spin_lock(&dev->n_qps_lock);
  1035. if (dev->n_qps_allocated == ib_qib_max_qps) {
  1036. spin_unlock(&dev->n_qps_lock);
  1037. ret = ERR_PTR(-ENOMEM);
  1038. goto bail_ip;
  1039. }
  1040. dev->n_qps_allocated++;
  1041. spin_unlock(&dev->n_qps_lock);
  1042. if (qp->ip) {
  1043. spin_lock_irq(&dev->pending_lock);
  1044. list_add(&qp->ip->pending_mmaps, &dev->pending_mmaps);
  1045. spin_unlock_irq(&dev->pending_lock);
  1046. }
  1047. ret = &qp->ibqp;
  1048. goto bail;
  1049. bail_ip:
  1050. if (qp->ip)
  1051. kref_put(&qp->ip->ref, qib_release_mmap_info);
  1052. else
  1053. vfree(qp->r_rq.wq);
  1054. free_qpn(&dev->qpn_table, qp->ibqp.qp_num);
  1055. bail_qp:
  1056. kfree(qp->s_hdr);
  1057. kfree(qp);
  1058. bail_swq:
  1059. vfree(swq);
  1060. bail:
  1061. return ret;
  1062. }
  1063. /**
  1064. * qib_destroy_qp - destroy a queue pair
  1065. * @ibqp: the queue pair to destroy
  1066. *
  1067. * Returns 0 on success.
  1068. *
  1069. * Note that this can be called while the QP is actively sending or
  1070. * receiving!
  1071. */
  1072. int qib_destroy_qp(struct ib_qp *ibqp)
  1073. {
  1074. struct qib_qp *qp = to_iqp(ibqp);
  1075. struct qib_ibdev *dev = to_idev(ibqp->device);
  1076. /* Make sure HW and driver activity is stopped. */
  1077. spin_lock_irq(&qp->s_lock);
  1078. if (qp->state != IB_QPS_RESET) {
  1079. qp->state = IB_QPS_RESET;
  1080. spin_lock(&dev->pending_lock);
  1081. if (!list_empty(&qp->iowait))
  1082. list_del_init(&qp->iowait);
  1083. spin_unlock(&dev->pending_lock);
  1084. qp->s_flags &= ~(QIB_S_TIMER | QIB_S_ANY_WAIT);
  1085. spin_unlock_irq(&qp->s_lock);
  1086. cancel_work_sync(&qp->s_work);
  1087. del_timer_sync(&qp->s_timer);
  1088. wait_event(qp->wait_dma, !atomic_read(&qp->s_dma_busy));
  1089. if (qp->s_tx) {
  1090. qib_put_txreq(qp->s_tx);
  1091. qp->s_tx = NULL;
  1092. }
  1093. remove_qp(dev, qp);
  1094. wait_event(qp->wait, !atomic_read(&qp->refcount));
  1095. clear_mr_refs(qp, 1);
  1096. } else
  1097. spin_unlock_irq(&qp->s_lock);
  1098. /* all user's cleaned up, mark it available */
  1099. free_qpn(&dev->qpn_table, qp->ibqp.qp_num);
  1100. spin_lock(&dev->n_qps_lock);
  1101. dev->n_qps_allocated--;
  1102. spin_unlock(&dev->n_qps_lock);
  1103. if (qp->ip)
  1104. kref_put(&qp->ip->ref, qib_release_mmap_info);
  1105. else
  1106. vfree(qp->r_rq.wq);
  1107. vfree(qp->s_wq);
  1108. kfree(qp->s_hdr);
  1109. kfree(qp);
  1110. return 0;
  1111. }
  1112. /**
  1113. * qib_init_qpn_table - initialize the QP number table for a device
  1114. * @qpt: the QPN table
  1115. */
  1116. void qib_init_qpn_table(struct qib_devdata *dd, struct qib_qpn_table *qpt)
  1117. {
  1118. spin_lock_init(&qpt->lock);
  1119. qpt->last = 1; /* start with QPN 2 */
  1120. qpt->nmaps = 1;
  1121. qpt->mask = dd->qpn_mask;
  1122. }
  1123. /**
  1124. * qib_free_qpn_table - free the QP number table for a device
  1125. * @qpt: the QPN table
  1126. */
  1127. void qib_free_qpn_table(struct qib_qpn_table *qpt)
  1128. {
  1129. int i;
  1130. for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
  1131. if (qpt->map[i].page)
  1132. free_page((unsigned long) qpt->map[i].page);
  1133. }
  1134. /**
  1135. * qib_get_credit - flush the send work queue of a QP
  1136. * @qp: the qp who's send work queue to flush
  1137. * @aeth: the Acknowledge Extended Transport Header
  1138. *
  1139. * The QP s_lock should be held.
  1140. */
  1141. void qib_get_credit(struct qib_qp *qp, u32 aeth)
  1142. {
  1143. u32 credit = (aeth >> QIB_AETH_CREDIT_SHIFT) & QIB_AETH_CREDIT_MASK;
  1144. /*
  1145. * If the credit is invalid, we can send
  1146. * as many packets as we like. Otherwise, we have to
  1147. * honor the credit field.
  1148. */
  1149. if (credit == QIB_AETH_CREDIT_INVAL) {
  1150. if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT)) {
  1151. qp->s_flags |= QIB_S_UNLIMITED_CREDIT;
  1152. if (qp->s_flags & QIB_S_WAIT_SSN_CREDIT) {
  1153. qp->s_flags &= ~QIB_S_WAIT_SSN_CREDIT;
  1154. qib_schedule_send(qp);
  1155. }
  1156. }
  1157. } else if (!(qp->s_flags & QIB_S_UNLIMITED_CREDIT)) {
  1158. /* Compute new LSN (i.e., MSN + credit) */
  1159. credit = (aeth + credit_table[credit]) & QIB_MSN_MASK;
  1160. if (qib_cmp24(credit, qp->s_lsn) > 0) {
  1161. qp->s_lsn = credit;
  1162. if (qp->s_flags & QIB_S_WAIT_SSN_CREDIT) {
  1163. qp->s_flags &= ~QIB_S_WAIT_SSN_CREDIT;
  1164. qib_schedule_send(qp);
  1165. }
  1166. }
  1167. }
  1168. }
  1169. #ifdef CONFIG_DEBUG_FS
  1170. struct qib_qp_iter {
  1171. struct qib_ibdev *dev;
  1172. struct qib_qp *qp;
  1173. int n;
  1174. };
  1175. struct qib_qp_iter *qib_qp_iter_init(struct qib_ibdev *dev)
  1176. {
  1177. struct qib_qp_iter *iter;
  1178. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1179. if (!iter)
  1180. return NULL;
  1181. iter->dev = dev;
  1182. if (qib_qp_iter_next(iter)) {
  1183. kfree(iter);
  1184. return NULL;
  1185. }
  1186. return iter;
  1187. }
  1188. int qib_qp_iter_next(struct qib_qp_iter *iter)
  1189. {
  1190. struct qib_ibdev *dev = iter->dev;
  1191. int n = iter->n;
  1192. int ret = 1;
  1193. struct qib_qp *pqp = iter->qp;
  1194. struct qib_qp *qp;
  1195. for (; n < dev->qp_table_size; n++) {
  1196. if (pqp)
  1197. qp = rcu_dereference(pqp->next);
  1198. else
  1199. qp = rcu_dereference(dev->qp_table[n]);
  1200. pqp = qp;
  1201. if (qp) {
  1202. iter->qp = qp;
  1203. iter->n = n;
  1204. return 0;
  1205. }
  1206. }
  1207. return ret;
  1208. }
  1209. static const char * const qp_type_str[] = {
  1210. "SMI", "GSI", "RC", "UC", "UD",
  1211. };
  1212. void qib_qp_iter_print(struct seq_file *s, struct qib_qp_iter *iter)
  1213. {
  1214. struct qib_swqe *wqe;
  1215. struct qib_qp *qp = iter->qp;
  1216. wqe = get_swqe_ptr(qp, qp->s_last);
  1217. seq_printf(s,
  1218. "N %d QP%u %s %u %u %u f=%x %u %u %u %u %u PSN %x %x %x %x %x (%u %u %u %u %u %u) QP%u LID %x\n",
  1219. iter->n,
  1220. qp->ibqp.qp_num,
  1221. qp_type_str[qp->ibqp.qp_type],
  1222. qp->state,
  1223. wqe->wr.opcode,
  1224. qp->s_hdrwords,
  1225. qp->s_flags,
  1226. atomic_read(&qp->s_dma_busy),
  1227. !list_empty(&qp->iowait),
  1228. qp->timeout,
  1229. wqe->ssn,
  1230. qp->s_lsn,
  1231. qp->s_last_psn,
  1232. qp->s_psn, qp->s_next_psn,
  1233. qp->s_sending_psn, qp->s_sending_hpsn,
  1234. qp->s_last, qp->s_acked, qp->s_cur,
  1235. qp->s_tail, qp->s_head, qp->s_size,
  1236. qp->remote_qpn,
  1237. qp->remote_ah_attr.dlid);
  1238. }
  1239. #endif