ipath_verbs.c 62 KB

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