ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / s390 / scsi / zfcp_ccw.c
1 /*
2  * linux/drivers/s390/scsi/zfcp_ccw.c
3  *
4  * FCP adapter driver for IBM eServer zSeries
5  *
6  * CCW driver related routines
7  *
8  * (C) Copyright IBM Corp. 2003, 2004
9  *
10  * Authors:
11  *      Martin Peschke <mpeschke@de.ibm.com>
12  *      Heiko Carstens <heiko.carstens@de.ibm.com>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  */
28
29 #define ZFCP_CCW_C_REVISION "$Revision: 1.52 $"
30
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <asm/ccwdev.h>
34 #include "zfcp_ext.h"
35 #include "zfcp_def.h"
36
37 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_CONFIG
38
39 static int zfcp_ccw_probe(struct ccw_device *);
40 static void zfcp_ccw_remove(struct ccw_device *);
41 static int zfcp_ccw_set_online(struct ccw_device *);
42 static int zfcp_ccw_set_offline(struct ccw_device *);
43 static int zfcp_ccw_notify(struct ccw_device *, int);
44
45 static struct ccw_device_id zfcp_ccw_device_id[] = {
46         {CCW_DEVICE_DEVTYPE(ZFCP_CONTROL_UNIT_TYPE,
47                             ZFCP_CONTROL_UNIT_MODEL,
48                             ZFCP_DEVICE_TYPE,
49                             ZFCP_DEVICE_MODEL)},
50         {CCW_DEVICE_DEVTYPE(ZFCP_CONTROL_UNIT_TYPE,
51                             ZFCP_CONTROL_UNIT_MODEL,
52                             ZFCP_DEVICE_TYPE,
53                             ZFCP_DEVICE_MODEL_PRIV)},
54         {},
55 };
56
57 static struct ccw_driver zfcp_ccw_driver = {
58         .owner       = THIS_MODULE,
59         .name        = ZFCP_NAME,
60         .ids         = zfcp_ccw_device_id,
61         .probe       = zfcp_ccw_probe,
62         .remove      = zfcp_ccw_remove,
63         .set_online  = zfcp_ccw_set_online,
64         .set_offline = zfcp_ccw_set_offline,
65         .notify      = zfcp_ccw_notify,
66 };
67
68 MODULE_DEVICE_TABLE(ccw, zfcp_ccw_device_id);
69
70 /**
71  * zfcp_ccw_probe - probe function of zfcp driver
72  * @ccw_device: pointer to belonging ccw device
73  *
74  * This function gets called by the common i/o layer and sets up the initial
75  * data structures for each fcp adapter, which was detected by the system.
76  * Also the sysfs files for this adapter will be created by this function.
77  * In addition the nameserver port will be added to the ports of the adapter
78  * and its sysfs representation will be created too.
79  */
80 static int
81 zfcp_ccw_probe(struct ccw_device *ccw_device)
82 {
83         struct zfcp_adapter *adapter;
84         int retval = 0;
85
86         down(&zfcp_data.config_sema);
87         adapter = zfcp_adapter_enqueue(ccw_device);
88         if (!adapter)
89                 retval = -EINVAL;
90         else
91                 ZFCP_LOG_DEBUG("Probed adapter %s\n",
92                                zfcp_get_busid_by_adapter(adapter));
93         up(&zfcp_data.config_sema);
94         return retval;
95 }
96
97 /**
98  * zfcp_ccw_remove - remove function of zfcp driver
99  * @ccw_device: pointer to belonging ccw device
100  *
101  * This function gets called by the common i/o layer and removes an adapter
102  * from the system. Task of this function is to get rid of all units and
103  * ports that belong to this adapter. And in addition all resources of this
104  * adapter will be freed too.
105  */
106 static void
107 zfcp_ccw_remove(struct ccw_device *ccw_device)
108 {
109         struct zfcp_adapter *adapter;
110         struct zfcp_port *port, *p;
111         struct zfcp_unit *unit, *u;
112
113         ccw_device_set_offline(ccw_device);
114         down(&zfcp_data.config_sema);
115         adapter = dev_get_drvdata(&ccw_device->dev);
116
117         ZFCP_LOG_DEBUG("Removing adapter %s\n",
118                        zfcp_get_busid_by_adapter(adapter));
119         write_lock_irq(&zfcp_data.config_lock);
120         list_for_each_entry_safe(port, p, &adapter->port_list_head, list) {
121                 list_for_each_entry_safe(unit, u, &port->unit_list_head, list) {
122                         list_move(&unit->list, &port->unit_remove_lh);
123                         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE,
124                                         &unit->status);
125                 }
126                 list_move(&port->list, &adapter->port_remove_lh);
127                 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
128         }
129         atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
130         write_unlock_irq(&zfcp_data.config_lock);
131
132         list_for_each_entry_safe(port, p, &adapter->port_remove_lh, list) {
133                 list_for_each_entry_safe(unit, u, &port->unit_remove_lh, list) {
134                         zfcp_unit_dequeue(unit);
135                 }
136                 zfcp_port_dequeue(port);
137         }
138         zfcp_adapter_wait(adapter);
139         zfcp_adapter_dequeue(adapter);
140
141         up(&zfcp_data.config_sema);
142 }
143
144 /**
145  * zfcp_ccw_set_online - set_online function of zfcp driver
146  * @ccw_device: pointer to belonging ccw device
147  *
148  * This function gets called by the common i/o layer and sets an adapter
149  * into state online. Setting an fcp device online means that it will be
150  * registered with the SCSI stack, that the QDIO queues will be set up
151  * and that the adapter will be opened (asynchronously).
152  */
153 static int
154 zfcp_ccw_set_online(struct ccw_device *ccw_device)
155 {
156         struct zfcp_adapter *adapter;
157         int retval;
158
159         down(&zfcp_data.config_sema);
160         adapter = dev_get_drvdata(&ccw_device->dev);
161
162         retval = zfcp_adapter_debug_register(adapter);
163         if (retval)
164                 goto out;
165         retval = zfcp_erp_thread_setup(adapter);
166         if (retval) {
167                 ZFCP_LOG_INFO("error: start of error recovery thread for "
168                               "adapter %s failed\n",
169                               zfcp_get_busid_by_adapter(adapter));
170                 goto out_erp_thread;
171         }
172
173         retval = zfcp_adapter_scsi_register(adapter);
174         if (retval)
175                 goto out_scsi_register;
176         zfcp_erp_modify_adapter_status(adapter, ZFCP_STATUS_COMMON_RUNNING,
177                                        ZFCP_SET);
178         zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
179         zfcp_erp_wait(adapter);
180         goto out;
181
182  out_scsi_register:
183         zfcp_erp_thread_kill(adapter);
184  out_erp_thread:
185         zfcp_adapter_debug_unregister(adapter);
186  out:
187         up(&zfcp_data.config_sema);
188         return retval;
189 }
190
191 /**
192  * zfcp_ccw_set_offline - set_offline function of zfcp driver
193  * @ccw_device: pointer to belonging ccw device
194  *
195  * This function gets called by the common i/o layer and sets an adapter
196  * into state offline. Setting an fcp device offline means that it will be
197  * unregistered from the SCSI stack and that the adapter will be shut down
198  * asynchronously.
199  */
200 static int
201 zfcp_ccw_set_offline(struct ccw_device *ccw_device)
202 {
203         struct zfcp_adapter *adapter;
204
205         down(&zfcp_data.config_sema);
206         adapter = dev_get_drvdata(&ccw_device->dev);
207         zfcp_erp_adapter_shutdown(adapter, 0);
208         zfcp_erp_wait(adapter);
209         zfcp_adapter_scsi_unregister(adapter);
210         zfcp_erp_thread_kill(adapter);
211         zfcp_adapter_debug_unregister(adapter);
212         up(&zfcp_data.config_sema);
213         return 0;
214 }
215
216 /**
217  * zfcp_ccw_notify
218  * @ccw_device: pointer to belonging ccw device
219  * @event: indicates if adapter was detached or attached
220  *
221  * This function gets called by the common i/o layer if an adapter has gone
222  * or reappeared.
223  */
224 static int
225 zfcp_ccw_notify(struct ccw_device *ccw_device, int event)
226 {
227         struct zfcp_adapter *adapter;
228
229         down(&zfcp_data.config_sema);
230         adapter = dev_get_drvdata(&ccw_device->dev);
231         switch (event) {
232         case CIO_GONE:
233                 ZFCP_LOG_NORMAL("adapter %s: device gone\n",
234                                 zfcp_get_busid_by_adapter(adapter));
235                 break;
236         case CIO_NO_PATH:
237                 ZFCP_LOG_NORMAL("adapter %s: no path\n",
238                                 zfcp_get_busid_by_adapter(adapter));
239                 break;
240         case CIO_OPER:
241                 ZFCP_LOG_NORMAL("adapter %s: operational again\n",
242                                 zfcp_get_busid_by_adapter(adapter));
243                 zfcp_erp_modify_adapter_status(adapter,
244                                                ZFCP_STATUS_COMMON_RUNNING,
245                                                ZFCP_SET);
246                 zfcp_erp_adapter_reopen(adapter,
247                                         ZFCP_STATUS_COMMON_ERP_FAILED);
248                 break;
249         }
250         up(&zfcp_data.config_sema);
251         return 1;
252 }
253
254 /**
255  * zfcp_ccw_register - ccw register function
256  *
257  * Registers the driver at the common i/o layer. This function will be called
258  * at module load time/system start.
259  */
260 int __init
261 zfcp_ccw_register(void)
262 {
263         int retval;
264
265         retval = ccw_driver_register(&zfcp_ccw_driver);
266         if (retval)
267                 goto out;
268         retval = zfcp_sysfs_driver_create_files(&zfcp_ccw_driver.driver);
269         if (retval)
270                 ccw_driver_unregister(&zfcp_ccw_driver);
271  out:
272         return retval;
273 }
274
275 /**
276  * zfcp_ccw_unregister - ccw unregister function
277  *
278  * Unregisters the driver from common i/o layer. Function will be called at
279  * module unload/system shutdown.
280  */
281 void __exit
282 zfcp_ccw_unregister(void)
283 {
284         zfcp_sysfs_driver_remove_files(&zfcp_ccw_driver.driver);
285         ccw_driver_unregister(&zfcp_ccw_driver);
286 }
287
288 #undef ZFCP_LOG_AREA