tx.c 51 KB

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