blob: c93012f2faa9ed1c5e8dd75a1542109e65b0d08a [file] [log] [blame]
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001/*
2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
4 *
5 * This program is free software; you may redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16 * SOFTWARE.
17 *
18 */
19
20#include <linux/kernel.h>
21#include <linux/errno.h>
22#include <linux/types.h>
23#include <linux/pci.h>
24#include <linux/delay.h>
25#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Scott Feldman01f2e4e2008-09-15 09:17:11 -070027
28#include "vnic_resource.h"
29#include "vnic_devcmd.h"
30#include "vnic_dev.h"
31#include "vnic_stats.h"
32
33struct vnic_res {
34 void __iomem *vaddr;
Scott Feldman27e6c7d2009-09-03 17:01:53 +000035 dma_addr_t bus_addr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -070036 unsigned int count;
37};
38
Scott Feldman4cdc44a2009-02-09 23:25:33 -080039#define VNIC_DEV_CAP_INIT 0x0001
Scott Feldman4cdc44a2009-02-09 23:25:33 -080040
Scott Feldman01f2e4e2008-09-15 09:17:11 -070041struct vnic_dev {
42 void *priv;
43 struct pci_dev *pdev;
44 struct vnic_res res[RES_TYPE_MAX];
45 enum vnic_dev_intr_mode intr_mode;
46 struct vnic_devcmd __iomem *devcmd;
47 struct vnic_devcmd_notify *notify;
48 struct vnic_devcmd_notify notify_copy;
49 dma_addr_t notify_pa;
Scott Feldman27372bf2008-11-21 21:28:18 -080050 u32 notify_sz;
Scott Feldman01f2e4e2008-09-15 09:17:11 -070051 u32 *linkstatus;
52 dma_addr_t linkstatus_pa;
53 struct vnic_stats *stats;
54 dma_addr_t stats_pa;
55 struct vnic_devcmd_fw_info *fw_info;
56 dma_addr_t fw_info_pa;
Scott Feldman4cdc44a2009-02-09 23:25:33 -080057 u32 cap_flags;
Scott Feldman01f2e4e2008-09-15 09:17:11 -070058};
59
60#define VNIC_MAX_RES_HDR_SIZE \
61 (sizeof(struct vnic_resource_header) + \
62 sizeof(struct vnic_resource) * RES_TYPE_MAX)
63#define VNIC_RES_STRIDE 128
64
65void *vnic_dev_priv(struct vnic_dev *vdev)
66{
67 return vdev->priv;
68}
69
70static int vnic_dev_discover_res(struct vnic_dev *vdev,
Scott Feldman27e6c7d2009-09-03 17:01:53 +000071 struct vnic_dev_bar *bar, unsigned int num_bars)
Scott Feldman01f2e4e2008-09-15 09:17:11 -070072{
73 struct vnic_resource_header __iomem *rh;
74 struct vnic_resource __iomem *r;
75 u8 type;
76
Scott Feldman27e6c7d2009-09-03 17:01:53 +000077 if (num_bars == 0)
78 return -EINVAL;
79
Scott Feldman01f2e4e2008-09-15 09:17:11 -070080 if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
81 printk(KERN_ERR "vNIC BAR0 res hdr length error\n");
82 return -EINVAL;
83 }
84
85 rh = bar->vaddr;
86 if (!rh) {
87 printk(KERN_ERR "vNIC BAR0 res hdr not mem-mapped\n");
88 return -EINVAL;
89 }
90
91 if (ioread32(&rh->magic) != VNIC_RES_MAGIC ||
92 ioread32(&rh->version) != VNIC_RES_VERSION) {
93 printk(KERN_ERR "vNIC BAR0 res magic/version error "
94 "exp (%lx/%lx) curr (%x/%x)\n",
95 VNIC_RES_MAGIC, VNIC_RES_VERSION,
96 ioread32(&rh->magic), ioread32(&rh->version));
97 return -EINVAL;
98 }
99
100 r = (struct vnic_resource __iomem *)(rh + 1);
101
102 while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
103
104 u8 bar_num = ioread8(&r->bar);
105 u32 bar_offset = ioread32(&r->bar_offset);
106 u32 count = ioread32(&r->count);
107 u32 len;
108
109 r++;
110
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000111 if (bar_num >= num_bars)
112 continue;
113
114 if (!bar[bar_num].len || !bar[bar_num].vaddr)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700115 continue;
116
117 switch (type) {
118 case RES_TYPE_WQ:
119 case RES_TYPE_RQ:
120 case RES_TYPE_CQ:
121 case RES_TYPE_INTR_CTRL:
122 /* each count is stride bytes long */
123 len = count * VNIC_RES_STRIDE;
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000124 if (len + bar_offset > bar[bar_num].len) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700125 printk(KERN_ERR "vNIC BAR0 resource %d "
126 "out-of-bounds, offset 0x%x + "
127 "size 0x%x > bar len 0x%lx\n",
128 type, bar_offset,
129 len,
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000130 bar[bar_num].len);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700131 return -EINVAL;
132 }
133 break;
134 case RES_TYPE_INTR_PBA_LEGACY:
135 case RES_TYPE_DEVCMD:
136 len = count;
137 break;
138 default:
139 continue;
140 }
141
142 vdev->res[type].count = count;
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000143 vdev->res[type].vaddr = (char __iomem *)bar[bar_num].vaddr +
144 bar_offset;
145 vdev->res[type].bus_addr = bar[bar_num].bus_addr + bar_offset;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700146 }
147
148 return 0;
149}
150
151unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
152 enum vnic_res_type type)
153{
154 return vdev->res[type].count;
155}
156
157void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
158 unsigned int index)
159{
160 if (!vdev->res[type].vaddr)
161 return NULL;
162
163 switch (type) {
164 case RES_TYPE_WQ:
165 case RES_TYPE_RQ:
166 case RES_TYPE_CQ:
167 case RES_TYPE_INTR_CTRL:
168 return (char __iomem *)vdev->res[type].vaddr +
169 index * VNIC_RES_STRIDE;
170 default:
171 return (char __iomem *)vdev->res[type].vaddr;
172 }
173}
174
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000175dma_addr_t vnic_dev_get_res_bus_addr(struct vnic_dev *vdev,
176 enum vnic_res_type type, unsigned int index)
177{
178 switch (type) {
179 case RES_TYPE_WQ:
180 case RES_TYPE_RQ:
181 case RES_TYPE_CQ:
182 case RES_TYPE_INTR_CTRL:
183 return vdev->res[type].bus_addr +
184 index * VNIC_RES_STRIDE;
185 default:
186 return vdev->res[type].bus_addr;
187 }
188}
189
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700190unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
191 unsigned int desc_count, unsigned int desc_size)
192{
193 /* The base address of the desc rings must be 512 byte aligned.
194 * Descriptor count is aligned to groups of 32 descriptors. A
195 * count of 0 means the maximum 4096 descriptors. Descriptor
196 * size is aligned to 16 bytes.
197 */
198
199 unsigned int count_align = 32;
200 unsigned int desc_align = 16;
201
202 ring->base_align = 512;
203
204 if (desc_count == 0)
205 desc_count = 4096;
206
207 ring->desc_count = ALIGN(desc_count, count_align);
208
209 ring->desc_size = ALIGN(desc_size, desc_align);
210
211 ring->size = ring->desc_count * ring->desc_size;
212 ring->size_unaligned = ring->size + ring->base_align;
213
214 return ring->size_unaligned;
215}
216
217void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
218{
219 memset(ring->descs, 0, ring->size);
220}
221
222int vnic_dev_alloc_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring,
223 unsigned int desc_count, unsigned int desc_size)
224{
225 vnic_dev_desc_ring_size(ring, desc_count, desc_size);
226
227 ring->descs_unaligned = pci_alloc_consistent(vdev->pdev,
228 ring->size_unaligned,
229 &ring->base_addr_unaligned);
230
231 if (!ring->descs_unaligned) {
232 printk(KERN_ERR
233 "Failed to allocate ring (size=%d), aborting\n",
234 (int)ring->size);
235 return -ENOMEM;
236 }
237
238 ring->base_addr = ALIGN(ring->base_addr_unaligned,
239 ring->base_align);
240 ring->descs = (u8 *)ring->descs_unaligned +
241 (ring->base_addr - ring->base_addr_unaligned);
242
243 vnic_dev_clear_desc_ring(ring);
244
245 ring->desc_avail = ring->desc_count - 1;
246
247 return 0;
248}
249
250void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
251{
252 if (ring->descs) {
253 pci_free_consistent(vdev->pdev,
254 ring->size_unaligned,
255 ring->descs_unaligned,
256 ring->base_addr_unaligned);
257 ring->descs = NULL;
258 }
259}
260
261int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
262 u64 *a0, u64 *a1, int wait)
263{
264 struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
265 int delay;
266 u32 status;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700267 int err;
268
269 status = ioread32(&devcmd->status);
270 if (status & STAT_BUSY) {
271 printk(KERN_ERR "Busy devcmd %d\n", _CMD_N(cmd));
272 return -EBUSY;
273 }
274
275 if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
276 writeq(*a0, &devcmd->args[0]);
277 writeq(*a1, &devcmd->args[1]);
278 wmb();
279 }
280
281 iowrite32(cmd, &devcmd->cmd);
282
283 if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000284 return 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700285
286 for (delay = 0; delay < wait; delay++) {
287
288 udelay(100);
289
290 status = ioread32(&devcmd->status);
291 if (!(status & STAT_BUSY)) {
292
293 if (status & STAT_ERROR) {
Scott Feldman27372bf2008-11-21 21:28:18 -0800294 err = (int)readq(&devcmd->args[0]);
295 if (err != ERR_ECMDUNKNOWN ||
296 cmd != CMD_CAPABILITY)
297 printk(KERN_ERR "Error %d devcmd %d\n",
298 err, _CMD_N(cmd));
299 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700300 }
301
302 if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
303 rmb();
304 *a0 = readq(&devcmd->args[0]);
305 *a1 = readq(&devcmd->args[1]);
306 }
307
308 return 0;
309 }
310 }
311
312 printk(KERN_ERR "Timedout devcmd %d\n", _CMD_N(cmd));
313 return -ETIMEDOUT;
314}
315
Hannes Eder5e4232e2008-12-26 00:01:18 -0800316static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
Scott Feldman27372bf2008-11-21 21:28:18 -0800317{
318 u64 a0 = (u32)cmd, a1 = 0;
319 int wait = 1000;
320 int err;
321
322 err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
323
324 return !(err || a0);
325}
326
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700327int vnic_dev_fw_info(struct vnic_dev *vdev,
328 struct vnic_devcmd_fw_info **fw_info)
329{
330 u64 a0, a1 = 0;
331 int wait = 1000;
332 int err = 0;
333
334 if (!vdev->fw_info) {
335 vdev->fw_info = pci_alloc_consistent(vdev->pdev,
336 sizeof(struct vnic_devcmd_fw_info),
337 &vdev->fw_info_pa);
338 if (!vdev->fw_info)
339 return -ENOMEM;
340
341 a0 = vdev->fw_info_pa;
342
343 /* only get fw_info once and cache it */
344 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
345 }
346
347 *fw_info = vdev->fw_info;
348
349 return err;
350}
351
Scott Feldman4badc382009-09-03 17:01:58 +0000352int vnic_dev_hw_version(struct vnic_dev *vdev, enum vnic_dev_hw_version *hw_ver)
353{
354 struct vnic_devcmd_fw_info *fw_info;
355 int err;
356
357 err = vnic_dev_fw_info(vdev, &fw_info);
358 if (err)
359 return err;
360
361 if (strncmp(fw_info->hw_version, "A1", sizeof("A1")) == 0)
362 *hw_ver = VNIC_DEV_HW_VER_A1;
363 else if (strncmp(fw_info->hw_version, "A2", sizeof("A2")) == 0)
364 *hw_ver = VNIC_DEV_HW_VER_A2;
365 else
366 *hw_ver = VNIC_DEV_HW_VER_UNKNOWN;
367
368 return 0;
369}
370
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700371int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
372 void *value)
373{
374 u64 a0, a1;
375 int wait = 1000;
376 int err;
377
378 a0 = offset;
379 a1 = size;
380
381 err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
382
383 switch (size) {
384 case 1: *(u8 *)value = (u8)a0; break;
385 case 2: *(u16 *)value = (u16)a0; break;
386 case 4: *(u32 *)value = (u32)a0; break;
387 case 8: *(u64 *)value = a0; break;
388 default: BUG(); break;
389 }
390
391 return err;
392}
393
394int vnic_dev_stats_clear(struct vnic_dev *vdev)
395{
396 u64 a0 = 0, a1 = 0;
397 int wait = 1000;
398 return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
399}
400
401int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
402{
403 u64 a0, a1;
404 int wait = 1000;
405
406 if (!vdev->stats) {
407 vdev->stats = pci_alloc_consistent(vdev->pdev,
408 sizeof(struct vnic_stats), &vdev->stats_pa);
409 if (!vdev->stats)
410 return -ENOMEM;
411 }
412
413 *stats = vdev->stats;
414 a0 = vdev->stats_pa;
415 a1 = sizeof(struct vnic_stats);
416
417 return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
418}
419
420int vnic_dev_close(struct vnic_dev *vdev)
421{
422 u64 a0 = 0, a1 = 0;
423 int wait = 1000;
424 return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
425}
426
427int vnic_dev_enable(struct vnic_dev *vdev)
428{
429 u64 a0 = 0, a1 = 0;
430 int wait = 1000;
431 return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
432}
433
434int vnic_dev_disable(struct vnic_dev *vdev)
435{
436 u64 a0 = 0, a1 = 0;
437 int wait = 1000;
438 return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
439}
440
441int vnic_dev_open(struct vnic_dev *vdev, int arg)
442{
443 u64 a0 = (u32)arg, a1 = 0;
444 int wait = 1000;
445 return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
446}
447
448int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
449{
450 u64 a0 = 0, a1 = 0;
451 int wait = 1000;
452 int err;
453
454 *done = 0;
455
456 err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
457 if (err)
458 return err;
459
460 *done = (a0 == 0);
461
462 return 0;
463}
464
465int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
466{
467 u64 a0 = (u32)arg, a1 = 0;
468 int wait = 1000;
469 return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
470}
471
472int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
473{
474 u64 a0 = 0, a1 = 0;
475 int wait = 1000;
476 int err;
477
478 *done = 0;
479
480 err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
481 if (err)
482 return err;
483
484 *done = (a0 == 0);
485
486 return 0;
487}
488
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +0000489int vnic_dev_hang_reset(struct vnic_dev *vdev, int arg)
490{
491 u64 a0 = (u32)arg, a1 = 0;
492 int wait = 1000;
493 int err;
494
495 err = vnic_dev_cmd(vdev, CMD_HANG_RESET, &a0, &a1, wait);
496 if (err == ERR_ECMDUNKNOWN) {
497 err = vnic_dev_soft_reset(vdev, arg);
498 if (err)
499 return err;
500
501 return vnic_dev_init(vdev, 0);
502 }
503
504 return err;
505}
506
507int vnic_dev_hang_reset_done(struct vnic_dev *vdev, int *done)
508{
509 u64 a0 = 0, a1 = 0;
510 int wait = 1000;
511 int err;
512
513 *done = 0;
514
515 err = vnic_dev_cmd(vdev, CMD_HANG_RESET_STATUS, &a0, &a1, wait);
516 if (err) {
517 if (err == ERR_ECMDUNKNOWN)
518 return vnic_dev_soft_reset_done(vdev, done);
519 return err;
520 }
521
522 *done = (a0 == 0);
523
524 return 0;
525}
526
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700527int vnic_dev_hang_notify(struct vnic_dev *vdev)
528{
529 u64 a0, a1;
530 int wait = 1000;
531 return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
532}
533
534int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
535{
536 u64 a0, a1;
537 int wait = 1000;
538 int err, i;
539
540 for (i = 0; i < ETH_ALEN; i++)
541 mac_addr[i] = 0;
542
543 err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
544 if (err)
545 return err;
546
547 for (i = 0; i < ETH_ALEN; i++)
548 mac_addr[i] = ((u8 *)&a0)[i];
549
550 return 0;
551}
552
553void vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
554 int broadcast, int promisc, int allmulti)
555{
556 u64 a0, a1 = 0;
557 int wait = 1000;
558 int err;
559
560 a0 = (directed ? CMD_PFILTER_DIRECTED : 0) |
561 (multicast ? CMD_PFILTER_MULTICAST : 0) |
562 (broadcast ? CMD_PFILTER_BROADCAST : 0) |
563 (promisc ? CMD_PFILTER_PROMISCUOUS : 0) |
564 (allmulti ? CMD_PFILTER_ALL_MULTICAST : 0);
565
566 err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
567 if (err)
568 printk(KERN_ERR "Can't set packet filter\n");
569}
570
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700571int vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700572{
573 u64 a0 = 0, a1 = 0;
574 int wait = 1000;
575 int err;
576 int i;
577
578 for (i = 0; i < ETH_ALEN; i++)
579 ((u8 *)&a0)[i] = addr[i];
580
581 err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
582 if (err)
Johannes Berg7c510e42008-10-27 17:47:26 -0700583 printk(KERN_ERR "Can't add addr [%pM], %d\n", addr, err);
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700584
585 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700586}
587
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700588int vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700589{
590 u64 a0 = 0, a1 = 0;
591 int wait = 1000;
592 int err;
593 int i;
594
595 for (i = 0; i < ETH_ALEN; i++)
596 ((u8 *)&a0)[i] = addr[i];
597
598 err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
599 if (err)
Johannes Berg7c510e42008-10-27 17:47:26 -0700600 printk(KERN_ERR "Can't del addr [%pM], %d\n", addr, err);
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700601
602 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700603}
604
Vasanthy Kollurif8cac142010-06-24 10:49:51 +0000605int vnic_dev_set_ig_vlan_rewrite_mode(struct vnic_dev *vdev,
606 u8 ig_vlan_rewrite_mode)
607{
608 u64 a0 = ig_vlan_rewrite_mode, a1 = 0;
609 int wait = 1000;
610 int err;
611
612 err = vnic_dev_cmd(vdev, CMD_IG_VLAN_REWRITE_MODE, &a0, &a1, wait);
613 if (err == ERR_ECMDUNKNOWN)
614 return 0;
615
616 return err;
617}
618
Scott Feldmand73149f2009-09-03 17:02:35 +0000619int vnic_dev_raise_intr(struct vnic_dev *vdev, u16 intr)
620{
621 u64 a0 = intr, a1 = 0;
622 int wait = 1000;
623 int err;
624
625 err = vnic_dev_cmd(vdev, CMD_IAR, &a0, &a1, wait);
626 if (err)
627 printk(KERN_ERR "Failed to raise INTR[%d], err %d\n",
628 intr, err);
629
630 return err;
631}
632
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000633int vnic_dev_notify_setcmd(struct vnic_dev *vdev,
634 void *notify_addr, dma_addr_t notify_pa, u16 intr)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700635{
636 u64 a0, a1;
637 int wait = 1000;
Scott Feldman27372bf2008-11-21 21:28:18 -0800638 int r;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700639
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000640 memset(notify_addr, 0, sizeof(struct vnic_devcmd_notify));
641 vdev->notify = notify_addr;
642 vdev->notify_pa = notify_pa;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700643
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000644 a0 = (u64)notify_pa;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700645 a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
646 a1 += sizeof(struct vnic_devcmd_notify);
647
Scott Feldman27372bf2008-11-21 21:28:18 -0800648 r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
649 vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
650 return r;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700651}
652
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000653int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
654{
655 void *notify_addr;
656 dma_addr_t notify_pa;
657
658 if (vdev->notify || vdev->notify_pa) {
659 printk(KERN_ERR "notify block %p still allocated",
660 vdev->notify);
661 return -EINVAL;
662 }
663
664 notify_addr = pci_alloc_consistent(vdev->pdev,
665 sizeof(struct vnic_devcmd_notify),
666 &notify_pa);
667 if (!notify_addr)
668 return -ENOMEM;
669
670 return vnic_dev_notify_setcmd(vdev, notify_addr, notify_pa, intr);
671}
672
673void vnic_dev_notify_unsetcmd(struct vnic_dev *vdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700674{
675 u64 a0, a1;
676 int wait = 1000;
677
678 a0 = 0; /* paddr = 0 to unset notify buffer */
679 a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
680 a1 += sizeof(struct vnic_devcmd_notify);
681
682 vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000683 vdev->notify = NULL;
684 vdev->notify_pa = 0;
Scott Feldman27372bf2008-11-21 21:28:18 -0800685 vdev->notify_sz = 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700686}
687
Vasanthy Kollurid883aa72010-03-18 16:19:59 +0000688void vnic_dev_notify_unset(struct vnic_dev *vdev)
689{
690 if (vdev->notify) {
691 pci_free_consistent(vdev->pdev,
692 sizeof(struct vnic_devcmd_notify),
693 vdev->notify,
694 vdev->notify_pa);
695 }
696
697 vnic_dev_notify_unsetcmd(vdev);
698}
699
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700700static int vnic_dev_notify_ready(struct vnic_dev *vdev)
701{
702 u32 *words;
Scott Feldman27372bf2008-11-21 21:28:18 -0800703 unsigned int nwords = vdev->notify_sz / 4;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700704 unsigned int i;
705 u32 csum;
706
Scott Feldman27372bf2008-11-21 21:28:18 -0800707 if (!vdev->notify || !vdev->notify_sz)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700708 return 0;
709
710 do {
711 csum = 0;
Scott Feldman27372bf2008-11-21 21:28:18 -0800712 memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700713 words = (u32 *)&vdev->notify_copy;
714 for (i = 1; i < nwords; i++)
715 csum += words[i];
716 } while (csum != words[0]);
717
718 return 1;
719}
720
721int vnic_dev_init(struct vnic_dev *vdev, int arg)
722{
723 u64 a0 = (u32)arg, a1 = 0;
724 int wait = 1000;
Scott Feldman4cdc44a2009-02-09 23:25:33 -0800725 int r = 0;
Scott Feldman27372bf2008-11-21 21:28:18 -0800726
Scott Feldman4cdc44a2009-02-09 23:25:33 -0800727 if (vdev->cap_flags & VNIC_DEV_CAP_INIT)
Scott Feldman27372bf2008-11-21 21:28:18 -0800728 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
729 else {
730 vnic_dev_cmd(vdev, CMD_INIT_v1, &a0, &a1, wait);
731 if (a0 & CMD_INITF_DEFAULT_MAC) {
732 // Emulate these for old CMD_INIT_v1 which
733 // didn't pass a0 so no CMD_INITF_*.
734 vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
735 vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
736 }
Scott Feldman4cdc44a2009-02-09 23:25:33 -0800737 }
738 return r;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700739}
740
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700741int vnic_dev_init_done(struct vnic_dev *vdev, int *done, int *err)
742{
743 u64 a0 = 0, a1 = 0;
744 int wait = 1000;
745 int ret;
746
747 *done = 0;
748
749 ret = vnic_dev_cmd(vdev, CMD_INIT_STATUS, &a0, &a1, wait);
750 if (ret)
751 return ret;
752
753 *done = (a0 == 0);
754
755 *err = (a0 == 0) ? a1 : 0;
756
757 return 0;
758}
759
760int vnic_dev_init_prov(struct vnic_dev *vdev, u8 *buf, u32 len)
761{
762 u64 a0, a1 = len;
763 int wait = 1000;
Randy Dunlapd49aba82010-06-08 07:00:20 +0000764 dma_addr_t prov_pa;
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700765 void *prov_buf;
766 int ret;
767
768 prov_buf = pci_alloc_consistent(vdev->pdev, len, &prov_pa);
769 if (!prov_buf)
770 return -ENOMEM;
771
772 memcpy(prov_buf, buf, len);
773
774 a0 = prov_pa;
775
776 ret = vnic_dev_cmd(vdev, CMD_INIT_PROV_INFO, &a0, &a1, wait);
777
778 pci_free_consistent(vdev->pdev, len, prov_buf, prov_pa);
779
780 return ret;
781}
782
783int vnic_dev_deinit(struct vnic_dev *vdev)
784{
785 u64 a0 = 0, a1 = 0;
786 int wait = 1000;
787
788 return vnic_dev_cmd(vdev, CMD_DEINIT, &a0, &a1, wait);
789}
790
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700791int vnic_dev_link_status(struct vnic_dev *vdev)
792{
793 if (vdev->linkstatus)
794 return *vdev->linkstatus;
795
796 if (!vnic_dev_notify_ready(vdev))
797 return 0;
798
799 return vdev->notify_copy.link_state;
800}
801
802u32 vnic_dev_port_speed(struct vnic_dev *vdev)
803{
804 if (!vnic_dev_notify_ready(vdev))
805 return 0;
806
807 return vdev->notify_copy.port_speed;
808}
809
810u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
811{
812 if (!vnic_dev_notify_ready(vdev))
813 return 0;
814
815 return vdev->notify_copy.msglvl;
816}
817
818u32 vnic_dev_mtu(struct vnic_dev *vdev)
819{
820 if (!vnic_dev_notify_ready(vdev))
821 return 0;
822
823 return vdev->notify_copy.mtu;
824}
825
Scott Feldman4cdc44a2009-02-09 23:25:33 -0800826u32 vnic_dev_link_down_cnt(struct vnic_dev *vdev)
827{
828 if (!vnic_dev_notify_ready(vdev))
829 return 0;
830
831 return vdev->notify_copy.link_down_cnt;
832}
833
834u32 vnic_dev_notify_status(struct vnic_dev *vdev)
835{
836 if (!vnic_dev_notify_ready(vdev))
837 return 0;
838
839 return vdev->notify_copy.status;
840}
841
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700842void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
843 enum vnic_dev_intr_mode intr_mode)
844{
845 vdev->intr_mode = intr_mode;
846}
847
848enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
849 struct vnic_dev *vdev)
850{
851 return vdev->intr_mode;
852}
853
854void vnic_dev_unregister(struct vnic_dev *vdev)
855{
856 if (vdev) {
857 if (vdev->notify)
858 pci_free_consistent(vdev->pdev,
859 sizeof(struct vnic_devcmd_notify),
860 vdev->notify,
861 vdev->notify_pa);
862 if (vdev->linkstatus)
863 pci_free_consistent(vdev->pdev,
864 sizeof(u32),
865 vdev->linkstatus,
866 vdev->linkstatus_pa);
867 if (vdev->stats)
868 pci_free_consistent(vdev->pdev,
869 sizeof(struct vnic_dev),
870 vdev->stats, vdev->stats_pa);
871 if (vdev->fw_info)
872 pci_free_consistent(vdev->pdev,
873 sizeof(struct vnic_devcmd_fw_info),
874 vdev->fw_info, vdev->fw_info_pa);
875 kfree(vdev);
876 }
877}
878
879struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000880 void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar,
881 unsigned int num_bars)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700882{
883 if (!vdev) {
884 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
885 if (!vdev)
886 return NULL;
887 }
888
889 vdev->priv = priv;
890 vdev->pdev = pdev;
891
Scott Feldman27e6c7d2009-09-03 17:01:53 +0000892 if (vnic_dev_discover_res(vdev, bar, num_bars))
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700893 goto err_out;
894
895 vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
896 if (!vdev->devcmd)
897 goto err_out;
898
Scott Feldman4cdc44a2009-02-09 23:25:33 -0800899 vdev->cap_flags = 0;
900
901 if (vnic_dev_capable(vdev, CMD_INIT))
902 vdev->cap_flags |= VNIC_DEV_CAP_INIT;
903
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700904 return vdev;
905
906err_out:
907 vnic_dev_unregister(vdev);
908 return NULL;
909}
910
Scott Feldman27372bf2008-11-21 21:28:18 -0800911