bcdc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 "dhd.h"
  25. #include "dhd_bus.h"
  26. #include "fwsignal.h"
  27. #include "dhd_dbg.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. /* Max valid buffer size that can be sent to the dongle */
  39. #define BCDC_MAX_MSG_SIZE (ETH_FRAME_LEN+ETH_FCS_LEN)
  40. /* BCDC flag definitions */
  41. #define BCDC_DCMD_ERROR 0x01 /* 1=cmd failed */
  42. #define BCDC_DCMD_SET 0x02 /* 0=get, 1=set cmd */
  43. #define BCDC_DCMD_IF_MASK 0xF000 /* I/F index */
  44. #define BCDC_DCMD_IF_SHIFT 12
  45. #define BCDC_DCMD_ID_MASK 0xFFFF0000 /* id an cmd pairing */
  46. #define BCDC_DCMD_ID_SHIFT 16 /* ID Mask shift bits */
  47. #define BCDC_DCMD_ID(flags) \
  48. (((flags) & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT)
  49. /*
  50. * BCDC header - Broadcom specific extension of CDC.
  51. * Used on data packets to convey priority across USB.
  52. */
  53. #define BCDC_HEADER_LEN 4
  54. #define BCDC_PROTO_VER 2 /* Protocol version */
  55. #define BCDC_FLAG_VER_MASK 0xf0 /* Protocol version mask */
  56. #define BCDC_FLAG_VER_SHIFT 4 /* Protocol version shift */
  57. #define BCDC_FLAG_SUM_GOOD 0x04 /* Good RX checksums */
  58. #define BCDC_FLAG_SUM_NEEDED 0x08 /* Dongle needs to do TX checksums */
  59. #define BCDC_PRIORITY_MASK 0x7
  60. #define BCDC_FLAG2_IF_MASK 0x0f /* packet rx interface in APSTA */
  61. #define BCDC_FLAG2_IF_SHIFT 0
  62. #define BCDC_GET_IF_IDX(hdr) \
  63. ((int)((((hdr)->flags2) & BCDC_FLAG2_IF_MASK) >> BCDC_FLAG2_IF_SHIFT))
  64. #define BCDC_SET_IF_IDX(hdr, idx) \
  65. ((hdr)->flags2 = (((hdr)->flags2 & ~BCDC_FLAG2_IF_MASK) | \
  66. ((idx) << BCDC_FLAG2_IF_SHIFT)))
  67. /**
  68. * struct brcmf_proto_bcdc_header - BCDC header format
  69. *
  70. * @flags: flags contain protocol and checksum info.
  71. * @priority: 802.1d priority and USB flow control info (bit 4:7).
  72. * @flags2: additional flags containing dongle interface index.
  73. * @data_offset: start of packet data. header is following by firmware signals.
  74. */
  75. struct brcmf_proto_bcdc_header {
  76. u8 flags;
  77. u8 priority;
  78. u8 flags2;
  79. u8 data_offset;
  80. };
  81. /*
  82. * maximum length of firmware signal data between
  83. * the BCDC header and packet data in the tx path.
  84. */
  85. #define BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES 12
  86. #define RETRIES 2 /* # of retries to retrieve matching dcmd response */
  87. #define BUS_HEADER_LEN (16+64) /* Must be atleast SDPCM_RESERVE
  88. * (amount of header tha might be added)
  89. * plus any space that might be needed
  90. * for bus alignment padding.
  91. */
  92. #define ROUND_UP_MARGIN 2048 /* Biggest bus block size possible for
  93. * round off at the end of buffer
  94. * Currently is SDIO
  95. */
  96. struct brcmf_bcdc {
  97. u16 reqid;
  98. u8 bus_header[BUS_HEADER_LEN];
  99. struct brcmf_proto_bcdc_dcmd msg;
  100. unsigned char buf[BRCMF_DCMD_MAXLEN + ROUND_UP_MARGIN];
  101. };
  102. static int brcmf_proto_bcdc_msg(struct brcmf_pub *drvr)
  103. {
  104. struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
  105. int len = le32_to_cpu(bcdc->msg.len) +
  106. sizeof(struct brcmf_proto_bcdc_dcmd);
  107. brcmf_dbg(BCDC, "Enter\n");
  108. /* NOTE : bcdc->msg.len holds the desired length of the buffer to be
  109. * returned. Only up to BCDC_MAX_MSG_SIZE of this buffer area
  110. * is actually sent to the dongle
  111. */
  112. if (len > BCDC_MAX_MSG_SIZE)
  113. len = BCDC_MAX_MSG_SIZE;
  114. /* Send request */
  115. return brcmf_bus_txctl(drvr->bus_if, (unsigned char *)&bcdc->msg, len);
  116. }
  117. static int brcmf_proto_bcdc_cmplt(struct brcmf_pub *drvr, u32 id, u32 len)
  118. {
  119. int ret;
  120. struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
  121. brcmf_dbg(BCDC, "Enter\n");
  122. len += sizeof(struct brcmf_proto_bcdc_dcmd);
  123. do {
  124. ret = brcmf_bus_rxctl(drvr->bus_if, (unsigned char *)&bcdc->msg,
  125. len);
  126. if (ret < 0)
  127. break;
  128. } while (BCDC_DCMD_ID(le32_to_cpu(bcdc->msg.flags)) != id);
  129. return ret;
  130. }
  131. static int
  132. brcmf_proto_bcdc_query_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
  133. void *buf, uint len)
  134. {
  135. struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
  136. struct brcmf_proto_bcdc_dcmd *msg = &bcdc->msg;
  137. void *info;
  138. int ret = 0, retries = 0;
  139. u32 id, flags;
  140. brcmf_dbg(BCDC, "Enter, cmd %d len %d\n", cmd, len);
  141. memset(msg, 0, sizeof(struct brcmf_proto_bcdc_dcmd));
  142. msg->cmd = cpu_to_le32(cmd);
  143. msg->len = cpu_to_le32(len);
  144. flags = (++bcdc->reqid << BCDC_DCMD_ID_SHIFT);
  145. flags = (flags & ~BCDC_DCMD_IF_MASK) |
  146. (ifidx << BCDC_DCMD_IF_SHIFT);
  147. msg->flags = cpu_to_le32(flags);
  148. if (buf)
  149. memcpy(bcdc->buf, buf, len);
  150. ret = brcmf_proto_bcdc_msg(drvr);
  151. if (ret < 0) {
  152. brcmf_err("brcmf_proto_bcdc_msg failed w/status %d\n",
  153. ret);
  154. goto done;
  155. }
  156. retry:
  157. /* wait for interrupt and get first fragment */
  158. ret = brcmf_proto_bcdc_cmplt(drvr, bcdc->reqid, len);
  159. if (ret < 0)
  160. goto done;
  161. flags = le32_to_cpu(msg->flags);
  162. id = (flags & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT;
  163. if ((id < bcdc->reqid) && (++retries < RETRIES))
  164. goto retry;
  165. if (id != bcdc->reqid) {
  166. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  167. brcmf_ifname(drvr, ifidx), id, bcdc->reqid);
  168. ret = -EINVAL;
  169. goto done;
  170. }
  171. /* Check info buffer */
  172. info = (void *)&msg[1];
  173. /* Copy info buffer */
  174. if (buf) {
  175. if (ret < (int)len)
  176. len = ret;
  177. memcpy(buf, info, len);
  178. }
  179. /* Check the ERROR flag */
  180. if (flags & BCDC_DCMD_ERROR)
  181. ret = le32_to_cpu(msg->status);
  182. done:
  183. return ret;
  184. }
  185. static int
  186. brcmf_proto_bcdc_set_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
  187. void *buf, uint len)
  188. {
  189. struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
  190. struct brcmf_proto_bcdc_dcmd *msg = &bcdc->msg;
  191. int ret = 0;
  192. u32 flags, id;
  193. brcmf_dbg(BCDC, "Enter, cmd %d len %d\n", cmd, len);
  194. memset(msg, 0, sizeof(struct brcmf_proto_bcdc_dcmd));
  195. msg->cmd = cpu_to_le32(cmd);
  196. msg->len = cpu_to_le32(len);
  197. flags = (++bcdc->reqid << BCDC_DCMD_ID_SHIFT) | BCDC_DCMD_SET;
  198. flags = (flags & ~BCDC_DCMD_IF_MASK) |
  199. (ifidx << BCDC_DCMD_IF_SHIFT);
  200. msg->flags = cpu_to_le32(flags);
  201. if (buf)
  202. memcpy(bcdc->buf, buf, len);
  203. ret = brcmf_proto_bcdc_msg(drvr);
  204. if (ret < 0)
  205. goto done;
  206. ret = brcmf_proto_bcdc_cmplt(drvr, bcdc->reqid, len);
  207. if (ret < 0)
  208. goto done;
  209. flags = le32_to_cpu(msg->flags);
  210. id = (flags & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT;
  211. if (id != bcdc->reqid) {
  212. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  213. brcmf_ifname(drvr, ifidx), id, bcdc->reqid);
  214. ret = -EINVAL;
  215. goto done;
  216. }
  217. /* Check the ERROR flag */
  218. if (flags & BCDC_DCMD_ERROR)
  219. ret = le32_to_cpu(msg->status);
  220. done:
  221. return ret;
  222. }
  223. static void
  224. brcmf_proto_bcdc_hdrpush(struct brcmf_pub *drvr, int ifidx, u8 offset,
  225. struct sk_buff *pktbuf)
  226. {
  227. struct brcmf_proto_bcdc_header *h;
  228. brcmf_dbg(BCDC, "Enter\n");
  229. /* Push BDC header used to convey priority for buses that don't */
  230. skb_push(pktbuf, BCDC_HEADER_LEN);
  231. h = (struct brcmf_proto_bcdc_header *)(pktbuf->data);
  232. h->flags = (BCDC_PROTO_VER << BCDC_FLAG_VER_SHIFT);
  233. if (pktbuf->ip_summed == CHECKSUM_PARTIAL)
  234. h->flags |= BCDC_FLAG_SUM_NEEDED;
  235. h->priority = (pktbuf->priority & BCDC_PRIORITY_MASK);
  236. h->flags2 = 0;
  237. h->data_offset = offset;
  238. BCDC_SET_IF_IDX(h, ifidx);
  239. trace_brcmf_bcdchdr(pktbuf->data);
  240. }
  241. static int
  242. brcmf_proto_bcdc_hdrpull(struct brcmf_pub *drvr, bool do_fws, u8 *ifidx,
  243. struct sk_buff *pktbuf)
  244. {
  245. struct brcmf_proto_bcdc_header *h;
  246. brcmf_dbg(BCDC, "Enter\n");
  247. /* Pop BCDC header used to convey priority for buses that don't */
  248. if (pktbuf->len <= BCDC_HEADER_LEN) {
  249. brcmf_dbg(INFO, "rx data too short (%d <= %d)\n",
  250. pktbuf->len, BCDC_HEADER_LEN);
  251. return -EBADE;
  252. }
  253. trace_brcmf_bcdchdr(pktbuf->data);
  254. h = (struct brcmf_proto_bcdc_header *)(pktbuf->data);
  255. *ifidx = BCDC_GET_IF_IDX(h);
  256. if (*ifidx >= BRCMF_MAX_IFS) {
  257. brcmf_err("rx data ifnum out of range (%d)\n", *ifidx);
  258. return -EBADE;
  259. }
  260. /* The ifidx is the idx to map to matching netdev/ifp. When receiving
  261. * events this is easy because it contains the bssidx which maps
  262. * 1-on-1 to the netdev/ifp. But for data frames the ifidx is rcvd.
  263. * bssidx 1 is used for p2p0 and no data can be received or
  264. * transmitted on it. Therefor bssidx is ifidx + 1 if ifidx > 0
  265. */
  266. if (*ifidx)
  267. (*ifidx)++;
  268. if (((h->flags & BCDC_FLAG_VER_MASK) >> BCDC_FLAG_VER_SHIFT) !=
  269. BCDC_PROTO_VER) {
  270. brcmf_err("%s: non-BCDC packet received, flags 0x%x\n",
  271. brcmf_ifname(drvr, *ifidx), h->flags);
  272. return -EBADE;
  273. }
  274. if (h->flags & BCDC_FLAG_SUM_GOOD) {
  275. brcmf_dbg(BCDC, "%s: BDC rcv, good checksum, flags 0x%x\n",
  276. brcmf_ifname(drvr, *ifidx), h->flags);
  277. pktbuf->ip_summed = CHECKSUM_UNNECESSARY;
  278. }
  279. pktbuf->priority = h->priority & BCDC_PRIORITY_MASK;
  280. skb_pull(pktbuf, BCDC_HEADER_LEN);
  281. if (do_fws)
  282. brcmf_fws_hdrpull(drvr, *ifidx, h->data_offset << 2, pktbuf);
  283. else
  284. skb_pull(pktbuf, h->data_offset << 2);
  285. if (pktbuf->len == 0)
  286. return -ENODATA;
  287. return 0;
  288. }
  289. int brcmf_proto_bcdc_attach(struct brcmf_pub *drvr)
  290. {
  291. struct brcmf_bcdc *bcdc;
  292. bcdc = kzalloc(sizeof(*bcdc), GFP_ATOMIC);
  293. if (!bcdc)
  294. goto fail;
  295. /* ensure that the msg buf directly follows the cdc msg struct */
  296. if ((unsigned long)(&bcdc->msg + 1) != (unsigned long)bcdc->buf) {
  297. brcmf_err("struct brcmf_proto_bcdc is not correctly defined\n");
  298. goto fail;
  299. }
  300. drvr->proto->hdrpush = brcmf_proto_bcdc_hdrpush;
  301. drvr->proto->hdrpull = brcmf_proto_bcdc_hdrpull;
  302. drvr->proto->query_dcmd = brcmf_proto_bcdc_query_dcmd;
  303. drvr->proto->set_dcmd = brcmf_proto_bcdc_set_dcmd;
  304. drvr->proto->pd = bcdc;
  305. drvr->hdrlen += BCDC_HEADER_LEN + BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES;
  306. drvr->bus_if->maxctl = BRCMF_DCMD_MAXLEN +
  307. sizeof(struct brcmf_proto_bcdc_dcmd) + ROUND_UP_MARGIN;
  308. return 0;
  309. fail:
  310. kfree(bcdc);
  311. return -ENOMEM;
  312. }
  313. void brcmf_proto_bcdc_detach(struct brcmf_pub *drvr)
  314. {
  315. kfree(drvr->proto->pd);
  316. drvr->proto->pd = NULL;
  317. }