tx-gen2.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2017 Intel Deutschland GmbH
  9. * Copyright(c) 2018 Intel Corporation
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * BSD LICENSE
  21. *
  22. * Copyright(c) 2017 Intel Deutschland GmbH
  23. * Copyright(c) 2018 Intel Corporation
  24. * All rights reserved.
  25. *
  26. * Redistribution and use in source and binary forms, with or without
  27. * modification, are permitted provided that the following conditions
  28. * are met:
  29. *
  30. * * Redistributions of source code must retain the above copyright
  31. * notice, this list of conditions and the following disclaimer.
  32. * * Redistributions in binary form must reproduce the above copyright
  33. * notice, this list of conditions and the following disclaimer in
  34. * the documentation and/or other materials provided with the
  35. * distribution.
  36. * * Neither the name Intel Corporation nor the names of its
  37. * contributors may be used to endorse or promote products derived
  38. * from this software without specific prior written permission.
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  41. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  42. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  43. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  44. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  46. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  47. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  48. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  49. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  50. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. *
  52. *****************************************************************************/
  53. #include <linux/pm_runtime.h>
  54. #include <net/tso.h>
  55. #include <linux/tcp.h>
  56. #include "iwl-debug.h"
  57. #include "iwl-csr.h"
  58. #include "iwl-io.h"
  59. #include "internal.h"
  60. #include "fw/api/tx.h"
  61. /*
  62. * iwl_pcie_gen2_tx_stop - Stop all Tx DMA channels
  63. */
  64. void iwl_pcie_gen2_tx_stop(struct iwl_trans *trans)
  65. {
  66. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  67. int txq_id;
  68. /*
  69. * This function can be called before the op_mode disabled the
  70. * queues. This happens when we have an rfkill interrupt.
  71. * Since we stop Tx altogether - mark the queues as stopped.
  72. */
  73. memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
  74. memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
  75. /* Unmap DMA from host system and free skb's */
  76. for (txq_id = 0; txq_id < ARRAY_SIZE(trans_pcie->txq); txq_id++) {
  77. if (!trans_pcie->txq[txq_id])
  78. continue;
  79. iwl_pcie_gen2_txq_unmap(trans, txq_id);
  80. }
  81. }
  82. /*
  83. * iwl_pcie_txq_update_byte_tbl - Set up entry in Tx byte-count array
  84. */
  85. static void iwl_pcie_gen2_update_byte_tbl(struct iwl_trans_pcie *trans_pcie,
  86. struct iwl_txq *txq, u16 byte_cnt,
  87. int num_tbs)
  88. {
  89. struct iwlagn_scd_bc_tbl *scd_bc_tbl = txq->bc_tbl.addr;
  90. struct iwl_trans *trans = iwl_trans_pcie_get_trans(trans_pcie);
  91. struct iwl_gen3_bc_tbl *scd_bc_tbl_gen3 = txq->bc_tbl.addr;
  92. int idx = iwl_pcie_get_cmd_index(txq, txq->write_ptr);
  93. u8 filled_tfd_size, num_fetch_chunks;
  94. u16 len = byte_cnt;
  95. __le16 bc_ent;
  96. if (trans_pcie->bc_table_dword)
  97. len = DIV_ROUND_UP(len, 4);
  98. if (WARN_ON(len > 0xFFF || idx >= txq->n_window))
  99. return;
  100. filled_tfd_size = offsetof(struct iwl_tfh_tfd, tbs) +
  101. num_tbs * sizeof(struct iwl_tfh_tb);
  102. /*
  103. * filled_tfd_size contains the number of filled bytes in the TFD.
  104. * Dividing it by 64 will give the number of chunks to fetch
  105. * to SRAM- 0 for one chunk, 1 for 2 and so on.
  106. * If, for example, TFD contains only 3 TBs then 32 bytes
  107. * of the TFD are used, and only one chunk of 64 bytes should
  108. * be fetched
  109. */
  110. num_fetch_chunks = DIV_ROUND_UP(filled_tfd_size, 64) - 1;
  111. bc_ent = cpu_to_le16(len | (num_fetch_chunks << 12));
  112. if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560)
  113. scd_bc_tbl_gen3->tfd_offset[idx] = bc_ent;
  114. else
  115. scd_bc_tbl->tfd_offset[idx] = bc_ent;
  116. }
  117. /*
  118. * iwl_pcie_gen2_txq_inc_wr_ptr - Send new write index to hardware
  119. */
  120. static void iwl_pcie_gen2_txq_inc_wr_ptr(struct iwl_trans *trans,
  121. struct iwl_txq *txq)
  122. {
  123. lockdep_assert_held(&txq->lock);
  124. IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq->id, txq->write_ptr);
  125. /*
  126. * if not in power-save mode, uCode will never sleep when we're
  127. * trying to tx (during RFKILL, we're not trying to tx).
  128. */
  129. iwl_write32(trans, HBUS_TARG_WRPTR, txq->write_ptr | (txq->id << 16));
  130. }
  131. static u8 iwl_pcie_gen2_get_num_tbs(struct iwl_trans *trans,
  132. struct iwl_tfh_tfd *tfd)
  133. {
  134. return le16_to_cpu(tfd->num_tbs) & 0x1f;
  135. }
  136. static void iwl_pcie_gen2_tfd_unmap(struct iwl_trans *trans,
  137. struct iwl_cmd_meta *meta,
  138. struct iwl_tfh_tfd *tfd)
  139. {
  140. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  141. int i, num_tbs;
  142. /* Sanity check on number of chunks */
  143. num_tbs = iwl_pcie_gen2_get_num_tbs(trans, tfd);
  144. if (num_tbs > trans_pcie->max_tbs) {
  145. IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
  146. return;
  147. }
  148. /* first TB is never freed - it's the bidirectional DMA data */
  149. for (i = 1; i < num_tbs; i++) {
  150. if (meta->tbs & BIT(i))
  151. dma_unmap_page(trans->dev,
  152. le64_to_cpu(tfd->tbs[i].addr),
  153. le16_to_cpu(tfd->tbs[i].tb_len),
  154. DMA_TO_DEVICE);
  155. else
  156. dma_unmap_single(trans->dev,
  157. le64_to_cpu(tfd->tbs[i].addr),
  158. le16_to_cpu(tfd->tbs[i].tb_len),
  159. DMA_TO_DEVICE);
  160. }
  161. tfd->num_tbs = 0;
  162. }
  163. static void iwl_pcie_gen2_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
  164. {
  165. /* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and
  166. * idx is bounded by n_window
  167. */
  168. int idx = iwl_pcie_get_cmd_index(txq, txq->read_ptr);
  169. lockdep_assert_held(&txq->lock);
  170. iwl_pcie_gen2_tfd_unmap(trans, &txq->entries[idx].meta,
  171. iwl_pcie_get_tfd(trans, txq, idx));
  172. /* free SKB */
  173. if (txq->entries) {
  174. struct sk_buff *skb;
  175. skb = txq->entries[idx].skb;
  176. /* Can be called from irqs-disabled context
  177. * If skb is not NULL, it means that the whole queue is being
  178. * freed and that the queue is not empty - free the skb
  179. */
  180. if (skb) {
  181. iwl_op_mode_free_skb(trans->op_mode, skb);
  182. txq->entries[idx].skb = NULL;
  183. }
  184. }
  185. }
  186. static int iwl_pcie_gen2_set_tb(struct iwl_trans *trans,
  187. struct iwl_tfh_tfd *tfd, dma_addr_t addr,
  188. u16 len)
  189. {
  190. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  191. int idx = iwl_pcie_gen2_get_num_tbs(trans, tfd);
  192. struct iwl_tfh_tb *tb = &tfd->tbs[idx];
  193. /* Each TFD can point to a maximum max_tbs Tx buffers */
  194. if (le16_to_cpu(tfd->num_tbs) >= trans_pcie->max_tbs) {
  195. IWL_ERR(trans, "Error can not send more than %d chunks\n",
  196. trans_pcie->max_tbs);
  197. return -EINVAL;
  198. }
  199. put_unaligned_le64(addr, &tb->addr);
  200. tb->tb_len = cpu_to_le16(len);
  201. tfd->num_tbs = cpu_to_le16(idx + 1);
  202. return idx;
  203. }
  204. static int iwl_pcie_gen2_build_amsdu(struct iwl_trans *trans,
  205. struct sk_buff *skb,
  206. struct iwl_tfh_tfd *tfd, int start_len,
  207. u8 hdr_len, struct iwl_device_cmd *dev_cmd)
  208. {
  209. #ifdef CONFIG_INET
  210. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  211. struct iwl_tx_cmd *tx_cmd = (void *)dev_cmd->payload;
  212. struct ieee80211_hdr *hdr = (void *)skb->data;
  213. unsigned int snap_ip_tcp_hdrlen, ip_hdrlen, total_len, hdr_room;
  214. unsigned int mss = skb_shinfo(skb)->gso_size;
  215. u16 length, amsdu_pad;
  216. u8 *start_hdr;
  217. struct iwl_tso_hdr_page *hdr_page;
  218. struct page **page_ptr;
  219. struct tso_t tso;
  220. trace_iwlwifi_dev_tx(trans->dev, skb, tfd, sizeof(*tfd),
  221. &dev_cmd->hdr, start_len, 0);
  222. ip_hdrlen = skb_transport_header(skb) - skb_network_header(skb);
  223. snap_ip_tcp_hdrlen = 8 + ip_hdrlen + tcp_hdrlen(skb);
  224. total_len = skb->len - snap_ip_tcp_hdrlen - hdr_len;
  225. amsdu_pad = 0;
  226. /* total amount of header we may need for this A-MSDU */
  227. hdr_room = DIV_ROUND_UP(total_len, mss) *
  228. (3 + snap_ip_tcp_hdrlen + sizeof(struct ethhdr));
  229. /* Our device supports 9 segments at most, it will fit in 1 page */
  230. hdr_page = get_page_hdr(trans, hdr_room);
  231. if (!hdr_page)
  232. return -ENOMEM;
  233. get_page(hdr_page->page);
  234. start_hdr = hdr_page->pos;
  235. page_ptr = (void *)((u8 *)skb->cb + trans_pcie->page_offs);
  236. *page_ptr = hdr_page->page;
  237. /*
  238. * Pull the ieee80211 header to be able to use TSO core,
  239. * we will restore it for the tx_status flow.
  240. */
  241. skb_pull(skb, hdr_len);
  242. /*
  243. * Remove the length of all the headers that we don't actually
  244. * have in the MPDU by themselves, but that we duplicate into
  245. * all the different MSDUs inside the A-MSDU.
  246. */
  247. le16_add_cpu(&tx_cmd->len, -snap_ip_tcp_hdrlen);
  248. tso_start(skb, &tso);
  249. while (total_len) {
  250. /* this is the data left for this subframe */
  251. unsigned int data_left = min_t(unsigned int, mss, total_len);
  252. struct sk_buff *csum_skb = NULL;
  253. unsigned int tb_len;
  254. dma_addr_t tb_phys;
  255. u8 *subf_hdrs_start = hdr_page->pos;
  256. total_len -= data_left;
  257. memset(hdr_page->pos, 0, amsdu_pad);
  258. hdr_page->pos += amsdu_pad;
  259. amsdu_pad = (4 - (sizeof(struct ethhdr) + snap_ip_tcp_hdrlen +
  260. data_left)) & 0x3;
  261. ether_addr_copy(hdr_page->pos, ieee80211_get_DA(hdr));
  262. hdr_page->pos += ETH_ALEN;
  263. ether_addr_copy(hdr_page->pos, ieee80211_get_SA(hdr));
  264. hdr_page->pos += ETH_ALEN;
  265. length = snap_ip_tcp_hdrlen + data_left;
  266. *((__be16 *)hdr_page->pos) = cpu_to_be16(length);
  267. hdr_page->pos += sizeof(length);
  268. /*
  269. * This will copy the SNAP as well which will be considered
  270. * as MAC header.
  271. */
  272. tso_build_hdr(skb, hdr_page->pos, &tso, data_left, !total_len);
  273. hdr_page->pos += snap_ip_tcp_hdrlen;
  274. tb_len = hdr_page->pos - start_hdr;
  275. tb_phys = dma_map_single(trans->dev, start_hdr,
  276. tb_len, DMA_TO_DEVICE);
  277. if (unlikely(dma_mapping_error(trans->dev, tb_phys))) {
  278. dev_kfree_skb(csum_skb);
  279. goto out_err;
  280. }
  281. iwl_pcie_gen2_set_tb(trans, tfd, tb_phys, tb_len);
  282. trace_iwlwifi_dev_tx_tso_chunk(trans->dev, start_hdr, tb_len);
  283. /* add this subframe's headers' length to the tx_cmd */
  284. le16_add_cpu(&tx_cmd->len, hdr_page->pos - subf_hdrs_start);
  285. /* prepare the start_hdr for the next subframe */
  286. start_hdr = hdr_page->pos;
  287. /* put the payload */
  288. while (data_left) {
  289. tb_len = min_t(unsigned int, tso.size, data_left);
  290. tb_phys = dma_map_single(trans->dev, tso.data,
  291. tb_len, DMA_TO_DEVICE);
  292. if (unlikely(dma_mapping_error(trans->dev, tb_phys))) {
  293. dev_kfree_skb(csum_skb);
  294. goto out_err;
  295. }
  296. iwl_pcie_gen2_set_tb(trans, tfd, tb_phys, tb_len);
  297. trace_iwlwifi_dev_tx_tso_chunk(trans->dev, tso.data,
  298. tb_len);
  299. data_left -= tb_len;
  300. tso_build_data(skb, &tso, tb_len);
  301. }
  302. }
  303. /* re -add the WiFi header */
  304. skb_push(skb, hdr_len);
  305. return 0;
  306. out_err:
  307. #endif
  308. return -EINVAL;
  309. }
  310. static struct
  311. iwl_tfh_tfd *iwl_pcie_gen2_build_tx_amsdu(struct iwl_trans *trans,
  312. struct iwl_txq *txq,
  313. struct iwl_device_cmd *dev_cmd,
  314. struct sk_buff *skb,
  315. struct iwl_cmd_meta *out_meta,
  316. int hdr_len,
  317. int tx_cmd_len)
  318. {
  319. int idx = iwl_pcie_get_cmd_index(txq, txq->write_ptr);
  320. struct iwl_tfh_tfd *tfd = iwl_pcie_get_tfd(trans, txq, idx);
  321. dma_addr_t tb_phys;
  322. int len;
  323. void *tb1_addr;
  324. tb_phys = iwl_pcie_get_first_tb_dma(txq, idx);
  325. iwl_pcie_gen2_set_tb(trans, tfd, tb_phys, IWL_FIRST_TB_SIZE);
  326. /*
  327. * The second TB (tb1) points to the remainder of the TX command
  328. * and the 802.11 header - dword aligned size
  329. * (This calculation modifies the TX command, so do it before the
  330. * setup of the first TB)
  331. */
  332. len = tx_cmd_len + sizeof(struct iwl_cmd_header) + hdr_len -
  333. IWL_FIRST_TB_SIZE;
  334. /* do not align A-MSDU to dword as the subframe header aligns it */
  335. /* map the data for TB1 */
  336. tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE;
  337. tb_phys = dma_map_single(trans->dev, tb1_addr, len, DMA_TO_DEVICE);
  338. if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
  339. goto out_err;
  340. iwl_pcie_gen2_set_tb(trans, tfd, tb_phys, len);
  341. if (iwl_pcie_gen2_build_amsdu(trans, skb, tfd,
  342. len + IWL_FIRST_TB_SIZE,
  343. hdr_len, dev_cmd))
  344. goto out_err;
  345. /* building the A-MSDU might have changed this data, memcpy it now */
  346. memcpy(&txq->first_tb_bufs[idx], &dev_cmd->hdr, IWL_FIRST_TB_SIZE);
  347. return tfd;
  348. out_err:
  349. iwl_pcie_gen2_tfd_unmap(trans, out_meta, tfd);
  350. return NULL;
  351. }
  352. static struct
  353. iwl_tfh_tfd *iwl_pcie_gen2_build_tx(struct iwl_trans *trans,
  354. struct iwl_txq *txq,
  355. struct iwl_device_cmd *dev_cmd,
  356. struct sk_buff *skb,
  357. struct iwl_cmd_meta *out_meta,
  358. int hdr_len,
  359. int tx_cmd_len)
  360. {
  361. int idx = iwl_pcie_get_cmd_index(txq, txq->write_ptr);
  362. struct iwl_tfh_tfd *tfd = iwl_pcie_get_tfd(trans, txq, idx);
  363. dma_addr_t tb_phys;
  364. int i, len, tb1_len, tb2_len;
  365. void *tb1_addr;
  366. tb_phys = iwl_pcie_get_first_tb_dma(txq, idx);
  367. /* The first TB points to bi-directional DMA data */
  368. memcpy(&txq->first_tb_bufs[idx], &dev_cmd->hdr, IWL_FIRST_TB_SIZE);
  369. iwl_pcie_gen2_set_tb(trans, tfd, tb_phys, IWL_FIRST_TB_SIZE);
  370. /*
  371. * The second TB (tb1) points to the remainder of the TX command
  372. * and the 802.11 header - dword aligned size
  373. * (This calculation modifies the TX command, so do it before the
  374. * setup of the first TB)
  375. */
  376. len = tx_cmd_len + sizeof(struct iwl_cmd_header) + hdr_len -
  377. IWL_FIRST_TB_SIZE;
  378. tb1_len = ALIGN(len, 4);
  379. /* map the data for TB1 */
  380. tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE;
  381. tb_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
  382. if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
  383. goto out_err;
  384. iwl_pcie_gen2_set_tb(trans, tfd, tb_phys, tb1_len);
  385. /* set up TFD's third entry to point to remainder of skb's head */
  386. tb2_len = skb_headlen(skb) - hdr_len;
  387. if (tb2_len > 0) {
  388. tb_phys = dma_map_single(trans->dev, skb->data + hdr_len,
  389. tb2_len, DMA_TO_DEVICE);
  390. if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
  391. goto out_err;
  392. iwl_pcie_gen2_set_tb(trans, tfd, tb_phys, tb2_len);
  393. }
  394. /* set up the remaining entries to point to the data */
  395. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  396. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  397. int tb_idx;
  398. if (!skb_frag_size(frag))
  399. continue;
  400. tb_phys = skb_frag_dma_map(trans->dev, frag, 0,
  401. skb_frag_size(frag), DMA_TO_DEVICE);
  402. if (unlikely(dma_mapping_error(trans->dev, tb_phys)))
  403. goto out_err;
  404. tb_idx = iwl_pcie_gen2_set_tb(trans, tfd, tb_phys,
  405. skb_frag_size(frag));
  406. out_meta->tbs |= BIT(tb_idx);
  407. }
  408. trace_iwlwifi_dev_tx(trans->dev, skb, tfd, sizeof(*tfd), &dev_cmd->hdr,
  409. IWL_FIRST_TB_SIZE + tb1_len, hdr_len);
  410. trace_iwlwifi_dev_tx_data(trans->dev, skb, hdr_len);
  411. return tfd;
  412. out_err:
  413. iwl_pcie_gen2_tfd_unmap(trans, out_meta, tfd);
  414. return NULL;
  415. }
  416. static
  417. struct iwl_tfh_tfd *iwl_pcie_gen2_build_tfd(struct iwl_trans *trans,
  418. struct iwl_txq *txq,
  419. struct iwl_device_cmd *dev_cmd,
  420. struct sk_buff *skb,
  421. struct iwl_cmd_meta *out_meta)
  422. {
  423. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  424. int idx = iwl_pcie_get_cmd_index(txq, txq->write_ptr);
  425. struct iwl_tfh_tfd *tfd = iwl_pcie_get_tfd(trans, txq, idx);
  426. int len, hdr_len;
  427. bool amsdu;
  428. /* There must be data left over for TB1 or this code must be changed */
  429. BUILD_BUG_ON(sizeof(struct iwl_tx_cmd_gen2) < IWL_FIRST_TB_SIZE);
  430. memset(tfd, 0, sizeof(*tfd));
  431. if (trans->cfg->device_family < IWL_DEVICE_FAMILY_22560)
  432. len = sizeof(struct iwl_tx_cmd_gen2);
  433. else
  434. len = sizeof(struct iwl_tx_cmd_gen3);
  435. amsdu = ieee80211_is_data_qos(hdr->frame_control) &&
  436. (*ieee80211_get_qos_ctl(hdr) &
  437. IEEE80211_QOS_CTL_A_MSDU_PRESENT);
  438. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  439. /*
  440. * Only build A-MSDUs here if doing so by GSO, otherwise it may be
  441. * an A-MSDU for other reasons, e.g. NAN or an A-MSDU having been
  442. * built in the higher layers already.
  443. */
  444. if (amsdu && skb_shinfo(skb)->gso_size)
  445. return iwl_pcie_gen2_build_tx_amsdu(trans, txq, dev_cmd, skb,
  446. out_meta, hdr_len, len);
  447. return iwl_pcie_gen2_build_tx(trans, txq, dev_cmd, skb, out_meta,
  448. hdr_len, len);
  449. }
  450. int iwl_trans_pcie_gen2_tx(struct iwl_trans *trans, struct sk_buff *skb,
  451. struct iwl_device_cmd *dev_cmd, int txq_id)
  452. {
  453. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  454. struct iwl_cmd_meta *out_meta;
  455. struct iwl_txq *txq = trans_pcie->txq[txq_id];
  456. u16 cmd_len;
  457. int idx;
  458. void *tfd;
  459. if (WARN_ONCE(!test_bit(txq_id, trans_pcie->queue_used),
  460. "TX on unused queue %d\n", txq_id))
  461. return -EINVAL;
  462. if (skb_is_nonlinear(skb) &&
  463. skb_shinfo(skb)->nr_frags > IWL_PCIE_MAX_FRAGS(trans_pcie) &&
  464. __skb_linearize(skb))
  465. return -ENOMEM;
  466. spin_lock(&txq->lock);
  467. if (iwl_queue_space(trans, txq) < txq->high_mark) {
  468. iwl_stop_queue(trans, txq);
  469. /* don't put the packet on the ring, if there is no room */
  470. if (unlikely(iwl_queue_space(trans, txq) < 3)) {
  471. struct iwl_device_cmd **dev_cmd_ptr;
  472. dev_cmd_ptr = (void *)((u8 *)skb->cb +
  473. trans_pcie->dev_cmd_offs);
  474. *dev_cmd_ptr = dev_cmd;
  475. __skb_queue_tail(&txq->overflow_q, skb);
  476. spin_unlock(&txq->lock);
  477. return 0;
  478. }
  479. }
  480. idx = iwl_pcie_get_cmd_index(txq, txq->write_ptr);
  481. /* Set up driver data for this TFD */
  482. txq->entries[idx].skb = skb;
  483. txq->entries[idx].cmd = dev_cmd;
  484. dev_cmd->hdr.sequence =
  485. cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  486. INDEX_TO_SEQ(idx)));
  487. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  488. out_meta = &txq->entries[idx].meta;
  489. out_meta->flags = 0;
  490. tfd = iwl_pcie_gen2_build_tfd(trans, txq, dev_cmd, skb, out_meta);
  491. if (!tfd) {
  492. spin_unlock(&txq->lock);
  493. return -1;
  494. }
  495. if (trans->cfg->device_family >= IWL_DEVICE_FAMILY_22560) {
  496. struct iwl_tx_cmd_gen3 *tx_cmd_gen3 =
  497. (void *)dev_cmd->payload;
  498. cmd_len = le16_to_cpu(tx_cmd_gen3->len);
  499. } else {
  500. struct iwl_tx_cmd_gen2 *tx_cmd_gen2 =
  501. (void *)dev_cmd->payload;
  502. cmd_len = le16_to_cpu(tx_cmd_gen2->len);
  503. }
  504. /* Set up entry for this TFD in Tx byte-count array */
  505. iwl_pcie_gen2_update_byte_tbl(trans_pcie, txq, cmd_len,
  506. iwl_pcie_gen2_get_num_tbs(trans, tfd));
  507. /* start timer if queue currently empty */
  508. if (txq->read_ptr == txq->write_ptr) {
  509. if (txq->wd_timeout)
  510. mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
  511. IWL_DEBUG_RPM(trans, "Q: %d first tx - take ref\n", txq->id);
  512. iwl_trans_ref(trans);
  513. }
  514. /* Tell device the write index *just past* this latest filled TFD */
  515. txq->write_ptr = iwl_queue_inc_wrap(trans, txq->write_ptr);
  516. iwl_pcie_gen2_txq_inc_wr_ptr(trans, txq);
  517. /*
  518. * At this point the frame is "transmitted" successfully
  519. * and we will get a TX status notification eventually.
  520. */
  521. spin_unlock(&txq->lock);
  522. return 0;
  523. }
  524. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  525. /*
  526. * iwl_pcie_gen2_enqueue_hcmd - enqueue a uCode command
  527. * @priv: device private data point
  528. * @cmd: a pointer to the ucode command structure
  529. *
  530. * The function returns < 0 values to indicate the operation
  531. * failed. On success, it returns the index (>= 0) of command in the
  532. * command queue.
  533. */
  534. static int iwl_pcie_gen2_enqueue_hcmd(struct iwl_trans *trans,
  535. struct iwl_host_cmd *cmd)
  536. {
  537. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  538. struct iwl_txq *txq = trans_pcie->txq[trans_pcie->cmd_queue];
  539. struct iwl_device_cmd *out_cmd;
  540. struct iwl_cmd_meta *out_meta;
  541. unsigned long flags;
  542. void *dup_buf = NULL;
  543. dma_addr_t phys_addr;
  544. int i, cmd_pos, idx;
  545. u16 copy_size, cmd_size, tb0_size;
  546. bool had_nocopy = false;
  547. u8 group_id = iwl_cmd_groupid(cmd->id);
  548. const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
  549. u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
  550. struct iwl_tfh_tfd *tfd;
  551. unsigned long flags2;
  552. copy_size = sizeof(struct iwl_cmd_header_wide);
  553. cmd_size = sizeof(struct iwl_cmd_header_wide);
  554. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  555. cmddata[i] = cmd->data[i];
  556. cmdlen[i] = cmd->len[i];
  557. if (!cmd->len[i])
  558. continue;
  559. /* need at least IWL_FIRST_TB_SIZE copied */
  560. if (copy_size < IWL_FIRST_TB_SIZE) {
  561. int copy = IWL_FIRST_TB_SIZE - copy_size;
  562. if (copy > cmdlen[i])
  563. copy = cmdlen[i];
  564. cmdlen[i] -= copy;
  565. cmddata[i] += copy;
  566. copy_size += copy;
  567. }
  568. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  569. had_nocopy = true;
  570. if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
  571. idx = -EINVAL;
  572. goto free_dup_buf;
  573. }
  574. } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
  575. /*
  576. * This is also a chunk that isn't copied
  577. * to the static buffer so set had_nocopy.
  578. */
  579. had_nocopy = true;
  580. /* only allowed once */
  581. if (WARN_ON(dup_buf)) {
  582. idx = -EINVAL;
  583. goto free_dup_buf;
  584. }
  585. dup_buf = kmemdup(cmddata[i], cmdlen[i],
  586. GFP_ATOMIC);
  587. if (!dup_buf)
  588. return -ENOMEM;
  589. } else {
  590. /* NOCOPY must not be followed by normal! */
  591. if (WARN_ON(had_nocopy)) {
  592. idx = -EINVAL;
  593. goto free_dup_buf;
  594. }
  595. copy_size += cmdlen[i];
  596. }
  597. cmd_size += cmd->len[i];
  598. }
  599. /*
  600. * If any of the command structures end up being larger than the
  601. * TFD_MAX_PAYLOAD_SIZE and they aren't dynamically allocated into
  602. * separate TFDs, then we will need to increase the size of the buffers
  603. */
  604. if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
  605. "Command %s (%#x) is too large (%d bytes)\n",
  606. iwl_get_cmd_string(trans, cmd->id), cmd->id, copy_size)) {
  607. idx = -EINVAL;
  608. goto free_dup_buf;
  609. }
  610. spin_lock_irqsave(&txq->lock, flags2);
  611. idx = iwl_pcie_get_cmd_index(txq, txq->write_ptr);
  612. tfd = iwl_pcie_get_tfd(trans, txq, txq->write_ptr);
  613. memset(tfd, 0, sizeof(*tfd));
  614. if (iwl_queue_space(trans, txq) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  615. spin_unlock_irqrestore(&txq->lock, flags2);
  616. IWL_ERR(trans, "No space in command queue\n");
  617. iwl_op_mode_cmd_queue_full(trans->op_mode);
  618. idx = -ENOSPC;
  619. goto free_dup_buf;
  620. }
  621. out_cmd = txq->entries[idx].cmd;
  622. out_meta = &txq->entries[idx].meta;
  623. /* re-initialize to NULL */
  624. memset(out_meta, 0, sizeof(*out_meta));
  625. if (cmd->flags & CMD_WANT_SKB)
  626. out_meta->source = cmd;
  627. /* set up the header */
  628. out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
  629. out_cmd->hdr_wide.group_id = group_id;
  630. out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
  631. out_cmd->hdr_wide.length =
  632. cpu_to_le16(cmd_size - sizeof(struct iwl_cmd_header_wide));
  633. out_cmd->hdr_wide.reserved = 0;
  634. out_cmd->hdr_wide.sequence =
  635. cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
  636. INDEX_TO_SEQ(txq->write_ptr));
  637. cmd_pos = sizeof(struct iwl_cmd_header_wide);
  638. copy_size = sizeof(struct iwl_cmd_header_wide);
  639. /* and copy the data that needs to be copied */
  640. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  641. int copy;
  642. if (!cmd->len[i])
  643. continue;
  644. /* copy everything if not nocopy/dup */
  645. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  646. IWL_HCMD_DFL_DUP))) {
  647. copy = cmd->len[i];
  648. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
  649. cmd_pos += copy;
  650. copy_size += copy;
  651. continue;
  652. }
  653. /*
  654. * Otherwise we need at least IWL_FIRST_TB_SIZE copied
  655. * in total (for bi-directional DMA), but copy up to what
  656. * we can fit into the payload for debug dump purposes.
  657. */
  658. copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
  659. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
  660. cmd_pos += copy;
  661. /* However, treat copy_size the proper way, we need it below */
  662. if (copy_size < IWL_FIRST_TB_SIZE) {
  663. copy = IWL_FIRST_TB_SIZE - copy_size;
  664. if (copy > cmd->len[i])
  665. copy = cmd->len[i];
  666. copy_size += copy;
  667. }
  668. }
  669. IWL_DEBUG_HC(trans,
  670. "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
  671. iwl_get_cmd_string(trans, cmd->id), group_id,
  672. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  673. cmd_size, txq->write_ptr, idx, trans_pcie->cmd_queue);
  674. /* start the TFD with the minimum copy bytes */
  675. tb0_size = min_t(int, copy_size, IWL_FIRST_TB_SIZE);
  676. memcpy(&txq->first_tb_bufs[idx], &out_cmd->hdr, tb0_size);
  677. iwl_pcie_gen2_set_tb(trans, tfd, iwl_pcie_get_first_tb_dma(txq, idx),
  678. tb0_size);
  679. /* map first command fragment, if any remains */
  680. if (copy_size > tb0_size) {
  681. phys_addr = dma_map_single(trans->dev,
  682. ((u8 *)&out_cmd->hdr) + tb0_size,
  683. copy_size - tb0_size,
  684. DMA_TO_DEVICE);
  685. if (dma_mapping_error(trans->dev, phys_addr)) {
  686. idx = -ENOMEM;
  687. iwl_pcie_gen2_tfd_unmap(trans, out_meta, tfd);
  688. goto out;
  689. }
  690. iwl_pcie_gen2_set_tb(trans, tfd, phys_addr,
  691. copy_size - tb0_size);
  692. }
  693. /* map the remaining (adjusted) nocopy/dup fragments */
  694. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  695. const void *data = cmddata[i];
  696. if (!cmdlen[i])
  697. continue;
  698. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  699. IWL_HCMD_DFL_DUP)))
  700. continue;
  701. if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
  702. data = dup_buf;
  703. phys_addr = dma_map_single(trans->dev, (void *)data,
  704. cmdlen[i], DMA_TO_DEVICE);
  705. if (dma_mapping_error(trans->dev, phys_addr)) {
  706. idx = -ENOMEM;
  707. iwl_pcie_gen2_tfd_unmap(trans, out_meta, tfd);
  708. goto out;
  709. }
  710. iwl_pcie_gen2_set_tb(trans, tfd, phys_addr, cmdlen[i]);
  711. }
  712. BUILD_BUG_ON(IWL_TFH_NUM_TBS > sizeof(out_meta->tbs) * BITS_PER_BYTE);
  713. out_meta->flags = cmd->flags;
  714. if (WARN_ON_ONCE(txq->entries[idx].free_buf))
  715. kzfree(txq->entries[idx].free_buf);
  716. txq->entries[idx].free_buf = dup_buf;
  717. trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
  718. /* start timer if queue currently empty */
  719. if (txq->read_ptr == txq->write_ptr && txq->wd_timeout)
  720. mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
  721. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  722. if (!(cmd->flags & CMD_SEND_IN_IDLE) &&
  723. !trans_pcie->ref_cmd_in_flight) {
  724. trans_pcie->ref_cmd_in_flight = true;
  725. IWL_DEBUG_RPM(trans, "set ref_cmd_in_flight - ref\n");
  726. iwl_trans_ref(trans);
  727. }
  728. /* Increment and update queue's write index */
  729. txq->write_ptr = iwl_queue_inc_wrap(trans, txq->write_ptr);
  730. iwl_pcie_gen2_txq_inc_wr_ptr(trans, txq);
  731. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  732. out:
  733. spin_unlock_irqrestore(&txq->lock, flags2);
  734. free_dup_buf:
  735. if (idx < 0)
  736. kfree(dup_buf);
  737. return idx;
  738. }
  739. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  740. static int iwl_pcie_gen2_send_hcmd_sync(struct iwl_trans *trans,
  741. struct iwl_host_cmd *cmd)
  742. {
  743. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  744. const char *cmd_str = iwl_get_cmd_string(trans, cmd->id);
  745. struct iwl_txq *txq = trans_pcie->txq[trans_pcie->cmd_queue];
  746. int cmd_idx;
  747. int ret;
  748. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n", cmd_str);
  749. if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE,
  750. &trans->status),
  751. "Command %s: a command is already active!\n", cmd_str))
  752. return -EIO;
  753. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n", cmd_str);
  754. if (pm_runtime_suspended(&trans_pcie->pci_dev->dev)) {
  755. ret = wait_event_timeout(trans_pcie->d0i3_waitq,
  756. pm_runtime_active(&trans_pcie->pci_dev->dev),
  757. msecs_to_jiffies(IWL_TRANS_IDLE_TIMEOUT));
  758. if (!ret) {
  759. IWL_ERR(trans, "Timeout exiting D0i3 before hcmd\n");
  760. return -ETIMEDOUT;
  761. }
  762. }
  763. cmd_idx = iwl_pcie_gen2_enqueue_hcmd(trans, cmd);
  764. if (cmd_idx < 0) {
  765. ret = cmd_idx;
  766. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  767. IWL_ERR(trans, "Error sending %s: enqueue_hcmd failed: %d\n",
  768. cmd_str, ret);
  769. return ret;
  770. }
  771. ret = wait_event_timeout(trans_pcie->wait_command_queue,
  772. !test_bit(STATUS_SYNC_HCMD_ACTIVE,
  773. &trans->status),
  774. HOST_COMPLETE_TIMEOUT);
  775. if (!ret) {
  776. IWL_ERR(trans, "Error sending %s: time out after %dms.\n",
  777. cmd_str, jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  778. IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n",
  779. txq->read_ptr, txq->write_ptr);
  780. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  781. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  782. cmd_str);
  783. ret = -ETIMEDOUT;
  784. iwl_force_nmi(trans);
  785. iwl_trans_fw_error(trans);
  786. goto cancel;
  787. }
  788. if (test_bit(STATUS_FW_ERROR, &trans->status)) {
  789. IWL_ERR(trans, "FW error in SYNC CMD %s\n", cmd_str);
  790. dump_stack();
  791. ret = -EIO;
  792. goto cancel;
  793. }
  794. if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  795. test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
  796. IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
  797. ret = -ERFKILL;
  798. goto cancel;
  799. }
  800. if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
  801. IWL_ERR(trans, "Error: Response NULL in '%s'\n", cmd_str);
  802. ret = -EIO;
  803. goto cancel;
  804. }
  805. return 0;
  806. cancel:
  807. if (cmd->flags & CMD_WANT_SKB) {
  808. /*
  809. * Cancel the CMD_WANT_SKB flag for the cmd in the
  810. * TX cmd queue. Otherwise in case the cmd comes
  811. * in later, it will possibly set an invalid
  812. * address (cmd->meta.source).
  813. */
  814. txq->entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
  815. }
  816. if (cmd->resp_pkt) {
  817. iwl_free_resp(cmd);
  818. cmd->resp_pkt = NULL;
  819. }
  820. return ret;
  821. }
  822. int iwl_trans_pcie_gen2_send_hcmd(struct iwl_trans *trans,
  823. struct iwl_host_cmd *cmd)
  824. {
  825. if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  826. test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
  827. IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
  828. cmd->id);
  829. return -ERFKILL;
  830. }
  831. if (cmd->flags & CMD_ASYNC) {
  832. int ret;
  833. /* An asynchronous command can not expect an SKB to be set. */
  834. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  835. return -EINVAL;
  836. ret = iwl_pcie_gen2_enqueue_hcmd(trans, cmd);
  837. if (ret < 0) {
  838. IWL_ERR(trans,
  839. "Error sending %s: enqueue_hcmd failed: %d\n",
  840. iwl_get_cmd_string(trans, cmd->id), ret);
  841. return ret;
  842. }
  843. return 0;
  844. }
  845. return iwl_pcie_gen2_send_hcmd_sync(trans, cmd);
  846. }
  847. /*
  848. * iwl_pcie_gen2_txq_unmap - Unmap any remaining DMA mappings and free skb's
  849. */
  850. void iwl_pcie_gen2_txq_unmap(struct iwl_trans *trans, int txq_id)
  851. {
  852. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  853. struct iwl_txq *txq = trans_pcie->txq[txq_id];
  854. spin_lock_bh(&txq->lock);
  855. while (txq->write_ptr != txq->read_ptr) {
  856. IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
  857. txq_id, txq->read_ptr);
  858. if (txq_id != trans_pcie->cmd_queue) {
  859. int idx = iwl_pcie_get_cmd_index(txq, txq->read_ptr);
  860. struct sk_buff *skb = txq->entries[idx].skb;
  861. if (WARN_ON_ONCE(!skb))
  862. continue;
  863. iwl_pcie_free_tso_page(trans_pcie, skb);
  864. }
  865. iwl_pcie_gen2_free_tfd(trans, txq);
  866. txq->read_ptr = iwl_queue_inc_wrap(trans, txq->read_ptr);
  867. if (txq->read_ptr == txq->write_ptr) {
  868. unsigned long flags;
  869. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  870. if (txq_id != trans_pcie->cmd_queue) {
  871. IWL_DEBUG_RPM(trans, "Q %d - last tx freed\n",
  872. txq->id);
  873. iwl_trans_unref(trans);
  874. } else if (trans_pcie->ref_cmd_in_flight) {
  875. trans_pcie->ref_cmd_in_flight = false;
  876. IWL_DEBUG_RPM(trans,
  877. "clear ref_cmd_in_flight\n");
  878. iwl_trans_unref(trans);
  879. }
  880. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  881. }
  882. }
  883. while (!skb_queue_empty(&txq->overflow_q)) {
  884. struct sk_buff *skb = __skb_dequeue(&txq->overflow_q);
  885. iwl_op_mode_free_skb(trans->op_mode, skb);
  886. }
  887. spin_unlock_bh(&txq->lock);
  888. /* just in case - this queue may have been stopped */
  889. iwl_wake_queue(trans, txq);
  890. }
  891. static void iwl_pcie_gen2_txq_free_memory(struct iwl_trans *trans,
  892. struct iwl_txq *txq)
  893. {
  894. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  895. struct device *dev = trans->dev;
  896. /* De-alloc circular buffer of TFDs */
  897. if (txq->tfds) {
  898. dma_free_coherent(dev,
  899. trans_pcie->tfd_size * txq->n_window,
  900. txq->tfds, txq->dma_addr);
  901. dma_free_coherent(dev,
  902. sizeof(*txq->first_tb_bufs) * txq->n_window,
  903. txq->first_tb_bufs, txq->first_tb_dma);
  904. }
  905. kfree(txq->entries);
  906. iwl_pcie_free_dma_ptr(trans, &txq->bc_tbl);
  907. kfree(txq);
  908. }
  909. /*
  910. * iwl_pcie_txq_free - Deallocate DMA queue.
  911. * @txq: Transmit queue to deallocate.
  912. *
  913. * Empty queue by removing and destroying all BD's.
  914. * Free all buffers.
  915. * 0-fill, but do not free "txq" descriptor structure.
  916. */
  917. static void iwl_pcie_gen2_txq_free(struct iwl_trans *trans, int txq_id)
  918. {
  919. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  920. struct iwl_txq *txq = trans_pcie->txq[txq_id];
  921. int i;
  922. if (WARN_ON(!txq))
  923. return;
  924. iwl_pcie_gen2_txq_unmap(trans, txq_id);
  925. /* De-alloc array of command/tx buffers */
  926. if (txq_id == trans_pcie->cmd_queue)
  927. for (i = 0; i < txq->n_window; i++) {
  928. kzfree(txq->entries[i].cmd);
  929. kzfree(txq->entries[i].free_buf);
  930. }
  931. del_timer_sync(&txq->stuck_timer);
  932. iwl_pcie_gen2_txq_free_memory(trans, txq);
  933. trans_pcie->txq[txq_id] = NULL;
  934. clear_bit(txq_id, trans_pcie->queue_used);
  935. }
  936. int iwl_trans_pcie_dyn_txq_alloc(struct iwl_trans *trans,
  937. struct iwl_tx_queue_cfg_cmd *cmd,
  938. int cmd_id, int size,
  939. unsigned int timeout)
  940. {
  941. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  942. struct iwl_tx_queue_cfg_rsp *rsp;
  943. struct iwl_txq *txq;
  944. struct iwl_host_cmd hcmd = {
  945. .id = cmd_id,
  946. .len = { sizeof(*cmd) },
  947. .data = { cmd, },
  948. .flags = CMD_WANT_SKB,
  949. };
  950. int ret, qid;
  951. u32 wr_ptr;
  952. txq = kzalloc(sizeof(*txq), GFP_KERNEL);
  953. if (!txq)
  954. return -ENOMEM;
  955. ret = iwl_pcie_alloc_dma_ptr(trans, &txq->bc_tbl,
  956. (trans->cfg->device_family >=
  957. IWL_DEVICE_FAMILY_22560) ?
  958. sizeof(struct iwl_gen3_bc_tbl) :
  959. sizeof(struct iwlagn_scd_bc_tbl));
  960. if (ret) {
  961. IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
  962. kfree(txq);
  963. return -ENOMEM;
  964. }
  965. ret = iwl_pcie_txq_alloc(trans, txq, size, false);
  966. if (ret) {
  967. IWL_ERR(trans, "Tx queue alloc failed\n");
  968. goto error;
  969. }
  970. ret = iwl_pcie_txq_init(trans, txq, size, false);
  971. if (ret) {
  972. IWL_ERR(trans, "Tx queue init failed\n");
  973. goto error;
  974. }
  975. txq->wd_timeout = msecs_to_jiffies(timeout);
  976. cmd->tfdq_addr = cpu_to_le64(txq->dma_addr);
  977. cmd->byte_cnt_addr = cpu_to_le64(txq->bc_tbl.dma);
  978. cmd->cb_size = cpu_to_le32(TFD_QUEUE_CB_SIZE(size));
  979. ret = iwl_trans_send_cmd(trans, &hcmd);
  980. if (ret)
  981. goto error;
  982. if (WARN_ON(iwl_rx_packet_payload_len(hcmd.resp_pkt) != sizeof(*rsp))) {
  983. ret = -EINVAL;
  984. goto error_free_resp;
  985. }
  986. rsp = (void *)hcmd.resp_pkt->data;
  987. qid = le16_to_cpu(rsp->queue_number);
  988. wr_ptr = le16_to_cpu(rsp->write_pointer);
  989. if (qid >= ARRAY_SIZE(trans_pcie->txq)) {
  990. WARN_ONCE(1, "queue index %d unsupported", qid);
  991. ret = -EIO;
  992. goto error_free_resp;
  993. }
  994. if (test_and_set_bit(qid, trans_pcie->queue_used)) {
  995. WARN_ONCE(1, "queue %d already used", qid);
  996. ret = -EIO;
  997. goto error_free_resp;
  998. }
  999. txq->id = qid;
  1000. trans_pcie->txq[qid] = txq;
  1001. wr_ptr &= (trans->cfg->base_params->max_tfd_queue_size - 1);
  1002. /* Place first TFD at index corresponding to start sequence number */
  1003. txq->read_ptr = wr_ptr;
  1004. txq->write_ptr = wr_ptr;
  1005. iwl_write_direct32(trans, HBUS_TARG_WRPTR,
  1006. (txq->write_ptr) | (qid << 16));
  1007. IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d\n", qid);
  1008. iwl_free_resp(&hcmd);
  1009. return qid;
  1010. error_free_resp:
  1011. iwl_free_resp(&hcmd);
  1012. error:
  1013. iwl_pcie_gen2_txq_free_memory(trans, txq);
  1014. return ret;
  1015. }
  1016. void iwl_trans_pcie_dyn_txq_free(struct iwl_trans *trans, int queue)
  1017. {
  1018. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1019. /*
  1020. * Upon HW Rfkill - we stop the device, and then stop the queues
  1021. * in the op_mode. Just for the sake of the simplicity of the op_mode,
  1022. * allow the op_mode to call txq_disable after it already called
  1023. * stop_device.
  1024. */
  1025. if (!test_and_clear_bit(queue, trans_pcie->queue_used)) {
  1026. WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
  1027. "queue %d not used", queue);
  1028. return;
  1029. }
  1030. iwl_pcie_gen2_txq_unmap(trans, queue);
  1031. iwl_pcie_gen2_txq_free_memory(trans, trans_pcie->txq[queue]);
  1032. trans_pcie->txq[queue] = NULL;
  1033. IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", queue);
  1034. }
  1035. void iwl_pcie_gen2_tx_free(struct iwl_trans *trans)
  1036. {
  1037. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1038. int i;
  1039. memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
  1040. /* Free all TX queues */
  1041. for (i = 0; i < ARRAY_SIZE(trans_pcie->txq); i++) {
  1042. if (!trans_pcie->txq[i])
  1043. continue;
  1044. iwl_pcie_gen2_txq_free(trans, i);
  1045. }
  1046. }
  1047. int iwl_pcie_gen2_tx_init(struct iwl_trans *trans)
  1048. {
  1049. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1050. struct iwl_txq *cmd_queue;
  1051. int txq_id = trans_pcie->cmd_queue, ret;
  1052. /* alloc and init the command queue */
  1053. if (!trans_pcie->txq[txq_id]) {
  1054. cmd_queue = kzalloc(sizeof(*cmd_queue), GFP_KERNEL);
  1055. if (!cmd_queue) {
  1056. IWL_ERR(trans, "Not enough memory for command queue\n");
  1057. return -ENOMEM;
  1058. }
  1059. trans_pcie->txq[txq_id] = cmd_queue;
  1060. ret = iwl_pcie_txq_alloc(trans, cmd_queue, TFD_CMD_SLOTS, true);
  1061. if (ret) {
  1062. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  1063. goto error;
  1064. }
  1065. } else {
  1066. cmd_queue = trans_pcie->txq[txq_id];
  1067. }
  1068. ret = iwl_pcie_txq_init(trans, cmd_queue, TFD_CMD_SLOTS, true);
  1069. if (ret) {
  1070. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  1071. goto error;
  1072. }
  1073. trans_pcie->txq[txq_id]->id = txq_id;
  1074. set_bit(txq_id, trans_pcie->queue_used);
  1075. return 0;
  1076. error:
  1077. iwl_pcie_gen2_tx_free(trans);
  1078. return ret;
  1079. }