blob: 733fe3bf6285f0e20c6d6737b600beac7ec7332e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Christof Schmittfa04c282008-06-10 18:20:56 +02002 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Christof Schmittfa04c282008-06-10 18:20:56 +02004 * Registration and callback for the s390 common I/O layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Swen Schillig6d1a27f2009-03-02 13:09:11 +01006 * Copyright IBM Corporation 2002, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Christof Schmittecf39d42008-12-25 13:39:53 +01009#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "zfcp_ext.h"
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/**
15 * zfcp_ccw_probe - probe function of zfcp driver
16 * @ccw_device: pointer to belonging ccw device
17 *
18 * This function gets called by the common i/o layer and sets up the initial
19 * data structures for each fcp adapter, which was detected by the system.
20 * Also the sysfs files for this adapter will be created by this function.
21 * In addition the nameserver port will be added to the ports of the adapter
22 * and its sysfs representation will be created too.
23 */
Christof Schmittfa04c282008-06-10 18:20:56 +020024static int zfcp_ccw_probe(struct ccw_device *ccw_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int retval = 0;
27
28 down(&zfcp_data.config_sema);
Swen Schillig317e6b62008-07-02 10:56:37 +020029 if (zfcp_adapter_enqueue(ccw_device)) {
Christof Schmittfa04c282008-06-10 18:20:56 +020030 dev_err(&ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +020031 "Setting up data structures for the "
32 "FCP adapter failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 retval = -EINVAL;
Christof Schmittfa04c282008-06-10 18:20:56 +020034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 up(&zfcp_data.config_sema);
36 return retval;
37}
38
39/**
40 * zfcp_ccw_remove - remove function of zfcp driver
41 * @ccw_device: pointer to belonging ccw device
42 *
43 * This function gets called by the common i/o layer and removes an adapter
44 * from the system. Task of this function is to get rid of all units and
45 * ports that belong to this adapter. And in addition all resources of this
46 * adapter will be freed too.
47 */
Christof Schmittfa04c282008-06-10 18:20:56 +020048static void zfcp_ccw_remove(struct ccw_device *ccw_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct zfcp_adapter *adapter;
51 struct zfcp_port *port, *p;
52 struct zfcp_unit *unit, *u;
Christof Schmitt04062892008-10-01 12:42:20 +020053 LIST_HEAD(unit_remove_lh);
54 LIST_HEAD(port_remove_lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 ccw_device_set_offline(ccw_device);
57 down(&zfcp_data.config_sema);
58 adapter = dev_get_drvdata(&ccw_device->dev);
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 write_lock_irq(&zfcp_data.config_lock);
61 list_for_each_entry_safe(port, p, &adapter->port_list_head, list) {
62 list_for_each_entry_safe(unit, u, &port->unit_list_head, list) {
Christof Schmitt04062892008-10-01 12:42:20 +020063 list_move(&unit->list, &unit_remove_lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE,
65 &unit->status);
66 }
Christof Schmitt04062892008-10-01 12:42:20 +020067 list_move(&port->list, &port_remove_lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
69 }
70 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
71 write_unlock_irq(&zfcp_data.config_lock);
72
Christof Schmitt04062892008-10-01 12:42:20 +020073 list_for_each_entry_safe(port, p, &port_remove_lh, list) {
74 list_for_each_entry_safe(unit, u, &unit_remove_lh, list) {
Christof Schmitt86f8a1b2009-03-02 13:08:55 +010075 if (unit->device)
Christof Schmitte39c8872007-11-05 12:37:46 +010076 scsi_remove_device(unit->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 zfcp_unit_dequeue(unit);
78 }
79 zfcp_port_dequeue(port);
80 }
Swen Schillig44cc76f2008-10-01 12:42:16 +020081 wait_event(adapter->remove_wq, atomic_read(&adapter->refcount) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 zfcp_adapter_dequeue(adapter);
83
84 up(&zfcp_data.config_sema);
85}
86
87/**
88 * zfcp_ccw_set_online - set_online function of zfcp driver
89 * @ccw_device: pointer to belonging ccw device
90 *
91 * This function gets called by the common i/o layer and sets an adapter
92 * into state online. Setting an fcp device online means that it will be
93 * registered with the SCSI stack, that the QDIO queues will be set up
94 * and that the adapter will be opened (asynchronously).
95 */
Christof Schmittfa04c282008-06-10 18:20:56 +020096static int zfcp_ccw_set_online(struct ccw_device *ccw_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct zfcp_adapter *adapter;
99 int retval;
100
101 down(&zfcp_data.config_sema);
102 adapter = dev_get_drvdata(&ccw_device->dev);
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 retval = zfcp_erp_thread_setup(adapter);
Christof Schmittfa04c282008-06-10 18:20:56 +0200105 if (retval)
Christof Schmittff17a292007-08-28 09:31:41 +0200106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200108 /* initialize request counter */
109 BUG_ON(!zfcp_reqlist_isempty(adapter));
110 adapter->req_no = 0;
111
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100112 zfcp_erp_modify_adapter_status(adapter, "ccsonl1", NULL,
Martin Peschke698ec0162008-03-27 14:22:02 +0100113 ZFCP_STATUS_COMMON_RUNNING, ZFCP_SET);
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100114 zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED,
115 "ccsonl2", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 zfcp_erp_wait(adapter);
Christof Schmitt77fd9492008-11-04 16:35:10 +0100117 up(&zfcp_data.config_sema);
118 flush_work(&adapter->scan_work);
119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 out:
122 up(&zfcp_data.config_sema);
123 return retval;
124}
125
126/**
127 * zfcp_ccw_set_offline - set_offline function of zfcp driver
128 * @ccw_device: pointer to belonging ccw device
129 *
130 * This function gets called by the common i/o layer and sets an adapter
Michael Loehr9f287452007-05-09 11:01:24 +0200131 * into state offline.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
Christof Schmittfa04c282008-06-10 18:20:56 +0200133static int zfcp_ccw_set_offline(struct ccw_device *ccw_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct zfcp_adapter *adapter;
136
137 down(&zfcp_data.config_sema);
138 adapter = dev_get_drvdata(&ccw_device->dev);
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100139 zfcp_erp_adapter_shutdown(adapter, 0, "ccsoff1", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 zfcp_erp_thread_kill(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 up(&zfcp_data.config_sema);
143 return 0;
144}
145
146/**
Christof Schmittfa04c282008-06-10 18:20:56 +0200147 * zfcp_ccw_notify - ccw notify function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 * @ccw_device: pointer to belonging ccw device
149 * @event: indicates if adapter was detached or attached
150 *
151 * This function gets called by the common i/o layer if an adapter has gone
152 * or reappeared.
153 */
Christof Schmittfa04c282008-06-10 18:20:56 +0200154static int zfcp_ccw_notify(struct ccw_device *ccw_device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Christof Schmittf48bf7f2008-08-21 13:43:34 +0200156 struct zfcp_adapter *adapter = dev_get_drvdata(&ccw_device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 switch (event) {
159 case CIO_GONE:
Christof Schmittff3b24f2008-10-01 12:42:15 +0200160 dev_warn(&adapter->ccw_device->dev,
161 "The FCP device has been detached\n");
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100162 zfcp_erp_adapter_shutdown(adapter, 0, "ccnoti1", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 break;
164 case CIO_NO_PATH:
Christof Schmittff3b24f2008-10-01 12:42:15 +0200165 dev_warn(&adapter->ccw_device->dev,
166 "The CHPID for the FCP device is offline\n");
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100167 zfcp_erp_adapter_shutdown(adapter, 0, "ccnoti2", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 break;
169 case CIO_OPER:
Christof Schmittff3b24f2008-10-01 12:42:15 +0200170 dev_info(&adapter->ccw_device->dev,
171 "The FCP device is operational again\n");
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100172 zfcp_erp_modify_adapter_status(adapter, "ccnoti3", NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 ZFCP_STATUS_COMMON_RUNNING,
174 ZFCP_SET);
Martin Peschke1f6f7122008-04-18 12:51:55 +0200175 zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100176 "ccnoti4", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 break;
Sebastian Ott47593bf2009-03-31 19:16:05 +0200178 case CIO_BOXED:
179 dev_warn(&adapter->ccw_device->dev,
180 "The ccw device did not respond in time.\n");
181 zfcp_erp_adapter_shutdown(adapter, 0, "ccnoti5", NULL);
182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return 1;
185}
186
187/**
Christof Schmittfa04c282008-06-10 18:20:56 +0200188 * zfcp_ccw_shutdown - handle shutdown from cio
189 * @cdev: device for adapter to shutdown.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
Christof Schmittfa04c282008-06-10 18:20:56 +0200191static void zfcp_ccw_shutdown(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct zfcp_adapter *adapter;
194
195 down(&zfcp_data.config_sema);
Cornelia Huck958974fb2007-10-12 16:11:21 +0200196 adapter = dev_get_drvdata(&cdev->dev);
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100197 zfcp_erp_adapter_shutdown(adapter, 0, "ccshut1", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 zfcp_erp_wait(adapter);
199 up(&zfcp_data.config_sema);
200}
201
Christof Schmittfa04c282008-06-10 18:20:56 +0200202static struct ccw_device_id zfcp_ccw_device_id[] = {
203 { CCW_DEVICE_DEVTYPE(0x1731, 0x3, 0x1732, 0x3) },
204 { CCW_DEVICE_DEVTYPE(0x1731, 0x3, 0x1732, 0x4) }, /* priv. */
205 {},
206};
207
208MODULE_DEVICE_TABLE(ccw, zfcp_ccw_device_id);
209
210static struct ccw_driver zfcp_ccw_driver = {
211 .owner = THIS_MODULE,
212 .name = "zfcp",
213 .ids = zfcp_ccw_device_id,
214 .probe = zfcp_ccw_probe,
215 .remove = zfcp_ccw_remove,
216 .set_online = zfcp_ccw_set_online,
217 .set_offline = zfcp_ccw_set_offline,
218 .notify = zfcp_ccw_notify,
219 .shutdown = zfcp_ccw_shutdown,
220};
221
222/**
223 * zfcp_ccw_register - ccw register function
224 *
225 * Registers the driver at the common i/o layer. This function will be called
226 * at module load time/system start.
227 */
228int __init zfcp_ccw_register(void)
229{
230 return ccw_driver_register(&zfcp_ccw_driver);
231}
Swen Schilliga1b449d2008-10-01 12:42:19 +0200232
233/**
234 * zfcp_get_adapter_by_busid - find zfcp_adapter struct
235 * @busid: bus id string of zfcp adapter to find
236 */
237struct zfcp_adapter *zfcp_get_adapter_by_busid(char *busid)
238{
239 struct ccw_device *ccw_device;
240 struct zfcp_adapter *adapter = NULL;
241
242 ccw_device = get_ccwdev_by_busid(&zfcp_ccw_driver, busid);
243 if (ccw_device) {
244 adapter = dev_get_drvdata(&ccw_device->dev);
245 put_device(&ccw_device->dev);
246 }
247 return adapter;
248}