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