qp.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /*
  2. * Copyright(c) 2015 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/hash.h>
  48. #include <linux/bitops.h>
  49. #include <linux/lockdep.h>
  50. #include <linux/vmalloc.h>
  51. #include <linux/slab.h>
  52. #include <rdma/ib_verbs.h>
  53. #include "qp.h"
  54. #include "vt.h"
  55. #include "trace.h"
  56. /*
  57. * Note that it is OK to post send work requests in the SQE and ERR
  58. * states; rvt_do_send() will process them and generate error
  59. * completions as per IB 1.2 C10-96.
  60. */
  61. const int ib_rvt_state_ops[IB_QPS_ERR + 1] = {
  62. [IB_QPS_RESET] = 0,
  63. [IB_QPS_INIT] = RVT_POST_RECV_OK,
  64. [IB_QPS_RTR] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK,
  65. [IB_QPS_RTS] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  66. RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK |
  67. RVT_PROCESS_NEXT_SEND_OK,
  68. [IB_QPS_SQD] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  69. RVT_POST_SEND_OK | RVT_PROCESS_SEND_OK,
  70. [IB_QPS_SQE] = RVT_POST_RECV_OK | RVT_PROCESS_RECV_OK |
  71. RVT_POST_SEND_OK | RVT_FLUSH_SEND,
  72. [IB_QPS_ERR] = RVT_POST_RECV_OK | RVT_FLUSH_RECV |
  73. RVT_POST_SEND_OK | RVT_FLUSH_SEND,
  74. };
  75. EXPORT_SYMBOL(ib_rvt_state_ops);
  76. static void get_map_page(struct rvt_qpn_table *qpt,
  77. struct rvt_qpn_map *map,
  78. gfp_t gfp)
  79. {
  80. unsigned long page = get_zeroed_page(gfp);
  81. /*
  82. * Free the page if someone raced with us installing it.
  83. */
  84. spin_lock(&qpt->lock);
  85. if (map->page)
  86. free_page(page);
  87. else
  88. map->page = (void *)page;
  89. spin_unlock(&qpt->lock);
  90. }
  91. /**
  92. * init_qpn_table - initialize the QP number table for a device
  93. * @qpt: the QPN table
  94. */
  95. static int init_qpn_table(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt)
  96. {
  97. u32 offset, i;
  98. struct rvt_qpn_map *map;
  99. int ret = 0;
  100. if (!(rdi->dparms.qpn_res_end >= rdi->dparms.qpn_res_start))
  101. return -EINVAL;
  102. spin_lock_init(&qpt->lock);
  103. qpt->last = rdi->dparms.qpn_start;
  104. qpt->incr = rdi->dparms.qpn_inc << rdi->dparms.qos_shift;
  105. /*
  106. * Drivers may want some QPs beyond what we need for verbs let them use
  107. * our qpn table. No need for two. Lets go ahead and mark the bitmaps
  108. * for those. The reserved range must be *after* the range which verbs
  109. * will pick from.
  110. */
  111. /* Figure out number of bit maps needed before reserved range */
  112. qpt->nmaps = rdi->dparms.qpn_res_start / RVT_BITS_PER_PAGE;
  113. /* This should always be zero */
  114. offset = rdi->dparms.qpn_res_start & RVT_BITS_PER_PAGE_MASK;
  115. /* Starting with the first reserved bit map */
  116. map = &qpt->map[qpt->nmaps];
  117. rvt_pr_info(rdi, "Reserving QPNs from 0x%x to 0x%x for non-verbs use\n",
  118. rdi->dparms.qpn_res_start, rdi->dparms.qpn_res_end);
  119. for (i = rdi->dparms.qpn_res_start; i <= rdi->dparms.qpn_res_end; i++) {
  120. if (!map->page) {
  121. get_map_page(qpt, map, GFP_KERNEL);
  122. if (!map->page) {
  123. ret = -ENOMEM;
  124. break;
  125. }
  126. }
  127. set_bit(offset, map->page);
  128. offset++;
  129. if (offset == RVT_BITS_PER_PAGE) {
  130. /* next page */
  131. qpt->nmaps++;
  132. map++;
  133. offset = 0;
  134. }
  135. }
  136. return ret;
  137. }
  138. /**
  139. * free_qpn_table - free the QP number table for a device
  140. * @qpt: the QPN table
  141. */
  142. static void free_qpn_table(struct rvt_qpn_table *qpt)
  143. {
  144. int i;
  145. for (i = 0; i < ARRAY_SIZE(qpt->map); i++)
  146. free_page((unsigned long)qpt->map[i].page);
  147. }
  148. int rvt_driver_qp_init(struct rvt_dev_info *rdi)
  149. {
  150. int i;
  151. int ret = -ENOMEM;
  152. if (rdi->flags & RVT_FLAG_QP_INIT_DRIVER) {
  153. rvt_pr_info(rdi, "Driver is doing QP init.\n");
  154. return 0;
  155. }
  156. if (!rdi->dparms.qp_table_size)
  157. return -EINVAL;
  158. /*
  159. * If driver is not doing any QP allocation then make sure it is
  160. * providing the necessary QP functions.
  161. */
  162. if (!rdi->driver_f.free_all_qps ||
  163. !rdi->driver_f.qp_priv_alloc ||
  164. !rdi->driver_f.qp_priv_free ||
  165. !rdi->driver_f.notify_qp_reset)
  166. return -EINVAL;
  167. /* allocate parent object */
  168. rdi->qp_dev = kzalloc(sizeof(*rdi->qp_dev), GFP_KERNEL);
  169. if (!rdi->qp_dev)
  170. return -ENOMEM;
  171. /* allocate hash table */
  172. rdi->qp_dev->qp_table_size = rdi->dparms.qp_table_size;
  173. rdi->qp_dev->qp_table_bits = ilog2(rdi->dparms.qp_table_size);
  174. rdi->qp_dev->qp_table =
  175. kmalloc(rdi->qp_dev->qp_table_size *
  176. sizeof(*rdi->qp_dev->qp_table),
  177. GFP_KERNEL);
  178. if (!rdi->qp_dev->qp_table)
  179. goto no_qp_table;
  180. for (i = 0; i < rdi->qp_dev->qp_table_size; i++)
  181. RCU_INIT_POINTER(rdi->qp_dev->qp_table[i], NULL);
  182. spin_lock_init(&rdi->qp_dev->qpt_lock);
  183. /* initialize qpn map */
  184. if (init_qpn_table(rdi, &rdi->qp_dev->qpn_table))
  185. goto fail_table;
  186. spin_lock_init(&rdi->n_qps_lock);
  187. return 0;
  188. fail_table:
  189. kfree(rdi->qp_dev->qp_table);
  190. free_qpn_table(&rdi->qp_dev->qpn_table);
  191. no_qp_table:
  192. kfree(rdi->qp_dev);
  193. return ret;
  194. }
  195. /**
  196. * free_all_qps - check for QPs still in use
  197. * @qpt: the QP table to empty
  198. *
  199. * There should not be any QPs still in use.
  200. * Free memory for table.
  201. */
  202. static unsigned rvt_free_all_qps(struct rvt_dev_info *rdi)
  203. {
  204. unsigned long flags;
  205. struct rvt_qp *qp;
  206. unsigned n, qp_inuse = 0;
  207. spinlock_t *ql; /* work around too long line below */
  208. if (rdi->driver_f.free_all_qps)
  209. qp_inuse = rdi->driver_f.free_all_qps(rdi);
  210. if (!rdi->qp_dev)
  211. return qp_inuse;
  212. ql = &rdi->qp_dev->qpt_lock;
  213. spin_lock_irqsave(ql, flags);
  214. for (n = 0; n < rdi->qp_dev->qp_table_size; n++) {
  215. qp = rcu_dereference_protected(rdi->qp_dev->qp_table[n],
  216. lockdep_is_held(ql));
  217. RCU_INIT_POINTER(rdi->qp_dev->qp_table[n], NULL);
  218. for (; qp; qp = rcu_dereference_protected(qp->next,
  219. lockdep_is_held(ql)))
  220. qp_inuse++;
  221. }
  222. spin_unlock_irqrestore(ql, flags);
  223. synchronize_rcu();
  224. return qp_inuse;
  225. }
  226. void rvt_qp_exit(struct rvt_dev_info *rdi)
  227. {
  228. u32 qps_inuse = rvt_free_all_qps(rdi);
  229. if (qps_inuse)
  230. rvt_pr_err(rdi, "QP memory leak! %u still in use\n",
  231. qps_inuse);
  232. if (!rdi->qp_dev)
  233. return;
  234. if (rdi->flags & RVT_FLAG_QP_INIT_DRIVER)
  235. return; /* driver did the qp init so nothing else to do */
  236. kfree(rdi->qp_dev->qp_table);
  237. free_qpn_table(&rdi->qp_dev->qpn_table);
  238. kfree(rdi->qp_dev);
  239. }
  240. static inline unsigned mk_qpn(struct rvt_qpn_table *qpt,
  241. struct rvt_qpn_map *map, unsigned off)
  242. {
  243. return (map - qpt->map) * RVT_BITS_PER_PAGE + off;
  244. }
  245. /*
  246. * Allocate the next available QPN or
  247. * zero/one for QP type IB_QPT_SMI/IB_QPT_GSI.
  248. */
  249. static int alloc_qpn(struct rvt_dev_info *rdi, struct rvt_qpn_table *qpt,
  250. enum ib_qp_type type, u8 port, gfp_t gfp)
  251. {
  252. u32 i, offset, max_scan, qpn;
  253. struct rvt_qpn_map *map;
  254. u32 ret;
  255. if (rdi->driver_f.alloc_qpn)
  256. return rdi->driver_f.alloc_qpn(rdi, qpt, type, port,
  257. GFP_KERNEL);
  258. if (type == IB_QPT_SMI || type == IB_QPT_GSI) {
  259. unsigned n;
  260. ret = type == IB_QPT_GSI;
  261. n = 1 << (ret + 2 * (port - 1));
  262. spin_lock(&qpt->lock);
  263. if (qpt->flags & n)
  264. ret = -EINVAL;
  265. else
  266. qpt->flags |= n;
  267. spin_unlock(&qpt->lock);
  268. goto bail;
  269. }
  270. qpn = qpt->last + qpt->incr;
  271. if (qpn >= RVT_QPN_MAX)
  272. qpn = qpt->incr | ((qpt->last & 1) ^ 1);
  273. /* offset carries bit 0 */
  274. offset = qpn & RVT_BITS_PER_PAGE_MASK;
  275. map = &qpt->map[qpn / RVT_BITS_PER_PAGE];
  276. max_scan = qpt->nmaps - !offset;
  277. for (i = 0;;) {
  278. if (unlikely(!map->page)) {
  279. get_map_page(qpt, map, gfp);
  280. if (unlikely(!map->page))
  281. break;
  282. }
  283. do {
  284. if (!test_and_set_bit(offset, map->page)) {
  285. qpt->last = qpn;
  286. ret = qpn;
  287. goto bail;
  288. }
  289. offset += qpt->incr;
  290. /*
  291. * This qpn might be bogus if offset >= BITS_PER_PAGE.
  292. * That is OK. It gets re-assigned below
  293. */
  294. qpn = mk_qpn(qpt, map, offset);
  295. } while (offset < RVT_BITS_PER_PAGE && qpn < RVT_QPN_MAX);
  296. /*
  297. * In order to keep the number of pages allocated to a
  298. * minimum, we scan the all existing pages before increasing
  299. * the size of the bitmap table.
  300. */
  301. if (++i > max_scan) {
  302. if (qpt->nmaps == RVT_QPNMAP_ENTRIES)
  303. break;
  304. map = &qpt->map[qpt->nmaps++];
  305. /* start at incr with current bit 0 */
  306. offset = qpt->incr | (offset & 1);
  307. } else if (map < &qpt->map[qpt->nmaps]) {
  308. ++map;
  309. /* start at incr with current bit 0 */
  310. offset = qpt->incr | (offset & 1);
  311. } else {
  312. map = &qpt->map[0];
  313. /* wrap to first map page, invert bit 0 */
  314. offset = qpt->incr | ((offset & 1) ^ 1);
  315. }
  316. /* there can be no bits at shift and below */
  317. WARN_ON(offset & (rdi->dparms.qos_shift - 1));
  318. qpn = mk_qpn(qpt, map, offset);
  319. }
  320. ret = -ENOMEM;
  321. bail:
  322. return ret;
  323. }
  324. static void free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  325. {
  326. struct rvt_qpn_map *map;
  327. map = qpt->map + qpn / RVT_BITS_PER_PAGE;
  328. if (map->page)
  329. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  330. }
  331. /**
  332. * reset_qp - initialize the QP state to the reset state
  333. * @qp: the QP to reset
  334. * @type: the QP type
  335. * r and s lock are required to be held by the caller
  336. */
  337. void rvt_reset_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp,
  338. enum ib_qp_type type)
  339. {
  340. if (qp->state != IB_QPS_RESET) {
  341. qp->state = IB_QPS_RESET;
  342. /* Let drivers flush their waitlist */
  343. rdi->driver_f.flush_qp_waiters(qp);
  344. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_ANY_WAIT);
  345. spin_unlock(&qp->s_lock);
  346. spin_unlock_irq(&qp->r_lock);
  347. /* Stop the send queue and the retry timer */
  348. rdi->driver_f.stop_send_queue(qp);
  349. del_timer_sync(&qp->s_timer);
  350. /* Wait for things to stop */
  351. rdi->driver_f.quiesce_qp(qp);
  352. /* take qp out the hash and wait for it to be unused */
  353. rvt_remove_qp(rdi, qp);
  354. wait_event(qp->wait, !atomic_read(&qp->refcount));
  355. /* grab the lock b/c it was locked at call time */
  356. spin_lock_irq(&qp->r_lock);
  357. spin_lock(&qp->s_lock);
  358. rvt_clear_mr_refs(qp, 1);
  359. }
  360. /*
  361. * Let the driver do any tear down it needs to for a qp
  362. * that has been reset
  363. */
  364. rdi->driver_f.notify_qp_reset(qp);
  365. qp->remote_qpn = 0;
  366. qp->qkey = 0;
  367. qp->qp_access_flags = 0;
  368. qp->s_flags &= RVT_S_SIGNAL_REQ_WR;
  369. qp->s_hdrwords = 0;
  370. qp->s_wqe = NULL;
  371. qp->s_draining = 0;
  372. qp->s_next_psn = 0;
  373. qp->s_last_psn = 0;
  374. qp->s_sending_psn = 0;
  375. qp->s_sending_hpsn = 0;
  376. qp->s_psn = 0;
  377. qp->r_psn = 0;
  378. qp->r_msn = 0;
  379. if (type == IB_QPT_RC) {
  380. qp->s_state = IB_OPCODE_RC_SEND_LAST;
  381. qp->r_state = IB_OPCODE_RC_SEND_LAST;
  382. } else {
  383. qp->s_state = IB_OPCODE_UC_SEND_LAST;
  384. qp->r_state = IB_OPCODE_UC_SEND_LAST;
  385. }
  386. qp->s_ack_state = IB_OPCODE_RC_ACKNOWLEDGE;
  387. qp->r_nak_state = 0;
  388. qp->r_aflags = 0;
  389. qp->r_flags = 0;
  390. qp->s_head = 0;
  391. qp->s_tail = 0;
  392. qp->s_cur = 0;
  393. qp->s_acked = 0;
  394. qp->s_last = 0;
  395. qp->s_ssn = 1;
  396. qp->s_lsn = 0;
  397. qp->s_mig_state = IB_MIG_MIGRATED;
  398. memset(qp->s_ack_queue, 0, sizeof(qp->s_ack_queue));
  399. qp->r_head_ack_queue = 0;
  400. qp->s_tail_ack_queue = 0;
  401. qp->s_num_rd_atomic = 0;
  402. if (qp->r_rq.wq) {
  403. qp->r_rq.wq->head = 0;
  404. qp->r_rq.wq->tail = 0;
  405. }
  406. qp->r_sge.num_sge = 0;
  407. }
  408. EXPORT_SYMBOL(rvt_reset_qp);
  409. /**
  410. * rvt_create_qp - create a queue pair for a device
  411. * @ibpd: the protection domain who's device we create the queue pair for
  412. * @init_attr: the attributes of the queue pair
  413. * @udata: user data for libibverbs.so
  414. *
  415. * Queue pair creation is mostly an rvt issue. However, drivers have their own
  416. * unique idea of what queue pair numbers mean. For instance there is a reserved
  417. * range for PSM.
  418. *
  419. * Returns the queue pair on success, otherwise returns an errno.
  420. *
  421. * Called by the ib_create_qp() core verbs function.
  422. */
  423. struct ib_qp *rvt_create_qp(struct ib_pd *ibpd,
  424. struct ib_qp_init_attr *init_attr,
  425. struct ib_udata *udata)
  426. {
  427. struct rvt_qp *qp;
  428. int err;
  429. struct rvt_swqe *swq = NULL;
  430. size_t sz;
  431. size_t sg_list_sz;
  432. struct ib_qp *ret = ERR_PTR(-ENOMEM);
  433. struct rvt_dev_info *rdi = ib_to_rvt(ibpd->device);
  434. void *priv = NULL;
  435. gfp_t gfp;
  436. if (!rdi)
  437. return ERR_PTR(-EINVAL);
  438. if (init_attr->cap.max_send_sge > rdi->dparms.props.max_sge ||
  439. init_attr->cap.max_send_wr > rdi->dparms.props.max_qp_wr ||
  440. init_attr->create_flags & ~(IB_QP_CREATE_USE_GFP_NOIO))
  441. return ERR_PTR(-EINVAL);
  442. /* GFP_NOIO is applicable to RC QP's only */
  443. if (init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO &&
  444. init_attr->qp_type != IB_QPT_RC)
  445. return ERR_PTR(-EINVAL);
  446. gfp = init_attr->create_flags & IB_QP_CREATE_USE_GFP_NOIO ?
  447. GFP_NOIO : GFP_KERNEL;
  448. /* Check receive queue parameters if no SRQ is specified. */
  449. if (!init_attr->srq) {
  450. if (init_attr->cap.max_recv_sge > rdi->dparms.props.max_sge ||
  451. init_attr->cap.max_recv_wr > rdi->dparms.props.max_qp_wr)
  452. return ERR_PTR(-EINVAL);
  453. if (init_attr->cap.max_send_sge +
  454. init_attr->cap.max_send_wr +
  455. init_attr->cap.max_recv_sge +
  456. init_attr->cap.max_recv_wr == 0)
  457. return ERR_PTR(-EINVAL);
  458. }
  459. switch (init_attr->qp_type) {
  460. case IB_QPT_SMI:
  461. case IB_QPT_GSI:
  462. if (init_attr->port_num == 0 ||
  463. init_attr->port_num > ibpd->device->phys_port_cnt)
  464. return ERR_PTR(-EINVAL);
  465. case IB_QPT_UC:
  466. case IB_QPT_RC:
  467. case IB_QPT_UD:
  468. sz = sizeof(struct rvt_sge) *
  469. init_attr->cap.max_send_sge +
  470. sizeof(struct rvt_swqe);
  471. if (gfp == GFP_NOIO)
  472. swq = __vmalloc(
  473. (init_attr->cap.max_send_wr + 1) * sz,
  474. gfp, PAGE_KERNEL);
  475. else
  476. swq = vmalloc(
  477. (init_attr->cap.max_send_wr + 1) * sz);
  478. if (!swq)
  479. return ERR_PTR(-ENOMEM);
  480. sz = sizeof(*qp);
  481. sg_list_sz = 0;
  482. if (init_attr->srq) {
  483. struct rvt_srq *srq = ibsrq_to_rvtsrq(init_attr->srq);
  484. if (srq->rq.max_sge > 1)
  485. sg_list_sz = sizeof(*qp->r_sg_list) *
  486. (srq->rq.max_sge - 1);
  487. } else if (init_attr->cap.max_recv_sge > 1)
  488. sg_list_sz = sizeof(*qp->r_sg_list) *
  489. (init_attr->cap.max_recv_sge - 1);
  490. qp = kzalloc(sz + sg_list_sz, gfp);
  491. if (!qp)
  492. goto bail_swq;
  493. RCU_INIT_POINTER(qp->next, NULL);
  494. /*
  495. * Driver needs to set up it's private QP structure and do any
  496. * initialization that is needed.
  497. */
  498. priv = rdi->driver_f.qp_priv_alloc(rdi, qp, gfp);
  499. if (!priv)
  500. goto bail_qp;
  501. qp->priv = priv;
  502. qp->timeout_jiffies =
  503. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  504. 1000UL);
  505. if (init_attr->srq) {
  506. sz = 0;
  507. } else {
  508. qp->r_rq.size = init_attr->cap.max_recv_wr + 1;
  509. qp->r_rq.max_sge = init_attr->cap.max_recv_sge;
  510. sz = (sizeof(struct ib_sge) * qp->r_rq.max_sge) +
  511. sizeof(struct rvt_rwqe);
  512. if (udata)
  513. qp->r_rq.wq = vmalloc_user(
  514. sizeof(struct rvt_rwq) +
  515. qp->r_rq.size * sz);
  516. else if (gfp == GFP_NOIO)
  517. qp->r_rq.wq = __vmalloc(
  518. sizeof(struct rvt_rwq) +
  519. qp->r_rq.size * sz,
  520. gfp, PAGE_KERNEL);
  521. else
  522. qp->r_rq.wq = vmalloc(
  523. sizeof(struct rvt_rwq) +
  524. qp->r_rq.size * sz);
  525. if (!qp->r_rq.wq)
  526. goto bail_driver_priv;
  527. }
  528. /*
  529. * ib_create_qp() will initialize qp->ibqp
  530. * except for qp->ibqp.qp_num.
  531. */
  532. spin_lock_init(&qp->r_lock);
  533. spin_lock_init(&qp->s_lock);
  534. spin_lock_init(&qp->r_rq.lock);
  535. atomic_set(&qp->refcount, 0);
  536. init_waitqueue_head(&qp->wait);
  537. init_timer(&qp->s_timer);
  538. qp->s_timer.data = (unsigned long)qp;
  539. INIT_LIST_HEAD(&qp->rspwait);
  540. qp->state = IB_QPS_RESET;
  541. qp->s_wq = swq;
  542. qp->s_size = init_attr->cap.max_send_wr + 1;
  543. qp->s_max_sge = init_attr->cap.max_send_sge;
  544. if (init_attr->sq_sig_type == IB_SIGNAL_REQ_WR)
  545. qp->s_flags = RVT_S_SIGNAL_REQ_WR;
  546. err = alloc_qpn(rdi, &rdi->qp_dev->qpn_table,
  547. init_attr->qp_type,
  548. init_attr->port_num, gfp);
  549. if (err < 0) {
  550. ret = ERR_PTR(err);
  551. goto bail_rq_wq;
  552. }
  553. qp->ibqp.qp_num = err;
  554. qp->port_num = init_attr->port_num;
  555. rvt_reset_qp(rdi, qp, init_attr->qp_type);
  556. break;
  557. default:
  558. /* Don't support raw QPs */
  559. return ERR_PTR(-EINVAL);
  560. }
  561. init_attr->cap.max_inline_data = 0;
  562. /*
  563. * Return the address of the RWQ as the offset to mmap.
  564. * See rvt_mmap() for details.
  565. */
  566. if (udata && udata->outlen >= sizeof(__u64)) {
  567. if (!qp->r_rq.wq) {
  568. __u64 offset = 0;
  569. err = ib_copy_to_udata(udata, &offset,
  570. sizeof(offset));
  571. if (err) {
  572. ret = ERR_PTR(err);
  573. goto bail_qpn;
  574. }
  575. } else {
  576. u32 s = sizeof(struct rvt_rwq) + qp->r_rq.size * sz;
  577. qp->ip = rvt_create_mmap_info(rdi, s,
  578. ibpd->uobject->context,
  579. qp->r_rq.wq);
  580. if (!qp->ip) {
  581. ret = ERR_PTR(-ENOMEM);
  582. goto bail_qpn;
  583. }
  584. err = ib_copy_to_udata(udata, &qp->ip->offset,
  585. sizeof(qp->ip->offset));
  586. if (err) {
  587. ret = ERR_PTR(err);
  588. goto bail_ip;
  589. }
  590. }
  591. }
  592. spin_lock(&rdi->n_qps_lock);
  593. if (rdi->n_qps_allocated == rdi->dparms.props.max_qp) {
  594. spin_unlock(&rdi->n_qps_lock);
  595. ret = ERR_PTR(-ENOMEM);
  596. goto bail_ip;
  597. }
  598. rdi->n_qps_allocated++;
  599. spin_unlock(&rdi->n_qps_lock);
  600. if (qp->ip) {
  601. spin_lock_irq(&rdi->pending_lock);
  602. list_add(&qp->ip->pending_mmaps, &rdi->pending_mmaps);
  603. spin_unlock_irq(&rdi->pending_lock);
  604. }
  605. ret = &qp->ibqp;
  606. /*
  607. * We have our QP and its good, now keep track of what types of opcodes
  608. * can be processed on this QP. We do this by keeping track of what the
  609. * 3 high order bits of the opcode are.
  610. */
  611. switch (init_attr->qp_type) {
  612. case IB_QPT_SMI:
  613. case IB_QPT_GSI:
  614. case IB_QPT_UD:
  615. qp->allowed_ops = IB_OPCODE_UD_SEND_ONLY & RVT_OPCODE_QP_MASK;
  616. break;
  617. case IB_QPT_RC:
  618. qp->allowed_ops = IB_OPCODE_RC_SEND_ONLY & RVT_OPCODE_QP_MASK;
  619. break;
  620. case IB_QPT_UC:
  621. qp->allowed_ops = IB_OPCODE_UC_SEND_ONLY & RVT_OPCODE_QP_MASK;
  622. break;
  623. default:
  624. ret = ERR_PTR(-EINVAL);
  625. goto bail_ip;
  626. }
  627. return ret;
  628. bail_ip:
  629. kref_put(&qp->ip->ref, rvt_release_mmap_info);
  630. bail_qpn:
  631. free_qpn(&rdi->qp_dev->qpn_table, qp->ibqp.qp_num);
  632. bail_rq_wq:
  633. vfree(qp->r_rq.wq);
  634. bail_driver_priv:
  635. rdi->driver_f.qp_priv_free(rdi, qp);
  636. bail_qp:
  637. kfree(qp);
  638. bail_swq:
  639. vfree(swq);
  640. return ret;
  641. }
  642. void rvt_clear_mr_refs(struct rvt_qp *qp, int clr_sends)
  643. {
  644. unsigned n;
  645. if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
  646. rvt_put_ss(&qp->s_rdma_read_sge);
  647. rvt_put_ss(&qp->r_sge);
  648. if (clr_sends) {
  649. while (qp->s_last != qp->s_head) {
  650. struct rvt_swqe *wqe = rvt_get_swqe_ptr(qp, qp->s_last);
  651. unsigned i;
  652. for (i = 0; i < wqe->wr.num_sge; i++) {
  653. struct rvt_sge *sge = &wqe->sg_list[i];
  654. rvt_put_mr(sge->mr);
  655. }
  656. if (qp->ibqp.qp_type == IB_QPT_UD ||
  657. qp->ibqp.qp_type == IB_QPT_SMI ||
  658. qp->ibqp.qp_type == IB_QPT_GSI)
  659. atomic_dec(&ibah_to_rvtah(
  660. wqe->ud_wr.ah)->refcount);
  661. if (++qp->s_last >= qp->s_size)
  662. qp->s_last = 0;
  663. }
  664. if (qp->s_rdma_mr) {
  665. rvt_put_mr(qp->s_rdma_mr);
  666. qp->s_rdma_mr = NULL;
  667. }
  668. }
  669. if (qp->ibqp.qp_type != IB_QPT_RC)
  670. return;
  671. for (n = 0; n < ARRAY_SIZE(qp->s_ack_queue); n++) {
  672. struct rvt_ack_entry *e = &qp->s_ack_queue[n];
  673. if (e->opcode == IB_OPCODE_RC_RDMA_READ_REQUEST &&
  674. e->rdma_sge.mr) {
  675. rvt_put_mr(e->rdma_sge.mr);
  676. e->rdma_sge.mr = NULL;
  677. }
  678. }
  679. }
  680. EXPORT_SYMBOL(rvt_clear_mr_refs);
  681. /**
  682. * rvt_error_qp - put a QP into the error state
  683. * @qp: the QP to put into the error state
  684. * @err: the receive completion error to signal if a RWQE is active
  685. *
  686. * Flushes both send and receive work queues.
  687. * Returns true if last WQE event should be generated.
  688. * The QP r_lock and s_lock should be held and interrupts disabled.
  689. * If we are already in error state, just return.
  690. */
  691. int rvt_error_qp(struct rvt_qp *qp, enum ib_wc_status err)
  692. {
  693. struct ib_wc wc;
  694. int ret = 0;
  695. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  696. if (qp->state == IB_QPS_ERR || qp->state == IB_QPS_RESET)
  697. goto bail;
  698. qp->state = IB_QPS_ERR;
  699. if (qp->s_flags & (RVT_S_TIMER | RVT_S_WAIT_RNR)) {
  700. qp->s_flags &= ~(RVT_S_TIMER | RVT_S_WAIT_RNR);
  701. del_timer(&qp->s_timer);
  702. }
  703. if (qp->s_flags & RVT_S_ANY_WAIT_SEND)
  704. qp->s_flags &= ~RVT_S_ANY_WAIT_SEND;
  705. rdi->driver_f.notify_error_qp(qp);
  706. /* Schedule the sending tasklet to drain the send work queue. */
  707. if (qp->s_last != qp->s_head)
  708. rdi->driver_f.schedule_send(qp);
  709. rvt_clear_mr_refs(qp, 0);
  710. memset(&wc, 0, sizeof(wc));
  711. wc.qp = &qp->ibqp;
  712. wc.opcode = IB_WC_RECV;
  713. if (test_and_clear_bit(RVT_R_WRID_VALID, &qp->r_aflags)) {
  714. wc.wr_id = qp->r_wr_id;
  715. wc.status = err;
  716. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  717. }
  718. wc.status = IB_WC_WR_FLUSH_ERR;
  719. if (qp->r_rq.wq) {
  720. struct rvt_rwq *wq;
  721. u32 head;
  722. u32 tail;
  723. spin_lock(&qp->r_rq.lock);
  724. /* sanity check pointers before trusting them */
  725. wq = qp->r_rq.wq;
  726. head = wq->head;
  727. if (head >= qp->r_rq.size)
  728. head = 0;
  729. tail = wq->tail;
  730. if (tail >= qp->r_rq.size)
  731. tail = 0;
  732. while (tail != head) {
  733. wc.wr_id = rvt_get_rwqe_ptr(&qp->r_rq, tail)->wr_id;
  734. if (++tail >= qp->r_rq.size)
  735. tail = 0;
  736. rvt_cq_enter(ibcq_to_rvtcq(qp->ibqp.recv_cq), &wc, 1);
  737. }
  738. wq->tail = tail;
  739. spin_unlock(&qp->r_rq.lock);
  740. } else if (qp->ibqp.event_handler) {
  741. ret = 1;
  742. }
  743. bail:
  744. return ret;
  745. }
  746. EXPORT_SYMBOL(rvt_error_qp);
  747. /*
  748. * Put the QP into the hash table.
  749. * The hash table holds a reference to the QP.
  750. */
  751. static void rvt_insert_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  752. {
  753. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  754. unsigned long flags;
  755. atomic_inc(&qp->refcount);
  756. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  757. if (qp->ibqp.qp_num <= 1) {
  758. rcu_assign_pointer(rvp->qp[qp->ibqp.qp_num], qp);
  759. } else {
  760. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  761. qp->next = rdi->qp_dev->qp_table[n];
  762. rcu_assign_pointer(rdi->qp_dev->qp_table[n], qp);
  763. trace_rvt_qpinsert(qp, n);
  764. }
  765. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  766. }
  767. /*
  768. * Remove the QP from the table so it can't be found asynchronously by
  769. * the receive routine.
  770. */
  771. void rvt_remove_qp(struct rvt_dev_info *rdi, struct rvt_qp *qp)
  772. {
  773. struct rvt_ibport *rvp = rdi->ports[qp->port_num - 1];
  774. u32 n = hash_32(qp->ibqp.qp_num, rdi->qp_dev->qp_table_bits);
  775. unsigned long flags;
  776. int removed = 1;
  777. spin_lock_irqsave(&rdi->qp_dev->qpt_lock, flags);
  778. if (rcu_dereference_protected(rvp->qp[0],
  779. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  780. RCU_INIT_POINTER(rvp->qp[0], NULL);
  781. } else if (rcu_dereference_protected(rvp->qp[1],
  782. lockdep_is_held(&rdi->qp_dev->qpt_lock)) == qp) {
  783. RCU_INIT_POINTER(rvp->qp[1], NULL);
  784. } else {
  785. struct rvt_qp *q;
  786. struct rvt_qp __rcu **qpp;
  787. removed = 0;
  788. qpp = &rdi->qp_dev->qp_table[n];
  789. for (; (q = rcu_dereference_protected(*qpp,
  790. lockdep_is_held(&rdi->qp_dev->qpt_lock))) != NULL;
  791. qpp = &q->next) {
  792. if (q == qp) {
  793. RCU_INIT_POINTER(*qpp,
  794. rcu_dereference_protected(qp->next,
  795. lockdep_is_held(&rdi->qp_dev->qpt_lock)));
  796. removed = 1;
  797. trace_rvt_qpremove(qp, n);
  798. break;
  799. }
  800. }
  801. }
  802. spin_unlock_irqrestore(&rdi->qp_dev->qpt_lock, flags);
  803. if (removed) {
  804. synchronize_rcu();
  805. if (atomic_dec_and_test(&qp->refcount))
  806. wake_up(&qp->wait);
  807. }
  808. }
  809. EXPORT_SYMBOL(rvt_remove_qp);
  810. /**
  811. * qib_modify_qp - modify the attributes of a queue pair
  812. * @ibqp: the queue pair who's attributes we're modifying
  813. * @attr: the new attributes
  814. * @attr_mask: the mask of attributes to modify
  815. * @udata: user data for libibverbs.so
  816. *
  817. * Returns 0 on success, otherwise returns an errno.
  818. */
  819. int rvt_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  820. int attr_mask, struct ib_udata *udata)
  821. {
  822. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  823. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  824. enum ib_qp_state cur_state, new_state;
  825. struct ib_event ev;
  826. int lastwqe = 0;
  827. int mig = 0;
  828. int pmtu = 0; /* for gcc warning only */
  829. enum rdma_link_layer link;
  830. link = rdma_port_get_link_layer(ibqp->device, qp->port_num);
  831. spin_lock_irq(&qp->r_lock);
  832. spin_lock(&qp->s_lock);
  833. cur_state = attr_mask & IB_QP_CUR_STATE ?
  834. attr->cur_qp_state : qp->state;
  835. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  836. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  837. attr_mask, link))
  838. goto inval;
  839. if (attr_mask & IB_QP_AV) {
  840. if (attr->ah_attr.dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
  841. goto inval;
  842. if (rvt_check_ah(qp->ibqp.device, &attr->ah_attr))
  843. goto inval;
  844. }
  845. if (attr_mask & IB_QP_ALT_PATH) {
  846. if (attr->alt_ah_attr.dlid >=
  847. be16_to_cpu(IB_MULTICAST_LID_BASE))
  848. goto inval;
  849. if (rvt_check_ah(qp->ibqp.device, &attr->alt_ah_attr))
  850. goto inval;
  851. if (attr->alt_pkey_index >= rvt_get_npkeys(rdi))
  852. goto inval;
  853. }
  854. if (attr_mask & IB_QP_PKEY_INDEX)
  855. if (attr->pkey_index >= rvt_get_npkeys(rdi))
  856. goto inval;
  857. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  858. if (attr->min_rnr_timer > 31)
  859. goto inval;
  860. if (attr_mask & IB_QP_PORT)
  861. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  862. qp->ibqp.qp_type == IB_QPT_GSI ||
  863. attr->port_num == 0 ||
  864. attr->port_num > ibqp->device->phys_port_cnt)
  865. goto inval;
  866. if (attr_mask & IB_QP_DEST_QPN)
  867. if (attr->dest_qp_num > RVT_QPN_MASK)
  868. goto inval;
  869. if (attr_mask & IB_QP_RETRY_CNT)
  870. if (attr->retry_cnt > 7)
  871. goto inval;
  872. if (attr_mask & IB_QP_RNR_RETRY)
  873. if (attr->rnr_retry > 7)
  874. goto inval;
  875. /*
  876. * Don't allow invalid path_mtu values. OK to set greater
  877. * than the active mtu (or even the max_cap, if we have tuned
  878. * that to a small mtu. We'll set qp->path_mtu
  879. * to the lesser of requested attribute mtu and active,
  880. * for packetizing messages.
  881. * Note that the QP port has to be set in INIT and MTU in RTR.
  882. */
  883. if (attr_mask & IB_QP_PATH_MTU) {
  884. pmtu = rdi->driver_f.get_pmtu_from_attr(rdi, qp, attr);
  885. if (pmtu < 0)
  886. goto inval;
  887. }
  888. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  889. if (attr->path_mig_state == IB_MIG_REARM) {
  890. if (qp->s_mig_state == IB_MIG_ARMED)
  891. goto inval;
  892. if (new_state != IB_QPS_RTS)
  893. goto inval;
  894. } else if (attr->path_mig_state == IB_MIG_MIGRATED) {
  895. if (qp->s_mig_state == IB_MIG_REARM)
  896. goto inval;
  897. if (new_state != IB_QPS_RTS && new_state != IB_QPS_SQD)
  898. goto inval;
  899. if (qp->s_mig_state == IB_MIG_ARMED)
  900. mig = 1;
  901. } else {
  902. goto inval;
  903. }
  904. }
  905. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  906. if (attr->max_dest_rd_atomic > rdi->dparms.max_rdma_atomic)
  907. goto inval;
  908. switch (new_state) {
  909. case IB_QPS_RESET:
  910. if (qp->state != IB_QPS_RESET)
  911. rvt_reset_qp(rdi, qp, ibqp->qp_type);
  912. break;
  913. case IB_QPS_RTR:
  914. /* Allow event to re-trigger if QP set to RTR more than once */
  915. qp->r_flags &= ~RVT_R_COMM_EST;
  916. qp->state = new_state;
  917. break;
  918. case IB_QPS_SQD:
  919. qp->s_draining = qp->s_last != qp->s_cur;
  920. qp->state = new_state;
  921. break;
  922. case IB_QPS_SQE:
  923. if (qp->ibqp.qp_type == IB_QPT_RC)
  924. goto inval;
  925. qp->state = new_state;
  926. break;
  927. case IB_QPS_ERR:
  928. lastwqe = rvt_error_qp(qp, IB_WC_WR_FLUSH_ERR);
  929. break;
  930. default:
  931. qp->state = new_state;
  932. break;
  933. }
  934. if (attr_mask & IB_QP_PKEY_INDEX)
  935. qp->s_pkey_index = attr->pkey_index;
  936. if (attr_mask & IB_QP_PORT)
  937. qp->port_num = attr->port_num;
  938. if (attr_mask & IB_QP_DEST_QPN)
  939. qp->remote_qpn = attr->dest_qp_num;
  940. if (attr_mask & IB_QP_SQ_PSN) {
  941. qp->s_next_psn = attr->sq_psn & rdi->dparms.psn_modify_mask;
  942. qp->s_psn = qp->s_next_psn;
  943. qp->s_sending_psn = qp->s_next_psn;
  944. qp->s_last_psn = qp->s_next_psn - 1;
  945. qp->s_sending_hpsn = qp->s_last_psn;
  946. }
  947. if (attr_mask & IB_QP_RQ_PSN)
  948. qp->r_psn = attr->rq_psn & rdi->dparms.psn_modify_mask;
  949. if (attr_mask & IB_QP_ACCESS_FLAGS)
  950. qp->qp_access_flags = attr->qp_access_flags;
  951. if (attr_mask & IB_QP_AV) {
  952. qp->remote_ah_attr = attr->ah_attr;
  953. qp->s_srate = attr->ah_attr.static_rate;
  954. qp->srate_mbps = ib_rate_to_mbps(qp->s_srate);
  955. }
  956. if (attr_mask & IB_QP_ALT_PATH) {
  957. qp->alt_ah_attr = attr->alt_ah_attr;
  958. qp->s_alt_pkey_index = attr->alt_pkey_index;
  959. }
  960. if (attr_mask & IB_QP_PATH_MIG_STATE) {
  961. qp->s_mig_state = attr->path_mig_state;
  962. if (mig) {
  963. qp->remote_ah_attr = qp->alt_ah_attr;
  964. qp->port_num = qp->alt_ah_attr.port_num;
  965. qp->s_pkey_index = qp->s_alt_pkey_index;
  966. /*
  967. * Ignored by drivers which do not support it. Not
  968. * really worth creating a call back into the driver
  969. * just to set a flag.
  970. */
  971. qp->s_flags |= RVT_S_AHG_CLEAR;
  972. }
  973. }
  974. if (attr_mask & IB_QP_PATH_MTU) {
  975. qp->pmtu = rdi->driver_f.mtu_from_qp(rdi, qp, pmtu);
  976. qp->path_mtu = rdi->driver_f.mtu_to_path_mtu(qp->pmtu);
  977. }
  978. if (attr_mask & IB_QP_RETRY_CNT) {
  979. qp->s_retry_cnt = attr->retry_cnt;
  980. qp->s_retry = attr->retry_cnt;
  981. }
  982. if (attr_mask & IB_QP_RNR_RETRY) {
  983. qp->s_rnr_retry_cnt = attr->rnr_retry;
  984. qp->s_rnr_retry = attr->rnr_retry;
  985. }
  986. if (attr_mask & IB_QP_MIN_RNR_TIMER)
  987. qp->r_min_rnr_timer = attr->min_rnr_timer;
  988. if (attr_mask & IB_QP_TIMEOUT) {
  989. qp->timeout = attr->timeout;
  990. qp->timeout_jiffies =
  991. usecs_to_jiffies((4096UL * (1UL << qp->timeout)) /
  992. 1000UL);
  993. }
  994. if (attr_mask & IB_QP_QKEY)
  995. qp->qkey = attr->qkey;
  996. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  997. qp->r_max_rd_atomic = attr->max_dest_rd_atomic;
  998. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  999. qp->s_max_rd_atomic = attr->max_rd_atomic;
  1000. spin_unlock(&qp->s_lock);
  1001. spin_unlock_irq(&qp->r_lock);
  1002. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1003. rvt_insert_qp(rdi, qp);
  1004. if (lastwqe) {
  1005. ev.device = qp->ibqp.device;
  1006. ev.element.qp = &qp->ibqp;
  1007. ev.event = IB_EVENT_QP_LAST_WQE_REACHED;
  1008. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1009. }
  1010. if (mig) {
  1011. ev.device = qp->ibqp.device;
  1012. ev.element.qp = &qp->ibqp;
  1013. ev.event = IB_EVENT_PATH_MIG;
  1014. qp->ibqp.event_handler(&ev, qp->ibqp.qp_context);
  1015. }
  1016. return 0;
  1017. inval:
  1018. spin_unlock(&qp->s_lock);
  1019. spin_unlock_irq(&qp->r_lock);
  1020. return -EINVAL;
  1021. }
  1022. /**
  1023. * rvt_destroy_qp - destroy a queue pair
  1024. * @ibqp: the queue pair to destroy
  1025. *
  1026. * Returns 0 on success.
  1027. *
  1028. * Note that this can be called while the QP is actively sending or
  1029. * receiving!
  1030. */
  1031. int rvt_destroy_qp(struct ib_qp *ibqp)
  1032. {
  1033. /*
  1034. * VT-DRIVER-API: qp_flush()
  1035. * Driver provies a mechanism to flush and wait for that flush to
  1036. * finish.
  1037. */
  1038. return -EOPNOTSUPP;
  1039. }
  1040. int rvt_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1041. int attr_mask, struct ib_qp_init_attr *init_attr)
  1042. {
  1043. return -EOPNOTSUPP;
  1044. }
  1045. /**
  1046. * rvt_post_receive - post a receive on a QP
  1047. * @ibqp: the QP to post the receive on
  1048. * @wr: the WR to post
  1049. * @bad_wr: the first bad WR is put here
  1050. *
  1051. * This may be called from interrupt context.
  1052. */
  1053. int rvt_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1054. struct ib_recv_wr **bad_wr)
  1055. {
  1056. /*
  1057. * When a packet arrives the driver needs to call up to rvt to process
  1058. * the packet. The UD, RC, UC processing will be done in rvt, however
  1059. * the driver should be able to override this if it so choses. Perhaps a
  1060. * set of function pointers set up at registration time.
  1061. */
  1062. return -EOPNOTSUPP;
  1063. }
  1064. /**
  1065. * rvt_post_one_wr - post one RC, UC, or UD send work request
  1066. * @qp: the QP to post on
  1067. * @wr: the work request to send
  1068. */
  1069. static int rvt_post_one_wr(struct rvt_qp *qp, struct ib_send_wr *wr)
  1070. {
  1071. struct rvt_swqe *wqe;
  1072. u32 next;
  1073. int i;
  1074. int j;
  1075. int acc;
  1076. struct rvt_lkey_table *rkt;
  1077. struct rvt_pd *pd;
  1078. struct rvt_dev_info *rdi = ib_to_rvt(qp->ibqp.device);
  1079. /* IB spec says that num_sge == 0 is OK. */
  1080. if (unlikely(wr->num_sge > qp->s_max_sge))
  1081. return -EINVAL;
  1082. /*
  1083. * Don't allow RDMA reads or atomic operations on UC or
  1084. * undefined operations.
  1085. * Make sure buffer is large enough to hold the result for atomics.
  1086. */
  1087. if (qp->ibqp.qp_type == IB_QPT_UC) {
  1088. if ((unsigned)wr->opcode >= IB_WR_RDMA_READ)
  1089. return -EINVAL;
  1090. } else if (qp->ibqp.qp_type != IB_QPT_RC) {
  1091. /* Check IB_QPT_SMI, IB_QPT_GSI, IB_QPT_UD opcode */
  1092. if (wr->opcode != IB_WR_SEND &&
  1093. wr->opcode != IB_WR_SEND_WITH_IMM)
  1094. return -EINVAL;
  1095. /* Check UD destination address PD */
  1096. if (qp->ibqp.pd != ud_wr(wr)->ah->pd)
  1097. return -EINVAL;
  1098. } else if ((unsigned)wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD) {
  1099. return -EINVAL;
  1100. } else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
  1101. (wr->num_sge == 0 ||
  1102. wr->sg_list[0].length < sizeof(u64) ||
  1103. wr->sg_list[0].addr & (sizeof(u64) - 1))) {
  1104. return -EINVAL;
  1105. } else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic) {
  1106. return -EINVAL;
  1107. }
  1108. next = qp->s_head + 1;
  1109. if (next >= qp->s_size)
  1110. next = 0;
  1111. if (next == qp->s_last)
  1112. return -ENOMEM;
  1113. rkt = &rdi->lkey_table;
  1114. pd = ibpd_to_rvtpd(qp->ibqp.pd);
  1115. wqe = rvt_get_swqe_ptr(qp, qp->s_head);
  1116. if (qp->ibqp.qp_type != IB_QPT_UC &&
  1117. qp->ibqp.qp_type != IB_QPT_RC)
  1118. memcpy(&wqe->ud_wr, ud_wr(wr), sizeof(wqe->ud_wr));
  1119. else if (wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
  1120. wr->opcode == IB_WR_RDMA_WRITE ||
  1121. wr->opcode == IB_WR_RDMA_READ)
  1122. memcpy(&wqe->rdma_wr, rdma_wr(wr), sizeof(wqe->rdma_wr));
  1123. else if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  1124. wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD)
  1125. memcpy(&wqe->atomic_wr, atomic_wr(wr), sizeof(wqe->atomic_wr));
  1126. else
  1127. memcpy(&wqe->wr, wr, sizeof(wqe->wr));
  1128. wqe->length = 0;
  1129. j = 0;
  1130. if (wr->num_sge) {
  1131. acc = wr->opcode >= IB_WR_RDMA_READ ?
  1132. IB_ACCESS_LOCAL_WRITE : 0;
  1133. for (i = 0; i < wr->num_sge; i++) {
  1134. u32 length = wr->sg_list[i].length;
  1135. int ok;
  1136. if (length == 0)
  1137. continue;
  1138. ok = rvt_lkey_ok(rkt, pd, &wqe->sg_list[j],
  1139. &wr->sg_list[i], acc);
  1140. if (!ok)
  1141. goto bail_inval_free;
  1142. wqe->length += length;
  1143. j++;
  1144. }
  1145. wqe->wr.num_sge = j;
  1146. }
  1147. if (qp->ibqp.qp_type == IB_QPT_UC ||
  1148. qp->ibqp.qp_type == IB_QPT_RC) {
  1149. if (wqe->length > 0x80000000U)
  1150. goto bail_inval_free;
  1151. } else {
  1152. atomic_inc(&ibah_to_rvtah(ud_wr(wr)->ah)->refcount);
  1153. }
  1154. wqe->ssn = qp->s_ssn++;
  1155. qp->s_head = next;
  1156. return 0;
  1157. bail_inval_free:
  1158. /* release mr holds */
  1159. while (j) {
  1160. struct rvt_sge *sge = &wqe->sg_list[--j];
  1161. rvt_put_mr(sge->mr);
  1162. }
  1163. return -EINVAL;
  1164. }
  1165. /**
  1166. * rvt_post_send - post a send on a QP
  1167. * @ibqp: the QP to post the send on
  1168. * @wr: the list of work requests to post
  1169. * @bad_wr: the first bad WR is put here
  1170. *
  1171. * This may be called from interrupt context.
  1172. */
  1173. int rvt_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1174. struct ib_send_wr **bad_wr)
  1175. {
  1176. struct rvt_qp *qp = ibqp_to_rvtqp(ibqp);
  1177. struct rvt_dev_info *rdi = ib_to_rvt(ibqp->device);
  1178. unsigned long flags = 0;
  1179. int call_send;
  1180. unsigned nreq = 0;
  1181. int err = 0;
  1182. spin_lock_irqsave(&qp->s_lock, flags);
  1183. /*
  1184. * Ensure QP state is such that we can send. If not bail out early,
  1185. * there is no need to do this every time we post a send.
  1186. */
  1187. if (unlikely(!(ib_rvt_state_ops[qp->state] & RVT_POST_SEND_OK))) {
  1188. spin_unlock_irqrestore(&qp->s_lock, flags);
  1189. return -EINVAL;
  1190. }
  1191. /*
  1192. * If the send queue is empty, and we only have a single WR then just go
  1193. * ahead and kick the send engine into gear. Otherwise we will always
  1194. * just schedule the send to happen later.
  1195. */
  1196. call_send = qp->s_head == ACCESS_ONCE(qp->s_last) && !wr->next;
  1197. for (; wr; wr = wr->next) {
  1198. err = rvt_post_one_wr(qp, wr);
  1199. if (unlikely(err)) {
  1200. *bad_wr = wr;
  1201. goto bail;
  1202. }
  1203. nreq++;
  1204. }
  1205. bail:
  1206. if (nreq && !call_send)
  1207. rdi->driver_f.schedule_send(qp);
  1208. spin_unlock_irqrestore(&qp->s_lock, flags);
  1209. if (nreq && call_send)
  1210. rdi->driver_f.do_send(qp);
  1211. return err;
  1212. }
  1213. /**
  1214. * rvt_post_srq_receive - post a receive on a shared receive queue
  1215. * @ibsrq: the SRQ to post the receive on
  1216. * @wr: the list of work requests to post
  1217. * @bad_wr: A pointer to the first WR to cause a problem is put here
  1218. *
  1219. * This may be called from interrupt context.
  1220. */
  1221. int rvt_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1222. struct ib_recv_wr **bad_wr)
  1223. {
  1224. return -EOPNOTSUPP;
  1225. }
  1226. void rvt_free_qpn(struct rvt_qpn_table *qpt, u32 qpn)
  1227. {
  1228. struct rvt_qpn_map *map;
  1229. map = qpt->map + qpn / RVT_BITS_PER_PAGE;
  1230. if (map->page)
  1231. clear_bit(qpn & RVT_BITS_PER_PAGE_MASK, map->page);
  1232. }
  1233. EXPORT_SYMBOL(rvt_free_qpn);
  1234. void rvt_dec_qp_cnt(struct rvt_dev_info *rdi)
  1235. {
  1236. spin_lock(&rdi->n_qps_lock);
  1237. rdi->n_qps_allocated--;
  1238. spin_unlock(&rdi->n_qps_lock);
  1239. }
  1240. EXPORT_SYMBOL(rvt_dec_qp_cnt);