pio.c 56 KB

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