init.c 40 KB

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