tx.c 48 KB

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