pio.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/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. /* PIO Send Memory Address details */
  672. #define PIO_ADDR_CONTEXT_MASK 0xfful
  673. #define PIO_ADDR_CONTEXT_SHIFT 16
  674. sc->base_addr = dd->piobase + ((hw_context & PIO_ADDR_CONTEXT_MASK)
  675. << PIO_ADDR_CONTEXT_SHIFT);
  676. /* set base and credits */
  677. reg = ((sci->credits & SC(CTRL_CTXT_DEPTH_MASK))
  678. << SC(CTRL_CTXT_DEPTH_SHIFT))
  679. | ((sci->base & SC(CTRL_CTXT_BASE_MASK))
  680. << SC(CTRL_CTXT_BASE_SHIFT));
  681. write_kctxt_csr(dd, hw_context, SC(CTRL), reg);
  682. set_pio_integrity(sc);
  683. /* unmask all errors */
  684. write_kctxt_csr(dd, hw_context, SC(ERR_MASK), (u64)-1);
  685. /* set the default partition key */
  686. write_kctxt_csr(dd, hw_context, SC(CHECK_PARTITION_KEY),
  687. (SC(CHECK_PARTITION_KEY_VALUE_MASK) &
  688. DEFAULT_PKEY) <<
  689. SC(CHECK_PARTITION_KEY_VALUE_SHIFT));
  690. /* per context type checks */
  691. if (type == SC_USER) {
  692. opval = USER_OPCODE_CHECK_VAL;
  693. opmask = USER_OPCODE_CHECK_MASK;
  694. } else {
  695. opval = OPCODE_CHECK_VAL_DISABLED;
  696. opmask = OPCODE_CHECK_MASK_DISABLED;
  697. }
  698. /* set the send context check opcode mask and value */
  699. write_kctxt_csr(dd, hw_context, SC(CHECK_OPCODE),
  700. ((u64)opmask << SC(CHECK_OPCODE_MASK_SHIFT)) |
  701. ((u64)opval << SC(CHECK_OPCODE_VALUE_SHIFT)));
  702. /* set up credit return */
  703. reg = dma & SC(CREDIT_RETURN_ADDR_ADDRESS_SMASK);
  704. write_kctxt_csr(dd, hw_context, SC(CREDIT_RETURN_ADDR), reg);
  705. /*
  706. * Calculate the initial credit return threshold.
  707. *
  708. * For Ack contexts, set a threshold for half the credits.
  709. * For User contexts use the given percentage. This has been
  710. * sanitized on driver start-up.
  711. * For Kernel contexts, use the default MTU plus a header
  712. * or half the credits, whichever is smaller. This should
  713. * work for both the 3-deep buffering allocation and the
  714. * pooling allocation.
  715. */
  716. if (type == SC_ACK) {
  717. thresh = sc_percent_to_threshold(sc, 50);
  718. } else if (type == SC_USER) {
  719. thresh = sc_percent_to_threshold(sc,
  720. user_credit_return_threshold);
  721. } else { /* kernel */
  722. thresh = min(sc_percent_to_threshold(sc, 50),
  723. sc_mtu_to_threshold(sc, hfi1_max_mtu,
  724. hdrqentsize));
  725. }
  726. reg = thresh << SC(CREDIT_CTRL_THRESHOLD_SHIFT);
  727. /* add in early return */
  728. if (type == SC_USER && HFI1_CAP_IS_USET(EARLY_CREDIT_RETURN))
  729. reg |= SC(CREDIT_CTRL_EARLY_RETURN_SMASK);
  730. else if (HFI1_CAP_IS_KSET(EARLY_CREDIT_RETURN)) /* kernel, ack */
  731. reg |= SC(CREDIT_CTRL_EARLY_RETURN_SMASK);
  732. /* set up write-through credit_ctrl */
  733. sc->credit_ctrl = reg;
  734. write_kctxt_csr(dd, hw_context, SC(CREDIT_CTRL), reg);
  735. /* User send contexts should not allow sending on VL15 */
  736. if (type == SC_USER) {
  737. reg = 1ULL << 15;
  738. write_kctxt_csr(dd, hw_context, SC(CHECK_VL), reg);
  739. }
  740. spin_unlock_irqrestore(&dd->sc_lock, flags);
  741. /*
  742. * Allocate shadow ring to track outstanding PIO buffers _after_
  743. * unlocking. We don't know the size until the lock is held and
  744. * we can't allocate while the lock is held. No one is using
  745. * the context yet, so allocate it now.
  746. *
  747. * User contexts do not get a shadow ring.
  748. */
  749. if (type != SC_USER) {
  750. /*
  751. * Size the shadow ring 1 larger than the number of credits
  752. * so head == tail can mean empty.
  753. */
  754. sc->sr_size = sci->credits + 1;
  755. sc->sr = kzalloc_node(sizeof(union pio_shadow_ring) *
  756. sc->sr_size, GFP_KERNEL, numa);
  757. if (!sc->sr) {
  758. sc_free(sc);
  759. return NULL;
  760. }
  761. }
  762. hfi1_cdbg(PIO,
  763. "Send context %u(%u) %s group %u credits %u credit_ctrl 0x%llx threshold %u\n",
  764. sw_index,
  765. hw_context,
  766. sc_type_name(type),
  767. sc->group,
  768. sc->credits,
  769. sc->credit_ctrl,
  770. thresh);
  771. return sc;
  772. }
  773. /* free a per-NUMA send context structure */
  774. void sc_free(struct send_context *sc)
  775. {
  776. struct hfi1_devdata *dd;
  777. unsigned long flags;
  778. u32 sw_index;
  779. u32 hw_context;
  780. if (!sc)
  781. return;
  782. sc->flags |= SCF_IN_FREE; /* ensure no restarts */
  783. dd = sc->dd;
  784. if (!list_empty(&sc->piowait))
  785. dd_dev_err(dd, "piowait list not empty!\n");
  786. sw_index = sc->sw_index;
  787. hw_context = sc->hw_context;
  788. sc_disable(sc); /* make sure the HW is disabled */
  789. flush_work(&sc->halt_work);
  790. spin_lock_irqsave(&dd->sc_lock, flags);
  791. dd->send_contexts[sw_index].sc = NULL;
  792. /* clear/disable all registers set in sc_alloc */
  793. write_kctxt_csr(dd, hw_context, SC(CTRL), 0);
  794. write_kctxt_csr(dd, hw_context, SC(CHECK_ENABLE), 0);
  795. write_kctxt_csr(dd, hw_context, SC(ERR_MASK), 0);
  796. write_kctxt_csr(dd, hw_context, SC(CHECK_PARTITION_KEY), 0);
  797. write_kctxt_csr(dd, hw_context, SC(CHECK_OPCODE), 0);
  798. write_kctxt_csr(dd, hw_context, SC(CREDIT_RETURN_ADDR), 0);
  799. write_kctxt_csr(dd, hw_context, SC(CREDIT_CTRL), 0);
  800. /* release the index and context for re-use */
  801. sc_hw_free(dd, sw_index, hw_context);
  802. spin_unlock_irqrestore(&dd->sc_lock, flags);
  803. kfree(sc->sr);
  804. free_percpu(sc->buffers_allocated);
  805. kfree(sc);
  806. }
  807. /* disable the context */
  808. void sc_disable(struct send_context *sc)
  809. {
  810. u64 reg;
  811. unsigned long flags;
  812. struct pio_buf *pbuf;
  813. if (!sc)
  814. return;
  815. /* do all steps, even if already disabled */
  816. spin_lock_irqsave(&sc->alloc_lock, flags);
  817. reg = read_kctxt_csr(sc->dd, sc->hw_context, SC(CTRL));
  818. reg &= ~SC(CTRL_CTXT_ENABLE_SMASK);
  819. sc->flags &= ~SCF_ENABLED;
  820. sc_wait_for_packet_egress(sc, 1);
  821. write_kctxt_csr(sc->dd, sc->hw_context, SC(CTRL), reg);
  822. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  823. /*
  824. * Flush any waiters. Once the context is disabled,
  825. * credit return interrupts are stopped (although there
  826. * could be one in-process when the context is disabled).
  827. * Wait one microsecond for any lingering interrupts, then
  828. * proceed with the flush.
  829. */
  830. udelay(1);
  831. spin_lock_irqsave(&sc->release_lock, flags);
  832. if (sc->sr) { /* this context has a shadow ring */
  833. while (sc->sr_tail != sc->sr_head) {
  834. pbuf = &sc->sr[sc->sr_tail].pbuf;
  835. if (pbuf->cb)
  836. (*pbuf->cb)(pbuf->arg, PRC_SC_DISABLE);
  837. sc->sr_tail++;
  838. if (sc->sr_tail >= sc->sr_size)
  839. sc->sr_tail = 0;
  840. }
  841. }
  842. spin_unlock_irqrestore(&sc->release_lock, flags);
  843. }
  844. /* return SendEgressCtxtStatus.PacketOccupancy */
  845. #define packet_occupancy(r) \
  846. (((r) & SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SMASK)\
  847. >> SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_PACKET_OCCUPANCY_SHIFT)
  848. /* is egress halted on the context? */
  849. #define egress_halted(r) \
  850. ((r) & SEND_EGRESS_CTXT_STATUS_CTXT_EGRESS_HALT_STATUS_SMASK)
  851. /* wait for packet egress, optionally pause for credit return */
  852. static void sc_wait_for_packet_egress(struct send_context *sc, int pause)
  853. {
  854. struct hfi1_devdata *dd = sc->dd;
  855. u64 reg = 0;
  856. u64 reg_prev;
  857. u32 loop = 0;
  858. while (1) {
  859. reg_prev = reg;
  860. reg = read_csr(dd, sc->hw_context * 8 +
  861. SEND_EGRESS_CTXT_STATUS);
  862. /* done if egress is stopped */
  863. if (egress_halted(reg))
  864. break;
  865. reg = packet_occupancy(reg);
  866. if (reg == 0)
  867. break;
  868. /* counter is reset if occupancy count changes */
  869. if (reg != reg_prev)
  870. loop = 0;
  871. if (loop > 50000) {
  872. /* timed out - bounce the link */
  873. dd_dev_err(dd,
  874. "%s: context %u(%u) timeout waiting for packets to egress, remaining count %u, bouncing link\n",
  875. __func__, sc->sw_index,
  876. sc->hw_context, (u32)reg);
  877. queue_work(dd->pport->hfi1_wq,
  878. &dd->pport->link_bounce_work);
  879. break;
  880. }
  881. loop++;
  882. udelay(1);
  883. }
  884. if (pause)
  885. /* Add additional delay to ensure chip returns all credits */
  886. pause_for_credit_return(dd);
  887. }
  888. void sc_wait(struct hfi1_devdata *dd)
  889. {
  890. int i;
  891. for (i = 0; i < dd->num_send_contexts; i++) {
  892. struct send_context *sc = dd->send_contexts[i].sc;
  893. if (!sc)
  894. continue;
  895. sc_wait_for_packet_egress(sc, 0);
  896. }
  897. }
  898. /*
  899. * Restart a context after it has been halted due to error.
  900. *
  901. * If the first step fails - wait for the halt to be asserted, return early.
  902. * Otherwise complain about timeouts but keep going.
  903. *
  904. * It is expected that allocations (enabled flag bit) have been shut off
  905. * already (only applies to kernel contexts).
  906. */
  907. int sc_restart(struct send_context *sc)
  908. {
  909. struct hfi1_devdata *dd = sc->dd;
  910. u64 reg;
  911. u32 loop;
  912. int count;
  913. /* bounce off if not halted, or being free'd */
  914. if (!(sc->flags & SCF_HALTED) || (sc->flags & SCF_IN_FREE))
  915. return -EINVAL;
  916. dd_dev_info(dd, "restarting send context %u(%u)\n", sc->sw_index,
  917. sc->hw_context);
  918. /*
  919. * Step 1: Wait for the context to actually halt.
  920. *
  921. * The error interrupt is asynchronous to actually setting halt
  922. * on the context.
  923. */
  924. loop = 0;
  925. while (1) {
  926. reg = read_kctxt_csr(dd, sc->hw_context, SC(STATUS));
  927. if (reg & SC(STATUS_CTXT_HALTED_SMASK))
  928. break;
  929. if (loop > 100) {
  930. dd_dev_err(dd, "%s: context %u(%u) not halting, skipping\n",
  931. __func__, sc->sw_index, sc->hw_context);
  932. return -ETIME;
  933. }
  934. loop++;
  935. udelay(1);
  936. }
  937. /*
  938. * Step 2: Ensure no users are still trying to write to PIO.
  939. *
  940. * For kernel contexts, we have already turned off buffer allocation.
  941. * Now wait for the buffer count to go to zero.
  942. *
  943. * For user contexts, the user handling code has cut off write access
  944. * to the context's PIO pages before calling this routine and will
  945. * restore write access after this routine returns.
  946. */
  947. if (sc->type != SC_USER) {
  948. /* kernel context */
  949. loop = 0;
  950. while (1) {
  951. count = get_buffers_allocated(sc);
  952. if (count == 0)
  953. break;
  954. if (loop > 100) {
  955. dd_dev_err(dd,
  956. "%s: context %u(%u) timeout waiting for PIO buffers to zero, remaining %d\n",
  957. __func__, sc->sw_index,
  958. sc->hw_context, count);
  959. }
  960. loop++;
  961. udelay(1);
  962. }
  963. }
  964. /*
  965. * Step 3: Wait for all packets to egress.
  966. * This is done while disabling the send context
  967. *
  968. * Step 4: Disable the context
  969. *
  970. * This is a superset of the halt. After the disable, the
  971. * errors can be cleared.
  972. */
  973. sc_disable(sc);
  974. /*
  975. * Step 5: Enable the context
  976. *
  977. * This enable will clear the halted flag and per-send context
  978. * error flags.
  979. */
  980. return sc_enable(sc);
  981. }
  982. /*
  983. * PIO freeze processing. To be called after the TXE block is fully frozen.
  984. * Go through all frozen send contexts and disable them. The contexts are
  985. * already stopped by the freeze.
  986. */
  987. void pio_freeze(struct hfi1_devdata *dd)
  988. {
  989. struct send_context *sc;
  990. int i;
  991. for (i = 0; i < dd->num_send_contexts; i++) {
  992. sc = dd->send_contexts[i].sc;
  993. /*
  994. * Don't disable unallocated, unfrozen, or user send contexts.
  995. * User send contexts will be disabled when the process
  996. * calls into the driver to reset its context.
  997. */
  998. if (!sc || !(sc->flags & SCF_FROZEN) || sc->type == SC_USER)
  999. continue;
  1000. /* only need to disable, the context is already stopped */
  1001. sc_disable(sc);
  1002. }
  1003. }
  1004. /*
  1005. * Unfreeze PIO for kernel send contexts. The precondition for calling this
  1006. * is that all PIO send contexts have been disabled and the SPC freeze has
  1007. * been cleared. Now perform the last step and re-enable each kernel context.
  1008. * User (PSM) processing will occur when PSM calls into the kernel to
  1009. * acknowledge the freeze.
  1010. */
  1011. void pio_kernel_unfreeze(struct hfi1_devdata *dd)
  1012. {
  1013. struct send_context *sc;
  1014. int i;
  1015. for (i = 0; i < dd->num_send_contexts; i++) {
  1016. sc = dd->send_contexts[i].sc;
  1017. if (!sc || !(sc->flags & SCF_FROZEN) || sc->type == SC_USER)
  1018. continue;
  1019. sc_enable(sc); /* will clear the sc frozen flag */
  1020. }
  1021. }
  1022. /*
  1023. * Wait for the SendPioInitCtxt.PioInitInProgress bit to clear.
  1024. * Returns:
  1025. * -ETIMEDOUT - if we wait too long
  1026. * -EIO - if there was an error
  1027. */
  1028. static int pio_init_wait_progress(struct hfi1_devdata *dd)
  1029. {
  1030. u64 reg;
  1031. int max, count = 0;
  1032. /* max is the longest possible HW init time / delay */
  1033. max = (dd->icode == ICODE_FPGA_EMULATION) ? 120 : 5;
  1034. while (1) {
  1035. reg = read_csr(dd, SEND_PIO_INIT_CTXT);
  1036. if (!(reg & SEND_PIO_INIT_CTXT_PIO_INIT_IN_PROGRESS_SMASK))
  1037. break;
  1038. if (count >= max)
  1039. return -ETIMEDOUT;
  1040. udelay(5);
  1041. count++;
  1042. }
  1043. return reg & SEND_PIO_INIT_CTXT_PIO_INIT_ERR_SMASK ? -EIO : 0;
  1044. }
  1045. /*
  1046. * Reset all of the send contexts to their power-on state. Used
  1047. * only during manual init - no lock against sc_enable needed.
  1048. */
  1049. void pio_reset_all(struct hfi1_devdata *dd)
  1050. {
  1051. int ret;
  1052. /* make sure the init engine is not busy */
  1053. ret = pio_init_wait_progress(dd);
  1054. /* ignore any timeout */
  1055. if (ret == -EIO) {
  1056. /* clear the error */
  1057. write_csr(dd, SEND_PIO_ERR_CLEAR,
  1058. SEND_PIO_ERR_CLEAR_PIO_INIT_SM_IN_ERR_SMASK);
  1059. }
  1060. /* reset init all */
  1061. write_csr(dd, SEND_PIO_INIT_CTXT,
  1062. SEND_PIO_INIT_CTXT_PIO_ALL_CTXT_INIT_SMASK);
  1063. udelay(2);
  1064. ret = pio_init_wait_progress(dd);
  1065. if (ret < 0) {
  1066. dd_dev_err(dd,
  1067. "PIO send context init %s while initializing all PIO blocks\n",
  1068. ret == -ETIMEDOUT ? "is stuck" : "had an error");
  1069. }
  1070. }
  1071. /* enable the context */
  1072. int sc_enable(struct send_context *sc)
  1073. {
  1074. u64 sc_ctrl, reg, pio;
  1075. struct hfi1_devdata *dd;
  1076. unsigned long flags;
  1077. int ret = 0;
  1078. if (!sc)
  1079. return -EINVAL;
  1080. dd = sc->dd;
  1081. /*
  1082. * Obtain the allocator lock to guard against any allocation
  1083. * attempts (which should not happen prior to context being
  1084. * enabled). On the release/disable side we don't need to
  1085. * worry about locking since the releaser will not do anything
  1086. * if the context accounting values have not changed.
  1087. */
  1088. spin_lock_irqsave(&sc->alloc_lock, flags);
  1089. sc_ctrl = read_kctxt_csr(dd, sc->hw_context, SC(CTRL));
  1090. if ((sc_ctrl & SC(CTRL_CTXT_ENABLE_SMASK)))
  1091. goto unlock; /* already enabled */
  1092. /* IMPORTANT: only clear free and fill if transitioning 0 -> 1 */
  1093. *sc->hw_free = 0;
  1094. sc->free = 0;
  1095. sc->alloc_free = 0;
  1096. sc->fill = 0;
  1097. sc->sr_head = 0;
  1098. sc->sr_tail = 0;
  1099. sc->flags = 0;
  1100. /* the alloc lock insures no fast path allocation */
  1101. reset_buffers_allocated(sc);
  1102. /*
  1103. * Clear all per-context errors. Some of these will be set when
  1104. * we are re-enabling after a context halt. Now that the context
  1105. * is disabled, the halt will not clear until after the PIO init
  1106. * engine runs below.
  1107. */
  1108. reg = read_kctxt_csr(dd, sc->hw_context, SC(ERR_STATUS));
  1109. if (reg)
  1110. write_kctxt_csr(dd, sc->hw_context, SC(ERR_CLEAR), reg);
  1111. /*
  1112. * The HW PIO initialization engine can handle only one init
  1113. * request at a time. Serialize access to each device's engine.
  1114. */
  1115. spin_lock(&dd->sc_init_lock);
  1116. /*
  1117. * Since access to this code block is serialized and
  1118. * each access waits for the initialization to complete
  1119. * before releasing the lock, the PIO initialization engine
  1120. * should not be in use, so we don't have to wait for the
  1121. * InProgress bit to go down.
  1122. */
  1123. pio = ((sc->hw_context & SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_MASK) <<
  1124. SEND_PIO_INIT_CTXT_PIO_CTXT_NUM_SHIFT) |
  1125. SEND_PIO_INIT_CTXT_PIO_SINGLE_CTXT_INIT_SMASK;
  1126. write_csr(dd, SEND_PIO_INIT_CTXT, pio);
  1127. /*
  1128. * Wait until the engine is done. Give the chip the required time
  1129. * so, hopefully, we read the register just once.
  1130. */
  1131. udelay(2);
  1132. ret = pio_init_wait_progress(dd);
  1133. spin_unlock(&dd->sc_init_lock);
  1134. if (ret) {
  1135. dd_dev_err(dd,
  1136. "sctxt%u(%u): Context not enabled due to init failure %d\n",
  1137. sc->sw_index, sc->hw_context, ret);
  1138. goto unlock;
  1139. }
  1140. /*
  1141. * All is well. Enable the context.
  1142. */
  1143. sc_ctrl |= SC(CTRL_CTXT_ENABLE_SMASK);
  1144. write_kctxt_csr(dd, sc->hw_context, SC(CTRL), sc_ctrl);
  1145. /*
  1146. * Read SendCtxtCtrl to force the write out and prevent a timing
  1147. * hazard where a PIO write may reach the context before the enable.
  1148. */
  1149. read_kctxt_csr(dd, sc->hw_context, SC(CTRL));
  1150. sc->flags |= SCF_ENABLED;
  1151. unlock:
  1152. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  1153. return ret;
  1154. }
  1155. /* force a credit return on the context */
  1156. void sc_return_credits(struct send_context *sc)
  1157. {
  1158. if (!sc)
  1159. return;
  1160. /* a 0->1 transition schedules a credit return */
  1161. write_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE),
  1162. SC(CREDIT_FORCE_FORCE_RETURN_SMASK));
  1163. /*
  1164. * Ensure that the write is flushed and the credit return is
  1165. * scheduled. We care more about the 0 -> 1 transition.
  1166. */
  1167. read_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE));
  1168. /* set back to 0 for next time */
  1169. write_kctxt_csr(sc->dd, sc->hw_context, SC(CREDIT_FORCE), 0);
  1170. }
  1171. /* allow all in-flight packets to drain on the context */
  1172. void sc_flush(struct send_context *sc)
  1173. {
  1174. if (!sc)
  1175. return;
  1176. sc_wait_for_packet_egress(sc, 1);
  1177. }
  1178. /* drop all packets on the context, no waiting until they are sent */
  1179. void sc_drop(struct send_context *sc)
  1180. {
  1181. if (!sc)
  1182. return;
  1183. dd_dev_info(sc->dd, "%s: context %u(%u) - not implemented\n",
  1184. __func__, sc->sw_index, sc->hw_context);
  1185. }
  1186. /*
  1187. * Start the software reaction to a context halt or SPC freeze:
  1188. * - mark the context as halted or frozen
  1189. * - stop buffer allocations
  1190. *
  1191. * Called from the error interrupt. Other work is deferred until
  1192. * out of the interrupt.
  1193. */
  1194. void sc_stop(struct send_context *sc, int flag)
  1195. {
  1196. unsigned long flags;
  1197. /* mark the context */
  1198. sc->flags |= flag;
  1199. /* stop buffer allocations */
  1200. spin_lock_irqsave(&sc->alloc_lock, flags);
  1201. sc->flags &= ~SCF_ENABLED;
  1202. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  1203. wake_up(&sc->halt_wait);
  1204. }
  1205. #define BLOCK_DWORDS (PIO_BLOCK_SIZE / sizeof(u32))
  1206. #define dwords_to_blocks(x) DIV_ROUND_UP(x, BLOCK_DWORDS)
  1207. /*
  1208. * The send context buffer "allocator".
  1209. *
  1210. * @sc: the PIO send context we are allocating from
  1211. * @len: length of whole packet - including PBC - in dwords
  1212. * @cb: optional callback to call when the buffer is finished sending
  1213. * @arg: argument for cb
  1214. *
  1215. * Return a pointer to a PIO buffer if successful, NULL if not enough room.
  1216. */
  1217. struct pio_buf *sc_buffer_alloc(struct send_context *sc, u32 dw_len,
  1218. pio_release_cb cb, void *arg)
  1219. {
  1220. struct pio_buf *pbuf = NULL;
  1221. unsigned long flags;
  1222. unsigned long avail;
  1223. unsigned long blocks = dwords_to_blocks(dw_len);
  1224. unsigned long start_fill;
  1225. int trycount = 0;
  1226. u32 head, next;
  1227. spin_lock_irqsave(&sc->alloc_lock, flags);
  1228. if (!(sc->flags & SCF_ENABLED)) {
  1229. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  1230. goto done;
  1231. }
  1232. retry:
  1233. avail = (unsigned long)sc->credits - (sc->fill - sc->alloc_free);
  1234. if (blocks > avail) {
  1235. /* not enough room */
  1236. if (unlikely(trycount)) { /* already tried to get more room */
  1237. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  1238. goto done;
  1239. }
  1240. /* copy from receiver cache line and recalculate */
  1241. sc->alloc_free = ACCESS_ONCE(sc->free);
  1242. avail =
  1243. (unsigned long)sc->credits -
  1244. (sc->fill - sc->alloc_free);
  1245. if (blocks > avail) {
  1246. /* still no room, actively update */
  1247. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  1248. sc_release_update(sc);
  1249. spin_lock_irqsave(&sc->alloc_lock, flags);
  1250. sc->alloc_free = ACCESS_ONCE(sc->free);
  1251. trycount++;
  1252. goto retry;
  1253. }
  1254. }
  1255. /* there is enough room */
  1256. preempt_disable();
  1257. this_cpu_inc(*sc->buffers_allocated);
  1258. /* read this once */
  1259. head = sc->sr_head;
  1260. /* "allocate" the buffer */
  1261. start_fill = sc->fill;
  1262. sc->fill += blocks;
  1263. /*
  1264. * Fill the parts that the releaser looks at before moving the head.
  1265. * The only necessary piece is the sent_at field. The credits
  1266. * we have just allocated cannot have been returned yet, so the
  1267. * cb and arg will not be looked at for a "while". Put them
  1268. * on this side of the memory barrier anyway.
  1269. */
  1270. pbuf = &sc->sr[head].pbuf;
  1271. pbuf->sent_at = sc->fill;
  1272. pbuf->cb = cb;
  1273. pbuf->arg = arg;
  1274. pbuf->sc = sc; /* could be filled in at sc->sr init time */
  1275. /* make sure this is in memory before updating the head */
  1276. /* calculate next head index, do not store */
  1277. next = head + 1;
  1278. if (next >= sc->sr_size)
  1279. next = 0;
  1280. /*
  1281. * update the head - must be last! - the releaser can look at fields
  1282. * in pbuf once we move the head
  1283. */
  1284. smp_wmb();
  1285. sc->sr_head = next;
  1286. spin_unlock_irqrestore(&sc->alloc_lock, flags);
  1287. /* finish filling in the buffer outside the lock */
  1288. pbuf->start = sc->base_addr + ((start_fill % sc->credits)
  1289. * PIO_BLOCK_SIZE);
  1290. pbuf->size = sc->credits * PIO_BLOCK_SIZE;
  1291. pbuf->end = sc->base_addr + pbuf->size;
  1292. pbuf->block_count = blocks;
  1293. pbuf->qw_written = 0;
  1294. pbuf->carry_bytes = 0;
  1295. pbuf->carry.val64 = 0;
  1296. done:
  1297. return pbuf;
  1298. }
  1299. /*
  1300. * There are at least two entities that can turn on credit return
  1301. * interrupts and they can overlap. Avoid problems by implementing
  1302. * a count scheme that is enforced by a lock. The lock is needed because
  1303. * the count and CSR write must be paired.
  1304. */
  1305. /*
  1306. * Start credit return interrupts. This is managed by a count. If already
  1307. * on, just increment the count.
  1308. */
  1309. void sc_add_credit_return_intr(struct send_context *sc)
  1310. {
  1311. unsigned long flags;
  1312. /* lock must surround both the count change and the CSR update */
  1313. spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
  1314. if (sc->credit_intr_count == 0) {
  1315. sc->credit_ctrl |= SC(CREDIT_CTRL_CREDIT_INTR_SMASK);
  1316. write_kctxt_csr(sc->dd, sc->hw_context,
  1317. SC(CREDIT_CTRL), sc->credit_ctrl);
  1318. }
  1319. sc->credit_intr_count++;
  1320. spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
  1321. }
  1322. /*
  1323. * Stop credit return interrupts. This is managed by a count. Decrement the
  1324. * count, if the last user, then turn the credit interrupts off.
  1325. */
  1326. void sc_del_credit_return_intr(struct send_context *sc)
  1327. {
  1328. unsigned long flags;
  1329. WARN_ON(sc->credit_intr_count == 0);
  1330. /* lock must surround both the count change and the CSR update */
  1331. spin_lock_irqsave(&sc->credit_ctrl_lock, flags);
  1332. sc->credit_intr_count--;
  1333. if (sc->credit_intr_count == 0) {
  1334. sc->credit_ctrl &= ~SC(CREDIT_CTRL_CREDIT_INTR_SMASK);
  1335. write_kctxt_csr(sc->dd, sc->hw_context,
  1336. SC(CREDIT_CTRL), sc->credit_ctrl);
  1337. }
  1338. spin_unlock_irqrestore(&sc->credit_ctrl_lock, flags);
  1339. }
  1340. /*
  1341. * The caller must be careful when calling this. All needint calls
  1342. * must be paired with !needint.
  1343. */
  1344. void hfi1_sc_wantpiobuf_intr(struct send_context *sc, u32 needint)
  1345. {
  1346. if (needint)
  1347. sc_add_credit_return_intr(sc);
  1348. else
  1349. sc_del_credit_return_intr(sc);
  1350. trace_hfi1_wantpiointr(sc, needint, sc->credit_ctrl);
  1351. if (needint) {
  1352. mmiowb();
  1353. sc_return_credits(sc);
  1354. }
  1355. }
  1356. /**
  1357. * sc_piobufavail - callback when a PIO buffer is available
  1358. * @sc: the send context
  1359. *
  1360. * This is called from the interrupt handler when a PIO buffer is
  1361. * available after hfi1_verbs_send() returned an error that no buffers were
  1362. * available. Disable the interrupt if there are no more QPs waiting.
  1363. */
  1364. static void sc_piobufavail(struct send_context *sc)
  1365. {
  1366. struct hfi1_devdata *dd = sc->dd;
  1367. struct hfi1_ibdev *dev = &dd->verbs_dev;
  1368. struct list_head *list;
  1369. struct rvt_qp *qps[PIO_WAIT_BATCH_SIZE];
  1370. struct rvt_qp *qp;
  1371. struct hfi1_qp_priv *priv;
  1372. unsigned long flags;
  1373. unsigned i, n = 0;
  1374. if (dd->send_contexts[sc->sw_index].type != SC_KERNEL &&
  1375. dd->send_contexts[sc->sw_index].type != SC_VL15)
  1376. return;
  1377. list = &sc->piowait;
  1378. /*
  1379. * Note: checking that the piowait list is empty and clearing
  1380. * the buffer available interrupt needs to be atomic or we
  1381. * could end up with QPs on the wait list with the interrupt
  1382. * disabled.
  1383. */
  1384. write_seqlock_irqsave(&dev->iowait_lock, flags);
  1385. while (!list_empty(list)) {
  1386. struct iowait *wait;
  1387. if (n == ARRAY_SIZE(qps))
  1388. break;
  1389. wait = list_first_entry(list, struct iowait, list);
  1390. qp = iowait_to_qp(wait);
  1391. priv = qp->priv;
  1392. list_del_init(&priv->s_iowait.list);
  1393. /* refcount held until actual wake up */
  1394. qps[n++] = qp;
  1395. }
  1396. /*
  1397. * If there had been waiters and there are more
  1398. * insure that we redo the force to avoid a potential hang.
  1399. */
  1400. if (n) {
  1401. hfi1_sc_wantpiobuf_intr(sc, 0);
  1402. if (!list_empty(list))
  1403. hfi1_sc_wantpiobuf_intr(sc, 1);
  1404. }
  1405. write_sequnlock_irqrestore(&dev->iowait_lock, flags);
  1406. for (i = 0; i < n; i++)
  1407. hfi1_qp_wakeup(qps[i],
  1408. RVT_S_WAIT_PIO | RVT_S_WAIT_PIO_DRAIN);
  1409. }
  1410. /* translate a send credit update to a bit code of reasons */
  1411. static inline int fill_code(u64 hw_free)
  1412. {
  1413. int code = 0;
  1414. if (hw_free & CR_STATUS_SMASK)
  1415. code |= PRC_STATUS_ERR;
  1416. if (hw_free & CR_CREDIT_RETURN_DUE_TO_PBC_SMASK)
  1417. code |= PRC_PBC;
  1418. if (hw_free & CR_CREDIT_RETURN_DUE_TO_THRESHOLD_SMASK)
  1419. code |= PRC_THRESHOLD;
  1420. if (hw_free & CR_CREDIT_RETURN_DUE_TO_ERR_SMASK)
  1421. code |= PRC_FILL_ERR;
  1422. if (hw_free & CR_CREDIT_RETURN_DUE_TO_FORCE_SMASK)
  1423. code |= PRC_SC_DISABLE;
  1424. return code;
  1425. }
  1426. /* use the jiffies compare to get the wrap right */
  1427. #define sent_before(a, b) time_before(a, b) /* a < b */
  1428. /*
  1429. * The send context buffer "releaser".
  1430. */
  1431. void sc_release_update(struct send_context *sc)
  1432. {
  1433. struct pio_buf *pbuf;
  1434. u64 hw_free;
  1435. u32 head, tail;
  1436. unsigned long old_free;
  1437. unsigned long free;
  1438. unsigned long extra;
  1439. unsigned long flags;
  1440. int code;
  1441. if (!sc)
  1442. return;
  1443. spin_lock_irqsave(&sc->release_lock, flags);
  1444. /* update free */
  1445. hw_free = le64_to_cpu(*sc->hw_free); /* volatile read */
  1446. old_free = sc->free;
  1447. extra = (((hw_free & CR_COUNTER_SMASK) >> CR_COUNTER_SHIFT)
  1448. - (old_free & CR_COUNTER_MASK))
  1449. & CR_COUNTER_MASK;
  1450. free = old_free + extra;
  1451. trace_hfi1_piofree(sc, extra);
  1452. /* call sent buffer callbacks */
  1453. code = -1; /* code not yet set */
  1454. head = ACCESS_ONCE(sc->sr_head); /* snapshot the head */
  1455. tail = sc->sr_tail;
  1456. while (head != tail) {
  1457. pbuf = &sc->sr[tail].pbuf;
  1458. if (sent_before(free, pbuf->sent_at)) {
  1459. /* not sent yet */
  1460. break;
  1461. }
  1462. if (pbuf->cb) {
  1463. if (code < 0) /* fill in code on first user */
  1464. code = fill_code(hw_free);
  1465. (*pbuf->cb)(pbuf->arg, code);
  1466. }
  1467. tail++;
  1468. if (tail >= sc->sr_size)
  1469. tail = 0;
  1470. }
  1471. sc->sr_tail = tail;
  1472. /* make sure tail is updated before free */
  1473. smp_wmb();
  1474. sc->free = free;
  1475. spin_unlock_irqrestore(&sc->release_lock, flags);
  1476. sc_piobufavail(sc);
  1477. }
  1478. /*
  1479. * Send context group releaser. Argument is the send context that caused
  1480. * the interrupt. Called from the send context interrupt handler.
  1481. *
  1482. * Call release on all contexts in the group.
  1483. *
  1484. * This routine takes the sc_lock without an irqsave because it is only
  1485. * called from an interrupt handler. Adjust if that changes.
  1486. */
  1487. void sc_group_release_update(struct hfi1_devdata *dd, u32 hw_context)
  1488. {
  1489. struct send_context *sc;
  1490. u32 sw_index;
  1491. u32 gc, gc_end;
  1492. spin_lock(&dd->sc_lock);
  1493. sw_index = dd->hw_to_sw[hw_context];
  1494. if (unlikely(sw_index >= dd->num_send_contexts)) {
  1495. dd_dev_err(dd, "%s: invalid hw (%u) to sw (%u) mapping\n",
  1496. __func__, hw_context, sw_index);
  1497. goto done;
  1498. }
  1499. sc = dd->send_contexts[sw_index].sc;
  1500. if (unlikely(!sc))
  1501. goto done;
  1502. gc = group_context(hw_context, sc->group);
  1503. gc_end = gc + group_size(sc->group);
  1504. for (; gc < gc_end; gc++) {
  1505. sw_index = dd->hw_to_sw[gc];
  1506. if (unlikely(sw_index >= dd->num_send_contexts)) {
  1507. dd_dev_err(dd,
  1508. "%s: invalid hw (%u) to sw (%u) mapping\n",
  1509. __func__, hw_context, sw_index);
  1510. continue;
  1511. }
  1512. sc_release_update(dd->send_contexts[sw_index].sc);
  1513. }
  1514. done:
  1515. spin_unlock(&dd->sc_lock);
  1516. }
  1517. /*
  1518. * pio_select_send_context_vl() - select send context
  1519. * @dd: devdata
  1520. * @selector: a spreading factor
  1521. * @vl: this vl
  1522. *
  1523. * This function returns a send context based on the selector and a vl.
  1524. * The mapping fields are protected by RCU
  1525. */
  1526. struct send_context *pio_select_send_context_vl(struct hfi1_devdata *dd,
  1527. u32 selector, u8 vl)
  1528. {
  1529. struct pio_vl_map *m;
  1530. struct pio_map_elem *e;
  1531. struct send_context *rval;
  1532. /*
  1533. * NOTE This should only happen if SC->VL changed after the initial
  1534. * checks on the QP/AH
  1535. * Default will return VL0's send context below
  1536. */
  1537. if (unlikely(vl >= num_vls)) {
  1538. rval = NULL;
  1539. goto done;
  1540. }
  1541. rcu_read_lock();
  1542. m = rcu_dereference(dd->pio_map);
  1543. if (unlikely(!m)) {
  1544. rcu_read_unlock();
  1545. return dd->vld[0].sc;
  1546. }
  1547. e = m->map[vl & m->mask];
  1548. rval = e->ksc[selector & e->mask];
  1549. rcu_read_unlock();
  1550. done:
  1551. rval = !rval ? dd->vld[0].sc : rval;
  1552. return rval;
  1553. }
  1554. /*
  1555. * pio_select_send_context_sc() - select send context
  1556. * @dd: devdata
  1557. * @selector: a spreading factor
  1558. * @sc5: the 5 bit sc
  1559. *
  1560. * This function returns an send context based on the selector and an sc
  1561. */
  1562. struct send_context *pio_select_send_context_sc(struct hfi1_devdata *dd,
  1563. u32 selector, u8 sc5)
  1564. {
  1565. u8 vl = sc_to_vlt(dd, sc5);
  1566. return pio_select_send_context_vl(dd, selector, vl);
  1567. }
  1568. /*
  1569. * Free the indicated map struct
  1570. */
  1571. static void pio_map_free(struct pio_vl_map *m)
  1572. {
  1573. int i;
  1574. for (i = 0; m && i < m->actual_vls; i++)
  1575. kfree(m->map[i]);
  1576. kfree(m);
  1577. }
  1578. /*
  1579. * Handle RCU callback
  1580. */
  1581. static void pio_map_rcu_callback(struct rcu_head *list)
  1582. {
  1583. struct pio_vl_map *m = container_of(list, struct pio_vl_map, list);
  1584. pio_map_free(m);
  1585. }
  1586. /*
  1587. * Set credit return threshold for the kernel send context
  1588. */
  1589. static void set_threshold(struct hfi1_devdata *dd, int scontext, int i)
  1590. {
  1591. u32 thres;
  1592. thres = min(sc_percent_to_threshold(dd->kernel_send_context[scontext],
  1593. 50),
  1594. sc_mtu_to_threshold(dd->kernel_send_context[scontext],
  1595. dd->vld[i].mtu,
  1596. dd->rcd[0]->rcvhdrqentsize));
  1597. sc_set_cr_threshold(dd->kernel_send_context[scontext], thres);
  1598. }
  1599. /*
  1600. * pio_map_init - called when #vls change
  1601. * @dd: hfi1_devdata
  1602. * @port: port number
  1603. * @num_vls: number of vls
  1604. * @vl_scontexts: per vl send context mapping (optional)
  1605. *
  1606. * This routine changes the mapping based on the number of vls.
  1607. *
  1608. * vl_scontexts is used to specify a non-uniform vl/send context
  1609. * loading. NULL implies auto computing the loading and giving each
  1610. * VL an uniform distribution of send contexts per VL.
  1611. *
  1612. * The auto algorithm computers the sc_per_vl and the number of extra
  1613. * send contexts. Any extra send contexts are added from the last VL
  1614. * on down
  1615. *
  1616. * rcu locking is used here to control access to the mapping fields.
  1617. *
  1618. * If either the num_vls or num_send_contexts are non-power of 2, the
  1619. * array sizes in the struct pio_vl_map and the struct pio_map_elem are
  1620. * rounded up to the next highest power of 2 and the first entry is
  1621. * reused in a round robin fashion.
  1622. *
  1623. * If an error occurs the map change is not done and the mapping is not
  1624. * chaged.
  1625. *
  1626. */
  1627. int pio_map_init(struct hfi1_devdata *dd, u8 port, u8 num_vls, u8 *vl_scontexts)
  1628. {
  1629. int i, j;
  1630. int extra, sc_per_vl;
  1631. int scontext = 1;
  1632. int num_kernel_send_contexts = 0;
  1633. u8 lvl_scontexts[OPA_MAX_VLS];
  1634. struct pio_vl_map *oldmap, *newmap;
  1635. if (!vl_scontexts) {
  1636. for (i = 0; i < dd->num_send_contexts; i++)
  1637. if (dd->send_contexts[i].type == SC_KERNEL)
  1638. num_kernel_send_contexts++;
  1639. /* truncate divide */
  1640. sc_per_vl = num_kernel_send_contexts / num_vls;
  1641. /* extras */
  1642. extra = num_kernel_send_contexts % num_vls;
  1643. vl_scontexts = lvl_scontexts;
  1644. /* add extras from last vl down */
  1645. for (i = num_vls - 1; i >= 0; i--, extra--)
  1646. vl_scontexts[i] = sc_per_vl + (extra > 0 ? 1 : 0);
  1647. }
  1648. /* build new map */
  1649. newmap = kzalloc(sizeof(*newmap) +
  1650. roundup_pow_of_two(num_vls) *
  1651. sizeof(struct pio_map_elem *),
  1652. GFP_KERNEL);
  1653. if (!newmap)
  1654. goto bail;
  1655. newmap->actual_vls = num_vls;
  1656. newmap->vls = roundup_pow_of_two(num_vls);
  1657. newmap->mask = (1 << ilog2(newmap->vls)) - 1;
  1658. for (i = 0; i < newmap->vls; i++) {
  1659. /* save for wrap around */
  1660. int first_scontext = scontext;
  1661. if (i < newmap->actual_vls) {
  1662. int sz = roundup_pow_of_two(vl_scontexts[i]);
  1663. /* only allocate once */
  1664. newmap->map[i] = kzalloc(sizeof(*newmap->map[i]) +
  1665. sz * sizeof(struct
  1666. send_context *),
  1667. GFP_KERNEL);
  1668. if (!newmap->map[i])
  1669. goto bail;
  1670. newmap->map[i]->mask = (1 << ilog2(sz)) - 1;
  1671. /*
  1672. * assign send contexts and
  1673. * adjust credit return threshold
  1674. */
  1675. for (j = 0; j < sz; j++) {
  1676. if (dd->kernel_send_context[scontext]) {
  1677. newmap->map[i]->ksc[j] =
  1678. dd->kernel_send_context[scontext];
  1679. set_threshold(dd, scontext, i);
  1680. }
  1681. if (++scontext >= first_scontext +
  1682. vl_scontexts[i])
  1683. /* wrap back to first send context */
  1684. scontext = first_scontext;
  1685. }
  1686. } else {
  1687. /* just re-use entry without allocating */
  1688. newmap->map[i] = newmap->map[i % num_vls];
  1689. }
  1690. scontext = first_scontext + vl_scontexts[i];
  1691. }
  1692. /* newmap in hand, save old map */
  1693. spin_lock_irq(&dd->pio_map_lock);
  1694. oldmap = rcu_dereference_protected(dd->pio_map,
  1695. lockdep_is_held(&dd->pio_map_lock));
  1696. /* publish newmap */
  1697. rcu_assign_pointer(dd->pio_map, newmap);
  1698. spin_unlock_irq(&dd->pio_map_lock);
  1699. /* success, free any old map after grace period */
  1700. if (oldmap)
  1701. call_rcu(&oldmap->list, pio_map_rcu_callback);
  1702. return 0;
  1703. bail:
  1704. /* free any partial allocation */
  1705. pio_map_free(newmap);
  1706. return -ENOMEM;
  1707. }
  1708. void free_pio_map(struct hfi1_devdata *dd)
  1709. {
  1710. /* Free PIO map if allocated */
  1711. if (rcu_access_pointer(dd->pio_map)) {
  1712. spin_lock_irq(&dd->pio_map_lock);
  1713. pio_map_free(rcu_access_pointer(dd->pio_map));
  1714. RCU_INIT_POINTER(dd->pio_map, NULL);
  1715. spin_unlock_irq(&dd->pio_map_lock);
  1716. synchronize_rcu();
  1717. }
  1718. kfree(dd->kernel_send_context);
  1719. dd->kernel_send_context = NULL;
  1720. }
  1721. int init_pervl_scs(struct hfi1_devdata *dd)
  1722. {
  1723. int i;
  1724. u64 mask, all_vl_mask = (u64)0x80ff; /* VLs 0-7, 15 */
  1725. u64 data_vls_mask = (u64)0x00ff; /* VLs 0-7 */
  1726. u32 ctxt;
  1727. struct hfi1_pportdata *ppd = dd->pport;
  1728. dd->vld[15].sc = sc_alloc(dd, SC_VL15,
  1729. dd->rcd[0]->rcvhdrqentsize, dd->node);
  1730. if (!dd->vld[15].sc)
  1731. return -ENOMEM;
  1732. hfi1_init_ctxt(dd->vld[15].sc);
  1733. dd->vld[15].mtu = enum_to_mtu(OPA_MTU_2048);
  1734. dd->kernel_send_context = kzalloc_node(dd->num_send_contexts *
  1735. sizeof(struct send_context *),
  1736. GFP_KERNEL, dd->node);
  1737. if (!dd->kernel_send_context)
  1738. goto freesc15;
  1739. dd->kernel_send_context[0] = dd->vld[15].sc;
  1740. for (i = 0; i < num_vls; i++) {
  1741. /*
  1742. * Since this function does not deal with a specific
  1743. * receive context but we need the RcvHdrQ entry size,
  1744. * use the size from rcd[0]. It is guaranteed to be
  1745. * valid at this point and will remain the same for all
  1746. * receive contexts.
  1747. */
  1748. dd->vld[i].sc = sc_alloc(dd, SC_KERNEL,
  1749. dd->rcd[0]->rcvhdrqentsize, dd->node);
  1750. if (!dd->vld[i].sc)
  1751. goto nomem;
  1752. dd->kernel_send_context[i + 1] = dd->vld[i].sc;
  1753. hfi1_init_ctxt(dd->vld[i].sc);
  1754. /* non VL15 start with the max MTU */
  1755. dd->vld[i].mtu = hfi1_max_mtu;
  1756. }
  1757. for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++) {
  1758. dd->kernel_send_context[i + 1] =
  1759. sc_alloc(dd, SC_KERNEL, dd->rcd[0]->rcvhdrqentsize, dd->node);
  1760. if (!dd->kernel_send_context[i + 1])
  1761. goto nomem;
  1762. hfi1_init_ctxt(dd->kernel_send_context[i + 1]);
  1763. }
  1764. sc_enable(dd->vld[15].sc);
  1765. ctxt = dd->vld[15].sc->hw_context;
  1766. mask = all_vl_mask & ~(1LL << 15);
  1767. write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
  1768. dd_dev_info(dd,
  1769. "Using send context %u(%u) for VL15\n",
  1770. dd->vld[15].sc->sw_index, ctxt);
  1771. for (i = 0; i < num_vls; i++) {
  1772. sc_enable(dd->vld[i].sc);
  1773. ctxt = dd->vld[i].sc->hw_context;
  1774. mask = all_vl_mask & ~(data_vls_mask);
  1775. write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
  1776. }
  1777. for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++) {
  1778. sc_enable(dd->kernel_send_context[i + 1]);
  1779. ctxt = dd->kernel_send_context[i + 1]->hw_context;
  1780. mask = all_vl_mask & ~(data_vls_mask);
  1781. write_kctxt_csr(dd, ctxt, SC(CHECK_VL), mask);
  1782. }
  1783. if (pio_map_init(dd, ppd->port - 1, num_vls, NULL))
  1784. goto nomem;
  1785. return 0;
  1786. nomem:
  1787. for (i = 0; i < num_vls; i++) {
  1788. sc_free(dd->vld[i].sc);
  1789. dd->vld[i].sc = NULL;
  1790. }
  1791. for (i = num_vls; i < INIT_SC_PER_VL * num_vls; i++)
  1792. sc_free(dd->kernel_send_context[i + 1]);
  1793. kfree(dd->kernel_send_context);
  1794. dd->kernel_send_context = NULL;
  1795. freesc15:
  1796. sc_free(dd->vld[15].sc);
  1797. return -ENOMEM;
  1798. }
  1799. int init_credit_return(struct hfi1_devdata *dd)
  1800. {
  1801. int ret;
  1802. int num_numa;
  1803. int i;
  1804. num_numa = num_online_nodes();
  1805. /* enforce the expectation that the numas are compact */
  1806. for (i = 0; i < num_numa; i++) {
  1807. if (!node_online(i)) {
  1808. dd_dev_err(dd, "NUMA nodes are not compact\n");
  1809. ret = -EINVAL;
  1810. goto done;
  1811. }
  1812. }
  1813. dd->cr_base = kcalloc(
  1814. num_numa,
  1815. sizeof(struct credit_return_base),
  1816. GFP_KERNEL);
  1817. if (!dd->cr_base) {
  1818. dd_dev_err(dd, "Unable to allocate credit return base\n");
  1819. ret = -ENOMEM;
  1820. goto done;
  1821. }
  1822. for (i = 0; i < num_numa; i++) {
  1823. int bytes = TXE_NUM_CONTEXTS * sizeof(struct credit_return);
  1824. set_dev_node(&dd->pcidev->dev, i);
  1825. dd->cr_base[i].va = dma_zalloc_coherent(
  1826. &dd->pcidev->dev,
  1827. bytes,
  1828. &dd->cr_base[i].dma,
  1829. GFP_KERNEL);
  1830. if (!dd->cr_base[i].va) {
  1831. set_dev_node(&dd->pcidev->dev, dd->node);
  1832. dd_dev_err(dd,
  1833. "Unable to allocate credit return DMA range for NUMA %d\n",
  1834. i);
  1835. ret = -ENOMEM;
  1836. goto done;
  1837. }
  1838. }
  1839. set_dev_node(&dd->pcidev->dev, dd->node);
  1840. ret = 0;
  1841. done:
  1842. return ret;
  1843. }
  1844. void free_credit_return(struct hfi1_devdata *dd)
  1845. {
  1846. int num_numa;
  1847. int i;
  1848. if (!dd->cr_base)
  1849. return;
  1850. num_numa = num_online_nodes();
  1851. for (i = 0; i < num_numa; i++) {
  1852. if (dd->cr_base[i].va) {
  1853. dma_free_coherent(&dd->pcidev->dev,
  1854. TXE_NUM_CONTEXTS *
  1855. sizeof(struct credit_return),
  1856. dd->cr_base[i].va,
  1857. dd->cr_base[i].dma);
  1858. }
  1859. }
  1860. kfree(dd->cr_base);
  1861. dd->cr_base = NULL;
  1862. }