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