tx.c 48 KB

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