sdma.h 33 KB

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