Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vnic_dev.c
Go to the documentation of this file.
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 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/delay.h>
24 #include <linux/if_ether.h>
25 #include <linux/slab.h>
26 #include "vnic_resource.h"
27 #include "vnic_devcmd.h"
28 #include "vnic_dev.h"
29 #include "vnic_stats.h"
30 
31 struct vnic_res {
32  void __iomem *vaddr;
33  unsigned int count;
34 };
35 
36 struct vnic_dev {
37  void *priv;
38  struct pci_dev *pdev;
39  struct vnic_res res[RES_TYPE_MAX];
41  struct vnic_devcmd __iomem *devcmd;
42  struct vnic_devcmd_notify *notify;
47  struct vnic_stats *stats;
51 };
52 
53 #define VNIC_MAX_RES_HDR_SIZE \
54  (sizeof(struct vnic_resource_header) + \
55  sizeof(struct vnic_resource) * RES_TYPE_MAX)
56 #define VNIC_RES_STRIDE 128
57 
58 void *vnic_dev_priv(struct vnic_dev *vdev)
59 {
60  return vdev->priv;
61 }
62 
63 static int vnic_dev_discover_res(struct vnic_dev *vdev,
64  struct vnic_dev_bar *bar)
65 {
66  struct vnic_resource_header __iomem *rh;
67  struct vnic_resource __iomem *r;
68  u8 type;
69 
70  if (bar->len < VNIC_MAX_RES_HDR_SIZE) {
71  printk(KERN_ERR "vNIC BAR0 res hdr length error\n");
72  return -EINVAL;
73  }
74 
75  rh = bar->vaddr;
76  if (!rh) {
77  printk(KERN_ERR "vNIC BAR0 res hdr not mem-mapped\n");
78  return -EINVAL;
79  }
80 
81  if (ioread32(&rh->magic) != VNIC_RES_MAGIC ||
83  printk(KERN_ERR "vNIC BAR0 res magic/version error "
84  "exp (%lx/%lx) curr (%x/%x)\n",
86  ioread32(&rh->magic), ioread32(&rh->version));
87  return -EINVAL;
88  }
89 
90  r = (struct vnic_resource __iomem *)(rh + 1);
91 
92  while ((type = ioread8(&r->type)) != RES_TYPE_EOL) {
93 
94  u8 bar_num = ioread8(&r->bar);
96  u32 count = ioread32(&r->count);
97  u32 len;
98 
99  r++;
100 
101  if (bar_num != 0) /* only mapping in BAR0 resources */
102  continue;
103 
104  switch (type) {
105  case RES_TYPE_WQ:
106  case RES_TYPE_RQ:
107  case RES_TYPE_CQ:
108  case RES_TYPE_INTR_CTRL:
109  /* each count is stride bytes long */
110  len = count * VNIC_RES_STRIDE;
111  if (len + bar_offset > bar->len) {
112  printk(KERN_ERR "vNIC BAR0 resource %d "
113  "out-of-bounds, offset 0x%x + "
114  "size 0x%x > bar len 0x%lx\n",
115  type, bar_offset,
116  len,
117  bar->len);
118  return -EINVAL;
119  }
120  break;
122  case RES_TYPE_DEVCMD:
123  len = count;
124  break;
125  default:
126  continue;
127  }
128 
129  vdev->res[type].count = count;
130  vdev->res[type].vaddr = (char __iomem *)bar->vaddr + bar_offset;
131  }
132 
133  return 0;
134 }
135 
136 unsigned int vnic_dev_get_res_count(struct vnic_dev *vdev,
137  enum vnic_res_type type)
138 {
139  return vdev->res[type].count;
140 }
141 
142 void __iomem *vnic_dev_get_res(struct vnic_dev *vdev, enum vnic_res_type type,
143  unsigned int index)
144 {
145  if (!vdev->res[type].vaddr)
146  return NULL;
147 
148  switch (type) {
149  case RES_TYPE_WQ:
150  case RES_TYPE_RQ:
151  case RES_TYPE_CQ:
152  case RES_TYPE_INTR_CTRL:
153  return (char __iomem *)vdev->res[type].vaddr +
154  index * VNIC_RES_STRIDE;
155  default:
156  return (char __iomem *)vdev->res[type].vaddr;
157  }
158 }
159 
161  unsigned int desc_count,
162  unsigned int desc_size)
163 {
164  /* The base address of the desc rings must be 512 byte aligned.
165  * Descriptor count is aligned to groups of 32 descriptors. A
166  * count of 0 means the maximum 4096 descriptors. Descriptor
167  * size is aligned to 16 bytes.
168  */
169 
170  unsigned int count_align = 32;
171  unsigned int desc_align = 16;
172 
173  ring->base_align = 512;
174 
175  if (desc_count == 0)
176  desc_count = 4096;
177 
178  ring->desc_count = ALIGN(desc_count, count_align);
179 
180  ring->desc_size = ALIGN(desc_size, desc_align);
181 
182  ring->size = ring->desc_count * ring->desc_size;
183  ring->size_unaligned = ring->size + ring->base_align;
184 
185  return ring->size_unaligned;
186 }
187 
189 {
190  memset(ring->descs, 0, ring->size);
191 }
192 
194  unsigned int desc_count, unsigned int desc_size)
195 {
196  vnic_dev_desc_ring_size(ring, desc_count, desc_size);
197 
199  ring->size_unaligned,
200  &ring->base_addr_unaligned);
201 
202  if (!ring->descs_unaligned) {
204  "Failed to allocate ring (size=%d), aborting\n",
205  (int)ring->size);
206  return -ENOMEM;
207  }
208 
209  ring->base_addr = ALIGN(ring->base_addr_unaligned,
210  ring->base_align);
211  ring->descs = (u8 *)ring->descs_unaligned +
212  (ring->base_addr - ring->base_addr_unaligned);
213 
215 
216  ring->desc_avail = ring->desc_count - 1;
217 
218  return 0;
219 }
220 
222 {
223  if (ring->descs) {
225  ring->size_unaligned,
226  ring->descs_unaligned,
227  ring->base_addr_unaligned);
228  ring->descs = NULL;
229  }
230 }
231 
232 int vnic_dev_cmd(struct vnic_dev *vdev, enum vnic_devcmd_cmd cmd,
233  u64 *a0, u64 *a1, int wait)
234 {
235  struct vnic_devcmd __iomem *devcmd = vdev->devcmd;
236  int delay;
237  u32 status;
238  int dev_cmd_err[] = {
239  /* convert from fw's version of error.h to host's version */
240  0, /* ERR_SUCCESS */
241  EINVAL, /* ERR_EINVAL */
242  EFAULT, /* ERR_EFAULT */
243  EPERM, /* ERR_EPERM */
244  EBUSY, /* ERR_EBUSY */
245  };
246  int err;
247 
248  status = ioread32(&devcmd->status);
249  if (status & STAT_BUSY) {
250  printk(KERN_ERR "Busy devcmd %d\n", _CMD_N(cmd));
251  return -EBUSY;
252  }
253 
254  if (_CMD_DIR(cmd) & _CMD_DIR_WRITE) {
255  writeq(*a0, &devcmd->args[0]);
256  writeq(*a1, &devcmd->args[1]);
257  wmb();
258  }
259 
260  iowrite32(cmd, &devcmd->cmd);
261 
262  if ((_CMD_FLAGS(cmd) & _CMD_FLAGS_NOWAIT))
263  return 0;
264 
265  for (delay = 0; delay < wait; delay++) {
266 
267  udelay(100);
268 
269  status = ioread32(&devcmd->status);
270  if (!(status & STAT_BUSY)) {
271 
272  if (status & STAT_ERROR) {
273  err = dev_cmd_err[(int)readq(&devcmd->args[0])];
274  printk(KERN_ERR "Error %d devcmd %d\n",
275  err, _CMD_N(cmd));
276  return -err;
277  }
278 
279  if (_CMD_DIR(cmd) & _CMD_DIR_READ) {
280  rmb();
281  *a0 = readq(&devcmd->args[0]);
282  *a1 = readq(&devcmd->args[1]);
283  }
284 
285  return 0;
286  }
287  }
288 
289  printk(KERN_ERR "Timedout devcmd %d\n", _CMD_N(cmd));
290  return -ETIMEDOUT;
291 }
292 
293 int vnic_dev_fw_info(struct vnic_dev *vdev,
294  struct vnic_devcmd_fw_info **fw_info)
295 {
296  u64 a0, a1 = 0;
297  int wait = 1000;
298  int err = 0;
299 
300  if (!vdev->fw_info) {
301  vdev->fw_info = pci_alloc_consistent(vdev->pdev,
302  sizeof(struct vnic_devcmd_fw_info),
303  &vdev->fw_info_pa);
304  if (!vdev->fw_info)
305  return -ENOMEM;
306 
307  a0 = vdev->fw_info_pa;
308 
309  /* only get fw_info once and cache it */
310  err = vnic_dev_cmd(vdev, CMD_MCPU_FW_INFO, &a0, &a1, wait);
311  }
312 
313  *fw_info = vdev->fw_info;
314 
315  return err;
316 }
317 
318 int vnic_dev_spec(struct vnic_dev *vdev, unsigned int offset, unsigned int size,
319  void *value)
320 {
321  u64 a0, a1;
322  int wait = 1000;
323  int err;
324 
325  a0 = offset;
326  a1 = size;
327 
328  err = vnic_dev_cmd(vdev, CMD_DEV_SPEC, &a0, &a1, wait);
329 
330  switch (size) {
331  case 1:
332  *(u8 *)value = (u8)a0;
333  break;
334  case 2:
335  *(u16 *)value = (u16)a0;
336  break;
337  case 4:
338  *(u32 *)value = (u32)a0;
339  break;
340  case 8:
341  *(u64 *)value = a0;
342  break;
343  default:
344  BUG();
345  break;
346  }
347 
348  return err;
349 }
350 
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 
447 {
448  u64 a0, a1;
449  int wait = 1000;
450  return vnic_dev_cmd(vdev, CMD_HANG_NOTIFY, &a0, &a1, wait);
451 }
452 
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)
503  "Can't add addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
504  addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
505  err);
506 }
507 
508 void vnic_dev_del_addr(struct vnic_dev *vdev, u8 *addr)
509 {
510  u64 a0 = 0, a1 = 0;
511  int wait = 1000;
512  int err;
513  int i;
514 
515  for (i = 0; i < ETH_ALEN; i++)
516  ((u8 *)&a0)[i] = addr[i];
517 
518  err = vnic_dev_cmd(vdev, CMD_ADDR_DEL, &a0, &a1, wait);
519  if (err)
521  "Can't del addr [%02x:%02x:%02x:%02x:%02x:%02x], %d\n",
522  addr[0], addr[1], addr[2], addr[3], addr[4], addr[5],
523  err);
524 }
525 
527 {
528  u64 a0, a1;
529  int wait = 1000;
530 
531  if (!vdev->notify) {
532  vdev->notify = pci_alloc_consistent(vdev->pdev,
533  sizeof(struct vnic_devcmd_notify),
534  &vdev->notify_pa);
535  if (!vdev->notify)
536  return -ENOMEM;
537  }
538 
539  a0 = vdev->notify_pa;
540  a1 = ((u64)intr << 32) & 0x0000ffff00000000ULL;
541  a1 += sizeof(struct vnic_devcmd_notify);
542 
543  return vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
544 }
545 
546 void vnic_dev_notify_unset(struct vnic_dev *vdev)
547 {
548  u64 a0, a1;
549  int wait = 1000;
550 
551  a0 = 0; /* paddr = 0 to unset notify buffer */
552  a1 = 0x0000ffff00000000ULL; /* intr num = -1 to unreg for intr */
553  a1 += sizeof(struct vnic_devcmd_notify);
554 
555  vnic_dev_cmd(vdev, CMD_NOTIFY, &a0, &a1, wait);
556 }
557 
558 static int vnic_dev_notify_ready(struct vnic_dev *vdev)
559 {
560  u32 *words;
561  unsigned int nwords = sizeof(struct vnic_devcmd_notify) / 4;
562  unsigned int i;
563  u32 csum;
564 
565  if (!vdev->notify)
566  return 0;
567 
568  do {
569  csum = 0;
570  memcpy(&vdev->notify_copy, vdev->notify,
571  sizeof(struct vnic_devcmd_notify));
572  words = (u32 *)&vdev->notify_copy;
573  for (i = 1; i < nwords; i++)
574  csum += words[i];
575  } while (csum != words[0]);
576 
577  return 1;
578 }
579 
580 int vnic_dev_init(struct vnic_dev *vdev, int arg)
581 {
582  u64 a0 = (u32)arg, a1 = 0;
583  int wait = 1000;
584  return vnic_dev_cmd(vdev, CMD_INIT, &a0, &a1, wait);
585 }
586 
588 {
589  if (vdev->linkstatus)
590  return *vdev->linkstatus;
591 
592  if (!vnic_dev_notify_ready(vdev))
593  return 0;
594 
595  return vdev->notify_copy.link_state;
596 }
597 
599 {
600  if (!vnic_dev_notify_ready(vdev))
601  return 0;
602 
603  return vdev->notify_copy.port_speed;
604 }
605 
607 {
608  if (!vnic_dev_notify_ready(vdev))
609  return 0;
610 
611  return vdev->notify_copy.msglvl;
612 }
613 
614 u32 vnic_dev_mtu(struct vnic_dev *vdev)
615 {
616  if (!vnic_dev_notify_ready(vdev))
617  return 0;
618 
619  return vdev->notify_copy.mtu;
620 }
621 
623 {
624  if (!vnic_dev_notify_ready(vdev))
625  return 0;
626 
627  return vdev->notify_copy.link_down_cnt;
628 }
629 
631  enum vnic_dev_intr_mode intr_mode)
632 {
633  vdev->intr_mode = intr_mode;
634 }
635 
637  struct vnic_dev *vdev)
638 {
639  return vdev->intr_mode;
640 }
641 
642 void vnic_dev_unregister(struct vnic_dev *vdev)
643 {
644  if (vdev) {
645  if (vdev->notify)
647  sizeof(struct vnic_devcmd_notify),
648  vdev->notify,
649  vdev->notify_pa);
650  if (vdev->linkstatus)
652  sizeof(u32),
653  vdev->linkstatus,
654  vdev->linkstatus_pa);
655  if (vdev->stats)
657  sizeof(struct vnic_stats),
658  vdev->stats, vdev->stats_pa);
659  if (vdev->fw_info)
661  sizeof(struct vnic_devcmd_fw_info),
662  vdev->fw_info, vdev->fw_info_pa);
663  kfree(vdev);
664  }
665 }
666 
667 struct vnic_dev *vnic_dev_register(struct vnic_dev *vdev,
668  void *priv, struct pci_dev *pdev, struct vnic_dev_bar *bar)
669 {
670  if (!vdev) {
671  vdev = kzalloc(sizeof(struct vnic_dev), GFP_KERNEL);
672  if (!vdev)
673  return NULL;
674  }
675 
676  vdev->priv = priv;
677  vdev->pdev = pdev;
678 
679  if (vnic_dev_discover_res(vdev, bar))
680  goto err_out;
681 
682  vdev->devcmd = vnic_dev_get_res(vdev, RES_TYPE_DEVCMD, 0);
683  if (!vdev->devcmd)
684  goto err_out;
685 
686  return vdev;
687 
688 err_out:
689  vnic_dev_unregister(vdev);
690  return NULL;
691 }