tx.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2013 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/etherdevice.h>
  30. #include <linux/slab.h>
  31. #include <linux/sched.h>
  32. #include "iwl-debug.h"
  33. #include "iwl-csr.h"
  34. #include "iwl-prph.h"
  35. #include "iwl-io.h"
  36. #include "iwl-op-mode.h"
  37. #include "internal.h"
  38. /* FIXME: need to abstract out TX command (once we know what it looks like) */
  39. #include "dvm/commands.h"
  40. #define IWL_TX_CRC_SIZE 4
  41. #define IWL_TX_DELIMITER_SIZE 4
  42. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  43. * DMA services
  44. *
  45. * Theory of operation
  46. *
  47. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  48. * of buffer descriptors, each of which points to one or more data buffers for
  49. * the device to read from or fill. Driver and device exchange status of each
  50. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  51. * entries in each circular buffer, to protect against confusing empty and full
  52. * queue states.
  53. *
  54. * The device reads or writes the data in the queues via the device's several
  55. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  56. *
  57. * For Tx queue, there are low mark and high mark limits. If, after queuing
  58. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  59. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  60. * Tx queue resumed.
  61. *
  62. ***************************************************/
  63. static int iwl_queue_space(const struct iwl_queue *q)
  64. {
  65. unsigned int max;
  66. unsigned int used;
  67. /*
  68. * To avoid ambiguity between empty and completely full queues, there
  69. * should always be less than q->n_bd elements in the queue.
  70. * If q->n_window is smaller than q->n_bd, there is no need to reserve
  71. * any queue entries for this purpose.
  72. */
  73. if (q->n_window < q->n_bd)
  74. max = q->n_window;
  75. else
  76. max = q->n_bd - 1;
  77. /*
  78. * q->n_bd is a power of 2, so the following is equivalent to modulo by
  79. * q->n_bd and is well defined for negative dividends.
  80. */
  81. used = (q->write_ptr - q->read_ptr) & (q->n_bd - 1);
  82. if (WARN_ON(used > max))
  83. return 0;
  84. return max - used;
  85. }
  86. /*
  87. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  88. */
  89. static int iwl_queue_init(struct iwl_queue *q, int count, int slots_num, u32 id)
  90. {
  91. q->n_bd = count;
  92. q->n_window = slots_num;
  93. q->id = id;
  94. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  95. * and iwl_queue_dec_wrap are broken. */
  96. if (WARN_ON(!is_power_of_2(count)))
  97. return -EINVAL;
  98. /* slots_num must be power-of-two size, otherwise
  99. * get_cmd_index is broken. */
  100. if (WARN_ON(!is_power_of_2(slots_num)))
  101. return -EINVAL;
  102. q->low_mark = q->n_window / 4;
  103. if (q->low_mark < 4)
  104. q->low_mark = 4;
  105. q->high_mark = q->n_window / 8;
  106. if (q->high_mark < 2)
  107. q->high_mark = 2;
  108. q->write_ptr = 0;
  109. q->read_ptr = 0;
  110. return 0;
  111. }
  112. static int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
  113. struct iwl_dma_ptr *ptr, size_t size)
  114. {
  115. if (WARN_ON(ptr->addr))
  116. return -EINVAL;
  117. ptr->addr = dma_alloc_coherent(trans->dev, size,
  118. &ptr->dma, GFP_KERNEL);
  119. if (!ptr->addr)
  120. return -ENOMEM;
  121. ptr->size = size;
  122. return 0;
  123. }
  124. static void iwl_pcie_free_dma_ptr(struct iwl_trans *trans,
  125. struct iwl_dma_ptr *ptr)
  126. {
  127. if (unlikely(!ptr->addr))
  128. return;
  129. dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
  130. memset(ptr, 0, sizeof(*ptr));
  131. }
  132. static void iwl_pcie_txq_stuck_timer(unsigned long data)
  133. {
  134. struct iwl_txq *txq = (void *)data;
  135. struct iwl_queue *q = &txq->q;
  136. struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
  137. struct iwl_trans *trans = iwl_trans_pcie_get_trans(trans_pcie);
  138. u32 scd_sram_addr = trans_pcie->scd_base_addr +
  139. SCD_TX_STTS_QUEUE_OFFSET(txq->q.id);
  140. u8 buf[16];
  141. int i;
  142. spin_lock(&txq->lock);
  143. /* check if triggered erroneously */
  144. if (txq->q.read_ptr == txq->q.write_ptr) {
  145. spin_unlock(&txq->lock);
  146. return;
  147. }
  148. spin_unlock(&txq->lock);
  149. IWL_ERR(trans, "Queue %d stuck for %u ms.\n", txq->q.id,
  150. jiffies_to_msecs(trans_pcie->wd_timeout));
  151. IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
  152. txq->q.read_ptr, txq->q.write_ptr);
  153. iwl_trans_read_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf));
  154. iwl_print_hex_error(trans, buf, sizeof(buf));
  155. for (i = 0; i < FH_TCSR_CHNL_NUM; i++)
  156. IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", i,
  157. iwl_read_direct32(trans, FH_TX_TRB_REG(i)));
  158. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
  159. u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(i));
  160. u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
  161. bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
  162. u32 tbl_dw =
  163. iwl_trans_read_mem32(trans,
  164. trans_pcie->scd_base_addr +
  165. SCD_TRANS_TBL_OFFSET_QUEUE(i));
  166. if (i & 0x1)
  167. tbl_dw = (tbl_dw & 0xFFFF0000) >> 16;
  168. else
  169. tbl_dw = tbl_dw & 0x0000FFFF;
  170. IWL_ERR(trans,
  171. "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n",
  172. i, active ? "" : "in", fifo, tbl_dw,
  173. iwl_read_prph(trans,
  174. SCD_QUEUE_RDPTR(i)) & (txq->q.n_bd - 1),
  175. iwl_read_prph(trans, SCD_QUEUE_WRPTR(i)));
  176. }
  177. for (i = q->read_ptr; i != q->write_ptr;
  178. i = iwl_queue_inc_wrap(i, q->n_bd))
  179. IWL_ERR(trans, "scratch %d = 0x%08x\n", i,
  180. le32_to_cpu(txq->scratchbufs[i].scratch));
  181. iwl_op_mode_nic_error(trans->op_mode);
  182. }
  183. /*
  184. * iwl_pcie_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  185. */
  186. static void iwl_pcie_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
  187. struct iwl_txq *txq, u16 byte_cnt)
  188. {
  189. struct iwlagn_scd_bc_tbl *scd_bc_tbl;
  190. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  191. int write_ptr = txq->q.write_ptr;
  192. int txq_id = txq->q.id;
  193. u8 sec_ctl = 0;
  194. u8 sta_id = 0;
  195. u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  196. __le16 bc_ent;
  197. struct iwl_tx_cmd *tx_cmd =
  198. (void *) txq->entries[txq->q.write_ptr].cmd->payload;
  199. scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  200. WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
  201. sta_id = tx_cmd->sta_id;
  202. sec_ctl = tx_cmd->sec_ctl;
  203. switch (sec_ctl & TX_CMD_SEC_MSK) {
  204. case TX_CMD_SEC_CCM:
  205. len += IEEE80211_CCMP_MIC_LEN;
  206. break;
  207. case TX_CMD_SEC_TKIP:
  208. len += IEEE80211_TKIP_ICV_LEN;
  209. break;
  210. case TX_CMD_SEC_WEP:
  211. len += IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN;
  212. break;
  213. }
  214. if (trans_pcie->bc_table_dword)
  215. len = DIV_ROUND_UP(len, 4);
  216. bc_ent = cpu_to_le16(len | (sta_id << 12));
  217. scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
  218. if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
  219. scd_bc_tbl[txq_id].
  220. tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
  221. }
  222. static void iwl_pcie_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
  223. struct iwl_txq *txq)
  224. {
  225. struct iwl_trans_pcie *trans_pcie =
  226. IWL_TRANS_GET_PCIE_TRANS(trans);
  227. struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  228. int txq_id = txq->q.id;
  229. int read_ptr = txq->q.read_ptr;
  230. u8 sta_id = 0;
  231. __le16 bc_ent;
  232. struct iwl_tx_cmd *tx_cmd =
  233. (void *)txq->entries[txq->q.read_ptr].cmd->payload;
  234. WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
  235. if (txq_id != trans_pcie->cmd_queue)
  236. sta_id = tx_cmd->sta_id;
  237. bc_ent = cpu_to_le16(1 | (sta_id << 12));
  238. scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
  239. if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
  240. scd_bc_tbl[txq_id].
  241. tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
  242. }
  243. /*
  244. * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
  245. */
  246. void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans, struct iwl_txq *txq)
  247. {
  248. u32 reg = 0;
  249. int txq_id = txq->q.id;
  250. if (txq->need_update == 0)
  251. return;
  252. if (trans->cfg->base_params->shadow_reg_enable) {
  253. /* shadow register enabled */
  254. iwl_write32(trans, HBUS_TARG_WRPTR,
  255. txq->q.write_ptr | (txq_id << 8));
  256. } else {
  257. struct iwl_trans_pcie *trans_pcie =
  258. IWL_TRANS_GET_PCIE_TRANS(trans);
  259. /* if we're trying to save power */
  260. if (test_bit(STATUS_TPOWER_PMI, &trans_pcie->status)) {
  261. /* wake up nic if it's powered down ...
  262. * uCode will wake up, and interrupt us again, so next
  263. * time we'll skip this part. */
  264. reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
  265. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  266. IWL_DEBUG_INFO(trans,
  267. "Tx queue %d requesting wakeup,"
  268. " GP1 = 0x%x\n", txq_id, reg);
  269. iwl_set_bit(trans, CSR_GP_CNTRL,
  270. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  271. return;
  272. }
  273. IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id,
  274. txq->q.write_ptr);
  275. iwl_write_direct32(trans, HBUS_TARG_WRPTR,
  276. txq->q.write_ptr | (txq_id << 8));
  277. /*
  278. * else not in power-save mode,
  279. * uCode will never sleep when we're
  280. * trying to tx (during RFKILL, we're not trying to tx).
  281. */
  282. } else
  283. iwl_write32(trans, HBUS_TARG_WRPTR,
  284. txq->q.write_ptr | (txq_id << 8));
  285. }
  286. txq->need_update = 0;
  287. }
  288. static inline dma_addr_t iwl_pcie_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
  289. {
  290. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  291. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  292. if (sizeof(dma_addr_t) > sizeof(u32))
  293. addr |=
  294. ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
  295. return addr;
  296. }
  297. static inline u16 iwl_pcie_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
  298. {
  299. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  300. return le16_to_cpu(tb->hi_n_len) >> 4;
  301. }
  302. static inline void iwl_pcie_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
  303. dma_addr_t addr, u16 len)
  304. {
  305. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  306. u16 hi_n_len = len << 4;
  307. put_unaligned_le32(addr, &tb->lo);
  308. if (sizeof(dma_addr_t) > sizeof(u32))
  309. hi_n_len |= ((addr >> 16) >> 16) & 0xF;
  310. tb->hi_n_len = cpu_to_le16(hi_n_len);
  311. tfd->num_tbs = idx + 1;
  312. }
  313. static inline u8 iwl_pcie_tfd_get_num_tbs(struct iwl_tfd *tfd)
  314. {
  315. return tfd->num_tbs & 0x1f;
  316. }
  317. static void iwl_pcie_tfd_unmap(struct iwl_trans *trans,
  318. struct iwl_cmd_meta *meta,
  319. struct iwl_tfd *tfd)
  320. {
  321. int i;
  322. int num_tbs;
  323. /* Sanity check on number of chunks */
  324. num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
  325. if (num_tbs >= IWL_NUM_OF_TBS) {
  326. IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
  327. /* @todo issue fatal error, it is quite serious situation */
  328. return;
  329. }
  330. /* first TB is never freed - it's the scratchbuf data */
  331. for (i = 1; i < num_tbs; i++)
  332. dma_unmap_single(trans->dev, iwl_pcie_tfd_tb_get_addr(tfd, i),
  333. iwl_pcie_tfd_tb_get_len(tfd, i),
  334. DMA_TO_DEVICE);
  335. tfd->num_tbs = 0;
  336. }
  337. /*
  338. * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  339. * @trans - transport private data
  340. * @txq - tx queue
  341. * @dma_dir - the direction of the DMA mapping
  342. *
  343. * Does NOT advance any TFD circular buffer read/write indexes
  344. * Does NOT free the TFD itself (which is within circular buffer)
  345. */
  346. static void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
  347. {
  348. struct iwl_tfd *tfd_tmp = txq->tfds;
  349. /* rd_ptr is bounded by n_bd and idx is bounded by n_window */
  350. int rd_ptr = txq->q.read_ptr;
  351. int idx = get_cmd_index(&txq->q, rd_ptr);
  352. lockdep_assert_held(&txq->lock);
  353. /* We have only q->n_window txq->entries, but we use q->n_bd tfds */
  354. iwl_pcie_tfd_unmap(trans, &txq->entries[idx].meta, &tfd_tmp[rd_ptr]);
  355. /* free SKB */
  356. if (txq->entries) {
  357. struct sk_buff *skb;
  358. skb = txq->entries[idx].skb;
  359. /* Can be called from irqs-disabled context
  360. * If skb is not NULL, it means that the whole queue is being
  361. * freed and that the queue is not empty - free the skb
  362. */
  363. if (skb) {
  364. iwl_op_mode_free_skb(trans->op_mode, skb);
  365. txq->entries[idx].skb = NULL;
  366. }
  367. }
  368. }
  369. static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
  370. dma_addr_t addr, u16 len, u8 reset)
  371. {
  372. struct iwl_queue *q;
  373. struct iwl_tfd *tfd, *tfd_tmp;
  374. u32 num_tbs;
  375. q = &txq->q;
  376. tfd_tmp = txq->tfds;
  377. tfd = &tfd_tmp[q->write_ptr];
  378. if (reset)
  379. memset(tfd, 0, sizeof(*tfd));
  380. num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
  381. /* Each TFD can point to a maximum 20 Tx buffers */
  382. if (num_tbs >= IWL_NUM_OF_TBS) {
  383. IWL_ERR(trans, "Error can not send more than %d chunks\n",
  384. IWL_NUM_OF_TBS);
  385. return -EINVAL;
  386. }
  387. if (WARN(addr & ~IWL_TX_DMA_MASK,
  388. "Unaligned address = %llx\n", (unsigned long long)addr))
  389. return -EINVAL;
  390. iwl_pcie_tfd_set_tb(tfd, num_tbs, addr, len);
  391. return 0;
  392. }
  393. static int iwl_pcie_txq_alloc(struct iwl_trans *trans,
  394. struct iwl_txq *txq, int slots_num,
  395. u32 txq_id)
  396. {
  397. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  398. size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
  399. size_t scratchbuf_sz;
  400. int i;
  401. if (WARN_ON(txq->entries || txq->tfds))
  402. return -EINVAL;
  403. setup_timer(&txq->stuck_timer, iwl_pcie_txq_stuck_timer,
  404. (unsigned long)txq);
  405. txq->trans_pcie = trans_pcie;
  406. txq->q.n_window = slots_num;
  407. txq->entries = kcalloc(slots_num,
  408. sizeof(struct iwl_pcie_txq_entry),
  409. GFP_KERNEL);
  410. if (!txq->entries)
  411. goto error;
  412. if (txq_id == trans_pcie->cmd_queue)
  413. for (i = 0; i < slots_num; i++) {
  414. txq->entries[i].cmd =
  415. kmalloc(sizeof(struct iwl_device_cmd),
  416. GFP_KERNEL);
  417. if (!txq->entries[i].cmd)
  418. goto error;
  419. }
  420. /* Circular buffer of transmit frame descriptors (TFDs),
  421. * shared with device */
  422. txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
  423. &txq->q.dma_addr, GFP_KERNEL);
  424. if (!txq->tfds)
  425. goto error;
  426. BUILD_BUG_ON(IWL_HCMD_SCRATCHBUF_SIZE != sizeof(*txq->scratchbufs));
  427. BUILD_BUG_ON(offsetof(struct iwl_pcie_txq_scratch_buf, scratch) !=
  428. sizeof(struct iwl_cmd_header) +
  429. offsetof(struct iwl_tx_cmd, scratch));
  430. scratchbuf_sz = sizeof(*txq->scratchbufs) * slots_num;
  431. txq->scratchbufs = dma_alloc_coherent(trans->dev, scratchbuf_sz,
  432. &txq->scratchbufs_dma,
  433. GFP_KERNEL);
  434. if (!txq->scratchbufs)
  435. goto err_free_tfds;
  436. txq->q.id = txq_id;
  437. return 0;
  438. err_free_tfds:
  439. dma_free_coherent(trans->dev, tfd_sz, txq->tfds, txq->q.dma_addr);
  440. error:
  441. if (txq->entries && txq_id == trans_pcie->cmd_queue)
  442. for (i = 0; i < slots_num; i++)
  443. kfree(txq->entries[i].cmd);
  444. kfree(txq->entries);
  445. txq->entries = NULL;
  446. return -ENOMEM;
  447. }
  448. static int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
  449. int slots_num, u32 txq_id)
  450. {
  451. int ret;
  452. txq->need_update = 0;
  453. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  454. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  455. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  456. /* Initialize queue's high/low-water marks, and head/tail indexes */
  457. ret = iwl_queue_init(&txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
  458. txq_id);
  459. if (ret)
  460. return ret;
  461. spin_lock_init(&txq->lock);
  462. /*
  463. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  464. * given Tx queue, and enable the DMA channel used for that queue.
  465. * Circular buffer (TFD queue in DRAM) physical base address */
  466. iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
  467. txq->q.dma_addr >> 8);
  468. return 0;
  469. }
  470. /*
  471. * iwl_pcie_txq_unmap - Unmap any remaining DMA mappings and free skb's
  472. */
  473. static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
  474. {
  475. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  476. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  477. struct iwl_queue *q = &txq->q;
  478. if (!q->n_bd)
  479. return;
  480. spin_lock_bh(&txq->lock);
  481. while (q->write_ptr != q->read_ptr) {
  482. IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
  483. txq_id, q->read_ptr);
  484. iwl_pcie_txq_free_tfd(trans, txq);
  485. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  486. }
  487. txq->active = false;
  488. spin_unlock_bh(&txq->lock);
  489. /* just in case - this queue may have been stopped */
  490. iwl_wake_queue(trans, txq);
  491. }
  492. /*
  493. * iwl_pcie_txq_free - Deallocate DMA queue.
  494. * @txq: Transmit queue to deallocate.
  495. *
  496. * Empty queue by removing and destroying all BD's.
  497. * Free all buffers.
  498. * 0-fill, but do not free "txq" descriptor structure.
  499. */
  500. static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
  501. {
  502. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  503. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  504. struct device *dev = trans->dev;
  505. int i;
  506. if (WARN_ON(!txq))
  507. return;
  508. iwl_pcie_txq_unmap(trans, txq_id);
  509. /* De-alloc array of command/tx buffers */
  510. if (txq_id == trans_pcie->cmd_queue)
  511. for (i = 0; i < txq->q.n_window; i++) {
  512. kfree(txq->entries[i].cmd);
  513. kfree(txq->entries[i].free_buf);
  514. }
  515. /* De-alloc circular buffer of TFDs */
  516. if (txq->q.n_bd) {
  517. dma_free_coherent(dev, sizeof(struct iwl_tfd) *
  518. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  519. txq->q.dma_addr = 0;
  520. dma_free_coherent(dev,
  521. sizeof(*txq->scratchbufs) * txq->q.n_window,
  522. txq->scratchbufs, txq->scratchbufs_dma);
  523. }
  524. kfree(txq->entries);
  525. txq->entries = NULL;
  526. del_timer_sync(&txq->stuck_timer);
  527. /* 0-fill queue descriptor structure */
  528. memset(txq, 0, sizeof(*txq));
  529. }
  530. /*
  531. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  532. */
  533. static void iwl_pcie_txq_set_sched(struct iwl_trans *trans, u32 mask)
  534. {
  535. struct iwl_trans_pcie __maybe_unused *trans_pcie =
  536. IWL_TRANS_GET_PCIE_TRANS(trans);
  537. iwl_write_prph(trans, SCD_TXFACT, mask);
  538. }
  539. void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
  540. {
  541. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  542. int nq = trans->cfg->base_params->num_of_queues;
  543. int chan;
  544. u32 reg_val;
  545. int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
  546. SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
  547. /* make sure all queue are not stopped/used */
  548. memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
  549. memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
  550. trans_pcie->scd_base_addr =
  551. iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
  552. WARN_ON(scd_base_addr != 0 &&
  553. scd_base_addr != trans_pcie->scd_base_addr);
  554. /* reset context data, TX status and translation data */
  555. iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
  556. SCD_CONTEXT_MEM_LOWER_BOUND,
  557. NULL, clear_dwords);
  558. iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
  559. trans_pcie->scd_bc_tbls.dma >> 10);
  560. /* The chain extension of the SCD doesn't work well. This feature is
  561. * enabled by default by the HW, so we need to disable it manually.
  562. */
  563. iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
  564. iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue,
  565. trans_pcie->cmd_fifo);
  566. /* Activate all Tx DMA/FIFO channels */
  567. iwl_pcie_txq_set_sched(trans, IWL_MASK(0, 7));
  568. /* Enable DMA channel */
  569. for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
  570. iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  571. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  572. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  573. /* Update FH chicken bits */
  574. reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
  575. iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
  576. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  577. /* Enable L1-Active */
  578. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  579. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  580. }
  581. void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
  582. {
  583. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  584. int txq_id;
  585. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  586. txq_id++) {
  587. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  588. iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
  589. txq->q.dma_addr >> 8);
  590. iwl_pcie_txq_unmap(trans, txq_id);
  591. txq->q.read_ptr = 0;
  592. txq->q.write_ptr = 0;
  593. }
  594. /* Tell NIC where to find the "keep warm" buffer */
  595. iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
  596. trans_pcie->kw.dma >> 4);
  597. iwl_pcie_tx_start(trans, trans_pcie->scd_base_addr);
  598. }
  599. /*
  600. * iwl_pcie_tx_stop - Stop all Tx DMA channels
  601. */
  602. int iwl_pcie_tx_stop(struct iwl_trans *trans)
  603. {
  604. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  605. int ch, txq_id, ret;
  606. unsigned long flags;
  607. /* Turn off all Tx DMA fifos */
  608. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  609. iwl_pcie_txq_set_sched(trans, 0);
  610. /* Stop each Tx DMA channel, and wait for it to be idle */
  611. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  612. iwl_write_direct32(trans,
  613. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  614. ret = iwl_poll_direct_bit(trans, FH_TSSR_TX_STATUS_REG,
  615. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000);
  616. if (ret < 0)
  617. IWL_ERR(trans,
  618. "Failing on timeout while stopping DMA channel %d [0x%08x]\n",
  619. ch,
  620. iwl_read_direct32(trans,
  621. FH_TSSR_TX_STATUS_REG));
  622. }
  623. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  624. if (!trans_pcie->txq) {
  625. IWL_WARN(trans,
  626. "Stopping tx queues that aren't allocated...\n");
  627. return 0;
  628. }
  629. /* Unmap DMA from host system and free skb's */
  630. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  631. txq_id++)
  632. iwl_pcie_txq_unmap(trans, txq_id);
  633. return 0;
  634. }
  635. /*
  636. * iwl_trans_tx_free - Free TXQ Context
  637. *
  638. * Destroy all TX DMA queues and structures
  639. */
  640. void iwl_pcie_tx_free(struct iwl_trans *trans)
  641. {
  642. int txq_id;
  643. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  644. /* Tx queues */
  645. if (trans_pcie->txq) {
  646. for (txq_id = 0;
  647. txq_id < trans->cfg->base_params->num_of_queues; txq_id++)
  648. iwl_pcie_txq_free(trans, txq_id);
  649. }
  650. kfree(trans_pcie->txq);
  651. trans_pcie->txq = NULL;
  652. iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
  653. iwl_pcie_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
  654. }
  655. /*
  656. * iwl_pcie_tx_alloc - allocate TX context
  657. * Allocate all Tx DMA structures and initialize them
  658. */
  659. static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
  660. {
  661. int ret;
  662. int txq_id, slots_num;
  663. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  664. u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues *
  665. sizeof(struct iwlagn_scd_bc_tbl);
  666. /*It is not allowed to alloc twice, so warn when this happens.
  667. * We cannot rely on the previous allocation, so free and fail */
  668. if (WARN_ON(trans_pcie->txq)) {
  669. ret = -EINVAL;
  670. goto error;
  671. }
  672. ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
  673. scd_bc_tbls_size);
  674. if (ret) {
  675. IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
  676. goto error;
  677. }
  678. /* Alloc keep-warm buffer */
  679. ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
  680. if (ret) {
  681. IWL_ERR(trans, "Keep Warm allocation failed\n");
  682. goto error;
  683. }
  684. trans_pcie->txq = kcalloc(trans->cfg->base_params->num_of_queues,
  685. sizeof(struct iwl_txq), GFP_KERNEL);
  686. if (!trans_pcie->txq) {
  687. IWL_ERR(trans, "Not enough memory for txq\n");
  688. ret = -ENOMEM;
  689. goto error;
  690. }
  691. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  692. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  693. txq_id++) {
  694. slots_num = (txq_id == trans_pcie->cmd_queue) ?
  695. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  696. ret = iwl_pcie_txq_alloc(trans, &trans_pcie->txq[txq_id],
  697. slots_num, txq_id);
  698. if (ret) {
  699. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  700. goto error;
  701. }
  702. }
  703. return 0;
  704. error:
  705. iwl_pcie_tx_free(trans);
  706. return ret;
  707. }
  708. int iwl_pcie_tx_init(struct iwl_trans *trans)
  709. {
  710. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  711. int ret;
  712. int txq_id, slots_num;
  713. unsigned long flags;
  714. bool alloc = false;
  715. if (!trans_pcie->txq) {
  716. ret = iwl_pcie_tx_alloc(trans);
  717. if (ret)
  718. goto error;
  719. alloc = true;
  720. }
  721. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  722. /* Turn off all Tx DMA fifos */
  723. iwl_write_prph(trans, SCD_TXFACT, 0);
  724. /* Tell NIC where to find the "keep warm" buffer */
  725. iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
  726. trans_pcie->kw.dma >> 4);
  727. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  728. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  729. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  730. txq_id++) {
  731. slots_num = (txq_id == trans_pcie->cmd_queue) ?
  732. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  733. ret = iwl_pcie_txq_init(trans, &trans_pcie->txq[txq_id],
  734. slots_num, txq_id);
  735. if (ret) {
  736. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  737. goto error;
  738. }
  739. }
  740. return 0;
  741. error:
  742. /*Upon error, free only if we allocated something */
  743. if (alloc)
  744. iwl_pcie_tx_free(trans);
  745. return ret;
  746. }
  747. static inline void iwl_pcie_txq_progress(struct iwl_trans_pcie *trans_pcie,
  748. struct iwl_txq *txq)
  749. {
  750. if (!trans_pcie->wd_timeout)
  751. return;
  752. /*
  753. * if empty delete timer, otherwise move timer forward
  754. * since we're making progress on this queue
  755. */
  756. if (txq->q.read_ptr == txq->q.write_ptr)
  757. del_timer(&txq->stuck_timer);
  758. else
  759. mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
  760. }
  761. /* Frees buffers until index _not_ inclusive */
  762. void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
  763. struct sk_buff_head *skbs)
  764. {
  765. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  766. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  767. /* n_bd is usually 256 => n_bd - 1 = 0xff */
  768. int tfd_num = ssn & (txq->q.n_bd - 1);
  769. struct iwl_queue *q = &txq->q;
  770. int last_to_free;
  771. /* This function is not meant to release cmd queue*/
  772. if (WARN_ON(txq_id == trans_pcie->cmd_queue))
  773. return;
  774. spin_lock_bh(&txq->lock);
  775. if (!txq->active) {
  776. IWL_DEBUG_TX_QUEUES(trans, "Q %d inactive - ignoring idx %d\n",
  777. txq_id, ssn);
  778. goto out;
  779. }
  780. if (txq->q.read_ptr == tfd_num)
  781. goto out;
  782. IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
  783. txq_id, txq->q.read_ptr, tfd_num, ssn);
  784. /*Since we free until index _not_ inclusive, the one before index is
  785. * the last we will free. This one must be used */
  786. last_to_free = iwl_queue_dec_wrap(tfd_num, q->n_bd);
  787. if (!iwl_queue_used(q, last_to_free)) {
  788. IWL_ERR(trans,
  789. "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
  790. __func__, txq_id, last_to_free, q->n_bd,
  791. q->write_ptr, q->read_ptr);
  792. goto out;
  793. }
  794. if (WARN_ON(!skb_queue_empty(skbs)))
  795. goto out;
  796. for (;
  797. q->read_ptr != tfd_num;
  798. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  799. if (WARN_ON_ONCE(txq->entries[txq->q.read_ptr].skb == NULL))
  800. continue;
  801. __skb_queue_tail(skbs, txq->entries[txq->q.read_ptr].skb);
  802. txq->entries[txq->q.read_ptr].skb = NULL;
  803. iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq);
  804. iwl_pcie_txq_free_tfd(trans, txq);
  805. }
  806. iwl_pcie_txq_progress(trans_pcie, txq);
  807. if (iwl_queue_space(&txq->q) > txq->q.low_mark)
  808. iwl_wake_queue(trans, txq);
  809. out:
  810. spin_unlock_bh(&txq->lock);
  811. }
  812. /*
  813. * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
  814. *
  815. * When FW advances 'R' index, all entries between old and new 'R' index
  816. * need to be reclaimed. As result, some free space forms. If there is
  817. * enough free space (> low mark), wake the stack that feeds us.
  818. */
  819. static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
  820. {
  821. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  822. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  823. struct iwl_queue *q = &txq->q;
  824. int nfreed = 0;
  825. lockdep_assert_held(&txq->lock);
  826. if ((idx >= q->n_bd) || (!iwl_queue_used(q, idx))) {
  827. IWL_ERR(trans,
  828. "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
  829. __func__, txq_id, idx, q->n_bd,
  830. q->write_ptr, q->read_ptr);
  831. return;
  832. }
  833. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  834. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  835. if (nfreed++ > 0) {
  836. IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
  837. idx, q->write_ptr, q->read_ptr);
  838. iwl_op_mode_nic_error(trans->op_mode);
  839. }
  840. }
  841. iwl_pcie_txq_progress(trans_pcie, txq);
  842. }
  843. static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
  844. u16 txq_id)
  845. {
  846. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  847. u32 tbl_dw_addr;
  848. u32 tbl_dw;
  849. u16 scd_q2ratid;
  850. scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  851. tbl_dw_addr = trans_pcie->scd_base_addr +
  852. SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
  853. tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
  854. if (txq_id & 0x1)
  855. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  856. else
  857. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  858. iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
  859. return 0;
  860. }
  861. static inline void iwl_pcie_txq_set_inactive(struct iwl_trans *trans,
  862. u16 txq_id)
  863. {
  864. /* Simply stop the queue, but don't change any configuration;
  865. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  866. iwl_write_prph(trans,
  867. SCD_QUEUE_STATUS_BITS(txq_id),
  868. (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  869. (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  870. }
  871. /* Receiver address (actually, Rx station's index into station table),
  872. * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
  873. #define BUILD_RAxTID(sta_id, tid) (((sta_id) << 4) + (tid))
  874. void iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, int fifo,
  875. int sta_id, int tid, int frame_limit, u16 ssn)
  876. {
  877. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  878. if (test_and_set_bit(txq_id, trans_pcie->queue_used))
  879. WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
  880. /* Stop this Tx queue before configuring it */
  881. iwl_pcie_txq_set_inactive(trans, txq_id);
  882. /* Set this queue as a chain-building queue unless it is CMD queue */
  883. if (txq_id != trans_pcie->cmd_queue)
  884. iwl_set_bits_prph(trans, SCD_QUEUECHAIN_SEL, BIT(txq_id));
  885. /* If this queue is mapped to a certain station: it is an AGG queue */
  886. if (sta_id >= 0) {
  887. u16 ra_tid = BUILD_RAxTID(sta_id, tid);
  888. /* Map receiver-address / traffic-ID to this queue */
  889. iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
  890. /* enable aggregations for the queue */
  891. iwl_set_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
  892. trans_pcie->txq[txq_id].ampdu = true;
  893. } else {
  894. /*
  895. * disable aggregations for the queue, this will also make the
  896. * ra_tid mapping configuration irrelevant since it is now a
  897. * non-AGG queue.
  898. */
  899. iwl_clear_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
  900. ssn = trans_pcie->txq[txq_id].q.read_ptr;
  901. }
  902. /* Place first TFD at index corresponding to start sequence number.
  903. * Assumes that ssn_idx is valid (!= 0xFFF) */
  904. trans_pcie->txq[txq_id].q.read_ptr = (ssn & 0xff);
  905. trans_pcie->txq[txq_id].q.write_ptr = (ssn & 0xff);
  906. iwl_write_direct32(trans, HBUS_TARG_WRPTR,
  907. (ssn & 0xff) | (txq_id << 8));
  908. iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
  909. /* Set up Tx window size and frame limit for this queue */
  910. iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
  911. SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
  912. iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
  913. SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  914. ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  915. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  916. ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  917. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  918. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  919. iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
  920. (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  921. (fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
  922. (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
  923. SCD_QUEUE_STTS_REG_MSK);
  924. trans_pcie->txq[txq_id].active = true;
  925. IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d on FIFO %d WrPtr: %d\n",
  926. txq_id, fifo, ssn & 0xff);
  927. }
  928. void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id)
  929. {
  930. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  931. u32 stts_addr = trans_pcie->scd_base_addr +
  932. SCD_TX_STTS_QUEUE_OFFSET(txq_id);
  933. static const u32 zero_val[4] = {};
  934. if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
  935. WARN_ONCE(1, "queue %d not used", txq_id);
  936. return;
  937. }
  938. iwl_pcie_txq_set_inactive(trans, txq_id);
  939. iwl_trans_write_mem(trans, stts_addr, (void *)zero_val,
  940. ARRAY_SIZE(zero_val));
  941. iwl_pcie_txq_unmap(trans, txq_id);
  942. trans_pcie->txq[txq_id].ampdu = false;
  943. IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
  944. }
  945. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  946. /*
  947. * iwl_pcie_enqueue_hcmd - enqueue a uCode command
  948. * @priv: device private data point
  949. * @cmd: a pointer to the ucode command structure
  950. *
  951. * The function returns < 0 values to indicate the operation
  952. * failed. On success, it returns the index (>= 0) of command in the
  953. * command queue.
  954. */
  955. static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
  956. struct iwl_host_cmd *cmd)
  957. {
  958. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  959. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  960. struct iwl_queue *q = &txq->q;
  961. struct iwl_device_cmd *out_cmd;
  962. struct iwl_cmd_meta *out_meta;
  963. void *dup_buf = NULL;
  964. dma_addr_t phys_addr;
  965. int idx;
  966. u16 copy_size, cmd_size, scratch_size;
  967. bool had_nocopy = false;
  968. int i;
  969. u32 cmd_pos;
  970. const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
  971. u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
  972. copy_size = sizeof(out_cmd->hdr);
  973. cmd_size = sizeof(out_cmd->hdr);
  974. /* need one for the header if the first is NOCOPY */
  975. BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
  976. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  977. cmddata[i] = cmd->data[i];
  978. cmdlen[i] = cmd->len[i];
  979. if (!cmd->len[i])
  980. continue;
  981. /* need at least IWL_HCMD_SCRATCHBUF_SIZE copied */
  982. if (copy_size < IWL_HCMD_SCRATCHBUF_SIZE) {
  983. int copy = IWL_HCMD_SCRATCHBUF_SIZE - copy_size;
  984. if (copy > cmdlen[i])
  985. copy = cmdlen[i];
  986. cmdlen[i] -= copy;
  987. cmddata[i] += copy;
  988. copy_size += copy;
  989. }
  990. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  991. had_nocopy = true;
  992. if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
  993. idx = -EINVAL;
  994. goto free_dup_buf;
  995. }
  996. } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
  997. /*
  998. * This is also a chunk that isn't copied
  999. * to the static buffer so set had_nocopy.
  1000. */
  1001. had_nocopy = true;
  1002. /* only allowed once */
  1003. if (WARN_ON(dup_buf)) {
  1004. idx = -EINVAL;
  1005. goto free_dup_buf;
  1006. }
  1007. dup_buf = kmemdup(cmddata[i], cmdlen[i],
  1008. GFP_ATOMIC);
  1009. if (!dup_buf)
  1010. return -ENOMEM;
  1011. } else {
  1012. /* NOCOPY must not be followed by normal! */
  1013. if (WARN_ON(had_nocopy)) {
  1014. idx = -EINVAL;
  1015. goto free_dup_buf;
  1016. }
  1017. copy_size += cmdlen[i];
  1018. }
  1019. cmd_size += cmd->len[i];
  1020. }
  1021. /*
  1022. * If any of the command structures end up being larger than
  1023. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  1024. * allocated into separate TFDs, then we will need to
  1025. * increase the size of the buffers.
  1026. */
  1027. if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
  1028. "Command %s (%#x) is too large (%d bytes)\n",
  1029. get_cmd_string(trans_pcie, cmd->id), cmd->id, copy_size)) {
  1030. idx = -EINVAL;
  1031. goto free_dup_buf;
  1032. }
  1033. spin_lock_bh(&txq->lock);
  1034. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  1035. spin_unlock_bh(&txq->lock);
  1036. IWL_ERR(trans, "No space in command queue\n");
  1037. iwl_op_mode_cmd_queue_full(trans->op_mode);
  1038. idx = -ENOSPC;
  1039. goto free_dup_buf;
  1040. }
  1041. idx = get_cmd_index(q, q->write_ptr);
  1042. out_cmd = txq->entries[idx].cmd;
  1043. out_meta = &txq->entries[idx].meta;
  1044. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  1045. if (cmd->flags & CMD_WANT_SKB)
  1046. out_meta->source = cmd;
  1047. /* set up the header */
  1048. out_cmd->hdr.cmd = cmd->id;
  1049. out_cmd->hdr.flags = 0;
  1050. out_cmd->hdr.sequence =
  1051. cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
  1052. INDEX_TO_SEQ(q->write_ptr));
  1053. /* and copy the data that needs to be copied */
  1054. cmd_pos = offsetof(struct iwl_device_cmd, payload);
  1055. copy_size = sizeof(out_cmd->hdr);
  1056. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1057. int copy = 0;
  1058. if (!cmd->len[i])
  1059. continue;
  1060. /* need at least IWL_HCMD_SCRATCHBUF_SIZE copied */
  1061. if (copy_size < IWL_HCMD_SCRATCHBUF_SIZE) {
  1062. copy = IWL_HCMD_SCRATCHBUF_SIZE - copy_size;
  1063. if (copy > cmd->len[i])
  1064. copy = cmd->len[i];
  1065. }
  1066. /* copy everything if not nocopy/dup */
  1067. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  1068. IWL_HCMD_DFL_DUP)))
  1069. copy = cmd->len[i];
  1070. if (copy) {
  1071. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
  1072. cmd_pos += copy;
  1073. copy_size += copy;
  1074. }
  1075. }
  1076. IWL_DEBUG_HC(trans,
  1077. "Sending command %s (#%x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
  1078. get_cmd_string(trans_pcie, out_cmd->hdr.cmd),
  1079. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  1080. cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue);
  1081. /* start the TFD with the scratchbuf */
  1082. scratch_size = min_t(int, copy_size, IWL_HCMD_SCRATCHBUF_SIZE);
  1083. memcpy(&txq->scratchbufs[q->write_ptr], &out_cmd->hdr, scratch_size);
  1084. iwl_pcie_txq_build_tfd(trans, txq,
  1085. iwl_pcie_get_scratchbuf_dma(txq, q->write_ptr),
  1086. scratch_size, 1);
  1087. /* map first command fragment, if any remains */
  1088. if (copy_size > scratch_size) {
  1089. phys_addr = dma_map_single(trans->dev,
  1090. ((u8 *)&out_cmd->hdr) + scratch_size,
  1091. copy_size - scratch_size,
  1092. DMA_TO_DEVICE);
  1093. if (dma_mapping_error(trans->dev, phys_addr)) {
  1094. iwl_pcie_tfd_unmap(trans, out_meta,
  1095. &txq->tfds[q->write_ptr]);
  1096. idx = -ENOMEM;
  1097. goto out;
  1098. }
  1099. iwl_pcie_txq_build_tfd(trans, txq, phys_addr,
  1100. copy_size - scratch_size, 0);
  1101. }
  1102. /* map the remaining (adjusted) nocopy/dup fragments */
  1103. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1104. const void *data = cmddata[i];
  1105. if (!cmdlen[i])
  1106. continue;
  1107. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  1108. IWL_HCMD_DFL_DUP)))
  1109. continue;
  1110. if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
  1111. data = dup_buf;
  1112. phys_addr = dma_map_single(trans->dev, (void *)data,
  1113. cmdlen[i], DMA_TO_DEVICE);
  1114. if (dma_mapping_error(trans->dev, phys_addr)) {
  1115. iwl_pcie_tfd_unmap(trans, out_meta,
  1116. &txq->tfds[q->write_ptr]);
  1117. idx = -ENOMEM;
  1118. goto out;
  1119. }
  1120. iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], 0);
  1121. }
  1122. out_meta->flags = cmd->flags;
  1123. if (WARN_ON_ONCE(txq->entries[idx].free_buf))
  1124. kfree(txq->entries[idx].free_buf);
  1125. txq->entries[idx].free_buf = dup_buf;
  1126. txq->need_update = 1;
  1127. trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr);
  1128. /* start timer if queue currently empty */
  1129. if (q->read_ptr == q->write_ptr && trans_pcie->wd_timeout)
  1130. mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
  1131. /* Increment and update queue's write index */
  1132. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  1133. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1134. out:
  1135. spin_unlock_bh(&txq->lock);
  1136. free_dup_buf:
  1137. if (idx < 0)
  1138. kfree(dup_buf);
  1139. return idx;
  1140. }
  1141. /*
  1142. * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
  1143. * @rxb: Rx buffer to reclaim
  1144. * @handler_status: return value of the handler of the command
  1145. * (put in setup_rx_handlers)
  1146. *
  1147. * If an Rx buffer has an async callback associated with it the callback
  1148. * will be executed. The attached skb (if present) will only be freed
  1149. * if the callback returns 1
  1150. */
  1151. void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
  1152. struct iwl_rx_cmd_buffer *rxb, int handler_status)
  1153. {
  1154. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1155. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1156. int txq_id = SEQ_TO_QUEUE(sequence);
  1157. int index = SEQ_TO_INDEX(sequence);
  1158. int cmd_index;
  1159. struct iwl_device_cmd *cmd;
  1160. struct iwl_cmd_meta *meta;
  1161. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1162. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  1163. /* If a Tx command is being handled and it isn't in the actual
  1164. * command queue then there a command routing bug has been introduced
  1165. * in the queue management code. */
  1166. if (WARN(txq_id != trans_pcie->cmd_queue,
  1167. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  1168. txq_id, trans_pcie->cmd_queue, sequence,
  1169. trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr,
  1170. trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) {
  1171. iwl_print_hex_error(trans, pkt, 32);
  1172. return;
  1173. }
  1174. spin_lock_bh(&txq->lock);
  1175. cmd_index = get_cmd_index(&txq->q, index);
  1176. cmd = txq->entries[cmd_index].cmd;
  1177. meta = &txq->entries[cmd_index].meta;
  1178. iwl_pcie_tfd_unmap(trans, meta, &txq->tfds[index]);
  1179. /* Input error checking is done when commands are added to queue. */
  1180. if (meta->flags & CMD_WANT_SKB) {
  1181. struct page *p = rxb_steal_page(rxb);
  1182. meta->source->resp_pkt = pkt;
  1183. meta->source->_rx_page_addr = (unsigned long)page_address(p);
  1184. meta->source->_rx_page_order = trans_pcie->rx_page_order;
  1185. meta->source->handler_status = handler_status;
  1186. }
  1187. iwl_pcie_cmdq_reclaim(trans, txq_id, index);
  1188. if (!(meta->flags & CMD_ASYNC)) {
  1189. if (!test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
  1190. IWL_WARN(trans,
  1191. "HCMD_ACTIVE already clear for command %s\n",
  1192. get_cmd_string(trans_pcie, cmd->hdr.cmd));
  1193. }
  1194. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  1195. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  1196. get_cmd_string(trans_pcie, cmd->hdr.cmd));
  1197. wake_up(&trans_pcie->wait_command_queue);
  1198. }
  1199. meta->flags = 0;
  1200. spin_unlock_bh(&txq->lock);
  1201. }
  1202. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  1203. #define COMMAND_POKE_TIMEOUT (HZ / 10)
  1204. static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
  1205. struct iwl_host_cmd *cmd)
  1206. {
  1207. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1208. int ret;
  1209. /* An asynchronous command can not expect an SKB to be set. */
  1210. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  1211. return -EINVAL;
  1212. ret = iwl_pcie_enqueue_hcmd(trans, cmd);
  1213. if (ret < 0) {
  1214. IWL_ERR(trans,
  1215. "Error sending %s: enqueue_hcmd failed: %d\n",
  1216. get_cmd_string(trans_pcie, cmd->id), ret);
  1217. return ret;
  1218. }
  1219. return 0;
  1220. }
  1221. static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
  1222. struct iwl_host_cmd *cmd)
  1223. {
  1224. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1225. int cmd_idx;
  1226. int ret;
  1227. int timeout = HOST_COMPLETE_TIMEOUT;
  1228. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
  1229. get_cmd_string(trans_pcie, cmd->id));
  1230. if (WARN_ON(test_and_set_bit(STATUS_HCMD_ACTIVE,
  1231. &trans_pcie->status))) {
  1232. IWL_ERR(trans, "Command %s: a command is already active!\n",
  1233. get_cmd_string(trans_pcie, cmd->id));
  1234. return -EIO;
  1235. }
  1236. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
  1237. get_cmd_string(trans_pcie, cmd->id));
  1238. cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
  1239. if (cmd_idx < 0) {
  1240. ret = cmd_idx;
  1241. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  1242. IWL_ERR(trans,
  1243. "Error sending %s: enqueue_hcmd failed: %d\n",
  1244. get_cmd_string(trans_pcie, cmd->id), ret);
  1245. return ret;
  1246. }
  1247. while (timeout > 0) {
  1248. unsigned long flags;
  1249. timeout -= COMMAND_POKE_TIMEOUT;
  1250. ret = wait_event_timeout(trans_pcie->wait_command_queue,
  1251. !test_bit(STATUS_HCMD_ACTIVE,
  1252. &trans_pcie->status),
  1253. COMMAND_POKE_TIMEOUT);
  1254. if (ret)
  1255. break;
  1256. /* poke the device - it may have lost the command */
  1257. if (iwl_trans_grab_nic_access(trans, true, &flags)) {
  1258. iwl_trans_release_nic_access(trans, &flags);
  1259. IWL_DEBUG_INFO(trans,
  1260. "Tried to wake NIC for command %s\n",
  1261. get_cmd_string(trans_pcie, cmd->id));
  1262. } else {
  1263. IWL_ERR(trans, "Failed to poke NIC for command %s\n",
  1264. get_cmd_string(trans_pcie, cmd->id));
  1265. break;
  1266. }
  1267. }
  1268. if (!ret) {
  1269. if (test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
  1270. struct iwl_txq *txq =
  1271. &trans_pcie->txq[trans_pcie->cmd_queue];
  1272. struct iwl_queue *q = &txq->q;
  1273. IWL_ERR(trans,
  1274. "Error sending %s: time out after %dms.\n",
  1275. get_cmd_string(trans_pcie, cmd->id),
  1276. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  1277. IWL_ERR(trans,
  1278. "Current CMD queue read_ptr %d write_ptr %d\n",
  1279. q->read_ptr, q->write_ptr);
  1280. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  1281. IWL_DEBUG_INFO(trans,
  1282. "Clearing HCMD_ACTIVE for command %s\n",
  1283. get_cmd_string(trans_pcie, cmd->id));
  1284. ret = -ETIMEDOUT;
  1285. goto cancel;
  1286. }
  1287. }
  1288. if (test_bit(STATUS_FW_ERROR, &trans_pcie->status)) {
  1289. IWL_ERR(trans, "FW error in SYNC CMD %s\n",
  1290. get_cmd_string(trans_pcie, cmd->id));
  1291. dump_stack();
  1292. ret = -EIO;
  1293. goto cancel;
  1294. }
  1295. if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  1296. test_bit(STATUS_RFKILL, &trans_pcie->status)) {
  1297. IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
  1298. ret = -ERFKILL;
  1299. goto cancel;
  1300. }
  1301. if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
  1302. IWL_ERR(trans, "Error: Response NULL in '%s'\n",
  1303. get_cmd_string(trans_pcie, cmd->id));
  1304. ret = -EIO;
  1305. goto cancel;
  1306. }
  1307. return 0;
  1308. cancel:
  1309. if (cmd->flags & CMD_WANT_SKB) {
  1310. /*
  1311. * Cancel the CMD_WANT_SKB flag for the cmd in the
  1312. * TX cmd queue. Otherwise in case the cmd comes
  1313. * in later, it will possibly set an invalid
  1314. * address (cmd->meta.source).
  1315. */
  1316. trans_pcie->txq[trans_pcie->cmd_queue].
  1317. entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
  1318. }
  1319. if (cmd->resp_pkt) {
  1320. iwl_free_resp(cmd);
  1321. cmd->resp_pkt = NULL;
  1322. }
  1323. return ret;
  1324. }
  1325. int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  1326. {
  1327. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1328. if (test_bit(STATUS_FW_ERROR, &trans_pcie->status))
  1329. return -EIO;
  1330. if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  1331. test_bit(STATUS_RFKILL, &trans_pcie->status)) {
  1332. IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
  1333. cmd->id);
  1334. return -ERFKILL;
  1335. }
  1336. if (cmd->flags & CMD_ASYNC)
  1337. return iwl_pcie_send_hcmd_async(trans, cmd);
  1338. /* We still can fail on RFKILL that can be asserted while we wait */
  1339. return iwl_pcie_send_hcmd_sync(trans, cmd);
  1340. }
  1341. int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  1342. struct iwl_device_cmd *dev_cmd, int txq_id)
  1343. {
  1344. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1345. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1346. struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  1347. struct iwl_cmd_meta *out_meta;
  1348. struct iwl_txq *txq;
  1349. struct iwl_queue *q;
  1350. dma_addr_t tb0_phys, tb1_phys, scratch_phys;
  1351. void *tb1_addr;
  1352. u16 len, tb1_len, tb2_len;
  1353. u8 wait_write_ptr = 0;
  1354. __le16 fc = hdr->frame_control;
  1355. u8 hdr_len = ieee80211_hdrlen(fc);
  1356. u16 wifi_seq;
  1357. txq = &trans_pcie->txq[txq_id];
  1358. q = &txq->q;
  1359. if (WARN_ONCE(!test_bit(txq_id, trans_pcie->queue_used),
  1360. "TX on unused queue %d\n", txq_id))
  1361. return -EINVAL;
  1362. spin_lock(&txq->lock);
  1363. /* In AGG mode, the index in the ring must correspond to the WiFi
  1364. * sequence number. This is a HW requirements to help the SCD to parse
  1365. * the BA.
  1366. * Check here that the packets are in the right place on the ring.
  1367. */
  1368. wifi_seq = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  1369. WARN_ONCE(txq->ampdu &&
  1370. (wifi_seq & 0xff) != q->write_ptr,
  1371. "Q: %d WiFi Seq %d tfdNum %d",
  1372. txq_id, wifi_seq, q->write_ptr);
  1373. /* Set up driver data for this TFD */
  1374. txq->entries[q->write_ptr].skb = skb;
  1375. txq->entries[q->write_ptr].cmd = dev_cmd;
  1376. dev_cmd->hdr.cmd = REPLY_TX;
  1377. dev_cmd->hdr.sequence =
  1378. cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  1379. INDEX_TO_SEQ(q->write_ptr)));
  1380. tb0_phys = iwl_pcie_get_scratchbuf_dma(txq, q->write_ptr);
  1381. scratch_phys = tb0_phys + sizeof(struct iwl_cmd_header) +
  1382. offsetof(struct iwl_tx_cmd, scratch);
  1383. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  1384. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  1385. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  1386. out_meta = &txq->entries[q->write_ptr].meta;
  1387. /*
  1388. * The second TB (tb1) points to the remainder of the TX command
  1389. * and the 802.11 header - dword aligned size
  1390. * (This calculation modifies the TX command, so do it before the
  1391. * setup of the first TB)
  1392. */
  1393. len = sizeof(struct iwl_tx_cmd) + sizeof(struct iwl_cmd_header) +
  1394. hdr_len - IWL_HCMD_SCRATCHBUF_SIZE;
  1395. tb1_len = ALIGN(len, 4);
  1396. /* Tell NIC about any 2-byte padding after MAC header */
  1397. if (tb1_len != len)
  1398. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  1399. /* The first TB points to the scratchbuf data - min_copy bytes */
  1400. memcpy(&txq->scratchbufs[q->write_ptr], &dev_cmd->hdr,
  1401. IWL_HCMD_SCRATCHBUF_SIZE);
  1402. iwl_pcie_txq_build_tfd(trans, txq, tb0_phys,
  1403. IWL_HCMD_SCRATCHBUF_SIZE, 1);
  1404. /* there must be data left over for TB1 or this code must be changed */
  1405. BUILD_BUG_ON(sizeof(struct iwl_tx_cmd) < IWL_HCMD_SCRATCHBUF_SIZE);
  1406. /* map the data for TB1 */
  1407. tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_HCMD_SCRATCHBUF_SIZE;
  1408. tb1_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
  1409. if (unlikely(dma_mapping_error(trans->dev, tb1_phys)))
  1410. goto out_err;
  1411. iwl_pcie_txq_build_tfd(trans, txq, tb1_phys, tb1_len, 0);
  1412. /*
  1413. * Set up TFD's third entry to point directly to remainder
  1414. * of skb, if any (802.11 null frames have no payload).
  1415. */
  1416. tb2_len = skb->len - hdr_len;
  1417. if (tb2_len > 0) {
  1418. dma_addr_t tb2_phys = dma_map_single(trans->dev,
  1419. skb->data + hdr_len,
  1420. tb2_len, DMA_TO_DEVICE);
  1421. if (unlikely(dma_mapping_error(trans->dev, tb2_phys))) {
  1422. iwl_pcie_tfd_unmap(trans, out_meta,
  1423. &txq->tfds[q->write_ptr]);
  1424. goto out_err;
  1425. }
  1426. iwl_pcie_txq_build_tfd(trans, txq, tb2_phys, tb2_len, 0);
  1427. }
  1428. /* Set up entry for this TFD in Tx byte-count array */
  1429. iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len));
  1430. trace_iwlwifi_dev_tx(trans->dev, skb,
  1431. &txq->tfds[txq->q.write_ptr],
  1432. sizeof(struct iwl_tfd),
  1433. &dev_cmd->hdr, IWL_HCMD_SCRATCHBUF_SIZE + tb1_len,
  1434. skb->data + hdr_len, tb2_len);
  1435. trace_iwlwifi_dev_tx_data(trans->dev, skb,
  1436. skb->data + hdr_len, tb2_len);
  1437. if (!ieee80211_has_morefrags(fc)) {
  1438. txq->need_update = 1;
  1439. } else {
  1440. wait_write_ptr = 1;
  1441. txq->need_update = 0;
  1442. }
  1443. /* start timer if queue currently empty */
  1444. if (txq->need_update && q->read_ptr == q->write_ptr &&
  1445. trans_pcie->wd_timeout)
  1446. mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
  1447. /* Tell device the write index *just past* this latest filled TFD */
  1448. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  1449. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1450. /*
  1451. * At this point the frame is "transmitted" successfully
  1452. * and we will get a TX status notification eventually,
  1453. * regardless of the value of ret. "ret" only indicates
  1454. * whether or not we should update the write pointer.
  1455. */
  1456. if (iwl_queue_space(q) < q->high_mark) {
  1457. if (wait_write_ptr) {
  1458. txq->need_update = 1;
  1459. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1460. } else {
  1461. iwl_stop_queue(trans, txq);
  1462. }
  1463. }
  1464. spin_unlock(&txq->lock);
  1465. return 0;
  1466. out_err:
  1467. spin_unlock(&txq->lock);
  1468. return -1;
  1469. }