verbs.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. /*
  2. * Copyright(c) 2015, 2016 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 "hfi.h"
  56. #include "common.h"
  57. #include "device.h"
  58. #include "trace.h"
  59. #include "qp.h"
  60. #include "verbs_txreq.h"
  61. static unsigned int hfi1_lkey_table_size = 16;
  62. module_param_named(lkey_table_size, hfi1_lkey_table_size, uint,
  63. S_IRUGO);
  64. MODULE_PARM_DESC(lkey_table_size,
  65. "LKEY table size in bits (2^n, 1 <= n <= 23)");
  66. static unsigned int hfi1_max_pds = 0xFFFF;
  67. module_param_named(max_pds, hfi1_max_pds, uint, S_IRUGO);
  68. MODULE_PARM_DESC(max_pds,
  69. "Maximum number of protection domains to support");
  70. static unsigned int hfi1_max_ahs = 0xFFFF;
  71. module_param_named(max_ahs, hfi1_max_ahs, uint, S_IRUGO);
  72. MODULE_PARM_DESC(max_ahs, "Maximum number of address handles to support");
  73. unsigned int hfi1_max_cqes = 0x2FFFFF;
  74. module_param_named(max_cqes, hfi1_max_cqes, uint, S_IRUGO);
  75. MODULE_PARM_DESC(max_cqes,
  76. "Maximum number of completion queue entries to support");
  77. unsigned int hfi1_max_cqs = 0x1FFFF;
  78. module_param_named(max_cqs, hfi1_max_cqs, uint, S_IRUGO);
  79. MODULE_PARM_DESC(max_cqs, "Maximum number of completion queues to support");
  80. unsigned int hfi1_max_qp_wrs = 0x3FFF;
  81. module_param_named(max_qp_wrs, hfi1_max_qp_wrs, uint, S_IRUGO);
  82. MODULE_PARM_DESC(max_qp_wrs, "Maximum number of QP WRs to support");
  83. unsigned int hfi1_max_qps = 32768;
  84. module_param_named(max_qps, hfi1_max_qps, uint, S_IRUGO);
  85. MODULE_PARM_DESC(max_qps, "Maximum number of QPs to support");
  86. unsigned int hfi1_max_sges = 0x60;
  87. module_param_named(max_sges, hfi1_max_sges, uint, S_IRUGO);
  88. MODULE_PARM_DESC(max_sges, "Maximum number of SGEs to support");
  89. unsigned int hfi1_max_mcast_grps = 16384;
  90. module_param_named(max_mcast_grps, hfi1_max_mcast_grps, uint, S_IRUGO);
  91. MODULE_PARM_DESC(max_mcast_grps,
  92. "Maximum number of multicast groups to support");
  93. unsigned int hfi1_max_mcast_qp_attached = 16;
  94. module_param_named(max_mcast_qp_attached, hfi1_max_mcast_qp_attached,
  95. uint, S_IRUGO);
  96. MODULE_PARM_DESC(max_mcast_qp_attached,
  97. "Maximum number of attached QPs to support");
  98. unsigned int hfi1_max_srqs = 1024;
  99. module_param_named(max_srqs, hfi1_max_srqs, uint, S_IRUGO);
  100. MODULE_PARM_DESC(max_srqs, "Maximum number of SRQs to support");
  101. unsigned int hfi1_max_srq_sges = 128;
  102. module_param_named(max_srq_sges, hfi1_max_srq_sges, uint, S_IRUGO);
  103. MODULE_PARM_DESC(max_srq_sges, "Maximum number of SRQ SGEs to support");
  104. unsigned int hfi1_max_srq_wrs = 0x1FFFF;
  105. module_param_named(max_srq_wrs, hfi1_max_srq_wrs, uint, S_IRUGO);
  106. MODULE_PARM_DESC(max_srq_wrs, "Maximum number of SRQ WRs support");
  107. unsigned short piothreshold = 256;
  108. module_param(piothreshold, ushort, S_IRUGO);
  109. MODULE_PARM_DESC(piothreshold, "size used to determine sdma vs. pio");
  110. #define COPY_CACHELESS 1
  111. #define COPY_ADAPTIVE 2
  112. static unsigned int sge_copy_mode;
  113. module_param(sge_copy_mode, uint, S_IRUGO);
  114. MODULE_PARM_DESC(sge_copy_mode,
  115. "Verbs copy mode: 0 use memcpy, 1 use cacheless copy, 2 adapt based on WSS");
  116. static void verbs_sdma_complete(
  117. struct sdma_txreq *cookie,
  118. int status);
  119. static int pio_wait(struct rvt_qp *qp,
  120. struct send_context *sc,
  121. struct hfi1_pkt_state *ps,
  122. u32 flag);
  123. /* Length of buffer to create verbs txreq cache name */
  124. #define TXREQ_NAME_LEN 24
  125. static uint wss_threshold;
  126. module_param(wss_threshold, uint, S_IRUGO);
  127. MODULE_PARM_DESC(wss_threshold, "Percentage (1-100) of LLC to use as a threshold for a cacheless copy");
  128. static uint wss_clean_period = 256;
  129. module_param(wss_clean_period, uint, S_IRUGO);
  130. MODULE_PARM_DESC(wss_clean_period, "Count of verbs copies before an entry in the page copy table is cleaned");
  131. /* memory working set size */
  132. struct hfi1_wss {
  133. unsigned long *entries;
  134. atomic_t total_count;
  135. atomic_t clean_counter;
  136. atomic_t clean_entry;
  137. int threshold;
  138. int num_entries;
  139. long pages_mask;
  140. };
  141. static struct hfi1_wss wss;
  142. int hfi1_wss_init(void)
  143. {
  144. long llc_size;
  145. long llc_bits;
  146. long table_size;
  147. long table_bits;
  148. /* check for a valid percent range - default to 80 if none or invalid */
  149. if (wss_threshold < 1 || wss_threshold > 100)
  150. wss_threshold = 80;
  151. /* reject a wildly large period */
  152. if (wss_clean_period > 1000000)
  153. wss_clean_period = 256;
  154. /* reject a zero period */
  155. if (wss_clean_period == 0)
  156. wss_clean_period = 1;
  157. /*
  158. * Calculate the table size - the next power of 2 larger than the
  159. * LLC size. LLC size is in KiB.
  160. */
  161. llc_size = wss_llc_size() * 1024;
  162. table_size = roundup_pow_of_two(llc_size);
  163. /* one bit per page in rounded up table */
  164. llc_bits = llc_size / PAGE_SIZE;
  165. table_bits = table_size / PAGE_SIZE;
  166. wss.pages_mask = table_bits - 1;
  167. wss.num_entries = table_bits / BITS_PER_LONG;
  168. wss.threshold = (llc_bits * wss_threshold) / 100;
  169. if (wss.threshold == 0)
  170. wss.threshold = 1;
  171. atomic_set(&wss.clean_counter, wss_clean_period);
  172. wss.entries = kcalloc(wss.num_entries, sizeof(*wss.entries),
  173. GFP_KERNEL);
  174. if (!wss.entries) {
  175. hfi1_wss_exit();
  176. return -ENOMEM;
  177. }
  178. return 0;
  179. }
  180. void hfi1_wss_exit(void)
  181. {
  182. /* coded to handle partially initialized and repeat callers */
  183. kfree(wss.entries);
  184. wss.entries = NULL;
  185. }
  186. /*
  187. * Advance the clean counter. When the clean period has expired,
  188. * clean an entry.
  189. *
  190. * This is implemented in atomics to avoid locking. Because multiple
  191. * variables are involved, it can be racy which can lead to slightly
  192. * inaccurate information. Since this is only a heuristic, this is
  193. * OK. Any innaccuracies will clean themselves out as the counter
  194. * advances. That said, it is unlikely the entry clean operation will
  195. * race - the next possible racer will not start until the next clean
  196. * period.
  197. *
  198. * The clean counter is implemented as a decrement to zero. When zero
  199. * is reached an entry is cleaned.
  200. */
  201. static void wss_advance_clean_counter(void)
  202. {
  203. int entry;
  204. int weight;
  205. unsigned long bits;
  206. /* become the cleaner if we decrement the counter to zero */
  207. if (atomic_dec_and_test(&wss.clean_counter)) {
  208. /*
  209. * Set, not add, the clean period. This avoids an issue
  210. * where the counter could decrement below the clean period.
  211. * Doing a set can result in lost decrements, slowing the
  212. * clean advance. Since this a heuristic, this possible
  213. * slowdown is OK.
  214. *
  215. * An alternative is to loop, advancing the counter by a
  216. * clean period until the result is > 0. However, this could
  217. * lead to several threads keeping another in the clean loop.
  218. * This could be mitigated by limiting the number of times
  219. * we stay in the loop.
  220. */
  221. atomic_set(&wss.clean_counter, wss_clean_period);
  222. /*
  223. * Uniquely grab the entry to clean and move to next.
  224. * The current entry is always the lower bits of
  225. * wss.clean_entry. The table size, wss.num_entries,
  226. * is always a power-of-2.
  227. */
  228. entry = (atomic_inc_return(&wss.clean_entry) - 1)
  229. & (wss.num_entries - 1);
  230. /* clear the entry and count the bits */
  231. bits = xchg(&wss.entries[entry], 0);
  232. weight = hweight64((u64)bits);
  233. /* only adjust the contended total count if needed */
  234. if (weight)
  235. atomic_sub(weight, &wss.total_count);
  236. }
  237. }
  238. /*
  239. * Insert the given address into the working set array.
  240. */
  241. static void wss_insert(void *address)
  242. {
  243. u32 page = ((unsigned long)address >> PAGE_SHIFT) & wss.pages_mask;
  244. u32 entry = page / BITS_PER_LONG; /* assumes this ends up a shift */
  245. u32 nr = page & (BITS_PER_LONG - 1);
  246. if (!test_and_set_bit(nr, &wss.entries[entry]))
  247. atomic_inc(&wss.total_count);
  248. wss_advance_clean_counter();
  249. }
  250. /*
  251. * Is the working set larger than the threshold?
  252. */
  253. static inline int wss_exceeds_threshold(void)
  254. {
  255. return atomic_read(&wss.total_count) >= wss.threshold;
  256. }
  257. /*
  258. * Length of header by opcode, 0 --> not supported
  259. */
  260. const u8 hdr_len_by_opcode[256] = {
  261. /* RC */
  262. [IB_OPCODE_RC_SEND_FIRST] = 12 + 8,
  263. [IB_OPCODE_RC_SEND_MIDDLE] = 12 + 8,
  264. [IB_OPCODE_RC_SEND_LAST] = 12 + 8,
  265. [IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE] = 12 + 8 + 4,
  266. [IB_OPCODE_RC_SEND_ONLY] = 12 + 8,
  267. [IB_OPCODE_RC_SEND_ONLY_WITH_IMMEDIATE] = 12 + 8 + 4,
  268. [IB_OPCODE_RC_RDMA_WRITE_FIRST] = 12 + 8 + 16,
  269. [IB_OPCODE_RC_RDMA_WRITE_MIDDLE] = 12 + 8,
  270. [IB_OPCODE_RC_RDMA_WRITE_LAST] = 12 + 8,
  271. [IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE] = 12 + 8 + 4,
  272. [IB_OPCODE_RC_RDMA_WRITE_ONLY] = 12 + 8 + 16,
  273. [IB_OPCODE_RC_RDMA_WRITE_ONLY_WITH_IMMEDIATE] = 12 + 8 + 20,
  274. [IB_OPCODE_RC_RDMA_READ_REQUEST] = 12 + 8 + 16,
  275. [IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST] = 12 + 8 + 4,
  276. [IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE] = 12 + 8,
  277. [IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST] = 12 + 8 + 4,
  278. [IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY] = 12 + 8 + 4,
  279. [IB_OPCODE_RC_ACKNOWLEDGE] = 12 + 8 + 4,
  280. [IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE] = 12 + 8 + 4 + 8,
  281. [IB_OPCODE_RC_COMPARE_SWAP] = 12 + 8 + 28,
  282. [IB_OPCODE_RC_FETCH_ADD] = 12 + 8 + 28,
  283. [IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE] = 12 + 8 + 4,
  284. [IB_OPCODE_RC_SEND_ONLY_WITH_INVALIDATE] = 12 + 8 + 4,
  285. /* UC */
  286. [IB_OPCODE_UC_SEND_FIRST] = 12 + 8,
  287. [IB_OPCODE_UC_SEND_MIDDLE] = 12 + 8,
  288. [IB_OPCODE_UC_SEND_LAST] = 12 + 8,
  289. [IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE] = 12 + 8 + 4,
  290. [IB_OPCODE_UC_SEND_ONLY] = 12 + 8,
  291. [IB_OPCODE_UC_SEND_ONLY_WITH_IMMEDIATE] = 12 + 8 + 4,
  292. [IB_OPCODE_UC_RDMA_WRITE_FIRST] = 12 + 8 + 16,
  293. [IB_OPCODE_UC_RDMA_WRITE_MIDDLE] = 12 + 8,
  294. [IB_OPCODE_UC_RDMA_WRITE_LAST] = 12 + 8,
  295. [IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE] = 12 + 8 + 4,
  296. [IB_OPCODE_UC_RDMA_WRITE_ONLY] = 12 + 8 + 16,
  297. [IB_OPCODE_UC_RDMA_WRITE_ONLY_WITH_IMMEDIATE] = 12 + 8 + 20,
  298. /* UD */
  299. [IB_OPCODE_UD_SEND_ONLY] = 12 + 8 + 8,
  300. [IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE] = 12 + 8 + 12
  301. };
  302. static const opcode_handler opcode_handler_tbl[256] = {
  303. /* RC */
  304. [IB_OPCODE_RC_SEND_FIRST] = &hfi1_rc_rcv,
  305. [IB_OPCODE_RC_SEND_MIDDLE] = &hfi1_rc_rcv,
  306. [IB_OPCODE_RC_SEND_LAST] = &hfi1_rc_rcv,
  307. [IB_OPCODE_RC_SEND_LAST_WITH_IMMEDIATE] = &hfi1_rc_rcv,
  308. [IB_OPCODE_RC_SEND_ONLY] = &hfi1_rc_rcv,
  309. [IB_OPCODE_RC_SEND_ONLY_WITH_IMMEDIATE] = &hfi1_rc_rcv,
  310. [IB_OPCODE_RC_RDMA_WRITE_FIRST] = &hfi1_rc_rcv,
  311. [IB_OPCODE_RC_RDMA_WRITE_MIDDLE] = &hfi1_rc_rcv,
  312. [IB_OPCODE_RC_RDMA_WRITE_LAST] = &hfi1_rc_rcv,
  313. [IB_OPCODE_RC_RDMA_WRITE_LAST_WITH_IMMEDIATE] = &hfi1_rc_rcv,
  314. [IB_OPCODE_RC_RDMA_WRITE_ONLY] = &hfi1_rc_rcv,
  315. [IB_OPCODE_RC_RDMA_WRITE_ONLY_WITH_IMMEDIATE] = &hfi1_rc_rcv,
  316. [IB_OPCODE_RC_RDMA_READ_REQUEST] = &hfi1_rc_rcv,
  317. [IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST] = &hfi1_rc_rcv,
  318. [IB_OPCODE_RC_RDMA_READ_RESPONSE_MIDDLE] = &hfi1_rc_rcv,
  319. [IB_OPCODE_RC_RDMA_READ_RESPONSE_LAST] = &hfi1_rc_rcv,
  320. [IB_OPCODE_RC_RDMA_READ_RESPONSE_ONLY] = &hfi1_rc_rcv,
  321. [IB_OPCODE_RC_ACKNOWLEDGE] = &hfi1_rc_rcv,
  322. [IB_OPCODE_RC_ATOMIC_ACKNOWLEDGE] = &hfi1_rc_rcv,
  323. [IB_OPCODE_RC_COMPARE_SWAP] = &hfi1_rc_rcv,
  324. [IB_OPCODE_RC_FETCH_ADD] = &hfi1_rc_rcv,
  325. [IB_OPCODE_RC_SEND_LAST_WITH_INVALIDATE] = &hfi1_rc_rcv,
  326. [IB_OPCODE_RC_SEND_ONLY_WITH_INVALIDATE] = &hfi1_rc_rcv,
  327. /* UC */
  328. [IB_OPCODE_UC_SEND_FIRST] = &hfi1_uc_rcv,
  329. [IB_OPCODE_UC_SEND_MIDDLE] = &hfi1_uc_rcv,
  330. [IB_OPCODE_UC_SEND_LAST] = &hfi1_uc_rcv,
  331. [IB_OPCODE_UC_SEND_LAST_WITH_IMMEDIATE] = &hfi1_uc_rcv,
  332. [IB_OPCODE_UC_SEND_ONLY] = &hfi1_uc_rcv,
  333. [IB_OPCODE_UC_SEND_ONLY_WITH_IMMEDIATE] = &hfi1_uc_rcv,
  334. [IB_OPCODE_UC_RDMA_WRITE_FIRST] = &hfi1_uc_rcv,
  335. [IB_OPCODE_UC_RDMA_WRITE_MIDDLE] = &hfi1_uc_rcv,
  336. [IB_OPCODE_UC_RDMA_WRITE_LAST] = &hfi1_uc_rcv,
  337. [IB_OPCODE_UC_RDMA_WRITE_LAST_WITH_IMMEDIATE] = &hfi1_uc_rcv,
  338. [IB_OPCODE_UC_RDMA_WRITE_ONLY] = &hfi1_uc_rcv,
  339. [IB_OPCODE_UC_RDMA_WRITE_ONLY_WITH_IMMEDIATE] = &hfi1_uc_rcv,
  340. /* UD */
  341. [IB_OPCODE_UD_SEND_ONLY] = &hfi1_ud_rcv,
  342. [IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE] = &hfi1_ud_rcv,
  343. /* CNP */
  344. [IB_OPCODE_CNP] = &hfi1_cnp_rcv
  345. };
  346. #define OPMASK 0x1f
  347. static const u32 pio_opmask[BIT(3)] = {
  348. /* RC */
  349. [IB_OPCODE_RC >> 5] =
  350. BIT(RC_OP(SEND_ONLY) & OPMASK) |
  351. BIT(RC_OP(SEND_ONLY_WITH_IMMEDIATE) & OPMASK) |
  352. BIT(RC_OP(RDMA_WRITE_ONLY) & OPMASK) |
  353. BIT(RC_OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE) & OPMASK) |
  354. BIT(RC_OP(RDMA_READ_REQUEST) & OPMASK) |
  355. BIT(RC_OP(ACKNOWLEDGE) & OPMASK) |
  356. BIT(RC_OP(ATOMIC_ACKNOWLEDGE) & OPMASK) |
  357. BIT(RC_OP(COMPARE_SWAP) & OPMASK) |
  358. BIT(RC_OP(FETCH_ADD) & OPMASK),
  359. /* UC */
  360. [IB_OPCODE_UC >> 5] =
  361. BIT(UC_OP(SEND_ONLY) & OPMASK) |
  362. BIT(UC_OP(SEND_ONLY_WITH_IMMEDIATE) & OPMASK) |
  363. BIT(UC_OP(RDMA_WRITE_ONLY) & OPMASK) |
  364. BIT(UC_OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE) & OPMASK),
  365. };
  366. /*
  367. * System image GUID.
  368. */
  369. __be64 ib_hfi1_sys_image_guid;
  370. /**
  371. * hfi1_copy_sge - copy data to SGE memory
  372. * @ss: the SGE state
  373. * @data: the data to copy
  374. * @length: the length of the data
  375. * @copy_last: do a separate copy of the last 8 bytes
  376. */
  377. void hfi1_copy_sge(
  378. struct rvt_sge_state *ss,
  379. void *data, u32 length,
  380. int release,
  381. int copy_last)
  382. {
  383. struct rvt_sge *sge = &ss->sge;
  384. int in_last = 0;
  385. int i;
  386. int cacheless_copy = 0;
  387. if (sge_copy_mode == COPY_CACHELESS) {
  388. cacheless_copy = length >= PAGE_SIZE;
  389. } else if (sge_copy_mode == COPY_ADAPTIVE) {
  390. if (length >= PAGE_SIZE) {
  391. /*
  392. * NOTE: this *assumes*:
  393. * o The first vaddr is the dest.
  394. * o If multiple pages, then vaddr is sequential.
  395. */
  396. wss_insert(sge->vaddr);
  397. if (length >= (2 * PAGE_SIZE))
  398. wss_insert(sge->vaddr + PAGE_SIZE);
  399. cacheless_copy = wss_exceeds_threshold();
  400. } else {
  401. wss_advance_clean_counter();
  402. }
  403. }
  404. if (copy_last) {
  405. if (length > 8) {
  406. length -= 8;
  407. } else {
  408. copy_last = 0;
  409. in_last = 1;
  410. }
  411. }
  412. again:
  413. while (length) {
  414. u32 len = sge->length;
  415. if (len > length)
  416. len = length;
  417. if (len > sge->sge_length)
  418. len = sge->sge_length;
  419. WARN_ON_ONCE(len == 0);
  420. if (unlikely(in_last)) {
  421. /* enforce byte transfer ordering */
  422. for (i = 0; i < len; i++)
  423. ((u8 *)sge->vaddr)[i] = ((u8 *)data)[i];
  424. } else if (cacheless_copy) {
  425. cacheless_memcpy(sge->vaddr, data, len);
  426. } else {
  427. memcpy(sge->vaddr, data, len);
  428. }
  429. sge->vaddr += len;
  430. sge->length -= len;
  431. sge->sge_length -= len;
  432. if (sge->sge_length == 0) {
  433. if (release)
  434. rvt_put_mr(sge->mr);
  435. if (--ss->num_sge)
  436. *sge = *ss->sg_list++;
  437. } else if (sge->length == 0 && sge->mr->lkey) {
  438. if (++sge->n >= RVT_SEGSZ) {
  439. if (++sge->m >= sge->mr->mapsz)
  440. break;
  441. sge->n = 0;
  442. }
  443. sge->vaddr =
  444. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  445. sge->length =
  446. sge->mr->map[sge->m]->segs[sge->n].length;
  447. }
  448. data += len;
  449. length -= len;
  450. }
  451. if (copy_last) {
  452. copy_last = 0;
  453. in_last = 1;
  454. length = 8;
  455. goto again;
  456. }
  457. }
  458. /**
  459. * hfi1_skip_sge - skip over SGE memory
  460. * @ss: the SGE state
  461. * @length: the number of bytes to skip
  462. */
  463. void hfi1_skip_sge(struct rvt_sge_state *ss, u32 length, int release)
  464. {
  465. struct rvt_sge *sge = &ss->sge;
  466. while (length) {
  467. u32 len = sge->length;
  468. if (len > length)
  469. len = length;
  470. if (len > sge->sge_length)
  471. len = sge->sge_length;
  472. WARN_ON_ONCE(len == 0);
  473. sge->vaddr += len;
  474. sge->length -= len;
  475. sge->sge_length -= len;
  476. if (sge->sge_length == 0) {
  477. if (release)
  478. rvt_put_mr(sge->mr);
  479. if (--ss->num_sge)
  480. *sge = *ss->sg_list++;
  481. } else if (sge->length == 0 && sge->mr->lkey) {
  482. if (++sge->n >= RVT_SEGSZ) {
  483. if (++sge->m >= sge->mr->mapsz)
  484. break;
  485. sge->n = 0;
  486. }
  487. sge->vaddr =
  488. sge->mr->map[sge->m]->segs[sge->n].vaddr;
  489. sge->length =
  490. sge->mr->map[sge->m]->segs[sge->n].length;
  491. }
  492. length -= len;
  493. }
  494. }
  495. /*
  496. * Make sure the QP is ready and able to accept the given opcode.
  497. */
  498. static inline opcode_handler qp_ok(int opcode, struct hfi1_packet *packet)
  499. {
  500. if (!(ib_rvt_state_ops[packet->qp->state] & RVT_PROCESS_RECV_OK))
  501. return NULL;
  502. if (((opcode & RVT_OPCODE_QP_MASK) == packet->qp->allowed_ops) ||
  503. (opcode == IB_OPCODE_CNP))
  504. return opcode_handler_tbl[opcode];
  505. return NULL;
  506. }
  507. /**
  508. * hfi1_ib_rcv - process an incoming packet
  509. * @packet: data packet information
  510. *
  511. * This is called to process an incoming packet at interrupt level.
  512. *
  513. * Tlen is the length of the header + data + CRC in bytes.
  514. */
  515. void hfi1_ib_rcv(struct hfi1_packet *packet)
  516. {
  517. struct hfi1_ctxtdata *rcd = packet->rcd;
  518. struct ib_header *hdr = packet->hdr;
  519. u32 tlen = packet->tlen;
  520. struct hfi1_pportdata *ppd = rcd->ppd;
  521. struct hfi1_ibport *ibp = &ppd->ibport_data;
  522. struct rvt_dev_info *rdi = &ppd->dd->verbs_dev.rdi;
  523. opcode_handler packet_handler;
  524. unsigned long flags;
  525. u32 qp_num;
  526. int lnh;
  527. u8 opcode;
  528. u16 lid;
  529. /* Check for GRH */
  530. lnh = be16_to_cpu(hdr->lrh[0]) & 3;
  531. if (lnh == HFI1_LRH_BTH) {
  532. packet->ohdr = &hdr->u.oth;
  533. } else if (lnh == HFI1_LRH_GRH) {
  534. u32 vtf;
  535. packet->ohdr = &hdr->u.l.oth;
  536. if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
  537. goto drop;
  538. vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
  539. if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
  540. goto drop;
  541. packet->rcv_flags |= HFI1_HAS_GRH;
  542. } else {
  543. goto drop;
  544. }
  545. trace_input_ibhdr(rcd->dd, hdr);
  546. opcode = (be32_to_cpu(packet->ohdr->bth[0]) >> 24);
  547. inc_opstats(tlen, &rcd->opstats->stats[opcode]);
  548. /* Get the destination QP number. */
  549. qp_num = be32_to_cpu(packet->ohdr->bth[1]) & RVT_QPN_MASK;
  550. lid = be16_to_cpu(hdr->lrh[1]);
  551. if (unlikely((lid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
  552. (lid != be16_to_cpu(IB_LID_PERMISSIVE)))) {
  553. struct rvt_mcast *mcast;
  554. struct rvt_mcast_qp *p;
  555. if (lnh != HFI1_LRH_GRH)
  556. goto drop;
  557. mcast = rvt_mcast_find(&ibp->rvp, &hdr->u.l.grh.dgid);
  558. if (!mcast)
  559. goto drop;
  560. list_for_each_entry_rcu(p, &mcast->qp_list, list) {
  561. packet->qp = p->qp;
  562. spin_lock_irqsave(&packet->qp->r_lock, flags);
  563. packet_handler = qp_ok(opcode, packet);
  564. if (likely(packet_handler))
  565. packet_handler(packet);
  566. else
  567. ibp->rvp.n_pkt_drops++;
  568. spin_unlock_irqrestore(&packet->qp->r_lock, flags);
  569. }
  570. /*
  571. * Notify rvt_multicast_detach() if it is waiting for us
  572. * to finish.
  573. */
  574. if (atomic_dec_return(&mcast->refcount) <= 1)
  575. wake_up(&mcast->wait);
  576. } else {
  577. rcu_read_lock();
  578. packet->qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
  579. if (!packet->qp) {
  580. rcu_read_unlock();
  581. goto drop;
  582. }
  583. spin_lock_irqsave(&packet->qp->r_lock, flags);
  584. packet_handler = qp_ok(opcode, packet);
  585. if (likely(packet_handler))
  586. packet_handler(packet);
  587. else
  588. ibp->rvp.n_pkt_drops++;
  589. spin_unlock_irqrestore(&packet->qp->r_lock, flags);
  590. rcu_read_unlock();
  591. }
  592. return;
  593. drop:
  594. ibp->rvp.n_pkt_drops++;
  595. }
  596. /*
  597. * This is called from a timer to check for QPs
  598. * which need kernel memory in order to send a packet.
  599. */
  600. static void mem_timer(unsigned long data)
  601. {
  602. struct hfi1_ibdev *dev = (struct hfi1_ibdev *)data;
  603. struct list_head *list = &dev->memwait;
  604. struct rvt_qp *qp = NULL;
  605. struct iowait *wait;
  606. unsigned long flags;
  607. struct hfi1_qp_priv *priv;
  608. write_seqlock_irqsave(&dev->iowait_lock, flags);
  609. if (!list_empty(list)) {
  610. wait = list_first_entry(list, struct iowait, list);
  611. qp = iowait_to_qp(wait);
  612. priv = qp->priv;
  613. list_del_init(&priv->s_iowait.list);
  614. priv->s_iowait.lock = NULL;
  615. /* refcount held until actual wake up */
  616. if (!list_empty(list))
  617. mod_timer(&dev->mem_timer, jiffies + 1);
  618. }
  619. write_sequnlock_irqrestore(&dev->iowait_lock, flags);
  620. if (qp)
  621. hfi1_qp_wakeup(qp, RVT_S_WAIT_KMEM);
  622. }
  623. void update_sge(struct rvt_sge_state *ss, u32 length)
  624. {
  625. struct rvt_sge *sge = &ss->sge;
  626. sge->vaddr += length;
  627. sge->length -= length;
  628. sge->sge_length -= length;
  629. if (sge->sge_length == 0) {
  630. if (--ss->num_sge)
  631. *sge = *ss->sg_list++;
  632. } else if (sge->length == 0 && sge->mr->lkey) {
  633. if (++sge->n >= RVT_SEGSZ) {
  634. if (++sge->m >= sge->mr->mapsz)
  635. return;
  636. sge->n = 0;
  637. }
  638. sge->vaddr = sge->mr->map[sge->m]->segs[sge->n].vaddr;
  639. sge->length = sge->mr->map[sge->m]->segs[sge->n].length;
  640. }
  641. }
  642. /*
  643. * This is called with progress side lock held.
  644. */
  645. /* New API */
  646. static void verbs_sdma_complete(
  647. struct sdma_txreq *cookie,
  648. int status)
  649. {
  650. struct verbs_txreq *tx =
  651. container_of(cookie, struct verbs_txreq, txreq);
  652. struct rvt_qp *qp = tx->qp;
  653. spin_lock(&qp->s_lock);
  654. if (tx->wqe) {
  655. hfi1_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
  656. } else if (qp->ibqp.qp_type == IB_QPT_RC) {
  657. struct ib_header *hdr;
  658. hdr = &tx->phdr.hdr;
  659. hfi1_rc_send_complete(qp, hdr);
  660. }
  661. spin_unlock(&qp->s_lock);
  662. hfi1_put_txreq(tx);
  663. }
  664. static int wait_kmem(struct hfi1_ibdev *dev,
  665. struct rvt_qp *qp,
  666. struct hfi1_pkt_state *ps)
  667. {
  668. struct hfi1_qp_priv *priv = qp->priv;
  669. unsigned long flags;
  670. int ret = 0;
  671. spin_lock_irqsave(&qp->s_lock, flags);
  672. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  673. write_seqlock(&dev->iowait_lock);
  674. list_add_tail(&ps->s_txreq->txreq.list,
  675. &priv->s_iowait.tx_head);
  676. if (list_empty(&priv->s_iowait.list)) {
  677. if (list_empty(&dev->memwait))
  678. mod_timer(&dev->mem_timer, jiffies + 1);
  679. qp->s_flags |= RVT_S_WAIT_KMEM;
  680. list_add_tail(&priv->s_iowait.list, &dev->memwait);
  681. priv->s_iowait.lock = &dev->iowait_lock;
  682. trace_hfi1_qpsleep(qp, RVT_S_WAIT_KMEM);
  683. rvt_get_qp(qp);
  684. }
  685. write_sequnlock(&dev->iowait_lock);
  686. qp->s_flags &= ~RVT_S_BUSY;
  687. ret = -EBUSY;
  688. }
  689. spin_unlock_irqrestore(&qp->s_lock, flags);
  690. return ret;
  691. }
  692. /*
  693. * This routine calls txadds for each sg entry.
  694. *
  695. * Add failures will revert the sge cursor
  696. */
  697. static noinline int build_verbs_ulp_payload(
  698. struct sdma_engine *sde,
  699. u32 length,
  700. struct verbs_txreq *tx)
  701. {
  702. struct rvt_sge_state *ss = tx->ss;
  703. struct rvt_sge *sg_list = ss->sg_list;
  704. struct rvt_sge sge = ss->sge;
  705. u8 num_sge = ss->num_sge;
  706. u32 len;
  707. int ret = 0;
  708. while (length) {
  709. len = ss->sge.length;
  710. if (len > length)
  711. len = length;
  712. if (len > ss->sge.sge_length)
  713. len = ss->sge.sge_length;
  714. WARN_ON_ONCE(len == 0);
  715. ret = sdma_txadd_kvaddr(
  716. sde->dd,
  717. &tx->txreq,
  718. ss->sge.vaddr,
  719. len);
  720. if (ret)
  721. goto bail_txadd;
  722. update_sge(ss, len);
  723. length -= len;
  724. }
  725. return ret;
  726. bail_txadd:
  727. /* unwind cursor */
  728. ss->sge = sge;
  729. ss->num_sge = num_sge;
  730. ss->sg_list = sg_list;
  731. return ret;
  732. }
  733. /*
  734. * Build the number of DMA descriptors needed to send length bytes of data.
  735. *
  736. * NOTE: DMA mapping is held in the tx until completed in the ring or
  737. * the tx desc is freed without having been submitted to the ring
  738. *
  739. * This routine ensures all the helper routine calls succeed.
  740. */
  741. /* New API */
  742. static int build_verbs_tx_desc(
  743. struct sdma_engine *sde,
  744. u32 length,
  745. struct verbs_txreq *tx,
  746. struct hfi1_ahg_info *ahg_info,
  747. u64 pbc)
  748. {
  749. int ret = 0;
  750. struct hfi1_sdma_header *phdr = &tx->phdr;
  751. u16 hdrbytes = tx->hdr_dwords << 2;
  752. if (!ahg_info->ahgcount) {
  753. ret = sdma_txinit_ahg(
  754. &tx->txreq,
  755. ahg_info->tx_flags,
  756. hdrbytes + length,
  757. ahg_info->ahgidx,
  758. 0,
  759. NULL,
  760. 0,
  761. verbs_sdma_complete);
  762. if (ret)
  763. goto bail_txadd;
  764. phdr->pbc = cpu_to_le64(pbc);
  765. ret = sdma_txadd_kvaddr(
  766. sde->dd,
  767. &tx->txreq,
  768. phdr,
  769. hdrbytes);
  770. if (ret)
  771. goto bail_txadd;
  772. } else {
  773. ret = sdma_txinit_ahg(
  774. &tx->txreq,
  775. ahg_info->tx_flags,
  776. length,
  777. ahg_info->ahgidx,
  778. ahg_info->ahgcount,
  779. ahg_info->ahgdesc,
  780. hdrbytes,
  781. verbs_sdma_complete);
  782. if (ret)
  783. goto bail_txadd;
  784. }
  785. /* add the ulp payload - if any. tx->ss can be NULL for acks */
  786. if (tx->ss)
  787. ret = build_verbs_ulp_payload(sde, length, tx);
  788. bail_txadd:
  789. return ret;
  790. }
  791. int hfi1_verbs_send_dma(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
  792. u64 pbc)
  793. {
  794. struct hfi1_qp_priv *priv = qp->priv;
  795. struct hfi1_ahg_info *ahg_info = priv->s_ahg;
  796. u32 hdrwords = qp->s_hdrwords;
  797. u32 len = ps->s_txreq->s_cur_size;
  798. u32 plen = hdrwords + ((len + 3) >> 2) + 2; /* includes pbc */
  799. struct hfi1_ibdev *dev = ps->dev;
  800. struct hfi1_pportdata *ppd = ps->ppd;
  801. struct verbs_txreq *tx;
  802. u64 pbc_flags = 0;
  803. u8 sc5 = priv->s_sc;
  804. int ret;
  805. tx = ps->s_txreq;
  806. if (!sdma_txreq_built(&tx->txreq)) {
  807. if (likely(pbc == 0)) {
  808. u32 vl = sc_to_vlt(dd_from_ibdev(qp->ibqp.device), sc5);
  809. /* No vl15 here */
  810. /* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */
  811. pbc_flags |= (!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT;
  812. pbc = create_pbc(ppd,
  813. pbc_flags,
  814. qp->srate_mbps,
  815. vl,
  816. plen);
  817. }
  818. tx->wqe = qp->s_wqe;
  819. ret = build_verbs_tx_desc(tx->sde, len, tx, ahg_info, pbc);
  820. if (unlikely(ret))
  821. goto bail_build;
  822. }
  823. ret = sdma_send_txreq(tx->sde, &priv->s_iowait, &tx->txreq);
  824. if (unlikely(ret < 0)) {
  825. if (ret == -ECOMM)
  826. goto bail_ecomm;
  827. return ret;
  828. }
  829. trace_sdma_output_ibhdr(dd_from_ibdev(qp->ibqp.device),
  830. &ps->s_txreq->phdr.hdr);
  831. return ret;
  832. bail_ecomm:
  833. /* The current one got "sent" */
  834. return 0;
  835. bail_build:
  836. ret = wait_kmem(dev, qp, ps);
  837. if (!ret) {
  838. /* free txreq - bad state */
  839. hfi1_put_txreq(ps->s_txreq);
  840. ps->s_txreq = NULL;
  841. }
  842. return ret;
  843. }
  844. /*
  845. * If we are now in the error state, return zero to flush the
  846. * send work request.
  847. */
  848. static int pio_wait(struct rvt_qp *qp,
  849. struct send_context *sc,
  850. struct hfi1_pkt_state *ps,
  851. u32 flag)
  852. {
  853. struct hfi1_qp_priv *priv = qp->priv;
  854. struct hfi1_devdata *dd = sc->dd;
  855. struct hfi1_ibdev *dev = &dd->verbs_dev;
  856. unsigned long flags;
  857. int ret = 0;
  858. /*
  859. * Note that as soon as want_buffer() is called and
  860. * possibly before it returns, sc_piobufavail()
  861. * could be called. Therefore, put QP on the I/O wait list before
  862. * enabling the PIO avail interrupt.
  863. */
  864. spin_lock_irqsave(&qp->s_lock, flags);
  865. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  866. write_seqlock(&dev->iowait_lock);
  867. list_add_tail(&ps->s_txreq->txreq.list,
  868. &priv->s_iowait.tx_head);
  869. if (list_empty(&priv->s_iowait.list)) {
  870. struct hfi1_ibdev *dev = &dd->verbs_dev;
  871. int was_empty;
  872. dev->n_piowait += !!(flag & RVT_S_WAIT_PIO);
  873. dev->n_piodrain += !!(flag & RVT_S_WAIT_PIO_DRAIN);
  874. qp->s_flags |= flag;
  875. was_empty = list_empty(&sc->piowait);
  876. list_add_tail(&priv->s_iowait.list, &sc->piowait);
  877. priv->s_iowait.lock = &dev->iowait_lock;
  878. trace_hfi1_qpsleep(qp, RVT_S_WAIT_PIO);
  879. rvt_get_qp(qp);
  880. /* counting: only call wantpiobuf_intr if first user */
  881. if (was_empty)
  882. hfi1_sc_wantpiobuf_intr(sc, 1);
  883. }
  884. write_sequnlock(&dev->iowait_lock);
  885. qp->s_flags &= ~RVT_S_BUSY;
  886. ret = -EBUSY;
  887. }
  888. spin_unlock_irqrestore(&qp->s_lock, flags);
  889. return ret;
  890. }
  891. static void verbs_pio_complete(void *arg, int code)
  892. {
  893. struct rvt_qp *qp = (struct rvt_qp *)arg;
  894. struct hfi1_qp_priv *priv = qp->priv;
  895. if (iowait_pio_dec(&priv->s_iowait))
  896. iowait_drain_wakeup(&priv->s_iowait);
  897. }
  898. int hfi1_verbs_send_pio(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
  899. u64 pbc)
  900. {
  901. struct hfi1_qp_priv *priv = qp->priv;
  902. u32 hdrwords = qp->s_hdrwords;
  903. struct rvt_sge_state *ss = ps->s_txreq->ss;
  904. u32 len = ps->s_txreq->s_cur_size;
  905. u32 dwords = (len + 3) >> 2;
  906. u32 plen = hdrwords + dwords + 2; /* includes pbc */
  907. struct hfi1_pportdata *ppd = ps->ppd;
  908. u32 *hdr = (u32 *)&ps->s_txreq->phdr.hdr;
  909. u64 pbc_flags = 0;
  910. u8 sc5;
  911. unsigned long flags = 0;
  912. struct send_context *sc;
  913. struct pio_buf *pbuf;
  914. int wc_status = IB_WC_SUCCESS;
  915. int ret = 0;
  916. pio_release_cb cb = NULL;
  917. /* only RC/UC use complete */
  918. switch (qp->ibqp.qp_type) {
  919. case IB_QPT_RC:
  920. case IB_QPT_UC:
  921. cb = verbs_pio_complete;
  922. break;
  923. default:
  924. break;
  925. }
  926. /* vl15 special case taken care of in ud.c */
  927. sc5 = priv->s_sc;
  928. sc = ps->s_txreq->psc;
  929. if (likely(pbc == 0)) {
  930. u8 vl = sc_to_vlt(dd_from_ibdev(qp->ibqp.device), sc5);
  931. /* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */
  932. pbc_flags |= (!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT;
  933. pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
  934. }
  935. if (cb)
  936. iowait_pio_inc(&priv->s_iowait);
  937. pbuf = sc_buffer_alloc(sc, plen, cb, qp);
  938. if (unlikely(!pbuf)) {
  939. if (cb)
  940. verbs_pio_complete(qp, 0);
  941. if (ppd->host_link_state != HLS_UP_ACTIVE) {
  942. /*
  943. * If we have filled the PIO buffers to capacity and are
  944. * not in an active state this request is not going to
  945. * go out to so just complete it with an error or else a
  946. * ULP or the core may be stuck waiting.
  947. */
  948. hfi1_cdbg(
  949. PIO,
  950. "alloc failed. state not active, completing");
  951. wc_status = IB_WC_GENERAL_ERR;
  952. goto pio_bail;
  953. } else {
  954. /*
  955. * This is a normal occurrence. The PIO buffs are full
  956. * up but we are still happily sending, well we could be
  957. * so lets continue to queue the request.
  958. */
  959. hfi1_cdbg(PIO, "alloc failed. state active, queuing");
  960. ret = pio_wait(qp, sc, ps, RVT_S_WAIT_PIO);
  961. if (!ret)
  962. /* txreq not queued - free */
  963. goto bail;
  964. /* tx consumed in wait */
  965. return ret;
  966. }
  967. }
  968. if (len == 0) {
  969. pio_copy(ppd->dd, pbuf, pbc, hdr, hdrwords);
  970. } else {
  971. if (ss) {
  972. seg_pio_copy_start(pbuf, pbc, hdr, hdrwords * 4);
  973. while (len) {
  974. void *addr = ss->sge.vaddr;
  975. u32 slen = ss->sge.length;
  976. if (slen > len)
  977. slen = len;
  978. update_sge(ss, slen);
  979. seg_pio_copy_mid(pbuf, addr, slen);
  980. len -= slen;
  981. }
  982. seg_pio_copy_end(pbuf);
  983. }
  984. }
  985. trace_pio_output_ibhdr(dd_from_ibdev(qp->ibqp.device),
  986. &ps->s_txreq->phdr.hdr);
  987. pio_bail:
  988. if (qp->s_wqe) {
  989. spin_lock_irqsave(&qp->s_lock, flags);
  990. hfi1_send_complete(qp, qp->s_wqe, wc_status);
  991. spin_unlock_irqrestore(&qp->s_lock, flags);
  992. } else if (qp->ibqp.qp_type == IB_QPT_RC) {
  993. spin_lock_irqsave(&qp->s_lock, flags);
  994. hfi1_rc_send_complete(qp, &ps->s_txreq->phdr.hdr);
  995. spin_unlock_irqrestore(&qp->s_lock, flags);
  996. }
  997. ret = 0;
  998. bail:
  999. hfi1_put_txreq(ps->s_txreq);
  1000. return ret;
  1001. }
  1002. /*
  1003. * egress_pkey_matches_entry - return 1 if the pkey matches ent (ent
  1004. * being an entry from the partition key table), return 0
  1005. * otherwise. Use the matching criteria for egress partition keys
  1006. * specified in the OPAv1 spec., section 9.1l.7.
  1007. */
  1008. static inline int egress_pkey_matches_entry(u16 pkey, u16 ent)
  1009. {
  1010. u16 mkey = pkey & PKEY_LOW_15_MASK;
  1011. u16 mentry = ent & PKEY_LOW_15_MASK;
  1012. if (mkey == mentry) {
  1013. /*
  1014. * If pkey[15] is set (full partition member),
  1015. * is bit 15 in the corresponding table element
  1016. * clear (limited member)?
  1017. */
  1018. if (pkey & PKEY_MEMBER_MASK)
  1019. return !!(ent & PKEY_MEMBER_MASK);
  1020. return 1;
  1021. }
  1022. return 0;
  1023. }
  1024. /**
  1025. * egress_pkey_check - check P_KEY of a packet
  1026. * @ppd: Physical IB port data
  1027. * @lrh: Local route header
  1028. * @bth: Base transport header
  1029. * @sc5: SC for packet
  1030. * @s_pkey_index: It will be used for look up optimization for kernel contexts
  1031. * only. If it is negative value, then it means user contexts is calling this
  1032. * function.
  1033. *
  1034. * It checks if hdr's pkey is valid.
  1035. *
  1036. * Return: 0 on success, otherwise, 1
  1037. */
  1038. int egress_pkey_check(struct hfi1_pportdata *ppd, __be16 *lrh, __be32 *bth,
  1039. u8 sc5, int8_t s_pkey_index)
  1040. {
  1041. struct hfi1_devdata *dd;
  1042. int i;
  1043. u16 pkey;
  1044. int is_user_ctxt_mechanism = (s_pkey_index < 0);
  1045. if (!(ppd->part_enforce & HFI1_PART_ENFORCE_OUT))
  1046. return 0;
  1047. pkey = (u16)be32_to_cpu(bth[0]);
  1048. /* If SC15, pkey[0:14] must be 0x7fff */
  1049. if ((sc5 == 0xf) && ((pkey & PKEY_LOW_15_MASK) != PKEY_LOW_15_MASK))
  1050. goto bad;
  1051. /* Is the pkey = 0x0, or 0x8000? */
  1052. if ((pkey & PKEY_LOW_15_MASK) == 0)
  1053. goto bad;
  1054. /*
  1055. * For the kernel contexts only, if a qp is passed into the function,
  1056. * the most likely matching pkey has index qp->s_pkey_index
  1057. */
  1058. if (!is_user_ctxt_mechanism &&
  1059. egress_pkey_matches_entry(pkey, ppd->pkeys[s_pkey_index])) {
  1060. return 0;
  1061. }
  1062. for (i = 0; i < MAX_PKEY_VALUES; i++) {
  1063. if (egress_pkey_matches_entry(pkey, ppd->pkeys[i]))
  1064. return 0;
  1065. }
  1066. bad:
  1067. /*
  1068. * For the user-context mechanism, the P_KEY check would only happen
  1069. * once per SDMA request, not once per packet. Therefore, there's no
  1070. * need to increment the counter for the user-context mechanism.
  1071. */
  1072. if (!is_user_ctxt_mechanism) {
  1073. incr_cntr64(&ppd->port_xmit_constraint_errors);
  1074. dd = ppd->dd;
  1075. if (!(dd->err_info_xmit_constraint.status &
  1076. OPA_EI_STATUS_SMASK)) {
  1077. u16 slid = be16_to_cpu(lrh[3]);
  1078. dd->err_info_xmit_constraint.status |=
  1079. OPA_EI_STATUS_SMASK;
  1080. dd->err_info_xmit_constraint.slid = slid;
  1081. dd->err_info_xmit_constraint.pkey = pkey;
  1082. }
  1083. }
  1084. return 1;
  1085. }
  1086. /**
  1087. * get_send_routine - choose an egress routine
  1088. *
  1089. * Choose an egress routine based on QP type
  1090. * and size
  1091. */
  1092. static inline send_routine get_send_routine(struct rvt_qp *qp,
  1093. struct verbs_txreq *tx)
  1094. {
  1095. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  1096. struct hfi1_qp_priv *priv = qp->priv;
  1097. struct ib_header *h = &tx->phdr.hdr;
  1098. if (unlikely(!(dd->flags & HFI1_HAS_SEND_DMA)))
  1099. return dd->process_pio_send;
  1100. switch (qp->ibqp.qp_type) {
  1101. case IB_QPT_SMI:
  1102. return dd->process_pio_send;
  1103. case IB_QPT_GSI:
  1104. case IB_QPT_UD:
  1105. break;
  1106. case IB_QPT_UC:
  1107. case IB_QPT_RC: {
  1108. u8 op = get_opcode(h);
  1109. if (piothreshold &&
  1110. tx->s_cur_size <= min(piothreshold, qp->pmtu) &&
  1111. (BIT(op & OPMASK) & pio_opmask[op >> 5]) &&
  1112. iowait_sdma_pending(&priv->s_iowait) == 0 &&
  1113. !sdma_txreq_built(&tx->txreq))
  1114. return dd->process_pio_send;
  1115. break;
  1116. }
  1117. default:
  1118. break;
  1119. }
  1120. return dd->process_dma_send;
  1121. }
  1122. /**
  1123. * hfi1_verbs_send - send a packet
  1124. * @qp: the QP to send on
  1125. * @ps: the state of the packet to send
  1126. *
  1127. * Return zero if packet is sent or queued OK.
  1128. * Return non-zero and clear qp->s_flags RVT_S_BUSY otherwise.
  1129. */
  1130. int hfi1_verbs_send(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
  1131. {
  1132. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  1133. struct hfi1_qp_priv *priv = qp->priv;
  1134. struct ib_other_headers *ohdr;
  1135. struct ib_header *hdr;
  1136. send_routine sr;
  1137. int ret;
  1138. u8 lnh;
  1139. hdr = &ps->s_txreq->phdr.hdr;
  1140. /* locate the pkey within the headers */
  1141. lnh = be16_to_cpu(hdr->lrh[0]) & 3;
  1142. if (lnh == HFI1_LRH_GRH)
  1143. ohdr = &hdr->u.l.oth;
  1144. else
  1145. ohdr = &hdr->u.oth;
  1146. sr = get_send_routine(qp, ps->s_txreq);
  1147. ret = egress_pkey_check(dd->pport,
  1148. hdr->lrh,
  1149. ohdr->bth,
  1150. priv->s_sc,
  1151. qp->s_pkey_index);
  1152. if (unlikely(ret)) {
  1153. /*
  1154. * The value we are returning here does not get propagated to
  1155. * the verbs caller. Thus we need to complete the request with
  1156. * error otherwise the caller could be sitting waiting on the
  1157. * completion event. Only do this for PIO. SDMA has its own
  1158. * mechanism for handling the errors. So for SDMA we can just
  1159. * return.
  1160. */
  1161. if (sr == dd->process_pio_send) {
  1162. unsigned long flags;
  1163. hfi1_cdbg(PIO, "%s() Failed. Completing with err",
  1164. __func__);
  1165. spin_lock_irqsave(&qp->s_lock, flags);
  1166. hfi1_send_complete(qp, qp->s_wqe, IB_WC_GENERAL_ERR);
  1167. spin_unlock_irqrestore(&qp->s_lock, flags);
  1168. }
  1169. return -EINVAL;
  1170. }
  1171. if (sr == dd->process_dma_send && iowait_pio_pending(&priv->s_iowait))
  1172. return pio_wait(qp,
  1173. ps->s_txreq->psc,
  1174. ps,
  1175. RVT_S_WAIT_PIO_DRAIN);
  1176. return sr(qp, ps, 0);
  1177. }
  1178. /**
  1179. * hfi1_fill_device_attr - Fill in rvt dev info device attributes.
  1180. * @dd: the device data structure
  1181. */
  1182. static void hfi1_fill_device_attr(struct hfi1_devdata *dd)
  1183. {
  1184. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  1185. u16 ver = dd->dc8051_ver;
  1186. memset(&rdi->dparms.props, 0, sizeof(rdi->dparms.props));
  1187. rdi->dparms.props.fw_ver = ((u64)(dc8051_ver_maj(ver)) << 16) |
  1188. (u64)dc8051_ver_min(ver);
  1189. rdi->dparms.props.device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
  1190. IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
  1191. IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
  1192. IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE |
  1193. IB_DEVICE_MEM_MGT_EXTENSIONS;
  1194. rdi->dparms.props.page_size_cap = PAGE_SIZE;
  1195. rdi->dparms.props.vendor_id = dd->oui1 << 16 | dd->oui2 << 8 | dd->oui3;
  1196. rdi->dparms.props.vendor_part_id = dd->pcidev->device;
  1197. rdi->dparms.props.hw_ver = dd->minrev;
  1198. rdi->dparms.props.sys_image_guid = ib_hfi1_sys_image_guid;
  1199. rdi->dparms.props.max_mr_size = U64_MAX;
  1200. rdi->dparms.props.max_fast_reg_page_list_len = UINT_MAX;
  1201. rdi->dparms.props.max_qp = hfi1_max_qps;
  1202. rdi->dparms.props.max_qp_wr = hfi1_max_qp_wrs;
  1203. rdi->dparms.props.max_sge = hfi1_max_sges;
  1204. rdi->dparms.props.max_sge_rd = hfi1_max_sges;
  1205. rdi->dparms.props.max_cq = hfi1_max_cqs;
  1206. rdi->dparms.props.max_ah = hfi1_max_ahs;
  1207. rdi->dparms.props.max_cqe = hfi1_max_cqes;
  1208. rdi->dparms.props.max_mr = rdi->lkey_table.max;
  1209. rdi->dparms.props.max_fmr = rdi->lkey_table.max;
  1210. rdi->dparms.props.max_map_per_fmr = 32767;
  1211. rdi->dparms.props.max_pd = hfi1_max_pds;
  1212. rdi->dparms.props.max_qp_rd_atom = HFI1_MAX_RDMA_ATOMIC;
  1213. rdi->dparms.props.max_qp_init_rd_atom = 255;
  1214. rdi->dparms.props.max_srq = hfi1_max_srqs;
  1215. rdi->dparms.props.max_srq_wr = hfi1_max_srq_wrs;
  1216. rdi->dparms.props.max_srq_sge = hfi1_max_srq_sges;
  1217. rdi->dparms.props.atomic_cap = IB_ATOMIC_GLOB;
  1218. rdi->dparms.props.max_pkeys = hfi1_get_npkeys(dd);
  1219. rdi->dparms.props.max_mcast_grp = hfi1_max_mcast_grps;
  1220. rdi->dparms.props.max_mcast_qp_attach = hfi1_max_mcast_qp_attached;
  1221. rdi->dparms.props.max_total_mcast_qp_attach =
  1222. rdi->dparms.props.max_mcast_qp_attach *
  1223. rdi->dparms.props.max_mcast_grp;
  1224. }
  1225. static inline u16 opa_speed_to_ib(u16 in)
  1226. {
  1227. u16 out = 0;
  1228. if (in & OPA_LINK_SPEED_25G)
  1229. out |= IB_SPEED_EDR;
  1230. if (in & OPA_LINK_SPEED_12_5G)
  1231. out |= IB_SPEED_FDR;
  1232. return out;
  1233. }
  1234. /*
  1235. * Convert a single OPA link width (no multiple flags) to an IB value.
  1236. * A zero OPA link width means link down, which means the IB width value
  1237. * is a don't care.
  1238. */
  1239. static inline u16 opa_width_to_ib(u16 in)
  1240. {
  1241. switch (in) {
  1242. case OPA_LINK_WIDTH_1X:
  1243. /* map 2x and 3x to 1x as they don't exist in IB */
  1244. case OPA_LINK_WIDTH_2X:
  1245. case OPA_LINK_WIDTH_3X:
  1246. return IB_WIDTH_1X;
  1247. default: /* link down or unknown, return our largest width */
  1248. case OPA_LINK_WIDTH_4X:
  1249. return IB_WIDTH_4X;
  1250. }
  1251. }
  1252. static int query_port(struct rvt_dev_info *rdi, u8 port_num,
  1253. struct ib_port_attr *props)
  1254. {
  1255. struct hfi1_ibdev *verbs_dev = dev_from_rdi(rdi);
  1256. struct hfi1_devdata *dd = dd_from_dev(verbs_dev);
  1257. struct hfi1_pportdata *ppd = &dd->pport[port_num - 1];
  1258. u16 lid = ppd->lid;
  1259. props->lid = lid ? lid : 0;
  1260. props->lmc = ppd->lmc;
  1261. /* OPA logical states match IB logical states */
  1262. props->state = driver_lstate(ppd);
  1263. props->phys_state = hfi1_ibphys_portstate(ppd);
  1264. props->gid_tbl_len = HFI1_GUIDS_PER_PORT;
  1265. props->active_width = (u8)opa_width_to_ib(ppd->link_width_active);
  1266. /* see rate_show() in ib core/sysfs.c */
  1267. props->active_speed = (u8)opa_speed_to_ib(ppd->link_speed_active);
  1268. props->max_vl_num = ppd->vls_supported;
  1269. /* Once we are a "first class" citizen and have added the OPA MTUs to
  1270. * the core we can advertise the larger MTU enum to the ULPs, for now
  1271. * advertise only 4K.
  1272. *
  1273. * Those applications which are either OPA aware or pass the MTU enum
  1274. * from the Path Records to us will get the new 8k MTU. Those that
  1275. * attempt to process the MTU enum may fail in various ways.
  1276. */
  1277. props->max_mtu = mtu_to_enum((!valid_ib_mtu(hfi1_max_mtu) ?
  1278. 4096 : hfi1_max_mtu), IB_MTU_4096);
  1279. props->active_mtu = !valid_ib_mtu(ppd->ibmtu) ? props->max_mtu :
  1280. mtu_to_enum(ppd->ibmtu, IB_MTU_2048);
  1281. return 0;
  1282. }
  1283. static int modify_device(struct ib_device *device,
  1284. int device_modify_mask,
  1285. struct ib_device_modify *device_modify)
  1286. {
  1287. struct hfi1_devdata *dd = dd_from_ibdev(device);
  1288. unsigned i;
  1289. int ret;
  1290. if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
  1291. IB_DEVICE_MODIFY_NODE_DESC)) {
  1292. ret = -EOPNOTSUPP;
  1293. goto bail;
  1294. }
  1295. if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
  1296. memcpy(device->node_desc, device_modify->node_desc,
  1297. IB_DEVICE_NODE_DESC_MAX);
  1298. for (i = 0; i < dd->num_pports; i++) {
  1299. struct hfi1_ibport *ibp = &dd->pport[i].ibport_data;
  1300. hfi1_node_desc_chg(ibp);
  1301. }
  1302. }
  1303. if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
  1304. ib_hfi1_sys_image_guid =
  1305. cpu_to_be64(device_modify->sys_image_guid);
  1306. for (i = 0; i < dd->num_pports; i++) {
  1307. struct hfi1_ibport *ibp = &dd->pport[i].ibport_data;
  1308. hfi1_sys_guid_chg(ibp);
  1309. }
  1310. }
  1311. ret = 0;
  1312. bail:
  1313. return ret;
  1314. }
  1315. static int shut_down_port(struct rvt_dev_info *rdi, u8 port_num)
  1316. {
  1317. struct hfi1_ibdev *verbs_dev = dev_from_rdi(rdi);
  1318. struct hfi1_devdata *dd = dd_from_dev(verbs_dev);
  1319. struct hfi1_pportdata *ppd = &dd->pport[port_num - 1];
  1320. int ret;
  1321. set_link_down_reason(ppd, OPA_LINKDOWN_REASON_UNKNOWN, 0,
  1322. OPA_LINKDOWN_REASON_UNKNOWN);
  1323. ret = set_link_state(ppd, HLS_DN_DOWNDEF);
  1324. return ret;
  1325. }
  1326. static int hfi1_get_guid_be(struct rvt_dev_info *rdi, struct rvt_ibport *rvp,
  1327. int guid_index, __be64 *guid)
  1328. {
  1329. struct hfi1_ibport *ibp = container_of(rvp, struct hfi1_ibport, rvp);
  1330. if (guid_index >= HFI1_GUIDS_PER_PORT)
  1331. return -EINVAL;
  1332. *guid = get_sguid(ibp, guid_index);
  1333. return 0;
  1334. }
  1335. /*
  1336. * convert ah port,sl to sc
  1337. */
  1338. u8 ah_to_sc(struct ib_device *ibdev, struct ib_ah_attr *ah)
  1339. {
  1340. struct hfi1_ibport *ibp = to_iport(ibdev, ah->port_num);
  1341. return ibp->sl_to_sc[ah->sl];
  1342. }
  1343. static int hfi1_check_ah(struct ib_device *ibdev, struct ib_ah_attr *ah_attr)
  1344. {
  1345. struct hfi1_ibport *ibp;
  1346. struct hfi1_pportdata *ppd;
  1347. struct hfi1_devdata *dd;
  1348. u8 sc5;
  1349. /* test the mapping for validity */
  1350. ibp = to_iport(ibdev, ah_attr->port_num);
  1351. ppd = ppd_from_ibp(ibp);
  1352. sc5 = ibp->sl_to_sc[ah_attr->sl];
  1353. dd = dd_from_ppd(ppd);
  1354. if (sc_to_vlt(dd, sc5) > num_vls && sc_to_vlt(dd, sc5) != 0xf)
  1355. return -EINVAL;
  1356. return 0;
  1357. }
  1358. static void hfi1_notify_new_ah(struct ib_device *ibdev,
  1359. struct ib_ah_attr *ah_attr,
  1360. struct rvt_ah *ah)
  1361. {
  1362. struct hfi1_ibport *ibp;
  1363. struct hfi1_pportdata *ppd;
  1364. struct hfi1_devdata *dd;
  1365. u8 sc5;
  1366. /*
  1367. * Do not trust reading anything from rvt_ah at this point as it is not
  1368. * done being setup. We can however modify things which we need to set.
  1369. */
  1370. ibp = to_iport(ibdev, ah_attr->port_num);
  1371. ppd = ppd_from_ibp(ibp);
  1372. sc5 = ibp->sl_to_sc[ah->attr.sl];
  1373. dd = dd_from_ppd(ppd);
  1374. ah->vl = sc_to_vlt(dd, sc5);
  1375. if (ah->vl < num_vls || ah->vl == 15)
  1376. ah->log_pmtu = ilog2(dd->vld[ah->vl].mtu);
  1377. }
  1378. struct ib_ah *hfi1_create_qp0_ah(struct hfi1_ibport *ibp, u16 dlid)
  1379. {
  1380. struct ib_ah_attr attr;
  1381. struct ib_ah *ah = ERR_PTR(-EINVAL);
  1382. struct rvt_qp *qp0;
  1383. memset(&attr, 0, sizeof(attr));
  1384. attr.dlid = dlid;
  1385. attr.port_num = ppd_from_ibp(ibp)->port;
  1386. rcu_read_lock();
  1387. qp0 = rcu_dereference(ibp->rvp.qp[0]);
  1388. if (qp0)
  1389. ah = ib_create_ah(qp0->ibqp.pd, &attr);
  1390. rcu_read_unlock();
  1391. return ah;
  1392. }
  1393. /**
  1394. * hfi1_get_npkeys - return the size of the PKEY table for context 0
  1395. * @dd: the hfi1_ib device
  1396. */
  1397. unsigned hfi1_get_npkeys(struct hfi1_devdata *dd)
  1398. {
  1399. return ARRAY_SIZE(dd->pport[0].pkeys);
  1400. }
  1401. static void init_ibport(struct hfi1_pportdata *ppd)
  1402. {
  1403. struct hfi1_ibport *ibp = &ppd->ibport_data;
  1404. size_t sz = ARRAY_SIZE(ibp->sl_to_sc);
  1405. int i;
  1406. for (i = 0; i < sz; i++) {
  1407. ibp->sl_to_sc[i] = i;
  1408. ibp->sc_to_sl[i] = i;
  1409. }
  1410. spin_lock_init(&ibp->rvp.lock);
  1411. /* Set the prefix to the default value (see ch. 4.1.1) */
  1412. ibp->rvp.gid_prefix = IB_DEFAULT_GID_PREFIX;
  1413. ibp->rvp.sm_lid = 0;
  1414. /* Below should only set bits defined in OPA PortInfo.CapabilityMask */
  1415. ibp->rvp.port_cap_flags = IB_PORT_AUTO_MIGR_SUP |
  1416. IB_PORT_CAP_MASK_NOTICE_SUP;
  1417. ibp->rvp.pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
  1418. ibp->rvp.pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
  1419. ibp->rvp.pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
  1420. ibp->rvp.pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
  1421. ibp->rvp.pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
  1422. RCU_INIT_POINTER(ibp->rvp.qp[0], NULL);
  1423. RCU_INIT_POINTER(ibp->rvp.qp[1], NULL);
  1424. }
  1425. static void hfi1_get_dev_fw_str(struct ib_device *ibdev, char *str,
  1426. size_t str_len)
  1427. {
  1428. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  1429. struct hfi1_ibdev *dev = dev_from_rdi(rdi);
  1430. u16 ver = dd_from_dev(dev)->dc8051_ver;
  1431. snprintf(str, str_len, "%u.%u", dc8051_ver_maj(ver),
  1432. dc8051_ver_min(ver));
  1433. }
  1434. static const char * const driver_cntr_names[] = {
  1435. /* must be element 0*/
  1436. "DRIVER_KernIntr",
  1437. "DRIVER_ErrorIntr",
  1438. "DRIVER_Tx_Errs",
  1439. "DRIVER_Rcv_Errs",
  1440. "DRIVER_HW_Errs",
  1441. "DRIVER_NoPIOBufs",
  1442. "DRIVER_CtxtsOpen",
  1443. "DRIVER_RcvLen_Errs",
  1444. "DRIVER_EgrBufFull",
  1445. "DRIVER_EgrHdrFull"
  1446. };
  1447. static const char **dev_cntr_names;
  1448. static const char **port_cntr_names;
  1449. static int num_driver_cntrs = ARRAY_SIZE(driver_cntr_names);
  1450. static int num_dev_cntrs;
  1451. static int num_port_cntrs;
  1452. static int cntr_names_initialized;
  1453. /*
  1454. * Convert a list of names separated by '\n' into an array of NULL terminated
  1455. * strings. Optionally some entries can be reserved in the array to hold extra
  1456. * external strings.
  1457. */
  1458. static int init_cntr_names(const char *names_in,
  1459. const int names_len,
  1460. int num_extra_names,
  1461. int *num_cntrs,
  1462. const char ***cntr_names)
  1463. {
  1464. char *names_out, *p, **q;
  1465. int i, n;
  1466. n = 0;
  1467. for (i = 0; i < names_len; i++)
  1468. if (names_in[i] == '\n')
  1469. n++;
  1470. names_out = kmalloc((n + num_extra_names) * sizeof(char *) + names_len,
  1471. GFP_KERNEL);
  1472. if (!names_out) {
  1473. *num_cntrs = 0;
  1474. *cntr_names = NULL;
  1475. return -ENOMEM;
  1476. }
  1477. p = names_out + (n + num_extra_names) * sizeof(char *);
  1478. memcpy(p, names_in, names_len);
  1479. q = (char **)names_out;
  1480. for (i = 0; i < n; i++) {
  1481. q[i] = p;
  1482. p = strchr(p, '\n');
  1483. *p++ = '\0';
  1484. }
  1485. *num_cntrs = n;
  1486. *cntr_names = (const char **)names_out;
  1487. return 0;
  1488. }
  1489. static struct rdma_hw_stats *alloc_hw_stats(struct ib_device *ibdev,
  1490. u8 port_num)
  1491. {
  1492. int i, err;
  1493. if (!cntr_names_initialized) {
  1494. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  1495. err = init_cntr_names(dd->cntrnames,
  1496. dd->cntrnameslen,
  1497. num_driver_cntrs,
  1498. &num_dev_cntrs,
  1499. &dev_cntr_names);
  1500. if (err)
  1501. return NULL;
  1502. for (i = 0; i < num_driver_cntrs; i++)
  1503. dev_cntr_names[num_dev_cntrs + i] =
  1504. driver_cntr_names[i];
  1505. err = init_cntr_names(dd->portcntrnames,
  1506. dd->portcntrnameslen,
  1507. 0,
  1508. &num_port_cntrs,
  1509. &port_cntr_names);
  1510. if (err) {
  1511. kfree(dev_cntr_names);
  1512. dev_cntr_names = NULL;
  1513. return NULL;
  1514. }
  1515. cntr_names_initialized = 1;
  1516. }
  1517. if (!port_num)
  1518. return rdma_alloc_hw_stats_struct(
  1519. dev_cntr_names,
  1520. num_dev_cntrs + num_driver_cntrs,
  1521. RDMA_HW_STATS_DEFAULT_LIFESPAN);
  1522. else
  1523. return rdma_alloc_hw_stats_struct(
  1524. port_cntr_names,
  1525. num_port_cntrs,
  1526. RDMA_HW_STATS_DEFAULT_LIFESPAN);
  1527. }
  1528. static u64 hfi1_sps_ints(void)
  1529. {
  1530. unsigned long flags;
  1531. struct hfi1_devdata *dd;
  1532. u64 sps_ints = 0;
  1533. spin_lock_irqsave(&hfi1_devs_lock, flags);
  1534. list_for_each_entry(dd, &hfi1_dev_list, list) {
  1535. sps_ints += get_all_cpu_total(dd->int_counter);
  1536. }
  1537. spin_unlock_irqrestore(&hfi1_devs_lock, flags);
  1538. return sps_ints;
  1539. }
  1540. static int get_hw_stats(struct ib_device *ibdev, struct rdma_hw_stats *stats,
  1541. u8 port, int index)
  1542. {
  1543. u64 *values;
  1544. int count;
  1545. if (!port) {
  1546. u64 *stats = (u64 *)&hfi1_stats;
  1547. int i;
  1548. hfi1_read_cntrs(dd_from_ibdev(ibdev), NULL, &values);
  1549. values[num_dev_cntrs] = hfi1_sps_ints();
  1550. for (i = 1; i < num_driver_cntrs; i++)
  1551. values[num_dev_cntrs + i] = stats[i];
  1552. count = num_dev_cntrs + num_driver_cntrs;
  1553. } else {
  1554. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  1555. hfi1_read_portcntrs(ppd_from_ibp(ibp), NULL, &values);
  1556. count = num_port_cntrs;
  1557. }
  1558. memcpy(stats->value, values, count * sizeof(u64));
  1559. return count;
  1560. }
  1561. /**
  1562. * hfi1_register_ib_device - register our device with the infiniband core
  1563. * @dd: the device data structure
  1564. * Return 0 if successful, errno if unsuccessful.
  1565. */
  1566. int hfi1_register_ib_device(struct hfi1_devdata *dd)
  1567. {
  1568. struct hfi1_ibdev *dev = &dd->verbs_dev;
  1569. struct ib_device *ibdev = &dev->rdi.ibdev;
  1570. struct hfi1_pportdata *ppd = dd->pport;
  1571. struct hfi1_ibport *ibp = &ppd->ibport_data;
  1572. unsigned i;
  1573. int ret;
  1574. size_t lcpysz = IB_DEVICE_NAME_MAX;
  1575. for (i = 0; i < dd->num_pports; i++)
  1576. init_ibport(ppd + i);
  1577. /* Only need to initialize non-zero fields. */
  1578. setup_timer(&dev->mem_timer, mem_timer, (unsigned long)dev);
  1579. seqlock_init(&dev->iowait_lock);
  1580. seqlock_init(&dev->txwait_lock);
  1581. INIT_LIST_HEAD(&dev->txwait);
  1582. INIT_LIST_HEAD(&dev->memwait);
  1583. ret = verbs_txreq_init(dev);
  1584. if (ret)
  1585. goto err_verbs_txreq;
  1586. /* Use first-port GUID as node guid */
  1587. ibdev->node_guid = get_sguid(ibp, HFI1_PORT_GUID_INDEX);
  1588. /*
  1589. * The system image GUID is supposed to be the same for all
  1590. * HFIs in a single system but since there can be other
  1591. * device types in the system, we can't be sure this is unique.
  1592. */
  1593. if (!ib_hfi1_sys_image_guid)
  1594. ib_hfi1_sys_image_guid = ibdev->node_guid;
  1595. lcpysz = strlcpy(ibdev->name, class_name(), lcpysz);
  1596. strlcpy(ibdev->name + lcpysz, "_%d", IB_DEVICE_NAME_MAX - lcpysz);
  1597. ibdev->owner = THIS_MODULE;
  1598. ibdev->phys_port_cnt = dd->num_pports;
  1599. ibdev->dma_device = &dd->pcidev->dev;
  1600. ibdev->modify_device = modify_device;
  1601. ibdev->alloc_hw_stats = alloc_hw_stats;
  1602. ibdev->get_hw_stats = get_hw_stats;
  1603. /* keep process mad in the driver */
  1604. ibdev->process_mad = hfi1_process_mad;
  1605. ibdev->get_dev_fw_str = hfi1_get_dev_fw_str;
  1606. strncpy(ibdev->node_desc, init_utsname()->nodename,
  1607. sizeof(ibdev->node_desc));
  1608. /*
  1609. * Fill in rvt info object.
  1610. */
  1611. dd->verbs_dev.rdi.driver_f.port_callback = hfi1_create_port_files;
  1612. dd->verbs_dev.rdi.driver_f.get_card_name = get_card_name;
  1613. dd->verbs_dev.rdi.driver_f.get_pci_dev = get_pci_dev;
  1614. dd->verbs_dev.rdi.driver_f.check_ah = hfi1_check_ah;
  1615. dd->verbs_dev.rdi.driver_f.notify_new_ah = hfi1_notify_new_ah;
  1616. dd->verbs_dev.rdi.driver_f.get_guid_be = hfi1_get_guid_be;
  1617. dd->verbs_dev.rdi.driver_f.query_port_state = query_port;
  1618. dd->verbs_dev.rdi.driver_f.shut_down_port = shut_down_port;
  1619. dd->verbs_dev.rdi.driver_f.cap_mask_chg = hfi1_cap_mask_chg;
  1620. /*
  1621. * Fill in rvt info device attributes.
  1622. */
  1623. hfi1_fill_device_attr(dd);
  1624. /* queue pair */
  1625. dd->verbs_dev.rdi.dparms.qp_table_size = hfi1_qp_table_size;
  1626. dd->verbs_dev.rdi.dparms.qpn_start = 0;
  1627. dd->verbs_dev.rdi.dparms.qpn_inc = 1;
  1628. dd->verbs_dev.rdi.dparms.qos_shift = dd->qos_shift;
  1629. dd->verbs_dev.rdi.dparms.qpn_res_start = kdeth_qp << 16;
  1630. dd->verbs_dev.rdi.dparms.qpn_res_end =
  1631. dd->verbs_dev.rdi.dparms.qpn_res_start + 65535;
  1632. dd->verbs_dev.rdi.dparms.max_rdma_atomic = HFI1_MAX_RDMA_ATOMIC;
  1633. dd->verbs_dev.rdi.dparms.psn_mask = PSN_MASK;
  1634. dd->verbs_dev.rdi.dparms.psn_shift = PSN_SHIFT;
  1635. dd->verbs_dev.rdi.dparms.psn_modify_mask = PSN_MODIFY_MASK;
  1636. dd->verbs_dev.rdi.dparms.core_cap_flags = RDMA_CORE_PORT_INTEL_OPA;
  1637. dd->verbs_dev.rdi.dparms.max_mad_size = OPA_MGMT_MAD_SIZE;
  1638. dd->verbs_dev.rdi.driver_f.qp_priv_alloc = qp_priv_alloc;
  1639. dd->verbs_dev.rdi.driver_f.qp_priv_free = qp_priv_free;
  1640. dd->verbs_dev.rdi.driver_f.free_all_qps = free_all_qps;
  1641. dd->verbs_dev.rdi.driver_f.notify_qp_reset = notify_qp_reset;
  1642. dd->verbs_dev.rdi.driver_f.do_send = hfi1_do_send;
  1643. dd->verbs_dev.rdi.driver_f.schedule_send = hfi1_schedule_send;
  1644. dd->verbs_dev.rdi.driver_f.schedule_send_no_lock = _hfi1_schedule_send;
  1645. dd->verbs_dev.rdi.driver_f.get_pmtu_from_attr = get_pmtu_from_attr;
  1646. dd->verbs_dev.rdi.driver_f.notify_error_qp = notify_error_qp;
  1647. dd->verbs_dev.rdi.driver_f.flush_qp_waiters = flush_qp_waiters;
  1648. dd->verbs_dev.rdi.driver_f.stop_send_queue = stop_send_queue;
  1649. dd->verbs_dev.rdi.driver_f.quiesce_qp = quiesce_qp;
  1650. dd->verbs_dev.rdi.driver_f.notify_error_qp = notify_error_qp;
  1651. dd->verbs_dev.rdi.driver_f.mtu_from_qp = mtu_from_qp;
  1652. dd->verbs_dev.rdi.driver_f.mtu_to_path_mtu = mtu_to_path_mtu;
  1653. dd->verbs_dev.rdi.driver_f.check_modify_qp = hfi1_check_modify_qp;
  1654. dd->verbs_dev.rdi.driver_f.modify_qp = hfi1_modify_qp;
  1655. dd->verbs_dev.rdi.driver_f.check_send_wqe = hfi1_check_send_wqe;
  1656. /* completeion queue */
  1657. snprintf(dd->verbs_dev.rdi.dparms.cq_name,
  1658. sizeof(dd->verbs_dev.rdi.dparms.cq_name),
  1659. "hfi1_cq%d", dd->unit);
  1660. dd->verbs_dev.rdi.dparms.node = dd->node;
  1661. /* misc settings */
  1662. dd->verbs_dev.rdi.flags = 0; /* Let rdmavt handle it all */
  1663. dd->verbs_dev.rdi.dparms.lkey_table_size = hfi1_lkey_table_size;
  1664. dd->verbs_dev.rdi.dparms.nports = dd->num_pports;
  1665. dd->verbs_dev.rdi.dparms.npkeys = hfi1_get_npkeys(dd);
  1666. /* post send table */
  1667. dd->verbs_dev.rdi.post_parms = hfi1_post_parms;
  1668. ppd = dd->pport;
  1669. for (i = 0; i < dd->num_pports; i++, ppd++)
  1670. rvt_init_port(&dd->verbs_dev.rdi,
  1671. &ppd->ibport_data.rvp,
  1672. i,
  1673. ppd->pkeys);
  1674. ret = rvt_register_device(&dd->verbs_dev.rdi);
  1675. if (ret)
  1676. goto err_verbs_txreq;
  1677. ret = hfi1_verbs_register_sysfs(dd);
  1678. if (ret)
  1679. goto err_class;
  1680. return ret;
  1681. err_class:
  1682. rvt_unregister_device(&dd->verbs_dev.rdi);
  1683. err_verbs_txreq:
  1684. verbs_txreq_exit(dev);
  1685. dd_dev_err(dd, "cannot register verbs: %d!\n", -ret);
  1686. return ret;
  1687. }
  1688. void hfi1_unregister_ib_device(struct hfi1_devdata *dd)
  1689. {
  1690. struct hfi1_ibdev *dev = &dd->verbs_dev;
  1691. hfi1_verbs_unregister_sysfs(dd);
  1692. rvt_unregister_device(&dd->verbs_dev.rdi);
  1693. if (!list_empty(&dev->txwait))
  1694. dd_dev_err(dd, "txwait list not empty!\n");
  1695. if (!list_empty(&dev->memwait))
  1696. dd_dev_err(dd, "memwait list not empty!\n");
  1697. del_timer_sync(&dev->mem_timer);
  1698. verbs_txreq_exit(dev);
  1699. kfree(dev_cntr_names);
  1700. kfree(port_cntr_names);
  1701. cntr_names_initialized = 0;
  1702. }
  1703. void hfi1_cnp_rcv(struct hfi1_packet *packet)
  1704. {
  1705. struct hfi1_ibport *ibp = &packet->rcd->ppd->ibport_data;
  1706. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  1707. struct ib_header *hdr = packet->hdr;
  1708. struct rvt_qp *qp = packet->qp;
  1709. u32 lqpn, rqpn = 0;
  1710. u16 rlid = 0;
  1711. u8 sl, sc5, svc_type;
  1712. switch (packet->qp->ibqp.qp_type) {
  1713. case IB_QPT_UC:
  1714. rlid = qp->remote_ah_attr.dlid;
  1715. rqpn = qp->remote_qpn;
  1716. svc_type = IB_CC_SVCTYPE_UC;
  1717. break;
  1718. case IB_QPT_RC:
  1719. rlid = qp->remote_ah_attr.dlid;
  1720. rqpn = qp->remote_qpn;
  1721. svc_type = IB_CC_SVCTYPE_RC;
  1722. break;
  1723. case IB_QPT_SMI:
  1724. case IB_QPT_GSI:
  1725. case IB_QPT_UD:
  1726. svc_type = IB_CC_SVCTYPE_UD;
  1727. break;
  1728. default:
  1729. ibp->rvp.n_pkt_drops++;
  1730. return;
  1731. }
  1732. sc5 = hdr2sc(hdr, packet->rhf);
  1733. sl = ibp->sc_to_sl[sc5];
  1734. lqpn = qp->ibqp.qp_num;
  1735. process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
  1736. }