Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / s390 / block / dasd_alias.c
blob8c4814258e93c9fe58e3711be1051a49b0af0269
1 /*
2 * PAV alias management for the DASD ECKD discipline
4 * Copyright IBM Corporation, 2007
5 * Author(s): Stefan Weinhuber <wein@de.ibm.com>
6 */
8 #define KMSG_COMPONENT "dasd-eckd"
10 #include <linux/list.h>
11 #include <linux/slab.h>
12 #include <asm/ebcdic.h>
13 #include "dasd_int.h"
14 #include "dasd_eckd.h"
16 #ifdef PRINTK_HEADER
17 #undef PRINTK_HEADER
18 #endif /* PRINTK_HEADER */
19 #define PRINTK_HEADER "dasd(eckd):"
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.
43 static void summary_unit_check_handling_work(struct work_struct *);
44 static void lcu_update_work(struct work_struct *);
45 static int _schedule_lcu_update(struct alias_lcu *, struct dasd_device *);
47 static struct alias_root aliastree = {
48 .serverlist = LIST_HEAD_INIT(aliastree.serverlist),
49 .lock = __SPIN_LOCK_UNLOCKED(aliastree.lock),
52 static struct alias_server *_find_server(struct dasd_uid *uid)
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;
62 return NULL;
65 static struct alias_lcu *_find_lcu(struct alias_server *server,
66 struct dasd_uid *uid)
68 struct alias_lcu *pos;
69 list_for_each_entry(pos, &server->lculist, lcu) {
70 if (pos->uid.ssid == uid->ssid)
71 return pos;
73 return NULL;
76 static struct alias_pav_group *_find_group(struct alias_lcu *lcu,
77 struct dasd_uid *uid)
79 struct alias_pav_group *pos;
80 __u8 search_unit_addr;
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);
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) {
97 if (pos->uid.base_unit_addr == search_unit_addr &&
98 !strncmp(pos->uid.vduit, uid->vduit, sizeof(uid->vduit)))
99 return pos;
101 return NULL;
104 static struct alias_server *_allocate_server(struct dasd_uid *uid)
106 struct alias_server *server;
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;
118 static void _free_server(struct alias_server *server)
120 kfree(server);
123 static struct alias_lcu *_allocate_lcu(struct dasd_uid *uid)
125 struct alias_lcu *lcu;
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;
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);
156 init_completion(&lcu->lcu_setup);
157 return lcu;
159 out_err4:
160 kfree(lcu->rsu_cqr->cpaddr);
161 out_err3:
162 kfree(lcu->rsu_cqr);
163 out_err2:
164 kfree(lcu->uac);
165 out_err1:
166 kfree(lcu);
167 return ERR_PTR(-ENOMEM);
170 static void _free_lcu(struct alias_lcu *lcu)
172 kfree(lcu->rsu_cqr->data);
173 kfree(lcu->rsu_cqr->cpaddr);
174 kfree(lcu->rsu_cqr);
175 kfree(lcu->uac);
176 kfree(lcu);
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)
186 int dasd_alias_make_device_known_to_lcu(struct dasd_device *device)
188 struct dasd_eckd_private *private;
189 unsigned long flags;
190 struct alias_server *server, *newserver;
191 struct alias_lcu *lcu, *newlcu;
192 int is_lcu_known;
193 struct dasd_uid *uid;
195 private = (struct dasd_eckd_private *) device->private;
196 uid = &private->uid;
197 spin_lock_irqsave(&aliastree.lock, flags);
198 is_lcu_known = 1;
199 server = _find_server(uid);
200 if (!server) {
201 spin_unlock_irqrestore(&aliastree.lock, flags);
202 newserver = _allocate_server(uid);
203 if (IS_ERR(newserver))
204 return PTR_ERR(newserver);
205 spin_lock_irqsave(&aliastree.lock, flags);
206 server = _find_server(uid);
207 if (!server) {
208 list_add(&newserver->server, &aliastree.serverlist);
209 server = newserver;
210 is_lcu_known = 0;
211 } else {
212 /* someone was faster */
213 _free_server(newserver);
217 lcu = _find_lcu(server, uid);
218 if (!lcu) {
219 spin_unlock_irqrestore(&aliastree.lock, flags);
220 newlcu = _allocate_lcu(uid);
221 if (IS_ERR(newlcu))
222 return PTR_ERR(newlcu);
223 spin_lock_irqsave(&aliastree.lock, flags);
224 lcu = _find_lcu(server, uid);
225 if (!lcu) {
226 list_add(&newlcu->lcu, &server->lculist);
227 lcu = newlcu;
228 is_lcu_known = 0;
229 } else {
230 /* someone was faster */
231 _free_lcu(newlcu);
233 is_lcu_known = 0;
235 spin_lock(&lcu->lock);
236 list_add(&device->alias_list, &lcu->inactive_devices);
237 private->lcu = lcu;
238 spin_unlock(&lcu->lock);
239 spin_unlock_irqrestore(&aliastree.lock, flags);
241 return is_lcu_known;
245 * The first device to be registered on an LCU will have to do
246 * some additional setup steps to configure that LCU on the
247 * storage server. All further devices should wait with their
248 * initialization until the first device is done.
249 * To synchronize this work, the first device will call
250 * dasd_alias_lcu_setup_complete when it is done, and all
251 * other devices will wait for it with dasd_alias_wait_for_lcu_setup.
253 void dasd_alias_lcu_setup_complete(struct dasd_device *device)
255 struct dasd_eckd_private *private;
256 unsigned long flags;
257 struct alias_server *server;
258 struct alias_lcu *lcu;
259 struct dasd_uid *uid;
261 private = (struct dasd_eckd_private *) device->private;
262 uid = &private->uid;
263 lcu = NULL;
264 spin_lock_irqsave(&aliastree.lock, flags);
265 server = _find_server(uid);
266 if (server)
267 lcu = _find_lcu(server, uid);
268 spin_unlock_irqrestore(&aliastree.lock, flags);
269 if (!lcu) {
270 DBF_EVENT_DEVID(DBF_ERR, device->cdev,
271 "could not find lcu for %04x %02x",
272 uid->ssid, uid->real_unit_addr);
273 WARN_ON(1);
274 return;
276 complete_all(&lcu->lcu_setup);
279 void dasd_alias_wait_for_lcu_setup(struct dasd_device *device)
281 struct dasd_eckd_private *private;
282 unsigned long flags;
283 struct alias_server *server;
284 struct alias_lcu *lcu;
285 struct dasd_uid *uid;
287 private = (struct dasd_eckd_private *) device->private;
288 uid = &private->uid;
289 lcu = NULL;
290 spin_lock_irqsave(&aliastree.lock, flags);
291 server = _find_server(uid);
292 if (server)
293 lcu = _find_lcu(server, uid);
294 spin_unlock_irqrestore(&aliastree.lock, flags);
295 if (!lcu) {
296 DBF_EVENT_DEVID(DBF_ERR, device->cdev,
297 "could not find lcu for %04x %02x",
298 uid->ssid, uid->real_unit_addr);
299 WARN_ON(1);
300 return;
302 wait_for_completion(&lcu->lcu_setup);
306 * This function removes a device from the scope of alias management.
307 * The complicated part is to make sure that it is not in use by
308 * any of the workers. If necessary cancel the work.
310 void dasd_alias_disconnect_device_from_lcu(struct dasd_device *device)
312 struct dasd_eckd_private *private;
313 unsigned long flags;
314 struct alias_lcu *lcu;
315 struct alias_server *server;
316 int was_pending;
318 private = (struct dasd_eckd_private *) device->private;
319 lcu = private->lcu;
320 spin_lock_irqsave(&lcu->lock, flags);
321 list_del_init(&device->alias_list);
322 /* make sure that the workers don't use this device */
323 if (device == lcu->suc_data.device) {
324 spin_unlock_irqrestore(&lcu->lock, flags);
325 cancel_work_sync(&lcu->suc_data.worker);
326 spin_lock_irqsave(&lcu->lock, flags);
327 if (device == lcu->suc_data.device)
328 lcu->suc_data.device = NULL;
330 was_pending = 0;
331 if (device == lcu->ruac_data.device) {
332 spin_unlock_irqrestore(&lcu->lock, flags);
333 was_pending = 1;
334 cancel_delayed_work_sync(&lcu->ruac_data.dwork);
335 spin_lock_irqsave(&lcu->lock, flags);
336 if (device == lcu->ruac_data.device)
337 lcu->ruac_data.device = NULL;
339 private->lcu = NULL;
340 spin_unlock_irqrestore(&lcu->lock, flags);
342 spin_lock_irqsave(&aliastree.lock, flags);
343 spin_lock(&lcu->lock);
344 if (list_empty(&lcu->grouplist) &&
345 list_empty(&lcu->active_devices) &&
346 list_empty(&lcu->inactive_devices)) {
347 list_del(&lcu->lcu);
348 spin_unlock(&lcu->lock);
349 _free_lcu(lcu);
350 lcu = NULL;
351 } else {
352 if (was_pending)
353 _schedule_lcu_update(lcu, NULL);
354 spin_unlock(&lcu->lock);
356 server = _find_server(&private->uid);
357 if (server && list_empty(&server->lculist)) {
358 list_del(&server->server);
359 _free_server(server);
361 spin_unlock_irqrestore(&aliastree.lock, flags);
365 * This function assumes that the unit address configuration stored
366 * in the lcu is up to date and will update the device uid before
367 * adding it to a pav group.
369 static int _add_device_to_lcu(struct alias_lcu *lcu,
370 struct dasd_device *device)
373 struct dasd_eckd_private *private;
374 struct alias_pav_group *group;
375 struct dasd_uid *uid;
377 private = (struct dasd_eckd_private *) device->private;
378 uid = &private->uid;
379 uid->type = lcu->uac->unit[uid->real_unit_addr].ua_type;
380 uid->base_unit_addr = lcu->uac->unit[uid->real_unit_addr].base_ua;
381 dasd_set_uid(device->cdev, &private->uid);
383 /* if we have no PAV anyway, we don't need to bother with PAV groups */
384 if (lcu->pav == NO_PAV) {
385 list_move(&device->alias_list, &lcu->active_devices);
386 return 0;
389 group = _find_group(lcu, uid);
390 if (!group) {
391 group = kzalloc(sizeof(*group), GFP_ATOMIC);
392 if (!group)
393 return -ENOMEM;
394 memcpy(group->uid.vendor, uid->vendor, sizeof(uid->vendor));
395 memcpy(group->uid.serial, uid->serial, sizeof(uid->serial));
396 group->uid.ssid = uid->ssid;
397 if (uid->type == UA_BASE_DEVICE)
398 group->uid.base_unit_addr = uid->real_unit_addr;
399 else
400 group->uid.base_unit_addr = uid->base_unit_addr;
401 memcpy(group->uid.vduit, uid->vduit, sizeof(uid->vduit));
402 INIT_LIST_HEAD(&group->group);
403 INIT_LIST_HEAD(&group->baselist);
404 INIT_LIST_HEAD(&group->aliaslist);
405 list_add(&group->group, &lcu->grouplist);
407 if (uid->type == UA_BASE_DEVICE)
408 list_move(&device->alias_list, &group->baselist);
409 else
410 list_move(&device->alias_list, &group->aliaslist);
411 private->pavgroup = group;
412 return 0;
415 static void _remove_device_from_lcu(struct alias_lcu *lcu,
416 struct dasd_device *device)
418 struct dasd_eckd_private *private;
419 struct alias_pav_group *group;
421 private = (struct dasd_eckd_private *) device->private;
422 list_move(&device->alias_list, &lcu->inactive_devices);
423 group = private->pavgroup;
424 if (!group)
425 return;
426 private->pavgroup = NULL;
427 if (list_empty(&group->baselist) && list_empty(&group->aliaslist)) {
428 list_del(&group->group);
429 kfree(group);
430 return;
432 if (group->next == device)
433 group->next = NULL;
436 static int read_unit_address_configuration(struct dasd_device *device,
437 struct alias_lcu *lcu)
439 struct dasd_psf_prssd_data *prssdp;
440 struct dasd_ccw_req *cqr;
441 struct ccw1 *ccw;
442 int rc;
443 unsigned long flags;
445 cqr = dasd_kmalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
446 (sizeof(struct dasd_psf_prssd_data)),
447 device);
448 if (IS_ERR(cqr))
449 return PTR_ERR(cqr);
450 cqr->startdev = device;
451 cqr->memdev = device;
452 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
453 cqr->retries = 10;
454 cqr->expires = 20 * HZ;
456 /* Prepare for Read Subsystem Data */
457 prssdp = (struct dasd_psf_prssd_data *) cqr->data;
458 memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
459 prssdp->order = PSF_ORDER_PRSSD;
460 prssdp->suborder = 0x0e; /* Read unit address configuration */
461 /* all other bytes of prssdp must be zero */
463 ccw = cqr->cpaddr;
464 ccw->cmd_code = DASD_ECKD_CCW_PSF;
465 ccw->count = sizeof(struct dasd_psf_prssd_data);
466 ccw->flags |= CCW_FLAG_CC;
467 ccw->cda = (__u32)(addr_t) prssdp;
469 /* Read Subsystem Data - feature codes */
470 memset(lcu->uac, 0, sizeof(*(lcu->uac)));
472 ccw++;
473 ccw->cmd_code = DASD_ECKD_CCW_RSSD;
474 ccw->count = sizeof(*(lcu->uac));
475 ccw->cda = (__u32)(addr_t) lcu->uac;
477 cqr->buildclk = get_clock();
478 cqr->status = DASD_CQR_FILLED;
480 /* need to unset flag here to detect race with summary unit check */
481 spin_lock_irqsave(&lcu->lock, flags);
482 lcu->flags &= ~NEED_UAC_UPDATE;
483 spin_unlock_irqrestore(&lcu->lock, flags);
485 do {
486 rc = dasd_sleep_on(cqr);
487 } while (rc && (cqr->retries > 0));
488 if (rc) {
489 spin_lock_irqsave(&lcu->lock, flags);
490 lcu->flags |= NEED_UAC_UPDATE;
491 spin_unlock_irqrestore(&lcu->lock, flags);
493 dasd_kfree_request(cqr, cqr->memdev);
494 return rc;
497 static int _lcu_update(struct dasd_device *refdev, struct alias_lcu *lcu)
499 unsigned long flags;
500 struct alias_pav_group *pavgroup, *tempgroup;
501 struct dasd_device *device, *tempdev;
502 int i, rc;
503 struct dasd_eckd_private *private;
505 spin_lock_irqsave(&lcu->lock, flags);
506 list_for_each_entry_safe(pavgroup, tempgroup, &lcu->grouplist, group) {
507 list_for_each_entry_safe(device, tempdev, &pavgroup->baselist,
508 alias_list) {
509 list_move(&device->alias_list, &lcu->active_devices);
510 private = (struct dasd_eckd_private *) device->private;
511 private->pavgroup = NULL;
513 list_for_each_entry_safe(device, tempdev, &pavgroup->aliaslist,
514 alias_list) {
515 list_move(&device->alias_list, &lcu->active_devices);
516 private = (struct dasd_eckd_private *) device->private;
517 private->pavgroup = NULL;
519 list_del(&pavgroup->group);
520 kfree(pavgroup);
522 spin_unlock_irqrestore(&lcu->lock, flags);
524 rc = read_unit_address_configuration(refdev, lcu);
525 if (rc)
526 return rc;
528 spin_lock_irqsave(&lcu->lock, flags);
529 lcu->pav = NO_PAV;
530 for (i = 0; i < MAX_DEVICES_PER_LCU; ++i) {
531 switch (lcu->uac->unit[i].ua_type) {
532 case UA_BASE_PAV_ALIAS:
533 lcu->pav = BASE_PAV;
534 break;
535 case UA_HYPER_PAV_ALIAS:
536 lcu->pav = HYPER_PAV;
537 break;
539 if (lcu->pav != NO_PAV)
540 break;
543 list_for_each_entry_safe(device, tempdev, &lcu->active_devices,
544 alias_list) {
545 _add_device_to_lcu(lcu, device);
547 spin_unlock_irqrestore(&lcu->lock, flags);
548 return 0;
551 static void lcu_update_work(struct work_struct *work)
553 struct alias_lcu *lcu;
554 struct read_uac_work_data *ruac_data;
555 struct dasd_device *device;
556 unsigned long flags;
557 int rc;
559 ruac_data = container_of(work, struct read_uac_work_data, dwork.work);
560 lcu = container_of(ruac_data, struct alias_lcu, ruac_data);
561 device = ruac_data->device;
562 rc = _lcu_update(device, lcu);
564 * Need to check flags again, as there could have been another
565 * prepare_update or a new device a new device while we were still
566 * processing the data
568 spin_lock_irqsave(&lcu->lock, flags);
569 if (rc || (lcu->flags & NEED_UAC_UPDATE)) {
570 DBF_DEV_EVENT(DBF_WARNING, device, "could not update"
571 " alias data in lcu (rc = %d), retry later", rc);
572 schedule_delayed_work(&lcu->ruac_data.dwork, 30*HZ);
573 } else {
574 lcu->ruac_data.device = NULL;
575 lcu->flags &= ~UPDATE_PENDING;
577 spin_unlock_irqrestore(&lcu->lock, flags);
580 static int _schedule_lcu_update(struct alias_lcu *lcu,
581 struct dasd_device *device)
583 struct dasd_device *usedev = NULL;
584 struct alias_pav_group *group;
586 lcu->flags |= NEED_UAC_UPDATE;
587 if (lcu->ruac_data.device) {
588 /* already scheduled or running */
589 return 0;
591 if (device && !list_empty(&device->alias_list))
592 usedev = device;
594 if (!usedev && !list_empty(&lcu->grouplist)) {
595 group = list_first_entry(&lcu->grouplist,
596 struct alias_pav_group, group);
597 if (!list_empty(&group->baselist))
598 usedev = list_first_entry(&group->baselist,
599 struct dasd_device,
600 alias_list);
601 else if (!list_empty(&group->aliaslist))
602 usedev = list_first_entry(&group->aliaslist,
603 struct dasd_device,
604 alias_list);
606 if (!usedev && !list_empty(&lcu->active_devices)) {
607 usedev = list_first_entry(&lcu->active_devices,
608 struct dasd_device, alias_list);
611 * if we haven't found a proper device yet, give up for now, the next
612 * device that will be set active will trigger an lcu update
614 if (!usedev)
615 return -EINVAL;
616 lcu->ruac_data.device = usedev;
617 schedule_delayed_work(&lcu->ruac_data.dwork, 0);
618 return 0;
621 int dasd_alias_add_device(struct dasd_device *device)
623 struct dasd_eckd_private *private;
624 struct alias_lcu *lcu;
625 unsigned long flags;
626 int rc;
628 private = (struct dasd_eckd_private *) device->private;
629 lcu = private->lcu;
630 rc = 0;
631 spin_lock_irqsave(&lcu->lock, flags);
632 if (!(lcu->flags & UPDATE_PENDING)) {
633 rc = _add_device_to_lcu(lcu, device);
634 if (rc)
635 lcu->flags |= UPDATE_PENDING;
637 if (lcu->flags & UPDATE_PENDING) {
638 list_move(&device->alias_list, &lcu->active_devices);
639 _schedule_lcu_update(lcu, device);
641 spin_unlock_irqrestore(&lcu->lock, flags);
642 return rc;
645 int dasd_alias_remove_device(struct dasd_device *device)
647 struct dasd_eckd_private *private;
648 struct alias_lcu *lcu;
649 unsigned long flags;
651 private = (struct dasd_eckd_private *) device->private;
652 lcu = private->lcu;
653 spin_lock_irqsave(&lcu->lock, flags);
654 _remove_device_from_lcu(lcu, device);
655 spin_unlock_irqrestore(&lcu->lock, flags);
656 return 0;
659 struct dasd_device *dasd_alias_get_start_dev(struct dasd_device *base_device)
662 struct dasd_device *alias_device;
663 struct alias_pav_group *group;
664 struct alias_lcu *lcu;
665 struct dasd_eckd_private *private, *alias_priv;
666 unsigned long flags;
668 private = (struct dasd_eckd_private *) base_device->private;
669 group = private->pavgroup;
670 lcu = private->lcu;
671 if (!group || !lcu)
672 return NULL;
673 if (lcu->pav == NO_PAV ||
674 lcu->flags & (NEED_UAC_UPDATE | UPDATE_PENDING))
675 return NULL;
677 spin_lock_irqsave(&lcu->lock, flags);
678 alias_device = group->next;
679 if (!alias_device) {
680 if (list_empty(&group->aliaslist)) {
681 spin_unlock_irqrestore(&lcu->lock, flags);
682 return NULL;
683 } else {
684 alias_device = list_first_entry(&group->aliaslist,
685 struct dasd_device,
686 alias_list);
689 if (list_is_last(&alias_device->alias_list, &group->aliaslist))
690 group->next = list_first_entry(&group->aliaslist,
691 struct dasd_device, alias_list);
692 else
693 group->next = list_first_entry(&alias_device->alias_list,
694 struct dasd_device, alias_list);
695 spin_unlock_irqrestore(&lcu->lock, flags);
696 alias_priv = (struct dasd_eckd_private *) alias_device->private;
697 if ((alias_priv->count < private->count) && !alias_device->stopped)
698 return alias_device;
699 else
700 return NULL;
704 * Summary unit check handling depends on the way alias devices
705 * are handled so it is done here rather then in dasd_eckd.c
707 static int reset_summary_unit_check(struct alias_lcu *lcu,
708 struct dasd_device *device,
709 char reason)
711 struct dasd_ccw_req *cqr;
712 int rc = 0;
713 struct ccw1 *ccw;
715 cqr = lcu->rsu_cqr;
716 strncpy((char *) &cqr->magic, "ECKD", 4);
717 ASCEBC((char *) &cqr->magic, 4);
718 ccw = cqr->cpaddr;
719 ccw->cmd_code = DASD_ECKD_CCW_RSCK;
720 ccw->flags = 0 ;
721 ccw->count = 16;
722 ccw->cda = (__u32)(addr_t) cqr->data;
723 ((char *)cqr->data)[0] = reason;
725 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
726 cqr->retries = 255; /* set retry counter to enable basic ERP */
727 cqr->startdev = device;
728 cqr->memdev = device;
729 cqr->block = NULL;
730 cqr->expires = 5 * HZ;
731 cqr->buildclk = get_clock();
732 cqr->status = DASD_CQR_FILLED;
734 rc = dasd_sleep_on_immediatly(cqr);
735 return rc;
738 static void _restart_all_base_devices_on_lcu(struct alias_lcu *lcu)
740 struct alias_pav_group *pavgroup;
741 struct dasd_device *device;
742 struct dasd_eckd_private *private;
744 /* active and inactive list can contain alias as well as base devices */
745 list_for_each_entry(device, &lcu->active_devices, alias_list) {
746 private = (struct dasd_eckd_private *) device->private;
747 if (private->uid.type != UA_BASE_DEVICE)
748 continue;
749 dasd_schedule_block_bh(device->block);
750 dasd_schedule_device_bh(device);
752 list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
753 private = (struct dasd_eckd_private *) device->private;
754 if (private->uid.type != UA_BASE_DEVICE)
755 continue;
756 dasd_schedule_block_bh(device->block);
757 dasd_schedule_device_bh(device);
759 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
760 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
761 dasd_schedule_block_bh(device->block);
762 dasd_schedule_device_bh(device);
767 static void flush_all_alias_devices_on_lcu(struct alias_lcu *lcu)
769 struct alias_pav_group *pavgroup;
770 struct dasd_device *device, *temp;
771 struct dasd_eckd_private *private;
772 int rc;
773 unsigned long flags;
774 LIST_HEAD(active);
777 * Problem here ist that dasd_flush_device_queue may wait
778 * for termination of a request to complete. We can't keep
779 * the lcu lock during that time, so we must assume that
780 * the lists may have changed.
781 * Idea: first gather all active alias devices in a separate list,
782 * then flush the first element of this list unlocked, and afterwards
783 * check if it is still on the list before moving it to the
784 * active_devices list.
787 spin_lock_irqsave(&lcu->lock, flags);
788 list_for_each_entry_safe(device, temp, &lcu->active_devices,
789 alias_list) {
790 private = (struct dasd_eckd_private *) device->private;
791 if (private->uid.type == UA_BASE_DEVICE)
792 continue;
793 list_move(&device->alias_list, &active);
796 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
797 list_splice_init(&pavgroup->aliaslist, &active);
799 while (!list_empty(&active)) {
800 device = list_first_entry(&active, struct dasd_device,
801 alias_list);
802 spin_unlock_irqrestore(&lcu->lock, flags);
803 rc = dasd_flush_device_queue(device);
804 spin_lock_irqsave(&lcu->lock, flags);
806 * only move device around if it wasn't moved away while we
807 * were waiting for the flush
809 if (device == list_first_entry(&active,
810 struct dasd_device, alias_list))
811 list_move(&device->alias_list, &lcu->active_devices);
813 spin_unlock_irqrestore(&lcu->lock, flags);
816 static void __stop_device_on_lcu(struct dasd_device *device,
817 struct dasd_device *pos)
819 /* If pos == device then device is already locked! */
820 if (pos == device) {
821 dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
822 return;
824 spin_lock(get_ccwdev_lock(pos->cdev));
825 dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
826 spin_unlock(get_ccwdev_lock(pos->cdev));
830 * This function is called in interrupt context, so the
831 * cdev lock for device is already locked!
833 static void _stop_all_devices_on_lcu(struct alias_lcu *lcu,
834 struct dasd_device *device)
836 struct alias_pav_group *pavgroup;
837 struct dasd_device *pos;
839 list_for_each_entry(pos, &lcu->active_devices, alias_list)
840 __stop_device_on_lcu(device, pos);
841 list_for_each_entry(pos, &lcu->inactive_devices, alias_list)
842 __stop_device_on_lcu(device, pos);
843 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
844 list_for_each_entry(pos, &pavgroup->baselist, alias_list)
845 __stop_device_on_lcu(device, pos);
846 list_for_each_entry(pos, &pavgroup->aliaslist, alias_list)
847 __stop_device_on_lcu(device, pos);
851 static void _unstop_all_devices_on_lcu(struct alias_lcu *lcu)
853 struct alias_pav_group *pavgroup;
854 struct dasd_device *device;
855 unsigned long flags;
857 list_for_each_entry(device, &lcu->active_devices, alias_list) {
858 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
859 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
860 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
863 list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
864 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
865 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
866 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
869 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
870 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
871 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
872 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
873 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
874 flags);
876 list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
877 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
878 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
879 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
880 flags);
885 static void summary_unit_check_handling_work(struct work_struct *work)
887 struct alias_lcu *lcu;
888 struct summary_unit_check_work_data *suc_data;
889 unsigned long flags;
890 struct dasd_device *device;
892 suc_data = container_of(work, struct summary_unit_check_work_data,
893 worker);
894 lcu = container_of(suc_data, struct alias_lcu, suc_data);
895 device = suc_data->device;
897 /* 1. flush alias devices */
898 flush_all_alias_devices_on_lcu(lcu);
900 /* 2. reset summary unit check */
901 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
902 dasd_device_remove_stop_bits(device,
903 (DASD_STOPPED_SU | DASD_STOPPED_PENDING));
904 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
905 reset_summary_unit_check(lcu, device, suc_data->reason);
907 spin_lock_irqsave(&lcu->lock, flags);
908 _unstop_all_devices_on_lcu(lcu);
909 _restart_all_base_devices_on_lcu(lcu);
910 /* 3. read new alias configuration */
911 _schedule_lcu_update(lcu, device);
912 lcu->suc_data.device = NULL;
913 spin_unlock_irqrestore(&lcu->lock, flags);
917 * note: this will be called from int handler context (cdev locked)
919 void dasd_alias_handle_summary_unit_check(struct dasd_device *device,
920 struct irb *irb)
922 struct alias_lcu *lcu;
923 char reason;
924 struct dasd_eckd_private *private;
925 char *sense;
927 private = (struct dasd_eckd_private *) device->private;
929 sense = dasd_get_sense(irb);
930 if (sense) {
931 reason = sense[8];
932 DBF_DEV_EVENT(DBF_NOTICE, device, "%s %x",
933 "eckd handle summary unit check: reason", reason);
934 } else {
935 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
936 "eckd handle summary unit check:"
937 " no reason code available");
938 return;
941 lcu = private->lcu;
942 if (!lcu) {
943 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
944 "device not ready to handle summary"
945 " unit check (no lcu structure)");
946 return;
948 spin_lock(&lcu->lock);
949 _stop_all_devices_on_lcu(lcu, device);
950 /* prepare for lcu_update */
951 private->lcu->flags |= NEED_UAC_UPDATE | UPDATE_PENDING;
952 /* If this device is about to be removed just return and wait for
953 * the next interrupt on a different device
955 if (list_empty(&device->alias_list)) {
956 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
957 "device is in offline processing,"
958 " don't do summary unit check handling");
959 spin_unlock(&lcu->lock);
960 return;
962 if (lcu->suc_data.device) {
963 /* already scheduled or running */
964 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
965 "previous instance of summary unit check worker"
966 " still pending");
967 spin_unlock(&lcu->lock);
968 return ;
970 lcu->suc_data.reason = reason;
971 lcu->suc_data.device = device;
972 spin_unlock(&lcu->lock);
973 schedule_work(&lcu->suc_data.worker);