qib_verbs.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. /*
  2. * Copyright (c) 2012, 2013 Intel Corporation. All rights reserved.
  3. * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
  4. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <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. list_for_each_entry_rcu(p, &mcast->qp_list, list)
  320. qib_qp_rcv(rcd, hdr, 1, data, tlen, p->qp);
  321. /*
  322. * Notify rvt_multicast_detach() if it is waiting for us
  323. * to finish.
  324. */
  325. if (atomic_dec_return(&mcast->refcount) <= 1)
  326. wake_up(&mcast->wait);
  327. } else {
  328. rcu_read_lock();
  329. qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
  330. if (!qp) {
  331. rcu_read_unlock();
  332. goto drop;
  333. }
  334. this_cpu_inc(ibp->pmastats->n_unicast_rcv);
  335. qib_qp_rcv(rcd, hdr, lnh == QIB_LRH_GRH, data, tlen, qp);
  336. rcu_read_unlock();
  337. }
  338. return;
  339. drop:
  340. ibp->rvp.n_pkt_drops++;
  341. }
  342. /*
  343. * This is called from a timer to check for QPs
  344. * which need kernel memory in order to send a packet.
  345. */
  346. static void mem_timer(unsigned long data)
  347. {
  348. struct qib_ibdev *dev = (struct qib_ibdev *) data;
  349. struct list_head *list = &dev->memwait;
  350. struct rvt_qp *qp = NULL;
  351. struct qib_qp_priv *priv = NULL;
  352. unsigned long flags;
  353. spin_lock_irqsave(&dev->rdi.pending_lock, flags);
  354. if (!list_empty(list)) {
  355. priv = list_entry(list->next, struct qib_qp_priv, iowait);
  356. qp = priv->owner;
  357. list_del_init(&priv->iowait);
  358. rvt_get_qp(qp);
  359. if (!list_empty(list))
  360. mod_timer(&dev->mem_timer, jiffies + 1);
  361. }
  362. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  363. if (qp) {
  364. spin_lock_irqsave(&qp->s_lock, flags);
  365. if (qp->s_flags & RVT_S_WAIT_KMEM) {
  366. qp->s_flags &= ~RVT_S_WAIT_KMEM;
  367. qib_schedule_send(qp);
  368. }
  369. spin_unlock_irqrestore(&qp->s_lock, flags);
  370. rvt_put_qp(qp);
  371. }
  372. }
  373. #ifdef __LITTLE_ENDIAN
  374. static inline u32 get_upper_bits(u32 data, u32 shift)
  375. {
  376. return data >> shift;
  377. }
  378. static inline u32 set_upper_bits(u32 data, u32 shift)
  379. {
  380. return data << shift;
  381. }
  382. static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
  383. {
  384. data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
  385. data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
  386. return data;
  387. }
  388. #else
  389. static inline u32 get_upper_bits(u32 data, u32 shift)
  390. {
  391. return data << shift;
  392. }
  393. static inline u32 set_upper_bits(u32 data, u32 shift)
  394. {
  395. return data >> shift;
  396. }
  397. static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
  398. {
  399. data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
  400. data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
  401. return data;
  402. }
  403. #endif
  404. static void copy_io(u32 __iomem *piobuf, struct rvt_sge_state *ss,
  405. u32 length, unsigned flush_wc)
  406. {
  407. u32 extra = 0;
  408. u32 data = 0;
  409. u32 last;
  410. while (1) {
  411. u32 len = ss->sge.length;
  412. u32 off;
  413. if (len > length)
  414. len = length;
  415. if (len > ss->sge.sge_length)
  416. len = ss->sge.sge_length;
  417. BUG_ON(len == 0);
  418. /* If the source address is not aligned, try to align it. */
  419. off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
  420. if (off) {
  421. u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
  422. ~(sizeof(u32) - 1));
  423. u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
  424. u32 y;
  425. y = sizeof(u32) - off;
  426. if (len > y)
  427. len = y;
  428. if (len + extra >= sizeof(u32)) {
  429. data |= set_upper_bits(v, extra *
  430. BITS_PER_BYTE);
  431. len = sizeof(u32) - extra;
  432. if (len == length) {
  433. last = data;
  434. break;
  435. }
  436. __raw_writel(data, piobuf);
  437. piobuf++;
  438. extra = 0;
  439. data = 0;
  440. } else {
  441. /* Clear unused upper bytes */
  442. data |= clear_upper_bytes(v, len, extra);
  443. if (len == length) {
  444. last = data;
  445. break;
  446. }
  447. extra += len;
  448. }
  449. } else if (extra) {
  450. /* Source address is aligned. */
  451. u32 *addr = (u32 *) ss->sge.vaddr;
  452. int shift = extra * BITS_PER_BYTE;
  453. int ushift = 32 - shift;
  454. u32 l = len;
  455. while (l >= sizeof(u32)) {
  456. u32 v = *addr;
  457. data |= set_upper_bits(v, shift);
  458. __raw_writel(data, piobuf);
  459. data = get_upper_bits(v, ushift);
  460. piobuf++;
  461. addr++;
  462. l -= sizeof(u32);
  463. }
  464. /*
  465. * We still have 'extra' number of bytes leftover.
  466. */
  467. if (l) {
  468. u32 v = *addr;
  469. if (l + extra >= sizeof(u32)) {
  470. data |= set_upper_bits(v, shift);
  471. len -= l + extra - sizeof(u32);
  472. if (len == length) {
  473. last = data;
  474. break;
  475. }
  476. __raw_writel(data, piobuf);
  477. piobuf++;
  478. extra = 0;
  479. data = 0;
  480. } else {
  481. /* Clear unused upper bytes */
  482. data |= clear_upper_bytes(v, l, extra);
  483. if (len == length) {
  484. last = data;
  485. break;
  486. }
  487. extra += l;
  488. }
  489. } else if (len == length) {
  490. last = data;
  491. break;
  492. }
  493. } else if (len == length) {
  494. u32 w;
  495. /*
  496. * Need to round up for the last dword in the
  497. * packet.
  498. */
  499. w = (len + 3) >> 2;
  500. qib_pio_copy(piobuf, ss->sge.vaddr, w - 1);
  501. piobuf += w - 1;
  502. last = ((u32 *) ss->sge.vaddr)[w - 1];
  503. break;
  504. } else {
  505. u32 w = len >> 2;
  506. qib_pio_copy(piobuf, ss->sge.vaddr, w);
  507. piobuf += w;
  508. extra = len & (sizeof(u32) - 1);
  509. if (extra) {
  510. u32 v = ((u32 *) ss->sge.vaddr)[w];
  511. /* Clear unused upper bytes */
  512. data = clear_upper_bytes(v, extra, 0);
  513. }
  514. }
  515. rvt_update_sge(ss, len, false);
  516. length -= len;
  517. }
  518. /* Update address before sending packet. */
  519. rvt_update_sge(ss, length, false);
  520. if (flush_wc) {
  521. /* must flush early everything before trigger word */
  522. qib_flush_wc();
  523. __raw_writel(last, piobuf);
  524. /* be sure trigger word is written */
  525. qib_flush_wc();
  526. } else
  527. __raw_writel(last, piobuf);
  528. }
  529. static noinline struct qib_verbs_txreq *__get_txreq(struct qib_ibdev *dev,
  530. struct rvt_qp *qp)
  531. {
  532. struct qib_qp_priv *priv = qp->priv;
  533. struct qib_verbs_txreq *tx;
  534. unsigned long flags;
  535. spin_lock_irqsave(&qp->s_lock, flags);
  536. spin_lock(&dev->rdi.pending_lock);
  537. if (!list_empty(&dev->txreq_free)) {
  538. struct list_head *l = dev->txreq_free.next;
  539. list_del(l);
  540. spin_unlock(&dev->rdi.pending_lock);
  541. spin_unlock_irqrestore(&qp->s_lock, flags);
  542. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  543. } else {
  544. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK &&
  545. list_empty(&priv->iowait)) {
  546. dev->n_txwait++;
  547. qp->s_flags |= RVT_S_WAIT_TX;
  548. list_add_tail(&priv->iowait, &dev->txwait);
  549. }
  550. qp->s_flags &= ~RVT_S_BUSY;
  551. spin_unlock(&dev->rdi.pending_lock);
  552. spin_unlock_irqrestore(&qp->s_lock, flags);
  553. tx = ERR_PTR(-EBUSY);
  554. }
  555. return tx;
  556. }
  557. static inline struct qib_verbs_txreq *get_txreq(struct qib_ibdev *dev,
  558. struct rvt_qp *qp)
  559. {
  560. struct qib_verbs_txreq *tx;
  561. unsigned long flags;
  562. spin_lock_irqsave(&dev->rdi.pending_lock, flags);
  563. /* assume the list non empty */
  564. if (likely(!list_empty(&dev->txreq_free))) {
  565. struct list_head *l = dev->txreq_free.next;
  566. list_del(l);
  567. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  568. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  569. } else {
  570. /* call slow path to get the extra lock */
  571. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  572. tx = __get_txreq(dev, qp);
  573. }
  574. return tx;
  575. }
  576. void qib_put_txreq(struct qib_verbs_txreq *tx)
  577. {
  578. struct qib_ibdev *dev;
  579. struct rvt_qp *qp;
  580. struct qib_qp_priv *priv;
  581. unsigned long flags;
  582. qp = tx->qp;
  583. dev = to_idev(qp->ibqp.device);
  584. if (tx->mr) {
  585. rvt_put_mr(tx->mr);
  586. tx->mr = NULL;
  587. }
  588. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF) {
  589. tx->txreq.flags &= ~QIB_SDMA_TXREQ_F_FREEBUF;
  590. dma_unmap_single(&dd_from_dev(dev)->pcidev->dev,
  591. tx->txreq.addr, tx->hdr_dwords << 2,
  592. DMA_TO_DEVICE);
  593. kfree(tx->align_buf);
  594. }
  595. spin_lock_irqsave(&dev->rdi.pending_lock, flags);
  596. /* Put struct back on free list */
  597. list_add(&tx->txreq.list, &dev->txreq_free);
  598. if (!list_empty(&dev->txwait)) {
  599. /* Wake up first QP wanting a free struct */
  600. priv = list_entry(dev->txwait.next, struct qib_qp_priv,
  601. iowait);
  602. qp = priv->owner;
  603. list_del_init(&priv->iowait);
  604. rvt_get_qp(qp);
  605. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  606. spin_lock_irqsave(&qp->s_lock, flags);
  607. if (qp->s_flags & RVT_S_WAIT_TX) {
  608. qp->s_flags &= ~RVT_S_WAIT_TX;
  609. qib_schedule_send(qp);
  610. }
  611. spin_unlock_irqrestore(&qp->s_lock, flags);
  612. rvt_put_qp(qp);
  613. } else
  614. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  615. }
  616. /*
  617. * This is called when there are send DMA descriptors that might be
  618. * available.
  619. *
  620. * This is called with ppd->sdma_lock held.
  621. */
  622. void qib_verbs_sdma_desc_avail(struct qib_pportdata *ppd, unsigned avail)
  623. {
  624. struct rvt_qp *qp, *nqp;
  625. struct qib_qp_priv *qpp, *nqpp;
  626. struct rvt_qp *qps[20];
  627. struct qib_ibdev *dev;
  628. unsigned i, n;
  629. n = 0;
  630. dev = &ppd->dd->verbs_dev;
  631. spin_lock(&dev->rdi.pending_lock);
  632. /* Search wait list for first QP wanting DMA descriptors. */
  633. list_for_each_entry_safe(qpp, nqpp, &dev->dmawait, iowait) {
  634. qp = qpp->owner;
  635. nqp = nqpp->owner;
  636. if (qp->port_num != ppd->port)
  637. continue;
  638. if (n == ARRAY_SIZE(qps))
  639. break;
  640. if (qpp->s_tx->txreq.sg_count > avail)
  641. break;
  642. avail -= qpp->s_tx->txreq.sg_count;
  643. list_del_init(&qpp->iowait);
  644. rvt_get_qp(qp);
  645. qps[n++] = qp;
  646. }
  647. spin_unlock(&dev->rdi.pending_lock);
  648. for (i = 0; i < n; i++) {
  649. qp = qps[i];
  650. spin_lock(&qp->s_lock);
  651. if (qp->s_flags & RVT_S_WAIT_DMA_DESC) {
  652. qp->s_flags &= ~RVT_S_WAIT_DMA_DESC;
  653. qib_schedule_send(qp);
  654. }
  655. spin_unlock(&qp->s_lock);
  656. rvt_put_qp(qp);
  657. }
  658. }
  659. /*
  660. * This is called with ppd->sdma_lock held.
  661. */
  662. static void sdma_complete(struct qib_sdma_txreq *cookie, int status)
  663. {
  664. struct qib_verbs_txreq *tx =
  665. container_of(cookie, struct qib_verbs_txreq, txreq);
  666. struct rvt_qp *qp = tx->qp;
  667. struct qib_qp_priv *priv = qp->priv;
  668. spin_lock(&qp->s_lock);
  669. if (tx->wqe)
  670. qib_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
  671. else if (qp->ibqp.qp_type == IB_QPT_RC) {
  672. struct ib_header *hdr;
  673. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF)
  674. hdr = &tx->align_buf->hdr;
  675. else {
  676. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  677. hdr = &dev->pio_hdrs[tx->hdr_inx].hdr;
  678. }
  679. qib_rc_send_complete(qp, hdr);
  680. }
  681. if (atomic_dec_and_test(&priv->s_dma_busy)) {
  682. if (qp->state == IB_QPS_RESET)
  683. wake_up(&priv->wait_dma);
  684. else if (qp->s_flags & RVT_S_WAIT_DMA) {
  685. qp->s_flags &= ~RVT_S_WAIT_DMA;
  686. qib_schedule_send(qp);
  687. }
  688. }
  689. spin_unlock(&qp->s_lock);
  690. qib_put_txreq(tx);
  691. }
  692. static int wait_kmem(struct qib_ibdev *dev, struct rvt_qp *qp)
  693. {
  694. struct qib_qp_priv *priv = qp->priv;
  695. unsigned long flags;
  696. int ret = 0;
  697. spin_lock_irqsave(&qp->s_lock, flags);
  698. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  699. spin_lock(&dev->rdi.pending_lock);
  700. if (list_empty(&priv->iowait)) {
  701. if (list_empty(&dev->memwait))
  702. mod_timer(&dev->mem_timer, jiffies + 1);
  703. qp->s_flags |= RVT_S_WAIT_KMEM;
  704. list_add_tail(&priv->iowait, &dev->memwait);
  705. }
  706. spin_unlock(&dev->rdi.pending_lock);
  707. qp->s_flags &= ~RVT_S_BUSY;
  708. ret = -EBUSY;
  709. }
  710. spin_unlock_irqrestore(&qp->s_lock, flags);
  711. return ret;
  712. }
  713. static int qib_verbs_send_dma(struct rvt_qp *qp, struct ib_header *hdr,
  714. u32 hdrwords, struct rvt_sge_state *ss, u32 len,
  715. u32 plen, u32 dwords)
  716. {
  717. struct qib_qp_priv *priv = qp->priv;
  718. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  719. struct qib_devdata *dd = dd_from_dev(dev);
  720. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  721. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  722. struct qib_verbs_txreq *tx;
  723. struct qib_pio_header *phdr;
  724. u32 control;
  725. u32 ndesc;
  726. int ret;
  727. tx = priv->s_tx;
  728. if (tx) {
  729. priv->s_tx = NULL;
  730. /* resend previously constructed packet */
  731. ret = qib_sdma_verbs_send(ppd, tx->ss, tx->dwords, tx);
  732. goto bail;
  733. }
  734. tx = get_txreq(dev, qp);
  735. if (IS_ERR(tx))
  736. goto bail_tx;
  737. control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
  738. be16_to_cpu(hdr->lrh[0]) >> 12);
  739. tx->qp = qp;
  740. tx->wqe = qp->s_wqe;
  741. tx->mr = qp->s_rdma_mr;
  742. if (qp->s_rdma_mr)
  743. qp->s_rdma_mr = NULL;
  744. tx->txreq.callback = sdma_complete;
  745. if (dd->flags & QIB_HAS_SDMA_TIMEOUT)
  746. tx->txreq.flags = QIB_SDMA_TXREQ_F_HEADTOHOST;
  747. else
  748. tx->txreq.flags = QIB_SDMA_TXREQ_F_INTREQ;
  749. if (plen + 1 > dd->piosize2kmax_dwords)
  750. tx->txreq.flags |= QIB_SDMA_TXREQ_F_USELARGEBUF;
  751. if (len) {
  752. /*
  753. * Don't try to DMA if it takes more descriptors than
  754. * the queue holds.
  755. */
  756. ndesc = qib_count_sge(ss, len);
  757. if (ndesc >= ppd->sdma_descq_cnt)
  758. ndesc = 0;
  759. } else
  760. ndesc = 1;
  761. if (ndesc) {
  762. phdr = &dev->pio_hdrs[tx->hdr_inx];
  763. phdr->pbc[0] = cpu_to_le32(plen);
  764. phdr->pbc[1] = cpu_to_le32(control);
  765. memcpy(&phdr->hdr, hdr, hdrwords << 2);
  766. tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEDESC;
  767. tx->txreq.sg_count = ndesc;
  768. tx->txreq.addr = dev->pio_hdrs_phys +
  769. tx->hdr_inx * sizeof(struct qib_pio_header);
  770. tx->hdr_dwords = hdrwords + 2; /* add PBC length */
  771. ret = qib_sdma_verbs_send(ppd, ss, dwords, tx);
  772. goto bail;
  773. }
  774. /* Allocate a buffer and copy the header and payload to it. */
  775. tx->hdr_dwords = plen + 1;
  776. phdr = kmalloc(tx->hdr_dwords << 2, GFP_ATOMIC);
  777. if (!phdr)
  778. goto err_tx;
  779. phdr->pbc[0] = cpu_to_le32(plen);
  780. phdr->pbc[1] = cpu_to_le32(control);
  781. memcpy(&phdr->hdr, hdr, hdrwords << 2);
  782. qib_copy_from_sge((u32 *) &phdr->hdr + hdrwords, ss, len);
  783. tx->txreq.addr = dma_map_single(&dd->pcidev->dev, phdr,
  784. tx->hdr_dwords << 2, DMA_TO_DEVICE);
  785. if (dma_mapping_error(&dd->pcidev->dev, tx->txreq.addr))
  786. goto map_err;
  787. tx->align_buf = phdr;
  788. tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEBUF;
  789. tx->txreq.sg_count = 1;
  790. ret = qib_sdma_verbs_send(ppd, NULL, 0, tx);
  791. goto unaligned;
  792. map_err:
  793. kfree(phdr);
  794. err_tx:
  795. qib_put_txreq(tx);
  796. ret = wait_kmem(dev, qp);
  797. unaligned:
  798. ibp->rvp.n_unaligned++;
  799. bail:
  800. return ret;
  801. bail_tx:
  802. ret = PTR_ERR(tx);
  803. goto bail;
  804. }
  805. /*
  806. * If we are now in the error state, return zero to flush the
  807. * send work request.
  808. */
  809. static int no_bufs_available(struct rvt_qp *qp)
  810. {
  811. struct qib_qp_priv *priv = qp->priv;
  812. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  813. struct qib_devdata *dd;
  814. unsigned long flags;
  815. int ret = 0;
  816. /*
  817. * Note that as soon as want_buffer() is called and
  818. * possibly before it returns, qib_ib_piobufavail()
  819. * could be called. Therefore, put QP on the I/O wait list before
  820. * enabling the PIO avail interrupt.
  821. */
  822. spin_lock_irqsave(&qp->s_lock, flags);
  823. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  824. spin_lock(&dev->rdi.pending_lock);
  825. if (list_empty(&priv->iowait)) {
  826. dev->n_piowait++;
  827. qp->s_flags |= RVT_S_WAIT_PIO;
  828. list_add_tail(&priv->iowait, &dev->piowait);
  829. dd = dd_from_dev(dev);
  830. dd->f_wantpiobuf_intr(dd, 1);
  831. }
  832. spin_unlock(&dev->rdi.pending_lock);
  833. qp->s_flags &= ~RVT_S_BUSY;
  834. ret = -EBUSY;
  835. }
  836. spin_unlock_irqrestore(&qp->s_lock, flags);
  837. return ret;
  838. }
  839. static int qib_verbs_send_pio(struct rvt_qp *qp, struct ib_header *ibhdr,
  840. u32 hdrwords, struct rvt_sge_state *ss, u32 len,
  841. u32 plen, u32 dwords)
  842. {
  843. struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  844. struct qib_pportdata *ppd = dd->pport + qp->port_num - 1;
  845. u32 *hdr = (u32 *) ibhdr;
  846. u32 __iomem *piobuf_orig;
  847. u32 __iomem *piobuf;
  848. u64 pbc;
  849. unsigned long flags;
  850. unsigned flush_wc;
  851. u32 control;
  852. u32 pbufn;
  853. control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
  854. be16_to_cpu(ibhdr->lrh[0]) >> 12);
  855. pbc = ((u64) control << 32) | plen;
  856. piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
  857. if (unlikely(piobuf == NULL))
  858. return no_bufs_available(qp);
  859. /*
  860. * Write the pbc.
  861. * We have to flush after the PBC for correctness on some cpus
  862. * or WC buffer can be written out of order.
  863. */
  864. writeq(pbc, piobuf);
  865. piobuf_orig = piobuf;
  866. piobuf += 2;
  867. flush_wc = dd->flags & QIB_PIO_FLUSH_WC;
  868. if (len == 0) {
  869. /*
  870. * If there is just the header portion, must flush before
  871. * writing last word of header for correctness, and after
  872. * the last header word (trigger word).
  873. */
  874. if (flush_wc) {
  875. qib_flush_wc();
  876. qib_pio_copy(piobuf, hdr, hdrwords - 1);
  877. qib_flush_wc();
  878. __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
  879. qib_flush_wc();
  880. } else
  881. qib_pio_copy(piobuf, hdr, hdrwords);
  882. goto done;
  883. }
  884. if (flush_wc)
  885. qib_flush_wc();
  886. qib_pio_copy(piobuf, hdr, hdrwords);
  887. piobuf += hdrwords;
  888. /* The common case is aligned and contained in one segment. */
  889. if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
  890. !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
  891. u32 *addr = (u32 *) ss->sge.vaddr;
  892. /* Update address before sending packet. */
  893. rvt_update_sge(ss, len, false);
  894. if (flush_wc) {
  895. qib_pio_copy(piobuf, addr, dwords - 1);
  896. /* must flush early everything before trigger word */
  897. qib_flush_wc();
  898. __raw_writel(addr[dwords - 1], piobuf + dwords - 1);
  899. /* be sure trigger word is written */
  900. qib_flush_wc();
  901. } else
  902. qib_pio_copy(piobuf, addr, dwords);
  903. goto done;
  904. }
  905. copy_io(piobuf, ss, len, flush_wc);
  906. done:
  907. if (dd->flags & QIB_USE_SPCL_TRIG) {
  908. u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
  909. qib_flush_wc();
  910. __raw_writel(0xaebecede, piobuf_orig + spcl_off);
  911. }
  912. qib_sendbuf_done(dd, pbufn);
  913. if (qp->s_rdma_mr) {
  914. rvt_put_mr(qp->s_rdma_mr);
  915. qp->s_rdma_mr = NULL;
  916. }
  917. if (qp->s_wqe) {
  918. spin_lock_irqsave(&qp->s_lock, flags);
  919. qib_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
  920. spin_unlock_irqrestore(&qp->s_lock, flags);
  921. } else if (qp->ibqp.qp_type == IB_QPT_RC) {
  922. spin_lock_irqsave(&qp->s_lock, flags);
  923. qib_rc_send_complete(qp, ibhdr);
  924. spin_unlock_irqrestore(&qp->s_lock, flags);
  925. }
  926. return 0;
  927. }
  928. /**
  929. * qib_verbs_send - send a packet
  930. * @qp: the QP to send on
  931. * @hdr: the packet header
  932. * @hdrwords: the number of 32-bit words in the header
  933. * @ss: the SGE to send
  934. * @len: the length of the packet in bytes
  935. *
  936. * Return zero if packet is sent or queued OK.
  937. * Return non-zero and clear qp->s_flags RVT_S_BUSY otherwise.
  938. */
  939. int qib_verbs_send(struct rvt_qp *qp, struct ib_header *hdr,
  940. u32 hdrwords, struct rvt_sge_state *ss, u32 len)
  941. {
  942. struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  943. u32 plen;
  944. int ret;
  945. u32 dwords = (len + 3) >> 2;
  946. /*
  947. * Calculate the send buffer trigger address.
  948. * The +1 counts for the pbc control dword following the pbc length.
  949. */
  950. plen = hdrwords + dwords + 1;
  951. /*
  952. * VL15 packets (IB_QPT_SMI) will always use PIO, so we
  953. * can defer SDMA restart until link goes ACTIVE without
  954. * worrying about just how we got there.
  955. */
  956. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  957. !(dd->flags & QIB_HAS_SEND_DMA))
  958. ret = qib_verbs_send_pio(qp, hdr, hdrwords, ss, len,
  959. plen, dwords);
  960. else
  961. ret = qib_verbs_send_dma(qp, hdr, hdrwords, ss, len,
  962. plen, dwords);
  963. return ret;
  964. }
  965. int qib_snapshot_counters(struct qib_pportdata *ppd, u64 *swords,
  966. u64 *rwords, u64 *spkts, u64 *rpkts,
  967. u64 *xmit_wait)
  968. {
  969. int ret;
  970. struct qib_devdata *dd = ppd->dd;
  971. if (!(dd->flags & QIB_PRESENT)) {
  972. /* no hardware, freeze, etc. */
  973. ret = -EINVAL;
  974. goto bail;
  975. }
  976. *swords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDSEND);
  977. *rwords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDRCV);
  978. *spkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTSEND);
  979. *rpkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTRCV);
  980. *xmit_wait = dd->f_portcntr(ppd, QIBPORTCNTR_SENDSTALL);
  981. ret = 0;
  982. bail:
  983. return ret;
  984. }
  985. /**
  986. * qib_get_counters - get various chip counters
  987. * @dd: the qlogic_ib device
  988. * @cntrs: counters are placed here
  989. *
  990. * Return the counters needed by recv_pma_get_portcounters().
  991. */
  992. int qib_get_counters(struct qib_pportdata *ppd,
  993. struct qib_verbs_counters *cntrs)
  994. {
  995. int ret;
  996. if (!(ppd->dd->flags & QIB_PRESENT)) {
  997. /* no hardware, freeze, etc. */
  998. ret = -EINVAL;
  999. goto bail;
  1000. }
  1001. cntrs->symbol_error_counter =
  1002. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBSYMBOLERR);
  1003. cntrs->link_error_recovery_counter =
  1004. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKERRRECOV);
  1005. /*
  1006. * The link downed counter counts when the other side downs the
  1007. * connection. We add in the number of times we downed the link
  1008. * due to local link integrity errors to compensate.
  1009. */
  1010. cntrs->link_downed_counter =
  1011. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKDOWN);
  1012. cntrs->port_rcv_errors =
  1013. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXDROPPKT) +
  1014. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVOVFL) +
  1015. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERR_RLEN) +
  1016. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_INVALIDRLEN) +
  1017. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLINK) +
  1018. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRICRC) +
  1019. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRVCRC) +
  1020. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLPCRC) +
  1021. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_BADFORMAT);
  1022. cntrs->port_rcv_errors +=
  1023. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXLOCALPHYERR);
  1024. cntrs->port_rcv_errors +=
  1025. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXVLERR);
  1026. cntrs->port_rcv_remphys_errors =
  1027. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVEBP);
  1028. cntrs->port_xmit_discards =
  1029. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_UNSUPVL);
  1030. cntrs->port_xmit_data = ppd->dd->f_portcntr(ppd,
  1031. QIBPORTCNTR_WORDSEND);
  1032. cntrs->port_rcv_data = ppd->dd->f_portcntr(ppd,
  1033. QIBPORTCNTR_WORDRCV);
  1034. cntrs->port_xmit_packets = ppd->dd->f_portcntr(ppd,
  1035. QIBPORTCNTR_PKTSEND);
  1036. cntrs->port_rcv_packets = ppd->dd->f_portcntr(ppd,
  1037. QIBPORTCNTR_PKTRCV);
  1038. cntrs->local_link_integrity_errors =
  1039. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_LLI);
  1040. cntrs->excessive_buffer_overrun_errors =
  1041. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_EXCESSBUFOVFL);
  1042. cntrs->vl15_dropped =
  1043. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_VL15PKTDROP);
  1044. ret = 0;
  1045. bail:
  1046. return ret;
  1047. }
  1048. /**
  1049. * qib_ib_piobufavail - callback when a PIO buffer is available
  1050. * @dd: the device pointer
  1051. *
  1052. * This is called from qib_intr() at interrupt level when a PIO buffer is
  1053. * available after qib_verbs_send() returned an error that no buffers were
  1054. * available. Disable the interrupt if there are no more QPs waiting.
  1055. */
  1056. void qib_ib_piobufavail(struct qib_devdata *dd)
  1057. {
  1058. struct qib_ibdev *dev = &dd->verbs_dev;
  1059. struct list_head *list;
  1060. struct rvt_qp *qps[5];
  1061. struct rvt_qp *qp;
  1062. unsigned long flags;
  1063. unsigned i, n;
  1064. struct qib_qp_priv *priv;
  1065. list = &dev->piowait;
  1066. n = 0;
  1067. /*
  1068. * Note: checking that the piowait list is empty and clearing
  1069. * the buffer available interrupt needs to be atomic or we
  1070. * could end up with QPs on the wait list with the interrupt
  1071. * disabled.
  1072. */
  1073. spin_lock_irqsave(&dev->rdi.pending_lock, flags);
  1074. while (!list_empty(list)) {
  1075. if (n == ARRAY_SIZE(qps))
  1076. goto full;
  1077. priv = list_entry(list->next, struct qib_qp_priv, iowait);
  1078. qp = priv->owner;
  1079. list_del_init(&priv->iowait);
  1080. rvt_get_qp(qp);
  1081. qps[n++] = qp;
  1082. }
  1083. dd->f_wantpiobuf_intr(dd, 0);
  1084. full:
  1085. spin_unlock_irqrestore(&dev->rdi.pending_lock, flags);
  1086. for (i = 0; i < n; i++) {
  1087. qp = qps[i];
  1088. spin_lock_irqsave(&qp->s_lock, flags);
  1089. if (qp->s_flags & RVT_S_WAIT_PIO) {
  1090. qp->s_flags &= ~RVT_S_WAIT_PIO;
  1091. qib_schedule_send(qp);
  1092. }
  1093. spin_unlock_irqrestore(&qp->s_lock, flags);
  1094. /* Notify qib_destroy_qp() if it is waiting. */
  1095. rvt_put_qp(qp);
  1096. }
  1097. }
  1098. static int qib_query_port(struct rvt_dev_info *rdi, u8 port_num,
  1099. struct ib_port_attr *props)
  1100. {
  1101. struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
  1102. struct qib_devdata *dd = dd_from_dev(ibdev);
  1103. struct qib_pportdata *ppd = &dd->pport[port_num - 1];
  1104. enum ib_mtu mtu;
  1105. u16 lid = ppd->lid;
  1106. /* props being zeroed by the caller, avoid zeroing it here */
  1107. props->lid = lid ? lid : be16_to_cpu(IB_LID_PERMISSIVE);
  1108. props->lmc = ppd->lmc;
  1109. props->state = dd->f_iblink_state(ppd->lastibcstat);
  1110. props->phys_state = dd->f_ibphys_portstate(ppd->lastibcstat);
  1111. props->gid_tbl_len = QIB_GUIDS_PER_PORT;
  1112. props->active_width = ppd->link_width_active;
  1113. /* See rate_show() */
  1114. props->active_speed = ppd->link_speed_active;
  1115. props->max_vl_num = qib_num_vls(ppd->vls_supported);
  1116. props->max_mtu = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
  1117. switch (ppd->ibmtu) {
  1118. case 4096:
  1119. mtu = IB_MTU_4096;
  1120. break;
  1121. case 2048:
  1122. mtu = IB_MTU_2048;
  1123. break;
  1124. case 1024:
  1125. mtu = IB_MTU_1024;
  1126. break;
  1127. case 512:
  1128. mtu = IB_MTU_512;
  1129. break;
  1130. case 256:
  1131. mtu = IB_MTU_256;
  1132. break;
  1133. default:
  1134. mtu = IB_MTU_2048;
  1135. }
  1136. props->active_mtu = mtu;
  1137. return 0;
  1138. }
  1139. static int qib_modify_device(struct ib_device *device,
  1140. int device_modify_mask,
  1141. struct ib_device_modify *device_modify)
  1142. {
  1143. struct qib_devdata *dd = dd_from_ibdev(device);
  1144. unsigned i;
  1145. int ret;
  1146. if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
  1147. IB_DEVICE_MODIFY_NODE_DESC)) {
  1148. ret = -EOPNOTSUPP;
  1149. goto bail;
  1150. }
  1151. if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
  1152. memcpy(device->node_desc, device_modify->node_desc,
  1153. IB_DEVICE_NODE_DESC_MAX);
  1154. for (i = 0; i < dd->num_pports; i++) {
  1155. struct qib_ibport *ibp = &dd->pport[i].ibport_data;
  1156. qib_node_desc_chg(ibp);
  1157. }
  1158. }
  1159. if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
  1160. ib_qib_sys_image_guid =
  1161. cpu_to_be64(device_modify->sys_image_guid);
  1162. for (i = 0; i < dd->num_pports; i++) {
  1163. struct qib_ibport *ibp = &dd->pport[i].ibport_data;
  1164. qib_sys_guid_chg(ibp);
  1165. }
  1166. }
  1167. ret = 0;
  1168. bail:
  1169. return ret;
  1170. }
  1171. static int qib_shut_down_port(struct rvt_dev_info *rdi, u8 port_num)
  1172. {
  1173. struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
  1174. struct qib_devdata *dd = dd_from_dev(ibdev);
  1175. struct qib_pportdata *ppd = &dd->pport[port_num - 1];
  1176. qib_set_linkstate(ppd, QIB_IB_LINKDOWN);
  1177. return 0;
  1178. }
  1179. static int qib_get_guid_be(struct rvt_dev_info *rdi, struct rvt_ibport *rvp,
  1180. int guid_index, __be64 *guid)
  1181. {
  1182. struct qib_ibport *ibp = container_of(rvp, struct qib_ibport, rvp);
  1183. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1184. if (guid_index == 0)
  1185. *guid = ppd->guid;
  1186. else if (guid_index < QIB_GUIDS_PER_PORT)
  1187. *guid = ibp->guids[guid_index - 1];
  1188. else
  1189. return -EINVAL;
  1190. return 0;
  1191. }
  1192. int qib_check_ah(struct ib_device *ibdev, struct rdma_ah_attr *ah_attr)
  1193. {
  1194. if (rdma_ah_get_sl(ah_attr) > 15)
  1195. return -EINVAL;
  1196. if (rdma_ah_get_dlid(ah_attr) == 0)
  1197. return -EINVAL;
  1198. if (rdma_ah_get_dlid(ah_attr) >=
  1199. be16_to_cpu(IB_MULTICAST_LID_BASE) &&
  1200. rdma_ah_get_dlid(ah_attr) !=
  1201. be16_to_cpu(IB_LID_PERMISSIVE) &&
  1202. !(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH))
  1203. return -EINVAL;
  1204. return 0;
  1205. }
  1206. static void qib_notify_new_ah(struct ib_device *ibdev,
  1207. struct rdma_ah_attr *ah_attr,
  1208. struct rvt_ah *ah)
  1209. {
  1210. struct qib_ibport *ibp;
  1211. struct qib_pportdata *ppd;
  1212. /*
  1213. * Do not trust reading anything from rvt_ah at this point as it is not
  1214. * done being setup. We can however modify things which we need to set.
  1215. */
  1216. ibp = to_iport(ibdev, rdma_ah_get_port_num(ah_attr));
  1217. ppd = ppd_from_ibp(ibp);
  1218. ah->vl = ibp->sl_to_vl[rdma_ah_get_sl(&ah->attr)];
  1219. ah->log_pmtu = ilog2(ppd->ibmtu);
  1220. }
  1221. struct ib_ah *qib_create_qp0_ah(struct qib_ibport *ibp, u16 dlid)
  1222. {
  1223. struct rdma_ah_attr attr;
  1224. struct ib_ah *ah = ERR_PTR(-EINVAL);
  1225. struct rvt_qp *qp0;
  1226. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1227. struct qib_devdata *dd = dd_from_ppd(ppd);
  1228. u8 port_num = ppd->port;
  1229. memset(&attr, 0, sizeof(attr));
  1230. attr.type = rdma_ah_find_type(&dd->verbs_dev.rdi.ibdev, port_num);
  1231. rdma_ah_set_dlid(&attr, dlid);
  1232. rdma_ah_set_port_num(&attr, port_num);
  1233. rcu_read_lock();
  1234. qp0 = rcu_dereference(ibp->rvp.qp[0]);
  1235. if (qp0)
  1236. ah = rdma_create_ah(qp0->ibqp.pd, &attr);
  1237. rcu_read_unlock();
  1238. return ah;
  1239. }
  1240. /**
  1241. * qib_get_npkeys - return the size of the PKEY table for context 0
  1242. * @dd: the qlogic_ib device
  1243. */
  1244. unsigned qib_get_npkeys(struct qib_devdata *dd)
  1245. {
  1246. return ARRAY_SIZE(dd->rcd[0]->pkeys);
  1247. }
  1248. /*
  1249. * Return the indexed PKEY from the port PKEY table.
  1250. * No need to validate rcd[ctxt]; the port is setup if we are here.
  1251. */
  1252. unsigned qib_get_pkey(struct qib_ibport *ibp, unsigned index)
  1253. {
  1254. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1255. struct qib_devdata *dd = ppd->dd;
  1256. unsigned ctxt = ppd->hw_pidx;
  1257. unsigned ret;
  1258. /* dd->rcd null if mini_init or some init failures */
  1259. if (!dd->rcd || index >= ARRAY_SIZE(dd->rcd[ctxt]->pkeys))
  1260. ret = 0;
  1261. else
  1262. ret = dd->rcd[ctxt]->pkeys[index];
  1263. return ret;
  1264. }
  1265. static void init_ibport(struct qib_pportdata *ppd)
  1266. {
  1267. struct qib_verbs_counters cntrs;
  1268. struct qib_ibport *ibp = &ppd->ibport_data;
  1269. spin_lock_init(&ibp->rvp.lock);
  1270. /* Set the prefix to the default value (see ch. 4.1.1) */
  1271. ibp->rvp.gid_prefix = IB_DEFAULT_GID_PREFIX;
  1272. ibp->rvp.sm_lid = be16_to_cpu(IB_LID_PERMISSIVE);
  1273. ibp->rvp.port_cap_flags = IB_PORT_SYS_IMAGE_GUID_SUP |
  1274. IB_PORT_CLIENT_REG_SUP | IB_PORT_SL_MAP_SUP |
  1275. IB_PORT_TRAP_SUP | IB_PORT_AUTO_MIGR_SUP |
  1276. IB_PORT_DR_NOTICE_SUP | IB_PORT_CAP_MASK_NOTICE_SUP |
  1277. IB_PORT_OTHER_LOCAL_CHANGES_SUP;
  1278. if (ppd->dd->flags & QIB_HAS_LINK_LATENCY)
  1279. ibp->rvp.port_cap_flags |= IB_PORT_LINK_LATENCY_SUP;
  1280. ibp->rvp.pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
  1281. ibp->rvp.pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
  1282. ibp->rvp.pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
  1283. ibp->rvp.pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
  1284. ibp->rvp.pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
  1285. /* Snapshot current HW counters to "clear" them. */
  1286. qib_get_counters(ppd, &cntrs);
  1287. ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
  1288. ibp->z_link_error_recovery_counter =
  1289. cntrs.link_error_recovery_counter;
  1290. ibp->z_link_downed_counter = cntrs.link_downed_counter;
  1291. ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
  1292. ibp->z_port_rcv_remphys_errors = cntrs.port_rcv_remphys_errors;
  1293. ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
  1294. ibp->z_port_xmit_data = cntrs.port_xmit_data;
  1295. ibp->z_port_rcv_data = cntrs.port_rcv_data;
  1296. ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
  1297. ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
  1298. ibp->z_local_link_integrity_errors =
  1299. cntrs.local_link_integrity_errors;
  1300. ibp->z_excessive_buffer_overrun_errors =
  1301. cntrs.excessive_buffer_overrun_errors;
  1302. ibp->z_vl15_dropped = cntrs.vl15_dropped;
  1303. RCU_INIT_POINTER(ibp->rvp.qp[0], NULL);
  1304. RCU_INIT_POINTER(ibp->rvp.qp[1], NULL);
  1305. }
  1306. /**
  1307. * qib_fill_device_attr - Fill in rvt dev info device attributes.
  1308. * @dd: the device data structure
  1309. */
  1310. static void qib_fill_device_attr(struct qib_devdata *dd)
  1311. {
  1312. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  1313. memset(&rdi->dparms.props, 0, sizeof(rdi->dparms.props));
  1314. rdi->dparms.props.max_pd = ib_qib_max_pds;
  1315. rdi->dparms.props.max_ah = ib_qib_max_ahs;
  1316. rdi->dparms.props.device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
  1317. IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
  1318. IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
  1319. IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE;
  1320. rdi->dparms.props.page_size_cap = PAGE_SIZE;
  1321. rdi->dparms.props.vendor_id =
  1322. QIB_SRC_OUI_1 << 16 | QIB_SRC_OUI_2 << 8 | QIB_SRC_OUI_3;
  1323. rdi->dparms.props.vendor_part_id = dd->deviceid;
  1324. rdi->dparms.props.hw_ver = dd->minrev;
  1325. rdi->dparms.props.sys_image_guid = ib_qib_sys_image_guid;
  1326. rdi->dparms.props.max_mr_size = ~0ULL;
  1327. rdi->dparms.props.max_qp = ib_qib_max_qps;
  1328. rdi->dparms.props.max_qp_wr = ib_qib_max_qp_wrs;
  1329. rdi->dparms.props.max_sge = ib_qib_max_sges;
  1330. rdi->dparms.props.max_sge_rd = ib_qib_max_sges;
  1331. rdi->dparms.props.max_cq = ib_qib_max_cqs;
  1332. rdi->dparms.props.max_cqe = ib_qib_max_cqes;
  1333. rdi->dparms.props.max_ah = ib_qib_max_ahs;
  1334. rdi->dparms.props.max_mr = rdi->lkey_table.max;
  1335. rdi->dparms.props.max_fmr = rdi->lkey_table.max;
  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. setup_timer(&dev->mem_timer, mem_timer, (unsigned long)dev);
  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. strlcpy(ibdev->name, "qib%d", IB_DEVICE_NAME_MAX);
  1403. ibdev->owner = THIS_MODULE;
  1404. ibdev->node_guid = ppd->guid;
  1405. ibdev->phys_port_cnt = dd->num_pports;
  1406. ibdev->dev.parent = &dd->pcidev->dev;
  1407. ibdev->modify_device = qib_modify_device;
  1408. ibdev->process_mad = qib_process_mad;
  1409. snprintf(ibdev->node_desc, sizeof(ibdev->node_desc),
  1410. "Intel Infiniband HCA %s", init_utsname()->nodename);
  1411. /*
  1412. * Fill in rvt info object.
  1413. */
  1414. dd->verbs_dev.rdi.driver_f.port_callback = qib_create_port_files;
  1415. dd->verbs_dev.rdi.driver_f.get_card_name = qib_get_card_name;
  1416. dd->verbs_dev.rdi.driver_f.get_pci_dev = qib_get_pci_dev;
  1417. dd->verbs_dev.rdi.driver_f.check_ah = qib_check_ah;
  1418. dd->verbs_dev.rdi.driver_f.check_send_wqe = qib_check_send_wqe;
  1419. dd->verbs_dev.rdi.driver_f.notify_new_ah = qib_notify_new_ah;
  1420. dd->verbs_dev.rdi.driver_f.alloc_qpn = qib_alloc_qpn;
  1421. dd->verbs_dev.rdi.driver_f.qp_priv_alloc = qib_qp_priv_alloc;
  1422. dd->verbs_dev.rdi.driver_f.qp_priv_free = qib_qp_priv_free;
  1423. dd->verbs_dev.rdi.driver_f.free_all_qps = qib_free_all_qps;
  1424. dd->verbs_dev.rdi.driver_f.notify_qp_reset = qib_notify_qp_reset;
  1425. dd->verbs_dev.rdi.driver_f.do_send = qib_do_send;
  1426. dd->verbs_dev.rdi.driver_f.schedule_send = qib_schedule_send;
  1427. dd->verbs_dev.rdi.driver_f.quiesce_qp = qib_quiesce_qp;
  1428. dd->verbs_dev.rdi.driver_f.stop_send_queue = qib_stop_send_queue;
  1429. dd->verbs_dev.rdi.driver_f.flush_qp_waiters = qib_flush_qp_waiters;
  1430. dd->verbs_dev.rdi.driver_f.notify_error_qp = qib_notify_error_qp;
  1431. dd->verbs_dev.rdi.driver_f.notify_restart_rc = qib_restart_rc;
  1432. dd->verbs_dev.rdi.driver_f.mtu_to_path_mtu = qib_mtu_to_path_mtu;
  1433. dd->verbs_dev.rdi.driver_f.mtu_from_qp = qib_mtu_from_qp;
  1434. dd->verbs_dev.rdi.driver_f.get_pmtu_from_attr = qib_get_pmtu_from_attr;
  1435. dd->verbs_dev.rdi.driver_f.schedule_send_no_lock = _qib_schedule_send;
  1436. dd->verbs_dev.rdi.driver_f.query_port_state = qib_query_port;
  1437. dd->verbs_dev.rdi.driver_f.shut_down_port = qib_shut_down_port;
  1438. dd->verbs_dev.rdi.driver_f.cap_mask_chg = qib_cap_mask_chg;
  1439. dd->verbs_dev.rdi.driver_f.notify_create_mad_agent =
  1440. qib_notify_create_mad_agent;
  1441. dd->verbs_dev.rdi.driver_f.notify_free_mad_agent =
  1442. qib_notify_free_mad_agent;
  1443. dd->verbs_dev.rdi.dparms.max_rdma_atomic = QIB_MAX_RDMA_ATOMIC;
  1444. dd->verbs_dev.rdi.driver_f.get_guid_be = qib_get_guid_be;
  1445. dd->verbs_dev.rdi.dparms.lkey_table_size = qib_lkey_table_size;
  1446. dd->verbs_dev.rdi.dparms.qp_table_size = ib_qib_qp_table_size;
  1447. dd->verbs_dev.rdi.dparms.qpn_start = 1;
  1448. dd->verbs_dev.rdi.dparms.qpn_res_start = QIB_KD_QP;
  1449. dd->verbs_dev.rdi.dparms.qpn_res_end = QIB_KD_QP; /* Reserve one QP */
  1450. dd->verbs_dev.rdi.dparms.qpn_inc = 1;
  1451. dd->verbs_dev.rdi.dparms.qos_shift = 1;
  1452. dd->verbs_dev.rdi.dparms.psn_mask = QIB_PSN_MASK;
  1453. dd->verbs_dev.rdi.dparms.psn_shift = QIB_PSN_SHIFT;
  1454. dd->verbs_dev.rdi.dparms.psn_modify_mask = QIB_PSN_MASK;
  1455. dd->verbs_dev.rdi.dparms.nports = dd->num_pports;
  1456. dd->verbs_dev.rdi.dparms.npkeys = qib_get_npkeys(dd);
  1457. dd->verbs_dev.rdi.dparms.node = dd->assigned_node_id;
  1458. dd->verbs_dev.rdi.dparms.core_cap_flags = RDMA_CORE_PORT_IBA_IB;
  1459. dd->verbs_dev.rdi.dparms.max_mad_size = IB_MGMT_MAD_SIZE;
  1460. snprintf(dd->verbs_dev.rdi.dparms.cq_name,
  1461. sizeof(dd->verbs_dev.rdi.dparms.cq_name),
  1462. "qib_cq%d", dd->unit);
  1463. qib_fill_device_attr(dd);
  1464. ppd = dd->pport;
  1465. for (i = 0; i < dd->num_pports; i++, ppd++) {
  1466. ctxt = ppd->hw_pidx;
  1467. rvt_init_port(&dd->verbs_dev.rdi,
  1468. &ppd->ibport_data.rvp,
  1469. i,
  1470. dd->rcd[ctxt]->pkeys);
  1471. }
  1472. ret = rvt_register_device(&dd->verbs_dev.rdi);
  1473. if (ret)
  1474. goto err_tx;
  1475. ret = qib_verbs_register_sysfs(dd);
  1476. if (ret)
  1477. goto err_class;
  1478. return ret;
  1479. err_class:
  1480. rvt_unregister_device(&dd->verbs_dev.rdi);
  1481. err_tx:
  1482. while (!list_empty(&dev->txreq_free)) {
  1483. struct list_head *l = dev->txreq_free.next;
  1484. struct qib_verbs_txreq *tx;
  1485. list_del(l);
  1486. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  1487. kfree(tx);
  1488. }
  1489. if (ppd->sdma_descq_cnt)
  1490. dma_free_coherent(&dd->pcidev->dev,
  1491. ppd->sdma_descq_cnt *
  1492. sizeof(struct qib_pio_header),
  1493. dev->pio_hdrs, dev->pio_hdrs_phys);
  1494. err_hdrs:
  1495. qib_dev_err(dd, "cannot register verbs: %d!\n", -ret);
  1496. return ret;
  1497. }
  1498. void qib_unregister_ib_device(struct qib_devdata *dd)
  1499. {
  1500. struct qib_ibdev *dev = &dd->verbs_dev;
  1501. qib_verbs_unregister_sysfs(dd);
  1502. rvt_unregister_device(&dd->verbs_dev.rdi);
  1503. if (!list_empty(&dev->piowait))
  1504. qib_dev_err(dd, "piowait list not empty!\n");
  1505. if (!list_empty(&dev->dmawait))
  1506. qib_dev_err(dd, "dmawait list not empty!\n");
  1507. if (!list_empty(&dev->txwait))
  1508. qib_dev_err(dd, "txwait list not empty!\n");
  1509. if (!list_empty(&dev->memwait))
  1510. qib_dev_err(dd, "memwait list not empty!\n");
  1511. del_timer_sync(&dev->mem_timer);
  1512. while (!list_empty(&dev->txreq_free)) {
  1513. struct list_head *l = dev->txreq_free.next;
  1514. struct qib_verbs_txreq *tx;
  1515. list_del(l);
  1516. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  1517. kfree(tx);
  1518. }
  1519. if (dd->pport->sdma_descq_cnt)
  1520. dma_free_coherent(&dd->pcidev->dev,
  1521. dd->pport->sdma_descq_cnt *
  1522. sizeof(struct qib_pio_header),
  1523. dev->pio_hdrs, dev->pio_hdrs_phys);
  1524. }
  1525. /**
  1526. * _qib_schedule_send - schedule progress
  1527. * @qp - the qp
  1528. *
  1529. * This schedules progress w/o regard to the s_flags.
  1530. *
  1531. * It is only used in post send, which doesn't hold
  1532. * the s_lock.
  1533. */
  1534. void _qib_schedule_send(struct rvt_qp *qp)
  1535. {
  1536. struct qib_ibport *ibp =
  1537. to_iport(qp->ibqp.device, qp->port_num);
  1538. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1539. struct qib_qp_priv *priv = qp->priv;
  1540. queue_work(ppd->qib_wq, &priv->s_work);
  1541. }
  1542. /**
  1543. * qib_schedule_send - schedule progress
  1544. * @qp - the qp
  1545. *
  1546. * This schedules qp progress. The s_lock
  1547. * should be held.
  1548. */
  1549. void qib_schedule_send(struct rvt_qp *qp)
  1550. {
  1551. if (qib_send_ok(qp))
  1552. _qib_schedule_send(qp);
  1553. }