htc_hst.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 = (struct htc_frame_hdr *)
  25. skb_push(skb, sizeof(struct htc_frame_hdr));
  26. hdr->endpoint_id = epid;
  27. hdr->flags = flags;
  28. hdr->payload_len = cpu_to_be16(len);
  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 = (struct 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. service_id = be16_to_cpu(svc_rspmsg->service_id);
  101. max_msglen = be16_to_cpu(svc_rspmsg->max_msg_len);
  102. endpoint = &target->endpoint[epid];
  103. for (tepid = (ENDPOINT_MAX - 1); tepid > ENDPOINT0; tepid--) {
  104. tmp_endpoint = &target->endpoint[tepid];
  105. if (tmp_endpoint->service_id == service_id) {
  106. tmp_endpoint->service_id = 0;
  107. break;
  108. }
  109. }
  110. if (tepid == ENDPOINT0)
  111. return;
  112. endpoint->service_id = service_id;
  113. endpoint->max_txqdepth = tmp_endpoint->max_txqdepth;
  114. endpoint->ep_callbacks = tmp_endpoint->ep_callbacks;
  115. endpoint->ul_pipeid = tmp_endpoint->ul_pipeid;
  116. endpoint->dl_pipeid = tmp_endpoint->dl_pipeid;
  117. endpoint->max_msglen = max_msglen;
  118. target->conn_rsp_epid = epid;
  119. complete(&target->cmd_wait);
  120. } else {
  121. target->conn_rsp_epid = ENDPOINT_UNUSED;
  122. }
  123. }
  124. static int htc_config_pipe_credits(struct htc_target *target)
  125. {
  126. struct sk_buff *skb;
  127. struct htc_config_pipe_msg *cp_msg;
  128. int ret;
  129. unsigned long time_left;
  130. skb = alloc_skb(50 + sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  131. if (!skb) {
  132. dev_err(target->dev, "failed to allocate send buffer\n");
  133. return -ENOMEM;
  134. }
  135. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  136. cp_msg = (struct htc_config_pipe_msg *)
  137. skb_put(skb, sizeof(struct htc_config_pipe_msg));
  138. cp_msg->message_id = cpu_to_be16(HTC_MSG_CONFIG_PIPE_ID);
  139. cp_msg->pipe_id = USB_WLAN_TX_PIPE;
  140. cp_msg->credits = target->credits;
  141. target->htc_flags |= HTC_OP_CONFIG_PIPE_CREDITS;
  142. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0);
  143. if (ret)
  144. goto err;
  145. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  146. if (!time_left) {
  147. dev_err(target->dev, "HTC credit config timeout\n");
  148. return -ETIMEDOUT;
  149. }
  150. return 0;
  151. err:
  152. kfree_skb(skb);
  153. return -EINVAL;
  154. }
  155. static int htc_setup_complete(struct htc_target *target)
  156. {
  157. struct sk_buff *skb;
  158. struct htc_comp_msg *comp_msg;
  159. int ret = 0;
  160. unsigned long time_left;
  161. skb = alloc_skb(50 + sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  162. if (!skb) {
  163. dev_err(target->dev, "failed to allocate send buffer\n");
  164. return -ENOMEM;
  165. }
  166. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  167. comp_msg = (struct htc_comp_msg *)
  168. skb_put(skb, sizeof(struct htc_comp_msg));
  169. comp_msg->msg_id = cpu_to_be16(HTC_MSG_SETUP_COMPLETE_ID);
  170. target->htc_flags |= HTC_OP_START_WAIT;
  171. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0);
  172. if (ret)
  173. goto err;
  174. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  175. if (!time_left) {
  176. dev_err(target->dev, "HTC start timeout\n");
  177. return -ETIMEDOUT;
  178. }
  179. return 0;
  180. err:
  181. kfree_skb(skb);
  182. return -EINVAL;
  183. }
  184. /* HTC APIs */
  185. int htc_init(struct htc_target *target)
  186. {
  187. int ret;
  188. ret = htc_config_pipe_credits(target);
  189. if (ret)
  190. return ret;
  191. return htc_setup_complete(target);
  192. }
  193. int htc_connect_service(struct htc_target *target,
  194. struct htc_service_connreq *service_connreq,
  195. enum htc_endpoint_id *conn_rsp_epid)
  196. {
  197. struct sk_buff *skb;
  198. struct htc_endpoint *endpoint;
  199. struct htc_conn_svc_msg *conn_msg;
  200. int ret;
  201. unsigned long time_left;
  202. /* Find an available endpoint */
  203. endpoint = get_next_avail_ep(target->endpoint);
  204. if (!endpoint) {
  205. dev_err(target->dev, "Endpoint is not available for"
  206. "service %d\n", service_connreq->service_id);
  207. return -EINVAL;
  208. }
  209. endpoint->service_id = service_connreq->service_id;
  210. endpoint->max_txqdepth = service_connreq->max_send_qdepth;
  211. endpoint->ul_pipeid = service_to_ulpipe(service_connreq->service_id);
  212. endpoint->dl_pipeid = service_to_dlpipe(service_connreq->service_id);
  213. endpoint->ep_callbacks = service_connreq->ep_callbacks;
  214. skb = alloc_skb(sizeof(struct htc_conn_svc_msg) +
  215. sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  216. if (!skb) {
  217. dev_err(target->dev, "Failed to allocate buf to send"
  218. "service connect req\n");
  219. return -ENOMEM;
  220. }
  221. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  222. conn_msg = (struct htc_conn_svc_msg *)
  223. skb_put(skb, sizeof(struct htc_conn_svc_msg));
  224. conn_msg->service_id = cpu_to_be16(service_connreq->service_id);
  225. conn_msg->msg_id = cpu_to_be16(HTC_MSG_CONNECT_SERVICE_ID);
  226. conn_msg->con_flags = cpu_to_be16(service_connreq->con_flags);
  227. conn_msg->dl_pipeid = endpoint->dl_pipeid;
  228. conn_msg->ul_pipeid = endpoint->ul_pipeid;
  229. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0);
  230. if (ret)
  231. goto err;
  232. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  233. if (!time_left) {
  234. dev_err(target->dev, "Service connection timeout for: %d\n",
  235. service_connreq->service_id);
  236. return -ETIMEDOUT;
  237. }
  238. *conn_rsp_epid = target->conn_rsp_epid;
  239. return 0;
  240. err:
  241. kfree_skb(skb);
  242. return ret;
  243. }
  244. int htc_send(struct htc_target *target, struct sk_buff *skb)
  245. {
  246. struct ath9k_htc_tx_ctl *tx_ctl;
  247. tx_ctl = HTC_SKB_CB(skb);
  248. return htc_issue_send(target, skb, skb->len, 0, tx_ctl->epid);
  249. }
  250. int htc_send_epid(struct htc_target *target, struct sk_buff *skb,
  251. enum htc_endpoint_id epid)
  252. {
  253. return htc_issue_send(target, skb, skb->len, 0, epid);
  254. }
  255. void htc_stop(struct htc_target *target)
  256. {
  257. target->hif->stop(target->hif_dev);
  258. }
  259. void htc_start(struct htc_target *target)
  260. {
  261. target->hif->start(target->hif_dev);
  262. }
  263. void htc_sta_drain(struct htc_target *target, u8 idx)
  264. {
  265. target->hif->sta_drain(target->hif_dev, idx);
  266. }
  267. void ath9k_htc_txcompletion_cb(struct htc_target *htc_handle,
  268. struct sk_buff *skb, bool txok)
  269. {
  270. struct htc_endpoint *endpoint;
  271. struct htc_frame_hdr *htc_hdr = NULL;
  272. if (htc_handle->htc_flags & HTC_OP_CONFIG_PIPE_CREDITS) {
  273. complete(&htc_handle->cmd_wait);
  274. htc_handle->htc_flags &= ~HTC_OP_CONFIG_PIPE_CREDITS;
  275. goto ret;
  276. }
  277. if (htc_handle->htc_flags & HTC_OP_START_WAIT) {
  278. complete(&htc_handle->cmd_wait);
  279. htc_handle->htc_flags &= ~HTC_OP_START_WAIT;
  280. goto ret;
  281. }
  282. if (skb) {
  283. htc_hdr = (struct htc_frame_hdr *) skb->data;
  284. endpoint = &htc_handle->endpoint[htc_hdr->endpoint_id];
  285. skb_pull(skb, sizeof(struct htc_frame_hdr));
  286. if (endpoint->ep_callbacks.tx) {
  287. endpoint->ep_callbacks.tx(endpoint->ep_callbacks.priv,
  288. skb, htc_hdr->endpoint_id,
  289. txok);
  290. } else {
  291. kfree_skb(skb);
  292. }
  293. }
  294. return;
  295. ret:
  296. kfree_skb(skb);
  297. }
  298. static void ath9k_htc_fw_panic_report(struct htc_target *htc_handle,
  299. struct sk_buff *skb)
  300. {
  301. uint32_t *pattern = (uint32_t *)skb->data;
  302. switch (*pattern) {
  303. case 0x33221199:
  304. {
  305. struct htc_panic_bad_vaddr *htc_panic;
  306. htc_panic = (struct htc_panic_bad_vaddr *) skb->data;
  307. dev_err(htc_handle->dev, "ath: firmware panic! "
  308. "exccause: 0x%08x; pc: 0x%08x; badvaddr: 0x%08x.\n",
  309. htc_panic->exccause, htc_panic->pc,
  310. htc_panic->badvaddr);
  311. break;
  312. }
  313. case 0x33221299:
  314. {
  315. struct htc_panic_bad_epid *htc_panic;
  316. htc_panic = (struct htc_panic_bad_epid *) skb->data;
  317. dev_err(htc_handle->dev, "ath: firmware panic! "
  318. "bad epid: 0x%08x\n", htc_panic->epid);
  319. break;
  320. }
  321. default:
  322. dev_err(htc_handle->dev, "ath: uknown panic pattern!\n");
  323. break;
  324. }
  325. }
  326. /*
  327. * HTC Messages are handled directly here and the obtained SKB
  328. * is freed.
  329. *
  330. * Service messages (Data, WMI) passed to the corresponding
  331. * endpoint RX handlers, which have to free the SKB.
  332. */
  333. void ath9k_htc_rx_msg(struct htc_target *htc_handle,
  334. struct sk_buff *skb, u32 len, u8 pipe_id)
  335. {
  336. struct htc_frame_hdr *htc_hdr;
  337. enum htc_endpoint_id epid;
  338. struct htc_endpoint *endpoint;
  339. __be16 *msg_id;
  340. if (!htc_handle || !skb)
  341. return;
  342. htc_hdr = (struct htc_frame_hdr *) skb->data;
  343. epid = htc_hdr->endpoint_id;
  344. if (epid == 0x99) {
  345. ath9k_htc_fw_panic_report(htc_handle, skb);
  346. kfree_skb(skb);
  347. return;
  348. }
  349. if (epid < 0 || epid >= ENDPOINT_MAX) {
  350. if (pipe_id != USB_REG_IN_PIPE)
  351. dev_kfree_skb_any(skb);
  352. else
  353. kfree_skb(skb);
  354. return;
  355. }
  356. if (epid == ENDPOINT0) {
  357. /* Handle trailer */
  358. if (htc_hdr->flags & HTC_FLAGS_RECV_TRAILER) {
  359. if (be32_to_cpu(*(__be32 *) skb->data) == 0x00C60000)
  360. /* Move past the Watchdog pattern */
  361. htc_hdr = (struct htc_frame_hdr *)(skb->data + 4);
  362. }
  363. /* Get the message ID */
  364. msg_id = (__be16 *) ((void *) htc_hdr +
  365. sizeof(struct htc_frame_hdr));
  366. /* Now process HTC messages */
  367. switch (be16_to_cpu(*msg_id)) {
  368. case HTC_MSG_READY_ID:
  369. htc_process_target_rdy(htc_handle, htc_hdr);
  370. break;
  371. case HTC_MSG_CONNECT_SERVICE_RESPONSE_ID:
  372. htc_process_conn_rsp(htc_handle, htc_hdr);
  373. break;
  374. default:
  375. break;
  376. }
  377. kfree_skb(skb);
  378. } else {
  379. if (htc_hdr->flags & HTC_FLAGS_RECV_TRAILER)
  380. skb_trim(skb, len - htc_hdr->control[0]);
  381. skb_pull(skb, sizeof(struct htc_frame_hdr));
  382. endpoint = &htc_handle->endpoint[epid];
  383. if (endpoint->ep_callbacks.rx)
  384. endpoint->ep_callbacks.rx(endpoint->ep_callbacks.priv,
  385. skb, epid);
  386. }
  387. }
  388. struct htc_target *ath9k_htc_hw_alloc(void *hif_handle,
  389. struct ath9k_htc_hif *hif,
  390. struct device *dev)
  391. {
  392. struct htc_endpoint *endpoint;
  393. struct htc_target *target;
  394. target = kzalloc(sizeof(struct htc_target), GFP_KERNEL);
  395. if (!target)
  396. return NULL;
  397. init_completion(&target->target_wait);
  398. init_completion(&target->cmd_wait);
  399. target->hif = hif;
  400. target->hif_dev = hif_handle;
  401. target->dev = dev;
  402. /* Assign control endpoint pipe IDs */
  403. endpoint = &target->endpoint[ENDPOINT0];
  404. endpoint->ul_pipeid = hif->control_ul_pipe;
  405. endpoint->dl_pipeid = hif->control_dl_pipe;
  406. atomic_set(&target->tgt_ready, 0);
  407. return target;
  408. }
  409. void ath9k_htc_hw_free(struct htc_target *htc)
  410. {
  411. kfree(htc);
  412. }
  413. int ath9k_htc_hw_init(struct htc_target *target,
  414. struct device *dev, u16 devid,
  415. char *product, u32 drv_info)
  416. {
  417. if (ath9k_htc_probe_device(target, dev, devid, product, drv_info)) {
  418. pr_err("Failed to initialize the device\n");
  419. return -ENODEV;
  420. }
  421. return 0;
  422. }
  423. void ath9k_htc_hw_deinit(struct htc_target *target, bool hot_unplug)
  424. {
  425. if (target)
  426. ath9k_htc_disconnect_device(target, hot_unplug);
  427. }