blob: 975492f0b8c8861e585ae9d7b4fc7778cbb88e0b [file] [log] [blame]
Ron Rindjunsky1053d352008-05-05 10:22:43 +08001/******************************************************************************
2 *
Johannes Berg128e63e2013-01-21 21:39:26 +01003 * Copyright(c) 2003 - 2013 Intel Corporation. All rights reserved.
Ron Rindjunsky1053d352008-05-05 10:22:43 +08004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Ron Rindjunsky1053d352008-05-05 10:22:43 +080026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
Tomas Winklerfd4abac2008-05-15 13:54:07 +080029#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Emmanuel Grumbach253a6342011-07-11 07:39:46 -070031#include <linux/sched.h>
Emmanuel Grumbach253a6342011-07-11 07:39:46 -070032
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070033#include "iwl-debug.h"
34#include "iwl-csr.h"
35#include "iwl-prph.h"
Ron Rindjunsky1053d352008-05-05 10:22:43 +080036#include "iwl-io.h"
Emmanuel Grumbached277c92012-02-09 16:08:15 +020037#include "iwl-op-mode.h"
Johannes Berg6468a012012-05-16 19:13:54 +020038#include "internal.h"
Johannes Berg6238b002012-04-02 15:04:33 +020039/* FIXME: need to abstract out TX command (once we know what it looks like) */
Johannes Berg1023fdc2012-05-15 12:16:34 +020040#include "dvm/commands.h"
Ron Rindjunsky1053d352008-05-05 10:22:43 +080041
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070042#define IWL_TX_CRC_SIZE 4
43#define IWL_TX_DELIMITER_SIZE 4
44
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +020045/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
46 * DMA services
47 *
48 * Theory of operation
49 *
50 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
51 * of buffer descriptors, each of which points to one or more data buffers for
52 * the device to read from or fill. Driver and device exchange status of each
53 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
54 * entries in each circular buffer, to protect against confusing empty and full
55 * queue states.
56 *
57 * The device reads or writes the data in the queues via the device's several
58 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
59 *
60 * For Tx queue, there are low mark and high mark limits. If, after queuing
61 * the packet for Tx, free space become < low mark, Tx queue stopped. When
62 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
63 * Tx queue resumed.
64 *
65 ***************************************************/
66static int iwl_queue_space(const struct iwl_queue *q)
67{
68 int s = q->read_ptr - q->write_ptr;
69
70 if (q->read_ptr > q->write_ptr)
71 s -= q->n_bd;
72
73 if (s <= 0)
74 s += q->n_window;
75 /* keep some reserve to not confuse empty and full situations */
76 s -= 2;
77 if (s < 0)
78 s = 0;
79 return s;
80}
81
82/*
83 * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
84 */
85static int iwl_queue_init(struct iwl_queue *q, int count, int slots_num, u32 id)
86{
87 q->n_bd = count;
88 q->n_window = slots_num;
89 q->id = id;
90
91 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
92 * and iwl_queue_dec_wrap are broken. */
93 if (WARN_ON(!is_power_of_2(count)))
94 return -EINVAL;
95
96 /* slots_num must be power-of-two size, otherwise
97 * get_cmd_index is broken. */
98 if (WARN_ON(!is_power_of_2(slots_num)))
99 return -EINVAL;
100
101 q->low_mark = q->n_window / 4;
102 if (q->low_mark < 4)
103 q->low_mark = 4;
104
105 q->high_mark = q->n_window / 8;
106 if (q->high_mark < 2)
107 q->high_mark = 2;
108
109 q->write_ptr = 0;
110 q->read_ptr = 0;
111
112 return 0;
113}
114
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200115static int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
116 struct iwl_dma_ptr *ptr, size_t size)
117{
118 if (WARN_ON(ptr->addr))
119 return -EINVAL;
120
121 ptr->addr = dma_alloc_coherent(trans->dev, size,
122 &ptr->dma, GFP_KERNEL);
123 if (!ptr->addr)
124 return -ENOMEM;
125 ptr->size = size;
126 return 0;
127}
128
129static void iwl_pcie_free_dma_ptr(struct iwl_trans *trans,
130 struct iwl_dma_ptr *ptr)
131{
132 if (unlikely(!ptr->addr))
133 return;
134
135 dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
136 memset(ptr, 0, sizeof(*ptr));
137}
138
139static void iwl_pcie_txq_stuck_timer(unsigned long data)
140{
141 struct iwl_txq *txq = (void *)data;
142 struct iwl_queue *q = &txq->q;
143 struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
144 struct iwl_trans *trans = iwl_trans_pcie_get_trans(trans_pcie);
145 u32 scd_sram_addr = trans_pcie->scd_base_addr +
146 SCD_TX_STTS_QUEUE_OFFSET(txq->q.id);
147 u8 buf[16];
148 int i;
149
150 spin_lock(&txq->lock);
151 /* check if triggered erroneously */
152 if (txq->q.read_ptr == txq->q.write_ptr) {
153 spin_unlock(&txq->lock);
154 return;
155 }
156 spin_unlock(&txq->lock);
157
158 IWL_ERR(trans, "Queue %d stuck for %u ms.\n", txq->q.id,
159 jiffies_to_msecs(trans_pcie->wd_timeout));
160 IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
161 txq->q.read_ptr, txq->q.write_ptr);
162
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +0200163 iwl_trans_read_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf));
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200164
165 iwl_print_hex_error(trans, buf, sizeof(buf));
166
167 for (i = 0; i < FH_TCSR_CHNL_NUM; i++)
168 IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", i,
169 iwl_read_direct32(trans, FH_TX_TRB_REG(i)));
170
171 for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
172 u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(i));
173 u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
174 bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
175 u32 tbl_dw =
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +0200176 iwl_trans_read_mem32(trans,
177 trans_pcie->scd_base_addr +
178 SCD_TRANS_TBL_OFFSET_QUEUE(i));
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200179
180 if (i & 0x1)
181 tbl_dw = (tbl_dw & 0xFFFF0000) >> 16;
182 else
183 tbl_dw = tbl_dw & 0x0000FFFF;
184
185 IWL_ERR(trans,
186 "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n",
187 i, active ? "" : "in", fifo, tbl_dw,
188 iwl_read_prph(trans,
189 SCD_QUEUE_RDPTR(i)) & (txq->q.n_bd - 1),
190 iwl_read_prph(trans, SCD_QUEUE_WRPTR(i)));
191 }
192
193 for (i = q->read_ptr; i != q->write_ptr;
194 i = iwl_queue_inc_wrap(i, q->n_bd)) {
195 struct iwl_tx_cmd *tx_cmd =
196 (struct iwl_tx_cmd *)txq->entries[i].cmd->payload;
197 IWL_ERR(trans, "scratch %d = 0x%08x\n", i,
198 get_unaligned_le32(&tx_cmd->scratch));
199 }
200
201 iwl_op_mode_nic_error(trans->op_mode);
202}
203
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200204/*
205 * iwl_pcie_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300206 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200207static void iwl_pcie_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
208 struct iwl_txq *txq, u16 byte_cnt)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300209{
Emmanuel Grumbach105183b2011-08-25 23:11:02 -0700210 struct iwlagn_scd_bc_tbl *scd_bc_tbl;
Johannes Berg20d3b642012-05-16 22:54:29 +0200211 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300212 int write_ptr = txq->q.write_ptr;
213 int txq_id = txq->q.id;
214 u8 sec_ctl = 0;
215 u8 sta_id = 0;
216 u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
217 __le16 bc_ent;
Emmanuel Grumbach132f98c2011-09-20 15:37:24 -0700218 struct iwl_tx_cmd *tx_cmd =
Johannes Bergbf8440e2012-03-19 17:12:06 +0100219 (void *) txq->entries[txq->q.write_ptr].cmd->payload;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300220
Emmanuel Grumbach105183b2011-08-25 23:11:02 -0700221 scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
222
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300223 WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
224
Emmanuel Grumbach132f98c2011-09-20 15:37:24 -0700225 sta_id = tx_cmd->sta_id;
226 sec_ctl = tx_cmd->sec_ctl;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300227
228 switch (sec_ctl & TX_CMD_SEC_MSK) {
229 case TX_CMD_SEC_CCM:
230 len += CCMP_MIC_LEN;
231 break;
232 case TX_CMD_SEC_TKIP:
233 len += TKIP_ICV_LEN;
234 break;
235 case TX_CMD_SEC_WEP:
236 len += WEP_IV_LEN + WEP_ICV_LEN;
237 break;
238 }
239
Emmanuel Grumbach046db342012-12-05 15:07:54 +0200240 if (trans_pcie->bc_table_dword)
241 len = DIV_ROUND_UP(len, 4);
242
243 bc_ent = cpu_to_le16(len | (sta_id << 12));
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300244
245 scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
246
247 if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
248 scd_bc_tbl[txq_id].
249 tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
250}
251
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200252static void iwl_pcie_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
253 struct iwl_txq *txq)
254{
255 struct iwl_trans_pcie *trans_pcie =
256 IWL_TRANS_GET_PCIE_TRANS(trans);
257 struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
258 int txq_id = txq->q.id;
259 int read_ptr = txq->q.read_ptr;
260 u8 sta_id = 0;
261 __le16 bc_ent;
262 struct iwl_tx_cmd *tx_cmd =
263 (void *)txq->entries[txq->q.read_ptr].cmd->payload;
264
265 WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
266
267 if (txq_id != trans_pcie->cmd_queue)
268 sta_id = tx_cmd->sta_id;
269
270 bc_ent = cpu_to_le16(1 | (sta_id << 12));
271 scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
272
273 if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
274 scd_bc_tbl[txq_id].
275 tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
276}
277
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200278/*
279 * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800280 */
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200281void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans, struct iwl_txq *txq)
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800282{
283 u32 reg = 0;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800284 int txq_id = txq->q.id;
285
286 if (txq->need_update == 0)
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -0800287 return;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800288
Emmanuel Grumbach035f7ff2012-03-26 08:57:01 -0700289 if (trans->cfg->base_params->shadow_reg_enable) {
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800290 /* shadow register enabled */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200291 iwl_write32(trans, HBUS_TARG_WRPTR,
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800292 txq->q.write_ptr | (txq_id << 8));
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800293 } else {
Don Fry47107e82012-03-15 13:27:06 -0700294 struct iwl_trans_pcie *trans_pcie =
295 IWL_TRANS_GET_PCIE_TRANS(trans);
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800296 /* if we're trying to save power */
Don Fry01d651d2012-03-23 08:34:31 -0700297 if (test_bit(STATUS_TPOWER_PMI, &trans_pcie->status)) {
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800298 /* wake up nic if it's powered down ...
299 * uCode will wake up, and interrupt us again, so next
300 * time we'll skip this part. */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200301 reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800302
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800303 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Emmanuel Grumbachfd656932011-08-25 23:11:19 -0700304 IWL_DEBUG_INFO(trans,
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800305 "Tx queue %d requesting wakeup,"
306 " GP1 = 0x%x\n", txq_id, reg);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200307 iwl_set_bit(trans, CSR_GP_CNTRL,
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800308 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
309 return;
310 }
311
Emmanuel Grumbach1c3fea82013-01-02 12:12:25 +0200312 IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id,
313 txq->q.write_ptr);
314
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200315 iwl_write_direct32(trans, HBUS_TARG_WRPTR,
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800316 txq->q.write_ptr | (txq_id << 8));
317
318 /*
319 * else not in power-save mode,
320 * uCode will never sleep when we're
321 * trying to tx (during RFKILL, we're not trying to tx).
322 */
323 } else
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200324 iwl_write32(trans, HBUS_TARG_WRPTR,
Wey-Yi Guyf81c1f42010-11-10 09:56:50 -0800325 txq->q.write_ptr | (txq_id << 8));
326 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800327 txq->need_update = 0;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800328}
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800329
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200330static inline dma_addr_t iwl_pcie_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
Johannes Berg214d14d2011-05-04 07:50:44 -0700331{
332 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
333
334 dma_addr_t addr = get_unaligned_le32(&tb->lo);
335 if (sizeof(dma_addr_t) > sizeof(u32))
336 addr |=
337 ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
338
339 return addr;
340}
341
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200342static inline u16 iwl_pcie_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
Johannes Berg214d14d2011-05-04 07:50:44 -0700343{
344 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
345
346 return le16_to_cpu(tb->hi_n_len) >> 4;
347}
348
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200349static inline void iwl_pcie_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
350 dma_addr_t addr, u16 len)
Johannes Berg214d14d2011-05-04 07:50:44 -0700351{
352 struct iwl_tfd_tb *tb = &tfd->tbs[idx];
353 u16 hi_n_len = len << 4;
354
355 put_unaligned_le32(addr, &tb->lo);
356 if (sizeof(dma_addr_t) > sizeof(u32))
357 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
358
359 tb->hi_n_len = cpu_to_le16(hi_n_len);
360
361 tfd->num_tbs = idx + 1;
362}
363
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200364static inline u8 iwl_pcie_tfd_get_num_tbs(struct iwl_tfd *tfd)
Johannes Berg214d14d2011-05-04 07:50:44 -0700365{
366 return tfd->num_tbs & 0x1f;
367}
368
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200369static void iwl_pcie_tfd_unmap(struct iwl_trans *trans,
Johannes Berg98891752013-02-26 11:28:19 +0100370 struct iwl_cmd_meta *meta,
371 struct iwl_tfd *tfd)
Johannes Berg214d14d2011-05-04 07:50:44 -0700372{
Johannes Berg214d14d2011-05-04 07:50:44 -0700373 int i;
374 int num_tbs;
375
Johannes Berg214d14d2011-05-04 07:50:44 -0700376 /* Sanity check on number of chunks */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200377 num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
Johannes Berg214d14d2011-05-04 07:50:44 -0700378
379 if (num_tbs >= IWL_NUM_OF_TBS) {
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700380 IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
Johannes Berg214d14d2011-05-04 07:50:44 -0700381 /* @todo issue fatal error, it is quite serious situation */
382 return;
383 }
384
385 /* Unmap tx_cmd */
386 if (num_tbs)
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200387 dma_unmap_single(trans->dev,
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700388 dma_unmap_addr(meta, mapping),
389 dma_unmap_len(meta, len),
Emmanuel Grumbach795414d2011-06-18 08:12:57 -0700390 DMA_BIDIRECTIONAL);
Johannes Berg214d14d2011-05-04 07:50:44 -0700391
392 /* Unmap chunks, if any. */
393 for (i = 1; i < num_tbs; i++)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200394 dma_unmap_single(trans->dev, iwl_pcie_tfd_tb_get_addr(tfd, i),
Johannes Berg98891752013-02-26 11:28:19 +0100395 iwl_pcie_tfd_tb_get_len(tfd, i),
396 DMA_TO_DEVICE);
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200397
398 tfd->num_tbs = 0;
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700399}
400
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200401/*
402 * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700403 * @trans - transport private data
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700404 * @txq - tx queue
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200405 * @dma_dir - the direction of the DMA mapping
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700406 *
407 * Does NOT advance any TFD circular buffer read/write indexes
408 * Does NOT free the TFD itself (which is within circular buffer)
409 */
Johannes Berg98891752013-02-26 11:28:19 +0100410static void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700411{
412 struct iwl_tfd *tfd_tmp = txq->tfds;
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700413
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200414 /* rd_ptr is bounded by n_bd and idx is bounded by n_window */
415 int rd_ptr = txq->q.read_ptr;
416 int idx = get_cmd_index(&txq->q, rd_ptr);
417
Johannes Berg015c15e2012-03-05 11:24:24 -0800418 lockdep_assert_held(&txq->lock);
419
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200420 /* We have only q->n_window txq->entries, but we use q->n_bd tfds */
Johannes Berg98891752013-02-26 11:28:19 +0100421 iwl_pcie_tfd_unmap(trans, &txq->entries[idx].meta, &tfd_tmp[rd_ptr]);
Johannes Berg214d14d2011-05-04 07:50:44 -0700422
423 /* free SKB */
Johannes Bergbf8440e2012-03-19 17:12:06 +0100424 if (txq->entries) {
Johannes Berg214d14d2011-05-04 07:50:44 -0700425 struct sk_buff *skb;
426
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200427 skb = txq->entries[idx].skb;
Johannes Berg214d14d2011-05-04 07:50:44 -0700428
Emmanuel Grumbach909e9b22011-09-15 11:46:30 -0700429 /* Can be called from irqs-disabled context
430 * If skb is not NULL, it means that the whole queue is being
431 * freed and that the queue is not empty - free the skb
432 */
Johannes Berg214d14d2011-05-04 07:50:44 -0700433 if (skb) {
Emmanuel Grumbached277c92012-02-09 16:08:15 +0200434 iwl_op_mode_free_skb(trans->op_mode, skb);
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200435 txq->entries[idx].skb = NULL;
Johannes Berg214d14d2011-05-04 07:50:44 -0700436 }
437 }
438}
439
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200440static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
441 dma_addr_t addr, u16 len, u8 reset)
Johannes Berg214d14d2011-05-04 07:50:44 -0700442{
443 struct iwl_queue *q;
444 struct iwl_tfd *tfd, *tfd_tmp;
445 u32 num_tbs;
446
447 q = &txq->q;
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700448 tfd_tmp = txq->tfds;
Johannes Berg214d14d2011-05-04 07:50:44 -0700449 tfd = &tfd_tmp[q->write_ptr];
450
451 if (reset)
452 memset(tfd, 0, sizeof(*tfd));
453
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200454 num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
Johannes Berg214d14d2011-05-04 07:50:44 -0700455
456 /* Each TFD can point to a maximum 20 Tx buffers */
457 if (num_tbs >= IWL_NUM_OF_TBS) {
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700458 IWL_ERR(trans, "Error can not send more than %d chunks\n",
Johannes Berg20d3b642012-05-16 22:54:29 +0200459 IWL_NUM_OF_TBS);
Johannes Berg214d14d2011-05-04 07:50:44 -0700460 return -EINVAL;
461 }
462
463 if (WARN_ON(addr & ~DMA_BIT_MASK(36)))
464 return -EINVAL;
465
466 if (unlikely(addr & ~IWL_TX_DMA_MASK))
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700467 IWL_ERR(trans, "Unaligned address = %llx\n",
Johannes Berg20d3b642012-05-16 22:54:29 +0200468 (unsigned long long)addr);
Johannes Berg214d14d2011-05-04 07:50:44 -0700469
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200470 iwl_pcie_tfd_set_tb(tfd, num_tbs, addr, len);
Johannes Berg214d14d2011-05-04 07:50:44 -0700471
472 return 0;
473}
474
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200475static int iwl_pcie_txq_alloc(struct iwl_trans *trans,
476 struct iwl_txq *txq, int slots_num,
477 u32 txq_id)
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800478{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200479 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
480 size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
481 int i;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800482
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200483 if (WARN_ON(txq->entries || txq->tfds))
484 return -EINVAL;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800485
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200486 setup_timer(&txq->stuck_timer, iwl_pcie_txq_stuck_timer,
487 (unsigned long)txq);
488 txq->trans_pcie = trans_pcie;
489
490 txq->q.n_window = slots_num;
491
492 txq->entries = kcalloc(slots_num,
493 sizeof(struct iwl_pcie_txq_entry),
494 GFP_KERNEL);
495
496 if (!txq->entries)
497 goto error;
498
499 if (txq_id == trans_pcie->cmd_queue)
500 for (i = 0; i < slots_num; i++) {
501 txq->entries[i].cmd =
502 kmalloc(sizeof(struct iwl_device_cmd),
503 GFP_KERNEL);
504 if (!txq->entries[i].cmd)
505 goto error;
506 }
507
508 /* Circular buffer of transmit frame descriptors (TFDs),
509 * shared with device */
510 txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
511 &txq->q.dma_addr, GFP_KERNEL);
512 if (!txq->tfds) {
513 IWL_ERR(trans, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
514 goto error;
515 }
516 txq->q.id = txq_id;
517
518 return 0;
519error:
520 if (txq->entries && txq_id == trans_pcie->cmd_queue)
521 for (i = 0; i < slots_num; i++)
522 kfree(txq->entries[i].cmd);
523 kfree(txq->entries);
524 txq->entries = NULL;
525
526 return -ENOMEM;
527
528}
529
530static int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
531 int slots_num, u32 txq_id)
532{
533 int ret;
534
535 txq->need_update = 0;
536
537 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
538 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
539 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
540
541 /* Initialize queue's high/low-water marks, and head/tail indexes */
542 ret = iwl_queue_init(&txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
543 txq_id);
544 if (ret)
545 return ret;
546
547 spin_lock_init(&txq->lock);
548
549 /*
550 * Tell nic where to find circular buffer of Tx Frame Descriptors for
551 * given Tx queue, and enable the DMA channel used for that queue.
552 * Circular buffer (TFD queue in DRAM) physical base address */
553 iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
554 txq->q.dma_addr >> 8);
555
556 return 0;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800557}
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800558
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200559/*
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200560 * iwl_pcie_txq_unmap - Unmap any remaining DMA mappings and free skb's
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800561 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200562static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800563{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200564 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
565 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
566 struct iwl_queue *q = &txq->q;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800567
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200568 if (!q->n_bd)
569 return;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800570
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200571 spin_lock_bh(&txq->lock);
572 while (q->write_ptr != q->read_ptr) {
Johannes Berg98891752013-02-26 11:28:19 +0100573 iwl_pcie_txq_free_tfd(trans, txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200574 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
575 }
576 spin_unlock_bh(&txq->lock);
577}
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800578
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200579/*
580 * iwl_pcie_txq_free - Deallocate DMA queue.
581 * @txq: Transmit queue to deallocate.
582 *
583 * Empty queue by removing and destroying all BD's.
584 * Free all buffers.
585 * 0-fill, but do not free "txq" descriptor structure.
586 */
587static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
588{
589 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
590 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
591 struct device *dev = trans->dev;
592 int i;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800593
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200594 if (WARN_ON(!txq))
595 return;
596
597 iwl_pcie_txq_unmap(trans, txq_id);
598
599 /* De-alloc array of command/tx buffers */
600 if (txq_id == trans_pcie->cmd_queue)
601 for (i = 0; i < txq->q.n_window; i++) {
602 kfree(txq->entries[i].cmd);
603 kfree(txq->entries[i].copy_cmd);
604 kfree(txq->entries[i].free_buf);
605 }
606
607 /* De-alloc circular buffer of TFDs */
608 if (txq->q.n_bd) {
609 dma_free_coherent(dev, sizeof(struct iwl_tfd) *
610 txq->q.n_bd, txq->tfds, txq->q.dma_addr);
Johannes Bergd21fa2d2013-01-08 00:25:21 +0100611 txq->q.dma_addr = 0;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200612 }
613
614 kfree(txq->entries);
615 txq->entries = NULL;
616
617 del_timer_sync(&txq->stuck_timer);
618
619 /* 0-fill queue descriptor structure */
620 memset(txq, 0, sizeof(*txq));
621}
622
623/*
624 * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
625 */
626static void iwl_pcie_txq_set_sched(struct iwl_trans *trans, u32 mask)
627{
628 struct iwl_trans_pcie __maybe_unused *trans_pcie =
629 IWL_TRANS_GET_PCIE_TRANS(trans);
630
631 iwl_write_prph(trans, SCD_TXFACT, mask);
632}
633
634void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
635{
636 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Johannes Berg22dc3c92013-01-09 00:47:07 +0100637 int nq = trans->cfg->base_params->num_of_queues;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200638 int chan;
639 u32 reg_val;
Johannes Berg22dc3c92013-01-09 00:47:07 +0100640 int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
641 SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200642
643 /* make sure all queue are not stopped/used */
644 memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
645 memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
646
647 trans_pcie->scd_base_addr =
648 iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
649
650 WARN_ON(scd_base_addr != 0 &&
651 scd_base_addr != trans_pcie->scd_base_addr);
652
Johannes Berg22dc3c92013-01-09 00:47:07 +0100653 /* reset context data, TX status and translation data */
654 iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
655 SCD_CONTEXT_MEM_LOWER_BOUND,
656 NULL, clear_dwords);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200657
658 iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
659 trans_pcie->scd_bc_tbls.dma >> 10);
660
661 /* The chain extension of the SCD doesn't work well. This feature is
662 * enabled by default by the HW, so we need to disable it manually.
663 */
664 iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
665
666 iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue,
667 trans_pcie->cmd_fifo);
668
669 /* Activate all Tx DMA/FIFO channels */
670 iwl_pcie_txq_set_sched(trans, IWL_MASK(0, 7));
671
672 /* Enable DMA channel */
673 for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
674 iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
675 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
676 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
677
678 /* Update FH chicken bits */
679 reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
680 iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
681 reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
682
683 /* Enable L1-Active */
684 iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
685 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
686}
687
Johannes Bergddaf5a52013-01-08 11:25:44 +0100688void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
689{
690 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
691 int txq_id;
692
693 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
694 txq_id++) {
695 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
696
697 iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
698 txq->q.dma_addr >> 8);
699 iwl_pcie_txq_unmap(trans, txq_id);
700 txq->q.read_ptr = 0;
701 txq->q.write_ptr = 0;
702 }
703
704 /* Tell NIC where to find the "keep warm" buffer */
705 iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
706 trans_pcie->kw.dma >> 4);
707
708 iwl_pcie_tx_start(trans, trans_pcie->scd_base_addr);
709}
710
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200711/*
712 * iwl_pcie_tx_stop - Stop all Tx DMA channels
713 */
714int iwl_pcie_tx_stop(struct iwl_trans *trans)
715{
716 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
717 int ch, txq_id, ret;
718 unsigned long flags;
719
720 /* Turn off all Tx DMA fifos */
721 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
722
723 iwl_pcie_txq_set_sched(trans, 0);
724
725 /* Stop each Tx DMA channel, and wait for it to be idle */
726 for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
727 iwl_write_direct32(trans,
728 FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
729 ret = iwl_poll_direct_bit(trans, FH_TSSR_TX_STATUS_REG,
730 FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000);
731 if (ret < 0)
732 IWL_ERR(trans,
733 "Failing on timeout while stopping DMA channel %d [0x%08x]\n",
734 ch,
735 iwl_read_direct32(trans,
736 FH_TSSR_TX_STATUS_REG));
737 }
738 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
739
740 if (!trans_pcie->txq) {
741 IWL_WARN(trans,
742 "Stopping tx queues that aren't allocated...\n");
743 return 0;
744 }
745
746 /* Unmap DMA from host system and free skb's */
747 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
748 txq_id++)
749 iwl_pcie_txq_unmap(trans, txq_id);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800750
751 return 0;
752}
753
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200754/*
755 * iwl_trans_tx_free - Free TXQ Context
756 *
757 * Destroy all TX DMA queues and structures
758 */
759void iwl_pcie_tx_free(struct iwl_trans *trans)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300760{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200761 int txq_id;
762 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300763
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200764 /* Tx queues */
765 if (trans_pcie->txq) {
766 for (txq_id = 0;
767 txq_id < trans->cfg->base_params->num_of_queues; txq_id++)
768 iwl_pcie_txq_free(trans, txq_id);
769 }
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300770
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200771 kfree(trans_pcie->txq);
772 trans_pcie->txq = NULL;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300773
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200774 iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300775
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200776 iwl_pcie_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300777}
778
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200779/*
780 * iwl_pcie_tx_alloc - allocate TX context
781 * Allocate all Tx DMA structures and initialize them
782 */
783static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
784{
785 int ret;
786 int txq_id, slots_num;
787 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
788
789 u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues *
790 sizeof(struct iwlagn_scd_bc_tbl);
791
792 /*It is not allowed to alloc twice, so warn when this happens.
793 * We cannot rely on the previous allocation, so free and fail */
794 if (WARN_ON(trans_pcie->txq)) {
795 ret = -EINVAL;
796 goto error;
797 }
798
799 ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
800 scd_bc_tbls_size);
801 if (ret) {
802 IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
803 goto error;
804 }
805
806 /* Alloc keep-warm buffer */
807 ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
808 if (ret) {
809 IWL_ERR(trans, "Keep Warm allocation failed\n");
810 goto error;
811 }
812
813 trans_pcie->txq = kcalloc(trans->cfg->base_params->num_of_queues,
814 sizeof(struct iwl_txq), GFP_KERNEL);
815 if (!trans_pcie->txq) {
816 IWL_ERR(trans, "Not enough memory for txq\n");
817 ret = ENOMEM;
818 goto error;
819 }
820
821 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
822 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
823 txq_id++) {
824 slots_num = (txq_id == trans_pcie->cmd_queue) ?
825 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
826 ret = iwl_pcie_txq_alloc(trans, &trans_pcie->txq[txq_id],
827 slots_num, txq_id);
828 if (ret) {
829 IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
830 goto error;
831 }
832 }
833
834 return 0;
835
836error:
837 iwl_pcie_tx_free(trans);
838
839 return ret;
840}
841int iwl_pcie_tx_init(struct iwl_trans *trans)
842{
843 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
844 int ret;
845 int txq_id, slots_num;
846 unsigned long flags;
847 bool alloc = false;
848
849 if (!trans_pcie->txq) {
850 ret = iwl_pcie_tx_alloc(trans);
851 if (ret)
852 goto error;
853 alloc = true;
854 }
855
856 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
857
858 /* Turn off all Tx DMA fifos */
859 iwl_write_prph(trans, SCD_TXFACT, 0);
860
861 /* Tell NIC where to find the "keep warm" buffer */
862 iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
863 trans_pcie->kw.dma >> 4);
864
865 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
866
867 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
868 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
869 txq_id++) {
870 slots_num = (txq_id == trans_pcie->cmd_queue) ?
871 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
872 ret = iwl_pcie_txq_init(trans, &trans_pcie->txq[txq_id],
873 slots_num, txq_id);
874 if (ret) {
875 IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
876 goto error;
877 }
878 }
879
880 return 0;
881error:
882 /*Upon error, free only if we allocated something */
883 if (alloc)
884 iwl_pcie_tx_free(trans);
885 return ret;
886}
887
888static inline void iwl_pcie_txq_progress(struct iwl_trans_pcie *trans_pcie,
889 struct iwl_txq *txq)
890{
891 if (!trans_pcie->wd_timeout)
892 return;
893
894 /*
895 * if empty delete timer, otherwise move timer forward
896 * since we're making progress on this queue
897 */
898 if (txq->q.read_ptr == txq->q.write_ptr)
899 del_timer(&txq->stuck_timer);
900 else
901 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
902}
903
904/* Frees buffers until index _not_ inclusive */
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200905void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
906 struct sk_buff_head *skbs)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200907{
908 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
909 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200910 /* n_bd is usually 256 => n_bd - 1 = 0xff */
911 int tfd_num = ssn & (txq->q.n_bd - 1);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200912 struct iwl_queue *q = &txq->q;
913 int last_to_free;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200914
915 /* This function is not meant to release cmd queue*/
916 if (WARN_ON(txq_id == trans_pcie->cmd_queue))
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200917 return;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200918
Johannes Berg2bfb5092012-12-27 21:43:48 +0100919 spin_lock_bh(&txq->lock);
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200920
921 if (txq->q.read_ptr == tfd_num)
922 goto out;
923
924 IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
925 txq_id, txq->q.read_ptr, tfd_num, ssn);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200926
927 /*Since we free until index _not_ inclusive, the one before index is
928 * the last we will free. This one must be used */
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200929 last_to_free = iwl_queue_dec_wrap(tfd_num, q->n_bd);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200930
Emmanuel Grumbach6ca6ebc2012-11-14 23:38:08 +0200931 if (!iwl_queue_used(q, last_to_free)) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200932 IWL_ERR(trans,
933 "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
934 __func__, txq_id, last_to_free, q->n_bd,
935 q->write_ptr, q->read_ptr);
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200936 goto out;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200937 }
938
939 if (WARN_ON(!skb_queue_empty(skbs)))
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200940 goto out;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200941
942 for (;
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200943 q->read_ptr != tfd_num;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200944 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
945
946 if (WARN_ON_ONCE(txq->entries[txq->q.read_ptr].skb == NULL))
947 continue;
948
949 __skb_queue_tail(skbs, txq->entries[txq->q.read_ptr].skb);
950
951 txq->entries[txq->q.read_ptr].skb = NULL;
952
953 iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq);
954
Johannes Berg98891752013-02-26 11:28:19 +0100955 iwl_pcie_txq_free_tfd(trans, txq);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200956 }
957
958 iwl_pcie_txq_progress(trans_pcie, txq);
959
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200960 if (iwl_queue_space(&txq->q) > txq->q.low_mark)
961 iwl_wake_queue(trans, txq);
962out:
Johannes Berg2bfb5092012-12-27 21:43:48 +0100963 spin_unlock_bh(&txq->lock);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200964}
965
966/*
967 * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
968 *
969 * When FW advances 'R' index, all entries between old and new 'R' index
970 * need to be reclaimed. As result, some free space forms. If there is
971 * enough free space (> low mark), wake the stack that feeds us.
972 */
973static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
974{
975 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
976 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
977 struct iwl_queue *q = &txq->q;
978 int nfreed = 0;
979
980 lockdep_assert_held(&txq->lock);
981
Emmanuel Grumbach6ca6ebc2012-11-14 23:38:08 +0200982 if ((idx >= q->n_bd) || (!iwl_queue_used(q, idx))) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200983 IWL_ERR(trans,
984 "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
985 __func__, txq_id, idx, q->n_bd,
986 q->write_ptr, q->read_ptr);
987 return;
988 }
989
990 for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
991 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
992
993 if (nfreed++ > 0) {
994 IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
995 idx, q->write_ptr, q->read_ptr);
996 iwl_op_mode_nic_error(trans->op_mode);
997 }
998 }
999
1000 iwl_pcie_txq_progress(trans_pcie, txq);
1001}
1002
1003static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001004 u16 txq_id)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001005{
Johannes Berg20d3b642012-05-16 22:54:29 +02001006 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001007 u32 tbl_dw_addr;
1008 u32 tbl_dw;
1009 u16 scd_q2ratid;
1010
1011 scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
1012
Emmanuel Grumbach105183b2011-08-25 23:11:02 -07001013 tbl_dw_addr = trans_pcie->scd_base_addr +
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001014 SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
1015
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001016 tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001017
1018 if (txq_id & 0x1)
1019 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
1020 else
1021 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
1022
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001023 iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001024
1025 return 0;
1026}
1027
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001028static inline void iwl_pcie_txq_set_inactive(struct iwl_trans *trans,
1029 u16 txq_id)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001030{
1031 /* Simply stop the queue, but don't change any configuration;
1032 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001033 iwl_write_prph(trans,
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001034 SCD_QUEUE_STATUS_BITS(txq_id),
1035 (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
1036 (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
1037}
1038
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001039void iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, int fifo,
1040 int sta_id, int tid, int frame_limit, u16 ssn)
Johannes Berg70a18c52012-03-05 11:24:44 -08001041{
Johannes Berg9eae88f2012-03-15 13:26:52 -07001042 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001043
Johannes Berg9eae88f2012-03-15 13:26:52 -07001044 if (test_and_set_bit(txq_id, trans_pcie->queue_used))
1045 WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001046
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001047 /* Stop this Tx queue before configuring it */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001048 iwl_pcie_txq_set_inactive(trans, txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001049
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001050 /* Set this queue as a chain-building queue unless it is CMD queue */
1051 if (txq_id != trans_pcie->cmd_queue)
1052 iwl_set_bits_prph(trans, SCD_QUEUECHAIN_SEL, BIT(txq_id));
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001053
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001054 /* If this queue is mapped to a certain station: it is an AGG queue */
1055 if (sta_id != IWL_INVALID_STATION) {
1056 u16 ra_tid = BUILD_RAxTID(sta_id, tid);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001057
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001058 /* Map receiver-address / traffic-ID to this queue */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001059 iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001060
1061 /* enable aggregations for the queue */
1062 iwl_set_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001063 } else {
1064 /*
1065 * disable aggregations for the queue, this will also make the
1066 * ra_tid mapping configuration irrelevant since it is now a
1067 * non-AGG queue.
1068 */
1069 iwl_clear_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001070 }
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001071
1072 /* Place first TFD at index corresponding to start sequence number.
1073 * Assumes that ssn_idx is valid (!= 0xFFF) */
Emmanuel Grumbach822e8b22011-11-21 13:25:31 +02001074 trans_pcie->txq[txq_id].q.read_ptr = (ssn & 0xff);
1075 trans_pcie->txq[txq_id].q.write_ptr = (ssn & 0xff);
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001076
1077 iwl_write_direct32(trans, HBUS_TARG_WRPTR,
1078 (ssn & 0xff) | (txq_id << 8));
1079 iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001080
1081 /* Set up Tx window size and frame limit for this queue */
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001082 iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001083 SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001084 iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
Johannes Berg9eae88f2012-03-15 13:26:52 -07001085 SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
1086 ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
1087 SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
1088 ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
1089 SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001090
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001091 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001092 iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
1093 (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1094 (fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
1095 (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
1096 SCD_QUEUE_STTS_REG_MSK);
1097 IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d on FIFO %d WrPtr: %d\n",
1098 txq_id, fifo, ssn & 0xff);
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001099}
1100
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001101void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id)
Emmanuel Grumbach288712a2011-08-25 23:11:25 -07001102{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001103 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach986ea6c2012-09-30 16:25:43 +02001104 u32 stts_addr = trans_pcie->scd_base_addr +
1105 SCD_TX_STTS_QUEUE_OFFSET(txq_id);
1106 static const u32 zero_val[4] = {};
Emmanuel Grumbach288712a2011-08-25 23:11:25 -07001107
Johannes Berg9eae88f2012-03-15 13:26:52 -07001108 if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
1109 WARN_ONCE(1, "queue %d not used", txq_id);
1110 return;
Emmanuel Grumbachbc237732011-11-21 13:25:31 +02001111 }
1112
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001113 iwl_pcie_txq_set_inactive(trans, txq_id);
Emmanuel Grumbachac928f82012-10-14 16:36:36 +02001114
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001115 iwl_trans_write_mem(trans, stts_addr, (void *)zero_val,
1116 ARRAY_SIZE(zero_val));
Emmanuel Grumbach986ea6c2012-09-30 16:25:43 +02001117
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001118 iwl_pcie_txq_unmap(trans, txq_id);
Emmanuel Grumbach6c3fd3f2012-10-18 12:38:37 +02001119
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001120 IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001121}
1122
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001123/*************** HOST COMMAND QUEUE FUNCTIONS *****/
1124
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001125/*
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001126 * iwl_pcie_enqueue_hcmd - enqueue a uCode command
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001127 * @priv: device private data point
1128 * @cmd: a point to the ucode command structure
1129 *
1130 * The function returns < 0 values to indicate the operation is
1131 * failed. On success, it turns the index (> 0) of command in the
1132 * command queue.
1133 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001134static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
1135 struct iwl_host_cmd *cmd)
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001136{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001137 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001138 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001139 struct iwl_queue *q = &txq->q;
Johannes Bergc2acea82009-07-24 11:13:05 -07001140 struct iwl_device_cmd *out_cmd;
1141 struct iwl_cmd_meta *out_meta;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001142 void *dup_buf = NULL;
Tomas Winklerf3674222008-08-04 16:00:44 +08001143 dma_addr_t phys_addr;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001144 int idx;
Johannes Berg8a964f42013-02-25 16:01:34 +01001145 u16 copy_size, cmd_size, dma_size;
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001146 bool had_nocopy = false;
1147 int i;
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001148 u32 cmd_pos;
Johannes Berg8a964f42013-02-25 16:01:34 +01001149 const u8 *cmddata[IWL_MAX_CMD_TFDS];
1150 u16 cmdlen[IWL_MAX_CMD_TFDS];
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001151
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001152 copy_size = sizeof(out_cmd->hdr);
1153 cmd_size = sizeof(out_cmd->hdr);
1154
1155 /* need one for the header if the first is NOCOPY */
1156 BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1);
1157
1158 for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
Johannes Berg8a964f42013-02-25 16:01:34 +01001159 cmddata[i] = cmd->data[i];
1160 cmdlen[i] = cmd->len[i];
1161
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001162 if (!cmd->len[i])
1163 continue;
Johannes Berg8a964f42013-02-25 16:01:34 +01001164
1165 /* need at least IWL_HCMD_MIN_COPY_SIZE copied */
1166 if (copy_size < IWL_HCMD_MIN_COPY_SIZE) {
1167 int copy = IWL_HCMD_MIN_COPY_SIZE - copy_size;
1168
1169 if (copy > cmdlen[i])
1170 copy = cmdlen[i];
1171 cmdlen[i] -= copy;
1172 cmddata[i] += copy;
1173 copy_size += copy;
1174 }
1175
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001176 if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
1177 had_nocopy = true;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001178 if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
1179 idx = -EINVAL;
1180 goto free_dup_buf;
1181 }
1182 } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
1183 /*
1184 * This is also a chunk that isn't copied
1185 * to the static buffer so set had_nocopy.
1186 */
1187 had_nocopy = true;
1188
1189 /* only allowed once */
1190 if (WARN_ON(dup_buf)) {
1191 idx = -EINVAL;
1192 goto free_dup_buf;
1193 }
1194
Johannes Berg8a964f42013-02-25 16:01:34 +01001195 dup_buf = kmemdup(cmddata[i], cmdlen[i],
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001196 GFP_ATOMIC);
1197 if (!dup_buf)
1198 return -ENOMEM;
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001199 } else {
1200 /* NOCOPY must not be followed by normal! */
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001201 if (WARN_ON(had_nocopy)) {
1202 idx = -EINVAL;
1203 goto free_dup_buf;
1204 }
Johannes Berg8a964f42013-02-25 16:01:34 +01001205 copy_size += cmdlen[i];
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001206 }
1207 cmd_size += cmd->len[i];
1208 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001209
Johannes Berg3e41ace2011-04-18 09:12:37 -07001210 /*
1211 * If any of the command structures end up being larger than
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001212 * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
1213 * allocated into separate TFDs, then we will need to
1214 * increase the size of the buffers.
Johannes Berg3e41ace2011-04-18 09:12:37 -07001215 */
Johannes Berg2a79e452012-09-26 13:32:13 +02001216 if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
1217 "Command %s (%#x) is too large (%d bytes)\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001218 get_cmd_string(trans_pcie, cmd->id), cmd->id, copy_size)) {
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001219 idx = -EINVAL;
1220 goto free_dup_buf;
1221 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001222
Johannes Berg015c15e2012-03-05 11:24:24 -08001223 spin_lock_bh(&txq->lock);
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001224
Johannes Bergc2acea82009-07-24 11:13:05 -07001225 if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
Johannes Berg015c15e2012-03-05 11:24:24 -08001226 spin_unlock_bh(&txq->lock);
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001227
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001228 IWL_ERR(trans, "No space in command queue\n");
Johannes Berg0e781842012-03-06 13:30:49 -08001229 iwl_op_mode_cmd_queue_full(trans->op_mode);
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001230 idx = -ENOSPC;
1231 goto free_dup_buf;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001232 }
1233
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001234 idx = get_cmd_index(q, q->write_ptr);
Johannes Bergbf8440e2012-03-19 17:12:06 +01001235 out_cmd = txq->entries[idx].cmd;
1236 out_meta = &txq->entries[idx].meta;
Johannes Bergc2acea82009-07-24 11:13:05 -07001237
Daniel C Halperin8ce73f32009-07-31 14:28:06 -07001238 memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
Johannes Bergc2acea82009-07-24 11:13:05 -07001239 if (cmd->flags & CMD_WANT_SKB)
1240 out_meta->source = cmd;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001241
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001242 /* set up the header */
1243
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001244 out_cmd->hdr.cmd = cmd->id;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001245 out_cmd->hdr.flags = 0;
Emmanuel Grumbachcefeaa52011-08-25 23:10:40 -07001246 out_cmd->hdr.sequence =
Meenakshi Venkataramanc6f600f2012-03-08 11:29:12 -08001247 cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
Emmanuel Grumbachcefeaa52011-08-25 23:10:40 -07001248 INDEX_TO_SEQ(q->write_ptr));
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001249
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001250 /* and copy the data that needs to be copied */
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001251 cmd_pos = offsetof(struct iwl_device_cmd, payload);
Johannes Berg8a964f42013-02-25 16:01:34 +01001252 copy_size = sizeof(out_cmd->hdr);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001253 for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
Johannes Berg8a964f42013-02-25 16:01:34 +01001254 int copy = 0;
1255
1256 if (!cmd->len)
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001257 continue;
Johannes Berg8a964f42013-02-25 16:01:34 +01001258
1259 /* need at least IWL_HCMD_MIN_COPY_SIZE copied */
1260 if (copy_size < IWL_HCMD_MIN_COPY_SIZE) {
1261 copy = IWL_HCMD_MIN_COPY_SIZE - copy_size;
1262
1263 if (copy > cmd->len[i])
1264 copy = cmd->len[i];
1265 }
1266
1267 /* copy everything if not nocopy/dup */
1268 if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1269 IWL_HCMD_DFL_DUP)))
1270 copy = cmd->len[i];
1271
1272 if (copy) {
1273 memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1274 cmd_pos += copy;
1275 copy_size += copy;
1276 }
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001277 }
1278
1279 WARN_ON_ONCE(txq->entries[idx].copy_cmd);
1280
1281 /*
1282 * since out_cmd will be the source address of the FH, it will write
1283 * the retry count there. So when the user needs to receivce the HCMD
1284 * that corresponds to the response in the response handler, it needs
1285 * to set CMD_WANT_HCMD.
1286 */
1287 if (cmd->flags & CMD_WANT_HCMD) {
1288 txq->entries[idx].copy_cmd =
1289 kmemdup(out_cmd, cmd_pos, GFP_ATOMIC);
1290 if (unlikely(!txq->entries[idx].copy_cmd)) {
1291 idx = -ENOMEM;
1292 goto out;
1293 }
Esti Kummerded2ae72008-08-04 16:00:45 +08001294 }
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001295
Johannes Bergd9fb6462012-03-26 08:23:39 -07001296 IWL_DEBUG_HC(trans,
Johannes Berg20d3b642012-05-16 22:54:29 +02001297 "Sending command %s (#%x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001298 get_cmd_string(trans_pcie, out_cmd->hdr.cmd),
Johannes Berg20d3b642012-05-16 22:54:29 +02001299 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
1300 cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001301
Johannes Berg8a964f42013-02-25 16:01:34 +01001302 /*
1303 * If the entire command is smaller than IWL_HCMD_MIN_COPY_SIZE, we must
1304 * still map at least that many bytes for the hardware to write back to.
1305 * We have enough space, so that's not a problem.
1306 */
1307 dma_size = max_t(u16, copy_size, IWL_HCMD_MIN_COPY_SIZE);
1308
1309 phys_addr = dma_map_single(trans->dev, &out_cmd->hdr, dma_size,
Johannes Berg20d3b642012-05-16 22:54:29 +02001310 DMA_BIDIRECTIONAL);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001311 if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
Johannes Berg2c46f722011-04-28 07:27:10 -07001312 idx = -ENOMEM;
1313 goto out;
1314 }
1315
FUJITA Tomonori2e724442010-06-03 14:19:20 +09001316 dma_unmap_addr_set(out_meta, mapping, phys_addr);
Johannes Berg8a964f42013-02-25 16:01:34 +01001317 dma_unmap_len_set(out_meta, len, dma_size);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001318
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001319 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, copy_size, 1);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001320
Johannes Berg8a964f42013-02-25 16:01:34 +01001321 /* map the remaining (adjusted) nocopy/dup fragments */
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001322 for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
Johannes Berg8a964f42013-02-25 16:01:34 +01001323 const void *data = cmddata[i];
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001324
Johannes Berg8a964f42013-02-25 16:01:34 +01001325 if (!cmdlen[i])
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001326 continue;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001327 if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1328 IWL_HCMD_DFL_DUP)))
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001329 continue;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001330 if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
1331 data = dup_buf;
1332 phys_addr = dma_map_single(trans->dev, (void *)data,
Johannes Berg98891752013-02-26 11:28:19 +01001333 cmdlen[i], DMA_TO_DEVICE);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001334 if (dma_mapping_error(trans->dev, phys_addr)) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001335 iwl_pcie_tfd_unmap(trans, out_meta,
Johannes Berg98891752013-02-26 11:28:19 +01001336 &txq->tfds[q->write_ptr]);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001337 idx = -ENOMEM;
1338 goto out;
1339 }
1340
Johannes Berg8a964f42013-02-25 16:01:34 +01001341 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], 0);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001342 }
Reinette Chatredf833b12009-04-21 10:55:48 -07001343
Emmanuel Grumbachafaf6b52011-07-08 08:46:09 -07001344 out_meta->flags = cmd->flags;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001345 if (WARN_ON_ONCE(txq->entries[idx].free_buf))
1346 kfree(txq->entries[idx].free_buf);
1347 txq->entries[idx].free_buf = dup_buf;
Johannes Berg2c46f722011-04-28 07:27:10 -07001348
1349 txq->need_update = 1;
1350
Johannes Berg8a964f42013-02-25 16:01:34 +01001351 trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr);
Reinette Chatredf833b12009-04-21 10:55:48 -07001352
Johannes Berg7c5ba4a2012-04-09 17:46:54 -07001353 /* start timer if queue currently empty */
1354 if (q->read_ptr == q->write_ptr && trans_pcie->wd_timeout)
1355 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
1356
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001357 /* Increment and update queue's write index */
1358 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001359 iwl_pcie_txq_inc_wr_ptr(trans, txq);
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001360
Johannes Berg2c46f722011-04-28 07:27:10 -07001361 out:
Johannes Berg015c15e2012-03-05 11:24:24 -08001362 spin_unlock_bh(&txq->lock);
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001363 free_dup_buf:
1364 if (idx < 0)
1365 kfree(dup_buf);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001366 return idx;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001367}
1368
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001369/*
1370 * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
Tomas Winkler17b88922008-05-29 16:35:12 +08001371 * @rxb: Rx buffer to reclaim
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001372 * @handler_status: return value of the handler of the command
1373 * (put in setup_rx_handlers)
Tomas Winkler17b88922008-05-29 16:35:12 +08001374 *
1375 * If an Rx buffer has an async callback associated with it the callback
1376 * will be executed. The attached skb (if present) will only be freed
1377 * if the callback returns 1
1378 */
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001379void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
1380 struct iwl_rx_cmd_buffer *rxb, int handler_status)
Tomas Winkler17b88922008-05-29 16:35:12 +08001381{
Zhu Yi2f301222009-10-09 17:19:45 +08001382 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler17b88922008-05-29 16:35:12 +08001383 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1384 int txq_id = SEQ_TO_QUEUE(sequence);
1385 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler17b88922008-05-29 16:35:12 +08001386 int cmd_index;
Johannes Bergc2acea82009-07-24 11:13:05 -07001387 struct iwl_device_cmd *cmd;
1388 struct iwl_cmd_meta *meta;
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001389 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001390 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
Tomas Winkler17b88922008-05-29 16:35:12 +08001391
1392 /* If a Tx command is being handled and it isn't in the actual
1393 * command queue then there a command routing bug has been introduced
1394 * in the queue management code. */
Meenakshi Venkataramanc6f600f2012-03-08 11:29:12 -08001395 if (WARN(txq_id != trans_pcie->cmd_queue,
Johannes Berg13bb9482010-08-23 10:46:33 +02001396 "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
Johannes Berg20d3b642012-05-16 22:54:29 +02001397 txq_id, trans_pcie->cmd_queue, sequence,
1398 trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr,
1399 trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) {
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -07001400 iwl_print_hex_error(trans, pkt, 32);
Johannes Berg55d6a3c2008-09-23 19:18:43 +02001401 return;
Winkler, Tomas01ef93232008-11-07 09:58:45 -08001402 }
Tomas Winkler17b88922008-05-29 16:35:12 +08001403
Johannes Berg2bfb5092012-12-27 21:43:48 +01001404 spin_lock_bh(&txq->lock);
Johannes Berg015c15e2012-03-05 11:24:24 -08001405
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001406 cmd_index = get_cmd_index(&txq->q, index);
Johannes Bergbf8440e2012-03-19 17:12:06 +01001407 cmd = txq->entries[cmd_index].cmd;
1408 meta = &txq->entries[cmd_index].meta;
Tomas Winkler17b88922008-05-29 16:35:12 +08001409
Johannes Berg98891752013-02-26 11:28:19 +01001410 iwl_pcie_tfd_unmap(trans, meta, &txq->tfds[index]);
Reinette Chatrec33de622009-10-30 14:36:10 -07001411
Tomas Winkler17b88922008-05-29 16:35:12 +08001412 /* Input error checking is done when commands are added to queue. */
Johannes Bergc2acea82009-07-24 11:13:05 -07001413 if (meta->flags & CMD_WANT_SKB) {
Johannes Berg48a2d662012-03-05 11:24:39 -08001414 struct page *p = rxb_steal_page(rxb);
Stanislaw Gruszka2624e962011-04-20 16:02:58 +02001415
Johannes Berg65b94a42012-03-05 11:24:38 -08001416 meta->source->resp_pkt = pkt;
1417 meta->source->_rx_page_addr = (unsigned long)page_address(p);
Johannes Bergb2cf4102012-04-09 17:46:51 -07001418 meta->source->_rx_page_order = trans_pcie->rx_page_order;
Johannes Berg65b94a42012-03-05 11:24:38 -08001419 meta->source->handler_status = handler_status;
Stanislaw Gruszka2624e962011-04-20 16:02:58 +02001420 }
Tomas Winkler17b88922008-05-29 16:35:12 +08001421
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001422 iwl_pcie_cmdq_reclaim(trans, txq_id, index);
Tomas Winkler17b88922008-05-29 16:35:12 +08001423
Johannes Bergc2acea82009-07-24 11:13:05 -07001424 if (!(meta->flags & CMD_ASYNC)) {
Don Fry74fda972012-03-20 16:36:54 -07001425 if (!test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
Wey-Yi Guy05c89b92011-10-10 07:26:48 -07001426 IWL_WARN(trans,
1427 "HCMD_ACTIVE already clear for command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001428 get_cmd_string(trans_pcie, cmd->hdr.cmd));
Wey-Yi Guy05c89b92011-10-10 07:26:48 -07001429 }
Don Fry74fda972012-03-20 16:36:54 -07001430 clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001431 IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001432 get_cmd_string(trans_pcie, cmd->hdr.cmd));
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001433 wake_up(&trans_pcie->wait_command_queue);
Tomas Winkler17b88922008-05-29 16:35:12 +08001434 }
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001435
Zhu Yidd487442010-03-22 02:28:41 -07001436 meta->flags = 0;
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001437
Johannes Berg2bfb5092012-12-27 21:43:48 +01001438 spin_unlock_bh(&txq->lock);
Tomas Winkler17b88922008-05-29 16:35:12 +08001439}
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001440
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001441#define HOST_COMPLETE_TIMEOUT (2 * HZ)
1442
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001443static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
1444 struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001445{
Johannes Bergd9fb6462012-03-26 08:23:39 -07001446 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001447 int ret;
1448
1449 /* An asynchronous command can not expect an SKB to be set. */
1450 if (WARN_ON(cmd->flags & CMD_WANT_SKB))
1451 return -EINVAL;
1452
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001453 ret = iwl_pcie_enqueue_hcmd(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001454 if (ret < 0) {
Johannes Berg721c32f2012-03-06 13:30:40 -08001455 IWL_ERR(trans,
Todd Previteb36b1102011-11-10 06:55:02 -08001456 "Error sending %s: enqueue_hcmd failed: %d\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001457 get_cmd_string(trans_pcie, cmd->id), ret);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001458 return ret;
1459 }
1460 return 0;
1461}
1462
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001463static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
1464 struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001465{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001466 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001467 int cmd_idx;
1468 int ret;
1469
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001470 IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001471 get_cmd_string(trans_pcie, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001472
Johannes Berg2cc39c92012-03-06 13:30:41 -08001473 if (WARN_ON(test_and_set_bit(STATUS_HCMD_ACTIVE,
Don Fry74fda972012-03-20 16:36:54 -07001474 &trans_pcie->status))) {
Johannes Berg2cc39c92012-03-06 13:30:41 -08001475 IWL_ERR(trans, "Command %s: a command is already active!\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001476 get_cmd_string(trans_pcie, cmd->id));
Johannes Berg2cc39c92012-03-06 13:30:41 -08001477 return -EIO;
1478 }
1479
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001480 IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001481 get_cmd_string(trans_pcie, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001482
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001483 cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001484 if (cmd_idx < 0) {
1485 ret = cmd_idx;
Don Fry74fda972012-03-20 16:36:54 -07001486 clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
Johannes Berg721c32f2012-03-06 13:30:40 -08001487 IWL_ERR(trans,
Todd Previteb36b1102011-11-10 06:55:02 -08001488 "Error sending %s: enqueue_hcmd failed: %d\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001489 get_cmd_string(trans_pcie, cmd->id), ret);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001490 return ret;
1491 }
1492
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001493 ret = wait_event_timeout(trans_pcie->wait_command_queue,
Johannes Berg20d3b642012-05-16 22:54:29 +02001494 !test_bit(STATUS_HCMD_ACTIVE,
1495 &trans_pcie->status),
1496 HOST_COMPLETE_TIMEOUT);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001497 if (!ret) {
Don Fry74fda972012-03-20 16:36:54 -07001498 if (test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001499 struct iwl_txq *txq =
Meenakshi Venkataramanc6f600f2012-03-08 11:29:12 -08001500 &trans_pcie->txq[trans_pcie->cmd_queue];
Wey-Yi Guyd10630a2011-10-10 07:26:46 -07001501 struct iwl_queue *q = &txq->q;
1502
Johannes Berg721c32f2012-03-06 13:30:40 -08001503 IWL_ERR(trans,
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001504 "Error sending %s: time out after %dms.\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001505 get_cmd_string(trans_pcie, cmd->id),
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001506 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
1507
Johannes Berg721c32f2012-03-06 13:30:40 -08001508 IWL_ERR(trans,
Wey-Yi Guyd10630a2011-10-10 07:26:46 -07001509 "Current CMD queue read_ptr %d write_ptr %d\n",
1510 q->read_ptr, q->write_ptr);
1511
Don Fry74fda972012-03-20 16:36:54 -07001512 clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
Johannes Bergd9fb6462012-03-26 08:23:39 -07001513 IWL_DEBUG_INFO(trans,
1514 "Clearing HCMD_ACTIVE for command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001515 get_cmd_string(trans_pcie, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001516 ret = -ETIMEDOUT;
1517 goto cancel;
1518 }
1519 }
1520
Johannes Bergd18aa872012-11-06 16:36:21 +01001521 if (test_bit(STATUS_FW_ERROR, &trans_pcie->status)) {
1522 IWL_ERR(trans, "FW error in SYNC CMD %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001523 get_cmd_string(trans_pcie, cmd->id));
Johannes Bergd18aa872012-11-06 16:36:21 +01001524 ret = -EIO;
1525 goto cancel;
1526 }
1527
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001528 if (test_bit(STATUS_RFKILL, &trans_pcie->status)) {
1529 IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
1530 ret = -ERFKILL;
1531 goto cancel;
1532 }
1533
Johannes Berg65b94a42012-03-05 11:24:38 -08001534 if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001535 IWL_ERR(trans, "Error: Response NULL in '%s'\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001536 get_cmd_string(trans_pcie, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001537 ret = -EIO;
1538 goto cancel;
1539 }
1540
1541 return 0;
1542
1543cancel:
1544 if (cmd->flags & CMD_WANT_SKB) {
1545 /*
1546 * Cancel the CMD_WANT_SKB flag for the cmd in the
1547 * TX cmd queue. Otherwise in case the cmd comes
1548 * in later, it will possibly set an invalid
1549 * address (cmd->meta.source).
1550 */
Johannes Bergbf8440e2012-03-19 17:12:06 +01001551 trans_pcie->txq[trans_pcie->cmd_queue].
1552 entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001553 }
Emmanuel Grumbach9cac4942011-11-10 06:55:20 -08001554
Johannes Berg65b94a42012-03-05 11:24:38 -08001555 if (cmd->resp_pkt) {
1556 iwl_free_resp(cmd);
1557 cmd->resp_pkt = NULL;
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001558 }
1559
1560 return ret;
1561}
1562
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001563int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001564{
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001565 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1566
Johannes Bergd18aa872012-11-06 16:36:21 +01001567 if (test_bit(STATUS_FW_ERROR, &trans_pcie->status))
1568 return -EIO;
1569
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001570 if (test_bit(STATUS_RFKILL, &trans_pcie->status))
1571 return -ERFKILL;
1572
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001573 if (cmd->flags & CMD_ASYNC)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001574 return iwl_pcie_send_hcmd_async(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001575
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001576 /* We still can fail on RFKILL that can be asserted while we wait */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001577 return iwl_pcie_send_hcmd_sync(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001578}
1579
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001580int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
1581 struct iwl_device_cmd *dev_cmd, int txq_id)
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001582{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001583 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001584 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1585 struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
1586 struct iwl_cmd_meta *out_meta;
1587 struct iwl_txq *txq;
1588 struct iwl_queue *q;
1589 dma_addr_t phys_addr = 0;
1590 dma_addr_t txcmd_phys;
1591 dma_addr_t scratch_phys;
1592 u16 len, firstlen, secondlen;
1593 u8 wait_write_ptr = 0;
1594 __le16 fc = hdr->frame_control;
1595 u8 hdr_len = ieee80211_hdrlen(fc);
1596 u16 __maybe_unused wifi_seq;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001597
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001598 txq = &trans_pcie->txq[txq_id];
1599 q = &txq->q;
Emmanuel Grumbach39644e92011-09-15 11:46:29 -07001600
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001601 if (unlikely(!test_bit(txq_id, trans_pcie->queue_used))) {
1602 WARN_ON_ONCE(1);
1603 return -EINVAL;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001604 }
1605
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001606 spin_lock(&txq->lock);
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001607
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001608 /* In AGG mode, the index in the ring must correspond to the WiFi
1609 * sequence number. This is a HW requirements to help the SCD to parse
1610 * the BA.
1611 * Check here that the packets are in the right place on the ring.
1612 */
1613#ifdef CONFIG_IWLWIFI_DEBUG
1614 wifi_seq = SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
1615 WARN_ONCE((iwl_read_prph(trans, SCD_AGGR_SEL) & BIT(txq_id)) &&
1616 ((wifi_seq & 0xff) != q->write_ptr),
1617 "Q: %d WiFi Seq %d tfdNum %d",
1618 txq_id, wifi_seq, q->write_ptr);
1619#endif
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001620
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001621 /* Set up driver data for this TFD */
1622 txq->entries[q->write_ptr].skb = skb;
1623 txq->entries[q->write_ptr].cmd = dev_cmd;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001624
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001625 dev_cmd->hdr.cmd = REPLY_TX;
1626 dev_cmd->hdr.sequence =
1627 cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
1628 INDEX_TO_SEQ(q->write_ptr)));
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001629
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001630 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1631 out_meta = &txq->entries[q->write_ptr].meta;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001632
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001633 /*
1634 * Use the first empty entry in this queue's command buffer array
1635 * to contain the Tx command and MAC header concatenated together
1636 * (payload data will be in another buffer).
1637 * Size of this varies, due to varying MAC header length.
1638 * If end is not dword aligned, we'll have 2 extra bytes at the end
1639 * of the MAC header (device reads on dword boundaries).
1640 * We'll tell device about this padding later.
1641 */
1642 len = sizeof(struct iwl_tx_cmd) +
1643 sizeof(struct iwl_cmd_header) + hdr_len;
1644 firstlen = (len + 3) & ~3;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001645
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001646 /* Tell NIC about any 2-byte padding after MAC header */
1647 if (firstlen != len)
1648 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1649
1650 /* Physical address of this Tx command's header (not MAC header!),
1651 * within command buffer array. */
1652 txcmd_phys = dma_map_single(trans->dev,
1653 &dev_cmd->hdr, firstlen,
1654 DMA_BIDIRECTIONAL);
1655 if (unlikely(dma_mapping_error(trans->dev, txcmd_phys)))
1656 goto out_err;
1657 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
1658 dma_unmap_len_set(out_meta, len, firstlen);
1659
1660 if (!ieee80211_has_morefrags(fc)) {
1661 txq->need_update = 1;
1662 } else {
1663 wait_write_ptr = 1;
1664 txq->need_update = 0;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001665 }
Johannes Berg7c5ba4a2012-04-09 17:46:54 -07001666
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001667 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1668 * if any (802.11 null frames have no payload). */
1669 secondlen = skb->len - hdr_len;
1670 if (secondlen > 0) {
1671 phys_addr = dma_map_single(trans->dev, skb->data + hdr_len,
1672 secondlen, DMA_TO_DEVICE);
1673 if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
1674 dma_unmap_single(trans->dev,
1675 dma_unmap_addr(out_meta, mapping),
1676 dma_unmap_len(out_meta, len),
1677 DMA_BIDIRECTIONAL);
1678 goto out_err;
1679 }
1680 }
Johannes Berg7c5ba4a2012-04-09 17:46:54 -07001681
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001682 /* Attach buffers to TFD */
1683 iwl_pcie_txq_build_tfd(trans, txq, txcmd_phys, firstlen, 1);
1684 if (secondlen > 0)
1685 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, secondlen, 0);
1686
1687 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
1688 offsetof(struct iwl_tx_cmd, scratch);
1689
1690 /* take back ownership of DMA buffer to enable update */
1691 dma_sync_single_for_cpu(trans->dev, txcmd_phys, firstlen,
1692 DMA_BIDIRECTIONAL);
1693 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1694 tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
1695
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001696 /* Set up entry for this TFD in Tx byte-count array */
1697 iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len));
1698
1699 dma_sync_single_for_device(trans->dev, txcmd_phys, firstlen,
1700 DMA_BIDIRECTIONAL);
1701
1702 trace_iwlwifi_dev_tx(trans->dev, skb,
1703 &txq->tfds[txq->q.write_ptr],
1704 sizeof(struct iwl_tfd),
1705 &dev_cmd->hdr, firstlen,
1706 skb->data + hdr_len, secondlen);
1707 trace_iwlwifi_dev_tx_data(trans->dev, skb,
1708 skb->data + hdr_len, secondlen);
1709
1710 /* start timer if queue currently empty */
1711 if (txq->need_update && q->read_ptr == q->write_ptr &&
1712 trans_pcie->wd_timeout)
1713 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
1714
1715 /* Tell device the write index *just past* this latest filled TFD */
1716 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
1717 iwl_pcie_txq_inc_wr_ptr(trans, txq);
1718
1719 /*
1720 * At this point the frame is "transmitted" successfully
1721 * and we will get a TX status notification eventually,
1722 * regardless of the value of ret. "ret" only indicates
1723 * whether or not we should update the write pointer.
1724 */
1725 if (iwl_queue_space(q) < q->high_mark) {
1726 if (wait_write_ptr) {
1727 txq->need_update = 1;
1728 iwl_pcie_txq_inc_wr_ptr(trans, txq);
1729 } else {
1730 iwl_stop_queue(trans, txq);
1731 }
1732 }
1733 spin_unlock(&txq->lock);
1734 return 0;
1735out_err:
1736 spin_unlock(&txq->lock);
1737 return -1;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001738}