iwl-trans-tx-pcie.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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. int txq_id = txq->q.id;
  340. int active =
  341. test_bit(txq_id, &priv(trans)->txq_ctx_active_msk) ? 1 : 0;
  342. iwl_write_prph(bus(trans), SCD_QUEUE_STATUS_BITS(txq_id),
  343. (active << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  344. (tx_fifo_id << SCD_QUEUE_STTS_REG_POS_TXF) |
  345. (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
  346. SCD_QUEUE_STTS_REG_MSK);
  347. txq->sched_retry = scd_retry;
  348. IWL_DEBUG_INFO(trans, "%s %s Queue %d on FIFO %d\n",
  349. active ? "Activate" : "Deactivate",
  350. scd_retry ? "BA" : "AC/CMD", txq_id, tx_fifo_id);
  351. }
  352. static inline int get_fifo_from_tid(struct iwl_trans_pcie *trans_pcie,
  353. u8 ctx, u16 tid)
  354. {
  355. const u8 *ac_to_fifo = trans_pcie->ac_to_fifo[ctx];
  356. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  357. return ac_to_fifo[tid_to_ac[tid]];
  358. /* no support for TIDs 8-15 yet */
  359. return -EINVAL;
  360. }
  361. void iwl_trans_pcie_tx_agg_setup(struct iwl_trans *trans,
  362. enum iwl_rxon_context_id ctx, int sta_id,
  363. int tid, int frame_limit)
  364. {
  365. int tx_fifo, txq_id, ssn_idx;
  366. u16 ra_tid;
  367. unsigned long flags;
  368. struct iwl_tid_data *tid_data;
  369. struct iwl_trans_pcie *trans_pcie =
  370. IWL_TRANS_GET_PCIE_TRANS(trans);
  371. if (WARN_ON(sta_id == IWL_INVALID_STATION))
  372. return;
  373. if (WARN_ON(tid >= IWL_MAX_TID_COUNT))
  374. return;
  375. tx_fifo = get_fifo_from_tid(trans_pcie, ctx, tid);
  376. if (WARN_ON(tx_fifo < 0)) {
  377. IWL_ERR(trans, "txq_agg_setup, bad fifo: %d\n", tx_fifo);
  378. return;
  379. }
  380. spin_lock_irqsave(&trans->shrd->sta_lock, flags);
  381. tid_data = &trans->shrd->tid_data[sta_id][tid];
  382. ssn_idx = SEQ_TO_SN(tid_data->seq_number);
  383. txq_id = tid_data->agg.txq_id;
  384. spin_unlock_irqrestore(&trans->shrd->sta_lock, flags);
  385. ra_tid = BUILD_RAxTID(sta_id, tid);
  386. spin_lock_irqsave(&trans->shrd->lock, flags);
  387. /* Stop this Tx queue before configuring it */
  388. iwlagn_tx_queue_stop_scheduler(trans, txq_id);
  389. /* Map receiver-address / traffic-ID to this queue */
  390. iwlagn_tx_queue_set_q2ratid(trans, ra_tid, txq_id);
  391. /* Set this queue as a chain-building queue */
  392. iwl_set_bits_prph(bus(trans), SCD_QUEUECHAIN_SEL, (1<<txq_id));
  393. /* enable aggregations for the queue */
  394. iwl_set_bits_prph(bus(trans), SCD_AGGR_SEL, (1<<txq_id));
  395. /* Place first TFD at index corresponding to start sequence number.
  396. * Assumes that ssn_idx is valid (!= 0xFFF) */
  397. priv(trans)->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  398. priv(trans)->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  399. iwl_trans_set_wr_ptrs(trans, txq_id, ssn_idx);
  400. /* Set up Tx window size and frame limit for this queue */
  401. iwl_write_targ_mem(bus(trans), trans_pcie->scd_base_addr +
  402. SCD_CONTEXT_QUEUE_OFFSET(txq_id) +
  403. sizeof(u32),
  404. ((frame_limit <<
  405. SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  406. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  407. ((frame_limit <<
  408. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  409. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  410. iwl_set_bits_prph(bus(trans), SCD_INTERRUPT_MASK, (1 << txq_id));
  411. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  412. iwl_trans_tx_queue_set_status(trans, &priv(trans)->txq[txq_id],
  413. tx_fifo, 1);
  414. priv(trans)->txq[txq_id].sta_id = sta_id;
  415. priv(trans)->txq[txq_id].tid = tid;
  416. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  417. }
  418. /*
  419. * Find first available (lowest unused) Tx Queue, mark it "active".
  420. * Called only when finding queue for aggregation.
  421. * Should never return anything < 7, because they should already
  422. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  423. */
  424. static int iwlagn_txq_ctx_activate_free(struct iwl_trans *trans)
  425. {
  426. int txq_id;
  427. for (txq_id = 0; txq_id < hw_params(trans).max_txq_num; txq_id++)
  428. if (!test_and_set_bit(txq_id,
  429. &priv(trans)->txq_ctx_active_msk))
  430. return txq_id;
  431. return -1;
  432. }
  433. int iwl_trans_pcie_tx_agg_alloc(struct iwl_trans *trans,
  434. enum iwl_rxon_context_id ctx, int sta_id,
  435. int tid, u16 *ssn)
  436. {
  437. struct iwl_tid_data *tid_data;
  438. unsigned long flags;
  439. u16 txq_id;
  440. struct iwl_priv *priv = priv(trans);
  441. txq_id = iwlagn_txq_ctx_activate_free(trans);
  442. if (txq_id == -1) {
  443. IWL_ERR(trans, "No free aggregation queue available\n");
  444. return -ENXIO;
  445. }
  446. spin_lock_irqsave(&trans->shrd->sta_lock, flags);
  447. tid_data = &trans->shrd->tid_data[sta_id][tid];
  448. *ssn = SEQ_TO_SN(tid_data->seq_number);
  449. tid_data->agg.txq_id = txq_id;
  450. iwl_set_swq_id(&priv->txq[txq_id], get_ac_from_tid(tid), txq_id);
  451. tid_data = &trans->shrd->tid_data[sta_id][tid];
  452. if (tid_data->tfds_in_queue == 0) {
  453. IWL_DEBUG_HT(trans, "HW queue is empty\n");
  454. tid_data->agg.state = IWL_AGG_ON;
  455. iwl_start_tx_ba_trans_ready(priv(trans), ctx, sta_id, tid);
  456. } else {
  457. IWL_DEBUG_HT(trans, "HW queue is NOT empty: %d packets in HW"
  458. "queue\n", tid_data->tfds_in_queue);
  459. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  460. }
  461. spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
  462. return 0;
  463. }
  464. void iwl_trans_pcie_txq_agg_disable(struct iwl_trans *trans, int txq_id)
  465. {
  466. iwlagn_tx_queue_stop_scheduler(trans, txq_id);
  467. iwl_clear_bits_prph(bus(trans), SCD_AGGR_SEL, (1 << txq_id));
  468. priv(trans)->txq[txq_id].q.read_ptr = 0;
  469. priv(trans)->txq[txq_id].q.write_ptr = 0;
  470. /* supposes that ssn_idx is valid (!= 0xFFF) */
  471. iwl_trans_set_wr_ptrs(trans, txq_id, 0);
  472. iwl_clear_bits_prph(bus(trans), SCD_INTERRUPT_MASK, (1 << txq_id));
  473. iwl_txq_ctx_deactivate(priv(trans), txq_id);
  474. iwl_trans_tx_queue_set_status(trans, &priv(trans)->txq[txq_id], 0, 0);
  475. }
  476. int iwl_trans_pcie_tx_agg_disable(struct iwl_trans *trans,
  477. enum iwl_rxon_context_id ctx, int sta_id,
  478. int tid)
  479. {
  480. unsigned long flags;
  481. int read_ptr, write_ptr;
  482. struct iwl_tid_data *tid_data;
  483. int txq_id;
  484. spin_lock_irqsave(&trans->shrd->sta_lock, flags);
  485. tid_data = &trans->shrd->tid_data[sta_id][tid];
  486. txq_id = tid_data->agg.txq_id;
  487. if ((IWLAGN_FIRST_AMPDU_QUEUE > txq_id) ||
  488. (IWLAGN_FIRST_AMPDU_QUEUE +
  489. hw_params(trans).num_ampdu_queues <= txq_id)) {
  490. IWL_ERR(trans,
  491. "queue number out of range: %d, must be %d to %d\n",
  492. txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
  493. IWLAGN_FIRST_AMPDU_QUEUE +
  494. hw_params(trans).num_ampdu_queues - 1);
  495. spin_unlock_irqrestore(&trans->shrd->sta_lock, flags);
  496. return -EINVAL;
  497. }
  498. switch (trans->shrd->tid_data[sta_id][tid].agg.state) {
  499. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  500. /*
  501. * This can happen if the peer stops aggregation
  502. * again before we've had a chance to drain the
  503. * queue we selected previously, i.e. before the
  504. * session was really started completely.
  505. */
  506. IWL_DEBUG_HT(trans, "AGG stop before setup done\n");
  507. goto turn_off;
  508. case IWL_AGG_ON:
  509. break;
  510. default:
  511. IWL_WARN(trans, "Stopping AGG while state not ON"
  512. "or starting\n");
  513. }
  514. write_ptr = priv(trans)->txq[txq_id].q.write_ptr;
  515. read_ptr = priv(trans)->txq[txq_id].q.read_ptr;
  516. /* The queue is not empty */
  517. if (write_ptr != read_ptr) {
  518. IWL_DEBUG_HT(trans, "Stopping a non empty AGG HW QUEUE\n");
  519. trans->shrd->tid_data[sta_id][tid].agg.state =
  520. IWL_EMPTYING_HW_QUEUE_DELBA;
  521. spin_unlock_irqrestore(&trans->shrd->sta_lock, flags);
  522. return 0;
  523. }
  524. IWL_DEBUG_HT(trans, "HW queue is empty\n");
  525. turn_off:
  526. trans->shrd->tid_data[sta_id][tid].agg.state = IWL_AGG_OFF;
  527. /* do not restore/save irqs */
  528. spin_unlock(&trans->shrd->sta_lock);
  529. spin_lock(&trans->shrd->lock);
  530. iwl_trans_pcie_txq_agg_disable(trans, txq_id);
  531. spin_unlock_irqrestore(&trans->shrd->lock, flags);
  532. iwl_stop_tx_ba_trans_ready(priv(trans), ctx, sta_id, tid);
  533. return 0;
  534. }
  535. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  536. /**
  537. * iwl_enqueue_hcmd - enqueue a uCode command
  538. * @priv: device private data point
  539. * @cmd: a point to the ucode command structure
  540. *
  541. * The function returns < 0 values to indicate the operation is
  542. * failed. On success, it turns the index (> 0) of command in the
  543. * command queue.
  544. */
  545. static int iwl_enqueue_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  546. {
  547. struct iwl_tx_queue *txq = &priv(trans)->txq[trans->shrd->cmd_queue];
  548. struct iwl_queue *q = &txq->q;
  549. struct iwl_device_cmd *out_cmd;
  550. struct iwl_cmd_meta *out_meta;
  551. dma_addr_t phys_addr;
  552. unsigned long flags;
  553. u32 idx;
  554. u16 copy_size, cmd_size;
  555. bool is_ct_kill = false;
  556. bool had_nocopy = false;
  557. int i;
  558. u8 *cmd_dest;
  559. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  560. const void *trace_bufs[IWL_MAX_CMD_TFDS + 1] = {};
  561. int trace_lens[IWL_MAX_CMD_TFDS + 1] = {};
  562. int trace_idx;
  563. #endif
  564. if (test_bit(STATUS_FW_ERROR, &trans->shrd->status)) {
  565. IWL_WARN(trans, "fw recovery, no hcmd send\n");
  566. return -EIO;
  567. }
  568. if ((trans->shrd->ucode_owner == IWL_OWNERSHIP_TM) &&
  569. !(cmd->flags & CMD_ON_DEMAND)) {
  570. IWL_DEBUG_HC(trans, "tm own the uCode, no regular hcmd send\n");
  571. return -EIO;
  572. }
  573. copy_size = sizeof(out_cmd->hdr);
  574. cmd_size = sizeof(out_cmd->hdr);
  575. /* need one for the header if the first is NOCOPY */
  576. BUILD_BUG_ON(IWL_MAX_CMD_TFDS > IWL_NUM_OF_TBS - 1);
  577. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  578. if (!cmd->len[i])
  579. continue;
  580. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  581. had_nocopy = true;
  582. } else {
  583. /* NOCOPY must not be followed by normal! */
  584. if (WARN_ON(had_nocopy))
  585. return -EINVAL;
  586. copy_size += cmd->len[i];
  587. }
  588. cmd_size += cmd->len[i];
  589. }
  590. /*
  591. * If any of the command structures end up being larger than
  592. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  593. * allocated into separate TFDs, then we will need to
  594. * increase the size of the buffers.
  595. */
  596. if (WARN_ON(copy_size > TFD_MAX_PAYLOAD_SIZE))
  597. return -EINVAL;
  598. if (iwl_is_rfkill(trans->shrd) || iwl_is_ctkill(trans->shrd)) {
  599. IWL_WARN(trans, "Not sending command - %s KILL\n",
  600. iwl_is_rfkill(trans->shrd) ? "RF" : "CT");
  601. return -EIO;
  602. }
  603. spin_lock_irqsave(&trans->hcmd_lock, flags);
  604. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  605. spin_unlock_irqrestore(&trans->hcmd_lock, flags);
  606. IWL_ERR(trans, "No space in command queue\n");
  607. is_ct_kill = iwl_check_for_ct_kill(priv(trans));
  608. if (!is_ct_kill) {
  609. IWL_ERR(trans, "Restarting adapter queue is full\n");
  610. iwlagn_fw_error(priv(trans), false);
  611. }
  612. return -ENOSPC;
  613. }
  614. idx = get_cmd_index(q, q->write_ptr);
  615. out_cmd = txq->cmd[idx];
  616. out_meta = &txq->meta[idx];
  617. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  618. if (cmd->flags & CMD_WANT_SKB)
  619. out_meta->source = cmd;
  620. if (cmd->flags & CMD_ASYNC)
  621. out_meta->callback = cmd->callback;
  622. /* set up the header */
  623. out_cmd->hdr.cmd = cmd->id;
  624. out_cmd->hdr.flags = 0;
  625. out_cmd->hdr.sequence =
  626. cpu_to_le16(QUEUE_TO_SEQ(trans->shrd->cmd_queue) |
  627. INDEX_TO_SEQ(q->write_ptr));
  628. /* and copy the data that needs to be copied */
  629. cmd_dest = &out_cmd->cmd.payload[0];
  630. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  631. if (!cmd->len[i])
  632. continue;
  633. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY)
  634. break;
  635. memcpy(cmd_dest, cmd->data[i], cmd->len[i]);
  636. cmd_dest += cmd->len[i];
  637. }
  638. IWL_DEBUG_HC(trans, "Sending command %s (#%x), seq: 0x%04X, "
  639. "%d bytes at %d[%d]:%d\n",
  640. get_cmd_string(out_cmd->hdr.cmd),
  641. out_cmd->hdr.cmd,
  642. le16_to_cpu(out_cmd->hdr.sequence), cmd_size,
  643. q->write_ptr, idx, trans->shrd->cmd_queue);
  644. phys_addr = dma_map_single(bus(trans)->dev, &out_cmd->hdr, copy_size,
  645. DMA_BIDIRECTIONAL);
  646. if (unlikely(dma_mapping_error(bus(trans)->dev, phys_addr))) {
  647. idx = -ENOMEM;
  648. goto out;
  649. }
  650. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  651. dma_unmap_len_set(out_meta, len, copy_size);
  652. iwlagn_txq_attach_buf_to_tfd(trans, txq,
  653. phys_addr, copy_size, 1);
  654. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  655. trace_bufs[0] = &out_cmd->hdr;
  656. trace_lens[0] = copy_size;
  657. trace_idx = 1;
  658. #endif
  659. for (i = 0; i < IWL_MAX_CMD_TFDS; i++) {
  660. if (!cmd->len[i])
  661. continue;
  662. if (!(cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY))
  663. continue;
  664. phys_addr = dma_map_single(bus(trans)->dev,
  665. (void *)cmd->data[i],
  666. cmd->len[i], DMA_BIDIRECTIONAL);
  667. if (dma_mapping_error(bus(trans)->dev, phys_addr)) {
  668. iwlagn_unmap_tfd(trans, out_meta,
  669. &txq->tfds[q->write_ptr],
  670. DMA_BIDIRECTIONAL);
  671. idx = -ENOMEM;
  672. goto out;
  673. }
  674. iwlagn_txq_attach_buf_to_tfd(trans, txq, phys_addr,
  675. cmd->len[i], 0);
  676. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  677. trace_bufs[trace_idx] = cmd->data[i];
  678. trace_lens[trace_idx] = cmd->len[i];
  679. trace_idx++;
  680. #endif
  681. }
  682. out_meta->flags = cmd->flags;
  683. txq->need_update = 1;
  684. /* check that tracing gets all possible blocks */
  685. BUILD_BUG_ON(IWL_MAX_CMD_TFDS + 1 != 3);
  686. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  687. trace_iwlwifi_dev_hcmd(priv(trans), cmd->flags,
  688. trace_bufs[0], trace_lens[0],
  689. trace_bufs[1], trace_lens[1],
  690. trace_bufs[2], trace_lens[2]);
  691. #endif
  692. /* Increment and update queue's write index */
  693. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  694. iwl_txq_update_write_ptr(trans, txq);
  695. out:
  696. spin_unlock_irqrestore(&trans->hcmd_lock, flags);
  697. return idx;
  698. }
  699. /**
  700. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  701. *
  702. * When FW advances 'R' index, all entries between old and new 'R' index
  703. * need to be reclaimed. As result, some free space forms. If there is
  704. * enough free space (> low mark), wake the stack that feeds us.
  705. */
  706. static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id, int idx)
  707. {
  708. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  709. struct iwl_queue *q = &txq->q;
  710. int nfreed = 0;
  711. if ((idx >= q->n_bd) || (iwl_queue_used(q, idx) == 0)) {
  712. IWL_ERR(priv, "%s: Read index for DMA queue txq id (%d), "
  713. "index %d is out of range [0-%d] %d %d.\n", __func__,
  714. txq_id, idx, q->n_bd, q->write_ptr, q->read_ptr);
  715. return;
  716. }
  717. for (idx = iwl_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  718. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  719. if (nfreed++ > 0) {
  720. IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", idx,
  721. q->write_ptr, q->read_ptr);
  722. iwlagn_fw_error(priv, false);
  723. }
  724. }
  725. }
  726. /**
  727. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  728. * @rxb: Rx buffer to reclaim
  729. *
  730. * If an Rx buffer has an async callback associated with it the callback
  731. * will be executed. The attached skb (if present) will only be freed
  732. * if the callback returns 1
  733. */
  734. void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
  735. {
  736. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  737. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  738. int txq_id = SEQ_TO_QUEUE(sequence);
  739. int index = SEQ_TO_INDEX(sequence);
  740. int cmd_index;
  741. struct iwl_device_cmd *cmd;
  742. struct iwl_cmd_meta *meta;
  743. struct iwl_trans *trans = trans(priv);
  744. struct iwl_tx_queue *txq = &priv->txq[trans->shrd->cmd_queue];
  745. unsigned long flags;
  746. /* If a Tx command is being handled and it isn't in the actual
  747. * command queue then there a command routing bug has been introduced
  748. * in the queue management code. */
  749. if (WARN(txq_id != trans->shrd->cmd_queue,
  750. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  751. txq_id, trans->shrd->cmd_queue, sequence,
  752. priv->txq[trans->shrd->cmd_queue].q.read_ptr,
  753. priv->txq[trans->shrd->cmd_queue].q.write_ptr)) {
  754. iwl_print_hex_error(priv, pkt, 32);
  755. return;
  756. }
  757. cmd_index = get_cmd_index(&txq->q, index);
  758. cmd = txq->cmd[cmd_index];
  759. meta = &txq->meta[cmd_index];
  760. iwlagn_unmap_tfd(trans, meta, &txq->tfds[index],
  761. DMA_BIDIRECTIONAL);
  762. /* Input error checking is done when commands are added to queue. */
  763. if (meta->flags & CMD_WANT_SKB) {
  764. meta->source->reply_page = (unsigned long)rxb_addr(rxb);
  765. rxb->page = NULL;
  766. } else if (meta->callback)
  767. meta->callback(priv, cmd, pkt);
  768. spin_lock_irqsave(&trans->hcmd_lock, flags);
  769. iwl_hcmd_queue_reclaim(priv, txq_id, index);
  770. if (!(meta->flags & CMD_ASYNC)) {
  771. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  772. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  773. get_cmd_string(cmd->hdr.cmd));
  774. wake_up_interruptible(&priv->wait_command_queue);
  775. }
  776. meta->flags = 0;
  777. spin_unlock_irqrestore(&trans->hcmd_lock, flags);
  778. }
  779. const char *get_cmd_string(u8 cmd)
  780. {
  781. switch (cmd) {
  782. IWL_CMD(REPLY_ALIVE);
  783. IWL_CMD(REPLY_ERROR);
  784. IWL_CMD(REPLY_RXON);
  785. IWL_CMD(REPLY_RXON_ASSOC);
  786. IWL_CMD(REPLY_QOS_PARAM);
  787. IWL_CMD(REPLY_RXON_TIMING);
  788. IWL_CMD(REPLY_ADD_STA);
  789. IWL_CMD(REPLY_REMOVE_STA);
  790. IWL_CMD(REPLY_REMOVE_ALL_STA);
  791. IWL_CMD(REPLY_TXFIFO_FLUSH);
  792. IWL_CMD(REPLY_WEPKEY);
  793. IWL_CMD(REPLY_TX);
  794. IWL_CMD(REPLY_LEDS_CMD);
  795. IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
  796. IWL_CMD(COEX_PRIORITY_TABLE_CMD);
  797. IWL_CMD(COEX_MEDIUM_NOTIFICATION);
  798. IWL_CMD(COEX_EVENT_CMD);
  799. IWL_CMD(REPLY_QUIET_CMD);
  800. IWL_CMD(REPLY_CHANNEL_SWITCH);
  801. IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
  802. IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
  803. IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
  804. IWL_CMD(POWER_TABLE_CMD);
  805. IWL_CMD(PM_SLEEP_NOTIFICATION);
  806. IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
  807. IWL_CMD(REPLY_SCAN_CMD);
  808. IWL_CMD(REPLY_SCAN_ABORT_CMD);
  809. IWL_CMD(SCAN_START_NOTIFICATION);
  810. IWL_CMD(SCAN_RESULTS_NOTIFICATION);
  811. IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
  812. IWL_CMD(BEACON_NOTIFICATION);
  813. IWL_CMD(REPLY_TX_BEACON);
  814. IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
  815. IWL_CMD(QUIET_NOTIFICATION);
  816. IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
  817. IWL_CMD(MEASURE_ABORT_NOTIFICATION);
  818. IWL_CMD(REPLY_BT_CONFIG);
  819. IWL_CMD(REPLY_STATISTICS_CMD);
  820. IWL_CMD(STATISTICS_NOTIFICATION);
  821. IWL_CMD(REPLY_CARD_STATE_CMD);
  822. IWL_CMD(CARD_STATE_NOTIFICATION);
  823. IWL_CMD(MISSED_BEACONS_NOTIFICATION);
  824. IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
  825. IWL_CMD(SENSITIVITY_CMD);
  826. IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
  827. IWL_CMD(REPLY_RX_PHY_CMD);
  828. IWL_CMD(REPLY_RX_MPDU_CMD);
  829. IWL_CMD(REPLY_RX);
  830. IWL_CMD(REPLY_COMPRESSED_BA);
  831. IWL_CMD(CALIBRATION_CFG_CMD);
  832. IWL_CMD(CALIBRATION_RES_NOTIFICATION);
  833. IWL_CMD(CALIBRATION_COMPLETE_NOTIFICATION);
  834. IWL_CMD(REPLY_TX_POWER_DBM_CMD);
  835. IWL_CMD(TEMPERATURE_NOTIFICATION);
  836. IWL_CMD(TX_ANT_CONFIGURATION_CMD);
  837. IWL_CMD(REPLY_BT_COEX_PROFILE_NOTIF);
  838. IWL_CMD(REPLY_BT_COEX_PRIO_TABLE);
  839. IWL_CMD(REPLY_BT_COEX_PROT_ENV);
  840. IWL_CMD(REPLY_WIPAN_PARAMS);
  841. IWL_CMD(REPLY_WIPAN_RXON);
  842. IWL_CMD(REPLY_WIPAN_RXON_TIMING);
  843. IWL_CMD(REPLY_WIPAN_RXON_ASSOC);
  844. IWL_CMD(REPLY_WIPAN_QOS_PARAM);
  845. IWL_CMD(REPLY_WIPAN_WEPKEY);
  846. IWL_CMD(REPLY_WIPAN_P2P_CHANNEL_SWITCH);
  847. IWL_CMD(REPLY_WIPAN_NOA_NOTIFICATION);
  848. IWL_CMD(REPLY_WIPAN_DEACTIVATION_COMPLETE);
  849. IWL_CMD(REPLY_WOWLAN_PATTERNS);
  850. IWL_CMD(REPLY_WOWLAN_WAKEUP_FILTER);
  851. IWL_CMD(REPLY_WOWLAN_TSC_RSC_PARAMS);
  852. IWL_CMD(REPLY_WOWLAN_TKIP_PARAMS);
  853. IWL_CMD(REPLY_WOWLAN_KEK_KCK_MATERIAL);
  854. IWL_CMD(REPLY_WOWLAN_GET_STATUS);
  855. default:
  856. return "UNKNOWN";
  857. }
  858. }
  859. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  860. static void iwl_generic_cmd_callback(struct iwl_priv *priv,
  861. struct iwl_device_cmd *cmd,
  862. struct iwl_rx_packet *pkt)
  863. {
  864. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  865. IWL_ERR(priv, "Bad return from %s (0x%08X)\n",
  866. get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  867. return;
  868. }
  869. #ifdef CONFIG_IWLWIFI_DEBUG
  870. switch (cmd->hdr.cmd) {
  871. case REPLY_TX_LINK_QUALITY_CMD:
  872. case SENSITIVITY_CMD:
  873. IWL_DEBUG_HC_DUMP(priv, "back from %s (0x%08X)\n",
  874. get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  875. break;
  876. default:
  877. IWL_DEBUG_HC(priv, "back from %s (0x%08X)\n",
  878. get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  879. }
  880. #endif
  881. }
  882. static int iwl_send_cmd_async(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  883. {
  884. int ret;
  885. /* An asynchronous command can not expect an SKB to be set. */
  886. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  887. return -EINVAL;
  888. /* Assign a generic callback if one is not provided */
  889. if (!cmd->callback)
  890. cmd->callback = iwl_generic_cmd_callback;
  891. if (test_bit(STATUS_EXIT_PENDING, &trans->shrd->status))
  892. return -EBUSY;
  893. ret = iwl_enqueue_hcmd(trans, cmd);
  894. if (ret < 0) {
  895. IWL_ERR(trans, "Error sending %s: enqueue_hcmd failed: %d\n",
  896. get_cmd_string(cmd->id), ret);
  897. return ret;
  898. }
  899. return 0;
  900. }
  901. static int iwl_send_cmd_sync(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  902. {
  903. int cmd_idx;
  904. int ret;
  905. lockdep_assert_held(&trans->shrd->mutex);
  906. /* A synchronous command can not have a callback set. */
  907. if (WARN_ON(cmd->callback))
  908. return -EINVAL;
  909. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
  910. get_cmd_string(cmd->id));
  911. set_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  912. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
  913. get_cmd_string(cmd->id));
  914. cmd_idx = iwl_enqueue_hcmd(trans, cmd);
  915. if (cmd_idx < 0) {
  916. ret = cmd_idx;
  917. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  918. IWL_ERR(trans, "Error sending %s: enqueue_hcmd failed: %d\n",
  919. get_cmd_string(cmd->id), ret);
  920. return ret;
  921. }
  922. ret = wait_event_interruptible_timeout(priv(trans)->wait_command_queue,
  923. !test_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status),
  924. HOST_COMPLETE_TIMEOUT);
  925. if (!ret) {
  926. if (test_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status)) {
  927. IWL_ERR(trans,
  928. "Error sending %s: time out after %dms.\n",
  929. get_cmd_string(cmd->id),
  930. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  931. clear_bit(STATUS_HCMD_ACTIVE, &trans->shrd->status);
  932. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command"
  933. "%s\n", get_cmd_string(cmd->id));
  934. ret = -ETIMEDOUT;
  935. goto cancel;
  936. }
  937. }
  938. if (test_bit(STATUS_RF_KILL_HW, &trans->shrd->status)) {
  939. IWL_ERR(trans, "Command %s aborted: RF KILL Switch\n",
  940. get_cmd_string(cmd->id));
  941. ret = -ECANCELED;
  942. goto fail;
  943. }
  944. if (test_bit(STATUS_FW_ERROR, &trans->shrd->status)) {
  945. IWL_ERR(trans, "Command %s failed: FW Error\n",
  946. get_cmd_string(cmd->id));
  947. ret = -EIO;
  948. goto fail;
  949. }
  950. if ((cmd->flags & CMD_WANT_SKB) && !cmd->reply_page) {
  951. IWL_ERR(trans, "Error: Response NULL in '%s'\n",
  952. get_cmd_string(cmd->id));
  953. ret = -EIO;
  954. goto cancel;
  955. }
  956. return 0;
  957. cancel:
  958. if (cmd->flags & CMD_WANT_SKB) {
  959. /*
  960. * Cancel the CMD_WANT_SKB flag for the cmd in the
  961. * TX cmd queue. Otherwise in case the cmd comes
  962. * in later, it will possibly set an invalid
  963. * address (cmd->meta.source).
  964. */
  965. priv(trans)->txq[trans->shrd->cmd_queue].meta[cmd_idx].flags &=
  966. ~CMD_WANT_SKB;
  967. }
  968. fail:
  969. if (cmd->reply_page) {
  970. iwl_free_pages(trans->shrd, cmd->reply_page);
  971. cmd->reply_page = 0;
  972. }
  973. return ret;
  974. }
  975. int iwl_trans_pcie_send_cmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  976. {
  977. if (cmd->flags & CMD_ASYNC)
  978. return iwl_send_cmd_async(trans, cmd);
  979. return iwl_send_cmd_sync(trans, cmd);
  980. }
  981. int iwl_trans_pcie_send_cmd_pdu(struct iwl_trans *trans, u8 id, u32 flags,
  982. u16 len, const void *data)
  983. {
  984. struct iwl_host_cmd cmd = {
  985. .id = id,
  986. .len = { len, },
  987. .data = { data, },
  988. .flags = flags,
  989. };
  990. return iwl_trans_pcie_send_cmd(trans, &cmd);
  991. }
  992. /* Frees buffers until index _not_ inclusive */
  993. int iwl_tx_queue_reclaim(struct iwl_trans *trans, int txq_id, int index,
  994. struct sk_buff_head *skbs)
  995. {
  996. struct iwl_tx_queue *txq = &priv(trans)->txq[txq_id];
  997. struct iwl_queue *q = &txq->q;
  998. int last_to_free;
  999. int freed = 0;
  1000. /*Since we free until index _not_ inclusive, the one before index is
  1001. * the last we will free. This one must be used */
  1002. last_to_free = iwl_queue_dec_wrap(index, q->n_bd);
  1003. if ((index >= q->n_bd) ||
  1004. (iwl_queue_used(q, last_to_free) == 0)) {
  1005. IWL_ERR(trans, "%s: Read index for DMA queue txq id (%d), "
  1006. "last_to_free %d is out of range [0-%d] %d %d.\n",
  1007. __func__, txq_id, last_to_free, q->n_bd,
  1008. q->write_ptr, q->read_ptr);
  1009. return 0;
  1010. }
  1011. IWL_DEBUG_TX_REPLY(trans, "reclaim: [%d, %d, %d]\n", txq_id,
  1012. q->read_ptr, index);
  1013. if (WARN_ON(!skb_queue_empty(skbs)))
  1014. return 0;
  1015. for (;
  1016. q->read_ptr != index;
  1017. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  1018. if (WARN_ON_ONCE(txq->skbs[txq->q.read_ptr] == NULL))
  1019. continue;
  1020. __skb_queue_tail(skbs, txq->skbs[txq->q.read_ptr]);
  1021. txq->skbs[txq->q.read_ptr] = NULL;
  1022. iwlagn_txq_inval_byte_cnt_tbl(trans, txq);
  1023. iwlagn_txq_free_tfd(trans, txq, txq->q.read_ptr);
  1024. freed++;
  1025. }
  1026. return freed;
  1027. }