verbs.c 52 KB

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