tx.c 48 KB

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