qib_verbs.c 47 KB

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