hostap_hw.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410
  1. /*
  2. * Host AP (software wireless LAN access point) driver for
  3. * Intersil Prism2/2.5/3.
  4. *
  5. * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
  6. * <j@w1.fi>
  7. * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation. See README and COPYING for
  12. * more details.
  13. *
  14. * FIX:
  15. * - there is currently no way of associating TX packets to correct wds device
  16. * when TX Exc/OK event occurs, so all tx_packets and some
  17. * tx_errors/tx_dropped are added to the main netdevice; using sw_support
  18. * field in txdesc might be used to fix this (using Alloc event to increment
  19. * tx_packets would need some further info in txfid table)
  20. *
  21. * Buffer Access Path (BAP) usage:
  22. * Prism2 cards have two separate BAPs for accessing the card memory. These
  23. * should allow concurrent access to two different frames and the driver
  24. * previously used BAP0 for sending data and BAP1 for receiving data.
  25. * However, there seems to be number of issues with concurrent access and at
  26. * least one know hardware bug in using BAP0 and BAP1 concurrently with PCI
  27. * Prism2.5. Therefore, the driver now only uses BAP0 for moving data between
  28. * host and card memories. BAP0 accesses are protected with local->baplock
  29. * (spin_lock_bh) to prevent concurrent use.
  30. */
  31. #include <asm/delay.h>
  32. #include <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 u16 hfa384x_read_reg(struct net_device *dev, u16 reg)
  125. {
  126. return HFA384X_INW(reg);
  127. }
  128. static void hfa384x_read_regs(struct net_device *dev,
  129. struct hfa384x_regs *regs)
  130. {
  131. regs->cmd = HFA384X_INW(HFA384X_CMD_OFF);
  132. regs->evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
  133. regs->offset0 = HFA384X_INW(HFA384X_OFFSET0_OFF);
  134. regs->offset1 = HFA384X_INW(HFA384X_OFFSET1_OFF);
  135. regs->swsupport0 = HFA384X_INW(HFA384X_SWSUPPORT0_OFF);
  136. }
  137. /**
  138. * __hostap_cmd_queue_free - Free Prism2 command queue entry (private)
  139. * @local: pointer to private Host AP driver data
  140. * @entry: Prism2 command queue entry to be freed
  141. * @del_req: request the entry to be removed
  142. *
  143. * Internal helper function for freeing Prism2 command queue entries.
  144. * Caller must have acquired local->cmdlock before calling this function.
  145. */
  146. static inline void __hostap_cmd_queue_free(local_info_t *local,
  147. struct hostap_cmd_queue *entry,
  148. int del_req)
  149. {
  150. if (del_req) {
  151. entry->del_req = 1;
  152. if (!list_empty(&entry->list)) {
  153. list_del_init(&entry->list);
  154. local->cmd_queue_len--;
  155. }
  156. }
  157. if (refcount_dec_and_test(&entry->usecnt) && entry->del_req)
  158. kfree(entry);
  159. }
  160. /**
  161. * hostap_cmd_queue_free - Free Prism2 command queue entry
  162. * @local: pointer to private Host AP driver data
  163. * @entry: Prism2 command queue entry to be freed
  164. * @del_req: request the entry to be removed
  165. *
  166. * Free a Prism2 command queue entry.
  167. */
  168. static inline void hostap_cmd_queue_free(local_info_t *local,
  169. struct hostap_cmd_queue *entry,
  170. int del_req)
  171. {
  172. unsigned long flags;
  173. spin_lock_irqsave(&local->cmdlock, flags);
  174. __hostap_cmd_queue_free(local, entry, del_req);
  175. spin_unlock_irqrestore(&local->cmdlock, flags);
  176. }
  177. /**
  178. * prism2_clear_cmd_queue - Free all pending Prism2 command queue entries
  179. * @local: pointer to private Host AP driver data
  180. */
  181. static void prism2_clear_cmd_queue(local_info_t *local)
  182. {
  183. struct list_head *ptr, *n;
  184. unsigned long flags;
  185. struct hostap_cmd_queue *entry;
  186. spin_lock_irqsave(&local->cmdlock, flags);
  187. list_for_each_safe(ptr, n, &local->cmd_queue) {
  188. entry = list_entry(ptr, struct hostap_cmd_queue, list);
  189. refcount_inc(&entry->usecnt);
  190. printk(KERN_DEBUG "%s: removed pending cmd_queue entry "
  191. "(type=%d, cmd=0x%04x, param0=0x%04x)\n",
  192. local->dev->name, entry->type, entry->cmd,
  193. entry->param0);
  194. __hostap_cmd_queue_free(local, entry, 1);
  195. }
  196. if (local->cmd_queue_len) {
  197. /* This should not happen; print debug message and clear
  198. * queue length. */
  199. printk(KERN_DEBUG "%s: cmd_queue_len (%d) not zero after "
  200. "flush\n", local->dev->name, local->cmd_queue_len);
  201. local->cmd_queue_len = 0;
  202. }
  203. spin_unlock_irqrestore(&local->cmdlock, flags);
  204. }
  205. /**
  206. * hfa384x_cmd_issue - Issue a Prism2 command to the hardware
  207. * @dev: pointer to net_device
  208. * @entry: Prism2 command queue entry to be issued
  209. */
  210. static int hfa384x_cmd_issue(struct net_device *dev,
  211. struct hostap_cmd_queue *entry)
  212. {
  213. struct hostap_interface *iface;
  214. local_info_t *local;
  215. int tries;
  216. u16 reg;
  217. unsigned long flags;
  218. iface = netdev_priv(dev);
  219. local = iface->local;
  220. if (local->func->card_present && !local->func->card_present(local))
  221. return -ENODEV;
  222. if (entry->issued) {
  223. printk(KERN_DEBUG "%s: driver bug - re-issuing command @%p\n",
  224. dev->name, entry);
  225. }
  226. /* wait until busy bit is clear; this should always be clear since the
  227. * commands are serialized */
  228. tries = HFA384X_CMD_BUSY_TIMEOUT;
  229. while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
  230. tries--;
  231. udelay(1);
  232. }
  233. #ifndef final_version
  234. if (tries != HFA384X_CMD_BUSY_TIMEOUT) {
  235. prism2_io_debug_error(dev, 1);
  236. printk(KERN_DEBUG "%s: hfa384x_cmd_issue: cmd reg was busy "
  237. "for %d usec\n", dev->name,
  238. HFA384X_CMD_BUSY_TIMEOUT - tries);
  239. }
  240. #endif
  241. if (tries == 0) {
  242. reg = HFA384X_INW(HFA384X_CMD_OFF);
  243. prism2_io_debug_error(dev, 2);
  244. printk(KERN_DEBUG "%s: hfa384x_cmd_issue - timeout - "
  245. "reg=0x%04x\n", dev->name, reg);
  246. return -ETIMEDOUT;
  247. }
  248. /* write command */
  249. spin_lock_irqsave(&local->cmdlock, flags);
  250. HFA384X_OUTW(entry->param0, HFA384X_PARAM0_OFF);
  251. HFA384X_OUTW(entry->param1, HFA384X_PARAM1_OFF);
  252. HFA384X_OUTW(entry->cmd, HFA384X_CMD_OFF);
  253. entry->issued = 1;
  254. spin_unlock_irqrestore(&local->cmdlock, flags);
  255. return 0;
  256. }
  257. /**
  258. * hfa384x_cmd - Issue a Prism2 command and wait (sleep) for completion
  259. * @dev: pointer to net_device
  260. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  261. * @param0: value for Param0 register
  262. * @param1: value for Param1 register (pointer; %NULL if not used)
  263. * @resp0: pointer for Resp0 data or %NULL if Resp0 is not needed
  264. *
  265. * Issue given command (possibly after waiting in command queue) and sleep
  266. * until the command is completed (or timed out or interrupted). This can be
  267. * called only from user process context.
  268. */
  269. static int hfa384x_cmd(struct net_device *dev, u16 cmd, u16 param0,
  270. u16 *param1, u16 *resp0)
  271. {
  272. struct hostap_interface *iface;
  273. local_info_t *local;
  274. int err, res, issue, issued = 0;
  275. unsigned long flags;
  276. struct hostap_cmd_queue *entry;
  277. DECLARE_WAITQUEUE(wait, current);
  278. iface = netdev_priv(dev);
  279. local = iface->local;
  280. if (in_interrupt()) {
  281. printk(KERN_DEBUG "%s: hfa384x_cmd called from interrupt "
  282. "context\n", dev->name);
  283. return -1;
  284. }
  285. if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN) {
  286. printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
  287. dev->name);
  288. return -1;
  289. }
  290. if (signal_pending(current))
  291. return -EINTR;
  292. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  293. if (entry == NULL)
  294. return -ENOMEM;
  295. refcount_set(&entry->usecnt, 1);
  296. entry->type = CMD_SLEEP;
  297. entry->cmd = cmd;
  298. entry->param0 = param0;
  299. if (param1)
  300. entry->param1 = *param1;
  301. init_waitqueue_head(&entry->compl);
  302. /* prepare to wait for command completion event, but do not sleep yet
  303. */
  304. add_wait_queue(&entry->compl, &wait);
  305. set_current_state(TASK_INTERRUPTIBLE);
  306. spin_lock_irqsave(&local->cmdlock, flags);
  307. issue = list_empty(&local->cmd_queue);
  308. if (issue)
  309. entry->issuing = 1;
  310. list_add_tail(&entry->list, &local->cmd_queue);
  311. local->cmd_queue_len++;
  312. spin_unlock_irqrestore(&local->cmdlock, flags);
  313. err = 0;
  314. if (!issue)
  315. goto wait_completion;
  316. if (signal_pending(current))
  317. err = -EINTR;
  318. if (!err) {
  319. if (hfa384x_cmd_issue(dev, entry))
  320. err = -ETIMEDOUT;
  321. else
  322. issued = 1;
  323. }
  324. wait_completion:
  325. if (!err && entry->type != CMD_COMPLETED) {
  326. /* sleep until command is completed or timed out */
  327. res = schedule_timeout(2 * HZ);
  328. } else
  329. res = -1;
  330. if (!err && signal_pending(current))
  331. err = -EINTR;
  332. if (err && issued) {
  333. /* the command was issued, so a CmdCompl event should occur
  334. * soon; however, there's a pending signal and
  335. * schedule_timeout() would be interrupted; wait a short period
  336. * of time to avoid removing entry from the list before
  337. * CmdCompl event */
  338. udelay(300);
  339. }
  340. set_current_state(TASK_RUNNING);
  341. remove_wait_queue(&entry->compl, &wait);
  342. /* If entry->list is still in the list, it must be removed
  343. * first and in this case prism2_cmd_ev() does not yet have
  344. * local reference to it, and the data can be kfree()'d
  345. * here. If the command completion event is still generated,
  346. * it will be assigned to next (possibly) pending command, but
  347. * the driver will reset the card anyway due to timeout
  348. *
  349. * If the entry is not in the list prism2_cmd_ev() has a local
  350. * reference to it, but keeps cmdlock as long as the data is
  351. * needed, so the data can be kfree()'d here. */
  352. /* FIX: if the entry->list is in the list, it has not been completed
  353. * yet, so removing it here is somewhat wrong.. this could cause
  354. * references to freed memory and next list_del() causing NULL pointer
  355. * dereference.. it would probably be better to leave the entry in the
  356. * list and the list should be emptied during hw reset */
  357. spin_lock_irqsave(&local->cmdlock, flags);
  358. if (!list_empty(&entry->list)) {
  359. printk(KERN_DEBUG "%s: hfa384x_cmd: entry still in list? "
  360. "(entry=%p, type=%d, res=%d)\n", dev->name, entry,
  361. entry->type, res);
  362. list_del_init(&entry->list);
  363. local->cmd_queue_len--;
  364. }
  365. spin_unlock_irqrestore(&local->cmdlock, flags);
  366. if (err) {
  367. printk(KERN_DEBUG "%s: hfa384x_cmd: interrupted; err=%d\n",
  368. dev->name, err);
  369. res = err;
  370. goto done;
  371. }
  372. if (entry->type != CMD_COMPLETED) {
  373. u16 reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
  374. printk(KERN_DEBUG "%s: hfa384x_cmd: command was not "
  375. "completed (res=%d, entry=%p, type=%d, cmd=0x%04x, "
  376. "param0=0x%04x, EVSTAT=%04x INTEN=%04x)\n", dev->name,
  377. res, entry, entry->type, entry->cmd, entry->param0, reg,
  378. HFA384X_INW(HFA384X_INTEN_OFF));
  379. if (reg & HFA384X_EV_CMD) {
  380. /* Command completion event is pending, but the
  381. * interrupt was not delivered - probably an issue
  382. * with pcmcia-cs configuration. */
  383. printk(KERN_WARNING "%s: interrupt delivery does not "
  384. "seem to work\n", dev->name);
  385. }
  386. prism2_io_debug_error(dev, 3);
  387. res = -ETIMEDOUT;
  388. goto done;
  389. }
  390. if (resp0 != NULL)
  391. *resp0 = entry->resp0;
  392. #ifndef final_version
  393. if (entry->res) {
  394. printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x, "
  395. "resp0=0x%04x\n",
  396. dev->name, cmd, entry->res, entry->resp0);
  397. }
  398. #endif /* final_version */
  399. res = entry->res;
  400. done:
  401. hostap_cmd_queue_free(local, entry, 1);
  402. return res;
  403. }
  404. /**
  405. * hfa384x_cmd_callback - Issue a Prism2 command; callback when completed
  406. * @dev: pointer to net_device
  407. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  408. * @param0: value for Param0 register
  409. * @callback: command completion callback function (%NULL = no callback)
  410. * @context: context data to be given to the callback function
  411. *
  412. * Issue given command (possibly after waiting in command queue) and use
  413. * callback function to indicate command completion. This can be called both
  414. * from user and interrupt context. The callback function will be called in
  415. * hardware IRQ context. It can be %NULL, when no function is called when
  416. * command is completed.
  417. */
  418. static int hfa384x_cmd_callback(struct net_device *dev, u16 cmd, u16 param0,
  419. void (*callback)(struct net_device *dev,
  420. long context, u16 resp0,
  421. u16 status),
  422. long context)
  423. {
  424. struct hostap_interface *iface;
  425. local_info_t *local;
  426. int issue, ret;
  427. unsigned long flags;
  428. struct hostap_cmd_queue *entry;
  429. iface = netdev_priv(dev);
  430. local = iface->local;
  431. if (local->cmd_queue_len >= HOSTAP_CMD_QUEUE_MAX_LEN + 2) {
  432. printk(KERN_DEBUG "%s: hfa384x_cmd: cmd_queue full\n",
  433. dev->name);
  434. return -1;
  435. }
  436. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  437. if (entry == NULL)
  438. return -ENOMEM;
  439. refcount_set(&entry->usecnt, 1);
  440. entry->type = CMD_CALLBACK;
  441. entry->cmd = cmd;
  442. entry->param0 = param0;
  443. entry->callback = callback;
  444. entry->context = context;
  445. spin_lock_irqsave(&local->cmdlock, flags);
  446. issue = list_empty(&local->cmd_queue);
  447. if (issue)
  448. entry->issuing = 1;
  449. list_add_tail(&entry->list, &local->cmd_queue);
  450. local->cmd_queue_len++;
  451. spin_unlock_irqrestore(&local->cmdlock, flags);
  452. if (issue && hfa384x_cmd_issue(dev, entry))
  453. ret = -ETIMEDOUT;
  454. else
  455. ret = 0;
  456. hostap_cmd_queue_free(local, entry, ret);
  457. return ret;
  458. }
  459. /**
  460. * __hfa384x_cmd_no_wait - Issue a Prism2 command (private)
  461. * @dev: pointer to net_device
  462. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  463. * @param0: value for Param0 register
  464. * @io_debug_num: I/O debug error number
  465. *
  466. * Shared helper function for hfa384x_cmd_wait() and hfa384x_cmd_no_wait().
  467. */
  468. static int __hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd, u16 param0,
  469. int io_debug_num)
  470. {
  471. int tries;
  472. u16 reg;
  473. /* wait until busy bit is clear; this should always be clear since the
  474. * commands are serialized */
  475. tries = HFA384X_CMD_BUSY_TIMEOUT;
  476. while (HFA384X_INW(HFA384X_CMD_OFF) & HFA384X_CMD_BUSY && tries > 0) {
  477. tries--;
  478. udelay(1);
  479. }
  480. if (tries == 0) {
  481. reg = HFA384X_INW(HFA384X_CMD_OFF);
  482. prism2_io_debug_error(dev, io_debug_num);
  483. printk(KERN_DEBUG "%s: __hfa384x_cmd_no_wait(%d) - timeout - "
  484. "reg=0x%04x\n", dev->name, io_debug_num, reg);
  485. return -ETIMEDOUT;
  486. }
  487. /* write command */
  488. HFA384X_OUTW(param0, HFA384X_PARAM0_OFF);
  489. HFA384X_OUTW(cmd, HFA384X_CMD_OFF);
  490. return 0;
  491. }
  492. /**
  493. * hfa384x_cmd_wait - Issue a Prism2 command and busy wait for completion
  494. * @dev: pointer to net_device
  495. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  496. * @param0: value for Param0 register
  497. */
  498. static int hfa384x_cmd_wait(struct net_device *dev, u16 cmd, u16 param0)
  499. {
  500. int res, tries;
  501. u16 reg;
  502. res = __hfa384x_cmd_no_wait(dev, cmd, param0, 4);
  503. if (res)
  504. return res;
  505. /* wait for command completion */
  506. if ((cmd & HFA384X_CMDCODE_MASK) == HFA384X_CMDCODE_DOWNLOAD)
  507. tries = HFA384X_DL_COMPL_TIMEOUT;
  508. else
  509. tries = HFA384X_CMD_COMPL_TIMEOUT;
  510. while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
  511. tries > 0) {
  512. tries--;
  513. udelay(10);
  514. }
  515. if (tries == 0) {
  516. reg = HFA384X_INW(HFA384X_EVSTAT_OFF);
  517. prism2_io_debug_error(dev, 5);
  518. printk(KERN_DEBUG "%s: hfa384x_cmd_wait - timeout2 - "
  519. "reg=0x%04x\n", dev->name, reg);
  520. return -ETIMEDOUT;
  521. }
  522. res = (HFA384X_INW(HFA384X_STATUS_OFF) &
  523. (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) | BIT(9) |
  524. BIT(8))) >> 8;
  525. #ifndef final_version
  526. if (res) {
  527. printk(KERN_DEBUG "%s: CMD=0x%04x => res=0x%02x\n",
  528. dev->name, cmd, res);
  529. }
  530. #endif
  531. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  532. return res;
  533. }
  534. /**
  535. * hfa384x_cmd_no_wait - Issue a Prism2 command; do not wait for completion
  536. * @dev: pointer to net_device
  537. * @cmd: Prism2 command code (HFA384X_CMD_CODE_*)
  538. * @param0: value for Param0 register
  539. */
  540. static inline int hfa384x_cmd_no_wait(struct net_device *dev, u16 cmd,
  541. u16 param0)
  542. {
  543. return __hfa384x_cmd_no_wait(dev, cmd, param0, 6);
  544. }
  545. /**
  546. * prism2_cmd_ev - Prism2 command completion event handler
  547. * @dev: pointer to net_device
  548. *
  549. * Interrupt handler for command completion events. Called by the main
  550. * interrupt handler in hardware IRQ context. Read Resp0 and status registers
  551. * from the hardware and ACK the event. Depending on the issued command type
  552. * either wake up the sleeping process that is waiting for command completion
  553. * or call the callback function. Issue the next command, if one is pending.
  554. */
  555. static void prism2_cmd_ev(struct net_device *dev)
  556. {
  557. struct hostap_interface *iface;
  558. local_info_t *local;
  559. struct hostap_cmd_queue *entry = NULL;
  560. iface = netdev_priv(dev);
  561. local = iface->local;
  562. spin_lock(&local->cmdlock);
  563. if (!list_empty(&local->cmd_queue)) {
  564. entry = list_entry(local->cmd_queue.next,
  565. struct hostap_cmd_queue, list);
  566. refcount_inc(&entry->usecnt);
  567. list_del_init(&entry->list);
  568. local->cmd_queue_len--;
  569. if (!entry->issued) {
  570. printk(KERN_DEBUG "%s: Command completion event, but "
  571. "cmd not issued\n", dev->name);
  572. __hostap_cmd_queue_free(local, entry, 1);
  573. entry = NULL;
  574. }
  575. }
  576. spin_unlock(&local->cmdlock);
  577. if (!entry) {
  578. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  579. printk(KERN_DEBUG "%s: Command completion event, but no "
  580. "pending commands\n", dev->name);
  581. return;
  582. }
  583. entry->resp0 = HFA384X_INW(HFA384X_RESP0_OFF);
  584. entry->res = (HFA384X_INW(HFA384X_STATUS_OFF) &
  585. (BIT(14) | BIT(13) | BIT(12) | BIT(11) | BIT(10) |
  586. BIT(9) | BIT(8))) >> 8;
  587. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  588. /* TODO: rest of the CmdEv handling could be moved to tasklet */
  589. if (entry->type == CMD_SLEEP) {
  590. entry->type = CMD_COMPLETED;
  591. wake_up_interruptible(&entry->compl);
  592. } else if (entry->type == CMD_CALLBACK) {
  593. if (entry->callback)
  594. entry->callback(dev, entry->context, entry->resp0,
  595. entry->res);
  596. } else {
  597. printk(KERN_DEBUG "%s: Invalid command completion type %d\n",
  598. dev->name, entry->type);
  599. }
  600. hostap_cmd_queue_free(local, entry, 1);
  601. /* issue next command, if pending */
  602. entry = NULL;
  603. spin_lock(&local->cmdlock);
  604. if (!list_empty(&local->cmd_queue)) {
  605. entry = list_entry(local->cmd_queue.next,
  606. struct hostap_cmd_queue, list);
  607. if (entry->issuing) {
  608. /* hfa384x_cmd() has already started issuing this
  609. * command, so do not start here */
  610. entry = NULL;
  611. }
  612. if (entry)
  613. refcount_inc(&entry->usecnt);
  614. }
  615. spin_unlock(&local->cmdlock);
  616. if (entry) {
  617. /* issue next command; if command issuing fails, remove the
  618. * entry from cmd_queue */
  619. int res = hfa384x_cmd_issue(dev, entry);
  620. spin_lock(&local->cmdlock);
  621. __hostap_cmd_queue_free(local, entry, res);
  622. spin_unlock(&local->cmdlock);
  623. }
  624. }
  625. static int hfa384x_wait_offset(struct net_device *dev, u16 o_off)
  626. {
  627. int tries = HFA384X_BAP_BUSY_TIMEOUT;
  628. int res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
  629. while (res && tries > 0) {
  630. tries--;
  631. udelay(1);
  632. res = HFA384X_INW(o_off) & HFA384X_OFFSET_BUSY;
  633. }
  634. return res;
  635. }
  636. /* Offset must be even */
  637. static int hfa384x_setup_bap(struct net_device *dev, u16 bap, u16 id,
  638. int offset)
  639. {
  640. u16 o_off, s_off;
  641. int ret = 0;
  642. if (offset % 2 || bap > 1)
  643. return -EINVAL;
  644. if (bap == BAP1) {
  645. o_off = HFA384X_OFFSET1_OFF;
  646. s_off = HFA384X_SELECT1_OFF;
  647. } else {
  648. o_off = HFA384X_OFFSET0_OFF;
  649. s_off = HFA384X_SELECT0_OFF;
  650. }
  651. if (hfa384x_wait_offset(dev, o_off)) {
  652. prism2_io_debug_error(dev, 7);
  653. printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout before\n",
  654. dev->name);
  655. ret = -ETIMEDOUT;
  656. goto out;
  657. }
  658. HFA384X_OUTW(id, s_off);
  659. HFA384X_OUTW(offset, o_off);
  660. if (hfa384x_wait_offset(dev, o_off)) {
  661. prism2_io_debug_error(dev, 8);
  662. printk(KERN_DEBUG "%s: hfa384x_setup_bap - timeout after\n",
  663. dev->name);
  664. ret = -ETIMEDOUT;
  665. goto out;
  666. }
  667. #ifndef final_version
  668. if (HFA384X_INW(o_off) & HFA384X_OFFSET_ERR) {
  669. prism2_io_debug_error(dev, 9);
  670. printk(KERN_DEBUG "%s: hfa384x_setup_bap - offset error "
  671. "(%d,0x04%x,%d); reg=0x%04x\n",
  672. dev->name, bap, id, offset, HFA384X_INW(o_off));
  673. ret = -EINVAL;
  674. }
  675. #endif
  676. out:
  677. return ret;
  678. }
  679. static int hfa384x_get_rid(struct net_device *dev, u16 rid, void *buf, int len,
  680. int exact_len)
  681. {
  682. struct hostap_interface *iface;
  683. local_info_t *local;
  684. int res, rlen = 0;
  685. struct hfa384x_rid_hdr rec;
  686. iface = netdev_priv(dev);
  687. local = iface->local;
  688. if (local->no_pri) {
  689. printk(KERN_DEBUG "%s: cannot get RID %04x (len=%d) - no PRI "
  690. "f/w\n", dev->name, rid, len);
  691. return -ENOTTY; /* Well.. not really correct, but return
  692. * something unique enough.. */
  693. }
  694. if ((local->func->card_present && !local->func->card_present(local)) ||
  695. local->hw_downloading)
  696. return -ENODEV;
  697. res = mutex_lock_interruptible(&local->rid_bap_mtx);
  698. if (res)
  699. return res;
  700. res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS, rid, NULL, NULL);
  701. if (res) {
  702. printk(KERN_DEBUG "%s: hfa384x_get_rid: CMDCODE_ACCESS failed "
  703. "(res=%d, rid=%04x, len=%d)\n",
  704. dev->name, res, rid, len);
  705. mutex_unlock(&local->rid_bap_mtx);
  706. return res;
  707. }
  708. spin_lock_bh(&local->baplock);
  709. res = hfa384x_setup_bap(dev, BAP0, rid, 0);
  710. if (res)
  711. goto unlock;
  712. res = hfa384x_from_bap(dev, BAP0, &rec, sizeof(rec));
  713. if (res)
  714. goto unlock;
  715. if (le16_to_cpu(rec.len) == 0) {
  716. /* RID not available */
  717. res = -ENODATA;
  718. goto unlock;
  719. }
  720. rlen = (le16_to_cpu(rec.len) - 1) * 2;
  721. if (exact_len && rlen != len) {
  722. printk(KERN_DEBUG "%s: hfa384x_get_rid - RID len mismatch: "
  723. "rid=0x%04x, len=%d (expected %d)\n",
  724. dev->name, rid, rlen, len);
  725. res = -ENODATA;
  726. }
  727. res = hfa384x_from_bap(dev, BAP0, buf, len);
  728. unlock:
  729. spin_unlock_bh(&local->baplock);
  730. mutex_unlock(&local->rid_bap_mtx);
  731. if (res) {
  732. if (res != -ENODATA)
  733. printk(KERN_DEBUG "%s: hfa384x_get_rid (rid=%04x, "
  734. "len=%d) - failed - res=%d\n", dev->name, rid,
  735. len, res);
  736. if (res == -ETIMEDOUT)
  737. prism2_hw_reset(dev);
  738. return res;
  739. }
  740. return rlen;
  741. }
  742. static int hfa384x_set_rid(struct net_device *dev, u16 rid, void *buf, int len)
  743. {
  744. struct hostap_interface *iface;
  745. local_info_t *local;
  746. struct hfa384x_rid_hdr rec;
  747. int res;
  748. iface = netdev_priv(dev);
  749. local = iface->local;
  750. if (local->no_pri) {
  751. printk(KERN_DEBUG "%s: cannot set RID %04x (len=%d) - no PRI "
  752. "f/w\n", dev->name, rid, len);
  753. return -ENOTTY; /* Well.. not really correct, but return
  754. * something unique enough.. */
  755. }
  756. if ((local->func->card_present && !local->func->card_present(local)) ||
  757. local->hw_downloading)
  758. return -ENODEV;
  759. rec.rid = cpu_to_le16(rid);
  760. /* RID len in words and +1 for rec.rid */
  761. rec.len = cpu_to_le16(len / 2 + len % 2 + 1);
  762. res = mutex_lock_interruptible(&local->rid_bap_mtx);
  763. if (res)
  764. return res;
  765. spin_lock_bh(&local->baplock);
  766. res = hfa384x_setup_bap(dev, BAP0, rid, 0);
  767. if (!res)
  768. res = hfa384x_to_bap(dev, BAP0, &rec, sizeof(rec));
  769. if (!res)
  770. res = hfa384x_to_bap(dev, BAP0, buf, len);
  771. spin_unlock_bh(&local->baplock);
  772. if (res) {
  773. printk(KERN_DEBUG "%s: hfa384x_set_rid (rid=%04x, len=%d) - "
  774. "failed - res=%d\n", dev->name, rid, len, res);
  775. mutex_unlock(&local->rid_bap_mtx);
  776. return res;
  777. }
  778. res = hfa384x_cmd(dev, HFA384X_CMDCODE_ACCESS_WRITE, rid, NULL, NULL);
  779. mutex_unlock(&local->rid_bap_mtx);
  780. if (res) {
  781. printk(KERN_DEBUG "%s: hfa384x_set_rid: CMDCODE_ACCESS_WRITE "
  782. "failed (res=%d, rid=%04x, len=%d)\n",
  783. dev->name, res, rid, len);
  784. if (res == -ETIMEDOUT)
  785. prism2_hw_reset(dev);
  786. }
  787. return res;
  788. }
  789. static void hfa384x_disable_interrupts(struct net_device *dev)
  790. {
  791. /* disable interrupts and clear event status */
  792. HFA384X_OUTW(0, HFA384X_INTEN_OFF);
  793. HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
  794. }
  795. static void hfa384x_enable_interrupts(struct net_device *dev)
  796. {
  797. /* ack pending events and enable interrupts from selected events */
  798. HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
  799. HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
  800. }
  801. static void hfa384x_events_no_bap0(struct net_device *dev)
  802. {
  803. HFA384X_OUTW(HFA384X_EVENT_MASK & ~HFA384X_BAP0_EVENTS,
  804. HFA384X_INTEN_OFF);
  805. }
  806. static void hfa384x_events_all(struct net_device *dev)
  807. {
  808. HFA384X_OUTW(HFA384X_EVENT_MASK, HFA384X_INTEN_OFF);
  809. }
  810. static void hfa384x_events_only_cmd(struct net_device *dev)
  811. {
  812. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_INTEN_OFF);
  813. }
  814. static u16 hfa384x_allocate_fid(struct net_device *dev, int len)
  815. {
  816. u16 fid;
  817. unsigned long delay;
  818. /* FIX: this could be replace with hfa384x_cmd() if the Alloc event
  819. * below would be handled like CmdCompl event (sleep here, wake up from
  820. * interrupt handler */
  821. if (hfa384x_cmd_wait(dev, HFA384X_CMDCODE_ALLOC, len)) {
  822. printk(KERN_DEBUG "%s: cannot allocate fid, len=%d\n",
  823. dev->name, len);
  824. return 0xffff;
  825. }
  826. delay = jiffies + HFA384X_ALLOC_COMPL_TIMEOUT;
  827. while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC) &&
  828. time_before(jiffies, delay))
  829. yield();
  830. if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC)) {
  831. printk("%s: fid allocate, len=%d - timeout\n", dev->name, len);
  832. return 0xffff;
  833. }
  834. fid = HFA384X_INW(HFA384X_ALLOCFID_OFF);
  835. HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
  836. return fid;
  837. }
  838. static int prism2_reset_port(struct net_device *dev)
  839. {
  840. struct hostap_interface *iface;
  841. local_info_t *local;
  842. int res;
  843. iface = netdev_priv(dev);
  844. local = iface->local;
  845. if (!local->dev_enabled)
  846. return 0;
  847. res = hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0,
  848. NULL, NULL);
  849. if (res)
  850. printk(KERN_DEBUG "%s: reset port failed to disable port\n",
  851. dev->name);
  852. else {
  853. res = hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0,
  854. NULL, NULL);
  855. if (res)
  856. printk(KERN_DEBUG "%s: reset port failed to enable "
  857. "port\n", dev->name);
  858. }
  859. /* It looks like at least some STA firmware versions reset
  860. * fragmentation threshold back to 2346 after enable command. Restore
  861. * the configured value, if it differs from this default. */
  862. if (local->fragm_threshold != 2346 &&
  863. hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
  864. local->fragm_threshold)) {
  865. printk(KERN_DEBUG "%s: failed to restore fragmentation "
  866. "threshold (%d) after Port0 enable\n",
  867. dev->name, local->fragm_threshold);
  868. }
  869. /* Some firmwares lose antenna selection settings on reset */
  870. (void) hostap_set_antsel(local);
  871. return res;
  872. }
  873. static int prism2_get_version_info(struct net_device *dev, u16 rid,
  874. const char *txt)
  875. {
  876. struct hfa384x_comp_ident comp;
  877. struct hostap_interface *iface;
  878. local_info_t *local;
  879. iface = netdev_priv(dev);
  880. local = iface->local;
  881. if (local->no_pri) {
  882. /* PRI f/w not yet available - cannot read RIDs */
  883. return -1;
  884. }
  885. if (hfa384x_get_rid(dev, rid, &comp, sizeof(comp), 1) < 0) {
  886. printk(KERN_DEBUG "Could not get RID for component %s\n", txt);
  887. return -1;
  888. }
  889. printk(KERN_INFO "%s: %s: id=0x%02x v%d.%d.%d\n", dev->name, txt,
  890. __le16_to_cpu(comp.id), __le16_to_cpu(comp.major),
  891. __le16_to_cpu(comp.minor), __le16_to_cpu(comp.variant));
  892. return 0;
  893. }
  894. static int prism2_setup_rids(struct net_device *dev)
  895. {
  896. struct hostap_interface *iface;
  897. local_info_t *local;
  898. __le16 tmp;
  899. int ret = 0;
  900. iface = netdev_priv(dev);
  901. local = iface->local;
  902. hostap_set_word(dev, HFA384X_RID_TICKTIME, 2000);
  903. if (!local->fw_ap) {
  904. u16 tmp1 = hostap_get_porttype(local);
  905. ret = hostap_set_word(dev, HFA384X_RID_CNFPORTTYPE, tmp1);
  906. if (ret) {
  907. printk("%s: Port type setting to %d failed\n",
  908. dev->name, tmp1);
  909. goto fail;
  910. }
  911. }
  912. /* Setting SSID to empty string seems to kill the card in Host AP mode
  913. */
  914. if (local->iw_mode != IW_MODE_MASTER || local->essid[0] != '\0') {
  915. ret = hostap_set_string(dev, HFA384X_RID_CNFOWNSSID,
  916. local->essid);
  917. if (ret) {
  918. printk("%s: AP own SSID setting failed\n", dev->name);
  919. goto fail;
  920. }
  921. }
  922. ret = hostap_set_word(dev, HFA384X_RID_CNFMAXDATALEN,
  923. PRISM2_DATA_MAXLEN);
  924. if (ret) {
  925. printk("%s: MAC data length setting to %d failed\n",
  926. dev->name, PRISM2_DATA_MAXLEN);
  927. goto fail;
  928. }
  929. if (hfa384x_get_rid(dev, HFA384X_RID_CHANNELLIST, &tmp, 2, 1) < 0) {
  930. printk("%s: Channel list read failed\n", dev->name);
  931. ret = -EINVAL;
  932. goto fail;
  933. }
  934. local->channel_mask = le16_to_cpu(tmp);
  935. if (local->channel < 1 || local->channel > 14 ||
  936. !(local->channel_mask & (1 << (local->channel - 1)))) {
  937. printk(KERN_WARNING "%s: Channel setting out of range "
  938. "(%d)!\n", dev->name, local->channel);
  939. ret = -EBUSY;
  940. goto fail;
  941. }
  942. ret = hostap_set_word(dev, HFA384X_RID_CNFOWNCHANNEL, local->channel);
  943. if (ret) {
  944. printk("%s: Channel setting to %d failed\n",
  945. dev->name, local->channel);
  946. goto fail;
  947. }
  948. ret = hostap_set_word(dev, HFA384X_RID_CNFBEACONINT,
  949. local->beacon_int);
  950. if (ret) {
  951. printk("%s: Beacon interval setting to %d failed\n",
  952. dev->name, local->beacon_int);
  953. /* this may fail with Symbol/Lucent firmware */
  954. if (ret == -ETIMEDOUT)
  955. goto fail;
  956. }
  957. ret = hostap_set_word(dev, HFA384X_RID_CNFOWNDTIMPERIOD,
  958. local->dtim_period);
  959. if (ret) {
  960. printk("%s: DTIM period setting to %d failed\n",
  961. dev->name, local->dtim_period);
  962. /* this may fail with Symbol/Lucent firmware */
  963. if (ret == -ETIMEDOUT)
  964. goto fail;
  965. }
  966. ret = hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
  967. local->is_promisc);
  968. if (ret)
  969. printk(KERN_INFO "%s: Setting promiscuous mode (%d) failed\n",
  970. dev->name, local->is_promisc);
  971. if (!local->fw_ap) {
  972. ret = hostap_set_string(dev, HFA384X_RID_CNFDESIREDSSID,
  973. local->essid);
  974. if (ret) {
  975. printk("%s: Desired SSID setting failed\n", dev->name);
  976. goto fail;
  977. }
  978. }
  979. /* Setup TXRateControl, defaults to allow use of 1, 2, 5.5, and
  980. * 11 Mbps in automatic TX rate fallback and 1 and 2 Mbps as basic
  981. * rates */
  982. if (local->tx_rate_control == 0) {
  983. local->tx_rate_control =
  984. HFA384X_RATES_1MBPS |
  985. HFA384X_RATES_2MBPS |
  986. HFA384X_RATES_5MBPS |
  987. HFA384X_RATES_11MBPS;
  988. }
  989. if (local->basic_rates == 0)
  990. local->basic_rates = HFA384X_RATES_1MBPS | HFA384X_RATES_2MBPS;
  991. if (!local->fw_ap) {
  992. ret = hostap_set_word(dev, HFA384X_RID_TXRATECONTROL,
  993. local->tx_rate_control);
  994. if (ret) {
  995. printk("%s: TXRateControl setting to %d failed\n",
  996. dev->name, local->tx_rate_control);
  997. goto fail;
  998. }
  999. ret = hostap_set_word(dev, HFA384X_RID_CNFSUPPORTEDRATES,
  1000. local->tx_rate_control);
  1001. if (ret) {
  1002. printk("%s: cnfSupportedRates setting to %d failed\n",
  1003. dev->name, local->tx_rate_control);
  1004. }
  1005. ret = hostap_set_word(dev, HFA384X_RID_CNFBASICRATES,
  1006. local->basic_rates);
  1007. if (ret) {
  1008. printk("%s: cnfBasicRates setting to %d failed\n",
  1009. dev->name, local->basic_rates);
  1010. }
  1011. ret = hostap_set_word(dev, HFA384X_RID_CREATEIBSS, 1);
  1012. if (ret) {
  1013. printk("%s: Create IBSS setting to 1 failed\n",
  1014. dev->name);
  1015. }
  1016. }
  1017. if (local->name_set)
  1018. (void) hostap_set_string(dev, HFA384X_RID_CNFOWNNAME,
  1019. local->name);
  1020. if (hostap_set_encryption(local)) {
  1021. printk(KERN_INFO "%s: could not configure encryption\n",
  1022. dev->name);
  1023. }
  1024. (void) hostap_set_antsel(local);
  1025. if (hostap_set_roaming(local)) {
  1026. printk(KERN_INFO "%s: could not set host roaming\n",
  1027. dev->name);
  1028. }
  1029. if (local->sta_fw_ver >= PRISM2_FW_VER(1,6,3) &&
  1030. hostap_set_word(dev, HFA384X_RID_CNFENHSECURITY, local->enh_sec))
  1031. printk(KERN_INFO "%s: cnfEnhSecurity setting to 0x%x failed\n",
  1032. dev->name, local->enh_sec);
  1033. /* 32-bit tallies were added in STA f/w 0.8.0, but they were apparently
  1034. * not working correctly (last seven counters report bogus values).
  1035. * This has been fixed in 0.8.2, so enable 32-bit tallies only
  1036. * beginning with that firmware version. Another bug fix for 32-bit
  1037. * tallies in 1.4.0; should 16-bit tallies be used for some other
  1038. * versions, too? */
  1039. if (local->sta_fw_ver >= PRISM2_FW_VER(0,8,2)) {
  1040. if (hostap_set_word(dev, HFA384X_RID_CNFTHIRTY2TALLY, 1)) {
  1041. printk(KERN_INFO "%s: cnfThirty2Tally setting "
  1042. "failed\n", dev->name);
  1043. local->tallies32 = 0;
  1044. } else
  1045. local->tallies32 = 1;
  1046. } else
  1047. local->tallies32 = 0;
  1048. hostap_set_auth_algs(local);
  1049. if (hostap_set_word(dev, HFA384X_RID_FRAGMENTATIONTHRESHOLD,
  1050. local->fragm_threshold)) {
  1051. printk(KERN_INFO "%s: setting FragmentationThreshold to %d "
  1052. "failed\n", dev->name, local->fragm_threshold);
  1053. }
  1054. if (hostap_set_word(dev, HFA384X_RID_RTSTHRESHOLD,
  1055. local->rts_threshold)) {
  1056. printk(KERN_INFO "%s: setting RTSThreshold to %d failed\n",
  1057. dev->name, local->rts_threshold);
  1058. }
  1059. if (local->manual_retry_count >= 0 &&
  1060. hostap_set_word(dev, HFA384X_RID_CNFALTRETRYCOUNT,
  1061. local->manual_retry_count)) {
  1062. printk(KERN_INFO "%s: setting cnfAltRetryCount to %d failed\n",
  1063. dev->name, local->manual_retry_count);
  1064. }
  1065. if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1) &&
  1066. hfa384x_get_rid(dev, HFA384X_RID_CNFDBMADJUST, &tmp, 2, 1) == 2) {
  1067. local->rssi_to_dBm = le16_to_cpu(tmp);
  1068. }
  1069. if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->wpa &&
  1070. hostap_set_word(dev, HFA384X_RID_SSNHANDLINGMODE, 1)) {
  1071. printk(KERN_INFO "%s: setting ssnHandlingMode to 1 failed\n",
  1072. dev->name);
  1073. }
  1074. if (local->sta_fw_ver >= PRISM2_FW_VER(1,7,0) && local->generic_elem &&
  1075. hfa384x_set_rid(dev, HFA384X_RID_GENERICELEMENT,
  1076. local->generic_elem, local->generic_elem_len)) {
  1077. printk(KERN_INFO "%s: setting genericElement failed\n",
  1078. dev->name);
  1079. }
  1080. fail:
  1081. return ret;
  1082. }
  1083. static int prism2_hw_init(struct net_device *dev, int initial)
  1084. {
  1085. struct hostap_interface *iface;
  1086. local_info_t *local;
  1087. int ret, first = 1;
  1088. unsigned long start, delay;
  1089. PDEBUG(DEBUG_FLOW, "prism2_hw_init()\n");
  1090. iface = netdev_priv(dev);
  1091. local = iface->local;
  1092. clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits);
  1093. init:
  1094. /* initialize HFA 384x */
  1095. ret = hfa384x_cmd_no_wait(dev, HFA384X_CMDCODE_INIT, 0);
  1096. if (ret) {
  1097. printk(KERN_INFO "%s: first command failed - assuming card "
  1098. "does not have primary firmware\n", dev_info);
  1099. }
  1100. if (first && (HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
  1101. /* EvStat has Cmd bit set in some cases, so retry once if no
  1102. * wait was needed */
  1103. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  1104. printk(KERN_DEBUG "%s: init command completed too quickly - "
  1105. "retrying\n", dev->name);
  1106. first = 0;
  1107. goto init;
  1108. }
  1109. start = jiffies;
  1110. delay = jiffies + HFA384X_INIT_TIMEOUT;
  1111. while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD) &&
  1112. time_before(jiffies, delay))
  1113. yield();
  1114. if (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_CMD)) {
  1115. printk(KERN_DEBUG "%s: assuming no Primary image in "
  1116. "flash - card initialization not completed\n",
  1117. dev_info);
  1118. local->no_pri = 1;
  1119. #ifdef PRISM2_DOWNLOAD_SUPPORT
  1120. if (local->sram_type == -1)
  1121. local->sram_type = prism2_get_ram_size(local);
  1122. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  1123. return 1;
  1124. }
  1125. local->no_pri = 0;
  1126. printk(KERN_DEBUG "prism2_hw_init: initialized in %lu ms\n",
  1127. (jiffies - start) * 1000 / HZ);
  1128. HFA384X_OUTW(HFA384X_EV_CMD, HFA384X_EVACK_OFF);
  1129. return 0;
  1130. }
  1131. static int prism2_hw_init2(struct net_device *dev, int initial)
  1132. {
  1133. struct hostap_interface *iface;
  1134. local_info_t *local;
  1135. int i;
  1136. iface = netdev_priv(dev);
  1137. local = iface->local;
  1138. #ifdef PRISM2_DOWNLOAD_SUPPORT
  1139. kfree(local->pda);
  1140. if (local->no_pri)
  1141. local->pda = NULL;
  1142. else
  1143. local->pda = prism2_read_pda(dev);
  1144. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  1145. hfa384x_disable_interrupts(dev);
  1146. #ifndef final_version
  1147. HFA384X_OUTW(HFA384X_MAGIC, HFA384X_SWSUPPORT0_OFF);
  1148. if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
  1149. printk("SWSUPPORT0 write/read failed: %04X != %04X\n",
  1150. HFA384X_INW(HFA384X_SWSUPPORT0_OFF), HFA384X_MAGIC);
  1151. goto failed;
  1152. }
  1153. #endif
  1154. if (initial || local->pri_only) {
  1155. hfa384x_events_only_cmd(dev);
  1156. /* get card version information */
  1157. if (prism2_get_version_info(dev, HFA384X_RID_NICID, "NIC") ||
  1158. prism2_get_version_info(dev, HFA384X_RID_PRIID, "PRI")) {
  1159. hfa384x_disable_interrupts(dev);
  1160. goto failed;
  1161. }
  1162. if (prism2_get_version_info(dev, HFA384X_RID_STAID, "STA")) {
  1163. printk(KERN_DEBUG "%s: Failed to read STA f/w version "
  1164. "- only Primary f/w present\n", dev->name);
  1165. local->pri_only = 1;
  1166. return 0;
  1167. }
  1168. local->pri_only = 0;
  1169. hfa384x_disable_interrupts(dev);
  1170. }
  1171. /* FIX: could convert allocate_fid to use sleeping CmdCompl wait and
  1172. * enable interrupts before this. This would also require some sort of
  1173. * sleeping AllocEv waiting */
  1174. /* allocate TX FIDs */
  1175. local->txfid_len = PRISM2_TXFID_LEN;
  1176. for (i = 0; i < PRISM2_TXFID_COUNT; i++) {
  1177. local->txfid[i] = hfa384x_allocate_fid(dev, local->txfid_len);
  1178. if (local->txfid[i] == 0xffff && local->txfid_len > 1600) {
  1179. local->txfid[i] = hfa384x_allocate_fid(dev, 1600);
  1180. if (local->txfid[i] != 0xffff) {
  1181. printk(KERN_DEBUG "%s: Using shorter TX FID "
  1182. "(1600 bytes)\n", dev->name);
  1183. local->txfid_len = 1600;
  1184. }
  1185. }
  1186. if (local->txfid[i] == 0xffff)
  1187. goto failed;
  1188. local->intransmitfid[i] = PRISM2_TXFID_EMPTY;
  1189. }
  1190. hfa384x_events_only_cmd(dev);
  1191. if (initial) {
  1192. struct list_head *ptr;
  1193. prism2_check_sta_fw_version(local);
  1194. if (hfa384x_get_rid(dev, HFA384X_RID_CNFOWNMACADDR,
  1195. dev->dev_addr, 6, 1) < 0) {
  1196. printk("%s: could not get own MAC address\n",
  1197. dev->name);
  1198. }
  1199. list_for_each(ptr, &local->hostap_interfaces) {
  1200. iface = list_entry(ptr, struct hostap_interface, list);
  1201. eth_hw_addr_inherit(iface->dev, dev);
  1202. }
  1203. } else if (local->fw_ap)
  1204. prism2_check_sta_fw_version(local);
  1205. prism2_setup_rids(dev);
  1206. /* MAC is now configured, but port 0 is not yet enabled */
  1207. return 0;
  1208. failed:
  1209. if (!local->no_pri)
  1210. printk(KERN_WARNING "%s: Initialization failed\n", dev_info);
  1211. return 1;
  1212. }
  1213. static int prism2_hw_enable(struct net_device *dev, int initial)
  1214. {
  1215. struct hostap_interface *iface;
  1216. local_info_t *local;
  1217. int was_resetting;
  1218. iface = netdev_priv(dev);
  1219. local = iface->local;
  1220. was_resetting = local->hw_resetting;
  1221. if (hfa384x_cmd(dev, HFA384X_CMDCODE_ENABLE, 0, NULL, NULL)) {
  1222. printk("%s: MAC port 0 enabling failed\n", dev->name);
  1223. return 1;
  1224. }
  1225. local->hw_ready = 1;
  1226. local->hw_reset_tries = 0;
  1227. local->hw_resetting = 0;
  1228. hfa384x_enable_interrupts(dev);
  1229. /* at least D-Link DWL-650 seems to require additional port reset
  1230. * before it starts acting as an AP, so reset port automatically
  1231. * here just in case */
  1232. if (initial && prism2_reset_port(dev)) {
  1233. printk("%s: MAC port 0 resetting failed\n", dev->name);
  1234. return 1;
  1235. }
  1236. if (was_resetting && netif_queue_stopped(dev)) {
  1237. /* If hw_reset() was called during pending transmit, netif
  1238. * queue was stopped. Wake it up now since the wlan card has
  1239. * been resetted. */
  1240. netif_wake_queue(dev);
  1241. }
  1242. return 0;
  1243. }
  1244. static int prism2_hw_config(struct net_device *dev, int initial)
  1245. {
  1246. struct hostap_interface *iface;
  1247. local_info_t *local;
  1248. iface = netdev_priv(dev);
  1249. local = iface->local;
  1250. if (local->hw_downloading)
  1251. return 1;
  1252. if (prism2_hw_init(dev, initial)) {
  1253. return local->no_pri ? 0 : 1;
  1254. }
  1255. if (prism2_hw_init2(dev, initial))
  1256. return 1;
  1257. /* Enable firmware if secondary image is loaded and at least one of the
  1258. * netdevices is up. */
  1259. if (!local->pri_only &&
  1260. (initial == 0 || (initial == 2 && local->num_dev_open > 0))) {
  1261. if (!local->dev_enabled)
  1262. prism2_callback(local, PRISM2_CALLBACK_ENABLE);
  1263. local->dev_enabled = 1;
  1264. return prism2_hw_enable(dev, initial);
  1265. }
  1266. return 0;
  1267. }
  1268. static void prism2_hw_shutdown(struct net_device *dev, int no_disable)
  1269. {
  1270. struct hostap_interface *iface;
  1271. local_info_t *local;
  1272. iface = netdev_priv(dev);
  1273. local = iface->local;
  1274. /* Allow only command completion events during disable */
  1275. hfa384x_events_only_cmd(dev);
  1276. local->hw_ready = 0;
  1277. if (local->dev_enabled)
  1278. prism2_callback(local, PRISM2_CALLBACK_DISABLE);
  1279. local->dev_enabled = 0;
  1280. if (local->func->card_present && !local->func->card_present(local)) {
  1281. printk(KERN_DEBUG "%s: card already removed or not configured "
  1282. "during shutdown\n", dev->name);
  1283. return;
  1284. }
  1285. if ((no_disable & HOSTAP_HW_NO_DISABLE) == 0 &&
  1286. hfa384x_cmd(dev, HFA384X_CMDCODE_DISABLE, 0, NULL, NULL))
  1287. printk(KERN_WARNING "%s: Shutdown failed\n", dev_info);
  1288. hfa384x_disable_interrupts(dev);
  1289. if (no_disable & HOSTAP_HW_ENABLE_CMDCOMPL)
  1290. hfa384x_events_only_cmd(dev);
  1291. else
  1292. prism2_clear_cmd_queue(local);
  1293. }
  1294. static void prism2_hw_reset(struct net_device *dev)
  1295. {
  1296. struct hostap_interface *iface;
  1297. local_info_t *local;
  1298. #if 0
  1299. static long last_reset = 0;
  1300. /* do not reset card more than once per second to avoid ending up in a
  1301. * busy loop resetting the card */
  1302. if (time_before_eq(jiffies, last_reset + HZ))
  1303. return;
  1304. last_reset = jiffies;
  1305. #endif
  1306. iface = netdev_priv(dev);
  1307. local = iface->local;
  1308. if (in_interrupt()) {
  1309. printk(KERN_DEBUG "%s: driver bug - prism2_hw_reset() called "
  1310. "in interrupt context\n", dev->name);
  1311. return;
  1312. }
  1313. if (local->hw_downloading)
  1314. return;
  1315. if (local->hw_resetting) {
  1316. printk(KERN_WARNING "%s: %s: already resetting card - "
  1317. "ignoring reset request\n", dev_info, dev->name);
  1318. return;
  1319. }
  1320. local->hw_reset_tries++;
  1321. if (local->hw_reset_tries > 10) {
  1322. printk(KERN_WARNING "%s: too many reset tries, skipping\n",
  1323. dev->name);
  1324. return;
  1325. }
  1326. printk(KERN_WARNING "%s: %s: resetting card\n", dev_info, dev->name);
  1327. hfa384x_disable_interrupts(dev);
  1328. local->hw_resetting = 1;
  1329. if (local->func->cor_sreset) {
  1330. /* Host system seems to hang in some cases with high traffic
  1331. * load or shared interrupts during COR sreset. Disable shared
  1332. * interrupts during reset to avoid these crashes. COS sreset
  1333. * takes quite a long time, so it is unfortunate that this
  1334. * seems to be needed. Anyway, I do not know of any better way
  1335. * of avoiding the crash. */
  1336. disable_irq(dev->irq);
  1337. local->func->cor_sreset(local);
  1338. enable_irq(dev->irq);
  1339. }
  1340. prism2_hw_shutdown(dev, 1);
  1341. prism2_hw_config(dev, 0);
  1342. local->hw_resetting = 0;
  1343. #ifdef PRISM2_DOWNLOAD_SUPPORT
  1344. if (local->dl_pri) {
  1345. printk(KERN_DEBUG "%s: persistent download of primary "
  1346. "firmware\n", dev->name);
  1347. if (prism2_download_genesis(local, local->dl_pri) < 0)
  1348. printk(KERN_WARNING "%s: download (PRI) failed\n",
  1349. dev->name);
  1350. }
  1351. if (local->dl_sec) {
  1352. printk(KERN_DEBUG "%s: persistent download of secondary "
  1353. "firmware\n", dev->name);
  1354. if (prism2_download_volatile(local, local->dl_sec) < 0)
  1355. printk(KERN_WARNING "%s: download (SEC) failed\n",
  1356. dev->name);
  1357. }
  1358. #endif /* PRISM2_DOWNLOAD_SUPPORT */
  1359. /* TODO: restore beacon TIM bits for STAs that have buffered frames */
  1360. }
  1361. static void prism2_schedule_reset(local_info_t *local)
  1362. {
  1363. schedule_work(&local->reset_queue);
  1364. }
  1365. /* Called only as scheduled task after noticing card timeout in interrupt
  1366. * context */
  1367. static void handle_reset_queue(struct work_struct *work)
  1368. {
  1369. local_info_t *local = container_of(work, local_info_t, reset_queue);
  1370. printk(KERN_DEBUG "%s: scheduled card reset\n", local->dev->name);
  1371. prism2_hw_reset(local->dev);
  1372. if (netif_queue_stopped(local->dev)) {
  1373. int i;
  1374. for (i = 0; i < PRISM2_TXFID_COUNT; i++)
  1375. if (local->intransmitfid[i] == PRISM2_TXFID_EMPTY) {
  1376. PDEBUG(DEBUG_EXTRA, "prism2_tx_timeout: "
  1377. "wake up queue\n");
  1378. netif_wake_queue(local->dev);
  1379. break;
  1380. }
  1381. }
  1382. }
  1383. static int prism2_get_txfid_idx(local_info_t *local)
  1384. {
  1385. int idx, end;
  1386. unsigned long flags;
  1387. spin_lock_irqsave(&local->txfidlock, flags);
  1388. end = idx = local->next_txfid;
  1389. do {
  1390. if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
  1391. local->intransmitfid[idx] = PRISM2_TXFID_RESERVED;
  1392. spin_unlock_irqrestore(&local->txfidlock, flags);
  1393. return idx;
  1394. }
  1395. idx++;
  1396. if (idx >= PRISM2_TXFID_COUNT)
  1397. idx = 0;
  1398. } while (idx != end);
  1399. spin_unlock_irqrestore(&local->txfidlock, flags);
  1400. PDEBUG(DEBUG_EXTRA2, "prism2_get_txfid_idx: no room in txfid buf: "
  1401. "packet dropped\n");
  1402. local->dev->stats.tx_dropped++;
  1403. return -1;
  1404. }
  1405. /* Called only from hardware IRQ */
  1406. static void prism2_transmit_cb(struct net_device *dev, long context,
  1407. u16 resp0, u16 res)
  1408. {
  1409. struct hostap_interface *iface;
  1410. local_info_t *local;
  1411. int idx = (int) context;
  1412. iface = netdev_priv(dev);
  1413. local = iface->local;
  1414. if (res) {
  1415. printk(KERN_DEBUG "%s: prism2_transmit_cb - res=0x%02x\n",
  1416. dev->name, res);
  1417. return;
  1418. }
  1419. if (idx < 0 || idx >= PRISM2_TXFID_COUNT) {
  1420. printk(KERN_DEBUG "%s: prism2_transmit_cb called with invalid "
  1421. "idx=%d\n", dev->name, idx);
  1422. return;
  1423. }
  1424. if (!test_and_clear_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
  1425. printk(KERN_DEBUG "%s: driver bug: prism2_transmit_cb called "
  1426. "with no pending transmit\n", dev->name);
  1427. }
  1428. if (netif_queue_stopped(dev)) {
  1429. /* ready for next TX, so wake up queue that was stopped in
  1430. * prism2_transmit() */
  1431. netif_wake_queue(dev);
  1432. }
  1433. spin_lock(&local->txfidlock);
  1434. /* With reclaim, Resp0 contains new txfid for transmit; the old txfid
  1435. * will be automatically allocated for the next TX frame */
  1436. local->intransmitfid[idx] = resp0;
  1437. PDEBUG(DEBUG_FID, "%s: prism2_transmit_cb: txfid[%d]=0x%04x, "
  1438. "resp0=0x%04x, transmit_txfid=0x%04x\n",
  1439. dev->name, idx, local->txfid[idx],
  1440. resp0, local->intransmitfid[local->next_txfid]);
  1441. idx++;
  1442. if (idx >= PRISM2_TXFID_COUNT)
  1443. idx = 0;
  1444. local->next_txfid = idx;
  1445. /* check if all TX buffers are occupied */
  1446. do {
  1447. if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY) {
  1448. spin_unlock(&local->txfidlock);
  1449. return;
  1450. }
  1451. idx++;
  1452. if (idx >= PRISM2_TXFID_COUNT)
  1453. idx = 0;
  1454. } while (idx != local->next_txfid);
  1455. spin_unlock(&local->txfidlock);
  1456. /* no empty TX buffers, stop queue */
  1457. netif_stop_queue(dev);
  1458. }
  1459. /* Called only from software IRQ if PCI bus master is not used (with bus master
  1460. * this can be called both from software and hardware IRQ) */
  1461. static int prism2_transmit(struct net_device *dev, int idx)
  1462. {
  1463. struct hostap_interface *iface;
  1464. local_info_t *local;
  1465. int res;
  1466. iface = netdev_priv(dev);
  1467. local = iface->local;
  1468. /* The driver tries to stop netif queue so that there would not be
  1469. * more than one attempt to transmit frames going on; check that this
  1470. * is really the case */
  1471. if (test_and_set_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
  1472. printk(KERN_DEBUG "%s: driver bug - prism2_transmit() called "
  1473. "when previous TX was pending\n", dev->name);
  1474. return -1;
  1475. }
  1476. /* stop the queue for the time that transmit is pending */
  1477. netif_stop_queue(dev);
  1478. /* transmit packet */
  1479. res = hfa384x_cmd_callback(
  1480. dev,
  1481. HFA384X_CMDCODE_TRANSMIT | HFA384X_CMD_TX_RECLAIM,
  1482. local->txfid[idx],
  1483. prism2_transmit_cb, (long) idx);
  1484. if (res) {
  1485. printk(KERN_DEBUG "%s: prism2_transmit: CMDCODE_TRANSMIT "
  1486. "failed (res=%d)\n", dev->name, res);
  1487. dev->stats.tx_dropped++;
  1488. netif_wake_queue(dev);
  1489. return -1;
  1490. }
  1491. netif_trans_update(dev);
  1492. /* Since we did not wait for command completion, the card continues
  1493. * to process on the background and we will finish handling when
  1494. * command completion event is handled (prism2_cmd_ev() function) */
  1495. return 0;
  1496. }
  1497. /* Send IEEE 802.11 frame (convert the header into Prism2 TX descriptor and
  1498. * send the payload with this descriptor) */
  1499. /* Called only from software IRQ */
  1500. static int prism2_tx_80211(struct sk_buff *skb, struct net_device *dev)
  1501. {
  1502. struct hostap_interface *iface;
  1503. local_info_t *local;
  1504. struct hfa384x_tx_frame txdesc;
  1505. struct hostap_skb_tx_data *meta;
  1506. int hdr_len, data_len, idx, res, ret = -1;
  1507. u16 tx_control, fc;
  1508. iface = netdev_priv(dev);
  1509. local = iface->local;
  1510. meta = (struct hostap_skb_tx_data *) skb->cb;
  1511. prism2_callback(local, PRISM2_CALLBACK_TX_START);
  1512. if ((local->func->card_present && !local->func->card_present(local)) ||
  1513. !local->hw_ready || local->hw_downloading || local->pri_only) {
  1514. if (net_ratelimit()) {
  1515. printk(KERN_DEBUG "%s: prism2_tx_80211: hw not ready -"
  1516. " skipping\n", dev->name);
  1517. }
  1518. goto fail;
  1519. }
  1520. memset(&txdesc, 0, sizeof(txdesc));
  1521. /* skb->data starts with txdesc->frame_control */
  1522. hdr_len = 24;
  1523. skb_copy_from_linear_data(skb, &txdesc.frame_control, hdr_len);
  1524. fc = le16_to_cpu(txdesc.frame_control);
  1525. if (ieee80211_is_data(txdesc.frame_control) &&
  1526. ieee80211_has_a4(txdesc.frame_control) &&
  1527. skb->len >= 30) {
  1528. /* Addr4 */
  1529. skb_copy_from_linear_data_offset(skb, hdr_len, txdesc.addr4,
  1530. ETH_ALEN);
  1531. hdr_len += ETH_ALEN;
  1532. }
  1533. tx_control = local->tx_control;
  1534. if (meta->tx_cb_idx) {
  1535. tx_control |= HFA384X_TX_CTRL_TX_OK;
  1536. txdesc.sw_support = cpu_to_le32(meta->tx_cb_idx);
  1537. }
  1538. txdesc.tx_control = cpu_to_le16(tx_control);
  1539. txdesc.tx_rate = meta->rate;
  1540. data_len = skb->len - hdr_len;
  1541. txdesc.data_len = cpu_to_le16(data_len);
  1542. txdesc.len = cpu_to_be16(data_len);
  1543. idx = prism2_get_txfid_idx(local);
  1544. if (idx < 0)
  1545. goto fail;
  1546. if (local->frame_dump & PRISM2_DUMP_TX_HDR)
  1547. hostap_dump_tx_header(dev->name, &txdesc);
  1548. spin_lock(&local->baplock);
  1549. res = hfa384x_setup_bap(dev, BAP0, local->txfid[idx], 0);
  1550. if (!res)
  1551. res = hfa384x_to_bap(dev, BAP0, &txdesc, sizeof(txdesc));
  1552. if (!res)
  1553. res = hfa384x_to_bap(dev, BAP0, skb->data + hdr_len,
  1554. skb->len - hdr_len);
  1555. spin_unlock(&local->baplock);
  1556. if (!res)
  1557. res = prism2_transmit(dev, idx);
  1558. if (res) {
  1559. printk(KERN_DEBUG "%s: prism2_tx_80211 - to BAP0 failed\n",
  1560. dev->name);
  1561. local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
  1562. schedule_work(&local->reset_queue);
  1563. goto fail;
  1564. }
  1565. ret = 0;
  1566. fail:
  1567. prism2_callback(local, PRISM2_CALLBACK_TX_END);
  1568. return ret;
  1569. }
  1570. /* Some SMP systems have reported number of odd errors with hostap_pci. fid
  1571. * register has changed values between consecutive reads for an unknown reason.
  1572. * This should really not happen, so more debugging is needed. This test
  1573. * version is a bit slower, but it will detect most of such register changes
  1574. * and will try to get the correct fid eventually. */
  1575. #define EXTRA_FID_READ_TESTS
  1576. static u16 prism2_read_fid_reg(struct net_device *dev, u16 reg)
  1577. {
  1578. #ifdef EXTRA_FID_READ_TESTS
  1579. u16 val, val2, val3;
  1580. int i;
  1581. for (i = 0; i < 10; i++) {
  1582. val = HFA384X_INW(reg);
  1583. val2 = HFA384X_INW(reg);
  1584. val3 = HFA384X_INW(reg);
  1585. if (val == val2 && val == val3)
  1586. return val;
  1587. printk(KERN_DEBUG "%s: detected fid change (try=%d, reg=%04x):"
  1588. " %04x %04x %04x\n",
  1589. dev->name, i, reg, val, val2, val3);
  1590. if ((val == val2 || val == val3) && val != 0)
  1591. return val;
  1592. if (val2 == val3 && val2 != 0)
  1593. return val2;
  1594. }
  1595. printk(KERN_WARNING "%s: Uhhuh.. could not read good fid from reg "
  1596. "%04x (%04x %04x %04x)\n", dev->name, reg, val, val2, val3);
  1597. return val;
  1598. #else /* EXTRA_FID_READ_TESTS */
  1599. return HFA384X_INW(reg);
  1600. #endif /* EXTRA_FID_READ_TESTS */
  1601. }
  1602. /* Called only as a tasklet (software IRQ) */
  1603. static void prism2_rx(local_info_t *local)
  1604. {
  1605. struct net_device *dev = local->dev;
  1606. int res, rx_pending = 0;
  1607. u16 len, hdr_len, rxfid, status, macport;
  1608. struct hfa384x_rx_frame rxdesc;
  1609. struct sk_buff *skb = NULL;
  1610. prism2_callback(local, PRISM2_CALLBACK_RX_START);
  1611. rxfid = prism2_read_fid_reg(dev, HFA384X_RXFID_OFF);
  1612. #ifndef final_version
  1613. if (rxfid == 0) {
  1614. rxfid = HFA384X_INW(HFA384X_RXFID_OFF);
  1615. printk(KERN_DEBUG "prism2_rx: rxfid=0 (next 0x%04x)\n",
  1616. rxfid);
  1617. if (rxfid == 0) {
  1618. schedule_work(&local->reset_queue);
  1619. goto rx_dropped;
  1620. }
  1621. /* try to continue with the new rxfid value */
  1622. }
  1623. #endif
  1624. spin_lock(&local->baplock);
  1625. res = hfa384x_setup_bap(dev, BAP0, rxfid, 0);
  1626. if (!res)
  1627. res = hfa384x_from_bap(dev, BAP0, &rxdesc, sizeof(rxdesc));
  1628. if (res) {
  1629. spin_unlock(&local->baplock);
  1630. printk(KERN_DEBUG "%s: copy from BAP0 failed %d\n", dev->name,
  1631. res);
  1632. if (res == -ETIMEDOUT) {
  1633. schedule_work(&local->reset_queue);
  1634. }
  1635. goto rx_dropped;
  1636. }
  1637. len = le16_to_cpu(rxdesc.data_len);
  1638. hdr_len = sizeof(rxdesc);
  1639. status = le16_to_cpu(rxdesc.status);
  1640. macport = (status >> 8) & 0x07;
  1641. /* Drop frames with too large reported payload length. Monitor mode
  1642. * seems to sometimes pass frames (e.g., ctrl::ack) with signed and
  1643. * negative value, so allow also values 65522 .. 65534 (-14 .. -2) for
  1644. * macport 7 */
  1645. if (len > PRISM2_DATA_MAXLEN + 8 /* WEP */) {
  1646. if (macport == 7 && local->iw_mode == IW_MODE_MONITOR) {
  1647. if (len >= (u16) -14) {
  1648. hdr_len -= 65535 - len;
  1649. hdr_len--;
  1650. }
  1651. len = 0;
  1652. } else {
  1653. spin_unlock(&local->baplock);
  1654. printk(KERN_DEBUG "%s: Received frame with invalid "
  1655. "length 0x%04x\n", dev->name, len);
  1656. hostap_dump_rx_header(dev->name, &rxdesc);
  1657. goto rx_dropped;
  1658. }
  1659. }
  1660. skb = dev_alloc_skb(len + hdr_len);
  1661. if (!skb) {
  1662. spin_unlock(&local->baplock);
  1663. printk(KERN_DEBUG "%s: RX failed to allocate skb\n",
  1664. dev->name);
  1665. goto rx_dropped;
  1666. }
  1667. skb->dev = dev;
  1668. skb_put_data(skb, &rxdesc, hdr_len);
  1669. if (len > 0)
  1670. res = hfa384x_from_bap(dev, BAP0, skb_put(skb, len), len);
  1671. spin_unlock(&local->baplock);
  1672. if (res) {
  1673. printk(KERN_DEBUG "%s: RX failed to read "
  1674. "frame data\n", dev->name);
  1675. goto rx_dropped;
  1676. }
  1677. skb_queue_tail(&local->rx_list, skb);
  1678. tasklet_schedule(&local->rx_tasklet);
  1679. rx_exit:
  1680. prism2_callback(local, PRISM2_CALLBACK_RX_END);
  1681. if (!rx_pending) {
  1682. HFA384X_OUTW(HFA384X_EV_RX, HFA384X_EVACK_OFF);
  1683. }
  1684. return;
  1685. rx_dropped:
  1686. dev->stats.rx_dropped++;
  1687. if (skb)
  1688. dev_kfree_skb(skb);
  1689. goto rx_exit;
  1690. }
  1691. /* Called only as a tasklet (software IRQ) */
  1692. static void hostap_rx_skb(local_info_t *local, struct sk_buff *skb)
  1693. {
  1694. struct hfa384x_rx_frame *rxdesc;
  1695. struct net_device *dev = skb->dev;
  1696. struct hostap_80211_rx_status stats;
  1697. int hdrlen, rx_hdrlen;
  1698. rx_hdrlen = sizeof(*rxdesc);
  1699. if (skb->len < sizeof(*rxdesc)) {
  1700. /* Allow monitor mode to receive shorter frames */
  1701. if (local->iw_mode == IW_MODE_MONITOR &&
  1702. skb->len >= sizeof(*rxdesc) - 30) {
  1703. rx_hdrlen = skb->len;
  1704. } else {
  1705. dev_kfree_skb(skb);
  1706. return;
  1707. }
  1708. }
  1709. rxdesc = (struct hfa384x_rx_frame *) skb->data;
  1710. if (local->frame_dump & PRISM2_DUMP_RX_HDR &&
  1711. skb->len >= sizeof(*rxdesc))
  1712. hostap_dump_rx_header(dev->name, rxdesc);
  1713. if (le16_to_cpu(rxdesc->status) & HFA384X_RX_STATUS_FCSERR &&
  1714. (!local->monitor_allow_fcserr ||
  1715. local->iw_mode != IW_MODE_MONITOR))
  1716. goto drop;
  1717. if (skb->len > PRISM2_DATA_MAXLEN) {
  1718. printk(KERN_DEBUG "%s: RX: len(%d) > MAX(%d)\n",
  1719. dev->name, skb->len, PRISM2_DATA_MAXLEN);
  1720. goto drop;
  1721. }
  1722. stats.mac_time = le32_to_cpu(rxdesc->time);
  1723. stats.signal = rxdesc->signal - local->rssi_to_dBm;
  1724. stats.noise = rxdesc->silence - local->rssi_to_dBm;
  1725. stats.rate = rxdesc->rate;
  1726. /* Convert Prism2 RX structure into IEEE 802.11 header */
  1727. hdrlen = hostap_80211_get_hdrlen(rxdesc->frame_control);
  1728. if (hdrlen > rx_hdrlen)
  1729. hdrlen = rx_hdrlen;
  1730. memmove(skb_pull(skb, rx_hdrlen - hdrlen),
  1731. &rxdesc->frame_control, hdrlen);
  1732. hostap_80211_rx(dev, skb, &stats);
  1733. return;
  1734. drop:
  1735. dev_kfree_skb(skb);
  1736. }
  1737. /* Called only as a tasklet (software IRQ) */
  1738. static void hostap_rx_tasklet(unsigned long data)
  1739. {
  1740. local_info_t *local = (local_info_t *) data;
  1741. struct sk_buff *skb;
  1742. while ((skb = skb_dequeue(&local->rx_list)) != NULL)
  1743. hostap_rx_skb(local, skb);
  1744. }
  1745. /* Called only from hardware IRQ */
  1746. static void prism2_alloc_ev(struct net_device *dev)
  1747. {
  1748. struct hostap_interface *iface;
  1749. local_info_t *local;
  1750. int idx;
  1751. u16 fid;
  1752. iface = netdev_priv(dev);
  1753. local = iface->local;
  1754. fid = prism2_read_fid_reg(dev, HFA384X_ALLOCFID_OFF);
  1755. PDEBUG(DEBUG_FID, "FID: interrupt: ALLOC - fid=0x%04x\n", fid);
  1756. spin_lock(&local->txfidlock);
  1757. idx = local->next_alloc;
  1758. do {
  1759. if (local->txfid[idx] == fid) {
  1760. PDEBUG(DEBUG_FID, "FID: found matching txfid[%d]\n",
  1761. idx);
  1762. #ifndef final_version
  1763. if (local->intransmitfid[idx] == PRISM2_TXFID_EMPTY)
  1764. printk("Already released txfid found at idx "
  1765. "%d\n", idx);
  1766. if (local->intransmitfid[idx] == PRISM2_TXFID_RESERVED)
  1767. printk("Already reserved txfid found at idx "
  1768. "%d\n", idx);
  1769. #endif
  1770. local->intransmitfid[idx] = PRISM2_TXFID_EMPTY;
  1771. idx++;
  1772. local->next_alloc = idx >= PRISM2_TXFID_COUNT ? 0 :
  1773. idx;
  1774. if (!test_bit(HOSTAP_BITS_TRANSMIT, &local->bits) &&
  1775. netif_queue_stopped(dev))
  1776. netif_wake_queue(dev);
  1777. spin_unlock(&local->txfidlock);
  1778. return;
  1779. }
  1780. idx++;
  1781. if (idx >= PRISM2_TXFID_COUNT)
  1782. idx = 0;
  1783. } while (idx != local->next_alloc);
  1784. printk(KERN_WARNING "%s: could not find matching txfid (0x%04x, new "
  1785. "read 0x%04x) for alloc event\n", dev->name, fid,
  1786. HFA384X_INW(HFA384X_ALLOCFID_OFF));
  1787. printk(KERN_DEBUG "TXFIDs:");
  1788. for (idx = 0; idx < PRISM2_TXFID_COUNT; idx++)
  1789. printk(" %04x[%04x]", local->txfid[idx],
  1790. local->intransmitfid[idx]);
  1791. printk("\n");
  1792. spin_unlock(&local->txfidlock);
  1793. /* FIX: should probably schedule reset; reference to one txfid was lost
  1794. * completely.. Bad things will happen if we run out of txfids
  1795. * Actually, this will cause netdev watchdog to notice TX timeout and
  1796. * then card reset after all txfids have been leaked. */
  1797. }
  1798. /* Called only as a tasklet (software IRQ) */
  1799. static void hostap_tx_callback(local_info_t *local,
  1800. struct hfa384x_tx_frame *txdesc, int ok,
  1801. char *payload)
  1802. {
  1803. u16 sw_support, hdrlen, len;
  1804. struct sk_buff *skb;
  1805. struct hostap_tx_callback_info *cb;
  1806. /* Make sure that frame was from us. */
  1807. if (!ether_addr_equal(txdesc->addr2, local->dev->dev_addr)) {
  1808. printk(KERN_DEBUG "%s: TX callback - foreign frame\n",
  1809. local->dev->name);
  1810. return;
  1811. }
  1812. sw_support = le32_to_cpu(txdesc->sw_support);
  1813. spin_lock(&local->lock);
  1814. cb = local->tx_callback;
  1815. while (cb != NULL && cb->idx != sw_support)
  1816. cb = cb->next;
  1817. spin_unlock(&local->lock);
  1818. if (cb == NULL) {
  1819. printk(KERN_DEBUG "%s: could not find TX callback (idx %d)\n",
  1820. local->dev->name, sw_support);
  1821. return;
  1822. }
  1823. hdrlen = hostap_80211_get_hdrlen(txdesc->frame_control);
  1824. len = le16_to_cpu(txdesc->data_len);
  1825. skb = dev_alloc_skb(hdrlen + len);
  1826. if (skb == NULL) {
  1827. printk(KERN_DEBUG "%s: hostap_tx_callback failed to allocate "
  1828. "skb\n", local->dev->name);
  1829. return;
  1830. }
  1831. skb_put_data(skb, (void *)&txdesc->frame_control, hdrlen);
  1832. if (payload)
  1833. skb_put_data(skb, payload, len);
  1834. skb->dev = local->dev;
  1835. skb_reset_mac_header(skb);
  1836. cb->func(skb, ok, cb->data);
  1837. }
  1838. /* Called only as a tasklet (software IRQ) */
  1839. static int hostap_tx_compl_read(local_info_t *local, int error,
  1840. struct hfa384x_tx_frame *txdesc,
  1841. char **payload)
  1842. {
  1843. u16 fid, len;
  1844. int res, ret = 0;
  1845. struct net_device *dev = local->dev;
  1846. fid = prism2_read_fid_reg(dev, HFA384X_TXCOMPLFID_OFF);
  1847. PDEBUG(DEBUG_FID, "interrupt: TX (err=%d) - fid=0x%04x\n", fid, error);
  1848. spin_lock(&local->baplock);
  1849. res = hfa384x_setup_bap(dev, BAP0, fid, 0);
  1850. if (!res)
  1851. res = hfa384x_from_bap(dev, BAP0, txdesc, sizeof(*txdesc));
  1852. if (res) {
  1853. PDEBUG(DEBUG_EXTRA, "%s: TX (err=%d) - fid=0x%04x - could not "
  1854. "read txdesc\n", dev->name, error, fid);
  1855. if (res == -ETIMEDOUT) {
  1856. schedule_work(&local->reset_queue);
  1857. }
  1858. ret = -1;
  1859. goto fail;
  1860. }
  1861. if (txdesc->sw_support) {
  1862. len = le16_to_cpu(txdesc->data_len);
  1863. if (len < PRISM2_DATA_MAXLEN) {
  1864. *payload = kmalloc(len, GFP_ATOMIC);
  1865. if (*payload == NULL ||
  1866. hfa384x_from_bap(dev, BAP0, *payload, len)) {
  1867. PDEBUG(DEBUG_EXTRA, "%s: could not read TX "
  1868. "frame payload\n", dev->name);
  1869. kfree(*payload);
  1870. *payload = NULL;
  1871. ret = -1;
  1872. goto fail;
  1873. }
  1874. }
  1875. }
  1876. fail:
  1877. spin_unlock(&local->baplock);
  1878. return ret;
  1879. }
  1880. /* Called only as a tasklet (software IRQ) */
  1881. static void prism2_tx_ev(local_info_t *local)
  1882. {
  1883. struct net_device *dev = local->dev;
  1884. char *payload = NULL;
  1885. struct hfa384x_tx_frame txdesc;
  1886. if (hostap_tx_compl_read(local, 0, &txdesc, &payload))
  1887. goto fail;
  1888. if (local->frame_dump & PRISM2_DUMP_TX_HDR) {
  1889. PDEBUG(DEBUG_EXTRA, "%s: TX - status=0x%04x "
  1890. "retry_count=%d tx_rate=%d seq_ctrl=%d "
  1891. "duration_id=%d\n",
  1892. dev->name, le16_to_cpu(txdesc.status),
  1893. txdesc.retry_count, txdesc.tx_rate,
  1894. le16_to_cpu(txdesc.seq_ctrl),
  1895. le16_to_cpu(txdesc.duration_id));
  1896. }
  1897. if (txdesc.sw_support)
  1898. hostap_tx_callback(local, &txdesc, 1, payload);
  1899. kfree(payload);
  1900. fail:
  1901. HFA384X_OUTW(HFA384X_EV_TX, HFA384X_EVACK_OFF);
  1902. }
  1903. /* Called only as a tasklet (software IRQ) */
  1904. static void hostap_sta_tx_exc_tasklet(unsigned long data)
  1905. {
  1906. local_info_t *local = (local_info_t *) data;
  1907. struct sk_buff *skb;
  1908. while ((skb = skb_dequeue(&local->sta_tx_exc_list)) != NULL) {
  1909. struct hfa384x_tx_frame *txdesc =
  1910. (struct hfa384x_tx_frame *) skb->data;
  1911. if (skb->len >= sizeof(*txdesc)) {
  1912. /* Convert Prism2 RX structure into IEEE 802.11 header
  1913. */
  1914. int hdrlen = hostap_80211_get_hdrlen(txdesc->frame_control);
  1915. memmove(skb_pull(skb, sizeof(*txdesc) - hdrlen),
  1916. &txdesc->frame_control, hdrlen);
  1917. hostap_handle_sta_tx_exc(local, skb);
  1918. }
  1919. dev_kfree_skb(skb);
  1920. }
  1921. }
  1922. /* Called only as a tasklet (software IRQ) */
  1923. static void prism2_txexc(local_info_t *local)
  1924. {
  1925. struct net_device *dev = local->dev;
  1926. u16 status, fc;
  1927. int show_dump, res;
  1928. char *payload = NULL;
  1929. struct hfa384x_tx_frame txdesc;
  1930. show_dump = local->frame_dump & PRISM2_DUMP_TXEXC_HDR;
  1931. dev->stats.tx_errors++;
  1932. res = hostap_tx_compl_read(local, 1, &txdesc, &payload);
  1933. HFA384X_OUTW(HFA384X_EV_TXEXC, HFA384X_EVACK_OFF);
  1934. if (res)
  1935. return;
  1936. status = le16_to_cpu(txdesc.status);
  1937. /* We produce a TXDROP event only for retry or lifetime
  1938. * exceeded, because that's the only status that really mean
  1939. * that this particular node went away.
  1940. * Other errors means that *we* screwed up. - Jean II */
  1941. if (status & (HFA384X_TX_STATUS_RETRYERR | HFA384X_TX_STATUS_AGEDERR))
  1942. {
  1943. union iwreq_data wrqu;
  1944. /* Copy 802.11 dest address. */
  1945. memcpy(wrqu.addr.sa_data, txdesc.addr1, ETH_ALEN);
  1946. wrqu.addr.sa_family = ARPHRD_ETHER;
  1947. wireless_send_event(dev, IWEVTXDROP, &wrqu, NULL);
  1948. } else
  1949. show_dump = 1;
  1950. if (local->iw_mode == IW_MODE_MASTER ||
  1951. local->iw_mode == IW_MODE_REPEAT ||
  1952. local->wds_type & HOSTAP_WDS_AP_CLIENT) {
  1953. struct sk_buff *skb;
  1954. skb = dev_alloc_skb(sizeof(txdesc));
  1955. if (skb) {
  1956. skb_put_data(skb, &txdesc, sizeof(txdesc));
  1957. skb_queue_tail(&local->sta_tx_exc_list, skb);
  1958. tasklet_schedule(&local->sta_tx_exc_tasklet);
  1959. }
  1960. }
  1961. if (txdesc.sw_support)
  1962. hostap_tx_callback(local, &txdesc, 0, payload);
  1963. kfree(payload);
  1964. if (!show_dump)
  1965. return;
  1966. PDEBUG(DEBUG_EXTRA, "%s: TXEXC - status=0x%04x (%s%s%s%s)"
  1967. " tx_control=%04x\n",
  1968. dev->name, status,
  1969. status & HFA384X_TX_STATUS_RETRYERR ? "[RetryErr]" : "",
  1970. status & HFA384X_TX_STATUS_AGEDERR ? "[AgedErr]" : "",
  1971. status & HFA384X_TX_STATUS_DISCON ? "[Discon]" : "",
  1972. status & HFA384X_TX_STATUS_FORMERR ? "[FormErr]" : "",
  1973. le16_to_cpu(txdesc.tx_control));
  1974. fc = le16_to_cpu(txdesc.frame_control);
  1975. PDEBUG(DEBUG_EXTRA, " retry_count=%d tx_rate=%d fc=0x%04x "
  1976. "(%s%s%s::%d%s%s)\n",
  1977. txdesc.retry_count, txdesc.tx_rate, fc,
  1978. ieee80211_is_mgmt(txdesc.frame_control) ? "Mgmt" : "",
  1979. ieee80211_is_ctl(txdesc.frame_control) ? "Ctrl" : "",
  1980. ieee80211_is_data(txdesc.frame_control) ? "Data" : "",
  1981. (fc & IEEE80211_FCTL_STYPE) >> 4,
  1982. ieee80211_has_tods(txdesc.frame_control) ? " ToDS" : "",
  1983. ieee80211_has_fromds(txdesc.frame_control) ? " FromDS" : "");
  1984. PDEBUG(DEBUG_EXTRA, " A1=%pM A2=%pM A3=%pM A4=%pM\n",
  1985. txdesc.addr1, txdesc.addr2,
  1986. txdesc.addr3, txdesc.addr4);
  1987. }
  1988. /* Called only as a tasklet (software IRQ) */
  1989. static void hostap_info_tasklet(unsigned long data)
  1990. {
  1991. local_info_t *local = (local_info_t *) data;
  1992. struct sk_buff *skb;
  1993. while ((skb = skb_dequeue(&local->info_list)) != NULL) {
  1994. hostap_info_process(local, skb);
  1995. dev_kfree_skb(skb);
  1996. }
  1997. }
  1998. /* Called only as a tasklet (software IRQ) */
  1999. static void prism2_info(local_info_t *local)
  2000. {
  2001. struct net_device *dev = local->dev;
  2002. u16 fid;
  2003. int res, left;
  2004. struct hfa384x_info_frame info;
  2005. struct sk_buff *skb;
  2006. fid = HFA384X_INW(HFA384X_INFOFID_OFF);
  2007. spin_lock(&local->baplock);
  2008. res = hfa384x_setup_bap(dev, BAP0, fid, 0);
  2009. if (!res)
  2010. res = hfa384x_from_bap(dev, BAP0, &info, sizeof(info));
  2011. if (res) {
  2012. spin_unlock(&local->baplock);
  2013. printk(KERN_DEBUG "Could not get info frame (fid=0x%04x)\n",
  2014. fid);
  2015. if (res == -ETIMEDOUT) {
  2016. schedule_work(&local->reset_queue);
  2017. }
  2018. goto out;
  2019. }
  2020. left = (le16_to_cpu(info.len) - 1) * 2;
  2021. if (info.len & cpu_to_le16(0x8000) || info.len == 0 || left > 2060) {
  2022. /* data register seems to give 0x8000 in some error cases even
  2023. * though busy bit is not set in offset register;
  2024. * in addition, length must be at least 1 due to type field */
  2025. spin_unlock(&local->baplock);
  2026. printk(KERN_DEBUG "%s: Received info frame with invalid "
  2027. "length 0x%04x (type 0x%04x)\n", dev->name,
  2028. le16_to_cpu(info.len), le16_to_cpu(info.type));
  2029. goto out;
  2030. }
  2031. skb = dev_alloc_skb(sizeof(info) + left);
  2032. if (skb == NULL) {
  2033. spin_unlock(&local->baplock);
  2034. printk(KERN_DEBUG "%s: Could not allocate skb for info "
  2035. "frame\n", dev->name);
  2036. goto out;
  2037. }
  2038. skb_put_data(skb, &info, sizeof(info));
  2039. if (left > 0 && hfa384x_from_bap(dev, BAP0, skb_put(skb, left), left))
  2040. {
  2041. spin_unlock(&local->baplock);
  2042. printk(KERN_WARNING "%s: Info frame read failed (fid=0x%04x, "
  2043. "len=0x%04x, type=0x%04x\n", dev->name, fid,
  2044. le16_to_cpu(info.len), le16_to_cpu(info.type));
  2045. dev_kfree_skb(skb);
  2046. goto out;
  2047. }
  2048. spin_unlock(&local->baplock);
  2049. skb_queue_tail(&local->info_list, skb);
  2050. tasklet_schedule(&local->info_tasklet);
  2051. out:
  2052. HFA384X_OUTW(HFA384X_EV_INFO, HFA384X_EVACK_OFF);
  2053. }
  2054. /* Called only as a tasklet (software IRQ) */
  2055. static void hostap_bap_tasklet(unsigned long data)
  2056. {
  2057. local_info_t *local = (local_info_t *) data;
  2058. struct net_device *dev = local->dev;
  2059. u16 ev;
  2060. int frames = 30;
  2061. if (local->func->card_present && !local->func->card_present(local))
  2062. return;
  2063. set_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
  2064. /* Process all pending BAP events without generating new interrupts
  2065. * for them */
  2066. while (frames-- > 0) {
  2067. ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
  2068. if (ev == 0xffff || !(ev & HFA384X_BAP0_EVENTS))
  2069. break;
  2070. if (ev & HFA384X_EV_RX)
  2071. prism2_rx(local);
  2072. if (ev & HFA384X_EV_INFO)
  2073. prism2_info(local);
  2074. if (ev & HFA384X_EV_TX)
  2075. prism2_tx_ev(local);
  2076. if (ev & HFA384X_EV_TXEXC)
  2077. prism2_txexc(local);
  2078. }
  2079. set_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
  2080. clear_bit(HOSTAP_BITS_BAP_TASKLET, &local->bits);
  2081. /* Enable interrupts for new BAP events */
  2082. hfa384x_events_all(dev);
  2083. clear_bit(HOSTAP_BITS_BAP_TASKLET2, &local->bits);
  2084. }
  2085. /* Called only from hardware IRQ */
  2086. static void prism2_infdrop(struct net_device *dev)
  2087. {
  2088. static unsigned long last_inquire = 0;
  2089. PDEBUG(DEBUG_EXTRA, "%s: INFDROP event\n", dev->name);
  2090. /* some firmware versions seem to get stuck with
  2091. * full CommTallies in high traffic load cases; every
  2092. * packet will then cause INFDROP event and CommTallies
  2093. * info frame will not be sent automatically. Try to
  2094. * get out of this state by inquiring CommTallies. */
  2095. if (!last_inquire || time_after(jiffies, last_inquire + HZ)) {
  2096. hfa384x_cmd_callback(dev, HFA384X_CMDCODE_INQUIRE,
  2097. HFA384X_INFO_COMMTALLIES, NULL, 0);
  2098. last_inquire = jiffies;
  2099. }
  2100. }
  2101. /* Called only from hardware IRQ */
  2102. static void prism2_ev_tick(struct net_device *dev)
  2103. {
  2104. struct hostap_interface *iface;
  2105. local_info_t *local;
  2106. u16 evstat, inten;
  2107. static int prev_stuck = 0;
  2108. iface = netdev_priv(dev);
  2109. local = iface->local;
  2110. if (time_after(jiffies, local->last_tick_timer + 5 * HZ) &&
  2111. local->last_tick_timer) {
  2112. evstat = HFA384X_INW(HFA384X_EVSTAT_OFF);
  2113. inten = HFA384X_INW(HFA384X_INTEN_OFF);
  2114. if (!prev_stuck) {
  2115. printk(KERN_INFO "%s: SW TICK stuck? "
  2116. "bits=0x%lx EvStat=%04x IntEn=%04x\n",
  2117. dev->name, local->bits, evstat, inten);
  2118. }
  2119. local->sw_tick_stuck++;
  2120. if ((evstat & HFA384X_BAP0_EVENTS) &&
  2121. (inten & HFA384X_BAP0_EVENTS)) {
  2122. printk(KERN_INFO "%s: trying to recover from IRQ "
  2123. "hang\n", dev->name);
  2124. hfa384x_events_no_bap0(dev);
  2125. }
  2126. prev_stuck = 1;
  2127. } else
  2128. prev_stuck = 0;
  2129. }
  2130. /* Called only from hardware IRQ */
  2131. static void prism2_check_magic(local_info_t *local)
  2132. {
  2133. /* at least PCI Prism2.5 with bus mastering seems to sometimes
  2134. * return 0x0000 in SWSUPPORT0 for unknown reason, but re-reading the
  2135. * register once or twice seems to get the correct value.. PCI cards
  2136. * cannot anyway be removed during normal operation, so there is not
  2137. * really any need for this verification with them. */
  2138. #ifndef PRISM2_PCI
  2139. #ifndef final_version
  2140. static unsigned long last_magic_err = 0;
  2141. struct net_device *dev = local->dev;
  2142. if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != HFA384X_MAGIC) {
  2143. if (!local->hw_ready)
  2144. return;
  2145. HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
  2146. if (time_after(jiffies, last_magic_err + 10 * HZ)) {
  2147. printk("%s: Interrupt, but SWSUPPORT0 does not match: "
  2148. "%04X != %04X - card removed?\n", dev->name,
  2149. HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
  2150. HFA384X_MAGIC);
  2151. last_magic_err = jiffies;
  2152. } else if (net_ratelimit()) {
  2153. printk(KERN_DEBUG "%s: interrupt - SWSUPPORT0=%04x "
  2154. "MAGIC=%04x\n", dev->name,
  2155. HFA384X_INW(HFA384X_SWSUPPORT0_OFF),
  2156. HFA384X_MAGIC);
  2157. }
  2158. if (HFA384X_INW(HFA384X_SWSUPPORT0_OFF) != 0xffff)
  2159. schedule_work(&local->reset_queue);
  2160. return;
  2161. }
  2162. #endif /* final_version */
  2163. #endif /* !PRISM2_PCI */
  2164. }
  2165. /* Called only from hardware IRQ */
  2166. static irqreturn_t prism2_interrupt(int irq, void *dev_id)
  2167. {
  2168. struct net_device *dev = dev_id;
  2169. struct hostap_interface *iface;
  2170. local_info_t *local;
  2171. int events = 0;
  2172. u16 ev;
  2173. iface = netdev_priv(dev);
  2174. local = iface->local;
  2175. /* Detect early interrupt before driver is fully configured */
  2176. spin_lock(&local->irq_init_lock);
  2177. if (!dev->base_addr) {
  2178. if (net_ratelimit()) {
  2179. printk(KERN_DEBUG "%s: Interrupt, but dev not configured\n",
  2180. dev->name);
  2181. }
  2182. spin_unlock(&local->irq_init_lock);
  2183. return IRQ_HANDLED;
  2184. }
  2185. spin_unlock(&local->irq_init_lock);
  2186. prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 0);
  2187. if (local->func->card_present && !local->func->card_present(local)) {
  2188. if (net_ratelimit()) {
  2189. printk(KERN_DEBUG "%s: Interrupt, but dev not OK\n",
  2190. dev->name);
  2191. }
  2192. return IRQ_HANDLED;
  2193. }
  2194. prism2_check_magic(local);
  2195. for (;;) {
  2196. ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
  2197. if (ev == 0xffff) {
  2198. if (local->shutdown)
  2199. return IRQ_HANDLED;
  2200. HFA384X_OUTW(0xffff, HFA384X_EVACK_OFF);
  2201. printk(KERN_DEBUG "%s: prism2_interrupt: ev=0xffff\n",
  2202. dev->name);
  2203. return IRQ_HANDLED;
  2204. }
  2205. ev &= HFA384X_INW(HFA384X_INTEN_OFF);
  2206. if (ev == 0)
  2207. break;
  2208. if (ev & HFA384X_EV_CMD) {
  2209. prism2_cmd_ev(dev);
  2210. }
  2211. /* Above events are needed even before hw is ready, but other
  2212. * events should be skipped during initialization. This may
  2213. * change for AllocEv if allocate_fid is implemented without
  2214. * busy waiting. */
  2215. if (!local->hw_ready || local->hw_resetting ||
  2216. !local->dev_enabled) {
  2217. ev = HFA384X_INW(HFA384X_EVSTAT_OFF);
  2218. if (ev & HFA384X_EV_CMD)
  2219. goto next_event;
  2220. if ((ev & HFA384X_EVENT_MASK) == 0)
  2221. return IRQ_HANDLED;
  2222. if (local->dev_enabled && (ev & ~HFA384X_EV_TICK) &&
  2223. net_ratelimit()) {
  2224. printk(KERN_DEBUG "%s: prism2_interrupt: hw "
  2225. "not ready; skipping events 0x%04x "
  2226. "(IntEn=0x%04x)%s%s%s\n",
  2227. dev->name, ev,
  2228. HFA384X_INW(HFA384X_INTEN_OFF),
  2229. !local->hw_ready ? " (!hw_ready)" : "",
  2230. local->hw_resetting ?
  2231. " (hw_resetting)" : "",
  2232. !local->dev_enabled ?
  2233. " (!dev_enabled)" : "");
  2234. }
  2235. HFA384X_OUTW(ev, HFA384X_EVACK_OFF);
  2236. return IRQ_HANDLED;
  2237. }
  2238. if (ev & HFA384X_EV_TICK) {
  2239. prism2_ev_tick(dev);
  2240. HFA384X_OUTW(HFA384X_EV_TICK, HFA384X_EVACK_OFF);
  2241. }
  2242. if (ev & HFA384X_EV_ALLOC) {
  2243. prism2_alloc_ev(dev);
  2244. HFA384X_OUTW(HFA384X_EV_ALLOC, HFA384X_EVACK_OFF);
  2245. }
  2246. /* Reading data from the card is quite time consuming, so do it
  2247. * in tasklets. TX, TXEXC, RX, and INFO events will be ACKed
  2248. * and unmasked after needed data has been read completely. */
  2249. if (ev & HFA384X_BAP0_EVENTS) {
  2250. hfa384x_events_no_bap0(dev);
  2251. tasklet_schedule(&local->bap_tasklet);
  2252. }
  2253. #ifndef final_version
  2254. if (ev & HFA384X_EV_WTERR) {
  2255. PDEBUG(DEBUG_EXTRA, "%s: WTERR event\n", dev->name);
  2256. HFA384X_OUTW(HFA384X_EV_WTERR, HFA384X_EVACK_OFF);
  2257. }
  2258. #endif /* final_version */
  2259. if (ev & HFA384X_EV_INFDROP) {
  2260. prism2_infdrop(dev);
  2261. HFA384X_OUTW(HFA384X_EV_INFDROP, HFA384X_EVACK_OFF);
  2262. }
  2263. next_event:
  2264. events++;
  2265. if (events >= PRISM2_MAX_INTERRUPT_EVENTS) {
  2266. PDEBUG(DEBUG_EXTRA, "prism2_interrupt: >%d events "
  2267. "(EvStat=0x%04x)\n",
  2268. PRISM2_MAX_INTERRUPT_EVENTS,
  2269. HFA384X_INW(HFA384X_EVSTAT_OFF));
  2270. break;
  2271. }
  2272. }
  2273. prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INTERRUPT, 0, 1);
  2274. return IRQ_RETVAL(events);
  2275. }
  2276. static void prism2_check_sta_fw_version(local_info_t *local)
  2277. {
  2278. struct hfa384x_comp_ident comp;
  2279. int id, variant, major, minor;
  2280. if (hfa384x_get_rid(local->dev, HFA384X_RID_STAID,
  2281. &comp, sizeof(comp), 1) < 0)
  2282. return;
  2283. local->fw_ap = 0;
  2284. id = le16_to_cpu(comp.id);
  2285. if (id != HFA384X_COMP_ID_STA) {
  2286. if (id == HFA384X_COMP_ID_FW_AP)
  2287. local->fw_ap = 1;
  2288. return;
  2289. }
  2290. major = __le16_to_cpu(comp.major);
  2291. minor = __le16_to_cpu(comp.minor);
  2292. variant = __le16_to_cpu(comp.variant);
  2293. local->sta_fw_ver = PRISM2_FW_VER(major, minor, variant);
  2294. /* Station firmware versions before 1.4.x seem to have a bug in
  2295. * firmware-based WEP encryption when using Host AP mode, so use
  2296. * host_encrypt as a default for them. Firmware version 1.4.9 is the
  2297. * first one that has been seen to produce correct encryption, but the
  2298. * bug might be fixed before that (although, at least 1.4.2 is broken).
  2299. */
  2300. local->fw_encrypt_ok = local->sta_fw_ver >= PRISM2_FW_VER(1,4,9);
  2301. if (local->iw_mode == IW_MODE_MASTER && !local->host_encrypt &&
  2302. !local->fw_encrypt_ok) {
  2303. printk(KERN_DEBUG "%s: defaulting to host-based encryption as "
  2304. "a workaround for firmware bug in Host AP mode WEP\n",
  2305. local->dev->name);
  2306. local->host_encrypt = 1;
  2307. }
  2308. /* IEEE 802.11 standard compliant WDS frames (4 addresses) were broken
  2309. * in station firmware versions before 1.5.x. With these versions, the
  2310. * driver uses a workaround with bogus frame format (4th address after
  2311. * the payload). This is not compatible with other AP devices. Since
  2312. * the firmware bug is fixed in the latest station firmware versions,
  2313. * automatically enable standard compliant mode for cards using station
  2314. * firmware version 1.5.0 or newer. */
  2315. if (local->sta_fw_ver >= PRISM2_FW_VER(1,5,0))
  2316. local->wds_type |= HOSTAP_WDS_STANDARD_FRAME;
  2317. else {
  2318. printk(KERN_DEBUG "%s: defaulting to bogus WDS frame as a "
  2319. "workaround for firmware bug in Host AP mode WDS\n",
  2320. local->dev->name);
  2321. }
  2322. hostap_check_sta_fw_version(local->ap, local->sta_fw_ver);
  2323. }
  2324. static void hostap_passive_scan(struct timer_list *t)
  2325. {
  2326. local_info_t *local = from_timer(local, t, passive_scan_timer);
  2327. struct net_device *dev = local->dev;
  2328. u16 chan;
  2329. if (local->passive_scan_interval <= 0)
  2330. return;
  2331. if (local->passive_scan_state == PASSIVE_SCAN_LISTEN) {
  2332. int max_tries = 16;
  2333. /* Even though host system does not really know when the WLAN
  2334. * MAC is sending frames, try to avoid changing channels for
  2335. * passive scanning when a host-generated frame is being
  2336. * transmitted */
  2337. if (test_bit(HOSTAP_BITS_TRANSMIT, &local->bits)) {
  2338. printk(KERN_DEBUG "%s: passive scan detected pending "
  2339. "TX - delaying\n", dev->name);
  2340. local->passive_scan_timer.expires = jiffies + HZ / 10;
  2341. add_timer(&local->passive_scan_timer);
  2342. return;
  2343. }
  2344. do {
  2345. local->passive_scan_channel++;
  2346. if (local->passive_scan_channel > 14)
  2347. local->passive_scan_channel = 1;
  2348. max_tries--;
  2349. } while (!(local->channel_mask &
  2350. (1 << (local->passive_scan_channel - 1))) &&
  2351. max_tries > 0);
  2352. if (max_tries == 0) {
  2353. printk(KERN_INFO "%s: no allowed passive scan channels"
  2354. " found\n", dev->name);
  2355. return;
  2356. }
  2357. printk(KERN_DEBUG "%s: passive scan channel %d\n",
  2358. dev->name, local->passive_scan_channel);
  2359. chan = local->passive_scan_channel;
  2360. local->passive_scan_state = PASSIVE_SCAN_WAIT;
  2361. local->passive_scan_timer.expires = jiffies + HZ / 10;
  2362. } else {
  2363. chan = local->channel;
  2364. local->passive_scan_state = PASSIVE_SCAN_LISTEN;
  2365. local->passive_scan_timer.expires = jiffies +
  2366. local->passive_scan_interval * HZ;
  2367. }
  2368. if (hfa384x_cmd_callback(dev, HFA384X_CMDCODE_TEST |
  2369. (HFA384X_TEST_CHANGE_CHANNEL << 8),
  2370. chan, NULL, 0))
  2371. printk(KERN_ERR "%s: passive scan channel set %d "
  2372. "failed\n", dev->name, chan);
  2373. add_timer(&local->passive_scan_timer);
  2374. }
  2375. /* Called only as a scheduled task when communications quality values should
  2376. * be updated. */
  2377. static void handle_comms_qual_update(struct work_struct *work)
  2378. {
  2379. local_info_t *local =
  2380. container_of(work, local_info_t, comms_qual_update);
  2381. prism2_update_comms_qual(local->dev);
  2382. }
  2383. /* Software watchdog - called as a timer. Hardware interrupt (Tick event) is
  2384. * used to monitor that local->last_tick_timer is being updated. If not,
  2385. * interrupt busy-loop is assumed and driver tries to recover by masking out
  2386. * some events. */
  2387. static void hostap_tick_timer(struct timer_list *t)
  2388. {
  2389. static unsigned long last_inquire = 0;
  2390. local_info_t *local = from_timer(local, t, tick_timer);
  2391. local->last_tick_timer = jiffies;
  2392. /* Inquire CommTallies every 10 seconds to keep the statistics updated
  2393. * more often during low load and when using 32-bit tallies. */
  2394. if ((!last_inquire || time_after(jiffies, last_inquire + 10 * HZ)) &&
  2395. !local->hw_downloading && local->hw_ready &&
  2396. !local->hw_resetting && local->dev_enabled) {
  2397. hfa384x_cmd_callback(local->dev, HFA384X_CMDCODE_INQUIRE,
  2398. HFA384X_INFO_COMMTALLIES, NULL, 0);
  2399. last_inquire = jiffies;
  2400. }
  2401. if ((local->last_comms_qual_update == 0 ||
  2402. time_after(jiffies, local->last_comms_qual_update + 10 * HZ)) &&
  2403. (local->iw_mode == IW_MODE_INFRA ||
  2404. local->iw_mode == IW_MODE_ADHOC)) {
  2405. schedule_work(&local->comms_qual_update);
  2406. }
  2407. local->tick_timer.expires = jiffies + 2 * HZ;
  2408. add_timer(&local->tick_timer);
  2409. }
  2410. #ifndef PRISM2_NO_PROCFS_DEBUG
  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 /* PRISM2_NO_PROCFS_DEBUG */
  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 */