blob: 93709fe28d7644c0132247ebce11d5737ee97b41 [file] [log] [blame]
Ron Rindjunsky1053d352008-05-05 10:22:43 +08001/******************************************************************************
2 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02003 * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
Ron Rindjunsky1053d352008-05-05 10:22:43 +08004 *
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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Ron Rindjunsky1053d352008-05-05 10:22:43 +080026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
Tomas Winklerfd4abac2008-05-15 13:54:07 +080029#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Emmanuel Grumbach253a6342011-07-11 07:39:46 -070031#include <linux/sched.h>
Emmanuel Grumbach253a6342011-07-11 07:39:46 -070032
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070033#include "iwl-debug.h"
34#include "iwl-csr.h"
35#include "iwl-prph.h"
Ron Rindjunsky1053d352008-05-05 10:22:43 +080036#include "iwl-io.h"
Emmanuel Grumbached277c92012-02-09 16:08:15 +020037#include "iwl-op-mode.h"
Johannes Berg6468a012012-05-16 19:13:54 +020038#include "internal.h"
Johannes Berg6238b002012-04-02 15:04:33 +020039/* FIXME: need to abstract out TX command (once we know what it looks like) */
Johannes Berg1023fdc2012-05-15 12:16:34 +020040#include "dvm/commands.h"
Ron Rindjunsky1053d352008-05-05 10:22:43 +080041
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070042#define IWL_TX_CRC_SIZE 4
43#define IWL_TX_DELIMITER_SIZE 4
44
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +020045/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
46 * DMA services
47 *
48 * Theory of operation
49 *
50 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
51 * of buffer descriptors, each of which points to one or more data buffers for
52 * the device to read from or fill. Driver and device exchange status of each
53 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
54 * entries in each circular buffer, to protect against confusing empty and full
55 * queue states.
56 *
57 * The device reads or writes the data in the queues via the device's several
58 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
59 *
60 * For Tx queue, there are low mark and high mark limits. If, after queuing
61 * the packet for Tx, free space become < low mark, Tx queue stopped. When
62 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
63 * Tx queue resumed.
64 *
65 ***************************************************/
66static int iwl_queue_space(const struct iwl_queue *q)
67{
Ido Yariva9b29242013-07-15 11:51:48 -040068 unsigned int max;
69 unsigned int used;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +020070
Ido Yariva9b29242013-07-15 11:51:48 -040071 /*
72 * To avoid ambiguity between empty and completely full queues, there
Johannes Berg83f32a42014-04-24 09:57:40 +020073 * should always be less than TFD_QUEUE_SIZE_MAX elements in the queue.
74 * If q->n_window is smaller than TFD_QUEUE_SIZE_MAX, there is no need
75 * to reserve any queue entries for this purpose.
Ido Yariva9b29242013-07-15 11:51:48 -040076 */
Johannes Berg83f32a42014-04-24 09:57:40 +020077 if (q->n_window < TFD_QUEUE_SIZE_MAX)
Ido Yariva9b29242013-07-15 11:51:48 -040078 max = q->n_window;
79 else
Johannes Berg83f32a42014-04-24 09:57:40 +020080 max = TFD_QUEUE_SIZE_MAX - 1;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +020081
Ido Yariva9b29242013-07-15 11:51:48 -040082 /*
Johannes Berg83f32a42014-04-24 09:57:40 +020083 * TFD_QUEUE_SIZE_MAX is a power of 2, so the following is equivalent to
84 * modulo by TFD_QUEUE_SIZE_MAX and is well defined.
Ido Yariva9b29242013-07-15 11:51:48 -040085 */
Johannes Berg83f32a42014-04-24 09:57:40 +020086 used = (q->write_ptr - q->read_ptr) & (TFD_QUEUE_SIZE_MAX - 1);
Ido Yariva9b29242013-07-15 11:51:48 -040087
88 if (WARN_ON(used > max))
89 return 0;
90
91 return max - used;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +020092}
93
94/*
95 * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
96 */
Johannes Berg83f32a42014-04-24 09:57:40 +020097static int iwl_queue_init(struct iwl_queue *q, int slots_num, u32 id)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +020098{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +020099 q->n_window = slots_num;
100 q->id = id;
101
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200102 /* slots_num must be power-of-two size, otherwise
103 * get_cmd_index is broken. */
104 if (WARN_ON(!is_power_of_2(slots_num)))
105 return -EINVAL;
106
107 q->low_mark = q->n_window / 4;
108 if (q->low_mark < 4)
109 q->low_mark = 4;
110
111 q->high_mark = q->n_window / 8;
112 if (q->high_mark < 2)
113 q->high_mark = 2;
114
115 q->write_ptr = 0;
116 q->read_ptr = 0;
117
118 return 0;
119}
120
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200121static int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
122 struct iwl_dma_ptr *ptr, size_t size)
123{
124 if (WARN_ON(ptr->addr))
125 return -EINVAL;
126
127 ptr->addr = dma_alloc_coherent(trans->dev, size,
128 &ptr->dma, GFP_KERNEL);
129 if (!ptr->addr)
130 return -ENOMEM;
131 ptr->size = size;
132 return 0;
133}
134
135static void iwl_pcie_free_dma_ptr(struct iwl_trans *trans,
136 struct iwl_dma_ptr *ptr)
137{
138 if (unlikely(!ptr->addr))
139 return;
140
141 dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
142 memset(ptr, 0, sizeof(*ptr));
143}
144
145static void iwl_pcie_txq_stuck_timer(unsigned long data)
146{
147 struct iwl_txq *txq = (void *)data;
148 struct iwl_queue *q = &txq->q;
149 struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
150 struct iwl_trans *trans = iwl_trans_pcie_get_trans(trans_pcie);
151 u32 scd_sram_addr = trans_pcie->scd_base_addr +
152 SCD_TX_STTS_QUEUE_OFFSET(txq->q.id);
153 u8 buf[16];
154 int i;
155
156 spin_lock(&txq->lock);
157 /* check if triggered erroneously */
158 if (txq->q.read_ptr == txq->q.write_ptr) {
159 spin_unlock(&txq->lock);
160 return;
161 }
162 spin_unlock(&txq->lock);
163
164 IWL_ERR(trans, "Queue %d stuck for %u ms.\n", txq->q.id,
165 jiffies_to_msecs(trans_pcie->wd_timeout));
166 IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
167 txq->q.read_ptr, txq->q.write_ptr);
168
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +0200169 iwl_trans_read_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf));
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200170
171 iwl_print_hex_error(trans, buf, sizeof(buf));
172
173 for (i = 0; i < FH_TCSR_CHNL_NUM; i++)
174 IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", i,
175 iwl_read_direct32(trans, FH_TX_TRB_REG(i)));
176
177 for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
178 u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(i));
179 u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
180 bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
181 u32 tbl_dw =
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +0200182 iwl_trans_read_mem32(trans,
183 trans_pcie->scd_base_addr +
184 SCD_TRANS_TBL_OFFSET_QUEUE(i));
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200185
186 if (i & 0x1)
187 tbl_dw = (tbl_dw & 0xFFFF0000) >> 16;
188 else
189 tbl_dw = tbl_dw & 0x0000FFFF;
190
191 IWL_ERR(trans,
192 "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n",
193 i, active ? "" : "in", fifo, tbl_dw,
Johannes Berg83f32a42014-04-24 09:57:40 +0200194 iwl_read_prph(trans, SCD_QUEUE_RDPTR(i)) &
195 (TFD_QUEUE_SIZE_MAX - 1),
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200196 iwl_read_prph(trans, SCD_QUEUE_WRPTR(i)));
197 }
198
199 for (i = q->read_ptr; i != q->write_ptr;
Johannes Berg83f32a42014-04-24 09:57:40 +0200200 i = iwl_queue_inc_wrap(i))
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200201 IWL_ERR(trans, "scratch %d = 0x%08x\n", i,
Johannes Berg38c0f3342013-02-27 13:18:50 +0100202 le32_to_cpu(txq->scratchbufs[i].scratch));
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200203
Emmanuel Grumbachcfadc3f2014-02-12 08:51:54 +0200204 iwl_write_prph(trans, DEVICE_SET_NMI_REG, 1);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200205}
206
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200207/*
208 * iwl_pcie_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300209 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200210static void iwl_pcie_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
211 struct iwl_txq *txq, u16 byte_cnt)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300212{
Emmanuel Grumbach105183b2011-08-25 23:11:02 -0700213 struct iwlagn_scd_bc_tbl *scd_bc_tbl;
Johannes Berg20d3b642012-05-16 22:54:29 +0200214 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300215 int write_ptr = txq->q.write_ptr;
216 int txq_id = txq->q.id;
217 u8 sec_ctl = 0;
218 u8 sta_id = 0;
219 u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
220 __le16 bc_ent;
Emmanuel Grumbach132f98c2011-09-20 15:37:24 -0700221 struct iwl_tx_cmd *tx_cmd =
Johannes Bergbf8440e2012-03-19 17:12:06 +0100222 (void *) txq->entries[txq->q.write_ptr].cmd->payload;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300223
Emmanuel Grumbach105183b2011-08-25 23:11:02 -0700224 scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
225
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300226 WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
227
Emmanuel Grumbach132f98c2011-09-20 15:37:24 -0700228 sta_id = tx_cmd->sta_id;
229 sec_ctl = tx_cmd->sec_ctl;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300230
231 switch (sec_ctl & TX_CMD_SEC_MSK) {
232 case TX_CMD_SEC_CCM:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200233 len += IEEE80211_CCMP_MIC_LEN;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300234 break;
235 case TX_CMD_SEC_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200236 len += IEEE80211_TKIP_ICV_LEN;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300237 break;
238 case TX_CMD_SEC_WEP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200239 len += IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300240 break;
241 }
242
Emmanuel Grumbach046db342012-12-05 15:07:54 +0200243 if (trans_pcie->bc_table_dword)
244 len = DIV_ROUND_UP(len, 4);
245
246 bc_ent = cpu_to_le16(len | (sta_id << 12));
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300247
248 scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
249
250 if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
251 scd_bc_tbl[txq_id].
252 tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
253}
254
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200255static void iwl_pcie_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
256 struct iwl_txq *txq)
257{
258 struct iwl_trans_pcie *trans_pcie =
259 IWL_TRANS_GET_PCIE_TRANS(trans);
260 struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
261 int txq_id = txq->q.id;
262 int read_ptr = txq->q.read_ptr;
263 u8 sta_id = 0;
264 __le16 bc_ent;
265 struct iwl_tx_cmd *tx_cmd =
266 (void *)txq->entries[txq->q.read_ptr].cmd->payload;
267
268 WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
269
270 if (txq_id != trans_pcie->cmd_queue)
271 sta_id = tx_cmd->sta_id;
272
273 bc_ent = cpu_to_le16(1 | (sta_id << 12));
274 scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
275
276 if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
277 scd_bc_tbl[txq_id].
278 tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
279}
280
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200281/*
282 * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800283 */
Johannes Bergea68f462014-02-27 14:36:55 +0100284static void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans,
285 struct iwl_txq *txq)
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800286{
Emmanuel Grumbach23e76d12014-01-20 09:50:29 +0200287 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800288 u32 reg = 0;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800289 int txq_id = txq->q.id;
290
Johannes Bergea68f462014-02-27 14:36:55 +0100291 lockdep_assert_held(&txq->lock);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800292
Eliad Peller50453882014-02-05 19:12:24 +0200293 /*
294 * explicitly wake up the NIC if:
295 * 1. shadow registers aren't enabled
296 * 2. NIC is woken up for CMD regardless of shadow outside this function
297 * 3. there is a chance that the NIC is asleep
298 */
299 if (!trans->cfg->base_params->shadow_reg_enable &&
300 txq_id != trans_pcie->cmd_queue &&
301 test_bit(STATUS_TPOWER_PMI, &trans->status)) {
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800302 /*
Eliad Peller50453882014-02-05 19:12:24 +0200303 * wake up nic if it's powered down ...
304 * uCode will wake up, and interrupt us again, so next
305 * time we'll skip this part.
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800306 */
Eliad Peller50453882014-02-05 19:12:24 +0200307 reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
308
309 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
310 IWL_DEBUG_INFO(trans, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
311 txq_id, reg);
312 iwl_set_bit(trans, CSR_GP_CNTRL,
313 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Johannes Bergea68f462014-02-27 14:36:55 +0100314 txq->need_update = true;
Eliad Peller50453882014-02-05 19:12:24 +0200315 return;
316 }
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800317 }
Eliad Peller50453882014-02-05 19:12:24 +0200318
319 /*
320 * if not in power-save mode, uCode will never sleep when we're
321 * trying to tx (during RFKILL, we're not trying to tx).
322 */
323 IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id, txq->q.write_ptr);
324 iwl_write32(trans, HBUS_TARG_WRPTR, txq->q.write_ptr | (txq_id << 8));
Johannes Bergea68f462014-02-27 14:36:55 +0100325}
Eliad Peller50453882014-02-05 19:12:24 +0200326
Johannes Bergea68f462014-02-27 14:36:55 +0100327void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans)
328{
329 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
330 int i;
331
332 for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
333 struct iwl_txq *txq = &trans_pcie->txq[i];
334
335 spin_lock(&txq->lock);
336 if (trans_pcie->txq[i].need_update) {
337 iwl_pcie_txq_inc_wr_ptr(trans, txq);
338 trans_pcie->txq[i].need_update = false;
339 }
340 spin_unlock(&txq->lock);
341 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800342}
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800343
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200344static inline dma_addr_t iwl_pcie_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
Johannes Berg214d14d2011-05-04 07:50:44 -0700345{
346 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
347
348 dma_addr_t addr = get_unaligned_le32(&tb->lo);
349 if (sizeof(dma_addr_t) > sizeof(u32))
350 addr |=
351 ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
352
353 return addr;
354}
355
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200356static inline u16 iwl_pcie_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
Johannes Berg214d14d2011-05-04 07:50:44 -0700357{
358 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
359
360 return le16_to_cpu(tb->hi_n_len) >> 4;
361}
362
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200363static inline void iwl_pcie_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
364 dma_addr_t addr, u16 len)
Johannes Berg214d14d2011-05-04 07:50:44 -0700365{
366 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
367 u16 hi_n_len = len << 4;
368
369 put_unaligned_le32(addr, &tb->lo);
370 if (sizeof(dma_addr_t) > sizeof(u32))
371 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
372
373 tb->hi_n_len = cpu_to_le16(hi_n_len);
374
375 tfd->num_tbs = idx + 1;
376}
377
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200378static inline u8 iwl_pcie_tfd_get_num_tbs(struct iwl_tfd *tfd)
Johannes Berg214d14d2011-05-04 07:50:44 -0700379{
380 return tfd->num_tbs & 0x1f;
381}
382
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200383static void iwl_pcie_tfd_unmap(struct iwl_trans *trans,
Johannes Berg98891752013-02-26 11:28:19 +0100384 struct iwl_cmd_meta *meta,
385 struct iwl_tfd *tfd)
Johannes Berg214d14d2011-05-04 07:50:44 -0700386{
Johannes Berg214d14d2011-05-04 07:50:44 -0700387 int i;
388 int num_tbs;
389
Johannes Berg214d14d2011-05-04 07:50:44 -0700390 /* Sanity check on number of chunks */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200391 num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
Johannes Berg214d14d2011-05-04 07:50:44 -0700392
393 if (num_tbs >= IWL_NUM_OF_TBS) {
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700394 IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
Johannes Berg214d14d2011-05-04 07:50:44 -0700395 /* @todo issue fatal error, it is quite serious situation */
396 return;
397 }
398
Johannes Berg38c0f3342013-02-27 13:18:50 +0100399 /* first TB is never freed - it's the scratchbuf data */
Johannes Berg214d14d2011-05-04 07:50:44 -0700400
Johannes Berg214d14d2011-05-04 07:50:44 -0700401 for (i = 1; i < num_tbs; i++)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200402 dma_unmap_single(trans->dev, iwl_pcie_tfd_tb_get_addr(tfd, i),
Johannes Berg98891752013-02-26 11:28:19 +0100403 iwl_pcie_tfd_tb_get_len(tfd, i),
404 DMA_TO_DEVICE);
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200405
406 tfd->num_tbs = 0;
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700407}
408
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200409/*
410 * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700411 * @trans - transport private data
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700412 * @txq - tx queue
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200413 * @dma_dir - the direction of the DMA mapping
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700414 *
415 * Does NOT advance any TFD circular buffer read/write indexes
416 * Does NOT free the TFD itself (which is within circular buffer)
417 */
Johannes Berg98891752013-02-26 11:28:19 +0100418static void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700419{
420 struct iwl_tfd *tfd_tmp = txq->tfds;
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700421
Johannes Berg83f32a42014-04-24 09:57:40 +0200422 /* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and
423 * idx is bounded by n_window
424 */
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200425 int rd_ptr = txq->q.read_ptr;
426 int idx = get_cmd_index(&txq->q, rd_ptr);
427
Johannes Berg015c15e2012-03-05 11:24:24 -0800428 lockdep_assert_held(&txq->lock);
429
Johannes Berg83f32a42014-04-24 09:57:40 +0200430 /* We have only q->n_window txq->entries, but we use
431 * TFD_QUEUE_SIZE_MAX tfds
432 */
Johannes Berg98891752013-02-26 11:28:19 +0100433 iwl_pcie_tfd_unmap(trans, &txq->entries[idx].meta, &tfd_tmp[rd_ptr]);
Johannes Berg214d14d2011-05-04 07:50:44 -0700434
435 /* free SKB */
Johannes Bergbf8440e2012-03-19 17:12:06 +0100436 if (txq->entries) {
Johannes Berg214d14d2011-05-04 07:50:44 -0700437 struct sk_buff *skb;
438
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200439 skb = txq->entries[idx].skb;
Johannes Berg214d14d2011-05-04 07:50:44 -0700440
Emmanuel Grumbach909e9b22011-09-15 11:46:30 -0700441 /* Can be called from irqs-disabled context
442 * If skb is not NULL, it means that the whole queue is being
443 * freed and that the queue is not empty - free the skb
444 */
Johannes Berg214d14d2011-05-04 07:50:44 -0700445 if (skb) {
Emmanuel Grumbached277c92012-02-09 16:08:15 +0200446 iwl_op_mode_free_skb(trans->op_mode, skb);
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200447 txq->entries[idx].skb = NULL;
Johannes Berg214d14d2011-05-04 07:50:44 -0700448 }
449 }
450}
451
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200452static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
Johannes Berg6d6e68f2014-04-23 19:00:56 +0200453 dma_addr_t addr, u16 len, bool reset)
Johannes Berg214d14d2011-05-04 07:50:44 -0700454{
455 struct iwl_queue *q;
456 struct iwl_tfd *tfd, *tfd_tmp;
457 u32 num_tbs;
458
459 q = &txq->q;
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700460 tfd_tmp = txq->tfds;
Johannes Berg214d14d2011-05-04 07:50:44 -0700461 tfd = &tfd_tmp[q->write_ptr];
462
463 if (reset)
464 memset(tfd, 0, sizeof(*tfd));
465
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200466 num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
Johannes Berg214d14d2011-05-04 07:50:44 -0700467
468 /* Each TFD can point to a maximum 20 Tx buffers */
469 if (num_tbs >= IWL_NUM_OF_TBS) {
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700470 IWL_ERR(trans, "Error can not send more than %d chunks\n",
Johannes Berg20d3b642012-05-16 22:54:29 +0200471 IWL_NUM_OF_TBS);
Johannes Berg214d14d2011-05-04 07:50:44 -0700472 return -EINVAL;
473 }
474
Eliad Peller1092b9b2013-07-16 17:53:43 +0300475 if (WARN(addr & ~IWL_TX_DMA_MASK,
476 "Unaligned address = %llx\n", (unsigned long long)addr))
Johannes Berg214d14d2011-05-04 07:50:44 -0700477 return -EINVAL;
478
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200479 iwl_pcie_tfd_set_tb(tfd, num_tbs, addr, len);
Johannes Berg214d14d2011-05-04 07:50:44 -0700480
481 return 0;
482}
483
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200484static int iwl_pcie_txq_alloc(struct iwl_trans *trans,
485 struct iwl_txq *txq, int slots_num,
486 u32 txq_id)
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800487{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200488 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
489 size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
Johannes Berg38c0f3342013-02-27 13:18:50 +0100490 size_t scratchbuf_sz;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200491 int i;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800492
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200493 if (WARN_ON(txq->entries || txq->tfds))
494 return -EINVAL;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800495
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200496 setup_timer(&txq->stuck_timer, iwl_pcie_txq_stuck_timer,
497 (unsigned long)txq);
498 txq->trans_pcie = trans_pcie;
499
500 txq->q.n_window = slots_num;
501
502 txq->entries = kcalloc(slots_num,
503 sizeof(struct iwl_pcie_txq_entry),
504 GFP_KERNEL);
505
506 if (!txq->entries)
507 goto error;
508
509 if (txq_id == trans_pcie->cmd_queue)
510 for (i = 0; i < slots_num; i++) {
511 txq->entries[i].cmd =
512 kmalloc(sizeof(struct iwl_device_cmd),
513 GFP_KERNEL);
514 if (!txq->entries[i].cmd)
515 goto error;
516 }
517
518 /* Circular buffer of transmit frame descriptors (TFDs),
519 * shared with device */
520 txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
521 &txq->q.dma_addr, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000522 if (!txq->tfds)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200523 goto error;
Johannes Berg38c0f3342013-02-27 13:18:50 +0100524
525 BUILD_BUG_ON(IWL_HCMD_SCRATCHBUF_SIZE != sizeof(*txq->scratchbufs));
526 BUILD_BUG_ON(offsetof(struct iwl_pcie_txq_scratch_buf, scratch) !=
527 sizeof(struct iwl_cmd_header) +
528 offsetof(struct iwl_tx_cmd, scratch));
529
530 scratchbuf_sz = sizeof(*txq->scratchbufs) * slots_num;
531
532 txq->scratchbufs = dma_alloc_coherent(trans->dev, scratchbuf_sz,
533 &txq->scratchbufs_dma,
534 GFP_KERNEL);
535 if (!txq->scratchbufs)
536 goto err_free_tfds;
537
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200538 txq->q.id = txq_id;
539
540 return 0;
Johannes Berg38c0f3342013-02-27 13:18:50 +0100541err_free_tfds:
542 dma_free_coherent(trans->dev, tfd_sz, txq->tfds, txq->q.dma_addr);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200543error:
544 if (txq->entries && txq_id == trans_pcie->cmd_queue)
545 for (i = 0; i < slots_num; i++)
546 kfree(txq->entries[i].cmd);
547 kfree(txq->entries);
548 txq->entries = NULL;
549
550 return -ENOMEM;
551
552}
553
554static int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
555 int slots_num, u32 txq_id)
556{
557 int ret;
558
Johannes Berg43aa6162014-02-27 14:24:36 +0100559 txq->need_update = false;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200560
561 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
562 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
563 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
564
565 /* Initialize queue's high/low-water marks, and head/tail indexes */
Johannes Berg83f32a42014-04-24 09:57:40 +0200566 ret = iwl_queue_init(&txq->q, slots_num, txq_id);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200567 if (ret)
568 return ret;
569
570 spin_lock_init(&txq->lock);
571
572 /*
573 * Tell nic where to find circular buffer of Tx Frame Descriptors for
574 * given Tx queue, and enable the DMA channel used for that queue.
575 * Circular buffer (TFD queue in DRAM) physical base address */
576 iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
577 txq->q.dma_addr >> 8);
578
579 return 0;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800580}
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800581
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200582/*
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200583 * iwl_pcie_txq_unmap - Unmap any remaining DMA mappings and free skb's
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800584 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200585static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800586{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200587 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
588 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
589 struct iwl_queue *q = &txq->q;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800590
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200591 spin_lock_bh(&txq->lock);
592 while (q->write_ptr != q->read_ptr) {
Emmanuel Grumbachb9676132013-06-13 11:45:59 +0300593 IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
594 txq_id, q->read_ptr);
Johannes Berg98891752013-02-26 11:28:19 +0100595 iwl_pcie_txq_free_tfd(trans, txq);
Johannes Berg83f32a42014-04-24 09:57:40 +0200596 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200597 }
Emmanuel Grumbachb9676132013-06-13 11:45:59 +0300598 txq->active = false;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200599 spin_unlock_bh(&txq->lock);
Emmanuel Grumbach8a487b12013-06-13 13:10:00 +0300600
601 /* just in case - this queue may have been stopped */
602 iwl_wake_queue(trans, txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200603}
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800604
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200605/*
606 * iwl_pcie_txq_free - Deallocate DMA queue.
607 * @txq: Transmit queue to deallocate.
608 *
609 * Empty queue by removing and destroying all BD's.
610 * Free all buffers.
611 * 0-fill, but do not free "txq" descriptor structure.
612 */
613static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
614{
615 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
616 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
617 struct device *dev = trans->dev;
618 int i;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800619
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200620 if (WARN_ON(!txq))
621 return;
622
623 iwl_pcie_txq_unmap(trans, txq_id);
624
625 /* De-alloc array of command/tx buffers */
626 if (txq_id == trans_pcie->cmd_queue)
627 for (i = 0; i < txq->q.n_window; i++) {
628 kfree(txq->entries[i].cmd);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200629 kfree(txq->entries[i].free_buf);
630 }
631
632 /* De-alloc circular buffer of TFDs */
Johannes Berg83f32a42014-04-24 09:57:40 +0200633 if (txq->tfds) {
634 dma_free_coherent(dev,
635 sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX,
636 txq->tfds, txq->q.dma_addr);
Johannes Bergd21fa2d2013-01-08 00:25:21 +0100637 txq->q.dma_addr = 0;
Johannes Berg83f32a42014-04-24 09:57:40 +0200638 txq->tfds = NULL;
Johannes Berg38c0f3342013-02-27 13:18:50 +0100639
640 dma_free_coherent(dev,
641 sizeof(*txq->scratchbufs) * txq->q.n_window,
642 txq->scratchbufs, txq->scratchbufs_dma);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200643 }
644
645 kfree(txq->entries);
646 txq->entries = NULL;
647
648 del_timer_sync(&txq->stuck_timer);
649
650 /* 0-fill queue descriptor structure */
651 memset(txq, 0, sizeof(*txq));
652}
653
654/*
655 * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
656 */
657static void iwl_pcie_txq_set_sched(struct iwl_trans *trans, u32 mask)
658{
659 struct iwl_trans_pcie __maybe_unused *trans_pcie =
660 IWL_TRANS_GET_PCIE_TRANS(trans);
661
662 iwl_write_prph(trans, SCD_TXFACT, mask);
663}
664
665void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
666{
667 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Johannes Berg22dc3c92013-01-09 00:47:07 +0100668 int nq = trans->cfg->base_params->num_of_queues;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200669 int chan;
670 u32 reg_val;
Johannes Berg22dc3c92013-01-09 00:47:07 +0100671 int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
672 SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200673
674 /* make sure all queue are not stopped/used */
675 memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
676 memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
677
678 trans_pcie->scd_base_addr =
679 iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
680
681 WARN_ON(scd_base_addr != 0 &&
682 scd_base_addr != trans_pcie->scd_base_addr);
683
Johannes Berg22dc3c92013-01-09 00:47:07 +0100684 /* reset context data, TX status and translation data */
685 iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
686 SCD_CONTEXT_MEM_LOWER_BOUND,
687 NULL, clear_dwords);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200688
689 iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
690 trans_pcie->scd_bc_tbls.dma >> 10);
691
692 /* The chain extension of the SCD doesn't work well. This feature is
693 * enabled by default by the HW, so we need to disable it manually.
694 */
Emmanuel Grumbache03bbb62014-04-13 10:49:16 +0300695 if (trans->cfg->base_params->scd_chain_ext_wa)
696 iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200697
698 iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue,
699 trans_pcie->cmd_fifo);
700
701 /* Activate all Tx DMA/FIFO channels */
702 iwl_pcie_txq_set_sched(trans, IWL_MASK(0, 7));
703
704 /* Enable DMA channel */
705 for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
706 iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
707 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
708 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
709
710 /* Update FH chicken bits */
711 reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
712 iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
713 reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
714
715 /* Enable L1-Active */
Eran Harary3073d8c2013-12-29 14:09:59 +0200716 if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
717 iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
718 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200719}
720
Johannes Bergddaf5a52013-01-08 11:25:44 +0100721void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
722{
723 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
724 int txq_id;
725
726 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
727 txq_id++) {
728 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
729
730 iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
731 txq->q.dma_addr >> 8);
732 iwl_pcie_txq_unmap(trans, txq_id);
733 txq->q.read_ptr = 0;
734 txq->q.write_ptr = 0;
735 }
736
737 /* Tell NIC where to find the "keep warm" buffer */
738 iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
739 trans_pcie->kw.dma >> 4);
740
741 iwl_pcie_tx_start(trans, trans_pcie->scd_base_addr);
742}
743
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200744/*
745 * iwl_pcie_tx_stop - Stop all Tx DMA channels
746 */
747int iwl_pcie_tx_stop(struct iwl_trans *trans)
748{
749 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
750 int ch, txq_id, ret;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200751
752 /* Turn off all Tx DMA fifos */
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +0200753 spin_lock(&trans_pcie->irq_lock);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200754
755 iwl_pcie_txq_set_sched(trans, 0);
756
757 /* Stop each Tx DMA channel, and wait for it to be idle */
758 for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
759 iwl_write_direct32(trans,
760 FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
761 ret = iwl_poll_direct_bit(trans, FH_TSSR_TX_STATUS_REG,
762 FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000);
763 if (ret < 0)
764 IWL_ERR(trans,
765 "Failing on timeout while stopping DMA channel %d [0x%08x]\n",
766 ch,
767 iwl_read_direct32(trans,
768 FH_TSSR_TX_STATUS_REG));
769 }
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +0200770 spin_unlock(&trans_pcie->irq_lock);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200771
Emmanuel Grumbachfba1c622013-12-19 22:19:17 +0200772 /*
773 * This function can be called before the op_mode disabled the
774 * queues. This happens when we have an rfkill interrupt.
775 * Since we stop Tx altogether - mark the queues as stopped.
776 */
777 memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
778 memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
779
780 /* This can happen: start_hw, stop_device */
781 if (!trans_pcie->txq)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200782 return 0;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200783
784 /* Unmap DMA from host system and free skb's */
785 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
786 txq_id++)
787 iwl_pcie_txq_unmap(trans, txq_id);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800788
789 return 0;
790}
791
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200792/*
793 * iwl_trans_tx_free - Free TXQ Context
794 *
795 * Destroy all TX DMA queues and structures
796 */
797void iwl_pcie_tx_free(struct iwl_trans *trans)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300798{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200799 int txq_id;
800 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300801
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200802 /* Tx queues */
803 if (trans_pcie->txq) {
804 for (txq_id = 0;
805 txq_id < trans->cfg->base_params->num_of_queues; txq_id++)
806 iwl_pcie_txq_free(trans, txq_id);
807 }
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300808
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200809 kfree(trans_pcie->txq);
810 trans_pcie->txq = NULL;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300811
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200812 iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300813
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200814 iwl_pcie_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300815}
816
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200817/*
818 * iwl_pcie_tx_alloc - allocate TX context
819 * Allocate all Tx DMA structures and initialize them
820 */
821static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
822{
823 int ret;
824 int txq_id, slots_num;
825 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
826
827 u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues *
828 sizeof(struct iwlagn_scd_bc_tbl);
829
830 /*It is not allowed to alloc twice, so warn when this happens.
831 * We cannot rely on the previous allocation, so free and fail */
832 if (WARN_ON(trans_pcie->txq)) {
833 ret = -EINVAL;
834 goto error;
835 }
836
837 ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
838 scd_bc_tbls_size);
839 if (ret) {
840 IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
841 goto error;
842 }
843
844 /* Alloc keep-warm buffer */
845 ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
846 if (ret) {
847 IWL_ERR(trans, "Keep Warm allocation failed\n");
848 goto error;
849 }
850
851 trans_pcie->txq = kcalloc(trans->cfg->base_params->num_of_queues,
852 sizeof(struct iwl_txq), GFP_KERNEL);
853 if (!trans_pcie->txq) {
854 IWL_ERR(trans, "Not enough memory for txq\n");
Dan Carpenter2ab9ba02013-08-11 02:03:21 +0300855 ret = -ENOMEM;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200856 goto error;
857 }
858
859 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
860 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
861 txq_id++) {
862 slots_num = (txq_id == trans_pcie->cmd_queue) ?
863 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
864 ret = iwl_pcie_txq_alloc(trans, &trans_pcie->txq[txq_id],
865 slots_num, txq_id);
866 if (ret) {
867 IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
868 goto error;
869 }
870 }
871
872 return 0;
873
874error:
875 iwl_pcie_tx_free(trans);
876
877 return ret;
878}
879int iwl_pcie_tx_init(struct iwl_trans *trans)
880{
881 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
882 int ret;
883 int txq_id, slots_num;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200884 bool alloc = false;
885
886 if (!trans_pcie->txq) {
887 ret = iwl_pcie_tx_alloc(trans);
888 if (ret)
889 goto error;
890 alloc = true;
891 }
892
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +0200893 spin_lock(&trans_pcie->irq_lock);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200894
895 /* Turn off all Tx DMA fifos */
896 iwl_write_prph(trans, SCD_TXFACT, 0);
897
898 /* Tell NIC where to find the "keep warm" buffer */
899 iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
900 trans_pcie->kw.dma >> 4);
901
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +0200902 spin_unlock(&trans_pcie->irq_lock);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200903
904 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
905 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
906 txq_id++) {
907 slots_num = (txq_id == trans_pcie->cmd_queue) ?
908 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
909 ret = iwl_pcie_txq_init(trans, &trans_pcie->txq[txq_id],
910 slots_num, txq_id);
911 if (ret) {
912 IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
913 goto error;
914 }
915 }
916
917 return 0;
918error:
919 /*Upon error, free only if we allocated something */
920 if (alloc)
921 iwl_pcie_tx_free(trans);
922 return ret;
923}
924
925static inline void iwl_pcie_txq_progress(struct iwl_trans_pcie *trans_pcie,
926 struct iwl_txq *txq)
927{
928 if (!trans_pcie->wd_timeout)
929 return;
930
931 /*
932 * if empty delete timer, otherwise move timer forward
933 * since we're making progress on this queue
934 */
935 if (txq->q.read_ptr == txq->q.write_ptr)
936 del_timer(&txq->stuck_timer);
937 else
938 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
939}
940
941/* Frees buffers until index _not_ inclusive */
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200942void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
943 struct sk_buff_head *skbs)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200944{
945 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
946 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
Johannes Berg83f32a42014-04-24 09:57:40 +0200947 int tfd_num = ssn & (TFD_QUEUE_SIZE_MAX - 1);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200948 struct iwl_queue *q = &txq->q;
949 int last_to_free;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200950
951 /* This function is not meant to release cmd queue*/
952 if (WARN_ON(txq_id == trans_pcie->cmd_queue))
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200953 return;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200954
Johannes Berg2bfb5092012-12-27 21:43:48 +0100955 spin_lock_bh(&txq->lock);
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200956
Emmanuel Grumbachb9676132013-06-13 11:45:59 +0300957 if (!txq->active) {
958 IWL_DEBUG_TX_QUEUES(trans, "Q %d inactive - ignoring idx %d\n",
959 txq_id, ssn);
960 goto out;
961 }
962
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200963 if (txq->q.read_ptr == tfd_num)
964 goto out;
965
966 IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
967 txq_id, txq->q.read_ptr, tfd_num, ssn);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200968
969 /*Since we free until index _not_ inclusive, the one before index is
970 * the last we will free. This one must be used */
Johannes Berg83f32a42014-04-24 09:57:40 +0200971 last_to_free = iwl_queue_dec_wrap(tfd_num);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200972
Emmanuel Grumbach6ca6ebc2012-11-14 23:38:08 +0200973 if (!iwl_queue_used(q, last_to_free)) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200974 IWL_ERR(trans,
975 "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
Johannes Berg83f32a42014-04-24 09:57:40 +0200976 __func__, txq_id, last_to_free, TFD_QUEUE_SIZE_MAX,
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200977 q->write_ptr, q->read_ptr);
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200978 goto out;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200979 }
980
981 if (WARN_ON(!skb_queue_empty(skbs)))
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200982 goto out;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200983
984 for (;
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200985 q->read_ptr != tfd_num;
Johannes Berg83f32a42014-04-24 09:57:40 +0200986 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr)) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200987
988 if (WARN_ON_ONCE(txq->entries[txq->q.read_ptr].skb == NULL))
989 continue;
990
991 __skb_queue_tail(skbs, txq->entries[txq->q.read_ptr].skb);
992
993 txq->entries[txq->q.read_ptr].skb = NULL;
994
995 iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq);
996
Johannes Berg98891752013-02-26 11:28:19 +0100997 iwl_pcie_txq_free_tfd(trans, txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200998 }
999
1000 iwl_pcie_txq_progress(trans_pcie, txq);
1001
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +02001002 if (iwl_queue_space(&txq->q) > txq->q.low_mark)
1003 iwl_wake_queue(trans, txq);
1004out:
Johannes Berg2bfb5092012-12-27 21:43:48 +01001005 spin_unlock_bh(&txq->lock);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001006}
1007
1008/*
1009 * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
1010 *
1011 * When FW advances 'R' index, all entries between old and new 'R' index
1012 * need to be reclaimed. As result, some free space forms. If there is
1013 * enough free space (> low mark), wake the stack that feeds us.
1014 */
1015static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
1016{
1017 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1018 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
1019 struct iwl_queue *q = &txq->q;
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001020 unsigned long flags;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001021 int nfreed = 0;
1022
1023 lockdep_assert_held(&txq->lock);
1024
Johannes Berg83f32a42014-04-24 09:57:40 +02001025 if ((idx >= TFD_QUEUE_SIZE_MAX) || (!iwl_queue_used(q, idx))) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001026 IWL_ERR(trans,
1027 "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
Johannes Berg83f32a42014-04-24 09:57:40 +02001028 __func__, txq_id, idx, TFD_QUEUE_SIZE_MAX,
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001029 q->write_ptr, q->read_ptr);
1030 return;
1031 }
1032
Johannes Berg83f32a42014-04-24 09:57:40 +02001033 for (idx = iwl_queue_inc_wrap(idx); q->read_ptr != idx;
1034 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr)) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001035
1036 if (nfreed++ > 0) {
1037 IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
1038 idx, q->write_ptr, q->read_ptr);
Emmanuel Grumbachcfadc3f2014-02-12 08:51:54 +02001039 iwl_write_prph(trans, DEVICE_SET_NMI_REG, 1);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001040 }
1041 }
1042
Emmanuel Grumbache7f76342014-03-25 10:00:31 +02001043 if (trans->cfg->base_params->apmg_wake_up_wa &&
1044 q->read_ptr == q->write_ptr) {
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001045 spin_lock_irqsave(&trans_pcie->reg_lock, flags);
1046 WARN_ON(!trans_pcie->cmd_in_flight);
1047 trans_pcie->cmd_in_flight = false;
1048 __iwl_trans_pcie_clear_bit(trans,
1049 CSR_GP_CNTRL,
1050 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1051 spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
1052 }
1053
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001054 iwl_pcie_txq_progress(trans_pcie, txq);
1055}
1056
1057static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001058 u16 txq_id)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001059{
Johannes Berg20d3b642012-05-16 22:54:29 +02001060 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001061 u32 tbl_dw_addr;
1062 u32 tbl_dw;
1063 u16 scd_q2ratid;
1064
1065 scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
1066
Emmanuel Grumbach105183b2011-08-25 23:11:02 -07001067 tbl_dw_addr = trans_pcie->scd_base_addr +
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001068 SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
1069
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001070 tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001071
1072 if (txq_id & 0x1)
1073 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
1074 else
1075 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
1076
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001077 iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001078
1079 return 0;
1080}
1081
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001082static inline void iwl_pcie_txq_set_inactive(struct iwl_trans *trans,
1083 u16 txq_id)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001084{
1085 /* Simply stop the queue, but don't change any configuration;
1086 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001087 iwl_write_prph(trans,
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001088 SCD_QUEUE_STATUS_BITS(txq_id),
1089 (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
1090 (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
1091}
1092
Emmanuel Grumbachbd5f6a32013-04-28 14:05:22 +03001093/* Receiver address (actually, Rx station's index into station table),
1094 * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
1095#define BUILD_RAxTID(sta_id, tid) (((sta_id) << 4) + (tid))
1096
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001097void iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, int fifo,
1098 int sta_id, int tid, int frame_limit, u16 ssn)
Johannes Berg70a18c52012-03-05 11:24:44 -08001099{
Johannes Berg9eae88f2012-03-15 13:26:52 -07001100 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001101
Johannes Berg9eae88f2012-03-15 13:26:52 -07001102 if (test_and_set_bit(txq_id, trans_pcie->queue_used))
1103 WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001104
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001105 /* Stop this Tx queue before configuring it */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001106 iwl_pcie_txq_set_inactive(trans, txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001107
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001108 /* Set this queue as a chain-building queue unless it is CMD queue */
1109 if (txq_id != trans_pcie->cmd_queue)
1110 iwl_set_bits_prph(trans, SCD_QUEUECHAIN_SEL, BIT(txq_id));
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001111
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001112 /* If this queue is mapped to a certain station: it is an AGG queue */
Emmanuel Grumbach881acd82013-03-19 16:16:00 +02001113 if (sta_id >= 0) {
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001114 u16 ra_tid = BUILD_RAxTID(sta_id, tid);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001115
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001116 /* Map receiver-address / traffic-ID to this queue */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001117 iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001118
1119 /* enable aggregations for the queue */
1120 iwl_set_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
Johannes Berg68972c42013-06-11 19:05:27 +02001121 trans_pcie->txq[txq_id].ampdu = true;
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001122 } else {
1123 /*
1124 * disable aggregations for the queue, this will also make the
1125 * ra_tid mapping configuration irrelevant since it is now a
1126 * non-AGG queue.
1127 */
1128 iwl_clear_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
Emmanuel Grumbachf4772522013-07-24 14:15:21 +03001129
1130 ssn = trans_pcie->txq[txq_id].q.read_ptr;
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001131 }
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001132
1133 /* Place first TFD at index corresponding to start sequence number.
1134 * Assumes that ssn_idx is valid (!= 0xFFF) */
Emmanuel Grumbach822e8b22011-11-21 13:25:31 +02001135 trans_pcie->txq[txq_id].q.read_ptr = (ssn & 0xff);
1136 trans_pcie->txq[txq_id].q.write_ptr = (ssn & 0xff);
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001137
1138 iwl_write_direct32(trans, HBUS_TARG_WRPTR,
1139 (ssn & 0xff) | (txq_id << 8));
1140 iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001141
1142 /* Set up Tx window size and frame limit for this queue */
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001143 iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001144 SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001145 iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
Johannes Berg9eae88f2012-03-15 13:26:52 -07001146 SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
1147 ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
1148 SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
1149 ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
1150 SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001151
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001152 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001153 iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
1154 (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1155 (fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
1156 (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
1157 SCD_QUEUE_STTS_REG_MSK);
Emmanuel Grumbachb9676132013-06-13 11:45:59 +03001158 trans_pcie->txq[txq_id].active = true;
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001159 IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d on FIFO %d WrPtr: %d\n",
1160 txq_id, fifo, ssn & 0xff);
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001161}
1162
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001163void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id)
Emmanuel Grumbach288712a2011-08-25 23:11:25 -07001164{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001165 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach986ea6c2012-09-30 16:25:43 +02001166 u32 stts_addr = trans_pcie->scd_base_addr +
1167 SCD_TX_STTS_QUEUE_OFFSET(txq_id);
1168 static const u32 zero_val[4] = {};
Emmanuel Grumbach288712a2011-08-25 23:11:25 -07001169
Emmanuel Grumbachfba1c622013-12-19 22:19:17 +02001170 /*
1171 * Upon HW Rfkill - we stop the device, and then stop the queues
1172 * in the op_mode. Just for the sake of the simplicity of the op_mode,
1173 * allow the op_mode to call txq_disable after it already called
1174 * stop_device.
1175 */
Johannes Berg9eae88f2012-03-15 13:26:52 -07001176 if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
Emmanuel Grumbachfba1c622013-12-19 22:19:17 +02001177 WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
1178 "queue %d not used", txq_id);
Johannes Berg9eae88f2012-03-15 13:26:52 -07001179 return;
Emmanuel Grumbachbc237732011-11-21 13:25:31 +02001180 }
1181
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001182 iwl_pcie_txq_set_inactive(trans, txq_id);
Emmanuel Grumbachac928f82012-10-14 16:36:36 +02001183
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001184 iwl_trans_write_mem(trans, stts_addr, (void *)zero_val,
1185 ARRAY_SIZE(zero_val));
Emmanuel Grumbach986ea6c2012-09-30 16:25:43 +02001186
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001187 iwl_pcie_txq_unmap(trans, txq_id);
Johannes Berg68972c42013-06-11 19:05:27 +02001188 trans_pcie->txq[txq_id].ampdu = false;
Emmanuel Grumbach6c3fd3f2012-10-18 12:38:37 +02001189
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001190 IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001191}
1192
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001193/*************** HOST COMMAND QUEUE FUNCTIONS *****/
1194
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001195/*
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001196 * iwl_pcie_enqueue_hcmd - enqueue a uCode command
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001197 * @priv: device private data point
Eliad Pellere89044d2013-07-16 17:33:26 +03001198 * @cmd: a pointer to the ucode command structure
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001199 *
Eliad Pellere89044d2013-07-16 17:33:26 +03001200 * The function returns < 0 values to indicate the operation
1201 * failed. On success, it returns the index (>= 0) of command in the
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001202 * command queue.
1203 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001204static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
1205 struct iwl_host_cmd *cmd)
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001206{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001207 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001208 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001209 struct iwl_queue *q = &txq->q;
Johannes Bergc2acea82009-07-24 11:13:05 -07001210 struct iwl_device_cmd *out_cmd;
1211 struct iwl_cmd_meta *out_meta;
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001212 unsigned long flags;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001213 void *dup_buf = NULL;
Tomas Winklerf3674222008-08-04 16:00:44 +08001214 dma_addr_t phys_addr;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001215 int idx;
Johannes Berg38c0f3342013-02-27 13:18:50 +01001216 u16 copy_size, cmd_size, scratch_size;
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001217 bool had_nocopy = false;
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001218 int i, ret;
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001219 u32 cmd_pos;
Johannes Berg1afbfb62013-02-26 11:32:26 +01001220 const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
1221 u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001222
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001223 copy_size = sizeof(out_cmd->hdr);
1224 cmd_size = sizeof(out_cmd->hdr);
1225
1226 /* need one for the header if the first is NOCOPY */
Johannes Berg1afbfb62013-02-26 11:32:26 +01001227 BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001228
Johannes Berg1afbfb62013-02-26 11:32:26 +01001229 for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
Johannes Berg8a964f42013-02-25 16:01:34 +01001230 cmddata[i] = cmd->data[i];
1231 cmdlen[i] = cmd->len[i];
1232
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001233 if (!cmd->len[i])
1234 continue;
Johannes Berg8a964f42013-02-25 16:01:34 +01001235
Johannes Berg38c0f3342013-02-27 13:18:50 +01001236 /* need at least IWL_HCMD_SCRATCHBUF_SIZE copied */
1237 if (copy_size < IWL_HCMD_SCRATCHBUF_SIZE) {
1238 int copy = IWL_HCMD_SCRATCHBUF_SIZE - copy_size;
Johannes Berg8a964f42013-02-25 16:01:34 +01001239
1240 if (copy > cmdlen[i])
1241 copy = cmdlen[i];
1242 cmdlen[i] -= copy;
1243 cmddata[i] += copy;
1244 copy_size += copy;
1245 }
1246
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001247 if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
1248 had_nocopy = true;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001249 if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
1250 idx = -EINVAL;
1251 goto free_dup_buf;
1252 }
1253 } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
1254 /*
1255 * This is also a chunk that isn't copied
1256 * to the static buffer so set had_nocopy.
1257 */
1258 had_nocopy = true;
1259
1260 /* only allowed once */
1261 if (WARN_ON(dup_buf)) {
1262 idx = -EINVAL;
1263 goto free_dup_buf;
1264 }
1265
Johannes Berg8a964f42013-02-25 16:01:34 +01001266 dup_buf = kmemdup(cmddata[i], cmdlen[i],
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001267 GFP_ATOMIC);
1268 if (!dup_buf)
1269 return -ENOMEM;
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001270 } else {
1271 /* NOCOPY must not be followed by normal! */
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001272 if (WARN_ON(had_nocopy)) {
1273 idx = -EINVAL;
1274 goto free_dup_buf;
1275 }
Johannes Berg8a964f42013-02-25 16:01:34 +01001276 copy_size += cmdlen[i];
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001277 }
1278 cmd_size += cmd->len[i];
1279 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001280
Johannes Berg3e41ace2011-04-18 09:12:37 -07001281 /*
1282 * If any of the command structures end up being larger than
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001283 * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
1284 * allocated into separate TFDs, then we will need to
1285 * increase the size of the buffers.
Johannes Berg3e41ace2011-04-18 09:12:37 -07001286 */
Johannes Berg2a79e452012-09-26 13:32:13 +02001287 if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
1288 "Command %s (%#x) is too large (%d bytes)\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001289 get_cmd_string(trans_pcie, cmd->id), cmd->id, copy_size)) {
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001290 idx = -EINVAL;
1291 goto free_dup_buf;
1292 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001293
Johannes Berg015c15e2012-03-05 11:24:24 -08001294 spin_lock_bh(&txq->lock);
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001295
Johannes Bergc2acea82009-07-24 11:13:05 -07001296 if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
Johannes Berg015c15e2012-03-05 11:24:24 -08001297 spin_unlock_bh(&txq->lock);
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001298
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001299 IWL_ERR(trans, "No space in command queue\n");
Johannes Berg0e781842012-03-06 13:30:49 -08001300 iwl_op_mode_cmd_queue_full(trans->op_mode);
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001301 idx = -ENOSPC;
1302 goto free_dup_buf;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001303 }
1304
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001305 idx = get_cmd_index(q, q->write_ptr);
Johannes Bergbf8440e2012-03-19 17:12:06 +01001306 out_cmd = txq->entries[idx].cmd;
1307 out_meta = &txq->entries[idx].meta;
Johannes Bergc2acea82009-07-24 11:13:05 -07001308
Daniel C Halperin8ce73f32009-07-31 14:28:06 -07001309 memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
Johannes Bergc2acea82009-07-24 11:13:05 -07001310 if (cmd->flags & CMD_WANT_SKB)
1311 out_meta->source = cmd;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001312
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001313 /* set up the header */
1314
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001315 out_cmd->hdr.cmd = cmd->id;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001316 out_cmd->hdr.flags = 0;
Emmanuel Grumbachcefeaa52011-08-25 23:10:40 -07001317 out_cmd->hdr.sequence =
Meenakshi Venkataramanc6f600f2012-03-08 11:29:12 -08001318 cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
Emmanuel Grumbachcefeaa52011-08-25 23:10:40 -07001319 INDEX_TO_SEQ(q->write_ptr));
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001320
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001321 /* and copy the data that needs to be copied */
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001322 cmd_pos = offsetof(struct iwl_device_cmd, payload);
Johannes Berg8a964f42013-02-25 16:01:34 +01001323 copy_size = sizeof(out_cmd->hdr);
Johannes Berg1afbfb62013-02-26 11:32:26 +01001324 for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
Johannes Berg8a964f42013-02-25 16:01:34 +01001325 int copy = 0;
1326
Emmanuel Grumbachcc904c72013-03-14 08:35:06 +02001327 if (!cmd->len[i])
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001328 continue;
Johannes Berg8a964f42013-02-25 16:01:34 +01001329
Johannes Berg38c0f3342013-02-27 13:18:50 +01001330 /* need at least IWL_HCMD_SCRATCHBUF_SIZE copied */
1331 if (copy_size < IWL_HCMD_SCRATCHBUF_SIZE) {
1332 copy = IWL_HCMD_SCRATCHBUF_SIZE - copy_size;
Johannes Berg8a964f42013-02-25 16:01:34 +01001333
1334 if (copy > cmd->len[i])
1335 copy = cmd->len[i];
1336 }
1337
1338 /* copy everything if not nocopy/dup */
1339 if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1340 IWL_HCMD_DFL_DUP)))
1341 copy = cmd->len[i];
1342
1343 if (copy) {
1344 memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1345 cmd_pos += copy;
1346 copy_size += copy;
1347 }
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001348 }
1349
Johannes Bergd9fb6462012-03-26 08:23:39 -07001350 IWL_DEBUG_HC(trans,
Johannes Berg20d3b642012-05-16 22:54:29 +02001351 "Sending command %s (#%x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001352 get_cmd_string(trans_pcie, out_cmd->hdr.cmd),
Johannes Berg20d3b642012-05-16 22:54:29 +02001353 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
1354 cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001355
Johannes Berg38c0f3342013-02-27 13:18:50 +01001356 /* start the TFD with the scratchbuf */
1357 scratch_size = min_t(int, copy_size, IWL_HCMD_SCRATCHBUF_SIZE);
1358 memcpy(&txq->scratchbufs[q->write_ptr], &out_cmd->hdr, scratch_size);
1359 iwl_pcie_txq_build_tfd(trans, txq,
1360 iwl_pcie_get_scratchbuf_dma(txq, q->write_ptr),
Johannes Berg6d6e68f2014-04-23 19:00:56 +02001361 scratch_size, true);
Johannes Berg8a964f42013-02-25 16:01:34 +01001362
Johannes Berg38c0f3342013-02-27 13:18:50 +01001363 /* map first command fragment, if any remains */
1364 if (copy_size > scratch_size) {
1365 phys_addr = dma_map_single(trans->dev,
1366 ((u8 *)&out_cmd->hdr) + scratch_size,
1367 copy_size - scratch_size,
1368 DMA_TO_DEVICE);
1369 if (dma_mapping_error(trans->dev, phys_addr)) {
1370 iwl_pcie_tfd_unmap(trans, out_meta,
1371 &txq->tfds[q->write_ptr]);
1372 idx = -ENOMEM;
1373 goto out;
1374 }
1375
1376 iwl_pcie_txq_build_tfd(trans, txq, phys_addr,
Johannes Berg6d6e68f2014-04-23 19:00:56 +02001377 copy_size - scratch_size, false);
Johannes Berg2c46f722011-04-28 07:27:10 -07001378 }
1379
Johannes Berg8a964f42013-02-25 16:01:34 +01001380 /* map the remaining (adjusted) nocopy/dup fragments */
Johannes Berg1afbfb62013-02-26 11:32:26 +01001381 for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
Johannes Berg8a964f42013-02-25 16:01:34 +01001382 const void *data = cmddata[i];
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001383
Johannes Berg8a964f42013-02-25 16:01:34 +01001384 if (!cmdlen[i])
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001385 continue;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001386 if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1387 IWL_HCMD_DFL_DUP)))
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001388 continue;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001389 if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
1390 data = dup_buf;
1391 phys_addr = dma_map_single(trans->dev, (void *)data,
Johannes Berg98891752013-02-26 11:28:19 +01001392 cmdlen[i], DMA_TO_DEVICE);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001393 if (dma_mapping_error(trans->dev, phys_addr)) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001394 iwl_pcie_tfd_unmap(trans, out_meta,
Johannes Berg98891752013-02-26 11:28:19 +01001395 &txq->tfds[q->write_ptr]);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001396 idx = -ENOMEM;
1397 goto out;
1398 }
1399
Johannes Berg6d6e68f2014-04-23 19:00:56 +02001400 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], false);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001401 }
Reinette Chatredf833b12009-04-21 10:55:48 -07001402
Emmanuel Grumbachafaf6b52011-07-08 08:46:09 -07001403 out_meta->flags = cmd->flags;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001404 if (WARN_ON_ONCE(txq->entries[idx].free_buf))
1405 kfree(txq->entries[idx].free_buf);
1406 txq->entries[idx].free_buf = dup_buf;
Johannes Berg2c46f722011-04-28 07:27:10 -07001407
Johannes Berg8a964f42013-02-25 16:01:34 +01001408 trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr);
Reinette Chatredf833b12009-04-21 10:55:48 -07001409
Johannes Berg7c5ba4a2012-04-09 17:46:54 -07001410 /* start timer if queue currently empty */
1411 if (q->read_ptr == q->write_ptr && trans_pcie->wd_timeout)
1412 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
1413
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001414 spin_lock_irqsave(&trans_pcie->reg_lock, flags);
1415
1416 /*
1417 * wake up the NIC to make sure that the firmware will see the host
1418 * command - we will let the NIC sleep once all the host commands
Emmanuel Grumbache7f76342014-03-25 10:00:31 +02001419 * returned. This needs to be done only on NICs that have
1420 * apmg_wake_up_wa set.
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001421 */
Emmanuel Grumbache7f76342014-03-25 10:00:31 +02001422 if (trans->cfg->base_params->apmg_wake_up_wa &&
1423 !trans_pcie->cmd_in_flight) {
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001424 trans_pcie->cmd_in_flight = true;
1425 __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
1426 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1427 ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
1428 CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
1429 (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
1430 CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP),
1431 15000);
1432 if (ret < 0) {
1433 __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
1434 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
1435 spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
1436 trans_pcie->cmd_in_flight = false;
1437 idx = -EIO;
1438 goto out;
1439 }
1440 }
1441
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001442 /* Increment and update queue's write index */
Johannes Berg83f32a42014-04-24 09:57:40 +02001443 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001444 iwl_pcie_txq_inc_wr_ptr(trans, txq);
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001445
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001446 spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
1447
Johannes Berg2c46f722011-04-28 07:27:10 -07001448 out:
Johannes Berg015c15e2012-03-05 11:24:24 -08001449 spin_unlock_bh(&txq->lock);
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001450 free_dup_buf:
1451 if (idx < 0)
1452 kfree(dup_buf);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001453 return idx;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001454}
1455
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001456/*
1457 * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
Tomas Winkler17b88922008-05-29 16:35:12 +08001458 * @rxb: Rx buffer to reclaim
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001459 * @handler_status: return value of the handler of the command
1460 * (put in setup_rx_handlers)
Tomas Winkler17b88922008-05-29 16:35:12 +08001461 *
1462 * If an Rx buffer has an async callback associated with it the callback
1463 * will be executed. The attached skb (if present) will only be freed
1464 * if the callback returns 1
1465 */
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001466void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
1467 struct iwl_rx_cmd_buffer *rxb, int handler_status)
Tomas Winkler17b88922008-05-29 16:35:12 +08001468{
Zhu Yi2f301222009-10-09 17:19:45 +08001469 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler17b88922008-05-29 16:35:12 +08001470 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1471 int txq_id = SEQ_TO_QUEUE(sequence);
1472 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler17b88922008-05-29 16:35:12 +08001473 int cmd_index;
Johannes Bergc2acea82009-07-24 11:13:05 -07001474 struct iwl_device_cmd *cmd;
1475 struct iwl_cmd_meta *meta;
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001476 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001477 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
Tomas Winkler17b88922008-05-29 16:35:12 +08001478
1479 /* If a Tx command is being handled and it isn't in the actual
1480 * command queue then there a command routing bug has been introduced
1481 * in the queue management code. */
Meenakshi Venkataramanc6f600f2012-03-08 11:29:12 -08001482 if (WARN(txq_id != trans_pcie->cmd_queue,
Johannes Berg13bb9482010-08-23 10:46:33 +02001483 "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
Johannes Berg20d3b642012-05-16 22:54:29 +02001484 txq_id, trans_pcie->cmd_queue, sequence,
1485 trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr,
1486 trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) {
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -07001487 iwl_print_hex_error(trans, pkt, 32);
Johannes Berg55d6a3c2008-09-23 19:18:43 +02001488 return;
Winkler, Tomas01ef93232008-11-07 09:58:45 -08001489 }
Tomas Winkler17b88922008-05-29 16:35:12 +08001490
Johannes Berg2bfb5092012-12-27 21:43:48 +01001491 spin_lock_bh(&txq->lock);
Johannes Berg015c15e2012-03-05 11:24:24 -08001492
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001493 cmd_index = get_cmd_index(&txq->q, index);
Johannes Bergbf8440e2012-03-19 17:12:06 +01001494 cmd = txq->entries[cmd_index].cmd;
1495 meta = &txq->entries[cmd_index].meta;
Tomas Winkler17b88922008-05-29 16:35:12 +08001496
Johannes Berg98891752013-02-26 11:28:19 +01001497 iwl_pcie_tfd_unmap(trans, meta, &txq->tfds[index]);
Reinette Chatrec33de622009-10-30 14:36:10 -07001498
Tomas Winkler17b88922008-05-29 16:35:12 +08001499 /* Input error checking is done when commands are added to queue. */
Johannes Bergc2acea82009-07-24 11:13:05 -07001500 if (meta->flags & CMD_WANT_SKB) {
Johannes Berg48a2d662012-03-05 11:24:39 -08001501 struct page *p = rxb_steal_page(rxb);
Stanislaw Gruszka2624e962011-04-20 16:02:58 +02001502
Johannes Berg65b94a42012-03-05 11:24:38 -08001503 meta->source->resp_pkt = pkt;
1504 meta->source->_rx_page_addr = (unsigned long)page_address(p);
Johannes Bergb2cf4102012-04-09 17:46:51 -07001505 meta->source->_rx_page_order = trans_pcie->rx_page_order;
Johannes Berg65b94a42012-03-05 11:24:38 -08001506 meta->source->handler_status = handler_status;
Stanislaw Gruszka2624e962011-04-20 16:02:58 +02001507 }
Tomas Winkler17b88922008-05-29 16:35:12 +08001508
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001509 iwl_pcie_cmdq_reclaim(trans, txq_id, index);
Tomas Winkler17b88922008-05-29 16:35:12 +08001510
Johannes Bergc2acea82009-07-24 11:13:05 -07001511 if (!(meta->flags & CMD_ASYNC)) {
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001512 if (!test_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status)) {
Wey-Yi Guy05c89b92011-10-10 07:26:48 -07001513 IWL_WARN(trans,
1514 "HCMD_ACTIVE already clear for command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001515 get_cmd_string(trans_pcie, cmd->hdr.cmd));
Wey-Yi Guy05c89b92011-10-10 07:26:48 -07001516 }
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001517 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001518 IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001519 get_cmd_string(trans_pcie, cmd->hdr.cmd));
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001520 wake_up(&trans_pcie->wait_command_queue);
Tomas Winkler17b88922008-05-29 16:35:12 +08001521 }
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001522
Zhu Yidd487442010-03-22 02:28:41 -07001523 meta->flags = 0;
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001524
Johannes Berg2bfb5092012-12-27 21:43:48 +01001525 spin_unlock_bh(&txq->lock);
Tomas Winkler17b88922008-05-29 16:35:12 +08001526}
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001527
Johannes Berg9439eac2013-10-09 09:59:25 +02001528#define HOST_COMPLETE_TIMEOUT (2 * HZ)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001529
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001530static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
1531 struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001532{
Johannes Bergd9fb6462012-03-26 08:23:39 -07001533 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001534 int ret;
1535
1536 /* An asynchronous command can not expect an SKB to be set. */
1537 if (WARN_ON(cmd->flags & CMD_WANT_SKB))
1538 return -EINVAL;
1539
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001540 ret = iwl_pcie_enqueue_hcmd(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001541 if (ret < 0) {
Johannes Berg721c32f2012-03-06 13:30:40 -08001542 IWL_ERR(trans,
Todd Previteb36b1102011-11-10 06:55:02 -08001543 "Error sending %s: enqueue_hcmd failed: %d\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001544 get_cmd_string(trans_pcie, cmd->id), ret);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001545 return ret;
1546 }
1547 return 0;
1548}
1549
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001550static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
1551 struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001552{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001553 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001554 int cmd_idx;
1555 int ret;
1556
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001557 IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001558 get_cmd_string(trans_pcie, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001559
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001560 if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE,
1561 &trans->status),
Johannes Bergbcbb8c92013-10-28 15:50:55 +01001562 "Command %s: a command is already active!\n",
1563 get_cmd_string(trans_pcie, cmd->id)))
Johannes Berg2cc39c92012-03-06 13:30:41 -08001564 return -EIO;
Johannes Berg2cc39c92012-03-06 13:30:41 -08001565
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001566 IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001567 get_cmd_string(trans_pcie, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001568
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001569 cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001570 if (cmd_idx < 0) {
1571 ret = cmd_idx;
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001572 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
Johannes Berg721c32f2012-03-06 13:30:40 -08001573 IWL_ERR(trans,
Todd Previteb36b1102011-11-10 06:55:02 -08001574 "Error sending %s: enqueue_hcmd failed: %d\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001575 get_cmd_string(trans_pcie, cmd->id), ret);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001576 return ret;
1577 }
1578
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001579 ret = wait_event_timeout(trans_pcie->wait_command_queue,
1580 !test_bit(STATUS_SYNC_HCMD_ACTIVE,
1581 &trans->status),
1582 HOST_COMPLETE_TIMEOUT);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001583 if (!ret) {
Johannes Berg6dde8c42013-10-31 18:30:38 +01001584 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
1585 struct iwl_queue *q = &txq->q;
Wey-Yi Guyd10630a2011-10-10 07:26:46 -07001586
Johannes Berg6dde8c42013-10-31 18:30:38 +01001587 IWL_ERR(trans, "Error sending %s: time out after %dms.\n",
1588 get_cmd_string(trans_pcie, cmd->id),
1589 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001590
Johannes Berg6dde8c42013-10-31 18:30:38 +01001591 IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n",
1592 q->read_ptr, q->write_ptr);
Wey-Yi Guyd10630a2011-10-10 07:26:46 -07001593
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001594 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
Johannes Berg6dde8c42013-10-31 18:30:38 +01001595 IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
1596 get_cmd_string(trans_pcie, cmd->id));
1597 ret = -ETIMEDOUT;
Emmanuel Grumbach42550a52013-09-11 14:16:20 +03001598
Emmanuel Grumbachcfadc3f2014-02-12 08:51:54 +02001599 iwl_write_prph(trans, DEVICE_SET_NMI_REG, 1);
Arik Nemtsov2a988e92013-12-01 13:50:40 +02001600 iwl_trans_fw_error(trans);
Emmanuel Grumbach42550a52013-09-11 14:16:20 +03001601
Johannes Berg6dde8c42013-10-31 18:30:38 +01001602 goto cancel;
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001603 }
1604
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001605 if (test_bit(STATUS_FW_ERROR, &trans->status)) {
Johannes Bergd18aa872012-11-06 16:36:21 +01001606 IWL_ERR(trans, "FW error in SYNC CMD %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001607 get_cmd_string(trans_pcie, cmd->id));
Johannes Bergb656fa32013-05-03 11:56:17 +02001608 dump_stack();
Johannes Bergd18aa872012-11-06 16:36:21 +01001609 ret = -EIO;
1610 goto cancel;
1611 }
1612
Eran Harary1094fa22013-06-02 12:40:34 +03001613 if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001614 test_bit(STATUS_RFKILL, &trans->status)) {
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001615 IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
1616 ret = -ERFKILL;
1617 goto cancel;
1618 }
1619
Johannes Berg65b94a42012-03-05 11:24:38 -08001620 if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001621 IWL_ERR(trans, "Error: Response NULL in '%s'\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001622 get_cmd_string(trans_pcie, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001623 ret = -EIO;
1624 goto cancel;
1625 }
1626
1627 return 0;
1628
1629cancel:
1630 if (cmd->flags & CMD_WANT_SKB) {
1631 /*
1632 * Cancel the CMD_WANT_SKB flag for the cmd in the
1633 * TX cmd queue. Otherwise in case the cmd comes
1634 * in later, it will possibly set an invalid
1635 * address (cmd->meta.source).
1636 */
Johannes Bergbf8440e2012-03-19 17:12:06 +01001637 trans_pcie->txq[trans_pcie->cmd_queue].
1638 entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001639 }
Emmanuel Grumbach9cac4942011-11-10 06:55:20 -08001640
Johannes Berg65b94a42012-03-05 11:24:38 -08001641 if (cmd->resp_pkt) {
1642 iwl_free_resp(cmd);
1643 cmd->resp_pkt = NULL;
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001644 }
1645
1646 return ret;
1647}
1648
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001649int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001650{
Eran Harary4f593342013-05-13 07:53:26 +03001651 if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001652 test_bit(STATUS_RFKILL, &trans->status)) {
Emmanuel Grumbach754d7d92013-03-13 22:16:20 +02001653 IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
1654 cmd->id);
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001655 return -ERFKILL;
Emmanuel Grumbach754d7d92013-03-13 22:16:20 +02001656 }
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001657
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001658 if (cmd->flags & CMD_ASYNC)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001659 return iwl_pcie_send_hcmd_async(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001660
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001661 /* We still can fail on RFKILL that can be asserted while we wait */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001662 return iwl_pcie_send_hcmd_sync(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001663}
1664
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001665int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
1666 struct iwl_device_cmd *dev_cmd, int txq_id)
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001667{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001668 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001669 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1670 struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
1671 struct iwl_cmd_meta *out_meta;
1672 struct iwl_txq *txq;
1673 struct iwl_queue *q;
Johannes Berg38c0f3342013-02-27 13:18:50 +01001674 dma_addr_t tb0_phys, tb1_phys, scratch_phys;
1675 void *tb1_addr;
1676 u16 len, tb1_len, tb2_len;
Johannes Bergea68f462014-02-27 14:36:55 +01001677 bool wait_write_ptr;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001678 __le16 fc = hdr->frame_control;
1679 u8 hdr_len = ieee80211_hdrlen(fc);
Johannes Berg68972c42013-06-11 19:05:27 +02001680 u16 wifi_seq;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001681
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001682 txq = &trans_pcie->txq[txq_id];
1683 q = &txq->q;
Emmanuel Grumbach39644e92011-09-15 11:46:29 -07001684
Johannes Berg961de6a2013-07-04 18:00:08 +02001685 if (WARN_ONCE(!test_bit(txq_id, trans_pcie->queue_used),
1686 "TX on unused queue %d\n", txq_id))
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001687 return -EINVAL;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001688
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001689 spin_lock(&txq->lock);
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001690
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001691 /* In AGG mode, the index in the ring must correspond to the WiFi
1692 * sequence number. This is a HW requirements to help the SCD to parse
1693 * the BA.
1694 * Check here that the packets are in the right place on the ring.
1695 */
Johannes Berg9a886582013-02-15 19:25:00 +01001696 wifi_seq = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
Eliad Peller1092b9b2013-07-16 17:53:43 +03001697 WARN_ONCE(txq->ampdu &&
Johannes Berg68972c42013-06-11 19:05:27 +02001698 (wifi_seq & 0xff) != q->write_ptr,
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001699 "Q: %d WiFi Seq %d tfdNum %d",
1700 txq_id, wifi_seq, q->write_ptr);
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001701
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001702 /* Set up driver data for this TFD */
1703 txq->entries[q->write_ptr].skb = skb;
1704 txq->entries[q->write_ptr].cmd = dev_cmd;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001705
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001706 dev_cmd->hdr.sequence =
1707 cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
1708 INDEX_TO_SEQ(q->write_ptr)));
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001709
Johannes Berg38c0f3342013-02-27 13:18:50 +01001710 tb0_phys = iwl_pcie_get_scratchbuf_dma(txq, q->write_ptr);
1711 scratch_phys = tb0_phys + sizeof(struct iwl_cmd_header) +
1712 offsetof(struct iwl_tx_cmd, scratch);
1713
1714 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1715 tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
1716
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001717 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1718 out_meta = &txq->entries[q->write_ptr].meta;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001719
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001720 /*
Johannes Berg38c0f3342013-02-27 13:18:50 +01001721 * The second TB (tb1) points to the remainder of the TX command
1722 * and the 802.11 header - dword aligned size
1723 * (This calculation modifies the TX command, so do it before the
1724 * setup of the first TB)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001725 */
Johannes Berg38c0f3342013-02-27 13:18:50 +01001726 len = sizeof(struct iwl_tx_cmd) + sizeof(struct iwl_cmd_header) +
1727 hdr_len - IWL_HCMD_SCRATCHBUF_SIZE;
Eliad Peller1092b9b2013-07-16 17:53:43 +03001728 tb1_len = ALIGN(len, 4);
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001729
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001730 /* Tell NIC about any 2-byte padding after MAC header */
Johannes Berg38c0f3342013-02-27 13:18:50 +01001731 if (tb1_len != len)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001732 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1733
Johannes Berg38c0f3342013-02-27 13:18:50 +01001734 /* The first TB points to the scratchbuf data - min_copy bytes */
1735 memcpy(&txq->scratchbufs[q->write_ptr], &dev_cmd->hdr,
1736 IWL_HCMD_SCRATCHBUF_SIZE);
1737 iwl_pcie_txq_build_tfd(trans, txq, tb0_phys,
Johannes Berg6d6e68f2014-04-23 19:00:56 +02001738 IWL_HCMD_SCRATCHBUF_SIZE, true);
Johannes Berg38c0f3342013-02-27 13:18:50 +01001739
1740 /* there must be data left over for TB1 or this code must be changed */
1741 BUILD_BUG_ON(sizeof(struct iwl_tx_cmd) < IWL_HCMD_SCRATCHBUF_SIZE);
1742
1743 /* map the data for TB1 */
1744 tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_HCMD_SCRATCHBUF_SIZE;
1745 tb1_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
1746 if (unlikely(dma_mapping_error(trans->dev, tb1_phys)))
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001747 goto out_err;
Johannes Berg6d6e68f2014-04-23 19:00:56 +02001748 iwl_pcie_txq_build_tfd(trans, txq, tb1_phys, tb1_len, false);
Johannes Berg38c0f3342013-02-27 13:18:50 +01001749
1750 /*
1751 * Set up TFD's third entry to point directly to remainder
1752 * of skb, if any (802.11 null frames have no payload).
1753 */
1754 tb2_len = skb->len - hdr_len;
1755 if (tb2_len > 0) {
1756 dma_addr_t tb2_phys = dma_map_single(trans->dev,
1757 skb->data + hdr_len,
1758 tb2_len, DMA_TO_DEVICE);
1759 if (unlikely(dma_mapping_error(trans->dev, tb2_phys))) {
1760 iwl_pcie_tfd_unmap(trans, out_meta,
1761 &txq->tfds[q->write_ptr]);
1762 goto out_err;
1763 }
Johannes Berg6d6e68f2014-04-23 19:00:56 +02001764 iwl_pcie_txq_build_tfd(trans, txq, tb2_phys, tb2_len, false);
Johannes Berg38c0f3342013-02-27 13:18:50 +01001765 }
1766
1767 /* Set up entry for this TFD in Tx byte-count array */
1768 iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len));
1769
1770 trace_iwlwifi_dev_tx(trans->dev, skb,
1771 &txq->tfds[txq->q.write_ptr],
1772 sizeof(struct iwl_tfd),
1773 &dev_cmd->hdr, IWL_HCMD_SCRATCHBUF_SIZE + tb1_len,
1774 skb->data + hdr_len, tb2_len);
1775 trace_iwlwifi_dev_tx_data(trans->dev, skb,
1776 skb->data + hdr_len, tb2_len);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001777
Johannes Bergea68f462014-02-27 14:36:55 +01001778 wait_write_ptr = ieee80211_has_morefrags(fc);
Johannes Berg7c5ba4a2012-04-09 17:46:54 -07001779
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001780 /* start timer if queue currently empty */
1781 if (txq->need_update && q->read_ptr == q->write_ptr &&
1782 trans_pcie->wd_timeout)
1783 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
1784
1785 /* Tell device the write index *just past* this latest filled TFD */
Johannes Berg83f32a42014-04-24 09:57:40 +02001786 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr);
Johannes Bergea68f462014-02-27 14:36:55 +01001787 if (!wait_write_ptr)
1788 iwl_pcie_txq_inc_wr_ptr(trans, txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001789
1790 /*
1791 * At this point the frame is "transmitted" successfully
Johannes Berg43aa6162014-02-27 14:24:36 +01001792 * and we will get a TX status notification eventually.
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001793 */
1794 if (iwl_queue_space(q) < q->high_mark) {
Johannes Bergea68f462014-02-27 14:36:55 +01001795 if (wait_write_ptr)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001796 iwl_pcie_txq_inc_wr_ptr(trans, txq);
Johannes Bergea68f462014-02-27 14:36:55 +01001797 else
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001798 iwl_stop_queue(trans, txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001799 }
1800 spin_unlock(&txq->lock);
1801 return 0;
1802out_err:
1803 spin_unlock(&txq->lock);
1804 return -1;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001805}