init.c 54 KB

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