verbs.c 51 KB

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