sdma.h 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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_clean(struct hfi1_devdata *dd, size_t num_engines);
  398. void sdma_all_running(struct hfi1_devdata *dd);
  399. void sdma_all_idle(struct hfi1_devdata *dd);
  400. void sdma_freeze_notify(struct hfi1_devdata *dd, int go_idle);
  401. void sdma_freeze(struct hfi1_devdata *dd);
  402. void sdma_unfreeze(struct hfi1_devdata *dd);
  403. void sdma_wait(struct hfi1_devdata *dd);
  404. /**
  405. * sdma_empty() - idle engine test
  406. * @engine: sdma engine
  407. *
  408. * Currently used by verbs as a latency optimization.
  409. *
  410. * Return:
  411. * 1 - empty, 0 - non-empty
  412. */
  413. static inline int sdma_empty(struct sdma_engine *sde)
  414. {
  415. return sde->descq_tail == sde->descq_head;
  416. }
  417. static inline u16 sdma_descq_freecnt(struct sdma_engine *sde)
  418. {
  419. return sde->descq_cnt -
  420. (sde->descq_tail -
  421. READ_ONCE(sde->descq_head)) - 1;
  422. }
  423. static inline u16 sdma_descq_inprocess(struct sdma_engine *sde)
  424. {
  425. return sde->descq_cnt - sdma_descq_freecnt(sde);
  426. }
  427. /*
  428. * Either head_lock or tail lock required to see
  429. * a steady state.
  430. */
  431. static inline int __sdma_running(struct sdma_engine *engine)
  432. {
  433. return engine->state.current_state == sdma_state_s99_running;
  434. }
  435. /**
  436. * sdma_running() - state suitability test
  437. * @engine: sdma engine
  438. *
  439. * sdma_running probes the internal state to determine if it is suitable
  440. * for submitting packets.
  441. *
  442. * Return:
  443. * 1 - ok to submit, 0 - not ok to submit
  444. *
  445. */
  446. static inline int sdma_running(struct sdma_engine *engine)
  447. {
  448. unsigned long flags;
  449. int ret;
  450. spin_lock_irqsave(&engine->tail_lock, flags);
  451. ret = __sdma_running(engine);
  452. spin_unlock_irqrestore(&engine->tail_lock, flags);
  453. return ret;
  454. }
  455. void _sdma_txreq_ahgadd(
  456. struct sdma_txreq *tx,
  457. u8 num_ahg,
  458. u8 ahg_entry,
  459. u32 *ahg,
  460. u8 ahg_hlen);
  461. /**
  462. * sdma_txinit_ahg() - initialize an sdma_txreq struct with AHG
  463. * @tx: tx request to initialize
  464. * @flags: flags to key last descriptor additions
  465. * @tlen: total packet length (pbc + headers + data)
  466. * @ahg_entry: ahg entry to use (0 - 31)
  467. * @num_ahg: ahg descriptor for first descriptor (0 - 9)
  468. * @ahg: array of AHG descriptors (up to 9 entries)
  469. * @ahg_hlen: number of bytes from ASIC entry to use
  470. * @cb: callback
  471. *
  472. * The allocation of the sdma_txreq and it enclosing structure is user
  473. * dependent. This routine must be called to initialize the user independent
  474. * fields.
  475. *
  476. * The currently supported flags are SDMA_TXREQ_F_URGENT,
  477. * SDMA_TXREQ_F_AHG_COPY, and SDMA_TXREQ_F_USE_AHG.
  478. *
  479. * SDMA_TXREQ_F_URGENT is used for latency sensitive situations where the
  480. * completion is desired as soon as possible.
  481. *
  482. * SDMA_TXREQ_F_AHG_COPY causes the header in the first descriptor to be
  483. * copied to chip entry. SDMA_TXREQ_F_USE_AHG causes the code to add in
  484. * the AHG descriptors into the first 1 to 3 descriptors.
  485. *
  486. * Completions of submitted requests can be gotten on selected
  487. * txreqs by giving a completion routine callback to sdma_txinit() or
  488. * sdma_txinit_ahg(). The environment in which the callback runs
  489. * can be from an ISR, a tasklet, or a thread, so no sleeping
  490. * kernel routines can be used. Aspects of the sdma ring may
  491. * be locked so care should be taken with locking.
  492. *
  493. * The callback pointer can be NULL to avoid any callback for the packet
  494. * being submitted. The callback will be provided this tx, a status, and a flag.
  495. *
  496. * The status will be one of SDMA_TXREQ_S_OK, SDMA_TXREQ_S_SENDERROR,
  497. * SDMA_TXREQ_S_ABORTED, or SDMA_TXREQ_S_SHUTDOWN.
  498. *
  499. * The flag, if the is the iowait had been used, indicates the iowait
  500. * sdma_busy count has reached zero.
  501. *
  502. * user data portion of tlen should be precise. The sdma_txadd_* entrances
  503. * will pad with a descriptor references 1 - 3 bytes when the number of bytes
  504. * specified in tlen have been supplied to the sdma_txreq.
  505. *
  506. * ahg_hlen is used to determine the number of on-chip entry bytes to
  507. * use as the header. This is for cases where the stored header is
  508. * larger than the header to be used in a packet. This is typical
  509. * for verbs where an RDMA_WRITE_FIRST is larger than the packet in
  510. * and RDMA_WRITE_MIDDLE.
  511. *
  512. */
  513. static inline int sdma_txinit_ahg(
  514. struct sdma_txreq *tx,
  515. u16 flags,
  516. u16 tlen,
  517. u8 ahg_entry,
  518. u8 num_ahg,
  519. u32 *ahg,
  520. u8 ahg_hlen,
  521. void (*cb)(struct sdma_txreq *, int))
  522. {
  523. if (tlen == 0)
  524. return -ENODATA;
  525. if (tlen > MAX_SDMA_PKT_SIZE)
  526. return -EMSGSIZE;
  527. tx->desc_limit = ARRAY_SIZE(tx->descs);
  528. tx->descp = &tx->descs[0];
  529. INIT_LIST_HEAD(&tx->list);
  530. tx->num_desc = 0;
  531. tx->flags = flags;
  532. tx->complete = cb;
  533. tx->coalesce_buf = NULL;
  534. tx->wait = NULL;
  535. tx->packet_len = tlen;
  536. tx->tlen = tx->packet_len;
  537. tx->descs[0].qw[0] = SDMA_DESC0_FIRST_DESC_FLAG;
  538. tx->descs[0].qw[1] = 0;
  539. if (flags & SDMA_TXREQ_F_AHG_COPY)
  540. tx->descs[0].qw[1] |=
  541. (((u64)ahg_entry & SDMA_DESC1_HEADER_INDEX_MASK)
  542. << SDMA_DESC1_HEADER_INDEX_SHIFT) |
  543. (((u64)SDMA_AHG_COPY & SDMA_DESC1_HEADER_MODE_MASK)
  544. << SDMA_DESC1_HEADER_MODE_SHIFT);
  545. else if (flags & SDMA_TXREQ_F_USE_AHG && num_ahg)
  546. _sdma_txreq_ahgadd(tx, num_ahg, ahg_entry, ahg, ahg_hlen);
  547. return 0;
  548. }
  549. /**
  550. * sdma_txinit() - initialize an sdma_txreq struct (no AHG)
  551. * @tx: tx request to initialize
  552. * @flags: flags to key last descriptor additions
  553. * @tlen: total packet length (pbc + headers + data)
  554. * @cb: callback pointer
  555. *
  556. * The allocation of the sdma_txreq and it enclosing structure is user
  557. * dependent. This routine must be called to initialize the user
  558. * independent fields.
  559. *
  560. * The currently supported flags is SDMA_TXREQ_F_URGENT.
  561. *
  562. * SDMA_TXREQ_F_URGENT is used for latency sensitive situations where the
  563. * completion is desired as soon as possible.
  564. *
  565. * Completions of submitted requests can be gotten on selected
  566. * txreqs by giving a completion routine callback to sdma_txinit() or
  567. * sdma_txinit_ahg(). The environment in which the callback runs
  568. * can be from an ISR, a tasklet, or a thread, so no sleeping
  569. * kernel routines can be used. The head size of the sdma ring may
  570. * be locked so care should be taken with locking.
  571. *
  572. * The callback pointer can be NULL to avoid any callback for the packet
  573. * being submitted.
  574. *
  575. * The callback, if non-NULL, will be provided this tx and a status. The
  576. * status will be one of SDMA_TXREQ_S_OK, SDMA_TXREQ_S_SENDERROR,
  577. * SDMA_TXREQ_S_ABORTED, or SDMA_TXREQ_S_SHUTDOWN.
  578. *
  579. */
  580. static inline int sdma_txinit(
  581. struct sdma_txreq *tx,
  582. u16 flags,
  583. u16 tlen,
  584. void (*cb)(struct sdma_txreq *, int))
  585. {
  586. return sdma_txinit_ahg(tx, flags, tlen, 0, 0, NULL, 0, cb);
  587. }
  588. /* helpers - don't use */
  589. static inline int sdma_mapping_type(struct sdma_desc *d)
  590. {
  591. return (d->qw[1] & SDMA_DESC1_GENERATION_SMASK)
  592. >> SDMA_DESC1_GENERATION_SHIFT;
  593. }
  594. static inline size_t sdma_mapping_len(struct sdma_desc *d)
  595. {
  596. return (d->qw[0] & SDMA_DESC0_BYTE_COUNT_SMASK)
  597. >> SDMA_DESC0_BYTE_COUNT_SHIFT;
  598. }
  599. static inline dma_addr_t sdma_mapping_addr(struct sdma_desc *d)
  600. {
  601. return (d->qw[0] & SDMA_DESC0_PHY_ADDR_SMASK)
  602. >> SDMA_DESC0_PHY_ADDR_SHIFT;
  603. }
  604. static inline void make_tx_sdma_desc(
  605. struct sdma_txreq *tx,
  606. int type,
  607. dma_addr_t addr,
  608. size_t len)
  609. {
  610. struct sdma_desc *desc = &tx->descp[tx->num_desc];
  611. if (!tx->num_desc) {
  612. /* qw[0] zero; qw[1] first, ahg mode already in from init */
  613. desc->qw[1] |= ((u64)type & SDMA_DESC1_GENERATION_MASK)
  614. << SDMA_DESC1_GENERATION_SHIFT;
  615. } else {
  616. desc->qw[0] = 0;
  617. desc->qw[1] = ((u64)type & SDMA_DESC1_GENERATION_MASK)
  618. << SDMA_DESC1_GENERATION_SHIFT;
  619. }
  620. desc->qw[0] |= (((u64)addr & SDMA_DESC0_PHY_ADDR_MASK)
  621. << SDMA_DESC0_PHY_ADDR_SHIFT) |
  622. (((u64)len & SDMA_DESC0_BYTE_COUNT_MASK)
  623. << SDMA_DESC0_BYTE_COUNT_SHIFT);
  624. }
  625. /* helper to extend txreq */
  626. int ext_coal_sdma_tx_descs(struct hfi1_devdata *dd, struct sdma_txreq *tx,
  627. int type, void *kvaddr, struct page *page,
  628. unsigned long offset, u16 len);
  629. int _pad_sdma_tx_descs(struct hfi1_devdata *, struct sdma_txreq *);
  630. void __sdma_txclean(struct hfi1_devdata *, struct sdma_txreq *);
  631. static inline void sdma_txclean(struct hfi1_devdata *dd, struct sdma_txreq *tx)
  632. {
  633. if (tx->num_desc)
  634. __sdma_txclean(dd, tx);
  635. }
  636. /* helpers used by public routines */
  637. static inline void _sdma_close_tx(struct hfi1_devdata *dd,
  638. struct sdma_txreq *tx)
  639. {
  640. tx->descp[tx->num_desc].qw[0] |=
  641. SDMA_DESC0_LAST_DESC_FLAG;
  642. tx->descp[tx->num_desc].qw[1] |=
  643. dd->default_desc1;
  644. if (tx->flags & SDMA_TXREQ_F_URGENT)
  645. tx->descp[tx->num_desc].qw[1] |=
  646. (SDMA_DESC1_HEAD_TO_HOST_FLAG |
  647. SDMA_DESC1_INT_REQ_FLAG);
  648. }
  649. static inline int _sdma_txadd_daddr(
  650. struct hfi1_devdata *dd,
  651. int type,
  652. struct sdma_txreq *tx,
  653. dma_addr_t addr,
  654. u16 len)
  655. {
  656. int rval = 0;
  657. make_tx_sdma_desc(
  658. tx,
  659. type,
  660. addr, len);
  661. WARN_ON(len > tx->tlen);
  662. tx->tlen -= len;
  663. /* special cases for last */
  664. if (!tx->tlen) {
  665. if (tx->packet_len & (sizeof(u32) - 1)) {
  666. rval = _pad_sdma_tx_descs(dd, tx);
  667. if (rval)
  668. return rval;
  669. } else {
  670. _sdma_close_tx(dd, tx);
  671. }
  672. }
  673. tx->num_desc++;
  674. return rval;
  675. }
  676. /**
  677. * sdma_txadd_page() - add a page to the sdma_txreq
  678. * @dd: the device to use for mapping
  679. * @tx: tx request to which the page is added
  680. * @page: page to map
  681. * @offset: offset within the page
  682. * @len: length in bytes
  683. *
  684. * This is used to add a page/offset/length descriptor.
  685. *
  686. * The mapping/unmapping of the page/offset/len is automatically handled.
  687. *
  688. * Return:
  689. * 0 - success, -ENOSPC - mapping fail, -ENOMEM - couldn't
  690. * extend/coalesce descriptor array
  691. */
  692. static inline int sdma_txadd_page(
  693. struct hfi1_devdata *dd,
  694. struct sdma_txreq *tx,
  695. struct page *page,
  696. unsigned long offset,
  697. u16 len)
  698. {
  699. dma_addr_t addr;
  700. int rval;
  701. if ((unlikely(tx->num_desc == tx->desc_limit))) {
  702. rval = ext_coal_sdma_tx_descs(dd, tx, SDMA_MAP_PAGE,
  703. NULL, page, offset, len);
  704. if (rval <= 0)
  705. return rval;
  706. }
  707. addr = dma_map_page(
  708. &dd->pcidev->dev,
  709. page,
  710. offset,
  711. len,
  712. DMA_TO_DEVICE);
  713. if (unlikely(dma_mapping_error(&dd->pcidev->dev, addr))) {
  714. __sdma_txclean(dd, tx);
  715. return -ENOSPC;
  716. }
  717. return _sdma_txadd_daddr(
  718. dd, SDMA_MAP_PAGE, tx, addr, len);
  719. }
  720. /**
  721. * sdma_txadd_daddr() - add a dma address to the sdma_txreq
  722. * @dd: the device to use for mapping
  723. * @tx: sdma_txreq to which the page is added
  724. * @addr: dma address mapped by caller
  725. * @len: length in bytes
  726. *
  727. * This is used to add a descriptor for memory that is already dma mapped.
  728. *
  729. * In this case, there is no unmapping as part of the progress processing for
  730. * this memory location.
  731. *
  732. * Return:
  733. * 0 - success, -ENOMEM - couldn't extend descriptor array
  734. */
  735. static inline int sdma_txadd_daddr(
  736. struct hfi1_devdata *dd,
  737. struct sdma_txreq *tx,
  738. dma_addr_t addr,
  739. u16 len)
  740. {
  741. int rval;
  742. if ((unlikely(tx->num_desc == tx->desc_limit))) {
  743. rval = ext_coal_sdma_tx_descs(dd, tx, SDMA_MAP_NONE,
  744. NULL, NULL, 0, 0);
  745. if (rval <= 0)
  746. return rval;
  747. }
  748. return _sdma_txadd_daddr(dd, SDMA_MAP_NONE, tx, addr, len);
  749. }
  750. /**
  751. * sdma_txadd_kvaddr() - add a kernel virtual address to sdma_txreq
  752. * @dd: the device to use for mapping
  753. * @tx: sdma_txreq to which the page is added
  754. * @kvaddr: the kernel virtual address
  755. * @len: length in bytes
  756. *
  757. * This is used to add a descriptor referenced by the indicated kvaddr and
  758. * len.
  759. *
  760. * The mapping/unmapping of the kvaddr and len is automatically handled.
  761. *
  762. * Return:
  763. * 0 - success, -ENOSPC - mapping fail, -ENOMEM - couldn't extend/coalesce
  764. * descriptor array
  765. */
  766. static inline int sdma_txadd_kvaddr(
  767. struct hfi1_devdata *dd,
  768. struct sdma_txreq *tx,
  769. void *kvaddr,
  770. u16 len)
  771. {
  772. dma_addr_t addr;
  773. int rval;
  774. if ((unlikely(tx->num_desc == tx->desc_limit))) {
  775. rval = ext_coal_sdma_tx_descs(dd, tx, SDMA_MAP_SINGLE,
  776. kvaddr, NULL, 0, len);
  777. if (rval <= 0)
  778. return rval;
  779. }
  780. addr = dma_map_single(
  781. &dd->pcidev->dev,
  782. kvaddr,
  783. len,
  784. DMA_TO_DEVICE);
  785. if (unlikely(dma_mapping_error(&dd->pcidev->dev, addr))) {
  786. __sdma_txclean(dd, tx);
  787. return -ENOSPC;
  788. }
  789. return _sdma_txadd_daddr(
  790. dd, SDMA_MAP_SINGLE, tx, addr, len);
  791. }
  792. struct iowait;
  793. int sdma_send_txreq(struct sdma_engine *sde,
  794. struct iowait *wait,
  795. struct sdma_txreq *tx,
  796. bool pkts_sent);
  797. int sdma_send_txlist(struct sdma_engine *sde,
  798. struct iowait *wait,
  799. struct list_head *tx_list,
  800. u32 *count);
  801. int sdma_ahg_alloc(struct sdma_engine *sde);
  802. void sdma_ahg_free(struct sdma_engine *sde, int ahg_index);
  803. /**
  804. * sdma_build_ahg - build ahg descriptor
  805. * @data
  806. * @dwindex
  807. * @startbit
  808. * @bits
  809. *
  810. * Build and return a 32 bit descriptor.
  811. */
  812. static inline u32 sdma_build_ahg_descriptor(
  813. u16 data,
  814. u8 dwindex,
  815. u8 startbit,
  816. u8 bits)
  817. {
  818. return (u32)(1UL << SDMA_AHG_UPDATE_ENABLE_SHIFT |
  819. ((startbit & SDMA_AHG_FIELD_START_MASK) <<
  820. SDMA_AHG_FIELD_START_SHIFT) |
  821. ((bits & SDMA_AHG_FIELD_LEN_MASK) <<
  822. SDMA_AHG_FIELD_LEN_SHIFT) |
  823. ((dwindex & SDMA_AHG_INDEX_MASK) <<
  824. SDMA_AHG_INDEX_SHIFT) |
  825. ((data & SDMA_AHG_VALUE_MASK) <<
  826. SDMA_AHG_VALUE_SHIFT));
  827. }
  828. /**
  829. * sdma_progress - use seq number of detect head progress
  830. * @sde: sdma_engine to check
  831. * @seq: base seq count
  832. * @tx: txreq for which we need to check descriptor availability
  833. *
  834. * This is used in the appropriate spot in the sleep routine
  835. * to check for potential ring progress. This routine gets the
  836. * seqcount before queuing the iowait structure for progress.
  837. *
  838. * If the seqcount indicates that progress needs to be checked,
  839. * re-submission is detected by checking whether the descriptor
  840. * queue has enough descriptor for the txreq.
  841. */
  842. static inline unsigned sdma_progress(struct sdma_engine *sde, unsigned seq,
  843. struct sdma_txreq *tx)
  844. {
  845. if (read_seqretry(&sde->head_lock, seq)) {
  846. sde->desc_avail = sdma_descq_freecnt(sde);
  847. if (tx->num_desc > sde->desc_avail)
  848. return 0;
  849. return 1;
  850. }
  851. return 0;
  852. }
  853. /**
  854. * sdma_iowait_schedule() - initialize wait structure
  855. * @sde: sdma_engine to schedule
  856. * @wait: wait struct to schedule
  857. *
  858. * This function initializes the iowait
  859. * structure embedded in the QP or PQ.
  860. *
  861. */
  862. static inline void sdma_iowait_schedule(
  863. struct sdma_engine *sde,
  864. struct iowait *wait)
  865. {
  866. struct hfi1_pportdata *ppd = sde->dd->pport;
  867. iowait_schedule(wait, ppd->hfi1_wq, sde->cpu);
  868. }
  869. /* for use by interrupt handling */
  870. void sdma_engine_error(struct sdma_engine *sde, u64 status);
  871. void sdma_engine_interrupt(struct sdma_engine *sde, u64 status);
  872. /*
  873. *
  874. * The diagram below details the relationship of the mapping structures
  875. *
  876. * Since the mapping now allows for non-uniform engines per vl, the
  877. * number of engines for a vl is either the vl_engines[vl] or
  878. * a computation based on num_sdma/num_vls:
  879. *
  880. * For example:
  881. * nactual = vl_engines ? vl_engines[vl] : num_sdma/num_vls
  882. *
  883. * n = roundup to next highest power of 2 using nactual
  884. *
  885. * In the case where there are num_sdma/num_vls doesn't divide
  886. * evenly, the extras are added from the last vl downward.
  887. *
  888. * For the case where n > nactual, the engines are assigned
  889. * in a round robin fashion wrapping back to the first engine
  890. * for a particular vl.
  891. *
  892. * dd->sdma_map
  893. * | sdma_map_elem[0]
  894. * | +--------------------+
  895. * v | mask |
  896. * sdma_vl_map |--------------------|
  897. * +--------------------------+ | sde[0] -> eng 1 |
  898. * | list (RCU) | |--------------------|
  899. * |--------------------------| ->| sde[1] -> eng 2 |
  900. * | mask | --/ |--------------------|
  901. * |--------------------------| -/ | * |
  902. * | actual_vls (max 8) | -/ |--------------------|
  903. * |--------------------------| --/ | sde[n-1] -> eng n |
  904. * | vls (max 8) | -/ +--------------------+
  905. * |--------------------------| --/
  906. * | map[0] |-/
  907. * |--------------------------| +---------------------+
  908. * | map[1] |--- | mask |
  909. * |--------------------------| \---- |---------------------|
  910. * | * | \-- | sde[0] -> eng 1+n |
  911. * | * | \---- |---------------------|
  912. * | * | \->| sde[1] -> eng 2+n |
  913. * |--------------------------| |---------------------|
  914. * | map[vls - 1] |- | * |
  915. * +--------------------------+ \- |---------------------|
  916. * \- | sde[m-1] -> eng m+n |
  917. * \ +---------------------+
  918. * \-
  919. * \
  920. * \- +----------------------+
  921. * \- | mask |
  922. * \ |----------------------|
  923. * \- | sde[0] -> eng 1+m+n |
  924. * \- |----------------------|
  925. * >| sde[1] -> eng 2+m+n |
  926. * |----------------------|
  927. * | * |
  928. * |----------------------|
  929. * | sde[o-1] -> eng o+m+n|
  930. * +----------------------+
  931. *
  932. */
  933. /**
  934. * struct sdma_map_elem - mapping for a vl
  935. * @mask - selector mask
  936. * @sde - array of engines for this vl
  937. *
  938. * The mask is used to "mod" the selector
  939. * to produce index into the trailing
  940. * array of sdes.
  941. */
  942. struct sdma_map_elem {
  943. u32 mask;
  944. struct sdma_engine *sde[0];
  945. };
  946. /**
  947. * struct sdma_map_el - mapping for a vl
  948. * @engine_to_vl - map of an engine to a vl
  949. * @list - rcu head for free callback
  950. * @mask - vl mask to "mod" the vl to produce an index to map array
  951. * @actual_vls - number of vls
  952. * @vls - number of vls rounded to next power of 2
  953. * @map - array of sdma_map_elem entries
  954. *
  955. * This is the parent mapping structure. The trailing
  956. * members of the struct point to sdma_map_elem entries, which
  957. * in turn point to an array of sde's for that vl.
  958. */
  959. struct sdma_vl_map {
  960. s8 engine_to_vl[TXE_NUM_SDMA_ENGINES];
  961. struct rcu_head list;
  962. u32 mask;
  963. u8 actual_vls;
  964. u8 vls;
  965. struct sdma_map_elem *map[0];
  966. };
  967. int sdma_map_init(
  968. struct hfi1_devdata *dd,
  969. u8 port,
  970. u8 num_vls,
  971. u8 *vl_engines);
  972. /* slow path */
  973. void _sdma_engine_progress_schedule(struct sdma_engine *sde);
  974. /**
  975. * sdma_engine_progress_schedule() - schedule progress on engine
  976. * @sde: sdma_engine to schedule progress
  977. *
  978. * This is the fast path.
  979. *
  980. */
  981. static inline void sdma_engine_progress_schedule(
  982. struct sdma_engine *sde)
  983. {
  984. if (!sde || sdma_descq_inprocess(sde) < (sde->descq_cnt / 8))
  985. return;
  986. _sdma_engine_progress_schedule(sde);
  987. }
  988. struct sdma_engine *sdma_select_engine_sc(
  989. struct hfi1_devdata *dd,
  990. u32 selector,
  991. u8 sc5);
  992. struct sdma_engine *sdma_select_engine_vl(
  993. struct hfi1_devdata *dd,
  994. u32 selector,
  995. u8 vl);
  996. struct sdma_engine *sdma_select_user_engine(struct hfi1_devdata *dd,
  997. u32 selector, u8 vl);
  998. ssize_t sdma_get_cpu_to_sde_map(struct sdma_engine *sde, char *buf);
  999. ssize_t sdma_set_cpu_to_sde_map(struct sdma_engine *sde, const char *buf,
  1000. size_t count);
  1001. int sdma_engine_get_vl(struct sdma_engine *sde);
  1002. void sdma_seqfile_dump_sde(struct seq_file *s, struct sdma_engine *);
  1003. void sdma_seqfile_dump_cpu_list(struct seq_file *s, struct hfi1_devdata *dd,
  1004. unsigned long cpuid);
  1005. #ifdef CONFIG_SDMA_VERBOSITY
  1006. void sdma_dumpstate(struct sdma_engine *);
  1007. #endif
  1008. static inline char *slashstrip(char *s)
  1009. {
  1010. char *r = s;
  1011. while (*s)
  1012. if (*s++ == '/')
  1013. r = s;
  1014. return r;
  1015. }
  1016. u16 sdma_get_descq_cnt(void);
  1017. extern uint mod_num_sdma;
  1018. void sdma_update_lmc(struct hfi1_devdata *dd, u64 mask, u32 lid);
  1019. #endif