verbs.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  1. /*
  2. * Copyright(c) 2015 - 2018 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <rdma/ib_mad.h>
  48. #include <rdma/ib_user_verbs.h>
  49. #include <linux/io.h>
  50. #include <linux/module.h>
  51. #include <linux/utsname.h>
  52. #include <linux/rculist.h>
  53. #include <linux/mm.h>
  54. #include <linux/vmalloc.h>
  55. #include <rdma/opa_addr.h>
  56. #include "hfi.h"
  57. #include "common.h"
  58. #include "device.h"
  59. #include "trace.h"
  60. #include "qp.h"
  61. #include "verbs_txreq.h"
  62. #include "debugfs.h"
  63. #include "vnic.h"
  64. #include "fault.h"
  65. #include "affinity.h"
  66. static unsigned int hfi1_lkey_table_size = 16;
  67. module_param_named(lkey_table_size, hfi1_lkey_table_size, uint,
  68. S_IRUGO);
  69. MODULE_PARM_DESC(lkey_table_size,
  70. "LKEY table size in bits (2^n, 1 <= n <= 23)");
  71. static unsigned int hfi1_max_pds = 0xFFFF;
  72. module_param_named(max_pds, hfi1_max_pds, uint, S_IRUGO);
  73. MODULE_PARM_DESC(max_pds,
  74. "Maximum number of protection domains to support");
  75. static unsigned int hfi1_max_ahs = 0xFFFF;
  76. module_param_named(max_ahs, hfi1_max_ahs, uint, S_IRUGO);
  77. MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
  78. unsigned int hfi1_max_cqes = 0x2FFFFF;
  79. module_param_named(max_cqes, hfi1_max_cqes, uint, S_IRUGO);
  80. MODULE_PARM_DESC(max_cqes,
  81. "Maximum number of completion queue entries to support");
  82. unsigned int hfi1_max_cqs = 0x1FFFF;
  83. module_param_named(max_cqs, hfi1_max_cqs, uint, S_IRUGO);
  84. MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
  85. unsigned int hfi1_max_qp_wrs = 0x3FFF;
  86. module_param_named(max_qp_wrs, hfi1_max_qp_wrs, uint, S_IRUGO);
  87. MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
  88. unsigned int hfi1_max_qps = 32768;
  89. module_param_named(max_qps, hfi1_max_qps, uint, S_IRUGO);
  90. MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
  91. unsigned int hfi1_max_sges = 0x60;
  92. module_param_named(max_sges, hfi1_max_sges, uint, S_IRUGO);
  93. MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
  94. unsigned int hfi1_max_mcast_grps = 16384;
  95. module_param_named(max_mcast_grps, hfi1_max_mcast_grps, uint, S_IRUGO);
  96. MODULE_PARM_DESC(max_mcast_grps,
  97. "Maximum number of multicast groups to support");
  98. unsigned int hfi1_max_mcast_qp_attached = 16;
  99. module_param_named(max_mcast_qp_attached, hfi1_max_mcast_qp_attached,
  100. uint, S_IRUGO);
  101. MODULE_PARM_DESC(max_mcast_qp_attached,
  102. "Maximum number of attached QPs to support");
  103. unsigned int hfi1_max_srqs = 1024;
  104. module_param_named(max_srqs, hfi1_max_srqs, uint, S_IRUGO);
  105. MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
  106. unsigned int hfi1_max_srq_sges = 128;
  107. module_param_named(max_srq_sges, hfi1_max_srq_sges, uint, S_IRUGO);
  108. MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
  109. unsigned int hfi1_max_srq_wrs = 0x1FFFF;
  110. module_param_named(max_srq_wrs, hfi1_max_srq_wrs, uint, S_IRUGO);
  111. MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
  112. unsigned short piothreshold = 256;
  113. module_param(piothreshold, ushort, S_IRUGO);
  114. MODULE_PARM_DESC(piothreshold, "size used to determine sdma vs. pio");
  115. static unsigned int sge_copy_mode;
  116. module_param(sge_copy_mode, uint, S_IRUGO);
  117. MODULE_PARM_DESC(sge_copy_mode,
  118. "Verbs copy mode: 0 use memcpy, 1 use cacheless copy, 2 adapt based on WSS");
  119. static void verbs_sdma_complete(
  120. struct sdma_txreq *cookie,
  121. int status);
  122. static int pio_wait(struct rvt_qp *qp,
  123. struct send_context *sc,
  124. struct hfi1_pkt_state *ps,
  125. u32 flag);
  126. /* Length of buffer to create verbs txreq cache name */
  127. #define TXREQ_NAME_LEN 24
  128. /* 16B trailing buffer */
  129. static const u8 trail_buf[MAX_16B_PADDING];
  130. static uint wss_threshold = 80;
  131. module_param(wss_threshold, uint, S_IRUGO);
  132. MODULE_PARM_DESC(wss_threshold, "Percentage (1-100) of LLC to use as a threshold for a cacheless copy");
  133. static uint wss_clean_period = 256;
  134. module_param(wss_clean_period, uint, S_IRUGO);
  135. MODULE_PARM_DESC(wss_clean_period, "Count of verbs copies before an entry in the page copy table is cleaned");
  136. /*
  137. * Translate ib_wr_opcode into ib_wc_opcode.
  138. */
  139. const enum ib_wc_opcode ib_hfi1_wc_opcode[] = {
  140. [IB_WR_RDMA_WRITE] = IB_WC_RDMA_WRITE,
  141. [IB_WR_RDMA_WRITE_WITH_IMM] = IB_WC_RDMA_WRITE,
  142. [IB_WR_SEND] = IB_WC_SEND,
  143. [IB_WR_SEND_WITH_IMM] = IB_WC_SEND,
  144. [IB_WR_RDMA_READ] = IB_WC_RDMA_READ,
  145. [IB_WR_ATOMIC_CMP_AND_SWP] = IB_WC_COMP_SWAP,
  146. [IB_WR_ATOMIC_FETCH_AND_ADD] = IB_WC_FETCH_ADD,
  147. [IB_WR_SEND_WITH_INV] = IB_WC_SEND,
  148. [IB_WR_LOCAL_INV] = IB_WC_LOCAL_INV,
  149. [IB_WR_REG_MR] = IB_WC_REG_MR
  150. };
  151. /*
  152. * Length of header by opcode, 0 --> not supported
  153. */
  154. const u8 hdr_len_by_opcode[256] = {
  155. /* RC */
  156. [IB_OPCODE_RC_SEND_FIRST] = 12 + 8,
  157. [IB_OPCODE_RC_SEND_MIDDLE] = 12 + 8,
  158. [IB_OPCODE_RC_SEND_LAST] = 12 + 8,
  159. [IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE] = 12 + 8 + 4,
  160. [IB_OPCODE_RC_SEND_ONLY] = 12 + 8,
  161. [IB_OPCODE_RC_SEND_ONLY_WITH_IMMEDIATE] = 12 + 8 + 4,
  162. [IB_OPCODE_RC_RDMA_WRITE_FIRST] = 12 + 8 + 16,
  163. [IB_OPCODE_RC_RDMA_WRITE_MIDDLE] = 12 + 8,
  164. [IB_OPCODE_RC_RDMA_WRITE_LAST] = 12 + 8,
  165. [IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE] = 12 + 8 + 4,
  166. [IB_OPCODE_RC_RDMA_WRITE_ONLY] = 12 + 8 + 16,
  167. [IB_OPCODE_RC_RDMA_WRITE_ONLY_WITH_IMMEDIATE] = 12 + 8 + 20,
  168. [IB_OPCODE_RC_RDMA_READ_REQUEST] = 12 + 8 + 16,
  169. [IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST] = 12 + 8 + 4,
  170. [IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE] = 12 + 8,
  171. [IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST] = 12 + 8 + 4,
  172. [IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY] = 12 + 8 + 4,
  173. [IB_OPCODE_RC_ACKNOWLEDGE] = 12 + 8 + 4,
  174. [IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE] = 12 + 8 + 4 + 8,
  175. [IB_OPCODE_RC_COMPARE_SWAP] = 12 + 8 + 28,
  176. [IB_OPCODE_RC_FETCH_ADD] = 12 + 8 + 28,
  177. [IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE] = 12 + 8 + 4,
  178. [IB_OPCODE_RC_SEND_ONLY_WITH_INVALIDATE] = 12 + 8 + 4,
  179. /* UC */
  180. [IB_OPCODE_UC_SEND_FIRST] = 12 + 8,
  181. [IB_OPCODE_UC_SEND_MIDDLE] = 12 + 8,
  182. [IB_OPCODE_UC_SEND_LAST] = 12 + 8,
  183. [IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE] = 12 + 8 + 4,
  184. [IB_OPCODE_UC_SEND_ONLY] = 12 + 8,
  185. [IB_OPCODE_UC_SEND_ONLY_WITH_IMMEDIATE] = 12 + 8 + 4,
  186. [IB_OPCODE_UC_RDMA_WRITE_FIRST] = 12 + 8 + 16,
  187. [IB_OPCODE_UC_RDMA_WRITE_MIDDLE] = 12 + 8,
  188. [IB_OPCODE_UC_RDMA_WRITE_LAST] = 12 + 8,
  189. [IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE] = 12 + 8 + 4,
  190. [IB_OPCODE_UC_RDMA_WRITE_ONLY] = 12 + 8 + 16,
  191. [IB_OPCODE_UC_RDMA_WRITE_ONLY_WITH_IMMEDIATE] = 12 + 8 + 20,
  192. /* UD */
  193. [IB_OPCODE_UD_SEND_ONLY] = 12 + 8 + 8,
  194. [IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE] = 12 + 8 + 12
  195. };
  196. static const opcode_handler opcode_handler_tbl[256] = {
  197. /* RC */
  198. [IB_OPCODE_RC_SEND_FIRST] = &hfi1_rc_rcv,
  199. [IB_OPCODE_RC_SEND_MIDDLE] = &hfi1_rc_rcv,
  200. [IB_OPCODE_RC_SEND_LAST] = &hfi1_rc_rcv,
  201. [IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE] = &hfi1_rc_rcv,
  202. [IB_OPCODE_RC_SEND_ONLY] = &hfi1_rc_rcv,
  203. [IB_OPCODE_RC_SEND_ONLY_WITH_IMMEDIATE] = &hfi1_rc_rcv,
  204. [IB_OPCODE_RC_RDMA_WRITE_FIRST] = &hfi1_rc_rcv,
  205. [IB_OPCODE_RC_RDMA_WRITE_MIDDLE] = &hfi1_rc_rcv,
  206. [IB_OPCODE_RC_RDMA_WRITE_LAST] = &hfi1_rc_rcv,
  207. [IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE] = &hfi1_rc_rcv,
  208. [IB_OPCODE_RC_RDMA_WRITE_ONLY] = &hfi1_rc_rcv,
  209. [IB_OPCODE_RC_RDMA_WRITE_ONLY_WITH_IMMEDIATE] = &hfi1_rc_rcv,
  210. [IB_OPCODE_RC_RDMA_READ_REQUEST] = &hfi1_rc_rcv,
  211. [IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST] = &hfi1_rc_rcv,
  212. [IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE] = &hfi1_rc_rcv,
  213. [IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST] = &hfi1_rc_rcv,
  214. [IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY] = &hfi1_rc_rcv,
  215. [IB_OPCODE_RC_ACKNOWLEDGE] = &hfi1_rc_rcv,
  216. [IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE] = &hfi1_rc_rcv,
  217. [IB_OPCODE_RC_COMPARE_SWAP] = &hfi1_rc_rcv,
  218. [IB_OPCODE_RC_FETCH_ADD] = &hfi1_rc_rcv,
  219. [IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE] = &hfi1_rc_rcv,
  220. [IB_OPCODE_RC_SEND_ONLY_WITH_INVALIDATE] = &hfi1_rc_rcv,
  221. /* UC */
  222. [IB_OPCODE_UC_SEND_FIRST] = &hfi1_uc_rcv,
  223. [IB_OPCODE_UC_SEND_MIDDLE] = &hfi1_uc_rcv,
  224. [IB_OPCODE_UC_SEND_LAST] = &hfi1_uc_rcv,
  225. [IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE] = &hfi1_uc_rcv,
  226. [IB_OPCODE_UC_SEND_ONLY] = &hfi1_uc_rcv,
  227. [IB_OPCODE_UC_SEND_ONLY_WITH_IMMEDIATE] = &hfi1_uc_rcv,
  228. [IB_OPCODE_UC_RDMA_WRITE_FIRST] = &hfi1_uc_rcv,
  229. [IB_OPCODE_UC_RDMA_WRITE_MIDDLE] = &hfi1_uc_rcv,
  230. [IB_OPCODE_UC_RDMA_WRITE_LAST] = &hfi1_uc_rcv,
  231. [IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE] = &hfi1_uc_rcv,
  232. [IB_OPCODE_UC_RDMA_WRITE_ONLY] = &hfi1_uc_rcv,
  233. [IB_OPCODE_UC_RDMA_WRITE_ONLY_WITH_IMMEDIATE] = &hfi1_uc_rcv,
  234. /* UD */
  235. [IB_OPCODE_UD_SEND_ONLY] = &hfi1_ud_rcv,
  236. [IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE] = &hfi1_ud_rcv,
  237. /* CNP */
  238. [IB_OPCODE_CNP] = &hfi1_cnp_rcv
  239. };
  240. #define OPMASK 0x1f
  241. static const u32 pio_opmask[BIT(3)] = {
  242. /* RC */
  243. [IB_OPCODE_RC >> 5] =
  244. BIT(RC_OP(SEND_ONLY) & OPMASK) |
  245. BIT(RC_OP(SEND_ONLY_WITH_IMMEDIATE) & OPMASK) |
  246. BIT(RC_OP(RDMA_WRITE_ONLY) & OPMASK) |
  247. BIT(RC_OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE) & OPMASK) |
  248. BIT(RC_OP(RDMA_READ_REQUEST) & OPMASK) |
  249. BIT(RC_OP(ACKNOWLEDGE) & OPMASK) |
  250. BIT(RC_OP(ATOMIC_ACKNOWLEDGE) & OPMASK) |
  251. BIT(RC_OP(COMPARE_SWAP) & OPMASK) |
  252. BIT(RC_OP(FETCH_ADD) & OPMASK),
  253. /* UC */
  254. [IB_OPCODE_UC >> 5] =
  255. BIT(UC_OP(SEND_ONLY) & OPMASK) |
  256. BIT(UC_OP(SEND_ONLY_WITH_IMMEDIATE) & OPMASK) |
  257. BIT(UC_OP(RDMA_WRITE_ONLY) & OPMASK) |
  258. BIT(UC_OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE) & OPMASK),
  259. };
  260. /*
  261. * System image GUID.
  262. */
  263. __be64 ib_hfi1_sys_image_guid;
  264. /*
  265. * Make sure the QP is ready and able to accept the given opcode.
  266. */
  267. static inline opcode_handler qp_ok(struct hfi1_packet *packet)
  268. {
  269. if (!(ib_rvt_state_ops[packet->qp->state] & RVT_PROCESS_RECV_OK))
  270. return NULL;
  271. if (((packet->opcode & RVT_OPCODE_QP_MASK) ==
  272. packet->qp->allowed_ops) ||
  273. (packet->opcode == IB_OPCODE_CNP))
  274. return opcode_handler_tbl[packet->opcode];
  275. return NULL;
  276. }
  277. static u64 hfi1_fault_tx(struct rvt_qp *qp, u8 opcode, u64 pbc)
  278. {
  279. #ifdef CONFIG_FAULT_INJECTION
  280. if ((opcode & IB_OPCODE_MSP) == IB_OPCODE_MSP)
  281. /*
  282. * In order to drop non-IB traffic we
  283. * set PbcInsertHrc to NONE (0x2).
  284. * The packet will still be delivered
  285. * to the receiving node but a
  286. * KHdrHCRCErr (KDETH packet with a bad
  287. * HCRC) will be triggered and the
  288. * packet will not be delivered to the
  289. * correct context.
  290. */
  291. pbc |= (u64)PBC_IHCRC_NONE << PBC_INSERT_HCRC_SHIFT;
  292. else
  293. /*
  294. * In order to drop regular verbs
  295. * traffic we set the PbcTestEbp
  296. * flag. The packet will still be
  297. * delivered to the receiving node but
  298. * a 'late ebp error' will be
  299. * triggered and will be dropped.
  300. */
  301. pbc |= PBC_TEST_EBP;
  302. #endif
  303. return pbc;
  304. }
  305. static int hfi1_do_pkey_check(struct hfi1_packet *packet)
  306. {
  307. struct hfi1_ctxtdata *rcd = packet->rcd;
  308. struct hfi1_pportdata *ppd = rcd->ppd;
  309. struct hfi1_16b_header *hdr = packet->hdr;
  310. u16 pkey;
  311. /* Pkey check needed only for bypass packets */
  312. if (packet->etype != RHF_RCV_TYPE_BYPASS)
  313. return 0;
  314. /* Perform pkey check */
  315. pkey = hfi1_16B_get_pkey(hdr);
  316. return ingress_pkey_check(ppd, pkey, packet->sc,
  317. packet->qp->s_pkey_index,
  318. packet->slid, true);
  319. }
  320. static inline void hfi1_handle_packet(struct hfi1_packet *packet,
  321. bool is_mcast)
  322. {
  323. u32 qp_num;
  324. struct hfi1_ctxtdata *rcd = packet->rcd;
  325. struct hfi1_pportdata *ppd = rcd->ppd;
  326. struct hfi1_ibport *ibp = rcd_to_iport(rcd);
  327. struct rvt_dev_info *rdi = &ppd->dd->verbs_dev.rdi;
  328. opcode_handler packet_handler;
  329. unsigned long flags;
  330. inc_opstats(packet->tlen, &rcd->opstats->stats[packet->opcode]);
  331. if (unlikely(is_mcast)) {
  332. struct rvt_mcast *mcast;
  333. struct rvt_mcast_qp *p;
  334. if (!packet->grh)
  335. goto drop;
  336. mcast = rvt_mcast_find(&ibp->rvp,
  337. &packet->grh->dgid,
  338. opa_get_lid(packet->dlid, 9B));
  339. if (!mcast)
  340. goto drop;
  341. list_for_each_entry_rcu(p, &mcast->qp_list, list) {
  342. packet->qp = p->qp;
  343. if (hfi1_do_pkey_check(packet))
  344. goto drop;
  345. spin_lock_irqsave(&packet->qp->r_lock, flags);
  346. packet_handler = qp_ok(packet);
  347. if (likely(packet_handler))
  348. packet_handler(packet);
  349. else
  350. ibp->rvp.n_pkt_drops++;
  351. spin_unlock_irqrestore(&packet->qp->r_lock, flags);
  352. }
  353. /*
  354. * Notify rvt_multicast_detach() if it is waiting for us
  355. * to finish.
  356. */
  357. if (atomic_dec_return(&mcast->refcount) <= 1)
  358. wake_up(&mcast->wait);
  359. } else {
  360. /* Get the destination QP number. */
  361. if (packet->etype == RHF_RCV_TYPE_BYPASS &&
  362. hfi1_16B_get_l4(packet->hdr) == OPA_16B_L4_FM)
  363. qp_num = hfi1_16B_get_dest_qpn(packet->mgmt);
  364. else
  365. qp_num = ib_bth_get_qpn(packet->ohdr);
  366. rcu_read_lock();
  367. packet->qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
  368. if (!packet->qp)
  369. goto unlock_drop;
  370. if (hfi1_do_pkey_check(packet))
  371. goto unlock_drop;
  372. spin_lock_irqsave(&packet->qp->r_lock, flags);
  373. packet_handler = qp_ok(packet);
  374. if (likely(packet_handler))
  375. packet_handler(packet);
  376. else
  377. ibp->rvp.n_pkt_drops++;
  378. spin_unlock_irqrestore(&packet->qp->r_lock, flags);
  379. rcu_read_unlock();
  380. }
  381. return;
  382. unlock_drop:
  383. rcu_read_unlock();
  384. drop:
  385. ibp->rvp.n_pkt_drops++;
  386. }
  387. /**
  388. * hfi1_ib_rcv - process an incoming packet
  389. * @packet: data packet information
  390. *
  391. * This is called to process an incoming packet at interrupt level.
  392. */
  393. void hfi1_ib_rcv(struct hfi1_packet *packet)
  394. {
  395. struct hfi1_ctxtdata *rcd = packet->rcd;
  396. trace_input_ibhdr(rcd->dd, packet, !!(rhf_dc_info(packet->rhf)));
  397. hfi1_handle_packet(packet, hfi1_check_mcast(packet->dlid));
  398. }
  399. void hfi1_16B_rcv(struct hfi1_packet *packet)
  400. {
  401. struct hfi1_ctxtdata *rcd = packet->rcd;
  402. trace_input_ibhdr(rcd->dd, packet, false);
  403. hfi1_handle_packet(packet, hfi1_check_mcast(packet->dlid));
  404. }
  405. /*
  406. * This is called from a timer to check for QPs
  407. * which need kernel memory in order to send a packet.
  408. */
  409. static void mem_timer(struct timer_list *t)
  410. {
  411. struct hfi1_ibdev *dev = from_timer(dev, t, mem_timer);
  412. struct list_head *list = &dev->memwait;
  413. struct rvt_qp *qp = NULL;
  414. struct iowait *wait;
  415. unsigned long flags;
  416. struct hfi1_qp_priv *priv;
  417. write_seqlock_irqsave(&dev->iowait_lock, flags);
  418. if (!list_empty(list)) {
  419. wait = list_first_entry(list, struct iowait, list);
  420. qp = iowait_to_qp(wait);
  421. priv = qp->priv;
  422. list_del_init(&priv->s_iowait.list);
  423. priv->s_iowait.lock = NULL;
  424. /* refcount held until actual wake up */
  425. if (!list_empty(list))
  426. mod_timer(&dev->mem_timer, jiffies + 1);
  427. }
  428. write_sequnlock_irqrestore(&dev->iowait_lock, flags);
  429. if (qp)
  430. hfi1_qp_wakeup(qp, RVT_S_WAIT_KMEM);
  431. }
  432. /*
  433. * This is called with progress side lock held.
  434. */
  435. /* New API */
  436. static void verbs_sdma_complete(
  437. struct sdma_txreq *cookie,
  438. int status)
  439. {
  440. struct verbs_txreq *tx =
  441. container_of(cookie, struct verbs_txreq, txreq);
  442. struct rvt_qp *qp = tx->qp;
  443. spin_lock(&qp->s_lock);
  444. if (tx->wqe) {
  445. rvt_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
  446. } else if (qp->ibqp.qp_type == IB_QPT_RC) {
  447. struct hfi1_opa_header *hdr;
  448. hdr = &tx->phdr.hdr;
  449. hfi1_rc_send_complete(qp, hdr);
  450. }
  451. spin_unlock(&qp->s_lock);
  452. hfi1_put_txreq(tx);
  453. }
  454. static int wait_kmem(struct hfi1_ibdev *dev,
  455. struct rvt_qp *qp,
  456. struct hfi1_pkt_state *ps)
  457. {
  458. struct hfi1_qp_priv *priv = qp->priv;
  459. unsigned long flags;
  460. int ret = 0;
  461. spin_lock_irqsave(&qp->s_lock, flags);
  462. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  463. write_seqlock(&dev->iowait_lock);
  464. list_add_tail(&ps->s_txreq->txreq.list,
  465. &ps->wait->tx_head);
  466. if (list_empty(&priv->s_iowait.list)) {
  467. if (list_empty(&dev->memwait))
  468. mod_timer(&dev->mem_timer, jiffies + 1);
  469. qp->s_flags |= RVT_S_WAIT_KMEM;
  470. list_add_tail(&priv->s_iowait.list, &dev->memwait);
  471. priv->s_iowait.lock = &dev->iowait_lock;
  472. trace_hfi1_qpsleep(qp, RVT_S_WAIT_KMEM);
  473. rvt_get_qp(qp);
  474. }
  475. write_sequnlock(&dev->iowait_lock);
  476. hfi1_qp_unbusy(qp, ps->wait);
  477. ret = -EBUSY;
  478. }
  479. spin_unlock_irqrestore(&qp->s_lock, flags);
  480. return ret;
  481. }
  482. /*
  483. * This routine calls txadds for each sg entry.
  484. *
  485. * Add failures will revert the sge cursor
  486. */
  487. static noinline int build_verbs_ulp_payload(
  488. struct sdma_engine *sde,
  489. u32 length,
  490. struct verbs_txreq *tx)
  491. {
  492. struct rvt_sge_state *ss = tx->ss;
  493. struct rvt_sge *sg_list = ss->sg_list;
  494. struct rvt_sge sge = ss->sge;
  495. u8 num_sge = ss->num_sge;
  496. u32 len;
  497. int ret = 0;
  498. while (length) {
  499. len = ss->sge.length;
  500. if (len > length)
  501. len = length;
  502. if (len > ss->sge.sge_length)
  503. len = ss->sge.sge_length;
  504. WARN_ON_ONCE(len == 0);
  505. ret = sdma_txadd_kvaddr(
  506. sde->dd,
  507. &tx->txreq,
  508. ss->sge.vaddr,
  509. len);
  510. if (ret)
  511. goto bail_txadd;
  512. rvt_update_sge(ss, len, false);
  513. length -= len;
  514. }
  515. return ret;
  516. bail_txadd:
  517. /* unwind cursor */
  518. ss->sge = sge;
  519. ss->num_sge = num_sge;
  520. ss->sg_list = sg_list;
  521. return ret;
  522. }
  523. /**
  524. * update_tx_opstats - record stats by opcode
  525. * @qp; the qp
  526. * @ps: transmit packet state
  527. * @plen: the plen in dwords
  528. *
  529. * This is a routine to record the tx opstats after a
  530. * packet has been presented to the egress mechanism.
  531. */
  532. static void update_tx_opstats(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
  533. u32 plen)
  534. {
  535. #ifdef CONFIG_DEBUG_FS
  536. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  537. struct hfi1_opcode_stats_perctx *s = get_cpu_ptr(dd->tx_opstats);
  538. inc_opstats(plen * 4, &s->stats[ps->opcode]);
  539. put_cpu_ptr(s);
  540. #endif
  541. }
  542. /*
  543. * Build the number of DMA descriptors needed to send length bytes of data.
  544. *
  545. * NOTE: DMA mapping is held in the tx until completed in the ring or
  546. * the tx desc is freed without having been submitted to the ring
  547. *
  548. * This routine ensures all the helper routine calls succeed.
  549. */
  550. /* New API */
  551. static int build_verbs_tx_desc(
  552. struct sdma_engine *sde,
  553. u32 length,
  554. struct verbs_txreq *tx,
  555. struct hfi1_ahg_info *ahg_info,
  556. u64 pbc)
  557. {
  558. int ret = 0;
  559. struct hfi1_sdma_header *phdr = &tx->phdr;
  560. u16 hdrbytes = (tx->hdr_dwords + sizeof(pbc) / 4) << 2;
  561. u8 extra_bytes = 0;
  562. if (tx->phdr.hdr.hdr_type) {
  563. /*
  564. * hdrbytes accounts for PBC. Need to subtract 8 bytes
  565. * before calculating padding.
  566. */
  567. extra_bytes = hfi1_get_16b_padding(hdrbytes - 8, length) +
  568. (SIZE_OF_CRC << 2) + SIZE_OF_LT;
  569. }
  570. if (!ahg_info->ahgcount) {
  571. ret = sdma_txinit_ahg(
  572. &tx->txreq,
  573. ahg_info->tx_flags,
  574. hdrbytes + length +
  575. extra_bytes,
  576. ahg_info->ahgidx,
  577. 0,
  578. NULL,
  579. 0,
  580. verbs_sdma_complete);
  581. if (ret)
  582. goto bail_txadd;
  583. phdr->pbc = cpu_to_le64(pbc);
  584. ret = sdma_txadd_kvaddr(
  585. sde->dd,
  586. &tx->txreq,
  587. phdr,
  588. hdrbytes);
  589. if (ret)
  590. goto bail_txadd;
  591. } else {
  592. ret = sdma_txinit_ahg(
  593. &tx->txreq,
  594. ahg_info->tx_flags,
  595. length,
  596. ahg_info->ahgidx,
  597. ahg_info->ahgcount,
  598. ahg_info->ahgdesc,
  599. hdrbytes,
  600. verbs_sdma_complete);
  601. if (ret)
  602. goto bail_txadd;
  603. }
  604. /* add the ulp payload - if any. tx->ss can be NULL for acks */
  605. if (tx->ss) {
  606. ret = build_verbs_ulp_payload(sde, length, tx);
  607. if (ret)
  608. goto bail_txadd;
  609. }
  610. /* add icrc, lt byte, and padding to flit */
  611. if (extra_bytes)
  612. ret = sdma_txadd_kvaddr(sde->dd, &tx->txreq,
  613. (void *)trail_buf, extra_bytes);
  614. bail_txadd:
  615. return ret;
  616. }
  617. int hfi1_verbs_send_dma(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
  618. u64 pbc)
  619. {
  620. struct hfi1_qp_priv *priv = qp->priv;
  621. struct hfi1_ahg_info *ahg_info = priv->s_ahg;
  622. u32 hdrwords = ps->s_txreq->hdr_dwords;
  623. u32 len = ps->s_txreq->s_cur_size;
  624. u32 plen;
  625. struct hfi1_ibdev *dev = ps->dev;
  626. struct hfi1_pportdata *ppd = ps->ppd;
  627. struct verbs_txreq *tx;
  628. u8 sc5 = priv->s_sc;
  629. int ret;
  630. u32 dwords;
  631. if (ps->s_txreq->phdr.hdr.hdr_type) {
  632. u8 extra_bytes = hfi1_get_16b_padding((hdrwords << 2), len);
  633. dwords = (len + extra_bytes + (SIZE_OF_CRC << 2) +
  634. SIZE_OF_LT) >> 2;
  635. } else {
  636. dwords = (len + 3) >> 2;
  637. }
  638. plen = hdrwords + dwords + sizeof(pbc) / 4;
  639. tx = ps->s_txreq;
  640. if (!sdma_txreq_built(&tx->txreq)) {
  641. if (likely(pbc == 0)) {
  642. u32 vl = sc_to_vlt(dd_from_ibdev(qp->ibqp.device), sc5);
  643. /* No vl15 here */
  644. /* set PBC_DC_INFO bit (aka SC[4]) in pbc */
  645. if (ps->s_txreq->phdr.hdr.hdr_type)
  646. pbc |= PBC_PACKET_BYPASS |
  647. PBC_INSERT_BYPASS_ICRC;
  648. else
  649. pbc |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
  650. if (unlikely(hfi1_dbg_should_fault_tx(qp, ps->opcode)))
  651. pbc = hfi1_fault_tx(qp, ps->opcode, pbc);
  652. pbc = create_pbc(ppd,
  653. pbc,
  654. qp->srate_mbps,
  655. vl,
  656. plen);
  657. }
  658. tx->wqe = qp->s_wqe;
  659. ret = build_verbs_tx_desc(tx->sde, len, tx, ahg_info, pbc);
  660. if (unlikely(ret))
  661. goto bail_build;
  662. }
  663. ret = sdma_send_txreq(tx->sde, ps->wait, &tx->txreq, ps->pkts_sent);
  664. if (unlikely(ret < 0)) {
  665. if (ret == -ECOMM)
  666. goto bail_ecomm;
  667. return ret;
  668. }
  669. update_tx_opstats(qp, ps, plen);
  670. trace_sdma_output_ibhdr(dd_from_ibdev(qp->ibqp.device),
  671. &ps->s_txreq->phdr.hdr, ib_is_sc5(sc5));
  672. return ret;
  673. bail_ecomm:
  674. /* The current one got "sent" */
  675. return 0;
  676. bail_build:
  677. ret = wait_kmem(dev, qp, ps);
  678. if (!ret) {
  679. /* free txreq - bad state */
  680. hfi1_put_txreq(ps->s_txreq);
  681. ps->s_txreq = NULL;
  682. }
  683. return ret;
  684. }
  685. /*
  686. * If we are now in the error state, return zero to flush the
  687. * send work request.
  688. */
  689. static int pio_wait(struct rvt_qp *qp,
  690. struct send_context *sc,
  691. struct hfi1_pkt_state *ps,
  692. u32 flag)
  693. {
  694. struct hfi1_qp_priv *priv = qp->priv;
  695. struct hfi1_devdata *dd = sc->dd;
  696. struct hfi1_ibdev *dev = &dd->verbs_dev;
  697. unsigned long flags;
  698. int ret = 0;
  699. /*
  700. * Note that as soon as want_buffer() is called and
  701. * possibly before it returns, sc_piobufavail()
  702. * could be called. Therefore, put QP on the I/O wait list before
  703. * enabling the PIO avail interrupt.
  704. */
  705. spin_lock_irqsave(&qp->s_lock, flags);
  706. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  707. write_seqlock(&dev->iowait_lock);
  708. list_add_tail(&ps->s_txreq->txreq.list,
  709. &ps->wait->tx_head);
  710. if (list_empty(&priv->s_iowait.list)) {
  711. struct hfi1_ibdev *dev = &dd->verbs_dev;
  712. int was_empty;
  713. dev->n_piowait += !!(flag & RVT_S_WAIT_PIO);
  714. dev->n_piodrain += !!(flag & HFI1_S_WAIT_PIO_DRAIN);
  715. qp->s_flags |= flag;
  716. was_empty = list_empty(&sc->piowait);
  717. iowait_queue(ps->pkts_sent, &priv->s_iowait,
  718. &sc->piowait);
  719. priv->s_iowait.lock = &dev->iowait_lock;
  720. trace_hfi1_qpsleep(qp, RVT_S_WAIT_PIO);
  721. rvt_get_qp(qp);
  722. /* counting: only call wantpiobuf_intr if first user */
  723. if (was_empty)
  724. hfi1_sc_wantpiobuf_intr(sc, 1);
  725. }
  726. write_sequnlock(&dev->iowait_lock);
  727. hfi1_qp_unbusy(qp, ps->wait);
  728. ret = -EBUSY;
  729. }
  730. spin_unlock_irqrestore(&qp->s_lock, flags);
  731. return ret;
  732. }
  733. static void verbs_pio_complete(void *arg, int code)
  734. {
  735. struct rvt_qp *qp = (struct rvt_qp *)arg;
  736. struct hfi1_qp_priv *priv = qp->priv;
  737. if (iowait_pio_dec(&priv->s_iowait))
  738. iowait_drain_wakeup(&priv->s_iowait);
  739. }
  740. int hfi1_verbs_send_pio(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
  741. u64 pbc)
  742. {
  743. struct hfi1_qp_priv *priv = qp->priv;
  744. u32 hdrwords = ps->s_txreq->hdr_dwords;
  745. struct rvt_sge_state *ss = ps->s_txreq->ss;
  746. u32 len = ps->s_txreq->s_cur_size;
  747. u32 dwords;
  748. u32 plen;
  749. struct hfi1_pportdata *ppd = ps->ppd;
  750. u32 *hdr;
  751. u8 sc5;
  752. unsigned long flags = 0;
  753. struct send_context *sc;
  754. struct pio_buf *pbuf;
  755. int wc_status = IB_WC_SUCCESS;
  756. int ret = 0;
  757. pio_release_cb cb = NULL;
  758. u8 extra_bytes = 0;
  759. if (ps->s_txreq->phdr.hdr.hdr_type) {
  760. u8 pad_size = hfi1_get_16b_padding((hdrwords << 2), len);
  761. extra_bytes = pad_size + (SIZE_OF_CRC << 2) + SIZE_OF_LT;
  762. dwords = (len + extra_bytes) >> 2;
  763. hdr = (u32 *)&ps->s_txreq->phdr.hdr.opah;
  764. } else {
  765. dwords = (len + 3) >> 2;
  766. hdr = (u32 *)&ps->s_txreq->phdr.hdr.ibh;
  767. }
  768. plen = hdrwords + dwords + sizeof(pbc) / 4;
  769. /* only RC/UC use complete */
  770. switch (qp->ibqp.qp_type) {
  771. case IB_QPT_RC:
  772. case IB_QPT_UC:
  773. cb = verbs_pio_complete;
  774. break;
  775. default:
  776. break;
  777. }
  778. /* vl15 special case taken care of in ud.c */
  779. sc5 = priv->s_sc;
  780. sc = ps->s_txreq->psc;
  781. if (likely(pbc == 0)) {
  782. u8 vl = sc_to_vlt(dd_from_ibdev(qp->ibqp.device), sc5);
  783. /* set PBC_DC_INFO bit (aka SC[4]) in pbc */
  784. if (ps->s_txreq->phdr.hdr.hdr_type)
  785. pbc |= PBC_PACKET_BYPASS | PBC_INSERT_BYPASS_ICRC;
  786. else
  787. pbc |= (ib_is_sc5(sc5) << PBC_DC_INFO_SHIFT);
  788. if (unlikely(hfi1_dbg_should_fault_tx(qp, ps->opcode)))
  789. pbc = hfi1_fault_tx(qp, ps->opcode, pbc);
  790. pbc = create_pbc(ppd, pbc, qp->srate_mbps, vl, plen);
  791. }
  792. if (cb)
  793. iowait_pio_inc(&priv->s_iowait);
  794. pbuf = sc_buffer_alloc(sc, plen, cb, qp);
  795. if (unlikely(!pbuf)) {
  796. if (cb)
  797. verbs_pio_complete(qp, 0);
  798. if (ppd->host_link_state != HLS_UP_ACTIVE) {
  799. /*
  800. * If we have filled the PIO buffers to capacity and are
  801. * not in an active state this request is not going to
  802. * go out to so just complete it with an error or else a
  803. * ULP or the core may be stuck waiting.
  804. */
  805. hfi1_cdbg(
  806. PIO,
  807. "alloc failed. state not active, completing");
  808. wc_status = IB_WC_GENERAL_ERR;
  809. goto pio_bail;
  810. } else {
  811. /*
  812. * This is a normal occurrence. The PIO buffs are full
  813. * up but we are still happily sending, well we could be
  814. * so lets continue to queue the request.
  815. */
  816. hfi1_cdbg(PIO, "alloc failed. state active, queuing");
  817. ret = pio_wait(qp, sc, ps, RVT_S_WAIT_PIO);
  818. if (!ret)
  819. /* txreq not queued - free */
  820. goto bail;
  821. /* tx consumed in wait */
  822. return ret;
  823. }
  824. }
  825. if (dwords == 0) {
  826. pio_copy(ppd->dd, pbuf, pbc, hdr, hdrwords);
  827. } else {
  828. seg_pio_copy_start(pbuf, pbc,
  829. hdr, hdrwords * 4);
  830. if (ss) {
  831. while (len) {
  832. void *addr = ss->sge.vaddr;
  833. u32 slen = ss->sge.length;
  834. if (slen > len)
  835. slen = len;
  836. rvt_update_sge(ss, slen, false);
  837. seg_pio_copy_mid(pbuf, addr, slen);
  838. len -= slen;
  839. }
  840. }
  841. /* add icrc, lt byte, and padding to flit */
  842. if (extra_bytes)
  843. seg_pio_copy_mid(pbuf, trail_buf, extra_bytes);
  844. seg_pio_copy_end(pbuf);
  845. }
  846. update_tx_opstats(qp, ps, plen);
  847. trace_pio_output_ibhdr(dd_from_ibdev(qp->ibqp.device),
  848. &ps->s_txreq->phdr.hdr, ib_is_sc5(sc5));
  849. pio_bail:
  850. if (qp->s_wqe) {
  851. spin_lock_irqsave(&qp->s_lock, flags);
  852. rvt_send_complete(qp, qp->s_wqe, wc_status);
  853. spin_unlock_irqrestore(&qp->s_lock, flags);
  854. } else if (qp->ibqp.qp_type == IB_QPT_RC) {
  855. spin_lock_irqsave(&qp->s_lock, flags);
  856. hfi1_rc_send_complete(qp, &ps->s_txreq->phdr.hdr);
  857. spin_unlock_irqrestore(&qp->s_lock, flags);
  858. }
  859. ret = 0;
  860. bail:
  861. hfi1_put_txreq(ps->s_txreq);
  862. return ret;
  863. }
  864. /*
  865. * egress_pkey_matches_entry - return 1 if the pkey matches ent (ent
  866. * being an entry from the partition key table), return 0
  867. * otherwise. Use the matching criteria for egress partition keys
  868. * specified in the OPAv1 spec., section 9.1l.7.
  869. */
  870. static inline int egress_pkey_matches_entry(u16 pkey, u16 ent)
  871. {
  872. u16 mkey = pkey & PKEY_LOW_15_MASK;
  873. u16 mentry = ent & PKEY_LOW_15_MASK;
  874. if (mkey == mentry) {
  875. /*
  876. * If pkey[15] is set (full partition member),
  877. * is bit 15 in the corresponding table element
  878. * clear (limited member)?
  879. */
  880. if (pkey & PKEY_MEMBER_MASK)
  881. return !!(ent & PKEY_MEMBER_MASK);
  882. return 1;
  883. }
  884. return 0;
  885. }
  886. /**
  887. * egress_pkey_check - check P_KEY of a packet
  888. * @ppd: Physical IB port data
  889. * @slid: SLID for packet
  890. * @bkey: PKEY for header
  891. * @sc5: SC for packet
  892. * @s_pkey_index: It will be used for look up optimization for kernel contexts
  893. * only. If it is negative value, then it means user contexts is calling this
  894. * function.
  895. *
  896. * It checks if hdr's pkey is valid.
  897. *
  898. * Return: 0 on success, otherwise, 1
  899. */
  900. int egress_pkey_check(struct hfi1_pportdata *ppd, u32 slid, u16 pkey,
  901. u8 sc5, int8_t s_pkey_index)
  902. {
  903. struct hfi1_devdata *dd;
  904. int i;
  905. int is_user_ctxt_mechanism = (s_pkey_index < 0);
  906. if (!(ppd->part_enforce & HFI1_PART_ENFORCE_OUT))
  907. return 0;
  908. /* If SC15, pkey[0:14] must be 0x7fff */
  909. if ((sc5 == 0xf) && ((pkey & PKEY_LOW_15_MASK) != PKEY_LOW_15_MASK))
  910. goto bad;
  911. /* Is the pkey = 0x0, or 0x8000? */
  912. if ((pkey & PKEY_LOW_15_MASK) == 0)
  913. goto bad;
  914. /*
  915. * For the kernel contexts only, if a qp is passed into the function,
  916. * the most likely matching pkey has index qp->s_pkey_index
  917. */
  918. if (!is_user_ctxt_mechanism &&
  919. egress_pkey_matches_entry(pkey, ppd->pkeys[s_pkey_index])) {
  920. return 0;
  921. }
  922. for (i = 0; i < MAX_PKEY_VALUES; i++) {
  923. if (egress_pkey_matches_entry(pkey, ppd->pkeys[i]))
  924. return 0;
  925. }
  926. bad:
  927. /*
  928. * For the user-context mechanism, the P_KEY check would only happen
  929. * once per SDMA request, not once per packet. Therefore, there's no
  930. * need to increment the counter for the user-context mechanism.
  931. */
  932. if (!is_user_ctxt_mechanism) {
  933. incr_cntr64(&ppd->port_xmit_constraint_errors);
  934. dd = ppd->dd;
  935. if (!(dd->err_info_xmit_constraint.status &
  936. OPA_EI_STATUS_SMASK)) {
  937. dd->err_info_xmit_constraint.status |=
  938. OPA_EI_STATUS_SMASK;
  939. dd->err_info_xmit_constraint.slid = slid;
  940. dd->err_info_xmit_constraint.pkey = pkey;
  941. }
  942. }
  943. return 1;
  944. }
  945. /**
  946. * get_send_routine - choose an egress routine
  947. *
  948. * Choose an egress routine based on QP type
  949. * and size
  950. */
  951. static inline send_routine get_send_routine(struct rvt_qp *qp,
  952. struct hfi1_pkt_state *ps)
  953. {
  954. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  955. struct hfi1_qp_priv *priv = qp->priv;
  956. struct verbs_txreq *tx = ps->s_txreq;
  957. if (unlikely(!(dd->flags & HFI1_HAS_SEND_DMA)))
  958. return dd->process_pio_send;
  959. switch (qp->ibqp.qp_type) {
  960. case IB_QPT_SMI:
  961. return dd->process_pio_send;
  962. case IB_QPT_GSI:
  963. case IB_QPT_UD:
  964. break;
  965. case IB_QPT_UC:
  966. case IB_QPT_RC: {
  967. if (piothreshold &&
  968. tx->s_cur_size <= min(piothreshold, qp->pmtu) &&
  969. (BIT(ps->opcode & OPMASK) & pio_opmask[ps->opcode >> 5]) &&
  970. iowait_sdma_pending(&priv->s_iowait) == 0 &&
  971. !sdma_txreq_built(&tx->txreq))
  972. return dd->process_pio_send;
  973. break;
  974. }
  975. default:
  976. break;
  977. }
  978. return dd->process_dma_send;
  979. }
  980. /**
  981. * hfi1_verbs_send - send a packet
  982. * @qp: the QP to send on
  983. * @ps: the state of the packet to send
  984. *
  985. * Return zero if packet is sent or queued OK.
  986. * Return non-zero and clear qp->s_flags RVT_S_BUSY otherwise.
  987. */
  988. int hfi1_verbs_send(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
  989. {
  990. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  991. struct hfi1_qp_priv *priv = qp->priv;
  992. struct ib_other_headers *ohdr = NULL;
  993. send_routine sr;
  994. int ret;
  995. u16 pkey;
  996. u32 slid;
  997. u8 l4 = 0;
  998. /* locate the pkey within the headers */
  999. if (ps->s_txreq->phdr.hdr.hdr_type) {
  1000. struct hfi1_16b_header *hdr = &ps->s_txreq->phdr.hdr.opah;
  1001. l4 = hfi1_16B_get_l4(hdr);
  1002. if (l4 == OPA_16B_L4_IB_LOCAL)
  1003. ohdr = &hdr->u.oth;
  1004. else if (l4 == OPA_16B_L4_IB_GLOBAL)
  1005. ohdr = &hdr->u.l.oth;
  1006. slid = hfi1_16B_get_slid(hdr);
  1007. pkey = hfi1_16B_get_pkey(hdr);
  1008. } else {
  1009. struct ib_header *hdr = &ps->s_txreq->phdr.hdr.ibh;
  1010. u8 lnh = ib_get_lnh(hdr);
  1011. if (lnh == HFI1_LRH_GRH)
  1012. ohdr = &hdr->u.l.oth;
  1013. else
  1014. ohdr = &hdr->u.oth;
  1015. slid = ib_get_slid(hdr);
  1016. pkey = ib_bth_get_pkey(ohdr);
  1017. }
  1018. if (likely(l4 != OPA_16B_L4_FM))
  1019. ps->opcode = ib_bth_get_opcode(ohdr);
  1020. else
  1021. ps->opcode = IB_OPCODE_UD_SEND_ONLY;
  1022. sr = get_send_routine(qp, ps);
  1023. ret = egress_pkey_check(dd->pport, slid, pkey,
  1024. priv->s_sc, qp->s_pkey_index);
  1025. if (unlikely(ret)) {
  1026. /*
  1027. * The value we are returning here does not get propagated to
  1028. * the verbs caller. Thus we need to complete the request with
  1029. * error otherwise the caller could be sitting waiting on the
  1030. * completion event. Only do this for PIO. SDMA has its own
  1031. * mechanism for handling the errors. So for SDMA we can just
  1032. * return.
  1033. */
  1034. if (sr == dd->process_pio_send) {
  1035. unsigned long flags;
  1036. hfi1_cdbg(PIO, "%s() Failed. Completing with err",
  1037. __func__);
  1038. spin_lock_irqsave(&qp->s_lock, flags);
  1039. rvt_send_complete(qp, qp->s_wqe, IB_WC_GENERAL_ERR);
  1040. spin_unlock_irqrestore(&qp->s_lock, flags);
  1041. }
  1042. return -EINVAL;
  1043. }
  1044. if (sr == dd->process_dma_send && iowait_pio_pending(&priv->s_iowait))
  1045. return pio_wait(qp,
  1046. ps->s_txreq->psc,
  1047. ps,
  1048. HFI1_S_WAIT_PIO_DRAIN);
  1049. return sr(qp, ps, 0);
  1050. }
  1051. /**
  1052. * hfi1_fill_device_attr - Fill in rvt dev info device attributes.
  1053. * @dd: the device data structure
  1054. */
  1055. static void hfi1_fill_device_attr(struct hfi1_devdata *dd)
  1056. {
  1057. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  1058. u32 ver = dd->dc8051_ver;
  1059. memset(&rdi->dparms.props, 0, sizeof(rdi->dparms.props));
  1060. rdi->dparms.props.fw_ver = ((u64)(dc8051_ver_maj(ver)) << 32) |
  1061. ((u64)(dc8051_ver_min(ver)) << 16) |
  1062. (u64)dc8051_ver_patch(ver);
  1063. rdi->dparms.props.device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
  1064. IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
  1065. IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
  1066. IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE |
  1067. IB_DEVICE_MEM_MGT_EXTENSIONS |
  1068. IB_DEVICE_RDMA_NETDEV_OPA_VNIC;
  1069. rdi->dparms.props.page_size_cap = PAGE_SIZE;
  1070. rdi->dparms.props.vendor_id = dd->oui1 << 16 | dd->oui2 << 8 | dd->oui3;
  1071. rdi->dparms.props.vendor_part_id = dd->pcidev->device;
  1072. rdi->dparms.props.hw_ver = dd->minrev;
  1073. rdi->dparms.props.sys_image_guid = ib_hfi1_sys_image_guid;
  1074. rdi->dparms.props.max_mr_size = U64_MAX;
  1075. rdi->dparms.props.max_fast_reg_page_list_len = UINT_MAX;
  1076. rdi->dparms.props.max_qp = hfi1_max_qps;
  1077. rdi->dparms.props.max_qp_wr = hfi1_max_qp_wrs;
  1078. rdi->dparms.props.max_send_sge = hfi1_max_sges;
  1079. rdi->dparms.props.max_recv_sge = hfi1_max_sges;
  1080. rdi->dparms.props.max_sge_rd = hfi1_max_sges;
  1081. rdi->dparms.props.max_cq = hfi1_max_cqs;
  1082. rdi->dparms.props.max_ah = hfi1_max_ahs;
  1083. rdi->dparms.props.max_cqe = hfi1_max_cqes;
  1084. rdi->dparms.props.max_mr = rdi->lkey_table.max;
  1085. rdi->dparms.props.max_fmr = rdi->lkey_table.max;
  1086. rdi->dparms.props.max_map_per_fmr = 32767;
  1087. rdi->dparms.props.max_pd = hfi1_max_pds;
  1088. rdi->dparms.props.max_qp_rd_atom = HFI1_MAX_RDMA_ATOMIC;
  1089. rdi->dparms.props.max_qp_init_rd_atom = 255;
  1090. rdi->dparms.props.max_srq = hfi1_max_srqs;
  1091. rdi->dparms.props.max_srq_wr = hfi1_max_srq_wrs;
  1092. rdi->dparms.props.max_srq_sge = hfi1_max_srq_sges;
  1093. rdi->dparms.props.atomic_cap = IB_ATOMIC_GLOB;
  1094. rdi->dparms.props.max_pkeys = hfi1_get_npkeys(dd);
  1095. rdi->dparms.props.max_mcast_grp = hfi1_max_mcast_grps;
  1096. rdi->dparms.props.max_mcast_qp_attach = hfi1_max_mcast_qp_attached;
  1097. rdi->dparms.props.max_total_mcast_qp_attach =
  1098. rdi->dparms.props.max_mcast_qp_attach *
  1099. rdi->dparms.props.max_mcast_grp;
  1100. }
  1101. static inline u16 opa_speed_to_ib(u16 in)
  1102. {
  1103. u16 out = 0;
  1104. if (in & OPA_LINK_SPEED_25G)
  1105. out |= IB_SPEED_EDR;
  1106. if (in & OPA_LINK_SPEED_12_5G)
  1107. out |= IB_SPEED_FDR;
  1108. return out;
  1109. }
  1110. /*
  1111. * Convert a single OPA link width (no multiple flags) to an IB value.
  1112. * A zero OPA link width means link down, which means the IB width value
  1113. * is a don't care.
  1114. */
  1115. static inline u16 opa_width_to_ib(u16 in)
  1116. {
  1117. switch (in) {
  1118. case OPA_LINK_WIDTH_1X:
  1119. /* map 2x and 3x to 1x as they don't exist in IB */
  1120. case OPA_LINK_WIDTH_2X:
  1121. case OPA_LINK_WIDTH_3X:
  1122. return IB_WIDTH_1X;
  1123. default: /* link down or unknown, return our largest width */
  1124. case OPA_LINK_WIDTH_4X:
  1125. return IB_WIDTH_4X;
  1126. }
  1127. }
  1128. static int query_port(struct rvt_dev_info *rdi, u8 port_num,
  1129. struct ib_port_attr *props)
  1130. {
  1131. struct hfi1_ibdev *verbs_dev = dev_from_rdi(rdi);
  1132. struct hfi1_devdata *dd = dd_from_dev(verbs_dev);
  1133. struct hfi1_pportdata *ppd = &dd->pport[port_num - 1];
  1134. u32 lid = ppd->lid;
  1135. /* props being zeroed by the caller, avoid zeroing it here */
  1136. props->lid = lid ? lid : 0;
  1137. props->lmc = ppd->lmc;
  1138. /* OPA logical states match IB logical states */
  1139. props->state = driver_lstate(ppd);
  1140. props->phys_state = driver_pstate(ppd);
  1141. props->gid_tbl_len = HFI1_GUIDS_PER_PORT;
  1142. props->active_width = (u8)opa_width_to_ib(ppd->link_width_active);
  1143. /* see rate_show() in ib core/sysfs.c */
  1144. props->active_speed = (u8)opa_speed_to_ib(ppd->link_speed_active);
  1145. props->max_vl_num = ppd->vls_supported;
  1146. /* Once we are a "first class" citizen and have added the OPA MTUs to
  1147. * the core we can advertise the larger MTU enum to the ULPs, for now
  1148. * advertise only 4K.
  1149. *
  1150. * Those applications which are either OPA aware or pass the MTU enum
  1151. * from the Path Records to us will get the new 8k MTU. Those that
  1152. * attempt to process the MTU enum may fail in various ways.
  1153. */
  1154. props->max_mtu = mtu_to_enum((!valid_ib_mtu(hfi1_max_mtu) ?
  1155. 4096 : hfi1_max_mtu), IB_MTU_4096);
  1156. props->active_mtu = !valid_ib_mtu(ppd->ibmtu) ? props->max_mtu :
  1157. mtu_to_enum(ppd->ibmtu, IB_MTU_4096);
  1158. return 0;
  1159. }
  1160. static int modify_device(struct ib_device *device,
  1161. int device_modify_mask,
  1162. struct ib_device_modify *device_modify)
  1163. {
  1164. struct hfi1_devdata *dd = dd_from_ibdev(device);
  1165. unsigned i;
  1166. int ret;
  1167. if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
  1168. IB_DEVICE_MODIFY_NODE_DESC)) {
  1169. ret = -EOPNOTSUPP;
  1170. goto bail;
  1171. }
  1172. if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
  1173. memcpy(device->node_desc, device_modify->node_desc,
  1174. IB_DEVICE_NODE_DESC_MAX);
  1175. for (i = 0; i < dd->num_pports; i++) {
  1176. struct hfi1_ibport *ibp = &dd->pport[i].ibport_data;
  1177. hfi1_node_desc_chg(ibp);
  1178. }
  1179. }
  1180. if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
  1181. ib_hfi1_sys_image_guid =
  1182. cpu_to_be64(device_modify->sys_image_guid);
  1183. for (i = 0; i < dd->num_pports; i++) {
  1184. struct hfi1_ibport *ibp = &dd->pport[i].ibport_data;
  1185. hfi1_sys_guid_chg(ibp);
  1186. }
  1187. }
  1188. ret = 0;
  1189. bail:
  1190. return ret;
  1191. }
  1192. static int shut_down_port(struct rvt_dev_info *rdi, u8 port_num)
  1193. {
  1194. struct hfi1_ibdev *verbs_dev = dev_from_rdi(rdi);
  1195. struct hfi1_devdata *dd = dd_from_dev(verbs_dev);
  1196. struct hfi1_pportdata *ppd = &dd->pport[port_num - 1];
  1197. int ret;
  1198. set_link_down_reason(ppd, OPA_LINKDOWN_REASON_UNKNOWN, 0,
  1199. OPA_LINKDOWN_REASON_UNKNOWN);
  1200. ret = set_link_state(ppd, HLS_DN_DOWNDEF);
  1201. return ret;
  1202. }
  1203. static int hfi1_get_guid_be(struct rvt_dev_info *rdi, struct rvt_ibport *rvp,
  1204. int guid_index, __be64 *guid)
  1205. {
  1206. struct hfi1_ibport *ibp = container_of(rvp, struct hfi1_ibport, rvp);
  1207. if (guid_index >= HFI1_GUIDS_PER_PORT)
  1208. return -EINVAL;
  1209. *guid = get_sguid(ibp, guid_index);
  1210. return 0;
  1211. }
  1212. /*
  1213. * convert ah port,sl to sc
  1214. */
  1215. u8 ah_to_sc(struct ib_device *ibdev, struct rdma_ah_attr *ah)
  1216. {
  1217. struct hfi1_ibport *ibp = to_iport(ibdev, rdma_ah_get_port_num(ah));
  1218. return ibp->sl_to_sc[rdma_ah_get_sl(ah)];
  1219. }
  1220. static int hfi1_check_ah(struct ib_device *ibdev, struct rdma_ah_attr *ah_attr)
  1221. {
  1222. struct hfi1_ibport *ibp;
  1223. struct hfi1_pportdata *ppd;
  1224. struct hfi1_devdata *dd;
  1225. u8 sc5;
  1226. u8 sl;
  1227. if (hfi1_check_mcast(rdma_ah_get_dlid(ah_attr)) &&
  1228. !(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH))
  1229. return -EINVAL;
  1230. /* test the mapping for validity */
  1231. ibp = to_iport(ibdev, rdma_ah_get_port_num(ah_attr));
  1232. ppd = ppd_from_ibp(ibp);
  1233. dd = dd_from_ppd(ppd);
  1234. sl = rdma_ah_get_sl(ah_attr);
  1235. if (sl >= ARRAY_SIZE(ibp->sl_to_sc))
  1236. return -EINVAL;
  1237. sc5 = ibp->sl_to_sc[sl];
  1238. if (sc_to_vlt(dd, sc5) > num_vls && sc_to_vlt(dd, sc5) != 0xf)
  1239. return -EINVAL;
  1240. return 0;
  1241. }
  1242. static void hfi1_notify_new_ah(struct ib_device *ibdev,
  1243. struct rdma_ah_attr *ah_attr,
  1244. struct rvt_ah *ah)
  1245. {
  1246. struct hfi1_ibport *ibp;
  1247. struct hfi1_pportdata *ppd;
  1248. struct hfi1_devdata *dd;
  1249. u8 sc5;
  1250. struct rdma_ah_attr *attr = &ah->attr;
  1251. /*
  1252. * Do not trust reading anything from rvt_ah at this point as it is not
  1253. * done being setup. We can however modify things which we need to set.
  1254. */
  1255. ibp = to_iport(ibdev, rdma_ah_get_port_num(ah_attr));
  1256. ppd = ppd_from_ibp(ibp);
  1257. sc5 = ibp->sl_to_sc[rdma_ah_get_sl(&ah->attr)];
  1258. hfi1_update_ah_attr(ibdev, attr);
  1259. hfi1_make_opa_lid(attr);
  1260. dd = dd_from_ppd(ppd);
  1261. ah->vl = sc_to_vlt(dd, sc5);
  1262. if (ah->vl < num_vls || ah->vl == 15)
  1263. ah->log_pmtu = ilog2(dd->vld[ah->vl].mtu);
  1264. }
  1265. /**
  1266. * hfi1_get_npkeys - return the size of the PKEY table for context 0
  1267. * @dd: the hfi1_ib device
  1268. */
  1269. unsigned hfi1_get_npkeys(struct hfi1_devdata *dd)
  1270. {
  1271. return ARRAY_SIZE(dd->pport[0].pkeys);
  1272. }
  1273. static void init_ibport(struct hfi1_pportdata *ppd)
  1274. {
  1275. struct hfi1_ibport *ibp = &ppd->ibport_data;
  1276. size_t sz = ARRAY_SIZE(ibp->sl_to_sc);
  1277. int i;
  1278. for (i = 0; i < sz; i++) {
  1279. ibp->sl_to_sc[i] = i;
  1280. ibp->sc_to_sl[i] = i;
  1281. }
  1282. for (i = 0; i < RVT_MAX_TRAP_LISTS ; i++)
  1283. INIT_LIST_HEAD(&ibp->rvp.trap_lists[i].list);
  1284. timer_setup(&ibp->rvp.trap_timer, hfi1_handle_trap_timer, 0);
  1285. spin_lock_init(&ibp->rvp.lock);
  1286. /* Set the prefix to the default value (see ch. 4.1.1) */
  1287. ibp->rvp.gid_prefix = IB_DEFAULT_GID_PREFIX;
  1288. ibp->rvp.sm_lid = 0;
  1289. /*
  1290. * Below should only set bits defined in OPA PortInfo.CapabilityMask
  1291. * and PortInfo.CapabilityMask3
  1292. */
  1293. ibp->rvp.port_cap_flags = IB_PORT_AUTO_MIGR_SUP |
  1294. IB_PORT_CAP_MASK_NOTICE_SUP;
  1295. ibp->rvp.port_cap3_flags = OPA_CAP_MASK3_IsSharedSpaceSupported;
  1296. ibp->rvp.pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
  1297. ibp->rvp.pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
  1298. ibp->rvp.pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
  1299. ibp->rvp.pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
  1300. ibp->rvp.pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
  1301. RCU_INIT_POINTER(ibp->rvp.qp[0], NULL);
  1302. RCU_INIT_POINTER(ibp->rvp.qp[1], NULL);
  1303. }
  1304. static void hfi1_get_dev_fw_str(struct ib_device *ibdev, char *str)
  1305. {
  1306. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  1307. struct hfi1_ibdev *dev = dev_from_rdi(rdi);
  1308. u32 ver = dd_from_dev(dev)->dc8051_ver;
  1309. snprintf(str, IB_FW_VERSION_NAME_MAX, "%u.%u.%u", dc8051_ver_maj(ver),
  1310. dc8051_ver_min(ver), dc8051_ver_patch(ver));
  1311. }
  1312. static const char * const driver_cntr_names[] = {
  1313. /* must be element 0*/
  1314. "DRIVER_KernIntr",
  1315. "DRIVER_ErrorIntr",
  1316. "DRIVER_Tx_Errs",
  1317. "DRIVER_Rcv_Errs",
  1318. "DRIVER_HW_Errs",
  1319. "DRIVER_NoPIOBufs",
  1320. "DRIVER_CtxtsOpen",
  1321. "DRIVER_RcvLen_Errs",
  1322. "DRIVER_EgrBufFull",
  1323. "DRIVER_EgrHdrFull"
  1324. };
  1325. static DEFINE_MUTEX(cntr_names_lock); /* protects the *_cntr_names bufers */
  1326. static const char **dev_cntr_names;
  1327. static const char **port_cntr_names;
  1328. int num_driver_cntrs = ARRAY_SIZE(driver_cntr_names);
  1329. static int num_dev_cntrs;
  1330. static int num_port_cntrs;
  1331. static int cntr_names_initialized;
  1332. /*
  1333. * Convert a list of names separated by '\n' into an array of NULL terminated
  1334. * strings. Optionally some entries can be reserved in the array to hold extra
  1335. * external strings.
  1336. */
  1337. static int init_cntr_names(const char *names_in,
  1338. const size_t names_len,
  1339. int num_extra_names,
  1340. int *num_cntrs,
  1341. const char ***cntr_names)
  1342. {
  1343. char *names_out, *p, **q;
  1344. int i, n;
  1345. n = 0;
  1346. for (i = 0; i < names_len; i++)
  1347. if (names_in[i] == '\n')
  1348. n++;
  1349. names_out = kmalloc((n + num_extra_names) * sizeof(char *) + names_len,
  1350. GFP_KERNEL);
  1351. if (!names_out) {
  1352. *num_cntrs = 0;
  1353. *cntr_names = NULL;
  1354. return -ENOMEM;
  1355. }
  1356. p = names_out + (n + num_extra_names) * sizeof(char *);
  1357. memcpy(p, names_in, names_len);
  1358. q = (char **)names_out;
  1359. for (i = 0; i < n; i++) {
  1360. q[i] = p;
  1361. p = strchr(p, '\n');
  1362. *p++ = '\0';
  1363. }
  1364. *num_cntrs = n;
  1365. *cntr_names = (const char **)names_out;
  1366. return 0;
  1367. }
  1368. static struct rdma_hw_stats *alloc_hw_stats(struct ib_device *ibdev,
  1369. u8 port_num)
  1370. {
  1371. int i, err;
  1372. mutex_lock(&cntr_names_lock);
  1373. if (!cntr_names_initialized) {
  1374. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  1375. err = init_cntr_names(dd->cntrnames,
  1376. dd->cntrnameslen,
  1377. num_driver_cntrs,
  1378. &num_dev_cntrs,
  1379. &dev_cntr_names);
  1380. if (err) {
  1381. mutex_unlock(&cntr_names_lock);
  1382. return NULL;
  1383. }
  1384. for (i = 0; i < num_driver_cntrs; i++)
  1385. dev_cntr_names[num_dev_cntrs + i] =
  1386. driver_cntr_names[i];
  1387. err = init_cntr_names(dd->portcntrnames,
  1388. dd->portcntrnameslen,
  1389. 0,
  1390. &num_port_cntrs,
  1391. &port_cntr_names);
  1392. if (err) {
  1393. kfree(dev_cntr_names);
  1394. dev_cntr_names = NULL;
  1395. mutex_unlock(&cntr_names_lock);
  1396. return NULL;
  1397. }
  1398. cntr_names_initialized = 1;
  1399. }
  1400. mutex_unlock(&cntr_names_lock);
  1401. if (!port_num)
  1402. return rdma_alloc_hw_stats_struct(
  1403. dev_cntr_names,
  1404. num_dev_cntrs + num_driver_cntrs,
  1405. RDMA_HW_STATS_DEFAULT_LIFESPAN);
  1406. else
  1407. return rdma_alloc_hw_stats_struct(
  1408. port_cntr_names,
  1409. num_port_cntrs,
  1410. RDMA_HW_STATS_DEFAULT_LIFESPAN);
  1411. }
  1412. static u64 hfi1_sps_ints(void)
  1413. {
  1414. unsigned long flags;
  1415. struct hfi1_devdata *dd;
  1416. u64 sps_ints = 0;
  1417. spin_lock_irqsave(&hfi1_devs_lock, flags);
  1418. list_for_each_entry(dd, &hfi1_dev_list, list) {
  1419. sps_ints += get_all_cpu_total(dd->int_counter);
  1420. }
  1421. spin_unlock_irqrestore(&hfi1_devs_lock, flags);
  1422. return sps_ints;
  1423. }
  1424. static int get_hw_stats(struct ib_device *ibdev, struct rdma_hw_stats *stats,
  1425. u8 port, int index)
  1426. {
  1427. u64 *values;
  1428. int count;
  1429. if (!port) {
  1430. u64 *stats = (u64 *)&hfi1_stats;
  1431. int i;
  1432. hfi1_read_cntrs(dd_from_ibdev(ibdev), NULL, &values);
  1433. values[num_dev_cntrs] = hfi1_sps_ints();
  1434. for (i = 1; i < num_driver_cntrs; i++)
  1435. values[num_dev_cntrs + i] = stats[i];
  1436. count = num_dev_cntrs + num_driver_cntrs;
  1437. } else {
  1438. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  1439. hfi1_read_portcntrs(ppd_from_ibp(ibp), NULL, &values);
  1440. count = num_port_cntrs;
  1441. }
  1442. memcpy(stats->value, values, count * sizeof(u64));
  1443. return count;
  1444. }
  1445. /**
  1446. * hfi1_register_ib_device - register our device with the infiniband core
  1447. * @dd: the device data structure
  1448. * Return 0 if successful, errno if unsuccessful.
  1449. */
  1450. int hfi1_register_ib_device(struct hfi1_devdata *dd)
  1451. {
  1452. struct hfi1_ibdev *dev = &dd->verbs_dev;
  1453. struct ib_device *ibdev = &dev->rdi.ibdev;
  1454. struct hfi1_pportdata *ppd = dd->pport;
  1455. struct hfi1_ibport *ibp = &ppd->ibport_data;
  1456. unsigned i;
  1457. int ret;
  1458. for (i = 0; i < dd->num_pports; i++)
  1459. init_ibport(ppd + i);
  1460. /* Only need to initialize non-zero fields. */
  1461. timer_setup(&dev->mem_timer, mem_timer, 0);
  1462. seqlock_init(&dev->iowait_lock);
  1463. seqlock_init(&dev->txwait_lock);
  1464. INIT_LIST_HEAD(&dev->txwait);
  1465. INIT_LIST_HEAD(&dev->memwait);
  1466. ret = verbs_txreq_init(dev);
  1467. if (ret)
  1468. goto err_verbs_txreq;
  1469. /* Use first-port GUID as node guid */
  1470. ibdev->node_guid = get_sguid(ibp, HFI1_PORT_GUID_INDEX);
  1471. /*
  1472. * The system image GUID is supposed to be the same for all
  1473. * HFIs in a single system but since there can be other
  1474. * device types in the system, we can't be sure this is unique.
  1475. */
  1476. if (!ib_hfi1_sys_image_guid)
  1477. ib_hfi1_sys_image_guid = ibdev->node_guid;
  1478. ibdev->owner = THIS_MODULE;
  1479. ibdev->phys_port_cnt = dd->num_pports;
  1480. ibdev->dev.parent = &dd->pcidev->dev;
  1481. ibdev->modify_device = modify_device;
  1482. ibdev->alloc_hw_stats = alloc_hw_stats;
  1483. ibdev->get_hw_stats = get_hw_stats;
  1484. ibdev->alloc_rdma_netdev = hfi1_vnic_alloc_rn;
  1485. /* keep process mad in the driver */
  1486. ibdev->process_mad = hfi1_process_mad;
  1487. ibdev->get_dev_fw_str = hfi1_get_dev_fw_str;
  1488. strlcpy(ibdev->node_desc, init_utsname()->nodename,
  1489. sizeof(ibdev->node_desc));
  1490. /*
  1491. * Fill in rvt info object.
  1492. */
  1493. dd->verbs_dev.rdi.driver_f.port_callback = hfi1_create_port_files;
  1494. dd->verbs_dev.rdi.driver_f.get_pci_dev = get_pci_dev;
  1495. dd->verbs_dev.rdi.driver_f.check_ah = hfi1_check_ah;
  1496. dd->verbs_dev.rdi.driver_f.notify_new_ah = hfi1_notify_new_ah;
  1497. dd->verbs_dev.rdi.driver_f.get_guid_be = hfi1_get_guid_be;
  1498. dd->verbs_dev.rdi.driver_f.query_port_state = query_port;
  1499. dd->verbs_dev.rdi.driver_f.shut_down_port = shut_down_port;
  1500. dd->verbs_dev.rdi.driver_f.cap_mask_chg = hfi1_cap_mask_chg;
  1501. /*
  1502. * Fill in rvt info device attributes.
  1503. */
  1504. hfi1_fill_device_attr(dd);
  1505. /* queue pair */
  1506. dd->verbs_dev.rdi.dparms.qp_table_size = hfi1_qp_table_size;
  1507. dd->verbs_dev.rdi.dparms.qpn_start = 0;
  1508. dd->verbs_dev.rdi.dparms.qpn_inc = 1;
  1509. dd->verbs_dev.rdi.dparms.qos_shift = dd->qos_shift;
  1510. dd->verbs_dev.rdi.dparms.qpn_res_start = kdeth_qp << 16;
  1511. dd->verbs_dev.rdi.dparms.qpn_res_end =
  1512. dd->verbs_dev.rdi.dparms.qpn_res_start + 65535;
  1513. dd->verbs_dev.rdi.dparms.max_rdma_atomic = HFI1_MAX_RDMA_ATOMIC;
  1514. dd->verbs_dev.rdi.dparms.psn_mask = PSN_MASK;
  1515. dd->verbs_dev.rdi.dparms.psn_shift = PSN_SHIFT;
  1516. dd->verbs_dev.rdi.dparms.psn_modify_mask = PSN_MODIFY_MASK;
  1517. dd->verbs_dev.rdi.dparms.core_cap_flags = RDMA_CORE_PORT_INTEL_OPA |
  1518. RDMA_CORE_CAP_OPA_AH;
  1519. dd->verbs_dev.rdi.dparms.max_mad_size = OPA_MGMT_MAD_SIZE;
  1520. dd->verbs_dev.rdi.driver_f.qp_priv_alloc = qp_priv_alloc;
  1521. dd->verbs_dev.rdi.driver_f.qp_priv_free = qp_priv_free;
  1522. dd->verbs_dev.rdi.driver_f.free_all_qps = free_all_qps;
  1523. dd->verbs_dev.rdi.driver_f.notify_qp_reset = notify_qp_reset;
  1524. dd->verbs_dev.rdi.driver_f.do_send = hfi1_do_send_from_rvt;
  1525. dd->verbs_dev.rdi.driver_f.schedule_send = hfi1_schedule_send;
  1526. dd->verbs_dev.rdi.driver_f.schedule_send_no_lock = _hfi1_schedule_send;
  1527. dd->verbs_dev.rdi.driver_f.get_pmtu_from_attr = get_pmtu_from_attr;
  1528. dd->verbs_dev.rdi.driver_f.notify_error_qp = notify_error_qp;
  1529. dd->verbs_dev.rdi.driver_f.flush_qp_waiters = flush_qp_waiters;
  1530. dd->verbs_dev.rdi.driver_f.stop_send_queue = stop_send_queue;
  1531. dd->verbs_dev.rdi.driver_f.quiesce_qp = quiesce_qp;
  1532. dd->verbs_dev.rdi.driver_f.notify_error_qp = notify_error_qp;
  1533. dd->verbs_dev.rdi.driver_f.mtu_from_qp = mtu_from_qp;
  1534. dd->verbs_dev.rdi.driver_f.mtu_to_path_mtu = mtu_to_path_mtu;
  1535. dd->verbs_dev.rdi.driver_f.check_modify_qp = hfi1_check_modify_qp;
  1536. dd->verbs_dev.rdi.driver_f.modify_qp = hfi1_modify_qp;
  1537. dd->verbs_dev.rdi.driver_f.notify_restart_rc = hfi1_restart_rc;
  1538. dd->verbs_dev.rdi.driver_f.setup_wqe = hfi1_setup_wqe;
  1539. dd->verbs_dev.rdi.driver_f.comp_vect_cpu_lookup =
  1540. hfi1_comp_vect_mappings_lookup;
  1541. /* completeion queue */
  1542. dd->verbs_dev.rdi.ibdev.num_comp_vectors = dd->comp_vect_possible_cpus;
  1543. dd->verbs_dev.rdi.dparms.node = dd->node;
  1544. /* misc settings */
  1545. dd->verbs_dev.rdi.flags = 0; /* Let rdmavt handle it all */
  1546. dd->verbs_dev.rdi.dparms.lkey_table_size = hfi1_lkey_table_size;
  1547. dd->verbs_dev.rdi.dparms.nports = dd->num_pports;
  1548. dd->verbs_dev.rdi.dparms.npkeys = hfi1_get_npkeys(dd);
  1549. dd->verbs_dev.rdi.dparms.sge_copy_mode = sge_copy_mode;
  1550. dd->verbs_dev.rdi.dparms.wss_threshold = wss_threshold;
  1551. dd->verbs_dev.rdi.dparms.wss_clean_period = wss_clean_period;
  1552. /* post send table */
  1553. dd->verbs_dev.rdi.post_parms = hfi1_post_parms;
  1554. /* opcode translation table */
  1555. dd->verbs_dev.rdi.wc_opcode = ib_hfi1_wc_opcode;
  1556. ppd = dd->pport;
  1557. for (i = 0; i < dd->num_pports; i++, ppd++)
  1558. rvt_init_port(&dd->verbs_dev.rdi,
  1559. &ppd->ibport_data.rvp,
  1560. i,
  1561. ppd->pkeys);
  1562. rdma_set_device_sysfs_group(&dd->verbs_dev.rdi.ibdev,
  1563. &ib_hfi1_attr_group);
  1564. ret = rvt_register_device(&dd->verbs_dev.rdi, RDMA_DRIVER_HFI1);
  1565. if (ret)
  1566. goto err_verbs_txreq;
  1567. ret = hfi1_verbs_register_sysfs(dd);
  1568. if (ret)
  1569. goto err_class;
  1570. return ret;
  1571. err_class:
  1572. rvt_unregister_device(&dd->verbs_dev.rdi);
  1573. err_verbs_txreq:
  1574. verbs_txreq_exit(dev);
  1575. dd_dev_err(dd, "cannot register verbs: %d!\n", -ret);
  1576. return ret;
  1577. }
  1578. void hfi1_unregister_ib_device(struct hfi1_devdata *dd)
  1579. {
  1580. struct hfi1_ibdev *dev = &dd->verbs_dev;
  1581. hfi1_verbs_unregister_sysfs(dd);
  1582. rvt_unregister_device(&dd->verbs_dev.rdi);
  1583. if (!list_empty(&dev->txwait))
  1584. dd_dev_err(dd, "txwait list not empty!\n");
  1585. if (!list_empty(&dev->memwait))
  1586. dd_dev_err(dd, "memwait list not empty!\n");
  1587. del_timer_sync(&dev->mem_timer);
  1588. verbs_txreq_exit(dev);
  1589. mutex_lock(&cntr_names_lock);
  1590. kfree(dev_cntr_names);
  1591. kfree(port_cntr_names);
  1592. dev_cntr_names = NULL;
  1593. port_cntr_names = NULL;
  1594. cntr_names_initialized = 0;
  1595. mutex_unlock(&cntr_names_lock);
  1596. }
  1597. void hfi1_cnp_rcv(struct hfi1_packet *packet)
  1598. {
  1599. struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
  1600. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  1601. struct ib_header *hdr = packet->hdr;
  1602. struct rvt_qp *qp = packet->qp;
  1603. u32 lqpn, rqpn = 0;
  1604. u16 rlid = 0;
  1605. u8 sl, sc5, svc_type;
  1606. switch (packet->qp->ibqp.qp_type) {
  1607. case IB_QPT_UC:
  1608. rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
  1609. rqpn = qp->remote_qpn;
  1610. svc_type = IB_CC_SVCTYPE_UC;
  1611. break;
  1612. case IB_QPT_RC:
  1613. rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
  1614. rqpn = qp->remote_qpn;
  1615. svc_type = IB_CC_SVCTYPE_RC;
  1616. break;
  1617. case IB_QPT_SMI:
  1618. case IB_QPT_GSI:
  1619. case IB_QPT_UD:
  1620. svc_type = IB_CC_SVCTYPE_UD;
  1621. break;
  1622. default:
  1623. ibp->rvp.n_pkt_drops++;
  1624. return;
  1625. }
  1626. sc5 = hfi1_9B_get_sc5(hdr, packet->rhf);
  1627. sl = ibp->sc_to_sl[sc5];
  1628. lqpn = qp->ibqp.qp_num;
  1629. process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
  1630. }