init.c 50 KB

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