qib_verbs.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
  3. * All rights reserved.
  4. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <rdma/ib_mad.h>
  35. #include <rdma/ib_user_verbs.h>
  36. #include <linux/io.h>
  37. #include <linux/module.h>
  38. #include <linux/utsname.h>
  39. #include <linux/rculist.h>
  40. #include <linux/mm.h>
  41. #include <linux/random.h>
  42. #include <linux/vmalloc.h>
  43. #include "qib.h"
  44. #include "qib_common.h"
  45. static unsigned int ib_qib_qp_table_size = 256;
  46. module_param_named(qp_table_size, ib_qib_qp_table_size, uint, S_IRUGO);
  47. MODULE_PARM_DESC(qp_table_size, "QP table size");
  48. unsigned int ib_qib_lkey_table_size = 16;
  49. module_param_named(lkey_table_size, ib_qib_lkey_table_size, uint,
  50. S_IRUGO);
  51. MODULE_PARM_DESC(lkey_table_size,
  52. "LKEY table size in bits (2^n, 1 <= n <= 23)");
  53. static unsigned int ib_qib_max_pds = 0xFFFF;
  54. module_param_named(max_pds, ib_qib_max_pds, uint, S_IRUGO);
  55. MODULE_PARM_DESC(max_pds,
  56. "Maximum number of protection domains to support");
  57. static unsigned int ib_qib_max_ahs = 0xFFFF;
  58. module_param_named(max_ahs, ib_qib_max_ahs, uint, S_IRUGO);
  59. MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
  60. unsigned int ib_qib_max_cqes = 0x2FFFF;
  61. module_param_named(max_cqes, ib_qib_max_cqes, uint, S_IRUGO);
  62. MODULE_PARM_DESC(max_cqes,
  63. "Maximum number of completion queue entries to support");
  64. unsigned int ib_qib_max_cqs = 0x1FFFF;
  65. module_param_named(max_cqs, ib_qib_max_cqs, uint, S_IRUGO);
  66. MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
  67. unsigned int ib_qib_max_qp_wrs = 0x3FFF;
  68. module_param_named(max_qp_wrs, ib_qib_max_qp_wrs, uint, S_IRUGO);
  69. MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
  70. unsigned int ib_qib_max_qps = 16384;
  71. module_param_named(max_qps, ib_qib_max_qps, uint, S_IRUGO);
  72. MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
  73. unsigned int ib_qib_max_sges = 0x60;
  74. module_param_named(max_sges, ib_qib_max_sges, uint, S_IRUGO);
  75. MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
  76. unsigned int ib_qib_max_mcast_grps = 16384;
  77. module_param_named(max_mcast_grps, ib_qib_max_mcast_grps, uint, S_IRUGO);
  78. MODULE_PARM_DESC(max_mcast_grps,
  79. "Maximum number of multicast groups to support");
  80. unsigned int ib_qib_max_mcast_qp_attached = 16;
  81. module_param_named(max_mcast_qp_attached, ib_qib_max_mcast_qp_attached,
  82. uint, S_IRUGO);
  83. MODULE_PARM_DESC(max_mcast_qp_attached,
  84. "Maximum number of attached QPs to support");
  85. unsigned int ib_qib_max_srqs = 1024;
  86. module_param_named(max_srqs, ib_qib_max_srqs, uint, S_IRUGO);
  87. MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
  88. unsigned int ib_qib_max_srq_sges = 128;
  89. module_param_named(max_srq_sges, ib_qib_max_srq_sges, uint, S_IRUGO);
  90. MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
  91. unsigned int ib_qib_max_srq_wrs = 0x1FFFF;
  92. module_param_named(max_srq_wrs, ib_qib_max_srq_wrs, uint, S_IRUGO);
  93. MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
  94. static unsigned int ib_qib_disable_sma;
  95. module_param_named(disable_sma, ib_qib_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; qib_do_send() will process them and generate error
  100. * completions as per IB 1.2 C10-96.
  101. */
  102. const int ib_qib_state_ops[IB_QPS_ERR + 1] = {
  103. [IB_QPS_RESET] = 0,
  104. [IB_QPS_INIT] = QIB_POST_RECV_OK,
  105. [IB_QPS_RTR] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK,
  106. [IB_QPS_RTS] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
  107. QIB_POST_SEND_OK | QIB_PROCESS_SEND_OK |
  108. QIB_PROCESS_NEXT_SEND_OK,
  109. [IB_QPS_SQD] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
  110. QIB_POST_SEND_OK | QIB_PROCESS_SEND_OK,
  111. [IB_QPS_SQE] = QIB_POST_RECV_OK | QIB_PROCESS_RECV_OK |
  112. QIB_POST_SEND_OK | QIB_FLUSH_SEND,
  113. [IB_QPS_ERR] = QIB_POST_RECV_OK | QIB_FLUSH_RECV |
  114. QIB_POST_SEND_OK | QIB_FLUSH_SEND,
  115. };
  116. struct qib_ucontext {
  117. struct ib_ucontext ibucontext;
  118. };
  119. static inline struct qib_ucontext *to_iucontext(struct ib_ucontext
  120. *ibucontext)
  121. {
  122. return container_of(ibucontext, struct qib_ucontext, ibucontext);
  123. }
  124. /*
  125. * Translate ib_wr_opcode into ib_wc_opcode.
  126. */
  127. const enum ib_wc_opcode ib_qib_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. __be64 ib_qib_sys_image_guid;
  140. /**
  141. * qib_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 qib_copy_sge(struct qib_sge_state *ss, void *data, u32 length, int release)
  147. {
  148. struct qib_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 (release)
  162. atomic_dec(&sge->mr->refcount);
  163. if (--ss->num_sge)
  164. *sge = *ss->sg_list++;
  165. } else if (sge->length == 0 && sge->mr->lkey) {
  166. if (++sge->n >= QIB_SEGSZ) {
  167. if (++sge->m >= sge->mr->mapsz)
  168. break;
  169. sge->n = 0;
  170. }
  171. sge->vaddr =
  172. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  173. sge->length =
  174. sge->mr->map[sge->m]->segs[sge->n].length;
  175. }
  176. data += len;
  177. length -= len;
  178. }
  179. }
  180. /**
  181. * qib_skip_sge - skip over SGE memory - XXX almost dup of prev func
  182. * @ss: the SGE state
  183. * @length: the number of bytes to skip
  184. */
  185. void qib_skip_sge(struct qib_sge_state *ss, u32 length, int release)
  186. {
  187. struct qib_sge *sge = &ss->sge;
  188. while (length) {
  189. u32 len = sge->length;
  190. if (len > length)
  191. len = length;
  192. if (len > sge->sge_length)
  193. len = sge->sge_length;
  194. BUG_ON(len == 0);
  195. sge->vaddr += len;
  196. sge->length -= len;
  197. sge->sge_length -= len;
  198. if (sge->sge_length == 0) {
  199. if (release)
  200. atomic_dec(&sge->mr->refcount);
  201. if (--ss->num_sge)
  202. *sge = *ss->sg_list++;
  203. } else if (sge->length == 0 && sge->mr->lkey) {
  204. if (++sge->n >= QIB_SEGSZ) {
  205. if (++sge->m >= sge->mr->mapsz)
  206. break;
  207. sge->n = 0;
  208. }
  209. sge->vaddr =
  210. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  211. sge->length =
  212. sge->mr->map[sge->m]->segs[sge->n].length;
  213. }
  214. length -= len;
  215. }
  216. }
  217. /*
  218. * Count the number of DMA descriptors needed to send length bytes of data.
  219. * Don't modify the qib_sge_state to get the count.
  220. * Return zero if any of the segments is not aligned.
  221. */
  222. static u32 qib_count_sge(struct qib_sge_state *ss, u32 length)
  223. {
  224. struct qib_sge *sg_list = ss->sg_list;
  225. struct qib_sge sge = ss->sge;
  226. u8 num_sge = ss->num_sge;
  227. u32 ndesc = 1; /* count the header */
  228. while (length) {
  229. u32 len = sge.length;
  230. if (len > length)
  231. len = length;
  232. if (len > sge.sge_length)
  233. len = sge.sge_length;
  234. BUG_ON(len == 0);
  235. if (((long) sge.vaddr & (sizeof(u32) - 1)) ||
  236. (len != length && (len & (sizeof(u32) - 1)))) {
  237. ndesc = 0;
  238. break;
  239. }
  240. ndesc++;
  241. sge.vaddr += len;
  242. sge.length -= len;
  243. sge.sge_length -= len;
  244. if (sge.sge_length == 0) {
  245. if (--num_sge)
  246. sge = *sg_list++;
  247. } else if (sge.length == 0 && sge.mr->lkey) {
  248. if (++sge.n >= QIB_SEGSZ) {
  249. if (++sge.m >= sge.mr->mapsz)
  250. break;
  251. sge.n = 0;
  252. }
  253. sge.vaddr =
  254. sge.mr->map[sge.m]->segs[sge.n].vaddr;
  255. sge.length =
  256. sge.mr->map[sge.m]->segs[sge.n].length;
  257. }
  258. length -= len;
  259. }
  260. return ndesc;
  261. }
  262. /*
  263. * Copy from the SGEs to the data buffer.
  264. */
  265. static void qib_copy_from_sge(void *data, struct qib_sge_state *ss, u32 length)
  266. {
  267. struct qib_sge *sge = &ss->sge;
  268. while (length) {
  269. u32 len = sge->length;
  270. if (len > length)
  271. len = length;
  272. if (len > sge->sge_length)
  273. len = sge->sge_length;
  274. BUG_ON(len == 0);
  275. memcpy(data, sge->vaddr, len);
  276. sge->vaddr += len;
  277. sge->length -= len;
  278. sge->sge_length -= len;
  279. if (sge->sge_length == 0) {
  280. if (--ss->num_sge)
  281. *sge = *ss->sg_list++;
  282. } else if (sge->length == 0 && sge->mr->lkey) {
  283. if (++sge->n >= QIB_SEGSZ) {
  284. if (++sge->m >= sge->mr->mapsz)
  285. break;
  286. sge->n = 0;
  287. }
  288. sge->vaddr =
  289. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  290. sge->length =
  291. sge->mr->map[sge->m]->segs[sge->n].length;
  292. }
  293. data += len;
  294. length -= len;
  295. }
  296. }
  297. /**
  298. * qib_post_one_send - post one RC, UC, or UD send work request
  299. * @qp: the QP to post on
  300. * @wr: the work request to send
  301. */
  302. static int qib_post_one_send(struct qib_qp *qp, struct ib_send_wr *wr)
  303. {
  304. struct qib_swqe *wqe;
  305. u32 next;
  306. int i;
  307. int j;
  308. int acc;
  309. int ret;
  310. unsigned long flags;
  311. struct qib_lkey_table *rkt;
  312. struct qib_pd *pd;
  313. spin_lock_irqsave(&qp->s_lock, flags);
  314. /* Check that state is OK to post send. */
  315. if (unlikely(!(ib_qib_state_ops[qp->state] & QIB_POST_SEND_OK)))
  316. goto bail_inval;
  317. /* IB spec says that num_sge == 0 is OK. */
  318. if (wr->num_sge > qp->s_max_sge)
  319. goto bail_inval;
  320. /*
  321. * Don't allow RDMA reads or atomic operations on UC or
  322. * undefined operations.
  323. * Make sure buffer is large enough to hold the result for atomics.
  324. */
  325. if (wr->opcode == IB_WR_FAST_REG_MR) {
  326. if (qib_fast_reg_mr(qp, wr))
  327. goto bail_inval;
  328. } else if (qp->ibqp.qp_type == IB_QPT_UC) {
  329. if ((unsigned) wr->opcode >= IB_WR_RDMA_READ)
  330. goto bail_inval;
  331. } else if (qp->ibqp.qp_type != IB_QPT_RC) {
  332. /* Check IB_QPT_SMI, IB_QPT_GSI, IB_QPT_UD opcode */
  333. if (wr->opcode != IB_WR_SEND &&
  334. wr->opcode != IB_WR_SEND_WITH_IMM)
  335. goto bail_inval;
  336. /* Check UD destination address PD */
  337. if (qp->ibqp.pd != wr->wr.ud.ah->pd)
  338. goto bail_inval;
  339. } else if ((unsigned) wr->opcode > IB_WR_ATOMIC_FETCH_AND_ADD)
  340. goto bail_inval;
  341. else if (wr->opcode >= IB_WR_ATOMIC_CMP_AND_SWP &&
  342. (wr->num_sge == 0 ||
  343. wr->sg_list[0].length < sizeof(u64) ||
  344. wr->sg_list[0].addr & (sizeof(u64) - 1)))
  345. goto bail_inval;
  346. else if (wr->opcode >= IB_WR_RDMA_READ && !qp->s_max_rd_atomic)
  347. goto bail_inval;
  348. next = qp->s_head + 1;
  349. if (next >= qp->s_size)
  350. next = 0;
  351. if (next == qp->s_last) {
  352. ret = -ENOMEM;
  353. goto bail;
  354. }
  355. rkt = &to_idev(qp->ibqp.device)->lk_table;
  356. pd = to_ipd(qp->ibqp.pd);
  357. wqe = get_swqe_ptr(qp, qp->s_head);
  358. wqe->wr = *wr;
  359. wqe->length = 0;
  360. j = 0;
  361. if (wr->num_sge) {
  362. acc = wr->opcode >= IB_WR_RDMA_READ ?
  363. IB_ACCESS_LOCAL_WRITE : 0;
  364. for (i = 0; i < wr->num_sge; i++) {
  365. u32 length = wr->sg_list[i].length;
  366. int ok;
  367. if (length == 0)
  368. continue;
  369. ok = qib_lkey_ok(rkt, pd, &wqe->sg_list[j],
  370. &wr->sg_list[i], acc);
  371. if (!ok)
  372. goto bail_inval_free;
  373. wqe->length += length;
  374. j++;
  375. }
  376. wqe->wr.num_sge = j;
  377. }
  378. if (qp->ibqp.qp_type == IB_QPT_UC ||
  379. qp->ibqp.qp_type == IB_QPT_RC) {
  380. if (wqe->length > 0x80000000U)
  381. goto bail_inval_free;
  382. } else if (wqe->length > (dd_from_ibdev(qp->ibqp.device)->pport +
  383. qp->port_num - 1)->ibmtu)
  384. goto bail_inval_free;
  385. else
  386. atomic_inc(&to_iah(wr->wr.ud.ah)->refcount);
  387. wqe->ssn = qp->s_ssn++;
  388. qp->s_head = next;
  389. ret = 0;
  390. goto bail;
  391. bail_inval_free:
  392. while (j) {
  393. struct qib_sge *sge = &wqe->sg_list[--j];
  394. atomic_dec(&sge->mr->refcount);
  395. }
  396. bail_inval:
  397. ret = -EINVAL;
  398. bail:
  399. spin_unlock_irqrestore(&qp->s_lock, flags);
  400. return ret;
  401. }
  402. /**
  403. * qib_post_send - post a send on a QP
  404. * @ibqp: the QP to post the send on
  405. * @wr: the list of work requests to post
  406. * @bad_wr: the first bad WR is put here
  407. *
  408. * This may be called from interrupt context.
  409. */
  410. static int qib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  411. struct ib_send_wr **bad_wr)
  412. {
  413. struct qib_qp *qp = to_iqp(ibqp);
  414. int err = 0;
  415. for (; wr; wr = wr->next) {
  416. err = qib_post_one_send(qp, wr);
  417. if (err) {
  418. *bad_wr = wr;
  419. goto bail;
  420. }
  421. }
  422. /* Try to do the send work in the caller's context. */
  423. qib_do_send(&qp->s_work);
  424. bail:
  425. return err;
  426. }
  427. /**
  428. * qib_post_receive - post a receive on a QP
  429. * @ibqp: the QP to post the receive on
  430. * @wr: the WR to post
  431. * @bad_wr: the first bad WR is put here
  432. *
  433. * This may be called from interrupt context.
  434. */
  435. static int qib_post_receive(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  436. struct ib_recv_wr **bad_wr)
  437. {
  438. struct qib_qp *qp = to_iqp(ibqp);
  439. struct qib_rwq *wq = qp->r_rq.wq;
  440. unsigned long flags;
  441. int ret;
  442. /* Check that state is OK to post receive. */
  443. if (!(ib_qib_state_ops[qp->state] & QIB_POST_RECV_OK) || !wq) {
  444. *bad_wr = wr;
  445. ret = -EINVAL;
  446. goto bail;
  447. }
  448. for (; wr; wr = wr->next) {
  449. struct qib_rwqe *wqe;
  450. u32 next;
  451. int i;
  452. if ((unsigned) wr->num_sge > qp->r_rq.max_sge) {
  453. *bad_wr = wr;
  454. ret = -EINVAL;
  455. goto bail;
  456. }
  457. spin_lock_irqsave(&qp->r_rq.lock, flags);
  458. next = wq->head + 1;
  459. if (next >= qp->r_rq.size)
  460. next = 0;
  461. if (next == wq->tail) {
  462. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  463. *bad_wr = wr;
  464. ret = -ENOMEM;
  465. goto bail;
  466. }
  467. wqe = get_rwqe_ptr(&qp->r_rq, wq->head);
  468. wqe->wr_id = wr->wr_id;
  469. wqe->num_sge = wr->num_sge;
  470. for (i = 0; i < wr->num_sge; i++)
  471. wqe->sg_list[i] = wr->sg_list[i];
  472. /* Make sure queue entry is written before the head index. */
  473. smp_wmb();
  474. wq->head = next;
  475. spin_unlock_irqrestore(&qp->r_rq.lock, flags);
  476. }
  477. ret = 0;
  478. bail:
  479. return ret;
  480. }
  481. /**
  482. * qib_qp_rcv - processing an incoming packet on a QP
  483. * @rcd: the context pointer
  484. * @hdr: the packet header
  485. * @has_grh: true if the packet has a GRH
  486. * @data: the packet data
  487. * @tlen: the packet length
  488. * @qp: the QP the packet came on
  489. *
  490. * This is called from qib_ib_rcv() to process an incoming packet
  491. * for the given QP.
  492. * Called at interrupt level.
  493. */
  494. static void qib_qp_rcv(struct qib_ctxtdata *rcd, struct qib_ib_header *hdr,
  495. int has_grh, void *data, u32 tlen, struct qib_qp *qp)
  496. {
  497. struct qib_ibport *ibp = &rcd->ppd->ibport_data;
  498. spin_lock(&qp->r_lock);
  499. /* Check for valid receive state. */
  500. if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK)) {
  501. ibp->n_pkt_drops++;
  502. goto unlock;
  503. }
  504. switch (qp->ibqp.qp_type) {
  505. case IB_QPT_SMI:
  506. case IB_QPT_GSI:
  507. if (ib_qib_disable_sma)
  508. break;
  509. /* FALLTHROUGH */
  510. case IB_QPT_UD:
  511. qib_ud_rcv(ibp, hdr, has_grh, data, tlen, qp);
  512. break;
  513. case IB_QPT_RC:
  514. qib_rc_rcv(rcd, hdr, has_grh, data, tlen, qp);
  515. break;
  516. case IB_QPT_UC:
  517. qib_uc_rcv(ibp, hdr, has_grh, data, tlen, qp);
  518. break;
  519. default:
  520. break;
  521. }
  522. unlock:
  523. spin_unlock(&qp->r_lock);
  524. }
  525. /**
  526. * qib_ib_rcv - process an incoming packet
  527. * @rcd: the context pointer
  528. * @rhdr: the header of the packet
  529. * @data: the packet payload
  530. * @tlen: the packet length
  531. *
  532. * This is called from qib_kreceive() to process an incoming packet at
  533. * interrupt level. Tlen is the length of the header + data + CRC in bytes.
  534. */
  535. void qib_ib_rcv(struct qib_ctxtdata *rcd, void *rhdr, void *data, u32 tlen)
  536. {
  537. struct qib_pportdata *ppd = rcd->ppd;
  538. struct qib_ibport *ibp = &ppd->ibport_data;
  539. struct qib_ib_header *hdr = rhdr;
  540. struct qib_other_headers *ohdr;
  541. struct qib_qp *qp;
  542. u32 qp_num;
  543. int lnh;
  544. u8 opcode;
  545. u16 lid;
  546. /* 24 == LRH+BTH+CRC */
  547. if (unlikely(tlen < 24))
  548. goto drop;
  549. /* Check for a valid destination LID (see ch. 7.11.1). */
  550. lid = be16_to_cpu(hdr->lrh[1]);
  551. if (lid < QIB_MULTICAST_LID_BASE) {
  552. lid &= ~((1 << ppd->lmc) - 1);
  553. if (unlikely(lid != ppd->lid))
  554. goto drop;
  555. }
  556. /* Check for GRH */
  557. lnh = be16_to_cpu(hdr->lrh[0]) & 3;
  558. if (lnh == QIB_LRH_BTH)
  559. ohdr = &hdr->u.oth;
  560. else if (lnh == QIB_LRH_GRH) {
  561. u32 vtf;
  562. ohdr = &hdr->u.l.oth;
  563. if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
  564. goto drop;
  565. vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
  566. if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
  567. goto drop;
  568. } else
  569. goto drop;
  570. opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
  571. ibp->opstats[opcode & 0x7f].n_bytes += tlen;
  572. ibp->opstats[opcode & 0x7f].n_packets++;
  573. /* Get the destination QP number. */
  574. qp_num = be32_to_cpu(ohdr->bth[1]) & QIB_QPN_MASK;
  575. if (qp_num == QIB_MULTICAST_QPN) {
  576. struct qib_mcast *mcast;
  577. struct qib_mcast_qp *p;
  578. if (lnh != QIB_LRH_GRH)
  579. goto drop;
  580. mcast = qib_mcast_find(ibp, &hdr->u.l.grh.dgid);
  581. if (mcast == NULL)
  582. goto drop;
  583. ibp->n_multicast_rcv++;
  584. list_for_each_entry_rcu(p, &mcast->qp_list, list)
  585. qib_qp_rcv(rcd, hdr, 1, data, tlen, p->qp);
  586. /*
  587. * Notify qib_multicast_detach() if it is waiting for us
  588. * to finish.
  589. */
  590. if (atomic_dec_return(&mcast->refcount) <= 1)
  591. wake_up(&mcast->wait);
  592. } else {
  593. if (rcd->lookaside_qp) {
  594. if (rcd->lookaside_qpn != qp_num) {
  595. if (atomic_dec_and_test(
  596. &rcd->lookaside_qp->refcount))
  597. wake_up(
  598. &rcd->lookaside_qp->wait);
  599. rcd->lookaside_qp = NULL;
  600. }
  601. }
  602. if (!rcd->lookaside_qp) {
  603. qp = qib_lookup_qpn(ibp, qp_num);
  604. if (!qp)
  605. goto drop;
  606. rcd->lookaside_qp = qp;
  607. rcd->lookaside_qpn = qp_num;
  608. } else
  609. qp = rcd->lookaside_qp;
  610. ibp->n_unicast_rcv++;
  611. qib_qp_rcv(rcd, hdr, lnh == QIB_LRH_GRH, data, tlen, qp);
  612. }
  613. return;
  614. drop:
  615. ibp->n_pkt_drops++;
  616. }
  617. /*
  618. * This is called from a timer to check for QPs
  619. * which need kernel memory in order to send a packet.
  620. */
  621. static void mem_timer(unsigned long data)
  622. {
  623. struct qib_ibdev *dev = (struct qib_ibdev *) data;
  624. struct list_head *list = &dev->memwait;
  625. struct qib_qp *qp = NULL;
  626. unsigned long flags;
  627. spin_lock_irqsave(&dev->pending_lock, flags);
  628. if (!list_empty(list)) {
  629. qp = list_entry(list->next, struct qib_qp, iowait);
  630. list_del_init(&qp->iowait);
  631. atomic_inc(&qp->refcount);
  632. if (!list_empty(list))
  633. mod_timer(&dev->mem_timer, jiffies + 1);
  634. }
  635. spin_unlock_irqrestore(&dev->pending_lock, flags);
  636. if (qp) {
  637. spin_lock_irqsave(&qp->s_lock, flags);
  638. if (qp->s_flags & QIB_S_WAIT_KMEM) {
  639. qp->s_flags &= ~QIB_S_WAIT_KMEM;
  640. qib_schedule_send(qp);
  641. }
  642. spin_unlock_irqrestore(&qp->s_lock, flags);
  643. if (atomic_dec_and_test(&qp->refcount))
  644. wake_up(&qp->wait);
  645. }
  646. }
  647. static void update_sge(struct qib_sge_state *ss, u32 length)
  648. {
  649. struct qib_sge *sge = &ss->sge;
  650. sge->vaddr += length;
  651. sge->length -= length;
  652. sge->sge_length -= length;
  653. if (sge->sge_length == 0) {
  654. if (--ss->num_sge)
  655. *sge = *ss->sg_list++;
  656. } else if (sge->length == 0 && sge->mr->lkey) {
  657. if (++sge->n >= QIB_SEGSZ) {
  658. if (++sge->m >= sge->mr->mapsz)
  659. return;
  660. sge->n = 0;
  661. }
  662. sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
  663. sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
  664. }
  665. }
  666. #ifdef __LITTLE_ENDIAN
  667. static inline u32 get_upper_bits(u32 data, u32 shift)
  668. {
  669. return data >> shift;
  670. }
  671. static inline u32 set_upper_bits(u32 data, u32 shift)
  672. {
  673. return data << shift;
  674. }
  675. static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
  676. {
  677. data <<= ((sizeof(u32) - n) * BITS_PER_BYTE);
  678. data >>= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
  679. return data;
  680. }
  681. #else
  682. static inline u32 get_upper_bits(u32 data, u32 shift)
  683. {
  684. return data << shift;
  685. }
  686. static inline u32 set_upper_bits(u32 data, u32 shift)
  687. {
  688. return data >> shift;
  689. }
  690. static inline u32 clear_upper_bytes(u32 data, u32 n, u32 off)
  691. {
  692. data >>= ((sizeof(u32) - n) * BITS_PER_BYTE);
  693. data <<= ((sizeof(u32) - n - off) * BITS_PER_BYTE);
  694. return data;
  695. }
  696. #endif
  697. static void copy_io(u32 __iomem *piobuf, struct qib_sge_state *ss,
  698. u32 length, unsigned flush_wc)
  699. {
  700. u32 extra = 0;
  701. u32 data = 0;
  702. u32 last;
  703. while (1) {
  704. u32 len = ss->sge.length;
  705. u32 off;
  706. if (len > length)
  707. len = length;
  708. if (len > ss->sge.sge_length)
  709. len = ss->sge.sge_length;
  710. BUG_ON(len == 0);
  711. /* If the source address is not aligned, try to align it. */
  712. off = (unsigned long)ss->sge.vaddr & (sizeof(u32) - 1);
  713. if (off) {
  714. u32 *addr = (u32 *)((unsigned long)ss->sge.vaddr &
  715. ~(sizeof(u32) - 1));
  716. u32 v = get_upper_bits(*addr, off * BITS_PER_BYTE);
  717. u32 y;
  718. y = sizeof(u32) - off;
  719. if (len > y)
  720. len = y;
  721. if (len + extra >= sizeof(u32)) {
  722. data |= set_upper_bits(v, extra *
  723. BITS_PER_BYTE);
  724. len = sizeof(u32) - extra;
  725. if (len == length) {
  726. last = data;
  727. break;
  728. }
  729. __raw_writel(data, piobuf);
  730. piobuf++;
  731. extra = 0;
  732. data = 0;
  733. } else {
  734. /* Clear unused upper bytes */
  735. data |= clear_upper_bytes(v, len, extra);
  736. if (len == length) {
  737. last = data;
  738. break;
  739. }
  740. extra += len;
  741. }
  742. } else if (extra) {
  743. /* Source address is aligned. */
  744. u32 *addr = (u32 *) ss->sge.vaddr;
  745. int shift = extra * BITS_PER_BYTE;
  746. int ushift = 32 - shift;
  747. u32 l = len;
  748. while (l >= sizeof(u32)) {
  749. u32 v = *addr;
  750. data |= set_upper_bits(v, shift);
  751. __raw_writel(data, piobuf);
  752. data = get_upper_bits(v, ushift);
  753. piobuf++;
  754. addr++;
  755. l -= sizeof(u32);
  756. }
  757. /*
  758. * We still have 'extra' number of bytes leftover.
  759. */
  760. if (l) {
  761. u32 v = *addr;
  762. if (l + extra >= sizeof(u32)) {
  763. data |= set_upper_bits(v, shift);
  764. len -= l + extra - sizeof(u32);
  765. if (len == length) {
  766. last = data;
  767. break;
  768. }
  769. __raw_writel(data, piobuf);
  770. piobuf++;
  771. extra = 0;
  772. data = 0;
  773. } else {
  774. /* Clear unused upper bytes */
  775. data |= clear_upper_bytes(v, l, extra);
  776. if (len == length) {
  777. last = data;
  778. break;
  779. }
  780. extra += l;
  781. }
  782. } else if (len == length) {
  783. last = data;
  784. break;
  785. }
  786. } else if (len == length) {
  787. u32 w;
  788. /*
  789. * Need to round up for the last dword in the
  790. * packet.
  791. */
  792. w = (len + 3) >> 2;
  793. qib_pio_copy(piobuf, ss->sge.vaddr, w - 1);
  794. piobuf += w - 1;
  795. last = ((u32 *) ss->sge.vaddr)[w - 1];
  796. break;
  797. } else {
  798. u32 w = len >> 2;
  799. qib_pio_copy(piobuf, ss->sge.vaddr, w);
  800. piobuf += w;
  801. extra = len & (sizeof(u32) - 1);
  802. if (extra) {
  803. u32 v = ((u32 *) ss->sge.vaddr)[w];
  804. /* Clear unused upper bytes */
  805. data = clear_upper_bytes(v, extra, 0);
  806. }
  807. }
  808. update_sge(ss, len);
  809. length -= len;
  810. }
  811. /* Update address before sending packet. */
  812. update_sge(ss, length);
  813. if (flush_wc) {
  814. /* must flush early everything before trigger word */
  815. qib_flush_wc();
  816. __raw_writel(last, piobuf);
  817. /* be sure trigger word is written */
  818. qib_flush_wc();
  819. } else
  820. __raw_writel(last, piobuf);
  821. }
  822. static noinline struct qib_verbs_txreq *__get_txreq(struct qib_ibdev *dev,
  823. struct qib_qp *qp)
  824. {
  825. struct qib_verbs_txreq *tx;
  826. unsigned long flags;
  827. spin_lock_irqsave(&qp->s_lock, flags);
  828. spin_lock(&dev->pending_lock);
  829. if (!list_empty(&dev->txreq_free)) {
  830. struct list_head *l = dev->txreq_free.next;
  831. list_del(l);
  832. spin_unlock(&dev->pending_lock);
  833. spin_unlock_irqrestore(&qp->s_lock, flags);
  834. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  835. } else {
  836. if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK &&
  837. list_empty(&qp->iowait)) {
  838. dev->n_txwait++;
  839. qp->s_flags |= QIB_S_WAIT_TX;
  840. list_add_tail(&qp->iowait, &dev->txwait);
  841. }
  842. qp->s_flags &= ~QIB_S_BUSY;
  843. spin_unlock(&dev->pending_lock);
  844. spin_unlock_irqrestore(&qp->s_lock, flags);
  845. tx = ERR_PTR(-EBUSY);
  846. }
  847. return tx;
  848. }
  849. static inline struct qib_verbs_txreq *get_txreq(struct qib_ibdev *dev,
  850. struct qib_qp *qp)
  851. {
  852. struct qib_verbs_txreq *tx;
  853. unsigned long flags;
  854. spin_lock_irqsave(&dev->pending_lock, flags);
  855. /* assume the list non empty */
  856. if (likely(!list_empty(&dev->txreq_free))) {
  857. struct list_head *l = dev->txreq_free.next;
  858. list_del(l);
  859. spin_unlock_irqrestore(&dev->pending_lock, flags);
  860. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  861. } else {
  862. /* call slow path to get the extra lock */
  863. spin_unlock_irqrestore(&dev->pending_lock, flags);
  864. tx = __get_txreq(dev, qp);
  865. }
  866. return tx;
  867. }
  868. void qib_put_txreq(struct qib_verbs_txreq *tx)
  869. {
  870. struct qib_ibdev *dev;
  871. struct qib_qp *qp;
  872. unsigned long flags;
  873. qp = tx->qp;
  874. dev = to_idev(qp->ibqp.device);
  875. if (atomic_dec_and_test(&qp->refcount))
  876. wake_up(&qp->wait);
  877. if (tx->mr) {
  878. atomic_dec(&tx->mr->refcount);
  879. tx->mr = NULL;
  880. }
  881. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF) {
  882. tx->txreq.flags &= ~QIB_SDMA_TXREQ_F_FREEBUF;
  883. dma_unmap_single(&dd_from_dev(dev)->pcidev->dev,
  884. tx->txreq.addr, tx->hdr_dwords << 2,
  885. DMA_TO_DEVICE);
  886. kfree(tx->align_buf);
  887. }
  888. spin_lock_irqsave(&dev->pending_lock, flags);
  889. /* Put struct back on free list */
  890. list_add(&tx->txreq.list, &dev->txreq_free);
  891. if (!list_empty(&dev->txwait)) {
  892. /* Wake up first QP wanting a free struct */
  893. qp = list_entry(dev->txwait.next, struct qib_qp, iowait);
  894. list_del_init(&qp->iowait);
  895. atomic_inc(&qp->refcount);
  896. spin_unlock_irqrestore(&dev->pending_lock, flags);
  897. spin_lock_irqsave(&qp->s_lock, flags);
  898. if (qp->s_flags & QIB_S_WAIT_TX) {
  899. qp->s_flags &= ~QIB_S_WAIT_TX;
  900. qib_schedule_send(qp);
  901. }
  902. spin_unlock_irqrestore(&qp->s_lock, flags);
  903. if (atomic_dec_and_test(&qp->refcount))
  904. wake_up(&qp->wait);
  905. } else
  906. spin_unlock_irqrestore(&dev->pending_lock, flags);
  907. }
  908. /*
  909. * This is called when there are send DMA descriptors that might be
  910. * available.
  911. *
  912. * This is called with ppd->sdma_lock held.
  913. */
  914. void qib_verbs_sdma_desc_avail(struct qib_pportdata *ppd, unsigned avail)
  915. {
  916. struct qib_qp *qp, *nqp;
  917. struct qib_qp *qps[20];
  918. struct qib_ibdev *dev;
  919. unsigned i, n;
  920. n = 0;
  921. dev = &ppd->dd->verbs_dev;
  922. spin_lock(&dev->pending_lock);
  923. /* Search wait list for first QP wanting DMA descriptors. */
  924. list_for_each_entry_safe(qp, nqp, &dev->dmawait, iowait) {
  925. if (qp->port_num != ppd->port)
  926. continue;
  927. if (n == ARRAY_SIZE(qps))
  928. break;
  929. if (qp->s_tx->txreq.sg_count > avail)
  930. break;
  931. avail -= qp->s_tx->txreq.sg_count;
  932. list_del_init(&qp->iowait);
  933. atomic_inc(&qp->refcount);
  934. qps[n++] = qp;
  935. }
  936. spin_unlock(&dev->pending_lock);
  937. for (i = 0; i < n; i++) {
  938. qp = qps[i];
  939. spin_lock(&qp->s_lock);
  940. if (qp->s_flags & QIB_S_WAIT_DMA_DESC) {
  941. qp->s_flags &= ~QIB_S_WAIT_DMA_DESC;
  942. qib_schedule_send(qp);
  943. }
  944. spin_unlock(&qp->s_lock);
  945. if (atomic_dec_and_test(&qp->refcount))
  946. wake_up(&qp->wait);
  947. }
  948. }
  949. /*
  950. * This is called with ppd->sdma_lock held.
  951. */
  952. static void sdma_complete(struct qib_sdma_txreq *cookie, int status)
  953. {
  954. struct qib_verbs_txreq *tx =
  955. container_of(cookie, struct qib_verbs_txreq, txreq);
  956. struct qib_qp *qp = tx->qp;
  957. spin_lock(&qp->s_lock);
  958. if (tx->wqe)
  959. qib_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
  960. else if (qp->ibqp.qp_type == IB_QPT_RC) {
  961. struct qib_ib_header *hdr;
  962. if (tx->txreq.flags & QIB_SDMA_TXREQ_F_FREEBUF)
  963. hdr = &tx->align_buf->hdr;
  964. else {
  965. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  966. hdr = &dev->pio_hdrs[tx->hdr_inx].hdr;
  967. }
  968. qib_rc_send_complete(qp, hdr);
  969. }
  970. if (atomic_dec_and_test(&qp->s_dma_busy)) {
  971. if (qp->state == IB_QPS_RESET)
  972. wake_up(&qp->wait_dma);
  973. else if (qp->s_flags & QIB_S_WAIT_DMA) {
  974. qp->s_flags &= ~QIB_S_WAIT_DMA;
  975. qib_schedule_send(qp);
  976. }
  977. }
  978. spin_unlock(&qp->s_lock);
  979. qib_put_txreq(tx);
  980. }
  981. static int wait_kmem(struct qib_ibdev *dev, struct qib_qp *qp)
  982. {
  983. unsigned long flags;
  984. int ret = 0;
  985. spin_lock_irqsave(&qp->s_lock, flags);
  986. if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
  987. spin_lock(&dev->pending_lock);
  988. if (list_empty(&qp->iowait)) {
  989. if (list_empty(&dev->memwait))
  990. mod_timer(&dev->mem_timer, jiffies + 1);
  991. qp->s_flags |= QIB_S_WAIT_KMEM;
  992. list_add_tail(&qp->iowait, &dev->memwait);
  993. }
  994. spin_unlock(&dev->pending_lock);
  995. qp->s_flags &= ~QIB_S_BUSY;
  996. ret = -EBUSY;
  997. }
  998. spin_unlock_irqrestore(&qp->s_lock, flags);
  999. return ret;
  1000. }
  1001. static int qib_verbs_send_dma(struct qib_qp *qp, struct qib_ib_header *hdr,
  1002. u32 hdrwords, struct qib_sge_state *ss, u32 len,
  1003. u32 plen, u32 dwords)
  1004. {
  1005. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  1006. struct qib_devdata *dd = dd_from_dev(dev);
  1007. struct qib_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
  1008. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1009. struct qib_verbs_txreq *tx;
  1010. struct qib_pio_header *phdr;
  1011. u32 control;
  1012. u32 ndesc;
  1013. int ret;
  1014. tx = qp->s_tx;
  1015. if (tx) {
  1016. qp->s_tx = NULL;
  1017. /* resend previously constructed packet */
  1018. ret = qib_sdma_verbs_send(ppd, tx->ss, tx->dwords, tx);
  1019. goto bail;
  1020. }
  1021. tx = get_txreq(dev, qp);
  1022. if (IS_ERR(tx))
  1023. goto bail_tx;
  1024. control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
  1025. be16_to_cpu(hdr->lrh[0]) >> 12);
  1026. tx->qp = qp;
  1027. atomic_inc(&qp->refcount);
  1028. tx->wqe = qp->s_wqe;
  1029. tx->mr = qp->s_rdma_mr;
  1030. if (qp->s_rdma_mr)
  1031. qp->s_rdma_mr = NULL;
  1032. tx->txreq.callback = sdma_complete;
  1033. if (dd->flags & QIB_HAS_SDMA_TIMEOUT)
  1034. tx->txreq.flags = QIB_SDMA_TXREQ_F_HEADTOHOST;
  1035. else
  1036. tx->txreq.flags = QIB_SDMA_TXREQ_F_INTREQ;
  1037. if (plen + 1 > dd->piosize2kmax_dwords)
  1038. tx->txreq.flags |= QIB_SDMA_TXREQ_F_USELARGEBUF;
  1039. if (len) {
  1040. /*
  1041. * Don't try to DMA if it takes more descriptors than
  1042. * the queue holds.
  1043. */
  1044. ndesc = qib_count_sge(ss, len);
  1045. if (ndesc >= ppd->sdma_descq_cnt)
  1046. ndesc = 0;
  1047. } else
  1048. ndesc = 1;
  1049. if (ndesc) {
  1050. phdr = &dev->pio_hdrs[tx->hdr_inx];
  1051. phdr->pbc[0] = cpu_to_le32(plen);
  1052. phdr->pbc[1] = cpu_to_le32(control);
  1053. memcpy(&phdr->hdr, hdr, hdrwords << 2);
  1054. tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEDESC;
  1055. tx->txreq.sg_count = ndesc;
  1056. tx->txreq.addr = dev->pio_hdrs_phys +
  1057. tx->hdr_inx * sizeof(struct qib_pio_header);
  1058. tx->hdr_dwords = hdrwords + 2; /* add PBC length */
  1059. ret = qib_sdma_verbs_send(ppd, ss, dwords, tx);
  1060. goto bail;
  1061. }
  1062. /* Allocate a buffer and copy the header and payload to it. */
  1063. tx->hdr_dwords = plen + 1;
  1064. phdr = kmalloc(tx->hdr_dwords << 2, GFP_ATOMIC);
  1065. if (!phdr)
  1066. goto err_tx;
  1067. phdr->pbc[0] = cpu_to_le32(plen);
  1068. phdr->pbc[1] = cpu_to_le32(control);
  1069. memcpy(&phdr->hdr, hdr, hdrwords << 2);
  1070. qib_copy_from_sge((u32 *) &phdr->hdr + hdrwords, ss, len);
  1071. tx->txreq.addr = dma_map_single(&dd->pcidev->dev, phdr,
  1072. tx->hdr_dwords << 2, DMA_TO_DEVICE);
  1073. if (dma_mapping_error(&dd->pcidev->dev, tx->txreq.addr))
  1074. goto map_err;
  1075. tx->align_buf = phdr;
  1076. tx->txreq.flags |= QIB_SDMA_TXREQ_F_FREEBUF;
  1077. tx->txreq.sg_count = 1;
  1078. ret = qib_sdma_verbs_send(ppd, NULL, 0, tx);
  1079. goto unaligned;
  1080. map_err:
  1081. kfree(phdr);
  1082. err_tx:
  1083. qib_put_txreq(tx);
  1084. ret = wait_kmem(dev, qp);
  1085. unaligned:
  1086. ibp->n_unaligned++;
  1087. bail:
  1088. return ret;
  1089. bail_tx:
  1090. ret = PTR_ERR(tx);
  1091. goto bail;
  1092. }
  1093. /*
  1094. * If we are now in the error state, return zero to flush the
  1095. * send work request.
  1096. */
  1097. static int no_bufs_available(struct qib_qp *qp)
  1098. {
  1099. struct qib_ibdev *dev = to_idev(qp->ibqp.device);
  1100. struct qib_devdata *dd;
  1101. unsigned long flags;
  1102. int ret = 0;
  1103. /*
  1104. * Note that as soon as want_buffer() is called and
  1105. * possibly before it returns, qib_ib_piobufavail()
  1106. * could be called. Therefore, put QP on the I/O wait list before
  1107. * enabling the PIO avail interrupt.
  1108. */
  1109. spin_lock_irqsave(&qp->s_lock, flags);
  1110. if (ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK) {
  1111. spin_lock(&dev->pending_lock);
  1112. if (list_empty(&qp->iowait)) {
  1113. dev->n_piowait++;
  1114. qp->s_flags |= QIB_S_WAIT_PIO;
  1115. list_add_tail(&qp->iowait, &dev->piowait);
  1116. dd = dd_from_dev(dev);
  1117. dd->f_wantpiobuf_intr(dd, 1);
  1118. }
  1119. spin_unlock(&dev->pending_lock);
  1120. qp->s_flags &= ~QIB_S_BUSY;
  1121. ret = -EBUSY;
  1122. }
  1123. spin_unlock_irqrestore(&qp->s_lock, flags);
  1124. return ret;
  1125. }
  1126. static int qib_verbs_send_pio(struct qib_qp *qp, struct qib_ib_header *ibhdr,
  1127. u32 hdrwords, struct qib_sge_state *ss, u32 len,
  1128. u32 plen, u32 dwords)
  1129. {
  1130. struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  1131. struct qib_pportdata *ppd = dd->pport + qp->port_num - 1;
  1132. u32 *hdr = (u32 *) ibhdr;
  1133. u32 __iomem *piobuf_orig;
  1134. u32 __iomem *piobuf;
  1135. u64 pbc;
  1136. unsigned long flags;
  1137. unsigned flush_wc;
  1138. u32 control;
  1139. u32 pbufn;
  1140. control = dd->f_setpbc_control(ppd, plen, qp->s_srate,
  1141. be16_to_cpu(ibhdr->lrh[0]) >> 12);
  1142. pbc = ((u64) control << 32) | plen;
  1143. piobuf = dd->f_getsendbuf(ppd, pbc, &pbufn);
  1144. if (unlikely(piobuf == NULL))
  1145. return no_bufs_available(qp);
  1146. /*
  1147. * Write the pbc.
  1148. * We have to flush after the PBC for correctness on some cpus
  1149. * or WC buffer can be written out of order.
  1150. */
  1151. writeq(pbc, piobuf);
  1152. piobuf_orig = piobuf;
  1153. piobuf += 2;
  1154. flush_wc = dd->flags & QIB_PIO_FLUSH_WC;
  1155. if (len == 0) {
  1156. /*
  1157. * If there is just the header portion, must flush before
  1158. * writing last word of header for correctness, and after
  1159. * the last header word (trigger word).
  1160. */
  1161. if (flush_wc) {
  1162. qib_flush_wc();
  1163. qib_pio_copy(piobuf, hdr, hdrwords - 1);
  1164. qib_flush_wc();
  1165. __raw_writel(hdr[hdrwords - 1], piobuf + hdrwords - 1);
  1166. qib_flush_wc();
  1167. } else
  1168. qib_pio_copy(piobuf, hdr, hdrwords);
  1169. goto done;
  1170. }
  1171. if (flush_wc)
  1172. qib_flush_wc();
  1173. qib_pio_copy(piobuf, hdr, hdrwords);
  1174. piobuf += hdrwords;
  1175. /* The common case is aligned and contained in one segment. */
  1176. if (likely(ss->num_sge == 1 && len <= ss->sge.length &&
  1177. !((unsigned long)ss->sge.vaddr & (sizeof(u32) - 1)))) {
  1178. u32 *addr = (u32 *) ss->sge.vaddr;
  1179. /* Update address before sending packet. */
  1180. update_sge(ss, len);
  1181. if (flush_wc) {
  1182. qib_pio_copy(piobuf, addr, dwords - 1);
  1183. /* must flush early everything before trigger word */
  1184. qib_flush_wc();
  1185. __raw_writel(addr[dwords - 1], piobuf + dwords - 1);
  1186. /* be sure trigger word is written */
  1187. qib_flush_wc();
  1188. } else
  1189. qib_pio_copy(piobuf, addr, dwords);
  1190. goto done;
  1191. }
  1192. copy_io(piobuf, ss, len, flush_wc);
  1193. done:
  1194. if (dd->flags & QIB_USE_SPCL_TRIG) {
  1195. u32 spcl_off = (pbufn >= dd->piobcnt2k) ? 2047 : 1023;
  1196. qib_flush_wc();
  1197. __raw_writel(0xaebecede, piobuf_orig + spcl_off);
  1198. }
  1199. qib_sendbuf_done(dd, pbufn);
  1200. if (qp->s_rdma_mr) {
  1201. atomic_dec(&qp->s_rdma_mr->refcount);
  1202. qp->s_rdma_mr = NULL;
  1203. }
  1204. if (qp->s_wqe) {
  1205. spin_lock_irqsave(&qp->s_lock, flags);
  1206. qib_send_complete(qp, qp->s_wqe, IB_WC_SUCCESS);
  1207. spin_unlock_irqrestore(&qp->s_lock, flags);
  1208. } else if (qp->ibqp.qp_type == IB_QPT_RC) {
  1209. spin_lock_irqsave(&qp->s_lock, flags);
  1210. qib_rc_send_complete(qp, ibhdr);
  1211. spin_unlock_irqrestore(&qp->s_lock, flags);
  1212. }
  1213. return 0;
  1214. }
  1215. /**
  1216. * qib_verbs_send - send a packet
  1217. * @qp: the QP to send on
  1218. * @hdr: the packet header
  1219. * @hdrwords: the number of 32-bit words in the header
  1220. * @ss: the SGE to send
  1221. * @len: the length of the packet in bytes
  1222. *
  1223. * Return zero if packet is sent or queued OK.
  1224. * Return non-zero and clear qp->s_flags QIB_S_BUSY otherwise.
  1225. */
  1226. int qib_verbs_send(struct qib_qp *qp, struct qib_ib_header *hdr,
  1227. u32 hdrwords, struct qib_sge_state *ss, u32 len)
  1228. {
  1229. struct qib_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  1230. u32 plen;
  1231. int ret;
  1232. u32 dwords = (len + 3) >> 2;
  1233. /*
  1234. * Calculate the send buffer trigger address.
  1235. * The +1 counts for the pbc control dword following the pbc length.
  1236. */
  1237. plen = hdrwords + dwords + 1;
  1238. /*
  1239. * VL15 packets (IB_QPT_SMI) will always use PIO, so we
  1240. * can defer SDMA restart until link goes ACTIVE without
  1241. * worrying about just how we got there.
  1242. */
  1243. if (qp->ibqp.qp_type == IB_QPT_SMI ||
  1244. !(dd->flags & QIB_HAS_SEND_DMA))
  1245. ret = qib_verbs_send_pio(qp, hdr, hdrwords, ss, len,
  1246. plen, dwords);
  1247. else
  1248. ret = qib_verbs_send_dma(qp, hdr, hdrwords, ss, len,
  1249. plen, dwords);
  1250. return ret;
  1251. }
  1252. int qib_snapshot_counters(struct qib_pportdata *ppd, u64 *swords,
  1253. u64 *rwords, u64 *spkts, u64 *rpkts,
  1254. u64 *xmit_wait)
  1255. {
  1256. int ret;
  1257. struct qib_devdata *dd = ppd->dd;
  1258. if (!(dd->flags & QIB_PRESENT)) {
  1259. /* no hardware, freeze, etc. */
  1260. ret = -EINVAL;
  1261. goto bail;
  1262. }
  1263. *swords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDSEND);
  1264. *rwords = dd->f_portcntr(ppd, QIBPORTCNTR_WORDRCV);
  1265. *spkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTSEND);
  1266. *rpkts = dd->f_portcntr(ppd, QIBPORTCNTR_PKTRCV);
  1267. *xmit_wait = dd->f_portcntr(ppd, QIBPORTCNTR_SENDSTALL);
  1268. ret = 0;
  1269. bail:
  1270. return ret;
  1271. }
  1272. /**
  1273. * qib_get_counters - get various chip counters
  1274. * @dd: the qlogic_ib device
  1275. * @cntrs: counters are placed here
  1276. *
  1277. * Return the counters needed by recv_pma_get_portcounters().
  1278. */
  1279. int qib_get_counters(struct qib_pportdata *ppd,
  1280. struct qib_verbs_counters *cntrs)
  1281. {
  1282. int ret;
  1283. if (!(ppd->dd->flags & QIB_PRESENT)) {
  1284. /* no hardware, freeze, etc. */
  1285. ret = -EINVAL;
  1286. goto bail;
  1287. }
  1288. cntrs->symbol_error_counter =
  1289. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBSYMBOLERR);
  1290. cntrs->link_error_recovery_counter =
  1291. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKERRRECOV);
  1292. /*
  1293. * The link downed counter counts when the other side downs the
  1294. * connection. We add in the number of times we downed the link
  1295. * due to local link integrity errors to compensate.
  1296. */
  1297. cntrs->link_downed_counter =
  1298. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBLINKDOWN);
  1299. cntrs->port_rcv_errors =
  1300. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXDROPPKT) +
  1301. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVOVFL) +
  1302. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERR_RLEN) +
  1303. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_INVALIDRLEN) +
  1304. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLINK) +
  1305. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRICRC) +
  1306. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRVCRC) +
  1307. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_ERRLPCRC) +
  1308. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_BADFORMAT);
  1309. cntrs->port_rcv_errors +=
  1310. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXLOCALPHYERR);
  1311. cntrs->port_rcv_errors +=
  1312. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RXVLERR);
  1313. cntrs->port_rcv_remphys_errors =
  1314. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_RCVEBP);
  1315. cntrs->port_xmit_discards =
  1316. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_UNSUPVL);
  1317. cntrs->port_xmit_data = ppd->dd->f_portcntr(ppd,
  1318. QIBPORTCNTR_WORDSEND);
  1319. cntrs->port_rcv_data = ppd->dd->f_portcntr(ppd,
  1320. QIBPORTCNTR_WORDRCV);
  1321. cntrs->port_xmit_packets = ppd->dd->f_portcntr(ppd,
  1322. QIBPORTCNTR_PKTSEND);
  1323. cntrs->port_rcv_packets = ppd->dd->f_portcntr(ppd,
  1324. QIBPORTCNTR_PKTRCV);
  1325. cntrs->local_link_integrity_errors =
  1326. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_LLI);
  1327. cntrs->excessive_buffer_overrun_errors =
  1328. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_EXCESSBUFOVFL);
  1329. cntrs->vl15_dropped =
  1330. ppd->dd->f_portcntr(ppd, QIBPORTCNTR_VL15PKTDROP);
  1331. ret = 0;
  1332. bail:
  1333. return ret;
  1334. }
  1335. /**
  1336. * qib_ib_piobufavail - callback when a PIO buffer is available
  1337. * @dd: the device pointer
  1338. *
  1339. * This is called from qib_intr() at interrupt level when a PIO buffer is
  1340. * available after qib_verbs_send() returned an error that no buffers were
  1341. * available. Disable the interrupt if there are no more QPs waiting.
  1342. */
  1343. void qib_ib_piobufavail(struct qib_devdata *dd)
  1344. {
  1345. struct qib_ibdev *dev = &dd->verbs_dev;
  1346. struct list_head *list;
  1347. struct qib_qp *qps[5];
  1348. struct qib_qp *qp;
  1349. unsigned long flags;
  1350. unsigned i, n;
  1351. list = &dev->piowait;
  1352. n = 0;
  1353. /*
  1354. * Note: checking that the piowait list is empty and clearing
  1355. * the buffer available interrupt needs to be atomic or we
  1356. * could end up with QPs on the wait list with the interrupt
  1357. * disabled.
  1358. */
  1359. spin_lock_irqsave(&dev->pending_lock, flags);
  1360. while (!list_empty(list)) {
  1361. if (n == ARRAY_SIZE(qps))
  1362. goto full;
  1363. qp = list_entry(list->next, struct qib_qp, iowait);
  1364. list_del_init(&qp->iowait);
  1365. atomic_inc(&qp->refcount);
  1366. qps[n++] = qp;
  1367. }
  1368. dd->f_wantpiobuf_intr(dd, 0);
  1369. full:
  1370. spin_unlock_irqrestore(&dev->pending_lock, flags);
  1371. for (i = 0; i < n; i++) {
  1372. qp = qps[i];
  1373. spin_lock_irqsave(&qp->s_lock, flags);
  1374. if (qp->s_flags & QIB_S_WAIT_PIO) {
  1375. qp->s_flags &= ~QIB_S_WAIT_PIO;
  1376. qib_schedule_send(qp);
  1377. }
  1378. spin_unlock_irqrestore(&qp->s_lock, flags);
  1379. /* Notify qib_destroy_qp() if it is waiting. */
  1380. if (atomic_dec_and_test(&qp->refcount))
  1381. wake_up(&qp->wait);
  1382. }
  1383. }
  1384. static int qib_query_device(struct ib_device *ibdev,
  1385. struct ib_device_attr *props)
  1386. {
  1387. struct qib_devdata *dd = dd_from_ibdev(ibdev);
  1388. struct qib_ibdev *dev = to_idev(ibdev);
  1389. memset(props, 0, sizeof(*props));
  1390. props->device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
  1391. IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
  1392. IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
  1393. IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE;
  1394. props->page_size_cap = PAGE_SIZE;
  1395. props->vendor_id =
  1396. QIB_SRC_OUI_1 << 16 | QIB_SRC_OUI_2 << 8 | QIB_SRC_OUI_3;
  1397. props->vendor_part_id = dd->deviceid;
  1398. props->hw_ver = dd->minrev;
  1399. props->sys_image_guid = ib_qib_sys_image_guid;
  1400. props->max_mr_size = ~0ULL;
  1401. props->max_qp = ib_qib_max_qps;
  1402. props->max_qp_wr = ib_qib_max_qp_wrs;
  1403. props->max_sge = ib_qib_max_sges;
  1404. props->max_cq = ib_qib_max_cqs;
  1405. props->max_ah = ib_qib_max_ahs;
  1406. props->max_cqe = ib_qib_max_cqes;
  1407. props->max_mr = dev->lk_table.max;
  1408. props->max_fmr = dev->lk_table.max;
  1409. props->max_map_per_fmr = 32767;
  1410. props->max_pd = ib_qib_max_pds;
  1411. props->max_qp_rd_atom = QIB_MAX_RDMA_ATOMIC;
  1412. props->max_qp_init_rd_atom = 255;
  1413. /* props->max_res_rd_atom */
  1414. props->max_srq = ib_qib_max_srqs;
  1415. props->max_srq_wr = ib_qib_max_srq_wrs;
  1416. props->max_srq_sge = ib_qib_max_srq_sges;
  1417. /* props->local_ca_ack_delay */
  1418. props->atomic_cap = IB_ATOMIC_GLOB;
  1419. props->max_pkeys = qib_get_npkeys(dd);
  1420. props->max_mcast_grp = ib_qib_max_mcast_grps;
  1421. props->max_mcast_qp_attach = ib_qib_max_mcast_qp_attached;
  1422. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  1423. props->max_mcast_grp;
  1424. return 0;
  1425. }
  1426. static int qib_query_port(struct ib_device *ibdev, u8 port,
  1427. struct ib_port_attr *props)
  1428. {
  1429. struct qib_devdata *dd = dd_from_ibdev(ibdev);
  1430. struct qib_ibport *ibp = to_iport(ibdev, port);
  1431. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1432. enum ib_mtu mtu;
  1433. u16 lid = ppd->lid;
  1434. memset(props, 0, sizeof(*props));
  1435. props->lid = lid ? lid : be16_to_cpu(IB_LID_PERMISSIVE);
  1436. props->lmc = ppd->lmc;
  1437. props->sm_lid = ibp->sm_lid;
  1438. props->sm_sl = ibp->sm_sl;
  1439. props->state = dd->f_iblink_state(ppd->lastibcstat);
  1440. props->phys_state = dd->f_ibphys_portstate(ppd->lastibcstat);
  1441. props->port_cap_flags = ibp->port_cap_flags;
  1442. props->gid_tbl_len = QIB_GUIDS_PER_PORT;
  1443. props->max_msg_sz = 0x80000000;
  1444. props->pkey_tbl_len = qib_get_npkeys(dd);
  1445. props->bad_pkey_cntr = ibp->pkey_violations;
  1446. props->qkey_viol_cntr = ibp->qkey_violations;
  1447. props->active_width = ppd->link_width_active;
  1448. /* See rate_show() */
  1449. props->active_speed = ppd->link_speed_active;
  1450. props->max_vl_num = qib_num_vls(ppd->vls_supported);
  1451. props->init_type_reply = 0;
  1452. props->max_mtu = qib_ibmtu ? qib_ibmtu : IB_MTU_4096;
  1453. switch (ppd->ibmtu) {
  1454. case 4096:
  1455. mtu = IB_MTU_4096;
  1456. break;
  1457. case 2048:
  1458. mtu = IB_MTU_2048;
  1459. break;
  1460. case 1024:
  1461. mtu = IB_MTU_1024;
  1462. break;
  1463. case 512:
  1464. mtu = IB_MTU_512;
  1465. break;
  1466. case 256:
  1467. mtu = IB_MTU_256;
  1468. break;
  1469. default:
  1470. mtu = IB_MTU_2048;
  1471. }
  1472. props->active_mtu = mtu;
  1473. props->subnet_timeout = ibp->subnet_timeout;
  1474. return 0;
  1475. }
  1476. static int qib_modify_device(struct ib_device *device,
  1477. int device_modify_mask,
  1478. struct ib_device_modify *device_modify)
  1479. {
  1480. struct qib_devdata *dd = dd_from_ibdev(device);
  1481. unsigned i;
  1482. int ret;
  1483. if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
  1484. IB_DEVICE_MODIFY_NODE_DESC)) {
  1485. ret = -EOPNOTSUPP;
  1486. goto bail;
  1487. }
  1488. if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
  1489. memcpy(device->node_desc, device_modify->node_desc, 64);
  1490. for (i = 0; i < dd->num_pports; i++) {
  1491. struct qib_ibport *ibp = &dd->pport[i].ibport_data;
  1492. qib_node_desc_chg(ibp);
  1493. }
  1494. }
  1495. if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
  1496. ib_qib_sys_image_guid =
  1497. cpu_to_be64(device_modify->sys_image_guid);
  1498. for (i = 0; i < dd->num_pports; i++) {
  1499. struct qib_ibport *ibp = &dd->pport[i].ibport_data;
  1500. qib_sys_guid_chg(ibp);
  1501. }
  1502. }
  1503. ret = 0;
  1504. bail:
  1505. return ret;
  1506. }
  1507. static int qib_modify_port(struct ib_device *ibdev, u8 port,
  1508. int port_modify_mask, struct ib_port_modify *props)
  1509. {
  1510. struct qib_ibport *ibp = to_iport(ibdev, port);
  1511. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1512. ibp->port_cap_flags |= props->set_port_cap_mask;
  1513. ibp->port_cap_flags &= ~props->clr_port_cap_mask;
  1514. if (props->set_port_cap_mask || props->clr_port_cap_mask)
  1515. qib_cap_mask_chg(ibp);
  1516. if (port_modify_mask & IB_PORT_SHUTDOWN)
  1517. qib_set_linkstate(ppd, QIB_IB_LINKDOWN);
  1518. if (port_modify_mask & IB_PORT_RESET_QKEY_CNTR)
  1519. ibp->qkey_violations = 0;
  1520. return 0;
  1521. }
  1522. static int qib_query_gid(struct ib_device *ibdev, u8 port,
  1523. int index, union ib_gid *gid)
  1524. {
  1525. struct qib_devdata *dd = dd_from_ibdev(ibdev);
  1526. int ret = 0;
  1527. if (!port || port > dd->num_pports)
  1528. ret = -EINVAL;
  1529. else {
  1530. struct qib_ibport *ibp = to_iport(ibdev, port);
  1531. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1532. gid->global.subnet_prefix = ibp->gid_prefix;
  1533. if (index == 0)
  1534. gid->global.interface_id = ppd->guid;
  1535. else if (index < QIB_GUIDS_PER_PORT)
  1536. gid->global.interface_id = ibp->guids[index - 1];
  1537. else
  1538. ret = -EINVAL;
  1539. }
  1540. return ret;
  1541. }
  1542. static struct ib_pd *qib_alloc_pd(struct ib_device *ibdev,
  1543. struct ib_ucontext *context,
  1544. struct ib_udata *udata)
  1545. {
  1546. struct qib_ibdev *dev = to_idev(ibdev);
  1547. struct qib_pd *pd;
  1548. struct ib_pd *ret;
  1549. /*
  1550. * This is actually totally arbitrary. Some correctness tests
  1551. * assume there's a maximum number of PDs that can be allocated.
  1552. * We don't actually have this limit, but we fail the test if
  1553. * we allow allocations of more than we report for this value.
  1554. */
  1555. pd = kmalloc(sizeof *pd, GFP_KERNEL);
  1556. if (!pd) {
  1557. ret = ERR_PTR(-ENOMEM);
  1558. goto bail;
  1559. }
  1560. spin_lock(&dev->n_pds_lock);
  1561. if (dev->n_pds_allocated == ib_qib_max_pds) {
  1562. spin_unlock(&dev->n_pds_lock);
  1563. kfree(pd);
  1564. ret = ERR_PTR(-ENOMEM);
  1565. goto bail;
  1566. }
  1567. dev->n_pds_allocated++;
  1568. spin_unlock(&dev->n_pds_lock);
  1569. /* ib_alloc_pd() will initialize pd->ibpd. */
  1570. pd->user = udata != NULL;
  1571. ret = &pd->ibpd;
  1572. bail:
  1573. return ret;
  1574. }
  1575. static int qib_dealloc_pd(struct ib_pd *ibpd)
  1576. {
  1577. struct qib_pd *pd = to_ipd(ibpd);
  1578. struct qib_ibdev *dev = to_idev(ibpd->device);
  1579. spin_lock(&dev->n_pds_lock);
  1580. dev->n_pds_allocated--;
  1581. spin_unlock(&dev->n_pds_lock);
  1582. kfree(pd);
  1583. return 0;
  1584. }
  1585. int qib_check_ah(struct ib_device *ibdev, struct ib_ah_attr *ah_attr)
  1586. {
  1587. /* A multicast address requires a GRH (see ch. 8.4.1). */
  1588. if (ah_attr->dlid >= QIB_MULTICAST_LID_BASE &&
  1589. ah_attr->dlid != QIB_PERMISSIVE_LID &&
  1590. !(ah_attr->ah_flags & IB_AH_GRH))
  1591. goto bail;
  1592. if ((ah_attr->ah_flags & IB_AH_GRH) &&
  1593. ah_attr->grh.sgid_index >= QIB_GUIDS_PER_PORT)
  1594. goto bail;
  1595. if (ah_attr->dlid == 0)
  1596. goto bail;
  1597. if (ah_attr->port_num < 1 ||
  1598. ah_attr->port_num > ibdev->phys_port_cnt)
  1599. goto bail;
  1600. if (ah_attr->static_rate != IB_RATE_PORT_CURRENT &&
  1601. ib_rate_to_mult(ah_attr->static_rate) < 0)
  1602. goto bail;
  1603. if (ah_attr->sl > 15)
  1604. goto bail;
  1605. return 0;
  1606. bail:
  1607. return -EINVAL;
  1608. }
  1609. /**
  1610. * qib_create_ah - create an address handle
  1611. * @pd: the protection domain
  1612. * @ah_attr: the attributes of the AH
  1613. *
  1614. * This may be called from interrupt context.
  1615. */
  1616. static struct ib_ah *qib_create_ah(struct ib_pd *pd,
  1617. struct ib_ah_attr *ah_attr)
  1618. {
  1619. struct qib_ah *ah;
  1620. struct ib_ah *ret;
  1621. struct qib_ibdev *dev = to_idev(pd->device);
  1622. unsigned long flags;
  1623. if (qib_check_ah(pd->device, ah_attr)) {
  1624. ret = ERR_PTR(-EINVAL);
  1625. goto bail;
  1626. }
  1627. ah = kmalloc(sizeof *ah, GFP_ATOMIC);
  1628. if (!ah) {
  1629. ret = ERR_PTR(-ENOMEM);
  1630. goto bail;
  1631. }
  1632. spin_lock_irqsave(&dev->n_ahs_lock, flags);
  1633. if (dev->n_ahs_allocated == ib_qib_max_ahs) {
  1634. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  1635. kfree(ah);
  1636. ret = ERR_PTR(-ENOMEM);
  1637. goto bail;
  1638. }
  1639. dev->n_ahs_allocated++;
  1640. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  1641. /* ib_create_ah() will initialize ah->ibah. */
  1642. ah->attr = *ah_attr;
  1643. atomic_set(&ah->refcount, 0);
  1644. ret = &ah->ibah;
  1645. bail:
  1646. return ret;
  1647. }
  1648. /**
  1649. * qib_destroy_ah - destroy an address handle
  1650. * @ibah: the AH to destroy
  1651. *
  1652. * This may be called from interrupt context.
  1653. */
  1654. static int qib_destroy_ah(struct ib_ah *ibah)
  1655. {
  1656. struct qib_ibdev *dev = to_idev(ibah->device);
  1657. struct qib_ah *ah = to_iah(ibah);
  1658. unsigned long flags;
  1659. if (atomic_read(&ah->refcount) != 0)
  1660. return -EBUSY;
  1661. spin_lock_irqsave(&dev->n_ahs_lock, flags);
  1662. dev->n_ahs_allocated--;
  1663. spin_unlock_irqrestore(&dev->n_ahs_lock, flags);
  1664. kfree(ah);
  1665. return 0;
  1666. }
  1667. static int qib_modify_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
  1668. {
  1669. struct qib_ah *ah = to_iah(ibah);
  1670. if (qib_check_ah(ibah->device, ah_attr))
  1671. return -EINVAL;
  1672. ah->attr = *ah_attr;
  1673. return 0;
  1674. }
  1675. static int qib_query_ah(struct ib_ah *ibah, struct ib_ah_attr *ah_attr)
  1676. {
  1677. struct qib_ah *ah = to_iah(ibah);
  1678. *ah_attr = ah->attr;
  1679. return 0;
  1680. }
  1681. /**
  1682. * qib_get_npkeys - return the size of the PKEY table for context 0
  1683. * @dd: the qlogic_ib device
  1684. */
  1685. unsigned qib_get_npkeys(struct qib_devdata *dd)
  1686. {
  1687. return ARRAY_SIZE(dd->rcd[0]->pkeys);
  1688. }
  1689. /*
  1690. * Return the indexed PKEY from the port PKEY table.
  1691. * No need to validate rcd[ctxt]; the port is setup if we are here.
  1692. */
  1693. unsigned qib_get_pkey(struct qib_ibport *ibp, unsigned index)
  1694. {
  1695. struct qib_pportdata *ppd = ppd_from_ibp(ibp);
  1696. struct qib_devdata *dd = ppd->dd;
  1697. unsigned ctxt = ppd->hw_pidx;
  1698. unsigned ret;
  1699. /* dd->rcd null if mini_init or some init failures */
  1700. if (!dd->rcd || index >= ARRAY_SIZE(dd->rcd[ctxt]->pkeys))
  1701. ret = 0;
  1702. else
  1703. ret = dd->rcd[ctxt]->pkeys[index];
  1704. return ret;
  1705. }
  1706. static int qib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  1707. u16 *pkey)
  1708. {
  1709. struct qib_devdata *dd = dd_from_ibdev(ibdev);
  1710. int ret;
  1711. if (index >= qib_get_npkeys(dd)) {
  1712. ret = -EINVAL;
  1713. goto bail;
  1714. }
  1715. *pkey = qib_get_pkey(to_iport(ibdev, port), index);
  1716. ret = 0;
  1717. bail:
  1718. return ret;
  1719. }
  1720. /**
  1721. * qib_alloc_ucontext - allocate a ucontest
  1722. * @ibdev: the infiniband device
  1723. * @udata: not used by the QLogic_IB driver
  1724. */
  1725. static struct ib_ucontext *qib_alloc_ucontext(struct ib_device *ibdev,
  1726. struct ib_udata *udata)
  1727. {
  1728. struct qib_ucontext *context;
  1729. struct ib_ucontext *ret;
  1730. context = kmalloc(sizeof *context, GFP_KERNEL);
  1731. if (!context) {
  1732. ret = ERR_PTR(-ENOMEM);
  1733. goto bail;
  1734. }
  1735. ret = &context->ibucontext;
  1736. bail:
  1737. return ret;
  1738. }
  1739. static int qib_dealloc_ucontext(struct ib_ucontext *context)
  1740. {
  1741. kfree(to_iucontext(context));
  1742. return 0;
  1743. }
  1744. static void init_ibport(struct qib_pportdata *ppd)
  1745. {
  1746. struct qib_verbs_counters cntrs;
  1747. struct qib_ibport *ibp = &ppd->ibport_data;
  1748. spin_lock_init(&ibp->lock);
  1749. /* Set the prefix to the default value (see ch. 4.1.1) */
  1750. ibp->gid_prefix = IB_DEFAULT_GID_PREFIX;
  1751. ibp->sm_lid = be16_to_cpu(IB_LID_PERMISSIVE);
  1752. ibp->port_cap_flags = IB_PORT_SYS_IMAGE_GUID_SUP |
  1753. IB_PORT_CLIENT_REG_SUP | IB_PORT_SL_MAP_SUP |
  1754. IB_PORT_TRAP_SUP | IB_PORT_AUTO_MIGR_SUP |
  1755. IB_PORT_DR_NOTICE_SUP | IB_PORT_CAP_MASK_NOTICE_SUP |
  1756. IB_PORT_OTHER_LOCAL_CHANGES_SUP;
  1757. if (ppd->dd->flags & QIB_HAS_LINK_LATENCY)
  1758. ibp->port_cap_flags |= IB_PORT_LINK_LATENCY_SUP;
  1759. ibp->pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
  1760. ibp->pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
  1761. ibp->pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
  1762. ibp->pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
  1763. ibp->pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
  1764. /* Snapshot current HW counters to "clear" them. */
  1765. qib_get_counters(ppd, &cntrs);
  1766. ibp->z_symbol_error_counter = cntrs.symbol_error_counter;
  1767. ibp->z_link_error_recovery_counter =
  1768. cntrs.link_error_recovery_counter;
  1769. ibp->z_link_downed_counter = cntrs.link_downed_counter;
  1770. ibp->z_port_rcv_errors = cntrs.port_rcv_errors;
  1771. ibp->z_port_rcv_remphys_errors = cntrs.port_rcv_remphys_errors;
  1772. ibp->z_port_xmit_discards = cntrs.port_xmit_discards;
  1773. ibp->z_port_xmit_data = cntrs.port_xmit_data;
  1774. ibp->z_port_rcv_data = cntrs.port_rcv_data;
  1775. ibp->z_port_xmit_packets = cntrs.port_xmit_packets;
  1776. ibp->z_port_rcv_packets = cntrs.port_rcv_packets;
  1777. ibp->z_local_link_integrity_errors =
  1778. cntrs.local_link_integrity_errors;
  1779. ibp->z_excessive_buffer_overrun_errors =
  1780. cntrs.excessive_buffer_overrun_errors;
  1781. ibp->z_vl15_dropped = cntrs.vl15_dropped;
  1782. RCU_INIT_POINTER(ibp->qp0, NULL);
  1783. RCU_INIT_POINTER(ibp->qp1, NULL);
  1784. }
  1785. /**
  1786. * qib_register_ib_device - register our device with the infiniband core
  1787. * @dd: the device data structure
  1788. * Return the allocated qib_ibdev pointer or NULL on error.
  1789. */
  1790. int qib_register_ib_device(struct qib_devdata *dd)
  1791. {
  1792. struct qib_ibdev *dev = &dd->verbs_dev;
  1793. struct ib_device *ibdev = &dev->ibdev;
  1794. struct qib_pportdata *ppd = dd->pport;
  1795. unsigned i, lk_tab_size;
  1796. int ret;
  1797. dev->qp_table_size = ib_qib_qp_table_size;
  1798. get_random_bytes(&dev->qp_rnd, sizeof(dev->qp_rnd));
  1799. dev->qp_table = kmalloc(dev->qp_table_size * sizeof *dev->qp_table,
  1800. GFP_KERNEL);
  1801. if (!dev->qp_table) {
  1802. ret = -ENOMEM;
  1803. goto err_qpt;
  1804. }
  1805. for (i = 0; i < dev->qp_table_size; i++)
  1806. RCU_INIT_POINTER(dev->qp_table[i], NULL);
  1807. for (i = 0; i < dd->num_pports; i++)
  1808. init_ibport(ppd + i);
  1809. /* Only need to initialize non-zero fields. */
  1810. spin_lock_init(&dev->qpt_lock);
  1811. spin_lock_init(&dev->n_pds_lock);
  1812. spin_lock_init(&dev->n_ahs_lock);
  1813. spin_lock_init(&dev->n_cqs_lock);
  1814. spin_lock_init(&dev->n_qps_lock);
  1815. spin_lock_init(&dev->n_srqs_lock);
  1816. spin_lock_init(&dev->n_mcast_grps_lock);
  1817. init_timer(&dev->mem_timer);
  1818. dev->mem_timer.function = mem_timer;
  1819. dev->mem_timer.data = (unsigned long) dev;
  1820. qib_init_qpn_table(dd, &dev->qpn_table);
  1821. /*
  1822. * The top ib_qib_lkey_table_size bits are used to index the
  1823. * table. The lower 8 bits can be owned by the user (copied from
  1824. * the LKEY). The remaining bits act as a generation number or tag.
  1825. */
  1826. spin_lock_init(&dev->lk_table.lock);
  1827. /* insure generation is at least 4 bits see keys.c */
  1828. if (ib_qib_lkey_table_size > MAX_LKEY_TABLE_BITS) {
  1829. qib_dev_warn(dd, "lkey bits %u too large, reduced to %u\n",
  1830. ib_qib_lkey_table_size, MAX_LKEY_TABLE_BITS);
  1831. ib_qib_lkey_table_size = MAX_LKEY_TABLE_BITS;
  1832. }
  1833. dev->lk_table.max = 1 << ib_qib_lkey_table_size;
  1834. lk_tab_size = dev->lk_table.max * sizeof(*dev->lk_table.table);
  1835. dev->lk_table.table = (struct qib_mregion **)
  1836. vmalloc(lk_tab_size);
  1837. if (dev->lk_table.table == NULL) {
  1838. ret = -ENOMEM;
  1839. goto err_lk;
  1840. }
  1841. memset(dev->lk_table.table, 0, lk_tab_size);
  1842. INIT_LIST_HEAD(&dev->pending_mmaps);
  1843. spin_lock_init(&dev->pending_lock);
  1844. dev->mmap_offset = PAGE_SIZE;
  1845. spin_lock_init(&dev->mmap_offset_lock);
  1846. INIT_LIST_HEAD(&dev->piowait);
  1847. INIT_LIST_HEAD(&dev->dmawait);
  1848. INIT_LIST_HEAD(&dev->txwait);
  1849. INIT_LIST_HEAD(&dev->memwait);
  1850. INIT_LIST_HEAD(&dev->txreq_free);
  1851. if (ppd->sdma_descq_cnt) {
  1852. dev->pio_hdrs = dma_alloc_coherent(&dd->pcidev->dev,
  1853. ppd->sdma_descq_cnt *
  1854. sizeof(struct qib_pio_header),
  1855. &dev->pio_hdrs_phys,
  1856. GFP_KERNEL);
  1857. if (!dev->pio_hdrs) {
  1858. ret = -ENOMEM;
  1859. goto err_hdrs;
  1860. }
  1861. }
  1862. for (i = 0; i < ppd->sdma_descq_cnt; i++) {
  1863. struct qib_verbs_txreq *tx;
  1864. tx = kzalloc(sizeof *tx, GFP_KERNEL);
  1865. if (!tx) {
  1866. ret = -ENOMEM;
  1867. goto err_tx;
  1868. }
  1869. tx->hdr_inx = i;
  1870. list_add(&tx->txreq.list, &dev->txreq_free);
  1871. }
  1872. /*
  1873. * The system image GUID is supposed to be the same for all
  1874. * IB HCAs in a single system but since there can be other
  1875. * device types in the system, we can't be sure this is unique.
  1876. */
  1877. if (!ib_qib_sys_image_guid)
  1878. ib_qib_sys_image_guid = ppd->guid;
  1879. strlcpy(ibdev->name, "qib%d", IB_DEVICE_NAME_MAX);
  1880. ibdev->owner = THIS_MODULE;
  1881. ibdev->node_guid = ppd->guid;
  1882. ibdev->uverbs_abi_ver = QIB_UVERBS_ABI_VERSION;
  1883. ibdev->uverbs_cmd_mask =
  1884. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  1885. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  1886. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  1887. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  1888. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  1889. (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
  1890. (1ull << IB_USER_VERBS_CMD_MODIFY_AH) |
  1891. (1ull << IB_USER_VERBS_CMD_QUERY_AH) |
  1892. (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
  1893. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  1894. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  1895. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  1896. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  1897. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  1898. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  1899. (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
  1900. (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
  1901. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  1902. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  1903. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  1904. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  1905. (1ull << IB_USER_VERBS_CMD_POST_SEND) |
  1906. (1ull << IB_USER_VERBS_CMD_POST_RECV) |
  1907. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  1908. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  1909. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  1910. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  1911. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  1912. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
  1913. (1ull << IB_USER_VERBS_CMD_POST_SRQ_RECV);
  1914. ibdev->node_type = RDMA_NODE_IB_CA;
  1915. ibdev->phys_port_cnt = dd->num_pports;
  1916. ibdev->num_comp_vectors = 1;
  1917. ibdev->dma_device = &dd->pcidev->dev;
  1918. ibdev->query_device = qib_query_device;
  1919. ibdev->modify_device = qib_modify_device;
  1920. ibdev->query_port = qib_query_port;
  1921. ibdev->modify_port = qib_modify_port;
  1922. ibdev->query_pkey = qib_query_pkey;
  1923. ibdev->query_gid = qib_query_gid;
  1924. ibdev->alloc_ucontext = qib_alloc_ucontext;
  1925. ibdev->dealloc_ucontext = qib_dealloc_ucontext;
  1926. ibdev->alloc_pd = qib_alloc_pd;
  1927. ibdev->dealloc_pd = qib_dealloc_pd;
  1928. ibdev->create_ah = qib_create_ah;
  1929. ibdev->destroy_ah = qib_destroy_ah;
  1930. ibdev->modify_ah = qib_modify_ah;
  1931. ibdev->query_ah = qib_query_ah;
  1932. ibdev->create_srq = qib_create_srq;
  1933. ibdev->modify_srq = qib_modify_srq;
  1934. ibdev->query_srq = qib_query_srq;
  1935. ibdev->destroy_srq = qib_destroy_srq;
  1936. ibdev->create_qp = qib_create_qp;
  1937. ibdev->modify_qp = qib_modify_qp;
  1938. ibdev->query_qp = qib_query_qp;
  1939. ibdev->destroy_qp = qib_destroy_qp;
  1940. ibdev->post_send = qib_post_send;
  1941. ibdev->post_recv = qib_post_receive;
  1942. ibdev->post_srq_recv = qib_post_srq_receive;
  1943. ibdev->create_cq = qib_create_cq;
  1944. ibdev->destroy_cq = qib_destroy_cq;
  1945. ibdev->resize_cq = qib_resize_cq;
  1946. ibdev->poll_cq = qib_poll_cq;
  1947. ibdev->req_notify_cq = qib_req_notify_cq;
  1948. ibdev->get_dma_mr = qib_get_dma_mr;
  1949. ibdev->reg_phys_mr = qib_reg_phys_mr;
  1950. ibdev->reg_user_mr = qib_reg_user_mr;
  1951. ibdev->dereg_mr = qib_dereg_mr;
  1952. ibdev->alloc_fast_reg_mr = qib_alloc_fast_reg_mr;
  1953. ibdev->alloc_fast_reg_page_list = qib_alloc_fast_reg_page_list;
  1954. ibdev->free_fast_reg_page_list = qib_free_fast_reg_page_list;
  1955. ibdev->alloc_fmr = qib_alloc_fmr;
  1956. ibdev->map_phys_fmr = qib_map_phys_fmr;
  1957. ibdev->unmap_fmr = qib_unmap_fmr;
  1958. ibdev->dealloc_fmr = qib_dealloc_fmr;
  1959. ibdev->attach_mcast = qib_multicast_attach;
  1960. ibdev->detach_mcast = qib_multicast_detach;
  1961. ibdev->process_mad = qib_process_mad;
  1962. ibdev->mmap = qib_mmap;
  1963. ibdev->dma_ops = &qib_dma_mapping_ops;
  1964. snprintf(ibdev->node_desc, sizeof(ibdev->node_desc),
  1965. QIB_IDSTR " %s", init_utsname()->nodename);
  1966. ret = ib_register_device(ibdev, qib_create_port_files);
  1967. if (ret)
  1968. goto err_reg;
  1969. ret = qib_create_agents(dev);
  1970. if (ret)
  1971. goto err_agents;
  1972. if (qib_verbs_register_sysfs(dd))
  1973. goto err_class;
  1974. goto bail;
  1975. err_class:
  1976. qib_free_agents(dev);
  1977. err_agents:
  1978. ib_unregister_device(ibdev);
  1979. err_reg:
  1980. err_tx:
  1981. while (!list_empty(&dev->txreq_free)) {
  1982. struct list_head *l = dev->txreq_free.next;
  1983. struct qib_verbs_txreq *tx;
  1984. list_del(l);
  1985. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  1986. kfree(tx);
  1987. }
  1988. if (ppd->sdma_descq_cnt)
  1989. dma_free_coherent(&dd->pcidev->dev,
  1990. ppd->sdma_descq_cnt *
  1991. sizeof(struct qib_pio_header),
  1992. dev->pio_hdrs, dev->pio_hdrs_phys);
  1993. err_hdrs:
  1994. vfree(dev->lk_table.table);
  1995. err_lk:
  1996. kfree(dev->qp_table);
  1997. err_qpt:
  1998. qib_dev_err(dd, "cannot register verbs: %d!\n", -ret);
  1999. bail:
  2000. return ret;
  2001. }
  2002. void qib_unregister_ib_device(struct qib_devdata *dd)
  2003. {
  2004. struct qib_ibdev *dev = &dd->verbs_dev;
  2005. struct ib_device *ibdev = &dev->ibdev;
  2006. u32 qps_inuse;
  2007. unsigned lk_tab_size;
  2008. qib_verbs_unregister_sysfs(dd);
  2009. qib_free_agents(dev);
  2010. ib_unregister_device(ibdev);
  2011. if (!list_empty(&dev->piowait))
  2012. qib_dev_err(dd, "piowait list not empty!\n");
  2013. if (!list_empty(&dev->dmawait))
  2014. qib_dev_err(dd, "dmawait list not empty!\n");
  2015. if (!list_empty(&dev->txwait))
  2016. qib_dev_err(dd, "txwait list not empty!\n");
  2017. if (!list_empty(&dev->memwait))
  2018. qib_dev_err(dd, "memwait list not empty!\n");
  2019. if (dev->dma_mr)
  2020. qib_dev_err(dd, "DMA MR not NULL!\n");
  2021. qps_inuse = qib_free_all_qps(dd);
  2022. if (qps_inuse)
  2023. qib_dev_err(dd, "QP memory leak! %u still in use\n",
  2024. qps_inuse);
  2025. del_timer_sync(&dev->mem_timer);
  2026. qib_free_qpn_table(&dev->qpn_table);
  2027. while (!list_empty(&dev->txreq_free)) {
  2028. struct list_head *l = dev->txreq_free.next;
  2029. struct qib_verbs_txreq *tx;
  2030. list_del(l);
  2031. tx = list_entry(l, struct qib_verbs_txreq, txreq.list);
  2032. kfree(tx);
  2033. }
  2034. if (dd->pport->sdma_descq_cnt)
  2035. dma_free_coherent(&dd->pcidev->dev,
  2036. dd->pport->sdma_descq_cnt *
  2037. sizeof(struct qib_pio_header),
  2038. dev->pio_hdrs, dev->pio_hdrs_phys);
  2039. lk_tab_size = dev->lk_table.max * sizeof(*dev->lk_table.table);
  2040. vfree(dev->lk_table.table);
  2041. kfree(dev->qp_table);
  2042. }