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