rsi_91x_hal.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /**
  2. * Copyright (c) 2014 Redpine Signals 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/firmware.h>
  17. #include <net/bluetooth/bluetooth.h>
  18. #include "rsi_mgmt.h"
  19. #include "rsi_hal.h"
  20. #include "rsi_sdio.h"
  21. #include "rsi_common.h"
  22. /* FLASH Firmware */
  23. static struct ta_metadata metadata_flash_content[] = {
  24. {"flash_content", 0x00010000},
  25. {"rsi/rs9113_wlan_qspi.rps", 0x00010000},
  26. {"rsi/rs9113_wlan_bt_dual_mode.rps", 0x00010000},
  27. {"flash_content", 0x00010000},
  28. {"rsi/rs9113_ap_bt_dual_mode.rps", 0x00010000},
  29. };
  30. int rsi_send_pkt_to_bus(struct rsi_common *common, struct sk_buff *skb)
  31. {
  32. struct rsi_hw *adapter = common->priv;
  33. int status;
  34. if (common->coex_mode > 1)
  35. mutex_lock(&common->tx_bus_mutex);
  36. status = adapter->host_intf_ops->write_pkt(common->priv,
  37. skb->data, skb->len);
  38. if (common->coex_mode > 1)
  39. mutex_unlock(&common->tx_bus_mutex);
  40. return status;
  41. }
  42. int rsi_prepare_mgmt_desc(struct rsi_common *common, struct sk_buff *skb)
  43. {
  44. struct rsi_hw *adapter = common->priv;
  45. struct ieee80211_hdr *wh = NULL;
  46. struct ieee80211_tx_info *info;
  47. struct ieee80211_conf *conf = &adapter->hw->conf;
  48. struct ieee80211_vif *vif;
  49. struct rsi_mgmt_desc *mgmt_desc;
  50. struct skb_info *tx_params;
  51. struct rsi_xtended_desc *xtend_desc = NULL;
  52. u8 header_size;
  53. u32 dword_align_bytes = 0;
  54. if (skb->len > MAX_MGMT_PKT_SIZE) {
  55. rsi_dbg(INFO_ZONE, "%s: Dropping mgmt pkt > 512\n", __func__);
  56. return -EINVAL;
  57. }
  58. info = IEEE80211_SKB_CB(skb);
  59. tx_params = (struct skb_info *)info->driver_data;
  60. vif = tx_params->vif;
  61. /* Update header size */
  62. header_size = FRAME_DESC_SZ + sizeof(struct rsi_xtended_desc);
  63. if (header_size > skb_headroom(skb)) {
  64. rsi_dbg(ERR_ZONE,
  65. "%s: Failed to add extended descriptor\n",
  66. __func__);
  67. return -ENOSPC;
  68. }
  69. skb_push(skb, header_size);
  70. dword_align_bytes = ((unsigned long)skb->data & 0x3f);
  71. if (dword_align_bytes > skb_headroom(skb)) {
  72. rsi_dbg(ERR_ZONE,
  73. "%s: Failed to add dword align\n", __func__);
  74. return -ENOSPC;
  75. }
  76. skb_push(skb, dword_align_bytes);
  77. header_size += dword_align_bytes;
  78. tx_params->internal_hdr_size = header_size;
  79. memset(&skb->data[0], 0, header_size);
  80. wh = (struct ieee80211_hdr *)&skb->data[header_size];
  81. mgmt_desc = (struct rsi_mgmt_desc *)skb->data;
  82. xtend_desc = (struct rsi_xtended_desc *)&skb->data[FRAME_DESC_SZ];
  83. rsi_set_len_qno(&mgmt_desc->len_qno, (skb->len - FRAME_DESC_SZ),
  84. RSI_WIFI_MGMT_Q);
  85. mgmt_desc->frame_type = TX_DOT11_MGMT;
  86. mgmt_desc->header_len = MIN_802_11_HDR_LEN;
  87. mgmt_desc->xtend_desc_size = header_size - FRAME_DESC_SZ;
  88. mgmt_desc->frame_info |= cpu_to_le16(RATE_INFO_ENABLE);
  89. if (is_broadcast_ether_addr(wh->addr1))
  90. mgmt_desc->frame_info |= cpu_to_le16(RSI_BROADCAST_PKT);
  91. mgmt_desc->seq_ctrl =
  92. cpu_to_le16(IEEE80211_SEQ_TO_SN(le16_to_cpu(wh->seq_ctrl)));
  93. if ((common->band == NL80211_BAND_2GHZ) && !common->p2p_enabled)
  94. mgmt_desc->rate_info = cpu_to_le16(RSI_RATE_1);
  95. else
  96. mgmt_desc->rate_info = cpu_to_le16(RSI_RATE_6);
  97. if (conf_is_ht40(conf))
  98. mgmt_desc->bbp_info = cpu_to_le16(FULL40M_ENABLE);
  99. if (ieee80211_is_probe_resp(wh->frame_control)) {
  100. mgmt_desc->misc_flags |= (RSI_ADD_DELTA_TSF_VAP_ID |
  101. RSI_FETCH_RETRY_CNT_FRM_HST);
  102. #define PROBE_RESP_RETRY_CNT 3
  103. xtend_desc->retry_cnt = PROBE_RESP_RETRY_CNT;
  104. }
  105. if (((vif->type == NL80211_IFTYPE_AP) ||
  106. (vif->type == NL80211_IFTYPE_P2P_GO)) &&
  107. (ieee80211_is_action(wh->frame_control))) {
  108. struct rsi_sta *rsta = rsi_find_sta(common, wh->addr1);
  109. if (rsta)
  110. mgmt_desc->sta_id = tx_params->sta_id;
  111. else
  112. return -EINVAL;
  113. }
  114. mgmt_desc->rate_info |=
  115. cpu_to_le16((tx_params->vap_id << RSI_DESC_VAP_ID_OFST) &
  116. RSI_DESC_VAP_ID_MASK);
  117. return 0;
  118. }
  119. /* This function prepares descriptor for given data packet */
  120. int rsi_prepare_data_desc(struct rsi_common *common, struct sk_buff *skb)
  121. {
  122. struct rsi_hw *adapter = common->priv;
  123. struct ieee80211_vif *vif;
  124. struct ieee80211_hdr *wh = NULL;
  125. struct ieee80211_tx_info *info;
  126. struct skb_info *tx_params;
  127. struct rsi_data_desc *data_desc;
  128. struct rsi_xtended_desc *xtend_desc;
  129. u8 ieee80211_size = MIN_802_11_HDR_LEN;
  130. u8 header_size;
  131. u8 vap_id = 0;
  132. u8 dword_align_bytes;
  133. u16 seq_num;
  134. info = IEEE80211_SKB_CB(skb);
  135. vif = info->control.vif;
  136. tx_params = (struct skb_info *)info->driver_data;
  137. header_size = FRAME_DESC_SZ + sizeof(struct rsi_xtended_desc);
  138. if (header_size > skb_headroom(skb)) {
  139. rsi_dbg(ERR_ZONE, "%s: Unable to send pkt\n", __func__);
  140. return -ENOSPC;
  141. }
  142. skb_push(skb, header_size);
  143. dword_align_bytes = ((unsigned long)skb->data & 0x3f);
  144. if (header_size > skb_headroom(skb)) {
  145. rsi_dbg(ERR_ZONE, "%s: Not enough headroom\n", __func__);
  146. return -ENOSPC;
  147. }
  148. skb_push(skb, dword_align_bytes);
  149. header_size += dword_align_bytes;
  150. tx_params->internal_hdr_size = header_size;
  151. data_desc = (struct rsi_data_desc *)skb->data;
  152. memset(data_desc, 0, header_size);
  153. xtend_desc = (struct rsi_xtended_desc *)&skb->data[FRAME_DESC_SZ];
  154. wh = (struct ieee80211_hdr *)&skb->data[header_size];
  155. seq_num = IEEE80211_SEQ_TO_SN(le16_to_cpu(wh->seq_ctrl));
  156. data_desc->xtend_desc_size = header_size - FRAME_DESC_SZ;
  157. if (ieee80211_is_data_qos(wh->frame_control)) {
  158. ieee80211_size += 2;
  159. data_desc->mac_flags |= cpu_to_le16(RSI_QOS_ENABLE);
  160. }
  161. if (((vif->type == NL80211_IFTYPE_STATION) ||
  162. (vif->type == NL80211_IFTYPE_P2P_CLIENT)) &&
  163. (adapter->ps_state == PS_ENABLED))
  164. wh->frame_control |= cpu_to_le16(RSI_SET_PS_ENABLE);
  165. if ((!(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) &&
  166. (common->secinfo.security_enable)) {
  167. if (rsi_is_cipher_wep(common))
  168. ieee80211_size += 4;
  169. else
  170. ieee80211_size += 8;
  171. data_desc->mac_flags |= cpu_to_le16(RSI_ENCRYPT_PKT);
  172. }
  173. rsi_set_len_qno(&data_desc->len_qno, (skb->len - FRAME_DESC_SZ),
  174. RSI_WIFI_DATA_Q);
  175. data_desc->header_len = ieee80211_size;
  176. if (common->min_rate != RSI_RATE_AUTO) {
  177. /* Send fixed rate */
  178. data_desc->frame_info = cpu_to_le16(RATE_INFO_ENABLE);
  179. data_desc->rate_info = cpu_to_le16(common->min_rate);
  180. if (conf_is_ht40(&common->priv->hw->conf))
  181. data_desc->bbp_info = cpu_to_le16(FULL40M_ENABLE);
  182. if ((common->vif_info[0].sgi) && (common->min_rate & 0x100)) {
  183. /* Only MCS rates */
  184. data_desc->rate_info |=
  185. cpu_to_le16(ENABLE_SHORTGI_RATE);
  186. }
  187. }
  188. if (skb->protocol == cpu_to_be16(ETH_P_PAE)) {
  189. rsi_dbg(INFO_ZONE, "*** Tx EAPOL ***\n");
  190. data_desc->frame_info = cpu_to_le16(RATE_INFO_ENABLE);
  191. if (common->band == NL80211_BAND_5GHZ)
  192. data_desc->rate_info = cpu_to_le16(RSI_RATE_6);
  193. else
  194. data_desc->rate_info = cpu_to_le16(RSI_RATE_1);
  195. data_desc->mac_flags |= cpu_to_le16(RSI_REKEY_PURPOSE);
  196. data_desc->misc_flags |= RSI_FETCH_RETRY_CNT_FRM_HST;
  197. #define EAPOL_RETRY_CNT 15
  198. xtend_desc->retry_cnt = EAPOL_RETRY_CNT;
  199. if (common->eapol4_confirm)
  200. skb->priority = VO_Q;
  201. else
  202. rsi_set_len_qno(&data_desc->len_qno,
  203. (skb->len - FRAME_DESC_SZ),
  204. RSI_WIFI_MGMT_Q);
  205. if ((skb->len - header_size) == EAPOL4_PACKET_LEN) {
  206. data_desc->misc_flags |=
  207. RSI_DESC_REQUIRE_CFM_TO_HOST;
  208. xtend_desc->confirm_frame_type = EAPOL4_CONFIRM;
  209. }
  210. }
  211. data_desc->mac_flags |= cpu_to_le16(seq_num & 0xfff);
  212. data_desc->qid_tid = ((skb->priority & 0xf) |
  213. ((tx_params->tid & 0xf) << 4));
  214. data_desc->sta_id = tx_params->sta_id;
  215. if ((is_broadcast_ether_addr(wh->addr1)) ||
  216. (is_multicast_ether_addr(wh->addr1))) {
  217. data_desc->frame_info = cpu_to_le16(RATE_INFO_ENABLE);
  218. data_desc->frame_info |= cpu_to_le16(RSI_BROADCAST_PKT);
  219. data_desc->sta_id = vap_id;
  220. if ((vif->type == NL80211_IFTYPE_AP) ||
  221. (vif->type == NL80211_IFTYPE_P2P_GO)) {
  222. if (common->band == NL80211_BAND_5GHZ)
  223. data_desc->rate_info = cpu_to_le16(RSI_RATE_6);
  224. else
  225. data_desc->rate_info = cpu_to_le16(RSI_RATE_1);
  226. }
  227. }
  228. if (((vif->type == NL80211_IFTYPE_AP) ||
  229. (vif->type == NL80211_IFTYPE_P2P_GO)) &&
  230. (ieee80211_has_moredata(wh->frame_control)))
  231. data_desc->frame_info |= cpu_to_le16(MORE_DATA_PRESENT);
  232. data_desc->rate_info |=
  233. cpu_to_le16((tx_params->vap_id << RSI_DESC_VAP_ID_OFST) &
  234. RSI_DESC_VAP_ID_MASK);
  235. return 0;
  236. }
  237. /* This function sends received data packet from driver to device */
  238. int rsi_send_data_pkt(struct rsi_common *common, struct sk_buff *skb)
  239. {
  240. struct rsi_hw *adapter = common->priv;
  241. struct ieee80211_vif *vif;
  242. struct ieee80211_tx_info *info;
  243. struct skb_info *tx_params;
  244. struct ieee80211_bss_conf *bss;
  245. int status = -EINVAL;
  246. u8 header_size;
  247. if (!skb)
  248. return 0;
  249. if (common->iface_down)
  250. goto err;
  251. info = IEEE80211_SKB_CB(skb);
  252. if (!info->control.vif)
  253. goto err;
  254. vif = info->control.vif;
  255. bss = &vif->bss_conf;
  256. tx_params = (struct skb_info *)info->driver_data;
  257. header_size = tx_params->internal_hdr_size;
  258. if (((vif->type == NL80211_IFTYPE_STATION) ||
  259. (vif->type == NL80211_IFTYPE_P2P_CLIENT)) &&
  260. (!bss->assoc))
  261. goto err;
  262. status = rsi_send_pkt_to_bus(common, skb);
  263. if (status)
  264. rsi_dbg(ERR_ZONE, "%s: Failed to write pkt\n", __func__);
  265. err:
  266. ++common->tx_stats.total_tx_pkt_freed[skb->priority];
  267. rsi_indicate_tx_status(adapter, skb, status);
  268. return status;
  269. }
  270. /**
  271. * rsi_send_mgmt_pkt() - This functions sends the received management packet
  272. * from driver to device.
  273. * @common: Pointer to the driver private structure.
  274. * @skb: Pointer to the socket buffer structure.
  275. *
  276. * Return: status: 0 on success, -1 on failure.
  277. */
  278. int rsi_send_mgmt_pkt(struct rsi_common *common,
  279. struct sk_buff *skb)
  280. {
  281. struct rsi_hw *adapter = common->priv;
  282. struct ieee80211_bss_conf *bss;
  283. struct ieee80211_hdr *wh;
  284. struct ieee80211_tx_info *info;
  285. struct skb_info *tx_params;
  286. struct rsi_mgmt_desc *mgmt_desc;
  287. struct rsi_xtended_desc *xtend_desc;
  288. int status = -E2BIG;
  289. u8 header_size;
  290. info = IEEE80211_SKB_CB(skb);
  291. tx_params = (struct skb_info *)info->driver_data;
  292. header_size = tx_params->internal_hdr_size;
  293. if (tx_params->flags & INTERNAL_MGMT_PKT) {
  294. status = adapter->host_intf_ops->write_pkt(common->priv,
  295. (u8 *)skb->data,
  296. skb->len);
  297. if (status) {
  298. rsi_dbg(ERR_ZONE,
  299. "%s: Failed to write the packet\n", __func__);
  300. }
  301. dev_kfree_skb(skb);
  302. return status;
  303. }
  304. bss = &info->control.vif->bss_conf;
  305. wh = (struct ieee80211_hdr *)&skb->data[header_size];
  306. mgmt_desc = (struct rsi_mgmt_desc *)skb->data;
  307. xtend_desc = (struct rsi_xtended_desc *)&skb->data[FRAME_DESC_SZ];
  308. /* Indicate to firmware to give cfm for probe */
  309. if (ieee80211_is_probe_req(wh->frame_control) && !bss->assoc) {
  310. rsi_dbg(INFO_ZONE,
  311. "%s: blocking mgmt queue\n", __func__);
  312. mgmt_desc->misc_flags = RSI_DESC_REQUIRE_CFM_TO_HOST;
  313. xtend_desc->confirm_frame_type = PROBEREQ_CONFIRM;
  314. common->mgmt_q_block = true;
  315. rsi_dbg(INFO_ZONE, "Mgmt queue blocked\n");
  316. }
  317. status = rsi_send_pkt_to_bus(common, skb);
  318. if (status)
  319. rsi_dbg(ERR_ZONE, "%s: Failed to write the packet\n", __func__);
  320. rsi_indicate_tx_status(common->priv, skb, status);
  321. return status;
  322. }
  323. int rsi_send_bt_pkt(struct rsi_common *common, struct sk_buff *skb)
  324. {
  325. int status = -EINVAL;
  326. u8 header_size = 0;
  327. struct rsi_bt_desc *bt_desc;
  328. u8 queueno = ((skb->data[1] >> 4) & 0xf);
  329. if (queueno == RSI_BT_MGMT_Q) {
  330. status = rsi_send_pkt_to_bus(common, skb);
  331. if (status)
  332. rsi_dbg(ERR_ZONE, "%s: Failed to write bt mgmt pkt\n",
  333. __func__);
  334. goto out;
  335. }
  336. header_size = FRAME_DESC_SZ;
  337. if (header_size > skb_headroom(skb)) {
  338. rsi_dbg(ERR_ZONE, "%s: Not enough headroom\n", __func__);
  339. status = -ENOSPC;
  340. goto out;
  341. }
  342. skb_push(skb, header_size);
  343. memset(skb->data, 0, header_size);
  344. bt_desc = (struct rsi_bt_desc *)skb->data;
  345. rsi_set_len_qno(&bt_desc->len_qno, (skb->len - FRAME_DESC_SZ),
  346. RSI_BT_DATA_Q);
  347. bt_desc->bt_pkt_type = cpu_to_le16(bt_cb(skb)->pkt_type);
  348. status = rsi_send_pkt_to_bus(common, skb);
  349. if (status)
  350. rsi_dbg(ERR_ZONE, "%s: Failed to write bt pkt\n", __func__);
  351. out:
  352. dev_kfree_skb(skb);
  353. return status;
  354. }
  355. int rsi_prepare_beacon(struct rsi_common *common, struct sk_buff *skb)
  356. {
  357. struct rsi_hw *adapter = (struct rsi_hw *)common->priv;
  358. struct rsi_data_desc *bcn_frm;
  359. struct ieee80211_hw *hw = common->priv->hw;
  360. struct ieee80211_conf *conf = &hw->conf;
  361. struct ieee80211_vif *vif;
  362. struct sk_buff *mac_bcn;
  363. u8 vap_id = 0, i;
  364. u16 tim_offset = 0;
  365. for (i = 0; i < RSI_MAX_VIFS; i++) {
  366. vif = adapter->vifs[i];
  367. if (!vif)
  368. continue;
  369. if ((vif->type == NL80211_IFTYPE_AP) ||
  370. (vif->type == NL80211_IFTYPE_P2P_GO))
  371. break;
  372. }
  373. if (!vif)
  374. return -EINVAL;
  375. mac_bcn = ieee80211_beacon_get_tim(adapter->hw,
  376. vif,
  377. &tim_offset, NULL);
  378. if (!mac_bcn) {
  379. rsi_dbg(ERR_ZONE, "Failed to get beacon from mac80211\n");
  380. return -EINVAL;
  381. }
  382. common->beacon_cnt++;
  383. bcn_frm = (struct rsi_data_desc *)skb->data;
  384. rsi_set_len_qno(&bcn_frm->len_qno, mac_bcn->len, RSI_WIFI_DATA_Q);
  385. bcn_frm->header_len = MIN_802_11_HDR_LEN;
  386. bcn_frm->frame_info = cpu_to_le16(RSI_DATA_DESC_MAC_BBP_INFO |
  387. RSI_DATA_DESC_NO_ACK_IND |
  388. RSI_DATA_DESC_BEACON_FRAME |
  389. RSI_DATA_DESC_INSERT_TSF |
  390. RSI_DATA_DESC_INSERT_SEQ_NO |
  391. RATE_INFO_ENABLE);
  392. bcn_frm->rate_info = cpu_to_le16(vap_id << 14);
  393. bcn_frm->qid_tid = BEACON_HW_Q;
  394. if (conf_is_ht40_plus(conf)) {
  395. bcn_frm->bbp_info = cpu_to_le16(LOWER_20_ENABLE);
  396. bcn_frm->bbp_info |= cpu_to_le16(LOWER_20_ENABLE >> 12);
  397. } else if (conf_is_ht40_minus(conf)) {
  398. bcn_frm->bbp_info = cpu_to_le16(UPPER_20_ENABLE);
  399. bcn_frm->bbp_info |= cpu_to_le16(UPPER_20_ENABLE >> 12);
  400. }
  401. if (common->band == NL80211_BAND_2GHZ)
  402. bcn_frm->bbp_info |= cpu_to_le16(RSI_RATE_1);
  403. else
  404. bcn_frm->bbp_info |= cpu_to_le16(RSI_RATE_6);
  405. if (mac_bcn->data[tim_offset + 2] == 0)
  406. bcn_frm->frame_info |= cpu_to_le16(RSI_DATA_DESC_DTIM_BEACON);
  407. memcpy(&skb->data[FRAME_DESC_SZ], mac_bcn->data, mac_bcn->len);
  408. skb_put(skb, mac_bcn->len + FRAME_DESC_SZ);
  409. dev_kfree_skb(mac_bcn);
  410. return 0;
  411. }
  412. static void bl_cmd_timeout(struct timer_list *t)
  413. {
  414. struct rsi_hw *adapter = from_timer(adapter, t, bl_cmd_timer);
  415. adapter->blcmd_timer_expired = true;
  416. del_timer(&adapter->bl_cmd_timer);
  417. }
  418. static int bl_start_cmd_timer(struct rsi_hw *adapter, u32 timeout)
  419. {
  420. timer_setup(&adapter->bl_cmd_timer, bl_cmd_timeout, 0);
  421. adapter->bl_cmd_timer.expires = (msecs_to_jiffies(timeout) + jiffies);
  422. adapter->blcmd_timer_expired = false;
  423. add_timer(&adapter->bl_cmd_timer);
  424. return 0;
  425. }
  426. static int bl_stop_cmd_timer(struct rsi_hw *adapter)
  427. {
  428. adapter->blcmd_timer_expired = false;
  429. if (timer_pending(&adapter->bl_cmd_timer))
  430. del_timer(&adapter->bl_cmd_timer);
  431. return 0;
  432. }
  433. static int bl_write_cmd(struct rsi_hw *adapter, u8 cmd, u8 exp_resp,
  434. u16 *cmd_resp)
  435. {
  436. struct rsi_host_intf_ops *hif_ops = adapter->host_intf_ops;
  437. u32 regin_val = 0, regout_val = 0;
  438. u32 regin_input = 0;
  439. u8 output = 0;
  440. int status;
  441. regin_input = (REGIN_INPUT | adapter->priv->coex_mode);
  442. while (!adapter->blcmd_timer_expired) {
  443. regin_val = 0;
  444. status = hif_ops->master_reg_read(adapter, SWBL_REGIN,
  445. &regin_val, 2);
  446. if (status < 0) {
  447. rsi_dbg(ERR_ZONE,
  448. "%s: Command %0x REGIN reading failed..\n",
  449. __func__, cmd);
  450. return status;
  451. }
  452. mdelay(1);
  453. if ((regin_val >> 12) != REGIN_VALID)
  454. break;
  455. }
  456. if (adapter->blcmd_timer_expired) {
  457. rsi_dbg(ERR_ZONE,
  458. "%s: Command %0x REGIN reading timed out..\n",
  459. __func__, cmd);
  460. return -ETIMEDOUT;
  461. }
  462. rsi_dbg(INFO_ZONE,
  463. "Issuing write to Regin val:%0x sending cmd:%0x\n",
  464. regin_val, (cmd | regin_input << 8));
  465. status = hif_ops->master_reg_write(adapter, SWBL_REGIN,
  466. (cmd | regin_input << 8), 2);
  467. if (status < 0)
  468. return status;
  469. mdelay(1);
  470. if (cmd == LOAD_HOSTED_FW || cmd == JUMP_TO_ZERO_PC) {
  471. /* JUMP_TO_ZERO_PC doesn't expect
  472. * any response. So return from here
  473. */
  474. return 0;
  475. }
  476. while (!adapter->blcmd_timer_expired) {
  477. regout_val = 0;
  478. status = hif_ops->master_reg_read(adapter, SWBL_REGOUT,
  479. &regout_val, 2);
  480. if (status < 0) {
  481. rsi_dbg(ERR_ZONE,
  482. "%s: Command %0x REGOUT reading failed..\n",
  483. __func__, cmd);
  484. return status;
  485. }
  486. mdelay(1);
  487. if ((regout_val >> 8) == REGOUT_VALID)
  488. break;
  489. }
  490. if (adapter->blcmd_timer_expired) {
  491. rsi_dbg(ERR_ZONE,
  492. "%s: Command %0x REGOUT reading timed out..\n",
  493. __func__, cmd);
  494. return status;
  495. }
  496. *cmd_resp = ((u16 *)&regout_val)[0] & 0xffff;
  497. output = ((u8 *)&regout_val)[0] & 0xff;
  498. status = hif_ops->master_reg_write(adapter, SWBL_REGOUT,
  499. (cmd | REGOUT_INVALID << 8), 2);
  500. if (status < 0) {
  501. rsi_dbg(ERR_ZONE,
  502. "%s: Command %0x REGOUT writing failed..\n",
  503. __func__, cmd);
  504. return status;
  505. }
  506. mdelay(1);
  507. if (output != exp_resp) {
  508. rsi_dbg(ERR_ZONE,
  509. "%s: Recvd resp %x for cmd %0x\n",
  510. __func__, output, cmd);
  511. return -EINVAL;
  512. }
  513. rsi_dbg(INFO_ZONE,
  514. "%s: Recvd Expected resp %x for cmd %0x\n",
  515. __func__, output, cmd);
  516. return 0;
  517. }
  518. static int bl_cmd(struct rsi_hw *adapter, u8 cmd, u8 exp_resp, char *str)
  519. {
  520. u16 regout_val = 0;
  521. u32 timeout;
  522. int status;
  523. if ((cmd == EOF_REACHED) || (cmd == PING_VALID) || (cmd == PONG_VALID))
  524. timeout = BL_BURN_TIMEOUT;
  525. else
  526. timeout = BL_CMD_TIMEOUT;
  527. bl_start_cmd_timer(adapter, timeout);
  528. status = bl_write_cmd(adapter, cmd, exp_resp, &regout_val);
  529. if (status < 0) {
  530. bl_stop_cmd_timer(adapter);
  531. rsi_dbg(ERR_ZONE,
  532. "%s: Command %s (%0x) writing failed..\n",
  533. __func__, str, cmd);
  534. return status;
  535. }
  536. bl_stop_cmd_timer(adapter);
  537. return 0;
  538. }
  539. #define CHECK_SUM_OFFSET 20
  540. #define LEN_OFFSET 8
  541. #define ADDR_OFFSET 16
  542. static int bl_write_header(struct rsi_hw *adapter, u8 *flash_content,
  543. u32 content_size)
  544. {
  545. struct rsi_host_intf_ops *hif_ops = adapter->host_intf_ops;
  546. struct bl_header *bl_hdr;
  547. u32 write_addr, write_len;
  548. int status;
  549. bl_hdr = kzalloc(sizeof(*bl_hdr), GFP_KERNEL);
  550. if (!bl_hdr)
  551. return -ENOMEM;
  552. bl_hdr->flags = 0;
  553. bl_hdr->image_no = cpu_to_le32(adapter->priv->coex_mode);
  554. bl_hdr->check_sum =
  555. cpu_to_le32(*(u32 *)&flash_content[CHECK_SUM_OFFSET]);
  556. bl_hdr->flash_start_address =
  557. cpu_to_le32(*(u32 *)&flash_content[ADDR_OFFSET]);
  558. bl_hdr->flash_len = cpu_to_le32(*(u32 *)&flash_content[LEN_OFFSET]);
  559. write_len = sizeof(struct bl_header);
  560. if (adapter->rsi_host_intf == RSI_HOST_INTF_USB) {
  561. write_addr = PING_BUFFER_ADDRESS;
  562. status = hif_ops->write_reg_multiple(adapter, write_addr,
  563. (u8 *)bl_hdr, write_len);
  564. if (status < 0) {
  565. rsi_dbg(ERR_ZONE,
  566. "%s: Failed to load Version/CRC structure\n",
  567. __func__);
  568. goto fail;
  569. }
  570. } else {
  571. write_addr = PING_BUFFER_ADDRESS >> 16;
  572. status = hif_ops->master_access_msword(adapter, write_addr);
  573. if (status < 0) {
  574. rsi_dbg(ERR_ZONE,
  575. "%s: Unable to set ms word to common reg\n",
  576. __func__);
  577. goto fail;
  578. }
  579. write_addr = RSI_SD_REQUEST_MASTER |
  580. (PING_BUFFER_ADDRESS & 0xFFFF);
  581. status = hif_ops->write_reg_multiple(adapter, write_addr,
  582. (u8 *)bl_hdr, write_len);
  583. if (status < 0) {
  584. rsi_dbg(ERR_ZONE,
  585. "%s: Failed to load Version/CRC structure\n",
  586. __func__);
  587. goto fail;
  588. }
  589. }
  590. status = 0;
  591. fail:
  592. kfree(bl_hdr);
  593. return status;
  594. }
  595. static u32 read_flash_capacity(struct rsi_hw *adapter)
  596. {
  597. u32 flash_sz = 0;
  598. if ((adapter->host_intf_ops->master_reg_read(adapter, FLASH_SIZE_ADDR,
  599. &flash_sz, 2)) < 0) {
  600. rsi_dbg(ERR_ZONE,
  601. "%s: Flash size reading failed..\n",
  602. __func__);
  603. return 0;
  604. }
  605. rsi_dbg(INIT_ZONE, "Flash capacity: %d KiloBytes\n", flash_sz);
  606. return (flash_sz * 1024); /* Return size in kbytes */
  607. }
  608. static int ping_pong_write(struct rsi_hw *adapter, u8 cmd, u8 *addr, u32 size)
  609. {
  610. struct rsi_host_intf_ops *hif_ops = adapter->host_intf_ops;
  611. u32 block_size = adapter->block_size;
  612. u32 cmd_addr;
  613. u16 cmd_resp, cmd_req;
  614. u8 *str;
  615. int status;
  616. if (cmd == PING_WRITE) {
  617. cmd_addr = PING_BUFFER_ADDRESS;
  618. cmd_resp = PONG_AVAIL;
  619. cmd_req = PING_VALID;
  620. str = "PING_VALID";
  621. } else {
  622. cmd_addr = PONG_BUFFER_ADDRESS;
  623. cmd_resp = PING_AVAIL;
  624. cmd_req = PONG_VALID;
  625. str = "PONG_VALID";
  626. }
  627. status = hif_ops->load_data_master_write(adapter, cmd_addr, size,
  628. block_size, addr);
  629. if (status) {
  630. rsi_dbg(ERR_ZONE, "%s: Unable to write blk at addr %0x\n",
  631. __func__, *addr);
  632. return status;
  633. }
  634. status = bl_cmd(adapter, cmd_req, cmd_resp, str);
  635. if (status)
  636. return status;
  637. return 0;
  638. }
  639. static int auto_fw_upgrade(struct rsi_hw *adapter, u8 *flash_content,
  640. u32 content_size)
  641. {
  642. u8 cmd;
  643. u32 temp_content_size, num_flash, index;
  644. u32 flash_start_address;
  645. int status;
  646. if (content_size > MAX_FLASH_FILE_SIZE) {
  647. rsi_dbg(ERR_ZONE,
  648. "%s: Flash Content size is more than 400K %u\n",
  649. __func__, MAX_FLASH_FILE_SIZE);
  650. return -EINVAL;
  651. }
  652. flash_start_address = *(u32 *)&flash_content[FLASH_START_ADDRESS];
  653. rsi_dbg(INFO_ZONE, "flash start address: %08x\n", flash_start_address);
  654. if (flash_start_address < FW_IMAGE_MIN_ADDRESS) {
  655. rsi_dbg(ERR_ZONE,
  656. "%s: Fw image Flash Start Address is less than 64K\n",
  657. __func__);
  658. return -EINVAL;
  659. }
  660. if (flash_start_address % FLASH_SECTOR_SIZE) {
  661. rsi_dbg(ERR_ZONE,
  662. "%s: Flash Start Address is not multiple of 4K\n",
  663. __func__);
  664. return -EINVAL;
  665. }
  666. if ((flash_start_address + content_size) > adapter->flash_capacity) {
  667. rsi_dbg(ERR_ZONE,
  668. "%s: Flash Content will cross max flash size\n",
  669. __func__);
  670. return -EINVAL;
  671. }
  672. temp_content_size = content_size;
  673. num_flash = content_size / FLASH_WRITE_CHUNK_SIZE;
  674. rsi_dbg(INFO_ZONE, "content_size: %d, num_flash: %d\n",
  675. content_size, num_flash);
  676. for (index = 0; index <= num_flash; index++) {
  677. rsi_dbg(INFO_ZONE, "flash index: %d\n", index);
  678. if (index != num_flash) {
  679. content_size = FLASH_WRITE_CHUNK_SIZE;
  680. rsi_dbg(INFO_ZONE, "QSPI content_size:%d\n",
  681. content_size);
  682. } else {
  683. content_size =
  684. temp_content_size % FLASH_WRITE_CHUNK_SIZE;
  685. rsi_dbg(INFO_ZONE,
  686. "Writing last sector content_size:%d\n",
  687. content_size);
  688. if (!content_size) {
  689. rsi_dbg(INFO_ZONE, "instruction size zero\n");
  690. break;
  691. }
  692. }
  693. if (index % 2)
  694. cmd = PING_WRITE;
  695. else
  696. cmd = PONG_WRITE;
  697. status = ping_pong_write(adapter, cmd, flash_content,
  698. content_size);
  699. if (status) {
  700. rsi_dbg(ERR_ZONE, "%s: Unable to load %d block\n",
  701. __func__, index);
  702. return status;
  703. }
  704. rsi_dbg(INFO_ZONE,
  705. "%s: Successfully loaded %d instructions\n",
  706. __func__, index);
  707. flash_content += content_size;
  708. }
  709. status = bl_cmd(adapter, EOF_REACHED, FW_LOADING_SUCCESSFUL,
  710. "EOF_REACHED");
  711. if (status)
  712. return status;
  713. rsi_dbg(INFO_ZONE, "FW loading is done and FW is running..\n");
  714. return 0;
  715. }
  716. static int rsi_load_firmware(struct rsi_hw *adapter)
  717. {
  718. struct rsi_common *common = adapter->priv;
  719. struct rsi_host_intf_ops *hif_ops = adapter->host_intf_ops;
  720. const struct firmware *fw_entry = NULL;
  721. u32 regout_val = 0, content_size;
  722. u16 tmp_regout_val = 0;
  723. struct ta_metadata *metadata_p;
  724. int status;
  725. bl_start_cmd_timer(adapter, BL_CMD_TIMEOUT);
  726. while (!adapter->blcmd_timer_expired) {
  727. status = hif_ops->master_reg_read(adapter, SWBL_REGOUT,
  728. &regout_val, 2);
  729. if (status < 0) {
  730. bl_stop_cmd_timer(adapter);
  731. rsi_dbg(ERR_ZONE,
  732. "%s: REGOUT read failed\n", __func__);
  733. return status;
  734. }
  735. mdelay(1);
  736. if ((regout_val >> 8) == REGOUT_VALID)
  737. break;
  738. }
  739. if (adapter->blcmd_timer_expired) {
  740. rsi_dbg(ERR_ZONE, "%s: REGOUT read timedout\n", __func__);
  741. rsi_dbg(ERR_ZONE,
  742. "%s: Soft boot loader not present\n", __func__);
  743. return -ETIMEDOUT;
  744. }
  745. bl_stop_cmd_timer(adapter);
  746. rsi_dbg(INFO_ZONE, "Received Board Version Number: %x\n",
  747. (regout_val & 0xff));
  748. status = hif_ops->master_reg_write(adapter, SWBL_REGOUT,
  749. (REGOUT_INVALID | REGOUT_INVALID << 8),
  750. 2);
  751. if (status < 0) {
  752. rsi_dbg(ERR_ZONE, "%s: REGOUT writing failed..\n", __func__);
  753. return status;
  754. }
  755. mdelay(1);
  756. status = bl_cmd(adapter, CONFIG_AUTO_READ_MODE, CMD_PASS,
  757. "AUTO_READ_CMD");
  758. if (status < 0)
  759. return status;
  760. adapter->flash_capacity = read_flash_capacity(adapter);
  761. if (adapter->flash_capacity <= 0) {
  762. rsi_dbg(ERR_ZONE,
  763. "%s: Unable to read flash size from EEPROM\n",
  764. __func__);
  765. return -EINVAL;
  766. }
  767. metadata_p = &metadata_flash_content[adapter->priv->coex_mode];
  768. rsi_dbg(INIT_ZONE, "%s: Loading file %s\n", __func__, metadata_p->name);
  769. adapter->fw_file_name = metadata_p->name;
  770. status = request_firmware(&fw_entry, metadata_p->name, adapter->device);
  771. if (status < 0) {
  772. rsi_dbg(ERR_ZONE, "%s: Failed to open file %s\n",
  773. __func__, metadata_p->name);
  774. return status;
  775. }
  776. content_size = fw_entry->size;
  777. rsi_dbg(INFO_ZONE, "FW Length = %d bytes\n", content_size);
  778. /* Get the firmware version */
  779. common->lmac_ver.ver.info.fw_ver[0] =
  780. fw_entry->data[LMAC_VER_OFFSET] & 0xFF;
  781. common->lmac_ver.ver.info.fw_ver[1] =
  782. fw_entry->data[LMAC_VER_OFFSET + 1] & 0xFF;
  783. common->lmac_ver.major = fw_entry->data[LMAC_VER_OFFSET + 2] & 0xFF;
  784. common->lmac_ver.release_num =
  785. fw_entry->data[LMAC_VER_OFFSET + 3] & 0xFF;
  786. common->lmac_ver.minor = fw_entry->data[LMAC_VER_OFFSET + 4] & 0xFF;
  787. common->lmac_ver.patch_num = 0;
  788. rsi_print_version(common);
  789. status = bl_write_header(adapter, (u8 *)fw_entry->data, content_size);
  790. if (status) {
  791. rsi_dbg(ERR_ZONE,
  792. "%s: RPS Image header loading failed\n",
  793. __func__);
  794. goto fail;
  795. }
  796. bl_start_cmd_timer(adapter, BL_CMD_TIMEOUT);
  797. status = bl_write_cmd(adapter, CHECK_CRC, CMD_PASS, &tmp_regout_val);
  798. if (status) {
  799. bl_stop_cmd_timer(adapter);
  800. rsi_dbg(ERR_ZONE,
  801. "%s: CHECK_CRC Command writing failed..\n",
  802. __func__);
  803. if ((tmp_regout_val & 0xff) == CMD_FAIL) {
  804. rsi_dbg(ERR_ZONE,
  805. "CRC Fail.. Proceeding to Upgrade mode\n");
  806. goto fw_upgrade;
  807. }
  808. }
  809. bl_stop_cmd_timer(adapter);
  810. status = bl_cmd(adapter, POLLING_MODE, CMD_PASS, "POLLING_MODE");
  811. if (status)
  812. goto fail;
  813. load_image_cmd:
  814. status = bl_cmd(adapter, LOAD_HOSTED_FW, LOADING_INITIATED,
  815. "LOAD_HOSTED_FW");
  816. if (status)
  817. goto fail;
  818. rsi_dbg(INFO_ZONE, "Load Image command passed..\n");
  819. goto success;
  820. fw_upgrade:
  821. status = bl_cmd(adapter, BURN_HOSTED_FW, SEND_RPS_FILE, "FW_UPGRADE");
  822. if (status)
  823. goto fail;
  824. rsi_dbg(INFO_ZONE, "Burn Command Pass.. Upgrading the firmware\n");
  825. status = auto_fw_upgrade(adapter, (u8 *)fw_entry->data, content_size);
  826. if (status == 0) {
  827. rsi_dbg(ERR_ZONE, "Firmware upgradation Done\n");
  828. goto load_image_cmd;
  829. }
  830. rsi_dbg(ERR_ZONE, "Firmware upgrade failed\n");
  831. status = bl_cmd(adapter, CONFIG_AUTO_READ_MODE, CMD_PASS,
  832. "AUTO_READ_MODE");
  833. if (status)
  834. goto fail;
  835. success:
  836. rsi_dbg(ERR_ZONE, "***** Firmware Loading successful *****\n");
  837. release_firmware(fw_entry);
  838. return 0;
  839. fail:
  840. rsi_dbg(ERR_ZONE, "##### Firmware loading failed #####\n");
  841. release_firmware(fw_entry);
  842. return status;
  843. }
  844. int rsi_hal_device_init(struct rsi_hw *adapter)
  845. {
  846. struct rsi_common *common = adapter->priv;
  847. switch (adapter->device_model) {
  848. case RSI_DEV_9113:
  849. if (rsi_load_firmware(adapter)) {
  850. rsi_dbg(ERR_ZONE,
  851. "%s: Failed to load TA instructions\n",
  852. __func__);
  853. return -EINVAL;
  854. }
  855. break;
  856. default:
  857. return -EINVAL;
  858. }
  859. common->fsm_state = FSM_CARD_NOT_READY;
  860. return 0;
  861. }
  862. EXPORT_SYMBOL_GPL(rsi_hal_device_init);