tx.c 49 KB

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