pio.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  1. /*
  2. * Copyright(c) 2015-2018 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/delay.h>
  48. #include "hfi.h"
  49. #include "qp.h"
  50. #include "trace.h"
  51. #define SC(name) SEND_CTXT_##name
  52. /*
  53. * Send Context functions
  54. */
  55. static void sc_wait_for_packet_egress(struct send_context *sc, int pause);
  56. /*
  57. * Set the CM reset bit and wait for it to clear. Use the provided
  58. * sendctrl register. This routine has no locking.
  59. */
  60. void __cm_reset(struct hfi1_devdata *dd, u64 sendctrl)
  61. {
  62. write_csr(dd, SEND_CTRL, sendctrl | SEND_CTRL_CM_RESET_SMASK);
  63. while (1) {
  64. udelay(1);
  65. sendctrl = read_csr(dd, SEND_CTRL);
  66. if ((sendctrl & SEND_CTRL_CM_RESET_SMASK) == 0)
  67. break;
  68. }
  69. }
  70. /* global control of PIO send */
  71. void pio_send_control(struct hfi1_devdata *dd, int op)
  72. {
  73. u64 reg, mask;
  74. unsigned long flags;
  75. int write = 1; /* write sendctrl back */
  76. int flush = 0; /* re-read sendctrl to make sure it is flushed */
  77. int i;
  78. spin_lock_irqsave(&dd->sendctrl_lock, flags);
  79. reg = read_csr(dd, SEND_CTRL);
  80. switch (op) {
  81. case PSC_GLOBAL_ENABLE:
  82. reg |= SEND_CTRL_SEND_ENABLE_SMASK;
  83. /* Fall through */
  84. case PSC_DATA_VL_ENABLE:
  85. mask = 0;
  86. for (i = 0; i < ARRAY_SIZE(dd->vld); i++)
  87. if (!dd->vld[i].mtu)
  88. mask |= BIT_ULL(i);
  89. /* Disallow sending on VLs not enabled */
  90. mask = (mask & SEND_CTRL_UNSUPPORTED_VL_MASK) <<
  91. SEND_CTRL_UNSUPPORTED_VL_SHIFT;
  92. reg = (reg & ~SEND_CTRL_UNSUPPORTED_VL_SMASK) | mask;
  93. break;
  94. case PSC_GLOBAL_DISABLE:
  95. reg &= ~SEND_CTRL_SEND_ENABLE_SMASK;
  96. break;
  97. case PSC_GLOBAL_VLARB_ENABLE:
  98. reg |= SEND_CTRL_VL_ARBITER_ENABLE_SMASK;
  99. break;
  100. case PSC_GLOBAL_VLARB_DISABLE:
  101. reg &= ~SEND_CTRL_VL_ARBITER_ENABLE_SMASK;
  102. break;
  103. case PSC_CM_RESET:
  104. __cm_reset(dd, reg);
  105. write = 0; /* CSR already written (and flushed) */
  106. break;
  107. case PSC_DATA_VL_DISABLE:
  108. reg |= SEND_CTRL_UNSUPPORTED_VL_SMASK;
  109. flush = 1;
  110. break;
  111. default:
  112. dd_dev_err(dd, "%s: invalid control %d\n", __func__, op);
  113. break;
  114. }
  115. if (write) {
  116. write_csr(dd, SEND_CTRL, reg);
  117. if (flush)
  118. (void)read_csr(dd, SEND_CTRL); /* flush write */
  119. }
  120. spin_unlock_irqrestore(&dd->sendctrl_lock, flags);
  121. }
  122. /* number of send context memory pools */
  123. #define NUM_SC_POOLS 2
  124. /* Send Context Size (SCS) wildcards */
  125. #define SCS_POOL_0 -1
  126. #define SCS_POOL_1 -2
  127. /* Send Context Count (SCC) wildcards */
  128. #define SCC_PER_VL -1
  129. #define SCC_PER_CPU -2
  130. #define SCC_PER_KRCVQ -3
  131. /* Send Context Size (SCS) constants */
  132. #define SCS_ACK_CREDITS 32
  133. #define SCS_VL15_CREDITS 102 /* 3 pkts of 2048B data + 128B header */
  134. #define PIO_THRESHOLD_CEILING 4096
  135. #define PIO_WAIT_BATCH_SIZE 5
  136. /* default send context sizes */
  137. static struct sc_config_sizes sc_config_sizes[SC_MAX] = {
  138. [SC_KERNEL] = { .size = SCS_POOL_0, /* even divide, pool 0 */
  139. .count = SCC_PER_VL }, /* one per NUMA */
  140. [SC_ACK] = { .size = SCS_ACK_CREDITS,
  141. .count = SCC_PER_KRCVQ },
  142. [SC_USER] = { .size = SCS_POOL_0, /* even divide, pool 0 */
  143. .count = SCC_PER_CPU }, /* one per CPU */
  144. [SC_VL15] = { .size = SCS_VL15_CREDITS,
  145. .count = 1 },
  146. };
  147. /* send context memory pool configuration */
  148. struct mem_pool_config {
  149. int centipercent; /* % of memory, in 100ths of 1% */
  150. int absolute_blocks; /* absolute block count */
  151. };
  152. /* default memory pool configuration: 100% in pool 0 */
  153. static struct mem_pool_config sc_mem_pool_config[NUM_SC_POOLS] = {
  154. /* centi%, abs blocks */
  155. { 10000, -1 }, /* pool 0 */
  156. { 0, -1 }, /* pool 1 */
  157. };
  158. /* memory pool information, used when calculating final sizes */
  159. struct mem_pool_info {
  160. int centipercent; /*
  161. * 100th of 1% of memory to use, -1 if blocks
  162. * already set
  163. */
  164. int count; /* count of contexts in the pool */
  165. int blocks; /* block size of the pool */
  166. int size; /* context size, in blocks */
  167. };
  168. /*
  169. * Convert a pool wildcard to a valid pool index. The wildcards
  170. * start at -1 and increase negatively. Map them as:
  171. * -1 => 0
  172. * -2 => 1
  173. * etc.
  174. *
  175. * Return -1 on non-wildcard input, otherwise convert to a pool number.
  176. */
  177. static int wildcard_to_pool(int wc)
  178. {
  179. if (wc >= 0)
  180. return -1; /* non-wildcard */
  181. return -wc - 1;
  182. }
  183. static const char *sc_type_names[SC_MAX] = {
  184. "kernel",
  185. "ack",
  186. "user",
  187. "vl15"
  188. };
  189. static const char *sc_type_name(int index)
  190. {
  191. if (index < 0 || index >= SC_MAX)
  192. return "unknown";
  193. return sc_type_names[index];
  194. }
  195. /*
  196. * Read the send context memory pool configuration and send context
  197. * size configuration. Replace any wildcards and come up with final
  198. * counts and sizes for the send context types.
  199. */
  200. int init_sc_pools_and_sizes(struct hfi1_devdata *dd)
  201. {
  202. struct mem_pool_info mem_pool_info[NUM_SC_POOLS] = { { 0 } };
  203. int total_blocks = (chip_pio_mem_size(dd) / PIO_BLOCK_SIZE) - 1;
  204. int total_contexts = 0;
  205. int fixed_blocks;
  206. int pool_blocks;
  207. int used_blocks;
  208. int cp_total; /* centipercent total */
  209. int ab_total; /* absolute block total */
  210. int extra;
  211. int i;
  212. /*
  213. * When SDMA is enabled, kernel context pio packet size is capped by
  214. * "piothreshold". Reduce pio buffer allocation for kernel context by
  215. * setting it to a fixed size. The allocation allows 3-deep buffering
  216. * of the largest pio packets plus up to 128 bytes header, sufficient
  217. * to maintain verbs performance.
  218. *
  219. * When SDMA is disabled, keep the default pooling allocation.
  220. */
  221. if (HFI1_CAP_IS_KSET(SDMA)) {
  222. u16 max_pkt_size = (piothreshold < PIO_THRESHOLD_CEILING) ?
  223. piothreshold : PIO_THRESHOLD_CEILING;
  224. sc_config_sizes[SC_KERNEL].size =
  225. 3 * (max_pkt_size + 128) / PIO_BLOCK_SIZE;
  226. }
  227. /*
  228. * Step 0:
  229. * - copy the centipercents/absolute sizes from the pool config
  230. * - sanity check these values
  231. * - add up centipercents, then later check for full value
  232. * - add up absolute blocks, then later check for over-commit
  233. */
  234. cp_total = 0;
  235. ab_total = 0;
  236. for (i = 0; i < NUM_SC_POOLS; i++) {
  237. int cp = sc_mem_pool_config[i].centipercent;
  238. int ab = sc_mem_pool_config[i].absolute_blocks;
  239. /*
  240. * A negative value is "unused" or "invalid". Both *can*
  241. * be valid, but centipercent wins, so check that first
  242. */
  243. if (cp >= 0) { /* centipercent valid */
  244. cp_total += cp;
  245. } else if (ab >= 0) { /* absolute blocks valid */
  246. ab_total += ab;
  247. } else { /* neither valid */
  248. dd_dev_err(
  249. dd,
  250. "Send context memory pool %d: both the block count and centipercent are invalid\n",
  251. i);
  252. return -EINVAL;
  253. }
  254. mem_pool_info[i].centipercent = cp;
  255. mem_pool_info[i].blocks = ab;
  256. }
  257. /* do not use both % and absolute blocks for different pools */
  258. if (cp_total != 0 && ab_total != 0) {
  259. dd_dev_err(
  260. dd,
  261. "All send context memory pools must be described as either centipercent or blocks, no mixing between pools\n");
  262. return -EINVAL;
  263. }
  264. /* if any percentages are present, they must add up to 100% x 100 */
  265. if (cp_total != 0 && cp_total != 10000) {
  266. dd_dev_err(
  267. dd,
  268. "Send context memory pool centipercent is %d, expecting 10000\n",
  269. cp_total);
  270. return -EINVAL;
  271. }
  272. /* the absolute pool total cannot be more than the mem total */
  273. if (ab_total > total_blocks) {
  274. dd_dev_err(
  275. dd,
  276. "Send context memory pool absolute block count %d is larger than the memory size %d\n",
  277. ab_total, total_blocks);
  278. return -EINVAL;
  279. }
  280. /*
  281. * Step 2:
  282. * - copy from the context size config
  283. * - replace context type wildcard counts with real values
  284. * - add up non-memory pool block sizes
  285. * - add up memory pool user counts
  286. */
  287. fixed_blocks = 0;
  288. for (i = 0; i < SC_MAX; i++) {
  289. int count = sc_config_sizes[i].count;
  290. int size = sc_config_sizes[i].size;
  291. int pool;
  292. /*
  293. * Sanity check count: Either a positive value or
  294. * one of the expected wildcards is valid. The positive
  295. * value is checked later when we compare against total
  296. * memory available.
  297. */
  298. if (i == SC_ACK) {
  299. count = dd->n_krcv_queues;
  300. } else if (i == SC_KERNEL) {
  301. count = INIT_SC_PER_VL * num_vls;
  302. } else if (count == SCC_PER_CPU) {
  303. count = dd->num_rcv_contexts - dd->n_krcv_queues;
  304. } else if (count < 0) {
  305. dd_dev_err(
  306. dd,
  307. "%s send context invalid count wildcard %d\n",
  308. sc_type_name(i), count);
  309. return -EINVAL;
  310. }
  311. if (total_contexts + count > chip_send_contexts(dd))
  312. count = chip_send_contexts(dd) - total_contexts;
  313. total_contexts += count;
  314. /*
  315. * Sanity check pool: The conversion will return a pool
  316. * number or -1 if a fixed (non-negative) value. The fixed
  317. * value is checked later when we compare against
  318. * total memory available.
  319. */
  320. pool = wildcard_to_pool(size);
  321. if (pool == -1) { /* non-wildcard */
  322. fixed_blocks += size * count;
  323. } else if (pool < NUM_SC_POOLS) { /* valid wildcard */
  324. mem_pool_info[pool].count += count;
  325. } else { /* invalid wildcard */
  326. dd_dev_err(
  327. dd,
  328. "%s send context invalid pool wildcard %d\n",
  329. sc_type_name(i), size);
  330. return -EINVAL;
  331. }
  332. dd->sc_sizes[i].count = count;
  333. dd->sc_sizes[i].size = size;
  334. }
  335. if (fixed_blocks > total_blocks) {
  336. dd_dev_err(
  337. dd,
  338. "Send context fixed block count, %u, larger than total block count %u\n",
  339. fixed_blocks, total_blocks);
  340. return -EINVAL;
  341. }
  342. /* step 3: calculate the blocks in the pools, and pool context sizes */
  343. pool_blocks = total_blocks - fixed_blocks;
  344. if (ab_total > pool_blocks) {
  345. dd_dev_err(
  346. dd,
  347. "Send context fixed pool sizes, %u, larger than pool block count %u\n",
  348. ab_total, pool_blocks);
  349. return -EINVAL;
  350. }
  351. /* subtract off the fixed pool blocks */
  352. pool_blocks -= ab_total;
  353. for (i = 0; i < NUM_SC_POOLS; i++) {
  354. struct mem_pool_info *pi = &mem_pool_info[i];
  355. /* % beats absolute blocks */
  356. if (pi->centipercent >= 0)
  357. pi->blocks = (pool_blocks * pi->centipercent) / 10000;
  358. if (pi->blocks == 0 && pi->count != 0) {
  359. dd_dev_err(
  360. dd,
  361. "Send context memory pool %d has %u contexts, but no blocks\n",
  362. i, pi->count);
  363. return -EINVAL;
  364. }
  365. if (pi->count == 0) {
  366. /* warn about wasted blocks */
  367. if (pi->blocks != 0)
  368. dd_dev_err(
  369. dd,
  370. "Send context memory pool %d has %u blocks, but zero contexts\n",
  371. i, pi->blocks);
  372. pi->size = 0;
  373. } else {
  374. pi->size = pi->blocks / pi->count;
  375. }
  376. }
  377. /* step 4: fill in the context type sizes from the pool sizes */
  378. used_blocks = 0;
  379. for (i = 0; i < SC_MAX; i++) {
  380. if (dd->sc_sizes[i].size < 0) {
  381. unsigned pool = wildcard_to_pool(dd->sc_sizes[i].size);
  382. WARN_ON_ONCE(pool >= NUM_SC_POOLS);
  383. dd->sc_sizes[i].size = mem_pool_info[pool].size;
  384. }
  385. /* make sure we are not larger than what is allowed by the HW */
  386. #define PIO_MAX_BLOCKS 1024
  387. if (dd->sc_sizes[i].size > PIO_MAX_BLOCKS)
  388. dd->sc_sizes[i].size = PIO_MAX_BLOCKS;
  389. /* calculate our total usage */
  390. used_blocks += dd->sc_sizes[i].size * dd->sc_sizes[i].count;
  391. }
  392. extra = total_blocks - used_blocks;
  393. if (extra != 0)
  394. dd_dev_info(dd, "unused send context blocks: %d\n", extra);
  395. return total_contexts;
  396. }
  397. int init_send_contexts(struct hfi1_devdata *dd)
  398. {
  399. u16 base;
  400. int ret, i, j, context;
  401. ret = init_credit_return(dd);
  402. if (ret)
  403. return ret;
  404. dd->hw_to_sw = kmalloc_array(TXE_NUM_CONTEXTS, sizeof(u8),
  405. GFP_KERNEL);
  406. dd->send_contexts = kcalloc(dd->num_send_contexts,
  407. sizeof(struct send_context_info),
  408. GFP_KERNEL);
  409. if (!dd->send_contexts || !dd->hw_to_sw) {
  410. kfree(dd->hw_to_sw);
  411. kfree(dd->send_contexts);
  412. free_credit_return(dd);
  413. return -ENOMEM;
  414. }
  415. /* hardware context map starts with invalid send context indices */
  416. for (i = 0; i < TXE_NUM_CONTEXTS; i++)
  417. dd->hw_to_sw[i] = INVALID_SCI;
  418. /*
  419. * All send contexts have their credit sizes. Allocate credits
  420. * for each context one after another from the global space.
  421. */
  422. context = 0;
  423. base = 1;
  424. for (i = 0; i < SC_MAX; i++) {
  425. struct sc_config_sizes *scs = &dd->sc_sizes[i];
  426. for (j = 0; j < scs->count; j++) {
  427. struct send_context_info *sci =
  428. &dd->send_contexts[context];
  429. sci->type = i;
  430. sci->base = base;
  431. sci->credits = scs->size;
  432. context++;
  433. base += scs->size;
  434. }
  435. }
  436. return 0;
  437. }
  438. /*
  439. * Allocate a software index and hardware context of the given type.
  440. *
  441. * Must be called with dd->sc_lock held.
  442. */
  443. static int sc_hw_alloc(struct hfi1_devdata *dd, int type, u32 *sw_index,
  444. u32 *hw_context)
  445. {
  446. struct send_context_info *sci;
  447. u32 index;
  448. u32 context;
  449. for (index = 0, sci = &dd->send_contexts[0];
  450. index < dd->num_send_contexts; index++, sci++) {
  451. if (sci->type == type && sci->allocated == 0) {
  452. sci->allocated = 1;
  453. /* use a 1:1 mapping, but make them non-equal */
  454. context = chip_send_contexts(dd) - index - 1;
  455. dd->hw_to_sw[context] = index;
  456. *sw_index = index;
  457. *hw_context = context;
  458. return 0; /* success */
  459. }
  460. }
  461. dd_dev_err(dd, "Unable to locate a free type %d send context\n", type);
  462. return -ENOSPC;
  463. }
  464. /*
  465. * Free the send context given by its software index.
  466. *
  467. * Must be called with dd->sc_lock held.
  468. */
  469. static void sc_hw_free(struct hfi1_devdata *dd, u32 sw_index, u32 hw_context)
  470. {
  471. struct send_context_info *sci;
  472. sci = &dd->send_contexts[sw_index];
  473. if (!sci->allocated) {
  474. dd_dev_err(dd, "%s: sw_index %u not allocated? hw_context %u\n",
  475. __func__, sw_index, hw_context);
  476. }
  477. sci->allocated = 0;
  478. dd->hw_to_sw[hw_context] = INVALID_SCI;
  479. }
  480. /* return the base context of a context in a group */
  481. static inline u32 group_context(u32 context, u32 group)
  482. {
  483. return (context >> group) << group;
  484. }
  485. /* return the size of a group */
  486. static inline u32 group_size(u32 group)
  487. {
  488. return 1 << group;
  489. }
  490. /*
  491. * Obtain the credit return addresses, kernel virtual and bus, for the
  492. * given sc.
  493. *
  494. * To understand this routine:
  495. * o va and dma are arrays of struct credit_return. One for each physical
  496. * send context, per NUMA.
  497. * o Each send context always looks in its relative location in a struct
  498. * credit_return for its credit return.
  499. * o Each send context in a group must have its return address CSR programmed
  500. * with the same value. Use the address of the first send context in the
  501. * group.
  502. */
  503. static void cr_group_addresses(struct send_context *sc, dma_addr_t *dma)
  504. {
  505. u32 gc = group_context(sc->hw_context, sc->group);
  506. u32 index = sc->hw_context & 0x7;
  507. sc->hw_free = &sc->dd->cr_base[sc->node].va[gc].cr[index];
  508. *dma = (unsigned long)
  509. &((struct credit_return *)sc->dd->cr_base[sc->node].dma)[gc];
  510. }
  511. /*
  512. * Work queue function triggered in error interrupt routine for
  513. * kernel contexts.
  514. */
  515. static void sc_halted(struct work_struct *work)
  516. {
  517. struct send_context *sc;
  518. sc = container_of(work, struct send_context, halt_work);
  519. sc_restart(sc);
  520. }
  521. /*
  522. * Calculate PIO block threshold for this send context using the given MTU.
  523. * Trigger a return when one MTU plus optional header of credits remain.
  524. *
  525. * Parameter mtu is in bytes.
  526. * Parameter hdrqentsize is in DWORDs.
  527. *
  528. * Return value is what to write into the CSR: trigger return when
  529. * unreturned credits pass this count.
  530. */
  531. u32 sc_mtu_to_threshold(struct send_context *sc, u32 mtu, u32 hdrqentsize)
  532. {
  533. u32 release_credits;
  534. u32 threshold;
  535. /* add in the header size, then divide by the PIO block size */
  536. mtu += hdrqentsize << 2;
  537. release_credits = DIV_ROUND_UP(mtu, PIO_BLOCK_SIZE);
  538. /* check against this context's credits */
  539. if (sc->credits <= release_credits)
  540. threshold = 1;
  541. else
  542. threshold = sc->credits - release_credits;
  543. return threshold;
  544. }
  545. /*
  546. * Calculate credit threshold in terms of percent of the allocated credits.
  547. * Trigger when unreturned credits equal or exceed the percentage of the whole.
  548. *
  549. * Return value is what to write into the CSR: trigger return when
  550. * unreturned credits pass this count.
  551. */
  552. u32 sc_percent_to_threshold(struct send_context *sc, u32 percent)
  553. {
  554. return (sc->credits * percent) / 100;
  555. }
  556. /*
  557. * Set the credit return threshold.
  558. */
  559. void sc_set_cr_threshold(struct send_context *sc, u32 new_threshold)
  560. {
  561. unsigned long flags;
  562. u32 old_threshold;
  563. int force_return = 0;
  564. spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
  565. old_threshold = (sc->credit_ctrl >>
  566. SC(CREDIT_CTRL_THRESHOLD_SHIFT))
  567. & SC(CREDIT_CTRL_THRESHOLD_MASK);
  568. if (new_threshold != old_threshold) {
  569. sc->credit_ctrl =
  570. (sc->credit_ctrl
  571. & ~SC(CREDIT_CTRL_THRESHOLD_SMASK))
  572. | ((new_threshold
  573. & SC(CREDIT_CTRL_THRESHOLD_MASK))
  574. << SC(CREDIT_CTRL_THRESHOLD_SHIFT));
  575. write_kctxt_csr(sc->dd, sc->hw_context,
  576. SC(CREDIT_CTRL), sc->credit_ctrl);
  577. /* force a credit return on change to avoid a possible stall */
  578. force_return = 1;
  579. }
  580. spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
  581. if (force_return)
  582. sc_return_credits(sc);
  583. }
  584. /*
  585. * set_pio_integrity
  586. *
  587. * Set the CHECK_ENABLE register for the send context 'sc'.
  588. */
  589. void set_pio_integrity(struct send_context *sc)
  590. {
  591. struct hfi1_devdata *dd = sc->dd;
  592. u32 hw_context = sc->hw_context;
  593. int type = sc->type;
  594. write_kctxt_csr(dd, hw_context,
  595. SC(CHECK_ENABLE),
  596. hfi1_pkt_default_send_ctxt_mask(dd, type));
  597. }
  598. static u32 get_buffers_allocated(struct send_context *sc)
  599. {
  600. int cpu;
  601. u32 ret = 0;
  602. for_each_possible_cpu(cpu)
  603. ret += *per_cpu_ptr(sc->buffers_allocated, cpu);
  604. return ret;
  605. }
  606. static void reset_buffers_allocated(struct send_context *sc)
  607. {
  608. int cpu;
  609. for_each_possible_cpu(cpu)
  610. (*per_cpu_ptr(sc->buffers_allocated, cpu)) = 0;
  611. }
  612. /*
  613. * Allocate a NUMA relative send context structure of the given type along
  614. * with a HW context.
  615. */
  616. struct send_context *sc_alloc(struct hfi1_devdata *dd, int type,
  617. uint hdrqentsize, int numa)
  618. {
  619. struct send_context_info *sci;
  620. struct send_context *sc = NULL;
  621. dma_addr_t dma;
  622. unsigned long flags;
  623. u64 reg;
  624. u32 thresh;
  625. u32 sw_index;
  626. u32 hw_context;
  627. int ret;
  628. u8 opval, opmask;
  629. /* do not allocate while frozen */
  630. if (dd->flags & HFI1_FROZEN)
  631. return NULL;
  632. sc = kzalloc_node(sizeof(*sc), GFP_KERNEL, numa);
  633. if (!sc)
  634. return NULL;
  635. sc->buffers_allocated = alloc_percpu(u32);
  636. if (!sc->buffers_allocated) {
  637. kfree(sc);
  638. dd_dev_err(dd,
  639. "Cannot allocate buffers_allocated per cpu counters\n"
  640. );
  641. return NULL;
  642. }
  643. spin_lock_irqsave(&dd->sc_lock, flags);
  644. ret = sc_hw_alloc(dd, type, &sw_index, &hw_context);
  645. if (ret) {
  646. spin_unlock_irqrestore(&dd->sc_lock, flags);
  647. free_percpu(sc->buffers_allocated);
  648. kfree(sc);
  649. return NULL;
  650. }
  651. sci = &dd->send_contexts[sw_index];
  652. sci->sc = sc;
  653. sc->dd = dd;
  654. sc->node = numa;
  655. sc->type = type;
  656. spin_lock_init(&sc->alloc_lock);
  657. spin_lock_init(&sc->release_lock);
  658. spin_lock_init(&sc->credit_ctrl_lock);
  659. INIT_LIST_HEAD(&sc->piowait);
  660. INIT_WORK(&sc->halt_work, sc_halted);
  661. init_waitqueue_head(&sc->halt_wait);
  662. /* grouping is always single context for now */
  663. sc->group = 0;
  664. sc->sw_index = sw_index;
  665. sc->hw_context = hw_context;
  666. cr_group_addresses(sc, &dma);
  667. sc->credits = sci->credits;
  668. sc->size = sc->credits * PIO_BLOCK_SIZE;
  669. /* PIO Send Memory Address details */
  670. #define PIO_ADDR_CONTEXT_MASK 0xfful
  671. #define PIO_ADDR_CONTEXT_SHIFT 16
  672. sc->base_addr = dd->piobase + ((hw_context & PIO_ADDR_CONTEXT_MASK)
  673. << PIO_ADDR_CONTEXT_SHIFT);
  674. /* set base and credits */
  675. reg = ((sci->credits & SC(CTRL_CTXT_DEPTH_MASK))
  676. << SC(CTRL_CTXT_DEPTH_SHIFT))
  677. | ((sci->base & SC(CTRL_CTXT_BASE_MASK))
  678. << SC(CTRL_CTXT_BASE_SHIFT));
  679. write_kctxt_csr(dd, hw_context, SC(CTRL), reg);
  680. set_pio_integrity(sc);
  681. /* unmask all errors */
  682. write_kctxt_csr(dd, hw_context, SC(ERR_MASK), (u64)-1);
  683. /* set the default partition key */
  684. write_kctxt_csr(dd, hw_context, SC(CHECK_PARTITION_KEY),
  685. (SC(CHECK_PARTITION_KEY_VALUE_MASK) &
  686. DEFAULT_PKEY) <<
  687. SC(CHECK_PARTITION_KEY_VALUE_SHIFT));
  688. /* per context type checks */
  689. if (type == SC_USER) {
  690. opval = USER_OPCODE_CHECK_VAL;
  691. opmask = USER_OPCODE_CHECK_MASK;
  692. } else {
  693. opval = OPCODE_CHECK_VAL_DISABLED;
  694. opmask = OPCODE_CHECK_MASK_DISABLED;
  695. }
  696. /* set the send context check opcode mask and value */
  697. write_kctxt_csr(dd, hw_context, SC(CHECK_OPCODE),
  698. ((u64)opmask << SC(CHECK_OPCODE_MASK_SHIFT)) |
  699. ((u64)opval << SC(CHECK_OPCODE_VALUE_SHIFT)));
  700. /* set up credit return */
  701. reg = dma & SC(CREDIT_RETURN_ADDR_ADDRESS_SMASK);
  702. write_kctxt_csr(dd, hw_context, SC(CREDIT_RETURN_ADDR), reg);
  703. /*
  704. * Calculate the initial credit return threshold.
  705. *
  706. * For Ack contexts, set a threshold for half the credits.
  707. * For User contexts use the given percentage. This has been
  708. * sanitized on driver start-up.
  709. * For Kernel contexts, use the default MTU plus a header
  710. * or half the credits, whichever is smaller. This should
  711. * work for both the 3-deep buffering allocation and the
  712. * pooling allocation.
  713. */
  714. if (type == SC_ACK) {
  715. thresh = sc_percent_to_threshold(sc, 50);
  716. } else if (type == SC_USER) {
  717. thresh = sc_percent_to_threshold(sc,
  718. user_credit_return_threshold);
  719. } else { /* kernel */
  720. thresh = min(sc_percent_to_threshold(sc, 50),
  721. sc_mtu_to_threshold(sc, hfi1_max_mtu,
  722. hdrqentsize));
  723. }
  724. reg = thresh << SC(CREDIT_CTRL_THRESHOLD_SHIFT);
  725. /* add in early return */
  726. if (type == SC_USER && HFI1_CAP_IS_USET(EARLY_CREDIT_RETURN))
  727. reg |= SC(CREDIT_CTRL_EARLY_RETURN_SMASK);
  728. else if (HFI1_CAP_IS_KSET(EARLY_CREDIT_RETURN)) /* kernel, ack */
  729. reg |= SC(CREDIT_CTRL_EARLY_RETURN_SMASK);
  730. /* set up write-through credit_ctrl */
  731. sc->credit_ctrl = reg;
  732. write_kctxt_csr(dd, hw_context, SC(CREDIT_CTRL), reg);
  733. /* User send contexts should not allow sending on VL15 */
  734. if (type == SC_USER) {
  735. reg = 1ULL << 15;
  736. write_kctxt_csr(dd, hw_context, SC(CHECK_VL), reg);
  737. }
  738. spin_unlock_irqrestore(&dd->sc_lock, flags);
  739. /*
  740. * Allocate shadow ring to track outstanding PIO buffers _after_
  741. * unlocking. We don't know the size until the lock is held and
  742. * we can't allocate while the lock is held. No one is using
  743. * the context yet, so allocate it now.
  744. *
  745. * User contexts do not get a shadow ring.
  746. */
  747. if (type != SC_USER) {
  748. /*
  749. * Size the shadow ring 1 larger than the number of credits
  750. * so head == tail can mean empty.
  751. */
  752. sc->sr_size = sci->credits + 1;
  753. sc->sr = kcalloc_node(sc->sr_size,
  754. sizeof(union pio_shadow_ring),
  755. GFP_KERNEL, numa);
  756. if (!sc->sr) {
  757. sc_free(sc);
  758. return NULL;
  759. }
  760. }
  761. hfi1_cdbg(PIO,
  762. "Send context %u(%u) %s group %u credits %u credit_ctrl 0x%llx threshold %u\n",
  763. sw_index,
  764. hw_context,
  765. sc_type_name(type),
  766. sc->group,
  767. sc->credits,
  768. sc->credit_ctrl,
  769. thresh);
  770. return sc;
  771. }
  772. /* free a per-NUMA send context structure */
  773. void sc_free(struct send_context *sc)
  774. {
  775. struct hfi1_devdata *dd;
  776. unsigned long flags;
  777. u32 sw_index;
  778. u32 hw_context;
  779. if (!sc)
  780. return;
  781. sc->flags |= SCF_IN_FREE; /* ensure no restarts */
  782. dd = sc->dd;
  783. if (!list_empty(&sc->piowait))
  784. dd_dev_err(dd, "piowait list not empty!\n");
  785. sw_index = sc->sw_index;
  786. hw_context = sc->hw_context;
  787. sc_disable(sc); /* make sure the HW is disabled */
  788. flush_work(&sc->halt_work);
  789. spin_lock_irqsave(&dd->sc_lock, flags);
  790. dd->send_contexts[sw_index].sc = NULL;
  791. /* clear/disable all registers set in sc_alloc */
  792. write_kctxt_csr(dd, hw_context, SC(CTRL), 0);
  793. write_kctxt_csr(dd, hw_context, SC(CHECK_ENABLE), 0);
  794. write_kctxt_csr(dd, hw_context, SC(ERR_MASK), 0);
  795. write_kctxt_csr(dd, hw_context, SC(CHECK_PARTITION_KEY), 0);
  796. write_kctxt_csr(dd, hw_context, SC(CHECK_OPCODE), 0);
  797. write_kctxt_csr(dd, hw_context, SC(CREDIT_RETURN_ADDR), 0);
  798. write_kctxt_csr(dd, hw_context, SC(CREDIT_CTRL), 0);
  799. /* release the index and context for re-use */
  800. sc_hw_free(dd, sw_index, hw_context);
  801. spin_unlock_irqrestore(&dd->sc_lock, flags);
  802. kfree(sc->sr);
  803. free_percpu(sc->buffers_allocated);
  804. kfree(sc);
  805. }
  806. /* disable the context */
  807. void sc_disable(struct send_context *sc)
  808. {
  809. u64 reg;
  810. struct pio_buf *pbuf;
  811. if (!sc)
  812. return;
  813. /* do all steps, even if already disabled */
  814. spin_lock_irq(&sc->alloc_lock);
  815. reg = read_kctxt_csr(sc->dd, sc->hw_context, SC(CTRL));
  816. reg &= ~SC(CTRL_CTXT_ENABLE_SMASK);
  817. sc->flags &= ~SCF_ENABLED;
  818. sc_wait_for_packet_egress(sc, 1);
  819. write_kctxt_csr(sc->dd, sc->hw_context, SC(CTRL), reg);
  820. /*
  821. * Flush any waiters. Once the context is disabled,
  822. * credit return interrupts are stopped (although there
  823. * could be one in-process when the context is disabled).
  824. * Wait one microsecond for any lingering interrupts, then
  825. * proceed with the flush.
  826. */
  827. udelay(1);
  828. spin_lock(&sc->release_lock);
  829. if (sc->sr) { /* this context has a shadow ring */
  830. while (sc->sr_tail != sc->sr_head) {
  831. pbuf = &sc->sr[sc->sr_tail].pbuf;
  832. if (pbuf->cb)
  833. (*pbuf->cb)(pbuf->arg, PRC_SC_DISABLE);
  834. sc->sr_tail++;
  835. if (sc->sr_tail >= sc->sr_size)
  836. sc->sr_tail = 0;
  837. }
  838. }
  839. spin_unlock(&sc->release_lock);
  840. spin_unlock_irq(&sc->alloc_lock);
  841. }
  842. /* return SendEgressCtxtStatus.PacketOccupancy */
  843. static u64 packet_occupancy(u64 reg)
  844. {
  845. return (reg &
  846. SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SMASK)
  847. >> SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SHIFT;
  848. }
  849. /* is egress halted on the context? */
  850. static bool egress_halted(u64 reg)
  851. {
  852. return !!(reg & SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_HALT_STATUS_SMASK);
  853. }
  854. /* is the send context halted? */
  855. static bool is_sc_halted(struct hfi1_devdata *dd, u32 hw_context)
  856. {
  857. return !!(read_kctxt_csr(dd, hw_context, SC(STATUS)) &
  858. SC(STATUS_CTXT_HALTED_SMASK));
  859. }
  860. /**
  861. * sc_wait_for_packet_egress
  862. * @sc: valid send context
  863. * @pause: wait for credit return
  864. *
  865. * Wait for packet egress, optionally pause for credit return
  866. *
  867. * Egress halt and Context halt are not necessarily the same thing, so
  868. * check for both.
  869. *
  870. * NOTE: The context halt bit may not be set immediately. Because of this,
  871. * it is necessary to check the SW SFC_HALTED bit (set in the IRQ) and the HW
  872. * context bit to determine if the context is halted.
  873. */
  874. static void sc_wait_for_packet_egress(struct send_context *sc, int pause)
  875. {
  876. struct hfi1_devdata *dd = sc->dd;
  877. u64 reg = 0;
  878. u64 reg_prev;
  879. u32 loop = 0;
  880. while (1) {
  881. reg_prev = reg;
  882. reg = read_csr(dd, sc->hw_context * 8 +
  883. SEND_EGRESS_CTXT_STATUS);
  884. /* done if any halt bits, SW or HW are set */
  885. if (sc->flags & SCF_HALTED ||
  886. is_sc_halted(dd, sc->hw_context) || egress_halted(reg))
  887. break;
  888. reg = packet_occupancy(reg);
  889. if (reg == 0)
  890. break;
  891. /* counter is reset if occupancy count changes */
  892. if (reg != reg_prev)
  893. loop = 0;
  894. if (loop > 50000) {
  895. /* timed out - bounce the link */
  896. dd_dev_err(dd,
  897. "%s: context %u(%u) timeout waiting for packets to egress, remaining count %u, bouncing link\n",
  898. __func__, sc->sw_index,
  899. sc->hw_context, (u32)reg);
  900. queue_work(dd->pport->link_wq,
  901. &dd->pport->link_bounce_work);
  902. break;
  903. }
  904. loop++;
  905. udelay(1);
  906. }
  907. if (pause)
  908. /* Add additional delay to ensure chip returns all credits */
  909. pause_for_credit_return(dd);
  910. }
  911. void sc_wait(struct hfi1_devdata *dd)
  912. {
  913. int i;
  914. for (i = 0; i < dd->num_send_contexts; i++) {
  915. struct send_context *sc = dd->send_contexts[i].sc;
  916. if (!sc)
  917. continue;
  918. sc_wait_for_packet_egress(sc, 0);
  919. }
  920. }
  921. /*
  922. * Restart a context after it has been halted due to error.
  923. *
  924. * If the first step fails - wait for the halt to be asserted, return early.
  925. * Otherwise complain about timeouts but keep going.
  926. *
  927. * It is expected that allocations (enabled flag bit) have been shut off
  928. * already (only applies to kernel contexts).
  929. */
  930. int sc_restart(struct send_context *sc)
  931. {
  932. struct hfi1_devdata *dd = sc->dd;
  933. u64 reg;
  934. u32 loop;
  935. int count;
  936. /* bounce off if not halted, or being free'd */
  937. if (!(sc->flags & SCF_HALTED) || (sc->flags & SCF_IN_FREE))
  938. return -EINVAL;
  939. dd_dev_info(dd, "restarting send context %u(%u)\n", sc->sw_index,
  940. sc->hw_context);
  941. /*
  942. * Step 1: Wait for the context to actually halt.
  943. *
  944. * The error interrupt is asynchronous to actually setting halt
  945. * on the context.
  946. */
  947. loop = 0;
  948. while (1) {
  949. reg = read_kctxt_csr(dd, sc->hw_context, SC(STATUS));
  950. if (reg & SC(STATUS_CTXT_HALTED_SMASK))
  951. break;
  952. if (loop > 100) {
  953. dd_dev_err(dd, "%s: context %u(%u) not halting, skipping\n",
  954. __func__, sc->sw_index, sc->hw_context);
  955. return -ETIME;
  956. }
  957. loop++;
  958. udelay(1);
  959. }
  960. /*
  961. * Step 2: Ensure no users are still trying to write to PIO.
  962. *
  963. * For kernel contexts, we have already turned off buffer allocation.
  964. * Now wait for the buffer count to go to zero.
  965. *
  966. * For user contexts, the user handling code has cut off write access
  967. * to the context's PIO pages before calling this routine and will
  968. * restore write access after this routine returns.
  969. */
  970. if (sc->type != SC_USER) {
  971. /* kernel context */
  972. loop = 0;
  973. while (1) {
  974. count = get_buffers_allocated(sc);
  975. if (count == 0)
  976. break;
  977. if (loop > 100) {
  978. dd_dev_err(dd,
  979. "%s: context %u(%u) timeout waiting for PIO buffers to zero, remaining %d\n",
  980. __func__, sc->sw_index,
  981. sc->hw_context, count);
  982. }
  983. loop++;
  984. udelay(1);
  985. }
  986. }
  987. /*
  988. * Step 3: Wait for all packets to egress.
  989. * This is done while disabling the send context
  990. *
  991. * Step 4: Disable the context
  992. *
  993. * This is a superset of the halt. After the disable, the
  994. * errors can be cleared.
  995. */
  996. sc_disable(sc);
  997. /*
  998. * Step 5: Enable the context
  999. *
  1000. * This enable will clear the halted flag and per-send context
  1001. * error flags.
  1002. */
  1003. return sc_enable(sc);
  1004. }
  1005. /*
  1006. * PIO freeze processing. To be called after the TXE block is fully frozen.
  1007. * Go through all frozen send contexts and disable them. The contexts are
  1008. * already stopped by the freeze.
  1009. */
  1010. void pio_freeze(struct hfi1_devdata *dd)
  1011. {
  1012. struct send_context *sc;
  1013. int i;
  1014. for (i = 0; i < dd->num_send_contexts; i++) {
  1015. sc = dd->send_contexts[i].sc;
  1016. /*
  1017. * Don't disable unallocated, unfrozen, or user send contexts.
  1018. * User send contexts will be disabled when the process
  1019. * calls into the driver to reset its context.
  1020. */
  1021. if (!sc || !(sc->flags & SCF_FROZEN) || sc->type == SC_USER)
  1022. continue;
  1023. /* only need to disable, the context is already stopped */
  1024. sc_disable(sc);
  1025. }
  1026. }
  1027. /*
  1028. * Unfreeze PIO for kernel send contexts. The precondition for calling this
  1029. * is that all PIO send contexts have been disabled and the SPC freeze has
  1030. * been cleared. Now perform the last step and re-enable each kernel context.
  1031. * User (PSM) processing will occur when PSM calls into the kernel to
  1032. * acknowledge the freeze.
  1033. */
  1034. void pio_kernel_unfreeze(struct hfi1_devdata *dd)
  1035. {
  1036. struct send_context *sc;
  1037. int i;
  1038. for (i = 0; i < dd->num_send_contexts; i++) {
  1039. sc = dd->send_contexts[i].sc;
  1040. if (!sc || !(sc->flags & SCF_FROZEN) || sc->type == SC_USER)
  1041. continue;
  1042. if (sc->flags & SCF_LINK_DOWN)
  1043. continue;
  1044. sc_enable(sc); /* will clear the sc frozen flag */
  1045. }
  1046. }
  1047. /**
  1048. * pio_kernel_linkup() - Re-enable send contexts after linkup event
  1049. * @dd: valid devive data
  1050. *
  1051. * When the link goes down, the freeze path is taken. However, a link down
  1052. * event is different from a freeze because if the send context is re-enabled
  1053. * whowever is sending data will start sending data again, which will hang
  1054. * any QP that is sending data.
  1055. *
  1056. * The freeze path now looks at the type of event that occurs and takes this
  1057. * path for link down event.
  1058. */
  1059. void pio_kernel_linkup(struct hfi1_devdata *dd)
  1060. {
  1061. struct send_context *sc;
  1062. int i;
  1063. for (i = 0; i < dd->num_send_contexts; i++) {
  1064. sc = dd->send_contexts[i].sc;
  1065. if (!sc || !(sc->flags & SCF_LINK_DOWN) || sc->type == SC_USER)
  1066. continue;
  1067. sc_enable(sc); /* will clear the sc link down flag */
  1068. }
  1069. }
  1070. /*
  1071. * Wait for the SendPioInitCtxt.PioInitInProgress bit to clear.
  1072. * Returns:
  1073. * -ETIMEDOUT - if we wait too long
  1074. * -EIO - if there was an error
  1075. */
  1076. static int pio_init_wait_progress(struct hfi1_devdata *dd)
  1077. {
  1078. u64 reg;
  1079. int max, count = 0;
  1080. /* max is the longest possible HW init time / delay */
  1081. max = (dd->icode == ICODE_FPGA_EMULATION) ? 120 : 5;
  1082. while (1) {
  1083. reg = read_csr(dd, SEND_PIO_INIT_CTXT);
  1084. if (!(reg & SEND_PIO_INIT_CTXT_PIO_INIT_IN_PROGRESS_SMASK))
  1085. break;
  1086. if (count >= max)
  1087. return -ETIMEDOUT;
  1088. udelay(5);
  1089. count++;
  1090. }
  1091. return reg & SEND_PIO_INIT_CTXT_PIO_INIT_ERR_SMASK ? -EIO : 0;
  1092. }
  1093. /*
  1094. * Reset all of the send contexts to their power-on state. Used
  1095. * only during manual init - no lock against sc_enable needed.
  1096. */
  1097. void pio_reset_all(struct hfi1_devdata *dd)
  1098. {
  1099. int ret;
  1100. /* make sure the init engine is not busy */
  1101. ret = pio_init_wait_progress(dd);
  1102. /* ignore any timeout */
  1103. if (ret == -EIO) {
  1104. /* clear the error */
  1105. write_csr(dd, SEND_PIO_ERR_CLEAR,
  1106. SEND_PIO_ERR_CLEAR_PIO_INIT_SM_IN_ERR_SMASK);
  1107. }
  1108. /* reset init all */
  1109. write_csr(dd, SEND_PIO_INIT_CTXT,
  1110. SEND_PIO_INIT_CTXT_PIO_ALL_CTXT_INIT_SMASK);
  1111. udelay(2);
  1112. ret = pio_init_wait_progress(dd);
  1113. if (ret < 0) {
  1114. dd_dev_err(dd,
  1115. "PIO send context init %s while initializing all PIO blocks\n",
  1116. ret == -ETIMEDOUT ? "is stuck" : "had an error");
  1117. }
  1118. }
  1119. /* enable the context */
  1120. int sc_enable(struct send_context *sc)
  1121. {
  1122. u64 sc_ctrl, reg, pio;
  1123. struct hfi1_devdata *dd;
  1124. unsigned long flags;
  1125. int ret = 0;
  1126. if (!sc)
  1127. return -EINVAL;
  1128. dd = sc->dd;
  1129. /*
  1130. * Obtain the allocator lock to guard against any allocation
  1131. * attempts (which should not happen prior to context being
  1132. * enabled). On the release/disable side we don't need to
  1133. * worry about locking since the releaser will not do anything
  1134. * if the context accounting values have not changed.
  1135. */
  1136. spin_lock_irqsave(&sc->alloc_lock, flags);
  1137. sc_ctrl = read_kctxt_csr(dd, sc->hw_context, SC(CTRL));
  1138. if ((sc_ctrl & SC(CTRL_CTXT_ENABLE_SMASK)))
  1139. goto unlock; /* already enabled */
  1140. /* IMPORTANT: only clear free and fill if transitioning 0 -> 1 */
  1141. *sc->hw_free = 0;
  1142. sc->free = 0;
  1143. sc->alloc_free = 0;
  1144. sc->fill = 0;
  1145. sc->fill_wrap = 0;
  1146. sc->sr_head = 0;
  1147. sc->sr_tail = 0;
  1148. sc->flags = 0;
  1149. /* the alloc lock insures no fast path allocation */
  1150. reset_buffers_allocated(sc);
  1151. /*
  1152. * Clear all per-context errors. Some of these will be set when
  1153. * we are re-enabling after a context halt. Now that the context
  1154. * is disabled, the halt will not clear until after the PIO init
  1155. * engine runs below.
  1156. */
  1157. reg = read_kctxt_csr(dd, sc->hw_context, SC(ERR_STATUS));
  1158. if (reg)
  1159. write_kctxt_csr(dd, sc->hw_context, SC(ERR_CLEAR), reg);
  1160. /*
  1161. * The HW PIO initialization engine can handle only one init
  1162. * request at a time. Serialize access to each device's engine.
  1163. */
  1164. spin_lock(&dd->sc_init_lock);
  1165. /*
  1166. * Since access to this code block is serialized and
  1167. * each access waits for the initialization to complete
  1168. * before releasing the lock, the PIO initialization engine
  1169. * should not be in use, so we don't have to wait for the
  1170. * InProgress bit to go down.
  1171. */
  1172. pio = ((sc->hw_context & SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_MASK) <<
  1173. SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_SHIFT) |
  1174. SEND_PIO_INIT_CTXT_PIO_SINGLE_CTXT_INIT_SMASK;
  1175. write_csr(dd, SEND_PIO_INIT_CTXT, pio);
  1176. /*
  1177. * Wait until the engine is done. Give the chip the required time
  1178. * so, hopefully, we read the register just once.
  1179. */
  1180. udelay(2);
  1181. ret = pio_init_wait_progress(dd);
  1182. spin_unlock(&dd->sc_init_lock);
  1183. if (ret) {
  1184. dd_dev_err(dd,
  1185. "sctxt%u(%u): Context not enabled due to init failure %d\n",
  1186. sc->sw_index, sc->hw_context, ret);
  1187. goto unlock;
  1188. }
  1189. /*
  1190. * All is well. Enable the context.
  1191. */
  1192. sc_ctrl |= SC(CTRL_CTXT_ENABLE_SMASK);
  1193. write_kctxt_csr(dd, sc->hw_context, SC(CTRL), sc_ctrl);
  1194. /*
  1195. * Read SendCtxtCtrl to force the write out and prevent a timing
  1196. * hazard where a PIO write may reach the context before the enable.
  1197. */
  1198. read_kctxt_csr(dd, sc->hw_context, SC(CTRL));
  1199. sc->flags |= SCF_ENABLED;
  1200. unlock:
  1201. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  1202. return ret;
  1203. }
  1204. /* force a credit return on the context */
  1205. void sc_return_credits(struct send_context *sc)
  1206. {
  1207. if (!sc)
  1208. return;
  1209. /* a 0->1 transition schedules a credit return */
  1210. write_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE),
  1211. SC(CREDIT_FORCE_FORCE_RETURN_SMASK));
  1212. /*
  1213. * Ensure that the write is flushed and the credit return is
  1214. * scheduled. We care more about the 0 -> 1 transition.
  1215. */
  1216. read_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE));
  1217. /* set back to 0 for next time */
  1218. write_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE), 0);
  1219. }
  1220. /* allow all in-flight packets to drain on the context */
  1221. void sc_flush(struct send_context *sc)
  1222. {
  1223. if (!sc)
  1224. return;
  1225. sc_wait_for_packet_egress(sc, 1);
  1226. }
  1227. /* drop all packets on the context, no waiting until they are sent */
  1228. void sc_drop(struct send_context *sc)
  1229. {
  1230. if (!sc)
  1231. return;
  1232. dd_dev_info(sc->dd, "%s: context %u(%u) - not implemented\n",
  1233. __func__, sc->sw_index, sc->hw_context);
  1234. }
  1235. /*
  1236. * Start the software reaction to a context halt or SPC freeze:
  1237. * - mark the context as halted or frozen
  1238. * - stop buffer allocations
  1239. *
  1240. * Called from the error interrupt. Other work is deferred until
  1241. * out of the interrupt.
  1242. */
  1243. void sc_stop(struct send_context *sc, int flag)
  1244. {
  1245. unsigned long flags;
  1246. /* stop buffer allocations */
  1247. spin_lock_irqsave(&sc->alloc_lock, flags);
  1248. /* mark the context */
  1249. sc->flags |= flag;
  1250. sc->flags &= ~SCF_ENABLED;
  1251. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  1252. wake_up(&sc->halt_wait);
  1253. }
  1254. #define BLOCK_DWORDS (PIO_BLOCK_SIZE / sizeof(u32))
  1255. #define dwords_to_blocks(x) DIV_ROUND_UP(x, BLOCK_DWORDS)
  1256. /*
  1257. * The send context buffer "allocator".
  1258. *
  1259. * @sc: the PIO send context we are allocating from
  1260. * @len: length of whole packet - including PBC - in dwords
  1261. * @cb: optional callback to call when the buffer is finished sending
  1262. * @arg: argument for cb
  1263. *
  1264. * Return a pointer to a PIO buffer if successful, NULL if not enough room.
  1265. */
  1266. struct pio_buf *sc_buffer_alloc(struct send_context *sc, u32 dw_len,
  1267. pio_release_cb cb, void *arg)
  1268. {
  1269. struct pio_buf *pbuf = NULL;
  1270. unsigned long flags;
  1271. unsigned long avail;
  1272. unsigned long blocks = dwords_to_blocks(dw_len);
  1273. u32 fill_wrap;
  1274. int trycount = 0;
  1275. u32 head, next;
  1276. spin_lock_irqsave(&sc->alloc_lock, flags);
  1277. if (!(sc->flags & SCF_ENABLED)) {
  1278. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  1279. goto done;
  1280. }
  1281. retry:
  1282. avail = (unsigned long)sc->credits - (sc->fill - sc->alloc_free);
  1283. if (blocks > avail) {
  1284. /* not enough room */
  1285. if (unlikely(trycount)) { /* already tried to get more room */
  1286. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  1287. goto done;
  1288. }
  1289. /* copy from receiver cache line and recalculate */
  1290. sc->alloc_free = READ_ONCE(sc->free);
  1291. avail =
  1292. (unsigned long)sc->credits -
  1293. (sc->fill - sc->alloc_free);
  1294. if (blocks > avail) {
  1295. /* still no room, actively update */
  1296. sc_release_update(sc);
  1297. sc->alloc_free = READ_ONCE(sc->free);
  1298. trycount++;
  1299. goto retry;
  1300. }
  1301. }
  1302. /* there is enough room */
  1303. preempt_disable();
  1304. this_cpu_inc(*sc->buffers_allocated);
  1305. /* read this once */
  1306. head = sc->sr_head;
  1307. /* "allocate" the buffer */
  1308. sc->fill += blocks;
  1309. fill_wrap = sc->fill_wrap;
  1310. sc->fill_wrap += blocks;
  1311. if (sc->fill_wrap >= sc->credits)
  1312. sc->fill_wrap = sc->fill_wrap - sc->credits;
  1313. /*
  1314. * Fill the parts that the releaser looks at before moving the head.
  1315. * The only necessary piece is the sent_at field. The credits
  1316. * we have just allocated cannot have been returned yet, so the
  1317. * cb and arg will not be looked at for a "while". Put them
  1318. * on this side of the memory barrier anyway.
  1319. */
  1320. pbuf = &sc->sr[head].pbuf;
  1321. pbuf->sent_at = sc->fill;
  1322. pbuf->cb = cb;
  1323. pbuf->arg = arg;
  1324. pbuf->sc = sc; /* could be filled in at sc->sr init time */
  1325. /* make sure this is in memory before updating the head */
  1326. /* calculate next head index, do not store */
  1327. next = head + 1;
  1328. if (next >= sc->sr_size)
  1329. next = 0;
  1330. /*
  1331. * update the head - must be last! - the releaser can look at fields
  1332. * in pbuf once we move the head
  1333. */
  1334. smp_wmb();
  1335. sc->sr_head = next;
  1336. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  1337. /* finish filling in the buffer outside the lock */
  1338. pbuf->start = sc->base_addr + fill_wrap * PIO_BLOCK_SIZE;
  1339. pbuf->end = sc->base_addr + sc->size;
  1340. pbuf->qw_written = 0;
  1341. pbuf->carry_bytes = 0;
  1342. pbuf->carry.val64 = 0;
  1343. done:
  1344. return pbuf;
  1345. }
  1346. /*
  1347. * There are at least two entities that can turn on credit return
  1348. * interrupts and they can overlap. Avoid problems by implementing
  1349. * a count scheme that is enforced by a lock. The lock is needed because
  1350. * the count and CSR write must be paired.
  1351. */
  1352. /*
  1353. * Start credit return interrupts. This is managed by a count. If already
  1354. * on, just increment the count.
  1355. */
  1356. void sc_add_credit_return_intr(struct send_context *sc)
  1357. {
  1358. unsigned long flags;
  1359. /* lock must surround both the count change and the CSR update */
  1360. spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
  1361. if (sc->credit_intr_count == 0) {
  1362. sc->credit_ctrl |= SC(CREDIT_CTRL_CREDIT_INTR_SMASK);
  1363. write_kctxt_csr(sc->dd, sc->hw_context,
  1364. SC(CREDIT_CTRL), sc->credit_ctrl);
  1365. }
  1366. sc->credit_intr_count++;
  1367. spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
  1368. }
  1369. /*
  1370. * Stop credit return interrupts. This is managed by a count. Decrement the
  1371. * count, if the last user, then turn the credit interrupts off.
  1372. */
  1373. void sc_del_credit_return_intr(struct send_context *sc)
  1374. {
  1375. unsigned long flags;
  1376. WARN_ON(sc->credit_intr_count == 0);
  1377. /* lock must surround both the count change and the CSR update */
  1378. spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
  1379. sc->credit_intr_count--;
  1380. if (sc->credit_intr_count == 0) {
  1381. sc->credit_ctrl &= ~SC(CREDIT_CTRL_CREDIT_INTR_SMASK);
  1382. write_kctxt_csr(sc->dd, sc->hw_context,
  1383. SC(CREDIT_CTRL), sc->credit_ctrl);
  1384. }
  1385. spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
  1386. }
  1387. /*
  1388. * The caller must be careful when calling this. All needint calls
  1389. * must be paired with !needint.
  1390. */
  1391. void hfi1_sc_wantpiobuf_intr(struct send_context *sc, u32 needint)
  1392. {
  1393. if (needint)
  1394. sc_add_credit_return_intr(sc);
  1395. else
  1396. sc_del_credit_return_intr(sc);
  1397. trace_hfi1_wantpiointr(sc, needint, sc->credit_ctrl);
  1398. if (needint) {
  1399. mmiowb();
  1400. sc_return_credits(sc);
  1401. }
  1402. }
  1403. /**
  1404. * sc_piobufavail - callback when a PIO buffer is available
  1405. * @sc: the send context
  1406. *
  1407. * This is called from the interrupt handler when a PIO buffer is
  1408. * available after hfi1_verbs_send() returned an error that no buffers were
  1409. * available. Disable the interrupt if there are no more QPs waiting.
  1410. */
  1411. static void sc_piobufavail(struct send_context *sc)
  1412. {
  1413. struct hfi1_devdata *dd = sc->dd;
  1414. struct hfi1_ibdev *dev = &dd->verbs_dev;
  1415. struct list_head *list;
  1416. struct rvt_qp *qps[PIO_WAIT_BATCH_SIZE];
  1417. struct rvt_qp *qp;
  1418. struct hfi1_qp_priv *priv;
  1419. unsigned long flags;
  1420. uint i, n = 0, max_idx = 0;
  1421. u8 max_starved_cnt = 0;
  1422. if (dd->send_contexts[sc->sw_index].type != SC_KERNEL &&
  1423. dd->send_contexts[sc->sw_index].type != SC_VL15)
  1424. return;
  1425. list = &sc->piowait;
  1426. /*
  1427. * Note: checking that the piowait list is empty and clearing
  1428. * the buffer available interrupt needs to be atomic or we
  1429. * could end up with QPs on the wait list with the interrupt
  1430. * disabled.
  1431. */
  1432. write_seqlock_irqsave(&dev->iowait_lock, flags);
  1433. while (!list_empty(list)) {
  1434. struct iowait *wait;
  1435. if (n == ARRAY_SIZE(qps))
  1436. break;
  1437. wait = list_first_entry(list, struct iowait, list);
  1438. qp = iowait_to_qp(wait);
  1439. priv = qp->priv;
  1440. list_del_init(&priv->s_iowait.list);
  1441. priv->s_iowait.lock = NULL;
  1442. iowait_starve_find_max(wait, &max_starved_cnt, n, &max_idx);
  1443. /* refcount held until actual wake up */
  1444. qps[n++] = qp;
  1445. }
  1446. /*
  1447. * If there had been waiters and there are more
  1448. * insure that we redo the force to avoid a potential hang.
  1449. */
  1450. if (n) {
  1451. hfi1_sc_wantpiobuf_intr(sc, 0);
  1452. if (!list_empty(list))
  1453. hfi1_sc_wantpiobuf_intr(sc, 1);
  1454. }
  1455. write_sequnlock_irqrestore(&dev->iowait_lock, flags);
  1456. /* Wake up the most starved one first */
  1457. if (n)
  1458. hfi1_qp_wakeup(qps[max_idx],
  1459. RVT_S_WAIT_PIO | HFI1_S_WAIT_PIO_DRAIN);
  1460. for (i = 0; i < n; i++)
  1461. if (i != max_idx)
  1462. hfi1_qp_wakeup(qps[i],
  1463. RVT_S_WAIT_PIO | HFI1_S_WAIT_PIO_DRAIN);
  1464. }
  1465. /* translate a send credit update to a bit code of reasons */
  1466. static inline int fill_code(u64 hw_free)
  1467. {
  1468. int code = 0;
  1469. if (hw_free & CR_STATUS_SMASK)
  1470. code |= PRC_STATUS_ERR;
  1471. if (hw_free & CR_CREDIT_RETURN_DUE_TO_PBC_SMASK)
  1472. code |= PRC_PBC;
  1473. if (hw_free & CR_CREDIT_RETURN_DUE_TO_THRESHOLD_SMASK)
  1474. code |= PRC_THRESHOLD;
  1475. if (hw_free & CR_CREDIT_RETURN_DUE_TO_ERR_SMASK)
  1476. code |= PRC_FILL_ERR;
  1477. if (hw_free & CR_CREDIT_RETURN_DUE_TO_FORCE_SMASK)
  1478. code |= PRC_SC_DISABLE;
  1479. return code;
  1480. }
  1481. /* use the jiffies compare to get the wrap right */
  1482. #define sent_before(a, b) time_before(a, b) /* a < b */
  1483. /*
  1484. * The send context buffer "releaser".
  1485. */
  1486. void sc_release_update(struct send_context *sc)
  1487. {
  1488. struct pio_buf *pbuf;
  1489. u64 hw_free;
  1490. u32 head, tail;
  1491. unsigned long old_free;
  1492. unsigned long free;
  1493. unsigned long extra;
  1494. unsigned long flags;
  1495. int code;
  1496. if (!sc)
  1497. return;
  1498. spin_lock_irqsave(&sc->release_lock, flags);
  1499. /* update free */
  1500. hw_free = le64_to_cpu(*sc->hw_free); /* volatile read */
  1501. old_free = sc->free;
  1502. extra = (((hw_free & CR_COUNTER_SMASK) >> CR_COUNTER_SHIFT)
  1503. - (old_free & CR_COUNTER_MASK))
  1504. & CR_COUNTER_MASK;
  1505. free = old_free + extra;
  1506. trace_hfi1_piofree(sc, extra);
  1507. /* call sent buffer callbacks */
  1508. code = -1; /* code not yet set */
  1509. head = READ_ONCE(sc->sr_head); /* snapshot the head */
  1510. tail = sc->sr_tail;
  1511. while (head != tail) {
  1512. pbuf = &sc->sr[tail].pbuf;
  1513. if (sent_before(free, pbuf->sent_at)) {
  1514. /* not sent yet */
  1515. break;
  1516. }
  1517. if (pbuf->cb) {
  1518. if (code < 0) /* fill in code on first user */
  1519. code = fill_code(hw_free);
  1520. (*pbuf->cb)(pbuf->arg, code);
  1521. }
  1522. tail++;
  1523. if (tail >= sc->sr_size)
  1524. tail = 0;
  1525. }
  1526. sc->sr_tail = tail;
  1527. /* make sure tail is updated before free */
  1528. smp_wmb();
  1529. sc->free = free;
  1530. spin_unlock_irqrestore(&sc->release_lock, flags);
  1531. sc_piobufavail(sc);
  1532. }
  1533. /*
  1534. * Send context group releaser. Argument is the send context that caused
  1535. * the interrupt. Called from the send context interrupt handler.
  1536. *
  1537. * Call release on all contexts in the group.
  1538. *
  1539. * This routine takes the sc_lock without an irqsave because it is only
  1540. * called from an interrupt handler. Adjust if that changes.
  1541. */
  1542. void sc_group_release_update(struct hfi1_devdata *dd, u32 hw_context)
  1543. {
  1544. struct send_context *sc;
  1545. u32 sw_index;
  1546. u32 gc, gc_end;
  1547. spin_lock(&dd->sc_lock);
  1548. sw_index = dd->hw_to_sw[hw_context];
  1549. if (unlikely(sw_index >= dd->num_send_contexts)) {
  1550. dd_dev_err(dd, "%s: invalid hw (%u) to sw (%u) mapping\n",
  1551. __func__, hw_context, sw_index);
  1552. goto done;
  1553. }
  1554. sc = dd->send_contexts[sw_index].sc;
  1555. if (unlikely(!sc))
  1556. goto done;
  1557. gc = group_context(hw_context, sc->group);
  1558. gc_end = gc + group_size(sc->group);
  1559. for (; gc < gc_end; gc++) {
  1560. sw_index = dd->hw_to_sw[gc];
  1561. if (unlikely(sw_index >= dd->num_send_contexts)) {
  1562. dd_dev_err(dd,
  1563. "%s: invalid hw (%u) to sw (%u) mapping\n",
  1564. __func__, hw_context, sw_index);
  1565. continue;
  1566. }
  1567. sc_release_update(dd->send_contexts[sw_index].sc);
  1568. }
  1569. done:
  1570. spin_unlock(&dd->sc_lock);
  1571. }
  1572. /*
  1573. * pio_select_send_context_vl() - select send context
  1574. * @dd: devdata
  1575. * @selector: a spreading factor
  1576. * @vl: this vl
  1577. *
  1578. * This function returns a send context based on the selector and a vl.
  1579. * The mapping fields are protected by RCU
  1580. */
  1581. struct send_context *pio_select_send_context_vl(struct hfi1_devdata *dd,
  1582. u32 selector, u8 vl)
  1583. {
  1584. struct pio_vl_map *m;
  1585. struct pio_map_elem *e;
  1586. struct send_context *rval;
  1587. /*
  1588. * NOTE This should only happen if SC->VL changed after the initial
  1589. * checks on the QP/AH
  1590. * Default will return VL0's send context below
  1591. */
  1592. if (unlikely(vl >= num_vls)) {
  1593. rval = NULL;
  1594. goto done;
  1595. }
  1596. rcu_read_lock();
  1597. m = rcu_dereference(dd->pio_map);
  1598. if (unlikely(!m)) {
  1599. rcu_read_unlock();
  1600. return dd->vld[0].sc;
  1601. }
  1602. e = m->map[vl & m->mask];
  1603. rval = e->ksc[selector & e->mask];
  1604. rcu_read_unlock();
  1605. done:
  1606. rval = !rval ? dd->vld[0].sc : rval;
  1607. return rval;
  1608. }
  1609. /*
  1610. * pio_select_send_context_sc() - select send context
  1611. * @dd: devdata
  1612. * @selector: a spreading factor
  1613. * @sc5: the 5 bit sc
  1614. *
  1615. * This function returns an send context based on the selector and an sc
  1616. */
  1617. struct send_context *pio_select_send_context_sc(struct hfi1_devdata *dd,
  1618. u32 selector, u8 sc5)
  1619. {
  1620. u8 vl = sc_to_vlt(dd, sc5);
  1621. return pio_select_send_context_vl(dd, selector, vl);
  1622. }
  1623. /*
  1624. * Free the indicated map struct
  1625. */
  1626. static void pio_map_free(struct pio_vl_map *m)
  1627. {
  1628. int i;
  1629. for (i = 0; m && i < m->actual_vls; i++)
  1630. kfree(m->map[i]);
  1631. kfree(m);
  1632. }
  1633. /*
  1634. * Handle RCU callback
  1635. */
  1636. static void pio_map_rcu_callback(struct rcu_head *list)
  1637. {
  1638. struct pio_vl_map *m = container_of(list, struct pio_vl_map, list);
  1639. pio_map_free(m);
  1640. }
  1641. /*
  1642. * Set credit return threshold for the kernel send context
  1643. */
  1644. static void set_threshold(struct hfi1_devdata *dd, int scontext, int i)
  1645. {
  1646. u32 thres;
  1647. thres = min(sc_percent_to_threshold(dd->kernel_send_context[scontext],
  1648. 50),
  1649. sc_mtu_to_threshold(dd->kernel_send_context[scontext],
  1650. dd->vld[i].mtu,
  1651. dd->rcd[0]->rcvhdrqentsize));
  1652. sc_set_cr_threshold(dd->kernel_send_context[scontext], thres);
  1653. }
  1654. /*
  1655. * pio_map_init - called when #vls change
  1656. * @dd: hfi1_devdata
  1657. * @port: port number
  1658. * @num_vls: number of vls
  1659. * @vl_scontexts: per vl send context mapping (optional)
  1660. *
  1661. * This routine changes the mapping based on the number of vls.
  1662. *
  1663. * vl_scontexts is used to specify a non-uniform vl/send context
  1664. * loading. NULL implies auto computing the loading and giving each
  1665. * VL an uniform distribution of send contexts per VL.
  1666. *
  1667. * The auto algorithm computers the sc_per_vl and the number of extra
  1668. * send contexts. Any extra send contexts are added from the last VL
  1669. * on down
  1670. *
  1671. * rcu locking is used here to control access to the mapping fields.
  1672. *
  1673. * If either the num_vls or num_send_contexts are non-power of 2, the
  1674. * array sizes in the struct pio_vl_map and the struct pio_map_elem are
  1675. * rounded up to the next highest power of 2 and the first entry is
  1676. * reused in a round robin fashion.
  1677. *
  1678. * If an error occurs the map change is not done and the mapping is not
  1679. * chaged.
  1680. *
  1681. */
  1682. int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls, u8 *vl_scontexts)
  1683. {
  1684. int i, j;
  1685. int extra, sc_per_vl;
  1686. int scontext = 1;
  1687. int num_kernel_send_contexts = 0;
  1688. u8 lvl_scontexts[OPA_MAX_VLS];
  1689. struct pio_vl_map *oldmap, *newmap;
  1690. if (!vl_scontexts) {
  1691. for (i = 0; i < dd->num_send_contexts; i++)
  1692. if (dd->send_contexts[i].type == SC_KERNEL)
  1693. num_kernel_send_contexts++;
  1694. /* truncate divide */
  1695. sc_per_vl = num_kernel_send_contexts / num_vls;
  1696. /* extras */
  1697. extra = num_kernel_send_contexts % num_vls;
  1698. vl_scontexts = lvl_scontexts;
  1699. /* add extras from last vl down */
  1700. for (i = num_vls - 1; i >= 0; i--, extra--)
  1701. vl_scontexts[i] = sc_per_vl + (extra > 0 ? 1 : 0);
  1702. }
  1703. /* build new map */
  1704. newmap = kzalloc(sizeof(*newmap) +
  1705. roundup_pow_of_two(num_vls) *
  1706. sizeof(struct pio_map_elem *),
  1707. GFP_KERNEL);
  1708. if (!newmap)
  1709. goto bail;
  1710. newmap->actual_vls = num_vls;
  1711. newmap->vls = roundup_pow_of_two(num_vls);
  1712. newmap->mask = (1 << ilog2(newmap->vls)) - 1;
  1713. for (i = 0; i < newmap->vls; i++) {
  1714. /* save for wrap around */
  1715. int first_scontext = scontext;
  1716. if (i < newmap->actual_vls) {
  1717. int sz = roundup_pow_of_two(vl_scontexts[i]);
  1718. /* only allocate once */
  1719. newmap->map[i] = kzalloc(sizeof(*newmap->map[i]) +
  1720. sz * sizeof(struct
  1721. send_context *),
  1722. GFP_KERNEL);
  1723. if (!newmap->map[i])
  1724. goto bail;
  1725. newmap->map[i]->mask = (1 << ilog2(sz)) - 1;
  1726. /*
  1727. * assign send contexts and
  1728. * adjust credit return threshold
  1729. */
  1730. for (j = 0; j < sz; j++) {
  1731. if (dd->kernel_send_context[scontext]) {
  1732. newmap->map[i]->ksc[j] =
  1733. dd->kernel_send_context[scontext];
  1734. set_threshold(dd, scontext, i);
  1735. }
  1736. if (++scontext >= first_scontext +
  1737. vl_scontexts[i])
  1738. /* wrap back to first send context */
  1739. scontext = first_scontext;
  1740. }
  1741. } else {
  1742. /* just re-use entry without allocating */
  1743. newmap->map[i] = newmap->map[i % num_vls];
  1744. }
  1745. scontext = first_scontext + vl_scontexts[i];
  1746. }
  1747. /* newmap in hand, save old map */
  1748. spin_lock_irq(&dd->pio_map_lock);
  1749. oldmap = rcu_dereference_protected(dd->pio_map,
  1750. lockdep_is_held(&dd->pio_map_lock));
  1751. /* publish newmap */
  1752. rcu_assign_pointer(dd->pio_map, newmap);
  1753. spin_unlock_irq(&dd->pio_map_lock);
  1754. /* success, free any old map after grace period */
  1755. if (oldmap)
  1756. call_rcu(&oldmap->list, pio_map_rcu_callback);
  1757. return 0;
  1758. bail:
  1759. /* free any partial allocation */
  1760. pio_map_free(newmap);
  1761. return -ENOMEM;
  1762. }
  1763. void free_pio_map(struct hfi1_devdata *dd)
  1764. {
  1765. /* Free PIO map if allocated */
  1766. if (rcu_access_pointer(dd->pio_map)) {
  1767. spin_lock_irq(&dd->pio_map_lock);
  1768. pio_map_free(rcu_access_pointer(dd->pio_map));
  1769. RCU_INIT_POINTER(dd->pio_map, NULL);
  1770. spin_unlock_irq(&dd->pio_map_lock);
  1771. synchronize_rcu();
  1772. }
  1773. kfree(dd->kernel_send_context);
  1774. dd->kernel_send_context = NULL;
  1775. }
  1776. int init_pervl_scs(struct hfi1_devdata *dd)
  1777. {
  1778. int i;
  1779. u64 mask, all_vl_mask = (u64)0x80ff; /* VLs 0-7, 15 */
  1780. u64 data_vls_mask = (u64)0x00ff; /* VLs 0-7 */
  1781. u32 ctxt;
  1782. struct hfi1_pportdata *ppd = dd->pport;
  1783. dd->vld[15].sc = sc_alloc(dd, SC_VL15,
  1784. dd->rcd[0]->rcvhdrqentsize, dd->node);
  1785. if (!dd->vld[15].sc)
  1786. return -ENOMEM;
  1787. hfi1_init_ctxt(dd->vld[15].sc);
  1788. dd->vld[15].mtu = enum_to_mtu(OPA_MTU_2048);
  1789. dd->kernel_send_context = kcalloc_node(dd->num_send_contexts,
  1790. sizeof(struct send_context *),
  1791. GFP_KERNEL, dd->node);
  1792. if (!dd->kernel_send_context)
  1793. goto freesc15;
  1794. dd->kernel_send_context[0] = dd->vld[15].sc;
  1795. for (i = 0; i < num_vls; i++) {
  1796. /*
  1797. * Since this function does not deal with a specific
  1798. * receive context but we need the RcvHdrQ entry size,
  1799. * use the size from rcd[0]. It is guaranteed to be
  1800. * valid at this point and will remain the same for all
  1801. * receive contexts.
  1802. */
  1803. dd->vld[i].sc = sc_alloc(dd, SC_KERNEL,
  1804. dd->rcd[0]->rcvhdrqentsize, dd->node);
  1805. if (!dd->vld[i].sc)
  1806. goto nomem;
  1807. dd->kernel_send_context[i + 1] = dd->vld[i].sc;
  1808. hfi1_init_ctxt(dd->vld[i].sc);
  1809. /* non VL15 start with the max MTU */
  1810. dd->vld[i].mtu = hfi1_max_mtu;
  1811. }
  1812. for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++) {
  1813. dd->kernel_send_context[i + 1] =
  1814. sc_alloc(dd, SC_KERNEL, dd->rcd[0]->rcvhdrqentsize, dd->node);
  1815. if (!dd->kernel_send_context[i + 1])
  1816. goto nomem;
  1817. hfi1_init_ctxt(dd->kernel_send_context[i + 1]);
  1818. }
  1819. sc_enable(dd->vld[15].sc);
  1820. ctxt = dd->vld[15].sc->hw_context;
  1821. mask = all_vl_mask & ~(1LL << 15);
  1822. write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
  1823. dd_dev_info(dd,
  1824. "Using send context %u(%u) for VL15\n",
  1825. dd->vld[15].sc->sw_index, ctxt);
  1826. for (i = 0; i < num_vls; i++) {
  1827. sc_enable(dd->vld[i].sc);
  1828. ctxt = dd->vld[i].sc->hw_context;
  1829. mask = all_vl_mask & ~(data_vls_mask);
  1830. write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
  1831. }
  1832. for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++) {
  1833. sc_enable(dd->kernel_send_context[i + 1]);
  1834. ctxt = dd->kernel_send_context[i + 1]->hw_context;
  1835. mask = all_vl_mask & ~(data_vls_mask);
  1836. write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
  1837. }
  1838. if (pio_map_init(dd, ppd->port - 1, num_vls, NULL))
  1839. goto nomem;
  1840. return 0;
  1841. nomem:
  1842. for (i = 0; i < num_vls; i++) {
  1843. sc_free(dd->vld[i].sc);
  1844. dd->vld[i].sc = NULL;
  1845. }
  1846. for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++)
  1847. sc_free(dd->kernel_send_context[i + 1]);
  1848. kfree(dd->kernel_send_context);
  1849. dd->kernel_send_context = NULL;
  1850. freesc15:
  1851. sc_free(dd->vld[15].sc);
  1852. return -ENOMEM;
  1853. }
  1854. int init_credit_return(struct hfi1_devdata *dd)
  1855. {
  1856. int ret;
  1857. int i;
  1858. dd->cr_base = kcalloc(
  1859. node_affinity.num_possible_nodes,
  1860. sizeof(struct credit_return_base),
  1861. GFP_KERNEL);
  1862. if (!dd->cr_base) {
  1863. ret = -ENOMEM;
  1864. goto done;
  1865. }
  1866. for_each_node_with_cpus(i) {
  1867. int bytes = TXE_NUM_CONTEXTS * sizeof(struct credit_return);
  1868. set_dev_node(&dd->pcidev->dev, i);
  1869. dd->cr_base[i].va = dma_zalloc_coherent(
  1870. &dd->pcidev->dev,
  1871. bytes,
  1872. &dd->cr_base[i].dma,
  1873. GFP_KERNEL);
  1874. if (!dd->cr_base[i].va) {
  1875. set_dev_node(&dd->pcidev->dev, dd->node);
  1876. dd_dev_err(dd,
  1877. "Unable to allocate credit return DMA range for NUMA %d\n",
  1878. i);
  1879. ret = -ENOMEM;
  1880. goto done;
  1881. }
  1882. }
  1883. set_dev_node(&dd->pcidev->dev, dd->node);
  1884. ret = 0;
  1885. done:
  1886. return ret;
  1887. }
  1888. void free_credit_return(struct hfi1_devdata *dd)
  1889. {
  1890. int i;
  1891. if (!dd->cr_base)
  1892. return;
  1893. for (i = 0; i < node_affinity.num_possible_nodes; i++) {
  1894. if (dd->cr_base[i].va) {
  1895. dma_free_coherent(&dd->pcidev->dev,
  1896. TXE_NUM_CONTEXTS *
  1897. sizeof(struct credit_return),
  1898. dd->cr_base[i].va,
  1899. dd->cr_base[i].dma);
  1900. }
  1901. }
  1902. kfree(dd->cr_base);
  1903. dd->cr_base = NULL;
  1904. }