init.c 54 KB

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