hermes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* hermes.c
  2. *
  3. * Driver core for the "Hermes" wireless MAC controller, as used in
  4. * the Lucent Orinoco and Cabletron RoamAbout cards. It should also
  5. * work on the hfa3841 and hfa3842 MAC controller chips used in the
  6. * Prism II chipsets.
  7. *
  8. * This is not a complete driver, just low-level access routines for
  9. * the MAC controller itself.
  10. *
  11. * Based on the prism2 driver from Absolute Value Systems' linux-wlan
  12. * project, the Linux wvlan_cs driver, Lucent's HCF-Light
  13. * (wvlan_hcf.c) library, and the NetBSD wireless driver (in no
  14. * particular order).
  15. *
  16. * Copyright (C) 2000, David Gibson, Linuxcare Australia.
  17. * (C) Copyright David Gibson, IBM Corp. 2001-2003.
  18. *
  19. * The contents of this file are subject to the Mozilla Public License
  20. * Version 1.1 (the "License"); you may not use this file except in
  21. * compliance with the License. You may obtain a copy of the License
  22. * at http://www.mozilla.org/MPL/
  23. *
  24. * Software distributed under the License is distributed on an "AS IS"
  25. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  26. * the License for the specific language governing rights and
  27. * limitations under the License.
  28. *
  29. * Alternatively, the contents of this file may be used under the
  30. * terms of the GNU General Public License version 2 (the "GPL"), in
  31. * which case the provisions of the GPL are applicable instead of the
  32. * above. If you wish to allow the use of your version of this file
  33. * only under the terms of the GPL and not to allow others to use your
  34. * version of this file under the MPL, indicate your decision by
  35. * deleting the provisions above and replace them with the notice and
  36. * other provisions required by the GPL. If you do not delete the
  37. * provisions above, a recipient may use your version of this file
  38. * under either the MPL or the GPL.
  39. */
  40. #include <linux/config.h>
  41. #include <linux/module.h>
  42. #include <linux/types.h>
  43. #include <linux/threads.h>
  44. #include <linux/smp.h>
  45. #include <asm/io.h>
  46. #include <linux/delay.h>
  47. #include <linux/init.h>
  48. #include <linux/kernel.h>
  49. #include <linux/net.h>
  50. #include <asm/errno.h>
  51. #include "hermes.h"
  52. MODULE_DESCRIPTION("Low-level driver helper for Lucent Hermes chipset and Prism II HFA384x wireless MAC controller");
  53. MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>"
  54. " & David Gibson <hermes@gibson.dropbear.id.au>");
  55. MODULE_LICENSE("Dual MPL/GPL");
  56. /* These are maximum timeouts. Most often, card wil react much faster */
  57. #define CMD_BUSY_TIMEOUT (100) /* In iterations of ~1us */
  58. #define CMD_INIT_TIMEOUT (50000) /* in iterations of ~10us */
  59. #define CMD_COMPL_TIMEOUT (20000) /* in iterations of ~10us */
  60. #define ALLOC_COMPL_TIMEOUT (1000) /* in iterations of ~10us */
  61. /*
  62. * Debugging helpers
  63. */
  64. #define DMSG(stuff...) do {printk(KERN_DEBUG "hermes @ %p: " , hw->iobase); \
  65. printk(stuff);} while (0)
  66. #undef HERMES_DEBUG
  67. #ifdef HERMES_DEBUG
  68. #include <stdarg.h>
  69. #define DEBUG(lvl, stuff...) if ( (lvl) <= HERMES_DEBUG) DMSG(stuff)
  70. #else /* ! HERMES_DEBUG */
  71. #define DEBUG(lvl, stuff...) do { } while (0)
  72. #endif /* ! HERMES_DEBUG */
  73. /*
  74. * Internal functions
  75. */
  76. /* Issue a command to the chip. Waiting for it to complete is the caller's
  77. problem.
  78. Returns -EBUSY if the command register is busy, 0 on success.
  79. Callable from any context.
  80. */
  81. static int hermes_issue_cmd(hermes_t *hw, u16 cmd, u16 param0)
  82. {
  83. int k = CMD_BUSY_TIMEOUT;
  84. u16 reg;
  85. /* First wait for the command register to unbusy */
  86. reg = hermes_read_regn(hw, CMD);
  87. while ( (reg & HERMES_CMD_BUSY) && k ) {
  88. k--;
  89. udelay(1);
  90. reg = hermes_read_regn(hw, CMD);
  91. }
  92. if (reg & HERMES_CMD_BUSY) {
  93. return -EBUSY;
  94. }
  95. hermes_write_regn(hw, PARAM2, 0);
  96. hermes_write_regn(hw, PARAM1, 0);
  97. hermes_write_regn(hw, PARAM0, param0);
  98. hermes_write_regn(hw, CMD, cmd);
  99. return 0;
  100. }
  101. /*
  102. * Function definitions
  103. */
  104. void hermes_struct_init(hermes_t *hw, void __iomem *address, int reg_spacing)
  105. {
  106. hw->iobase = address;
  107. hw->reg_spacing = reg_spacing;
  108. hw->inten = 0x0;
  109. #ifdef HERMES_DEBUG_BUFFER
  110. hw->dbufp = 0;
  111. memset(&hw->dbuf, 0xff, sizeof(hw->dbuf));
  112. memset(&hw->profile, 0, sizeof(hw->profile));
  113. #endif
  114. }
  115. int hermes_init(hermes_t *hw)
  116. {
  117. u16 status, reg;
  118. int err = 0;
  119. int k;
  120. /* We don't want to be interrupted while resetting the chipset */
  121. hw->inten = 0x0;
  122. hermes_write_regn(hw, INTEN, 0);
  123. hermes_write_regn(hw, EVACK, 0xffff);
  124. /* Normally it's a "can't happen" for the command register to
  125. be busy when we go to issue a command because we are
  126. serializing all commands. However we want to have some
  127. chance of resetting the card even if it gets into a stupid
  128. state, so we actually wait to see if the command register
  129. will unbusy itself here. */
  130. k = CMD_BUSY_TIMEOUT;
  131. reg = hermes_read_regn(hw, CMD);
  132. while (k && (reg & HERMES_CMD_BUSY)) {
  133. if (reg == 0xffff) /* Special case - the card has probably been removed,
  134. so don't wait for the timeout */
  135. return -ENODEV;
  136. k--;
  137. udelay(1);
  138. reg = hermes_read_regn(hw, CMD);
  139. }
  140. /* No need to explicitly handle the timeout - if we've timed
  141. out hermes_issue_cmd() will probably return -EBUSY below */
  142. /* According to the documentation, EVSTAT may contain
  143. obsolete event occurrence information. We have to acknowledge
  144. it by writing EVACK. */
  145. reg = hermes_read_regn(hw, EVSTAT);
  146. hermes_write_regn(hw, EVACK, reg);
  147. /* We don't use hermes_docmd_wait here, because the reset wipes
  148. the magic constant in SWSUPPORT0 away, and it gets confused */
  149. err = hermes_issue_cmd(hw, HERMES_CMD_INIT, 0);
  150. if (err)
  151. return err;
  152. reg = hermes_read_regn(hw, EVSTAT);
  153. k = CMD_INIT_TIMEOUT;
  154. while ( (! (reg & HERMES_EV_CMD)) && k) {
  155. k--;
  156. udelay(10);
  157. reg = hermes_read_regn(hw, EVSTAT);
  158. }
  159. hermes_write_regn(hw, SWSUPPORT0, HERMES_MAGIC);
  160. if (! hermes_present(hw)) {
  161. DEBUG(0, "hermes @ 0x%x: Card removed during reset.\n",
  162. hw->iobase);
  163. err = -ENODEV;
  164. goto out;
  165. }
  166. if (! (reg & HERMES_EV_CMD)) {
  167. printk(KERN_ERR "hermes @ %p: "
  168. "Timeout waiting for card to reset (reg=0x%04x)!\n",
  169. hw->iobase, reg);
  170. err = -ETIMEDOUT;
  171. goto out;
  172. }
  173. status = hermes_read_regn(hw, STATUS);
  174. hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
  175. if (status & HERMES_STATUS_RESULT)
  176. err = -EIO;
  177. out:
  178. return err;
  179. }
  180. /* Issue a command to the chip, and (busy!) wait for it to
  181. * complete.
  182. *
  183. * Returns: < 0 on internal error, 0 on success, > 0 on error returned by the firmware
  184. *
  185. * Callable from any context, but locking is your problem. */
  186. int hermes_docmd_wait(hermes_t *hw, u16 cmd, u16 parm0,
  187. struct hermes_response *resp)
  188. {
  189. int err;
  190. int k;
  191. u16 reg;
  192. u16 status;
  193. err = hermes_issue_cmd(hw, cmd, parm0);
  194. if (err) {
  195. if (! hermes_present(hw)) {
  196. if (net_ratelimit())
  197. printk(KERN_WARNING "hermes @ %p: "
  198. "Card removed while issuing command "
  199. "0x%04x.\n", hw->iobase, cmd);
  200. err = -ENODEV;
  201. } else
  202. if (net_ratelimit())
  203. printk(KERN_ERR "hermes @ %p: "
  204. "Error %d issuing command 0x%04x.\n",
  205. hw->iobase, err, cmd);
  206. goto out;
  207. }
  208. reg = hermes_read_regn(hw, EVSTAT);
  209. k = CMD_COMPL_TIMEOUT;
  210. while ( (! (reg & HERMES_EV_CMD)) && k) {
  211. k--;
  212. udelay(10);
  213. reg = hermes_read_regn(hw, EVSTAT);
  214. }
  215. if (! hermes_present(hw)) {
  216. printk(KERN_WARNING "hermes @ %p: Card removed "
  217. "while waiting for command 0x%04x completion.\n",
  218. hw->iobase, cmd);
  219. err = -ENODEV;
  220. goto out;
  221. }
  222. if (! (reg & HERMES_EV_CMD)) {
  223. printk(KERN_ERR "hermes @ %p: Timeout waiting for "
  224. "command 0x%04x completion.\n", hw->iobase, cmd);
  225. err = -ETIMEDOUT;
  226. goto out;
  227. }
  228. status = hermes_read_regn(hw, STATUS);
  229. if (resp) {
  230. resp->status = status;
  231. resp->resp0 = hermes_read_regn(hw, RESP0);
  232. resp->resp1 = hermes_read_regn(hw, RESP1);
  233. resp->resp2 = hermes_read_regn(hw, RESP2);
  234. }
  235. hermes_write_regn(hw, EVACK, HERMES_EV_CMD);
  236. if (status & HERMES_STATUS_RESULT)
  237. err = -EIO;
  238. out:
  239. return err;
  240. }
  241. int hermes_allocate(hermes_t *hw, u16 size, u16 *fid)
  242. {
  243. int err = 0;
  244. int k;
  245. u16 reg;
  246. if ( (size < HERMES_ALLOC_LEN_MIN) || (size > HERMES_ALLOC_LEN_MAX) )
  247. return -EINVAL;
  248. err = hermes_docmd_wait(hw, HERMES_CMD_ALLOC, size, NULL);
  249. if (err) {
  250. return err;
  251. }
  252. reg = hermes_read_regn(hw, EVSTAT);
  253. k = ALLOC_COMPL_TIMEOUT;
  254. while ( (! (reg & HERMES_EV_ALLOC)) && k) {
  255. k--;
  256. udelay(10);
  257. reg = hermes_read_regn(hw, EVSTAT);
  258. }
  259. if (! hermes_present(hw)) {
  260. printk(KERN_WARNING "hermes @ %p: "
  261. "Card removed waiting for frame allocation.\n",
  262. hw->iobase);
  263. return -ENODEV;
  264. }
  265. if (! (reg & HERMES_EV_ALLOC)) {
  266. printk(KERN_ERR "hermes @ %p: "
  267. "Timeout waiting for frame allocation\n",
  268. hw->iobase);
  269. return -ETIMEDOUT;
  270. }
  271. *fid = hermes_read_regn(hw, ALLOCFID);
  272. hermes_write_regn(hw, EVACK, HERMES_EV_ALLOC);
  273. return 0;
  274. }
  275. /* Set up a BAP to read a particular chunk of data from card's internal buffer.
  276. *
  277. * Returns: < 0 on internal failure (errno), 0 on success, >0 on error
  278. * from firmware
  279. *
  280. * Callable from any context */
  281. static int hermes_bap_seek(hermes_t *hw, int bap, u16 id, u16 offset)
  282. {
  283. int sreg = bap ? HERMES_SELECT1 : HERMES_SELECT0;
  284. int oreg = bap ? HERMES_OFFSET1 : HERMES_OFFSET0;
  285. int k;
  286. u16 reg;
  287. /* Paranoia.. */
  288. if ( (offset > HERMES_BAP_OFFSET_MAX) || (offset % 2) )
  289. return -EINVAL;
  290. k = HERMES_BAP_BUSY_TIMEOUT;
  291. reg = hermes_read_reg(hw, oreg);
  292. while ((reg & HERMES_OFFSET_BUSY) && k) {
  293. k--;
  294. udelay(1);
  295. reg = hermes_read_reg(hw, oreg);
  296. }
  297. #ifdef HERMES_DEBUG_BUFFER
  298. hw->profile[HERMES_BAP_BUSY_TIMEOUT - k]++;
  299. if (k < HERMES_BAP_BUSY_TIMEOUT) {
  300. struct hermes_debug_entry *e =
  301. &hw->dbuf[(hw->dbufp++) % HERMES_DEBUG_BUFSIZE];
  302. e->bap = bap;
  303. e->id = id;
  304. e->offset = offset;
  305. e->cycles = HERMES_BAP_BUSY_TIMEOUT - k;
  306. }
  307. #endif
  308. if (reg & HERMES_OFFSET_BUSY)
  309. return -ETIMEDOUT;
  310. /* Now we actually set up the transfer */
  311. hermes_write_reg(hw, sreg, id);
  312. hermes_write_reg(hw, oreg, offset);
  313. /* Wait for the BAP to be ready */
  314. k = HERMES_BAP_BUSY_TIMEOUT;
  315. reg = hermes_read_reg(hw, oreg);
  316. while ( (reg & (HERMES_OFFSET_BUSY | HERMES_OFFSET_ERR)) && k) {
  317. k--;
  318. udelay(1);
  319. reg = hermes_read_reg(hw, oreg);
  320. }
  321. if (reg != offset) {
  322. printk(KERN_ERR "hermes @ %p: BAP%d offset %s: "
  323. "reg=0x%x id=0x%x offset=0x%x\n", hw->iobase, bap,
  324. (reg & HERMES_OFFSET_BUSY) ? "timeout" : "error",
  325. reg, id, offset);
  326. if (reg & HERMES_OFFSET_BUSY) {
  327. return -ETIMEDOUT;
  328. }
  329. return -EIO; /* error or wrong offset */
  330. }
  331. return 0;
  332. }
  333. /* Read a block of data from the chip's buffer, via the
  334. * BAP. Synchronization/serialization is the caller's problem. len
  335. * must be even.
  336. *
  337. * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
  338. */
  339. int hermes_bap_pread(hermes_t *hw, int bap, void *buf, unsigned len,
  340. u16 id, u16 offset)
  341. {
  342. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  343. int err = 0;
  344. if ( (len < 0) || (len % 2) )
  345. return -EINVAL;
  346. err = hermes_bap_seek(hw, bap, id, offset);
  347. if (err)
  348. goto out;
  349. /* Actually do the transfer */
  350. hermes_read_words(hw, dreg, buf, len/2);
  351. out:
  352. return err;
  353. }
  354. /* Write a block of data to the chip's buffer, via the
  355. * BAP. Synchronization/serialization is the caller's problem. len
  356. * must be even.
  357. *
  358. * Returns: < 0 on internal failure (errno), 0 on success, > 0 on error from firmware
  359. */
  360. int hermes_bap_pwrite(hermes_t *hw, int bap, const void *buf, unsigned len,
  361. u16 id, u16 offset)
  362. {
  363. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  364. int err = 0;
  365. if ( (len < 0) || (len % 2) )
  366. return -EINVAL;
  367. err = hermes_bap_seek(hw, bap, id, offset);
  368. if (err)
  369. goto out;
  370. /* Actually do the transfer */
  371. hermes_write_words(hw, dreg, buf, len/2);
  372. out:
  373. return err;
  374. }
  375. /* Read a Length-Type-Value record from the card.
  376. *
  377. * If length is NULL, we ignore the length read from the card, and
  378. * read the entire buffer regardless. This is useful because some of
  379. * the configuration records appear to have incorrect lengths in
  380. * practice.
  381. *
  382. * Callable from user or bh context. */
  383. int hermes_read_ltv(hermes_t *hw, int bap, u16 rid, unsigned bufsize,
  384. u16 *length, void *buf)
  385. {
  386. int err = 0;
  387. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  388. u16 rlength, rtype;
  389. unsigned nwords;
  390. if ( (bufsize < 0) || (bufsize % 2) )
  391. return -EINVAL;
  392. err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS, rid, NULL);
  393. if (err)
  394. return err;
  395. err = hermes_bap_seek(hw, bap, rid, 0);
  396. if (err)
  397. return err;
  398. rlength = hermes_read_reg(hw, dreg);
  399. if (! rlength)
  400. return -ENODATA;
  401. rtype = hermes_read_reg(hw, dreg);
  402. if (length)
  403. *length = rlength;
  404. if (rtype != rid)
  405. printk(KERN_WARNING "hermes @ %p: %s(): "
  406. "rid (0x%04x) does not match type (0x%04x)\n",
  407. hw->iobase, __FUNCTION__, rid, rtype);
  408. if (HERMES_RECLEN_TO_BYTES(rlength) > bufsize)
  409. printk(KERN_WARNING "hermes @ %p: "
  410. "Truncating LTV record from %d to %d bytes. "
  411. "(rid=0x%04x, len=0x%04x)\n", hw->iobase,
  412. HERMES_RECLEN_TO_BYTES(rlength), bufsize, rid, rlength);
  413. nwords = min((unsigned)rlength - 1, bufsize / 2);
  414. hermes_read_words(hw, dreg, buf, nwords);
  415. return 0;
  416. }
  417. int hermes_write_ltv(hermes_t *hw, int bap, u16 rid,
  418. u16 length, const void *value)
  419. {
  420. int dreg = bap ? HERMES_DATA1 : HERMES_DATA0;
  421. int err = 0;
  422. unsigned count;
  423. if (length == 0)
  424. return -EINVAL;
  425. err = hermes_bap_seek(hw, bap, rid, 0);
  426. if (err)
  427. return err;
  428. hermes_write_reg(hw, dreg, length);
  429. hermes_write_reg(hw, dreg, rid);
  430. count = length - 1;
  431. hermes_write_words(hw, dreg, value, count);
  432. err = hermes_docmd_wait(hw, HERMES_CMD_ACCESS | HERMES_CMD_WRITE,
  433. rid, NULL);
  434. return err;
  435. }
  436. EXPORT_SYMBOL(hermes_struct_init);
  437. EXPORT_SYMBOL(hermes_init);
  438. EXPORT_SYMBOL(hermes_docmd_wait);
  439. EXPORT_SYMBOL(hermes_allocate);
  440. EXPORT_SYMBOL(hermes_bap_pread);
  441. EXPORT_SYMBOL(hermes_bap_pwrite);
  442. EXPORT_SYMBOL(hermes_read_ltv);
  443. EXPORT_SYMBOL(hermes_write_ltv);
  444. static int __init init_hermes(void)
  445. {
  446. return 0;
  447. }
  448. static void __exit exit_hermes(void)
  449. {
  450. }
  451. module_init(init_hermes);
  452. module_exit(exit_hermes);