init.c 50 KB

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