iwl-agn-lib.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  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/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/sched.h>
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-io.h"
  36. #include "iwl-helpers.h"
  37. #include "iwl-agn-hw.h"
  38. #include "iwl-agn.h"
  39. static inline u32 iwlagn_get_scd_ssn(struct iwl5000_tx_resp *tx_resp)
  40. {
  41. return le32_to_cpup((__le32 *)&tx_resp->status +
  42. tx_resp->frame_count) & MAX_SN;
  43. }
  44. static int iwlagn_tx_status_reply_tx(struct iwl_priv *priv,
  45. struct iwl_ht_agg *agg,
  46. struct iwl5000_tx_resp *tx_resp,
  47. int txq_id, u16 start_idx)
  48. {
  49. u16 status;
  50. struct agg_tx_status *frame_status = &tx_resp->status;
  51. struct ieee80211_tx_info *info = NULL;
  52. struct ieee80211_hdr *hdr = NULL;
  53. u32 rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  54. int i, sh, idx;
  55. u16 seq;
  56. if (agg->wait_for_ba)
  57. IWL_DEBUG_TX_REPLY(priv, "got tx response w/o block-ack\n");
  58. agg->frame_count = tx_resp->frame_count;
  59. agg->start_idx = start_idx;
  60. agg->rate_n_flags = rate_n_flags;
  61. agg->bitmap = 0;
  62. /* # frames attempted by Tx command */
  63. if (agg->frame_count == 1) {
  64. /* Only one frame was attempted; no block-ack will arrive */
  65. status = le16_to_cpu(frame_status[0].status);
  66. idx = start_idx;
  67. /* FIXME: code repetition */
  68. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, StartIdx=%d idx=%d\n",
  69. agg->frame_count, agg->start_idx, idx);
  70. info = IEEE80211_SKB_CB(priv->txq[txq_id].txb[idx].skb[0]);
  71. info->status.rates[0].count = tx_resp->failure_frame + 1;
  72. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  73. info->flags |= iwl_tx_status_to_mac80211(status);
  74. iwl_hwrate_to_tx_control(priv, rate_n_flags, info);
  75. /* FIXME: code repetition end */
  76. IWL_DEBUG_TX_REPLY(priv, "1 Frame 0x%x failure :%d\n",
  77. status & 0xff, tx_resp->failure_frame);
  78. IWL_DEBUG_TX_REPLY(priv, "Rate Info rate_n_flags=%x\n", rate_n_flags);
  79. agg->wait_for_ba = 0;
  80. } else {
  81. /* Two or more frames were attempted; expect block-ack */
  82. u64 bitmap = 0;
  83. int start = agg->start_idx;
  84. /* Construct bit-map of pending frames within Tx window */
  85. for (i = 0; i < agg->frame_count; i++) {
  86. u16 sc;
  87. status = le16_to_cpu(frame_status[i].status);
  88. seq = le16_to_cpu(frame_status[i].sequence);
  89. idx = SEQ_TO_INDEX(seq);
  90. txq_id = SEQ_TO_QUEUE(seq);
  91. if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
  92. AGG_TX_STATE_ABORT_MSK))
  93. continue;
  94. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, txq_id=%d idx=%d\n",
  95. agg->frame_count, txq_id, idx);
  96. hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
  97. if (!hdr) {
  98. IWL_ERR(priv,
  99. "BUG_ON idx doesn't point to valid skb"
  100. " idx=%d, txq_id=%d\n", idx, txq_id);
  101. return -1;
  102. }
  103. sc = le16_to_cpu(hdr->seq_ctrl);
  104. if (idx != (SEQ_TO_SN(sc) & 0xff)) {
  105. IWL_ERR(priv,
  106. "BUG_ON idx doesn't match seq control"
  107. " idx=%d, seq_idx=%d, seq=%d\n",
  108. idx, SEQ_TO_SN(sc),
  109. hdr->seq_ctrl);
  110. return -1;
  111. }
  112. IWL_DEBUG_TX_REPLY(priv, "AGG Frame i=%d idx %d seq=%d\n",
  113. i, idx, SEQ_TO_SN(sc));
  114. sh = idx - start;
  115. if (sh > 64) {
  116. sh = (start - idx) + 0xff;
  117. bitmap = bitmap << sh;
  118. sh = 0;
  119. start = idx;
  120. } else if (sh < -64)
  121. sh = 0xff - (start - idx);
  122. else if (sh < 0) {
  123. sh = start - idx;
  124. start = idx;
  125. bitmap = bitmap << sh;
  126. sh = 0;
  127. }
  128. bitmap |= 1ULL << sh;
  129. IWL_DEBUG_TX_REPLY(priv, "start=%d bitmap=0x%llx\n",
  130. start, (unsigned long long)bitmap);
  131. }
  132. agg->bitmap = bitmap;
  133. agg->start_idx = start;
  134. IWL_DEBUG_TX_REPLY(priv, "Frames %d start_idx=%d bitmap=0x%llx\n",
  135. agg->frame_count, agg->start_idx,
  136. (unsigned long long)agg->bitmap);
  137. if (bitmap)
  138. agg->wait_for_ba = 1;
  139. }
  140. return 0;
  141. }
  142. static void iwlagn_rx_reply_tx(struct iwl_priv *priv,
  143. struct iwl_rx_mem_buffer *rxb)
  144. {
  145. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  146. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  147. int txq_id = SEQ_TO_QUEUE(sequence);
  148. int index = SEQ_TO_INDEX(sequence);
  149. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  150. struct ieee80211_tx_info *info;
  151. struct iwl5000_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  152. u32 status = le16_to_cpu(tx_resp->status.status);
  153. int tid;
  154. int sta_id;
  155. int freed;
  156. if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
  157. IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
  158. "is out of range [0-%d] %d %d\n", txq_id,
  159. index, txq->q.n_bd, txq->q.write_ptr,
  160. txq->q.read_ptr);
  161. return;
  162. }
  163. info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb[0]);
  164. memset(&info->status, 0, sizeof(info->status));
  165. tid = (tx_resp->ra_tid & IWL50_TX_RES_TID_MSK) >> IWL50_TX_RES_TID_POS;
  166. sta_id = (tx_resp->ra_tid & IWL50_TX_RES_RA_MSK) >> IWL50_TX_RES_RA_POS;
  167. if (txq->sched_retry) {
  168. const u32 scd_ssn = iwlagn_get_scd_ssn(tx_resp);
  169. struct iwl_ht_agg *agg = NULL;
  170. agg = &priv->stations[sta_id].tid[tid].agg;
  171. iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
  172. /* check if BAR is needed */
  173. if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
  174. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  175. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  176. index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  177. IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
  178. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  179. scd_ssn , index, txq_id, txq->swq_id);
  180. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  181. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  182. if (priv->mac80211_registered &&
  183. (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  184. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)) {
  185. if (agg->state == IWL_AGG_OFF)
  186. iwl_wake_queue(priv, txq_id);
  187. else
  188. iwl_wake_queue(priv, txq->swq_id);
  189. }
  190. }
  191. } else {
  192. BUG_ON(txq_id != txq->swq_id);
  193. info->status.rates[0].count = tx_resp->failure_frame + 1;
  194. info->flags |= iwl_tx_status_to_mac80211(status);
  195. iwl_hwrate_to_tx_control(priv,
  196. le32_to_cpu(tx_resp->rate_n_flags),
  197. info);
  198. IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
  199. "0x%x retries %d\n",
  200. txq_id,
  201. iwl_get_tx_fail_reason(status), status,
  202. le32_to_cpu(tx_resp->rate_n_flags),
  203. tx_resp->failure_frame);
  204. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  205. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  206. if (priv->mac80211_registered &&
  207. (iwl_queue_space(&txq->q) > txq->q.low_mark))
  208. iwl_wake_queue(priv, txq_id);
  209. }
  210. iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
  211. if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
  212. IWL_ERR(priv, "TODO: Implement Tx ABORT REQUIRED!!!\n");
  213. }
  214. void iwlagn_rx_handler_setup(struct iwl_priv *priv)
  215. {
  216. /* init calibration handlers */
  217. priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
  218. iwlagn_rx_calib_result;
  219. priv->rx_handlers[CALIBRATION_COMPLETE_NOTIFICATION] =
  220. iwlagn_rx_calib_complete;
  221. priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
  222. }
  223. void iwlagn_setup_deferred_work(struct iwl_priv *priv)
  224. {
  225. /* in agn, the tx power calibration is done in uCode */
  226. priv->disable_tx_power_cal = 1;
  227. }
  228. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  229. {
  230. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  231. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  232. }
  233. int iwlagn_send_tx_power(struct iwl_priv *priv)
  234. {
  235. struct iwl5000_tx_power_dbm_cmd tx_power_cmd;
  236. u8 tx_ant_cfg_cmd;
  237. /* half dBm need to multiply */
  238. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  239. if (priv->tx_power_lmt_in_half_dbm &&
  240. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  241. /*
  242. * For the newer devices which using enhanced/extend tx power
  243. * table in EEPROM, the format is in half dBm. driver need to
  244. * convert to dBm format before report to mac80211.
  245. * By doing so, there is a possibility of 1/2 dBm resolution
  246. * lost. driver will perform "round-up" operation before
  247. * reporting, but it will cause 1/2 dBm tx power over the
  248. * regulatory limit. Perform the checking here, if the
  249. * "tx_power_user_lmt" is higher than EEPROM value (in
  250. * half-dBm format), lower the tx power based on EEPROM
  251. */
  252. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  253. }
  254. tx_power_cmd.flags = IWL50_TX_POWER_NO_CLOSED;
  255. tx_power_cmd.srv_chan_lmt = IWL50_TX_POWER_AUTO;
  256. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  257. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  258. else
  259. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  260. return iwl_send_cmd_pdu_async(priv, tx_ant_cfg_cmd,
  261. sizeof(tx_power_cmd), &tx_power_cmd,
  262. NULL);
  263. }
  264. void iwlagn_temperature(struct iwl_priv *priv)
  265. {
  266. /* store temperature from statistics (in Celsius) */
  267. priv->temperature = le32_to_cpu(priv->statistics.general.temperature);
  268. iwl_tt_handler(priv);
  269. }
  270. u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
  271. {
  272. struct iwl_eeprom_calib_hdr {
  273. u8 version;
  274. u8 pa_type;
  275. u16 voltage;
  276. } *hdr;
  277. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
  278. EEPROM_5000_CALIB_ALL);
  279. return hdr->version;
  280. }
  281. /*
  282. * EEPROM
  283. */
  284. static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
  285. {
  286. u16 offset = 0;
  287. if ((address & INDIRECT_ADDRESS) == 0)
  288. return address;
  289. switch (address & INDIRECT_TYPE_MSK) {
  290. case INDIRECT_HOST:
  291. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_HOST);
  292. break;
  293. case INDIRECT_GENERAL:
  294. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_GENERAL);
  295. break;
  296. case INDIRECT_REGULATORY:
  297. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_REGULATORY);
  298. break;
  299. case INDIRECT_CALIBRATION:
  300. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_CALIBRATION);
  301. break;
  302. case INDIRECT_PROCESS_ADJST:
  303. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_PROCESS_ADJST);
  304. break;
  305. case INDIRECT_OTHERS:
  306. offset = iwl_eeprom_query16(priv, EEPROM_5000_LINK_OTHERS);
  307. break;
  308. default:
  309. IWL_ERR(priv, "illegal indirect type: 0x%X\n",
  310. address & INDIRECT_TYPE_MSK);
  311. break;
  312. }
  313. /* translate the offset from words to byte */
  314. return (address & ADDRESS_MSK) + (offset << 1);
  315. }
  316. const u8 *iwlagn_eeprom_query_addr(const struct iwl_priv *priv,
  317. size_t offset)
  318. {
  319. u32 address = eeprom_indirect_address(priv, offset);
  320. BUG_ON(address >= priv->cfg->eeprom_size);
  321. return &priv->eeprom[address];
  322. }
  323. struct iwl_mod_params iwlagn_mod_params = {
  324. .amsdu_size_8K = 1,
  325. .restart_fw = 1,
  326. /* the rest are 0 by default */
  327. };
  328. void iwlagn_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  329. {
  330. unsigned long flags;
  331. int i;
  332. spin_lock_irqsave(&rxq->lock, flags);
  333. INIT_LIST_HEAD(&rxq->rx_free);
  334. INIT_LIST_HEAD(&rxq->rx_used);
  335. /* Fill the rx_used queue with _all_ of the Rx buffers */
  336. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  337. /* In the reset function, these buffers may have been allocated
  338. * to an SKB, so we need to unmap and free potential storage */
  339. if (rxq->pool[i].page != NULL) {
  340. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  341. PAGE_SIZE << priv->hw_params.rx_page_order,
  342. PCI_DMA_FROMDEVICE);
  343. __iwl_free_pages(priv, rxq->pool[i].page);
  344. rxq->pool[i].page = NULL;
  345. }
  346. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  347. }
  348. /* Set us so that we have processed and used all buffers, but have
  349. * not restocked the Rx queue with fresh buffers */
  350. rxq->read = rxq->write = 0;
  351. rxq->write_actual = 0;
  352. rxq->free_count = 0;
  353. spin_unlock_irqrestore(&rxq->lock, flags);
  354. }
  355. int iwlagn_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  356. {
  357. u32 rb_size;
  358. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  359. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  360. if (!priv->cfg->use_isr_legacy)
  361. rb_timeout = RX_RB_TIMEOUT;
  362. if (priv->cfg->mod_params->amsdu_size_8K)
  363. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  364. else
  365. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  366. /* Stop Rx DMA */
  367. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  368. /* Reset driver's Rx queue write index */
  369. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  370. /* Tell device where to find RBD circular buffer in DRAM */
  371. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  372. (u32)(rxq->dma_addr >> 8));
  373. /* Tell device where in DRAM to update its Rx status */
  374. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  375. rxq->rb_stts_dma >> 4);
  376. /* Enable Rx DMA
  377. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  378. * the credit mechanism in 5000 HW RX FIFO
  379. * Direct rx interrupts to hosts
  380. * Rx buffer size 4 or 8k
  381. * RB timeout 0x10
  382. * 256 RBDs
  383. */
  384. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  385. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  386. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  387. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  388. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  389. rb_size|
  390. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  391. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  392. /* Set interrupt coalescing timer to default (2048 usecs) */
  393. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  394. return 0;
  395. }
  396. int iwlagn_hw_nic_init(struct iwl_priv *priv)
  397. {
  398. unsigned long flags;
  399. struct iwl_rx_queue *rxq = &priv->rxq;
  400. int ret;
  401. /* nic_init */
  402. spin_lock_irqsave(&priv->lock, flags);
  403. priv->cfg->ops->lib->apm_ops.init(priv);
  404. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  405. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  406. spin_unlock_irqrestore(&priv->lock, flags);
  407. ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
  408. priv->cfg->ops->lib->apm_ops.config(priv);
  409. /* Allocate the RX queue, or reset if it is already allocated */
  410. if (!rxq->bd) {
  411. ret = iwl_rx_queue_alloc(priv);
  412. if (ret) {
  413. IWL_ERR(priv, "Unable to initialize Rx queue\n");
  414. return -ENOMEM;
  415. }
  416. } else
  417. iwlagn_rx_queue_reset(priv, rxq);
  418. iwlagn_rx_replenish(priv);
  419. iwlagn_rx_init(priv, rxq);
  420. spin_lock_irqsave(&priv->lock, flags);
  421. rxq->need_update = 1;
  422. iwl_rx_queue_update_write_ptr(priv, rxq);
  423. spin_unlock_irqrestore(&priv->lock, flags);
  424. /* Allocate and init all Tx and Command queues */
  425. ret = iwlagn_txq_ctx_reset(priv);
  426. if (ret)
  427. return ret;
  428. set_bit(STATUS_INIT, &priv->status);
  429. return 0;
  430. }
  431. /**
  432. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  433. */
  434. static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
  435. dma_addr_t dma_addr)
  436. {
  437. return cpu_to_le32((u32)(dma_addr >> 8));
  438. }
  439. /**
  440. * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
  441. *
  442. * If there are slots in the RX queue that need to be restocked,
  443. * and we have free pre-allocated buffers, fill the ranks as much
  444. * as we can, pulling from rx_free.
  445. *
  446. * This moves the 'write' index forward to catch up with 'processed', and
  447. * also updates the memory address in the firmware to reference the new
  448. * target buffer.
  449. */
  450. void iwlagn_rx_queue_restock(struct iwl_priv *priv)
  451. {
  452. struct iwl_rx_queue *rxq = &priv->rxq;
  453. struct list_head *element;
  454. struct iwl_rx_mem_buffer *rxb;
  455. unsigned long flags;
  456. int write;
  457. spin_lock_irqsave(&rxq->lock, flags);
  458. write = rxq->write & ~0x7;
  459. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  460. /* Get next free Rx buffer, remove from free list */
  461. element = rxq->rx_free.next;
  462. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  463. list_del(element);
  464. /* Point to Rx buffer via next RBD in circular buffer */
  465. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
  466. rxb->page_dma);
  467. rxq->queue[rxq->write] = rxb;
  468. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  469. rxq->free_count--;
  470. }
  471. spin_unlock_irqrestore(&rxq->lock, flags);
  472. /* If the pre-allocated buffer pool is dropping low, schedule to
  473. * refill it */
  474. if (rxq->free_count <= RX_LOW_WATERMARK)
  475. queue_work(priv->workqueue, &priv->rx_replenish);
  476. /* If we've added more space for the firmware to place data, tell it.
  477. * Increment device's write pointer in multiples of 8. */
  478. if (rxq->write_actual != (rxq->write & ~0x7)) {
  479. spin_lock_irqsave(&rxq->lock, flags);
  480. rxq->need_update = 1;
  481. spin_unlock_irqrestore(&rxq->lock, flags);
  482. iwl_rx_queue_update_write_ptr(priv, rxq);
  483. }
  484. }
  485. /**
  486. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  487. *
  488. * When moving to rx_free an SKB is allocated for the slot.
  489. *
  490. * Also restock the Rx queue via iwl_rx_queue_restock.
  491. * This is called as a scheduled work item (except for during initialization)
  492. */
  493. void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  494. {
  495. struct iwl_rx_queue *rxq = &priv->rxq;
  496. struct list_head *element;
  497. struct iwl_rx_mem_buffer *rxb;
  498. struct page *page;
  499. unsigned long flags;
  500. gfp_t gfp_mask = priority;
  501. while (1) {
  502. spin_lock_irqsave(&rxq->lock, flags);
  503. if (list_empty(&rxq->rx_used)) {
  504. spin_unlock_irqrestore(&rxq->lock, flags);
  505. return;
  506. }
  507. spin_unlock_irqrestore(&rxq->lock, flags);
  508. if (rxq->free_count > RX_LOW_WATERMARK)
  509. gfp_mask |= __GFP_NOWARN;
  510. if (priv->hw_params.rx_page_order > 0)
  511. gfp_mask |= __GFP_COMP;
  512. /* Alloc a new receive buffer */
  513. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  514. if (!page) {
  515. if (net_ratelimit())
  516. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  517. "order: %d\n",
  518. priv->hw_params.rx_page_order);
  519. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  520. net_ratelimit())
  521. IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
  522. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  523. rxq->free_count);
  524. /* We don't reschedule replenish work here -- we will
  525. * call the restock method and if it still needs
  526. * more buffers it will schedule replenish */
  527. return;
  528. }
  529. spin_lock_irqsave(&rxq->lock, flags);
  530. if (list_empty(&rxq->rx_used)) {
  531. spin_unlock_irqrestore(&rxq->lock, flags);
  532. __free_pages(page, priv->hw_params.rx_page_order);
  533. return;
  534. }
  535. element = rxq->rx_used.next;
  536. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  537. list_del(element);
  538. spin_unlock_irqrestore(&rxq->lock, flags);
  539. rxb->page = page;
  540. /* Get physical address of the RB */
  541. rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
  542. PAGE_SIZE << priv->hw_params.rx_page_order,
  543. PCI_DMA_FROMDEVICE);
  544. /* dma address must be no more than 36 bits */
  545. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  546. /* and also 256 byte aligned! */
  547. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  548. spin_lock_irqsave(&rxq->lock, flags);
  549. list_add_tail(&rxb->list, &rxq->rx_free);
  550. rxq->free_count++;
  551. priv->alloc_rxb_page++;
  552. spin_unlock_irqrestore(&rxq->lock, flags);
  553. }
  554. }
  555. void iwlagn_rx_replenish(struct iwl_priv *priv)
  556. {
  557. unsigned long flags;
  558. iwlagn_rx_allocate(priv, GFP_KERNEL);
  559. spin_lock_irqsave(&priv->lock, flags);
  560. iwlagn_rx_queue_restock(priv);
  561. spin_unlock_irqrestore(&priv->lock, flags);
  562. }
  563. void iwlagn_rx_replenish_now(struct iwl_priv *priv)
  564. {
  565. iwlagn_rx_allocate(priv, GFP_ATOMIC);
  566. iwlagn_rx_queue_restock(priv);
  567. }
  568. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  569. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  570. * This free routine walks the list of POOL entries and if SKB is set to
  571. * non NULL it is unmapped and freed
  572. */
  573. void iwlagn_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  574. {
  575. int i;
  576. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  577. if (rxq->pool[i].page != NULL) {
  578. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  579. PAGE_SIZE << priv->hw_params.rx_page_order,
  580. PCI_DMA_FROMDEVICE);
  581. __iwl_free_pages(priv, rxq->pool[i].page);
  582. rxq->pool[i].page = NULL;
  583. }
  584. }
  585. dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  586. rxq->dma_addr);
  587. dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
  588. rxq->rb_stts, rxq->rb_stts_dma);
  589. rxq->bd = NULL;
  590. rxq->rb_stts = NULL;
  591. }
  592. int iwlagn_rxq_stop(struct iwl_priv *priv)
  593. {
  594. /* stop Rx DMA */
  595. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  596. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  597. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  598. return 0;
  599. }