pio.c 57 KB

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