qib_verbs.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. /*
  2. * Copyright (c) 2012 - 2018 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 <rdma/ib_mad.h>
  35. #include <rdma/ib_user_verbs.h>
  36. #include <linux/io.h>
  37. #include <linux/module.h>
  38. #include <linux/utsname.h>
  39. #include <linux/rculist.h>
  40. #include <linux/mm.h>
  41. #include <linux/random.h>
  42. #include <linux/vmalloc.h>
  43. #include <rdma/rdma_vt.h>
  44. #include "qib.h"
  45. #include "qib_common.h"
  46. static unsigned int ib_qib_qp_table_size = 256;
  47. module_param_named(qp_table_size, ib_qib_qp_table_size, uint, S_IRUGO);
  48. MODULE_PARM_DESC(qp_table_size, "QP table size");
  49. static unsigned int qib_lkey_table_size = 16;
  50. module_param_named(lkey_table_size, qib_lkey_table_size, uint,
  51. S_IRUGO);
  52. MODULE_PARM_DESC(lkey_table_size,
  53. "LKEY table size in bits (2^n, 1 <= n <= 23)");
  54. static unsigned int ib_qib_max_pds = 0xFFFF;
  55. module_param_named(max_pds, ib_qib_max_pds, uint, S_IRUGO);
  56. MODULE_PARM_DESC(max_pds,
  57. "Maximum number of protection domains to support");
  58. static unsigned int ib_qib_max_ahs = 0xFFFF;
  59. module_param_named(max_ahs, ib_qib_max_ahs, uint, S_IRUGO);
  60. MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
  61. unsigned int ib_qib_max_cqes = 0x2FFFF;
  62. module_param_named(max_cqes, ib_qib_max_cqes, uint, S_IRUGO);
  63. MODULE_PARM_DESC(max_cqes,
  64. "Maximum number of completion queue entries to support");
  65. unsigned int ib_qib_max_cqs = 0x1FFFF;
  66. module_param_named(max_cqs, ib_qib_max_cqs, uint, S_IRUGO);
  67. MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
  68. unsigned int ib_qib_max_qp_wrs = 0x3FFF;
  69. module_param_named(max_qp_wrs, ib_qib_max_qp_wrs, uint, S_IRUGO);
  70. MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
  71. unsigned int ib_qib_max_qps = 16384;
  72. module_param_named(max_qps, ib_qib_max_qps, uint, S_IRUGO);
  73. MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
  74. unsigned int ib_qib_max_sges = 0x60;
  75. module_param_named(max_sges, ib_qib_max_sges, uint, S_IRUGO);
  76. MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
  77. unsigned int ib_qib_max_mcast_grps = 16384;
  78. module_param_named(max_mcast_grps, ib_qib_max_mcast_grps, uint, S_IRUGO);
  79. MODULE_PARM_DESC(max_mcast_grps,
  80. "Maximum number of multicast groups to support");
  81. unsigned int ib_qib_max_mcast_qp_attached = 16;
  82. module_param_named(max_mcast_qp_attached, ib_qib_max_mcast_qp_attached,
  83. uint, S_IRUGO);
  84. MODULE_PARM_DESC(max_mcast_qp_attached,
  85. "Maximum number of attached QPs to support");
  86. unsigned int ib_qib_max_srqs = 1024;
  87. module_param_named(max_srqs, ib_qib_max_srqs, uint, S_IRUGO);
  88. MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
  89. unsigned int ib_qib_max_srq_sges = 128;
  90. module_param_named(max_srq_sges, ib_qib_max_srq_sges, uint, S_IRUGO);
  91. MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
  92. unsigned int ib_qib_max_srq_wrs = 0x1FFFF;
  93. module_param_named(max_srq_wrs, ib_qib_max_srq_wrs, uint, S_IRUGO);
  94. MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
  95. static unsigned int ib_qib_disable_sma;
  96. module_param_named(disable_sma, ib_qib_disable_sma, uint, S_IWUSR | S_IRUGO);
  97. MODULE_PARM_DESC(disable_sma, "Disable the SMA");
  98. /*
  99. * Translate ib_wr_opcode into ib_wc_opcode.
  100. */
  101. const enum ib_wc_opcode ib_qib_wc_opcode[] = {
  102. [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
  103. [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
  104. [IB_WR_SEND] = IB_WC_SEND,
  105. [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
  106. [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
  107. [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
  108. [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
  109. };
  110. /*
  111. * System image GUID.
  112. */
  113. __be64 ib_qib_sys_image_guid;
  114. /**
  115. * qib_copy_sge - copy data to SGE memory
  116. * @ss: the SGE state
  117. * @data: the data to copy
  118. * @length: the length of the data
  119. */
  120. void qib_copy_sge(struct rvt_sge_state *ss, void *data, u32 length, int release)
  121. {
  122. struct rvt_sge *sge = &ss->sge;
  123. while (length) {
  124. u32 len = rvt_get_sge_length(sge, length);
  125. WARN_ON_ONCE(len == 0);
  126. memcpy(sge->vaddr, data, len);
  127. rvt_update_sge(ss, len, release);
  128. data += len;
  129. length -= len;
  130. }
  131. }
  132. /*
  133. * Count the number of DMA descriptors needed to send length bytes of data.
  134. * Don't modify the qib_sge_state to get the count.
  135. * Return zero if any of the segments is not aligned.
  136. */
  137. static u32 qib_count_sge(struct rvt_sge_state *ss, u32 length)
  138. {
  139. struct rvt_sge *sg_list = ss->sg_list;
  140. struct rvt_sge sge = ss->sge;
  141. u8 num_sge = ss->num_sge;
  142. u32 ndesc = 1; /* count the header */
  143. while (length) {
  144. u32 len = sge.length;
  145. if (len > length)
  146. len = length;
  147. if (len > sge.sge_length)
  148. len = sge.sge_length;
  149. BUG_ON(len == 0);
  150. if (((long) sge.vaddr & (sizeof(u32) - 1)) ||
  151. (len != length && (len & (sizeof(u32) - 1)))) {
  152. ndesc = 0;
  153. break;
  154. }
  155. ndesc++;
  156. sge.vaddr += len;
  157. sge.length -= len;
  158. sge.sge_length -= len;
  159. if (sge.sge_length == 0) {
  160. if (--num_sge)
  161. sge = *sg_list++;
  162. } else if (sge.length == 0 && sge.mr->lkey) {
  163. if (++sge.n >= RVT_SEGSZ) {
  164. if (++sge.m >= sge.mr->mapsz)
  165. break;
  166. sge.n = 0;
  167. }
  168. sge.vaddr =
  169. sge.mr->map[sge.m]->segs[sge.n].vaddr;
  170. sge.length =
  171. sge.mr->map[sge.m]->segs[sge.n].length;
  172. }
  173. length -= len;
  174. }
  175. return ndesc;
  176. }
  177. /*
  178. * Copy from the SGEs to the data buffer.
  179. */
  180. static void qib_copy_from_sge(void *data, struct rvt_sge_state *ss, u32 length)
  181. {
  182. struct rvt_sge *sge = &ss->sge;
  183. while (length) {
  184. u32 len = sge->length;
  185. if (len > length)
  186. len = length;
  187. if (len > sge->sge_length)
  188. len = sge->sge_length;
  189. BUG_ON(len == 0);
  190. memcpy(data, sge->vaddr, len);
  191. sge->vaddr += len;
  192. sge->length -= len;
  193. sge->sge_length -= len;
  194. if (sge->sge_length == 0) {
  195. if (--ss->num_sge)
  196. *sge = *ss->sg_list++;
  197. } else if (sge->length == 0 && sge->mr->lkey) {
  198. if (++sge->n >= RVT_SEGSZ) {
  199. if (++sge->m >= sge->mr->mapsz)
  200. break;
  201. sge->n = 0;
  202. }
  203. sge->vaddr =
  204. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  205. sge->length =
  206. sge->mr->map[sge->m]->segs[sge->n].length;
  207. }
  208. data += len;
  209. length -= len;
  210. }
  211. }
  212. /**
  213. * qib_qp_rcv - processing an incoming packet on a QP
  214. * @rcd: the context pointer
  215. * @hdr: the packet header
  216. * @has_grh: true if the packet has a GRH
  217. * @data: the packet data
  218. * @tlen: the packet length
  219. * @qp: the QP the packet came on
  220. *
  221. * This is called from qib_ib_rcv() to process an incoming packet
  222. * for the given QP.
  223. * Called at interrupt level.
  224. */
  225. static void qib_qp_rcv(struct qib_ctxtdata *rcd, struct ib_header *hdr,
  226. int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
  227. {
  228. struct qib_ibport *ibp = &rcd->ppd->ibport_data;
  229. spin_lock(&qp->r_lock);
  230. /* Check for valid receive state. */
  231. if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK)) {
  232. ibp->rvp.n_pkt_drops++;
  233. goto unlock;
  234. }
  235. switch (qp->ibqp.qp_type) {
  236. case IB_QPT_SMI:
  237. case IB_QPT_GSI:
  238. if (ib_qib_disable_sma)
  239. break;
  240. /* FALLTHROUGH */
  241. case IB_QPT_UD:
  242. qib_ud_rcv(ibp, hdr, has_grh, data, tlen, qp);
  243. break;
  244. case IB_QPT_RC:
  245. qib_rc_rcv(rcd, hdr, has_grh, data, tlen, qp);
  246. break;
  247. case IB_QPT_UC:
  248. qib_uc_rcv(ibp, hdr, has_grh, data, tlen, qp);
  249. break;
  250. default:
  251. break;
  252. }
  253. unlock:
  254. spin_unlock(&qp->r_lock);
  255. }
  256. /**
  257. * qib_ib_rcv - process an incoming packet
  258. * @rcd: the context pointer
  259. * @rhdr: the header of the packet
  260. * @data: the packet payload
  261. * @tlen: the packet length
  262. *
  263. * This is called from qib_kreceive() to process an incoming packet at
  264. * interrupt level. Tlen is the length of the header + data + CRC in bytes.
  265. */
  266. void qib_ib_rcv(struct qib_ctxtdata *rcd, void *rhdr, void *data, u32 tlen)
  267. {
  268. struct qib_pportdata *ppd = rcd->ppd;
  269. struct qib_ibport *ibp = &ppd->ibport_data;
  270. struct ib_header *hdr = rhdr;
  271. struct qib_devdata *dd = ppd->dd;
  272. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  273. struct ib_other_headers *ohdr;
  274. struct rvt_qp *qp;
  275. u32 qp_num;
  276. int lnh;
  277. u8 opcode;
  278. u16 lid;
  279. /* 24 == LRH+BTH+CRC */
  280. if (unlikely(tlen < 24))
  281. goto drop;
  282. /* Check for a valid destination LID (see ch. 7.11.1). */
  283. lid = be16_to_cpu(hdr->lrh[1]);
  284. if (lid < be16_to_cpu(IB_MULTICAST_LID_BASE)) {
  285. lid &= ~((1 << ppd->lmc) - 1);
  286. if (unlikely(lid != ppd->lid))
  287. goto drop;
  288. }
  289. /* Check for GRH */
  290. lnh = be16_to_cpu(hdr->lrh[0]) & 3;
  291. if (lnh == QIB_LRH_BTH)
  292. ohdr = &hdr->u.oth;
  293. else if (lnh == QIB_LRH_GRH) {
  294. u32 vtf;
  295. ohdr = &hdr->u.l.oth;
  296. if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
  297. goto drop;
  298. vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
  299. if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
  300. goto drop;
  301. } else
  302. goto drop;
  303. opcode = (be32_to_cpu(ohdr->bth[0]) >> 24) & 0x7f;
  304. #ifdef CONFIG_DEBUG_FS
  305. rcd->opstats->stats[opcode].n_bytes += tlen;
  306. rcd->opstats->stats[opcode].n_packets++;
  307. #endif
  308. /* Get the destination QP number. */
  309. qp_num = be32_to_cpu(ohdr->bth[1]) & RVT_QPN_MASK;
  310. if (qp_num == QIB_MULTICAST_QPN) {
  311. struct rvt_mcast *mcast;
  312. struct rvt_mcast_qp *p;
  313. if (lnh != QIB_LRH_GRH)
  314. goto drop;
  315. mcast = rvt_mcast_find(&ibp->rvp, &hdr->u.l.grh.dgid, lid);
  316. if (mcast == NULL)
  317. goto drop;
  318. this_cpu_inc(ibp->pmastats->n_multicast_rcv);
  319. rcu_read_lock();
  320. list_for_each_entry_rcu(p, &mcast->qp_list, list)
  321. qib_qp_rcv(rcd, hdr, 1, data, tlen, p->qp);
  322. rcu_read_unlock();
  323. /*
  324. * Notify rvt_multicast_detach() if it is waiting for us
  325. * to finish.
  326. */
  327. if (atomic_dec_return(&mcast->refcount) <= 1)
  328. wake_up(&mcast->wait);
  329. } else {
  330. rcu_read_lock();
  331. qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
  332. if (!qp) {
  333. rcu_read_unlock();
  334. goto drop;
  335. }
  336. this_cpu_inc(ibp->pmastats->n_unicast_rcv);
  337. qib_qp_rcv(rcd, hdr, lnh == QIB_LRH_GRH, data, tlen, qp);
  338. rcu_read_unlock();
  339. }
  340. return;
  341. drop:
  342. ibp->rvp.n_pkt_drops++;
  343. }
  344. /*
  345. * This is called from a timer to check for QPs
  346. * which need kernel memory in order to send a packet.
  347. */
  348. static void mem_timer(struct timer_list *t)
  349. {
  350. struct qib_ibdev *dev = from_timer(dev, t, mem_timer);
  351. struct list_head *list = &dev->memwait;
  352. struct rvt_qp *qp = NULL;
  353. struct qib_qp_priv *priv = NULL;
  354. unsigned long flags;
  355. spin_lock_irqsave(&dev->rdi.pending_lock, flags);
  356. if (!list_empty(list)) {
  357. priv = list_entry(list->next, struct qib_qp_priv, iowait);
  358. qp = priv->owner;
  359. list_del_init(&priv->iowait);
  360. rvt_get_qp(qp);
  361. if (!list_empty(list))
  362. mod_timer(&dev->mem_timer, jiffies + 1);
  363. }
  364. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  365. if (qp) {
  366. spin_lock_irqsave(&qp->s_lock, flags);
  367. if (qp->s_flags & RVT_S_WAIT_KMEM) {
  368. qp->s_flags &= ~RVT_S_WAIT_KMEM;
  369. qib_schedule_send(qp);
  370. }
  371. spin_unlock_irqrestore(&qp->s_lock, flags);
  372. rvt_put_qp(qp);
  373. }
  374. }
  375. #ifdef __LITTLE_ENDIAN
  376. static inline u32 get_upper_bits(u32 data, u32 shift)
  377. {
  378. return data >> shift;
  379. }
  380. static inline u32 set_upper_bits(u32 data, u32 shift)
  381. {
  382. return data << shift;
  383. }
  384. static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
  385. {
  386. data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
  387. data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
  388. return data;
  389. }
  390. #else
  391. static inline u32 get_upper_bits(u32 data, u32 shift)
  392. {
  393. return data << shift;
  394. }
  395. static inline u32 set_upper_bits(u32 data, u32 shift)
  396. {
  397. return data >> shift;
  398. }
  399. static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
  400. {
  401. data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
  402. data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
  403. return data;
  404. }
  405. #endif
  406. static void copy_io(u32 __iomem *piobuf, struct rvt_sge_state *ss,
  407. u32 length, unsigned flush_wc)
  408. {
  409. u32 extra = 0;
  410. u32 data = 0;
  411. u32 last;
  412. while (1) {
  413. u32 len = ss->sge.length;
  414. u32 off;
  415. if (len > length)
  416. len = length;
  417. if (len > ss->sge.sge_length)
  418. len = ss->sge.sge_length;
  419. BUG_ON(len == 0);
  420. /* If the source address is not aligned, try to align it. */
  421. off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
  422. if (off) {
  423. u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
  424. ~(sizeof(u32) - 1));
  425. u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
  426. u32 y;
  427. y = sizeof(u32) - off;
  428. if (len > y)
  429. len = y;
  430. if (len + extra >= sizeof(u32)) {
  431. data |= set_upper_bits(v, extra *
  432. BITS_PER_BYTE);
  433. len = sizeof(u32) - extra;
  434. if (len == length) {
  435. last = data;
  436. break;
  437. }
  438. __raw_writel(data, piobuf);
  439. piobuf++;
  440. extra = 0;
  441. data = 0;
  442. } else {
  443. /* Clear unused upper bytes */
  444. data |= clear_upper_bytes(v, len, extra);
  445. if (len == length) {
  446. last = data;
  447. break;
  448. }
  449. extra += len;
  450. }
  451. } else if (extra) {
  452. /* Source address is aligned. */
  453. u32 *addr = (u32 *) ss->sge.vaddr;
  454. int shift = extra * BITS_PER_BYTE;
  455. int ushift = 32 - shift;
  456. u32 l = len;
  457. while (l >= sizeof(u32)) {
  458. u32 v = *addr;
  459. data |= set_upper_bits(v, shift);
  460. __raw_writel(data, piobuf);
  461. data = get_upper_bits(v, ushift);
  462. piobuf++;
  463. addr++;
  464. l -= sizeof(u32);
  465. }
  466. /*
  467. * We still have 'extra' number of bytes leftover.
  468. */
  469. if (l) {
  470. u32 v = *addr;
  471. if (l + extra >= sizeof(u32)) {
  472. data |= set_upper_bits(v, shift);
  473. len -= l + extra - sizeof(u32);
  474. if (len == length) {
  475. last = data;
  476. break;
  477. }
  478. __raw_writel(data, piobuf);
  479. piobuf++;
  480. extra = 0;
  481. data = 0;
  482. } else {
  483. /* Clear unused upper bytes */
  484. data |= clear_upper_bytes(v, l, extra);
  485. if (len == length) {
  486. last = data;
  487. break;
  488. }
  489. extra += l;
  490. }
  491. } else if (len == length) {
  492. last = data;
  493. break;
  494. }
  495. } else if (len == length) {
  496. u32 w;
  497. /*
  498. * Need to round up for the last dword in the
  499. * packet.
  500. */
  501. w = (len + 3) >> 2;
  502. qib_pio_copy(piobuf, ss->sge.vaddr, w - 1);
  503. piobuf += w - 1;
  504. last = ((u32 *) ss->sge.vaddr)[w - 1];
  505. break;
  506. } else {
  507. u32 w = len >> 2;
  508. qib_pio_copy(piobuf, ss->sge.vaddr, w);
  509. piobuf += w;
  510. extra = len & (sizeof(u32) - 1);
  511. if (extra) {
  512. u32 v = ((u32 *) ss->sge.vaddr)[w];
  513. /* Clear unused upper bytes */
  514. data = clear_upper_bytes(v, extra, 0);
  515. }
  516. }
  517. rvt_update_sge(ss, len, false);
  518. length -= len;
  519. }
  520. /* Update address before sending packet. */
  521. rvt_update_sge(ss, length, false);
  522. if (flush_wc) {
  523. /* must flush early everything before trigger word */
  524. qib_flush_wc();
  525. __raw_writel(last, piobuf);
  526. /* be sure trigger word is written */
  527. qib_flush_wc();
  528. } else
  529. __raw_writel(last, piobuf);
  530. }
  531. static noinline struct qib_verbs_txreq *__get_txreq(struct qib_ibdev *dev,
  532. struct rvt_qp *qp)
  533. {
  534. struct qib_qp_priv *priv = qp->priv;
  535. struct qib_verbs_txreq *tx;
  536. unsigned long flags;
  537. spin_lock_irqsave(&qp->s_lock, flags);
  538. spin_lock(&dev->rdi.pending_lock);
  539. if (!list_empty(&dev->txreq_free)) {
  540. struct list_head *l = dev->txreq_free.next;
  541. list_del(l);
  542. spin_unlock(&dev->rdi.pending_lock);
  543. spin_unlock_irqrestore(&qp->s_lock, flags);
  544. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  545. } else {
  546. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK &&
  547. list_empty(&priv->iowait)) {
  548. dev->n_txwait++;
  549. qp->s_flags |= RVT_S_WAIT_TX;
  550. list_add_tail(&priv->iowait, &dev->txwait);
  551. }
  552. qp->s_flags &= ~RVT_S_BUSY;
  553. spin_unlock(&dev->rdi.pending_lock);
  554. spin_unlock_irqrestore(&qp->s_lock, flags);
  555. tx = ERR_PTR(-EBUSY);
  556. }
  557. return tx;
  558. }
  559. static inline struct qib_verbs_txreq *get_txreq(struct qib_ibdev *dev,
  560. struct rvt_qp *qp)
  561. {
  562. struct qib_verbs_txreq *tx;
  563. unsigned long flags;
  564. spin_lock_irqsave(&dev->rdi.pending_lock, flags);
  565. /* assume the list non empty */
  566. if (likely(!list_empty(&dev->txreq_free))) {
  567. struct list_head *l = dev->txreq_free.next;
  568. list_del(l);
  569. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  570. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  571. } else {
  572. /* call slow path to get the extra lock */
  573. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  574. tx = __get_txreq(dev, qp);
  575. }
  576. return tx;
  577. }
  578. void qib_put_txreq(struct qib_verbs_txreq *tx)
  579. {
  580. struct qib_ibdev *dev;
  581. struct rvt_qp *qp;
  582. struct qib_qp_priv *priv;
  583. unsigned long flags;
  584. qp = tx->qp;
  585. dev = to_idev(qp->ibqp.device);
  586. if (tx->mr) {
  587. rvt_put_mr(tx->mr);
  588. tx->mr = NULL;
  589. }
  590. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF) {
  591. tx->txreq.flags &= ~QIB_SDMA_TXREQ_F_FREEBUF;
  592. dma_unmap_single(&dd_from_dev(dev)->pcidev->dev,
  593. tx->txreq.addr, tx->hdr_dwords << 2,
  594. DMA_TO_DEVICE);
  595. kfree(tx->align_buf);
  596. }
  597. spin_lock_irqsave(&dev->rdi.pending_lock, flags);
  598. /* Put struct back on free list */
  599. list_add(&tx->txreq.list, &dev->txreq_free);
  600. if (!list_empty(&dev->txwait)) {
  601. /* Wake up first QP wanting a free struct */
  602. priv = list_entry(dev->txwait.next, struct qib_qp_priv,
  603. iowait);
  604. qp = priv->owner;
  605. list_del_init(&priv->iowait);
  606. rvt_get_qp(qp);
  607. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  608. spin_lock_irqsave(&qp->s_lock, flags);
  609. if (qp->s_flags & RVT_S_WAIT_TX) {
  610. qp->s_flags &= ~RVT_S_WAIT_TX;
  611. qib_schedule_send(qp);
  612. }
  613. spin_unlock_irqrestore(&qp->s_lock, flags);
  614. rvt_put_qp(qp);
  615. } else
  616. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  617. }
  618. /*
  619. * This is called when there are send DMA descriptors that might be
  620. * available.
  621. *
  622. * This is called with ppd->sdma_lock held.
  623. */
  624. void qib_verbs_sdma_desc_avail(struct qib_pportdata *ppd, unsigned avail)
  625. {
  626. struct rvt_qp *qp;
  627. struct qib_qp_priv *qpp, *nqpp;
  628. struct rvt_qp *qps[20];
  629. struct qib_ibdev *dev;
  630. unsigned i, n;
  631. n = 0;
  632. dev = &ppd->dd->verbs_dev;
  633. spin_lock(&dev->rdi.pending_lock);
  634. /* Search wait list for first QP wanting DMA descriptors. */
  635. list_for_each_entry_safe(qpp, nqpp, &dev->dmawait, iowait) {
  636. qp = qpp->owner;
  637. if (qp->port_num != ppd->port)
  638. continue;
  639. if (n == ARRAY_SIZE(qps))
  640. break;
  641. if (qpp->s_tx->txreq.sg_count > avail)
  642. break;
  643. avail -= qpp->s_tx->txreq.sg_count;
  644. list_del_init(&qpp->iowait);
  645. rvt_get_qp(qp);
  646. qps[n++] = qp;
  647. }
  648. spin_unlock(&dev->rdi.pending_lock);
  649. for (i = 0; i < n; i++) {
  650. qp = qps[i];
  651. spin_lock(&qp->s_lock);
  652. if (qp->s_flags & RVT_S_WAIT_DMA_DESC) {
  653. qp->s_flags &= ~RVT_S_WAIT_DMA_DESC;
  654. qib_schedule_send(qp);
  655. }
  656. spin_unlock(&qp->s_lock);
  657. rvt_put_qp(qp);
  658. }
  659. }
  660. /*
  661. * This is called with ppd->sdma_lock held.
  662. */
  663. static void sdma_complete(struct qib_sdma_txreq *cookie, int status)
  664. {
  665. struct qib_verbs_txreq *tx =
  666. container_of(cookie, struct qib_verbs_txreq, txreq);
  667. struct rvt_qp *qp = tx->qp;
  668. struct qib_qp_priv *priv = qp->priv;
  669. spin_lock(&qp->s_lock);
  670. if (tx->wqe)
  671. qib_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
  672. else if (qp->ibqp.qp_type == IB_QPT_RC) {
  673. struct ib_header *hdr;
  674. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF)
  675. hdr = &tx->align_buf->hdr;
  676. else {
  677. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  678. hdr = &dev->pio_hdrs[tx->hdr_inx].hdr;
  679. }
  680. qib_rc_send_complete(qp, hdr);
  681. }
  682. if (atomic_dec_and_test(&priv->s_dma_busy)) {
  683. if (qp->state == IB_QPS_RESET)
  684. wake_up(&priv->wait_dma);
  685. else if (qp->s_flags & RVT_S_WAIT_DMA) {
  686. qp->s_flags &= ~RVT_S_WAIT_DMA;
  687. qib_schedule_send(qp);
  688. }
  689. }
  690. spin_unlock(&qp->s_lock);
  691. qib_put_txreq(tx);
  692. }
  693. static int wait_kmem(struct qib_ibdev *dev, struct rvt_qp *qp)
  694. {
  695. struct qib_qp_priv *priv = qp->priv;
  696. unsigned long flags;
  697. int ret = 0;
  698. spin_lock_irqsave(&qp->s_lock, flags);
  699. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  700. spin_lock(&dev->rdi.pending_lock);
  701. if (list_empty(&priv->iowait)) {
  702. if (list_empty(&dev->memwait))
  703. mod_timer(&dev->mem_timer, jiffies + 1);
  704. qp->s_flags |= RVT_S_WAIT_KMEM;
  705. list_add_tail(&priv->iowait, &dev->memwait);
  706. }
  707. spin_unlock(&dev->rdi.pending_lock);
  708. qp->s_flags &= ~RVT_S_BUSY;
  709. ret = -EBUSY;
  710. }
  711. spin_unlock_irqrestore(&qp->s_lock, flags);
  712. return ret;
  713. }
  714. static int qib_verbs_send_dma(struct rvt_qp *qp, struct ib_header *hdr,
  715. u32 hdrwords, struct rvt_sge_state *ss, u32 len,
  716. u32 plen, u32 dwords)
  717. {
  718. struct qib_qp_priv *priv = qp->priv;
  719. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  720. struct qib_devdata *dd = dd_from_dev(dev);
  721. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  722. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  723. struct qib_verbs_txreq *tx;
  724. struct qib_pio_header *phdr;
  725. u32 control;
  726. u32 ndesc;
  727. int ret;
  728. tx = priv->s_tx;
  729. if (tx) {
  730. priv->s_tx = NULL;
  731. /* resend previously constructed packet */
  732. ret = qib_sdma_verbs_send(ppd, tx->ss, tx->dwords, tx);
  733. goto bail;
  734. }
  735. tx = get_txreq(dev, qp);
  736. if (IS_ERR(tx))
  737. goto bail_tx;
  738. control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
  739. be16_to_cpu(hdr->lrh[0]) >> 12);
  740. tx->qp = qp;
  741. tx->wqe = qp->s_wqe;
  742. tx->mr = qp->s_rdma_mr;
  743. if (qp->s_rdma_mr)
  744. qp->s_rdma_mr = NULL;
  745. tx->txreq.callback = sdma_complete;
  746. if (dd->flags & QIB_HAS_SDMA_TIMEOUT)
  747. tx->txreq.flags = QIB_SDMA_TXREQ_F_HEADTOHOST;
  748. else
  749. tx->txreq.flags = QIB_SDMA_TXREQ_F_INTREQ;
  750. if (plen + 1 > dd->piosize2kmax_dwords)
  751. tx->txreq.flags |= QIB_SDMA_TXREQ_F_USELARGEBUF;
  752. if (len) {
  753. /*
  754. * Don't try to DMA if it takes more descriptors than
  755. * the queue holds.
  756. */
  757. ndesc = qib_count_sge(ss, len);
  758. if (ndesc >= ppd->sdma_descq_cnt)
  759. ndesc = 0;
  760. } else
  761. ndesc = 1;
  762. if (ndesc) {
  763. phdr = &dev->pio_hdrs[tx->hdr_inx];
  764. phdr->pbc[0] = cpu_to_le32(plen);
  765. phdr->pbc[1] = cpu_to_le32(control);
  766. memcpy(&phdr->hdr, hdr, hdrwords << 2);
  767. tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEDESC;
  768. tx->txreq.sg_count = ndesc;
  769. tx->txreq.addr = dev->pio_hdrs_phys +
  770. tx->hdr_inx * sizeof(struct qib_pio_header);
  771. tx->hdr_dwords = hdrwords + 2; /* add PBC length */
  772. ret = qib_sdma_verbs_send(ppd, ss, dwords, tx);
  773. goto bail;
  774. }
  775. /* Allocate a buffer and copy the header and payload to it. */
  776. tx->hdr_dwords = plen + 1;
  777. phdr = kmalloc(tx->hdr_dwords << 2, GFP_ATOMIC);
  778. if (!phdr)
  779. goto err_tx;
  780. phdr->pbc[0] = cpu_to_le32(plen);
  781. phdr->pbc[1] = cpu_to_le32(control);
  782. memcpy(&phdr->hdr, hdr, hdrwords << 2);
  783. qib_copy_from_sge((u32 *) &phdr->hdr + hdrwords, ss, len);
  784. tx->txreq.addr = dma_map_single(&dd->pcidev->dev, phdr,
  785. tx->hdr_dwords << 2, DMA_TO_DEVICE);
  786. if (dma_mapping_error(&dd->pcidev->dev, tx->txreq.addr))
  787. goto map_err;
  788. tx->align_buf = phdr;
  789. tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEBUF;
  790. tx->txreq.sg_count = 1;
  791. ret = qib_sdma_verbs_send(ppd, NULL, 0, tx);
  792. goto unaligned;
  793. map_err:
  794. kfree(phdr);
  795. err_tx:
  796. qib_put_txreq(tx);
  797. ret = wait_kmem(dev, qp);
  798. unaligned:
  799. ibp->rvp.n_unaligned++;
  800. bail:
  801. return ret;
  802. bail_tx:
  803. ret = PTR_ERR(tx);
  804. goto bail;
  805. }
  806. /*
  807. * If we are now in the error state, return zero to flush the
  808. * send work request.
  809. */
  810. static int no_bufs_available(struct rvt_qp *qp)
  811. {
  812. struct qib_qp_priv *priv = qp->priv;
  813. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  814. struct qib_devdata *dd;
  815. unsigned long flags;
  816. int ret = 0;
  817. /*
  818. * Note that as soon as want_buffer() is called and
  819. * possibly before it returns, qib_ib_piobufavail()
  820. * could be called. Therefore, put QP on the I/O wait list before
  821. * enabling the PIO avail interrupt.
  822. */
  823. spin_lock_irqsave(&qp->s_lock, flags);
  824. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  825. spin_lock(&dev->rdi.pending_lock);
  826. if (list_empty(&priv->iowait)) {
  827. dev->n_piowait++;
  828. qp->s_flags |= RVT_S_WAIT_PIO;
  829. list_add_tail(&priv->iowait, &dev->piowait);
  830. dd = dd_from_dev(dev);
  831. dd->f_wantpiobuf_intr(dd, 1);
  832. }
  833. spin_unlock(&dev->rdi.pending_lock);
  834. qp->s_flags &= ~RVT_S_BUSY;
  835. ret = -EBUSY;
  836. }
  837. spin_unlock_irqrestore(&qp->s_lock, flags);
  838. return ret;
  839. }
  840. static int qib_verbs_send_pio(struct rvt_qp *qp, struct ib_header *ibhdr,
  841. u32 hdrwords, struct rvt_sge_state *ss, u32 len,
  842. u32 plen, u32 dwords)
  843. {
  844. struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  845. struct qib_pportdata *ppd = dd->pport + qp->port_num - 1;
  846. u32 *hdr = (u32 *) ibhdr;
  847. u32 __iomem *piobuf_orig;
  848. u32 __iomem *piobuf;
  849. u64 pbc;
  850. unsigned long flags;
  851. unsigned flush_wc;
  852. u32 control;
  853. u32 pbufn;
  854. control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
  855. be16_to_cpu(ibhdr->lrh[0]) >> 12);
  856. pbc = ((u64) control << 32) | plen;
  857. piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
  858. if (unlikely(piobuf == NULL))
  859. return no_bufs_available(qp);
  860. /*
  861. * Write the pbc.
  862. * We have to flush after the PBC for correctness on some cpus
  863. * or WC buffer can be written out of order.
  864. */
  865. writeq(pbc, piobuf);
  866. piobuf_orig = piobuf;
  867. piobuf += 2;
  868. flush_wc = dd->flags & QIB_PIO_FLUSH_WC;
  869. if (len == 0) {
  870. /*
  871. * If there is just the header portion, must flush before
  872. * writing last word of header for correctness, and after
  873. * the last header word (trigger word).
  874. */
  875. if (flush_wc) {
  876. qib_flush_wc();
  877. qib_pio_copy(piobuf, hdr, hdrwords - 1);
  878. qib_flush_wc();
  879. __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
  880. qib_flush_wc();
  881. } else
  882. qib_pio_copy(piobuf, hdr, hdrwords);
  883. goto done;
  884. }
  885. if (flush_wc)
  886. qib_flush_wc();
  887. qib_pio_copy(piobuf, hdr, hdrwords);
  888. piobuf += hdrwords;
  889. /* The common case is aligned and contained in one segment. */
  890. if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
  891. !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
  892. u32 *addr = (u32 *) ss->sge.vaddr;
  893. /* Update address before sending packet. */
  894. rvt_update_sge(ss, len, false);
  895. if (flush_wc) {
  896. qib_pio_copy(piobuf, addr, dwords - 1);
  897. /* must flush early everything before trigger word */
  898. qib_flush_wc();
  899. __raw_writel(addr[dwords - 1], piobuf + dwords - 1);
  900. /* be sure trigger word is written */
  901. qib_flush_wc();
  902. } else
  903. qib_pio_copy(piobuf, addr, dwords);
  904. goto done;
  905. }
  906. copy_io(piobuf, ss, len, flush_wc);
  907. done:
  908. if (dd->flags & QIB_USE_SPCL_TRIG) {
  909. u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
  910. qib_flush_wc();
  911. __raw_writel(0xaebecede, piobuf_orig + spcl_off);
  912. }
  913. qib_sendbuf_done(dd, pbufn);
  914. if (qp->s_rdma_mr) {
  915. rvt_put_mr(qp->s_rdma_mr);
  916. qp->s_rdma_mr = NULL;
  917. }
  918. if (qp->s_wqe) {
  919. spin_lock_irqsave(&qp->s_lock, flags);
  920. qib_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
  921. spin_unlock_irqrestore(&qp->s_lock, flags);
  922. } else if (qp->ibqp.qp_type == IB_QPT_RC) {
  923. spin_lock_irqsave(&qp->s_lock, flags);
  924. qib_rc_send_complete(qp, ibhdr);
  925. spin_unlock_irqrestore(&qp->s_lock, flags);
  926. }
  927. return 0;
  928. }
  929. /**
  930. * qib_verbs_send - send a packet
  931. * @qp: the QP to send on
  932. * @hdr: the packet header
  933. * @hdrwords: the number of 32-bit words in the header
  934. * @ss: the SGE to send
  935. * @len: the length of the packet in bytes
  936. *
  937. * Return zero if packet is sent or queued OK.
  938. * Return non-zero and clear qp->s_flags RVT_S_BUSY otherwise.
  939. */
  940. int qib_verbs_send(struct rvt_qp *qp, struct ib_header *hdr,
  941. u32 hdrwords, struct rvt_sge_state *ss, u32 len)
  942. {
  943. struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  944. u32 plen;
  945. int ret;
  946. u32 dwords = (len + 3) >> 2;
  947. /*
  948. * Calculate the send buffer trigger address.
  949. * The +1 counts for the pbc control dword following the pbc length.
  950. */
  951. plen = hdrwords + dwords + 1;
  952. /*
  953. * VL15 packets (IB_QPT_SMI) will always use PIO, so we
  954. * can defer SDMA restart until link goes ACTIVE without
  955. * worrying about just how we got there.
  956. */
  957. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  958. !(dd->flags & QIB_HAS_SEND_DMA))
  959. ret = qib_verbs_send_pio(qp, hdr, hdrwords, ss, len,
  960. plen, dwords);
  961. else
  962. ret = qib_verbs_send_dma(qp, hdr, hdrwords, ss, len,
  963. plen, dwords);
  964. return ret;
  965. }
  966. int qib_snapshot_counters(struct qib_pportdata *ppd, u64 *swords,
  967. u64 *rwords, u64 *spkts, u64 *rpkts,
  968. u64 *xmit_wait)
  969. {
  970. int ret;
  971. struct qib_devdata *dd = ppd->dd;
  972. if (!(dd->flags & QIB_PRESENT)) {
  973. /* no hardware, freeze, etc. */
  974. ret = -EINVAL;
  975. goto bail;
  976. }
  977. *swords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDSEND);
  978. *rwords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDRCV);
  979. *spkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTSEND);
  980. *rpkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTRCV);
  981. *xmit_wait = dd->f_portcntr(ppd, QIBPORTCNTR_SENDSTALL);
  982. ret = 0;
  983. bail:
  984. return ret;
  985. }
  986. /**
  987. * qib_get_counters - get various chip counters
  988. * @dd: the qlogic_ib device
  989. * @cntrs: counters are placed here
  990. *
  991. * Return the counters needed by recv_pma_get_portcounters().
  992. */
  993. int qib_get_counters(struct qib_pportdata *ppd,
  994. struct qib_verbs_counters *cntrs)
  995. {
  996. int ret;
  997. if (!(ppd->dd->flags & QIB_PRESENT)) {
  998. /* no hardware, freeze, etc. */
  999. ret = -EINVAL;
  1000. goto bail;
  1001. }
  1002. cntrs->symbol_error_counter =
  1003. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBSYMBOLERR);
  1004. cntrs->link_error_recovery_counter =
  1005. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKERRRECOV);
  1006. /*
  1007. * The link downed counter counts when the other side downs the
  1008. * connection. We add in the number of times we downed the link
  1009. * due to local link integrity errors to compensate.
  1010. */
  1011. cntrs->link_downed_counter =
  1012. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKDOWN);
  1013. cntrs->port_rcv_errors =
  1014. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXDROPPKT) +
  1015. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVOVFL) +
  1016. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERR_RLEN) +
  1017. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_INVALIDRLEN) +
  1018. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLINK) +
  1019. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRICRC) +
  1020. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRVCRC) +
  1021. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLPCRC) +
  1022. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_BADFORMAT);
  1023. cntrs->port_rcv_errors +=
  1024. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXLOCALPHYERR);
  1025. cntrs->port_rcv_errors +=
  1026. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXVLERR);
  1027. cntrs->port_rcv_remphys_errors =
  1028. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVEBP);
  1029. cntrs->port_xmit_discards =
  1030. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_UNSUPVL);
  1031. cntrs->port_xmit_data = ppd->dd->f_portcntr(ppd,
  1032. QIBPORTCNTR_WORDSEND);
  1033. cntrs->port_rcv_data = ppd->dd->f_portcntr(ppd,
  1034. QIBPORTCNTR_WORDRCV);
  1035. cntrs->port_xmit_packets = ppd->dd->f_portcntr(ppd,
  1036. QIBPORTCNTR_PKTSEND);
  1037. cntrs->port_rcv_packets = ppd->dd->f_portcntr(ppd,
  1038. QIBPORTCNTR_PKTRCV);
  1039. cntrs->local_link_integrity_errors =
  1040. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_LLI);
  1041. cntrs->excessive_buffer_overrun_errors =
  1042. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_EXCESSBUFOVFL);
  1043. cntrs->vl15_dropped =
  1044. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_VL15PKTDROP);
  1045. ret = 0;
  1046. bail:
  1047. return ret;
  1048. }
  1049. /**
  1050. * qib_ib_piobufavail - callback when a PIO buffer is available
  1051. * @dd: the device pointer
  1052. *
  1053. * This is called from qib_intr() at interrupt level when a PIO buffer is
  1054. * available after qib_verbs_send() returned an error that no buffers were
  1055. * available. Disable the interrupt if there are no more QPs waiting.
  1056. */
  1057. void qib_ib_piobufavail(struct qib_devdata *dd)
  1058. {
  1059. struct qib_ibdev *dev = &dd->verbs_dev;
  1060. struct list_head *list;
  1061. struct rvt_qp *qps[5];
  1062. struct rvt_qp *qp;
  1063. unsigned long flags;
  1064. unsigned i, n;
  1065. struct qib_qp_priv *priv;
  1066. list = &dev->piowait;
  1067. n = 0;
  1068. /*
  1069. * Note: checking that the piowait list is empty and clearing
  1070. * the buffer available interrupt needs to be atomic or we
  1071. * could end up with QPs on the wait list with the interrupt
  1072. * disabled.
  1073. */
  1074. spin_lock_irqsave(&dev->rdi.pending_lock, flags);
  1075. while (!list_empty(list)) {
  1076. if (n == ARRAY_SIZE(qps))
  1077. goto full;
  1078. priv = list_entry(list->next, struct qib_qp_priv, iowait);
  1079. qp = priv->owner;
  1080. list_del_init(&priv->iowait);
  1081. rvt_get_qp(qp);
  1082. qps[n++] = qp;
  1083. }
  1084. dd->f_wantpiobuf_intr(dd, 0);
  1085. full:
  1086. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  1087. for (i = 0; i < n; i++) {
  1088. qp = qps[i];
  1089. spin_lock_irqsave(&qp->s_lock, flags);
  1090. if (qp->s_flags & RVT_S_WAIT_PIO) {
  1091. qp->s_flags &= ~RVT_S_WAIT_PIO;
  1092. qib_schedule_send(qp);
  1093. }
  1094. spin_unlock_irqrestore(&qp->s_lock, flags);
  1095. /* Notify qib_destroy_qp() if it is waiting. */
  1096. rvt_put_qp(qp);
  1097. }
  1098. }
  1099. static int qib_query_port(struct rvt_dev_info *rdi, u8 port_num,
  1100. struct ib_port_attr *props)
  1101. {
  1102. struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
  1103. struct qib_devdata *dd = dd_from_dev(ibdev);
  1104. struct qib_pportdata *ppd = &dd->pport[port_num - 1];
  1105. enum ib_mtu mtu;
  1106. u16 lid = ppd->lid;
  1107. /* props being zeroed by the caller, avoid zeroing it here */
  1108. props->lid = lid ? lid : be16_to_cpu(IB_LID_PERMISSIVE);
  1109. props->lmc = ppd->lmc;
  1110. props->state = dd->f_iblink_state(ppd->lastibcstat);
  1111. props->phys_state = dd->f_ibphys_portstate(ppd->lastibcstat);
  1112. props->gid_tbl_len = QIB_GUIDS_PER_PORT;
  1113. props->active_width = ppd->link_width_active;
  1114. /* See rate_show() */
  1115. props->active_speed = ppd->link_speed_active;
  1116. props->max_vl_num = qib_num_vls(ppd->vls_supported);
  1117. props->max_mtu = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
  1118. switch (ppd->ibmtu) {
  1119. case 4096:
  1120. mtu = IB_MTU_4096;
  1121. break;
  1122. case 2048:
  1123. mtu = IB_MTU_2048;
  1124. break;
  1125. case 1024:
  1126. mtu = IB_MTU_1024;
  1127. break;
  1128. case 512:
  1129. mtu = IB_MTU_512;
  1130. break;
  1131. case 256:
  1132. mtu = IB_MTU_256;
  1133. break;
  1134. default:
  1135. mtu = IB_MTU_2048;
  1136. }
  1137. props->active_mtu = mtu;
  1138. return 0;
  1139. }
  1140. static int qib_modify_device(struct ib_device *device,
  1141. int device_modify_mask,
  1142. struct ib_device_modify *device_modify)
  1143. {
  1144. struct qib_devdata *dd = dd_from_ibdev(device);
  1145. unsigned i;
  1146. int ret;
  1147. if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
  1148. IB_DEVICE_MODIFY_NODE_DESC)) {
  1149. ret = -EOPNOTSUPP;
  1150. goto bail;
  1151. }
  1152. if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
  1153. memcpy(device->node_desc, device_modify->node_desc,
  1154. IB_DEVICE_NODE_DESC_MAX);
  1155. for (i = 0; i < dd->num_pports; i++) {
  1156. struct qib_ibport *ibp = &dd->pport[i].ibport_data;
  1157. qib_node_desc_chg(ibp);
  1158. }
  1159. }
  1160. if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
  1161. ib_qib_sys_image_guid =
  1162. cpu_to_be64(device_modify->sys_image_guid);
  1163. for (i = 0; i < dd->num_pports; i++) {
  1164. struct qib_ibport *ibp = &dd->pport[i].ibport_data;
  1165. qib_sys_guid_chg(ibp);
  1166. }
  1167. }
  1168. ret = 0;
  1169. bail:
  1170. return ret;
  1171. }
  1172. static int qib_shut_down_port(struct rvt_dev_info *rdi, u8 port_num)
  1173. {
  1174. struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
  1175. struct qib_devdata *dd = dd_from_dev(ibdev);
  1176. struct qib_pportdata *ppd = &dd->pport[port_num - 1];
  1177. qib_set_linkstate(ppd, QIB_IB_LINKDOWN);
  1178. return 0;
  1179. }
  1180. static int qib_get_guid_be(struct rvt_dev_info *rdi, struct rvt_ibport *rvp,
  1181. int guid_index, __be64 *guid)
  1182. {
  1183. struct qib_ibport *ibp = container_of(rvp, struct qib_ibport, rvp);
  1184. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1185. if (guid_index == 0)
  1186. *guid = ppd->guid;
  1187. else if (guid_index < QIB_GUIDS_PER_PORT)
  1188. *guid = ibp->guids[guid_index - 1];
  1189. else
  1190. return -EINVAL;
  1191. return 0;
  1192. }
  1193. int qib_check_ah(struct ib_device *ibdev, struct rdma_ah_attr *ah_attr)
  1194. {
  1195. if (rdma_ah_get_sl(ah_attr) > 15)
  1196. return -EINVAL;
  1197. if (rdma_ah_get_dlid(ah_attr) == 0)
  1198. return -EINVAL;
  1199. if (rdma_ah_get_dlid(ah_attr) >=
  1200. be16_to_cpu(IB_MULTICAST_LID_BASE) &&
  1201. rdma_ah_get_dlid(ah_attr) !=
  1202. be16_to_cpu(IB_LID_PERMISSIVE) &&
  1203. !(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH))
  1204. return -EINVAL;
  1205. return 0;
  1206. }
  1207. static void qib_notify_new_ah(struct ib_device *ibdev,
  1208. struct rdma_ah_attr *ah_attr,
  1209. struct rvt_ah *ah)
  1210. {
  1211. struct qib_ibport *ibp;
  1212. struct qib_pportdata *ppd;
  1213. /*
  1214. * Do not trust reading anything from rvt_ah at this point as it is not
  1215. * done being setup. We can however modify things which we need to set.
  1216. */
  1217. ibp = to_iport(ibdev, rdma_ah_get_port_num(ah_attr));
  1218. ppd = ppd_from_ibp(ibp);
  1219. ah->vl = ibp->sl_to_vl[rdma_ah_get_sl(&ah->attr)];
  1220. ah->log_pmtu = ilog2(ppd->ibmtu);
  1221. }
  1222. struct ib_ah *qib_create_qp0_ah(struct qib_ibport *ibp, u16 dlid)
  1223. {
  1224. struct rdma_ah_attr attr;
  1225. struct ib_ah *ah = ERR_PTR(-EINVAL);
  1226. struct rvt_qp *qp0;
  1227. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1228. struct qib_devdata *dd = dd_from_ppd(ppd);
  1229. u8 port_num = ppd->port;
  1230. memset(&attr, 0, sizeof(attr));
  1231. attr.type = rdma_ah_find_type(&dd->verbs_dev.rdi.ibdev, port_num);
  1232. rdma_ah_set_dlid(&attr, dlid);
  1233. rdma_ah_set_port_num(&attr, port_num);
  1234. rcu_read_lock();
  1235. qp0 = rcu_dereference(ibp->rvp.qp[0]);
  1236. if (qp0)
  1237. ah = rdma_create_ah(qp0->ibqp.pd, &attr);
  1238. rcu_read_unlock();
  1239. return ah;
  1240. }
  1241. /**
  1242. * qib_get_npkeys - return the size of the PKEY table for context 0
  1243. * @dd: the qlogic_ib device
  1244. */
  1245. unsigned qib_get_npkeys(struct qib_devdata *dd)
  1246. {
  1247. return ARRAY_SIZE(dd->rcd[0]->pkeys);
  1248. }
  1249. /*
  1250. * Return the indexed PKEY from the port PKEY table.
  1251. * No need to validate rcd[ctxt]; the port is setup if we are here.
  1252. */
  1253. unsigned qib_get_pkey(struct qib_ibport *ibp, unsigned index)
  1254. {
  1255. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1256. struct qib_devdata *dd = ppd->dd;
  1257. unsigned ctxt = ppd->hw_pidx;
  1258. unsigned ret;
  1259. /* dd->rcd null if mini_init or some init failures */
  1260. if (!dd->rcd || index >= ARRAY_SIZE(dd->rcd[ctxt]->pkeys))
  1261. ret = 0;
  1262. else
  1263. ret = dd->rcd[ctxt]->pkeys[index];
  1264. return ret;
  1265. }
  1266. static void init_ibport(struct qib_pportdata *ppd)
  1267. {
  1268. struct qib_verbs_counters cntrs;
  1269. struct qib_ibport *ibp = &ppd->ibport_data;
  1270. spin_lock_init(&ibp->rvp.lock);
  1271. /* Set the prefix to the default value (see ch. 4.1.1) */
  1272. ibp->rvp.gid_prefix = IB_DEFAULT_GID_PREFIX;
  1273. ibp->rvp.sm_lid = be16_to_cpu(IB_LID_PERMISSIVE);
  1274. ibp->rvp.port_cap_flags = IB_PORT_SYS_IMAGE_GUID_SUP |
  1275. IB_PORT_CLIENT_REG_SUP | IB_PORT_SL_MAP_SUP |
  1276. IB_PORT_TRAP_SUP | IB_PORT_AUTO_MIGR_SUP |
  1277. IB_PORT_DR_NOTICE_SUP | IB_PORT_CAP_MASK_NOTICE_SUP |
  1278. IB_PORT_OTHER_LOCAL_CHANGES_SUP;
  1279. if (ppd->dd->flags & QIB_HAS_LINK_LATENCY)
  1280. ibp->rvp.port_cap_flags |= IB_PORT_LINK_LATENCY_SUP;
  1281. ibp->rvp.pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
  1282. ibp->rvp.pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
  1283. ibp->rvp.pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
  1284. ibp->rvp.pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
  1285. ibp->rvp.pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
  1286. /* Snapshot current HW counters to "clear" them. */
  1287. qib_get_counters(ppd, &cntrs);
  1288. ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
  1289. ibp->z_link_error_recovery_counter =
  1290. cntrs.link_error_recovery_counter;
  1291. ibp->z_link_downed_counter = cntrs.link_downed_counter;
  1292. ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
  1293. ibp->z_port_rcv_remphys_errors = cntrs.port_rcv_remphys_errors;
  1294. ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
  1295. ibp->z_port_xmit_data = cntrs.port_xmit_data;
  1296. ibp->z_port_rcv_data = cntrs.port_rcv_data;
  1297. ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
  1298. ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
  1299. ibp->z_local_link_integrity_errors =
  1300. cntrs.local_link_integrity_errors;
  1301. ibp->z_excessive_buffer_overrun_errors =
  1302. cntrs.excessive_buffer_overrun_errors;
  1303. ibp->z_vl15_dropped = cntrs.vl15_dropped;
  1304. RCU_INIT_POINTER(ibp->rvp.qp[0], NULL);
  1305. RCU_INIT_POINTER(ibp->rvp.qp[1], NULL);
  1306. }
  1307. /**
  1308. * qib_fill_device_attr - Fill in rvt dev info device attributes.
  1309. * @dd: the device data structure
  1310. */
  1311. static void qib_fill_device_attr(struct qib_devdata *dd)
  1312. {
  1313. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  1314. memset(&rdi->dparms.props, 0, sizeof(rdi->dparms.props));
  1315. rdi->dparms.props.max_pd = ib_qib_max_pds;
  1316. rdi->dparms.props.max_ah = ib_qib_max_ahs;
  1317. rdi->dparms.props.device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
  1318. IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
  1319. IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
  1320. IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE;
  1321. rdi->dparms.props.page_size_cap = PAGE_SIZE;
  1322. rdi->dparms.props.vendor_id =
  1323. QIB_SRC_OUI_1 << 16 | QIB_SRC_OUI_2 << 8 | QIB_SRC_OUI_3;
  1324. rdi->dparms.props.vendor_part_id = dd->deviceid;
  1325. rdi->dparms.props.hw_ver = dd->minrev;
  1326. rdi->dparms.props.sys_image_guid = ib_qib_sys_image_guid;
  1327. rdi->dparms.props.max_mr_size = ~0ULL;
  1328. rdi->dparms.props.max_qp = ib_qib_max_qps;
  1329. rdi->dparms.props.max_qp_wr = ib_qib_max_qp_wrs;
  1330. rdi->dparms.props.max_send_sge = ib_qib_max_sges;
  1331. rdi->dparms.props.max_recv_sge = ib_qib_max_sges;
  1332. rdi->dparms.props.max_sge_rd = ib_qib_max_sges;
  1333. rdi->dparms.props.max_cq = ib_qib_max_cqs;
  1334. rdi->dparms.props.max_cqe = ib_qib_max_cqes;
  1335. rdi->dparms.props.max_ah = ib_qib_max_ahs;
  1336. rdi->dparms.props.max_map_per_fmr = 32767;
  1337. rdi->dparms.props.max_qp_rd_atom = QIB_MAX_RDMA_ATOMIC;
  1338. rdi->dparms.props.max_qp_init_rd_atom = 255;
  1339. rdi->dparms.props.max_srq = ib_qib_max_srqs;
  1340. rdi->dparms.props.max_srq_wr = ib_qib_max_srq_wrs;
  1341. rdi->dparms.props.max_srq_sge = ib_qib_max_srq_sges;
  1342. rdi->dparms.props.atomic_cap = IB_ATOMIC_GLOB;
  1343. rdi->dparms.props.max_pkeys = qib_get_npkeys(dd);
  1344. rdi->dparms.props.max_mcast_grp = ib_qib_max_mcast_grps;
  1345. rdi->dparms.props.max_mcast_qp_attach = ib_qib_max_mcast_qp_attached;
  1346. rdi->dparms.props.max_total_mcast_qp_attach =
  1347. rdi->dparms.props.max_mcast_qp_attach *
  1348. rdi->dparms.props.max_mcast_grp;
  1349. /* post send table */
  1350. dd->verbs_dev.rdi.post_parms = qib_post_parms;
  1351. }
  1352. /**
  1353. * qib_register_ib_device - register our device with the infiniband core
  1354. * @dd: the device data structure
  1355. * Return the allocated qib_ibdev pointer or NULL on error.
  1356. */
  1357. int qib_register_ib_device(struct qib_devdata *dd)
  1358. {
  1359. struct qib_ibdev *dev = &dd->verbs_dev;
  1360. struct ib_device *ibdev = &dev->rdi.ibdev;
  1361. struct qib_pportdata *ppd = dd->pport;
  1362. unsigned i, ctxt;
  1363. int ret;
  1364. get_random_bytes(&dev->qp_rnd, sizeof(dev->qp_rnd));
  1365. for (i = 0; i < dd->num_pports; i++)
  1366. init_ibport(ppd + i);
  1367. /* Only need to initialize non-zero fields. */
  1368. timer_setup(&dev->mem_timer, mem_timer, 0);
  1369. INIT_LIST_HEAD(&dev->piowait);
  1370. INIT_LIST_HEAD(&dev->dmawait);
  1371. INIT_LIST_HEAD(&dev->txwait);
  1372. INIT_LIST_HEAD(&dev->memwait);
  1373. INIT_LIST_HEAD(&dev->txreq_free);
  1374. if (ppd->sdma_descq_cnt) {
  1375. dev->pio_hdrs = dma_alloc_coherent(&dd->pcidev->dev,
  1376. ppd->sdma_descq_cnt *
  1377. sizeof(struct qib_pio_header),
  1378. &dev->pio_hdrs_phys,
  1379. GFP_KERNEL);
  1380. if (!dev->pio_hdrs) {
  1381. ret = -ENOMEM;
  1382. goto err_hdrs;
  1383. }
  1384. }
  1385. for (i = 0; i < ppd->sdma_descq_cnt; i++) {
  1386. struct qib_verbs_txreq *tx;
  1387. tx = kzalloc(sizeof(*tx), GFP_KERNEL);
  1388. if (!tx) {
  1389. ret = -ENOMEM;
  1390. goto err_tx;
  1391. }
  1392. tx->hdr_inx = i;
  1393. list_add(&tx->txreq.list, &dev->txreq_free);
  1394. }
  1395. /*
  1396. * The system image GUID is supposed to be the same for all
  1397. * IB HCAs in a single system but since there can be other
  1398. * device types in the system, we can't be sure this is unique.
  1399. */
  1400. if (!ib_qib_sys_image_guid)
  1401. ib_qib_sys_image_guid = ppd->guid;
  1402. ibdev->owner = THIS_MODULE;
  1403. ibdev->node_guid = ppd->guid;
  1404. ibdev->phys_port_cnt = dd->num_pports;
  1405. ibdev->dev.parent = &dd->pcidev->dev;
  1406. ibdev->modify_device = qib_modify_device;
  1407. ibdev->process_mad = qib_process_mad;
  1408. snprintf(ibdev->node_desc, sizeof(ibdev->node_desc),
  1409. "Intel Infiniband HCA %s", init_utsname()->nodename);
  1410. /*
  1411. * Fill in rvt info object.
  1412. */
  1413. dd->verbs_dev.rdi.driver_f.port_callback = qib_create_port_files;
  1414. dd->verbs_dev.rdi.driver_f.get_pci_dev = qib_get_pci_dev;
  1415. dd->verbs_dev.rdi.driver_f.check_ah = qib_check_ah;
  1416. dd->verbs_dev.rdi.driver_f.check_send_wqe = qib_check_send_wqe;
  1417. dd->verbs_dev.rdi.driver_f.notify_new_ah = qib_notify_new_ah;
  1418. dd->verbs_dev.rdi.driver_f.alloc_qpn = qib_alloc_qpn;
  1419. dd->verbs_dev.rdi.driver_f.qp_priv_alloc = qib_qp_priv_alloc;
  1420. dd->verbs_dev.rdi.driver_f.qp_priv_free = qib_qp_priv_free;
  1421. dd->verbs_dev.rdi.driver_f.free_all_qps = qib_free_all_qps;
  1422. dd->verbs_dev.rdi.driver_f.notify_qp_reset = qib_notify_qp_reset;
  1423. dd->verbs_dev.rdi.driver_f.do_send = qib_do_send;
  1424. dd->verbs_dev.rdi.driver_f.schedule_send = qib_schedule_send;
  1425. dd->verbs_dev.rdi.driver_f.quiesce_qp = qib_quiesce_qp;
  1426. dd->verbs_dev.rdi.driver_f.stop_send_queue = qib_stop_send_queue;
  1427. dd->verbs_dev.rdi.driver_f.flush_qp_waiters = qib_flush_qp_waiters;
  1428. dd->verbs_dev.rdi.driver_f.notify_error_qp = qib_notify_error_qp;
  1429. dd->verbs_dev.rdi.driver_f.notify_restart_rc = qib_restart_rc;
  1430. dd->verbs_dev.rdi.driver_f.mtu_to_path_mtu = qib_mtu_to_path_mtu;
  1431. dd->verbs_dev.rdi.driver_f.mtu_from_qp = qib_mtu_from_qp;
  1432. dd->verbs_dev.rdi.driver_f.get_pmtu_from_attr = qib_get_pmtu_from_attr;
  1433. dd->verbs_dev.rdi.driver_f.schedule_send_no_lock = _qib_schedule_send;
  1434. dd->verbs_dev.rdi.driver_f.query_port_state = qib_query_port;
  1435. dd->verbs_dev.rdi.driver_f.shut_down_port = qib_shut_down_port;
  1436. dd->verbs_dev.rdi.driver_f.cap_mask_chg = qib_cap_mask_chg;
  1437. dd->verbs_dev.rdi.driver_f.notify_create_mad_agent =
  1438. qib_notify_create_mad_agent;
  1439. dd->verbs_dev.rdi.driver_f.notify_free_mad_agent =
  1440. qib_notify_free_mad_agent;
  1441. dd->verbs_dev.rdi.dparms.max_rdma_atomic = QIB_MAX_RDMA_ATOMIC;
  1442. dd->verbs_dev.rdi.driver_f.get_guid_be = qib_get_guid_be;
  1443. dd->verbs_dev.rdi.dparms.lkey_table_size = qib_lkey_table_size;
  1444. dd->verbs_dev.rdi.dparms.qp_table_size = ib_qib_qp_table_size;
  1445. dd->verbs_dev.rdi.dparms.qpn_start = 1;
  1446. dd->verbs_dev.rdi.dparms.qpn_res_start = QIB_KD_QP;
  1447. dd->verbs_dev.rdi.dparms.qpn_res_end = QIB_KD_QP; /* Reserve one QP */
  1448. dd->verbs_dev.rdi.dparms.qpn_inc = 1;
  1449. dd->verbs_dev.rdi.dparms.qos_shift = 1;
  1450. dd->verbs_dev.rdi.dparms.psn_mask = QIB_PSN_MASK;
  1451. dd->verbs_dev.rdi.dparms.psn_shift = QIB_PSN_SHIFT;
  1452. dd->verbs_dev.rdi.dparms.psn_modify_mask = QIB_PSN_MASK;
  1453. dd->verbs_dev.rdi.dparms.nports = dd->num_pports;
  1454. dd->verbs_dev.rdi.dparms.npkeys = qib_get_npkeys(dd);
  1455. dd->verbs_dev.rdi.dparms.node = dd->assigned_node_id;
  1456. dd->verbs_dev.rdi.dparms.core_cap_flags = RDMA_CORE_PORT_IBA_IB;
  1457. dd->verbs_dev.rdi.dparms.max_mad_size = IB_MGMT_MAD_SIZE;
  1458. qib_fill_device_attr(dd);
  1459. ppd = dd->pport;
  1460. for (i = 0; i < dd->num_pports; i++, ppd++) {
  1461. ctxt = ppd->hw_pidx;
  1462. rvt_init_port(&dd->verbs_dev.rdi,
  1463. &ppd->ibport_data.rvp,
  1464. i,
  1465. dd->rcd[ctxt]->pkeys);
  1466. }
  1467. ret = rvt_register_device(&dd->verbs_dev.rdi, RDMA_DRIVER_QIB);
  1468. if (ret)
  1469. goto err_tx;
  1470. ret = qib_verbs_register_sysfs(dd);
  1471. if (ret)
  1472. goto err_class;
  1473. return ret;
  1474. err_class:
  1475. rvt_unregister_device(&dd->verbs_dev.rdi);
  1476. err_tx:
  1477. while (!list_empty(&dev->txreq_free)) {
  1478. struct list_head *l = dev->txreq_free.next;
  1479. struct qib_verbs_txreq *tx;
  1480. list_del(l);
  1481. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  1482. kfree(tx);
  1483. }
  1484. if (ppd->sdma_descq_cnt)
  1485. dma_free_coherent(&dd->pcidev->dev,
  1486. ppd->sdma_descq_cnt *
  1487. sizeof(struct qib_pio_header),
  1488. dev->pio_hdrs, dev->pio_hdrs_phys);
  1489. err_hdrs:
  1490. qib_dev_err(dd, "cannot register verbs: %d!\n", -ret);
  1491. return ret;
  1492. }
  1493. void qib_unregister_ib_device(struct qib_devdata *dd)
  1494. {
  1495. struct qib_ibdev *dev = &dd->verbs_dev;
  1496. qib_verbs_unregister_sysfs(dd);
  1497. rvt_unregister_device(&dd->verbs_dev.rdi);
  1498. if (!list_empty(&dev->piowait))
  1499. qib_dev_err(dd, "piowait list not empty!\n");
  1500. if (!list_empty(&dev->dmawait))
  1501. qib_dev_err(dd, "dmawait list not empty!\n");
  1502. if (!list_empty(&dev->txwait))
  1503. qib_dev_err(dd, "txwait list not empty!\n");
  1504. if (!list_empty(&dev->memwait))
  1505. qib_dev_err(dd, "memwait list not empty!\n");
  1506. del_timer_sync(&dev->mem_timer);
  1507. while (!list_empty(&dev->txreq_free)) {
  1508. struct list_head *l = dev->txreq_free.next;
  1509. struct qib_verbs_txreq *tx;
  1510. list_del(l);
  1511. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  1512. kfree(tx);
  1513. }
  1514. if (dd->pport->sdma_descq_cnt)
  1515. dma_free_coherent(&dd->pcidev->dev,
  1516. dd->pport->sdma_descq_cnt *
  1517. sizeof(struct qib_pio_header),
  1518. dev->pio_hdrs, dev->pio_hdrs_phys);
  1519. }
  1520. /**
  1521. * _qib_schedule_send - schedule progress
  1522. * @qp - the qp
  1523. *
  1524. * This schedules progress w/o regard to the s_flags.
  1525. *
  1526. * It is only used in post send, which doesn't hold
  1527. * the s_lock.
  1528. */
  1529. void _qib_schedule_send(struct rvt_qp *qp)
  1530. {
  1531. struct qib_ibport *ibp =
  1532. to_iport(qp->ibqp.device, qp->port_num);
  1533. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1534. struct qib_qp_priv *priv = qp->priv;
  1535. queue_work(ppd->qib_wq, &priv->s_work);
  1536. }
  1537. /**
  1538. * qib_schedule_send - schedule progress
  1539. * @qp - the qp
  1540. *
  1541. * This schedules qp progress. The s_lock
  1542. * should be held.
  1543. */
  1544. void qib_schedule_send(struct rvt_qp *qp)
  1545. {
  1546. if (qib_send_ok(qp))
  1547. _qib_schedule_send(qp);
  1548. }