tx.c 50 KB

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