cmd.c revision 978cd3a0b82969c1f85942e208f8a00412964ef6
1/* 2 * This file is part of wl1271 3 * 4 * Copyright (C) 2009-2010 Nokia Corporation 5 * 6 * Contact: Luciano Coelho <luciano.coelho@nokia.com> 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * version 2 as published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 20 * 02110-1301 USA 21 * 22 */ 23 24#include <linux/module.h> 25#include <linux/platform_device.h> 26#include <linux/spi/spi.h> 27#include <linux/etherdevice.h> 28#include <linux/ieee80211.h> 29#include <linux/slab.h> 30 31#include "wlcore.h" 32#include "debug.h" 33#include "io.h" 34#include "acx.h" 35#include "wl12xx_80211.h" 36#include "cmd.h" 37#include "event.h" 38#include "tx.h" 39#include "hw_ops.h" 40 41#define WL1271_CMD_FAST_POLL_COUNT 50 42#define WL1271_WAIT_EVENT_FAST_POLL_COUNT 20 43 44/* 45 * send command to firmware 46 * 47 * @wl: wl struct 48 * @id: command id 49 * @buf: buffer containing the command, must work with dma 50 * @len: length of the buffer 51 */ 52int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len, 53 size_t res_len) 54{ 55 struct wl1271_cmd_header *cmd; 56 unsigned long timeout; 57 u32 intr; 58 int ret = 0; 59 u16 status; 60 u16 poll_count = 0; 61 62 if (WARN_ON(unlikely(wl->state == WLCORE_STATE_RESTARTING))) 63 return -EIO; 64 65 cmd = buf; 66 cmd->id = cpu_to_le16(id); 67 cmd->status = 0; 68 69 WARN_ON(len % 4 != 0); 70 WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags)); 71 72 ret = wlcore_write(wl, wl->cmd_box_addr, buf, len, false); 73 if (ret < 0) 74 goto fail; 75 76 /* 77 * TODO: we just need this because one bit is in a different 78 * place. Is there any better way? 79 */ 80 ret = wl->ops->trigger_cmd(wl, wl->cmd_box_addr, buf, len); 81 if (ret < 0) 82 goto fail; 83 84 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT); 85 86 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr); 87 if (ret < 0) 88 goto fail; 89 90 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) { 91 if (time_after(jiffies, timeout)) { 92 wl1271_error("command complete timeout"); 93 ret = -ETIMEDOUT; 94 goto fail; 95 } 96 97 poll_count++; 98 if (poll_count < WL1271_CMD_FAST_POLL_COUNT) 99 udelay(10); 100 else 101 msleep(1); 102 103 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr); 104 if (ret < 0) 105 goto fail; 106 } 107 108 /* read back the status code of the command */ 109 if (res_len == 0) 110 res_len = sizeof(struct wl1271_cmd_header); 111 112 ret = wlcore_read(wl, wl->cmd_box_addr, cmd, res_len, false); 113 if (ret < 0) 114 goto fail; 115 116 status = le16_to_cpu(cmd->status); 117 if (status != CMD_STATUS_SUCCESS) { 118 wl1271_error("command execute failure %d", status); 119 ret = -EIO; 120 goto fail; 121 } 122 123 ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK, 124 WL1271_ACX_INTR_CMD_COMPLETE); 125 if (ret < 0) 126 goto fail; 127 128 return 0; 129 130fail: 131 wl12xx_queue_recovery_work(wl); 132 return ret; 133} 134EXPORT_SYMBOL_GPL(wl1271_cmd_send); 135 136/* 137 * Poll the mailbox event field until any of the bits in the mask is set or a 138 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs) 139 */ 140int wlcore_cmd_wait_for_event_or_timeout(struct wl1271 *wl, 141 u32 mask, bool *timeout) 142{ 143 u32 *events_vector; 144 u32 event; 145 unsigned long timeout_time; 146 u16 poll_count = 0; 147 int ret = 0; 148 149 *timeout = false; 150 151 events_vector = kmalloc(sizeof(*events_vector), GFP_KERNEL | GFP_DMA); 152 if (!events_vector) 153 return -ENOMEM; 154 155 timeout_time = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT); 156 157 do { 158 if (time_after(jiffies, timeout_time)) { 159 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d", 160 (int)mask); 161 *timeout = true; 162 goto out; 163 } 164 165 poll_count++; 166 if (poll_count < WL1271_WAIT_EVENT_FAST_POLL_COUNT) 167 usleep_range(50, 51); 168 else 169 usleep_range(1000, 5000); 170 171 /* read from both event fields */ 172 ret = wlcore_read(wl, wl->mbox_ptr[0], events_vector, 173 sizeof(*events_vector), false); 174 if (ret < 0) 175 goto out; 176 177 event = *events_vector & mask; 178 179 ret = wlcore_read(wl, wl->mbox_ptr[1], events_vector, 180 sizeof(*events_vector), false); 181 if (ret < 0) 182 goto out; 183 184 event |= *events_vector & mask; 185 } while (!event); 186 187out: 188 kfree(events_vector); 189 return ret; 190} 191EXPORT_SYMBOL_GPL(wlcore_cmd_wait_for_event_or_timeout); 192 193int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type, 194 u8 *role_id) 195{ 196 struct wl12xx_cmd_role_enable *cmd; 197 int ret; 198 199 wl1271_debug(DEBUG_CMD, "cmd role enable"); 200 201 if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID)) 202 return -EBUSY; 203 204 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 205 if (!cmd) { 206 ret = -ENOMEM; 207 goto out; 208 } 209 210 /* get role id */ 211 cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES); 212 if (cmd->role_id >= WL12XX_MAX_ROLES) { 213 ret = -EBUSY; 214 goto out_free; 215 } 216 217 memcpy(cmd->mac_address, addr, ETH_ALEN); 218 cmd->role_type = role_type; 219 220 ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0); 221 if (ret < 0) { 222 wl1271_error("failed to initiate cmd role enable"); 223 goto out_free; 224 } 225 226 __set_bit(cmd->role_id, wl->roles_map); 227 *role_id = cmd->role_id; 228 229out_free: 230 kfree(cmd); 231 232out: 233 return ret; 234} 235 236int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id) 237{ 238 struct wl12xx_cmd_role_disable *cmd; 239 int ret; 240 241 wl1271_debug(DEBUG_CMD, "cmd role disable"); 242 243 if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID)) 244 return -ENOENT; 245 246 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 247 if (!cmd) { 248 ret = -ENOMEM; 249 goto out; 250 } 251 cmd->role_id = *role_id; 252 253 ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0); 254 if (ret < 0) { 255 wl1271_error("failed to initiate cmd role disable"); 256 goto out_free; 257 } 258 259 __clear_bit(*role_id, wl->roles_map); 260 *role_id = WL12XX_INVALID_ROLE_ID; 261 262out_free: 263 kfree(cmd); 264 265out: 266 return ret; 267} 268 269static int wlcore_get_new_session_id(struct wl1271 *wl, u8 hlid) 270{ 271 if (wl->session_ids[hlid] >= SESSION_COUNTER_MAX) 272 wl->session_ids[hlid] = 0; 273 274 wl->session_ids[hlid]++; 275 276 return wl->session_ids[hlid]; 277} 278 279int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid) 280{ 281 unsigned long flags; 282 u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS); 283 if (link >= WL12XX_MAX_LINKS) 284 return -EBUSY; 285 286 wl->session_ids[link] = wlcore_get_new_session_id(wl, link); 287 288 /* these bits are used by op_tx */ 289 spin_lock_irqsave(&wl->wl_lock, flags); 290 __set_bit(link, wl->links_map); 291 __set_bit(link, wlvif->links_map); 292 spin_unlock_irqrestore(&wl->wl_lock, flags); 293 *hlid = link; 294 return 0; 295} 296 297void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid) 298{ 299 unsigned long flags; 300 301 if (*hlid == WL12XX_INVALID_LINK_ID) 302 return; 303 304 /* these bits are used by op_tx */ 305 spin_lock_irqsave(&wl->wl_lock, flags); 306 __clear_bit(*hlid, wl->links_map); 307 __clear_bit(*hlid, wlvif->links_map); 308 spin_unlock_irqrestore(&wl->wl_lock, flags); 309 310 /* 311 * At this point op_tx() will not add more packets to the queues. We 312 * can purge them. 313 */ 314 wl1271_tx_reset_link_queues(wl, *hlid); 315 316 *hlid = WL12XX_INVALID_LINK_ID; 317} 318 319static u8 wlcore_get_native_channel_type(u8 nl_channel_type) 320{ 321 switch (nl_channel_type) { 322 case NL80211_CHAN_NO_HT: 323 return WLCORE_CHAN_NO_HT; 324 case NL80211_CHAN_HT20: 325 return WLCORE_CHAN_HT20; 326 case NL80211_CHAN_HT40MINUS: 327 return WLCORE_CHAN_HT40MINUS; 328 case NL80211_CHAN_HT40PLUS: 329 return WLCORE_CHAN_HT40PLUS; 330 default: 331 WARN_ON(1); 332 return WLCORE_CHAN_NO_HT; 333 } 334} 335 336static int wl12xx_cmd_role_start_dev(struct wl1271 *wl, 337 struct wl12xx_vif *wlvif, 338 enum ieee80211_band band, 339 int channel) 340{ 341 struct wl12xx_cmd_role_start *cmd; 342 int ret; 343 344 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 345 if (!cmd) { 346 ret = -ENOMEM; 347 goto out; 348 } 349 350 wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id); 351 352 cmd->role_id = wlvif->dev_role_id; 353 if (band == IEEE80211_BAND_5GHZ) 354 cmd->band = WLCORE_BAND_5GHZ; 355 cmd->channel = channel; 356 357 if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) { 358 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid); 359 if (ret) 360 goto out_free; 361 } 362 cmd->device.hlid = wlvif->dev_hlid; 363 cmd->device.session = wl->session_ids[wlvif->dev_hlid]; 364 365 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d", 366 cmd->role_id, cmd->device.hlid, cmd->device.session); 367 368 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0); 369 if (ret < 0) { 370 wl1271_error("failed to initiate cmd role enable"); 371 goto err_hlid; 372 } 373 374 goto out_free; 375 376err_hlid: 377 /* clear links on error */ 378 wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid); 379 380out_free: 381 kfree(cmd); 382 383out: 384 return ret; 385} 386 387static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl, 388 struct wl12xx_vif *wlvif) 389{ 390 struct wl12xx_cmd_role_stop *cmd; 391 int ret; 392 393 if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID)) 394 return -EINVAL; 395 396 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 397 if (!cmd) { 398 ret = -ENOMEM; 399 goto out; 400 } 401 402 wl1271_debug(DEBUG_CMD, "cmd role stop dev"); 403 404 cmd->role_id = wlvif->dev_role_id; 405 cmd->disc_type = DISCONNECT_IMMEDIATE; 406 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED); 407 408 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0); 409 if (ret < 0) { 410 wl1271_error("failed to initiate cmd role stop"); 411 goto out_free; 412 } 413 414 wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid); 415 416out_free: 417 kfree(cmd); 418 419out: 420 return ret; 421} 422 423int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif) 424{ 425 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif); 426 struct wl12xx_cmd_role_start *cmd; 427 u32 supported_rates; 428 int ret; 429 430 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 431 if (!cmd) { 432 ret = -ENOMEM; 433 goto out; 434 } 435 436 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id); 437 438 cmd->role_id = wlvif->role_id; 439 if (wlvif->band == IEEE80211_BAND_5GHZ) 440 cmd->band = WLCORE_BAND_5GHZ; 441 cmd->channel = wlvif->channel; 442 cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set); 443 cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int); 444 cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY; 445 cmd->sta.ssid_len = wlvif->ssid_len; 446 memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len); 447 memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN); 448 449 supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES | 450 wlcore_hw_sta_get_ap_rate_mask(wl, wlvif); 451 if (wlvif->p2p) 452 supported_rates &= ~CONF_TX_CCK_RATES; 453 454 cmd->sta.local_rates = cpu_to_le32(supported_rates); 455 456 cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type); 457 458 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) { 459 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid); 460 if (ret) 461 goto out_free; 462 } 463 cmd->sta.hlid = wlvif->sta.hlid; 464 cmd->sta.session = wl->session_ids[wlvif->sta.hlid]; 465 /* 466 * We don't have the correct remote rates in this stage, and there 467 * is no way to update them later, so use our supported rates instead. 468 * The fw will take the configured rate policies into account anyway. 469 */ 470 cmd->sta.remote_rates = cpu_to_le32(supported_rates); 471 472 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d " 473 "basic_rate_set: 0x%x, remote_rates: 0x%x", 474 wlvif->role_id, cmd->sta.hlid, cmd->sta.session, 475 wlvif->basic_rate_set, wlvif->rate_set); 476 477 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0); 478 if (ret < 0) { 479 wl1271_error("failed to initiate cmd role start sta"); 480 goto err_hlid; 481 } 482 483 goto out_free; 484 485err_hlid: 486 /* clear links on error. */ 487 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid); 488 489out_free: 490 kfree(cmd); 491 492out: 493 return ret; 494} 495 496/* use this function to stop ibss as well */ 497int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif) 498{ 499 struct wl12xx_cmd_role_stop *cmd; 500 int ret; 501 502 if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)) 503 return -EINVAL; 504 505 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 506 if (!cmd) { 507 ret = -ENOMEM; 508 goto out; 509 } 510 511 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id); 512 513 cmd->role_id = wlvif->role_id; 514 cmd->disc_type = DISCONNECT_IMMEDIATE; 515 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED); 516 517 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0); 518 if (ret < 0) { 519 wl1271_error("failed to initiate cmd role stop sta"); 520 goto out_free; 521 } 522 523 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid); 524 525out_free: 526 kfree(cmd); 527 528out: 529 return ret; 530} 531 532int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif) 533{ 534 struct wl12xx_cmd_role_start *cmd; 535 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif); 536 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf; 537 u32 supported_rates; 538 int ret; 539 540 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id); 541 542 /* trying to use hidden SSID with an old hostapd version */ 543 if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) { 544 wl1271_error("got a null SSID from beacon/bss"); 545 ret = -EINVAL; 546 goto out; 547 } 548 549 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 550 if (!cmd) { 551 ret = -ENOMEM; 552 goto out; 553 } 554 555 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid); 556 if (ret < 0) 557 goto out_free; 558 559 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid); 560 if (ret < 0) 561 goto out_free_global; 562 563 cmd->role_id = wlvif->role_id; 564 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period); 565 cmd->ap.bss_index = WL1271_AP_BSS_INDEX; 566 cmd->ap.global_hlid = wlvif->ap.global_hlid; 567 cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid; 568 cmd->ap.global_session_id = wl->session_ids[wlvif->ap.global_hlid]; 569 cmd->ap.bcast_session_id = wl->session_ids[wlvif->ap.bcast_hlid]; 570 cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set); 571 cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int); 572 cmd->ap.dtim_interval = bss_conf->dtim_period; 573 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP; 574 /* FIXME: Change when adding DFS */ 575 cmd->ap.reset_tsf = 1; /* By default reset AP TSF */ 576 cmd->ap.wmm = wlvif->wmm_enabled; 577 cmd->channel = wlvif->channel; 578 cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type); 579 580 if (!bss_conf->hidden_ssid) { 581 /* take the SSID from the beacon for backward compatibility */ 582 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC; 583 cmd->ap.ssid_len = wlvif->ssid_len; 584 memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len); 585 } else { 586 cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN; 587 cmd->ap.ssid_len = bss_conf->ssid_len; 588 memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len); 589 } 590 591 supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES | 592 wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif); 593 594 wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x", 595 supported_rates); 596 597 cmd->ap.local_rates = cpu_to_le32(supported_rates); 598 599 switch (wlvif->band) { 600 case IEEE80211_BAND_2GHZ: 601 cmd->band = WLCORE_BAND_2_4GHZ; 602 break; 603 case IEEE80211_BAND_5GHZ: 604 cmd->band = WLCORE_BAND_5GHZ; 605 break; 606 default: 607 wl1271_warning("ap start - unknown band: %d", (int)wlvif->band); 608 cmd->band = WLCORE_BAND_2_4GHZ; 609 break; 610 } 611 612 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0); 613 if (ret < 0) { 614 wl1271_error("failed to initiate cmd role start ap"); 615 goto out_free_bcast; 616 } 617 618 goto out_free; 619 620out_free_bcast: 621 wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid); 622 623out_free_global: 624 wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid); 625 626out_free: 627 kfree(cmd); 628 629out: 630 return ret; 631} 632 633int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif) 634{ 635 struct wl12xx_cmd_role_stop *cmd; 636 int ret; 637 638 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 639 if (!cmd) { 640 ret = -ENOMEM; 641 goto out; 642 } 643 644 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id); 645 646 cmd->role_id = wlvif->role_id; 647 648 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0); 649 if (ret < 0) { 650 wl1271_error("failed to initiate cmd role stop ap"); 651 goto out_free; 652 } 653 654 wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid); 655 wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid); 656 657out_free: 658 kfree(cmd); 659 660out: 661 return ret; 662} 663 664int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif) 665{ 666 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif); 667 struct wl12xx_cmd_role_start *cmd; 668 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf; 669 int ret; 670 671 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 672 if (!cmd) { 673 ret = -ENOMEM; 674 goto out; 675 } 676 677 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id); 678 679 cmd->role_id = wlvif->role_id; 680 if (wlvif->band == IEEE80211_BAND_5GHZ) 681 cmd->band = WLCORE_BAND_5GHZ; 682 cmd->channel = wlvif->channel; 683 cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set); 684 cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int); 685 cmd->ibss.dtim_interval = bss_conf->dtim_period; 686 cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY; 687 cmd->ibss.ssid_len = wlvif->ssid_len; 688 memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len); 689 memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN); 690 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set); 691 692 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) { 693 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid); 694 if (ret) 695 goto out_free; 696 } 697 cmd->ibss.hlid = wlvif->sta.hlid; 698 cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set); 699 700 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d " 701 "basic_rate_set: 0x%x, remote_rates: 0x%x", 702 wlvif->role_id, cmd->sta.hlid, cmd->sta.session, 703 wlvif->basic_rate_set, wlvif->rate_set); 704 705 wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM", 706 vif->bss_conf.bssid); 707 708 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0); 709 if (ret < 0) { 710 wl1271_error("failed to initiate cmd role enable"); 711 goto err_hlid; 712 } 713 714 goto out_free; 715 716err_hlid: 717 /* clear links on error. */ 718 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid); 719 720out_free: 721 kfree(cmd); 722 723out: 724 return ret; 725} 726 727 728/** 729 * send test command to firmware 730 * 731 * @wl: wl struct 732 * @buf: buffer containing the command, with all headers, must work with dma 733 * @len: length of the buffer 734 * @answer: is answer needed 735 */ 736int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer) 737{ 738 int ret; 739 size_t res_len = 0; 740 741 wl1271_debug(DEBUG_CMD, "cmd test"); 742 743 if (answer) 744 res_len = buf_len; 745 746 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len); 747 748 if (ret < 0) { 749 wl1271_warning("TEST command failed"); 750 return ret; 751 } 752 753 return ret; 754} 755EXPORT_SYMBOL_GPL(wl1271_cmd_test); 756 757/** 758 * read acx from firmware 759 * 760 * @wl: wl struct 761 * @id: acx id 762 * @buf: buffer for the response, including all headers, must work with dma 763 * @len: length of buf 764 */ 765int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len) 766{ 767 struct acx_header *acx = buf; 768 int ret; 769 770 wl1271_debug(DEBUG_CMD, "cmd interrogate"); 771 772 acx->id = cpu_to_le16(id); 773 774 /* payload length, does not include any headers */ 775 acx->len = cpu_to_le16(len - sizeof(*acx)); 776 777 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len); 778 if (ret < 0) 779 wl1271_error("INTERROGATE command failed"); 780 781 return ret; 782} 783 784/** 785 * write acx value to firmware 786 * 787 * @wl: wl struct 788 * @id: acx id 789 * @buf: buffer containing acx, including all headers, must work with dma 790 * @len: length of buf 791 */ 792int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len) 793{ 794 struct acx_header *acx = buf; 795 int ret; 796 797 wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id); 798 799 acx->id = cpu_to_le16(id); 800 801 /* payload length, does not include any headers */ 802 acx->len = cpu_to_le16(len - sizeof(*acx)); 803 804 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0); 805 if (ret < 0) { 806 wl1271_warning("CONFIGURE command NOK"); 807 return ret; 808 } 809 810 return 0; 811} 812EXPORT_SYMBOL_GPL(wl1271_cmd_configure); 813 814int wl1271_cmd_data_path(struct wl1271 *wl, bool enable) 815{ 816 struct cmd_enabledisable_path *cmd; 817 int ret; 818 u16 cmd_rx, cmd_tx; 819 820 wl1271_debug(DEBUG_CMD, "cmd data path"); 821 822 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 823 if (!cmd) { 824 ret = -ENOMEM; 825 goto out; 826 } 827 828 /* the channel here is only used for calibration, so hardcoded to 1 */ 829 cmd->channel = 1; 830 831 if (enable) { 832 cmd_rx = CMD_ENABLE_RX; 833 cmd_tx = CMD_ENABLE_TX; 834 } else { 835 cmd_rx = CMD_DISABLE_RX; 836 cmd_tx = CMD_DISABLE_TX; 837 } 838 839 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0); 840 if (ret < 0) { 841 wl1271_error("rx %s cmd for channel %d failed", 842 enable ? "start" : "stop", cmd->channel); 843 goto out; 844 } 845 846 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d", 847 enable ? "start" : "stop", cmd->channel); 848 849 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0); 850 if (ret < 0) { 851 wl1271_error("tx %s cmd for channel %d failed", 852 enable ? "start" : "stop", cmd->channel); 853 goto out; 854 } 855 856 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d", 857 enable ? "start" : "stop", cmd->channel); 858 859out: 860 kfree(cmd); 861 return ret; 862} 863EXPORT_SYMBOL_GPL(wl1271_cmd_data_path); 864 865int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif, 866 u8 ps_mode, u16 auto_ps_timeout) 867{ 868 struct wl1271_cmd_ps_params *ps_params = NULL; 869 int ret = 0; 870 871 wl1271_debug(DEBUG_CMD, "cmd set ps mode"); 872 873 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL); 874 if (!ps_params) { 875 ret = -ENOMEM; 876 goto out; 877 } 878 879 ps_params->role_id = wlvif->role_id; 880 ps_params->ps_mode = ps_mode; 881 ps_params->auto_ps_timeout = auto_ps_timeout; 882 883 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params, 884 sizeof(*ps_params), 0); 885 if (ret < 0) { 886 wl1271_error("cmd set_ps_mode failed"); 887 goto out; 888 } 889 890out: 891 kfree(ps_params); 892 return ret; 893} 894 895int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id, 896 u16 template_id, void *buf, size_t buf_len, 897 int index, u32 rates) 898{ 899 struct wl1271_cmd_template_set *cmd; 900 int ret = 0; 901 902 wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)", 903 template_id, role_id); 904 905 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE); 906 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE); 907 908 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 909 if (!cmd) { 910 ret = -ENOMEM; 911 goto out; 912 } 913 914 /* during initialization wlvif is NULL */ 915 cmd->role_id = role_id; 916 cmd->len = cpu_to_le16(buf_len); 917 cmd->template_type = template_id; 918 cmd->enabled_rates = cpu_to_le32(rates); 919 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit; 920 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit; 921 cmd->index = index; 922 923 if (buf) 924 memcpy(cmd->template_data, buf, buf_len); 925 926 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0); 927 if (ret < 0) { 928 wl1271_warning("cmd set_template failed: %d", ret); 929 goto out_free; 930 } 931 932out_free: 933 kfree(cmd); 934 935out: 936 return ret; 937} 938 939int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif) 940{ 941 struct sk_buff *skb = NULL; 942 int size; 943 void *ptr; 944 int ret = -ENOMEM; 945 946 947 if (wlvif->bss_type == BSS_TYPE_IBSS) { 948 size = sizeof(struct wl12xx_null_data_template); 949 ptr = NULL; 950 } else { 951 skb = ieee80211_nullfunc_get(wl->hw, 952 wl12xx_wlvif_to_vif(wlvif)); 953 if (!skb) 954 goto out; 955 size = skb->len; 956 ptr = skb->data; 957 } 958 959 ret = wl1271_cmd_template_set(wl, wlvif->role_id, 960 CMD_TEMPL_NULL_DATA, ptr, size, 0, 961 wlvif->basic_rate); 962 963out: 964 dev_kfree_skb(skb); 965 if (ret) 966 wl1271_warning("cmd buld null data failed %d", ret); 967 968 return ret; 969 970} 971 972int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl, 973 struct wl12xx_vif *wlvif) 974{ 975 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif); 976 struct sk_buff *skb = NULL; 977 int ret = -ENOMEM; 978 979 skb = ieee80211_nullfunc_get(wl->hw, vif); 980 if (!skb) 981 goto out; 982 983 ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV, 984 skb->data, skb->len, 985 wlvif->sta.klv_template_id, 986 wlvif->basic_rate); 987 988out: 989 dev_kfree_skb(skb); 990 if (ret) 991 wl1271_warning("cmd build klv null data failed %d", ret); 992 993 return ret; 994 995} 996 997int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif, 998 u16 aid) 999{ 1000 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif); 1001 struct sk_buff *skb; 1002 int ret = 0; 1003 1004 skb = ieee80211_pspoll_get(wl->hw, vif); 1005 if (!skb) 1006 goto out; 1007 1008 ret = wl1271_cmd_template_set(wl, wlvif->role_id, 1009 CMD_TEMPL_PS_POLL, skb->data, 1010 skb->len, 0, wlvif->basic_rate_set); 1011 1012out: 1013 dev_kfree_skb(skb); 1014 return ret; 1015} 1016 1017int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif, 1018 u8 role_id, u8 band, 1019 const u8 *ssid, size_t ssid_len, 1020 const u8 *ie, size_t ie_len, bool sched_scan) 1021{ 1022 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif); 1023 struct sk_buff *skb; 1024 int ret; 1025 u32 rate; 1026 u16 template_id_2_4 = wl->scan_templ_id_2_4; 1027 u16 template_id_5 = wl->scan_templ_id_5; 1028 1029 skb = ieee80211_probereq_get(wl->hw, vif, ssid, ssid_len, 1030 ie, ie_len); 1031 if (!skb) { 1032 ret = -ENOMEM; 1033 goto out; 1034 } 1035 1036 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len); 1037 1038 if (sched_scan && 1039 (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) { 1040 template_id_2_4 = wl->sched_scan_templ_id_2_4; 1041 template_id_5 = wl->sched_scan_templ_id_5; 1042 } 1043 1044 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]); 1045 if (band == IEEE80211_BAND_2GHZ) 1046 ret = wl1271_cmd_template_set(wl, role_id, 1047 template_id_2_4, 1048 skb->data, skb->len, 0, rate); 1049 else 1050 ret = wl1271_cmd_template_set(wl, role_id, 1051 template_id_5, 1052 skb->data, skb->len, 0, rate); 1053 1054out: 1055 dev_kfree_skb(skb); 1056 return ret; 1057} 1058EXPORT_SYMBOL_GPL(wl12xx_cmd_build_probe_req); 1059 1060struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl, 1061 struct wl12xx_vif *wlvif, 1062 struct sk_buff *skb) 1063{ 1064 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif); 1065 int ret; 1066 u32 rate; 1067 1068 if (!skb) 1069 skb = ieee80211_ap_probereq_get(wl->hw, vif); 1070 if (!skb) 1071 goto out; 1072 1073 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len); 1074 1075 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]); 1076 if (wlvif->band == IEEE80211_BAND_2GHZ) 1077 ret = wl1271_cmd_template_set(wl, wlvif->role_id, 1078 CMD_TEMPL_CFG_PROBE_REQ_2_4, 1079 skb->data, skb->len, 0, rate); 1080 else 1081 ret = wl1271_cmd_template_set(wl, wlvif->role_id, 1082 CMD_TEMPL_CFG_PROBE_REQ_5, 1083 skb->data, skb->len, 0, rate); 1084 1085 if (ret < 0) 1086 wl1271_error("Unable to set ap probe request template."); 1087 1088out: 1089 return skb; 1090} 1091 1092int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif) 1093{ 1094 int ret, extra = 0; 1095 u16 fc; 1096 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif); 1097 struct sk_buff *skb; 1098 struct wl12xx_arp_rsp_template *tmpl; 1099 struct ieee80211_hdr_3addr *hdr; 1100 struct arphdr *arp_hdr; 1101 1102 skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) + 1103 WL1271_EXTRA_SPACE_MAX); 1104 if (!skb) { 1105 wl1271_error("failed to allocate buffer for arp rsp template"); 1106 return -ENOMEM; 1107 } 1108 1109 skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX); 1110 1111 tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl)); 1112 memset(tmpl, 0, sizeof(*tmpl)); 1113 1114 /* llc layer */ 1115 memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header)); 1116 tmpl->llc_type = cpu_to_be16(ETH_P_ARP); 1117 1118 /* arp header */ 1119 arp_hdr = &tmpl->arp_hdr; 1120 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER); 1121 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP); 1122 arp_hdr->ar_hln = ETH_ALEN; 1123 arp_hdr->ar_pln = 4; 1124 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY); 1125 1126 /* arp payload */ 1127 memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN); 1128 tmpl->sender_ip = wlvif->ip_addr; 1129 1130 /* encryption space */ 1131 switch (wlvif->encryption_type) { 1132 case KEY_TKIP: 1133 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE) 1134 extra = WL1271_EXTRA_SPACE_TKIP; 1135 break; 1136 case KEY_AES: 1137 extra = WL1271_EXTRA_SPACE_AES; 1138 break; 1139 case KEY_NONE: 1140 case KEY_WEP: 1141 case KEY_GEM: 1142 extra = 0; 1143 break; 1144 default: 1145 wl1271_warning("Unknown encryption type: %d", 1146 wlvif->encryption_type); 1147 ret = -EINVAL; 1148 goto out; 1149 } 1150 1151 if (extra) { 1152 u8 *space = skb_push(skb, extra); 1153 memset(space, 0, extra); 1154 } 1155 1156 /* QoS header - BE */ 1157 if (wlvif->sta.qos) 1158 memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16)); 1159 1160 /* mac80211 header */ 1161 hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr)); 1162 memset(hdr, 0, sizeof(*hdr)); 1163 fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS; 1164 if (wlvif->sta.qos) 1165 fc |= IEEE80211_STYPE_QOS_DATA; 1166 else 1167 fc |= IEEE80211_STYPE_DATA; 1168 if (wlvif->encryption_type != KEY_NONE) 1169 fc |= IEEE80211_FCTL_PROTECTED; 1170 1171 hdr->frame_control = cpu_to_le16(fc); 1172 memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN); 1173 memcpy(hdr->addr2, vif->addr, ETH_ALEN); 1174 memset(hdr->addr3, 0xff, ETH_ALEN); 1175 1176 ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP, 1177 skb->data, skb->len, 0, 1178 wlvif->basic_rate); 1179out: 1180 dev_kfree_skb(skb); 1181 return ret; 1182} 1183 1184int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif) 1185{ 1186 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif); 1187 struct ieee80211_qos_hdr template; 1188 1189 memset(&template, 0, sizeof(template)); 1190 1191 memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN); 1192 memcpy(template.addr2, vif->addr, ETH_ALEN); 1193 memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN); 1194 1195 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA | 1196 IEEE80211_STYPE_QOS_NULLFUNC | 1197 IEEE80211_FCTL_TODS); 1198 1199 /* FIXME: not sure what priority to use here */ 1200 template.qos_ctrl = cpu_to_le16(0); 1201 1202 return wl1271_cmd_template_set(wl, wlvif->role_id, 1203 CMD_TEMPL_QOS_NULL_DATA, &template, 1204 sizeof(template), 0, 1205 wlvif->basic_rate); 1206} 1207 1208int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid) 1209{ 1210 struct wl1271_cmd_set_keys *cmd; 1211 int ret = 0; 1212 1213 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id); 1214 1215 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1216 if (!cmd) { 1217 ret = -ENOMEM; 1218 goto out; 1219 } 1220 1221 cmd->hlid = hlid; 1222 cmd->key_id = id; 1223 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE; 1224 cmd->key_action = cpu_to_le16(KEY_SET_ID); 1225 cmd->key_type = KEY_WEP; 1226 1227 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0); 1228 if (ret < 0) { 1229 wl1271_warning("cmd set_default_wep_key failed: %d", ret); 1230 goto out; 1231 } 1232 1233out: 1234 kfree(cmd); 1235 1236 return ret; 1237} 1238 1239int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif, 1240 u16 action, u8 id, u8 key_type, 1241 u8 key_size, const u8 *key, const u8 *addr, 1242 u32 tx_seq_32, u16 tx_seq_16) 1243{ 1244 struct wl1271_cmd_set_keys *cmd; 1245 int ret = 0; 1246 1247 /* hlid might have already been deleted */ 1248 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) 1249 return 0; 1250 1251 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1252 if (!cmd) { 1253 ret = -ENOMEM; 1254 goto out; 1255 } 1256 1257 cmd->hlid = wlvif->sta.hlid; 1258 1259 if (key_type == KEY_WEP) 1260 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE; 1261 else if (is_broadcast_ether_addr(addr)) 1262 cmd->lid_key_type = BROADCAST_LID_TYPE; 1263 else 1264 cmd->lid_key_type = UNICAST_LID_TYPE; 1265 1266 cmd->key_action = cpu_to_le16(action); 1267 cmd->key_size = key_size; 1268 cmd->key_type = key_type; 1269 1270 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16); 1271 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32); 1272 1273 cmd->key_id = id; 1274 1275 if (key_type == KEY_TKIP) { 1276 /* 1277 * We get the key in the following form: 1278 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes) 1279 * but the target is expecting: 1280 * TKIP - RX MIC - TX MIC 1281 */ 1282 memcpy(cmd->key, key, 16); 1283 memcpy(cmd->key + 16, key + 24, 8); 1284 memcpy(cmd->key + 24, key + 16, 8); 1285 1286 } else { 1287 memcpy(cmd->key, key, key_size); 1288 } 1289 1290 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd)); 1291 1292 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0); 1293 if (ret < 0) { 1294 wl1271_warning("could not set keys"); 1295 goto out; 1296 } 1297 1298out: 1299 kfree(cmd); 1300 1301 return ret; 1302} 1303 1304/* 1305 * TODO: merge with sta/ibss into 1 set_key function. 1306 * note there are slight diffs 1307 */ 1308int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif, 1309 u16 action, u8 id, u8 key_type, 1310 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32, 1311 u16 tx_seq_16) 1312{ 1313 struct wl1271_cmd_set_keys *cmd; 1314 int ret = 0; 1315 u8 lid_type; 1316 1317 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1318 if (!cmd) 1319 return -ENOMEM; 1320 1321 if (hlid == wlvif->ap.bcast_hlid) { 1322 if (key_type == KEY_WEP) 1323 lid_type = WEP_DEFAULT_LID_TYPE; 1324 else 1325 lid_type = BROADCAST_LID_TYPE; 1326 } else { 1327 lid_type = UNICAST_LID_TYPE; 1328 } 1329 1330 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d" 1331 " hlid: %d", (int)action, (int)id, (int)lid_type, 1332 (int)key_type, (int)hlid); 1333 1334 cmd->lid_key_type = lid_type; 1335 cmd->hlid = hlid; 1336 cmd->key_action = cpu_to_le16(action); 1337 cmd->key_size = key_size; 1338 cmd->key_type = key_type; 1339 cmd->key_id = id; 1340 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16); 1341 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32); 1342 1343 if (key_type == KEY_TKIP) { 1344 /* 1345 * We get the key in the following form: 1346 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes) 1347 * but the target is expecting: 1348 * TKIP - RX MIC - TX MIC 1349 */ 1350 memcpy(cmd->key, key, 16); 1351 memcpy(cmd->key + 16, key + 24, 8); 1352 memcpy(cmd->key + 24, key + 16, 8); 1353 } else { 1354 memcpy(cmd->key, key, key_size); 1355 } 1356 1357 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd)); 1358 1359 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0); 1360 if (ret < 0) { 1361 wl1271_warning("could not set ap keys"); 1362 goto out; 1363 } 1364 1365out: 1366 kfree(cmd); 1367 return ret; 1368} 1369 1370int wl12xx_cmd_set_peer_state(struct wl1271 *wl, struct wl12xx_vif *wlvif, 1371 u8 hlid) 1372{ 1373 struct wl12xx_cmd_set_peer_state *cmd; 1374 int ret = 0; 1375 1376 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid); 1377 1378 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1379 if (!cmd) { 1380 ret = -ENOMEM; 1381 goto out; 1382 } 1383 1384 cmd->hlid = hlid; 1385 cmd->state = WL1271_CMD_STA_STATE_CONNECTED; 1386 1387 /* wmm param is valid only for station role */ 1388 if (wlvif->bss_type == BSS_TYPE_STA_BSS) 1389 cmd->wmm = wlvif->wmm_enabled; 1390 1391 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0); 1392 if (ret < 0) { 1393 wl1271_error("failed to send set peer state command"); 1394 goto out_free; 1395 } 1396 1397out_free: 1398 kfree(cmd); 1399 1400out: 1401 return ret; 1402} 1403 1404int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif, 1405 struct ieee80211_sta *sta, u8 hlid) 1406{ 1407 struct wl12xx_cmd_add_peer *cmd; 1408 int i, ret; 1409 u32 sta_rates; 1410 1411 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid); 1412 1413 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1414 if (!cmd) { 1415 ret = -ENOMEM; 1416 goto out; 1417 } 1418 1419 memcpy(cmd->addr, sta->addr, ETH_ALEN); 1420 cmd->bss_index = WL1271_AP_BSS_INDEX; 1421 cmd->aid = sta->aid; 1422 cmd->hlid = hlid; 1423 cmd->sp_len = sta->max_sp; 1424 cmd->wmm = sta->wme ? 1 : 0; 1425 cmd->session_id = wl->session_ids[hlid]; 1426 1427 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++) 1428 if (sta->wme && (sta->uapsd_queues & BIT(i))) 1429 cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] = 1430 WL1271_PSD_UPSD_TRIGGER; 1431 else 1432 cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] = 1433 WL1271_PSD_LEGACY; 1434 1435 1436 sta_rates = sta->supp_rates[wlvif->band]; 1437 if (sta->ht_cap.ht_supported) 1438 sta_rates |= 1439 (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) | 1440 (sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET); 1441 1442 cmd->supported_rates = 1443 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates, 1444 wlvif->band)); 1445 1446 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x", 1447 cmd->supported_rates, sta->uapsd_queues); 1448 1449 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0); 1450 if (ret < 0) { 1451 wl1271_error("failed to initiate cmd add peer"); 1452 goto out_free; 1453 } 1454 1455out_free: 1456 kfree(cmd); 1457 1458out: 1459 return ret; 1460} 1461 1462int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid) 1463{ 1464 struct wl12xx_cmd_remove_peer *cmd; 1465 int ret; 1466 bool timeout = false; 1467 1468 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid); 1469 1470 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1471 if (!cmd) { 1472 ret = -ENOMEM; 1473 goto out; 1474 } 1475 1476 cmd->hlid = hlid; 1477 /* We never send a deauth, mac80211 is in charge of this */ 1478 cmd->reason_opcode = 0; 1479 cmd->send_deauth_flag = 0; 1480 1481 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0); 1482 if (ret < 0) { 1483 wl1271_error("failed to initiate cmd remove peer"); 1484 goto out_free; 1485 } 1486 1487 ret = wl->ops->wait_for_event(wl, 1488 WLCORE_EVENT_PEER_REMOVE_COMPLETE, 1489 &timeout); 1490 1491 /* 1492 * We are ok with a timeout here. The event is sometimes not sent 1493 * due to a firmware bug. In case of another error (like SDIO timeout) 1494 * queue a recovery. 1495 */ 1496 if (ret) 1497 wl12xx_queue_recovery_work(wl); 1498 1499out_free: 1500 kfree(cmd); 1501 1502out: 1503 return ret; 1504} 1505 1506int wl12xx_cmd_config_fwlog(struct wl1271 *wl) 1507{ 1508 struct wl12xx_cmd_config_fwlog *cmd; 1509 int ret = 0; 1510 1511 wl1271_debug(DEBUG_CMD, "cmd config firmware logger"); 1512 1513 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1514 if (!cmd) { 1515 ret = -ENOMEM; 1516 goto out; 1517 } 1518 1519 cmd->logger_mode = wl->conf.fwlog.mode; 1520 cmd->log_severity = wl->conf.fwlog.severity; 1521 cmd->timestamp = wl->conf.fwlog.timestamp; 1522 cmd->output = wl->conf.fwlog.output; 1523 cmd->threshold = wl->conf.fwlog.threshold; 1524 1525 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0); 1526 if (ret < 0) { 1527 wl1271_error("failed to send config firmware logger command"); 1528 goto out_free; 1529 } 1530 1531out_free: 1532 kfree(cmd); 1533 1534out: 1535 return ret; 1536} 1537 1538int wl12xx_cmd_start_fwlog(struct wl1271 *wl) 1539{ 1540 struct wl12xx_cmd_start_fwlog *cmd; 1541 int ret = 0; 1542 1543 wl1271_debug(DEBUG_CMD, "cmd start firmware logger"); 1544 1545 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1546 if (!cmd) { 1547 ret = -ENOMEM; 1548 goto out; 1549 } 1550 1551 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0); 1552 if (ret < 0) { 1553 wl1271_error("failed to send start firmware logger command"); 1554 goto out_free; 1555 } 1556 1557out_free: 1558 kfree(cmd); 1559 1560out: 1561 return ret; 1562} 1563 1564int wl12xx_cmd_stop_fwlog(struct wl1271 *wl) 1565{ 1566 struct wl12xx_cmd_stop_fwlog *cmd; 1567 int ret = 0; 1568 1569 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger"); 1570 1571 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1572 if (!cmd) { 1573 ret = -ENOMEM; 1574 goto out; 1575 } 1576 1577 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0); 1578 if (ret < 0) { 1579 wl1271_error("failed to send stop firmware logger command"); 1580 goto out_free; 1581 } 1582 1583out_free: 1584 kfree(cmd); 1585 1586out: 1587 return ret; 1588} 1589 1590static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, 1591 u8 role_id, enum ieee80211_band band, u8 channel) 1592{ 1593 struct wl12xx_cmd_roc *cmd; 1594 int ret = 0; 1595 1596 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", channel, role_id); 1597 1598 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID)) 1599 return -EINVAL; 1600 1601 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1602 if (!cmd) { 1603 ret = -ENOMEM; 1604 goto out; 1605 } 1606 1607 cmd->role_id = role_id; 1608 cmd->channel = channel; 1609 switch (band) { 1610 case IEEE80211_BAND_2GHZ: 1611 cmd->band = WLCORE_BAND_2_4GHZ; 1612 break; 1613 case IEEE80211_BAND_5GHZ: 1614 cmd->band = WLCORE_BAND_5GHZ; 1615 break; 1616 default: 1617 wl1271_error("roc - unknown band: %d", (int)wlvif->band); 1618 ret = -EINVAL; 1619 goto out_free; 1620 } 1621 1622 1623 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0); 1624 if (ret < 0) { 1625 wl1271_error("failed to send ROC command"); 1626 goto out_free; 1627 } 1628 1629out_free: 1630 kfree(cmd); 1631 1632out: 1633 return ret; 1634} 1635 1636static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id) 1637{ 1638 struct wl12xx_cmd_croc *cmd; 1639 int ret = 0; 1640 1641 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id); 1642 1643 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1644 if (!cmd) { 1645 ret = -ENOMEM; 1646 goto out; 1647 } 1648 cmd->role_id = role_id; 1649 1650 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd, 1651 sizeof(*cmd), 0); 1652 if (ret < 0) { 1653 wl1271_error("failed to send ROC command"); 1654 goto out_free; 1655 } 1656 1657out_free: 1658 kfree(cmd); 1659 1660out: 1661 return ret; 1662} 1663 1664int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id, 1665 enum ieee80211_band band, u8 channel) 1666{ 1667 int ret = 0; 1668 1669 if (WARN_ON(test_bit(role_id, wl->roc_map))) 1670 return 0; 1671 1672 ret = wl12xx_cmd_roc(wl, wlvif, role_id, band, channel); 1673 if (ret < 0) 1674 goto out; 1675 1676 __set_bit(role_id, wl->roc_map); 1677out: 1678 return ret; 1679} 1680 1681int wl12xx_croc(struct wl1271 *wl, u8 role_id) 1682{ 1683 int ret = 0; 1684 1685 if (WARN_ON(!test_bit(role_id, wl->roc_map))) 1686 return 0; 1687 1688 ret = wl12xx_cmd_croc(wl, role_id); 1689 if (ret < 0) 1690 goto out; 1691 1692 __clear_bit(role_id, wl->roc_map); 1693 1694 /* 1695 * Rearm the tx watchdog when removing the last ROC. This prevents 1696 * recoveries due to just finished ROCs - when Tx hasn't yet had 1697 * a chance to get out. 1698 */ 1699 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES) 1700 wl12xx_rearm_tx_watchdog_locked(wl); 1701out: 1702 return ret; 1703} 1704 1705int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl, struct wl12xx_vif *wlvif) 1706{ 1707 struct wl12xx_cmd_stop_channel_switch *cmd; 1708 int ret; 1709 1710 wl1271_debug(DEBUG_ACX, "cmd stop channel switch"); 1711 1712 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 1713 if (!cmd) { 1714 ret = -ENOMEM; 1715 goto out; 1716 } 1717 1718 cmd->role_id = wlvif->role_id; 1719 1720 ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0); 1721 if (ret < 0) { 1722 wl1271_error("failed to stop channel switch command"); 1723 goto out_free; 1724 } 1725 1726out_free: 1727 kfree(cmd); 1728 1729out: 1730 return ret; 1731} 1732 1733/* start dev role and roc on its channel */ 1734int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif, 1735 enum ieee80211_band band, int channel) 1736{ 1737 int ret; 1738 1739 if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS || 1740 wlvif->bss_type == BSS_TYPE_IBSS))) 1741 return -EINVAL; 1742 1743 ret = wl12xx_cmd_role_enable(wl, 1744 wl12xx_wlvif_to_vif(wlvif)->addr, 1745 WL1271_ROLE_DEVICE, 1746 &wlvif->dev_role_id); 1747 if (ret < 0) 1748 goto out; 1749 1750 ret = wl12xx_cmd_role_start_dev(wl, wlvif, band, channel); 1751 if (ret < 0) 1752 goto out_disable; 1753 1754 ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id, band, channel); 1755 if (ret < 0) 1756 goto out_stop; 1757 1758 return 0; 1759 1760out_stop: 1761 wl12xx_cmd_role_stop_dev(wl, wlvif); 1762out_disable: 1763 wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id); 1764out: 1765 return ret; 1766} 1767 1768/* croc dev hlid, and stop the role */ 1769int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif) 1770{ 1771 int ret; 1772 1773 if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS || 1774 wlvif->bss_type == BSS_TYPE_IBSS))) 1775 return -EINVAL; 1776 1777 /* flush all pending packets */ 1778 ret = wlcore_tx_work_locked(wl); 1779 if (ret < 0) 1780 goto out; 1781 1782 if (test_bit(wlvif->dev_role_id, wl->roc_map)) { 1783 ret = wl12xx_croc(wl, wlvif->dev_role_id); 1784 if (ret < 0) 1785 goto out; 1786 } 1787 1788 ret = wl12xx_cmd_role_stop_dev(wl, wlvif); 1789 if (ret < 0) 1790 goto out; 1791 1792 ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id); 1793 if (ret < 0) 1794 goto out; 1795 1796out: 1797 return ret; 1798} 1799