wilc_wlan.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. /* ////////////////////////////////////////////////////////////////////////// */
  2. /* */
  3. /* Copyright (c) Atmel Corporation. All rights reserved. */
  4. /* */
  5. /* Module Name: wilc_wlan.c */
  6. /* */
  7. /* */
  8. /* //////////////////////////////////////////////////////////////////////////// */
  9. #include "wilc_wlan_if.h"
  10. #include "wilc_wfi_netdevice.h"
  11. #include "wilc_wlan_cfg.h"
  12. /********************************************
  13. *
  14. * Global
  15. *
  16. ********************************************/
  17. extern wilc_hif_func_t hif_sdio;
  18. extern wilc_hif_func_t hif_spi;
  19. u32 wilc_get_chipid(u8 update);
  20. typedef struct {
  21. int quit;
  22. /**
  23. * input interface functions
  24. **/
  25. wilc_wlan_io_func_t io_func;
  26. /**
  27. * host interface functions
  28. **/
  29. wilc_hif_func_t hif_func;
  30. /**
  31. * configuration interface functions
  32. **/
  33. int cfg_frame_in_use;
  34. wilc_cfg_frame_t cfg_frame;
  35. u32 cfg_frame_offset;
  36. int cfg_seq_no;
  37. /**
  38. * RX buffer
  39. **/
  40. #ifdef MEMORY_STATIC
  41. u8 *rx_buffer;
  42. u32 rx_buffer_offset;
  43. #endif
  44. /**
  45. * TX buffer
  46. **/
  47. u8 *tx_buffer;
  48. u32 tx_buffer_offset;
  49. /**
  50. * TX queue
  51. **/
  52. unsigned long txq_spinlock_flags;
  53. struct txq_entry_t *txq_head;
  54. struct txq_entry_t *txq_tail;
  55. int txq_entries;
  56. int txq_exit;
  57. /**
  58. * RX queue
  59. **/
  60. struct rxq_entry_t *rxq_head;
  61. struct rxq_entry_t *rxq_tail;
  62. int rxq_entries;
  63. int rxq_exit;
  64. } wilc_wlan_dev_t;
  65. static wilc_wlan_dev_t g_wlan;
  66. static inline void chip_allow_sleep(void);
  67. static inline void chip_wakeup(void);
  68. /********************************************
  69. *
  70. * Debug
  71. *
  72. ********************************************/
  73. static u32 dbgflag = N_INIT | N_ERR | N_INTR | N_TXQ | N_RXQ;
  74. static void wilc_debug(u32 flag, char *fmt, ...)
  75. {
  76. char buf[256];
  77. va_list args;
  78. if (flag & dbgflag) {
  79. va_start(args, fmt);
  80. vsprintf(buf, fmt, args);
  81. va_end(args);
  82. linux_wlan_dbg(buf);
  83. }
  84. }
  85. static CHIP_PS_STATE_T genuChipPSstate = CHIP_WAKEDUP;
  86. /*acquire_bus() and release_bus() are made static inline functions*/
  87. /*as a temporary workaround to fix a problem of receiving*/
  88. /*unknown interrupt from FW*/
  89. static inline void acquire_bus(BUS_ACQUIRE_T acquire)
  90. {
  91. mutex_lock(&g_linux_wlan->hif_cs);
  92. #ifndef WILC_OPTIMIZE_SLEEP_INT
  93. if (genuChipPSstate != CHIP_WAKEDUP)
  94. #endif
  95. {
  96. if (acquire == ACQUIRE_AND_WAKEUP)
  97. chip_wakeup();
  98. }
  99. }
  100. static inline void release_bus(BUS_RELEASE_T release)
  101. {
  102. #ifdef WILC_OPTIMIZE_SLEEP_INT
  103. if (release == RELEASE_ALLOW_SLEEP)
  104. chip_allow_sleep();
  105. #endif
  106. mutex_unlock(&g_linux_wlan->hif_cs);
  107. }
  108. /********************************************
  109. *
  110. * Queue
  111. *
  112. ********************************************/
  113. static void wilc_wlan_txq_remove(struct txq_entry_t *tqe)
  114. {
  115. wilc_wlan_dev_t *p = &g_wlan;
  116. if (tqe == p->txq_head) {
  117. p->txq_head = tqe->next;
  118. if (p->txq_head)
  119. p->txq_head->prev = NULL;
  120. } else if (tqe == p->txq_tail) {
  121. p->txq_tail = (tqe->prev);
  122. if (p->txq_tail)
  123. p->txq_tail->next = NULL;
  124. } else {
  125. tqe->prev->next = tqe->next;
  126. tqe->next->prev = tqe->prev;
  127. }
  128. p->txq_entries -= 1;
  129. }
  130. static struct txq_entry_t *wilc_wlan_txq_remove_from_head(void)
  131. {
  132. struct txq_entry_t *tqe;
  133. wilc_wlan_dev_t *p = &g_wlan;
  134. unsigned long flags;
  135. spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
  136. if (p->txq_head) {
  137. tqe = p->txq_head;
  138. p->txq_head = tqe->next;
  139. if (p->txq_head)
  140. p->txq_head->prev = NULL;
  141. p->txq_entries -= 1;
  142. } else {
  143. tqe = NULL;
  144. }
  145. spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
  146. return tqe;
  147. }
  148. static void wilc_wlan_txq_add_to_tail(struct txq_entry_t *tqe)
  149. {
  150. wilc_wlan_dev_t *p = &g_wlan;
  151. unsigned long flags;
  152. spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
  153. if (p->txq_head == NULL) {
  154. tqe->next = NULL;
  155. tqe->prev = NULL;
  156. p->txq_head = tqe;
  157. p->txq_tail = tqe;
  158. } else {
  159. tqe->next = NULL;
  160. tqe->prev = p->txq_tail;
  161. p->txq_tail->next = tqe;
  162. p->txq_tail = tqe;
  163. }
  164. p->txq_entries += 1;
  165. PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
  166. spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
  167. /**
  168. * wake up TX queue
  169. **/
  170. PRINT_D(TX_DBG, "Wake the txq_handling\n");
  171. up(&g_linux_wlan->txq_event);
  172. }
  173. static int wilc_wlan_txq_add_to_head(struct txq_entry_t *tqe)
  174. {
  175. wilc_wlan_dev_t *p = &g_wlan;
  176. unsigned long flags;
  177. if (linux_wlan_lock_timeout(&g_linux_wlan->txq_add_to_head_cs,
  178. CFG_PKTS_TIMEOUT))
  179. return -1;
  180. spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
  181. if (p->txq_head == NULL) {
  182. tqe->next = NULL;
  183. tqe->prev = NULL;
  184. p->txq_head = tqe;
  185. p->txq_tail = tqe;
  186. } else {
  187. tqe->next = p->txq_head;
  188. tqe->prev = NULL;
  189. p->txq_head->prev = tqe;
  190. p->txq_head = tqe;
  191. }
  192. p->txq_entries += 1;
  193. PRINT_D(TX_DBG, "Number of entries in TxQ = %d\n", p->txq_entries);
  194. spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
  195. up(&g_linux_wlan->txq_add_to_head_cs);
  196. /**
  197. * wake up TX queue
  198. **/
  199. up(&g_linux_wlan->txq_event);
  200. PRINT_D(TX_DBG, "Wake up the txq_handler\n");
  201. return 0;
  202. }
  203. u32 Statisitcs_totalAcks = 0, Statisitcs_DroppedAcks = 0;
  204. #ifdef TCP_ACK_FILTER
  205. struct Ack_session_info;
  206. struct Ack_session_info {
  207. u32 Ack_seq_num;
  208. u32 Bigger_Ack_num;
  209. u16 src_port;
  210. u16 dst_port;
  211. u16 status;
  212. };
  213. typedef struct {
  214. u32 ack_num;
  215. u32 Session_index;
  216. struct txq_entry_t *txqe;
  217. } Pending_Acks_info_t /*Ack_info_t*/;
  218. struct Ack_session_info *Free_head;
  219. struct Ack_session_info *Alloc_head;
  220. #define NOT_TCP_ACK (-1)
  221. #define MAX_TCP_SESSION 25
  222. #define MAX_PENDING_ACKS 256
  223. struct Ack_session_info Acks_keep_track_info[2 * MAX_TCP_SESSION];
  224. Pending_Acks_info_t Pending_Acks_info[MAX_PENDING_ACKS];
  225. u32 PendingAcks_arrBase;
  226. u32 Opened_TCP_session;
  227. u32 Pending_Acks;
  228. static inline int Init_TCP_tracking(void)
  229. {
  230. return 0;
  231. }
  232. static inline int add_TCP_track_session(u32 src_prt, u32 dst_prt, u32 seq)
  233. {
  234. Acks_keep_track_info[Opened_TCP_session].Ack_seq_num = seq;
  235. Acks_keep_track_info[Opened_TCP_session].Bigger_Ack_num = 0;
  236. Acks_keep_track_info[Opened_TCP_session].src_port = src_prt;
  237. Acks_keep_track_info[Opened_TCP_session].dst_port = dst_prt;
  238. Opened_TCP_session++;
  239. PRINT_D(TCP_ENH, "TCP Session %d to Ack %d\n", Opened_TCP_session, seq);
  240. return 0;
  241. }
  242. static inline int Update_TCP_track_session(u32 index, u32 Ack)
  243. {
  244. if (Ack > Acks_keep_track_info[index].Bigger_Ack_num)
  245. Acks_keep_track_info[index].Bigger_Ack_num = Ack;
  246. return 0;
  247. }
  248. static inline int add_TCP_Pending_Ack(u32 Ack, u32 Session_index, struct txq_entry_t *txqe)
  249. {
  250. Statisitcs_totalAcks++;
  251. if (Pending_Acks < MAX_PENDING_ACKS) {
  252. Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].ack_num = Ack;
  253. Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].txqe = txqe;
  254. Pending_Acks_info[PendingAcks_arrBase + Pending_Acks].Session_index = Session_index;
  255. txqe->tcp_PendingAck_index = PendingAcks_arrBase + Pending_Acks;
  256. Pending_Acks++;
  257. } else {
  258. }
  259. return 0;
  260. }
  261. static inline int remove_TCP_related(void)
  262. {
  263. wilc_wlan_dev_t *p = &g_wlan;
  264. unsigned long flags;
  265. spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
  266. spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
  267. return 0;
  268. }
  269. static inline int tcp_process(struct net_device *dev, struct txq_entry_t *tqe)
  270. {
  271. int ret;
  272. u8 *eth_hdr_ptr;
  273. u8 *buffer = tqe->buffer;
  274. unsigned short h_proto;
  275. int i;
  276. wilc_wlan_dev_t *p = &g_wlan;
  277. unsigned long flags;
  278. perInterface_wlan_t *nic;
  279. struct wilc *wilc;
  280. nic = netdev_priv(dev);
  281. wilc = nic->wilc;
  282. spin_lock_irqsave(&wilc->txq_spinlock, flags);
  283. eth_hdr_ptr = &buffer[0];
  284. h_proto = ntohs(*((unsigned short *)&eth_hdr_ptr[12]));
  285. if (h_proto == 0x0800) { /* IP */
  286. u8 *ip_hdr_ptr;
  287. u8 protocol;
  288. ip_hdr_ptr = &buffer[ETHERNET_HDR_LEN];
  289. protocol = ip_hdr_ptr[9];
  290. if (protocol == 0x06) {
  291. u8 *tcp_hdr_ptr;
  292. u32 IHL, Total_Length, Data_offset;
  293. tcp_hdr_ptr = &ip_hdr_ptr[IP_HDR_LEN];
  294. IHL = (ip_hdr_ptr[0] & 0xf) << 2;
  295. Total_Length = (((u32)ip_hdr_ptr[2]) << 8) + ((u32)ip_hdr_ptr[3]);
  296. Data_offset = (((u32)tcp_hdr_ptr[12] & 0xf0) >> 2);
  297. if (Total_Length == (IHL + Data_offset)) { /*we want to recognize the clear Acks(packet only carry Ack infos not with data) so data size must be equal zero*/
  298. u32 seq_no, Ack_no;
  299. seq_no = (((u32)tcp_hdr_ptr[4]) << 24) + (((u32)tcp_hdr_ptr[5]) << 16) + (((u32)tcp_hdr_ptr[6]) << 8) + ((u32)tcp_hdr_ptr[7]);
  300. Ack_no = (((u32)tcp_hdr_ptr[8]) << 24) + (((u32)tcp_hdr_ptr[9]) << 16) + (((u32)tcp_hdr_ptr[10]) << 8) + ((u32)tcp_hdr_ptr[11]);
  301. for (i = 0; i < Opened_TCP_session; i++) {
  302. if (Acks_keep_track_info[i].Ack_seq_num == seq_no) {
  303. Update_TCP_track_session(i, Ack_no);
  304. break;
  305. }
  306. }
  307. if (i == Opened_TCP_session)
  308. add_TCP_track_session(0, 0, seq_no);
  309. add_TCP_Pending_Ack(Ack_no, i, tqe);
  310. }
  311. } else {
  312. ret = 0;
  313. }
  314. } else {
  315. ret = 0;
  316. }
  317. spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
  318. return ret;
  319. }
  320. static int wilc_wlan_txq_filter_dup_tcp_ack(struct net_device *dev)
  321. {
  322. perInterface_wlan_t *nic;
  323. struct wilc *wilc;
  324. u32 i = 0;
  325. u32 Dropped = 0;
  326. wilc_wlan_dev_t *p = &g_wlan;
  327. nic = netdev_priv(dev);
  328. wilc = nic->wilc;
  329. spin_lock_irqsave(&wilc->txq_spinlock, p->txq_spinlock_flags);
  330. for (i = PendingAcks_arrBase; i < (PendingAcks_arrBase + Pending_Acks); i++) {
  331. if (Pending_Acks_info[i].ack_num < Acks_keep_track_info[Pending_Acks_info[i].Session_index].Bigger_Ack_num) {
  332. struct txq_entry_t *tqe;
  333. PRINT_D(TCP_ENH, "DROP ACK: %u\n", Pending_Acks_info[i].ack_num);
  334. tqe = Pending_Acks_info[i].txqe;
  335. if (tqe) {
  336. wilc_wlan_txq_remove(tqe);
  337. Statisitcs_DroppedAcks++;
  338. tqe->status = 1; /* mark the packet send */
  339. if (tqe->tx_complete_func)
  340. tqe->tx_complete_func(tqe->priv, tqe->status);
  341. kfree(tqe);
  342. Dropped++;
  343. }
  344. }
  345. }
  346. Pending_Acks = 0;
  347. Opened_TCP_session = 0;
  348. if (PendingAcks_arrBase == 0)
  349. PendingAcks_arrBase = MAX_TCP_SESSION;
  350. else
  351. PendingAcks_arrBase = 0;
  352. spin_unlock_irqrestore(&wilc->txq_spinlock, p->txq_spinlock_flags);
  353. while (Dropped > 0) {
  354. /*consume the semaphore count of the removed packet*/
  355. linux_wlan_lock_timeout(&wilc->txq_event, 1);
  356. Dropped--;
  357. }
  358. return 1;
  359. }
  360. #endif
  361. bool EnableTCPAckFilter = false;
  362. void Enable_TCP_ACK_Filter(bool value)
  363. {
  364. EnableTCPAckFilter = value;
  365. }
  366. bool is_TCP_ACK_Filter_Enabled(void)
  367. {
  368. return EnableTCPAckFilter;
  369. }
  370. static int wilc_wlan_txq_add_cfg_pkt(u8 *buffer, u32 buffer_size)
  371. {
  372. wilc_wlan_dev_t *p = &g_wlan;
  373. struct txq_entry_t *tqe;
  374. PRINT_D(TX_DBG, "Adding config packet ...\n");
  375. if (p->quit) {
  376. PRINT_D(TX_DBG, "Return due to clear function\n");
  377. up(&g_linux_wlan->cfg_event);
  378. return 0;
  379. }
  380. tqe = kmalloc(sizeof(struct txq_entry_t), GFP_ATOMIC);
  381. if (tqe == NULL) {
  382. PRINT_ER("Failed to allocate memory\n");
  383. return 0;
  384. }
  385. tqe->type = WILC_CFG_PKT;
  386. tqe->buffer = buffer;
  387. tqe->buffer_size = buffer_size;
  388. tqe->tx_complete_func = NULL;
  389. tqe->priv = NULL;
  390. #ifdef TCP_ACK_FILTER
  391. tqe->tcp_PendingAck_index = NOT_TCP_ACK;
  392. #endif
  393. /**
  394. * Configuration packet always at the front
  395. **/
  396. PRINT_D(TX_DBG, "Adding the config packet at the Queue tail\n");
  397. if (wilc_wlan_txq_add_to_head(tqe))
  398. return 0;
  399. return 1;
  400. }
  401. int wilc_wlan_txq_add_net_pkt(struct net_device *dev, void *priv, u8 *buffer,
  402. u32 buffer_size, wilc_tx_complete_func_t func)
  403. {
  404. wilc_wlan_dev_t *p = &g_wlan;
  405. struct txq_entry_t *tqe;
  406. if (p->quit)
  407. return 0;
  408. tqe = kmalloc(sizeof(struct txq_entry_t), GFP_ATOMIC);
  409. if (tqe == NULL)
  410. return 0;
  411. tqe->type = WILC_NET_PKT;
  412. tqe->buffer = buffer;
  413. tqe->buffer_size = buffer_size;
  414. tqe->tx_complete_func = func;
  415. tqe->priv = priv;
  416. PRINT_D(TX_DBG, "Adding mgmt packet at the Queue tail\n");
  417. #ifdef TCP_ACK_FILTER
  418. tqe->tcp_PendingAck_index = NOT_TCP_ACK;
  419. if (is_TCP_ACK_Filter_Enabled())
  420. tcp_process(dev, tqe);
  421. #endif
  422. wilc_wlan_txq_add_to_tail(tqe);
  423. /*return number of itemes in the queue*/
  424. return p->txq_entries;
  425. }
  426. int wilc_wlan_txq_add_mgmt_pkt(void *priv, u8 *buffer, u32 buffer_size, wilc_tx_complete_func_t func)
  427. {
  428. wilc_wlan_dev_t *p = &g_wlan;
  429. struct txq_entry_t *tqe;
  430. if (p->quit)
  431. return 0;
  432. tqe = kmalloc(sizeof(struct txq_entry_t), GFP_KERNEL);
  433. if (tqe == NULL)
  434. return 0;
  435. tqe->type = WILC_MGMT_PKT;
  436. tqe->buffer = buffer;
  437. tqe->buffer_size = buffer_size;
  438. tqe->tx_complete_func = func;
  439. tqe->priv = priv;
  440. #ifdef TCP_ACK_FILTER
  441. tqe->tcp_PendingAck_index = NOT_TCP_ACK;
  442. #endif
  443. PRINT_D(TX_DBG, "Adding Network packet at the Queue tail\n");
  444. wilc_wlan_txq_add_to_tail(tqe);
  445. return 1;
  446. }
  447. static struct txq_entry_t *wilc_wlan_txq_get_first(void)
  448. {
  449. wilc_wlan_dev_t *p = &g_wlan;
  450. struct txq_entry_t *tqe;
  451. unsigned long flags;
  452. spin_lock_irqsave(&g_linux_wlan->txq_spinlock, flags);
  453. tqe = p->txq_head;
  454. spin_unlock_irqrestore(&g_linux_wlan->txq_spinlock, flags);
  455. return tqe;
  456. }
  457. static struct txq_entry_t *wilc_wlan_txq_get_next(struct wilc *wilc,
  458. struct txq_entry_t *tqe)
  459. {
  460. unsigned long flags;
  461. spin_lock_irqsave(&wilc->txq_spinlock, flags);
  462. tqe = tqe->next;
  463. spin_unlock_irqrestore(&wilc->txq_spinlock, flags);
  464. return tqe;
  465. }
  466. static int wilc_wlan_rxq_add(struct wilc *wilc, struct rxq_entry_t *rqe)
  467. {
  468. wilc_wlan_dev_t *p = &g_wlan;
  469. if (p->quit)
  470. return 0;
  471. mutex_lock(&wilc->rxq_cs);
  472. if (p->rxq_head == NULL) {
  473. PRINT_D(RX_DBG, "Add to Queue head\n");
  474. rqe->next = NULL;
  475. p->rxq_head = rqe;
  476. p->rxq_tail = rqe;
  477. } else {
  478. PRINT_D(RX_DBG, "Add to Queue tail\n");
  479. p->rxq_tail->next = rqe;
  480. rqe->next = NULL;
  481. p->rxq_tail = rqe;
  482. }
  483. p->rxq_entries += 1;
  484. PRINT_D(RX_DBG, "Number of queue entries: %d\n", p->rxq_entries);
  485. mutex_unlock(&wilc->rxq_cs);
  486. return p->rxq_entries;
  487. }
  488. static struct rxq_entry_t *wilc_wlan_rxq_remove(struct wilc *wilc)
  489. {
  490. wilc_wlan_dev_t *p = &g_wlan;
  491. PRINT_D(RX_DBG, "Getting rxQ element\n");
  492. if (p->rxq_head) {
  493. struct rxq_entry_t *rqe;
  494. mutex_lock(&wilc->rxq_cs);
  495. rqe = p->rxq_head;
  496. p->rxq_head = p->rxq_head->next;
  497. p->rxq_entries -= 1;
  498. PRINT_D(RX_DBG, "RXQ entries decreased\n");
  499. mutex_unlock(&wilc->rxq_cs);
  500. return rqe;
  501. }
  502. PRINT_D(RX_DBG, "Nothing to get from Q\n");
  503. return NULL;
  504. }
  505. /********************************************
  506. *
  507. * Power Save handle functions
  508. *
  509. ********************************************/
  510. #ifdef WILC_OPTIMIZE_SLEEP_INT
  511. static inline void chip_allow_sleep(void)
  512. {
  513. u32 reg = 0;
  514. /* Clear bit 1 */
  515. g_wlan.hif_func.hif_read_reg(0xf0, &reg);
  516. g_wlan.hif_func.hif_write_reg(0xf0, reg & ~BIT(0));
  517. }
  518. static inline void chip_wakeup(void)
  519. {
  520. u32 reg, clk_status_reg, trials = 0;
  521. u32 sleep_time;
  522. if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
  523. do {
  524. g_wlan.hif_func.hif_read_reg(1, &reg);
  525. /* Set bit 1 */
  526. g_wlan.hif_func.hif_write_reg(1, reg | BIT(1));
  527. /* Clear bit 1*/
  528. g_wlan.hif_func.hif_write_reg(1, reg & ~BIT(1));
  529. do {
  530. /* Wait for the chip to stabilize*/
  531. usleep_range(2 * 1000, 2 * 1000);
  532. /* Make sure chip is awake. This is an extra step that can be removed */
  533. /* later to avoid the bus access overhead */
  534. if ((wilc_get_chipid(true) == 0))
  535. wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
  536. } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
  537. } while (wilc_get_chipid(true) == 0);
  538. } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO) {
  539. g_wlan.hif_func.hif_read_reg(0xf0, &reg);
  540. do {
  541. /* Set bit 1 */
  542. g_wlan.hif_func.hif_write_reg(0xf0, reg | BIT(0));
  543. /* Check the clock status */
  544. g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
  545. /* in case of clocks off, wait 2ms, and check it again. */
  546. /* if still off, wait for another 2ms, for a total wait of 6ms. */
  547. /* If still off, redo the wake up sequence */
  548. while (((clk_status_reg & 0x1) == 0) && (((++trials) % 3) == 0)) {
  549. /* Wait for the chip to stabilize*/
  550. usleep_range(2 * 1000, 2 * 1000);
  551. /* Make sure chip is awake. This is an extra step that can be removed */
  552. /* later to avoid the bus access overhead */
  553. g_wlan.hif_func.hif_read_reg(0xf1, &clk_status_reg);
  554. if ((clk_status_reg & 0x1) == 0)
  555. wilc_debug(N_ERR, "clocks still OFF. Wake up failed\n");
  556. }
  557. /* in case of failure, Reset the wakeup bit to introduce a new edge on the next loop */
  558. if ((clk_status_reg & 0x1) == 0) {
  559. /* Reset bit 0 */
  560. g_wlan.hif_func.hif_write_reg(0xf0, reg &
  561. (~BIT(0)));
  562. }
  563. } while ((clk_status_reg & 0x1) == 0);
  564. }
  565. if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
  566. g_wlan.hif_func.hif_read_reg(0x1C0C, &reg);
  567. reg &= ~BIT(0);
  568. g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
  569. if (wilc_get_chipid(false) >= 0x1002b0) {
  570. /* Enable PALDO back right after wakeup */
  571. u32 val32;
  572. g_wlan.hif_func.hif_read_reg(0x1e1c, &val32);
  573. val32 |= BIT(6);
  574. g_wlan.hif_func.hif_write_reg(0x1e1c, val32);
  575. g_wlan.hif_func.hif_read_reg(0x1e9c, &val32);
  576. val32 |= BIT(6);
  577. g_wlan.hif_func.hif_write_reg(0x1e9c, val32);
  578. }
  579. }
  580. genuChipPSstate = CHIP_WAKEDUP;
  581. }
  582. #else
  583. static inline void chip_wakeup(void)
  584. {
  585. u32 reg, trials = 0;
  586. do {
  587. if ((g_wlan.io_func.io_type & 0x1) == HIF_SPI) {
  588. g_wlan.hif_func.hif_read_reg(1, &reg);
  589. /* Make sure bit 1 is 0 before we start. */
  590. g_wlan.hif_func.hif_write_reg(1, reg & ~BIT(1));
  591. /* Set bit 1 */
  592. g_wlan.hif_func.hif_write_reg(1, reg | BIT(1));
  593. /* Clear bit 1*/
  594. g_wlan.hif_func.hif_write_reg(1, reg & ~BIT(1));
  595. } else if ((g_wlan.io_func.io_type & 0x1) == HIF_SDIO) {
  596. /* Make sure bit 0 is 0 before we start. */
  597. g_wlan.hif_func.hif_read_reg(0xf0, &reg);
  598. g_wlan.hif_func.hif_write_reg(0xf0, reg & ~BIT(0));
  599. /* Set bit 1 */
  600. g_wlan.hif_func.hif_write_reg(0xf0, reg | BIT(0));
  601. /* Clear bit 1 */
  602. g_wlan.hif_func.hif_write_reg(0xf0, reg & ~BIT(0));
  603. }
  604. do {
  605. /* Wait for the chip to stabilize*/
  606. mdelay(3);
  607. /* Make sure chip is awake. This is an extra step that can be removed */
  608. /* later to avoid the bus access overhead */
  609. if ((wilc_get_chipid(true) == 0))
  610. wilc_debug(N_ERR, "Couldn't read chip id. Wake up failed\n");
  611. } while ((wilc_get_chipid(true) == 0) && ((++trials % 3) == 0));
  612. } while (wilc_get_chipid(true) == 0);
  613. if (genuChipPSstate == CHIP_SLEEPING_MANUAL) {
  614. g_wlan.hif_func.hif_read_reg(0x1C0C, &reg);
  615. reg &= ~BIT(0);
  616. g_wlan.hif_func.hif_write_reg(0x1C0C, reg);
  617. if (wilc_get_chipid(false) >= 0x1002b0) {
  618. /* Enable PALDO back right after wakeup */
  619. u32 val32;
  620. g_wlan.hif_func.hif_read_reg(0x1e1c, &val32);
  621. val32 |= BIT(6);
  622. g_wlan.hif_func.hif_write_reg(0x1e1c, val32);
  623. g_wlan.hif_func.hif_read_reg(0x1e9c, &val32);
  624. val32 |= BIT(6);
  625. g_wlan.hif_func.hif_write_reg(0x1e9c, val32);
  626. }
  627. }
  628. genuChipPSstate = CHIP_WAKEDUP;
  629. }
  630. #endif
  631. void chip_sleep_manually(u32 u32SleepTime)
  632. {
  633. if (genuChipPSstate != CHIP_WAKEDUP) {
  634. /* chip is already sleeping. Do nothing */
  635. return;
  636. }
  637. acquire_bus(ACQUIRE_ONLY);
  638. #ifdef WILC_OPTIMIZE_SLEEP_INT
  639. chip_allow_sleep();
  640. #endif
  641. /* Trigger the manual sleep interrupt */
  642. g_wlan.hif_func.hif_write_reg(0x10a8, 1);
  643. genuChipPSstate = CHIP_SLEEPING_MANUAL;
  644. release_bus(RELEASE_ONLY);
  645. }
  646. /********************************************
  647. *
  648. * Tx, Rx queue handle functions
  649. *
  650. ********************************************/
  651. int wilc_wlan_handle_txq(struct net_device *dev, u32 *pu32TxqCount)
  652. {
  653. wilc_wlan_dev_t *p = (wilc_wlan_dev_t *)&g_wlan;
  654. int i, entries = 0;
  655. u32 sum;
  656. u32 reg;
  657. u8 *txb = p->tx_buffer;
  658. u32 offset = 0;
  659. int vmm_sz = 0;
  660. struct txq_entry_t *tqe;
  661. int ret = 0;
  662. int counter;
  663. int timeout;
  664. u32 vmm_table[WILC_VMM_TBL_SIZE];
  665. perInterface_wlan_t *nic;
  666. struct wilc *wilc;
  667. nic = netdev_priv(dev);
  668. wilc = nic->wilc;
  669. p->txq_exit = 0;
  670. do {
  671. if (p->quit)
  672. break;
  673. linux_wlan_lock_timeout(&wilc->txq_add_to_head_cs,
  674. CFG_PKTS_TIMEOUT);
  675. #ifdef TCP_ACK_FILTER
  676. wilc_wlan_txq_filter_dup_tcp_ack(dev);
  677. #endif
  678. /**
  679. * build the vmm list
  680. **/
  681. PRINT_D(TX_DBG, "Getting the head of the TxQ\n");
  682. tqe = wilc_wlan_txq_get_first();
  683. i = 0;
  684. sum = 0;
  685. do {
  686. if ((tqe != NULL) && (i < (WILC_VMM_TBL_SIZE - 1)) /* reserve last entry to 0 */) {
  687. if (tqe->type == WILC_CFG_PKT)
  688. vmm_sz = ETH_CONFIG_PKT_HDR_OFFSET;
  689. else if (tqe->type == WILC_NET_PKT)
  690. vmm_sz = ETH_ETHERNET_HDR_OFFSET;
  691. else
  692. vmm_sz = HOST_HDR_OFFSET;
  693. vmm_sz += tqe->buffer_size;
  694. PRINT_D(TX_DBG, "VMM Size before alignment = %d\n", vmm_sz);
  695. if (vmm_sz & 0x3) { /* has to be word aligned */
  696. vmm_sz = (vmm_sz + 4) & ~0x3;
  697. }
  698. if ((sum + vmm_sz) > LINUX_TX_SIZE)
  699. break;
  700. PRINT_D(TX_DBG, "VMM Size AFTER alignment = %d\n", vmm_sz);
  701. vmm_table[i] = vmm_sz / 4; /* table take the word size */
  702. PRINT_D(TX_DBG, "VMMTable entry size = %d\n", vmm_table[i]);
  703. if (tqe->type == WILC_CFG_PKT) {
  704. vmm_table[i] |= BIT(10);
  705. PRINT_D(TX_DBG, "VMMTable entry changed for CFG packet = %d\n", vmm_table[i]);
  706. }
  707. #ifdef BIG_ENDIAN
  708. vmm_table[i] = BYTE_SWAP(vmm_table[i]);
  709. #endif
  710. i++;
  711. sum += vmm_sz;
  712. PRINT_D(TX_DBG, "sum = %d\n", sum);
  713. tqe = wilc_wlan_txq_get_next(wilc, tqe);
  714. } else {
  715. break;
  716. }
  717. } while (1);
  718. if (i == 0) { /* nothing in the queue */
  719. PRINT_D(TX_DBG, "Nothing in TX-Q\n");
  720. break;
  721. } else {
  722. PRINT_D(TX_DBG, "Mark the last entry in VMM table - number of previous entries = %d\n", i);
  723. vmm_table[i] = 0x0; /* mark the last element to 0 */
  724. }
  725. acquire_bus(ACQUIRE_AND_WAKEUP);
  726. counter = 0;
  727. do {
  728. ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, &reg);
  729. if (!ret) {
  730. wilc_debug(N_ERR, "[wilc txq]: fail can't read reg vmm_tbl_entry..\n");
  731. break;
  732. }
  733. if ((reg & 0x1) == 0) {
  734. /**
  735. * write to vmm table
  736. **/
  737. PRINT_D(TX_DBG, "Writing VMM table ... with Size = %d\n", ((i + 1) * 4));
  738. break;
  739. } else {
  740. counter++;
  741. if (counter > 200) {
  742. counter = 0;
  743. PRINT_D(TX_DBG, "Looping in tx ctrl , forcce quit\n");
  744. ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, 0);
  745. break;
  746. }
  747. /**
  748. * wait for vmm table is ready
  749. **/
  750. PRINT_WRN(GENERIC_DBG, "[wilc txq]: warn, vmm table not clear yet, wait...\n");
  751. release_bus(RELEASE_ALLOW_SLEEP);
  752. usleep_range(3000, 3000);
  753. acquire_bus(ACQUIRE_AND_WAKEUP);
  754. }
  755. } while (!p->quit);
  756. if (!ret)
  757. goto _end_;
  758. timeout = 200;
  759. do {
  760. /**
  761. * write to vmm table
  762. **/
  763. ret = p->hif_func.hif_block_tx(WILC_VMM_TBL_RX_SHADOW_BASE, (u8 *)vmm_table, ((i + 1) * 4));
  764. if (!ret) {
  765. wilc_debug(N_ERR, "ERR block TX of VMM table.\n");
  766. break;
  767. }
  768. /**
  769. * interrupt firmware
  770. **/
  771. ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x2);
  772. if (!ret) {
  773. wilc_debug(N_ERR, "[wilc txq]: fail can't write reg host_vmm_ctl..\n");
  774. break;
  775. }
  776. /**
  777. * wait for confirm...
  778. **/
  779. do {
  780. ret = p->hif_func.hif_read_reg(WILC_HOST_VMM_CTL, &reg);
  781. if (!ret) {
  782. wilc_debug(N_ERR, "[wilc txq]: fail can't read reg host_vmm_ctl..\n");
  783. break;
  784. }
  785. if ((reg >> 2) & 0x1) {
  786. /**
  787. * Get the entries
  788. **/
  789. entries = ((reg >> 3) & 0x3f);
  790. break;
  791. } else {
  792. release_bus(RELEASE_ALLOW_SLEEP);
  793. usleep_range(3000, 3000);
  794. acquire_bus(ACQUIRE_AND_WAKEUP);
  795. PRINT_WRN(GENERIC_DBG, "Can't get VMM entery - reg = %2x\n", reg);
  796. }
  797. } while (--timeout);
  798. if (timeout <= 0) {
  799. ret = p->hif_func.hif_write_reg(WILC_HOST_VMM_CTL, 0x0);
  800. break;
  801. }
  802. if (!ret)
  803. break;
  804. if (entries == 0) {
  805. PRINT_WRN(GENERIC_DBG, "[wilc txq]: no more buffer in the chip (reg: %08x), retry later [[ %d, %x ]]\n", reg, i, vmm_table[i - 1]);
  806. /* undo the transaction. */
  807. ret = p->hif_func.hif_read_reg(WILC_HOST_TX_CTRL, &reg);
  808. if (!ret) {
  809. wilc_debug(N_ERR, "[wilc txq]: fail can't read reg WILC_HOST_TX_CTRL..\n");
  810. break;
  811. }
  812. reg &= ~BIT(0);
  813. ret = p->hif_func.hif_write_reg(WILC_HOST_TX_CTRL, reg);
  814. if (!ret) {
  815. wilc_debug(N_ERR, "[wilc txq]: fail can't write reg WILC_HOST_TX_CTRL..\n");
  816. break;
  817. }
  818. break;
  819. } else {
  820. break;
  821. }
  822. } while (1);
  823. if (!ret)
  824. goto _end_;
  825. if (entries == 0) {
  826. ret = WILC_TX_ERR_NO_BUF;
  827. goto _end_;
  828. }
  829. /* since copying data into txb takes some time, then
  830. * allow the bus lock to be released let the RX task go. */
  831. release_bus(RELEASE_ALLOW_SLEEP);
  832. /**
  833. * Copy data to the TX buffer
  834. **/
  835. offset = 0;
  836. i = 0;
  837. do {
  838. tqe = wilc_wlan_txq_remove_from_head();
  839. if (tqe != NULL && (vmm_table[i] != 0)) {
  840. u32 header, buffer_offset;
  841. #ifdef BIG_ENDIAN
  842. vmm_table[i] = BYTE_SWAP(vmm_table[i]);
  843. #endif
  844. vmm_sz = (vmm_table[i] & 0x3ff); /* in word unit */
  845. vmm_sz *= 4;
  846. header = (tqe->type << 31) | (tqe->buffer_size << 15) | vmm_sz;
  847. if (tqe->type == WILC_MGMT_PKT)
  848. header |= BIT(30);
  849. else
  850. header &= ~BIT(30);
  851. #ifdef BIG_ENDIAN
  852. header = BYTE_SWAP(header);
  853. #endif
  854. memcpy(&txb[offset], &header, 4);
  855. if (tqe->type == WILC_CFG_PKT) {
  856. buffer_offset = ETH_CONFIG_PKT_HDR_OFFSET;
  857. }
  858. else if (tqe->type == WILC_NET_PKT) {
  859. char *pBSSID = ((struct tx_complete_data *)(tqe->priv))->pBssid;
  860. buffer_offset = ETH_ETHERNET_HDR_OFFSET;
  861. /* copy the bssid at the sart of the buffer */
  862. memcpy(&txb[offset + 4], pBSSID, 6);
  863. }
  864. else {
  865. buffer_offset = HOST_HDR_OFFSET;
  866. }
  867. memcpy(&txb[offset + buffer_offset], tqe->buffer, tqe->buffer_size);
  868. offset += vmm_sz;
  869. i++;
  870. tqe->status = 1; /* mark the packet send */
  871. if (tqe->tx_complete_func)
  872. tqe->tx_complete_func(tqe->priv, tqe->status);
  873. #ifdef TCP_ACK_FILTER
  874. if (tqe->tcp_PendingAck_index != NOT_TCP_ACK)
  875. Pending_Acks_info[tqe->tcp_PendingAck_index].txqe = NULL;
  876. #endif
  877. kfree(tqe);
  878. } else {
  879. break;
  880. }
  881. } while (--entries);
  882. /**
  883. * lock the bus
  884. **/
  885. acquire_bus(ACQUIRE_AND_WAKEUP);
  886. ret = p->hif_func.hif_clear_int_ext(ENABLE_TX_VMM);
  887. if (!ret) {
  888. wilc_debug(N_ERR, "[wilc txq]: fail can't start tx VMM ...\n");
  889. goto _end_;
  890. }
  891. /**
  892. * transfer
  893. **/
  894. ret = p->hif_func.hif_block_tx_ext(0, txb, offset);
  895. if (!ret) {
  896. wilc_debug(N_ERR, "[wilc txq]: fail can't block tx ext...\n");
  897. goto _end_;
  898. }
  899. _end_:
  900. release_bus(RELEASE_ALLOW_SLEEP);
  901. if (ret != 1)
  902. break;
  903. } while (0);
  904. up(&wilc->txq_add_to_head_cs);
  905. p->txq_exit = 1;
  906. PRINT_D(TX_DBG, "THREAD: Exiting txq\n");
  907. /* return tx[]q count */
  908. *pu32TxqCount = p->txq_entries;
  909. return ret;
  910. }
  911. static void wilc_wlan_handle_rxq(struct wilc *wilc)
  912. {
  913. wilc_wlan_dev_t *p = &g_wlan;
  914. int offset = 0, size, has_packet = 0;
  915. u8 *buffer;
  916. struct rxq_entry_t *rqe;
  917. p->rxq_exit = 0;
  918. do {
  919. if (p->quit) {
  920. PRINT_D(RX_DBG, "exit 1st do-while due to Clean_UP function\n");
  921. up(&wilc->cfg_event);
  922. break;
  923. }
  924. rqe = wilc_wlan_rxq_remove(wilc);
  925. if (rqe == NULL) {
  926. PRINT_D(RX_DBG, "nothing in the queue - exit 1st do-while\n");
  927. break;
  928. }
  929. buffer = rqe->buffer;
  930. size = rqe->buffer_size;
  931. PRINT_D(RX_DBG, "rxQ entery Size = %d - Address = %p\n", size, buffer);
  932. offset = 0;
  933. do {
  934. u32 header;
  935. u32 pkt_len, pkt_offset, tp_len;
  936. int is_cfg_packet;
  937. PRINT_D(RX_DBG, "In the 2nd do-while\n");
  938. memcpy(&header, &buffer[offset], 4);
  939. #ifdef BIG_ENDIAN
  940. header = BYTE_SWAP(header);
  941. #endif
  942. PRINT_D(RX_DBG, "Header = %04x - Offset = %d\n", header, offset);
  943. is_cfg_packet = (header >> 31) & 0x1;
  944. pkt_offset = (header >> 22) & 0x1ff;
  945. tp_len = (header >> 11) & 0x7ff;
  946. pkt_len = header & 0x7ff;
  947. if (pkt_len == 0 || tp_len == 0) {
  948. wilc_debug(N_RXQ, "[wilc rxq]: data corrupt, packet len or tp_len is 0 [%d][%d]\n", pkt_len, tp_len);
  949. break;
  950. }
  951. #define IS_MANAGMEMENT 0x100
  952. #define IS_MANAGMEMENT_CALLBACK 0x080
  953. #define IS_MGMT_STATUS_SUCCES 0x040
  954. if (pkt_offset & IS_MANAGMEMENT) {
  955. /* reset mgmt indicator bit, to use pkt_offeset in furthur calculations */
  956. pkt_offset &= ~(IS_MANAGMEMENT | IS_MANAGMEMENT_CALLBACK | IS_MGMT_STATUS_SUCCES);
  957. WILC_WFI_mgmt_rx(wilc, &buffer[offset + HOST_HDR_OFFSET], pkt_len);
  958. }
  959. else
  960. {
  961. if (!is_cfg_packet) {
  962. if (pkt_len > 0) {
  963. frmw_to_linux(wilc,
  964. &buffer[offset],
  965. pkt_len,
  966. pkt_offset);
  967. has_packet = 1;
  968. }
  969. } else {
  970. wilc_cfg_rsp_t rsp;
  971. wilc_wlan_cfg_indicate_rx(&buffer[pkt_offset + offset], pkt_len, &rsp);
  972. if (rsp.type == WILC_CFG_RSP) {
  973. /**
  974. * wake up the waiting task...
  975. **/
  976. PRINT_D(RX_DBG, "p->cfg_seq_no = %d - rsp.seq_no = %d\n", p->cfg_seq_no, rsp.seq_no);
  977. if (p->cfg_seq_no == rsp.seq_no)
  978. up(&wilc->cfg_event);
  979. } else if (rsp.type == WILC_CFG_RSP_STATUS) {
  980. /**
  981. * Call back to indicate status...
  982. **/
  983. linux_wlan_mac_indicate(wilc, WILC_MAC_INDICATE_STATUS);
  984. } else if (rsp.type == WILC_CFG_RSP_SCAN) {
  985. linux_wlan_mac_indicate(wilc, WILC_MAC_INDICATE_SCAN);
  986. }
  987. }
  988. }
  989. offset += tp_len;
  990. if (offset >= size)
  991. break;
  992. } while (1);
  993. #ifndef MEMORY_STATIC
  994. kfree(buffer);
  995. #endif
  996. kfree(rqe);
  997. if (has_packet)
  998. linux_wlan_rx_complete();
  999. } while (1);
  1000. p->rxq_exit = 1;
  1001. PRINT_D(RX_DBG, "THREAD: Exiting RX thread\n");
  1002. }
  1003. /********************************************
  1004. *
  1005. * Fast DMA Isr
  1006. *
  1007. ********************************************/
  1008. static void wilc_unknown_isr_ext(void)
  1009. {
  1010. g_wlan.hif_func.hif_clear_int_ext(0);
  1011. }
  1012. static void wilc_pllupdate_isr_ext(u32 int_stats)
  1013. {
  1014. int trials = 10;
  1015. g_wlan.hif_func.hif_clear_int_ext(PLL_INT_CLR);
  1016. /* Waiting for PLL */
  1017. mdelay(WILC_PLL_TO);
  1018. /* poll till read a valid data */
  1019. while (!(ISWILC1000(wilc_get_chipid(true)) && --trials)) {
  1020. PRINT_D(TX_DBG, "PLL update retrying\n");
  1021. mdelay(1);
  1022. }
  1023. }
  1024. static void wilc_sleeptimer_isr_ext(u32 int_stats1)
  1025. {
  1026. g_wlan.hif_func.hif_clear_int_ext(SLEEP_INT_CLR);
  1027. #ifndef WILC_OPTIMIZE_SLEEP_INT
  1028. genuChipPSstate = CHIP_SLEEPING_AUTO;
  1029. #endif
  1030. }
  1031. static void wilc_wlan_handle_isr_ext(struct wilc *wilc, u32 int_status)
  1032. {
  1033. wilc_wlan_dev_t *p = &g_wlan;
  1034. #ifdef MEMORY_STATIC
  1035. u32 offset = p->rx_buffer_offset;
  1036. #endif
  1037. u8 *buffer = NULL;
  1038. u32 size;
  1039. u32 retries = 0;
  1040. int ret = 0;
  1041. struct rxq_entry_t *rqe;
  1042. /**
  1043. * Get the rx size
  1044. **/
  1045. size = ((int_status & 0x7fff) << 2);
  1046. while (!size && retries < 10) {
  1047. u32 time = 0;
  1048. /*looping more secure*/
  1049. /*zero size make a crashe because the dma will not happen and that will block the firmware*/
  1050. wilc_debug(N_ERR, "RX Size equal zero ... Trying to read it again for %d time\n", time++);
  1051. p->hif_func.hif_read_size(&size);
  1052. size = ((size & 0x7fff) << 2);
  1053. retries++;
  1054. }
  1055. if (size > 0) {
  1056. #ifdef MEMORY_STATIC
  1057. if (LINUX_RX_SIZE - offset < size)
  1058. offset = 0;
  1059. if (p->rx_buffer)
  1060. buffer = &p->rx_buffer[offset];
  1061. else {
  1062. wilc_debug(N_ERR, "[wilc isr]: fail Rx Buffer is NULL...drop the packets (%d)\n", size);
  1063. goto _end_;
  1064. }
  1065. #else
  1066. buffer = kmalloc(size, GFP_KERNEL);
  1067. if (buffer == NULL) {
  1068. wilc_debug(N_ERR, "[wilc isr]: fail alloc host memory...drop the packets (%d)\n", size);
  1069. usleep_range(100 * 1000, 100 * 1000);
  1070. goto _end_;
  1071. }
  1072. #endif
  1073. /**
  1074. * clear the chip's interrupt after getting size some register getting corrupted after clear the interrupt
  1075. **/
  1076. p->hif_func.hif_clear_int_ext(DATA_INT_CLR | ENABLE_RX_VMM);
  1077. /**
  1078. * start transfer
  1079. **/
  1080. ret = p->hif_func.hif_block_rx_ext(0, buffer, size);
  1081. if (!ret) {
  1082. wilc_debug(N_ERR, "[wilc isr]: fail block rx...\n");
  1083. goto _end_;
  1084. }
  1085. _end_:
  1086. if (ret) {
  1087. #ifdef MEMORY_STATIC
  1088. offset += size;
  1089. p->rx_buffer_offset = offset;
  1090. #endif
  1091. /**
  1092. * add to rx queue
  1093. **/
  1094. rqe = kmalloc(sizeof(struct rxq_entry_t), GFP_KERNEL);
  1095. if (rqe != NULL) {
  1096. rqe->buffer = buffer;
  1097. rqe->buffer_size = size;
  1098. PRINT_D(RX_DBG, "rxq entery Size= %d - Address = %p\n", rqe->buffer_size, rqe->buffer);
  1099. wilc_wlan_rxq_add(wilc, rqe);
  1100. }
  1101. } else {
  1102. #ifndef MEMORY_STATIC
  1103. kfree(buffer);
  1104. #endif
  1105. }
  1106. }
  1107. wilc_wlan_handle_rxq(wilc);
  1108. }
  1109. void wilc_handle_isr(void *wilc)
  1110. {
  1111. u32 int_status;
  1112. acquire_bus(ACQUIRE_AND_WAKEUP);
  1113. g_wlan.hif_func.hif_read_int(&int_status);
  1114. if (int_status & PLL_INT_EXT)
  1115. wilc_pllupdate_isr_ext(int_status);
  1116. if (int_status & DATA_INT_EXT) {
  1117. wilc_wlan_handle_isr_ext(wilc, int_status);
  1118. #ifndef WILC_OPTIMIZE_SLEEP_INT
  1119. /* Chip is up and talking*/
  1120. genuChipPSstate = CHIP_WAKEDUP;
  1121. #endif
  1122. }
  1123. if (int_status & SLEEP_INT_EXT)
  1124. wilc_sleeptimer_isr_ext(int_status);
  1125. if (!(int_status & (ALL_INT_EXT))) {
  1126. #ifdef WILC_SDIO
  1127. PRINT_D(TX_DBG, ">> UNKNOWN_INTERRUPT - 0x%08x\n", int_status);
  1128. #endif
  1129. wilc_unknown_isr_ext();
  1130. }
  1131. release_bus(RELEASE_ALLOW_SLEEP);
  1132. }
  1133. /********************************************
  1134. *
  1135. * Firmware download
  1136. *
  1137. ********************************************/
  1138. int wilc_wlan_firmware_download(const u8 *buffer, u32 buffer_size)
  1139. {
  1140. wilc_wlan_dev_t *p = &g_wlan;
  1141. u32 offset;
  1142. u32 addr, size, size2, blksz;
  1143. u8 *dma_buffer;
  1144. int ret = 0;
  1145. blksz = BIT(12);
  1146. /* Allocate a DMA coherent buffer. */
  1147. dma_buffer = kmalloc(blksz, GFP_KERNEL);
  1148. if (dma_buffer == NULL) {
  1149. /*EIO 5*/
  1150. ret = -5;
  1151. PRINT_ER("Can't allocate buffer for firmware download IO error\n ");
  1152. goto _fail_1;
  1153. }
  1154. PRINT_D(INIT_DBG, "Downloading firmware size = %d ...\n", buffer_size);
  1155. /**
  1156. * load the firmware
  1157. **/
  1158. offset = 0;
  1159. do {
  1160. memcpy(&addr, &buffer[offset], 4);
  1161. memcpy(&size, &buffer[offset + 4], 4);
  1162. #ifdef BIG_ENDIAN
  1163. addr = BYTE_SWAP(addr);
  1164. size = BYTE_SWAP(size);
  1165. #endif
  1166. acquire_bus(ACQUIRE_ONLY);
  1167. offset += 8;
  1168. while (((int)size) && (offset < buffer_size)) {
  1169. if (size <= blksz)
  1170. size2 = size;
  1171. else
  1172. size2 = blksz;
  1173. /* Copy firmware into a DMA coherent buffer */
  1174. memcpy(dma_buffer, &buffer[offset], size2);
  1175. ret = p->hif_func.hif_block_tx(addr, dma_buffer, size2);
  1176. if (!ret)
  1177. break;
  1178. addr += size2;
  1179. offset += size2;
  1180. size -= size2;
  1181. }
  1182. release_bus(RELEASE_ONLY);
  1183. if (!ret) {
  1184. /*EIO 5*/
  1185. ret = -5;
  1186. PRINT_ER("Can't download firmware IO error\n ");
  1187. goto _fail_;
  1188. }
  1189. PRINT_D(INIT_DBG, "Offset = %d\n", offset);
  1190. } while (offset < buffer_size);
  1191. _fail_:
  1192. kfree(dma_buffer);
  1193. _fail_1:
  1194. return (ret < 0) ? ret : 0;
  1195. }
  1196. /********************************************
  1197. *
  1198. * Common
  1199. *
  1200. ********************************************/
  1201. int wilc_wlan_start(void)
  1202. {
  1203. wilc_wlan_dev_t *p = &g_wlan;
  1204. u32 reg = 0;
  1205. int ret;
  1206. u32 chipid;
  1207. /**
  1208. * Set the host interface
  1209. **/
  1210. if (p->io_func.io_type == HIF_SDIO) {
  1211. reg = 0;
  1212. reg |= BIT(3); /* bug 4456 and 4557 */
  1213. } else if (p->io_func.io_type == HIF_SPI) {
  1214. reg = 1;
  1215. }
  1216. acquire_bus(ACQUIRE_ONLY);
  1217. ret = p->hif_func.hif_write_reg(WILC_VMM_CORE_CFG, reg);
  1218. if (!ret) {
  1219. wilc_debug(N_ERR, "[wilc start]: fail write reg vmm_core_cfg...\n");
  1220. release_bus(RELEASE_ONLY);
  1221. /* EIO 5*/
  1222. ret = -5;
  1223. return ret;
  1224. }
  1225. reg = 0;
  1226. #ifdef WILC_SDIO_IRQ_GPIO
  1227. reg |= WILC_HAVE_SDIO_IRQ_GPIO;
  1228. #endif
  1229. #ifdef WILC_DISABLE_PMU
  1230. #else
  1231. reg |= WILC_HAVE_USE_PMU;
  1232. #endif
  1233. #ifdef WILC_SLEEP_CLK_SRC_XO
  1234. reg |= WILC_HAVE_SLEEP_CLK_SRC_XO;
  1235. #elif defined WILC_SLEEP_CLK_SRC_RTC
  1236. reg |= WILC_HAVE_SLEEP_CLK_SRC_RTC;
  1237. #endif
  1238. #ifdef WILC_EXT_PA_INV_TX_RX
  1239. reg |= WILC_HAVE_EXT_PA_INV_TX_RX;
  1240. #endif
  1241. reg |= WILC_HAVE_LEGACY_RF_SETTINGS;
  1242. /*Set oscillator frequency*/
  1243. #ifdef XTAL_24
  1244. reg |= WILC_HAVE_XTAL_24;
  1245. #endif
  1246. /*Enable/Disable GPIO configuration for FW logs*/
  1247. #ifdef DISABLE_WILC_UART
  1248. reg |= WILC_HAVE_DISABLE_WILC_UART;
  1249. #endif
  1250. ret = p->hif_func.hif_write_reg(WILC_GP_REG_1, reg);
  1251. if (!ret) {
  1252. wilc_debug(N_ERR, "[wilc start]: fail write WILC_GP_REG_1 ...\n");
  1253. release_bus(RELEASE_ONLY);
  1254. /* EIO 5*/
  1255. ret = -5;
  1256. return ret;
  1257. }
  1258. /**
  1259. * Bus related
  1260. **/
  1261. p->hif_func.hif_sync_ext(NUM_INT_EXT);
  1262. ret = p->hif_func.hif_read_reg(0x1000, &chipid);
  1263. if (!ret) {
  1264. wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1000 ...\n");
  1265. release_bus(RELEASE_ONLY);
  1266. /* EIO 5*/
  1267. ret = -5;
  1268. return ret;
  1269. }
  1270. /**
  1271. * Go...
  1272. **/
  1273. p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
  1274. if ((reg & BIT(10)) == BIT(10)) {
  1275. reg &= ~BIT(10);
  1276. p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
  1277. p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
  1278. }
  1279. reg |= BIT(10);
  1280. ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
  1281. p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
  1282. release_bus(RELEASE_ONLY);
  1283. return (ret < 0) ? ret : 0;
  1284. }
  1285. void wilc_wlan_global_reset(void)
  1286. {
  1287. wilc_wlan_dev_t *p = &g_wlan;
  1288. acquire_bus(ACQUIRE_AND_WAKEUP);
  1289. p->hif_func.hif_write_reg(WILC_GLB_RESET_0, 0x0);
  1290. release_bus(RELEASE_ONLY);
  1291. }
  1292. int wilc_wlan_stop(void)
  1293. {
  1294. wilc_wlan_dev_t *p = &g_wlan;
  1295. u32 reg = 0;
  1296. int ret;
  1297. u8 timeout = 10;
  1298. /**
  1299. * TODO: stop the firmware, need a re-download
  1300. **/
  1301. acquire_bus(ACQUIRE_AND_WAKEUP);
  1302. ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
  1303. if (!ret) {
  1304. PRINT_ER("Error while reading reg\n");
  1305. release_bus(RELEASE_ALLOW_SLEEP);
  1306. return ret;
  1307. }
  1308. reg &= ~BIT(10);
  1309. ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
  1310. if (!ret) {
  1311. PRINT_ER("Error while writing reg\n");
  1312. release_bus(RELEASE_ALLOW_SLEEP);
  1313. return ret;
  1314. }
  1315. do {
  1316. ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
  1317. if (!ret) {
  1318. PRINT_ER("Error while reading reg\n");
  1319. release_bus(RELEASE_ALLOW_SLEEP);
  1320. return ret;
  1321. }
  1322. PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
  1323. /*Workaround to ensure that the chip is actually reset*/
  1324. if ((reg & BIT(10))) {
  1325. PRINT_D(GENERIC_DBG, "Bit 10 not reset : Retry %d\n", timeout);
  1326. reg &= ~BIT(10);
  1327. ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
  1328. timeout--;
  1329. } else {
  1330. PRINT_D(GENERIC_DBG, "Bit 10 reset after : Retry %d\n", timeout);
  1331. ret = p->hif_func.hif_read_reg(WILC_GLB_RESET_0, &reg);
  1332. if (!ret) {
  1333. PRINT_ER("Error while reading reg\n");
  1334. release_bus(RELEASE_ALLOW_SLEEP);
  1335. return ret;
  1336. }
  1337. PRINT_D(GENERIC_DBG, "Read RESET Reg %x : Retry%d\n", reg, timeout);
  1338. break;
  1339. }
  1340. } while (timeout);
  1341. reg = (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(8) | BIT(9) | BIT(26) |
  1342. BIT(29) | BIT(30) | BIT(31));
  1343. p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
  1344. reg = (u32)~BIT(10);
  1345. ret = p->hif_func.hif_write_reg(WILC_GLB_RESET_0, reg);
  1346. release_bus(RELEASE_ALLOW_SLEEP);
  1347. return ret;
  1348. }
  1349. void wilc_wlan_cleanup(struct net_device *dev)
  1350. {
  1351. wilc_wlan_dev_t *p = &g_wlan;
  1352. struct txq_entry_t *tqe;
  1353. struct rxq_entry_t *rqe;
  1354. u32 reg = 0;
  1355. int ret;
  1356. perInterface_wlan_t *nic;
  1357. struct wilc *wilc;
  1358. nic = netdev_priv(dev);
  1359. wilc = nic->wilc;
  1360. p->quit = 1;
  1361. do {
  1362. tqe = wilc_wlan_txq_remove_from_head();
  1363. if (tqe == NULL)
  1364. break;
  1365. if (tqe->tx_complete_func)
  1366. tqe->tx_complete_func(tqe->priv, 0);
  1367. kfree(tqe);
  1368. } while (1);
  1369. do {
  1370. rqe = wilc_wlan_rxq_remove(wilc);
  1371. if (rqe == NULL)
  1372. break;
  1373. #ifndef MEMORY_STATIC
  1374. kfree(rqe->buffer);
  1375. #endif
  1376. kfree(rqe);
  1377. } while (1);
  1378. /**
  1379. * clean up buffer
  1380. **/
  1381. #ifdef MEMORY_STATIC
  1382. kfree(p->rx_buffer);
  1383. p->rx_buffer = NULL;
  1384. #endif
  1385. kfree(p->tx_buffer);
  1386. acquire_bus(ACQUIRE_AND_WAKEUP);
  1387. ret = p->hif_func.hif_read_reg(WILC_GP_REG_0, &reg);
  1388. if (!ret) {
  1389. PRINT_ER("Error while reading reg\n");
  1390. release_bus(RELEASE_ALLOW_SLEEP);
  1391. }
  1392. PRINT_ER("Writing ABORT reg\n");
  1393. ret = p->hif_func.hif_write_reg(WILC_GP_REG_0, (reg | ABORT_INT));
  1394. if (!ret) {
  1395. PRINT_ER("Error while writing reg\n");
  1396. release_bus(RELEASE_ALLOW_SLEEP);
  1397. }
  1398. release_bus(RELEASE_ALLOW_SLEEP);
  1399. /**
  1400. * io clean up
  1401. **/
  1402. p->hif_func.hif_deinit(NULL);
  1403. }
  1404. static int wilc_wlan_cfg_commit(int type, u32 drvHandler)
  1405. {
  1406. wilc_wlan_dev_t *p = &g_wlan;
  1407. wilc_cfg_frame_t *cfg = &p->cfg_frame;
  1408. int total_len = p->cfg_frame_offset + 4 + DRIVER_HANDLER_SIZE;
  1409. int seq_no = p->cfg_seq_no % 256;
  1410. int driver_handler = (u32)drvHandler;
  1411. /**
  1412. * Set up header
  1413. **/
  1414. if (type == WILC_CFG_SET) { /* Set */
  1415. cfg->wid_header[0] = 'W';
  1416. } else { /* Query */
  1417. cfg->wid_header[0] = 'Q';
  1418. }
  1419. cfg->wid_header[1] = seq_no; /* sequence number */
  1420. cfg->wid_header[2] = (u8)total_len;
  1421. cfg->wid_header[3] = (u8)(total_len >> 8);
  1422. cfg->wid_header[4] = (u8)driver_handler;
  1423. cfg->wid_header[5] = (u8)(driver_handler >> 8);
  1424. cfg->wid_header[6] = (u8)(driver_handler >> 16);
  1425. cfg->wid_header[7] = (u8)(driver_handler >> 24);
  1426. p->cfg_seq_no = seq_no;
  1427. /**
  1428. * Add to TX queue
  1429. **/
  1430. if (!wilc_wlan_txq_add_cfg_pkt(&cfg->wid_header[0], total_len))
  1431. return -1;
  1432. return 0;
  1433. }
  1434. int wilc_wlan_cfg_set(int start, u32 wid, u8 *buffer, u32 buffer_size,
  1435. int commit, u32 drvHandler)
  1436. {
  1437. wilc_wlan_dev_t *p = &g_wlan;
  1438. u32 offset;
  1439. int ret_size;
  1440. if (p->cfg_frame_in_use)
  1441. return 0;
  1442. if (start)
  1443. p->cfg_frame_offset = 0;
  1444. offset = p->cfg_frame_offset;
  1445. ret_size = wilc_wlan_cfg_set_wid(p->cfg_frame.frame, offset, (u16)wid,
  1446. buffer, buffer_size);
  1447. offset += ret_size;
  1448. p->cfg_frame_offset = offset;
  1449. if (commit) {
  1450. PRINT_D(TX_DBG, "[WILC]PACKET Commit with sequence number %d\n", p->cfg_seq_no);
  1451. PRINT_D(RX_DBG, "Processing cfg_set()\n");
  1452. p->cfg_frame_in_use = 1;
  1453. if (wilc_wlan_cfg_commit(WILC_CFG_SET, drvHandler))
  1454. ret_size = 0;
  1455. if (linux_wlan_lock_timeout(&g_linux_wlan->cfg_event,
  1456. CFG_PKTS_TIMEOUT)) {
  1457. PRINT_D(TX_DBG, "Set Timed Out\n");
  1458. ret_size = 0;
  1459. }
  1460. p->cfg_frame_in_use = 0;
  1461. p->cfg_frame_offset = 0;
  1462. p->cfg_seq_no += 1;
  1463. }
  1464. return ret_size;
  1465. }
  1466. int wilc_wlan_cfg_get(int start, u32 wid, int commit, u32 drvHandler)
  1467. {
  1468. wilc_wlan_dev_t *p = &g_wlan;
  1469. u32 offset;
  1470. int ret_size;
  1471. if (p->cfg_frame_in_use)
  1472. return 0;
  1473. if (start)
  1474. p->cfg_frame_offset = 0;
  1475. offset = p->cfg_frame_offset;
  1476. ret_size = wilc_wlan_cfg_get_wid(p->cfg_frame.frame, offset, (u16)wid);
  1477. offset += ret_size;
  1478. p->cfg_frame_offset = offset;
  1479. if (commit) {
  1480. p->cfg_frame_in_use = 1;
  1481. if (wilc_wlan_cfg_commit(WILC_CFG_QUERY, drvHandler))
  1482. ret_size = 0;
  1483. if (linux_wlan_lock_timeout(&g_linux_wlan->cfg_event,
  1484. CFG_PKTS_TIMEOUT)) {
  1485. PRINT_D(TX_DBG, "Get Timed Out\n");
  1486. ret_size = 0;
  1487. }
  1488. PRINT_D(GENERIC_DBG, "[WILC]Get Response received\n");
  1489. p->cfg_frame_in_use = 0;
  1490. p->cfg_frame_offset = 0;
  1491. p->cfg_seq_no += 1;
  1492. }
  1493. return ret_size;
  1494. }
  1495. int wilc_wlan_cfg_get_val(u32 wid, u8 *buffer, u32 buffer_size)
  1496. {
  1497. int ret;
  1498. ret = wilc_wlan_cfg_get_wid_value((u16)wid, buffer, buffer_size);
  1499. return ret;
  1500. }
  1501. void wilc_bus_set_max_speed(void)
  1502. {
  1503. /* Increase bus speed to max possible. */
  1504. g_wlan.hif_func.hif_set_max_bus_speed();
  1505. }
  1506. void wilc_bus_set_default_speed(void)
  1507. {
  1508. /* Restore bus speed to default. */
  1509. g_wlan.hif_func.hif_set_default_bus_speed();
  1510. }
  1511. u32 init_chip(void)
  1512. {
  1513. u32 chipid;
  1514. u32 reg, ret = 0;
  1515. acquire_bus(ACQUIRE_ONLY);
  1516. chipid = wilc_get_chipid(true);
  1517. if ((chipid & 0xfff) != 0xa0) {
  1518. /**
  1519. * Avoid booting from boot ROM. Make sure that Drive IRQN [SDIO platform]
  1520. * or SD_DAT3 [SPI platform] to ?1?
  1521. **/
  1522. /* Set cortus reset register to register control. */
  1523. ret = g_wlan.hif_func.hif_read_reg(0x1118, &reg);
  1524. if (!ret) {
  1525. wilc_debug(N_ERR, "[wilc start]: fail read reg 0x1118 ...\n");
  1526. return ret;
  1527. }
  1528. reg |= BIT(0);
  1529. ret = g_wlan.hif_func.hif_write_reg(0x1118, reg);
  1530. if (!ret) {
  1531. wilc_debug(N_ERR, "[wilc start]: fail write reg 0x1118 ...\n");
  1532. return ret;
  1533. }
  1534. /**
  1535. * Write branch intruction to IRAM (0x71 trap) at location 0xFFFF0000
  1536. * (Cortus map) or C0000 (AHB map).
  1537. **/
  1538. ret = g_wlan.hif_func.hif_write_reg(0xc0000, 0x71);
  1539. if (!ret) {
  1540. wilc_debug(N_ERR, "[wilc start]: fail write reg 0xc0000 ...\n");
  1541. return ret;
  1542. }
  1543. }
  1544. release_bus(RELEASE_ONLY);
  1545. return ret;
  1546. }
  1547. u32 wilc_get_chipid(u8 update)
  1548. {
  1549. static u32 chipid;
  1550. /* SDIO can't read into global variables */
  1551. /* Use this variable as a temp, then copy to the global */
  1552. u32 tempchipid = 0;
  1553. u32 rfrevid;
  1554. if (chipid == 0 || update != 0) {
  1555. g_wlan.hif_func.hif_read_reg(0x1000, &tempchipid);
  1556. g_wlan.hif_func.hif_read_reg(0x13f4, &rfrevid);
  1557. if (!ISWILC1000(tempchipid)) {
  1558. chipid = 0;
  1559. goto _fail_;
  1560. }
  1561. if (tempchipid == 0x1002a0) {
  1562. if (rfrevid == 0x1) { /* 1002A0 */
  1563. } else { /* if (rfrevid == 0x2) */ /* 1002A1 */
  1564. tempchipid = 0x1002a1;
  1565. }
  1566. } else if (tempchipid == 0x1002b0) {
  1567. if (rfrevid == 3) { /* 1002B0 */
  1568. } else if (rfrevid == 4) { /* 1002B1 */
  1569. tempchipid = 0x1002b1;
  1570. } else { /* if(rfrevid == 5) */ /* 1002B2 */
  1571. tempchipid = 0x1002b2;
  1572. }
  1573. } else {
  1574. }
  1575. chipid = tempchipid;
  1576. }
  1577. _fail_:
  1578. return chipid;
  1579. }
  1580. int wilc_wlan_init(wilc_wlan_inp_t *inp)
  1581. {
  1582. int ret = 0;
  1583. PRINT_D(INIT_DBG, "Initializing WILC_Wlan ...\n");
  1584. memset((void *)&g_wlan, 0, sizeof(wilc_wlan_dev_t));
  1585. /**
  1586. * store the input
  1587. **/
  1588. memcpy((void *)&g_wlan.io_func, (void *)&inp->io_func, sizeof(wilc_wlan_io_func_t));
  1589. /***
  1590. * host interface init
  1591. **/
  1592. if ((inp->io_func.io_type & 0x1) == HIF_SDIO) {
  1593. if (!hif_sdio.hif_init(inp, wilc_debug)) {
  1594. /* EIO 5 */
  1595. ret = -5;
  1596. goto _fail_;
  1597. }
  1598. memcpy((void *)&g_wlan.hif_func, &hif_sdio, sizeof(wilc_hif_func_t));
  1599. } else {
  1600. if ((inp->io_func.io_type & 0x1) == HIF_SPI) {
  1601. /**
  1602. * TODO:
  1603. **/
  1604. if (!hif_spi.hif_init(inp, wilc_debug)) {
  1605. /* EIO 5 */
  1606. ret = -5;
  1607. goto _fail_;
  1608. }
  1609. memcpy((void *)&g_wlan.hif_func, &hif_spi, sizeof(wilc_hif_func_t));
  1610. } else {
  1611. /* EIO 5 */
  1612. ret = -5;
  1613. goto _fail_;
  1614. }
  1615. }
  1616. /***
  1617. * mac interface init
  1618. **/
  1619. if (!wilc_wlan_cfg_init(wilc_debug)) {
  1620. /* ENOBUFS 105 */
  1621. ret = -105;
  1622. goto _fail_;
  1623. }
  1624. /**
  1625. * alloc tx, rx buffer
  1626. **/
  1627. if (g_wlan.tx_buffer == NULL)
  1628. g_wlan.tx_buffer = kmalloc(LINUX_TX_SIZE, GFP_KERNEL);
  1629. PRINT_D(TX_DBG, "g_wlan.tx_buffer = %p\n", g_wlan.tx_buffer);
  1630. if (g_wlan.tx_buffer == NULL) {
  1631. /* ENOBUFS 105 */
  1632. ret = -105;
  1633. PRINT_ER("Can't allocate Tx Buffer");
  1634. goto _fail_;
  1635. }
  1636. /* rx_buffer is not used unless we activate USE_MEM STATIC which is not applicable, allocating such memory is useless*/
  1637. #if defined (MEMORY_STATIC)
  1638. if (g_wlan.rx_buffer == NULL)
  1639. g_wlan.rx_buffer = kmalloc(LINUX_RX_SIZE, GFP_KERNEL);
  1640. PRINT_D(TX_DBG, "g_wlan.rx_buffer =%p\n", g_wlan.rx_buffer);
  1641. if (g_wlan.rx_buffer == NULL) {
  1642. /* ENOBUFS 105 */
  1643. ret = -105;
  1644. PRINT_ER("Can't allocate Rx Buffer");
  1645. goto _fail_;
  1646. }
  1647. #endif
  1648. if (!init_chip()) {
  1649. /* EIO 5 */
  1650. ret = -5;
  1651. goto _fail_;
  1652. }
  1653. #ifdef TCP_ACK_FILTER
  1654. Init_TCP_tracking();
  1655. #endif
  1656. return 1;
  1657. _fail_:
  1658. #ifdef MEMORY_STATIC
  1659. kfree(g_wlan.rx_buffer);
  1660. g_wlan.rx_buffer = NULL;
  1661. #endif
  1662. kfree(g_wlan.tx_buffer);
  1663. g_wlan.tx_buffer = NULL;
  1664. return ret;
  1665. }
  1666. u16 Set_machw_change_vir_if(struct net_device *dev, bool bValue)
  1667. {
  1668. u16 ret;
  1669. u32 reg;
  1670. perInterface_wlan_t *nic;
  1671. struct wilc *wilc;
  1672. nic = netdev_priv(dev);
  1673. wilc = nic->wilc;
  1674. /*Reset WILC_CHANGING_VIR_IF register to allow adding futrue keys to CE H/W*/
  1675. mutex_lock(&wilc->hif_cs);
  1676. ret = (&g_wlan)->hif_func.hif_read_reg(WILC_CHANGING_VIR_IF, &reg);
  1677. if (!ret)
  1678. PRINT_ER("Error while Reading reg WILC_CHANGING_VIR_IF\n");
  1679. if (bValue)
  1680. reg |= BIT(31);
  1681. else
  1682. reg &= ~BIT(31);
  1683. ret = (&g_wlan)->hif_func.hif_write_reg(WILC_CHANGING_VIR_IF, reg);
  1684. if (!ret)
  1685. PRINT_ER("Error while writing reg WILC_CHANGING_VIR_IF\n");
  1686. mutex_unlock(&wilc->hif_cs);
  1687. return ret;
  1688. }