htc_hst.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Copyright (c) 2010-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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include "htc.h"
  18. static int htc_issue_send(struct htc_target *target, struct sk_buff* skb,
  19. u16 len, u8 flags, u8 epid)
  20. {
  21. struct htc_frame_hdr *hdr;
  22. struct htc_endpoint *endpoint = &target->endpoint[epid];
  23. int status;
  24. hdr = skb_push(skb, sizeof(struct htc_frame_hdr));
  25. hdr->endpoint_id = epid;
  26. hdr->flags = flags;
  27. hdr->payload_len = cpu_to_be16(len);
  28. memset(hdr->control, 0, sizeof(hdr->control));
  29. status = target->hif->send(target->hif_dev, endpoint->ul_pipeid, skb);
  30. return status;
  31. }
  32. static struct htc_endpoint *get_next_avail_ep(struct htc_endpoint *endpoint)
  33. {
  34. enum htc_endpoint_id avail_epid;
  35. for (avail_epid = (ENDPOINT_MAX - 1); avail_epid > ENDPOINT0; avail_epid--)
  36. if (endpoint[avail_epid].service_id == 0)
  37. return &endpoint[avail_epid];
  38. return NULL;
  39. }
  40. static u8 service_to_ulpipe(u16 service_id)
  41. {
  42. switch (service_id) {
  43. case WMI_CONTROL_SVC:
  44. return 4;
  45. case WMI_BEACON_SVC:
  46. case WMI_CAB_SVC:
  47. case WMI_UAPSD_SVC:
  48. case WMI_MGMT_SVC:
  49. case WMI_DATA_VO_SVC:
  50. case WMI_DATA_VI_SVC:
  51. case WMI_DATA_BE_SVC:
  52. case WMI_DATA_BK_SVC:
  53. return 1;
  54. default:
  55. return 0;
  56. }
  57. }
  58. static u8 service_to_dlpipe(u16 service_id)
  59. {
  60. switch (service_id) {
  61. case WMI_CONTROL_SVC:
  62. return 3;
  63. case WMI_BEACON_SVC:
  64. case WMI_CAB_SVC:
  65. case WMI_UAPSD_SVC:
  66. case WMI_MGMT_SVC:
  67. case WMI_DATA_VO_SVC:
  68. case WMI_DATA_VI_SVC:
  69. case WMI_DATA_BE_SVC:
  70. case WMI_DATA_BK_SVC:
  71. return 2;
  72. default:
  73. return 0;
  74. }
  75. }
  76. static void htc_process_target_rdy(struct htc_target *target,
  77. void *buf)
  78. {
  79. struct htc_endpoint *endpoint;
  80. struct htc_ready_msg *htc_ready_msg = buf;
  81. target->credit_size = be16_to_cpu(htc_ready_msg->credit_size);
  82. endpoint = &target->endpoint[ENDPOINT0];
  83. endpoint->service_id = HTC_CTRL_RSVD_SVC;
  84. endpoint->max_msglen = HTC_MAX_CONTROL_MESSAGE_LENGTH;
  85. atomic_inc(&target->tgt_ready);
  86. complete(&target->target_wait);
  87. }
  88. static void htc_process_conn_rsp(struct htc_target *target,
  89. struct htc_frame_hdr *htc_hdr)
  90. {
  91. struct htc_conn_svc_rspmsg *svc_rspmsg;
  92. struct htc_endpoint *endpoint, *tmp_endpoint = NULL;
  93. u16 service_id;
  94. u16 max_msglen;
  95. enum htc_endpoint_id epid, tepid;
  96. svc_rspmsg = (struct htc_conn_svc_rspmsg *)
  97. ((void *) htc_hdr + sizeof(struct htc_frame_hdr));
  98. if (svc_rspmsg->status == HTC_SERVICE_SUCCESS) {
  99. epid = svc_rspmsg->endpoint_id;
  100. /* Check that the received epid for the endpoint to attach
  101. * a new service is valid. ENDPOINT0 can't be used here as it
  102. * is already reserved for HTC_CTRL_RSVD_SVC service and thus
  103. * should not be modified.
  104. */
  105. if (epid <= ENDPOINT0 || epid >= ENDPOINT_MAX)
  106. return;
  107. service_id = be16_to_cpu(svc_rspmsg->service_id);
  108. max_msglen = be16_to_cpu(svc_rspmsg->max_msg_len);
  109. endpoint = &target->endpoint[epid];
  110. for (tepid = (ENDPOINT_MAX - 1); tepid > ENDPOINT0; tepid--) {
  111. tmp_endpoint = &target->endpoint[tepid];
  112. if (tmp_endpoint->service_id == service_id) {
  113. tmp_endpoint->service_id = 0;
  114. break;
  115. }
  116. }
  117. if (tepid == ENDPOINT0)
  118. return;
  119. endpoint->service_id = service_id;
  120. endpoint->max_txqdepth = tmp_endpoint->max_txqdepth;
  121. endpoint->ep_callbacks = tmp_endpoint->ep_callbacks;
  122. endpoint->ul_pipeid = tmp_endpoint->ul_pipeid;
  123. endpoint->dl_pipeid = tmp_endpoint->dl_pipeid;
  124. endpoint->max_msglen = max_msglen;
  125. target->conn_rsp_epid = epid;
  126. complete(&target->cmd_wait);
  127. } else {
  128. target->conn_rsp_epid = ENDPOINT_UNUSED;
  129. }
  130. }
  131. static int htc_config_pipe_credits(struct htc_target *target)
  132. {
  133. struct sk_buff *skb;
  134. struct htc_config_pipe_msg *cp_msg;
  135. int ret;
  136. unsigned long time_left;
  137. skb = alloc_skb(50 + sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  138. if (!skb) {
  139. dev_err(target->dev, "failed to allocate send buffer\n");
  140. return -ENOMEM;
  141. }
  142. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  143. cp_msg = skb_put(skb, sizeof(struct htc_config_pipe_msg));
  144. cp_msg->message_id = cpu_to_be16(HTC_MSG_CONFIG_PIPE_ID);
  145. cp_msg->pipe_id = USB_WLAN_TX_PIPE;
  146. cp_msg->credits = target->credits;
  147. target->htc_flags |= HTC_OP_CONFIG_PIPE_CREDITS;
  148. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0);
  149. if (ret)
  150. goto err;
  151. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  152. if (!time_left) {
  153. dev_err(target->dev, "HTC credit config timeout\n");
  154. return -ETIMEDOUT;
  155. }
  156. return 0;
  157. err:
  158. kfree_skb(skb);
  159. return -EINVAL;
  160. }
  161. static int htc_setup_complete(struct htc_target *target)
  162. {
  163. struct sk_buff *skb;
  164. struct htc_comp_msg *comp_msg;
  165. int ret = 0;
  166. unsigned long time_left;
  167. skb = alloc_skb(50 + sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  168. if (!skb) {
  169. dev_err(target->dev, "failed to allocate send buffer\n");
  170. return -ENOMEM;
  171. }
  172. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  173. comp_msg = skb_put(skb, sizeof(struct htc_comp_msg));
  174. comp_msg->msg_id = cpu_to_be16(HTC_MSG_SETUP_COMPLETE_ID);
  175. target->htc_flags |= HTC_OP_START_WAIT;
  176. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0);
  177. if (ret)
  178. goto err;
  179. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  180. if (!time_left) {
  181. dev_err(target->dev, "HTC start timeout\n");
  182. return -ETIMEDOUT;
  183. }
  184. return 0;
  185. err:
  186. kfree_skb(skb);
  187. return -EINVAL;
  188. }
  189. /* HTC APIs */
  190. int htc_init(struct htc_target *target)
  191. {
  192. int ret;
  193. ret = htc_config_pipe_credits(target);
  194. if (ret)
  195. return ret;
  196. return htc_setup_complete(target);
  197. }
  198. int htc_connect_service(struct htc_target *target,
  199. struct htc_service_connreq *service_connreq,
  200. enum htc_endpoint_id *conn_rsp_epid)
  201. {
  202. struct sk_buff *skb;
  203. struct htc_endpoint *endpoint;
  204. struct htc_conn_svc_msg *conn_msg;
  205. int ret;
  206. unsigned long time_left;
  207. /* Find an available endpoint */
  208. endpoint = get_next_avail_ep(target->endpoint);
  209. if (!endpoint) {
  210. dev_err(target->dev, "Endpoint is not available for service %d\n",
  211. service_connreq->service_id);
  212. return -EINVAL;
  213. }
  214. endpoint->service_id = service_connreq->service_id;
  215. endpoint->max_txqdepth = service_connreq->max_send_qdepth;
  216. endpoint->ul_pipeid = service_to_ulpipe(service_connreq->service_id);
  217. endpoint->dl_pipeid = service_to_dlpipe(service_connreq->service_id);
  218. endpoint->ep_callbacks = service_connreq->ep_callbacks;
  219. skb = alloc_skb(sizeof(struct htc_conn_svc_msg) +
  220. sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  221. if (!skb) {
  222. dev_err(target->dev, "Failed to allocate buf to send"
  223. "service connect req\n");
  224. return -ENOMEM;
  225. }
  226. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  227. conn_msg = skb_put(skb, sizeof(struct htc_conn_svc_msg));
  228. conn_msg->service_id = cpu_to_be16(service_connreq->service_id);
  229. conn_msg->msg_id = cpu_to_be16(HTC_MSG_CONNECT_SERVICE_ID);
  230. conn_msg->con_flags = cpu_to_be16(service_connreq->con_flags);
  231. conn_msg->dl_pipeid = endpoint->dl_pipeid;
  232. conn_msg->ul_pipeid = endpoint->ul_pipeid;
  233. /* To prevent infoleak */
  234. conn_msg->svc_meta_len = 0;
  235. conn_msg->pad = 0;
  236. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0);
  237. if (ret)
  238. goto err;
  239. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  240. if (!time_left) {
  241. dev_err(target->dev, "Service connection timeout for: %d\n",
  242. service_connreq->service_id);
  243. return -ETIMEDOUT;
  244. }
  245. if (target->conn_rsp_epid < 0 || target->conn_rsp_epid >= ENDPOINT_MAX)
  246. return -EINVAL;
  247. *conn_rsp_epid = target->conn_rsp_epid;
  248. return 0;
  249. err:
  250. kfree_skb(skb);
  251. return ret;
  252. }
  253. int htc_send(struct htc_target *target, struct sk_buff *skb)
  254. {
  255. struct ath9k_htc_tx_ctl *tx_ctl;
  256. tx_ctl = HTC_SKB_CB(skb);
  257. return htc_issue_send(target, skb, skb->len, 0, tx_ctl->epid);
  258. }
  259. int htc_send_epid(struct htc_target *target, struct sk_buff *skb,
  260. enum htc_endpoint_id epid)
  261. {
  262. return htc_issue_send(target, skb, skb->len, 0, epid);
  263. }
  264. void htc_stop(struct htc_target *target)
  265. {
  266. target->hif->stop(target->hif_dev);
  267. }
  268. void htc_start(struct htc_target *target)
  269. {
  270. target->hif->start(target->hif_dev);
  271. }
  272. void htc_sta_drain(struct htc_target *target, u8 idx)
  273. {
  274. target->hif->sta_drain(target->hif_dev, idx);
  275. }
  276. void ath9k_htc_txcompletion_cb(struct htc_target *htc_handle,
  277. struct sk_buff *skb, bool txok)
  278. {
  279. struct htc_endpoint *endpoint;
  280. struct htc_frame_hdr *htc_hdr = NULL;
  281. if (htc_handle->htc_flags & HTC_OP_CONFIG_PIPE_CREDITS) {
  282. complete(&htc_handle->cmd_wait);
  283. htc_handle->htc_flags &= ~HTC_OP_CONFIG_PIPE_CREDITS;
  284. goto ret;
  285. }
  286. if (htc_handle->htc_flags & HTC_OP_START_WAIT) {
  287. complete(&htc_handle->cmd_wait);
  288. htc_handle->htc_flags &= ~HTC_OP_START_WAIT;
  289. goto ret;
  290. }
  291. if (skb) {
  292. htc_hdr = (struct htc_frame_hdr *) skb->data;
  293. if (htc_hdr->endpoint_id >= ARRAY_SIZE(htc_handle->endpoint))
  294. goto ret;
  295. endpoint = &htc_handle->endpoint[htc_hdr->endpoint_id];
  296. skb_pull(skb, sizeof(struct htc_frame_hdr));
  297. if (endpoint->ep_callbacks.tx) {
  298. endpoint->ep_callbacks.tx(endpoint->ep_callbacks.priv,
  299. skb, htc_hdr->endpoint_id,
  300. txok);
  301. } else {
  302. kfree_skb(skb);
  303. }
  304. }
  305. return;
  306. ret:
  307. kfree_skb(skb);
  308. }
  309. static void ath9k_htc_fw_panic_report(struct htc_target *htc_handle,
  310. struct sk_buff *skb, u32 len)
  311. {
  312. uint32_t *pattern = (uint32_t *)skb->data;
  313. if (*pattern == 0x33221199 && len >= sizeof(struct htc_panic_bad_vaddr)) {
  314. struct htc_panic_bad_vaddr *htc_panic;
  315. htc_panic = (struct htc_panic_bad_vaddr *) skb->data;
  316. dev_err(htc_handle->dev, "ath: firmware panic! "
  317. "exccause: 0x%08x; pc: 0x%08x; badvaddr: 0x%08x.\n",
  318. htc_panic->exccause, htc_panic->pc,
  319. htc_panic->badvaddr);
  320. return;
  321. }
  322. if (*pattern == 0x33221299) {
  323. struct htc_panic_bad_epid *htc_panic;
  324. htc_panic = (struct htc_panic_bad_epid *) skb->data;
  325. dev_err(htc_handle->dev, "ath: firmware panic! "
  326. "bad epid: 0x%08x\n", htc_panic->epid);
  327. return;
  328. }
  329. dev_err(htc_handle->dev, "ath: unknown panic pattern!\n");
  330. }
  331. /*
  332. * HTC Messages are handled directly here and the obtained SKB
  333. * is freed.
  334. *
  335. * Service messages (Data, WMI) are passed to the corresponding
  336. * endpoint RX handlers, which have to free the SKB.
  337. */
  338. void ath9k_htc_rx_msg(struct htc_target *htc_handle,
  339. struct sk_buff *skb, u32 len, u8 pipe_id)
  340. {
  341. struct htc_frame_hdr *htc_hdr;
  342. enum htc_endpoint_id epid;
  343. struct htc_endpoint *endpoint;
  344. __be16 *msg_id;
  345. if (!htc_handle || !skb)
  346. return;
  347. /* A valid message requires len >= 8.
  348. *
  349. * sizeof(struct htc_frame_hdr) == 8
  350. * sizeof(struct htc_ready_msg) == 8
  351. * sizeof(struct htc_panic_bad_vaddr) == 16
  352. * sizeof(struct htc_panic_bad_epid) == 8
  353. */
  354. if (unlikely(len < sizeof(struct htc_frame_hdr)))
  355. goto invalid;
  356. htc_hdr = (struct htc_frame_hdr *) skb->data;
  357. epid = htc_hdr->endpoint_id;
  358. if (epid == 0x99) {
  359. ath9k_htc_fw_panic_report(htc_handle, skb, len);
  360. kfree_skb(skb);
  361. return;
  362. }
  363. if (epid < 0 || epid >= ENDPOINT_MAX) {
  364. invalid:
  365. if (pipe_id != USB_REG_IN_PIPE)
  366. dev_kfree_skb_any(skb);
  367. else
  368. kfree_skb(skb);
  369. return;
  370. }
  371. if (epid == ENDPOINT0) {
  372. /* Handle trailer */
  373. if (htc_hdr->flags & HTC_FLAGS_RECV_TRAILER) {
  374. if (be32_to_cpu(*(__be32 *) skb->data) == 0x00C60000) {
  375. /* Move past the Watchdog pattern */
  376. htc_hdr = (struct htc_frame_hdr *)(skb->data + 4);
  377. len -= 4;
  378. }
  379. }
  380. /* Get the message ID */
  381. if (unlikely(len < sizeof(struct htc_frame_hdr) + sizeof(__be16)))
  382. goto invalid;
  383. msg_id = (__be16 *) ((void *) htc_hdr +
  384. sizeof(struct htc_frame_hdr));
  385. /* Now process HTC messages */
  386. switch (be16_to_cpu(*msg_id)) {
  387. case HTC_MSG_READY_ID:
  388. if (unlikely(len < sizeof(struct htc_ready_msg)))
  389. goto invalid;
  390. htc_process_target_rdy(htc_handle, htc_hdr);
  391. break;
  392. case HTC_MSG_CONNECT_SERVICE_RESPONSE_ID:
  393. if (unlikely(len < sizeof(struct htc_frame_hdr) +
  394. sizeof(struct htc_conn_svc_rspmsg)))
  395. goto invalid;
  396. htc_process_conn_rsp(htc_handle, htc_hdr);
  397. break;
  398. default:
  399. break;
  400. }
  401. kfree_skb(skb);
  402. } else {
  403. if (htc_hdr->flags & HTC_FLAGS_RECV_TRAILER)
  404. skb_trim(skb, len - htc_hdr->control[0]);
  405. skb_pull(skb, sizeof(struct htc_frame_hdr));
  406. endpoint = &htc_handle->endpoint[epid];
  407. if (endpoint->ep_callbacks.rx)
  408. endpoint->ep_callbacks.rx(endpoint->ep_callbacks.priv,
  409. skb, epid);
  410. else
  411. goto invalid;
  412. }
  413. }
  414. struct htc_target *ath9k_htc_hw_alloc(void *hif_handle,
  415. struct ath9k_htc_hif *hif,
  416. struct device *dev)
  417. {
  418. struct htc_endpoint *endpoint;
  419. struct htc_target *target;
  420. target = kzalloc_obj(struct htc_target);
  421. if (!target)
  422. return NULL;
  423. init_completion(&target->target_wait);
  424. init_completion(&target->cmd_wait);
  425. target->hif = hif;
  426. target->hif_dev = hif_handle;
  427. target->dev = dev;
  428. /* Assign control endpoint pipe IDs */
  429. endpoint = &target->endpoint[ENDPOINT0];
  430. endpoint->ul_pipeid = hif->control_ul_pipe;
  431. endpoint->dl_pipeid = hif->control_dl_pipe;
  432. atomic_set(&target->tgt_ready, 0);
  433. return target;
  434. }
  435. void ath9k_htc_hw_free(struct htc_target *htc)
  436. {
  437. kfree(htc);
  438. }
  439. int ath9k_htc_hw_init(struct htc_target *target,
  440. struct device *dev, u16 devid,
  441. char *product, u32 drv_info)
  442. {
  443. if (ath9k_htc_probe_device(target, dev, devid, product, drv_info)) {
  444. pr_err("Failed to initialize the device\n");
  445. return -ENODEV;
  446. }
  447. return 0;
  448. }
  449. void ath9k_htc_hw_deinit(struct htc_target *target, bool hot_unplug)
  450. {
  451. if (target)
  452. ath9k_htc_disconnect_device(target, hot_unplug);
  453. }