init.c 49 KB

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