Commit | Line | Data |
---|---|---|
1053d35f RR |
1 | /****************************************************************************** |
2 | * | |
fb4961db | 3 | * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved. |
1053d35f RR |
4 | * |
5 | * Portions of this file are derived from the ipw3945 project, as well | |
6 | * as portions of the ieee80211 subsystem header files. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of version 2 of the GNU General Public License as | |
10 | * published by the Free Software Foundation. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 | * more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along with | |
18 | * this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA | |
20 | * | |
21 | * The full GNU General Public License is included in this distribution in the | |
22 | * file called LICENSE. | |
23 | * | |
24 | * Contact Information: | |
759ef89f | 25 | * Intel Linux Wireless <ilw@linux.intel.com> |
1053d35f RR |
26 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 |
27 | * | |
28 | *****************************************************************************/ | |
fd4abac5 | 29 | #include <linux/etherdevice.h> |
5a0e3ad6 | 30 | #include <linux/slab.h> |
253a634c | 31 | #include <linux/sched.h> |
253a634c | 32 | |
522376d2 EG |
33 | #include "iwl-debug.h" |
34 | #include "iwl-csr.h" | |
35 | #include "iwl-prph.h" | |
1053d35f | 36 | #include "iwl-io.h" |
ed277c93 | 37 | #include "iwl-op-mode.h" |
6468a01a | 38 | #include "internal.h" |
6238b008 | 39 | /* FIXME: need to abstract out TX command (once we know what it looks like) */ |
1023fdc4 | 40 | #include "dvm/commands.h" |
1053d35f | 41 | |
522376d2 EG |
42 | #define IWL_TX_CRC_SIZE 4 |
43 | #define IWL_TX_DELIMITER_SIZE 4 | |
44 | ||
f02831be EG |
45 | /*************** 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 | ***************************************************/ | |
66 | static 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 | */ | |
85 | static 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 | ||
115 | ||
116 | static int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans, | |
117 | struct iwl_dma_ptr *ptr, size_t size) | |
118 | { | |
119 | if (WARN_ON(ptr->addr)) | |
120 | return -EINVAL; | |
121 | ||
122 | ptr->addr = dma_alloc_coherent(trans->dev, size, | |
123 | &ptr->dma, GFP_KERNEL); | |
124 | if (!ptr->addr) | |
125 | return -ENOMEM; | |
126 | ptr->size = size; | |
127 | return 0; | |
128 | } | |
129 | ||
130 | static void iwl_pcie_free_dma_ptr(struct iwl_trans *trans, | |
131 | struct iwl_dma_ptr *ptr) | |
132 | { | |
133 | if (unlikely(!ptr->addr)) | |
134 | return; | |
135 | ||
136 | dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma); | |
137 | memset(ptr, 0, sizeof(*ptr)); | |
138 | } | |
139 | ||
140 | static void iwl_pcie_txq_stuck_timer(unsigned long data) | |
141 | { | |
142 | struct iwl_txq *txq = (void *)data; | |
143 | struct iwl_queue *q = &txq->q; | |
144 | struct iwl_trans_pcie *trans_pcie = txq->trans_pcie; | |
145 | struct iwl_trans *trans = iwl_trans_pcie_get_trans(trans_pcie); | |
146 | u32 scd_sram_addr = trans_pcie->scd_base_addr + | |
147 | SCD_TX_STTS_QUEUE_OFFSET(txq->q.id); | |
148 | u8 buf[16]; | |
149 | int i; | |
150 | ||
151 | spin_lock(&txq->lock); | |
152 | /* check if triggered erroneously */ | |
153 | if (txq->q.read_ptr == txq->q.write_ptr) { | |
154 | spin_unlock(&txq->lock); | |
155 | return; | |
156 | } | |
157 | spin_unlock(&txq->lock); | |
158 | ||
159 | IWL_ERR(trans, "Queue %d stuck for %u ms.\n", txq->q.id, | |
160 | jiffies_to_msecs(trans_pcie->wd_timeout)); | |
161 | IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n", | |
162 | txq->q.read_ptr, txq->q.write_ptr); | |
163 | ||
164 | iwl_read_targ_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf)); | |
165 | ||
166 | iwl_print_hex_error(trans, buf, sizeof(buf)); | |
167 | ||
168 | for (i = 0; i < FH_TCSR_CHNL_NUM; i++) | |
169 | IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", i, | |
170 | iwl_read_direct32(trans, FH_TX_TRB_REG(i))); | |
171 | ||
172 | for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) { | |
173 | u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(i)); | |
174 | u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7; | |
175 | bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE)); | |
176 | u32 tbl_dw = | |
177 | iwl_read_targ_mem(trans, | |
178 | trans_pcie->scd_base_addr + | |
179 | SCD_TRANS_TBL_OFFSET_QUEUE(i)); | |
180 | ||
181 | if (i & 0x1) | |
182 | tbl_dw = (tbl_dw & 0xFFFF0000) >> 16; | |
183 | else | |
184 | tbl_dw = tbl_dw & 0x0000FFFF; | |
185 | ||
186 | IWL_ERR(trans, | |
187 | "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n", | |
188 | i, active ? "" : "in", fifo, tbl_dw, | |
189 | iwl_read_prph(trans, | |
190 | SCD_QUEUE_RDPTR(i)) & (txq->q.n_bd - 1), | |
191 | iwl_read_prph(trans, SCD_QUEUE_WRPTR(i))); | |
192 | } | |
193 | ||
194 | for (i = q->read_ptr; i != q->write_ptr; | |
195 | i = iwl_queue_inc_wrap(i, q->n_bd)) { | |
196 | struct iwl_tx_cmd *tx_cmd = | |
197 | (struct iwl_tx_cmd *)txq->entries[i].cmd->payload; | |
198 | IWL_ERR(trans, "scratch %d = 0x%08x\n", i, | |
199 | get_unaligned_le32(&tx_cmd->scratch)); | |
200 | } | |
201 | ||
202 | iwl_op_mode_nic_error(trans->op_mode); | |
203 | } | |
204 | ||
990aa6d7 EG |
205 | /* |
206 | * iwl_pcie_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array | |
48d42c42 | 207 | */ |
f02831be EG |
208 | static void iwl_pcie_txq_update_byte_cnt_tbl(struct iwl_trans *trans, |
209 | struct iwl_txq *txq, u16 byte_cnt) | |
48d42c42 | 210 | { |
105183b1 | 211 | struct iwlagn_scd_bc_tbl *scd_bc_tbl; |
20d3b647 | 212 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); |
48d42c42 EG |
213 | int write_ptr = txq->q.write_ptr; |
214 | int txq_id = txq->q.id; | |
215 | u8 sec_ctl = 0; | |
216 | u8 sta_id = 0; | |
217 | u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE; | |
218 | __le16 bc_ent; | |
132f98c2 | 219 | struct iwl_tx_cmd *tx_cmd = |
bf8440e6 | 220 | (void *) txq->entries[txq->q.write_ptr].cmd->payload; |
48d42c42 | 221 | |
105183b1 EG |
222 | scd_bc_tbl = trans_pcie->scd_bc_tbls.addr; |
223 | ||
48d42c42 EG |
224 | WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX); |
225 | ||
132f98c2 EG |
226 | sta_id = tx_cmd->sta_id; |
227 | sec_ctl = tx_cmd->sec_ctl; | |
48d42c42 EG |
228 | |
229 | switch (sec_ctl & TX_CMD_SEC_MSK) { | |
230 | case TX_CMD_SEC_CCM: | |
231 | len += CCMP_MIC_LEN; | |
232 | break; | |
233 | case TX_CMD_SEC_TKIP: | |
234 | len += TKIP_ICV_LEN; | |
235 | break; | |
236 | case TX_CMD_SEC_WEP: | |
237 | len += WEP_IV_LEN + WEP_ICV_LEN; | |
238 | break; | |
239 | } | |
240 | ||
241 | bc_ent = cpu_to_le16((len & 0xFFF) | (sta_id << 12)); | |
242 | ||
243 | scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent; | |
244 | ||
245 | if (write_ptr < TFD_QUEUE_SIZE_BC_DUP) | |
246 | scd_bc_tbl[txq_id]. | |
247 | tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent; | |
248 | } | |
249 | ||
f02831be EG |
250 | static void iwl_pcie_txq_inval_byte_cnt_tbl(struct iwl_trans *trans, |
251 | struct iwl_txq *txq) | |
252 | { | |
253 | struct iwl_trans_pcie *trans_pcie = | |
254 | IWL_TRANS_GET_PCIE_TRANS(trans); | |
255 | struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr; | |
256 | int txq_id = txq->q.id; | |
257 | int read_ptr = txq->q.read_ptr; | |
258 | u8 sta_id = 0; | |
259 | __le16 bc_ent; | |
260 | struct iwl_tx_cmd *tx_cmd = | |
261 | (void *)txq->entries[txq->q.read_ptr].cmd->payload; | |
262 | ||
263 | WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX); | |
264 | ||
265 | if (txq_id != trans_pcie->cmd_queue) | |
266 | sta_id = tx_cmd->sta_id; | |
267 | ||
268 | bc_ent = cpu_to_le16(1 | (sta_id << 12)); | |
269 | scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent; | |
270 | ||
271 | if (read_ptr < TFD_QUEUE_SIZE_BC_DUP) | |
272 | scd_bc_tbl[txq_id]. | |
273 | tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent; | |
274 | } | |
275 | ||
990aa6d7 EG |
276 | /* |
277 | * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware | |
fd4abac5 | 278 | */ |
990aa6d7 | 279 | void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans, struct iwl_txq *txq) |
fd4abac5 TW |
280 | { |
281 | u32 reg = 0; | |
fd4abac5 TW |
282 | int txq_id = txq->q.id; |
283 | ||
284 | if (txq->need_update == 0) | |
7bfedc59 | 285 | return; |
fd4abac5 | 286 | |
035f7ff2 | 287 | if (trans->cfg->base_params->shadow_reg_enable) { |
f81c1f48 | 288 | /* shadow register enabled */ |
1042db2a | 289 | iwl_write32(trans, HBUS_TARG_WRPTR, |
f81c1f48 WYG |
290 | txq->q.write_ptr | (txq_id << 8)); |
291 | } else { | |
47107e84 DF |
292 | struct iwl_trans_pcie *trans_pcie = |
293 | IWL_TRANS_GET_PCIE_TRANS(trans); | |
f81c1f48 | 294 | /* if we're trying to save power */ |
01d651d4 | 295 | if (test_bit(STATUS_TPOWER_PMI, &trans_pcie->status)) { |
f81c1f48 WYG |
296 | /* wake up nic if it's powered down ... |
297 | * uCode will wake up, and interrupt us again, so next | |
298 | * time we'll skip this part. */ | |
1042db2a | 299 | reg = iwl_read32(trans, CSR_UCODE_DRV_GP1); |
fd4abac5 | 300 | |
f81c1f48 | 301 | if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) { |
fd656935 | 302 | IWL_DEBUG_INFO(trans, |
f81c1f48 WYG |
303 | "Tx queue %d requesting wakeup," |
304 | " GP1 = 0x%x\n", txq_id, reg); | |
1042db2a | 305 | iwl_set_bit(trans, CSR_GP_CNTRL, |
f81c1f48 WYG |
306 | CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ); |
307 | return; | |
308 | } | |
fd4abac5 | 309 | |
1042db2a | 310 | iwl_write_direct32(trans, HBUS_TARG_WRPTR, |
fd4abac5 | 311 | txq->q.write_ptr | (txq_id << 8)); |
fd4abac5 | 312 | |
f81c1f48 WYG |
313 | /* |
314 | * else not in power-save mode, | |
315 | * uCode will never sleep when we're | |
316 | * trying to tx (during RFKILL, we're not trying to tx). | |
317 | */ | |
318 | } else | |
1042db2a | 319 | iwl_write32(trans, HBUS_TARG_WRPTR, |
f81c1f48 WYG |
320 | txq->q.write_ptr | (txq_id << 8)); |
321 | } | |
fd4abac5 | 322 | txq->need_update = 0; |
fd4abac5 | 323 | } |
fd4abac5 | 324 | |
f02831be | 325 | static inline dma_addr_t iwl_pcie_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx) |
214d14d4 JB |
326 | { |
327 | struct iwl_tfd_tb *tb = &tfd->tbs[idx]; | |
328 | ||
329 | dma_addr_t addr = get_unaligned_le32(&tb->lo); | |
330 | if (sizeof(dma_addr_t) > sizeof(u32)) | |
331 | addr |= | |
332 | ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16; | |
333 | ||
334 | return addr; | |
335 | } | |
336 | ||
f02831be | 337 | static inline u16 iwl_pcie_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx) |
214d14d4 JB |
338 | { |
339 | struct iwl_tfd_tb *tb = &tfd->tbs[idx]; | |
340 | ||
341 | return le16_to_cpu(tb->hi_n_len) >> 4; | |
342 | } | |
343 | ||
f02831be EG |
344 | static inline void iwl_pcie_tfd_set_tb(struct iwl_tfd *tfd, u8 idx, |
345 | dma_addr_t addr, u16 len) | |
214d14d4 JB |
346 | { |
347 | struct iwl_tfd_tb *tb = &tfd->tbs[idx]; | |
348 | u16 hi_n_len = len << 4; | |
349 | ||
350 | put_unaligned_le32(addr, &tb->lo); | |
351 | if (sizeof(dma_addr_t) > sizeof(u32)) | |
352 | hi_n_len |= ((addr >> 16) >> 16) & 0xF; | |
353 | ||
354 | tb->hi_n_len = cpu_to_le16(hi_n_len); | |
355 | ||
356 | tfd->num_tbs = idx + 1; | |
357 | } | |
358 | ||
f02831be | 359 | static inline u8 iwl_pcie_tfd_get_num_tbs(struct iwl_tfd *tfd) |
214d14d4 JB |
360 | { |
361 | return tfd->num_tbs & 0x1f; | |
362 | } | |
363 | ||
f02831be EG |
364 | static void iwl_pcie_tfd_unmap(struct iwl_trans *trans, |
365 | struct iwl_cmd_meta *meta, struct iwl_tfd *tfd, | |
366 | enum dma_data_direction dma_dir) | |
214d14d4 | 367 | { |
214d14d4 JB |
368 | int i; |
369 | int num_tbs; | |
370 | ||
214d14d4 | 371 | /* Sanity check on number of chunks */ |
f02831be | 372 | num_tbs = iwl_pcie_tfd_get_num_tbs(tfd); |
214d14d4 JB |
373 | |
374 | if (num_tbs >= IWL_NUM_OF_TBS) { | |
6d8f6eeb | 375 | IWL_ERR(trans, "Too many chunks: %i\n", num_tbs); |
214d14d4 JB |
376 | /* @todo issue fatal error, it is quite serious situation */ |
377 | return; | |
378 | } | |
379 | ||
380 | /* Unmap tx_cmd */ | |
381 | if (num_tbs) | |
1042db2a | 382 | dma_unmap_single(trans->dev, |
4ce7cc2b JB |
383 | dma_unmap_addr(meta, mapping), |
384 | dma_unmap_len(meta, len), | |
795414db | 385 | DMA_BIDIRECTIONAL); |
214d14d4 JB |
386 | |
387 | /* Unmap chunks, if any. */ | |
388 | for (i = 1; i < num_tbs; i++) | |
f02831be EG |
389 | dma_unmap_single(trans->dev, iwl_pcie_tfd_tb_get_addr(tfd, i), |
390 | iwl_pcie_tfd_tb_get_len(tfd, i), dma_dir); | |
ebed633c EG |
391 | |
392 | tfd->num_tbs = 0; | |
4ce7cc2b JB |
393 | } |
394 | ||
990aa6d7 EG |
395 | /* |
396 | * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr] | |
6d8f6eeb | 397 | * @trans - transport private data |
4ce7cc2b | 398 | * @txq - tx queue |
ebed633c | 399 | * @dma_dir - the direction of the DMA mapping |
4ce7cc2b JB |
400 | * |
401 | * Does NOT advance any TFD circular buffer read/write indexes | |
402 | * Does NOT free the TFD itself (which is within circular buffer) | |
403 | */ | |
f02831be EG |
404 | static void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq, |
405 | enum dma_data_direction dma_dir) | |
4ce7cc2b JB |
406 | { |
407 | struct iwl_tfd *tfd_tmp = txq->tfds; | |
4ce7cc2b | 408 | |
ebed633c EG |
409 | /* rd_ptr is bounded by n_bd and idx is bounded by n_window */ |
410 | int rd_ptr = txq->q.read_ptr; | |
411 | int idx = get_cmd_index(&txq->q, rd_ptr); | |
412 | ||
015c15e1 JB |
413 | lockdep_assert_held(&txq->lock); |
414 | ||
ebed633c | 415 | /* We have only q->n_window txq->entries, but we use q->n_bd tfds */ |
f02831be EG |
416 | iwl_pcie_tfd_unmap(trans, &txq->entries[idx].meta, &tfd_tmp[rd_ptr], |
417 | dma_dir); | |
214d14d4 JB |
418 | |
419 | /* free SKB */ | |
bf8440e6 | 420 | if (txq->entries) { |
214d14d4 JB |
421 | struct sk_buff *skb; |
422 | ||
ebed633c | 423 | skb = txq->entries[idx].skb; |
214d14d4 | 424 | |
909e9b23 EG |
425 | /* Can be called from irqs-disabled context |
426 | * If skb is not NULL, it means that the whole queue is being | |
427 | * freed and that the queue is not empty - free the skb | |
428 | */ | |
214d14d4 | 429 | if (skb) { |
ed277c93 | 430 | iwl_op_mode_free_skb(trans->op_mode, skb); |
ebed633c | 431 | txq->entries[idx].skb = NULL; |
214d14d4 JB |
432 | } |
433 | } | |
434 | } | |
435 | ||
f02831be EG |
436 | static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq, |
437 | dma_addr_t addr, u16 len, u8 reset) | |
214d14d4 JB |
438 | { |
439 | struct iwl_queue *q; | |
440 | struct iwl_tfd *tfd, *tfd_tmp; | |
441 | u32 num_tbs; | |
442 | ||
443 | q = &txq->q; | |
4ce7cc2b | 444 | tfd_tmp = txq->tfds; |
214d14d4 JB |
445 | tfd = &tfd_tmp[q->write_ptr]; |
446 | ||
f02831be EG |
447 | if (reset) |
448 | memset(tfd, 0, sizeof(*tfd)); | |
449 | ||
450 | num_tbs = iwl_pcie_tfd_get_num_tbs(tfd); | |
451 | ||
452 | /* Each TFD can point to a maximum 20 Tx buffers */ | |
453 | if (num_tbs >= IWL_NUM_OF_TBS) { | |
454 | IWL_ERR(trans, "Error can not send more than %d chunks\n", | |
455 | IWL_NUM_OF_TBS); | |
456 | return -EINVAL; | |
457 | } | |
458 | ||
459 | if (WARN_ON(addr & ~DMA_BIT_MASK(36))) | |
460 | return -EINVAL; | |
461 | ||
462 | if (unlikely(addr & ~IWL_TX_DMA_MASK)) | |
463 | IWL_ERR(trans, "Unaligned address = %llx\n", | |
464 | (unsigned long long)addr); | |
465 | ||
466 | iwl_pcie_tfd_set_tb(tfd, num_tbs, addr, len); | |
467 | ||
468 | return 0; | |
469 | } | |
470 | ||
471 | static int iwl_pcie_txq_alloc(struct iwl_trans *trans, | |
472 | struct iwl_txq *txq, int slots_num, | |
473 | u32 txq_id) | |
474 | { | |
475 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); | |
476 | size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX; | |
477 | int i; | |
478 | ||
479 | if (WARN_ON(txq->entries || txq->tfds)) | |
480 | return -EINVAL; | |
481 | ||
482 | setup_timer(&txq->stuck_timer, iwl_pcie_txq_stuck_timer, | |
483 | (unsigned long)txq); | |
484 | txq->trans_pcie = trans_pcie; | |
485 | ||
486 | txq->q.n_window = slots_num; | |
487 | ||
488 | txq->entries = kcalloc(slots_num, | |
489 | sizeof(struct iwl_pcie_txq_entry), | |
490 | GFP_KERNEL); | |
491 | ||
492 | if (!txq->entries) | |
493 | goto error; | |
494 | ||
495 | if (txq_id == trans_pcie->cmd_queue) | |
496 | for (i = 0; i < slots_num; i++) { | |
497 | txq->entries[i].cmd = | |
498 | kmalloc(sizeof(struct iwl_device_cmd), | |
499 | GFP_KERNEL); | |
500 | if (!txq->entries[i].cmd) | |
501 | goto error; | |
502 | } | |
503 | ||
504 | /* Circular buffer of transmit frame descriptors (TFDs), | |
505 | * shared with device */ | |
506 | txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz, | |
507 | &txq->q.dma_addr, GFP_KERNEL); | |
508 | if (!txq->tfds) { | |
509 | IWL_ERR(trans, "dma_alloc_coherent(%zd) failed\n", tfd_sz); | |
510 | goto error; | |
511 | } | |
512 | txq->q.id = txq_id; | |
513 | ||
514 | return 0; | |
515 | error: | |
516 | if (txq->entries && txq_id == trans_pcie->cmd_queue) | |
517 | for (i = 0; i < slots_num; i++) | |
518 | kfree(txq->entries[i].cmd); | |
519 | kfree(txq->entries); | |
520 | txq->entries = NULL; | |
521 | ||
522 | return -ENOMEM; | |
523 | ||
524 | } | |
525 | ||
526 | static int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq, | |
527 | int slots_num, u32 txq_id) | |
528 | { | |
529 | int ret; | |
530 | ||
531 | txq->need_update = 0; | |
532 | ||
533 | /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise | |
534 | * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */ | |
535 | BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1)); | |
536 | ||
537 | /* Initialize queue's high/low-water marks, and head/tail indexes */ | |
538 | ret = iwl_queue_init(&txq->q, TFD_QUEUE_SIZE_MAX, slots_num, | |
539 | txq_id); | |
540 | if (ret) | |
541 | return ret; | |
542 | ||
543 | spin_lock_init(&txq->lock); | |
544 | ||
545 | /* | |
546 | * Tell nic where to find circular buffer of Tx Frame Descriptors for | |
547 | * given Tx queue, and enable the DMA channel used for that queue. | |
548 | * Circular buffer (TFD queue in DRAM) physical base address */ | |
549 | iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id), | |
550 | txq->q.dma_addr >> 8); | |
551 | ||
552 | return 0; | |
553 | } | |
554 | ||
555 | /* | |
556 | * iwl_pcie_txq_unmap - Unmap any remaining DMA mappings and free skb's | |
557 | */ | |
558 | static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id) | |
559 | { | |
560 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); | |
561 | struct iwl_txq *txq = &trans_pcie->txq[txq_id]; | |
562 | struct iwl_queue *q = &txq->q; | |
563 | enum dma_data_direction dma_dir; | |
564 | ||
565 | if (!q->n_bd) | |
566 | return; | |
567 | ||
568 | /* In the command queue, all the TBs are mapped as BIDI | |
569 | * so unmap them as such. | |
570 | */ | |
571 | if (txq_id == trans_pcie->cmd_queue) | |
572 | dma_dir = DMA_BIDIRECTIONAL; | |
573 | else | |
574 | dma_dir = DMA_TO_DEVICE; | |
575 | ||
576 | spin_lock_bh(&txq->lock); | |
577 | while (q->write_ptr != q->read_ptr) { | |
578 | iwl_pcie_txq_free_tfd(trans, txq, dma_dir); | |
579 | q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd); | |
580 | } | |
581 | spin_unlock_bh(&txq->lock); | |
582 | } | |
583 | ||
584 | /* | |
585 | * iwl_pcie_txq_free - Deallocate DMA queue. | |
586 | * @txq: Transmit queue to deallocate. | |
587 | * | |
588 | * Empty queue by removing and destroying all BD's. | |
589 | * Free all buffers. | |
590 | * 0-fill, but do not free "txq" descriptor structure. | |
591 | */ | |
592 | static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id) | |
593 | { | |
594 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); | |
595 | struct iwl_txq *txq = &trans_pcie->txq[txq_id]; | |
596 | struct device *dev = trans->dev; | |
597 | int i; | |
598 | ||
599 | if (WARN_ON(!txq)) | |
600 | return; | |
601 | ||
602 | iwl_pcie_txq_unmap(trans, txq_id); | |
603 | ||
604 | /* De-alloc array of command/tx buffers */ | |
605 | if (txq_id == trans_pcie->cmd_queue) | |
606 | for (i = 0; i < txq->q.n_window; i++) { | |
607 | kfree(txq->entries[i].cmd); | |
608 | kfree(txq->entries[i].copy_cmd); | |
609 | kfree(txq->entries[i].free_buf); | |
610 | } | |
611 | ||
612 | /* De-alloc circular buffer of TFDs */ | |
613 | if (txq->q.n_bd) { | |
614 | dma_free_coherent(dev, sizeof(struct iwl_tfd) * | |
615 | txq->q.n_bd, txq->tfds, txq->q.dma_addr); | |
616 | memset(&txq->q.dma_addr, 0, sizeof(txq->q.dma_addr)); | |
617 | } | |
618 | ||
619 | kfree(txq->entries); | |
620 | txq->entries = NULL; | |
621 | ||
622 | del_timer_sync(&txq->stuck_timer); | |
623 | ||
624 | /* 0-fill queue descriptor structure */ | |
625 | memset(txq, 0, sizeof(*txq)); | |
626 | } | |
627 | ||
628 | /* | |
629 | * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask | |
630 | */ | |
631 | static void iwl_pcie_txq_set_sched(struct iwl_trans *trans, u32 mask) | |
632 | { | |
633 | struct iwl_trans_pcie __maybe_unused *trans_pcie = | |
634 | IWL_TRANS_GET_PCIE_TRANS(trans); | |
635 | ||
636 | iwl_write_prph(trans, SCD_TXFACT, mask); | |
637 | } | |
638 | ||
639 | void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr) | |
640 | { | |
641 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); | |
642 | u32 a; | |
643 | int chan; | |
644 | u32 reg_val; | |
645 | ||
646 | /* make sure all queue are not stopped/used */ | |
647 | memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped)); | |
648 | memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used)); | |
649 | ||
650 | trans_pcie->scd_base_addr = | |
651 | iwl_read_prph(trans, SCD_SRAM_BASE_ADDR); | |
652 | ||
653 | WARN_ON(scd_base_addr != 0 && | |
654 | scd_base_addr != trans_pcie->scd_base_addr); | |
655 | ||
656 | a = trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_LOWER_BOUND; | |
657 | /* reset conext data memory */ | |
658 | for (; a < trans_pcie->scd_base_addr + SCD_CONTEXT_MEM_UPPER_BOUND; | |
659 | a += 4) | |
660 | iwl_write_targ_mem(trans, a, 0); | |
661 | /* reset tx status memory */ | |
662 | for (; a < trans_pcie->scd_base_addr + SCD_TX_STTS_MEM_UPPER_BOUND; | |
663 | a += 4) | |
664 | iwl_write_targ_mem(trans, a, 0); | |
665 | for (; a < trans_pcie->scd_base_addr + | |
666 | SCD_TRANS_TBL_OFFSET_QUEUE( | |
667 | trans->cfg->base_params->num_of_queues); | |
668 | a += 4) | |
669 | iwl_write_targ_mem(trans, a, 0); | |
670 | ||
671 | iwl_write_prph(trans, SCD_DRAM_BASE_ADDR, | |
672 | trans_pcie->scd_bc_tbls.dma >> 10); | |
673 | ||
674 | /* The chain extension of the SCD doesn't work well. This feature is | |
675 | * enabled by default by the HW, so we need to disable it manually. | |
676 | */ | |
677 | iwl_write_prph(trans, SCD_CHAINEXT_EN, 0); | |
678 | ||
679 | iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue, | |
680 | trans_pcie->cmd_fifo); | |
681 | ||
682 | /* Activate all Tx DMA/FIFO channels */ | |
683 | iwl_pcie_txq_set_sched(trans, IWL_MASK(0, 7)); | |
684 | ||
685 | /* Enable DMA channel */ | |
686 | for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++) | |
687 | iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan), | |
688 | FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE | | |
689 | FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE); | |
690 | ||
691 | /* Update FH chicken bits */ | |
692 | reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG); | |
693 | iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG, | |
694 | reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN); | |
695 | ||
696 | /* Enable L1-Active */ | |
697 | iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG, | |
698 | APMG_PCIDEV_STT_VAL_L1_ACT_DIS); | |
699 | } | |
700 | ||
701 | /* | |
702 | * iwl_pcie_tx_stop - Stop all Tx DMA channels | |
703 | */ | |
704 | int iwl_pcie_tx_stop(struct iwl_trans *trans) | |
705 | { | |
706 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); | |
707 | int ch, txq_id, ret; | |
708 | unsigned long flags; | |
709 | ||
710 | /* Turn off all Tx DMA fifos */ | |
711 | spin_lock_irqsave(&trans_pcie->irq_lock, flags); | |
712 | ||
713 | iwl_pcie_txq_set_sched(trans, 0); | |
714 | ||
715 | /* Stop each Tx DMA channel, and wait for it to be idle */ | |
716 | for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) { | |
717 | iwl_write_direct32(trans, | |
718 | FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0); | |
719 | ret = iwl_poll_direct_bit(trans, FH_TSSR_TX_STATUS_REG, | |
720 | FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000); | |
721 | if (ret < 0) | |
722 | IWL_ERR(trans, | |
723 | "Failing on timeout while stopping DMA channel %d [0x%08x]\n", | |
724 | ch, | |
725 | iwl_read_direct32(trans, | |
726 | FH_TSSR_TX_STATUS_REG)); | |
727 | } | |
728 | spin_unlock_irqrestore(&trans_pcie->irq_lock, flags); | |
729 | ||
730 | if (!trans_pcie->txq) { | |
731 | IWL_WARN(trans, | |
732 | "Stopping tx queues that aren't allocated...\n"); | |
733 | return 0; | |
734 | } | |
735 | ||
736 | /* Unmap DMA from host system and free skb's */ | |
737 | for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues; | |
738 | txq_id++) | |
739 | iwl_pcie_txq_unmap(trans, txq_id); | |
740 | ||
741 | return 0; | |
742 | } | |
743 | ||
744 | /* | |
745 | * iwl_trans_tx_free - Free TXQ Context | |
746 | * | |
747 | * Destroy all TX DMA queues and structures | |
748 | */ | |
749 | void iwl_pcie_tx_free(struct iwl_trans *trans) | |
750 | { | |
751 | int txq_id; | |
752 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); | |
753 | ||
754 | /* Tx queues */ | |
755 | if (trans_pcie->txq) { | |
756 | for (txq_id = 0; | |
757 | txq_id < trans->cfg->base_params->num_of_queues; txq_id++) | |
758 | iwl_pcie_txq_free(trans, txq_id); | |
759 | } | |
760 | ||
761 | kfree(trans_pcie->txq); | |
762 | trans_pcie->txq = NULL; | |
763 | ||
764 | iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw); | |
765 | ||
766 | iwl_pcie_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls); | |
767 | } | |
768 | ||
769 | /* | |
770 | * iwl_pcie_tx_alloc - allocate TX context | |
771 | * Allocate all Tx DMA structures and initialize them | |
772 | */ | |
773 | static int iwl_pcie_tx_alloc(struct iwl_trans *trans) | |
774 | { | |
775 | int ret; | |
776 | int txq_id, slots_num; | |
777 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); | |
778 | ||
779 | u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues * | |
780 | sizeof(struct iwlagn_scd_bc_tbl); | |
781 | ||
782 | /*It is not allowed to alloc twice, so warn when this happens. | |
783 | * We cannot rely on the previous allocation, so free and fail */ | |
784 | if (WARN_ON(trans_pcie->txq)) { | |
785 | ret = -EINVAL; | |
786 | goto error; | |
787 | } | |
788 | ||
789 | ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls, | |
790 | scd_bc_tbls_size); | |
791 | if (ret) { | |
792 | IWL_ERR(trans, "Scheduler BC Table allocation failed\n"); | |
793 | goto error; | |
794 | } | |
795 | ||
796 | /* Alloc keep-warm buffer */ | |
797 | ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE); | |
798 | if (ret) { | |
799 | IWL_ERR(trans, "Keep Warm allocation failed\n"); | |
800 | goto error; | |
801 | } | |
802 | ||
803 | trans_pcie->txq = kcalloc(trans->cfg->base_params->num_of_queues, | |
804 | sizeof(struct iwl_txq), GFP_KERNEL); | |
805 | if (!trans_pcie->txq) { | |
806 | IWL_ERR(trans, "Not enough memory for txq\n"); | |
807 | ret = ENOMEM; | |
808 | goto error; | |
809 | } | |
810 | ||
811 | /* Alloc and init all Tx queues, including the command queue (#4/#9) */ | |
812 | for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues; | |
813 | txq_id++) { | |
814 | slots_num = (txq_id == trans_pcie->cmd_queue) ? | |
815 | TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS; | |
816 | ret = iwl_pcie_txq_alloc(trans, &trans_pcie->txq[txq_id], | |
817 | slots_num, txq_id); | |
818 | if (ret) { | |
819 | IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id); | |
820 | goto error; | |
821 | } | |
822 | } | |
823 | ||
824 | return 0; | |
825 | ||
826 | error: | |
827 | iwl_pcie_tx_free(trans); | |
828 | ||
829 | return ret; | |
830 | } | |
831 | int iwl_pcie_tx_init(struct iwl_trans *trans) | |
832 | { | |
833 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); | |
834 | int ret; | |
835 | int txq_id, slots_num; | |
836 | unsigned long flags; | |
837 | bool alloc = false; | |
838 | ||
839 | if (!trans_pcie->txq) { | |
840 | ret = iwl_pcie_tx_alloc(trans); | |
841 | if (ret) | |
842 | goto error; | |
843 | alloc = true; | |
844 | } | |
845 | ||
846 | spin_lock_irqsave(&trans_pcie->irq_lock, flags); | |
847 | ||
848 | /* Turn off all Tx DMA fifos */ | |
849 | iwl_write_prph(trans, SCD_TXFACT, 0); | |
850 | ||
851 | /* Tell NIC where to find the "keep warm" buffer */ | |
852 | iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG, | |
853 | trans_pcie->kw.dma >> 4); | |
854 | ||
855 | spin_unlock_irqrestore(&trans_pcie->irq_lock, flags); | |
856 | ||
857 | /* Alloc and init all Tx queues, including the command queue (#4/#9) */ | |
858 | for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues; | |
859 | txq_id++) { | |
860 | slots_num = (txq_id == trans_pcie->cmd_queue) ? | |
861 | TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS; | |
862 | ret = iwl_pcie_txq_init(trans, &trans_pcie->txq[txq_id], | |
863 | slots_num, txq_id); | |
864 | if (ret) { | |
865 | IWL_ERR(trans, "Tx %d queue init failed\n", txq_id); | |
866 | goto error; | |
867 | } | |
868 | } | |
869 | ||
870 | return 0; | |
871 | error: | |
872 | /*Upon error, free only if we allocated something */ | |
873 | if (alloc) | |
874 | iwl_pcie_tx_free(trans); | |
875 | return ret; | |
876 | } | |
877 | ||
878 | static inline void iwl_pcie_txq_progress(struct iwl_trans_pcie *trans_pcie, | |
879 | struct iwl_txq *txq) | |
880 | { | |
881 | if (!trans_pcie->wd_timeout) | |
882 | return; | |
883 | ||
884 | /* | |
885 | * if empty delete timer, otherwise move timer forward | |
886 | * since we're making progress on this queue | |
887 | */ | |
888 | if (txq->q.read_ptr == txq->q.write_ptr) | |
889 | del_timer(&txq->stuck_timer); | |
890 | else | |
891 | mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout); | |
892 | } | |
893 | ||
894 | /* Frees buffers until index _not_ inclusive */ | |
f6d497cd EG |
895 | void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn, |
896 | struct sk_buff_head *skbs) | |
f02831be EG |
897 | { |
898 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); | |
899 | struct iwl_txq *txq = &trans_pcie->txq[txq_id]; | |
f6d497cd EG |
900 | /* n_bd is usually 256 => n_bd - 1 = 0xff */ |
901 | int tfd_num = ssn & (txq->q.n_bd - 1); | |
f02831be EG |
902 | struct iwl_queue *q = &txq->q; |
903 | int last_to_free; | |
f02831be EG |
904 | |
905 | /* This function is not meant to release cmd queue*/ | |
906 | if (WARN_ON(txq_id == trans_pcie->cmd_queue)) | |
f6d497cd | 907 | return; |
214d14d4 | 908 | |
f6d497cd EG |
909 | spin_lock(&txq->lock); |
910 | ||
911 | if (txq->q.read_ptr == tfd_num) | |
912 | goto out; | |
913 | ||
914 | IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n", | |
915 | txq_id, txq->q.read_ptr, tfd_num, ssn); | |
214d14d4 | 916 | |
f02831be EG |
917 | /*Since we free until index _not_ inclusive, the one before index is |
918 | * the last we will free. This one must be used */ | |
f6d497cd | 919 | last_to_free = iwl_queue_dec_wrap(tfd_num, q->n_bd); |
f02831be | 920 | |
f6d497cd | 921 | if (iwl_queue_used(q, last_to_free) == 0) { |
f02831be EG |
922 | IWL_ERR(trans, |
923 | "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n", | |
924 | __func__, txq_id, last_to_free, q->n_bd, | |
925 | q->write_ptr, q->read_ptr); | |
f6d497cd | 926 | goto out; |
214d14d4 JB |
927 | } |
928 | ||
f02831be | 929 | if (WARN_ON(!skb_queue_empty(skbs))) |
f6d497cd | 930 | goto out; |
214d14d4 | 931 | |
f02831be | 932 | for (; |
f6d497cd | 933 | q->read_ptr != tfd_num; |
f02831be | 934 | q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) { |
214d14d4 | 935 | |
f02831be EG |
936 | if (WARN_ON_ONCE(txq->entries[txq->q.read_ptr].skb == NULL)) |
937 | continue; | |
214d14d4 | 938 | |
f02831be | 939 | __skb_queue_tail(skbs, txq->entries[txq->q.read_ptr].skb); |
214d14d4 | 940 | |
f02831be | 941 | txq->entries[txq->q.read_ptr].skb = NULL; |
fd4abac5 | 942 | |
f02831be | 943 | iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq); |
fd4abac5 | 944 | |
f02831be | 945 | iwl_pcie_txq_free_tfd(trans, txq, DMA_TO_DEVICE); |
f02831be | 946 | } |
fd4abac5 | 947 | |
f02831be EG |
948 | iwl_pcie_txq_progress(trans_pcie, txq); |
949 | ||
f6d497cd EG |
950 | if (iwl_queue_space(&txq->q) > txq->q.low_mark) |
951 | iwl_wake_queue(trans, txq); | |
952 | out: | |
f02831be | 953 | spin_unlock(&txq->lock); |
1053d35f RR |
954 | } |
955 | ||
f02831be EG |
956 | /* |
957 | * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd | |
958 | * | |
959 | * When FW advances 'R' index, all entries between old and new 'R' index | |
960 | * need to be reclaimed. As result, some free space forms. If there is | |
961 | * enough free space (> low mark), wake the stack that feeds us. | |
962 | */ | |
963 | static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx) | |
48d42c42 | 964 | { |
f02831be EG |
965 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); |
966 | struct iwl_txq *txq = &trans_pcie->txq[txq_id]; | |
967 | struct iwl_queue *q = &txq->q; | |
968 | int nfreed = 0; | |
48d42c42 | 969 | |
f02831be | 970 | lockdep_assert_held(&txq->lock); |
48d42c42 | 971 | |
f02831be EG |
972 | if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) { |
973 | IWL_ERR(trans, | |
974 | "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n", | |
975 | __func__, txq_id, idx, q->n_bd, | |
976 | q->write_ptr, q->read_ptr); | |
977 | return; | |
978 | } | |
48d42c42 | 979 | |
f02831be EG |
980 | for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx; |
981 | q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) { | |
48d42c42 | 982 | |
f02831be EG |
983 | if (nfreed++ > 0) { |
984 | IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n", | |
985 | idx, q->write_ptr, q->read_ptr); | |
986 | iwl_op_mode_nic_error(trans->op_mode); | |
987 | } | |
988 | } | |
989 | ||
990 | iwl_pcie_txq_progress(trans_pcie, txq); | |
48d42c42 EG |
991 | } |
992 | ||
f02831be | 993 | static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid, |
1ce8658c | 994 | u16 txq_id) |
48d42c42 | 995 | { |
20d3b647 | 996 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); |
48d42c42 EG |
997 | u32 tbl_dw_addr; |
998 | u32 tbl_dw; | |
999 | u16 scd_q2ratid; | |
1000 | ||
1001 | scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK; | |
1002 | ||
105183b1 | 1003 | tbl_dw_addr = trans_pcie->scd_base_addr + |
48d42c42 EG |
1004 | SCD_TRANS_TBL_OFFSET_QUEUE(txq_id); |
1005 | ||
1042db2a | 1006 | tbl_dw = iwl_read_targ_mem(trans, tbl_dw_addr); |
48d42c42 EG |
1007 | |
1008 | if (txq_id & 0x1) | |
1009 | tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF); | |
1010 | else | |
1011 | tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000); | |
1012 | ||
1042db2a | 1013 | iwl_write_targ_mem(trans, tbl_dw_addr, tbl_dw); |
48d42c42 EG |
1014 | |
1015 | return 0; | |
1016 | } | |
1017 | ||
f02831be EG |
1018 | static inline void iwl_pcie_txq_set_inactive(struct iwl_trans *trans, |
1019 | u16 txq_id) | |
48d42c42 EG |
1020 | { |
1021 | /* Simply stop the queue, but don't change any configuration; | |
1022 | * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */ | |
1042db2a | 1023 | iwl_write_prph(trans, |
48d42c42 EG |
1024 | SCD_QUEUE_STATUS_BITS(txq_id), |
1025 | (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)| | |
1026 | (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN)); | |
1027 | } | |
1028 | ||
f02831be EG |
1029 | void iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, int fifo, |
1030 | int sta_id, int tid, int frame_limit, u16 ssn) | |
48d42c42 | 1031 | { |
9eae88fa | 1032 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); |
4beaf6c2 | 1033 | |
9eae88fa JB |
1034 | if (test_and_set_bit(txq_id, trans_pcie->queue_used)) |
1035 | WARN_ONCE(1, "queue %d already used - expect issues", txq_id); | |
48d42c42 | 1036 | |
48d42c42 | 1037 | /* Stop this Tx queue before configuring it */ |
f02831be | 1038 | iwl_pcie_txq_set_inactive(trans, txq_id); |
48d42c42 | 1039 | |
4beaf6c2 EG |
1040 | /* Set this queue as a chain-building queue unless it is CMD queue */ |
1041 | if (txq_id != trans_pcie->cmd_queue) | |
1042 | iwl_set_bits_prph(trans, SCD_QUEUECHAIN_SEL, BIT(txq_id)); | |
1043 | ||
1044 | /* If this queue is mapped to a certain station: it is an AGG queue */ | |
1045 | if (sta_id != IWL_INVALID_STATION) { | |
1046 | u16 ra_tid = BUILD_RAxTID(sta_id, tid); | |
48d42c42 | 1047 | |
4beaf6c2 | 1048 | /* Map receiver-address / traffic-ID to this queue */ |
f02831be | 1049 | iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id); |
48d42c42 | 1050 | |
4beaf6c2 EG |
1051 | /* enable aggregations for the queue */ |
1052 | iwl_set_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id)); | |
1ce8658c EG |
1053 | } else { |
1054 | /* | |
1055 | * disable aggregations for the queue, this will also make the | |
1056 | * ra_tid mapping configuration irrelevant since it is now a | |
1057 | * non-AGG queue. | |
1058 | */ | |
1059 | iwl_clear_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id)); | |
4beaf6c2 | 1060 | } |
48d42c42 EG |
1061 | |
1062 | /* Place first TFD at index corresponding to start sequence number. | |
1063 | * Assumes that ssn_idx is valid (!= 0xFFF) */ | |
822e8b2a EG |
1064 | trans_pcie->txq[txq_id].q.read_ptr = (ssn & 0xff); |
1065 | trans_pcie->txq[txq_id].q.write_ptr = (ssn & 0xff); | |
1ce8658c EG |
1066 | |
1067 | iwl_write_direct32(trans, HBUS_TARG_WRPTR, | |
1068 | (ssn & 0xff) | (txq_id << 8)); | |
1069 | iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn); | |
48d42c42 EG |
1070 | |
1071 | /* Set up Tx window size and frame limit for this queue */ | |
4beaf6c2 EG |
1072 | iwl_write_targ_mem(trans, trans_pcie->scd_base_addr + |
1073 | SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0); | |
1042db2a | 1074 | iwl_write_targ_mem(trans, trans_pcie->scd_base_addr + |
9eae88fa JB |
1075 | SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32), |
1076 | ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) & | |
1077 | SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) | | |
1078 | ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) & | |
1079 | SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK)); | |
48d42c42 | 1080 | |
48d42c42 | 1081 | /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */ |
1ce8658c EG |
1082 | iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id), |
1083 | (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) | | |
1084 | (fifo << SCD_QUEUE_STTS_REG_POS_TXF) | | |
1085 | (1 << SCD_QUEUE_STTS_REG_POS_WSL) | | |
1086 | SCD_QUEUE_STTS_REG_MSK); | |
1087 | IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d on FIFO %d WrPtr: %d\n", | |
1088 | txq_id, fifo, ssn & 0xff); | |
4beaf6c2 EG |
1089 | } |
1090 | ||
f02831be | 1091 | void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id) |
288712a6 | 1092 | { |
8ad71bef | 1093 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); |
986ea6c9 EG |
1094 | u32 stts_addr = trans_pcie->scd_base_addr + |
1095 | SCD_TX_STTS_QUEUE_OFFSET(txq_id); | |
1096 | static const u32 zero_val[4] = {}; | |
288712a6 | 1097 | |
9eae88fa JB |
1098 | if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) { |
1099 | WARN_ONCE(1, "queue %d not used", txq_id); | |
1100 | return; | |
48d42c42 EG |
1101 | } |
1102 | ||
f02831be | 1103 | iwl_pcie_txq_set_inactive(trans, txq_id); |
ac928f8d | 1104 | |
986ea6c9 EG |
1105 | _iwl_write_targ_mem_dwords(trans, stts_addr, |
1106 | zero_val, ARRAY_SIZE(zero_val)); | |
1107 | ||
990aa6d7 | 1108 | iwl_pcie_txq_unmap(trans, txq_id); |
6c3fd3f0 | 1109 | |
1ce8658c | 1110 | IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id); |
48d42c42 EG |
1111 | } |
1112 | ||
fd4abac5 TW |
1113 | /*************** HOST COMMAND QUEUE FUNCTIONS *****/ |
1114 | ||
990aa6d7 | 1115 | /* |
f02831be | 1116 | * iwl_pcie_enqueue_hcmd - enqueue a uCode command |
fd4abac5 TW |
1117 | * @priv: device private data point |
1118 | * @cmd: a point to the ucode command structure | |
1119 | * | |
1120 | * The function returns < 0 values to indicate the operation is | |
1121 | * failed. On success, it turns the index (> 0) of command in the | |
1122 | * command queue. | |
1123 | */ | |
f02831be EG |
1124 | static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans, |
1125 | struct iwl_host_cmd *cmd) | |
fd4abac5 | 1126 | { |
8ad71bef | 1127 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); |
990aa6d7 | 1128 | struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue]; |
fd4abac5 | 1129 | struct iwl_queue *q = &txq->q; |
c2acea8e JB |
1130 | struct iwl_device_cmd *out_cmd; |
1131 | struct iwl_cmd_meta *out_meta; | |
f4feb8ac | 1132 | void *dup_buf = NULL; |
fd4abac5 | 1133 | dma_addr_t phys_addr; |
f4feb8ac | 1134 | int idx; |
4ce7cc2b | 1135 | u16 copy_size, cmd_size; |
4ce7cc2b JB |
1136 | bool had_nocopy = false; |
1137 | int i; | |
96791422 | 1138 | u32 cmd_pos; |
fd4abac5 | 1139 | |
4ce7cc2b JB |
1140 | copy_size = sizeof(out_cmd->hdr); |
1141 | cmd_size = sizeof(out_cmd->hdr); | |
1142 | ||
1143 | /* need one for the header if the first is NOCOPY */ | |
1144 | BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1); | |
1145 | ||
1146 | for (i = 0; i < IWL_MAX_CMD_TFDS; i++) { | |
1147 | if (!cmd->len[i]) | |
1148 | continue; | |
1149 | if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) { | |
1150 | had_nocopy = true; | |
f4feb8ac JB |
1151 | if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) { |
1152 | idx = -EINVAL; | |
1153 | goto free_dup_buf; | |
1154 | } | |
1155 | } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) { | |
1156 | /* | |
1157 | * This is also a chunk that isn't copied | |
1158 | * to the static buffer so set had_nocopy. | |
1159 | */ | |
1160 | had_nocopy = true; | |
1161 | ||
1162 | /* only allowed once */ | |
1163 | if (WARN_ON(dup_buf)) { | |
1164 | idx = -EINVAL; | |
1165 | goto free_dup_buf; | |
1166 | } | |
1167 | ||
1168 | dup_buf = kmemdup(cmd->data[i], cmd->len[i], | |
1169 | GFP_ATOMIC); | |
1170 | if (!dup_buf) | |
1171 | return -ENOMEM; | |
4ce7cc2b JB |
1172 | } else { |
1173 | /* NOCOPY must not be followed by normal! */ | |
f4feb8ac JB |
1174 | if (WARN_ON(had_nocopy)) { |
1175 | idx = -EINVAL; | |
1176 | goto free_dup_buf; | |
1177 | } | |
4ce7cc2b JB |
1178 | copy_size += cmd->len[i]; |
1179 | } | |
1180 | cmd_size += cmd->len[i]; | |
1181 | } | |
fd4abac5 | 1182 | |
3e41ace5 JB |
1183 | /* |
1184 | * If any of the command structures end up being larger than | |
4ce7cc2b JB |
1185 | * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically |
1186 | * allocated into separate TFDs, then we will need to | |
1187 | * increase the size of the buffers. | |
3e41ace5 | 1188 | */ |
2a79e45e JB |
1189 | if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE, |
1190 | "Command %s (%#x) is too large (%d bytes)\n", | |
990aa6d7 | 1191 | get_cmd_string(trans_pcie, cmd->id), cmd->id, copy_size)) { |
f4feb8ac JB |
1192 | idx = -EINVAL; |
1193 | goto free_dup_buf; | |
1194 | } | |
fd4abac5 | 1195 | |
015c15e1 | 1196 | spin_lock_bh(&txq->lock); |
3598e177 | 1197 | |
c2acea8e | 1198 | if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) { |
015c15e1 | 1199 | spin_unlock_bh(&txq->lock); |
3598e177 | 1200 | |
6d8f6eeb | 1201 | IWL_ERR(trans, "No space in command queue\n"); |
0e781842 | 1202 | iwl_op_mode_cmd_queue_full(trans->op_mode); |
f4feb8ac JB |
1203 | idx = -ENOSPC; |
1204 | goto free_dup_buf; | |
fd4abac5 TW |
1205 | } |
1206 | ||
4ce7cc2b | 1207 | idx = get_cmd_index(q, q->write_ptr); |
bf8440e6 JB |
1208 | out_cmd = txq->entries[idx].cmd; |
1209 | out_meta = &txq->entries[idx].meta; | |
c2acea8e | 1210 | |
8ce73f3a | 1211 | memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */ |
c2acea8e JB |
1212 | if (cmd->flags & CMD_WANT_SKB) |
1213 | out_meta->source = cmd; | |
fd4abac5 | 1214 | |
4ce7cc2b | 1215 | /* set up the header */ |
fd4abac5 | 1216 | |
4ce7cc2b | 1217 | out_cmd->hdr.cmd = cmd->id; |
fd4abac5 | 1218 | out_cmd->hdr.flags = 0; |
cefeaa5f | 1219 | out_cmd->hdr.sequence = |
c6f600fc | 1220 | cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) | |
cefeaa5f | 1221 | INDEX_TO_SEQ(q->write_ptr)); |
4ce7cc2b JB |
1222 | |
1223 | /* and copy the data that needs to be copied */ | |
96791422 | 1224 | cmd_pos = offsetof(struct iwl_device_cmd, payload); |
4ce7cc2b JB |
1225 | for (i = 0; i < IWL_MAX_CMD_TFDS; i++) { |
1226 | if (!cmd->len[i]) | |
1227 | continue; | |
f4feb8ac JB |
1228 | if (cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY | |
1229 | IWL_HCMD_DFL_DUP)) | |
4ce7cc2b | 1230 | break; |
96791422 EG |
1231 | memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], cmd->len[i]); |
1232 | cmd_pos += cmd->len[i]; | |
1233 | } | |
1234 | ||
1235 | WARN_ON_ONCE(txq->entries[idx].copy_cmd); | |
1236 | ||
1237 | /* | |
1238 | * since out_cmd will be the source address of the FH, it will write | |
1239 | * the retry count there. So when the user needs to receivce the HCMD | |
1240 | * that corresponds to the response in the response handler, it needs | |
1241 | * to set CMD_WANT_HCMD. | |
1242 | */ | |
1243 | if (cmd->flags & CMD_WANT_HCMD) { | |
1244 | txq->entries[idx].copy_cmd = | |
1245 | kmemdup(out_cmd, cmd_pos, GFP_ATOMIC); | |
1246 | if (unlikely(!txq->entries[idx].copy_cmd)) { | |
1247 | idx = -ENOMEM; | |
1248 | goto out; | |
1249 | } | |
ded2ae7c | 1250 | } |
4ce7cc2b | 1251 | |
d9fb6465 | 1252 | IWL_DEBUG_HC(trans, |
20d3b647 | 1253 | "Sending command %s (#%x), seq: 0x%04X, %d bytes at %d[%d]:%d\n", |
990aa6d7 | 1254 | get_cmd_string(trans_pcie, out_cmd->hdr.cmd), |
20d3b647 JB |
1255 | out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence), |
1256 | cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue); | |
4ce7cc2b | 1257 | |
1042db2a | 1258 | phys_addr = dma_map_single(trans->dev, &out_cmd->hdr, copy_size, |
20d3b647 | 1259 | DMA_BIDIRECTIONAL); |
1042db2a | 1260 | if (unlikely(dma_mapping_error(trans->dev, phys_addr))) { |
2c46f72e JB |
1261 | idx = -ENOMEM; |
1262 | goto out; | |
1263 | } | |
1264 | ||
2e724443 | 1265 | dma_unmap_addr_set(out_meta, mapping, phys_addr); |
4ce7cc2b JB |
1266 | dma_unmap_len_set(out_meta, len, copy_size); |
1267 | ||
f02831be | 1268 | iwl_pcie_txq_build_tfd(trans, txq, phys_addr, copy_size, 1); |
4ce7cc2b JB |
1269 | |
1270 | for (i = 0; i < IWL_MAX_CMD_TFDS; i++) { | |
f4feb8ac JB |
1271 | const void *data = cmd->data[i]; |
1272 | ||
4ce7cc2b JB |
1273 | if (!cmd->len[i]) |
1274 | continue; | |
f4feb8ac JB |
1275 | if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY | |
1276 | IWL_HCMD_DFL_DUP))) | |
4ce7cc2b | 1277 | continue; |
f4feb8ac JB |
1278 | if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) |
1279 | data = dup_buf; | |
1280 | phys_addr = dma_map_single(trans->dev, (void *)data, | |
3be3fdb5 | 1281 | cmd->len[i], DMA_BIDIRECTIONAL); |
1042db2a | 1282 | if (dma_mapping_error(trans->dev, phys_addr)) { |
f02831be EG |
1283 | iwl_pcie_tfd_unmap(trans, out_meta, |
1284 | &txq->tfds[q->write_ptr], | |
1285 | DMA_BIDIRECTIONAL); | |
4ce7cc2b JB |
1286 | idx = -ENOMEM; |
1287 | goto out; | |
1288 | } | |
1289 | ||
f02831be | 1290 | iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmd->len[i], 0); |
4ce7cc2b | 1291 | } |
df833b1d | 1292 | |
afaf6b57 | 1293 | out_meta->flags = cmd->flags; |
f4feb8ac JB |
1294 | if (WARN_ON_ONCE(txq->entries[idx].free_buf)) |
1295 | kfree(txq->entries[idx].free_buf); | |
1296 | txq->entries[idx].free_buf = dup_buf; | |
2c46f72e JB |
1297 | |
1298 | txq->need_update = 1; | |
1299 | ||
45eab7cc JB |
1300 | trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, |
1301 | &out_cmd->hdr, copy_size); | |
df833b1d | 1302 | |
7c5ba4a8 JB |
1303 | /* start timer if queue currently empty */ |
1304 | if (q->read_ptr == q->write_ptr && trans_pcie->wd_timeout) | |
1305 | mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout); | |
1306 | ||
fd4abac5 TW |
1307 | /* Increment and update queue's write index */ |
1308 | q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd); | |
990aa6d7 | 1309 | iwl_pcie_txq_inc_wr_ptr(trans, txq); |
fd4abac5 | 1310 | |
2c46f72e | 1311 | out: |
015c15e1 | 1312 | spin_unlock_bh(&txq->lock); |
f4feb8ac JB |
1313 | free_dup_buf: |
1314 | if (idx < 0) | |
1315 | kfree(dup_buf); | |
7bfedc59 | 1316 | return idx; |
fd4abac5 TW |
1317 | } |
1318 | ||
990aa6d7 EG |
1319 | /* |
1320 | * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them | |
17b88929 | 1321 | * @rxb: Rx buffer to reclaim |
247c61d6 EG |
1322 | * @handler_status: return value of the handler of the command |
1323 | * (put in setup_rx_handlers) | |
17b88929 TW |
1324 | * |
1325 | * If an Rx buffer has an async callback associated with it the callback | |
1326 | * will be executed. The attached skb (if present) will only be freed | |
1327 | * if the callback returns 1 | |
1328 | */ | |
990aa6d7 EG |
1329 | void iwl_pcie_hcmd_complete(struct iwl_trans *trans, |
1330 | struct iwl_rx_cmd_buffer *rxb, int handler_status) | |
17b88929 | 1331 | { |
2f301227 | 1332 | struct iwl_rx_packet *pkt = rxb_addr(rxb); |
17b88929 TW |
1333 | u16 sequence = le16_to_cpu(pkt->hdr.sequence); |
1334 | int txq_id = SEQ_TO_QUEUE(sequence); | |
1335 | int index = SEQ_TO_INDEX(sequence); | |
17b88929 | 1336 | int cmd_index; |
c2acea8e JB |
1337 | struct iwl_device_cmd *cmd; |
1338 | struct iwl_cmd_meta *meta; | |
8ad71bef | 1339 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); |
990aa6d7 | 1340 | struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue]; |
17b88929 TW |
1341 | |
1342 | /* If a Tx command is being handled and it isn't in the actual | |
1343 | * command queue then there a command routing bug has been introduced | |
1344 | * in the queue management code. */ | |
c6f600fc | 1345 | if (WARN(txq_id != trans_pcie->cmd_queue, |
13bb9483 | 1346 | "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n", |
20d3b647 JB |
1347 | txq_id, trans_pcie->cmd_queue, sequence, |
1348 | trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr, | |
1349 | trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) { | |
3e10caeb | 1350 | iwl_print_hex_error(trans, pkt, 32); |
55d6a3cd | 1351 | return; |
01ef9323 | 1352 | } |
17b88929 | 1353 | |
015c15e1 JB |
1354 | spin_lock(&txq->lock); |
1355 | ||
4ce7cc2b | 1356 | cmd_index = get_cmd_index(&txq->q, index); |
bf8440e6 JB |
1357 | cmd = txq->entries[cmd_index].cmd; |
1358 | meta = &txq->entries[cmd_index].meta; | |
17b88929 | 1359 | |
f02831be | 1360 | iwl_pcie_tfd_unmap(trans, meta, &txq->tfds[index], DMA_BIDIRECTIONAL); |
c33de625 | 1361 | |
17b88929 | 1362 | /* Input error checking is done when commands are added to queue. */ |
c2acea8e | 1363 | if (meta->flags & CMD_WANT_SKB) { |
48a2d66f | 1364 | struct page *p = rxb_steal_page(rxb); |
65b94a4a | 1365 | |
65b94a4a JB |
1366 | meta->source->resp_pkt = pkt; |
1367 | meta->source->_rx_page_addr = (unsigned long)page_address(p); | |
b2cf410c | 1368 | meta->source->_rx_page_order = trans_pcie->rx_page_order; |
247c61d6 | 1369 | meta->source->handler_status = handler_status; |
247c61d6 | 1370 | } |
2624e96c | 1371 | |
f02831be | 1372 | iwl_pcie_cmdq_reclaim(trans, txq_id, index); |
17b88929 | 1373 | |
c2acea8e | 1374 | if (!(meta->flags & CMD_ASYNC)) { |
74fda971 | 1375 | if (!test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) { |
05c89b91 WYG |
1376 | IWL_WARN(trans, |
1377 | "HCMD_ACTIVE already clear for command %s\n", | |
990aa6d7 | 1378 | get_cmd_string(trans_pcie, cmd->hdr.cmd)); |
05c89b91 | 1379 | } |
74fda971 | 1380 | clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status); |
6d8f6eeb | 1381 | IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n", |
990aa6d7 | 1382 | get_cmd_string(trans_pcie, cmd->hdr.cmd)); |
f946b529 | 1383 | wake_up(&trans_pcie->wait_command_queue); |
17b88929 | 1384 | } |
3598e177 | 1385 | |
dd487449 | 1386 | meta->flags = 0; |
3598e177 | 1387 | |
015c15e1 | 1388 | spin_unlock(&txq->lock); |
17b88929 | 1389 | } |
253a634c | 1390 | |
253a634c EG |
1391 | #define HOST_COMPLETE_TIMEOUT (2 * HZ) |
1392 | ||
f02831be EG |
1393 | static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans, |
1394 | struct iwl_host_cmd *cmd) | |
253a634c | 1395 | { |
d9fb6465 | 1396 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); |
253a634c EG |
1397 | int ret; |
1398 | ||
1399 | /* An asynchronous command can not expect an SKB to be set. */ | |
1400 | if (WARN_ON(cmd->flags & CMD_WANT_SKB)) | |
1401 | return -EINVAL; | |
1402 | ||
253a634c | 1403 | |
f02831be | 1404 | ret = iwl_pcie_enqueue_hcmd(trans, cmd); |
253a634c | 1405 | if (ret < 0) { |
721c32f7 | 1406 | IWL_ERR(trans, |
b36b110c | 1407 | "Error sending %s: enqueue_hcmd failed: %d\n", |
990aa6d7 | 1408 | get_cmd_string(trans_pcie, cmd->id), ret); |
253a634c EG |
1409 | return ret; |
1410 | } | |
1411 | return 0; | |
1412 | } | |
1413 | ||
f02831be EG |
1414 | static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans, |
1415 | struct iwl_host_cmd *cmd) | |
253a634c | 1416 | { |
8ad71bef | 1417 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); |
253a634c EG |
1418 | int cmd_idx; |
1419 | int ret; | |
1420 | ||
6d8f6eeb | 1421 | IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n", |
990aa6d7 | 1422 | get_cmd_string(trans_pcie, cmd->id)); |
253a634c | 1423 | |
2cc39c94 | 1424 | if (WARN_ON(test_and_set_bit(STATUS_HCMD_ACTIVE, |
74fda971 | 1425 | &trans_pcie->status))) { |
2cc39c94 | 1426 | IWL_ERR(trans, "Command %s: a command is already active!\n", |
990aa6d7 | 1427 | get_cmd_string(trans_pcie, cmd->id)); |
2cc39c94 JB |
1428 | return -EIO; |
1429 | } | |
1430 | ||
6d8f6eeb | 1431 | IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n", |
990aa6d7 | 1432 | get_cmd_string(trans_pcie, cmd->id)); |
253a634c | 1433 | |
f02831be | 1434 | cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd); |
253a634c EG |
1435 | if (cmd_idx < 0) { |
1436 | ret = cmd_idx; | |
74fda971 | 1437 | clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status); |
721c32f7 | 1438 | IWL_ERR(trans, |
b36b110c | 1439 | "Error sending %s: enqueue_hcmd failed: %d\n", |
990aa6d7 | 1440 | get_cmd_string(trans_pcie, cmd->id), ret); |
253a634c EG |
1441 | return ret; |
1442 | } | |
1443 | ||
f946b529 | 1444 | ret = wait_event_timeout(trans_pcie->wait_command_queue, |
20d3b647 JB |
1445 | !test_bit(STATUS_HCMD_ACTIVE, |
1446 | &trans_pcie->status), | |
1447 | HOST_COMPLETE_TIMEOUT); | |
253a634c | 1448 | if (!ret) { |
74fda971 | 1449 | if (test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) { |
990aa6d7 | 1450 | struct iwl_txq *txq = |
c6f600fc | 1451 | &trans_pcie->txq[trans_pcie->cmd_queue]; |
d10630af WYG |
1452 | struct iwl_queue *q = &txq->q; |
1453 | ||
721c32f7 | 1454 | IWL_ERR(trans, |
253a634c | 1455 | "Error sending %s: time out after %dms.\n", |
990aa6d7 | 1456 | get_cmd_string(trans_pcie, cmd->id), |
253a634c EG |
1457 | jiffies_to_msecs(HOST_COMPLETE_TIMEOUT)); |
1458 | ||
721c32f7 | 1459 | IWL_ERR(trans, |
d10630af WYG |
1460 | "Current CMD queue read_ptr %d write_ptr %d\n", |
1461 | q->read_ptr, q->write_ptr); | |
1462 | ||
74fda971 | 1463 | clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status); |
d9fb6465 JB |
1464 | IWL_DEBUG_INFO(trans, |
1465 | "Clearing HCMD_ACTIVE for command %s\n", | |
990aa6d7 | 1466 | get_cmd_string(trans_pcie, cmd->id)); |
253a634c EG |
1467 | ret = -ETIMEDOUT; |
1468 | goto cancel; | |
1469 | } | |
1470 | } | |
1471 | ||
d18aa87f JB |
1472 | if (test_bit(STATUS_FW_ERROR, &trans_pcie->status)) { |
1473 | IWL_ERR(trans, "FW error in SYNC CMD %s\n", | |
990aa6d7 | 1474 | get_cmd_string(trans_pcie, cmd->id)); |
d18aa87f JB |
1475 | ret = -EIO; |
1476 | goto cancel; | |
1477 | } | |
1478 | ||
f946b529 EG |
1479 | if (test_bit(STATUS_RFKILL, &trans_pcie->status)) { |
1480 | IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n"); | |
1481 | ret = -ERFKILL; | |
1482 | goto cancel; | |
1483 | } | |
1484 | ||
65b94a4a | 1485 | if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) { |
6d8f6eeb | 1486 | IWL_ERR(trans, "Error: Response NULL in '%s'\n", |
990aa6d7 | 1487 | get_cmd_string(trans_pcie, cmd->id)); |
253a634c EG |
1488 | ret = -EIO; |
1489 | goto cancel; | |
1490 | } | |
1491 | ||
1492 | return 0; | |
1493 | ||
1494 | cancel: | |
1495 | if (cmd->flags & CMD_WANT_SKB) { | |
1496 | /* | |
1497 | * Cancel the CMD_WANT_SKB flag for the cmd in the | |
1498 | * TX cmd queue. Otherwise in case the cmd comes | |
1499 | * in later, it will possibly set an invalid | |
1500 | * address (cmd->meta.source). | |
1501 | */ | |
bf8440e6 JB |
1502 | trans_pcie->txq[trans_pcie->cmd_queue]. |
1503 | entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB; | |
253a634c | 1504 | } |
9cac4943 | 1505 | |
65b94a4a JB |
1506 | if (cmd->resp_pkt) { |
1507 | iwl_free_resp(cmd); | |
1508 | cmd->resp_pkt = NULL; | |
253a634c EG |
1509 | } |
1510 | ||
1511 | return ret; | |
1512 | } | |
1513 | ||
f02831be | 1514 | int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd) |
253a634c | 1515 | { |
f946b529 EG |
1516 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); |
1517 | ||
d18aa87f JB |
1518 | if (test_bit(STATUS_FW_ERROR, &trans_pcie->status)) |
1519 | return -EIO; | |
1520 | ||
f946b529 EG |
1521 | if (test_bit(STATUS_RFKILL, &trans_pcie->status)) |
1522 | return -ERFKILL; | |
1523 | ||
253a634c | 1524 | if (cmd->flags & CMD_ASYNC) |
f02831be | 1525 | return iwl_pcie_send_hcmd_async(trans, cmd); |
253a634c | 1526 | |
f946b529 | 1527 | /* We still can fail on RFKILL that can be asserted while we wait */ |
f02831be | 1528 | return iwl_pcie_send_hcmd_sync(trans, cmd); |
253a634c EG |
1529 | } |
1530 | ||
f02831be EG |
1531 | int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb, |
1532 | struct iwl_device_cmd *dev_cmd, int txq_id) | |
a0eaad71 | 1533 | { |
8ad71bef | 1534 | struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans); |
f02831be EG |
1535 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; |
1536 | struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload; | |
1537 | struct iwl_cmd_meta *out_meta; | |
1538 | struct iwl_txq *txq; | |
1539 | struct iwl_queue *q; | |
1540 | dma_addr_t phys_addr = 0; | |
1541 | dma_addr_t txcmd_phys; | |
1542 | dma_addr_t scratch_phys; | |
1543 | u16 len, firstlen, secondlen; | |
1544 | u8 wait_write_ptr = 0; | |
1545 | __le16 fc = hdr->frame_control; | |
1546 | u8 hdr_len = ieee80211_hdrlen(fc); | |
1547 | u16 __maybe_unused wifi_seq; | |
1548 | ||
1549 | txq = &trans_pcie->txq[txq_id]; | |
1550 | q = &txq->q; | |
a0eaad71 | 1551 | |
f02831be EG |
1552 | if (unlikely(!test_bit(txq_id, trans_pcie->queue_used))) { |
1553 | WARN_ON_ONCE(1); | |
1554 | return -EINVAL; | |
1555 | } | |
39644e9a | 1556 | |
f02831be | 1557 | spin_lock(&txq->lock); |
015c15e1 | 1558 | |
f02831be EG |
1559 | /* In AGG mode, the index in the ring must correspond to the WiFi |
1560 | * sequence number. This is a HW requirements to help the SCD to parse | |
1561 | * the BA. | |
1562 | * Check here that the packets are in the right place on the ring. | |
1563 | */ | |
1564 | #ifdef CONFIG_IWLWIFI_DEBUG | |
1565 | wifi_seq = SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl)); | |
1566 | WARN_ONCE((iwl_read_prph(trans, SCD_AGGR_SEL) & BIT(txq_id)) && | |
1567 | ((wifi_seq & 0xff) != q->write_ptr), | |
1568 | "Q: %d WiFi Seq %d tfdNum %d", | |
1569 | txq_id, wifi_seq, q->write_ptr); | |
1570 | #endif | |
1571 | ||
1572 | /* Set up driver data for this TFD */ | |
1573 | txq->entries[q->write_ptr].skb = skb; | |
1574 | txq->entries[q->write_ptr].cmd = dev_cmd; | |
1575 | ||
1576 | dev_cmd->hdr.cmd = REPLY_TX; | |
1577 | dev_cmd->hdr.sequence = | |
1578 | cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) | | |
1579 | INDEX_TO_SEQ(q->write_ptr))); | |
1580 | ||
1581 | /* Set up first empty entry in queue's array of Tx/cmd buffers */ | |
1582 | out_meta = &txq->entries[q->write_ptr].meta; | |
a0eaad71 | 1583 | |
f02831be EG |
1584 | /* |
1585 | * Use the first empty entry in this queue's command buffer array | |
1586 | * to contain the Tx command and MAC header concatenated together | |
1587 | * (payload data will be in another buffer). | |
1588 | * Size of this varies, due to varying MAC header length. | |
1589 | * If end is not dword aligned, we'll have 2 extra bytes at the end | |
1590 | * of the MAC header (device reads on dword boundaries). | |
1591 | * We'll tell device about this padding later. | |
1592 | */ | |
1593 | len = sizeof(struct iwl_tx_cmd) + | |
1594 | sizeof(struct iwl_cmd_header) + hdr_len; | |
1595 | firstlen = (len + 3) & ~3; | |
1596 | ||
1597 | /* Tell NIC about any 2-byte padding after MAC header */ | |
1598 | if (firstlen != len) | |
1599 | tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK; | |
1600 | ||
1601 | /* Physical address of this Tx command's header (not MAC header!), | |
1602 | * within command buffer array. */ | |
1603 | txcmd_phys = dma_map_single(trans->dev, | |
1604 | &dev_cmd->hdr, firstlen, | |
1605 | DMA_BIDIRECTIONAL); | |
1606 | if (unlikely(dma_mapping_error(trans->dev, txcmd_phys))) | |
1607 | goto out_err; | |
1608 | dma_unmap_addr_set(out_meta, mapping, txcmd_phys); | |
1609 | dma_unmap_len_set(out_meta, len, firstlen); | |
1610 | ||
1611 | if (!ieee80211_has_morefrags(fc)) { | |
1612 | txq->need_update = 1; | |
1613 | } else { | |
1614 | wait_write_ptr = 1; | |
1615 | txq->need_update = 0; | |
a0eaad71 EG |
1616 | } |
1617 | ||
f02831be EG |
1618 | /* Set up TFD's 2nd entry to point directly to remainder of skb, |
1619 | * if any (802.11 null frames have no payload). */ | |
1620 | secondlen = skb->len - hdr_len; | |
1621 | if (secondlen > 0) { | |
1622 | phys_addr = dma_map_single(trans->dev, skb->data + hdr_len, | |
1623 | secondlen, DMA_TO_DEVICE); | |
1624 | if (unlikely(dma_mapping_error(trans->dev, phys_addr))) { | |
1625 | dma_unmap_single(trans->dev, | |
1626 | dma_unmap_addr(out_meta, mapping), | |
1627 | dma_unmap_len(out_meta, len), | |
1628 | DMA_BIDIRECTIONAL); | |
1629 | goto out_err; | |
1630 | } | |
1631 | } | |
a0eaad71 | 1632 | |
f02831be EG |
1633 | /* Attach buffers to TFD */ |
1634 | iwl_pcie_txq_build_tfd(trans, txq, txcmd_phys, firstlen, 1); | |
1635 | if (secondlen > 0) | |
1636 | iwl_pcie_txq_build_tfd(trans, txq, phys_addr, secondlen, 0); | |
a0eaad71 | 1637 | |
f02831be EG |
1638 | scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) + |
1639 | offsetof(struct iwl_tx_cmd, scratch); | |
a0eaad71 | 1640 | |
f02831be EG |
1641 | /* take back ownership of DMA buffer to enable update */ |
1642 | dma_sync_single_for_cpu(trans->dev, txcmd_phys, firstlen, | |
1643 | DMA_BIDIRECTIONAL); | |
1644 | tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys); | |
1645 | tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys); | |
a0eaad71 | 1646 | |
f02831be EG |
1647 | IWL_DEBUG_TX(trans, "sequence nr = 0X%x\n", |
1648 | le16_to_cpu(dev_cmd->hdr.sequence)); | |
1649 | IWL_DEBUG_TX(trans, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags)); | |
a0eaad71 | 1650 | |
f02831be EG |
1651 | /* Set up entry for this TFD in Tx byte-count array */ |
1652 | iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len)); | |
a0eaad71 | 1653 | |
f02831be EG |
1654 | dma_sync_single_for_device(trans->dev, txcmd_phys, firstlen, |
1655 | DMA_BIDIRECTIONAL); | |
7c5ba4a8 | 1656 | |
f02831be EG |
1657 | trace_iwlwifi_dev_tx(trans->dev, skb, |
1658 | &txq->tfds[txq->q.write_ptr], | |
1659 | sizeof(struct iwl_tfd), | |
1660 | &dev_cmd->hdr, firstlen, | |
1661 | skb->data + hdr_len, secondlen); | |
1662 | trace_iwlwifi_dev_tx_data(trans->dev, skb, | |
1663 | skb->data + hdr_len, secondlen); | |
7c5ba4a8 | 1664 | |
f02831be EG |
1665 | /* start timer if queue currently empty */ |
1666 | if (txq->need_update && q->read_ptr == q->write_ptr && | |
1667 | trans_pcie->wd_timeout) | |
1668 | mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout); | |
1669 | ||
1670 | /* Tell device the write index *just past* this latest filled TFD */ | |
1671 | q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd); | |
1672 | iwl_pcie_txq_inc_wr_ptr(trans, txq); | |
1673 | ||
1674 | /* | |
1675 | * At this point the frame is "transmitted" successfully | |
1676 | * and we will get a TX status notification eventually, | |
1677 | * regardless of the value of ret. "ret" only indicates | |
1678 | * whether or not we should update the write pointer. | |
1679 | */ | |
1680 | if (iwl_queue_space(q) < q->high_mark) { | |
1681 | if (wait_write_ptr) { | |
1682 | txq->need_update = 1; | |
1683 | iwl_pcie_txq_inc_wr_ptr(trans, txq); | |
1684 | } else { | |
1685 | iwl_stop_queue(trans, txq); | |
1686 | } | |
1687 | } | |
1688 | spin_unlock(&txq->lock); | |
1689 | return 0; | |
1690 | out_err: | |
1691 | spin_unlock(&txq->lock); | |
1692 | return -1; | |
a0eaad71 | 1693 | } |