3 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
20 #include <linux/kernel.h>
21 #ifdef CONFIG_MODVERSIONS
22 #include <config/modversions.h>
24 #include "diagnostics/appos_subsystems.h"
26 #include "vbuschannel.h"
27 #include "vbushelper.h"
28 #include <linux/types.h>
30 #include <linux/uuid.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/pci.h>
34 #include <linux/device.h>
35 #include <linux/list.h>
36 #include <linux/slab.h>
37 #include <linux/mod_devicetable.h>
38 #include <linux/if_ether.h>
39 #include <linux/version.h>
40 #include <linux/debugfs.h>
42 #include "guestlinuxdebug.h"
45 struct driver_private {
47 struct klist klist_devices;
48 struct klist_node knode_bus;
49 struct module_kobject *mkobj;
50 struct device_driver *driver;
53 #define to_driver(obj) container_of(obj, struct driver_private, kobj)
55 /* bus_id went away in 2.6.30 - the size was 20 bytes, so we'll define
56 * it ourselves, and a macro to make getting the field a bit simpler.
59 #define BUS_ID_SIZE 20
62 #define BUS_ID(x) dev_name(x)
64 /* MAX_BUF = 4 busses x ( 32 devices/bus + 1 busline) x 80 characters
65 * = 10,560 bytes ~ 2^14 = 16,384 bytes
71 /* this is shorter than using __FILE__ (full path name) in
72 * debug/info/error messages
74 #define CURRENT_FILE_PC VIRT_PCI_PC_virtpci_c
75 #define __MYFILE__ "virtpci.c"
77 #define VIRTPCI_VERSION "01.00"
79 /*****************************************************/
80 /* Forward declarations */
81 /*****************************************************/
83 static int delete_vbus_device(struct device *vbus, void *data);
84 static int match_busid(struct device *dev, void *data);
85 static void virtpci_bus_release(struct device *dev);
86 static void virtpci_device_release(struct device *dev);
87 static int virtpci_device_add(struct device *parentbus, int devtype,
88 struct add_virt_guestpart *addparams,
89 struct scsi_adap_info *scsi,
90 struct net_adap_info *net);
91 static int virtpci_device_del(struct device *parentbus, int devtype,
92 struct vhba_wwnn *wwnn, unsigned char macaddr[]);
93 static int virtpci_device_serverdown(struct device *parentbus, int devtype,
94 struct vhba_wwnn *wwnn,
95 unsigned char macaddr[]);
96 static int virtpci_device_serverup(struct device *parentbus, int devtype,
97 struct vhba_wwnn *wwnn,
98 unsigned char macaddr[]);
99 static ssize_t virtpci_driver_attr_show(struct kobject *kobj,
100 struct attribute *attr, char *buf);
101 static ssize_t virtpci_driver_attr_store(struct kobject *kobj,
102 struct attribute *attr,
103 const char *buf, size_t count);
104 static int virtpci_bus_match(struct device *dev, struct device_driver *drv);
105 static int virtpci_uevent(struct device *dev, struct kobj_uevent_env *env);
106 static int virtpci_device_probe(struct device *dev);
107 static int virtpci_device_remove(struct device *dev);
109 static ssize_t info_debugfs_read(struct file *file, char __user *buf,
110 size_t len, loff_t *offset);
112 static const struct file_operations debugfs_info_fops = {
113 .read = info_debugfs_read,
116 /*****************************************************/
118 /*****************************************************/
120 /* methods in bus_type struct allow the bus code to serve as an
121 * intermediary between the device core and individual device core and
124 static struct bus_type virtpci_bus_type = {
125 .name = "uisvirtpci",
126 .match = virtpci_bus_match,
127 .uevent = virtpci_uevent,
130 static struct device virtpci_rootbus_device = {
131 .init_name = "vbusroot", /* root bus */
132 .release = virtpci_bus_release
135 /* filled in with info about parent chipset driver when we register with it */
136 static struct ultra_vbus_deviceinfo chipset_driver_info;
138 static const struct sysfs_ops virtpci_driver_sysfs_ops = {
139 .show = virtpci_driver_attr_show,
140 .store = virtpci_driver_attr_store,
143 static struct kobj_type virtpci_driver_kobj_type = {
144 .sysfs_ops = &virtpci_driver_sysfs_ops,
147 static struct virtpci_dev *vpcidev_list_head;
148 static DEFINE_RWLOCK(vpcidev_list_lock);
150 /* filled in with info about this driver, wrt it servicing client busses */
151 static struct ultra_vbus_deviceinfo bus_driver_info;
153 /*****************************************************/
154 /* debugfs entries */
155 /*****************************************************/
156 /* dentry is used to create the debugfs entry directory
159 static struct dentry *virtpci_debugfs_dir;
161 struct virtpci_busdev {
162 struct device virtpci_bus_device;
165 /*****************************************************/
166 /* Local functions */
167 /*****************************************************/
170 int WAIT_FOR_IO_CHANNEL(struct spar_io_channel_protocol __iomem *chanptr)
175 if (SPAR_CHANNEL_SERVER_READY(&chanptr->channel_header))
177 UIS_THREAD_WAIT_SEC(1);
183 /* Write the contents of <info> to the ULTRA_VBUS_CHANNEL_PROTOCOL.ChpInfo. */
184 static int write_vbus_chp_info(struct spar_vbus_channel_protocol *chan,
185 struct ultra_vbus_deviceinfo *info)
192 off = sizeof(struct channel_header) + chan->hdr_info.chp_info_offset;
193 if (chan->hdr_info.chp_info_offset == 0) {
196 memcpy(((u8 *)(chan)) + off, info, sizeof(*info));
200 /* Write the contents of <info> to the ULTRA_VBUS_CHANNEL_PROTOCOL.BusInfo. */
201 static int write_vbus_bus_info(struct spar_vbus_channel_protocol *chan,
202 struct ultra_vbus_deviceinfo *info)
209 off = sizeof(struct channel_header) + chan->hdr_info.bus_info_offset;
210 if (chan->hdr_info.bus_info_offset == 0)
212 memcpy(((u8 *)(chan)) + off, info, sizeof(*info));
216 /* Write the contents of <info> to the
217 * ULTRA_VBUS_CHANNEL_PROTOCOL.DevInfo[<devix>].
220 write_vbus_dev_info(struct spar_vbus_channel_protocol *chan,
221 struct ultra_vbus_deviceinfo *info, int devix)
229 (sizeof(struct channel_header) +
230 chan->hdr_info.dev_info_offset) +
231 (chan->hdr_info.device_info_struct_bytes * devix);
232 if (chan->hdr_info.dev_info_offset == 0)
235 memcpy(((u8 *)(chan)) + off, info, sizeof(*info));
240 * returns 0 failure, 1 success,
242 static int add_vbus(struct add_vbus_guestpart *addparams)
247 vbus = kzalloc(sizeof(*vbus), GFP_ATOMIC);
249 POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
253 dev_set_name(vbus, "vbus%d", addparams->bus_no);
254 vbus->release = virtpci_bus_release;
255 vbus->parent = &virtpci_rootbus_device; /* root bus is parent */
256 vbus->bus = &virtpci_bus_type; /* bus type */
257 vbus->platform_data = (__force void *)addparams->chanptr;
259 /* register a virt bus device -
260 * this bus shows up under /sys/devices with .name value
261 * "virtpci%d" any devices added to this bus then show up under
262 * /sys/devices/virtpci0
264 ret = device_register(vbus);
266 POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
269 write_vbus_chp_info(vbus->platform_data /* chanptr */,
270 &chipset_driver_info);
271 write_vbus_bus_info(vbus->platform_data /* chanptr */,
273 POSTCODE_LINUX_2(VPCI_CREATE_EXIT_PC, POSTCODE_SEVERITY_INFO);
277 /* for CHANSOCK wwwnn/max are AUTO-GENERATED; for normal channels,
278 * wwnn/max are in the channel header.
280 #define GET_SCSIADAPINFO_FROM_CHANPTR(chanptr) { \
281 memcpy_fromio(&scsi.wwnn, \
282 &((struct spar_io_channel_protocol __iomem *) \
283 chanptr)->vhba.wwnn, \
284 sizeof(struct vhba_wwnn)); \
285 memcpy_fromio(&scsi.max, \
286 &((struct spar_io_channel_protocol __iomem *) \
287 chanptr)->vhba.max, \
288 sizeof(struct vhba_config_max)); \
292 * returns 0 failure, 1 success,
294 static int add_vhba(struct add_virt_guestpart *addparams)
297 struct scsi_adap_info scsi;
299 unsigned char busid[BUS_ID_SIZE];
301 POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
302 if (!WAIT_FOR_IO_CHANNEL
303 ((struct spar_io_channel_protocol __iomem *)addparams->chanptr)) {
304 POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
308 GET_SCSIADAPINFO_FROM_CHANPTR(addparams->chanptr);
310 /* find bus device with the busid that matches match_busid */
311 sprintf(busid, "vbus%d", addparams->bus_no);
312 vbus = bus_find_device(&virtpci_bus_type, NULL,
313 (void *)busid, match_busid);
317 i = virtpci_device_add(vbus, VIRTHBA_TYPE, addparams, &scsi, NULL);
319 POSTCODE_LINUX_3(VPCI_CREATE_EXIT_PC, i,
320 POSTCODE_SEVERITY_INFO);
325 /* for CHANSOCK macaddr is AUTO-GENERATED; for normal channels,
326 * macaddr is in the channel header.
328 #define GET_NETADAPINFO_FROM_CHANPTR(chanptr) { \
329 memcpy_fromio(net.mac_addr, \
330 ((struct spar_io_channel_protocol __iomem *) \
331 chanptr)->vnic.macaddr, \
334 readl(&((struct spar_io_channel_protocol __iomem *)\
335 chanptr)->vnic.num_rcv_bufs); \
336 net.mtu = readl(&((struct spar_io_channel_protocol __iomem *) \
337 chanptr)->vnic.mtu); \
338 memcpy_fromio(&net.zone_uuid, \
339 &((struct spar_io_channel_protocol __iomem *)\
340 chanptr)->vnic.zone_uuid, \
345 * returns 0 failure, 1 success,
348 add_vnic(struct add_virt_guestpart *addparams)
351 struct net_adap_info net;
353 unsigned char busid[BUS_ID_SIZE];
355 POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
356 if (!WAIT_FOR_IO_CHANNEL
357 ((struct spar_io_channel_protocol __iomem *)addparams->chanptr)) {
358 POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
362 GET_NETADAPINFO_FROM_CHANPTR(addparams->chanptr);
364 /* find bus device with the busid that matches match_busid */
365 sprintf(busid, "vbus%d", addparams->bus_no);
366 vbus = bus_find_device(&virtpci_bus_type, NULL,
367 (void *)busid, match_busid);
371 i = virtpci_device_add(vbus, VIRTNIC_TYPE, addparams, NULL, &net);
373 POSTCODE_LINUX_3(VPCI_CREATE_EXIT_PC, i,
374 POSTCODE_SEVERITY_INFO);
381 * returns 0 failure, 1 success,
384 delete_vbus(struct del_vbus_guestpart *delparams)
387 unsigned char busid[BUS_ID_SIZE];
389 /* find bus device with the busid that matches match_busid */
390 sprintf(busid, "vbus%d", delparams->bus_no);
391 vbus = bus_find_device(&virtpci_bus_type, NULL,
392 (void *)busid, match_busid);
396 /* ensure that bus has no devices? -- TBD */
401 delete_vbus_device(struct device *vbus, void *data)
403 struct device *dev = &virtpci_rootbus_device;
405 if ((data) && match_busid(vbus, (void *)BUS_ID(dev))) {
406 /* skip it - don't delete root bus */
407 return 0; /* pretend no error */
409 device_unregister(vbus);
411 return 0; /* no error */
415 * returns 0 failure, 1 success,
417 static int pause_vhba(struct pause_virt_guestpart *pauseparams)
420 struct scsi_adap_info scsi;
422 GET_SCSIADAPINFO_FROM_CHANPTR(pauseparams->chanptr);
424 i = virtpci_device_serverdown(NULL /*no parent bus */, VIRTHBA_TYPE,
430 * returns 0 failure, 1 success,
432 static int pause_vnic(struct pause_virt_guestpart *pauseparams)
435 struct net_adap_info net;
437 GET_NETADAPINFO_FROM_CHANPTR(pauseparams->chanptr);
439 i = virtpci_device_serverdown(NULL /*no parent bus */, VIRTNIC_TYPE,
445 * returns 0 failure, 1 success,
447 static int resume_vhba(struct resume_virt_guestpart *resumeparams)
450 struct scsi_adap_info scsi;
452 GET_SCSIADAPINFO_FROM_CHANPTR(resumeparams->chanptr);
454 i = virtpci_device_serverup(NULL /*no parent bus */, VIRTHBA_TYPE,
460 * returns 0 failure, 1 success,
463 resume_vnic(struct resume_virt_guestpart *resumeparams)
466 struct net_adap_info net;
468 GET_NETADAPINFO_FROM_CHANPTR(resumeparams->chanptr);
470 i = virtpci_device_serverup(NULL /*no parent bus */, VIRTNIC_TYPE,
476 * returns 0 failure, 1 success,
478 static int delete_vhba(struct del_virt_guestpart *delparams)
481 struct scsi_adap_info scsi;
483 GET_SCSIADAPINFO_FROM_CHANPTR(delparams->chanptr);
485 i = virtpci_device_del(NULL /*no parent bus */, VIRTHBA_TYPE,
494 * returns 0 failure, 1 success,
496 static int delete_vnic(struct del_virt_guestpart *delparams)
499 struct net_adap_info net;
501 GET_NETADAPINFO_FROM_CHANPTR(delparams->chanptr);
503 i = virtpci_device_del(NULL /*no parent bus */, VIRTNIC_TYPE, NULL,
508 #define DELETE_ONE_VPCIDEV(vpcidev) { \
509 device_unregister(&vpcidev->generic_dev); \
513 /* deletes all vhbas and vnics
514 * returns 0 failure, 1 success,
516 static void delete_all(void)
520 struct virtpci_dev *tmpvpcidev, *nextvpcidev;
522 /* delete the entire vhba/vnic list in one shot */
523 write_lock_irqsave(&vpcidev_list_lock, flags);
524 tmpvpcidev = vpcidev_list_head;
525 vpcidev_list_head = NULL;
526 write_unlock_irqrestore(&vpcidev_list_lock, flags);
528 /* delete one vhba/vnic at a time */
530 nextvpcidev = tmpvpcidev->next;
531 /* delete the vhba/vnic at tmpvpcidev */
532 DELETE_ONE_VPCIDEV(tmpvpcidev);
533 tmpvpcidev = nextvpcidev;
537 /* now delete each vbus */
538 bus_for_each_dev(&virtpci_bus_type, NULL, (void *)1,
542 /* deletes all vnics or vhbas
543 * returns 0 failure, 1 success,
545 static int delete_all_virt(enum virtpci_dev_type devtype,
546 struct del_vbus_guestpart *delparams)
549 unsigned char busid[BUS_ID_SIZE];
552 /* find bus device with the busid that matches match_busid */
553 sprintf(busid, "vbus%d", delparams->bus_no);
554 vbus = bus_find_device(&virtpci_bus_type, NULL,
555 (void *)busid, match_busid);
559 if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE))
562 /* delete all vhbas/vnics */
563 i = virtpci_device_del(vbus, devtype, NULL, NULL);
567 static int virtpci_ctrlchan_func(struct guest_msgs *msg)
569 switch (msg->msgtype) {
571 return add_vbus(&msg->add_vbus);
573 return add_vhba(&msg->add_vhba);
575 return add_vnic(&msg->add_vnic);
577 return delete_vbus(&msg->del_vbus);
579 return delete_vhba(&msg->del_vhba);
581 return delete_vnic(&msg->del_vhba);
582 case GUEST_DEL_ALL_VHBAS:
583 return delete_all_virt(VIRTHBA_TYPE, &msg->del_all_vhbas);
584 case GUEST_DEL_ALL_VNICS:
585 return delete_all_virt(VIRTNIC_TYPE, &msg->del_all_vnics);
586 case GUEST_DEL_ALL_VBUSES:
589 case GUEST_PAUSE_VHBA:
590 return pause_vhba(&msg->pause_vhba);
591 case GUEST_PAUSE_VNIC:
592 return pause_vnic(&msg->pause_vnic);
593 case GUEST_RESUME_VHBA:
594 return resume_vhba(&msg->resume_vhba);
595 case GUEST_RESUME_VNIC:
596 return resume_vnic(&msg->resume_vnic);
602 /* same as driver_helper in bus.c linux */
603 static int match_busid(struct device *dev, void *data)
605 const char *name = data;
607 if (strcmp(name, BUS_ID(dev)) == 0)
612 /*****************************************************/
614 /*****************************************************/
616 static const struct pci_device_id *
617 virtpci_match_device(const struct pci_device_id *ids,
618 const struct virtpci_dev *dev)
620 while (ids->vendor || ids->subvendor || ids->class_mask) {
621 if ((ids->vendor == dev->vendor) &&
622 (ids->device == dev->device))
630 /* NOTE: !!!!!! This function is called when a new device is added
631 * for this bus. Or, it is called for existing devices when a new
632 * driver is added for this bus. It returns nonzero if a given device
633 * can be handled by the given driver.
635 static int virtpci_bus_match(struct device *dev, struct device_driver *drv)
637 struct virtpci_dev *virtpcidev = device_to_virtpci_dev(dev);
638 struct virtpci_driver *virtpcidrv = driver_to_virtpci_driver(drv);
641 /* check ids list for a match */
642 if (virtpci_match_device(virtpcidrv->id_table, virtpcidev))
645 return match; /* 0 - no match; 1 - yes it matches */
648 static int virtpci_uevent(struct device *dev, struct kobj_uevent_env *env)
650 /* add variables to the environment prior to the generation of
651 * hotplug events to user space
653 if (add_uevent_var(env, "VIRTPCI_VERSION=%s", VIRTPCI_VERSION))
658 /* For a child device just created on a client bus, fill in
659 * information about the driver that is controlling this device into
660 * the appropriate slot within the vbus channel of the bus
663 static void fix_vbus_dev_info(struct device *dev, int dev_no, int dev_type,
664 struct virtpci_driver *virtpcidrv)
668 struct ultra_vbus_deviceinfo dev_info;
680 chan = vbus->platform_data;
685 case PCI_DEVICE_ID_VIRTHBA:
688 case PCI_DEVICE_ID_VIRTNIC:
695 bus_device_info_init(&dev_info, stype,
699 write_vbus_dev_info(chan, &dev_info, dev_no);
701 /* Re-write bus+chipset info, because it is possible that this
702 * was previously written by our good counterpart, visorbus.
704 write_vbus_chp_info(chan, &chipset_driver_info);
705 write_vbus_bus_info(chan, &bus_driver_info);
708 /* This function is called to query the existence of a specific device
709 * and whether this driver can work with it. It should return -ENODEV
710 * in case of failure.
712 static int virtpci_device_probe(struct device *dev)
714 struct virtpci_dev *virtpcidev = device_to_virtpci_dev(dev);
715 struct virtpci_driver *virtpcidrv =
716 driver_to_virtpci_driver(dev->driver);
717 const struct pci_device_id *id;
720 POSTCODE_LINUX_2(VPCI_PROBE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
721 /* static match and static probe vs dynamic match & dynamic
722 * probe - do we care?.
724 if (!virtpcidrv->id_table)
727 id = virtpci_match_device(virtpcidrv->id_table, virtpcidev);
731 /* increment reference count */
734 /* if virtpcidev is not already claimed & probe function is
737 if (!virtpcidev->mydriver && virtpcidrv->probe) {
738 /* call the probe function - virthba or virtnic probe
739 * is what it should be
741 error = virtpcidrv->probe(virtpcidev, id);
743 fix_vbus_dev_info(dev, virtpcidev->device_no,
744 virtpcidev->device, virtpcidrv);
745 virtpcidev->mydriver = virtpcidrv;
746 POSTCODE_LINUX_2(VPCI_PROBE_EXIT_PC,
747 POSTCODE_SEVERITY_INFO);
752 POSTCODE_LINUX_2(VPCI_PROBE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
753 return error; /* -ENODEV for probe failure */
756 static int virtpci_device_remove(struct device *dev_)
758 /* dev_ passed in is the HBA device which we called
759 * generic_dev in our virtpcidev struct
761 struct virtpci_dev *virtpcidev = device_to_virtpci_dev(dev_);
762 struct virtpci_driver *virtpcidrv = virtpcidev->mydriver;
765 /* TEMP: assuming we have only one such driver for now */
766 if (virtpcidrv->remove)
767 virtpcidrv->remove(virtpcidev);
768 virtpcidev->mydriver = NULL;
775 /*****************************************************/
777 /*****************************************************/
779 static void virtpci_bus_release(struct device *dev)
783 /*****************************************************/
784 /* Adapter functions */
785 /*****************************************************/
787 /* scsi is expected to be NULL for VNIC add
788 * net is expected to be NULL for VHBA add
790 static int virtpci_device_add(struct device *parentbus, int devtype,
791 struct add_virt_guestpart *addparams,
792 struct scsi_adap_info *scsi,
793 struct net_adap_info *net)
795 struct virtpci_dev *virtpcidev = NULL;
796 struct virtpci_dev *tmpvpcidev = NULL, *prev;
799 struct spar_io_channel_protocol __iomem *io_chan = NULL;
802 POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
804 if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE)) {
805 POSTCODE_LINUX_3(VPCI_CREATE_FAILURE_PC, devtype,
806 POSTCODE_SEVERITY_ERR);
810 /* add a Virtual Device */
811 virtpcidev = kzalloc(sizeof(*virtpcidev), GFP_ATOMIC);
813 POSTCODE_LINUX_2(MALLOC_FAILURE_PC, POSTCODE_SEVERITY_ERR);
817 /* initialize stuff unique to virtpci_dev struct */
818 virtpcidev->devtype = devtype;
819 if (devtype == VIRTHBA_TYPE) {
820 virtpcidev->device = PCI_DEVICE_ID_VIRTHBA;
821 virtpcidev->scsi = *scsi;
823 virtpcidev->device = PCI_DEVICE_ID_VIRTNIC;
824 virtpcidev->net = *net;
826 virtpcidev->vendor = PCI_VENDOR_ID_UNISYS;
827 virtpcidev->bus_no = addparams->bus_no;
828 virtpcidev->device_no = addparams->device_no;
830 virtpcidev->queueinfo.chan = addparams->chanptr;
831 virtpcidev->queueinfo.send_int_if_needed = NULL;
833 /* Set up safe queue... */
834 io_chan = (struct spar_io_channel_protocol __iomem *)
835 virtpcidev->queueinfo.chan;
837 virtpcidev->intr = addparams->intr;
839 /* initialize stuff in the device portion of the struct */
840 virtpcidev->generic_dev.bus = &virtpci_bus_type;
841 virtpcidev->generic_dev.parent = parentbus;
842 virtpcidev->generic_dev.release = virtpci_device_release;
844 dev_set_name(&virtpcidev->generic_dev, "%x:%x",
845 addparams->bus_no, addparams->device_no);
847 /* add the vhba/vnic to virtpci device list - but check for
848 * duplicate wwnn/macaddr first
850 write_lock_irqsave(&vpcidev_list_lock, flags);
851 for (tmpvpcidev = vpcidev_list_head; tmpvpcidev;
852 tmpvpcidev = tmpvpcidev->next) {
853 if (devtype == VIRTHBA_TYPE) {
854 if ((tmpvpcidev->scsi.wwnn.wwnn1 == scsi->wwnn.wwnn1) &&
855 (tmpvpcidev->scsi.wwnn.wwnn2 == scsi->wwnn.wwnn2)) {
856 /* duplicate - already have vpcidev
862 (tmpvpcidev->net.mac_addr, net->mac_addr,
863 MAX_MACADDR_LEN) == 0) {
864 /* duplicate - already have vnic with this wwnn */
869 /* found a vhba/vnic already in the list with same
870 * wwnn or macaddr - reject add
872 write_unlock_irqrestore(&vpcidev_list_lock, flags);
874 POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
878 /* add it at the head */
879 if (!vpcidev_list_head) {
880 vpcidev_list_head = virtpcidev;
882 /* insert virtpcidev at the head of our linked list of
885 virtpcidev->next = vpcidev_list_head;
886 vpcidev_list_head = virtpcidev;
889 write_unlock_irqrestore(&vpcidev_list_lock, flags);
891 /* Must transition channel to ATTACHED state BEFORE
892 * registering the device, because polling of the channel
893 * queues can begin at any time after device_register().
895 dev = &virtpcidev->generic_dev;
896 SPAR_CHANNEL_CLIENT_TRANSITION(addparams->chanptr,
898 CHANNELCLI_ATTACHED, NULL);
900 /* don't register until device has been added to
901 * list. Otherwise, a device_unregister from this function can
902 * cause a "scheduling while atomic".
904 ret = device_register(&virtpcidev->generic_dev);
905 /* NOTE: THIS IS CALLING HOTPLUG virtpci_hotplug!!!
906 * This call to device_register results in virtpci_bus_match
907 * being called !!!!! And, if match returns success, then
908 * virtpcidev->generic_dev.driver is setup to core_driver,
909 * i.e., virtpci and the probe function
910 * virtpcidev->generic_dev.driver->probe is called which
911 * results in virtpci_device_probe being called. And if
912 * virtpci_device_probe is successful
915 dev = &virtpcidev->generic_dev;
916 SPAR_CHANNEL_CLIENT_TRANSITION(addparams->chanptr,
918 CHANNELCLI_DETACHED, NULL);
919 /* remove virtpcidev, the one we just added, from the list */
920 write_lock_irqsave(&vpcidev_list_lock, flags);
921 for (tmpvpcidev = vpcidev_list_head, prev = NULL;
923 prev = tmpvpcidev, tmpvpcidev = tmpvpcidev->next) {
924 if (tmpvpcidev == virtpcidev) {
926 prev->next = tmpvpcidev->next;
928 vpcidev_list_head = tmpvpcidev->next;
932 write_unlock_irqrestore(&vpcidev_list_lock, flags);
937 POSTCODE_LINUX_2(VPCI_CREATE_EXIT_PC, POSTCODE_SEVERITY_INFO);
941 static int virtpci_device_serverdown(struct device *parentbus,
943 struct vhba_wwnn *wwnn,
944 unsigned char macaddr[])
946 int pausethisone = 0;
948 struct virtpci_dev *tmpvpcidev, *prevvpcidev;
949 struct virtpci_driver *vpcidriver;
953 if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE))
956 /* find the vhba or vnic in virtpci device list */
957 write_lock_irqsave(&vpcidev_list_lock, flags);
959 for (tmpvpcidev = vpcidev_list_head, prevvpcidev = NULL;
960 (tmpvpcidev && !found);
961 prevvpcidev = tmpvpcidev, tmpvpcidev = tmpvpcidev->next) {
962 if (tmpvpcidev->devtype != devtype)
965 if (devtype == VIRTHBA_TYPE) {
967 ((tmpvpcidev->scsi.wwnn.wwnn1 == wwnn->wwnn1) &&
968 (tmpvpcidev->scsi.wwnn.wwnn2 == wwnn->wwnn2));
969 /* devtype is vhba, we're pausing vhba whose
970 * wwnn matches the current device's wwnn
972 } else { /* VIRTNIC_TYPE */
974 memcmp(tmpvpcidev->net.mac_addr, macaddr,
975 MAX_MACADDR_LEN) == 0;
976 /* devtype is vnic, we're pausing vnic whose
977 * macaddr matches the current device's macaddr */
984 vpcidriver = tmpvpcidev->mydriver;
985 rc = vpcidriver->suspend(tmpvpcidev, 0);
987 write_unlock_irqrestore(&vpcidev_list_lock, flags);
995 static int virtpci_device_serverup(struct device *parentbus,
997 struct vhba_wwnn *wwnn,
998 unsigned char macaddr[])
1000 int resumethisone = 0;
1002 struct virtpci_dev *tmpvpcidev, *prevvpcidev;
1003 struct virtpci_driver *vpcidriver;
1004 unsigned long flags;
1007 if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE))
1011 /* find the vhba or vnic in virtpci device list */
1012 write_lock_irqsave(&vpcidev_list_lock, flags);
1014 for (tmpvpcidev = vpcidev_list_head, prevvpcidev = NULL;
1015 (tmpvpcidev && !found);
1016 prevvpcidev = tmpvpcidev, tmpvpcidev = tmpvpcidev->next) {
1017 if (tmpvpcidev->devtype != devtype)
1020 if (devtype == VIRTHBA_TYPE) {
1022 ((tmpvpcidev->scsi.wwnn.wwnn1 == wwnn->wwnn1) &&
1023 (tmpvpcidev->scsi.wwnn.wwnn2 == wwnn->wwnn2));
1024 /* devtype is vhba, we're resuming vhba whose
1025 * wwnn matches the current device's wwnn */
1026 } else { /* VIRTNIC_TYPE */
1028 memcmp(tmpvpcidev->net.mac_addr, macaddr,
1029 MAX_MACADDR_LEN) == 0;
1030 /* devtype is vnic, we're resuming vnic whose
1031 * macaddr matches the current device's macaddr */
1038 vpcidriver = tmpvpcidev->mydriver;
1039 /* This should be done at BUS resume time, but an
1040 * existing problem prevents us from ever getting a bus
1041 * resume... This hack would fail to work should we
1042 * ever have a bus that contains NO devices, since we
1043 * would never even get here in that case.
1045 fix_vbus_dev_info(&tmpvpcidev->generic_dev,
1046 tmpvpcidev->device_no,
1047 tmpvpcidev->device, vpcidriver);
1048 rc = vpcidriver->resume(tmpvpcidev);
1051 write_unlock_irqrestore(&vpcidev_list_lock, flags);
1059 static int virtpci_device_del(struct device *parentbus,
1060 int devtype, struct vhba_wwnn *wwnn,
1061 unsigned char macaddr[])
1063 int count = 0, all = 0, delthisone;
1064 struct virtpci_dev *tmpvpcidev, *prevvpcidev, *dellist = NULL;
1065 unsigned long flags;
1067 #define DEL_CONTINUE { \
1068 prevvpcidev = tmpvpcidev;\
1069 tmpvpcidev = tmpvpcidev->next;\
1073 if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE))
1076 /* see if we are to delete all - NOTE: all implies we have a
1079 all = ((devtype == VIRTHBA_TYPE) && (!wwnn)) ||
1080 ((devtype == VIRTNIC_TYPE) && (!macaddr));
1082 /* find all the vhba or vnic or both in virtpci device list
1083 * keep list of ones we are deleting so we can call
1084 * device_unregister after we release the lock; otherwise we
1085 * encounter "schedule while atomic"
1087 write_lock_irqsave(&vpcidev_list_lock, flags);
1088 for (tmpvpcidev = vpcidev_list_head, prevvpcidev = NULL; tmpvpcidev;) {
1089 if (tmpvpcidev->devtype != devtype)
1094 (tmpvpcidev->generic_dev.parent == parentbus);
1095 /* we're deleting all vhbas or vnics on the
1096 * specified parent bus
1098 } else if (devtype == VIRTHBA_TYPE) {
1100 ((tmpvpcidev->scsi.wwnn.wwnn1 == wwnn->wwnn1) &&
1101 (tmpvpcidev->scsi.wwnn.wwnn2 == wwnn->wwnn2));
1102 /* devtype is vhba, we're deleting vhba whose
1103 * wwnn matches the current device's wwnn
1105 } else { /* VIRTNIC_TYPE */
1107 memcmp(tmpvpcidev->net.mac_addr, macaddr,
1108 MAX_MACADDR_LEN) == 0;
1109 /* devtype is vnic, we're deleting vnic whose
1110 * macaddr matches the current device's macaddr
1117 /* take vhba/vnic out of the list */
1120 prevvpcidev->next = tmpvpcidev->next;
1122 vpcidev_list_head = tmpvpcidev->next;
1124 /* add it to our deletelist */
1125 tmpvpcidev->next = dellist;
1126 dellist = tmpvpcidev;
1131 /* going to top of loop again - set tmpvpcidev to next
1132 * one we're to process
1135 tmpvpcidev = prevvpcidev->next;
1137 tmpvpcidev = vpcidev_list_head;
1139 write_unlock_irqrestore(&vpcidev_list_lock, flags);
1141 if (!all && (count == 0))
1144 /* now delete each one from delete list */
1147 tmpvpcidev = dellist->next;
1148 /* delete the vhba/vnic at dellist */
1149 DELETE_ONE_VPCIDEV(dellist);
1151 dellist = tmpvpcidev;
1157 static void virtpci_device_release(struct device *dev_)
1159 /* this function is called when the last reference to the
1164 /*****************************************************/
1165 /* Driver functions */
1166 /*****************************************************/
1168 #define kobj_to_device_driver(obj) container_of(obj, struct device_driver, kobj)
1169 #define attribute_to_driver_attribute(obj) \
1170 container_of(obj, struct driver_attribute, attr)
1172 static ssize_t virtpci_driver_attr_show(struct kobject *kobj,
1173 struct attribute *attr,
1176 struct driver_attribute *dattr = attribute_to_driver_attribute(attr);
1179 struct driver_private *dprivate = to_driver(kobj);
1180 struct device_driver *driver = dprivate->driver;
1183 ret = dattr->show(driver, buf);
1188 static ssize_t virtpci_driver_attr_store(struct kobject *kobj,
1189 struct attribute *attr,
1190 const char *buf, size_t count)
1192 struct driver_attribute *dattr = attribute_to_driver_attribute(attr);
1195 struct driver_private *dprivate = to_driver(kobj);
1196 struct device_driver *driver = dprivate->driver;
1199 ret = dattr->store(driver, buf, count);
1204 /* register a new virtpci driver */
1205 int virtpci_register_driver(struct virtpci_driver *drv)
1211 /* initialize core driver fields needed to call driver_register */
1212 drv->core_driver.name = drv->name; /* name of driver in sysfs */
1213 drv->core_driver.bus = &virtpci_bus_type; /* type of bus this
1214 * driver works with */
1215 drv->core_driver.probe = virtpci_device_probe; /* called to query the
1217 * specific device and
1218 * whether this driver
1219 *can work with it */
1220 drv->core_driver.remove = virtpci_device_remove; /* called when the
1222 * from the system */
1223 /* register with core */
1224 result = driver_register(&drv->core_driver);
1225 /* calls bus_add_driver which calls driver_attach and
1229 return result; /* failed */
1231 drv->core_driver.p->kobj.ktype = &virtpci_driver_kobj_type;
1235 EXPORT_SYMBOL_GPL(virtpci_register_driver);
1237 void virtpci_unregister_driver(struct virtpci_driver *drv)
1239 driver_unregister(&drv->core_driver);
1240 /* driver_unregister calls bus_remove_driver
1241 * bus_remove_driver calls device_detach
1242 * device_detach calls device_release_driver for each of the
1244 * device_release driver calls drv->remove which is
1245 * virtpci_device_remove
1246 * virtpci_device_remove calls virthba_remove
1249 EXPORT_SYMBOL_GPL(virtpci_unregister_driver);
1251 /*****************************************************/
1252 /* debugfs filesystem functions */
1253 /*****************************************************/
1254 struct print_vbus_info {
1260 static int print_vbus(struct device *vbus, void *data)
1262 struct print_vbus_info *p = (struct print_vbus_info *)data;
1264 *p->str_pos += scnprintf(p->buf + *p->str_pos, *p->len - *p->str_pos,
1265 "bus_id:%s\n", dev_name(vbus));
1269 static ssize_t info_debugfs_read(struct file *file, char __user *buf,
1270 size_t len, loff_t *offset)
1272 ssize_t bytes_read = 0;
1274 struct virtpci_dev *tmpvpcidev;
1275 unsigned long flags;
1276 struct print_vbus_info printparam;
1281 vbuf = kzalloc(len, GFP_KERNEL);
1285 str_pos += scnprintf(vbuf + str_pos, len - str_pos,
1286 " Virtual PCI Bus devices\n");
1287 printparam.str_pos = &str_pos;
1288 printparam.buf = vbuf;
1289 printparam.len = &len;
1290 bus_for_each_dev(&virtpci_bus_type, NULL, (void *)&printparam,
1293 str_pos += scnprintf(vbuf + str_pos, len - str_pos,
1294 "\n Virtual PCI devices\n");
1295 read_lock_irqsave(&vpcidev_list_lock, flags);
1296 tmpvpcidev = vpcidev_list_head;
1297 while (tmpvpcidev) {
1298 if (tmpvpcidev->devtype == VIRTHBA_TYPE) {
1299 str_pos += scnprintf(vbuf + str_pos, len - str_pos,
1300 "[%d:%d] VHba:%08x:%08x max-config:%d-%d-%d-%d",
1302 tmpvpcidev->device_no,
1303 tmpvpcidev->scsi.wwnn.wwnn1,
1304 tmpvpcidev->scsi.wwnn.wwnn2,
1305 tmpvpcidev->scsi.max.max_channel,
1306 tmpvpcidev->scsi.max.max_id,
1307 tmpvpcidev->scsi.max.max_lun,
1308 tmpvpcidev->scsi.max.cmd_per_lun);
1310 str_pos += scnprintf(vbuf + str_pos, len - str_pos,
1311 "[%d:%d] VNic:%pM num_rcv_bufs:%d mtu:%d",
1313 tmpvpcidev->device_no,
1314 tmpvpcidev->net.mac_addr,
1315 tmpvpcidev->net.num_rcv_bufs,
1316 tmpvpcidev->net.mtu);
1318 str_pos += scnprintf(vbuf + str_pos,
1319 len - str_pos, " chanptr:%p\n",
1320 tmpvpcidev->queueinfo.chan);
1321 tmpvpcidev = tmpvpcidev->next;
1323 read_unlock_irqrestore(&vpcidev_list_lock, flags);
1325 str_pos += scnprintf(vbuf + str_pos, len - str_pos, "\n");
1326 bytes_read = simple_read_from_buffer(buf, len, offset, vbuf, str_pos);
1331 /*****************************************************/
1332 /* Module Init & Exit functions */
1333 /*****************************************************/
1335 static int __init virtpci_mod_init(void)
1339 if (!unisys_spar_platform)
1342 POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1344 ret = bus_register(&virtpci_bus_type);
1345 /* creates /sys/bus/uisvirtpci which contains devices &
1349 POSTCODE_LINUX_3(VPCI_CREATE_FAILURE_PC, ret,
1350 POSTCODE_SEVERITY_ERR);
1353 bus_device_info_init(&bus_driver_info, "clientbus", "virtpci",
1356 /* create a root bus used to parent all the virtpci buses. */
1357 ret = device_register(&virtpci_rootbus_device);
1359 bus_unregister(&virtpci_bus_type);
1360 POSTCODE_LINUX_3(VPCI_CREATE_FAILURE_PC, ret,
1361 POSTCODE_SEVERITY_ERR);
1365 if (!uisctrl_register_req_handler(2, (void *)&virtpci_ctrlchan_func,
1366 &chipset_driver_info)) {
1367 POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
1368 device_unregister(&virtpci_rootbus_device);
1369 bus_unregister(&virtpci_bus_type);
1373 /* create debugfs directory and info file inside. */
1374 virtpci_debugfs_dir = debugfs_create_dir("virtpci", NULL);
1375 debugfs_create_file("info", S_IRUSR, virtpci_debugfs_dir,
1376 NULL, &debugfs_info_fops);
1377 POSTCODE_LINUX_2(VPCI_CREATE_EXIT_PC, POSTCODE_SEVERITY_INFO);
1381 static void __exit virtpci_mod_exit(void)
1383 /* unregister the callback function */
1384 device_unregister(&virtpci_rootbus_device);
1385 bus_unregister(&virtpci_bus_type);
1386 debugfs_remove_recursive(virtpci_debugfs_dir);
1389 module_init(virtpci_mod_init);
1390 module_exit(virtpci_mod_exit);
1391 MODULE_LICENSE("GPL");
1392 MODULE_AUTHOR("Usha Srinivasan");
1393 MODULE_ALIAS("uisvirtpci");