pio.c 56 KB

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