rt2x00queue.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00
  19. Abstract: rt2x00 queue datastructures and routines
  20. */
  21. #ifndef RT2X00QUEUE_H
  22. #define RT2X00QUEUE_H
  23. #include <linux/prefetch.h>
  24. /**
  25. * DOC: Entrie frame size
  26. *
  27. * Ralink PCI devices demand the Frame size to be a multiple of 128 bytes,
  28. * for USB devices this restriction does not apply, but the value of
  29. * 2432 makes sense since it is big enough to contain the maximum fragment
  30. * size according to the ieee802.11 specs.
  31. */
  32. #define DATA_FRAME_SIZE 2432
  33. #define MGMT_FRAME_SIZE 256
  34. /**
  35. * DOC: Number of entries per queue
  36. *
  37. * Under normal load without fragmentation 12 entries are sufficient
  38. * without the queue being filled up to the maximum. When using fragmentation
  39. * and the queue threshold code we need to add some additional margins to
  40. * make sure the queue will never (or only under extreme load) fill up
  41. * completely.
  42. * Since we don't use preallocated DMA having a large number of queue entries
  43. * will have only minimal impact on the memory requirements for the queue.
  44. */
  45. #define RX_ENTRIES 24
  46. #define TX_ENTRIES 24
  47. #define BEACON_ENTRIES 1
  48. #define ATIM_ENTRIES 8
  49. /**
  50. * enum data_queue_qid: Queue identification
  51. *
  52. * @QID_AC_BE: AC BE queue
  53. * @QID_AC_BK: AC BK queue
  54. * @QID_AC_VI: AC VI queue
  55. * @QID_AC_VO: AC VO queue
  56. * @QID_HCCA: HCCA queue
  57. * @QID_MGMT: MGMT queue (prio queue)
  58. * @QID_RX: RX queue
  59. * @QID_OTHER: None of the above (don't use, only present for completeness)
  60. * @QID_BEACON: Beacon queue (value unspecified, don't send it to device)
  61. * @QID_ATIM: Atim queue (value unspeficied, don't send it to device)
  62. */
  63. enum data_queue_qid {
  64. QID_AC_BE = 0,
  65. QID_AC_BK = 1,
  66. QID_AC_VI = 2,
  67. QID_AC_VO = 3,
  68. QID_HCCA = 4,
  69. QID_MGMT = 13,
  70. QID_RX = 14,
  71. QID_OTHER = 15,
  72. QID_BEACON,
  73. QID_ATIM,
  74. };
  75. /**
  76. * enum skb_frame_desc_flags: Flags for &struct skb_frame_desc
  77. *
  78. * @SKBDESC_DMA_MAPPED_RX: &skb_dma field has been mapped for RX
  79. * @SKBDESC_DMA_MAPPED_TX: &skb_dma field has been mapped for TX
  80. */
  81. enum skb_frame_desc_flags {
  82. SKBDESC_DMA_MAPPED_RX = (1 << 0),
  83. SKBDESC_DMA_MAPPED_TX = (1 << 1),
  84. };
  85. /**
  86. * struct skb_frame_desc: Descriptor information for the skb buffer
  87. *
  88. * This structure is placed over the driver_data array, this means that
  89. * this structure should not exceed the size of that array (40 bytes).
  90. *
  91. * @flags: Frame flags, see &enum skb_frame_desc_flags.
  92. * @desc_len: Length of the frame descriptor.
  93. * @desc: Pointer to descriptor part of the frame.
  94. * Note that this pointer could point to something outside
  95. * of the scope of the skb->data pointer.
  96. * @skb_dma: (PCI-only) the DMA address associated with the sk buffer.
  97. * @entry: The entry to which this sk buffer belongs.
  98. */
  99. struct skb_frame_desc {
  100. unsigned int flags;
  101. unsigned int desc_len;
  102. void *desc;
  103. dma_addr_t skb_dma;
  104. struct queue_entry *entry;
  105. };
  106. /**
  107. * get_skb_frame_desc - Obtain the rt2x00 frame descriptor from a sk_buff.
  108. * @skb: &struct sk_buff from where we obtain the &struct skb_frame_desc
  109. */
  110. static inline struct skb_frame_desc* get_skb_frame_desc(struct sk_buff *skb)
  111. {
  112. BUILD_BUG_ON(sizeof(struct skb_frame_desc) >
  113. IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
  114. return (struct skb_frame_desc *)&IEEE80211_SKB_CB(skb)->driver_data;
  115. }
  116. /**
  117. * enum rxdone_entry_desc_flags: Flags for &struct rxdone_entry_desc
  118. *
  119. * @RXDONE_SIGNAL_PLCP: Does the signal field contain the plcp value,
  120. * or does it contain the bitrate itself.
  121. * @RXDONE_MY_BSS: Does this frame originate from device's BSS.
  122. */
  123. enum rxdone_entry_desc_flags {
  124. RXDONE_SIGNAL_PLCP = 1 << 0,
  125. RXDONE_MY_BSS = 1 << 1,
  126. };
  127. /**
  128. * struct rxdone_entry_desc: RX Entry descriptor
  129. *
  130. * Summary of information that has been read from the RX frame descriptor.
  131. *
  132. * @signal: Signal of the received frame.
  133. * @rssi: RSSI of the received frame.
  134. * @size: Data size of the received frame.
  135. * @flags: MAC80211 receive flags (See &enum mac80211_rx_flags).
  136. * @dev_flags: Ralink receive flags (See &enum rxdone_entry_desc_flags).
  137. */
  138. struct rxdone_entry_desc {
  139. int signal;
  140. int rssi;
  141. int size;
  142. int flags;
  143. int dev_flags;
  144. };
  145. /**
  146. * enum txdone_entry_desc_flags: Flags for &struct txdone_entry_desc
  147. *
  148. * @TXDONE_UNKNOWN: Hardware could not determine success of transmission.
  149. * @TXDONE_SUCCESS: Frame was successfully send
  150. * @TXDONE_FAILURE: Frame was not successfully send
  151. * @TXDONE_EXCESSIVE_RETRY: In addition to &TXDONE_FAILURE, the
  152. * frame transmission failed due to excessive retries.
  153. */
  154. enum txdone_entry_desc_flags {
  155. TXDONE_UNKNOWN = 1 << 0,
  156. TXDONE_SUCCESS = 1 << 1,
  157. TXDONE_FAILURE = 1 << 2,
  158. TXDONE_EXCESSIVE_RETRY = 1 << 3,
  159. };
  160. /**
  161. * struct txdone_entry_desc: TX done entry descriptor
  162. *
  163. * Summary of information that has been read from the TX frame descriptor
  164. * after the device is done with transmission.
  165. *
  166. * @flags: TX done flags (See &enum txdone_entry_desc_flags).
  167. * @retry: Retry count.
  168. */
  169. struct txdone_entry_desc {
  170. unsigned long flags;
  171. int retry;
  172. };
  173. /**
  174. * enum txentry_desc_flags: Status flags for TX entry descriptor
  175. *
  176. * @ENTRY_TXD_RTS_FRAME: This frame is a RTS frame.
  177. * @ENTRY_TXD_CTS_FRAME: This frame is a CTS-to-self frame.
  178. * @ENTRY_TXD_OFDM_RATE: This frame is send out with an OFDM rate.
  179. * @ENTRY_TXD_FIRST_FRAGMENT: This is the first frame.
  180. * @ENTRY_TXD_MORE_FRAG: This frame is followed by another fragment.
  181. * @ENTRY_TXD_REQ_TIMESTAMP: Require timestamp to be inserted.
  182. * @ENTRY_TXD_BURST: This frame belongs to the same burst event.
  183. * @ENTRY_TXD_ACK: An ACK is required for this frame.
  184. * @ENTRY_TXD_RETRY_MODE: When set, the long retry count is used.
  185. */
  186. enum txentry_desc_flags {
  187. ENTRY_TXD_RTS_FRAME,
  188. ENTRY_TXD_CTS_FRAME,
  189. ENTRY_TXD_OFDM_RATE,
  190. ENTRY_TXD_FIRST_FRAGMENT,
  191. ENTRY_TXD_MORE_FRAG,
  192. ENTRY_TXD_REQ_TIMESTAMP,
  193. ENTRY_TXD_BURST,
  194. ENTRY_TXD_ACK,
  195. ENTRY_TXD_RETRY_MODE,
  196. };
  197. /**
  198. * struct txentry_desc: TX Entry descriptor
  199. *
  200. * Summary of information for the frame descriptor before sending a TX frame.
  201. *
  202. * @flags: Descriptor flags (See &enum queue_entry_flags).
  203. * @queue: Queue identification (See &enum data_queue_qid).
  204. * @length_high: PLCP length high word.
  205. * @length_low: PLCP length low word.
  206. * @signal: PLCP signal.
  207. * @service: PLCP service.
  208. * @retry_limit: Max number of retries.
  209. * @aifs: AIFS value.
  210. * @ifs: IFS value.
  211. * @cw_min: cwmin value.
  212. * @cw_max: cwmax value.
  213. */
  214. struct txentry_desc {
  215. unsigned long flags;
  216. enum data_queue_qid queue;
  217. u16 length_high;
  218. u16 length_low;
  219. u16 signal;
  220. u16 service;
  221. short retry_limit;
  222. short aifs;
  223. short ifs;
  224. short cw_min;
  225. short cw_max;
  226. };
  227. /**
  228. * enum queue_entry_flags: Status flags for queue entry
  229. *
  230. * @ENTRY_BCN_ASSIGNED: This entry has been assigned to an interface.
  231. * As long as this bit is set, this entry may only be touched
  232. * through the interface structure.
  233. * @ENTRY_OWNER_DEVICE_DATA: This entry is owned by the device for data
  234. * transfer (either TX or RX depending on the queue). The entry should
  235. * only be touched after the device has signaled it is done with it.
  236. * @ENTRY_OWNER_DEVICE_CRYPTO: This entry is owned by the device for data
  237. * encryption or decryption. The entry should only be touched after
  238. * the device has signaled it is done with it.
  239. * @ENTRY_DATA_PENDING: This entry contains a valid frame and is waiting
  240. * for the signal to start sending.
  241. */
  242. enum queue_entry_flags {
  243. ENTRY_BCN_ASSIGNED,
  244. ENTRY_OWNER_DEVICE_DATA,
  245. ENTRY_OWNER_DEVICE_CRYPTO,
  246. ENTRY_DATA_PENDING,
  247. };
  248. /**
  249. * struct queue_entry: Entry inside the &struct data_queue
  250. *
  251. * @flags: Entry flags, see &enum queue_entry_flags.
  252. * @queue: The data queue (&struct data_queue) to which this entry belongs.
  253. * @skb: The buffer which is currently being transmitted (for TX queue),
  254. * or used to directly recieve data in (for RX queue).
  255. * @entry_idx: The entry index number.
  256. * @priv_data: Private data belonging to this queue entry. The pointer
  257. * points to data specific to a particular driver and queue type.
  258. */
  259. struct queue_entry {
  260. unsigned long flags;
  261. struct data_queue *queue;
  262. struct sk_buff *skb;
  263. unsigned int entry_idx;
  264. void *priv_data;
  265. };
  266. /**
  267. * enum queue_index: Queue index type
  268. *
  269. * @Q_INDEX: Index pointer to the current entry in the queue, if this entry is
  270. * owned by the hardware then the queue is considered to be full.
  271. * @Q_INDEX_DONE: Index pointer to the next entry which will be completed by
  272. * the hardware and for which we need to run the txdone handler. If this
  273. * entry is not owned by the hardware the queue is considered to be empty.
  274. * @Q_INDEX_CRYPTO: Index pointer to the next entry which encryption/decription
  275. * will be completed by the hardware next.
  276. * @Q_INDEX_MAX: Keep last, used in &struct data_queue to determine the size
  277. * of the index array.
  278. */
  279. enum queue_index {
  280. Q_INDEX,
  281. Q_INDEX_DONE,
  282. Q_INDEX_CRYPTO,
  283. Q_INDEX_MAX,
  284. };
  285. /**
  286. * struct data_queue: Data queue
  287. *
  288. * @rt2x00dev: Pointer to main &struct rt2x00dev where this queue belongs to.
  289. * @entries: Base address of the &struct queue_entry which are
  290. * part of this queue.
  291. * @qid: The queue identification, see &enum data_queue_qid.
  292. * @lock: Spinlock to protect index handling. Whenever @index, @index_done or
  293. * @index_crypt needs to be changed this lock should be grabbed to prevent
  294. * index corruption due to concurrency.
  295. * @count: Number of frames handled in the queue.
  296. * @limit: Maximum number of entries in the queue.
  297. * @threshold: Minimum number of free entries before queue is kicked by force.
  298. * @length: Number of frames in queue.
  299. * @index: Index pointers to entry positions in the queue,
  300. * use &enum queue_index to get a specific index field.
  301. * @aifs: The aifs value for outgoing frames (field ignored in RX queue).
  302. * @cw_min: The cw min value for outgoing frames (field ignored in RX queue).
  303. * @cw_max: The cw max value for outgoing frames (field ignored in RX queue).
  304. * @data_size: Maximum data size for the frames in this queue.
  305. * @desc_size: Hardware descriptor size for the data in this queue.
  306. */
  307. struct data_queue {
  308. struct rt2x00_dev *rt2x00dev;
  309. struct queue_entry *entries;
  310. enum data_queue_qid qid;
  311. spinlock_t lock;
  312. unsigned int count;
  313. unsigned short limit;
  314. unsigned short threshold;
  315. unsigned short length;
  316. unsigned short index[Q_INDEX_MAX];
  317. unsigned short aifs;
  318. unsigned short cw_min;
  319. unsigned short cw_max;
  320. unsigned short data_size;
  321. unsigned short desc_size;
  322. };
  323. /**
  324. * struct data_queue_desc: Data queue description
  325. *
  326. * The information in this structure is used by drivers
  327. * to inform rt2x00lib about the creation of the data queue.
  328. *
  329. * @entry_num: Maximum number of entries for a queue.
  330. * @data_size: Maximum data size for the frames in this queue.
  331. * @desc_size: Hardware descriptor size for the data in this queue.
  332. * @priv_size: Size of per-queue_entry private data.
  333. */
  334. struct data_queue_desc {
  335. unsigned short entry_num;
  336. unsigned short data_size;
  337. unsigned short desc_size;
  338. unsigned short priv_size;
  339. };
  340. /**
  341. * queue_end - Return pointer to the last queue (HELPER MACRO).
  342. * @__dev: Pointer to &struct rt2x00_dev
  343. *
  344. * Using the base rx pointer and the maximum number of available queues,
  345. * this macro will return the address of 1 position beyond the end of the
  346. * queues array.
  347. */
  348. #define queue_end(__dev) \
  349. &(__dev)->rx[(__dev)->data_queues]
  350. /**
  351. * tx_queue_end - Return pointer to the last TX queue (HELPER MACRO).
  352. * @__dev: Pointer to &struct rt2x00_dev
  353. *
  354. * Using the base tx pointer and the maximum number of available TX
  355. * queues, this macro will return the address of 1 position beyond
  356. * the end of the TX queue array.
  357. */
  358. #define tx_queue_end(__dev) \
  359. &(__dev)->tx[(__dev)->ops->tx_queues]
  360. /**
  361. * queue_loop - Loop through the queues within a specific range (HELPER MACRO).
  362. * @__entry: Pointer where the current queue entry will be stored in.
  363. * @__start: Start queue pointer.
  364. * @__end: End queue pointer.
  365. *
  366. * This macro will loop through all queues between &__start and &__end.
  367. */
  368. #define queue_loop(__entry, __start, __end) \
  369. for ((__entry) = (__start); \
  370. prefetch(&(__entry)[1]), (__entry) != (__end); \
  371. (__entry) = &(__entry)[1])
  372. /**
  373. * queue_for_each - Loop through all queues
  374. * @__dev: Pointer to &struct rt2x00_dev
  375. * @__entry: Pointer where the current queue entry will be stored in.
  376. *
  377. * This macro will loop through all available queues.
  378. */
  379. #define queue_for_each(__dev, __entry) \
  380. queue_loop(__entry, (__dev)->rx, queue_end(__dev))
  381. /**
  382. * tx_queue_for_each - Loop through the TX queues
  383. * @__dev: Pointer to &struct rt2x00_dev
  384. * @__entry: Pointer where the current queue entry will be stored in.
  385. *
  386. * This macro will loop through all TX related queues excluding
  387. * the Beacon and Atim queues.
  388. */
  389. #define tx_queue_for_each(__dev, __entry) \
  390. queue_loop(__entry, (__dev)->tx, tx_queue_end(__dev))
  391. /**
  392. * txall_queue_for_each - Loop through all TX related queues
  393. * @__dev: Pointer to &struct rt2x00_dev
  394. * @__entry: Pointer where the current queue entry will be stored in.
  395. *
  396. * This macro will loop through all TX related queues including
  397. * the Beacon and Atim queues.
  398. */
  399. #define txall_queue_for_each(__dev, __entry) \
  400. queue_loop(__entry, (__dev)->tx, queue_end(__dev))
  401. /**
  402. * rt2x00queue_empty - Check if the queue is empty.
  403. * @queue: Queue to check if empty.
  404. */
  405. static inline int rt2x00queue_empty(struct data_queue *queue)
  406. {
  407. return queue->length == 0;
  408. }
  409. /**
  410. * rt2x00queue_full - Check if the queue is full.
  411. * @queue: Queue to check if full.
  412. */
  413. static inline int rt2x00queue_full(struct data_queue *queue)
  414. {
  415. return queue->length == queue->limit;
  416. }
  417. /**
  418. * rt2x00queue_free - Check the number of available entries in queue.
  419. * @queue: Queue to check.
  420. */
  421. static inline int rt2x00queue_available(struct data_queue *queue)
  422. {
  423. return queue->limit - queue->length;
  424. }
  425. /**
  426. * rt2x00queue_threshold - Check if the queue is below threshold
  427. * @queue: Queue to check.
  428. */
  429. static inline int rt2x00queue_threshold(struct data_queue *queue)
  430. {
  431. return rt2x00queue_available(queue) < queue->threshold;
  432. }
  433. /**
  434. * rt2x00_desc_read - Read a word from the hardware descriptor.
  435. * @desc: Base descriptor address
  436. * @word: Word index from where the descriptor should be read.
  437. * @value: Address where the descriptor value should be written into.
  438. */
  439. static inline void rt2x00_desc_read(__le32 *desc, const u8 word, u32 *value)
  440. {
  441. *value = le32_to_cpu(desc[word]);
  442. }
  443. /**
  444. * rt2x00_desc_write - wrote a word to the hardware descriptor.
  445. * @desc: Base descriptor address
  446. * @word: Word index from where the descriptor should be written.
  447. * @value: Value that should be written into the descriptor.
  448. */
  449. static inline void rt2x00_desc_write(__le32 *desc, const u8 word, u32 value)
  450. {
  451. desc[word] = cpu_to_le32(value);
  452. }
  453. #endif /* RT2X00QUEUE_H */