iwl-trans-tx-pcie.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 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-agn.h"
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-io.h"
  36. #include "iwl-helpers.h"
  37. #include "iwl-trans-int-pcie.h"
  38. /**
  39. * iwl_trans_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  40. */
  41. void iwl_trans_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
  42. struct iwl_tx_queue *txq,
  43. u16 byte_cnt)
  44. {
  45. struct iwlagn_scd_bc_tbl *scd_bc_tbl;
  46. struct iwl_trans_pcie *trans_pcie =
  47. IWL_TRANS_GET_PCIE_TRANS(trans);
  48. int write_ptr = txq->q.write_ptr;
  49. int txq_id = txq->q.id;
  50. u8 sec_ctl = 0;
  51. u8 sta_id = 0;
  52. u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  53. __le16 bc_ent;
  54. scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  55. WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
  56. sta_id = txq->cmd[txq->q.write_ptr]->cmd.tx.sta_id;
  57. sec_ctl = txq->cmd[txq->q.write_ptr]->cmd.tx.sec_ctl;
  58. switch (sec_ctl & TX_CMD_SEC_MSK) {
  59. case TX_CMD_SEC_CCM:
  60. len += CCMP_MIC_LEN;
  61. break;
  62. case TX_CMD_SEC_TKIP:
  63. len += TKIP_ICV_LEN;
  64. break;
  65. case TX_CMD_SEC_WEP:
  66. len += WEP_IV_LEN + WEP_ICV_LEN;
  67. break;
  68. }
  69. bc_ent = cpu_to_le16((len & 0xFFF) | (sta_id << 12));
  70. scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
  71. if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
  72. scd_bc_tbl[txq_id].
  73. tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
  74. }
  75. /**
  76. * iwl_txq_update_write_ptr - Send new write index to hardware
  77. */
  78. void iwl_txq_update_write_ptr(struct iwl_trans *trans, struct iwl_tx_queue *txq)
  79. {
  80. u32 reg = 0;
  81. int txq_id = txq->q.id;
  82. if (txq->need_update == 0)
  83. return;
  84. if (hw_params(trans).shadow_reg_enable) {
  85. /* shadow register enabled */
  86. iwl_write32(bus(trans), HBUS_TARG_WRPTR,
  87. txq->q.write_ptr | (txq_id << 8));
  88. } else {
  89. /* if we're trying to save power */
  90. if (test_bit(STATUS_POWER_PMI, &trans->shrd->status)) {
  91. /* wake up nic if it's powered down ...
  92. * uCode will wake up, and interrupt us again, so next
  93. * time we'll skip this part. */
  94. reg = iwl_read32(bus(trans), CSR_UCODE_DRV_GP1);
  95. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  96. IWL_DEBUG_INFO(trans,
  97. "Tx queue %d requesting wakeup,"
  98. " GP1 = 0x%x\n", txq_id, reg);
  99. iwl_set_bit(bus(trans), CSR_GP_CNTRL,
  100. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  101. return;
  102. }
  103. iwl_write_direct32(bus(trans), HBUS_TARG_WRPTR,
  104. txq->q.write_ptr | (txq_id << 8));
  105. /*
  106. * else not in power-save mode,
  107. * uCode will never sleep when we're
  108. * trying to tx (during RFKILL, we're not trying to tx).
  109. */
  110. } else
  111. iwl_write32(bus(trans), HBUS_TARG_WRPTR,
  112. txq->q.write_ptr | (txq_id << 8));
  113. }
  114. txq->need_update = 0;
  115. }
  116. static inline dma_addr_t iwl_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
  117. {
  118. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  119. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  120. if (sizeof(dma_addr_t) > sizeof(u32))
  121. addr |=
  122. ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
  123. return addr;
  124. }
  125. static inline u16 iwl_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
  126. {
  127. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  128. return le16_to_cpu(tb->hi_n_len) >> 4;
  129. }
  130. static inline void iwl_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
  131. dma_addr_t addr, u16 len)
  132. {
  133. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  134. u16 hi_n_len = len << 4;
  135. put_unaligned_le32(addr, &tb->lo);
  136. if (sizeof(dma_addr_t) > sizeof(u32))
  137. hi_n_len |= ((addr >> 16) >> 16) & 0xF;
  138. tb->hi_n_len = cpu_to_le16(hi_n_len);
  139. tfd->num_tbs = idx + 1;
  140. }
  141. static inline u8 iwl_tfd_get_num_tbs(struct iwl_tfd *tfd)
  142. {
  143. return tfd->num_tbs & 0x1f;
  144. }
  145. static void iwlagn_unmap_tfd(struct iwl_trans *trans, struct iwl_cmd_meta *meta,
  146. struct iwl_tfd *tfd, enum dma_data_direction dma_dir)
  147. {
  148. int i;
  149. int num_tbs;
  150. /* Sanity check on number of chunks */
  151. num_tbs = iwl_tfd_get_num_tbs(tfd);
  152. if (num_tbs >= IWL_NUM_OF_TBS) {
  153. IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
  154. /* @todo issue fatal error, it is quite serious situation */
  155. return;
  156. }
  157. /* Unmap tx_cmd */
  158. if (num_tbs)
  159. dma_unmap_single(bus(trans)->dev,
  160. dma_unmap_addr(meta, mapping),
  161. dma_unmap_len(meta, len),
  162. DMA_BIDIRECTIONAL);
  163. /* Unmap chunks, if any. */
  164. for (i = 1; i < num_tbs; i++)
  165. dma_unmap_single(bus(trans)->dev, iwl_tfd_tb_get_addr(tfd, i),
  166. iwl_tfd_tb_get_len(tfd, i), dma_dir);
  167. }
  168. /**
  169. * iwlagn_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  170. * @trans - transport private data
  171. * @txq - tx queue
  172. * @index - the index of the TFD to be freed
  173. *
  174. * Does NOT advance any TFD circular buffer read/write indexes
  175. * Does NOT free the TFD itself (which is within circular buffer)
  176. */
  177. void iwlagn_txq_free_tfd(struct iwl_trans *trans, struct iwl_tx_queue *txq,
  178. int index)
  179. {
  180. struct iwl_tfd *tfd_tmp = txq->tfds;
  181. iwlagn_unmap_tfd(trans, &txq->meta[index], &tfd_tmp[index],
  182. DMA_TO_DEVICE);
  183. /* free SKB */
  184. if (txq->skbs) {
  185. struct sk_buff *skb;
  186. skb = txq->skbs[index];
  187. /* can be called from irqs-disabled context */
  188. if (skb) {
  189. dev_kfree_skb_any(skb);
  190. txq->skbs[index] = NULL;
  191. }
  192. }
  193. }
  194. int iwlagn_txq_attach_buf_to_tfd(struct iwl_trans *trans,
  195. struct iwl_tx_queue *txq,
  196. dma_addr_t addr, u16 len,
  197. u8 reset)
  198. {
  199. struct iwl_queue *q;
  200. struct iwl_tfd *tfd, *tfd_tmp;
  201. u32 num_tbs;
  202. q = &txq->q;
  203. tfd_tmp = txq->tfds;
  204. tfd = &tfd_tmp[q->write_ptr];
  205. if (reset)
  206. memset(tfd, 0, sizeof(*tfd));
  207. num_tbs = iwl_tfd_get_num_tbs(tfd);
  208. /* Each TFD can point to a maximum 20 Tx buffers */
  209. if (num_tbs >= IWL_NUM_OF_TBS) {
  210. IWL_ERR(trans, "Error can not send more than %d chunks\n",
  211. IWL_NUM_OF_TBS);
  212. return -EINVAL;
  213. }
  214. if (WARN_ON(addr & ~DMA_BIT_MASK(36)))
  215. return -EINVAL;
  216. if (unlikely(addr & ~IWL_TX_DMA_MASK))
  217. IWL_ERR(trans, "Unaligned address = %llx\n",
  218. (unsigned long long)addr);
  219. iwl_tfd_set_tb(tfd, num_tbs, addr, len);
  220. return 0;
  221. }
  222. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  223. * DMA services
  224. *
  225. * Theory of operation
  226. *
  227. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  228. * of buffer descriptors, each of which points to one or more data buffers for
  229. * the device to read from or fill. Driver and device exchange status of each
  230. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  231. * entries in each circular buffer, to protect against confusing empty and full
  232. * queue states.
  233. *
  234. * The device reads or writes the data in the queues via the device's several
  235. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  236. *
  237. * For Tx queue, there are low mark and high mark limits. If, after queuing
  238. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  239. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  240. * Tx queue resumed.
  241. *
  242. ***************************************************/
  243. int iwl_queue_space(const struct iwl_queue *q)
  244. {
  245. int s = q->read_ptr - q->write_ptr;
  246. if (q->read_ptr > q->write_ptr)
  247. s -= q->n_bd;
  248. if (s <= 0)
  249. s += q->n_window;
  250. /* keep some reserve to not confuse empty and full situations */
  251. s -= 2;
  252. if (s < 0)
  253. s = 0;
  254. return s;
  255. }
  256. /**
  257. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  258. */
  259. int iwl_queue_init(struct iwl_queue *q, int count, int slots_num, u32 id)
  260. {
  261. q->n_bd = count;
  262. q->n_window = slots_num;
  263. q->id = id;
  264. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  265. * and iwl_queue_dec_wrap are broken. */
  266. if (WARN_ON(!is_power_of_2(count)))
  267. return -EINVAL;
  268. /* slots_num must be power-of-two size, otherwise
  269. * get_cmd_index is broken. */
  270. if (WARN_ON(!is_power_of_2(slots_num)))
  271. return -EINVAL;
  272. q->low_mark = q->n_window / 4;
  273. if (q->low_mark < 4)
  274. q->low_mark = 4;
  275. q->high_mark = q->n_window / 8;
  276. if (q->high_mark < 2)
  277. q->high_mark = 2;
  278. q->write_ptr = q->read_ptr = 0;
  279. return 0;
  280. }
  281. static void iwlagn_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
  282. struct iwl_tx_queue *txq)
  283. {
  284. struct iwl_trans_pcie *trans_pcie =
  285. IWL_TRANS_GET_PCIE_TRANS(trans);
  286. struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  287. int txq_id = txq->q.id;
  288. int read_ptr = txq->q.read_ptr;
  289. u8 sta_id = 0;
  290. __le16 bc_ent;
  291. WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
  292. if (txq_id != trans->shrd->cmd_queue)
  293. sta_id = txq->cmd[read_ptr]->cmd.tx.sta_id;
  294. bc_ent = cpu_to_le16(1 | (sta_id << 12));
  295. scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
  296. if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
  297. scd_bc_tbl[txq_id].
  298. tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
  299. }
  300. static int iwlagn_tx_queue_set_q2ratid(struct iwl_trans *trans, u16 ra_tid,
  301. u16 txq_id)
  302. {
  303. u32 tbl_dw_addr;
  304. u32 tbl_dw;
  305. u16 scd_q2ratid;
  306. struct iwl_trans_pcie *trans_pcie =
  307. IWL_TRANS_GET_PCIE_TRANS(trans);
  308. scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  309. tbl_dw_addr = trans_pcie->scd_base_addr +
  310. SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
  311. tbl_dw = iwl_read_targ_mem(bus(trans), tbl_dw_addr);
  312. if (txq_id & 0x1)
  313. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  314. else
  315. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  316. iwl_write_targ_mem(bus(trans), tbl_dw_addr, tbl_dw);
  317. return 0;
  318. }
  319. static void iwlagn_tx_queue_stop_scheduler(struct iwl_trans *trans, u16 txq_id)
  320. {
  321. /* Simply stop the queue, but don't change any configuration;
  322. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  323. iwl_write_prph(bus(trans),
  324. SCD_QUEUE_STATUS_BITS(txq_id),
  325. (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  326. (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  327. }
  328. void iwl_trans_set_wr_ptrs(struct iwl_trans *trans,
  329. int txq_id, u32 index)
  330. {
  331. iwl_write_direct32(bus(trans), HBUS_TARG_WRPTR,
  332. (index & 0xff) | (txq_id << 8));
  333. iwl_write_prph(bus(trans), SCD_QUEUE_RDPTR(txq_id), index);
  334. }
  335. void iwl_trans_tx_queue_set_status(struct iwl_trans *trans,
  336. struct iwl_tx_queue *txq,
  337. int tx_fifo_id, int scd_retry)
  338. {
  339. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  340. int txq_id = txq->q.id;
  341. int active =
  342. test_bit(txq_id, &trans_pcie->txq_ctx_active_msk) ? 1 : 0;
  343. iwl_write_prph(bus(trans), SCD_QUEUE_STATUS_BITS(txq_id),
  344. (active << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  345. (tx_fifo_id << SCD_QUEUE_STTS_REG_POS_TXF) |
  346. (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
  347. SCD_QUEUE_STTS_REG_MSK);
  348. txq->sched_retry = scd_retry;
  349. IWL_DEBUG_INFO(trans, "%s %s Queue %d on FIFO %d\n",
  350. active ? "Activate" : "Deactivate",
  351. scd_retry ? "BA" : "AC/CMD", txq_id, tx_fifo_id);
  352. }
  353. static inline int get_fifo_from_tid(struct iwl_trans_pcie *trans_pcie,
  354. u8 ctx, u16 tid)
  355. {
  356. const u8 *ac_to_fifo = trans_pcie->ac_to_fifo[ctx];
  357. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  358. return ac_to_fifo[tid_to_ac[tid]];
  359. /* no support for TIDs 8-15 yet */
  360. return -EINVAL;
  361. }
  362. void iwl_trans_pcie_tx_agg_setup(struct iwl_trans *trans,
  363. enum iwl_rxon_context_id ctx, int sta_id,
  364. int tid, int frame_limit)
  365. {
  366. int tx_fifo, txq_id, ssn_idx;
  367. u16 ra_tid;
  368. unsigned long flags;
  369. struct iwl_tid_data *tid_data;
  370. struct iwl_trans_pcie *trans_pcie =
  371. IWL_TRANS_GET_PCIE_TRANS(trans);
  372. if (WARN_ON(sta_id == IWL_INVALID_STATION))
  373. return;
  374. if (WARN_ON(tid >= IWL_MAX_TID_COUNT))
  375. return;
  376. tx_fifo = get_fifo_from_tid(trans_pcie, ctx, tid);
  377. if (WARN_ON(tx_fifo < 0)) {
  378. IWL_ERR(trans, "txq_agg_setup, bad fifo: %d\n", tx_fifo);
  379. return;
  380. }
  381. spin_lock_irqsave(&trans->shrd->sta_lock, flags);
  382. tid_data = &trans->shrd->tid_data[sta_id][tid];
  383. ssn_idx = SEQ_TO_SN(tid_data->seq_number);
  384. txq_id = tid_data->agg.txq_id;
  385. spin_unlock_irqrestore(&trans->shrd->sta_lock, flags);
  386. ra_tid = BUILD_RAxTID(sta_id, tid);
  387. spin_lock_irqsave(&trans->shrd->lock, flags);
  388. /* Stop this Tx queue before configuring it */
  389. iwlagn_tx_queue_stop_scheduler(trans, txq_id);
  390. /* Map receiver-address / traffic-ID to this queue */
  391. iwlagn_tx_queue_set_q2ratid(trans, ra_tid, txq_id);
  392. /* Set this queue as a chain-building queue */
  393. iwl_set_bits_prph(bus(trans), SCD_QUEUECHAIN_SEL, (1<<txq_id));
  394. /* enable aggregations for the queue */
  395. iwl_set_bits_prph(bus(trans), SCD_AGGR_SEL, (1<<txq_id));
  396. /* Place first TFD at index corresponding to start sequence number.
  397. * Assumes that ssn_idx is valid (!= 0xFFF) */
  398. trans_pcie->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  399. trans_pcie->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  400. iwl_trans_set_wr_ptrs(trans, txq_id, ssn_idx);
  401. /* Set up Tx window size and frame limit for this queue */
  402. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  403. SCD_CONTEXT_QUEUE_OFFSET(txq_id) +
  404. sizeof(u32),
  405. ((frame_limit <<
  406. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  407. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  408. ((frame_limit <<
  409. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  410. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  411. iwl_set_bits_prph(bus(trans), SCD_INTERRUPT_MASK, (1 << txq_id));
  412. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  413. iwl_trans_tx_queue_set_status(trans, &trans_pcie->txq[txq_id],
  414. tx_fifo, 1);
  415. trans_pcie->txq[txq_id].sta_id = sta_id;
  416. trans_pcie->txq[txq_id].tid = tid;
  417. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  418. }
  419. /*
  420. * Find first available (lowest unused) Tx Queue, mark it "active".
  421. * Called only when finding queue for aggregation.
  422. * Should never return anything < 7, because they should already
  423. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  424. */
  425. static int iwlagn_txq_ctx_activate_free(struct iwl_trans *trans)
  426. {
  427. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  428. int txq_id;
  429. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  430. if (!test_and_set_bit(txq_id,
  431. &trans_pcie->txq_ctx_active_msk))
  432. return txq_id;
  433. return -1;
  434. }
  435. int iwl_trans_pcie_tx_agg_alloc(struct iwl_trans *trans,
  436. enum iwl_rxon_context_id ctx, int sta_id,
  437. int tid, u16 *ssn)
  438. {
  439. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  440. struct iwl_tid_data *tid_data;
  441. unsigned long flags;
  442. int txq_id;
  443. struct iwl_priv *priv = priv(trans);
  444. txq_id = iwlagn_txq_ctx_activate_free(trans);
  445. if (txq_id == -1) {
  446. IWL_ERR(trans, "No free aggregation queue available\n");
  447. return -ENXIO;
  448. }
  449. spin_lock_irqsave(&trans->shrd->sta_lock, flags);
  450. tid_data = &trans->shrd->tid_data[sta_id][tid];
  451. *ssn = SEQ_TO_SN(tid_data->seq_number);
  452. tid_data->agg.txq_id = txq_id;
  453. iwl_set_swq_id(&trans_pcie->txq[txq_id], get_ac_from_tid(tid), txq_id);
  454. tid_data = &trans->shrd->tid_data[sta_id][tid];
  455. if (tid_data->tfds_in_queue == 0) {
  456. IWL_DEBUG_HT(trans, "HW queue is empty\n");
  457. tid_data->agg.state = IWL_AGG_ON;
  458. iwl_start_tx_ba_trans_ready(priv(trans), ctx, sta_id, tid);
  459. } else {
  460. IWL_DEBUG_HT(trans, "HW queue is NOT empty: %d packets in HW"
  461. "queue\n", tid_data->tfds_in_queue);
  462. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  463. }
  464. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  465. return 0;
  466. }
  467. void iwl_trans_pcie_txq_agg_disable(struct iwl_trans *trans, int txq_id)
  468. {
  469. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  470. iwlagn_tx_queue_stop_scheduler(trans, txq_id);
  471. iwl_clear_bits_prph(bus(trans), SCD_AGGR_SEL, (1 << txq_id));
  472. trans_pcie->txq[txq_id].q.read_ptr = 0;
  473. trans_pcie->txq[txq_id].q.write_ptr = 0;
  474. /* supposes that ssn_idx is valid (!= 0xFFF) */
  475. iwl_trans_set_wr_ptrs(trans, txq_id, 0);
  476. iwl_clear_bits_prph(bus(trans), SCD_INTERRUPT_MASK, (1 << txq_id));
  477. iwl_txq_ctx_deactivate(trans_pcie, txq_id);
  478. iwl_trans_tx_queue_set_status(trans, &trans_pcie->txq[txq_id], 0, 0);
  479. }
  480. int iwl_trans_pcie_tx_agg_disable(struct iwl_trans *trans,
  481. enum iwl_rxon_context_id ctx, int sta_id,
  482. int tid)
  483. {
  484. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  485. unsigned long flags;
  486. int read_ptr, write_ptr;
  487. struct iwl_tid_data *tid_data;
  488. int txq_id;
  489. spin_lock_irqsave(&trans->shrd->sta_lock, flags);
  490. tid_data = &trans->shrd->tid_data[sta_id][tid];
  491. txq_id = tid_data->agg.txq_id;
  492. if ((IWLAGN_FIRST_AMPDU_QUEUE > txq_id) ||
  493. (IWLAGN_FIRST_AMPDU_QUEUE +
  494. hw_params(trans).num_ampdu_queues <= txq_id)) {
  495. IWL_ERR(trans,
  496. "queue number out of range: %d, must be %d to %d\n",
  497. txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
  498. IWLAGN_FIRST_AMPDU_QUEUE +
  499. hw_params(trans).num_ampdu_queues - 1);
  500. spin_unlock_irqrestore(&trans->shrd->sta_lock, flags);
  501. return -EINVAL;
  502. }
  503. switch (trans->shrd->tid_data[sta_id][tid].agg.state) {
  504. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  505. /*
  506. * This can happen if the peer stops aggregation
  507. * again before we've had a chance to drain the
  508. * queue we selected previously, i.e. before the
  509. * session was really started completely.
  510. */
  511. IWL_DEBUG_HT(trans, "AGG stop before setup done\n");
  512. goto turn_off;
  513. case IWL_AGG_ON:
  514. break;
  515. default:
  516. IWL_WARN(trans, "Stopping AGG while state not ON"
  517. "or starting\n");
  518. }
  519. write_ptr = trans_pcie->txq[txq_id].q.write_ptr;
  520. read_ptr = trans_pcie->txq[txq_id].q.read_ptr;
  521. /* The queue is not empty */
  522. if (write_ptr != read_ptr) {
  523. IWL_DEBUG_HT(trans, "Stopping a non empty AGG HW QUEUE\n");
  524. trans->shrd->tid_data[sta_id][tid].agg.state =
  525. IWL_EMPTYING_HW_QUEUE_DELBA;
  526. spin_unlock_irqrestore(&trans->shrd->sta_lock, flags);
  527. return 0;
  528. }
  529. IWL_DEBUG_HT(trans, "HW queue is empty\n");
  530. turn_off:
  531. trans->shrd->tid_data[sta_id][tid].agg.state = IWL_AGG_OFF;
  532. /* do not restore/save irqs */
  533. spin_unlock(&trans->shrd->sta_lock);
  534. spin_lock(&trans->shrd->lock);
  535. iwl_trans_pcie_txq_agg_disable(trans, txq_id);
  536. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  537. iwl_stop_tx_ba_trans_ready(priv(trans), ctx, sta_id, tid);
  538. return 0;
  539. }
  540. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  541. /**
  542. * iwl_enqueue_hcmd - enqueue a uCode command
  543. * @priv: device private data point
  544. * @cmd: a point to the ucode command structure
  545. *
  546. * The function returns < 0 values to indicate the operation is
  547. * failed. On success, it turns the index (> 0) of command in the
  548. * command queue.
  549. */
  550. static int iwl_enqueue_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  551. {
  552. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  553. struct iwl_tx_queue *txq = &trans_pcie->txq[trans->shrd->cmd_queue];
  554. struct iwl_queue *q = &txq->q;
  555. struct iwl_device_cmd *out_cmd;
  556. struct iwl_cmd_meta *out_meta;
  557. dma_addr_t phys_addr;
  558. unsigned long flags;
  559. u32 idx;
  560. u16 copy_size, cmd_size;
  561. bool is_ct_kill = false;
  562. bool had_nocopy = false;
  563. int i;
  564. u8 *cmd_dest;
  565. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  566. const void *trace_bufs[IWL_MAX_CMD_TFDS + 1] = {};
  567. int trace_lens[IWL_MAX_CMD_TFDS + 1] = {};
  568. int trace_idx;
  569. #endif
  570. if (test_bit(STATUS_FW_ERROR, &trans->shrd->status)) {
  571. IWL_WARN(trans, "fw recovery, no hcmd send\n");
  572. return -EIO;
  573. }
  574. if ((trans->shrd->ucode_owner == IWL_OWNERSHIP_TM) &&
  575. !(cmd->flags & CMD_ON_DEMAND)) {
  576. IWL_DEBUG_HC(trans, "tm own the uCode, no regular hcmd send\n");
  577. return -EIO;
  578. }
  579. copy_size = sizeof(out_cmd->hdr);
  580. cmd_size = sizeof(out_cmd->hdr);
  581. /* need one for the header if the first is NOCOPY */
  582. BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1);
  583. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  584. if (!cmd->len[i])
  585. continue;
  586. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  587. had_nocopy = true;
  588. } else {
  589. /* NOCOPY must not be followed by normal! */
  590. if (WARN_ON(had_nocopy))
  591. return -EINVAL;
  592. copy_size += cmd->len[i];
  593. }
  594. cmd_size += cmd->len[i];
  595. }
  596. /*
  597. * If any of the command structures end up being larger than
  598. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  599. * allocated into separate TFDs, then we will need to
  600. * increase the size of the buffers.
  601. */
  602. if (WARN_ON(copy_size > TFD_MAX_PAYLOAD_SIZE))
  603. return -EINVAL;
  604. if (iwl_is_rfkill(trans->shrd) || iwl_is_ctkill(trans->shrd)) {
  605. IWL_WARN(trans, "Not sending command - %s KILL\n",
  606. iwl_is_rfkill(trans->shrd) ? "RF" : "CT");
  607. return -EIO;
  608. }
  609. spin_lock_irqsave(&trans->hcmd_lock, flags);
  610. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  611. spin_unlock_irqrestore(&trans->hcmd_lock, flags);
  612. IWL_ERR(trans, "No space in command queue\n");
  613. is_ct_kill = iwl_check_for_ct_kill(priv(trans));
  614. if (!is_ct_kill) {
  615. IWL_ERR(trans, "Restarting adapter queue is full\n");
  616. iwlagn_fw_error(priv(trans), false);
  617. }
  618. return -ENOSPC;
  619. }
  620. idx = get_cmd_index(q, q->write_ptr);
  621. out_cmd = txq->cmd[idx];
  622. out_meta = &txq->meta[idx];
  623. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  624. if (cmd->flags & CMD_WANT_SKB)
  625. out_meta->source = cmd;
  626. if (cmd->flags & CMD_ASYNC)
  627. out_meta->callback = cmd->callback;
  628. /* set up the header */
  629. out_cmd->hdr.cmd = cmd->id;
  630. out_cmd->hdr.flags = 0;
  631. out_cmd->hdr.sequence =
  632. cpu_to_le16(QUEUE_TO_SEQ(trans->shrd->cmd_queue) |
  633. INDEX_TO_SEQ(q->write_ptr));
  634. /* and copy the data that needs to be copied */
  635. cmd_dest = &out_cmd->cmd.payload[0];
  636. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  637. if (!cmd->len[i])
  638. continue;
  639. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY)
  640. break;
  641. memcpy(cmd_dest, cmd->data[i], cmd->len[i]);
  642. cmd_dest += cmd->len[i];
  643. }
  644. IWL_DEBUG_HC(trans, "Sending command %s (#%x), seq: 0x%04X, "
  645. "%d bytes at %d[%d]:%d\n",
  646. get_cmd_string(out_cmd->hdr.cmd),
  647. out_cmd->hdr.cmd,
  648. le16_to_cpu(out_cmd->hdr.sequence), cmd_size,
  649. q->write_ptr, idx, trans->shrd->cmd_queue);
  650. phys_addr = dma_map_single(bus(trans)->dev, &out_cmd->hdr, copy_size,
  651. DMA_BIDIRECTIONAL);
  652. if (unlikely(dma_mapping_error(bus(trans)->dev, phys_addr))) {
  653. idx = -ENOMEM;
  654. goto out;
  655. }
  656. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  657. dma_unmap_len_set(out_meta, len, copy_size);
  658. iwlagn_txq_attach_buf_to_tfd(trans, txq,
  659. phys_addr, copy_size, 1);
  660. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  661. trace_bufs[0] = &out_cmd->hdr;
  662. trace_lens[0] = copy_size;
  663. trace_idx = 1;
  664. #endif
  665. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  666. if (!cmd->len[i])
  667. continue;
  668. if (!(cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY))
  669. continue;
  670. phys_addr = dma_map_single(bus(trans)->dev,
  671. (void *)cmd->data[i],
  672. cmd->len[i], DMA_BIDIRECTIONAL);
  673. if (dma_mapping_error(bus(trans)->dev, phys_addr)) {
  674. iwlagn_unmap_tfd(trans, out_meta,
  675. &txq->tfds[q->write_ptr],
  676. DMA_BIDIRECTIONAL);
  677. idx = -ENOMEM;
  678. goto out;
  679. }
  680. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr,
  681. cmd->len[i], 0);
  682. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  683. trace_bufs[trace_idx] = cmd->data[i];
  684. trace_lens[trace_idx] = cmd->len[i];
  685. trace_idx++;
  686. #endif
  687. }
  688. out_meta->flags = cmd->flags;
  689. txq->need_update = 1;
  690. /* check that tracing gets all possible blocks */
  691. BUILD_BUG_ON(IWL_MAX_CMD_TFDS + 1 != 3);
  692. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  693. trace_iwlwifi_dev_hcmd(priv(trans), cmd->flags,
  694. trace_bufs[0], trace_lens[0],
  695. trace_bufs[1], trace_lens[1],
  696. trace_bufs[2], trace_lens[2]);
  697. #endif
  698. /* Increment and update queue's write index */
  699. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  700. iwl_txq_update_write_ptr(trans, txq);
  701. out:
  702. spin_unlock_irqrestore(&trans->hcmd_lock, flags);
  703. return idx;
  704. }
  705. /**
  706. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  707. *
  708. * When FW advances 'R' index, all entries between old and new 'R' index
  709. * need to be reclaimed. As result, some free space forms. If there is
  710. * enough free space (> low mark), wake the stack that feeds us.
  711. */
  712. static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id, int idx)
  713. {
  714. struct iwl_trans_pcie *trans_pcie =
  715. IWL_TRANS_GET_PCIE_TRANS(trans(priv));
  716. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  717. struct iwl_queue *q = &txq->q;
  718. int nfreed = 0;
  719. if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
  720. IWL_ERR(priv, "%s: Read index for DMA queue txq id (%d), "
  721. "index %d is out of range [0-%d] %d %d.\n", __func__,
  722. txq_id, idx, q->n_bd, q->write_ptr, q->read_ptr);
  723. return;
  724. }
  725. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  726. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  727. if (nfreed++ > 0) {
  728. IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", idx,
  729. q->write_ptr, q->read_ptr);
  730. iwlagn_fw_error(priv, false);
  731. }
  732. }
  733. }
  734. /**
  735. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  736. * @rxb: Rx buffer to reclaim
  737. *
  738. * If an Rx buffer has an async callback associated with it the callback
  739. * will be executed. The attached skb (if present) will only be freed
  740. * if the callback returns 1
  741. */
  742. void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
  743. {
  744. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  745. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  746. int txq_id = SEQ_TO_QUEUE(sequence);
  747. int index = SEQ_TO_INDEX(sequence);
  748. int cmd_index;
  749. struct iwl_device_cmd *cmd;
  750. struct iwl_cmd_meta *meta;
  751. struct iwl_trans *trans = trans(priv);
  752. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  753. struct iwl_tx_queue *txq = &trans_pcie->txq[trans->shrd->cmd_queue];
  754. unsigned long flags;
  755. /* If a Tx command is being handled and it isn't in the actual
  756. * command queue then there a command routing bug has been introduced
  757. * in the queue management code. */
  758. if (WARN(txq_id != trans->shrd->cmd_queue,
  759. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  760. txq_id, trans->shrd->cmd_queue, sequence,
  761. trans_pcie->txq[trans->shrd->cmd_queue].q.read_ptr,
  762. trans_pcie->txq[trans->shrd->cmd_queue].q.write_ptr)) {
  763. iwl_print_hex_error(priv, pkt, 32);
  764. return;
  765. }
  766. cmd_index = get_cmd_index(&txq->q, index);
  767. cmd = txq->cmd[cmd_index];
  768. meta = &txq->meta[cmd_index];
  769. iwlagn_unmap_tfd(trans, meta, &txq->tfds[index],
  770. DMA_BIDIRECTIONAL);
  771. /* Input error checking is done when commands are added to queue. */
  772. if (meta->flags & CMD_WANT_SKB) {
  773. meta->source->reply_page = (unsigned long)rxb_addr(rxb);
  774. rxb->page = NULL;
  775. } else if (meta->callback)
  776. meta->callback(priv, cmd, pkt);
  777. spin_lock_irqsave(&trans->hcmd_lock, flags);
  778. iwl_hcmd_queue_reclaim(priv, txq_id, index);
  779. if (!(meta->flags & CMD_ASYNC)) {
  780. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  781. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  782. get_cmd_string(cmd->hdr.cmd));
  783. wake_up_interruptible(&priv->wait_command_queue);
  784. }
  785. meta->flags = 0;
  786. spin_unlock_irqrestore(&trans->hcmd_lock, flags);
  787. }
  788. const char *get_cmd_string(u8 cmd)
  789. {
  790. switch (cmd) {
  791. IWL_CMD(REPLY_ALIVE);
  792. IWL_CMD(REPLY_ERROR);
  793. IWL_CMD(REPLY_RXON);
  794. IWL_CMD(REPLY_RXON_ASSOC);
  795. IWL_CMD(REPLY_QOS_PARAM);
  796. IWL_CMD(REPLY_RXON_TIMING);
  797. IWL_CMD(REPLY_ADD_STA);
  798. IWL_CMD(REPLY_REMOVE_STA);
  799. IWL_CMD(REPLY_REMOVE_ALL_STA);
  800. IWL_CMD(REPLY_TXFIFO_FLUSH);
  801. IWL_CMD(REPLY_WEPKEY);
  802. IWL_CMD(REPLY_TX);
  803. IWL_CMD(REPLY_LEDS_CMD);
  804. IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
  805. IWL_CMD(COEX_PRIORITY_TABLE_CMD);
  806. IWL_CMD(COEX_MEDIUM_NOTIFICATION);
  807. IWL_CMD(COEX_EVENT_CMD);
  808. IWL_CMD(REPLY_QUIET_CMD);
  809. IWL_CMD(REPLY_CHANNEL_SWITCH);
  810. IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
  811. IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
  812. IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
  813. IWL_CMD(POWER_TABLE_CMD);
  814. IWL_CMD(PM_SLEEP_NOTIFICATION);
  815. IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
  816. IWL_CMD(REPLY_SCAN_CMD);
  817. IWL_CMD(REPLY_SCAN_ABORT_CMD);
  818. IWL_CMD(SCAN_START_NOTIFICATION);
  819. IWL_CMD(SCAN_RESULTS_NOTIFICATION);
  820. IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
  821. IWL_CMD(BEACON_NOTIFICATION);
  822. IWL_CMD(REPLY_TX_BEACON);
  823. IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
  824. IWL_CMD(QUIET_NOTIFICATION);
  825. IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
  826. IWL_CMD(MEASURE_ABORT_NOTIFICATION);
  827. IWL_CMD(REPLY_BT_CONFIG);
  828. IWL_CMD(REPLY_STATISTICS_CMD);
  829. IWL_CMD(STATISTICS_NOTIFICATION);
  830. IWL_CMD(REPLY_CARD_STATE_CMD);
  831. IWL_CMD(CARD_STATE_NOTIFICATION);
  832. IWL_CMD(MISSED_BEACONS_NOTIFICATION);
  833. IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
  834. IWL_CMD(SENSITIVITY_CMD);
  835. IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
  836. IWL_CMD(REPLY_RX_PHY_CMD);
  837. IWL_CMD(REPLY_RX_MPDU_CMD);
  838. IWL_CMD(REPLY_RX);
  839. IWL_CMD(REPLY_COMPRESSED_BA);
  840. IWL_CMD(CALIBRATION_CFG_CMD);
  841. IWL_CMD(CALIBRATION_RES_NOTIFICATION);
  842. IWL_CMD(CALIBRATION_COMPLETE_NOTIFICATION);
  843. IWL_CMD(REPLY_TX_POWER_DBM_CMD);
  844. IWL_CMD(TEMPERATURE_NOTIFICATION);
  845. IWL_CMD(TX_ANT_CONFIGURATION_CMD);
  846. IWL_CMD(REPLY_BT_COEX_PROFILE_NOTIF);
  847. IWL_CMD(REPLY_BT_COEX_PRIO_TABLE);
  848. IWL_CMD(REPLY_BT_COEX_PROT_ENV);
  849. IWL_CMD(REPLY_WIPAN_PARAMS);
  850. IWL_CMD(REPLY_WIPAN_RXON);
  851. IWL_CMD(REPLY_WIPAN_RXON_TIMING);
  852. IWL_CMD(REPLY_WIPAN_RXON_ASSOC);
  853. IWL_CMD(REPLY_WIPAN_QOS_PARAM);
  854. IWL_CMD(REPLY_WIPAN_WEPKEY);
  855. IWL_CMD(REPLY_WIPAN_P2P_CHANNEL_SWITCH);
  856. IWL_CMD(REPLY_WIPAN_NOA_NOTIFICATION);
  857. IWL_CMD(REPLY_WIPAN_DEACTIVATION_COMPLETE);
  858. IWL_CMD(REPLY_WOWLAN_PATTERNS);
  859. IWL_CMD(REPLY_WOWLAN_WAKEUP_FILTER);
  860. IWL_CMD(REPLY_WOWLAN_TSC_RSC_PARAMS);
  861. IWL_CMD(REPLY_WOWLAN_TKIP_PARAMS);
  862. IWL_CMD(REPLY_WOWLAN_KEK_KCK_MATERIAL);
  863. IWL_CMD(REPLY_WOWLAN_GET_STATUS);
  864. default:
  865. return "UNKNOWN";
  866. }
  867. }
  868. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  869. static void iwl_generic_cmd_callback(struct iwl_priv *priv,
  870. struct iwl_device_cmd *cmd,
  871. struct iwl_rx_packet *pkt)
  872. {
  873. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  874. IWL_ERR(priv, "Bad return from %s (0x%08X)\n",
  875. get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  876. return;
  877. }
  878. #ifdef CONFIG_IWLWIFI_DEBUG
  879. switch (cmd->hdr.cmd) {
  880. case REPLY_TX_LINK_QUALITY_CMD:
  881. case SENSITIVITY_CMD:
  882. IWL_DEBUG_HC_DUMP(priv, "back from %s (0x%08X)\n",
  883. get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  884. break;
  885. default:
  886. IWL_DEBUG_HC(priv, "back from %s (0x%08X)\n",
  887. get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  888. }
  889. #endif
  890. }
  891. static int iwl_send_cmd_async(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  892. {
  893. int ret;
  894. /* An asynchronous command can not expect an SKB to be set. */
  895. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  896. return -EINVAL;
  897. /* Assign a generic callback if one is not provided */
  898. if (!cmd->callback)
  899. cmd->callback = iwl_generic_cmd_callback;
  900. if (test_bit(STATUS_EXIT_PENDING, &trans->shrd->status))
  901. return -EBUSY;
  902. ret = iwl_enqueue_hcmd(trans, cmd);
  903. if (ret < 0) {
  904. IWL_ERR(trans, "Error sending %s: enqueue_hcmd failed: %d\n",
  905. get_cmd_string(cmd->id), ret);
  906. return ret;
  907. }
  908. return 0;
  909. }
  910. static int iwl_send_cmd_sync(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  911. {
  912. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  913. int cmd_idx;
  914. int ret;
  915. lockdep_assert_held(&trans->shrd->mutex);
  916. /* A synchronous command can not have a callback set. */
  917. if (WARN_ON(cmd->callback))
  918. return -EINVAL;
  919. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
  920. get_cmd_string(cmd->id));
  921. set_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  922. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
  923. get_cmd_string(cmd->id));
  924. cmd_idx = iwl_enqueue_hcmd(trans, cmd);
  925. if (cmd_idx < 0) {
  926. ret = cmd_idx;
  927. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  928. IWL_ERR(trans, "Error sending %s: enqueue_hcmd failed: %d\n",
  929. get_cmd_string(cmd->id), ret);
  930. return ret;
  931. }
  932. ret = wait_event_interruptible_timeout(priv(trans)->wait_command_queue,
  933. !test_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status),
  934. HOST_COMPLETE_TIMEOUT);
  935. if (!ret) {
  936. if (test_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status)) {
  937. IWL_ERR(trans,
  938. "Error sending %s: time out after %dms.\n",
  939. get_cmd_string(cmd->id),
  940. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  941. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  942. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command"
  943. "%s\n", get_cmd_string(cmd->id));
  944. ret = -ETIMEDOUT;
  945. goto cancel;
  946. }
  947. }
  948. if (test_bit(STATUS_RF_KILL_HW, &trans->shrd->status)) {
  949. IWL_ERR(trans, "Command %s aborted: RF KILL Switch\n",
  950. get_cmd_string(cmd->id));
  951. ret = -ECANCELED;
  952. goto fail;
  953. }
  954. if (test_bit(STATUS_FW_ERROR, &trans->shrd->status)) {
  955. IWL_ERR(trans, "Command %s failed: FW Error\n",
  956. get_cmd_string(cmd->id));
  957. ret = -EIO;
  958. goto fail;
  959. }
  960. if ((cmd->flags & CMD_WANT_SKB) && !cmd->reply_page) {
  961. IWL_ERR(trans, "Error: Response NULL in '%s'\n",
  962. get_cmd_string(cmd->id));
  963. ret = -EIO;
  964. goto cancel;
  965. }
  966. return 0;
  967. cancel:
  968. if (cmd->flags & CMD_WANT_SKB) {
  969. /*
  970. * Cancel the CMD_WANT_SKB flag for the cmd in the
  971. * TX cmd queue. Otherwise in case the cmd comes
  972. * in later, it will possibly set an invalid
  973. * address (cmd->meta.source).
  974. */
  975. trans_pcie->txq[trans->shrd->cmd_queue].meta[cmd_idx].flags &=
  976. ~CMD_WANT_SKB;
  977. }
  978. fail:
  979. if (cmd->reply_page) {
  980. iwl_free_pages(trans->shrd, cmd->reply_page);
  981. cmd->reply_page = 0;
  982. }
  983. return ret;
  984. }
  985. int iwl_trans_pcie_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  986. {
  987. if (cmd->flags & CMD_ASYNC)
  988. return iwl_send_cmd_async(trans, cmd);
  989. return iwl_send_cmd_sync(trans, cmd);
  990. }
  991. int iwl_trans_pcie_send_cmd_pdu(struct iwl_trans *trans, u8 id, u32 flags,
  992. u16 len, const void *data)
  993. {
  994. struct iwl_host_cmd cmd = {
  995. .id = id,
  996. .len = { len, },
  997. .data = { data, },
  998. .flags = flags,
  999. };
  1000. return iwl_trans_pcie_send_cmd(trans, &cmd);
  1001. }
  1002. /* Frees buffers until index _not_ inclusive */
  1003. int iwl_tx_queue_reclaim(struct iwl_trans *trans, int txq_id, int index,
  1004. struct sk_buff_head *skbs)
  1005. {
  1006. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1007. struct iwl_tx_queue *txq = &trans_pcie->txq[txq_id];
  1008. struct iwl_queue *q = &txq->q;
  1009. int last_to_free;
  1010. int freed = 0;
  1011. /*Since we free until index _not_ inclusive, the one before index is
  1012. * the last we will free. This one must be used */
  1013. last_to_free = iwl_queue_dec_wrap(index, q->n_bd);
  1014. if ((index >= q->n_bd) ||
  1015. (iwl_queue_used(q, last_to_free) == 0)) {
  1016. IWL_ERR(trans, "%s: Read index for DMA queue txq id (%d), "
  1017. "last_to_free %d is out of range [0-%d] %d %d.\n",
  1018. __func__, txq_id, last_to_free, q->n_bd,
  1019. q->write_ptr, q->read_ptr);
  1020. return 0;
  1021. }
  1022. IWL_DEBUG_TX_REPLY(trans, "reclaim: [%d, %d, %d]\n", txq_id,
  1023. q->read_ptr, index);
  1024. if (WARN_ON(!skb_queue_empty(skbs)))
  1025. return 0;
  1026. for (;
  1027. q->read_ptr != index;
  1028. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  1029. if (WARN_ON_ONCE(txq->skbs[txq->q.read_ptr] == NULL))
  1030. continue;
  1031. __skb_queue_tail(skbs, txq->skbs[txq->q.read_ptr]);
  1032. txq->skbs[txq->q.read_ptr] = NULL;
  1033. iwlagn_txq_inval_byte_cnt_tbl(trans, txq);
  1034. iwlagn_txq_free_tfd(trans, txq, txq->q.read_ptr);
  1035. freed++;
  1036. }
  1037. return freed;
  1038. }