dhd_common.c 9.3 KB

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