ipath_verbs.c 49 KB

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