ipath_verbs.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  1. /*
  2. * Copyright (c) 2006, 2007, 2008 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(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. * Count the number of DMA descriptors needed to send length bytes of data.
  216. * Don't modify the ipath_sge_state to get the count.
  217. * Return zero if any of the segments is not aligned.
  218. */
  219. static u32 ipath_count_sge(struct ipath_sge_state *ss, u32 length)
  220. {
  221. struct ipath_sge *sg_list = ss->sg_list;
  222. struct ipath_sge sge = ss->sge;
  223. u8 num_sge = ss->num_sge;
  224. u32 ndesc = 1; /* count the header */
  225. while (length) {
  226. u32 len = sge.length;
  227. if (len > length)
  228. len = length;
  229. if (len > sge.sge_length)
  230. len = sge.sge_length;
  231. BUG_ON(len == 0);
  232. if (((long) sge.vaddr & (sizeof(u32) - 1)) ||
  233. (len != length && (len & (sizeof(u32) - 1)))) {
  234. ndesc = 0;
  235. break;
  236. }
  237. ndesc++;
  238. sge.vaddr += len;
  239. sge.length -= len;
  240. sge.sge_length -= len;
  241. if (sge.sge_length == 0) {
  242. if (--num_sge)
  243. sge = *sg_list++;
  244. } else if (sge.length == 0 && sge.mr != NULL) {
  245. if (++sge.n >= IPATH_SEGSZ) {
  246. if (++sge.m >= sge.mr->mapsz)
  247. break;
  248. sge.n = 0;
  249. }
  250. sge.vaddr =
  251. sge.mr->map[sge.m]->segs[sge.n].vaddr;
  252. sge.length =
  253. sge.mr->map[sge.m]->segs[sge.n].length;
  254. }
  255. length -= len;
  256. }
  257. return ndesc;
  258. }
  259. /*
  260. * Copy from the SGEs to the data buffer.
  261. */
  262. static void ipath_copy_from_sge(void *data, struct ipath_sge_state *ss,
  263. u32 length)
  264. {
  265. struct ipath_sge *sge = &ss->sge;
  266. while (length) {
  267. u32 len = sge->length;
  268. if (len > length)
  269. len = length;
  270. if (len > sge->sge_length)
  271. len = sge->sge_length;
  272. BUG_ON(len == 0);
  273. memcpy(data, sge->vaddr, len);
  274. sge->vaddr += len;
  275. sge->length -= len;
  276. sge->sge_length -= len;
  277. if (sge->sge_length == 0) {
  278. if (--ss->num_sge)
  279. *sge = *ss->sg_list++;
  280. } else if (sge->length == 0 && sge->mr != NULL) {
  281. if (++sge->n >= IPATH_SEGSZ) {
  282. if (++sge->m >= sge->mr->mapsz)
  283. break;
  284. sge->n = 0;
  285. }
  286. sge->vaddr =
  287. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  288. sge->length =
  289. sge->mr->map[sge->m]->segs[sge->n].length;
  290. }
  291. data += len;
  292. length -= len;
  293. }
  294. }
  295. /**
  296. * ipath_post_one_send - post one RC, UC, or UD send work request
  297. * @qp: the QP to post on
  298. * @wr: the work request to send
  299. */
  300. static int ipath_post_one_send(struct ipath_qp *qp, struct ib_send_wr *wr)
  301. {
  302. struct ipath_swqe *wqe;
  303. u32 next;
  304. int i;
  305. int j;
  306. int acc;
  307. int ret;
  308. unsigned long flags;
  309. spin_lock_irqsave(&qp->s_lock, flags);
  310. /* Check that state is OK to post send. */
  311. if (unlikely(!(ib_ipath_state_ops[qp->state] & IPATH_POST_SEND_OK))) {
  312. if (qp->state != IB_QPS_SQE && qp->state != IB_QPS_ERR)
  313. goto bail_inval;
  314. /* C10-96 says generate a flushed completion entry. */
  315. ipath_flush_wqe(qp, wr);
  316. ret = 0;
  317. goto bail;
  318. }
  319. /* IB spec says that num_sge == 0 is OK. */
  320. if (wr->num_sge > qp->s_max_sge)
  321. goto bail_inval;
  322. /*
  323. * Don't allow RDMA reads or atomic operations on UC or
  324. * undefined operations.
  325. * Make sure buffer is large enough to hold the result for atomics.
  326. */
  327. if (qp->ibqp.qp_type == IB_QPT_UC) {
  328. if ((unsigned) wr->opcode >= IB_WR_RDMA_READ)
  329. goto bail_inval;
  330. } else if (qp->ibqp.qp_type == IB_QPT_UD) {
  331. /* Check UD opcode */
  332. if (wr->opcode != IB_WR_SEND &&
  333. wr->opcode != IB_WR_SEND_WITH_IMM)
  334. goto bail_inval;
  335. /* Check UD destination address PD */
  336. if (qp->ibqp.pd != wr->wr.ud.ah->pd)
  337. goto bail_inval;
  338. } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD)
  339. goto bail_inval;
  340. else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
  341. (wr->num_sge == 0 ||
  342. wr->sg_list[0].length < sizeof(u64) ||
  343. wr->sg_list[0].addr & (sizeof(u64) - 1)))
  344. goto bail_inval;
  345. else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic)
  346. goto bail_inval;
  347. next = qp->s_head + 1;
  348. if (next >= qp->s_size)
  349. next = 0;
  350. if (next == qp->s_last) {
  351. ret = -ENOMEM;
  352. goto bail;
  353. }
  354. wqe = get_swqe_ptr(qp, qp->s_head);
  355. wqe->wr = *wr;
  356. wqe->ssn = qp->s_ssn++;
  357. wqe->length = 0;
  358. if (wr->num_sge) {
  359. acc = wr->opcode >= IB_WR_RDMA_READ ?
  360. IB_ACCESS_LOCAL_WRITE : 0;
  361. for (i = 0, j = 0; i < wr->num_sge; i++) {
  362. u32 length = wr->sg_list[i].length;
  363. int ok;
  364. if (length == 0)
  365. continue;
  366. ok = ipath_lkey_ok(qp, &wqe->sg_list[j],
  367. &wr->sg_list[i], acc);
  368. if (!ok)
  369. goto bail_inval;
  370. wqe->length += length;
  371. j++;
  372. }
  373. wqe->wr.num_sge = j;
  374. }
  375. if (qp->ibqp.qp_type == IB_QPT_UC ||
  376. qp->ibqp.qp_type == IB_QPT_RC) {
  377. if (wqe->length > 0x80000000U)
  378. goto bail_inval;
  379. } else if (wqe->length > to_idev(qp->ibqp.device)->dd->ipath_ibmtu)
  380. goto bail_inval;
  381. qp->s_head = next;
  382. ret = 0;
  383. goto bail;
  384. bail_inval:
  385. ret = -EINVAL;
  386. bail:
  387. spin_unlock_irqrestore(&qp->s_lock, flags);
  388. return ret;
  389. }
  390. /**
  391. * ipath_post_send - post a send on a QP
  392. * @ibqp: the QP to post the send on
  393. * @wr: the list of work requests to post
  394. * @bad_wr: the first bad WR is put here
  395. *
  396. * This may be called from interrupt context.
  397. */
  398. static int ipath_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  399. struct ib_send_wr **bad_wr)
  400. {
  401. struct ipath_qp *qp = to_iqp(ibqp);
  402. int err = 0;
  403. for (; wr; wr = wr->next) {
  404. err = ipath_post_one_send(qp, wr);
  405. if (err) {
  406. *bad_wr = wr;
  407. goto bail;
  408. }
  409. }
  410. /* Try to do the send work in the caller's context. */
  411. ipath_do_send((unsigned long) qp);
  412. bail:
  413. return err;
  414. }
  415. /**
  416. * ipath_post_receive - post a receive on a QP
  417. * @ibqp: the QP to post the receive on
  418. * @wr: the WR to post
  419. * @bad_wr: the first bad WR is put here
  420. *
  421. * This may be called from interrupt context.
  422. */
  423. static int ipath_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  424. struct ib_recv_wr **bad_wr)
  425. {
  426. struct ipath_qp *qp = to_iqp(ibqp);
  427. struct ipath_rwq *wq = qp->r_rq.wq;
  428. unsigned long flags;
  429. int ret;
  430. /* Check that state is OK to post receive. */
  431. if (!(ib_ipath_state_ops[qp->state] & IPATH_POST_RECV_OK) || !wq) {
  432. *bad_wr = wr;
  433. ret = -EINVAL;
  434. goto bail;
  435. }
  436. for (; wr; wr = wr->next) {
  437. struct ipath_rwqe *wqe;
  438. u32 next;
  439. int i;
  440. if ((unsigned) wr->num_sge > qp->r_rq.max_sge) {
  441. *bad_wr = wr;
  442. ret = -EINVAL;
  443. goto bail;
  444. }
  445. spin_lock_irqsave(&qp->r_rq.lock, flags);
  446. next = wq->head + 1;
  447. if (next >= qp->r_rq.size)
  448. next = 0;
  449. if (next == wq->tail) {
  450. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  451. *bad_wr = wr;
  452. ret = -ENOMEM;
  453. goto bail;
  454. }
  455. wqe = get_rwqe_ptr(&qp->r_rq, wq->head);
  456. wqe->wr_id = wr->wr_id;
  457. wqe->num_sge = wr->num_sge;
  458. for (i = 0; i < wr->num_sge; i++)
  459. wqe->sg_list[i] = wr->sg_list[i];
  460. /* Make sure queue entry is written before the head index. */
  461. smp_wmb();
  462. wq->head = next;
  463. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  464. }
  465. ret = 0;
  466. bail:
  467. return ret;
  468. }
  469. /**
  470. * ipath_qp_rcv - processing an incoming packet on a QP
  471. * @dev: the device the packet came on
  472. * @hdr: the packet header
  473. * @has_grh: true if the packet has a GRH
  474. * @data: the packet data
  475. * @tlen: the packet length
  476. * @qp: the QP the packet came on
  477. *
  478. * This is called from ipath_ib_rcv() to process an incoming packet
  479. * for the given QP.
  480. * Called at interrupt level.
  481. */
  482. static void ipath_qp_rcv(struct ipath_ibdev *dev,
  483. struct ipath_ib_header *hdr, int has_grh,
  484. void *data, u32 tlen, struct ipath_qp *qp)
  485. {
  486. /* Check for valid receive state. */
  487. if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_RECV_OK)) {
  488. dev->n_pkt_drops++;
  489. return;
  490. }
  491. switch (qp->ibqp.qp_type) {
  492. case IB_QPT_SMI:
  493. case IB_QPT_GSI:
  494. if (ib_ipath_disable_sma)
  495. break;
  496. /* FALLTHROUGH */
  497. case IB_QPT_UD:
  498. ipath_ud_rcv(dev, hdr, has_grh, data, tlen, qp);
  499. break;
  500. case IB_QPT_RC:
  501. ipath_rc_rcv(dev, hdr, has_grh, data, tlen, qp);
  502. break;
  503. case IB_QPT_UC:
  504. ipath_uc_rcv(dev, hdr, has_grh, data, tlen, qp);
  505. break;
  506. default:
  507. break;
  508. }
  509. }
  510. /**
  511. * ipath_ib_rcv - process an incoming packet
  512. * @arg: the device pointer
  513. * @rhdr: the header of the packet
  514. * @data: the packet data
  515. * @tlen: the packet length
  516. *
  517. * This is called from ipath_kreceive() to process an incoming packet at
  518. * interrupt level. Tlen is the length of the header + data + CRC in bytes.
  519. */
  520. void ipath_ib_rcv(struct ipath_ibdev *dev, void *rhdr, void *data,
  521. u32 tlen)
  522. {
  523. struct ipath_ib_header *hdr = rhdr;
  524. struct ipath_other_headers *ohdr;
  525. struct ipath_qp *qp;
  526. u32 qp_num;
  527. int lnh;
  528. u8 opcode;
  529. u16 lid;
  530. if (unlikely(dev == NULL))
  531. goto bail;
  532. if (unlikely(tlen < 24)) { /* LRH+BTH+CRC */
  533. dev->rcv_errors++;
  534. goto bail;
  535. }
  536. /* Check for a valid destination LID (see ch. 7.11.1). */
  537. lid = be16_to_cpu(hdr->lrh[1]);
  538. if (lid < IPATH_MULTICAST_LID_BASE) {
  539. lid &= ~((1 << dev->dd->ipath_lmc) - 1);
  540. if (unlikely(lid != dev->dd->ipath_lid)) {
  541. dev->rcv_errors++;
  542. goto bail;
  543. }
  544. }
  545. /* Check for GRH */
  546. lnh = be16_to_cpu(hdr->lrh[0]) & 3;
  547. if (lnh == IPATH_LRH_BTH)
  548. ohdr = &hdr->u.oth;
  549. else if (lnh == IPATH_LRH_GRH)
  550. ohdr = &hdr->u.l.oth;
  551. else {
  552. dev->rcv_errors++;
  553. goto bail;
  554. }
  555. opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
  556. dev->opstats[opcode].n_bytes += tlen;
  557. dev->opstats[opcode].n_packets++;
  558. /* Get the destination QP number. */
  559. qp_num = be32_to_cpu(ohdr->bth[1]) & IPATH_QPN_MASK;
  560. if (qp_num == IPATH_MULTICAST_QPN) {
  561. struct ipath_mcast *mcast;
  562. struct ipath_mcast_qp *p;
  563. if (lnh != IPATH_LRH_GRH) {
  564. dev->n_pkt_drops++;
  565. goto bail;
  566. }
  567. mcast = ipath_mcast_find(&hdr->u.l.grh.dgid);
  568. if (mcast == NULL) {
  569. dev->n_pkt_drops++;
  570. goto bail;
  571. }
  572. dev->n_multicast_rcv++;
  573. list_for_each_entry_rcu(p, &mcast->qp_list, list)
  574. ipath_qp_rcv(dev, hdr, 1, data, tlen, p->qp);
  575. /*
  576. * Notify ipath_multicast_detach() if it is waiting for us
  577. * to finish.
  578. */
  579. if (atomic_dec_return(&mcast->refcount) <= 1)
  580. wake_up(&mcast->wait);
  581. } else {
  582. qp = ipath_lookup_qpn(&dev->qp_table, qp_num);
  583. if (qp) {
  584. dev->n_unicast_rcv++;
  585. ipath_qp_rcv(dev, hdr, lnh == IPATH_LRH_GRH, data,
  586. tlen, qp);
  587. /*
  588. * Notify ipath_destroy_qp() if it is waiting
  589. * for us to finish.
  590. */
  591. if (atomic_dec_and_test(&qp->refcount))
  592. wake_up(&qp->wait);
  593. } else
  594. dev->n_pkt_drops++;
  595. }
  596. bail:;
  597. }
  598. /**
  599. * ipath_ib_timer - verbs timer
  600. * @arg: the device pointer
  601. *
  602. * This is called from ipath_do_rcv_timer() at interrupt level to check for
  603. * QPs which need retransmits and to collect performance numbers.
  604. */
  605. static void ipath_ib_timer(struct ipath_ibdev *dev)
  606. {
  607. struct ipath_qp *resend = NULL;
  608. struct list_head *last;
  609. struct ipath_qp *qp;
  610. unsigned long flags;
  611. if (dev == NULL)
  612. return;
  613. spin_lock_irqsave(&dev->pending_lock, flags);
  614. /* Start filling the next pending queue. */
  615. if (++dev->pending_index >= ARRAY_SIZE(dev->pending))
  616. dev->pending_index = 0;
  617. /* Save any requests still in the new queue, they have timed out. */
  618. last = &dev->pending[dev->pending_index];
  619. while (!list_empty(last)) {
  620. qp = list_entry(last->next, struct ipath_qp, timerwait);
  621. list_del_init(&qp->timerwait);
  622. qp->timer_next = resend;
  623. resend = qp;
  624. atomic_inc(&qp->refcount);
  625. }
  626. last = &dev->rnrwait;
  627. if (!list_empty(last)) {
  628. qp = list_entry(last->next, struct ipath_qp, timerwait);
  629. if (--qp->s_rnr_timeout == 0) {
  630. do {
  631. list_del_init(&qp->timerwait);
  632. tasklet_hi_schedule(&qp->s_task);
  633. if (list_empty(last))
  634. break;
  635. qp = list_entry(last->next, struct ipath_qp,
  636. timerwait);
  637. } while (qp->s_rnr_timeout == 0);
  638. }
  639. }
  640. /*
  641. * We should only be in the started state if pma_sample_start != 0
  642. */
  643. if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_STARTED &&
  644. --dev->pma_sample_start == 0) {
  645. dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_RUNNING;
  646. ipath_snapshot_counters(dev->dd, &dev->ipath_sword,
  647. &dev->ipath_rword,
  648. &dev->ipath_spkts,
  649. &dev->ipath_rpkts,
  650. &dev->ipath_xmit_wait);
  651. }
  652. if (dev->pma_sample_status == IB_PMA_SAMPLE_STATUS_RUNNING) {
  653. if (dev->pma_sample_interval == 0) {
  654. u64 ta, tb, tc, td, te;
  655. dev->pma_sample_status = IB_PMA_SAMPLE_STATUS_DONE;
  656. ipath_snapshot_counters(dev->dd, &ta, &tb,
  657. &tc, &td, &te);
  658. dev->ipath_sword = ta - dev->ipath_sword;
  659. dev->ipath_rword = tb - dev->ipath_rword;
  660. dev->ipath_spkts = tc - dev->ipath_spkts;
  661. dev->ipath_rpkts = td - dev->ipath_rpkts;
  662. dev->ipath_xmit_wait = te - dev->ipath_xmit_wait;
  663. }
  664. else
  665. dev->pma_sample_interval--;
  666. }
  667. spin_unlock_irqrestore(&dev->pending_lock, flags);
  668. /* XXX What if timer fires again while this is running? */
  669. for (qp = resend; qp != NULL; qp = qp->timer_next) {
  670. struct ib_wc wc;
  671. spin_lock_irqsave(&qp->s_lock, flags);
  672. if (qp->s_last != qp->s_tail && qp->state == IB_QPS_RTS) {
  673. dev->n_timeouts++;
  674. ipath_restart_rc(qp, qp->s_last_psn + 1, &wc);
  675. }
  676. spin_unlock_irqrestore(&qp->s_lock, flags);
  677. /* Notify ipath_destroy_qp() if it is waiting. */
  678. if (atomic_dec_and_test(&qp->refcount))
  679. wake_up(&qp->wait);
  680. }
  681. }
  682. static void update_sge(struct ipath_sge_state *ss, u32 length)
  683. {
  684. struct ipath_sge *sge = &ss->sge;
  685. sge->vaddr += length;
  686. sge->length -= length;
  687. sge->sge_length -= length;
  688. if (sge->sge_length == 0) {
  689. if (--ss->num_sge)
  690. *sge = *ss->sg_list++;
  691. } else if (sge->length == 0 && sge->mr != NULL) {
  692. if (++sge->n >= IPATH_SEGSZ) {
  693. if (++sge->m >= sge->mr->mapsz)
  694. return;
  695. sge->n = 0;
  696. }
  697. sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
  698. sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
  699. }
  700. }
  701. #ifdef __LITTLE_ENDIAN
  702. static inline u32 get_upper_bits(u32 data, u32 shift)
  703. {
  704. return data >> shift;
  705. }
  706. static inline u32 set_upper_bits(u32 data, u32 shift)
  707. {
  708. return data << shift;
  709. }
  710. static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
  711. {
  712. data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
  713. data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
  714. return data;
  715. }
  716. #else
  717. static inline u32 get_upper_bits(u32 data, u32 shift)
  718. {
  719. return data << shift;
  720. }
  721. static inline u32 set_upper_bits(u32 data, u32 shift)
  722. {
  723. return data >> shift;
  724. }
  725. static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
  726. {
  727. data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
  728. data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
  729. return data;
  730. }
  731. #endif
  732. static void copy_io(u32 __iomem *piobuf, struct ipath_sge_state *ss,
  733. u32 length, unsigned flush_wc)
  734. {
  735. u32 extra = 0;
  736. u32 data = 0;
  737. u32 last;
  738. while (1) {
  739. u32 len = ss->sge.length;
  740. u32 off;
  741. if (len > length)
  742. len = length;
  743. if (len > ss->sge.sge_length)
  744. len = ss->sge.sge_length;
  745. BUG_ON(len == 0);
  746. /* If the source address is not aligned, try to align it. */
  747. off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
  748. if (off) {
  749. u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
  750. ~(sizeof(u32) - 1));
  751. u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
  752. u32 y;
  753. y = sizeof(u32) - off;
  754. if (len > y)
  755. len = y;
  756. if (len + extra >= sizeof(u32)) {
  757. data |= set_upper_bits(v, extra *
  758. BITS_PER_BYTE);
  759. len = sizeof(u32) - extra;
  760. if (len == length) {
  761. last = data;
  762. break;
  763. }
  764. __raw_writel(data, piobuf);
  765. piobuf++;
  766. extra = 0;
  767. data = 0;
  768. } else {
  769. /* Clear unused upper bytes */
  770. data |= clear_upper_bytes(v, len, extra);
  771. if (len == length) {
  772. last = data;
  773. break;
  774. }
  775. extra += len;
  776. }
  777. } else if (extra) {
  778. /* Source address is aligned. */
  779. u32 *addr = (u32 *) ss->sge.vaddr;
  780. int shift = extra * BITS_PER_BYTE;
  781. int ushift = 32 - shift;
  782. u32 l = len;
  783. while (l >= sizeof(u32)) {
  784. u32 v = *addr;
  785. data |= set_upper_bits(v, shift);
  786. __raw_writel(data, piobuf);
  787. data = get_upper_bits(v, ushift);
  788. piobuf++;
  789. addr++;
  790. l -= sizeof(u32);
  791. }
  792. /*
  793. * We still have 'extra' number of bytes leftover.
  794. */
  795. if (l) {
  796. u32 v = *addr;
  797. if (l + extra >= sizeof(u32)) {
  798. data |= set_upper_bits(v, shift);
  799. len -= l + extra - sizeof(u32);
  800. if (len == length) {
  801. last = data;
  802. break;
  803. }
  804. __raw_writel(data, piobuf);
  805. piobuf++;
  806. extra = 0;
  807. data = 0;
  808. } else {
  809. /* Clear unused upper bytes */
  810. data |= clear_upper_bytes(v, l,
  811. extra);
  812. if (len == length) {
  813. last = data;
  814. break;
  815. }
  816. extra += l;
  817. }
  818. } else if (len == length) {
  819. last = data;
  820. break;
  821. }
  822. } else if (len == length) {
  823. u32 w;
  824. /*
  825. * Need to round up for the last dword in the
  826. * packet.
  827. */
  828. w = (len + 3) >> 2;
  829. __iowrite32_copy(piobuf, ss->sge.vaddr, w - 1);
  830. piobuf += w - 1;
  831. last = ((u32 *) ss->sge.vaddr)[w - 1];
  832. break;
  833. } else {
  834. u32 w = len >> 2;
  835. __iowrite32_copy(piobuf, ss->sge.vaddr, w);
  836. piobuf += w;
  837. extra = len & (sizeof(u32) - 1);
  838. if (extra) {
  839. u32 v = ((u32 *) ss->sge.vaddr)[w];
  840. /* Clear unused upper bytes */
  841. data = clear_upper_bytes(v, extra, 0);
  842. }
  843. }
  844. update_sge(ss, len);
  845. length -= len;
  846. }
  847. /* Update address before sending packet. */
  848. update_sge(ss, length);
  849. if (flush_wc) {
  850. /* must flush early everything before trigger word */
  851. ipath_flush_wc();
  852. __raw_writel(last, piobuf);
  853. /* be sure trigger word is written */
  854. ipath_flush_wc();
  855. } else
  856. __raw_writel(last, piobuf);
  857. }
  858. /*
  859. * Convert IB rate to delay multiplier.
  860. */
  861. unsigned ipath_ib_rate_to_mult(enum ib_rate rate)
  862. {
  863. switch (rate) {
  864. case IB_RATE_2_5_GBPS: return 8;
  865. case IB_RATE_5_GBPS: return 4;
  866. case IB_RATE_10_GBPS: return 2;
  867. case IB_RATE_20_GBPS: return 1;
  868. default: return 0;
  869. }
  870. }
  871. /*
  872. * Convert delay multiplier to IB rate
  873. */
  874. static enum ib_rate ipath_mult_to_ib_rate(unsigned mult)
  875. {
  876. switch (mult) {
  877. case 8: return IB_RATE_2_5_GBPS;
  878. case 4: return IB_RATE_5_GBPS;
  879. case 2: return IB_RATE_10_GBPS;
  880. case 1: return IB_RATE_20_GBPS;
  881. default: return IB_RATE_PORT_CURRENT;
  882. }
  883. }
  884. static inline struct ipath_verbs_txreq *get_txreq(struct ipath_ibdev *dev)
  885. {
  886. struct ipath_verbs_txreq *tx = NULL;
  887. unsigned long flags;
  888. spin_lock_irqsave(&dev->pending_lock, flags);
  889. if (!list_empty(&dev->txreq_free)) {
  890. struct list_head *l = dev->txreq_free.next;
  891. list_del(l);
  892. tx = list_entry(l, struct ipath_verbs_txreq, txreq.list);
  893. }
  894. spin_unlock_irqrestore(&dev->pending_lock, flags);
  895. return tx;
  896. }
  897. static inline void put_txreq(struct ipath_ibdev *dev,
  898. struct ipath_verbs_txreq *tx)
  899. {
  900. unsigned long flags;
  901. spin_lock_irqsave(&dev->pending_lock, flags);
  902. list_add(&tx->txreq.list, &dev->txreq_free);
  903. spin_unlock_irqrestore(&dev->pending_lock, flags);
  904. }
  905. static void sdma_complete(void *cookie, int status)
  906. {
  907. struct ipath_verbs_txreq *tx = cookie;
  908. struct ipath_qp *qp = tx->qp;
  909. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  910. /* Generate a completion queue entry if needed */
  911. if (qp->ibqp.qp_type != IB_QPT_RC && tx->wqe) {
  912. enum ib_wc_status ibs = status == IPATH_SDMA_TXREQ_S_OK ?
  913. IB_WC_SUCCESS : IB_WC_WR_FLUSH_ERR;
  914. ipath_send_complete(qp, tx->wqe, ibs);
  915. }
  916. if (tx->txreq.flags & IPATH_SDMA_TXREQ_F_FREEBUF)
  917. kfree(tx->txreq.map_addr);
  918. put_txreq(dev, tx);
  919. if (atomic_dec_and_test(&qp->refcount))
  920. wake_up(&qp->wait);
  921. }
  922. /*
  923. * Compute the number of clock cycles of delay before sending the next packet.
  924. * The multipliers reflect the number of clocks for the fastest rate so
  925. * one tick at 4xDDR is 8 ticks at 1xSDR.
  926. * If the destination port will take longer to receive a packet than
  927. * the outgoing link can send it, we need to delay sending the next packet
  928. * by the difference in time it takes the receiver to receive and the sender
  929. * to send this packet.
  930. * Note that this delay is always correct for UC and RC but not always
  931. * optimal for UD. For UD, the destination HCA can be different for each
  932. * packet, in which case, we could send packets to a different destination
  933. * while "waiting" for the delay. The overhead for doing this without
  934. * HW support is more than just paying the cost of delaying some packets
  935. * unnecessarily.
  936. */
  937. static inline unsigned ipath_pkt_delay(u32 plen, u8 snd_mult, u8 rcv_mult)
  938. {
  939. return (rcv_mult > snd_mult) ?
  940. (plen * (rcv_mult - snd_mult) + 1) >> 1 : 0;
  941. }
  942. static int ipath_verbs_send_dma(struct ipath_qp *qp,
  943. struct ipath_ib_header *hdr, u32 hdrwords,
  944. struct ipath_sge_state *ss, u32 len,
  945. u32 plen, u32 dwords)
  946. {
  947. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  948. struct ipath_devdata *dd = dev->dd;
  949. struct ipath_verbs_txreq *tx;
  950. u32 *piobuf;
  951. u32 control;
  952. u32 ndesc;
  953. int ret;
  954. tx = qp->s_tx;
  955. if (tx) {
  956. qp->s_tx = NULL;
  957. /* resend previously constructed packet */
  958. ret = ipath_sdma_verbs_send(dd, tx->ss, tx->len, tx);
  959. if (ret)
  960. qp->s_tx = tx;
  961. goto bail;
  962. }
  963. tx = get_txreq(dev);
  964. if (!tx) {
  965. ret = -EBUSY;
  966. goto bail;
  967. }
  968. /*
  969. * Get the saved delay count we computed for the previous packet
  970. * and save the delay count for this packet to be used next time
  971. * we get here.
  972. */
  973. control = qp->s_pkt_delay;
  974. qp->s_pkt_delay = ipath_pkt_delay(plen, dd->delay_mult, qp->s_dmult);
  975. tx->qp = qp;
  976. atomic_inc(&qp->refcount);
  977. tx->wqe = qp->s_wqe;
  978. tx->txreq.callback = sdma_complete;
  979. tx->txreq.callback_cookie = tx;
  980. tx->txreq.flags = IPATH_SDMA_TXREQ_F_HEADTOHOST |
  981. IPATH_SDMA_TXREQ_F_INTREQ | IPATH_SDMA_TXREQ_F_FREEDESC;
  982. if (plen + 1 >= IPATH_SMALLBUF_DWORDS)
  983. tx->txreq.flags |= IPATH_SDMA_TXREQ_F_USELARGEBUF;
  984. /* VL15 packets bypass credit check */
  985. if ((be16_to_cpu(hdr->lrh[0]) >> 12) == 15) {
  986. control |= 1ULL << 31;
  987. tx->txreq.flags |= IPATH_SDMA_TXREQ_F_VL15;
  988. }
  989. if (len) {
  990. /*
  991. * Don't try to DMA if it takes more descriptors than
  992. * the queue holds.
  993. */
  994. ndesc = ipath_count_sge(ss, len);
  995. if (ndesc >= dd->ipath_sdma_descq_cnt)
  996. ndesc = 0;
  997. } else
  998. ndesc = 1;
  999. if (ndesc) {
  1000. tx->hdr.pbc[0] = cpu_to_le32(plen);
  1001. tx->hdr.pbc[1] = cpu_to_le32(control);
  1002. memcpy(&tx->hdr.hdr, hdr, hdrwords << 2);
  1003. tx->txreq.sg_count = ndesc;
  1004. tx->map_len = (hdrwords + 2) << 2;
  1005. tx->txreq.map_addr = &tx->hdr;
  1006. ret = ipath_sdma_verbs_send(dd, ss, dwords, tx);
  1007. if (ret) {
  1008. /* save ss and length in dwords */
  1009. tx->ss = ss;
  1010. tx->len = dwords;
  1011. qp->s_tx = tx;
  1012. }
  1013. goto bail;
  1014. }
  1015. /* Allocate a buffer and copy the header and payload to it. */
  1016. tx->map_len = (plen + 1) << 2;
  1017. piobuf = kmalloc(tx->map_len, GFP_ATOMIC);
  1018. if (unlikely(piobuf == NULL)) {
  1019. ret = -EBUSY;
  1020. goto err_tx;
  1021. }
  1022. tx->txreq.map_addr = piobuf;
  1023. tx->txreq.flags |= IPATH_SDMA_TXREQ_F_FREEBUF;
  1024. tx->txreq.sg_count = 1;
  1025. *piobuf++ = (__force u32) cpu_to_le32(plen);
  1026. *piobuf++ = (__force u32) cpu_to_le32(control);
  1027. memcpy(piobuf, hdr, hdrwords << 2);
  1028. ipath_copy_from_sge(piobuf + hdrwords, ss, len);
  1029. ret = ipath_sdma_verbs_send(dd, NULL, 0, tx);
  1030. /*
  1031. * If we couldn't queue the DMA request, save the info
  1032. * and try again later rather than destroying the
  1033. * buffer and undoing the side effects of the copy.
  1034. */
  1035. if (ret) {
  1036. tx->ss = NULL;
  1037. tx->len = 0;
  1038. qp->s_tx = tx;
  1039. }
  1040. dev->n_unaligned++;
  1041. goto bail;
  1042. err_tx:
  1043. if (atomic_dec_and_test(&qp->refcount))
  1044. wake_up(&qp->wait);
  1045. put_txreq(dev, tx);
  1046. bail:
  1047. return ret;
  1048. }
  1049. static int ipath_verbs_send_pio(struct ipath_qp *qp,
  1050. struct ipath_ib_header *ibhdr, u32 hdrwords,
  1051. struct ipath_sge_state *ss, u32 len,
  1052. u32 plen, u32 dwords)
  1053. {
  1054. struct ipath_devdata *dd = to_idev(qp->ibqp.device)->dd;
  1055. u32 *hdr = (u32 *) ibhdr;
  1056. u32 __iomem *piobuf;
  1057. unsigned flush_wc;
  1058. u32 control;
  1059. int ret;
  1060. piobuf = ipath_getpiobuf(dd, plen, NULL);
  1061. if (unlikely(piobuf == NULL)) {
  1062. ret = -EBUSY;
  1063. goto bail;
  1064. }
  1065. /*
  1066. * Get the saved delay count we computed for the previous packet
  1067. * and save the delay count for this packet to be used next time
  1068. * we get here.
  1069. */
  1070. control = qp->s_pkt_delay;
  1071. qp->s_pkt_delay = ipath_pkt_delay(plen, dd->delay_mult, qp->s_dmult);
  1072. /* VL15 packets bypass credit check */
  1073. if ((be16_to_cpu(ibhdr->lrh[0]) >> 12) == 15)
  1074. control |= 1ULL << 31;
  1075. /*
  1076. * Write the length to the control qword plus any needed flags.
  1077. * We have to flush after the PBC for correctness on some cpus
  1078. * or WC buffer can be written out of order.
  1079. */
  1080. writeq(((u64) control << 32) | plen, piobuf);
  1081. piobuf += 2;
  1082. flush_wc = dd->ipath_flags & IPATH_PIO_FLUSH_WC;
  1083. if (len == 0) {
  1084. /*
  1085. * If there is just the header portion, must flush before
  1086. * writing last word of header for correctness, and after
  1087. * the last header word (trigger word).
  1088. */
  1089. if (flush_wc) {
  1090. ipath_flush_wc();
  1091. __iowrite32_copy(piobuf, hdr, hdrwords - 1);
  1092. ipath_flush_wc();
  1093. __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
  1094. ipath_flush_wc();
  1095. } else
  1096. __iowrite32_copy(piobuf, hdr, hdrwords);
  1097. goto done;
  1098. }
  1099. if (flush_wc)
  1100. ipath_flush_wc();
  1101. __iowrite32_copy(piobuf, hdr, hdrwords);
  1102. piobuf += hdrwords;
  1103. /* The common case is aligned and contained in one segment. */
  1104. if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
  1105. !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
  1106. u32 *addr = (u32 *) ss->sge.vaddr;
  1107. /* Update address before sending packet. */
  1108. update_sge(ss, len);
  1109. if (flush_wc) {
  1110. __iowrite32_copy(piobuf, addr, dwords - 1);
  1111. /* must flush early everything before trigger word */
  1112. ipath_flush_wc();
  1113. __raw_writel(addr[dwords - 1], piobuf + dwords - 1);
  1114. /* be sure trigger word is written */
  1115. ipath_flush_wc();
  1116. } else
  1117. __iowrite32_copy(piobuf, addr, dwords);
  1118. goto done;
  1119. }
  1120. copy_io(piobuf, ss, len, flush_wc);
  1121. done:
  1122. if (qp->s_wqe)
  1123. ipath_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
  1124. ret = 0;
  1125. bail:
  1126. return ret;
  1127. }
  1128. /**
  1129. * ipath_verbs_send - send a packet
  1130. * @qp: the QP to send on
  1131. * @hdr: the packet header
  1132. * @hdrwords: the number of 32-bit words in the header
  1133. * @ss: the SGE to send
  1134. * @len: the length of the packet in bytes
  1135. */
  1136. int ipath_verbs_send(struct ipath_qp *qp, struct ipath_ib_header *hdr,
  1137. u32 hdrwords, struct ipath_sge_state *ss, u32 len)
  1138. {
  1139. struct ipath_devdata *dd = to_idev(qp->ibqp.device)->dd;
  1140. u32 plen;
  1141. int ret;
  1142. u32 dwords = (len + 3) >> 2;
  1143. /*
  1144. * Calculate the send buffer trigger address.
  1145. * The +1 counts for the pbc control dword following the pbc length.
  1146. */
  1147. plen = hdrwords + dwords + 1;
  1148. /*
  1149. * VL15 packets (IB_QPT_SMI) will always use PIO, so we
  1150. * can defer SDMA restart until link goes ACTIVE without
  1151. * worrying about just how we got there.
  1152. */
  1153. if (qp->ibqp.qp_type == IB_QPT_SMI)
  1154. ret = ipath_verbs_send_pio(qp, hdr, hdrwords, ss, len,
  1155. plen, dwords);
  1156. /* All non-VL15 packets are dropped if link is not ACTIVE */
  1157. else if (!(dd->ipath_flags & IPATH_LINKACTIVE)) {
  1158. if (qp->s_wqe)
  1159. ipath_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
  1160. ret = 0;
  1161. } else if (dd->ipath_flags & IPATH_HAS_SEND_DMA)
  1162. ret = ipath_verbs_send_dma(qp, hdr, hdrwords, ss, len,
  1163. plen, dwords);
  1164. else
  1165. ret = ipath_verbs_send_pio(qp, hdr, hdrwords, ss, len,
  1166. plen, dwords);
  1167. return ret;
  1168. }
  1169. int ipath_snapshot_counters(struct ipath_devdata *dd, u64 *swords,
  1170. u64 *rwords, u64 *spkts, u64 *rpkts,
  1171. u64 *xmit_wait)
  1172. {
  1173. int ret;
  1174. if (!(dd->ipath_flags & IPATH_INITTED)) {
  1175. /* no hardware, freeze, etc. */
  1176. ret = -EINVAL;
  1177. goto bail;
  1178. }
  1179. *swords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordsendcnt);
  1180. *rwords = ipath_snap_cntr(dd, dd->ipath_cregs->cr_wordrcvcnt);
  1181. *spkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktsendcnt);
  1182. *rpkts = ipath_snap_cntr(dd, dd->ipath_cregs->cr_pktrcvcnt);
  1183. *xmit_wait = ipath_snap_cntr(dd, dd->ipath_cregs->cr_sendstallcnt);
  1184. ret = 0;
  1185. bail:
  1186. return ret;
  1187. }
  1188. /**
  1189. * ipath_get_counters - get various chip counters
  1190. * @dd: the infinipath device
  1191. * @cntrs: counters are placed here
  1192. *
  1193. * Return the counters needed by recv_pma_get_portcounters().
  1194. */
  1195. int ipath_get_counters(struct ipath_devdata *dd,
  1196. struct ipath_verbs_counters *cntrs)
  1197. {
  1198. struct ipath_cregs const *crp = dd->ipath_cregs;
  1199. int ret;
  1200. if (!(dd->ipath_flags & IPATH_INITTED)) {
  1201. /* no hardware, freeze, etc. */
  1202. ret = -EINVAL;
  1203. goto bail;
  1204. }
  1205. cntrs->symbol_error_counter =
  1206. ipath_snap_cntr(dd, crp->cr_ibsymbolerrcnt);
  1207. cntrs->link_error_recovery_counter =
  1208. ipath_snap_cntr(dd, crp->cr_iblinkerrrecovcnt);
  1209. /*
  1210. * The link downed counter counts when the other side downs the
  1211. * connection. We add in the number of times we downed the link
  1212. * due to local link integrity errors to compensate.
  1213. */
  1214. cntrs->link_downed_counter =
  1215. ipath_snap_cntr(dd, crp->cr_iblinkdowncnt);
  1216. cntrs->port_rcv_errors =
  1217. ipath_snap_cntr(dd, crp->cr_rxdroppktcnt) +
  1218. ipath_snap_cntr(dd, crp->cr_rcvovflcnt) +
  1219. ipath_snap_cntr(dd, crp->cr_portovflcnt) +
  1220. ipath_snap_cntr(dd, crp->cr_err_rlencnt) +
  1221. ipath_snap_cntr(dd, crp->cr_invalidrlencnt) +
  1222. ipath_snap_cntr(dd, crp->cr_errlinkcnt) +
  1223. ipath_snap_cntr(dd, crp->cr_erricrccnt) +
  1224. ipath_snap_cntr(dd, crp->cr_errvcrccnt) +
  1225. ipath_snap_cntr(dd, crp->cr_errlpcrccnt) +
  1226. ipath_snap_cntr(dd, crp->cr_badformatcnt) +
  1227. dd->ipath_rxfc_unsupvl_errs;
  1228. if (crp->cr_rxotherlocalphyerrcnt)
  1229. cntrs->port_rcv_errors +=
  1230. ipath_snap_cntr(dd, crp->cr_rxotherlocalphyerrcnt);
  1231. if (crp->cr_rxvlerrcnt)
  1232. cntrs->port_rcv_errors +=
  1233. ipath_snap_cntr(dd, crp->cr_rxvlerrcnt);
  1234. cntrs->port_rcv_remphys_errors =
  1235. ipath_snap_cntr(dd, crp->cr_rcvebpcnt);
  1236. cntrs->port_xmit_discards = ipath_snap_cntr(dd, crp->cr_unsupvlcnt);
  1237. cntrs->port_xmit_data = ipath_snap_cntr(dd, crp->cr_wordsendcnt);
  1238. cntrs->port_rcv_data = ipath_snap_cntr(dd, crp->cr_wordrcvcnt);
  1239. cntrs->port_xmit_packets = ipath_snap_cntr(dd, crp->cr_pktsendcnt);
  1240. cntrs->port_rcv_packets = ipath_snap_cntr(dd, crp->cr_pktrcvcnt);
  1241. cntrs->local_link_integrity_errors =
  1242. crp->cr_locallinkintegrityerrcnt ?
  1243. ipath_snap_cntr(dd, crp->cr_locallinkintegrityerrcnt) :
  1244. ((dd->ipath_flags & IPATH_GPIO_ERRINTRS) ?
  1245. dd->ipath_lli_errs : dd->ipath_lli_errors);
  1246. cntrs->excessive_buffer_overrun_errors =
  1247. crp->cr_excessbufferovflcnt ?
  1248. ipath_snap_cntr(dd, crp->cr_excessbufferovflcnt) :
  1249. dd->ipath_overrun_thresh_errs;
  1250. cntrs->vl15_dropped = crp->cr_vl15droppedpktcnt ?
  1251. ipath_snap_cntr(dd, crp->cr_vl15droppedpktcnt) : 0;
  1252. ret = 0;
  1253. bail:
  1254. return ret;
  1255. }
  1256. /**
  1257. * ipath_ib_piobufavail - callback when a PIO buffer is available
  1258. * @arg: the device pointer
  1259. *
  1260. * This is called from ipath_intr() at interrupt level when a PIO buffer is
  1261. * available after ipath_verbs_send() returned an error that no buffers were
  1262. * available. Return 1 if we consumed all the PIO buffers and we still have
  1263. * QPs waiting for buffers (for now, just do a tasklet_hi_schedule and
  1264. * return zero).
  1265. */
  1266. int ipath_ib_piobufavail(struct ipath_ibdev *dev)
  1267. {
  1268. struct ipath_qp *qp;
  1269. unsigned long flags;
  1270. if (dev == NULL)
  1271. goto bail;
  1272. spin_lock_irqsave(&dev->pending_lock, flags);
  1273. while (!list_empty(&dev->piowait)) {
  1274. qp = list_entry(dev->piowait.next, struct ipath_qp,
  1275. piowait);
  1276. list_del_init(&qp->piowait);
  1277. clear_bit(IPATH_S_BUSY, &qp->s_busy);
  1278. tasklet_hi_schedule(&qp->s_task);
  1279. }
  1280. spin_unlock_irqrestore(&dev->pending_lock, flags);
  1281. bail:
  1282. return 0;
  1283. }
  1284. static int ipath_query_device(struct ib_device *ibdev,
  1285. struct ib_device_attr *props)
  1286. {
  1287. struct ipath_ibdev *dev = to_idev(ibdev);
  1288. memset(props, 0, sizeof(*props));
  1289. props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
  1290. IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
  1291. IB_DEVICE_SYS_IMAGE_GUID;
  1292. props->page_size_cap = PAGE_SIZE;
  1293. props->vendor_id = dev->dd->ipath_vendorid;
  1294. props->vendor_part_id = dev->dd->ipath_deviceid;
  1295. props->hw_ver = dev->dd->ipath_pcirev;
  1296. props->sys_image_guid = dev->sys_image_guid;
  1297. props->max_mr_size = ~0ull;
  1298. props->max_qp = ib_ipath_max_qps;
  1299. props->max_qp_wr = ib_ipath_max_qp_wrs;
  1300. props->max_sge = ib_ipath_max_sges;
  1301. props->max_cq = ib_ipath_max_cqs;
  1302. props->max_ah = ib_ipath_max_ahs;
  1303. props->max_cqe = ib_ipath_max_cqes;
  1304. props->max_mr = dev->lk_table.max;
  1305. props->max_fmr = dev->lk_table.max;
  1306. props->max_map_per_fmr = 32767;
  1307. props->max_pd = ib_ipath_max_pds;
  1308. props->max_qp_rd_atom = IPATH_MAX_RDMA_ATOMIC;
  1309. props->max_qp_init_rd_atom = 255;
  1310. /* props->max_res_rd_atom */
  1311. props->max_srq = ib_ipath_max_srqs;
  1312. props->max_srq_wr = ib_ipath_max_srq_wrs;
  1313. props->max_srq_sge = ib_ipath_max_srq_sges;
  1314. /* props->local_ca_ack_delay */
  1315. props->atomic_cap = IB_ATOMIC_GLOB;
  1316. props->max_pkeys = ipath_get_npkeys(dev->dd);
  1317. props->max_mcast_grp = ib_ipath_max_mcast_grps;
  1318. props->max_mcast_qp_attach = ib_ipath_max_mcast_qp_attached;
  1319. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  1320. props->max_mcast_grp;
  1321. return 0;
  1322. }
  1323. const u8 ipath_cvt_physportstate[32] = {
  1324. [INFINIPATH_IBCS_LT_STATE_DISABLED] = IB_PHYSPORTSTATE_DISABLED,
  1325. [INFINIPATH_IBCS_LT_STATE_LINKUP] = IB_PHYSPORTSTATE_LINKUP,
  1326. [INFINIPATH_IBCS_LT_STATE_POLLACTIVE] = IB_PHYSPORTSTATE_POLL,
  1327. [INFINIPATH_IBCS_LT_STATE_POLLQUIET] = IB_PHYSPORTSTATE_POLL,
  1328. [INFINIPATH_IBCS_LT_STATE_SLEEPDELAY] = IB_PHYSPORTSTATE_SLEEP,
  1329. [INFINIPATH_IBCS_LT_STATE_SLEEPQUIET] = IB_PHYSPORTSTATE_SLEEP,
  1330. [INFINIPATH_IBCS_LT_STATE_CFGDEBOUNCE] =
  1331. IB_PHYSPORTSTATE_CFG_TRAIN,
  1332. [INFINIPATH_IBCS_LT_STATE_CFGRCVFCFG] =
  1333. IB_PHYSPORTSTATE_CFG_TRAIN,
  1334. [INFINIPATH_IBCS_LT_STATE_CFGWAITRMT] =
  1335. IB_PHYSPORTSTATE_CFG_TRAIN,
  1336. [INFINIPATH_IBCS_LT_STATE_CFGIDLE] = IB_PHYSPORTSTATE_CFG_TRAIN,
  1337. [INFINIPATH_IBCS_LT_STATE_RECOVERRETRAIN] =
  1338. IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
  1339. [INFINIPATH_IBCS_LT_STATE_RECOVERWAITRMT] =
  1340. IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
  1341. [INFINIPATH_IBCS_LT_STATE_RECOVERIDLE] =
  1342. IB_PHYSPORTSTATE_LINK_ERR_RECOVER,
  1343. [0x10] = IB_PHYSPORTSTATE_CFG_TRAIN,
  1344. [0x11] = IB_PHYSPORTSTATE_CFG_TRAIN,
  1345. [0x12] = IB_PHYSPORTSTATE_CFG_TRAIN,
  1346. [0x13] = IB_PHYSPORTSTATE_CFG_TRAIN,
  1347. [0x14] = IB_PHYSPORTSTATE_CFG_TRAIN,
  1348. [0x15] = IB_PHYSPORTSTATE_CFG_TRAIN,
  1349. [0x16] = IB_PHYSPORTSTATE_CFG_TRAIN,
  1350. [0x17] = IB_PHYSPORTSTATE_CFG_TRAIN
  1351. };
  1352. u32 ipath_get_cr_errpkey(struct ipath_devdata *dd)
  1353. {
  1354. return ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
  1355. }
  1356. static int ipath_query_port(struct ib_device *ibdev,
  1357. u8 port, struct ib_port_attr *props)
  1358. {
  1359. struct ipath_ibdev *dev = to_idev(ibdev);
  1360. struct ipath_devdata *dd = dev->dd;
  1361. enum ib_mtu mtu;
  1362. u16 lid = dd->ipath_lid;
  1363. u64 ibcstat;
  1364. memset(props, 0, sizeof(*props));
  1365. props->lid = lid ? lid : __constant_be16_to_cpu(IB_LID_PERMISSIVE);
  1366. props->lmc = dd->ipath_lmc;
  1367. props->sm_lid = dev->sm_lid;
  1368. props->sm_sl = dev->sm_sl;
  1369. ibcstat = dd->ipath_lastibcstat;
  1370. /* map LinkState to IB portinfo values. */
  1371. props->state = ipath_ib_linkstate(dd, ibcstat) + 1;
  1372. /* See phys_state_show() */
  1373. props->phys_state = /* MEA: assumes shift == 0 */
  1374. ipath_cvt_physportstate[dd->ipath_lastibcstat &
  1375. dd->ibcs_lts_mask];
  1376. props->port_cap_flags = dev->port_cap_flags;
  1377. props->gid_tbl_len = 1;
  1378. props->max_msg_sz = 0x80000000;
  1379. props->pkey_tbl_len = ipath_get_npkeys(dd);
  1380. props->bad_pkey_cntr = ipath_get_cr_errpkey(dd) -
  1381. dev->z_pkey_violations;
  1382. props->qkey_viol_cntr = dev->qkey_violations;
  1383. props->active_width = dd->ipath_link_width_active;
  1384. /* See rate_show() */
  1385. props->active_speed = dd->ipath_link_speed_active;
  1386. props->max_vl_num = 1; /* VLCap = VL0 */
  1387. props->init_type_reply = 0;
  1388. props->max_mtu = ipath_mtu4096 ? IB_MTU_4096 : IB_MTU_2048;
  1389. switch (dd->ipath_ibmtu) {
  1390. case 4096:
  1391. mtu = IB_MTU_4096;
  1392. break;
  1393. case 2048:
  1394. mtu = IB_MTU_2048;
  1395. break;
  1396. case 1024:
  1397. mtu = IB_MTU_1024;
  1398. break;
  1399. case 512:
  1400. mtu = IB_MTU_512;
  1401. break;
  1402. case 256:
  1403. mtu = IB_MTU_256;
  1404. break;
  1405. default:
  1406. mtu = IB_MTU_2048;
  1407. }
  1408. props->active_mtu = mtu;
  1409. props->subnet_timeout = dev->subnet_timeout;
  1410. return 0;
  1411. }
  1412. static int ipath_modify_device(struct ib_device *device,
  1413. int device_modify_mask,
  1414. struct ib_device_modify *device_modify)
  1415. {
  1416. int ret;
  1417. if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
  1418. IB_DEVICE_MODIFY_NODE_DESC)) {
  1419. ret = -EOPNOTSUPP;
  1420. goto bail;
  1421. }
  1422. if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC)
  1423. memcpy(device->node_desc, device_modify->node_desc, 64);
  1424. if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID)
  1425. to_idev(device)->sys_image_guid =
  1426. cpu_to_be64(device_modify->sys_image_guid);
  1427. ret = 0;
  1428. bail:
  1429. return ret;
  1430. }
  1431. static int ipath_modify_port(struct ib_device *ibdev,
  1432. u8 port, int port_modify_mask,
  1433. struct ib_port_modify *props)
  1434. {
  1435. struct ipath_ibdev *dev = to_idev(ibdev);
  1436. dev->port_cap_flags |= props->set_port_cap_mask;
  1437. dev->port_cap_flags &= ~props->clr_port_cap_mask;
  1438. if (port_modify_mask & IB_PORT_SHUTDOWN)
  1439. ipath_set_linkstate(dev->dd, IPATH_IB_LINKDOWN);
  1440. if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
  1441. dev->qkey_violations = 0;
  1442. return 0;
  1443. }
  1444. static int ipath_query_gid(struct ib_device *ibdev, u8 port,
  1445. int index, union ib_gid *gid)
  1446. {
  1447. struct ipath_ibdev *dev = to_idev(ibdev);
  1448. int ret;
  1449. if (index >= 1) {
  1450. ret = -EINVAL;
  1451. goto bail;
  1452. }
  1453. gid->global.subnet_prefix = dev->gid_prefix;
  1454. gid->global.interface_id = dev->dd->ipath_guid;
  1455. ret = 0;
  1456. bail:
  1457. return ret;
  1458. }
  1459. static struct ib_pd *ipath_alloc_pd(struct ib_device *ibdev,
  1460. struct ib_ucontext *context,
  1461. struct ib_udata *udata)
  1462. {
  1463. struct ipath_ibdev *dev = to_idev(ibdev);
  1464. struct ipath_pd *pd;
  1465. struct ib_pd *ret;
  1466. /*
  1467. * This is actually totally arbitrary. Some correctness tests
  1468. * assume there's a maximum number of PDs that can be allocated.
  1469. * We don't actually have this limit, but we fail the test if
  1470. * we allow allocations of more than we report for this value.
  1471. */
  1472. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  1473. if (!pd) {
  1474. ret = ERR_PTR(-ENOMEM);
  1475. goto bail;
  1476. }
  1477. spin_lock(&dev->n_pds_lock);
  1478. if (dev->n_pds_allocated == ib_ipath_max_pds) {
  1479. spin_unlock(&dev->n_pds_lock);
  1480. kfree(pd);
  1481. ret = ERR_PTR(-ENOMEM);
  1482. goto bail;
  1483. }
  1484. dev->n_pds_allocated++;
  1485. spin_unlock(&dev->n_pds_lock);
  1486. /* ib_alloc_pd() will initialize pd->ibpd. */
  1487. pd->user = udata != NULL;
  1488. ret = &pd->ibpd;
  1489. bail:
  1490. return ret;
  1491. }
  1492. static int ipath_dealloc_pd(struct ib_pd *ibpd)
  1493. {
  1494. struct ipath_pd *pd = to_ipd(ibpd);
  1495. struct ipath_ibdev *dev = to_idev(ibpd->device);
  1496. spin_lock(&dev->n_pds_lock);
  1497. dev->n_pds_allocated--;
  1498. spin_unlock(&dev->n_pds_lock);
  1499. kfree(pd);
  1500. return 0;
  1501. }
  1502. /**
  1503. * ipath_create_ah - create an address handle
  1504. * @pd: the protection domain
  1505. * @ah_attr: the attributes of the AH
  1506. *
  1507. * This may be called from interrupt context.
  1508. */
  1509. static struct ib_ah *ipath_create_ah(struct ib_pd *pd,
  1510. struct ib_ah_attr *ah_attr)
  1511. {
  1512. struct ipath_ah *ah;
  1513. struct ib_ah *ret;
  1514. struct ipath_ibdev *dev = to_idev(pd->device);
  1515. unsigned long flags;
  1516. /* A multicast address requires a GRH (see ch. 8.4.1). */
  1517. if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&
  1518. ah_attr->dlid != IPATH_PERMISSIVE_LID &&
  1519. !(ah_attr->ah_flags & IB_AH_GRH)) {
  1520. ret = ERR_PTR(-EINVAL);
  1521. goto bail;
  1522. }
  1523. if (ah_attr->dlid == 0) {
  1524. ret = ERR_PTR(-EINVAL);
  1525. goto bail;
  1526. }
  1527. if (ah_attr->port_num < 1 ||
  1528. ah_attr->port_num > pd->device->phys_port_cnt) {
  1529. ret = ERR_PTR(-EINVAL);
  1530. goto bail;
  1531. }
  1532. ah = kmalloc(sizeof *ah, GFP_ATOMIC);
  1533. if (!ah) {
  1534. ret = ERR_PTR(-ENOMEM);
  1535. goto bail;
  1536. }
  1537. spin_lock_irqsave(&dev->n_ahs_lock, flags);
  1538. if (dev->n_ahs_allocated == ib_ipath_max_ahs) {
  1539. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  1540. kfree(ah);
  1541. ret = ERR_PTR(-ENOMEM);
  1542. goto bail;
  1543. }
  1544. dev->n_ahs_allocated++;
  1545. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  1546. /* ib_create_ah() will initialize ah->ibah. */
  1547. ah->attr = *ah_attr;
  1548. ah->attr.static_rate = ipath_ib_rate_to_mult(ah_attr->static_rate);
  1549. ret = &ah->ibah;
  1550. bail:
  1551. return ret;
  1552. }
  1553. /**
  1554. * ipath_destroy_ah - destroy an address handle
  1555. * @ibah: the AH to destroy
  1556. *
  1557. * This may be called from interrupt context.
  1558. */
  1559. static int ipath_destroy_ah(struct ib_ah *ibah)
  1560. {
  1561. struct ipath_ibdev *dev = to_idev(ibah->device);
  1562. struct ipath_ah *ah = to_iah(ibah);
  1563. unsigned long flags;
  1564. spin_lock_irqsave(&dev->n_ahs_lock, flags);
  1565. dev->n_ahs_allocated--;
  1566. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  1567. kfree(ah);
  1568. return 0;
  1569. }
  1570. static int ipath_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
  1571. {
  1572. struct ipath_ah *ah = to_iah(ibah);
  1573. *ah_attr = ah->attr;
  1574. ah_attr->static_rate = ipath_mult_to_ib_rate(ah->attr.static_rate);
  1575. return 0;
  1576. }
  1577. /**
  1578. * ipath_get_npkeys - return the size of the PKEY table for port 0
  1579. * @dd: the infinipath device
  1580. */
  1581. unsigned ipath_get_npkeys(struct ipath_devdata *dd)
  1582. {
  1583. return ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys);
  1584. }
  1585. /**
  1586. * ipath_get_pkey - return the indexed PKEY from the port 0 PKEY table
  1587. * @dd: the infinipath device
  1588. * @index: the PKEY index
  1589. */
  1590. unsigned ipath_get_pkey(struct ipath_devdata *dd, unsigned index)
  1591. {
  1592. unsigned ret;
  1593. if (index >= ARRAY_SIZE(dd->ipath_pd[0]->port_pkeys))
  1594. ret = 0;
  1595. else
  1596. ret = dd->ipath_pd[0]->port_pkeys[index];
  1597. return ret;
  1598. }
  1599. static int ipath_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  1600. u16 *pkey)
  1601. {
  1602. struct ipath_ibdev *dev = to_idev(ibdev);
  1603. int ret;
  1604. if (index >= ipath_get_npkeys(dev->dd)) {
  1605. ret = -EINVAL;
  1606. goto bail;
  1607. }
  1608. *pkey = ipath_get_pkey(dev->dd, index);
  1609. ret = 0;
  1610. bail:
  1611. return ret;
  1612. }
  1613. /**
  1614. * ipath_alloc_ucontext - allocate a ucontest
  1615. * @ibdev: the infiniband device
  1616. * @udata: not used by the InfiniPath driver
  1617. */
  1618. static struct ib_ucontext *ipath_alloc_ucontext(struct ib_device *ibdev,
  1619. struct ib_udata *udata)
  1620. {
  1621. struct ipath_ucontext *context;
  1622. struct ib_ucontext *ret;
  1623. context = kmalloc(sizeof *context, GFP_KERNEL);
  1624. if (!context) {
  1625. ret = ERR_PTR(-ENOMEM);
  1626. goto bail;
  1627. }
  1628. ret = &context->ibucontext;
  1629. bail:
  1630. return ret;
  1631. }
  1632. static int ipath_dealloc_ucontext(struct ib_ucontext *context)
  1633. {
  1634. kfree(to_iucontext(context));
  1635. return 0;
  1636. }
  1637. static int ipath_verbs_register_sysfs(struct ib_device *dev);
  1638. static void __verbs_timer(unsigned long arg)
  1639. {
  1640. struct ipath_devdata *dd = (struct ipath_devdata *) arg;
  1641. /* Handle verbs layer timeouts. */
  1642. ipath_ib_timer(dd->verbs_dev);
  1643. mod_timer(&dd->verbs_timer, jiffies + 1);
  1644. }
  1645. static int enable_timer(struct ipath_devdata *dd)
  1646. {
  1647. /*
  1648. * Early chips had a design flaw where the chip and kernel idea
  1649. * of the tail register don't always agree, and therefore we won't
  1650. * get an interrupt on the next packet received.
  1651. * If the board supports per packet receive interrupts, use it.
  1652. * Otherwise, the timer function periodically checks for packets
  1653. * to cover this case.
  1654. * Either way, the timer is needed for verbs layer related
  1655. * processing.
  1656. */
  1657. if (dd->ipath_flags & IPATH_GPIO_INTR) {
  1658. ipath_write_kreg(dd, dd->ipath_kregs->kr_debugportselect,
  1659. 0x2074076542310ULL);
  1660. /* Enable GPIO bit 2 interrupt */
  1661. dd->ipath_gpio_mask |= (u64) (1 << IPATH_GPIO_PORT0_BIT);
  1662. ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
  1663. dd->ipath_gpio_mask);
  1664. }
  1665. init_timer(&dd->verbs_timer);
  1666. dd->verbs_timer.function = __verbs_timer;
  1667. dd->verbs_timer.data = (unsigned long)dd;
  1668. dd->verbs_timer.expires = jiffies + 1;
  1669. add_timer(&dd->verbs_timer);
  1670. return 0;
  1671. }
  1672. static int disable_timer(struct ipath_devdata *dd)
  1673. {
  1674. /* Disable GPIO bit 2 interrupt */
  1675. if (dd->ipath_flags & IPATH_GPIO_INTR) {
  1676. /* Disable GPIO bit 2 interrupt */
  1677. dd->ipath_gpio_mask &= ~((u64) (1 << IPATH_GPIO_PORT0_BIT));
  1678. ipath_write_kreg(dd, dd->ipath_kregs->kr_gpio_mask,
  1679. dd->ipath_gpio_mask);
  1680. /*
  1681. * We might want to undo changes to debugportselect,
  1682. * but how?
  1683. */
  1684. }
  1685. del_timer_sync(&dd->verbs_timer);
  1686. return 0;
  1687. }
  1688. /**
  1689. * ipath_register_ib_device - register our device with the infiniband core
  1690. * @dd: the device data structure
  1691. * Return the allocated ipath_ibdev pointer or NULL on error.
  1692. */
  1693. int ipath_register_ib_device(struct ipath_devdata *dd)
  1694. {
  1695. struct ipath_verbs_counters cntrs;
  1696. struct ipath_ibdev *idev;
  1697. struct ib_device *dev;
  1698. struct ipath_verbs_txreq *tx;
  1699. unsigned i;
  1700. int ret;
  1701. idev = (struct ipath_ibdev *)ib_alloc_device(sizeof *idev);
  1702. if (idev == NULL) {
  1703. ret = -ENOMEM;
  1704. goto bail;
  1705. }
  1706. dev = &idev->ibdev;
  1707. if (dd->ipath_sdma_descq_cnt) {
  1708. tx = kmalloc(dd->ipath_sdma_descq_cnt * sizeof *tx,
  1709. GFP_KERNEL);
  1710. if (tx == NULL) {
  1711. ret = -ENOMEM;
  1712. goto err_tx;
  1713. }
  1714. } else
  1715. tx = NULL;
  1716. idev->txreq_bufs = tx;
  1717. /* Only need to initialize non-zero fields. */
  1718. spin_lock_init(&idev->n_pds_lock);
  1719. spin_lock_init(&idev->n_ahs_lock);
  1720. spin_lock_init(&idev->n_cqs_lock);
  1721. spin_lock_init(&idev->n_qps_lock);
  1722. spin_lock_init(&idev->n_srqs_lock);
  1723. spin_lock_init(&idev->n_mcast_grps_lock);
  1724. spin_lock_init(&idev->qp_table.lock);
  1725. spin_lock_init(&idev->lk_table.lock);
  1726. idev->sm_lid = __constant_be16_to_cpu(IB_LID_PERMISSIVE);
  1727. /* Set the prefix to the default value (see ch. 4.1.1) */
  1728. idev->gid_prefix = __constant_cpu_to_be64(0xfe80000000000000ULL);
  1729. ret = ipath_init_qp_table(idev, ib_ipath_qp_table_size);
  1730. if (ret)
  1731. goto err_qp;
  1732. /*
  1733. * The top ib_ipath_lkey_table_size bits are used to index the
  1734. * table. The lower 8 bits can be owned by the user (copied from
  1735. * the LKEY). The remaining bits act as a generation number or tag.
  1736. */
  1737. idev->lk_table.max = 1 << ib_ipath_lkey_table_size;
  1738. idev->lk_table.table = kzalloc(idev->lk_table.max *
  1739. sizeof(*idev->lk_table.table),
  1740. GFP_KERNEL);
  1741. if (idev->lk_table.table == NULL) {
  1742. ret = -ENOMEM;
  1743. goto err_lk;
  1744. }
  1745. INIT_LIST_HEAD(&idev->pending_mmaps);
  1746. spin_lock_init(&idev->pending_lock);
  1747. idev->mmap_offset = PAGE_SIZE;
  1748. spin_lock_init(&idev->mmap_offset_lock);
  1749. INIT_LIST_HEAD(&idev->pending[0]);
  1750. INIT_LIST_HEAD(&idev->pending[1]);
  1751. INIT_LIST_HEAD(&idev->pending[2]);
  1752. INIT_LIST_HEAD(&idev->piowait);
  1753. INIT_LIST_HEAD(&idev->rnrwait);
  1754. INIT_LIST_HEAD(&idev->txreq_free);
  1755. idev->pending_index = 0;
  1756. idev->port_cap_flags =
  1757. IB_PORT_SYS_IMAGE_GUID_SUP | IB_PORT_CLIENT_REG_SUP;
  1758. if (dd->ipath_flags & IPATH_HAS_LINK_LATENCY)
  1759. idev->port_cap_flags |= IB_PORT_LINK_LATENCY_SUP;
  1760. idev->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
  1761. idev->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
  1762. idev->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
  1763. idev->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
  1764. idev->pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
  1765. /* Snapshot current HW counters to "clear" them. */
  1766. ipath_get_counters(dd, &cntrs);
  1767. idev->z_symbol_error_counter = cntrs.symbol_error_counter;
  1768. idev->z_link_error_recovery_counter =
  1769. cntrs.link_error_recovery_counter;
  1770. idev->z_link_downed_counter = cntrs.link_downed_counter;
  1771. idev->z_port_rcv_errors = cntrs.port_rcv_errors;
  1772. idev->z_port_rcv_remphys_errors =
  1773. cntrs.port_rcv_remphys_errors;
  1774. idev->z_port_xmit_discards = cntrs.port_xmit_discards;
  1775. idev->z_port_xmit_data = cntrs.port_xmit_data;
  1776. idev->z_port_rcv_data = cntrs.port_rcv_data;
  1777. idev->z_port_xmit_packets = cntrs.port_xmit_packets;
  1778. idev->z_port_rcv_packets = cntrs.port_rcv_packets;
  1779. idev->z_local_link_integrity_errors =
  1780. cntrs.local_link_integrity_errors;
  1781. idev->z_excessive_buffer_overrun_errors =
  1782. cntrs.excessive_buffer_overrun_errors;
  1783. idev->z_vl15_dropped = cntrs.vl15_dropped;
  1784. for (i = 0; i < dd->ipath_sdma_descq_cnt; i++, tx++)
  1785. list_add(&tx->txreq.list, &idev->txreq_free);
  1786. /*
  1787. * The system image GUID is supposed to be the same for all
  1788. * IB HCAs in a single system but since there can be other
  1789. * device types in the system, we can't be sure this is unique.
  1790. */
  1791. if (!sys_image_guid)
  1792. sys_image_guid = dd->ipath_guid;
  1793. idev->sys_image_guid = sys_image_guid;
  1794. idev->ib_unit = dd->ipath_unit;
  1795. idev->dd = dd;
  1796. strlcpy(dev->name, "ipath%d", IB_DEVICE_NAME_MAX);
  1797. dev->owner = THIS_MODULE;
  1798. dev->node_guid = dd->ipath_guid;
  1799. dev->uverbs_abi_ver = IPATH_UVERBS_ABI_VERSION;
  1800. dev->uverbs_cmd_mask =
  1801. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  1802. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  1803. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  1804. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  1805. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  1806. (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
  1807. (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
  1808. (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
  1809. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  1810. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  1811. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  1812. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  1813. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  1814. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  1815. (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
  1816. (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
  1817. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  1818. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  1819. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  1820. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  1821. (1ull << IB_USER_VERBS_CMD_POST_SEND) |
  1822. (1ull << IB_USER_VERBS_CMD_POST_RECV) |
  1823. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  1824. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  1825. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  1826. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  1827. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  1828. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
  1829. (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
  1830. dev->node_type = RDMA_NODE_IB_CA;
  1831. dev->phys_port_cnt = 1;
  1832. dev->num_comp_vectors = 1;
  1833. dev->dma_device = &dd->pcidev->dev;
  1834. dev->query_device = ipath_query_device;
  1835. dev->modify_device = ipath_modify_device;
  1836. dev->query_port = ipath_query_port;
  1837. dev->modify_port = ipath_modify_port;
  1838. dev->query_pkey = ipath_query_pkey;
  1839. dev->query_gid = ipath_query_gid;
  1840. dev->alloc_ucontext = ipath_alloc_ucontext;
  1841. dev->dealloc_ucontext = ipath_dealloc_ucontext;
  1842. dev->alloc_pd = ipath_alloc_pd;
  1843. dev->dealloc_pd = ipath_dealloc_pd;
  1844. dev->create_ah = ipath_create_ah;
  1845. dev->destroy_ah = ipath_destroy_ah;
  1846. dev->query_ah = ipath_query_ah;
  1847. dev->create_srq = ipath_create_srq;
  1848. dev->modify_srq = ipath_modify_srq;
  1849. dev->query_srq = ipath_query_srq;
  1850. dev->destroy_srq = ipath_destroy_srq;
  1851. dev->create_qp = ipath_create_qp;
  1852. dev->modify_qp = ipath_modify_qp;
  1853. dev->query_qp = ipath_query_qp;
  1854. dev->destroy_qp = ipath_destroy_qp;
  1855. dev->post_send = ipath_post_send;
  1856. dev->post_recv = ipath_post_receive;
  1857. dev->post_srq_recv = ipath_post_srq_receive;
  1858. dev->create_cq = ipath_create_cq;
  1859. dev->destroy_cq = ipath_destroy_cq;
  1860. dev->resize_cq = ipath_resize_cq;
  1861. dev->poll_cq = ipath_poll_cq;
  1862. dev->req_notify_cq = ipath_req_notify_cq;
  1863. dev->get_dma_mr = ipath_get_dma_mr;
  1864. dev->reg_phys_mr = ipath_reg_phys_mr;
  1865. dev->reg_user_mr = ipath_reg_user_mr;
  1866. dev->dereg_mr = ipath_dereg_mr;
  1867. dev->alloc_fmr = ipath_alloc_fmr;
  1868. dev->map_phys_fmr = ipath_map_phys_fmr;
  1869. dev->unmap_fmr = ipath_unmap_fmr;
  1870. dev->dealloc_fmr = ipath_dealloc_fmr;
  1871. dev->attach_mcast = ipath_multicast_attach;
  1872. dev->detach_mcast = ipath_multicast_detach;
  1873. dev->process_mad = ipath_process_mad;
  1874. dev->mmap = ipath_mmap;
  1875. dev->dma_ops = &ipath_dma_mapping_ops;
  1876. snprintf(dev->node_desc, sizeof(dev->node_desc),
  1877. IPATH_IDSTR " %s", init_utsname()->nodename);
  1878. ret = ib_register_device(dev);
  1879. if (ret)
  1880. goto err_reg;
  1881. if (ipath_verbs_register_sysfs(dev))
  1882. goto err_class;
  1883. enable_timer(dd);
  1884. goto bail;
  1885. err_class:
  1886. ib_unregister_device(dev);
  1887. err_reg:
  1888. kfree(idev->lk_table.table);
  1889. err_lk:
  1890. kfree(idev->qp_table.table);
  1891. err_qp:
  1892. kfree(idev->txreq_bufs);
  1893. err_tx:
  1894. ib_dealloc_device(dev);
  1895. ipath_dev_err(dd, "cannot register verbs: %d!\n", -ret);
  1896. idev = NULL;
  1897. bail:
  1898. dd->verbs_dev = idev;
  1899. return ret;
  1900. }
  1901. void ipath_unregister_ib_device(struct ipath_ibdev *dev)
  1902. {
  1903. struct ib_device *ibdev = &dev->ibdev;
  1904. disable_timer(dev->dd);
  1905. ib_unregister_device(ibdev);
  1906. if (!list_empty(&dev->pending[0]) ||
  1907. !list_empty(&dev->pending[1]) ||
  1908. !list_empty(&dev->pending[2]))
  1909. ipath_dev_err(dev->dd, "pending list not empty!\n");
  1910. if (!list_empty(&dev->piowait))
  1911. ipath_dev_err(dev->dd, "piowait list not empty!\n");
  1912. if (!list_empty(&dev->rnrwait))
  1913. ipath_dev_err(dev->dd, "rnrwait list not empty!\n");
  1914. if (!ipath_mcast_tree_empty())
  1915. ipath_dev_err(dev->dd, "multicast table memory leak!\n");
  1916. /*
  1917. * Note that ipath_unregister_ib_device() can be called before all
  1918. * the QPs are destroyed!
  1919. */
  1920. ipath_free_all_qps(&dev->qp_table);
  1921. kfree(dev->qp_table.table);
  1922. kfree(dev->lk_table.table);
  1923. kfree(dev->txreq_bufs);
  1924. ib_dealloc_device(ibdev);
  1925. }
  1926. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  1927. char *buf)
  1928. {
  1929. struct ipath_ibdev *dev =
  1930. container_of(device, struct ipath_ibdev, ibdev.dev);
  1931. return sprintf(buf, "%x\n", dev->dd->ipath_pcirev);
  1932. }
  1933. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  1934. char *buf)
  1935. {
  1936. struct ipath_ibdev *dev =
  1937. container_of(device, struct ipath_ibdev, ibdev.dev);
  1938. int ret;
  1939. ret = dev->dd->ipath_f_get_boardname(dev->dd, buf, 128);
  1940. if (ret < 0)
  1941. goto bail;
  1942. strcat(buf, "\n");
  1943. ret = strlen(buf);
  1944. bail:
  1945. return ret;
  1946. }
  1947. static ssize_t show_stats(struct device *device, struct device_attribute *attr,
  1948. char *buf)
  1949. {
  1950. struct ipath_ibdev *dev =
  1951. container_of(device, struct ipath_ibdev, ibdev.dev);
  1952. int i;
  1953. int len;
  1954. len = sprintf(buf,
  1955. "RC resends %d\n"
  1956. "RC no QACK %d\n"
  1957. "RC ACKs %d\n"
  1958. "RC SEQ NAKs %d\n"
  1959. "RC RDMA seq %d\n"
  1960. "RC RNR NAKs %d\n"
  1961. "RC OTH NAKs %d\n"
  1962. "RC timeouts %d\n"
  1963. "RC RDMA dup %d\n"
  1964. "RC stalls %d\n"
  1965. "piobuf wait %d\n"
  1966. "no piobuf %d\n"
  1967. "unaligned %d\n"
  1968. "PKT drops %d\n"
  1969. "WQE errs %d\n",
  1970. dev->n_rc_resends, dev->n_rc_qacks, dev->n_rc_acks,
  1971. dev->n_seq_naks, dev->n_rdma_seq, dev->n_rnr_naks,
  1972. dev->n_other_naks, dev->n_timeouts,
  1973. dev->n_rdma_dup_busy, dev->n_rc_stalls, dev->n_piowait,
  1974. dev->n_no_piobuf, dev->n_unaligned,
  1975. dev->n_pkt_drops, dev->n_wqe_errs);
  1976. for (i = 0; i < ARRAY_SIZE(dev->opstats); i++) {
  1977. const struct ipath_opcode_stats *si = &dev->opstats[i];
  1978. if (!si->n_packets && !si->n_bytes)
  1979. continue;
  1980. len += sprintf(buf + len, "%02x %llu/%llu\n", i,
  1981. (unsigned long long) si->n_packets,
  1982. (unsigned long long) si->n_bytes);
  1983. }
  1984. return len;
  1985. }
  1986. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  1987. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  1988. static DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL);
  1989. static DEVICE_ATTR(stats, S_IRUGO, show_stats, NULL);
  1990. static struct device_attribute *ipath_class_attributes[] = {
  1991. &dev_attr_hw_rev,
  1992. &dev_attr_hca_type,
  1993. &dev_attr_board_id,
  1994. &dev_attr_stats
  1995. };
  1996. static int ipath_verbs_register_sysfs(struct ib_device *dev)
  1997. {
  1998. int i;
  1999. int ret;
  2000. for (i = 0; i < ARRAY_SIZE(ipath_class_attributes); ++i)
  2001. if (device_create_file(&dev->dev,
  2002. ipath_class_attributes[i])) {
  2003. ret = 1;
  2004. goto bail;
  2005. }
  2006. ret = 0;
  2007. bail:
  2008. return ret;
  2009. }