2 * Copyright 2008 Cisco Systems, Inc. All rights reserved.
3 * Copyright 2007 Nuova Systems, Inc. All rights reserved.
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.
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
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>
27 #include "vnic_resource.h"
28 #include "vnic_devcmd.h"
30 #include "vnic_stats.h"
37 #define VNIC_DEV_CAP_INIT 0x0001
38 #define VNIC_DEV_CAP_PERBI 0x0002
43 struct vnic_res res
[RES_TYPE_MAX
];
44 enum vnic_dev_intr_mode intr_mode
;
45 struct vnic_devcmd __iomem
*devcmd
;
46 struct vnic_devcmd_notify
*notify
;
47 struct vnic_devcmd_notify notify_copy
;
51 dma_addr_t linkstatus_pa
;
52 struct vnic_stats
*stats
;
54 struct vnic_devcmd_fw_info
*fw_info
;
55 dma_addr_t fw_info_pa
;
59 #define VNIC_MAX_RES_HDR_SIZE \
60 (sizeof(struct vnic_resource_header) + \
61 sizeof(struct vnic_resource) * RES_TYPE_MAX)
62 #define VNIC_RES_STRIDE 128
64 void *vnic_dev_priv(struct vnic_dev
*vdev
)
69 static int vnic_dev_discover_res(struct vnic_dev
*vdev
,
70 struct vnic_dev_bar
*bar
)
72 struct vnic_resource_header __iomem
*rh
;
73 struct vnic_resource __iomem
*r
;
76 if (bar
->len
< VNIC_MAX_RES_HDR_SIZE
) {
77 printk(KERN_ERR
"vNIC BAR0 res hdr length error\n");
83 printk(KERN_ERR
"vNIC BAR0 res hdr not mem-mapped\n");
87 if (ioread32(&rh
->magic
) != VNIC_RES_MAGIC
||
88 ioread32(&rh
->version
) != VNIC_RES_VERSION
) {
89 printk(KERN_ERR
"vNIC BAR0 res magic/version error "
90 "exp (%lx/%lx) curr (%x/%x)\n",
91 VNIC_RES_MAGIC
, VNIC_RES_VERSION
,
92 ioread32(&rh
->magic
), ioread32(&rh
->version
));
96 r
= (struct vnic_resource __iomem
*)(rh
+ 1);
98 while ((type
= ioread8(&r
->type
)) != RES_TYPE_EOL
) {
100 u8 bar_num
= ioread8(&r
->bar
);
101 u32 bar_offset
= ioread32(&r
->bar_offset
);
102 u32 count
= ioread32(&r
->count
);
107 if (bar_num
!= 0) /* only mapping in BAR0 resources */
114 case RES_TYPE_INTR_CTRL
:
115 /* each count is stride bytes long */
116 len
= count
* VNIC_RES_STRIDE
;
117 if (len
+ bar_offset
> bar
->len
) {
118 printk(KERN_ERR
"vNIC BAR0 resource %d "
119 "out-of-bounds, offset 0x%x + "
120 "size 0x%x > bar len 0x%lx\n",
127 case RES_TYPE_INTR_PBA_LEGACY
:
128 case RES_TYPE_DEVCMD
:
135 vdev
->res
[type
].count
= count
;
136 vdev
->res
[type
].vaddr
= (char __iomem
*)bar
->vaddr
+ bar_offset
;
142 unsigned int vnic_dev_get_res_count(struct vnic_dev
*vdev
,
143 enum vnic_res_type type
)
145 return vdev
->res
[type
].count
;
148 void __iomem
*vnic_dev_get_res(struct vnic_dev
*vdev
, enum vnic_res_type type
,
151 if (!vdev
->res
[type
].vaddr
)
158 case RES_TYPE_INTR_CTRL
:
159 return (char __iomem
*)vdev
->res
[type
].vaddr
+
160 index
* VNIC_RES_STRIDE
;
162 return (char __iomem
*)vdev
->res
[type
].vaddr
;
166 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring
*ring
,
167 unsigned int desc_count
, unsigned int desc_size
)
169 /* The base address of the desc rings must be 512 byte aligned.
170 * Descriptor count is aligned to groups of 32 descriptors. A
171 * count of 0 means the maximum 4096 descriptors. Descriptor
172 * size is aligned to 16 bytes.
175 unsigned int count_align
= 32;
176 unsigned int desc_align
= 16;
178 ring
->base_align
= 512;
183 ring
->desc_count
= ALIGN(desc_count
, count_align
);
185 ring
->desc_size
= ALIGN(desc_size
, desc_align
);
187 ring
->size
= ring
->desc_count
* ring
->desc_size
;
188 ring
->size_unaligned
= ring
->size
+ ring
->base_align
;
190 return ring
->size_unaligned
;
193 void vnic_dev_clear_desc_ring(struct vnic_dev_ring
*ring
)
195 memset(ring
->descs
, 0, ring
->size
);
198 int vnic_dev_alloc_desc_ring(struct vnic_dev
*vdev
, struct vnic_dev_ring
*ring
,
199 unsigned int desc_count
, unsigned int desc_size
)
201 vnic_dev_desc_ring_size(ring
, desc_count
, desc_size
);
203 ring
->descs_unaligned
= pci_alloc_consistent(vdev
->pdev
,
204 ring
->size_unaligned
,
205 &ring
->base_addr_unaligned
);
207 if (!ring
->descs_unaligned
) {
209 "Failed to allocate ring (size=%d), aborting\n",
214 ring
->base_addr
= ALIGN(ring
->base_addr_unaligned
,
216 ring
->descs
= (u8
*)ring
->descs_unaligned
+
217 (ring
->base_addr
- ring
->base_addr_unaligned
);
219 vnic_dev_clear_desc_ring(ring
);
221 ring
->desc_avail
= ring
->desc_count
- 1;
226 void vnic_dev_free_desc_ring(struct vnic_dev
*vdev
, struct vnic_dev_ring
*ring
)
229 pci_free_consistent(vdev
->pdev
,
230 ring
->size_unaligned
,
231 ring
->descs_unaligned
,
232 ring
->base_addr_unaligned
);
237 int vnic_dev_cmd(struct vnic_dev
*vdev
, enum vnic_devcmd_cmd cmd
,
238 u64
*a0
, u64
*a1
, int wait
)
240 struct vnic_devcmd __iomem
*devcmd
= vdev
->devcmd
;
245 status
= ioread32(&devcmd
->status
);
246 if (status
& STAT_BUSY
) {
247 printk(KERN_ERR
"Busy devcmd %d\n", _CMD_N(cmd
));
251 if (_CMD_DIR(cmd
) & _CMD_DIR_WRITE
) {
252 writeq(*a0
, &devcmd
->args
[0]);
253 writeq(*a1
, &devcmd
->args
[1]);
257 iowrite32(cmd
, &devcmd
->cmd
);
259 if ((_CMD_FLAGS(cmd
) & _CMD_FLAGS_NOWAIT
))
262 for (delay
= 0; delay
< wait
; delay
++) {
266 status
= ioread32(&devcmd
->status
);
267 if (!(status
& STAT_BUSY
)) {
269 if (status
& STAT_ERROR
) {
270 err
= (int)readq(&devcmd
->args
[0]);
271 if (err
!= ERR_ECMDUNKNOWN
||
272 cmd
!= CMD_CAPABILITY
)
273 printk(KERN_ERR
"Error %d devcmd %d\n",
278 if (_CMD_DIR(cmd
) & _CMD_DIR_READ
) {
280 *a0
= readq(&devcmd
->args
[0]);
281 *a1
= readq(&devcmd
->args
[1]);
288 printk(KERN_ERR
"Timedout devcmd %d\n", _CMD_N(cmd
));
292 static int vnic_dev_capable(struct vnic_dev
*vdev
, enum vnic_devcmd_cmd cmd
)
294 u64 a0
= (u32
)cmd
, a1
= 0;
298 err
= vnic_dev_cmd(vdev
, CMD_CAPABILITY
, &a0
, &a1
, wait
);
303 int vnic_dev_fw_info(struct vnic_dev
*vdev
,
304 struct vnic_devcmd_fw_info
**fw_info
)
310 if (!vdev
->fw_info
) {
311 vdev
->fw_info
= pci_alloc_consistent(vdev
->pdev
,
312 sizeof(struct vnic_devcmd_fw_info
),
317 a0
= vdev
->fw_info_pa
;
319 /* only get fw_info once and cache it */
320 err
= vnic_dev_cmd(vdev
, CMD_MCPU_FW_INFO
, &a0
, &a1
, wait
);
323 *fw_info
= vdev
->fw_info
;
328 int vnic_dev_spec(struct vnic_dev
*vdev
, unsigned int offset
, unsigned int size
,
338 err
= vnic_dev_cmd(vdev
, CMD_DEV_SPEC
, &a0
, &a1
, wait
);
341 case 1: *(u8
*)value
= (u8
)a0
; break;
342 case 2: *(u16
*)value
= (u16
)a0
; break;
343 case 4: *(u32
*)value
= (u32
)a0
; break;
344 case 8: *(u64
*)value
= a0
; break;
345 default: BUG(); break;
351 int vnic_dev_stats_clear(struct vnic_dev
*vdev
)
355 return vnic_dev_cmd(vdev
, CMD_STATS_CLEAR
, &a0
, &a1
, wait
);
358 int vnic_dev_stats_dump(struct vnic_dev
*vdev
, struct vnic_stats
**stats
)
364 vdev
->stats
= pci_alloc_consistent(vdev
->pdev
,
365 sizeof(struct vnic_stats
), &vdev
->stats_pa
);
370 *stats
= vdev
->stats
;
372 a1
= sizeof(struct vnic_stats
);
374 return vnic_dev_cmd(vdev
, CMD_STATS_DUMP
, &a0
, &a1
, wait
);
377 int vnic_dev_close(struct vnic_dev
*vdev
)
381 return vnic_dev_cmd(vdev
, CMD_CLOSE
, &a0
, &a1
, wait
);
384 int vnic_dev_enable(struct vnic_dev
*vdev
)
388 return vnic_dev_cmd(vdev
, CMD_ENABLE
, &a0
, &a1
, wait
);
391 int vnic_dev_disable(struct vnic_dev
*vdev
)
395 return vnic_dev_cmd(vdev
, CMD_DISABLE
, &a0
, &a1
, wait
);
398 int vnic_dev_open(struct vnic_dev
*vdev
, int arg
)
400 u64 a0
= (u32
)arg
, a1
= 0;
402 return vnic_dev_cmd(vdev
, CMD_OPEN
, &a0
, &a1
, wait
);
405 int vnic_dev_open_done(struct vnic_dev
*vdev
, int *done
)
413 err
= vnic_dev_cmd(vdev
, CMD_OPEN_STATUS
, &a0
, &a1
, wait
);
422 int vnic_dev_soft_reset(struct vnic_dev
*vdev
, int arg
)
424 u64 a0
= (u32
)arg
, a1
= 0;
426 return vnic_dev_cmd(vdev
, CMD_SOFT_RESET
, &a0
, &a1
, wait
);
429 int vnic_dev_soft_reset_done(struct vnic_dev
*vdev
, int *done
)
437 err
= vnic_dev_cmd(vdev
, CMD_SOFT_RESET_STATUS
, &a0
, &a1
, wait
);
446 int vnic_dev_hang_notify(struct vnic_dev
*vdev
)
450 return vnic_dev_cmd(vdev
, CMD_HANG_NOTIFY
, &a0
, &a1
, wait
);
453 int vnic_dev_mac_addr(struct vnic_dev
*vdev
, u8
*mac_addr
)
459 for (i
= 0; i
< ETH_ALEN
; i
++)
462 err
= vnic_dev_cmd(vdev
, CMD_MAC_ADDR
, &a0
, &a1
, wait
);
466 for (i
= 0; i
< ETH_ALEN
; i
++)
467 mac_addr
[i
] = ((u8
*)&a0
)[i
];
472 void vnic_dev_packet_filter(struct vnic_dev
*vdev
, int directed
, int multicast
,
473 int broadcast
, int promisc
, int allmulti
)
479 a0
= (directed
? CMD_PFILTER_DIRECTED
: 0) |
480 (multicast
? CMD_PFILTER_MULTICAST
: 0) |
481 (broadcast
? CMD_PFILTER_BROADCAST
: 0) |
482 (promisc
? CMD_PFILTER_PROMISCUOUS
: 0) |
483 (allmulti
? CMD_PFILTER_ALL_MULTICAST
: 0);
485 err
= vnic_dev_cmd(vdev
, CMD_PACKET_FILTER
, &a0
, &a1
, wait
);
487 printk(KERN_ERR
"Can't set packet filter\n");
490 void vnic_dev_add_addr(struct vnic_dev
*vdev
, u8
*addr
)
497 for (i
= 0; i
< ETH_ALEN
; i
++)
498 ((u8
*)&a0
)[i
] = addr
[i
];
500 err
= vnic_dev_cmd(vdev
, CMD_ADDR_ADD
, &a0
, &a1
, wait
);
502 printk(KERN_ERR
"Can't add addr [%pM], %d\n", addr
, err
);
505 void vnic_dev_del_addr(struct vnic_dev
*vdev
, u8
*addr
)
512 for (i
= 0; i
< ETH_ALEN
; i
++)
513 ((u8
*)&a0
)[i
] = addr
[i
];
515 err
= vnic_dev_cmd(vdev
, CMD_ADDR_DEL
, &a0
, &a1
, wait
);
517 printk(KERN_ERR
"Can't del addr [%pM], %d\n", addr
, err
);
520 int vnic_dev_notify_set(struct vnic_dev
*vdev
, u16 intr
)
527 vdev
->notify
= pci_alloc_consistent(vdev
->pdev
,
528 sizeof(struct vnic_devcmd_notify
),
532 memset(vdev
->notify
, 0, sizeof(struct vnic_devcmd_notify
));
535 a0
= vdev
->notify_pa
;
536 a1
= ((u64
)intr
<< 32) & 0x0000ffff00000000ULL
;
537 a1
+= sizeof(struct vnic_devcmd_notify
);
539 r
= vnic_dev_cmd(vdev
, CMD_NOTIFY
, &a0
, &a1
, wait
);
540 vdev
->notify_sz
= (r
== 0) ? (u32
)a1
: 0;
544 void vnic_dev_notify_unset(struct vnic_dev
*vdev
)
549 a0
= 0; /* paddr = 0 to unset notify buffer */
550 a1
= 0x0000ffff00000000ULL
; /* intr num = -1 to unreg for intr */
551 a1
+= sizeof(struct vnic_devcmd_notify
);
553 vnic_dev_cmd(vdev
, CMD_NOTIFY
, &a0
, &a1
, wait
);
557 static int vnic_dev_notify_ready(struct vnic_dev
*vdev
)
560 unsigned int nwords
= vdev
->notify_sz
/ 4;
564 if (!vdev
->notify
|| !vdev
->notify_sz
)
569 memcpy(&vdev
->notify_copy
, vdev
->notify
, vdev
->notify_sz
);
570 words
= (u32
*)&vdev
->notify_copy
;
571 for (i
= 1; i
< nwords
; i
++)
573 } while (csum
!= words
[0]);
578 int vnic_dev_init(struct vnic_dev
*vdev
, int arg
)
580 u64 a0
= (u32
)arg
, a1
= 0;
584 if (vdev
->cap_flags
& VNIC_DEV_CAP_INIT
)
585 r
= vnic_dev_cmd(vdev
, CMD_INIT
, &a0
, &a1
, wait
);
587 vnic_dev_cmd(vdev
, CMD_INIT_v1
, &a0
, &a1
, wait
);
588 if (a0
& CMD_INITF_DEFAULT_MAC
) {
589 // Emulate these for old CMD_INIT_v1 which
590 // didn't pass a0 so no CMD_INITF_*.
591 vnic_dev_cmd(vdev
, CMD_MAC_ADDR
, &a0
, &a1
, wait
);
592 vnic_dev_cmd(vdev
, CMD_ADDR_ADD
, &a0
, &a1
, wait
);
598 int vnic_dev_link_status(struct vnic_dev
*vdev
)
600 if (vdev
->linkstatus
)
601 return *vdev
->linkstatus
;
603 if (!vnic_dev_notify_ready(vdev
))
606 return vdev
->notify_copy
.link_state
;
609 u32
vnic_dev_port_speed(struct vnic_dev
*vdev
)
611 if (!vnic_dev_notify_ready(vdev
))
614 return vdev
->notify_copy
.port_speed
;
617 u32
vnic_dev_msg_lvl(struct vnic_dev
*vdev
)
619 if (!vnic_dev_notify_ready(vdev
))
622 return vdev
->notify_copy
.msglvl
;
625 u32
vnic_dev_mtu(struct vnic_dev
*vdev
)
627 if (!vnic_dev_notify_ready(vdev
))
630 return vdev
->notify_copy
.mtu
;
633 u32
vnic_dev_link_down_cnt(struct vnic_dev
*vdev
)
635 if (!vnic_dev_notify_ready(vdev
))
638 return vdev
->notify_copy
.link_down_cnt
;
641 u32
vnic_dev_notify_status(struct vnic_dev
*vdev
)
643 if (!vnic_dev_notify_ready(vdev
))
646 return vdev
->notify_copy
.status
;
649 void vnic_dev_set_intr_mode(struct vnic_dev
*vdev
,
650 enum vnic_dev_intr_mode intr_mode
)
652 vdev
->intr_mode
= intr_mode
;
655 enum vnic_dev_intr_mode
vnic_dev_get_intr_mode(
656 struct vnic_dev
*vdev
)
658 return vdev
->intr_mode
;
661 void vnic_dev_unregister(struct vnic_dev
*vdev
)
665 pci_free_consistent(vdev
->pdev
,
666 sizeof(struct vnic_devcmd_notify
),
669 if (vdev
->linkstatus
)
670 pci_free_consistent(vdev
->pdev
,
673 vdev
->linkstatus_pa
);
675 pci_free_consistent(vdev
->pdev
,
676 sizeof(struct vnic_dev
),
677 vdev
->stats
, vdev
->stats_pa
);
679 pci_free_consistent(vdev
->pdev
,
680 sizeof(struct vnic_devcmd_fw_info
),
681 vdev
->fw_info
, vdev
->fw_info_pa
);
686 struct vnic_dev
*vnic_dev_register(struct vnic_dev
*vdev
,
687 void *priv
, struct pci_dev
*pdev
, struct vnic_dev_bar
*bar
)
690 vdev
= kzalloc(sizeof(struct vnic_dev
), GFP_ATOMIC
);
698 if (vnic_dev_discover_res(vdev
, bar
))
701 vdev
->devcmd
= vnic_dev_get_res(vdev
, RES_TYPE_DEVCMD
, 0);
707 if (vnic_dev_capable(vdev
, CMD_INIT
))
708 vdev
->cap_flags
|= VNIC_DEV_CAP_INIT
;
713 vnic_dev_unregister(vdev
);