tx-gen2.c 29 KB

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