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