iwl-trans-pcie-int.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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/wait.h>
  35. #include <linux/pci.h>
  36. #include "iwl-fh.h"
  37. #include "iwl-csr.h"
  38. #include "iwl-shared.h"
  39. #include "iwl-trans.h"
  40. #include "iwl-debug.h"
  41. #include "iwl-io.h"
  42. #include "iwl-op-mode.h"
  43. struct iwl_tx_queue;
  44. struct iwl_queue;
  45. struct iwl_host_cmd;
  46. /*This file includes the declaration that are internal to the
  47. * trans_pcie layer */
  48. struct iwl_rx_mem_buffer {
  49. dma_addr_t page_dma;
  50. struct page *page;
  51. struct list_head list;
  52. };
  53. /**
  54. * struct isr_statistics - interrupt statistics
  55. *
  56. */
  57. struct isr_statistics {
  58. u32 hw;
  59. u32 sw;
  60. u32 err_code;
  61. u32 sch;
  62. u32 alive;
  63. u32 rfkill;
  64. u32 ctkill;
  65. u32 wakeup;
  66. u32 rx;
  67. u32 tx;
  68. u32 unhandled;
  69. };
  70. /**
  71. * struct iwl_rx_queue - Rx queue
  72. * @bd: driver's pointer to buffer of receive buffer descriptors (rbd)
  73. * @bd_dma: bus address of buffer of receive buffer descriptors (rbd)
  74. * @pool:
  75. * @queue:
  76. * @read: Shared index to newest available Rx buffer
  77. * @write: Shared index to oldest written Rx packet
  78. * @free_count: Number of pre-allocated buffers in rx_free
  79. * @write_actual:
  80. * @rx_free: list of free SKBs for use
  81. * @rx_used: List of Rx buffers with no SKB
  82. * @need_update: flag to indicate we need to update read/write index
  83. * @rb_stts: driver's pointer to receive buffer status
  84. * @rb_stts_dma: bus address of receive buffer status
  85. * @lock:
  86. *
  87. * NOTE: rx_free and rx_used are used as a FIFO for iwl_rx_mem_buffers
  88. */
  89. struct iwl_rx_queue {
  90. __le32 *bd;
  91. dma_addr_t bd_dma;
  92. struct iwl_rx_mem_buffer pool[RX_QUEUE_SIZE + RX_FREE_BUFFERS];
  93. struct iwl_rx_mem_buffer *queue[RX_QUEUE_SIZE];
  94. u32 read;
  95. u32 write;
  96. u32 free_count;
  97. u32 write_actual;
  98. struct list_head rx_free;
  99. struct list_head rx_used;
  100. int need_update;
  101. struct iwl_rb_status *rb_stts;
  102. dma_addr_t rb_stts_dma;
  103. spinlock_t lock;
  104. };
  105. struct iwl_dma_ptr {
  106. dma_addr_t dma;
  107. void *addr;
  108. size_t size;
  109. };
  110. /**
  111. * iwl_queue_inc_wrap - increment queue index, wrap back to beginning
  112. * @index -- current index
  113. * @n_bd -- total number of entries in queue (must be power of 2)
  114. */
  115. static inline int iwl_queue_inc_wrap(int index, int n_bd)
  116. {
  117. return ++index & (n_bd - 1);
  118. }
  119. /**
  120. * iwl_queue_dec_wrap - decrement queue index, wrap back to end
  121. * @index -- current index
  122. * @n_bd -- total number of entries in queue (must be power of 2)
  123. */
  124. static inline int iwl_queue_dec_wrap(int index, int n_bd)
  125. {
  126. return --index & (n_bd - 1);
  127. }
  128. struct iwl_cmd_meta {
  129. /* only for SYNC commands, iff the reply skb is wanted */
  130. struct iwl_host_cmd *source;
  131. u32 flags;
  132. DEFINE_DMA_UNMAP_ADDR(mapping);
  133. DEFINE_DMA_UNMAP_LEN(len);
  134. };
  135. /*
  136. * Generic queue structure
  137. *
  138. * Contains common data for Rx and Tx queues.
  139. *
  140. * Note the difference between n_bd and n_window: the hardware
  141. * always assumes 256 descriptors, so n_bd is always 256 (unless
  142. * there might be HW changes in the future). For the normal TX
  143. * queues, n_window, which is the size of the software queue data
  144. * is also 256; however, for the command queue, n_window is only
  145. * 32 since we don't need so many commands pending. Since the HW
  146. * still uses 256 BDs for DMA though, n_bd stays 256. As a result,
  147. * the software buffers (in the variables @meta, @txb in struct
  148. * iwl_tx_queue) only have 32 entries, while the HW buffers (@tfds
  149. * in the same struct) have 256.
  150. * This means that we end up with the following:
  151. * HW entries: | 0 | ... | N * 32 | ... | N * 32 + 31 | ... | 255 |
  152. * SW entries: | 0 | ... | 31 |
  153. * where N is a number between 0 and 7. This means that the SW
  154. * data is a window overlayed over the HW queue.
  155. */
  156. struct iwl_queue {
  157. int n_bd; /* number of BDs in this queue */
  158. int write_ptr; /* 1-st empty entry (index) host_w*/
  159. int read_ptr; /* last used entry (index) host_r*/
  160. /* use for monitoring and recovering the stuck queue */
  161. dma_addr_t dma_addr; /* physical addr for BD's */
  162. int n_window; /* safe queue window */
  163. u32 id;
  164. int low_mark; /* low watermark, resume queue if free
  165. * space more than this */
  166. int high_mark; /* high watermark, stop queue if free
  167. * space less than this */
  168. };
  169. /**
  170. * struct iwl_tx_queue - Tx Queue for DMA
  171. * @q: generic Rx/Tx queue descriptor
  172. * @bd: base of circular buffer of TFDs
  173. * @cmd: array of command/TX buffer pointers
  174. * @meta: array of meta data for each command/tx buffer
  175. * @dma_addr_cmd: physical address of cmd/tx buffer array
  176. * @txb: array of per-TFD driver data
  177. * lock: queue lock
  178. * @time_stamp: time (in jiffies) of last read_ptr change
  179. * @need_update: indicates need to update read/write index
  180. *
  181. * A Tx queue consists of circular buffer of BDs (a.k.a. TFDs, transmit frame
  182. * descriptors) and required locking structures.
  183. */
  184. #define TFD_TX_CMD_SLOTS 256
  185. #define TFD_CMD_SLOTS 32
  186. struct iwl_tx_queue {
  187. struct iwl_queue q;
  188. struct iwl_tfd *tfds;
  189. struct iwl_device_cmd **cmd;
  190. struct iwl_cmd_meta *meta;
  191. struct sk_buff **skbs;
  192. spinlock_t lock;
  193. unsigned long time_stamp;
  194. u8 need_update;
  195. u8 active;
  196. };
  197. /**
  198. * struct iwl_trans_pcie - PCIe transport specific data
  199. * @rxq: all the RX queue data
  200. * @rx_replenish: work that will be called when buffers need to be allocated
  201. * @trans: pointer to the generic transport area
  202. * @irq - the irq number for the device
  203. * @irq_requested: true when the irq has been requested
  204. * @scd_base_addr: scheduler sram base address in SRAM
  205. * @scd_bc_tbls: pointer to the byte count table of the scheduler
  206. * @kw: keep warm address
  207. * @pci_dev: basic pci-network driver stuff
  208. * @hw_base: pci hardware address support
  209. * @ucode_write_complete: indicates that the ucode has been copied.
  210. * @ucode_write_waitq: wait queue for uCode load
  211. * @status - transport specific status flags
  212. * @cmd_queue - command queue number
  213. */
  214. struct iwl_trans_pcie {
  215. struct iwl_rx_queue rxq;
  216. struct work_struct rx_replenish;
  217. struct iwl_trans *trans;
  218. /* INT ICT Table */
  219. __le32 *ict_tbl;
  220. dma_addr_t ict_tbl_dma;
  221. int ict_index;
  222. u32 inta;
  223. bool use_ict;
  224. bool irq_requested;
  225. struct tasklet_struct irq_tasklet;
  226. struct isr_statistics isr_stats;
  227. unsigned int irq;
  228. spinlock_t irq_lock;
  229. u32 inta_mask;
  230. u32 scd_base_addr;
  231. struct iwl_dma_ptr scd_bc_tbls;
  232. struct iwl_dma_ptr kw;
  233. struct iwl_tx_queue *txq;
  234. unsigned long queue_used[BITS_TO_LONGS(IWL_MAX_HW_QUEUES)];
  235. unsigned long queue_stopped[BITS_TO_LONGS(IWL_MAX_HW_QUEUES)];
  236. /* PCI bus related data */
  237. struct pci_dev *pci_dev;
  238. void __iomem *hw_base;
  239. bool ucode_write_complete;
  240. wait_queue_head_t ucode_write_waitq;
  241. unsigned long status;
  242. u8 cmd_queue;
  243. u8 n_no_reclaim_cmds;
  244. u8 no_reclaim_cmds[MAX_NO_RECLAIM_CMDS];
  245. u8 setup_q_to_fifo[IWL_MAX_HW_QUEUES];
  246. u8 n_q_to_fifo;
  247. };
  248. #define IWL_TRANS_GET_PCIE_TRANS(_iwl_trans) \
  249. ((struct iwl_trans_pcie *) ((_iwl_trans)->trans_specific))
  250. /*****************************************************
  251. * RX
  252. ******************************************************/
  253. void iwl_bg_rx_replenish(struct work_struct *data);
  254. void iwl_irq_tasklet(struct iwl_trans *trans);
  255. void iwlagn_rx_replenish(struct iwl_trans *trans);
  256. void iwl_rx_queue_update_write_ptr(struct iwl_trans *trans,
  257. struct iwl_rx_queue *q);
  258. /*****************************************************
  259. * ICT
  260. ******************************************************/
  261. void iwl_reset_ict(struct iwl_trans *trans);
  262. void iwl_disable_ict(struct iwl_trans *trans);
  263. int iwl_alloc_isr_ict(struct iwl_trans *trans);
  264. void iwl_free_isr_ict(struct iwl_trans *trans);
  265. irqreturn_t iwl_isr_ict(int irq, void *data);
  266. /*****************************************************
  267. * TX / HCMD
  268. ******************************************************/
  269. void iwl_txq_update_write_ptr(struct iwl_trans *trans,
  270. struct iwl_tx_queue *txq);
  271. int iwlagn_txq_attach_buf_to_tfd(struct iwl_trans *trans,
  272. struct iwl_tx_queue *txq,
  273. dma_addr_t addr, u16 len, u8 reset);
  274. int iwl_queue_init(struct iwl_queue *q, int count, int slots_num, u32 id);
  275. int iwl_trans_pcie_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd);
  276. void iwl_tx_cmd_complete(struct iwl_trans *trans,
  277. struct iwl_rx_cmd_buffer *rxb, int handler_status);
  278. void iwl_trans_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
  279. struct iwl_tx_queue *txq,
  280. u16 byte_cnt);
  281. void iwl_trans_pcie_tx_agg_disable(struct iwl_trans *trans, int queue);
  282. void iwl_trans_set_wr_ptrs(struct iwl_trans *trans, int txq_id, u32 index);
  283. void iwl_trans_tx_queue_set_status(struct iwl_trans *trans,
  284. struct iwl_tx_queue *txq,
  285. int tx_fifo_id, bool active);
  286. void iwl_trans_pcie_tx_agg_setup(struct iwl_trans *trans, int queue, int fifo,
  287. int sta_id, int tid, int frame_limit, u16 ssn);
  288. void iwlagn_txq_free_tfd(struct iwl_trans *trans, struct iwl_tx_queue *txq,
  289. int index, enum dma_data_direction dma_dir);
  290. int iwl_tx_queue_reclaim(struct iwl_trans *trans, int txq_id, int index,
  291. struct sk_buff_head *skbs);
  292. int iwl_queue_space(const struct iwl_queue *q);
  293. /*****************************************************
  294. * Error handling
  295. ******************************************************/
  296. int iwl_dump_fh(struct iwl_trans *trans, char **buf, bool display);
  297. void iwl_dump_csr(struct iwl_trans *trans);
  298. /*****************************************************
  299. * Helpers
  300. ******************************************************/
  301. static inline void iwl_disable_interrupts(struct iwl_trans *trans)
  302. {
  303. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  304. clear_bit(STATUS_INT_ENABLED, &trans_pcie->status);
  305. /* disable interrupts from uCode/NIC to host */
  306. iwl_write32(trans, CSR_INT_MASK, 0x00000000);
  307. /* acknowledge/clear/reset any interrupts still pending
  308. * from uCode or flow handler (Rx/Tx DMA) */
  309. iwl_write32(trans, CSR_INT, 0xffffffff);
  310. iwl_write32(trans, CSR_FH_INT_STATUS, 0xffffffff);
  311. IWL_DEBUG_ISR(trans, "Disabled interrupts\n");
  312. }
  313. static inline void iwl_enable_interrupts(struct iwl_trans *trans)
  314. {
  315. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  316. IWL_DEBUG_ISR(trans, "Enabling interrupts\n");
  317. set_bit(STATUS_INT_ENABLED, &trans_pcie->status);
  318. iwl_write32(trans, CSR_INT_MASK, trans_pcie->inta_mask);
  319. }
  320. static inline void iwl_enable_rfkill_int(struct iwl_trans *trans)
  321. {
  322. IWL_DEBUG_ISR(trans, "Enabling rfkill interrupt\n");
  323. iwl_write32(trans, CSR_INT_MASK, CSR_INT_BIT_RF_KILL);
  324. }
  325. static inline void iwl_wake_queue(struct iwl_trans *trans,
  326. struct iwl_tx_queue *txq)
  327. {
  328. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  329. if (test_and_clear_bit(txq->q.id, trans_pcie->queue_stopped)) {
  330. IWL_DEBUG_TX_QUEUES(trans, "Wake hwq %d\n", txq->q.id);
  331. iwl_op_mode_queue_not_full(trans->op_mode, txq->q.id);
  332. }
  333. }
  334. static inline void iwl_stop_queue(struct iwl_trans *trans,
  335. struct iwl_tx_queue *txq)
  336. {
  337. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  338. if (!test_and_set_bit(txq->q.id, trans_pcie->queue_stopped)) {
  339. iwl_op_mode_queue_full(trans->op_mode, txq->q.id);
  340. IWL_DEBUG_TX_QUEUES(trans, "Stop hwq %d\n", txq->q.id);
  341. } else
  342. IWL_DEBUG_TX_QUEUES(trans, "hwq %d already stopped\n",
  343. txq->q.id);
  344. }
  345. static inline int iwl_queue_used(const struct iwl_queue *q, int i)
  346. {
  347. return q->write_ptr >= q->read_ptr ?
  348. (i >= q->read_ptr && i < q->write_ptr) :
  349. !(i < q->read_ptr && i >= q->write_ptr);
  350. }
  351. static inline u8 get_cmd_index(struct iwl_queue *q, u32 index)
  352. {
  353. return index & (q->n_window - 1);
  354. }
  355. #endif /* __iwl_trans_int_pcie_h__ */