init.c 39 KB

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