ipath_verbs.c 62 KB

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