Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 Cisco Systems, Inc. All rights reserved. |
| 3 | * Copyright 2007 Nuova Systems, Inc. All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you may redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; version 2 of the License. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 10 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 11 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 12 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 13 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 14 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 15 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 16 | * SOFTWARE. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/if_ether.h> |
| 26 | |
| 27 | #include "vnic_resource.h" |
| 28 | #include "vnic_devcmd.h" |
| 29 | #include "vnic_dev.h" |
| 30 | #include "vnic_stats.h" |
| 31 | |
| 32 | struct vnic_res { |
| 33 | void __iomem *vaddr; |
| 34 | unsigned int count; |
| 35 | }; |
| 36 | |
| 37 | struct vnic_dev { |
| 38 | void *priv; |
| 39 | struct pci_dev *pdev; |
| 40 | struct vnic_res res[RES_TYPE_MAX]; |
| 41 | enum vnic_dev_intr_mode intr_mode; |
| 42 | struct vnic_devcmd __iomem *devcmd; |
| 43 | struct vnic_devcmd_notify *notify; |
| 44 | struct vnic_devcmd_notify notify_copy; |
| 45 | dma_addr_t notify_pa; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 46 | u32 notify_sz; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 47 | u32 *linkstatus; |
| 48 | dma_addr_t linkstatus_pa; |
| 49 | struct vnic_stats *stats; |
| 50 | dma_addr_t stats_pa; |
| 51 | struct vnic_devcmd_fw_info *fw_info; |
| 52 | dma_addr_t fw_info_pa; |
| 53 | }; |
| 54 | |
| 55 | #define VNIC_MAX_RES_HDR_SIZE \ |
| 56 | (sizeof(struct vnic_resource_header) + \ |
| 57 | sizeof(struct vnic_resource) * RES_TYPE_MAX) |
| 58 | #define VNIC_RES_STRIDE 128 |
| 59 | |
| 60 | void *vnic_dev_priv(struct vnic_dev *vdev) |
| 61 | { |
| 62 | return vdev->priv; |
| 63 | } |
| 64 | |
| 65 | static int vnic_dev_discover_res(struct vnic_dev *vdev, |
| 66 | struct vnic_dev_bar *bar) |
| 67 | { |
| 68 | struct vnic_resource_header __iomem *rh; |
| 69 | struct vnic_resource __iomem *r; |
| 70 | u8 type; |
| 71 | |
| 72 | if (bar->len < VNIC_MAX_RES_HDR_SIZE) { |
| 73 | printk(KERN_ERR "vNIC BAR0 res hdr length error\n"); |
| 74 | return -EINVAL; |
| 75 | } |
| 76 | |
| 77 | rh = bar->vaddr; |
| 78 | if (!rh) { |
| 79 | printk(KERN_ERR "vNIC BAR0 res hdr not mem-mapped\n"); |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | |
| 83 | if (ioread32(&rh->magic) != VNIC_RES_MAGIC || |
| 84 | ioread32(&rh->version) != VNIC_RES_VERSION) { |
| 85 | printk(KERN_ERR "vNIC BAR0 res magic/version error " |
| 86 | "exp (%lx/%lx) curr (%x/%x)\n", |
| 87 | VNIC_RES_MAGIC, VNIC_RES_VERSION, |
| 88 | ioread32(&rh->magic), ioread32(&rh->version)); |
| 89 | return -EINVAL; |
| 90 | } |
| 91 | |
| 92 | r = (struct vnic_resource __iomem *)(rh + 1); |
| 93 | |
| 94 | while ((type = ioread8(&r->type)) != RES_TYPE_EOL) { |
| 95 | |
| 96 | u8 bar_num = ioread8(&r->bar); |
| 97 | u32 bar_offset = ioread32(&r->bar_offset); |
| 98 | u32 count = ioread32(&r->count); |
| 99 | u32 len; |
| 100 | |
| 101 | r++; |
| 102 | |
| 103 | if (bar_num != 0) /* only mapping in BAR0 resources */ |
| 104 | continue; |
| 105 | |
| 106 | switch (type) { |
| 107 | case RES_TYPE_WQ: |
| 108 | case RES_TYPE_RQ: |
| 109 | case RES_TYPE_CQ: |
| 110 | case RES_TYPE_INTR_CTRL: |
| 111 | /* each count is stride bytes long */ |
| 112 | len = count * VNIC_RES_STRIDE; |
| 113 | if (len + bar_offset > bar->len) { |
| 114 | printk(KERN_ERR "vNIC BAR0 resource %d " |
| 115 | "out-of-bounds, offset 0x%x + " |
| 116 | "size 0x%x > bar len 0x%lx\n", |
| 117 | type, bar_offset, |
| 118 | len, |
| 119 | bar->len); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | break; |
| 123 | case RES_TYPE_INTR_PBA_LEGACY: |
| 124 | case RES_TYPE_DEVCMD: |
| 125 | len = count; |
| 126 | break; |
| 127 | default: |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | vdev->res[type].count = count; |
| 132 | vdev->res[type].vaddr = (char __iomem *)bar->vaddr + bar_offset; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev, |
| 139 | enum vnic_res_type type) |
| 140 | { |
| 141 | return vdev->res[type].count; |
| 142 | } |
| 143 | |
| 144 | void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type, |
| 145 | unsigned int index) |
| 146 | { |
| 147 | if (!vdev->res[type].vaddr) |
| 148 | return NULL; |
| 149 | |
| 150 | switch (type) { |
| 151 | case RES_TYPE_WQ: |
| 152 | case RES_TYPE_RQ: |
| 153 | case RES_TYPE_CQ: |
| 154 | case RES_TYPE_INTR_CTRL: |
| 155 | return (char __iomem *)vdev->res[type].vaddr + |
| 156 | index * VNIC_RES_STRIDE; |
| 157 | default: |
| 158 | return (char __iomem *)vdev->res[type].vaddr; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring, |
| 163 | unsigned int desc_count, unsigned int desc_size) |
| 164 | { |
| 165 | /* The base address of the desc rings must be 512 byte aligned. |
| 166 | * Descriptor count is aligned to groups of 32 descriptors. A |
| 167 | * count of 0 means the maximum 4096 descriptors. Descriptor |
| 168 | * size is aligned to 16 bytes. |
| 169 | */ |
| 170 | |
| 171 | unsigned int count_align = 32; |
| 172 | unsigned int desc_align = 16; |
| 173 | |
| 174 | ring->base_align = 512; |
| 175 | |
| 176 | if (desc_count == 0) |
| 177 | desc_count = 4096; |
| 178 | |
| 179 | ring->desc_count = ALIGN(desc_count, count_align); |
| 180 | |
| 181 | ring->desc_size = ALIGN(desc_size, desc_align); |
| 182 | |
| 183 | ring->size = ring->desc_count * ring->desc_size; |
| 184 | ring->size_unaligned = ring->size + ring->base_align; |
| 185 | |
| 186 | return ring->size_unaligned; |
| 187 | } |
| 188 | |
| 189 | void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring) |
| 190 | { |
| 191 | memset(ring->descs, 0, ring->size); |
| 192 | } |
| 193 | |
| 194 | int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring, |
| 195 | unsigned int desc_count, unsigned int desc_size) |
| 196 | { |
| 197 | vnic_dev_desc_ring_size(ring, desc_count, desc_size); |
| 198 | |
| 199 | ring->descs_unaligned = pci_alloc_consistent(vdev->pdev, |
| 200 | ring->size_unaligned, |
| 201 | &ring->base_addr_unaligned); |
| 202 | |
| 203 | if (!ring->descs_unaligned) { |
| 204 | printk(KERN_ERR |
| 205 | "Failed to allocate ring (size=%d), aborting\n", |
| 206 | (int)ring->size); |
| 207 | return -ENOMEM; |
| 208 | } |
| 209 | |
| 210 | ring->base_addr = ALIGN(ring->base_addr_unaligned, |
| 211 | ring->base_align); |
| 212 | ring->descs = (u8 *)ring->descs_unaligned + |
| 213 | (ring->base_addr - ring->base_addr_unaligned); |
| 214 | |
| 215 | vnic_dev_clear_desc_ring(ring); |
| 216 | |
| 217 | ring->desc_avail = ring->desc_count - 1; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring) |
| 223 | { |
| 224 | if (ring->descs) { |
| 225 | pci_free_consistent(vdev->pdev, |
| 226 | ring->size_unaligned, |
| 227 | ring->descs_unaligned, |
| 228 | ring->base_addr_unaligned); |
| 229 | ring->descs = NULL; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, |
| 234 | u64 *a0, u64 *a1, int wait) |
| 235 | { |
| 236 | struct vnic_devcmd __iomem *devcmd = vdev->devcmd; |
| 237 | int delay; |
| 238 | u32 status; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 239 | int err; |
| 240 | |
| 241 | status = ioread32(&devcmd->status); |
| 242 | if (status & STAT_BUSY) { |
| 243 | printk(KERN_ERR "Busy devcmd %d\n", _CMD_N(cmd)); |
| 244 | return -EBUSY; |
| 245 | } |
| 246 | |
| 247 | if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) { |
| 248 | writeq(*a0, &devcmd->args[0]); |
| 249 | writeq(*a1, &devcmd->args[1]); |
| 250 | wmb(); |
| 251 | } |
| 252 | |
| 253 | iowrite32(cmd, &devcmd->cmd); |
| 254 | |
| 255 | if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT)) |
| 256 | return 0; |
| 257 | |
| 258 | for (delay = 0; delay < wait; delay++) { |
| 259 | |
| 260 | udelay(100); |
| 261 | |
| 262 | status = ioread32(&devcmd->status); |
| 263 | if (!(status & STAT_BUSY)) { |
| 264 | |
| 265 | if (status & STAT_ERROR) { |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 266 | err = (int)readq(&devcmd->args[0]); |
| 267 | if (err != ERR_ECMDUNKNOWN || |
| 268 | cmd != CMD_CAPABILITY) |
| 269 | printk(KERN_ERR "Error %d devcmd %d\n", |
| 270 | err, _CMD_N(cmd)); |
| 271 | return err; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | if (_CMD_DIR(cmd) & _CMD_DIR_READ) { |
| 275 | rmb(); |
| 276 | *a0 = readq(&devcmd->args[0]); |
| 277 | *a1 = readq(&devcmd->args[1]); |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | printk(KERN_ERR "Timedout devcmd %d\n", _CMD_N(cmd)); |
| 285 | return -ETIMEDOUT; |
| 286 | } |
| 287 | |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 288 | int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd) |
| 289 | { |
| 290 | u64 a0 = (u32)cmd, a1 = 0; |
| 291 | int wait = 1000; |
| 292 | int err; |
| 293 | |
| 294 | err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait); |
| 295 | |
| 296 | return !(err || a0); |
| 297 | } |
| 298 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 299 | int vnic_dev_fw_info(struct vnic_dev *vdev, |
| 300 | struct vnic_devcmd_fw_info **fw_info) |
| 301 | { |
| 302 | u64 a0, a1 = 0; |
| 303 | int wait = 1000; |
| 304 | int err = 0; |
| 305 | |
| 306 | if (!vdev->fw_info) { |
| 307 | vdev->fw_info = pci_alloc_consistent(vdev->pdev, |
| 308 | sizeof(struct vnic_devcmd_fw_info), |
| 309 | &vdev->fw_info_pa); |
| 310 | if (!vdev->fw_info) |
| 311 | return -ENOMEM; |
| 312 | |
| 313 | a0 = vdev->fw_info_pa; |
| 314 | |
| 315 | /* only get fw_info once and cache it */ |
| 316 | err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait); |
| 317 | } |
| 318 | |
| 319 | *fw_info = vdev->fw_info; |
| 320 | |
| 321 | return err; |
| 322 | } |
| 323 | |
| 324 | int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size, |
| 325 | void *value) |
| 326 | { |
| 327 | u64 a0, a1; |
| 328 | int wait = 1000; |
| 329 | int err; |
| 330 | |
| 331 | a0 = offset; |
| 332 | a1 = size; |
| 333 | |
| 334 | err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait); |
| 335 | |
| 336 | switch (size) { |
| 337 | case 1: *(u8 *)value = (u8)a0; break; |
| 338 | case 2: *(u16 *)value = (u16)a0; break; |
| 339 | case 4: *(u32 *)value = (u32)a0; break; |
| 340 | case 8: *(u64 *)value = a0; break; |
| 341 | default: BUG(); break; |
| 342 | } |
| 343 | |
| 344 | return err; |
| 345 | } |
| 346 | |
| 347 | int vnic_dev_stats_clear(struct vnic_dev *vdev) |
| 348 | { |
| 349 | u64 a0 = 0, a1 = 0; |
| 350 | int wait = 1000; |
| 351 | return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait); |
| 352 | } |
| 353 | |
| 354 | int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats) |
| 355 | { |
| 356 | u64 a0, a1; |
| 357 | int wait = 1000; |
| 358 | |
| 359 | if (!vdev->stats) { |
| 360 | vdev->stats = pci_alloc_consistent(vdev->pdev, |
| 361 | sizeof(struct vnic_stats), &vdev->stats_pa); |
| 362 | if (!vdev->stats) |
| 363 | return -ENOMEM; |
| 364 | } |
| 365 | |
| 366 | *stats = vdev->stats; |
| 367 | a0 = vdev->stats_pa; |
| 368 | a1 = sizeof(struct vnic_stats); |
| 369 | |
| 370 | return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait); |
| 371 | } |
| 372 | |
| 373 | int vnic_dev_close(struct vnic_dev *vdev) |
| 374 | { |
| 375 | u64 a0 = 0, a1 = 0; |
| 376 | int wait = 1000; |
| 377 | return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait); |
| 378 | } |
| 379 | |
| 380 | int vnic_dev_enable(struct vnic_dev *vdev) |
| 381 | { |
| 382 | u64 a0 = 0, a1 = 0; |
| 383 | int wait = 1000; |
| 384 | return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait); |
| 385 | } |
| 386 | |
| 387 | int vnic_dev_disable(struct vnic_dev *vdev) |
| 388 | { |
| 389 | u64 a0 = 0, a1 = 0; |
| 390 | int wait = 1000; |
| 391 | return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait); |
| 392 | } |
| 393 | |
| 394 | int vnic_dev_open(struct vnic_dev *vdev, int arg) |
| 395 | { |
| 396 | u64 a0 = (u32)arg, a1 = 0; |
| 397 | int wait = 1000; |
| 398 | return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait); |
| 399 | } |
| 400 | |
| 401 | int vnic_dev_open_done(struct vnic_dev *vdev, int *done) |
| 402 | { |
| 403 | u64 a0 = 0, a1 = 0; |
| 404 | int wait = 1000; |
| 405 | int err; |
| 406 | |
| 407 | *done = 0; |
| 408 | |
| 409 | err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait); |
| 410 | if (err) |
| 411 | return err; |
| 412 | |
| 413 | *done = (a0 == 0); |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg) |
| 419 | { |
| 420 | u64 a0 = (u32)arg, a1 = 0; |
| 421 | int wait = 1000; |
| 422 | return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait); |
| 423 | } |
| 424 | |
| 425 | int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done) |
| 426 | { |
| 427 | u64 a0 = 0, a1 = 0; |
| 428 | int wait = 1000; |
| 429 | int err; |
| 430 | |
| 431 | *done = 0; |
| 432 | |
| 433 | err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait); |
| 434 | if (err) |
| 435 | return err; |
| 436 | |
| 437 | *done = (a0 == 0); |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | int vnic_dev_hang_notify(struct vnic_dev *vdev) |
| 443 | { |
| 444 | u64 a0, a1; |
| 445 | int wait = 1000; |
| 446 | return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait); |
| 447 | } |
| 448 | |
| 449 | int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr) |
| 450 | { |
| 451 | u64 a0, a1; |
| 452 | int wait = 1000; |
| 453 | int err, i; |
| 454 | |
| 455 | for (i = 0; i < ETH_ALEN; i++) |
| 456 | mac_addr[i] = 0; |
| 457 | |
| 458 | err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait); |
| 459 | if (err) |
| 460 | return err; |
| 461 | |
| 462 | for (i = 0; i < ETH_ALEN; i++) |
| 463 | mac_addr[i] = ((u8 *)&a0)[i]; |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | void vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast, |
| 469 | int broadcast, int promisc, int allmulti) |
| 470 | { |
| 471 | u64 a0, a1 = 0; |
| 472 | int wait = 1000; |
| 473 | int err; |
| 474 | |
| 475 | a0 = (directed ? CMD_PFILTER_DIRECTED : 0) | |
| 476 | (multicast ? CMD_PFILTER_MULTICAST : 0) | |
| 477 | (broadcast ? CMD_PFILTER_BROADCAST : 0) | |
| 478 | (promisc ? CMD_PFILTER_PROMISCUOUS : 0) | |
| 479 | (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0); |
| 480 | |
| 481 | err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait); |
| 482 | if (err) |
| 483 | printk(KERN_ERR "Can't set packet filter\n"); |
| 484 | } |
| 485 | |
| 486 | void vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr) |
| 487 | { |
| 488 | u64 a0 = 0, a1 = 0; |
| 489 | int wait = 1000; |
| 490 | int err; |
| 491 | int i; |
| 492 | |
| 493 | for (i = 0; i < ETH_ALEN; i++) |
| 494 | ((u8 *)&a0)[i] = addr[i]; |
| 495 | |
| 496 | err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); |
| 497 | if (err) |
Johannes Berg | 7c510e4 | 2008-10-27 17:47:26 -0700 | [diff] [blame] | 498 | printk(KERN_ERR "Can't add addr [%pM], %d\n", addr, err); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | void vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr) |
| 502 | { |
| 503 | u64 a0 = 0, a1 = 0; |
| 504 | int wait = 1000; |
| 505 | int err; |
| 506 | int i; |
| 507 | |
| 508 | for (i = 0; i < ETH_ALEN; i++) |
| 509 | ((u8 *)&a0)[i] = addr[i]; |
| 510 | |
| 511 | err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait); |
| 512 | if (err) |
Johannes Berg | 7c510e4 | 2008-10-27 17:47:26 -0700 | [diff] [blame] | 513 | printk(KERN_ERR "Can't del addr [%pM], %d\n", addr, err); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr) |
| 517 | { |
| 518 | u64 a0, a1; |
| 519 | int wait = 1000; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 520 | int r; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 521 | |
| 522 | if (!vdev->notify) { |
| 523 | vdev->notify = pci_alloc_consistent(vdev->pdev, |
| 524 | sizeof(struct vnic_devcmd_notify), |
| 525 | &vdev->notify_pa); |
| 526 | if (!vdev->notify) |
| 527 | return -ENOMEM; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 528 | memset(vdev->notify, 0, sizeof(struct vnic_devcmd_notify)); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | a0 = vdev->notify_pa; |
| 532 | a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL; |
| 533 | a1 += sizeof(struct vnic_devcmd_notify); |
| 534 | |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 535 | r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); |
| 536 | vdev->notify_sz = (r == 0) ? (u32)a1 : 0; |
| 537 | return r; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | void vnic_dev_notify_unset(struct vnic_dev *vdev) |
| 541 | { |
| 542 | u64 a0, a1; |
| 543 | int wait = 1000; |
| 544 | |
| 545 | a0 = 0; /* paddr = 0 to unset notify buffer */ |
| 546 | a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */ |
| 547 | a1 += sizeof(struct vnic_devcmd_notify); |
| 548 | |
| 549 | vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 550 | vdev->notify_sz = 0; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static int vnic_dev_notify_ready(struct vnic_dev *vdev) |
| 554 | { |
| 555 | u32 *words; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 556 | unsigned int nwords = vdev->notify_sz / 4; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 557 | unsigned int i; |
| 558 | u32 csum; |
| 559 | |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 560 | if (!vdev->notify || !vdev->notify_sz) |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 561 | return 0; |
| 562 | |
| 563 | do { |
| 564 | csum = 0; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 565 | memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 566 | words = (u32 *)&vdev->notify_copy; |
| 567 | for (i = 1; i < nwords; i++) |
| 568 | csum += words[i]; |
| 569 | } while (csum != words[0]); |
| 570 | |
| 571 | return 1; |
| 572 | } |
| 573 | |
| 574 | int vnic_dev_init(struct vnic_dev *vdev, int arg) |
| 575 | { |
| 576 | u64 a0 = (u32)arg, a1 = 0; |
| 577 | int wait = 1000; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 578 | int r = 0; |
| 579 | |
| 580 | if (vnic_dev_capable(vdev, CMD_INIT)) |
| 581 | r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait); |
| 582 | else { |
| 583 | vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait); |
| 584 | if (a0 & CMD_INITF_DEFAULT_MAC) { |
| 585 | // Emulate these for old CMD_INIT_v1 which |
| 586 | // didn't pass a0 so no CMD_INITF_*. |
| 587 | vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait); |
| 588 | vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); |
| 589 | } |
| 590 | } |
| 591 | return r; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | int vnic_dev_link_status(struct vnic_dev *vdev) |
| 595 | { |
| 596 | if (vdev->linkstatus) |
| 597 | return *vdev->linkstatus; |
| 598 | |
| 599 | if (!vnic_dev_notify_ready(vdev)) |
| 600 | return 0; |
| 601 | |
| 602 | return vdev->notify_copy.link_state; |
| 603 | } |
| 604 | |
| 605 | u32 vnic_dev_port_speed(struct vnic_dev *vdev) |
| 606 | { |
| 607 | if (!vnic_dev_notify_ready(vdev)) |
| 608 | return 0; |
| 609 | |
| 610 | return vdev->notify_copy.port_speed; |
| 611 | } |
| 612 | |
| 613 | u32 vnic_dev_msg_lvl(struct vnic_dev *vdev) |
| 614 | { |
| 615 | if (!vnic_dev_notify_ready(vdev)) |
| 616 | return 0; |
| 617 | |
| 618 | return vdev->notify_copy.msglvl; |
| 619 | } |
| 620 | |
| 621 | u32 vnic_dev_mtu(struct vnic_dev *vdev) |
| 622 | { |
| 623 | if (!vnic_dev_notify_ready(vdev)) |
| 624 | return 0; |
| 625 | |
| 626 | return vdev->notify_copy.mtu; |
| 627 | } |
| 628 | |
| 629 | void vnic_dev_set_intr_mode(struct vnic_dev *vdev, |
| 630 | enum vnic_dev_intr_mode intr_mode) |
| 631 | { |
| 632 | vdev->intr_mode = intr_mode; |
| 633 | } |
| 634 | |
| 635 | enum vnic_dev_intr_mode vnic_dev_get_intr_mode( |
| 636 | struct vnic_dev *vdev) |
| 637 | { |
| 638 | return vdev->intr_mode; |
| 639 | } |
| 640 | |
| 641 | void vnic_dev_unregister(struct vnic_dev *vdev) |
| 642 | { |
| 643 | if (vdev) { |
| 644 | if (vdev->notify) |
| 645 | pci_free_consistent(vdev->pdev, |
| 646 | sizeof(struct vnic_devcmd_notify), |
| 647 | vdev->notify, |
| 648 | vdev->notify_pa); |
| 649 | if (vdev->linkstatus) |
| 650 | pci_free_consistent(vdev->pdev, |
| 651 | sizeof(u32), |
| 652 | vdev->linkstatus, |
| 653 | vdev->linkstatus_pa); |
| 654 | if (vdev->stats) |
| 655 | pci_free_consistent(vdev->pdev, |
| 656 | sizeof(struct vnic_dev), |
| 657 | vdev->stats, vdev->stats_pa); |
| 658 | if (vdev->fw_info) |
| 659 | pci_free_consistent(vdev->pdev, |
| 660 | sizeof(struct vnic_devcmd_fw_info), |
| 661 | vdev->fw_info, vdev->fw_info_pa); |
| 662 | kfree(vdev); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev, |
| 667 | void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar) |
| 668 | { |
| 669 | if (!vdev) { |
| 670 | vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC); |
| 671 | if (!vdev) |
| 672 | return NULL; |
| 673 | } |
| 674 | |
| 675 | vdev->priv = priv; |
| 676 | vdev->pdev = pdev; |
| 677 | |
| 678 | if (vnic_dev_discover_res(vdev, bar)) |
| 679 | goto err_out; |
| 680 | |
| 681 | vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0); |
| 682 | if (!vdev->devcmd) |
| 683 | goto err_out; |
| 684 | |
| 685 | return vdev; |
| 686 | |
| 687 | err_out: |
| 688 | vnic_dev_unregister(vdev); |
| 689 | return NULL; |
| 690 | } |
| 691 | |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame^] | 692 | |