xref: /linux/drivers/net/wireless/intel/iwlwifi/pcie/tx-gen2.c (revision ee057c8c194b9283f4137b253b70e292693a39f0)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2017 Intel Deutschland GmbH
4  * Copyright (C) 2018-2020, 2023-2024 Intel Corporation
5  */
6 #include <net/tso.h>
7 #include <linux/tcp.h>
8 
9 #include "iwl-debug.h"
10 #include "iwl-csr.h"
11 #include "iwl-io.h"
12 #include "internal.h"
13 #include "fw/api/tx.h"
14 #include "fw/api/commands.h"
15 #include "fw/api/datapath.h"
16 #include "iwl-scd.h"
17 
18 static struct page *get_workaround_page(struct iwl_trans *trans,
19 					struct sk_buff *skb)
20 {
21 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
22 	struct iwl_tso_page_info *info;
23 	struct page **page_ptr;
24 	struct page *ret;
25 	dma_addr_t phys;
26 
27 	page_ptr = (void *)((u8 *)skb->cb + trans_pcie->txqs.page_offs);
28 
29 	ret = alloc_page(GFP_ATOMIC);
30 	if (!ret)
31 		return NULL;
32 
33 	info = IWL_TSO_PAGE_INFO(page_address(ret));
34 
35 	/* Create a DMA mapping for the page */
36 	phys = dma_map_page_attrs(trans->dev, ret, 0, PAGE_SIZE,
37 				  DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
38 	if (unlikely(dma_mapping_error(trans->dev, phys))) {
39 		__free_page(ret);
40 		return NULL;
41 	}
42 
43 	/* Store physical address and set use count */
44 	info->dma_addr = phys;
45 	refcount_set(&info->use_count, 1);
46 
47 	/* set the chaining pointer to the previous page if there */
48 	info->next = *page_ptr;
49 	*page_ptr = ret;
50 
51 	return ret;
52 }
53 
54 /*
55  * Add a TB and if needed apply the FH HW bug workaround;
56  * meta != NULL indicates that it's a page mapping and we
57  * need to dma_unmap_page() and set the meta->tbs bit in
58  * this case.
59  */
60 static int iwl_txq_gen2_set_tb_with_wa(struct iwl_trans *trans,
61 				       struct sk_buff *skb,
62 				       struct iwl_tfh_tfd *tfd,
63 				       dma_addr_t phys, void *virt,
64 				       u16 len, struct iwl_cmd_meta *meta,
65 				       bool unmap)
66 {
67 	dma_addr_t oldphys = phys;
68 	struct page *page;
69 	int ret;
70 
71 	if (unlikely(dma_mapping_error(trans->dev, phys)))
72 		return -ENOMEM;
73 
74 	if (likely(!iwl_txq_crosses_4g_boundary(phys, len))) {
75 		ret = iwl_txq_gen2_set_tb(trans, tfd, phys, len);
76 
77 		if (ret < 0)
78 			goto unmap;
79 
80 		if (meta)
81 			meta->tbs |= BIT(ret);
82 
83 		ret = 0;
84 		goto trace;
85 	}
86 
87 	/*
88 	 * Work around a hardware bug. If (as expressed in the
89 	 * condition above) the TB ends on a 32-bit boundary,
90 	 * then the next TB may be accessed with the wrong
91 	 * address.
92 	 * To work around it, copy the data elsewhere and make
93 	 * a new mapping for it so the device will not fail.
94 	 */
95 
96 	if (WARN_ON(len > IWL_TSO_PAGE_DATA_SIZE)) {
97 		ret = -ENOBUFS;
98 		goto unmap;
99 	}
100 
101 	page = get_workaround_page(trans, skb);
102 	if (!page) {
103 		ret = -ENOMEM;
104 		goto unmap;
105 	}
106 
107 	memcpy(page_address(page), virt, len);
108 
109 	/*
110 	 * This is a bit odd, but performance does not matter here, what
111 	 * matters are the expectations of the calling code and TB cleanup
112 	 * function.
113 	 *
114 	 * As such, if unmap is set, then create another mapping for the TB
115 	 * entry as it will be unmapped later. On the other hand, if it is not
116 	 * set, then the TB entry will not be unmapped and instead we simply
117 	 * reference and sync the mapping that get_workaround_page() created.
118 	 */
119 	if (unmap) {
120 		phys = dma_map_single(trans->dev, page_address(page), len,
121 				      DMA_TO_DEVICE);
122 		if (unlikely(dma_mapping_error(trans->dev, phys)))
123 			return -ENOMEM;
124 	} else {
125 		phys = iwl_pcie_get_tso_page_phys(page_address(page));
126 		dma_sync_single_for_device(trans->dev, phys, len,
127 					   DMA_TO_DEVICE);
128 	}
129 
130 	ret = iwl_txq_gen2_set_tb(trans, tfd, phys, len);
131 	if (ret < 0) {
132 		/* unmap the new allocation as single */
133 		oldphys = phys;
134 		meta = NULL;
135 		goto unmap;
136 	}
137 
138 	IWL_DEBUG_TX(trans,
139 		     "TB bug workaround: copied %d bytes from 0x%llx to 0x%llx\n",
140 		     len, (unsigned long long)oldphys,
141 		     (unsigned long long)phys);
142 
143 	ret = 0;
144 unmap:
145 	if (!unmap)
146 		goto trace;
147 
148 	if (meta)
149 		dma_unmap_page(trans->dev, oldphys, len, DMA_TO_DEVICE);
150 	else
151 		dma_unmap_single(trans->dev, oldphys, len, DMA_TO_DEVICE);
152 trace:
153 	trace_iwlwifi_dev_tx_tb(trans->dev, skb, virt, phys, len);
154 
155 	return ret;
156 }
157 
158 static int iwl_txq_gen2_build_amsdu(struct iwl_trans *trans,
159 				    struct sk_buff *skb,
160 				    struct iwl_tfh_tfd *tfd,
161 				    struct iwl_cmd_meta *out_meta,
162 				    int start_len,
163 				    u8 hdr_len,
164 				    struct iwl_device_tx_cmd *dev_cmd)
165 {
166 #ifdef CONFIG_INET
167 	struct iwl_tx_cmd_gen2 *tx_cmd = (void *)dev_cmd->payload;
168 	struct ieee80211_hdr *hdr = (void *)skb->data;
169 	unsigned int snap_ip_tcp_hdrlen, ip_hdrlen, total_len, hdr_room;
170 	unsigned int mss = skb_shinfo(skb)->gso_size;
171 	dma_addr_t start_hdr_phys;
172 	u16 length, amsdu_pad;
173 	u8 *start_hdr;
174 	struct sg_table *sgt;
175 	struct tso_t tso;
176 
177 	trace_iwlwifi_dev_tx(trans->dev, skb, tfd, sizeof(*tfd),
178 			     &dev_cmd->hdr, start_len, 0);
179 
180 	ip_hdrlen = skb_network_header_len(skb);
181 	snap_ip_tcp_hdrlen = 8 + ip_hdrlen + tcp_hdrlen(skb);
182 	total_len = skb->len - snap_ip_tcp_hdrlen - hdr_len;
183 	amsdu_pad = 0;
184 
185 	/* total amount of header we may need for this A-MSDU */
186 	hdr_room = DIV_ROUND_UP(total_len, mss) *
187 		(3 + snap_ip_tcp_hdrlen + sizeof(struct ethhdr));
188 
189 	/* Our device supports 9 segments at most, it will fit in 1 page */
190 	sgt = iwl_pcie_prep_tso(trans, skb, out_meta, &start_hdr, hdr_room);
191 	if (!sgt)
192 		return -ENOMEM;
193 
194 	start_hdr_phys = iwl_pcie_get_tso_page_phys(start_hdr);
195 
196 	/*
197 	 * Pull the ieee80211 header to be able to use TSO core,
198 	 * we will restore it for the tx_status flow.
199 	 */
200 	skb_pull(skb, hdr_len);
201 
202 	/*
203 	 * Remove the length of all the headers that we don't actually
204 	 * have in the MPDU by themselves, but that we duplicate into
205 	 * all the different MSDUs inside the A-MSDU.
206 	 */
207 	le16_add_cpu(&tx_cmd->len, -snap_ip_tcp_hdrlen);
208 
209 	tso_start(skb, &tso);
210 
211 	while (total_len) {
212 		/* this is the data left for this subframe */
213 		unsigned int data_left = min_t(unsigned int, mss, total_len);
214 		unsigned int tb_len;
215 		dma_addr_t tb_phys;
216 		u8 *pos_hdr = start_hdr;
217 
218 		total_len -= data_left;
219 
220 		memset(pos_hdr, 0, amsdu_pad);
221 		pos_hdr += amsdu_pad;
222 		amsdu_pad = (4 - (sizeof(struct ethhdr) + snap_ip_tcp_hdrlen +
223 				  data_left)) & 0x3;
224 		ether_addr_copy(pos_hdr, ieee80211_get_DA(hdr));
225 		pos_hdr += ETH_ALEN;
226 		ether_addr_copy(pos_hdr, ieee80211_get_SA(hdr));
227 		pos_hdr += ETH_ALEN;
228 
229 		length = snap_ip_tcp_hdrlen + data_left;
230 		*((__be16 *)pos_hdr) = cpu_to_be16(length);
231 		pos_hdr += sizeof(length);
232 
233 		/*
234 		 * This will copy the SNAP as well which will be considered
235 		 * as MAC header.
236 		 */
237 		tso_build_hdr(skb, pos_hdr, &tso, data_left, !total_len);
238 
239 		pos_hdr += snap_ip_tcp_hdrlen;
240 
241 		tb_len = pos_hdr - start_hdr;
242 		tb_phys = iwl_pcie_get_tso_page_phys(start_hdr);
243 
244 		/*
245 		 * No need for _with_wa, this is from the TSO page and
246 		 * we leave some space at the end of it so can't hit
247 		 * the buggy scenario.
248 		 */
249 		iwl_txq_gen2_set_tb(trans, tfd, tb_phys, tb_len);
250 		trace_iwlwifi_dev_tx_tb(trans->dev, skb, start_hdr,
251 					tb_phys, tb_len);
252 		/* add this subframe's headers' length to the tx_cmd */
253 		le16_add_cpu(&tx_cmd->len, tb_len);
254 
255 		/* prepare the start_hdr for the next subframe */
256 		start_hdr = pos_hdr;
257 
258 		/* put the payload */
259 		while (data_left) {
260 			int ret;
261 
262 			tb_len = min_t(unsigned int, tso.size, data_left);
263 			tb_phys = iwl_pcie_get_sgt_tb_phys(sgt, tso.data);
264 			/* Not a real mapping error, use direct comparison */
265 			if (unlikely(tb_phys == DMA_MAPPING_ERROR))
266 				goto out_err;
267 
268 			ret = iwl_txq_gen2_set_tb_with_wa(trans, skb, tfd,
269 							  tb_phys, tso.data,
270 							  tb_len, NULL, false);
271 			if (ret)
272 				goto out_err;
273 
274 			data_left -= tb_len;
275 			tso_build_data(skb, &tso, tb_len);
276 		}
277 	}
278 
279 	dma_sync_single_for_device(trans->dev, start_hdr_phys, hdr_room,
280 				   DMA_TO_DEVICE);
281 
282 	/* re -add the WiFi header */
283 	skb_push(skb, hdr_len);
284 
285 	return 0;
286 
287 out_err:
288 #endif
289 	return -EINVAL;
290 }
291 
292 static struct
293 iwl_tfh_tfd *iwl_txq_gen2_build_tx_amsdu(struct iwl_trans *trans,
294 					 struct iwl_txq *txq,
295 					 struct iwl_device_tx_cmd *dev_cmd,
296 					 struct sk_buff *skb,
297 					 struct iwl_cmd_meta *out_meta,
298 					 int hdr_len,
299 					 int tx_cmd_len)
300 {
301 	int idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
302 	struct iwl_tfh_tfd *tfd = iwl_txq_get_tfd(trans, txq, idx);
303 	dma_addr_t tb_phys;
304 	int len;
305 	void *tb1_addr;
306 
307 	tb_phys = iwl_txq_get_first_tb_dma(txq, idx);
308 
309 	/*
310 	 * No need for _with_wa, the first TB allocation is aligned up
311 	 * to a 64-byte boundary and thus can't be at the end or cross
312 	 * a page boundary (much less a 2^32 boundary).
313 	 */
314 	iwl_txq_gen2_set_tb(trans, tfd, tb_phys, IWL_FIRST_TB_SIZE);
315 
316 	/*
317 	 * The second TB (tb1) points to the remainder of the TX command
318 	 * and the 802.11 header - dword aligned size
319 	 * (This calculation modifies the TX command, so do it before the
320 	 * setup of the first TB)
321 	 */
322 	len = tx_cmd_len + sizeof(struct iwl_cmd_header) + hdr_len -
323 	      IWL_FIRST_TB_SIZE;
324 
325 	/* do not align A-MSDU to dword as the subframe header aligns it */
326 
327 	/* map the data for TB1 */
328 	tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE;
329 	tb_phys = dma_map_single(trans->dev, tb1_addr, len, DMA_TO_DEVICE);
330 	if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
331 		goto out_err;
332 	/*
333 	 * No need for _with_wa(), we ensure (via alignment) that the data
334 	 * here can never cross or end at a page boundary.
335 	 */
336 	iwl_txq_gen2_set_tb(trans, tfd, tb_phys, len);
337 
338 	if (iwl_txq_gen2_build_amsdu(trans, skb, tfd, out_meta,
339 				     len + IWL_FIRST_TB_SIZE, hdr_len, dev_cmd))
340 		goto out_err;
341 
342 	/* building the A-MSDU might have changed this data, memcpy it now */
343 	memcpy(&txq->first_tb_bufs[idx], dev_cmd, IWL_FIRST_TB_SIZE);
344 	return tfd;
345 
346 out_err:
347 	iwl_txq_gen2_tfd_unmap(trans, out_meta, tfd);
348 	return NULL;
349 }
350 
351 static int iwl_txq_gen2_tx_add_frags(struct iwl_trans *trans,
352 				     struct sk_buff *skb,
353 				     struct iwl_tfh_tfd *tfd,
354 				     struct iwl_cmd_meta *out_meta)
355 {
356 	int i;
357 
358 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
359 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
360 		dma_addr_t tb_phys;
361 		unsigned int fragsz = skb_frag_size(frag);
362 		int ret;
363 
364 		if (!fragsz)
365 			continue;
366 
367 		tb_phys = skb_frag_dma_map(trans->dev, frag, 0,
368 					   fragsz, DMA_TO_DEVICE);
369 		ret = iwl_txq_gen2_set_tb_with_wa(trans, skb, tfd, tb_phys,
370 						  skb_frag_address(frag),
371 						  fragsz, out_meta, true);
372 		if (ret)
373 			return ret;
374 	}
375 
376 	return 0;
377 }
378 
379 static struct
380 iwl_tfh_tfd *iwl_txq_gen2_build_tx(struct iwl_trans *trans,
381 				   struct iwl_txq *txq,
382 				   struct iwl_device_tx_cmd *dev_cmd,
383 				   struct sk_buff *skb,
384 				   struct iwl_cmd_meta *out_meta,
385 				   int hdr_len,
386 				   int tx_cmd_len,
387 				   bool pad)
388 {
389 	int idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
390 	struct iwl_tfh_tfd *tfd = iwl_txq_get_tfd(trans, txq, idx);
391 	dma_addr_t tb_phys;
392 	int len, tb1_len, tb2_len;
393 	void *tb1_addr;
394 	struct sk_buff *frag;
395 
396 	tb_phys = iwl_txq_get_first_tb_dma(txq, idx);
397 
398 	/* The first TB points to bi-directional DMA data */
399 	memcpy(&txq->first_tb_bufs[idx], dev_cmd, IWL_FIRST_TB_SIZE);
400 
401 	/*
402 	 * No need for _with_wa, the first TB allocation is aligned up
403 	 * to a 64-byte boundary and thus can't be at the end or cross
404 	 * a page boundary (much less a 2^32 boundary).
405 	 */
406 	iwl_txq_gen2_set_tb(trans, tfd, tb_phys, IWL_FIRST_TB_SIZE);
407 
408 	/*
409 	 * The second TB (tb1) points to the remainder of the TX command
410 	 * and the 802.11 header - dword aligned size
411 	 * (This calculation modifies the TX command, so do it before the
412 	 * setup of the first TB)
413 	 */
414 	len = tx_cmd_len + sizeof(struct iwl_cmd_header) + hdr_len -
415 	      IWL_FIRST_TB_SIZE;
416 
417 	if (pad)
418 		tb1_len = ALIGN(len, 4);
419 	else
420 		tb1_len = len;
421 
422 	/* map the data for TB1 */
423 	tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE;
424 	tb_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
425 	if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
426 		goto out_err;
427 	/*
428 	 * No need for _with_wa(), we ensure (via alignment) that the data
429 	 * here can never cross or end at a page boundary.
430 	 */
431 	iwl_txq_gen2_set_tb(trans, tfd, tb_phys, tb1_len);
432 	trace_iwlwifi_dev_tx(trans->dev, skb, tfd, sizeof(*tfd), &dev_cmd->hdr,
433 			     IWL_FIRST_TB_SIZE + tb1_len, hdr_len);
434 
435 	/* set up TFD's third entry to point to remainder of skb's head */
436 	tb2_len = skb_headlen(skb) - hdr_len;
437 
438 	if (tb2_len > 0) {
439 		int ret;
440 
441 		tb_phys = dma_map_single(trans->dev, skb->data + hdr_len,
442 					 tb2_len, DMA_TO_DEVICE);
443 		ret = iwl_txq_gen2_set_tb_with_wa(trans, skb, tfd, tb_phys,
444 						  skb->data + hdr_len, tb2_len,
445 						  NULL, true);
446 		if (ret)
447 			goto out_err;
448 	}
449 
450 	if (iwl_txq_gen2_tx_add_frags(trans, skb, tfd, out_meta))
451 		goto out_err;
452 
453 	skb_walk_frags(skb, frag) {
454 		int ret;
455 
456 		tb_phys = dma_map_single(trans->dev, frag->data,
457 					 skb_headlen(frag), DMA_TO_DEVICE);
458 		ret = iwl_txq_gen2_set_tb_with_wa(trans, skb, tfd, tb_phys,
459 						  frag->data,
460 						  skb_headlen(frag), NULL,
461 						  true);
462 		if (ret)
463 			goto out_err;
464 		if (iwl_txq_gen2_tx_add_frags(trans, frag, tfd, out_meta))
465 			goto out_err;
466 	}
467 
468 	return tfd;
469 
470 out_err:
471 	iwl_txq_gen2_tfd_unmap(trans, out_meta, tfd);
472 	return NULL;
473 }
474 
475 static
476 struct iwl_tfh_tfd *iwl_txq_gen2_build_tfd(struct iwl_trans *trans,
477 					   struct iwl_txq *txq,
478 					   struct iwl_device_tx_cmd *dev_cmd,
479 					   struct sk_buff *skb,
480 					   struct iwl_cmd_meta *out_meta)
481 {
482 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
483 	int idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
484 	struct iwl_tfh_tfd *tfd = iwl_txq_get_tfd(trans, txq, idx);
485 	int len, hdr_len;
486 	bool amsdu;
487 
488 	/* There must be data left over for TB1 or this code must be changed */
489 	BUILD_BUG_ON(sizeof(struct iwl_tx_cmd_gen2) < IWL_FIRST_TB_SIZE);
490 	BUILD_BUG_ON(sizeof(struct iwl_cmd_header) +
491 		     offsetofend(struct iwl_tx_cmd_gen2, dram_info) >
492 		     IWL_FIRST_TB_SIZE);
493 	BUILD_BUG_ON(sizeof(struct iwl_tx_cmd_gen3) < IWL_FIRST_TB_SIZE);
494 	BUILD_BUG_ON(sizeof(struct iwl_cmd_header) +
495 		     offsetofend(struct iwl_tx_cmd_gen3, dram_info) >
496 		     IWL_FIRST_TB_SIZE);
497 
498 	memset(tfd, 0, sizeof(*tfd));
499 
500 	if (trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_AX210)
501 		len = sizeof(struct iwl_tx_cmd_gen2);
502 	else
503 		len = sizeof(struct iwl_tx_cmd_gen3);
504 
505 	amsdu = ieee80211_is_data_qos(hdr->frame_control) &&
506 			(*ieee80211_get_qos_ctl(hdr) &
507 			 IEEE80211_QOS_CTL_A_MSDU_PRESENT);
508 
509 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
510 
511 	/*
512 	 * Only build A-MSDUs here if doing so by GSO, otherwise it may be
513 	 * an A-MSDU for other reasons, e.g. NAN or an A-MSDU having been
514 	 * built in the higher layers already.
515 	 */
516 	if (amsdu && skb_shinfo(skb)->gso_size)
517 		return iwl_txq_gen2_build_tx_amsdu(trans, txq, dev_cmd, skb,
518 						    out_meta, hdr_len, len);
519 	return iwl_txq_gen2_build_tx(trans, txq, dev_cmd, skb, out_meta,
520 				      hdr_len, len, !amsdu);
521 }
522 
523 int iwl_txq_space(struct iwl_trans *trans, const struct iwl_txq *q)
524 {
525 	unsigned int max;
526 	unsigned int used;
527 
528 	/*
529 	 * To avoid ambiguity between empty and completely full queues, there
530 	 * should always be less than max_tfd_queue_size elements in the queue.
531 	 * If q->n_window is smaller than max_tfd_queue_size, there is no need
532 	 * to reserve any queue entries for this purpose.
533 	 */
534 	if (q->n_window < trans->trans_cfg->base_params->max_tfd_queue_size)
535 		max = q->n_window;
536 	else
537 		max = trans->trans_cfg->base_params->max_tfd_queue_size - 1;
538 
539 	/*
540 	 * max_tfd_queue_size is a power of 2, so the following is equivalent to
541 	 * modulo by max_tfd_queue_size and is well defined.
542 	 */
543 	used = (q->write_ptr - q->read_ptr) &
544 		(trans->trans_cfg->base_params->max_tfd_queue_size - 1);
545 
546 	if (WARN_ON(used > max))
547 		return 0;
548 
549 	return max - used;
550 }
551 
552 /*
553  * iwl_pcie_gen2_update_byte_tbl - Set up entry in Tx byte-count array
554  */
555 static void iwl_pcie_gen2_update_byte_tbl(struct iwl_trans *trans,
556 					  struct iwl_txq *txq, u16 byte_cnt,
557 					  int num_tbs)
558 {
559 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
560 	int idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
561 	u8 filled_tfd_size, num_fetch_chunks;
562 	u16 len = byte_cnt;
563 	__le16 bc_ent;
564 
565 	if (WARN(idx >= txq->n_window, "%d >= %d\n", idx, txq->n_window))
566 		return;
567 
568 	filled_tfd_size = offsetof(struct iwl_tfh_tfd, tbs) +
569 			  num_tbs * sizeof(struct iwl_tfh_tb);
570 	/*
571 	 * filled_tfd_size contains the number of filled bytes in the TFD.
572 	 * Dividing it by 64 will give the number of chunks to fetch
573 	 * to SRAM- 0 for one chunk, 1 for 2 and so on.
574 	 * If, for example, TFD contains only 3 TBs then 32 bytes
575 	 * of the TFD are used, and only one chunk of 64 bytes should
576 	 * be fetched
577 	 */
578 	num_fetch_chunks = DIV_ROUND_UP(filled_tfd_size, 64) - 1;
579 
580 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
581 		struct iwl_gen3_bc_tbl_entry *scd_bc_tbl_gen3 = txq->bc_tbl.addr;
582 
583 		/* Starting from AX210, the HW expects bytes */
584 		WARN_ON(trans_pcie->txqs.bc_table_dword);
585 		WARN_ON(len > 0x3FFF);
586 		bc_ent = cpu_to_le16(len | (num_fetch_chunks << 14));
587 		scd_bc_tbl_gen3[idx].tfd_offset = bc_ent;
588 	} else {
589 		struct iwlagn_scd_bc_tbl *scd_bc_tbl = txq->bc_tbl.addr;
590 
591 		/* Before AX210, the HW expects DW */
592 		WARN_ON(!trans_pcie->txqs.bc_table_dword);
593 		len = DIV_ROUND_UP(len, 4);
594 		WARN_ON(len > 0xFFF);
595 		bc_ent = cpu_to_le16(len | (num_fetch_chunks << 12));
596 		scd_bc_tbl->tfd_offset[idx] = bc_ent;
597 	}
598 }
599 
600 static u8 iwl_txq_gen2_get_num_tbs(struct iwl_tfh_tfd *tfd)
601 {
602 	return le16_to_cpu(tfd->num_tbs) & 0x1f;
603 }
604 
605 int iwl_txq_gen2_set_tb(struct iwl_trans *trans, struct iwl_tfh_tfd *tfd,
606 			dma_addr_t addr, u16 len)
607 {
608 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
609 	int idx = iwl_txq_gen2_get_num_tbs(tfd);
610 	struct iwl_tfh_tb *tb;
611 
612 	/* Only WARN here so we know about the issue, but we mess up our
613 	 * unmap path because not every place currently checks for errors
614 	 * returned from this function - it can only return an error if
615 	 * there's no more space, and so when we know there is enough we
616 	 * don't always check ...
617 	 */
618 	WARN(iwl_txq_crosses_4g_boundary(addr, len),
619 	     "possible DMA problem with iova:0x%llx, len:%d\n",
620 	     (unsigned long long)addr, len);
621 
622 	if (WARN_ON(idx >= IWL_TFH_NUM_TBS))
623 		return -EINVAL;
624 	tb = &tfd->tbs[idx];
625 
626 	/* Each TFD can point to a maximum max_tbs Tx buffers */
627 	if (le16_to_cpu(tfd->num_tbs) >= trans_pcie->txqs.tfd.max_tbs) {
628 		IWL_ERR(trans, "Error can not send more than %d chunks\n",
629 			trans_pcie->txqs.tfd.max_tbs);
630 		return -EINVAL;
631 	}
632 
633 	put_unaligned_le64(addr, &tb->addr);
634 	tb->tb_len = cpu_to_le16(len);
635 
636 	tfd->num_tbs = cpu_to_le16(idx + 1);
637 
638 	return idx;
639 }
640 
641 void iwl_txq_gen2_tfd_unmap(struct iwl_trans *trans,
642 			    struct iwl_cmd_meta *meta,
643 			    struct iwl_tfh_tfd *tfd)
644 {
645 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
646 	int i, num_tbs;
647 
648 	/* Sanity check on number of chunks */
649 	num_tbs = iwl_txq_gen2_get_num_tbs(tfd);
650 
651 	if (num_tbs > trans_pcie->txqs.tfd.max_tbs) {
652 		IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
653 		return;
654 	}
655 
656 	/* TB1 is mapped directly, the rest is the TSO page and SG list. */
657 	if (meta->sg_offset)
658 		num_tbs = 2;
659 
660 	/* first TB is never freed - it's the bidirectional DMA data */
661 	for (i = 1; i < num_tbs; i++) {
662 		if (meta->tbs & BIT(i))
663 			dma_unmap_page(trans->dev,
664 				       le64_to_cpu(tfd->tbs[i].addr),
665 				       le16_to_cpu(tfd->tbs[i].tb_len),
666 				       DMA_TO_DEVICE);
667 		else
668 			dma_unmap_single(trans->dev,
669 					 le64_to_cpu(tfd->tbs[i].addr),
670 					 le16_to_cpu(tfd->tbs[i].tb_len),
671 					 DMA_TO_DEVICE);
672 	}
673 
674 	iwl_txq_set_tfd_invalid_gen2(trans, tfd);
675 }
676 
677 static void iwl_txq_gen2_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
678 {
679 	/* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and
680 	 * idx is bounded by n_window
681 	 */
682 	int idx = iwl_txq_get_cmd_index(txq, txq->read_ptr);
683 	struct sk_buff *skb;
684 
685 	lockdep_assert_held(&txq->lock);
686 
687 	if (!txq->entries)
688 		return;
689 
690 	iwl_txq_gen2_tfd_unmap(trans, &txq->entries[idx].meta,
691 			       iwl_txq_get_tfd(trans, txq, idx));
692 
693 	skb = txq->entries[idx].skb;
694 
695 	/* Can be called from irqs-disabled context
696 	 * If skb is not NULL, it means that the whole queue is being
697 	 * freed and that the queue is not empty - free the skb
698 	 */
699 	if (skb) {
700 		iwl_op_mode_free_skb(trans->op_mode, skb);
701 		txq->entries[idx].skb = NULL;
702 	}
703 }
704 
705 /*
706  * iwl_txq_inc_wr_ptr - Send new write index to hardware
707  */
708 static void iwl_txq_inc_wr_ptr(struct iwl_trans *trans, struct iwl_txq *txq)
709 {
710 	lockdep_assert_held(&txq->lock);
711 
712 	IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq->id, txq->write_ptr);
713 
714 	/*
715 	 * if not in power-save mode, uCode will never sleep when we're
716 	 * trying to tx (during RFKILL, we're not trying to tx).
717 	 */
718 	iwl_write32(trans, HBUS_TARG_WRPTR, txq->write_ptr | (txq->id << 16));
719 }
720 
721 int iwl_txq_gen2_tx(struct iwl_trans *trans, struct sk_buff *skb,
722 		    struct iwl_device_tx_cmd *dev_cmd, int txq_id)
723 {
724 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
725 	struct iwl_cmd_meta *out_meta;
726 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
727 	u16 cmd_len;
728 	int idx;
729 	void *tfd;
730 
731 	if (WARN_ONCE(txq_id >= IWL_MAX_TVQM_QUEUES,
732 		      "queue %d out of range", txq_id))
733 		return -EINVAL;
734 
735 	if (WARN_ONCE(!test_bit(txq_id, trans_pcie->txqs.queue_used),
736 		      "TX on unused queue %d\n", txq_id))
737 		return -EINVAL;
738 
739 	if (skb_is_nonlinear(skb) &&
740 	    skb_shinfo(skb)->nr_frags > IWL_TRANS_PCIE_MAX_FRAGS(trans_pcie) &&
741 	    __skb_linearize(skb))
742 		return -ENOMEM;
743 
744 	spin_lock(&txq->lock);
745 
746 	if (iwl_txq_space(trans, txq) < txq->high_mark) {
747 		iwl_txq_stop(trans, txq);
748 
749 		/* don't put the packet on the ring, if there is no room */
750 		if (unlikely(iwl_txq_space(trans, txq) < 3)) {
751 			struct iwl_device_tx_cmd **dev_cmd_ptr;
752 
753 			dev_cmd_ptr = (void *)((u8 *)skb->cb +
754 					       trans_pcie->txqs.dev_cmd_offs);
755 
756 			*dev_cmd_ptr = dev_cmd;
757 			__skb_queue_tail(&txq->overflow_q, skb);
758 			spin_unlock(&txq->lock);
759 			return 0;
760 		}
761 	}
762 
763 	idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
764 
765 	/* Set up driver data for this TFD */
766 	txq->entries[idx].skb = skb;
767 	txq->entries[idx].cmd = dev_cmd;
768 
769 	dev_cmd->hdr.sequence =
770 		cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
771 			    INDEX_TO_SEQ(idx)));
772 
773 	/* Set up first empty entry in queue's array of Tx/cmd buffers */
774 	out_meta = &txq->entries[idx].meta;
775 	memset(out_meta, 0, sizeof(*out_meta));
776 
777 	tfd = iwl_txq_gen2_build_tfd(trans, txq, dev_cmd, skb, out_meta);
778 	if (!tfd) {
779 		spin_unlock(&txq->lock);
780 		return -1;
781 	}
782 
783 	if (trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
784 		struct iwl_tx_cmd_gen3 *tx_cmd_gen3 =
785 			(void *)dev_cmd->payload;
786 
787 		cmd_len = le16_to_cpu(tx_cmd_gen3->len);
788 	} else {
789 		struct iwl_tx_cmd_gen2 *tx_cmd_gen2 =
790 			(void *)dev_cmd->payload;
791 
792 		cmd_len = le16_to_cpu(tx_cmd_gen2->len);
793 	}
794 
795 	/* Set up entry for this TFD in Tx byte-count array */
796 	iwl_pcie_gen2_update_byte_tbl(trans, txq, cmd_len,
797 				      iwl_txq_gen2_get_num_tbs(tfd));
798 
799 	/* start timer if queue currently empty */
800 	if (txq->read_ptr == txq->write_ptr && txq->wd_timeout)
801 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
802 
803 	/* Tell device the write index *just past* this latest filled TFD */
804 	txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
805 	iwl_txq_inc_wr_ptr(trans, txq);
806 	/*
807 	 * At this point the frame is "transmitted" successfully
808 	 * and we will get a TX status notification eventually.
809 	 */
810 	spin_unlock(&txq->lock);
811 	return 0;
812 }
813 
814 /*************** HOST COMMAND QUEUE FUNCTIONS   *****/
815 
816 /*
817  * iwl_txq_gen2_unmap -  Unmap any remaining DMA mappings and free skb's
818  */
819 static void iwl_txq_gen2_unmap(struct iwl_trans *trans, int txq_id)
820 {
821 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
822 	struct iwl_txq *txq = trans_pcie->txqs.txq[txq_id];
823 
824 	spin_lock_bh(&txq->reclaim_lock);
825 	spin_lock(&txq->lock);
826 	while (txq->write_ptr != txq->read_ptr) {
827 		IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
828 				   txq_id, txq->read_ptr);
829 
830 		if (txq_id != trans_pcie->txqs.cmd.q_id) {
831 			int idx = iwl_txq_get_cmd_index(txq, txq->read_ptr);
832 			struct iwl_cmd_meta *cmd_meta = &txq->entries[idx].meta;
833 			struct sk_buff *skb = txq->entries[idx].skb;
834 
835 			if (!WARN_ON_ONCE(!skb))
836 				iwl_pcie_free_tso_pages(trans, skb, cmd_meta);
837 		}
838 		iwl_txq_gen2_free_tfd(trans, txq);
839 		txq->read_ptr = iwl_txq_inc_wrap(trans, txq->read_ptr);
840 	}
841 
842 	while (!skb_queue_empty(&txq->overflow_q)) {
843 		struct sk_buff *skb = __skb_dequeue(&txq->overflow_q);
844 
845 		iwl_op_mode_free_skb(trans->op_mode, skb);
846 	}
847 
848 	spin_unlock(&txq->lock);
849 	spin_unlock_bh(&txq->reclaim_lock);
850 
851 	/* just in case - this queue may have been stopped */
852 	iwl_trans_pcie_wake_queue(trans, txq);
853 }
854 
855 static void iwl_txq_gen2_free_memory(struct iwl_trans *trans,
856 				     struct iwl_txq *txq)
857 {
858 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
859 	struct device *dev = trans->dev;
860 
861 	/* De-alloc circular buffer of TFDs */
862 	if (txq->tfds) {
863 		dma_free_coherent(dev,
864 				  trans_pcie->txqs.tfd.size * txq->n_window,
865 				  txq->tfds, txq->dma_addr);
866 		dma_free_coherent(dev,
867 				  sizeof(*txq->first_tb_bufs) * txq->n_window,
868 				  txq->first_tb_bufs, txq->first_tb_dma);
869 	}
870 
871 	kfree(txq->entries);
872 	if (txq->bc_tbl.addr)
873 		dma_pool_free(trans_pcie->txqs.bc_pool,
874 			      txq->bc_tbl.addr, txq->bc_tbl.dma);
875 	kfree(txq);
876 }
877 
878 /*
879  * iwl_pcie_txq_free - Deallocate DMA queue.
880  * @txq: Transmit queue to deallocate.
881  *
882  * Empty queue by removing and destroying all BD's.
883  * Free all buffers.
884  * 0-fill, but do not free "txq" descriptor structure.
885  */
886 static void iwl_txq_gen2_free(struct iwl_trans *trans, int txq_id)
887 {
888 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
889 	struct iwl_txq *txq;
890 	int i;
891 
892 	if (WARN_ONCE(txq_id >= IWL_MAX_TVQM_QUEUES,
893 		      "queue %d out of range", txq_id))
894 		return;
895 
896 	txq = trans_pcie->txqs.txq[txq_id];
897 
898 	if (WARN_ON(!txq))
899 		return;
900 
901 	iwl_txq_gen2_unmap(trans, txq_id);
902 
903 	/* De-alloc array of command/tx buffers */
904 	if (txq_id == trans_pcie->txqs.cmd.q_id)
905 		for (i = 0; i < txq->n_window; i++) {
906 			kfree_sensitive(txq->entries[i].cmd);
907 			kfree_sensitive(txq->entries[i].free_buf);
908 		}
909 	del_timer_sync(&txq->stuck_timer);
910 
911 	iwl_txq_gen2_free_memory(trans, txq);
912 
913 	trans_pcie->txqs.txq[txq_id] = NULL;
914 
915 	clear_bit(txq_id, trans_pcie->txqs.queue_used);
916 }
917 
918 static struct iwl_txq *
919 iwl_txq_dyn_alloc_dma(struct iwl_trans *trans, int size, unsigned int timeout)
920 {
921 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
922 	size_t bc_tbl_size, bc_tbl_entries;
923 	struct iwl_txq *txq;
924 	int ret;
925 
926 	WARN_ON(!trans_pcie->txqs.bc_tbl_size);
927 
928 	bc_tbl_size = trans_pcie->txqs.bc_tbl_size;
929 	bc_tbl_entries = bc_tbl_size / sizeof(u16);
930 
931 	if (WARN_ON(size > bc_tbl_entries))
932 		return ERR_PTR(-EINVAL);
933 
934 	txq = kzalloc(sizeof(*txq), GFP_KERNEL);
935 	if (!txq)
936 		return ERR_PTR(-ENOMEM);
937 
938 	txq->bc_tbl.addr = dma_pool_alloc(trans_pcie->txqs.bc_pool, GFP_KERNEL,
939 					  &txq->bc_tbl.dma);
940 	if (!txq->bc_tbl.addr) {
941 		IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
942 		kfree(txq);
943 		return ERR_PTR(-ENOMEM);
944 	}
945 
946 	ret = iwl_pcie_txq_alloc(trans, txq, size, false);
947 	if (ret) {
948 		IWL_ERR(trans, "Tx queue alloc failed\n");
949 		goto error;
950 	}
951 	ret = iwl_txq_init(trans, txq, size, false);
952 	if (ret) {
953 		IWL_ERR(trans, "Tx queue init failed\n");
954 		goto error;
955 	}
956 
957 	txq->wd_timeout = msecs_to_jiffies(timeout);
958 
959 	return txq;
960 
961 error:
962 	iwl_txq_gen2_free_memory(trans, txq);
963 	return ERR_PTR(ret);
964 }
965 
966 static int iwl_pcie_txq_alloc_response(struct iwl_trans *trans,
967 				       struct iwl_txq *txq,
968 				       struct iwl_host_cmd *hcmd)
969 {
970 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
971 	struct iwl_tx_queue_cfg_rsp *rsp;
972 	int ret, qid;
973 	u32 wr_ptr;
974 
975 	if (WARN_ON(iwl_rx_packet_payload_len(hcmd->resp_pkt) !=
976 		    sizeof(*rsp))) {
977 		ret = -EINVAL;
978 		goto error_free_resp;
979 	}
980 
981 	rsp = (void *)hcmd->resp_pkt->data;
982 	qid = le16_to_cpu(rsp->queue_number);
983 	wr_ptr = le16_to_cpu(rsp->write_pointer);
984 
985 	if (qid >= ARRAY_SIZE(trans_pcie->txqs.txq)) {
986 		WARN_ONCE(1, "queue index %d unsupported", qid);
987 		ret = -EIO;
988 		goto error_free_resp;
989 	}
990 
991 	if (test_and_set_bit(qid, trans_pcie->txqs.queue_used)) {
992 		WARN_ONCE(1, "queue %d already used", qid);
993 		ret = -EIO;
994 		goto error_free_resp;
995 	}
996 
997 	if (WARN_ONCE(trans_pcie->txqs.txq[qid],
998 		      "queue %d already allocated\n", qid)) {
999 		ret = -EIO;
1000 		goto error_free_resp;
1001 	}
1002 
1003 	txq->id = qid;
1004 	trans_pcie->txqs.txq[qid] = txq;
1005 	wr_ptr &= (trans->trans_cfg->base_params->max_tfd_queue_size - 1);
1006 
1007 	/* Place first TFD at index corresponding to start sequence number */
1008 	txq->read_ptr = wr_ptr;
1009 	txq->write_ptr = wr_ptr;
1010 
1011 	IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d\n", qid);
1012 
1013 	iwl_free_resp(hcmd);
1014 	return qid;
1015 
1016 error_free_resp:
1017 	iwl_free_resp(hcmd);
1018 	iwl_txq_gen2_free_memory(trans, txq);
1019 	return ret;
1020 }
1021 
1022 int iwl_txq_dyn_alloc(struct iwl_trans *trans, u32 flags, u32 sta_mask,
1023 		      u8 tid, int size, unsigned int timeout)
1024 {
1025 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1026 	struct iwl_txq *txq;
1027 	union {
1028 		struct iwl_tx_queue_cfg_cmd old;
1029 		struct iwl_scd_queue_cfg_cmd new;
1030 	} cmd;
1031 	struct iwl_host_cmd hcmd = {
1032 		.flags = CMD_WANT_SKB,
1033 	};
1034 	int ret;
1035 
1036 	/* take the min with bytecount table entries allowed */
1037 	size = min_t(u32, size, trans_pcie->txqs.bc_tbl_size / sizeof(u16));
1038 	/* but must be power of 2 values for calculating read/write pointers */
1039 	size = rounddown_pow_of_two(size);
1040 
1041 	if (trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_BZ &&
1042 	    trans->hw_rev_step == SILICON_A_STEP) {
1043 		size = 4096;
1044 		txq = iwl_txq_dyn_alloc_dma(trans, size, timeout);
1045 	} else {
1046 		do {
1047 			txq = iwl_txq_dyn_alloc_dma(trans, size, timeout);
1048 			if (!IS_ERR(txq))
1049 				break;
1050 
1051 			IWL_DEBUG_TX_QUEUES(trans,
1052 					    "Failed allocating TXQ of size %d for sta mask %x tid %d, ret: %ld\n",
1053 					    size, sta_mask, tid,
1054 					    PTR_ERR(txq));
1055 			size /= 2;
1056 		} while (size >= 16);
1057 	}
1058 
1059 	if (IS_ERR(txq))
1060 		return PTR_ERR(txq);
1061 
1062 	if (trans_pcie->txqs.queue_alloc_cmd_ver == 0) {
1063 		memset(&cmd.old, 0, sizeof(cmd.old));
1064 		cmd.old.tfdq_addr = cpu_to_le64(txq->dma_addr);
1065 		cmd.old.byte_cnt_addr = cpu_to_le64(txq->bc_tbl.dma);
1066 		cmd.old.cb_size = cpu_to_le32(TFD_QUEUE_CB_SIZE(size));
1067 		cmd.old.flags = cpu_to_le16(flags | TX_QUEUE_CFG_ENABLE_QUEUE);
1068 		cmd.old.tid = tid;
1069 
1070 		if (hweight32(sta_mask) != 1) {
1071 			ret = -EINVAL;
1072 			goto error;
1073 		}
1074 		cmd.old.sta_id = ffs(sta_mask) - 1;
1075 
1076 		hcmd.id = SCD_QUEUE_CFG;
1077 		hcmd.len[0] = sizeof(cmd.old);
1078 		hcmd.data[0] = &cmd.old;
1079 	} else if (trans_pcie->txqs.queue_alloc_cmd_ver == 3) {
1080 		memset(&cmd.new, 0, sizeof(cmd.new));
1081 		cmd.new.operation = cpu_to_le32(IWL_SCD_QUEUE_ADD);
1082 		cmd.new.u.add.tfdq_dram_addr = cpu_to_le64(txq->dma_addr);
1083 		cmd.new.u.add.bc_dram_addr = cpu_to_le64(txq->bc_tbl.dma);
1084 		cmd.new.u.add.cb_size = cpu_to_le32(TFD_QUEUE_CB_SIZE(size));
1085 		cmd.new.u.add.flags = cpu_to_le32(flags);
1086 		cmd.new.u.add.sta_mask = cpu_to_le32(sta_mask);
1087 		cmd.new.u.add.tid = tid;
1088 
1089 		hcmd.id = WIDE_ID(DATA_PATH_GROUP, SCD_QUEUE_CONFIG_CMD);
1090 		hcmd.len[0] = sizeof(cmd.new);
1091 		hcmd.data[0] = &cmd.new;
1092 	} else {
1093 		ret = -EOPNOTSUPP;
1094 		goto error;
1095 	}
1096 
1097 	ret = iwl_trans_send_cmd(trans, &hcmd);
1098 	if (ret)
1099 		goto error;
1100 
1101 	return iwl_pcie_txq_alloc_response(trans, txq, &hcmd);
1102 
1103 error:
1104 	iwl_txq_gen2_free_memory(trans, txq);
1105 	return ret;
1106 }
1107 
1108 void iwl_txq_dyn_free(struct iwl_trans *trans, int queue)
1109 {
1110 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1111 
1112 	if (WARN(queue >= IWL_MAX_TVQM_QUEUES,
1113 		 "queue %d out of range", queue))
1114 		return;
1115 
1116 	/*
1117 	 * Upon HW Rfkill - we stop the device, and then stop the queues
1118 	 * in the op_mode. Just for the sake of the simplicity of the op_mode,
1119 	 * allow the op_mode to call txq_disable after it already called
1120 	 * stop_device.
1121 	 */
1122 	if (!test_and_clear_bit(queue, trans_pcie->txqs.queue_used)) {
1123 		WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
1124 			  "queue %d not used", queue);
1125 		return;
1126 	}
1127 
1128 	iwl_txq_gen2_free(trans, queue);
1129 
1130 	IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", queue);
1131 }
1132 
1133 void iwl_txq_gen2_tx_free(struct iwl_trans *trans)
1134 {
1135 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1136 	int i;
1137 
1138 	memset(trans_pcie->txqs.queue_used, 0,
1139 	       sizeof(trans_pcie->txqs.queue_used));
1140 
1141 	/* Free all TX queues */
1142 	for (i = 0; i < ARRAY_SIZE(trans_pcie->txqs.txq); i++) {
1143 		if (!trans_pcie->txqs.txq[i])
1144 			continue;
1145 
1146 		iwl_txq_gen2_free(trans, i);
1147 	}
1148 }
1149 
1150 int iwl_txq_gen2_init(struct iwl_trans *trans, int txq_id, int queue_size)
1151 {
1152 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1153 	struct iwl_txq *queue;
1154 	int ret;
1155 
1156 	/* alloc and init the tx queue */
1157 	if (!trans_pcie->txqs.txq[txq_id]) {
1158 		queue = kzalloc(sizeof(*queue), GFP_KERNEL);
1159 		if (!queue) {
1160 			IWL_ERR(trans, "Not enough memory for tx queue\n");
1161 			return -ENOMEM;
1162 		}
1163 		trans_pcie->txqs.txq[txq_id] = queue;
1164 		ret = iwl_pcie_txq_alloc(trans, queue, queue_size, true);
1165 		if (ret) {
1166 			IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
1167 			goto error;
1168 		}
1169 	} else {
1170 		queue = trans_pcie->txqs.txq[txq_id];
1171 	}
1172 
1173 	ret = iwl_txq_init(trans, queue, queue_size,
1174 			   (txq_id == trans_pcie->txqs.cmd.q_id));
1175 	if (ret) {
1176 		IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
1177 		goto error;
1178 	}
1179 	trans_pcie->txqs.txq[txq_id]->id = txq_id;
1180 	set_bit(txq_id, trans_pcie->txqs.queue_used);
1181 
1182 	return 0;
1183 
1184 error:
1185 	iwl_txq_gen2_tx_free(trans);
1186 	return ret;
1187 }
1188 
1189 /*************** HOST COMMAND QUEUE FUNCTIONS   *****/
1190 
1191 /*
1192  * iwl_pcie_gen2_enqueue_hcmd - enqueue a uCode command
1193  * @priv: device private data point
1194  * @cmd: a pointer to the ucode command structure
1195  *
1196  * The function returns < 0 values to indicate the operation
1197  * failed. On success, it returns the index (>= 0) of command in the
1198  * command queue.
1199  */
1200 int iwl_pcie_gen2_enqueue_hcmd(struct iwl_trans *trans,
1201 			       struct iwl_host_cmd *cmd)
1202 {
1203 	struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
1204 	struct iwl_txq *txq = trans_pcie->txqs.txq[trans_pcie->txqs.cmd.q_id];
1205 	struct iwl_device_cmd *out_cmd;
1206 	struct iwl_cmd_meta *out_meta;
1207 	void *dup_buf = NULL;
1208 	dma_addr_t phys_addr;
1209 	int i, cmd_pos, idx;
1210 	u16 copy_size, cmd_size, tb0_size;
1211 	bool had_nocopy = false;
1212 	u8 group_id = iwl_cmd_groupid(cmd->id);
1213 	const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
1214 	u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
1215 	struct iwl_tfh_tfd *tfd;
1216 	unsigned long flags;
1217 
1218 	if (WARN_ON(cmd->flags & CMD_BLOCK_TXQS))
1219 		return -EINVAL;
1220 
1221 	copy_size = sizeof(struct iwl_cmd_header_wide);
1222 	cmd_size = sizeof(struct iwl_cmd_header_wide);
1223 
1224 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1225 		cmddata[i] = cmd->data[i];
1226 		cmdlen[i] = cmd->len[i];
1227 
1228 		if (!cmd->len[i])
1229 			continue;
1230 
1231 		/* need at least IWL_FIRST_TB_SIZE copied */
1232 		if (copy_size < IWL_FIRST_TB_SIZE) {
1233 			int copy = IWL_FIRST_TB_SIZE - copy_size;
1234 
1235 			if (copy > cmdlen[i])
1236 				copy = cmdlen[i];
1237 			cmdlen[i] -= copy;
1238 			cmddata[i] += copy;
1239 			copy_size += copy;
1240 		}
1241 
1242 		if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
1243 			had_nocopy = true;
1244 			if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
1245 				idx = -EINVAL;
1246 				goto free_dup_buf;
1247 			}
1248 		} else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
1249 			/*
1250 			 * This is also a chunk that isn't copied
1251 			 * to the static buffer so set had_nocopy.
1252 			 */
1253 			had_nocopy = true;
1254 
1255 			/* only allowed once */
1256 			if (WARN_ON(dup_buf)) {
1257 				idx = -EINVAL;
1258 				goto free_dup_buf;
1259 			}
1260 
1261 			dup_buf = kmemdup(cmddata[i], cmdlen[i],
1262 					  GFP_ATOMIC);
1263 			if (!dup_buf)
1264 				return -ENOMEM;
1265 		} else {
1266 			/* NOCOPY must not be followed by normal! */
1267 			if (WARN_ON(had_nocopy)) {
1268 				idx = -EINVAL;
1269 				goto free_dup_buf;
1270 			}
1271 			copy_size += cmdlen[i];
1272 		}
1273 		cmd_size += cmd->len[i];
1274 	}
1275 
1276 	/*
1277 	 * If any of the command structures end up being larger than the
1278 	 * TFD_MAX_PAYLOAD_SIZE and they aren't dynamically allocated into
1279 	 * separate TFDs, then we will need to increase the size of the buffers
1280 	 */
1281 	if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
1282 		 "Command %s (%#x) is too large (%d bytes)\n",
1283 		 iwl_get_cmd_string(trans, cmd->id), cmd->id, copy_size)) {
1284 		idx = -EINVAL;
1285 		goto free_dup_buf;
1286 	}
1287 
1288 	spin_lock_irqsave(&txq->lock, flags);
1289 
1290 	idx = iwl_txq_get_cmd_index(txq, txq->write_ptr);
1291 	tfd = iwl_txq_get_tfd(trans, txq, txq->write_ptr);
1292 	memset(tfd, 0, sizeof(*tfd));
1293 
1294 	if (iwl_txq_space(trans, txq) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
1295 		spin_unlock_irqrestore(&txq->lock, flags);
1296 
1297 		IWL_ERR(trans, "No space in command queue\n");
1298 		iwl_op_mode_cmd_queue_full(trans->op_mode);
1299 		idx = -ENOSPC;
1300 		goto free_dup_buf;
1301 	}
1302 
1303 	out_cmd = txq->entries[idx].cmd;
1304 	out_meta = &txq->entries[idx].meta;
1305 
1306 	/* re-initialize, this also marks the SG list as unused */
1307 	memset(out_meta, 0, sizeof(*out_meta));
1308 	if (cmd->flags & CMD_WANT_SKB)
1309 		out_meta->source = cmd;
1310 
1311 	/* set up the header */
1312 	out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
1313 	out_cmd->hdr_wide.group_id = group_id;
1314 	out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
1315 	out_cmd->hdr_wide.length =
1316 		cpu_to_le16(cmd_size - sizeof(struct iwl_cmd_header_wide));
1317 	out_cmd->hdr_wide.reserved = 0;
1318 	out_cmd->hdr_wide.sequence =
1319 		cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->txqs.cmd.q_id) |
1320 					 INDEX_TO_SEQ(txq->write_ptr));
1321 
1322 	cmd_pos = sizeof(struct iwl_cmd_header_wide);
1323 	copy_size = sizeof(struct iwl_cmd_header_wide);
1324 
1325 	/* and copy the data that needs to be copied */
1326 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1327 		int copy;
1328 
1329 		if (!cmd->len[i])
1330 			continue;
1331 
1332 		/* copy everything if not nocopy/dup */
1333 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1334 					   IWL_HCMD_DFL_DUP))) {
1335 			copy = cmd->len[i];
1336 
1337 			memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1338 			cmd_pos += copy;
1339 			copy_size += copy;
1340 			continue;
1341 		}
1342 
1343 		/*
1344 		 * Otherwise we need at least IWL_FIRST_TB_SIZE copied
1345 		 * in total (for bi-directional DMA), but copy up to what
1346 		 * we can fit into the payload for debug dump purposes.
1347 		 */
1348 		copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
1349 
1350 		memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
1351 		cmd_pos += copy;
1352 
1353 		/* However, treat copy_size the proper way, we need it below */
1354 		if (copy_size < IWL_FIRST_TB_SIZE) {
1355 			copy = IWL_FIRST_TB_SIZE - copy_size;
1356 
1357 			if (copy > cmd->len[i])
1358 				copy = cmd->len[i];
1359 			copy_size += copy;
1360 		}
1361 	}
1362 
1363 	IWL_DEBUG_HC(trans,
1364 		     "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
1365 		     iwl_get_cmd_string(trans, cmd->id), group_id,
1366 		     out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
1367 		     cmd_size, txq->write_ptr, idx, trans_pcie->txqs.cmd.q_id);
1368 
1369 	/* start the TFD with the minimum copy bytes */
1370 	tb0_size = min_t(int, copy_size, IWL_FIRST_TB_SIZE);
1371 	memcpy(&txq->first_tb_bufs[idx], out_cmd, tb0_size);
1372 	iwl_txq_gen2_set_tb(trans, tfd, iwl_txq_get_first_tb_dma(txq, idx),
1373 			    tb0_size);
1374 
1375 	/* map first command fragment, if any remains */
1376 	if (copy_size > tb0_size) {
1377 		phys_addr = dma_map_single(trans->dev,
1378 					   (u8 *)out_cmd + tb0_size,
1379 					   copy_size - tb0_size,
1380 					   DMA_TO_DEVICE);
1381 		if (dma_mapping_error(trans->dev, phys_addr)) {
1382 			idx = -ENOMEM;
1383 			iwl_txq_gen2_tfd_unmap(trans, out_meta, tfd);
1384 			goto out;
1385 		}
1386 		iwl_txq_gen2_set_tb(trans, tfd, phys_addr,
1387 				    copy_size - tb0_size);
1388 	}
1389 
1390 	/* map the remaining (adjusted) nocopy/dup fragments */
1391 	for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
1392 		void *data = (void *)(uintptr_t)cmddata[i];
1393 
1394 		if (!cmdlen[i])
1395 			continue;
1396 		if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
1397 					   IWL_HCMD_DFL_DUP)))
1398 			continue;
1399 		if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
1400 			data = dup_buf;
1401 		phys_addr = dma_map_single(trans->dev, data,
1402 					   cmdlen[i], DMA_TO_DEVICE);
1403 		if (dma_mapping_error(trans->dev, phys_addr)) {
1404 			idx = -ENOMEM;
1405 			iwl_txq_gen2_tfd_unmap(trans, out_meta, tfd);
1406 			goto out;
1407 		}
1408 		iwl_txq_gen2_set_tb(trans, tfd, phys_addr, cmdlen[i]);
1409 	}
1410 
1411 	BUILD_BUG_ON(IWL_TFH_NUM_TBS > sizeof(out_meta->tbs) * BITS_PER_BYTE);
1412 	out_meta->flags = cmd->flags;
1413 	if (WARN_ON_ONCE(txq->entries[idx].free_buf))
1414 		kfree_sensitive(txq->entries[idx].free_buf);
1415 	txq->entries[idx].free_buf = dup_buf;
1416 
1417 	trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
1418 
1419 	/* start timer if queue currently empty */
1420 	if (txq->read_ptr == txq->write_ptr && txq->wd_timeout)
1421 		mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
1422 
1423 	spin_lock(&trans_pcie->reg_lock);
1424 	/* Increment and update queue's write index */
1425 	txq->write_ptr = iwl_txq_inc_wrap(trans, txq->write_ptr);
1426 	iwl_txq_inc_wr_ptr(trans, txq);
1427 	spin_unlock(&trans_pcie->reg_lock);
1428 
1429 out:
1430 	spin_unlock_irqrestore(&txq->lock, flags);
1431 free_dup_buf:
1432 	if (idx < 0)
1433 		kfree(dup_buf);
1434 	return idx;
1435 }
1436