init.c 54 KB

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