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 "diagnostics/appos_subsystems.h"
25 #include "uisutils.h"
26 #include "vbuschannel.h"
27 #include "vbushelper.h"
28 #include <linux/types.h>
29 #include <linux/io.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>
41 #include "version.h"
42 #include "guestlinuxdebug.h"
43 #include "timskmod.h"
44 
45 struct driver_private {
46 	struct kobject kobj;
47 	struct klist klist_devices;
48 	struct klist_node knode_bus;
49 	struct module_kobject *mkobj;
50 	struct device_driver *driver;
51 };
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 
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);
108 
109 static ssize_t info_debugfs_read(struct file *file, char __user *buf,
110 				 size_t len, loff_t *offset);
111 
112 static const struct file_operations debugfs_info_fops = {
113 	.read = info_debugfs_read,
114 };
115 
116 /*****************************************************/
117 /* Globals                                           */
118 /*****************************************************/
119 
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
122  * individual drivers
123  */
124 static struct bus_type virtpci_bus_type = {
125 	.name = "uisvirtpci",
126 	.match = virtpci_bus_match,
127 	.uevent = virtpci_uevent,
128 };
129 
130 static struct device virtpci_rootbus_device = {
131 	.init_name = "vbusroot",	/* root bus */
132 	.release = virtpci_bus_release
133 };
134 
135 /* filled in with info about parent chipset driver when we register with it */
136 static struct ultra_vbus_deviceinfo chipset_driver_info;
137 
138 static const struct sysfs_ops virtpci_driver_sysfs_ops = {
139 	.show = virtpci_driver_attr_show,
140 	.store = virtpci_driver_attr_store,
141 };
142 
143 static struct kobj_type virtpci_driver_kobj_type = {
144 	.sysfs_ops = &virtpci_driver_sysfs_ops,
145 };
146 
147 static struct virtpci_dev *vpcidev_list_head;
148 static DEFINE_RWLOCK(vpcidev_list_lock);
149 
150 /* filled in with info about this driver, wrt it servicing client busses */
151 static struct ultra_vbus_deviceinfo bus_driver_info;
152 
153 /*****************************************************/
154 /* debugfs entries                                   */
155 /*****************************************************/
156 /* dentry is used to create the debugfs entry directory
157  * for virtpci
158  */
159 static struct dentry *virtpci_debugfs_dir;
160 
161 struct virtpci_busdev {
162 	struct device virtpci_bus_device;
163 };
164 
165 /*****************************************************/
166 /* Local functions                                   */
167 /*****************************************************/
168 
169 static inline
WAIT_FOR_IO_CHANNEL(struct spar_io_channel_protocol __iomem * chanptr)170 int WAIT_FOR_IO_CHANNEL(struct spar_io_channel_protocol __iomem  *chanptr)
171 {
172 	int count = 120;
173 
174 	while (count > 0) {
175 		if (SPAR_CHANNEL_SERVER_READY(&chanptr->channel_header))
176 			return 1;
177 		UIS_THREAD_WAIT_SEC(1);
178 		count--;
179 	}
180 	return 0;
181 }
182 
183 /* Write the contents of <info> to the ULTRA_VBUS_CHANNEL_PROTOCOL.ChpInfo. */
write_vbus_chp_info(struct spar_vbus_channel_protocol * chan,struct ultra_vbus_deviceinfo * info)184 static int write_vbus_chp_info(struct spar_vbus_channel_protocol *chan,
185 			       struct ultra_vbus_deviceinfo *info)
186 {
187 	int off;
188 
189 	if (!chan)
190 		return -1;
191 
192 	off = sizeof(struct channel_header) + chan->hdr_info.chp_info_offset;
193 	if (chan->hdr_info.chp_info_offset == 0) {
194 		return -1;
195 	}
196 	memcpy(((u8 *)(chan)) + off, info, sizeof(*info));
197 	return 0;
198 }
199 
200 /* Write the contents of <info> to the ULTRA_VBUS_CHANNEL_PROTOCOL.BusInfo. */
write_vbus_bus_info(struct spar_vbus_channel_protocol * chan,struct ultra_vbus_deviceinfo * info)201 static int write_vbus_bus_info(struct spar_vbus_channel_protocol *chan,
202 			       struct ultra_vbus_deviceinfo *info)
203 {
204 	int off;
205 
206 	if (!chan)
207 		return -1;
208 
209 	off = sizeof(struct channel_header) + chan->hdr_info.bus_info_offset;
210 	if (chan->hdr_info.bus_info_offset == 0)
211 		return -1;
212 	memcpy(((u8 *)(chan)) + off, info, sizeof(*info));
213 	return 0;
214 }
215 
216 /* Write the contents of <info> to the
217  * ULTRA_VBUS_CHANNEL_PROTOCOL.DevInfo[<devix>].
218  */
219 static int
write_vbus_dev_info(struct spar_vbus_channel_protocol * chan,struct ultra_vbus_deviceinfo * info,int devix)220 write_vbus_dev_info(struct spar_vbus_channel_protocol *chan,
221 		    struct ultra_vbus_deviceinfo *info, int devix)
222 {
223 	int off;
224 
225 	if (!chan)
226 		return -1;
227 
228 	off =
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)
233 		return -1;
234 
235 	memcpy(((u8 *)(chan)) + off, info, sizeof(*info));
236 	return 0;
237 }
238 
239 /* adds a vbus
240  * returns 0 failure, 1 success,
241  */
add_vbus(struct add_vbus_guestpart * addparams)242 static int add_vbus(struct add_vbus_guestpart *addparams)
243 {
244 	int ret;
245 	struct device *vbus;
246 
247 	vbus = kzalloc(sizeof(*vbus), GFP_ATOMIC);
248 
249 	POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
250 	if (!vbus)
251 		return 0;
252 
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;
258 
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
263 	 */
264 	ret = device_register(vbus);
265 	if (ret) {
266 		POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
267 		return 0;
268 	}
269 	write_vbus_chp_info(vbus->platform_data /* chanptr */,
270 			    &chipset_driver_info);
271 	write_vbus_bus_info(vbus->platform_data /* chanptr */,
272 			    &bus_driver_info);
273 	POSTCODE_LINUX_2(VPCI_CREATE_EXIT_PC, POSTCODE_SEVERITY_INFO);
274 	return 1;
275 }
276 
277 /* for CHANSOCK wwwnn/max are AUTO-GENERATED; for normal channels,
278  * wwnn/max are in the channel header.
279  */
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));			\
289 	}
290 
291 /* adds a vhba
292  * returns 0 failure, 1 success,
293  */
add_vhba(struct add_virt_guestpart * addparams)294 static int add_vhba(struct add_virt_guestpart *addparams)
295 {
296 	int i;
297 	struct scsi_adap_info scsi;
298 	struct device *vbus;
299 	unsigned char busid[BUS_ID_SIZE];
300 
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);
305 		return 0;
306 	}
307 
308 	GET_SCSIADAPINFO_FROM_CHANPTR(addparams->chanptr);
309 
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);
314 	if (!vbus)
315 		return 0;
316 
317 	i = virtpci_device_add(vbus, VIRTHBA_TYPE, addparams, &scsi, NULL);
318 	if (i) {
319 		POSTCODE_LINUX_3(VPCI_CREATE_EXIT_PC, i,
320 				 POSTCODE_SEVERITY_INFO);
321 	}
322 	return i;
323 }
324 
325 /* for CHANSOCK macaddr is AUTO-GENERATED; for normal channels,
326  * macaddr is in the channel header.
327  */
328 #define GET_NETADAPINFO_FROM_CHANPTR(chanptr) {				\
329 		memcpy_fromio(net.mac_addr,				\
330 		       ((struct spar_io_channel_protocol __iomem *)	\
331 		       chanptr)->vnic.macaddr,				\
332 		       MAX_MACADDR_LEN);				\
333 		net.num_rcv_bufs =					\
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,		\
341 			      sizeof(uuid_le));				\
342 }
343 
344 /* adds a vnic
345  * returns 0 failure, 1 success,
346  */
347 static int
add_vnic(struct add_virt_guestpart * addparams)348 add_vnic(struct add_virt_guestpart *addparams)
349 {
350 	int i;
351 	struct net_adap_info net;
352 	struct device *vbus;
353 	unsigned char busid[BUS_ID_SIZE];
354 
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);
359 		return 0;
360 	}
361 
362 	GET_NETADAPINFO_FROM_CHANPTR(addparams->chanptr);
363 
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);
368 	if (!vbus)
369 		return 0;
370 
371 	i = virtpci_device_add(vbus, VIRTNIC_TYPE, addparams, NULL, &net);
372 	if (i) {
373 		POSTCODE_LINUX_3(VPCI_CREATE_EXIT_PC, i,
374 				 POSTCODE_SEVERITY_INFO);
375 		return 1;
376 	}
377 	return 0;
378 }
379 
380 /* delete vbus
381  * returns 0 failure, 1 success,
382  */
383 static int
delete_vbus(struct del_vbus_guestpart * delparams)384 delete_vbus(struct del_vbus_guestpart *delparams)
385 {
386 	struct device *vbus;
387 	unsigned char busid[BUS_ID_SIZE];
388 
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);
393 	if (!vbus)
394 		return 0;
395 
396 	/* ensure that bus has no devices? -- TBD */
397 	return 1;
398 }
399 
400 static int
delete_vbus_device(struct device * vbus,void * data)401 delete_vbus_device(struct device *vbus, void *data)
402 {
403 	struct device *dev = &virtpci_rootbus_device;
404 
405 	if ((data) && match_busid(vbus, (void *)BUS_ID(dev))) {
406 		/* skip it - don't delete root bus */
407 		return 0;	/* pretend no error */
408 	}
409 	device_unregister(vbus);
410 	kfree(vbus);
411 	return 0;		/* no error */
412 }
413 
414 /* pause vhba
415 * returns 0 failure, 1 success,
416 */
pause_vhba(struct pause_virt_guestpart * pauseparams)417 static int pause_vhba(struct pause_virt_guestpart *pauseparams)
418 {
419 	int i;
420 	struct scsi_adap_info scsi;
421 
422 	GET_SCSIADAPINFO_FROM_CHANPTR(pauseparams->chanptr);
423 
424 	i = virtpci_device_serverdown(NULL /*no parent bus */, VIRTHBA_TYPE,
425 				      &scsi.wwnn, NULL);
426 	return i;
427 }
428 
429 /* pause vnic
430  * returns 0 failure, 1 success,
431  */
pause_vnic(struct pause_virt_guestpart * pauseparams)432 static int pause_vnic(struct pause_virt_guestpart *pauseparams)
433 {
434 	int i;
435 	struct net_adap_info net;
436 
437 	GET_NETADAPINFO_FROM_CHANPTR(pauseparams->chanptr);
438 
439 	i = virtpci_device_serverdown(NULL /*no parent bus */, VIRTNIC_TYPE,
440 				      NULL, net.mac_addr);
441 	return i;
442 }
443 
444 /* resume vhba
445  * returns 0 failure, 1 success,
446  */
resume_vhba(struct resume_virt_guestpart * resumeparams)447 static int resume_vhba(struct resume_virt_guestpart *resumeparams)
448 {
449 	int i;
450 	struct scsi_adap_info scsi;
451 
452 	GET_SCSIADAPINFO_FROM_CHANPTR(resumeparams->chanptr);
453 
454 	i = virtpci_device_serverup(NULL /*no parent bus */, VIRTHBA_TYPE,
455 				    &scsi.wwnn, NULL);
456 	return i;
457 }
458 
459 /* resume vnic
460 * returns 0 failure, 1 success,
461 */
462 static int
resume_vnic(struct resume_virt_guestpart * resumeparams)463 resume_vnic(struct resume_virt_guestpart *resumeparams)
464 {
465 	int i;
466 	struct net_adap_info net;
467 
468 	GET_NETADAPINFO_FROM_CHANPTR(resumeparams->chanptr);
469 
470 	i = virtpci_device_serverup(NULL /*no parent bus */, VIRTNIC_TYPE,
471 				    NULL, net.mac_addr);
472 	return i;
473 }
474 
475 /* delete vhba
476 * returns 0 failure, 1 success,
477 */
delete_vhba(struct del_virt_guestpart * delparams)478 static int delete_vhba(struct del_virt_guestpart *delparams)
479 {
480 	int i;
481 	struct scsi_adap_info scsi;
482 
483 	GET_SCSIADAPINFO_FROM_CHANPTR(delparams->chanptr);
484 
485 	i = virtpci_device_del(NULL /*no parent bus */, VIRTHBA_TYPE,
486 			       &scsi.wwnn, NULL);
487 	if (i) {
488 		return 1;
489 	}
490 	return 0;
491 }
492 
493 /* deletes a vnic
494  * returns 0 failure, 1 success,
495  */
delete_vnic(struct del_virt_guestpart * delparams)496 static int delete_vnic(struct del_virt_guestpart *delparams)
497 {
498 	int i;
499 	struct net_adap_info net;
500 
501 	GET_NETADAPINFO_FROM_CHANPTR(delparams->chanptr);
502 
503 	i = virtpci_device_del(NULL /*no parent bus */, VIRTNIC_TYPE, NULL,
504 			       net.mac_addr);
505 	return i;
506 }
507 
508 #define DELETE_ONE_VPCIDEV(vpcidev) { \
509 	device_unregister(&vpcidev->generic_dev); \
510 	kfree(vpcidev); \
511 }
512 
513 /* deletes all vhbas and vnics
514  * returns 0 failure, 1 success,
515  */
delete_all(void)516 static void delete_all(void)
517 {
518 	int count = 0;
519 	unsigned long flags;
520 	struct virtpci_dev *tmpvpcidev, *nextvpcidev;
521 
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);
527 
528 	/* delete one vhba/vnic at a time */
529 	while (tmpvpcidev) {
530 		nextvpcidev = tmpvpcidev->next;
531 		/* delete the vhba/vnic at tmpvpcidev */
532 		DELETE_ONE_VPCIDEV(tmpvpcidev);
533 		tmpvpcidev = nextvpcidev;
534 		count++;
535 	}
536 
537 	/* now delete each vbus */
538 	bus_for_each_dev(&virtpci_bus_type, NULL, (void *)1,
539 			 delete_vbus_device);
540 }
541 
542 /* deletes all vnics or vhbas
543  * returns 0 failure, 1 success,
544  */
delete_all_virt(enum virtpci_dev_type devtype,struct del_vbus_guestpart * delparams)545 static int delete_all_virt(enum virtpci_dev_type devtype,
546 			   struct del_vbus_guestpart *delparams)
547 {
548 	int i;
549 	unsigned char busid[BUS_ID_SIZE];
550 	struct device *vbus;
551 
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);
556 	if (!vbus)
557 		return 0;
558 
559 	if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE))
560 		return 0;
561 
562 	/* delete all vhbas/vnics */
563 	i = virtpci_device_del(vbus, devtype, NULL, NULL);
564 	return 1;
565 }
566 
virtpci_ctrlchan_func(struct guest_msgs * msg)567 static int virtpci_ctrlchan_func(struct guest_msgs *msg)
568 {
569 	switch (msg->msgtype) {
570 	case GUEST_ADD_VBUS:
571 		return add_vbus(&msg->add_vbus);
572 	case GUEST_ADD_VHBA:
573 		return add_vhba(&msg->add_vhba);
574 	case GUEST_ADD_VNIC:
575 		return add_vnic(&msg->add_vnic);
576 	case GUEST_DEL_VBUS:
577 		return delete_vbus(&msg->del_vbus);
578 	case GUEST_DEL_VHBA:
579 		return delete_vhba(&msg->del_vhba);
580 	case GUEST_DEL_VNIC:
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:
587 		delete_all();
588 		return 1;
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);
597 	default:
598 		return 0;
599 	}
600 }
601 
602 /* same as driver_helper in bus.c linux */
match_busid(struct device * dev,void * data)603 static int match_busid(struct device *dev, void *data)
604 {
605 	const char *name = data;
606 
607 	if (strcmp(name, BUS_ID(dev)) == 0)
608 		return 1;
609 	return 0;
610 }
611 
612 /*****************************************************/
613 /*  Bus functions                                    */
614 /*****************************************************/
615 
616 static const struct pci_device_id *
virtpci_match_device(const struct pci_device_id * ids,const struct virtpci_dev * dev)617 virtpci_match_device(const struct pci_device_id *ids,
618 		     const struct virtpci_dev *dev)
619 {
620 	while (ids->vendor || ids->subvendor || ids->class_mask) {
621 		if ((ids->vendor == dev->vendor) &&
622 		    (ids->device == dev->device))
623 			return ids;
624 
625 		ids++;
626 	}
627 	return NULL;
628 }
629 
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.
634 */
virtpci_bus_match(struct device * dev,struct device_driver * drv)635 static int virtpci_bus_match(struct device *dev, struct device_driver *drv)
636 {
637 	struct virtpci_dev *virtpcidev = device_to_virtpci_dev(dev);
638 	struct virtpci_driver *virtpcidrv = driver_to_virtpci_driver(drv);
639 	int match = 0;
640 
641 	/* check ids list for a match */
642 	if (virtpci_match_device(virtpcidrv->id_table, virtpcidev))
643 		match = 1;
644 
645 	return match;		/* 0 - no match; 1 - yes it matches */
646 }
647 
virtpci_uevent(struct device * dev,struct kobj_uevent_env * env)648 static int virtpci_uevent(struct device *dev, struct kobj_uevent_env *env)
649 {
650 	/* add variables to the environment prior to the generation of
651 	 * hotplug events to user space
652 	 */
653 	if (add_uevent_var(env, "VIRTPCI_VERSION=%s", VIRTPCI_VERSION))
654 		return -ENOMEM;
655 	return 0;
656 }
657 
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
661  * instance.
662  */
fix_vbus_dev_info(struct device * dev,int dev_no,int dev_type,struct virtpci_driver * virtpcidrv)663 static void fix_vbus_dev_info(struct device *dev, int dev_no, int dev_type,
664 			      struct virtpci_driver *virtpcidrv)
665 {
666 	struct device *vbus;
667 	void *chan;
668 	struct ultra_vbus_deviceinfo dev_info;
669 	const char *stype;
670 
671 	if (!dev)
672 		return;
673 	if (!virtpcidrv)
674 		return;
675 
676 	vbus = dev->parent;
677 	if (!vbus)
678 		return;
679 
680 	chan = vbus->platform_data;
681 	if (!chan)
682 		return;
683 
684 	switch (dev_type) {
685 	case PCI_DEVICE_ID_VIRTHBA:
686 		stype = "vHBA";
687 		break;
688 	case PCI_DEVICE_ID_VIRTNIC:
689 		stype = "vNIC";
690 		break;
691 	default:
692 		stype = "unknown";
693 		break;
694 	}
695 	bus_device_info_init(&dev_info, stype,
696 			     virtpcidrv->name,
697 			     virtpcidrv->version,
698 			     virtpcidrv->vertag);
699 	write_vbus_dev_info(chan, &dev_info, dev_no);
700 
701 	/* Re-write bus+chipset info, because it is possible that this
702 	* was previously written by our good counterpart, visorbus.
703 	*/
704 	write_vbus_chp_info(chan, &chipset_driver_info);
705 	write_vbus_bus_info(chan, &bus_driver_info);
706 }
707 
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.
711 */
virtpci_device_probe(struct device * dev)712 static int virtpci_device_probe(struct device *dev)
713 {
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;
718 	int error = 0;
719 
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?.
723 	 */
724 	if (!virtpcidrv->id_table)
725 		return -ENODEV;
726 
727 	id = virtpci_match_device(virtpcidrv->id_table, virtpcidev);
728 	if (!id)
729 		return -ENODEV;
730 
731 	/* increment reference count */
732 	get_device(dev);
733 
734 	/* if virtpcidev is not already claimed & probe function is
735 	 * valid, probe it
736 	 */
737 	if (!virtpcidev->mydriver && virtpcidrv->probe) {
738 		/* call the probe function - virthba or virtnic probe
739 		 * is what it should be
740 		 */
741 		error = virtpcidrv->probe(virtpcidev, id);
742 		if (!error) {
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);
748 		} else {
749 			put_device(dev);
750 		}
751 	}
752 	POSTCODE_LINUX_2(VPCI_PROBE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
753 	return error;		/* -ENODEV for probe failure */
754 }
755 
virtpci_device_remove(struct device * dev_)756 static int virtpci_device_remove(struct device *dev_)
757 {
758 	/* dev_ passed in is the HBA device which we called
759 	* generic_dev in our virtpcidev struct
760 	*/
761 	struct virtpci_dev *virtpcidev = device_to_virtpci_dev(dev_);
762 	struct virtpci_driver *virtpcidrv = virtpcidev->mydriver;
763 
764 	if (virtpcidrv) {
765 		/* TEMP: assuming we have only one such driver for now */
766 		if (virtpcidrv->remove)
767 			virtpcidrv->remove(virtpcidev);
768 		virtpcidev->mydriver = NULL;
769 	}
770 
771 	put_device(dev_);
772 	return 0;
773 }
774 
775 /*****************************************************/
776 /* Bus functions                                     */
777 /*****************************************************/
778 
virtpci_bus_release(struct device * dev)779 static void virtpci_bus_release(struct device *dev)
780 {
781 }
782 
783 /*****************************************************/
784 /* Adapter functions                                 */
785 /*****************************************************/
786 
787 /* scsi is expected to be NULL for VNIC add
788  * net is expected to be NULL for VHBA add
789  */
virtpci_device_add(struct device * parentbus,int devtype,struct add_virt_guestpart * addparams,struct scsi_adap_info * scsi,struct net_adap_info * net)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)
794 {
795 	struct virtpci_dev *virtpcidev = NULL;
796 	struct virtpci_dev *tmpvpcidev = NULL, *prev;
797 	unsigned long flags;
798 	int ret;
799 	struct spar_io_channel_protocol __iomem *io_chan = NULL;
800 	struct device *dev;
801 
802 	POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
803 
804 	if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE)) {
805 		POSTCODE_LINUX_3(VPCI_CREATE_FAILURE_PC, devtype,
806 				 POSTCODE_SEVERITY_ERR);
807 		return 0;
808 	}
809 
810 	/* add a Virtual Device */
811 	virtpcidev = kzalloc(sizeof(*virtpcidev), GFP_ATOMIC);
812 	if (!virtpcidev) {
813 		POSTCODE_LINUX_2(MALLOC_FAILURE_PC, POSTCODE_SEVERITY_ERR);
814 		return 0;
815 	}
816 
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;
822 	} else {
823 		virtpcidev->device = PCI_DEVICE_ID_VIRTNIC;
824 		virtpcidev->net = *net;
825 	}
826 	virtpcidev->vendor = PCI_VENDOR_ID_UNISYS;
827 	virtpcidev->bus_no = addparams->bus_no;
828 	virtpcidev->device_no = addparams->device_no;
829 
830 	virtpcidev->queueinfo.chan = addparams->chanptr;
831 	virtpcidev->queueinfo.send_int_if_needed = NULL;
832 
833 	/* Set up safe queue... */
834 	io_chan = (struct spar_io_channel_protocol __iomem *)
835 		virtpcidev->queueinfo.chan;
836 
837 	virtpcidev->intr = addparams->intr;
838 
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;
843 
844 	dev_set_name(&virtpcidev->generic_dev, "%x:%x",
845 		     addparams->bus_no, addparams->device_no);
846 
847 	/* add the vhba/vnic to virtpci device list - but check for
848 	 * duplicate wwnn/macaddr first
849 	 */
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
857 				   with this wwnn */
858 				break;
859 			}
860 		} else
861 		    if (memcmp
862 			(tmpvpcidev->net.mac_addr, net->mac_addr,
863 			 MAX_MACADDR_LEN) == 0) {
864 			/* duplicate - already have vnic with this wwnn */
865 			break;
866 		}
867 	}
868 	if (tmpvpcidev) {
869 		/* found a vhba/vnic already in the list with same
870 		 * wwnn or macaddr - reject add
871 		 */
872 		write_unlock_irqrestore(&vpcidev_list_lock, flags);
873 		kfree(virtpcidev);
874 		POSTCODE_LINUX_2(VPCI_CREATE_FAILURE_PC, POSTCODE_SEVERITY_ERR);
875 		return 0;
876 	}
877 
878 	/* add it at the head */
879 	if (!vpcidev_list_head) {
880 		vpcidev_list_head = virtpcidev;
881 	} else {
882 		/* insert virtpcidev at the head of our linked list of
883 		 * vpcidevs
884 		 */
885 		virtpcidev->next = vpcidev_list_head;
886 		vpcidev_list_head = virtpcidev;
887 	}
888 
889 	write_unlock_irqrestore(&vpcidev_list_lock, flags);
890 
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().
894 	 */
895 	dev = &virtpcidev->generic_dev;
896 	SPAR_CHANNEL_CLIENT_TRANSITION(addparams->chanptr,
897 				       BUS_ID(dev),
898 				       CHANNELCLI_ATTACHED, NULL);
899 
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".
903 	*/
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
913 	 */
914 	if (ret) {
915 		dev = &virtpcidev->generic_dev;
916 		SPAR_CHANNEL_CLIENT_TRANSITION(addparams->chanptr,
917 					       BUS_ID(dev),
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;
922 		     tmpvpcidev;
923 		     prev = tmpvpcidev, tmpvpcidev = tmpvpcidev->next) {
924 			if (tmpvpcidev == virtpcidev) {
925 				if (prev)
926 					prev->next = tmpvpcidev->next;
927 				else
928 					vpcidev_list_head = tmpvpcidev->next;
929 				break;
930 			}
931 		}
932 		write_unlock_irqrestore(&vpcidev_list_lock, flags);
933 		kfree(virtpcidev);
934 		return 0;
935 	}
936 
937 	POSTCODE_LINUX_2(VPCI_CREATE_EXIT_PC, POSTCODE_SEVERITY_INFO);
938 	return 1;
939 }
940 
virtpci_device_serverdown(struct device * parentbus,int devtype,struct vhba_wwnn * wwnn,unsigned char macaddr[])941 static int virtpci_device_serverdown(struct device *parentbus,
942 				     int devtype,
943 				     struct vhba_wwnn *wwnn,
944 				     unsigned char macaddr[])
945 {
946 	int pausethisone = 0;
947 	bool found = false;
948 	struct virtpci_dev *tmpvpcidev, *prevvpcidev;
949 	struct virtpci_driver *vpcidriver;
950 	unsigned long flags;
951 	int rc = 0;
952 
953 	if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE))
954 		return 0;
955 
956 	/* find the vhba or vnic in virtpci device list */
957 	write_lock_irqsave(&vpcidev_list_lock, flags);
958 
959 	for (tmpvpcidev = vpcidev_list_head, prevvpcidev = NULL;
960 	     (tmpvpcidev && !found);
961 	     prevvpcidev = tmpvpcidev, tmpvpcidev = tmpvpcidev->next) {
962 		if (tmpvpcidev->devtype != devtype)
963 			continue;
964 
965 		if (devtype == VIRTHBA_TYPE) {
966 			pausethisone =
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
971 			*/
972 		} else {	/* VIRTNIC_TYPE */
973 			pausethisone =
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 */
978 		}
979 
980 		if (!pausethisone)
981 			continue;
982 
983 		found = true;
984 		vpcidriver = tmpvpcidev->mydriver;
985 		rc = vpcidriver->suspend(tmpvpcidev, 0);
986 	}
987 	write_unlock_irqrestore(&vpcidev_list_lock, flags);
988 
989 	if (!found)
990 		return 0;
991 
992 	return rc;
993 }
994 
virtpci_device_serverup(struct device * parentbus,int devtype,struct vhba_wwnn * wwnn,unsigned char macaddr[])995 static int virtpci_device_serverup(struct device *parentbus,
996 				   int devtype,
997 				   struct vhba_wwnn *wwnn,
998 				   unsigned char macaddr[])
999 {
1000 	int resumethisone = 0;
1001 	bool found = false;
1002 	struct virtpci_dev *tmpvpcidev, *prevvpcidev;
1003 	struct virtpci_driver *vpcidriver;
1004 	unsigned long flags;
1005 	int rc = 0;
1006 
1007 	if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE))
1008 		return 0;
1009 
1010 
1011 	/* find the vhba or vnic in virtpci device list */
1012 	write_lock_irqsave(&vpcidev_list_lock, flags);
1013 
1014 	for (tmpvpcidev = vpcidev_list_head, prevvpcidev = NULL;
1015 	     (tmpvpcidev && !found);
1016 	     prevvpcidev = tmpvpcidev, tmpvpcidev = tmpvpcidev->next) {
1017 		if (tmpvpcidev->devtype != devtype)
1018 			continue;
1019 
1020 		if (devtype == VIRTHBA_TYPE) {
1021 			resumethisone =
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 */
1027 			resumethisone =
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 */
1032 		}
1033 
1034 		if (!resumethisone)
1035 			continue;
1036 
1037 		found = true;
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.
1044 		*/
1045 		fix_vbus_dev_info(&tmpvpcidev->generic_dev,
1046 				  tmpvpcidev->device_no,
1047 				  tmpvpcidev->device, vpcidriver);
1048 		rc = vpcidriver->resume(tmpvpcidev);
1049 	}
1050 
1051 	write_unlock_irqrestore(&vpcidev_list_lock, flags);
1052 
1053 	if (!found)
1054 		return 0;
1055 
1056 	return rc;
1057 }
1058 
virtpci_device_del(struct device * parentbus,int devtype,struct vhba_wwnn * wwnn,unsigned char macaddr[])1059 static int virtpci_device_del(struct device *parentbus,
1060 			      int devtype, struct vhba_wwnn *wwnn,
1061 			      unsigned char macaddr[])
1062 {
1063 	int count = 0, all = 0, delthisone;
1064 	struct virtpci_dev *tmpvpcidev, *prevvpcidev, *dellist = NULL;
1065 	unsigned long flags;
1066 
1067 #define DEL_CONTINUE { \
1068 	prevvpcidev = tmpvpcidev;\
1069 	tmpvpcidev = tmpvpcidev->next;\
1070 	continue; \
1071 }
1072 
1073 	if ((devtype != VIRTHBA_TYPE) && (devtype != VIRTNIC_TYPE))
1074 		return 0;
1075 
1076 	/* see if we are to delete all - NOTE: all implies we have a
1077 	 * valid parentbus
1078 	 */
1079 	all = ((devtype == VIRTHBA_TYPE) && (!wwnn)) ||
1080 	    ((devtype == VIRTNIC_TYPE) && (!macaddr));
1081 
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"
1086 	*/
1087 	write_lock_irqsave(&vpcidev_list_lock, flags);
1088 	for (tmpvpcidev = vpcidev_list_head, prevvpcidev = NULL; tmpvpcidev;) {
1089 		if (tmpvpcidev->devtype != devtype)
1090 			DEL_CONTINUE;
1091 
1092 		if (all) {
1093 			delthisone =
1094 			    (tmpvpcidev->generic_dev.parent == parentbus);
1095 			/* we're deleting all vhbas or vnics on the
1096 			 * specified parent bus
1097 			 */
1098 		} else if (devtype == VIRTHBA_TYPE) {
1099 			delthisone =
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
1104 			 */
1105 		} else {	/* VIRTNIC_TYPE */
1106 			delthisone =
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
1111 			*/
1112 		}
1113 
1114 		if (!delthisone)
1115 			DEL_CONTINUE;
1116 
1117 		/* take vhba/vnic out of the list */
1118 		if (prevvpcidev)
1119 			/* not at head */
1120 			prevvpcidev->next = tmpvpcidev->next;
1121 		else
1122 			vpcidev_list_head = tmpvpcidev->next;
1123 
1124 		/* add it to our deletelist */
1125 		tmpvpcidev->next = dellist;
1126 		dellist = tmpvpcidev;
1127 
1128 		count++;
1129 		if (!all)
1130 			break;	/* done */
1131 		/* going to top of loop again - set tmpvpcidev to next
1132 		 * one we're to process
1133 		 */
1134 		if (prevvpcidev)
1135 			tmpvpcidev = prevvpcidev->next;
1136 		else
1137 			tmpvpcidev = vpcidev_list_head;
1138 	}
1139 	write_unlock_irqrestore(&vpcidev_list_lock, flags);
1140 
1141 	if (!all && (count == 0))
1142 		return 0;
1143 
1144 	/* now delete each one from delete list */
1145 	while (dellist) {
1146 		/* save next */
1147 		tmpvpcidev = dellist->next;
1148 		/* delete the vhba/vnic at dellist */
1149 		DELETE_ONE_VPCIDEV(dellist);
1150 		/* do next */
1151 		dellist = tmpvpcidev;
1152 	}
1153 
1154 	return count;
1155 }
1156 
virtpci_device_release(struct device * dev_)1157 static void virtpci_device_release(struct device *dev_)
1158 {
1159 	/* this function is called when the last reference to the
1160 	 * device is removed
1161 	 */
1162 }
1163 
1164 /*****************************************************/
1165 /* Driver functions                                  */
1166 /*****************************************************/
1167 
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)
1171 
virtpci_driver_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1172 static ssize_t virtpci_driver_attr_show(struct kobject *kobj,
1173 					struct attribute *attr,
1174 					char *buf)
1175 {
1176 	struct driver_attribute *dattr = attribute_to_driver_attribute(attr);
1177 	ssize_t ret = 0;
1178 
1179 	struct driver_private *dprivate = to_driver(kobj);
1180 	struct device_driver *driver = dprivate->driver;
1181 
1182 	if (dattr->show)
1183 		ret = dattr->show(driver, buf);
1184 
1185 	return ret;
1186 }
1187 
virtpci_driver_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)1188 static ssize_t virtpci_driver_attr_store(struct kobject *kobj,
1189 					 struct attribute *attr,
1190 					 const char *buf, size_t count)
1191 {
1192 	struct driver_attribute *dattr = attribute_to_driver_attribute(attr);
1193 	ssize_t ret = 0;
1194 
1195 	struct driver_private *dprivate = to_driver(kobj);
1196 	struct device_driver *driver = dprivate->driver;
1197 
1198 	if (dattr->store)
1199 		ret = dattr->store(driver, buf, count);
1200 
1201 	return ret;
1202 }
1203 
1204 /* register a new virtpci driver */
virtpci_register_driver(struct virtpci_driver * drv)1205 int virtpci_register_driver(struct virtpci_driver *drv)
1206 {
1207 	int result = 0;
1208 
1209 	if (!drv->id_table)
1210 		return 1;
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
1216 							 * existence of a
1217 							 * specific device and
1218 							 * whether this driver
1219 							 *can work with it */
1220 	drv->core_driver.remove = virtpci_device_remove; /* called when the
1221 							  * device is removed
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
1226 	 * module_add_driver
1227 	 */
1228 	if (result)
1229 		return result;	/* failed */
1230 
1231 	drv->core_driver.p->kobj.ktype = &virtpci_driver_kobj_type;
1232 
1233 	return 0;
1234 }
1235 EXPORT_SYMBOL_GPL(virtpci_register_driver);
1236 
virtpci_unregister_driver(struct virtpci_driver * drv)1237 void virtpci_unregister_driver(struct virtpci_driver *drv)
1238 {
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
1243 	 * driver's devices
1244 	 * device_release driver calls drv->remove which is
1245 	 * virtpci_device_remove
1246 	 * virtpci_device_remove calls virthba_remove
1247 	 */
1248 }
1249 EXPORT_SYMBOL_GPL(virtpci_unregister_driver);
1250 
1251 /*****************************************************/
1252 /* debugfs filesystem functions                      */
1253 /*****************************************************/
1254 struct print_vbus_info {
1255 	int *str_pos;
1256 	char *buf;
1257 	size_t *len;
1258 };
1259 
print_vbus(struct device * vbus,void * data)1260 static int print_vbus(struct device *vbus, void *data)
1261 {
1262 	struct print_vbus_info *p = (struct print_vbus_info *)data;
1263 
1264 	*p->str_pos += scnprintf(p->buf + *p->str_pos, *p->len - *p->str_pos,
1265 				"bus_id:%s\n", dev_name(vbus));
1266 	return 0;
1267 }
1268 
info_debugfs_read(struct file * file,char __user * buf,size_t len,loff_t * offset)1269 static ssize_t info_debugfs_read(struct file *file, char __user *buf,
1270 				 size_t len, loff_t *offset)
1271 {
1272 	ssize_t bytes_read = 0;
1273 	int str_pos = 0;
1274 	struct virtpci_dev *tmpvpcidev;
1275 	unsigned long flags;
1276 	struct print_vbus_info printparam;
1277 	char *vbuf;
1278 
1279 	if (len > MAX_BUF)
1280 		len = MAX_BUF;
1281 	vbuf = kzalloc(len, GFP_KERNEL);
1282 	if (!vbuf)
1283 		return -ENOMEM;
1284 
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,
1291 			 print_vbus);
1292 
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",
1301 					tmpvpcidev->bus_no,
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);
1309 		} else {
1310 			str_pos += scnprintf(vbuf + str_pos, len - str_pos,
1311 					"[%d:%d] VNic:%pM num_rcv_bufs:%d mtu:%d",
1312 					tmpvpcidev->bus_no,
1313 					tmpvpcidev->device_no,
1314 					tmpvpcidev->net.mac_addr,
1315 					tmpvpcidev->net.num_rcv_bufs,
1316 					tmpvpcidev->net.mtu);
1317 		}
1318 		str_pos += scnprintf(vbuf + str_pos,
1319 				len - str_pos, " chanptr:%p\n",
1320 				tmpvpcidev->queueinfo.chan);
1321 				tmpvpcidev = tmpvpcidev->next;
1322 	}
1323 	read_unlock_irqrestore(&vpcidev_list_lock, flags);
1324 
1325 	str_pos += scnprintf(vbuf + str_pos, len - str_pos, "\n");
1326 	bytes_read = simple_read_from_buffer(buf, len, offset, vbuf, str_pos);
1327 	kfree(vbuf);
1328 	return bytes_read;
1329 }
1330 
1331 /*****************************************************/
1332 /* Module Init & Exit functions                      */
1333 /*****************************************************/
1334 
virtpci_mod_init(void)1335 static int __init virtpci_mod_init(void)
1336 {
1337 	int ret;
1338 
1339 	if (!unisys_spar_platform)
1340 		return -ENODEV;
1341 
1342 	POSTCODE_LINUX_2(VPCI_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1343 
1344 	ret = bus_register(&virtpci_bus_type);
1345 	/* creates /sys/bus/uisvirtpci which contains devices &
1346 	 * drivers directory
1347 	 */
1348 	if (ret) {
1349 		POSTCODE_LINUX_3(VPCI_CREATE_FAILURE_PC, ret,
1350 				 POSTCODE_SEVERITY_ERR);
1351 		return ret;
1352 	}
1353 	bus_device_info_init(&bus_driver_info, "clientbus", "virtpci",
1354 			     VERSION, NULL);
1355 
1356 	/* create a root bus used to parent all the virtpci buses. */
1357 	ret = device_register(&virtpci_rootbus_device);
1358 	if (ret) {
1359 		bus_unregister(&virtpci_bus_type);
1360 		POSTCODE_LINUX_3(VPCI_CREATE_FAILURE_PC, ret,
1361 				 POSTCODE_SEVERITY_ERR);
1362 		return ret;
1363 	}
1364 
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);
1370 		return -1;
1371 	}
1372 
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);
1378 	return 0;
1379 }
1380 
virtpci_mod_exit(void)1381 static void __exit virtpci_mod_exit(void)
1382 {
1383 	/* unregister the callback function */
1384 	device_unregister(&virtpci_rootbus_device);
1385 	bus_unregister(&virtpci_bus_type);
1386 	debugfs_remove_recursive(virtpci_debugfs_dir);
1387 }
1388 
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");
1394 
1395