sdma.h 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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. bool pkts_sent);
  796. int sdma_send_txlist(struct sdma_engine *sde,
  797. struct iowait *wait,
  798. struct list_head *tx_list,
  799. u32 *count);
  800. int sdma_ahg_alloc(struct sdma_engine *sde);
  801. void sdma_ahg_free(struct sdma_engine *sde, int ahg_index);
  802. /**
  803. * sdma_build_ahg - build ahg descriptor
  804. * @data
  805. * @dwindex
  806. * @startbit
  807. * @bits
  808. *
  809. * Build and return a 32 bit descriptor.
  810. */
  811. static inline u32 sdma_build_ahg_descriptor(
  812. u16 data,
  813. u8 dwindex,
  814. u8 startbit,
  815. u8 bits)
  816. {
  817. return (u32)(1UL << SDMA_AHG_UPDATE_ENABLE_SHIFT |
  818. ((startbit & SDMA_AHG_FIELD_START_MASK) <<
  819. SDMA_AHG_FIELD_START_SHIFT) |
  820. ((bits & SDMA_AHG_FIELD_LEN_MASK) <<
  821. SDMA_AHG_FIELD_LEN_SHIFT) |
  822. ((dwindex & SDMA_AHG_INDEX_MASK) <<
  823. SDMA_AHG_INDEX_SHIFT) |
  824. ((data & SDMA_AHG_VALUE_MASK) <<
  825. SDMA_AHG_VALUE_SHIFT));
  826. }
  827. /**
  828. * sdma_progress - use seq number of detect head progress
  829. * @sde: sdma_engine to check
  830. * @seq: base seq count
  831. * @tx: txreq for which we need to check descriptor availability
  832. *
  833. * This is used in the appropriate spot in the sleep routine
  834. * to check for potential ring progress. This routine gets the
  835. * seqcount before queuing the iowait structure for progress.
  836. *
  837. * If the seqcount indicates that progress needs to be checked,
  838. * re-submission is detected by checking whether the descriptor
  839. * queue has enough descriptor for the txreq.
  840. */
  841. static inline unsigned sdma_progress(struct sdma_engine *sde, unsigned seq,
  842. struct sdma_txreq *tx)
  843. {
  844. if (read_seqretry(&sde->head_lock, seq)) {
  845. sde->desc_avail = sdma_descq_freecnt(sde);
  846. if (tx->num_desc > sde->desc_avail)
  847. return 0;
  848. return 1;
  849. }
  850. return 0;
  851. }
  852. /**
  853. * sdma_iowait_schedule() - initialize wait structure
  854. * @sde: sdma_engine to schedule
  855. * @wait: wait struct to schedule
  856. *
  857. * This function initializes the iowait
  858. * structure embedded in the QP or PQ.
  859. *
  860. */
  861. static inline void sdma_iowait_schedule(
  862. struct sdma_engine *sde,
  863. struct iowait *wait)
  864. {
  865. struct hfi1_pportdata *ppd = sde->dd->pport;
  866. iowait_schedule(wait, ppd->hfi1_wq, sde->cpu);
  867. }
  868. /* for use by interrupt handling */
  869. void sdma_engine_error(struct sdma_engine *sde, u64 status);
  870. void sdma_engine_interrupt(struct sdma_engine *sde, u64 status);
  871. /*
  872. *
  873. * The diagram below details the relationship of the mapping structures
  874. *
  875. * Since the mapping now allows for non-uniform engines per vl, the
  876. * number of engines for a vl is either the vl_engines[vl] or
  877. * a computation based on num_sdma/num_vls:
  878. *
  879. * For example:
  880. * nactual = vl_engines ? vl_engines[vl] : num_sdma/num_vls
  881. *
  882. * n = roundup to next highest power of 2 using nactual
  883. *
  884. * In the case where there are num_sdma/num_vls doesn't divide
  885. * evenly, the extras are added from the last vl downward.
  886. *
  887. * For the case where n > nactual, the engines are assigned
  888. * in a round robin fashion wrapping back to the first engine
  889. * for a particular vl.
  890. *
  891. * dd->sdma_map
  892. * | sdma_map_elem[0]
  893. * | +--------------------+
  894. * v | mask |
  895. * sdma_vl_map |--------------------|
  896. * +--------------------------+ | sde[0] -> eng 1 |
  897. * | list (RCU) | |--------------------|
  898. * |--------------------------| ->| sde[1] -> eng 2 |
  899. * | mask | --/ |--------------------|
  900. * |--------------------------| -/ | * |
  901. * | actual_vls (max 8) | -/ |--------------------|
  902. * |--------------------------| --/ | sde[n-1] -> eng n |
  903. * | vls (max 8) | -/ +--------------------+
  904. * |--------------------------| --/
  905. * | map[0] |-/
  906. * |--------------------------| +---------------------+
  907. * | map[1] |--- | mask |
  908. * |--------------------------| \---- |---------------------|
  909. * | * | \-- | sde[0] -> eng 1+n |
  910. * | * | \---- |---------------------|
  911. * | * | \->| sde[1] -> eng 2+n |
  912. * |--------------------------| |---------------------|
  913. * | map[vls - 1] |- | * |
  914. * +--------------------------+ \- |---------------------|
  915. * \- | sde[m-1] -> eng m+n |
  916. * \ +---------------------+
  917. * \-
  918. * \
  919. * \- +----------------------+
  920. * \- | mask |
  921. * \ |----------------------|
  922. * \- | sde[0] -> eng 1+m+n |
  923. * \- |----------------------|
  924. * >| sde[1] -> eng 2+m+n |
  925. * |----------------------|
  926. * | * |
  927. * |----------------------|
  928. * | sde[o-1] -> eng o+m+n|
  929. * +----------------------+
  930. *
  931. */
  932. /**
  933. * struct sdma_map_elem - mapping for a vl
  934. * @mask - selector mask
  935. * @sde - array of engines for this vl
  936. *
  937. * The mask is used to "mod" the selector
  938. * to produce index into the trailing
  939. * array of sdes.
  940. */
  941. struct sdma_map_elem {
  942. u32 mask;
  943. struct sdma_engine *sde[0];
  944. };
  945. /**
  946. * struct sdma_map_el - mapping for a vl
  947. * @engine_to_vl - map of an engine to a vl
  948. * @list - rcu head for free callback
  949. * @mask - vl mask to "mod" the vl to produce an index to map array
  950. * @actual_vls - number of vls
  951. * @vls - number of vls rounded to next power of 2
  952. * @map - array of sdma_map_elem entries
  953. *
  954. * This is the parent mapping structure. The trailing
  955. * members of the struct point to sdma_map_elem entries, which
  956. * in turn point to an array of sde's for that vl.
  957. */
  958. struct sdma_vl_map {
  959. s8 engine_to_vl[TXE_NUM_SDMA_ENGINES];
  960. struct rcu_head list;
  961. u32 mask;
  962. u8 actual_vls;
  963. u8 vls;
  964. struct sdma_map_elem *map[0];
  965. };
  966. int sdma_map_init(
  967. struct hfi1_devdata *dd,
  968. u8 port,
  969. u8 num_vls,
  970. u8 *vl_engines);
  971. /* slow path */
  972. void _sdma_engine_progress_schedule(struct sdma_engine *sde);
  973. /**
  974. * sdma_engine_progress_schedule() - schedule progress on engine
  975. * @sde: sdma_engine to schedule progress
  976. *
  977. * This is the fast path.
  978. *
  979. */
  980. static inline void sdma_engine_progress_schedule(
  981. struct sdma_engine *sde)
  982. {
  983. if (!sde || sdma_descq_inprocess(sde) < (sde->descq_cnt / 8))
  984. return;
  985. _sdma_engine_progress_schedule(sde);
  986. }
  987. struct sdma_engine *sdma_select_engine_sc(
  988. struct hfi1_devdata *dd,
  989. u32 selector,
  990. u8 sc5);
  991. struct sdma_engine *sdma_select_engine_vl(
  992. struct hfi1_devdata *dd,
  993. u32 selector,
  994. u8 vl);
  995. struct sdma_engine *sdma_select_user_engine(struct hfi1_devdata *dd,
  996. u32 selector, u8 vl);
  997. ssize_t sdma_get_cpu_to_sde_map(struct sdma_engine *sde, char *buf);
  998. ssize_t sdma_set_cpu_to_sde_map(struct sdma_engine *sde, const char *buf,
  999. size_t count);
  1000. int sdma_engine_get_vl(struct sdma_engine *sde);
  1001. void sdma_seqfile_dump_sde(struct seq_file *s, struct sdma_engine *);
  1002. void sdma_seqfile_dump_cpu_list(struct seq_file *s, struct hfi1_devdata *dd,
  1003. unsigned long cpuid);
  1004. #ifdef CONFIG_SDMA_VERBOSITY
  1005. void sdma_dumpstate(struct sdma_engine *);
  1006. #endif
  1007. static inline char *slashstrip(char *s)
  1008. {
  1009. char *r = s;
  1010. while (*s)
  1011. if (*s++ == '/')
  1012. r = s;
  1013. return r;
  1014. }
  1015. u16 sdma_get_descq_cnt(void);
  1016. extern uint mod_num_sdma;
  1017. void sdma_update_lmc(struct hfi1_devdata *dd, u64 mask, u32 lid);
  1018. #endif