virtpci.c revision 1d2def986df26bc8ad96e4a824e149dd5fc0e054
1/* virtpci.c
2 *
3 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
4 * All rights reserved.
5 *
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.
10 *
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
15 * details.
16 */
17
18#define EXPORT_SYMTAB
19
20#include <linux/kernel.h>
21#ifdef CONFIG_MODVERSIONS
22#include <config/modversions.h>
23#endif
24#include "uniklog.h"
25#include "diagnostics/appos_subsystems.h"
26#include "uisutils.h"
27#include "vbuschannel.h"
28#include "vbushelper.h"
29#include <linux/types.h>
30#include <linux/io.h>
31#include <linux/uuid.h>
32#include <linux/module.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/device.h>
36#include <linux/list.h>
37#include <linux/slab.h>
38#include <linux/mod_devicetable.h>
39#include <linux/if_ether.h>
40#include <linux/version.h>
41#include <linux/debugfs.h>
42#include "version.h"
43#include "guestlinuxdebug.h"
44#include "timskmodutils.h"
45
46struct driver_private {
47	struct kobject kobj;
48	struct klist klist_devices;
49	struct klist_node knode_bus;
50	struct module_kobject *mkobj;
51	struct device_driver *driver;
52};
53#define to_driver(obj) container_of(obj, struct driver_private, kobj)
54
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.
57 */
58#ifndef BUS_ID_SIZE
59#define BUS_ID_SIZE 20
60#endif
61
62#define BUS_ID(x) dev_name(x)
63
64/* MAX_BUF = 4 busses x ( 32 devices/bus + 1 busline) x 80 characters
65 *         = 10,560 bytes ~ 2^14 = 16,384 bytes
66 */
67#define MAX_BUF 16384
68
69#include "virtpci.h"
70
71/* this is shorter than using __FILE__ (full path name) in
72 * debug/info/error messages
73 */
74#define CURRENT_FILE_PC VIRT_PCI_PC_virtpci_c
75#define __MYFILE__ "virtpci.c"
76
77#define VIRTPCI_VERSION "01.00"
78
79/*****************************************************/
80/* Forward declarations                              */
81/*****************************************************/
82
83static int delete_vbus_device(struct device *vbus, void *data);
84static int match_busid(struct device *dev, void *data);
85static void virtpci_bus_release(struct device *dev);
86static void virtpci_device_release(struct device *dev);
87static 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);
91static int virtpci_device_del(struct device *parentbus, int devtype,
92			      struct vhba_wwnn *wwnn, unsigned char macaddr[]);
93static int virtpci_device_serverdown(struct device *parentbus, int devtype,
94				     struct vhba_wwnn *wwnn,
95				     unsigned char macaddr[]);
96static int virtpci_device_serverup(struct device *parentbus, int devtype,
97				   struct vhba_wwnn *wwnn,
98				   unsigned char macaddr[]);
99static ssize_t virtpci_driver_attr_show(struct kobject *kobj,
100					struct attribute *attr, char *buf);
101static ssize_t virtpci_driver_attr_store(struct kobject *kobj,
102					 struct attribute *attr,
103					 const char *buf, size_t count);
104static int virtpci_bus_match(struct device *dev, struct device_driver *drv);
105static int virtpci_uevent(struct device *dev, struct kobj_uevent_env *env);
106static int virtpci_device_suspend(struct device *dev, pm_message_t state);
107static int virtpci_device_resume(struct device *dev);
108static int virtpci_device_probe(struct device *dev);
109static int virtpci_device_remove(struct device *dev);
110
111static ssize_t info_debugfs_read(struct file *file, char __user *buf,
112			      size_t len, loff_t *offset);
113
114static const struct file_operations debugfs_info_fops = {
115	.read = info_debugfs_read,
116};
117
118/*****************************************************/
119/* Globals                                           */
120/*****************************************************/
121
122/* methods in bus_type struct allow the bus code to serve as an
123 * intermediary between the device core and individual device core and
124 * individual drivers
125 */
126static struct bus_type virtpci_bus_type = {
127	.name = "uisvirtpci",
128	.match = virtpci_bus_match,
129	.uevent = virtpci_uevent,
130	.suspend = virtpci_device_suspend,
131	.resume = virtpci_device_resume,
132};
133
134static struct device virtpci_rootbus_device = {
135	.init_name = "vbusroot",	/* root bus */
136	.release = virtpci_bus_release
137};
138
139/* filled in with info about parent chipset driver when we register with it */
140static ULTRA_VBUS_DEVICEINFO Chipset_DriverInfo;
141
142static const struct sysfs_ops virtpci_driver_sysfs_ops = {
143	.show = virtpci_driver_attr_show,
144	.store = virtpci_driver_attr_store,
145};
146
147static struct kobj_type virtpci_driver_kobj_type = {
148	.sysfs_ops = &virtpci_driver_sysfs_ops,
149};
150
151static struct virtpci_dev *VpcidevListHead;
152static DEFINE_RWLOCK(VpcidevListLock);
153
154/* filled in with info about this driver, wrt it servicing client busses */
155static ULTRA_VBUS_DEVICEINFO Bus_DriverInfo;
156
157/*****************************************************/
158/* debugfs entries                                   */
159/*****************************************************/
160/* dentry is used to create the debugfs entry directory
161 * for virtpci
162 */
163static struct dentry *virtpci_debugfs_dir;
164
165struct virtpci_busdev {
166	struct device virtpci_bus_device;
167};
168
169/*****************************************************/
170/* Local functions                                   */
171/*****************************************************/
172
173static inline
174int WAIT_FOR_IO_CHANNEL(ULTRA_IO_CHANNEL_PROTOCOL __iomem  *chanptr)
175{
176	int count = 120;
177	while (count > 0) {
178
179		if (ULTRA_CHANNEL_SERVER_READY(&chanptr->ChannelHeader))
180			return 1;
181		UIS_THREAD_WAIT_SEC(1);
182		count--;
183	}
184	return 0;
185}
186
187/* Write the contents of <info> to the ULTRA_VBUS_CHANNEL_PROTOCOL.ChpInfo. */
188static int write_vbus_chpInfo(ULTRA_VBUS_CHANNEL_PROTOCOL *chan,
189			      ULTRA_VBUS_DEVICEINFO *info)
190{
191	int off;
192	if (!chan) {
193		LOGERR("vbus channel not present");
194		return -1;
195	}
196	off = sizeof(ULTRA_CHANNEL_PROTOCOL) + chan->HdrInfo.chpInfoByteOffset;
197	if (chan->HdrInfo.chpInfoByteOffset == 0) {
198		LOGERR("vbus channel not used, because chpInfoByteOffset == 0");
199		return -1;
200	}
201	memcpy(((u8 *) (chan)) + off, info, sizeof(*info));
202	return 0;
203}
204
205/* Write the contents of <info> to the ULTRA_VBUS_CHANNEL_PROTOCOL.BusInfo. */
206static int write_vbus_busInfo(ULTRA_VBUS_CHANNEL_PROTOCOL *chan,
207			      ULTRA_VBUS_DEVICEINFO *info)
208{
209	int off;
210	if (!chan) {
211		LOGERR("vbus channel not present");
212		return -1;
213	}
214	off = sizeof(ULTRA_CHANNEL_PROTOCOL) + chan->HdrInfo.busInfoByteOffset;
215	if (chan->HdrInfo.busInfoByteOffset == 0) {
216		LOGERR("vbus channel not used, because busInfoByteOffset == 0");
217		return -1;
218	}
219	memcpy(((u8 *) (chan)) + off, info, sizeof(*info));
220	return 0;
221}
222
223/* Write the contents of <info> to the
224 * ULTRA_VBUS_CHANNEL_PROTOCOL.DevInfo[<devix>].
225 */
226static int
227write_vbus_devInfo(ULTRA_VBUS_CHANNEL_PROTOCOL *chan,
228		   ULTRA_VBUS_DEVICEINFO *info, int devix)
229{
230	int off;
231	if (!chan) {
232		LOGERR("vbus channel not present");
233		return -1;
234	}
235	off =
236	    (sizeof(ULTRA_CHANNEL_PROTOCOL) +
237	     chan->HdrInfo.devInfoByteOffset) +
238	    (chan->HdrInfo.deviceInfoStructBytes * devix);
239	if (chan->HdrInfo.devInfoByteOffset == 0) {
240		LOGERR("vbus channel not used, because devInfoByteOffset == 0");
241		return -1;
242	}
243	memcpy(((u8 *) (chan)) + off, info, sizeof(*info));
244	return 0;
245}
246
247/* adds a vbus
248 * returns 0 failure, 1 success,
249 */
250static int add_vbus(struct add_vbus_guestpart *addparams)
251{
252	int ret;
253	struct device *vbus;
254	vbus = kzalloc(sizeof(struct device), GFP_ATOMIC);
255
256	POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
257	if (!vbus)
258		return 0;
259
260	dev_set_name(vbus, "vbus%d", addparams->busNo);
261	vbus->release = virtpci_bus_release;
262	vbus->parent = &virtpci_rootbus_device;	/* root bus is parent */
263	vbus->bus = &virtpci_bus_type;	/* bus type */
264	vbus->platform_data = (__force void *)addparams->chanptr;
265
266	/* register a virt bus device -
267	 * this bus shows up under /sys/devices with .name value
268	 * "virtpci%d" any devices added to this bus then show up under
269	 * /sys/devices/virtpci0
270	 */
271	ret = device_register(vbus);
272	if (ret) {
273		LOGERR("device_register FAILED:%d\n", ret);
274		POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
275		return 0;
276	}
277	write_vbus_chpInfo(vbus->platform_data /* chanptr */ ,
278			   &Chipset_DriverInfo);
279	write_vbus_busInfo(vbus->platform_data /* chanptr */ , &Bus_DriverInfo);
280	LOGINF("Added vbus %d; device %s created successfully\n",
281	       addparams->busNo, BUS_ID(vbus));
282	POSTCODE_LINUX_2(VPCI_CREATE_EXIT_PC, POSTCODE_SEVERITY_INFO);
283	return 1;
284}
285
286/* for CHANSOCK wwwnn/max are AUTO-GENERATED; for normal channels,
287 * wwnn/max are in the channel header.
288 */
289#define GET_SCSIADAPINFO_FROM_CHANPTR(chanptr) {			\
290	memcpy_fromio(&scsi.wwnn,					\
291		      &((ULTRA_IO_CHANNEL_PROTOCOL __iomem *)		\
292			chanptr)->vhba.wwnn,				\
293		      sizeof(struct vhba_wwnn));			\
294	memcpy_fromio(&scsi.max,					\
295		      &((ULTRA_IO_CHANNEL_PROTOCOL __iomem *)		\
296			chanptr)->vhba.max,				\
297		      sizeof(struct vhba_config_max));			\
298	}
299
300/* find bus device with the busid that matches - match_busid matches bus_id */
301#define GET_BUS_DEV(busno) { \
302	sprintf(busid, "vbus%d", busno); \
303	vbus = bus_find_device(&virtpci_bus_type, NULL, \
304			       (void *)busid, match_busid);	\
305	if (!vbus) { \
306		LOGERR("**** FAILED to find vbus %s\n", busid); \
307		return 0; \
308	} \
309}
310
311/* adds a vhba
312 * returns 0 failure, 1 success,
313 */
314static int add_vhba(struct add_virt_guestpart *addparams)
315{
316	int i;
317	struct scsi_adap_info scsi;
318	struct device *vbus;
319	unsigned char busid[BUS_ID_SIZE];
320
321	POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
322	if (!WAIT_FOR_IO_CHANNEL
323	    ((ULTRA_IO_CHANNEL_PROTOCOL __iomem *) addparams->chanptr)) {
324		LOGERR("Timed out.  Channel not ready\n");
325		POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
326		return 0;
327	}
328
329	GET_SCSIADAPINFO_FROM_CHANPTR(addparams->chanptr);
330
331	GET_BUS_DEV(addparams->busNo);
332
333	LOGINF("Adding vhba wwnn:%x:%x config:%d-%d-%d-%d chanptr:%p\n",
334	       scsi.wwnn.wwnn1, scsi.wwnn.wwnn2,
335	       scsi.max.max_channel, scsi.max.max_id, scsi.max.max_lun,
336	       scsi.max.cmd_per_lun, addparams->chanptr);
337	i = virtpci_device_add(vbus, VIRTHBA_TYPE, addparams, &scsi, NULL);
338	if (i) {
339		LOGINF("Added vhba wwnn:%x:%x chanptr:%p\n", scsi.wwnn.wwnn1,
340		       scsi.wwnn.wwnn2, addparams->chanptr);
341		POSTCODE_LINUX_3(VPCI_CREATE_EXIT_PC, i,
342				 POSTCODE_SEVERITY_INFO);
343	}
344	return i;
345
346}
347
348/* for CHANSOCK macaddr is AUTO-GENERATED; for normal channels,
349 * macaddr is in the channel header.
350 */
351#define GET_NETADAPINFO_FROM_CHANPTR(chanptr) {				\
352		memcpy_fromio(net.mac_addr,				\
353		       ((ULTRA_IO_CHANNEL_PROTOCOL __iomem *)		\
354			chanptr)->vnic.macaddr,				\
355		       MAX_MACADDR_LEN);				\
356		net.num_rcv_bufs =					\
357			readl(&((ULTRA_IO_CHANNEL_PROTOCOL __iomem *)	\
358				chanptr)->vnic.num_rcv_bufs);		\
359		net.mtu = readl(&((ULTRA_IO_CHANNEL_PROTOCOL __iomem *) \
360				  chanptr)->vnic.mtu);			\
361		memcpy_fromio(&net.zoneGuid, \
362			      &((ULTRA_IO_CHANNEL_PROTOCOL __iomem *)	\
363				chanptr)->vnic.zoneGuid,		\
364			      sizeof(uuid_le));				\
365}
366
367/* adds a vnic
368 * returns 0 failure, 1 success,
369 */
370static int
371add_vnic(struct add_virt_guestpart *addparams)
372{
373	int i;
374	struct net_adap_info net;
375	struct device *vbus;
376	unsigned char busid[BUS_ID_SIZE];
377
378	POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
379	if (!WAIT_FOR_IO_CHANNEL
380	    ((ULTRA_IO_CHANNEL_PROTOCOL __iomem *) addparams->chanptr)) {
381		LOGERR("Timed out, channel not ready\n");
382		POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
383		return 0;
384	}
385
386	GET_NETADAPINFO_FROM_CHANPTR(addparams->chanptr);
387
388	GET_BUS_DEV(addparams->busNo);
389
390	LOGINF("Adding vnic macaddr:%02x:%02x:%02x:%02x:%02x:%02x rcvbufs:%d mtu:%d chanptr:%p%pUL\n",
391	     net.mac_addr[0], net.mac_addr[1], net.mac_addr[2], net.mac_addr[3],
392	     net.mac_addr[4], net.mac_addr[5], net.num_rcv_bufs, net.mtu,
393	     addparams->chanptr, &net.zoneGuid);
394	i = virtpci_device_add(vbus, VIRTNIC_TYPE, addparams, NULL, &net);
395	if (i) {
396		LOGINF("Added vnic macaddr:%02x:%02x:%02x:%02x:%02x:%02x\n",
397		       net.mac_addr[0], net.mac_addr[1], net.mac_addr[2],
398		       net.mac_addr[3], net.mac_addr[4], net.mac_addr[5]);
399		POSTCODE_LINUX_3(VPCI_CREATE_EXIT_PC, i,
400				 POSTCODE_SEVERITY_INFO);
401		return 1;
402	}
403	return 0;
404}
405
406/* delete vbus
407 * returns 0 failure, 1 success,
408 */
409static int
410delete_vbus(struct del_vbus_guestpart *delparams)
411{
412	struct device *vbus;
413	unsigned char busid[BUS_ID_SIZE];
414
415	GET_BUS_DEV(delparams->busNo);
416	/* ensure that bus has no devices? -- TBD */
417	LOGINF("Deleting %s\n", BUS_ID(vbus));
418	if (delete_vbus_device(vbus, NULL))
419		return 0;	/* failure */
420	LOGINF("Deleted vbus %d\n", delparams->busNo);
421	return 1;
422}
423
424static int
425delete_vbus_device(struct device *vbus, void *data)
426{
427	int checkforroot = (data != NULL);
428	struct device *pDev = &virtpci_rootbus_device;
429
430	if ((checkforroot) && match_busid(vbus, (void *) BUS_ID(pDev))) {
431		/* skip it - don't delete root bus */
432		LOGINF("skipping root bus\n");
433		return 0;	/* pretend no error */
434	}
435	LOGINF("Calling unregister for %s\n", BUS_ID(vbus));
436	device_unregister(vbus);
437	kfree(vbus);
438	LOGINF("VBus unregister and freed\n");
439	return 0;		/* no error */
440}
441
442/* pause vhba
443* returns 0 failure, 1 success,
444*/
445static int pause_vhba(struct pause_virt_guestpart *pauseparams)
446{
447	int i;
448	struct scsi_adap_info scsi;
449
450	GET_SCSIADAPINFO_FROM_CHANPTR(pauseparams->chanptr);
451
452	LOGINF("Pausing vhba wwnn:%x:%x\n", scsi.wwnn.wwnn1, scsi.wwnn.wwnn2);
453	i = virtpci_device_serverdown(NULL /*no parent bus */ , VIRTHBA_TYPE,
454				      &scsi.wwnn, NULL);
455	if (i)
456		LOGINF("Paused vhba wwnn:%x:%x\n", scsi.wwnn.wwnn1,
457		       scsi.wwnn.wwnn2);
458	return i;
459}
460
461/* pause vnic
462 * returns 0 failure, 1 success,
463 */
464static int pause_vnic(struct pause_virt_guestpart *pauseparams)
465{
466	int i;
467	struct net_adap_info net;
468
469	GET_NETADAPINFO_FROM_CHANPTR(pauseparams->chanptr);
470
471	LOGINF("Pausing vnic macaddr:%02x:%02x:%02x:%02x:%02x:%02x\n",
472	       net.mac_addr[0], net.mac_addr[1], net.mac_addr[2],
473	       net.mac_addr[3], net.mac_addr[4], net.mac_addr[5]);
474	i = virtpci_device_serverdown(NULL /*no parent bus */ , VIRTNIC_TYPE,
475				      NULL, net.mac_addr);
476	if (i) {
477		LOGINF(" Paused vnic macaddr:%02x:%02x:%02x:%02x:%02x:%02x\n",
478		       net.mac_addr[0], net.mac_addr[1], net.mac_addr[2],
479		       net.mac_addr[3], net.mac_addr[4], net.mac_addr[5]);
480	}
481	return i;
482}
483
484/* resume vhba
485 * returns 0 failure, 1 success,
486 */
487static int resume_vhba(struct resume_virt_guestpart *resumeparams)
488{
489	int i;
490	struct scsi_adap_info scsi;
491
492	GET_SCSIADAPINFO_FROM_CHANPTR(resumeparams->chanptr);
493
494	LOGINF("Resuming vhba wwnn:%x:%x\n", scsi.wwnn.wwnn1, scsi.wwnn.wwnn2);
495	i = virtpci_device_serverup(NULL /*no parent bus */ , VIRTHBA_TYPE,
496				    &scsi.wwnn, NULL);
497	if (i)
498		LOGINF("Resumed vhba wwnn:%x:%x\n", scsi.wwnn.wwnn1,
499		       scsi.wwnn.wwnn2);
500	return i;
501}
502
503/* resume vnic
504* returns 0 failure, 1 success,
505*/
506static int
507resume_vnic(struct resume_virt_guestpart *resumeparams)
508{
509	int i;
510	struct net_adap_info net;
511
512	GET_NETADAPINFO_FROM_CHANPTR(resumeparams->chanptr);
513
514	LOGINF("Resuming vnic macaddr:%02x:%02x:%02x:%02x:%02x:%02x\n",
515	       net.mac_addr[0], net.mac_addr[1], net.mac_addr[2],
516	       net.mac_addr[3], net.mac_addr[4], net.mac_addr[5]);
517	i = virtpci_device_serverup(NULL /*no parent bus */ , VIRTNIC_TYPE,
518				    NULL, net.mac_addr);
519	if (i) {
520		LOGINF(" Resumed vnic macaddr:%02x:%02x:%02x:%02x:%02x:%02x\n",
521		       net.mac_addr[0], net.mac_addr[1], net.mac_addr[2],
522		       net.mac_addr[3], net.mac_addr[4], net.mac_addr[5]);
523	}
524	return i;
525}
526
527/* delete vhba
528* returns 0 failure, 1 success,
529*/
530static int delete_vhba(struct del_virt_guestpart *delparams)
531{
532	int i;
533	struct scsi_adap_info scsi;
534
535	GET_SCSIADAPINFO_FROM_CHANPTR(delparams->chanptr);
536
537	LOGINF("Deleting vhba wwnn:%x:%x\n", scsi.wwnn.wwnn1, scsi.wwnn.wwnn2);
538	i = virtpci_device_del(NULL /*no parent bus */ , VIRTHBA_TYPE,
539			       &scsi.wwnn, NULL);
540	if (i) {
541		LOGINF("Deleted vhba wwnn:%x:%x\n", scsi.wwnn.wwnn1,
542		       scsi.wwnn.wwnn2);
543		return 1;
544	}
545	return 0;
546}
547
548/* deletes a vnic
549 * returns 0 failure, 1 success,
550 */
551static int delete_vnic(struct del_virt_guestpart *delparams)
552{
553	int i;
554	struct net_adap_info net;
555
556	GET_NETADAPINFO_FROM_CHANPTR(delparams->chanptr);
557
558	LOGINF("Deleting vnic macaddr:%02x:%02x:%02x:%02x:%02x:%02x\n",
559	       net.mac_addr[0], net.mac_addr[1], net.mac_addr[2],
560	       net.mac_addr[3], net.mac_addr[4], net.mac_addr[5]);
561	i = virtpci_device_del(NULL /*no parent bus */ , VIRTNIC_TYPE, NULL,
562			       net.mac_addr);
563	if (i) {
564		LOGINF("Deleted vnic macaddr:%02x:%02x:%02x:%02x:%02x:%02x\n",
565		       net.mac_addr[0], net.mac_addr[1], net.mac_addr[2],
566		       net.mac_addr[3], net.mac_addr[4], net.mac_addr[5]);
567	}
568	return i;
569}
570
571#define DELETE_ONE_VPCIDEV(vpcidev) { \
572	LOGINF("calling device_unregister:%p\n", &vpcidev->generic_dev); \
573	device_unregister(&vpcidev->generic_dev); \
574	LOGINF("Deleted %p\n", vpcidev); \
575	kfree(vpcidev); \
576}
577
578/* deletes all vhbas and vnics
579 * returns 0 failure, 1 success,
580 */
581static void delete_all(void)
582{
583	int count = 0;
584	unsigned long flags;
585	struct virtpci_dev *tmpvpcidev, *nextvpcidev;
586
587	/* delete the entire vhba/vnic list in one shot */
588	write_lock_irqsave(&VpcidevListLock, flags);
589	tmpvpcidev = VpcidevListHead;
590	VpcidevListHead = NULL;
591	write_unlock_irqrestore(&VpcidevListLock, flags);
592
593	/* delete one vhba/vnic at a time */
594	while (tmpvpcidev) {
595		nextvpcidev = tmpvpcidev->next;
596		/* delete the vhba/vnic at tmpvpcidev */
597		DELETE_ONE_VPCIDEV(tmpvpcidev);
598		tmpvpcidev = nextvpcidev;
599		count++;
600	}
601	LOGINF("Deleted %d vhbas/vnics.\n", count);
602
603	/* now delete each vbus */
604	if (bus_for_each_dev
605	    (&virtpci_bus_type, NULL, (void *) 1, delete_vbus_device))
606		LOGERR("delete of all vbus failed\n");
607}
608
609/* deletes all vnics or vhbas
610 * returns 0 failure, 1 success,
611 */
612static int delete_all_virt(VIRTPCI_DEV_TYPE devtype, struct del_vbus_guestpart *delparams)
613{
614	int i;
615	unsigned char busid[BUS_ID_SIZE];
616	struct device *vbus;
617
618	GET_BUS_DEV(delparams->busNo);
619
620	if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE)) {
621		LOGERR("**** FAILED to delete all devices; devtype:%d not vhba:%d or vnic:%d\n",
622		     devtype, VIRTHBA_TYPE, VIRTNIC_TYPE);
623		return 0;
624	}
625
626	LOGINF("Deleting all %s in vbus %s\n",
627	       devtype == VIRTHBA_TYPE ? "vhbas" : "vnics", busid);
628	/* delete all vhbas/vnics */
629	i = virtpci_device_del(vbus, devtype, NULL, NULL);
630	if (i > 0)
631		LOGINF("Deleted %d %s\n", i,
632		       devtype == VIRTHBA_TYPE ? "vhbas" : "vnics");
633	return 1;
634}
635
636static int virtpci_ctrlchan_func(struct guest_msgs *msg)
637{
638	switch (msg->msgtype) {
639	case GUEST_ADD_VBUS:
640		return add_vbus(&msg->add_vbus);
641	case GUEST_ADD_VHBA:
642		return add_vhba(&msg->add_vhba);
643	case GUEST_ADD_VNIC:
644		return add_vnic(&msg->add_vnic);
645	case GUEST_DEL_VBUS:
646		return delete_vbus(&msg->del_vbus);
647	case GUEST_DEL_VHBA:
648		return delete_vhba(&msg->del_vhba);
649	case GUEST_DEL_VNIC:
650		return delete_vnic(&msg->del_vhba);
651	case GUEST_DEL_ALL_VHBAS:
652		return delete_all_virt(VIRTHBA_TYPE, &msg->del_all_vhbas);
653	case GUEST_DEL_ALL_VNICS:
654		return delete_all_virt(VIRTNIC_TYPE, &msg->del_all_vnics);
655	case GUEST_DEL_ALL_VBUSES:
656		delete_all();
657		return 1;
658	case GUEST_PAUSE_VHBA:
659		return pause_vhba(&msg->pause_vhba);
660	case GUEST_PAUSE_VNIC:
661		return pause_vnic(&msg->pause_vnic);
662	case GUEST_RESUME_VHBA:
663		return resume_vhba(&msg->resume_vhba);
664	case GUEST_RESUME_VNIC:
665		return resume_vnic(&msg->resume_vnic);
666	default:
667		LOGERR("invalid message type %d.\n", msg->msgtype);
668		return 0;
669	}
670}
671
672/* same as driver_helper in bus.c linux */
673static int match_busid(struct device *dev, void *data)
674{
675	const char *name = data;
676
677	if (strcmp(name, BUS_ID(dev)) == 0)
678		return 1;
679	return 0;
680}
681
682/*****************************************************/
683/*  Bus functions                                    */
684/*****************************************************/
685
686static const struct pci_device_id *
687virtpci_match_device(const struct pci_device_id *ids,
688		     const struct virtpci_dev *dev)
689{
690	while (ids->vendor || ids->subvendor || ids->class_mask) {
691		DBGINF("ids->vendor:%x dev->vendor:%x ids->device:%x dev->device:%x\n",
692		     ids->vendor, dev->vendor, ids->device, dev->device);
693
694		if ((ids->vendor == dev->vendor)
695		    && (ids->device == dev->device))
696			return ids;
697
698		ids++;
699	}
700	return NULL;
701}
702
703/* NOTE: !!!!!!  This function is called when a new device is added
704* for this bus.  Or, it is called for existing devices when a new
705* driver is added for this bus.  It returns nonzero if a given device
706* can be handled by the given driver.
707*/
708static int virtpci_bus_match(struct device *dev, struct device_driver *drv)
709{
710	struct virtpci_dev *virtpcidev = device_to_virtpci_dev(dev);
711	struct virtpci_driver *virtpcidrv = driver_to_virtpci_driver(drv);
712	int match = 0;
713
714	DBGINF("In virtpci_bus_match dev->bus_id:%s drv->name:%s\n",
715	       dev->bus_id, drv->name);
716
717	/* check ids list for a match */
718	if (virtpci_match_device(virtpcidrv->id_table, virtpcidev))
719		match = 1;
720
721	DBGINF("returning match:%d\n", match);
722	return match;		/* 0 - no match; 1 - yes it matches */
723}
724
725static int virtpci_uevent(struct device *dev, struct kobj_uevent_env *env)
726{
727	DBGINF("In virtpci_hotplug\n");
728	/* add variables to the environment prior to the generation of
729	 * hotplug events to user space
730	 */
731	if (add_uevent_var(env, "VIRTPCI_VERSION=%s", VIRTPCI_VERSION))
732		return -ENOMEM;
733	return 0;
734}
735
736static int virtpci_device_suspend(struct device *dev, pm_message_t state)
737{
738	DBGINF("In virtpci_device_suspend -NYI ****\n");
739	return 0;
740}
741
742static int virtpci_device_resume(struct device *dev)
743{
744	DBGINF("In virtpci_device_resume -NYI ****\n");
745	return 0;
746}
747
748/* For a child device just created on a client bus, fill in
749 * information about the driver that is controlling this device into
750 * the the appropriate slot within the vbus channel of the bus
751 * instance.
752 */
753static void fix_vbus_devInfo(struct device *dev, int devNo, int devType,
754			     struct virtpci_driver *virtpcidrv)
755{
756	struct device *vbus;
757	void *pChan;
758	ULTRA_VBUS_DEVICEINFO devInfo;
759	const char *stype;
760
761	if (!dev) {
762		LOGERR("%s dev is NULL", __func__);
763		return;
764	}
765	if (!virtpcidrv) {
766		LOGERR("%s driver is NULL", __func__);
767		return;
768	}
769	vbus = dev->parent;
770	if (!vbus) {
771		LOGERR("%s dev has no parent bus", __func__);
772		return;
773	}
774	pChan = vbus->platform_data;
775	if (!pChan) {
776		LOGERR("%s dev bus has no channel", __func__);
777		return;
778	}
779	switch (devType) {
780	case PCI_DEVICE_ID_VIRTHBA:
781		stype = "vHBA";
782		break;
783	case PCI_DEVICE_ID_VIRTNIC:
784		stype = "vNIC";
785		break;
786	default:
787		stype = "unknown";
788		break;
789	}
790	BusDeviceInfo_Init(&devInfo, stype,
791			   virtpcidrv->name,
792			   virtpcidrv->version,
793			   virtpcidrv->vertag);
794	write_vbus_devInfo(pChan, &devInfo, devNo);
795
796	/* Re-write bus+chipset info, because it is possible that this
797	* was previously written by our good counterpart, visorbus.
798	*/
799	write_vbus_chpInfo(pChan, &Chipset_DriverInfo);
800	write_vbus_busInfo(pChan, &Bus_DriverInfo);
801}
802
803/* This function is called to query the existence of a specific device
804* and whether this driver can work with it.  It should return -ENODEV
805* in case of failure.
806*/
807static int virtpci_device_probe(struct device *dev)
808{
809	struct virtpci_dev *virtpcidev = device_to_virtpci_dev(dev);
810	struct virtpci_driver *virtpcidrv =
811	    driver_to_virtpci_driver(dev->driver);
812	const struct pci_device_id *id;
813	int error = 0;
814
815	LOGINF("In virtpci_device_probe dev:%p virtpcidev:%p virtpcidrv:%p\n",
816	       dev, virtpcidev, virtpcidrv);	/* VERBOSE/DEBUG ? */
817	POSTCODE_LINUX_2(VPCI_PROBE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
818	/* static match and static probe vs dynamic match & dynamic
819	 * probe - do we care?.
820	 */
821	if (!virtpcidrv->id_table)
822		return -ENODEV;
823
824	id = virtpci_match_device(virtpcidrv->id_table, virtpcidev);
825	if (!id)
826		return -ENODEV;
827
828	/* increment reference count */
829	get_device(dev);
830
831	/* if virtpcidev is not already claimed & probe function is
832	 * valid, probe it
833	 */
834	if (!virtpcidev->mydriver && virtpcidrv->probe) {
835		/* call the probe function - virthba or virtnic probe
836		 * is what it should be
837		 */
838		error = virtpcidrv->probe(virtpcidev, id);
839		if (!error) {
840			fix_vbus_devInfo(dev, virtpcidev->deviceNo,
841					 virtpcidev->device, virtpcidrv);
842			virtpcidev->mydriver = virtpcidrv;
843			POSTCODE_LINUX_2(VPCI_PROBE_EXIT_PC,
844					 POSTCODE_SEVERITY_INFO);
845		} else
846			put_device(dev);
847	}
848	POSTCODE_LINUX_2(VPCI_PROBE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
849	return error;		/* -ENODEV for probe failure */
850}
851
852static int virtpci_device_remove(struct device *dev_)
853{
854	/* dev_ passed in is the HBA device which we called
855	* generic_dev in our virtpcidev struct
856	*/
857	struct virtpci_dev *virtpcidev = device_to_virtpci_dev(dev_);
858	struct virtpci_driver *virtpcidrv = virtpcidev->mydriver;
859	LOGINF("In virtpci_device_remove bus_id:%s dev_:%p virtpcidev:%p dev->driver:%p drivername:%s\n",
860	       BUS_ID(dev_), dev_, virtpcidev, dev_->driver,
861	       dev_->driver->name);	/* VERBOSE/DEBUG */
862	if (virtpcidrv) {
863		/* TEMP: assuming we have only one such driver for now */
864		if (virtpcidrv->remove)
865			virtpcidrv->remove(virtpcidev);
866		virtpcidev->mydriver = NULL;
867	}
868
869	DBGINF("calling putdevice\n");
870	put_device(dev_);
871
872	DBGINF("Leaving\n");
873	return 0;
874}
875
876/*****************************************************/
877/* Bus functions                                     */
878/*****************************************************/
879
880static void virtpci_bus_release(struct device *dev)
881{
882	/* this function is called when the last reference to the
883	 * device is removed
884	 */
885	DBGINF("In virtpci_bus_release\n");
886	/* what else is supposed to happen here? */
887}
888
889/*****************************************************/
890/* Adapter functions                                 */
891/*****************************************************/
892
893static int virtpci_device_add(struct device *parentbus, int devtype,
894			      struct add_virt_guestpart *addparams,
895			      struct scsi_adap_info *scsi, /* NULL for VNIC add */
896			      struct net_adap_info *net	/* NULL for VHBA add */)
897{
898	struct virtpci_dev *virtpcidev = NULL;
899	struct virtpci_dev *tmpvpcidev = NULL, *prev;
900	unsigned long flags;
901	int ret;
902	ULTRA_IO_CHANNEL_PROTOCOL __iomem *pIoChan = NULL;
903	struct device *pDev;
904
905	LOGINF("virtpci_device_add parentbus:%p chanptr:%p\n", parentbus,
906	       addparams->chanptr);
907
908	POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
909
910	if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE)) {
911		LOGERR("**** FAILED to add device; devtype:%d not vhba:%d or vnic:%d\n",
912		     devtype, VIRTHBA_TYPE, VIRTNIC_TYPE);
913		POSTCODE_LINUX_3(VPCI_CREATE_FAILURE_PC, devtype,
914				 POSTCODE_SEVERITY_ERR);
915		return 0;
916	}
917
918	/* add a Virtual Device */
919	virtpcidev = kzalloc(sizeof(struct virtpci_dev), GFP_ATOMIC);
920	if (virtpcidev == NULL) {
921		LOGERR("can't add device - malloc FALLED\n");
922		POSTCODE_LINUX_2(MALLOC_FAILURE_PC, POSTCODE_SEVERITY_ERR);
923		return 0;
924	}
925
926	/* initialize stuff unique to virtpci_dev struct */
927	virtpcidev->devtype = devtype;
928	if (devtype == VIRTHBA_TYPE) {
929		virtpcidev->device = PCI_DEVICE_ID_VIRTHBA;
930		virtpcidev->scsi = *scsi;
931	} else {
932		virtpcidev->device = PCI_DEVICE_ID_VIRTNIC;
933		virtpcidev->net = *net;
934	}
935	virtpcidev->vendor = PCI_VENDOR_ID_UNISYS;
936	virtpcidev->busNo = addparams->busNo;
937	virtpcidev->deviceNo = addparams->deviceNo;
938
939	virtpcidev->queueinfo.chan = addparams->chanptr;
940	virtpcidev->queueinfo.send_int_if_needed = NULL;
941
942	/* Set up safe queue... */
943	pIoChan = (ULTRA_IO_CHANNEL_PROTOCOL __iomem *)
944		virtpcidev->queueinfo.chan;
945
946	virtpcidev->intr = addparams->intr;
947
948	/* initialize stuff in the device portion of the struct */
949	virtpcidev->generic_dev.bus = &virtpci_bus_type;
950	virtpcidev->generic_dev.parent = parentbus;
951	virtpcidev->generic_dev.release = virtpci_device_release;
952
953	dev_set_name(&virtpcidev->generic_dev, "%x:%x",
954		     addparams->busNo, addparams->deviceNo);
955
956	/* add the vhba/vnic to virtpci device list - but check for
957	 * duplicate wwnn/macaddr first
958	 */
959	write_lock_irqsave(&VpcidevListLock, flags);
960	for (tmpvpcidev = VpcidevListHead; tmpvpcidev;
961	     tmpvpcidev = tmpvpcidev->next) {
962		if (devtype == VIRTHBA_TYPE) {
963			if ((tmpvpcidev->scsi.wwnn.wwnn1 == scsi->wwnn.wwnn1) &&
964			    (tmpvpcidev->scsi.wwnn.wwnn2 == scsi->wwnn.wwnn2)) {
965				/* duplicate - already have vpcidev
966				   with this wwnn */
967				break;
968			}
969		} else
970		    if (memcmp
971			(tmpvpcidev->net.mac_addr, net->mac_addr,
972			 MAX_MACADDR_LEN) == 0) {
973			/* duplicate - already have vnic with this wwnn */
974			break;
975		}
976	}
977	if (tmpvpcidev) {
978		/* found a vhba/vnic already in the list with same
979		 * wwnn or macaddr - reject add
980		 */
981		write_unlock_irqrestore(&VpcidevListLock, flags);
982		kfree(virtpcidev);
983		LOGERR("**** FAILED vhba/vnic already exists in the list\n");
984		POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
985		return 0;
986	}
987
988	/* add it at the head */
989	if (!VpcidevListHead)
990		VpcidevListHead = virtpcidev;
991	else {
992		/* insert virtpcidev at the head of our linked list of
993		 * vpcidevs
994		 */
995		virtpcidev->next = VpcidevListHead;
996		VpcidevListHead = virtpcidev;
997	}
998
999	write_unlock_irqrestore(&VpcidevListLock, flags);
1000
1001	/* Must transition channel to ATTACHED state BEFORE
1002	 * registering the device, because polling of the channel
1003	 * queues can begin at any time after device_register().
1004	 */
1005	pDev = &virtpcidev->generic_dev;
1006	ULTRA_CHANNEL_CLIENT_TRANSITION(addparams->chanptr,
1007					BUS_ID(pDev),
1008					CHANNELCLI_ATTACHED, NULL);
1009
1010	/* don't register until device has been added to
1011	* list. Otherwise, a device_unregister from this function can
1012	* cause a "scheduling while atomic".
1013	*/
1014	DBGINF("registering device:%p with bus_id:%s\n",
1015	       &virtpcidev->generic_dev, virtpcidev->generic_dev.bus_id);
1016	ret = device_register(&virtpcidev->generic_dev);
1017	/* NOTE: THIS IS CALLING HOTPLUG virtpci_hotplug!!!
1018	 * This call to device_register results in virtpci_bus_match
1019	 * being called !!!!!  And, if match returns success, then
1020	 * virtpcidev->generic_dev.driver is setup to core_driver,
1021	 * i.e., virtpci and the probe function
1022	 * virtpcidev->generic_dev.driver->probe is called which
1023	 * results in virtpci_device_probe being called. And if
1024	 * virtpci_device_probe is successful
1025	 */
1026	if (ret) {
1027		LOGERR("device_register returned %d\n", ret);
1028		pDev = &virtpcidev->generic_dev;
1029		ULTRA_CHANNEL_CLIENT_TRANSITION(addparams->chanptr,
1030						BUS_ID(pDev),
1031						CHANNELCLI_DETACHED, NULL);
1032		/* remove virtpcidev, the one we just added, from the list */
1033		write_lock_irqsave(&VpcidevListLock, flags);
1034		for (tmpvpcidev = VpcidevListHead, prev = NULL;
1035		     tmpvpcidev;
1036		     prev = tmpvpcidev, tmpvpcidev = tmpvpcidev->next) {
1037			if (tmpvpcidev == virtpcidev) {
1038				if (prev)
1039					prev->next = tmpvpcidev->next;
1040				else
1041					VpcidevListHead = tmpvpcidev->next;
1042				break;
1043			}
1044		}
1045		write_unlock_irqrestore(&VpcidevListLock, flags);
1046		kfree(virtpcidev);
1047		return 0;
1048	}
1049
1050	LOGINF("Added %s:%d:%d &virtpcidev->generic_dev:%p\n",
1051	       (devtype == VIRTHBA_TYPE) ? "virthba" : "virtnic",
1052	       addparams->busNo, addparams->deviceNo, &virtpcidev->generic_dev);
1053	POSTCODE_LINUX_2(VPCI_CREATE_EXIT_PC, POSTCODE_SEVERITY_INFO);
1054	return 1;
1055}
1056
1057static int virtpci_device_serverdown(struct device *parentbus,
1058				     int devtype,
1059				     struct vhba_wwnn *wwnn,
1060				     unsigned char macaddr[])
1061{
1062	int pausethisone = 0;
1063	bool found = false;
1064	struct virtpci_dev *tmpvpcidev, *prevvpcidev;
1065	struct virtpci_driver *vpcidriver;
1066	unsigned long flags;
1067	int rc = 0;
1068
1069	if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE)) {
1070		LOGERR("**** FAILED to pause device; devtype:%d not vhba:%d or vnic:%d\n",
1071		       devtype, VIRTHBA_TYPE, VIRTNIC_TYPE);
1072		return 0;
1073	}
1074
1075	/* find the vhba or vnic in virtpci device list */
1076	write_lock_irqsave(&VpcidevListLock, flags);
1077
1078	for (tmpvpcidev = VpcidevListHead, prevvpcidev = NULL;
1079	     (tmpvpcidev && !found);
1080	     prevvpcidev = tmpvpcidev, tmpvpcidev = tmpvpcidev->next) {
1081		if (tmpvpcidev->devtype != devtype)
1082			continue;
1083
1084		if (devtype == VIRTHBA_TYPE) {
1085			pausethisone =
1086			    ((tmpvpcidev->scsi.wwnn.wwnn1 == wwnn->wwnn1) &&
1087			     (tmpvpcidev->scsi.wwnn.wwnn2 == wwnn->wwnn2));
1088			/* devtype is vhba, we're pausing vhba whose
1089			* wwnn matches the current device's wwnn
1090			*/
1091		} else {	/* VIRTNIC_TYPE */
1092			pausethisone =
1093			    memcmp(tmpvpcidev->net.mac_addr, macaddr,
1094				   MAX_MACADDR_LEN) == 0;
1095			/* devtype is vnic, we're pausing vnic whose
1096			* macaddr matches the current device's macaddr */
1097		}
1098
1099		if (!pausethisone)
1100			continue;
1101
1102		found = true;
1103		vpcidriver = tmpvpcidev->mydriver;
1104		rc = vpcidriver->suspend(tmpvpcidev, 0);
1105	}
1106	write_unlock_irqrestore(&VpcidevListLock, flags);
1107
1108	if (!found) {
1109		LOGERR("**** FAILED to find vhba/vnic in the list\n");
1110		return 0;
1111	}
1112
1113	return rc;
1114}
1115
1116static int virtpci_device_serverup(struct device *parentbus,
1117				   int devtype,
1118				   struct vhba_wwnn *wwnn,
1119				   unsigned char macaddr[])
1120{
1121	int resumethisone = 0;
1122	bool found = false;
1123	struct virtpci_dev *tmpvpcidev, *prevvpcidev;
1124	struct virtpci_driver *vpcidriver;
1125	unsigned long flags;
1126	int rc = 0;
1127
1128	if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE)) {
1129		LOGERR("**** FAILED to resume device; devtype:%d not vhba:%d or vnic:%d\n",
1130		       devtype, VIRTHBA_TYPE, VIRTNIC_TYPE);
1131		return 0;
1132	}
1133
1134	/* find the vhba or vnic in virtpci device list */
1135	write_lock_irqsave(&VpcidevListLock, flags);
1136
1137	for (tmpvpcidev = VpcidevListHead, prevvpcidev = NULL;
1138	     (tmpvpcidev && !found);
1139	     prevvpcidev = tmpvpcidev, tmpvpcidev = tmpvpcidev->next) {
1140		if (tmpvpcidev->devtype != devtype)
1141			continue;
1142
1143		if (devtype == VIRTHBA_TYPE) {
1144			resumethisone =
1145			    ((tmpvpcidev->scsi.wwnn.wwnn1 == wwnn->wwnn1) &&
1146			     (tmpvpcidev->scsi.wwnn.wwnn2 == wwnn->wwnn2));
1147			/* devtype is vhba, we're resuming vhba whose
1148			* wwnn matches the current device's wwnn */
1149		} else {	/* VIRTNIC_TYPE */
1150			resumethisone =
1151			    memcmp(tmpvpcidev->net.mac_addr, macaddr,
1152				   MAX_MACADDR_LEN) == 0;
1153			/* devtype is vnic, we're resuming vnic whose
1154			* macaddr matches the current device's macaddr */
1155		}
1156
1157		if (!resumethisone)
1158			continue;
1159
1160		found = true;
1161		vpcidriver = tmpvpcidev->mydriver;
1162		/* This should be done at BUS resume time, but an
1163		* existing problem prevents us from ever getting a bus
1164		* resume...  This hack would fail to work should we
1165		* ever have a bus that contains NO devices, since we
1166		* would never even get here in that case.
1167		*/
1168		fix_vbus_devInfo(&tmpvpcidev->generic_dev, tmpvpcidev->deviceNo,
1169				 tmpvpcidev->device, vpcidriver);
1170		rc = vpcidriver->resume(tmpvpcidev);
1171	}
1172
1173	write_unlock_irqrestore(&VpcidevListLock, flags);
1174
1175	if (!found) {
1176		LOGERR("**** FAILED to find vhba/vnic in the list\n");
1177		return 0;
1178	}
1179
1180	return rc;
1181}
1182
1183static int virtpci_device_del(struct device *parentbus,
1184			      int devtype, struct vhba_wwnn *wwnn,
1185			      unsigned char macaddr[])
1186{
1187	int count = 0, all = 0, delthisone;
1188	struct virtpci_dev *tmpvpcidev, *prevvpcidev, *dellist = NULL;
1189	unsigned long flags;
1190
1191#define DEL_CONTINUE { \
1192	prevvpcidev = tmpvpcidev;\
1193	tmpvpcidev = tmpvpcidev->next;\
1194	continue; \
1195}
1196
1197	if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE)) {
1198		LOGERR("**** FAILED to delete device; devtype:%d not vhba:%d or vnic:%d\n",
1199		       devtype, VIRTHBA_TYPE, VIRTNIC_TYPE);
1200		return 0;
1201	}
1202
1203	/* see if we are to delete all - NOTE: all implies we have a
1204	 * valid parentbus
1205	 */
1206	all = ((devtype == VIRTHBA_TYPE) && (wwnn == NULL)) ||
1207	    ((devtype == VIRTNIC_TYPE) && (macaddr == NULL));
1208
1209	/* find all the vhba or vnic or both in virtpci device list
1210	* keep list of ones we are deleting so we can call
1211	* device_unregister after we release the lock; otherwise we
1212	* encounter "schedule while atomic"
1213	*/
1214	write_lock_irqsave(&VpcidevListLock, flags);
1215	for (tmpvpcidev = VpcidevListHead, prevvpcidev = NULL; tmpvpcidev;) {
1216		if (tmpvpcidev->devtype != devtype)
1217			DEL_CONTINUE;
1218
1219		if (all) {
1220			delthisone =
1221			    (tmpvpcidev->generic_dev.parent == parentbus);
1222			/* we're deleting all vhbas or vnics on the
1223			 * specified parent bus
1224			 */
1225		} else if (devtype == VIRTHBA_TYPE) {
1226			delthisone =
1227			    ((tmpvpcidev->scsi.wwnn.wwnn1 == wwnn->wwnn1) &&
1228			     (tmpvpcidev->scsi.wwnn.wwnn2 == wwnn->wwnn2));
1229			/* devtype is vhba, we're deleting vhba whose
1230			 * wwnn matches the current device's wwnn
1231			 */
1232		} else {	/* VIRTNIC_TYPE */
1233			delthisone =
1234			    memcmp(tmpvpcidev->net.mac_addr, macaddr,
1235				   MAX_MACADDR_LEN) == 0;
1236			/* devtype is vnic, we're deleting vnic whose
1237			* macaddr matches the current device's macaddr
1238			*/
1239		}
1240
1241		if (!delthisone)
1242			DEL_CONTINUE;
1243
1244		/* take vhba/vnic out of the list */
1245		if (prevvpcidev)
1246			/* not at head */
1247			prevvpcidev->next = tmpvpcidev->next;
1248		else
1249			VpcidevListHead = tmpvpcidev->next;
1250
1251		/* add it to our deletelist */
1252		tmpvpcidev->next = dellist;
1253		dellist = tmpvpcidev;
1254
1255		count++;
1256		if (!all)
1257			break;	/* done */
1258		/* going to top of loop again - set tmpvpcidev to next
1259		 * one we're to process
1260		 */
1261		if (prevvpcidev)
1262			tmpvpcidev = prevvpcidev->next;
1263		else
1264			tmpvpcidev = VpcidevListHead;
1265	}
1266	write_unlock_irqrestore(&VpcidevListLock, flags);
1267
1268	if (!all && (count == 0)) {
1269		LOGERR("**** FAILED to find vhba/vnic in the list\n");
1270		return 0;
1271	}
1272
1273	/* now delete each one from delete list */
1274	while (dellist) {
1275		/* save next */
1276		tmpvpcidev = dellist->next;
1277		/* delete the vhba/vnic at dellist */
1278		DELETE_ONE_VPCIDEV(dellist);
1279		/* do next */
1280		dellist = tmpvpcidev;
1281	}
1282
1283	return count;
1284}
1285
1286static void virtpci_device_release(struct device *dev_)
1287{
1288	/* this function is called when the last reference to the
1289	 * device is removed
1290	 */
1291	LOGINF("In virtpci_device_release:%p - NOT YET IMPLEMENTED\n", dev_);
1292}
1293
1294/*****************************************************/
1295/* Driver functions                                  */
1296/*****************************************************/
1297
1298#define kobj_to_device_driver(obj) container_of(obj, struct device_driver, kobj)
1299#define attribute_to_driver_attribute(obj) \
1300	container_of(obj, struct driver_attribute, attr)
1301
1302static ssize_t virtpci_driver_attr_show(struct kobject *kobj,
1303					struct attribute *attr,
1304					char *buf)
1305{
1306	struct driver_attribute *dattr = attribute_to_driver_attribute(attr);
1307	ssize_t ret = 0;
1308
1309	struct driver_private *dprivate = to_driver(kobj);
1310	struct device_driver *driver;
1311	if (dprivate != NULL)
1312		driver = dprivate->driver;
1313	else
1314		driver = NULL;
1315
1316	DBGINF("In virtpci_driver_attr_show driver->name:%s\n", driver->name);
1317	if (driver) {
1318		if (dattr->show)
1319			ret = dattr->show(driver, buf);
1320	}
1321	return ret;
1322}
1323
1324static ssize_t virtpci_driver_attr_store(struct kobject *kobj,
1325					 struct attribute *attr,
1326					 const char *buf, size_t count)
1327{
1328	struct driver_attribute *dattr = attribute_to_driver_attribute(attr);
1329	ssize_t ret = 0;
1330
1331	struct driver_private *dprivate = to_driver(kobj);
1332	struct device_driver *driver;
1333	if (dprivate != NULL)
1334		driver = dprivate->driver;
1335	else
1336		driver = NULL;
1337
1338	DBGINF("In virtpci_driver_attr_store driver->name:%s\n", driver->name);
1339
1340	if (driver) {
1341		if (dattr->store)
1342			ret = dattr->store(driver, buf, count);
1343	}
1344	return ret;
1345}
1346
1347/* register a new virtpci driver */
1348int virtpci_register_driver(struct virtpci_driver *drv)
1349{
1350	int result = 0;
1351
1352	DBGINF("In virtpci_register_driver\n");
1353
1354	if (drv->id_table == NULL) {
1355		LOGERR("id_table missing\n");
1356		return 1;
1357	}
1358	/* initialize core driver fields needed to call driver_register */
1359	drv->core_driver.name = drv->name;	/* name of driver in sysfs */
1360	drv->core_driver.bus = &virtpci_bus_type;	/* type of bus this
1361							 * driver works with */
1362	drv->core_driver.probe = virtpci_device_probe;	/* called to query the
1363							 * existence of a
1364							 * specific device and
1365							 * whether this driver
1366							 *can work with it */
1367	drv->core_driver.remove = virtpci_device_remove; /* called when the
1368							  * device is removed
1369							  * from the system */
1370	/* register with core */
1371	result = driver_register(&drv->core_driver);
1372	/* calls bus_add_driver which calls driver_attach and
1373	 * module_add_driver
1374	 */
1375	if (result)
1376		return result;	/* failed */
1377
1378	drv->core_driver.p->kobj.ktype = &virtpci_driver_kobj_type;
1379
1380	return 0;
1381}
1382EXPORT_SYMBOL_GPL(virtpci_register_driver);
1383
1384void virtpci_unregister_driver(struct virtpci_driver *drv)
1385{
1386	DBGINF("In virtpci_unregister_driver drv:%p\n", drv);
1387	driver_unregister(&drv->core_driver);
1388	/* driver_unregister calls bus_remove_driver
1389	 * bus_remove_driver calls device_detach
1390	 * device_detach calls device_release_driver for each of the
1391	 * driver's devices
1392	 * device_release driver calls drv->remove which is
1393	 * virtpci_device_remove
1394	 * virtpci_device_remove calls virthba_remove
1395	 */
1396	DBGINF("Leaving\n");
1397}
1398EXPORT_SYMBOL_GPL(virtpci_unregister_driver);
1399
1400/*****************************************************/
1401/* debugfs filesystem functions                      */
1402/*****************************************************/
1403struct print_vbus_info {
1404	int *str_pos;
1405	char *buf;
1406	size_t *len;
1407};
1408
1409static int print_vbus(struct device *vbus, void *data)
1410{
1411	struct print_vbus_info *p = (struct print_vbus_info *)data;
1412
1413	*p->str_pos += scnprintf(p->buf + *p->str_pos, *p->len - *p->str_pos,
1414				"bus_id:%s\n", dev_name(vbus));
1415	return 0;
1416}
1417
1418static ssize_t info_debugfs_read(struct file *file, char __user *buf,
1419			      size_t len, loff_t *offset)
1420{
1421	ssize_t bytes_read = 0;
1422	int str_pos = 0;
1423	struct virtpci_dev *tmpvpcidev;
1424	unsigned long flags;
1425	struct print_vbus_info printparam;
1426	char *vbuf;
1427
1428	if (len > MAX_BUF)
1429		len = MAX_BUF;
1430	vbuf = kzalloc(len, GFP_KERNEL);
1431	if (!vbuf)
1432		return -ENOMEM;
1433
1434	str_pos += scnprintf(vbuf + str_pos, len - str_pos,
1435			" Virtual PCI Bus devices\n");
1436	printparam.str_pos = &str_pos;
1437	printparam.buf = vbuf;
1438	printparam.len = &len;
1439	if (bus_for_each_dev(&virtpci_bus_type, NULL,
1440			     (void *) &printparam, print_vbus))
1441		LOGERR("Failed to find bus\n");
1442
1443	str_pos += scnprintf(vbuf + str_pos, len - str_pos,
1444			"\n Virtual PCI devices\n");
1445	read_lock_irqsave(&VpcidevListLock, flags);
1446	tmpvpcidev = VpcidevListHead;
1447	while (tmpvpcidev) {
1448		if (tmpvpcidev->devtype == VIRTHBA_TYPE) {
1449			str_pos += scnprintf(vbuf + str_pos, len - str_pos,
1450					"[%d:%d] VHba:%08x:%08x max-config:%d-%d-%d-%d",
1451					tmpvpcidev->busNo, tmpvpcidev->deviceNo,
1452					tmpvpcidev->scsi.wwnn.wwnn1,
1453					tmpvpcidev->scsi.wwnn.wwnn2,
1454					tmpvpcidev->scsi.max.max_channel,
1455					tmpvpcidev->scsi.max.max_id,
1456					tmpvpcidev->scsi.max.max_lun,
1457					tmpvpcidev->scsi.max.cmd_per_lun);
1458		} else {
1459			str_pos += scnprintf(vbuf + str_pos, len - str_pos,
1460					"[%d:%d] VNic:%02x:%02x:%02x:%02x:%02x:%02x num_rcv_bufs:%d mtu:%d",
1461					tmpvpcidev->busNo, tmpvpcidev->deviceNo,
1462					tmpvpcidev->net.mac_addr[0],
1463					tmpvpcidev->net.mac_addr[1],
1464					tmpvpcidev->net.mac_addr[2],
1465					tmpvpcidev->net.mac_addr[3],
1466					tmpvpcidev->net.mac_addr[4],
1467					tmpvpcidev->net.mac_addr[5],
1468					tmpvpcidev->net.num_rcv_bufs,
1469					tmpvpcidev->net.mtu);
1470		}
1471		str_pos += scnprintf(vbuf + str_pos,
1472				len - str_pos, " chanptr:%p\n",
1473				tmpvpcidev->queueinfo.chan);
1474				tmpvpcidev = tmpvpcidev->next;
1475	}
1476	read_unlock_irqrestore(&VpcidevListLock, flags);
1477
1478	str_pos += scnprintf(vbuf + str_pos, len - str_pos, "\n");
1479	bytes_read = simple_read_from_buffer(buf, len, offset, vbuf, str_pos);
1480	kfree(vbuf);
1481	return bytes_read;
1482}
1483
1484/*****************************************************/
1485/* Module Init & Exit functions                      */
1486/*****************************************************/
1487
1488static int __init virtpci_mod_init(void)
1489{
1490	int ret;
1491
1492
1493	if (!unisys_spar_platform)
1494		return -ENODEV;
1495
1496	POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1497
1498	ret = bus_register(&virtpci_bus_type);
1499	/* creates /sys/bus/uisvirtpci which contains devices &
1500	 * drivers directory
1501	 */
1502	if (ret) {
1503		LOGERR("bus_register ****FAILED:%d\n", ret);
1504		POSTCODE_LINUX_3(VPCI_CREATE_FAILURE_PC, ret,
1505				 POSTCODE_SEVERITY_ERR);
1506		return ret;
1507	}
1508	DBGINF("bus_register successful\n");
1509	BusDeviceInfo_Init(&Bus_DriverInfo, "clientbus", "virtpci",
1510			   VERSION, NULL);
1511
1512	/* create a root bus used to parent all the virtpci buses. */
1513	ret = device_register(&virtpci_rootbus_device);
1514	if (ret) {
1515		LOGERR("device_register FAILED:%d\n", ret);
1516		bus_unregister(&virtpci_bus_type);
1517		POSTCODE_LINUX_3(VPCI_CREATE_FAILURE_PC, ret,
1518				 POSTCODE_SEVERITY_ERR);
1519		return ret;
1520	}
1521	DBGINF("device_register successful ret:%x\n", ret);
1522
1523	if (!uisctrl_register_req_handler(2, (void *) &virtpci_ctrlchan_func,
1524					  &Chipset_DriverInfo)) {
1525		LOGERR("uisctrl_register_req_handler ****FAILED.\n");
1526		POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
1527		device_unregister(&virtpci_rootbus_device);
1528		bus_unregister(&virtpci_bus_type);
1529		return -1;
1530	}
1531
1532	LOGINF("successfully registered virtpci_ctrlchan_func (0x%p) as callback.\n",
1533	     (void *) &virtpci_ctrlchan_func);
1534	/* create debugfs directory and info file inside. */
1535	virtpci_debugfs_dir = debugfs_create_dir("virtpci", NULL);
1536	debugfs_create_file("info", S_IRUSR, virtpci_debugfs_dir,
1537			NULL, &debugfs_info_fops);
1538	LOGINF("Leaving\n");
1539	POSTCODE_LINUX_2(VPCI_CREATE_EXIT_PC, POSTCODE_SEVERITY_INFO);
1540	return 0;
1541}
1542
1543static void __exit virtpci_mod_exit(void)
1544{
1545	LOGINF("virtpci_mod_exit...\n");
1546
1547	/* unregister the callback function */
1548	if (!uisctrl_register_req_handler(2, NULL, NULL))
1549		LOGERR("uisctrl_register_req_handler ****FAILED.\n");
1550
1551	device_unregister(&virtpci_rootbus_device);
1552	bus_unregister(&virtpci_bus_type);
1553	debugfs_remove_recursive(virtpci_debugfs_dir);
1554	LOGINF("Leaving\n");
1555
1556}
1557
1558module_init(virtpci_mod_init);
1559module_exit(virtpci_mod_exit);
1560MODULE_LICENSE("GPL");
1561MODULE_AUTHOR("Usha Srinivasan");
1562MODULE_ALIAS("uisvirtpci");
1563
1564