iwl-trans-pcie-int.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #ifndef __iwl_trans_int_pcie_h__
  30. #define __iwl_trans_int_pcie_h__
  31. #include <linux/spinlock.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/pci.h>
  35. #include "iwl-fh.h"
  36. #include "iwl-csr.h"
  37. #include "iwl-shared.h"
  38. #include "iwl-trans.h"
  39. #include "iwl-debug.h"
  40. #include "iwl-io.h"
  41. #include "iwl-op-mode.h"
  42. struct iwl_tx_queue;
  43. struct iwl_queue;
  44. struct iwl_host_cmd;
  45. /*This file includes the declaration that are internal to the
  46. * trans_pcie layer */
  47. struct iwl_rx_mem_buffer {
  48. dma_addr_t page_dma;
  49. struct page *page;
  50. struct list_head list;
  51. };
  52. /**
  53. * struct isr_statistics - interrupt statistics
  54. *
  55. */
  56. struct isr_statistics {
  57. u32 hw;
  58. u32 sw;
  59. u32 err_code;
  60. u32 sch;
  61. u32 alive;
  62. u32 rfkill;
  63. u32 ctkill;
  64. u32 wakeup;
  65. u32 rx;
  66. u32 tx;
  67. u32 unhandled;
  68. };
  69. /**
  70. * struct iwl_rx_queue - Rx queue
  71. * @bd: driver's pointer to buffer of receive buffer descriptors (rbd)
  72. * @bd_dma: bus address of buffer of receive buffer descriptors (rbd)
  73. * @pool:
  74. * @queue:
  75. * @read: Shared index to newest available Rx buffer
  76. * @write: Shared index to oldest written Rx packet
  77. * @free_count: Number of pre-allocated buffers in rx_free
  78. * @write_actual:
  79. * @rx_free: list of free SKBs for use
  80. * @rx_used: List of Rx buffers with no SKB
  81. * @need_update: flag to indicate we need to update read/write index
  82. * @rb_stts: driver's pointer to receive buffer status
  83. * @rb_stts_dma: bus address of receive buffer status
  84. * @lock:
  85. *
  86. * NOTE: rx_free and rx_used are used as a FIFO for iwl_rx_mem_buffers
  87. */
  88. struct iwl_rx_queue {
  89. __le32 *bd;
  90. dma_addr_t bd_dma;
  91. struct iwl_rx_mem_buffer pool[RX_QUEUE_SIZE + RX_FREE_BUFFERS];
  92. struct iwl_rx_mem_buffer *queue[RX_QUEUE_SIZE];
  93. u32 read;
  94. u32 write;
  95. u32 free_count;
  96. u32 write_actual;
  97. struct list_head rx_free;
  98. struct list_head rx_used;
  99. int need_update;
  100. struct iwl_rb_status *rb_stts;
  101. dma_addr_t rb_stts_dma;
  102. spinlock_t lock;
  103. };
  104. struct iwl_dma_ptr {
  105. dma_addr_t dma;
  106. void *addr;
  107. size_t size;
  108. };
  109. /**
  110. * iwl_queue_inc_wrap - increment queue index, wrap back to beginning
  111. * @index -- current index
  112. * @n_bd -- total number of entries in queue (must be power of 2)
  113. */
  114. static inline int iwl_queue_inc_wrap(int index, int n_bd)
  115. {
  116. return ++index & (n_bd - 1);
  117. }
  118. /**
  119. * iwl_queue_dec_wrap - decrement queue index, wrap back to end
  120. * @index -- current index
  121. * @n_bd -- total number of entries in queue (must be power of 2)
  122. */
  123. static inline int iwl_queue_dec_wrap(int index, int n_bd)
  124. {
  125. return --index & (n_bd - 1);
  126. }
  127. /*
  128. * This queue number is required for proper operation
  129. * because the ucode will stop/start the scheduler as
  130. * required.
  131. */
  132. #define IWL_IPAN_MCAST_QUEUE 8
  133. struct iwl_cmd_meta {
  134. /* only for SYNC commands, iff the reply skb is wanted */
  135. struct iwl_host_cmd *source;
  136. u32 flags;
  137. DEFINE_DMA_UNMAP_ADDR(mapping);
  138. DEFINE_DMA_UNMAP_LEN(len);
  139. };
  140. /*
  141. * Generic queue structure
  142. *
  143. * Contains common data for Rx and Tx queues.
  144. *
  145. * Note the difference between n_bd and n_window: the hardware
  146. * always assumes 256 descriptors, so n_bd is always 256 (unless
  147. * there might be HW changes in the future). For the normal TX
  148. * queues, n_window, which is the size of the software queue data
  149. * is also 256; however, for the command queue, n_window is only
  150. * 32 since we don't need so many commands pending. Since the HW
  151. * still uses 256 BDs for DMA though, n_bd stays 256. As a result,
  152. * the software buffers (in the variables @meta, @txb in struct
  153. * iwl_tx_queue) only have 32 entries, while the HW buffers (@tfds
  154. * in the same struct) have 256.
  155. * This means that we end up with the following:
  156. * HW entries: | 0 | ... | N * 32 | ... | N * 32 + 31 | ... | 255 |
  157. * SW entries: | 0 | ... | 31 |
  158. * where N is a number between 0 and 7. This means that the SW
  159. * data is a window overlayed over the HW queue.
  160. */
  161. struct iwl_queue {
  162. int n_bd; /* number of BDs in this queue */
  163. int write_ptr; /* 1-st empty entry (index) host_w*/
  164. int read_ptr; /* last used entry (index) host_r*/
  165. /* use for monitoring and recovering the stuck queue */
  166. dma_addr_t dma_addr; /* physical addr for BD's */
  167. int n_window; /* safe queue window */
  168. u32 id;
  169. int low_mark; /* low watermark, resume queue if free
  170. * space more than this */
  171. int high_mark; /* high watermark, stop queue if free
  172. * space less than this */
  173. };
  174. /**
  175. * struct iwl_tx_queue - Tx Queue for DMA
  176. * @q: generic Rx/Tx queue descriptor
  177. * @bd: base of circular buffer of TFDs
  178. * @cmd: array of command/TX buffer pointers
  179. * @meta: array of meta data for each command/tx buffer
  180. * @dma_addr_cmd: physical address of cmd/tx buffer array
  181. * @txb: array of per-TFD driver data
  182. * lock: queue lock
  183. * @time_stamp: time (in jiffies) of last read_ptr change
  184. * @need_update: indicates need to update read/write index
  185. * @sched_retry: indicates queue is high-throughput aggregation (HT AGG) enabled
  186. * @sta_id: valid if sched_retry is set
  187. * @tid: valid if sched_retry is set
  188. *
  189. * A Tx queue consists of circular buffer of BDs (a.k.a. TFDs, transmit frame
  190. * descriptors) and required locking structures.
  191. */
  192. #define TFD_TX_CMD_SLOTS 256
  193. #define TFD_CMD_SLOTS 32
  194. struct iwl_tx_queue {
  195. struct iwl_queue q;
  196. struct iwl_tfd *tfds;
  197. struct iwl_device_cmd **cmd;
  198. struct iwl_cmd_meta *meta;
  199. struct sk_buff **skbs;
  200. spinlock_t lock;
  201. unsigned long time_stamp;
  202. u8 need_update;
  203. u8 sched_retry;
  204. u8 active;
  205. u8 swq_id;
  206. u16 sta_id;
  207. u16 tid;
  208. };
  209. /**
  210. * struct iwl_trans_pcie - PCIe transport specific data
  211. * @rxq: all the RX queue data
  212. * @rx_replenish: work that will be called when buffers need to be allocated
  213. * @trans: pointer to the generic transport area
  214. * @irq_requested: true when the irq has been requested
  215. * @scd_base_addr: scheduler sram base address in SRAM
  216. * @scd_bc_tbls: pointer to the byte count table of the scheduler
  217. * @kw: keep warm address
  218. * @ac_to_fifo: to what fifo is a specifc AC mapped ?
  219. * @ac_to_queue: to what tx queue is a specifc AC mapped ?
  220. * @mcast_queue:
  221. * @txq: Tx DMA processing queues
  222. * @txq_ctx_active_msk: what queue is active
  223. * queue_stopped: tracks what queue is stopped
  224. * queue_stop_count: tracks what SW queue is stopped
  225. * @pci_dev: basic pci-network driver stuff
  226. * @hw_base: pci hardware address support
  227. */
  228. struct iwl_trans_pcie {
  229. struct iwl_rx_queue rxq;
  230. struct work_struct rx_replenish;
  231. struct iwl_trans *trans;
  232. /* INT ICT Table */
  233. __le32 *ict_tbl;
  234. dma_addr_t ict_tbl_dma;
  235. int ict_index;
  236. u32 inta;
  237. bool use_ict;
  238. bool irq_requested;
  239. struct tasklet_struct irq_tasklet;
  240. struct isr_statistics isr_stats;
  241. spinlock_t irq_lock;
  242. u32 inta_mask;
  243. u32 scd_base_addr;
  244. struct iwl_dma_ptr scd_bc_tbls;
  245. struct iwl_dma_ptr kw;
  246. const u8 *ac_to_fifo[NUM_IWL_RXON_CTX];
  247. const u8 *ac_to_queue[NUM_IWL_RXON_CTX];
  248. u8 mcast_queue[NUM_IWL_RXON_CTX];
  249. u8 agg_txq[IWLAGN_STATION_COUNT][IWL_MAX_TID_COUNT];
  250. struct iwl_tx_queue *txq;
  251. unsigned long txq_ctx_active_msk;
  252. #define IWL_MAX_HW_QUEUES 32
  253. unsigned long queue_stopped[BITS_TO_LONGS(IWL_MAX_HW_QUEUES)];
  254. atomic_t queue_stop_count[4];
  255. /* PCI bus related data */
  256. struct pci_dev *pci_dev;
  257. void __iomem *hw_base;
  258. };
  259. #define IWL_TRANS_GET_PCIE_TRANS(_iwl_trans) \
  260. ((struct iwl_trans_pcie *) ((_iwl_trans)->trans_specific))
  261. /*****************************************************
  262. * RX
  263. ******************************************************/
  264. void iwl_bg_rx_replenish(struct work_struct *data);
  265. void iwl_irq_tasklet(struct iwl_trans *trans);
  266. void iwlagn_rx_replenish(struct iwl_trans *trans);
  267. void iwl_rx_queue_update_write_ptr(struct iwl_trans *trans,
  268. struct iwl_rx_queue *q);
  269. /*****************************************************
  270. * ICT
  271. ******************************************************/
  272. void iwl_reset_ict(struct iwl_trans *trans);
  273. void iwl_disable_ict(struct iwl_trans *trans);
  274. int iwl_alloc_isr_ict(struct iwl_trans *trans);
  275. void iwl_free_isr_ict(struct iwl_trans *trans);
  276. irqreturn_t iwl_isr_ict(int irq, void *data);
  277. /*****************************************************
  278. * TX / HCMD
  279. ******************************************************/
  280. void iwl_txq_update_write_ptr(struct iwl_trans *trans,
  281. struct iwl_tx_queue *txq);
  282. int iwlagn_txq_attach_buf_to_tfd(struct iwl_trans *trans,
  283. struct iwl_tx_queue *txq,
  284. dma_addr_t addr, u16 len, u8 reset);
  285. int iwl_queue_init(struct iwl_queue *q, int count, int slots_num, u32 id);
  286. int iwl_trans_pcie_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd);
  287. void iwl_tx_cmd_complete(struct iwl_trans *trans,
  288. struct iwl_rx_cmd_buffer *rxb, int handler_status);
  289. void iwl_trans_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
  290. struct iwl_tx_queue *txq,
  291. u16 byte_cnt);
  292. int iwl_trans_pcie_tx_agg_disable(struct iwl_trans *trans,
  293. int sta_id, int tid);
  294. void iwl_trans_set_wr_ptrs(struct iwl_trans *trans, int txq_id, u32 index);
  295. void iwl_trans_tx_queue_set_status(struct iwl_trans *trans,
  296. struct iwl_tx_queue *txq,
  297. int tx_fifo_id, int scd_retry);
  298. int iwl_trans_pcie_tx_agg_alloc(struct iwl_trans *trans, int sta_id, int tid);
  299. void iwl_trans_pcie_tx_agg_setup(struct iwl_trans *trans,
  300. enum iwl_rxon_context_id ctx,
  301. int sta_id, int tid, int frame_limit, u16 ssn);
  302. void iwlagn_txq_free_tfd(struct iwl_trans *trans, struct iwl_tx_queue *txq,
  303. int index, enum dma_data_direction dma_dir);
  304. int iwl_tx_queue_reclaim(struct iwl_trans *trans, int txq_id, int index,
  305. struct sk_buff_head *skbs);
  306. int iwl_queue_space(const struct iwl_queue *q);
  307. /*****************************************************
  308. * Error handling
  309. ******************************************************/
  310. int iwl_dump_nic_event_log(struct iwl_trans *trans, bool full_log,
  311. char **buf, bool display);
  312. int iwl_dump_fh(struct iwl_trans *trans, char **buf, bool display);
  313. void iwl_dump_csr(struct iwl_trans *trans);
  314. /*****************************************************
  315. * Helpers
  316. ******************************************************/
  317. static inline void iwl_disable_interrupts(struct iwl_trans *trans)
  318. {
  319. clear_bit(STATUS_INT_ENABLED, &trans->shrd->status);
  320. /* disable interrupts from uCode/NIC to host */
  321. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  322. /* acknowledge/clear/reset any interrupts still pending
  323. * from uCode or flow handler (Rx/Tx DMA) */
  324. iwl_write32(trans, CSR_INT, 0xffffffff);
  325. iwl_write32(trans, CSR_FH_INT_STATUS, 0xffffffff);
  326. IWL_DEBUG_ISR(trans, "Disabled interrupts\n");
  327. }
  328. static inline void iwl_enable_interrupts(struct iwl_trans *trans)
  329. {
  330. struct iwl_trans_pcie *trans_pcie =
  331. IWL_TRANS_GET_PCIE_TRANS(trans);
  332. IWL_DEBUG_ISR(trans, "Enabling interrupts\n");
  333. set_bit(STATUS_INT_ENABLED, &trans->shrd->status);
  334. iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);
  335. }
  336. /*
  337. * we have 8 bits used like this:
  338. *
  339. * 7 6 5 4 3 2 1 0
  340. * | | | | | | | |
  341. * | | | | | | +-+-------- AC queue (0-3)
  342. * | | | | | |
  343. * | +-+-+-+-+------------ HW queue ID
  344. * |
  345. * +---------------------- unused
  346. */
  347. static inline void iwl_set_swq_id(struct iwl_tx_queue *txq, u8 ac, u8 hwq)
  348. {
  349. BUG_ON(ac > 3); /* only have 2 bits */
  350. BUG_ON(hwq > 31); /* only use 5 bits */
  351. txq->swq_id = (hwq << 2) | ac;
  352. }
  353. static inline u8 iwl_get_queue_ac(struct iwl_tx_queue *txq)
  354. {
  355. return txq->swq_id & 0x3;
  356. }
  357. static inline void iwl_wake_queue(struct iwl_trans *trans,
  358. struct iwl_tx_queue *txq, const char *msg)
  359. {
  360. u8 queue = txq->swq_id;
  361. u8 ac = queue & 3;
  362. u8 hwq = (queue >> 2) & 0x1f;
  363. struct iwl_trans_pcie *trans_pcie =
  364. IWL_TRANS_GET_PCIE_TRANS(trans);
  365. if (test_and_clear_bit(hwq, trans_pcie->queue_stopped)) {
  366. if (atomic_dec_return(&trans_pcie->queue_stop_count[ac]) <= 0) {
  367. iwl_op_mode_queue_not_full(trans->op_mode, ac);
  368. IWL_DEBUG_TX_QUEUES(trans, "Wake hwq %d ac %d. %s",
  369. hwq, ac, msg);
  370. } else {
  371. IWL_DEBUG_TX_QUEUES(trans, "Don't wake hwq %d ac %d"
  372. " stop count %d. %s",
  373. hwq, ac, atomic_read(&trans_pcie->
  374. queue_stop_count[ac]), msg);
  375. }
  376. }
  377. }
  378. static inline void iwl_stop_queue(struct iwl_trans *trans,
  379. struct iwl_tx_queue *txq, const char *msg)
  380. {
  381. u8 queue = txq->swq_id;
  382. u8 ac = queue & 3;
  383. u8 hwq = (queue >> 2) & 0x1f;
  384. struct iwl_trans_pcie *trans_pcie =
  385. IWL_TRANS_GET_PCIE_TRANS(trans);
  386. if (!test_and_set_bit(hwq, trans_pcie->queue_stopped)) {
  387. if (atomic_inc_return(&trans_pcie->queue_stop_count[ac]) > 0) {
  388. iwl_op_mode_queue_full(trans->op_mode, ac);
  389. IWL_DEBUG_TX_QUEUES(trans, "Stop hwq %d ac %d"
  390. " stop count %d. %s",
  391. hwq, ac, atomic_read(&trans_pcie->
  392. queue_stop_count[ac]), msg);
  393. } else {
  394. IWL_DEBUG_TX_QUEUES(trans, "Don't stop hwq %d ac %d"
  395. " stop count %d. %s",
  396. hwq, ac, atomic_read(&trans_pcie->
  397. queue_stop_count[ac]), msg);
  398. }
  399. } else {
  400. IWL_DEBUG_TX_QUEUES(trans, "stop hwq %d, but it is stopped/ %s",
  401. hwq, msg);
  402. }
  403. }
  404. #ifdef ieee80211_stop_queue
  405. #undef ieee80211_stop_queue
  406. #endif
  407. #define ieee80211_stop_queue DO_NOT_USE_ieee80211_stop_queue
  408. #ifdef ieee80211_wake_queue
  409. #undef ieee80211_wake_queue
  410. #endif
  411. #define ieee80211_wake_queue DO_NOT_USE_ieee80211_wake_queue
  412. static inline void iwl_txq_ctx_activate(struct iwl_trans_pcie *trans_pcie,
  413. int txq_id)
  414. {
  415. set_bit(txq_id, &trans_pcie->txq_ctx_active_msk);
  416. }
  417. static inline void iwl_txq_ctx_deactivate(struct iwl_trans_pcie *trans_pcie,
  418. int txq_id)
  419. {
  420. clear_bit(txq_id, &trans_pcie->txq_ctx_active_msk);
  421. }
  422. static inline int iwl_queue_used(const struct iwl_queue *q, int i)
  423. {
  424. return q->write_ptr >= q->read_ptr ?
  425. (i >= q->read_ptr && i < q->write_ptr) :
  426. !(i < q->read_ptr && i >= q->write_ptr);
  427. }
  428. static inline u8 get_cmd_index(struct iwl_queue *q, u32 index)
  429. {
  430. return index & (q->n_window - 1);
  431. }
  432. #define IWL_TX_FIFO_BK 0 /* shared */
  433. #define IWL_TX_FIFO_BE 1
  434. #define IWL_TX_FIFO_VI 2 /* shared */
  435. #define IWL_TX_FIFO_VO 3
  436. #define IWL_TX_FIFO_BK_IPAN IWL_TX_FIFO_BK
  437. #define IWL_TX_FIFO_BE_IPAN 4
  438. #define IWL_TX_FIFO_VI_IPAN IWL_TX_FIFO_VI
  439. #define IWL_TX_FIFO_VO_IPAN 5
  440. /* re-uses the VO FIFO, uCode will properly flush/schedule */
  441. #define IWL_TX_FIFO_AUX 5
  442. #define IWL_TX_FIFO_UNUSED -1
  443. /* AUX (TX during scan dwell) queue */
  444. #define IWL_AUX_QUEUE 10
  445. #endif /* __iwl_trans_int_pcie_h__ */