xref: /linux/drivers/net/wireless/intel/iwlwifi/pcie/gen1_2/tx.c (revision 189f164e573e18d9f8876dbd3ad8fcbe11f93037)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2003-2014, 2018-2021, 2023-2025 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/etherdevice.h>
8 #include <linux/ieee80211.h>
9 #include <linux/dmapool.h>
10 #include <linux/slab.h>
11 #include <linux/sched.h>
12 #include <linux/tcp.h>
13 #include <net/ip6_checksum.h>
14 #include <net/tso.h>
15 
16 #include "fw/api/commands.h"
17 #include "fw/api/datapath.h"
18 #include "fw/api/debug.h"
19 #include "iwl-fh.h"
20 #include "iwl-debug.h"
21 #include "iwl-csr.h"
22 #include "iwl-prph.h"
23 #include "iwl-io.h"
24 #include "iwl-scd.h"
25 #include "iwl-op-mode.h"
26 #include "internal.h"
27 #include "fw/api/tx.h"
28 #include "fw/dbg.h"
29 #include "pcie/utils.h"
30 
31 /*************** DMA-QUEUE-GENERAL-FUNCTIONS  *****
32  * DMA services
33  *
34  * Theory of operation
35  *
36  * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
37  * of buffer descriptors, each of which points to one or more data buffers for
38  * the device to read from or fill.  Driver and device exchange status of each
39  * queue via "read" and "write" pointers.  Driver keeps minimum of 2 empty
40  * entries in each circular buffer, to protect against confusing empty and full
41  * queue states.
42  *
43  * The device reads or writes the data in the queues via the device's several
44  * DMA/FIFO channels.  Each queue is mapped to a single DMA channel.
45  *
46  * For Tx queue, there are low mark and high mark limits. If, after queuing
47  * the packet for Tx, free space become < low mark, Tx queue stopped. When
48  * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
49  * Tx queue resumed.
50  *
51  ***************************************************/
52 
53 
iwl_pcie_alloc_dma_ptr(struct iwl_trans * trans,struct iwl_dma_ptr * ptr,size_t size)54 int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
55 			   struct iwl_dma_ptr *ptr, size_t size)
56 {
57 	if (WARN_ON(ptr->addr))
58 		return -EINVAL;
59 
60 	ptr->addr = dma_alloc_coherent(trans->dev, size,
61 				       &ptr->dma, GFP_KERNEL);
62 	if (!ptr->addr)
63 		return -ENOMEM;
64 	ptr->size = size;
65 	return 0;
66 }
67 
iwl_pcie_free_dma_ptr(struct iwl_trans * trans,struct iwl_dma_ptr * ptr)68 void iwl_pcie_free_dma_ptr(struct iwl_trans *trans, struct iwl_dma_ptr *ptr)
69 {
70 	if (unlikely(!ptr->addr))
71 		return;
72 
73 	dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
74 	memset(ptr, 0, sizeof(*ptr));
75 }
76 
77 /*
78  * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
79  */
iwl_pcie_txq_inc_wr_ptr(struct iwl_trans * trans,struct iwl_txq * txq)80 static void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans,
81 				    struct iwl_txq *txq)
82 {
83 	u32 reg = 0;
84 	int txq_id = txq->id;
85 
86 	lockdep_assert_held(&txq->lock);
87 
88 	/*
89 	 * explicitly wake up the NIC if:
90 	 * 1. shadow registers aren't enabled
91 	 * 2. NIC is woken up for CMD regardless of shadow outside this function
92 	 * 3. there is a chance that the NIC is asleep
93 	 */
94 	if (!trans->mac_cfg->base->shadow_reg_enable &&
95 	    txq_id != trans->conf.cmd_queue &&
96 	    test_bit(STATUS_TPOWER_PMI, &trans->status)) {
97 		/*
98 		 * wake up nic if it's powered down ...
99 		 * uCode will wake up, and interrupt us again, so next
100 		 * time we'll skip this part.
101 		 */
102 		reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
103 
104 		if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
105 			IWL_DEBUG_INFO(trans, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
106 				       txq_id, reg);
107 			iwl_set_bit(trans, CSR_GP_CNTRL,
108 				    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
109 			txq->need_update = true;
110 			return;
111 		}
112 	}
113 
114 	/*
115 	 * if not in power-save mode, uCode will never sleep when we're
116 	 * trying to tx (during RFKILL, we're not trying to tx).
117 	 */
118 	IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id, txq->write_ptr);
119 	if (!txq->block)
120 		iwl_write32(trans, HBUS_TARG_WRPTR,
121 			    txq->write_ptr | (txq_id << 8));
122 }
123 
iwl_pcie_txq_check_wrptrs(struct iwl_trans * trans)124 void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans)
125 {
126 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
127 	int i;
128 
129 	for (i = 0; i < trans->mac_cfg->base->num_of_queues; i++) {
130 		struct iwl_txq *txq = trans_pcie->txqs.txq[i];
131 
132 		if (!test_bit(i, trans_pcie->txqs.queue_used))
133 			continue;
134 
135 		spin_lock_bh(&txq->lock);
136 		if (txq->need_update) {
137 			iwl_pcie_txq_inc_wr_ptr(trans, txq);
138 			txq->need_update = false;
139 		}
140 		spin_unlock_bh(&txq->lock);
141 	}
142 }
143 
iwl_pcie_gen1_tfd_set_tb(struct iwl_tfd * tfd,u8 idx,dma_addr_t addr,u16 len)144 static inline void iwl_pcie_gen1_tfd_set_tb(struct iwl_tfd *tfd,
145 					    u8 idx, dma_addr_t addr, u16 len)
146 {
147 	struct iwl_tfd_tb *tb = &tfd->tbs[idx];
148 	u16 hi_n_len = len << 4;
149 
150 	put_unaligned_le32(addr, &tb->lo);
151 	hi_n_len |= iwl_get_dma_hi_addr(addr);
152 
153 	tb->hi_n_len = cpu_to_le16(hi_n_len);
154 
155 	tfd->num_tbs = idx + 1;
156 }
157 
iwl_txq_gen1_tfd_get_num_tbs(struct iwl_tfd * tfd)158 static inline u8 iwl_txq_gen1_tfd_get_num_tbs(struct iwl_tfd *tfd)
159 {
160 	return tfd->num_tbs & 0x1f;
161 }
162 
iwl_pcie_txq_build_tfd(struct iwl_trans * trans,struct iwl_txq * txq,dma_addr_t addr,u16 len,bool reset)163 static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
164 				  dma_addr_t addr, u16 len, bool reset)
165 {
166 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
167 	void *tfd;
168 	u32 num_tbs;
169 
170 	tfd = (u8 *)txq->tfds + trans_pcie->txqs.tfd.size * txq->write_ptr;
171 
172 	if (reset)
173 		memset(tfd, 0, trans_pcie->txqs.tfd.size);
174 
175 	num_tbs = iwl_txq_gen1_tfd_get_num_tbs(tfd);
176 
177 	/* Each TFD can point to a maximum max_tbs Tx buffers */
178 	if (num_tbs >= trans_pcie->txqs.tfd.max_tbs) {
179 		IWL_ERR(trans, "Error can not send more than %d chunks\n",
180 			trans_pcie->txqs.tfd.max_tbs);
181 		return -EINVAL;
182 	}
183 
184 	if (WARN(addr & ~IWL_TX_DMA_MASK,
185 		 "Unaligned address = %llx\n", (unsigned long long)addr))
186 		return -EINVAL;
187 
188 	iwl_pcie_gen1_tfd_set_tb(tfd, num_tbs, addr, len);
189 
190 	return num_tbs;
191 }
192 
iwl_pcie_clear_cmd_in_flight(struct iwl_trans * trans)193 static void iwl_pcie_clear_cmd_in_flight(struct iwl_trans *trans)
194 {
195 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
196 
197 	if (!trans->mac_cfg->base->apmg_wake_up_wa)
198 		return;
199 
200 	spin_lock(&trans_pcie->reg_lock);
201 
202 	if (WARN_ON(!trans_pcie->cmd_hold_nic_awake)) {
203 		spin_unlock(&trans_pcie->reg_lock);
204 		return;
205 	}
206 
207 	trans_pcie->cmd_hold_nic_awake = false;
208 	iwl_trans_clear_bit(trans, CSR_GP_CNTRL,
209 			    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
210 	spin_unlock(&trans_pcie->reg_lock);
211 }
212 
iwl_pcie_free_and_unmap_tso_page(struct iwl_trans * trans,struct page * page)213 static void iwl_pcie_free_and_unmap_tso_page(struct iwl_trans *trans,
214 					     struct page *page)
215 {
216 	struct iwl_tso_page_info *info = IWL_TSO_PAGE_INFO(page_address(page));
217 
218 	/* Decrease internal use count and unmap/free page if needed */
219 	if (refcount_dec_and_test(&info->use_count)) {
220 		dma_unmap_page(trans->dev, info->dma_addr, PAGE_SIZE,
221 			       DMA_TO_DEVICE);
222 
223 		__free_page(page);
224 	}
225 }
226 
iwl_pcie_free_tso_pages(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_cmd_meta * cmd_meta)227 void iwl_pcie_free_tso_pages(struct iwl_trans *trans, struct sk_buff *skb,
228 			     struct iwl_cmd_meta *cmd_meta)
229 {
230 	struct page **page_ptr;
231 	struct page *next;
232 
233 	page_ptr = (void *)((u8 *)skb->cb + trans->conf.cb_data_offs);
234 	next = *page_ptr;
235 	*page_ptr = NULL;
236 
237 	while (next) {
238 		struct iwl_tso_page_info *info;
239 		struct page *tmp = next;
240 
241 		info = IWL_TSO_PAGE_INFO(page_address(next));
242 		next = info->next;
243 
244 		/* Unmap the scatter gather list that is on the last page */
245 		if (!next && cmd_meta->sg_offset) {
246 			struct sg_table *sgt;
247 
248 			sgt = (void *)((u8 *)page_address(tmp) +
249 				       cmd_meta->sg_offset);
250 
251 			dma_unmap_sgtable(trans->dev, sgt, DMA_TO_DEVICE, 0);
252 		}
253 
254 		iwl_pcie_free_and_unmap_tso_page(trans, tmp);
255 	}
256 }
257 
258 static inline dma_addr_t
iwl_txq_gen1_tfd_tb_get_addr(struct iwl_tfd * tfd,u8 idx)259 iwl_txq_gen1_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
260 {
261 	struct iwl_tfd_tb *tb = &tfd->tbs[idx];
262 	dma_addr_t addr;
263 	dma_addr_t hi_len;
264 
265 	addr = get_unaligned_le32(&tb->lo);
266 
267 	if (sizeof(dma_addr_t) <= sizeof(u32))
268 		return addr;
269 
270 	hi_len = le16_to_cpu(tb->hi_n_len) & 0xF;
271 
272 	/*
273 	 * shift by 16 twice to avoid warnings on 32-bit
274 	 * (where this code never runs anyway due to the
275 	 * if statement above)
276 	 */
277 	return addr | ((hi_len << 16) << 16);
278 }
279 
iwl_txq_set_tfd_invalid_gen1(struct iwl_trans * trans,struct iwl_tfd * tfd)280 static void iwl_txq_set_tfd_invalid_gen1(struct iwl_trans *trans,
281 					 struct iwl_tfd *tfd)
282 {
283 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
284 
285 	tfd->num_tbs = 0;
286 
287 	iwl_pcie_gen1_tfd_set_tb(tfd, 0, trans_pcie->invalid_tx_cmd.dma,
288 				 trans_pcie->invalid_tx_cmd.size);
289 }
290 
iwl_txq_gen1_tfd_unmap(struct iwl_trans * trans,struct iwl_cmd_meta * meta,struct iwl_txq * txq,int index)291 static void iwl_txq_gen1_tfd_unmap(struct iwl_trans *trans,
292 				   struct iwl_cmd_meta *meta,
293 				   struct iwl_txq *txq, int index)
294 {
295 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
296 	int i, num_tbs;
297 	struct iwl_tfd *tfd = iwl_txq_get_tfd(trans, txq, index);
298 
299 	/* Sanity check on number of chunks */
300 	num_tbs = iwl_txq_gen1_tfd_get_num_tbs(tfd);
301 
302 	if (num_tbs > trans_pcie->txqs.tfd.max_tbs) {
303 		IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
304 		/* @todo issue fatal error, it is quite serious situation */
305 		return;
306 	}
307 
308 	/* TB1 is mapped directly, the rest is the TSO page and SG list. */
309 	if (meta->sg_offset)
310 		num_tbs = 2;
311 
312 	/* first TB is never freed - it's the bidirectional DMA data */
313 
314 	for (i = 1; i < num_tbs; i++) {
315 		if (meta->tbs & BIT(i))
316 			dma_unmap_page(trans->dev,
317 				       iwl_txq_gen1_tfd_tb_get_addr(tfd, i),
318 				       iwl_txq_gen1_tfd_tb_get_len(trans,
319 								   tfd, i),
320 				       DMA_TO_DEVICE);
321 		else
322 			dma_unmap_single(trans->dev,
323 					 iwl_txq_gen1_tfd_tb_get_addr(tfd, i),
324 					 iwl_txq_gen1_tfd_tb_get_len(trans,
325 								     tfd, i),
326 					 DMA_TO_DEVICE);
327 	}
328 
329 	meta->tbs = 0;
330 
331 	iwl_txq_set_tfd_invalid_gen1(trans, tfd);
332 }
333 
334 /**
335  * iwl_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
336  * @trans: transport private data
337  * @txq: tx queue
338  * @read_ptr: the TXQ read_ptr to free
339  *
340  * Does NOT advance any TFD circular buffer read/write indexes
341  * Does NOT free the TFD itself (which is within circular buffer)
342  */
iwl_txq_free_tfd(struct iwl_trans * trans,struct iwl_txq * txq,int read_ptr)343 static void iwl_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
344 			     int read_ptr)
345 {
346 	/* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and
347 	 * idx is bounded by n_window
348 	 */
349 	int idx = iwl_txq_get_cmd_index(txq, read_ptr);
350 	struct sk_buff *skb;
351 
352 	lockdep_assert_held(&txq->reclaim_lock);
353 
354 	if (!txq->entries)
355 		return;
356 
357 	/* We have only q->n_window txq->entries, but we use
358 	 * TFD_QUEUE_SIZE_MAX tfds
359 	 */
360 	if (trans->mac_cfg->gen2)
361 		iwl_txq_gen2_tfd_unmap(trans, &txq->entries[idx].meta,
362 				       iwl_txq_get_tfd(trans, txq, read_ptr));
363 	else
364 		iwl_txq_gen1_tfd_unmap(trans, &txq->entries[idx].meta,
365 				       txq, read_ptr);
366 
367 	/* free SKB */
368 	skb = txq->entries[idx].skb;
369 
370 	/* Can be called from irqs-disabled context
371 	 * If skb is not NULL, it means that the whole queue is being
372 	 * freed and that the queue is not empty - free the skb
373 	 */
374 	if (skb) {
375 		iwl_op_mode_free_skb(trans->op_mode, skb);
376 		txq->entries[idx].skb = NULL;
377 	}
378 }
379 
380 /*
381  * iwl_pcie_txq_unmap -  Unmap any remaining DMA mappings and free skb's
382  */
iwl_pcie_txq_unmap(struct iwl_trans * trans,int txq_id)383 static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
384 {
385 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
386 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
387 
388 	if (!txq) {
389 		IWL_ERR(trans, "Trying to free a queue that wasn't allocated?\n");
390 		return;
391 	}
392 
393 	spin_lock_bh(&txq->reclaim_lock);
394 	spin_lock(&txq->lock);
395 	while (txq->write_ptr != txq->read_ptr) {
396 		IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
397 				   txq_id, txq->read_ptr);
398 
399 		if (txq_id != trans->conf.cmd_queue) {
400 			struct sk_buff *skb = txq->entries[txq->read_ptr].skb;
401 			struct iwl_cmd_meta *cmd_meta =
402 				&txq->entries[txq->read_ptr].meta;
403 
404 			if (WARN_ON_ONCE(!skb))
405 				continue;
406 
407 			iwl_pcie_free_tso_pages(trans, skb, cmd_meta);
408 		}
409 		iwl_txq_free_tfd(trans, txq, txq->read_ptr);
410 		txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr);
411 
412 		if (txq->read_ptr == txq->write_ptr &&
413 		    txq_id == trans->conf.cmd_queue)
414 			iwl_pcie_clear_cmd_in_flight(trans);
415 	}
416 
417 	while (!skb_queue_empty(&txq->overflow_q)) {
418 		struct sk_buff *skb = __skb_dequeue(&txq->overflow_q);
419 
420 		iwl_op_mode_free_skb(trans->op_mode, skb);
421 	}
422 
423 	spin_unlock(&txq->lock);
424 	spin_unlock_bh(&txq->reclaim_lock);
425 
426 	/* just in case - this queue may have been stopped */
427 	iwl_trans_pcie_wake_queue(trans, txq);
428 }
429 
430 /*
431  * iwl_pcie_txq_free - Deallocate DMA queue.
432  * @txq: Transmit queue to deallocate.
433  *
434  * Empty queue by removing and destroying all BD's.
435  * Free all buffers.
436  * 0-fill, but do not free "txq" descriptor structure.
437  */
iwl_pcie_txq_free(struct iwl_trans * trans,int txq_id)438 static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
439 {
440 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
441 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
442 	struct device *dev = trans->dev;
443 	int i;
444 
445 	if (WARN_ON(!txq))
446 		return;
447 
448 	iwl_pcie_txq_unmap(trans, txq_id);
449 
450 	/* De-alloc array of command/tx buffers */
451 	if (txq_id == trans->conf.cmd_queue)
452 		for (i = 0; i < txq->n_window; i++) {
453 			kfree_sensitive(txq->entries[i].cmd);
454 			kfree_sensitive(txq->entries[i].free_buf);
455 		}
456 
457 	/* De-alloc circular buffer of TFDs */
458 	if (txq->tfds) {
459 		dma_free_coherent(dev,
460 				  trans_pcie->txqs.tfd.size *
461 				  trans->mac_cfg->base->max_tfd_queue_size,
462 				  txq->tfds, txq->dma_addr);
463 		txq->dma_addr = 0;
464 		txq->tfds = NULL;
465 
466 		dma_free_coherent(dev,
467 				  sizeof(*txq->first_tb_bufs) * txq->n_window,
468 				  txq->first_tb_bufs, txq->first_tb_dma);
469 	}
470 
471 	kfree(txq->entries);
472 	txq->entries = NULL;
473 
474 	timer_delete_sync(&txq->stuck_timer);
475 
476 	/* 0-fill queue descriptor structure */
477 	memset(txq, 0, sizeof(*txq));
478 }
479 
iwl_pcie_tx_start(struct iwl_trans * trans)480 void iwl_pcie_tx_start(struct iwl_trans *trans)
481 {
482 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
483 	int nq = trans->mac_cfg->base->num_of_queues;
484 	int chan;
485 	u32 reg_val;
486 	int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
487 				SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
488 
489 	/* make sure all queue are not stopped/used */
490 	memset(trans_pcie->txqs.queue_stopped, 0,
491 	       sizeof(trans_pcie->txqs.queue_stopped));
492 	memset(trans_pcie->txqs.queue_used, 0,
493 	       sizeof(trans_pcie->txqs.queue_used));
494 
495 	trans_pcie->scd_base_addr =
496 		iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
497 
498 	/* reset context data, TX status and translation data */
499 	iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
500 				   SCD_CONTEXT_MEM_LOWER_BOUND,
501 			    NULL, clear_dwords);
502 
503 	iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
504 		       trans_pcie->txqs.scd_bc_tbls.dma >> 10);
505 
506 	/* The chain extension of the SCD doesn't work well. This feature is
507 	 * enabled by default by the HW, so we need to disable it manually.
508 	 */
509 	if (trans->mac_cfg->base->scd_chain_ext_wa)
510 		iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
511 
512 	iwl_trans_ac_txq_enable(trans, trans->conf.cmd_queue,
513 				trans->conf.cmd_fifo,
514 				IWL_DEF_WD_TIMEOUT);
515 
516 	/* Activate all Tx DMA/FIFO channels */
517 	iwl_scd_activate_fifos(trans);
518 
519 	/* Enable DMA channel */
520 	for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
521 		iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
522 				   FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
523 				   FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
524 
525 	/* Update FH chicken bits */
526 	reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
527 	iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
528 			   reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
529 
530 	/* Enable L1-Active */
531 	if (trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_8000)
532 		iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
533 				    APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
534 }
535 
iwl_trans_pcie_tx_reset(struct iwl_trans * trans)536 void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
537 {
538 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
539 	int txq_id;
540 
541 	/*
542 	 * we should never get here in gen2 trans mode return early to avoid
543 	 * having invalid accesses
544 	 */
545 	if (WARN_ON_ONCE(trans->mac_cfg->gen2))
546 		return;
547 
548 	for (txq_id = 0; txq_id < trans->mac_cfg->base->num_of_queues;
549 	     txq_id++) {
550 		struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
551 		if (trans->mac_cfg->gen2)
552 			iwl_write_direct64(trans,
553 					   FH_MEM_CBBC_QUEUE(trans, txq_id),
554 					   txq->dma_addr);
555 		else
556 			iwl_write_direct32(trans,
557 					   FH_MEM_CBBC_QUEUE(trans, txq_id),
558 					   txq->dma_addr >> 8);
559 		iwl_pcie_txq_unmap(trans, txq_id);
560 		txq->read_ptr = 0;
561 		txq->write_ptr = 0;
562 	}
563 
564 	/* Tell NIC where to find the "keep warm" buffer */
565 	iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
566 			   trans_pcie->kw.dma >> 4);
567 
568 	/*
569 	 * Send 0 as the scd_base_addr since the device may have be reset
570 	 * while we were in WoWLAN in which case SCD_SRAM_BASE_ADDR will
571 	 * contain garbage.
572 	 */
573 	iwl_pcie_tx_start(trans);
574 }
575 
iwl_pcie_tx_stop_fh(struct iwl_trans * trans)576 static void iwl_pcie_tx_stop_fh(struct iwl_trans *trans)
577 {
578 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
579 	int ch, ret;
580 	u32 mask = 0;
581 
582 	spin_lock_bh(&trans_pcie->irq_lock);
583 
584 	if (!iwl_trans_grab_nic_access(trans))
585 		goto out;
586 
587 	/* Stop each Tx DMA channel */
588 	for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
589 		iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
590 		mask |= FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch);
591 	}
592 
593 	/* Wait for DMA channels to be idle */
594 	ret = iwl_poll_bits(trans, FH_TSSR_TX_STATUS_REG, mask, 5000);
595 	if (ret)
596 		IWL_ERR(trans,
597 			"Failing on timeout while stopping DMA channel %d [0x%08x]\n",
598 			ch, iwl_read32(trans, FH_TSSR_TX_STATUS_REG));
599 
600 	iwl_trans_release_nic_access(trans);
601 
602 out:
603 	spin_unlock_bh(&trans_pcie->irq_lock);
604 }
605 
606 /*
607  * iwl_pcie_tx_stop - Stop all Tx DMA channels
608  */
iwl_pcie_tx_stop(struct iwl_trans * trans)609 int iwl_pcie_tx_stop(struct iwl_trans *trans)
610 {
611 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
612 	int txq_id;
613 
614 	/* Turn off all Tx DMA fifos */
615 	iwl_scd_deactivate_fifos(trans);
616 
617 	/* Turn off all Tx DMA channels */
618 	iwl_pcie_tx_stop_fh(trans);
619 
620 	/*
621 	 * This function can be called before the op_mode disabled the
622 	 * queues. This happens when we have an rfkill interrupt.
623 	 * Since we stop Tx altogether - mark the queues as stopped.
624 	 */
625 	memset(trans_pcie->txqs.queue_stopped, 0,
626 	       sizeof(trans_pcie->txqs.queue_stopped));
627 	memset(trans_pcie->txqs.queue_used, 0,
628 	       sizeof(trans_pcie->txqs.queue_used));
629 
630 	/* This can happen: start_hw, stop_device */
631 	if (!trans_pcie->txq_memory)
632 		return 0;
633 
634 	/* Unmap DMA from host system and free skb's */
635 	for (txq_id = 0; txq_id < trans->mac_cfg->base->num_of_queues;
636 	     txq_id++)
637 		iwl_pcie_txq_unmap(trans, txq_id);
638 
639 	return 0;
640 }
641 
642 /*
643  * iwl_trans_tx_free - Free TXQ Context
644  *
645  * Destroy all TX DMA queues and structures
646  */
iwl_pcie_tx_free(struct iwl_trans * trans)647 void iwl_pcie_tx_free(struct iwl_trans *trans)
648 {
649 	int txq_id;
650 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
651 
652 	memset(trans_pcie->txqs.queue_used, 0,
653 	       sizeof(trans_pcie->txqs.queue_used));
654 
655 	/* Tx queues */
656 	if (trans_pcie->txq_memory) {
657 		for (txq_id = 0;
658 		     txq_id < trans->mac_cfg->base->num_of_queues;
659 		     txq_id++) {
660 			iwl_pcie_txq_free(trans, txq_id);
661 			trans_pcie->txqs.txq[txq_id] = NULL;
662 		}
663 	}
664 
665 	kfree(trans_pcie->txq_memory);
666 	trans_pcie->txq_memory = NULL;
667 
668 	iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
669 
670 	iwl_pcie_free_dma_ptr(trans, &trans_pcie->txqs.scd_bc_tbls);
671 }
672 
iwl_txq_log_scd_error(struct iwl_trans * trans,struct iwl_txq * txq)673 void iwl_txq_log_scd_error(struct iwl_trans *trans, struct iwl_txq *txq)
674 {
675 	u32 txq_id = txq->id;
676 	u32 status;
677 	bool active;
678 	u8 fifo;
679 
680 	if (trans->mac_cfg->gen2) {
681 		IWL_ERR(trans, "Queue %d is stuck %d %d\n", txq_id,
682 			txq->read_ptr, txq->write_ptr);
683 		/* TODO: access new SCD registers and dump them */
684 		return;
685 	}
686 
687 	status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id));
688 	fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
689 	active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
690 
691 	IWL_ERR(trans,
692 		"Queue %d is %sactive on fifo %d and stuck for %u ms. SW [%d, %d] HW [%d, %d] FH TRB=0x0%x\n",
693 		txq_id, active ? "" : "in", fifo,
694 		jiffies_to_msecs(txq->wd_timeout),
695 		txq->read_ptr, txq->write_ptr,
696 		iwl_read_prph(trans, SCD_QUEUE_RDPTR(txq_id)) &
697 			(trans->mac_cfg->base->max_tfd_queue_size - 1),
698 			iwl_read_prph(trans, SCD_QUEUE_WRPTR(txq_id)) &
699 			(trans->mac_cfg->base->max_tfd_queue_size - 1),
700 			iwl_read_direct32(trans, FH_TX_TRB_REG(fifo)));
701 }
702 
iwl_txq_stuck_timer(struct timer_list * t)703 static void iwl_txq_stuck_timer(struct timer_list *t)
704 {
705 	struct iwl_txq *txq = timer_container_of(txq, t, stuck_timer);
706 	struct iwl_trans *trans = txq->trans;
707 
708 	spin_lock(&txq->lock);
709 	/* check if triggered erroneously */
710 	if (txq->read_ptr == txq->write_ptr) {
711 		spin_unlock(&txq->lock);
712 		return;
713 	}
714 	spin_unlock(&txq->lock);
715 
716 	iwl_txq_log_scd_error(trans, txq);
717 
718 	iwl_force_nmi(trans);
719 }
720 
iwl_pcie_txq_alloc(struct iwl_trans * trans,struct iwl_txq * txq,int slots_num,bool cmd_queue)721 int iwl_pcie_txq_alloc(struct iwl_trans *trans, struct iwl_txq *txq,
722 		       int slots_num, bool cmd_queue)
723 {
724 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
725 	size_t num_entries = trans->mac_cfg->gen2 ?
726 		slots_num : trans->mac_cfg->base->max_tfd_queue_size;
727 	size_t tfd_sz;
728 	size_t tb0_buf_sz;
729 	int i;
730 
731 	if (WARN_ONCE(slots_num <= 0, "Invalid slots num:%d\n", slots_num))
732 		return -EINVAL;
733 
734 	if (WARN_ON(txq->entries || txq->tfds))
735 		return -EINVAL;
736 
737 	tfd_sz = trans_pcie->txqs.tfd.size * num_entries;
738 
739 	timer_setup(&txq->stuck_timer, iwl_txq_stuck_timer, 0);
740 	txq->trans = trans;
741 
742 	txq->n_window = slots_num;
743 
744 	txq->entries = kzalloc_objs(struct iwl_pcie_txq_entry, slots_num);
745 
746 	if (!txq->entries)
747 		goto error;
748 
749 	if (cmd_queue)
750 		for (i = 0; i < slots_num; i++) {
751 			txq->entries[i].cmd =
752 				kmalloc_obj(struct iwl_device_cmd);
753 			if (!txq->entries[i].cmd)
754 				goto error;
755 		}
756 
757 	/* Circular buffer of transmit frame descriptors (TFDs),
758 	 * shared with device
759 	 */
760 	txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
761 				       &txq->dma_addr, GFP_KERNEL);
762 	if (!txq->tfds)
763 		goto error;
764 
765 	BUILD_BUG_ON(sizeof(*txq->first_tb_bufs) != IWL_FIRST_TB_SIZE_ALIGN);
766 
767 	tb0_buf_sz = sizeof(*txq->first_tb_bufs) * slots_num;
768 
769 	txq->first_tb_bufs = dma_alloc_coherent(trans->dev, tb0_buf_sz,
770 						&txq->first_tb_dma,
771 						GFP_KERNEL);
772 	if (!txq->first_tb_bufs)
773 		goto err_free_tfds;
774 
775 	for (i = 0; i < num_entries; i++) {
776 		void *tfd = iwl_txq_get_tfd(trans, txq, i);
777 
778 		if (trans->mac_cfg->gen2)
779 			iwl_txq_set_tfd_invalid_gen2(trans, tfd);
780 		else
781 			iwl_txq_set_tfd_invalid_gen1(trans, tfd);
782 	}
783 
784 	return 0;
785 err_free_tfds:
786 	dma_free_coherent(trans->dev, tfd_sz, txq->tfds, txq->dma_addr);
787 	txq->tfds = NULL;
788 error:
789 	if (txq->entries && cmd_queue)
790 		for (i = 0; i < slots_num; i++)
791 			kfree(txq->entries[i].cmd);
792 	kfree(txq->entries);
793 	txq->entries = NULL;
794 
795 	return -ENOMEM;
796 }
797 
798 #define BC_TABLE_SIZE	(sizeof(struct iwl_bc_tbl_entry) * TFD_QUEUE_BC_SIZE)
799 
800 /*
801  * iwl_pcie_tx_alloc - allocate TX context
802  * Allocate all Tx DMA structures and initialize them
803  */
iwl_pcie_tx_alloc(struct iwl_trans * trans)804 static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
805 {
806 	int ret;
807 	int txq_id, slots_num;
808 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
809 	u16 bc_tbls_size = trans->mac_cfg->base->num_of_queues;
810 
811 	if (WARN_ON(trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210))
812 		return -EINVAL;
813 
814 	bc_tbls_size *= BC_TABLE_SIZE;
815 
816 	/*It is not allowed to alloc twice, so warn when this happens.
817 	 * We cannot rely on the previous allocation, so free and fail */
818 	if (WARN_ON(trans_pcie->txq_memory)) {
819 		ret = -EINVAL;
820 		goto error;
821 	}
822 
823 	ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->txqs.scd_bc_tbls,
824 				     bc_tbls_size);
825 	if (ret) {
826 		IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
827 		goto error;
828 	}
829 
830 	/* Alloc keep-warm buffer */
831 	ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
832 	if (ret) {
833 		IWL_ERR(trans, "Keep Warm allocation failed\n");
834 		goto error;
835 	}
836 
837 	trans_pcie->txq_memory =
838 		kzalloc_objs(struct iwl_txq,
839 			     trans->mac_cfg->base->num_of_queues);
840 	if (!trans_pcie->txq_memory) {
841 		IWL_ERR(trans, "Not enough memory for txq\n");
842 		ret = -ENOMEM;
843 		goto error;
844 	}
845 
846 	/* Alloc and init all Tx queues, including the command queue (#4/#9) */
847 	for (txq_id = 0; txq_id < trans->mac_cfg->base->num_of_queues;
848 	     txq_id++) {
849 		bool cmd_queue = (txq_id == trans->conf.cmd_queue);
850 
851 		if (cmd_queue)
852 			slots_num = max_t(u32, IWL_CMD_QUEUE_SIZE,
853 					  trans->mac_cfg->base->min_txq_size);
854 		else
855 			slots_num = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
856 					  trans->mac_cfg->base->min_ba_txq_size);
857 		trans_pcie->txqs.txq[txq_id] = &trans_pcie->txq_memory[txq_id];
858 		ret = iwl_pcie_txq_alloc(trans, trans_pcie->txqs.txq[txq_id],
859 					 slots_num, cmd_queue);
860 		if (ret) {
861 			IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
862 			goto error;
863 		}
864 		trans_pcie->txqs.txq[txq_id]->id = txq_id;
865 	}
866 
867 	return 0;
868 
869 error:
870 	iwl_pcie_tx_free(trans);
871 
872 	return ret;
873 }
874 
875 /*
876  * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
877  */
iwl_queue_init(struct iwl_txq * q,int slots_num)878 static int iwl_queue_init(struct iwl_txq *q, int slots_num)
879 {
880 	q->n_window = slots_num;
881 
882 	/* slots_num must be power-of-two size, otherwise
883 	 * iwl_txq_get_cmd_index is broken.
884 	 */
885 	if (WARN_ON(!is_power_of_2(slots_num)))
886 		return -EINVAL;
887 
888 	q->low_mark = q->n_window / 4;
889 	if (q->low_mark < 4)
890 		q->low_mark = 4;
891 
892 	q->high_mark = q->n_window / 8;
893 	if (q->high_mark < 2)
894 		q->high_mark = 2;
895 
896 	q->write_ptr = 0;
897 	q->read_ptr = 0;
898 
899 	return 0;
900 }
901 
iwl_txq_init(struct iwl_trans * trans,struct iwl_txq * txq,int slots_num,bool cmd_queue)902 int iwl_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
903 		 int slots_num, bool cmd_queue)
904 {
905 	u32 tfd_queue_max_size =
906 		trans->mac_cfg->base->max_tfd_queue_size;
907 	int ret;
908 
909 	txq->need_update = false;
910 
911 	/* max_tfd_queue_size must be power-of-two size, otherwise
912 	 * iwl_txq_inc_wrap and iwl_txq_dec_wrap are broken.
913 	 */
914 	if (WARN_ONCE(tfd_queue_max_size & (tfd_queue_max_size - 1),
915 		      "Max tfd queue size must be a power of two, but is %d",
916 		      tfd_queue_max_size))
917 		return -EINVAL;
918 
919 	/* Initialize queue's high/low-water marks, and head/tail indexes */
920 	ret = iwl_queue_init(txq, slots_num);
921 	if (ret)
922 		return ret;
923 
924 	spin_lock_init(&txq->lock);
925 	spin_lock_init(&txq->reclaim_lock);
926 
927 	if (cmd_queue) {
928 		static struct lock_class_key iwl_txq_cmd_queue_lock_class;
929 
930 		lockdep_set_class(&txq->lock, &iwl_txq_cmd_queue_lock_class);
931 	}
932 
933 	__skb_queue_head_init(&txq->overflow_q);
934 
935 	return 0;
936 }
937 
iwl_pcie_tx_init(struct iwl_trans * trans)938 int iwl_pcie_tx_init(struct iwl_trans *trans)
939 {
940 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
941 	int ret;
942 	int txq_id, slots_num;
943 	bool alloc = false;
944 
945 	if (!trans_pcie->txq_memory) {
946 		ret = iwl_pcie_tx_alloc(trans);
947 		if (ret)
948 			goto error;
949 		alloc = true;
950 	}
951 
952 	spin_lock_bh(&trans_pcie->irq_lock);
953 
954 	/* Turn off all Tx DMA fifos */
955 	iwl_scd_deactivate_fifos(trans);
956 
957 	/* Tell NIC where to find the "keep warm" buffer */
958 	iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
959 			   trans_pcie->kw.dma >> 4);
960 
961 	spin_unlock_bh(&trans_pcie->irq_lock);
962 
963 	/* Alloc and init all Tx queues, including the command queue (#4/#9) */
964 	for (txq_id = 0; txq_id < trans->mac_cfg->base->num_of_queues;
965 	     txq_id++) {
966 		bool cmd_queue = (txq_id == trans->conf.cmd_queue);
967 
968 		if (cmd_queue)
969 			slots_num = max_t(u32, IWL_CMD_QUEUE_SIZE,
970 					  trans->mac_cfg->base->min_txq_size);
971 		else
972 			slots_num = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
973 					  trans->mac_cfg->base->min_ba_txq_size);
974 		ret = iwl_txq_init(trans, trans_pcie->txqs.txq[txq_id], slots_num,
975 				   cmd_queue);
976 		if (ret) {
977 			IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
978 			goto error;
979 		}
980 
981 		/*
982 		 * Tell nic where to find circular buffer of TFDs for a
983 		 * given Tx queue, and enable the DMA channel used for that
984 		 * queue.
985 		 * Circular buffer (TFD queue in DRAM) physical base address
986 		 */
987 		iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(trans, txq_id),
988 				   trans_pcie->txqs.txq[txq_id]->dma_addr >> 8);
989 	}
990 
991 	iwl_set_bits_prph(trans, SCD_GP_CTRL, SCD_GP_CTRL_AUTO_ACTIVE_MODE);
992 	if (trans->mac_cfg->base->num_of_queues > 20)
993 		iwl_set_bits_prph(trans, SCD_GP_CTRL,
994 				  SCD_GP_CTRL_ENABLE_31_QUEUES);
995 
996 	return 0;
997 error:
998 	/*Upon error, free only if we allocated something */
999 	if (alloc)
1000 		iwl_pcie_tx_free(trans);
1001 	return ret;
1002 }
1003 
iwl_pcie_set_cmd_in_flight(struct iwl_trans * trans,const struct iwl_host_cmd * cmd)1004 static int iwl_pcie_set_cmd_in_flight(struct iwl_trans *trans,
1005 				      const struct iwl_host_cmd *cmd)
1006 {
1007 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1008 
1009 	/* Make sure the NIC is still alive in the bus */
1010 	if (test_bit(STATUS_TRANS_DEAD, &trans->status))
1011 		return -ENODEV;
1012 
1013 	if (!trans->mac_cfg->base->apmg_wake_up_wa)
1014 		return 0;
1015 
1016 	/*
1017 	 * wake up the NIC to make sure that the firmware will see the host
1018 	 * command - we will let the NIC sleep once all the host commands
1019 	 * returned. This needs to be done only on NICs that have
1020 	 * apmg_wake_up_wa set (see above.)
1021 	 */
1022 	if (!_iwl_trans_pcie_grab_nic_access(trans, false))
1023 		return -EIO;
1024 
1025 	/*
1026 	 * In iwl_trans_grab_nic_access(), we've acquired the reg_lock.
1027 	 * There, we also returned immediately if cmd_hold_nic_awake is
1028 	 * already true, so it's OK to unconditionally set it to true.
1029 	 */
1030 	trans_pcie->cmd_hold_nic_awake = true;
1031 	spin_unlock(&trans_pcie->reg_lock);
1032 
1033 	return 0;
1034 }
1035 
iwl_txq_progress(struct iwl_txq * txq)1036 static void iwl_txq_progress(struct iwl_txq *txq)
1037 {
1038 	lockdep_assert_held(&txq->lock);
1039 
1040 	if (!txq->wd_timeout)
1041 		return;
1042 
1043 	/*
1044 	 * station is asleep and we send data - that must
1045 	 * be uAPSD or PS-Poll. Don't rearm the timer.
1046 	 */
1047 	if (txq->frozen)
1048 		return;
1049 
1050 	/*
1051 	 * if empty delete timer, otherwise move timer forward
1052 	 * since we're making progress on this queue
1053 	 */
1054 	if (txq->read_ptr == txq->write_ptr)
1055 		timer_delete(&txq->stuck_timer);
1056 	else
1057 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
1058 }
1059 
iwl_txq_used(const struct iwl_txq * q,int i,int read_ptr,int write_ptr)1060 static inline bool iwl_txq_used(const struct iwl_txq *q, int i,
1061 				int read_ptr, int write_ptr)
1062 {
1063 	int index = iwl_txq_get_cmd_index(q, i);
1064 	int r = iwl_txq_get_cmd_index(q, read_ptr);
1065 	int w = iwl_txq_get_cmd_index(q, write_ptr);
1066 
1067 	return w >= r ?
1068 		(index >= r && index < w) :
1069 		!(index < r && index >= w);
1070 }
1071 
1072 /*
1073  * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
1074  *
1075  * When FW advances 'R' index, all entries between old and new 'R' index
1076  * need to be reclaimed. As result, some free space forms.  If there is
1077  * enough free space (> low mark), wake the stack that feeds us.
1078  */
iwl_pcie_cmdq_reclaim(struct iwl_trans * trans,int txq_id,int idx)1079 static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
1080 {
1081 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1082 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
1083 	int nfreed = 0;
1084 	u16 r;
1085 
1086 	lockdep_assert_held(&txq->lock);
1087 
1088 	idx = iwl_txq_get_cmd_index(txq, idx);
1089 	r = iwl_txq_get_cmd_index(txq, txq->read_ptr);
1090 
1091 	if (idx >= trans->mac_cfg->base->max_tfd_queue_size ||
1092 	    (!iwl_txq_used(txq, idx, txq->read_ptr, txq->write_ptr))) {
1093 		WARN_ONCE(test_bit(txq_id, trans_pcie->txqs.queue_used),
1094 			  "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
1095 			  __func__, txq_id, idx,
1096 			  trans->mac_cfg->base->max_tfd_queue_size,
1097 			  txq->write_ptr, txq->read_ptr);
1098 		return;
1099 	}
1100 
1101 	for (idx = iwl_txq_inc_wrap(trans, idx); r != idx;
1102 	     r = iwl_txq_inc_wrap(trans, r)) {
1103 		txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr);
1104 
1105 		if (nfreed++ > 0) {
1106 			IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
1107 				idx, txq->write_ptr, r);
1108 			iwl_force_nmi(trans);
1109 		}
1110 	}
1111 
1112 	if (txq->read_ptr == txq->write_ptr)
1113 		iwl_pcie_clear_cmd_in_flight(trans);
1114 
1115 	iwl_txq_progress(txq);
1116 }
1117 
iwl_pcie_txq_set_ratid_map(struct iwl_trans * trans,u16 ra_tid,u16 txq_id)1118 static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
1119 				 u16 txq_id)
1120 {
1121 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1122 	u32 tbl_dw_addr;
1123 	u32 tbl_dw;
1124 	u16 scd_q2ratid;
1125 
1126 	scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
1127 
1128 	tbl_dw_addr = trans_pcie->scd_base_addr +
1129 			SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
1130 
1131 	tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
1132 
1133 	if (txq_id & 0x1)
1134 		tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
1135 	else
1136 		tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
1137 
1138 	iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
1139 
1140 	return 0;
1141 }
1142 
1143 /* Receiver address (actually, Rx station's index into station table),
1144  * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
1145 #define BUILD_RAxTID(sta_id, tid)	(((sta_id) << 4) + (tid))
1146 
iwl_trans_pcie_txq_enable(struct iwl_trans * trans,int txq_id,u16 ssn,const struct iwl_trans_txq_scd_cfg * cfg,unsigned int wdg_timeout)1147 bool iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, u16 ssn,
1148 			       const struct iwl_trans_txq_scd_cfg *cfg,
1149 			       unsigned int wdg_timeout)
1150 {
1151 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1152 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
1153 	int fifo = -1;
1154 	bool scd_bug = false;
1155 
1156 	if (test_and_set_bit(txq_id, trans_pcie->txqs.queue_used))
1157 		WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
1158 
1159 	txq->wd_timeout = msecs_to_jiffies(wdg_timeout);
1160 
1161 	if (cfg) {
1162 		fifo = cfg->fifo;
1163 
1164 		/* Disable the scheduler prior configuring the cmd queue */
1165 		if (txq_id == trans->conf.cmd_queue &&
1166 		    trans->conf.scd_set_active)
1167 			iwl_scd_enable_set_active(trans, 0);
1168 
1169 		/* Stop this Tx queue before configuring it */
1170 		iwl_scd_txq_set_inactive(trans, txq_id);
1171 
1172 		/* Set this queue as a chain-building queue unless it is CMD */
1173 		if (txq_id != trans->conf.cmd_queue)
1174 			iwl_scd_txq_set_chain(trans, txq_id);
1175 
1176 		if (cfg->aggregate) {
1177 			u16 ra_tid = BUILD_RAxTID(cfg->sta_id, cfg->tid);
1178 
1179 			/* Map receiver-address / traffic-ID to this queue */
1180 			iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
1181 
1182 			/* enable aggregations for the queue */
1183 			iwl_scd_txq_enable_agg(trans, txq_id);
1184 			txq->ampdu = true;
1185 		} else {
1186 			/*
1187 			 * disable aggregations for the queue, this will also
1188 			 * make the ra_tid mapping configuration irrelevant
1189 			 * since it is now a non-AGG queue.
1190 			 */
1191 			iwl_scd_txq_disable_agg(trans, txq_id);
1192 
1193 			ssn = txq->read_ptr;
1194 		}
1195 	} else {
1196 		/*
1197 		 * If we need to move the SCD write pointer by steps of
1198 		 * 0x40, 0x80 or 0xc0, it gets stuck. Avoids this and let
1199 		 * the op_mode know by returning true later.
1200 		 * Do this only in case cfg is NULL since this trick can
1201 		 * be done only if we have DQA enabled which is true for mvm
1202 		 * only. And mvm never sets a cfg pointer.
1203 		 * This is really ugly, but this is the easiest way out for
1204 		 * this sad hardware issue.
1205 		 * This bug has been fixed on devices 9000 and up.
1206 		 */
1207 		scd_bug = !trans->mac_cfg->mq_rx_supported &&
1208 			!((ssn - txq->write_ptr) & 0x3f) &&
1209 			(ssn != txq->write_ptr);
1210 		if (scd_bug)
1211 			ssn++;
1212 	}
1213 
1214 	/* Place first TFD at index corresponding to start sequence number.
1215 	 * Assumes that ssn_idx is valid (!= 0xFFF) */
1216 	txq->read_ptr = (ssn & 0xff);
1217 	txq->write_ptr = (ssn & 0xff);
1218 	iwl_write_direct32(trans, HBUS_TARG_WRPTR,
1219 			   (ssn & 0xff) | (txq_id << 8));
1220 
1221 	if (cfg) {
1222 		u8 frame_limit = cfg->frame_limit;
1223 
1224 		iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
1225 
1226 		/* Set up Tx window size and frame limit for this queue */
1227 		iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
1228 				SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
1229 		iwl_trans_write_mem32(trans,
1230 			trans_pcie->scd_base_addr +
1231 			SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
1232 			SCD_QUEUE_CTX_REG2_VAL(WIN_SIZE, frame_limit) |
1233 			SCD_QUEUE_CTX_REG2_VAL(FRAME_LIMIT, frame_limit));
1234 
1235 		/* Set up status area in SRAM, map to Tx DMA/FIFO, activate */
1236 		iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
1237 			       (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1238 			       (cfg->fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
1239 			       (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
1240 			       SCD_QUEUE_STTS_REG_MSK);
1241 
1242 		/* enable the scheduler for this queue (only) */
1243 		if (txq_id == trans->conf.cmd_queue &&
1244 		    trans->conf.scd_set_active)
1245 			iwl_scd_enable_set_active(trans, BIT(txq_id));
1246 
1247 		IWL_DEBUG_TX_QUEUES(trans,
1248 				    "Activate queue %d on FIFO %d WrPtr: %d\n",
1249 				    txq_id, fifo, ssn & 0xff);
1250 	} else {
1251 		IWL_DEBUG_TX_QUEUES(trans,
1252 				    "Activate queue %d WrPtr: %d\n",
1253 				    txq_id, ssn & 0xff);
1254 	}
1255 
1256 	return scd_bug;
1257 }
1258 
iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans * trans,u32 txq_id,bool shared_mode)1259 void iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans *trans, u32 txq_id,
1260 					bool shared_mode)
1261 {
1262 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1263 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
1264 
1265 	txq->ampdu = !shared_mode;
1266 }
1267 
iwl_trans_pcie_txq_disable(struct iwl_trans * trans,int txq_id,bool configure_scd)1268 void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id,
1269 				bool configure_scd)
1270 {
1271 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1272 	u32 stts_addr = trans_pcie->scd_base_addr +
1273 			SCD_TX_STTS_QUEUE_OFFSET(txq_id);
1274 	static const u32 zero_val[4] = {};
1275 
1276 	trans_pcie->txqs.txq[txq_id]->frozen_expiry_remainder = 0;
1277 	trans_pcie->txqs.txq[txq_id]->frozen = false;
1278 
1279 	/*
1280 	 * Upon HW Rfkill - we stop the device, and then stop the queues
1281 	 * in the op_mode. Just for the sake of the simplicity of the op_mode,
1282 	 * allow the op_mode to call txq_disable after it already called
1283 	 * stop_device.
1284 	 */
1285 	if (!test_and_clear_bit(txq_id, trans_pcie->txqs.queue_used)) {
1286 		WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
1287 			  "queue %d not used", txq_id);
1288 		return;
1289 	}
1290 
1291 	if (configure_scd) {
1292 		iwl_scd_txq_set_inactive(trans, txq_id);
1293 
1294 		iwl_trans_write_mem(trans, stts_addr, (const void *)zero_val,
1295 				    ARRAY_SIZE(zero_val));
1296 	}
1297 
1298 	iwl_pcie_txq_unmap(trans, txq_id);
1299 	trans_pcie->txqs.txq[txq_id]->ampdu = false;
1300 
1301 	IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
1302 }
1303 
1304 /*************** HOST COMMAND QUEUE FUNCTIONS   *****/
1305 
iwl_trans_pcie_block_txq_ptrs(struct iwl_trans * trans,bool block)1306 static void iwl_trans_pcie_block_txq_ptrs(struct iwl_trans *trans, bool block)
1307 {
1308 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1309 	int i;
1310 
1311 	for (i = 0; i < trans->mac_cfg->base->num_of_queues; i++) {
1312 		struct iwl_txq *txq = trans_pcie->txqs.txq[i];
1313 
1314 		if (i == trans->conf.cmd_queue)
1315 			continue;
1316 
1317 		/* we skip the command queue (obviously) so it's OK to nest */
1318 		spin_lock_nested(&txq->lock, 1);
1319 
1320 		if (!block && !(WARN_ON_ONCE(!txq->block))) {
1321 			txq->block--;
1322 			if (!txq->block) {
1323 				iwl_write32(trans, HBUS_TARG_WRPTR,
1324 					    txq->write_ptr | (i << 8));
1325 			}
1326 		} else if (block) {
1327 			txq->block++;
1328 		}
1329 
1330 		spin_unlock(&txq->lock);
1331 	}
1332 }
1333 
1334 /*
1335  * iwl_pcie_enqueue_hcmd - enqueue a uCode command
1336  * @priv: device private data point
1337  * @cmd: a pointer to the ucode command structure
1338  *
1339  * The function returns < 0 values to indicate the operation
1340  * failed. On success, it returns the index (>= 0) of command in the
1341  * command queue.
1342  */
iwl_pcie_enqueue_hcmd(struct iwl_trans * trans,struct iwl_host_cmd * cmd)1343 int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
1344 			  struct iwl_host_cmd *cmd)
1345 {
1346 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1347 	struct iwl_txq *txq = trans_pcie->txqs.txq[trans->conf.cmd_queue];
1348 	struct iwl_device_cmd *out_cmd;
1349 	struct iwl_cmd_meta *out_meta;
1350 	void *dup_buf = NULL;
1351 	dma_addr_t phys_addr;
1352 	int idx;
1353 	u16 copy_size, cmd_size, tb0_size;
1354 	bool had_nocopy = false;
1355 	u8 group_id = iwl_cmd_groupid(cmd->id);
1356 	int i, ret;
1357 	u32 cmd_pos;
1358 	const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
1359 	u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
1360 	unsigned long flags;
1361 
1362 	if (WARN(!trans->conf.wide_cmd_header &&
1363 		 group_id > IWL_ALWAYS_LONG_GROUP,
1364 		 "unsupported wide command %#x\n", cmd->id))
1365 		return -EINVAL;
1366 
1367 	if (group_id != 0) {
1368 		copy_size = sizeof(struct iwl_cmd_header_wide);
1369 		cmd_size = sizeof(struct iwl_cmd_header_wide);
1370 	} else {
1371 		copy_size = sizeof(struct iwl_cmd_header);
1372 		cmd_size = sizeof(struct iwl_cmd_header);
1373 	}
1374 
1375 	/* need one for the header if the first is NOCOPY */
1376 	BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
1377 
1378 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1379 		cmddata[i] = cmd->data[i];
1380 		cmdlen[i] = cmd->len[i];
1381 
1382 		if (!cmd->len[i])
1383 			continue;
1384 
1385 		/* need at least IWL_FIRST_TB_SIZE copied */
1386 		if (copy_size < IWL_FIRST_TB_SIZE) {
1387 			int copy = IWL_FIRST_TB_SIZE - copy_size;
1388 
1389 			if (copy > cmdlen[i])
1390 				copy = cmdlen[i];
1391 			cmdlen[i] -= copy;
1392 			cmddata[i] += copy;
1393 			copy_size += copy;
1394 		}
1395 
1396 		if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
1397 			had_nocopy = true;
1398 			if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
1399 				idx = -EINVAL;
1400 				goto free_dup_buf;
1401 			}
1402 		} else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
1403 			/*
1404 			 * This is also a chunk that isn't copied
1405 			 * to the static buffer so set had_nocopy.
1406 			 */
1407 			had_nocopy = true;
1408 
1409 			/* only allowed once */
1410 			if (WARN_ON(dup_buf)) {
1411 				idx = -EINVAL;
1412 				goto free_dup_buf;
1413 			}
1414 
1415 			dup_buf = kmemdup(cmddata[i], cmdlen[i],
1416 					  GFP_ATOMIC);
1417 			if (!dup_buf)
1418 				return -ENOMEM;
1419 		} else {
1420 			/* NOCOPY must not be followed by normal! */
1421 			if (WARN_ON(had_nocopy)) {
1422 				idx = -EINVAL;
1423 				goto free_dup_buf;
1424 			}
1425 			copy_size += cmdlen[i];
1426 		}
1427 		cmd_size += cmd->len[i];
1428 	}
1429 
1430 	/*
1431 	 * If any of the command structures end up being larger than
1432 	 * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
1433 	 * allocated into separate TFDs, then we will need to
1434 	 * increase the size of the buffers.
1435 	 */
1436 	if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
1437 		 "Command %s (%#x) is too large (%d bytes)\n",
1438 		 iwl_get_cmd_string(trans, cmd->id),
1439 		 cmd->id, copy_size)) {
1440 		idx = -EINVAL;
1441 		goto free_dup_buf;
1442 	}
1443 
1444 	spin_lock_irqsave(&txq->lock, flags);
1445 
1446 	if (iwl_txq_space(trans, txq) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
1447 		spin_unlock_irqrestore(&txq->lock, flags);
1448 
1449 		IWL_ERR(trans, "No space in command queue\n");
1450 		iwl_op_mode_nic_error(trans->op_mode,
1451 				      IWL_ERR_TYPE_CMD_QUEUE_FULL);
1452 		iwl_trans_schedule_reset(trans, IWL_ERR_TYPE_CMD_QUEUE_FULL);
1453 		idx = -ENOSPC;
1454 		goto free_dup_buf;
1455 	}
1456 
1457 	idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
1458 	out_cmd = txq->entries[idx].cmd;
1459 	out_meta = &txq->entries[idx].meta;
1460 
1461 	/* re-initialize, this also marks the SG list as unused */
1462 	memset(out_meta, 0, sizeof(*out_meta));
1463 	if (cmd->flags & CMD_WANT_SKB)
1464 		out_meta->source = cmd;
1465 
1466 	/* set up the header */
1467 	if (group_id != 0) {
1468 		out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
1469 		out_cmd->hdr_wide.group_id = group_id;
1470 		out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
1471 		out_cmd->hdr_wide.length =
1472 			cpu_to_le16(cmd_size -
1473 				    sizeof(struct iwl_cmd_header_wide));
1474 		out_cmd->hdr_wide.reserved = 0;
1475 		out_cmd->hdr_wide.sequence =
1476 			cpu_to_le16(QUEUE_TO_SEQ(trans->conf.cmd_queue) |
1477 						 INDEX_TO_SEQ(txq->write_ptr));
1478 
1479 		cmd_pos = sizeof(struct iwl_cmd_header_wide);
1480 		copy_size = sizeof(struct iwl_cmd_header_wide);
1481 	} else {
1482 		out_cmd->hdr.cmd = iwl_cmd_opcode(cmd->id);
1483 		out_cmd->hdr.sequence =
1484 			cpu_to_le16(QUEUE_TO_SEQ(trans->conf.cmd_queue) |
1485 						 INDEX_TO_SEQ(txq->write_ptr));
1486 		out_cmd->hdr.group_id = 0;
1487 
1488 		cmd_pos = sizeof(struct iwl_cmd_header);
1489 		copy_size = sizeof(struct iwl_cmd_header);
1490 	}
1491 
1492 	/* and copy the data that needs to be copied */
1493 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1494 		int copy;
1495 
1496 		if (!cmd->len[i])
1497 			continue;
1498 
1499 		/* copy everything if not nocopy/dup */
1500 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1501 					   IWL_HCMD_DFL_DUP))) {
1502 			copy = cmd->len[i];
1503 
1504 			memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1505 			cmd_pos += copy;
1506 			copy_size += copy;
1507 			continue;
1508 		}
1509 
1510 		/*
1511 		 * Otherwise we need at least IWL_FIRST_TB_SIZE copied
1512 		 * in total (for bi-directional DMA), but copy up to what
1513 		 * we can fit into the payload for debug dump purposes.
1514 		 */
1515 		copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
1516 
1517 		memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1518 		cmd_pos += copy;
1519 
1520 		/* However, treat copy_size the proper way, we need it below */
1521 		if (copy_size < IWL_FIRST_TB_SIZE) {
1522 			copy = IWL_FIRST_TB_SIZE - copy_size;
1523 
1524 			if (copy > cmd->len[i])
1525 				copy = cmd->len[i];
1526 			copy_size += copy;
1527 		}
1528 	}
1529 
1530 	IWL_DEBUG_HC(trans,
1531 		     "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
1532 		     iwl_get_cmd_string(trans, cmd->id),
1533 		     group_id, out_cmd->hdr.cmd,
1534 		     le16_to_cpu(out_cmd->hdr.sequence),
1535 		     cmd_size, txq->write_ptr, idx, trans->conf.cmd_queue);
1536 
1537 	/* start the TFD with the minimum copy bytes */
1538 	tb0_size = min_t(int, copy_size, IWL_FIRST_TB_SIZE);
1539 	memcpy(&txq->first_tb_bufs[idx], &out_cmd->hdr, tb0_size);
1540 	iwl_pcie_txq_build_tfd(trans, txq,
1541 			       iwl_txq_get_first_tb_dma(txq, idx),
1542 			       tb0_size, true);
1543 
1544 	/* map first command fragment, if any remains */
1545 	if (copy_size > tb0_size) {
1546 		phys_addr = dma_map_single(trans->dev,
1547 					   ((u8 *)&out_cmd->hdr) + tb0_size,
1548 					   copy_size - tb0_size,
1549 					   DMA_TO_DEVICE);
1550 		if (dma_mapping_error(trans->dev, phys_addr)) {
1551 			iwl_txq_gen1_tfd_unmap(trans, out_meta, txq,
1552 					       txq->write_ptr);
1553 			idx = -ENOMEM;
1554 			goto out;
1555 		}
1556 
1557 		iwl_pcie_txq_build_tfd(trans, txq, phys_addr,
1558 				       copy_size - tb0_size, false);
1559 	}
1560 
1561 	/* map the remaining (adjusted) nocopy/dup fragments */
1562 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1563 		void *data = (void *)(uintptr_t)cmddata[i];
1564 
1565 		if (!cmdlen[i])
1566 			continue;
1567 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1568 					   IWL_HCMD_DFL_DUP)))
1569 			continue;
1570 		if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
1571 			data = dup_buf;
1572 		phys_addr = dma_map_single(trans->dev, data,
1573 					   cmdlen[i], DMA_TO_DEVICE);
1574 		if (dma_mapping_error(trans->dev, phys_addr)) {
1575 			iwl_txq_gen1_tfd_unmap(trans, out_meta, txq,
1576 					       txq->write_ptr);
1577 			idx = -ENOMEM;
1578 			goto out;
1579 		}
1580 
1581 		iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], false);
1582 	}
1583 
1584 	BUILD_BUG_ON(IWL_TFH_NUM_TBS > sizeof(out_meta->tbs) * BITS_PER_BYTE);
1585 	out_meta->flags = cmd->flags;
1586 	if (WARN_ON_ONCE(txq->entries[idx].free_buf))
1587 		kfree_sensitive(txq->entries[idx].free_buf);
1588 	txq->entries[idx].free_buf = dup_buf;
1589 
1590 	trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
1591 
1592 	/* start timer if queue currently empty */
1593 	if (txq->read_ptr == txq->write_ptr && txq->wd_timeout)
1594 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
1595 
1596 	ret = iwl_pcie_set_cmd_in_flight(trans, cmd);
1597 	if (ret < 0) {
1598 		idx = ret;
1599 		goto out;
1600 	}
1601 
1602 	if (cmd->flags & CMD_BLOCK_TXQS)
1603 		iwl_trans_pcie_block_txq_ptrs(trans, true);
1604 
1605 	/* Increment and update queue's write index */
1606 	txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
1607 	iwl_pcie_txq_inc_wr_ptr(trans, txq);
1608 
1609  out:
1610 	spin_unlock_irqrestore(&txq->lock, flags);
1611  free_dup_buf:
1612 	if (idx < 0)
1613 		kfree(dup_buf);
1614 	return idx;
1615 }
1616 
1617 /*
1618  * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
1619  * @rxb: Rx buffer to reclaim
1620  */
iwl_pcie_hcmd_complete(struct iwl_trans * trans,struct iwl_rx_cmd_buffer * rxb)1621 void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
1622 			    struct iwl_rx_cmd_buffer *rxb)
1623 {
1624 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1625 	u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1626 	u8 group_id;
1627 	u32 cmd_id;
1628 	int txq_id = SEQ_TO_QUEUE(sequence);
1629 	int index = SEQ_TO_INDEX(sequence);
1630 	int cmd_index;
1631 	struct iwl_device_cmd *cmd;
1632 	struct iwl_cmd_meta *meta;
1633 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1634 	struct iwl_txq *txq = trans_pcie->txqs.txq[trans->conf.cmd_queue];
1635 
1636 	/* If a Tx command is being handled and it isn't in the actual
1637 	 * command queue then there a command routing bug has been introduced
1638 	 * in the queue management code. */
1639 	if (IWL_FW_CHECK(trans, txq_id != trans->conf.cmd_queue,
1640 			 "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d pkt=%*phN\n",
1641 			 txq_id, trans->conf.cmd_queue, sequence, txq->read_ptr,
1642 			 txq->write_ptr, 32, pkt))
1643 		return;
1644 
1645 	spin_lock_bh(&txq->lock);
1646 
1647 	cmd_index = iwl_txq_get_cmd_index(txq, index);
1648 	cmd = txq->entries[cmd_index].cmd;
1649 	meta = &txq->entries[cmd_index].meta;
1650 	group_id = cmd->hdr.group_id;
1651 	cmd_id = WIDE_ID(group_id, cmd->hdr.cmd);
1652 
1653 	if (trans->mac_cfg->gen2)
1654 		iwl_txq_gen2_tfd_unmap(trans, meta,
1655 				       iwl_txq_get_tfd(trans, txq, index));
1656 	else
1657 		iwl_txq_gen1_tfd_unmap(trans, meta, txq, index);
1658 
1659 	/* Input error checking is done when commands are added to queue. */
1660 	if (meta->flags & CMD_WANT_SKB) {
1661 		struct page *p = rxb_steal_page(rxb);
1662 
1663 		meta->source->resp_pkt = pkt;
1664 		meta->source->_rx_page_addr = (unsigned long)page_address(p);
1665 		meta->source->_rx_page_order = trans_pcie->rx_page_order;
1666 	}
1667 
1668 	if (meta->flags & CMD_BLOCK_TXQS)
1669 		iwl_trans_pcie_block_txq_ptrs(trans, false);
1670 
1671 	iwl_pcie_cmdq_reclaim(trans, txq_id, index);
1672 
1673 	if (!(meta->flags & CMD_ASYNC)) {
1674 		if (!test_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status)) {
1675 			IWL_WARN(trans,
1676 				 "HCMD_ACTIVE already clear for command %s\n",
1677 				 iwl_get_cmd_string(trans, cmd_id));
1678 		}
1679 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
1680 		IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
1681 			       iwl_get_cmd_string(trans, cmd_id));
1682 		wake_up(&trans_pcie->wait_command_queue);
1683 	}
1684 
1685 	meta->flags = 0;
1686 
1687 	spin_unlock_bh(&txq->lock);
1688 }
1689 
iwl_fill_data_tbs(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_txq * txq,u8 hdr_len,struct iwl_cmd_meta * out_meta)1690 static int iwl_fill_data_tbs(struct iwl_trans *trans, struct sk_buff *skb,
1691 			     struct iwl_txq *txq, u8 hdr_len,
1692 			     struct iwl_cmd_meta *out_meta)
1693 {
1694 	u16 head_tb_len;
1695 	int i;
1696 
1697 	/*
1698 	 * Set up TFD's third entry to point directly to remainder
1699 	 * of skb's head, if any
1700 	 */
1701 	head_tb_len = skb_headlen(skb) - hdr_len;
1702 
1703 	if (head_tb_len > 0) {
1704 		dma_addr_t tb_phys = dma_map_single(trans->dev,
1705 						    skb->data + hdr_len,
1706 						    head_tb_len, DMA_TO_DEVICE);
1707 		if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
1708 			return -EINVAL;
1709 		trace_iwlwifi_dev_tx_tb(trans->dev, skb, skb->data + hdr_len,
1710 					tb_phys, head_tb_len);
1711 		iwl_pcie_txq_build_tfd(trans, txq, tb_phys, head_tb_len, false);
1712 	}
1713 
1714 	/* set up the remaining entries to point to the data */
1715 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1716 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1717 		dma_addr_t tb_phys;
1718 		int tb_idx;
1719 
1720 		if (!skb_frag_size(frag))
1721 			continue;
1722 
1723 		tb_phys = skb_frag_dma_map(trans->dev, frag, 0,
1724 					   skb_frag_size(frag), DMA_TO_DEVICE);
1725 
1726 		if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
1727 			return -EINVAL;
1728 		trace_iwlwifi_dev_tx_tb(trans->dev, skb, skb_frag_address(frag),
1729 					tb_phys, skb_frag_size(frag));
1730 		tb_idx = iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
1731 						skb_frag_size(frag), false);
1732 		if (tb_idx < 0)
1733 			return tb_idx;
1734 
1735 		out_meta->tbs |= BIT(tb_idx);
1736 	}
1737 
1738 	return 0;
1739 }
1740 
1741 #ifdef CONFIG_INET
iwl_pcie_get_page_hdr(struct iwl_trans * trans,size_t len,struct sk_buff * skb)1742 static void *iwl_pcie_get_page_hdr(struct iwl_trans *trans,
1743 				   size_t len, struct sk_buff *skb)
1744 {
1745 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1746 	struct iwl_tso_hdr_page *p = this_cpu_ptr(trans_pcie->txqs.tso_hdr_page);
1747 	struct iwl_tso_page_info *info;
1748 	struct page **page_ptr;
1749 	dma_addr_t phys;
1750 	void *ret;
1751 
1752 	page_ptr = (void *)((u8 *)skb->cb + trans->conf.cb_data_offs);
1753 
1754 	if (WARN_ON(*page_ptr))
1755 		return NULL;
1756 
1757 	if (!p->page)
1758 		goto alloc;
1759 
1760 	/*
1761 	 * Check if there's enough room on this page
1762 	 *
1763 	 * Note that we put a page chaining pointer *last* in the
1764 	 * page - we need it somewhere, and if it's there then we
1765 	 * avoid DMA mapping the last bits of the page which may
1766 	 * trigger the 32-bit boundary hardware bug.
1767 	 *
1768 	 * (see also get_workaround_page() in tx-gen2.c)
1769 	 */
1770 	if (((unsigned long)p->pos & ~PAGE_MASK) + len < IWL_TSO_PAGE_DATA_SIZE) {
1771 		info = IWL_TSO_PAGE_INFO(page_address(p->page));
1772 		goto out;
1773 	}
1774 
1775 	/* We don't have enough room on this page, get a new one. */
1776 	iwl_pcie_free_and_unmap_tso_page(trans, p->page);
1777 
1778 alloc:
1779 	p->page = alloc_page(GFP_ATOMIC);
1780 	if (!p->page)
1781 		return NULL;
1782 	p->pos = page_address(p->page);
1783 
1784 	info = IWL_TSO_PAGE_INFO(page_address(p->page));
1785 
1786 	/* set the chaining pointer to NULL */
1787 	info->next = NULL;
1788 
1789 	/* Create a DMA mapping for the page */
1790 	phys = dma_map_page_attrs(trans->dev, p->page, 0, PAGE_SIZE,
1791 				  DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
1792 	if (unlikely(dma_mapping_error(trans->dev, phys))) {
1793 		__free_page(p->page);
1794 		p->page = NULL;
1795 
1796 		return NULL;
1797 	}
1798 
1799 	/* Store physical address and set use count */
1800 	info->dma_addr = phys;
1801 	refcount_set(&info->use_count, 1);
1802 out:
1803 	*page_ptr = p->page;
1804 	/* Return an internal reference for the caller */
1805 	refcount_inc(&info->use_count);
1806 	ret = p->pos;
1807 	p->pos += len;
1808 
1809 	return ret;
1810 }
1811 
1812 /**
1813  * iwl_pcie_get_sgt_tb_phys - Find TB address in mapped SG list
1814  * @sgt: scatter gather table
1815  * @offset: Offset into the mapped memory (i.e. SKB payload data)
1816  * @len: Length of the area
1817  *
1818  * Find the DMA address that corresponds to the SKB payload data at the
1819  * position given by @offset.
1820  *
1821  * Returns: Address for TB entry
1822  */
iwl_pcie_get_sgt_tb_phys(struct sg_table * sgt,unsigned int offset,unsigned int len)1823 dma_addr_t iwl_pcie_get_sgt_tb_phys(struct sg_table *sgt, unsigned int offset,
1824 				    unsigned int len)
1825 {
1826 	struct scatterlist *sg;
1827 	unsigned int sg_offset = 0;
1828 	int i;
1829 
1830 	/*
1831 	 * Search the mapped DMA areas in the SG for the area that contains the
1832 	 * data at offset with the given length.
1833 	 */
1834 	for_each_sgtable_dma_sg(sgt, sg, i) {
1835 		if (offset >= sg_offset &&
1836 		    offset + len <= sg_offset + sg_dma_len(sg))
1837 			return sg_dma_address(sg) + offset - sg_offset;
1838 
1839 		sg_offset += sg_dma_len(sg);
1840 	}
1841 
1842 	WARN_ON_ONCE(1);
1843 
1844 	return DMA_MAPPING_ERROR;
1845 }
1846 
1847 /**
1848  * iwl_pcie_prep_tso - Prepare TSO page and SKB for sending
1849  * @trans: transport private data
1850  * @skb: the SKB to map
1851  * @cmd_meta: command meta to store the scatter list information for unmapping
1852  * @hdr: output argument for TSO headers
1853  * @hdr_room: requested length for TSO headers
1854  * @offset: offset into the data from which mapping should start
1855  *
1856  * Allocate space for a scatter gather list and TSO headers and map the SKB
1857  * using the scatter gather list. The SKB is unmapped again when the page is
1858  * free'ed again at the end of the operation.
1859  *
1860  * Returns: newly allocated and mapped scatter gather table with list
1861  */
iwl_pcie_prep_tso(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_cmd_meta * cmd_meta,u8 ** hdr,unsigned int hdr_room,unsigned int offset)1862 struct sg_table *iwl_pcie_prep_tso(struct iwl_trans *trans, struct sk_buff *skb,
1863 				   struct iwl_cmd_meta *cmd_meta,
1864 				   u8 **hdr, unsigned int hdr_room,
1865 				   unsigned int offset)
1866 {
1867 	struct sg_table *sgt;
1868 	unsigned int n_segments = skb_shinfo(skb)->nr_frags + 1;
1869 	int orig_nents;
1870 
1871 	if (WARN_ON_ONCE(skb_has_frag_list(skb)))
1872 		return NULL;
1873 
1874 	*hdr = iwl_pcie_get_page_hdr(trans,
1875 				     hdr_room + __alignof__(struct sg_table) +
1876 				     sizeof(struct sg_table) +
1877 				     n_segments * sizeof(struct scatterlist),
1878 				     skb);
1879 	if (!*hdr)
1880 		return NULL;
1881 
1882 	sgt = (void *)PTR_ALIGN(*hdr + hdr_room, __alignof__(struct sg_table));
1883 	sgt->sgl = (void *)(sgt + 1);
1884 
1885 	sg_init_table(sgt->sgl, n_segments);
1886 
1887 	/* Only map the data, not the header (it is copied to the TSO page) */
1888 	orig_nents = skb_to_sgvec(skb, sgt->sgl, offset, skb->len - offset);
1889 	if (WARN_ON_ONCE(orig_nents <= 0))
1890 		return NULL;
1891 
1892 	sgt->orig_nents = orig_nents;
1893 
1894 	/* And map the entire SKB */
1895 	if (dma_map_sgtable(trans->dev, sgt, DMA_TO_DEVICE, 0) < 0)
1896 		return NULL;
1897 
1898 	/* Store non-zero (i.e. valid) offset for unmapping */
1899 	cmd_meta->sg_offset = (unsigned long) sgt & ~PAGE_MASK;
1900 
1901 	return sgt;
1902 }
1903 
iwl_fill_data_tbs_amsdu(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_txq * txq,u8 hdr_len,struct iwl_cmd_meta * out_meta,struct iwl_device_tx_cmd * dev_cmd,u16 tb1_len)1904 static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
1905 				   struct iwl_txq *txq, u8 hdr_len,
1906 				   struct iwl_cmd_meta *out_meta,
1907 				   struct iwl_device_tx_cmd *dev_cmd,
1908 				   u16 tb1_len)
1909 {
1910 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1911 	struct iwl_tx_cmd_v6 *tx_cmd = (void *)dev_cmd->payload;
1912 	struct ieee80211_hdr *hdr = (void *)skb->data;
1913 	unsigned int snap_ip_tcp_hdrlen, ip_hdrlen, total_len, hdr_room;
1914 	unsigned int mss = skb_shinfo(skb)->gso_size;
1915 	unsigned int data_offset = 0;
1916 	u16 length, iv_len, amsdu_pad;
1917 	dma_addr_t start_hdr_phys;
1918 	u8 *start_hdr, *pos_hdr;
1919 	struct sg_table *sgt;
1920 	struct tso_t tso;
1921 
1922 	/* if the packet is protected, then it must be CCMP or GCMP */
1923 	BUILD_BUG_ON(IEEE80211_CCMP_HDR_LEN != IEEE80211_GCMP_HDR_LEN);
1924 	iv_len = ieee80211_has_protected(hdr->frame_control) ?
1925 		IEEE80211_CCMP_HDR_LEN : 0;
1926 
1927 	trace_iwlwifi_dev_tx(trans->dev, skb,
1928 			     iwl_txq_get_tfd(trans, txq, txq->write_ptr),
1929 			     trans_pcie->txqs.tfd.size,
1930 			     &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len, 0);
1931 
1932 	ip_hdrlen = skb_network_header_len(skb);
1933 	snap_ip_tcp_hdrlen = 8 + ip_hdrlen + tcp_hdrlen(skb);
1934 	total_len = skb->len - snap_ip_tcp_hdrlen - hdr_len - iv_len;
1935 	amsdu_pad = 0;
1936 
1937 	/* total amount of header we may need for this A-MSDU */
1938 	hdr_room = DIV_ROUND_UP(total_len, mss) *
1939 		(3 + snap_ip_tcp_hdrlen + sizeof(struct ethhdr)) + iv_len;
1940 
1941 	/* Our device supports 9 segments at most, it will fit in 1 page */
1942 	sgt = iwl_pcie_prep_tso(trans, skb, out_meta, &start_hdr, hdr_room,
1943 				snap_ip_tcp_hdrlen + hdr_len + iv_len);
1944 	if (!sgt)
1945 		return -ENOMEM;
1946 
1947 	start_hdr_phys = iwl_pcie_get_tso_page_phys(start_hdr);
1948 	pos_hdr = start_hdr;
1949 	memcpy(pos_hdr, skb->data + hdr_len, iv_len);
1950 	pos_hdr += iv_len;
1951 
1952 	/*
1953 	 * Pull the ieee80211 header + IV to be able to use TSO core,
1954 	 * we will restore it for the tx_status flow.
1955 	 */
1956 	skb_pull(skb, hdr_len + iv_len);
1957 
1958 	/*
1959 	 * Remove the length of all the headers that we don't actually
1960 	 * have in the MPDU by themselves, but that we duplicate into
1961 	 * all the different MSDUs inside the A-MSDU.
1962 	 */
1963 	le16_add_cpu(&tx_cmd->params.len, -snap_ip_tcp_hdrlen);
1964 
1965 	tso_start(skb, &tso);
1966 
1967 	while (total_len) {
1968 		/* this is the data left for this subframe */
1969 		unsigned int data_left =
1970 			min_t(unsigned int, mss, total_len);
1971 		unsigned int hdr_tb_len;
1972 		dma_addr_t hdr_tb_phys;
1973 		u8 *subf_hdrs_start = pos_hdr;
1974 
1975 		total_len -= data_left;
1976 
1977 		memset(pos_hdr, 0, amsdu_pad);
1978 		pos_hdr += amsdu_pad;
1979 		amsdu_pad = (4 - (sizeof(struct ethhdr) + snap_ip_tcp_hdrlen +
1980 				  data_left)) & 0x3;
1981 		ether_addr_copy(pos_hdr, ieee80211_get_DA(hdr));
1982 		pos_hdr += ETH_ALEN;
1983 		ether_addr_copy(pos_hdr, ieee80211_get_SA(hdr));
1984 		pos_hdr += ETH_ALEN;
1985 
1986 		length = snap_ip_tcp_hdrlen + data_left;
1987 		*((__be16 *)pos_hdr) = cpu_to_be16(length);
1988 		pos_hdr += sizeof(length);
1989 
1990 		/*
1991 		 * This will copy the SNAP as well which will be considered
1992 		 * as MAC header.
1993 		 */
1994 		tso_build_hdr(skb, pos_hdr, &tso, data_left, !total_len);
1995 
1996 		pos_hdr += snap_ip_tcp_hdrlen;
1997 
1998 		hdr_tb_len = pos_hdr - start_hdr;
1999 		hdr_tb_phys = iwl_pcie_get_tso_page_phys(start_hdr);
2000 
2001 		iwl_pcie_txq_build_tfd(trans, txq, hdr_tb_phys,
2002 				       hdr_tb_len, false);
2003 		trace_iwlwifi_dev_tx_tb(trans->dev, skb, start_hdr,
2004 					hdr_tb_phys, hdr_tb_len);
2005 		/* add this subframe's headers' length to the tx_cmd */
2006 		le16_add_cpu(&tx_cmd->params.len, pos_hdr - subf_hdrs_start);
2007 
2008 		/* prepare the start_hdr for the next subframe */
2009 		start_hdr = pos_hdr;
2010 
2011 		/* put the payload */
2012 		while (data_left) {
2013 			unsigned int size = min_t(unsigned int, tso.size,
2014 						  data_left);
2015 			dma_addr_t tb_phys;
2016 
2017 			tb_phys = iwl_pcie_get_sgt_tb_phys(sgt, data_offset, size);
2018 			/* Not a real mapping error, use direct comparison */
2019 			if (unlikely(tb_phys == DMA_MAPPING_ERROR))
2020 				return -EINVAL;
2021 
2022 			iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
2023 					       size, false);
2024 			trace_iwlwifi_dev_tx_tb(trans->dev, skb, tso.data,
2025 						tb_phys, size);
2026 
2027 			data_left -= size;
2028 			data_offset += size;
2029 			tso_build_data(skb, &tso, size);
2030 		}
2031 	}
2032 
2033 	dma_sync_single_for_device(trans->dev, start_hdr_phys, hdr_room,
2034 				   DMA_TO_DEVICE);
2035 
2036 	/* re -add the WiFi header and IV */
2037 	skb_push(skb, hdr_len + iv_len);
2038 
2039 	return 0;
2040 }
2041 #else /* CONFIG_INET */
iwl_fill_data_tbs_amsdu(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_txq * txq,u8 hdr_len,struct iwl_cmd_meta * out_meta,struct iwl_device_tx_cmd * dev_cmd,u16 tb1_len)2042 static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
2043 				   struct iwl_txq *txq, u8 hdr_len,
2044 				   struct iwl_cmd_meta *out_meta,
2045 				   struct iwl_device_tx_cmd *dev_cmd,
2046 				   u16 tb1_len)
2047 {
2048 	/* No A-MSDU without CONFIG_INET */
2049 	WARN_ON(1);
2050 
2051 	return -1;
2052 }
2053 #endif /* CONFIG_INET */
2054 
2055 #define IWL_TX_CRC_SIZE 4
2056 #define IWL_TX_DELIMITER_SIZE 4
2057 
2058 /*
2059  * iwl_txq_gen1_update_byte_cnt_tbl - Set up entry in Tx byte-count array
2060  */
iwl_txq_gen1_update_byte_cnt_tbl(struct iwl_trans * trans,struct iwl_txq * txq,u16 byte_cnt,int num_tbs)2061 static void iwl_txq_gen1_update_byte_cnt_tbl(struct iwl_trans *trans,
2062 					     struct iwl_txq *txq, u16 byte_cnt,
2063 					     int num_tbs)
2064 {
2065 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2066 	struct iwl_bc_tbl_entry *scd_bc_tbl;
2067 	int write_ptr = txq->write_ptr;
2068 	int txq_id = txq->id;
2069 	u8 sec_ctl = 0;
2070 	u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
2071 	__le16 bc_ent;
2072 	struct iwl_device_tx_cmd *dev_cmd = txq->entries[txq->write_ptr].cmd;
2073 	struct iwl_tx_cmd_v6 *tx_cmd = (void *)dev_cmd->payload;
2074 	u8 sta_id = tx_cmd->params.sta_id;
2075 
2076 	scd_bc_tbl = trans_pcie->txqs.scd_bc_tbls.addr;
2077 
2078 	sec_ctl = tx_cmd->params.sec_ctl;
2079 
2080 	switch (sec_ctl & TX_CMD_SEC_MSK) {
2081 	case TX_CMD_SEC_CCM:
2082 		len += IEEE80211_CCMP_MIC_LEN;
2083 		break;
2084 	case TX_CMD_SEC_TKIP:
2085 		len += IEEE80211_TKIP_ICV_LEN;
2086 		break;
2087 	case TX_CMD_SEC_WEP:
2088 		len += IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN;
2089 		break;
2090 	}
2091 
2092 	if (trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_7000 &&
2093 	    trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
2094 		len = DIV_ROUND_UP(len, 4);
2095 
2096 	if (WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX))
2097 		return;
2098 
2099 	bc_ent = cpu_to_le16(len | (sta_id << 12));
2100 
2101 	scd_bc_tbl[txq_id * TFD_QUEUE_BC_SIZE + write_ptr].tfd_offset = bc_ent;
2102 
2103 	if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
2104 		scd_bc_tbl[txq_id * TFD_QUEUE_BC_SIZE + TFD_QUEUE_SIZE_MAX + write_ptr].tfd_offset =
2105 			bc_ent;
2106 }
2107 
iwl_trans_pcie_tx(struct iwl_trans * trans,struct sk_buff * skb,struct iwl_device_tx_cmd * dev_cmd,int txq_id)2108 int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
2109 		      struct iwl_device_tx_cmd *dev_cmd, int txq_id)
2110 {
2111 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2112 	struct ieee80211_hdr *hdr;
2113 	struct iwl_tx_cmd_v6 *tx_cmd = (struct iwl_tx_cmd_v6 *)dev_cmd->payload;
2114 	struct iwl_cmd_meta *out_meta;
2115 	struct iwl_txq *txq;
2116 	dma_addr_t tb0_phys, tb1_phys, scratch_phys;
2117 	void *tb1_addr;
2118 	void *tfd;
2119 	u16 len, tb1_len;
2120 	bool wait_write_ptr;
2121 	__le16 fc;
2122 	u8 hdr_len;
2123 	u16 wifi_seq;
2124 	bool amsdu;
2125 
2126 	txq = trans_pcie->txqs.txq[txq_id];
2127 
2128 	if (WARN_ONCE(!test_bit(txq_id, trans_pcie->txqs.queue_used),
2129 		      "TX on unused queue %d\n", txq_id))
2130 		return -EINVAL;
2131 
2132 	if (skb_is_nonlinear(skb) &&
2133 	    skb_shinfo(skb)->nr_frags > IWL_TRANS_PCIE_MAX_FRAGS(trans_pcie) &&
2134 	    __skb_linearize(skb))
2135 		return -ENOMEM;
2136 
2137 	/* mac80211 always puts the full header into the SKB's head,
2138 	 * so there's no need to check if it's readable there
2139 	 */
2140 	hdr = (struct ieee80211_hdr *)skb->data;
2141 	fc = hdr->frame_control;
2142 	hdr_len = ieee80211_hdrlen(fc);
2143 
2144 	spin_lock(&txq->lock);
2145 
2146 	if (iwl_txq_space(trans, txq) < txq->high_mark) {
2147 		iwl_txq_stop(trans, txq);
2148 
2149 		/* don't put the packet on the ring, if there is no room */
2150 		if (unlikely(iwl_txq_space(trans, txq) < 3)) {
2151 			struct iwl_device_tx_cmd **dev_cmd_ptr;
2152 
2153 			dev_cmd_ptr = (void *)((u8 *)skb->cb +
2154 					       trans->conf.cb_data_offs +
2155 					       sizeof(void *));
2156 
2157 			*dev_cmd_ptr = dev_cmd;
2158 			__skb_queue_tail(&txq->overflow_q, skb);
2159 
2160 			spin_unlock(&txq->lock);
2161 			return 0;
2162 		}
2163 	}
2164 
2165 	/* In AGG mode, the index in the ring must correspond to the WiFi
2166 	 * sequence number. This is a HW requirements to help the SCD to parse
2167 	 * the BA.
2168 	 * Check here that the packets are in the right place on the ring.
2169 	 */
2170 	wifi_seq = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
2171 	WARN_ONCE(txq->ampdu &&
2172 		  (wifi_seq & 0xff) != txq->write_ptr,
2173 		  "Q: %d WiFi Seq %d tfdNum %d",
2174 		  txq_id, wifi_seq, txq->write_ptr);
2175 
2176 	/* Set up driver data for this TFD */
2177 	txq->entries[txq->write_ptr].skb = skb;
2178 	txq->entries[txq->write_ptr].cmd = dev_cmd;
2179 
2180 	dev_cmd->hdr.sequence =
2181 		cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2182 			    INDEX_TO_SEQ(txq->write_ptr)));
2183 
2184 	tb0_phys = iwl_txq_get_first_tb_dma(txq, txq->write_ptr);
2185 	scratch_phys = tb0_phys + sizeof(struct iwl_cmd_header) +
2186 		       offsetof(struct iwl_tx_cmd_v6_params, scratch);
2187 
2188 	tx_cmd->params.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2189 	tx_cmd->params.dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
2190 
2191 	/* Set up first empty entry in queue's array of Tx/cmd buffers */
2192 	out_meta = &txq->entries[txq->write_ptr].meta;
2193 	memset(out_meta, 0, sizeof(*out_meta));
2194 
2195 	/*
2196 	 * The second TB (tb1) points to the remainder of the TX command
2197 	 * and the 802.11 header - dword aligned size
2198 	 * (This calculation modifies the TX command, so do it before the
2199 	 * setup of the first TB)
2200 	 */
2201 	len = sizeof(struct iwl_tx_cmd_v6) + sizeof(struct iwl_cmd_header) +
2202 	      hdr_len - IWL_FIRST_TB_SIZE;
2203 	/* do not align A-MSDU to dword as the subframe header aligns it */
2204 	amsdu = ieee80211_is_data_qos(fc) &&
2205 		(*ieee80211_get_qos_ctl(hdr) &
2206 		 IEEE80211_QOS_CTL_A_MSDU_PRESENT);
2207 	if (!amsdu) {
2208 		tb1_len = ALIGN(len, 4);
2209 		/* Tell NIC about any 2-byte padding after MAC header */
2210 		if (tb1_len != len)
2211 			tx_cmd->params.tx_flags |= cpu_to_le32(TX_CMD_FLG_MH_PAD);
2212 	} else {
2213 		tb1_len = len;
2214 	}
2215 
2216 	/*
2217 	 * The first TB points to bi-directional DMA data, we'll
2218 	 * memcpy the data into it later.
2219 	 */
2220 	iwl_pcie_txq_build_tfd(trans, txq, tb0_phys,
2221 			       IWL_FIRST_TB_SIZE, true);
2222 
2223 	/* there must be data left over for TB1 or this code must be changed */
2224 	BUILD_BUG_ON(sizeof(struct iwl_tx_cmd_v6) < IWL_FIRST_TB_SIZE);
2225 	BUILD_BUG_ON(sizeof(struct iwl_cmd_header) +
2226 		     offsetofend(struct iwl_tx_cmd_v6_params, scratch) >
2227 		     IWL_FIRST_TB_SIZE);
2228 
2229 	/* map the data for TB1 */
2230 	tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE;
2231 	tb1_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
2232 	if (unlikely(dma_mapping_error(trans->dev, tb1_phys)))
2233 		goto out_err;
2234 	iwl_pcie_txq_build_tfd(trans, txq, tb1_phys, tb1_len, false);
2235 
2236 	trace_iwlwifi_dev_tx(trans->dev, skb,
2237 			     iwl_txq_get_tfd(trans, txq, txq->write_ptr),
2238 			     trans_pcie->txqs.tfd.size,
2239 			     &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len,
2240 			     hdr_len);
2241 
2242 	/*
2243 	 * If gso_size wasn't set, don't give the frame "amsdu treatment"
2244 	 * (adding subframes, etc.).
2245 	 * This can happen in some testing flows when the amsdu was already
2246 	 * pre-built, and we just need to send the resulting skb.
2247 	 */
2248 	if (amsdu && skb_shinfo(skb)->gso_size) {
2249 		if (unlikely(iwl_fill_data_tbs_amsdu(trans, skb, txq, hdr_len,
2250 						     out_meta, dev_cmd,
2251 						     tb1_len)))
2252 			goto out_err;
2253 	} else {
2254 		struct sk_buff *frag;
2255 
2256 		if (unlikely(iwl_fill_data_tbs(trans, skb, txq, hdr_len,
2257 					       out_meta)))
2258 			goto out_err;
2259 
2260 		skb_walk_frags(skb, frag) {
2261 			if (unlikely(iwl_fill_data_tbs(trans, frag, txq, 0,
2262 						       out_meta)))
2263 				goto out_err;
2264 		}
2265 	}
2266 
2267 	/* building the A-MSDU might have changed this data, so memcpy it now */
2268 	memcpy(&txq->first_tb_bufs[txq->write_ptr], dev_cmd, IWL_FIRST_TB_SIZE);
2269 
2270 	tfd = iwl_txq_get_tfd(trans, txq, txq->write_ptr);
2271 	/* Set up entry for this TFD in Tx byte-count array */
2272 	iwl_txq_gen1_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->params.len),
2273 					 iwl_txq_gen1_tfd_get_num_tbs(tfd));
2274 
2275 	wait_write_ptr = ieee80211_has_morefrags(fc);
2276 
2277 	/* start timer if queue currently empty */
2278 	if (txq->read_ptr == txq->write_ptr && txq->wd_timeout) {
2279 		/*
2280 		 * If the TXQ is active, then set the timer, if not,
2281 		 * set the timer in remainder so that the timer will
2282 		 * be armed with the right value when the station will
2283 		 * wake up.
2284 		 */
2285 		if (!txq->frozen)
2286 			mod_timer(&txq->stuck_timer,
2287 				  jiffies + txq->wd_timeout);
2288 		else
2289 			txq->frozen_expiry_remainder = txq->wd_timeout;
2290 	}
2291 
2292 	/* Tell device the write index *just past* this latest filled TFD */
2293 	txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
2294 	if (!wait_write_ptr)
2295 		iwl_pcie_txq_inc_wr_ptr(trans, txq);
2296 
2297 	/*
2298 	 * At this point the frame is "transmitted" successfully
2299 	 * and we will get a TX status notification eventually.
2300 	 */
2301 	spin_unlock(&txq->lock);
2302 	return 0;
2303 out_err:
2304 	iwl_txq_gen1_tfd_unmap(trans, out_meta, txq, txq->write_ptr);
2305 	spin_unlock(&txq->lock);
2306 	return -1;
2307 }
2308 
iwl_txq_gen1_inval_byte_cnt_tbl(struct iwl_trans * trans,struct iwl_txq * txq,int read_ptr)2309 static void iwl_txq_gen1_inval_byte_cnt_tbl(struct iwl_trans *trans,
2310 					    struct iwl_txq *txq,
2311 					    int read_ptr)
2312 {
2313 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2314 	struct iwl_bc_tbl_entry *scd_bc_tbl = trans_pcie->txqs.scd_bc_tbls.addr;
2315 	int txq_id = txq->id;
2316 	u8 sta_id = 0;
2317 	__le16 bc_ent;
2318 	struct iwl_device_tx_cmd *dev_cmd = txq->entries[read_ptr].cmd;
2319 	struct iwl_tx_cmd_v6 *tx_cmd = (void *)dev_cmd->payload;
2320 
2321 	WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
2322 
2323 	if (txq_id != trans->conf.cmd_queue)
2324 		sta_id = tx_cmd->params.sta_id;
2325 
2326 	bc_ent = cpu_to_le16(1 | (sta_id << 12));
2327 
2328 	scd_bc_tbl[txq_id * TFD_QUEUE_BC_SIZE + read_ptr].tfd_offset = bc_ent;
2329 
2330 	if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
2331 		scd_bc_tbl[txq_id * TFD_QUEUE_BC_SIZE + TFD_QUEUE_SIZE_MAX + read_ptr].tfd_offset =
2332 			bc_ent;
2333 }
2334 
2335 /* Frees buffers until index _not_ inclusive */
iwl_pcie_reclaim(struct iwl_trans * trans,int txq_id,int ssn,struct sk_buff_head * skbs,bool is_flush)2336 void iwl_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
2337 		      struct sk_buff_head *skbs, bool is_flush)
2338 {
2339 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2340 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
2341 	int tfd_num, read_ptr, last_to_free;
2342 	int txq_read_ptr, txq_write_ptr;
2343 
2344 	/* This function is not meant to release cmd queue*/
2345 	if (WARN_ON(txq_id == trans->conf.cmd_queue))
2346 		return;
2347 
2348 	if (WARN_ON(!txq))
2349 		return;
2350 
2351 	tfd_num = iwl_txq_get_cmd_index(txq, ssn);
2352 
2353 	spin_lock_bh(&txq->reclaim_lock);
2354 
2355 	spin_lock(&txq->lock);
2356 	txq_read_ptr = txq->read_ptr;
2357 	txq_write_ptr = txq->write_ptr;
2358 	spin_unlock(&txq->lock);
2359 
2360 	/* There is nothing to do if we are flushing an empty queue */
2361 	if (is_flush && txq_write_ptr == txq_read_ptr)
2362 		goto out;
2363 
2364 	read_ptr = iwl_txq_get_cmd_index(txq, txq_read_ptr);
2365 
2366 	if (!test_bit(txq_id, trans_pcie->txqs.queue_used)) {
2367 		IWL_DEBUG_TX_QUEUES(trans, "Q %d inactive - ignoring idx %d\n",
2368 				    txq_id, ssn);
2369 		goto out;
2370 	}
2371 
2372 	if (read_ptr == tfd_num)
2373 		goto out;
2374 
2375 	IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d (%d) -> %d (%d)\n",
2376 			   txq_id, read_ptr, txq_read_ptr, tfd_num, ssn);
2377 
2378 	/* Since we free until index _not_ inclusive, the one before index is
2379 	 * the last we will free. This one must be used
2380 	 */
2381 	last_to_free = iwl_txq_dec_wrap(trans, tfd_num);
2382 
2383 	if (!iwl_txq_used(txq, last_to_free, txq_read_ptr, txq_write_ptr)) {
2384 		IWL_ERR(trans,
2385 			"%s: Read index for txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
2386 			__func__, txq_id, last_to_free,
2387 			trans->mac_cfg->base->max_tfd_queue_size,
2388 			txq_write_ptr, txq_read_ptr);
2389 
2390 		iwl_op_mode_time_point(trans->op_mode,
2391 				       IWL_FW_INI_TIME_POINT_FAKE_TX,
2392 				       NULL);
2393 		goto out;
2394 	}
2395 
2396 	if (WARN_ON(!skb_queue_empty(skbs)))
2397 		goto out;
2398 
2399 	for (;
2400 	     read_ptr != tfd_num;
2401 	     txq_read_ptr = iwl_txq_inc_wrap(trans, txq_read_ptr),
2402 	     read_ptr = iwl_txq_get_cmd_index(txq, txq_read_ptr)) {
2403 		struct iwl_cmd_meta *cmd_meta = &txq->entries[read_ptr].meta;
2404 		struct sk_buff *skb = txq->entries[read_ptr].skb;
2405 
2406 		if (WARN_ONCE(!skb, "no SKB at %d (%d) on queue %d\n",
2407 			      read_ptr, txq_read_ptr, txq_id))
2408 			continue;
2409 
2410 		iwl_pcie_free_tso_pages(trans, skb, cmd_meta);
2411 
2412 		__skb_queue_tail(skbs, skb);
2413 
2414 		txq->entries[read_ptr].skb = NULL;
2415 
2416 		if (!trans->mac_cfg->gen2)
2417 			iwl_txq_gen1_inval_byte_cnt_tbl(trans, txq,
2418 							txq_read_ptr);
2419 
2420 		iwl_txq_free_tfd(trans, txq, txq_read_ptr);
2421 	}
2422 
2423 	spin_lock(&txq->lock);
2424 	txq->read_ptr = txq_read_ptr;
2425 
2426 	iwl_txq_progress(txq);
2427 
2428 	if (iwl_txq_space(trans, txq) > txq->low_mark &&
2429 	    test_bit(txq_id, trans_pcie->txqs.queue_stopped)) {
2430 		struct sk_buff_head overflow_skbs;
2431 		struct sk_buff *skb;
2432 
2433 		__skb_queue_head_init(&overflow_skbs);
2434 		skb_queue_splice_init(&txq->overflow_q,
2435 				      is_flush ? skbs : &overflow_skbs);
2436 
2437 		/*
2438 		 * We are going to transmit from the overflow queue.
2439 		 * Remember this state so that wait_for_txq_empty will know we
2440 		 * are adding more packets to the TFD queue. It cannot rely on
2441 		 * the state of &txq->overflow_q, as we just emptied it, but
2442 		 * haven't TXed the content yet.
2443 		 */
2444 		txq->overflow_tx = true;
2445 
2446 		/*
2447 		 * This is tricky: we are in reclaim path and are holding
2448 		 * reclaim_lock, so noone will try to access the txq data
2449 		 * from that path. We stopped tx, so we can't have tx as well.
2450 		 * Bottom line, we can unlock and re-lock later.
2451 		 */
2452 		spin_unlock(&txq->lock);
2453 
2454 		while ((skb = __skb_dequeue(&overflow_skbs))) {
2455 			struct iwl_device_tx_cmd *dev_cmd_ptr;
2456 
2457 			dev_cmd_ptr = *(void **)((u8 *)skb->cb +
2458 						 trans->conf.cb_data_offs +
2459 						 sizeof(void *));
2460 
2461 			/*
2462 			 * Note that we can very well be overflowing again.
2463 			 * In that case, iwl_txq_space will be small again
2464 			 * and we won't wake mac80211's queue.
2465 			 */
2466 			iwl_trans_tx(trans, skb, dev_cmd_ptr, txq_id);
2467 		}
2468 
2469 		if (iwl_txq_space(trans, txq) > txq->low_mark)
2470 			iwl_trans_pcie_wake_queue(trans, txq);
2471 
2472 		spin_lock(&txq->lock);
2473 		txq->overflow_tx = false;
2474 	}
2475 
2476 	spin_unlock(&txq->lock);
2477 out:
2478 	spin_unlock_bh(&txq->reclaim_lock);
2479 }
2480 
2481 /* Set wr_ptr of specific device and txq  */
iwl_pcie_set_q_ptrs(struct iwl_trans * trans,int txq_id,int ptr)2482 void iwl_pcie_set_q_ptrs(struct iwl_trans *trans, int txq_id, int ptr)
2483 {
2484 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2485 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
2486 
2487 	spin_lock_bh(&txq->lock);
2488 
2489 	txq->write_ptr = ptr;
2490 	txq->read_ptr = txq->write_ptr;
2491 
2492 	spin_unlock_bh(&txq->lock);
2493 }
2494 
iwl_pcie_freeze_txq_timer(struct iwl_trans * trans,unsigned long txqs,bool freeze)2495 void iwl_pcie_freeze_txq_timer(struct iwl_trans *trans,
2496 			       unsigned long txqs, bool freeze)
2497 {
2498 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2499 	int queue;
2500 
2501 	for_each_set_bit(queue, &txqs, BITS_PER_LONG) {
2502 		struct iwl_txq *txq = trans_pcie->txqs.txq[queue];
2503 		unsigned long now;
2504 
2505 		spin_lock_bh(&txq->lock);
2506 
2507 		now = jiffies;
2508 
2509 		if (txq->frozen == freeze)
2510 			goto next_queue;
2511 
2512 		IWL_DEBUG_TX_QUEUES(trans, "%s TXQ %d\n",
2513 				    freeze ? "Freezing" : "Waking", queue);
2514 
2515 		txq->frozen = freeze;
2516 
2517 		if (txq->read_ptr == txq->write_ptr)
2518 			goto next_queue;
2519 
2520 		if (freeze) {
2521 			if (unlikely(time_after(now,
2522 						txq->stuck_timer.expires))) {
2523 				/*
2524 				 * The timer should have fired, maybe it is
2525 				 * spinning right now on the lock.
2526 				 */
2527 				goto next_queue;
2528 			}
2529 			/* remember how long until the timer fires */
2530 			txq->frozen_expiry_remainder =
2531 				txq->stuck_timer.expires - now;
2532 			timer_delete(&txq->stuck_timer);
2533 			goto next_queue;
2534 		}
2535 
2536 		/*
2537 		 * Wake a non-empty queue -> arm timer with the
2538 		 * remainder before it froze
2539 		 */
2540 		mod_timer(&txq->stuck_timer,
2541 			  now + txq->frozen_expiry_remainder);
2542 
2543 next_queue:
2544 		spin_unlock_bh(&txq->lock);
2545 	}
2546 }
2547 
2548 #define HOST_COMPLETE_TIMEOUT	(2 * HZ)
2549 
iwl_trans_pcie_send_hcmd_sync(struct iwl_trans * trans,struct iwl_host_cmd * cmd,const char * cmd_str)2550 static int iwl_trans_pcie_send_hcmd_sync(struct iwl_trans *trans,
2551 					 struct iwl_host_cmd *cmd,
2552 					 const char *cmd_str)
2553 {
2554 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
2555 	struct iwl_txq *txq = trans_pcie->txqs.txq[trans->conf.cmd_queue];
2556 	int cmd_idx;
2557 	int ret;
2558 
2559 	IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n", cmd_str);
2560 
2561 	if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE,
2562 				  &trans->status),
2563 		 "Command %s: a command is already active!\n", cmd_str))
2564 		return -EIO;
2565 
2566 	IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n", cmd_str);
2567 
2568 	if (trans->mac_cfg->gen2)
2569 		cmd_idx = iwl_pcie_gen2_enqueue_hcmd(trans, cmd);
2570 	else
2571 		cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
2572 
2573 	if (cmd_idx < 0) {
2574 		ret = cmd_idx;
2575 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
2576 		IWL_ERR(trans, "Error sending %s: enqueue_hcmd failed: %d\n",
2577 			cmd_str, ret);
2578 		return ret;
2579 	}
2580 
2581 	ret = wait_event_timeout(trans_pcie->wait_command_queue,
2582 				 !test_bit(STATUS_SYNC_HCMD_ACTIVE,
2583 					   &trans->status),
2584 				 HOST_COMPLETE_TIMEOUT);
2585 	if (!ret) {
2586 		IWL_ERR(trans, "Error sending %s: time out after %dms.\n",
2587 			cmd_str, jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
2588 
2589 		IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n",
2590 			txq->read_ptr, txq->write_ptr);
2591 
2592 		clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
2593 		IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
2594 			       cmd_str);
2595 		ret = -ETIMEDOUT;
2596 
2597 		iwl_trans_pcie_sync_nmi(trans);
2598 		goto cancel;
2599 	}
2600 
2601 	if (test_bit(STATUS_FW_ERROR, &trans->status)) {
2602 		if (trans->suppress_cmd_error_once) {
2603 			trans->suppress_cmd_error_once = false;
2604 		} else {
2605 			IWL_ERR(trans, "FW error in SYNC CMD %s\n", cmd_str);
2606 			dump_stack();
2607 		}
2608 		ret = -EIO;
2609 		goto cancel;
2610 	}
2611 
2612 	if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
2613 	    test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
2614 		IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
2615 		ret = -ERFKILL;
2616 		goto cancel;
2617 	}
2618 
2619 	if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
2620 		IWL_ERR(trans, "Error: Response NULL in '%s'\n", cmd_str);
2621 		ret = -EIO;
2622 		goto cancel;
2623 	}
2624 
2625 	return 0;
2626 
2627 cancel:
2628 	if (cmd->flags & CMD_WANT_SKB) {
2629 		/*
2630 		 * Cancel the CMD_WANT_SKB flag for the cmd in the
2631 		 * TX cmd queue. Otherwise in case the cmd comes
2632 		 * in later, it will possibly set an invalid
2633 		 * address (cmd->meta.source).
2634 		 */
2635 		txq->entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
2636 	}
2637 
2638 	if (cmd->resp_pkt) {
2639 		iwl_free_resp(cmd);
2640 		cmd->resp_pkt = NULL;
2641 	}
2642 
2643 	return ret;
2644 }
2645 
iwl_trans_pcie_send_hcmd(struct iwl_trans * trans,struct iwl_host_cmd * cmd)2646 int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans,
2647 			     struct iwl_host_cmd *cmd)
2648 {
2649 	const char *cmd_str = iwl_get_cmd_string(trans, cmd->id);
2650 
2651 	/* Make sure the NIC is still alive in the bus */
2652 	if (test_bit(STATUS_TRANS_DEAD, &trans->status))
2653 		return -ENODEV;
2654 
2655 	if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
2656 	    test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
2657 		IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
2658 				  cmd->id);
2659 		return -ERFKILL;
2660 	}
2661 
2662 	if (cmd->flags & CMD_ASYNC) {
2663 		int ret;
2664 
2665 		IWL_DEBUG_INFO(trans, "Sending async command %s\n", cmd_str);
2666 
2667 		/* An asynchronous command can not expect an SKB to be set. */
2668 		if (WARN_ON(cmd->flags & CMD_WANT_SKB))
2669 			return -EINVAL;
2670 
2671 		if (trans->mac_cfg->gen2)
2672 			ret = iwl_pcie_gen2_enqueue_hcmd(trans, cmd);
2673 		else
2674 			ret = iwl_pcie_enqueue_hcmd(trans, cmd);
2675 
2676 		if (ret < 0) {
2677 			IWL_ERR(trans,
2678 				"Error sending %s: enqueue_hcmd failed: %d\n",
2679 				iwl_get_cmd_string(trans, cmd->id), ret);
2680 			return ret;
2681 		}
2682 		return 0;
2683 	}
2684 
2685 	return iwl_trans_pcie_send_hcmd_sync(trans, cmd, cmd_str);
2686 }
2687