cfg80211.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright (c) 2012 Qualcomm Atheros, Inc.
  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
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "wil6210.h"
  17. #include "wmi.h"
  18. #define CHAN60G(_channel, _flags) { \
  19. .band = IEEE80211_BAND_60GHZ, \
  20. .center_freq = 56160 + (2160 * (_channel)), \
  21. .hw_value = (_channel), \
  22. .flags = (_flags), \
  23. .max_antenna_gain = 0, \
  24. .max_power = 40, \
  25. }
  26. static struct ieee80211_channel wil_60ghz_channels[] = {
  27. CHAN60G(1, 0),
  28. CHAN60G(2, 0),
  29. CHAN60G(3, 0),
  30. /* channel 4 not supported yet */
  31. };
  32. static struct ieee80211_supported_band wil_band_60ghz = {
  33. .channels = wil_60ghz_channels,
  34. .n_channels = ARRAY_SIZE(wil_60ghz_channels),
  35. .ht_cap = {
  36. .ht_supported = true,
  37. .cap = 0, /* TODO */
  38. .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, /* TODO */
  39. .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, /* TODO */
  40. .mcs = {
  41. /* MCS 1..12 - SC PHY */
  42. .rx_mask = {0xfe, 0x1f}, /* 1..12 */
  43. .tx_params = IEEE80211_HT_MCS_TX_DEFINED, /* TODO */
  44. },
  45. },
  46. };
  47. static const struct ieee80211_txrx_stypes
  48. wil_mgmt_stypes[NUM_NL80211_IFTYPES] = {
  49. [NL80211_IFTYPE_STATION] = {
  50. .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  51. BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
  52. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  53. BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
  54. },
  55. [NL80211_IFTYPE_AP] = {
  56. .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  57. BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
  58. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  59. BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
  60. },
  61. [NL80211_IFTYPE_P2P_CLIENT] = {
  62. .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  63. BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
  64. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  65. BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
  66. },
  67. [NL80211_IFTYPE_P2P_GO] = {
  68. .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  69. BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
  70. .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
  71. BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
  72. },
  73. };
  74. static const u32 wil_cipher_suites[] = {
  75. WLAN_CIPHER_SUITE_GCMP,
  76. };
  77. int wil_iftype_nl2wmi(enum nl80211_iftype type)
  78. {
  79. static const struct {
  80. enum nl80211_iftype nl;
  81. enum wmi_network_type wmi;
  82. } __nl2wmi[] = {
  83. {NL80211_IFTYPE_ADHOC, WMI_NETTYPE_ADHOC},
  84. {NL80211_IFTYPE_STATION, WMI_NETTYPE_INFRA},
  85. {NL80211_IFTYPE_AP, WMI_NETTYPE_AP},
  86. {NL80211_IFTYPE_P2P_CLIENT, WMI_NETTYPE_P2P},
  87. {NL80211_IFTYPE_P2P_GO, WMI_NETTYPE_P2P},
  88. {NL80211_IFTYPE_MONITOR, WMI_NETTYPE_ADHOC}, /* FIXME */
  89. };
  90. uint i;
  91. for (i = 0; i < ARRAY_SIZE(__nl2wmi); i++) {
  92. if (__nl2wmi[i].nl == type)
  93. return __nl2wmi[i].wmi;
  94. }
  95. return -EOPNOTSUPP;
  96. }
  97. static int wil_cfg80211_get_station(struct wiphy *wiphy,
  98. struct net_device *ndev,
  99. u8 *mac, struct station_info *sinfo)
  100. {
  101. struct wil6210_priv *wil = wiphy_to_wil(wiphy);
  102. int rc;
  103. struct wmi_notify_req_cmd cmd = {
  104. .cid = 0,
  105. .interval_usec = 0,
  106. };
  107. if (memcmp(mac, wil->dst_addr[0], ETH_ALEN))
  108. return -ENOENT;
  109. /* WMI_NOTIFY_REQ_DONE_EVENTID handler fills wil->stats.bf_mcs */
  110. rc = wmi_call(wil, WMI_NOTIFY_REQ_CMDID, &cmd, sizeof(cmd),
  111. WMI_NOTIFY_REQ_DONE_EVENTID, NULL, 0, 20);
  112. if (rc)
  113. return rc;
  114. sinfo->generation = wil->sinfo_gen;
  115. sinfo->filled |= STATION_INFO_TX_BITRATE;
  116. sinfo->txrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
  117. sinfo->txrate.mcs = wil->stats.bf_mcs;
  118. sinfo->filled |= STATION_INFO_RX_BITRATE;
  119. sinfo->rxrate.flags = RATE_INFO_FLAGS_MCS | RATE_INFO_FLAGS_60G;
  120. sinfo->rxrate.mcs = wil->stats.last_mcs_rx;
  121. if (test_bit(wil_status_fwconnected, &wil->status)) {
  122. sinfo->filled |= STATION_INFO_SIGNAL;
  123. sinfo->signal = 12; /* TODO: provide real value */
  124. }
  125. return 0;
  126. }
  127. static int wil_cfg80211_change_iface(struct wiphy *wiphy,
  128. struct net_device *ndev,
  129. enum nl80211_iftype type, u32 *flags,
  130. struct vif_params *params)
  131. {
  132. struct wil6210_priv *wil = wiphy_to_wil(wiphy);
  133. struct wireless_dev *wdev = wil->wdev;
  134. switch (type) {
  135. case NL80211_IFTYPE_STATION:
  136. case NL80211_IFTYPE_AP:
  137. case NL80211_IFTYPE_P2P_CLIENT:
  138. case NL80211_IFTYPE_P2P_GO:
  139. break;
  140. case NL80211_IFTYPE_MONITOR:
  141. if (flags)
  142. wil->monitor_flags = *flags;
  143. else
  144. wil->monitor_flags = 0;
  145. break;
  146. default:
  147. return -EOPNOTSUPP;
  148. }
  149. wdev->iftype = type;
  150. return 0;
  151. }
  152. static int wil_cfg80211_scan(struct wiphy *wiphy,
  153. struct cfg80211_scan_request *request)
  154. {
  155. struct wil6210_priv *wil = wiphy_to_wil(wiphy);
  156. struct wireless_dev *wdev = wil->wdev;
  157. struct {
  158. struct wmi_start_scan_cmd cmd;
  159. u16 chnl[4];
  160. } __packed cmd;
  161. uint i, n;
  162. if (wil->scan_request) {
  163. wil_err(wil, "Already scanning\n");
  164. return -EAGAIN;
  165. }
  166. /* check we are client side */
  167. switch (wdev->iftype) {
  168. case NL80211_IFTYPE_STATION:
  169. case NL80211_IFTYPE_P2P_CLIENT:
  170. break;
  171. default:
  172. return -EOPNOTSUPP;
  173. }
  174. /* FW don't support scan after connection attempt */
  175. if (test_bit(wil_status_dontscan, &wil->status)) {
  176. wil_err(wil, "Scan after connect attempt not supported\n");
  177. return -EBUSY;
  178. }
  179. wil->scan_request = request;
  180. memset(&cmd, 0, sizeof(cmd));
  181. cmd.cmd.num_channels = 0;
  182. n = min(request->n_channels, 4U);
  183. for (i = 0; i < n; i++) {
  184. int ch = request->channels[i]->hw_value;
  185. if (ch == 0) {
  186. wil_err(wil,
  187. "Scan requested for unknown frequency %dMhz\n",
  188. request->channels[i]->center_freq);
  189. continue;
  190. }
  191. /* 0-based channel indexes */
  192. cmd.cmd.channel_list[cmd.cmd.num_channels++].channel = ch - 1;
  193. wil_dbg_misc(wil, "Scan for ch %d : %d MHz\n", ch,
  194. request->channels[i]->center_freq);
  195. }
  196. return wmi_send(wil, WMI_START_SCAN_CMDID, &cmd, sizeof(cmd.cmd) +
  197. cmd.cmd.num_channels * sizeof(cmd.cmd.channel_list[0]));
  198. }
  199. static int wil_cfg80211_connect(struct wiphy *wiphy,
  200. struct net_device *ndev,
  201. struct cfg80211_connect_params *sme)
  202. {
  203. struct wil6210_priv *wil = wiphy_to_wil(wiphy);
  204. struct cfg80211_bss *bss;
  205. struct wmi_connect_cmd conn;
  206. const u8 *ssid_eid;
  207. const u8 *rsn_eid;
  208. int ch;
  209. int rc = 0;
  210. bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
  211. sme->ssid, sme->ssid_len,
  212. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  213. if (!bss) {
  214. wil_err(wil, "Unable to find BSS\n");
  215. return -ENOENT;
  216. }
  217. ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
  218. if (!ssid_eid) {
  219. wil_err(wil, "No SSID\n");
  220. rc = -ENOENT;
  221. goto out;
  222. }
  223. rsn_eid = sme->ie ?
  224. cfg80211_find_ie(WLAN_EID_RSN, sme->ie, sme->ie_len) :
  225. NULL;
  226. if (rsn_eid) {
  227. if (sme->ie_len > WMI_MAX_IE_LEN) {
  228. rc = -ERANGE;
  229. wil_err(wil, "IE too large (%td bytes)\n",
  230. sme->ie_len);
  231. goto out;
  232. }
  233. /*
  234. * For secure assoc, send:
  235. * (1) WMI_DELETE_CIPHER_KEY_CMD
  236. * (2) WMI_SET_APPIE_CMD
  237. */
  238. rc = wmi_del_cipher_key(wil, 0, bss->bssid);
  239. if (rc) {
  240. wil_err(wil, "WMI_DELETE_CIPHER_KEY_CMD failed\n");
  241. goto out;
  242. }
  243. /* WMI_SET_APPIE_CMD */
  244. rc = wmi_set_ie(wil, WMI_FRAME_ASSOC_REQ, sme->ie_len, sme->ie);
  245. if (rc) {
  246. wil_err(wil, "WMI_SET_APPIE_CMD failed\n");
  247. goto out;
  248. }
  249. }
  250. /* WMI_CONNECT_CMD */
  251. memset(&conn, 0, sizeof(conn));
  252. switch (bss->capability & WLAN_CAPABILITY_DMG_TYPE_MASK) {
  253. case WLAN_CAPABILITY_DMG_TYPE_AP:
  254. conn.network_type = WMI_NETTYPE_INFRA;
  255. break;
  256. case WLAN_CAPABILITY_DMG_TYPE_PBSS:
  257. conn.network_type = WMI_NETTYPE_P2P;
  258. break;
  259. default:
  260. wil_err(wil, "Unsupported BSS type, capability= 0x%04x\n",
  261. bss->capability);
  262. goto out;
  263. }
  264. if (rsn_eid) {
  265. conn.dot11_auth_mode = WMI_AUTH11_SHARED;
  266. conn.auth_mode = WMI_AUTH_WPA2_PSK;
  267. conn.pairwise_crypto_type = WMI_CRYPT_AES_GCMP;
  268. conn.pairwise_crypto_len = 16;
  269. } else {
  270. conn.dot11_auth_mode = WMI_AUTH11_OPEN;
  271. conn.auth_mode = WMI_AUTH_NONE;
  272. }
  273. conn.ssid_len = min_t(u8, ssid_eid[1], 32);
  274. memcpy(conn.ssid, ssid_eid+2, conn.ssid_len);
  275. ch = bss->channel->hw_value;
  276. if (ch == 0) {
  277. wil_err(wil, "BSS at unknown frequency %dMhz\n",
  278. bss->channel->center_freq);
  279. rc = -EOPNOTSUPP;
  280. goto out;
  281. }
  282. conn.channel = ch - 1;
  283. memcpy(conn.bssid, bss->bssid, 6);
  284. memcpy(conn.dst_mac, bss->bssid, 6);
  285. /*
  286. * FW don't support scan after connection attempt
  287. */
  288. set_bit(wil_status_dontscan, &wil->status);
  289. rc = wmi_send(wil, WMI_CONNECT_CMDID, &conn, sizeof(conn));
  290. if (rc == 0) {
  291. /* Connect can take lots of time */
  292. mod_timer(&wil->connect_timer,
  293. jiffies + msecs_to_jiffies(2000));
  294. }
  295. out:
  296. cfg80211_put_bss(wiphy, bss);
  297. return rc;
  298. }
  299. static int wil_cfg80211_disconnect(struct wiphy *wiphy,
  300. struct net_device *ndev,
  301. u16 reason_code)
  302. {
  303. int rc;
  304. struct wil6210_priv *wil = wiphy_to_wil(wiphy);
  305. rc = wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
  306. return rc;
  307. }
  308. static int wil_cfg80211_set_channel(struct wiphy *wiphy,
  309. struct cfg80211_chan_def *chandef)
  310. {
  311. struct wil6210_priv *wil = wiphy_to_wil(wiphy);
  312. struct wireless_dev *wdev = wil->wdev;
  313. wdev->preset_chandef = *chandef;
  314. return 0;
  315. }
  316. static int wil_cfg80211_add_key(struct wiphy *wiphy,
  317. struct net_device *ndev,
  318. u8 key_index, bool pairwise,
  319. const u8 *mac_addr,
  320. struct key_params *params)
  321. {
  322. struct wil6210_priv *wil = wiphy_to_wil(wiphy);
  323. /* group key is not used */
  324. if (!pairwise)
  325. return 0;
  326. return wmi_add_cipher_key(wil, key_index, mac_addr,
  327. params->key_len, params->key);
  328. }
  329. static int wil_cfg80211_del_key(struct wiphy *wiphy,
  330. struct net_device *ndev,
  331. u8 key_index, bool pairwise,
  332. const u8 *mac_addr)
  333. {
  334. struct wil6210_priv *wil = wiphy_to_wil(wiphy);
  335. /* group key is not used */
  336. if (!pairwise)
  337. return 0;
  338. return wmi_del_cipher_key(wil, key_index, mac_addr);
  339. }
  340. /* Need to be present or wiphy_new() will WARN */
  341. static int wil_cfg80211_set_default_key(struct wiphy *wiphy,
  342. struct net_device *ndev,
  343. u8 key_index, bool unicast,
  344. bool multicast)
  345. {
  346. return 0;
  347. }
  348. static int wil_cfg80211_start_ap(struct wiphy *wiphy,
  349. struct net_device *ndev,
  350. struct cfg80211_ap_settings *info)
  351. {
  352. int rc = 0;
  353. struct wil6210_priv *wil = wiphy_to_wil(wiphy);
  354. struct wireless_dev *wdev = ndev->ieee80211_ptr;
  355. struct ieee80211_channel *channel = info->chandef.chan;
  356. struct cfg80211_beacon_data *bcon = &info->beacon;
  357. u8 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
  358. if (!channel) {
  359. wil_err(wil, "AP: No channel???\n");
  360. return -EINVAL;
  361. }
  362. wil_dbg_misc(wil, "AP on Channel %d %d MHz, %s\n", channel->hw_value,
  363. channel->center_freq, info->privacy ? "secure" : "open");
  364. print_hex_dump_bytes("SSID ", DUMP_PREFIX_OFFSET,
  365. info->ssid, info->ssid_len);
  366. rc = wil_reset(wil);
  367. if (rc)
  368. return rc;
  369. rc = wmi_set_ssid(wil, info->ssid_len, info->ssid);
  370. if (rc)
  371. return rc;
  372. /* MAC address - pre-requisite for other commands */
  373. wmi_set_mac_address(wil, ndev->dev_addr);
  374. /* IE's */
  375. /* bcon 'head IE's are not relevant for 60g band */
  376. /*
  377. * FW do not form regular beacon, so bcon IE's are not set
  378. * For the DMG bcon, when it will be supported, bcon IE's will
  379. * be reused; add something like:
  380. * wmi_set_ie(wil, WMI_FRAME_BEACON, bcon->beacon_ies_len,
  381. * bcon->beacon_ies);
  382. */
  383. wmi_set_ie(wil, WMI_FRAME_PROBE_RESP, bcon->proberesp_ies_len,
  384. bcon->proberesp_ies);
  385. wmi_set_ie(wil, WMI_FRAME_ASSOC_RESP, bcon->assocresp_ies_len,
  386. bcon->assocresp_ies);
  387. wil->secure_pcp = info->privacy;
  388. rc = wmi_pcp_start(wil, info->beacon_interval, wmi_nettype,
  389. channel->hw_value);
  390. if (rc)
  391. return rc;
  392. /* Rx VRING. After MAC and beacon */
  393. rc = wil_rx_init(wil);
  394. netif_carrier_on(ndev);
  395. return rc;
  396. }
  397. static int wil_cfg80211_stop_ap(struct wiphy *wiphy,
  398. struct net_device *ndev)
  399. {
  400. int rc = 0;
  401. struct wil6210_priv *wil = wiphy_to_wil(wiphy);
  402. rc = wmi_pcp_stop(wil);
  403. return rc;
  404. }
  405. static struct cfg80211_ops wil_cfg80211_ops = {
  406. .scan = wil_cfg80211_scan,
  407. .connect = wil_cfg80211_connect,
  408. .disconnect = wil_cfg80211_disconnect,
  409. .change_virtual_intf = wil_cfg80211_change_iface,
  410. .get_station = wil_cfg80211_get_station,
  411. .set_monitor_channel = wil_cfg80211_set_channel,
  412. .add_key = wil_cfg80211_add_key,
  413. .del_key = wil_cfg80211_del_key,
  414. .set_default_key = wil_cfg80211_set_default_key,
  415. /* AP mode */
  416. .start_ap = wil_cfg80211_start_ap,
  417. .stop_ap = wil_cfg80211_stop_ap,
  418. };
  419. static void wil_wiphy_init(struct wiphy *wiphy)
  420. {
  421. /* TODO: set real value */
  422. wiphy->max_scan_ssids = 10;
  423. wiphy->max_num_pmkids = 0 /* TODO: */;
  424. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  425. BIT(NL80211_IFTYPE_AP) |
  426. BIT(NL80211_IFTYPE_MONITOR);
  427. /* TODO: enable P2P when integrated with supplicant:
  428. * BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO)
  429. */
  430. wiphy->flags |= WIPHY_FLAG_HAVE_AP_SME |
  431. WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
  432. dev_warn(wiphy_dev(wiphy), "%s : flags = 0x%08x\n",
  433. __func__, wiphy->flags);
  434. wiphy->probe_resp_offload =
  435. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
  436. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
  437. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
  438. wiphy->bands[IEEE80211_BAND_60GHZ] = &wil_band_60ghz;
  439. /* TODO: figure this out */
  440. wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  441. wiphy->cipher_suites = wil_cipher_suites;
  442. wiphy->n_cipher_suites = ARRAY_SIZE(wil_cipher_suites);
  443. wiphy->mgmt_stypes = wil_mgmt_stypes;
  444. }
  445. struct wireless_dev *wil_cfg80211_init(struct device *dev)
  446. {
  447. int rc = 0;
  448. struct wireless_dev *wdev;
  449. wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
  450. if (!wdev)
  451. return ERR_PTR(-ENOMEM);
  452. wdev->wiphy = wiphy_new(&wil_cfg80211_ops,
  453. sizeof(struct wil6210_priv));
  454. if (!wdev->wiphy) {
  455. rc = -ENOMEM;
  456. goto out;
  457. }
  458. set_wiphy_dev(wdev->wiphy, dev);
  459. wil_wiphy_init(wdev->wiphy);
  460. rc = wiphy_register(wdev->wiphy);
  461. if (rc < 0)
  462. goto out_failed_reg;
  463. return wdev;
  464. out_failed_reg:
  465. wiphy_free(wdev->wiphy);
  466. out:
  467. kfree(wdev);
  468. return ERR_PTR(rc);
  469. }
  470. void wil_wdev_free(struct wil6210_priv *wil)
  471. {
  472. struct wireless_dev *wdev = wil_to_wdev(wil);
  473. if (!wdev)
  474. return;
  475. wiphy_unregister(wdev->wiphy);
  476. wiphy_free(wdev->wiphy);
  477. kfree(wdev);
  478. }