tx.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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,
  312. struct iwl_tfd *tfd)
  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),
  333. DMA_TO_DEVICE);
  334. tfd->num_tbs = 0;
  335. }
  336. /*
  337. * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  338. * @trans - transport private data
  339. * @txq - tx queue
  340. * @dma_dir - the direction of the DMA mapping
  341. *
  342. * Does NOT advance any TFD circular buffer read/write indexes
  343. * Does NOT free the TFD itself (which is within circular buffer)
  344. */
  345. static void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
  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. /* free SKB */
  355. if (txq->entries) {
  356. struct sk_buff *skb;
  357. skb = txq->entries[idx].skb;
  358. /* Can be called from irqs-disabled context
  359. * If skb is not NULL, it means that the whole queue is being
  360. * freed and that the queue is not empty - free the skb
  361. */
  362. if (skb) {
  363. iwl_op_mode_free_skb(trans->op_mode, skb);
  364. txq->entries[idx].skb = NULL;
  365. }
  366. }
  367. }
  368. static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
  369. dma_addr_t addr, u16 len, u8 reset)
  370. {
  371. struct iwl_queue *q;
  372. struct iwl_tfd *tfd, *tfd_tmp;
  373. u32 num_tbs;
  374. q = &txq->q;
  375. tfd_tmp = txq->tfds;
  376. tfd = &tfd_tmp[q->write_ptr];
  377. if (reset)
  378. memset(tfd, 0, sizeof(*tfd));
  379. num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
  380. /* Each TFD can point to a maximum 20 Tx buffers */
  381. if (num_tbs >= IWL_NUM_OF_TBS) {
  382. IWL_ERR(trans, "Error can not send more than %d chunks\n",
  383. IWL_NUM_OF_TBS);
  384. return -EINVAL;
  385. }
  386. if (WARN_ON(addr & ~DMA_BIT_MASK(36)))
  387. return -EINVAL;
  388. if (unlikely(addr & ~IWL_TX_DMA_MASK))
  389. IWL_ERR(trans, "Unaligned address = %llx\n",
  390. (unsigned long long)addr);
  391. iwl_pcie_tfd_set_tb(tfd, num_tbs, addr, len);
  392. return 0;
  393. }
  394. static int iwl_pcie_txq_alloc(struct iwl_trans *trans,
  395. struct iwl_txq *txq, int slots_num,
  396. u32 txq_id)
  397. {
  398. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  399. size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
  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. IWL_ERR(trans, "dma_alloc_coherent(%zd) failed\n", tfd_sz);
  426. goto error;
  427. }
  428. txq->q.id = txq_id;
  429. return 0;
  430. error:
  431. if (txq->entries && txq_id == trans_pcie->cmd_queue)
  432. for (i = 0; i < slots_num; i++)
  433. kfree(txq->entries[i].cmd);
  434. kfree(txq->entries);
  435. txq->entries = NULL;
  436. return -ENOMEM;
  437. }
  438. static int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
  439. int slots_num, u32 txq_id)
  440. {
  441. int ret;
  442. txq->need_update = 0;
  443. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  444. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  445. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  446. /* Initialize queue's high/low-water marks, and head/tail indexes */
  447. ret = iwl_queue_init(&txq->q, TFD_QUEUE_SIZE_MAX, slots_num,
  448. txq_id);
  449. if (ret)
  450. return ret;
  451. spin_lock_init(&txq->lock);
  452. /*
  453. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  454. * given Tx queue, and enable the DMA channel used for that queue.
  455. * Circular buffer (TFD queue in DRAM) physical base address */
  456. iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
  457. txq->q.dma_addr >> 8);
  458. return 0;
  459. }
  460. /*
  461. * iwl_pcie_txq_unmap - Unmap any remaining DMA mappings and free skb's
  462. */
  463. static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
  464. {
  465. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  466. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  467. struct iwl_queue *q = &txq->q;
  468. if (!q->n_bd)
  469. return;
  470. spin_lock_bh(&txq->lock);
  471. while (q->write_ptr != q->read_ptr) {
  472. iwl_pcie_txq_free_tfd(trans, txq);
  473. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd);
  474. }
  475. spin_unlock_bh(&txq->lock);
  476. }
  477. /*
  478. * iwl_pcie_txq_free - Deallocate DMA queue.
  479. * @txq: Transmit queue to deallocate.
  480. *
  481. * Empty queue by removing and destroying all BD's.
  482. * Free all buffers.
  483. * 0-fill, but do not free "txq" descriptor structure.
  484. */
  485. static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
  486. {
  487. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  488. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  489. struct device *dev = trans->dev;
  490. int i;
  491. if (WARN_ON(!txq))
  492. return;
  493. iwl_pcie_txq_unmap(trans, txq_id);
  494. /* De-alloc array of command/tx buffers */
  495. if (txq_id == trans_pcie->cmd_queue)
  496. for (i = 0; i < txq->q.n_window; i++) {
  497. kfree(txq->entries[i].cmd);
  498. kfree(txq->entries[i].copy_cmd);
  499. kfree(txq->entries[i].free_buf);
  500. }
  501. /* De-alloc circular buffer of TFDs */
  502. if (txq->q.n_bd) {
  503. dma_free_coherent(dev, sizeof(struct iwl_tfd) *
  504. txq->q.n_bd, txq->tfds, txq->q.dma_addr);
  505. txq->q.dma_addr = 0;
  506. }
  507. kfree(txq->entries);
  508. txq->entries = NULL;
  509. del_timer_sync(&txq->stuck_timer);
  510. /* 0-fill queue descriptor structure */
  511. memset(txq, 0, sizeof(*txq));
  512. }
  513. /*
  514. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  515. */
  516. static void iwl_pcie_txq_set_sched(struct iwl_trans *trans, u32 mask)
  517. {
  518. struct iwl_trans_pcie __maybe_unused *trans_pcie =
  519. IWL_TRANS_GET_PCIE_TRANS(trans);
  520. iwl_write_prph(trans, SCD_TXFACT, mask);
  521. }
  522. void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
  523. {
  524. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  525. int nq = trans->cfg->base_params->num_of_queues;
  526. int chan;
  527. u32 reg_val;
  528. int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
  529. SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
  530. /* make sure all queue are not stopped/used */
  531. memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
  532. memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
  533. trans_pcie->scd_base_addr =
  534. iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
  535. WARN_ON(scd_base_addr != 0 &&
  536. scd_base_addr != trans_pcie->scd_base_addr);
  537. /* reset context data, TX status and translation data */
  538. iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
  539. SCD_CONTEXT_MEM_LOWER_BOUND,
  540. NULL, clear_dwords);
  541. iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
  542. trans_pcie->scd_bc_tbls.dma >> 10);
  543. /* The chain extension of the SCD doesn't work well. This feature is
  544. * enabled by default by the HW, so we need to disable it manually.
  545. */
  546. iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
  547. iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue,
  548. trans_pcie->cmd_fifo);
  549. /* Activate all Tx DMA/FIFO channels */
  550. iwl_pcie_txq_set_sched(trans, IWL_MASK(0, 7));
  551. /* Enable DMA channel */
  552. for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
  553. iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  554. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  555. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  556. /* Update FH chicken bits */
  557. reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
  558. iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
  559. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  560. /* Enable L1-Active */
  561. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  562. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  563. }
  564. void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
  565. {
  566. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  567. int txq_id;
  568. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  569. txq_id++) {
  570. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  571. iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
  572. txq->q.dma_addr >> 8);
  573. iwl_pcie_txq_unmap(trans, txq_id);
  574. txq->q.read_ptr = 0;
  575. txq->q.write_ptr = 0;
  576. }
  577. /* Tell NIC where to find the "keep warm" buffer */
  578. iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
  579. trans_pcie->kw.dma >> 4);
  580. iwl_pcie_tx_start(trans, trans_pcie->scd_base_addr);
  581. }
  582. /*
  583. * iwl_pcie_tx_stop - Stop all Tx DMA channels
  584. */
  585. int iwl_pcie_tx_stop(struct iwl_trans *trans)
  586. {
  587. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  588. int ch, txq_id, ret;
  589. unsigned long flags;
  590. /* Turn off all Tx DMA fifos */
  591. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  592. iwl_pcie_txq_set_sched(trans, 0);
  593. /* Stop each Tx DMA channel, and wait for it to be idle */
  594. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  595. iwl_write_direct32(trans,
  596. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  597. ret = iwl_poll_direct_bit(trans, FH_TSSR_TX_STATUS_REG,
  598. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000);
  599. if (ret < 0)
  600. IWL_ERR(trans,
  601. "Failing on timeout while stopping DMA channel %d [0x%08x]\n",
  602. ch,
  603. iwl_read_direct32(trans,
  604. FH_TSSR_TX_STATUS_REG));
  605. }
  606. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  607. if (!trans_pcie->txq) {
  608. IWL_WARN(trans,
  609. "Stopping tx queues that aren't allocated...\n");
  610. return 0;
  611. }
  612. /* Unmap DMA from host system and free skb's */
  613. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  614. txq_id++)
  615. iwl_pcie_txq_unmap(trans, txq_id);
  616. return 0;
  617. }
  618. /*
  619. * iwl_trans_tx_free - Free TXQ Context
  620. *
  621. * Destroy all TX DMA queues and structures
  622. */
  623. void iwl_pcie_tx_free(struct iwl_trans *trans)
  624. {
  625. int txq_id;
  626. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  627. /* Tx queues */
  628. if (trans_pcie->txq) {
  629. for (txq_id = 0;
  630. txq_id < trans->cfg->base_params->num_of_queues; txq_id++)
  631. iwl_pcie_txq_free(trans, txq_id);
  632. }
  633. kfree(trans_pcie->txq);
  634. trans_pcie->txq = NULL;
  635. iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
  636. iwl_pcie_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
  637. }
  638. /*
  639. * iwl_pcie_tx_alloc - allocate TX context
  640. * Allocate all Tx DMA structures and initialize them
  641. */
  642. static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
  643. {
  644. int ret;
  645. int txq_id, slots_num;
  646. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  647. u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues *
  648. sizeof(struct iwlagn_scd_bc_tbl);
  649. /*It is not allowed to alloc twice, so warn when this happens.
  650. * We cannot rely on the previous allocation, so free and fail */
  651. if (WARN_ON(trans_pcie->txq)) {
  652. ret = -EINVAL;
  653. goto error;
  654. }
  655. ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
  656. scd_bc_tbls_size);
  657. if (ret) {
  658. IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
  659. goto error;
  660. }
  661. /* Alloc keep-warm buffer */
  662. ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
  663. if (ret) {
  664. IWL_ERR(trans, "Keep Warm allocation failed\n");
  665. goto error;
  666. }
  667. trans_pcie->txq = kcalloc(trans->cfg->base_params->num_of_queues,
  668. sizeof(struct iwl_txq), GFP_KERNEL);
  669. if (!trans_pcie->txq) {
  670. IWL_ERR(trans, "Not enough memory for txq\n");
  671. ret = ENOMEM;
  672. goto error;
  673. }
  674. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  675. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  676. txq_id++) {
  677. slots_num = (txq_id == trans_pcie->cmd_queue) ?
  678. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  679. ret = iwl_pcie_txq_alloc(trans, &trans_pcie->txq[txq_id],
  680. slots_num, txq_id);
  681. if (ret) {
  682. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  683. goto error;
  684. }
  685. }
  686. return 0;
  687. error:
  688. iwl_pcie_tx_free(trans);
  689. return ret;
  690. }
  691. int iwl_pcie_tx_init(struct iwl_trans *trans)
  692. {
  693. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  694. int ret;
  695. int txq_id, slots_num;
  696. unsigned long flags;
  697. bool alloc = false;
  698. if (!trans_pcie->txq) {
  699. ret = iwl_pcie_tx_alloc(trans);
  700. if (ret)
  701. goto error;
  702. alloc = true;
  703. }
  704. spin_lock_irqsave(&trans_pcie->irq_lock, flags);
  705. /* Turn off all Tx DMA fifos */
  706. iwl_write_prph(trans, SCD_TXFACT, 0);
  707. /* Tell NIC where to find the "keep warm" buffer */
  708. iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
  709. trans_pcie->kw.dma >> 4);
  710. spin_unlock_irqrestore(&trans_pcie->irq_lock, flags);
  711. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  712. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  713. txq_id++) {
  714. slots_num = (txq_id == trans_pcie->cmd_queue) ?
  715. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  716. ret = iwl_pcie_txq_init(trans, &trans_pcie->txq[txq_id],
  717. slots_num, txq_id);
  718. if (ret) {
  719. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  720. goto error;
  721. }
  722. }
  723. return 0;
  724. error:
  725. /*Upon error, free only if we allocated something */
  726. if (alloc)
  727. iwl_pcie_tx_free(trans);
  728. return ret;
  729. }
  730. static inline void iwl_pcie_txq_progress(struct iwl_trans_pcie *trans_pcie,
  731. struct iwl_txq *txq)
  732. {
  733. if (!trans_pcie->wd_timeout)
  734. return;
  735. /*
  736. * if empty delete timer, otherwise move timer forward
  737. * since we're making progress on this queue
  738. */
  739. if (txq->q.read_ptr == txq->q.write_ptr)
  740. del_timer(&txq->stuck_timer);
  741. else
  742. mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
  743. }
  744. /* Frees buffers until index _not_ inclusive */
  745. void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
  746. struct sk_buff_head *skbs)
  747. {
  748. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  749. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  750. /* n_bd is usually 256 => n_bd - 1 = 0xff */
  751. int tfd_num = ssn & (txq->q.n_bd - 1);
  752. struct iwl_queue *q = &txq->q;
  753. int last_to_free;
  754. /* This function is not meant to release cmd queue*/
  755. if (WARN_ON(txq_id == trans_pcie->cmd_queue))
  756. return;
  757. spin_lock_bh(&txq->lock);
  758. if (txq->q.read_ptr == tfd_num)
  759. goto out;
  760. IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
  761. txq_id, txq->q.read_ptr, tfd_num, ssn);
  762. /*Since we free until index _not_ inclusive, the one before index is
  763. * the last we will free. This one must be used */
  764. last_to_free = iwl_queue_dec_wrap(tfd_num, q->n_bd);
  765. if (!iwl_queue_used(q, last_to_free)) {
  766. IWL_ERR(trans,
  767. "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
  768. __func__, txq_id, last_to_free, q->n_bd,
  769. q->write_ptr, q->read_ptr);
  770. goto out;
  771. }
  772. if (WARN_ON(!skb_queue_empty(skbs)))
  773. goto out;
  774. for (;
  775. q->read_ptr != tfd_num;
  776. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  777. if (WARN_ON_ONCE(txq->entries[txq->q.read_ptr].skb == NULL))
  778. continue;
  779. __skb_queue_tail(skbs, txq->entries[txq->q.read_ptr].skb);
  780. txq->entries[txq->q.read_ptr].skb = NULL;
  781. iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq);
  782. iwl_pcie_txq_free_tfd(trans, txq);
  783. }
  784. iwl_pcie_txq_progress(trans_pcie, txq);
  785. if (iwl_queue_space(&txq->q) > txq->q.low_mark)
  786. iwl_wake_queue(trans, txq);
  787. out:
  788. spin_unlock_bh(&txq->lock);
  789. }
  790. /*
  791. * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
  792. *
  793. * When FW advances 'R' index, all entries between old and new 'R' index
  794. * need to be reclaimed. As result, some free space forms. If there is
  795. * enough free space (> low mark), wake the stack that feeds us.
  796. */
  797. static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
  798. {
  799. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  800. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  801. struct iwl_queue *q = &txq->q;
  802. int nfreed = 0;
  803. lockdep_assert_held(&txq->lock);
  804. if ((idx >= q->n_bd) || (!iwl_queue_used(q, idx))) {
  805. IWL_ERR(trans,
  806. "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
  807. __func__, txq_id, idx, q->n_bd,
  808. q->write_ptr, q->read_ptr);
  809. return;
  810. }
  811. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  812. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  813. if (nfreed++ > 0) {
  814. IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
  815. idx, q->write_ptr, q->read_ptr);
  816. iwl_op_mode_nic_error(trans->op_mode);
  817. }
  818. }
  819. iwl_pcie_txq_progress(trans_pcie, txq);
  820. }
  821. static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
  822. u16 txq_id)
  823. {
  824. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  825. u32 tbl_dw_addr;
  826. u32 tbl_dw;
  827. u16 scd_q2ratid;
  828. scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  829. tbl_dw_addr = trans_pcie->scd_base_addr +
  830. SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
  831. tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
  832. if (txq_id & 0x1)
  833. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  834. else
  835. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  836. iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
  837. return 0;
  838. }
  839. static inline void iwl_pcie_txq_set_inactive(struct iwl_trans *trans,
  840. u16 txq_id)
  841. {
  842. /* Simply stop the queue, but don't change any configuration;
  843. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  844. iwl_write_prph(trans,
  845. SCD_QUEUE_STATUS_BITS(txq_id),
  846. (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  847. (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  848. }
  849. void iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, int fifo,
  850. int sta_id, int tid, int frame_limit, u16 ssn)
  851. {
  852. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  853. if (test_and_set_bit(txq_id, trans_pcie->queue_used))
  854. WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
  855. /* Stop this Tx queue before configuring it */
  856. iwl_pcie_txq_set_inactive(trans, txq_id);
  857. /* Set this queue as a chain-building queue unless it is CMD queue */
  858. if (txq_id != trans_pcie->cmd_queue)
  859. iwl_set_bits_prph(trans, SCD_QUEUECHAIN_SEL, BIT(txq_id));
  860. /* If this queue is mapped to a certain station: it is an AGG queue */
  861. if (sta_id != IWL_INVALID_STATION) {
  862. u16 ra_tid = BUILD_RAxTID(sta_id, tid);
  863. /* Map receiver-address / traffic-ID to this queue */
  864. iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
  865. /* enable aggregations for the queue */
  866. iwl_set_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
  867. } else {
  868. /*
  869. * disable aggregations for the queue, this will also make the
  870. * ra_tid mapping configuration irrelevant since it is now a
  871. * non-AGG queue.
  872. */
  873. iwl_clear_bits_prph(trans, SCD_AGGR_SEL, BIT(txq_id));
  874. }
  875. /* Place first TFD at index corresponding to start sequence number.
  876. * Assumes that ssn_idx is valid (!= 0xFFF) */
  877. trans_pcie->txq[txq_id].q.read_ptr = (ssn & 0xff);
  878. trans_pcie->txq[txq_id].q.write_ptr = (ssn & 0xff);
  879. iwl_write_direct32(trans, HBUS_TARG_WRPTR,
  880. (ssn & 0xff) | (txq_id << 8));
  881. iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
  882. /* Set up Tx window size and frame limit for this queue */
  883. iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
  884. SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
  885. iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
  886. SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  887. ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  888. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  889. ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  890. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  891. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  892. iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
  893. (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  894. (fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
  895. (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
  896. SCD_QUEUE_STTS_REG_MSK);
  897. IWL_DEBUG_TX_QUEUES(trans, "Activate queue %d on FIFO %d WrPtr: %d\n",
  898. txq_id, fifo, ssn & 0xff);
  899. }
  900. void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id)
  901. {
  902. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  903. u32 stts_addr = trans_pcie->scd_base_addr +
  904. SCD_TX_STTS_QUEUE_OFFSET(txq_id);
  905. static const u32 zero_val[4] = {};
  906. if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
  907. WARN_ONCE(1, "queue %d not used", txq_id);
  908. return;
  909. }
  910. iwl_pcie_txq_set_inactive(trans, txq_id);
  911. iwl_trans_write_mem(trans, stts_addr, (void *)zero_val,
  912. ARRAY_SIZE(zero_val));
  913. iwl_pcie_txq_unmap(trans, txq_id);
  914. IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
  915. }
  916. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  917. /*
  918. * iwl_pcie_enqueue_hcmd - enqueue a uCode command
  919. * @priv: device private data point
  920. * @cmd: a point to the ucode command structure
  921. *
  922. * The function returns < 0 values to indicate the operation is
  923. * failed. On success, it turns the index (> 0) of command in the
  924. * command queue.
  925. */
  926. static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
  927. struct iwl_host_cmd *cmd)
  928. {
  929. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  930. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  931. struct iwl_queue *q = &txq->q;
  932. struct iwl_device_cmd *out_cmd;
  933. struct iwl_cmd_meta *out_meta;
  934. void *dup_buf = NULL;
  935. dma_addr_t phys_addr;
  936. int idx;
  937. u16 copy_size, cmd_size, dma_size;
  938. bool had_nocopy = false;
  939. int i;
  940. u32 cmd_pos;
  941. const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
  942. u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
  943. copy_size = sizeof(out_cmd->hdr);
  944. cmd_size = sizeof(out_cmd->hdr);
  945. /* need one for the header if the first is NOCOPY */
  946. BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
  947. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  948. cmddata[i] = cmd->data[i];
  949. cmdlen[i] = cmd->len[i];
  950. if (!cmd->len[i])
  951. continue;
  952. /* need at least IWL_HCMD_MIN_COPY_SIZE copied */
  953. if (copy_size < IWL_HCMD_MIN_COPY_SIZE) {
  954. int copy = IWL_HCMD_MIN_COPY_SIZE - copy_size;
  955. if (copy > cmdlen[i])
  956. copy = cmdlen[i];
  957. cmdlen[i] -= copy;
  958. cmddata[i] += copy;
  959. copy_size += copy;
  960. }
  961. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  962. had_nocopy = true;
  963. if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
  964. idx = -EINVAL;
  965. goto free_dup_buf;
  966. }
  967. } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
  968. /*
  969. * This is also a chunk that isn't copied
  970. * to the static buffer so set had_nocopy.
  971. */
  972. had_nocopy = true;
  973. /* only allowed once */
  974. if (WARN_ON(dup_buf)) {
  975. idx = -EINVAL;
  976. goto free_dup_buf;
  977. }
  978. dup_buf = kmemdup(cmddata[i], cmdlen[i],
  979. GFP_ATOMIC);
  980. if (!dup_buf)
  981. return -ENOMEM;
  982. } else {
  983. /* NOCOPY must not be followed by normal! */
  984. if (WARN_ON(had_nocopy)) {
  985. idx = -EINVAL;
  986. goto free_dup_buf;
  987. }
  988. copy_size += cmdlen[i];
  989. }
  990. cmd_size += cmd->len[i];
  991. }
  992. /*
  993. * If any of the command structures end up being larger than
  994. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  995. * allocated into separate TFDs, then we will need to
  996. * increase the size of the buffers.
  997. */
  998. if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
  999. "Command %s (%#x) is too large (%d bytes)\n",
  1000. get_cmd_string(trans_pcie, cmd->id), cmd->id, copy_size)) {
  1001. idx = -EINVAL;
  1002. goto free_dup_buf;
  1003. }
  1004. spin_lock_bh(&txq->lock);
  1005. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  1006. spin_unlock_bh(&txq->lock);
  1007. IWL_ERR(trans, "No space in command queue\n");
  1008. iwl_op_mode_cmd_queue_full(trans->op_mode);
  1009. idx = -ENOSPC;
  1010. goto free_dup_buf;
  1011. }
  1012. idx = get_cmd_index(q, q->write_ptr);
  1013. out_cmd = txq->entries[idx].cmd;
  1014. out_meta = &txq->entries[idx].meta;
  1015. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  1016. if (cmd->flags & CMD_WANT_SKB)
  1017. out_meta->source = cmd;
  1018. /* set up the header */
  1019. out_cmd->hdr.cmd = cmd->id;
  1020. out_cmd->hdr.flags = 0;
  1021. out_cmd->hdr.sequence =
  1022. cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
  1023. INDEX_TO_SEQ(q->write_ptr));
  1024. /* and copy the data that needs to be copied */
  1025. cmd_pos = offsetof(struct iwl_device_cmd, payload);
  1026. copy_size = sizeof(out_cmd->hdr);
  1027. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1028. int copy = 0;
  1029. if (!cmd->len)
  1030. continue;
  1031. /* need at least IWL_HCMD_MIN_COPY_SIZE copied */
  1032. if (copy_size < IWL_HCMD_MIN_COPY_SIZE) {
  1033. copy = IWL_HCMD_MIN_COPY_SIZE - copy_size;
  1034. if (copy > cmd->len[i])
  1035. copy = cmd->len[i];
  1036. }
  1037. /* copy everything if not nocopy/dup */
  1038. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  1039. IWL_HCMD_DFL_DUP)))
  1040. copy = cmd->len[i];
  1041. if (copy) {
  1042. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
  1043. cmd_pos += copy;
  1044. copy_size += copy;
  1045. }
  1046. }
  1047. WARN_ON_ONCE(txq->entries[idx].copy_cmd);
  1048. /*
  1049. * since out_cmd will be the source address of the FH, it will write
  1050. * the retry count there. So when the user needs to receivce the HCMD
  1051. * that corresponds to the response in the response handler, it needs
  1052. * to set CMD_WANT_HCMD.
  1053. */
  1054. if (cmd->flags & CMD_WANT_HCMD) {
  1055. txq->entries[idx].copy_cmd =
  1056. kmemdup(out_cmd, cmd_pos, GFP_ATOMIC);
  1057. if (unlikely(!txq->entries[idx].copy_cmd)) {
  1058. idx = -ENOMEM;
  1059. goto out;
  1060. }
  1061. }
  1062. IWL_DEBUG_HC(trans,
  1063. "Sending command %s (#%x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
  1064. get_cmd_string(trans_pcie, out_cmd->hdr.cmd),
  1065. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  1066. cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue);
  1067. /*
  1068. * If the entire command is smaller than IWL_HCMD_MIN_COPY_SIZE, we must
  1069. * still map at least that many bytes for the hardware to write back to.
  1070. * We have enough space, so that's not a problem.
  1071. */
  1072. dma_size = max_t(u16, copy_size, IWL_HCMD_MIN_COPY_SIZE);
  1073. phys_addr = dma_map_single(trans->dev, &out_cmd->hdr, dma_size,
  1074. DMA_BIDIRECTIONAL);
  1075. if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
  1076. idx = -ENOMEM;
  1077. goto out;
  1078. }
  1079. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  1080. dma_unmap_len_set(out_meta, len, dma_size);
  1081. iwl_pcie_txq_build_tfd(trans, txq, phys_addr, copy_size, 1);
  1082. /* map the remaining (adjusted) nocopy/dup fragments */
  1083. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1084. const void *data = cmddata[i];
  1085. if (!cmdlen[i])
  1086. continue;
  1087. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  1088. IWL_HCMD_DFL_DUP)))
  1089. continue;
  1090. if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
  1091. data = dup_buf;
  1092. phys_addr = dma_map_single(trans->dev, (void *)data,
  1093. cmdlen[i], DMA_TO_DEVICE);
  1094. if (dma_mapping_error(trans->dev, phys_addr)) {
  1095. iwl_pcie_tfd_unmap(trans, out_meta,
  1096. &txq->tfds[q->write_ptr]);
  1097. idx = -ENOMEM;
  1098. goto out;
  1099. }
  1100. iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], 0);
  1101. }
  1102. out_meta->flags = cmd->flags;
  1103. if (WARN_ON_ONCE(txq->entries[idx].free_buf))
  1104. kfree(txq->entries[idx].free_buf);
  1105. txq->entries[idx].free_buf = dup_buf;
  1106. txq->need_update = 1;
  1107. trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr);
  1108. /* start timer if queue currently empty */
  1109. if (q->read_ptr == q->write_ptr && trans_pcie->wd_timeout)
  1110. mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
  1111. /* Increment and update queue's write index */
  1112. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  1113. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1114. out:
  1115. spin_unlock_bh(&txq->lock);
  1116. free_dup_buf:
  1117. if (idx < 0)
  1118. kfree(dup_buf);
  1119. return idx;
  1120. }
  1121. /*
  1122. * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
  1123. * @rxb: Rx buffer to reclaim
  1124. * @handler_status: return value of the handler of the command
  1125. * (put in setup_rx_handlers)
  1126. *
  1127. * If an Rx buffer has an async callback associated with it the callback
  1128. * will be executed. The attached skb (if present) will only be freed
  1129. * if the callback returns 1
  1130. */
  1131. void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
  1132. struct iwl_rx_cmd_buffer *rxb, int handler_status)
  1133. {
  1134. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1135. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1136. int txq_id = SEQ_TO_QUEUE(sequence);
  1137. int index = SEQ_TO_INDEX(sequence);
  1138. int cmd_index;
  1139. struct iwl_device_cmd *cmd;
  1140. struct iwl_cmd_meta *meta;
  1141. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1142. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  1143. /* If a Tx command is being handled and it isn't in the actual
  1144. * command queue then there a command routing bug has been introduced
  1145. * in the queue management code. */
  1146. if (WARN(txq_id != trans_pcie->cmd_queue,
  1147. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  1148. txq_id, trans_pcie->cmd_queue, sequence,
  1149. trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr,
  1150. trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) {
  1151. iwl_print_hex_error(trans, pkt, 32);
  1152. return;
  1153. }
  1154. spin_lock_bh(&txq->lock);
  1155. cmd_index = get_cmd_index(&txq->q, index);
  1156. cmd = txq->entries[cmd_index].cmd;
  1157. meta = &txq->entries[cmd_index].meta;
  1158. iwl_pcie_tfd_unmap(trans, meta, &txq->tfds[index]);
  1159. /* Input error checking is done when commands are added to queue. */
  1160. if (meta->flags & CMD_WANT_SKB) {
  1161. struct page *p = rxb_steal_page(rxb);
  1162. meta->source->resp_pkt = pkt;
  1163. meta->source->_rx_page_addr = (unsigned long)page_address(p);
  1164. meta->source->_rx_page_order = trans_pcie->rx_page_order;
  1165. meta->source->handler_status = handler_status;
  1166. }
  1167. iwl_pcie_cmdq_reclaim(trans, txq_id, index);
  1168. if (!(meta->flags & CMD_ASYNC)) {
  1169. if (!test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
  1170. IWL_WARN(trans,
  1171. "HCMD_ACTIVE already clear for command %s\n",
  1172. get_cmd_string(trans_pcie, cmd->hdr.cmd));
  1173. }
  1174. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  1175. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  1176. get_cmd_string(trans_pcie, cmd->hdr.cmd));
  1177. wake_up(&trans_pcie->wait_command_queue);
  1178. }
  1179. meta->flags = 0;
  1180. spin_unlock_bh(&txq->lock);
  1181. }
  1182. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  1183. static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
  1184. struct iwl_host_cmd *cmd)
  1185. {
  1186. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1187. int ret;
  1188. /* An asynchronous command can not expect an SKB to be set. */
  1189. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  1190. return -EINVAL;
  1191. ret = iwl_pcie_enqueue_hcmd(trans, cmd);
  1192. if (ret < 0) {
  1193. IWL_ERR(trans,
  1194. "Error sending %s: enqueue_hcmd failed: %d\n",
  1195. get_cmd_string(trans_pcie, cmd->id), ret);
  1196. return ret;
  1197. }
  1198. return 0;
  1199. }
  1200. static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
  1201. struct iwl_host_cmd *cmd)
  1202. {
  1203. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1204. int cmd_idx;
  1205. int ret;
  1206. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
  1207. get_cmd_string(trans_pcie, cmd->id));
  1208. if (WARN_ON(test_and_set_bit(STATUS_HCMD_ACTIVE,
  1209. &trans_pcie->status))) {
  1210. IWL_ERR(trans, "Command %s: a command is already active!\n",
  1211. get_cmd_string(trans_pcie, cmd->id));
  1212. return -EIO;
  1213. }
  1214. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
  1215. get_cmd_string(trans_pcie, cmd->id));
  1216. cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
  1217. if (cmd_idx < 0) {
  1218. ret = cmd_idx;
  1219. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  1220. IWL_ERR(trans,
  1221. "Error sending %s: enqueue_hcmd failed: %d\n",
  1222. get_cmd_string(trans_pcie, cmd->id), ret);
  1223. return ret;
  1224. }
  1225. ret = wait_event_timeout(trans_pcie->wait_command_queue,
  1226. !test_bit(STATUS_HCMD_ACTIVE,
  1227. &trans_pcie->status),
  1228. HOST_COMPLETE_TIMEOUT);
  1229. if (!ret) {
  1230. if (test_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status)) {
  1231. struct iwl_txq *txq =
  1232. &trans_pcie->txq[trans_pcie->cmd_queue];
  1233. struct iwl_queue *q = &txq->q;
  1234. IWL_ERR(trans,
  1235. "Error sending %s: time out after %dms.\n",
  1236. get_cmd_string(trans_pcie, cmd->id),
  1237. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  1238. IWL_ERR(trans,
  1239. "Current CMD queue read_ptr %d write_ptr %d\n",
  1240. q->read_ptr, q->write_ptr);
  1241. clear_bit(STATUS_HCMD_ACTIVE, &trans_pcie->status);
  1242. IWL_DEBUG_INFO(trans,
  1243. "Clearing HCMD_ACTIVE for command %s\n",
  1244. get_cmd_string(trans_pcie, cmd->id));
  1245. ret = -ETIMEDOUT;
  1246. goto cancel;
  1247. }
  1248. }
  1249. if (test_bit(STATUS_FW_ERROR, &trans_pcie->status)) {
  1250. IWL_ERR(trans, "FW error in SYNC CMD %s\n",
  1251. get_cmd_string(trans_pcie, cmd->id));
  1252. ret = -EIO;
  1253. goto cancel;
  1254. }
  1255. if (test_bit(STATUS_RFKILL, &trans_pcie->status)) {
  1256. IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
  1257. ret = -ERFKILL;
  1258. goto cancel;
  1259. }
  1260. if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
  1261. IWL_ERR(trans, "Error: Response NULL in '%s'\n",
  1262. get_cmd_string(trans_pcie, cmd->id));
  1263. ret = -EIO;
  1264. goto cancel;
  1265. }
  1266. return 0;
  1267. cancel:
  1268. if (cmd->flags & CMD_WANT_SKB) {
  1269. /*
  1270. * Cancel the CMD_WANT_SKB flag for the cmd in the
  1271. * TX cmd queue. Otherwise in case the cmd comes
  1272. * in later, it will possibly set an invalid
  1273. * address (cmd->meta.source).
  1274. */
  1275. trans_pcie->txq[trans_pcie->cmd_queue].
  1276. entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
  1277. }
  1278. if (cmd->resp_pkt) {
  1279. iwl_free_resp(cmd);
  1280. cmd->resp_pkt = NULL;
  1281. }
  1282. return ret;
  1283. }
  1284. int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  1285. {
  1286. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1287. if (test_bit(STATUS_FW_ERROR, &trans_pcie->status))
  1288. return -EIO;
  1289. if (test_bit(STATUS_RFKILL, &trans_pcie->status))
  1290. return -ERFKILL;
  1291. if (cmd->flags & CMD_ASYNC)
  1292. return iwl_pcie_send_hcmd_async(trans, cmd);
  1293. /* We still can fail on RFKILL that can be asserted while we wait */
  1294. return iwl_pcie_send_hcmd_sync(trans, cmd);
  1295. }
  1296. int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  1297. struct iwl_device_cmd *dev_cmd, int txq_id)
  1298. {
  1299. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1300. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1301. struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  1302. struct iwl_cmd_meta *out_meta;
  1303. struct iwl_txq *txq;
  1304. struct iwl_queue *q;
  1305. dma_addr_t phys_addr = 0;
  1306. dma_addr_t txcmd_phys;
  1307. dma_addr_t scratch_phys;
  1308. u16 len, firstlen, secondlen;
  1309. u8 wait_write_ptr = 0;
  1310. __le16 fc = hdr->frame_control;
  1311. u8 hdr_len = ieee80211_hdrlen(fc);
  1312. u16 __maybe_unused wifi_seq;
  1313. txq = &trans_pcie->txq[txq_id];
  1314. q = &txq->q;
  1315. if (unlikely(!test_bit(txq_id, trans_pcie->queue_used))) {
  1316. WARN_ON_ONCE(1);
  1317. return -EINVAL;
  1318. }
  1319. spin_lock(&txq->lock);
  1320. /* In AGG mode, the index in the ring must correspond to the WiFi
  1321. * sequence number. This is a HW requirements to help the SCD to parse
  1322. * the BA.
  1323. * Check here that the packets are in the right place on the ring.
  1324. */
  1325. #ifdef CONFIG_IWLWIFI_DEBUG
  1326. wifi_seq = SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  1327. WARN_ONCE((iwl_read_prph(trans, SCD_AGGR_SEL) & BIT(txq_id)) &&
  1328. ((wifi_seq & 0xff) != q->write_ptr),
  1329. "Q: %d WiFi Seq %d tfdNum %d",
  1330. txq_id, wifi_seq, q->write_ptr);
  1331. #endif
  1332. /* Set up driver data for this TFD */
  1333. txq->entries[q->write_ptr].skb = skb;
  1334. txq->entries[q->write_ptr].cmd = dev_cmd;
  1335. dev_cmd->hdr.cmd = REPLY_TX;
  1336. dev_cmd->hdr.sequence =
  1337. cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  1338. INDEX_TO_SEQ(q->write_ptr)));
  1339. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  1340. out_meta = &txq->entries[q->write_ptr].meta;
  1341. /*
  1342. * Use the first empty entry in this queue's command buffer array
  1343. * to contain the Tx command and MAC header concatenated together
  1344. * (payload data will be in another buffer).
  1345. * Size of this varies, due to varying MAC header length.
  1346. * If end is not dword aligned, we'll have 2 extra bytes at the end
  1347. * of the MAC header (device reads on dword boundaries).
  1348. * We'll tell device about this padding later.
  1349. */
  1350. len = sizeof(struct iwl_tx_cmd) +
  1351. sizeof(struct iwl_cmd_header) + hdr_len;
  1352. firstlen = (len + 3) & ~3;
  1353. /* Tell NIC about any 2-byte padding after MAC header */
  1354. if (firstlen != len)
  1355. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  1356. /* Physical address of this Tx command's header (not MAC header!),
  1357. * within command buffer array. */
  1358. txcmd_phys = dma_map_single(trans->dev,
  1359. &dev_cmd->hdr, firstlen,
  1360. DMA_BIDIRECTIONAL);
  1361. if (unlikely(dma_mapping_error(trans->dev, txcmd_phys)))
  1362. goto out_err;
  1363. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  1364. dma_unmap_len_set(out_meta, len, firstlen);
  1365. if (!ieee80211_has_morefrags(fc)) {
  1366. txq->need_update = 1;
  1367. } else {
  1368. wait_write_ptr = 1;
  1369. txq->need_update = 0;
  1370. }
  1371. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  1372. * if any (802.11 null frames have no payload). */
  1373. secondlen = skb->len - hdr_len;
  1374. if (secondlen > 0) {
  1375. phys_addr = dma_map_single(trans->dev, skb->data + hdr_len,
  1376. secondlen, DMA_TO_DEVICE);
  1377. if (unlikely(dma_mapping_error(trans->dev, phys_addr))) {
  1378. dma_unmap_single(trans->dev,
  1379. dma_unmap_addr(out_meta, mapping),
  1380. dma_unmap_len(out_meta, len),
  1381. DMA_BIDIRECTIONAL);
  1382. goto out_err;
  1383. }
  1384. }
  1385. /* Attach buffers to TFD */
  1386. iwl_pcie_txq_build_tfd(trans, txq, txcmd_phys, firstlen, 1);
  1387. if (secondlen > 0)
  1388. iwl_pcie_txq_build_tfd(trans, txq, phys_addr, secondlen, 0);
  1389. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  1390. offsetof(struct iwl_tx_cmd, scratch);
  1391. /* take back ownership of DMA buffer to enable update */
  1392. dma_sync_single_for_cpu(trans->dev, txcmd_phys, firstlen,
  1393. DMA_BIDIRECTIONAL);
  1394. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  1395. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  1396. /* Set up entry for this TFD in Tx byte-count array */
  1397. iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len));
  1398. dma_sync_single_for_device(trans->dev, txcmd_phys, firstlen,
  1399. DMA_BIDIRECTIONAL);
  1400. trace_iwlwifi_dev_tx(trans->dev, skb,
  1401. &txq->tfds[txq->q.write_ptr],
  1402. sizeof(struct iwl_tfd),
  1403. &dev_cmd->hdr, firstlen,
  1404. skb->data + hdr_len, secondlen);
  1405. trace_iwlwifi_dev_tx_data(trans->dev, skb,
  1406. skb->data + hdr_len, secondlen);
  1407. /* start timer if queue currently empty */
  1408. if (txq->need_update && q->read_ptr == q->write_ptr &&
  1409. trans_pcie->wd_timeout)
  1410. mod_timer(&txq->stuck_timer, jiffies + trans_pcie->wd_timeout);
  1411. /* Tell device the write index *just past* this latest filled TFD */
  1412. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  1413. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1414. /*
  1415. * At this point the frame is "transmitted" successfully
  1416. * and we will get a TX status notification eventually,
  1417. * regardless of the value of ret. "ret" only indicates
  1418. * whether or not we should update the write pointer.
  1419. */
  1420. if (iwl_queue_space(q) < q->high_mark) {
  1421. if (wait_write_ptr) {
  1422. txq->need_update = 1;
  1423. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1424. } else {
  1425. iwl_stop_queue(trans, txq);
  1426. }
  1427. }
  1428. spin_unlock(&txq->lock);
  1429. return 0;
  1430. out_err:
  1431. spin_unlock(&txq->lock);
  1432. return -1;
  1433. }