ipath_verbs.c 62 KB

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