1 /* $OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $ */ 2 3 /* 4 * Copyright (c) 2014 genua mbh <info@genua.de> 5 * Copyright (c) 2014 Fixup Software Ltd. 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /*- 21 * Based on BSD-licensed source modules in the Linux iwlwifi driver, 22 * which were used as the reference documentation for this implementation. 23 * 24 * Driver version we are currently based off of is 25 * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd) 26 * 27 *********************************************************************** 28 * 29 * This file is provided under a dual BSD/GPLv2 license. When using or 30 * redistributing this file, you may do so under either license. 31 * 32 * GPL LICENSE SUMMARY 33 * 34 * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved. 35 * 36 * This program is free software; you can redistribute it and/or modify 37 * it under the terms of version 2 of the GNU General Public License as 38 * published by the Free Software Foundation. 39 * 40 * This program is distributed in the hope that it will be useful, but 41 * WITHOUT ANY WARRANTY; without even the implied warranty of 42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 43 * General Public License for more details. 44 * 45 * You should have received a copy of the GNU General Public License 46 * along with this program; if not, write to the Free Software 47 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, 48 * USA 49 * 50 * The full GNU General Public License is included in this distribution 51 * in the file called COPYING. 52 * 53 * Contact Information: 54 * Intel Linux Wireless <ilw@linux.intel.com> 55 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 56 * 57 * 58 * BSD LICENSE 59 * 60 * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved. 61 * All rights reserved. 62 * 63 * Redistribution and use in source and binary forms, with or without 64 * modification, are permitted provided that the following conditions 65 * are met: 66 * 67 * * Redistributions of source code must retain the above copyright 68 * notice, this list of conditions and the following disclaimer. 69 * * Redistributions in binary form must reproduce the above copyright 70 * notice, this list of conditions and the following disclaimer in 71 * the documentation and/or other materials provided with the 72 * distribution. 73 * * Neither the name Intel Corporation nor the names of its 74 * contributors may be used to endorse or promote products derived 75 * from this software without specific prior written permission. 76 * 77 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 78 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 79 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 80 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 81 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 82 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 83 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 84 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 85 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 86 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 87 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 88 */ 89 90 /*- 91 * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr> 92 * 93 * Permission to use, copy, modify, and distribute this software for any 94 * purpose with or without fee is hereby granted, provided that the above 95 * copyright notice and this permission notice appear in all copies. 96 * 97 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 98 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 99 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 100 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 101 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 102 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 103 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 104 */ 105 #include <sys/param.h> 106 #include <sys/bus.h> 107 #include <sys/conf.h> 108 #include <sys/endian.h> 109 #include <sys/firmware.h> 110 #include <sys/kernel.h> 111 #include <sys/malloc.h> 112 #include <sys/mbuf.h> 113 #include <sys/rman.h> 114 #include <sys/sysctl.h> 115 #include <sys/linker.h> 116 117 #include <machine/endian.h> 118 119 #include <bus/pci/pcivar.h> 120 #include <bus/pci/pcireg.h> 121 122 #include <net/bpf.h> 123 124 #include <net/if.h> 125 #include <net/if_var.h> 126 #include <net/if_arp.h> 127 #include <net/if_dl.h> 128 #include <net/if_media.h> 129 #include <net/if_types.h> 130 131 #include <netinet/in.h> 132 #include <netinet/in_systm.h> 133 #include <netinet/if_ether.h> 134 #include <netinet/ip.h> 135 136 #include <netproto/802_11/ieee80211_var.h> 137 #include <netproto/802_11/ieee80211_regdomain.h> 138 #include <netproto/802_11/ieee80211_ratectl.h> 139 #include <netproto/802_11/ieee80211_radiotap.h> 140 141 #include "if_iwmreg.h" 142 #include "if_iwmvar.h" 143 #include "if_iwm_config.h" 144 #include "if_iwm_debug.h" 145 #include "if_iwm_binding.h" 146 #include "if_iwm_util.h" 147 #include "if_iwm_pcie_trans.h" 148 149 /* 150 * Send a command to the firmware. We try to implement the Linux 151 * driver interface for the routine. 152 * mostly from if_iwn (iwn_cmd()). 153 * 154 * For now, we always copy the first part and map the second one (if it exists). 155 */ 156 int 157 iwm_send_cmd(struct iwm_softc *sc, struct iwm_host_cmd *hcmd) 158 { 159 struct iwm_tx_ring *ring = &sc->txq[IWM_CMD_QUEUE]; 160 struct iwm_tfd *desc; 161 struct iwm_tx_data *txdata = NULL; 162 struct iwm_device_cmd *cmd; 163 struct mbuf *m; 164 bus_dma_segment_t seg; 165 bus_addr_t paddr; 166 uint32_t addr_lo; 167 int error = 0, i, paylen, off; 168 int code; 169 int async, wantresp; 170 int group_id; 171 int nsegs; 172 size_t hdrlen, datasz; 173 uint8_t *data; 174 175 code = hcmd->id; 176 async = hcmd->flags & IWM_CMD_ASYNC; 177 wantresp = hcmd->flags & IWM_CMD_WANT_SKB; 178 data = NULL; 179 180 for (i = 0, paylen = 0; i < nitems(hcmd->len); i++) { 181 paylen += hcmd->len[i]; 182 } 183 184 /* if the command wants an answer, busy sc_cmd_resp */ 185 if (wantresp) { 186 KASSERT(!async, ("invalid async parameter")); 187 while (sc->sc_wantresp != -1) 188 lksleep(&sc->sc_wantresp, &sc->sc_lk, 0, "iwmcmdsl", 0); 189 sc->sc_wantresp = ring->qid << 16 | ring->cur; 190 IWM_DPRINTF(sc, IWM_DEBUG_CMD, 191 "wantresp is %x\n", sc->sc_wantresp); 192 } 193 194 /* 195 * Is the hardware still available? (after e.g. above wait). 196 */ 197 if (sc->sc_flags & IWM_FLAG_STOPPED) { 198 error = ENXIO; 199 goto out; 200 } 201 202 desc = &ring->desc[ring->cur]; 203 txdata = &ring->data[ring->cur]; 204 205 group_id = iwm_cmd_groupid(code); 206 if (group_id != 0) { 207 hdrlen = sizeof(cmd->hdr_wide); 208 datasz = sizeof(cmd->data_wide); 209 } else { 210 hdrlen = sizeof(cmd->hdr); 211 datasz = sizeof(cmd->data); 212 } 213 214 if (paylen > datasz) { 215 IWM_DPRINTF(sc, IWM_DEBUG_CMD, 216 "large command paylen=%u len0=%u\n", 217 paylen, hcmd->len[0]); 218 /* Command is too large */ 219 size_t totlen = hdrlen + paylen; 220 if (paylen > IWM_MAX_CMD_PAYLOAD_SIZE) { 221 device_printf(sc->sc_dev, 222 "firmware command too long (%zd bytes)\n", 223 totlen); 224 error = EINVAL; 225 goto out; 226 } 227 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, IWM_RBUF_SIZE); 228 if (m == NULL) { 229 error = ENOBUFS; 230 goto out; 231 } 232 233 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; 234 #if defined(__DragonFly__) 235 error = bus_dmamap_load_mbuf_segment(ring->data_dmat, 236 txdata->map, m, &seg, 1, &nsegs, BUS_DMA_NOWAIT); 237 #else 238 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, 239 txdata->map, m, &seg, &nsegs, BUS_DMA_NOWAIT); 240 #endif 241 if (error != 0) { 242 device_printf(sc->sc_dev, 243 "%s: can't map mbuf, error %d\n", __func__, error); 244 m_freem(m); 245 goto out; 246 } 247 txdata->m = m; /* mbuf will be freed in iwm_cmd_done() */ 248 cmd = mtod(m, struct iwm_device_cmd *); 249 paddr = seg.ds_addr; 250 } else { 251 cmd = &ring->cmd[ring->cur]; 252 paddr = txdata->cmd_paddr; 253 } 254 255 if (group_id != 0) { 256 cmd->hdr_wide.opcode = iwm_cmd_opcode(code); 257 cmd->hdr_wide.group_id = group_id; 258 cmd->hdr_wide.qid = ring->qid; 259 cmd->hdr_wide.idx = ring->cur; 260 cmd->hdr_wide.length = htole16(paylen); 261 cmd->hdr_wide.version = iwm_cmd_version(code); 262 data = cmd->data_wide; 263 } else { 264 cmd->hdr.code = iwm_cmd_opcode(code); 265 cmd->hdr.flags = 0; 266 cmd->hdr.qid = ring->qid; 267 cmd->hdr.idx = ring->cur; 268 data = cmd->data; 269 } 270 271 for (i = 0, off = 0; i < nitems(hcmd->data); i++) { 272 if (hcmd->len[i] == 0) 273 continue; 274 memcpy(data + off, hcmd->data[i], hcmd->len[i]); 275 off += hcmd->len[i]; 276 } 277 KASSERT(off == paylen, ("off %d != paylen %d", off, paylen)); 278 279 /* lo field is not aligned */ 280 addr_lo = htole32((uint32_t)paddr); 281 memcpy(&desc->tbs[0].lo, &addr_lo, sizeof(uint32_t)); 282 desc->tbs[0].hi_n_len = htole16(iwm_get_dma_hi_addr(paddr) 283 | ((hdrlen + paylen) << 4)); 284 desc->num_tbs = 1; 285 286 IWM_DPRINTF(sc, IWM_DEBUG_CMD, 287 "iwm_send_cmd 0x%x size=%lu %s\n", 288 code, 289 (unsigned long) (hcmd->len[0] + hcmd->len[1] + hdrlen), 290 async ? " (async)" : ""); 291 292 if (paylen > datasz) { 293 bus_dmamap_sync(ring->data_dmat, txdata->map, 294 BUS_DMASYNC_PREWRITE); 295 } else { 296 bus_dmamap_sync(ring->cmd_dma.tag, ring->cmd_dma.map, 297 BUS_DMASYNC_PREWRITE); 298 } 299 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 300 BUS_DMASYNC_PREWRITE); 301 302 error = iwm_pcie_set_cmd_in_flight(sc); 303 if (error) 304 goto out; 305 ring->queued++; 306 307 #if 0 308 iwm_update_sched(sc, ring->qid, ring->cur, 0, 0); 309 #endif 310 IWM_DPRINTF(sc, IWM_DEBUG_CMD, 311 "sending command 0x%x qid %d, idx %d\n", 312 code, ring->qid, ring->cur); 313 314 /* Kick command ring. */ 315 ring->cur = (ring->cur + 1) % IWM_TX_RING_COUNT; 316 IWM_WRITE(sc, IWM_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur); 317 318 if (!async) { 319 /* m..m-mmyy-mmyyyy-mym-ym m-my generation */ 320 int generation = sc->sc_generation; 321 error = lksleep(desc, &sc->sc_lk, PCATCH, "iwmcmd", hz); 322 if (error == 0) { 323 /* if hardware is no longer up, return error */ 324 if (generation != sc->sc_generation) { 325 error = ENXIO; 326 } else { 327 hcmd->resp_pkt = (void *)sc->sc_cmd_resp; 328 } 329 } 330 } 331 out: 332 if (wantresp && error != 0) { 333 iwm_free_resp(sc, hcmd); 334 } 335 336 return error; 337 } 338 339 /* iwlwifi: mvm/utils.c */ 340 int 341 iwm_send_cmd_pdu(struct iwm_softc *sc, uint32_t id, 342 uint32_t flags, uint16_t len, const void *data) 343 { 344 struct iwm_host_cmd cmd = { 345 .id = id, 346 .len = { len, }, 347 .data = { data, }, 348 .flags = flags, 349 }; 350 351 return iwm_send_cmd(sc, &cmd); 352 } 353 354 /* iwlwifi: mvm/utils.c */ 355 int 356 iwm_send_cmd_status(struct iwm_softc *sc, 357 struct iwm_host_cmd *cmd, uint32_t *status) 358 { 359 struct iwm_rx_packet *pkt; 360 struct iwm_cmd_response *resp; 361 int error, resp_len; 362 363 KASSERT((cmd->flags & IWM_CMD_WANT_SKB) == 0, 364 ("invalid command")); 365 cmd->flags |= IWM_CMD_SYNC | IWM_CMD_WANT_SKB; 366 367 if ((error = iwm_send_cmd(sc, cmd)) != 0) 368 return error; 369 pkt = cmd->resp_pkt; 370 371 /* Can happen if RFKILL is asserted */ 372 if (!pkt) { 373 error = 0; 374 goto out_free_resp; 375 } 376 377 if (pkt->hdr.flags & IWM_CMD_FAILED_MSK) { 378 error = EIO; 379 goto out_free_resp; 380 } 381 382 resp_len = iwm_rx_packet_payload_len(pkt); 383 if (resp_len != sizeof(*resp)) { 384 error = EIO; 385 goto out_free_resp; 386 } 387 388 resp = (void *)pkt->data; 389 *status = le32toh(resp->status); 390 out_free_resp: 391 iwm_free_resp(sc, cmd); 392 return error; 393 } 394 395 /* iwlwifi/mvm/utils.c */ 396 int 397 iwm_send_cmd_pdu_status(struct iwm_softc *sc, uint32_t id, 398 uint16_t len, const void *data, uint32_t *status) 399 { 400 struct iwm_host_cmd cmd = { 401 .id = id, 402 .len = { len, }, 403 .data = { data, }, 404 }; 405 406 return iwm_send_cmd_status(sc, &cmd, status); 407 } 408 409 void 410 iwm_free_resp(struct iwm_softc *sc, struct iwm_host_cmd *hcmd) 411 { 412 KASSERT(sc->sc_wantresp != -1, ("already freed")); 413 KASSERT((hcmd->flags & (IWM_CMD_WANT_SKB|IWM_CMD_SYNC)) 414 == (IWM_CMD_WANT_SKB|IWM_CMD_SYNC), ("invalid flags")); 415 sc->sc_wantresp = -1; 416 wakeup(&sc->sc_wantresp); 417 } 418 419 #if !defined(__DragonFly__) 420 421 static void 422 iwm_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 423 { 424 if (error != 0) 425 return; 426 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs)); 427 *(bus_addr_t *)arg = segs[0].ds_addr; 428 } 429 430 #endif 431 432 int 433 iwm_dma_contig_alloc(bus_dma_tag_t tag, struct iwm_dma_info *dma, 434 bus_size_t size, bus_size_t alignment) 435 { 436 int error; 437 438 dma->tag = NULL; 439 dma->map = NULL; 440 dma->size = size; 441 dma->vaddr = NULL; 442 443 #if defined(__DragonFly__) 444 bus_dmamem_t dmem; 445 error = bus_dmamem_coherent(tag, alignment, 0, 446 BUS_SPACE_MAXADDR_32BIT, 447 BUS_SPACE_MAXADDR, 448 size, BUS_DMA_NOWAIT, &dmem); 449 if (error != 0) 450 goto fail; 451 dma->tag = dmem.dmem_tag; 452 dma->map = dmem.dmem_map; 453 dma->vaddr = dmem.dmem_addr; 454 dma->paddr = dmem.dmem_busaddr; 455 #else 456 error = bus_dma_tag_create(tag, alignment, 457 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size, 458 1, size, 0, NULL, NULL, &dma->tag); 459 if (error != 0) 460 goto fail; 461 462 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr, 463 BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, &dma->map); 464 if (error != 0) 465 goto fail; 466 467 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size, 468 iwm_dma_map_addr, &dma->paddr, BUS_DMA_NOWAIT); 469 if (error != 0) { 470 bus_dmamem_free(dma->tag, dma->vaddr, dma->map); 471 dma->vaddr = NULL; 472 goto fail; 473 } 474 #endif 475 476 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 477 478 return 0; 479 480 fail: 481 iwm_dma_contig_free(dma); 482 483 return error; 484 } 485 486 void 487 iwm_dma_contig_free(struct iwm_dma_info *dma) 488 { 489 if (dma->vaddr != NULL) { 490 bus_dmamap_sync(dma->tag, dma->map, 491 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 492 bus_dmamap_unload(dma->tag, dma->map); 493 bus_dmamem_free(dma->tag, dma->vaddr, dma->map); 494 dma->vaddr = NULL; 495 } 496 if (dma->tag != NULL) { 497 bus_dma_tag_destroy(dma->tag); 498 dma->tag = NULL; 499 } 500 } 501 502 /** 503 * iwm_send_lq_cmd() - Send link quality command 504 * @init: This command is sent as part of station initialization right 505 * after station has been added. 506 * 507 * The link quality command is sent as the last step of station creation. 508 * This is the special case in which init is set and we call a callback in 509 * this case to clear the state indicating that station creation is in 510 * progress. 511 */ 512 int 513 iwm_send_lq_cmd(struct iwm_softc *sc, struct iwm_lq_cmd *lq, boolean_t init) 514 { 515 struct iwm_host_cmd cmd = { 516 .id = IWM_LQ_CMD, 517 .len = { sizeof(struct iwm_lq_cmd), }, 518 .flags = init ? 0 : IWM_CMD_ASYNC, 519 .data = { lq, }, 520 }; 521 522 if (lq->sta_id == IWM_STATION_COUNT) 523 return EINVAL; 524 525 return iwm_send_cmd(sc, &cmd); 526 } 527 528 boolean_t 529 iwm_rx_diversity_allowed(struct iwm_softc *sc) 530 { 531 if (num_of_ant(iwm_get_valid_rx_ant(sc)) == 1) 532 return FALSE; 533 534 /* 535 * XXX Also return FALSE when SMPS (Spatial Multiplexing Powersave) 536 * is used on any vap (in the future). 537 */ 538 539 return TRUE; 540 } 541