iwl-trans-pcie-int.h 14 KB

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