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 28KB

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