Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 1 | /* |
Vasanthy Kolluri | 29046f9 | 2010-06-24 10:52:26 +0000 | [diff] [blame] | 2 | * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved. |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 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 | |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 32 | enum vnic_proxy_type { |
| 33 | PROXY_NONE, |
| 34 | PROXY_BY_BDF, |
| 35 | }; |
| 36 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 37 | struct vnic_res { |
| 38 | void __iomem *vaddr; |
Scott Feldman | 27e6c7d | 2009-09-03 17:01:53 +0000 | [diff] [blame] | 39 | dma_addr_t bus_addr; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 40 | unsigned int count; |
| 41 | }; |
| 42 | |
| 43 | struct vnic_dev { |
| 44 | void *priv; |
| 45 | struct pci_dev *pdev; |
| 46 | struct vnic_res res[RES_TYPE_MAX]; |
| 47 | enum vnic_dev_intr_mode intr_mode; |
| 48 | struct vnic_devcmd __iomem *devcmd; |
| 49 | struct vnic_devcmd_notify *notify; |
| 50 | struct vnic_devcmd_notify notify_copy; |
| 51 | dma_addr_t notify_pa; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 52 | u32 notify_sz; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 53 | dma_addr_t linkstatus_pa; |
| 54 | struct vnic_stats *stats; |
| 55 | dma_addr_t stats_pa; |
| 56 | struct vnic_devcmd_fw_info *fw_info; |
| 57 | dma_addr_t fw_info_pa; |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 58 | enum vnic_proxy_type proxy; |
| 59 | u32 proxy_index; |
| 60 | u64 args[VNIC_DEVCMD_NARGS]; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | #define VNIC_MAX_RES_HDR_SIZE \ |
| 64 | (sizeof(struct vnic_resource_header) + \ |
| 65 | sizeof(struct vnic_resource) * RES_TYPE_MAX) |
| 66 | #define VNIC_RES_STRIDE 128 |
| 67 | |
| 68 | void *vnic_dev_priv(struct vnic_dev *vdev) |
| 69 | { |
| 70 | return vdev->priv; |
| 71 | } |
| 72 | |
| 73 | static int vnic_dev_discover_res(struct vnic_dev *vdev, |
Scott Feldman | 27e6c7d | 2009-09-03 17:01:53 +0000 | [diff] [blame] | 74 | struct vnic_dev_bar *bar, unsigned int num_bars) |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 75 | { |
| 76 | struct vnic_resource_header __iomem *rh; |
| 77 | struct vnic_resource __iomem *r; |
| 78 | u8 type; |
| 79 | |
Scott Feldman | 27e6c7d | 2009-09-03 17:01:53 +0000 | [diff] [blame] | 80 | if (num_bars == 0) |
| 81 | return -EINVAL; |
| 82 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 83 | if (bar->len < VNIC_MAX_RES_HDR_SIZE) { |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 84 | pr_err("vNIC BAR0 res hdr length error\n"); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 85 | return -EINVAL; |
| 86 | } |
| 87 | |
| 88 | rh = bar->vaddr; |
| 89 | if (!rh) { |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 90 | pr_err("vNIC BAR0 res hdr not mem-mapped\n"); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 91 | return -EINVAL; |
| 92 | } |
| 93 | |
| 94 | if (ioread32(&rh->magic) != VNIC_RES_MAGIC || |
| 95 | ioread32(&rh->version) != VNIC_RES_VERSION) { |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 96 | pr_err("vNIC BAR0 res magic/version error " |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 97 | "exp (%lx/%lx) curr (%x/%x)\n", |
| 98 | VNIC_RES_MAGIC, VNIC_RES_VERSION, |
| 99 | ioread32(&rh->magic), ioread32(&rh->version)); |
| 100 | return -EINVAL; |
| 101 | } |
| 102 | |
| 103 | r = (struct vnic_resource __iomem *)(rh + 1); |
| 104 | |
| 105 | while ((type = ioread8(&r->type)) != RES_TYPE_EOL) { |
| 106 | |
| 107 | u8 bar_num = ioread8(&r->bar); |
| 108 | u32 bar_offset = ioread32(&r->bar_offset); |
| 109 | u32 count = ioread32(&r->count); |
| 110 | u32 len; |
| 111 | |
| 112 | r++; |
| 113 | |
Scott Feldman | 27e6c7d | 2009-09-03 17:01:53 +0000 | [diff] [blame] | 114 | if (bar_num >= num_bars) |
| 115 | continue; |
| 116 | |
| 117 | if (!bar[bar_num].len || !bar[bar_num].vaddr) |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 118 | continue; |
| 119 | |
| 120 | switch (type) { |
| 121 | case RES_TYPE_WQ: |
| 122 | case RES_TYPE_RQ: |
| 123 | case RES_TYPE_CQ: |
| 124 | case RES_TYPE_INTR_CTRL: |
| 125 | /* each count is stride bytes long */ |
| 126 | len = count * VNIC_RES_STRIDE; |
Scott Feldman | 27e6c7d | 2009-09-03 17:01:53 +0000 | [diff] [blame] | 127 | if (len + bar_offset > bar[bar_num].len) { |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 128 | pr_err("vNIC BAR0 resource %d " |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 129 | "out-of-bounds, offset 0x%x + " |
| 130 | "size 0x%x > bar len 0x%lx\n", |
| 131 | type, bar_offset, |
| 132 | len, |
Scott Feldman | 27e6c7d | 2009-09-03 17:01:53 +0000 | [diff] [blame] | 133 | bar[bar_num].len); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 134 | return -EINVAL; |
| 135 | } |
| 136 | break; |
| 137 | case RES_TYPE_INTR_PBA_LEGACY: |
| 138 | case RES_TYPE_DEVCMD: |
| 139 | len = count; |
| 140 | break; |
| 141 | default: |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | vdev->res[type].count = count; |
Scott Feldman | 27e6c7d | 2009-09-03 17:01:53 +0000 | [diff] [blame] | 146 | vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr + |
| 147 | bar_offset; |
| 148 | vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev, |
| 155 | enum vnic_res_type type) |
| 156 | { |
| 157 | return vdev->res[type].count; |
| 158 | } |
| 159 | |
| 160 | void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type, |
| 161 | unsigned int index) |
| 162 | { |
| 163 | if (!vdev->res[type].vaddr) |
| 164 | return NULL; |
| 165 | |
| 166 | switch (type) { |
| 167 | case RES_TYPE_WQ: |
| 168 | case RES_TYPE_RQ: |
| 169 | case RES_TYPE_CQ: |
| 170 | case RES_TYPE_INTR_CTRL: |
| 171 | return (char __iomem *)vdev->res[type].vaddr + |
| 172 | index * VNIC_RES_STRIDE; |
| 173 | default: |
| 174 | return (char __iomem *)vdev->res[type].vaddr; |
| 175 | } |
| 176 | } |
| 177 | |
Scott Feldman | 27e6c7d | 2009-09-03 17:01:53 +0000 | [diff] [blame] | 178 | dma_addr_t vnic_dev_get_res_bus_addr(struct vnic_dev *vdev, |
| 179 | enum vnic_res_type type, unsigned int index) |
| 180 | { |
| 181 | switch (type) { |
| 182 | case RES_TYPE_WQ: |
| 183 | case RES_TYPE_RQ: |
| 184 | case RES_TYPE_CQ: |
| 185 | case RES_TYPE_INTR_CTRL: |
| 186 | return vdev->res[type].bus_addr + |
| 187 | index * VNIC_RES_STRIDE; |
| 188 | default: |
| 189 | return vdev->res[type].bus_addr; |
| 190 | } |
| 191 | } |
| 192 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 193 | unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring, |
| 194 | unsigned int desc_count, unsigned int desc_size) |
| 195 | { |
| 196 | /* The base address of the desc rings must be 512 byte aligned. |
| 197 | * Descriptor count is aligned to groups of 32 descriptors. A |
| 198 | * count of 0 means the maximum 4096 descriptors. Descriptor |
| 199 | * size is aligned to 16 bytes. |
| 200 | */ |
| 201 | |
| 202 | unsigned int count_align = 32; |
| 203 | unsigned int desc_align = 16; |
| 204 | |
| 205 | ring->base_align = 512; |
| 206 | |
| 207 | if (desc_count == 0) |
| 208 | desc_count = 4096; |
| 209 | |
| 210 | ring->desc_count = ALIGN(desc_count, count_align); |
| 211 | |
| 212 | ring->desc_size = ALIGN(desc_size, desc_align); |
| 213 | |
| 214 | ring->size = ring->desc_count * ring->desc_size; |
| 215 | ring->size_unaligned = ring->size + ring->base_align; |
| 216 | |
| 217 | return ring->size_unaligned; |
| 218 | } |
| 219 | |
| 220 | void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring) |
| 221 | { |
| 222 | memset(ring->descs, 0, ring->size); |
| 223 | } |
| 224 | |
| 225 | int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring, |
| 226 | unsigned int desc_count, unsigned int desc_size) |
| 227 | { |
| 228 | vnic_dev_desc_ring_size(ring, desc_count, desc_size); |
| 229 | |
| 230 | ring->descs_unaligned = pci_alloc_consistent(vdev->pdev, |
| 231 | ring->size_unaligned, |
| 232 | &ring->base_addr_unaligned); |
| 233 | |
| 234 | if (!ring->descs_unaligned) { |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 235 | pr_err("Failed to allocate ring (size=%d), aborting\n", |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 236 | (int)ring->size); |
| 237 | return -ENOMEM; |
| 238 | } |
| 239 | |
| 240 | ring->base_addr = ALIGN(ring->base_addr_unaligned, |
| 241 | ring->base_align); |
| 242 | ring->descs = (u8 *)ring->descs_unaligned + |
| 243 | (ring->base_addr - ring->base_addr_unaligned); |
| 244 | |
| 245 | vnic_dev_clear_desc_ring(ring); |
| 246 | |
| 247 | ring->desc_avail = ring->desc_count - 1; |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring) |
| 253 | { |
| 254 | if (ring->descs) { |
| 255 | pci_free_consistent(vdev->pdev, |
| 256 | ring->size_unaligned, |
| 257 | ring->descs_unaligned, |
| 258 | ring->base_addr_unaligned); |
| 259 | ring->descs = NULL; |
| 260 | } |
| 261 | } |
| 262 | |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 263 | static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, |
| 264 | int wait) |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 265 | { |
| 266 | struct vnic_devcmd __iomem *devcmd = vdev->devcmd; |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 267 | unsigned int i; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 268 | int delay; |
| 269 | u32 status; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 270 | int err; |
| 271 | |
| 272 | status = ioread32(&devcmd->status); |
Vasanthy Kolluri | 506e119 | 2010-06-24 10:52:08 +0000 | [diff] [blame] | 273 | if (status == 0xFFFFFFFF) { |
| 274 | /* PCI-e target device is gone */ |
| 275 | return -ENODEV; |
| 276 | } |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 277 | if (status & STAT_BUSY) { |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 278 | pr_err("Busy devcmd %d\n", _CMD_N(cmd)); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 279 | return -EBUSY; |
| 280 | } |
| 281 | |
| 282 | if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) { |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 283 | for (i = 0; i < VNIC_DEVCMD_NARGS; i++) |
| 284 | writeq(vdev->args[i], &devcmd->args[i]); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 285 | wmb(); |
| 286 | } |
| 287 | |
| 288 | iowrite32(cmd, &devcmd->cmd); |
| 289 | |
| 290 | if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT)) |
Scott Feldman | 27e6c7d | 2009-09-03 17:01:53 +0000 | [diff] [blame] | 291 | return 0; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 292 | |
| 293 | for (delay = 0; delay < wait; delay++) { |
| 294 | |
| 295 | udelay(100); |
| 296 | |
| 297 | status = ioread32(&devcmd->status); |
Vasanthy Kolluri | 506e119 | 2010-06-24 10:52:08 +0000 | [diff] [blame] | 298 | if (status == 0xFFFFFFFF) { |
| 299 | /* PCI-e target device is gone */ |
| 300 | return -ENODEV; |
| 301 | } |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 302 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 303 | if (!(status & STAT_BUSY)) { |
| 304 | |
| 305 | if (status & STAT_ERROR) { |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 306 | err = (int)readq(&devcmd->args[0]); |
| 307 | if (err != ERR_ECMDUNKNOWN || |
| 308 | cmd != CMD_CAPABILITY) |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 309 | pr_err("Error %d devcmd %d\n", |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 310 | err, _CMD_N(cmd)); |
| 311 | return err; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (_CMD_DIR(cmd) & _CMD_DIR_READ) { |
| 315 | rmb(); |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 316 | for (i = 0; i < VNIC_DEVCMD_NARGS; i++) |
| 317 | vdev->args[i] = readq(&devcmd->args[i]); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | } |
| 323 | |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 324 | pr_err("Timedout devcmd %d\n", _CMD_N(cmd)); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 325 | return -ETIMEDOUT; |
| 326 | } |
| 327 | |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 328 | static int vnic_dev_cmd_proxy_by_bdf(struct vnic_dev *vdev, |
| 329 | enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait) |
| 330 | { |
| 331 | u32 status; |
| 332 | int err; |
| 333 | |
| 334 | memset(vdev->args, 0, sizeof(vdev->args)); |
| 335 | |
| 336 | vdev->args[0] = vdev->proxy_index; /* bdf */ |
| 337 | vdev->args[1] = cmd; |
| 338 | vdev->args[2] = *a0; |
| 339 | vdev->args[3] = *a1; |
| 340 | |
| 341 | err = _vnic_dev_cmd(vdev, CMD_PROXY_BY_BDF, wait); |
| 342 | if (err) |
| 343 | return err; |
| 344 | |
| 345 | status = (u32)vdev->args[0]; |
| 346 | if (status & STAT_ERROR) { |
| 347 | err = (int)vdev->args[1]; |
| 348 | if (err != ERR_ECMDUNKNOWN || |
| 349 | cmd != CMD_CAPABILITY) |
| 350 | pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd)); |
| 351 | return err; |
| 352 | } |
| 353 | |
| 354 | *a0 = vdev->args[1]; |
| 355 | *a1 = vdev->args[2]; |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev, |
| 361 | enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait) |
| 362 | { |
| 363 | int err; |
| 364 | |
| 365 | vdev->args[0] = *a0; |
| 366 | vdev->args[1] = *a1; |
| 367 | |
| 368 | err = _vnic_dev_cmd(vdev, cmd, wait); |
| 369 | |
| 370 | *a0 = vdev->args[0]; |
| 371 | *a1 = vdev->args[1]; |
| 372 | |
| 373 | return err; |
| 374 | } |
| 375 | |
| 376 | void vnic_dev_cmd_proxy_by_bdf_start(struct vnic_dev *vdev, u16 bdf) |
| 377 | { |
| 378 | vdev->proxy = PROXY_BY_BDF; |
| 379 | vdev->proxy_index = bdf; |
| 380 | } |
| 381 | |
| 382 | void vnic_dev_cmd_proxy_end(struct vnic_dev *vdev) |
| 383 | { |
| 384 | vdev->proxy = PROXY_NONE; |
| 385 | vdev->proxy_index = 0; |
| 386 | } |
| 387 | |
| 388 | int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd, |
| 389 | u64 *a0, u64 *a1, int wait) |
| 390 | { |
| 391 | memset(vdev->args, 0, sizeof(vdev->args)); |
| 392 | |
| 393 | switch (vdev->proxy) { |
| 394 | case PROXY_BY_BDF: |
| 395 | return vnic_dev_cmd_proxy_by_bdf(vdev, cmd, a0, a1, wait); |
| 396 | case PROXY_NONE: |
| 397 | default: |
| 398 | return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait); |
| 399 | } |
| 400 | } |
| 401 | |
Hannes Eder | 5e4232e | 2008-12-26 00:01:18 -0800 | [diff] [blame] | 402 | static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd) |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 403 | { |
| 404 | u64 a0 = (u32)cmd, a1 = 0; |
| 405 | int wait = 1000; |
| 406 | int err; |
| 407 | |
| 408 | err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait); |
| 409 | |
| 410 | return !(err || a0); |
| 411 | } |
| 412 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 413 | int vnic_dev_fw_info(struct vnic_dev *vdev, |
| 414 | struct vnic_devcmd_fw_info **fw_info) |
| 415 | { |
| 416 | u64 a0, a1 = 0; |
| 417 | int wait = 1000; |
| 418 | int err = 0; |
| 419 | |
| 420 | if (!vdev->fw_info) { |
| 421 | vdev->fw_info = pci_alloc_consistent(vdev->pdev, |
| 422 | sizeof(struct vnic_devcmd_fw_info), |
| 423 | &vdev->fw_info_pa); |
| 424 | if (!vdev->fw_info) |
| 425 | return -ENOMEM; |
| 426 | |
| 427 | a0 = vdev->fw_info_pa; |
| 428 | |
| 429 | /* only get fw_info once and cache it */ |
| 430 | err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait); |
| 431 | } |
| 432 | |
| 433 | *fw_info = vdev->fw_info; |
| 434 | |
| 435 | return err; |
| 436 | } |
| 437 | |
Scott Feldman | 4badc38 | 2009-09-03 17:01:58 +0000 | [diff] [blame] | 438 | int vnic_dev_hw_version(struct vnic_dev *vdev, enum vnic_dev_hw_version *hw_ver) |
| 439 | { |
| 440 | struct vnic_devcmd_fw_info *fw_info; |
| 441 | int err; |
| 442 | |
| 443 | err = vnic_dev_fw_info(vdev, &fw_info); |
| 444 | if (err) |
| 445 | return err; |
| 446 | |
| 447 | if (strncmp(fw_info->hw_version, "A1", sizeof("A1")) == 0) |
| 448 | *hw_ver = VNIC_DEV_HW_VER_A1; |
| 449 | else if (strncmp(fw_info->hw_version, "A2", sizeof("A2")) == 0) |
| 450 | *hw_ver = VNIC_DEV_HW_VER_A2; |
| 451 | else |
| 452 | *hw_ver = VNIC_DEV_HW_VER_UNKNOWN; |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 457 | int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size, |
| 458 | void *value) |
| 459 | { |
| 460 | u64 a0, a1; |
| 461 | int wait = 1000; |
| 462 | int err; |
| 463 | |
| 464 | a0 = offset; |
| 465 | a1 = size; |
| 466 | |
| 467 | err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait); |
| 468 | |
| 469 | switch (size) { |
| 470 | case 1: *(u8 *)value = (u8)a0; break; |
| 471 | case 2: *(u16 *)value = (u16)a0; break; |
| 472 | case 4: *(u32 *)value = (u32)a0; break; |
| 473 | case 8: *(u64 *)value = a0; break; |
| 474 | default: BUG(); break; |
| 475 | } |
| 476 | |
| 477 | return err; |
| 478 | } |
| 479 | |
| 480 | int vnic_dev_stats_clear(struct vnic_dev *vdev) |
| 481 | { |
| 482 | u64 a0 = 0, a1 = 0; |
| 483 | int wait = 1000; |
| 484 | return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait); |
| 485 | } |
| 486 | |
| 487 | int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats) |
| 488 | { |
| 489 | u64 a0, a1; |
| 490 | int wait = 1000; |
| 491 | |
| 492 | if (!vdev->stats) { |
| 493 | vdev->stats = pci_alloc_consistent(vdev->pdev, |
| 494 | sizeof(struct vnic_stats), &vdev->stats_pa); |
| 495 | if (!vdev->stats) |
| 496 | return -ENOMEM; |
| 497 | } |
| 498 | |
| 499 | *stats = vdev->stats; |
| 500 | a0 = vdev->stats_pa; |
| 501 | a1 = sizeof(struct vnic_stats); |
| 502 | |
| 503 | return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait); |
| 504 | } |
| 505 | |
| 506 | int vnic_dev_close(struct vnic_dev *vdev) |
| 507 | { |
| 508 | u64 a0 = 0, a1 = 0; |
| 509 | int wait = 1000; |
| 510 | return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait); |
| 511 | } |
| 512 | |
| 513 | int vnic_dev_enable(struct vnic_dev *vdev) |
| 514 | { |
| 515 | u64 a0 = 0, a1 = 0; |
| 516 | int wait = 1000; |
| 517 | return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait); |
| 518 | } |
| 519 | |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 520 | int vnic_dev_enable_wait(struct vnic_dev *vdev) |
| 521 | { |
| 522 | u64 a0 = 0, a1 = 0; |
| 523 | int wait = 1000; |
| 524 | int err; |
| 525 | |
| 526 | err = vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait); |
| 527 | if (err == ERR_ECMDUNKNOWN) |
| 528 | return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait); |
| 529 | |
| 530 | return err; |
| 531 | } |
| 532 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 533 | int vnic_dev_disable(struct vnic_dev *vdev) |
| 534 | { |
| 535 | u64 a0 = 0, a1 = 0; |
| 536 | int wait = 1000; |
| 537 | return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait); |
| 538 | } |
| 539 | |
| 540 | int vnic_dev_open(struct vnic_dev *vdev, int arg) |
| 541 | { |
| 542 | u64 a0 = (u32)arg, a1 = 0; |
| 543 | int wait = 1000; |
| 544 | return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait); |
| 545 | } |
| 546 | |
| 547 | int vnic_dev_open_done(struct vnic_dev *vdev, int *done) |
| 548 | { |
| 549 | u64 a0 = 0, a1 = 0; |
| 550 | int wait = 1000; |
| 551 | int err; |
| 552 | |
| 553 | *done = 0; |
| 554 | |
| 555 | err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait); |
| 556 | if (err) |
| 557 | return err; |
| 558 | |
| 559 | *done = (a0 == 0); |
| 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg) |
| 565 | { |
| 566 | u64 a0 = (u32)arg, a1 = 0; |
| 567 | int wait = 1000; |
| 568 | return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait); |
| 569 | } |
| 570 | |
| 571 | int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done) |
| 572 | { |
| 573 | u64 a0 = 0, a1 = 0; |
| 574 | int wait = 1000; |
| 575 | int err; |
| 576 | |
| 577 | *done = 0; |
| 578 | |
| 579 | err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait); |
| 580 | if (err) |
| 581 | return err; |
| 582 | |
| 583 | *done = (a0 == 0); |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
Vasanthy Kolluri | 99ef563 | 2010-06-24 10:50:00 +0000 | [diff] [blame] | 588 | int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg) |
| 589 | { |
| 590 | u64 a0 = (u32)arg, a1 = 0; |
| 591 | int wait = 1000; |
| 592 | int err; |
| 593 | |
| 594 | err = vnic_dev_cmd(vdev, CMD_HANG_RESET, &a0, &a1, wait); |
| 595 | if (err == ERR_ECMDUNKNOWN) { |
| 596 | err = vnic_dev_soft_reset(vdev, arg); |
| 597 | if (err) |
| 598 | return err; |
| 599 | |
| 600 | return vnic_dev_init(vdev, 0); |
| 601 | } |
| 602 | |
| 603 | return err; |
| 604 | } |
| 605 | |
| 606 | int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done) |
| 607 | { |
| 608 | u64 a0 = 0, a1 = 0; |
| 609 | int wait = 1000; |
| 610 | int err; |
| 611 | |
| 612 | *done = 0; |
| 613 | |
| 614 | err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS, &a0, &a1, wait); |
| 615 | if (err) { |
| 616 | if (err == ERR_ECMDUNKNOWN) |
| 617 | return vnic_dev_soft_reset_done(vdev, done); |
| 618 | return err; |
| 619 | } |
| 620 | |
| 621 | *done = (a0 == 0); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 626 | int vnic_dev_hang_notify(struct vnic_dev *vdev) |
| 627 | { |
| 628 | u64 a0, a1; |
| 629 | int wait = 1000; |
| 630 | return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait); |
| 631 | } |
| 632 | |
| 633 | int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr) |
| 634 | { |
| 635 | u64 a0, a1; |
| 636 | int wait = 1000; |
| 637 | int err, i; |
| 638 | |
| 639 | for (i = 0; i < ETH_ALEN; i++) |
| 640 | mac_addr[i] = 0; |
| 641 | |
| 642 | err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait); |
| 643 | if (err) |
| 644 | return err; |
| 645 | |
| 646 | for (i = 0; i < ETH_ALEN; i++) |
| 647 | mac_addr[i] = ((u8 *)&a0)[i]; |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
Vasanthy Kolluri | 383ab92 | 2010-06-24 10:50:12 +0000 | [diff] [blame] | 652 | int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast, |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 653 | int broadcast, int promisc, int allmulti) |
| 654 | { |
| 655 | u64 a0, a1 = 0; |
| 656 | int wait = 1000; |
| 657 | int err; |
| 658 | |
| 659 | a0 = (directed ? CMD_PFILTER_DIRECTED : 0) | |
| 660 | (multicast ? CMD_PFILTER_MULTICAST : 0) | |
| 661 | (broadcast ? CMD_PFILTER_BROADCAST : 0) | |
| 662 | (promisc ? CMD_PFILTER_PROMISCUOUS : 0) | |
| 663 | (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0); |
| 664 | |
| 665 | err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait); |
| 666 | if (err) |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 667 | pr_err("Can't set packet filter\n"); |
Vasanthy Kolluri | 383ab92 | 2010-06-24 10:50:12 +0000 | [diff] [blame] | 668 | |
| 669 | return err; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 672 | int vnic_dev_packet_filter_all(struct vnic_dev *vdev, int directed, |
| 673 | int multicast, int broadcast, int promisc, int allmulti) |
| 674 | { |
| 675 | u64 a0, a1 = 0; |
| 676 | int wait = 1000; |
| 677 | int err; |
| 678 | |
| 679 | a0 = (directed ? CMD_PFILTER_DIRECTED : 0) | |
| 680 | (multicast ? CMD_PFILTER_MULTICAST : 0) | |
| 681 | (broadcast ? CMD_PFILTER_BROADCAST : 0) | |
| 682 | (promisc ? CMD_PFILTER_PROMISCUOUS : 0) | |
| 683 | (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0); |
| 684 | |
| 685 | err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER_ALL, &a0, &a1, wait); |
| 686 | if (err) |
| 687 | pr_err("Can't set packet filter\n"); |
| 688 | |
| 689 | return err; |
| 690 | } |
| 691 | |
Scott Feldman | f8bd909 | 2010-05-17 22:50:19 -0700 | [diff] [blame] | 692 | int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr) |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 693 | { |
| 694 | u64 a0 = 0, a1 = 0; |
| 695 | int wait = 1000; |
| 696 | int err; |
| 697 | int i; |
| 698 | |
| 699 | for (i = 0; i < ETH_ALEN; i++) |
| 700 | ((u8 *)&a0)[i] = addr[i]; |
| 701 | |
| 702 | err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); |
| 703 | if (err) |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 704 | pr_err("Can't add addr [%pM], %d\n", addr, err); |
Scott Feldman | f8bd909 | 2010-05-17 22:50:19 -0700 | [diff] [blame] | 705 | |
| 706 | return err; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Scott Feldman | f8bd909 | 2010-05-17 22:50:19 -0700 | [diff] [blame] | 709 | int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr) |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 710 | { |
| 711 | u64 a0 = 0, a1 = 0; |
| 712 | int wait = 1000; |
| 713 | int err; |
| 714 | int i; |
| 715 | |
| 716 | for (i = 0; i < ETH_ALEN; i++) |
| 717 | ((u8 *)&a0)[i] = addr[i]; |
| 718 | |
| 719 | err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait); |
| 720 | if (err) |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 721 | pr_err("Can't del addr [%pM], %d\n", addr, err); |
Scott Feldman | f8bd909 | 2010-05-17 22:50:19 -0700 | [diff] [blame] | 722 | |
| 723 | return err; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Vasanthy Kolluri | f8cac14 | 2010-06-24 10:49:51 +0000 | [diff] [blame] | 726 | int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev, |
| 727 | u8 ig_vlan_rewrite_mode) |
| 728 | { |
| 729 | u64 a0 = ig_vlan_rewrite_mode, a1 = 0; |
| 730 | int wait = 1000; |
| 731 | int err; |
| 732 | |
| 733 | err = vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE, &a0, &a1, wait); |
| 734 | if (err == ERR_ECMDUNKNOWN) |
| 735 | return 0; |
| 736 | |
| 737 | return err; |
| 738 | } |
| 739 | |
Scott Feldman | d73149f | 2009-09-03 17:02:35 +0000 | [diff] [blame] | 740 | int vnic_dev_raise_intr(struct vnic_dev *vdev, u16 intr) |
| 741 | { |
| 742 | u64 a0 = intr, a1 = 0; |
| 743 | int wait = 1000; |
| 744 | int err; |
| 745 | |
| 746 | err = vnic_dev_cmd(vdev, CMD_IAR, &a0, &a1, wait); |
| 747 | if (err) |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 748 | pr_err("Failed to raise INTR[%d], err %d\n", intr, err); |
Scott Feldman | d73149f | 2009-09-03 17:02:35 +0000 | [diff] [blame] | 749 | |
| 750 | return err; |
| 751 | } |
| 752 | |
Vasanthy Kolluri | d883aa7 | 2010-03-18 16:19:59 +0000 | [diff] [blame] | 753 | int vnic_dev_notify_setcmd(struct vnic_dev *vdev, |
| 754 | void *notify_addr, dma_addr_t notify_pa, u16 intr) |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 755 | { |
| 756 | u64 a0, a1; |
| 757 | int wait = 1000; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 758 | int r; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 759 | |
Vasanthy Kolluri | d883aa7 | 2010-03-18 16:19:59 +0000 | [diff] [blame] | 760 | memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify)); |
| 761 | vdev->notify = notify_addr; |
| 762 | vdev->notify_pa = notify_pa; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 763 | |
Vasanthy Kolluri | d883aa7 | 2010-03-18 16:19:59 +0000 | [diff] [blame] | 764 | a0 = (u64)notify_pa; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 765 | a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL; |
| 766 | a1 += sizeof(struct vnic_devcmd_notify); |
| 767 | |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 768 | r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); |
| 769 | vdev->notify_sz = (r == 0) ? (u32)a1 : 0; |
| 770 | return r; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Vasanthy Kolluri | d883aa7 | 2010-03-18 16:19:59 +0000 | [diff] [blame] | 773 | int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr) |
| 774 | { |
| 775 | void *notify_addr; |
| 776 | dma_addr_t notify_pa; |
| 777 | |
| 778 | if (vdev->notify || vdev->notify_pa) { |
Vasanthy Kolluri | a7a79de | 2010-06-24 10:50:56 +0000 | [diff] [blame] | 779 | pr_err("notify block %p still allocated", vdev->notify); |
Vasanthy Kolluri | d883aa7 | 2010-03-18 16:19:59 +0000 | [diff] [blame] | 780 | return -EINVAL; |
| 781 | } |
| 782 | |
| 783 | notify_addr = pci_alloc_consistent(vdev->pdev, |
| 784 | sizeof(struct vnic_devcmd_notify), |
| 785 | ¬ify_pa); |
| 786 | if (!notify_addr) |
| 787 | return -ENOMEM; |
| 788 | |
| 789 | return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr); |
| 790 | } |
| 791 | |
Vasanthy Kolluri | 383ab92 | 2010-06-24 10:50:12 +0000 | [diff] [blame] | 792 | int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev) |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 793 | { |
| 794 | u64 a0, a1; |
| 795 | int wait = 1000; |
Vasanthy Kolluri | 383ab92 | 2010-06-24 10:50:12 +0000 | [diff] [blame] | 796 | int err; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 797 | |
| 798 | a0 = 0; /* paddr = 0 to unset notify buffer */ |
| 799 | a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */ |
| 800 | a1 += sizeof(struct vnic_devcmd_notify); |
| 801 | |
Vasanthy Kolluri | 383ab92 | 2010-06-24 10:50:12 +0000 | [diff] [blame] | 802 | err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait); |
Vasanthy Kolluri | d883aa7 | 2010-03-18 16:19:59 +0000 | [diff] [blame] | 803 | vdev->notify = NULL; |
| 804 | vdev->notify_pa = 0; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 805 | vdev->notify_sz = 0; |
Vasanthy Kolluri | 383ab92 | 2010-06-24 10:50:12 +0000 | [diff] [blame] | 806 | |
| 807 | return err; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Vasanthy Kolluri | 383ab92 | 2010-06-24 10:50:12 +0000 | [diff] [blame] | 810 | int vnic_dev_notify_unset(struct vnic_dev *vdev) |
Vasanthy Kolluri | d883aa7 | 2010-03-18 16:19:59 +0000 | [diff] [blame] | 811 | { |
| 812 | if (vdev->notify) { |
| 813 | pci_free_consistent(vdev->pdev, |
| 814 | sizeof(struct vnic_devcmd_notify), |
| 815 | vdev->notify, |
| 816 | vdev->notify_pa); |
| 817 | } |
| 818 | |
Vasanthy Kolluri | 383ab92 | 2010-06-24 10:50:12 +0000 | [diff] [blame] | 819 | return vnic_dev_notify_unsetcmd(vdev); |
Vasanthy Kolluri | d883aa7 | 2010-03-18 16:19:59 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 822 | static int vnic_dev_notify_ready(struct vnic_dev *vdev) |
| 823 | { |
| 824 | u32 *words; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 825 | unsigned int nwords = vdev->notify_sz / 4; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 826 | unsigned int i; |
| 827 | u32 csum; |
| 828 | |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 829 | if (!vdev->notify || !vdev->notify_sz) |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 830 | return 0; |
| 831 | |
| 832 | do { |
| 833 | csum = 0; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 834 | memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 835 | words = (u32 *)&vdev->notify_copy; |
| 836 | for (i = 1; i < nwords; i++) |
| 837 | csum += words[i]; |
| 838 | } while (csum != words[0]); |
| 839 | |
| 840 | return 1; |
| 841 | } |
| 842 | |
| 843 | int vnic_dev_init(struct vnic_dev *vdev, int arg) |
| 844 | { |
| 845 | u64 a0 = (u32)arg, a1 = 0; |
| 846 | int wait = 1000; |
Scott Feldman | 4cdc44a | 2009-02-09 23:25:33 -0800 | [diff] [blame] | 847 | int r = 0; |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 848 | |
Vasanthy Kolluri | 29046f9 | 2010-06-24 10:52:26 +0000 | [diff] [blame] | 849 | if (vnic_dev_capable(vdev, CMD_INIT)) |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 850 | r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait); |
| 851 | else { |
| 852 | vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait); |
| 853 | if (a0 & CMD_INITF_DEFAULT_MAC) { |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 854 | /* Emulate these for old CMD_INIT_v1 which |
| 855 | * didn't pass a0 so no CMD_INITF_*. |
| 856 | */ |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 857 | vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait); |
| 858 | vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait); |
| 859 | } |
Scott Feldman | 4cdc44a | 2009-02-09 23:25:33 -0800 | [diff] [blame] | 860 | } |
| 861 | return r; |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Scott Feldman | f8bd909 | 2010-05-17 22:50:19 -0700 | [diff] [blame] | 864 | int vnic_dev_init_done(struct vnic_dev *vdev, int *done, int *err) |
| 865 | { |
| 866 | u64 a0 = 0, a1 = 0; |
| 867 | int wait = 1000; |
| 868 | int ret; |
| 869 | |
| 870 | *done = 0; |
| 871 | |
| 872 | ret = vnic_dev_cmd(vdev, CMD_INIT_STATUS, &a0, &a1, wait); |
| 873 | if (ret) |
| 874 | return ret; |
| 875 | |
| 876 | *done = (a0 == 0); |
| 877 | |
Vasanthy Kolluri | 70feadf | 2010-06-24 10:51:43 +0000 | [diff] [blame] | 878 | *err = (a0 == 0) ? (int)a1:0; |
Scott Feldman | f8bd909 | 2010-05-17 22:50:19 -0700 | [diff] [blame] | 879 | |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | int vnic_dev_init_prov(struct vnic_dev *vdev, u8 *buf, u32 len) |
| 884 | { |
| 885 | u64 a0, a1 = len; |
| 886 | int wait = 1000; |
Randy Dunlap | d49aba8 | 2010-06-08 07:00:20 +0000 | [diff] [blame] | 887 | dma_addr_t prov_pa; |
Scott Feldman | f8bd909 | 2010-05-17 22:50:19 -0700 | [diff] [blame] | 888 | void *prov_buf; |
| 889 | int ret; |
| 890 | |
| 891 | prov_buf = pci_alloc_consistent(vdev->pdev, len, &prov_pa); |
| 892 | if (!prov_buf) |
| 893 | return -ENOMEM; |
| 894 | |
| 895 | memcpy(prov_buf, buf, len); |
| 896 | |
| 897 | a0 = prov_pa; |
| 898 | |
| 899 | ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO, &a0, &a1, wait); |
| 900 | |
| 901 | pci_free_consistent(vdev->pdev, len, prov_buf, prov_pa); |
| 902 | |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | int vnic_dev_deinit(struct vnic_dev *vdev) |
| 907 | { |
| 908 | u64 a0 = 0, a1 = 0; |
| 909 | int wait = 1000; |
| 910 | |
| 911 | return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait); |
| 912 | } |
| 913 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 914 | int vnic_dev_link_status(struct vnic_dev *vdev) |
| 915 | { |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 916 | if (!vnic_dev_notify_ready(vdev)) |
| 917 | return 0; |
| 918 | |
| 919 | return vdev->notify_copy.link_state; |
| 920 | } |
| 921 | |
| 922 | u32 vnic_dev_port_speed(struct vnic_dev *vdev) |
| 923 | { |
| 924 | if (!vnic_dev_notify_ready(vdev)) |
| 925 | return 0; |
| 926 | |
| 927 | return vdev->notify_copy.port_speed; |
| 928 | } |
| 929 | |
| 930 | u32 vnic_dev_msg_lvl(struct vnic_dev *vdev) |
| 931 | { |
| 932 | if (!vnic_dev_notify_ready(vdev)) |
| 933 | return 0; |
| 934 | |
| 935 | return vdev->notify_copy.msglvl; |
| 936 | } |
| 937 | |
| 938 | u32 vnic_dev_mtu(struct vnic_dev *vdev) |
| 939 | { |
| 940 | if (!vnic_dev_notify_ready(vdev)) |
| 941 | return 0; |
| 942 | |
| 943 | return vdev->notify_copy.mtu; |
| 944 | } |
| 945 | |
Scott Feldman | 4cdc44a | 2009-02-09 23:25:33 -0800 | [diff] [blame] | 946 | u32 vnic_dev_link_down_cnt(struct vnic_dev *vdev) |
| 947 | { |
| 948 | if (!vnic_dev_notify_ready(vdev)) |
| 949 | return 0; |
| 950 | |
| 951 | return vdev->notify_copy.link_down_cnt; |
| 952 | } |
| 953 | |
| 954 | u32 vnic_dev_notify_status(struct vnic_dev *vdev) |
| 955 | { |
| 956 | if (!vnic_dev_notify_ready(vdev)) |
| 957 | return 0; |
| 958 | |
| 959 | return vdev->notify_copy.status; |
| 960 | } |
| 961 | |
Vasanthy Kolluri | 383ab92 | 2010-06-24 10:50:12 +0000 | [diff] [blame] | 962 | u32 vnic_dev_uif(struct vnic_dev *vdev) |
| 963 | { |
| 964 | if (!vnic_dev_notify_ready(vdev)) |
| 965 | return 0; |
| 966 | |
| 967 | return vdev->notify_copy.uif; |
| 968 | } |
| 969 | |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 970 | void vnic_dev_set_intr_mode(struct vnic_dev *vdev, |
| 971 | enum vnic_dev_intr_mode intr_mode) |
| 972 | { |
| 973 | vdev->intr_mode = intr_mode; |
| 974 | } |
| 975 | |
| 976 | enum vnic_dev_intr_mode vnic_dev_get_intr_mode( |
| 977 | struct vnic_dev *vdev) |
| 978 | { |
| 979 | return vdev->intr_mode; |
| 980 | } |
| 981 | |
| 982 | void vnic_dev_unregister(struct vnic_dev *vdev) |
| 983 | { |
| 984 | if (vdev) { |
| 985 | if (vdev->notify) |
| 986 | pci_free_consistent(vdev->pdev, |
| 987 | sizeof(struct vnic_devcmd_notify), |
| 988 | vdev->notify, |
| 989 | vdev->notify_pa); |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 990 | if (vdev->stats) |
| 991 | pci_free_consistent(vdev->pdev, |
Vasanthy Kolluri | 29046f9 | 2010-06-24 10:52:26 +0000 | [diff] [blame] | 992 | sizeof(struct vnic_stats), |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 993 | vdev->stats, vdev->stats_pa); |
| 994 | if (vdev->fw_info) |
| 995 | pci_free_consistent(vdev->pdev, |
| 996 | sizeof(struct vnic_devcmd_fw_info), |
| 997 | vdev->fw_info, vdev->fw_info_pa); |
| 998 | kfree(vdev); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev, |
Scott Feldman | 27e6c7d | 2009-09-03 17:01:53 +0000 | [diff] [blame] | 1003 | void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar, |
| 1004 | unsigned int num_bars) |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 1005 | { |
| 1006 | if (!vdev) { |
| 1007 | vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC); |
| 1008 | if (!vdev) |
| 1009 | return NULL; |
| 1010 | } |
| 1011 | |
| 1012 | vdev->priv = priv; |
| 1013 | vdev->pdev = pdev; |
| 1014 | |
Scott Feldman | 27e6c7d | 2009-09-03 17:01:53 +0000 | [diff] [blame] | 1015 | if (vnic_dev_discover_res(vdev, bar, num_bars)) |
Scott Feldman | 01f2e4e | 2008-09-15 09:17:11 -0700 | [diff] [blame] | 1016 | goto err_out; |
| 1017 | |
| 1018 | vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0); |
| 1019 | if (!vdev->devcmd) |
| 1020 | goto err_out; |
| 1021 | |
| 1022 | return vdev; |
| 1023 | |
| 1024 | err_out: |
| 1025 | vnic_dev_unregister(vdev); |
| 1026 | return NULL; |
| 1027 | } |
| 1028 | |
Scott Feldman | 27372bf | 2008-11-21 21:28:18 -0800 | [diff] [blame] | 1029 | |