init.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  1. /*
  2. * Copyright(c) 2015 - 2018 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/pci.h>
  48. #include <linux/netdevice.h>
  49. #include <linux/vmalloc.h>
  50. #include <linux/delay.h>
  51. #include <linux/idr.h>
  52. #include <linux/module.h>
  53. #include <linux/printk.h>
  54. #include <linux/hrtimer.h>
  55. #include <linux/bitmap.h>
  56. #include <rdma/rdma_vt.h>
  57. #include "hfi.h"
  58. #include "device.h"
  59. #include "common.h"
  60. #include "trace.h"
  61. #include "mad.h"
  62. #include "sdma.h"
  63. #include "debugfs.h"
  64. #include "verbs.h"
  65. #include "aspm.h"
  66. #include "affinity.h"
  67. #include "vnic.h"
  68. #include "exp_rcv.h"
  69. #undef pr_fmt
  70. #define pr_fmt(fmt) DRIVER_NAME ": " fmt
  71. #define HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES 5
  72. /*
  73. * min buffers we want to have per context, after driver
  74. */
  75. #define HFI1_MIN_USER_CTXT_BUFCNT 7
  76. #define HFI1_MIN_HDRQ_EGRBUF_CNT 2
  77. #define HFI1_MAX_HDRQ_EGRBUF_CNT 16352
  78. #define HFI1_MIN_EAGER_BUFFER_SIZE (4 * 1024) /* 4KB */
  79. #define HFI1_MAX_EAGER_BUFFER_SIZE (256 * 1024) /* 256KB */
  80. #define NUM_IB_PORTS 1
  81. /*
  82. * Number of user receive contexts we are configured to use (to allow for more
  83. * pio buffers per ctxt, etc.) Zero means use one user context per CPU.
  84. */
  85. int num_user_contexts = -1;
  86. module_param_named(num_user_contexts, num_user_contexts, int, 0444);
  87. MODULE_PARM_DESC(
  88. num_user_contexts, "Set max number of user contexts to use (default: -1 will use the real (non-HT) CPU count)");
  89. uint krcvqs[RXE_NUM_DATA_VL];
  90. int krcvqsset;
  91. module_param_array(krcvqs, uint, &krcvqsset, S_IRUGO);
  92. MODULE_PARM_DESC(krcvqs, "Array of the number of non-control kernel receive queues by VL");
  93. /* computed based on above array */
  94. unsigned long n_krcvqs;
  95. static unsigned hfi1_rcvarr_split = 25;
  96. module_param_named(rcvarr_split, hfi1_rcvarr_split, uint, S_IRUGO);
  97. MODULE_PARM_DESC(rcvarr_split, "Percent of context's RcvArray entries used for Eager buffers");
  98. static uint eager_buffer_size = (8 << 20); /* 8MB */
  99. module_param(eager_buffer_size, uint, S_IRUGO);
  100. MODULE_PARM_DESC(eager_buffer_size, "Size of the eager buffers, default: 8MB");
  101. static uint rcvhdrcnt = 2048; /* 2x the max eager buffer count */
  102. module_param_named(rcvhdrcnt, rcvhdrcnt, uint, S_IRUGO);
  103. MODULE_PARM_DESC(rcvhdrcnt, "Receive header queue count (default 2048)");
  104. static uint hfi1_hdrq_entsize = 32;
  105. module_param_named(hdrq_entsize, hfi1_hdrq_entsize, uint, 0444);
  106. MODULE_PARM_DESC(hdrq_entsize, "Size of header queue entries: 2 - 8B, 16 - 64B, 32 - 128B (default)");
  107. unsigned int user_credit_return_threshold = 33; /* default is 33% */
  108. module_param(user_credit_return_threshold, uint, S_IRUGO);
  109. MODULE_PARM_DESC(user_credit_return_threshold, "Credit return threshold for user send contexts, return when unreturned credits passes this many blocks (in percent of allocated blocks, 0 is off)");
  110. static inline u64 encode_rcv_header_entry_size(u16 size);
  111. static struct idr hfi1_unit_table;
  112. static int hfi1_create_kctxt(struct hfi1_devdata *dd,
  113. struct hfi1_pportdata *ppd)
  114. {
  115. struct hfi1_ctxtdata *rcd;
  116. int ret;
  117. /* Control context has to be always 0 */
  118. BUILD_BUG_ON(HFI1_CTRL_CTXT != 0);
  119. ret = hfi1_create_ctxtdata(ppd, dd->node, &rcd);
  120. if (ret < 0) {
  121. dd_dev_err(dd, "Kernel receive context allocation failed\n");
  122. return ret;
  123. }
  124. /*
  125. * Set up the kernel context flags here and now because they use
  126. * default values for all receive side memories. User contexts will
  127. * be handled as they are created.
  128. */
  129. rcd->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) |
  130. HFI1_CAP_KGET(NODROP_RHQ_FULL) |
  131. HFI1_CAP_KGET(NODROP_EGR_FULL) |
  132. HFI1_CAP_KGET(DMA_RTAIL);
  133. /* Control context must use DMA_RTAIL */
  134. if (rcd->ctxt == HFI1_CTRL_CTXT)
  135. rcd->flags |= HFI1_CAP_DMA_RTAIL;
  136. rcd->seq_cnt = 1;
  137. rcd->sc = sc_alloc(dd, SC_ACK, rcd->rcvhdrqentsize, dd->node);
  138. if (!rcd->sc) {
  139. dd_dev_err(dd, "Kernel send context allocation failed\n");
  140. return -ENOMEM;
  141. }
  142. hfi1_init_ctxt(rcd->sc);
  143. return 0;
  144. }
  145. /*
  146. * Create the receive context array and one or more kernel contexts
  147. */
  148. int hfi1_create_kctxts(struct hfi1_devdata *dd)
  149. {
  150. u16 i;
  151. int ret;
  152. dd->rcd = kcalloc_node(dd->num_rcv_contexts, sizeof(*dd->rcd),
  153. GFP_KERNEL, dd->node);
  154. if (!dd->rcd)
  155. return -ENOMEM;
  156. for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
  157. ret = hfi1_create_kctxt(dd, dd->pport);
  158. if (ret)
  159. goto bail;
  160. }
  161. return 0;
  162. bail:
  163. for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i)
  164. hfi1_free_ctxt(dd->rcd[i]);
  165. /* All the contexts should be freed, free the array */
  166. kfree(dd->rcd);
  167. dd->rcd = NULL;
  168. return ret;
  169. }
  170. /*
  171. * Helper routines for the receive context reference count (rcd and uctxt).
  172. */
  173. static void hfi1_rcd_init(struct hfi1_ctxtdata *rcd)
  174. {
  175. kref_init(&rcd->kref);
  176. }
  177. /**
  178. * hfi1_rcd_free - When reference is zero clean up.
  179. * @kref: pointer to an initialized rcd data structure
  180. *
  181. */
  182. static void hfi1_rcd_free(struct kref *kref)
  183. {
  184. unsigned long flags;
  185. struct hfi1_ctxtdata *rcd =
  186. container_of(kref, struct hfi1_ctxtdata, kref);
  187. hfi1_free_ctxtdata(rcd->dd, rcd);
  188. spin_lock_irqsave(&rcd->dd->uctxt_lock, flags);
  189. rcd->dd->rcd[rcd->ctxt] = NULL;
  190. spin_unlock_irqrestore(&rcd->dd->uctxt_lock, flags);
  191. kfree(rcd);
  192. }
  193. /**
  194. * hfi1_rcd_put - decrement reference for rcd
  195. * @rcd: pointer to an initialized rcd data structure
  196. *
  197. * Use this to put a reference after the init.
  198. */
  199. int hfi1_rcd_put(struct hfi1_ctxtdata *rcd)
  200. {
  201. if (rcd)
  202. return kref_put(&rcd->kref, hfi1_rcd_free);
  203. return 0;
  204. }
  205. /**
  206. * hfi1_rcd_get - increment reference for rcd
  207. * @rcd: pointer to an initialized rcd data structure
  208. *
  209. * Use this to get a reference after the init.
  210. */
  211. void hfi1_rcd_get(struct hfi1_ctxtdata *rcd)
  212. {
  213. kref_get(&rcd->kref);
  214. }
  215. /**
  216. * allocate_rcd_index - allocate an rcd index from the rcd array
  217. * @dd: pointer to a valid devdata structure
  218. * @rcd: rcd data structure to assign
  219. * @index: pointer to index that is allocated
  220. *
  221. * Find an empty index in the rcd array, and assign the given rcd to it.
  222. * If the array is full, we are EBUSY.
  223. *
  224. */
  225. static int allocate_rcd_index(struct hfi1_devdata *dd,
  226. struct hfi1_ctxtdata *rcd, u16 *index)
  227. {
  228. unsigned long flags;
  229. u16 ctxt;
  230. spin_lock_irqsave(&dd->uctxt_lock, flags);
  231. for (ctxt = 0; ctxt < dd->num_rcv_contexts; ctxt++)
  232. if (!dd->rcd[ctxt])
  233. break;
  234. if (ctxt < dd->num_rcv_contexts) {
  235. rcd->ctxt = ctxt;
  236. dd->rcd[ctxt] = rcd;
  237. hfi1_rcd_init(rcd);
  238. }
  239. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  240. if (ctxt >= dd->num_rcv_contexts)
  241. return -EBUSY;
  242. *index = ctxt;
  243. return 0;
  244. }
  245. /**
  246. * hfi1_rcd_get_by_index_safe - validate the ctxt index before accessing the
  247. * array
  248. * @dd: pointer to a valid devdata structure
  249. * @ctxt: the index of an possilbe rcd
  250. *
  251. * This is a wrapper for hfi1_rcd_get_by_index() to validate that the given
  252. * ctxt index is valid.
  253. *
  254. * The caller is responsible for making the _put().
  255. *
  256. */
  257. struct hfi1_ctxtdata *hfi1_rcd_get_by_index_safe(struct hfi1_devdata *dd,
  258. u16 ctxt)
  259. {
  260. if (ctxt < dd->num_rcv_contexts)
  261. return hfi1_rcd_get_by_index(dd, ctxt);
  262. return NULL;
  263. }
  264. /**
  265. * hfi1_rcd_get_by_index
  266. * @dd: pointer to a valid devdata structure
  267. * @ctxt: the index of an possilbe rcd
  268. *
  269. * We need to protect access to the rcd array. If access is needed to
  270. * one or more index, get the protecting spinlock and then increment the
  271. * kref.
  272. *
  273. * The caller is responsible for making the _put().
  274. *
  275. */
  276. struct hfi1_ctxtdata *hfi1_rcd_get_by_index(struct hfi1_devdata *dd, u16 ctxt)
  277. {
  278. unsigned long flags;
  279. struct hfi1_ctxtdata *rcd = NULL;
  280. spin_lock_irqsave(&dd->uctxt_lock, flags);
  281. if (dd->rcd[ctxt]) {
  282. rcd = dd->rcd[ctxt];
  283. hfi1_rcd_get(rcd);
  284. }
  285. spin_unlock_irqrestore(&dd->uctxt_lock, flags);
  286. return rcd;
  287. }
  288. /*
  289. * Common code for user and kernel context create and setup.
  290. * NOTE: the initial kref is done here (hf1_rcd_init()).
  291. */
  292. int hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, int numa,
  293. struct hfi1_ctxtdata **context)
  294. {
  295. struct hfi1_devdata *dd = ppd->dd;
  296. struct hfi1_ctxtdata *rcd;
  297. unsigned kctxt_ngroups = 0;
  298. u32 base;
  299. if (dd->rcv_entries.nctxt_extra >
  300. dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt)
  301. kctxt_ngroups = (dd->rcv_entries.nctxt_extra -
  302. (dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt));
  303. rcd = kzalloc_node(sizeof(*rcd), GFP_KERNEL, numa);
  304. if (rcd) {
  305. u32 rcvtids, max_entries;
  306. u16 ctxt;
  307. int ret;
  308. ret = allocate_rcd_index(dd, rcd, &ctxt);
  309. if (ret) {
  310. *context = NULL;
  311. kfree(rcd);
  312. return ret;
  313. }
  314. INIT_LIST_HEAD(&rcd->qp_wait_list);
  315. hfi1_exp_tid_group_init(rcd);
  316. rcd->ppd = ppd;
  317. rcd->dd = dd;
  318. rcd->numa_id = numa;
  319. rcd->rcv_array_groups = dd->rcv_entries.ngroups;
  320. rcd->rhf_rcv_function_map = normal_rhf_rcv_functions;
  321. mutex_init(&rcd->exp_mutex);
  322. hfi1_cdbg(PROC, "setting up context %u\n", rcd->ctxt);
  323. /*
  324. * Calculate the context's RcvArray entry starting point.
  325. * We do this here because we have to take into account all
  326. * the RcvArray entries that previous context would have
  327. * taken and we have to account for any extra groups assigned
  328. * to the static (kernel) or dynamic (vnic/user) contexts.
  329. */
  330. if (ctxt < dd->first_dyn_alloc_ctxt) {
  331. if (ctxt < kctxt_ngroups) {
  332. base = ctxt * (dd->rcv_entries.ngroups + 1);
  333. rcd->rcv_array_groups++;
  334. } else {
  335. base = kctxt_ngroups +
  336. (ctxt * dd->rcv_entries.ngroups);
  337. }
  338. } else {
  339. u16 ct = ctxt - dd->first_dyn_alloc_ctxt;
  340. base = ((dd->n_krcv_queues * dd->rcv_entries.ngroups) +
  341. kctxt_ngroups);
  342. if (ct < dd->rcv_entries.nctxt_extra) {
  343. base += ct * (dd->rcv_entries.ngroups + 1);
  344. rcd->rcv_array_groups++;
  345. } else {
  346. base += dd->rcv_entries.nctxt_extra +
  347. (ct * dd->rcv_entries.ngroups);
  348. }
  349. }
  350. rcd->eager_base = base * dd->rcv_entries.group_size;
  351. rcd->rcvhdrq_cnt = rcvhdrcnt;
  352. rcd->rcvhdrqentsize = hfi1_hdrq_entsize;
  353. rcd->rhf_offset =
  354. rcd->rcvhdrqentsize - sizeof(u64) / sizeof(u32);
  355. /*
  356. * Simple Eager buffer allocation: we have already pre-allocated
  357. * the number of RcvArray entry groups. Each ctxtdata structure
  358. * holds the number of groups for that context.
  359. *
  360. * To follow CSR requirements and maintain cacheline alignment,
  361. * make sure all sizes and bases are multiples of group_size.
  362. *
  363. * The expected entry count is what is left after assigning
  364. * eager.
  365. */
  366. max_entries = rcd->rcv_array_groups *
  367. dd->rcv_entries.group_size;
  368. rcvtids = ((max_entries * hfi1_rcvarr_split) / 100);
  369. rcd->egrbufs.count = round_down(rcvtids,
  370. dd->rcv_entries.group_size);
  371. if (rcd->egrbufs.count > MAX_EAGER_ENTRIES) {
  372. dd_dev_err(dd, "ctxt%u: requested too many RcvArray entries.\n",
  373. rcd->ctxt);
  374. rcd->egrbufs.count = MAX_EAGER_ENTRIES;
  375. }
  376. hfi1_cdbg(PROC,
  377. "ctxt%u: max Eager buffer RcvArray entries: %u\n",
  378. rcd->ctxt, rcd->egrbufs.count);
  379. /*
  380. * Allocate array that will hold the eager buffer accounting
  381. * data.
  382. * This will allocate the maximum possible buffer count based
  383. * on the value of the RcvArray split parameter.
  384. * The resulting value will be rounded down to the closest
  385. * multiple of dd->rcv_entries.group_size.
  386. */
  387. rcd->egrbufs.buffers =
  388. kcalloc_node(rcd->egrbufs.count,
  389. sizeof(*rcd->egrbufs.buffers),
  390. GFP_KERNEL, numa);
  391. if (!rcd->egrbufs.buffers)
  392. goto bail;
  393. rcd->egrbufs.rcvtids =
  394. kcalloc_node(rcd->egrbufs.count,
  395. sizeof(*rcd->egrbufs.rcvtids),
  396. GFP_KERNEL, numa);
  397. if (!rcd->egrbufs.rcvtids)
  398. goto bail;
  399. rcd->egrbufs.size = eager_buffer_size;
  400. /*
  401. * The size of the buffers programmed into the RcvArray
  402. * entries needs to be big enough to handle the highest
  403. * MTU supported.
  404. */
  405. if (rcd->egrbufs.size < hfi1_max_mtu) {
  406. rcd->egrbufs.size = __roundup_pow_of_two(hfi1_max_mtu);
  407. hfi1_cdbg(PROC,
  408. "ctxt%u: eager bufs size too small. Adjusting to %zu\n",
  409. rcd->ctxt, rcd->egrbufs.size);
  410. }
  411. rcd->egrbufs.rcvtid_size = HFI1_MAX_EAGER_BUFFER_SIZE;
  412. /* Applicable only for statically created kernel contexts */
  413. if (ctxt < dd->first_dyn_alloc_ctxt) {
  414. rcd->opstats = kzalloc_node(sizeof(*rcd->opstats),
  415. GFP_KERNEL, numa);
  416. if (!rcd->opstats)
  417. goto bail;
  418. }
  419. *context = rcd;
  420. return 0;
  421. }
  422. bail:
  423. *context = NULL;
  424. hfi1_free_ctxt(rcd);
  425. return -ENOMEM;
  426. }
  427. /**
  428. * hfi1_free_ctxt
  429. * @rcd: pointer to an initialized rcd data structure
  430. *
  431. * This wrapper is the free function that matches hfi1_create_ctxtdata().
  432. * When a context is done being used (kernel or user), this function is called
  433. * for the "final" put to match the kref init from hf1i_create_ctxtdata().
  434. * Other users of the context do a get/put sequence to make sure that the
  435. * structure isn't removed while in use.
  436. */
  437. void hfi1_free_ctxt(struct hfi1_ctxtdata *rcd)
  438. {
  439. hfi1_rcd_put(rcd);
  440. }
  441. /*
  442. * Convert a receive header entry size that to the encoding used in the CSR.
  443. *
  444. * Return a zero if the given size is invalid.
  445. */
  446. static inline u64 encode_rcv_header_entry_size(u16 size)
  447. {
  448. /* there are only 3 valid receive header entry sizes */
  449. if (size == 2)
  450. return 1;
  451. if (size == 16)
  452. return 2;
  453. else if (size == 32)
  454. return 4;
  455. return 0; /* invalid */
  456. }
  457. /*
  458. * Select the largest ccti value over all SLs to determine the intra-
  459. * packet gap for the link.
  460. *
  461. * called with cca_timer_lock held (to protect access to cca_timer
  462. * array), and rcu_read_lock() (to protect access to cc_state).
  463. */
  464. void set_link_ipg(struct hfi1_pportdata *ppd)
  465. {
  466. struct hfi1_devdata *dd = ppd->dd;
  467. struct cc_state *cc_state;
  468. int i;
  469. u16 cce, ccti_limit, max_ccti = 0;
  470. u16 shift, mult;
  471. u64 src;
  472. u32 current_egress_rate; /* Mbits /sec */
  473. u32 max_pkt_time;
  474. /*
  475. * max_pkt_time is the maximum packet egress time in units
  476. * of the fabric clock period 1/(805 MHz).
  477. */
  478. cc_state = get_cc_state(ppd);
  479. if (!cc_state)
  480. /*
  481. * This should _never_ happen - rcu_read_lock() is held,
  482. * and set_link_ipg() should not be called if cc_state
  483. * is NULL.
  484. */
  485. return;
  486. for (i = 0; i < OPA_MAX_SLS; i++) {
  487. u16 ccti = ppd->cca_timer[i].ccti;
  488. if (ccti > max_ccti)
  489. max_ccti = ccti;
  490. }
  491. ccti_limit = cc_state->cct.ccti_limit;
  492. if (max_ccti > ccti_limit)
  493. max_ccti = ccti_limit;
  494. cce = cc_state->cct.entries[max_ccti].entry;
  495. shift = (cce & 0xc000) >> 14;
  496. mult = (cce & 0x3fff);
  497. current_egress_rate = active_egress_rate(ppd);
  498. max_pkt_time = egress_cycles(ppd->ibmaxlen, current_egress_rate);
  499. src = (max_pkt_time >> shift) * mult;
  500. src &= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SMASK;
  501. src <<= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SHIFT;
  502. write_csr(dd, SEND_STATIC_RATE_CONTROL, src);
  503. }
  504. static enum hrtimer_restart cca_timer_fn(struct hrtimer *t)
  505. {
  506. struct cca_timer *cca_timer;
  507. struct hfi1_pportdata *ppd;
  508. int sl;
  509. u16 ccti_timer, ccti_min;
  510. struct cc_state *cc_state;
  511. unsigned long flags;
  512. enum hrtimer_restart ret = HRTIMER_NORESTART;
  513. cca_timer = container_of(t, struct cca_timer, hrtimer);
  514. ppd = cca_timer->ppd;
  515. sl = cca_timer->sl;
  516. rcu_read_lock();
  517. cc_state = get_cc_state(ppd);
  518. if (!cc_state) {
  519. rcu_read_unlock();
  520. return HRTIMER_NORESTART;
  521. }
  522. /*
  523. * 1) decrement ccti for SL
  524. * 2) calculate IPG for link (set_link_ipg())
  525. * 3) restart timer, unless ccti is at min value
  526. */
  527. ccti_min = cc_state->cong_setting.entries[sl].ccti_min;
  528. ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
  529. spin_lock_irqsave(&ppd->cca_timer_lock, flags);
  530. if (cca_timer->ccti > ccti_min) {
  531. cca_timer->ccti--;
  532. set_link_ipg(ppd);
  533. }
  534. if (cca_timer->ccti > ccti_min) {
  535. unsigned long nsec = 1024 * ccti_timer;
  536. /* ccti_timer is in units of 1.024 usec */
  537. hrtimer_forward_now(t, ns_to_ktime(nsec));
  538. ret = HRTIMER_RESTART;
  539. }
  540. spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
  541. rcu_read_unlock();
  542. return ret;
  543. }
  544. /*
  545. * Common code for initializing the physical port structure.
  546. */
  547. void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd,
  548. struct hfi1_devdata *dd, u8 hw_pidx, u8 port)
  549. {
  550. int i;
  551. uint default_pkey_idx;
  552. struct cc_state *cc_state;
  553. ppd->dd = dd;
  554. ppd->hw_pidx = hw_pidx;
  555. ppd->port = port; /* IB port number, not index */
  556. ppd->prev_link_width = LINK_WIDTH_DEFAULT;
  557. /*
  558. * There are C_VL_COUNT number of PortVLXmitWait counters.
  559. * Adding 1 to C_VL_COUNT to include the PortXmitWait counter.
  560. */
  561. for (i = 0; i < C_VL_COUNT + 1; i++) {
  562. ppd->port_vl_xmit_wait_last[i] = 0;
  563. ppd->vl_xmit_flit_cnt[i] = 0;
  564. }
  565. default_pkey_idx = 1;
  566. ppd->pkeys[default_pkey_idx] = DEFAULT_P_KEY;
  567. ppd->part_enforce |= HFI1_PART_ENFORCE_IN;
  568. if (loopback) {
  569. dd_dev_err(dd, "Faking data partition 0x8001 in idx %u\n",
  570. !default_pkey_idx);
  571. ppd->pkeys[!default_pkey_idx] = 0x8001;
  572. }
  573. INIT_WORK(&ppd->link_vc_work, handle_verify_cap);
  574. INIT_WORK(&ppd->link_up_work, handle_link_up);
  575. INIT_WORK(&ppd->link_down_work, handle_link_down);
  576. INIT_WORK(&ppd->freeze_work, handle_freeze);
  577. INIT_WORK(&ppd->link_downgrade_work, handle_link_downgrade);
  578. INIT_WORK(&ppd->sma_message_work, handle_sma_message);
  579. INIT_WORK(&ppd->link_bounce_work, handle_link_bounce);
  580. INIT_DELAYED_WORK(&ppd->start_link_work, handle_start_link);
  581. INIT_WORK(&ppd->linkstate_active_work, receive_interrupt_work);
  582. INIT_WORK(&ppd->qsfp_info.qsfp_work, qsfp_event);
  583. mutex_init(&ppd->hls_lock);
  584. spin_lock_init(&ppd->qsfp_info.qsfp_lock);
  585. ppd->qsfp_info.ppd = ppd;
  586. ppd->sm_trap_qp = 0x0;
  587. ppd->sa_qp = 0x1;
  588. ppd->hfi1_wq = NULL;
  589. spin_lock_init(&ppd->cca_timer_lock);
  590. for (i = 0; i < OPA_MAX_SLS; i++) {
  591. hrtimer_init(&ppd->cca_timer[i].hrtimer, CLOCK_MONOTONIC,
  592. HRTIMER_MODE_REL);
  593. ppd->cca_timer[i].ppd = ppd;
  594. ppd->cca_timer[i].sl = i;
  595. ppd->cca_timer[i].ccti = 0;
  596. ppd->cca_timer[i].hrtimer.function = cca_timer_fn;
  597. }
  598. ppd->cc_max_table_entries = IB_CC_TABLE_CAP_DEFAULT;
  599. spin_lock_init(&ppd->cc_state_lock);
  600. spin_lock_init(&ppd->cc_log_lock);
  601. cc_state = kzalloc(sizeof(*cc_state), GFP_KERNEL);
  602. RCU_INIT_POINTER(ppd->cc_state, cc_state);
  603. if (!cc_state)
  604. goto bail;
  605. return;
  606. bail:
  607. dd_dev_err(dd, "Congestion Control Agent disabled for port %d\n", port);
  608. }
  609. /*
  610. * Do initialization for device that is only needed on
  611. * first detect, not on resets.
  612. */
  613. static int loadtime_init(struct hfi1_devdata *dd)
  614. {
  615. return 0;
  616. }
  617. /**
  618. * init_after_reset - re-initialize after a reset
  619. * @dd: the hfi1_ib device
  620. *
  621. * sanity check at least some of the values after reset, and
  622. * ensure no receive or transmit (explicitly, in case reset
  623. * failed
  624. */
  625. static int init_after_reset(struct hfi1_devdata *dd)
  626. {
  627. int i;
  628. struct hfi1_ctxtdata *rcd;
  629. /*
  630. * Ensure chip does no sends or receives, tail updates, or
  631. * pioavail updates while we re-initialize. This is mostly
  632. * for the driver data structures, not chip registers.
  633. */
  634. for (i = 0; i < dd->num_rcv_contexts; i++) {
  635. rcd = hfi1_rcd_get_by_index(dd, i);
  636. hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
  637. HFI1_RCVCTRL_INTRAVAIL_DIS |
  638. HFI1_RCVCTRL_TAILUPD_DIS, rcd);
  639. hfi1_rcd_put(rcd);
  640. }
  641. pio_send_control(dd, PSC_GLOBAL_DISABLE);
  642. for (i = 0; i < dd->num_send_contexts; i++)
  643. sc_disable(dd->send_contexts[i].sc);
  644. return 0;
  645. }
  646. static void enable_chip(struct hfi1_devdata *dd)
  647. {
  648. struct hfi1_ctxtdata *rcd;
  649. u32 rcvmask;
  650. u16 i;
  651. /* enable PIO send */
  652. pio_send_control(dd, PSC_GLOBAL_ENABLE);
  653. /*
  654. * Enable kernel ctxts' receive and receive interrupt.
  655. * Other ctxts done as user opens and initializes them.
  656. */
  657. for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
  658. rcd = hfi1_rcd_get_by_index(dd, i);
  659. if (!rcd)
  660. continue;
  661. rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB;
  662. rcvmask |= HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ?
  663. HFI1_RCVCTRL_TAILUPD_ENB : HFI1_RCVCTRL_TAILUPD_DIS;
  664. if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
  665. rcvmask |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
  666. if (HFI1_CAP_KGET_MASK(rcd->flags, NODROP_RHQ_FULL))
  667. rcvmask |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
  668. if (HFI1_CAP_KGET_MASK(rcd->flags, NODROP_EGR_FULL))
  669. rcvmask |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
  670. hfi1_rcvctrl(dd, rcvmask, rcd);
  671. sc_enable(rcd->sc);
  672. hfi1_rcd_put(rcd);
  673. }
  674. }
  675. /**
  676. * create_workqueues - create per port workqueues
  677. * @dd: the hfi1_ib device
  678. */
  679. static int create_workqueues(struct hfi1_devdata *dd)
  680. {
  681. int pidx;
  682. struct hfi1_pportdata *ppd;
  683. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  684. ppd = dd->pport + pidx;
  685. if (!ppd->hfi1_wq) {
  686. ppd->hfi1_wq =
  687. alloc_workqueue(
  688. "hfi%d_%d",
  689. WQ_SYSFS | WQ_HIGHPRI | WQ_CPU_INTENSIVE,
  690. HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES,
  691. dd->unit, pidx);
  692. if (!ppd->hfi1_wq)
  693. goto wq_error;
  694. }
  695. if (!ppd->link_wq) {
  696. /*
  697. * Make the link workqueue single-threaded to enforce
  698. * serialization.
  699. */
  700. ppd->link_wq =
  701. alloc_workqueue(
  702. "hfi_link_%d_%d",
  703. WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND,
  704. 1, /* max_active */
  705. dd->unit, pidx);
  706. if (!ppd->link_wq)
  707. goto wq_error;
  708. }
  709. }
  710. return 0;
  711. wq_error:
  712. pr_err("alloc_workqueue failed for port %d\n", pidx + 1);
  713. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  714. ppd = dd->pport + pidx;
  715. if (ppd->hfi1_wq) {
  716. destroy_workqueue(ppd->hfi1_wq);
  717. ppd->hfi1_wq = NULL;
  718. }
  719. if (ppd->link_wq) {
  720. destroy_workqueue(ppd->link_wq);
  721. ppd->link_wq = NULL;
  722. }
  723. }
  724. return -ENOMEM;
  725. }
  726. /**
  727. * enable_general_intr() - Enable the IRQs that will be handled by the
  728. * general interrupt handler.
  729. * @dd: valid devdata
  730. *
  731. */
  732. static void enable_general_intr(struct hfi1_devdata *dd)
  733. {
  734. set_intr_bits(dd, CCE_ERR_INT, MISC_ERR_INT, true);
  735. set_intr_bits(dd, PIO_ERR_INT, TXE_ERR_INT, true);
  736. set_intr_bits(dd, IS_SENDCTXT_ERR_START, IS_SENDCTXT_ERR_END, true);
  737. set_intr_bits(dd, PBC_INT, GPIO_ASSERT_INT, true);
  738. set_intr_bits(dd, TCRIT_INT, TCRIT_INT, true);
  739. set_intr_bits(dd, IS_DC_START, IS_DC_END, true);
  740. set_intr_bits(dd, IS_SENDCREDIT_START, IS_SENDCREDIT_END, true);
  741. }
  742. /**
  743. * hfi1_init - do the actual initialization sequence on the chip
  744. * @dd: the hfi1_ib device
  745. * @reinit: re-initializing, so don't allocate new memory
  746. *
  747. * Do the actual initialization sequence on the chip. This is done
  748. * both from the init routine called from the PCI infrastructure, and
  749. * when we reset the chip, or detect that it was reset internally,
  750. * or it's administratively re-enabled.
  751. *
  752. * Memory allocation here and in called routines is only done in
  753. * the first case (reinit == 0). We have to be careful, because even
  754. * without memory allocation, we need to re-write all the chip registers
  755. * TIDs, etc. after the reset or enable has completed.
  756. */
  757. int hfi1_init(struct hfi1_devdata *dd, int reinit)
  758. {
  759. int ret = 0, pidx, lastfail = 0;
  760. unsigned long len;
  761. u16 i;
  762. struct hfi1_ctxtdata *rcd;
  763. struct hfi1_pportdata *ppd;
  764. /* Set up send low level handlers */
  765. dd->process_pio_send = hfi1_verbs_send_pio;
  766. dd->process_dma_send = hfi1_verbs_send_dma;
  767. dd->pio_inline_send = pio_copy;
  768. dd->process_vnic_dma_send = hfi1_vnic_send_dma;
  769. if (is_ax(dd)) {
  770. atomic_set(&dd->drop_packet, DROP_PACKET_ON);
  771. dd->do_drop = 1;
  772. } else {
  773. atomic_set(&dd->drop_packet, DROP_PACKET_OFF);
  774. dd->do_drop = 0;
  775. }
  776. /* make sure the link is not "up" */
  777. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  778. ppd = dd->pport + pidx;
  779. ppd->linkup = 0;
  780. }
  781. if (reinit)
  782. ret = init_after_reset(dd);
  783. else
  784. ret = loadtime_init(dd);
  785. if (ret)
  786. goto done;
  787. /* allocate dummy tail memory for all receive contexts */
  788. dd->rcvhdrtail_dummy_kvaddr = dma_zalloc_coherent(
  789. &dd->pcidev->dev, sizeof(u64),
  790. &dd->rcvhdrtail_dummy_dma,
  791. GFP_KERNEL);
  792. if (!dd->rcvhdrtail_dummy_kvaddr) {
  793. dd_dev_err(dd, "cannot allocate dummy tail memory\n");
  794. ret = -ENOMEM;
  795. goto done;
  796. }
  797. /* dd->rcd can be NULL if early initialization failed */
  798. for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i) {
  799. /*
  800. * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
  801. * re-init, the simplest way to handle this is to free
  802. * existing, and re-allocate.
  803. * Need to re-create rest of ctxt 0 ctxtdata as well.
  804. */
  805. rcd = hfi1_rcd_get_by_index(dd, i);
  806. if (!rcd)
  807. continue;
  808. rcd->do_interrupt = &handle_receive_interrupt;
  809. lastfail = hfi1_create_rcvhdrq(dd, rcd);
  810. if (!lastfail)
  811. lastfail = hfi1_setup_eagerbufs(rcd);
  812. if (lastfail) {
  813. dd_dev_err(dd,
  814. "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
  815. ret = lastfail;
  816. }
  817. /* enable IRQ */
  818. hfi1_rcd_put(rcd);
  819. }
  820. /* Allocate enough memory for user event notification. */
  821. len = PAGE_ALIGN(chip_rcv_contexts(dd) * HFI1_MAX_SHARED_CTXTS *
  822. sizeof(*dd->events));
  823. dd->events = vmalloc_user(len);
  824. if (!dd->events)
  825. dd_dev_err(dd, "Failed to allocate user events page\n");
  826. /*
  827. * Allocate a page for device and port status.
  828. * Page will be shared amongst all user processes.
  829. */
  830. dd->status = vmalloc_user(PAGE_SIZE);
  831. if (!dd->status)
  832. dd_dev_err(dd, "Failed to allocate dev status page\n");
  833. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  834. ppd = dd->pport + pidx;
  835. if (dd->status)
  836. /* Currently, we only have one port */
  837. ppd->statusp = &dd->status->port;
  838. set_mtu(ppd);
  839. }
  840. /* enable chip even if we have an error, so we can debug cause */
  841. enable_chip(dd);
  842. done:
  843. /*
  844. * Set status even if port serdes is not initialized
  845. * so that diags will work.
  846. */
  847. if (dd->status)
  848. dd->status->dev |= HFI1_STATUS_CHIP_PRESENT |
  849. HFI1_STATUS_INITTED;
  850. if (!ret) {
  851. /* enable all interrupts from the chip */
  852. enable_general_intr(dd);
  853. init_qsfp_int(dd);
  854. /* chip is OK for user apps; mark it as initialized */
  855. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  856. ppd = dd->pport + pidx;
  857. /*
  858. * start the serdes - must be after interrupts are
  859. * enabled so we are notified when the link goes up
  860. */
  861. lastfail = bringup_serdes(ppd);
  862. if (lastfail)
  863. dd_dev_info(dd,
  864. "Failed to bring up port %u\n",
  865. ppd->port);
  866. /*
  867. * Set status even if port serdes is not initialized
  868. * so that diags will work.
  869. */
  870. if (ppd->statusp)
  871. *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT |
  872. HFI1_STATUS_INITTED;
  873. if (!ppd->link_speed_enabled)
  874. continue;
  875. }
  876. }
  877. /* if ret is non-zero, we probably should do some cleanup here... */
  878. return ret;
  879. }
  880. static inline struct hfi1_devdata *__hfi1_lookup(int unit)
  881. {
  882. return idr_find(&hfi1_unit_table, unit);
  883. }
  884. struct hfi1_devdata *hfi1_lookup(int unit)
  885. {
  886. struct hfi1_devdata *dd;
  887. unsigned long flags;
  888. spin_lock_irqsave(&hfi1_devs_lock, flags);
  889. dd = __hfi1_lookup(unit);
  890. spin_unlock_irqrestore(&hfi1_devs_lock, flags);
  891. return dd;
  892. }
  893. /*
  894. * Stop the timers during unit shutdown, or after an error late
  895. * in initialization.
  896. */
  897. static void stop_timers(struct hfi1_devdata *dd)
  898. {
  899. struct hfi1_pportdata *ppd;
  900. int pidx;
  901. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  902. ppd = dd->pport + pidx;
  903. if (ppd->led_override_timer.function) {
  904. del_timer_sync(&ppd->led_override_timer);
  905. atomic_set(&ppd->led_override_timer_active, 0);
  906. }
  907. }
  908. }
  909. /**
  910. * shutdown_device - shut down a device
  911. * @dd: the hfi1_ib device
  912. *
  913. * This is called to make the device quiet when we are about to
  914. * unload the driver, and also when the device is administratively
  915. * disabled. It does not free any data structures.
  916. * Everything it does has to be setup again by hfi1_init(dd, 1)
  917. */
  918. static void shutdown_device(struct hfi1_devdata *dd)
  919. {
  920. struct hfi1_pportdata *ppd;
  921. struct hfi1_ctxtdata *rcd;
  922. unsigned pidx;
  923. int i;
  924. if (dd->flags & HFI1_SHUTDOWN)
  925. return;
  926. dd->flags |= HFI1_SHUTDOWN;
  927. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  928. ppd = dd->pport + pidx;
  929. ppd->linkup = 0;
  930. if (ppd->statusp)
  931. *ppd->statusp &= ~(HFI1_STATUS_IB_CONF |
  932. HFI1_STATUS_IB_READY);
  933. }
  934. dd->flags &= ~HFI1_INITTED;
  935. /* mask and clean up interrupts */
  936. set_intr_bits(dd, IS_FIRST_SOURCE, IS_LAST_SOURCE, false);
  937. msix_clean_up_interrupts(dd);
  938. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  939. ppd = dd->pport + pidx;
  940. for (i = 0; i < dd->num_rcv_contexts; i++) {
  941. rcd = hfi1_rcd_get_by_index(dd, i);
  942. hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS |
  943. HFI1_RCVCTRL_CTXT_DIS |
  944. HFI1_RCVCTRL_INTRAVAIL_DIS |
  945. HFI1_RCVCTRL_PKEY_DIS |
  946. HFI1_RCVCTRL_ONE_PKT_EGR_DIS, rcd);
  947. hfi1_rcd_put(rcd);
  948. }
  949. /*
  950. * Gracefully stop all sends allowing any in progress to
  951. * trickle out first.
  952. */
  953. for (i = 0; i < dd->num_send_contexts; i++)
  954. sc_flush(dd->send_contexts[i].sc);
  955. }
  956. /*
  957. * Enough for anything that's going to trickle out to have actually
  958. * done so.
  959. */
  960. udelay(20);
  961. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  962. ppd = dd->pport + pidx;
  963. /* disable all contexts */
  964. for (i = 0; i < dd->num_send_contexts; i++)
  965. sc_disable(dd->send_contexts[i].sc);
  966. /* disable the send device */
  967. pio_send_control(dd, PSC_GLOBAL_DISABLE);
  968. shutdown_led_override(ppd);
  969. /*
  970. * Clear SerdesEnable.
  971. * We can't count on interrupts since we are stopping.
  972. */
  973. hfi1_quiet_serdes(ppd);
  974. if (ppd->hfi1_wq) {
  975. destroy_workqueue(ppd->hfi1_wq);
  976. ppd->hfi1_wq = NULL;
  977. }
  978. if (ppd->link_wq) {
  979. destroy_workqueue(ppd->link_wq);
  980. ppd->link_wq = NULL;
  981. }
  982. }
  983. sdma_exit(dd);
  984. }
  985. /**
  986. * hfi1_free_ctxtdata - free a context's allocated data
  987. * @dd: the hfi1_ib device
  988. * @rcd: the ctxtdata structure
  989. *
  990. * free up any allocated data for a context
  991. * It should never change any chip state, or global driver state.
  992. */
  993. void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
  994. {
  995. u32 e;
  996. if (!rcd)
  997. return;
  998. if (rcd->rcvhdrq) {
  999. dma_free_coherent(&dd->pcidev->dev, rcvhdrq_size(rcd),
  1000. rcd->rcvhdrq, rcd->rcvhdrq_dma);
  1001. rcd->rcvhdrq = NULL;
  1002. if (rcd->rcvhdrtail_kvaddr) {
  1003. dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
  1004. (void *)rcd->rcvhdrtail_kvaddr,
  1005. rcd->rcvhdrqtailaddr_dma);
  1006. rcd->rcvhdrtail_kvaddr = NULL;
  1007. }
  1008. }
  1009. /* all the RcvArray entries should have been cleared by now */
  1010. kfree(rcd->egrbufs.rcvtids);
  1011. rcd->egrbufs.rcvtids = NULL;
  1012. for (e = 0; e < rcd->egrbufs.alloced; e++) {
  1013. if (rcd->egrbufs.buffers[e].dma)
  1014. dma_free_coherent(&dd->pcidev->dev,
  1015. rcd->egrbufs.buffers[e].len,
  1016. rcd->egrbufs.buffers[e].addr,
  1017. rcd->egrbufs.buffers[e].dma);
  1018. }
  1019. kfree(rcd->egrbufs.buffers);
  1020. rcd->egrbufs.alloced = 0;
  1021. rcd->egrbufs.buffers = NULL;
  1022. sc_free(rcd->sc);
  1023. rcd->sc = NULL;
  1024. vfree(rcd->subctxt_uregbase);
  1025. vfree(rcd->subctxt_rcvegrbuf);
  1026. vfree(rcd->subctxt_rcvhdr_base);
  1027. kfree(rcd->opstats);
  1028. rcd->subctxt_uregbase = NULL;
  1029. rcd->subctxt_rcvegrbuf = NULL;
  1030. rcd->subctxt_rcvhdr_base = NULL;
  1031. rcd->opstats = NULL;
  1032. }
  1033. /*
  1034. * Release our hold on the shared asic data. If we are the last one,
  1035. * return the structure to be finalized outside the lock. Must be
  1036. * holding hfi1_devs_lock.
  1037. */
  1038. static struct hfi1_asic_data *release_asic_data(struct hfi1_devdata *dd)
  1039. {
  1040. struct hfi1_asic_data *ad;
  1041. int other;
  1042. if (!dd->asic_data)
  1043. return NULL;
  1044. dd->asic_data->dds[dd->hfi1_id] = NULL;
  1045. other = dd->hfi1_id ? 0 : 1;
  1046. ad = dd->asic_data;
  1047. dd->asic_data = NULL;
  1048. /* return NULL if the other dd still has a link */
  1049. return ad->dds[other] ? NULL : ad;
  1050. }
  1051. static void finalize_asic_data(struct hfi1_devdata *dd,
  1052. struct hfi1_asic_data *ad)
  1053. {
  1054. clean_up_i2c(dd, ad);
  1055. kfree(ad);
  1056. }
  1057. /**
  1058. * hfi1_clean_devdata - cleans up per-unit data structure
  1059. * @dd: pointer to a valid devdata structure
  1060. *
  1061. * It cleans up all data structures set up by
  1062. * by hfi1_alloc_devdata().
  1063. */
  1064. static void hfi1_clean_devdata(struct hfi1_devdata *dd)
  1065. {
  1066. struct hfi1_asic_data *ad;
  1067. unsigned long flags;
  1068. spin_lock_irqsave(&hfi1_devs_lock, flags);
  1069. if (!list_empty(&dd->list)) {
  1070. idr_remove(&hfi1_unit_table, dd->unit);
  1071. list_del_init(&dd->list);
  1072. }
  1073. ad = release_asic_data(dd);
  1074. spin_unlock_irqrestore(&hfi1_devs_lock, flags);
  1075. finalize_asic_data(dd, ad);
  1076. free_platform_config(dd);
  1077. rcu_barrier(); /* wait for rcu callbacks to complete */
  1078. free_percpu(dd->int_counter);
  1079. free_percpu(dd->rcv_limit);
  1080. free_percpu(dd->send_schedule);
  1081. free_percpu(dd->tx_opstats);
  1082. dd->int_counter = NULL;
  1083. dd->rcv_limit = NULL;
  1084. dd->send_schedule = NULL;
  1085. dd->tx_opstats = NULL;
  1086. kfree(dd->comp_vect);
  1087. dd->comp_vect = NULL;
  1088. sdma_clean(dd, dd->num_sdma);
  1089. rvt_dealloc_device(&dd->verbs_dev.rdi);
  1090. }
  1091. static void __hfi1_free_devdata(struct kobject *kobj)
  1092. {
  1093. struct hfi1_devdata *dd =
  1094. container_of(kobj, struct hfi1_devdata, kobj);
  1095. hfi1_clean_devdata(dd);
  1096. }
  1097. static struct kobj_type hfi1_devdata_type = {
  1098. .release = __hfi1_free_devdata,
  1099. };
  1100. void hfi1_free_devdata(struct hfi1_devdata *dd)
  1101. {
  1102. kobject_put(&dd->kobj);
  1103. }
  1104. /**
  1105. * hfi1_alloc_devdata - Allocate our primary per-unit data structure.
  1106. * @pdev: Valid PCI device
  1107. * @extra: How many bytes to alloc past the default
  1108. *
  1109. * Must be done via verbs allocator, because the verbs cleanup process
  1110. * both does cleanup and free of the data structure.
  1111. * "extra" is for chip-specific data.
  1112. *
  1113. * Use the idr mechanism to get a unit number for this unit.
  1114. */
  1115. static struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev,
  1116. size_t extra)
  1117. {
  1118. unsigned long flags;
  1119. struct hfi1_devdata *dd;
  1120. int ret, nports;
  1121. /* extra is * number of ports */
  1122. nports = extra / sizeof(struct hfi1_pportdata);
  1123. dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra,
  1124. nports);
  1125. if (!dd)
  1126. return ERR_PTR(-ENOMEM);
  1127. dd->num_pports = nports;
  1128. dd->pport = (struct hfi1_pportdata *)(dd + 1);
  1129. dd->pcidev = pdev;
  1130. pci_set_drvdata(pdev, dd);
  1131. INIT_LIST_HEAD(&dd->list);
  1132. idr_preload(GFP_KERNEL);
  1133. spin_lock_irqsave(&hfi1_devs_lock, flags);
  1134. ret = idr_alloc(&hfi1_unit_table, dd, 0, 0, GFP_NOWAIT);
  1135. if (ret >= 0) {
  1136. dd->unit = ret;
  1137. list_add(&dd->list, &hfi1_dev_list);
  1138. }
  1139. dd->node = -1;
  1140. spin_unlock_irqrestore(&hfi1_devs_lock, flags);
  1141. idr_preload_end();
  1142. if (ret < 0) {
  1143. dev_err(&pdev->dev,
  1144. "Could not allocate unit ID: error %d\n", -ret);
  1145. goto bail;
  1146. }
  1147. rvt_set_ibdev_name(&dd->verbs_dev.rdi, "%s_%d", class_name(), dd->unit);
  1148. /*
  1149. * Initialize all locks for the device. This needs to be as early as
  1150. * possible so locks are usable.
  1151. */
  1152. spin_lock_init(&dd->sc_lock);
  1153. spin_lock_init(&dd->sendctrl_lock);
  1154. spin_lock_init(&dd->rcvctrl_lock);
  1155. spin_lock_init(&dd->uctxt_lock);
  1156. spin_lock_init(&dd->hfi1_diag_trans_lock);
  1157. spin_lock_init(&dd->sc_init_lock);
  1158. spin_lock_init(&dd->dc8051_memlock);
  1159. seqlock_init(&dd->sc2vl_lock);
  1160. spin_lock_init(&dd->sde_map_lock);
  1161. spin_lock_init(&dd->pio_map_lock);
  1162. mutex_init(&dd->dc8051_lock);
  1163. init_waitqueue_head(&dd->event_queue);
  1164. spin_lock_init(&dd->irq_src_lock);
  1165. dd->int_counter = alloc_percpu(u64);
  1166. if (!dd->int_counter) {
  1167. ret = -ENOMEM;
  1168. goto bail;
  1169. }
  1170. dd->rcv_limit = alloc_percpu(u64);
  1171. if (!dd->rcv_limit) {
  1172. ret = -ENOMEM;
  1173. goto bail;
  1174. }
  1175. dd->send_schedule = alloc_percpu(u64);
  1176. if (!dd->send_schedule) {
  1177. ret = -ENOMEM;
  1178. goto bail;
  1179. }
  1180. dd->tx_opstats = alloc_percpu(struct hfi1_opcode_stats_perctx);
  1181. if (!dd->tx_opstats) {
  1182. ret = -ENOMEM;
  1183. goto bail;
  1184. }
  1185. dd->comp_vect = kzalloc(sizeof(*dd->comp_vect), GFP_KERNEL);
  1186. if (!dd->comp_vect) {
  1187. ret = -ENOMEM;
  1188. goto bail;
  1189. }
  1190. kobject_init(&dd->kobj, &hfi1_devdata_type);
  1191. return dd;
  1192. bail:
  1193. hfi1_clean_devdata(dd);
  1194. return ERR_PTR(ret);
  1195. }
  1196. /*
  1197. * Called from freeze mode handlers, and from PCI error
  1198. * reporting code. Should be paranoid about state of
  1199. * system and data structures.
  1200. */
  1201. void hfi1_disable_after_error(struct hfi1_devdata *dd)
  1202. {
  1203. if (dd->flags & HFI1_INITTED) {
  1204. u32 pidx;
  1205. dd->flags &= ~HFI1_INITTED;
  1206. if (dd->pport)
  1207. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  1208. struct hfi1_pportdata *ppd;
  1209. ppd = dd->pport + pidx;
  1210. if (dd->flags & HFI1_PRESENT)
  1211. set_link_state(ppd, HLS_DN_DISABLE);
  1212. if (ppd->statusp)
  1213. *ppd->statusp &= ~HFI1_STATUS_IB_READY;
  1214. }
  1215. }
  1216. /*
  1217. * Mark as having had an error for driver, and also
  1218. * for /sys and status word mapped to user programs.
  1219. * This marks unit as not usable, until reset.
  1220. */
  1221. if (dd->status)
  1222. dd->status->dev |= HFI1_STATUS_HWERROR;
  1223. }
  1224. static void remove_one(struct pci_dev *);
  1225. static int init_one(struct pci_dev *, const struct pci_device_id *);
  1226. static void shutdown_one(struct pci_dev *);
  1227. #define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
  1228. #define PFX DRIVER_NAME ": "
  1229. const struct pci_device_id hfi1_pci_tbl[] = {
  1230. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
  1231. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
  1232. { 0, }
  1233. };
  1234. MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
  1235. static struct pci_driver hfi1_pci_driver = {
  1236. .name = DRIVER_NAME,
  1237. .probe = init_one,
  1238. .remove = remove_one,
  1239. .shutdown = shutdown_one,
  1240. .id_table = hfi1_pci_tbl,
  1241. .err_handler = &hfi1_pci_err_handler,
  1242. };
  1243. static void __init compute_krcvqs(void)
  1244. {
  1245. int i;
  1246. for (i = 0; i < krcvqsset; i++)
  1247. n_krcvqs += krcvqs[i];
  1248. }
  1249. /*
  1250. * Do all the generic driver unit- and chip-independent memory
  1251. * allocation and initialization.
  1252. */
  1253. static int __init hfi1_mod_init(void)
  1254. {
  1255. int ret;
  1256. ret = dev_init();
  1257. if (ret)
  1258. goto bail;
  1259. ret = node_affinity_init();
  1260. if (ret)
  1261. goto bail;
  1262. /* validate max MTU before any devices start */
  1263. if (!valid_opa_max_mtu(hfi1_max_mtu)) {
  1264. pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
  1265. hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
  1266. hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
  1267. }
  1268. /* valid CUs run from 1-128 in powers of 2 */
  1269. if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
  1270. hfi1_cu = 1;
  1271. /* valid credit return threshold is 0-100, variable is unsigned */
  1272. if (user_credit_return_threshold > 100)
  1273. user_credit_return_threshold = 100;
  1274. compute_krcvqs();
  1275. /*
  1276. * sanitize receive interrupt count, time must wait until after
  1277. * the hardware type is known
  1278. */
  1279. if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
  1280. rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
  1281. /* reject invalid combinations */
  1282. if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
  1283. pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
  1284. rcv_intr_count = 1;
  1285. }
  1286. if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
  1287. /*
  1288. * Avoid indefinite packet delivery by requiring a timeout
  1289. * if count is > 1.
  1290. */
  1291. pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
  1292. rcv_intr_timeout = 1;
  1293. }
  1294. if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
  1295. /*
  1296. * The dynamic algorithm expects a non-zero timeout
  1297. * and a count > 1.
  1298. */
  1299. pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
  1300. rcv_intr_dynamic = 0;
  1301. }
  1302. /* sanitize link CRC options */
  1303. link_crc_mask &= SUPPORTED_CRCS;
  1304. /*
  1305. * These must be called before the driver is registered with
  1306. * the PCI subsystem.
  1307. */
  1308. idr_init(&hfi1_unit_table);
  1309. hfi1_dbg_init();
  1310. ret = pci_register_driver(&hfi1_pci_driver);
  1311. if (ret < 0) {
  1312. pr_err("Unable to register driver: error %d\n", -ret);
  1313. goto bail_dev;
  1314. }
  1315. goto bail; /* all OK */
  1316. bail_dev:
  1317. hfi1_dbg_exit();
  1318. idr_destroy(&hfi1_unit_table);
  1319. dev_cleanup();
  1320. bail:
  1321. return ret;
  1322. }
  1323. module_init(hfi1_mod_init);
  1324. /*
  1325. * Do the non-unit driver cleanup, memory free, etc. at unload.
  1326. */
  1327. static void __exit hfi1_mod_cleanup(void)
  1328. {
  1329. pci_unregister_driver(&hfi1_pci_driver);
  1330. node_affinity_destroy_all();
  1331. hfi1_dbg_exit();
  1332. idr_destroy(&hfi1_unit_table);
  1333. dispose_firmware(); /* asymmetric with obtain_firmware() */
  1334. dev_cleanup();
  1335. }
  1336. module_exit(hfi1_mod_cleanup);
  1337. /* this can only be called after a successful initialization */
  1338. static void cleanup_device_data(struct hfi1_devdata *dd)
  1339. {
  1340. int ctxt;
  1341. int pidx;
  1342. /* users can't do anything more with chip */
  1343. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  1344. struct hfi1_pportdata *ppd = &dd->pport[pidx];
  1345. struct cc_state *cc_state;
  1346. int i;
  1347. if (ppd->statusp)
  1348. *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
  1349. for (i = 0; i < OPA_MAX_SLS; i++)
  1350. hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
  1351. spin_lock(&ppd->cc_state_lock);
  1352. cc_state = get_cc_state_protected(ppd);
  1353. RCU_INIT_POINTER(ppd->cc_state, NULL);
  1354. spin_unlock(&ppd->cc_state_lock);
  1355. if (cc_state)
  1356. kfree_rcu(cc_state, rcu);
  1357. }
  1358. free_credit_return(dd);
  1359. if (dd->rcvhdrtail_dummy_kvaddr) {
  1360. dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
  1361. (void *)dd->rcvhdrtail_dummy_kvaddr,
  1362. dd->rcvhdrtail_dummy_dma);
  1363. dd->rcvhdrtail_dummy_kvaddr = NULL;
  1364. }
  1365. /*
  1366. * Free any resources still in use (usually just kernel contexts)
  1367. * at unload; we do for ctxtcnt, because that's what we allocate.
  1368. */
  1369. for (ctxt = 0; dd->rcd && ctxt < dd->num_rcv_contexts; ctxt++) {
  1370. struct hfi1_ctxtdata *rcd = dd->rcd[ctxt];
  1371. if (rcd) {
  1372. hfi1_clear_tids(rcd);
  1373. hfi1_free_ctxt(rcd);
  1374. }
  1375. }
  1376. kfree(dd->rcd);
  1377. dd->rcd = NULL;
  1378. free_pio_map(dd);
  1379. /* must follow rcv context free - need to remove rcv's hooks */
  1380. for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
  1381. sc_free(dd->send_contexts[ctxt].sc);
  1382. dd->num_send_contexts = 0;
  1383. kfree(dd->send_contexts);
  1384. dd->send_contexts = NULL;
  1385. kfree(dd->hw_to_sw);
  1386. dd->hw_to_sw = NULL;
  1387. kfree(dd->boardname);
  1388. vfree(dd->events);
  1389. vfree(dd->status);
  1390. }
  1391. /*
  1392. * Clean up on unit shutdown, or error during unit load after
  1393. * successful initialization.
  1394. */
  1395. static void postinit_cleanup(struct hfi1_devdata *dd)
  1396. {
  1397. hfi1_start_cleanup(dd);
  1398. hfi1_comp_vectors_clean_up(dd);
  1399. hfi1_dev_affinity_clean_up(dd);
  1400. hfi1_pcie_ddcleanup(dd);
  1401. hfi1_pcie_cleanup(dd->pcidev);
  1402. cleanup_device_data(dd);
  1403. hfi1_free_devdata(dd);
  1404. }
  1405. static int init_validate_rcvhdrcnt(struct hfi1_devdata *dd, uint thecnt)
  1406. {
  1407. if (thecnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) {
  1408. dd_dev_err(dd, "Receive header queue count too small\n");
  1409. return -EINVAL;
  1410. }
  1411. if (thecnt > HFI1_MAX_HDRQ_EGRBUF_CNT) {
  1412. dd_dev_err(dd,
  1413. "Receive header queue count cannot be greater than %u\n",
  1414. HFI1_MAX_HDRQ_EGRBUF_CNT);
  1415. return -EINVAL;
  1416. }
  1417. if (thecnt % HDRQ_INCREMENT) {
  1418. dd_dev_err(dd, "Receive header queue count %d must be divisible by %lu\n",
  1419. thecnt, HDRQ_INCREMENT);
  1420. return -EINVAL;
  1421. }
  1422. return 0;
  1423. }
  1424. static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1425. {
  1426. int ret = 0, j, pidx, initfail;
  1427. struct hfi1_devdata *dd;
  1428. struct hfi1_pportdata *ppd;
  1429. /* First, lock the non-writable module parameters */
  1430. HFI1_CAP_LOCK();
  1431. /* Validate dev ids */
  1432. if (!(ent->device == PCI_DEVICE_ID_INTEL0 ||
  1433. ent->device == PCI_DEVICE_ID_INTEL1)) {
  1434. dev_err(&pdev->dev, "Failing on unknown Intel deviceid 0x%x\n",
  1435. ent->device);
  1436. ret = -ENODEV;
  1437. goto bail;
  1438. }
  1439. /* Allocate the dd so we can get to work */
  1440. dd = hfi1_alloc_devdata(pdev, NUM_IB_PORTS *
  1441. sizeof(struct hfi1_pportdata));
  1442. if (IS_ERR(dd)) {
  1443. ret = PTR_ERR(dd);
  1444. goto bail;
  1445. }
  1446. /* Validate some global module parameters */
  1447. ret = init_validate_rcvhdrcnt(dd, rcvhdrcnt);
  1448. if (ret)
  1449. goto bail;
  1450. /* use the encoding function as a sanitization check */
  1451. if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
  1452. dd_dev_err(dd, "Invalid HdrQ Entry size %u\n",
  1453. hfi1_hdrq_entsize);
  1454. ret = -EINVAL;
  1455. goto bail;
  1456. }
  1457. /* The receive eager buffer size must be set before the receive
  1458. * contexts are created.
  1459. *
  1460. * Set the eager buffer size. Validate that it falls in a range
  1461. * allowed by the hardware - all powers of 2 between the min and
  1462. * max. The maximum valid MTU is within the eager buffer range
  1463. * so we do not need to cap the max_mtu by an eager buffer size
  1464. * setting.
  1465. */
  1466. if (eager_buffer_size) {
  1467. if (!is_power_of_2(eager_buffer_size))
  1468. eager_buffer_size =
  1469. roundup_pow_of_two(eager_buffer_size);
  1470. eager_buffer_size =
  1471. clamp_val(eager_buffer_size,
  1472. MIN_EAGER_BUFFER * 8,
  1473. MAX_EAGER_BUFFER_TOTAL);
  1474. dd_dev_info(dd, "Eager buffer size %u\n",
  1475. eager_buffer_size);
  1476. } else {
  1477. dd_dev_err(dd, "Invalid Eager buffer size of 0\n");
  1478. ret = -EINVAL;
  1479. goto bail;
  1480. }
  1481. /* restrict value of hfi1_rcvarr_split */
  1482. hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
  1483. ret = hfi1_pcie_init(dd);
  1484. if (ret)
  1485. goto bail;
  1486. /*
  1487. * Do device-specific initialization, function table setup, dd
  1488. * allocation, etc.
  1489. */
  1490. ret = hfi1_init_dd(dd);
  1491. if (ret)
  1492. goto clean_bail; /* error already printed */
  1493. ret = create_workqueues(dd);
  1494. if (ret)
  1495. goto clean_bail;
  1496. /* do the generic initialization */
  1497. initfail = hfi1_init(dd, 0);
  1498. /* setup vnic */
  1499. hfi1_vnic_setup(dd);
  1500. ret = hfi1_register_ib_device(dd);
  1501. /*
  1502. * Now ready for use. this should be cleared whenever we
  1503. * detect a reset, or initiate one. If earlier failure,
  1504. * we still create devices, so diags, etc. can be used
  1505. * to determine cause of problem.
  1506. */
  1507. if (!initfail && !ret) {
  1508. dd->flags |= HFI1_INITTED;
  1509. /* create debufs files after init and ib register */
  1510. hfi1_dbg_ibdev_init(&dd->verbs_dev);
  1511. }
  1512. j = hfi1_device_create(dd);
  1513. if (j)
  1514. dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
  1515. if (initfail || ret) {
  1516. msix_clean_up_interrupts(dd);
  1517. stop_timers(dd);
  1518. flush_workqueue(ib_wq);
  1519. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  1520. hfi1_quiet_serdes(dd->pport + pidx);
  1521. ppd = dd->pport + pidx;
  1522. if (ppd->hfi1_wq) {
  1523. destroy_workqueue(ppd->hfi1_wq);
  1524. ppd->hfi1_wq = NULL;
  1525. }
  1526. if (ppd->link_wq) {
  1527. destroy_workqueue(ppd->link_wq);
  1528. ppd->link_wq = NULL;
  1529. }
  1530. }
  1531. if (!j)
  1532. hfi1_device_remove(dd);
  1533. if (!ret)
  1534. hfi1_unregister_ib_device(dd);
  1535. hfi1_vnic_cleanup(dd);
  1536. postinit_cleanup(dd);
  1537. if (initfail)
  1538. ret = initfail;
  1539. goto bail; /* everything already cleaned */
  1540. }
  1541. sdma_start(dd);
  1542. return 0;
  1543. clean_bail:
  1544. hfi1_pcie_cleanup(pdev);
  1545. bail:
  1546. return ret;
  1547. }
  1548. static void wait_for_clients(struct hfi1_devdata *dd)
  1549. {
  1550. /*
  1551. * Remove the device init value and complete the device if there is
  1552. * no clients or wait for active clients to finish.
  1553. */
  1554. if (atomic_dec_and_test(&dd->user_refcount))
  1555. complete(&dd->user_comp);
  1556. wait_for_completion(&dd->user_comp);
  1557. }
  1558. static void remove_one(struct pci_dev *pdev)
  1559. {
  1560. struct hfi1_devdata *dd = pci_get_drvdata(pdev);
  1561. /* close debugfs files before ib unregister */
  1562. hfi1_dbg_ibdev_exit(&dd->verbs_dev);
  1563. /* remove the /dev hfi1 interface */
  1564. hfi1_device_remove(dd);
  1565. /* wait for existing user space clients to finish */
  1566. wait_for_clients(dd);
  1567. /* unregister from IB core */
  1568. hfi1_unregister_ib_device(dd);
  1569. /* cleanup vnic */
  1570. hfi1_vnic_cleanup(dd);
  1571. /*
  1572. * Disable the IB link, disable interrupts on the device,
  1573. * clear dma engines, etc.
  1574. */
  1575. shutdown_device(dd);
  1576. stop_timers(dd);
  1577. /* wait until all of our (qsfp) queue_work() calls complete */
  1578. flush_workqueue(ib_wq);
  1579. postinit_cleanup(dd);
  1580. }
  1581. static void shutdown_one(struct pci_dev *pdev)
  1582. {
  1583. struct hfi1_devdata *dd = pci_get_drvdata(pdev);
  1584. shutdown_device(dd);
  1585. }
  1586. /**
  1587. * hfi1_create_rcvhdrq - create a receive header queue
  1588. * @dd: the hfi1_ib device
  1589. * @rcd: the context data
  1590. *
  1591. * This must be contiguous memory (from an i/o perspective), and must be
  1592. * DMA'able (which means for some systems, it will go through an IOMMU,
  1593. * or be forced into a low address range).
  1594. */
  1595. int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
  1596. {
  1597. unsigned amt;
  1598. u64 reg;
  1599. if (!rcd->rcvhdrq) {
  1600. gfp_t gfp_flags;
  1601. amt = rcvhdrq_size(rcd);
  1602. if (rcd->ctxt < dd->first_dyn_alloc_ctxt || rcd->is_vnic)
  1603. gfp_flags = GFP_KERNEL;
  1604. else
  1605. gfp_flags = GFP_USER;
  1606. rcd->rcvhdrq = dma_zalloc_coherent(
  1607. &dd->pcidev->dev, amt, &rcd->rcvhdrq_dma,
  1608. gfp_flags | __GFP_COMP);
  1609. if (!rcd->rcvhdrq) {
  1610. dd_dev_err(dd,
  1611. "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
  1612. amt, rcd->ctxt);
  1613. goto bail;
  1614. }
  1615. if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ||
  1616. HFI1_CAP_UGET_MASK(rcd->flags, DMA_RTAIL)) {
  1617. rcd->rcvhdrtail_kvaddr = dma_zalloc_coherent(
  1618. &dd->pcidev->dev, PAGE_SIZE,
  1619. &rcd->rcvhdrqtailaddr_dma, gfp_flags);
  1620. if (!rcd->rcvhdrtail_kvaddr)
  1621. goto bail_free;
  1622. }
  1623. }
  1624. /*
  1625. * These values are per-context:
  1626. * RcvHdrCnt
  1627. * RcvHdrEntSize
  1628. * RcvHdrSize
  1629. */
  1630. reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT)
  1631. & RCV_HDR_CNT_CNT_MASK)
  1632. << RCV_HDR_CNT_CNT_SHIFT;
  1633. write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg);
  1634. reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize)
  1635. & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK)
  1636. << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT;
  1637. write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg);
  1638. reg = ((u64)DEFAULT_RCVHDRSIZE & RCV_HDR_SIZE_HDR_SIZE_MASK)
  1639. << RCV_HDR_SIZE_HDR_SIZE_SHIFT;
  1640. write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg);
  1641. /*
  1642. * Program dummy tail address for every receive context
  1643. * before enabling any receive context
  1644. */
  1645. write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR,
  1646. dd->rcvhdrtail_dummy_dma);
  1647. return 0;
  1648. bail_free:
  1649. dd_dev_err(dd,
  1650. "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
  1651. rcd->ctxt);
  1652. dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
  1653. rcd->rcvhdrq_dma);
  1654. rcd->rcvhdrq = NULL;
  1655. bail:
  1656. return -ENOMEM;
  1657. }
  1658. /**
  1659. * allocate eager buffers, both kernel and user contexts.
  1660. * @rcd: the context we are setting up.
  1661. *
  1662. * Allocate the eager TID buffers and program them into hip.
  1663. * They are no longer completely contiguous, we do multiple allocation
  1664. * calls. Otherwise we get the OOM code involved, by asking for too
  1665. * much per call, with disastrous results on some kernels.
  1666. */
  1667. int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
  1668. {
  1669. struct hfi1_devdata *dd = rcd->dd;
  1670. u32 max_entries, egrtop, alloced_bytes = 0;
  1671. gfp_t gfp_flags;
  1672. u16 order, idx = 0;
  1673. int ret = 0;
  1674. u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
  1675. /*
  1676. * GFP_USER, but without GFP_FS, so buffer cache can be
  1677. * coalesced (we hope); otherwise, even at order 4,
  1678. * heavy filesystem activity makes these fail, and we can
  1679. * use compound pages.
  1680. */
  1681. gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
  1682. /*
  1683. * The minimum size of the eager buffers is a groups of MTU-sized
  1684. * buffers.
  1685. * The global eager_buffer_size parameter is checked against the
  1686. * theoretical lower limit of the value. Here, we check against the
  1687. * MTU.
  1688. */
  1689. if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
  1690. rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
  1691. /*
  1692. * If using one-pkt-per-egr-buffer, lower the eager buffer
  1693. * size to the max MTU (page-aligned).
  1694. */
  1695. if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
  1696. rcd->egrbufs.rcvtid_size = round_mtu;
  1697. /*
  1698. * Eager buffers sizes of 1MB or less require smaller TID sizes
  1699. * to satisfy the "multiple of 8 RcvArray entries" requirement.
  1700. */
  1701. if (rcd->egrbufs.size <= (1 << 20))
  1702. rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
  1703. rounddown_pow_of_two(rcd->egrbufs.size / 8));
  1704. while (alloced_bytes < rcd->egrbufs.size &&
  1705. rcd->egrbufs.alloced < rcd->egrbufs.count) {
  1706. rcd->egrbufs.buffers[idx].addr =
  1707. dma_zalloc_coherent(&dd->pcidev->dev,
  1708. rcd->egrbufs.rcvtid_size,
  1709. &rcd->egrbufs.buffers[idx].dma,
  1710. gfp_flags);
  1711. if (rcd->egrbufs.buffers[idx].addr) {
  1712. rcd->egrbufs.buffers[idx].len =
  1713. rcd->egrbufs.rcvtid_size;
  1714. rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
  1715. rcd->egrbufs.buffers[idx].addr;
  1716. rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].dma =
  1717. rcd->egrbufs.buffers[idx].dma;
  1718. rcd->egrbufs.alloced++;
  1719. alloced_bytes += rcd->egrbufs.rcvtid_size;
  1720. idx++;
  1721. } else {
  1722. u32 new_size, i, j;
  1723. u64 offset = 0;
  1724. /*
  1725. * Fail the eager buffer allocation if:
  1726. * - we are already using the lowest acceptable size
  1727. * - we are using one-pkt-per-egr-buffer (this implies
  1728. * that we are accepting only one size)
  1729. */
  1730. if (rcd->egrbufs.rcvtid_size == round_mtu ||
  1731. !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
  1732. dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
  1733. rcd->ctxt);
  1734. ret = -ENOMEM;
  1735. goto bail_rcvegrbuf_phys;
  1736. }
  1737. new_size = rcd->egrbufs.rcvtid_size / 2;
  1738. /*
  1739. * If the first attempt to allocate memory failed, don't
  1740. * fail everything but continue with the next lower
  1741. * size.
  1742. */
  1743. if (idx == 0) {
  1744. rcd->egrbufs.rcvtid_size = new_size;
  1745. continue;
  1746. }
  1747. /*
  1748. * Re-partition already allocated buffers to a smaller
  1749. * size.
  1750. */
  1751. rcd->egrbufs.alloced = 0;
  1752. for (i = 0, j = 0, offset = 0; j < idx; i++) {
  1753. if (i >= rcd->egrbufs.count)
  1754. break;
  1755. rcd->egrbufs.rcvtids[i].dma =
  1756. rcd->egrbufs.buffers[j].dma + offset;
  1757. rcd->egrbufs.rcvtids[i].addr =
  1758. rcd->egrbufs.buffers[j].addr + offset;
  1759. rcd->egrbufs.alloced++;
  1760. if ((rcd->egrbufs.buffers[j].dma + offset +
  1761. new_size) ==
  1762. (rcd->egrbufs.buffers[j].dma +
  1763. rcd->egrbufs.buffers[j].len)) {
  1764. j++;
  1765. offset = 0;
  1766. } else {
  1767. offset += new_size;
  1768. }
  1769. }
  1770. rcd->egrbufs.rcvtid_size = new_size;
  1771. }
  1772. }
  1773. rcd->egrbufs.numbufs = idx;
  1774. rcd->egrbufs.size = alloced_bytes;
  1775. hfi1_cdbg(PROC,
  1776. "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %zuKB\n",
  1777. rcd->ctxt, rcd->egrbufs.alloced,
  1778. rcd->egrbufs.rcvtid_size / 1024, rcd->egrbufs.size / 1024);
  1779. /*
  1780. * Set the contexts rcv array head update threshold to the closest
  1781. * power of 2 (so we can use a mask instead of modulo) below half
  1782. * the allocated entries.
  1783. */
  1784. rcd->egrbufs.threshold =
  1785. rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
  1786. /*
  1787. * Compute the expected RcvArray entry base. This is done after
  1788. * allocating the eager buffers in order to maximize the
  1789. * expected RcvArray entries for the context.
  1790. */
  1791. max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
  1792. egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
  1793. rcd->expected_count = max_entries - egrtop;
  1794. if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
  1795. rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
  1796. rcd->expected_base = rcd->eager_base + egrtop;
  1797. hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
  1798. rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
  1799. rcd->eager_base, rcd->expected_base);
  1800. if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
  1801. hfi1_cdbg(PROC,
  1802. "ctxt%u: current Eager buffer size is invalid %u\n",
  1803. rcd->ctxt, rcd->egrbufs.rcvtid_size);
  1804. ret = -EINVAL;
  1805. goto bail_rcvegrbuf_phys;
  1806. }
  1807. for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
  1808. hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
  1809. rcd->egrbufs.rcvtids[idx].dma, order);
  1810. cond_resched();
  1811. }
  1812. return 0;
  1813. bail_rcvegrbuf_phys:
  1814. for (idx = 0; idx < rcd->egrbufs.alloced &&
  1815. rcd->egrbufs.buffers[idx].addr;
  1816. idx++) {
  1817. dma_free_coherent(&dd->pcidev->dev,
  1818. rcd->egrbufs.buffers[idx].len,
  1819. rcd->egrbufs.buffers[idx].addr,
  1820. rcd->egrbufs.buffers[idx].dma);
  1821. rcd->egrbufs.buffers[idx].addr = NULL;
  1822. rcd->egrbufs.buffers[idx].dma = 0;
  1823. rcd->egrbufs.buffers[idx].len = 0;
  1824. }
  1825. return ret;
  1826. }