Merge branch 'linus' into x86/core
[pandora-kernel.git] / drivers / net / enic / vnic_dev.c
1 /*
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>
26
27 #include "vnic_resource.h"
28 #include "vnic_devcmd.h"
29 #include "vnic_dev.h"
30 #include "vnic_stats.h"
31
32 struct vnic_res {
33         void __iomem *vaddr;
34         unsigned int count;
35 };
36
37 #define VNIC_DEV_CAP_INIT       0x0001
38 #define VNIC_DEV_CAP_PERBI      0x0002
39
40 struct vnic_dev {
41         void *priv;
42         struct pci_dev *pdev;
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;
48         dma_addr_t notify_pa;
49         u32 notify_sz;
50         u32 *linkstatus;
51         dma_addr_t linkstatus_pa;
52         struct vnic_stats *stats;
53         dma_addr_t stats_pa;
54         struct vnic_devcmd_fw_info *fw_info;
55         dma_addr_t fw_info_pa;
56         u32 cap_flags;
57 };
58
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
63
64 void *vnic_dev_priv(struct vnic_dev *vdev)
65 {
66         return vdev->priv;
67 }
68
69 static int vnic_dev_discover_res(struct vnic_dev *vdev,
70         struct vnic_dev_bar *bar)
71 {
72         struct vnic_resource_header __iomem *rh;
73         struct vnic_resource __iomem *r;
74         u8 type;
75
76         if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
77                 printk(KERN_ERR "vNIC BAR0 res hdr length error\n");
78                 return -EINVAL;
79         }
80
81         rh = bar->vaddr;
82         if (!rh) {
83                 printk(KERN_ERR "vNIC BAR0 res hdr not mem-mapped\n");
84                 return -EINVAL;
85         }
86
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));
93                 return -EINVAL;
94         }
95
96         r = (struct vnic_resource __iomem *)(rh + 1);
97
98         while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
99
100                 u8 bar_num = ioread8(&r->bar);
101                 u32 bar_offset = ioread32(&r->bar_offset);
102                 u32 count = ioread32(&r->count);
103                 u32 len;
104
105                 r++;
106
107                 if (bar_num != 0)  /* only mapping in BAR0 resources */
108                         continue;
109
110                 switch (type) {
111                 case RES_TYPE_WQ:
112                 case RES_TYPE_RQ:
113                 case RES_TYPE_CQ:
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",
121                                         type, bar_offset,
122                                         len,
123                                         bar->len);
124                                 return -EINVAL;
125                         }
126                         break;
127                 case RES_TYPE_INTR_PBA_LEGACY:
128                 case RES_TYPE_DEVCMD:
129                         len = count;
130                         break;
131                 default:
132                         continue;
133                 }
134
135                 vdev->res[type].count = count;
136                 vdev->res[type].vaddr = (char __iomem *)bar->vaddr + bar_offset;
137         }
138
139         return 0;
140 }
141
142 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
143         enum vnic_res_type type)
144 {
145         return vdev->res[type].count;
146 }
147
148 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
149         unsigned int index)
150 {
151         if (!vdev->res[type].vaddr)
152                 return NULL;
153
154         switch (type) {
155         case RES_TYPE_WQ:
156         case RES_TYPE_RQ:
157         case RES_TYPE_CQ:
158         case RES_TYPE_INTR_CTRL:
159                 return (char __iomem *)vdev->res[type].vaddr +
160                         index * VNIC_RES_STRIDE;
161         default:
162                 return (char __iomem *)vdev->res[type].vaddr;
163         }
164 }
165
166 unsigned int vnic_dev_desc_ring_size(struct vnic_dev_ring *ring,
167         unsigned int desc_count, unsigned int desc_size)
168 {
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.
173          */
174
175         unsigned int count_align = 32;
176         unsigned int desc_align = 16;
177
178         ring->base_align = 512;
179
180         if (desc_count == 0)
181                 desc_count = 4096;
182
183         ring->desc_count = ALIGN(desc_count, count_align);
184
185         ring->desc_size = ALIGN(desc_size, desc_align);
186
187         ring->size = ring->desc_count * ring->desc_size;
188         ring->size_unaligned = ring->size + ring->base_align;
189
190         return ring->size_unaligned;
191 }
192
193 void vnic_dev_clear_desc_ring(struct vnic_dev_ring *ring)
194 {
195         memset(ring->descs, 0, ring->size);
196 }
197
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)
200 {
201         vnic_dev_desc_ring_size(ring, desc_count, desc_size);
202
203         ring->descs_unaligned = pci_alloc_consistent(vdev->pdev,
204                 ring->size_unaligned,
205                 &ring->base_addr_unaligned);
206
207         if (!ring->descs_unaligned) {
208                 printk(KERN_ERR
209                   "Failed to allocate ring (size=%d), aborting\n",
210                         (int)ring->size);
211                 return -ENOMEM;
212         }
213
214         ring->base_addr = ALIGN(ring->base_addr_unaligned,
215                 ring->base_align);
216         ring->descs = (u8 *)ring->descs_unaligned +
217                 (ring->base_addr - ring->base_addr_unaligned);
218
219         vnic_dev_clear_desc_ring(ring);
220
221         ring->desc_avail = ring->desc_count - 1;
222
223         return 0;
224 }
225
226 void vnic_dev_free_desc_ring(struct vnic_dev *vdev, struct vnic_dev_ring *ring)
227 {
228         if (ring->descs) {
229                 pci_free_consistent(vdev->pdev,
230                         ring->size_unaligned,
231                         ring->descs_unaligned,
232                         ring->base_addr_unaligned);
233                 ring->descs = NULL;
234         }
235 }
236
237 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
238         u64 *a0, u64 *a1, int wait)
239 {
240         struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
241         int delay;
242         u32 status;
243         int err;
244
245         status = ioread32(&devcmd->status);
246         if (status & STAT_BUSY) {
247                 printk(KERN_ERR "Busy devcmd %d\n", _CMD_N(cmd));
248                 return -EBUSY;
249         }
250
251         if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
252                 writeq(*a0, &devcmd->args[0]);
253                 writeq(*a1, &devcmd->args[1]);
254                 wmb();
255         }
256
257         iowrite32(cmd, &devcmd->cmd);
258
259         if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
260                         return 0;
261
262         for (delay = 0; delay < wait; delay++) {
263
264                 udelay(100);
265
266                 status = ioread32(&devcmd->status);
267                 if (!(status & STAT_BUSY)) {
268
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",
274                                                 err, _CMD_N(cmd));
275                                 return err;
276                         }
277
278                         if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
279                                 rmb();
280                                 *a0 = readq(&devcmd->args[0]);
281                                 *a1 = readq(&devcmd->args[1]);
282                         }
283
284                         return 0;
285                 }
286         }
287
288         printk(KERN_ERR "Timedout devcmd %d\n", _CMD_N(cmd));
289         return -ETIMEDOUT;
290 }
291
292 static int vnic_dev_capable(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd)
293 {
294         u64 a0 = (u32)cmd, a1 = 0;
295         int wait = 1000;
296         int err;
297
298         err = vnic_dev_cmd(vdev, CMD_CAPABILITY, &a0, &a1, wait);
299
300         return !(err || a0);
301 }
302
303 int vnic_dev_fw_info(struct vnic_dev *vdev,
304         struct vnic_devcmd_fw_info **fw_info)
305 {
306         u64 a0, a1 = 0;
307         int wait = 1000;
308         int err = 0;
309
310         if (!vdev->fw_info) {
311                 vdev->fw_info = pci_alloc_consistent(vdev->pdev,
312                         sizeof(struct vnic_devcmd_fw_info),
313                         &vdev->fw_info_pa);
314                 if (!vdev->fw_info)
315                         return -ENOMEM;
316
317                 a0 = vdev->fw_info_pa;
318
319                 /* only get fw_info once and cache it */
320                 err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
321         }
322
323         *fw_info = vdev->fw_info;
324
325         return err;
326 }
327
328 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
329         void *value)
330 {
331         u64 a0, a1;
332         int wait = 1000;
333         int err;
334
335         a0 = offset;
336         a1 = size;
337
338         err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
339
340         switch (size) {
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;
346         }
347
348         return err;
349 }
350
351 int vnic_dev_stats_clear(struct vnic_dev *vdev)
352 {
353         u64 a0 = 0, a1 = 0;
354         int wait = 1000;
355         return vnic_dev_cmd(vdev, CMD_STATS_CLEAR, &a0, &a1, wait);
356 }
357
358 int vnic_dev_stats_dump(struct vnic_dev *vdev, struct vnic_stats **stats)
359 {
360         u64 a0, a1;
361         int wait = 1000;
362
363         if (!vdev->stats) {
364                 vdev->stats = pci_alloc_consistent(vdev->pdev,
365                         sizeof(struct vnic_stats), &vdev->stats_pa);
366                 if (!vdev->stats)
367                         return -ENOMEM;
368         }
369
370         *stats = vdev->stats;
371         a0 = vdev->stats_pa;
372         a1 = sizeof(struct vnic_stats);
373
374         return vnic_dev_cmd(vdev, CMD_STATS_DUMP, &a0, &a1, wait);
375 }
376
377 int vnic_dev_close(struct vnic_dev *vdev)
378 {
379         u64 a0 = 0, a1 = 0;
380         int wait = 1000;
381         return vnic_dev_cmd(vdev, CMD_CLOSE, &a0, &a1, wait);
382 }
383
384 int vnic_dev_enable(struct vnic_dev *vdev)
385 {
386         u64 a0 = 0, a1 = 0;
387         int wait = 1000;
388         return vnic_dev_cmd(vdev, CMD_ENABLE, &a0, &a1, wait);
389 }
390
391 int vnic_dev_disable(struct vnic_dev *vdev)
392 {
393         u64 a0 = 0, a1 = 0;
394         int wait = 1000;
395         return vnic_dev_cmd(vdev, CMD_DISABLE, &a0, &a1, wait);
396 }
397
398 int vnic_dev_open(struct vnic_dev *vdev, int arg)
399 {
400         u64 a0 = (u32)arg, a1 = 0;
401         int wait = 1000;
402         return vnic_dev_cmd(vdev, CMD_OPEN, &a0, &a1, wait);
403 }
404
405 int vnic_dev_open_done(struct vnic_dev *vdev, int *done)
406 {
407         u64 a0 = 0, a1 = 0;
408         int wait = 1000;
409         int err;
410
411         *done = 0;
412
413         err = vnic_dev_cmd(vdev, CMD_OPEN_STATUS, &a0, &a1, wait);
414         if (err)
415                 return err;
416
417         *done = (a0 == 0);
418
419         return 0;
420 }
421
422 int vnic_dev_soft_reset(struct vnic_dev *vdev, int arg)
423 {
424         u64 a0 = (u32)arg, a1 = 0;
425         int wait = 1000;
426         return vnic_dev_cmd(vdev, CMD_SOFT_RESET, &a0, &a1, wait);
427 }
428
429 int vnic_dev_soft_reset_done(struct vnic_dev *vdev, int *done)
430 {
431         u64 a0 = 0, a1 = 0;
432         int wait = 1000;
433         int err;
434
435         *done = 0;
436
437         err = vnic_dev_cmd(vdev, CMD_SOFT_RESET_STATUS, &a0, &a1, wait);
438         if (err)
439                 return err;
440
441         *done = (a0 == 0);
442
443         return 0;
444 }
445
446 int vnic_dev_hang_notify(struct vnic_dev *vdev)
447 {
448         u64 a0, a1;
449         int wait = 1000;
450         return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
451 }
452
453 int vnic_dev_mac_addr(struct vnic_dev *vdev, u8 *mac_addr)
454 {
455         u64 a0, a1;
456         int wait = 1000;
457         int err, i;
458
459         for (i = 0; i < ETH_ALEN; i++)
460                 mac_addr[i] = 0;
461
462         err = vnic_dev_cmd(vdev, CMD_MAC_ADDR, &a0, &a1, wait);
463         if (err)
464                 return err;
465
466         for (i = 0; i < ETH_ALEN; i++)
467                 mac_addr[i] = ((u8 *)&a0)[i];
468
469         return 0;
470 }
471
472 void vnic_dev_packet_filter(struct vnic_dev *vdev, int directed, int multicast,
473         int broadcast, int promisc, int allmulti)
474 {
475         u64 a0, a1 = 0;
476         int wait = 1000;
477         int err;
478
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);
484
485         err = vnic_dev_cmd(vdev, CMD_PACKET_FILTER, &a0, &a1, wait);
486         if (err)
487                 printk(KERN_ERR "Can't set packet filter\n");
488 }
489
490 void vnic_dev_add_addr(struct vnic_dev *vdev, u8 *addr)
491 {
492         u64 a0 = 0, a1 = 0;
493         int wait = 1000;
494         int err;
495         int i;
496
497         for (i = 0; i < ETH_ALEN; i++)
498                 ((u8 *)&a0)[i] = addr[i];
499
500         err = vnic_dev_cmd(vdev, CMD_ADDR_ADD, &a0, &a1, wait);
501         if (err)
502                 printk(KERN_ERR "Can't add addr [%pM], %d\n", addr, err);
503 }
504
505 void vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
506 {
507         u64 a0 = 0, a1 = 0;
508         int wait = 1000;
509         int err;
510         int i;
511
512         for (i = 0; i < ETH_ALEN; i++)
513                 ((u8 *)&a0)[i] = addr[i];
514
515         err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
516         if (err)
517                 printk(KERN_ERR "Can't del addr [%pM], %d\n", addr, err);
518 }
519
520 int vnic_dev_notify_set(struct vnic_dev *vdev, u16 intr)
521 {
522         u64 a0, a1;
523         int wait = 1000;
524         int r;
525
526         if (!vdev->notify) {
527                 vdev->notify = pci_alloc_consistent(vdev->pdev,
528                         sizeof(struct vnic_devcmd_notify),
529                         &vdev->notify_pa);
530                 if (!vdev->notify)
531                         return -ENOMEM;
532                 memset(vdev->notify, 0, sizeof(struct vnic_devcmd_notify));
533         }
534
535         a0 = vdev->notify_pa;
536         a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
537         a1 += sizeof(struct vnic_devcmd_notify);
538
539         r = vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
540         vdev->notify_sz = (r == 0) ? (u32)a1 : 0;
541         return r;
542 }
543
544 void vnic_dev_notify_unset(struct vnic_dev *vdev)
545 {
546         u64 a0, a1;
547         int wait = 1000;
548
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);
552
553         vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
554         vdev->notify_sz = 0;
555 }
556
557 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
558 {
559         u32 *words;
560         unsigned int nwords = vdev->notify_sz / 4;
561         unsigned int i;
562         u32 csum;
563
564         if (!vdev->notify || !vdev->notify_sz)
565                 return 0;
566
567         do {
568                 csum = 0;
569                 memcpy(&vdev->notify_copy, vdev->notify, vdev->notify_sz);
570                 words = (u32 *)&vdev->notify_copy;
571                 for (i = 1; i < nwords; i++)
572                         csum += words[i];
573         } while (csum != words[0]);
574
575         return 1;
576 }
577
578 int vnic_dev_init(struct vnic_dev *vdev, int arg)
579 {
580         u64 a0 = (u32)arg, a1 = 0;
581         int wait = 1000;
582         int r = 0;
583
584         if (vdev->cap_flags & VNIC_DEV_CAP_INIT)
585                 r = vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
586         else {
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);
593                 }
594         }
595         return r;
596 }
597
598 int vnic_dev_link_status(struct vnic_dev *vdev)
599 {
600         if (vdev->linkstatus)
601                 return *vdev->linkstatus;
602
603         if (!vnic_dev_notify_ready(vdev))
604                 return 0;
605
606         return vdev->notify_copy.link_state;
607 }
608
609 u32 vnic_dev_port_speed(struct vnic_dev *vdev)
610 {
611         if (!vnic_dev_notify_ready(vdev))
612                 return 0;
613
614         return vdev->notify_copy.port_speed;
615 }
616
617 u32 vnic_dev_msg_lvl(struct vnic_dev *vdev)
618 {
619         if (!vnic_dev_notify_ready(vdev))
620                 return 0;
621
622         return vdev->notify_copy.msglvl;
623 }
624
625 u32 vnic_dev_mtu(struct vnic_dev *vdev)
626 {
627         if (!vnic_dev_notify_ready(vdev))
628                 return 0;
629
630         return vdev->notify_copy.mtu;
631 }
632
633 u32 vnic_dev_link_down_cnt(struct vnic_dev *vdev)
634 {
635         if (!vnic_dev_notify_ready(vdev))
636                 return 0;
637
638         return vdev->notify_copy.link_down_cnt;
639 }
640
641 u32 vnic_dev_notify_status(struct vnic_dev *vdev)
642 {
643         if (!vnic_dev_notify_ready(vdev))
644                 return 0;
645
646         return vdev->notify_copy.status;
647 }
648
649 void vnic_dev_set_intr_mode(struct vnic_dev *vdev,
650         enum vnic_dev_intr_mode intr_mode)
651 {
652         vdev->intr_mode = intr_mode;
653 }
654
655 enum vnic_dev_intr_mode vnic_dev_get_intr_mode(
656         struct vnic_dev *vdev)
657 {
658         return vdev->intr_mode;
659 }
660
661 void vnic_dev_unregister(struct vnic_dev *vdev)
662 {
663         if (vdev) {
664                 if (vdev->notify)
665                         pci_free_consistent(vdev->pdev,
666                                 sizeof(struct vnic_devcmd_notify),
667                                 vdev->notify,
668                                 vdev->notify_pa);
669                 if (vdev->linkstatus)
670                         pci_free_consistent(vdev->pdev,
671                                 sizeof(u32),
672                                 vdev->linkstatus,
673                                 vdev->linkstatus_pa);
674                 if (vdev->stats)
675                         pci_free_consistent(vdev->pdev,
676                                 sizeof(struct vnic_dev),
677                                 vdev->stats, vdev->stats_pa);
678                 if (vdev->fw_info)
679                         pci_free_consistent(vdev->pdev,
680                                 sizeof(struct vnic_devcmd_fw_info),
681                                 vdev->fw_info, vdev->fw_info_pa);
682                 kfree(vdev);
683         }
684 }
685
686 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
687         void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar)
688 {
689         if (!vdev) {
690                 vdev = kzalloc(sizeof(struct vnic_dev), GFP_ATOMIC);
691                 if (!vdev)
692                         return NULL;
693         }
694
695         vdev->priv = priv;
696         vdev->pdev = pdev;
697
698         if (vnic_dev_discover_res(vdev, bar))
699                 goto err_out;
700
701         vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
702         if (!vdev->devcmd)
703                 goto err_out;
704
705         vdev->cap_flags = 0;
706
707         if (vnic_dev_capable(vdev, CMD_INIT))
708                 vdev->cap_flags |= VNIC_DEV_CAP_INIT;
709
710         return vdev;
711
712 err_out:
713         vnic_dev_unregister(vdev);
714         return NULL;
715 }
716
717