/drivers/net/wireless/iwlegacy/iwl3945-base.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C · 4017 lines · 2705 code · 702 blank · 610 comment · 348 complexity · 946a0e2e55dd8e77993ba52ea93cfda9 MD5 · raw file

Large files are truncated click here to view the full file

  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/pci.h>
  34. #include <linux/pci-aspm.h>
  35. #include <linux/slab.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/delay.h>
  38. #include <linux/sched.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/wireless.h>
  42. #include <linux/firmware.h>
  43. #include <linux/etherdevice.h>
  44. #include <linux/if_arp.h>
  45. #include <net/ieee80211_radiotap.h>
  46. #include <net/mac80211.h>
  47. #include <asm/div64.h>
  48. #define DRV_NAME "iwl3945"
  49. #include "iwl-fh.h"
  50. #include "iwl-3945-fh.h"
  51. #include "iwl-commands.h"
  52. #include "iwl-sta.h"
  53. #include "iwl-3945.h"
  54. #include "iwl-core.h"
  55. #include "iwl-helpers.h"
  56. #include "iwl-dev.h"
  57. #include "iwl-spectrum.h"
  58. /*
  59. * module name, copyright, version, etc.
  60. */
  61. #define DRV_DESCRIPTION \
  62. "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
  63. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  64. #define VD "d"
  65. #else
  66. #define VD
  67. #endif
  68. /*
  69. * add "s" to indicate spectrum measurement included.
  70. * we add it here to be consistent with previous releases in which
  71. * this was configurable.
  72. */
  73. #define DRV_VERSION IWLWIFI_VERSION VD "s"
  74. #define DRV_COPYRIGHT "Copyright(c) 2003-2011 Intel Corporation"
  75. #define DRV_AUTHOR "<ilw@linux.intel.com>"
  76. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  77. MODULE_VERSION(DRV_VERSION);
  78. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  79. MODULE_LICENSE("GPL");
  80. /* module parameters */
  81. struct iwl_mod_params iwl3945_mod_params = {
  82. .sw_crypto = 1,
  83. .restart_fw = 1,
  84. .disable_hw_scan = 1,
  85. /* the rest are 0 by default */
  86. };
  87. /**
  88. * iwl3945_get_antenna_flags - Get antenna flags for RXON command
  89. * @priv: eeprom and antenna fields are used to determine antenna flags
  90. *
  91. * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
  92. * iwl3945_mod_params.antenna specifies the antenna diversity mode:
  93. *
  94. * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
  95. * IWL_ANTENNA_MAIN - Force MAIN antenna
  96. * IWL_ANTENNA_AUX - Force AUX antenna
  97. */
  98. __le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
  99. {
  100. struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  101. switch (iwl3945_mod_params.antenna) {
  102. case IWL_ANTENNA_DIVERSITY:
  103. return 0;
  104. case IWL_ANTENNA_MAIN:
  105. if (eeprom->antenna_switch_type)
  106. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  107. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  108. case IWL_ANTENNA_AUX:
  109. if (eeprom->antenna_switch_type)
  110. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  111. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  112. }
  113. /* bad antenna selector value */
  114. IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
  115. iwl3945_mod_params.antenna);
  116. return 0; /* "diversity" is default if error */
  117. }
  118. static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  119. struct ieee80211_key_conf *keyconf,
  120. u8 sta_id)
  121. {
  122. unsigned long flags;
  123. __le16 key_flags = 0;
  124. int ret;
  125. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  126. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  127. if (sta_id == priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id)
  128. key_flags |= STA_KEY_MULTICAST_MSK;
  129. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  130. keyconf->hw_key_idx = keyconf->keyidx;
  131. key_flags &= ~STA_KEY_FLG_INVALID;
  132. spin_lock_irqsave(&priv->sta_lock, flags);
  133. priv->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  134. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  135. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  136. keyconf->keylen);
  137. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  138. keyconf->keylen);
  139. if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  140. == STA_KEY_FLG_NO_ENC)
  141. priv->stations[sta_id].sta.key.key_offset =
  142. iwl_legacy_get_free_ucode_key_index(priv);
  143. /* else, we are overriding an existing key => no need to allocated room
  144. * in uCode. */
  145. WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  146. "no space for a new key");
  147. priv->stations[sta_id].sta.key.key_flags = key_flags;
  148. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  149. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  150. IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
  151. ret = iwl_legacy_send_add_sta(priv,
  152. &priv->stations[sta_id].sta, CMD_ASYNC);
  153. spin_unlock_irqrestore(&priv->sta_lock, flags);
  154. return ret;
  155. }
  156. static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  157. struct ieee80211_key_conf *keyconf,
  158. u8 sta_id)
  159. {
  160. return -EOPNOTSUPP;
  161. }
  162. static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
  163. struct ieee80211_key_conf *keyconf,
  164. u8 sta_id)
  165. {
  166. return -EOPNOTSUPP;
  167. }
  168. static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
  169. {
  170. unsigned long flags;
  171. struct iwl_legacy_addsta_cmd sta_cmd;
  172. spin_lock_irqsave(&priv->sta_lock, flags);
  173. memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
  174. memset(&priv->stations[sta_id].sta.key, 0,
  175. sizeof(struct iwl4965_keyinfo));
  176. priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
  177. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  178. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  179. memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_legacy_addsta_cmd));
  180. spin_unlock_irqrestore(&priv->sta_lock, flags);
  181. IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
  182. return iwl_legacy_send_add_sta(priv, &sta_cmd, CMD_SYNC);
  183. }
  184. static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
  185. struct ieee80211_key_conf *keyconf, u8 sta_id)
  186. {
  187. int ret = 0;
  188. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  189. switch (keyconf->cipher) {
  190. case WLAN_CIPHER_SUITE_CCMP:
  191. ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  192. break;
  193. case WLAN_CIPHER_SUITE_TKIP:
  194. ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  195. break;
  196. case WLAN_CIPHER_SUITE_WEP40:
  197. case WLAN_CIPHER_SUITE_WEP104:
  198. ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  199. break;
  200. default:
  201. IWL_ERR(priv, "Unknown alg: %s alg=%x\n", __func__,
  202. keyconf->cipher);
  203. ret = -EINVAL;
  204. }
  205. IWL_DEBUG_WEP(priv, "Set dynamic key: alg=%x len=%d idx=%d sta=%d ret=%d\n",
  206. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  207. sta_id, ret);
  208. return ret;
  209. }
  210. static int iwl3945_remove_static_key(struct iwl_priv *priv)
  211. {
  212. int ret = -EOPNOTSUPP;
  213. return ret;
  214. }
  215. static int iwl3945_set_static_key(struct iwl_priv *priv,
  216. struct ieee80211_key_conf *key)
  217. {
  218. if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  219. key->cipher == WLAN_CIPHER_SUITE_WEP104)
  220. return -EOPNOTSUPP;
  221. IWL_ERR(priv, "Static key invalid: cipher %x\n", key->cipher);
  222. return -EINVAL;
  223. }
  224. static void iwl3945_clear_free_frames(struct iwl_priv *priv)
  225. {
  226. struct list_head *element;
  227. IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
  228. priv->frames_count);
  229. while (!list_empty(&priv->free_frames)) {
  230. element = priv->free_frames.next;
  231. list_del(element);
  232. kfree(list_entry(element, struct iwl3945_frame, list));
  233. priv->frames_count--;
  234. }
  235. if (priv->frames_count) {
  236. IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
  237. priv->frames_count);
  238. priv->frames_count = 0;
  239. }
  240. }
  241. static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
  242. {
  243. struct iwl3945_frame *frame;
  244. struct list_head *element;
  245. if (list_empty(&priv->free_frames)) {
  246. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  247. if (!frame) {
  248. IWL_ERR(priv, "Could not allocate frame!\n");
  249. return NULL;
  250. }
  251. priv->frames_count++;
  252. return frame;
  253. }
  254. element = priv->free_frames.next;
  255. list_del(element);
  256. return list_entry(element, struct iwl3945_frame, list);
  257. }
  258. static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
  259. {
  260. memset(frame, 0, sizeof(*frame));
  261. list_add(&frame->list, &priv->free_frames);
  262. }
  263. unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
  264. struct ieee80211_hdr *hdr,
  265. int left)
  266. {
  267. if (!iwl_legacy_is_associated(priv, IWL_RXON_CTX_BSS) || !priv->beacon_skb)
  268. return 0;
  269. if (priv->beacon_skb->len > left)
  270. return 0;
  271. memcpy(hdr, priv->beacon_skb->data, priv->beacon_skb->len);
  272. return priv->beacon_skb->len;
  273. }
  274. static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
  275. {
  276. struct iwl3945_frame *frame;
  277. unsigned int frame_size;
  278. int rc;
  279. u8 rate;
  280. frame = iwl3945_get_free_frame(priv);
  281. if (!frame) {
  282. IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
  283. "command.\n");
  284. return -ENOMEM;
  285. }
  286. rate = iwl_legacy_get_lowest_plcp(priv,
  287. &priv->contexts[IWL_RXON_CTX_BSS]);
  288. frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
  289. rc = iwl_legacy_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
  290. &frame->u.cmd[0]);
  291. iwl3945_free_frame(priv, frame);
  292. return rc;
  293. }
  294. static void iwl3945_unset_hw_params(struct iwl_priv *priv)
  295. {
  296. if (priv->_3945.shared_virt)
  297. dma_free_coherent(&priv->pci_dev->dev,
  298. sizeof(struct iwl3945_shared),
  299. priv->_3945.shared_virt,
  300. priv->_3945.shared_phys);
  301. }
  302. static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
  303. struct ieee80211_tx_info *info,
  304. struct iwl_device_cmd *cmd,
  305. struct sk_buff *skb_frag,
  306. int sta_id)
  307. {
  308. struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  309. struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
  310. tx_cmd->sec_ctl = 0;
  311. switch (keyinfo->cipher) {
  312. case WLAN_CIPHER_SUITE_CCMP:
  313. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  314. memcpy(tx_cmd->key, keyinfo->key, keyinfo->keylen);
  315. IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
  316. break;
  317. case WLAN_CIPHER_SUITE_TKIP:
  318. break;
  319. case WLAN_CIPHER_SUITE_WEP104:
  320. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  321. /* fall through */
  322. case WLAN_CIPHER_SUITE_WEP40:
  323. tx_cmd->sec_ctl |= TX_CMD_SEC_WEP |
  324. (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
  325. memcpy(&tx_cmd->key[3], keyinfo->key, keyinfo->keylen);
  326. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  327. "with key %d\n", info->control.hw_key->hw_key_idx);
  328. break;
  329. default:
  330. IWL_ERR(priv, "Unknown encode cipher %x\n", keyinfo->cipher);
  331. break;
  332. }
  333. }
  334. /*
  335. * handle build REPLY_TX command notification.
  336. */
  337. static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
  338. struct iwl_device_cmd *cmd,
  339. struct ieee80211_tx_info *info,
  340. struct ieee80211_hdr *hdr, u8 std_id)
  341. {
  342. struct iwl3945_tx_cmd *tx_cmd = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  343. __le32 tx_flags = tx_cmd->tx_flags;
  344. __le16 fc = hdr->frame_control;
  345. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  346. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  347. tx_flags |= TX_CMD_FLG_ACK_MSK;
  348. if (ieee80211_is_mgmt(fc))
  349. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  350. if (ieee80211_is_probe_resp(fc) &&
  351. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  352. tx_flags |= TX_CMD_FLG_TSF_MSK;
  353. } else {
  354. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  355. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  356. }
  357. tx_cmd->sta_id = std_id;
  358. if (ieee80211_has_morefrags(fc))
  359. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  360. if (ieee80211_is_data_qos(fc)) {
  361. u8 *qc = ieee80211_get_qos_ctl(hdr);
  362. tx_cmd->tid_tspec = qc[0] & 0xf;
  363. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  364. } else {
  365. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  366. }
  367. iwl_legacy_tx_cmd_protection(priv, info, fc, &tx_flags);
  368. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  369. if (ieee80211_is_mgmt(fc)) {
  370. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  371. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  372. else
  373. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  374. } else {
  375. tx_cmd->timeout.pm_frame_timeout = 0;
  376. }
  377. tx_cmd->driver_txop = 0;
  378. tx_cmd->tx_flags = tx_flags;
  379. tx_cmd->next_frame_len = 0;
  380. }
  381. /*
  382. * start REPLY_TX command process
  383. */
  384. static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  385. {
  386. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  387. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  388. struct iwl3945_tx_cmd *tx_cmd;
  389. struct iwl_tx_queue *txq = NULL;
  390. struct iwl_queue *q = NULL;
  391. struct iwl_device_cmd *out_cmd;
  392. struct iwl_cmd_meta *out_meta;
  393. dma_addr_t phys_addr;
  394. dma_addr_t txcmd_phys;
  395. int txq_id = skb_get_queue_mapping(skb);
  396. u16 len, idx, hdr_len;
  397. u8 id;
  398. u8 unicast;
  399. u8 sta_id;
  400. u8 tid = 0;
  401. __le16 fc;
  402. u8 wait_write_ptr = 0;
  403. unsigned long flags;
  404. spin_lock_irqsave(&priv->lock, flags);
  405. if (iwl_legacy_is_rfkill(priv)) {
  406. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  407. goto drop_unlock;
  408. }
  409. if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
  410. IWL_ERR(priv, "ERROR: No TX rate available.\n");
  411. goto drop_unlock;
  412. }
  413. unicast = !is_multicast_ether_addr(hdr->addr1);
  414. id = 0;
  415. fc = hdr->frame_control;
  416. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  417. if (ieee80211_is_auth(fc))
  418. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  419. else if (ieee80211_is_assoc_req(fc))
  420. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  421. else if (ieee80211_is_reassoc_req(fc))
  422. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  423. #endif
  424. spin_unlock_irqrestore(&priv->lock, flags);
  425. hdr_len = ieee80211_hdrlen(fc);
  426. /* Find index into station table for destination station */
  427. sta_id = iwl_legacy_sta_id_or_broadcast(
  428. priv, &priv->contexts[IWL_RXON_CTX_BSS],
  429. info->control.sta);
  430. if (sta_id == IWL_INVALID_STATION) {
  431. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  432. hdr->addr1);
  433. goto drop;
  434. }
  435. IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
  436. if (ieee80211_is_data_qos(fc)) {
  437. u8 *qc = ieee80211_get_qos_ctl(hdr);
  438. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  439. if (unlikely(tid >= MAX_TID_COUNT))
  440. goto drop;
  441. }
  442. /* Descriptor for chosen Tx queue */
  443. txq = &priv->txq[txq_id];
  444. q = &txq->q;
  445. if ((iwl_legacy_queue_space(q) < q->high_mark))
  446. goto drop;
  447. spin_lock_irqsave(&priv->lock, flags);
  448. idx = iwl_legacy_get_cmd_index(q, q->write_ptr, 0);
  449. /* Set up driver data for this TFD */
  450. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  451. txq->txb[q->write_ptr].skb = skb;
  452. txq->txb[q->write_ptr].ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  453. /* Init first empty entry in queue's array of Tx/cmd buffers */
  454. out_cmd = txq->cmd[idx];
  455. out_meta = &txq->meta[idx];
  456. tx_cmd = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
  457. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  458. memset(tx_cmd, 0, sizeof(*tx_cmd));
  459. /*
  460. * Set up the Tx-command (not MAC!) header.
  461. * Store the chosen Tx queue and TFD index within the sequence field;
  462. * after Tx, uCode's Tx response will return this value so driver can
  463. * locate the frame within the tx queue and do post-tx processing.
  464. */
  465. out_cmd->hdr.cmd = REPLY_TX;
  466. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  467. INDEX_TO_SEQ(q->write_ptr)));
  468. /* Copy MAC header from skb into command buffer */
  469. memcpy(tx_cmd->hdr, hdr, hdr_len);
  470. if (info->control.hw_key)
  471. iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
  472. /* TODO need this for burst mode later on */
  473. iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
  474. /* set is_hcca to 0; it probably will never be implemented */
  475. iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
  476. /* Total # bytes to be transmitted */
  477. len = (u16)skb->len;
  478. tx_cmd->len = cpu_to_le16(len);
  479. iwl_legacy_dbg_log_tx_data_frame(priv, len, hdr);
  480. iwl_legacy_update_stats(priv, true, fc, len);
  481. tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
  482. tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
  483. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  484. txq->need_update = 1;
  485. } else {
  486. wait_write_ptr = 1;
  487. txq->need_update = 0;
  488. }
  489. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  490. le16_to_cpu(out_cmd->hdr.sequence));
  491. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  492. iwl_print_hex_dump(priv, IWL_DL_TX, tx_cmd, sizeof(*tx_cmd));
  493. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr,
  494. ieee80211_hdrlen(fc));
  495. /*
  496. * Use the first empty entry in this queue's command buffer array
  497. * to contain the Tx command and MAC header concatenated together
  498. * (payload data will be in another buffer).
  499. * Size of this varies, due to varying MAC header length.
  500. * If end is not dword aligned, we'll have 2 extra bytes at the end
  501. * of the MAC header (device reads on dword boundaries).
  502. * We'll tell device about this padding later.
  503. */
  504. len = sizeof(struct iwl3945_tx_cmd) +
  505. sizeof(struct iwl_cmd_header) + hdr_len;
  506. len = (len + 3) & ~3;
  507. /* Physical address of this Tx command's header (not MAC header!),
  508. * within command buffer array. */
  509. txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
  510. len, PCI_DMA_TODEVICE);
  511. /* we do not map meta data ... so we can safely access address to
  512. * provide to unmap command*/
  513. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  514. dma_unmap_len_set(out_meta, len, len);
  515. /* Add buffer containing Tx command and MAC(!) header to TFD's
  516. * first entry */
  517. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  518. txcmd_phys, len, 1, 0);
  519. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  520. * if any (802.11 null frames have no payload). */
  521. len = skb->len - hdr_len;
  522. if (len) {
  523. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  524. len, PCI_DMA_TODEVICE);
  525. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  526. phys_addr, len,
  527. 0, U32_PAD(len));
  528. }
  529. /* Tell device the write index *just past* this latest filled TFD */
  530. q->write_ptr = iwl_legacy_queue_inc_wrap(q->write_ptr, q->n_bd);
  531. iwl_legacy_txq_update_write_ptr(priv, txq);
  532. spin_unlock_irqrestore(&priv->lock, flags);
  533. if ((iwl_legacy_queue_space(q) < q->high_mark)
  534. && priv->mac80211_registered) {
  535. if (wait_write_ptr) {
  536. spin_lock_irqsave(&priv->lock, flags);
  537. txq->need_update = 1;
  538. iwl_legacy_txq_update_write_ptr(priv, txq);
  539. spin_unlock_irqrestore(&priv->lock, flags);
  540. }
  541. iwl_legacy_stop_queue(priv, txq);
  542. }
  543. return 0;
  544. drop_unlock:
  545. spin_unlock_irqrestore(&priv->lock, flags);
  546. drop:
  547. return -1;
  548. }
  549. static int iwl3945_get_measurement(struct iwl_priv *priv,
  550. struct ieee80211_measurement_params *params,
  551. u8 type)
  552. {
  553. struct iwl_spectrum_cmd spectrum;
  554. struct iwl_rx_packet *pkt;
  555. struct iwl_host_cmd cmd = {
  556. .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
  557. .data = (void *)&spectrum,
  558. .flags = CMD_WANT_SKB,
  559. };
  560. u32 add_time = le64_to_cpu(params->start_time);
  561. int rc;
  562. int spectrum_resp_status;
  563. int duration = le16_to_cpu(params->duration);
  564. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  565. if (iwl_legacy_is_associated(priv, IWL_RXON_CTX_BSS))
  566. add_time = iwl_legacy_usecs_to_beacons(priv,
  567. le64_to_cpu(params->start_time) - priv->_3945.last_tsf,
  568. le16_to_cpu(ctx->timing.beacon_interval));
  569. memset(&spectrum, 0, sizeof(spectrum));
  570. spectrum.channel_count = cpu_to_le16(1);
  571. spectrum.flags =
  572. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  573. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  574. cmd.len = sizeof(spectrum);
  575. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  576. if (iwl_legacy_is_associated(priv, IWL_RXON_CTX_BSS))
  577. spectrum.start_time =
  578. iwl_legacy_add_beacon_time(priv,
  579. priv->_3945.last_beacon_time, add_time,
  580. le16_to_cpu(ctx->timing.beacon_interval));
  581. else
  582. spectrum.start_time = 0;
  583. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  584. spectrum.channels[0].channel = params->channel;
  585. spectrum.channels[0].type = type;
  586. if (ctx->active.flags & RXON_FLG_BAND_24G_MSK)
  587. spectrum.flags |= RXON_FLG_BAND_24G_MSK |
  588. RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
  589. rc = iwl_legacy_send_cmd_sync(priv, &cmd);
  590. if (rc)
  591. return rc;
  592. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  593. if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
  594. IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
  595. rc = -EIO;
  596. }
  597. spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
  598. switch (spectrum_resp_status) {
  599. case 0: /* Command will be handled */
  600. if (pkt->u.spectrum.id != 0xff) {
  601. IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
  602. pkt->u.spectrum.id);
  603. priv->measurement_status &= ~MEASUREMENT_READY;
  604. }
  605. priv->measurement_status |= MEASUREMENT_ACTIVE;
  606. rc = 0;
  607. break;
  608. case 1: /* Command will not be handled */
  609. rc = -EAGAIN;
  610. break;
  611. }
  612. iwl_legacy_free_pages(priv, cmd.reply_page);
  613. return rc;
  614. }
  615. static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
  616. struct iwl_rx_mem_buffer *rxb)
  617. {
  618. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  619. struct iwl_alive_resp *palive;
  620. struct delayed_work *pwork;
  621. palive = &pkt->u.alive_frame;
  622. IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
  623. "0x%01X 0x%01X\n",
  624. palive->is_valid, palive->ver_type,
  625. palive->ver_subtype);
  626. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  627. IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
  628. memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
  629. sizeof(struct iwl_alive_resp));
  630. pwork = &priv->init_alive_start;
  631. } else {
  632. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  633. memcpy(&priv->card_alive, &pkt->u.alive_frame,
  634. sizeof(struct iwl_alive_resp));
  635. pwork = &priv->alive_start;
  636. iwl3945_disable_events(priv);
  637. }
  638. /* We delay the ALIVE response by 5ms to
  639. * give the HW RF Kill time to activate... */
  640. if (palive->is_valid == UCODE_VALID_OK)
  641. queue_delayed_work(priv->workqueue, pwork,
  642. msecs_to_jiffies(5));
  643. else
  644. IWL_WARN(priv, "uCode did not respond OK.\n");
  645. }
  646. static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
  647. struct iwl_rx_mem_buffer *rxb)
  648. {
  649. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  650. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  651. #endif
  652. IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
  653. }
  654. static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
  655. struct iwl_rx_mem_buffer *rxb)
  656. {
  657. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  658. struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
  659. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  660. u8 rate = beacon->beacon_notify_hdr.rate;
  661. IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
  662. "tsf %d %d rate %d\n",
  663. le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
  664. beacon->beacon_notify_hdr.failure_frame,
  665. le32_to_cpu(beacon->ibss_mgr_status),
  666. le32_to_cpu(beacon->high_tsf),
  667. le32_to_cpu(beacon->low_tsf), rate);
  668. #endif
  669. priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
  670. }
  671. /* Handle notification from uCode that card's power state is changing
  672. * due to software, hardware, or critical temperature RFKILL */
  673. static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
  674. struct iwl_rx_mem_buffer *rxb)
  675. {
  676. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  677. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  678. unsigned long status = priv->status;
  679. IWL_WARN(priv, "Card state received: HW:%s SW:%s\n",
  680. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  681. (flags & SW_CARD_DISABLED) ? "Kill" : "On");
  682. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  683. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  684. if (flags & HW_CARD_DISABLED)
  685. set_bit(STATUS_RF_KILL_HW, &priv->status);
  686. else
  687. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  688. iwl_legacy_scan_cancel(priv);
  689. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  690. test_bit(STATUS_RF_KILL_HW, &priv->status)))
  691. wiphy_rfkill_set_hw_state(priv->hw->wiphy,
  692. test_bit(STATUS_RF_KILL_HW, &priv->status));
  693. else
  694. wake_up(&priv->wait_command_queue);
  695. }
  696. /**
  697. * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
  698. *
  699. * Setup the RX handlers for each of the reply types sent from the uCode
  700. * to the host.
  701. *
  702. * This function chains into the hardware specific files for them to setup
  703. * any hardware specific handlers as well.
  704. */
  705. static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
  706. {
  707. priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
  708. priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
  709. priv->rx_handlers[REPLY_ERROR] = iwl_legacy_rx_reply_error;
  710. priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_legacy_rx_csa;
  711. priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  712. iwl_legacy_rx_spectrum_measure_notif;
  713. priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_legacy_rx_pm_sleep_notif;
  714. priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  715. iwl_legacy_rx_pm_debug_statistics_notif;
  716. priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
  717. /*
  718. * The same handler is used for both the REPLY to a discrete
  719. * statistics request from the host as well as for the periodic
  720. * statistics notifications (after received beacons) from the uCode.
  721. */
  722. priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_reply_statistics;
  723. priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
  724. iwl_legacy_setup_rx_scan_handlers(priv);
  725. priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
  726. /* Set up hardware specific Rx handlers */
  727. iwl3945_hw_rx_handler_setup(priv);
  728. }
  729. /************************** RX-FUNCTIONS ****************************/
  730. /*
  731. * Rx theory of operation
  732. *
  733. * The host allocates 32 DMA target addresses and passes the host address
  734. * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
  735. * 0 to 31
  736. *
  737. * Rx Queue Indexes
  738. * The host/firmware share two index registers for managing the Rx buffers.
  739. *
  740. * The READ index maps to the first position that the firmware may be writing
  741. * to -- the driver can read up to (but not including) this position and get
  742. * good data.
  743. * The READ index is managed by the firmware once the card is enabled.
  744. *
  745. * The WRITE index maps to the last position the driver has read from -- the
  746. * position preceding WRITE is the last slot the firmware can place a packet.
  747. *
  748. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  749. * WRITE = READ.
  750. *
  751. * During initialization, the host sets up the READ queue position to the first
  752. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  753. *
  754. * When the firmware places a packet in a buffer, it will advance the READ index
  755. * and fire the RX interrupt. The driver can then query the READ index and
  756. * process as many packets as possible, moving the WRITE index forward as it
  757. * resets the Rx queue buffers with new memory.
  758. *
  759. * The management in the driver is as follows:
  760. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  761. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  762. * to replenish the iwl->rxq->rx_free.
  763. * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
  764. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  765. * 'processed' and 'read' driver indexes as well)
  766. * + A received packet is processed and handed to the kernel network stack,
  767. * detached from the iwl->rxq. The driver 'processed' index is updated.
  768. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  769. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  770. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  771. * were enough free buffers and RX_STALLED is set it is cleared.
  772. *
  773. *
  774. * Driver sequence:
  775. *
  776. * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
  777. * iwl3945_rx_queue_restock
  778. * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
  779. * queue, updates firmware pointers, and updates
  780. * the WRITE index. If insufficient rx_free buffers
  781. * are available, schedules iwl3945_rx_replenish
  782. *
  783. * -- enable interrupts --
  784. * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
  785. * READ INDEX, detaching the SKB from the pool.
  786. * Moves the packet buffer from queue to rx_used.
  787. * Calls iwl3945_rx_queue_restock to refill any empty
  788. * slots.
  789. * ...
  790. *
  791. */
  792. /**
  793. * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  794. */
  795. static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
  796. dma_addr_t dma_addr)
  797. {
  798. return cpu_to_le32((u32)dma_addr);
  799. }
  800. /**
  801. * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
  802. *
  803. * If there are slots in the RX queue that need to be restocked,
  804. * and we have free pre-allocated buffers, fill the ranks as much
  805. * as we can, pulling from rx_free.
  806. *
  807. * This moves the 'write' index forward to catch up with 'processed', and
  808. * also updates the memory address in the firmware to reference the new
  809. * target buffer.
  810. */
  811. static void iwl3945_rx_queue_restock(struct iwl_priv *priv)
  812. {
  813. struct iwl_rx_queue *rxq = &priv->rxq;
  814. struct list_head *element;
  815. struct iwl_rx_mem_buffer *rxb;
  816. unsigned long flags;
  817. int write;
  818. spin_lock_irqsave(&rxq->lock, flags);
  819. write = rxq->write & ~0x7;
  820. while ((iwl_legacy_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  821. /* Get next free Rx buffer, remove from free list */
  822. element = rxq->rx_free.next;
  823. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  824. list_del(element);
  825. /* Point to Rx buffer via next RBD in circular buffer */
  826. rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->page_dma);
  827. rxq->queue[rxq->write] = rxb;
  828. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  829. rxq->free_count--;
  830. }
  831. spin_unlock_irqrestore(&rxq->lock, flags);
  832. /* If the pre-allocated buffer pool is dropping low, schedule to
  833. * refill it */
  834. if (rxq->free_count <= RX_LOW_WATERMARK)
  835. queue_work(priv->workqueue, &priv->rx_replenish);
  836. /* If we've added more space for the firmware to place data, tell it.
  837. * Increment device's write pointer in multiples of 8. */
  838. if ((rxq->write_actual != (rxq->write & ~0x7))
  839. || (abs(rxq->write - rxq->read) > 7)) {
  840. spin_lock_irqsave(&rxq->lock, flags);
  841. rxq->need_update = 1;
  842. spin_unlock_irqrestore(&rxq->lock, flags);
  843. iwl_legacy_rx_queue_update_write_ptr(priv, rxq);
  844. }
  845. }
  846. /**
  847. * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
  848. *
  849. * When moving to rx_free an SKB is allocated for the slot.
  850. *
  851. * Also restock the Rx queue via iwl3945_rx_queue_restock.
  852. * This is called as a scheduled work item (except for during initialization)
  853. */
  854. static void iwl3945_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  855. {
  856. struct iwl_rx_queue *rxq = &priv->rxq;
  857. struct list_head *element;
  858. struct iwl_rx_mem_buffer *rxb;
  859. struct page *page;
  860. unsigned long flags;
  861. gfp_t gfp_mask = priority;
  862. while (1) {
  863. spin_lock_irqsave(&rxq->lock, flags);
  864. if (list_empty(&rxq->rx_used)) {
  865. spin_unlock_irqrestore(&rxq->lock, flags);
  866. return;
  867. }
  868. spin_unlock_irqrestore(&rxq->lock, flags);
  869. if (rxq->free_count > RX_LOW_WATERMARK)
  870. gfp_mask |= __GFP_NOWARN;
  871. if (priv->hw_params.rx_page_order > 0)
  872. gfp_mask |= __GFP_COMP;
  873. /* Alloc a new receive buffer */
  874. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  875. if (!page) {
  876. if (net_ratelimit())
  877. IWL_DEBUG_INFO(priv, "Failed to allocate SKB buffer.\n");
  878. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  879. net_ratelimit())
  880. IWL_CRIT(priv, "Failed to allocate SKB buffer with %s. Only %u free buffers remaining.\n",
  881. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  882. rxq->free_count);
  883. /* We don't reschedule replenish work here -- we will
  884. * call the restock method and if it still needs
  885. * more buffers it will schedule replenish */
  886. break;
  887. }
  888. spin_lock_irqsave(&rxq->lock, flags);
  889. if (list_empty(&rxq->rx_used)) {
  890. spin_unlock_irqrestore(&rxq->lock, flags);
  891. __free_pages(page, priv->hw_params.rx_page_order);
  892. return;
  893. }
  894. element = rxq->rx_used.next;
  895. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  896. list_del(element);
  897. spin_unlock_irqrestore(&rxq->lock, flags);
  898. rxb->page = page;
  899. /* Get physical address of RB/SKB */
  900. rxb->page_dma = pci_map_page(priv->pci_dev, page, 0,
  901. PAGE_SIZE << priv->hw_params.rx_page_order,
  902. PCI_DMA_FROMDEVICE);
  903. spin_lock_irqsave(&rxq->lock, flags);
  904. list_add_tail(&rxb->list, &rxq->rx_free);
  905. rxq->free_count++;
  906. priv->alloc_rxb_page++;
  907. spin_unlock_irqrestore(&rxq->lock, flags);
  908. }
  909. }
  910. void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  911. {
  912. unsigned long flags;
  913. int i;
  914. spin_lock_irqsave(&rxq->lock, flags);
  915. INIT_LIST_HEAD(&rxq->rx_free);
  916. INIT_LIST_HEAD(&rxq->rx_used);
  917. /* Fill the rx_used queue with _all_ of the Rx buffers */
  918. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  919. /* In the reset function, these buffers may have been allocated
  920. * to an SKB, so we need to unmap and free potential storage */
  921. if (rxq->pool[i].page != NULL) {
  922. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  923. PAGE_SIZE << priv->hw_params.rx_page_order,
  924. PCI_DMA_FROMDEVICE);
  925. __iwl_legacy_free_pages(priv, rxq->pool[i].page);
  926. rxq->pool[i].page = NULL;
  927. }
  928. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  929. }
  930. /* Set us so that we have processed and used all buffers, but have
  931. * not restocked the Rx queue with fresh buffers */
  932. rxq->read = rxq->write = 0;
  933. rxq->write_actual = 0;
  934. rxq->free_count = 0;
  935. spin_unlock_irqrestore(&rxq->lock, flags);
  936. }
  937. void iwl3945_rx_replenish(void *data)
  938. {
  939. struct iwl_priv *priv = data;
  940. unsigned long flags;
  941. iwl3945_rx_allocate(priv, GFP_KERNEL);
  942. spin_lock_irqsave(&priv->lock, flags);
  943. iwl3945_rx_queue_restock(priv);
  944. spin_unlock_irqrestore(&priv->lock, flags);
  945. }
  946. static void iwl3945_rx_replenish_now(struct iwl_priv *priv)
  947. {
  948. iwl3945_rx_allocate(priv, GFP_ATOMIC);
  949. iwl3945_rx_queue_restock(priv);
  950. }
  951. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  952. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  953. * This free routine walks the list of POOL entries and if SKB is set to
  954. * non NULL it is unmapped and freed
  955. */
  956. static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  957. {
  958. int i;
  959. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  960. if (rxq->pool[i].page != NULL) {
  961. pci_unmap_page(priv->pci_dev, rxq->pool[i].page_dma,
  962. PAGE_SIZE << priv->hw_params.rx_page_order,
  963. PCI_DMA_FROMDEVICE);
  964. __iwl_legacy_free_pages(priv, rxq->pool[i].page);
  965. rxq->pool[i].page = NULL;
  966. }
  967. }
  968. dma_free_coherent(&priv->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  969. rxq->bd_dma);
  970. dma_free_coherent(&priv->pci_dev->dev, sizeof(struct iwl_rb_status),
  971. rxq->rb_stts, rxq->rb_stts_dma);
  972. rxq->bd = NULL;
  973. rxq->rb_stts = NULL;
  974. }
  975. /* Convert linear signal-to-noise ratio into dB */
  976. static u8 ratio2dB[100] = {
  977. /* 0 1 2 3 4 5 6 7 8 9 */
  978. 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
  979. 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
  980. 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
  981. 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
  982. 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
  983. 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
  984. 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
  985. 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
  986. 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
  987. 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
  988. };
  989. /* Calculates a relative dB value from a ratio of linear
  990. * (i.e. not dB) signal levels.
  991. * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
  992. int iwl3945_calc_db_from_ratio(int sig_ratio)
  993. {
  994. /* 1000:1 or higher just report as 60 dB */
  995. if (sig_ratio >= 1000)
  996. return 60;
  997. /* 100:1 or higher, divide by 10 and use table,
  998. * add 20 dB to make up for divide by 10 */
  999. if (sig_ratio >= 100)
  1000. return 20 + (int)ratio2dB[sig_ratio/10];
  1001. /* We shouldn't see this */
  1002. if (sig_ratio < 1)
  1003. return 0;
  1004. /* Use table for ratios 1:1 - 99:1 */
  1005. return (int)ratio2dB[sig_ratio];
  1006. }
  1007. /**
  1008. * iwl3945_rx_handle - Main entry function for receiving responses from uCode
  1009. *
  1010. * Uses the priv->rx_handlers callback function array to invoke
  1011. * the appropriate handlers, including command responses,
  1012. * frame-received notifications, and other notifications.
  1013. */
  1014. static void iwl3945_rx_handle(struct iwl_priv *priv)
  1015. {
  1016. struct iwl_rx_mem_buffer *rxb;
  1017. struct iwl_rx_packet *pkt;
  1018. struct iwl_rx_queue *rxq = &priv->rxq;
  1019. u32 r, i;
  1020. int reclaim;
  1021. unsigned long flags;
  1022. u8 fill_rx = 0;
  1023. u32 count = 8;
  1024. int total_empty = 0;
  1025. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  1026. * buffer that the driver may process (last buffer filled by ucode). */
  1027. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  1028. i = rxq->read;
  1029. /* calculate total frames need to be restock after handling RX */
  1030. total_empty = r - rxq->write_actual;
  1031. if (total_empty < 0)
  1032. total_empty += RX_QUEUE_SIZE;
  1033. if (total_empty > (RX_QUEUE_SIZE / 2))
  1034. fill_rx = 1;
  1035. /* Rx interrupt, but nothing sent from uCode */
  1036. if (i == r)
  1037. IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
  1038. while (i != r) {
  1039. int len;
  1040. rxb = rxq->queue[i];
  1041. /* If an RXB doesn't have a Rx queue slot associated with it,
  1042. * then a bug has been introduced in the queue refilling
  1043. * routines -- catch it here */
  1044. BUG_ON(rxb == NULL);
  1045. rxq->queue[i] = NULL;
  1046. pci_unmap_page(priv->pci_dev, rxb->page_dma,
  1047. PAGE_SIZE << priv->hw_params.rx_page_order,
  1048. PCI_DMA_FROMDEVICE);
  1049. pkt = rxb_addr(rxb);
  1050. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  1051. len += sizeof(u32); /* account for status word */
  1052. trace_iwlwifi_legacy_dev_rx(priv, pkt, len);
  1053. /* Reclaim a command buffer only if this packet is a response
  1054. * to a (driver-originated) command.
  1055. * If the packet (e.g. Rx frame) originated from uCode,
  1056. * there is no command buffer to reclaim.
  1057. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  1058. * but apparently a few don't get set; catch them here. */
  1059. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  1060. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  1061. (pkt->hdr.cmd != REPLY_TX);
  1062. /* Based on type of command response or notification,
  1063. * handle those that need handling via function in
  1064. * rx_handlers table. See iwl3945_setup_rx_handlers() */
  1065. if (priv->rx_handlers[pkt->hdr.cmd]) {
  1066. IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r, i,
  1067. iwl_legacy_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  1068. priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
  1069. priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
  1070. } else {
  1071. /* No handling needed */
  1072. IWL_DEBUG_RX(priv,
  1073. "r %d i %d No handler needed for %s, 0x%02x\n",
  1074. r, i, iwl_legacy_get_cmd_string(pkt->hdr.cmd),
  1075. pkt->hdr.cmd);
  1076. }
  1077. /*
  1078. * XXX: After here, we should always check rxb->page
  1079. * against NULL before touching it or its virtual
  1080. * memory (pkt). Because some rx_handler might have
  1081. * already taken or freed the pages.
  1082. */
  1083. if (reclaim) {
  1084. /* Invoke any callbacks, transfer the buffer to caller,
  1085. * and fire off the (possibly) blocking iwl_legacy_send_cmd()
  1086. * as we reclaim the driver command queue */
  1087. if (rxb->page)
  1088. iwl_legacy_tx_cmd_complete(priv, rxb);
  1089. else
  1090. IWL_WARN(priv, "Claim null rxb?\n");
  1091. }
  1092. /* Reuse the page if possible. For notification packets and
  1093. * SKBs that fail to Rx correctly, add them back into the
  1094. * rx_free list for reuse later. */
  1095. spin_lock_irqsave(&rxq->lock, flags);
  1096. if (rxb->page != NULL) {
  1097. rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
  1098. 0, PAGE_SIZE << priv->hw_params.rx_page_order,
  1099. PCI_DMA_FROMDEVICE);
  1100. list_add_tail(&rxb->list, &rxq->rx_free);
  1101. rxq->free_count++;
  1102. } else
  1103. list_add_tail(&rxb->list, &rxq->rx_used);
  1104. spin_unlock_irqrestore(&rxq->lock, flags);
  1105. i = (i + 1) & RX_QUEUE_MASK;
  1106. /* If there are a lot of unused frames,
  1107. * restock the Rx queue so ucode won't assert. */
  1108. if (fill_rx) {
  1109. count++;
  1110. if (count >= 8) {
  1111. rxq->read = i;
  1112. iwl3945_rx_replenish_now(priv);
  1113. count = 0;
  1114. }
  1115. }
  1116. }
  1117. /* Backtrack one entry */
  1118. rxq->read = i;
  1119. if (fill_rx)
  1120. iwl3945_rx_replenish_now(priv);
  1121. else
  1122. iwl3945_rx_queue_restock(priv);
  1123. }
  1124. /* call this function to flush any scheduled tasklet */
  1125. static inline void iwl3945_synchronize_irq(struct iwl_priv *priv)
  1126. {
  1127. /* wait to make sure we flush pending tasklet*/
  1128. synchronize_irq(priv->pci_dev->irq);
  1129. tasklet_kill(&priv->irq_tasklet);
  1130. }
  1131. static const char *iwl3945_desc_lookup(int i)
  1132. {
  1133. switch (i) {
  1134. case 1:
  1135. return "FAIL";
  1136. case 2:
  1137. return "BAD_PARAM";
  1138. case 3:
  1139. return "BAD_CHECKSUM";
  1140. case 4:
  1141. return "NMI_INTERRUPT";
  1142. case 5:
  1143. return "SYSASSERT";
  1144. case 6:
  1145. return "FATAL_ERROR";
  1146. }
  1147. return "UNKNOWN";
  1148. }
  1149. #define ERROR_START_OFFSET (1 * sizeof(u32))
  1150. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  1151. void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
  1152. {
  1153. u32 i;
  1154. u32 desc, time, count, base, data1;
  1155. u32 blink1, blink2, ilink1, ilink2;
  1156. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  1157. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  1158. IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
  1159. return;
  1160. }
  1161. count = iwl_legacy_read_targ_mem(priv, base);
  1162. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  1163. IWL_ERR(priv, "Start IWL Error Log Dump:\n");
  1164. IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
  1165. priv->status, count);
  1166. }
  1167. IWL_ERR(priv, "Desc Time asrtPC blink2 "
  1168. "ilink1 nmiPC Line\n");
  1169. for (i = ERROR_START_OFFSET;
  1170. i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
  1171. i += ERROR_ELEM_SIZE) {
  1172. desc = iwl_legacy_read_targ_mem(priv, base + i);
  1173. time =
  1174. iwl_legacy_read_targ_mem(priv, base + i + 1 * sizeof(u32));
  1175. blink1 =
  1176. iwl_legacy_read_targ_mem(priv, base + i + 2 * sizeof(u32));
  1177. blink2 =
  1178. iwl_legacy_read_targ_mem(priv, base + i + 3 * sizeof(u32));
  1179. ilink1 =
  1180. iwl_legacy_read_targ_mem(priv, base + i + 4 * sizeof(u32));
  1181. ilink2 =
  1182. iwl_legacy_read_targ_mem(priv, base + i + 5 * sizeof(u32));
  1183. data1 =
  1184. iwl_legacy_read_targ_mem(priv, base + i + 6 * sizeof(u32));
  1185. IWL_ERR(priv,
  1186. "%-13s (0x%X) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
  1187. iwl3945_desc_lookup(desc), desc, time, blink1, blink2,
  1188. ilink1, ilink2, data1);
  1189. trace_iwlwifi_legacy_dev_ucode_error(priv, desc, time, data1, 0,
  1190. 0, blink1, blink2, ilink1, ilink2);
  1191. }
  1192. }
  1193. static void iwl3945_irq_tasklet(struct iwl_priv *priv)
  1194. {
  1195. u32 inta, handled = 0;
  1196. u32 inta_fh;
  1197. unsigned long flags;
  1198. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1199. u32 inta_mask;
  1200. #endif
  1201. spin_lock_irqsave(&priv->lock, flags);
  1202. /* Ack/clear/reset pending uCode interrupts.
  1203. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  1204. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  1205. inta = iwl_read32(priv, CSR_INT);
  1206. iwl_write32(priv, CSR_INT, inta);
  1207. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  1208. * Any new interrupts that happen after this, either while we're
  1209. * in this tasklet, or later, will show up in next ISR/tasklet. */
  1210. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  1211. iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
  1212. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1213. if (iwl_legacy_get_debug_level(priv) & IWL_DL_ISR) {
  1214. /* just for debug */
  1215. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  1216. IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  1217. inta, inta_mask, inta_fh);
  1218. }
  1219. #endif
  1220. spin_unlock_irqrestore(&priv->lock, flags);
  1221. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  1222. * atomic, make sure that inta covers all the interrupts that
  1223. * we've discovered, even if FH interrupt came in just after
  1224. * reading CSR_INT. */
  1225. if (inta_fh & CSR39_FH_INT_RX_MASK)
  1226. inta |= CSR_INT_BIT_FH_RX;
  1227. if (inta_fh & CSR39_FH_INT_TX_MASK)
  1228. inta |= CSR_INT_BIT_FH_TX;
  1229. /* Now service all interrupt bits discovered above. */
  1230. if (inta & CSR_INT_BIT_HW_ERR) {
  1231. IWL_ERR(priv, "Hardware error detected. Restarting.\n");
  1232. /* Tell the device to stop sending interrupts */
  1233. iwl_legacy_disable_interrupts(priv);
  1234. priv->isr_stats.hw++;
  1235. iwl_legacy_irq_handle_error(priv);
  1236. handled |= CSR_INT_BIT_HW_ERR;
  1237. return;
  1238. }
  1239. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1240. if (iwl_legacy_get_debug_level(priv) & (IWL_DL_ISR)) {
  1241. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  1242. if (inta & CSR_INT_BIT_SCD) {
  1243. IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
  1244. "the frame/frames.\n");
  1245. priv->isr_stats.sch++;
  1246. }
  1247. /* Alive notification via Rx interrupt will do the real work */
  1248. if (inta & CSR_INT_BIT_ALIVE) {
  1249. IWL_DEBUG_ISR(priv, "Alive interrupt\n");
  1250. priv->isr_stats.alive++;
  1251. }
  1252. }
  1253. #endif
  1254. /* Safely ignore these bits for debug checks below */
  1255. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  1256. /* Error detected by uCode */
  1257. if (inta & CSR_INT_BIT_SW_ERR) {
  1258. IWL_ERR(priv, "Microcode SW error detected. "
  1259. "Restarting 0x%X.\n", inta);
  1260. priv->isr_stats.sw++;
  1261. iwl_legacy_irq_handle_error(priv);
  1262. handled |= CSR_INT_BIT_SW_ERR;
  1263. }
  1264. /* uCode wakes up after power-down sleep */
  1265. if (inta & CSR_INT_BIT_WAKEUP) {
  1266. IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
  1267. iwl_legacy_rx_queue_update_write_ptr(priv, &priv->rxq);
  1268. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[0]);
  1269. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[1]);
  1270. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[2]);
  1271. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[3]);
  1272. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[4]);
  1273. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[5]);
  1274. priv->isr_stats.wakeup++;
  1275. handled |= CSR_INT_BIT_WAKEUP;
  1276. }
  1277. /* All uCode command responses, including Tx command responses,
  1278. * Rx "responses" (frame-received notification), and other
  1279. * notifications from uCode come through here*/
  1280. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  1281. iwl3945_rx_handle(priv);
  1282. priv->isr_stats.rx++;
  1283. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  1284. }
  1285. if (inta & CSR_INT_BIT_FH_TX) {
  1286. IWL_DEBUG_ISR(priv, "Tx interrupt\n");
  1287. priv->isr_stats.tx++;
  1288. iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
  1289. iwl_legacy_write_direct32(priv, FH39_TCSR_CREDIT
  1290. (FH39_SRVC_CHNL), 0x0);
  1291. handled |= CSR_INT_BIT_FH_TX;
  1292. }
  1293. if (inta & ~handled) {
  1294. IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  1295. priv->isr_stats.unhandled++;
  1296. }
  1297. if (inta & ~priv->inta_mask) {
  1298. IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
  1299. inta & ~priv->inta_mask);
  1300. IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
  1301. }
  1302. /* Re-enable all interrupts */
  1303. /* only Re-enable if disabled by irq */
  1304. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  1305. iwl_legacy_enable_interrupts(priv);
  1306. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1307. if (iwl_legacy_get_debug_level(priv) & (IWL_DL_ISR)) {
  1308. inta = iwl_read32(priv, CSR_INT);
  1309. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  1310. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  1311. IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  1312. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  1313. }
  1314. #endif
  1315. }
  1316. static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
  1317. enum ieee80211_band band,
  1318. u8 is_active, u8 n_probes,
  1319. struct iwl3945_scan_channel *scan_ch,
  1320. struct ieee80211_vif *vif)
  1321. {
  1322. struct ieee80211_channel *chan;
  1323. const struct ieee80211_supported_band *sband;
  1324. const struct iwl_channel_info *ch_info;
  1325. u16 passive_dwell = 0;
  1326. u16 active_dwell = 0;
  1327. int added, i;
  1328. sband = iwl_get_hw_mode(priv, band);
  1329. if (!sband)
  1330. return 0;
  1331. active_dwell = iwl_legacy_get_active_dwell_time(priv, band, n_probes);
  1332. passive_dwell = iwl_legacy_get_passive_dwell_time(priv, band, vif);
  1333. if (passive_dwell <= active_dwell)
  1334. passive_dwell = active_dwell + 1;
  1335. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  1336. chan = priv->scan_request->channels[i];
  1337. if (chan->band != band)
  1338. continue;
  1339. scan_ch->channel = chan->hw_value;
  1340. ch_info = iwl_legacy_get_channel_info(priv, band,
  1341. scan_ch->channel);
  1342. if (!iwl_legacy_is_channel_valid(ch_info)) {
  1343. IWL_DEBUG_SCAN(priv,
  1344. "Channel %d is INVALID for this band.\n",
  1345. scan_ch->channel);
  1346. continue;
  1347. }
  1348. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  1349. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  1350. /* If passive , set up for auto-switch
  1351. * and use long active_dwell time.
  1352. */
  1353. if (!is_active || iwl_legacy_is_channel_passive(ch_info) ||
  1354. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
  1355. scan_ch->type = 0; /* passive */
  1356. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  1357. scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
  1358. } else {
  1359. scan_ch->type = 1; /* active */
  1360. }
  1361. /* Set direct probe bits. These may be used both for active
  1362. * scan channels (probes gets sent right away),
  1363. * or for passive channels (probes get se sent only after
  1364. * hearing clear Rx packet).*/
  1365. if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
  1366. if (n_probes)
  1367. scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
  1368. } else {
  1369. /* uCode v1 does not allow setting direct probe bits on
  1370. * passive channel.…