blob: 041127ad372a73d7dbc471f1154f87e4249ad46b [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,
370 struct iwl_cmd_meta *meta, struct iwl_tfd *tfd,
371 enum dma_data_direction dma_dir)
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),
395 iwl_pcie_tfd_tb_get_len(tfd, i), dma_dir);
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200396
397 tfd->num_tbs = 0;
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700398}
399
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200400/*
401 * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700402 * @trans - transport private data
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700403 * @txq - tx queue
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200404 * @dma_dir - the direction of the DMA mapping
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700405 *
406 * Does NOT advance any TFD circular buffer read/write indexes
407 * Does NOT free the TFD itself (which is within circular buffer)
408 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200409static void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
410 enum dma_data_direction dma_dir)
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 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200421 iwl_pcie_tfd_unmap(trans, &txq->entries[idx].meta, &tfd_tmp[rd_ptr],
422 dma_dir);
Johannes Berg214d14d2011-05-04 07:50:44 -0700423
424 /* free SKB */
Johannes Bergbf8440e2012-03-19 17:12:06 +0100425 if (txq->entries) {
Johannes Berg214d14d2011-05-04 07:50:44 -0700426 struct sk_buff *skb;
427
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200428 skb = txq->entries[idx].skb;
Johannes Berg214d14d2011-05-04 07:50:44 -0700429
Emmanuel Grumbach909e9b22011-09-15 11:46:30 -0700430 /* Can be called from irqs-disabled context
431 * If skb is not NULL, it means that the whole queue is being
432 * freed and that the queue is not empty - free the skb
433 */
Johannes Berg214d14d2011-05-04 07:50:44 -0700434 if (skb) {
Emmanuel Grumbached277c92012-02-09 16:08:15 +0200435 iwl_op_mode_free_skb(trans->op_mode, skb);
Emmanuel Grumbachebed6332012-05-16 22:35:58 +0200436 txq->entries[idx].skb = NULL;
Johannes Berg214d14d2011-05-04 07:50:44 -0700437 }
438 }
439}
440
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200441static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
442 dma_addr_t addr, u16 len, u8 reset)
Johannes Berg214d14d2011-05-04 07:50:44 -0700443{
444 struct iwl_queue *q;
445 struct iwl_tfd *tfd, *tfd_tmp;
446 u32 num_tbs;
447
448 q = &txq->q;
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700449 tfd_tmp = txq->tfds;
Johannes Berg214d14d2011-05-04 07:50:44 -0700450 tfd = &tfd_tmp[q->write_ptr];
451
452 if (reset)
453 memset(tfd, 0, sizeof(*tfd));
454
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200455 num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
Johannes Berg214d14d2011-05-04 07:50:44 -0700456
457 /* Each TFD can point to a maximum 20 Tx buffers */
458 if (num_tbs >= IWL_NUM_OF_TBS) {
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700459 IWL_ERR(trans, "Error can not send more than %d chunks\n",
Johannes Berg20d3b642012-05-16 22:54:29 +0200460 IWL_NUM_OF_TBS);
Johannes Berg214d14d2011-05-04 07:50:44 -0700461 return -EINVAL;
462 }
463
464 if (WARN_ON(addr & ~DMA_BIT_MASK(36)))
465 return -EINVAL;
466
467 if (unlikely(addr & ~IWL_TX_DMA_MASK))
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -0700468 IWL_ERR(trans, "Unaligned address = %llx\n",
Johannes Berg20d3b642012-05-16 22:54:29 +0200469 (unsigned long long)addr);
Johannes Berg214d14d2011-05-04 07:50:44 -0700470
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200471 iwl_pcie_tfd_set_tb(tfd, num_tbs, addr, len);
Johannes Berg214d14d2011-05-04 07:50:44 -0700472
473 return 0;
474}
475
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200476static int iwl_pcie_txq_alloc(struct iwl_trans *trans,
477 struct iwl_txq *txq, int slots_num,
478 u32 txq_id)
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800479{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200480 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
481 size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
482 int i;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800483
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200484 if (WARN_ON(txq->entries || txq->tfds))
485 return -EINVAL;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800486
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200487 setup_timer(&txq->stuck_timer, iwl_pcie_txq_stuck_timer,
488 (unsigned long)txq);
489 txq->trans_pcie = trans_pcie;
490
491 txq->q.n_window = slots_num;
492
493 txq->entries = kcalloc(slots_num,
494 sizeof(struct iwl_pcie_txq_entry),
495 GFP_KERNEL);
496
497 if (!txq->entries)
498 goto error;
499
500 if (txq_id == trans_pcie->cmd_queue)
501 for (i = 0; i < slots_num; i++) {
502 txq->entries[i].cmd =
503 kmalloc(sizeof(struct iwl_device_cmd),
504 GFP_KERNEL);
505 if (!txq->entries[i].cmd)
506 goto error;
507 }
508
509 /* Circular buffer of transmit frame descriptors (TFDs),
510 * shared with device */
511 txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
512 &txq->q.dma_addr, GFP_KERNEL);
513 if (!txq->tfds) {
514 IWL_ERR(trans, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
515 goto error;
516 }
517 txq->q.id = txq_id;
518
519 return 0;
520error:
521 if (txq->entries && txq_id == trans_pcie->cmd_queue)
522 for (i = 0; i < slots_num; i++)
523 kfree(txq->entries[i].cmd);
524 kfree(txq->entries);
525 txq->entries = NULL;
526
527 return -ENOMEM;
528
529}
530
531static int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
532 int slots_num, u32 txq_id)
533{
534 int ret;
535
536 txq->need_update = 0;
537
538 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
539 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
540 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
541
542 /* Initialize queue's high/low-water marks, and head/tail indexes */
543 ret = iwl_queue_init(&txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
544 txq_id);
545 if (ret)
546 return ret;
547
548 spin_lock_init(&txq->lock);
549
550 /*
551 * Tell nic where to find circular buffer of Tx Frame Descriptors for
552 * given Tx queue, and enable the DMA channel used for that queue.
553 * Circular buffer (TFD queue in DRAM) physical base address */
554 iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
555 txq->q.dma_addr >> 8);
556
557 return 0;
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800558}
Tomas Winklerfd4abac2008-05-15 13:54:07 +0800559
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +0200560/*
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200561 * iwl_pcie_txq_unmap - Unmap any remaining DMA mappings and free skb's
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800562 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200563static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800564{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200565 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
566 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
567 struct iwl_queue *q = &txq->q;
568 enum dma_data_direction dma_dir;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800569
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200570 if (!q->n_bd)
571 return;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800572
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200573 /* In the command queue, all the TBs are mapped as BIDI
574 * so unmap them as such.
575 */
576 if (txq_id == trans_pcie->cmd_queue)
577 dma_dir = DMA_BIDIRECTIONAL;
578 else
579 dma_dir = DMA_TO_DEVICE;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800580
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200581 spin_lock_bh(&txq->lock);
582 while (q->write_ptr != q->read_ptr) {
583 iwl_pcie_txq_free_tfd(trans, txq, dma_dir);
584 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
585 }
586 spin_unlock_bh(&txq->lock);
587}
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800588
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200589/*
590 * iwl_pcie_txq_free - Deallocate DMA queue.
591 * @txq: Transmit queue to deallocate.
592 *
593 * Empty queue by removing and destroying all BD's.
594 * Free all buffers.
595 * 0-fill, but do not free "txq" descriptor structure.
596 */
597static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
598{
599 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
600 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
601 struct device *dev = trans->dev;
602 int i;
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800603
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200604 if (WARN_ON(!txq))
605 return;
606
607 iwl_pcie_txq_unmap(trans, txq_id);
608
609 /* De-alloc array of command/tx buffers */
610 if (txq_id == trans_pcie->cmd_queue)
611 for (i = 0; i < txq->q.n_window; i++) {
612 kfree(txq->entries[i].cmd);
613 kfree(txq->entries[i].copy_cmd);
614 kfree(txq->entries[i].free_buf);
615 }
616
617 /* De-alloc circular buffer of TFDs */
618 if (txq->q.n_bd) {
619 dma_free_coherent(dev, sizeof(struct iwl_tfd) *
620 txq->q.n_bd, txq->tfds, txq->q.dma_addr);
Johannes Bergd21fa2d2013-01-08 00:25:21 +0100621 txq->q.dma_addr = 0;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200622 }
623
624 kfree(txq->entries);
625 txq->entries = NULL;
626
627 del_timer_sync(&txq->stuck_timer);
628
629 /* 0-fill queue descriptor structure */
630 memset(txq, 0, sizeof(*txq));
631}
632
633/*
634 * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
635 */
636static void iwl_pcie_txq_set_sched(struct iwl_trans *trans, u32 mask)
637{
638 struct iwl_trans_pcie __maybe_unused *trans_pcie =
639 IWL_TRANS_GET_PCIE_TRANS(trans);
640
641 iwl_write_prph(trans, SCD_TXFACT, mask);
642}
643
644void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
645{
646 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Johannes Berg22dc3c92013-01-09 00:47:07 +0100647 int nq = trans->cfg->base_params->num_of_queues;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200648 int chan;
649 u32 reg_val;
Johannes Berg22dc3c92013-01-09 00:47:07 +0100650 int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
651 SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200652
653 /* make sure all queue are not stopped/used */
654 memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
655 memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
656
657 trans_pcie->scd_base_addr =
658 iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
659
660 WARN_ON(scd_base_addr != 0 &&
661 scd_base_addr != trans_pcie->scd_base_addr);
662
Johannes Berg22dc3c92013-01-09 00:47:07 +0100663 /* reset context data, TX status and translation data */
664 iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
665 SCD_CONTEXT_MEM_LOWER_BOUND,
666 NULL, clear_dwords);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200667
668 iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
669 trans_pcie->scd_bc_tbls.dma >> 10);
670
671 /* The chain extension of the SCD doesn't work well. This feature is
672 * enabled by default by the HW, so we need to disable it manually.
673 */
674 iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
675
676 iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue,
677 trans_pcie->cmd_fifo);
678
679 /* Activate all Tx DMA/FIFO channels */
680 iwl_pcie_txq_set_sched(trans, IWL_MASK(0, 7));
681
682 /* Enable DMA channel */
683 for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
684 iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
685 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
686 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
687
688 /* Update FH chicken bits */
689 reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
690 iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
691 reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
692
693 /* Enable L1-Active */
694 iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
695 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
696}
697
Johannes Bergddaf5a52013-01-08 11:25:44 +0100698void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
699{
700 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
701 int txq_id;
702
703 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
704 txq_id++) {
705 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
706
707 iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
708 txq->q.dma_addr >> 8);
709 iwl_pcie_txq_unmap(trans, txq_id);
710 txq->q.read_ptr = 0;
711 txq->q.write_ptr = 0;
712 }
713
714 /* Tell NIC where to find the "keep warm" buffer */
715 iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
716 trans_pcie->kw.dma >> 4);
717
718 iwl_pcie_tx_start(trans, trans_pcie->scd_base_addr);
719}
720
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200721/*
722 * iwl_pcie_tx_stop - Stop all Tx DMA channels
723 */
724int iwl_pcie_tx_stop(struct iwl_trans *trans)
725{
726 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
727 int ch, txq_id, ret;
728 unsigned long flags;
729
730 /* Turn off all Tx DMA fifos */
731 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
732
733 iwl_pcie_txq_set_sched(trans, 0);
734
735 /* Stop each Tx DMA channel, and wait for it to be idle */
736 for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
737 iwl_write_direct32(trans,
738 FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
739 ret = iwl_poll_direct_bit(trans, FH_TSSR_TX_STATUS_REG,
740 FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000);
741 if (ret < 0)
742 IWL_ERR(trans,
743 "Failing on timeout while stopping DMA channel %d [0x%08x]\n",
744 ch,
745 iwl_read_direct32(trans,
746 FH_TSSR_TX_STATUS_REG));
747 }
748 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
749
750 if (!trans_pcie->txq) {
751 IWL_WARN(trans,
752 "Stopping tx queues that aren't allocated...\n");
753 return 0;
754 }
755
756 /* Unmap DMA from host system and free skb's */
757 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
758 txq_id++)
759 iwl_pcie_txq_unmap(trans, txq_id);
Ron Rindjunsky1053d352008-05-05 10:22:43 +0800760
761 return 0;
762}
763
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200764/*
765 * iwl_trans_tx_free - Free TXQ Context
766 *
767 * Destroy all TX DMA queues and structures
768 */
769void iwl_pcie_tx_free(struct iwl_trans *trans)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300770{
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200771 int txq_id;
772 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300773
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200774 /* Tx queues */
775 if (trans_pcie->txq) {
776 for (txq_id = 0;
777 txq_id < trans->cfg->base_params->num_of_queues; txq_id++)
778 iwl_pcie_txq_free(trans, txq_id);
779 }
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300780
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200781 kfree(trans_pcie->txq);
782 trans_pcie->txq = NULL;
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300783
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200784 iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300785
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200786 iwl_pcie_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +0300787}
788
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200789/*
790 * iwl_pcie_tx_alloc - allocate TX context
791 * Allocate all Tx DMA structures and initialize them
792 */
793static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
794{
795 int ret;
796 int txq_id, slots_num;
797 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
798
799 u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues *
800 sizeof(struct iwlagn_scd_bc_tbl);
801
802 /*It is not allowed to alloc twice, so warn when this happens.
803 * We cannot rely on the previous allocation, so free and fail */
804 if (WARN_ON(trans_pcie->txq)) {
805 ret = -EINVAL;
806 goto error;
807 }
808
809 ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
810 scd_bc_tbls_size);
811 if (ret) {
812 IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
813 goto error;
814 }
815
816 /* Alloc keep-warm buffer */
817 ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
818 if (ret) {
819 IWL_ERR(trans, "Keep Warm allocation failed\n");
820 goto error;
821 }
822
823 trans_pcie->txq = kcalloc(trans->cfg->base_params->num_of_queues,
824 sizeof(struct iwl_txq), GFP_KERNEL);
825 if (!trans_pcie->txq) {
826 IWL_ERR(trans, "Not enough memory for txq\n");
827 ret = ENOMEM;
828 goto error;
829 }
830
831 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
832 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
833 txq_id++) {
834 slots_num = (txq_id == trans_pcie->cmd_queue) ?
835 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
836 ret = iwl_pcie_txq_alloc(trans, &trans_pcie->txq[txq_id],
837 slots_num, txq_id);
838 if (ret) {
839 IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
840 goto error;
841 }
842 }
843
844 return 0;
845
846error:
847 iwl_pcie_tx_free(trans);
848
849 return ret;
850}
851int iwl_pcie_tx_init(struct iwl_trans *trans)
852{
853 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
854 int ret;
855 int txq_id, slots_num;
856 unsigned long flags;
857 bool alloc = false;
858
859 if (!trans_pcie->txq) {
860 ret = iwl_pcie_tx_alloc(trans);
861 if (ret)
862 goto error;
863 alloc = true;
864 }
865
866 spin_lock_irqsave(&trans_pcie->irq_lock, flags);
867
868 /* Turn off all Tx DMA fifos */
869 iwl_write_prph(trans, SCD_TXFACT, 0);
870
871 /* Tell NIC where to find the "keep warm" buffer */
872 iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
873 trans_pcie->kw.dma >> 4);
874
875 spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
876
877 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
878 for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
879 txq_id++) {
880 slots_num = (txq_id == trans_pcie->cmd_queue) ?
881 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
882 ret = iwl_pcie_txq_init(trans, &trans_pcie->txq[txq_id],
883 slots_num, txq_id);
884 if (ret) {
885 IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
886 goto error;
887 }
888 }
889
890 return 0;
891error:
892 /*Upon error, free only if we allocated something */
893 if (alloc)
894 iwl_pcie_tx_free(trans);
895 return ret;
896}
897
898static inline void iwl_pcie_txq_progress(struct iwl_trans_pcie *trans_pcie,
899 struct iwl_txq *txq)
900{
901 if (!trans_pcie->wd_timeout)
902 return;
903
904 /*
905 * if empty delete timer, otherwise move timer forward
906 * since we're making progress on this queue
907 */
908 if (txq->q.read_ptr == txq->q.write_ptr)
909 del_timer(&txq->stuck_timer);
910 else
911 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
912}
913
914/* Frees buffers until index _not_ inclusive */
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200915void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
916 struct sk_buff_head *skbs)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200917{
918 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
919 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200920 /* n_bd is usually 256 => n_bd - 1 = 0xff */
921 int tfd_num = ssn & (txq->q.n_bd - 1);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200922 struct iwl_queue *q = &txq->q;
923 int last_to_free;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200924
925 /* This function is not meant to release cmd queue*/
926 if (WARN_ON(txq_id == trans_pcie->cmd_queue))
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200927 return;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200928
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200929 spin_lock(&txq->lock);
930
931 if (txq->q.read_ptr == tfd_num)
932 goto out;
933
934 IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
935 txq_id, txq->q.read_ptr, tfd_num, ssn);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200936
937 /*Since we free until index _not_ inclusive, the one before index is
938 * the last we will free. This one must be used */
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200939 last_to_free = iwl_queue_dec_wrap(tfd_num, q->n_bd);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200940
Emmanuel Grumbach6ca6ebc2012-11-14 23:38:08 +0200941 if (!iwl_queue_used(q, last_to_free)) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200942 IWL_ERR(trans,
943 "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
944 __func__, txq_id, last_to_free, q->n_bd,
945 q->write_ptr, q->read_ptr);
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200946 goto out;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200947 }
948
949 if (WARN_ON(!skb_queue_empty(skbs)))
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200950 goto out;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200951
952 for (;
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200953 q->read_ptr != tfd_num;
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200954 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
955
956 if (WARN_ON_ONCE(txq->entries[txq->q.read_ptr].skb == NULL))
957 continue;
958
959 __skb_queue_tail(skbs, txq->entries[txq->q.read_ptr].skb);
960
961 txq->entries[txq->q.read_ptr].skb = NULL;
962
963 iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq);
964
965 iwl_pcie_txq_free_tfd(trans, txq, DMA_TO_DEVICE);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200966 }
967
968 iwl_pcie_txq_progress(trans_pcie, txq);
969
Emmanuel Grumbachf6d497c2012-11-14 23:32:57 +0200970 if (iwl_queue_space(&txq->q) > txq->q.low_mark)
971 iwl_wake_queue(trans, txq);
972out:
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200973 spin_unlock(&txq->lock);
974}
975
976/*
977 * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
978 *
979 * When FW advances 'R' index, all entries between old and new 'R' index
980 * need to be reclaimed. As result, some free space forms. If there is
981 * enough free space (> low mark), wake the stack that feeds us.
982 */
983static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
984{
985 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
986 struct iwl_txq *txq = &trans_pcie->txq[txq_id];
987 struct iwl_queue *q = &txq->q;
988 int nfreed = 0;
989
990 lockdep_assert_held(&txq->lock);
991
Emmanuel Grumbach6ca6ebc2012-11-14 23:38:08 +0200992 if ((idx >= q->n_bd) || (!iwl_queue_used(q, idx))) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +0200993 IWL_ERR(trans,
994 "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
995 __func__, txq_id, idx, q->n_bd,
996 q->write_ptr, q->read_ptr);
997 return;
998 }
999
1000 for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
1001 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
1002
1003 if (nfreed++ > 0) {
1004 IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
1005 idx, q->write_ptr, q->read_ptr);
1006 iwl_op_mode_nic_error(trans->op_mode);
1007 }
1008 }
1009
1010 iwl_pcie_txq_progress(trans_pcie, txq);
1011}
1012
1013static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001014 u16 txq_id)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001015{
Johannes Berg20d3b642012-05-16 22:54:29 +02001016 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001017 u32 tbl_dw_addr;
1018 u32 tbl_dw;
1019 u16 scd_q2ratid;
1020
1021 scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
1022
Emmanuel Grumbach105183b2011-08-25 23:11:02 -07001023 tbl_dw_addr = trans_pcie->scd_base_addr +
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001024 SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
1025
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001026 tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001027
1028 if (txq_id & 0x1)
1029 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
1030 else
1031 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
1032
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001033 iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001034
1035 return 0;
1036}
1037
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001038static inline void iwl_pcie_txq_set_inactive(struct iwl_trans *trans,
1039 u16 txq_id)
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001040{
1041 /* Simply stop the queue, but don't change any configuration;
1042 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001043 iwl_write_prph(trans,
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001044 SCD_QUEUE_STATUS_BITS(txq_id),
1045 (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
1046 (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
1047}
1048
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001049void iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, int fifo,
1050 int sta_id, int tid, int frame_limit, u16 ssn)
Johannes Berg70a18c52012-03-05 11:24:44 -08001051{
Johannes Berg9eae88f2012-03-15 13:26:52 -07001052 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001053
Johannes Berg9eae88f2012-03-15 13:26:52 -07001054 if (test_and_set_bit(txq_id, trans_pcie->queue_used))
1055 WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001056
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001057 /* Stop this Tx queue before configuring it */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001058 iwl_pcie_txq_set_inactive(trans, txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001059
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001060 /* Set this queue as a chain-building queue unless it is CMD queue */
1061 if (txq_id != trans_pcie->cmd_queue)
1062 iwl_set_bits_prph(trans, SCD_QUEUECHAIN_SEL, BIT(txq_id));
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001063
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001064 /* If this queue is mapped to a certain station: it is an AGG queue */
1065 if (sta_id != IWL_INVALID_STATION) {
1066 u16 ra_tid = BUILD_RAxTID(sta_id, tid);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001067
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001068 /* Map receiver-address / traffic-ID to this queue */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001069 iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001070
1071 /* enable aggregations for the queue */
1072 iwl_set_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001073 } else {
1074 /*
1075 * disable aggregations for the queue, this will also make the
1076 * ra_tid mapping configuration irrelevant since it is now a
1077 * non-AGG queue.
1078 */
1079 iwl_clear_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001080 }
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001081
1082 /* Place first TFD at index corresponding to start sequence number.
1083 * Assumes that ssn_idx is valid (!= 0xFFF) */
Emmanuel Grumbach822e8b22011-11-21 13:25:31 +02001084 trans_pcie->txq[txq_id].q.read_ptr = (ssn & 0xff);
1085 trans_pcie->txq[txq_id].q.write_ptr = (ssn & 0xff);
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001086
1087 iwl_write_direct32(trans, HBUS_TARG_WRPTR,
1088 (ssn & 0xff) | (txq_id << 8));
1089 iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001090
1091 /* Set up Tx window size and frame limit for this queue */
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001092 iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001093 SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001094 iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
Johannes Berg9eae88f2012-03-15 13:26:52 -07001095 SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
1096 ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
1097 SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
1098 ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
1099 SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001100
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001101 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001102 iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
1103 (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1104 (fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
1105 (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
1106 SCD_QUEUE_STTS_REG_MSK);
1107 IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d on FIFO %d WrPtr: %d\n",
1108 txq_id, fifo, ssn & 0xff);
Emmanuel Grumbach4beaf6c2012-05-29 11:29:10 +03001109}
1110
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001111void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id)
Emmanuel Grumbach288712a2011-08-25 23:11:25 -07001112{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001113 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach986ea6c2012-09-30 16:25:43 +02001114 u32 stts_addr = trans_pcie->scd_base_addr +
1115 SCD_TX_STTS_QUEUE_OFFSET(txq_id);
1116 static const u32 zero_val[4] = {};
Emmanuel Grumbach288712a2011-08-25 23:11:25 -07001117
Johannes Berg9eae88f2012-03-15 13:26:52 -07001118 if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
1119 WARN_ONCE(1, "queue %d not used", txq_id);
1120 return;
Emmanuel Grumbachbc237732011-11-21 13:25:31 +02001121 }
1122
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001123 iwl_pcie_txq_set_inactive(trans, txq_id);
Emmanuel Grumbachac928f82012-10-14 16:36:36 +02001124
Emmanuel Grumbach4fd442d2012-12-24 14:27:11 +02001125 iwl_trans_write_mem(trans, stts_addr, (void *)zero_val,
1126 ARRAY_SIZE(zero_val));
Emmanuel Grumbach986ea6c2012-09-30 16:25:43 +02001127
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001128 iwl_pcie_txq_unmap(trans, txq_id);
Emmanuel Grumbach6c3fd3f2012-10-18 12:38:37 +02001129
Emmanuel Grumbach1ce86582012-06-04 16:48:17 +03001130 IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
Emmanuel Grumbach48d42c42011-07-10 10:47:01 +03001131}
1132
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001133/*************** HOST COMMAND QUEUE FUNCTIONS *****/
1134
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001135/*
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001136 * iwl_pcie_enqueue_hcmd - enqueue a uCode command
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001137 * @priv: device private data point
1138 * @cmd: a point to the ucode command structure
1139 *
1140 * The function returns < 0 values to indicate the operation is
1141 * failed. On success, it turns the index (> 0) of command in the
1142 * command queue.
1143 */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001144static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
1145 struct iwl_host_cmd *cmd)
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001146{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001147 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001148 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001149 struct iwl_queue *q = &txq->q;
Johannes Bergc2acea82009-07-24 11:13:05 -07001150 struct iwl_device_cmd *out_cmd;
1151 struct iwl_cmd_meta *out_meta;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001152 void *dup_buf = NULL;
Tomas Winklerf3674222008-08-04 16:00:44 +08001153 dma_addr_t phys_addr;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001154 int idx;
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001155 u16 copy_size, cmd_size;
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001156 bool had_nocopy = false;
1157 int i;
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001158 u32 cmd_pos;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001159
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001160 copy_size = sizeof(out_cmd->hdr);
1161 cmd_size = sizeof(out_cmd->hdr);
1162
1163 /* need one for the header if the first is NOCOPY */
1164 BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1);
1165
1166 for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
1167 if (!cmd->len[i])
1168 continue;
1169 if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
1170 had_nocopy = true;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001171 if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
1172 idx = -EINVAL;
1173 goto free_dup_buf;
1174 }
1175 } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
1176 /*
1177 * This is also a chunk that isn't copied
1178 * to the static buffer so set had_nocopy.
1179 */
1180 had_nocopy = true;
1181
1182 /* only allowed once */
1183 if (WARN_ON(dup_buf)) {
1184 idx = -EINVAL;
1185 goto free_dup_buf;
1186 }
1187
1188 dup_buf = kmemdup(cmd->data[i], cmd->len[i],
1189 GFP_ATOMIC);
1190 if (!dup_buf)
1191 return -ENOMEM;
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001192 } else {
1193 /* NOCOPY must not be followed by normal! */
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001194 if (WARN_ON(had_nocopy)) {
1195 idx = -EINVAL;
1196 goto free_dup_buf;
1197 }
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001198 copy_size += cmd->len[i];
1199 }
1200 cmd_size += cmd->len[i];
1201 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001202
Johannes Berg3e41ace2011-04-18 09:12:37 -07001203 /*
1204 * If any of the command structures end up being larger than
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001205 * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
1206 * allocated into separate TFDs, then we will need to
1207 * increase the size of the buffers.
Johannes Berg3e41ace2011-04-18 09:12:37 -07001208 */
Johannes Berg2a79e452012-09-26 13:32:13 +02001209 if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
1210 "Command %s (%#x) is too large (%d bytes)\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001211 get_cmd_string(trans_pcie, cmd->id), cmd->id, copy_size)) {
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001212 idx = -EINVAL;
1213 goto free_dup_buf;
1214 }
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001215
Johannes Berg015c15e2012-03-05 11:24:24 -08001216 spin_lock_bh(&txq->lock);
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001217
Johannes Bergc2acea82009-07-24 11:13:05 -07001218 if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
Johannes Berg015c15e2012-03-05 11:24:24 -08001219 spin_unlock_bh(&txq->lock);
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001220
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001221 IWL_ERR(trans, "No space in command queue\n");
Johannes Berg0e781842012-03-06 13:30:49 -08001222 iwl_op_mode_cmd_queue_full(trans->op_mode);
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001223 idx = -ENOSPC;
1224 goto free_dup_buf;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001225 }
1226
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001227 idx = get_cmd_index(q, q->write_ptr);
Johannes Bergbf8440e2012-03-19 17:12:06 +01001228 out_cmd = txq->entries[idx].cmd;
1229 out_meta = &txq->entries[idx].meta;
Johannes Bergc2acea82009-07-24 11:13:05 -07001230
Daniel C Halperin8ce73f32009-07-31 14:28:06 -07001231 memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
Johannes Bergc2acea82009-07-24 11:13:05 -07001232 if (cmd->flags & CMD_WANT_SKB)
1233 out_meta->source = cmd;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001234
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001235 /* set up the header */
1236
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001237 out_cmd->hdr.cmd = cmd->id;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001238 out_cmd->hdr.flags = 0;
Emmanuel Grumbachcefeaa52011-08-25 23:10:40 -07001239 out_cmd->hdr.sequence =
Meenakshi Venkataramanc6f600f2012-03-08 11:29:12 -08001240 cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
Emmanuel Grumbachcefeaa52011-08-25 23:10:40 -07001241 INDEX_TO_SEQ(q->write_ptr));
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001242
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001243 /* and copy the data that needs to be copied */
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001244 cmd_pos = offsetof(struct iwl_device_cmd, payload);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001245 for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
1246 if (!cmd->len[i])
1247 continue;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001248 if (cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1249 IWL_HCMD_DFL_DUP))
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001250 break;
Emmanuel Grumbach96791422012-07-24 01:58:32 +03001251 memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], cmd->len[i]);
1252 cmd_pos += cmd->len[i];
1253 }
1254
1255 WARN_ON_ONCE(txq->entries[idx].copy_cmd);
1256
1257 /*
1258 * since out_cmd will be the source address of the FH, it will write
1259 * the retry count there. So when the user needs to receivce the HCMD
1260 * that corresponds to the response in the response handler, it needs
1261 * to set CMD_WANT_HCMD.
1262 */
1263 if (cmd->flags & CMD_WANT_HCMD) {
1264 txq->entries[idx].copy_cmd =
1265 kmemdup(out_cmd, cmd_pos, GFP_ATOMIC);
1266 if (unlikely(!txq->entries[idx].copy_cmd)) {
1267 idx = -ENOMEM;
1268 goto out;
1269 }
Esti Kummerded2ae72008-08-04 16:00:45 +08001270 }
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001271
Johannes Bergd9fb6462012-03-26 08:23:39 -07001272 IWL_DEBUG_HC(trans,
Johannes Berg20d3b642012-05-16 22:54:29 +02001273 "Sending command %s (#%x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001274 get_cmd_string(trans_pcie, out_cmd->hdr.cmd),
Johannes Berg20d3b642012-05-16 22:54:29 +02001275 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
1276 cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001277
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001278 phys_addr = dma_map_single(trans->dev, &out_cmd->hdr, copy_size,
Johannes Berg20d3b642012-05-16 22:54:29 +02001279 DMA_BIDIRECTIONAL);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001280 if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
Johannes Berg2c46f722011-04-28 07:27:10 -07001281 idx = -ENOMEM;
1282 goto out;
1283 }
1284
FUJITA Tomonori2e724442010-06-03 14:19:20 +09001285 dma_unmap_addr_set(out_meta, mapping, phys_addr);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001286 dma_unmap_len_set(out_meta, len, copy_size);
1287
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001288 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, copy_size, 1);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001289
1290 for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001291 const void *data = cmd->data[i];
1292
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001293 if (!cmd->len[i])
1294 continue;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001295 if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1296 IWL_HCMD_DFL_DUP)))
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001297 continue;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001298 if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
1299 data = dup_buf;
1300 phys_addr = dma_map_single(trans->dev, (void *)data,
John W. Linville3be3fdb2011-06-28 13:53:32 -04001301 cmd->len[i], DMA_BIDIRECTIONAL);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +02001302 if (dma_mapping_error(trans->dev, phys_addr)) {
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001303 iwl_pcie_tfd_unmap(trans, out_meta,
1304 &txq->tfds[q->write_ptr],
1305 DMA_BIDIRECTIONAL);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001306 idx = -ENOMEM;
1307 goto out;
1308 }
1309
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001310 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmd->len[i], 0);
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001311 }
Reinette Chatredf833b12009-04-21 10:55:48 -07001312
Emmanuel Grumbachafaf6b52011-07-08 08:46:09 -07001313 out_meta->flags = cmd->flags;
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001314 if (WARN_ON_ONCE(txq->entries[idx].free_buf))
1315 kfree(txq->entries[idx].free_buf);
1316 txq->entries[idx].free_buf = dup_buf;
Johannes Berg2c46f722011-04-28 07:27:10 -07001317
1318 txq->need_update = 1;
1319
Johannes Berg45eab7c2012-09-05 00:33:53 +02001320 trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size,
1321 &out_cmd->hdr, copy_size);
Reinette Chatredf833b12009-04-21 10:55:48 -07001322
Johannes Berg7c5ba4a2012-04-09 17:46:54 -07001323 /* start timer if queue currently empty */
1324 if (q->read_ptr == q->write_ptr && trans_pcie->wd_timeout)
1325 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
1326
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001327 /* Increment and update queue's write index */
1328 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001329 iwl_pcie_txq_inc_wr_ptr(trans, txq);
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001330
Johannes Berg2c46f722011-04-28 07:27:10 -07001331 out:
Johannes Berg015c15e2012-03-05 11:24:24 -08001332 spin_unlock_bh(&txq->lock);
Johannes Bergf4feb8a2012-10-19 14:24:43 +02001333 free_dup_buf:
1334 if (idx < 0)
1335 kfree(dup_buf);
Abhijeet Kolekar7bfedc52010-02-03 13:47:56 -08001336 return idx;
Tomas Winklerfd4abac2008-05-15 13:54:07 +08001337}
1338
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001339/*
1340 * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
Tomas Winkler17b88922008-05-29 16:35:12 +08001341 * @rxb: Rx buffer to reclaim
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001342 * @handler_status: return value of the handler of the command
1343 * (put in setup_rx_handlers)
Tomas Winkler17b88922008-05-29 16:35:12 +08001344 *
1345 * If an Rx buffer has an async callback associated with it the callback
1346 * will be executed. The attached skb (if present) will only be freed
1347 * if the callback returns 1
1348 */
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001349void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
1350 struct iwl_rx_cmd_buffer *rxb, int handler_status)
Tomas Winkler17b88922008-05-29 16:35:12 +08001351{
Zhu Yi2f301222009-10-09 17:19:45 +08001352 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler17b88922008-05-29 16:35:12 +08001353 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1354 int txq_id = SEQ_TO_QUEUE(sequence);
1355 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler17b88922008-05-29 16:35:12 +08001356 int cmd_index;
Johannes Bergc2acea82009-07-24 11:13:05 -07001357 struct iwl_device_cmd *cmd;
1358 struct iwl_cmd_meta *meta;
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001359 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001360 struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
Tomas Winkler17b88922008-05-29 16:35:12 +08001361
1362 /* If a Tx command is being handled and it isn't in the actual
1363 * command queue then there a command routing bug has been introduced
1364 * in the queue management code. */
Meenakshi Venkataramanc6f600f2012-03-08 11:29:12 -08001365 if (WARN(txq_id != trans_pcie->cmd_queue,
Johannes Berg13bb9482010-08-23 10:46:33 +02001366 "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
Johannes Berg20d3b642012-05-16 22:54:29 +02001367 txq_id, trans_pcie->cmd_queue, sequence,
1368 trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr,
1369 trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) {
Emmanuel Grumbach3e10cae2011-09-06 09:31:18 -07001370 iwl_print_hex_error(trans, pkt, 32);
Johannes Berg55d6a3c2008-09-23 19:18:43 +02001371 return;
Winkler, Tomas01ef93232008-11-07 09:58:45 -08001372 }
Tomas Winkler17b88922008-05-29 16:35:12 +08001373
Johannes Berg015c15e2012-03-05 11:24:24 -08001374 spin_lock(&txq->lock);
1375
Johannes Berg4ce7cc22011-05-13 11:57:40 -07001376 cmd_index = get_cmd_index(&txq->q, index);
Johannes Bergbf8440e2012-03-19 17:12:06 +01001377 cmd = txq->entries[cmd_index].cmd;
1378 meta = &txq->entries[cmd_index].meta;
Tomas Winkler17b88922008-05-29 16:35:12 +08001379
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001380 iwl_pcie_tfd_unmap(trans, meta, &txq->tfds[index], DMA_BIDIRECTIONAL);
Reinette Chatrec33de622009-10-30 14:36:10 -07001381
Tomas Winkler17b88922008-05-29 16:35:12 +08001382 /* Input error checking is done when commands are added to queue. */
Johannes Bergc2acea82009-07-24 11:13:05 -07001383 if (meta->flags & CMD_WANT_SKB) {
Johannes Berg48a2d662012-03-05 11:24:39 -08001384 struct page *p = rxb_steal_page(rxb);
Stanislaw Gruszka2624e962011-04-20 16:02:58 +02001385
Johannes Berg65b94a42012-03-05 11:24:38 -08001386 meta->source->resp_pkt = pkt;
1387 meta->source->_rx_page_addr = (unsigned long)page_address(p);
Johannes Bergb2cf4102012-04-09 17:46:51 -07001388 meta->source->_rx_page_order = trans_pcie->rx_page_order;
Johannes Berg65b94a42012-03-05 11:24:38 -08001389 meta->source->handler_status = handler_status;
Stanislaw Gruszka2624e962011-04-20 16:02:58 +02001390 }
Tomas Winkler17b88922008-05-29 16:35:12 +08001391
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001392 iwl_pcie_cmdq_reclaim(trans, txq_id, index);
Tomas Winkler17b88922008-05-29 16:35:12 +08001393
Johannes Bergc2acea82009-07-24 11:13:05 -07001394 if (!(meta->flags & CMD_ASYNC)) {
Don Fry74fda972012-03-20 16:36:54 -07001395 if (!test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
Wey-Yi Guy05c89b92011-10-10 07:26:48 -07001396 IWL_WARN(trans,
1397 "HCMD_ACTIVE already clear for command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001398 get_cmd_string(trans_pcie, cmd->hdr.cmd));
Wey-Yi Guy05c89b92011-10-10 07:26:48 -07001399 }
Don Fry74fda972012-03-20 16:36:54 -07001400 clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001401 IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001402 get_cmd_string(trans_pcie, cmd->hdr.cmd));
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001403 wake_up(&trans_pcie->wait_command_queue);
Tomas Winkler17b88922008-05-29 16:35:12 +08001404 }
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001405
Zhu Yidd487442010-03-22 02:28:41 -07001406 meta->flags = 0;
Stanislaw Gruszka3598e172011-03-31 17:36:26 +02001407
Johannes Berg015c15e2012-03-05 11:24:24 -08001408 spin_unlock(&txq->lock);
Tomas Winkler17b88922008-05-29 16:35:12 +08001409}
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001410
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001411#define HOST_COMPLETE_TIMEOUT (2 * HZ)
1412
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001413static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
1414 struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001415{
Johannes Bergd9fb6462012-03-26 08:23:39 -07001416 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001417 int ret;
1418
1419 /* An asynchronous command can not expect an SKB to be set. */
1420 if (WARN_ON(cmd->flags & CMD_WANT_SKB))
1421 return -EINVAL;
1422
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001423 ret = iwl_pcie_enqueue_hcmd(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001424 if (ret < 0) {
Johannes Berg721c32f2012-03-06 13:30:40 -08001425 IWL_ERR(trans,
Todd Previteb36b1102011-11-10 06:55:02 -08001426 "Error sending %s: enqueue_hcmd failed: %d\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001427 get_cmd_string(trans_pcie, cmd->id), ret);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001428 return ret;
1429 }
1430 return 0;
1431}
1432
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001433static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
1434 struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001435{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001436 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001437 int cmd_idx;
1438 int ret;
1439
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001440 IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001441 get_cmd_string(trans_pcie, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001442
Johannes Berg2cc39c92012-03-06 13:30:41 -08001443 if (WARN_ON(test_and_set_bit(STATUS_HCMD_ACTIVE,
Don Fry74fda972012-03-20 16:36:54 -07001444 &trans_pcie->status))) {
Johannes Berg2cc39c92012-03-06 13:30:41 -08001445 IWL_ERR(trans, "Command %s: a command is already active!\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001446 get_cmd_string(trans_pcie, cmd->id));
Johannes Berg2cc39c92012-03-06 13:30:41 -08001447 return -EIO;
1448 }
1449
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001450 IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001451 get_cmd_string(trans_pcie, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001452
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001453 cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001454 if (cmd_idx < 0) {
1455 ret = cmd_idx;
Don Fry74fda972012-03-20 16:36:54 -07001456 clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
Johannes Berg721c32f2012-03-06 13:30:40 -08001457 IWL_ERR(trans,
Todd Previteb36b1102011-11-10 06:55:02 -08001458 "Error sending %s: enqueue_hcmd failed: %d\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001459 get_cmd_string(trans_pcie, cmd->id), ret);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001460 return ret;
1461 }
1462
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001463 ret = wait_event_timeout(trans_pcie->wait_command_queue,
Johannes Berg20d3b642012-05-16 22:54:29 +02001464 !test_bit(STATUS_HCMD_ACTIVE,
1465 &trans_pcie->status),
1466 HOST_COMPLETE_TIMEOUT);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001467 if (!ret) {
Don Fry74fda972012-03-20 16:36:54 -07001468 if (test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001469 struct iwl_txq *txq =
Meenakshi Venkataramanc6f600f2012-03-08 11:29:12 -08001470 &trans_pcie->txq[trans_pcie->cmd_queue];
Wey-Yi Guyd10630a2011-10-10 07:26:46 -07001471 struct iwl_queue *q = &txq->q;
1472
Johannes Berg721c32f2012-03-06 13:30:40 -08001473 IWL_ERR(trans,
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001474 "Error sending %s: time out after %dms.\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001475 get_cmd_string(trans_pcie, cmd->id),
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001476 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
1477
Johannes Berg721c32f2012-03-06 13:30:40 -08001478 IWL_ERR(trans,
Wey-Yi Guyd10630a2011-10-10 07:26:46 -07001479 "Current CMD queue read_ptr %d write_ptr %d\n",
1480 q->read_ptr, q->write_ptr);
1481
Don Fry74fda972012-03-20 16:36:54 -07001482 clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
Johannes Bergd9fb6462012-03-26 08:23:39 -07001483 IWL_DEBUG_INFO(trans,
1484 "Clearing HCMD_ACTIVE for command %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001485 get_cmd_string(trans_pcie, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001486 ret = -ETIMEDOUT;
1487 goto cancel;
1488 }
1489 }
1490
Johannes Bergd18aa872012-11-06 16:36:21 +01001491 if (test_bit(STATUS_FW_ERROR, &trans_pcie->status)) {
1492 IWL_ERR(trans, "FW error in SYNC CMD %s\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001493 get_cmd_string(trans_pcie, cmd->id));
Johannes Bergd18aa872012-11-06 16:36:21 +01001494 ret = -EIO;
1495 goto cancel;
1496 }
1497
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001498 if (test_bit(STATUS_RFKILL, &trans_pcie->status)) {
1499 IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
1500 ret = -ERFKILL;
1501 goto cancel;
1502 }
1503
Johannes Berg65b94a42012-03-05 11:24:38 -08001504 if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
Emmanuel Grumbach6d8f6ee2011-08-25 23:11:06 -07001505 IWL_ERR(trans, "Error: Response NULL in '%s'\n",
Emmanuel Grumbach990aa6d2012-11-14 12:39:52 +02001506 get_cmd_string(trans_pcie, cmd->id));
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001507 ret = -EIO;
1508 goto cancel;
1509 }
1510
1511 return 0;
1512
1513cancel:
1514 if (cmd->flags & CMD_WANT_SKB) {
1515 /*
1516 * Cancel the CMD_WANT_SKB flag for the cmd in the
1517 * TX cmd queue. Otherwise in case the cmd comes
1518 * in later, it will possibly set an invalid
1519 * address (cmd->meta.source).
1520 */
Johannes Bergbf8440e2012-03-19 17:12:06 +01001521 trans_pcie->txq[trans_pcie->cmd_queue].
1522 entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001523 }
Emmanuel Grumbach9cac4942011-11-10 06:55:20 -08001524
Johannes Berg65b94a42012-03-05 11:24:38 -08001525 if (cmd->resp_pkt) {
1526 iwl_free_resp(cmd);
1527 cmd->resp_pkt = NULL;
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001528 }
1529
1530 return ret;
1531}
1532
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001533int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001534{
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001535 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1536
Johannes Bergd18aa872012-11-06 16:36:21 +01001537 if (test_bit(STATUS_FW_ERROR, &trans_pcie->status))
1538 return -EIO;
1539
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001540 if (test_bit(STATUS_RFKILL, &trans_pcie->status))
1541 return -ERFKILL;
1542
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001543 if (cmd->flags & CMD_ASYNC)
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001544 return iwl_pcie_send_hcmd_async(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001545
Emmanuel Grumbachf946b522012-10-25 17:25:52 +02001546 /* We still can fail on RFKILL that can be asserted while we wait */
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001547 return iwl_pcie_send_hcmd_sync(trans, cmd);
Emmanuel Grumbach253a6342011-07-11 07:39:46 -07001548}
1549
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001550int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
1551 struct iwl_device_cmd *dev_cmd, int txq_id)
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001552{
Emmanuel Grumbach8ad71be2011-08-25 23:11:32 -07001553 struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001554 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1555 struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
1556 struct iwl_cmd_meta *out_meta;
1557 struct iwl_txq *txq;
1558 struct iwl_queue *q;
1559 dma_addr_t phys_addr = 0;
1560 dma_addr_t txcmd_phys;
1561 dma_addr_t scratch_phys;
1562 u16 len, firstlen, secondlen;
1563 u8 wait_write_ptr = 0;
1564 __le16 fc = hdr->frame_control;
1565 u8 hdr_len = ieee80211_hdrlen(fc);
1566 u16 __maybe_unused wifi_seq;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001567
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001568 txq = &trans_pcie->txq[txq_id];
1569 q = &txq->q;
Emmanuel Grumbach39644e92011-09-15 11:46:29 -07001570
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001571 if (unlikely(!test_bit(txq_id, trans_pcie->queue_used))) {
1572 WARN_ON_ONCE(1);
1573 return -EINVAL;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001574 }
1575
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001576 spin_lock(&txq->lock);
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001577
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001578 /* In AGG mode, the index in the ring must correspond to the WiFi
1579 * sequence number. This is a HW requirements to help the SCD to parse
1580 * the BA.
1581 * Check here that the packets are in the right place on the ring.
1582 */
1583#ifdef CONFIG_IWLWIFI_DEBUG
1584 wifi_seq = SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
1585 WARN_ONCE((iwl_read_prph(trans, SCD_AGGR_SEL) & BIT(txq_id)) &&
1586 ((wifi_seq & 0xff) != q->write_ptr),
1587 "Q: %d WiFi Seq %d tfdNum %d",
1588 txq_id, wifi_seq, q->write_ptr);
1589#endif
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001590
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001591 /* Set up driver data for this TFD */
1592 txq->entries[q->write_ptr].skb = skb;
1593 txq->entries[q->write_ptr].cmd = dev_cmd;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001594
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001595 dev_cmd->hdr.cmd = REPLY_TX;
1596 dev_cmd->hdr.sequence =
1597 cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
1598 INDEX_TO_SEQ(q->write_ptr)));
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001599
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001600 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1601 out_meta = &txq->entries[q->write_ptr].meta;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001602
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001603 /*
1604 * Use the first empty entry in this queue's command buffer array
1605 * to contain the Tx command and MAC header concatenated together
1606 * (payload data will be in another buffer).
1607 * Size of this varies, due to varying MAC header length.
1608 * If end is not dword aligned, we'll have 2 extra bytes at the end
1609 * of the MAC header (device reads on dword boundaries).
1610 * We'll tell device about this padding later.
1611 */
1612 len = sizeof(struct iwl_tx_cmd) +
1613 sizeof(struct iwl_cmd_header) + hdr_len;
1614 firstlen = (len + 3) & ~3;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001615
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001616 /* Tell NIC about any 2-byte padding after MAC header */
1617 if (firstlen != len)
1618 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1619
1620 /* Physical address of this Tx command's header (not MAC header!),
1621 * within command buffer array. */
1622 txcmd_phys = dma_map_single(trans->dev,
1623 &dev_cmd->hdr, firstlen,
1624 DMA_BIDIRECTIONAL);
1625 if (unlikely(dma_mapping_error(trans->dev, txcmd_phys)))
1626 goto out_err;
1627 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
1628 dma_unmap_len_set(out_meta, len, firstlen);
1629
1630 if (!ieee80211_has_morefrags(fc)) {
1631 txq->need_update = 1;
1632 } else {
1633 wait_write_ptr = 1;
1634 txq->need_update = 0;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001635 }
Johannes Berg7c5ba4a2012-04-09 17:46:54 -07001636
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001637 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1638 * if any (802.11 null frames have no payload). */
1639 secondlen = skb->len - hdr_len;
1640 if (secondlen > 0) {
1641 phys_addr = dma_map_single(trans->dev, skb->data + hdr_len,
1642 secondlen, DMA_TO_DEVICE);
1643 if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
1644 dma_unmap_single(trans->dev,
1645 dma_unmap_addr(out_meta, mapping),
1646 dma_unmap_len(out_meta, len),
1647 DMA_BIDIRECTIONAL);
1648 goto out_err;
1649 }
1650 }
Johannes Berg7c5ba4a2012-04-09 17:46:54 -07001651
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001652 /* Attach buffers to TFD */
1653 iwl_pcie_txq_build_tfd(trans, txq, txcmd_phys, firstlen, 1);
1654 if (secondlen > 0)
1655 iwl_pcie_txq_build_tfd(trans, txq, phys_addr, secondlen, 0);
1656
1657 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
1658 offsetof(struct iwl_tx_cmd, scratch);
1659
1660 /* take back ownership of DMA buffer to enable update */
1661 dma_sync_single_for_cpu(trans->dev, txcmd_phys, firstlen,
1662 DMA_BIDIRECTIONAL);
1663 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1664 tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
1665
Emmanuel Grumbachf02831b2012-11-14 14:44:18 +02001666 /* Set up entry for this TFD in Tx byte-count array */
1667 iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len));
1668
1669 dma_sync_single_for_device(trans->dev, txcmd_phys, firstlen,
1670 DMA_BIDIRECTIONAL);
1671
1672 trace_iwlwifi_dev_tx(trans->dev, skb,
1673 &txq->tfds[txq->q.write_ptr],
1674 sizeof(struct iwl_tfd),
1675 &dev_cmd->hdr, firstlen,
1676 skb->data + hdr_len, secondlen);
1677 trace_iwlwifi_dev_tx_data(trans->dev, skb,
1678 skb->data + hdr_len, secondlen);
1679
1680 /* start timer if queue currently empty */
1681 if (txq->need_update && q->read_ptr == q->write_ptr &&
1682 trans_pcie->wd_timeout)
1683 mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
1684
1685 /* Tell device the write index *just past* this latest filled TFD */
1686 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
1687 iwl_pcie_txq_inc_wr_ptr(trans, txq);
1688
1689 /*
1690 * At this point the frame is "transmitted" successfully
1691 * and we will get a TX status notification eventually,
1692 * regardless of the value of ret. "ret" only indicates
1693 * whether or not we should update the write pointer.
1694 */
1695 if (iwl_queue_space(q) < q->high_mark) {
1696 if (wait_write_ptr) {
1697 txq->need_update = 1;
1698 iwl_pcie_txq_inc_wr_ptr(trans, txq);
1699 } else {
1700 iwl_stop_queue(trans, txq);
1701 }
1702 }
1703 spin_unlock(&txq->lock);
1704 return 0;
1705out_err:
1706 spin_unlock(&txq->lock);
1707 return -1;
Emmanuel Grumbacha0eaad72011-08-25 23:11:00 -07001708}