ipath_verbs.c 62 KB

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