ipath_verbs.c 62 KB

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