blob: c489e72107de636439fc0f0182da8346b83a3797 [file] [log] [blame]
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001/*
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00002 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
Scott Feldman01f2e4e2008-09-15 09:17:11 -07003 * 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 Kolluri70feadf2010-06-24 10:51:43 +000032enum vnic_proxy_type {
33 PROXY_NONE,
34 PROXY_BY_BDF,
35};
36
Scott Feldman01f2e4e2008-09-15 09:17:11 -070037struct vnic_res {
38 void __iomem *vaddr;
Scott Feldman27e6c7d2009-09-03 17:01:53 +000039 dma_addr_t bus_addr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -070040 unsigned int count;
41};
42
43struct vnic_dev {
44 void *priv;
45 struct pci_dev *pdev;
46 struct vnic_res res[RES_TYPE_MAX];
47 enum vnic_dev_intr_mode intr_mode;
48 struct vnic_devcmd __iomem *devcmd;
49 struct vnic_devcmd_notify *notify;
50 struct vnic_devcmd_notify notify_copy;
51 dma_addr_t notify_pa;
Scott Feldman27372bf2008-11-21 21:28:18 -080052 u32 notify_sz;
Scott Feldman01f2e4e2008-09-15 09:17:11 -070053 dma_addr_t linkstatus_pa;
54 struct vnic_stats *stats;
55 dma_addr_t stats_pa;
56 struct vnic_devcmd_fw_info *fw_info;
57 dma_addr_t fw_info_pa;
Vasanthy Kolluri70feadf2010-06-24 10:51:43 +000058 enum vnic_proxy_type proxy;
59 u32 proxy_index;
60 u64 args[VNIC_DEVCMD_NARGS];
Scott Feldman01f2e4e2008-09-15 09:17:11 -070061};
62
63#define VNIC_MAX_RES_HDR_SIZE \
64 (sizeof(struct vnic_resource_header) + \
65 sizeof(struct vnic_resource) * RES_TYPE_MAX)
66#define VNIC_RES_STRIDE 128
67
68void *vnic_dev_priv(struct vnic_dev *vdev)
69{
70 return vdev->priv;
71}
72
73static int vnic_dev_discover_res(struct vnic_dev *vdev,
Scott Feldman27e6c7d2009-09-03 17:01:53 +000074 struct vnic_dev_bar *bar, unsigned int num_bars)
Scott Feldman01f2e4e2008-09-15 09:17:11 -070075{
76 struct vnic_resource_header __iomem *rh;
Roopa Prabhu90cf0b52010-08-10 18:55:00 +000077 struct mgmt_barmap_hdr __iomem *mrh;
Scott Feldman01f2e4e2008-09-15 09:17:11 -070078 struct vnic_resource __iomem *r;
79 u8 type;
80
Scott Feldman27e6c7d2009-09-03 17:01:53 +000081 if (num_bars == 0)
82 return -EINVAL;
83
Scott Feldman01f2e4e2008-09-15 09:17:11 -070084 if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +000085 pr_err("vNIC BAR0 res hdr length error\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -070086 return -EINVAL;
87 }
88
Roopa Prabhu90cf0b52010-08-10 18:55:00 +000089 rh = bar->vaddr;
90 mrh = bar->vaddr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -070091 if (!rh) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +000092 pr_err("vNIC BAR0 res hdr not mem-mapped\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -070093 return -EINVAL;
94 }
95
Roopa Prabhu90cf0b52010-08-10 18:55:00 +000096 /* Check for mgmt vnic in addition to normal vnic */
97 if ((ioread32(&rh->magic) != VNIC_RES_MAGIC) ||
98 (ioread32(&rh->version) != VNIC_RES_VERSION)) {
99 if ((ioread32(&mrh->magic) != MGMTVNIC_MAGIC) ||
100 (ioread32(&mrh->version) != MGMTVNIC_VERSION)) {
101 pr_err("vNIC BAR0 res magic/version error "
102 "exp (%lx/%lx) or (%lx/%lx), curr (%x/%x)\n",
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700103 VNIC_RES_MAGIC, VNIC_RES_VERSION,
Roopa Prabhu90cf0b52010-08-10 18:55:00 +0000104 MGMTVNIC_MAGIC, MGMTVNIC_VERSION,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700105 ioread32(&rh->magic), ioread32(&rh->version));
Roopa Prabhu90cf0b52010-08-10 18:55:00 +0000106 return -EINVAL;
107 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700108 }
109
Roopa Prabhu90cf0b52010-08-10 18:55:00 +0000110 if (ioread32(&mrh->magic) == MGMTVNIC_MAGIC)
111 r = (struct vnic_resource __iomem *)(mrh + 1);
112 else
113 r = (struct vnic_resource __iomem *)(rh + 1);
114
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700115
116 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
117
118 u8 bar_num = ioread8(&r->bar);
119 u32 bar_offset = ioread32(&r->bar_offset);
120 u32 count = ioread32(&r->count);
121 u32 len;
122
123 r++;
124
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000125 if (bar_num >= num_bars)
126 continue;
127
128 if (!bar[bar_num].len || !bar[bar_num].vaddr)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700129 continue;
130
131 switch (type) {
132 case RES_TYPE_WQ:
133 case RES_TYPE_RQ:
134 case RES_TYPE_CQ:
135 case RES_TYPE_INTR_CTRL:
136 /* each count is stride bytes long */
137 len = count * VNIC_RES_STRIDE;
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000138 if (len + bar_offset > bar[bar_num].len) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000139 pr_err("vNIC BAR0 resource %d "
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700140 "out-of-bounds, offset 0x%x + "
141 "size 0x%x > bar len 0x%lx\n",
142 type, bar_offset,
143 len,
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000144 bar[bar_num].len);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700145 return -EINVAL;
146 }
147 break;
148 case RES_TYPE_INTR_PBA_LEGACY:
149 case RES_TYPE_DEVCMD:
150 len = count;
151 break;
152 default:
153 continue;
154 }
155
156 vdev->res[type].count = count;
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000157 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
158 bar_offset;
159 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700160 }
161
162 return 0;
163}
164
165unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
166 enum vnic_res_type type)
167{
168 return vdev->res[type].count;
169}
170
171void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
172 unsigned int index)
173{
174 if (!vdev->res[type].vaddr)
175 return NULL;
176
177 switch (type) {
178 case RES_TYPE_WQ:
179 case RES_TYPE_RQ:
180 case RES_TYPE_CQ:
181 case RES_TYPE_INTR_CTRL:
182 return (char __iomem *)vdev->res[type].vaddr +
183 index * VNIC_RES_STRIDE;
184 default:
185 return (char __iomem *)vdev->res[type].vaddr;
186 }
187}
188
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +0000189static unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700190 unsigned int desc_count, unsigned int desc_size)
191{
192 /* The base address of the desc rings must be 512 byte aligned.
193 * Descriptor count is aligned to groups of 32 descriptors. A
194 * count of 0 means the maximum 4096 descriptors. Descriptor
195 * size is aligned to 16 bytes.
196 */
197
198 unsigned int count_align = 32;
199 unsigned int desc_align = 16;
200
201 ring->base_align = 512;
202
203 if (desc_count == 0)
204 desc_count = 4096;
205
206 ring->desc_count = ALIGN(desc_count, count_align);
207
208 ring->desc_size = ALIGN(desc_size, desc_align);
209
210 ring->size = ring->desc_count * ring->desc_size;
211 ring->size_unaligned = ring->size + ring->base_align;
212
213 return ring->size_unaligned;
214}
215
216void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
217{
218 memset(ring->descs, 0, ring->size);
219}
220
221int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring,
222 unsigned int desc_count, unsigned int desc_size)
223{
224 vnic_dev_desc_ring_size(ring, desc_count, desc_size);
225
226 ring->descs_unaligned = pci_alloc_consistent(vdev->pdev,
227 ring->size_unaligned,
228 &ring->base_addr_unaligned);
229
230 if (!ring->descs_unaligned) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000231 pr_err("Failed to allocate ring (size=%d), aborting\n",
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700232 (int)ring->size);
233 return -ENOMEM;
234 }
235
236 ring->base_addr = ALIGN(ring->base_addr_unaligned,
237 ring->base_align);
238 ring->descs = (u8 *)ring->descs_unaligned +
239 (ring->base_addr - ring->base_addr_unaligned);
240
241 vnic_dev_clear_desc_ring(ring);
242
243 ring->desc_avail = ring->desc_count - 1;
244
245 return 0;
246}
247
248void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
249{
250 if (ring->descs) {
251 pci_free_consistent(vdev->pdev,
252 ring->size_unaligned,
253 ring->descs_unaligned,
254 ring->base_addr_unaligned);
255 ring->descs = NULL;
256 }
257}
258
Vasanthy Kolluri70feadf2010-06-24 10:51:43 +0000259static int _vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
260 int wait)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700261{
262 struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
Vasanthy Kolluri70feadf2010-06-24 10:51:43 +0000263 unsigned int i;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700264 int delay;
265 u32 status;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700266 int err;
267
268 status = ioread32(&devcmd->status);
Vasanthy Kolluri506e1192010-06-24 10:52:08 +0000269 if (status == 0xFFFFFFFF) {
270 /* PCI-e target device is gone */
271 return -ENODEV;
272 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700273 if (status & STAT_BUSY) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000274 pr_err("Busy devcmd %d\n", _CMD_N(cmd));
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700275 return -EBUSY;
276 }
277
278 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
Vasanthy Kolluri70feadf2010-06-24 10:51:43 +0000279 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
280 writeq(vdev->args[i], &devcmd->args[i]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700281 wmb();
282 }
283
284 iowrite32(cmd, &devcmd->cmd);
285
286 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000287 return 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700288
289 for (delay = 0; delay < wait; delay++) {
290
291 udelay(100);
292
293 status = ioread32(&devcmd->status);
Vasanthy Kolluri506e1192010-06-24 10:52:08 +0000294 if (status == 0xFFFFFFFF) {
295 /* PCI-e target device is gone */
296 return -ENODEV;
297 }
Vasanthy Kolluri70feadf2010-06-24 10:51:43 +0000298
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700299 if (!(status & STAT_BUSY)) {
300
301 if (status & STAT_ERROR) {
Scott Feldman27372bf2008-11-21 21:28:18 -0800302 err = (int)readq(&devcmd->args[0]);
303 if (err != ERR_ECMDUNKNOWN ||
304 cmd != CMD_CAPABILITY)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000305 pr_err("Error %d devcmd %d\n",
Scott Feldman27372bf2008-11-21 21:28:18 -0800306 err, _CMD_N(cmd));
307 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700308 }
309
310 if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
311 rmb();
Vasanthy Kolluri70feadf2010-06-24 10:51:43 +0000312 for (i = 0; i < VNIC_DEVCMD_NARGS; i++)
313 vdev->args[i] = readq(&devcmd->args[i]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700314 }
315
316 return 0;
317 }
318 }
319
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000320 pr_err("Timedout devcmd %d\n", _CMD_N(cmd));
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700321 return -ETIMEDOUT;
322}
323
Vasanthy Kolluri70feadf2010-06-24 10:51:43 +0000324static int vnic_dev_cmd_proxy_by_bdf(struct vnic_dev *vdev,
325 enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait)
326{
327 u32 status;
328 int err;
329
330 memset(vdev->args, 0, sizeof(vdev->args));
331
332 vdev->args[0] = vdev->proxy_index; /* bdf */
333 vdev->args[1] = cmd;
334 vdev->args[2] = *a0;
335 vdev->args[3] = *a1;
336
337 err = _vnic_dev_cmd(vdev, CMD_PROXY_BY_BDF, wait);
338 if (err)
339 return err;
340
341 status = (u32)vdev->args[0];
342 if (status & STAT_ERROR) {
343 err = (int)vdev->args[1];
344 if (err != ERR_ECMDUNKNOWN ||
345 cmd != CMD_CAPABILITY)
346 pr_err("Error %d proxy devcmd %d\n", err, _CMD_N(cmd));
347 return err;
348 }
349
350 *a0 = vdev->args[1];
351 *a1 = vdev->args[2];
352
353 return 0;
354}
355
356static int vnic_dev_cmd_no_proxy(struct vnic_dev *vdev,
357 enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1, int wait)
358{
359 int err;
360
361 vdev->args[0] = *a0;
362 vdev->args[1] = *a1;
363
364 err = _vnic_dev_cmd(vdev, cmd, wait);
365
366 *a0 = vdev->args[0];
367 *a1 = vdev->args[1];
368
369 return err;
370}
371
Vasanthy Kolluri70feadf2010-06-24 10:51:43 +0000372int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
373 u64 *a0, u64 *a1, int wait)
374{
375 memset(vdev->args, 0, sizeof(vdev->args));
376
377 switch (vdev->proxy) {
378 case PROXY_BY_BDF:
379 return vnic_dev_cmd_proxy_by_bdf(vdev, cmd, a0, a1, wait);
380 case PROXY_NONE:
381 default:
382 return vnic_dev_cmd_no_proxy(vdev, cmd, a0, a1, wait);
383 }
384}
385
Hannes Eder5e4232e2008-12-26 00:01:18 -0800386static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
Scott Feldman27372bf2008-11-21 21:28:18 -0800387{
388 u64 a0 = (u32)cmd, a1 = 0;
389 int wait = 1000;
390 int err;
391
392 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
393
394 return !(err || a0);
395}
396
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700397int vnic_dev_fw_info(struct vnic_dev *vdev,
398 struct vnic_devcmd_fw_info **fw_info)
399{
400 u64 a0, a1 = 0;
401 int wait = 1000;
402 int err = 0;
403
404 if (!vdev->fw_info) {
405 vdev->fw_info = pci_alloc_consistent(vdev->pdev,
406 sizeof(struct vnic_devcmd_fw_info),
407 &vdev->fw_info_pa);
408 if (!vdev->fw_info)
409 return -ENOMEM;
410
411 a0 = vdev->fw_info_pa;
412
413 /* only get fw_info once and cache it */
414 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
415 }
416
417 *fw_info = vdev->fw_info;
418
419 return err;
420}
421
422int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
423 void *value)
424{
425 u64 a0, a1;
426 int wait = 1000;
427 int err;
428
429 a0 = offset;
430 a1 = size;
431
432 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
433
434 switch (size) {
435 case 1: *(u8 *)value = (u8)a0; break;
436 case 2: *(u16 *)value = (u16)a0; break;
437 case 4: *(u32 *)value = (u32)a0; break;
438 case 8: *(u64 *)value = a0; break;
439 default: BUG(); break;
440 }
441
442 return err;
443}
444
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700445int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
446{
447 u64 a0, a1;
448 int wait = 1000;
449
450 if (!vdev->stats) {
451 vdev->stats = pci_alloc_consistent(vdev->pdev,
452 sizeof(struct vnic_stats), &vdev->stats_pa);
453 if (!vdev->stats)
454 return -ENOMEM;
455 }
456
457 *stats = vdev->stats;
458 a0 = vdev->stats_pa;
459 a1 = sizeof(struct vnic_stats);
460
461 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
462}
463
464int vnic_dev_close(struct vnic_dev *vdev)
465{
466 u64 a0 = 0, a1 = 0;
467 int wait = 1000;
468 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
469}
470
Vasanthy Kolluri2db77e02010-10-20 10:17:09 +0000471int vnic_dev_enable_wait(struct vnic_dev *vdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700472{
473 u64 a0 = 0, a1 = 0;
474 int wait = 1000;
Vasanthy Kolluri2db77e02010-10-20 10:17:09 +0000475 int err;
476
477 err = vnic_dev_cmd(vdev, CMD_ENABLE_WAIT, &a0, &a1, wait);
478 if (err == ERR_ECMDUNKNOWN)
479 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
480
481 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700482}
483
484int vnic_dev_disable(struct vnic_dev *vdev)
485{
486 u64 a0 = 0, a1 = 0;
487 int wait = 1000;
488 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
489}
490
491int vnic_dev_open(struct vnic_dev *vdev, int arg)
492{
493 u64 a0 = (u32)arg, a1 = 0;
494 int wait = 1000;
495 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
496}
497
498int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
499{
500 u64 a0 = 0, a1 = 0;
501 int wait = 1000;
502 int err;
503
504 *done = 0;
505
506 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
507 if (err)
508 return err;
509
510 *done = (a0 == 0);
511
512 return 0;
513}
514
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +0000515static int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700516{
517 u64 a0 = (u32)arg, a1 = 0;
518 int wait = 1000;
519 return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
520}
521
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +0000522static int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700523{
524 u64 a0 = 0, a1 = 0;
525 int wait = 1000;
526 int err;
527
528 *done = 0;
529
530 err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
531 if (err)
532 return err;
533
534 *done = (a0 == 0);
535
536 return 0;
537}
538
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +0000539int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg)
540{
541 u64 a0 = (u32)arg, a1 = 0;
542 int wait = 1000;
543 int err;
544
545 err = vnic_dev_cmd(vdev, CMD_HANG_RESET, &a0, &a1, wait);
546 if (err == ERR_ECMDUNKNOWN) {
547 err = vnic_dev_soft_reset(vdev, arg);
548 if (err)
549 return err;
550
551 return vnic_dev_init(vdev, 0);
552 }
553
554 return err;
555}
556
557int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done)
558{
559 u64 a0 = 0, a1 = 0;
560 int wait = 1000;
561 int err;
562
563 *done = 0;
564
565 err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS, &a0, &a1, wait);
566 if (err) {
567 if (err == ERR_ECMDUNKNOWN)
568 return vnic_dev_soft_reset_done(vdev, done);
569 return err;
570 }
571
572 *done = (a0 == 0);
573
574 return 0;
575}
576
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700577int vnic_dev_hang_notify(struct vnic_dev *vdev)
578{
579 u64 a0, a1;
580 int wait = 1000;
581 return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
582}
583
584int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
585{
586 u64 a0, a1;
587 int wait = 1000;
588 int err, i;
589
590 for (i = 0; i < ETH_ALEN; i++)
591 mac_addr[i] = 0;
592
593 err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
594 if (err)
595 return err;
596
597 for (i = 0; i < ETH_ALEN; i++)
598 mac_addr[i] = ((u8 *)&a0)[i];
599
600 return 0;
601}
602
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000603int vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700604 int broadcast, int promisc, int allmulti)
605{
606 u64 a0, a1 = 0;
607 int wait = 1000;
608 int err;
609
610 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
611 (multicast ? CMD_PFILTER_MULTICAST : 0) |
612 (broadcast ? CMD_PFILTER_BROADCAST : 0) |
613 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
614 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
615
616 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
617 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000618 pr_err("Can't set packet filter\n");
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000619
620 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700621}
622
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700623int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700624{
625 u64 a0 = 0, a1 = 0;
626 int wait = 1000;
627 int err;
628 int i;
629
630 for (i = 0; i < ETH_ALEN; i++)
631 ((u8 *)&a0)[i] = addr[i];
632
633 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
634 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000635 pr_err("Can't add addr [%pM], %d\n", addr, err);
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700636
637 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700638}
639
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700640int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700641{
642 u64 a0 = 0, a1 = 0;
643 int wait = 1000;
644 int err;
645 int i;
646
647 for (i = 0; i < ETH_ALEN; i++)
648 ((u8 *)&a0)[i] = addr[i];
649
650 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
651 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000652 pr_err("Can't del addr [%pM], %d\n", addr, err);
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700653
654 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700655}
656
Vasanthy Kollurif8cac142010-06-24 10:49:51 +0000657int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
658 u8 ig_vlan_rewrite_mode)
659{
660 u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
661 int wait = 1000;
662 int err;
663
664 err = vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE, &a0, &a1, wait);
665 if (err == ERR_ECMDUNKNOWN)
666 return 0;
667
668 return err;
669}
670
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +0000671static int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000672 void *notify_addr, dma_addr_t notify_pa, u16 intr)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700673{
674 u64 a0, a1;
675 int wait = 1000;
Scott Feldman27372bf2008-11-21 21:28:18 -0800676 int r;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700677
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000678 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
679 vdev->notify = notify_addr;
680 vdev->notify_pa = notify_pa;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700681
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000682 a0 = (u64)notify_pa;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700683 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
684 a1 += sizeof(struct vnic_devcmd_notify);
685
Scott Feldman27372bf2008-11-21 21:28:18 -0800686 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
687 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
688 return r;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700689}
690
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000691int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
692{
693 void *notify_addr;
694 dma_addr_t notify_pa;
695
696 if (vdev->notify || vdev->notify_pa) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000697 pr_err("notify block %p still allocated", vdev->notify);
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000698 return -EINVAL;
699 }
700
701 notify_addr = pci_alloc_consistent(vdev->pdev,
702 sizeof(struct vnic_devcmd_notify),
703 &notify_pa);
704 if (!notify_addr)
705 return -ENOMEM;
706
707 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
708}
709
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +0000710static int vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700711{
712 u64 a0, a1;
713 int wait = 1000;
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000714 int err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700715
716 a0 = 0; /* paddr = 0 to unset notify buffer */
717 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
718 a1 += sizeof(struct vnic_devcmd_notify);
719
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000720 err = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000721 vdev->notify = NULL;
722 vdev->notify_pa = 0;
Scott Feldman27372bf2008-11-21 21:28:18 -0800723 vdev->notify_sz = 0;
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000724
725 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700726}
727
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000728int vnic_dev_notify_unset(struct vnic_dev *vdev)
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000729{
730 if (vdev->notify) {
731 pci_free_consistent(vdev->pdev,
732 sizeof(struct vnic_devcmd_notify),
733 vdev->notify,
734 vdev->notify_pa);
735 }
736
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000737 return vnic_dev_notify_unsetcmd(vdev);
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000738}
739
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700740static int vnic_dev_notify_ready(struct vnic_dev *vdev)
741{
742 u32 *words;
Scott Feldman27372bf2008-11-21 21:28:18 -0800743 unsigned int nwords = vdev->notify_sz / 4;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700744 unsigned int i;
745 u32 csum;
746
Scott Feldman27372bf2008-11-21 21:28:18 -0800747 if (!vdev->notify || !vdev->notify_sz)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700748 return 0;
749
750 do {
751 csum = 0;
Scott Feldman27372bf2008-11-21 21:28:18 -0800752 memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700753 words = (u32 *)&vdev->notify_copy;
754 for (i = 1; i < nwords; i++)
755 csum += words[i];
756 } while (csum != words[0]);
757
758 return 1;
759}
760
761int vnic_dev_init(struct vnic_dev *vdev, int arg)
762{
763 u64 a0 = (u32)arg, a1 = 0;
764 int wait = 1000;
Scott Feldman4cdc44a2009-02-09 23:25:33 -0800765 int r = 0;
Scott Feldman27372bf2008-11-21 21:28:18 -0800766
Vasanthy Kolluri29046f92010-06-24 10:52:26 +0000767 if (vnic_dev_capable(vdev, CMD_INIT))
Scott Feldman27372bf2008-11-21 21:28:18 -0800768 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
769 else {
770 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
771 if (a0 & CMD_INITF_DEFAULT_MAC) {
Vasanthy Kolluri70feadf2010-06-24 10:51:43 +0000772 /* Emulate these for old CMD_INIT_v1 which
773 * didn't pass a0 so no CMD_INITF_*.
774 */
Scott Feldman27372bf2008-11-21 21:28:18 -0800775 vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
776 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
777 }
Scott Feldman4cdc44a2009-02-09 23:25:33 -0800778 }
779 return r;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700780}
781
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700782int vnic_dev_init_done(struct vnic_dev *vdev, int *done, int *err)
783{
784 u64 a0 = 0, a1 = 0;
785 int wait = 1000;
786 int ret;
787
788 *done = 0;
789
790 ret = vnic_dev_cmd(vdev, CMD_INIT_STATUS, &a0, &a1, wait);
791 if (ret)
792 return ret;
793
794 *done = (a0 == 0);
795
Vasanthy Kolluri70feadf2010-06-24 10:51:43 +0000796 *err = (a0 == 0) ? (int)a1:0;
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700797
798 return 0;
799}
800
801int vnic_dev_init_prov(struct vnic_dev *vdev, u8 *buf, u32 len)
802{
803 u64 a0, a1 = len;
804 int wait = 1000;
Randy Dunlapd49aba82010-06-08 07:00:20 +0000805 dma_addr_t prov_pa;
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700806 void *prov_buf;
807 int ret;
808
809 prov_buf = pci_alloc_consistent(vdev->pdev, len, &prov_pa);
810 if (!prov_buf)
811 return -ENOMEM;
812
813 memcpy(prov_buf, buf, len);
814
815 a0 = prov_pa;
816
817 ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO, &a0, &a1, wait);
818
819 pci_free_consistent(vdev->pdev, len, prov_buf, prov_pa);
820
821 return ret;
822}
823
824int vnic_dev_deinit(struct vnic_dev *vdev)
825{
826 u64 a0 = 0, a1 = 0;
827 int wait = 1000;
828
829 return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
830}
831
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700832int vnic_dev_link_status(struct vnic_dev *vdev)
833{
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700834 if (!vnic_dev_notify_ready(vdev))
835 return 0;
836
837 return vdev->notify_copy.link_state;
838}
839
840u32 vnic_dev_port_speed(struct vnic_dev *vdev)
841{
842 if (!vnic_dev_notify_ready(vdev))
843 return 0;
844
845 return vdev->notify_copy.port_speed;
846}
847
848u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
849{
850 if (!vnic_dev_notify_ready(vdev))
851 return 0;
852
853 return vdev->notify_copy.msglvl;
854}
855
856u32 vnic_dev_mtu(struct vnic_dev *vdev)
857{
858 if (!vnic_dev_notify_ready(vdev))
859 return 0;
860
861 return vdev->notify_copy.mtu;
862}
863
864void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
865 enum vnic_dev_intr_mode intr_mode)
866{
867 vdev->intr_mode = intr_mode;
868}
869
870enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
871 struct vnic_dev *vdev)
872{
873 return vdev->intr_mode;
874}
875
876void vnic_dev_unregister(struct vnic_dev *vdev)
877{
878 if (vdev) {
879 if (vdev->notify)
880 pci_free_consistent(vdev->pdev,
881 sizeof(struct vnic_devcmd_notify),
882 vdev->notify,
883 vdev->notify_pa);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700884 if (vdev->stats)
885 pci_free_consistent(vdev->pdev,
Vasanthy Kolluri29046f92010-06-24 10:52:26 +0000886 sizeof(struct vnic_stats),
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700887 vdev->stats, vdev->stats_pa);
888 if (vdev->fw_info)
889 pci_free_consistent(vdev->pdev,
890 sizeof(struct vnic_devcmd_fw_info),
891 vdev->fw_info, vdev->fw_info_pa);
892 kfree(vdev);
893 }
894}
895
896struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000897 void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar,
898 unsigned int num_bars)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700899{
900 if (!vdev) {
901 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
902 if (!vdev)
903 return NULL;
904 }
905
906 vdev->priv = priv;
907 vdev->pdev = pdev;
908
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000909 if (vnic_dev_discover_res(vdev, bar, num_bars))
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700910 goto err_out;
911
912 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
913 if (!vdev->devcmd)
914 goto err_out;
915
916 return vdev;
917
918err_out:
919 vnic_dev_unregister(vdev);
920 return NULL;
921}
922
Scott Feldman27372bf2008-11-21 21:28:18 -0800923