ipath_verbs.c 62 KB

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