verbs.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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 bool 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. * @release: boolean to release MR
  376. * @copy_last: do a separate copy of the last 8 bytes
  377. */
  378. void hfi1_copy_sge(
  379. struct rvt_sge_state *ss,
  380. void *data, u32 length,
  381. bool release,
  382. bool copy_last)
  383. {
  384. struct rvt_sge *sge = &ss->sge;
  385. int i;
  386. bool in_last = false;
  387. bool cacheless_copy = false;
  388. if (sge_copy_mode == COPY_CACHELESS) {
  389. cacheless_copy = length >= PAGE_SIZE;
  390. } else if (sge_copy_mode == COPY_ADAPTIVE) {
  391. if (length >= PAGE_SIZE) {
  392. /*
  393. * NOTE: this *assumes*:
  394. * o The first vaddr is the dest.
  395. * o If multiple pages, then vaddr is sequential.
  396. */
  397. wss_insert(sge->vaddr);
  398. if (length >= (2 * PAGE_SIZE))
  399. wss_insert(sge->vaddr + PAGE_SIZE);
  400. cacheless_copy = wss_exceeds_threshold();
  401. } else {
  402. wss_advance_clean_counter();
  403. }
  404. }
  405. if (copy_last) {
  406. if (length > 8) {
  407. length -= 8;
  408. } else {
  409. copy_last = false;
  410. in_last = true;
  411. }
  412. }
  413. again:
  414. while (length) {
  415. u32 len = rvt_get_sge_length(sge, length);
  416. WARN_ON_ONCE(len == 0);
  417. if (unlikely(in_last)) {
  418. /* enforce byte transfer ordering */
  419. for (i = 0; i < len; i++)
  420. ((u8 *)sge->vaddr)[i] = ((u8 *)data)[i];
  421. } else if (cacheless_copy) {
  422. cacheless_memcpy(sge->vaddr, data, len);
  423. } else {
  424. memcpy(sge->vaddr, data, len);
  425. }
  426. rvt_update_sge(ss, len, release);
  427. data += len;
  428. length -= len;
  429. }
  430. if (copy_last) {
  431. copy_last = false;
  432. in_last = true;
  433. length = 8;
  434. goto again;
  435. }
  436. }
  437. /*
  438. * Make sure the QP is ready and able to accept the given opcode.
  439. */
  440. static inline opcode_handler qp_ok(int opcode, struct hfi1_packet *packet)
  441. {
  442. if (!(ib_rvt_state_ops[packet->qp->state] & RVT_PROCESS_RECV_OK))
  443. return NULL;
  444. if (((opcode & RVT_OPCODE_QP_MASK) == packet->qp->allowed_ops) ||
  445. (opcode == IB_OPCODE_CNP))
  446. return opcode_handler_tbl[opcode];
  447. return NULL;
  448. }
  449. /**
  450. * hfi1_ib_rcv - process an incoming packet
  451. * @packet: data packet information
  452. *
  453. * This is called to process an incoming packet at interrupt level.
  454. *
  455. * Tlen is the length of the header + data + CRC in bytes.
  456. */
  457. void hfi1_ib_rcv(struct hfi1_packet *packet)
  458. {
  459. struct hfi1_ctxtdata *rcd = packet->rcd;
  460. struct ib_header *hdr = packet->hdr;
  461. u32 tlen = packet->tlen;
  462. struct hfi1_pportdata *ppd = rcd->ppd;
  463. struct hfi1_ibport *ibp = rcd_to_iport(rcd);
  464. struct rvt_dev_info *rdi = &ppd->dd->verbs_dev.rdi;
  465. opcode_handler packet_handler;
  466. unsigned long flags;
  467. u32 qp_num;
  468. int lnh;
  469. u8 opcode;
  470. u16 lid;
  471. /* Check for GRH */
  472. lnh = be16_to_cpu(hdr->lrh[0]) & 3;
  473. if (lnh == HFI1_LRH_BTH) {
  474. packet->ohdr = &hdr->u.oth;
  475. } else if (lnh == HFI1_LRH_GRH) {
  476. u32 vtf;
  477. packet->ohdr = &hdr->u.l.oth;
  478. if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
  479. goto drop;
  480. vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
  481. if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
  482. goto drop;
  483. packet->rcv_flags |= HFI1_HAS_GRH;
  484. } else {
  485. goto drop;
  486. }
  487. trace_input_ibhdr(rcd->dd, hdr);
  488. opcode = (be32_to_cpu(packet->ohdr->bth[0]) >> 24);
  489. inc_opstats(tlen, &rcd->opstats->stats[opcode]);
  490. /* Get the destination QP number. */
  491. qp_num = be32_to_cpu(packet->ohdr->bth[1]) & RVT_QPN_MASK;
  492. lid = be16_to_cpu(hdr->lrh[1]);
  493. if (unlikely((lid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
  494. (lid != be16_to_cpu(IB_LID_PERMISSIVE)))) {
  495. struct rvt_mcast *mcast;
  496. struct rvt_mcast_qp *p;
  497. if (lnh != HFI1_LRH_GRH)
  498. goto drop;
  499. mcast = rvt_mcast_find(&ibp->rvp, &hdr->u.l.grh.dgid);
  500. if (!mcast)
  501. goto drop;
  502. list_for_each_entry_rcu(p, &mcast->qp_list, list) {
  503. packet->qp = p->qp;
  504. spin_lock_irqsave(&packet->qp->r_lock, flags);
  505. packet_handler = qp_ok(opcode, packet);
  506. if (likely(packet_handler))
  507. packet_handler(packet);
  508. else
  509. ibp->rvp.n_pkt_drops++;
  510. spin_unlock_irqrestore(&packet->qp->r_lock, flags);
  511. }
  512. /*
  513. * Notify rvt_multicast_detach() if it is waiting for us
  514. * to finish.
  515. */
  516. if (atomic_dec_return(&mcast->refcount) <= 1)
  517. wake_up(&mcast->wait);
  518. } else {
  519. rcu_read_lock();
  520. packet->qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
  521. if (!packet->qp) {
  522. rcu_read_unlock();
  523. goto drop;
  524. }
  525. spin_lock_irqsave(&packet->qp->r_lock, flags);
  526. packet_handler = qp_ok(opcode, packet);
  527. if (likely(packet_handler))
  528. packet_handler(packet);
  529. else
  530. ibp->rvp.n_pkt_drops++;
  531. spin_unlock_irqrestore(&packet->qp->r_lock, flags);
  532. rcu_read_unlock();
  533. }
  534. return;
  535. drop:
  536. ibp->rvp.n_pkt_drops++;
  537. }
  538. /*
  539. * This is called from a timer to check for QPs
  540. * which need kernel memory in order to send a packet.
  541. */
  542. static void mem_timer(unsigned long data)
  543. {
  544. struct hfi1_ibdev *dev = (struct hfi1_ibdev *)data;
  545. struct list_head *list = &dev->memwait;
  546. struct rvt_qp *qp = NULL;
  547. struct iowait *wait;
  548. unsigned long flags;
  549. struct hfi1_qp_priv *priv;
  550. write_seqlock_irqsave(&dev->iowait_lock, flags);
  551. if (!list_empty(list)) {
  552. wait = list_first_entry(list, struct iowait, list);
  553. qp = iowait_to_qp(wait);
  554. priv = qp->priv;
  555. list_del_init(&priv->s_iowait.list);
  556. priv->s_iowait.lock = NULL;
  557. /* refcount held until actual wake up */
  558. if (!list_empty(list))
  559. mod_timer(&dev->mem_timer, jiffies + 1);
  560. }
  561. write_sequnlock_irqrestore(&dev->iowait_lock, flags);
  562. if (qp)
  563. hfi1_qp_wakeup(qp, RVT_S_WAIT_KMEM);
  564. }
  565. /*
  566. * This is called with progress side lock held.
  567. */
  568. /* New API */
  569. static void verbs_sdma_complete(
  570. struct sdma_txreq *cookie,
  571. int status)
  572. {
  573. struct verbs_txreq *tx =
  574. container_of(cookie, struct verbs_txreq, txreq);
  575. struct rvt_qp *qp = tx->qp;
  576. spin_lock(&qp->s_lock);
  577. if (tx->wqe) {
  578. hfi1_send_complete(qp, tx->wqe, IB_WC_SUCCESS);
  579. } else if (qp->ibqp.qp_type == IB_QPT_RC) {
  580. struct ib_header *hdr;
  581. hdr = &tx->phdr.hdr;
  582. hfi1_rc_send_complete(qp, hdr);
  583. }
  584. spin_unlock(&qp->s_lock);
  585. hfi1_put_txreq(tx);
  586. }
  587. static int wait_kmem(struct hfi1_ibdev *dev,
  588. struct rvt_qp *qp,
  589. struct hfi1_pkt_state *ps)
  590. {
  591. struct hfi1_qp_priv *priv = qp->priv;
  592. unsigned long flags;
  593. int ret = 0;
  594. spin_lock_irqsave(&qp->s_lock, flags);
  595. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  596. write_seqlock(&dev->iowait_lock);
  597. list_add_tail(&ps->s_txreq->txreq.list,
  598. &priv->s_iowait.tx_head);
  599. if (list_empty(&priv->s_iowait.list)) {
  600. if (list_empty(&dev->memwait))
  601. mod_timer(&dev->mem_timer, jiffies + 1);
  602. qp->s_flags |= RVT_S_WAIT_KMEM;
  603. list_add_tail(&priv->s_iowait.list, &dev->memwait);
  604. priv->s_iowait.lock = &dev->iowait_lock;
  605. trace_hfi1_qpsleep(qp, RVT_S_WAIT_KMEM);
  606. rvt_get_qp(qp);
  607. }
  608. write_sequnlock(&dev->iowait_lock);
  609. qp->s_flags &= ~RVT_S_BUSY;
  610. ret = -EBUSY;
  611. }
  612. spin_unlock_irqrestore(&qp->s_lock, flags);
  613. return ret;
  614. }
  615. /*
  616. * This routine calls txadds for each sg entry.
  617. *
  618. * Add failures will revert the sge cursor
  619. */
  620. static noinline int build_verbs_ulp_payload(
  621. struct sdma_engine *sde,
  622. u32 length,
  623. struct verbs_txreq *tx)
  624. {
  625. struct rvt_sge_state *ss = tx->ss;
  626. struct rvt_sge *sg_list = ss->sg_list;
  627. struct rvt_sge sge = ss->sge;
  628. u8 num_sge = ss->num_sge;
  629. u32 len;
  630. int ret = 0;
  631. while (length) {
  632. len = ss->sge.length;
  633. if (len > length)
  634. len = length;
  635. if (len > ss->sge.sge_length)
  636. len = ss->sge.sge_length;
  637. WARN_ON_ONCE(len == 0);
  638. ret = sdma_txadd_kvaddr(
  639. sde->dd,
  640. &tx->txreq,
  641. ss->sge.vaddr,
  642. len);
  643. if (ret)
  644. goto bail_txadd;
  645. rvt_update_sge(ss, len, false);
  646. length -= len;
  647. }
  648. return ret;
  649. bail_txadd:
  650. /* unwind cursor */
  651. ss->sge = sge;
  652. ss->num_sge = num_sge;
  653. ss->sg_list = sg_list;
  654. return ret;
  655. }
  656. /*
  657. * Build the number of DMA descriptors needed to send length bytes of data.
  658. *
  659. * NOTE: DMA mapping is held in the tx until completed in the ring or
  660. * the tx desc is freed without having been submitted to the ring
  661. *
  662. * This routine ensures all the helper routine calls succeed.
  663. */
  664. /* New API */
  665. static int build_verbs_tx_desc(
  666. struct sdma_engine *sde,
  667. u32 length,
  668. struct verbs_txreq *tx,
  669. struct hfi1_ahg_info *ahg_info,
  670. u64 pbc)
  671. {
  672. int ret = 0;
  673. struct hfi1_sdma_header *phdr = &tx->phdr;
  674. u16 hdrbytes = tx->hdr_dwords << 2;
  675. if (!ahg_info->ahgcount) {
  676. ret = sdma_txinit_ahg(
  677. &tx->txreq,
  678. ahg_info->tx_flags,
  679. hdrbytes + length,
  680. ahg_info->ahgidx,
  681. 0,
  682. NULL,
  683. 0,
  684. verbs_sdma_complete);
  685. if (ret)
  686. goto bail_txadd;
  687. phdr->pbc = cpu_to_le64(pbc);
  688. ret = sdma_txadd_kvaddr(
  689. sde->dd,
  690. &tx->txreq,
  691. phdr,
  692. hdrbytes);
  693. if (ret)
  694. goto bail_txadd;
  695. } else {
  696. ret = sdma_txinit_ahg(
  697. &tx->txreq,
  698. ahg_info->tx_flags,
  699. length,
  700. ahg_info->ahgidx,
  701. ahg_info->ahgcount,
  702. ahg_info->ahgdesc,
  703. hdrbytes,
  704. verbs_sdma_complete);
  705. if (ret)
  706. goto bail_txadd;
  707. }
  708. /* add the ulp payload - if any. tx->ss can be NULL for acks */
  709. if (tx->ss)
  710. ret = build_verbs_ulp_payload(sde, length, tx);
  711. bail_txadd:
  712. return ret;
  713. }
  714. int hfi1_verbs_send_dma(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
  715. u64 pbc)
  716. {
  717. struct hfi1_qp_priv *priv = qp->priv;
  718. struct hfi1_ahg_info *ahg_info = priv->s_ahg;
  719. u32 hdrwords = qp->s_hdrwords;
  720. u32 len = ps->s_txreq->s_cur_size;
  721. u32 plen = hdrwords + ((len + 3) >> 2) + 2; /* includes pbc */
  722. struct hfi1_ibdev *dev = ps->dev;
  723. struct hfi1_pportdata *ppd = ps->ppd;
  724. struct verbs_txreq *tx;
  725. u64 pbc_flags = 0;
  726. u8 sc5 = priv->s_sc;
  727. int ret;
  728. tx = ps->s_txreq;
  729. if (!sdma_txreq_built(&tx->txreq)) {
  730. if (likely(pbc == 0)) {
  731. u32 vl = sc_to_vlt(dd_from_ibdev(qp->ibqp.device), sc5);
  732. /* No vl15 here */
  733. /* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */
  734. pbc_flags |= (!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT;
  735. pbc = create_pbc(ppd,
  736. pbc_flags,
  737. qp->srate_mbps,
  738. vl,
  739. plen);
  740. }
  741. tx->wqe = qp->s_wqe;
  742. ret = build_verbs_tx_desc(tx->sde, len, tx, ahg_info, pbc);
  743. if (unlikely(ret))
  744. goto bail_build;
  745. }
  746. ret = sdma_send_txreq(tx->sde, &priv->s_iowait, &tx->txreq);
  747. if (unlikely(ret < 0)) {
  748. if (ret == -ECOMM)
  749. goto bail_ecomm;
  750. return ret;
  751. }
  752. trace_sdma_output_ibhdr(dd_from_ibdev(qp->ibqp.device),
  753. &ps->s_txreq->phdr.hdr);
  754. return ret;
  755. bail_ecomm:
  756. /* The current one got "sent" */
  757. return 0;
  758. bail_build:
  759. ret = wait_kmem(dev, qp, ps);
  760. if (!ret) {
  761. /* free txreq - bad state */
  762. hfi1_put_txreq(ps->s_txreq);
  763. ps->s_txreq = NULL;
  764. }
  765. return ret;
  766. }
  767. /*
  768. * If we are now in the error state, return zero to flush the
  769. * send work request.
  770. */
  771. static int pio_wait(struct rvt_qp *qp,
  772. struct send_context *sc,
  773. struct hfi1_pkt_state *ps,
  774. u32 flag)
  775. {
  776. struct hfi1_qp_priv *priv = qp->priv;
  777. struct hfi1_devdata *dd = sc->dd;
  778. struct hfi1_ibdev *dev = &dd->verbs_dev;
  779. unsigned long flags;
  780. int ret = 0;
  781. /*
  782. * Note that as soon as want_buffer() is called and
  783. * possibly before it returns, sc_piobufavail()
  784. * could be called. Therefore, put QP on the I/O wait list before
  785. * enabling the PIO avail interrupt.
  786. */
  787. spin_lock_irqsave(&qp->s_lock, flags);
  788. if (ib_rvt_state_ops[qp->state] & RVT_PROCESS_RECV_OK) {
  789. write_seqlock(&dev->iowait_lock);
  790. list_add_tail(&ps->s_txreq->txreq.list,
  791. &priv->s_iowait.tx_head);
  792. if (list_empty(&priv->s_iowait.list)) {
  793. struct hfi1_ibdev *dev = &dd->verbs_dev;
  794. int was_empty;
  795. dev->n_piowait += !!(flag & RVT_S_WAIT_PIO);
  796. dev->n_piodrain += !!(flag & RVT_S_WAIT_PIO_DRAIN);
  797. qp->s_flags |= flag;
  798. was_empty = list_empty(&sc->piowait);
  799. list_add_tail(&priv->s_iowait.list, &sc->piowait);
  800. priv->s_iowait.lock = &dev->iowait_lock;
  801. trace_hfi1_qpsleep(qp, RVT_S_WAIT_PIO);
  802. rvt_get_qp(qp);
  803. /* counting: only call wantpiobuf_intr if first user */
  804. if (was_empty)
  805. hfi1_sc_wantpiobuf_intr(sc, 1);
  806. }
  807. write_sequnlock(&dev->iowait_lock);
  808. qp->s_flags &= ~RVT_S_BUSY;
  809. ret = -EBUSY;
  810. }
  811. spin_unlock_irqrestore(&qp->s_lock, flags);
  812. return ret;
  813. }
  814. static void verbs_pio_complete(void *arg, int code)
  815. {
  816. struct rvt_qp *qp = (struct rvt_qp *)arg;
  817. struct hfi1_qp_priv *priv = qp->priv;
  818. if (iowait_pio_dec(&priv->s_iowait))
  819. iowait_drain_wakeup(&priv->s_iowait);
  820. }
  821. int hfi1_verbs_send_pio(struct rvt_qp *qp, struct hfi1_pkt_state *ps,
  822. u64 pbc)
  823. {
  824. struct hfi1_qp_priv *priv = qp->priv;
  825. u32 hdrwords = qp->s_hdrwords;
  826. struct rvt_sge_state *ss = ps->s_txreq->ss;
  827. u32 len = ps->s_txreq->s_cur_size;
  828. u32 dwords = (len + 3) >> 2;
  829. u32 plen = hdrwords + dwords + 2; /* includes pbc */
  830. struct hfi1_pportdata *ppd = ps->ppd;
  831. u32 *hdr = (u32 *)&ps->s_txreq->phdr.hdr;
  832. u64 pbc_flags = 0;
  833. u8 sc5;
  834. unsigned long flags = 0;
  835. struct send_context *sc;
  836. struct pio_buf *pbuf;
  837. int wc_status = IB_WC_SUCCESS;
  838. int ret = 0;
  839. pio_release_cb cb = NULL;
  840. /* only RC/UC use complete */
  841. switch (qp->ibqp.qp_type) {
  842. case IB_QPT_RC:
  843. case IB_QPT_UC:
  844. cb = verbs_pio_complete;
  845. break;
  846. default:
  847. break;
  848. }
  849. /* vl15 special case taken care of in ud.c */
  850. sc5 = priv->s_sc;
  851. sc = ps->s_txreq->psc;
  852. if (likely(pbc == 0)) {
  853. u8 vl = sc_to_vlt(dd_from_ibdev(qp->ibqp.device), sc5);
  854. /* set PBC_DC_INFO bit (aka SC[4]) in pbc_flags */
  855. pbc_flags |= (!!(sc5 & 0x10)) << PBC_DC_INFO_SHIFT;
  856. pbc = create_pbc(ppd, pbc_flags, qp->srate_mbps, vl, plen);
  857. }
  858. if (cb)
  859. iowait_pio_inc(&priv->s_iowait);
  860. pbuf = sc_buffer_alloc(sc, plen, cb, qp);
  861. if (unlikely(!pbuf)) {
  862. if (cb)
  863. verbs_pio_complete(qp, 0);
  864. if (ppd->host_link_state != HLS_UP_ACTIVE) {
  865. /*
  866. * If we have filled the PIO buffers to capacity and are
  867. * not in an active state this request is not going to
  868. * go out to so just complete it with an error or else a
  869. * ULP or the core may be stuck waiting.
  870. */
  871. hfi1_cdbg(
  872. PIO,
  873. "alloc failed. state not active, completing");
  874. wc_status = IB_WC_GENERAL_ERR;
  875. goto pio_bail;
  876. } else {
  877. /*
  878. * This is a normal occurrence. The PIO buffs are full
  879. * up but we are still happily sending, well we could be
  880. * so lets continue to queue the request.
  881. */
  882. hfi1_cdbg(PIO, "alloc failed. state active, queuing");
  883. ret = pio_wait(qp, sc, ps, RVT_S_WAIT_PIO);
  884. if (!ret)
  885. /* txreq not queued - free */
  886. goto bail;
  887. /* tx consumed in wait */
  888. return ret;
  889. }
  890. }
  891. if (len == 0) {
  892. pio_copy(ppd->dd, pbuf, pbc, hdr, hdrwords);
  893. } else {
  894. if (ss) {
  895. seg_pio_copy_start(pbuf, pbc, hdr, hdrwords * 4);
  896. while (len) {
  897. void *addr = ss->sge.vaddr;
  898. u32 slen = ss->sge.length;
  899. if (slen > len)
  900. slen = len;
  901. rvt_update_sge(ss, slen, false);
  902. seg_pio_copy_mid(pbuf, addr, slen);
  903. len -= slen;
  904. }
  905. seg_pio_copy_end(pbuf);
  906. }
  907. }
  908. trace_pio_output_ibhdr(dd_from_ibdev(qp->ibqp.device),
  909. &ps->s_txreq->phdr.hdr);
  910. pio_bail:
  911. if (qp->s_wqe) {
  912. spin_lock_irqsave(&qp->s_lock, flags);
  913. hfi1_send_complete(qp, qp->s_wqe, wc_status);
  914. spin_unlock_irqrestore(&qp->s_lock, flags);
  915. } else if (qp->ibqp.qp_type == IB_QPT_RC) {
  916. spin_lock_irqsave(&qp->s_lock, flags);
  917. hfi1_rc_send_complete(qp, &ps->s_txreq->phdr.hdr);
  918. spin_unlock_irqrestore(&qp->s_lock, flags);
  919. }
  920. ret = 0;
  921. bail:
  922. hfi1_put_txreq(ps->s_txreq);
  923. return ret;
  924. }
  925. /*
  926. * egress_pkey_matches_entry - return 1 if the pkey matches ent (ent
  927. * being an entry from the partition key table), return 0
  928. * otherwise. Use the matching criteria for egress partition keys
  929. * specified in the OPAv1 spec., section 9.1l.7.
  930. */
  931. static inline int egress_pkey_matches_entry(u16 pkey, u16 ent)
  932. {
  933. u16 mkey = pkey & PKEY_LOW_15_MASK;
  934. u16 mentry = ent & PKEY_LOW_15_MASK;
  935. if (mkey == mentry) {
  936. /*
  937. * If pkey[15] is set (full partition member),
  938. * is bit 15 in the corresponding table element
  939. * clear (limited member)?
  940. */
  941. if (pkey & PKEY_MEMBER_MASK)
  942. return !!(ent & PKEY_MEMBER_MASK);
  943. return 1;
  944. }
  945. return 0;
  946. }
  947. /**
  948. * egress_pkey_check - check P_KEY of a packet
  949. * @ppd: Physical IB port data
  950. * @lrh: Local route header
  951. * @bth: Base transport header
  952. * @sc5: SC for packet
  953. * @s_pkey_index: It will be used for look up optimization for kernel contexts
  954. * only. If it is negative value, then it means user contexts is calling this
  955. * function.
  956. *
  957. * It checks if hdr's pkey is valid.
  958. *
  959. * Return: 0 on success, otherwise, 1
  960. */
  961. int egress_pkey_check(struct hfi1_pportdata *ppd, __be16 *lrh, __be32 *bth,
  962. u8 sc5, int8_t s_pkey_index)
  963. {
  964. struct hfi1_devdata *dd;
  965. int i;
  966. u16 pkey;
  967. int is_user_ctxt_mechanism = (s_pkey_index < 0);
  968. if (!(ppd->part_enforce & HFI1_PART_ENFORCE_OUT))
  969. return 0;
  970. pkey = (u16)be32_to_cpu(bth[0]);
  971. /* If SC15, pkey[0:14] must be 0x7fff */
  972. if ((sc5 == 0xf) && ((pkey & PKEY_LOW_15_MASK) != PKEY_LOW_15_MASK))
  973. goto bad;
  974. /* Is the pkey = 0x0, or 0x8000? */
  975. if ((pkey & PKEY_LOW_15_MASK) == 0)
  976. goto bad;
  977. /*
  978. * For the kernel contexts only, if a qp is passed into the function,
  979. * the most likely matching pkey has index qp->s_pkey_index
  980. */
  981. if (!is_user_ctxt_mechanism &&
  982. egress_pkey_matches_entry(pkey, ppd->pkeys[s_pkey_index])) {
  983. return 0;
  984. }
  985. for (i = 0; i < MAX_PKEY_VALUES; i++) {
  986. if (egress_pkey_matches_entry(pkey, ppd->pkeys[i]))
  987. return 0;
  988. }
  989. bad:
  990. /*
  991. * For the user-context mechanism, the P_KEY check would only happen
  992. * once per SDMA request, not once per packet. Therefore, there's no
  993. * need to increment the counter for the user-context mechanism.
  994. */
  995. if (!is_user_ctxt_mechanism) {
  996. incr_cntr64(&ppd->port_xmit_constraint_errors);
  997. dd = ppd->dd;
  998. if (!(dd->err_info_xmit_constraint.status &
  999. OPA_EI_STATUS_SMASK)) {
  1000. u16 slid = be16_to_cpu(lrh[3]);
  1001. dd->err_info_xmit_constraint.status |=
  1002. OPA_EI_STATUS_SMASK;
  1003. dd->err_info_xmit_constraint.slid = slid;
  1004. dd->err_info_xmit_constraint.pkey = pkey;
  1005. }
  1006. }
  1007. return 1;
  1008. }
  1009. /**
  1010. * get_send_routine - choose an egress routine
  1011. *
  1012. * Choose an egress routine based on QP type
  1013. * and size
  1014. */
  1015. static inline send_routine get_send_routine(struct rvt_qp *qp,
  1016. struct verbs_txreq *tx)
  1017. {
  1018. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  1019. struct hfi1_qp_priv *priv = qp->priv;
  1020. struct ib_header *h = &tx->phdr.hdr;
  1021. if (unlikely(!(dd->flags & HFI1_HAS_SEND_DMA)))
  1022. return dd->process_pio_send;
  1023. switch (qp->ibqp.qp_type) {
  1024. case IB_QPT_SMI:
  1025. return dd->process_pio_send;
  1026. case IB_QPT_GSI:
  1027. case IB_QPT_UD:
  1028. break;
  1029. case IB_QPT_UC:
  1030. case IB_QPT_RC: {
  1031. u8 op = get_opcode(h);
  1032. if (piothreshold &&
  1033. tx->s_cur_size <= min(piothreshold, qp->pmtu) &&
  1034. (BIT(op & OPMASK) & pio_opmask[op >> 5]) &&
  1035. iowait_sdma_pending(&priv->s_iowait) == 0 &&
  1036. !sdma_txreq_built(&tx->txreq))
  1037. return dd->process_pio_send;
  1038. break;
  1039. }
  1040. default:
  1041. break;
  1042. }
  1043. return dd->process_dma_send;
  1044. }
  1045. /**
  1046. * hfi1_verbs_send - send a packet
  1047. * @qp: the QP to send on
  1048. * @ps: the state of the packet to send
  1049. *
  1050. * Return zero if packet is sent or queued OK.
  1051. * Return non-zero and clear qp->s_flags RVT_S_BUSY otherwise.
  1052. */
  1053. int hfi1_verbs_send(struct rvt_qp *qp, struct hfi1_pkt_state *ps)
  1054. {
  1055. struct hfi1_devdata *dd = dd_from_ibdev(qp->ibqp.device);
  1056. struct hfi1_qp_priv *priv = qp->priv;
  1057. struct ib_other_headers *ohdr;
  1058. struct ib_header *hdr;
  1059. send_routine sr;
  1060. int ret;
  1061. u8 lnh;
  1062. hdr = &ps->s_txreq->phdr.hdr;
  1063. /* locate the pkey within the headers */
  1064. lnh = be16_to_cpu(hdr->lrh[0]) & 3;
  1065. if (lnh == HFI1_LRH_GRH)
  1066. ohdr = &hdr->u.l.oth;
  1067. else
  1068. ohdr = &hdr->u.oth;
  1069. sr = get_send_routine(qp, ps->s_txreq);
  1070. ret = egress_pkey_check(dd->pport,
  1071. hdr->lrh,
  1072. ohdr->bth,
  1073. priv->s_sc,
  1074. qp->s_pkey_index);
  1075. if (unlikely(ret)) {
  1076. /*
  1077. * The value we are returning here does not get propagated to
  1078. * the verbs caller. Thus we need to complete the request with
  1079. * error otherwise the caller could be sitting waiting on the
  1080. * completion event. Only do this for PIO. SDMA has its own
  1081. * mechanism for handling the errors. So for SDMA we can just
  1082. * return.
  1083. */
  1084. if (sr == dd->process_pio_send) {
  1085. unsigned long flags;
  1086. hfi1_cdbg(PIO, "%s() Failed. Completing with err",
  1087. __func__);
  1088. spin_lock_irqsave(&qp->s_lock, flags);
  1089. hfi1_send_complete(qp, qp->s_wqe, IB_WC_GENERAL_ERR);
  1090. spin_unlock_irqrestore(&qp->s_lock, flags);
  1091. }
  1092. return -EINVAL;
  1093. }
  1094. if (sr == dd->process_dma_send && iowait_pio_pending(&priv->s_iowait))
  1095. return pio_wait(qp,
  1096. ps->s_txreq->psc,
  1097. ps,
  1098. RVT_S_WAIT_PIO_DRAIN);
  1099. return sr(qp, ps, 0);
  1100. }
  1101. /**
  1102. * hfi1_fill_device_attr - Fill in rvt dev info device attributes.
  1103. * @dd: the device data structure
  1104. */
  1105. static void hfi1_fill_device_attr(struct hfi1_devdata *dd)
  1106. {
  1107. struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
  1108. u16 ver = dd->dc8051_ver;
  1109. memset(&rdi->dparms.props, 0, sizeof(rdi->dparms.props));
  1110. rdi->dparms.props.fw_ver = ((u64)(dc8051_ver_maj(ver)) << 16) |
  1111. (u64)dc8051_ver_min(ver);
  1112. rdi->dparms.props.device_cap_flags = IB_DEVICE_BAD_PKEY_CNTR |
  1113. IB_DEVICE_BAD_QKEY_CNTR | IB_DEVICE_SHUTDOWN_PORT |
  1114. IB_DEVICE_SYS_IMAGE_GUID | IB_DEVICE_RC_RNR_NAK_GEN |
  1115. IB_DEVICE_PORT_ACTIVE_EVENT | IB_DEVICE_SRQ_RESIZE |
  1116. IB_DEVICE_MEM_MGT_EXTENSIONS;
  1117. rdi->dparms.props.page_size_cap = PAGE_SIZE;
  1118. rdi->dparms.props.vendor_id = dd->oui1 << 16 | dd->oui2 << 8 | dd->oui3;
  1119. rdi->dparms.props.vendor_part_id = dd->pcidev->device;
  1120. rdi->dparms.props.hw_ver = dd->minrev;
  1121. rdi->dparms.props.sys_image_guid = ib_hfi1_sys_image_guid;
  1122. rdi->dparms.props.max_mr_size = U64_MAX;
  1123. rdi->dparms.props.max_fast_reg_page_list_len = UINT_MAX;
  1124. rdi->dparms.props.max_qp = hfi1_max_qps;
  1125. rdi->dparms.props.max_qp_wr = hfi1_max_qp_wrs;
  1126. rdi->dparms.props.max_sge = hfi1_max_sges;
  1127. rdi->dparms.props.max_sge_rd = hfi1_max_sges;
  1128. rdi->dparms.props.max_cq = hfi1_max_cqs;
  1129. rdi->dparms.props.max_ah = hfi1_max_ahs;
  1130. rdi->dparms.props.max_cqe = hfi1_max_cqes;
  1131. rdi->dparms.props.max_mr = rdi->lkey_table.max;
  1132. rdi->dparms.props.max_fmr = rdi->lkey_table.max;
  1133. rdi->dparms.props.max_map_per_fmr = 32767;
  1134. rdi->dparms.props.max_pd = hfi1_max_pds;
  1135. rdi->dparms.props.max_qp_rd_atom = HFI1_MAX_RDMA_ATOMIC;
  1136. rdi->dparms.props.max_qp_init_rd_atom = 255;
  1137. rdi->dparms.props.max_srq = hfi1_max_srqs;
  1138. rdi->dparms.props.max_srq_wr = hfi1_max_srq_wrs;
  1139. rdi->dparms.props.max_srq_sge = hfi1_max_srq_sges;
  1140. rdi->dparms.props.atomic_cap = IB_ATOMIC_GLOB;
  1141. rdi->dparms.props.max_pkeys = hfi1_get_npkeys(dd);
  1142. rdi->dparms.props.max_mcast_grp = hfi1_max_mcast_grps;
  1143. rdi->dparms.props.max_mcast_qp_attach = hfi1_max_mcast_qp_attached;
  1144. rdi->dparms.props.max_total_mcast_qp_attach =
  1145. rdi->dparms.props.max_mcast_qp_attach *
  1146. rdi->dparms.props.max_mcast_grp;
  1147. }
  1148. static inline u16 opa_speed_to_ib(u16 in)
  1149. {
  1150. u16 out = 0;
  1151. if (in & OPA_LINK_SPEED_25G)
  1152. out |= IB_SPEED_EDR;
  1153. if (in & OPA_LINK_SPEED_12_5G)
  1154. out |= IB_SPEED_FDR;
  1155. return out;
  1156. }
  1157. /*
  1158. * Convert a single OPA link width (no multiple flags) to an IB value.
  1159. * A zero OPA link width means link down, which means the IB width value
  1160. * is a don't care.
  1161. */
  1162. static inline u16 opa_width_to_ib(u16 in)
  1163. {
  1164. switch (in) {
  1165. case OPA_LINK_WIDTH_1X:
  1166. /* map 2x and 3x to 1x as they don't exist in IB */
  1167. case OPA_LINK_WIDTH_2X:
  1168. case OPA_LINK_WIDTH_3X:
  1169. return IB_WIDTH_1X;
  1170. default: /* link down or unknown, return our largest width */
  1171. case OPA_LINK_WIDTH_4X:
  1172. return IB_WIDTH_4X;
  1173. }
  1174. }
  1175. static int query_port(struct rvt_dev_info *rdi, u8 port_num,
  1176. struct ib_port_attr *props)
  1177. {
  1178. struct hfi1_ibdev *verbs_dev = dev_from_rdi(rdi);
  1179. struct hfi1_devdata *dd = dd_from_dev(verbs_dev);
  1180. struct hfi1_pportdata *ppd = &dd->pport[port_num - 1];
  1181. u16 lid = ppd->lid;
  1182. /* props being zeroed by the caller, avoid zeroing it here */
  1183. props->lid = lid ? lid : 0;
  1184. props->lmc = ppd->lmc;
  1185. /* OPA logical states match IB logical states */
  1186. props->state = driver_lstate(ppd);
  1187. props->phys_state = hfi1_ibphys_portstate(ppd);
  1188. props->gid_tbl_len = HFI1_GUIDS_PER_PORT;
  1189. props->active_width = (u8)opa_width_to_ib(ppd->link_width_active);
  1190. /* see rate_show() in ib core/sysfs.c */
  1191. props->active_speed = (u8)opa_speed_to_ib(ppd->link_speed_active);
  1192. props->max_vl_num = ppd->vls_supported;
  1193. /* Once we are a "first class" citizen and have added the OPA MTUs to
  1194. * the core we can advertise the larger MTU enum to the ULPs, for now
  1195. * advertise only 4K.
  1196. *
  1197. * Those applications which are either OPA aware or pass the MTU enum
  1198. * from the Path Records to us will get the new 8k MTU. Those that
  1199. * attempt to process the MTU enum may fail in various ways.
  1200. */
  1201. props->max_mtu = mtu_to_enum((!valid_ib_mtu(hfi1_max_mtu) ?
  1202. 4096 : hfi1_max_mtu), IB_MTU_4096);
  1203. props->active_mtu = !valid_ib_mtu(ppd->ibmtu) ? props->max_mtu :
  1204. mtu_to_enum(ppd->ibmtu, IB_MTU_2048);
  1205. return 0;
  1206. }
  1207. static int modify_device(struct ib_device *device,
  1208. int device_modify_mask,
  1209. struct ib_device_modify *device_modify)
  1210. {
  1211. struct hfi1_devdata *dd = dd_from_ibdev(device);
  1212. unsigned i;
  1213. int ret;
  1214. if (device_modify_mask & ~(IB_DEVICE_MODIFY_SYS_IMAGE_GUID |
  1215. IB_DEVICE_MODIFY_NODE_DESC)) {
  1216. ret = -EOPNOTSUPP;
  1217. goto bail;
  1218. }
  1219. if (device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC) {
  1220. memcpy(device->node_desc, device_modify->node_desc,
  1221. IB_DEVICE_NODE_DESC_MAX);
  1222. for (i = 0; i < dd->num_pports; i++) {
  1223. struct hfi1_ibport *ibp = &dd->pport[i].ibport_data;
  1224. hfi1_node_desc_chg(ibp);
  1225. }
  1226. }
  1227. if (device_modify_mask & IB_DEVICE_MODIFY_SYS_IMAGE_GUID) {
  1228. ib_hfi1_sys_image_guid =
  1229. cpu_to_be64(device_modify->sys_image_guid);
  1230. for (i = 0; i < dd->num_pports; i++) {
  1231. struct hfi1_ibport *ibp = &dd->pport[i].ibport_data;
  1232. hfi1_sys_guid_chg(ibp);
  1233. }
  1234. }
  1235. ret = 0;
  1236. bail:
  1237. return ret;
  1238. }
  1239. static int shut_down_port(struct rvt_dev_info *rdi, u8 port_num)
  1240. {
  1241. struct hfi1_ibdev *verbs_dev = dev_from_rdi(rdi);
  1242. struct hfi1_devdata *dd = dd_from_dev(verbs_dev);
  1243. struct hfi1_pportdata *ppd = &dd->pport[port_num - 1];
  1244. int ret;
  1245. set_link_down_reason(ppd, OPA_LINKDOWN_REASON_UNKNOWN, 0,
  1246. OPA_LINKDOWN_REASON_UNKNOWN);
  1247. ret = set_link_state(ppd, HLS_DN_DOWNDEF);
  1248. return ret;
  1249. }
  1250. static int hfi1_get_guid_be(struct rvt_dev_info *rdi, struct rvt_ibport *rvp,
  1251. int guid_index, __be64 *guid)
  1252. {
  1253. struct hfi1_ibport *ibp = container_of(rvp, struct hfi1_ibport, rvp);
  1254. if (guid_index >= HFI1_GUIDS_PER_PORT)
  1255. return -EINVAL;
  1256. *guid = get_sguid(ibp, guid_index);
  1257. return 0;
  1258. }
  1259. /*
  1260. * convert ah port,sl to sc
  1261. */
  1262. u8 ah_to_sc(struct ib_device *ibdev, struct ib_ah_attr *ah)
  1263. {
  1264. struct hfi1_ibport *ibp = to_iport(ibdev, ah->port_num);
  1265. return ibp->sl_to_sc[ah->sl];
  1266. }
  1267. static int hfi1_check_ah(struct ib_device *ibdev, struct ib_ah_attr *ah_attr)
  1268. {
  1269. struct hfi1_ibport *ibp;
  1270. struct hfi1_pportdata *ppd;
  1271. struct hfi1_devdata *dd;
  1272. u8 sc5;
  1273. /* test the mapping for validity */
  1274. ibp = to_iport(ibdev, ah_attr->port_num);
  1275. ppd = ppd_from_ibp(ibp);
  1276. sc5 = ibp->sl_to_sc[ah_attr->sl];
  1277. dd = dd_from_ppd(ppd);
  1278. if (sc_to_vlt(dd, sc5) > num_vls && sc_to_vlt(dd, sc5) != 0xf)
  1279. return -EINVAL;
  1280. return 0;
  1281. }
  1282. static void hfi1_notify_new_ah(struct ib_device *ibdev,
  1283. struct ib_ah_attr *ah_attr,
  1284. struct rvt_ah *ah)
  1285. {
  1286. struct hfi1_ibport *ibp;
  1287. struct hfi1_pportdata *ppd;
  1288. struct hfi1_devdata *dd;
  1289. u8 sc5;
  1290. /*
  1291. * Do not trust reading anything from rvt_ah at this point as it is not
  1292. * done being setup. We can however modify things which we need to set.
  1293. */
  1294. ibp = to_iport(ibdev, ah_attr->port_num);
  1295. ppd = ppd_from_ibp(ibp);
  1296. sc5 = ibp->sl_to_sc[ah->attr.sl];
  1297. dd = dd_from_ppd(ppd);
  1298. ah->vl = sc_to_vlt(dd, sc5);
  1299. if (ah->vl < num_vls || ah->vl == 15)
  1300. ah->log_pmtu = ilog2(dd->vld[ah->vl].mtu);
  1301. }
  1302. struct ib_ah *hfi1_create_qp0_ah(struct hfi1_ibport *ibp, u16 dlid)
  1303. {
  1304. struct ib_ah_attr attr;
  1305. struct ib_ah *ah = ERR_PTR(-EINVAL);
  1306. struct rvt_qp *qp0;
  1307. memset(&attr, 0, sizeof(attr));
  1308. attr.dlid = dlid;
  1309. attr.port_num = ppd_from_ibp(ibp)->port;
  1310. rcu_read_lock();
  1311. qp0 = rcu_dereference(ibp->rvp.qp[0]);
  1312. if (qp0)
  1313. ah = ib_create_ah(qp0->ibqp.pd, &attr);
  1314. rcu_read_unlock();
  1315. return ah;
  1316. }
  1317. /**
  1318. * hfi1_get_npkeys - return the size of the PKEY table for context 0
  1319. * @dd: the hfi1_ib device
  1320. */
  1321. unsigned hfi1_get_npkeys(struct hfi1_devdata *dd)
  1322. {
  1323. return ARRAY_SIZE(dd->pport[0].pkeys);
  1324. }
  1325. static void init_ibport(struct hfi1_pportdata *ppd)
  1326. {
  1327. struct hfi1_ibport *ibp = &ppd->ibport_data;
  1328. size_t sz = ARRAY_SIZE(ibp->sl_to_sc);
  1329. int i;
  1330. for (i = 0; i < sz; i++) {
  1331. ibp->sl_to_sc[i] = i;
  1332. ibp->sc_to_sl[i] = i;
  1333. }
  1334. spin_lock_init(&ibp->rvp.lock);
  1335. /* Set the prefix to the default value (see ch. 4.1.1) */
  1336. ibp->rvp.gid_prefix = IB_DEFAULT_GID_PREFIX;
  1337. ibp->rvp.sm_lid = 0;
  1338. /* Below should only set bits defined in OPA PortInfo.CapabilityMask */
  1339. ibp->rvp.port_cap_flags = IB_PORT_AUTO_MIGR_SUP |
  1340. IB_PORT_CAP_MASK_NOTICE_SUP;
  1341. ibp->rvp.pma_counter_select[0] = IB_PMA_PORT_XMIT_DATA;
  1342. ibp->rvp.pma_counter_select[1] = IB_PMA_PORT_RCV_DATA;
  1343. ibp->rvp.pma_counter_select[2] = IB_PMA_PORT_XMIT_PKTS;
  1344. ibp->rvp.pma_counter_select[3] = IB_PMA_PORT_RCV_PKTS;
  1345. ibp->rvp.pma_counter_select[4] = IB_PMA_PORT_XMIT_WAIT;
  1346. RCU_INIT_POINTER(ibp->rvp.qp[0], NULL);
  1347. RCU_INIT_POINTER(ibp->rvp.qp[1], NULL);
  1348. }
  1349. static void hfi1_get_dev_fw_str(struct ib_device *ibdev, char *str,
  1350. size_t str_len)
  1351. {
  1352. struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
  1353. struct hfi1_ibdev *dev = dev_from_rdi(rdi);
  1354. u16 ver = dd_from_dev(dev)->dc8051_ver;
  1355. snprintf(str, str_len, "%u.%u", dc8051_ver_maj(ver),
  1356. dc8051_ver_min(ver));
  1357. }
  1358. static const char * const driver_cntr_names[] = {
  1359. /* must be element 0*/
  1360. "DRIVER_KernIntr",
  1361. "DRIVER_ErrorIntr",
  1362. "DRIVER_Tx_Errs",
  1363. "DRIVER_Rcv_Errs",
  1364. "DRIVER_HW_Errs",
  1365. "DRIVER_NoPIOBufs",
  1366. "DRIVER_CtxtsOpen",
  1367. "DRIVER_RcvLen_Errs",
  1368. "DRIVER_EgrBufFull",
  1369. "DRIVER_EgrHdrFull"
  1370. };
  1371. static const char **dev_cntr_names;
  1372. static const char **port_cntr_names;
  1373. static int num_driver_cntrs = ARRAY_SIZE(driver_cntr_names);
  1374. static int num_dev_cntrs;
  1375. static int num_port_cntrs;
  1376. static int cntr_names_initialized;
  1377. /*
  1378. * Convert a list of names separated by '\n' into an array of NULL terminated
  1379. * strings. Optionally some entries can be reserved in the array to hold extra
  1380. * external strings.
  1381. */
  1382. static int init_cntr_names(const char *names_in,
  1383. const size_t names_len,
  1384. int num_extra_names,
  1385. int *num_cntrs,
  1386. const char ***cntr_names)
  1387. {
  1388. char *names_out, *p, **q;
  1389. int i, n;
  1390. n = 0;
  1391. for (i = 0; i < names_len; i++)
  1392. if (names_in[i] == '\n')
  1393. n++;
  1394. names_out = kmalloc((n + num_extra_names) * sizeof(char *) + names_len,
  1395. GFP_KERNEL);
  1396. if (!names_out) {
  1397. *num_cntrs = 0;
  1398. *cntr_names = NULL;
  1399. return -ENOMEM;
  1400. }
  1401. p = names_out + (n + num_extra_names) * sizeof(char *);
  1402. memcpy(p, names_in, names_len);
  1403. q = (char **)names_out;
  1404. for (i = 0; i < n; i++) {
  1405. q[i] = p;
  1406. p = strchr(p, '\n');
  1407. *p++ = '\0';
  1408. }
  1409. *num_cntrs = n;
  1410. *cntr_names = (const char **)names_out;
  1411. return 0;
  1412. }
  1413. static struct rdma_hw_stats *alloc_hw_stats(struct ib_device *ibdev,
  1414. u8 port_num)
  1415. {
  1416. int i, err;
  1417. if (!cntr_names_initialized) {
  1418. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  1419. err = init_cntr_names(dd->cntrnames,
  1420. dd->cntrnameslen,
  1421. num_driver_cntrs,
  1422. &num_dev_cntrs,
  1423. &dev_cntr_names);
  1424. if (err)
  1425. return NULL;
  1426. for (i = 0; i < num_driver_cntrs; i++)
  1427. dev_cntr_names[num_dev_cntrs + i] =
  1428. driver_cntr_names[i];
  1429. err = init_cntr_names(dd->portcntrnames,
  1430. dd->portcntrnameslen,
  1431. 0,
  1432. &num_port_cntrs,
  1433. &port_cntr_names);
  1434. if (err) {
  1435. kfree(dev_cntr_names);
  1436. dev_cntr_names = NULL;
  1437. return NULL;
  1438. }
  1439. cntr_names_initialized = 1;
  1440. }
  1441. if (!port_num)
  1442. return rdma_alloc_hw_stats_struct(
  1443. dev_cntr_names,
  1444. num_dev_cntrs + num_driver_cntrs,
  1445. RDMA_HW_STATS_DEFAULT_LIFESPAN);
  1446. else
  1447. return rdma_alloc_hw_stats_struct(
  1448. port_cntr_names,
  1449. num_port_cntrs,
  1450. RDMA_HW_STATS_DEFAULT_LIFESPAN);
  1451. }
  1452. static u64 hfi1_sps_ints(void)
  1453. {
  1454. unsigned long flags;
  1455. struct hfi1_devdata *dd;
  1456. u64 sps_ints = 0;
  1457. spin_lock_irqsave(&hfi1_devs_lock, flags);
  1458. list_for_each_entry(dd, &hfi1_dev_list, list) {
  1459. sps_ints += get_all_cpu_total(dd->int_counter);
  1460. }
  1461. spin_unlock_irqrestore(&hfi1_devs_lock, flags);
  1462. return sps_ints;
  1463. }
  1464. static int get_hw_stats(struct ib_device *ibdev, struct rdma_hw_stats *stats,
  1465. u8 port, int index)
  1466. {
  1467. u64 *values;
  1468. int count;
  1469. if (!port) {
  1470. u64 *stats = (u64 *)&hfi1_stats;
  1471. int i;
  1472. hfi1_read_cntrs(dd_from_ibdev(ibdev), NULL, &values);
  1473. values[num_dev_cntrs] = hfi1_sps_ints();
  1474. for (i = 1; i < num_driver_cntrs; i++)
  1475. values[num_dev_cntrs + i] = stats[i];
  1476. count = num_dev_cntrs + num_driver_cntrs;
  1477. } else {
  1478. struct hfi1_ibport *ibp = to_iport(ibdev, port);
  1479. hfi1_read_portcntrs(ppd_from_ibp(ibp), NULL, &values);
  1480. count = num_port_cntrs;
  1481. }
  1482. memcpy(stats->value, values, count * sizeof(u64));
  1483. return count;
  1484. }
  1485. /**
  1486. * hfi1_register_ib_device - register our device with the infiniband core
  1487. * @dd: the device data structure
  1488. * Return 0 if successful, errno if unsuccessful.
  1489. */
  1490. int hfi1_register_ib_device(struct hfi1_devdata *dd)
  1491. {
  1492. struct hfi1_ibdev *dev = &dd->verbs_dev;
  1493. struct ib_device *ibdev = &dev->rdi.ibdev;
  1494. struct hfi1_pportdata *ppd = dd->pport;
  1495. struct hfi1_ibport *ibp = &ppd->ibport_data;
  1496. unsigned i;
  1497. int ret;
  1498. size_t lcpysz = IB_DEVICE_NAME_MAX;
  1499. for (i = 0; i < dd->num_pports; i++)
  1500. init_ibport(ppd + i);
  1501. /* Only need to initialize non-zero fields. */
  1502. setup_timer(&dev->mem_timer, mem_timer, (unsigned long)dev);
  1503. seqlock_init(&dev->iowait_lock);
  1504. seqlock_init(&dev->txwait_lock);
  1505. INIT_LIST_HEAD(&dev->txwait);
  1506. INIT_LIST_HEAD(&dev->memwait);
  1507. ret = verbs_txreq_init(dev);
  1508. if (ret)
  1509. goto err_verbs_txreq;
  1510. /* Use first-port GUID as node guid */
  1511. ibdev->node_guid = get_sguid(ibp, HFI1_PORT_GUID_INDEX);
  1512. /*
  1513. * The system image GUID is supposed to be the same for all
  1514. * HFIs in a single system but since there can be other
  1515. * device types in the system, we can't be sure this is unique.
  1516. */
  1517. if (!ib_hfi1_sys_image_guid)
  1518. ib_hfi1_sys_image_guid = ibdev->node_guid;
  1519. lcpysz = strlcpy(ibdev->name, class_name(), lcpysz);
  1520. strlcpy(ibdev->name + lcpysz, "_%d", IB_DEVICE_NAME_MAX - lcpysz);
  1521. ibdev->owner = THIS_MODULE;
  1522. ibdev->phys_port_cnt = dd->num_pports;
  1523. ibdev->dev.parent = &dd->pcidev->dev;
  1524. ibdev->modify_device = modify_device;
  1525. ibdev->alloc_hw_stats = alloc_hw_stats;
  1526. ibdev->get_hw_stats = get_hw_stats;
  1527. /* keep process mad in the driver */
  1528. ibdev->process_mad = hfi1_process_mad;
  1529. ibdev->get_dev_fw_str = hfi1_get_dev_fw_str;
  1530. strncpy(ibdev->node_desc, init_utsname()->nodename,
  1531. sizeof(ibdev->node_desc));
  1532. /*
  1533. * Fill in rvt info object.
  1534. */
  1535. dd->verbs_dev.rdi.driver_f.port_callback = hfi1_create_port_files;
  1536. dd->verbs_dev.rdi.driver_f.get_card_name = get_card_name;
  1537. dd->verbs_dev.rdi.driver_f.get_pci_dev = get_pci_dev;
  1538. dd->verbs_dev.rdi.driver_f.check_ah = hfi1_check_ah;
  1539. dd->verbs_dev.rdi.driver_f.notify_new_ah = hfi1_notify_new_ah;
  1540. dd->verbs_dev.rdi.driver_f.get_guid_be = hfi1_get_guid_be;
  1541. dd->verbs_dev.rdi.driver_f.query_port_state = query_port;
  1542. dd->verbs_dev.rdi.driver_f.shut_down_port = shut_down_port;
  1543. dd->verbs_dev.rdi.driver_f.cap_mask_chg = hfi1_cap_mask_chg;
  1544. /*
  1545. * Fill in rvt info device attributes.
  1546. */
  1547. hfi1_fill_device_attr(dd);
  1548. /* queue pair */
  1549. dd->verbs_dev.rdi.dparms.qp_table_size = hfi1_qp_table_size;
  1550. dd->verbs_dev.rdi.dparms.qpn_start = 0;
  1551. dd->verbs_dev.rdi.dparms.qpn_inc = 1;
  1552. dd->verbs_dev.rdi.dparms.qos_shift = dd->qos_shift;
  1553. dd->verbs_dev.rdi.dparms.qpn_res_start = kdeth_qp << 16;
  1554. dd->verbs_dev.rdi.dparms.qpn_res_end =
  1555. dd->verbs_dev.rdi.dparms.qpn_res_start + 65535;
  1556. dd->verbs_dev.rdi.dparms.max_rdma_atomic = HFI1_MAX_RDMA_ATOMIC;
  1557. dd->verbs_dev.rdi.dparms.psn_mask = PSN_MASK;
  1558. dd->verbs_dev.rdi.dparms.psn_shift = PSN_SHIFT;
  1559. dd->verbs_dev.rdi.dparms.psn_modify_mask = PSN_MODIFY_MASK;
  1560. dd->verbs_dev.rdi.dparms.core_cap_flags = RDMA_CORE_PORT_INTEL_OPA;
  1561. dd->verbs_dev.rdi.dparms.max_mad_size = OPA_MGMT_MAD_SIZE;
  1562. dd->verbs_dev.rdi.driver_f.qp_priv_alloc = qp_priv_alloc;
  1563. dd->verbs_dev.rdi.driver_f.qp_priv_free = qp_priv_free;
  1564. dd->verbs_dev.rdi.driver_f.free_all_qps = free_all_qps;
  1565. dd->verbs_dev.rdi.driver_f.notify_qp_reset = notify_qp_reset;
  1566. dd->verbs_dev.rdi.driver_f.do_send = hfi1_do_send;
  1567. dd->verbs_dev.rdi.driver_f.schedule_send = hfi1_schedule_send;
  1568. dd->verbs_dev.rdi.driver_f.schedule_send_no_lock = _hfi1_schedule_send;
  1569. dd->verbs_dev.rdi.driver_f.get_pmtu_from_attr = get_pmtu_from_attr;
  1570. dd->verbs_dev.rdi.driver_f.notify_error_qp = notify_error_qp;
  1571. dd->verbs_dev.rdi.driver_f.flush_qp_waiters = flush_qp_waiters;
  1572. dd->verbs_dev.rdi.driver_f.stop_send_queue = stop_send_queue;
  1573. dd->verbs_dev.rdi.driver_f.quiesce_qp = quiesce_qp;
  1574. dd->verbs_dev.rdi.driver_f.notify_error_qp = notify_error_qp;
  1575. dd->verbs_dev.rdi.driver_f.mtu_from_qp = mtu_from_qp;
  1576. dd->verbs_dev.rdi.driver_f.mtu_to_path_mtu = mtu_to_path_mtu;
  1577. dd->verbs_dev.rdi.driver_f.check_modify_qp = hfi1_check_modify_qp;
  1578. dd->verbs_dev.rdi.driver_f.modify_qp = hfi1_modify_qp;
  1579. dd->verbs_dev.rdi.driver_f.notify_restart_rc = hfi1_restart_rc;
  1580. dd->verbs_dev.rdi.driver_f.check_send_wqe = hfi1_check_send_wqe;
  1581. /* completeion queue */
  1582. snprintf(dd->verbs_dev.rdi.dparms.cq_name,
  1583. sizeof(dd->verbs_dev.rdi.dparms.cq_name),
  1584. "hfi1_cq%d", dd->unit);
  1585. dd->verbs_dev.rdi.dparms.node = dd->node;
  1586. /* misc settings */
  1587. dd->verbs_dev.rdi.flags = 0; /* Let rdmavt handle it all */
  1588. dd->verbs_dev.rdi.dparms.lkey_table_size = hfi1_lkey_table_size;
  1589. dd->verbs_dev.rdi.dparms.nports = dd->num_pports;
  1590. dd->verbs_dev.rdi.dparms.npkeys = hfi1_get_npkeys(dd);
  1591. /* post send table */
  1592. dd->verbs_dev.rdi.post_parms = hfi1_post_parms;
  1593. ppd = dd->pport;
  1594. for (i = 0; i < dd->num_pports; i++, ppd++)
  1595. rvt_init_port(&dd->verbs_dev.rdi,
  1596. &ppd->ibport_data.rvp,
  1597. i,
  1598. ppd->pkeys);
  1599. ret = rvt_register_device(&dd->verbs_dev.rdi);
  1600. if (ret)
  1601. goto err_verbs_txreq;
  1602. ret = hfi1_verbs_register_sysfs(dd);
  1603. if (ret)
  1604. goto err_class;
  1605. return ret;
  1606. err_class:
  1607. rvt_unregister_device(&dd->verbs_dev.rdi);
  1608. err_verbs_txreq:
  1609. verbs_txreq_exit(dev);
  1610. dd_dev_err(dd, "cannot register verbs: %d!\n", -ret);
  1611. return ret;
  1612. }
  1613. void hfi1_unregister_ib_device(struct hfi1_devdata *dd)
  1614. {
  1615. struct hfi1_ibdev *dev = &dd->verbs_dev;
  1616. hfi1_verbs_unregister_sysfs(dd);
  1617. rvt_unregister_device(&dd->verbs_dev.rdi);
  1618. if (!list_empty(&dev->txwait))
  1619. dd_dev_err(dd, "txwait list not empty!\n");
  1620. if (!list_empty(&dev->memwait))
  1621. dd_dev_err(dd, "memwait list not empty!\n");
  1622. del_timer_sync(&dev->mem_timer);
  1623. verbs_txreq_exit(dev);
  1624. kfree(dev_cntr_names);
  1625. kfree(port_cntr_names);
  1626. cntr_names_initialized = 0;
  1627. }
  1628. void hfi1_cnp_rcv(struct hfi1_packet *packet)
  1629. {
  1630. struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
  1631. struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
  1632. struct ib_header *hdr = packet->hdr;
  1633. struct rvt_qp *qp = packet->qp;
  1634. u32 lqpn, rqpn = 0;
  1635. u16 rlid = 0;
  1636. u8 sl, sc5, svc_type;
  1637. switch (packet->qp->ibqp.qp_type) {
  1638. case IB_QPT_UC:
  1639. rlid = qp->remote_ah_attr.dlid;
  1640. rqpn = qp->remote_qpn;
  1641. svc_type = IB_CC_SVCTYPE_UC;
  1642. break;
  1643. case IB_QPT_RC:
  1644. rlid = qp->remote_ah_attr.dlid;
  1645. rqpn = qp->remote_qpn;
  1646. svc_type = IB_CC_SVCTYPE_RC;
  1647. break;
  1648. case IB_QPT_SMI:
  1649. case IB_QPT_GSI:
  1650. case IB_QPT_UD:
  1651. svc_type = IB_CC_SVCTYPE_UD;
  1652. break;
  1653. default:
  1654. ibp->rvp.n_pkt_drops++;
  1655. return;
  1656. }
  1657. sc5 = hdr2sc(hdr, packet->rhf);
  1658. sl = ibp->sc_to_sl[sc5];
  1659. lqpn = qp->ibqp.qp_num;
  1660. process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
  1661. }