init.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  1. /*
  2. * Copyright (c) 2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/of.h>
  17. #include <linux/mmc/sdio_func.h>
  18. #include "core.h"
  19. #include "cfg80211.h"
  20. #include "target.h"
  21. #include "debug.h"
  22. #include "hif-ops.h"
  23. unsigned int debug_mask;
  24. static unsigned int testmode;
  25. module_param(debug_mask, uint, 0644);
  26. module_param(testmode, uint, 0644);
  27. /*
  28. * Include definitions here that can be used to tune the WLAN module
  29. * behavior. Different customers can tune the behavior as per their needs,
  30. * here.
  31. */
  32. /*
  33. * This configuration item enable/disable keepalive support.
  34. * Keepalive support: In the absence of any data traffic to AP, null
  35. * frames will be sent to the AP at periodic interval, to keep the association
  36. * active. This configuration item defines the periodic interval.
  37. * Use value of zero to disable keepalive support
  38. * Default: 60 seconds
  39. */
  40. #define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
  41. /*
  42. * This configuration item sets the value of disconnect timeout
  43. * Firmware delays sending the disconnec event to the host for this
  44. * timeout after is gets disconnected from the current AP.
  45. * If the firmware successly roams within the disconnect timeout
  46. * it sends a new connect event
  47. */
  48. #define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
  49. #define CONFIG_AR600x_DEBUG_UART_TX_PIN 8
  50. #define ATH6KL_DATA_OFFSET 64
  51. struct sk_buff *ath6kl_buf_alloc(int size)
  52. {
  53. struct sk_buff *skb;
  54. u16 reserved;
  55. /* Add chacheline space at front and back of buffer */
  56. reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
  57. sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
  58. skb = dev_alloc_skb(size + reserved);
  59. if (skb)
  60. skb_reserve(skb, reserved - L1_CACHE_BYTES);
  61. return skb;
  62. }
  63. void ath6kl_init_profile_info(struct ath6kl *ar)
  64. {
  65. ar->ssid_len = 0;
  66. memset(ar->ssid, 0, sizeof(ar->ssid));
  67. ar->dot11_auth_mode = OPEN_AUTH;
  68. ar->auth_mode = NONE_AUTH;
  69. ar->prwise_crypto = NONE_CRYPT;
  70. ar->prwise_crypto_len = 0;
  71. ar->grp_crypto = NONE_CRYPT;
  72. ar->grp_crypto_len = 0;
  73. memset(ar->wep_key_list, 0, sizeof(ar->wep_key_list));
  74. memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
  75. memset(ar->bssid, 0, sizeof(ar->bssid));
  76. ar->bss_ch = 0;
  77. ar->nw_type = ar->next_mode = INFRA_NETWORK;
  78. }
  79. static u8 ath6kl_get_fw_iftype(struct ath6kl *ar)
  80. {
  81. switch (ar->nw_type) {
  82. case INFRA_NETWORK:
  83. return HI_OPTION_FW_MODE_BSS_STA;
  84. case ADHOC_NETWORK:
  85. return HI_OPTION_FW_MODE_IBSS;
  86. case AP_NETWORK:
  87. return HI_OPTION_FW_MODE_AP;
  88. default:
  89. ath6kl_err("Unsupported interface type :%d\n", ar->nw_type);
  90. return 0xff;
  91. }
  92. }
  93. static int ath6kl_set_host_app_area(struct ath6kl *ar)
  94. {
  95. u32 address, data;
  96. struct host_app_area host_app_area;
  97. /* Fetch the address of the host_app_area_s
  98. * instance in the host interest area */
  99. address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
  100. address = TARG_VTOP(ar->target_type, address);
  101. if (ath6kl_diag_read32(ar, address, &data))
  102. return -EIO;
  103. address = TARG_VTOP(ar->target_type, data);
  104. host_app_area.wmi_protocol_ver = WMI_PROTOCOL_VERSION;
  105. if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
  106. sizeof(struct host_app_area)))
  107. return -EIO;
  108. return 0;
  109. }
  110. static inline void set_ac2_ep_map(struct ath6kl *ar,
  111. u8 ac,
  112. enum htc_endpoint_id ep)
  113. {
  114. ar->ac2ep_map[ac] = ep;
  115. ar->ep2ac_map[ep] = ac;
  116. }
  117. /* connect to a service */
  118. static int ath6kl_connectservice(struct ath6kl *ar,
  119. struct htc_service_connect_req *con_req,
  120. char *desc)
  121. {
  122. int status;
  123. struct htc_service_connect_resp response;
  124. memset(&response, 0, sizeof(response));
  125. status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
  126. if (status) {
  127. ath6kl_err("failed to connect to %s service status:%d\n",
  128. desc, status);
  129. return status;
  130. }
  131. switch (con_req->svc_id) {
  132. case WMI_CONTROL_SVC:
  133. if (test_bit(WMI_ENABLED, &ar->flag))
  134. ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
  135. ar->ctrl_ep = response.endpoint;
  136. break;
  137. case WMI_DATA_BE_SVC:
  138. set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
  139. break;
  140. case WMI_DATA_BK_SVC:
  141. set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
  142. break;
  143. case WMI_DATA_VI_SVC:
  144. set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
  145. break;
  146. case WMI_DATA_VO_SVC:
  147. set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
  148. break;
  149. default:
  150. ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
  151. return -EINVAL;
  152. }
  153. return 0;
  154. }
  155. static int ath6kl_init_service_ep(struct ath6kl *ar)
  156. {
  157. struct htc_service_connect_req connect;
  158. memset(&connect, 0, sizeof(connect));
  159. /* these fields are the same for all service endpoints */
  160. connect.ep_cb.rx = ath6kl_rx;
  161. connect.ep_cb.rx_refill = ath6kl_rx_refill;
  162. connect.ep_cb.tx_full = ath6kl_tx_queue_full;
  163. /*
  164. * Set the max queue depth so that our ath6kl_tx_queue_full handler
  165. * gets called.
  166. */
  167. connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
  168. connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
  169. if (!connect.ep_cb.rx_refill_thresh)
  170. connect.ep_cb.rx_refill_thresh++;
  171. /* connect to control service */
  172. connect.svc_id = WMI_CONTROL_SVC;
  173. if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
  174. return -EIO;
  175. connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
  176. /*
  177. * Limit the HTC message size on the send path, although e can
  178. * receive A-MSDU frames of 4K, we will only send ethernet-sized
  179. * (802.3) frames on the send path.
  180. */
  181. connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
  182. /*
  183. * To reduce the amount of committed memory for larger A_MSDU
  184. * frames, use the recv-alloc threshold mechanism for larger
  185. * packets.
  186. */
  187. connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
  188. connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
  189. /*
  190. * For the remaining data services set the connection flag to
  191. * reduce dribbling, if configured to do so.
  192. */
  193. connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
  194. connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
  195. connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
  196. connect.svc_id = WMI_DATA_BE_SVC;
  197. if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
  198. return -EIO;
  199. /* connect to back-ground map this to WMI LOW_PRI */
  200. connect.svc_id = WMI_DATA_BK_SVC;
  201. if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
  202. return -EIO;
  203. /* connect to Video service, map this to to HI PRI */
  204. connect.svc_id = WMI_DATA_VI_SVC;
  205. if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
  206. return -EIO;
  207. /*
  208. * Connect to VO service, this is currently not mapped to a WMI
  209. * priority stream due to historical reasons. WMI originally
  210. * defined 3 priorities over 3 mailboxes We can change this when
  211. * WMI is reworked so that priorities are not dependent on
  212. * mailboxes.
  213. */
  214. connect.svc_id = WMI_DATA_VO_SVC;
  215. if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
  216. return -EIO;
  217. return 0;
  218. }
  219. static void ath6kl_init_control_info(struct ath6kl *ar)
  220. {
  221. u8 ctr;
  222. clear_bit(WMI_ENABLED, &ar->flag);
  223. ath6kl_init_profile_info(ar);
  224. ar->def_txkey_index = 0;
  225. memset(ar->wep_key_list, 0, sizeof(ar->wep_key_list));
  226. ar->ch_hint = 0;
  227. ar->listen_intvl_t = A_DEFAULT_LISTEN_INTERVAL;
  228. ar->listen_intvl_b = 0;
  229. ar->tx_pwr = 0;
  230. clear_bit(SKIP_SCAN, &ar->flag);
  231. set_bit(WMM_ENABLED, &ar->flag);
  232. ar->intra_bss = 1;
  233. memset(&ar->sc_params, 0, sizeof(ar->sc_params));
  234. ar->sc_params.short_scan_ratio = WMI_SHORTSCANRATIO_DEFAULT;
  235. ar->sc_params.scan_ctrl_flags = DEFAULT_SCAN_CTRL_FLAGS;
  236. ar->lrssi_roam_threshold = DEF_LRSSI_ROAM_THRESHOLD;
  237. memset((u8 *)ar->sta_list, 0,
  238. AP_MAX_NUM_STA * sizeof(struct ath6kl_sta));
  239. spin_lock_init(&ar->mcastpsq_lock);
  240. /* Init the PS queues */
  241. for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
  242. spin_lock_init(&ar->sta_list[ctr].psq_lock);
  243. skb_queue_head_init(&ar->sta_list[ctr].psq);
  244. }
  245. skb_queue_head_init(&ar->mcastpsq);
  246. memcpy(ar->ap_country_code, DEF_AP_COUNTRY_CODE, 3);
  247. }
  248. /*
  249. * Set HTC/Mbox operational parameters, this can only be called when the
  250. * target is in the BMI phase.
  251. */
  252. static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
  253. u8 htc_ctrl_buf)
  254. {
  255. int status;
  256. u32 blk_size;
  257. blk_size = ar->mbox_info.block_size;
  258. if (htc_ctrl_buf)
  259. blk_size |= ((u32)htc_ctrl_buf) << 16;
  260. /* set the host interest area for the block size */
  261. status = ath6kl_bmi_write(ar,
  262. ath6kl_get_hi_item_addr(ar,
  263. HI_ITEM(hi_mbox_io_block_sz)),
  264. (u8 *)&blk_size,
  265. 4);
  266. if (status) {
  267. ath6kl_err("bmi_write_memory for IO block size failed\n");
  268. goto out;
  269. }
  270. ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
  271. blk_size,
  272. ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
  273. if (mbox_isr_yield_val) {
  274. /* set the host interest area for the mbox ISR yield limit */
  275. status = ath6kl_bmi_write(ar,
  276. ath6kl_get_hi_item_addr(ar,
  277. HI_ITEM(hi_mbox_isr_yield_limit)),
  278. (u8 *)&mbox_isr_yield_val,
  279. 4);
  280. if (status) {
  281. ath6kl_err("bmi_write_memory for yield limit failed\n");
  282. goto out;
  283. }
  284. }
  285. out:
  286. return status;
  287. }
  288. #define REG_DUMP_COUNT_AR6003 60
  289. #define REGISTER_DUMP_LEN_MAX 60
  290. static void ath6kl_dump_target_assert_info(struct ath6kl *ar)
  291. {
  292. u32 address;
  293. u32 regdump_loc = 0;
  294. int status;
  295. u32 regdump_val[REGISTER_DUMP_LEN_MAX];
  296. u32 i;
  297. if (ar->target_type != TARGET_TYPE_AR6003)
  298. return;
  299. /* the reg dump pointer is copied to the host interest area */
  300. address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_failure_state));
  301. address = TARG_VTOP(ar->target_type, address);
  302. /* read RAM location through diagnostic window */
  303. status = ath6kl_diag_read32(ar, address, &regdump_loc);
  304. if (status || !regdump_loc) {
  305. ath6kl_err("failed to get ptr to register dump area\n");
  306. return;
  307. }
  308. ath6kl_dbg(ATH6KL_DBG_TRC, "location of register dump data: 0x%X\n",
  309. regdump_loc);
  310. regdump_loc = TARG_VTOP(ar->target_type, regdump_loc);
  311. /* fetch register dump data */
  312. status = ath6kl_diag_read(ar, regdump_loc, (u8 *)&regdump_val[0],
  313. REG_DUMP_COUNT_AR6003 * (sizeof(u32)));
  314. if (status) {
  315. ath6kl_err("failed to get register dump\n");
  316. return;
  317. }
  318. ath6kl_dbg(ATH6KL_DBG_TRC, "Register Dump:\n");
  319. for (i = 0; i < REG_DUMP_COUNT_AR6003; i++)
  320. ath6kl_dbg(ATH6KL_DBG_TRC, " %d : 0x%8.8X\n",
  321. i, regdump_val[i]);
  322. }
  323. void ath6kl_target_failure(struct ath6kl *ar)
  324. {
  325. ath6kl_err("target asserted\n");
  326. /* try dumping target assertion information (if any) */
  327. ath6kl_dump_target_assert_info(ar);
  328. }
  329. static int ath6kl_target_config_wlan_params(struct ath6kl *ar)
  330. {
  331. int status = 0;
  332. int ret;
  333. /*
  334. * Configure the device for rx dot11 header rules. "0,0" are the
  335. * default values. Required if checksum offload is needed. Set
  336. * RxMetaVersion to 2.
  337. */
  338. if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi,
  339. ar->rx_meta_ver, 0, 0)) {
  340. ath6kl_err("unable to set the rx frame format\n");
  341. status = -EIO;
  342. }
  343. if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
  344. if ((ath6kl_wmi_pmparams_cmd(ar->wmi, 0, 1, 0, 0, 1,
  345. IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
  346. ath6kl_err("unable to set power save fail event policy\n");
  347. status = -EIO;
  348. }
  349. if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
  350. if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, 0,
  351. WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
  352. ath6kl_err("unable to set barker preamble policy\n");
  353. status = -EIO;
  354. }
  355. if (ath6kl_wmi_set_keepalive_cmd(ar->wmi,
  356. WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
  357. ath6kl_err("unable to set keep alive interval\n");
  358. status = -EIO;
  359. }
  360. if (ath6kl_wmi_disctimeout_cmd(ar->wmi,
  361. WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
  362. ath6kl_err("unable to set disconnect timeout\n");
  363. status = -EIO;
  364. }
  365. if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
  366. if (ath6kl_wmi_set_wmm_txop(ar->wmi, WMI_TXOP_DISABLED)) {
  367. ath6kl_err("unable to set txop bursting\n");
  368. status = -EIO;
  369. }
  370. if (ar->p2p) {
  371. ret = ath6kl_wmi_info_req_cmd(ar->wmi,
  372. P2P_FLAG_CAPABILITIES_REQ |
  373. P2P_FLAG_MACADDR_REQ |
  374. P2P_FLAG_HMODEL_REQ);
  375. if (ret) {
  376. ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
  377. "capabilities (%d) - assuming P2P not "
  378. "supported\n", ret);
  379. ar->p2p = 0;
  380. }
  381. }
  382. if (ar->p2p) {
  383. /* Enable Probe Request reporting for P2P */
  384. ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, true);
  385. if (ret) {
  386. ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
  387. "Request reporting (%d)\n", ret);
  388. }
  389. }
  390. return status;
  391. }
  392. int ath6kl_configure_target(struct ath6kl *ar)
  393. {
  394. u32 param, ram_reserved_size;
  395. u8 fw_iftype;
  396. fw_iftype = ath6kl_get_fw_iftype(ar);
  397. if (fw_iftype == 0xff)
  398. return -EINVAL;
  399. /* Tell target which HTC version it is used*/
  400. param = HTC_PROTOCOL_VERSION;
  401. if (ath6kl_bmi_write(ar,
  402. ath6kl_get_hi_item_addr(ar,
  403. HI_ITEM(hi_app_host_interest)),
  404. (u8 *)&param, 4) != 0) {
  405. ath6kl_err("bmi_write_memory for htc version failed\n");
  406. return -EIO;
  407. }
  408. /* set the firmware mode to STA/IBSS/AP */
  409. param = 0;
  410. if (ath6kl_bmi_read(ar,
  411. ath6kl_get_hi_item_addr(ar,
  412. HI_ITEM(hi_option_flag)),
  413. (u8 *)&param, 4) != 0) {
  414. ath6kl_err("bmi_read_memory for setting fwmode failed\n");
  415. return -EIO;
  416. }
  417. param |= (1 << HI_OPTION_NUM_DEV_SHIFT);
  418. param |= (fw_iftype << HI_OPTION_FW_MODE_SHIFT);
  419. if (ar->p2p && fw_iftype == HI_OPTION_FW_MODE_BSS_STA) {
  420. param |= HI_OPTION_FW_SUBMODE_P2PDEV <<
  421. HI_OPTION_FW_SUBMODE_SHIFT;
  422. }
  423. param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
  424. param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
  425. if (ath6kl_bmi_write(ar,
  426. ath6kl_get_hi_item_addr(ar,
  427. HI_ITEM(hi_option_flag)),
  428. (u8 *)&param,
  429. 4) != 0) {
  430. ath6kl_err("bmi_write_memory for setting fwmode failed\n");
  431. return -EIO;
  432. }
  433. ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
  434. /*
  435. * Hardcode the address use for the extended board data
  436. * Ideally this should be pre-allocate by the OS at boot time
  437. * But since it is a new feature and board data is loaded
  438. * at init time, we have to workaround this from host.
  439. * It is difficult to patch the firmware boot code,
  440. * but possible in theory.
  441. */
  442. param = ar->hw.board_ext_data_addr;
  443. ram_reserved_size = ar->hw.reserved_ram_size;
  444. if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
  445. HI_ITEM(hi_board_ext_data)),
  446. (u8 *)&param, 4) != 0) {
  447. ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
  448. return -EIO;
  449. }
  450. if (ath6kl_bmi_write(ar, ath6kl_get_hi_item_addr(ar,
  451. HI_ITEM(hi_end_ram_reserve_sz)),
  452. (u8 *)&ram_reserved_size, 4) != 0) {
  453. ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
  454. return -EIO;
  455. }
  456. /* set the block size for the target */
  457. if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
  458. /* use default number of control buffers */
  459. return -EIO;
  460. return 0;
  461. }
  462. struct ath6kl *ath6kl_core_alloc(struct device *sdev)
  463. {
  464. struct net_device *dev;
  465. struct ath6kl *ar;
  466. struct wireless_dev *wdev;
  467. wdev = ath6kl_cfg80211_init(sdev);
  468. if (!wdev) {
  469. ath6kl_err("ath6kl_cfg80211_init failed\n");
  470. return NULL;
  471. }
  472. ar = wdev_priv(wdev);
  473. ar->dev = sdev;
  474. ar->wdev = wdev;
  475. wdev->iftype = NL80211_IFTYPE_STATION;
  476. if (ath6kl_debug_init(ar)) {
  477. ath6kl_err("Failed to initialize debugfs\n");
  478. ath6kl_cfg80211_deinit(ar);
  479. return NULL;
  480. }
  481. dev = alloc_netdev(0, "wlan%d", ether_setup);
  482. if (!dev) {
  483. ath6kl_err("no memory for network device instance\n");
  484. ath6kl_cfg80211_deinit(ar);
  485. return NULL;
  486. }
  487. dev->ieee80211_ptr = wdev;
  488. SET_NETDEV_DEV(dev, wiphy_dev(wdev->wiphy));
  489. wdev->netdev = dev;
  490. ar->sme_state = SME_DISCONNECTED;
  491. ar->auto_auth_stage = AUTH_IDLE;
  492. init_netdev(dev);
  493. ar->net_dev = dev;
  494. set_bit(WLAN_ENABLED, &ar->flag);
  495. ar->wlan_pwr_state = WLAN_POWER_STATE_ON;
  496. spin_lock_init(&ar->lock);
  497. ath6kl_init_control_info(ar);
  498. init_waitqueue_head(&ar->event_wq);
  499. sema_init(&ar->sem, 1);
  500. clear_bit(DESTROY_IN_PROGRESS, &ar->flag);
  501. INIT_LIST_HEAD(&ar->amsdu_rx_buffer_queue);
  502. setup_timer(&ar->disconnect_timer, disconnect_timer_handler,
  503. (unsigned long) dev);
  504. return ar;
  505. }
  506. int ath6kl_unavail_ev(struct ath6kl *ar)
  507. {
  508. ath6kl_destroy(ar->net_dev, 1);
  509. return 0;
  510. }
  511. /* firmware upload */
  512. static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
  513. u8 **fw, size_t *fw_len)
  514. {
  515. const struct firmware *fw_entry;
  516. int ret;
  517. ret = request_firmware(&fw_entry, filename, ar->dev);
  518. if (ret)
  519. return ret;
  520. *fw_len = fw_entry->size;
  521. *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
  522. if (*fw == NULL)
  523. ret = -ENOMEM;
  524. release_firmware(fw_entry);
  525. return ret;
  526. }
  527. #ifdef CONFIG_OF
  528. static const char *get_target_ver_dir(const struct ath6kl *ar)
  529. {
  530. switch (ar->version.target_ver) {
  531. case AR6003_REV1_VERSION:
  532. return "ath6k/AR6003/hw1.0";
  533. case AR6003_REV2_VERSION:
  534. return "ath6k/AR6003/hw2.0";
  535. case AR6003_REV3_VERSION:
  536. return "ath6k/AR6003/hw2.1.1";
  537. }
  538. ath6kl_warn("%s: unsupported target version 0x%x.\n", __func__,
  539. ar->version.target_ver);
  540. return NULL;
  541. }
  542. /*
  543. * Check the device tree for a board-id and use it to construct
  544. * the pathname to the firmware file. Used (for now) to find a
  545. * fallback to the "bdata.bin" file--typically a symlink to the
  546. * appropriate board-specific file.
  547. */
  548. static bool check_device_tree(struct ath6kl *ar)
  549. {
  550. static const char *board_id_prop = "atheros,board-id";
  551. struct device_node *node;
  552. char board_filename[64];
  553. const char *board_id;
  554. int ret;
  555. for_each_compatible_node(node, NULL, "atheros,ath6kl") {
  556. board_id = of_get_property(node, board_id_prop, NULL);
  557. if (board_id == NULL) {
  558. ath6kl_warn("No \"%s\" property on %s node.\n",
  559. board_id_prop, node->name);
  560. continue;
  561. }
  562. snprintf(board_filename, sizeof(board_filename),
  563. "%s/bdata.%s.bin", get_target_ver_dir(ar), board_id);
  564. ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
  565. &ar->fw_board_len);
  566. if (ret) {
  567. ath6kl_err("Failed to get DT board file %s: %d\n",
  568. board_filename, ret);
  569. continue;
  570. }
  571. return true;
  572. }
  573. return false;
  574. }
  575. #else
  576. static bool check_device_tree(struct ath6kl *ar)
  577. {
  578. return false;
  579. }
  580. #endif /* CONFIG_OF */
  581. static int ath6kl_fetch_board_file(struct ath6kl *ar)
  582. {
  583. const char *filename;
  584. int ret;
  585. if (ar->fw_board != NULL)
  586. return 0;
  587. switch (ar->version.target_ver) {
  588. case AR6003_REV2_VERSION:
  589. filename = AR6003_REV2_BOARD_DATA_FILE;
  590. break;
  591. case AR6004_REV1_VERSION:
  592. filename = AR6004_REV1_BOARD_DATA_FILE;
  593. break;
  594. default:
  595. filename = AR6003_REV3_BOARD_DATA_FILE;
  596. break;
  597. }
  598. ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
  599. &ar->fw_board_len);
  600. if (ret == 0) {
  601. /* managed to get proper board file */
  602. return 0;
  603. }
  604. if (check_device_tree(ar)) {
  605. /* got board file from device tree */
  606. return 0;
  607. }
  608. /* there was no proper board file, try to use default instead */
  609. ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
  610. filename, ret);
  611. switch (ar->version.target_ver) {
  612. case AR6003_REV2_VERSION:
  613. filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
  614. break;
  615. case AR6004_REV1_VERSION:
  616. filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
  617. break;
  618. default:
  619. filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
  620. break;
  621. }
  622. ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
  623. &ar->fw_board_len);
  624. if (ret) {
  625. ath6kl_err("Failed to get default board file %s: %d\n",
  626. filename, ret);
  627. return ret;
  628. }
  629. ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
  630. ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
  631. return 0;
  632. }
  633. static int ath6kl_fetch_otp_file(struct ath6kl *ar)
  634. {
  635. const char *filename;
  636. int ret;
  637. if (ar->fw_otp != NULL)
  638. return 0;
  639. switch (ar->version.target_ver) {
  640. case AR6003_REV2_VERSION:
  641. filename = AR6003_REV2_OTP_FILE;
  642. break;
  643. case AR6004_REV1_VERSION:
  644. ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
  645. return 0;
  646. break;
  647. default:
  648. filename = AR6003_REV3_OTP_FILE;
  649. break;
  650. }
  651. ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
  652. &ar->fw_otp_len);
  653. if (ret) {
  654. ath6kl_err("Failed to get OTP file %s: %d\n",
  655. filename, ret);
  656. return ret;
  657. }
  658. return 0;
  659. }
  660. static int ath6kl_fetch_fw_file(struct ath6kl *ar)
  661. {
  662. const char *filename;
  663. int ret;
  664. if (ar->fw != NULL)
  665. return 0;
  666. if (testmode) {
  667. switch (ar->version.target_ver) {
  668. case AR6003_REV2_VERSION:
  669. filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
  670. break;
  671. case AR6003_REV3_VERSION:
  672. filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
  673. break;
  674. case AR6004_REV1_VERSION:
  675. ath6kl_warn("testmode not supported with ar6004\n");
  676. return -EOPNOTSUPP;
  677. default:
  678. ath6kl_warn("unknown target version: 0x%x\n",
  679. ar->version.target_ver);
  680. return -EINVAL;
  681. }
  682. set_bit(TESTMODE, &ar->flag);
  683. goto get_fw;
  684. }
  685. switch (ar->version.target_ver) {
  686. case AR6003_REV2_VERSION:
  687. filename = AR6003_REV2_FIRMWARE_FILE;
  688. break;
  689. case AR6004_REV1_VERSION:
  690. filename = AR6004_REV1_FIRMWARE_FILE;
  691. break;
  692. default:
  693. filename = AR6003_REV3_FIRMWARE_FILE;
  694. break;
  695. }
  696. get_fw:
  697. ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
  698. if (ret) {
  699. ath6kl_err("Failed to get firmware file %s: %d\n",
  700. filename, ret);
  701. return ret;
  702. }
  703. return 0;
  704. }
  705. static int ath6kl_fetch_patch_file(struct ath6kl *ar)
  706. {
  707. const char *filename;
  708. int ret;
  709. switch (ar->version.target_ver) {
  710. case AR6003_REV2_VERSION:
  711. filename = AR6003_REV2_PATCH_FILE;
  712. break;
  713. case AR6004_REV1_VERSION:
  714. /* FIXME: implement for AR6004 */
  715. return 0;
  716. break;
  717. default:
  718. filename = AR6003_REV3_PATCH_FILE;
  719. break;
  720. }
  721. if (ar->fw_patch == NULL) {
  722. ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
  723. &ar->fw_patch_len);
  724. if (ret) {
  725. ath6kl_err("Failed to get patch file %s: %d\n",
  726. filename, ret);
  727. return ret;
  728. }
  729. }
  730. return 0;
  731. }
  732. static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
  733. {
  734. int ret;
  735. ret = ath6kl_fetch_otp_file(ar);
  736. if (ret)
  737. return ret;
  738. ret = ath6kl_fetch_fw_file(ar);
  739. if (ret)
  740. return ret;
  741. ret = ath6kl_fetch_patch_file(ar);
  742. if (ret)
  743. return ret;
  744. return 0;
  745. }
  746. static int ath6kl_fetch_fw_api2(struct ath6kl *ar)
  747. {
  748. size_t magic_len, len, ie_len;
  749. const struct firmware *fw;
  750. struct ath6kl_fw_ie *hdr;
  751. const char *filename;
  752. const u8 *data;
  753. int ret, ie_id, i, index, bit;
  754. __le32 *val;
  755. switch (ar->version.target_ver) {
  756. case AR6003_REV2_VERSION:
  757. filename = AR6003_REV2_FIRMWARE_2_FILE;
  758. break;
  759. case AR6003_REV3_VERSION:
  760. filename = AR6003_REV3_FIRMWARE_2_FILE;
  761. break;
  762. case AR6004_REV1_VERSION:
  763. filename = AR6004_REV1_FIRMWARE_2_FILE;
  764. break;
  765. default:
  766. return -EOPNOTSUPP;
  767. }
  768. ret = request_firmware(&fw, filename, ar->dev);
  769. if (ret)
  770. return ret;
  771. data = fw->data;
  772. len = fw->size;
  773. /* magic also includes the null byte, check that as well */
  774. magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
  775. if (len < magic_len) {
  776. ret = -EINVAL;
  777. goto out;
  778. }
  779. if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
  780. ret = -EINVAL;
  781. goto out;
  782. }
  783. len -= magic_len;
  784. data += magic_len;
  785. /* loop elements */
  786. while (len > sizeof(struct ath6kl_fw_ie)) {
  787. /* hdr is unaligned! */
  788. hdr = (struct ath6kl_fw_ie *) data;
  789. ie_id = le32_to_cpup(&hdr->id);
  790. ie_len = le32_to_cpup(&hdr->len);
  791. len -= sizeof(*hdr);
  792. data += sizeof(*hdr);
  793. if (len < ie_len) {
  794. ret = -EINVAL;
  795. goto out;
  796. }
  797. switch (ie_id) {
  798. case ATH6KL_FW_IE_OTP_IMAGE:
  799. ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
  800. if (ar->fw_otp == NULL) {
  801. ret = -ENOMEM;
  802. goto out;
  803. }
  804. ar->fw_otp_len = ie_len;
  805. break;
  806. case ATH6KL_FW_IE_FW_IMAGE:
  807. ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
  808. if (ar->fw == NULL) {
  809. ret = -ENOMEM;
  810. goto out;
  811. }
  812. ar->fw_len = ie_len;
  813. break;
  814. case ATH6KL_FW_IE_PATCH_IMAGE:
  815. ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
  816. if (ar->fw_patch == NULL) {
  817. ret = -ENOMEM;
  818. goto out;
  819. }
  820. ar->fw_patch_len = ie_len;
  821. break;
  822. case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
  823. val = (__le32 *) data;
  824. ar->hw.reserved_ram_size = le32_to_cpup(val);
  825. break;
  826. case ATH6KL_FW_IE_CAPABILITIES:
  827. for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
  828. index = ALIGN(i, 8) / 8;
  829. bit = i % 8;
  830. if (data[index] & (1 << bit))
  831. __set_bit(i, ar->fw_capabilities);
  832. }
  833. break;
  834. default:
  835. ath6kl_dbg(ATH6KL_DBG_TRC, "Unknown fw ie: %u\n",
  836. le32_to_cpup(&hdr->id));
  837. break;
  838. }
  839. len -= ie_len;
  840. data += ie_len;
  841. };
  842. ret = 0;
  843. out:
  844. release_firmware(fw);
  845. return ret;
  846. }
  847. static int ath6kl_fetch_firmwares(struct ath6kl *ar)
  848. {
  849. int ret;
  850. ret = ath6kl_fetch_board_file(ar);
  851. if (ret)
  852. return ret;
  853. ret = ath6kl_fetch_fw_api2(ar);
  854. if (ret == 0)
  855. /* fw api 2 found, use it */
  856. return 0;
  857. ret = ath6kl_fetch_fw_api1(ar);
  858. if (ret)
  859. return ret;
  860. return 0;
  861. }
  862. static int ath6kl_upload_board_file(struct ath6kl *ar)
  863. {
  864. u32 board_address, board_ext_address, param;
  865. u32 board_data_size, board_ext_data_size;
  866. int ret;
  867. if (WARN_ON(ar->fw_board == NULL))
  868. return -ENOENT;
  869. /*
  870. * Determine where in Target RAM to write Board Data.
  871. * For AR6004, host determine Target RAM address for
  872. * writing board data.
  873. */
  874. if (ar->target_type == TARGET_TYPE_AR6004) {
  875. board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
  876. ath6kl_bmi_write(ar,
  877. ath6kl_get_hi_item_addr(ar,
  878. HI_ITEM(hi_board_data)),
  879. (u8 *) &board_address, 4);
  880. } else {
  881. ath6kl_bmi_read(ar,
  882. ath6kl_get_hi_item_addr(ar,
  883. HI_ITEM(hi_board_data)),
  884. (u8 *) &board_address, 4);
  885. }
  886. ath6kl_dbg(ATH6KL_DBG_TRC, "board data download addr: 0x%x\n",
  887. board_address);
  888. /* determine where in target ram to write extended board data */
  889. ath6kl_bmi_read(ar,
  890. ath6kl_get_hi_item_addr(ar,
  891. HI_ITEM(hi_board_ext_data)),
  892. (u8 *) &board_ext_address, 4);
  893. ath6kl_dbg(ATH6KL_DBG_TRC, "board file download addr: 0x%x\n",
  894. board_ext_address);
  895. if (board_ext_address == 0) {
  896. ath6kl_err("Failed to get board file target address.\n");
  897. return -EINVAL;
  898. }
  899. switch (ar->target_type) {
  900. case TARGET_TYPE_AR6003:
  901. board_data_size = AR6003_BOARD_DATA_SZ;
  902. board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
  903. break;
  904. case TARGET_TYPE_AR6004:
  905. board_data_size = AR6004_BOARD_DATA_SZ;
  906. board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
  907. break;
  908. default:
  909. WARN_ON(1);
  910. return -EINVAL;
  911. break;
  912. }
  913. if (ar->fw_board_len == (board_data_size +
  914. board_ext_data_size)) {
  915. /* write extended board data */
  916. ret = ath6kl_bmi_write(ar, board_ext_address,
  917. ar->fw_board + board_data_size,
  918. board_ext_data_size);
  919. if (ret) {
  920. ath6kl_err("Failed to write extended board data: %d\n",
  921. ret);
  922. return ret;
  923. }
  924. /* record that extended board data is initialized */
  925. param = (board_ext_data_size << 16) | 1;
  926. ath6kl_bmi_write(ar,
  927. ath6kl_get_hi_item_addr(ar,
  928. HI_ITEM(hi_board_ext_data_config)),
  929. (unsigned char *) &param, 4);
  930. }
  931. if (ar->fw_board_len < board_data_size) {
  932. ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
  933. ret = -EINVAL;
  934. return ret;
  935. }
  936. ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
  937. board_data_size);
  938. if (ret) {
  939. ath6kl_err("Board file bmi write failed: %d\n", ret);
  940. return ret;
  941. }
  942. /* record the fact that Board Data IS initialized */
  943. param = 1;
  944. ath6kl_bmi_write(ar,
  945. ath6kl_get_hi_item_addr(ar,
  946. HI_ITEM(hi_board_data_initialized)),
  947. (u8 *)&param, 4);
  948. return ret;
  949. }
  950. static int ath6kl_upload_otp(struct ath6kl *ar)
  951. {
  952. u32 address, param;
  953. int ret;
  954. if (WARN_ON(ar->fw_otp == NULL))
  955. return -ENOENT;
  956. address = ar->hw.app_load_addr;
  957. ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
  958. ar->fw_otp_len);
  959. if (ret) {
  960. ath6kl_err("Failed to upload OTP file: %d\n", ret);
  961. return ret;
  962. }
  963. /* read firmware start address */
  964. ret = ath6kl_bmi_read(ar,
  965. ath6kl_get_hi_item_addr(ar,
  966. HI_ITEM(hi_app_start)),
  967. (u8 *) &address, sizeof(address));
  968. if (ret) {
  969. ath6kl_err("Failed to read hi_app_start: %d\n", ret);
  970. return ret;
  971. }
  972. ar->hw.app_start_override_addr = address;
  973. /* execute the OTP code */
  974. param = 0;
  975. ath6kl_bmi_execute(ar, address, &param);
  976. return ret;
  977. }
  978. static int ath6kl_upload_firmware(struct ath6kl *ar)
  979. {
  980. u32 address;
  981. int ret;
  982. if (WARN_ON(ar->fw == NULL))
  983. return -ENOENT;
  984. address = ar->hw.app_load_addr;
  985. ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
  986. if (ret) {
  987. ath6kl_err("Failed to write firmware: %d\n", ret);
  988. return ret;
  989. }
  990. /*
  991. * Set starting address for firmware
  992. * Don't need to setup app_start override addr on AR6004
  993. */
  994. if (ar->target_type != TARGET_TYPE_AR6004) {
  995. address = ar->hw.app_start_override_addr;
  996. ath6kl_bmi_set_app_start(ar, address);
  997. }
  998. return ret;
  999. }
  1000. static int ath6kl_upload_patch(struct ath6kl *ar)
  1001. {
  1002. u32 address, param;
  1003. int ret;
  1004. if (WARN_ON(ar->fw_patch == NULL))
  1005. return -ENOENT;
  1006. address = ar->hw.dataset_patch_addr;
  1007. ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
  1008. if (ret) {
  1009. ath6kl_err("Failed to write patch file: %d\n", ret);
  1010. return ret;
  1011. }
  1012. param = address;
  1013. ath6kl_bmi_write(ar,
  1014. ath6kl_get_hi_item_addr(ar,
  1015. HI_ITEM(hi_dset_list_head)),
  1016. (unsigned char *) &param, 4);
  1017. return 0;
  1018. }
  1019. static int ath6kl_init_upload(struct ath6kl *ar)
  1020. {
  1021. u32 param, options, sleep, address;
  1022. int status = 0;
  1023. if (ar->target_type != TARGET_TYPE_AR6003 &&
  1024. ar->target_type != TARGET_TYPE_AR6004)
  1025. return -EINVAL;
  1026. /* temporarily disable system sleep */
  1027. address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
  1028. status = ath6kl_bmi_reg_read(ar, address, &param);
  1029. if (status)
  1030. return status;
  1031. options = param;
  1032. param |= ATH6KL_OPTION_SLEEP_DISABLE;
  1033. status = ath6kl_bmi_reg_write(ar, address, param);
  1034. if (status)
  1035. return status;
  1036. address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
  1037. status = ath6kl_bmi_reg_read(ar, address, &param);
  1038. if (status)
  1039. return status;
  1040. sleep = param;
  1041. param |= SM(SYSTEM_SLEEP_DISABLE, 1);
  1042. status = ath6kl_bmi_reg_write(ar, address, param);
  1043. if (status)
  1044. return status;
  1045. ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
  1046. options, sleep);
  1047. /* program analog PLL register */
  1048. /* no need to control 40/44MHz clock on AR6004 */
  1049. if (ar->target_type != TARGET_TYPE_AR6004) {
  1050. status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
  1051. 0xF9104001);
  1052. if (status)
  1053. return status;
  1054. /* Run at 80/88MHz by default */
  1055. param = SM(CPU_CLOCK_STANDARD, 1);
  1056. address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
  1057. status = ath6kl_bmi_reg_write(ar, address, param);
  1058. if (status)
  1059. return status;
  1060. }
  1061. param = 0;
  1062. address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
  1063. param = SM(LPO_CAL_ENABLE, 1);
  1064. status = ath6kl_bmi_reg_write(ar, address, param);
  1065. if (status)
  1066. return status;
  1067. /* WAR to avoid SDIO CRC err */
  1068. if (ar->version.target_ver == AR6003_REV2_VERSION) {
  1069. ath6kl_err("temporary war to avoid sdio crc error\n");
  1070. param = 0x20;
  1071. address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
  1072. status = ath6kl_bmi_reg_write(ar, address, param);
  1073. if (status)
  1074. return status;
  1075. address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
  1076. status = ath6kl_bmi_reg_write(ar, address, param);
  1077. if (status)
  1078. return status;
  1079. address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
  1080. status = ath6kl_bmi_reg_write(ar, address, param);
  1081. if (status)
  1082. return status;
  1083. address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
  1084. status = ath6kl_bmi_reg_write(ar, address, param);
  1085. if (status)
  1086. return status;
  1087. }
  1088. /* write EEPROM data to Target RAM */
  1089. status = ath6kl_upload_board_file(ar);
  1090. if (status)
  1091. return status;
  1092. /* transfer One time Programmable data */
  1093. status = ath6kl_upload_otp(ar);
  1094. if (status)
  1095. return status;
  1096. /* Download Target firmware */
  1097. status = ath6kl_upload_firmware(ar);
  1098. if (status)
  1099. return status;
  1100. status = ath6kl_upload_patch(ar);
  1101. if (status)
  1102. return status;
  1103. /* Restore system sleep */
  1104. address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
  1105. status = ath6kl_bmi_reg_write(ar, address, sleep);
  1106. if (status)
  1107. return status;
  1108. address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
  1109. param = options | 0x20;
  1110. status = ath6kl_bmi_reg_write(ar, address, param);
  1111. if (status)
  1112. return status;
  1113. /* Configure GPIO AR6003 UART */
  1114. param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
  1115. status = ath6kl_bmi_write(ar,
  1116. ath6kl_get_hi_item_addr(ar,
  1117. HI_ITEM(hi_dbg_uart_txpin)),
  1118. (u8 *)&param, 4);
  1119. return status;
  1120. }
  1121. static int ath6kl_init_hw_params(struct ath6kl *ar)
  1122. {
  1123. switch (ar->version.target_ver) {
  1124. case AR6003_REV2_VERSION:
  1125. ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
  1126. ar->hw.app_load_addr = AR6003_REV2_APP_LOAD_ADDRESS;
  1127. ar->hw.board_ext_data_addr = AR6003_REV2_BOARD_EXT_DATA_ADDRESS;
  1128. ar->hw.reserved_ram_size = AR6003_REV2_RAM_RESERVE_SIZE;
  1129. break;
  1130. case AR6003_REV3_VERSION:
  1131. ar->hw.dataset_patch_addr = AR6003_REV3_DATASET_PATCH_ADDRESS;
  1132. ar->hw.app_load_addr = 0x1234;
  1133. ar->hw.board_ext_data_addr = AR6003_REV3_BOARD_EXT_DATA_ADDRESS;
  1134. ar->hw.reserved_ram_size = AR6003_REV3_RAM_RESERVE_SIZE;
  1135. break;
  1136. case AR6004_REV1_VERSION:
  1137. ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
  1138. ar->hw.app_load_addr = AR6003_REV3_APP_LOAD_ADDRESS;
  1139. ar->hw.board_ext_data_addr = AR6004_REV1_BOARD_EXT_DATA_ADDRESS;
  1140. ar->hw.reserved_ram_size = AR6004_REV1_RAM_RESERVE_SIZE;
  1141. break;
  1142. default:
  1143. ath6kl_err("Unsupported hardware version: 0x%x\n",
  1144. ar->version.target_ver);
  1145. return -EINVAL;
  1146. }
  1147. return 0;
  1148. }
  1149. static int ath6kl_init(struct net_device *dev)
  1150. {
  1151. struct ath6kl *ar = ath6kl_priv(dev);
  1152. int status = 0;
  1153. s32 timeleft;
  1154. if (!ar)
  1155. return -EIO;
  1156. /* Do we need to finish the BMI phase */
  1157. if (ath6kl_bmi_done(ar)) {
  1158. status = -EIO;
  1159. goto ath6kl_init_done;
  1160. }
  1161. /* Indicate that WMI is enabled (although not ready yet) */
  1162. set_bit(WMI_ENABLED, &ar->flag);
  1163. ar->wmi = ath6kl_wmi_init(ar);
  1164. if (!ar->wmi) {
  1165. ath6kl_err("failed to initialize wmi\n");
  1166. status = -EIO;
  1167. goto ath6kl_init_done;
  1168. }
  1169. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
  1170. wlan_node_table_init(&ar->scan_table);
  1171. /*
  1172. * The reason we have to wait for the target here is that the
  1173. * driver layer has to init BMI in order to set the host block
  1174. * size.
  1175. */
  1176. if (ath6kl_htc_wait_target(ar->htc_target)) {
  1177. status = -EIO;
  1178. goto err_node_cleanup;
  1179. }
  1180. if (ath6kl_init_service_ep(ar)) {
  1181. status = -EIO;
  1182. goto err_cleanup_scatter;
  1183. }
  1184. /* setup access class priority mappings */
  1185. ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
  1186. ar->ac_stream_pri_map[WMM_AC_BE] = 1;
  1187. ar->ac_stream_pri_map[WMM_AC_VI] = 2;
  1188. ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
  1189. /* give our connected endpoints some buffers */
  1190. ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
  1191. ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
  1192. /* allocate some buffers that handle larger AMSDU frames */
  1193. ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
  1194. /* setup credit distribution */
  1195. ath6k_setup_credit_dist(ar->htc_target, &ar->credit_state_info);
  1196. ath6kl_cookie_init(ar);
  1197. /* start HTC */
  1198. status = ath6kl_htc_start(ar->htc_target);
  1199. if (status) {
  1200. ath6kl_cookie_cleanup(ar);
  1201. goto err_rxbuf_cleanup;
  1202. }
  1203. /* Wait for Wmi event to be ready */
  1204. timeleft = wait_event_interruptible_timeout(ar->event_wq,
  1205. test_bit(WMI_READY,
  1206. &ar->flag),
  1207. WMI_TIMEOUT);
  1208. if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
  1209. ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
  1210. ATH6KL_ABI_VERSION, ar->version.abi_ver);
  1211. status = -EIO;
  1212. goto err_htc_stop;
  1213. }
  1214. if (!timeleft || signal_pending(current)) {
  1215. ath6kl_err("wmi is not ready or wait was interrupted\n");
  1216. status = -EIO;
  1217. goto err_htc_stop;
  1218. }
  1219. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
  1220. /* communicate the wmi protocol verision to the target */
  1221. if ((ath6kl_set_host_app_area(ar)) != 0)
  1222. ath6kl_err("unable to set the host app area\n");
  1223. ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
  1224. ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
  1225. status = ath6kl_target_config_wlan_params(ar);
  1226. if (!status)
  1227. goto ath6kl_init_done;
  1228. err_htc_stop:
  1229. ath6kl_htc_stop(ar->htc_target);
  1230. err_rxbuf_cleanup:
  1231. ath6kl_htc_flush_rx_buf(ar->htc_target);
  1232. ath6kl_cleanup_amsdu_rxbufs(ar);
  1233. err_cleanup_scatter:
  1234. ath6kl_hif_cleanup_scatter(ar);
  1235. err_node_cleanup:
  1236. wlan_node_table_cleanup(&ar->scan_table);
  1237. ath6kl_wmi_shutdown(ar->wmi);
  1238. clear_bit(WMI_ENABLED, &ar->flag);
  1239. ar->wmi = NULL;
  1240. ath6kl_init_done:
  1241. return status;
  1242. }
  1243. int ath6kl_core_init(struct ath6kl *ar)
  1244. {
  1245. int ret = 0;
  1246. struct ath6kl_bmi_target_info targ_info;
  1247. ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
  1248. if (!ar->ath6kl_wq)
  1249. return -ENOMEM;
  1250. ret = ath6kl_bmi_init(ar);
  1251. if (ret)
  1252. goto err_wq;
  1253. ret = ath6kl_bmi_get_target_info(ar, &targ_info);
  1254. if (ret)
  1255. goto err_bmi_cleanup;
  1256. ar->version.target_ver = le32_to_cpu(targ_info.version);
  1257. ar->target_type = le32_to_cpu(targ_info.type);
  1258. ar->wdev->wiphy->hw_version = le32_to_cpu(targ_info.version);
  1259. ret = ath6kl_init_hw_params(ar);
  1260. if (ret)
  1261. goto err_bmi_cleanup;
  1262. ret = ath6kl_configure_target(ar);
  1263. if (ret)
  1264. goto err_bmi_cleanup;
  1265. ar->htc_target = ath6kl_htc_create(ar);
  1266. if (!ar->htc_target) {
  1267. ret = -ENOMEM;
  1268. goto err_bmi_cleanup;
  1269. }
  1270. ar->aggr_cntxt = aggr_init(ar->net_dev);
  1271. if (!ar->aggr_cntxt) {
  1272. ath6kl_err("failed to initialize aggr\n");
  1273. ret = -ENOMEM;
  1274. goto err_htc_cleanup;
  1275. }
  1276. ret = ath6kl_fetch_firmwares(ar);
  1277. if (ret)
  1278. goto err_htc_cleanup;
  1279. ret = ath6kl_init_upload(ar);
  1280. if (ret)
  1281. goto err_htc_cleanup;
  1282. ret = ath6kl_init(ar->net_dev);
  1283. if (ret)
  1284. goto err_htc_cleanup;
  1285. /* This runs the init function if registered */
  1286. ret = register_netdev(ar->net_dev);
  1287. if (ret) {
  1288. ath6kl_err("register_netdev failed\n");
  1289. ath6kl_destroy(ar->net_dev, 0);
  1290. return ret;
  1291. }
  1292. set_bit(NETDEV_REGISTERED, &ar->flag);
  1293. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
  1294. __func__, ar->net_dev->name, ar->net_dev, ar);
  1295. return ret;
  1296. err_htc_cleanup:
  1297. ath6kl_htc_cleanup(ar->htc_target);
  1298. err_bmi_cleanup:
  1299. ath6kl_bmi_cleanup(ar);
  1300. err_wq:
  1301. destroy_workqueue(ar->ath6kl_wq);
  1302. return ret;
  1303. }
  1304. void ath6kl_stop_txrx(struct ath6kl *ar)
  1305. {
  1306. struct net_device *ndev = ar->net_dev;
  1307. if (!ndev)
  1308. return;
  1309. set_bit(DESTROY_IN_PROGRESS, &ar->flag);
  1310. if (down_interruptible(&ar->sem)) {
  1311. ath6kl_err("down_interruptible failed\n");
  1312. return;
  1313. }
  1314. if (ar->wlan_pwr_state != WLAN_POWER_STATE_CUT_PWR)
  1315. ath6kl_stop_endpoint(ndev, false, true);
  1316. clear_bit(WLAN_ENABLED, &ar->flag);
  1317. }
  1318. /*
  1319. * We need to differentiate between the surprise and planned removal of the
  1320. * device because of the following consideration:
  1321. *
  1322. * - In case of surprise removal, the hcd already frees up the pending
  1323. * for the device and hence there is no need to unregister the function
  1324. * driver inorder to get these requests. For planned removal, the function
  1325. * driver has to explicitly unregister itself to have the hcd return all the
  1326. * pending requests before the data structures for the devices are freed up.
  1327. * Note that as per the current implementation, the function driver will
  1328. * end up releasing all the devices since there is no API to selectively
  1329. * release a particular device.
  1330. *
  1331. * - Certain commands issued to the target can be skipped for surprise
  1332. * removal since they will anyway not go through.
  1333. */
  1334. void ath6kl_destroy(struct net_device *dev, unsigned int unregister)
  1335. {
  1336. struct ath6kl *ar;
  1337. if (!dev || !ath6kl_priv(dev)) {
  1338. ath6kl_err("failed to get device structure\n");
  1339. return;
  1340. }
  1341. ar = ath6kl_priv(dev);
  1342. destroy_workqueue(ar->ath6kl_wq);
  1343. if (ar->htc_target)
  1344. ath6kl_htc_cleanup(ar->htc_target);
  1345. aggr_module_destroy(ar->aggr_cntxt);
  1346. ath6kl_cookie_cleanup(ar);
  1347. ath6kl_cleanup_amsdu_rxbufs(ar);
  1348. ath6kl_bmi_cleanup(ar);
  1349. ath6kl_debug_cleanup(ar);
  1350. if (unregister && test_bit(NETDEV_REGISTERED, &ar->flag)) {
  1351. unregister_netdev(dev);
  1352. clear_bit(NETDEV_REGISTERED, &ar->flag);
  1353. }
  1354. free_netdev(dev);
  1355. wlan_node_table_cleanup(&ar->scan_table);
  1356. kfree(ar->fw_board);
  1357. kfree(ar->fw_otp);
  1358. kfree(ar->fw);
  1359. kfree(ar->fw_patch);
  1360. ath6kl_cfg80211_deinit(ar);
  1361. }