init.c 41 KB

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