bcdc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  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 ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /*******************************************************************************
  17. * Communicates with the dongle by using dcmd codes.
  18. * For certain dcmd codes, the dongle interprets string data from the host.
  19. ******************************************************************************/
  20. #include <linux/types.h>
  21. #include <linux/netdevice.h>
  22. #include <brcmu_utils.h>
  23. #include <brcmu_wifi.h>
  24. #include "core.h"
  25. #include "bus.h"
  26. #include "fwsignal.h"
  27. #include "debug.h"
  28. #include "tracepoint.h"
  29. #include "proto.h"
  30. #include "bcdc.h"
  31. struct brcmf_proto_bcdc_dcmd {
  32. __le32 cmd; /* dongle command value */
  33. __le32 len; /* lower 16: output buflen;
  34. * upper 16: input buflen (excludes header) */
  35. __le32 flags; /* flag defns given below */
  36. __le32 status; /* status code returned from the device */
  37. };
  38. /* BCDC flag definitions */
  39. #define BCDC_DCMD_ERROR 0x01 /* 1=cmd failed */
  40. #define BCDC_DCMD_SET 0x02 /* 0=get, 1=set cmd */
  41. #define BCDC_DCMD_IF_MASK 0xF000 /* I/F index */
  42. #define BCDC_DCMD_IF_SHIFT 12
  43. #define BCDC_DCMD_ID_MASK 0xFFFF0000 /* id an cmd pairing */
  44. #define BCDC_DCMD_ID_SHIFT 16 /* ID Mask shift bits */
  45. #define BCDC_DCMD_ID(flags) \
  46. (((flags) & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT)
  47. /*
  48. * BCDC header - Broadcom specific extension of CDC.
  49. * Used on data packets to convey priority across USB.
  50. */
  51. #define BCDC_HEADER_LEN 4
  52. #define BCDC_PROTO_VER 2 /* Protocol version */
  53. #define BCDC_FLAG_VER_MASK 0xf0 /* Protocol version mask */
  54. #define BCDC_FLAG_VER_SHIFT 4 /* Protocol version shift */
  55. #define BCDC_FLAG_SUM_GOOD 0x04 /* Good RX checksums */
  56. #define BCDC_FLAG_SUM_NEEDED 0x08 /* Dongle needs to do TX checksums */
  57. #define BCDC_PRIORITY_MASK 0x7
  58. #define BCDC_FLAG2_IF_MASK 0x0f /* packet rx interface in APSTA */
  59. #define BCDC_FLAG2_IF_SHIFT 0
  60. #define BCDC_GET_IF_IDX(hdr) \
  61. ((int)((((hdr)->flags2) & BCDC_FLAG2_IF_MASK) >> BCDC_FLAG2_IF_SHIFT))
  62. #define BCDC_SET_IF_IDX(hdr, idx) \
  63. ((hdr)->flags2 = (((hdr)->flags2 & ~BCDC_FLAG2_IF_MASK) | \
  64. ((idx) << BCDC_FLAG2_IF_SHIFT)))
  65. /**
  66. * struct brcmf_proto_bcdc_header - BCDC header format
  67. *
  68. * @flags: flags contain protocol and checksum info.
  69. * @priority: 802.1d priority and USB flow control info (bit 4:7).
  70. * @flags2: additional flags containing dongle interface index.
  71. * @data_offset: start of packet data. header is following by firmware signals.
  72. */
  73. struct brcmf_proto_bcdc_header {
  74. u8 flags;
  75. u8 priority;
  76. u8 flags2;
  77. u8 data_offset;
  78. };
  79. /*
  80. * maximum length of firmware signal data between
  81. * the BCDC header and packet data in the tx path.
  82. */
  83. #define BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES 12
  84. #define RETRIES 2 /* # of retries to retrieve matching dcmd response */
  85. #define BUS_HEADER_LEN (16+64) /* Must be atleast SDPCM_RESERVE
  86. * (amount of header tha might be added)
  87. * plus any space that might be needed
  88. * for bus alignment padding.
  89. */
  90. struct brcmf_bcdc {
  91. u16 reqid;
  92. u8 bus_header[BUS_HEADER_LEN];
  93. struct brcmf_proto_bcdc_dcmd msg;
  94. unsigned char buf[BRCMF_DCMD_MAXLEN];
  95. struct brcmf_fws_info *fws;
  96. };
  97. struct brcmf_fws_info *drvr_to_fws(struct brcmf_pub *drvr)
  98. {
  99. struct brcmf_bcdc *bcdc = drvr->proto->pd;
  100. return bcdc->fws;
  101. }
  102. static int
  103. brcmf_proto_bcdc_msg(struct brcmf_pub *drvr, int ifidx, uint cmd, void *buf,
  104. uint len, bool set)
  105. {
  106. struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
  107. struct brcmf_proto_bcdc_dcmd *msg = &bcdc->msg;
  108. u32 flags;
  109. brcmf_dbg(BCDC, "Enter\n");
  110. memset(msg, 0, sizeof(struct brcmf_proto_bcdc_dcmd));
  111. msg->cmd = cpu_to_le32(cmd);
  112. msg->len = cpu_to_le32(len);
  113. flags = (++bcdc->reqid << BCDC_DCMD_ID_SHIFT);
  114. if (set)
  115. flags |= BCDC_DCMD_SET;
  116. flags = (flags & ~BCDC_DCMD_IF_MASK) |
  117. (ifidx << BCDC_DCMD_IF_SHIFT);
  118. msg->flags = cpu_to_le32(flags);
  119. if (buf)
  120. memcpy(bcdc->buf, buf, len);
  121. len += sizeof(*msg);
  122. if (len > BRCMF_TX_IOCTL_MAX_MSG_SIZE)
  123. len = BRCMF_TX_IOCTL_MAX_MSG_SIZE;
  124. /* Send request */
  125. return brcmf_bus_txctl(drvr->bus_if, (unsigned char *)&bcdc->msg, len);
  126. }
  127. static int brcmf_proto_bcdc_cmplt(struct brcmf_pub *drvr, u32 id, u32 len)
  128. {
  129. int ret;
  130. struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
  131. brcmf_dbg(BCDC, "Enter\n");
  132. len += sizeof(struct brcmf_proto_bcdc_dcmd);
  133. do {
  134. ret = brcmf_bus_rxctl(drvr->bus_if, (unsigned char *)&bcdc->msg,
  135. len);
  136. if (ret < 0)
  137. break;
  138. } while (BCDC_DCMD_ID(le32_to_cpu(bcdc->msg.flags)) != id);
  139. return ret;
  140. }
  141. static int
  142. brcmf_proto_bcdc_query_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
  143. void *buf, uint len)
  144. {
  145. struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
  146. struct brcmf_proto_bcdc_dcmd *msg = &bcdc->msg;
  147. void *info;
  148. int ret = 0, retries = 0;
  149. u32 id, flags;
  150. brcmf_dbg(BCDC, "Enter, cmd %d len %d\n", cmd, len);
  151. ret = brcmf_proto_bcdc_msg(drvr, ifidx, cmd, buf, len, false);
  152. if (ret < 0) {
  153. brcmf_err("brcmf_proto_bcdc_msg failed w/status %d\n",
  154. ret);
  155. goto done;
  156. }
  157. retry:
  158. /* wait for interrupt and get first fragment */
  159. ret = brcmf_proto_bcdc_cmplt(drvr, bcdc->reqid, len);
  160. if (ret < 0)
  161. goto done;
  162. flags = le32_to_cpu(msg->flags);
  163. id = (flags & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT;
  164. if ((id < bcdc->reqid) && (++retries < RETRIES))
  165. goto retry;
  166. if (id != bcdc->reqid) {
  167. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  168. brcmf_ifname(brcmf_get_ifp(drvr, ifidx)), id,
  169. bcdc->reqid);
  170. ret = -EINVAL;
  171. goto done;
  172. }
  173. /* Check info buffer */
  174. info = (void *)&bcdc->buf[0];
  175. /* Copy info buffer */
  176. if (buf) {
  177. if (ret < (int)len)
  178. len = ret;
  179. memcpy(buf, info, len);
  180. }
  181. /* Check the ERROR flag */
  182. if (flags & BCDC_DCMD_ERROR)
  183. ret = le32_to_cpu(msg->status);
  184. done:
  185. return ret;
  186. }
  187. static int
  188. brcmf_proto_bcdc_set_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
  189. void *buf, uint len)
  190. {
  191. struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
  192. struct brcmf_proto_bcdc_dcmd *msg = &bcdc->msg;
  193. int ret = 0;
  194. u32 flags, id;
  195. brcmf_dbg(BCDC, "Enter, cmd %d len %d\n", cmd, len);
  196. ret = brcmf_proto_bcdc_msg(drvr, ifidx, cmd, buf, len, true);
  197. if (ret < 0)
  198. goto done;
  199. ret = brcmf_proto_bcdc_cmplt(drvr, bcdc->reqid, len);
  200. if (ret < 0)
  201. goto done;
  202. flags = le32_to_cpu(msg->flags);
  203. id = (flags & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT;
  204. if (id != bcdc->reqid) {
  205. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  206. brcmf_ifname(brcmf_get_ifp(drvr, ifidx)), id,
  207. bcdc->reqid);
  208. ret = -EINVAL;
  209. goto done;
  210. }
  211. /* Check the ERROR flag */
  212. if (flags & BCDC_DCMD_ERROR)
  213. ret = le32_to_cpu(msg->status);
  214. done:
  215. return ret;
  216. }
  217. static void
  218. brcmf_proto_bcdc_hdrpush(struct brcmf_pub *drvr, int ifidx, u8 offset,
  219. struct sk_buff *pktbuf)
  220. {
  221. struct brcmf_proto_bcdc_header *h;
  222. brcmf_dbg(BCDC, "Enter\n");
  223. /* Push BDC header used to convey priority for buses that don't */
  224. skb_push(pktbuf, BCDC_HEADER_LEN);
  225. h = (struct brcmf_proto_bcdc_header *)(pktbuf->data);
  226. h->flags = (BCDC_PROTO_VER << BCDC_FLAG_VER_SHIFT);
  227. if (pktbuf->ip_summed == CHECKSUM_PARTIAL)
  228. h->flags |= BCDC_FLAG_SUM_NEEDED;
  229. h->priority = (pktbuf->priority & BCDC_PRIORITY_MASK);
  230. h->flags2 = 0;
  231. h->data_offset = offset;
  232. BCDC_SET_IF_IDX(h, ifidx);
  233. trace_brcmf_bcdchdr(pktbuf->data);
  234. }
  235. static int
  236. brcmf_proto_bcdc_hdrpull(struct brcmf_pub *drvr, bool do_fws,
  237. struct sk_buff *pktbuf, struct brcmf_if **ifp)
  238. {
  239. struct brcmf_proto_bcdc_header *h;
  240. struct brcmf_if *tmp_if;
  241. brcmf_dbg(BCDC, "Enter\n");
  242. /* Pop BCDC header used to convey priority for buses that don't */
  243. if (pktbuf->len <= BCDC_HEADER_LEN) {
  244. brcmf_dbg(INFO, "rx data too short (%d <= %d)\n",
  245. pktbuf->len, BCDC_HEADER_LEN);
  246. return -EBADE;
  247. }
  248. trace_brcmf_bcdchdr(pktbuf->data);
  249. h = (struct brcmf_proto_bcdc_header *)(pktbuf->data);
  250. tmp_if = brcmf_get_ifp(drvr, BCDC_GET_IF_IDX(h));
  251. if (!tmp_if) {
  252. brcmf_dbg(INFO, "no matching ifp found\n");
  253. return -EBADE;
  254. }
  255. if (((h->flags & BCDC_FLAG_VER_MASK) >> BCDC_FLAG_VER_SHIFT) !=
  256. BCDC_PROTO_VER) {
  257. brcmf_err("%s: non-BCDC packet received, flags 0x%x\n",
  258. brcmf_ifname(tmp_if), h->flags);
  259. return -EBADE;
  260. }
  261. if (h->flags & BCDC_FLAG_SUM_GOOD) {
  262. brcmf_dbg(BCDC, "%s: BDC rcv, good checksum, flags 0x%x\n",
  263. brcmf_ifname(tmp_if), h->flags);
  264. pktbuf->ip_summed = CHECKSUM_UNNECESSARY;
  265. }
  266. pktbuf->priority = h->priority & BCDC_PRIORITY_MASK;
  267. skb_pull(pktbuf, BCDC_HEADER_LEN);
  268. if (do_fws)
  269. brcmf_fws_hdrpull(tmp_if, h->data_offset << 2, pktbuf);
  270. else
  271. skb_pull(pktbuf, h->data_offset << 2);
  272. if (pktbuf->len == 0)
  273. return -ENODATA;
  274. if (ifp != NULL)
  275. *ifp = tmp_if;
  276. return 0;
  277. }
  278. static int brcmf_proto_bcdc_tx_queue_data(struct brcmf_pub *drvr, int ifidx,
  279. struct sk_buff *skb)
  280. {
  281. struct brcmf_if *ifp = brcmf_get_ifp(drvr, ifidx);
  282. struct brcmf_bcdc *bcdc = drvr->proto->pd;
  283. if (!brcmf_fws_queue_skbs(bcdc->fws))
  284. return brcmf_proto_txdata(drvr, ifidx, 0, skb);
  285. return brcmf_fws_process_skb(ifp, skb);
  286. }
  287. static int
  288. brcmf_proto_bcdc_txdata(struct brcmf_pub *drvr, int ifidx, u8 offset,
  289. struct sk_buff *pktbuf)
  290. {
  291. brcmf_proto_bcdc_hdrpush(drvr, ifidx, offset, pktbuf);
  292. return brcmf_bus_txdata(drvr->bus_if, pktbuf);
  293. }
  294. void brcmf_proto_bcdc_txflowblock(struct device *dev, bool state)
  295. {
  296. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  297. struct brcmf_pub *drvr = bus_if->drvr;
  298. brcmf_dbg(TRACE, "Enter\n");
  299. brcmf_fws_bus_blocked(drvr, state);
  300. }
  301. void
  302. brcmf_proto_bcdc_txcomplete(struct device *dev, struct sk_buff *txp,
  303. bool success)
  304. {
  305. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  306. struct brcmf_bcdc *bcdc = bus_if->drvr->proto->pd;
  307. struct brcmf_if *ifp;
  308. /* await txstatus signal for firmware if active */
  309. if (brcmf_fws_fc_active(bcdc->fws)) {
  310. if (!success)
  311. brcmf_fws_bustxfail(bcdc->fws, txp);
  312. } else {
  313. if (brcmf_proto_bcdc_hdrpull(bus_if->drvr, false, txp, &ifp))
  314. brcmu_pkt_buf_free_skb(txp);
  315. else
  316. brcmf_txfinalize(ifp, txp, success);
  317. }
  318. }
  319. static void
  320. brcmf_proto_bcdc_configure_addr_mode(struct brcmf_pub *drvr, int ifidx,
  321. enum proto_addr_mode addr_mode)
  322. {
  323. }
  324. static void
  325. brcmf_proto_bcdc_delete_peer(struct brcmf_pub *drvr, int ifidx,
  326. u8 peer[ETH_ALEN])
  327. {
  328. }
  329. static void
  330. brcmf_proto_bcdc_add_tdls_peer(struct brcmf_pub *drvr, int ifidx,
  331. u8 peer[ETH_ALEN])
  332. {
  333. }
  334. static void brcmf_proto_bcdc_rxreorder(struct brcmf_if *ifp,
  335. struct sk_buff *skb)
  336. {
  337. brcmf_fws_rxreorder(ifp, skb);
  338. }
  339. static void
  340. brcmf_proto_bcdc_add_if(struct brcmf_if *ifp)
  341. {
  342. brcmf_fws_add_interface(ifp);
  343. }
  344. static void
  345. brcmf_proto_bcdc_del_if(struct brcmf_if *ifp)
  346. {
  347. brcmf_fws_del_interface(ifp);
  348. }
  349. static void
  350. brcmf_proto_bcdc_reset_if(struct brcmf_if *ifp)
  351. {
  352. brcmf_fws_reset_interface(ifp);
  353. }
  354. static int
  355. brcmf_proto_bcdc_init_done(struct brcmf_pub *drvr)
  356. {
  357. struct brcmf_bcdc *bcdc = drvr->proto->pd;
  358. struct brcmf_fws_info *fws;
  359. fws = brcmf_fws_attach(drvr);
  360. if (IS_ERR(fws))
  361. return PTR_ERR(fws);
  362. bcdc->fws = fws;
  363. return 0;
  364. }
  365. int brcmf_proto_bcdc_attach(struct brcmf_pub *drvr)
  366. {
  367. struct brcmf_bcdc *bcdc;
  368. bcdc = kzalloc(sizeof(*bcdc), GFP_ATOMIC);
  369. if (!bcdc)
  370. goto fail;
  371. /* ensure that the msg buf directly follows the cdc msg struct */
  372. if ((unsigned long)(&bcdc->msg + 1) != (unsigned long)bcdc->buf) {
  373. brcmf_err("struct brcmf_proto_bcdc is not correctly defined\n");
  374. goto fail;
  375. }
  376. drvr->proto->hdrpull = brcmf_proto_bcdc_hdrpull;
  377. drvr->proto->query_dcmd = brcmf_proto_bcdc_query_dcmd;
  378. drvr->proto->set_dcmd = brcmf_proto_bcdc_set_dcmd;
  379. drvr->proto->tx_queue_data = brcmf_proto_bcdc_tx_queue_data;
  380. drvr->proto->txdata = brcmf_proto_bcdc_txdata;
  381. drvr->proto->configure_addr_mode = brcmf_proto_bcdc_configure_addr_mode;
  382. drvr->proto->delete_peer = brcmf_proto_bcdc_delete_peer;
  383. drvr->proto->add_tdls_peer = brcmf_proto_bcdc_add_tdls_peer;
  384. drvr->proto->rxreorder = brcmf_proto_bcdc_rxreorder;
  385. drvr->proto->add_if = brcmf_proto_bcdc_add_if;
  386. drvr->proto->del_if = brcmf_proto_bcdc_del_if;
  387. drvr->proto->reset_if = brcmf_proto_bcdc_reset_if;
  388. drvr->proto->init_done = brcmf_proto_bcdc_init_done;
  389. drvr->proto->pd = bcdc;
  390. drvr->hdrlen += BCDC_HEADER_LEN + BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES;
  391. drvr->bus_if->maxctl = BRCMF_DCMD_MAXLEN +
  392. sizeof(struct brcmf_proto_bcdc_dcmd);
  393. return 0;
  394. fail:
  395. kfree(bcdc);
  396. return -ENOMEM;
  397. }
  398. void brcmf_proto_bcdc_detach(struct brcmf_pub *drvr)
  399. {
  400. struct brcmf_bcdc *bcdc = drvr->proto->pd;
  401. drvr->proto->pd = NULL;
  402. brcmf_fws_detach(bcdc->fws);
  403. kfree(bcdc);
  404. }