tx.c 49 KB

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