blob: b4b925ed5267c50585086325c6c14c4dca127581 [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.
Luciano Coelho4cbb8e502015-08-18 16:02:38 +03004 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
5 * Copyright(c) 2016 Intel Deutschland GmbH
Ron Rindjunsky1053d352008-05-05 10:22:43 +08006 *
7 * Portions of this file are derived from the ipw3945 project, as well
8 * as portions of the ieee80211 subsystem header files.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
22 *
23 * The full GNU General Public License is included in this distribution in the
24 * file called LICENSE.
25 *
26 * Contact Information:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020027 * Intel Linux Wireless <linuxwifi@intel.com>
Ron Rindjunsky1053d352008-05-05 10:22:43 +080028 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
29 *
30 *****************************************************************************/
Tomas Winklerfd4abac2008-05-15 13:54:07 +080031#include <linux/etherdevice.h>
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +030032#include <linux/ieee80211.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Emmanuel Grumbach253a6342011-07-11 07:39:46 -070034#include <linux/sched.h>
Luca Coelho71b12302016-03-11 12:12:16 +020035#include <linux/pm_runtime.h>
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +030036#include <net/ip6_checksum.h>
37#include <net/tso.h>
Emmanuel Grumbach253a6342011-07-11 07:39:46 -070038
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070039#include "iwl-debug.h"
40#include "iwl-csr.h"
41#include "iwl-prph.h"
Ron Rindjunsky1053d352008-05-05 10:22:43 +080042#include "iwl-io.h"
Avri Altman680073b2014-07-14 09:40:27 +030043#include "iwl-scd.h"
Emmanuel Grumbached277c92012-02-09 16:08:15 +020044#include "iwl-op-mode.h"
Johannes Berg6468a012012-05-16 19:13:54 +020045#include "internal.h"
Johannes Berg6238b002012-04-02 15:04:33 +020046/* FIXME: need to abstract out TX command (once we know what it looks like) */
Johannes Berg1023fdc2012-05-15 12:16:34 +020047#include "dvm/commands.h"
Ron Rindjunsky1053d352008-05-05 10:22:43 +080048
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070049#define IWL_TX_CRC_SIZE 4
50#define IWL_TX_DELIMITER_SIZE 4
51
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +020052/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
53 * DMA services
54 *
55 * Theory of operation
56 *
57 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
58 * of buffer descriptors, each of which points to one or more data buffers for
59 * the device to read from or fill. Driver and device exchange status of each
60 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
61 * entries in each circular buffer, to protect against confusing empty and full
62 * queue states.
63 *
64 * The device reads or writes the data in the queues via the device's several
65 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
66 *
67 * For Tx queue, there are low mark and high mark limits. If, after queuing
68 * the packet for Tx, free space become < low mark, Tx queue stopped. When
69 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
70 * Tx queue resumed.
71 *
72 ***************************************************/
Sara Sharone22744a2016-06-22 17:23:34 +030073
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +020074static int iwl_queue_space(const struct iwl_queue *q)
75{
Ido Yariva9b29242013-07-15 11:51:48 -040076 unsigned int max;
77 unsigned int used;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +020078
Ido Yariva9b29242013-07-15 11:51:48 -040079 /*
80 * To avoid ambiguity between empty and completely full queues, there
Johannes Berg83f32a42014-04-24 09:57:40 +020081 * should always be less than TFD_QUEUE_SIZE_MAX elements in the queue.
82 * If q->n_window is smaller than TFD_QUEUE_SIZE_MAX, there is no need
83 * to reserve any queue entries for this purpose.
Ido Yariva9b29242013-07-15 11:51:48 -040084 */
Johannes Berg83f32a42014-04-24 09:57:40 +020085 if (q->n_window < TFD_QUEUE_SIZE_MAX)
Ido Yariva9b29242013-07-15 11:51:48 -040086 max = q->n_window;
87 else
Johannes Berg83f32a42014-04-24 09:57:40 +020088 max = TFD_QUEUE_SIZE_MAX - 1;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +020089
Ido Yariva9b29242013-07-15 11:51:48 -040090 /*
Johannes Berg83f32a42014-04-24 09:57:40 +020091 * TFD_QUEUE_SIZE_MAX is a power of 2, so the following is equivalent to
92 * modulo by TFD_QUEUE_SIZE_MAX and is well defined.
Ido Yariva9b29242013-07-15 11:51:48 -040093 */
Johannes Berg83f32a42014-04-24 09:57:40 +020094 used = (q->write_ptr - q->read_ptr) & (TFD_QUEUE_SIZE_MAX - 1);
Ido Yariva9b29242013-07-15 11:51:48 -040095
96 if (WARN_ON(used > max))
97 return 0;
98
99 return max - used;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200100}
101
102/*
103 * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
104 */
Johannes Berg83f32a42014-04-24 09:57:40 +0200105static int iwl_queue_init(struct iwl_queue *q, int slots_num, u32 id)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200106{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200107 q->n_window = slots_num;
108 q->id = id;
109
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200110 /* slots_num must be power-of-two size, otherwise
111 * get_cmd_index is broken. */
112 if (WARN_ON(!is_power_of_2(slots_num)))
113 return -EINVAL;
114
115 q->low_mark = q->n_window / 4;
116 if (q->low_mark < 4)
117 q->low_mark = 4;
118
119 q->high_mark = q->n_window / 8;
120 if (q->high_mark < 2)
121 q->high_mark = 2;
122
123 q->write_ptr = 0;
124 q->read_ptr = 0;
125
126 return 0;
127}
128
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200129static int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
130 struct iwl_dma_ptr *ptr, size_t size)
131{
132 if (WARN_ON(ptr->addr))
133 return -EINVAL;
134
135 ptr->addr = dma_alloc_coherent(trans->dev, size,
136 &ptr->dma, GFP_KERNEL);
137 if (!ptr->addr)
138 return -ENOMEM;
139 ptr->size = size;
140 return 0;
141}
142
143static void iwl_pcie_free_dma_ptr(struct iwl_trans *trans,
144 struct iwl_dma_ptr *ptr)
145{
146 if (unlikely(!ptr->addr))
147 return;
148
149 dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
150 memset(ptr, 0, sizeof(*ptr));
151}
152
153static void iwl_pcie_txq_stuck_timer(unsigned long data)
154{
155 struct iwl_txq *txq = (void *)data;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200156 struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
157 struct iwl_trans *trans = iwl_trans_pcie_get_trans(trans_pcie);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200158
159 spin_lock(&txq->lock);
160 /* check if triggered erroneously */
161 if (txq->q.read_ptr == txq->q.write_ptr) {
162 spin_unlock(&txq->lock);
163 return;
164 }
165 spin_unlock(&txq->lock);
166
167 IWL_ERR(trans, "Queue %d stuck for %u ms.\n", txq->q.id,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200168 jiffies_to_msecs(txq->wd_timeout));
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200169
Sara Sharon38398ef2016-06-30 11:48:30 +0300170 iwl_trans_pcie_log_scd_error(trans, txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200171
Liad Kaufman4c9706d2014-04-27 16:46:09 +0300172 iwl_force_nmi(trans);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200173}
174
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200175/*
176 * iwl_pcie_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300177 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200178static void iwl_pcie_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
Sara Sharon4fe10bc2016-07-04 14:34:26 +0300179 struct iwl_txq *txq, u16 byte_cnt,
180 int num_tbs)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300181{
Emmanuel Grumbach105183b2011-08-25 23:11:02 -0700182 struct iwlagn_scd_bc_tbl *scd_bc_tbl;
Johannes Berg20d3b642012-05-16 22:54:29 +0200183 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300184 int write_ptr = txq->q.write_ptr;
185 int txq_id = txq->q.id;
186 u8 sec_ctl = 0;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300187 u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
188 __le16 bc_ent;
Emmanuel Grumbach132f98c2011-09-20 15:37:24 -0700189 struct iwl_tx_cmd *tx_cmd =
Johannes Bergbf8440e2012-03-19 17:12:06 +0100190 (void *) txq->entries[txq->q.write_ptr].cmd->payload;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300191
Emmanuel Grumbach105183b2011-08-25 23:11:02 -0700192 scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
193
Emmanuel Grumbach132f98c2011-09-20 15:37:24 -0700194 sec_ctl = tx_cmd->sec_ctl;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300195
196 switch (sec_ctl & TX_CMD_SEC_MSK) {
197 case TX_CMD_SEC_CCM:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200198 len += IEEE80211_CCMP_MIC_LEN;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300199 break;
200 case TX_CMD_SEC_TKIP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200201 len += IEEE80211_TKIP_ICV_LEN;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300202 break;
203 case TX_CMD_SEC_WEP:
Johannes Berg4325f6c2013-05-08 13:09:08 +0200204 len += IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300205 break;
206 }
Emmanuel Grumbach046db342012-12-05 15:07:54 +0200207 if (trans_pcie->bc_table_dword)
208 len = DIV_ROUND_UP(len, 4);
209
Emmanuel Grumbach31f920b2015-07-02 14:53:02 +0300210 if (WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX))
211 return;
212
Sara Sharon4fe10bc2016-07-04 14:34:26 +0300213 if (trans->cfg->use_tfh) {
214 u8 filled_tfd_size = offsetof(struct iwl_tfh_tfd, tbs) +
215 num_tbs * sizeof(struct iwl_tfh_tb);
216 /*
217 * filled_tfd_size contains the number of filled bytes in the
218 * TFD.
219 * Dividing it by 64 will give the number of chunks to fetch
220 * to SRAM- 0 for one chunk, 1 for 2 and so on.
221 * If, for example, TFD contains only 3 TBs then 32 bytes
222 * of the TFD are used, and only one chunk of 64 bytes should
223 * be fetched
224 */
225 u8 num_fetch_chunks = DIV_ROUND_UP(filled_tfd_size, 64) - 1;
226
227 bc_ent = cpu_to_le16(len | (num_fetch_chunks << 12));
228 } else {
229 u8 sta_id = tx_cmd->sta_id;
230
231 bc_ent = cpu_to_le16(len | (sta_id << 12));
232 }
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300233
234 scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
235
236 if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
237 scd_bc_tbl[txq_id].
238 tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
239}
240
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200241static void iwl_pcie_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
242 struct iwl_txq *txq)
243{
244 struct iwl_trans_pcie *trans_pcie =
245 IWL_TRANS_GET_PCIE_TRANS(trans);
246 struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
247 int txq_id = txq->q.id;
248 int read_ptr = txq->q.read_ptr;
249 u8 sta_id = 0;
250 __le16 bc_ent;
251 struct iwl_tx_cmd *tx_cmd =
252 (void *)txq->entries[txq->q.read_ptr].cmd->payload;
253
254 WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
255
256 if (txq_id != trans_pcie->cmd_queue)
257 sta_id = tx_cmd->sta_id;
258
259 bc_ent = cpu_to_le16(1 | (sta_id << 12));
Sara Sharon4fe10bc2016-07-04 14:34:26 +0300260
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200261 scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
262
263 if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
264 scd_bc_tbl[txq_id].
265 tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
266}
267
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200268/*
269 * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800270 */
Johannes Bergea68f462014-02-27 14:36:55 +0100271static void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans,
272 struct iwl_txq *txq)
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800273{
Emmanuel Grumbach23e76d12014-01-20 09:50:29 +0200274 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800275 u32 reg = 0;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800276 int txq_id = txq->q.id;
277
Johannes Bergea68f462014-02-27 14:36:55 +0100278 lockdep_assert_held(&txq->lock);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800279
Eliad Peller50453882014-02-05 19:12:24 +0200280 /*
281 * explicitly wake up the NIC if:
282 * 1. shadow registers aren't enabled
283 * 2. NIC is woken up for CMD regardless of shadow outside this function
284 * 3. there is a chance that the NIC is asleep
285 */
286 if (!trans->cfg->base_params->shadow_reg_enable &&
287 txq_id != trans_pcie->cmd_queue &&
288 test_bit(STATUS_TPOWER_PMI, &trans->status)) {
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800289 /*
Eliad Peller50453882014-02-05 19:12:24 +0200290 * wake up nic if it's powered down ...
291 * uCode will wake up, and interrupt us again, so next
292 * time we'll skip this part.
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800293 */
Eliad Peller50453882014-02-05 19:12:24 +0200294 reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
295
296 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
297 IWL_DEBUG_INFO(trans, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
298 txq_id, reg);
299 iwl_set_bit(trans, CSR_GP_CNTRL,
300 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Johannes Bergea68f462014-02-27 14:36:55 +0100301 txq->need_update = true;
Eliad Peller50453882014-02-05 19:12:24 +0200302 return;
303 }
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800304 }
Eliad Peller50453882014-02-05 19:12:24 +0200305
306 /*
307 * if not in power-save mode, uCode will never sleep when we're
308 * trying to tx (during RFKILL, we're not trying to tx).
309 */
310 IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id, txq->q.write_ptr);
Emmanuel Grumbach0cd58ea2015-11-24 13:24:24 +0200311 if (!txq->block)
312 iwl_write32(trans, HBUS_TARG_WRPTR,
313 txq->q.write_ptr | (txq_id << 8));
Johannes Bergea68f462014-02-27 14:36:55 +0100314}
Eliad Peller50453882014-02-05 19:12:24 +0200315
Johannes Bergea68f462014-02-27 14:36:55 +0100316void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans)
317{
318 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
319 int i;
320
321 for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
322 struct iwl_txq *txq = &trans_pcie->txq[i];
323
Emmanuel Grumbachd090f872014-05-13 08:10:51 +0300324 spin_lock_bh(&txq->lock);
Johannes Bergea68f462014-02-27 14:36:55 +0100325 if (trans_pcie->txq[i].need_update) {
326 iwl_pcie_txq_inc_wr_ptr(trans, txq);
327 trans_pcie->txq[i].need_update = false;
328 }
Emmanuel Grumbachd090f872014-05-13 08:10:51 +0300329 spin_unlock_bh(&txq->lock);
Johannes Bergea68f462014-02-27 14:36:55 +0100330 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800331}
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800332
Sara Sharon6983ba62016-06-26 13:17:56 +0300333static inline void *iwl_pcie_get_tfd(struct iwl_trans_pcie *trans_pcie,
334 struct iwl_txq *txq, int idx)
Johannes Berg214d14d2011-05-04 07:50:44 -0700335{
Sara Sharon6983ba62016-06-26 13:17:56 +0300336 return txq->tfds + trans_pcie->tfd_size * idx;
337}
Johannes Berg214d14d2011-05-04 07:50:44 -0700338
Sara Sharon6983ba62016-06-26 13:17:56 +0300339static inline dma_addr_t iwl_pcie_tfd_tb_get_addr(struct iwl_trans *trans,
340 void *tfd, u8 idx)
341{
342 struct iwl_tfd *tfd_fh;
343 struct iwl_tfd_tb *tb;
344 dma_addr_t addr;
345
346 if (trans->cfg->use_tfh) {
347 struct iwl_tfh_tfd *tfd_fh = (void *)tfd;
348 struct iwl_tfh_tb *tb = &tfd_fh->tbs[idx];
349
350 return (dma_addr_t)(le64_to_cpu(tb->addr));
351 }
352
353 tfd_fh = (void *)tfd;
354 tb = &tfd_fh->tbs[idx];
355 addr = get_unaligned_le32(&tb->lo);
356
Johannes Berg214d14d2011-05-04 07:50:44 -0700357 if (sizeof(dma_addr_t) > sizeof(u32))
358 addr |=
359 ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
360
361 return addr;
362}
363
Sara Sharon6983ba62016-06-26 13:17:56 +0300364static inline void iwl_pcie_tfd_set_tb(struct iwl_trans *trans, void *tfd,
365 u8 idx, dma_addr_t addr, u16 len)
Johannes Berg214d14d2011-05-04 07:50:44 -0700366{
Sara Sharon6983ba62016-06-26 13:17:56 +0300367 if (trans->cfg->use_tfh) {
368 struct iwl_tfh_tfd *tfd_fh = (void *)tfd;
369 struct iwl_tfh_tb *tb = &tfd_fh->tbs[idx];
Johannes Berg214d14d2011-05-04 07:50:44 -0700370
Sara Sharon6983ba62016-06-26 13:17:56 +0300371 put_unaligned_le64(addr, &tb->addr);
372 tb->tb_len = cpu_to_le16(len);
Johannes Berg214d14d2011-05-04 07:50:44 -0700373
Sara Sharon6983ba62016-06-26 13:17:56 +0300374 tfd_fh->num_tbs = cpu_to_le16(idx + 1);
375 } else {
376 struct iwl_tfd *tfd_fh = (void *)tfd;
377 struct iwl_tfd_tb *tb = &tfd_fh->tbs[idx];
Johannes Berg214d14d2011-05-04 07:50:44 -0700378
Sara Sharon6983ba62016-06-26 13:17:56 +0300379 u16 hi_n_len = len << 4;
380
381 put_unaligned_le32(addr, &tb->lo);
382 if (sizeof(dma_addr_t) > sizeof(u32))
383 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
384
385 tb->hi_n_len = cpu_to_le16(hi_n_len);
386
387 tfd_fh->num_tbs = idx + 1;
388 }
Johannes Berg214d14d2011-05-04 07:50:44 -0700389}
390
Sara Sharon6983ba62016-06-26 13:17:56 +0300391static inline u8 iwl_pcie_tfd_get_num_tbs(struct iwl_trans *trans, void *tfd)
Johannes Berg214d14d2011-05-04 07:50:44 -0700392{
Sara Sharon6983ba62016-06-26 13:17:56 +0300393 struct iwl_tfd *tfd_fh;
394
395 if (trans->cfg->use_tfh) {
396 struct iwl_tfh_tfd *tfd_fh = (void *)tfd;
397
398 return le16_to_cpu(tfd_fh->num_tbs) & 0x1f;
399 }
400
401 tfd_fh = (void *)tfd;
402 return tfd_fh->num_tbs & 0x1f;
Johannes Berg214d14d2011-05-04 07:50:44 -0700403}
404
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200405static void iwl_pcie_tfd_unmap(struct iwl_trans *trans,
Johannes Berg98891752013-02-26 11:28:19 +0100406 struct iwl_cmd_meta *meta,
Sara Sharon6983ba62016-06-26 13:17:56 +0300407 struct iwl_txq *txq, int index)
Johannes Berg214d14d2011-05-04 07:50:44 -0700408{
Sara Sharon3cd19802016-06-23 16:31:40 +0300409 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
410 int i, num_tbs;
Sara Sharon6983ba62016-06-26 13:17:56 +0300411 void *tfd = iwl_pcie_get_tfd(trans_pcie, txq, index);
Johannes Berg214d14d2011-05-04 07:50:44 -0700412
Johannes Berg214d14d2011-05-04 07:50:44 -0700413 /* Sanity check on number of chunks */
Sara Sharon6983ba62016-06-26 13:17:56 +0300414 num_tbs = iwl_pcie_tfd_get_num_tbs(trans, tfd);
Johannes Berg214d14d2011-05-04 07:50:44 -0700415
Sara Sharon3cd19802016-06-23 16:31:40 +0300416 if (num_tbs >= trans_pcie->max_tbs) {
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700417 IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
Johannes Berg214d14d2011-05-04 07:50:44 -0700418 /* @todo issue fatal error, it is quite serious situation */
419 return;
420 }
421
Sara Sharon8de437c2016-06-09 17:56:38 +0300422 /* first TB is never freed - it's the bidirectional DMA data */
Johannes Berg214d14d2011-05-04 07:50:44 -0700423
Johannes Berg206eea72015-04-17 16:38:31 +0200424 for (i = 1; i < num_tbs; i++) {
Sara Sharon3cd19802016-06-23 16:31:40 +0300425 if (meta->tbs & BIT(i))
Johannes Berg206eea72015-04-17 16:38:31 +0200426 dma_unmap_page(trans->dev,
Sara Sharon6983ba62016-06-26 13:17:56 +0300427 iwl_pcie_tfd_tb_get_addr(trans, tfd, i),
428 iwl_pcie_tfd_tb_get_len(trans, tfd, i),
Johannes Berg206eea72015-04-17 16:38:31 +0200429 DMA_TO_DEVICE);
430 else
431 dma_unmap_single(trans->dev,
Sara Sharon6983ba62016-06-26 13:17:56 +0300432 iwl_pcie_tfd_tb_get_addr(trans, tfd,
433 i),
434 iwl_pcie_tfd_tb_get_len(trans, tfd,
435 i),
Johannes Berg206eea72015-04-17 16:38:31 +0200436 DMA_TO_DEVICE);
437 }
Sara Sharon6983ba62016-06-26 13:17:56 +0300438
439 if (trans->cfg->use_tfh) {
440 struct iwl_tfh_tfd *tfd_fh = (void *)tfd;
441
442 tfd_fh->num_tbs = 0;
443 } else {
444 struct iwl_tfd *tfd_fh = (void *)tfd;
445
446 tfd_fh->num_tbs = 0;
447 }
448
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700449}
450
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200451/*
452 * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700453 * @trans - transport private data
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700454 * @txq - tx queue
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200455 * @dma_dir - the direction of the DMA mapping
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700456 *
457 * Does NOT advance any TFD circular buffer read/write indexes
458 * Does NOT free the TFD itself (which is within circular buffer)
459 */
Johannes Berg98891752013-02-26 11:28:19 +0100460static void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700461{
Johannes Berg83f32a42014-04-24 09:57:40 +0200462 /* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and
463 * idx is bounded by n_window
464 */
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200465 int rd_ptr = txq->q.read_ptr;
466 int idx = get_cmd_index(&txq->q, rd_ptr);
467
Johannes Berg015c15e2012-03-05 11:24:24 -0800468 lockdep_assert_held(&txq->lock);
469
Johannes Berg83f32a42014-04-24 09:57:40 +0200470 /* We have only q->n_window txq->entries, but we use
471 * TFD_QUEUE_SIZE_MAX tfds
472 */
Sara Sharon6983ba62016-06-26 13:17:56 +0300473 iwl_pcie_tfd_unmap(trans, &txq->entries[idx].meta, txq, rd_ptr);
Johannes Berg214d14d2011-05-04 07:50:44 -0700474
475 /* free SKB */
Johannes Bergbf8440e2012-03-19 17:12:06 +0100476 if (txq->entries) {
Johannes Berg214d14d2011-05-04 07:50:44 -0700477 struct sk_buff *skb;
478
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200479 skb = txq->entries[idx].skb;
Johannes Berg214d14d2011-05-04 07:50:44 -0700480
Emmanuel Grumbach909e9b22011-09-15 11:46:30 -0700481 /* Can be called from irqs-disabled context
482 * If skb is not NULL, it means that the whole queue is being
483 * freed and that the queue is not empty - free the skb
484 */
Johannes Berg214d14d2011-05-04 07:50:44 -0700485 if (skb) {
Emmanuel Grumbached277c92012-02-09 16:08:15 +0200486 iwl_op_mode_free_skb(trans->op_mode, skb);
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200487 txq->entries[idx].skb = NULL;
Johannes Berg214d14d2011-05-04 07:50:44 -0700488 }
489 }
490}
491
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200492static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
Johannes Berg6d6e68f2014-04-23 19:00:56 +0200493 dma_addr_t addr, u16 len, bool reset)
Johannes Berg214d14d2011-05-04 07:50:44 -0700494{
Sara Sharon3cd19802016-06-23 16:31:40 +0300495 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Johannes Berg214d14d2011-05-04 07:50:44 -0700496 struct iwl_queue *q;
Sara Sharon6983ba62016-06-26 13:17:56 +0300497 void *tfd;
Johannes Berg214d14d2011-05-04 07:50:44 -0700498 u32 num_tbs;
499
500 q = &txq->q;
Sara Sharon6983ba62016-06-26 13:17:56 +0300501 tfd = txq->tfds + trans_pcie->tfd_size * q->write_ptr;
Johannes Berg214d14d2011-05-04 07:50:44 -0700502
503 if (reset)
Sara Sharon6983ba62016-06-26 13:17:56 +0300504 memset(tfd, 0, trans_pcie->tfd_size);
Johannes Berg214d14d2011-05-04 07:50:44 -0700505
Sara Sharon6983ba62016-06-26 13:17:56 +0300506 num_tbs = iwl_pcie_tfd_get_num_tbs(trans, tfd);
Johannes Berg214d14d2011-05-04 07:50:44 -0700507
Sara Sharon6983ba62016-06-26 13:17:56 +0300508 /* Each TFD can point to a maximum max_tbs Tx buffers */
Sara Sharon3cd19802016-06-23 16:31:40 +0300509 if (num_tbs >= trans_pcie->max_tbs) {
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700510 IWL_ERR(trans, "Error can not send more than %d chunks\n",
Sara Sharon3cd19802016-06-23 16:31:40 +0300511 trans_pcie->max_tbs);
Johannes Berg214d14d2011-05-04 07:50:44 -0700512 return -EINVAL;
513 }
514
Eliad Peller1092b9b2013-07-16 17:53:43 +0300515 if (WARN(addr & ~IWL_TX_DMA_MASK,
516 "Unaligned address = %llx\n", (unsigned long long)addr))
Johannes Berg214d14d2011-05-04 07:50:44 -0700517 return -EINVAL;
518
Sara Sharon6983ba62016-06-26 13:17:56 +0300519 iwl_pcie_tfd_set_tb(trans, tfd, num_tbs, addr, len);
Johannes Berg214d14d2011-05-04 07:50:44 -0700520
Johannes Berg206eea72015-04-17 16:38:31 +0200521 return num_tbs;
Johannes Berg214d14d2011-05-04 07:50:44 -0700522}
523
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200524static int iwl_pcie_txq_alloc(struct iwl_trans *trans,
525 struct iwl_txq *txq, int slots_num,
526 u32 txq_id)
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800527{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200528 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Sara Sharon6983ba62016-06-26 13:17:56 +0300529 size_t tfd_sz = trans_pcie->tfd_size * TFD_QUEUE_SIZE_MAX;
Sara Sharon8de437c2016-06-09 17:56:38 +0300530 size_t tb0_buf_sz;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200531 int i;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800532
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200533 if (WARN_ON(txq->entries || txq->tfds))
534 return -EINVAL;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800535
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200536 setup_timer(&txq->stuck_timer, iwl_pcie_txq_stuck_timer,
537 (unsigned long)txq);
538 txq->trans_pcie = trans_pcie;
539
540 txq->q.n_window = slots_num;
541
542 txq->entries = kcalloc(slots_num,
543 sizeof(struct iwl_pcie_txq_entry),
544 GFP_KERNEL);
545
546 if (!txq->entries)
547 goto error;
548
549 if (txq_id == trans_pcie->cmd_queue)
550 for (i = 0; i < slots_num; i++) {
551 txq->entries[i].cmd =
552 kmalloc(sizeof(struct iwl_device_cmd),
553 GFP_KERNEL);
554 if (!txq->entries[i].cmd)
555 goto error;
556 }
557
558 /* Circular buffer of transmit frame descriptors (TFDs),
559 * shared with device */
560 txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
561 &txq->q.dma_addr, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000562 if (!txq->tfds)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200563 goto error;
Johannes Berg38c0f3342013-02-27 13:18:50 +0100564
Sara Sharon8de437c2016-06-09 17:56:38 +0300565 BUILD_BUG_ON(IWL_FIRST_TB_SIZE_ALIGN != sizeof(*txq->first_tb_bufs));
Johannes Berg38c0f3342013-02-27 13:18:50 +0100566
Sara Sharon8de437c2016-06-09 17:56:38 +0300567 tb0_buf_sz = sizeof(*txq->first_tb_bufs) * slots_num;
Johannes Berg38c0f3342013-02-27 13:18:50 +0100568
Sara Sharon8de437c2016-06-09 17:56:38 +0300569 txq->first_tb_bufs = dma_alloc_coherent(trans->dev, tb0_buf_sz,
570 &txq->first_tb_dma,
Johannes Berg38c0f3342013-02-27 13:18:50 +0100571 GFP_KERNEL);
Sara Sharon8de437c2016-06-09 17:56:38 +0300572 if (!txq->first_tb_bufs)
Johannes Berg38c0f3342013-02-27 13:18:50 +0100573 goto err_free_tfds;
574
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200575 txq->q.id = txq_id;
576
577 return 0;
Johannes Berg38c0f3342013-02-27 13:18:50 +0100578err_free_tfds:
579 dma_free_coherent(trans->dev, tfd_sz, txq->tfds, txq->q.dma_addr);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200580error:
581 if (txq->entries && txq_id == trans_pcie->cmd_queue)
582 for (i = 0; i < slots_num; i++)
583 kfree(txq->entries[i].cmd);
584 kfree(txq->entries);
585 txq->entries = NULL;
586
587 return -ENOMEM;
588
589}
590
591static int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
592 int slots_num, u32 txq_id)
593{
594 int ret;
595
Johannes Berg43aa6162014-02-27 14:24:36 +0100596 txq->need_update = false;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200597
598 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
599 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
600 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
601
602 /* Initialize queue's high/low-water marks, and head/tail indexes */
Johannes Berg83f32a42014-04-24 09:57:40 +0200603 ret = iwl_queue_init(&txq->q, slots_num, txq_id);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200604 if (ret)
605 return ret;
606
607 spin_lock_init(&txq->lock);
Emmanuel Grumbach39555252016-01-14 09:39:21 +0200608 __skb_queue_head_init(&txq->overflow_q);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200609
610 /*
611 * Tell nic where to find circular buffer of Tx Frame Descriptors for
612 * given Tx queue, and enable the DMA channel used for that queue.
613 * Circular buffer (TFD queue in DRAM) physical base address */
Sara Sharone22744a2016-06-22 17:23:34 +0300614 if (trans->cfg->use_tfh)
615 iwl_write_direct64(trans,
616 FH_MEM_CBBC_QUEUE(trans, txq_id),
617 txq->q.dma_addr);
618 else
619 iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(trans, txq_id),
620 txq->q.dma_addr >> 8);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200621
622 return 0;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800623}
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800624
Johannes Berg21cb3222016-06-21 13:11:48 +0200625static void iwl_pcie_free_tso_page(struct iwl_trans_pcie *trans_pcie,
626 struct sk_buff *skb)
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +0300627{
Johannes Berg21cb3222016-06-21 13:11:48 +0200628 struct page **page_ptr;
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +0300629
Johannes Berg21cb3222016-06-21 13:11:48 +0200630 page_ptr = (void *)((u8 *)skb->cb + trans_pcie->page_offs);
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +0300631
Johannes Berg21cb3222016-06-21 13:11:48 +0200632 if (*page_ptr) {
633 __free_page(*page_ptr);
634 *page_ptr = NULL;
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +0300635 }
636}
637
Sara Sharon01d11cd2016-03-09 17:38:47 +0200638static void iwl_pcie_clear_cmd_in_flight(struct iwl_trans *trans)
639{
640 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
641
642 lockdep_assert_held(&trans_pcie->reg_lock);
643
644 if (trans_pcie->ref_cmd_in_flight) {
645 trans_pcie->ref_cmd_in_flight = false;
646 IWL_DEBUG_RPM(trans, "clear ref_cmd_in_flight - unref\n");
Luca Coelhoc24c7f52016-03-30 20:59:27 +0300647 iwl_trans_unref(trans);
Sara Sharon01d11cd2016-03-09 17:38:47 +0200648 }
649
650 if (!trans->cfg->base_params->apmg_wake_up_wa)
651 return;
652 if (WARN_ON(!trans_pcie->cmd_hold_nic_awake))
653 return;
654
655 trans_pcie->cmd_hold_nic_awake = false;
656 __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
657 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
658}
659
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200660/*
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200661 * iwl_pcie_txq_unmap - Unmap any remaining DMA mappings and free skb's
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800662 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200663static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800664{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200665 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
666 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
667 struct iwl_queue *q = &txq->q;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800668
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200669 spin_lock_bh(&txq->lock);
670 while (q->write_ptr != q->read_ptr) {
Emmanuel Grumbachb9676132013-06-13 11:45:59 +0300671 IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
672 txq_id, q->read_ptr);
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +0300673
674 if (txq_id != trans_pcie->cmd_queue) {
675 struct sk_buff *skb = txq->entries[q->read_ptr].skb;
676
677 if (WARN_ON_ONCE(!skb))
678 continue;
679
Johannes Berg21cb3222016-06-21 13:11:48 +0200680 iwl_pcie_free_tso_page(trans_pcie, skb);
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +0300681 }
Johannes Berg98891752013-02-26 11:28:19 +0100682 iwl_pcie_txq_free_tfd(trans, txq);
Johannes Berg83f32a42014-04-24 09:57:40 +0200683 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr);
Sara Sharon01d11cd2016-03-09 17:38:47 +0200684
685 if (q->read_ptr == q->write_ptr) {
686 unsigned long flags;
687
688 spin_lock_irqsave(&trans_pcie->reg_lock, flags);
689 if (txq_id != trans_pcie->cmd_queue) {
690 IWL_DEBUG_RPM(trans, "Q %d - last tx freed\n",
691 q->id);
Luca Coelhoc24c7f52016-03-30 20:59:27 +0300692 iwl_trans_unref(trans);
Sara Sharon01d11cd2016-03-09 17:38:47 +0200693 } else {
694 iwl_pcie_clear_cmd_in_flight(trans);
695 }
696 spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
697 }
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200698 }
Emmanuel Grumbachb9676132013-06-13 11:45:59 +0300699 txq->active = false;
Emmanuel Grumbach39555252016-01-14 09:39:21 +0200700
701 while (!skb_queue_empty(&txq->overflow_q)) {
702 struct sk_buff *skb = __skb_dequeue(&txq->overflow_q);
703
704 iwl_op_mode_free_skb(trans->op_mode, skb);
705 }
706
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200707 spin_unlock_bh(&txq->lock);
Emmanuel Grumbach8a487b12013-06-13 13:10:00 +0300708
709 /* just in case - this queue may have been stopped */
710 iwl_wake_queue(trans, txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200711}
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800712
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200713/*
714 * iwl_pcie_txq_free - Deallocate DMA queue.
715 * @txq: Transmit queue to deallocate.
716 *
717 * Empty queue by removing and destroying all BD's.
718 * Free all buffers.
719 * 0-fill, but do not free "txq" descriptor structure.
720 */
721static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
722{
723 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
724 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
725 struct device *dev = trans->dev;
726 int i;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800727
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200728 if (WARN_ON(!txq))
729 return;
730
731 iwl_pcie_txq_unmap(trans, txq_id);
732
733 /* De-alloc array of command/tx buffers */
734 if (txq_id == trans_pcie->cmd_queue)
735 for (i = 0; i < txq->q.n_window; i++) {
Johannes Berg5d4185a2014-09-09 21:16:06 +0200736 kzfree(txq->entries[i].cmd);
737 kzfree(txq->entries[i].free_buf);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200738 }
739
740 /* De-alloc circular buffer of TFDs */
Johannes Berg83f32a42014-04-24 09:57:40 +0200741 if (txq->tfds) {
742 dma_free_coherent(dev,
Sara Sharon6983ba62016-06-26 13:17:56 +0300743 trans_pcie->tfd_size * TFD_QUEUE_SIZE_MAX,
Johannes Berg83f32a42014-04-24 09:57:40 +0200744 txq->tfds, txq->q.dma_addr);
Johannes Bergd21fa2d2013-01-08 00:25:21 +0100745 txq->q.dma_addr = 0;
Johannes Berg83f32a42014-04-24 09:57:40 +0200746 txq->tfds = NULL;
Johannes Berg38c0f3342013-02-27 13:18:50 +0100747
748 dma_free_coherent(dev,
Sara Sharon8de437c2016-06-09 17:56:38 +0300749 sizeof(*txq->first_tb_bufs) * txq->q.n_window,
750 txq->first_tb_bufs, txq->first_tb_dma);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200751 }
752
753 kfree(txq->entries);
754 txq->entries = NULL;
755
756 del_timer_sync(&txq->stuck_timer);
757
758 /* 0-fill queue descriptor structure */
759 memset(txq, 0, sizeof(*txq));
760}
761
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200762void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
763{
764 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Johannes Berg22dc3c92013-01-09 00:47:07 +0100765 int nq = trans->cfg->base_params->num_of_queues;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200766 int chan;
767 u32 reg_val;
Johannes Berg22dc3c92013-01-09 00:47:07 +0100768 int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
769 SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200770
771 /* make sure all queue are not stopped/used */
772 memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
773 memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
774
Sara Sharonae797852016-06-30 16:36:24 +0300775 if (trans->cfg->use_tfh)
776 return;
777
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200778 trans_pcie->scd_base_addr =
779 iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
780
781 WARN_ON(scd_base_addr != 0 &&
782 scd_base_addr != trans_pcie->scd_base_addr);
783
Johannes Berg22dc3c92013-01-09 00:47:07 +0100784 /* reset context data, TX status and translation data */
785 iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
786 SCD_CONTEXT_MEM_LOWER_BOUND,
787 NULL, clear_dwords);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200788
789 iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
790 trans_pcie->scd_bc_tbls.dma >> 10);
791
792 /* The chain extension of the SCD doesn't work well. This feature is
793 * enabled by default by the HW, so we need to disable it manually.
794 */
Emmanuel Grumbache03bbb62014-04-13 10:49:16 +0300795 if (trans->cfg->base_params->scd_chain_ext_wa)
796 iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200797
798 iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200799 trans_pcie->cmd_fifo,
800 trans_pcie->cmd_q_wdg_timeout);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200801
802 /* Activate all Tx DMA/FIFO channels */
Avri Altman680073b2014-07-14 09:40:27 +0300803 iwl_scd_activate_fifos(trans);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200804
805 /* Enable DMA channel */
806 for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
807 iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
808 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
809 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
810
811 /* Update FH chicken bits */
812 reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
813 iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
814 reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
815
816 /* Enable L1-Active */
Eran Harary3073d8c2013-12-29 14:09:59 +0200817 if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
818 iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
819 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200820}
821
Johannes Bergddaf5a52013-01-08 11:25:44 +0100822void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
823{
824 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
825 int txq_id;
826
827 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
828 txq_id++) {
829 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
Sara Sharone22744a2016-06-22 17:23:34 +0300830 if (trans->cfg->use_tfh)
831 iwl_write_direct64(trans,
832 FH_MEM_CBBC_QUEUE(trans, txq_id),
833 txq->q.dma_addr);
834 else
835 iwl_write_direct32(trans,
836 FH_MEM_CBBC_QUEUE(trans, txq_id),
837 txq->q.dma_addr >> 8);
Johannes Bergddaf5a52013-01-08 11:25:44 +0100838 iwl_pcie_txq_unmap(trans, txq_id);
839 txq->q.read_ptr = 0;
840 txq->q.write_ptr = 0;
841 }
842
843 /* Tell NIC where to find the "keep warm" buffer */
844 iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
845 trans_pcie->kw.dma >> 4);
846
Emmanuel Grumbachcd8f4382015-01-29 21:34:00 +0200847 /*
848 * Send 0 as the scd_base_addr since the device may have be reset
849 * while we were in WoWLAN in which case SCD_SRAM_BASE_ADDR will
850 * contain garbage.
851 */
852 iwl_pcie_tx_start(trans, 0);
Johannes Bergddaf5a52013-01-08 11:25:44 +0100853}
854
Emmanuel Grumbach36277232015-02-25 15:49:39 +0200855static void iwl_pcie_tx_stop_fh(struct iwl_trans *trans)
856{
857 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
858 unsigned long flags;
859 int ch, ret;
860 u32 mask = 0;
861
862 spin_lock(&trans_pcie->irq_lock);
863
Emmanuel Grumbach23ba9342015-12-17 11:55:13 +0200864 if (!iwl_trans_grab_nic_access(trans, &flags))
Emmanuel Grumbach36277232015-02-25 15:49:39 +0200865 goto out;
866
867 /* Stop each Tx DMA channel */
868 for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
869 iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
870 mask |= FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch);
871 }
872
873 /* Wait for DMA channels to be idle */
874 ret = iwl_poll_bit(trans, FH_TSSR_TX_STATUS_REG, mask, mask, 5000);
875 if (ret < 0)
876 IWL_ERR(trans,
877 "Failing on timeout while stopping DMA channel %d [0x%08x]\n",
878 ch, iwl_read32(trans, FH_TSSR_TX_STATUS_REG));
879
880 iwl_trans_release_nic_access(trans, &flags);
881
882out:
883 spin_unlock(&trans_pcie->irq_lock);
884}
885
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200886/*
887 * iwl_pcie_tx_stop - Stop all Tx DMA channels
888 */
889int iwl_pcie_tx_stop(struct iwl_trans *trans)
890{
891 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach36277232015-02-25 15:49:39 +0200892 int txq_id;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200893
894 /* Turn off all Tx DMA fifos */
Avri Altman680073b2014-07-14 09:40:27 +0300895 iwl_scd_deactivate_fifos(trans);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200896
Emmanuel Grumbach36277232015-02-25 15:49:39 +0200897 /* Turn off all Tx DMA channels */
898 iwl_pcie_tx_stop_fh(trans);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200899
Emmanuel Grumbachfba1c622013-12-19 22:19:17 +0200900 /*
901 * This function can be called before the op_mode disabled the
902 * queues. This happens when we have an rfkill interrupt.
903 * Since we stop Tx altogether - mark the queues as stopped.
904 */
905 memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
906 memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
907
908 /* This can happen: start_hw, stop_device */
909 if (!trans_pcie->txq)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200910 return 0;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200911
912 /* Unmap DMA from host system and free skb's */
913 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
914 txq_id++)
915 iwl_pcie_txq_unmap(trans, txq_id);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800916
917 return 0;
918}
919
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200920/*
921 * iwl_trans_tx_free - Free TXQ Context
922 *
923 * Destroy all TX DMA queues and structures
924 */
925void iwl_pcie_tx_free(struct iwl_trans *trans)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300926{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200927 int txq_id;
928 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300929
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200930 /* Tx queues */
931 if (trans_pcie->txq) {
932 for (txq_id = 0;
933 txq_id < trans->cfg->base_params->num_of_queues; txq_id++)
934 iwl_pcie_txq_free(trans, txq_id);
935 }
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300936
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200937 kfree(trans_pcie->txq);
938 trans_pcie->txq = NULL;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300939
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200940 iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300941
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200942 iwl_pcie_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300943}
944
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200945/*
946 * iwl_pcie_tx_alloc - allocate TX context
947 * Allocate all Tx DMA structures and initialize them
948 */
949static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
950{
951 int ret;
952 int txq_id, slots_num;
953 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
954
955 u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues *
956 sizeof(struct iwlagn_scd_bc_tbl);
957
958 /*It is not allowed to alloc twice, so warn when this happens.
959 * We cannot rely on the previous allocation, so free and fail */
960 if (WARN_ON(trans_pcie->txq)) {
961 ret = -EINVAL;
962 goto error;
963 }
964
965 ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
966 scd_bc_tbls_size);
967 if (ret) {
968 IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
969 goto error;
970 }
971
972 /* Alloc keep-warm buffer */
973 ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
974 if (ret) {
975 IWL_ERR(trans, "Keep Warm allocation failed\n");
976 goto error;
977 }
978
979 trans_pcie->txq = kcalloc(trans->cfg->base_params->num_of_queues,
980 sizeof(struct iwl_txq), GFP_KERNEL);
981 if (!trans_pcie->txq) {
982 IWL_ERR(trans, "Not enough memory for txq\n");
Dan Carpenter2ab9ba02013-08-11 02:03:21 +0300983 ret = -ENOMEM;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200984 goto error;
985 }
986
987 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
988 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
989 txq_id++) {
990 slots_num = (txq_id == trans_pcie->cmd_queue) ?
991 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
992 ret = iwl_pcie_txq_alloc(trans, &trans_pcie->txq[txq_id],
993 slots_num, txq_id);
994 if (ret) {
995 IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
996 goto error;
997 }
998 }
999
1000 return 0;
1001
1002error:
1003 iwl_pcie_tx_free(trans);
1004
1005 return ret;
1006}
1007int iwl_pcie_tx_init(struct iwl_trans *trans)
1008{
1009 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1010 int ret;
1011 int txq_id, slots_num;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001012 bool alloc = false;
1013
1014 if (!trans_pcie->txq) {
1015 ret = iwl_pcie_tx_alloc(trans);
1016 if (ret)
1017 goto error;
1018 alloc = true;
1019 }
1020
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +02001021 spin_lock(&trans_pcie->irq_lock);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001022
1023 /* Turn off all Tx DMA fifos */
Avri Altman680073b2014-07-14 09:40:27 +03001024 iwl_scd_deactivate_fifos(trans);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001025
1026 /* Tell NIC where to find the "keep warm" buffer */
1027 iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
1028 trans_pcie->kw.dma >> 4);
1029
Emmanuel Grumbach7b70bd62013-12-11 10:22:28 +02001030 spin_unlock(&trans_pcie->irq_lock);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001031
1032 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
1033 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
1034 txq_id++) {
1035 slots_num = (txq_id == trans_pcie->cmd_queue) ?
1036 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
1037 ret = iwl_pcie_txq_init(trans, &trans_pcie->txq[txq_id],
1038 slots_num, txq_id);
1039 if (ret) {
1040 IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
1041 goto error;
1042 }
1043 }
1044
Sara Sharonae797852016-06-30 16:36:24 +03001045 if (trans->cfg->use_tfh) {
Sara Sharone22744a2016-06-22 17:23:34 +03001046 iwl_write_direct32(trans, TFH_TRANSFER_MODE,
1047 TFH_TRANSFER_MAX_PENDING_REQ |
1048 TFH_CHUNK_SIZE_128 |
1049 TFH_CHUNK_SPLIT_MODE);
Sara Sharonae797852016-06-30 16:36:24 +03001050 return 0;
1051 }
Sara Sharone22744a2016-06-22 17:23:34 +03001052
Haim Dreyfuss94ce9e52015-06-14 11:17:07 +03001053 iwl_set_bits_prph(trans, SCD_GP_CTRL, SCD_GP_CTRL_AUTO_ACTIVE_MODE);
Emmanuel Grumbachcb6bb122015-01-25 10:36:31 +02001054 if (trans->cfg->base_params->num_of_queues > 20)
1055 iwl_set_bits_prph(trans, SCD_GP_CTRL,
1056 SCD_GP_CTRL_ENABLE_31_QUEUES);
1057
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001058 return 0;
1059error:
1060 /*Upon error, free only if we allocated something */
1061 if (alloc)
1062 iwl_pcie_tx_free(trans);
1063 return ret;
1064}
1065
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001066static inline void iwl_pcie_txq_progress(struct iwl_txq *txq)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001067{
Emmanuel Grumbache0b8d4052015-01-20 17:02:40 +02001068 lockdep_assert_held(&txq->lock);
1069
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001070 if (!txq->wd_timeout)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001071 return;
1072
1073 /*
Emmanuel Grumbache0b8d4052015-01-20 17:02:40 +02001074 * station is asleep and we send data - that must
1075 * be uAPSD or PS-Poll. Don't rearm the timer.
1076 */
1077 if (txq->frozen)
1078 return;
1079
1080 /*
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001081 * if empty delete timer, otherwise move timer forward
1082 * since we're making progress on this queue
1083 */
1084 if (txq->q.read_ptr == txq->q.write_ptr)
1085 del_timer(&txq->stuck_timer);
1086 else
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001087 mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001088}
1089
1090/* Frees buffers until index _not_ inclusive */
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +02001091void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
1092 struct sk_buff_head *skbs)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001093{
1094 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1095 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
Johannes Berg83f32a42014-04-24 09:57:40 +02001096 int tfd_num = ssn & (TFD_QUEUE_SIZE_MAX - 1);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001097 struct iwl_queue *q = &txq->q;
1098 int last_to_free;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001099
1100 /* This function is not meant to release cmd queue*/
1101 if (WARN_ON(txq_id == trans_pcie->cmd_queue))
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +02001102 return;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001103
Johannes Berg2bfb5092012-12-27 21:43:48 +01001104 spin_lock_bh(&txq->lock);
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +02001105
Emmanuel Grumbachb9676132013-06-13 11:45:59 +03001106 if (!txq->active) {
1107 IWL_DEBUG_TX_QUEUES(trans, "Q %d inactive - ignoring idx %d\n",
1108 txq_id, ssn);
1109 goto out;
1110 }
1111
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +02001112 if (txq->q.read_ptr == tfd_num)
1113 goto out;
1114
1115 IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
1116 txq_id, txq->q.read_ptr, tfd_num, ssn);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001117
1118 /*Since we free until index _not_ inclusive, the one before index is
1119 * the last we will free. This one must be used */
Johannes Berg83f32a42014-04-24 09:57:40 +02001120 last_to_free = iwl_queue_dec_wrap(tfd_num);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001121
Emmanuel Grumbach6ca6ebc2012-11-14 23:38:08 +02001122 if (!iwl_queue_used(q, last_to_free)) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001123 IWL_ERR(trans,
1124 "%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 +02001125 __func__, txq_id, last_to_free, TFD_QUEUE_SIZE_MAX,
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001126 q->write_ptr, q->read_ptr);
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +02001127 goto out;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001128 }
1129
1130 if (WARN_ON(!skb_queue_empty(skbs)))
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +02001131 goto out;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001132
1133 for (;
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +02001134 q->read_ptr != tfd_num;
Johannes Berg83f32a42014-04-24 09:57:40 +02001135 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr)) {
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +03001136 struct sk_buff *skb = txq->entries[txq->q.read_ptr].skb;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001137
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +03001138 if (WARN_ON_ONCE(!skb))
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001139 continue;
1140
Johannes Berg21cb3222016-06-21 13:11:48 +02001141 iwl_pcie_free_tso_page(trans_pcie, skb);
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +03001142
1143 __skb_queue_tail(skbs, skb);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001144
1145 txq->entries[txq->q.read_ptr].skb = NULL;
1146
Sara Sharon4fe10bc2016-07-04 14:34:26 +03001147 if (!trans->cfg->use_tfh)
1148 iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001149
Johannes Berg98891752013-02-26 11:28:19 +01001150 iwl_pcie_txq_free_tfd(trans, txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001151 }
1152
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001153 iwl_pcie_txq_progress(txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001154
Emmanuel Grumbach39555252016-01-14 09:39:21 +02001155 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
1156 test_bit(txq_id, trans_pcie->queue_stopped)) {
Emmanuel Grumbach685b3462016-02-23 11:34:17 +02001157 struct sk_buff_head overflow_skbs;
Emmanuel Grumbach39555252016-01-14 09:39:21 +02001158
Emmanuel Grumbach685b3462016-02-23 11:34:17 +02001159 __skb_queue_head_init(&overflow_skbs);
1160 skb_queue_splice_init(&txq->overflow_q, &overflow_skbs);
Emmanuel Grumbach39555252016-01-14 09:39:21 +02001161
1162 /*
1163 * This is tricky: we are in reclaim path which is non
1164 * re-entrant, so noone will try to take the access the
1165 * txq data from that path. We stopped tx, so we can't
1166 * have tx as well. Bottom line, we can unlock and re-lock
1167 * later.
1168 */
1169 spin_unlock_bh(&txq->lock);
1170
Emmanuel Grumbach685b3462016-02-23 11:34:17 +02001171 while (!skb_queue_empty(&overflow_skbs)) {
1172 struct sk_buff *skb = __skb_dequeue(&overflow_skbs);
Johannes Berg21cb3222016-06-21 13:11:48 +02001173 struct iwl_device_cmd *dev_cmd_ptr;
1174
1175 dev_cmd_ptr = *(void **)((u8 *)skb->cb +
1176 trans_pcie->dev_cmd_offs);
Emmanuel Grumbach39555252016-01-14 09:39:21 +02001177
1178 /*
1179 * Note that we can very well be overflowing again.
1180 * In that case, iwl_queue_space will be small again
1181 * and we won't wake mac80211's queue.
1182 */
Johannes Berg21cb3222016-06-21 13:11:48 +02001183 iwl_trans_pcie_tx(trans, skb, dev_cmd_ptr, txq_id);
Emmanuel Grumbach39555252016-01-14 09:39:21 +02001184 }
1185 spin_lock_bh(&txq->lock);
1186
1187 if (iwl_queue_space(&txq->q) > txq->q.low_mark)
1188 iwl_wake_queue(trans, txq);
1189 }
Eliad Peller7616f332014-11-20 17:33:43 +02001190
1191 if (q->read_ptr == q->write_ptr) {
1192 IWL_DEBUG_RPM(trans, "Q %d - last tx reclaimed\n", q->id);
Luca Coelhoc24c7f52016-03-30 20:59:27 +03001193 iwl_trans_unref(trans);
Eliad Peller7616f332014-11-20 17:33:43 +02001194 }
1195
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +02001196out:
Johannes Berg2bfb5092012-12-27 21:43:48 +01001197 spin_unlock_bh(&txq->lock);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001198}
1199
Eliad Peller7616f332014-11-20 17:33:43 +02001200static int iwl_pcie_set_cmd_in_flight(struct iwl_trans *trans,
1201 const struct iwl_host_cmd *cmd)
Eliad Peller804d4c52014-11-20 14:36:26 +02001202{
1203 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1204 int ret;
1205
1206 lockdep_assert_held(&trans_pcie->reg_lock);
1207
Eliad Peller7616f332014-11-20 17:33:43 +02001208 if (!(cmd->flags & CMD_SEND_IN_IDLE) &&
1209 !trans_pcie->ref_cmd_in_flight) {
1210 trans_pcie->ref_cmd_in_flight = true;
1211 IWL_DEBUG_RPM(trans, "set ref_cmd_in_flight - ref\n");
Luca Coelhoc24c7f52016-03-30 20:59:27 +03001212 iwl_trans_ref(trans);
Eliad Peller7616f332014-11-20 17:33:43 +02001213 }
1214
Eliad Peller804d4c52014-11-20 14:36:26 +02001215 /*
1216 * wake up the NIC to make sure that the firmware will see the host
1217 * command - we will let the NIC sleep once all the host commands
1218 * returned. This needs to be done only on NICs that have
1219 * apmg_wake_up_wa set.
1220 */
Ilan Peerfc8a3502015-05-13 14:34:07 +03001221 if (trans->cfg->base_params->apmg_wake_up_wa &&
1222 !trans_pcie->cmd_hold_nic_awake) {
Eliad Peller804d4c52014-11-20 14:36:26 +02001223 __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
1224 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Eliad Peller804d4c52014-11-20 14:36:26 +02001225
1226 ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
1227 CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
1228 (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
1229 CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP),
1230 15000);
1231 if (ret < 0) {
1232 __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
1233 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Eliad Peller804d4c52014-11-20 14:36:26 +02001234 IWL_ERR(trans, "Failed to wake NIC for hcmd\n");
1235 return -EIO;
1236 }
Ilan Peerfc8a3502015-05-13 14:34:07 +03001237 trans_pcie->cmd_hold_nic_awake = true;
Eliad Peller804d4c52014-11-20 14:36:26 +02001238 }
1239
1240 return 0;
1241}
1242
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001243/*
1244 * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
1245 *
1246 * When FW advances 'R' index, all entries between old and new 'R' index
1247 * need to be reclaimed. As result, some free space forms. If there is
1248 * enough free space (> low mark), wake the stack that feeds us.
1249 */
1250static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
1251{
1252 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1253 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
1254 struct iwl_queue *q = &txq->q;
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001255 unsigned long flags;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001256 int nfreed = 0;
1257
1258 lockdep_assert_held(&txq->lock);
1259
Johannes Berg83f32a42014-04-24 09:57:40 +02001260 if ((idx >= TFD_QUEUE_SIZE_MAX) || (!iwl_queue_used(q, idx))) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001261 IWL_ERR(trans,
1262 "%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 +02001263 __func__, txq_id, idx, TFD_QUEUE_SIZE_MAX,
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001264 q->write_ptr, q->read_ptr);
1265 return;
1266 }
1267
Johannes Berg83f32a42014-04-24 09:57:40 +02001268 for (idx = iwl_queue_inc_wrap(idx); q->read_ptr != idx;
1269 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr)) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001270
1271 if (nfreed++ > 0) {
1272 IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
1273 idx, q->write_ptr, q->read_ptr);
Liad Kaufman4c9706d2014-04-27 16:46:09 +03001274 iwl_force_nmi(trans);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001275 }
1276 }
1277
Eliad Peller804d4c52014-11-20 14:36:26 +02001278 if (q->read_ptr == q->write_ptr) {
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001279 spin_lock_irqsave(&trans_pcie->reg_lock, flags);
Eliad Peller804d4c52014-11-20 14:36:26 +02001280 iwl_pcie_clear_cmd_in_flight(trans);
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001281 spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
1282 }
1283
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001284 iwl_pcie_txq_progress(txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001285}
1286
1287static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001288 u16 txq_id)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001289{
Johannes Berg20d3b642012-05-16 22:54:29 +02001290 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001291 u32 tbl_dw_addr;
1292 u32 tbl_dw;
1293 u16 scd_q2ratid;
1294
1295 scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
1296
Emmanuel Grumbach105183b2011-08-25 23:11:02 -07001297 tbl_dw_addr = trans_pcie->scd_base_addr +
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001298 SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
1299
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001300 tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001301
1302 if (txq_id & 0x1)
1303 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
1304 else
1305 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
1306
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001307 iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001308
1309 return 0;
1310}
1311
Emmanuel Grumbachbd5f6a32013-04-28 14:05:22 +03001312/* Receiver address (actually, Rx station's index into station table),
1313 * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
1314#define BUILD_RAxTID(sta_id, tid) (((sta_id) << 4) + (tid))
1315
Johannes Bergfea77952014-08-01 11:58:47 +02001316void iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, u16 ssn,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001317 const struct iwl_trans_txq_scd_cfg *cfg,
1318 unsigned int wdg_timeout)
Johannes Berg70a18c52012-03-05 11:24:44 -08001319{
Johannes Berg9eae88f2012-03-15 13:26:52 -07001320 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001321 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
Johannes Bergd4578ea2014-08-01 12:17:40 +02001322 int fifo = -1;
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001323
Johannes Berg9eae88f2012-03-15 13:26:52 -07001324 if (test_and_set_bit(txq_id, trans_pcie->queue_used))
1325 WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001326
Sara Sharonae797852016-06-30 16:36:24 +03001327 if (cfg && trans->cfg->use_tfh)
1328 WARN_ONCE(1, "Expected no calls to SCD configuration");
1329
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001330 txq->wd_timeout = msecs_to_jiffies(wdg_timeout);
1331
Johannes Bergd4578ea2014-08-01 12:17:40 +02001332 if (cfg) {
1333 fifo = cfg->fifo;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001334
Avri Altman002a9e22014-07-24 19:25:10 +03001335 /* Disable the scheduler prior configuring the cmd queue */
Emmanuel Grumbach3a736bc2014-09-10 11:16:41 +03001336 if (txq_id == trans_pcie->cmd_queue &&
1337 trans_pcie->scd_set_active)
Avri Altman002a9e22014-07-24 19:25:10 +03001338 iwl_scd_enable_set_active(trans, 0);
1339
Johannes Bergd4578ea2014-08-01 12:17:40 +02001340 /* Stop this Tx queue before configuring it */
1341 iwl_scd_txq_set_inactive(trans, txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001342
Johannes Bergd4578ea2014-08-01 12:17:40 +02001343 /* Set this queue as a chain-building queue unless it is CMD */
1344 if (txq_id != trans_pcie->cmd_queue)
1345 iwl_scd_txq_set_chain(trans, txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001346
Johannes Berg64ba8932014-08-01 13:33:46 +02001347 if (cfg->aggregate) {
Johannes Bergd4578ea2014-08-01 12:17:40 +02001348 u16 ra_tid = BUILD_RAxTID(cfg->sta_id, cfg->tid);
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001349
Johannes Bergd4578ea2014-08-01 12:17:40 +02001350 /* Map receiver-address / traffic-ID to this queue */
1351 iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
Emmanuel Grumbachf4772522013-07-24 14:15:21 +03001352
Johannes Bergd4578ea2014-08-01 12:17:40 +02001353 /* enable aggregations for the queue */
1354 iwl_scd_txq_enable_agg(trans, txq_id);
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001355 txq->ampdu = true;
Johannes Bergd4578ea2014-08-01 12:17:40 +02001356 } else {
1357 /*
1358 * disable aggregations for the queue, this will also
1359 * make the ra_tid mapping configuration irrelevant
1360 * since it is now a non-AGG queue.
1361 */
1362 iwl_scd_txq_disable_agg(trans, txq_id);
1363
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001364 ssn = txq->q.read_ptr;
Johannes Bergd4578ea2014-08-01 12:17:40 +02001365 }
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001366 }
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001367
1368 /* Place first TFD at index corresponding to start sequence number.
1369 * Assumes that ssn_idx is valid (!= 0xFFF) */
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001370 txq->q.read_ptr = (ssn & 0xff);
1371 txq->q.write_ptr = (ssn & 0xff);
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +02001372 iwl_write_direct32(trans, HBUS_TARG_WRPTR,
1373 (ssn & 0xff) | (txq_id << 8));
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001374
Johannes Bergd4578ea2014-08-01 12:17:40 +02001375 if (cfg) {
1376 u8 frame_limit = cfg->frame_limit;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001377
Johannes Bergd4578ea2014-08-01 12:17:40 +02001378 iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
1379
1380 /* Set up Tx window size and frame limit for this queue */
1381 iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
1382 SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
1383 iwl_trans_write_mem32(trans,
1384 trans_pcie->scd_base_addr +
Johannes Berg9eae88f2012-03-15 13:26:52 -07001385 SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
1386 ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
Johannes Bergd4578ea2014-08-01 12:17:40 +02001387 SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
Johannes Berg9eae88f2012-03-15 13:26:52 -07001388 ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
Johannes Bergd4578ea2014-08-01 12:17:40 +02001389 SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001390
Johannes Bergd4578ea2014-08-01 12:17:40 +02001391 /* Set up status area in SRAM, map to Tx DMA/FIFO, activate */
1392 iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
1393 (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1394 (cfg->fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
1395 (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
1396 SCD_QUEUE_STTS_REG_MSK);
Avri Altman002a9e22014-07-24 19:25:10 +03001397
1398 /* enable the scheduler for this queue (only) */
Emmanuel Grumbach3a736bc2014-09-10 11:16:41 +03001399 if (txq_id == trans_pcie->cmd_queue &&
1400 trans_pcie->scd_set_active)
Avri Altman002a9e22014-07-24 19:25:10 +03001401 iwl_scd_enable_set_active(trans, BIT(txq_id));
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +02001402
1403 IWL_DEBUG_TX_QUEUES(trans,
1404 "Activate queue %d on FIFO %d WrPtr: %d\n",
1405 txq_id, fifo, ssn & 0xff);
1406 } else {
1407 IWL_DEBUG_TX_QUEUES(trans,
1408 "Activate queue %d WrPtr: %d\n",
1409 txq_id, ssn & 0xff);
Johannes Bergd4578ea2014-08-01 12:17:40 +02001410 }
1411
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001412 txq->active = true;
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001413}
1414
Liad Kaufman42db09c2016-05-02 14:01:14 +03001415void iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans *trans, u32 txq_id,
1416 bool shared_mode)
1417{
1418 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1419 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
1420
1421 txq->ampdu = !shared_mode;
1422}
1423
Sara Sharon8aacf4b2016-07-04 15:40:11 +03001424dma_addr_t iwl_trans_pcie_get_txq_byte_table(struct iwl_trans *trans, int txq)
1425{
1426 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1427
1428 return trans_pcie->scd_bc_tbls.dma +
1429 txq * sizeof(struct iwlagn_scd_bc_tbl);
1430}
1431
Johannes Bergd4578ea2014-08-01 12:17:40 +02001432void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id,
1433 bool configure_scd)
Emmanuel Grumbach288712a2011-08-25 23:11:25 -07001434{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001435 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach986ea6c2012-09-30 16:25:43 +02001436 u32 stts_addr = trans_pcie->scd_base_addr +
1437 SCD_TX_STTS_QUEUE_OFFSET(txq_id);
1438 static const u32 zero_val[4] = {};
Emmanuel Grumbach288712a2011-08-25 23:11:25 -07001439
Emmanuel Grumbache0b8d4052015-01-20 17:02:40 +02001440 trans_pcie->txq[txq_id].frozen_expiry_remainder = 0;
1441 trans_pcie->txq[txq_id].frozen = false;
1442
Emmanuel Grumbachfba1c622013-12-19 22:19:17 +02001443 /*
1444 * Upon HW Rfkill - we stop the device, and then stop the queues
1445 * in the op_mode. Just for the sake of the simplicity of the op_mode,
1446 * allow the op_mode to call txq_disable after it already called
1447 * stop_device.
1448 */
Johannes Berg9eae88f2012-03-15 13:26:52 -07001449 if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
Emmanuel Grumbachfba1c622013-12-19 22:19:17 +02001450 WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
1451 "queue %d not used", txq_id);
Johannes Berg9eae88f2012-03-15 13:26:52 -07001452 return;
Emmanuel Grumbachbc237732011-11-21 13:25:31 +02001453 }
1454
Sara Sharonae797852016-06-30 16:36:24 +03001455 if (configure_scd && trans->cfg->use_tfh)
1456 WARN_ONCE(1, "Expected no calls to SCD configuration");
1457
Johannes Bergd4578ea2014-08-01 12:17:40 +02001458 if (configure_scd) {
1459 iwl_scd_txq_set_inactive(trans, txq_id);
Emmanuel Grumbachac928f82012-10-14 16:36:36 +02001460
Johannes Bergd4578ea2014-08-01 12:17:40 +02001461 iwl_trans_write_mem(trans, stts_addr, (void *)zero_val,
1462 ARRAY_SIZE(zero_val));
1463 }
Emmanuel Grumbach986ea6c2012-09-30 16:25:43 +02001464
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001465 iwl_pcie_txq_unmap(trans, txq_id);
Johannes Berg68972c42013-06-11 19:05:27 +02001466 trans_pcie->txq[txq_id].ampdu = false;
Emmanuel Grumbach6c3fd3f2012-10-18 12:38:37 +02001467
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001468 IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001469}
1470
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001471/*************** HOST COMMAND QUEUE FUNCTIONS *****/
1472
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001473/*
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001474 * iwl_pcie_enqueue_hcmd - enqueue a uCode command
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001475 * @priv: device private data point
Eliad Pellere89044d2013-07-16 17:33:26 +03001476 * @cmd: a pointer to the ucode command structure
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001477 *
Eliad Pellere89044d2013-07-16 17:33:26 +03001478 * The function returns < 0 values to indicate the operation
1479 * failed. On success, it returns the index (>= 0) of command in the
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001480 * command queue.
1481 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001482static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
1483 struct iwl_host_cmd *cmd)
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001484{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001485 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001486 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001487 struct iwl_queue *q = &txq->q;
Johannes Bergc2acea82009-07-24 11:13:05 -07001488 struct iwl_device_cmd *out_cmd;
1489 struct iwl_cmd_meta *out_meta;
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001490 unsigned long flags;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001491 void *dup_buf = NULL;
Tomas Winklerf3674222008-08-04 16:00:44 +08001492 dma_addr_t phys_addr;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001493 int idx;
Sara Sharon8de437c2016-06-09 17:56:38 +03001494 u16 copy_size, cmd_size, tb0_size;
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001495 bool had_nocopy = false;
Aviya Erenfeldab021652015-06-09 16:45:52 +03001496 u8 group_id = iwl_cmd_groupid(cmd->id);
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001497 int i, ret;
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001498 u32 cmd_pos;
Johannes Berg1afbfb62013-02-26 11:32:26 +01001499 const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
1500 u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001501
Johannes Berg88742c92015-06-30 15:31:22 +02001502 if (WARN(!trans_pcie->wide_cmd_header &&
1503 group_id > IWL_ALWAYS_LONG_GROUP,
Aviya Erenfeldab021652015-06-09 16:45:52 +03001504 "unsupported wide command %#x\n", cmd->id))
1505 return -EINVAL;
1506
1507 if (group_id != 0) {
1508 copy_size = sizeof(struct iwl_cmd_header_wide);
1509 cmd_size = sizeof(struct iwl_cmd_header_wide);
1510 } else {
1511 copy_size = sizeof(struct iwl_cmd_header);
1512 cmd_size = sizeof(struct iwl_cmd_header);
1513 }
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001514
1515 /* need one for the header if the first is NOCOPY */
Johannes Berg1afbfb62013-02-26 11:32:26 +01001516 BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001517
Johannes Berg1afbfb62013-02-26 11:32:26 +01001518 for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
Johannes Berg8a964f42013-02-25 16:01:34 +01001519 cmddata[i] = cmd->data[i];
1520 cmdlen[i] = cmd->len[i];
1521
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001522 if (!cmd->len[i])
1523 continue;
Johannes Berg8a964f42013-02-25 16:01:34 +01001524
Sara Sharon8de437c2016-06-09 17:56:38 +03001525 /* need at least IWL_FIRST_TB_SIZE copied */
1526 if (copy_size < IWL_FIRST_TB_SIZE) {
1527 int copy = IWL_FIRST_TB_SIZE - copy_size;
Johannes Berg8a964f42013-02-25 16:01:34 +01001528
1529 if (copy > cmdlen[i])
1530 copy = cmdlen[i];
1531 cmdlen[i] -= copy;
1532 cmddata[i] += copy;
1533 copy_size += copy;
1534 }
1535
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001536 if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
1537 had_nocopy = true;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001538 if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
1539 idx = -EINVAL;
1540 goto free_dup_buf;
1541 }
1542 } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
1543 /*
1544 * This is also a chunk that isn't copied
1545 * to the static buffer so set had_nocopy.
1546 */
1547 had_nocopy = true;
1548
1549 /* only allowed once */
1550 if (WARN_ON(dup_buf)) {
1551 idx = -EINVAL;
1552 goto free_dup_buf;
1553 }
1554
Johannes Berg8a964f42013-02-25 16:01:34 +01001555 dup_buf = kmemdup(cmddata[i], cmdlen[i],
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001556 GFP_ATOMIC);
1557 if (!dup_buf)
1558 return -ENOMEM;
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001559 } else {
1560 /* NOCOPY must not be followed by normal! */
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001561 if (WARN_ON(had_nocopy)) {
1562 idx = -EINVAL;
1563 goto free_dup_buf;
1564 }
Johannes Berg8a964f42013-02-25 16:01:34 +01001565 copy_size += cmdlen[i];
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001566 }
1567 cmd_size += cmd->len[i];
1568 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001569
Johannes Berg3e41ace2011-04-18 09:12:37 -07001570 /*
1571 * If any of the command structures end up being larger than
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001572 * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
1573 * allocated into separate TFDs, then we will need to
1574 * increase the size of the buffers.
Johannes Berg3e41ace2011-04-18 09:12:37 -07001575 */
Johannes Berg2a79e452012-09-26 13:32:13 +02001576 if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
1577 "Command %s (%#x) is too large (%d bytes)\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001578 iwl_get_cmd_string(trans, cmd->id),
1579 cmd->id, copy_size)) {
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001580 idx = -EINVAL;
1581 goto free_dup_buf;
1582 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001583
Johannes Berg015c15e2012-03-05 11:24:24 -08001584 spin_lock_bh(&txq->lock);
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001585
Johannes Bergc2acea82009-07-24 11:13:05 -07001586 if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
Johannes Berg015c15e2012-03-05 11:24:24 -08001587 spin_unlock_bh(&txq->lock);
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001588
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001589 IWL_ERR(trans, "No space in command queue\n");
Johannes Berg0e781842012-03-06 13:30:49 -08001590 iwl_op_mode_cmd_queue_full(trans->op_mode);
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001591 idx = -ENOSPC;
1592 goto free_dup_buf;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001593 }
1594
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001595 idx = get_cmd_index(q, q->write_ptr);
Johannes Bergbf8440e2012-03-19 17:12:06 +01001596 out_cmd = txq->entries[idx].cmd;
1597 out_meta = &txq->entries[idx].meta;
Johannes Bergc2acea82009-07-24 11:13:05 -07001598
Daniel C Halperin8ce73f32009-07-31 14:28:06 -07001599 memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
Johannes Bergc2acea82009-07-24 11:13:05 -07001600 if (cmd->flags & CMD_WANT_SKB)
1601 out_meta->source = cmd;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001602
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001603 /* set up the header */
Aviya Erenfeldab021652015-06-09 16:45:52 +03001604 if (group_id != 0) {
1605 out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
1606 out_cmd->hdr_wide.group_id = group_id;
1607 out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
1608 out_cmd->hdr_wide.length =
1609 cpu_to_le16(cmd_size -
1610 sizeof(struct iwl_cmd_header_wide));
1611 out_cmd->hdr_wide.reserved = 0;
1612 out_cmd->hdr_wide.sequence =
1613 cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
1614 INDEX_TO_SEQ(q->write_ptr));
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001615
Aviya Erenfeldab021652015-06-09 16:45:52 +03001616 cmd_pos = sizeof(struct iwl_cmd_header_wide);
1617 copy_size = sizeof(struct iwl_cmd_header_wide);
1618 } else {
1619 out_cmd->hdr.cmd = iwl_cmd_opcode(cmd->id);
1620 out_cmd->hdr.sequence =
1621 cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
1622 INDEX_TO_SEQ(q->write_ptr));
1623 out_cmd->hdr.group_id = 0;
1624
1625 cmd_pos = sizeof(struct iwl_cmd_header);
1626 copy_size = sizeof(struct iwl_cmd_header);
1627 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001628
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001629 /* and copy the data that needs to be copied */
Johannes Berg1afbfb62013-02-26 11:32:26 +01001630 for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
Johannes Berg4d075002014-04-24 10:41:31 +02001631 int copy;
Johannes Berg8a964f42013-02-25 16:01:34 +01001632
Emmanuel Grumbachcc904c72013-03-14 08:35:06 +02001633 if (!cmd->len[i])
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001634 continue;
Johannes Berg8a964f42013-02-25 16:01:34 +01001635
Johannes Berg4d075002014-04-24 10:41:31 +02001636 /* copy everything if not nocopy/dup */
1637 if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1638 IWL_HCMD_DFL_DUP))) {
1639 copy = cmd->len[i];
1640
1641 memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1642 cmd_pos += copy;
1643 copy_size += copy;
1644 continue;
1645 }
1646
1647 /*
Sara Sharon8de437c2016-06-09 17:56:38 +03001648 * Otherwise we need at least IWL_FIRST_TB_SIZE copied
1649 * in total (for bi-directional DMA), but copy up to what
Johannes Berg4d075002014-04-24 10:41:31 +02001650 * we can fit into the payload for debug dump purposes.
1651 */
1652 copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
1653
1654 memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1655 cmd_pos += copy;
1656
1657 /* However, treat copy_size the proper way, we need it below */
Sara Sharon8de437c2016-06-09 17:56:38 +03001658 if (copy_size < IWL_FIRST_TB_SIZE) {
1659 copy = IWL_FIRST_TB_SIZE - copy_size;
Johannes Berg8a964f42013-02-25 16:01:34 +01001660
1661 if (copy > cmd->len[i])
1662 copy = cmd->len[i];
Johannes Berg8a964f42013-02-25 16:01:34 +01001663 copy_size += copy;
1664 }
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001665 }
1666
Johannes Bergd9fb6462012-03-26 08:23:39 -07001667 IWL_DEBUG_HC(trans,
Aviya Erenfeldab021652015-06-09 16:45:52 +03001668 "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001669 iwl_get_cmd_string(trans, cmd->id),
Aviya Erenfeldab021652015-06-09 16:45:52 +03001670 group_id, out_cmd->hdr.cmd,
1671 le16_to_cpu(out_cmd->hdr.sequence),
Johannes Berg20d3b642012-05-16 22:54:29 +02001672 cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001673
Sara Sharon8de437c2016-06-09 17:56:38 +03001674 /* start the TFD with the minimum copy bytes */
1675 tb0_size = min_t(int, copy_size, IWL_FIRST_TB_SIZE);
1676 memcpy(&txq->first_tb_bufs[idx], &out_cmd->hdr, tb0_size);
Johannes Berg38c0f3342013-02-27 13:18:50 +01001677 iwl_pcie_txq_build_tfd(trans, txq,
Sara Sharon8de437c2016-06-09 17:56:38 +03001678 iwl_pcie_get_first_tb_dma(txq, idx),
1679 tb0_size, true);
Johannes Berg8a964f42013-02-25 16:01:34 +01001680
Johannes Berg38c0f3342013-02-27 13:18:50 +01001681 /* map first command fragment, if any remains */
Sara Sharon8de437c2016-06-09 17:56:38 +03001682 if (copy_size > tb0_size) {
Johannes Berg38c0f3342013-02-27 13:18:50 +01001683 phys_addr = dma_map_single(trans->dev,
Sara Sharon8de437c2016-06-09 17:56:38 +03001684 ((u8 *)&out_cmd->hdr) + tb0_size,
1685 copy_size - tb0_size,
Johannes Berg38c0f3342013-02-27 13:18:50 +01001686 DMA_TO_DEVICE);
1687 if (dma_mapping_error(trans->dev, phys_addr)) {
Sara Sharon6983ba62016-06-26 13:17:56 +03001688 iwl_pcie_tfd_unmap(trans, out_meta, txq, q->write_ptr);
Johannes Berg38c0f3342013-02-27 13:18:50 +01001689 idx = -ENOMEM;
1690 goto out;
1691 }
1692
1693 iwl_pcie_txq_build_tfd(trans, txq, phys_addr,
Sara Sharon8de437c2016-06-09 17:56:38 +03001694 copy_size - tb0_size, false);
Johannes Berg2c46f722011-04-28 07:27:10 -07001695 }
1696
Johannes Berg8a964f42013-02-25 16:01:34 +01001697 /* map the remaining (adjusted) nocopy/dup fragments */
Johannes Berg1afbfb62013-02-26 11:32:26 +01001698 for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
Johannes Berg8a964f42013-02-25 16:01:34 +01001699 const void *data = cmddata[i];
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001700
Johannes Berg8a964f42013-02-25 16:01:34 +01001701 if (!cmdlen[i])
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001702 continue;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001703 if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1704 IWL_HCMD_DFL_DUP)))
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001705 continue;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001706 if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
1707 data = dup_buf;
1708 phys_addr = dma_map_single(trans->dev, (void *)data,
Johannes Berg98891752013-02-26 11:28:19 +01001709 cmdlen[i], DMA_TO_DEVICE);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001710 if (dma_mapping_error(trans->dev, phys_addr)) {
Sara Sharon6983ba62016-06-26 13:17:56 +03001711 iwl_pcie_tfd_unmap(trans, out_meta, txq, q->write_ptr);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001712 idx = -ENOMEM;
1713 goto out;
1714 }
1715
Johannes Berg6d6e68f2014-04-23 19:00:56 +02001716 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], false);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001717 }
Reinette Chatredf833b12009-04-21 10:55:48 -07001718
Sara Sharon3cd19802016-06-23 16:31:40 +03001719 BUILD_BUG_ON(IWL_TFH_NUM_TBS > sizeof(out_meta->tbs) * BITS_PER_BYTE);
Emmanuel Grumbachafaf6b52011-07-08 08:46:09 -07001720 out_meta->flags = cmd->flags;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001721 if (WARN_ON_ONCE(txq->entries[idx].free_buf))
Johannes Berg5d4185a2014-09-09 21:16:06 +02001722 kzfree(txq->entries[idx].free_buf);
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001723 txq->entries[idx].free_buf = dup_buf;
Johannes Berg2c46f722011-04-28 07:27:10 -07001724
Aviya Erenfeldab021652015-06-09 16:45:52 +03001725 trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
Reinette Chatredf833b12009-04-21 10:55:48 -07001726
Johannes Berg7c5ba4a2012-04-09 17:46:54 -07001727 /* start timer if queue currently empty */
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001728 if (q->read_ptr == q->write_ptr && txq->wd_timeout)
1729 mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
Johannes Berg7c5ba4a2012-04-09 17:46:54 -07001730
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001731 spin_lock_irqsave(&trans_pcie->reg_lock, flags);
Eliad Peller7616f332014-11-20 17:33:43 +02001732 ret = iwl_pcie_set_cmd_in_flight(trans, cmd);
Eliad Peller804d4c52014-11-20 14:36:26 +02001733 if (ret < 0) {
1734 idx = ret;
1735 spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
1736 goto out;
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001737 }
1738
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001739 /* Increment and update queue's write index */
Johannes Berg83f32a42014-04-24 09:57:40 +02001740 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001741 iwl_pcie_txq_inc_wr_ptr(trans, txq);
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001742
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001743 spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
1744
Johannes Berg2c46f722011-04-28 07:27:10 -07001745 out:
Johannes Berg015c15e2012-03-05 11:24:24 -08001746 spin_unlock_bh(&txq->lock);
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001747 free_dup_buf:
1748 if (idx < 0)
1749 kfree(dup_buf);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001750 return idx;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001751}
1752
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001753/*
1754 * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
Tomas Winkler17b88922008-05-29 16:35:12 +08001755 * @rxb: Rx buffer to reclaim
Tomas Winkler17b88922008-05-29 16:35:12 +08001756 */
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001757void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
Johannes Bergf7e64692015-06-23 21:58:17 +02001758 struct iwl_rx_cmd_buffer *rxb)
Tomas Winkler17b88922008-05-29 16:35:12 +08001759{
Zhu Yi2f301222009-10-09 17:19:45 +08001760 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler17b88922008-05-29 16:35:12 +08001761 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
Sharon Dvir39bdb172015-10-15 18:18:09 +03001762 u8 group_id = iwl_cmd_groupid(pkt->hdr.group_id);
1763 u32 cmd_id;
Tomas Winkler17b88922008-05-29 16:35:12 +08001764 int txq_id = SEQ_TO_QUEUE(sequence);
1765 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler17b88922008-05-29 16:35:12 +08001766 int cmd_index;
Johannes Bergc2acea82009-07-24 11:13:05 -07001767 struct iwl_device_cmd *cmd;
1768 struct iwl_cmd_meta *meta;
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001769 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001770 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
Tomas Winkler17b88922008-05-29 16:35:12 +08001771
1772 /* If a Tx command is being handled and it isn't in the actual
1773 * command queue then there a command routing bug has been introduced
1774 * in the queue management code. */
Meenakshi Venkataramanc6f600f2012-03-08 11:29:12 -08001775 if (WARN(txq_id != trans_pcie->cmd_queue,
Johannes Berg13bb9482010-08-23 10:46:33 +02001776 "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
Johannes Berg20d3b642012-05-16 22:54:29 +02001777 txq_id, trans_pcie->cmd_queue, sequence,
1778 trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr,
1779 trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) {
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -07001780 iwl_print_hex_error(trans, pkt, 32);
Johannes Berg55d6a3c2008-09-23 19:18:43 +02001781 return;
Winkler, Tomas01ef93232008-11-07 09:58:45 -08001782 }
Tomas Winkler17b88922008-05-29 16:35:12 +08001783
Johannes Berg2bfb5092012-12-27 21:43:48 +01001784 spin_lock_bh(&txq->lock);
Johannes Berg015c15e2012-03-05 11:24:24 -08001785
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001786 cmd_index = get_cmd_index(&txq->q, index);
Johannes Bergbf8440e2012-03-19 17:12:06 +01001787 cmd = txq->entries[cmd_index].cmd;
1788 meta = &txq->entries[cmd_index].meta;
Sharon Dvir39bdb172015-10-15 18:18:09 +03001789 cmd_id = iwl_cmd_id(cmd->hdr.cmd, group_id, 0);
Tomas Winkler17b88922008-05-29 16:35:12 +08001790
Sara Sharon6983ba62016-06-26 13:17:56 +03001791 iwl_pcie_tfd_unmap(trans, meta, txq, index);
Reinette Chatrec33de622009-10-30 14:36:10 -07001792
Tomas Winkler17b88922008-05-29 16:35:12 +08001793 /* Input error checking is done when commands are added to queue. */
Johannes Bergc2acea82009-07-24 11:13:05 -07001794 if (meta->flags & CMD_WANT_SKB) {
Johannes Berg48a2d662012-03-05 11:24:39 -08001795 struct page *p = rxb_steal_page(rxb);
Stanislaw Gruszka2624e962011-04-20 16:02:58 +02001796
Johannes Berg65b94a42012-03-05 11:24:38 -08001797 meta->source->resp_pkt = pkt;
1798 meta->source->_rx_page_addr = (unsigned long)page_address(p);
Johannes Bergb2cf4102012-04-09 17:46:51 -07001799 meta->source->_rx_page_order = trans_pcie->rx_page_order;
Stanislaw Gruszka2624e962011-04-20 16:02:58 +02001800 }
Tomas Winkler17b88922008-05-29 16:35:12 +08001801
Emmanuel Grumbachdcbb4742015-11-24 15:17:37 +02001802 if (meta->flags & CMD_WANT_ASYNC_CALLBACK)
1803 iwl_op_mode_async_cb(trans->op_mode, cmd);
1804
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001805 iwl_pcie_cmdq_reclaim(trans, txq_id, index);
Tomas Winkler17b88922008-05-29 16:35:12 +08001806
Johannes Bergc2acea82009-07-24 11:13:05 -07001807 if (!(meta->flags & CMD_ASYNC)) {
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001808 if (!test_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status)) {
Wey-Yi Guy05c89b92011-10-10 07:26:48 -07001809 IWL_WARN(trans,
1810 "HCMD_ACTIVE already clear for command %s\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001811 iwl_get_cmd_string(trans, cmd_id));
Wey-Yi Guy05c89b92011-10-10 07:26:48 -07001812 }
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001813 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001814 IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001815 iwl_get_cmd_string(trans, cmd_id));
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001816 wake_up(&trans_pcie->wait_command_queue);
Tomas Winkler17b88922008-05-29 16:35:12 +08001817 }
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001818
Luciano Coelho4cbb8e502015-08-18 16:02:38 +03001819 if (meta->flags & CMD_MAKE_TRANS_IDLE) {
1820 IWL_DEBUG_INFO(trans, "complete %s - mark trans as idle\n",
1821 iwl_get_cmd_string(trans, cmd->hdr.cmd));
1822 set_bit(STATUS_TRANS_IDLE, &trans->status);
1823 wake_up(&trans_pcie->d0i3_waitq);
1824 }
1825
1826 if (meta->flags & CMD_WAKE_UP_TRANS) {
1827 IWL_DEBUG_INFO(trans, "complete %s - clear trans idle flag\n",
1828 iwl_get_cmd_string(trans, cmd->hdr.cmd));
1829 clear_bit(STATUS_TRANS_IDLE, &trans->status);
1830 wake_up(&trans_pcie->d0i3_waitq);
1831 }
1832
Zhu Yidd487442010-03-22 02:28:41 -07001833 meta->flags = 0;
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001834
Johannes Berg2bfb5092012-12-27 21:43:48 +01001835 spin_unlock_bh(&txq->lock);
Tomas Winkler17b88922008-05-29 16:35:12 +08001836}
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001837
Johannes Berg9439eac2013-10-09 09:59:25 +02001838#define HOST_COMPLETE_TIMEOUT (2 * HZ)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001839
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001840static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
1841 struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001842{
1843 int ret;
1844
1845 /* An asynchronous command can not expect an SKB to be set. */
1846 if (WARN_ON(cmd->flags & CMD_WANT_SKB))
1847 return -EINVAL;
1848
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001849 ret = iwl_pcie_enqueue_hcmd(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001850 if (ret < 0) {
Johannes Berg721c32f2012-03-06 13:30:40 -08001851 IWL_ERR(trans,
Todd Previteb36b1102011-11-10 06:55:02 -08001852 "Error sending %s: enqueue_hcmd failed: %d\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001853 iwl_get_cmd_string(trans, cmd->id), ret);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001854 return ret;
1855 }
1856 return 0;
1857}
1858
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001859static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
1860 struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001861{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001862 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001863 int cmd_idx;
1864 int ret;
1865
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001866 IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001867 iwl_get_cmd_string(trans, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001868
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001869 if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE,
1870 &trans->status),
Johannes Bergbcbb8c92013-10-28 15:50:55 +01001871 "Command %s: a command is already active!\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001872 iwl_get_cmd_string(trans, cmd->id)))
Johannes Berg2cc39c92012-03-06 13:30:41 -08001873 return -EIO;
Johannes Berg2cc39c92012-03-06 13:30:41 -08001874
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001875 IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001876 iwl_get_cmd_string(trans, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001877
Luca Coelho71b12302016-03-11 12:12:16 +02001878 if (pm_runtime_suspended(&trans_pcie->pci_dev->dev)) {
1879 ret = wait_event_timeout(trans_pcie->d0i3_waitq,
1880 pm_runtime_active(&trans_pcie->pci_dev->dev),
1881 msecs_to_jiffies(IWL_TRANS_IDLE_TIMEOUT));
1882 if (!ret) {
1883 IWL_ERR(trans, "Timeout exiting D0i3 before hcmd\n");
1884 return -ETIMEDOUT;
1885 }
1886 }
1887
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001888 cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001889 if (cmd_idx < 0) {
1890 ret = cmd_idx;
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001891 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
Johannes Berg721c32f2012-03-06 13:30:40 -08001892 IWL_ERR(trans,
Todd Previteb36b1102011-11-10 06:55:02 -08001893 "Error sending %s: enqueue_hcmd failed: %d\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001894 iwl_get_cmd_string(trans, cmd->id), ret);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001895 return ret;
1896 }
1897
Emmanuel Grumbachb9439492013-12-22 15:09:40 +02001898 ret = wait_event_timeout(trans_pcie->wait_command_queue,
1899 !test_bit(STATUS_SYNC_HCMD_ACTIVE,
1900 &trans->status),
1901 HOST_COMPLETE_TIMEOUT);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001902 if (!ret) {
Johannes Berg6dde8c42013-10-31 18:30:38 +01001903 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
1904 struct iwl_queue *q = &txq->q;
Wey-Yi Guyd10630a2011-10-10 07:26:46 -07001905
Johannes Berg6dde8c42013-10-31 18:30:38 +01001906 IWL_ERR(trans, "Error sending %s: time out after %dms.\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001907 iwl_get_cmd_string(trans, cmd->id),
Johannes Berg6dde8c42013-10-31 18:30:38 +01001908 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001909
Johannes Berg6dde8c42013-10-31 18:30:38 +01001910 IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n",
1911 q->read_ptr, q->write_ptr);
Wey-Yi Guyd10630a2011-10-10 07:26:46 -07001912
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001913 clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
Johannes Berg6dde8c42013-10-31 18:30:38 +01001914 IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001915 iwl_get_cmd_string(trans, cmd->id));
Johannes Berg6dde8c42013-10-31 18:30:38 +01001916 ret = -ETIMEDOUT;
Emmanuel Grumbach42550a52013-09-11 14:16:20 +03001917
Liad Kaufman4c9706d2014-04-27 16:46:09 +03001918 iwl_force_nmi(trans);
Arik Nemtsov2a988e92013-12-01 13:50:40 +02001919 iwl_trans_fw_error(trans);
Emmanuel Grumbach42550a52013-09-11 14:16:20 +03001920
Johannes Berg6dde8c42013-10-31 18:30:38 +01001921 goto cancel;
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001922 }
1923
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001924 if (test_bit(STATUS_FW_ERROR, &trans->status)) {
Johannes Bergd18aa872012-11-06 16:36:21 +01001925 IWL_ERR(trans, "FW error in SYNC CMD %s\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001926 iwl_get_cmd_string(trans, cmd->id));
Johannes Bergb656fa32013-05-03 11:56:17 +02001927 dump_stack();
Johannes Bergd18aa872012-11-06 16:36:21 +01001928 ret = -EIO;
1929 goto cancel;
1930 }
1931
Eran Harary1094fa22013-06-02 12:40:34 +03001932 if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001933 test_bit(STATUS_RFKILL, &trans->status)) {
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001934 IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
1935 ret = -ERFKILL;
1936 goto cancel;
1937 }
1938
Johannes Berg65b94a42012-03-05 11:24:38 -08001939 if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001940 IWL_ERR(trans, "Error: Response NULL in '%s'\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001941 iwl_get_cmd_string(trans, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001942 ret = -EIO;
1943 goto cancel;
1944 }
1945
1946 return 0;
1947
1948cancel:
1949 if (cmd->flags & CMD_WANT_SKB) {
1950 /*
1951 * Cancel the CMD_WANT_SKB flag for the cmd in the
1952 * TX cmd queue. Otherwise in case the cmd comes
1953 * in later, it will possibly set an invalid
1954 * address (cmd->meta.source).
1955 */
Johannes Bergbf8440e2012-03-19 17:12:06 +01001956 trans_pcie->txq[trans_pcie->cmd_queue].
1957 entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001958 }
Emmanuel Grumbach9cac4942011-11-10 06:55:20 -08001959
Johannes Berg65b94a42012-03-05 11:24:38 -08001960 if (cmd->resp_pkt) {
1961 iwl_free_resp(cmd);
1962 cmd->resp_pkt = NULL;
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001963 }
1964
1965 return ret;
1966}
1967
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001968int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001969{
Eran Harary4f593342013-05-13 07:53:26 +03001970 if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
Arik Nemtsoveb7ff772013-12-01 12:30:38 +02001971 test_bit(STATUS_RFKILL, &trans->status)) {
Emmanuel Grumbach754d7d92013-03-13 22:16:20 +02001972 IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
1973 cmd->id);
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001974 return -ERFKILL;
Emmanuel Grumbach754d7d92013-03-13 22:16:20 +02001975 }
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001976
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001977 if (cmd->flags & CMD_ASYNC)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001978 return iwl_pcie_send_hcmd_async(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001979
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001980 /* We still can fail on RFKILL that can be asserted while we wait */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001981 return iwl_pcie_send_hcmd_sync(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001982}
1983
Emmanuel Grumbach3a0b2a42015-10-14 22:10:50 +03001984static int iwl_fill_data_tbs(struct iwl_trans *trans, struct sk_buff *skb,
1985 struct iwl_txq *txq, u8 hdr_len,
1986 struct iwl_cmd_meta *out_meta,
1987 struct iwl_device_cmd *dev_cmd, u16 tb1_len)
1988{
Sara Sharon6983ba62016-06-26 13:17:56 +03001989 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach3a0b2a42015-10-14 22:10:50 +03001990 struct iwl_queue *q = &txq->q;
1991 u16 tb2_len;
1992 int i;
1993
1994 /*
1995 * Set up TFD's third entry to point directly to remainder
1996 * of skb's head, if any
1997 */
1998 tb2_len = skb_headlen(skb) - hdr_len;
1999
2000 if (tb2_len > 0) {
2001 dma_addr_t tb2_phys = dma_map_single(trans->dev,
2002 skb->data + hdr_len,
2003 tb2_len, DMA_TO_DEVICE);
2004 if (unlikely(dma_mapping_error(trans->dev, tb2_phys))) {
Sara Sharon6983ba62016-06-26 13:17:56 +03002005 iwl_pcie_tfd_unmap(trans, out_meta, txq, q->write_ptr);
Emmanuel Grumbach3a0b2a42015-10-14 22:10:50 +03002006 return -EINVAL;
2007 }
2008 iwl_pcie_txq_build_tfd(trans, txq, tb2_phys, tb2_len, false);
2009 }
2010
2011 /* set up the remaining entries to point to the data */
2012 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2013 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2014 dma_addr_t tb_phys;
2015 int tb_idx;
2016
2017 if (!skb_frag_size(frag))
2018 continue;
2019
2020 tb_phys = skb_frag_dma_map(trans->dev, frag, 0,
2021 skb_frag_size(frag), DMA_TO_DEVICE);
2022
2023 if (unlikely(dma_mapping_error(trans->dev, tb_phys))) {
Sara Sharon6983ba62016-06-26 13:17:56 +03002024 iwl_pcie_tfd_unmap(trans, out_meta, txq, q->write_ptr);
Emmanuel Grumbach3a0b2a42015-10-14 22:10:50 +03002025 return -EINVAL;
2026 }
2027 tb_idx = iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
2028 skb_frag_size(frag), false);
2029
Sara Sharon3cd19802016-06-23 16:31:40 +03002030 out_meta->tbs |= BIT(tb_idx);
Emmanuel Grumbach3a0b2a42015-10-14 22:10:50 +03002031 }
2032
2033 trace_iwlwifi_dev_tx(trans->dev, skb,
Sara Sharon6983ba62016-06-26 13:17:56 +03002034 iwl_pcie_get_tfd(trans_pcie, txq, q->write_ptr),
2035 trans_pcie->tfd_size,
Sara Sharon8de437c2016-06-09 17:56:38 +03002036 &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len,
Emmanuel Grumbach3a0b2a42015-10-14 22:10:50 +03002037 skb->data + hdr_len, tb2_len);
2038 trace_iwlwifi_dev_tx_data(trans->dev, skb,
2039 hdr_len, skb->len - hdr_len);
2040 return 0;
2041}
2042
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +03002043#ifdef CONFIG_INET
2044static struct iwl_tso_hdr_page *
2045get_page_hdr(struct iwl_trans *trans, size_t len)
2046{
2047 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2048 struct iwl_tso_hdr_page *p = this_cpu_ptr(trans_pcie->tso_hdr_page);
2049
2050 if (!p->page)
2051 goto alloc;
2052
2053 /* enough room on this page */
2054 if (p->pos + len < (u8 *)page_address(p->page) + PAGE_SIZE)
2055 return p;
2056
2057 /* We don't have enough room on this page, get a new one. */
2058 __free_page(p->page);
2059
2060alloc:
2061 p->page = alloc_page(GFP_ATOMIC);
2062 if (!p->page)
2063 return NULL;
2064 p->pos = page_address(p->page);
2065 return p;
2066}
2067
2068static void iwl_compute_pseudo_hdr_csum(void *iph, struct tcphdr *tcph,
2069 bool ipv6, unsigned int len)
2070{
2071 if (ipv6) {
2072 struct ipv6hdr *iphv6 = iph;
2073
2074 tcph->check = ~csum_ipv6_magic(&iphv6->saddr, &iphv6->daddr,
2075 len + tcph->doff * 4,
2076 IPPROTO_TCP, 0);
2077 } else {
2078 struct iphdr *iphv4 = iph;
2079
2080 ip_send_check(iphv4);
2081 tcph->check = ~csum_tcpudp_magic(iphv4->saddr, iphv4->daddr,
2082 len + tcph->doff * 4,
2083 IPPROTO_TCP, 0);
2084 }
2085}
2086
2087static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
2088 struct iwl_txq *txq, u8 hdr_len,
2089 struct iwl_cmd_meta *out_meta,
2090 struct iwl_device_cmd *dev_cmd, u16 tb1_len)
2091{
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +03002092 struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
2093 struct ieee80211_hdr *hdr = (void *)skb->data;
2094 unsigned int snap_ip_tcp_hdrlen, ip_hdrlen, total_len, hdr_room;
2095 unsigned int mss = skb_shinfo(skb)->gso_size;
2096 struct iwl_queue *q = &txq->q;
2097 u16 length, iv_len, amsdu_pad;
2098 u8 *start_hdr;
2099 struct iwl_tso_hdr_page *hdr_page;
Johannes Berg21cb3222016-06-21 13:11:48 +02002100 struct page **page_ptr;
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +03002101 int ret;
2102 struct tso_t tso;
2103
2104 /* if the packet is protected, then it must be CCMP or GCMP */
2105 BUILD_BUG_ON(IEEE80211_CCMP_HDR_LEN != IEEE80211_GCMP_HDR_LEN);
2106 iv_len = ieee80211_has_protected(hdr->frame_control) ?
2107 IEEE80211_CCMP_HDR_LEN : 0;
2108
2109 trace_iwlwifi_dev_tx(trans->dev, skb,
Sara Sharon6983ba62016-06-26 13:17:56 +03002110 iwl_pcie_get_tfd(trans_pcie, txq, q->write_ptr),
2111 trans_pcie->tfd_size,
Sara Sharon8de437c2016-06-09 17:56:38 +03002112 &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len,
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +03002113 NULL, 0);
2114
2115 ip_hdrlen = skb_transport_header(skb) - skb_network_header(skb);
2116 snap_ip_tcp_hdrlen = 8 + ip_hdrlen + tcp_hdrlen(skb);
2117 total_len = skb->len - snap_ip_tcp_hdrlen - hdr_len - iv_len;
2118 amsdu_pad = 0;
2119
2120 /* total amount of header we may need for this A-MSDU */
2121 hdr_room = DIV_ROUND_UP(total_len, mss) *
2122 (3 + snap_ip_tcp_hdrlen + sizeof(struct ethhdr)) + iv_len;
2123
2124 /* Our device supports 9 segments at most, it will fit in 1 page */
2125 hdr_page = get_page_hdr(trans, hdr_room);
2126 if (!hdr_page)
2127 return -ENOMEM;
2128
2129 get_page(hdr_page->page);
2130 start_hdr = hdr_page->pos;
Johannes Berg21cb3222016-06-21 13:11:48 +02002131 page_ptr = (void *)((u8 *)skb->cb + trans_pcie->page_offs);
2132 *page_ptr = hdr_page->page;
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +03002133 memcpy(hdr_page->pos, skb->data + hdr_len, iv_len);
2134 hdr_page->pos += iv_len;
2135
2136 /*
2137 * Pull the ieee80211 header + IV to be able to use TSO core,
2138 * we will restore it for the tx_status flow.
2139 */
2140 skb_pull(skb, hdr_len + iv_len);
2141
2142 tso_start(skb, &tso);
2143
2144 while (total_len) {
2145 /* this is the data left for this subframe */
2146 unsigned int data_left =
2147 min_t(unsigned int, mss, total_len);
2148 struct sk_buff *csum_skb = NULL;
2149 unsigned int hdr_tb_len;
2150 dma_addr_t hdr_tb_phys;
2151 struct tcphdr *tcph;
2152 u8 *iph;
2153
2154 total_len -= data_left;
2155
2156 memset(hdr_page->pos, 0, amsdu_pad);
2157 hdr_page->pos += amsdu_pad;
2158 amsdu_pad = (4 - (sizeof(struct ethhdr) + snap_ip_tcp_hdrlen +
2159 data_left)) & 0x3;
2160 ether_addr_copy(hdr_page->pos, ieee80211_get_DA(hdr));
2161 hdr_page->pos += ETH_ALEN;
2162 ether_addr_copy(hdr_page->pos, ieee80211_get_SA(hdr));
2163 hdr_page->pos += ETH_ALEN;
2164
2165 length = snap_ip_tcp_hdrlen + data_left;
2166 *((__be16 *)hdr_page->pos) = cpu_to_be16(length);
2167 hdr_page->pos += sizeof(length);
2168
2169 /*
2170 * This will copy the SNAP as well which will be considered
2171 * as MAC header.
2172 */
2173 tso_build_hdr(skb, hdr_page->pos, &tso, data_left, !total_len);
2174 iph = hdr_page->pos + 8;
2175 tcph = (void *)(iph + ip_hdrlen);
2176
2177 /* For testing on current hardware only */
2178 if (trans_pcie->sw_csum_tx) {
2179 csum_skb = alloc_skb(data_left + tcp_hdrlen(skb),
2180 GFP_ATOMIC);
2181 if (!csum_skb) {
2182 ret = -ENOMEM;
2183 goto out_unmap;
2184 }
2185
2186 iwl_compute_pseudo_hdr_csum(iph, tcph,
2187 skb->protocol ==
2188 htons(ETH_P_IPV6),
2189 data_left);
2190
2191 memcpy(skb_put(csum_skb, tcp_hdrlen(skb)),
2192 tcph, tcp_hdrlen(skb));
2193 skb_set_transport_header(csum_skb, 0);
2194 csum_skb->csum_start =
2195 (unsigned char *)tcp_hdr(csum_skb) -
2196 csum_skb->head;
2197 }
2198
2199 hdr_page->pos += snap_ip_tcp_hdrlen;
2200
2201 hdr_tb_len = hdr_page->pos - start_hdr;
2202 hdr_tb_phys = dma_map_single(trans->dev, start_hdr,
2203 hdr_tb_len, DMA_TO_DEVICE);
2204 if (unlikely(dma_mapping_error(trans->dev, hdr_tb_phys))) {
2205 dev_kfree_skb(csum_skb);
2206 ret = -EINVAL;
2207 goto out_unmap;
2208 }
2209 iwl_pcie_txq_build_tfd(trans, txq, hdr_tb_phys,
2210 hdr_tb_len, false);
2211 trace_iwlwifi_dev_tx_tso_chunk(trans->dev, start_hdr,
2212 hdr_tb_len);
2213
2214 /* prepare the start_hdr for the next subframe */
2215 start_hdr = hdr_page->pos;
2216
2217 /* put the payload */
2218 while (data_left) {
2219 unsigned int size = min_t(unsigned int, tso.size,
2220 data_left);
2221 dma_addr_t tb_phys;
2222
2223 if (trans_pcie->sw_csum_tx)
2224 memcpy(skb_put(csum_skb, size), tso.data, size);
2225
2226 tb_phys = dma_map_single(trans->dev, tso.data,
2227 size, DMA_TO_DEVICE);
2228 if (unlikely(dma_mapping_error(trans->dev, tb_phys))) {
2229 dev_kfree_skb(csum_skb);
2230 ret = -EINVAL;
2231 goto out_unmap;
2232 }
2233
2234 iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
2235 size, false);
2236 trace_iwlwifi_dev_tx_tso_chunk(trans->dev, tso.data,
2237 size);
2238
2239 data_left -= size;
2240 tso_build_data(skb, &tso, size);
2241 }
2242
2243 /* For testing on early hardware only */
2244 if (trans_pcie->sw_csum_tx) {
2245 __wsum csum;
2246
2247 csum = skb_checksum(csum_skb,
2248 skb_checksum_start_offset(csum_skb),
2249 csum_skb->len -
2250 skb_checksum_start_offset(csum_skb),
2251 0);
2252 dev_kfree_skb(csum_skb);
2253 dma_sync_single_for_cpu(trans->dev, hdr_tb_phys,
2254 hdr_tb_len, DMA_TO_DEVICE);
2255 tcph->check = csum_fold(csum);
2256 dma_sync_single_for_device(trans->dev, hdr_tb_phys,
2257 hdr_tb_len, DMA_TO_DEVICE);
2258 }
2259 }
2260
2261 /* re -add the WiFi header and IV */
2262 skb_push(skb, hdr_len + iv_len);
2263
2264 return 0;
2265
2266out_unmap:
Sara Sharon6983ba62016-06-26 13:17:56 +03002267 iwl_pcie_tfd_unmap(trans, out_meta, txq, q->write_ptr);
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +03002268 return ret;
2269}
2270#else /* CONFIG_INET */
2271static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
2272 struct iwl_txq *txq, u8 hdr_len,
2273 struct iwl_cmd_meta *out_meta,
2274 struct iwl_device_cmd *dev_cmd, u16 tb1_len)
2275{
2276 /* No A-MSDU without CONFIG_INET */
2277 WARN_ON(1);
2278
2279 return -1;
2280}
2281#endif /* CONFIG_INET */
2282
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002283int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
2284 struct iwl_device_cmd *dev_cmd, int txq_id)
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07002285{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07002286 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Johannes Berg206eea72015-04-17 16:38:31 +02002287 struct ieee80211_hdr *hdr;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002288 struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
2289 struct iwl_cmd_meta *out_meta;
2290 struct iwl_txq *txq;
2291 struct iwl_queue *q;
Johannes Berg38c0f3342013-02-27 13:18:50 +01002292 dma_addr_t tb0_phys, tb1_phys, scratch_phys;
2293 void *tb1_addr;
Sara Sharon4fe10bc2016-07-04 14:34:26 +03002294 void *tfd;
Emmanuel Grumbach3a0b2a42015-10-14 22:10:50 +03002295 u16 len, tb1_len;
Johannes Bergea68f462014-02-27 14:36:55 +01002296 bool wait_write_ptr;
Johannes Berg206eea72015-04-17 16:38:31 +02002297 __le16 fc;
2298 u8 hdr_len;
Johannes Berg68972c42013-06-11 19:05:27 +02002299 u16 wifi_seq;
Sara Sharonc772a3d32016-03-13 17:19:38 +02002300 bool amsdu;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07002301
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002302 txq = &trans_pcie->txq[txq_id];
2303 q = &txq->q;
Emmanuel Grumbach39644e92011-09-15 11:46:29 -07002304
Johannes Berg961de6a2013-07-04 18:00:08 +02002305 if (WARN_ONCE(!test_bit(txq_id, trans_pcie->queue_used),
2306 "TX on unused queue %d\n", txq_id))
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002307 return -EINVAL;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07002308
Emmanuel Grumbach41837ca92015-10-21 09:00:07 +03002309 if (unlikely(trans_pcie->sw_csum_tx &&
2310 skb->ip_summed == CHECKSUM_PARTIAL)) {
2311 int offs = skb_checksum_start_offset(skb);
2312 int csum_offs = offs + skb->csum_offset;
2313 __wsum csum;
2314
2315 if (skb_ensure_writable(skb, csum_offs + sizeof(__sum16)))
2316 return -1;
2317
2318 csum = skb_checksum(skb, offs, skb->len - offs, 0);
2319 *(__sum16 *)(skb->data + csum_offs) = csum_fold(csum);
Emmanuel Grumbach39555252016-01-14 09:39:21 +02002320
2321 skb->ip_summed = CHECKSUM_UNNECESSARY;
Emmanuel Grumbach41837ca92015-10-21 09:00:07 +03002322 }
2323
Johannes Berg206eea72015-04-17 16:38:31 +02002324 if (skb_is_nonlinear(skb) &&
Sara Sharon3cd19802016-06-23 16:31:40 +03002325 skb_shinfo(skb)->nr_frags > IWL_PCIE_MAX_FRAGS(trans_pcie) &&
Johannes Berg206eea72015-04-17 16:38:31 +02002326 __skb_linearize(skb))
2327 return -ENOMEM;
2328
2329 /* mac80211 always puts the full header into the SKB's head,
2330 * so there's no need to check if it's readable there
2331 */
2332 hdr = (struct ieee80211_hdr *)skb->data;
2333 fc = hdr->frame_control;
2334 hdr_len = ieee80211_hdrlen(fc);
2335
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002336 spin_lock(&txq->lock);
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07002337
Emmanuel Grumbach39555252016-01-14 09:39:21 +02002338 if (iwl_queue_space(q) < q->high_mark) {
2339 iwl_stop_queue(trans, txq);
2340
2341 /* don't put the packet on the ring, if there is no room */
2342 if (unlikely(iwl_queue_space(q) < 3)) {
Johannes Berg21cb3222016-06-21 13:11:48 +02002343 struct iwl_device_cmd **dev_cmd_ptr;
Emmanuel Grumbach39555252016-01-14 09:39:21 +02002344
Johannes Berg21cb3222016-06-21 13:11:48 +02002345 dev_cmd_ptr = (void *)((u8 *)skb->cb +
2346 trans_pcie->dev_cmd_offs);
2347
2348 *dev_cmd_ptr = dev_cmd;
Emmanuel Grumbach39555252016-01-14 09:39:21 +02002349 __skb_queue_tail(&txq->overflow_q, skb);
2350
2351 spin_unlock(&txq->lock);
2352 return 0;
2353 }
2354 }
2355
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002356 /* In AGG mode, the index in the ring must correspond to the WiFi
2357 * sequence number. This is a HW requirements to help the SCD to parse
2358 * the BA.
2359 * Check here that the packets are in the right place on the ring.
2360 */
Johannes Berg9a886582013-02-15 19:25:00 +01002361 wifi_seq = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
Eliad Peller1092b9b2013-07-16 17:53:43 +03002362 WARN_ONCE(txq->ampdu &&
Johannes Berg68972c42013-06-11 19:05:27 +02002363 (wifi_seq & 0xff) != q->write_ptr,
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002364 "Q: %d WiFi Seq %d tfdNum %d",
2365 txq_id, wifi_seq, q->write_ptr);
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07002366
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002367 /* Set up driver data for this TFD */
2368 txq->entries[q->write_ptr].skb = skb;
2369 txq->entries[q->write_ptr].cmd = dev_cmd;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07002370
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002371 dev_cmd->hdr.sequence =
2372 cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2373 INDEX_TO_SEQ(q->write_ptr)));
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07002374
Sara Sharon8de437c2016-06-09 17:56:38 +03002375 tb0_phys = iwl_pcie_get_first_tb_dma(txq, q->write_ptr);
Johannes Berg38c0f3342013-02-27 13:18:50 +01002376 scratch_phys = tb0_phys + sizeof(struct iwl_cmd_header) +
2377 offsetof(struct iwl_tx_cmd, scratch);
2378
2379 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
2380 tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
2381
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002382 /* Set up first empty entry in queue's array of Tx/cmd buffers */
2383 out_meta = &txq->entries[q->write_ptr].meta;
Johannes Berg206eea72015-04-17 16:38:31 +02002384 out_meta->flags = 0;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07002385
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002386 /*
Johannes Berg38c0f3342013-02-27 13:18:50 +01002387 * The second TB (tb1) points to the remainder of the TX command
2388 * and the 802.11 header - dword aligned size
2389 * (This calculation modifies the TX command, so do it before the
2390 * setup of the first TB)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002391 */
Johannes Berg38c0f3342013-02-27 13:18:50 +01002392 len = sizeof(struct iwl_tx_cmd) + sizeof(struct iwl_cmd_header) +
Sara Sharon8de437c2016-06-09 17:56:38 +03002393 hdr_len - IWL_FIRST_TB_SIZE;
Sara Sharonc772a3d32016-03-13 17:19:38 +02002394 /* do not align A-MSDU to dword as the subframe header aligns it */
2395 amsdu = ieee80211_is_data_qos(fc) &&
2396 (*ieee80211_get_qos_ctl(hdr) &
2397 IEEE80211_QOS_CTL_A_MSDU_PRESENT);
2398 if (trans_pcie->sw_csum_tx || !amsdu) {
2399 tb1_len = ALIGN(len, 4);
2400 /* Tell NIC about any 2-byte padding after MAC header */
2401 if (tb1_len != len)
2402 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2403 } else {
2404 tb1_len = len;
2405 }
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002406
Sara Sharon8de437c2016-06-09 17:56:38 +03002407 /* The first TB points to bi-directional DMA data */
2408 memcpy(&txq->first_tb_bufs[q->write_ptr], &dev_cmd->hdr,
2409 IWL_FIRST_TB_SIZE);
Johannes Berg38c0f3342013-02-27 13:18:50 +01002410 iwl_pcie_txq_build_tfd(trans, txq, tb0_phys,
Sara Sharon8de437c2016-06-09 17:56:38 +03002411 IWL_FIRST_TB_SIZE, true);
Johannes Berg38c0f3342013-02-27 13:18:50 +01002412
2413 /* there must be data left over for TB1 or this code must be changed */
Sara Sharon8de437c2016-06-09 17:56:38 +03002414 BUILD_BUG_ON(sizeof(struct iwl_tx_cmd) < IWL_FIRST_TB_SIZE);
Johannes Berg38c0f3342013-02-27 13:18:50 +01002415
2416 /* map the data for TB1 */
Sara Sharon8de437c2016-06-09 17:56:38 +03002417 tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE;
Johannes Berg38c0f3342013-02-27 13:18:50 +01002418 tb1_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
2419 if (unlikely(dma_mapping_error(trans->dev, tb1_phys)))
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002420 goto out_err;
Johannes Berg6d6e68f2014-04-23 19:00:56 +02002421 iwl_pcie_txq_build_tfd(trans, txq, tb1_phys, tb1_len, false);
Johannes Berg38c0f3342013-02-27 13:18:50 +01002422
Sara Sharonc772a3d32016-03-13 17:19:38 +02002423 if (amsdu) {
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +03002424 if (unlikely(iwl_fill_data_tbs_amsdu(trans, skb, txq, hdr_len,
2425 out_meta, dev_cmd,
2426 tb1_len)))
2427 goto out_err;
2428 } else if (unlikely(iwl_fill_data_tbs(trans, skb, txq, hdr_len,
2429 out_meta, dev_cmd, tb1_len))) {
Emmanuel Grumbach3a0b2a42015-10-14 22:10:50 +03002430 goto out_err;
Emmanuel Grumbach6eb5e5292015-10-18 09:31:24 +03002431 }
Johannes Berg206eea72015-04-17 16:38:31 +02002432
Sara Sharon4fe10bc2016-07-04 14:34:26 +03002433 tfd = iwl_pcie_get_tfd(trans_pcie, txq, q->write_ptr);
Johannes Berg38c0f3342013-02-27 13:18:50 +01002434 /* Set up entry for this TFD in Tx byte-count array */
Sara Sharon4fe10bc2016-07-04 14:34:26 +03002435 iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len),
2436 iwl_pcie_tfd_get_num_tbs(trans, tfd));
Johannes Berg38c0f3342013-02-27 13:18:50 +01002437
Johannes Bergea68f462014-02-27 14:36:55 +01002438 wait_write_ptr = ieee80211_has_morefrags(fc);
Johannes Berg7c5ba4a2012-04-09 17:46:54 -07002439
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002440 /* start timer if queue currently empty */
Eliad Peller7616f332014-11-20 17:33:43 +02002441 if (q->read_ptr == q->write_ptr) {
Emmanuel Grumbachaecdc632015-07-29 23:06:41 +03002442 if (txq->wd_timeout) {
2443 /*
2444 * If the TXQ is active, then set the timer, if not,
2445 * set the timer in remainder so that the timer will
2446 * be armed with the right value when the station will
2447 * wake up.
2448 */
2449 if (!txq->frozen)
2450 mod_timer(&txq->stuck_timer,
2451 jiffies + txq->wd_timeout);
2452 else
2453 txq->frozen_expiry_remainder = txq->wd_timeout;
2454 }
Eliad Peller7616f332014-11-20 17:33:43 +02002455 IWL_DEBUG_RPM(trans, "Q: %d first tx - take ref\n", q->id);
Luca Coelhoc24c7f52016-03-30 20:59:27 +03002456 iwl_trans_ref(trans);
Eliad Peller7616f332014-11-20 17:33:43 +02002457 }
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002458
2459 /* Tell device the write index *just past* this latest filled TFD */
Johannes Berg83f32a42014-04-24 09:57:40 +02002460 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr);
Johannes Bergea68f462014-02-27 14:36:55 +01002461 if (!wait_write_ptr)
2462 iwl_pcie_txq_inc_wr_ptr(trans, txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002463
2464 /*
2465 * At this point the frame is "transmitted" successfully
Johannes Berg43aa6162014-02-27 14:24:36 +01002466 * and we will get a TX status notification eventually.
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002467 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02002468 spin_unlock(&txq->lock);
2469 return 0;
2470out_err:
2471 spin_unlock(&txq->lock);
2472 return -1;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07002473}