blob: 4ff914c0f13c16ee824a2d83ba617371a67d084a [file] [log] [blame]
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01001/*
2 * PAV alias management for the DASD ECKD discipline
3 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 2007
Stefan Weinhuber8e09f212008-01-26 14:11:23 +01005 * Author(s): Stefan Weinhuber <wein@de.ibm.com>
6 */
7
Stefan Haberlandca99dab2009-09-11 10:28:30 +02008#define KMSG_COMPONENT "dasd-eckd"
Stefan Haberlandfc19f382009-03-26 15:23:49 +01009
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010010#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010012#include <asm/ebcdic.h>
13#include "dasd_int.h"
14#include "dasd_eckd.h"
15
16#ifdef PRINTK_HEADER
17#undef PRINTK_HEADER
18#endif /* PRINTK_HEADER */
19#define PRINTK_HEADER "dasd(eckd):"
20
21
22/*
23 * General concept of alias management:
24 * - PAV and DASD alias management is specific to the eckd discipline.
25 * - A device is connected to an lcu as long as the device exists.
26 * dasd_alias_make_device_known_to_lcu will be called wenn the
27 * device is checked by the eckd discipline and
28 * dasd_alias_disconnect_device_from_lcu will be called
29 * before the device is deleted.
30 * - The dasd_alias_add_device / dasd_alias_remove_device
31 * functions mark the point when a device is 'ready for service'.
32 * - A summary unit check is a rare occasion, but it is mandatory to
33 * support it. It requires some complex recovery actions before the
34 * devices can be used again (see dasd_alias_handle_summary_unit_check).
35 * - dasd_alias_get_start_dev will find an alias device that can be used
36 * instead of the base device and does some (very simple) load balancing.
37 * This is the function that gets called for each I/O, so when improving
38 * something, this function should get faster or better, the rest has just
39 * to be correct.
40 */
41
42
43static void summary_unit_check_handling_work(struct work_struct *);
44static void lcu_update_work(struct work_struct *);
45static int _schedule_lcu_update(struct alias_lcu *, struct dasd_device *);
46
47static struct alias_root aliastree = {
48 .serverlist = LIST_HEAD_INIT(aliastree.serverlist),
49 .lock = __SPIN_LOCK_UNLOCKED(aliastree.lock),
50};
51
52static struct alias_server *_find_server(struct dasd_uid *uid)
53{
54 struct alias_server *pos;
55 list_for_each_entry(pos, &aliastree.serverlist, server) {
56 if (!strncmp(pos->uid.vendor, uid->vendor,
57 sizeof(uid->vendor))
58 && !strncmp(pos->uid.serial, uid->serial,
59 sizeof(uid->serial)))
60 return pos;
Peter Senna Tschudin3b974872015-08-04 17:11:15 +020061 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010062 return NULL;
63}
64
65static struct alias_lcu *_find_lcu(struct alias_server *server,
66 struct dasd_uid *uid)
67{
68 struct alias_lcu *pos;
69 list_for_each_entry(pos, &server->lculist, lcu) {
70 if (pos->uid.ssid == uid->ssid)
71 return pos;
Peter Senna Tschudin3b974872015-08-04 17:11:15 +020072 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010073 return NULL;
74}
75
76static struct alias_pav_group *_find_group(struct alias_lcu *lcu,
77 struct dasd_uid *uid)
78{
79 struct alias_pav_group *pos;
80 __u8 search_unit_addr;
81
82 /* for hyper pav there is only one group */
83 if (lcu->pav == HYPER_PAV) {
84 if (list_empty(&lcu->grouplist))
85 return NULL;
86 else
87 return list_first_entry(&lcu->grouplist,
88 struct alias_pav_group, group);
89 }
90
91 /* for base pav we have to find the group that matches the base */
92 if (uid->type == UA_BASE_DEVICE)
93 search_unit_addr = uid->real_unit_addr;
94 else
95 search_unit_addr = uid->base_unit_addr;
96 list_for_each_entry(pos, &lcu->grouplist, group) {
Stefan Weinhuber4abb08c2008-08-01 16:39:09 +020097 if (pos->uid.base_unit_addr == search_unit_addr &&
98 !strncmp(pos->uid.vduit, uid->vduit, sizeof(uid->vduit)))
Stefan Weinhuber8e09f212008-01-26 14:11:23 +010099 return pos;
Peter Senna Tschudin3b974872015-08-04 17:11:15 +0200100 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100101 return NULL;
102}
103
104static struct alias_server *_allocate_server(struct dasd_uid *uid)
105{
106 struct alias_server *server;
107
108 server = kzalloc(sizeof(*server), GFP_KERNEL);
109 if (!server)
110 return ERR_PTR(-ENOMEM);
111 memcpy(server->uid.vendor, uid->vendor, sizeof(uid->vendor));
112 memcpy(server->uid.serial, uid->serial, sizeof(uid->serial));
113 INIT_LIST_HEAD(&server->server);
114 INIT_LIST_HEAD(&server->lculist);
115 return server;
116}
117
118static void _free_server(struct alias_server *server)
119{
120 kfree(server);
121}
122
123static struct alias_lcu *_allocate_lcu(struct dasd_uid *uid)
124{
125 struct alias_lcu *lcu;
126
127 lcu = kzalloc(sizeof(*lcu), GFP_KERNEL);
128 if (!lcu)
129 return ERR_PTR(-ENOMEM);
130 lcu->uac = kzalloc(sizeof(*(lcu->uac)), GFP_KERNEL | GFP_DMA);
131 if (!lcu->uac)
132 goto out_err1;
133 lcu->rsu_cqr = kzalloc(sizeof(*lcu->rsu_cqr), GFP_KERNEL | GFP_DMA);
134 if (!lcu->rsu_cqr)
135 goto out_err2;
136 lcu->rsu_cqr->cpaddr = kzalloc(sizeof(struct ccw1),
137 GFP_KERNEL | GFP_DMA);
138 if (!lcu->rsu_cqr->cpaddr)
139 goto out_err3;
140 lcu->rsu_cqr->data = kzalloc(16, GFP_KERNEL | GFP_DMA);
141 if (!lcu->rsu_cqr->data)
142 goto out_err4;
143
144 memcpy(lcu->uid.vendor, uid->vendor, sizeof(uid->vendor));
145 memcpy(lcu->uid.serial, uid->serial, sizeof(uid->serial));
146 lcu->uid.ssid = uid->ssid;
147 lcu->pav = NO_PAV;
148 lcu->flags = NEED_UAC_UPDATE | UPDATE_PENDING;
149 INIT_LIST_HEAD(&lcu->lcu);
150 INIT_LIST_HEAD(&lcu->inactive_devices);
151 INIT_LIST_HEAD(&lcu->active_devices);
152 INIT_LIST_HEAD(&lcu->grouplist);
153 INIT_WORK(&lcu->suc_data.worker, summary_unit_check_handling_work);
154 INIT_DELAYED_WORK(&lcu->ruac_data.dwork, lcu_update_work);
155 spin_lock_init(&lcu->lock);
Stefan Weinhuberf4ac1d02009-12-07 12:51:53 +0100156 init_completion(&lcu->lcu_setup);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100157 return lcu;
158
159out_err4:
160 kfree(lcu->rsu_cqr->cpaddr);
161out_err3:
162 kfree(lcu->rsu_cqr);
163out_err2:
164 kfree(lcu->uac);
165out_err1:
166 kfree(lcu);
167 return ERR_PTR(-ENOMEM);
168}
169
170static void _free_lcu(struct alias_lcu *lcu)
171{
172 kfree(lcu->rsu_cqr->data);
173 kfree(lcu->rsu_cqr->cpaddr);
174 kfree(lcu->rsu_cqr);
175 kfree(lcu->uac);
176 kfree(lcu);
177}
178
179/*
180 * This is the function that will allocate all the server and lcu data,
181 * so this function must be called first for a new device.
182 * If the return value is 1, the lcu was already known before, if it
183 * is 0, this is a new lcu.
184 * Negative return code indicates that something went wrong (e.g. -ENOMEM)
185 */
186int dasd_alias_make_device_known_to_lcu(struct dasd_device *device)
187{
188 struct dasd_eckd_private *private;
189 unsigned long flags;
190 struct alias_server *server, *newserver;
191 struct alias_lcu *lcu, *newlcu;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200192 struct dasd_uid uid;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100193
194 private = (struct dasd_eckd_private *) device->private;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200195
196 device->discipline->get_uid(device, &uid);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100197 spin_lock_irqsave(&aliastree.lock, flags);
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200198 server = _find_server(&uid);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100199 if (!server) {
200 spin_unlock_irqrestore(&aliastree.lock, flags);
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200201 newserver = _allocate_server(&uid);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100202 if (IS_ERR(newserver))
203 return PTR_ERR(newserver);
204 spin_lock_irqsave(&aliastree.lock, flags);
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200205 server = _find_server(&uid);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100206 if (!server) {
207 list_add(&newserver->server, &aliastree.serverlist);
208 server = newserver;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100209 } else {
210 /* someone was faster */
211 _free_server(newserver);
212 }
213 }
214
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200215 lcu = _find_lcu(server, &uid);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100216 if (!lcu) {
217 spin_unlock_irqrestore(&aliastree.lock, flags);
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200218 newlcu = _allocate_lcu(&uid);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100219 if (IS_ERR(newlcu))
Roel Kluin6d53cfe2009-12-18 17:43:17 +0100220 return PTR_ERR(newlcu);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100221 spin_lock_irqsave(&aliastree.lock, flags);
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200222 lcu = _find_lcu(server, &uid);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100223 if (!lcu) {
224 list_add(&newlcu->lcu, &server->lculist);
225 lcu = newlcu;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100226 } else {
227 /* someone was faster */
228 _free_lcu(newlcu);
229 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100230 }
231 spin_lock(&lcu->lock);
232 list_add(&device->alias_list, &lcu->inactive_devices);
233 private->lcu = lcu;
234 spin_unlock(&lcu->lock);
235 spin_unlock_irqrestore(&aliastree.lock, flags);
236
Stefan Haberlandf9f8d022012-01-18 18:03:40 +0100237 return 0;
Stefan Weinhuberf4ac1d02009-12-07 12:51:53 +0100238}
239
240/*
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100241 * This function removes a device from the scope of alias management.
242 * The complicated part is to make sure that it is not in use by
243 * any of the workers. If necessary cancel the work.
244 */
245void dasd_alias_disconnect_device_from_lcu(struct dasd_device *device)
246{
247 struct dasd_eckd_private *private;
248 unsigned long flags;
249 struct alias_lcu *lcu;
250 struct alias_server *server;
251 int was_pending;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200252 struct dasd_uid uid;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100253
254 private = (struct dasd_eckd_private *) device->private;
255 lcu = private->lcu;
Stefan Haberlandf602f6d62011-01-31 11:30:03 +0100256 /* nothing to do if already disconnected */
257 if (!lcu)
258 return;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200259 device->discipline->get_uid(device, &uid);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100260 spin_lock_irqsave(&lcu->lock, flags);
261 list_del_init(&device->alias_list);
262 /* make sure that the workers don't use this device */
263 if (device == lcu->suc_data.device) {
264 spin_unlock_irqrestore(&lcu->lock, flags);
265 cancel_work_sync(&lcu->suc_data.worker);
266 spin_lock_irqsave(&lcu->lock, flags);
267 if (device == lcu->suc_data.device)
268 lcu->suc_data.device = NULL;
269 }
270 was_pending = 0;
271 if (device == lcu->ruac_data.device) {
272 spin_unlock_irqrestore(&lcu->lock, flags);
273 was_pending = 1;
274 cancel_delayed_work_sync(&lcu->ruac_data.dwork);
275 spin_lock_irqsave(&lcu->lock, flags);
276 if (device == lcu->ruac_data.device)
277 lcu->ruac_data.device = NULL;
278 }
279 private->lcu = NULL;
280 spin_unlock_irqrestore(&lcu->lock, flags);
281
282 spin_lock_irqsave(&aliastree.lock, flags);
283 spin_lock(&lcu->lock);
284 if (list_empty(&lcu->grouplist) &&
285 list_empty(&lcu->active_devices) &&
286 list_empty(&lcu->inactive_devices)) {
287 list_del(&lcu->lcu);
288 spin_unlock(&lcu->lock);
289 _free_lcu(lcu);
290 lcu = NULL;
291 } else {
292 if (was_pending)
293 _schedule_lcu_update(lcu, NULL);
294 spin_unlock(&lcu->lock);
295 }
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200296 server = _find_server(&uid);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100297 if (server && list_empty(&server->lculist)) {
298 list_del(&server->server);
299 _free_server(server);
300 }
301 spin_unlock_irqrestore(&aliastree.lock, flags);
302}
303
304/*
305 * This function assumes that the unit address configuration stored
306 * in the lcu is up to date and will update the device uid before
307 * adding it to a pav group.
308 */
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200309
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100310static int _add_device_to_lcu(struct alias_lcu *lcu,
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200311 struct dasd_device *device,
312 struct dasd_device *pos)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100313{
314
315 struct dasd_eckd_private *private;
316 struct alias_pav_group *group;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200317 struct dasd_uid uid;
318 unsigned long flags;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100319
320 private = (struct dasd_eckd_private *) device->private;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200321
322 /* only lock if not already locked */
323 if (device != pos)
324 spin_lock_irqsave_nested(get_ccwdev_lock(device->cdev), flags,
325 CDEV_NESTED_SECOND);
326 private->uid.type = lcu->uac->unit[private->uid.real_unit_addr].ua_type;
327 private->uid.base_unit_addr =
328 lcu->uac->unit[private->uid.real_unit_addr].base_ua;
329 uid = private->uid;
330
331 if (device != pos)
332 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100333
334 /* if we have no PAV anyway, we don't need to bother with PAV groups */
335 if (lcu->pav == NO_PAV) {
336 list_move(&device->alias_list, &lcu->active_devices);
337 return 0;
338 }
339
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200340 group = _find_group(lcu, &uid);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100341 if (!group) {
342 group = kzalloc(sizeof(*group), GFP_ATOMIC);
343 if (!group)
344 return -ENOMEM;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200345 memcpy(group->uid.vendor, uid.vendor, sizeof(uid.vendor));
346 memcpy(group->uid.serial, uid.serial, sizeof(uid.serial));
347 group->uid.ssid = uid.ssid;
348 if (uid.type == UA_BASE_DEVICE)
349 group->uid.base_unit_addr = uid.real_unit_addr;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100350 else
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200351 group->uid.base_unit_addr = uid.base_unit_addr;
352 memcpy(group->uid.vduit, uid.vduit, sizeof(uid.vduit));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100353 INIT_LIST_HEAD(&group->group);
354 INIT_LIST_HEAD(&group->baselist);
355 INIT_LIST_HEAD(&group->aliaslist);
356 list_add(&group->group, &lcu->grouplist);
357 }
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200358 if (uid.type == UA_BASE_DEVICE)
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100359 list_move(&device->alias_list, &group->baselist);
360 else
361 list_move(&device->alias_list, &group->aliaslist);
362 private->pavgroup = group;
363 return 0;
364};
365
366static void _remove_device_from_lcu(struct alias_lcu *lcu,
367 struct dasd_device *device)
368{
369 struct dasd_eckd_private *private;
370 struct alias_pav_group *group;
371
372 private = (struct dasd_eckd_private *) device->private;
373 list_move(&device->alias_list, &lcu->inactive_devices);
374 group = private->pavgroup;
375 if (!group)
376 return;
377 private->pavgroup = NULL;
378 if (list_empty(&group->baselist) && list_empty(&group->aliaslist)) {
379 list_del(&group->group);
380 kfree(group);
381 return;
382 }
383 if (group->next == device)
384 group->next = NULL;
385};
386
Stefan Haberland03429f32012-09-11 17:19:12 +0200387static int
388suborder_not_supported(struct dasd_ccw_req *cqr)
389{
390 char *sense;
391 char reason;
392 char msg_format;
393 char msg_no;
394
395 sense = dasd_get_sense(&cqr->irb);
396 if (!sense)
397 return 0;
398
399 reason = sense[0];
400 msg_format = (sense[7] & 0xF0);
401 msg_no = (sense[7] & 0x0F);
402
403 /* command reject, Format 0 MSG 4 - invalid parameter */
404 if ((reason == 0x80) && (msg_format == 0x00) && (msg_no == 0x04))
405 return 1;
406
407 return 0;
408}
409
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100410static int read_unit_address_configuration(struct dasd_device *device,
411 struct alias_lcu *lcu)
412{
413 struct dasd_psf_prssd_data *prssdp;
414 struct dasd_ccw_req *cqr;
415 struct ccw1 *ccw;
416 int rc;
417 unsigned long flags;
418
Stefan Haberland68b781f2009-09-11 10:28:29 +0200419 cqr = dasd_kmalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100420 (sizeof(struct dasd_psf_prssd_data)),
421 device);
422 if (IS_ERR(cqr))
423 return PTR_ERR(cqr);
424 cqr->startdev = device;
425 cqr->memdev = device;
426 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
427 cqr->retries = 10;
428 cqr->expires = 20 * HZ;
429
430 /* Prepare for Read Subsystem Data */
431 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
432 memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
433 prssdp->order = PSF_ORDER_PRSSD;
434 prssdp->suborder = 0x0e; /* Read unit address configuration */
435 /* all other bytes of prssdp must be zero */
436
437 ccw = cqr->cpaddr;
438 ccw->cmd_code = DASD_ECKD_CCW_PSF;
439 ccw->count = sizeof(struct dasd_psf_prssd_data);
440 ccw->flags |= CCW_FLAG_CC;
441 ccw->cda = (__u32)(addr_t) prssdp;
442
443 /* Read Subsystem Data - feature codes */
444 memset(lcu->uac, 0, sizeof(*(lcu->uac)));
445
446 ccw++;
447 ccw->cmd_code = DASD_ECKD_CCW_RSSD;
448 ccw->count = sizeof(*(lcu->uac));
449 ccw->cda = (__u32)(addr_t) lcu->uac;
450
Heiko Carstens1aae0562013-01-30 09:49:40 +0100451 cqr->buildclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100452 cqr->status = DASD_CQR_FILLED;
453
454 /* need to unset flag here to detect race with summary unit check */
455 spin_lock_irqsave(&lcu->lock, flags);
456 lcu->flags &= ~NEED_UAC_UPDATE;
457 spin_unlock_irqrestore(&lcu->lock, flags);
458
459 do {
460 rc = dasd_sleep_on(cqr);
Stefan Haberland03429f32012-09-11 17:19:12 +0200461 if (rc && suborder_not_supported(cqr))
462 return -EOPNOTSUPP;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100463 } while (rc && (cqr->retries > 0));
464 if (rc) {
465 spin_lock_irqsave(&lcu->lock, flags);
466 lcu->flags |= NEED_UAC_UPDATE;
467 spin_unlock_irqrestore(&lcu->lock, flags);
468 }
469 dasd_kfree_request(cqr, cqr->memdev);
470 return rc;
471}
472
473static int _lcu_update(struct dasd_device *refdev, struct alias_lcu *lcu)
474{
475 unsigned long flags;
476 struct alias_pav_group *pavgroup, *tempgroup;
477 struct dasd_device *device, *tempdev;
478 int i, rc;
479 struct dasd_eckd_private *private;
480
481 spin_lock_irqsave(&lcu->lock, flags);
482 list_for_each_entry_safe(pavgroup, tempgroup, &lcu->grouplist, group) {
483 list_for_each_entry_safe(device, tempdev, &pavgroup->baselist,
484 alias_list) {
485 list_move(&device->alias_list, &lcu->active_devices);
486 private = (struct dasd_eckd_private *) device->private;
487 private->pavgroup = NULL;
488 }
489 list_for_each_entry_safe(device, tempdev, &pavgroup->aliaslist,
490 alias_list) {
491 list_move(&device->alias_list, &lcu->active_devices);
492 private = (struct dasd_eckd_private *) device->private;
493 private->pavgroup = NULL;
494 }
495 list_del(&pavgroup->group);
496 kfree(pavgroup);
497 }
498 spin_unlock_irqrestore(&lcu->lock, flags);
499
500 rc = read_unit_address_configuration(refdev, lcu);
501 if (rc)
502 return rc;
503
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200504 /* need to take cdev lock before lcu lock */
505 spin_lock_irqsave_nested(get_ccwdev_lock(refdev->cdev), flags,
506 CDEV_NESTED_FIRST);
507 spin_lock(&lcu->lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100508 lcu->pav = NO_PAV;
509 for (i = 0; i < MAX_DEVICES_PER_LCU; ++i) {
510 switch (lcu->uac->unit[i].ua_type) {
511 case UA_BASE_PAV_ALIAS:
512 lcu->pav = BASE_PAV;
513 break;
514 case UA_HYPER_PAV_ALIAS:
515 lcu->pav = HYPER_PAV;
516 break;
517 }
518 if (lcu->pav != NO_PAV)
519 break;
520 }
521
522 list_for_each_entry_safe(device, tempdev, &lcu->active_devices,
523 alias_list) {
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200524 _add_device_to_lcu(lcu, device, refdev);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100525 }
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200526 spin_unlock(&lcu->lock);
527 spin_unlock_irqrestore(get_ccwdev_lock(refdev->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100528 return 0;
529}
530
531static void lcu_update_work(struct work_struct *work)
532{
533 struct alias_lcu *lcu;
534 struct read_uac_work_data *ruac_data;
535 struct dasd_device *device;
536 unsigned long flags;
537 int rc;
538
539 ruac_data = container_of(work, struct read_uac_work_data, dwork.work);
540 lcu = container_of(ruac_data, struct alias_lcu, ruac_data);
541 device = ruac_data->device;
542 rc = _lcu_update(device, lcu);
543 /*
544 * Need to check flags again, as there could have been another
545 * prepare_update or a new device a new device while we were still
546 * processing the data
547 */
548 spin_lock_irqsave(&lcu->lock, flags);
Stefan Haberland03429f32012-09-11 17:19:12 +0200549 if ((rc && (rc != -EOPNOTSUPP)) || (lcu->flags & NEED_UAC_UPDATE)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100550 DBF_DEV_EVENT(DBF_WARNING, device, "could not update"
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100551 " alias data in lcu (rc = %d), retry later", rc);
552 schedule_delayed_work(&lcu->ruac_data.dwork, 30*HZ);
553 } else {
554 lcu->ruac_data.device = NULL;
555 lcu->flags &= ~UPDATE_PENDING;
556 }
557 spin_unlock_irqrestore(&lcu->lock, flags);
558}
559
560static int _schedule_lcu_update(struct alias_lcu *lcu,
561 struct dasd_device *device)
562{
563 struct dasd_device *usedev = NULL;
564 struct alias_pav_group *group;
565
566 lcu->flags |= NEED_UAC_UPDATE;
567 if (lcu->ruac_data.device) {
568 /* already scheduled or running */
569 return 0;
570 }
571 if (device && !list_empty(&device->alias_list))
572 usedev = device;
573
574 if (!usedev && !list_empty(&lcu->grouplist)) {
575 group = list_first_entry(&lcu->grouplist,
576 struct alias_pav_group, group);
577 if (!list_empty(&group->baselist))
578 usedev = list_first_entry(&group->baselist,
579 struct dasd_device,
580 alias_list);
581 else if (!list_empty(&group->aliaslist))
582 usedev = list_first_entry(&group->aliaslist,
583 struct dasd_device,
584 alias_list);
585 }
586 if (!usedev && !list_empty(&lcu->active_devices)) {
587 usedev = list_first_entry(&lcu->active_devices,
588 struct dasd_device, alias_list);
589 }
590 /*
591 * if we haven't found a proper device yet, give up for now, the next
592 * device that will be set active will trigger an lcu update
593 */
594 if (!usedev)
595 return -EINVAL;
596 lcu->ruac_data.device = usedev;
597 schedule_delayed_work(&lcu->ruac_data.dwork, 0);
598 return 0;
599}
600
601int dasd_alias_add_device(struct dasd_device *device)
602{
603 struct dasd_eckd_private *private;
604 struct alias_lcu *lcu;
605 unsigned long flags;
606 int rc;
607
608 private = (struct dasd_eckd_private *) device->private;
609 lcu = private->lcu;
610 rc = 0;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200611
612 /* need to take cdev lock before lcu lock */
613 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
614 spin_lock(&lcu->lock);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100615 if (!(lcu->flags & UPDATE_PENDING)) {
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200616 rc = _add_device_to_lcu(lcu, device, device);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100617 if (rc)
618 lcu->flags |= UPDATE_PENDING;
619 }
620 if (lcu->flags & UPDATE_PENDING) {
621 list_move(&device->alias_list, &lcu->active_devices);
622 _schedule_lcu_update(lcu, device);
623 }
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200624 spin_unlock(&lcu->lock);
625 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100626 return rc;
627}
628
Stefan Haberland501183f2010-05-17 10:00:10 +0200629int dasd_alias_update_add_device(struct dasd_device *device)
630{
631 struct dasd_eckd_private *private;
632 private = (struct dasd_eckd_private *) device->private;
633 private->lcu->flags |= UPDATE_PENDING;
634 return dasd_alias_add_device(device);
635}
636
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100637int dasd_alias_remove_device(struct dasd_device *device)
638{
639 struct dasd_eckd_private *private;
640 struct alias_lcu *lcu;
641 unsigned long flags;
642
643 private = (struct dasd_eckd_private *) device->private;
644 lcu = private->lcu;
Stefan Haberlandf602f6d62011-01-31 11:30:03 +0100645 /* nothing to do if already removed */
646 if (!lcu)
647 return 0;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100648 spin_lock_irqsave(&lcu->lock, flags);
649 _remove_device_from_lcu(lcu, device);
650 spin_unlock_irqrestore(&lcu->lock, flags);
651 return 0;
652}
653
654struct dasd_device *dasd_alias_get_start_dev(struct dasd_device *base_device)
655{
656
657 struct dasd_device *alias_device;
658 struct alias_pav_group *group;
659 struct alias_lcu *lcu;
660 struct dasd_eckd_private *private, *alias_priv;
661 unsigned long flags;
662
663 private = (struct dasd_eckd_private *) base_device->private;
664 group = private->pavgroup;
665 lcu = private->lcu;
666 if (!group || !lcu)
667 return NULL;
668 if (lcu->pav == NO_PAV ||
669 lcu->flags & (NEED_UAC_UPDATE | UPDATE_PENDING))
670 return NULL;
Stefan Haberlandb38f27e2011-12-27 11:27:28 +0100671 if (unlikely(!(private->features.feature[8] & 0x01))) {
672 /*
673 * PAV enabled but prefix not, very unlikely
674 * seems to be a lost pathgroup
675 * use base device to do IO
676 */
677 DBF_DEV_EVENT(DBF_ERR, base_device, "%s",
678 "Prefix not enabled with PAV enabled\n");
679 return NULL;
680 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100681
682 spin_lock_irqsave(&lcu->lock, flags);
683 alias_device = group->next;
684 if (!alias_device) {
685 if (list_empty(&group->aliaslist)) {
686 spin_unlock_irqrestore(&lcu->lock, flags);
687 return NULL;
688 } else {
689 alias_device = list_first_entry(&group->aliaslist,
690 struct dasd_device,
691 alias_list);
692 }
693 }
694 if (list_is_last(&alias_device->alias_list, &group->aliaslist))
695 group->next = list_first_entry(&group->aliaslist,
696 struct dasd_device, alias_list);
697 else
698 group->next = list_first_entry(&alias_device->alias_list,
699 struct dasd_device, alias_list);
700 spin_unlock_irqrestore(&lcu->lock, flags);
701 alias_priv = (struct dasd_eckd_private *) alias_device->private;
Stefan Haberlandf81a49d2015-07-10 10:47:09 +0200702 if ((alias_priv->count < private->count) && !alias_device->stopped &&
703 !test_bit(DASD_FLAG_OFFLINE, &alias_device->flags))
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100704 return alias_device;
705 else
706 return NULL;
707}
708
709/*
710 * Summary unit check handling depends on the way alias devices
711 * are handled so it is done here rather then in dasd_eckd.c
712 */
713static int reset_summary_unit_check(struct alias_lcu *lcu,
714 struct dasd_device *device,
715 char reason)
716{
717 struct dasd_ccw_req *cqr;
718 int rc = 0;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100719 struct ccw1 *ccw;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100720
721 cqr = lcu->rsu_cqr;
722 strncpy((char *) &cqr->magic, "ECKD", 4);
723 ASCEBC((char *) &cqr->magic, 4);
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100724 ccw = cqr->cpaddr;
725 ccw->cmd_code = DASD_ECKD_CCW_RSCK;
Stefan Haberland020bf042015-12-15 10:16:43 +0100726 ccw->flags = CCW_FLAG_SLI;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100727 ccw->count = 16;
728 ccw->cda = (__u32)(addr_t) cqr->data;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100729 ((char *)cqr->data)[0] = reason;
730
731 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
732 cqr->retries = 255; /* set retry counter to enable basic ERP */
733 cqr->startdev = device;
734 cqr->memdev = device;
735 cqr->block = NULL;
736 cqr->expires = 5 * HZ;
Heiko Carstens1aae0562013-01-30 09:49:40 +0100737 cqr->buildclk = get_tod_clock();
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100738 cqr->status = DASD_CQR_FILLED;
739
740 rc = dasd_sleep_on_immediatly(cqr);
741 return rc;
742}
743
744static void _restart_all_base_devices_on_lcu(struct alias_lcu *lcu)
745{
746 struct alias_pav_group *pavgroup;
747 struct dasd_device *device;
748 struct dasd_eckd_private *private;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200749 unsigned long flags;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100750
751 /* active and inactive list can contain alias as well as base devices */
752 list_for_each_entry(device, &lcu->active_devices, alias_list) {
753 private = (struct dasd_eckd_private *) device->private;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200754 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
755 if (private->uid.type != UA_BASE_DEVICE) {
756 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
757 flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100758 continue;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200759 }
760 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100761 dasd_schedule_block_bh(device->block);
762 dasd_schedule_device_bh(device);
763 }
764 list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
765 private = (struct dasd_eckd_private *) device->private;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200766 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
767 if (private->uid.type != UA_BASE_DEVICE) {
768 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
769 flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100770 continue;
Stefan Haberland2dedf0d2010-05-17 10:00:11 +0200771 }
772 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100773 dasd_schedule_block_bh(device->block);
774 dasd_schedule_device_bh(device);
775 }
776 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
777 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
778 dasd_schedule_block_bh(device->block);
779 dasd_schedule_device_bh(device);
780 }
781 }
782}
783
784static void flush_all_alias_devices_on_lcu(struct alias_lcu *lcu)
785{
786 struct alias_pav_group *pavgroup;
787 struct dasd_device *device, *temp;
788 struct dasd_eckd_private *private;
789 int rc;
790 unsigned long flags;
791 LIST_HEAD(active);
792
793 /*
794 * Problem here ist that dasd_flush_device_queue may wait
795 * for termination of a request to complete. We can't keep
796 * the lcu lock during that time, so we must assume that
797 * the lists may have changed.
798 * Idea: first gather all active alias devices in a separate list,
799 * then flush the first element of this list unlocked, and afterwards
800 * check if it is still on the list before moving it to the
801 * active_devices list.
802 */
803
804 spin_lock_irqsave(&lcu->lock, flags);
805 list_for_each_entry_safe(device, temp, &lcu->active_devices,
806 alias_list) {
807 private = (struct dasd_eckd_private *) device->private;
808 if (private->uid.type == UA_BASE_DEVICE)
809 continue;
810 list_move(&device->alias_list, &active);
811 }
812
813 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
814 list_splice_init(&pavgroup->aliaslist, &active);
815 }
816 while (!list_empty(&active)) {
817 device = list_first_entry(&active, struct dasd_device,
818 alias_list);
819 spin_unlock_irqrestore(&lcu->lock, flags);
820 rc = dasd_flush_device_queue(device);
821 spin_lock_irqsave(&lcu->lock, flags);
822 /*
823 * only move device around if it wasn't moved away while we
824 * were waiting for the flush
825 */
826 if (device == list_first_entry(&active,
Stefan Haberland6933c352015-10-14 11:01:05 +0200827 struct dasd_device, alias_list)) {
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100828 list_move(&device->alias_list, &lcu->active_devices);
Stefan Haberland6933c352015-10-14 11:01:05 +0200829 private = (struct dasd_eckd_private *) device->private;
830 private->pavgroup = NULL;
831 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100832 }
833 spin_unlock_irqrestore(&lcu->lock, flags);
834}
835
Heiko Carstensa8061702008-04-17 07:46:26 +0200836static void __stop_device_on_lcu(struct dasd_device *device,
837 struct dasd_device *pos)
838{
839 /* If pos == device then device is already locked! */
840 if (pos == device) {
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100841 dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
Heiko Carstensa8061702008-04-17 07:46:26 +0200842 return;
843 }
844 spin_lock(get_ccwdev_lock(pos->cdev));
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100845 dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
Heiko Carstensa8061702008-04-17 07:46:26 +0200846 spin_unlock(get_ccwdev_lock(pos->cdev));
847}
848
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100849/*
850 * This function is called in interrupt context, so the
851 * cdev lock for device is already locked!
852 */
853static void _stop_all_devices_on_lcu(struct alias_lcu *lcu,
854 struct dasd_device *device)
855{
856 struct alias_pav_group *pavgroup;
857 struct dasd_device *pos;
858
Heiko Carstensa8061702008-04-17 07:46:26 +0200859 list_for_each_entry(pos, &lcu->active_devices, alias_list)
860 __stop_device_on_lcu(device, pos);
861 list_for_each_entry(pos, &lcu->inactive_devices, alias_list)
862 __stop_device_on_lcu(device, pos);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100863 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
Heiko Carstensa8061702008-04-17 07:46:26 +0200864 list_for_each_entry(pos, &pavgroup->baselist, alias_list)
865 __stop_device_on_lcu(device, pos);
866 list_for_each_entry(pos, &pavgroup->aliaslist, alias_list)
867 __stop_device_on_lcu(device, pos);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100868 }
869}
870
871static void _unstop_all_devices_on_lcu(struct alias_lcu *lcu)
872{
873 struct alias_pav_group *pavgroup;
874 struct dasd_device *device;
875 unsigned long flags;
876
877 list_for_each_entry(device, &lcu->active_devices, alias_list) {
878 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100879 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100880 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
881 }
882
883 list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
884 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100885 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100886 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
887 }
888
889 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
890 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
891 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100892 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100893 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
894 flags);
895 }
896 list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
897 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100898 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100899 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
900 flags);
901 }
902 }
903}
904
905static void summary_unit_check_handling_work(struct work_struct *work)
906{
907 struct alias_lcu *lcu;
908 struct summary_unit_check_work_data *suc_data;
909 unsigned long flags;
910 struct dasd_device *device;
911
912 suc_data = container_of(work, struct summary_unit_check_work_data,
913 worker);
914 lcu = container_of(suc_data, struct alias_lcu, suc_data);
915 device = suc_data->device;
916
917 /* 1. flush alias devices */
918 flush_all_alias_devices_on_lcu(lcu);
919
920 /* 2. reset summary unit check */
921 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
Stefan Weinhubereb6e1992009-12-07 12:51:51 +0100922 dasd_device_remove_stop_bits(device,
923 (DASD_STOPPED_SU | DASD_STOPPED_PENDING));
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100924 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
925 reset_summary_unit_check(lcu, device, suc_data->reason);
926
927 spin_lock_irqsave(&lcu->lock, flags);
928 _unstop_all_devices_on_lcu(lcu);
929 _restart_all_base_devices_on_lcu(lcu);
930 /* 3. read new alias configuration */
931 _schedule_lcu_update(lcu, device);
932 lcu->suc_data.device = NULL;
933 spin_unlock_irqrestore(&lcu->lock, flags);
934}
935
936/*
937 * note: this will be called from int handler context (cdev locked)
938 */
939void dasd_alias_handle_summary_unit_check(struct dasd_device *device,
940 struct irb *irb)
941{
942 struct alias_lcu *lcu;
943 char reason;
944 struct dasd_eckd_private *private;
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100945 char *sense;
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100946
947 private = (struct dasd_eckd_private *) device->private;
948
Stefan Weinhuberf3eb5382009-03-26 15:23:48 +0100949 sense = dasd_get_sense(irb);
950 if (sense) {
951 reason = sense[8];
952 DBF_DEV_EVENT(DBF_NOTICE, device, "%s %x",
953 "eckd handle summary unit check: reason", reason);
954 } else {
955 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
956 "eckd handle summary unit check:"
957 " no reason code available");
958 return;
959 }
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100960
961 lcu = private->lcu;
962 if (!lcu) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100963 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100964 "device not ready to handle summary"
965 " unit check (no lcu structure)");
966 return;
967 }
968 spin_lock(&lcu->lock);
969 _stop_all_devices_on_lcu(lcu, device);
970 /* prepare for lcu_update */
971 private->lcu->flags |= NEED_UAC_UPDATE | UPDATE_PENDING;
972 /* If this device is about to be removed just return and wait for
973 * the next interrupt on a different device
974 */
975 if (list_empty(&device->alias_list)) {
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100976 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100977 "device is in offline processing,"
978 " don't do summary unit check handling");
979 spin_unlock(&lcu->lock);
980 return;
981 }
982 if (lcu->suc_data.device) {
983 /* already scheduled or running */
Stefan Haberlandfc19f382009-03-26 15:23:49 +0100984 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
Stefan Weinhuber8e09f212008-01-26 14:11:23 +0100985 "previous instance of summary unit check worker"
986 " still pending");
987 spin_unlock(&lcu->lock);
988 return ;
989 }
990 lcu->suc_data.reason = reason;
991 lcu->suc_data.device = device;
992 spin_unlock(&lcu->lock);
993 schedule_work(&lcu->suc_data.worker);
994};