qib_verbs.c 61 KB

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