dhd_common.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/netdevice.h>
  19. #include <brcmu_wifi.h>
  20. #include <brcmu_utils.h>
  21. #include "dhd.h"
  22. #include "dhd_bus.h"
  23. #include "dhd_dbg.h"
  24. #include "fwil.h"
  25. #include "tracepoint.h"
  26. #define PKTFILTER_BUF_SIZE 128
  27. #define BRCMF_DEFAULT_BCN_TIMEOUT 3
  28. #define BRCMF_DEFAULT_SCAN_CHANNEL_TIME 40
  29. #define BRCMF_DEFAULT_SCAN_UNASSOC_TIME 40
  30. #define BRCMF_DEFAULT_PACKET_FILTER "100 0 0 0 0x01 0x00"
  31. #ifdef DEBUG
  32. static const char brcmf_version[] =
  33. "Dongle Host Driver, version " BRCMF_VERSION_STR "\nCompiled on "
  34. __DATE__ " at " __TIME__;
  35. #else
  36. static const char brcmf_version[] =
  37. "Dongle Host Driver, version " BRCMF_VERSION_STR;
  38. #endif
  39. bool brcmf_c_prec_enq(struct device *dev, struct pktq *q,
  40. struct sk_buff *pkt, int prec)
  41. {
  42. struct sk_buff *p;
  43. int eprec = -1; /* precedence to evict from */
  44. bool discard_oldest;
  45. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  46. struct brcmf_pub *drvr = bus_if->drvr;
  47. /* Fast case, precedence queue is not full and we are also not
  48. * exceeding total queue length
  49. */
  50. if (!pktq_pfull(q, prec) && !pktq_full(q)) {
  51. brcmu_pktq_penq(q, prec, pkt);
  52. return true;
  53. }
  54. /* Determine precedence from which to evict packet, if any */
  55. if (pktq_pfull(q, prec))
  56. eprec = prec;
  57. else if (pktq_full(q)) {
  58. p = brcmu_pktq_peek_tail(q, &eprec);
  59. if (eprec > prec)
  60. return false;
  61. }
  62. /* Evict if needed */
  63. if (eprec >= 0) {
  64. /* Detect queueing to unconfigured precedence */
  65. discard_oldest = ac_bitmap_tst(drvr->wme_dp, eprec);
  66. if (eprec == prec && !discard_oldest)
  67. return false; /* refuse newer (incoming) packet */
  68. /* Evict packet according to discard policy */
  69. p = discard_oldest ? brcmu_pktq_pdeq(q, eprec) :
  70. brcmu_pktq_pdeq_tail(q, eprec);
  71. if (p == NULL)
  72. brcmf_err("brcmu_pktq_penq() failed, oldest %d\n",
  73. discard_oldest);
  74. brcmu_pkt_buf_free_skb(p);
  75. }
  76. /* Enqueue */
  77. p = brcmu_pktq_penq(q, prec, pkt);
  78. if (p == NULL)
  79. brcmf_err("brcmu_pktq_penq() failed\n");
  80. return p != NULL;
  81. }
  82. /* Convert user's input in hex pattern to byte-size mask */
  83. static int brcmf_c_pattern_atoh(char *src, char *dst)
  84. {
  85. int i;
  86. if (strncmp(src, "0x", 2) != 0 && strncmp(src, "0X", 2) != 0) {
  87. brcmf_err("Mask invalid format. Needs to start with 0x\n");
  88. return -EINVAL;
  89. }
  90. src = src + 2; /* Skip past 0x */
  91. if (strlen(src) % 2 != 0) {
  92. brcmf_err("Mask invalid format. Length must be even.\n");
  93. return -EINVAL;
  94. }
  95. for (i = 0; *src != '\0'; i++) {
  96. unsigned long res;
  97. char num[3];
  98. strncpy(num, src, 2);
  99. num[2] = '\0';
  100. if (kstrtoul(num, 16, &res))
  101. return -EINVAL;
  102. dst[i] = (u8)res;
  103. src += 2;
  104. }
  105. return i;
  106. }
  107. static void
  108. brcmf_c_pktfilter_offload_enable(struct brcmf_if *ifp, char *arg, int enable,
  109. int master_mode)
  110. {
  111. unsigned long res;
  112. char *argv;
  113. char *arg_save = NULL, *arg_org = NULL;
  114. s32 err;
  115. struct brcmf_pkt_filter_enable_le enable_parm;
  116. arg_save = kstrdup(arg, GFP_ATOMIC);
  117. if (!arg_save)
  118. goto fail;
  119. arg_org = arg_save;
  120. argv = strsep(&arg_save, " ");
  121. if (argv == NULL) {
  122. brcmf_err("No args provided\n");
  123. goto fail;
  124. }
  125. /* Parse packet filter id. */
  126. enable_parm.id = 0;
  127. if (!kstrtoul(argv, 0, &res))
  128. enable_parm.id = cpu_to_le32((u32)res);
  129. /* Enable/disable the specified filter. */
  130. enable_parm.enable = cpu_to_le32(enable);
  131. err = brcmf_fil_iovar_data_set(ifp, "pkt_filter_enable", &enable_parm,
  132. sizeof(enable_parm));
  133. if (err)
  134. brcmf_err("Set pkt_filter_enable error (%d)\n", err);
  135. /* Control the master mode */
  136. err = brcmf_fil_iovar_int_set(ifp, "pkt_filter_mode", master_mode);
  137. if (err)
  138. brcmf_err("Set pkt_filter_mode error (%d)\n", err);
  139. fail:
  140. kfree(arg_org);
  141. }
  142. static void brcmf_c_pktfilter_offload_set(struct brcmf_if *ifp, char *arg)
  143. {
  144. struct brcmf_pkt_filter_le *pkt_filter;
  145. unsigned long res;
  146. int buf_len;
  147. s32 err;
  148. u32 mask_size;
  149. u32 pattern_size;
  150. char *argv[8], *buf = NULL;
  151. int i = 0;
  152. char *arg_save = NULL, *arg_org = NULL;
  153. arg_save = kstrdup(arg, GFP_ATOMIC);
  154. if (!arg_save)
  155. goto fail;
  156. arg_org = arg_save;
  157. buf = kmalloc(PKTFILTER_BUF_SIZE, GFP_ATOMIC);
  158. if (!buf)
  159. goto fail;
  160. argv[i] = strsep(&arg_save, " ");
  161. while (argv[i]) {
  162. i++;
  163. if (i >= 8) {
  164. brcmf_err("Too many parameters\n");
  165. goto fail;
  166. }
  167. argv[i] = strsep(&arg_save, " ");
  168. }
  169. if (i != 6) {
  170. brcmf_err("Not enough args provided %d\n", i);
  171. goto fail;
  172. }
  173. pkt_filter = (struct brcmf_pkt_filter_le *)buf;
  174. /* Parse packet filter id. */
  175. pkt_filter->id = 0;
  176. if (!kstrtoul(argv[0], 0, &res))
  177. pkt_filter->id = cpu_to_le32((u32)res);
  178. /* Parse filter polarity. */
  179. pkt_filter->negate_match = 0;
  180. if (!kstrtoul(argv[1], 0, &res))
  181. pkt_filter->negate_match = cpu_to_le32((u32)res);
  182. /* Parse filter type. */
  183. pkt_filter->type = 0;
  184. if (!kstrtoul(argv[2], 0, &res))
  185. pkt_filter->type = cpu_to_le32((u32)res);
  186. /* Parse pattern filter offset. */
  187. pkt_filter->u.pattern.offset = 0;
  188. if (!kstrtoul(argv[3], 0, &res))
  189. pkt_filter->u.pattern.offset = cpu_to_le32((u32)res);
  190. /* Parse pattern filter mask. */
  191. mask_size = brcmf_c_pattern_atoh(argv[4],
  192. (char *)pkt_filter->u.pattern.mask_and_pattern);
  193. /* Parse pattern filter pattern. */
  194. pattern_size = brcmf_c_pattern_atoh(argv[5],
  195. (char *)&pkt_filter->u.pattern.mask_and_pattern[mask_size]);
  196. if (mask_size != pattern_size) {
  197. brcmf_err("Mask and pattern not the same size\n");
  198. goto fail;
  199. }
  200. pkt_filter->u.pattern.size_bytes = cpu_to_le32(mask_size);
  201. buf_len = offsetof(struct brcmf_pkt_filter_le,
  202. u.pattern.mask_and_pattern);
  203. buf_len += mask_size + pattern_size;
  204. err = brcmf_fil_iovar_data_set(ifp, "pkt_filter_add", pkt_filter,
  205. buf_len);
  206. if (err)
  207. brcmf_err("Set pkt_filter_add error (%d)\n", err);
  208. fail:
  209. kfree(arg_org);
  210. kfree(buf);
  211. }
  212. int brcmf_c_preinit_dcmds(struct brcmf_if *ifp)
  213. {
  214. s8 eventmask[BRCMF_EVENTING_MASK_LEN];
  215. u8 buf[BRCMF_DCMD_SMLEN];
  216. char *ptr;
  217. s32 err;
  218. /* retreive mac address */
  219. err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", ifp->mac_addr,
  220. sizeof(ifp->mac_addr));
  221. if (err < 0) {
  222. brcmf_err("Retreiving cur_etheraddr failed, %d\n",
  223. err);
  224. goto done;
  225. }
  226. memcpy(ifp->drvr->mac, ifp->mac_addr, sizeof(ifp->drvr->mac));
  227. /* query for 'ver' to get version info from firmware */
  228. memset(buf, 0, sizeof(buf));
  229. strcpy(buf, "ver");
  230. err = brcmf_fil_iovar_data_get(ifp, "ver", buf, sizeof(buf));
  231. if (err < 0) {
  232. brcmf_err("Retreiving version information failed, %d\n",
  233. err);
  234. goto done;
  235. }
  236. ptr = (char *)buf;
  237. strsep(&ptr, "\n");
  238. /* Print fw version info */
  239. brcmf_err("Firmware version = %s\n", buf);
  240. /*
  241. * Setup timeout if Beacons are lost and roam is off to report
  242. * link down
  243. */
  244. err = brcmf_fil_iovar_int_set(ifp, "bcn_timeout",
  245. BRCMF_DEFAULT_BCN_TIMEOUT);
  246. if (err) {
  247. brcmf_err("bcn_timeout error (%d)\n", err);
  248. goto done;
  249. }
  250. /* Enable/Disable build-in roaming to allowed ext supplicant to take
  251. * of romaing
  252. */
  253. err = brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
  254. if (err) {
  255. brcmf_err("roam_off error (%d)\n", err);
  256. goto done;
  257. }
  258. /* Setup event_msgs, enable E_IF */
  259. err = brcmf_fil_iovar_data_get(ifp, "event_msgs", eventmask,
  260. BRCMF_EVENTING_MASK_LEN);
  261. if (err) {
  262. brcmf_err("Get event_msgs error (%d)\n", err);
  263. goto done;
  264. }
  265. setbit(eventmask, BRCMF_E_IF);
  266. err = brcmf_fil_iovar_data_set(ifp, "event_msgs", eventmask,
  267. BRCMF_EVENTING_MASK_LEN);
  268. if (err) {
  269. brcmf_err("Set event_msgs error (%d)\n", err);
  270. goto done;
  271. }
  272. /* Setup default scan channel time */
  273. err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
  274. BRCMF_DEFAULT_SCAN_CHANNEL_TIME);
  275. if (err) {
  276. brcmf_err("BRCMF_C_SET_SCAN_CHANNEL_TIME error (%d)\n",
  277. err);
  278. goto done;
  279. }
  280. /* Setup default scan unassoc time */
  281. err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
  282. BRCMF_DEFAULT_SCAN_UNASSOC_TIME);
  283. if (err) {
  284. brcmf_err("BRCMF_C_SET_SCAN_UNASSOC_TIME error (%d)\n",
  285. err);
  286. goto done;
  287. }
  288. /* Setup packet filter */
  289. brcmf_c_pktfilter_offload_set(ifp, BRCMF_DEFAULT_PACKET_FILTER);
  290. brcmf_c_pktfilter_offload_enable(ifp, BRCMF_DEFAULT_PACKET_FILTER,
  291. 0, true);
  292. /* do bus specific preinit here */
  293. err = brcmf_bus_preinit(ifp->drvr->bus_if);
  294. done:
  295. return err;
  296. }
  297. #ifdef CONFIG_BRCM_TRACING
  298. void __brcmf_err(const char *func, const char *fmt, ...)
  299. {
  300. struct va_format vaf = {
  301. .fmt = fmt,
  302. };
  303. va_list args;
  304. va_start(args, fmt);
  305. vaf.va = &args;
  306. pr_err("%s: %pV", func, &vaf);
  307. trace_brcmf_err(func, &vaf);
  308. va_end(args);
  309. }
  310. #endif
  311. #if defined(CONFIG_BRCM_TRACING) || defined(CONFIG_BRCMDBG)
  312. void __brcmf_dbg(u32 level, const char *func, const char *fmt, ...)
  313. {
  314. struct va_format vaf = {
  315. .fmt = fmt,
  316. };
  317. va_list args;
  318. va_start(args, fmt);
  319. vaf.va = &args;
  320. if (brcmf_msg_level & level)
  321. pr_debug("%s %pV", func, &vaf);
  322. trace_brcmf_dbg(level, func, &vaf);
  323. va_end(args);
  324. }
  325. #endif