You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prism2.c 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /**************************************************************************
  2. Etherboot - BOOTP/TFTP Bootstrap Program
  3. Prism2 NIC driver for Etherboot
  4. Written by Michael Brown of Fen Systems Ltd
  5. $Id$
  6. ***************************************************************************/
  7. /*
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2, or (at
  11. * your option) any later version.
  12. */
  13. FILE_LICENCE ( GPL2_OR_LATER );
  14. #include <etherboot.h>
  15. #include <nic.h>
  16. #include <gpxe/pci.h>
  17. #include <gpxe/ethernet.h>
  18. /*
  19. * Hard-coded SSID
  20. * Leave blank in order to connect to any available SSID
  21. */
  22. static const char hardcoded_ssid[] = "";
  23. /*
  24. * Maximum number of info packets to wait for on a join attempt.
  25. * Some APs (including the Linksys WAP11) will send a "you are disconnected" packet
  26. * before sending the "you are connected" packet, if the card has previously been
  27. * attached to the AP.
  28. *
  29. * 2 is probably a sensible value, but YMMV.
  30. */
  31. #define MAX_JOIN_INFO_COUNT 2
  32. /*
  33. * Type of Prism2 interface to support
  34. * If not already defined, select PLX
  35. */
  36. #ifndef WLAN_HOSTIF
  37. #define WLAN_HOSTIF WLAN_PLX
  38. #endif
  39. /*
  40. * Include wlan_compat, p80211 and hfa384x header files from Linux Prism2 driver
  41. * We need to hack some defines in order to avoid compiling kernel-specific routines
  42. */
  43. #define __LINUX_WLAN__
  44. #undef __KERNEL__
  45. #define __I386__
  46. #include "wlan_compat.h"
  47. #include "p80211hdr.h"
  48. #include "hfa384x.h"
  49. #define BAP_TIMEOUT ( 5000 )
  50. /*
  51. * A few hacks to make the coding environment more Linux-like. This makes it somewhat
  52. * quicker to convert code from the Linux Prism2 driver.
  53. */
  54. #include <errno.h>
  55. #define __le16_to_cpu(x) (x)
  56. #define __le32_to_cpu(x) (x)
  57. #define __cpu_to_le16(x) (x)
  58. #define __cpu_to_le32(x) (x)
  59. #define hfa384x2host_16(n) (__le16_to_cpu((UINT16)(n)))
  60. #define hfa384x2host_32(n) (__le32_to_cpu((UINT32)(n)))
  61. #define host2hfa384x_16(n) (__cpu_to_le16((UINT16)(n)))
  62. #define host2hfa384x_32(n) (__cpu_to_le32((UINT32)(n)))
  63. /*
  64. * PLX9052 PCI register offsets
  65. * Taken from PLX9052 datasheet available from http://www.plxtech.com/download/9052/databook/9052db-20.pdf
  66. */
  67. #define PLX_LOCAL_CONFIG_REGISTER_BASE ( PCI_BASE_ADDRESS_1 )
  68. #define PLX_LOCAL_ADDRESS_SPACE_0_BASE ( PCI_BASE_ADDRESS_2 )
  69. #define PLX_LOCAL_ADDRESS_SPACE_1_BASE ( PCI_BASE_ADDRESS_3 )
  70. #define PLX_LOCAL_ADDRESS_SPACE_2_BASE ( PCI_BASE_ADDRESS_4 )
  71. #define PLX_LOCAL_ADDRESS_SPACE_3_BASE ( PCI_BASE_ADDRESS_5 )
  72. #define PRISM2_PLX_ATTR_MEM_BASE ( PLX_LOCAL_ADDRESS_SPACE_0_BASE )
  73. #define PRISM2_PLX_IO_BASE ( PLX_LOCAL_ADDRESS_SPACE_1_BASE )
  74. #define PRISM2_PCI_MEM_BASE ( PCI_BASE_ADDRESS_0 )
  75. /*
  76. * PCMCIA CIS types
  77. * Taken from cistpl.h in pcmcia-cs
  78. */
  79. #define CISTPL_VERS_1 ( 0x15 )
  80. #define CISTPL_END ( 0xff )
  81. #define CIS_STEP ( 2 )
  82. #define CISTPL_HEADER_LEN ( 2 * CIS_STEP )
  83. #define CISTPL_LEN_OFF ( 1 * CIS_STEP )
  84. #define CISTPL_VERS_1_STR_OFF ( 4 * CIS_STEP )
  85. /*
  86. * Prism2 constants
  87. * Taken from prism2sta.c in linux-wlan-ng
  88. */
  89. #define COR_OFFSET ( 0x3e0 ) /* COR attribute offset of Prism2 PC card */
  90. #define COR_VALUE ( 0x41 ) /* Enable PC card with irq in level trigger (but interrupts disabled) */
  91. /* NIC specific static variables */
  92. /* The hfa384x_t structure is used extensively in the Linux driver but is ifdef'd out in our include since __KERNEL__ is not defined.
  93. * This is a dummy version that contains only the fields we are interested in.
  94. */
  95. typedef struct hfa384x
  96. {
  97. UINT32 iobase;
  98. void *membase;
  99. UINT16 lastcmd;
  100. UINT16 status; /* in host order */
  101. UINT16 resp0; /* in host order */
  102. UINT16 resp1; /* in host order */
  103. UINT16 resp2; /* in host order */
  104. UINT8 bssid[WLAN_BSSID_LEN];
  105. } hfa384x_t;
  106. /* The global instance of the hardware (i.e. where we store iobase and membase, in the absence of anywhere better to put them */
  107. static hfa384x_t hw_global = {
  108. 0, 0, 0, 0, 0, 0, 0, {0,0,0,0,0,0}
  109. };
  110. /*
  111. * 802.11 headers in addition to those in hfa384x_tx_frame_t (LLC and SNAP)
  112. * Taken from p80211conv.h
  113. */
  114. typedef struct wlan_llc
  115. {
  116. UINT8 dsap;
  117. UINT8 ssap;
  118. UINT8 ctl;
  119. } wlan_llc_t;
  120. static const wlan_llc_t wlan_llc_snap = { 0xaa, 0xaa, 0x03 }; /* LLC header indicating SNAP (?) */
  121. #define WLAN_IEEE_OUI_LEN 3
  122. typedef struct wlan_snap
  123. {
  124. UINT8 oui[WLAN_IEEE_OUI_LEN];
  125. UINT16 type;
  126. } wlan_snap_t;
  127. typedef struct wlan_80211hdr
  128. {
  129. wlan_llc_t llc;
  130. wlan_snap_t snap;
  131. } wlan_80211hdr_t;
  132. /*
  133. * Function prototypes
  134. */
  135. /*
  136. * Hardware-level hfa384x functions
  137. * These are based on the ones in hfa384x.h (which are ifdef'd out since __KERNEL__ is not defined).
  138. * Basically, these functions are the result of hand-evaluating all the ifdefs and defines in the hfa384x.h versions.
  139. */
  140. /* Retrieve the value of one of the MAC registers. */
  141. static inline UINT16 hfa384x_getreg( hfa384x_t *hw, UINT reg )
  142. {
  143. #if (WLAN_HOSTIF == WLAN_PLX)
  144. return inw ( hw->iobase + reg );
  145. #elif (WLAN_HOSTIF == WLAN_PCI)
  146. return readw ( hw->membase + reg );
  147. #endif
  148. }
  149. /* Set the value of one of the MAC registers. */
  150. static inline void hfa384x_setreg( hfa384x_t *hw, UINT16 val, UINT reg )
  151. {
  152. #if (WLAN_HOSTIF == WLAN_PLX)
  153. outw ( val, hw->iobase + reg );
  154. #elif (WLAN_HOSTIF == WLAN_PCI)
  155. writew ( val, hw->membase + reg );
  156. #endif
  157. return;
  158. }
  159. /*
  160. * Noswap versions
  161. * Etherboot is i386 only, so swap and noswap are the same...
  162. */
  163. static inline UINT16 hfa384x_getreg_noswap( hfa384x_t *hw, UINT reg )
  164. {
  165. return hfa384x_getreg ( hw, reg );
  166. }
  167. static inline void hfa384x_setreg_noswap( hfa384x_t *hw, UINT16 val, UINT reg )
  168. {
  169. hfa384x_setreg ( hw, val, reg );
  170. }
  171. /*
  172. * Low-level hfa384x functions
  173. * These are based on the ones in hfa384x.c, modified to work in the Etherboot environment.
  174. */
  175. /*
  176. * hfa384x_docmd_wait
  177. *
  178. * Waits for availability of the Command register, then
  179. * issues the given command. Then polls the Evstat register
  180. * waiting for command completion.
  181. * Arguments:
  182. * hw device structure
  183. * cmd Command in host order
  184. * parm0 Parameter0 in host order
  185. * parm1 Parameter1 in host order
  186. * parm2 Parameter2 in host order
  187. * Returns:
  188. * 0 success
  189. * >0 command indicated error, Status and Resp0-2 are
  190. * in hw structure.
  191. */
  192. static int hfa384x_docmd_wait( hfa384x_t *hw, UINT16 cmd, UINT16 parm0, UINT16 parm1, UINT16 parm2)
  193. {
  194. UINT16 reg = 0;
  195. UINT16 counter = 0;
  196. /* wait for the busy bit to clear */
  197. counter = 0;
  198. reg = hfa384x_getreg(hw, HFA384x_CMD);
  199. while ( HFA384x_CMD_ISBUSY(reg) && (counter < 10) ) {
  200. reg = hfa384x_getreg(hw, HFA384x_CMD);
  201. counter++;
  202. udelay(10);
  203. }
  204. if (HFA384x_CMD_ISBUSY(reg)) {
  205. printf("hfa384x_cmd timeout(1), reg=0x%0hx.\n", reg);
  206. return -ETIMEDOUT;
  207. }
  208. /* busy bit clear, write command */
  209. hfa384x_setreg(hw, parm0, HFA384x_PARAM0);
  210. hfa384x_setreg(hw, parm1, HFA384x_PARAM1);
  211. hfa384x_setreg(hw, parm2, HFA384x_PARAM2);
  212. hw->lastcmd = cmd;
  213. hfa384x_setreg(hw, cmd, HFA384x_CMD);
  214. /* Now wait for completion */
  215. counter = 0;
  216. reg = hfa384x_getreg(hw, HFA384x_EVSTAT);
  217. /* Initialization is the problem. It takes about
  218. 100ms. "normal" commands are typically is about
  219. 200-400 us (I've never seen less than 200). Longer
  220. is better so that we're not hammering the bus. */
  221. while ( !HFA384x_EVSTAT_ISCMD(reg) && (counter < 5000)) {
  222. reg = hfa384x_getreg(hw, HFA384x_EVSTAT);
  223. counter++;
  224. udelay(200);
  225. }
  226. if ( ! HFA384x_EVSTAT_ISCMD(reg) ) {
  227. printf("hfa384x_cmd timeout(2), reg=0x%0hx.\n", reg);
  228. return -ETIMEDOUT;
  229. }
  230. /* Read status and response */
  231. hw->status = hfa384x_getreg(hw, HFA384x_STATUS);
  232. hw->resp0 = hfa384x_getreg(hw, HFA384x_RESP0);
  233. hw->resp1 = hfa384x_getreg(hw, HFA384x_RESP1);
  234. hw->resp2 = hfa384x_getreg(hw, HFA384x_RESP2);
  235. hfa384x_setreg(hw, HFA384x_EVACK_CMD, HFA384x_EVACK);
  236. return HFA384x_STATUS_RESULT_GET(hw->status);
  237. }
  238. /*
  239. * Prepare BAP for access. Assigns FID and RID, sets offset register
  240. * and waits for BAP to become available.
  241. *
  242. * Arguments:
  243. * hw device structure
  244. * id FID or RID, destined for the select register (host order)
  245. * offset An _even_ offset into the buffer for the given FID/RID.
  246. * Returns:
  247. * 0 success
  248. */
  249. static int hfa384x_prepare_bap(hfa384x_t *hw, UINT16 id, UINT16 offset)
  250. {
  251. int result = 0;
  252. UINT16 reg;
  253. UINT16 i;
  254. /* Validate offset, buf, and len */
  255. if ( (offset > HFA384x_BAP_OFFSET_MAX) || (offset % 2) ) {
  256. result = -EINVAL;
  257. } else {
  258. /* Write fid/rid and offset */
  259. hfa384x_setreg(hw, id, HFA384x_SELECT0);
  260. udelay(10);
  261. hfa384x_setreg(hw, offset, HFA384x_OFFSET0);
  262. /* Wait for offset[busy] to clear (see BAP_TIMEOUT) */
  263. i = 0;
  264. do {
  265. reg = hfa384x_getreg(hw, HFA384x_OFFSET0);
  266. if ( i > 0 ) udelay(2);
  267. i++;
  268. } while ( i < BAP_TIMEOUT && HFA384x_OFFSET_ISBUSY(reg));
  269. if ( i >= BAP_TIMEOUT ) {
  270. /* failure */
  271. result = reg;
  272. } else if ( HFA384x_OFFSET_ISERR(reg) ){
  273. /* failure */
  274. result = reg;
  275. }
  276. }
  277. return result;
  278. }
  279. /*
  280. * Copy data from BAP to memory.
  281. *
  282. * Arguments:
  283. * hw device structure
  284. * id FID or RID, destined for the select register (host order)
  285. * offset An _even_ offset into the buffer for the given FID/RID.
  286. * buf ptr to array of bytes
  287. * len length of data to transfer in bytes
  288. * Returns:
  289. * 0 success
  290. */
  291. static int hfa384x_copy_from_bap(hfa384x_t *hw, UINT16 id, UINT16 offset,
  292. void *buf, UINT len)
  293. {
  294. int result = 0;
  295. UINT8 *d = (UINT8*)buf;
  296. UINT16 i;
  297. UINT16 reg = 0;
  298. /* Prepare BAP */
  299. result = hfa384x_prepare_bap ( hw, id, offset );
  300. if ( result == 0 ) {
  301. /* Read even(len) buf contents from data reg */
  302. for ( i = 0; i < (len & 0xfffe); i+=2 ) {
  303. *(UINT16*)(&(d[i])) = hfa384x_getreg_noswap(hw, HFA384x_DATA0);
  304. }
  305. /* If len odd, handle last byte */
  306. if ( len % 2 ){
  307. reg = hfa384x_getreg_noswap(hw, HFA384x_DATA0);
  308. d[len-1] = ((UINT8*)(&reg))[0];
  309. }
  310. }
  311. if (result) {
  312. printf ( "copy_from_bap(%#hx, %#hx, %d) failed, result=%#hx\n", id, offset, len, result);
  313. }
  314. return result;
  315. }
  316. /*
  317. * Copy data from memory to BAP.
  318. *
  319. * Arguments:
  320. * hw device structure
  321. * id FID or RID, destined for the select register (host order)
  322. * offset An _even_ offset into the buffer for the given FID/RID.
  323. * buf ptr to array of bytes
  324. * len length of data to transfer in bytes
  325. * Returns:
  326. * 0 success
  327. */
  328. static int hfa384x_copy_to_bap(hfa384x_t *hw, UINT16 id, UINT16 offset,
  329. void *buf, UINT len)
  330. {
  331. int result = 0;
  332. UINT8 *d = (UINT8*)buf;
  333. UINT16 i;
  334. UINT16 savereg;
  335. /* Prepare BAP */
  336. result = hfa384x_prepare_bap ( hw, id, offset );
  337. if ( result == 0 ) {
  338. /* Write even(len) buf contents to data reg */
  339. for ( i = 0; i < (len & 0xfffe); i+=2 ) {
  340. hfa384x_setreg_noswap(hw, *(UINT16*)(&(d[i])), HFA384x_DATA0);
  341. }
  342. /* If len odd, handle last byte */
  343. if ( len % 2 ){
  344. savereg = hfa384x_getreg_noswap(hw, HFA384x_DATA0);
  345. result = hfa384x_prepare_bap ( hw, id, offset + (len & 0xfffe) );
  346. if ( result == 0 ) {
  347. ((UINT8*)(&savereg))[0] = d[len-1];
  348. hfa384x_setreg_noswap(hw, savereg, HFA384x_DATA0);
  349. }
  350. }
  351. }
  352. if (result) {
  353. printf ( "copy_to_bap(%#hx, %#hx, %d) failed, result=%#hx\n", id, offset, len, result);
  354. }
  355. return result;
  356. }
  357. /*
  358. * Request a given record to be copied to/from the record buffer.
  359. *
  360. * Arguments:
  361. * hw device structure
  362. * write [0|1] copy the record buffer to the given
  363. * configuration record. (host order)
  364. * rid RID of the record to read/write. (host order)
  365. *
  366. * Returns:
  367. * 0 success
  368. */
  369. static inline int hfa384x_cmd_access(hfa384x_t *hw, UINT16 write, UINT16 rid)
  370. {
  371. return hfa384x_docmd_wait(hw, HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_ACCESS) | HFA384x_CMD_WRITE_SET(write), rid, 0, 0);
  372. }
  373. /*
  374. * Performs the sequence necessary to read a config/info item.
  375. *
  376. * Arguments:
  377. * hw device structure
  378. * rid config/info record id (host order)
  379. * buf host side record buffer. Upon return it will
  380. * contain the body portion of the record (minus the
  381. * RID and len).
  382. * len buffer length (in bytes, should match record length)
  383. *
  384. * Returns:
  385. * 0 success
  386. */
  387. static int hfa384x_drvr_getconfig(hfa384x_t *hw, UINT16 rid, void *buf, UINT16 len)
  388. {
  389. int result = 0;
  390. hfa384x_rec_t rec;
  391. /* Request read of RID */
  392. result = hfa384x_cmd_access( hw, 0, rid);
  393. if ( result ) {
  394. printf("Call to hfa384x_cmd_access failed\n");
  395. return -1;
  396. }
  397. /* Copy out record length */
  398. result = hfa384x_copy_from_bap( hw, rid, 0, &rec, sizeof(rec));
  399. if ( result ) {
  400. return -1;
  401. }
  402. /* Validate the record length */
  403. if ( ((hfa384x2host_16(rec.reclen)-1)*2) != len ) { /* note body len calculation in bytes */
  404. printf ( "RID len mismatch, rid=%#hx hlen=%d fwlen=%d\n", rid, len, (hfa384x2host_16(rec.reclen)-1)*2);
  405. return -1;
  406. }
  407. /* Copy out record data */
  408. result = hfa384x_copy_from_bap( hw, rid, sizeof(rec), buf, len);
  409. return result;
  410. }
  411. /*
  412. * Performs the sequence necessary to read a 16/32 bit config/info item
  413. * and convert it to host order.
  414. *
  415. * Arguments:
  416. * hw device structure
  417. * rid config/info record id (in host order)
  418. * val ptr to 16/32 bit buffer to receive value (in host order)
  419. *
  420. * Returns:
  421. * 0 success
  422. */
  423. #if 0 /* Not actually used anywhere */
  424. static int hfa384x_drvr_getconfig16(hfa384x_t *hw, UINT16 rid, void *val)
  425. {
  426. int result = 0;
  427. result = hfa384x_drvr_getconfig(hw, rid, val, sizeof(UINT16));
  428. if ( result == 0 ) {
  429. *((UINT16*)val) = hfa384x2host_16(*((UINT16*)val));
  430. }
  431. return result;
  432. }
  433. #endif
  434. #if 0 /* Not actually used anywhere */
  435. static int hfa384x_drvr_getconfig32(hfa384x_t *hw, UINT16 rid, void *val)
  436. {
  437. int result = 0;
  438. result = hfa384x_drvr_getconfig(hw, rid, val, sizeof(UINT32));
  439. if ( result == 0 ) {
  440. *((UINT32*)val) = hfa384x2host_32(*((UINT32*)val));
  441. }
  442. return result;
  443. }
  444. #endif
  445. /*
  446. * Performs the sequence necessary to write a config/info item.
  447. *
  448. * Arguments:
  449. * hw device structure
  450. * rid config/info record id (in host order)
  451. * buf host side record buffer
  452. * len buffer length (in bytes)
  453. *
  454. * Returns:
  455. * 0 success
  456. */
  457. static int hfa384x_drvr_setconfig(hfa384x_t *hw, UINT16 rid, void *buf, UINT16 len)
  458. {
  459. int result = 0;
  460. hfa384x_rec_t rec;
  461. rec.rid = host2hfa384x_16(rid);
  462. rec.reclen = host2hfa384x_16((len/2) + 1); /* note conversion to words, +1 for rid field */
  463. /* write the record header */
  464. result = hfa384x_copy_to_bap( hw, rid, 0, &rec, sizeof(rec));
  465. if ( result ) {
  466. printf("Failure writing record header\n");
  467. return -1;
  468. }
  469. /* write the record data (if there is any) */
  470. if ( len > 0 ) {
  471. result = hfa384x_copy_to_bap( hw, rid, sizeof(rec), buf, len);
  472. if ( result ) {
  473. printf("Failure writing record data\n");
  474. return -1;
  475. }
  476. }
  477. /* Trigger setting of record */
  478. result = hfa384x_cmd_access( hw, 1, rid);
  479. return result;
  480. }
  481. /*
  482. * Performs the sequence necessary to write a 16/32 bit config/info item.
  483. *
  484. * Arguments:
  485. * hw device structure
  486. * rid config/info record id (in host order)
  487. * val 16/32 bit value to store (in host order)
  488. *
  489. * Returns:
  490. * 0 success
  491. */
  492. static int hfa384x_drvr_setconfig16(hfa384x_t *hw, UINT16 rid, UINT16 *val)
  493. {
  494. UINT16 value;
  495. value = host2hfa384x_16(*val);
  496. return hfa384x_drvr_setconfig(hw, rid, &value, sizeof(UINT16));
  497. }
  498. #if 0 /* Not actually used anywhere */
  499. static int hfa384x_drvr_setconfig32(hfa384x_t *hw, UINT16 rid, UINT32 *val)
  500. {
  501. UINT32 value;
  502. value = host2hfa384x_32(*val);
  503. return hfa384x_drvr_setconfig(hw, rid, &value, sizeof(UINT32));
  504. }
  505. #endif
  506. /*
  507. * Wait for an event, with specified checking interval and timeout.
  508. * Automatically acknolwedges events.
  509. *
  510. * Arguments:
  511. * hw device structure
  512. * event_mask EVSTAT register mask of events to wait for
  513. * event_ack EVACK register set of events to be acknowledged if they happen (can be
  514. * used to acknowledge "ignorable" events in addition to the "main" event)
  515. * wait Time (in us) to wait between each poll of the register
  516. * timeout Maximum number of polls before timing out
  517. * descr Descriptive text string of what is being waited for
  518. * (will be printed out if a timeout happens)
  519. *
  520. * Returns:
  521. * value of EVSTAT register, or 0 on failure
  522. */
  523. static int hfa384x_wait_for_event(hfa384x_t *hw, UINT16 event_mask, UINT16 event_ack, int wait, int timeout, const char *descr)
  524. {
  525. UINT16 reg;
  526. int count = 0;
  527. do {
  528. reg = hfa384x_getreg(hw, HFA384x_EVSTAT);
  529. if ( count > 0 ) udelay(wait);
  530. count++;
  531. } while ( !(reg & event_mask) && count < timeout);
  532. if ( count >= timeout ) {
  533. printf("hfa384x: Timed out waiting for %s\n", descr);
  534. return 0; /* Return failure */
  535. }
  536. /* Acknowledge all events that we were waiting on */
  537. hfa384x_setreg(hw, reg & ( event_mask | event_ack ), HFA384x_EVACK);
  538. return reg;
  539. }
  540. /**************************************************************************
  541. POLL - Wait for a frame
  542. ***************************************************************************/
  543. static int prism2_poll(struct nic *nic, int retrieve)
  544. {
  545. UINT16 reg;
  546. UINT16 rxfid;
  547. UINT16 result;
  548. hfa384x_rx_frame_t rxdesc;
  549. hfa384x_t *hw = &hw_global;
  550. /* Check for received packet */
  551. reg = hfa384x_getreg(hw, HFA384x_EVSTAT);
  552. if ( ! HFA384x_EVSTAT_ISRX(reg) ) {
  553. /* No packet received - return 0 */
  554. return 0;
  555. }
  556. if ( ! retrieve ) return 1;
  557. /* Acknowledge RX event */
  558. hfa384x_setreg(hw, HFA384x_EVACK_RX_SET(1), HFA384x_EVACK);
  559. /* Get RX FID */
  560. rxfid = hfa384x_getreg(hw, HFA384x_RXFID);
  561. /* Get the descriptor (including headers) */
  562. result = hfa384x_copy_from_bap(hw, rxfid, 0, &rxdesc, sizeof(rxdesc));
  563. if ( result ) {
  564. return 0; /* fail */
  565. }
  566. /* Byte order convert once up front. */
  567. rxdesc.status = hfa384x2host_16(rxdesc.status);
  568. rxdesc.time = hfa384x2host_32(rxdesc.time);
  569. rxdesc.data_len = hfa384x2host_16(rxdesc.data_len);
  570. /* Fill in nic->packetlen */
  571. nic->packetlen = rxdesc.data_len;
  572. if ( nic->packetlen > 0 ) {
  573. /* Fill in nic->packet */
  574. /*
  575. * NOTE: Packets as received have an 8-byte header (LLC+SNAP(?)) terminating with the packet type.
  576. * Etherboot expects a 14-byte header terminating with the packet type (it ignores the rest of the
  577. * header), so we use a quick hack to achieve this.
  578. */
  579. result = hfa384x_copy_from_bap(hw, rxfid, HFA384x_RX_DATA_OFF,
  580. nic->packet + ETH_HLEN - sizeof(wlan_80211hdr_t), nic->packetlen);
  581. if ( result ) {
  582. return 0; /* fail */
  583. }
  584. }
  585. return 1; /* Packet successfully received */
  586. }
  587. /**************************************************************************
  588. TRANSMIT - Transmit a frame
  589. ***************************************************************************/
  590. static void prism2_transmit(
  591. struct nic *nic,
  592. const char *d, /* Destination */
  593. unsigned int t, /* Type */
  594. unsigned int s, /* size */
  595. const char *p) /* Packet */
  596. {
  597. hfa384x_t *hw = &hw_global;
  598. hfa384x_tx_frame_t txdesc;
  599. wlan_80211hdr_t p80211hdr = { wlan_llc_snap, {{0,0,0},0} };
  600. UINT16 fid;
  601. UINT16 status;
  602. int result;
  603. // Request FID allocation
  604. result = hfa384x_docmd_wait(hw, HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_ALLOC), HFA384x_DRVR_TXBUF_MAX, 0, 0);
  605. if (result != 0) {
  606. printf("hfa384x: Tx FID allocate command failed: Aborting transmit..\n");
  607. return;
  608. }
  609. if ( !hfa384x_wait_for_event(hw, HFA384x_EVSTAT_ALLOC, HFA384x_EVACK_INFO, 10, 50, "Tx FID to be allocated\n" ) ) return;
  610. fid = hfa384x_getreg(hw, HFA384x_ALLOCFID);
  611. /* Build Tx frame structure */
  612. memset(&txdesc, 0, sizeof(txdesc));
  613. txdesc.tx_control = host2hfa384x_16( HFA384x_TX_MACPORT_SET(0) | HFA384x_TX_STRUCTYPE_SET(1) |
  614. HFA384x_TX_TXEX_SET(1) | HFA384x_TX_TXOK_SET(1) );
  615. txdesc.frame_control = host2ieee16( WLAN_SET_FC_FTYPE(WLAN_FTYPE_DATA) |
  616. WLAN_SET_FC_FSTYPE(WLAN_FSTYPE_DATAONLY) |
  617. WLAN_SET_FC_TODS(1) );
  618. memcpy(txdesc.address1, hw->bssid, WLAN_ADDR_LEN);
  619. memcpy(txdesc.address2, nic->node_addr, WLAN_ADDR_LEN);
  620. memcpy(txdesc.address3, d, WLAN_ADDR_LEN);
  621. txdesc.data_len = host2hfa384x_16( sizeof(txdesc) + sizeof(p80211hdr) + s );
  622. /* Set up SNAP header */
  623. /* Let OUI default to RFC1042 (0x000000) */
  624. p80211hdr.snap.type = htons(t);
  625. /* Copy txdesc, p80211hdr and payload parts to FID */
  626. result = hfa384x_copy_to_bap(hw, fid, 0, &txdesc, sizeof(txdesc));
  627. if ( result ) return; /* fail */
  628. result = hfa384x_copy_to_bap( hw, fid, sizeof(txdesc), &p80211hdr, sizeof(p80211hdr) );
  629. if ( result ) return; /* fail */
  630. result = hfa384x_copy_to_bap( hw, fid, sizeof(txdesc) + sizeof(p80211hdr), (UINT8*)p, s );
  631. if ( result ) return; /* fail */
  632. /* Issue Tx command */
  633. result = hfa384x_docmd_wait(hw, HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_TX), fid, 0, 0);
  634. if ( result != 0 ) {
  635. printf("hfa384x: Transmit failed with result %#hx.\n", result);
  636. return;
  637. }
  638. /* Wait for transmit completion (or exception) */
  639. result = hfa384x_wait_for_event(hw, HFA384x_EVSTAT_TXEXC | HFA384x_EVSTAT_TX, HFA384x_EVACK_INFO,
  640. 200, 500, "Tx to complete\n" );
  641. if ( !result ) return; /* timeout failure */
  642. if ( HFA384x_EVSTAT_ISTXEXC(result) ) {
  643. fid = hfa384x_getreg(hw, HFA384x_TXCOMPLFID);
  644. printf ( "Tx exception occurred with fid %#hx\n", fid );
  645. result = hfa384x_copy_from_bap(hw, fid, 0, &status, sizeof(status));
  646. if ( result ) return; /* fail */
  647. printf("hfa384x: Tx error occurred (status %#hx):\n", status);
  648. if ( HFA384x_TXSTATUS_ISACKERR(status) ) { printf(" ...acknowledgement error\n"); }
  649. if ( HFA384x_TXSTATUS_ISFORMERR(status) ) { printf(" ...format error\n"); }
  650. if ( HFA384x_TXSTATUS_ISDISCON(status) ) { printf(" ...disconnected error\n"); }
  651. if ( HFA384x_TXSTATUS_ISAGEDERR(status) ) { printf(" ...AGED error\n"); }
  652. if ( HFA384x_TXSTATUS_ISRETRYERR(status) ) { printf(" ...retry error\n"); }
  653. return; /* fail */
  654. }
  655. }
  656. /**************************************************************************
  657. DISABLE - Turn off ethernet interface
  658. ***************************************************************************/
  659. static void prism2_disable ( struct nic *nic __unused ) {
  660. /* put the card in its initial state */
  661. }
  662. /**************************************************************************
  663. IRQ - Enable, Disable, or Force interrupts
  664. ***************************************************************************/
  665. static void prism2_irq(struct nic *nic __unused, irq_action_t action __unused)
  666. {
  667. switch ( action ) {
  668. case DISABLE :
  669. break;
  670. case ENABLE :
  671. break;
  672. case FORCE :
  673. break;
  674. }
  675. }
  676. /**************************************************************************
  677. Operations table
  678. ***************************************************************************/
  679. static struct nic_operations prism2_operations = {
  680. .connect = dummy_connect,
  681. .poll = prism2_poll,
  682. .transmit = prism2_transmit,
  683. .irq = prism2_irq,
  684. };
  685. /**************************************************************************
  686. PROBE - Look for an adapter, this routine's visible to the outside
  687. You should omit the last argument struct pci_device * for a non-PCI NIC
  688. ***************************************************************************/
  689. static int prism2_probe ( struct nic *nic, hfa384x_t *hw ) {
  690. int result;
  691. UINT16 tmp16 = 0;
  692. UINT16 infofid;
  693. hfa384x_InfFrame_t inf;
  694. char ssid[HFA384x_RID_CNFDESIREDSSID_LEN];
  695. int info_count = 0;
  696. nic->irqno = 0;
  697. /* Initialize card */
  698. result = hfa384x_docmd_wait(hw, HFA384x_CMDCODE_INIT, 0,0,0); /* Send initialize command */
  699. if ( result ) printf ( "Initialize command returned %#hx\n", result );
  700. hfa384x_setreg(hw, 0, HFA384x_INTEN); /* Disable interrupts */
  701. hfa384x_setreg(hw, 0xffff, HFA384x_EVACK); /* Acknowledge any spurious events */
  702. DBG ( "MAC address %s\n", eth_ntoa ( nic->node_addr ) );
  703. /* Retrieve MAC address (and fill out nic->node_addr) */
  704. hfa384x_drvr_getconfig ( hw, HFA384x_RID_CNFOWNMACADDR, nic->node_addr, HFA384x_RID_CNFOWNMACADDR_LEN );
  705. /* Prepare card for autojoin */
  706. /* This procedure is reverse-engineered from a register-level trace of the Linux driver's join process */
  707. tmp16 = WLAN_DATA_MAXLEN; /* Set maximum data length */
  708. result = hfa384x_drvr_setconfig16(hw, HFA384x_RID_CNFMAXDATALEN, &tmp16);
  709. if ( result ) printf ( "Set Max Data Length command returned %#hx\n", result );
  710. tmp16 = 0x000f; /* Set transmit rate(?) */
  711. result = hfa384x_drvr_setconfig16(hw, HFA384x_RID_TXRATECNTL, &tmp16);
  712. if ( result ) printf ( "Set Transmit Rate command returned %#hx\n", result );
  713. tmp16 = HFA384x_CNFAUTHENTICATION_OPENSYSTEM; /* Set authentication type to OpenSystem */
  714. result = hfa384x_drvr_setconfig16(hw, HFA384x_RID_CNFAUTHENTICATION, &tmp16);
  715. if ( result ) printf ( "Set Authentication Type command returned %#hx\n", result );
  716. /* Set SSID */
  717. memset(ssid, 0, HFA384x_RID_CNFDESIREDSSID_LEN);
  718. for ( tmp16=0; tmp16<sizeof(hardcoded_ssid); tmp16++ ) { ssid[2+tmp16] = hardcoded_ssid[tmp16]; }
  719. ssid[0] = sizeof(hardcoded_ssid) - 1; /* Ignore terminating zero */
  720. result = hfa384x_drvr_setconfig(hw, HFA384x_RID_CNFDESIREDSSID, ssid, HFA384x_RID_CNFDESIREDSSID_LEN); /* Set the SSID */
  721. if ( result ) printf ( "Set SSID command returned %#hx\n", result );
  722. tmp16 = 1; /* Set port type to ESS port */
  723. result = hfa384x_drvr_setconfig16(hw, HFA384x_RID_CNFPORTTYPE, &tmp16);
  724. if ( result ) printf ( "Set port type command returned %#hx\n", result );
  725. /* Enable card */
  726. result = hfa384x_docmd_wait(hw, HFA384x_CMD_CMDCODE_SET(HFA384x_CMDCODE_ENABLE) | HFA384x_CMD_MACPORT_SET(0), 0,0,0);
  727. if ( result ) printf ( "Enable command returned %#hx\n", result );
  728. do {
  729. /* Increment info_count, abort if too many attempts.
  730. * See comment next to definition of MAX_JOIN_INFO_COUNT for explanation.
  731. */
  732. info_count++;
  733. if ( info_count > MAX_JOIN_INFO_COUNT ) {
  734. printf ( "Too many failed attempts - aborting\n" );
  735. return 0;
  736. }
  737. /* Wait for info frame to indicate link status */
  738. if ( sizeof(hardcoded_ssid) == 1 ) {
  739. /* Empty SSID => join to any SSID */
  740. printf ( "Attempting to autojoin to any available access point (attempt %d)...", info_count );
  741. } else {
  742. printf ( "Attempting to autojoin to SSID %s (attempt %d)...", &ssid[2], info_count );
  743. }
  744. if ( !hfa384x_wait_for_event(hw, HFA384x_EVSTAT_INFO, 0, 1000, 2000, "Info event" ) ) return 0;
  745. printf("done\n");
  746. infofid = hfa384x_getreg(hw, HFA384x_INFOFID);
  747. /* Retrieve the length */
  748. result = hfa384x_copy_from_bap( hw, infofid, 0, &inf.framelen, sizeof(UINT16));
  749. if ( result ) return 0; /* fail */
  750. inf.framelen = hfa384x2host_16(inf.framelen);
  751. /* Retrieve the rest */
  752. result = hfa384x_copy_from_bap( hw, infofid, sizeof(UINT16),
  753. &(inf.infotype), inf.framelen * sizeof(UINT16));
  754. if ( result ) return 0; /* fail */
  755. if ( inf.infotype != HFA384x_IT_LINKSTATUS ) {
  756. /* Not a Link Status info frame: die */
  757. printf ( "Unexpected info frame type %#hx (not LinkStatus type)\n", inf.infotype );
  758. return 0;
  759. }
  760. inf.info.linkstatus.linkstatus = hfa384x2host_16(inf.info.linkstatus.linkstatus);
  761. if ( inf.info.linkstatus.linkstatus != HFA384x_LINK_CONNECTED ) {
  762. /* Link not connected - retry */
  763. printf ( "Link not connected (status %#hx)\n", inf.info.linkstatus.linkstatus );
  764. }
  765. } while ( inf.info.linkstatus.linkstatus != HFA384x_LINK_CONNECTED );
  766. /* Retrieve BSSID and print Connected message */
  767. result = hfa384x_drvr_getconfig(hw, HFA384x_RID_CURRENTBSSID, hw->bssid, WLAN_BSSID_LEN);
  768. DBG ( "Link connected (BSSID %s - ", eth_ntoa ( hw->bssid ) );
  769. DBG ( " MAC address %s)\n", eth_ntoa (nic->node_addr ) );
  770. /* point to NIC specific routines */
  771. nic->nic_op = &prism2_operations;
  772. return 1;
  773. }