blob: a6a29a24d28a4bfc84f2dfd4142b39ceb34657ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02002 * This file is part of the zfcp device driver for
3 * FCP adapters for IBM System z9 and zSeries.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andreas Herrmann4a9d2d82006-05-22 18:14:08 +02005 * (C) Copyright IBM Corp. 2002, 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "zfcp_ext.h"
23
24#define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
25
26static int zfcp_ccw_probe(struct ccw_device *);
27static void zfcp_ccw_remove(struct ccw_device *);
28static int zfcp_ccw_set_online(struct ccw_device *);
29static int zfcp_ccw_set_offline(struct ccw_device *);
30static int zfcp_ccw_notify(struct ccw_device *, int);
Cornelia Huck958974fb2007-10-12 16:11:21 +020031static void zfcp_ccw_shutdown(struct ccw_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33static struct ccw_device_id zfcp_ccw_device_id[] = {
34 {CCW_DEVICE_DEVTYPE(ZFCP_CONTROL_UNIT_TYPE,
35 ZFCP_CONTROL_UNIT_MODEL,
36 ZFCP_DEVICE_TYPE,
37 ZFCP_DEVICE_MODEL)},
38 {CCW_DEVICE_DEVTYPE(ZFCP_CONTROL_UNIT_TYPE,
39 ZFCP_CONTROL_UNIT_MODEL,
40 ZFCP_DEVICE_TYPE,
41 ZFCP_DEVICE_MODEL_PRIV)},
42 {},
43};
44
45static struct ccw_driver zfcp_ccw_driver = {
46 .owner = THIS_MODULE,
47 .name = ZFCP_NAME,
48 .ids = zfcp_ccw_device_id,
49 .probe = zfcp_ccw_probe,
50 .remove = zfcp_ccw_remove,
51 .set_online = zfcp_ccw_set_online,
52 .set_offline = zfcp_ccw_set_offline,
53 .notify = zfcp_ccw_notify,
Cornelia Huck958974fb2007-10-12 16:11:21 +020054 .shutdown = zfcp_ccw_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055};
56
57MODULE_DEVICE_TABLE(ccw, zfcp_ccw_device_id);
58
59/**
60 * zfcp_ccw_probe - probe function of zfcp driver
61 * @ccw_device: pointer to belonging ccw device
62 *
63 * This function gets called by the common i/o layer and sets up the initial
64 * data structures for each fcp adapter, which was detected by the system.
65 * Also the sysfs files for this adapter will be created by this function.
66 * In addition the nameserver port will be added to the ports of the adapter
67 * and its sysfs representation will be created too.
68 */
69static int
70zfcp_ccw_probe(struct ccw_device *ccw_device)
71{
72 struct zfcp_adapter *adapter;
73 int retval = 0;
74
75 down(&zfcp_data.config_sema);
76 adapter = zfcp_adapter_enqueue(ccw_device);
77 if (!adapter)
78 retval = -EINVAL;
79 else
80 ZFCP_LOG_DEBUG("Probed adapter %s\n",
81 zfcp_get_busid_by_adapter(adapter));
82 up(&zfcp_data.config_sema);
83 return retval;
84}
85
86/**
87 * zfcp_ccw_remove - remove function of zfcp driver
88 * @ccw_device: pointer to belonging ccw device
89 *
90 * This function gets called by the common i/o layer and removes an adapter
91 * from the system. Task of this function is to get rid of all units and
92 * ports that belong to this adapter. And in addition all resources of this
93 * adapter will be freed too.
94 */
95static void
96zfcp_ccw_remove(struct ccw_device *ccw_device)
97{
98 struct zfcp_adapter *adapter;
99 struct zfcp_port *port, *p;
100 struct zfcp_unit *unit, *u;
101
102 ccw_device_set_offline(ccw_device);
103 down(&zfcp_data.config_sema);
104 adapter = dev_get_drvdata(&ccw_device->dev);
105
106 ZFCP_LOG_DEBUG("Removing adapter %s\n",
107 zfcp_get_busid_by_adapter(adapter));
108 write_lock_irq(&zfcp_data.config_lock);
109 list_for_each_entry_safe(port, p, &adapter->port_list_head, list) {
110 list_for_each_entry_safe(unit, u, &port->unit_list_head, list) {
111 list_move(&unit->list, &port->unit_remove_lh);
112 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE,
113 &unit->status);
114 }
115 list_move(&port->list, &adapter->port_remove_lh);
116 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
117 }
118 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
119 write_unlock_irq(&zfcp_data.config_lock);
120
121 list_for_each_entry_safe(port, p, &adapter->port_remove_lh, list) {
122 list_for_each_entry_safe(unit, u, &port->unit_remove_lh, list) {
Christof Schmitte39c8872007-11-05 12:37:46 +0100123 if (atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
124 &unit->status))
125 scsi_remove_device(unit->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 zfcp_unit_dequeue(unit);
127 }
128 zfcp_port_dequeue(port);
129 }
130 zfcp_adapter_wait(adapter);
131 zfcp_adapter_dequeue(adapter);
132
133 up(&zfcp_data.config_sema);
134}
135
136/**
137 * zfcp_ccw_set_online - set_online function of zfcp driver
138 * @ccw_device: pointer to belonging ccw device
139 *
140 * This function gets called by the common i/o layer and sets an adapter
141 * into state online. Setting an fcp device online means that it will be
142 * registered with the SCSI stack, that the QDIO queues will be set up
143 * and that the adapter will be opened (asynchronously).
144 */
145static int
146zfcp_ccw_set_online(struct ccw_device *ccw_device)
147{
148 struct zfcp_adapter *adapter;
149 int retval;
150
151 down(&zfcp_data.config_sema);
152 adapter = dev_get_drvdata(&ccw_device->dev);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 retval = zfcp_erp_thread_setup(adapter);
155 if (retval) {
156 ZFCP_LOG_INFO("error: start of error recovery thread for "
157 "adapter %s failed\n",
158 zfcp_get_busid_by_adapter(adapter));
Christof Schmittff17a292007-08-28 09:31:41 +0200159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162 retval = zfcp_adapter_scsi_register(adapter);
163 if (retval)
164 goto out_scsi_register;
Volker Sameskefea9d6c2006-08-02 11:05:16 +0200165
166 /* initialize request counter */
167 BUG_ON(!zfcp_reqlist_isempty(adapter));
168 adapter->req_no = 0;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 zfcp_erp_modify_adapter_status(adapter, ZFCP_STATUS_COMMON_RUNNING,
171 ZFCP_SET);
172 zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED);
173 zfcp_erp_wait(adapter);
174 goto out;
175
176 out_scsi_register:
177 zfcp_erp_thread_kill(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 out:
179 up(&zfcp_data.config_sema);
180 return retval;
181}
182
183/**
184 * zfcp_ccw_set_offline - set_offline function of zfcp driver
185 * @ccw_device: pointer to belonging ccw device
186 *
187 * This function gets called by the common i/o layer and sets an adapter
Michael Loehr9f287452007-05-09 11:01:24 +0200188 * into state offline.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
190static int
191zfcp_ccw_set_offline(struct ccw_device *ccw_device)
192{
193 struct zfcp_adapter *adapter;
194
195 down(&zfcp_data.config_sema);
196 adapter = dev_get_drvdata(&ccw_device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 zfcp_erp_adapter_shutdown(adapter, 0);
198 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 zfcp_erp_thread_kill(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 up(&zfcp_data.config_sema);
201 return 0;
202}
203
204/**
205 * zfcp_ccw_notify
206 * @ccw_device: pointer to belonging ccw device
207 * @event: indicates if adapter was detached or attached
208 *
209 * This function gets called by the common i/o layer if an adapter has gone
210 * or reappeared.
211 */
212static int
213zfcp_ccw_notify(struct ccw_device *ccw_device, int event)
214{
215 struct zfcp_adapter *adapter;
216
217 down(&zfcp_data.config_sema);
218 adapter = dev_get_drvdata(&ccw_device->dev);
219 switch (event) {
220 case CIO_GONE:
221 ZFCP_LOG_NORMAL("adapter %s: device gone\n",
222 zfcp_get_busid_by_adapter(adapter));
223 debug_text_event(adapter->erp_dbf,1,"dev_gone");
224 zfcp_erp_adapter_shutdown(adapter, 0);
225 break;
226 case CIO_NO_PATH:
227 ZFCP_LOG_NORMAL("adapter %s: no path\n",
228 zfcp_get_busid_by_adapter(adapter));
229 debug_text_event(adapter->erp_dbf,1,"no_path");
230 zfcp_erp_adapter_shutdown(adapter, 0);
231 break;
232 case CIO_OPER:
233 ZFCP_LOG_NORMAL("adapter %s: operational again\n",
234 zfcp_get_busid_by_adapter(adapter));
235 debug_text_event(adapter->erp_dbf,1,"dev_oper");
236 zfcp_erp_modify_adapter_status(adapter,
237 ZFCP_STATUS_COMMON_RUNNING,
238 ZFCP_SET);
239 zfcp_erp_adapter_reopen(adapter,
240 ZFCP_STATUS_COMMON_ERP_FAILED);
241 break;
242 }
243 zfcp_erp_wait(adapter);
244 up(&zfcp_data.config_sema);
245 return 1;
246}
247
248/**
249 * zfcp_ccw_register - ccw register function
250 *
251 * Registers the driver at the common i/o layer. This function will be called
252 * at module load time/system start.
253 */
254int __init
255zfcp_ccw_register(void)
256{
257 int retval;
258
259 retval = ccw_driver_register(&zfcp_ccw_driver);
260 if (retval)
261 goto out;
262 retval = zfcp_sysfs_driver_create_files(&zfcp_ccw_driver.driver);
263 if (retval)
264 ccw_driver_unregister(&zfcp_ccw_driver);
265 out:
266 return retval;
267}
268
269/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * zfcp_ccw_shutdown - gets called on reboot/shutdown
271 *
272 * Makes sure that QDIO queues are down when the system gets stopped.
273 */
274static void
Cornelia Huck958974fb2007-10-12 16:11:21 +0200275zfcp_ccw_shutdown(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct zfcp_adapter *adapter;
278
279 down(&zfcp_data.config_sema);
Cornelia Huck958974fb2007-10-12 16:11:21 +0200280 adapter = dev_get_drvdata(&cdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 zfcp_erp_adapter_shutdown(adapter, 0);
282 zfcp_erp_wait(adapter);
283 up(&zfcp_data.config_sema);
284}
285
286#undef ZFCP_LOG_AREA