init.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  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. /*
  1111. * Initialize all locks for the device. This needs to be as early as
  1112. * possible so locks are usable.
  1113. */
  1114. spin_lock_init(&dd->sc_lock);
  1115. spin_lock_init(&dd->sendctrl_lock);
  1116. spin_lock_init(&dd->rcvctrl_lock);
  1117. spin_lock_init(&dd->uctxt_lock);
  1118. spin_lock_init(&dd->hfi1_diag_trans_lock);
  1119. spin_lock_init(&dd->sc_init_lock);
  1120. spin_lock_init(&dd->dc8051_memlock);
  1121. seqlock_init(&dd->sc2vl_lock);
  1122. spin_lock_init(&dd->sde_map_lock);
  1123. spin_lock_init(&dd->pio_map_lock);
  1124. mutex_init(&dd->dc8051_lock);
  1125. init_waitqueue_head(&dd->event_queue);
  1126. dd->int_counter = alloc_percpu(u64);
  1127. if (!dd->int_counter) {
  1128. ret = -ENOMEM;
  1129. goto bail;
  1130. }
  1131. dd->rcv_limit = alloc_percpu(u64);
  1132. if (!dd->rcv_limit) {
  1133. ret = -ENOMEM;
  1134. goto bail;
  1135. }
  1136. dd->send_schedule = alloc_percpu(u64);
  1137. if (!dd->send_schedule) {
  1138. ret = -ENOMEM;
  1139. goto bail;
  1140. }
  1141. dd->tx_opstats = alloc_percpu(struct hfi1_opcode_stats_perctx);
  1142. if (!dd->tx_opstats) {
  1143. ret = -ENOMEM;
  1144. goto bail;
  1145. }
  1146. kobject_init(&dd->kobj, &hfi1_devdata_type);
  1147. return dd;
  1148. bail:
  1149. if (!list_empty(&dd->list))
  1150. list_del_init(&dd->list);
  1151. rvt_dealloc_device(&dd->verbs_dev.rdi);
  1152. return ERR_PTR(ret);
  1153. }
  1154. /*
  1155. * Called from freeze mode handlers, and from PCI error
  1156. * reporting code. Should be paranoid about state of
  1157. * system and data structures.
  1158. */
  1159. void hfi1_disable_after_error(struct hfi1_devdata *dd)
  1160. {
  1161. if (dd->flags & HFI1_INITTED) {
  1162. u32 pidx;
  1163. dd->flags &= ~HFI1_INITTED;
  1164. if (dd->pport)
  1165. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  1166. struct hfi1_pportdata *ppd;
  1167. ppd = dd->pport + pidx;
  1168. if (dd->flags & HFI1_PRESENT)
  1169. set_link_state(ppd, HLS_DN_DISABLE);
  1170. if (ppd->statusp)
  1171. *ppd->statusp &= ~HFI1_STATUS_IB_READY;
  1172. }
  1173. }
  1174. /*
  1175. * Mark as having had an error for driver, and also
  1176. * for /sys and status word mapped to user programs.
  1177. * This marks unit as not usable, until reset.
  1178. */
  1179. if (dd->status)
  1180. dd->status->dev |= HFI1_STATUS_HWERROR;
  1181. }
  1182. static void remove_one(struct pci_dev *);
  1183. static int init_one(struct pci_dev *, const struct pci_device_id *);
  1184. #define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
  1185. #define PFX DRIVER_NAME ": "
  1186. const struct pci_device_id hfi1_pci_tbl[] = {
  1187. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
  1188. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
  1189. { 0, }
  1190. };
  1191. MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
  1192. static struct pci_driver hfi1_pci_driver = {
  1193. .name = DRIVER_NAME,
  1194. .probe = init_one,
  1195. .remove = remove_one,
  1196. .id_table = hfi1_pci_tbl,
  1197. .err_handler = &hfi1_pci_err_handler,
  1198. };
  1199. static void __init compute_krcvqs(void)
  1200. {
  1201. int i;
  1202. for (i = 0; i < krcvqsset; i++)
  1203. n_krcvqs += krcvqs[i];
  1204. }
  1205. /*
  1206. * Do all the generic driver unit- and chip-independent memory
  1207. * allocation and initialization.
  1208. */
  1209. static int __init hfi1_mod_init(void)
  1210. {
  1211. int ret;
  1212. ret = dev_init();
  1213. if (ret)
  1214. goto bail;
  1215. ret = node_affinity_init();
  1216. if (ret)
  1217. goto bail;
  1218. /* validate max MTU before any devices start */
  1219. if (!valid_opa_max_mtu(hfi1_max_mtu)) {
  1220. pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
  1221. hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
  1222. hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
  1223. }
  1224. /* valid CUs run from 1-128 in powers of 2 */
  1225. if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
  1226. hfi1_cu = 1;
  1227. /* valid credit return threshold is 0-100, variable is unsigned */
  1228. if (user_credit_return_threshold > 100)
  1229. user_credit_return_threshold = 100;
  1230. compute_krcvqs();
  1231. /*
  1232. * sanitize receive interrupt count, time must wait until after
  1233. * the hardware type is known
  1234. */
  1235. if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
  1236. rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
  1237. /* reject invalid combinations */
  1238. if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
  1239. pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
  1240. rcv_intr_count = 1;
  1241. }
  1242. if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
  1243. /*
  1244. * Avoid indefinite packet delivery by requiring a timeout
  1245. * if count is > 1.
  1246. */
  1247. pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
  1248. rcv_intr_timeout = 1;
  1249. }
  1250. if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
  1251. /*
  1252. * The dynamic algorithm expects a non-zero timeout
  1253. * and a count > 1.
  1254. */
  1255. pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
  1256. rcv_intr_dynamic = 0;
  1257. }
  1258. /* sanitize link CRC options */
  1259. link_crc_mask &= SUPPORTED_CRCS;
  1260. /*
  1261. * These must be called before the driver is registered with
  1262. * the PCI subsystem.
  1263. */
  1264. idr_init(&hfi1_unit_table);
  1265. hfi1_dbg_init();
  1266. ret = hfi1_wss_init();
  1267. if (ret < 0)
  1268. goto bail_wss;
  1269. ret = pci_register_driver(&hfi1_pci_driver);
  1270. if (ret < 0) {
  1271. pr_err("Unable to register driver: error %d\n", -ret);
  1272. goto bail_dev;
  1273. }
  1274. goto bail; /* all OK */
  1275. bail_dev:
  1276. hfi1_wss_exit();
  1277. bail_wss:
  1278. hfi1_dbg_exit();
  1279. idr_destroy(&hfi1_unit_table);
  1280. dev_cleanup();
  1281. bail:
  1282. return ret;
  1283. }
  1284. module_init(hfi1_mod_init);
  1285. /*
  1286. * Do the non-unit driver cleanup, memory free, etc. at unload.
  1287. */
  1288. static void __exit hfi1_mod_cleanup(void)
  1289. {
  1290. pci_unregister_driver(&hfi1_pci_driver);
  1291. node_affinity_destroy();
  1292. hfi1_wss_exit();
  1293. hfi1_dbg_exit();
  1294. idr_destroy(&hfi1_unit_table);
  1295. dispose_firmware(); /* asymmetric with obtain_firmware() */
  1296. dev_cleanup();
  1297. }
  1298. module_exit(hfi1_mod_cleanup);
  1299. /* this can only be called after a successful initialization */
  1300. static void cleanup_device_data(struct hfi1_devdata *dd)
  1301. {
  1302. int ctxt;
  1303. int pidx;
  1304. /* users can't do anything more with chip */
  1305. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  1306. struct hfi1_pportdata *ppd = &dd->pport[pidx];
  1307. struct cc_state *cc_state;
  1308. int i;
  1309. if (ppd->statusp)
  1310. *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
  1311. for (i = 0; i < OPA_MAX_SLS; i++)
  1312. hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
  1313. spin_lock(&ppd->cc_state_lock);
  1314. cc_state = get_cc_state_protected(ppd);
  1315. RCU_INIT_POINTER(ppd->cc_state, NULL);
  1316. spin_unlock(&ppd->cc_state_lock);
  1317. if (cc_state)
  1318. kfree_rcu(cc_state, rcu);
  1319. }
  1320. free_credit_return(dd);
  1321. if (dd->rcvhdrtail_dummy_kvaddr) {
  1322. dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
  1323. (void *)dd->rcvhdrtail_dummy_kvaddr,
  1324. dd->rcvhdrtail_dummy_dma);
  1325. dd->rcvhdrtail_dummy_kvaddr = NULL;
  1326. }
  1327. /*
  1328. * Free any resources still in use (usually just kernel contexts)
  1329. * at unload; we do for ctxtcnt, because that's what we allocate.
  1330. */
  1331. for (ctxt = 0; dd->rcd && ctxt < dd->num_rcv_contexts; ctxt++) {
  1332. struct hfi1_ctxtdata *rcd = dd->rcd[ctxt];
  1333. if (rcd) {
  1334. hfi1_clear_tids(rcd);
  1335. hfi1_free_ctxt(rcd);
  1336. }
  1337. }
  1338. kfree(dd->rcd);
  1339. dd->rcd = NULL;
  1340. free_pio_map(dd);
  1341. /* must follow rcv context free - need to remove rcv's hooks */
  1342. for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
  1343. sc_free(dd->send_contexts[ctxt].sc);
  1344. dd->num_send_contexts = 0;
  1345. kfree(dd->send_contexts);
  1346. dd->send_contexts = NULL;
  1347. kfree(dd->hw_to_sw);
  1348. dd->hw_to_sw = NULL;
  1349. kfree(dd->boardname);
  1350. vfree(dd->events);
  1351. vfree(dd->status);
  1352. }
  1353. /*
  1354. * Clean up on unit shutdown, or error during unit load after
  1355. * successful initialization.
  1356. */
  1357. static void postinit_cleanup(struct hfi1_devdata *dd)
  1358. {
  1359. hfi1_start_cleanup(dd);
  1360. hfi1_pcie_ddcleanup(dd);
  1361. hfi1_pcie_cleanup(dd->pcidev);
  1362. cleanup_device_data(dd);
  1363. hfi1_free_devdata(dd);
  1364. }
  1365. static int init_validate_rcvhdrcnt(struct device *dev, uint thecnt)
  1366. {
  1367. if (thecnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) {
  1368. hfi1_early_err(dev, "Receive header queue count too small\n");
  1369. return -EINVAL;
  1370. }
  1371. if (thecnt > HFI1_MAX_HDRQ_EGRBUF_CNT) {
  1372. hfi1_early_err(dev,
  1373. "Receive header queue count cannot be greater than %u\n",
  1374. HFI1_MAX_HDRQ_EGRBUF_CNT);
  1375. return -EINVAL;
  1376. }
  1377. if (thecnt % HDRQ_INCREMENT) {
  1378. hfi1_early_err(dev, "Receive header queue count %d must be divisible by %lu\n",
  1379. thecnt, HDRQ_INCREMENT);
  1380. return -EINVAL;
  1381. }
  1382. return 0;
  1383. }
  1384. static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1385. {
  1386. int ret = 0, j, pidx, initfail;
  1387. struct hfi1_devdata *dd;
  1388. struct hfi1_pportdata *ppd;
  1389. /* First, lock the non-writable module parameters */
  1390. HFI1_CAP_LOCK();
  1391. /* Validate dev ids */
  1392. if (!(ent->device == PCI_DEVICE_ID_INTEL0 ||
  1393. ent->device == PCI_DEVICE_ID_INTEL1)) {
  1394. hfi1_early_err(&pdev->dev,
  1395. "Failing on unknown Intel deviceid 0x%x\n",
  1396. ent->device);
  1397. ret = -ENODEV;
  1398. goto bail;
  1399. }
  1400. /* Validate some global module parameters */
  1401. ret = init_validate_rcvhdrcnt(&pdev->dev, rcvhdrcnt);
  1402. if (ret)
  1403. goto bail;
  1404. /* use the encoding function as a sanitization check */
  1405. if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
  1406. hfi1_early_err(&pdev->dev, "Invalid HdrQ Entry size %u\n",
  1407. hfi1_hdrq_entsize);
  1408. ret = -EINVAL;
  1409. goto bail;
  1410. }
  1411. /* The receive eager buffer size must be set before the receive
  1412. * contexts are created.
  1413. *
  1414. * Set the eager buffer size. Validate that it falls in a range
  1415. * allowed by the hardware - all powers of 2 between the min and
  1416. * max. The maximum valid MTU is within the eager buffer range
  1417. * so we do not need to cap the max_mtu by an eager buffer size
  1418. * setting.
  1419. */
  1420. if (eager_buffer_size) {
  1421. if (!is_power_of_2(eager_buffer_size))
  1422. eager_buffer_size =
  1423. roundup_pow_of_two(eager_buffer_size);
  1424. eager_buffer_size =
  1425. clamp_val(eager_buffer_size,
  1426. MIN_EAGER_BUFFER * 8,
  1427. MAX_EAGER_BUFFER_TOTAL);
  1428. hfi1_early_info(&pdev->dev, "Eager buffer size %u\n",
  1429. eager_buffer_size);
  1430. } else {
  1431. hfi1_early_err(&pdev->dev, "Invalid Eager buffer size of 0\n");
  1432. ret = -EINVAL;
  1433. goto bail;
  1434. }
  1435. /* restrict value of hfi1_rcvarr_split */
  1436. hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
  1437. ret = hfi1_pcie_init(pdev, ent);
  1438. if (ret)
  1439. goto bail;
  1440. /*
  1441. * Do device-specific initialization, function table setup, dd
  1442. * allocation, etc.
  1443. */
  1444. dd = hfi1_init_dd(pdev, ent);
  1445. if (IS_ERR(dd)) {
  1446. ret = PTR_ERR(dd);
  1447. goto clean_bail; /* error already printed */
  1448. }
  1449. ret = create_workqueues(dd);
  1450. if (ret)
  1451. goto clean_bail;
  1452. /* do the generic initialization */
  1453. initfail = hfi1_init(dd, 0);
  1454. /* setup vnic */
  1455. hfi1_vnic_setup(dd);
  1456. ret = hfi1_register_ib_device(dd);
  1457. /*
  1458. * Now ready for use. this should be cleared whenever we
  1459. * detect a reset, or initiate one. If earlier failure,
  1460. * we still create devices, so diags, etc. can be used
  1461. * to determine cause of problem.
  1462. */
  1463. if (!initfail && !ret) {
  1464. dd->flags |= HFI1_INITTED;
  1465. /* create debufs files after init and ib register */
  1466. hfi1_dbg_ibdev_init(&dd->verbs_dev);
  1467. }
  1468. j = hfi1_device_create(dd);
  1469. if (j)
  1470. dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
  1471. if (initfail || ret) {
  1472. stop_timers(dd);
  1473. flush_workqueue(ib_wq);
  1474. for (pidx = 0; pidx < dd->num_pports; ++pidx) {
  1475. hfi1_quiet_serdes(dd->pport + pidx);
  1476. ppd = dd->pport + pidx;
  1477. if (ppd->hfi1_wq) {
  1478. destroy_workqueue(ppd->hfi1_wq);
  1479. ppd->hfi1_wq = NULL;
  1480. }
  1481. if (ppd->link_wq) {
  1482. destroy_workqueue(ppd->link_wq);
  1483. ppd->link_wq = NULL;
  1484. }
  1485. }
  1486. if (!j)
  1487. hfi1_device_remove(dd);
  1488. if (!ret)
  1489. hfi1_unregister_ib_device(dd);
  1490. hfi1_vnic_cleanup(dd);
  1491. postinit_cleanup(dd);
  1492. if (initfail)
  1493. ret = initfail;
  1494. goto bail; /* everything already cleaned */
  1495. }
  1496. sdma_start(dd);
  1497. return 0;
  1498. clean_bail:
  1499. hfi1_pcie_cleanup(pdev);
  1500. bail:
  1501. return ret;
  1502. }
  1503. static void wait_for_clients(struct hfi1_devdata *dd)
  1504. {
  1505. /*
  1506. * Remove the device init value and complete the device if there is
  1507. * no clients or wait for active clients to finish.
  1508. */
  1509. if (atomic_dec_and_test(&dd->user_refcount))
  1510. complete(&dd->user_comp);
  1511. wait_for_completion(&dd->user_comp);
  1512. }
  1513. static void remove_one(struct pci_dev *pdev)
  1514. {
  1515. struct hfi1_devdata *dd = pci_get_drvdata(pdev);
  1516. /* close debugfs files before ib unregister */
  1517. hfi1_dbg_ibdev_exit(&dd->verbs_dev);
  1518. /* remove the /dev hfi1 interface */
  1519. hfi1_device_remove(dd);
  1520. /* wait for existing user space clients to finish */
  1521. wait_for_clients(dd);
  1522. /* unregister from IB core */
  1523. hfi1_unregister_ib_device(dd);
  1524. /* cleanup vnic */
  1525. hfi1_vnic_cleanup(dd);
  1526. /*
  1527. * Disable the IB link, disable interrupts on the device,
  1528. * clear dma engines, etc.
  1529. */
  1530. shutdown_device(dd);
  1531. stop_timers(dd);
  1532. /* wait until all of our (qsfp) queue_work() calls complete */
  1533. flush_workqueue(ib_wq);
  1534. postinit_cleanup(dd);
  1535. }
  1536. /**
  1537. * hfi1_create_rcvhdrq - create a receive header queue
  1538. * @dd: the hfi1_ib device
  1539. * @rcd: the context data
  1540. *
  1541. * This must be contiguous memory (from an i/o perspective), and must be
  1542. * DMA'able (which means for some systems, it will go through an IOMMU,
  1543. * or be forced into a low address range).
  1544. */
  1545. int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
  1546. {
  1547. unsigned amt;
  1548. u64 reg;
  1549. if (!rcd->rcvhdrq) {
  1550. dma_addr_t dma_hdrqtail;
  1551. gfp_t gfp_flags;
  1552. /*
  1553. * rcvhdrqentsize is in DWs, so we have to convert to bytes
  1554. * (* sizeof(u32)).
  1555. */
  1556. amt = PAGE_ALIGN(rcd->rcvhdrq_cnt * rcd->rcvhdrqentsize *
  1557. sizeof(u32));
  1558. if (rcd->ctxt < dd->first_dyn_alloc_ctxt || rcd->is_vnic)
  1559. gfp_flags = GFP_KERNEL;
  1560. else
  1561. gfp_flags = GFP_USER;
  1562. rcd->rcvhdrq = dma_zalloc_coherent(
  1563. &dd->pcidev->dev, amt, &rcd->rcvhdrq_dma,
  1564. gfp_flags | __GFP_COMP);
  1565. if (!rcd->rcvhdrq) {
  1566. dd_dev_err(dd,
  1567. "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
  1568. amt, rcd->ctxt);
  1569. goto bail;
  1570. }
  1571. if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
  1572. rcd->rcvhdrtail_kvaddr = dma_zalloc_coherent(
  1573. &dd->pcidev->dev, PAGE_SIZE, &dma_hdrqtail,
  1574. gfp_flags);
  1575. if (!rcd->rcvhdrtail_kvaddr)
  1576. goto bail_free;
  1577. rcd->rcvhdrqtailaddr_dma = dma_hdrqtail;
  1578. }
  1579. rcd->rcvhdrq_size = amt;
  1580. }
  1581. /*
  1582. * These values are per-context:
  1583. * RcvHdrCnt
  1584. * RcvHdrEntSize
  1585. * RcvHdrSize
  1586. */
  1587. reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT)
  1588. & RCV_HDR_CNT_CNT_MASK)
  1589. << RCV_HDR_CNT_CNT_SHIFT;
  1590. write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg);
  1591. reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize)
  1592. & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK)
  1593. << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT;
  1594. write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg);
  1595. reg = (dd->rcvhdrsize & RCV_HDR_SIZE_HDR_SIZE_MASK)
  1596. << RCV_HDR_SIZE_HDR_SIZE_SHIFT;
  1597. write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg);
  1598. /*
  1599. * Program dummy tail address for every receive context
  1600. * before enabling any receive context
  1601. */
  1602. write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR,
  1603. dd->rcvhdrtail_dummy_dma);
  1604. return 0;
  1605. bail_free:
  1606. dd_dev_err(dd,
  1607. "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
  1608. rcd->ctxt);
  1609. dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
  1610. rcd->rcvhdrq_dma);
  1611. rcd->rcvhdrq = NULL;
  1612. bail:
  1613. return -ENOMEM;
  1614. }
  1615. /**
  1616. * allocate eager buffers, both kernel and user contexts.
  1617. * @rcd: the context we are setting up.
  1618. *
  1619. * Allocate the eager TID buffers and program them into hip.
  1620. * They are no longer completely contiguous, we do multiple allocation
  1621. * calls. Otherwise we get the OOM code involved, by asking for too
  1622. * much per call, with disastrous results on some kernels.
  1623. */
  1624. int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
  1625. {
  1626. struct hfi1_devdata *dd = rcd->dd;
  1627. u32 max_entries, egrtop, alloced_bytes = 0, idx = 0;
  1628. gfp_t gfp_flags;
  1629. u16 order;
  1630. int ret = 0;
  1631. u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
  1632. /*
  1633. * GFP_USER, but without GFP_FS, so buffer cache can be
  1634. * coalesced (we hope); otherwise, even at order 4,
  1635. * heavy filesystem activity makes these fail, and we can
  1636. * use compound pages.
  1637. */
  1638. gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
  1639. /*
  1640. * The minimum size of the eager buffers is a groups of MTU-sized
  1641. * buffers.
  1642. * The global eager_buffer_size parameter is checked against the
  1643. * theoretical lower limit of the value. Here, we check against the
  1644. * MTU.
  1645. */
  1646. if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
  1647. rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
  1648. /*
  1649. * If using one-pkt-per-egr-buffer, lower the eager buffer
  1650. * size to the max MTU (page-aligned).
  1651. */
  1652. if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
  1653. rcd->egrbufs.rcvtid_size = round_mtu;
  1654. /*
  1655. * Eager buffers sizes of 1MB or less require smaller TID sizes
  1656. * to satisfy the "multiple of 8 RcvArray entries" requirement.
  1657. */
  1658. if (rcd->egrbufs.size <= (1 << 20))
  1659. rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
  1660. rounddown_pow_of_two(rcd->egrbufs.size / 8));
  1661. while (alloced_bytes < rcd->egrbufs.size &&
  1662. rcd->egrbufs.alloced < rcd->egrbufs.count) {
  1663. rcd->egrbufs.buffers[idx].addr =
  1664. dma_zalloc_coherent(&dd->pcidev->dev,
  1665. rcd->egrbufs.rcvtid_size,
  1666. &rcd->egrbufs.buffers[idx].dma,
  1667. gfp_flags);
  1668. if (rcd->egrbufs.buffers[idx].addr) {
  1669. rcd->egrbufs.buffers[idx].len =
  1670. rcd->egrbufs.rcvtid_size;
  1671. rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
  1672. rcd->egrbufs.buffers[idx].addr;
  1673. rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].dma =
  1674. rcd->egrbufs.buffers[idx].dma;
  1675. rcd->egrbufs.alloced++;
  1676. alloced_bytes += rcd->egrbufs.rcvtid_size;
  1677. idx++;
  1678. } else {
  1679. u32 new_size, i, j;
  1680. u64 offset = 0;
  1681. /*
  1682. * Fail the eager buffer allocation if:
  1683. * - we are already using the lowest acceptable size
  1684. * - we are using one-pkt-per-egr-buffer (this implies
  1685. * that we are accepting only one size)
  1686. */
  1687. if (rcd->egrbufs.rcvtid_size == round_mtu ||
  1688. !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
  1689. dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
  1690. rcd->ctxt);
  1691. ret = -ENOMEM;
  1692. goto bail_rcvegrbuf_phys;
  1693. }
  1694. new_size = rcd->egrbufs.rcvtid_size / 2;
  1695. /*
  1696. * If the first attempt to allocate memory failed, don't
  1697. * fail everything but continue with the next lower
  1698. * size.
  1699. */
  1700. if (idx == 0) {
  1701. rcd->egrbufs.rcvtid_size = new_size;
  1702. continue;
  1703. }
  1704. /*
  1705. * Re-partition already allocated buffers to a smaller
  1706. * size.
  1707. */
  1708. rcd->egrbufs.alloced = 0;
  1709. for (i = 0, j = 0, offset = 0; j < idx; i++) {
  1710. if (i >= rcd->egrbufs.count)
  1711. break;
  1712. rcd->egrbufs.rcvtids[i].dma =
  1713. rcd->egrbufs.buffers[j].dma + offset;
  1714. rcd->egrbufs.rcvtids[i].addr =
  1715. rcd->egrbufs.buffers[j].addr + offset;
  1716. rcd->egrbufs.alloced++;
  1717. if ((rcd->egrbufs.buffers[j].dma + offset +
  1718. new_size) ==
  1719. (rcd->egrbufs.buffers[j].dma +
  1720. rcd->egrbufs.buffers[j].len)) {
  1721. j++;
  1722. offset = 0;
  1723. } else {
  1724. offset += new_size;
  1725. }
  1726. }
  1727. rcd->egrbufs.rcvtid_size = new_size;
  1728. }
  1729. }
  1730. rcd->egrbufs.numbufs = idx;
  1731. rcd->egrbufs.size = alloced_bytes;
  1732. hfi1_cdbg(PROC,
  1733. "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %zuKB\n",
  1734. rcd->ctxt, rcd->egrbufs.alloced,
  1735. rcd->egrbufs.rcvtid_size / 1024, rcd->egrbufs.size / 1024);
  1736. /*
  1737. * Set the contexts rcv array head update threshold to the closest
  1738. * power of 2 (so we can use a mask instead of modulo) below half
  1739. * the allocated entries.
  1740. */
  1741. rcd->egrbufs.threshold =
  1742. rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
  1743. /*
  1744. * Compute the expected RcvArray entry base. This is done after
  1745. * allocating the eager buffers in order to maximize the
  1746. * expected RcvArray entries for the context.
  1747. */
  1748. max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
  1749. egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
  1750. rcd->expected_count = max_entries - egrtop;
  1751. if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
  1752. rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
  1753. rcd->expected_base = rcd->eager_base + egrtop;
  1754. hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
  1755. rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
  1756. rcd->eager_base, rcd->expected_base);
  1757. if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
  1758. hfi1_cdbg(PROC,
  1759. "ctxt%u: current Eager buffer size is invalid %u\n",
  1760. rcd->ctxt, rcd->egrbufs.rcvtid_size);
  1761. ret = -EINVAL;
  1762. goto bail_rcvegrbuf_phys;
  1763. }
  1764. for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
  1765. hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
  1766. rcd->egrbufs.rcvtids[idx].dma, order);
  1767. cond_resched();
  1768. }
  1769. return 0;
  1770. bail_rcvegrbuf_phys:
  1771. for (idx = 0; idx < rcd->egrbufs.alloced &&
  1772. rcd->egrbufs.buffers[idx].addr;
  1773. idx++) {
  1774. dma_free_coherent(&dd->pcidev->dev,
  1775. rcd->egrbufs.buffers[idx].len,
  1776. rcd->egrbufs.buffers[idx].addr,
  1777. rcd->egrbufs.buffers[idx].dma);
  1778. rcd->egrbufs.buffers[idx].addr = NULL;
  1779. rcd->egrbufs.buffers[idx].dma = 0;
  1780. rcd->egrbufs.buffers[idx].len = 0;
  1781. }
  1782. return ret;
  1783. }