sdma.h 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. #ifndef _HFI1_SDMA_H
  2. #define _HFI1_SDMA_H
  3. /*
  4. * Copyright(c) 2015, 2016 Intel Corporation.
  5. *
  6. * This file is provided under a dual BSD/GPLv2 license. When using or
  7. * redistributing this file, you may do so under either license.
  8. *
  9. * GPL LICENSE SUMMARY
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * BSD LICENSE
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. *
  26. * - Redistributions of source code must retain the above copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * - Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in
  30. * the documentation and/or other materials provided with the
  31. * distribution.
  32. * - Neither the name of Intel Corporation nor the names of its
  33. * contributors may be used to endorse or promote products derived
  34. * from this software without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  39. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  40. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  43. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  44. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  46. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. */
  49. #include <linux/types.h>
  50. #include <linux/list.h>
  51. #include <asm/byteorder.h>
  52. #include <linux/workqueue.h>
  53. #include <linux/rculist.h>
  54. #include "hfi.h"
  55. #include "verbs.h"
  56. #include "sdma_txreq.h"
  57. /* Hardware limit */
  58. #define MAX_DESC 64
  59. /* Hardware limit for SDMA packet size */
  60. #define MAX_SDMA_PKT_SIZE ((16 * 1024) - 1)
  61. #define SDMA_TXREQ_S_OK 0
  62. #define SDMA_TXREQ_S_SENDERROR 1
  63. #define SDMA_TXREQ_S_ABORTED 2
  64. #define SDMA_TXREQ_S_SHUTDOWN 3
  65. /* flags bits */
  66. #define SDMA_TXREQ_F_URGENT 0x0001
  67. #define SDMA_TXREQ_F_AHG_COPY 0x0002
  68. #define SDMA_TXREQ_F_USE_AHG 0x0004
  69. #define SDMA_MAP_NONE 0
  70. #define SDMA_MAP_SINGLE 1
  71. #define SDMA_MAP_PAGE 2
  72. #define SDMA_AHG_VALUE_MASK 0xffff
  73. #define SDMA_AHG_VALUE_SHIFT 0
  74. #define SDMA_AHG_INDEX_MASK 0xf
  75. #define SDMA_AHG_INDEX_SHIFT 16
  76. #define SDMA_AHG_FIELD_LEN_MASK 0xf
  77. #define SDMA_AHG_FIELD_LEN_SHIFT 20
  78. #define SDMA_AHG_FIELD_START_MASK 0x1f
  79. #define SDMA_AHG_FIELD_START_SHIFT 24
  80. #define SDMA_AHG_UPDATE_ENABLE_MASK 0x1
  81. #define SDMA_AHG_UPDATE_ENABLE_SHIFT 31
  82. /* AHG modes */
  83. /*
  84. * Be aware the ordering and values
  85. * for SDMA_AHG_APPLY_UPDATE[123]
  86. * are assumed in generating a skip
  87. * count in submit_tx() in sdma.c
  88. */
  89. #define SDMA_AHG_NO_AHG 0
  90. #define SDMA_AHG_COPY 1
  91. #define SDMA_AHG_APPLY_UPDATE1 2
  92. #define SDMA_AHG_APPLY_UPDATE2 3
  93. #define SDMA_AHG_APPLY_UPDATE3 4
  94. /*
  95. * Bits defined in the send DMA descriptor.
  96. */
  97. #define SDMA_DESC0_FIRST_DESC_FLAG BIT_ULL(63)
  98. #define SDMA_DESC0_LAST_DESC_FLAG BIT_ULL(62)
  99. #define SDMA_DESC0_BYTE_COUNT_SHIFT 48
  100. #define SDMA_DESC0_BYTE_COUNT_WIDTH 14
  101. #define SDMA_DESC0_BYTE_COUNT_MASK \
  102. ((1ULL << SDMA_DESC0_BYTE_COUNT_WIDTH) - 1)
  103. #define SDMA_DESC0_BYTE_COUNT_SMASK \
  104. (SDMA_DESC0_BYTE_COUNT_MASK << SDMA_DESC0_BYTE_COUNT_SHIFT)
  105. #define SDMA_DESC0_PHY_ADDR_SHIFT 0
  106. #define SDMA_DESC0_PHY_ADDR_WIDTH 48
  107. #define SDMA_DESC0_PHY_ADDR_MASK \
  108. ((1ULL << SDMA_DESC0_PHY_ADDR_WIDTH) - 1)
  109. #define SDMA_DESC0_PHY_ADDR_SMASK \
  110. (SDMA_DESC0_PHY_ADDR_MASK << SDMA_DESC0_PHY_ADDR_SHIFT)
  111. #define SDMA_DESC1_HEADER_UPDATE1_SHIFT 32
  112. #define SDMA_DESC1_HEADER_UPDATE1_WIDTH 32
  113. #define SDMA_DESC1_HEADER_UPDATE1_MASK \
  114. ((1ULL << SDMA_DESC1_HEADER_UPDATE1_WIDTH) - 1)
  115. #define SDMA_DESC1_HEADER_UPDATE1_SMASK \
  116. (SDMA_DESC1_HEADER_UPDATE1_MASK << SDMA_DESC1_HEADER_UPDATE1_SHIFT)
  117. #define SDMA_DESC1_HEADER_MODE_SHIFT 13
  118. #define SDMA_DESC1_HEADER_MODE_WIDTH 3
  119. #define SDMA_DESC1_HEADER_MODE_MASK \
  120. ((1ULL << SDMA_DESC1_HEADER_MODE_WIDTH) - 1)
  121. #define SDMA_DESC1_HEADER_MODE_SMASK \
  122. (SDMA_DESC1_HEADER_MODE_MASK << SDMA_DESC1_HEADER_MODE_SHIFT)
  123. #define SDMA_DESC1_HEADER_INDEX_SHIFT 8
  124. #define SDMA_DESC1_HEADER_INDEX_WIDTH 5
  125. #define SDMA_DESC1_HEADER_INDEX_MASK \
  126. ((1ULL << SDMA_DESC1_HEADER_INDEX_WIDTH) - 1)
  127. #define SDMA_DESC1_HEADER_INDEX_SMASK \
  128. (SDMA_DESC1_HEADER_INDEX_MASK << SDMA_DESC1_HEADER_INDEX_SHIFT)
  129. #define SDMA_DESC1_HEADER_DWS_SHIFT 4
  130. #define SDMA_DESC1_HEADER_DWS_WIDTH 4
  131. #define SDMA_DESC1_HEADER_DWS_MASK \
  132. ((1ULL << SDMA_DESC1_HEADER_DWS_WIDTH) - 1)
  133. #define SDMA_DESC1_HEADER_DWS_SMASK \
  134. (SDMA_DESC1_HEADER_DWS_MASK << SDMA_DESC1_HEADER_DWS_SHIFT)
  135. #define SDMA_DESC1_GENERATION_SHIFT 2
  136. #define SDMA_DESC1_GENERATION_WIDTH 2
  137. #define SDMA_DESC1_GENERATION_MASK \
  138. ((1ULL << SDMA_DESC1_GENERATION_WIDTH) - 1)
  139. #define SDMA_DESC1_GENERATION_SMASK \
  140. (SDMA_DESC1_GENERATION_MASK << SDMA_DESC1_GENERATION_SHIFT)
  141. #define SDMA_DESC1_INT_REQ_FLAG BIT_ULL(1)
  142. #define SDMA_DESC1_HEAD_TO_HOST_FLAG BIT_ULL(0)
  143. enum sdma_states {
  144. sdma_state_s00_hw_down,
  145. sdma_state_s10_hw_start_up_halt_wait,
  146. sdma_state_s15_hw_start_up_clean_wait,
  147. sdma_state_s20_idle,
  148. sdma_state_s30_sw_clean_up_wait,
  149. sdma_state_s40_hw_clean_up_wait,
  150. sdma_state_s50_hw_halt_wait,
  151. sdma_state_s60_idle_halt_wait,
  152. sdma_state_s80_hw_freeze,
  153. sdma_state_s82_freeze_sw_clean,
  154. sdma_state_s99_running,
  155. };
  156. enum sdma_events {
  157. sdma_event_e00_go_hw_down,
  158. sdma_event_e10_go_hw_start,
  159. sdma_event_e15_hw_halt_done,
  160. sdma_event_e25_hw_clean_up_done,
  161. sdma_event_e30_go_running,
  162. sdma_event_e40_sw_cleaned,
  163. sdma_event_e50_hw_cleaned,
  164. sdma_event_e60_hw_halted,
  165. sdma_event_e70_go_idle,
  166. sdma_event_e80_hw_freeze,
  167. sdma_event_e81_hw_frozen,
  168. sdma_event_e82_hw_unfreeze,
  169. sdma_event_e85_link_down,
  170. sdma_event_e90_sw_halted,
  171. };
  172. struct sdma_set_state_action {
  173. unsigned op_enable:1;
  174. unsigned op_intenable:1;
  175. unsigned op_halt:1;
  176. unsigned op_cleanup:1;
  177. unsigned go_s99_running_tofalse:1;
  178. unsigned go_s99_running_totrue:1;
  179. };
  180. struct sdma_state {
  181. struct kref kref;
  182. struct completion comp;
  183. enum sdma_states current_state;
  184. unsigned current_op;
  185. unsigned go_s99_running;
  186. /* debugging/development */
  187. enum sdma_states previous_state;
  188. unsigned previous_op;
  189. enum sdma_events last_event;
  190. };
  191. /**
  192. * DOC: sdma exported routines
  193. *
  194. * These sdma routines fit into three categories:
  195. * - The SDMA API for building and submitting packets
  196. * to the ring
  197. *
  198. * - Initialization and tear down routines to buildup
  199. * and tear down SDMA
  200. *
  201. * - ISR entrances to handle interrupts, state changes
  202. * and errors
  203. */
  204. /**
  205. * DOC: sdma PSM/verbs API
  206. *
  207. * The sdma API is designed to be used by both PSM
  208. * and verbs to supply packets to the SDMA ring.
  209. *
  210. * The usage of the API is as follows:
  211. *
  212. * Embed a struct iowait in the QP or
  213. * PQ. The iowait should be initialized with a
  214. * call to iowait_init().
  215. *
  216. * The user of the API should create an allocation method
  217. * for their version of the txreq. slabs, pre-allocated lists,
  218. * and dma pools can be used. Once the user's overload of
  219. * the sdma_txreq has been allocated, the sdma_txreq member
  220. * must be initialized with sdma_txinit() or sdma_txinit_ahg().
  221. *
  222. * The txreq must be declared with the sdma_txreq first.
  223. *
  224. * The tx request, once initialized, is manipulated with calls to
  225. * sdma_txadd_daddr(), sdma_txadd_page(), or sdma_txadd_kvaddr()
  226. * for each disjoint memory location. It is the user's responsibility
  227. * to understand the packet boundaries and page boundaries to do the
  228. * appropriate number of sdma_txadd_* calls.. The user
  229. * must be prepared to deal with failures from these routines due to
  230. * either memory allocation or dma_mapping failures.
  231. *
  232. * The mapping specifics for each memory location are recorded
  233. * in the tx. Memory locations added with sdma_txadd_page()
  234. * and sdma_txadd_kvaddr() are automatically mapped when added
  235. * to the tx and nmapped as part of the progress processing in the
  236. * SDMA interrupt handling.
  237. *
  238. * sdma_txadd_daddr() is used to add an dma_addr_t memory to the
  239. * tx. An example of a use case would be a pre-allocated
  240. * set of headers allocated via dma_pool_alloc() or
  241. * dma_alloc_coherent(). For these memory locations, it
  242. * is the responsibility of the user to handle that unmapping.
  243. * (This would usually be at an unload or job termination.)
  244. *
  245. * The routine sdma_send_txreq() is used to submit
  246. * a tx to the ring after the appropriate number of
  247. * sdma_txadd_* have been done.
  248. *
  249. * If it is desired to send a burst of sdma_txreqs, sdma_send_txlist()
  250. * can be used to submit a list of packets.
  251. *
  252. * The user is free to use the link overhead in the struct sdma_txreq as
  253. * long as the tx isn't in flight.
  254. *
  255. * The extreme degenerate case of the number of descriptors
  256. * exceeding the ring size is automatically handled as
  257. * memory locations are added. An overflow of the descriptor
  258. * array that is part of the sdma_txreq is also automatically
  259. * handled.
  260. *
  261. */
  262. /**
  263. * DOC: Infrastructure calls
  264. *
  265. * sdma_init() is used to initialize data structures and
  266. * CSRs for the desired number of SDMA engines.
  267. *
  268. * sdma_start() is used to kick the SDMA engines initialized
  269. * with sdma_init(). Interrupts must be enabled at this
  270. * point since aspects of the state machine are interrupt
  271. * driven.
  272. *
  273. * sdma_engine_error() and sdma_engine_interrupt() are
  274. * entrances for interrupts.
  275. *
  276. * sdma_map_init() is for the management of the mapping
  277. * table when the number of vls is changed.
  278. *
  279. */
  280. /*
  281. * struct hw_sdma_desc - raw 128 bit SDMA descriptor
  282. *
  283. * This is the raw descriptor in the SDMA ring
  284. */
  285. struct hw_sdma_desc {
  286. /* private: don't use directly */
  287. __le64 qw[2];
  288. };
  289. /**
  290. * struct sdma_engine - Data pertaining to each SDMA engine.
  291. * @dd: a back-pointer to the device data
  292. * @ppd: per port back-pointer
  293. * @imask: mask for irq manipulation
  294. * @idle_mask: mask for determining if an interrupt is due to sdma_idle
  295. *
  296. * This structure has the state for each sdma_engine.
  297. *
  298. * Accessing to non public fields are not supported
  299. * since the private members are subject to change.
  300. */
  301. struct sdma_engine {
  302. /* read mostly */
  303. struct hfi1_devdata *dd;
  304. struct hfi1_pportdata *ppd;
  305. /* private: */
  306. void __iomem *tail_csr;
  307. u64 imask; /* clear interrupt mask */
  308. u64 idle_mask;
  309. u64 progress_mask;
  310. u64 int_mask;
  311. /* private: */
  312. volatile __le64 *head_dma; /* DMA'ed by chip */
  313. /* private: */
  314. dma_addr_t head_phys;
  315. /* private: */
  316. struct hw_sdma_desc *descq;
  317. /* private: */
  318. unsigned descq_full_count;
  319. struct sdma_txreq **tx_ring;
  320. /* private: */
  321. dma_addr_t descq_phys;
  322. /* private */
  323. u32 sdma_mask;
  324. /* private */
  325. struct sdma_state state;
  326. /* private */
  327. int cpu;
  328. /* private: */
  329. u8 sdma_shift;
  330. /* private: */
  331. u8 this_idx; /* zero relative engine */
  332. /* protect changes to senddmactrl shadow */
  333. spinlock_t senddmactrl_lock;
  334. /* private: */
  335. u64 p_senddmactrl; /* shadow per-engine SendDmaCtrl */
  336. /* read/write using tail_lock */
  337. spinlock_t tail_lock ____cacheline_aligned_in_smp;
  338. #ifdef CONFIG_HFI1_DEBUG_SDMA_ORDER
  339. /* private: */
  340. u64 tail_sn;
  341. #endif
  342. /* private: */
  343. u32 descq_tail;
  344. /* private: */
  345. unsigned long ahg_bits;
  346. /* private: */
  347. u16 desc_avail;
  348. /* private: */
  349. u16 tx_tail;
  350. /* private: */
  351. u16 descq_cnt;
  352. /* read/write using head_lock */
  353. /* private: */
  354. seqlock_t head_lock ____cacheline_aligned_in_smp;
  355. #ifdef CONFIG_HFI1_DEBUG_SDMA_ORDER
  356. /* private: */
  357. u64 head_sn;
  358. #endif
  359. /* private: */
  360. u32 descq_head;
  361. /* private: */
  362. u16 tx_head;
  363. /* private: */
  364. u64 last_status;
  365. /* private */
  366. u64 err_cnt;
  367. /* private */
  368. u64 sdma_int_cnt;
  369. u64 idle_int_cnt;
  370. u64 progress_int_cnt;
  371. /* private: */
  372. struct list_head dmawait;
  373. /* CONFIG SDMA for now, just blindly duplicate */
  374. /* private: */
  375. struct tasklet_struct sdma_hw_clean_up_task
  376. ____cacheline_aligned_in_smp;
  377. /* private: */
  378. struct tasklet_struct sdma_sw_clean_up_task
  379. ____cacheline_aligned_in_smp;
  380. /* private: */
  381. struct work_struct err_halt_worker;
  382. /* private */
  383. struct timer_list err_progress_check_timer;
  384. u32 progress_check_head;
  385. /* private: */
  386. struct work_struct flush_worker;
  387. /* protect flush list */
  388. spinlock_t flushlist_lock;
  389. /* private: */
  390. struct list_head flushlist;
  391. struct cpumask cpu_mask;
  392. struct kobject kobj;
  393. };
  394. int sdma_init(struct hfi1_devdata *dd, u8 port);
  395. void sdma_start(struct hfi1_devdata *dd);
  396. void sdma_exit(struct hfi1_devdata *dd);
  397. void sdma_all_running(struct hfi1_devdata *dd);
  398. void sdma_all_idle(struct hfi1_devdata *dd);
  399. void sdma_freeze_notify(struct hfi1_devdata *dd, int go_idle);
  400. void sdma_freeze(struct hfi1_devdata *dd);
  401. void sdma_unfreeze(struct hfi1_devdata *dd);
  402. void sdma_wait(struct hfi1_devdata *dd);
  403. /**
  404. * sdma_empty() - idle engine test
  405. * @engine: sdma engine
  406. *
  407. * Currently used by verbs as a latency optimization.
  408. *
  409. * Return:
  410. * 1 - empty, 0 - non-empty
  411. */
  412. static inline int sdma_empty(struct sdma_engine *sde)
  413. {
  414. return sde->descq_tail == sde->descq_head;
  415. }
  416. static inline u16 sdma_descq_freecnt(struct sdma_engine *sde)
  417. {
  418. return sde->descq_cnt -
  419. (sde->descq_tail -
  420. ACCESS_ONCE(sde->descq_head)) - 1;
  421. }
  422. static inline u16 sdma_descq_inprocess(struct sdma_engine *sde)
  423. {
  424. return sde->descq_cnt - sdma_descq_freecnt(sde);
  425. }
  426. /*
  427. * Either head_lock or tail lock required to see
  428. * a steady state.
  429. */
  430. static inline int __sdma_running(struct sdma_engine *engine)
  431. {
  432. return engine->state.current_state == sdma_state_s99_running;
  433. }
  434. /**
  435. * sdma_running() - state suitability test
  436. * @engine: sdma engine
  437. *
  438. * sdma_running probes the internal state to determine if it is suitable
  439. * for submitting packets.
  440. *
  441. * Return:
  442. * 1 - ok to submit, 0 - not ok to submit
  443. *
  444. */
  445. static inline int sdma_running(struct sdma_engine *engine)
  446. {
  447. unsigned long flags;
  448. int ret;
  449. spin_lock_irqsave(&engine->tail_lock, flags);
  450. ret = __sdma_running(engine);
  451. spin_unlock_irqrestore(&engine->tail_lock, flags);
  452. return ret;
  453. }
  454. void _sdma_txreq_ahgadd(
  455. struct sdma_txreq *tx,
  456. u8 num_ahg,
  457. u8 ahg_entry,
  458. u32 *ahg,
  459. u8 ahg_hlen);
  460. /**
  461. * sdma_txinit_ahg() - initialize an sdma_txreq struct with AHG
  462. * @tx: tx request to initialize
  463. * @flags: flags to key last descriptor additions
  464. * @tlen: total packet length (pbc + headers + data)
  465. * @ahg_entry: ahg entry to use (0 - 31)
  466. * @num_ahg: ahg descriptor for first descriptor (0 - 9)
  467. * @ahg: array of AHG descriptors (up to 9 entries)
  468. * @ahg_hlen: number of bytes from ASIC entry to use
  469. * @cb: callback
  470. *
  471. * The allocation of the sdma_txreq and it enclosing structure is user
  472. * dependent. This routine must be called to initialize the user independent
  473. * fields.
  474. *
  475. * The currently supported flags are SDMA_TXREQ_F_URGENT,
  476. * SDMA_TXREQ_F_AHG_COPY, and SDMA_TXREQ_F_USE_AHG.
  477. *
  478. * SDMA_TXREQ_F_URGENT is used for latency sensitive situations where the
  479. * completion is desired as soon as possible.
  480. *
  481. * SDMA_TXREQ_F_AHG_COPY causes the header in the first descriptor to be
  482. * copied to chip entry. SDMA_TXREQ_F_USE_AHG causes the code to add in
  483. * the AHG descriptors into the first 1 to 3 descriptors.
  484. *
  485. * Completions of submitted requests can be gotten on selected
  486. * txreqs by giving a completion routine callback to sdma_txinit() or
  487. * sdma_txinit_ahg(). The environment in which the callback runs
  488. * can be from an ISR, a tasklet, or a thread, so no sleeping
  489. * kernel routines can be used. Aspects of the sdma ring may
  490. * be locked so care should be taken with locking.
  491. *
  492. * The callback pointer can be NULL to avoid any callback for the packet
  493. * being submitted. The callback will be provided this tx, a status, and a flag.
  494. *
  495. * The status will be one of SDMA_TXREQ_S_OK, SDMA_TXREQ_S_SENDERROR,
  496. * SDMA_TXREQ_S_ABORTED, or SDMA_TXREQ_S_SHUTDOWN.
  497. *
  498. * The flag, if the is the iowait had been used, indicates the iowait
  499. * sdma_busy count has reached zero.
  500. *
  501. * user data portion of tlen should be precise. The sdma_txadd_* entrances
  502. * will pad with a descriptor references 1 - 3 bytes when the number of bytes
  503. * specified in tlen have been supplied to the sdma_txreq.
  504. *
  505. * ahg_hlen is used to determine the number of on-chip entry bytes to
  506. * use as the header. This is for cases where the stored header is
  507. * larger than the header to be used in a packet. This is typical
  508. * for verbs where an RDMA_WRITE_FIRST is larger than the packet in
  509. * and RDMA_WRITE_MIDDLE.
  510. *
  511. */
  512. static inline int sdma_txinit_ahg(
  513. struct sdma_txreq *tx,
  514. u16 flags,
  515. u16 tlen,
  516. u8 ahg_entry,
  517. u8 num_ahg,
  518. u32 *ahg,
  519. u8 ahg_hlen,
  520. void (*cb)(struct sdma_txreq *, int))
  521. {
  522. if (tlen == 0)
  523. return -ENODATA;
  524. if (tlen > MAX_SDMA_PKT_SIZE)
  525. return -EMSGSIZE;
  526. tx->desc_limit = ARRAY_SIZE(tx->descs);
  527. tx->descp = &tx->descs[0];
  528. INIT_LIST_HEAD(&tx->list);
  529. tx->num_desc = 0;
  530. tx->flags = flags;
  531. tx->complete = cb;
  532. tx->coalesce_buf = NULL;
  533. tx->wait = NULL;
  534. tx->packet_len = tlen;
  535. tx->tlen = tx->packet_len;
  536. tx->descs[0].qw[0] = SDMA_DESC0_FIRST_DESC_FLAG;
  537. tx->descs[0].qw[1] = 0;
  538. if (flags & SDMA_TXREQ_F_AHG_COPY)
  539. tx->descs[0].qw[1] |=
  540. (((u64)ahg_entry & SDMA_DESC1_HEADER_INDEX_MASK)
  541. << SDMA_DESC1_HEADER_INDEX_SHIFT) |
  542. (((u64)SDMA_AHG_COPY & SDMA_DESC1_HEADER_MODE_MASK)
  543. << SDMA_DESC1_HEADER_MODE_SHIFT);
  544. else if (flags & SDMA_TXREQ_F_USE_AHG && num_ahg)
  545. _sdma_txreq_ahgadd(tx, num_ahg, ahg_entry, ahg, ahg_hlen);
  546. return 0;
  547. }
  548. /**
  549. * sdma_txinit() - initialize an sdma_txreq struct (no AHG)
  550. * @tx: tx request to initialize
  551. * @flags: flags to key last descriptor additions
  552. * @tlen: total packet length (pbc + headers + data)
  553. * @cb: callback pointer
  554. *
  555. * The allocation of the sdma_txreq and it enclosing structure is user
  556. * dependent. This routine must be called to initialize the user
  557. * independent fields.
  558. *
  559. * The currently supported flags is SDMA_TXREQ_F_URGENT.
  560. *
  561. * SDMA_TXREQ_F_URGENT is used for latency sensitive situations where the
  562. * completion is desired as soon as possible.
  563. *
  564. * Completions of submitted requests can be gotten on selected
  565. * txreqs by giving a completion routine callback to sdma_txinit() or
  566. * sdma_txinit_ahg(). The environment in which the callback runs
  567. * can be from an ISR, a tasklet, or a thread, so no sleeping
  568. * kernel routines can be used. The head size of the sdma ring may
  569. * be locked so care should be taken with locking.
  570. *
  571. * The callback pointer can be NULL to avoid any callback for the packet
  572. * being submitted.
  573. *
  574. * The callback, if non-NULL, will be provided this tx and a status. The
  575. * status will be one of SDMA_TXREQ_S_OK, SDMA_TXREQ_S_SENDERROR,
  576. * SDMA_TXREQ_S_ABORTED, or SDMA_TXREQ_S_SHUTDOWN.
  577. *
  578. */
  579. static inline int sdma_txinit(
  580. struct sdma_txreq *tx,
  581. u16 flags,
  582. u16 tlen,
  583. void (*cb)(struct sdma_txreq *, int))
  584. {
  585. return sdma_txinit_ahg(tx, flags, tlen, 0, 0, NULL, 0, cb);
  586. }
  587. /* helpers - don't use */
  588. static inline int sdma_mapping_type(struct sdma_desc *d)
  589. {
  590. return (d->qw[1] & SDMA_DESC1_GENERATION_SMASK)
  591. >> SDMA_DESC1_GENERATION_SHIFT;
  592. }
  593. static inline size_t sdma_mapping_len(struct sdma_desc *d)
  594. {
  595. return (d->qw[0] & SDMA_DESC0_BYTE_COUNT_SMASK)
  596. >> SDMA_DESC0_BYTE_COUNT_SHIFT;
  597. }
  598. static inline dma_addr_t sdma_mapping_addr(struct sdma_desc *d)
  599. {
  600. return (d->qw[0] & SDMA_DESC0_PHY_ADDR_SMASK)
  601. >> SDMA_DESC0_PHY_ADDR_SHIFT;
  602. }
  603. static inline void make_tx_sdma_desc(
  604. struct sdma_txreq *tx,
  605. int type,
  606. dma_addr_t addr,
  607. size_t len)
  608. {
  609. struct sdma_desc *desc = &tx->descp[tx->num_desc];
  610. if (!tx->num_desc) {
  611. /* qw[0] zero; qw[1] first, ahg mode already in from init */
  612. desc->qw[1] |= ((u64)type & SDMA_DESC1_GENERATION_MASK)
  613. << SDMA_DESC1_GENERATION_SHIFT;
  614. } else {
  615. desc->qw[0] = 0;
  616. desc->qw[1] = ((u64)type & SDMA_DESC1_GENERATION_MASK)
  617. << SDMA_DESC1_GENERATION_SHIFT;
  618. }
  619. desc->qw[0] |= (((u64)addr & SDMA_DESC0_PHY_ADDR_MASK)
  620. << SDMA_DESC0_PHY_ADDR_SHIFT) |
  621. (((u64)len & SDMA_DESC0_BYTE_COUNT_MASK)
  622. << SDMA_DESC0_BYTE_COUNT_SHIFT);
  623. }
  624. /* helper to extend txreq */
  625. int ext_coal_sdma_tx_descs(struct hfi1_devdata *dd, struct sdma_txreq *tx,
  626. int type, void *kvaddr, struct page *page,
  627. unsigned long offset, u16 len);
  628. int _pad_sdma_tx_descs(struct hfi1_devdata *, struct sdma_txreq *);
  629. void __sdma_txclean(struct hfi1_devdata *, struct sdma_txreq *);
  630. static inline void sdma_txclean(struct hfi1_devdata *dd, struct sdma_txreq *tx)
  631. {
  632. if (tx->num_desc)
  633. __sdma_txclean(dd, tx);
  634. }
  635. /* helpers used by public routines */
  636. static inline void _sdma_close_tx(struct hfi1_devdata *dd,
  637. struct sdma_txreq *tx)
  638. {
  639. tx->descp[tx->num_desc].qw[0] |=
  640. SDMA_DESC0_LAST_DESC_FLAG;
  641. tx->descp[tx->num_desc].qw[1] |=
  642. dd->default_desc1;
  643. if (tx->flags & SDMA_TXREQ_F_URGENT)
  644. tx->descp[tx->num_desc].qw[1] |=
  645. (SDMA_DESC1_HEAD_TO_HOST_FLAG |
  646. SDMA_DESC1_INT_REQ_FLAG);
  647. }
  648. static inline int _sdma_txadd_daddr(
  649. struct hfi1_devdata *dd,
  650. int type,
  651. struct sdma_txreq *tx,
  652. dma_addr_t addr,
  653. u16 len)
  654. {
  655. int rval = 0;
  656. make_tx_sdma_desc(
  657. tx,
  658. type,
  659. addr, len);
  660. WARN_ON(len > tx->tlen);
  661. tx->tlen -= len;
  662. /* special cases for last */
  663. if (!tx->tlen) {
  664. if (tx->packet_len & (sizeof(u32) - 1)) {
  665. rval = _pad_sdma_tx_descs(dd, tx);
  666. if (rval)
  667. return rval;
  668. } else {
  669. _sdma_close_tx(dd, tx);
  670. }
  671. }
  672. tx->num_desc++;
  673. return rval;
  674. }
  675. /**
  676. * sdma_txadd_page() - add a page to the sdma_txreq
  677. * @dd: the device to use for mapping
  678. * @tx: tx request to which the page is added
  679. * @page: page to map
  680. * @offset: offset within the page
  681. * @len: length in bytes
  682. *
  683. * This is used to add a page/offset/length descriptor.
  684. *
  685. * The mapping/unmapping of the page/offset/len is automatically handled.
  686. *
  687. * Return:
  688. * 0 - success, -ENOSPC - mapping fail, -ENOMEM - couldn't
  689. * extend/coalesce descriptor array
  690. */
  691. static inline int sdma_txadd_page(
  692. struct hfi1_devdata *dd,
  693. struct sdma_txreq *tx,
  694. struct page *page,
  695. unsigned long offset,
  696. u16 len)
  697. {
  698. dma_addr_t addr;
  699. int rval;
  700. if ((unlikely(tx->num_desc == tx->desc_limit))) {
  701. rval = ext_coal_sdma_tx_descs(dd, tx, SDMA_MAP_PAGE,
  702. NULL, page, offset, len);
  703. if (rval <= 0)
  704. return rval;
  705. }
  706. addr = dma_map_page(
  707. &dd->pcidev->dev,
  708. page,
  709. offset,
  710. len,
  711. DMA_TO_DEVICE);
  712. if (unlikely(dma_mapping_error(&dd->pcidev->dev, addr))) {
  713. __sdma_txclean(dd, tx);
  714. return -ENOSPC;
  715. }
  716. return _sdma_txadd_daddr(
  717. dd, SDMA_MAP_PAGE, tx, addr, len);
  718. }
  719. /**
  720. * sdma_txadd_daddr() - add a dma address to the sdma_txreq
  721. * @dd: the device to use for mapping
  722. * @tx: sdma_txreq to which the page is added
  723. * @addr: dma address mapped by caller
  724. * @len: length in bytes
  725. *
  726. * This is used to add a descriptor for memory that is already dma mapped.
  727. *
  728. * In this case, there is no unmapping as part of the progress processing for
  729. * this memory location.
  730. *
  731. * Return:
  732. * 0 - success, -ENOMEM - couldn't extend descriptor array
  733. */
  734. static inline int sdma_txadd_daddr(
  735. struct hfi1_devdata *dd,
  736. struct sdma_txreq *tx,
  737. dma_addr_t addr,
  738. u16 len)
  739. {
  740. int rval;
  741. if ((unlikely(tx->num_desc == tx->desc_limit))) {
  742. rval = ext_coal_sdma_tx_descs(dd, tx, SDMA_MAP_NONE,
  743. NULL, NULL, 0, 0);
  744. if (rval <= 0)
  745. return rval;
  746. }
  747. return _sdma_txadd_daddr(dd, SDMA_MAP_NONE, tx, addr, len);
  748. }
  749. /**
  750. * sdma_txadd_kvaddr() - add a kernel virtual address to sdma_txreq
  751. * @dd: the device to use for mapping
  752. * @tx: sdma_txreq to which the page is added
  753. * @kvaddr: the kernel virtual address
  754. * @len: length in bytes
  755. *
  756. * This is used to add a descriptor referenced by the indicated kvaddr and
  757. * len.
  758. *
  759. * The mapping/unmapping of the kvaddr and len is automatically handled.
  760. *
  761. * Return:
  762. * 0 - success, -ENOSPC - mapping fail, -ENOMEM - couldn't extend/coalesce
  763. * descriptor array
  764. */
  765. static inline int sdma_txadd_kvaddr(
  766. struct hfi1_devdata *dd,
  767. struct sdma_txreq *tx,
  768. void *kvaddr,
  769. u16 len)
  770. {
  771. dma_addr_t addr;
  772. int rval;
  773. if ((unlikely(tx->num_desc == tx->desc_limit))) {
  774. rval = ext_coal_sdma_tx_descs(dd, tx, SDMA_MAP_SINGLE,
  775. kvaddr, NULL, 0, len);
  776. if (rval <= 0)
  777. return rval;
  778. }
  779. addr = dma_map_single(
  780. &dd->pcidev->dev,
  781. kvaddr,
  782. len,
  783. DMA_TO_DEVICE);
  784. if (unlikely(dma_mapping_error(&dd->pcidev->dev, addr))) {
  785. __sdma_txclean(dd, tx);
  786. return -ENOSPC;
  787. }
  788. return _sdma_txadd_daddr(
  789. dd, SDMA_MAP_SINGLE, tx, addr, len);
  790. }
  791. struct iowait;
  792. int sdma_send_txreq(struct sdma_engine *sde,
  793. struct iowait *wait,
  794. struct sdma_txreq *tx);
  795. int sdma_send_txlist(struct sdma_engine *sde,
  796. struct iowait *wait,
  797. struct list_head *tx_list,
  798. u32 *count);
  799. int sdma_ahg_alloc(struct sdma_engine *sde);
  800. void sdma_ahg_free(struct sdma_engine *sde, int ahg_index);
  801. /**
  802. * sdma_build_ahg - build ahg descriptor
  803. * @data
  804. * @dwindex
  805. * @startbit
  806. * @bits
  807. *
  808. * Build and return a 32 bit descriptor.
  809. */
  810. static inline u32 sdma_build_ahg_descriptor(
  811. u16 data,
  812. u8 dwindex,
  813. u8 startbit,
  814. u8 bits)
  815. {
  816. return (u32)(1UL << SDMA_AHG_UPDATE_ENABLE_SHIFT |
  817. ((startbit & SDMA_AHG_FIELD_START_MASK) <<
  818. SDMA_AHG_FIELD_START_SHIFT) |
  819. ((bits & SDMA_AHG_FIELD_LEN_MASK) <<
  820. SDMA_AHG_FIELD_LEN_SHIFT) |
  821. ((dwindex & SDMA_AHG_INDEX_MASK) <<
  822. SDMA_AHG_INDEX_SHIFT) |
  823. ((data & SDMA_AHG_VALUE_MASK) <<
  824. SDMA_AHG_VALUE_SHIFT));
  825. }
  826. /**
  827. * sdma_progress - use seq number of detect head progress
  828. * @sde: sdma_engine to check
  829. * @seq: base seq count
  830. * @tx: txreq for which we need to check descriptor availability
  831. *
  832. * This is used in the appropriate spot in the sleep routine
  833. * to check for potential ring progress. This routine gets the
  834. * seqcount before queuing the iowait structure for progress.
  835. *
  836. * If the seqcount indicates that progress needs to be checked,
  837. * re-submission is detected by checking whether the descriptor
  838. * queue has enough descriptor for the txreq.
  839. */
  840. static inline unsigned sdma_progress(struct sdma_engine *sde, unsigned seq,
  841. struct sdma_txreq *tx)
  842. {
  843. if (read_seqretry(&sde->head_lock, seq)) {
  844. sde->desc_avail = sdma_descq_freecnt(sde);
  845. if (tx->num_desc > sde->desc_avail)
  846. return 0;
  847. return 1;
  848. }
  849. return 0;
  850. }
  851. /**
  852. * sdma_iowait_schedule() - initialize wait structure
  853. * @sde: sdma_engine to schedule
  854. * @wait: wait struct to schedule
  855. *
  856. * This function initializes the iowait
  857. * structure embedded in the QP or PQ.
  858. *
  859. */
  860. static inline void sdma_iowait_schedule(
  861. struct sdma_engine *sde,
  862. struct iowait *wait)
  863. {
  864. struct hfi1_pportdata *ppd = sde->dd->pport;
  865. iowait_schedule(wait, ppd->hfi1_wq, sde->cpu);
  866. }
  867. /* for use by interrupt handling */
  868. void sdma_engine_error(struct sdma_engine *sde, u64 status);
  869. void sdma_engine_interrupt(struct sdma_engine *sde, u64 status);
  870. /*
  871. *
  872. * The diagram below details the relationship of the mapping structures
  873. *
  874. * Since the mapping now allows for non-uniform engines per vl, the
  875. * number of engines for a vl is either the vl_engines[vl] or
  876. * a computation based on num_sdma/num_vls:
  877. *
  878. * For example:
  879. * nactual = vl_engines ? vl_engines[vl] : num_sdma/num_vls
  880. *
  881. * n = roundup to next highest power of 2 using nactual
  882. *
  883. * In the case where there are num_sdma/num_vls doesn't divide
  884. * evenly, the extras are added from the last vl downward.
  885. *
  886. * For the case where n > nactual, the engines are assigned
  887. * in a round robin fashion wrapping back to the first engine
  888. * for a particular vl.
  889. *
  890. * dd->sdma_map
  891. * | sdma_map_elem[0]
  892. * | +--------------------+
  893. * v | mask |
  894. * sdma_vl_map |--------------------|
  895. * +--------------------------+ | sde[0] -> eng 1 |
  896. * | list (RCU) | |--------------------|
  897. * |--------------------------| ->| sde[1] -> eng 2 |
  898. * | mask | --/ |--------------------|
  899. * |--------------------------| -/ | * |
  900. * | actual_vls (max 8) | -/ |--------------------|
  901. * |--------------------------| --/ | sde[n] -> eng n |
  902. * | vls (max 8) | -/ +--------------------+
  903. * |--------------------------| --/
  904. * | map[0] |-/
  905. * |--------------------------| +--------------------+
  906. * | map[1] |--- | mask |
  907. * |--------------------------| \---- |--------------------|
  908. * | * | \-- | sde[0] -> eng 1+n |
  909. * | * | \---- |--------------------|
  910. * | * | \->| sde[1] -> eng 2+n |
  911. * |--------------------------| |--------------------|
  912. * | map[vls - 1] |- | * |
  913. * +--------------------------+ \- |--------------------|
  914. * \- | sde[m] -> eng m+n |
  915. * \ +--------------------+
  916. * \-
  917. * \
  918. * \- +--------------------+
  919. * \- | mask |
  920. * \ |--------------------|
  921. * \- | sde[0] -> eng 1+m+n|
  922. * \- |--------------------|
  923. * >| sde[1] -> eng 2+m+n|
  924. * |--------------------|
  925. * | * |
  926. * |--------------------|
  927. * | sde[o] -> eng o+m+n|
  928. * +--------------------+
  929. *
  930. */
  931. /**
  932. * struct sdma_map_elem - mapping for a vl
  933. * @mask - selector mask
  934. * @sde - array of engines for this vl
  935. *
  936. * The mask is used to "mod" the selector
  937. * to produce index into the trailing
  938. * array of sdes.
  939. */
  940. struct sdma_map_elem {
  941. u32 mask;
  942. struct sdma_engine *sde[0];
  943. };
  944. /**
  945. * struct sdma_map_el - mapping for a vl
  946. * @engine_to_vl - map of an engine to a vl
  947. * @list - rcu head for free callback
  948. * @mask - vl mask to "mod" the vl to produce an index to map array
  949. * @actual_vls - number of vls
  950. * @vls - number of vls rounded to next power of 2
  951. * @map - array of sdma_map_elem entries
  952. *
  953. * This is the parent mapping structure. The trailing
  954. * members of the struct point to sdma_map_elem entries, which
  955. * in turn point to an array of sde's for that vl.
  956. */
  957. struct sdma_vl_map {
  958. s8 engine_to_vl[TXE_NUM_SDMA_ENGINES];
  959. struct rcu_head list;
  960. u32 mask;
  961. u8 actual_vls;
  962. u8 vls;
  963. struct sdma_map_elem *map[0];
  964. };
  965. int sdma_map_init(
  966. struct hfi1_devdata *dd,
  967. u8 port,
  968. u8 num_vls,
  969. u8 *vl_engines);
  970. /* slow path */
  971. void _sdma_engine_progress_schedule(struct sdma_engine *sde);
  972. /**
  973. * sdma_engine_progress_schedule() - schedule progress on engine
  974. * @sde: sdma_engine to schedule progress
  975. *
  976. * This is the fast path.
  977. *
  978. */
  979. static inline void sdma_engine_progress_schedule(
  980. struct sdma_engine *sde)
  981. {
  982. if (!sde || sdma_descq_inprocess(sde) < (sde->descq_cnt / 8))
  983. return;
  984. _sdma_engine_progress_schedule(sde);
  985. }
  986. struct sdma_engine *sdma_select_engine_sc(
  987. struct hfi1_devdata *dd,
  988. u32 selector,
  989. u8 sc5);
  990. struct sdma_engine *sdma_select_engine_vl(
  991. struct hfi1_devdata *dd,
  992. u32 selector,
  993. u8 vl);
  994. struct sdma_engine *sdma_select_user_engine(struct hfi1_devdata *dd,
  995. u32 selector, u8 vl);
  996. ssize_t sdma_get_cpu_to_sde_map(struct sdma_engine *sde, char *buf);
  997. ssize_t sdma_set_cpu_to_sde_map(struct sdma_engine *sde, const char *buf,
  998. size_t count);
  999. int sdma_engine_get_vl(struct sdma_engine *sde);
  1000. void sdma_seqfile_dump_sde(struct seq_file *s, struct sdma_engine *);
  1001. void sdma_seqfile_dump_cpu_list(struct seq_file *s, struct hfi1_devdata *dd,
  1002. unsigned long cpuid);
  1003. #ifdef CONFIG_SDMA_VERBOSITY
  1004. void sdma_dumpstate(struct sdma_engine *);
  1005. #endif
  1006. static inline char *slashstrip(char *s)
  1007. {
  1008. char *r = s;
  1009. while (*s)
  1010. if (*s++ == '/')
  1011. r = s;
  1012. return r;
  1013. }
  1014. u16 sdma_get_descq_cnt(void);
  1015. extern uint mod_num_sdma;
  1016. void sdma_update_lmc(struct hfi1_devdata *dd, u64 mask, u32 lid);
  1017. #endif