init.c 50 KB

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