init.c 54 KB

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