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