init.c 55 KB

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