hostap_hw.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408
  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 <linux/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/seq_file.h>
  38. #include <linux/if_arp.h>
  39. #include <linux/delay.h>
  40. #include <linux/random.h>
  41. #include <linux/wait.h>
  42. #include <linux/sched/signal.h>
  43. #include <linux/rtnetlink.h>
  44. #include <linux/wireless.h>
  45. #include <net/iw_handler.h>
  46. #include <net/lib80211.h>
  47. #include <asm/irq.h>
  48. #include "hostap_80211.h"
  49. #include "hostap.h"
  50. #include "hostap_ap.h"
  51. /* #define final_version */
  52. static int mtu = 1500;
  53. module_param(mtu, int, 0444);
  54. MODULE_PARM_DESC(mtu, "Maximum transfer unit");
  55. static int channel[MAX_PARM_DEVICES] = { 3, DEF_INTS };
  56. module_param_array(channel, int, NULL, 0444);
  57. MODULE_PARM_DESC(channel, "Initial channel");
  58. static char essid[33] = "test";
  59. module_param_string(essid, essid, sizeof(essid), 0444);
  60. MODULE_PARM_DESC(essid, "Host AP's ESSID");
  61. static int iw_mode[MAX_PARM_DEVICES] = { IW_MODE_MASTER, DEF_INTS };
  62. module_param_array(iw_mode, int, NULL, 0444);
  63. MODULE_PARM_DESC(iw_mode, "Initial operation mode");
  64. static int beacon_int[MAX_PARM_DEVICES] = { 100, DEF_INTS };
  65. module_param_array(beacon_int, int, NULL, 0444);
  66. MODULE_PARM_DESC(beacon_int, "Beacon interval (1 = 1024 usec)");
  67. static int dtim_period[MAX_PARM_DEVICES] = { 1, DEF_INTS };
  68. module_param_array(dtim_period, int, NULL, 0444);
  69. MODULE_PARM_DESC(dtim_period, "DTIM period");
  70. static char dev_template[16] = "wlan%d";
  71. module_param_string(dev_template, dev_template, sizeof(dev_template), 0444);
  72. MODULE_PARM_DESC(dev_template, "Prefix for network device name (default: "
  73. "wlan%d)");
  74. #ifdef final_version
  75. #define EXTRA_EVENTS_WTERR 0
  76. #else
  77. /* check WTERR events (Wait Time-out) in development versions */
  78. #define EXTRA_EVENTS_WTERR HFA384X_EV_WTERR
  79. #endif
  80. /* Events that will be using BAP0 */
  81. #define HFA384X_BAP0_EVENTS \
  82. (HFA384X_EV_TXEXC | HFA384X_EV_RX | HFA384X_EV_INFO | HFA384X_EV_TX)
  83. /* event mask, i.e., events that will result in an interrupt */
  84. #define HFA384X_EVENT_MASK \
  85. (HFA384X_BAP0_EVENTS | HFA384X_EV_ALLOC | HFA384X_EV_INFDROP | \
  86. HFA384X_EV_CMD | HFA384X_EV_TICK | \
  87. EXTRA_EVENTS_WTERR)
  88. /* Default TX control flags: use 802.11 headers and request interrupt for
  89. * failed transmits. Frames that request ACK callback, will add
  90. * _TX_OK flag and _ALT_RTRY flag may be used to select different retry policy.
  91. */
  92. #define HFA384X_TX_CTRL_FLAGS \
  93. (HFA384X_TX_CTRL_802_11 | HFA384X_TX_CTRL_TX_EX)
  94. /* ca. 1 usec */
  95. #define HFA384X_CMD_BUSY_TIMEOUT 5000
  96. #define HFA384X_BAP_BUSY_TIMEOUT 50000
  97. /* ca. 10 usec */
  98. #define HFA384X_CMD_COMPL_TIMEOUT 20000
  99. #define HFA384X_DL_COMPL_TIMEOUT 1000000
  100. /* Wait times for initialization; yield to other processes to avoid busy
  101. * waiting for long time. */
  102. #define HFA384X_INIT_TIMEOUT (HZ / 2) /* 500 ms */
  103. #define HFA384X_ALLOC_COMPL_TIMEOUT (HZ / 20) /* 50 ms */
  104. static void prism2_hw_reset(struct net_device *dev);
  105. static void prism2_check_sta_fw_version(local_info_t *local);
  106. #ifdef PRISM2_DOWNLOAD_SUPPORT
  107. /* hostap_download.c */
  108. static const struct file_operations prism2_download_aux_dump_proc_fops;
  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 void hfa384x_read_regs(struct net_device *dev,
  125. struct hfa384x_regs *regs)
  126. {
  127. regs->cmd = HFA384X_INW(HFA384X_CMD_OFF);
  128. regs->evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
  129. regs->offset0 = HFA384X_INW(HFA384X_OFFSET0_OFF);
  130. regs->offset1 = HFA384X_INW(HFA384X_OFFSET1_OFF);
  131. regs->swsupport0 = HFA384X_INW(HFA384X_SWSUPPORT0_OFF);
  132. }
  133. /**
  134. * __hostap_cmd_queue_free - Free Prism2 command queue entry (private)
  135. * @local: pointer to private Host AP driver data
  136. * @entry: Prism2 command queue entry to be freed
  137. * @del_req: request the entry to be removed
  138. *
  139. * Internal helper function for freeing Prism2 command queue entries.
  140. * Caller must have acquired local->cmdlock before calling this function.
  141. */
  142. static inline void __hostap_cmd_queue_free(local_info_t *local,
  143. struct hostap_cmd_queue *entry,
  144. int del_req)
  145. {
  146. if (del_req) {
  147. entry->del_req = 1;
  148. if (!list_empty(&entry->list)) {
  149. list_del_init(&entry->list);
  150. local->cmd_queue_len--;
  151. }
  152. }
  153. if (refcount_dec_and_test(&entry->usecnt) && entry->del_req)
  154. kfree(entry);
  155. }
  156. /**
  157. * hostap_cmd_queue_free - Free Prism2 command queue entry
  158. * @local: pointer to private Host AP driver data
  159. * @entry: Prism2 command queue entry to be freed
  160. * @del_req: request the entry to be removed
  161. *
  162. * Free a Prism2 command queue entry.
  163. */
  164. static inline void hostap_cmd_queue_free(local_info_t *local,
  165. struct hostap_cmd_queue *entry,
  166. int del_req)
  167. {
  168. unsigned long flags;
  169. spin_lock_irqsave(&local->cmdlock, flags);
  170. __hostap_cmd_queue_free(local, entry, del_req);
  171. spin_unlock_irqrestore(&local->cmdlock, flags);
  172. }
  173. /**
  174. * prism2_clear_cmd_queue - Free all pending Prism2 command queue entries
  175. * @local: pointer to private Host AP driver data
  176. */
  177. static void prism2_clear_cmd_queue(local_info_t *local)
  178. {
  179. struct list_head *ptr, *n;
  180. unsigned long flags;
  181. struct hostap_cmd_queue *entry;
  182. spin_lock_irqsave(&local->cmdlock, flags);
  183. list_for_each_safe(ptr, n, &local->cmd_queue) {
  184. entry = list_entry(ptr, struct hostap_cmd_queue, list);
  185. refcount_inc(&entry->usecnt);
  186. printk(KERN_DEBUG "%s: removed pending cmd_queue entry "
  187. "(type=%d, cmd=0x%04x, param0=0x%04x)\n",
  188. local->dev->name, entry->type, entry->cmd,
  189. entry->param0);
  190. __hostap_cmd_queue_free(local, entry, 1);
  191. }
  192. if (local->cmd_queue_len) {
  193. /* This should not happen; print debug message and clear
  194. * queue length. */
  195. printk(KERN_DEBUG "%s: cmd_queue_len (%d) not zero after "
  196. "flush\n", local->dev->name, local->cmd_queue_len);
  197. local->cmd_queue_len = 0;
  198. }
  199. spin_unlock_irqrestore(&local->cmdlock, flags);
  200. }
  201. /**
  202. * hfa384x_cmd_issue - Issue a Prism2 command to the hardware
  203. * @dev: pointer to net_device
  204. * @entry: Prism2 command queue entry to be issued
  205. */
  206. static int hfa384x_cmd_issue(struct net_device *dev,
  207. struct hostap_cmd_queue *entry)
  208. {
  209. struct hostap_interface *iface;
  210. local_info_t *local;
  211. int tries;
  212. u16 reg;
  213. unsigned long flags;
  214. iface = netdev_priv(dev);
  215. local = iface->local;
  216. if (local->func->card_present && !local->func->card_present(local))
  217. return -ENODEV;
  218. if (entry->issued) {
  219. printk(KERN_DEBUG "%s: driver bug - re-issuing command @%p\n",
  220. dev->name, entry);
  221. }
  222. /* wait until busy bit is clear; this should always be clear since the
  223. * commands are serialized */
  224. tries = HFA384X_CMD_BUSY_TIMEOUT;
  225. while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
  226. tries--;
  227. udelay(1);
  228. }
  229. #ifndef final_version
  230. if (tries != HFA384X_CMD_BUSY_TIMEOUT) {
  231. prism2_io_debug_error(dev, 1);
  232. printk(KERN_DEBUG "%s: hfa384x_cmd_issue: cmd reg was busy "
  233. "for %d usec\n", dev->name,
  234. HFA384X_CMD_BUSY_TIMEOUT - tries);
  235. }
  236. #endif
  237. if (tries == 0) {
  238. reg = HFA384X_INW(HFA384X_CMD_OFF);
  239. prism2_io_debug_error(dev, 2);
  240. printk(KERN_DEBUG "%s: hfa384x_cmd_issue - timeout - "
  241. "reg=0x%04x\n", dev->name, reg);
  242. return -ETIMEDOUT;
  243. }
  244. /* write command */
  245. spin_lock_irqsave(&local->cmdlock, flags);
  246. HFA384X_OUTW(entry->param0, HFA384X_PARAM0_OFF);
  247. HFA384X_OUTW(entry->param1, HFA384X_PARAM1_OFF);
  248. HFA384X_OUTW(entry->cmd, HFA384X_CMD_OFF);
  249. entry->issued = 1;
  250. spin_unlock_irqrestore(&local->cmdlock, flags);
  251. return 0;
  252. }
  253. /**
  254. * hfa384x_cmd - Issue a Prism2 command and wait (sleep) for completion
  255. * @dev: pointer to net_device
  256. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  257. * @param0: value for Param0 register
  258. * @param1: value for Param1 register (pointer; %NULL if not used)
  259. * @resp0: pointer for Resp0 data or %NULL if Resp0 is not needed
  260. *
  261. * Issue given command (possibly after waiting in command queue) and sleep
  262. * until the command is completed (or timed out or interrupted). This can be
  263. * called only from user process context.
  264. */
  265. static int hfa384x_cmd(struct net_device *dev, u16 cmd, u16 param0,
  266. u16 *param1, u16 *resp0)
  267. {
  268. struct hostap_interface *iface;
  269. local_info_t *local;
  270. int err, res, issue, issued = 0;
  271. unsigned long flags;
  272. struct hostap_cmd_queue *entry;
  273. DECLARE_WAITQUEUE(wait, current);
  274. iface = netdev_priv(dev);
  275. local = iface->local;
  276. if (in_interrupt()) {
  277. printk(KERN_DEBUG "%s: hfa384x_cmd called from interrupt "
  278. "context\n", dev->name);
  279. return -1;
  280. }
  281. if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN) {
  282. printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
  283. dev->name);
  284. return -1;
  285. }
  286. if (signal_pending(current))
  287. return -EINTR;
  288. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  289. if (entry == NULL)
  290. return -ENOMEM;
  291. refcount_set(&entry->usecnt, 1);
  292. entry->type = CMD_SLEEP;
  293. entry->cmd = cmd;
  294. entry->param0 = param0;
  295. if (param1)
  296. entry->param1 = *param1;
  297. init_waitqueue_head(&entry->compl);
  298. /* prepare to wait for command completion event, but do not sleep yet
  299. */
  300. add_wait_queue(&entry->compl, &wait);
  301. set_current_state(TASK_INTERRUPTIBLE);
  302. spin_lock_irqsave(&local->cmdlock, flags);
  303. issue = list_empty(&local->cmd_queue);
  304. if (issue)
  305. entry->issuing = 1;
  306. list_add_tail(&entry->list, &local->cmd_queue);
  307. local->cmd_queue_len++;
  308. spin_unlock_irqrestore(&local->cmdlock, flags);
  309. err = 0;
  310. if (!issue)
  311. goto wait_completion;
  312. if (signal_pending(current))
  313. err = -EINTR;
  314. if (!err) {
  315. if (hfa384x_cmd_issue(dev, entry))
  316. err = -ETIMEDOUT;
  317. else
  318. issued = 1;
  319. }
  320. wait_completion:
  321. if (!err && entry->type != CMD_COMPLETED) {
  322. /* sleep until command is completed or timed out */
  323. res = schedule_timeout(2 * HZ);
  324. } else
  325. res = -1;
  326. if (!err && signal_pending(current))
  327. err = -EINTR;
  328. if (err && issued) {
  329. /* the command was issued, so a CmdCompl event should occur
  330. * soon; however, there's a pending signal and
  331. * schedule_timeout() would be interrupted; wait a short period
  332. * of time to avoid removing entry from the list before
  333. * CmdCompl event */
  334. udelay(300);
  335. }
  336. set_current_state(TASK_RUNNING);
  337. remove_wait_queue(&entry->compl, &wait);
  338. /* If entry->list is still in the list, it must be removed
  339. * first and in this case prism2_cmd_ev() does not yet have
  340. * local reference to it, and the data can be kfree()'d
  341. * here. If the command completion event is still generated,
  342. * it will be assigned to next (possibly) pending command, but
  343. * the driver will reset the card anyway due to timeout
  344. *
  345. * If the entry is not in the list prism2_cmd_ev() has a local
  346. * reference to it, but keeps cmdlock as long as the data is
  347. * needed, so the data can be kfree()'d here. */
  348. /* FIX: if the entry->list is in the list, it has not been completed
  349. * yet, so removing it here is somewhat wrong.. this could cause
  350. * references to freed memory and next list_del() causing NULL pointer
  351. * dereference.. it would probably be better to leave the entry in the
  352. * list and the list should be emptied during hw reset */
  353. spin_lock_irqsave(&local->cmdlock, flags);
  354. if (!list_empty(&entry->list)) {
  355. printk(KERN_DEBUG "%s: hfa384x_cmd: entry still in list? "
  356. "(entry=%p, type=%d, res=%d)\n", dev->name, entry,
  357. entry->type, res);
  358. list_del_init(&entry->list);
  359. local->cmd_queue_len--;
  360. }
  361. spin_unlock_irqrestore(&local->cmdlock, flags);
  362. if (err) {
  363. printk(KERN_DEBUG "%s: hfa384x_cmd: interrupted; err=%d\n",
  364. dev->name, err);
  365. res = err;
  366. goto done;
  367. }
  368. if (entry->type != CMD_COMPLETED) {
  369. u16 reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
  370. printk(KERN_DEBUG "%s: hfa384x_cmd: command was not "
  371. "completed (res=%d, entry=%p, type=%d, cmd=0x%04x, "
  372. "param0=0x%04x, EVSTAT=%04x INTEN=%04x)\n", dev->name,
  373. res, entry, entry->type, entry->cmd, entry->param0, reg,
  374. HFA384X_INW(HFA384X_INTEN_OFF));
  375. if (reg & HFA384X_EV_CMD) {
  376. /* Command completion event is pending, but the
  377. * interrupt was not delivered - probably an issue
  378. * with pcmcia-cs configuration. */
  379. printk(KERN_WARNING "%s: interrupt delivery does not "
  380. "seem to work\n", dev->name);
  381. }
  382. prism2_io_debug_error(dev, 3);
  383. res = -ETIMEDOUT;
  384. goto done;
  385. }
  386. if (resp0 != NULL)
  387. *resp0 = entry->resp0;
  388. #ifndef final_version
  389. if (entry->res) {
  390. printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x, "
  391. "resp0=0x%04x\n",
  392. dev->name, cmd, entry->res, entry->resp0);
  393. }
  394. #endif /* final_version */
  395. res = entry->res;
  396. done:
  397. hostap_cmd_queue_free(local, entry, 1);
  398. return res;
  399. }
  400. /**
  401. * hfa384x_cmd_callback - Issue a Prism2 command; callback when completed
  402. * @dev: pointer to net_device
  403. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  404. * @param0: value for Param0 register
  405. * @callback: command completion callback function (%NULL = no callback)
  406. * @context: context data to be given to the callback function
  407. *
  408. * Issue given command (possibly after waiting in command queue) and use
  409. * callback function to indicate command completion. This can be called both
  410. * from user and interrupt context. The callback function will be called in
  411. * hardware IRQ context. It can be %NULL, when no function is called when
  412. * command is completed.
  413. */
  414. static int hfa384x_cmd_callback(struct net_device *dev, u16 cmd, u16 param0,
  415. void (*callback)(struct net_device *dev,
  416. long context, u16 resp0,
  417. u16 status),
  418. long context)
  419. {
  420. struct hostap_interface *iface;
  421. local_info_t *local;
  422. int issue, ret;
  423. unsigned long flags;
  424. struct hostap_cmd_queue *entry;
  425. iface = netdev_priv(dev);
  426. local = iface->local;
  427. if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN + 2) {
  428. printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
  429. dev->name);
  430. return -1;
  431. }
  432. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  433. if (entry == NULL)
  434. return -ENOMEM;
  435. refcount_set(&entry->usecnt, 1);
  436. entry->type = CMD_CALLBACK;
  437. entry->cmd = cmd;
  438. entry->param0 = param0;
  439. entry->callback = callback;
  440. entry->context = context;
  441. spin_lock_irqsave(&local->cmdlock, flags);
  442. issue = list_empty(&local->cmd_queue);
  443. if (issue)
  444. entry->issuing = 1;
  445. list_add_tail(&entry->list, &local->cmd_queue);
  446. local->cmd_queue_len++;
  447. spin_unlock_irqrestore(&local->cmdlock, flags);
  448. if (issue && hfa384x_cmd_issue(dev, entry))
  449. ret = -ETIMEDOUT;
  450. else
  451. ret = 0;
  452. hostap_cmd_queue_free(local, entry, ret);
  453. return ret;
  454. }
  455. /**
  456. * __hfa384x_cmd_no_wait - Issue a Prism2 command (private)
  457. * @dev: pointer to net_device
  458. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  459. * @param0: value for Param0 register
  460. * @io_debug_num: I/O debug error number
  461. *
  462. * Shared helper function for hfa384x_cmd_wait() and hfa384x_cmd_no_wait().
  463. */
  464. static int __hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd, u16 param0,
  465. int io_debug_num)
  466. {
  467. int tries;
  468. u16 reg;
  469. /* wait until busy bit is clear; this should always be clear since the
  470. * commands are serialized */
  471. tries = HFA384X_CMD_BUSY_TIMEOUT;
  472. while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
  473. tries--;
  474. udelay(1);
  475. }
  476. if (tries == 0) {
  477. reg = HFA384X_INW(HFA384X_CMD_OFF);
  478. prism2_io_debug_error(dev, io_debug_num);
  479. printk(KERN_DEBUG "%s: __hfa384x_cmd_no_wait(%d) - timeout - "
  480. "reg=0x%04x\n", dev->name, io_debug_num, reg);
  481. return -ETIMEDOUT;
  482. }
  483. /* write command */
  484. HFA384X_OUTW(param0, HFA384X_PARAM0_OFF);
  485. HFA384X_OUTW(cmd, HFA384X_CMD_OFF);
  486. return 0;
  487. }
  488. /**
  489. * hfa384x_cmd_wait - Issue a Prism2 command and busy wait for completion
  490. * @dev: pointer to net_device
  491. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  492. * @param0: value for Param0 register
  493. */
  494. static int hfa384x_cmd_wait(struct net_device *dev, u16 cmd, u16 param0)
  495. {
  496. int res, tries;
  497. u16 reg;
  498. res = __hfa384x_cmd_no_wait(dev, cmd, param0, 4);
  499. if (res)
  500. return res;
  501. /* wait for command completion */
  502. if ((cmd & HFA384X_CMDCODE_MASK) == HFA384X_CMDCODE_DOWNLOAD)
  503. tries = HFA384X_DL_COMPL_TIMEOUT;
  504. else
  505. tries = HFA384X_CMD_COMPL_TIMEOUT;
  506. while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
  507. tries > 0) {
  508. tries--;
  509. udelay(10);
  510. }
  511. if (tries == 0) {
  512. reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
  513. prism2_io_debug_error(dev, 5);
  514. printk(KERN_DEBUG "%s: hfa384x_cmd_wait - timeout2 - "
  515. "reg=0x%04x\n", dev->name, reg);
  516. return -ETIMEDOUT;
  517. }
  518. res = (HFA384X_INW(HFA384X_STATUS_OFF) &
  519. (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) | BIT(9) |
  520. BIT(8))) >> 8;
  521. #ifndef final_version
  522. if (res) {
  523. printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x\n",
  524. dev->name, cmd, res);
  525. }
  526. #endif
  527. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  528. return res;
  529. }
  530. /**
  531. * hfa384x_cmd_no_wait - Issue a Prism2 command; do not wait for completion
  532. * @dev: pointer to net_device
  533. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  534. * @param0: value for Param0 register
  535. */
  536. static inline int hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd,
  537. u16 param0)
  538. {
  539. return __hfa384x_cmd_no_wait(dev, cmd, param0, 6);
  540. }
  541. /**
  542. * prism2_cmd_ev - Prism2 command completion event handler
  543. * @dev: pointer to net_device
  544. *
  545. * Interrupt handler for command completion events. Called by the main
  546. * interrupt handler in hardware IRQ context. Read Resp0 and status registers
  547. * from the hardware and ACK the event. Depending on the issued command type
  548. * either wake up the sleeping process that is waiting for command completion
  549. * or call the callback function. Issue the next command, if one is pending.
  550. */
  551. static void prism2_cmd_ev(struct net_device *dev)
  552. {
  553. struct hostap_interface *iface;
  554. local_info_t *local;
  555. struct hostap_cmd_queue *entry = NULL;
  556. iface = netdev_priv(dev);
  557. local = iface->local;
  558. spin_lock(&local->cmdlock);
  559. if (!list_empty(&local->cmd_queue)) {
  560. entry = list_entry(local->cmd_queue.next,
  561. struct hostap_cmd_queue, list);
  562. refcount_inc(&entry->usecnt);
  563. list_del_init(&entry->list);
  564. local->cmd_queue_len--;
  565. if (!entry->issued) {
  566. printk(KERN_DEBUG "%s: Command completion event, but "
  567. "cmd not issued\n", dev->name);
  568. __hostap_cmd_queue_free(local, entry, 1);
  569. entry = NULL;
  570. }
  571. }
  572. spin_unlock(&local->cmdlock);
  573. if (!entry) {
  574. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  575. printk(KERN_DEBUG "%s: Command completion event, but no "
  576. "pending commands\n", dev->name);
  577. return;
  578. }
  579. entry->resp0 = HFA384X_INW(HFA384X_RESP0_OFF);
  580. entry->res = (HFA384X_INW(HFA384X_STATUS_OFF) &
  581. (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) |
  582. BIT(9) | BIT(8))) >> 8;
  583. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  584. /* TODO: rest of the CmdEv handling could be moved to tasklet */
  585. if (entry->type == CMD_SLEEP) {
  586. entry->type = CMD_COMPLETED;
  587. wake_up_interruptible(&entry->compl);
  588. } else if (entry->type == CMD_CALLBACK) {
  589. if (entry->callback)
  590. entry->callback(dev, entry->context, entry->resp0,
  591. entry->res);
  592. } else {
  593. printk(KERN_DEBUG "%s: Invalid command completion type %d\n",
  594. dev->name, entry->type);
  595. }
  596. hostap_cmd_queue_free(local, entry, 1);
  597. /* issue next command, if pending */
  598. entry = NULL;
  599. spin_lock(&local->cmdlock);
  600. if (!list_empty(&local->cmd_queue)) {
  601. entry = list_entry(local->cmd_queue.next,
  602. struct hostap_cmd_queue, list);
  603. if (entry->issuing) {
  604. /* hfa384x_cmd() has already started issuing this
  605. * command, so do not start here */
  606. entry = NULL;
  607. }
  608. if (entry)
  609. refcount_inc(&entry->usecnt);
  610. }
  611. spin_unlock(&local->cmdlock);
  612. if (entry) {
  613. /* issue next command; if command issuing fails, remove the
  614. * entry from cmd_queue */
  615. int res = hfa384x_cmd_issue(dev, entry);
  616. spin_lock(&local->cmdlock);
  617. __hostap_cmd_queue_free(local, entry, res);
  618. spin_unlock(&local->cmdlock);
  619. }
  620. }
  621. static int hfa384x_wait_offset(struct net_device *dev, u16 o_off)
  622. {
  623. int tries = HFA384X_BAP_BUSY_TIMEOUT;
  624. int res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
  625. while (res && tries > 0) {
  626. tries--;
  627. udelay(1);
  628. res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
  629. }
  630. return res;
  631. }
  632. /* Offset must be even */
  633. static int hfa384x_setup_bap(struct net_device *dev, u16 bap, u16 id,
  634. int offset)
  635. {
  636. u16 o_off, s_off;
  637. int ret = 0;
  638. if (offset % 2 || bap > 1)
  639. return -EINVAL;
  640. if (bap == BAP1) {
  641. o_off = HFA384X_OFFSET1_OFF;
  642. s_off = HFA384X_SELECT1_OFF;
  643. } else {
  644. o_off = HFA384X_OFFSET0_OFF;
  645. s_off = HFA384X_SELECT0_OFF;
  646. }
  647. if (hfa384x_wait_offset(dev, o_off)) {
  648. prism2_io_debug_error(dev, 7);
  649. printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout before\n",
  650. dev->name);
  651. ret = -ETIMEDOUT;
  652. goto out;
  653. }
  654. HFA384X_OUTW(id, s_off);
  655. HFA384X_OUTW(offset, o_off);
  656. if (hfa384x_wait_offset(dev, o_off)) {
  657. prism2_io_debug_error(dev, 8);
  658. printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout after\n",
  659. dev->name);
  660. ret = -ETIMEDOUT;
  661. goto out;
  662. }
  663. #ifndef final_version
  664. if (HFA384X_INW(o_off) & HFA384X_OFFSET_ERR) {
  665. prism2_io_debug_error(dev, 9);
  666. printk(KERN_DEBUG "%s: hfa384x_setup_bap - offset error "
  667. "(%d,0x04%x,%d); reg=0x%04x\n",
  668. dev->name, bap, id, offset, HFA384X_INW(o_off));
  669. ret = -EINVAL;
  670. }
  671. #endif
  672. out:
  673. return ret;
  674. }
  675. static int hfa384x_get_rid(struct net_device *dev, u16 rid, void *buf, int len,
  676. int exact_len)
  677. {
  678. struct hostap_interface *iface;
  679. local_info_t *local;
  680. int res, rlen = 0;
  681. struct hfa384x_rid_hdr rec;
  682. iface = netdev_priv(dev);
  683. local = iface->local;
  684. if (local->no_pri) {
  685. printk(KERN_DEBUG "%s: cannot get RID %04x (len=%d) - no PRI "
  686. "f/w\n", dev->name, rid, len);
  687. return -ENOTTY; /* Well.. not really correct, but return
  688. * something unique enough.. */
  689. }
  690. if ((local->func->card_present && !local->func->card_present(local)) ||
  691. local->hw_downloading)
  692. return -ENODEV;
  693. res = mutex_lock_interruptible(&local->rid_bap_mtx);
  694. if (res)
  695. return res;
  696. res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS, rid, NULL, NULL);
  697. if (res) {
  698. printk(KERN_DEBUG "%s: hfa384x_get_rid: CMDCODE_ACCESS failed "
  699. "(res=%d, rid=%04x, len=%d)\n",
  700. dev->name, res, rid, len);
  701. mutex_unlock(&local->rid_bap_mtx);
  702. return res;
  703. }
  704. spin_lock_bh(&local->baplock);
  705. res = hfa384x_setup_bap(dev, BAP0, rid, 0);
  706. if (res)
  707. goto unlock;
  708. res = hfa384x_from_bap(dev, BAP0, &rec, sizeof(rec));
  709. if (res)
  710. goto unlock;
  711. if (le16_to_cpu(rec.len) == 0) {
  712. /* RID not available */
  713. res = -ENODATA;
  714. goto unlock;
  715. }
  716. rlen = (le16_to_cpu(rec.len) - 1) * 2;
  717. if (exact_len && rlen != len) {
  718. printk(KERN_DEBUG "%s: hfa384x_get_rid - RID len mismatch: "
  719. "rid=0x%04x, len=%d (expected %d)\n",
  720. dev->name, rid, rlen, len);
  721. res = -ENODATA;
  722. }
  723. res = hfa384x_from_bap(dev, BAP0, buf, len);
  724. unlock:
  725. spin_unlock_bh(&local->baplock);
  726. mutex_unlock(&local->rid_bap_mtx);
  727. if (res) {
  728. if (res != -ENODATA)
  729. printk(KERN_DEBUG "%s: hfa384x_get_rid (rid=%04x, "
  730. "len=%d) - failed - res=%d\n", dev->name, rid,
  731. len, res);
  732. if (res == -ETIMEDOUT)
  733. prism2_hw_reset(dev);
  734. return res;
  735. }
  736. return rlen;
  737. }
  738. static int hfa384x_set_rid(struct net_device *dev, u16 rid, void *buf, int len)
  739. {
  740. struct hostap_interface *iface;
  741. local_info_t *local;
  742. struct hfa384x_rid_hdr rec;
  743. int res;
  744. iface = netdev_priv(dev);
  745. local = iface->local;
  746. if (local->no_pri) {
  747. printk(KERN_DEBUG "%s: cannot set RID %04x (len=%d) - no PRI "
  748. "f/w\n", dev->name, rid, len);
  749. return -ENOTTY; /* Well.. not really correct, but return
  750. * something unique enough.. */
  751. }
  752. if ((local->func->card_present && !local->func->card_present(local)) ||
  753. local->hw_downloading)
  754. return -ENODEV;
  755. rec.rid = cpu_to_le16(rid);
  756. /* RID len in words and +1 for rec.rid */
  757. rec.len = cpu_to_le16(len / 2 + len % 2 + 1);
  758. res = mutex_lock_interruptible(&local->rid_bap_mtx);
  759. if (res)
  760. return res;
  761. spin_lock_bh(&local->baplock);
  762. res = hfa384x_setup_bap(dev, BAP0, rid, 0);
  763. if (!res)
  764. res = hfa384x_to_bap(dev, BAP0, &rec, sizeof(rec));
  765. if (!res)
  766. res = hfa384x_to_bap(dev, BAP0, buf, len);
  767. spin_unlock_bh(&local->baplock);
  768. if (res) {
  769. printk(KERN_DEBUG "%s: hfa384x_set_rid (rid=%04x, len=%d) - "
  770. "failed - res=%d\n", dev->name, rid, len, res);
  771. mutex_unlock(&local->rid_bap_mtx);
  772. return res;
  773. }
  774. res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS_WRITE, rid, NULL, NULL);
  775. mutex_unlock(&local->rid_bap_mtx);
  776. if (res) {
  777. printk(KERN_DEBUG "%s: hfa384x_set_rid: CMDCODE_ACCESS_WRITE "
  778. "failed (res=%d, rid=%04x, len=%d)\n",
  779. dev->name, res, rid, len);
  780. if (res == -ETIMEDOUT)
  781. prism2_hw_reset(dev);
  782. }
  783. return res;
  784. }
  785. static void hfa384x_disable_interrupts(struct net_device *dev)
  786. {
  787. /* disable interrupts and clear event status */
  788. HFA384X_OUTW(0, HFA384X_INTEN_OFF);
  789. HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
  790. }
  791. static void hfa384x_enable_interrupts(struct net_device *dev)
  792. {
  793. /* ack pending events and enable interrupts from selected events */
  794. HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
  795. HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
  796. }
  797. static void hfa384x_events_no_bap0(struct net_device *dev)
  798. {
  799. HFA384X_OUTW(HFA384X_EVENT_MASK & ~HFA384X_BAP0_EVENTS,
  800. HFA384X_INTEN_OFF);
  801. }
  802. static void hfa384x_events_all(struct net_device *dev)
  803. {
  804. HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
  805. }
  806. static void hfa384x_events_only_cmd(struct net_device *dev)
  807. {
  808. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_INTEN_OFF);
  809. }
  810. static u16 hfa384x_allocate_fid(struct net_device *dev, int len)
  811. {
  812. u16 fid;
  813. unsigned long delay;
  814. /* FIX: this could be replace with hfa384x_cmd() if the Alloc event
  815. * below would be handled like CmdCompl event (sleep here, wake up from
  816. * interrupt handler */
  817. if (hfa384x_cmd_wait(dev, HFA384X_CMDCODE_ALLOC, len)) {
  818. printk(KERN_DEBUG "%s: cannot allocate fid, len=%d\n",
  819. dev->name, len);
  820. return 0xffff;
  821. }
  822. delay = jiffies + HFA384X_ALLOC_COMPL_TIMEOUT;
  823. while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC) &&
  824. time_before(jiffies, delay))
  825. yield();
  826. if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC)) {
  827. printk("%s: fid allocate, len=%d - timeout\n", dev->name, len);
  828. return 0xffff;
  829. }
  830. fid = HFA384X_INW(HFA384X_ALLOCFID_OFF);
  831. HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
  832. return fid;
  833. }
  834. static int prism2_reset_port(struct net_device *dev)
  835. {
  836. struct hostap_interface *iface;
  837. local_info_t *local;
  838. int res;
  839. iface = netdev_priv(dev);
  840. local = iface->local;
  841. if (!local->dev_enabled)
  842. return 0;
  843. res = hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0,
  844. NULL, NULL);
  845. if (res)
  846. printk(KERN_DEBUG "%s: reset port failed to disable port\n",
  847. dev->name);
  848. else {
  849. res = hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0,
  850. NULL, NULL);
  851. if (res)
  852. printk(KERN_DEBUG "%s: reset port failed to enable "
  853. "port\n", dev->name);
  854. }
  855. /* It looks like at least some STA firmware versions reset
  856. * fragmentation threshold back to 2346 after enable command. Restore
  857. * the configured value, if it differs from this default. */
  858. if (local->fragm_threshold != 2346 &&
  859. hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
  860. local->fragm_threshold)) {
  861. printk(KERN_DEBUG "%s: failed to restore fragmentation "
  862. "threshold (%d) after Port0 enable\n",
  863. dev->name, local->fragm_threshold);
  864. }
  865. /* Some firmwares lose antenna selection settings on reset */
  866. (void) hostap_set_antsel(local);
  867. return res;
  868. }
  869. static int prism2_get_version_info(struct net_device *dev, u16 rid,
  870. const char *txt)
  871. {
  872. struct hfa384x_comp_ident comp;
  873. struct hostap_interface *iface;
  874. local_info_t *local;
  875. iface = netdev_priv(dev);
  876. local = iface->local;
  877. if (local->no_pri) {
  878. /* PRI f/w not yet available - cannot read RIDs */
  879. return -1;
  880. }
  881. if (hfa384x_get_rid(dev, rid, &comp, sizeof(comp), 1) < 0) {
  882. printk(KERN_DEBUG "Could not get RID for component %s\n", txt);
  883. return -1;
  884. }
  885. printk(KERN_INFO "%s: %s: id=0x%02x v%d.%d.%d\n", dev->name, txt,
  886. __le16_to_cpu(comp.id), __le16_to_cpu(comp.major),
  887. __le16_to_cpu(comp.minor), __le16_to_cpu(comp.variant));
  888. return 0;
  889. }
  890. static int prism2_setup_rids(struct net_device *dev)
  891. {
  892. struct hostap_interface *iface;
  893. local_info_t *local;
  894. __le16 tmp;
  895. int ret = 0;
  896. iface = netdev_priv(dev);
  897. local = iface->local;
  898. hostap_set_word(dev, HFA384X_RID_TICKTIME, 2000);
  899. if (!local->fw_ap) {
  900. u16 tmp1 = hostap_get_porttype(local);
  901. ret = hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE, tmp1);
  902. if (ret) {
  903. printk("%s: Port type setting to %d failed\n",
  904. dev->name, tmp1);
  905. goto fail;
  906. }
  907. }
  908. /* Setting SSID to empty string seems to kill the card in Host AP mode
  909. */
  910. if (local->iw_mode != IW_MODE_MASTER || local->essid[0] != '\0') {
  911. ret = hostap_set_string(dev, HFA384X_RID_CNFOWNSSID,
  912. local->essid);
  913. if (ret) {
  914. printk("%s: AP own SSID setting failed\n", dev->name);
  915. goto fail;
  916. }
  917. }
  918. ret = hostap_set_word(dev, HFA384X_RID_CNFMAXDATALEN,
  919. PRISM2_DATA_MAXLEN);
  920. if (ret) {
  921. printk("%s: MAC data length setting to %d failed\n",
  922. dev->name, PRISM2_DATA_MAXLEN);
  923. goto fail;
  924. }
  925. if (hfa384x_get_rid(dev, HFA384X_RID_CHANNELLIST, &tmp, 2, 1) < 0) {
  926. printk("%s: Channel list read failed\n", dev->name);
  927. ret = -EINVAL;
  928. goto fail;
  929. }
  930. local->channel_mask = le16_to_cpu(tmp);
  931. if (local->channel < 1 || local->channel > 14 ||
  932. !(local->channel_mask & (1 << (local->channel - 1)))) {
  933. printk(KERN_WARNING "%s: Channel setting out of range "
  934. "(%d)!\n", dev->name, local->channel);
  935. ret = -EBUSY;
  936. goto fail;
  937. }
  938. ret = hostap_set_word(dev, HFA384X_RID_CNFOWNCHANNEL, local->channel);
  939. if (ret) {
  940. printk("%s: Channel setting to %d failed\n",
  941. dev->name, local->channel);
  942. goto fail;
  943. }
  944. ret = hostap_set_word(dev, HFA384X_RID_CNFBEACONINT,
  945. local->beacon_int);
  946. if (ret) {
  947. printk("%s: Beacon interval setting to %d failed\n",
  948. dev->name, local->beacon_int);
  949. /* this may fail with Symbol/Lucent firmware */
  950. if (ret == -ETIMEDOUT)
  951. goto fail;
  952. }
  953. ret = hostap_set_word(dev, HFA384X_RID_CNFOWNDTIMPERIOD,
  954. local->dtim_period);
  955. if (ret) {
  956. printk("%s: DTIM period setting to %d failed\n",
  957. dev->name, local->dtim_period);
  958. /* this may fail with Symbol/Lucent firmware */
  959. if (ret == -ETIMEDOUT)
  960. goto fail;
  961. }
  962. ret = hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
  963. local->is_promisc);
  964. if (ret)
  965. printk(KERN_INFO "%s: Setting promiscuous mode (%d) failed\n",
  966. dev->name, local->is_promisc);
  967. if (!local->fw_ap) {
  968. ret = hostap_set_string(dev, HFA384X_RID_CNFDESIREDSSID,
  969. local->essid);
  970. if (ret) {
  971. printk("%s: Desired SSID setting failed\n", dev->name);
  972. goto fail;
  973. }
  974. }
  975. /* Setup TXRateControl, defaults to allow use of 1, 2, 5.5, and
  976. * 11 Mbps in automatic TX rate fallback and 1 and 2 Mbps as basic
  977. * rates */
  978. if (local->tx_rate_control == 0) {
  979. local->tx_rate_control =
  980. HFA384X_RATES_1MBPS |
  981. HFA384X_RATES_2MBPS |
  982. HFA384X_RATES_5MBPS |
  983. HFA384X_RATES_11MBPS;
  984. }
  985. if (local->basic_rates == 0)
  986. local->basic_rates = HFA384X_RATES_1MBPS | HFA384X_RATES_2MBPS;
  987. if (!local->fw_ap) {
  988. ret = hostap_set_word(dev, HFA384X_RID_TXRATECONTROL,
  989. local->tx_rate_control);
  990. if (ret) {
  991. printk("%s: TXRateControl setting to %d failed\n",
  992. dev->name, local->tx_rate_control);
  993. goto fail;
  994. }
  995. ret = hostap_set_word(dev, HFA384X_RID_CNFSUPPORTEDRATES,
  996. local->tx_rate_control);
  997. if (ret) {
  998. printk("%s: cnfSupportedRates setting to %d failed\n",
  999. dev->name, local->tx_rate_control);
  1000. }
  1001. ret = hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
  1002. local->basic_rates);
  1003. if (ret) {
  1004. printk("%s: cnfBasicRates setting to %d failed\n",
  1005. dev->name, local->basic_rates);
  1006. }
  1007. ret = hostap_set_word(dev, HFA384X_RID_CREATEIBSS, 1);
  1008. if (ret) {
  1009. printk("%s: Create IBSS setting to 1 failed\n",
  1010. dev->name);
  1011. }
  1012. }
  1013. if (local->name_set)
  1014. (void) hostap_set_string(dev, HFA384X_RID_CNFOWNNAME,
  1015. local->name);
  1016. if (hostap_set_encryption(local)) {
  1017. printk(KERN_INFO "%s: could not configure encryption\n",
  1018. dev->name);
  1019. }
  1020. (void) hostap_set_antsel(local);
  1021. if (hostap_set_roaming(local)) {
  1022. printk(KERN_INFO "%s: could not set host roaming\n",
  1023. dev->name);
  1024. }
  1025. if (local->sta_fw_ver >= PRISM2_FW_VER(1,6,3) &&
  1026. hostap_set_word(dev, HFA384X_RID_CNFENHSECURITY, local->enh_sec))
  1027. printk(KERN_INFO "%s: cnfEnhSecurity setting to 0x%x failed\n",
  1028. dev->name, local->enh_sec);
  1029. /* 32-bit tallies were added in STA f/w 0.8.0, but they were apparently
  1030. * not working correctly (last seven counters report bogus values).
  1031. * This has been fixed in 0.8.2, so enable 32-bit tallies only
  1032. * beginning with that firmware version. Another bug fix for 32-bit
  1033. * tallies in 1.4.0; should 16-bit tallies be used for some other
  1034. * versions, too? */
  1035. if (local->sta_fw_ver >= PRISM2_FW_VER(0,8,2)) {
  1036. if (hostap_set_word(dev, HFA384X_RID_CNFTHIRTY2TALLY, 1)) {
  1037. printk(KERN_INFO "%s: cnfThirty2Tally setting "
  1038. "failed\n", dev->name);
  1039. local->tallies32 = 0;
  1040. } else
  1041. local->tallies32 = 1;
  1042. } else
  1043. local->tallies32 = 0;
  1044. hostap_set_auth_algs(local);
  1045. if (hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
  1046. local->fragm_threshold)) {
  1047. printk(KERN_INFO "%s: setting FragmentationThreshold to %d "
  1048. "failed\n", dev->name, local->fragm_threshold);
  1049. }
  1050. if (hostap_set_word(dev, HFA384X_RID_RTSTHRESHOLD,
  1051. local->rts_threshold)) {
  1052. printk(KERN_INFO "%s: setting RTSThreshold to %d failed\n",
  1053. dev->name, local->rts_threshold);
  1054. }
  1055. if (local->manual_retry_count >= 0 &&
  1056. hostap_set_word(dev, HFA384X_RID_CNFALTRETRYCOUNT,
  1057. local->manual_retry_count)) {
  1058. printk(KERN_INFO "%s: setting cnfAltRetryCount to %d failed\n",
  1059. dev->name, local->manual_retry_count);
  1060. }
  1061. if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1) &&
  1062. hfa384x_get_rid(dev, HFA384X_RID_CNFDBMADJUST, &tmp, 2, 1) == 2) {
  1063. local->rssi_to_dBm = le16_to_cpu(tmp);
  1064. }
  1065. if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->wpa &&
  1066. hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE, 1)) {
  1067. printk(KERN_INFO "%s: setting ssnHandlingMode to 1 failed\n",
  1068. dev->name);
  1069. }
  1070. if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->generic_elem &&
  1071. hfa384x_set_rid(dev, HFA384X_RID_GENERICELEMENT,
  1072. local->generic_elem, local->generic_elem_len)) {
  1073. printk(KERN_INFO "%s: setting genericElement failed\n",
  1074. dev->name);
  1075. }
  1076. fail:
  1077. return ret;
  1078. }
  1079. static int prism2_hw_init(struct net_device *dev, int initial)
  1080. {
  1081. struct hostap_interface *iface;
  1082. local_info_t *local;
  1083. int ret, first = 1;
  1084. unsigned long start, delay;
  1085. PDEBUG(DEBUG_FLOW, "prism2_hw_init()\n");
  1086. iface = netdev_priv(dev);
  1087. local = iface->local;
  1088. clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits);
  1089. init:
  1090. /* initialize HFA 384x */
  1091. ret = hfa384x_cmd_no_wait(dev, HFA384X_CMDCODE_INIT, 0);
  1092. if (ret) {
  1093. printk(KERN_INFO "%s: first command failed - assuming card "
  1094. "does not have primary firmware\n", dev_info);
  1095. }
  1096. if (first && (HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
  1097. /* EvStat has Cmd bit set in some cases, so retry once if no
  1098. * wait was needed */
  1099. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  1100. printk(KERN_DEBUG "%s: init command completed too quickly - "
  1101. "retrying\n", dev->name);
  1102. first = 0;
  1103. goto init;
  1104. }
  1105. start = jiffies;
  1106. delay = jiffies + HFA384X_INIT_TIMEOUT;
  1107. while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
  1108. time_before(jiffies, delay))
  1109. yield();
  1110. if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
  1111. printk(KERN_DEBUG "%s: assuming no Primary image in "
  1112. "flash - card initialization not completed\n",
  1113. dev_info);
  1114. local->no_pri = 1;
  1115. #ifdef PRISM2_DOWNLOAD_SUPPORT
  1116. if (local->sram_type == -1)
  1117. local->sram_type = prism2_get_ram_size(local);
  1118. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  1119. return 1;
  1120. }
  1121. local->no_pri = 0;
  1122. printk(KERN_DEBUG "prism2_hw_init: initialized in %lu ms\n",
  1123. (jiffies - start) * 1000 / HZ);
  1124. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  1125. return 0;
  1126. }
  1127. static int prism2_hw_init2(struct net_device *dev, int initial)
  1128. {
  1129. struct hostap_interface *iface;
  1130. local_info_t *local;
  1131. int i;
  1132. iface = netdev_priv(dev);
  1133. local = iface->local;
  1134. #ifdef PRISM2_DOWNLOAD_SUPPORT
  1135. kfree(local->pda);
  1136. if (local->no_pri)
  1137. local->pda = NULL;
  1138. else
  1139. local->pda = prism2_read_pda(dev);
  1140. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  1141. hfa384x_disable_interrupts(dev);
  1142. #ifndef final_version
  1143. HFA384X_OUTW(HFA384X_MAGIC, HFA384X_SWSUPPORT0_OFF);
  1144. if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
  1145. printk("SWSUPPORT0 write/read failed: %04X != %04X\n",
  1146. HFA384X_INW(HFA384X_SWSUPPORT0_OFF), HFA384X_MAGIC);
  1147. goto failed;
  1148. }
  1149. #endif
  1150. if (initial || local->pri_only) {
  1151. hfa384x_events_only_cmd(dev);
  1152. /* get card version information */
  1153. if (prism2_get_version_info(dev, HFA384X_RID_NICID, "NIC") ||
  1154. prism2_get_version_info(dev, HFA384X_RID_PRIID, "PRI")) {
  1155. hfa384x_disable_interrupts(dev);
  1156. goto failed;
  1157. }
  1158. if (prism2_get_version_info(dev, HFA384X_RID_STAID, "STA")) {
  1159. printk(KERN_DEBUG "%s: Failed to read STA f/w version "
  1160. "- only Primary f/w present\n", dev->name);
  1161. local->pri_only = 1;
  1162. return 0;
  1163. }
  1164. local->pri_only = 0;
  1165. hfa384x_disable_interrupts(dev);
  1166. }
  1167. /* FIX: could convert allocate_fid to use sleeping CmdCompl wait and
  1168. * enable interrupts before this. This would also require some sort of
  1169. * sleeping AllocEv waiting */
  1170. /* allocate TX FIDs */
  1171. local->txfid_len = PRISM2_TXFID_LEN;
  1172. for (i = 0; i < PRISM2_TXFID_COUNT; i++) {
  1173. local->txfid[i] = hfa384x_allocate_fid(dev, local->txfid_len);
  1174. if (local->txfid[i] == 0xffff && local->txfid_len > 1600) {
  1175. local->txfid[i] = hfa384x_allocate_fid(dev, 1600);
  1176. if (local->txfid[i] != 0xffff) {
  1177. printk(KERN_DEBUG "%s: Using shorter TX FID "
  1178. "(1600 bytes)\n", dev->name);
  1179. local->txfid_len = 1600;
  1180. }
  1181. }
  1182. if (local->txfid[i] == 0xffff)
  1183. goto failed;
  1184. local->intransmitfid[i] = PRISM2_TXFID_EMPTY;
  1185. }
  1186. hfa384x_events_only_cmd(dev);
  1187. if (initial) {
  1188. struct list_head *ptr;
  1189. prism2_check_sta_fw_version(local);
  1190. if (hfa384x_get_rid(dev, HFA384X_RID_CNFOWNMACADDR,
  1191. dev->dev_addr, 6, 1) < 0) {
  1192. printk("%s: could not get own MAC address\n",
  1193. dev->name);
  1194. }
  1195. list_for_each(ptr, &local->hostap_interfaces) {
  1196. iface = list_entry(ptr, struct hostap_interface, list);
  1197. eth_hw_addr_inherit(iface->dev, dev);
  1198. }
  1199. } else if (local->fw_ap)
  1200. prism2_check_sta_fw_version(local);
  1201. prism2_setup_rids(dev);
  1202. /* MAC is now configured, but port 0 is not yet enabled */
  1203. return 0;
  1204. failed:
  1205. if (!local->no_pri)
  1206. printk(KERN_WARNING "%s: Initialization failed\n", dev_info);
  1207. return 1;
  1208. }
  1209. static int prism2_hw_enable(struct net_device *dev, int initial)
  1210. {
  1211. struct hostap_interface *iface;
  1212. local_info_t *local;
  1213. int was_resetting;
  1214. iface = netdev_priv(dev);
  1215. local = iface->local;
  1216. was_resetting = local->hw_resetting;
  1217. if (hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0, NULL, NULL)) {
  1218. printk("%s: MAC port 0 enabling failed\n", dev->name);
  1219. return 1;
  1220. }
  1221. local->hw_ready = 1;
  1222. local->hw_reset_tries = 0;
  1223. local->hw_resetting = 0;
  1224. hfa384x_enable_interrupts(dev);
  1225. /* at least D-Link DWL-650 seems to require additional port reset
  1226. * before it starts acting as an AP, so reset port automatically
  1227. * here just in case */
  1228. if (initial && prism2_reset_port(dev)) {
  1229. printk("%s: MAC port 0 resetting failed\n", dev->name);
  1230. return 1;
  1231. }
  1232. if (was_resetting && netif_queue_stopped(dev)) {
  1233. /* If hw_reset() was called during pending transmit, netif
  1234. * queue was stopped. Wake it up now since the wlan card has
  1235. * been resetted. */
  1236. netif_wake_queue(dev);
  1237. }
  1238. return 0;
  1239. }
  1240. static int prism2_hw_config(struct net_device *dev, int initial)
  1241. {
  1242. struct hostap_interface *iface;
  1243. local_info_t *local;
  1244. iface = netdev_priv(dev);
  1245. local = iface->local;
  1246. if (local->hw_downloading)
  1247. return 1;
  1248. if (prism2_hw_init(dev, initial)) {
  1249. return local->no_pri ? 0 : 1;
  1250. }
  1251. if (prism2_hw_init2(dev, initial))
  1252. return 1;
  1253. /* Enable firmware if secondary image is loaded and at least one of the
  1254. * netdevices is up. */
  1255. if (!local->pri_only &&
  1256. (initial == 0 || (initial == 2 && local->num_dev_open > 0))) {
  1257. if (!local->dev_enabled)
  1258. prism2_callback(local, PRISM2_CALLBACK_ENABLE);
  1259. local->dev_enabled = 1;
  1260. return prism2_hw_enable(dev, initial);
  1261. }
  1262. return 0;
  1263. }
  1264. static void prism2_hw_shutdown(struct net_device *dev, int no_disable)
  1265. {
  1266. struct hostap_interface *iface;
  1267. local_info_t *local;
  1268. iface = netdev_priv(dev);
  1269. local = iface->local;
  1270. /* Allow only command completion events during disable */
  1271. hfa384x_events_only_cmd(dev);
  1272. local->hw_ready = 0;
  1273. if (local->dev_enabled)
  1274. prism2_callback(local, PRISM2_CALLBACK_DISABLE);
  1275. local->dev_enabled = 0;
  1276. if (local->func->card_present && !local->func->card_present(local)) {
  1277. printk(KERN_DEBUG "%s: card already removed or not configured "
  1278. "during shutdown\n", dev->name);
  1279. return;
  1280. }
  1281. if ((no_disable & HOSTAP_HW_NO_DISABLE) == 0 &&
  1282. hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0, NULL, NULL))
  1283. printk(KERN_WARNING "%s: Shutdown failed\n", dev_info);
  1284. hfa384x_disable_interrupts(dev);
  1285. if (no_disable & HOSTAP_HW_ENABLE_CMDCOMPL)
  1286. hfa384x_events_only_cmd(dev);
  1287. else
  1288. prism2_clear_cmd_queue(local);
  1289. }
  1290. static void prism2_hw_reset(struct net_device *dev)
  1291. {
  1292. struct hostap_interface *iface;
  1293. local_info_t *local;
  1294. #if 0
  1295. static long last_reset = 0;
  1296. /* do not reset card more than once per second to avoid ending up in a
  1297. * busy loop resetting the card */
  1298. if (time_before_eq(jiffies, last_reset + HZ))
  1299. return;
  1300. last_reset = jiffies;
  1301. #endif
  1302. iface = netdev_priv(dev);
  1303. local = iface->local;
  1304. if (in_interrupt()) {
  1305. printk(KERN_DEBUG "%s: driver bug - prism2_hw_reset() called "
  1306. "in interrupt context\n", dev->name);
  1307. return;
  1308. }
  1309. if (local->hw_downloading)
  1310. return;
  1311. if (local->hw_resetting) {
  1312. printk(KERN_WARNING "%s: %s: already resetting card - "
  1313. "ignoring reset request\n", dev_info, dev->name);
  1314. return;
  1315. }
  1316. local->hw_reset_tries++;
  1317. if (local->hw_reset_tries > 10) {
  1318. printk(KERN_WARNING "%s: too many reset tries, skipping\n",
  1319. dev->name);
  1320. return;
  1321. }
  1322. printk(KERN_WARNING "%s: %s: resetting card\n", dev_info, dev->name);
  1323. hfa384x_disable_interrupts(dev);
  1324. local->hw_resetting = 1;
  1325. if (local->func->cor_sreset) {
  1326. /* Host system seems to hang in some cases with high traffic
  1327. * load or shared interrupts during COR sreset. Disable shared
  1328. * interrupts during reset to avoid these crashes. COS sreset
  1329. * takes quite a long time, so it is unfortunate that this
  1330. * seems to be needed. Anyway, I do not know of any better way
  1331. * of avoiding the crash. */
  1332. disable_irq(dev->irq);
  1333. local->func->cor_sreset(local);
  1334. enable_irq(dev->irq);
  1335. }
  1336. prism2_hw_shutdown(dev, 1);
  1337. prism2_hw_config(dev, 0);
  1338. local->hw_resetting = 0;
  1339. #ifdef PRISM2_DOWNLOAD_SUPPORT
  1340. if (local->dl_pri) {
  1341. printk(KERN_DEBUG "%s: persistent download of primary "
  1342. "firmware\n", dev->name);
  1343. if (prism2_download_genesis(local, local->dl_pri) < 0)
  1344. printk(KERN_WARNING "%s: download (PRI) failed\n",
  1345. dev->name);
  1346. }
  1347. if (local->dl_sec) {
  1348. printk(KERN_DEBUG "%s: persistent download of secondary "
  1349. "firmware\n", dev->name);
  1350. if (prism2_download_volatile(local, local->dl_sec) < 0)
  1351. printk(KERN_WARNING "%s: download (SEC) failed\n",
  1352. dev->name);
  1353. }
  1354. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  1355. /* TODO: restore beacon TIM bits for STAs that have buffered frames */
  1356. }
  1357. static void prism2_schedule_reset(local_info_t *local)
  1358. {
  1359. schedule_work(&local->reset_queue);
  1360. }
  1361. /* Called only as scheduled task after noticing card timeout in interrupt
  1362. * context */
  1363. static void handle_reset_queue(struct work_struct *work)
  1364. {
  1365. local_info_t *local = container_of(work, local_info_t, reset_queue);
  1366. printk(KERN_DEBUG "%s: scheduled card reset\n", local->dev->name);
  1367. prism2_hw_reset(local->dev);
  1368. if (netif_queue_stopped(local->dev)) {
  1369. int i;
  1370. for (i = 0; i < PRISM2_TXFID_COUNT; i++)
  1371. if (local->intransmitfid[i] == PRISM2_TXFID_EMPTY) {
  1372. PDEBUG(DEBUG_EXTRA, "prism2_tx_timeout: "
  1373. "wake up queue\n");
  1374. netif_wake_queue(local->dev);
  1375. break;
  1376. }
  1377. }
  1378. }
  1379. static int prism2_get_txfid_idx(local_info_t *local)
  1380. {
  1381. int idx, end;
  1382. unsigned long flags;
  1383. spin_lock_irqsave(&local->txfidlock, flags);
  1384. end = idx = local->next_txfid;
  1385. do {
  1386. if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
  1387. local->intransmitfid[idx] = PRISM2_TXFID_RESERVED;
  1388. spin_unlock_irqrestore(&local->txfidlock, flags);
  1389. return idx;
  1390. }
  1391. idx++;
  1392. if (idx >= PRISM2_TXFID_COUNT)
  1393. idx = 0;
  1394. } while (idx != end);
  1395. spin_unlock_irqrestore(&local->txfidlock, flags);
  1396. PDEBUG(DEBUG_EXTRA2, "prism2_get_txfid_idx: no room in txfid buf: "
  1397. "packet dropped\n");
  1398. local->dev->stats.tx_dropped++;
  1399. return -1;
  1400. }
  1401. /* Called only from hardware IRQ */
  1402. static void prism2_transmit_cb(struct net_device *dev, long context,
  1403. u16 resp0, u16 res)
  1404. {
  1405. struct hostap_interface *iface;
  1406. local_info_t *local;
  1407. int idx = (int) context;
  1408. iface = netdev_priv(dev);
  1409. local = iface->local;
  1410. if (res) {
  1411. printk(KERN_DEBUG "%s: prism2_transmit_cb - res=0x%02x\n",
  1412. dev->name, res);
  1413. return;
  1414. }
  1415. if (idx < 0 || idx >= PRISM2_TXFID_COUNT) {
  1416. printk(KERN_DEBUG "%s: prism2_transmit_cb called with invalid "
  1417. "idx=%d\n", dev->name, idx);
  1418. return;
  1419. }
  1420. if (!test_and_clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
  1421. printk(KERN_DEBUG "%s: driver bug: prism2_transmit_cb called "
  1422. "with no pending transmit\n", dev->name);
  1423. }
  1424. if (netif_queue_stopped(dev)) {
  1425. /* ready for next TX, so wake up queue that was stopped in
  1426. * prism2_transmit() */
  1427. netif_wake_queue(dev);
  1428. }
  1429. spin_lock(&local->txfidlock);
  1430. /* With reclaim, Resp0 contains new txfid for transmit; the old txfid
  1431. * will be automatically allocated for the next TX frame */
  1432. local->intransmitfid[idx] = resp0;
  1433. PDEBUG(DEBUG_FID, "%s: prism2_transmit_cb: txfid[%d]=0x%04x, "
  1434. "resp0=0x%04x, transmit_txfid=0x%04x\n",
  1435. dev->name, idx, local->txfid[idx],
  1436. resp0, local->intransmitfid[local->next_txfid]);
  1437. idx++;
  1438. if (idx >= PRISM2_TXFID_COUNT)
  1439. idx = 0;
  1440. local->next_txfid = idx;
  1441. /* check if all TX buffers are occupied */
  1442. do {
  1443. if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
  1444. spin_unlock(&local->txfidlock);
  1445. return;
  1446. }
  1447. idx++;
  1448. if (idx >= PRISM2_TXFID_COUNT)
  1449. idx = 0;
  1450. } while (idx != local->next_txfid);
  1451. spin_unlock(&local->txfidlock);
  1452. /* no empty TX buffers, stop queue */
  1453. netif_stop_queue(dev);
  1454. }
  1455. /* Called only from software IRQ if PCI bus master is not used (with bus master
  1456. * this can be called both from software and hardware IRQ) */
  1457. static int prism2_transmit(struct net_device *dev, int idx)
  1458. {
  1459. struct hostap_interface *iface;
  1460. local_info_t *local;
  1461. int res;
  1462. iface = netdev_priv(dev);
  1463. local = iface->local;
  1464. /* The driver tries to stop netif queue so that there would not be
  1465. * more than one attempt to transmit frames going on; check that this
  1466. * is really the case */
  1467. if (test_and_set_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
  1468. printk(KERN_DEBUG "%s: driver bug - prism2_transmit() called "
  1469. "when previous TX was pending\n", dev->name);
  1470. return -1;
  1471. }
  1472. /* stop the queue for the time that transmit is pending */
  1473. netif_stop_queue(dev);
  1474. /* transmit packet */
  1475. res = hfa384x_cmd_callback(
  1476. dev,
  1477. HFA384X_CMDCODE_TRANSMIT | HFA384X_CMD_TX_RECLAIM,
  1478. local->txfid[idx],
  1479. prism2_transmit_cb, (long) idx);
  1480. if (res) {
  1481. printk(KERN_DEBUG "%s: prism2_transmit: CMDCODE_TRANSMIT "
  1482. "failed (res=%d)\n", dev->name, res);
  1483. dev->stats.tx_dropped++;
  1484. netif_wake_queue(dev);
  1485. return -1;
  1486. }
  1487. netif_trans_update(dev);
  1488. /* Since we did not wait for command completion, the card continues
  1489. * to process on the background and we will finish handling when
  1490. * command completion event is handled (prism2_cmd_ev() function) */
  1491. return 0;
  1492. }
  1493. /* Send IEEE 802.11 frame (convert the header into Prism2 TX descriptor and
  1494. * send the payload with this descriptor) */
  1495. /* Called only from software IRQ */
  1496. static int prism2_tx_80211(struct sk_buff *skb, struct net_device *dev)
  1497. {
  1498. struct hostap_interface *iface;
  1499. local_info_t *local;
  1500. struct hfa384x_tx_frame txdesc;
  1501. struct hostap_skb_tx_data *meta;
  1502. int hdr_len, data_len, idx, res, ret = -1;
  1503. u16 tx_control, fc;
  1504. iface = netdev_priv(dev);
  1505. local = iface->local;
  1506. meta = (struct hostap_skb_tx_data *) skb->cb;
  1507. prism2_callback(local, PRISM2_CALLBACK_TX_START);
  1508. if ((local->func->card_present && !local->func->card_present(local)) ||
  1509. !local->hw_ready || local->hw_downloading || local->pri_only) {
  1510. if (net_ratelimit()) {
  1511. printk(KERN_DEBUG "%s: prism2_tx_80211: hw not ready -"
  1512. " skipping\n", dev->name);
  1513. }
  1514. goto fail;
  1515. }
  1516. memset(&txdesc, 0, sizeof(txdesc));
  1517. /* skb->data starts with txdesc->frame_control */
  1518. hdr_len = 24;
  1519. skb_copy_from_linear_data(skb, &txdesc.frame_control, hdr_len);
  1520. fc = le16_to_cpu(txdesc.frame_control);
  1521. if (ieee80211_is_data(txdesc.frame_control) &&
  1522. ieee80211_has_a4(txdesc.frame_control) &&
  1523. skb->len >= 30) {
  1524. /* Addr4 */
  1525. skb_copy_from_linear_data_offset(skb, hdr_len, txdesc.addr4,
  1526. ETH_ALEN);
  1527. hdr_len += ETH_ALEN;
  1528. }
  1529. tx_control = local->tx_control;
  1530. if (meta->tx_cb_idx) {
  1531. tx_control |= HFA384X_TX_CTRL_TX_OK;
  1532. txdesc.sw_support = cpu_to_le32(meta->tx_cb_idx);
  1533. }
  1534. txdesc.tx_control = cpu_to_le16(tx_control);
  1535. txdesc.tx_rate = meta->rate;
  1536. data_len = skb->len - hdr_len;
  1537. txdesc.data_len = cpu_to_le16(data_len);
  1538. txdesc.len = cpu_to_be16(data_len);
  1539. idx = prism2_get_txfid_idx(local);
  1540. if (idx < 0)
  1541. goto fail;
  1542. if (local->frame_dump & PRISM2_DUMP_TX_HDR)
  1543. hostap_dump_tx_header(dev->name, &txdesc);
  1544. spin_lock(&local->baplock);
  1545. res = hfa384x_setup_bap(dev, BAP0, local->txfid[idx], 0);
  1546. if (!res)
  1547. res = hfa384x_to_bap(dev, BAP0, &txdesc, sizeof(txdesc));
  1548. if (!res)
  1549. res = hfa384x_to_bap(dev, BAP0, skb->data + hdr_len,
  1550. skb->len - hdr_len);
  1551. spin_unlock(&local->baplock);
  1552. if (!res)
  1553. res = prism2_transmit(dev, idx);
  1554. if (res) {
  1555. printk(KERN_DEBUG "%s: prism2_tx_80211 - to BAP0 failed\n",
  1556. dev->name);
  1557. local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
  1558. schedule_work(&local->reset_queue);
  1559. goto fail;
  1560. }
  1561. ret = 0;
  1562. fail:
  1563. prism2_callback(local, PRISM2_CALLBACK_TX_END);
  1564. return ret;
  1565. }
  1566. /* Some SMP systems have reported number of odd errors with hostap_pci. fid
  1567. * register has changed values between consecutive reads for an unknown reason.
  1568. * This should really not happen, so more debugging is needed. This test
  1569. * version is a bit slower, but it will detect most of such register changes
  1570. * and will try to get the correct fid eventually. */
  1571. #define EXTRA_FID_READ_TESTS
  1572. static u16 prism2_read_fid_reg(struct net_device *dev, u16 reg)
  1573. {
  1574. #ifdef EXTRA_FID_READ_TESTS
  1575. u16 val, val2, val3;
  1576. int i;
  1577. for (i = 0; i < 10; i++) {
  1578. val = HFA384X_INW(reg);
  1579. val2 = HFA384X_INW(reg);
  1580. val3 = HFA384X_INW(reg);
  1581. if (val == val2 && val == val3)
  1582. return val;
  1583. printk(KERN_DEBUG "%s: detected fid change (try=%d, reg=%04x):"
  1584. " %04x %04x %04x\n",
  1585. dev->name, i, reg, val, val2, val3);
  1586. if ((val == val2 || val == val3) && val != 0)
  1587. return val;
  1588. if (val2 == val3 && val2 != 0)
  1589. return val2;
  1590. }
  1591. printk(KERN_WARNING "%s: Uhhuh.. could not read good fid from reg "
  1592. "%04x (%04x %04x %04x)\n", dev->name, reg, val, val2, val3);
  1593. return val;
  1594. #else /* EXTRA_FID_READ_TESTS */
  1595. return HFA384X_INW(reg);
  1596. #endif /* EXTRA_FID_READ_TESTS */
  1597. }
  1598. /* Called only as a tasklet (software IRQ) */
  1599. static void prism2_rx(local_info_t *local)
  1600. {
  1601. struct net_device *dev = local->dev;
  1602. int res, rx_pending = 0;
  1603. u16 len, hdr_len, rxfid, status, macport;
  1604. struct hfa384x_rx_frame rxdesc;
  1605. struct sk_buff *skb = NULL;
  1606. prism2_callback(local, PRISM2_CALLBACK_RX_START);
  1607. rxfid = prism2_read_fid_reg(dev, HFA384X_RXFID_OFF);
  1608. #ifndef final_version
  1609. if (rxfid == 0) {
  1610. rxfid = HFA384X_INW(HFA384X_RXFID_OFF);
  1611. printk(KERN_DEBUG "prism2_rx: rxfid=0 (next 0x%04x)\n",
  1612. rxfid);
  1613. if (rxfid == 0) {
  1614. schedule_work(&local->reset_queue);
  1615. goto rx_dropped;
  1616. }
  1617. /* try to continue with the new rxfid value */
  1618. }
  1619. #endif
  1620. spin_lock(&local->baplock);
  1621. res = hfa384x_setup_bap(dev, BAP0, rxfid, 0);
  1622. if (!res)
  1623. res = hfa384x_from_bap(dev, BAP0, &rxdesc, sizeof(rxdesc));
  1624. if (res) {
  1625. spin_unlock(&local->baplock);
  1626. printk(KERN_DEBUG "%s: copy from BAP0 failed %d\n", dev->name,
  1627. res);
  1628. if (res == -ETIMEDOUT) {
  1629. schedule_work(&local->reset_queue);
  1630. }
  1631. goto rx_dropped;
  1632. }
  1633. len = le16_to_cpu(rxdesc.data_len);
  1634. hdr_len = sizeof(rxdesc);
  1635. status = le16_to_cpu(rxdesc.status);
  1636. macport = (status >> 8) & 0x07;
  1637. /* Drop frames with too large reported payload length. Monitor mode
  1638. * seems to sometimes pass frames (e.g., ctrl::ack) with signed and
  1639. * negative value, so allow also values 65522 .. 65534 (-14 .. -2) for
  1640. * macport 7 */
  1641. if (len > PRISM2_DATA_MAXLEN + 8 /* WEP */) {
  1642. if (macport == 7 && local->iw_mode == IW_MODE_MONITOR) {
  1643. if (len >= (u16) -14) {
  1644. hdr_len -= 65535 - len;
  1645. hdr_len--;
  1646. }
  1647. len = 0;
  1648. } else {
  1649. spin_unlock(&local->baplock);
  1650. printk(KERN_DEBUG "%s: Received frame with invalid "
  1651. "length 0x%04x\n", dev->name, len);
  1652. hostap_dump_rx_header(dev->name, &rxdesc);
  1653. goto rx_dropped;
  1654. }
  1655. }
  1656. skb = dev_alloc_skb(len + hdr_len);
  1657. if (!skb) {
  1658. spin_unlock(&local->baplock);
  1659. printk(KERN_DEBUG "%s: RX failed to allocate skb\n",
  1660. dev->name);
  1661. goto rx_dropped;
  1662. }
  1663. skb->dev = dev;
  1664. skb_put_data(skb, &rxdesc, hdr_len);
  1665. if (len > 0)
  1666. res = hfa384x_from_bap(dev, BAP0, skb_put(skb, len), len);
  1667. spin_unlock(&local->baplock);
  1668. if (res) {
  1669. printk(KERN_DEBUG "%s: RX failed to read "
  1670. "frame data\n", dev->name);
  1671. goto rx_dropped;
  1672. }
  1673. skb_queue_tail(&local->rx_list, skb);
  1674. tasklet_schedule(&local->rx_tasklet);
  1675. rx_exit:
  1676. prism2_callback(local, PRISM2_CALLBACK_RX_END);
  1677. if (!rx_pending) {
  1678. HFA384X_OUTW(HFA384X_EV_RX, HFA384X_EVACK_OFF);
  1679. }
  1680. return;
  1681. rx_dropped:
  1682. dev->stats.rx_dropped++;
  1683. if (skb)
  1684. dev_kfree_skb(skb);
  1685. goto rx_exit;
  1686. }
  1687. /* Called only as a tasklet (software IRQ) */
  1688. static void hostap_rx_skb(local_info_t *local, struct sk_buff *skb)
  1689. {
  1690. struct hfa384x_rx_frame *rxdesc;
  1691. struct net_device *dev = skb->dev;
  1692. struct hostap_80211_rx_status stats;
  1693. int hdrlen, rx_hdrlen;
  1694. rx_hdrlen = sizeof(*rxdesc);
  1695. if (skb->len < sizeof(*rxdesc)) {
  1696. /* Allow monitor mode to receive shorter frames */
  1697. if (local->iw_mode == IW_MODE_MONITOR &&
  1698. skb->len >= sizeof(*rxdesc) - 30) {
  1699. rx_hdrlen = skb->len;
  1700. } else {
  1701. dev_kfree_skb(skb);
  1702. return;
  1703. }
  1704. }
  1705. rxdesc = (struct hfa384x_rx_frame *) skb->data;
  1706. if (local->frame_dump & PRISM2_DUMP_RX_HDR &&
  1707. skb->len >= sizeof(*rxdesc))
  1708. hostap_dump_rx_header(dev->name, rxdesc);
  1709. if (le16_to_cpu(rxdesc->status) & HFA384X_RX_STATUS_FCSERR &&
  1710. (!local->monitor_allow_fcserr ||
  1711. local->iw_mode != IW_MODE_MONITOR))
  1712. goto drop;
  1713. if (skb->len > PRISM2_DATA_MAXLEN) {
  1714. printk(KERN_DEBUG "%s: RX: len(%d) > MAX(%d)\n",
  1715. dev->name, skb->len, PRISM2_DATA_MAXLEN);
  1716. goto drop;
  1717. }
  1718. stats.mac_time = le32_to_cpu(rxdesc->time);
  1719. stats.signal = rxdesc->signal - local->rssi_to_dBm;
  1720. stats.noise = rxdesc->silence - local->rssi_to_dBm;
  1721. stats.rate = rxdesc->rate;
  1722. /* Convert Prism2 RX structure into IEEE 802.11 header */
  1723. hdrlen = hostap_80211_get_hdrlen(rxdesc->frame_control);
  1724. if (hdrlen > rx_hdrlen)
  1725. hdrlen = rx_hdrlen;
  1726. memmove(skb_pull(skb, rx_hdrlen - hdrlen),
  1727. &rxdesc->frame_control, hdrlen);
  1728. hostap_80211_rx(dev, skb, &stats);
  1729. return;
  1730. drop:
  1731. dev_kfree_skb(skb);
  1732. }
  1733. /* Called only as a tasklet (software IRQ) */
  1734. static void hostap_rx_tasklet(unsigned long data)
  1735. {
  1736. local_info_t *local = (local_info_t *) data;
  1737. struct sk_buff *skb;
  1738. while ((skb = skb_dequeue(&local->rx_list)) != NULL)
  1739. hostap_rx_skb(local, skb);
  1740. }
  1741. /* Called only from hardware IRQ */
  1742. static void prism2_alloc_ev(struct net_device *dev)
  1743. {
  1744. struct hostap_interface *iface;
  1745. local_info_t *local;
  1746. int idx;
  1747. u16 fid;
  1748. iface = netdev_priv(dev);
  1749. local = iface->local;
  1750. fid = prism2_read_fid_reg(dev, HFA384X_ALLOCFID_OFF);
  1751. PDEBUG(DEBUG_FID, "FID: interrupt: ALLOC - fid=0x%04x\n", fid);
  1752. spin_lock(&local->txfidlock);
  1753. idx = local->next_alloc;
  1754. do {
  1755. if (local->txfid[idx] == fid) {
  1756. PDEBUG(DEBUG_FID, "FID: found matching txfid[%d]\n",
  1757. idx);
  1758. #ifndef final_version
  1759. if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY)
  1760. printk("Already released txfid found at idx "
  1761. "%d\n", idx);
  1762. if (local->intransmitfid[idx] == PRISM2_TXFID_RESERVED)
  1763. printk("Already reserved txfid found at idx "
  1764. "%d\n", idx);
  1765. #endif
  1766. local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
  1767. idx++;
  1768. local->next_alloc = idx >= PRISM2_TXFID_COUNT ? 0 :
  1769. idx;
  1770. if (!test_bit(HOSTAP_BITS_TRANSMIT, &local->bits) &&
  1771. netif_queue_stopped(dev))
  1772. netif_wake_queue(dev);
  1773. spin_unlock(&local->txfidlock);
  1774. return;
  1775. }
  1776. idx++;
  1777. if (idx >= PRISM2_TXFID_COUNT)
  1778. idx = 0;
  1779. } while (idx != local->next_alloc);
  1780. printk(KERN_WARNING "%s: could not find matching txfid (0x%04x, new "
  1781. "read 0x%04x) for alloc event\n", dev->name, fid,
  1782. HFA384X_INW(HFA384X_ALLOCFID_OFF));
  1783. printk(KERN_DEBUG "TXFIDs:");
  1784. for (idx = 0; idx < PRISM2_TXFID_COUNT; idx++)
  1785. printk(" %04x[%04x]", local->txfid[idx],
  1786. local->intransmitfid[idx]);
  1787. printk("\n");
  1788. spin_unlock(&local->txfidlock);
  1789. /* FIX: should probably schedule reset; reference to one txfid was lost
  1790. * completely.. Bad things will happen if we run out of txfids
  1791. * Actually, this will cause netdev watchdog to notice TX timeout and
  1792. * then card reset after all txfids have been leaked. */
  1793. }
  1794. /* Called only as a tasklet (software IRQ) */
  1795. static void hostap_tx_callback(local_info_t *local,
  1796. struct hfa384x_tx_frame *txdesc, int ok,
  1797. char *payload)
  1798. {
  1799. u16 sw_support, hdrlen, len;
  1800. struct sk_buff *skb;
  1801. struct hostap_tx_callback_info *cb;
  1802. /* Make sure that frame was from us. */
  1803. if (!ether_addr_equal(txdesc->addr2, local->dev->dev_addr)) {
  1804. printk(KERN_DEBUG "%s: TX callback - foreign frame\n",
  1805. local->dev->name);
  1806. return;
  1807. }
  1808. sw_support = le32_to_cpu(txdesc->sw_support);
  1809. spin_lock(&local->lock);
  1810. cb = local->tx_callback;
  1811. while (cb != NULL && cb->idx != sw_support)
  1812. cb = cb->next;
  1813. spin_unlock(&local->lock);
  1814. if (cb == NULL) {
  1815. printk(KERN_DEBUG "%s: could not find TX callback (idx %d)\n",
  1816. local->dev->name, sw_support);
  1817. return;
  1818. }
  1819. hdrlen = hostap_80211_get_hdrlen(txdesc->frame_control);
  1820. len = le16_to_cpu(txdesc->data_len);
  1821. skb = dev_alloc_skb(hdrlen + len);
  1822. if (skb == NULL) {
  1823. printk(KERN_DEBUG "%s: hostap_tx_callback failed to allocate "
  1824. "skb\n", local->dev->name);
  1825. return;
  1826. }
  1827. skb_put_data(skb, (void *)&txdesc->frame_control, hdrlen);
  1828. if (payload)
  1829. skb_put_data(skb, payload, len);
  1830. skb->dev = local->dev;
  1831. skb_reset_mac_header(skb);
  1832. cb->func(skb, ok, cb->data);
  1833. }
  1834. /* Called only as a tasklet (software IRQ) */
  1835. static int hostap_tx_compl_read(local_info_t *local, int error,
  1836. struct hfa384x_tx_frame *txdesc,
  1837. char **payload)
  1838. {
  1839. u16 fid, len;
  1840. int res, ret = 0;
  1841. struct net_device *dev = local->dev;
  1842. fid = prism2_read_fid_reg(dev, HFA384X_TXCOMPLFID_OFF);
  1843. PDEBUG(DEBUG_FID, "interrupt: TX (err=%d) - fid=0x%04x\n", fid, error);
  1844. spin_lock(&local->baplock);
  1845. res = hfa384x_setup_bap(dev, BAP0, fid, 0);
  1846. if (!res)
  1847. res = hfa384x_from_bap(dev, BAP0, txdesc, sizeof(*txdesc));
  1848. if (res) {
  1849. PDEBUG(DEBUG_EXTRA, "%s: TX (err=%d) - fid=0x%04x - could not "
  1850. "read txdesc\n", dev->name, error, fid);
  1851. if (res == -ETIMEDOUT) {
  1852. schedule_work(&local->reset_queue);
  1853. }
  1854. ret = -1;
  1855. goto fail;
  1856. }
  1857. if (txdesc->sw_support) {
  1858. len = le16_to_cpu(txdesc->data_len);
  1859. if (len < PRISM2_DATA_MAXLEN) {
  1860. *payload = kmalloc(len, GFP_ATOMIC);
  1861. if (*payload == NULL ||
  1862. hfa384x_from_bap(dev, BAP0, *payload, len)) {
  1863. PDEBUG(DEBUG_EXTRA, "%s: could not read TX "
  1864. "frame payload\n", dev->name);
  1865. kfree(*payload);
  1866. *payload = NULL;
  1867. ret = -1;
  1868. goto fail;
  1869. }
  1870. }
  1871. }
  1872. fail:
  1873. spin_unlock(&local->baplock);
  1874. return ret;
  1875. }
  1876. /* Called only as a tasklet (software IRQ) */
  1877. static void prism2_tx_ev(local_info_t *local)
  1878. {
  1879. struct net_device *dev = local->dev;
  1880. char *payload = NULL;
  1881. struct hfa384x_tx_frame txdesc;
  1882. if (hostap_tx_compl_read(local, 0, &txdesc, &payload))
  1883. goto fail;
  1884. if (local->frame_dump & PRISM2_DUMP_TX_HDR) {
  1885. PDEBUG(DEBUG_EXTRA, "%s: TX - status=0x%04x "
  1886. "retry_count=%d tx_rate=%d seq_ctrl=%d "
  1887. "duration_id=%d\n",
  1888. dev->name, le16_to_cpu(txdesc.status),
  1889. txdesc.retry_count, txdesc.tx_rate,
  1890. le16_to_cpu(txdesc.seq_ctrl),
  1891. le16_to_cpu(txdesc.duration_id));
  1892. }
  1893. if (txdesc.sw_support)
  1894. hostap_tx_callback(local, &txdesc, 1, payload);
  1895. kfree(payload);
  1896. fail:
  1897. HFA384X_OUTW(HFA384X_EV_TX, HFA384X_EVACK_OFF);
  1898. }
  1899. /* Called only as a tasklet (software IRQ) */
  1900. static void hostap_sta_tx_exc_tasklet(unsigned long data)
  1901. {
  1902. local_info_t *local = (local_info_t *) data;
  1903. struct sk_buff *skb;
  1904. while ((skb = skb_dequeue(&local->sta_tx_exc_list)) != NULL) {
  1905. struct hfa384x_tx_frame *txdesc =
  1906. (struct hfa384x_tx_frame *) skb->data;
  1907. if (skb->len >= sizeof(*txdesc)) {
  1908. /* Convert Prism2 RX structure into IEEE 802.11 header
  1909. */
  1910. int hdrlen = hostap_80211_get_hdrlen(txdesc->frame_control);
  1911. memmove(skb_pull(skb, sizeof(*txdesc) - hdrlen),
  1912. &txdesc->frame_control, hdrlen);
  1913. hostap_handle_sta_tx_exc(local, skb);
  1914. }
  1915. dev_kfree_skb(skb);
  1916. }
  1917. }
  1918. /* Called only as a tasklet (software IRQ) */
  1919. static void prism2_txexc(local_info_t *local)
  1920. {
  1921. struct net_device *dev = local->dev;
  1922. u16 status, fc;
  1923. int show_dump, res;
  1924. char *payload = NULL;
  1925. struct hfa384x_tx_frame txdesc;
  1926. show_dump = local->frame_dump & PRISM2_DUMP_TXEXC_HDR;
  1927. dev->stats.tx_errors++;
  1928. res = hostap_tx_compl_read(local, 1, &txdesc, &payload);
  1929. HFA384X_OUTW(HFA384X_EV_TXEXC, HFA384X_EVACK_OFF);
  1930. if (res)
  1931. return;
  1932. status = le16_to_cpu(txdesc.status);
  1933. /* We produce a TXDROP event only for retry or lifetime
  1934. * exceeded, because that's the only status that really mean
  1935. * that this particular node went away.
  1936. * Other errors means that *we* screwed up. - Jean II */
  1937. if (status & (HFA384X_TX_STATUS_RETRYERR | HFA384X_TX_STATUS_AGEDERR))
  1938. {
  1939. union iwreq_data wrqu;
  1940. /* Copy 802.11 dest address. */
  1941. memcpy(wrqu.addr.sa_data, txdesc.addr1, ETH_ALEN);
  1942. wrqu.addr.sa_family = ARPHRD_ETHER;
  1943. wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
  1944. } else
  1945. show_dump = 1;
  1946. if (local->iw_mode == IW_MODE_MASTER ||
  1947. local->iw_mode == IW_MODE_REPEAT ||
  1948. local->wds_type & HOSTAP_WDS_AP_CLIENT) {
  1949. struct sk_buff *skb;
  1950. skb = dev_alloc_skb(sizeof(txdesc));
  1951. if (skb) {
  1952. skb_put_data(skb, &txdesc, sizeof(txdesc));
  1953. skb_queue_tail(&local->sta_tx_exc_list, skb);
  1954. tasklet_schedule(&local->sta_tx_exc_tasklet);
  1955. }
  1956. }
  1957. if (txdesc.sw_support)
  1958. hostap_tx_callback(local, &txdesc, 0, payload);
  1959. kfree(payload);
  1960. if (!show_dump)
  1961. return;
  1962. PDEBUG(DEBUG_EXTRA, "%s: TXEXC - status=0x%04x (%s%s%s%s)"
  1963. " tx_control=%04x\n",
  1964. dev->name, status,
  1965. status & HFA384X_TX_STATUS_RETRYERR ? "[RetryErr]" : "",
  1966. status & HFA384X_TX_STATUS_AGEDERR ? "[AgedErr]" : "",
  1967. status & HFA384X_TX_STATUS_DISCON ? "[Discon]" : "",
  1968. status & HFA384X_TX_STATUS_FORMERR ? "[FormErr]" : "",
  1969. le16_to_cpu(txdesc.tx_control));
  1970. fc = le16_to_cpu(txdesc.frame_control);
  1971. PDEBUG(DEBUG_EXTRA, " retry_count=%d tx_rate=%d fc=0x%04x "
  1972. "(%s%s%s::%d%s%s)\n",
  1973. txdesc.retry_count, txdesc.tx_rate, fc,
  1974. ieee80211_is_mgmt(txdesc.frame_control) ? "Mgmt" : "",
  1975. ieee80211_is_ctl(txdesc.frame_control) ? "Ctrl" : "",
  1976. ieee80211_is_data(txdesc.frame_control) ? "Data" : "",
  1977. (fc & IEEE80211_FCTL_STYPE) >> 4,
  1978. ieee80211_has_tods(txdesc.frame_control) ? " ToDS" : "",
  1979. ieee80211_has_fromds(txdesc.frame_control) ? " FromDS" : "");
  1980. PDEBUG(DEBUG_EXTRA, " A1=%pM A2=%pM A3=%pM A4=%pM\n",
  1981. txdesc.addr1, txdesc.addr2,
  1982. txdesc.addr3, txdesc.addr4);
  1983. }
  1984. /* Called only as a tasklet (software IRQ) */
  1985. static void hostap_info_tasklet(unsigned long data)
  1986. {
  1987. local_info_t *local = (local_info_t *) data;
  1988. struct sk_buff *skb;
  1989. while ((skb = skb_dequeue(&local->info_list)) != NULL) {
  1990. hostap_info_process(local, skb);
  1991. dev_kfree_skb(skb);
  1992. }
  1993. }
  1994. /* Called only as a tasklet (software IRQ) */
  1995. static void prism2_info(local_info_t *local)
  1996. {
  1997. struct net_device *dev = local->dev;
  1998. u16 fid;
  1999. int res, left;
  2000. struct hfa384x_info_frame info;
  2001. struct sk_buff *skb;
  2002. fid = HFA384X_INW(HFA384X_INFOFID_OFF);
  2003. spin_lock(&local->baplock);
  2004. res = hfa384x_setup_bap(dev, BAP0, fid, 0);
  2005. if (!res)
  2006. res = hfa384x_from_bap(dev, BAP0, &info, sizeof(info));
  2007. if (res) {
  2008. spin_unlock(&local->baplock);
  2009. printk(KERN_DEBUG "Could not get info frame (fid=0x%04x)\n",
  2010. fid);
  2011. if (res == -ETIMEDOUT) {
  2012. schedule_work(&local->reset_queue);
  2013. }
  2014. goto out;
  2015. }
  2016. left = (le16_to_cpu(info.len) - 1) * 2;
  2017. if (info.len & cpu_to_le16(0x8000) || info.len == 0 || left > 2060) {
  2018. /* data register seems to give 0x8000 in some error cases even
  2019. * though busy bit is not set in offset register;
  2020. * in addition, length must be at least 1 due to type field */
  2021. spin_unlock(&local->baplock);
  2022. printk(KERN_DEBUG "%s: Received info frame with invalid "
  2023. "length 0x%04x (type 0x%04x)\n", dev->name,
  2024. le16_to_cpu(info.len), le16_to_cpu(info.type));
  2025. goto out;
  2026. }
  2027. skb = dev_alloc_skb(sizeof(info) + left);
  2028. if (skb == NULL) {
  2029. spin_unlock(&local->baplock);
  2030. printk(KERN_DEBUG "%s: Could not allocate skb for info "
  2031. "frame\n", dev->name);
  2032. goto out;
  2033. }
  2034. skb_put_data(skb, &info, sizeof(info));
  2035. if (left > 0 && hfa384x_from_bap(dev, BAP0, skb_put(skb, left), left))
  2036. {
  2037. spin_unlock(&local->baplock);
  2038. printk(KERN_WARNING "%s: Info frame read failed (fid=0x%04x, "
  2039. "len=0x%04x, type=0x%04x\n", dev->name, fid,
  2040. le16_to_cpu(info.len), le16_to_cpu(info.type));
  2041. dev_kfree_skb(skb);
  2042. goto out;
  2043. }
  2044. spin_unlock(&local->baplock);
  2045. skb_queue_tail(&local->info_list, skb);
  2046. tasklet_schedule(&local->info_tasklet);
  2047. out:
  2048. HFA384X_OUTW(HFA384X_EV_INFO, HFA384X_EVACK_OFF);
  2049. }
  2050. /* Called only as a tasklet (software IRQ) */
  2051. static void hostap_bap_tasklet(unsigned long data)
  2052. {
  2053. local_info_t *local = (local_info_t *) data;
  2054. struct net_device *dev = local->dev;
  2055. u16 ev;
  2056. int frames = 30;
  2057. if (local->func->card_present && !local->func->card_present(local))
  2058. return;
  2059. set_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
  2060. /* Process all pending BAP events without generating new interrupts
  2061. * for them */
  2062. while (frames-- > 0) {
  2063. ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
  2064. if (ev == 0xffff || !(ev & HFA384X_BAP0_EVENTS))
  2065. break;
  2066. if (ev & HFA384X_EV_RX)
  2067. prism2_rx(local);
  2068. if (ev & HFA384X_EV_INFO)
  2069. prism2_info(local);
  2070. if (ev & HFA384X_EV_TX)
  2071. prism2_tx_ev(local);
  2072. if (ev & HFA384X_EV_TXEXC)
  2073. prism2_txexc(local);
  2074. }
  2075. set_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
  2076. clear_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
  2077. /* Enable interrupts for new BAP events */
  2078. hfa384x_events_all(dev);
  2079. clear_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
  2080. }
  2081. /* Called only from hardware IRQ */
  2082. static void prism2_infdrop(struct net_device *dev)
  2083. {
  2084. static unsigned long last_inquire = 0;
  2085. PDEBUG(DEBUG_EXTRA, "%s: INFDROP event\n", dev->name);
  2086. /* some firmware versions seem to get stuck with
  2087. * full CommTallies in high traffic load cases; every
  2088. * packet will then cause INFDROP event and CommTallies
  2089. * info frame will not be sent automatically. Try to
  2090. * get out of this state by inquiring CommTallies. */
  2091. if (!last_inquire || time_after(jiffies, last_inquire + HZ)) {
  2092. hfa384x_cmd_callback(dev, HFA384X_CMDCODE_INQUIRE,
  2093. HFA384X_INFO_COMMTALLIES, NULL, 0);
  2094. last_inquire = jiffies;
  2095. }
  2096. }
  2097. /* Called only from hardware IRQ */
  2098. static void prism2_ev_tick(struct net_device *dev)
  2099. {
  2100. struct hostap_interface *iface;
  2101. local_info_t *local;
  2102. u16 evstat, inten;
  2103. static int prev_stuck = 0;
  2104. iface = netdev_priv(dev);
  2105. local = iface->local;
  2106. if (time_after(jiffies, local->last_tick_timer + 5 * HZ) &&
  2107. local->last_tick_timer) {
  2108. evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
  2109. inten = HFA384X_INW(HFA384X_INTEN_OFF);
  2110. if (!prev_stuck) {
  2111. printk(KERN_INFO "%s: SW TICK stuck? "
  2112. "bits=0x%lx EvStat=%04x IntEn=%04x\n",
  2113. dev->name, local->bits, evstat, inten);
  2114. }
  2115. local->sw_tick_stuck++;
  2116. if ((evstat & HFA384X_BAP0_EVENTS) &&
  2117. (inten & HFA384X_BAP0_EVENTS)) {
  2118. printk(KERN_INFO "%s: trying to recover from IRQ "
  2119. "hang\n", dev->name);
  2120. hfa384x_events_no_bap0(dev);
  2121. }
  2122. prev_stuck = 1;
  2123. } else
  2124. prev_stuck = 0;
  2125. }
  2126. /* Called only from hardware IRQ */
  2127. static void prism2_check_magic(local_info_t *local)
  2128. {
  2129. /* at least PCI Prism2.5 with bus mastering seems to sometimes
  2130. * return 0x0000 in SWSUPPORT0 for unknown reason, but re-reading the
  2131. * register once or twice seems to get the correct value.. PCI cards
  2132. * cannot anyway be removed during normal operation, so there is not
  2133. * really any need for this verification with them. */
  2134. #ifndef PRISM2_PCI
  2135. #ifndef final_version
  2136. static unsigned long last_magic_err = 0;
  2137. struct net_device *dev = local->dev;
  2138. if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
  2139. if (!local->hw_ready)
  2140. return;
  2141. HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
  2142. if (time_after(jiffies, last_magic_err + 10 * HZ)) {
  2143. printk("%s: Interrupt, but SWSUPPORT0 does not match: "
  2144. "%04X != %04X - card removed?\n", dev->name,
  2145. HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
  2146. HFA384X_MAGIC);
  2147. last_magic_err = jiffies;
  2148. } else if (net_ratelimit()) {
  2149. printk(KERN_DEBUG "%s: interrupt - SWSUPPORT0=%04x "
  2150. "MAGIC=%04x\n", dev->name,
  2151. HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
  2152. HFA384X_MAGIC);
  2153. }
  2154. if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != 0xffff)
  2155. schedule_work(&local->reset_queue);
  2156. return;
  2157. }
  2158. #endif /* final_version */
  2159. #endif /* !PRISM2_PCI */
  2160. }
  2161. /* Called only from hardware IRQ */
  2162. static irqreturn_t prism2_interrupt(int irq, void *dev_id)
  2163. {
  2164. struct net_device *dev = dev_id;
  2165. struct hostap_interface *iface;
  2166. local_info_t *local;
  2167. int events = 0;
  2168. u16 ev;
  2169. iface = netdev_priv(dev);
  2170. local = iface->local;
  2171. /* Detect early interrupt before driver is fully configured */
  2172. spin_lock(&local->irq_init_lock);
  2173. if (!dev->base_addr) {
  2174. if (net_ratelimit()) {
  2175. printk(KERN_DEBUG "%s: Interrupt, but dev not configured\n",
  2176. dev->name);
  2177. }
  2178. spin_unlock(&local->irq_init_lock);
  2179. return IRQ_HANDLED;
  2180. }
  2181. spin_unlock(&local->irq_init_lock);
  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(struct timer_list *t)
  2321. {
  2322. local_info_t *local = from_timer(local, t, passive_scan_timer);
  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(struct timer_list *t)
  2384. {
  2385. static unsigned long last_inquire = 0;
  2386. local_info_t *local = from_timer(local, t, tick_timer);
  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. #if !defined(PRISM2_NO_PROCFS_DEBUG) && defined(CONFIG_PROC_FS)
  2407. static u16 hfa384x_read_reg(struct net_device *dev, u16 reg)
  2408. {
  2409. return HFA384X_INW(reg);
  2410. }
  2411. static int prism2_registers_proc_show(struct seq_file *m, void *v)
  2412. {
  2413. local_info_t *local = m->private;
  2414. #define SHOW_REG(n) \
  2415. seq_printf(m, #n "=%04x\n", hfa384x_read_reg(local->dev, HFA384X_##n##_OFF))
  2416. SHOW_REG(CMD);
  2417. SHOW_REG(PARAM0);
  2418. SHOW_REG(PARAM1);
  2419. SHOW_REG(PARAM2);
  2420. SHOW_REG(STATUS);
  2421. SHOW_REG(RESP0);
  2422. SHOW_REG(RESP1);
  2423. SHOW_REG(RESP2);
  2424. SHOW_REG(INFOFID);
  2425. SHOW_REG(CONTROL);
  2426. SHOW_REG(SELECT0);
  2427. SHOW_REG(SELECT1);
  2428. SHOW_REG(OFFSET0);
  2429. SHOW_REG(OFFSET1);
  2430. SHOW_REG(RXFID);
  2431. SHOW_REG(ALLOCFID);
  2432. SHOW_REG(TXCOMPLFID);
  2433. SHOW_REG(SWSUPPORT0);
  2434. SHOW_REG(SWSUPPORT1);
  2435. SHOW_REG(SWSUPPORT2);
  2436. SHOW_REG(EVSTAT);
  2437. SHOW_REG(INTEN);
  2438. SHOW_REG(EVACK);
  2439. /* Do not read data registers, because they change the state of the
  2440. * MAC (offset += 2) */
  2441. /* SHOW_REG(DATA0); */
  2442. /* SHOW_REG(DATA1); */
  2443. SHOW_REG(AUXPAGE);
  2444. SHOW_REG(AUXOFFSET);
  2445. /* SHOW_REG(AUXDATA); */
  2446. #ifdef PRISM2_PCI
  2447. SHOW_REG(PCICOR);
  2448. SHOW_REG(PCIHCR);
  2449. SHOW_REG(PCI_M0_ADDRH);
  2450. SHOW_REG(PCI_M0_ADDRL);
  2451. SHOW_REG(PCI_M0_LEN);
  2452. SHOW_REG(PCI_M0_CTL);
  2453. SHOW_REG(PCI_STATUS);
  2454. SHOW_REG(PCI_M1_ADDRH);
  2455. SHOW_REG(PCI_M1_ADDRL);
  2456. SHOW_REG(PCI_M1_LEN);
  2457. SHOW_REG(PCI_M1_CTL);
  2458. #endif /* PRISM2_PCI */
  2459. return 0;
  2460. }
  2461. #endif
  2462. struct set_tim_data {
  2463. struct list_head list;
  2464. int aid;
  2465. int set;
  2466. };
  2467. static int prism2_set_tim(struct net_device *dev, int aid, int set)
  2468. {
  2469. struct list_head *ptr;
  2470. struct set_tim_data *new_entry;
  2471. struct hostap_interface *iface;
  2472. local_info_t *local;
  2473. iface = netdev_priv(dev);
  2474. local = iface->local;
  2475. new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
  2476. if (new_entry == NULL)
  2477. return -ENOMEM;
  2478. new_entry->aid = aid;
  2479. new_entry->set = set;
  2480. spin_lock_bh(&local->set_tim_lock);
  2481. list_for_each(ptr, &local->set_tim_list) {
  2482. struct set_tim_data *entry =
  2483. list_entry(ptr, struct set_tim_data, list);
  2484. if (entry->aid == aid) {
  2485. PDEBUG(DEBUG_PS2, "%s: prism2_set_tim: aid=%d "
  2486. "set=%d ==> %d\n",
  2487. local->dev->name, aid, entry->set, set);
  2488. entry->set = set;
  2489. kfree(new_entry);
  2490. new_entry = NULL;
  2491. break;
  2492. }
  2493. }
  2494. if (new_entry)
  2495. list_add_tail(&new_entry->list, &local->set_tim_list);
  2496. spin_unlock_bh(&local->set_tim_lock);
  2497. schedule_work(&local->set_tim_queue);
  2498. return 0;
  2499. }
  2500. static void handle_set_tim_queue(struct work_struct *work)
  2501. {
  2502. local_info_t *local = container_of(work, local_info_t, set_tim_queue);
  2503. struct set_tim_data *entry;
  2504. u16 val;
  2505. for (;;) {
  2506. entry = NULL;
  2507. spin_lock_bh(&local->set_tim_lock);
  2508. if (!list_empty(&local->set_tim_list)) {
  2509. entry = list_entry(local->set_tim_list.next,
  2510. struct set_tim_data, list);
  2511. list_del(&entry->list);
  2512. }
  2513. spin_unlock_bh(&local->set_tim_lock);
  2514. if (!entry)
  2515. break;
  2516. PDEBUG(DEBUG_PS2, "%s: handle_set_tim_queue: aid=%d set=%d\n",
  2517. local->dev->name, entry->aid, entry->set);
  2518. val = entry->aid;
  2519. if (entry->set)
  2520. val |= 0x8000;
  2521. if (hostap_set_word(local->dev, HFA384X_RID_CNFTIMCTRL, val)) {
  2522. printk(KERN_DEBUG "%s: set_tim failed (aid=%d "
  2523. "set=%d)\n",
  2524. local->dev->name, entry->aid, entry->set);
  2525. }
  2526. kfree(entry);
  2527. }
  2528. }
  2529. static void prism2_clear_set_tim_queue(local_info_t *local)
  2530. {
  2531. struct list_head *ptr, *n;
  2532. list_for_each_safe(ptr, n, &local->set_tim_list) {
  2533. struct set_tim_data *entry;
  2534. entry = list_entry(ptr, struct set_tim_data, list);
  2535. list_del(&entry->list);
  2536. kfree(entry);
  2537. }
  2538. }
  2539. /*
  2540. * HostAP uses two layers of net devices, where the inner
  2541. * layer gets called all the time from the outer layer.
  2542. * This is a natural nesting, which needs a split lock type.
  2543. */
  2544. static struct lock_class_key hostap_netdev_xmit_lock_key;
  2545. static struct lock_class_key hostap_netdev_addr_lock_key;
  2546. static void prism2_set_lockdep_class_one(struct net_device *dev,
  2547. struct netdev_queue *txq,
  2548. void *_unused)
  2549. {
  2550. lockdep_set_class(&txq->_xmit_lock,
  2551. &hostap_netdev_xmit_lock_key);
  2552. }
  2553. static void prism2_set_lockdep_class(struct net_device *dev)
  2554. {
  2555. lockdep_set_class(&dev->addr_list_lock,
  2556. &hostap_netdev_addr_lock_key);
  2557. netdev_for_each_tx_queue(dev, prism2_set_lockdep_class_one, NULL);
  2558. }
  2559. static struct net_device *
  2560. prism2_init_local_data(struct prism2_helper_functions *funcs, int card_idx,
  2561. struct device *sdev)
  2562. {
  2563. struct net_device *dev;
  2564. struct hostap_interface *iface;
  2565. struct local_info *local;
  2566. int len, i, ret;
  2567. if (funcs == NULL)
  2568. return NULL;
  2569. len = strlen(dev_template);
  2570. if (len >= IFNAMSIZ || strstr(dev_template, "%d") == NULL) {
  2571. printk(KERN_WARNING "hostap: Invalid dev_template='%s'\n",
  2572. dev_template);
  2573. return NULL;
  2574. }
  2575. len = sizeof(struct hostap_interface) +
  2576. 3 + sizeof(struct local_info) +
  2577. 3 + sizeof(struct ap_data);
  2578. dev = alloc_etherdev(len);
  2579. if (dev == NULL)
  2580. return NULL;
  2581. iface = netdev_priv(dev);
  2582. local = (struct local_info *) ((((long) (iface + 1)) + 3) & ~3);
  2583. local->ap = (struct ap_data *) ((((long) (local + 1)) + 3) & ~3);
  2584. local->dev = iface->dev = dev;
  2585. iface->local = local;
  2586. iface->type = HOSTAP_INTERFACE_MASTER;
  2587. INIT_LIST_HEAD(&local->hostap_interfaces);
  2588. local->hw_module = THIS_MODULE;
  2589. #ifdef PRISM2_IO_DEBUG
  2590. local->io_debug_enabled = 1;
  2591. #endif /* PRISM2_IO_DEBUG */
  2592. local->func = funcs;
  2593. local->func->cmd = hfa384x_cmd;
  2594. local->func->read_regs = hfa384x_read_regs;
  2595. local->func->get_rid = hfa384x_get_rid;
  2596. local->func->set_rid = hfa384x_set_rid;
  2597. local->func->hw_enable = prism2_hw_enable;
  2598. local->func->hw_config = prism2_hw_config;
  2599. local->func->hw_reset = prism2_hw_reset;
  2600. local->func->hw_shutdown = prism2_hw_shutdown;
  2601. local->func->reset_port = prism2_reset_port;
  2602. local->func->schedule_reset = prism2_schedule_reset;
  2603. #ifdef PRISM2_DOWNLOAD_SUPPORT
  2604. local->func->read_aux_fops = &prism2_download_aux_dump_proc_fops;
  2605. local->func->download = prism2_download;
  2606. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  2607. local->func->tx = prism2_tx_80211;
  2608. local->func->set_tim = prism2_set_tim;
  2609. local->func->need_tx_headroom = 0; /* no need to add txdesc in
  2610. * skb->data (FIX: maybe for DMA bus
  2611. * mastering? */
  2612. local->mtu = mtu;
  2613. rwlock_init(&local->iface_lock);
  2614. spin_lock_init(&local->txfidlock);
  2615. spin_lock_init(&local->cmdlock);
  2616. spin_lock_init(&local->baplock);
  2617. spin_lock_init(&local->lock);
  2618. spin_lock_init(&local->irq_init_lock);
  2619. mutex_init(&local->rid_bap_mtx);
  2620. if (card_idx < 0 || card_idx >= MAX_PARM_DEVICES)
  2621. card_idx = 0;
  2622. local->card_idx = card_idx;
  2623. len = strlen(essid);
  2624. memcpy(local->essid, essid,
  2625. len > MAX_SSID_LEN ? MAX_SSID_LEN : len);
  2626. local->essid[MAX_SSID_LEN] = '\0';
  2627. i = GET_INT_PARM(iw_mode, card_idx);
  2628. if ((i >= IW_MODE_ADHOC && i <= IW_MODE_REPEAT) ||
  2629. i == IW_MODE_MONITOR) {
  2630. local->iw_mode = i;
  2631. } else {
  2632. printk(KERN_WARNING "prism2: Unknown iw_mode %d; using "
  2633. "IW_MODE_MASTER\n", i);
  2634. local->iw_mode = IW_MODE_MASTER;
  2635. }
  2636. local->channel = GET_INT_PARM(channel, card_idx);
  2637. local->beacon_int = GET_INT_PARM(beacon_int, card_idx);
  2638. local->dtim_period = GET_INT_PARM(dtim_period, card_idx);
  2639. local->wds_max_connections = 16;
  2640. local->tx_control = HFA384X_TX_CTRL_FLAGS;
  2641. local->manual_retry_count = -1;
  2642. local->rts_threshold = 2347;
  2643. local->fragm_threshold = 2346;
  2644. local->rssi_to_dBm = 100; /* default; to be overriden by
  2645. * cnfDbmAdjust, if available */
  2646. local->auth_algs = PRISM2_AUTH_OPEN | PRISM2_AUTH_SHARED_KEY;
  2647. local->sram_type = -1;
  2648. local->scan_channel_mask = 0xffff;
  2649. local->monitor_type = PRISM2_MONITOR_RADIOTAP;
  2650. /* Initialize task queue structures */
  2651. INIT_WORK(&local->reset_queue, handle_reset_queue);
  2652. INIT_WORK(&local->set_multicast_list_queue,
  2653. hostap_set_multicast_list_queue);
  2654. INIT_WORK(&local->set_tim_queue, handle_set_tim_queue);
  2655. INIT_LIST_HEAD(&local->set_tim_list);
  2656. spin_lock_init(&local->set_tim_lock);
  2657. INIT_WORK(&local->comms_qual_update, handle_comms_qual_update);
  2658. /* Initialize tasklets for handling hardware IRQ related operations
  2659. * outside hw IRQ handler */
  2660. #define HOSTAP_TASKLET_INIT(q, f, d) \
  2661. do { memset((q), 0, sizeof(*(q))); (q)->func = (f); (q)->data = (d); } \
  2662. while (0)
  2663. HOSTAP_TASKLET_INIT(&local->bap_tasklet, hostap_bap_tasklet,
  2664. (unsigned long) local);
  2665. HOSTAP_TASKLET_INIT(&local->info_tasklet, hostap_info_tasklet,
  2666. (unsigned long) local);
  2667. hostap_info_init(local);
  2668. HOSTAP_TASKLET_INIT(&local->rx_tasklet,
  2669. hostap_rx_tasklet, (unsigned long) local);
  2670. skb_queue_head_init(&local->rx_list);
  2671. HOSTAP_TASKLET_INIT(&local->sta_tx_exc_tasklet,
  2672. hostap_sta_tx_exc_tasklet, (unsigned long) local);
  2673. skb_queue_head_init(&local->sta_tx_exc_list);
  2674. INIT_LIST_HEAD(&local->cmd_queue);
  2675. init_waitqueue_head(&local->hostscan_wq);
  2676. lib80211_crypt_info_init(&local->crypt_info, dev->name, &local->lock);
  2677. timer_setup(&local->passive_scan_timer, hostap_passive_scan, 0);
  2678. timer_setup(&local->tick_timer, hostap_tick_timer, 0);
  2679. local->tick_timer.expires = jiffies + 2 * HZ;
  2680. add_timer(&local->tick_timer);
  2681. INIT_LIST_HEAD(&local->bss_list);
  2682. hostap_setup_dev(dev, local, HOSTAP_INTERFACE_MASTER);
  2683. dev->type = ARPHRD_IEEE80211;
  2684. dev->header_ops = &hostap_80211_ops;
  2685. rtnl_lock();
  2686. ret = dev_alloc_name(dev, "wifi%d");
  2687. SET_NETDEV_DEV(dev, sdev);
  2688. if (ret >= 0)
  2689. ret = register_netdevice(dev);
  2690. prism2_set_lockdep_class(dev);
  2691. rtnl_unlock();
  2692. if (ret < 0) {
  2693. printk(KERN_WARNING "%s: register netdevice failed!\n",
  2694. dev_info);
  2695. goto fail;
  2696. }
  2697. printk(KERN_INFO "%s: Registered netdevice %s\n", dev_info, dev->name);
  2698. hostap_init_data(local);
  2699. return dev;
  2700. fail:
  2701. free_netdev(dev);
  2702. return NULL;
  2703. }
  2704. static int hostap_hw_ready(struct net_device *dev)
  2705. {
  2706. struct hostap_interface *iface;
  2707. struct local_info *local;
  2708. iface = netdev_priv(dev);
  2709. local = iface->local;
  2710. local->ddev = hostap_add_interface(local, HOSTAP_INTERFACE_MAIN, 0,
  2711. "", dev_template);
  2712. if (local->ddev) {
  2713. if (local->iw_mode == IW_MODE_INFRA ||
  2714. local->iw_mode == IW_MODE_ADHOC) {
  2715. netif_carrier_off(local->dev);
  2716. netif_carrier_off(local->ddev);
  2717. }
  2718. hostap_init_proc(local);
  2719. #ifndef PRISM2_NO_PROCFS_DEBUG
  2720. proc_create_single_data("registers", 0, local->proc,
  2721. prism2_registers_proc_show, local);
  2722. #endif /* PRISM2_NO_PROCFS_DEBUG */
  2723. hostap_init_ap_proc(local);
  2724. return 0;
  2725. }
  2726. return -1;
  2727. }
  2728. static void prism2_free_local_data(struct net_device *dev)
  2729. {
  2730. struct hostap_tx_callback_info *tx_cb, *tx_cb_prev;
  2731. int i;
  2732. struct hostap_interface *iface;
  2733. struct local_info *local;
  2734. struct list_head *ptr, *n;
  2735. if (dev == NULL)
  2736. return;
  2737. iface = netdev_priv(dev);
  2738. local = iface->local;
  2739. /* Unregister all netdevs before freeing local data. */
  2740. list_for_each_safe(ptr, n, &local->hostap_interfaces) {
  2741. iface = list_entry(ptr, struct hostap_interface, list);
  2742. if (iface->type == HOSTAP_INTERFACE_MASTER) {
  2743. /* special handling for this interface below */
  2744. continue;
  2745. }
  2746. hostap_remove_interface(iface->dev, 0, 1);
  2747. }
  2748. unregister_netdev(local->dev);
  2749. flush_work(&local->reset_queue);
  2750. flush_work(&local->set_multicast_list_queue);
  2751. flush_work(&local->set_tim_queue);
  2752. #ifndef PRISM2_NO_STATION_MODES
  2753. flush_work(&local->info_queue);
  2754. #endif
  2755. flush_work(&local->comms_qual_update);
  2756. lib80211_crypt_info_free(&local->crypt_info);
  2757. if (timer_pending(&local->passive_scan_timer))
  2758. del_timer(&local->passive_scan_timer);
  2759. if (timer_pending(&local->tick_timer))
  2760. del_timer(&local->tick_timer);
  2761. prism2_clear_cmd_queue(local);
  2762. skb_queue_purge(&local->info_list);
  2763. skb_queue_purge(&local->rx_list);
  2764. skb_queue_purge(&local->sta_tx_exc_list);
  2765. if (local->dev_enabled)
  2766. prism2_callback(local, PRISM2_CALLBACK_DISABLE);
  2767. if (local->ap != NULL)
  2768. hostap_free_data(local->ap);
  2769. #ifndef PRISM2_NO_PROCFS_DEBUG
  2770. if (local->proc != NULL)
  2771. remove_proc_entry("registers", local->proc);
  2772. #endif /* PRISM2_NO_PROCFS_DEBUG */
  2773. hostap_remove_proc(local);
  2774. tx_cb = local->tx_callback;
  2775. while (tx_cb != NULL) {
  2776. tx_cb_prev = tx_cb;
  2777. tx_cb = tx_cb->next;
  2778. kfree(tx_cb_prev);
  2779. }
  2780. hostap_set_hostapd(local, 0, 0);
  2781. hostap_set_hostapd_sta(local, 0, 0);
  2782. for (i = 0; i < PRISM2_FRAG_CACHE_LEN; i++) {
  2783. if (local->frag_cache[i].skb != NULL)
  2784. dev_kfree_skb(local->frag_cache[i].skb);
  2785. }
  2786. #ifdef PRISM2_DOWNLOAD_SUPPORT
  2787. prism2_download_free_data(local->dl_pri);
  2788. prism2_download_free_data(local->dl_sec);
  2789. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  2790. prism2_clear_set_tim_queue(local);
  2791. list_for_each_safe(ptr, n, &local->bss_list) {
  2792. struct hostap_bss_info *bss =
  2793. list_entry(ptr, struct hostap_bss_info, list);
  2794. kfree(bss);
  2795. }
  2796. kfree(local->pda);
  2797. kfree(local->last_scan_results);
  2798. kfree(local->generic_elem);
  2799. free_netdev(local->dev);
  2800. }
  2801. #if (defined(PRISM2_PCI) && defined(CONFIG_PM)) || defined(PRISM2_PCCARD)
  2802. static void prism2_suspend(struct net_device *dev)
  2803. {
  2804. struct hostap_interface *iface;
  2805. struct local_info *local;
  2806. union iwreq_data wrqu;
  2807. iface = netdev_priv(dev);
  2808. local = iface->local;
  2809. /* Send disconnect event, e.g., to trigger reassociation after resume
  2810. * if wpa_supplicant is used. */
  2811. memset(&wrqu, 0, sizeof(wrqu));
  2812. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  2813. wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
  2814. /* Disable hardware and firmware */
  2815. prism2_hw_shutdown(dev, 0);
  2816. }
  2817. #endif /* (PRISM2_PCI && CONFIG_PM) || PRISM2_PCCARD */
  2818. /* These might at some point be compiled separately and used as separate
  2819. * kernel modules or linked into one */
  2820. #ifdef PRISM2_DOWNLOAD_SUPPORT
  2821. #include "hostap_download.c"
  2822. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  2823. #ifdef PRISM2_CALLBACK
  2824. /* External hostap_callback.c file can be used to, e.g., blink activity led.
  2825. * This can use platform specific code and must define prism2_callback()
  2826. * function (if PRISM2_CALLBACK is not defined, these function calls are not
  2827. * used. */
  2828. #include "hostap_callback.c"
  2829. #endif /* PRISM2_CALLBACK */