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