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