/drivers/net/wireless/hostap/hostap_hw.c

https://bitbucket.org/wisechild/galaxy-nexus · C · 3424 lines · 2481 code · 579 blank · 364 comment · 486 complexity · bb1d47b2964c61ccab6534c0a8327f2c MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * Host AP (software wireless LAN access point) driver for
  3. * Intersil Prism2/2.5/3.
  4. *
  5. * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
  6. * <j@w1.fi>
  7. * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation. See README and COPYING for
  12. * more details.
  13. *
  14. * FIX:
  15. * - there is currently no way of associating TX packets to correct wds device
  16. * when TX Exc/OK event occurs, so all tx_packets and some
  17. * tx_errors/tx_dropped are added to the main netdevice; using sw_support
  18. * field in txdesc might be used to fix this (using Alloc event to increment
  19. * tx_packets would need some further info in txfid table)
  20. *
  21. * Buffer Access Path (BAP) usage:
  22. * Prism2 cards have two separate BAPs for accessing the card memory. These
  23. * should allow concurrent access to two different frames and the driver
  24. * previously used BAP0 for sending data and BAP1 for receiving data.
  25. * However, there seems to be number of issues with concurrent access and at
  26. * least one know hardware bug in using BAP0 and BAP1 concurrently with PCI
  27. * Prism2.5. Therefore, the driver now only uses BAP0 for moving data between
  28. * host and card memories. BAP0 accesses are protected with local->baplock
  29. * (spin_lock_bh) to prevent concurrent use.
  30. */
  31. #include <asm/delay.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/slab.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/if_arp.h>
  38. #include <linux/delay.h>
  39. #include <linux/random.h>
  40. #include <linux/wait.h>
  41. #include <linux/sched.h>
  42. #include <linux/rtnetlink.h>
  43. #include <linux/wireless.h>
  44. #include <net/iw_handler.h>
  45. #include <net/lib80211.h>
  46. #include <asm/irq.h>
  47. #include "hostap_80211.h"
  48. #include "hostap.h"
  49. #include "hostap_ap.h"
  50. /* #define final_version */
  51. static int mtu = 1500;
  52. module_param(mtu, int, 0444);
  53. MODULE_PARM_DESC(mtu, "Maximum transfer unit");
  54. static int channel[MAX_PARM_DEVICES] = { 3, DEF_INTS };
  55. module_param_array(channel, int, NULL, 0444);
  56. MODULE_PARM_DESC(channel, "Initial channel");
  57. static char essid[33] = "test";
  58. module_param_string(essid, essid, sizeof(essid), 0444);
  59. MODULE_PARM_DESC(essid, "Host AP's ESSID");
  60. static int iw_mode[MAX_PARM_DEVICES] = { IW_MODE_MASTER, DEF_INTS };
  61. module_param_array(iw_mode, int, NULL, 0444);
  62. MODULE_PARM_DESC(iw_mode, "Initial operation mode");
  63. static int beacon_int[MAX_PARM_DEVICES] = { 100, DEF_INTS };
  64. module_param_array(beacon_int, int, NULL, 0444);
  65. MODULE_PARM_DESC(beacon_int, "Beacon interval (1 = 1024 usec)");
  66. static int dtim_period[MAX_PARM_DEVICES] = { 1, DEF_INTS };
  67. module_param_array(dtim_period, int, NULL, 0444);
  68. MODULE_PARM_DESC(dtim_period, "DTIM period");
  69. static char dev_template[16] = "wlan%d";
  70. module_param_string(dev_template, dev_template, sizeof(dev_template), 0444);
  71. MODULE_PARM_DESC(dev_template, "Prefix for network device name (default: "
  72. "wlan%d)");
  73. #ifdef final_version
  74. #define EXTRA_EVENTS_WTERR 0
  75. #else
  76. /* check WTERR events (Wait Time-out) in development versions */
  77. #define EXTRA_EVENTS_WTERR HFA384X_EV_WTERR
  78. #endif
  79. /* Events that will be using BAP0 */
  80. #define HFA384X_BAP0_EVENTS \
  81. (HFA384X_EV_TXEXC | HFA384X_EV_RX | HFA384X_EV_INFO | HFA384X_EV_TX)
  82. /* event mask, i.e., events that will result in an interrupt */
  83. #define HFA384X_EVENT_MASK \
  84. (HFA384X_BAP0_EVENTS | HFA384X_EV_ALLOC | HFA384X_EV_INFDROP | \
  85. HFA384X_EV_CMD | HFA384X_EV_TICK | \
  86. EXTRA_EVENTS_WTERR)
  87. /* Default TX control flags: use 802.11 headers and request interrupt for
  88. * failed transmits. Frames that request ACK callback, will add
  89. * _TX_OK flag and _ALT_RTRY flag may be used to select different retry policy.
  90. */
  91. #define HFA384X_TX_CTRL_FLAGS \
  92. (HFA384X_TX_CTRL_802_11 | HFA384X_TX_CTRL_TX_EX)
  93. /* ca. 1 usec */
  94. #define HFA384X_CMD_BUSY_TIMEOUT 5000
  95. #define HFA384X_BAP_BUSY_TIMEOUT 50000
  96. /* ca. 10 usec */
  97. #define HFA384X_CMD_COMPL_TIMEOUT 20000
  98. #define HFA384X_DL_COMPL_TIMEOUT 1000000
  99. /* Wait times for initialization; yield to other processes to avoid busy
  100. * waiting for long time. */
  101. #define HFA384X_INIT_TIMEOUT (HZ / 2) /* 500 ms */
  102. #define HFA384X_ALLOC_COMPL_TIMEOUT (HZ / 20) /* 50 ms */
  103. static void prism2_hw_reset(struct net_device *dev);
  104. static void prism2_check_sta_fw_version(local_info_t *local);
  105. #ifdef PRISM2_DOWNLOAD_SUPPORT
  106. /* hostap_download.c */
  107. static int prism2_download_aux_dump(struct net_device *dev,
  108. unsigned int addr, int len, u8 *buf);
  109. static u8 * prism2_read_pda(struct net_device *dev);
  110. static int prism2_download(local_info_t *local,
  111. struct prism2_download_param *param);
  112. static void prism2_download_free_data(struct prism2_download_data *dl);
  113. static int prism2_download_volatile(local_info_t *local,
  114. struct prism2_download_data *param);
  115. static int prism2_download_genesis(local_info_t *local,
  116. struct prism2_download_data *param);
  117. static int prism2_get_ram_size(local_info_t *local);
  118. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  119. #ifndef final_version
  120. /* magic value written to SWSUPPORT0 reg. for detecting whether card is still
  121. * present */
  122. #define HFA384X_MAGIC 0x8A32
  123. #endif
  124. static u16 hfa384x_read_reg(struct net_device *dev, u16 reg)
  125. {
  126. return HFA384X_INW(reg);
  127. }
  128. static void hfa384x_read_regs(struct net_device *dev,
  129. struct hfa384x_regs *regs)
  130. {
  131. regs->cmd = HFA384X_INW(HFA384X_CMD_OFF);
  132. regs->evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
  133. regs->offset0 = HFA384X_INW(HFA384X_OFFSET0_OFF);
  134. regs->offset1 = HFA384X_INW(HFA384X_OFFSET1_OFF);
  135. regs->swsupport0 = HFA384X_INW(HFA384X_SWSUPPORT0_OFF);
  136. }
  137. /**
  138. * __hostap_cmd_queue_free - Free Prism2 command queue entry (private)
  139. * @local: pointer to private Host AP driver data
  140. * @entry: Prism2 command queue entry to be freed
  141. * @del_req: request the entry to be removed
  142. *
  143. * Internal helper function for freeing Prism2 command queue entries.
  144. * Caller must have acquired local->cmdlock before calling this function.
  145. */
  146. static inline void __hostap_cmd_queue_free(local_info_t *local,
  147. struct hostap_cmd_queue *entry,
  148. int del_req)
  149. {
  150. if (del_req) {
  151. entry->del_req = 1;
  152. if (!list_empty(&entry->list)) {
  153. list_del_init(&entry->list);
  154. local->cmd_queue_len--;
  155. }
  156. }
  157. if (atomic_dec_and_test(&entry->usecnt) && entry->del_req)
  158. kfree(entry);
  159. }
  160. /**
  161. * hostap_cmd_queue_free - Free Prism2 command queue entry
  162. * @local: pointer to private Host AP driver data
  163. * @entry: Prism2 command queue entry to be freed
  164. * @del_req: request the entry to be removed
  165. *
  166. * Free a Prism2 command queue entry.
  167. */
  168. static inline void hostap_cmd_queue_free(local_info_t *local,
  169. struct hostap_cmd_queue *entry,
  170. int del_req)
  171. {
  172. unsigned long flags;
  173. spin_lock_irqsave(&local->cmdlock, flags);
  174. __hostap_cmd_queue_free(local, entry, del_req);
  175. spin_unlock_irqrestore(&local->cmdlock, flags);
  176. }
  177. /**
  178. * prism2_clear_cmd_queue - Free all pending Prism2 command queue entries
  179. * @local: pointer to private Host AP driver data
  180. */
  181. static void prism2_clear_cmd_queue(local_info_t *local)
  182. {
  183. struct list_head *ptr, *n;
  184. unsigned long flags;
  185. struct hostap_cmd_queue *entry;
  186. spin_lock_irqsave(&local->cmdlock, flags);
  187. list_for_each_safe(ptr, n, &local->cmd_queue) {
  188. entry = list_entry(ptr, struct hostap_cmd_queue, list);
  189. atomic_inc(&entry->usecnt);
  190. printk(KERN_DEBUG "%s: removed pending cmd_queue entry "
  191. "(type=%d, cmd=0x%04x, param0=0x%04x)\n",
  192. local->dev->name, entry->type, entry->cmd,
  193. entry->param0);
  194. __hostap_cmd_queue_free(local, entry, 1);
  195. }
  196. if (local->cmd_queue_len) {
  197. /* This should not happen; print debug message and clear
  198. * queue length. */
  199. printk(KERN_DEBUG "%s: cmd_queue_len (%d) not zero after "
  200. "flush\n", local->dev->name, local->cmd_queue_len);
  201. local->cmd_queue_len = 0;
  202. }
  203. spin_unlock_irqrestore(&local->cmdlock, flags);
  204. }
  205. /**
  206. * hfa384x_cmd_issue - Issue a Prism2 command to the hardware
  207. * @dev: pointer to net_device
  208. * @entry: Prism2 command queue entry to be issued
  209. */
  210. static int hfa384x_cmd_issue(struct net_device *dev,
  211. struct hostap_cmd_queue *entry)
  212. {
  213. struct hostap_interface *iface;
  214. local_info_t *local;
  215. int tries;
  216. u16 reg;
  217. unsigned long flags;
  218. iface = netdev_priv(dev);
  219. local = iface->local;
  220. if (local->func->card_present && !local->func->card_present(local))
  221. return -ENODEV;
  222. if (entry->issued) {
  223. printk(KERN_DEBUG "%s: driver bug - re-issuing command @%p\n",
  224. dev->name, entry);
  225. }
  226. /* wait until busy bit is clear; this should always be clear since the
  227. * commands are serialized */
  228. tries = HFA384X_CMD_BUSY_TIMEOUT;
  229. while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
  230. tries--;
  231. udelay(1);
  232. }
  233. #ifndef final_version
  234. if (tries != HFA384X_CMD_BUSY_TIMEOUT) {
  235. prism2_io_debug_error(dev, 1);
  236. printk(KERN_DEBUG "%s: hfa384x_cmd_issue: cmd reg was busy "
  237. "for %d usec\n", dev->name,
  238. HFA384X_CMD_BUSY_TIMEOUT - tries);
  239. }
  240. #endif
  241. if (tries == 0) {
  242. reg = HFA384X_INW(HFA384X_CMD_OFF);
  243. prism2_io_debug_error(dev, 2);
  244. printk(KERN_DEBUG "%s: hfa384x_cmd_issue - timeout - "
  245. "reg=0x%04x\n", dev->name, reg);
  246. return -ETIMEDOUT;
  247. }
  248. /* write command */
  249. spin_lock_irqsave(&local->cmdlock, flags);
  250. HFA384X_OUTW(entry->param0, HFA384X_PARAM0_OFF);
  251. HFA384X_OUTW(entry->param1, HFA384X_PARAM1_OFF);
  252. HFA384X_OUTW(entry->cmd, HFA384X_CMD_OFF);
  253. entry->issued = 1;
  254. spin_unlock_irqrestore(&local->cmdlock, flags);
  255. return 0;
  256. }
  257. /**
  258. * hfa384x_cmd - Issue a Prism2 command and wait (sleep) for completion
  259. * @dev: pointer to net_device
  260. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  261. * @param0: value for Param0 register
  262. * @param1: value for Param1 register (pointer; %NULL if not used)
  263. * @resp0: pointer for Resp0 data or %NULL if Resp0 is not needed
  264. *
  265. * Issue given command (possibly after waiting in command queue) and sleep
  266. * until the command is completed (or timed out or interrupted). This can be
  267. * called only from user process context.
  268. */
  269. static int hfa384x_cmd(struct net_device *dev, u16 cmd, u16 param0,
  270. u16 *param1, u16 *resp0)
  271. {
  272. struct hostap_interface *iface;
  273. local_info_t *local;
  274. int err, res, issue, issued = 0;
  275. unsigned long flags;
  276. struct hostap_cmd_queue *entry;
  277. DECLARE_WAITQUEUE(wait, current);
  278. iface = netdev_priv(dev);
  279. local = iface->local;
  280. if (in_interrupt()) {
  281. printk(KERN_DEBUG "%s: hfa384x_cmd called from interrupt "
  282. "context\n", dev->name);
  283. return -1;
  284. }
  285. if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN) {
  286. printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
  287. dev->name);
  288. return -1;
  289. }
  290. if (signal_pending(current))
  291. return -EINTR;
  292. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  293. if (entry == NULL) {
  294. printk(KERN_DEBUG "%s: hfa384x_cmd - kmalloc failed\n",
  295. dev->name);
  296. return -ENOMEM;
  297. }
  298. atomic_set(&entry->usecnt, 1);
  299. entry->type = CMD_SLEEP;
  300. entry->cmd = cmd;
  301. entry->param0 = param0;
  302. if (param1)
  303. entry->param1 = *param1;
  304. init_waitqueue_head(&entry->compl);
  305. /* prepare to wait for command completion event, but do not sleep yet
  306. */
  307. add_wait_queue(&entry->compl, &wait);
  308. set_current_state(TASK_INTERRUPTIBLE);
  309. spin_lock_irqsave(&local->cmdlock, flags);
  310. issue = list_empty(&local->cmd_queue);
  311. if (issue)
  312. entry->issuing = 1;
  313. list_add_tail(&entry->list, &local->cmd_queue);
  314. local->cmd_queue_len++;
  315. spin_unlock_irqrestore(&local->cmdlock, flags);
  316. err = 0;
  317. if (!issue)
  318. goto wait_completion;
  319. if (signal_pending(current))
  320. err = -EINTR;
  321. if (!err) {
  322. if (hfa384x_cmd_issue(dev, entry))
  323. err = -ETIMEDOUT;
  324. else
  325. issued = 1;
  326. }
  327. wait_completion:
  328. if (!err && entry->type != CMD_COMPLETED) {
  329. /* sleep until command is completed or timed out */
  330. res = schedule_timeout(2 * HZ);
  331. } else
  332. res = -1;
  333. if (!err && signal_pending(current))
  334. err = -EINTR;
  335. if (err && issued) {
  336. /* the command was issued, so a CmdCompl event should occur
  337. * soon; however, there's a pending signal and
  338. * schedule_timeout() would be interrupted; wait a short period
  339. * of time to avoid removing entry from the list before
  340. * CmdCompl event */
  341. udelay(300);
  342. }
  343. set_current_state(TASK_RUNNING);
  344. remove_wait_queue(&entry->compl, &wait);
  345. /* If entry->list is still in the list, it must be removed
  346. * first and in this case prism2_cmd_ev() does not yet have
  347. * local reference to it, and the data can be kfree()'d
  348. * here. If the command completion event is still generated,
  349. * it will be assigned to next (possibly) pending command, but
  350. * the driver will reset the card anyway due to timeout
  351. *
  352. * If the entry is not in the list prism2_cmd_ev() has a local
  353. * reference to it, but keeps cmdlock as long as the data is
  354. * needed, so the data can be kfree()'d here. */
  355. /* FIX: if the entry->list is in the list, it has not been completed
  356. * yet, so removing it here is somewhat wrong.. this could cause
  357. * references to freed memory and next list_del() causing NULL pointer
  358. * dereference.. it would probably be better to leave the entry in the
  359. * list and the list should be emptied during hw reset */
  360. spin_lock_irqsave(&local->cmdlock, flags);
  361. if (!list_empty(&entry->list)) {
  362. printk(KERN_DEBUG "%s: hfa384x_cmd: entry still in list? "
  363. "(entry=%p, type=%d, res=%d)\n", dev->name, entry,
  364. entry->type, res);
  365. list_del_init(&entry->list);
  366. local->cmd_queue_len--;
  367. }
  368. spin_unlock_irqrestore(&local->cmdlock, flags);
  369. if (err) {
  370. printk(KERN_DEBUG "%s: hfa384x_cmd: interrupted; err=%d\n",
  371. dev->name, err);
  372. res = err;
  373. goto done;
  374. }
  375. if (entry->type != CMD_COMPLETED) {
  376. u16 reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
  377. printk(KERN_DEBUG "%s: hfa384x_cmd: command was not "
  378. "completed (res=%d, entry=%p, type=%d, cmd=0x%04x, "
  379. "param0=0x%04x, EVSTAT=%04x INTEN=%04x)\n", dev->name,
  380. res, entry, entry->type, entry->cmd, entry->param0, reg,
  381. HFA384X_INW(HFA384X_INTEN_OFF));
  382. if (reg & HFA384X_EV_CMD) {
  383. /* Command completion event is pending, but the
  384. * interrupt was not delivered - probably an issue
  385. * with pcmcia-cs configuration. */
  386. printk(KERN_WARNING "%s: interrupt delivery does not "
  387. "seem to work\n", dev->name);
  388. }
  389. prism2_io_debug_error(dev, 3);
  390. res = -ETIMEDOUT;
  391. goto done;
  392. }
  393. if (resp0 != NULL)
  394. *resp0 = entry->resp0;
  395. #ifndef final_version
  396. if (entry->res) {
  397. printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x, "
  398. "resp0=0x%04x\n",
  399. dev->name, cmd, entry->res, entry->resp0);
  400. }
  401. #endif /* final_version */
  402. res = entry->res;
  403. done:
  404. hostap_cmd_queue_free(local, entry, 1);
  405. return res;
  406. }
  407. /**
  408. * hfa384x_cmd_callback - Issue a Prism2 command; callback when completed
  409. * @dev: pointer to net_device
  410. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  411. * @param0: value for Param0 register
  412. * @callback: command completion callback function (%NULL = no callback)
  413. * @context: context data to be given to the callback function
  414. *
  415. * Issue given command (possibly after waiting in command queue) and use
  416. * callback function to indicate command completion. This can be called both
  417. * from user and interrupt context. The callback function will be called in
  418. * hardware IRQ context. It can be %NULL, when no function is called when
  419. * command is completed.
  420. */
  421. static int hfa384x_cmd_callback(struct net_device *dev, u16 cmd, u16 param0,
  422. void (*callback)(struct net_device *dev,
  423. long context, u16 resp0,
  424. u16 status),
  425. long context)
  426. {
  427. struct hostap_interface *iface;
  428. local_info_t *local;
  429. int issue, ret;
  430. unsigned long flags;
  431. struct hostap_cmd_queue *entry;
  432. iface = netdev_priv(dev);
  433. local = iface->local;
  434. if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN + 2) {
  435. printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
  436. dev->name);
  437. return -1;
  438. }
  439. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  440. if (entry == NULL) {
  441. printk(KERN_DEBUG "%s: hfa384x_cmd_callback - kmalloc "
  442. "failed\n", dev->name);
  443. return -ENOMEM;
  444. }
  445. atomic_set(&entry->usecnt, 1);
  446. entry->type = CMD_CALLBACK;
  447. entry->cmd = cmd;
  448. entry->param0 = param0;
  449. entry->callback = callback;
  450. entry->context = context;
  451. spin_lock_irqsave(&local->cmdlock, flags);
  452. issue = list_empty(&local->cmd_queue);
  453. if (issue)
  454. entry->issuing = 1;
  455. list_add_tail(&entry->list, &local->cmd_queue);
  456. local->cmd_queue_len++;
  457. spin_unlock_irqrestore(&local->cmdlock, flags);
  458. if (issue && hfa384x_cmd_issue(dev, entry))
  459. ret = -ETIMEDOUT;
  460. else
  461. ret = 0;
  462. hostap_cmd_queue_free(local, entry, ret);
  463. return ret;
  464. }
  465. /**
  466. * __hfa384x_cmd_no_wait - Issue a Prism2 command (private)
  467. * @dev: pointer to net_device
  468. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  469. * @param0: value for Param0 register
  470. * @io_debug_num: I/O debug error number
  471. *
  472. * Shared helper function for hfa384x_cmd_wait() and hfa384x_cmd_no_wait().
  473. */
  474. static int __hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd, u16 param0,
  475. int io_debug_num)
  476. {
  477. int tries;
  478. u16 reg;
  479. /* wait until busy bit is clear; this should always be clear since the
  480. * commands are serialized */
  481. tries = HFA384X_CMD_BUSY_TIMEOUT;
  482. while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
  483. tries--;
  484. udelay(1);
  485. }
  486. if (tries == 0) {
  487. reg = HFA384X_INW(HFA384X_CMD_OFF);
  488. prism2_io_debug_error(dev, io_debug_num);
  489. printk(KERN_DEBUG "%s: __hfa384x_cmd_no_wait(%d) - timeout - "
  490. "reg=0x%04x\n", dev->name, io_debug_num, reg);
  491. return -ETIMEDOUT;
  492. }
  493. /* write command */
  494. HFA384X_OUTW(param0, HFA384X_PARAM0_OFF);
  495. HFA384X_OUTW(cmd, HFA384X_CMD_OFF);
  496. return 0;
  497. }
  498. /**
  499. * hfa384x_cmd_wait - Issue a Prism2 command and busy wait for completion
  500. * @dev: pointer to net_device
  501. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  502. * @param0: value for Param0 register
  503. */
  504. static int hfa384x_cmd_wait(struct net_device *dev, u16 cmd, u16 param0)
  505. {
  506. int res, tries;
  507. u16 reg;
  508. res = __hfa384x_cmd_no_wait(dev, cmd, param0, 4);
  509. if (res)
  510. return res;
  511. /* wait for command completion */
  512. if ((cmd & HFA384X_CMDCODE_MASK) == HFA384X_CMDCODE_DOWNLOAD)
  513. tries = HFA384X_DL_COMPL_TIMEOUT;
  514. else
  515. tries = HFA384X_CMD_COMPL_TIMEOUT;
  516. while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
  517. tries > 0) {
  518. tries--;
  519. udelay(10);
  520. }
  521. if (tries == 0) {
  522. reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
  523. prism2_io_debug_error(dev, 5);
  524. printk(KERN_DEBUG "%s: hfa384x_cmd_wait - timeout2 - "
  525. "reg=0x%04x\n", dev->name, reg);
  526. return -ETIMEDOUT;
  527. }
  528. res = (HFA384X_INW(HFA384X_STATUS_OFF) &
  529. (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) | BIT(9) |
  530. BIT(8))) >> 8;
  531. #ifndef final_version
  532. if (res) {
  533. printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x\n",
  534. dev->name, cmd, res);
  535. }
  536. #endif
  537. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  538. return res;
  539. }
  540. /**
  541. * hfa384x_cmd_no_wait - Issue a Prism2 command; do not wait for completion
  542. * @dev: pointer to net_device
  543. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  544. * @param0: value for Param0 register
  545. */
  546. static inline int hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd,
  547. u16 param0)
  548. {
  549. return __hfa384x_cmd_no_wait(dev, cmd, param0, 6);
  550. }
  551. /**
  552. * prism2_cmd_ev - Prism2 command completion event handler
  553. * @dev: pointer to net_device
  554. *
  555. * Interrupt handler for command completion events. Called by the main
  556. * interrupt handler in hardware IRQ context. Read Resp0 and status registers
  557. * from the hardware and ACK the event. Depending on the issued command type
  558. * either wake up the sleeping process that is waiting for command completion
  559. * or call the callback function. Issue the next command, if one is pending.
  560. */
  561. static void prism2_cmd_ev(struct net_device *dev)
  562. {
  563. struct hostap_interface *iface;
  564. local_info_t *local;
  565. struct hostap_cmd_queue *entry = NULL;
  566. iface = netdev_priv(dev);
  567. local = iface->local;
  568. spin_lock(&local->cmdlock);
  569. if (!list_empty(&local->cmd_queue)) {
  570. entry = list_entry(local->cmd_queue.next,
  571. struct hostap_cmd_queue, list);
  572. atomic_inc(&entry->usecnt);
  573. list_del_init(&entry->list);
  574. local->cmd_queue_len--;
  575. if (!entry->issued) {
  576. printk(KERN_DEBUG "%s: Command completion event, but "
  577. "cmd not issued\n", dev->name);
  578. __hostap_cmd_queue_free(local, entry, 1);
  579. entry = NULL;
  580. }
  581. }
  582. spin_unlock(&local->cmdlock);
  583. if (!entry) {
  584. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  585. printk(KERN_DEBUG "%s: Command completion event, but no "
  586. "pending commands\n", dev->name);
  587. return;
  588. }
  589. entry->resp0 = HFA384X_INW(HFA384X_RESP0_OFF);
  590. entry->res = (HFA384X_INW(HFA384X_STATUS_OFF) &
  591. (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) |
  592. BIT(9) | BIT(8))) >> 8;
  593. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  594. /* TODO: rest of the CmdEv handling could be moved to tasklet */
  595. if (entry->type == CMD_SLEEP) {
  596. entry->type = CMD_COMPLETED;
  597. wake_up_interruptible(&entry->compl);
  598. } else if (entry->type == CMD_CALLBACK) {
  599. if (entry->callback)
  600. entry->callback(dev, entry->context, entry->resp0,
  601. entry->res);
  602. } else {
  603. printk(KERN_DEBUG "%s: Invalid command completion type %d\n",
  604. dev->name, entry->type);
  605. }
  606. hostap_cmd_queue_free(local, entry, 1);
  607. /* issue next command, if pending */
  608. entry = NULL;
  609. spin_lock(&local->cmdlock);
  610. if (!list_empty(&local->cmd_queue)) {
  611. entry = list_entry(local->cmd_queue.next,
  612. struct hostap_cmd_queue, list);
  613. if (entry->issuing) {
  614. /* hfa384x_cmd() has already started issuing this
  615. * command, so do not start here */
  616. entry = NULL;
  617. }
  618. if (entry)
  619. atomic_inc(&entry->usecnt);
  620. }
  621. spin_unlock(&local->cmdlock);
  622. if (entry) {
  623. /* issue next command; if command issuing fails, remove the
  624. * entry from cmd_queue */
  625. int res = hfa384x_cmd_issue(dev, entry);
  626. spin_lock(&local->cmdlock);
  627. __hostap_cmd_queue_free(local, entry, res);
  628. spin_unlock(&local->cmdlock);
  629. }
  630. }
  631. static int hfa384x_wait_offset(struct net_device *dev, u16 o_off)
  632. {
  633. int tries = HFA384X_BAP_BUSY_TIMEOUT;
  634. int res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
  635. while (res && tries > 0) {
  636. tries--;
  637. udelay(1);
  638. res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
  639. }
  640. return res;
  641. }
  642. /* Offset must be even */
  643. static int hfa384x_setup_bap(struct net_device *dev, u16 bap, u16 id,
  644. int offset)
  645. {
  646. u16 o_off, s_off;
  647. int ret = 0;
  648. if (offset % 2 || bap > 1)
  649. return -EINVAL;
  650. if (bap == BAP1) {
  651. o_off = HFA384X_OFFSET1_OFF;
  652. s_off = HFA384X_SELECT1_OFF;
  653. } else {
  654. o_off = HFA384X_OFFSET0_OFF;
  655. s_off = HFA384X_SELECT0_OFF;
  656. }
  657. if (hfa384x_wait_offset(dev, o_off)) {
  658. prism2_io_debug_error(dev, 7);
  659. printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout before\n",
  660. dev->name);
  661. ret = -ETIMEDOUT;
  662. goto out;
  663. }
  664. HFA384X_OUTW(id, s_off);
  665. HFA384X_OUTW(offset, o_off);
  666. if (hfa384x_wait_offset(dev, o_off)) {
  667. prism2_io_debug_error(dev, 8);
  668. printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout after\n",
  669. dev->name);
  670. ret = -ETIMEDOUT;
  671. goto out;
  672. }
  673. #ifndef final_version
  674. if (HFA384X_INW(o_off) & HFA384X_OFFSET_ERR) {
  675. prism2_io_debug_error(dev, 9);
  676. printk(KERN_DEBUG "%s: hfa384x_setup_bap - offset error "
  677. "(%d,0x04%x,%d); reg=0x%04x\n",
  678. dev->name, bap, id, offset, HFA384X_INW(o_off));
  679. ret = -EINVAL;
  680. }
  681. #endif
  682. out:
  683. return ret;
  684. }
  685. static int hfa384x_get_rid(struct net_device *dev, u16 rid, void *buf, int len,
  686. int exact_len)
  687. {
  688. struct hostap_interface *iface;
  689. local_info_t *local;
  690. int res, rlen = 0;
  691. struct hfa384x_rid_hdr rec;
  692. iface = netdev_priv(dev);
  693. local = iface->local;
  694. if (local->no_pri) {
  695. printk(KERN_DEBUG "%s: cannot get RID %04x (len=%d) - no PRI "
  696. "f/w\n", dev->name, rid, len);
  697. return -ENOTTY; /* Well.. not really correct, but return
  698. * something unique enough.. */
  699. }
  700. if ((local->func->card_present && !local->func->card_present(local)) ||
  701. local->hw_downloading)
  702. return -ENODEV;
  703. res = mutex_lock_interruptible(&local->rid_bap_mtx);
  704. if (res)
  705. return res;
  706. res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS, rid, NULL, NULL);
  707. if (res) {
  708. printk(KERN_DEBUG "%s: hfa384x_get_rid: CMDCODE_ACCESS failed "
  709. "(res=%d, rid=%04x, len=%d)\n",
  710. dev->name, res, rid, len);
  711. mutex_unlock(&local->rid_bap_mtx);
  712. return res;
  713. }
  714. spin_lock_bh(&local->baplock);
  715. res = hfa384x_setup_bap(dev, BAP0, rid, 0);
  716. if (!res)
  717. res = hfa384x_from_bap(dev, BAP0, &rec, sizeof(rec));
  718. if (le16_to_cpu(rec.len) == 0) {
  719. /* RID not available */
  720. res = -ENODATA;
  721. }
  722. rlen = (le16_to_cpu(rec.len) - 1) * 2;
  723. if (!res && exact_len && rlen != len) {
  724. printk(KERN_DEBUG "%s: hfa384x_get_rid - RID len mismatch: "
  725. "rid=0x%04x, len=%d (expected %d)\n",
  726. dev->name, rid, rlen, len);
  727. res = -ENODATA;
  728. }
  729. if (!res)
  730. res = hfa384x_from_bap(dev, BAP0, buf, len);
  731. spin_unlock_bh(&local->baplock);
  732. mutex_unlock(&local->rid_bap_mtx);
  733. if (res) {
  734. if (res != -ENODATA)
  735. printk(KERN_DEBUG "%s: hfa384x_get_rid (rid=%04x, "
  736. "len=%d) - failed - res=%d\n", dev->name, rid,
  737. len, res);
  738. if (res == -ETIMEDOUT)
  739. prism2_hw_reset(dev);
  740. return res;
  741. }
  742. return rlen;
  743. }
  744. static int hfa384x_set_rid(struct net_device *dev, u16 rid, void *buf, int len)
  745. {
  746. struct hostap_interface *iface;
  747. local_info_t *local;
  748. struct hfa384x_rid_hdr rec;
  749. int res;
  750. iface = netdev_priv(dev);
  751. local = iface->local;
  752. if (local->no_pri) {
  753. printk(KERN_DEBUG "%s: cannot set RID %04x (len=%d) - no PRI "
  754. "f/w\n", dev->name, rid, len);
  755. return -ENOTTY; /* Well.. not really correct, but return
  756. * something unique enough.. */
  757. }
  758. if ((local->func->card_present && !local->func->card_present(local)) ||
  759. local->hw_downloading)
  760. return -ENODEV;
  761. rec.rid = cpu_to_le16(rid);
  762. /* RID len in words and +1 for rec.rid */
  763. rec.len = cpu_to_le16(len / 2 + len % 2 + 1);
  764. res = mutex_lock_interruptible(&local->rid_bap_mtx);
  765. if (res)
  766. return res;
  767. spin_lock_bh(&local->baplock);
  768. res = hfa384x_setup_bap(dev, BAP0, rid, 0);
  769. if (!res)
  770. res = hfa384x_to_bap(dev, BAP0, &rec, sizeof(rec));
  771. if (!res)
  772. res = hfa384x_to_bap(dev, BAP0, buf, len);
  773. spin_unlock_bh(&local->baplock);
  774. if (res) {
  775. printk(KERN_DEBUG "%s: hfa384x_set_rid (rid=%04x, len=%d) - "
  776. "failed - res=%d\n", dev->name, rid, len, res);
  777. mutex_unlock(&local->rid_bap_mtx);
  778. return res;
  779. }
  780. res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS_WRITE, rid, NULL, NULL);
  781. mutex_unlock(&local->rid_bap_mtx);
  782. if (res) {
  783. printk(KERN_DEBUG "%s: hfa384x_set_rid: CMDCODE_ACCESS_WRITE "
  784. "failed (res=%d, rid=%04x, len=%d)\n",
  785. dev->name, res, rid, len);
  786. if (res == -ETIMEDOUT)
  787. prism2_hw_reset(dev);
  788. }
  789. return res;
  790. }
  791. static void hfa384x_disable_interrupts(struct net_device *dev)
  792. {
  793. /* disable interrupts and clear event status */
  794. HFA384X_OUTW(0, HFA384X_INTEN_OFF);
  795. HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
  796. }
  797. static void hfa384x_enable_interrupts(struct net_device *dev)
  798. {
  799. /* ack pending events and enable interrupts from selected events */
  800. HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
  801. HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
  802. }
  803. static void hfa384x_events_no_bap0(struct net_device *dev)
  804. {
  805. HFA384X_OUTW(HFA384X_EVENT_MASK & ~HFA384X_BAP0_EVENTS,
  806. HFA384X_INTEN_OFF);
  807. }
  808. static void hfa384x_events_all(struct net_device *dev)
  809. {
  810. HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
  811. }
  812. static void hfa384x_events_only_cmd(struct net_device *dev)
  813. {
  814. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_INTEN_OFF);
  815. }
  816. static u16 hfa384x_allocate_fid(struct net_device *dev, int len)
  817. {
  818. u16 fid;
  819. unsigned long delay;
  820. /* FIX: this could be replace with hfa384x_cmd() if the Alloc event
  821. * below would be handled like CmdCompl event (sleep here, wake up from
  822. * interrupt handler */
  823. if (hfa384x_cmd_wait(dev, HFA384X_CMDCODE_ALLOC, len)) {
  824. printk(KERN_DEBUG "%s: cannot allocate fid, len=%d\n",
  825. dev->name, len);
  826. return 0xffff;
  827. }
  828. delay = jiffies + HFA384X_ALLOC_COMPL_TIMEOUT;
  829. while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC) &&
  830. time_before(jiffies, delay))
  831. yield();
  832. if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC)) {
  833. printk("%s: fid allocate, len=%d - timeout\n", dev->name, len);
  834. return 0xffff;
  835. }
  836. fid = HFA384X_INW(HFA384X_ALLOCFID_OFF);
  837. HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
  838. return fid;
  839. }
  840. static int prism2_reset_port(struct net_device *dev)
  841. {
  842. struct hostap_interface *iface;
  843. local_info_t *local;
  844. int res;
  845. iface = netdev_priv(dev);
  846. local = iface->local;
  847. if (!local->dev_enabled)
  848. return 0;
  849. res = hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0,
  850. NULL, NULL);
  851. if (res)
  852. printk(KERN_DEBUG "%s: reset port failed to disable port\n",
  853. dev->name);
  854. else {
  855. res = hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0,
  856. NULL, NULL);
  857. if (res)
  858. printk(KERN_DEBUG "%s: reset port failed to enable "
  859. "port\n", dev->name);
  860. }
  861. /* It looks like at least some STA firmware versions reset
  862. * fragmentation threshold back to 2346 after enable command. Restore
  863. * the configured value, if it differs from this default. */
  864. if (local->fragm_threshold != 2346 &&
  865. hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
  866. local->fragm_threshold)) {
  867. printk(KERN_DEBUG "%s: failed to restore fragmentation "
  868. "threshold (%d) after Port0 enable\n",
  869. dev->name, local->fragm_threshold);
  870. }
  871. /* Some firmwares lose antenna selection settings on reset */
  872. (void) hostap_set_antsel(local);
  873. return res;
  874. }
  875. static int prism2_get_version_info(struct net_device *dev, u16 rid,
  876. const char *txt)
  877. {
  878. struct hfa384x_comp_ident comp;
  879. struct hostap_interface *iface;
  880. local_info_t *local;
  881. iface = netdev_priv(dev);
  882. local = iface->local;
  883. if (local->no_pri) {
  884. /* PRI f/w not yet available - cannot read RIDs */
  885. return -1;
  886. }
  887. if (hfa384x_get_rid(dev, rid, &comp, sizeof(comp), 1) < 0) {
  888. printk(KERN_DEBUG "Could not get RID for component %s\n", txt);
  889. return -1;
  890. }
  891. printk(KERN_INFO "%s: %s: id=0x%02x v%d.%d.%d\n", dev->name, txt,
  892. __le16_to_cpu(comp.id), __le16_to_cpu(comp.major),
  893. __le16_to_cpu(comp.minor), __le16_to_cpu(comp.variant));
  894. return 0;
  895. }
  896. static int prism2_setup_rids(struct net_device *dev)
  897. {
  898. struct hostap_interface *iface;
  899. local_info_t *local;
  900. __le16 tmp;
  901. int ret = 0;
  902. iface = netdev_priv(dev);
  903. local = iface->local;
  904. hostap_set_word(dev, HFA384X_RID_TICKTIME, 2000);
  905. if (!local->fw_ap) {
  906. u16 tmp1 = hostap_get_porttype(local);
  907. ret = hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE, tmp1);
  908. if (ret) {
  909. printk("%s: Port type setting to %d failed\n",
  910. dev->name, tmp1);
  911. goto fail;
  912. }
  913. }
  914. /* Setting SSID to empty string seems to kill the card in Host AP mode
  915. */
  916. if (local->iw_mode != IW_MODE_MASTER || local->essid[0] != '\0') {
  917. ret = hostap_set_string(dev, HFA384X_RID_CNFOWNSSID,
  918. local->essid);
  919. if (ret) {
  920. printk("%s: AP own SSID setting failed\n", dev->name);
  921. goto fail;
  922. }
  923. }
  924. ret = hostap_set_word(dev, HFA384X_RID_CNFMAXDATALEN,
  925. PRISM2_DATA_MAXLEN);
  926. if (ret) {
  927. printk("%s: MAC data length setting to %d failed\n",
  928. dev->name, PRISM2_DATA_MAXLEN);
  929. goto fail;
  930. }
  931. if (hfa384x_get_rid(dev, HFA384X_RID_CHANNELLIST, &tmp, 2, 1) < 0) {
  932. printk("%s: Channel list read failed\n", dev->name);
  933. ret = -EINVAL;
  934. goto fail;
  935. }
  936. local->channel_mask = le16_to_cpu(tmp);
  937. if (local->channel < 1 || local->channel > 14 ||
  938. !(local->channel_mask & (1 << (local->channel - 1)))) {
  939. printk(KERN_WARNING "%s: Channel setting out of range "
  940. "(%d)!\n", dev->name, local->channel);
  941. ret = -EBUSY;
  942. goto fail;
  943. }
  944. ret = hostap_set_word(dev, HFA384X_RID_CNFOWNCHANNEL, local->channel);
  945. if (ret) {
  946. printk("%s: Channel setting to %d failed\n",
  947. dev->name, local->channel);
  948. goto fail;
  949. }
  950. ret = hostap_set_word(dev, HFA384X_RID_CNFBEACONINT,
  951. local->beacon_int);
  952. if (ret) {
  953. printk("%s: Beacon interval setting to %d failed\n",
  954. dev->name, local->beacon_int);
  955. /* this may fail with Symbol/Lucent firmware */
  956. if (ret == -ETIMEDOUT)
  957. goto fail;
  958. }
  959. ret = hostap_set_word(dev, HFA384X_RID_CNFOWNDTIMPERIOD,
  960. local->dtim_period);
  961. if (ret) {
  962. printk("%s: DTIM period setting to %d failed\n",
  963. dev->name, local->dtim_period);
  964. /* this may fail with Symbol/Lucent firmware */
  965. if (ret == -ETIMEDOUT)
  966. goto fail;
  967. }
  968. ret = hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
  969. local->is_promisc);
  970. if (ret)
  971. printk(KERN_INFO "%s: Setting promiscuous mode (%d) failed\n",
  972. dev->name, local->is_promisc);
  973. if (!local->fw_ap) {
  974. ret = hostap_set_string(dev, HFA384X_RID_CNFDESIREDSSID,
  975. local->essid);
  976. if (ret) {
  977. printk("%s: Desired SSID setting failed\n", dev->name);
  978. goto fail;
  979. }
  980. }
  981. /* Setup TXRateControl, defaults to allow use of 1, 2, 5.5, and
  982. * 11 Mbps in automatic TX rate fallback and 1 and 2 Mbps as basic
  983. * rates */
  984. if (local->tx_rate_control == 0) {
  985. local->tx_rate_control =
  986. HFA384X_RATES_1MBPS |
  987. HFA384X_RATES_2MBPS |
  988. HFA384X_RATES_5MBPS |
  989. HFA384X_RATES_11MBPS;
  990. }
  991. if (local->basic_rates == 0)
  992. local->basic_rates = HFA384X_RATES_1MBPS | HFA384X_RATES_2MBPS;
  993. if (!local->fw_ap) {
  994. ret = hostap_set_word(dev, HFA384X_RID_TXRATECONTROL,
  995. local->tx_rate_control);
  996. if (ret) {
  997. printk("%s: TXRateControl setting to %d failed\n",
  998. dev->name, local->tx_rate_control);
  999. goto fail;
  1000. }
  1001. ret = hostap_set_word(dev, HFA384X_RID_CNFSUPPORTEDRATES,
  1002. local->tx_rate_control);
  1003. if (ret) {
  1004. printk("%s: cnfSupportedRates setting to %d failed\n",
  1005. dev->name, local->tx_rate_control);
  1006. }
  1007. ret = hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
  1008. local->basic_rates);
  1009. if (ret) {
  1010. printk("%s: cnfBasicRates setting to %d failed\n",
  1011. dev->name, local->basic_rates);
  1012. }
  1013. ret = hostap_set_word(dev, HFA384X_RID_CREATEIBSS, 1);
  1014. if (ret) {
  1015. printk("%s: Create IBSS setting to 1 failed\n",
  1016. dev->name);
  1017. }
  1018. }
  1019. if (local->name_set)
  1020. (void) hostap_set_string(dev, HFA384X_RID_CNFOWNNAME,
  1021. local->name);
  1022. if (hostap_set_encryption(local)) {
  1023. printk(KERN_INFO "%s: could not configure encryption\n",
  1024. dev->name);
  1025. }
  1026. (void) hostap_set_antsel(local);
  1027. if (hostap_set_roaming(local)) {
  1028. printk(KERN_INFO "%s: could not set host roaming\n",
  1029. dev->name);
  1030. }
  1031. if (local->sta_fw_ver >= PRISM2_FW_VER(1,6,3) &&
  1032. hostap_set_word(dev, HFA384X_RID_CNFENHSECURITY, local->enh_sec))
  1033. printk(KERN_INFO "%s: cnfEnhSecurity setting to 0x%x failed\n",
  1034. dev->name, local->enh_sec);
  1035. /* 32-bit tallies were added in STA f/w 0.8.0, but they were apparently
  1036. * not working correctly (last seven counters report bogus values).
  1037. * This has been fixed in 0.8.2, so enable 32-bit tallies only
  1038. * beginning with that firmware version. Another bug fix for 32-bit
  1039. * tallies in 1.4.0; should 16-bit tallies be used for some other
  1040. * versions, too? */
  1041. if (local->sta_fw_ver >= PRISM2_FW_VER(0,8,2)) {
  1042. if (hostap_set_word(dev, HFA384X_RID_CNFTHIRTY2TALLY, 1)) {
  1043. printk(KERN_INFO "%s: cnfThirty2Tally setting "
  1044. "failed\n", dev->name);
  1045. local->tallies32 = 0;
  1046. } else
  1047. local->tallies32 = 1;
  1048. } else
  1049. local->tallies32 = 0;
  1050. hostap_set_auth_algs(local);
  1051. if (hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
  1052. local->fragm_threshold)) {
  1053. printk(KERN_INFO "%s: setting FragmentationThreshold to %d "
  1054. "failed\n", dev->name, local->fragm_threshold);
  1055. }
  1056. if (hostap_set_word(dev, HFA384X_RID_RTSTHRESHOLD,
  1057. local->rts_threshold)) {
  1058. printk(KERN_INFO "%s: setting RTSThreshold to %d failed\n",
  1059. dev->name, local->rts_threshold);
  1060. }
  1061. if (local->manual_retry_count >= 0 &&
  1062. hostap_set_word(dev, HFA384X_RID_CNFALTRETRYCOUNT,
  1063. local->manual_retry_count)) {
  1064. printk(KERN_INFO "%s: setting cnfAltRetryCount to %d failed\n",
  1065. dev->name, local->manual_retry_count);
  1066. }
  1067. if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1) &&
  1068. hfa384x_get_rid(dev, HFA384X_RID_CNFDBMADJUST, &tmp, 2, 1) == 2) {
  1069. local->rssi_to_dBm = le16_to_cpu(tmp);
  1070. }
  1071. if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->wpa &&
  1072. hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE, 1)) {
  1073. printk(KERN_INFO "%s: setting ssnHandlingMode to 1 failed\n",
  1074. dev->name);
  1075. }
  1076. if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->generic_elem &&
  1077. hfa384x_set_rid(dev, HFA384X_RID_GENERICELEMENT,
  1078. local->generic_elem, local->generic_elem_len)) {
  1079. printk(KERN_INFO "%s: setting genericElement failed\n",
  1080. dev->name);
  1081. }
  1082. fail:
  1083. return ret;
  1084. }
  1085. static int prism2_hw_init(struct net_device *dev, int initial)
  1086. {
  1087. struct hostap_interface *iface;
  1088. local_info_t *local;
  1089. int ret, first = 1;
  1090. unsigned long start, delay;
  1091. PDEBUG(DEBUG_FLOW, "prism2_hw_init()\n");
  1092. iface = netdev_priv(dev);
  1093. local = iface->local;
  1094. clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits);
  1095. init:
  1096. /* initialize HFA 384x */
  1097. ret = hfa384x_cmd_no_wait(dev, HFA384X_CMDCODE_INIT, 0);
  1098. if (ret) {
  1099. printk(KERN_INFO "%s: first command failed - assuming card "
  1100. "does not have primary firmware\n", dev_info);
  1101. }
  1102. if (first && (HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
  1103. /* EvStat has Cmd bit set in some cases, so retry once if no
  1104. * wait was needed */
  1105. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  1106. printk(KERN_DEBUG "%s: init command completed too quickly - "
  1107. "retrying\n", dev->name);
  1108. first = 0;
  1109. goto init;
  1110. }
  1111. start = jiffies;
  1112. delay = jiffies + HFA384X_INIT_TIMEOUT;
  1113. while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
  1114. time_before(jiffies, delay))
  1115. yield();
  1116. if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
  1117. printk(KERN_DEBUG "%s: assuming no Primary image in "
  1118. "flash - card initialization not completed\n",
  1119. dev_info);
  1120. local->no_pri = 1;
  1121. #ifdef PRISM2_DOWNLOAD_SUPPORT
  1122. if (local->sram_type == -1)
  1123. local->sram_type = prism2_get_ram_size(local);
  1124. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  1125. return 1;
  1126. }
  1127. local->no_pri = 0;
  1128. printk(KERN_DEBUG "prism2_hw_init: initialized in %lu ms\n",
  1129. (jiffies - start) * 1000 / HZ);
  1130. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  1131. return 0;
  1132. }
  1133. static int prism2_hw_init2(struct net_device *dev, int initial)
  1134. {
  1135. struct hostap_interface *iface;
  1136. local_info_t *local;
  1137. int i;
  1138. iface = netdev_priv(dev);
  1139. local = iface->local;
  1140. #ifdef PRISM2_DOWNLOAD_SUPPORT
  1141. kfree(local->pda);
  1142. if (local->no_pri)
  1143. local->pda = NULL;
  1144. else
  1145. local->pda = prism2_read_pda(dev);
  1146. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  1147. hfa384x_disable_interrupts(dev);
  1148. #ifndef final_version
  1149. HFA384X_OUTW(HFA384X_MAGIC, HFA384X_SWSUPPORT0_OFF);
  1150. if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
  1151. printk("SWSUPPORT0 write/read failed: %04X != %04X\n",
  1152. HFA384X_INW(HFA384X_SWSUPPORT0_OFF), HFA384X_MAGIC);
  1153. goto failed;
  1154. }
  1155. #endif
  1156. if (initial || local->pri_only) {
  1157. hfa384x_events_only_cmd(dev);
  1158. /* get card version information */
  1159. if (prism2_get_version_info(dev, HFA384X_RID_NICID, "NIC") ||
  1160. prism2_get_version_info(dev, HFA384X_RID_PRIID, "PRI")) {
  1161. hfa384x_disable_interrupts(dev);
  1162. goto failed;
  1163. }
  1164. if (prism2_get_version_info(dev, HFA384X_RID_STAID, "STA")) {
  1165. printk(KERN_DEBUG "%s: Failed to read STA f/w version "
  1166. "- only Primary f/w present\n", dev->name);
  1167. local->pri_only = 1;
  1168. return 0;
  1169. }
  1170. local->pri_only = 0;
  1171. hfa384x_disable_interrupts(dev);
  1172. }
  1173. /* FIX: could convert allocate_fid to use sleeping CmdCompl wait and
  1174. * enable interrupts before this. This would also require some sort of
  1175. * sleeping AllocEv waiting */
  1176. /* allocate TX FIDs */
  1177. local->txfid_len = PRISM2_TXFID_LEN;
  1178. for (i = 0; i < PRISM2_TXFID_COUNT; i++) {
  1179. local->txfid[i] = hfa384x_allocate_fid(dev, local->txfid_len);
  1180. if (local->txfid[i] == 0xffff && local->txfid_len > 1600) {
  1181. local->txfid[i] = hfa384x_allocate_fid(dev, 1600);
  1182. if (local->txfid[i] != 0xffff) {
  1183. printk(KERN_DEBUG "%s: Using shorter TX FID "
  1184. "(1600 bytes)\n", dev->name);
  1185. local->txfid_len = 1600;
  1186. }
  1187. }
  1188. if (local->txfid[i] == 0xffff)
  1189. goto failed;
  1190. local->intransmitfid[i] = PRISM2_TXFID_EMPTY;
  1191. }
  1192. hfa384x_events_only_cmd(dev);
  1193. if (initial) {
  1194. struct list_head *ptr;
  1195. prism2_check_sta_fw_version(local);
  1196. if (hfa384x_get_rid(dev, HFA384X_RID_CNFOWNMACADDR,
  1197. dev->dev_addr, 6, 1) < 0) {
  1198. printk("%s: could not get own MAC address\n",
  1199. dev->name);
  1200. }
  1201. list_for_each(ptr, &local->hostap_interfaces) {
  1202. iface = list_entry(ptr, struct hostap_interface, list);
  1203. memcpy(iface->dev->dev_addr, dev->dev_addr, ETH_ALEN);
  1204. }
  1205. } else if (local->fw_ap)
  1206. prism2_check_sta_fw_version(local);
  1207. prism2_setup_rids(dev);
  1208. /* MAC is now configured, but port 0 is not yet enabled */
  1209. return 0;
  1210. failed:
  1211. if (!local->no_pri)
  1212. printk(KERN_WARNING "%s: Initialization failed\n", dev_info);
  1213. return 1;
  1214. }
  1215. static int prism2_hw_enable(struct net_device *dev, int initial)
  1216. {
  1217. struct hostap_interface *iface;
  1218. local_info_t *local;
  1219. int was_resetting;
  1220. iface = netdev_priv(dev);
  1221. local = iface->local;
  1222. was_resetting = local->hw_resetting;
  1223. if (hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0, NULL, NULL)) {
  1224. printk("%s: MAC port 0 enabling failed\n", dev->name);
  1225. return 1;
  1226. }
  1227. local->hw_ready = 1;
  1228. local->hw_reset_tries = 0;
  1229. local->hw_resetting = 0;
  1230. hfa384x_enable_interrupts(dev);
  1231. /* at least D-Link DWL-650 seems to require additional port reset
  1232. * before it starts acting as an AP, so reset port automatically
  1233. * here just in case */
  1234. if (initial && prism2_reset_port(dev)) {
  1235. printk("%s: MAC port 0 reseting failed\n", dev->name);
  1236. return 1;
  1237. }
  1238. if (was_resetting && netif_queue_stopped(dev)) {
  1239. /* If hw_reset() was called during pending transmit, netif
  1240. * queue was stopped. Wake it up now since the wlan card has
  1241. * been resetted. */
  1242. netif_wake_queue(dev);
  1243. }
  1244. return 0;
  1245. }
  1246. static int prism2_hw_config(struct net_device *dev, int initial)
  1247. {
  1248. struct hostap_interface *iface;
  1249. local_info_t *local;
  1250. iface = netdev_priv(dev);
  1251. local = iface->local;
  1252. if (local->hw_downloading)
  1253. return 1;
  1254. if (prism2_hw_init(dev, initial)) {
  1255. return local->no_pri ? 0 : 1;
  1256. }
  1257. if (prism2_hw_init2(dev, initial))
  1258. return 1;
  1259. /* Enable firmware if secondary image is loaded and at least one of the
  1260. * netdevices is up. */
  1261. if (!local->pri_only &&
  1262. (initial == 0 || (initial == 2 && local->num_dev_open > 0))) {
  1263. if (!local->dev_enabled)
  1264. prism2_callback(local, PRISM2_CALLBACK_ENABLE);
  1265. local->dev_enabled = 1;
  1266. return prism2_hw_enable(dev, initial);
  1267. }
  1268. return 0;
  1269. }
  1270. static void prism2_hw_shutdown(struct net_device *dev, int no_disable)
  1271. {
  1272. struct hostap_interface *iface;
  1273. local_info_t *local;
  1274. iface = netdev_priv(dev);
  1275. local = iface->local;
  1276. /* Allow only command completion events during disable */
  1277. hfa384x_events_only_cmd(dev);
  1278. local->hw_ready = 0;
  1279. if (local->dev_enabled)
  1280. prism2_callback(local, PRISM2_CALLBACK_DISABLE);
  1281. local->dev_enabled = 0;
  1282. if (local->func->card_present && !local->func->card_present(local)) {
  1283. printk(KERN_DEBUG "%s: card already removed or not configured "
  1284. "during shutdown\n", dev->name);
  1285. return;
  1286. }
  1287. if ((no_disable & HOSTAP_HW_NO_DISABLE) == 0 &&
  1288. hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0, NULL, NULL))
  1289. printk(KERN_WARNING "%s: Shutdown failed\n", dev_info);
  1290. hfa384x_disable_interrupts(dev);
  1291. if (no_disable & HOSTAP_HW_ENABLE_CMDCOMPL)
  1292. hfa384x_events_only_cmd(dev);
  1293. else
  1294. prism2_clear_cmd_queue(local);
  1295. }
  1296. static void prism2_hw_reset(struct net_device *dev)
  1297. {
  1298. struct hostap_interface *iface;
  1299. local_info_t *local;
  1300. #if 0
  1301. static long last_reset = 0;
  1302. /* do not reset card more than once per second to avoid ending up in a
  1303. * busy loop reseting the card */
  1304. if (time_before_eq(jiffies, last_reset + HZ))
  1305. return;
  1306. last_reset = jiffies;
  1307. #endif
  1308. iface = netdev_priv(dev);
  1309. local = iface->local;
  1310. if (in_interrupt()) {
  1311. printk(KERN_DEBUG "%s: driver bug - prism2_hw_reset() called "
  1312. "in interrupt context\n", dev->name);
  1313. return;
  1314. }
  1315. if (local->hw_downloading)
  1316. return;
  1317. if (local->hw_resetting) {
  1318. printk(KERN_WARNING "%s: %s: already resetting card - "
  1319. "ignoring reset request\n", dev_info, dev->name);
  1320. return;
  1321. }
  1322. local->hw_reset_tries++;
  1323. if (local->hw_reset_tries > 10) {
  1324. printk(KERN_WARNING "%s: too many reset tries, skipping\n",
  1325. dev->name);
  1326. return;
  1327. }
  1328. printk(KERN_WARNING "%s: %s: resetting card\n", dev_info, dev->name);
  1329. hfa384x_disable_interrupts(dev);
  1330. local->hw_resetting = 1;
  1331. if (local->func->cor_sreset) {
  1332. /* Host system seems to hang in some cases with high traffic
  1333. * load or shared interrupts during COR sreset. Disable shared
  1334. * interrupts during reset to avoid these crashes. COS sreset
  1335. * takes quite a long time, so it is unfortunate that this
  1336. * seems to be needed. Anyway, I do not know of any better way
  1337. * of avoiding the crash. */
  1338. disable_irq(dev->irq);
  1339. local->func->cor_sreset(local);
  1340. enable_irq(dev->irq);
  1341. }
  1342. prism2_hw_shutdown(dev, 1);
  1343. prism2_hw_config(dev, 0);
  1344. local->hw_resetting = 0;
  1345. #ifdef PRISM2_DOWNLOAD_SUPPORT
  1346. if (local->dl_pri) {
  1347. printk(KERN_DEBUG "%s: persistent download of primary "
  1348. "firmware\n", dev->name);
  1349. if (prism2_download_genesis(local, local->dl_pri) < 0)
  1350. printk(KERN_WARNING "%s: download (PRI) failed\n",
  1351. dev->name);
  1352. }
  1353. if (local->dl_sec) {
  1354. printk(KERN_DEBUG "%s: persistent download of secondary "
  1355. "firmware\n", dev->name);
  1356. if (prism2_download_volatile(local, local->dl_sec) < 0)
  1357. printk(KERN_WARNING "%s: download (SEC) failed\n",
  1358. dev->name);
  1359. }
  1360. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  1361. /* TODO: restore beacon TIM bits for STAs that have buffered frames */
  1362. }
  1363. static void prism2_schedule_reset(local_info_t *local)
  1364. {
  1365. schedule_work(&local->reset_queue);
  1366. }
  1367. /* Called only as scheduled task after noticing card timeout in interrupt
  1368. * context */
  1369. static void handle_reset_queue(struct work_struct *work)
  1370. {
  1371. local_info_t *local = container_of(work, local_info_t, reset_queue);
  1372. printk(KERN_DEBUG "%s: scheduled card reset\n", local->dev->name);
  1373. prism2_hw_reset(local->dev);
  1374. if (netif_queue_stopped(local->dev)) {
  1375. int i;
  1376. for (i = 0; i < PRISM2_TXFID_COUNT; i++)
  1377. if (local->intransmitfid[i] == PRISM2_TXFID_EMPTY) {
  1378. PDEBUG(DEBUG_EXTRA, "prism2_tx_timeout: "
  1379. "wake up queue\n");
  1380. netif_wake_queue(local->dev);
  1381. break;
  1382. }
  1383. }
  1384. }
  1385. static int prism2_get_txfid_idx(local_info_t *local)
  1386. {
  1387. int idx, end;
  1388. unsigned long flags;
  1389. spin_lock_irqsave(&local->txfidlock, flags);
  1390. end = idx = local->next_txfid;
  1391. do {
  1392. if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
  1393. local->intransmitfid[idx] = PRISM2_TXFID_RESERVED;
  1394. spin_unlock_irqrestore(&local->txfidlock, flags);
  1395. return idx;
  1396. }
  1397. idx++;
  1398. if (idx >= PRISM2_TXFID_COUNT)
  1399. idx = 0;
  1400. } while (idx != end);
  1401. spin_unlock_irqrestore(&local->txfidlock, flags);
  1402. PDEBUG(DEBUG_EXTRA2, "prism2_get_txfid_idx: no room in txfid buf: "
  1403. "packet dropped\n");
  1404. local->dev->stats.tx_dropped++;
  1405. return -1;
  1406. }
  1407. /* Called only from hardware IRQ */
  1408. static void prism2_transmit_cb(struct net_device *dev, long context,
  1409. u16 resp0, u16 res)
  1410. {
  1411. struct hostap_interface *iface;
  1412. local_info_t *local;
  1413. int idx = (int) context;
  1414. iface = netdev_priv(dev);
  1415. local = iface->local;
  1416. if (res) {
  1417. printk(KERN_DEBUG "%s: prism2_transmit_cb - res=0x%02x\n",
  1418. dev->name, res);
  1419. return;
  1420. }
  1421. if (idx < 0 || idx >= PRISM2_TXFID_COUNT) {
  1422. printk(KERN_DEBUG "%s: prism2_transmit_cb called with invalid "
  1423. "idx=%d\n", dev->name, idx);
  1424. return;
  1425. }
  1426. if (!test_and_clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
  1427. printk(KERN_DEBUG "%s: driver bug: prism2_transmit_cb called "
  1428. "with no pending transmit\n", dev->name);
  1429. }
  1430. if (netif_queue_stopped(dev)) {
  1431. /* ready for next TX, so wake up queue that was stopped in
  1432. * prism2_transmit() */
  1433. netif_wake_queue(dev);
  1434. }
  1435. spin_lock(&local->txfidlock);
  1436. /* With reclaim, Resp0 contains new txfid for transmit; the old txfid
  1437. * will be automatically allocated for the next TX frame */
  1438. local->intransmitfid[idx] = resp0;
  1439. PDEBUG(DEBUG_FID, "%s: prism2_transmit_cb: txfid[%d]=0x%04x, "
  1440. "resp0=0x%04x, transmit_txfid=0x%04x\n",
  1441. dev->name, idx, local->txfid[idx],
  1442. resp0, local->intransmitfid[local->next_txfid]);
  1443. idx++;
  1444. if (idx >= PRISM2_TXFID_COUNT)
  1445. idx = 0;
  1446. local->next_txfid = idx;
  1447. /* check if all TX buffers are occupied */
  1448. do {
  1449. if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
  1450. spin_unlock(&local->txfidlock);
  1451. return;
  1452. }
  1453. idx++;
  1454. if (idx >= PRISM2_TXFID_COUNT)
  1455. idx = 0;
  1456. } while (idx != local->next_txfid);
  1457. spin_unlock(&local->txfidlock);
  1458. /* no empty TX buffers, stop queue */
  1459. netif_stop_queue(dev);
  1460. }
  1461. /* Called only from software IRQ if PCI bus master is not used (with bus master
  1462. * this can be called both from software and hardware IRQ) */
  1463. static int prism2_transmit(struct net_device *dev, int idx)
  1464. {
  1465. struct hostap_interface *iface;
  1466. local_info_t *local;
  1467. int res;
  1468. iface = netdev_priv(dev);
  1469. local = iface->local;
  1470. /* The driver tries to stop netif queue so that there would not be
  1471. * more than one attempt to transmit frames going on; check that this
  1472. * is really the case */
  1473. if (test_and_set_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
  1474. printk(KERN_DEBUG "%s: driver bug - prism2_transmit() called "
  1475. "when previous TX was pending\n", dev->name);
  1476. return -1;
  1477. }
  1478. /* stop the queue for the time that transmit is pending */
  1479. netif_stop_queue(dev);
  1480. /* transmit packet */
  1481. res = hfa384x_cmd_callback(
  1482. dev,
  1483. HFA384X_CMDCODE_TRANSMIT | HFA384X_CMD_TX_RECLAIM,
  1484. local->txfid[idx],
  1485. prism2_transmit_cb, (long) idx);
  1486. if (res) {
  1487. printk(KERN_DEBUG "%s: prism2_transmit: CMDCODE_TRANSMIT "
  1488. "failed (res=%d)\n", dev