ipath_verbs.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  1. /*
  2. * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <rdma/ib_mad.h>
  34. #include <rdma/ib_user_verbs.h>
  35. #include <linux/io.h>
  36. #include <linux/utsname.h>
  37. #include "ipath_kernel.h"
  38. #include "ipath_verbs.h"
  39. #include "ipath_common.h"
  40. static unsigned int ib_ipath_qp_table_size = 251;
  41. module_param_named(qp_table_size, ib_ipath_qp_table_size, uint, S_IRUGO);
  42. MODULE_PARM_DESC(qp_table_size, "QP table size");
  43. unsigned int ib_ipath_lkey_table_size = 12;
  44. module_param_named(lkey_table_size, ib_ipath_lkey_table_size, uint,
  45. S_IRUGO);
  46. MODULE_PARM_DESC(lkey_table_size,
  47. "LKEY table size in bits (2^n, 1 <= n <= 23)");
  48. static unsigned int ib_ipath_max_pds = 0xFFFF;
  49. module_param_named(max_pds, ib_ipath_max_pds, uint, S_IWUSR | S_IRUGO);
  50. MODULE_PARM_DESC(max_pds,
  51. "Maximum number of protection domains to support");
  52. static unsigned int ib_ipath_max_ahs = 0xFFFF;
  53. module_param_named(max_ahs, ib_ipath_max_ahs, uint, S_IWUSR | S_IRUGO);
  54. MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
  55. unsigned int ib_ipath_max_cqes = 0x2FFFF;
  56. module_param_named(max_cqes, ib_ipath_max_cqes, uint, S_IWUSR | S_IRUGO);
  57. MODULE_PARM_DESC(max_cqes,
  58. "Maximum number of completion queue entries to support");
  59. unsigned int ib_ipath_max_cqs = 0x1FFFF;
  60. module_param_named(max_cqs, ib_ipath_max_cqs, uint, S_IWUSR | S_IRUGO);
  61. MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
  62. unsigned int ib_ipath_max_qp_wrs = 0x3FFF;
  63. module_param_named(max_qp_wrs, ib_ipath_max_qp_wrs, uint,
  64. S_IWUSR | S_IRUGO);
  65. MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
  66. unsigned int ib_ipath_max_qps = 16384;
  67. module_param_named(max_qps, ib_ipath_max_qps, uint, S_IWUSR | S_IRUGO);
  68. MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
  69. unsigned int ib_ipath_max_sges = 0x60;
  70. module_param_named(max_sges, ib_ipath_max_sges, uint, S_IWUSR | S_IRUGO);
  71. MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
  72. unsigned int ib_ipath_max_mcast_grps = 16384;
  73. module_param_named(max_mcast_grps, ib_ipath_max_mcast_grps, uint,
  74. S_IWUSR | S_IRUGO);
  75. MODULE_PARM_DESC(max_mcast_grps,
  76. "Maximum number of multicast groups to support");
  77. unsigned int ib_ipath_max_mcast_qp_attached = 16;
  78. module_param_named(max_mcast_qp_attached, ib_ipath_max_mcast_qp_attached,
  79. uint, S_IWUSR | S_IRUGO);
  80. MODULE_PARM_DESC(max_mcast_qp_attached,
  81. "Maximum number of attached QPs to support");
  82. unsigned int ib_ipath_max_srqs = 1024;
  83. module_param_named(max_srqs, ib_ipath_max_srqs, uint, S_IWUSR | S_IRUGO);
  84. MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
  85. unsigned int ib_ipath_max_srq_sges = 128;
  86. module_param_named(max_srq_sges, ib_ipath_max_srq_sges,
  87. uint, S_IWUSR | S_IRUGO);
  88. MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
  89. unsigned int ib_ipath_max_srq_wrs = 0x1FFFF;
  90. module_param_named(max_srq_wrs, ib_ipath_max_srq_wrs,
  91. uint, S_IWUSR | S_IRUGO);
  92. MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
  93. static unsigned int ib_ipath_disable_sma;
  94. module_param_named(disable_sma, ib_ipath_disable_sma, uint, S_IWUSR | S_IRUGO);
  95. MODULE_PARM_DESC(ib_ipath_disable_sma, "Disable the SMA");
  96. const int ib_ipath_state_ops[IB_QPS_ERR + 1] = {
  97. [IB_QPS_RESET] = 0,
  98. [IB_QPS_INIT] = IPATH_POST_RECV_OK,
  99. [IB_QPS_RTR] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
  100. [IB_QPS_RTS] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
  101. IPATH_POST_SEND_OK | IPATH_PROCESS_SEND_OK,
  102. [IB_QPS_SQD] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK |
  103. IPATH_POST_SEND_OK,
  104. [IB_QPS_SQE] = IPATH_POST_RECV_OK | IPATH_PROCESS_RECV_OK,
  105. [IB_QPS_ERR] = 0,
  106. };
  107. struct ipath_ucontext {
  108. struct ib_ucontext ibucontext;
  109. };
  110. static inline struct ipath_ucontext *to_iucontext(struct ib_ucontext
  111. *ibucontext)
  112. {
  113. return container_of(ibucontext, struct ipath_ucontext, ibucontext);
  114. }
  115. /*
  116. * Translate ib_wr_opcode into ib_wc_opcode.
  117. */
  118. const enum ib_wc_opcode ib_ipath_wc_opcode[] = {
  119. [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
  120. [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
  121. [IB_WR_SEND] = IB_WC_SEND,
  122. [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
  123. [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
  124. [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
  125. [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD
  126. };
  127. /*
  128. * System image GUID.
  129. */
  130. static __be64 sys_image_guid;
  131. /**
  132. * ipath_copy_sge - copy data to SGE memory
  133. * @ss: the SGE state
  134. * @data: the data to copy
  135. * @length: the length of the data
  136. */
  137. void ipath_copy_sge(struct ipath_sge_state *ss, void *data, u32 length)
  138. {
  139. struct ipath_sge *sge = &ss->sge;
  140. while (length) {
  141. u32 len = sge->length;
  142. if (len > length)
  143. len = length;
  144. if (len > sge->sge_length)
  145. len = sge->sge_length;
  146. BUG_ON(len == 0);
  147. memcpy(sge->vaddr, data, len);
  148. sge->vaddr += len;
  149. sge->length -= len;
  150. sge->sge_length -= len;
  151. if (sge->sge_length == 0) {
  152. if (--ss->num_sge)
  153. *sge = *ss->sg_list++;
  154. } else if (sge->length == 0 && sge->mr != NULL) {
  155. if (++sge->n >= IPATH_SEGSZ) {
  156. if (++sge->m >= sge->mr->mapsz)
  157. break;
  158. sge->n = 0;
  159. }
  160. sge->vaddr =
  161. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  162. sge->length =
  163. sge->mr->map[sge->m]->segs[sge->n].length;
  164. }
  165. data += len;
  166. length -= len;
  167. }
  168. }
  169. /**
  170. * ipath_skip_sge - skip over SGE memory - XXX almost dup of prev func
  171. * @ss: the SGE state
  172. * @length: the number of bytes to skip
  173. */
  174. void ipath_skip_sge(struct ipath_sge_state *ss, u32 length)
  175. {
  176. struct ipath_sge *sge = &ss->sge;
  177. while (length) {
  178. u32 len = sge->length;
  179. if (len > length)
  180. len = length;
  181. if (len > sge->sge_length)
  182. len = sge->sge_length;
  183. BUG_ON(len == 0);
  184. sge->vaddr += len;
  185. sge->length -= len;
  186. sge->sge_length -= len;
  187. if (sge->sge_length == 0) {
  188. if (--ss->num_sge)
  189. *sge = *ss->sg_list++;
  190. } else if (sge->length == 0 && sge->mr != NULL) {
  191. if (++sge->n >= IPATH_SEGSZ) {
  192. if (++sge->m >= sge->mr->mapsz)
  193. break;
  194. sge->n = 0;
  195. }
  196. sge->vaddr =
  197. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  198. sge->length =
  199. sge->mr->map[sge->m]->segs[sge->n].length;
  200. }
  201. length -= len;
  202. }
  203. }
  204. static void ipath_flush_wqe(struct ipath_qp *qp, struct ib_send_wr *wr)
  205. {
  206. struct ib_wc wc;
  207. memset(&wc, 0, sizeof(wc));
  208. wc.wr_id = wr->wr_id;
  209. wc.status = IB_WC_WR_FLUSH_ERR;
  210. wc.opcode = ib_ipath_wc_opcode[wr->opcode];
  211. wc.qp = &qp->ibqp;
  212. ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 1);
  213. }
  214. /**
  215. * ipath_post_one_send - post one RC, UC, or UD send work request
  216. * @qp: the QP to post on
  217. * @wr: the work request to send
  218. */
  219. static int ipath_post_one_send(struct ipath_qp *qp, struct ib_send_wr *wr)
  220. {
  221. struct ipath_swqe *wqe;
  222. u32 next;
  223. int i;
  224. int j;
  225. int acc;
  226. int ret;
  227. unsigned long flags;
  228. spin_lock_irqsave(&qp->s_lock, flags);
  229. /* Check that state is OK to post send. */
  230. if (unlikely(!(ib_ipath_state_ops[qp->state] & IPATH_POST_SEND_OK))) {
  231. if (qp->state != IB_QPS_SQE && qp->state != IB_QPS_ERR)
  232. goto bail_inval;
  233. /* C10-96 says generate a flushed completion entry. */
  234. ipath_flush_wqe(qp, wr);
  235. ret = 0;
  236. goto bail;
  237. }
  238. /* IB spec says that num_sge == 0 is OK. */
  239. if (wr->num_sge > qp->s_max_sge)
  240. goto bail_inval;
  241. /*
  242. * Don't allow RDMA reads or atomic operations on UC or
  243. * undefined operations.
  244. * Make sure buffer is large enough to hold the result for atomics.
  245. */
  246. if (qp->ibqp.qp_type == IB_QPT_UC) {
  247. if ((unsigned) wr->opcode >= IB_WR_RDMA_READ)
  248. goto bail_inval;
  249. } else if (qp->ibqp.qp_type == IB_QPT_UD) {
  250. /* Check UD opcode */
  251. if (wr->opcode != IB_WR_SEND &&
  252. wr->opcode != IB_WR_SEND_WITH_IMM)
  253. goto bail_inval;
  254. /* Check UD destination address PD */
  255. if (qp->ibqp.pd != wr->wr.ud.ah->pd)
  256. goto bail_inval;
  257. } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD)
  258. goto bail_inval;
  259. else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
  260. (wr->num_sge == 0 ||
  261. wr->sg_list[0].length < sizeof(u64) ||
  262. wr->sg_list[0].addr & (sizeof(u64) - 1)))
  263. goto bail_inval;
  264. else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic)
  265. goto bail_inval;
  266. next = qp->s_head + 1;
  267. if (next >= qp->s_size)
  268. next = 0;
  269. if (next == qp->s_last) {
  270. ret = -ENOMEM;
  271. goto bail;
  272. }
  273. wqe = get_swqe_ptr(qp, qp->s_head);
  274. wqe->wr = *wr;
  275. wqe->ssn = qp->s_ssn++;
  276. wqe->length = 0;
  277. if (wr->num_sge) {
  278. acc = wr->opcode >= IB_WR_RDMA_READ ?
  279. IB_ACCESS_LOCAL_WRITE : 0;
  280. for (i = 0, j = 0; i < wr->num_sge; i++) {
  281. u32 length = wr->sg_list[i].length;
  282. int ok;
  283. if (length == 0)
  284. continue;
  285. ok = ipath_lkey_ok(qp, &wqe->sg_list[j],
  286. &wr->sg_list[i], acc);
  287. if (!ok)
  288. goto bail_inval;
  289. wqe->length += length;
  290. j++;
  291. }
  292. wqe->wr.num_sge = j;
  293. }
  294. if (qp->ibqp.qp_type == IB_QPT_UC ||
  295. qp->ibqp.qp_type == IB_QPT_RC) {
  296. if (wqe->length > 0x80000000U)
  297. goto bail_inval;
  298. } else if (wqe->length > to_idev(qp->ibqp.device)->dd->ipath_ibmtu)
  299. goto bail_inval;
  300. qp->s_head = next;
  301. ret = 0;
  302. goto bail;
  303. bail_inval:
  304. ret = -EINVAL;
  305. bail:
  306. spin_unlock_irqrestore(&qp->s_lock, flags);
  307. return ret;
  308. }
  309. /**
  310. * ipath_post_send - post a send on a QP
  311. * @ibqp: the QP to post the send on
  312. * @wr: the list of work requests to post
  313. * @bad_wr: the first bad WR is put here
  314. *
  315. * This may be called from interrupt context.
  316. */
  317. static int ipath_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  318. struct ib_send_wr **bad_wr)
  319. {
  320. struct ipath_qp *qp = to_iqp(ibqp);
  321. int err = 0;
  322. for (; wr; wr = wr->next) {
  323. err = ipath_post_one_send(qp, wr);
  324. if (err) {
  325. *bad_wr = wr;
  326. goto bail;
  327. }
  328. }
  329. /* Try to do the send work in the caller's context. */
  330. ipath_do_send((unsigned long) qp);
  331. bail:
  332. return err;
  333. }
  334. /**
  335. * ipath_post_receive - post a receive on a QP
  336. * @ibqp: the QP to post the receive on
  337. * @wr: the WR to post
  338. * @bad_wr: the first bad WR is put here
  339. *
  340. * This may be called from interrupt context.
  341. */
  342. static int ipath_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  343. struct ib_recv_wr **bad_wr)
  344. {
  345. struct ipath_qp *qp = to_iqp(ibqp);
  346. struct ipath_rwq *wq = qp->r_rq.wq;
  347. unsigned long flags;
  348. int ret;
  349. /* Check that state is OK to post receive. */
  350. if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_RECV_OK) || !wq) {
  351. *bad_wr = wr;
  352. ret = -EINVAL;
  353. goto bail;
  354. }
  355. for (; wr; wr = wr->next) {
  356. struct ipath_rwqe *wqe;
  357. u32 next;
  358. int i;
  359. if ((unsigned) wr->num_sge > qp->r_rq.max_sge) {
  360. *bad_wr = wr;
  361. ret = -EINVAL;
  362. goto bail;
  363. }
  364. spin_lock_irqsave(&qp->r_rq.lock, flags);
  365. next = wq->head + 1;
  366. if (next >= qp->r_rq.size)
  367. next = 0;
  368. if (next == wq->tail) {
  369. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  370. *bad_wr = wr;
  371. ret = -ENOMEM;
  372. goto bail;
  373. }
  374. wqe = get_rwqe_ptr(&qp->r_rq, wq->head);
  375. wqe->wr_id = wr->wr_id;
  376. wqe->num_sge = wr->num_sge;
  377. for (i = 0; i < wr->num_sge; i++)
  378. wqe->sg_list[i] = wr->sg_list[i];
  379. /* Make sure queue entry is written before the head index. */
  380. smp_wmb();
  381. wq->head = next;
  382. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  383. }
  384. ret = 0;
  385. bail:
  386. return ret;
  387. }
  388. /**
  389. * ipath_qp_rcv - processing an incoming packet on a QP
  390. * @dev: the device the packet came on
  391. * @hdr: the packet header
  392. * @has_grh: true if the packet has a GRH
  393. * @data: the packet data
  394. * @tlen: the packet length
  395. * @qp: the QP the packet came on
  396. *
  397. * This is called from ipath_ib_rcv() to process an incoming packet
  398. * for the given QP.
  399. * Called at interrupt level.
  400. */
  401. static void ipath_qp_rcv(struct ipath_ibdev *dev,
  402. struct ipath_ib_header *hdr, int has_grh,
  403. void *data, u32 tlen, struct ipath_qp *qp)
  404. {
  405. /* Check for valid receive state. */
  406. if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {
  407. dev->n_pkt_drops++;
  408. return;
  409. }
  410. switch (qp->ibqp.qp_type) {
  411. case IB_QPT_SMI:
  412. case IB_QPT_GSI:
  413. if (ib_ipath_disable_sma)
  414. break;
  415. /* FALLTHROUGH */
  416. case IB_QPT_UD:
  417. ipath_ud_rcv(dev, hdr, has_grh, data, tlen, qp);
  418. break;
  419. case IB_QPT_RC:
  420. ipath_rc_rcv(dev, hdr, has_grh, data, tlen, qp);
  421. break;
  422. case IB_QPT_UC:
  423. ipath_uc_rcv(dev, hdr, has_grh, data, tlen, qp);
  424. break;
  425. default:
  426. break;
  427. }
  428. }
  429. /**
  430. * ipath_ib_rcv - process an incoming packet
  431. * @arg: the device pointer
  432. * @rhdr: the header of the packet
  433. * @data: the packet data
  434. * @tlen: the packet length
  435. *
  436. * This is called from ipath_kreceive() to process an incoming packet at
  437. * interrupt level. Tlen is the length of the header + data + CRC in bytes.
  438. */
  439. void ipath_ib_rcv(struct ipath_ibdev *dev, void *rhdr, void *data,
  440. u32 tlen)
  441. {
  442. struct ipath_ib_header *hdr = rhdr;
  443. struct ipath_other_headers *ohdr;
  444. struct ipath_qp *qp;
  445. u32 qp_num;
  446. int lnh;
  447. u8 opcode;
  448. u16 lid;
  449. if (unlikely(dev == NULL))
  450. goto bail;
  451. if (unlikely(tlen < 24)) { /* LRH+BTH+CRC */
  452. dev->rcv_errors++;
  453. goto bail;
  454. }
  455. /* Check for a valid destination LID (see ch. 7.11.1). */
  456. lid = be16_to_cpu(hdr->lrh[1]);
  457. if (lid < IPATH_MULTICAST_LID_BASE) {
  458. lid &= ~((1 << dev->dd->ipath_lmc) - 1);
  459. if (unlikely(lid != dev->dd->ipath_lid)) {
  460. dev->rcv_errors++;
  461. goto bail;
  462. }
  463. }
  464. /* Check for GRH */
  465. lnh = be16_to_cpu(hdr->lrh[0]) & 3;
  466. if (lnh == IPATH_LRH_BTH)
  467. ohdr = &hdr->u.oth;
  468. else if (lnh == IPATH_LRH_GRH)
  469. ohdr = &hdr->u.l.oth;
  470. else {
  471. dev->rcv_errors++;
  472. goto bail;
  473. }
  474. opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
  475. dev->opstats[opcode].n_bytes += tlen;
  476. dev->opstats[opcode].n_packets++;
  477. /* Get the destination QP number. */
  478. qp_num = be32_to_cpu(ohdr->bth[1]) & IPATH_QPN_MASK;
  479. if (qp_num == IPATH_MULTICAST_QPN) {
  480. struct ipath_mcast *mcast;
  481. struct ipath_mcast_qp *p;
  482. if (lnh != IPATH_LRH_GRH) {
  483. dev->n_pkt_drops++;
  484. goto bail;
  485. }
  486. mcast = ipath_mcast_find(&hdr->u.l.grh.dgid);
  487. if (mcast == NULL) {
  488. dev->n_pkt_drops++;
  489. goto bail;
  490. }
  491. dev->n_multicast_rcv++;
  492. list_for_each_entry_rcu(p, &mcast->qp_list, list)
  493. ipath_qp_rcv(dev, hdr, 1, data, tlen, p->qp);
  494. /*
  495. * Notify ipath_multicast_detach() if it is waiting for us
  496. * to finish.
  497. */
  498. if (atomic_dec_return(&mcast->refcount) <= 1)
  499. wake_up(&mcast->wait);
  500. } else {
  501. qp = ipath_lookup_qpn(&dev->qp_table, qp_num);
  502. if (qp) {
  503. dev->n_unicast_rcv++;
  504. ipath_qp_rcv(dev, hdr, lnh == IPATH_LRH_GRH, data,
  505. tlen, qp);
  506. /*
  507. * Notify ipath_destroy_qp() if it is waiting
  508. * for us to finish.
  509. */
  510. if (atomic_dec_and_test(&qp->refcount))
  511. wake_up(&qp->wait);
  512. } else
  513. dev->n_pkt_drops++;
  514. }
  515. bail:;
  516. }
  517. /**
  518. * ipath_ib_timer - verbs timer
  519. * @arg: the device pointer
  520. *
  521. * This is called from ipath_do_rcv_timer() at interrupt level to check for
  522. * QPs which need retransmits and to collect performance numbers.
  523. */
  524. static void ipath_ib_timer(struct ipath_ibdev *dev)
  525. {
  526. struct ipath_qp *resend = NULL;
  527. struct list_head *last;
  528. struct ipath_qp *qp;
  529. unsigned long flags;
  530. if (dev == NULL)
  531. return;
  532. spin_lock_irqsave(&dev->pending_lock, flags);
  533. /* Start filling the next pending queue. */
  534. if (++dev->pending_index >= ARRAY_SIZE(dev->pending))
  535. dev->pending_index = 0;
  536. /* Save any requests still in the new queue, they have timed out. */
  537. last = &dev->pending[dev->pending_index];
  538. while (!list_empty(last)) {
  539. qp = list_entry(last->next, struct ipath_qp, timerwait);
  540. list_del_init(&qp->timerwait);
  541. qp->timer_next = resend;
  542. resend = qp;
  543. atomic_inc(&qp->refcount);
  544. }
  545. last = &dev->rnrwait;
  546. if (!list_empty(last)) {
  547. qp = list_entry(last->next, struct ipath_qp, timerwait);
  548. if (--qp->s_rnr_timeout == 0) {
  549. do {
  550. list_del_init(&qp->timerwait);
  551. tasklet_hi_schedule(&qp->s_task);
  552. if (list_empty(last))
  553. break;
  554. qp = list_entry(last->next, struct ipath_qp,
  555. timerwait);
  556. } while (qp->s_rnr_timeout == 0);
  557. }
  558. }
  559. /*
  560. * We should only be in the started state if pma_sample_start != 0
  561. */
  562. if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_STARTED &&
  563. --dev->pma_sample_start == 0) {
  564. dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_RUNNING;
  565. ipath_snapshot_counters(dev->dd, &dev->ipath_sword,
  566. &dev->ipath_rword,
  567. &dev->ipath_spkts,
  568. &dev->ipath_rpkts,
  569. &dev->ipath_xmit_wait);
  570. }
  571. if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_RUNNING) {
  572. if (dev->pma_sample_interval == 0) {
  573. u64 ta, tb, tc, td, te;
  574. dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_DONE;
  575. ipath_snapshot_counters(dev->dd, &ta, &tb,
  576. &tc, &td, &te);
  577. dev->ipath_sword = ta - dev->ipath_sword;
  578. dev->ipath_rword = tb - dev->ipath_rword;
  579. dev->ipath_spkts = tc - dev->ipath_spkts;
  580. dev->ipath_rpkts = td - dev->ipath_rpkts;
  581. dev->ipath_xmit_wait = te - dev->ipath_xmit_wait;
  582. }
  583. else
  584. dev->pma_sample_interval--;
  585. }
  586. spin_unlock_irqrestore(&dev->pending_lock, flags);
  587. /* XXX What if timer fires again while this is running? */
  588. for (qp = resend; qp != NULL; qp = qp->timer_next) {
  589. struct ib_wc wc;
  590. spin_lock_irqsave(&qp->s_lock, flags);
  591. if (qp->s_last != qp->s_tail && qp->state == IB_QPS_RTS) {
  592. dev->n_timeouts++;
  593. ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
  594. }
  595. spin_unlock_irqrestore(&qp->s_lock, flags);
  596. /* Notify ipath_destroy_qp() if it is waiting. */
  597. if (atomic_dec_and_test(&qp->refcount))
  598. wake_up(&qp->wait);
  599. }
  600. }
  601. static void update_sge(struct ipath_sge_state *ss, u32 length)
  602. {
  603. struct ipath_sge *sge = &ss->sge;
  604. sge->vaddr += length;
  605. sge->length -= length;
  606. sge->sge_length -= length;
  607. if (sge->sge_length == 0) {
  608. if (--ss->num_sge)
  609. *sge = *ss->sg_list++;
  610. } else if (sge->length == 0 && sge->mr != NULL) {
  611. if (++sge->n >= IPATH_SEGSZ) {
  612. if (++sge->m >= sge->mr->mapsz)
  613. return;
  614. sge->n = 0;
  615. }
  616. sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
  617. sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
  618. }
  619. }
  620. #ifdef __LITTLE_ENDIAN
  621. static inline u32 get_upper_bits(u32 data, u32 shift)
  622. {
  623. return data >> shift;
  624. }
  625. static inline u32 set_upper_bits(u32 data, u32 shift)
  626. {
  627. return data << shift;
  628. }
  629. static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
  630. {
  631. data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
  632. data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
  633. return data;
  634. }
  635. #else
  636. static inline u32 get_upper_bits(u32 data, u32 shift)
  637. {
  638. return data << shift;
  639. }
  640. static inline u32 set_upper_bits(u32 data, u32 shift)
  641. {
  642. return data >> shift;
  643. }
  644. static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
  645. {
  646. data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
  647. data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
  648. return data;
  649. }
  650. #endif
  651. static void copy_io(u32 __iomem *piobuf, struct ipath_sge_state *ss,
  652. u32 length, unsigned flush_wc)
  653. {
  654. u32 extra = 0;
  655. u32 data = 0;
  656. u32 last;
  657. while (1) {
  658. u32 len = ss->sge.length;
  659. u32 off;
  660. if (len > length)
  661. len = length;
  662. if (len > ss->sge.sge_length)
  663. len = ss->sge.sge_length;
  664. BUG_ON(len == 0);
  665. /* If the source address is not aligned, try to align it. */
  666. off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
  667. if (off) {
  668. u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
  669. ~(sizeof(u32) - 1));
  670. u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
  671. u32 y;
  672. y = sizeof(u32) - off;
  673. if (len > y)
  674. len = y;
  675. if (len + extra >= sizeof(u32)) {
  676. data |= set_upper_bits(v, extra *
  677. BITS_PER_BYTE);
  678. len = sizeof(u32) - extra;
  679. if (len == length) {
  680. last = data;
  681. break;
  682. }
  683. __raw_writel(data, piobuf);
  684. piobuf++;
  685. extra = 0;
  686. data = 0;
  687. } else {
  688. /* Clear unused upper bytes */
  689. data |= clear_upper_bytes(v, len, extra);
  690. if (len == length) {
  691. last = data;
  692. break;
  693. }
  694. extra += len;
  695. }
  696. } else if (extra) {
  697. /* Source address is aligned. */
  698. u32 *addr = (u32 *) ss->sge.vaddr;
  699. int shift = extra * BITS_PER_BYTE;
  700. int ushift = 32 - shift;
  701. u32 l = len;
  702. while (l >= sizeof(u32)) {
  703. u32 v = *addr;
  704. data |= set_upper_bits(v, shift);
  705. __raw_writel(data, piobuf);
  706. data = get_upper_bits(v, ushift);
  707. piobuf++;
  708. addr++;
  709. l -= sizeof(u32);
  710. }
  711. /*
  712. * We still have 'extra' number of bytes leftover.
  713. */
  714. if (l) {
  715. u32 v = *addr;
  716. if (l + extra >= sizeof(u32)) {
  717. data |= set_upper_bits(v, shift);
  718. len -= l + extra - sizeof(u32);
  719. if (len == length) {
  720. last = data;
  721. break;
  722. }
  723. __raw_writel(data, piobuf);
  724. piobuf++;
  725. extra = 0;
  726. data = 0;
  727. } else {
  728. /* Clear unused upper bytes */
  729. data |= clear_upper_bytes(v, l,
  730. extra);
  731. if (len == length) {
  732. last = data;
  733. break;
  734. }
  735. extra += l;
  736. }
  737. } else if (len == length) {
  738. last = data;
  739. break;
  740. }
  741. } else if (len == length) {
  742. u32 w;
  743. /*
  744. * Need to round up for the last dword in the
  745. * packet.
  746. */
  747. w = (len + 3) >> 2;
  748. __iowrite32_copy(piobuf, ss->sge.vaddr, w - 1);
  749. piobuf += w - 1;
  750. last = ((u32 *) ss->sge.vaddr)[w - 1];
  751. break;
  752. } else {
  753. u32 w = len >> 2;
  754. __iowrite32_copy(piobuf, ss->sge.vaddr, w);
  755. piobuf += w;
  756. extra = len & (sizeof(u32) - 1);
  757. if (extra) {
  758. u32 v = ((u32 *) ss->sge.vaddr)[w];
  759. /* Clear unused upper bytes */
  760. data = clear_upper_bytes(v, extra, 0);
  761. }
  762. }
  763. update_sge(ss, len);
  764. length -= len;
  765. }
  766. /* Update address before sending packet. */
  767. update_sge(ss, length);
  768. if (flush_wc) {
  769. /* must flush early everything before trigger word */
  770. ipath_flush_wc();
  771. __raw_writel(last, piobuf);
  772. /* be sure trigger word is written */
  773. ipath_flush_wc();
  774. } else
  775. __raw_writel(last, piobuf);
  776. }
  777. static int ipath_verbs_send_pio(struct ipath_qp *qp, u32 *hdr, u32 hdrwords,
  778. struct ipath_sge_state *ss, u32 len,
  779. u32 plen, u32 dwords)
  780. {
  781. struct ipath_devdata *dd = to_idev(qp->ibqp.device)->dd;
  782. u32 __iomem *piobuf;
  783. unsigned flush_wc;
  784. int ret;
  785. piobuf = ipath_getpiobuf(dd, NULL);
  786. if (unlikely(piobuf == NULL)) {
  787. ret = -EBUSY;
  788. goto bail;
  789. }
  790. /*
  791. * Write len to control qword, no flags.
  792. * We have to flush after the PBC for correctness on some cpus
  793. * or WC buffer can be written out of order.
  794. */
  795. writeq(plen, piobuf);
  796. piobuf += 2;
  797. flush_wc = dd->ipath_flags & IPATH_PIO_FLUSH_WC;
  798. if (len == 0) {
  799. /*
  800. * If there is just the header portion, must flush before
  801. * writing last word of header for correctness, and after
  802. * the last header word (trigger word).
  803. */
  804. if (flush_wc) {
  805. ipath_flush_wc();
  806. __iowrite32_copy(piobuf, hdr, hdrwords - 1);
  807. ipath_flush_wc();
  808. __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
  809. ipath_flush_wc();
  810. } else
  811. __iowrite32_copy(piobuf, hdr, hdrwords);
  812. goto done;
  813. }
  814. if (flush_wc)
  815. ipath_flush_wc();
  816. __iowrite32_copy(piobuf, hdr, hdrwords);
  817. piobuf += hdrwords;
  818. /* The common case is aligned and contained in one segment. */
  819. if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
  820. !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
  821. u32 *addr = (u32 *) ss->sge.vaddr;
  822. /* Update address before sending packet. */
  823. update_sge(ss, len);
  824. if (flush_wc) {
  825. __iowrite32_copy(piobuf, addr, dwords - 1);
  826. /* must flush early everything before trigger word */
  827. ipath_flush_wc();
  828. __raw_writel(addr[dwords - 1], piobuf + dwords - 1);
  829. /* be sure trigger word is written */
  830. ipath_flush_wc();
  831. } else
  832. __iowrite32_copy(piobuf, addr, dwords);
  833. goto done;
  834. }
  835. copy_io(piobuf, ss, len, flush_wc);
  836. done:
  837. if (qp->s_wqe)
  838. ipath_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
  839. ret = 0;
  840. bail:
  841. return ret;
  842. }
  843. /**
  844. * ipath_verbs_send - send a packet
  845. * @qp: the QP to send on
  846. * @hdr: the packet header
  847. * @hdrwords: the number of words in the header
  848. * @ss: the SGE to send
  849. * @len: the length of the packet in bytes
  850. */
  851. int ipath_verbs_send(struct ipath_qp *qp, struct ipath_ib_header *hdr,
  852. u32 hdrwords, struct ipath_sge_state *ss, u32 len)
  853. {
  854. struct ipath_devdata *dd = to_idev(qp->ibqp.device)->dd;
  855. u32 plen;
  856. int ret;
  857. u32 dwords = (len + 3) >> 2;
  858. /* +1 is for the qword padding of pbc */
  859. plen = hdrwords + dwords + 1;
  860. /* Drop non-VL15 packets if we are not in the active state */
  861. if (!(dd->ipath_flags & IPATH_LINKACTIVE) &&
  862. qp->ibqp.qp_type != IB_QPT_SMI) {
  863. if (qp->s_wqe)
  864. ipath_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
  865. ret = 0;
  866. } else
  867. ret = ipath_verbs_send_pio(qp, (u32 *) hdr, hdrwords,
  868. ss, len, plen, dwords);
  869. return ret;
  870. }
  871. int ipath_snapshot_counters(struct ipath_devdata *dd, u64 *swords,
  872. u64 *rwords, u64 *spkts, u64 *rpkts,
  873. u64 *xmit_wait)
  874. {
  875. int ret;
  876. if (!(dd->ipath_flags & IPATH_INITTED)) {
  877. /* no hardware, freeze, etc. */
  878. ret = -EINVAL;
  879. goto bail;
  880. }
  881. *swords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
  882. *rwords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
  883. *spkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
  884. *rpkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
  885. *xmit_wait = ipath_snap_cntr(dd, dd->ipath_cregs->cr_sendstallcnt);
  886. ret = 0;
  887. bail:
  888. return ret;
  889. }
  890. /**
  891. * ipath_get_counters - get various chip counters
  892. * @dd: the infinipath device
  893. * @cntrs: counters are placed here
  894. *
  895. * Return the counters needed by recv_pma_get_portcounters().
  896. */
  897. int ipath_get_counters(struct ipath_devdata *dd,
  898. struct ipath_verbs_counters *cntrs)
  899. {
  900. struct ipath_cregs const *crp = dd->ipath_cregs;
  901. int ret;
  902. if (!(dd->ipath_flags & IPATH_INITTED)) {
  903. /* no hardware, freeze, etc. */
  904. ret = -EINVAL;
  905. goto bail;
  906. }
  907. cntrs->symbol_error_counter =
  908. ipath_snap_cntr(dd, crp->cr_ibsymbolerrcnt);
  909. cntrs->link_error_recovery_counter =
  910. ipath_snap_cntr(dd, crp->cr_iblinkerrrecovcnt);
  911. /*
  912. * The link downed counter counts when the other side downs the
  913. * connection. We add in the number of times we downed the link
  914. * due to local link integrity errors to compensate.
  915. */
  916. cntrs->link_downed_counter =
  917. ipath_snap_cntr(dd, crp->cr_iblinkdowncnt);
  918. cntrs->port_rcv_errors =
  919. ipath_snap_cntr(dd, crp->cr_rxdroppktcnt) +
  920. ipath_snap_cntr(dd, crp->cr_rcvovflcnt) +
  921. ipath_snap_cntr(dd, crp->cr_portovflcnt) +
  922. ipath_snap_cntr(dd, crp->cr_err_rlencnt) +
  923. ipath_snap_cntr(dd, crp->cr_invalidrlencnt) +
  924. ipath_snap_cntr(dd, crp->cr_errlinkcnt) +
  925. ipath_snap_cntr(dd, crp->cr_erricrccnt) +
  926. ipath_snap_cntr(dd, crp->cr_errvcrccnt) +
  927. ipath_snap_cntr(dd, crp->cr_errlpcrccnt) +
  928. ipath_snap_cntr(dd, crp->cr_badformatcnt) +
  929. dd->ipath_rxfc_unsupvl_errs;
  930. cntrs->port_rcv_remphys_errors =
  931. ipath_snap_cntr(dd, crp->cr_rcvebpcnt);
  932. cntrs->port_xmit_discards = ipath_snap_cntr(dd, crp->cr_unsupvlcnt);
  933. cntrs->port_xmit_data = ipath_snap_cntr(dd, crp->cr_wordsendcnt);
  934. cntrs->port_rcv_data = ipath_snap_cntr(dd, crp->cr_wordrcvcnt);
  935. cntrs->port_xmit_packets = ipath_snap_cntr(dd, crp->cr_pktsendcnt);
  936. cntrs->port_rcv_packets = ipath_snap_cntr(dd, crp->cr_pktrcvcnt);
  937. cntrs->local_link_integrity_errors =
  938. (dd->ipath_flags & IPATH_GPIO_ERRINTRS) ?
  939. dd->ipath_lli_errs : dd->ipath_lli_errors;
  940. cntrs->excessive_buffer_overrun_errors = dd->ipath_overrun_thresh_errs;
  941. ret = 0;
  942. bail:
  943. return ret;
  944. }
  945. /**
  946. * ipath_ib_piobufavail - callback when a PIO buffer is available
  947. * @arg: the device pointer
  948. *
  949. * This is called from ipath_intr() at interrupt level when a PIO buffer is
  950. * available after ipath_verbs_send() returned an error that no buffers were
  951. * available. Return 1 if we consumed all the PIO buffers and we still have
  952. * QPs waiting for buffers (for now, just do a tasklet_hi_schedule and
  953. * return zero).
  954. */
  955. int ipath_ib_piobufavail(struct ipath_ibdev *dev)
  956. {
  957. struct ipath_qp *qp;
  958. unsigned long flags;
  959. if (dev == NULL)
  960. goto bail;
  961. spin_lock_irqsave(&dev->pending_lock, flags);
  962. while (!list_empty(&dev->piowait)) {
  963. qp = list_entry(dev->piowait.next, struct ipath_qp,
  964. piowait);
  965. list_del_init(&qp->piowait);
  966. clear_bit(IPATH_S_BUSY, &qp->s_busy);
  967. tasklet_hi_schedule(&qp->s_task);
  968. }
  969. spin_unlock_irqrestore(&dev->pending_lock, flags);
  970. bail:
  971. return 0;
  972. }
  973. static int ipath_query_device(struct ib_device *ibdev,
  974. struct ib_device_attr *props)
  975. {
  976. struct ipath_ibdev *dev = to_idev(ibdev);
  977. memset(props, 0, sizeof(*props));
  978. props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
  979. IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
  980. IB_DEVICE_SYS_IMAGE_GUID;
  981. props->page_size_cap = PAGE_SIZE;
  982. props->vendor_id = dev->dd->ipath_vendorid;
  983. props->vendor_part_id = dev->dd->ipath_deviceid;
  984. props->hw_ver = dev->dd->ipath_pcirev;
  985. props->sys_image_guid = dev->sys_image_guid;
  986. props->max_mr_size = ~0ull;
  987. props->max_qp = ib_ipath_max_qps;
  988. props->max_qp_wr = ib_ipath_max_qp_wrs;
  989. props->max_sge = ib_ipath_max_sges;
  990. props->max_cq = ib_ipath_max_cqs;
  991. props->max_ah = ib_ipath_max_ahs;
  992. props->max_cqe = ib_ipath_max_cqes;
  993. props->max_mr = dev->lk_table.max;
  994. props->max_fmr = dev->lk_table.max;
  995. props->max_map_per_fmr = 32767;
  996. props->max_pd = ib_ipath_max_pds;
  997. props->max_qp_rd_atom = IPATH_MAX_RDMA_ATOMIC;
  998. props->max_qp_init_rd_atom = 255;
  999. /* props->max_res_rd_atom */
  1000. props->max_srq = ib_ipath_max_srqs;
  1001. props->max_srq_wr = ib_ipath_max_srq_wrs;
  1002. props->max_srq_sge = ib_ipath_max_srq_sges;
  1003. /* props->local_ca_ack_delay */
  1004. props->atomic_cap = IB_ATOMIC_GLOB;
  1005. props->max_pkeys = ipath_get_npkeys(dev->dd);
  1006. props->max_mcast_grp = ib_ipath_max_mcast_grps;
  1007. props->max_mcast_qp_attach = ib_ipath_max_mcast_qp_attached;
  1008. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  1009. props->max_mcast_grp;
  1010. return 0;
  1011. }
  1012. const u8 ipath_cvt_physportstate[16] = {
  1013. [INFINIPATH_IBCS_LT_STATE_DISABLED] = 3,
  1014. [INFINIPATH_IBCS_LT_STATE_LINKUP] = 5,
  1015. [INFINIPATH_IBCS_LT_STATE_POLLACTIVE] = 2,
  1016. [INFINIPATH_IBCS_LT_STATE_POLLQUIET] = 2,
  1017. [INFINIPATH_IBCS_LT_STATE_SLEEPDELAY] = 1,
  1018. [INFINIPATH_IBCS_LT_STATE_SLEEPQUIET] = 1,
  1019. [INFINIPATH_IBCS_LT_STATE_CFGDEBOUNCE] = 4,
  1020. [INFINIPATH_IBCS_LT_STATE_CFGRCVFCFG] = 4,
  1021. [INFINIPATH_IBCS_LT_STATE_CFGWAITRMT] = 4,
  1022. [INFINIPATH_IBCS_LT_STATE_CFGIDLE] = 4,
  1023. [INFINIPATH_IBCS_LT_STATE_RECOVERRETRAIN] = 6,
  1024. [INFINIPATH_IBCS_LT_STATE_RECOVERWAITRMT] = 6,
  1025. [INFINIPATH_IBCS_LT_STATE_RECOVERIDLE] = 6,
  1026. };
  1027. u32 ipath_get_cr_errpkey(struct ipath_devdata *dd)
  1028. {
  1029. return ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
  1030. }
  1031. static int ipath_query_port(struct ib_device *ibdev,
  1032. u8 port, struct ib_port_attr *props)
  1033. {
  1034. struct ipath_ibdev *dev = to_idev(ibdev);
  1035. struct ipath_devdata *dd = dev->dd;
  1036. enum ib_mtu mtu;
  1037. u16 lid = dd->ipath_lid;
  1038. u64 ibcstat;
  1039. memset(props, 0, sizeof(*props));
  1040. props->lid = lid ? lid : __constant_be16_to_cpu(IB_LID_PERMISSIVE);
  1041. props->lmc = dd->ipath_lmc;
  1042. props->sm_lid = dev->sm_lid;
  1043. props->sm_sl = dev->sm_sl;
  1044. ibcstat = dd->ipath_lastibcstat;
  1045. props->state = ((ibcstat >> 4) & 0x3) + 1;
  1046. /* See phys_state_show() */
  1047. props->phys_state = ipath_cvt_physportstate[
  1048. dd->ipath_lastibcstat & 0xf];
  1049. props->port_cap_flags = dev->port_cap_flags;
  1050. props->gid_tbl_len = 1;
  1051. props->max_msg_sz = 0x80000000;
  1052. props->pkey_tbl_len = ipath_get_npkeys(dd);
  1053. props->bad_pkey_cntr = ipath_get_cr_errpkey(dd) -
  1054. dev->z_pkey_violations;
  1055. props->qkey_viol_cntr = dev->qkey_violations;
  1056. props->active_width = IB_WIDTH_4X;
  1057. /* See rate_show() */
  1058. props->active_speed = 1; /* Regular 10Mbs speed. */
  1059. props->max_vl_num = 1; /* VLCap = VL0 */
  1060. props->init_type_reply = 0;
  1061. /*
  1062. * Note: the chip supports a maximum MTU of 4096, but the driver
  1063. * hasn't implemented this feature yet, so set the maximum value
  1064. * to 2048.
  1065. */
  1066. props->max_mtu = IB_MTU_2048;
  1067. switch (dd->ipath_ibmtu) {
  1068. case 4096:
  1069. mtu = IB_MTU_4096;
  1070. break;
  1071. case 2048:
  1072. mtu = IB_MTU_2048;
  1073. break;
  1074. case 1024:
  1075. mtu = IB_MTU_1024;
  1076. break;
  1077. case 512:
  1078. mtu = IB_MTU_512;
  1079. break;
  1080. case 256:
  1081. mtu = IB_MTU_256;
  1082. break;
  1083. default:
  1084. mtu = IB_MTU_2048;
  1085. }
  1086. props->active_mtu = mtu;
  1087. props->subnet_timeout = dev->subnet_timeout;
  1088. return 0;
  1089. }
  1090. static int ipath_modify_device(struct ib_device *device,
  1091. int device_modify_mask,
  1092. struct ib_device_modify *device_modify)
  1093. {
  1094. int ret;
  1095. if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
  1096. IB_DEVICE_MODIFY_NODE_DESC)) {
  1097. ret = -EOPNOTSUPP;
  1098. goto bail;
  1099. }
  1100. if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC)
  1101. memcpy(device->node_desc, device_modify->node_desc, 64);
  1102. if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
  1103. to_idev(device)->sys_image_guid =
  1104. cpu_to_be64(device_modify->sys_image_guid);
  1105. ret = 0;
  1106. bail:
  1107. return ret;
  1108. }
  1109. static int ipath_modify_port(struct ib_device *ibdev,
  1110. u8 port, int port_modify_mask,
  1111. struct ib_port_modify *props)
  1112. {
  1113. struct ipath_ibdev *dev = to_idev(ibdev);
  1114. dev->port_cap_flags |= props->set_port_cap_mask;
  1115. dev->port_cap_flags &= ~props->clr_port_cap_mask;
  1116. if (port_modify_mask & IB_PORT_SHUTDOWN)
  1117. ipath_set_linkstate(dev->dd, IPATH_IB_LINKDOWN);
  1118. if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
  1119. dev->qkey_violations = 0;
  1120. return 0;
  1121. }
  1122. static int ipath_query_gid(struct ib_device *ibdev, u8 port,
  1123. int index, union ib_gid *gid)
  1124. {
  1125. struct ipath_ibdev *dev = to_idev(ibdev);
  1126. int ret;
  1127. if (index >= 1) {
  1128. ret = -EINVAL;
  1129. goto bail;
  1130. }
  1131. gid->global.subnet_prefix = dev->gid_prefix;
  1132. gid->global.interface_id = dev->dd->ipath_guid;
  1133. ret = 0;
  1134. bail:
  1135. return ret;
  1136. }
  1137. static struct ib_pd *ipath_alloc_pd(struct ib_device *ibdev,
  1138. struct ib_ucontext *context,
  1139. struct ib_udata *udata)
  1140. {
  1141. struct ipath_ibdev *dev = to_idev(ibdev);
  1142. struct ipath_pd *pd;
  1143. struct ib_pd *ret;
  1144. /*
  1145. * This is actually totally arbitrary. Some correctness tests
  1146. * assume there's a maximum number of PDs that can be allocated.
  1147. * We don't actually have this limit, but we fail the test if
  1148. * we allow allocations of more than we report for this value.
  1149. */
  1150. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  1151. if (!pd) {
  1152. ret = ERR_PTR(-ENOMEM);
  1153. goto bail;
  1154. }
  1155. spin_lock(&dev->n_pds_lock);
  1156. if (dev->n_pds_allocated == ib_ipath_max_pds) {
  1157. spin_unlock(&dev->n_pds_lock);
  1158. kfree(pd);
  1159. ret = ERR_PTR(-ENOMEM);
  1160. goto bail;
  1161. }
  1162. dev->n_pds_allocated++;
  1163. spin_unlock(&dev->n_pds_lock);
  1164. /* ib_alloc_pd() will initialize pd->ibpd. */
  1165. pd->user = udata != NULL;
  1166. ret = &pd->ibpd;
  1167. bail:
  1168. return ret;
  1169. }
  1170. static int ipath_dealloc_pd(struct ib_pd *ibpd)
  1171. {
  1172. struct ipath_pd *pd = to_ipd(ibpd);
  1173. struct ipath_ibdev *dev = to_idev(ibpd->device);
  1174. spin_lock(&dev->n_pds_lock);
  1175. dev->n_pds_allocated--;
  1176. spin_unlock(&dev->n_pds_lock);
  1177. kfree(pd);
  1178. return 0;
  1179. }
  1180. /**
  1181. * ipath_create_ah - create an address handle
  1182. * @pd: the protection domain
  1183. * @ah_attr: the attributes of the AH
  1184. *
  1185. * This may be called from interrupt context.
  1186. */
  1187. static struct ib_ah *ipath_create_ah(struct ib_pd *pd,
  1188. struct ib_ah_attr *ah_attr)
  1189. {
  1190. struct ipath_ah *ah;
  1191. struct ib_ah *ret;
  1192. struct ipath_ibdev *dev = to_idev(pd->device);
  1193. unsigned long flags;
  1194. /* A multicast address requires a GRH (see ch. 8.4.1). */
  1195. if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&
  1196. ah_attr->dlid != IPATH_PERMISSIVE_LID &&
  1197. !(ah_attr->ah_flags & IB_AH_GRH)) {
  1198. ret = ERR_PTR(-EINVAL);
  1199. goto bail;
  1200. }
  1201. if (ah_attr->dlid == 0) {
  1202. ret = ERR_PTR(-EINVAL);
  1203. goto bail;
  1204. }
  1205. if (ah_attr->port_num < 1 ||
  1206. ah_attr->port_num > pd->device->phys_port_cnt) {
  1207. ret = ERR_PTR(-EINVAL);
  1208. goto bail;
  1209. }
  1210. ah = kmalloc(sizeof *ah, GFP_ATOMIC);
  1211. if (!ah) {
  1212. ret = ERR_PTR(-ENOMEM);
  1213. goto bail;
  1214. }
  1215. spin_lock_irqsave(&dev->n_ahs_lock, flags);
  1216. if (dev->n_ahs_allocated == ib_ipath_max_ahs) {
  1217. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  1218. kfree(ah);
  1219. ret = ERR_PTR(-ENOMEM);
  1220. goto bail;
  1221. }
  1222. dev->n_ahs_allocated++;
  1223. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  1224. /* ib_create_ah() will initialize ah->ibah. */
  1225. ah->attr = *ah_attr;
  1226. ret = &ah->ibah;
  1227. bail:
  1228. return ret;
  1229. }
  1230. /**
  1231. * ipath_destroy_ah - destroy an address handle
  1232. * @ibah: the AH to destroy
  1233. *
  1234. * This may be called from interrupt context.
  1235. */
  1236. static int ipath_destroy_ah(struct ib_ah *ibah)
  1237. {
  1238. struct ipath_ibdev *dev = to_idev(ibah->device);
  1239. struct ipath_ah *ah = to_iah(ibah);
  1240. unsigned long flags;
  1241. spin_lock_irqsave(&dev->n_ahs_lock, flags);
  1242. dev->n_ahs_allocated--;
  1243. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  1244. kfree(ah);
  1245. return 0;
  1246. }
  1247. static int ipath_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
  1248. {
  1249. struct ipath_ah *ah = to_iah(ibah);
  1250. *ah_attr = ah->attr;
  1251. return 0;
  1252. }
  1253. /**
  1254. * ipath_get_npkeys - return the size of the PKEY table for port 0
  1255. * @dd: the infinipath device
  1256. */
  1257. unsigned ipath_get_npkeys(struct ipath_devdata *dd)
  1258. {
  1259. return ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys);
  1260. }
  1261. /**
  1262. * ipath_get_pkey - return the indexed PKEY from the port 0 PKEY table
  1263. * @dd: the infinipath device
  1264. * @index: the PKEY index
  1265. */
  1266. unsigned ipath_get_pkey(struct ipath_devdata *dd, unsigned index)
  1267. {
  1268. unsigned ret;
  1269. if (index >= ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys))
  1270. ret = 0;
  1271. else
  1272. ret = dd->ipath_pd[0]->port_pkeys[index];
  1273. return ret;
  1274. }
  1275. static int ipath_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  1276. u16 *pkey)
  1277. {
  1278. struct ipath_ibdev *dev = to_idev(ibdev);
  1279. int ret;
  1280. if (index >= ipath_get_npkeys(dev->dd)) {
  1281. ret = -EINVAL;
  1282. goto bail;
  1283. }
  1284. *pkey = ipath_get_pkey(dev->dd, index);
  1285. ret = 0;
  1286. bail:
  1287. return ret;
  1288. }
  1289. /**
  1290. * ipath_alloc_ucontext - allocate a ucontest
  1291. * @ibdev: the infiniband device
  1292. * @udata: not used by the InfiniPath driver
  1293. */
  1294. static struct ib_ucontext *ipath_alloc_ucontext(struct ib_device *ibdev,
  1295. struct ib_udata *udata)
  1296. {
  1297. struct ipath_ucontext *context;
  1298. struct ib_ucontext *ret;
  1299. context = kmalloc(sizeof *context, GFP_KERNEL);
  1300. if (!context) {
  1301. ret = ERR_PTR(-ENOMEM);
  1302. goto bail;
  1303. }
  1304. ret = &context->ibucontext;
  1305. bail:
  1306. return ret;
  1307. }
  1308. static int ipath_dealloc_ucontext(struct ib_ucontext *context)
  1309. {
  1310. kfree(to_iucontext(context));
  1311. return 0;
  1312. }
  1313. static int ipath_verbs_register_sysfs(struct ib_device *dev);
  1314. static void __verbs_timer(unsigned long arg)
  1315. {
  1316. struct ipath_devdata *dd = (struct ipath_devdata *) arg;
  1317. /* Handle verbs layer timeouts. */
  1318. ipath_ib_timer(dd->verbs_dev);
  1319. mod_timer(&dd->verbs_timer, jiffies + 1);
  1320. }
  1321. static int enable_timer(struct ipath_devdata *dd)
  1322. {
  1323. /*
  1324. * Early chips had a design flaw where the chip and kernel idea
  1325. * of the tail register don't always agree, and therefore we won't
  1326. * get an interrupt on the next packet received.
  1327. * If the board supports per packet receive interrupts, use it.
  1328. * Otherwise, the timer function periodically checks for packets
  1329. * to cover this case.
  1330. * Either way, the timer is needed for verbs layer related
  1331. * processing.
  1332. */
  1333. if (dd->ipath_flags & IPATH_GPIO_INTR) {
  1334. ipath_write_kreg(dd, dd->ipath_kregs->kr_debugportselect,
  1335. 0x2074076542310ULL);
  1336. /* Enable GPIO bit 2 interrupt */
  1337. dd->ipath_gpio_mask |= (u64) (1 << IPATH_GPIO_PORT0_BIT);
  1338. ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
  1339. dd->ipath_gpio_mask);
  1340. }
  1341. init_timer(&dd->verbs_timer);
  1342. dd->verbs_timer.function = __verbs_timer;
  1343. dd->verbs_timer.data = (unsigned long)dd;
  1344. dd->verbs_timer.expires = jiffies + 1;
  1345. add_timer(&dd->verbs_timer);
  1346. return 0;
  1347. }
  1348. static int disable_timer(struct ipath_devdata *dd)
  1349. {
  1350. /* Disable GPIO bit 2 interrupt */
  1351. if (dd->ipath_flags & IPATH_GPIO_INTR) {
  1352. /* Disable GPIO bit 2 interrupt */
  1353. dd->ipath_gpio_mask &= ~((u64) (1 << IPATH_GPIO_PORT0_BIT));
  1354. ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
  1355. dd->ipath_gpio_mask);
  1356. /*
  1357. * We might want to undo changes to debugportselect,
  1358. * but how?
  1359. */
  1360. }
  1361. del_timer_sync(&dd->verbs_timer);
  1362. return 0;
  1363. }
  1364. /**
  1365. * ipath_register_ib_device - register our device with the infiniband core
  1366. * @dd: the device data structure
  1367. * Return the allocated ipath_ibdev pointer or NULL on error.
  1368. */
  1369. int ipath_register_ib_device(struct ipath_devdata *dd)
  1370. {
  1371. struct ipath_verbs_counters cntrs;
  1372. struct ipath_ibdev *idev;
  1373. struct ib_device *dev;
  1374. int ret;
  1375. idev = (struct ipath_ibdev *)ib_alloc_device(sizeof *idev);
  1376. if (idev == NULL) {
  1377. ret = -ENOMEM;
  1378. goto bail;
  1379. }
  1380. dev = &idev->ibdev;
  1381. /* Only need to initialize non-zero fields. */
  1382. spin_lock_init(&idev->n_pds_lock);
  1383. spin_lock_init(&idev->n_ahs_lock);
  1384. spin_lock_init(&idev->n_cqs_lock);
  1385. spin_lock_init(&idev->n_qps_lock);
  1386. spin_lock_init(&idev->n_srqs_lock);
  1387. spin_lock_init(&idev->n_mcast_grps_lock);
  1388. spin_lock_init(&idev->qp_table.lock);
  1389. spin_lock_init(&idev->lk_table.lock);
  1390. idev->sm_lid = __constant_be16_to_cpu(IB_LID_PERMISSIVE);
  1391. /* Set the prefix to the default value (see ch. 4.1.1) */
  1392. idev->gid_prefix = __constant_cpu_to_be64(0xfe80000000000000ULL);
  1393. ret = ipath_init_qp_table(idev, ib_ipath_qp_table_size);
  1394. if (ret)
  1395. goto err_qp;
  1396. /*
  1397. * The top ib_ipath_lkey_table_size bits are used to index the
  1398. * table. The lower 8 bits can be owned by the user (copied from
  1399. * the LKEY). The remaining bits act as a generation number or tag.
  1400. */
  1401. idev->lk_table.max = 1 << ib_ipath_lkey_table_size;
  1402. idev->lk_table.table = kzalloc(idev->lk_table.max *
  1403. sizeof(*idev->lk_table.table),
  1404. GFP_KERNEL);
  1405. if (idev->lk_table.table == NULL) {
  1406. ret = -ENOMEM;
  1407. goto err_lk;
  1408. }
  1409. INIT_LIST_HEAD(&idev->pending_mmaps);
  1410. spin_lock_init(&idev->pending_lock);
  1411. idev->mmap_offset = PAGE_SIZE;
  1412. spin_lock_init(&idev->mmap_offset_lock);
  1413. INIT_LIST_HEAD(&idev->pending[0]);
  1414. INIT_LIST_HEAD(&idev->pending[1]);
  1415. INIT_LIST_HEAD(&idev->pending[2]);
  1416. INIT_LIST_HEAD(&idev->piowait);
  1417. INIT_LIST_HEAD(&idev->rnrwait);
  1418. idev->pending_index = 0;
  1419. idev->port_cap_flags =
  1420. IB_PORT_SYS_IMAGE_GUID_SUP | IB_PORT_CLIENT_REG_SUP;
  1421. idev->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
  1422. idev->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
  1423. idev->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
  1424. idev->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
  1425. idev->pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
  1426. idev->link_width_enabled = 3; /* 1x or 4x */
  1427. /* Snapshot current HW counters to "clear" them. */
  1428. ipath_get_counters(dd, &cntrs);
  1429. idev->z_symbol_error_counter = cntrs.symbol_error_counter;
  1430. idev->z_link_error_recovery_counter =
  1431. cntrs.link_error_recovery_counter;
  1432. idev->z_link_downed_counter = cntrs.link_downed_counter;
  1433. idev->z_port_rcv_errors = cntrs.port_rcv_errors;
  1434. idev->z_port_rcv_remphys_errors =
  1435. cntrs.port_rcv_remphys_errors;
  1436. idev->z_port_xmit_discards = cntrs.port_xmit_discards;
  1437. idev->z_port_xmit_data = cntrs.port_xmit_data;
  1438. idev->z_port_rcv_data = cntrs.port_rcv_data;
  1439. idev->z_port_xmit_packets = cntrs.port_xmit_packets;
  1440. idev->z_port_rcv_packets = cntrs.port_rcv_packets;
  1441. idev->z_local_link_integrity_errors =
  1442. cntrs.local_link_integrity_errors;
  1443. idev->z_excessive_buffer_overrun_errors =
  1444. cntrs.excessive_buffer_overrun_errors;
  1445. /*
  1446. * The system image GUID is supposed to be the same for all
  1447. * IB HCAs in a single system but since there can be other
  1448. * device types in the system, we can't be sure this is unique.
  1449. */
  1450. if (!sys_image_guid)
  1451. sys_image_guid = dd->ipath_guid;
  1452. idev->sys_image_guid = sys_image_guid;
  1453. idev->ib_unit = dd->ipath_unit;
  1454. idev->dd = dd;
  1455. strlcpy(dev->name, "ipath%d", IB_DEVICE_NAME_MAX);
  1456. dev->owner = THIS_MODULE;
  1457. dev->node_guid = dd->ipath_guid;
  1458. dev->uverbs_abi_ver = IPATH_UVERBS_ABI_VERSION;
  1459. dev->uverbs_cmd_mask =
  1460. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  1461. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  1462. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  1463. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  1464. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  1465. (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
  1466. (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
  1467. (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
  1468. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  1469. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  1470. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  1471. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  1472. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  1473. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  1474. (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
  1475. (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
  1476. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  1477. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  1478. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  1479. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  1480. (1ull << IB_USER_VERBS_CMD_POST_SEND) |
  1481. (1ull << IB_USER_VERBS_CMD_POST_RECV) |
  1482. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  1483. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  1484. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  1485. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  1486. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  1487. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
  1488. (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
  1489. dev->node_type = RDMA_NODE_IB_CA;
  1490. dev->phys_port_cnt = 1;
  1491. dev->num_comp_vectors = 1;
  1492. dev->dma_device = &dd->pcidev->dev;
  1493. dev->query_device = ipath_query_device;
  1494. dev->modify_device = ipath_modify_device;
  1495. dev->query_port = ipath_query_port;
  1496. dev->modify_port = ipath_modify_port;
  1497. dev->query_pkey = ipath_query_pkey;
  1498. dev->query_gid = ipath_query_gid;
  1499. dev->alloc_ucontext = ipath_alloc_ucontext;
  1500. dev->dealloc_ucontext = ipath_dealloc_ucontext;
  1501. dev->alloc_pd = ipath_alloc_pd;
  1502. dev->dealloc_pd = ipath_dealloc_pd;
  1503. dev->create_ah = ipath_create_ah;
  1504. dev->destroy_ah = ipath_destroy_ah;
  1505. dev->query_ah = ipath_query_ah;
  1506. dev->create_srq = ipath_create_srq;
  1507. dev->modify_srq = ipath_modify_srq;
  1508. dev->query_srq = ipath_query_srq;
  1509. dev->destroy_srq = ipath_destroy_srq;
  1510. dev->create_qp = ipath_create_qp;
  1511. dev->modify_qp = ipath_modify_qp;
  1512. dev->query_qp = ipath_query_qp;
  1513. dev->destroy_qp = ipath_destroy_qp;
  1514. dev->post_send = ipath_post_send;
  1515. dev->post_recv = ipath_post_receive;
  1516. dev->post_srq_recv = ipath_post_srq_receive;
  1517. dev->create_cq = ipath_create_cq;
  1518. dev->destroy_cq = ipath_destroy_cq;
  1519. dev->resize_cq = ipath_resize_cq;
  1520. dev->poll_cq = ipath_poll_cq;
  1521. dev->req_notify_cq = ipath_req_notify_cq;
  1522. dev->get_dma_mr = ipath_get_dma_mr;
  1523. dev->reg_phys_mr = ipath_reg_phys_mr;
  1524. dev->reg_user_mr = ipath_reg_user_mr;
  1525. dev->dereg_mr = ipath_dereg_mr;
  1526. dev->alloc_fmr = ipath_alloc_fmr;
  1527. dev->map_phys_fmr = ipath_map_phys_fmr;
  1528. dev->unmap_fmr = ipath_unmap_fmr;
  1529. dev->dealloc_fmr = ipath_dealloc_fmr;
  1530. dev->attach_mcast = ipath_multicast_attach;
  1531. dev->detach_mcast = ipath_multicast_detach;
  1532. dev->process_mad = ipath_process_mad;
  1533. dev->mmap = ipath_mmap;
  1534. dev->dma_ops = &ipath_dma_mapping_ops;
  1535. snprintf(dev->node_desc, sizeof(dev->node_desc),
  1536. IPATH_IDSTR " %s", init_utsname()->nodename);
  1537. ret = ib_register_device(dev);
  1538. if (ret)
  1539. goto err_reg;
  1540. if (ipath_verbs_register_sysfs(dev))
  1541. goto err_class;
  1542. enable_timer(dd);
  1543. goto bail;
  1544. err_class:
  1545. ib_unregister_device(dev);
  1546. err_reg:
  1547. kfree(idev->lk_table.table);
  1548. err_lk:
  1549. kfree(idev->qp_table.table);
  1550. err_qp:
  1551. ib_dealloc_device(dev);
  1552. ipath_dev_err(dd, "cannot register verbs: %d!\n", -ret);
  1553. idev = NULL;
  1554. bail:
  1555. dd->verbs_dev = idev;
  1556. return ret;
  1557. }
  1558. void ipath_unregister_ib_device(struct ipath_ibdev *dev)
  1559. {
  1560. struct ib_device *ibdev = &dev->ibdev;
  1561. disable_timer(dev->dd);
  1562. ib_unregister_device(ibdev);
  1563. if (!list_empty(&dev->pending[0]) ||
  1564. !list_empty(&dev->pending[1]) ||
  1565. !list_empty(&dev->pending[2]))
  1566. ipath_dev_err(dev->dd, "pending list not empty!\n");
  1567. if (!list_empty(&dev->piowait))
  1568. ipath_dev_err(dev->dd, "piowait list not empty!\n");
  1569. if (!list_empty(&dev->rnrwait))
  1570. ipath_dev_err(dev->dd, "rnrwait list not empty!\n");
  1571. if (!ipath_mcast_tree_empty())
  1572. ipath_dev_err(dev->dd, "multicast table memory leak!\n");
  1573. /*
  1574. * Note that ipath_unregister_ib_device() can be called before all
  1575. * the QPs are destroyed!
  1576. */
  1577. ipath_free_all_qps(&dev->qp_table);
  1578. kfree(dev->qp_table.table);
  1579. kfree(dev->lk_table.table);
  1580. ib_dealloc_device(ibdev);
  1581. }
  1582. static ssize_t show_rev(struct class_device *cdev, char *buf)
  1583. {
  1584. struct ipath_ibdev *dev =
  1585. container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
  1586. return sprintf(buf, "%x\n", dev->dd->ipath_pcirev);
  1587. }
  1588. static ssize_t show_hca(struct class_device *cdev, char *buf)
  1589. {
  1590. struct ipath_ibdev *dev =
  1591. container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
  1592. int ret;
  1593. ret = dev->dd->ipath_f_get_boardname(dev->dd, buf, 128);
  1594. if (ret < 0)
  1595. goto bail;
  1596. strcat(buf, "\n");
  1597. ret = strlen(buf);
  1598. bail:
  1599. return ret;
  1600. }
  1601. static ssize_t show_stats(struct class_device *cdev, char *buf)
  1602. {
  1603. struct ipath_ibdev *dev =
  1604. container_of(cdev, struct ipath_ibdev, ibdev.class_dev);
  1605. int i;
  1606. int len;
  1607. len = sprintf(buf,
  1608. "RC resends %d\n"
  1609. "RC no QACK %d\n"
  1610. "RC ACKs %d\n"
  1611. "RC SEQ NAKs %d\n"
  1612. "RC RDMA seq %d\n"
  1613. "RC RNR NAKs %d\n"
  1614. "RC OTH NAKs %d\n"
  1615. "RC timeouts %d\n"
  1616. "RC RDMA dup %d\n"
  1617. "RC stalls %d\n"
  1618. "piobuf wait %d\n"
  1619. "no piobuf %d\n"
  1620. "PKT drops %d\n"
  1621. "WQE errs %d\n",
  1622. dev->n_rc_resends, dev->n_rc_qacks, dev->n_rc_acks,
  1623. dev->n_seq_naks, dev->n_rdma_seq, dev->n_rnr_naks,
  1624. dev->n_other_naks, dev->n_timeouts,
  1625. dev->n_rdma_dup_busy, dev->n_rc_stalls, dev->n_piowait,
  1626. dev->n_no_piobuf, dev->n_pkt_drops, dev->n_wqe_errs);
  1627. for (i = 0; i < ARRAY_SIZE(dev->opstats); i++) {
  1628. const struct ipath_opcode_stats *si = &dev->opstats[i];
  1629. if (!si->n_packets && !si->n_bytes)
  1630. continue;
  1631. len += sprintf(buf + len, "%02x %llu/%llu\n", i,
  1632. (unsigned long long) si->n_packets,
  1633. (unsigned long long) si->n_bytes);
  1634. }
  1635. return len;
  1636. }
  1637. static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  1638. static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  1639. static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL);
  1640. static CLASS_DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
  1641. static struct class_device_attribute *ipath_class_attributes[] = {
  1642. &class_device_attr_hw_rev,
  1643. &class_device_attr_hca_type,
  1644. &class_device_attr_board_id,
  1645. &class_device_attr_stats
  1646. };
  1647. static int ipath_verbs_register_sysfs(struct ib_device *dev)
  1648. {
  1649. int i;
  1650. int ret;
  1651. for (i = 0; i < ARRAY_SIZE(ipath_class_attributes); ++i)
  1652. if (class_device_create_file(&dev->class_dev,
  1653. ipath_class_attributes[i])) {
  1654. ret = 1;
  1655. goto bail;
  1656. }
  1657. ret = 0;
  1658. bail:
  1659. return ret;
  1660. }