bcdc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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, int *fwerr)
  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. *fwerr = 0;
  152. ret = brcmf_proto_bcdc_msg(drvr, ifidx, cmd, buf, len, false);
  153. if (ret < 0) {
  154. brcmf_err("brcmf_proto_bcdc_msg failed w/status %d\n",
  155. ret);
  156. goto done;
  157. }
  158. retry:
  159. /* wait for interrupt and get first fragment */
  160. ret = brcmf_proto_bcdc_cmplt(drvr, bcdc->reqid, len);
  161. if (ret < 0)
  162. goto done;
  163. flags = le32_to_cpu(msg->flags);
  164. id = (flags & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT;
  165. if ((id < bcdc->reqid) && (++retries < RETRIES))
  166. goto retry;
  167. if (id != bcdc->reqid) {
  168. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  169. brcmf_ifname(brcmf_get_ifp(drvr, ifidx)), id,
  170. bcdc->reqid);
  171. ret = -EINVAL;
  172. goto done;
  173. }
  174. /* Check info buffer */
  175. info = (void *)&bcdc->buf[0];
  176. /* Copy info buffer */
  177. if (buf) {
  178. if (ret < (int)len)
  179. len = ret;
  180. memcpy(buf, info, len);
  181. }
  182. ret = 0;
  183. /* Check the ERROR flag */
  184. if (flags & BCDC_DCMD_ERROR)
  185. *fwerr = le32_to_cpu(msg->status);
  186. done:
  187. return ret;
  188. }
  189. static int
  190. brcmf_proto_bcdc_set_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
  191. void *buf, uint len, int *fwerr)
  192. {
  193. struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
  194. struct brcmf_proto_bcdc_dcmd *msg = &bcdc->msg;
  195. int ret;
  196. u32 flags, id;
  197. brcmf_dbg(BCDC, "Enter, cmd %d len %d\n", cmd, len);
  198. *fwerr = 0;
  199. ret = brcmf_proto_bcdc_msg(drvr, ifidx, cmd, buf, len, true);
  200. if (ret < 0)
  201. goto done;
  202. ret = brcmf_proto_bcdc_cmplt(drvr, bcdc->reqid, len);
  203. if (ret < 0)
  204. goto done;
  205. flags = le32_to_cpu(msg->flags);
  206. id = (flags & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT;
  207. if (id != bcdc->reqid) {
  208. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  209. brcmf_ifname(brcmf_get_ifp(drvr, ifidx)), id,
  210. bcdc->reqid);
  211. ret = -EINVAL;
  212. goto done;
  213. }
  214. ret = 0;
  215. /* Check the ERROR flag */
  216. if (flags & BCDC_DCMD_ERROR)
  217. *fwerr = le32_to_cpu(msg->status);
  218. done:
  219. return ret;
  220. }
  221. static void
  222. brcmf_proto_bcdc_hdrpush(struct brcmf_pub *drvr, int ifidx, u8 offset,
  223. struct sk_buff *pktbuf)
  224. {
  225. struct brcmf_proto_bcdc_header *h;
  226. brcmf_dbg(BCDC, "Enter\n");
  227. /* Push BDC header used to convey priority for buses that don't */
  228. skb_push(pktbuf, BCDC_HEADER_LEN);
  229. h = (struct brcmf_proto_bcdc_header *)(pktbuf->data);
  230. h->flags = (BCDC_PROTO_VER << BCDC_FLAG_VER_SHIFT);
  231. if (pktbuf->ip_summed == CHECKSUM_PARTIAL)
  232. h->flags |= BCDC_FLAG_SUM_NEEDED;
  233. h->priority = (pktbuf->priority & BCDC_PRIORITY_MASK);
  234. h->flags2 = 0;
  235. h->data_offset = offset;
  236. BCDC_SET_IF_IDX(h, ifidx);
  237. trace_brcmf_bcdchdr(pktbuf->data);
  238. }
  239. static int
  240. brcmf_proto_bcdc_hdrpull(struct brcmf_pub *drvr, bool do_fws,
  241. struct sk_buff *pktbuf, struct brcmf_if **ifp)
  242. {
  243. struct brcmf_proto_bcdc_header *h;
  244. struct brcmf_if *tmp_if;
  245. brcmf_dbg(BCDC, "Enter\n");
  246. /* Pop BCDC header used to convey priority for buses that don't */
  247. if (pktbuf->len <= BCDC_HEADER_LEN) {
  248. brcmf_dbg(INFO, "rx data too short (%d <= %d)\n",
  249. pktbuf->len, BCDC_HEADER_LEN);
  250. return -EBADE;
  251. }
  252. trace_brcmf_bcdchdr(pktbuf->data);
  253. h = (struct brcmf_proto_bcdc_header *)(pktbuf->data);
  254. tmp_if = brcmf_get_ifp(drvr, BCDC_GET_IF_IDX(h));
  255. if (!tmp_if) {
  256. brcmf_dbg(INFO, "no matching ifp found\n");
  257. return -EBADE;
  258. }
  259. if (((h->flags & BCDC_FLAG_VER_MASK) >> BCDC_FLAG_VER_SHIFT) !=
  260. BCDC_PROTO_VER) {
  261. brcmf_err("%s: non-BCDC packet received, flags 0x%x\n",
  262. brcmf_ifname(tmp_if), h->flags);
  263. return -EBADE;
  264. }
  265. if (h->flags & BCDC_FLAG_SUM_GOOD) {
  266. brcmf_dbg(BCDC, "%s: BDC rcv, good checksum, flags 0x%x\n",
  267. brcmf_ifname(tmp_if), h->flags);
  268. pktbuf->ip_summed = CHECKSUM_UNNECESSARY;
  269. }
  270. pktbuf->priority = h->priority & BCDC_PRIORITY_MASK;
  271. skb_pull(pktbuf, BCDC_HEADER_LEN);
  272. if (do_fws)
  273. brcmf_fws_hdrpull(tmp_if, h->data_offset << 2, pktbuf);
  274. else
  275. skb_pull(pktbuf, h->data_offset << 2);
  276. if (pktbuf->len == 0)
  277. return -ENODATA;
  278. if (ifp != NULL)
  279. *ifp = tmp_if;
  280. return 0;
  281. }
  282. static int brcmf_proto_bcdc_tx_queue_data(struct brcmf_pub *drvr, int ifidx,
  283. struct sk_buff *skb)
  284. {
  285. struct brcmf_if *ifp = brcmf_get_ifp(drvr, ifidx);
  286. struct brcmf_bcdc *bcdc = drvr->proto->pd;
  287. if (!brcmf_fws_queue_skbs(bcdc->fws))
  288. return brcmf_proto_txdata(drvr, ifidx, 0, skb);
  289. return brcmf_fws_process_skb(ifp, skb);
  290. }
  291. static int
  292. brcmf_proto_bcdc_txdata(struct brcmf_pub *drvr, int ifidx, u8 offset,
  293. struct sk_buff *pktbuf)
  294. {
  295. brcmf_proto_bcdc_hdrpush(drvr, ifidx, offset, pktbuf);
  296. return brcmf_bus_txdata(drvr->bus_if, pktbuf);
  297. }
  298. void brcmf_proto_bcdc_txflowblock(struct device *dev, bool state)
  299. {
  300. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  301. struct brcmf_pub *drvr = bus_if->drvr;
  302. brcmf_dbg(TRACE, "Enter\n");
  303. brcmf_fws_bus_blocked(drvr, state);
  304. }
  305. void
  306. brcmf_proto_bcdc_txcomplete(struct device *dev, struct sk_buff *txp,
  307. bool success)
  308. {
  309. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  310. struct brcmf_bcdc *bcdc = bus_if->drvr->proto->pd;
  311. struct brcmf_if *ifp;
  312. /* await txstatus signal for firmware if active */
  313. if (brcmf_fws_fc_active(bcdc->fws)) {
  314. if (!success)
  315. brcmf_fws_bustxfail(bcdc->fws, txp);
  316. } else {
  317. if (brcmf_proto_bcdc_hdrpull(bus_if->drvr, false, txp, &ifp))
  318. brcmu_pkt_buf_free_skb(txp);
  319. else
  320. brcmf_txfinalize(ifp, txp, success);
  321. }
  322. }
  323. static void
  324. brcmf_proto_bcdc_configure_addr_mode(struct brcmf_pub *drvr, int ifidx,
  325. enum proto_addr_mode addr_mode)
  326. {
  327. }
  328. static void
  329. brcmf_proto_bcdc_delete_peer(struct brcmf_pub *drvr, int ifidx,
  330. u8 peer[ETH_ALEN])
  331. {
  332. }
  333. static void
  334. brcmf_proto_bcdc_add_tdls_peer(struct brcmf_pub *drvr, int ifidx,
  335. u8 peer[ETH_ALEN])
  336. {
  337. }
  338. static void brcmf_proto_bcdc_rxreorder(struct brcmf_if *ifp,
  339. struct sk_buff *skb)
  340. {
  341. brcmf_fws_rxreorder(ifp, skb);
  342. }
  343. static void
  344. brcmf_proto_bcdc_add_if(struct brcmf_if *ifp)
  345. {
  346. brcmf_fws_add_interface(ifp);
  347. }
  348. static void
  349. brcmf_proto_bcdc_del_if(struct brcmf_if *ifp)
  350. {
  351. brcmf_fws_del_interface(ifp);
  352. }
  353. static void
  354. brcmf_proto_bcdc_reset_if(struct brcmf_if *ifp)
  355. {
  356. brcmf_fws_reset_interface(ifp);
  357. }
  358. static int
  359. brcmf_proto_bcdc_init_done(struct brcmf_pub *drvr)
  360. {
  361. struct brcmf_bcdc *bcdc = drvr->proto->pd;
  362. struct brcmf_fws_info *fws;
  363. fws = brcmf_fws_attach(drvr);
  364. if (IS_ERR(fws))
  365. return PTR_ERR(fws);
  366. bcdc->fws = fws;
  367. return 0;
  368. }
  369. static void brcmf_proto_bcdc_debugfs_create(struct brcmf_pub *drvr)
  370. {
  371. brcmf_fws_debugfs_create(drvr);
  372. }
  373. int brcmf_proto_bcdc_attach(struct brcmf_pub *drvr)
  374. {
  375. struct brcmf_bcdc *bcdc;
  376. bcdc = kzalloc(sizeof(*bcdc), GFP_ATOMIC);
  377. if (!bcdc)
  378. goto fail;
  379. /* ensure that the msg buf directly follows the cdc msg struct */
  380. if ((unsigned long)(&bcdc->msg + 1) != (unsigned long)bcdc->buf) {
  381. brcmf_err("struct brcmf_proto_bcdc is not correctly defined\n");
  382. goto fail;
  383. }
  384. drvr->proto->hdrpull = brcmf_proto_bcdc_hdrpull;
  385. drvr->proto->query_dcmd = brcmf_proto_bcdc_query_dcmd;
  386. drvr->proto->set_dcmd = brcmf_proto_bcdc_set_dcmd;
  387. drvr->proto->tx_queue_data = brcmf_proto_bcdc_tx_queue_data;
  388. drvr->proto->txdata = brcmf_proto_bcdc_txdata;
  389. drvr->proto->configure_addr_mode = brcmf_proto_bcdc_configure_addr_mode;
  390. drvr->proto->delete_peer = brcmf_proto_bcdc_delete_peer;
  391. drvr->proto->add_tdls_peer = brcmf_proto_bcdc_add_tdls_peer;
  392. drvr->proto->rxreorder = brcmf_proto_bcdc_rxreorder;
  393. drvr->proto->add_if = brcmf_proto_bcdc_add_if;
  394. drvr->proto->del_if = brcmf_proto_bcdc_del_if;
  395. drvr->proto->reset_if = brcmf_proto_bcdc_reset_if;
  396. drvr->proto->init_done = brcmf_proto_bcdc_init_done;
  397. drvr->proto->debugfs_create = brcmf_proto_bcdc_debugfs_create;
  398. drvr->proto->pd = bcdc;
  399. drvr->hdrlen += BCDC_HEADER_LEN + BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES;
  400. drvr->bus_if->maxctl = BRCMF_DCMD_MAXLEN +
  401. sizeof(struct brcmf_proto_bcdc_dcmd);
  402. return 0;
  403. fail:
  404. kfree(bcdc);
  405. return -ENOMEM;
  406. }
  407. void brcmf_proto_bcdc_detach_pre_delif(struct brcmf_pub *drvr)
  408. {
  409. struct brcmf_bcdc *bcdc = drvr->proto->pd;
  410. brcmf_fws_detach_pre_delif(bcdc->fws);
  411. }
  412. void brcmf_proto_bcdc_detach_post_delif(struct brcmf_pub *drvr)
  413. {
  414. struct brcmf_bcdc *bcdc = drvr->proto->pd;
  415. drvr->proto->pd = NULL;
  416. brcmf_fws_detach_post_delif(bcdc->fws);
  417. kfree(bcdc);
  418. }