hostap_hw.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410
  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->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. struct net_device_stats *stats;
  1488. printk(KERN_DEBUG "%s: prism2_transmit: CMDCODE_TRANSMIT "
  1489. "failed (res=%d)\n", dev->name, res);
  1490. stats = hostap_get_stats(dev);
  1491. stats->tx_dropped++;
  1492. netif_wake_queue(dev);
  1493. return -1;
  1494. }
  1495. dev->trans_start = jiffies;
  1496. /* Since we did not wait for command completion, the card continues
  1497. * to process on the background and we will finish handling when
  1498. * command completion event is handled (prism2_cmd_ev() function) */
  1499. return 0;
  1500. }
  1501. /* Send IEEE 802.11 frame (convert the header into Prism2 TX descriptor and
  1502. * send the payload with this descriptor) */
  1503. /* Called only from software IRQ */
  1504. static int prism2_tx_80211(struct sk_buff *skb, struct net_device *dev)
  1505. {
  1506. struct hostap_interface *iface;
  1507. local_info_t *local;
  1508. struct hfa384x_tx_frame txdesc;
  1509. struct hostap_skb_tx_data *meta;
  1510. int hdr_len, data_len, idx, res, ret = -1;
  1511. u16 tx_control, fc;
  1512. iface = netdev_priv(dev);
  1513. local = iface->local;
  1514. meta = (struct hostap_skb_tx_data *) skb->cb;
  1515. prism2_callback(local, PRISM2_CALLBACK_TX_START);
  1516. if ((local->func->card_present && !local->func->card_present(local)) ||
  1517. !local->hw_ready || local->hw_downloading || local->pri_only) {
  1518. if (net_ratelimit()) {
  1519. printk(KERN_DEBUG "%s: prism2_tx_80211: hw not ready -"
  1520. " skipping\n", dev->name);
  1521. }
  1522. goto fail;
  1523. }
  1524. memset(&txdesc, 0, sizeof(txdesc));
  1525. /* skb->data starts with txdesc->frame_control */
  1526. hdr_len = 24;
  1527. skb_copy_from_linear_data(skb, &txdesc.frame_control, hdr_len);
  1528. fc = le16_to_cpu(txdesc.frame_control);
  1529. if (ieee80211_is_data(txdesc.frame_control) &&
  1530. ieee80211_has_a4(txdesc.frame_control) &&
  1531. skb->len >= 30) {
  1532. /* Addr4 */
  1533. skb_copy_from_linear_data_offset(skb, hdr_len, txdesc.addr4,
  1534. ETH_ALEN);
  1535. hdr_len += ETH_ALEN;
  1536. }
  1537. tx_control = local->tx_control;
  1538. if (meta->tx_cb_idx) {
  1539. tx_control |= HFA384X_TX_CTRL_TX_OK;
  1540. txdesc.sw_support = cpu_to_le32(meta->tx_cb_idx);
  1541. }
  1542. txdesc.tx_control = cpu_to_le16(tx_control);
  1543. txdesc.tx_rate = meta->rate;
  1544. data_len = skb->len - hdr_len;
  1545. txdesc.data_len = cpu_to_le16(data_len);
  1546. txdesc.len = cpu_to_be16(data_len);
  1547. idx = prism2_get_txfid_idx(local);
  1548. if (idx < 0)
  1549. goto fail;
  1550. if (local->frame_dump & PRISM2_DUMP_TX_HDR)
  1551. hostap_dump_tx_header(dev->name, &txdesc);
  1552. spin_lock(&local->baplock);
  1553. res = hfa384x_setup_bap(dev, BAP0, local->txfid[idx], 0);
  1554. if (!res)
  1555. res = hfa384x_to_bap(dev, BAP0, &txdesc, sizeof(txdesc));
  1556. if (!res)
  1557. res = hfa384x_to_bap(dev, BAP0, skb->data + hdr_len,
  1558. skb->len - hdr_len);
  1559. spin_unlock(&local->baplock);
  1560. if (!res)
  1561. res = prism2_transmit(dev, idx);
  1562. if (res) {
  1563. printk(KERN_DEBUG "%s: prism2_tx_80211 - to BAP0 failed\n",
  1564. dev->name);
  1565. local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
  1566. schedule_work(&local->reset_queue);
  1567. goto fail;
  1568. }
  1569. ret = 0;
  1570. fail:
  1571. prism2_callback(local, PRISM2_CALLBACK_TX_END);
  1572. return ret;
  1573. }
  1574. /* Some SMP systems have reported number of odd errors with hostap_pci. fid
  1575. * register has changed values between consecutive reads for an unknown reason.
  1576. * This should really not happen, so more debugging is needed. This test
  1577. * version is a big slower, but it will detect most of such register changes
  1578. * and will try to get the correct fid eventually. */
  1579. #define EXTRA_FID_READ_TESTS
  1580. static u16 prism2_read_fid_reg(struct net_device *dev, u16 reg)
  1581. {
  1582. #ifdef EXTRA_FID_READ_TESTS
  1583. u16 val, val2, val3;
  1584. int i;
  1585. for (i = 0; i < 10; i++) {
  1586. val = HFA384X_INW(reg);
  1587. val2 = HFA384X_INW(reg);
  1588. val3 = HFA384X_INW(reg);
  1589. if (val == val2 && val == val3)
  1590. return val;
  1591. printk(KERN_DEBUG "%s: detected fid change (try=%d, reg=%04x):"
  1592. " %04x %04x %04x\n",
  1593. dev->name, i, reg, val, val2, val3);
  1594. if ((val == val2 || val == val3) && val != 0)
  1595. return val;
  1596. if (val2 == val3 && val2 != 0)
  1597. return val2;
  1598. }
  1599. printk(KERN_WARNING "%s: Uhhuh.. could not read good fid from reg "
  1600. "%04x (%04x %04x %04x)\n", dev->name, reg, val, val2, val3);
  1601. return val;
  1602. #else /* EXTRA_FID_READ_TESTS */
  1603. return HFA384X_INW(reg);
  1604. #endif /* EXTRA_FID_READ_TESTS */
  1605. }
  1606. /* Called only as a tasklet (software IRQ) */
  1607. static void prism2_rx(local_info_t *local)
  1608. {
  1609. struct net_device *dev = local->dev;
  1610. int res, rx_pending = 0;
  1611. u16 len, hdr_len, rxfid, status, macport;
  1612. struct net_device_stats *stats;
  1613. struct hfa384x_rx_frame rxdesc;
  1614. struct sk_buff *skb = NULL;
  1615. prism2_callback(local, PRISM2_CALLBACK_RX_START);
  1616. stats = hostap_get_stats(dev);
  1617. rxfid = prism2_read_fid_reg(dev, HFA384X_RXFID_OFF);
  1618. #ifndef final_version
  1619. if (rxfid == 0) {
  1620. rxfid = HFA384X_INW(HFA384X_RXFID_OFF);
  1621. printk(KERN_DEBUG "prism2_rx: rxfid=0 (next 0x%04x)\n",
  1622. rxfid);
  1623. if (rxfid == 0) {
  1624. schedule_work(&local->reset_queue);
  1625. goto rx_dropped;
  1626. }
  1627. /* try to continue with the new rxfid value */
  1628. }
  1629. #endif
  1630. spin_lock(&local->baplock);
  1631. res = hfa384x_setup_bap(dev, BAP0, rxfid, 0);
  1632. if (!res)
  1633. res = hfa384x_from_bap(dev, BAP0, &rxdesc, sizeof(rxdesc));
  1634. if (res) {
  1635. spin_unlock(&local->baplock);
  1636. printk(KERN_DEBUG "%s: copy from BAP0 failed %d\n", dev->name,
  1637. res);
  1638. if (res == -ETIMEDOUT) {
  1639. schedule_work(&local->reset_queue);
  1640. }
  1641. goto rx_dropped;
  1642. }
  1643. len = le16_to_cpu(rxdesc.data_len);
  1644. hdr_len = sizeof(rxdesc);
  1645. status = le16_to_cpu(rxdesc.status);
  1646. macport = (status >> 8) & 0x07;
  1647. /* Drop frames with too large reported payload length. Monitor mode
  1648. * seems to sometimes pass frames (e.g., ctrl::ack) with signed and
  1649. * negative value, so allow also values 65522 .. 65534 (-14 .. -2) for
  1650. * macport 7 */
  1651. if (len > PRISM2_DATA_MAXLEN + 8 /* WEP */) {
  1652. if (macport == 7 && local->iw_mode == IW_MODE_MONITOR) {
  1653. if (len >= (u16) -14) {
  1654. hdr_len -= 65535 - len;
  1655. hdr_len--;
  1656. }
  1657. len = 0;
  1658. } else {
  1659. spin_unlock(&local->baplock);
  1660. printk(KERN_DEBUG "%s: Received frame with invalid "
  1661. "length 0x%04x\n", dev->name, len);
  1662. hostap_dump_rx_header(dev->name, &rxdesc);
  1663. goto rx_dropped;
  1664. }
  1665. }
  1666. skb = dev_alloc_skb(len + hdr_len);
  1667. if (!skb) {
  1668. spin_unlock(&local->baplock);
  1669. printk(KERN_DEBUG "%s: RX failed to allocate skb\n",
  1670. dev->name);
  1671. goto rx_dropped;
  1672. }
  1673. skb->dev = dev;
  1674. memcpy(skb_put(skb, hdr_len), &rxdesc, hdr_len);
  1675. if (len > 0)
  1676. res = hfa384x_from_bap(dev, BAP0, skb_put(skb, len), len);
  1677. spin_unlock(&local->baplock);
  1678. if (res) {
  1679. printk(KERN_DEBUG "%s: RX failed to read "
  1680. "frame data\n", dev->name);
  1681. goto rx_dropped;
  1682. }
  1683. skb_queue_tail(&local->rx_list, skb);
  1684. tasklet_schedule(&local->rx_tasklet);
  1685. rx_exit:
  1686. prism2_callback(local, PRISM2_CALLBACK_RX_END);
  1687. if (!rx_pending) {
  1688. HFA384X_OUTW(HFA384X_EV_RX, HFA384X_EVACK_OFF);
  1689. }
  1690. return;
  1691. rx_dropped:
  1692. stats->rx_dropped++;
  1693. if (skb)
  1694. dev_kfree_skb(skb);
  1695. goto rx_exit;
  1696. }
  1697. /* Called only as a tasklet (software IRQ) */
  1698. static void hostap_rx_skb(local_info_t *local, struct sk_buff *skb)
  1699. {
  1700. struct hfa384x_rx_frame *rxdesc;
  1701. struct net_device *dev = skb->dev;
  1702. struct hostap_80211_rx_status stats;
  1703. int hdrlen, rx_hdrlen;
  1704. rx_hdrlen = sizeof(*rxdesc);
  1705. if (skb->len < sizeof(*rxdesc)) {
  1706. /* Allow monitor mode to receive shorter frames */
  1707. if (local->iw_mode == IW_MODE_MONITOR &&
  1708. skb->len >= sizeof(*rxdesc) - 30) {
  1709. rx_hdrlen = skb->len;
  1710. } else {
  1711. dev_kfree_skb(skb);
  1712. return;
  1713. }
  1714. }
  1715. rxdesc = (struct hfa384x_rx_frame *) skb->data;
  1716. if (local->frame_dump & PRISM2_DUMP_RX_HDR &&
  1717. skb->len >= sizeof(*rxdesc))
  1718. hostap_dump_rx_header(dev->name, rxdesc);
  1719. if (le16_to_cpu(rxdesc->status) & HFA384X_RX_STATUS_FCSERR &&
  1720. (!local->monitor_allow_fcserr ||
  1721. local->iw_mode != IW_MODE_MONITOR))
  1722. goto drop;
  1723. if (skb->len > PRISM2_DATA_MAXLEN) {
  1724. printk(KERN_DEBUG "%s: RX: len(%d) > MAX(%d)\n",
  1725. dev->name, skb->len, PRISM2_DATA_MAXLEN);
  1726. goto drop;
  1727. }
  1728. stats.mac_time = le32_to_cpu(rxdesc->time);
  1729. stats.signal = rxdesc->signal - local->rssi_to_dBm;
  1730. stats.noise = rxdesc->silence - local->rssi_to_dBm;
  1731. stats.rate = rxdesc->rate;
  1732. /* Convert Prism2 RX structure into IEEE 802.11 header */
  1733. hdrlen = hostap_80211_get_hdrlen(rxdesc->frame_control);
  1734. if (hdrlen > rx_hdrlen)
  1735. hdrlen = rx_hdrlen;
  1736. memmove(skb_pull(skb, rx_hdrlen - hdrlen),
  1737. &rxdesc->frame_control, hdrlen);
  1738. hostap_80211_rx(dev, skb, &stats);
  1739. return;
  1740. drop:
  1741. dev_kfree_skb(skb);
  1742. }
  1743. /* Called only as a tasklet (software IRQ) */
  1744. static void hostap_rx_tasklet(unsigned long data)
  1745. {
  1746. local_info_t *local = (local_info_t *) data;
  1747. struct sk_buff *skb;
  1748. while ((skb = skb_dequeue(&local->rx_list)) != NULL)
  1749. hostap_rx_skb(local, skb);
  1750. }
  1751. /* Called only from hardware IRQ */
  1752. static void prism2_alloc_ev(struct net_device *dev)
  1753. {
  1754. struct hostap_interface *iface;
  1755. local_info_t *local;
  1756. int idx;
  1757. u16 fid;
  1758. iface = netdev_priv(dev);
  1759. local = iface->local;
  1760. fid = prism2_read_fid_reg(dev, HFA384X_ALLOCFID_OFF);
  1761. PDEBUG(DEBUG_FID, "FID: interrupt: ALLOC - fid=0x%04x\n", fid);
  1762. spin_lock(&local->txfidlock);
  1763. idx = local->next_alloc;
  1764. do {
  1765. if (local->txfid[idx] == fid) {
  1766. PDEBUG(DEBUG_FID, "FID: found matching txfid[%d]\n",
  1767. idx);
  1768. #ifndef final_version
  1769. if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY)
  1770. printk("Already released txfid found at idx "
  1771. "%d\n", idx);
  1772. if (local->intransmitfid[idx] == PRISM2_TXFID_RESERVED)
  1773. printk("Already reserved txfid found at idx "
  1774. "%d\n", idx);
  1775. #endif
  1776. local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
  1777. idx++;
  1778. local->next_alloc = idx >= PRISM2_TXFID_COUNT ? 0 :
  1779. idx;
  1780. if (!test_bit(HOSTAP_BITS_TRANSMIT, &local->bits) &&
  1781. netif_queue_stopped(dev))
  1782. netif_wake_queue(dev);
  1783. spin_unlock(&local->txfidlock);
  1784. return;
  1785. }
  1786. idx++;
  1787. if (idx >= PRISM2_TXFID_COUNT)
  1788. idx = 0;
  1789. } while (idx != local->next_alloc);
  1790. printk(KERN_WARNING "%s: could not find matching txfid (0x%04x, new "
  1791. "read 0x%04x) for alloc event\n", dev->name, fid,
  1792. HFA384X_INW(HFA384X_ALLOCFID_OFF));
  1793. printk(KERN_DEBUG "TXFIDs:");
  1794. for (idx = 0; idx < PRISM2_TXFID_COUNT; idx++)
  1795. printk(" %04x[%04x]", local->txfid[idx],
  1796. local->intransmitfid[idx]);
  1797. printk("\n");
  1798. spin_unlock(&local->txfidlock);
  1799. /* FIX: should probably schedule reset; reference to one txfid was lost
  1800. * completely.. Bad things will happen if we run out of txfids
  1801. * Actually, this will cause netdev watchdog to notice TX timeout and
  1802. * then card reset after all txfids have been leaked. */
  1803. }
  1804. /* Called only as a tasklet (software IRQ) */
  1805. static void hostap_tx_callback(local_info_t *local,
  1806. struct hfa384x_tx_frame *txdesc, int ok,
  1807. char *payload)
  1808. {
  1809. u16 sw_support, hdrlen, len;
  1810. struct sk_buff *skb;
  1811. struct hostap_tx_callback_info *cb;
  1812. /* Make sure that frame was from us. */
  1813. if (memcmp(txdesc->addr2, local->dev->dev_addr, ETH_ALEN)) {
  1814. printk(KERN_DEBUG "%s: TX callback - foreign frame\n",
  1815. local->dev->name);
  1816. return;
  1817. }
  1818. sw_support = le32_to_cpu(txdesc->sw_support);
  1819. spin_lock(&local->lock);
  1820. cb = local->tx_callback;
  1821. while (cb != NULL && cb->idx != sw_support)
  1822. cb = cb->next;
  1823. spin_unlock(&local->lock);
  1824. if (cb == NULL) {
  1825. printk(KERN_DEBUG "%s: could not find TX callback (idx %d)\n",
  1826. local->dev->name, sw_support);
  1827. return;
  1828. }
  1829. hdrlen = hostap_80211_get_hdrlen(txdesc->frame_control);
  1830. len = le16_to_cpu(txdesc->data_len);
  1831. skb = dev_alloc_skb(hdrlen + len);
  1832. if (skb == NULL) {
  1833. printk(KERN_DEBUG "%s: hostap_tx_callback failed to allocate "
  1834. "skb\n", local->dev->name);
  1835. return;
  1836. }
  1837. memcpy(skb_put(skb, hdrlen), (void *) &txdesc->frame_control, hdrlen);
  1838. if (payload)
  1839. memcpy(skb_put(skb, len), payload, len);
  1840. skb->dev = local->dev;
  1841. skb_reset_mac_header(skb);
  1842. cb->func(skb, ok, cb->data);
  1843. }
  1844. /* Called only as a tasklet (software IRQ) */
  1845. static int hostap_tx_compl_read(local_info_t *local, int error,
  1846. struct hfa384x_tx_frame *txdesc,
  1847. char **payload)
  1848. {
  1849. u16 fid, len;
  1850. int res, ret = 0;
  1851. struct net_device *dev = local->dev;
  1852. fid = prism2_read_fid_reg(dev, HFA384X_TXCOMPLFID_OFF);
  1853. PDEBUG(DEBUG_FID, "interrupt: TX (err=%d) - fid=0x%04x\n", fid, error);
  1854. spin_lock(&local->baplock);
  1855. res = hfa384x_setup_bap(dev, BAP0, fid, 0);
  1856. if (!res)
  1857. res = hfa384x_from_bap(dev, BAP0, txdesc, sizeof(*txdesc));
  1858. if (res) {
  1859. PDEBUG(DEBUG_EXTRA, "%s: TX (err=%d) - fid=0x%04x - could not "
  1860. "read txdesc\n", dev->name, error, fid);
  1861. if (res == -ETIMEDOUT) {
  1862. schedule_work(&local->reset_queue);
  1863. }
  1864. ret = -1;
  1865. goto fail;
  1866. }
  1867. if (txdesc->sw_support) {
  1868. len = le16_to_cpu(txdesc->data_len);
  1869. if (len < PRISM2_DATA_MAXLEN) {
  1870. *payload = kmalloc(len, GFP_ATOMIC);
  1871. if (*payload == NULL ||
  1872. hfa384x_from_bap(dev, BAP0, *payload, len)) {
  1873. PDEBUG(DEBUG_EXTRA, "%s: could not read TX "
  1874. "frame payload\n", dev->name);
  1875. kfree(*payload);
  1876. *payload = NULL;
  1877. ret = -1;
  1878. goto fail;
  1879. }
  1880. }
  1881. }
  1882. fail:
  1883. spin_unlock(&local->baplock);
  1884. return ret;
  1885. }
  1886. /* Called only as a tasklet (software IRQ) */
  1887. static void prism2_tx_ev(local_info_t *local)
  1888. {
  1889. struct net_device *dev = local->dev;
  1890. char *payload = NULL;
  1891. struct hfa384x_tx_frame txdesc;
  1892. if (hostap_tx_compl_read(local, 0, &txdesc, &payload))
  1893. goto fail;
  1894. if (local->frame_dump & PRISM2_DUMP_TX_HDR) {
  1895. PDEBUG(DEBUG_EXTRA, "%s: TX - status=0x%04x "
  1896. "retry_count=%d tx_rate=%d seq_ctrl=%d "
  1897. "duration_id=%d\n",
  1898. dev->name, le16_to_cpu(txdesc.status),
  1899. txdesc.retry_count, txdesc.tx_rate,
  1900. le16_to_cpu(txdesc.seq_ctrl),
  1901. le16_to_cpu(txdesc.duration_id));
  1902. }
  1903. if (txdesc.sw_support)
  1904. hostap_tx_callback(local, &txdesc, 1, payload);
  1905. kfree(payload);
  1906. fail:
  1907. HFA384X_OUTW(HFA384X_EV_TX, HFA384X_EVACK_OFF);
  1908. }
  1909. /* Called only as a tasklet (software IRQ) */
  1910. static void hostap_sta_tx_exc_tasklet(unsigned long data)
  1911. {
  1912. local_info_t *local = (local_info_t *) data;
  1913. struct sk_buff *skb;
  1914. while ((skb = skb_dequeue(&local->sta_tx_exc_list)) != NULL) {
  1915. struct hfa384x_tx_frame *txdesc =
  1916. (struct hfa384x_tx_frame *) skb->data;
  1917. if (skb->len >= sizeof(*txdesc)) {
  1918. /* Convert Prism2 RX structure into IEEE 802.11 header
  1919. */
  1920. int hdrlen = hostap_80211_get_hdrlen(txdesc->frame_control);
  1921. memmove(skb_pull(skb, sizeof(*txdesc) - hdrlen),
  1922. &txdesc->frame_control, hdrlen);
  1923. hostap_handle_sta_tx_exc(local, skb);
  1924. }
  1925. dev_kfree_skb(skb);
  1926. }
  1927. }
  1928. /* Called only as a tasklet (software IRQ) */
  1929. static void prism2_txexc(local_info_t *local)
  1930. {
  1931. struct net_device *dev = local->dev;
  1932. u16 status, fc;
  1933. int show_dump, res;
  1934. char *payload = NULL;
  1935. struct hfa384x_tx_frame txdesc;
  1936. show_dump = local->frame_dump & PRISM2_DUMP_TXEXC_HDR;
  1937. local->stats.tx_errors++;
  1938. res = hostap_tx_compl_read(local, 1, &txdesc, &payload);
  1939. HFA384X_OUTW(HFA384X_EV_TXEXC, HFA384X_EVACK_OFF);
  1940. if (res)
  1941. return;
  1942. status = le16_to_cpu(txdesc.status);
  1943. /* We produce a TXDROP event only for retry or lifetime
  1944. * exceeded, because that's the only status that really mean
  1945. * that this particular node went away.
  1946. * Other errors means that *we* screwed up. - Jean II */
  1947. if (status & (HFA384X_TX_STATUS_RETRYERR | HFA384X_TX_STATUS_AGEDERR))
  1948. {
  1949. union iwreq_data wrqu;
  1950. /* Copy 802.11 dest address. */
  1951. memcpy(wrqu.addr.sa_data, txdesc.addr1, ETH_ALEN);
  1952. wrqu.addr.sa_family = ARPHRD_ETHER;
  1953. wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
  1954. } else
  1955. show_dump = 1;
  1956. if (local->iw_mode == IW_MODE_MASTER ||
  1957. local->iw_mode == IW_MODE_REPEAT ||
  1958. local->wds_type & HOSTAP_WDS_AP_CLIENT) {
  1959. struct sk_buff *skb;
  1960. skb = dev_alloc_skb(sizeof(txdesc));
  1961. if (skb) {
  1962. memcpy(skb_put(skb, sizeof(txdesc)), &txdesc,
  1963. sizeof(txdesc));
  1964. skb_queue_tail(&local->sta_tx_exc_list, skb);
  1965. tasklet_schedule(&local->sta_tx_exc_tasklet);
  1966. }
  1967. }
  1968. if (txdesc.sw_support)
  1969. hostap_tx_callback(local, &txdesc, 0, payload);
  1970. kfree(payload);
  1971. if (!show_dump)
  1972. return;
  1973. PDEBUG(DEBUG_EXTRA, "%s: TXEXC - status=0x%04x (%s%s%s%s)"
  1974. " tx_control=%04x\n",
  1975. dev->name, status,
  1976. status & HFA384X_TX_STATUS_RETRYERR ? "[RetryErr]" : "",
  1977. status & HFA384X_TX_STATUS_AGEDERR ? "[AgedErr]" : "",
  1978. status & HFA384X_TX_STATUS_DISCON ? "[Discon]" : "",
  1979. status & HFA384X_TX_STATUS_FORMERR ? "[FormErr]" : "",
  1980. le16_to_cpu(txdesc.tx_control));
  1981. fc = le16_to_cpu(txdesc.frame_control);
  1982. PDEBUG(DEBUG_EXTRA, " retry_count=%d tx_rate=%d fc=0x%04x "
  1983. "(%s%s%s::%d%s%s)\n",
  1984. txdesc.retry_count, txdesc.tx_rate, fc,
  1985. ieee80211_is_mgmt(txdesc.frame_control) ? "Mgmt" : "",
  1986. ieee80211_is_ctl(txdesc.frame_control) ? "Ctrl" : "",
  1987. ieee80211_is_data(txdesc.frame_control) ? "Data" : "",
  1988. (fc & IEEE80211_FCTL_STYPE) >> 4,
  1989. ieee80211_has_tods(txdesc.frame_control) ? " ToDS" : "",
  1990. ieee80211_has_fromds(txdesc.frame_control) ? " FromDS" : "");
  1991. PDEBUG(DEBUG_EXTRA, " A1=%pM A2=%pM A3=%pM A4=%pM\n",
  1992. txdesc.addr1, txdesc.addr2,
  1993. txdesc.addr3, txdesc.addr4);
  1994. }
  1995. /* Called only as a tasklet (software IRQ) */
  1996. static void hostap_info_tasklet(unsigned long data)
  1997. {
  1998. local_info_t *local = (local_info_t *) data;
  1999. struct sk_buff *skb;
  2000. while ((skb = skb_dequeue(&local->info_list)) != NULL) {
  2001. hostap_info_process(local, skb);
  2002. dev_kfree_skb(skb);
  2003. }
  2004. }
  2005. /* Called only as a tasklet (software IRQ) */
  2006. static void prism2_info(local_info_t *local)
  2007. {
  2008. struct net_device *dev = local->dev;
  2009. u16 fid;
  2010. int res, left;
  2011. struct hfa384x_info_frame info;
  2012. struct sk_buff *skb;
  2013. fid = HFA384X_INW(HFA384X_INFOFID_OFF);
  2014. spin_lock(&local->baplock);
  2015. res = hfa384x_setup_bap(dev, BAP0, fid, 0);
  2016. if (!res)
  2017. res = hfa384x_from_bap(dev, BAP0, &info, sizeof(info));
  2018. if (res) {
  2019. spin_unlock(&local->baplock);
  2020. printk(KERN_DEBUG "Could not get info frame (fid=0x%04x)\n",
  2021. fid);
  2022. if (res == -ETIMEDOUT) {
  2023. schedule_work(&local->reset_queue);
  2024. }
  2025. goto out;
  2026. }
  2027. left = (le16_to_cpu(info.len) - 1) * 2;
  2028. if (info.len & cpu_to_le16(0x8000) || info.len == 0 || left > 2060) {
  2029. /* data register seems to give 0x8000 in some error cases even
  2030. * though busy bit is not set in offset register;
  2031. * in addition, length must be at least 1 due to type field */
  2032. spin_unlock(&local->baplock);
  2033. printk(KERN_DEBUG "%s: Received info frame with invalid "
  2034. "length 0x%04x (type 0x%04x)\n", dev->name,
  2035. le16_to_cpu(info.len), le16_to_cpu(info.type));
  2036. goto out;
  2037. }
  2038. skb = dev_alloc_skb(sizeof(info) + left);
  2039. if (skb == NULL) {
  2040. spin_unlock(&local->baplock);
  2041. printk(KERN_DEBUG "%s: Could not allocate skb for info "
  2042. "frame\n", dev->name);
  2043. goto out;
  2044. }
  2045. memcpy(skb_put(skb, sizeof(info)), &info, sizeof(info));
  2046. if (left > 0 && hfa384x_from_bap(dev, BAP0, skb_put(skb, left), left))
  2047. {
  2048. spin_unlock(&local->baplock);
  2049. printk(KERN_WARNING "%s: Info frame read failed (fid=0x%04x, "
  2050. "len=0x%04x, type=0x%04x\n", dev->name, fid,
  2051. le16_to_cpu(info.len), le16_to_cpu(info.type));
  2052. dev_kfree_skb(skb);
  2053. goto out;
  2054. }
  2055. spin_unlock(&local->baplock);
  2056. skb_queue_tail(&local->info_list, skb);
  2057. tasklet_schedule(&local->info_tasklet);
  2058. out:
  2059. HFA384X_OUTW(HFA384X_EV_INFO, HFA384X_EVACK_OFF);
  2060. }
  2061. /* Called only as a tasklet (software IRQ) */
  2062. static void hostap_bap_tasklet(unsigned long data)
  2063. {
  2064. local_info_t *local = (local_info_t *) data;
  2065. struct net_device *dev = local->dev;
  2066. u16 ev;
  2067. int frames = 30;
  2068. if (local->func->card_present && !local->func->card_present(local))
  2069. return;
  2070. set_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
  2071. /* Process all pending BAP events without generating new interrupts
  2072. * for them */
  2073. while (frames-- > 0) {
  2074. ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
  2075. if (ev == 0xffff || !(ev & HFA384X_BAP0_EVENTS))
  2076. break;
  2077. if (ev & HFA384X_EV_RX)
  2078. prism2_rx(local);
  2079. if (ev & HFA384X_EV_INFO)
  2080. prism2_info(local);
  2081. if (ev & HFA384X_EV_TX)
  2082. prism2_tx_ev(local);
  2083. if (ev & HFA384X_EV_TXEXC)
  2084. prism2_txexc(local);
  2085. }
  2086. set_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
  2087. clear_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
  2088. /* Enable interrupts for new BAP events */
  2089. hfa384x_events_all(dev);
  2090. clear_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
  2091. }
  2092. /* Called only from hardware IRQ */
  2093. static void prism2_infdrop(struct net_device *dev)
  2094. {
  2095. static unsigned long last_inquire = 0;
  2096. PDEBUG(DEBUG_EXTRA, "%s: INFDROP event\n", dev->name);
  2097. /* some firmware versions seem to get stuck with
  2098. * full CommTallies in high traffic load cases; every
  2099. * packet will then cause INFDROP event and CommTallies
  2100. * info frame will not be sent automatically. Try to
  2101. * get out of this state by inquiring CommTallies. */
  2102. if (!last_inquire || time_after(jiffies, last_inquire + HZ)) {
  2103. hfa384x_cmd_callback(dev, HFA384X_CMDCODE_INQUIRE,
  2104. HFA384X_INFO_COMMTALLIES, NULL, 0);
  2105. last_inquire = jiffies;
  2106. }
  2107. }
  2108. /* Called only from hardware IRQ */
  2109. static void prism2_ev_tick(struct net_device *dev)
  2110. {
  2111. struct hostap_interface *iface;
  2112. local_info_t *local;
  2113. u16 evstat, inten;
  2114. static int prev_stuck = 0;
  2115. iface = netdev_priv(dev);
  2116. local = iface->local;
  2117. if (time_after(jiffies, local->last_tick_timer + 5 * HZ) &&
  2118. local->last_tick_timer) {
  2119. evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
  2120. inten = HFA384X_INW(HFA384X_INTEN_OFF);
  2121. if (!prev_stuck) {
  2122. printk(KERN_INFO "%s: SW TICK stuck? "
  2123. "bits=0x%lx EvStat=%04x IntEn=%04x\n",
  2124. dev->name, local->bits, evstat, inten);
  2125. }
  2126. local->sw_tick_stuck++;
  2127. if ((evstat & HFA384X_BAP0_EVENTS) &&
  2128. (inten & HFA384X_BAP0_EVENTS)) {
  2129. printk(KERN_INFO "%s: trying to recover from IRQ "
  2130. "hang\n", dev->name);
  2131. hfa384x_events_no_bap0(dev);
  2132. }
  2133. prev_stuck = 1;
  2134. } else
  2135. prev_stuck = 0;
  2136. }
  2137. /* Called only from hardware IRQ */
  2138. static void prism2_check_magic(local_info_t *local)
  2139. {
  2140. /* at least PCI Prism2.5 with bus mastering seems to sometimes
  2141. * return 0x0000 in SWSUPPORT0 for unknown reason, but re-reading the
  2142. * register once or twice seems to get the correct value.. PCI cards
  2143. * cannot anyway be removed during normal operation, so there is not
  2144. * really any need for this verification with them. */
  2145. #ifndef PRISM2_PCI
  2146. #ifndef final_version
  2147. static unsigned long last_magic_err = 0;
  2148. struct net_device *dev = local->dev;
  2149. if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
  2150. if (!local->hw_ready)
  2151. return;
  2152. HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
  2153. if (time_after(jiffies, last_magic_err + 10 * HZ)) {
  2154. printk("%s: Interrupt, but SWSUPPORT0 does not match: "
  2155. "%04X != %04X - card removed?\n", dev->name,
  2156. HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
  2157. HFA384X_MAGIC);
  2158. last_magic_err = jiffies;
  2159. } else if (net_ratelimit()) {
  2160. printk(KERN_DEBUG "%s: interrupt - SWSUPPORT0=%04x "
  2161. "MAGIC=%04x\n", dev->name,
  2162. HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
  2163. HFA384X_MAGIC);
  2164. }
  2165. if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != 0xffff)
  2166. schedule_work(&local->reset_queue);
  2167. return;
  2168. }
  2169. #endif /* final_version */
  2170. #endif /* !PRISM2_PCI */
  2171. }
  2172. /* Called only from hardware IRQ */
  2173. static irqreturn_t prism2_interrupt(int irq, void *dev_id)
  2174. {
  2175. struct net_device *dev = dev_id;
  2176. struct hostap_interface *iface;
  2177. local_info_t *local;
  2178. int events = 0;
  2179. u16 ev;
  2180. iface = netdev_priv(dev);
  2181. local = iface->local;
  2182. prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 0);
  2183. if (local->func->card_present && !local->func->card_present(local)) {
  2184. if (net_ratelimit()) {
  2185. printk(KERN_DEBUG "%s: Interrupt, but dev not OK\n",
  2186. dev->name);
  2187. }
  2188. return IRQ_HANDLED;
  2189. }
  2190. prism2_check_magic(local);
  2191. for (;;) {
  2192. ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
  2193. if (ev == 0xffff) {
  2194. if (local->shutdown)
  2195. return IRQ_HANDLED;
  2196. HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
  2197. printk(KERN_DEBUG "%s: prism2_interrupt: ev=0xffff\n",
  2198. dev->name);
  2199. return IRQ_HANDLED;
  2200. }
  2201. ev &= HFA384X_INW(HFA384X_INTEN_OFF);
  2202. if (ev == 0)
  2203. break;
  2204. if (ev & HFA384X_EV_CMD) {
  2205. prism2_cmd_ev(dev);
  2206. }
  2207. /* Above events are needed even before hw is ready, but other
  2208. * events should be skipped during initialization. This may
  2209. * change for AllocEv if allocate_fid is implemented without
  2210. * busy waiting. */
  2211. if (!local->hw_ready || local->hw_resetting ||
  2212. !local->dev_enabled) {
  2213. ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
  2214. if (ev & HFA384X_EV_CMD)
  2215. goto next_event;
  2216. if ((ev & HFA384X_EVENT_MASK) == 0)
  2217. return IRQ_HANDLED;
  2218. if (local->dev_enabled && (ev & ~HFA384X_EV_TICK) &&
  2219. net_ratelimit()) {
  2220. printk(KERN_DEBUG "%s: prism2_interrupt: hw "
  2221. "not ready; skipping events 0x%04x "
  2222. "(IntEn=0x%04x)%s%s%s\n",
  2223. dev->name, ev,
  2224. HFA384X_INW(HFA384X_INTEN_OFF),
  2225. !local->hw_ready ? " (!hw_ready)" : "",
  2226. local->hw_resetting ?
  2227. " (hw_resetting)" : "",
  2228. !local->dev_enabled ?
  2229. " (!dev_enabled)" : "");
  2230. }
  2231. HFA384X_OUTW(ev, HFA384X_EVACK_OFF);
  2232. return IRQ_HANDLED;
  2233. }
  2234. if (ev & HFA384X_EV_TICK) {
  2235. prism2_ev_tick(dev);
  2236. HFA384X_OUTW(HFA384X_EV_TICK, HFA384X_EVACK_OFF);
  2237. }
  2238. if (ev & HFA384X_EV_ALLOC) {
  2239. prism2_alloc_ev(dev);
  2240. HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
  2241. }
  2242. /* Reading data from the card is quite time consuming, so do it
  2243. * in tasklets. TX, TXEXC, RX, and INFO events will be ACKed
  2244. * and unmasked after needed data has been read completely. */
  2245. if (ev & HFA384X_BAP0_EVENTS) {
  2246. hfa384x_events_no_bap0(dev);
  2247. tasklet_schedule(&local->bap_tasklet);
  2248. }
  2249. #ifndef final_version
  2250. if (ev & HFA384X_EV_WTERR) {
  2251. PDEBUG(DEBUG_EXTRA, "%s: WTERR event\n", dev->name);
  2252. HFA384X_OUTW(HFA384X_EV_WTERR, HFA384X_EVACK_OFF);
  2253. }
  2254. #endif /* final_version */
  2255. if (ev & HFA384X_EV_INFDROP) {
  2256. prism2_infdrop(dev);
  2257. HFA384X_OUTW(HFA384X_EV_INFDROP, HFA384X_EVACK_OFF);
  2258. }
  2259. next_event:
  2260. events++;
  2261. if (events >= PRISM2_MAX_INTERRUPT_EVENTS) {
  2262. PDEBUG(DEBUG_EXTRA, "prism2_interrupt: >%d events "
  2263. "(EvStat=0x%04x)\n",
  2264. PRISM2_MAX_INTERRUPT_EVENTS,
  2265. HFA384X_INW(HFA384X_EVSTAT_OFF));
  2266. break;
  2267. }
  2268. }
  2269. prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 1);
  2270. return IRQ_RETVAL(events);
  2271. }
  2272. static void prism2_check_sta_fw_version(local_info_t *local)
  2273. {
  2274. struct hfa384x_comp_ident comp;
  2275. int id, variant, major, minor;
  2276. if (hfa384x_get_rid(local->dev, HFA384X_RID_STAID,
  2277. &comp, sizeof(comp), 1) < 0)
  2278. return;
  2279. local->fw_ap = 0;
  2280. id = le16_to_cpu(comp.id);
  2281. if (id != HFA384X_COMP_ID_STA) {
  2282. if (id == HFA384X_COMP_ID_FW_AP)
  2283. local->fw_ap = 1;
  2284. return;
  2285. }
  2286. major = __le16_to_cpu(comp.major);
  2287. minor = __le16_to_cpu(comp.minor);
  2288. variant = __le16_to_cpu(comp.variant);
  2289. local->sta_fw_ver = PRISM2_FW_VER(major, minor, variant);
  2290. /* Station firmware versions before 1.4.x seem to have a bug in
  2291. * firmware-based WEP encryption when using Host AP mode, so use
  2292. * host_encrypt as a default for them. Firmware version 1.4.9 is the
  2293. * first one that has been seen to produce correct encryption, but the
  2294. * bug might be fixed before that (although, at least 1.4.2 is broken).
  2295. */
  2296. local->fw_encrypt_ok = local->sta_fw_ver >= PRISM2_FW_VER(1,4,9);
  2297. if (local->iw_mode == IW_MODE_MASTER && !local->host_encrypt &&
  2298. !local->fw_encrypt_ok) {
  2299. printk(KERN_DEBUG "%s: defaulting to host-based encryption as "
  2300. "a workaround for firmware bug in Host AP mode WEP\n",
  2301. local->dev->name);
  2302. local->host_encrypt = 1;
  2303. }
  2304. /* IEEE 802.11 standard compliant WDS frames (4 addresses) were broken
  2305. * in station firmware versions before 1.5.x. With these versions, the
  2306. * driver uses a workaround with bogus frame format (4th address after
  2307. * the payload). This is not compatible with other AP devices. Since
  2308. * the firmware bug is fixed in the latest station firmware versions,
  2309. * automatically enable standard compliant mode for cards using station
  2310. * firmware version 1.5.0 or newer. */
  2311. if (local->sta_fw_ver >= PRISM2_FW_VER(1,5,0))
  2312. local->wds_type |= HOSTAP_WDS_STANDARD_FRAME;
  2313. else {
  2314. printk(KERN_DEBUG "%s: defaulting to bogus WDS frame as a "
  2315. "workaround for firmware bug in Host AP mode WDS\n",
  2316. local->dev->name);
  2317. }
  2318. hostap_check_sta_fw_version(local->ap, local->sta_fw_ver);
  2319. }
  2320. static void hostap_passive_scan(unsigned long data)
  2321. {
  2322. local_info_t *local = (local_info_t *) data;
  2323. struct net_device *dev = local->dev;
  2324. u16 chan;
  2325. if (local->passive_scan_interval <= 0)
  2326. return;
  2327. if (local->passive_scan_state == PASSIVE_SCAN_LISTEN) {
  2328. int max_tries = 16;
  2329. /* Even though host system does not really know when the WLAN
  2330. * MAC is sending frames, try to avoid changing channels for
  2331. * passive scanning when a host-generated frame is being
  2332. * transmitted */
  2333. if (test_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
  2334. printk(KERN_DEBUG "%s: passive scan detected pending "
  2335. "TX - delaying\n", dev->name);
  2336. local->passive_scan_timer.expires = jiffies + HZ / 10;
  2337. add_timer(&local->passive_scan_timer);
  2338. return;
  2339. }
  2340. do {
  2341. local->passive_scan_channel++;
  2342. if (local->passive_scan_channel > 14)
  2343. local->passive_scan_channel = 1;
  2344. max_tries--;
  2345. } while (!(local->channel_mask &
  2346. (1 << (local->passive_scan_channel - 1))) &&
  2347. max_tries > 0);
  2348. if (max_tries == 0) {
  2349. printk(KERN_INFO "%s: no allowed passive scan channels"
  2350. " found\n", dev->name);
  2351. return;
  2352. }
  2353. printk(KERN_DEBUG "%s: passive scan channel %d\n",
  2354. dev->name, local->passive_scan_channel);
  2355. chan = local->passive_scan_channel;
  2356. local->passive_scan_state = PASSIVE_SCAN_WAIT;
  2357. local->passive_scan_timer.expires = jiffies + HZ / 10;
  2358. } else {
  2359. chan = local->channel;
  2360. local->passive_scan_state = PASSIVE_SCAN_LISTEN;
  2361. local->passive_scan_timer.expires = jiffies +
  2362. local->passive_scan_interval * HZ;
  2363. }
  2364. if (hfa384x_cmd_callback(dev, HFA384X_CMDCODE_TEST |
  2365. (HFA384X_TEST_CHANGE_CHANNEL << 8),
  2366. chan, NULL, 0))
  2367. printk(KERN_ERR "%s: passive scan channel set %d "
  2368. "failed\n", dev->name, chan);
  2369. add_timer(&local->passive_scan_timer);
  2370. }
  2371. /* Called only as a scheduled task when communications quality values should
  2372. * be updated. */
  2373. static void handle_comms_qual_update(struct work_struct *work)
  2374. {
  2375. local_info_t *local =
  2376. container_of(work, local_info_t, comms_qual_update);
  2377. prism2_update_comms_qual(local->dev);
  2378. }
  2379. /* Software watchdog - called as a timer. Hardware interrupt (Tick event) is
  2380. * used to monitor that local->last_tick_timer is being updated. If not,
  2381. * interrupt busy-loop is assumed and driver tries to recover by masking out
  2382. * some events. */
  2383. static void hostap_tick_timer(unsigned long data)
  2384. {
  2385. static unsigned long last_inquire = 0;
  2386. local_info_t *local = (local_info_t *) data;
  2387. local->last_tick_timer = jiffies;
  2388. /* Inquire CommTallies every 10 seconds to keep the statistics updated
  2389. * more often during low load and when using 32-bit tallies. */
  2390. if ((!last_inquire || time_after(jiffies, last_inquire + 10 * HZ)) &&
  2391. !local->hw_downloading && local->hw_ready &&
  2392. !local->hw_resetting && local->dev_enabled) {
  2393. hfa384x_cmd_callback(local->dev, HFA384X_CMDCODE_INQUIRE,
  2394. HFA384X_INFO_COMMTALLIES, NULL, 0);
  2395. last_inquire = jiffies;
  2396. }
  2397. if ((local->last_comms_qual_update == 0 ||
  2398. time_after(jiffies, local->last_comms_qual_update + 10 * HZ)) &&
  2399. (local->iw_mode == IW_MODE_INFRA ||
  2400. local->iw_mode == IW_MODE_ADHOC)) {
  2401. schedule_work(&local->comms_qual_update);
  2402. }
  2403. local->tick_timer.expires = jiffies + 2 * HZ;
  2404. add_timer(&local->tick_timer);
  2405. }
  2406. #ifndef PRISM2_NO_PROCFS_DEBUG
  2407. static int prism2_registers_proc_read(char *page, char **start, off_t off,
  2408. int count, int *eof, void *data)
  2409. {
  2410. char *p = page;
  2411. local_info_t *local = (local_info_t *) data;
  2412. if (off != 0) {
  2413. *eof = 1;
  2414. return 0;
  2415. }
  2416. #define SHOW_REG(n) \
  2417. p += sprintf(p, #n "=%04x\n", hfa384x_read_reg(local->dev, HFA384X_##n##_OFF))
  2418. SHOW_REG(CMD);
  2419. SHOW_REG(PARAM0);
  2420. SHOW_REG(PARAM1);
  2421. SHOW_REG(PARAM2);
  2422. SHOW_REG(STATUS);
  2423. SHOW_REG(RESP0);
  2424. SHOW_REG(RESP1);
  2425. SHOW_REG(RESP2);
  2426. SHOW_REG(INFOFID);
  2427. SHOW_REG(CONTROL);
  2428. SHOW_REG(SELECT0);
  2429. SHOW_REG(SELECT1);
  2430. SHOW_REG(OFFSET0);
  2431. SHOW_REG(OFFSET1);
  2432. SHOW_REG(RXFID);
  2433. SHOW_REG(ALLOCFID);
  2434. SHOW_REG(TXCOMPLFID);
  2435. SHOW_REG(SWSUPPORT0);
  2436. SHOW_REG(SWSUPPORT1);
  2437. SHOW_REG(SWSUPPORT2);
  2438. SHOW_REG(EVSTAT);
  2439. SHOW_REG(INTEN);
  2440. SHOW_REG(EVACK);
  2441. /* Do not read data registers, because they change the state of the
  2442. * MAC (offset += 2) */
  2443. /* SHOW_REG(DATA0); */
  2444. /* SHOW_REG(DATA1); */
  2445. SHOW_REG(AUXPAGE);
  2446. SHOW_REG(AUXOFFSET);
  2447. /* SHOW_REG(AUXDATA); */
  2448. #ifdef PRISM2_PCI
  2449. SHOW_REG(PCICOR);
  2450. SHOW_REG(PCIHCR);
  2451. SHOW_REG(PCI_M0_ADDRH);
  2452. SHOW_REG(PCI_M0_ADDRL);
  2453. SHOW_REG(PCI_M0_LEN);
  2454. SHOW_REG(PCI_M0_CTL);
  2455. SHOW_REG(PCI_STATUS);
  2456. SHOW_REG(PCI_M1_ADDRH);
  2457. SHOW_REG(PCI_M1_ADDRL);
  2458. SHOW_REG(PCI_M1_LEN);
  2459. SHOW_REG(PCI_M1_CTL);
  2460. #endif /* PRISM2_PCI */
  2461. return (p - page);
  2462. }
  2463. #endif /* PRISM2_NO_PROCFS_DEBUG */
  2464. struct set_tim_data {
  2465. struct list_head list;
  2466. int aid;
  2467. int set;
  2468. };
  2469. static int prism2_set_tim(struct net_device *dev, int aid, int set)
  2470. {
  2471. struct list_head *ptr;
  2472. struct set_tim_data *new_entry;
  2473. struct hostap_interface *iface;
  2474. local_info_t *local;
  2475. iface = netdev_priv(dev);
  2476. local = iface->local;
  2477. new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
  2478. if (new_entry == NULL) {
  2479. printk(KERN_DEBUG "%s: prism2_set_tim: kmalloc failed\n",
  2480. local->dev->name);
  2481. return -ENOMEM;
  2482. }
  2483. new_entry->aid = aid;
  2484. new_entry->set = set;
  2485. spin_lock_bh(&local->set_tim_lock);
  2486. list_for_each(ptr, &local->set_tim_list) {
  2487. struct set_tim_data *entry =
  2488. list_entry(ptr, struct set_tim_data, list);
  2489. if (entry->aid == aid) {
  2490. PDEBUG(DEBUG_PS2, "%s: prism2_set_tim: aid=%d "
  2491. "set=%d ==> %d\n",
  2492. local->dev->name, aid, entry->set, set);
  2493. entry->set = set;
  2494. kfree(new_entry);
  2495. new_entry = NULL;
  2496. break;
  2497. }
  2498. }
  2499. if (new_entry)
  2500. list_add_tail(&new_entry->list, &local->set_tim_list);
  2501. spin_unlock_bh(&local->set_tim_lock);
  2502. schedule_work(&local->set_tim_queue);
  2503. return 0;
  2504. }
  2505. static void handle_set_tim_queue(struct work_struct *work)
  2506. {
  2507. local_info_t *local = container_of(work, local_info_t, set_tim_queue);
  2508. struct set_tim_data *entry;
  2509. u16 val;
  2510. for (;;) {
  2511. entry = NULL;
  2512. spin_lock_bh(&local->set_tim_lock);
  2513. if (!list_empty(&local->set_tim_list)) {
  2514. entry = list_entry(local->set_tim_list.next,
  2515. struct set_tim_data, list);
  2516. list_del(&entry->list);
  2517. }
  2518. spin_unlock_bh(&local->set_tim_lock);
  2519. if (!entry)
  2520. break;
  2521. PDEBUG(DEBUG_PS2, "%s: handle_set_tim_queue: aid=%d set=%d\n",
  2522. local->dev->name, entry->aid, entry->set);
  2523. val = entry->aid;
  2524. if (entry->set)
  2525. val |= 0x8000;
  2526. if (hostap_set_word(local->dev, HFA384X_RID_CNFTIMCTRL, val)) {
  2527. printk(KERN_DEBUG "%s: set_tim failed (aid=%d "
  2528. "set=%d)\n",
  2529. local->dev->name, entry->aid, entry->set);
  2530. }
  2531. kfree(entry);
  2532. }
  2533. }
  2534. static void prism2_clear_set_tim_queue(local_info_t *local)
  2535. {
  2536. struct list_head *ptr, *n;
  2537. list_for_each_safe(ptr, n, &local->set_tim_list) {
  2538. struct set_tim_data *entry;
  2539. entry = list_entry(ptr, struct set_tim_data, list);
  2540. list_del(&entry->list);
  2541. kfree(entry);
  2542. }
  2543. }
  2544. /*
  2545. * HostAP uses two layers of net devices, where the inner
  2546. * layer gets called all the time from the outer layer.
  2547. * This is a natural nesting, which needs a split lock type.
  2548. */
  2549. static struct lock_class_key hostap_netdev_xmit_lock_key;
  2550. static struct lock_class_key hostap_netdev_addr_lock_key;
  2551. static void prism2_set_lockdep_class_one(struct net_device *dev,
  2552. struct netdev_queue *txq,
  2553. void *_unused)
  2554. {
  2555. lockdep_set_class(&txq->_xmit_lock,
  2556. &hostap_netdev_xmit_lock_key);
  2557. }
  2558. static void prism2_set_lockdep_class(struct net_device *dev)
  2559. {
  2560. lockdep_set_class(&dev->addr_list_lock,
  2561. &hostap_netdev_addr_lock_key);
  2562. netdev_for_each_tx_queue(dev, prism2_set_lockdep_class_one, NULL);
  2563. }
  2564. static struct net_device *
  2565. prism2_init_local_data(struct prism2_helper_functions *funcs, int card_idx,
  2566. struct device *sdev)
  2567. {
  2568. struct net_device *dev;
  2569. struct hostap_interface *iface;
  2570. struct local_info *local;
  2571. int len, i, ret;
  2572. if (funcs == NULL)
  2573. return NULL;
  2574. len = strlen(dev_template);
  2575. if (len >= IFNAMSIZ || strstr(dev_template, "%d") == NULL) {
  2576. printk(KERN_WARNING "hostap: Invalid dev_template='%s'\n",
  2577. dev_template);
  2578. return NULL;
  2579. }
  2580. len = sizeof(struct hostap_interface) +
  2581. 3 + sizeof(struct local_info) +
  2582. 3 + sizeof(struct ap_data);
  2583. dev = alloc_etherdev(len);
  2584. if (dev == NULL)
  2585. return NULL;
  2586. iface = netdev_priv(dev);
  2587. local = (struct local_info *) ((((long) (iface + 1)) + 3) & ~3);
  2588. local->ap = (struct ap_data *) ((((long) (local + 1)) + 3) & ~3);
  2589. local->dev = iface->dev = dev;
  2590. iface->local = local;
  2591. iface->type = HOSTAP_INTERFACE_MASTER;
  2592. INIT_LIST_HEAD(&local->hostap_interfaces);
  2593. local->hw_module = THIS_MODULE;
  2594. #ifdef PRISM2_IO_DEBUG
  2595. local->io_debug_enabled = 1;
  2596. #endif /* PRISM2_IO_DEBUG */
  2597. local->func = funcs;
  2598. local->func->cmd = hfa384x_cmd;
  2599. local->func->read_regs = hfa384x_read_regs;
  2600. local->func->get_rid = hfa384x_get_rid;
  2601. local->func->set_rid = hfa384x_set_rid;
  2602. local->func->hw_enable = prism2_hw_enable;
  2603. local->func->hw_config = prism2_hw_config;
  2604. local->func->hw_reset = prism2_hw_reset;
  2605. local->func->hw_shutdown = prism2_hw_shutdown;
  2606. local->func->reset_port = prism2_reset_port;
  2607. local->func->schedule_reset = prism2_schedule_reset;
  2608. #ifdef PRISM2_DOWNLOAD_SUPPORT
  2609. local->func->read_aux = prism2_download_aux_dump;
  2610. local->func->download = prism2_download;
  2611. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  2612. local->func->tx = prism2_tx_80211;
  2613. local->func->set_tim = prism2_set_tim;
  2614. local->func->need_tx_headroom = 0; /* no need to add txdesc in
  2615. * skb->data (FIX: maybe for DMA bus
  2616. * mastering? */
  2617. local->mtu = mtu;
  2618. rwlock_init(&local->iface_lock);
  2619. spin_lock_init(&local->txfidlock);
  2620. spin_lock_init(&local->cmdlock);
  2621. spin_lock_init(&local->baplock);
  2622. spin_lock_init(&local->lock);
  2623. mutex_init(&local->rid_bap_mtx);
  2624. if (card_idx < 0 || card_idx >= MAX_PARM_DEVICES)
  2625. card_idx = 0;
  2626. local->card_idx = card_idx;
  2627. len = strlen(essid);
  2628. memcpy(local->essid, essid,
  2629. len > MAX_SSID_LEN ? MAX_SSID_LEN : len);
  2630. local->essid[MAX_SSID_LEN] = '\0';
  2631. i = GET_INT_PARM(iw_mode, card_idx);
  2632. if ((i >= IW_MODE_ADHOC && i <= IW_MODE_REPEAT) ||
  2633. i == IW_MODE_MONITOR) {
  2634. local->iw_mode = i;
  2635. } else {
  2636. printk(KERN_WARNING "prism2: Unknown iw_mode %d; using "
  2637. "IW_MODE_MASTER\n", i);
  2638. local->iw_mode = IW_MODE_MASTER;
  2639. }
  2640. local->channel = GET_INT_PARM(channel, card_idx);
  2641. local->beacon_int = GET_INT_PARM(beacon_int, card_idx);
  2642. local->dtim_period = GET_INT_PARM(dtim_period, card_idx);
  2643. local->wds_max_connections = 16;
  2644. local->tx_control = HFA384X_TX_CTRL_FLAGS;
  2645. local->manual_retry_count = -1;
  2646. local->rts_threshold = 2347;
  2647. local->fragm_threshold = 2346;
  2648. local->rssi_to_dBm = 100; /* default; to be overriden by
  2649. * cnfDbmAdjust, if available */
  2650. local->auth_algs = PRISM2_AUTH_OPEN | PRISM2_AUTH_SHARED_KEY;
  2651. local->sram_type = -1;
  2652. local->scan_channel_mask = 0xffff;
  2653. local->monitor_type = PRISM2_MONITOR_RADIOTAP;
  2654. /* Initialize task queue structures */
  2655. INIT_WORK(&local->reset_queue, handle_reset_queue);
  2656. INIT_WORK(&local->set_multicast_list_queue,
  2657. hostap_set_multicast_list_queue);
  2658. INIT_WORK(&local->set_tim_queue, handle_set_tim_queue);
  2659. INIT_LIST_HEAD(&local->set_tim_list);
  2660. spin_lock_init(&local->set_tim_lock);
  2661. INIT_WORK(&local->comms_qual_update, handle_comms_qual_update);
  2662. /* Initialize tasklets for handling hardware IRQ related operations
  2663. * outside hw IRQ handler */
  2664. #define HOSTAP_TASKLET_INIT(q, f, d) \
  2665. do { memset((q), 0, sizeof(*(q))); (q)->func = (f); (q)->data = (d); } \
  2666. while (0)
  2667. HOSTAP_TASKLET_INIT(&local->bap_tasklet, hostap_bap_tasklet,
  2668. (unsigned long) local);
  2669. HOSTAP_TASKLET_INIT(&local->info_tasklet, hostap_info_tasklet,
  2670. (unsigned long) local);
  2671. hostap_info_init(local);
  2672. HOSTAP_TASKLET_INIT(&local->rx_tasklet,
  2673. hostap_rx_tasklet, (unsigned long) local);
  2674. skb_queue_head_init(&local->rx_list);
  2675. HOSTAP_TASKLET_INIT(&local->sta_tx_exc_tasklet,
  2676. hostap_sta_tx_exc_tasklet, (unsigned long) local);
  2677. skb_queue_head_init(&local->sta_tx_exc_list);
  2678. INIT_LIST_HEAD(&local->cmd_queue);
  2679. init_waitqueue_head(&local->hostscan_wq);
  2680. lib80211_crypt_info_init(&local->crypt_info, dev->name, &local->lock);
  2681. init_timer(&local->passive_scan_timer);
  2682. local->passive_scan_timer.data = (unsigned long) local;
  2683. local->passive_scan_timer.function = hostap_passive_scan;
  2684. init_timer(&local->tick_timer);
  2685. local->tick_timer.data = (unsigned long) local;
  2686. local->tick_timer.function = hostap_tick_timer;
  2687. local->tick_timer.expires = jiffies + 2 * HZ;
  2688. add_timer(&local->tick_timer);
  2689. INIT_LIST_HEAD(&local->bss_list);
  2690. hostap_setup_dev(dev, local, HOSTAP_INTERFACE_MASTER);
  2691. dev->hard_start_xmit = hostap_master_start_xmit;
  2692. dev->type = ARPHRD_IEEE80211;
  2693. dev->header_ops = &hostap_80211_ops;
  2694. rtnl_lock();
  2695. ret = dev_alloc_name(dev, "wifi%d");
  2696. SET_NETDEV_DEV(dev, sdev);
  2697. if (ret >= 0)
  2698. ret = register_netdevice(dev);
  2699. prism2_set_lockdep_class(dev);
  2700. rtnl_unlock();
  2701. if (ret < 0) {
  2702. printk(KERN_WARNING "%s: register netdevice failed!\n",
  2703. dev_info);
  2704. goto fail;
  2705. }
  2706. printk(KERN_INFO "%s: Registered netdevice %s\n", dev_info, dev->name);
  2707. hostap_init_data(local);
  2708. return dev;
  2709. fail:
  2710. free_netdev(dev);
  2711. return NULL;
  2712. }
  2713. static int hostap_hw_ready(struct net_device *dev)
  2714. {
  2715. struct hostap_interface *iface;
  2716. struct local_info *local;
  2717. iface = netdev_priv(dev);
  2718. local = iface->local;
  2719. local->ddev = hostap_add_interface(local, HOSTAP_INTERFACE_MAIN, 0,
  2720. "", dev_template);
  2721. if (local->ddev) {
  2722. if (local->iw_mode == IW_MODE_INFRA ||
  2723. local->iw_mode == IW_MODE_ADHOC) {
  2724. netif_carrier_off(local->dev);
  2725. netif_carrier_off(local->ddev);
  2726. }
  2727. hostap_init_proc(local);
  2728. #ifndef PRISM2_NO_PROCFS_DEBUG
  2729. create_proc_read_entry("registers", 0, local->proc,
  2730. prism2_registers_proc_read, local);
  2731. #endif /* PRISM2_NO_PROCFS_DEBUG */
  2732. hostap_init_ap_proc(local);
  2733. return 0;
  2734. }
  2735. return -1;
  2736. }
  2737. static void prism2_free_local_data(struct net_device *dev)
  2738. {
  2739. struct hostap_tx_callback_info *tx_cb, *tx_cb_prev;
  2740. int i;
  2741. struct hostap_interface *iface;
  2742. struct local_info *local;
  2743. struct list_head *ptr, *n;
  2744. if (dev == NULL)
  2745. return;
  2746. iface = netdev_priv(dev);
  2747. local = iface->local;
  2748. /* Unregister all netdevs before freeing local data. */
  2749. list_for_each_safe(ptr, n, &local->hostap_interfaces) {
  2750. iface = list_entry(ptr, struct hostap_interface, list);
  2751. if (iface->type == HOSTAP_INTERFACE_MASTER) {
  2752. /* special handling for this interface below */
  2753. continue;
  2754. }
  2755. hostap_remove_interface(iface->dev, 0, 1);
  2756. }
  2757. unregister_netdev(local->dev);
  2758. flush_scheduled_work();
  2759. lib80211_crypt_info_free(&local->crypt_info);
  2760. if (timer_pending(&local->passive_scan_timer))
  2761. del_timer(&local->passive_scan_timer);
  2762. if (timer_pending(&local->tick_timer))
  2763. del_timer(&local->tick_timer);
  2764. prism2_clear_cmd_queue(local);
  2765. skb_queue_purge(&local->info_list);
  2766. skb_queue_purge(&local->rx_list);
  2767. skb_queue_purge(&local->sta_tx_exc_list);
  2768. if (local->dev_enabled)
  2769. prism2_callback(local, PRISM2_CALLBACK_DISABLE);
  2770. if (local->ap != NULL)
  2771. hostap_free_data(local->ap);
  2772. #ifndef PRISM2_NO_PROCFS_DEBUG
  2773. if (local->proc != NULL)
  2774. remove_proc_entry("registers", local->proc);
  2775. #endif /* PRISM2_NO_PROCFS_DEBUG */
  2776. hostap_remove_proc(local);
  2777. tx_cb = local->tx_callback;
  2778. while (tx_cb != NULL) {
  2779. tx_cb_prev = tx_cb;
  2780. tx_cb = tx_cb->next;
  2781. kfree(tx_cb_prev);
  2782. }
  2783. hostap_set_hostapd(local, 0, 0);
  2784. hostap_set_hostapd_sta(local, 0, 0);
  2785. for (i = 0; i < PRISM2_FRAG_CACHE_LEN; i++) {
  2786. if (local->frag_cache[i].skb != NULL)
  2787. dev_kfree_skb(local->frag_cache[i].skb);
  2788. }
  2789. #ifdef PRISM2_DOWNLOAD_SUPPORT
  2790. prism2_download_free_data(local->dl_pri);
  2791. prism2_download_free_data(local->dl_sec);
  2792. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  2793. prism2_clear_set_tim_queue(local);
  2794. list_for_each_safe(ptr, n, &local->bss_list) {
  2795. struct hostap_bss_info *bss =
  2796. list_entry(ptr, struct hostap_bss_info, list);
  2797. kfree(bss);
  2798. }
  2799. kfree(local->pda);
  2800. kfree(local->last_scan_results);
  2801. kfree(local->generic_elem);
  2802. free_netdev(local->dev);
  2803. }
  2804. #if (defined(PRISM2_PCI) && defined(CONFIG_PM)) || defined(PRISM2_PCCARD)
  2805. static void prism2_suspend(struct net_device *dev)
  2806. {
  2807. struct hostap_interface *iface;
  2808. struct local_info *local;
  2809. union iwreq_data wrqu;
  2810. iface = netdev_priv(dev);
  2811. local = iface->local;
  2812. /* Send disconnect event, e.g., to trigger reassociation after resume
  2813. * if wpa_supplicant is used. */
  2814. memset(&wrqu, 0, sizeof(wrqu));
  2815. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  2816. wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
  2817. /* Disable hardware and firmware */
  2818. prism2_hw_shutdown(dev, 0);
  2819. }
  2820. #endif /* (PRISM2_PCI && CONFIG_PM) || PRISM2_PCCARD */
  2821. /* These might at some point be compiled separately and used as separate
  2822. * kernel modules or linked into one */
  2823. #ifdef PRISM2_DOWNLOAD_SUPPORT
  2824. #include "hostap_download.c"
  2825. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  2826. #ifdef PRISM2_CALLBACK
  2827. /* External hostap_callback.c file can be used to, e.g., blink activity led.
  2828. * This can use platform specific code and must define prism2_callback()
  2829. * function (if PRISM2_CALLBACK is not defined, these function calls are not
  2830. * used. */
  2831. #include "hostap_callback.c"
  2832. #endif /* PRISM2_CALLBACK */