init.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  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. init_netdev(dev);
  492. ar->net_dev = dev;
  493. set_bit(WLAN_ENABLED, &ar->flag);
  494. ar->wlan_pwr_state = WLAN_POWER_STATE_ON;
  495. spin_lock_init(&ar->lock);
  496. ath6kl_init_control_info(ar);
  497. init_waitqueue_head(&ar->event_wq);
  498. sema_init(&ar->sem, 1);
  499. clear_bit(DESTROY_IN_PROGRESS, &ar->flag);
  500. INIT_LIST_HEAD(&ar->amsdu_rx_buffer_queue);
  501. setup_timer(&ar->disconnect_timer, disconnect_timer_handler,
  502. (unsigned long) dev);
  503. return ar;
  504. }
  505. int ath6kl_unavail_ev(struct ath6kl *ar)
  506. {
  507. ath6kl_destroy(ar->net_dev, 1);
  508. return 0;
  509. }
  510. /* firmware upload */
  511. static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
  512. u8 **fw, size_t *fw_len)
  513. {
  514. const struct firmware *fw_entry;
  515. int ret;
  516. ret = request_firmware(&fw_entry, filename, ar->dev);
  517. if (ret)
  518. return ret;
  519. *fw_len = fw_entry->size;
  520. *fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
  521. if (*fw == NULL)
  522. ret = -ENOMEM;
  523. release_firmware(fw_entry);
  524. return ret;
  525. }
  526. #ifdef CONFIG_OF
  527. static const char *get_target_ver_dir(const struct ath6kl *ar)
  528. {
  529. switch (ar->version.target_ver) {
  530. case AR6003_REV1_VERSION:
  531. return "ath6k/AR6003/hw1.0";
  532. case AR6003_REV2_VERSION:
  533. return "ath6k/AR6003/hw2.0";
  534. case AR6003_REV3_VERSION:
  535. return "ath6k/AR6003/hw2.1.1";
  536. }
  537. ath6kl_warn("%s: unsupported target version 0x%x.\n", __func__,
  538. ar->version.target_ver);
  539. return NULL;
  540. }
  541. /*
  542. * Check the device tree for a board-id and use it to construct
  543. * the pathname to the firmware file. Used (for now) to find a
  544. * fallback to the "bdata.bin" file--typically a symlink to the
  545. * appropriate board-specific file.
  546. */
  547. static bool check_device_tree(struct ath6kl *ar)
  548. {
  549. static const char *board_id_prop = "atheros,board-id";
  550. struct device_node *node;
  551. char board_filename[64];
  552. const char *board_id;
  553. int ret;
  554. for_each_compatible_node(node, NULL, "atheros,ath6kl") {
  555. board_id = of_get_property(node, board_id_prop, NULL);
  556. if (board_id == NULL) {
  557. ath6kl_warn("No \"%s\" property on %s node.\n",
  558. board_id_prop, node->name);
  559. continue;
  560. }
  561. snprintf(board_filename, sizeof(board_filename),
  562. "%s/bdata.%s.bin", get_target_ver_dir(ar), board_id);
  563. ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
  564. &ar->fw_board_len);
  565. if (ret) {
  566. ath6kl_err("Failed to get DT board file %s: %d\n",
  567. board_filename, ret);
  568. continue;
  569. }
  570. return true;
  571. }
  572. return false;
  573. }
  574. #else
  575. static bool check_device_tree(struct ath6kl *ar)
  576. {
  577. return false;
  578. }
  579. #endif /* CONFIG_OF */
  580. static int ath6kl_fetch_board_file(struct ath6kl *ar)
  581. {
  582. const char *filename;
  583. int ret;
  584. if (ar->fw_board != NULL)
  585. return 0;
  586. switch (ar->version.target_ver) {
  587. case AR6003_REV2_VERSION:
  588. filename = AR6003_REV2_BOARD_DATA_FILE;
  589. break;
  590. case AR6004_REV1_VERSION:
  591. filename = AR6004_REV1_BOARD_DATA_FILE;
  592. break;
  593. default:
  594. filename = AR6003_REV3_BOARD_DATA_FILE;
  595. break;
  596. }
  597. ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
  598. &ar->fw_board_len);
  599. if (ret == 0) {
  600. /* managed to get proper board file */
  601. return 0;
  602. }
  603. if (check_device_tree(ar)) {
  604. /* got board file from device tree */
  605. return 0;
  606. }
  607. /* there was no proper board file, try to use default instead */
  608. ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
  609. filename, ret);
  610. switch (ar->version.target_ver) {
  611. case AR6003_REV2_VERSION:
  612. filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
  613. break;
  614. case AR6004_REV1_VERSION:
  615. filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
  616. break;
  617. default:
  618. filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
  619. break;
  620. }
  621. ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
  622. &ar->fw_board_len);
  623. if (ret) {
  624. ath6kl_err("Failed to get default board file %s: %d\n",
  625. filename, ret);
  626. return ret;
  627. }
  628. ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
  629. ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
  630. return 0;
  631. }
  632. static int ath6kl_fetch_otp_file(struct ath6kl *ar)
  633. {
  634. const char *filename;
  635. int ret;
  636. if (ar->fw_otp != NULL)
  637. return 0;
  638. switch (ar->version.target_ver) {
  639. case AR6003_REV2_VERSION:
  640. filename = AR6003_REV2_OTP_FILE;
  641. break;
  642. case AR6004_REV1_VERSION:
  643. ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
  644. return 0;
  645. break;
  646. default:
  647. filename = AR6003_REV3_OTP_FILE;
  648. break;
  649. }
  650. ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
  651. &ar->fw_otp_len);
  652. if (ret) {
  653. ath6kl_err("Failed to get OTP file %s: %d\n",
  654. filename, ret);
  655. return ret;
  656. }
  657. return 0;
  658. }
  659. static int ath6kl_fetch_fw_file(struct ath6kl *ar)
  660. {
  661. const char *filename;
  662. int ret;
  663. if (ar->fw != NULL)
  664. return 0;
  665. if (testmode) {
  666. switch (ar->version.target_ver) {
  667. case AR6003_REV2_VERSION:
  668. filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
  669. break;
  670. case AR6003_REV3_VERSION:
  671. filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
  672. break;
  673. case AR6004_REV1_VERSION:
  674. ath6kl_warn("testmode not supported with ar6004\n");
  675. return -EOPNOTSUPP;
  676. default:
  677. ath6kl_warn("unknown target version: 0x%x\n",
  678. ar->version.target_ver);
  679. return -EINVAL;
  680. }
  681. set_bit(TESTMODE, &ar->flag);
  682. goto get_fw;
  683. }
  684. switch (ar->version.target_ver) {
  685. case AR6003_REV2_VERSION:
  686. filename = AR6003_REV2_FIRMWARE_FILE;
  687. break;
  688. case AR6004_REV1_VERSION:
  689. filename = AR6004_REV1_FIRMWARE_FILE;
  690. break;
  691. default:
  692. filename = AR6003_REV3_FIRMWARE_FILE;
  693. break;
  694. }
  695. get_fw:
  696. ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
  697. if (ret) {
  698. ath6kl_err("Failed to get firmware file %s: %d\n",
  699. filename, ret);
  700. return ret;
  701. }
  702. return 0;
  703. }
  704. static int ath6kl_fetch_patch_file(struct ath6kl *ar)
  705. {
  706. const char *filename;
  707. int ret;
  708. switch (ar->version.target_ver) {
  709. case AR6003_REV2_VERSION:
  710. filename = AR6003_REV2_PATCH_FILE;
  711. break;
  712. case AR6004_REV1_VERSION:
  713. /* FIXME: implement for AR6004 */
  714. return 0;
  715. break;
  716. default:
  717. filename = AR6003_REV3_PATCH_FILE;
  718. break;
  719. }
  720. if (ar->fw_patch == NULL) {
  721. ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
  722. &ar->fw_patch_len);
  723. if (ret) {
  724. ath6kl_err("Failed to get patch file %s: %d\n",
  725. filename, ret);
  726. return ret;
  727. }
  728. }
  729. return 0;
  730. }
  731. static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
  732. {
  733. int ret;
  734. ret = ath6kl_fetch_otp_file(ar);
  735. if (ret)
  736. return ret;
  737. ret = ath6kl_fetch_fw_file(ar);
  738. if (ret)
  739. return ret;
  740. ret = ath6kl_fetch_patch_file(ar);
  741. if (ret)
  742. return ret;
  743. return 0;
  744. }
  745. static int ath6kl_fetch_fw_api2(struct ath6kl *ar)
  746. {
  747. size_t magic_len, len, ie_len;
  748. const struct firmware *fw;
  749. struct ath6kl_fw_ie *hdr;
  750. const char *filename;
  751. const u8 *data;
  752. int ret, ie_id, i, index, bit;
  753. __le32 *val;
  754. switch (ar->version.target_ver) {
  755. case AR6003_REV2_VERSION:
  756. filename = AR6003_REV2_FIRMWARE_2_FILE;
  757. break;
  758. case AR6003_REV3_VERSION:
  759. filename = AR6003_REV3_FIRMWARE_2_FILE;
  760. break;
  761. case AR6004_REV1_VERSION:
  762. filename = AR6004_REV1_FIRMWARE_2_FILE;
  763. break;
  764. default:
  765. return -EOPNOTSUPP;
  766. }
  767. ret = request_firmware(&fw, filename, ar->dev);
  768. if (ret)
  769. return ret;
  770. data = fw->data;
  771. len = fw->size;
  772. /* magic also includes the null byte, check that as well */
  773. magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
  774. if (len < magic_len) {
  775. ret = -EINVAL;
  776. goto out;
  777. }
  778. if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
  779. ret = -EINVAL;
  780. goto out;
  781. }
  782. len -= magic_len;
  783. data += magic_len;
  784. /* loop elements */
  785. while (len > sizeof(struct ath6kl_fw_ie)) {
  786. /* hdr is unaligned! */
  787. hdr = (struct ath6kl_fw_ie *) data;
  788. ie_id = le32_to_cpup(&hdr->id);
  789. ie_len = le32_to_cpup(&hdr->len);
  790. len -= sizeof(*hdr);
  791. data += sizeof(*hdr);
  792. if (len < ie_len) {
  793. ret = -EINVAL;
  794. goto out;
  795. }
  796. switch (ie_id) {
  797. case ATH6KL_FW_IE_OTP_IMAGE:
  798. ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
  799. if (ar->fw_otp == NULL) {
  800. ret = -ENOMEM;
  801. goto out;
  802. }
  803. ar->fw_otp_len = ie_len;
  804. break;
  805. case ATH6KL_FW_IE_FW_IMAGE:
  806. ar->fw = kmemdup(data, ie_len, GFP_KERNEL);
  807. if (ar->fw == NULL) {
  808. ret = -ENOMEM;
  809. goto out;
  810. }
  811. ar->fw_len = ie_len;
  812. break;
  813. case ATH6KL_FW_IE_PATCH_IMAGE:
  814. ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
  815. if (ar->fw_patch == NULL) {
  816. ret = -ENOMEM;
  817. goto out;
  818. }
  819. ar->fw_patch_len = ie_len;
  820. break;
  821. case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
  822. val = (__le32 *) data;
  823. ar->hw.reserved_ram_size = le32_to_cpup(val);
  824. break;
  825. case ATH6KL_FW_IE_CAPABILITIES:
  826. for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
  827. index = ALIGN(i, 8) / 8;
  828. bit = i % 8;
  829. if (data[index] & (1 << bit))
  830. __set_bit(i, ar->fw_capabilities);
  831. }
  832. break;
  833. default:
  834. ath6kl_dbg(ATH6KL_DBG_TRC, "Unknown fw ie: %u\n",
  835. le32_to_cpup(&hdr->id));
  836. break;
  837. }
  838. len -= ie_len;
  839. data += ie_len;
  840. };
  841. ret = 0;
  842. out:
  843. release_firmware(fw);
  844. return ret;
  845. }
  846. static int ath6kl_fetch_firmwares(struct ath6kl *ar)
  847. {
  848. int ret;
  849. ret = ath6kl_fetch_board_file(ar);
  850. if (ret)
  851. return ret;
  852. ret = ath6kl_fetch_fw_api2(ar);
  853. if (ret == 0)
  854. /* fw api 2 found, use it */
  855. return 0;
  856. ret = ath6kl_fetch_fw_api1(ar);
  857. if (ret)
  858. return ret;
  859. return 0;
  860. }
  861. static int ath6kl_upload_board_file(struct ath6kl *ar)
  862. {
  863. u32 board_address, board_ext_address, param;
  864. u32 board_data_size, board_ext_data_size;
  865. int ret;
  866. if (WARN_ON(ar->fw_board == NULL))
  867. return -ENOENT;
  868. /*
  869. * Determine where in Target RAM to write Board Data.
  870. * For AR6004, host determine Target RAM address for
  871. * writing board data.
  872. */
  873. if (ar->target_type == TARGET_TYPE_AR6004) {
  874. board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
  875. ath6kl_bmi_write(ar,
  876. ath6kl_get_hi_item_addr(ar,
  877. HI_ITEM(hi_board_data)),
  878. (u8 *) &board_address, 4);
  879. } else {
  880. ath6kl_bmi_read(ar,
  881. ath6kl_get_hi_item_addr(ar,
  882. HI_ITEM(hi_board_data)),
  883. (u8 *) &board_address, 4);
  884. }
  885. ath6kl_dbg(ATH6KL_DBG_TRC, "board data download addr: 0x%x\n",
  886. board_address);
  887. /* determine where in target ram to write extended board data */
  888. ath6kl_bmi_read(ar,
  889. ath6kl_get_hi_item_addr(ar,
  890. HI_ITEM(hi_board_ext_data)),
  891. (u8 *) &board_ext_address, 4);
  892. ath6kl_dbg(ATH6KL_DBG_TRC, "board file download addr: 0x%x\n",
  893. board_ext_address);
  894. if (board_ext_address == 0) {
  895. ath6kl_err("Failed to get board file target address.\n");
  896. return -EINVAL;
  897. }
  898. switch (ar->target_type) {
  899. case TARGET_TYPE_AR6003:
  900. board_data_size = AR6003_BOARD_DATA_SZ;
  901. board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
  902. break;
  903. case TARGET_TYPE_AR6004:
  904. board_data_size = AR6004_BOARD_DATA_SZ;
  905. board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
  906. break;
  907. default:
  908. WARN_ON(1);
  909. return -EINVAL;
  910. break;
  911. }
  912. if (ar->fw_board_len == (board_data_size +
  913. board_ext_data_size)) {
  914. /* write extended board data */
  915. ret = ath6kl_bmi_write(ar, board_ext_address,
  916. ar->fw_board + board_data_size,
  917. board_ext_data_size);
  918. if (ret) {
  919. ath6kl_err("Failed to write extended board data: %d\n",
  920. ret);
  921. return ret;
  922. }
  923. /* record that extended board data is initialized */
  924. param = (board_ext_data_size << 16) | 1;
  925. ath6kl_bmi_write(ar,
  926. ath6kl_get_hi_item_addr(ar,
  927. HI_ITEM(hi_board_ext_data_config)),
  928. (unsigned char *) &param, 4);
  929. }
  930. if (ar->fw_board_len < board_data_size) {
  931. ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
  932. ret = -EINVAL;
  933. return ret;
  934. }
  935. ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
  936. board_data_size);
  937. if (ret) {
  938. ath6kl_err("Board file bmi write failed: %d\n", ret);
  939. return ret;
  940. }
  941. /* record the fact that Board Data IS initialized */
  942. param = 1;
  943. ath6kl_bmi_write(ar,
  944. ath6kl_get_hi_item_addr(ar,
  945. HI_ITEM(hi_board_data_initialized)),
  946. (u8 *)&param, 4);
  947. return ret;
  948. }
  949. static int ath6kl_upload_otp(struct ath6kl *ar)
  950. {
  951. u32 address, param;
  952. int ret;
  953. if (WARN_ON(ar->fw_otp == NULL))
  954. return -ENOENT;
  955. address = ar->hw.app_load_addr;
  956. ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
  957. ar->fw_otp_len);
  958. if (ret) {
  959. ath6kl_err("Failed to upload OTP file: %d\n", ret);
  960. return ret;
  961. }
  962. /* read firmware start address */
  963. ret = ath6kl_bmi_read(ar,
  964. ath6kl_get_hi_item_addr(ar,
  965. HI_ITEM(hi_app_start)),
  966. (u8 *) &address, sizeof(address));
  967. if (ret) {
  968. ath6kl_err("Failed to read hi_app_start: %d\n", ret);
  969. return ret;
  970. }
  971. ar->hw.app_start_override_addr = address;
  972. /* execute the OTP code */
  973. param = 0;
  974. ath6kl_bmi_execute(ar, address, &param);
  975. return ret;
  976. }
  977. static int ath6kl_upload_firmware(struct ath6kl *ar)
  978. {
  979. u32 address;
  980. int ret;
  981. if (WARN_ON(ar->fw == NULL))
  982. return -ENOENT;
  983. address = ar->hw.app_load_addr;
  984. ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
  985. if (ret) {
  986. ath6kl_err("Failed to write firmware: %d\n", ret);
  987. return ret;
  988. }
  989. /*
  990. * Set starting address for firmware
  991. * Don't need to setup app_start override addr on AR6004
  992. */
  993. if (ar->target_type != TARGET_TYPE_AR6004) {
  994. address = ar->hw.app_start_override_addr;
  995. ath6kl_bmi_set_app_start(ar, address);
  996. }
  997. return ret;
  998. }
  999. static int ath6kl_upload_patch(struct ath6kl *ar)
  1000. {
  1001. u32 address, param;
  1002. int ret;
  1003. if (WARN_ON(ar->fw_patch == NULL))
  1004. return -ENOENT;
  1005. address = ar->hw.dataset_patch_addr;
  1006. ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
  1007. if (ret) {
  1008. ath6kl_err("Failed to write patch file: %d\n", ret);
  1009. return ret;
  1010. }
  1011. param = address;
  1012. ath6kl_bmi_write(ar,
  1013. ath6kl_get_hi_item_addr(ar,
  1014. HI_ITEM(hi_dset_list_head)),
  1015. (unsigned char *) &param, 4);
  1016. return 0;
  1017. }
  1018. static int ath6kl_init_upload(struct ath6kl *ar)
  1019. {
  1020. u32 param, options, sleep, address;
  1021. int status = 0;
  1022. if (ar->target_type != TARGET_TYPE_AR6003 &&
  1023. ar->target_type != TARGET_TYPE_AR6004)
  1024. return -EINVAL;
  1025. /* temporarily disable system sleep */
  1026. address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
  1027. status = ath6kl_bmi_reg_read(ar, address, &param);
  1028. if (status)
  1029. return status;
  1030. options = param;
  1031. param |= ATH6KL_OPTION_SLEEP_DISABLE;
  1032. status = ath6kl_bmi_reg_write(ar, address, param);
  1033. if (status)
  1034. return status;
  1035. address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
  1036. status = ath6kl_bmi_reg_read(ar, address, &param);
  1037. if (status)
  1038. return status;
  1039. sleep = param;
  1040. param |= SM(SYSTEM_SLEEP_DISABLE, 1);
  1041. status = ath6kl_bmi_reg_write(ar, address, param);
  1042. if (status)
  1043. return status;
  1044. ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
  1045. options, sleep);
  1046. /* program analog PLL register */
  1047. /* no need to control 40/44MHz clock on AR6004 */
  1048. if (ar->target_type != TARGET_TYPE_AR6004) {
  1049. status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
  1050. 0xF9104001);
  1051. if (status)
  1052. return status;
  1053. /* Run at 80/88MHz by default */
  1054. param = SM(CPU_CLOCK_STANDARD, 1);
  1055. address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
  1056. status = ath6kl_bmi_reg_write(ar, address, param);
  1057. if (status)
  1058. return status;
  1059. }
  1060. param = 0;
  1061. address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
  1062. param = SM(LPO_CAL_ENABLE, 1);
  1063. status = ath6kl_bmi_reg_write(ar, address, param);
  1064. if (status)
  1065. return status;
  1066. /* WAR to avoid SDIO CRC err */
  1067. if (ar->version.target_ver == AR6003_REV2_VERSION) {
  1068. ath6kl_err("temporary war to avoid sdio crc error\n");
  1069. param = 0x20;
  1070. address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
  1071. status = ath6kl_bmi_reg_write(ar, address, param);
  1072. if (status)
  1073. return status;
  1074. address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
  1075. status = ath6kl_bmi_reg_write(ar, address, param);
  1076. if (status)
  1077. return status;
  1078. address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
  1079. status = ath6kl_bmi_reg_write(ar, address, param);
  1080. if (status)
  1081. return status;
  1082. address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
  1083. status = ath6kl_bmi_reg_write(ar, address, param);
  1084. if (status)
  1085. return status;
  1086. }
  1087. /* write EEPROM data to Target RAM */
  1088. status = ath6kl_upload_board_file(ar);
  1089. if (status)
  1090. return status;
  1091. /* transfer One time Programmable data */
  1092. status = ath6kl_upload_otp(ar);
  1093. if (status)
  1094. return status;
  1095. /* Download Target firmware */
  1096. status = ath6kl_upload_firmware(ar);
  1097. if (status)
  1098. return status;
  1099. status = ath6kl_upload_patch(ar);
  1100. if (status)
  1101. return status;
  1102. /* Restore system sleep */
  1103. address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
  1104. status = ath6kl_bmi_reg_write(ar, address, sleep);
  1105. if (status)
  1106. return status;
  1107. address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
  1108. param = options | 0x20;
  1109. status = ath6kl_bmi_reg_write(ar, address, param);
  1110. if (status)
  1111. return status;
  1112. /* Configure GPIO AR6003 UART */
  1113. param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
  1114. status = ath6kl_bmi_write(ar,
  1115. ath6kl_get_hi_item_addr(ar,
  1116. HI_ITEM(hi_dbg_uart_txpin)),
  1117. (u8 *)&param, 4);
  1118. return status;
  1119. }
  1120. static int ath6kl_init_hw_params(struct ath6kl *ar)
  1121. {
  1122. switch (ar->version.target_ver) {
  1123. case AR6003_REV2_VERSION:
  1124. ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
  1125. ar->hw.app_load_addr = AR6003_REV2_APP_LOAD_ADDRESS;
  1126. ar->hw.board_ext_data_addr = AR6003_REV2_BOARD_EXT_DATA_ADDRESS;
  1127. ar->hw.reserved_ram_size = AR6003_REV2_RAM_RESERVE_SIZE;
  1128. break;
  1129. case AR6003_REV3_VERSION:
  1130. ar->hw.dataset_patch_addr = AR6003_REV3_DATASET_PATCH_ADDRESS;
  1131. ar->hw.app_load_addr = 0x1234;
  1132. ar->hw.board_ext_data_addr = AR6003_REV3_BOARD_EXT_DATA_ADDRESS;
  1133. ar->hw.reserved_ram_size = AR6003_REV3_RAM_RESERVE_SIZE;
  1134. break;
  1135. case AR6004_REV1_VERSION:
  1136. ar->hw.dataset_patch_addr = AR6003_REV2_DATASET_PATCH_ADDRESS;
  1137. ar->hw.app_load_addr = AR6003_REV3_APP_LOAD_ADDRESS;
  1138. ar->hw.board_ext_data_addr = AR6004_REV1_BOARD_EXT_DATA_ADDRESS;
  1139. ar->hw.reserved_ram_size = AR6004_REV1_RAM_RESERVE_SIZE;
  1140. break;
  1141. default:
  1142. ath6kl_err("Unsupported hardware version: 0x%x\n",
  1143. ar->version.target_ver);
  1144. return -EINVAL;
  1145. }
  1146. return 0;
  1147. }
  1148. static int ath6kl_init(struct net_device *dev)
  1149. {
  1150. struct ath6kl *ar = ath6kl_priv(dev);
  1151. int status = 0;
  1152. s32 timeleft;
  1153. if (!ar)
  1154. return -EIO;
  1155. /* Do we need to finish the BMI phase */
  1156. if (ath6kl_bmi_done(ar)) {
  1157. status = -EIO;
  1158. goto ath6kl_init_done;
  1159. }
  1160. /* Indicate that WMI is enabled (although not ready yet) */
  1161. set_bit(WMI_ENABLED, &ar->flag);
  1162. ar->wmi = ath6kl_wmi_init(ar);
  1163. if (!ar->wmi) {
  1164. ath6kl_err("failed to initialize wmi\n");
  1165. status = -EIO;
  1166. goto ath6kl_init_done;
  1167. }
  1168. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
  1169. /*
  1170. * The reason we have to wait for the target here is that the
  1171. * driver layer has to init BMI in order to set the host block
  1172. * size.
  1173. */
  1174. if (ath6kl_htc_wait_target(ar->htc_target)) {
  1175. status = -EIO;
  1176. goto err_node_cleanup;
  1177. }
  1178. if (ath6kl_init_service_ep(ar)) {
  1179. status = -EIO;
  1180. goto err_cleanup_scatter;
  1181. }
  1182. /* setup access class priority mappings */
  1183. ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest */
  1184. ar->ac_stream_pri_map[WMM_AC_BE] = 1;
  1185. ar->ac_stream_pri_map[WMM_AC_VI] = 2;
  1186. ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
  1187. /* give our connected endpoints some buffers */
  1188. ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
  1189. ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
  1190. /* allocate some buffers that handle larger AMSDU frames */
  1191. ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
  1192. /* setup credit distribution */
  1193. ath6k_setup_credit_dist(ar->htc_target, &ar->credit_state_info);
  1194. ath6kl_cookie_init(ar);
  1195. /* start HTC */
  1196. status = ath6kl_htc_start(ar->htc_target);
  1197. if (status) {
  1198. ath6kl_cookie_cleanup(ar);
  1199. goto err_rxbuf_cleanup;
  1200. }
  1201. /* Wait for Wmi event to be ready */
  1202. timeleft = wait_event_interruptible_timeout(ar->event_wq,
  1203. test_bit(WMI_READY,
  1204. &ar->flag),
  1205. WMI_TIMEOUT);
  1206. if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
  1207. ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
  1208. ATH6KL_ABI_VERSION, ar->version.abi_ver);
  1209. status = -EIO;
  1210. goto err_htc_stop;
  1211. }
  1212. if (!timeleft || signal_pending(current)) {
  1213. ath6kl_err("wmi is not ready or wait was interrupted\n");
  1214. status = -EIO;
  1215. goto err_htc_stop;
  1216. }
  1217. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
  1218. /* communicate the wmi protocol verision to the target */
  1219. if ((ath6kl_set_host_app_area(ar)) != 0)
  1220. ath6kl_err("unable to set the host app area\n");
  1221. ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
  1222. ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
  1223. status = ath6kl_target_config_wlan_params(ar);
  1224. if (!status)
  1225. goto ath6kl_init_done;
  1226. err_htc_stop:
  1227. ath6kl_htc_stop(ar->htc_target);
  1228. err_rxbuf_cleanup:
  1229. ath6kl_htc_flush_rx_buf(ar->htc_target);
  1230. ath6kl_cleanup_amsdu_rxbufs(ar);
  1231. err_cleanup_scatter:
  1232. ath6kl_hif_cleanup_scatter(ar);
  1233. err_node_cleanup:
  1234. ath6kl_wmi_shutdown(ar->wmi);
  1235. clear_bit(WMI_ENABLED, &ar->flag);
  1236. ar->wmi = NULL;
  1237. ath6kl_init_done:
  1238. return status;
  1239. }
  1240. int ath6kl_core_init(struct ath6kl *ar)
  1241. {
  1242. int ret = 0;
  1243. struct ath6kl_bmi_target_info targ_info;
  1244. ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
  1245. if (!ar->ath6kl_wq)
  1246. return -ENOMEM;
  1247. ret = ath6kl_bmi_init(ar);
  1248. if (ret)
  1249. goto err_wq;
  1250. ret = ath6kl_bmi_get_target_info(ar, &targ_info);
  1251. if (ret)
  1252. goto err_bmi_cleanup;
  1253. ar->version.target_ver = le32_to_cpu(targ_info.version);
  1254. ar->target_type = le32_to_cpu(targ_info.type);
  1255. ar->wdev->wiphy->hw_version = le32_to_cpu(targ_info.version);
  1256. ret = ath6kl_init_hw_params(ar);
  1257. if (ret)
  1258. goto err_bmi_cleanup;
  1259. ret = ath6kl_configure_target(ar);
  1260. if (ret)
  1261. goto err_bmi_cleanup;
  1262. ar->htc_target = ath6kl_htc_create(ar);
  1263. if (!ar->htc_target) {
  1264. ret = -ENOMEM;
  1265. goto err_bmi_cleanup;
  1266. }
  1267. ar->aggr_cntxt = aggr_init(ar->net_dev);
  1268. if (!ar->aggr_cntxt) {
  1269. ath6kl_err("failed to initialize aggr\n");
  1270. ret = -ENOMEM;
  1271. goto err_htc_cleanup;
  1272. }
  1273. ret = ath6kl_fetch_firmwares(ar);
  1274. if (ret)
  1275. goto err_htc_cleanup;
  1276. ret = ath6kl_init_upload(ar);
  1277. if (ret)
  1278. goto err_htc_cleanup;
  1279. ret = ath6kl_init(ar->net_dev);
  1280. if (ret)
  1281. goto err_htc_cleanup;
  1282. /* This runs the init function if registered */
  1283. ret = register_netdev(ar->net_dev);
  1284. if (ret) {
  1285. ath6kl_err("register_netdev failed\n");
  1286. ath6kl_destroy(ar->net_dev, 0);
  1287. return ret;
  1288. }
  1289. set_bit(NETDEV_REGISTERED, &ar->flag);
  1290. ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
  1291. __func__, ar->net_dev->name, ar->net_dev, ar);
  1292. return ret;
  1293. err_htc_cleanup:
  1294. ath6kl_htc_cleanup(ar->htc_target);
  1295. err_bmi_cleanup:
  1296. ath6kl_bmi_cleanup(ar);
  1297. err_wq:
  1298. destroy_workqueue(ar->ath6kl_wq);
  1299. return ret;
  1300. }
  1301. void ath6kl_stop_txrx(struct ath6kl *ar)
  1302. {
  1303. struct net_device *ndev = ar->net_dev;
  1304. if (!ndev)
  1305. return;
  1306. set_bit(DESTROY_IN_PROGRESS, &ar->flag);
  1307. if (down_interruptible(&ar->sem)) {
  1308. ath6kl_err("down_interruptible failed\n");
  1309. return;
  1310. }
  1311. if (ar->wlan_pwr_state != WLAN_POWER_STATE_CUT_PWR)
  1312. ath6kl_stop_endpoint(ndev, false, true);
  1313. clear_bit(WLAN_ENABLED, &ar->flag);
  1314. }
  1315. /*
  1316. * We need to differentiate between the surprise and planned removal of the
  1317. * device because of the following consideration:
  1318. *
  1319. * - In case of surprise removal, the hcd already frees up the pending
  1320. * for the device and hence there is no need to unregister the function
  1321. * driver inorder to get these requests. For planned removal, the function
  1322. * driver has to explicitly unregister itself to have the hcd return all the
  1323. * pending requests before the data structures for the devices are freed up.
  1324. * Note that as per the current implementation, the function driver will
  1325. * end up releasing all the devices since there is no API to selectively
  1326. * release a particular device.
  1327. *
  1328. * - Certain commands issued to the target can be skipped for surprise
  1329. * removal since they will anyway not go through.
  1330. */
  1331. void ath6kl_destroy(struct net_device *dev, unsigned int unregister)
  1332. {
  1333. struct ath6kl *ar;
  1334. if (!dev || !ath6kl_priv(dev)) {
  1335. ath6kl_err("failed to get device structure\n");
  1336. return;
  1337. }
  1338. ar = ath6kl_priv(dev);
  1339. destroy_workqueue(ar->ath6kl_wq);
  1340. if (ar->htc_target)
  1341. ath6kl_htc_cleanup(ar->htc_target);
  1342. aggr_module_destroy(ar->aggr_cntxt);
  1343. ath6kl_cookie_cleanup(ar);
  1344. ath6kl_cleanup_amsdu_rxbufs(ar);
  1345. ath6kl_bmi_cleanup(ar);
  1346. ath6kl_debug_cleanup(ar);
  1347. if (unregister && test_bit(NETDEV_REGISTERED, &ar->flag)) {
  1348. unregister_netdev(dev);
  1349. clear_bit(NETDEV_REGISTERED, &ar->flag);
  1350. }
  1351. free_netdev(dev);
  1352. kfree(ar->fw_board);
  1353. kfree(ar->fw_otp);
  1354. kfree(ar->fw);
  1355. kfree(ar->fw_patch);
  1356. ath6kl_cfg80211_deinit(ar);
  1357. }