[S390] dasd: add dynamic pav toleration
[linux-2.6-block.git] / drivers / s390 / block / dasd_alias.c
1 /*
2  * PAV alias management for the DASD ECKD discipline
3  *
4  * Copyright IBM Corporation, 2007
5  * Author(s): Stefan Weinhuber <wein@de.ibm.com>
6  */
7
8 #define KMSG_COMPONENT "dasd-eckd"
9
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"
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
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 *);
46
47 static struct alias_root aliastree = {
48         .serverlist = LIST_HEAD_INIT(aliastree.serverlist),
49         .lock = __SPIN_LOCK_UNLOCKED(aliastree.lock),
50 };
51
52 static 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;
61         };
62         return NULL;
63 }
64
65 static 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;
72         };
73         return NULL;
74 }
75
76 static 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) {
97                 if (pos->uid.base_unit_addr == search_unit_addr &&
98                     !strncmp(pos->uid.vduit, uid->vduit, sizeof(uid->vduit)))
99                         return pos;
100         };
101         return NULL;
102 }
103
104 static 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
118 static void _free_server(struct alias_server *server)
119 {
120         kfree(server);
121 }
122
123 static 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);
156         init_completion(&lcu->lcu_setup);
157         return lcu;
158
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);
168 }
169
170 static 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  */
186 int 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;
192         int is_lcu_known;
193         struct dasd_uid *uid;
194
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);
214                 }
215         }
216
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);
232                 }
233                 is_lcu_known = 0;
234         }
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);
240
241         return is_lcu_known;
242 }
243
244 /*
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.
252  */
253 void dasd_alias_lcu_setup_complete(struct dasd_device *device)
254 {
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;
260
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;
275         }
276         complete_all(&lcu->lcu_setup);
277 }
278
279 void dasd_alias_wait_for_lcu_setup(struct dasd_device *device)
280 {
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;
286
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;
301         }
302         wait_for_completion(&lcu->lcu_setup);
303 }
304
305 /*
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.
309  */
310 void dasd_alias_disconnect_device_from_lcu(struct dasd_device *device)
311 {
312         struct dasd_eckd_private *private;
313         unsigned long flags;
314         struct alias_lcu *lcu;
315         struct alias_server *server;
316         int was_pending;
317
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;
329         }
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;
338         }
339         private->lcu = NULL;
340         spin_unlock_irqrestore(&lcu->lock, flags);
341
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);
355         }
356         server = _find_server(&private->uid);
357         if (server && list_empty(&server->lculist)) {
358                 list_del(&server->server);
359                 _free_server(server);
360         }
361         spin_unlock_irqrestore(&aliastree.lock, flags);
362 }
363
364 /*
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.
368  */
369 static int _add_device_to_lcu(struct alias_lcu *lcu,
370                               struct dasd_device *device)
371 {
372
373         struct dasd_eckd_private *private;
374         struct alias_pav_group *group;
375         struct dasd_uid *uid;
376
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);
382
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;
387         }
388
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);
406         }
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;
413 };
414
415 static void _remove_device_from_lcu(struct alias_lcu *lcu,
416                                     struct dasd_device *device)
417 {
418         struct dasd_eckd_private *private;
419         struct alias_pav_group *group;
420
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;
431         }
432         if (group->next == device)
433                 group->next = NULL;
434 };
435
436 static int read_unit_address_configuration(struct dasd_device *device,
437                                            struct alias_lcu *lcu)
438 {
439         struct dasd_psf_prssd_data *prssdp;
440         struct dasd_ccw_req *cqr;
441         struct ccw1 *ccw;
442         int rc;
443         unsigned long flags;
444
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;
455
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 */
462
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;
468
469         /* Read Subsystem Data - feature codes */
470         memset(lcu->uac, 0, sizeof(*(lcu->uac)));
471
472         ccw++;
473         ccw->cmd_code = DASD_ECKD_CCW_RSSD;
474         ccw->count = sizeof(*(lcu->uac));
475         ccw->cda = (__u32)(addr_t) lcu->uac;
476
477         cqr->buildclk = get_clock();
478         cqr->status = DASD_CQR_FILLED;
479
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);
484
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);
492         }
493         dasd_kfree_request(cqr, cqr->memdev);
494         return rc;
495 }
496
497 static int _lcu_update(struct dasd_device *refdev, struct alias_lcu *lcu)
498 {
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;
504
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;
512                 }
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;
518                 }
519                 list_del(&pavgroup->group);
520                 kfree(pavgroup);
521         }
522         spin_unlock_irqrestore(&lcu->lock, flags);
523
524         rc = read_unit_address_configuration(refdev, lcu);
525         if (rc)
526                 return rc;
527
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;
538                 }
539                 if (lcu->pav != NO_PAV)
540                         break;
541         }
542
543         list_for_each_entry_safe(device, tempdev, &lcu->active_devices,
544                                  alias_list) {
545                 _add_device_to_lcu(lcu, device);
546         }
547         spin_unlock_irqrestore(&lcu->lock, flags);
548         return 0;
549 }
550
551 static void lcu_update_work(struct work_struct *work)
552 {
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;
558
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);
563         /*
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
567          */
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;
576         }
577         spin_unlock_irqrestore(&lcu->lock, flags);
578 }
579
580 static int _schedule_lcu_update(struct alias_lcu *lcu,
581                                 struct dasd_device *device)
582 {
583         struct dasd_device *usedev = NULL;
584         struct alias_pav_group *group;
585
586         lcu->flags |= NEED_UAC_UPDATE;
587         if (lcu->ruac_data.device) {
588                 /* already scheduled or running */
589                 return 0;
590         }
591         if (device && !list_empty(&device->alias_list))
592                 usedev = device;
593
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);
605         }
606         if (!usedev && !list_empty(&lcu->active_devices)) {
607                 usedev = list_first_entry(&lcu->active_devices,
608                                           struct dasd_device, alias_list);
609         }
610         /*
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
613          */
614         if (!usedev)
615                 return -EINVAL;
616         lcu->ruac_data.device = usedev;
617         schedule_delayed_work(&lcu->ruac_data.dwork, 0);
618         return 0;
619 }
620
621 int dasd_alias_add_device(struct dasd_device *device)
622 {
623         struct dasd_eckd_private *private;
624         struct alias_lcu *lcu;
625         unsigned long flags;
626         int rc;
627
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;
636         }
637         if (lcu->flags & UPDATE_PENDING) {
638                 list_move(&device->alias_list, &lcu->active_devices);
639                 _schedule_lcu_update(lcu, device);
640         }
641         spin_unlock_irqrestore(&lcu->lock, flags);
642         return rc;
643 }
644
645 int dasd_alias_update_add_device(struct dasd_device *device)
646 {
647         struct dasd_eckd_private *private;
648         private = (struct dasd_eckd_private *) device->private;
649         private->lcu->flags |= UPDATE_PENDING;
650         return dasd_alias_add_device(device);
651 }
652
653 int dasd_alias_remove_device(struct dasd_device *device)
654 {
655         struct dasd_eckd_private *private;
656         struct alias_lcu *lcu;
657         unsigned long flags;
658
659         private = (struct dasd_eckd_private *) device->private;
660         lcu = private->lcu;
661         spin_lock_irqsave(&lcu->lock, flags);
662         _remove_device_from_lcu(lcu, device);
663         spin_unlock_irqrestore(&lcu->lock, flags);
664         return 0;
665 }
666
667 struct dasd_device *dasd_alias_get_start_dev(struct dasd_device *base_device)
668 {
669
670         struct dasd_device *alias_device;
671         struct alias_pav_group *group;
672         struct alias_lcu *lcu;
673         struct dasd_eckd_private *private, *alias_priv;
674         unsigned long flags;
675
676         private = (struct dasd_eckd_private *) base_device->private;
677         group = private->pavgroup;
678         lcu = private->lcu;
679         if (!group || !lcu)
680                 return NULL;
681         if (lcu->pav == NO_PAV ||
682             lcu->flags & (NEED_UAC_UPDATE | UPDATE_PENDING))
683                 return NULL;
684
685         spin_lock_irqsave(&lcu->lock, flags);
686         alias_device = group->next;
687         if (!alias_device) {
688                 if (list_empty(&group->aliaslist)) {
689                         spin_unlock_irqrestore(&lcu->lock, flags);
690                         return NULL;
691                 } else {
692                         alias_device = list_first_entry(&group->aliaslist,
693                                                         struct dasd_device,
694                                                         alias_list);
695                 }
696         }
697         if (list_is_last(&alias_device->alias_list, &group->aliaslist))
698                 group->next = list_first_entry(&group->aliaslist,
699                                                struct dasd_device, alias_list);
700         else
701                 group->next = list_first_entry(&alias_device->alias_list,
702                                                struct dasd_device, alias_list);
703         spin_unlock_irqrestore(&lcu->lock, flags);
704         alias_priv = (struct dasd_eckd_private *) alias_device->private;
705         if ((alias_priv->count < private->count) && !alias_device->stopped)
706                 return alias_device;
707         else
708                 return NULL;
709 }
710
711 /*
712  * Summary unit check handling depends on the way alias devices
713  * are handled so it is done here rather then in dasd_eckd.c
714  */
715 static int reset_summary_unit_check(struct alias_lcu *lcu,
716                                     struct dasd_device *device,
717                                     char reason)
718 {
719         struct dasd_ccw_req *cqr;
720         int rc = 0;
721         struct ccw1 *ccw;
722
723         cqr = lcu->rsu_cqr;
724         strncpy((char *) &cqr->magic, "ECKD", 4);
725         ASCEBC((char *) &cqr->magic, 4);
726         ccw = cqr->cpaddr;
727         ccw->cmd_code = DASD_ECKD_CCW_RSCK;
728         ccw->flags = 0 ;
729         ccw->count = 16;
730         ccw->cda = (__u32)(addr_t) cqr->data;
731         ((char *)cqr->data)[0] = reason;
732
733         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
734         cqr->retries = 255;     /* set retry counter to enable basic ERP */
735         cqr->startdev = device;
736         cqr->memdev = device;
737         cqr->block = NULL;
738         cqr->expires = 5 * HZ;
739         cqr->buildclk = get_clock();
740         cqr->status = DASD_CQR_FILLED;
741
742         rc = dasd_sleep_on_immediatly(cqr);
743         return rc;
744 }
745
746 static void _restart_all_base_devices_on_lcu(struct alias_lcu *lcu)
747 {
748         struct alias_pav_group *pavgroup;
749         struct dasd_device *device;
750         struct dasd_eckd_private *private;
751
752         /* active and inactive list can contain alias as well as base devices */
753         list_for_each_entry(device, &lcu->active_devices, alias_list) {
754                 private = (struct dasd_eckd_private *) device->private;
755                 if (private->uid.type != UA_BASE_DEVICE)
756                         continue;
757                 dasd_schedule_block_bh(device->block);
758                 dasd_schedule_device_bh(device);
759         }
760         list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
761                 private = (struct dasd_eckd_private *) device->private;
762                 if (private->uid.type != UA_BASE_DEVICE)
763                         continue;
764                 dasd_schedule_block_bh(device->block);
765                 dasd_schedule_device_bh(device);
766         }
767         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
768                 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
769                         dasd_schedule_block_bh(device->block);
770                         dasd_schedule_device_bh(device);
771                 }
772         }
773 }
774
775 static void flush_all_alias_devices_on_lcu(struct alias_lcu *lcu)
776 {
777         struct alias_pav_group *pavgroup;
778         struct dasd_device *device, *temp;
779         struct dasd_eckd_private *private;
780         int rc;
781         unsigned long flags;
782         LIST_HEAD(active);
783
784         /*
785          * Problem here ist that dasd_flush_device_queue may wait
786          * for termination of a request to complete. We can't keep
787          * the lcu lock during that time, so we must assume that
788          * the lists may have changed.
789          * Idea: first gather all active alias devices in a separate list,
790          * then flush the first element of this list unlocked, and afterwards
791          * check if it is still on the list before moving it to the
792          * active_devices list.
793          */
794
795         spin_lock_irqsave(&lcu->lock, flags);
796         list_for_each_entry_safe(device, temp, &lcu->active_devices,
797                                  alias_list) {
798                 private = (struct dasd_eckd_private *) device->private;
799                 if (private->uid.type == UA_BASE_DEVICE)
800                         continue;
801                 list_move(&device->alias_list, &active);
802         }
803
804         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
805                 list_splice_init(&pavgroup->aliaslist, &active);
806         }
807         while (!list_empty(&active)) {
808                 device = list_first_entry(&active, struct dasd_device,
809                                           alias_list);
810                 spin_unlock_irqrestore(&lcu->lock, flags);
811                 rc = dasd_flush_device_queue(device);
812                 spin_lock_irqsave(&lcu->lock, flags);
813                 /*
814                  * only move device around if it wasn't moved away while we
815                  * were waiting for the flush
816                  */
817                 if (device == list_first_entry(&active,
818                                                struct dasd_device, alias_list))
819                         list_move(&device->alias_list, &lcu->active_devices);
820         }
821         spin_unlock_irqrestore(&lcu->lock, flags);
822 }
823
824 static void __stop_device_on_lcu(struct dasd_device *device,
825                                  struct dasd_device *pos)
826 {
827         /* If pos == device then device is already locked! */
828         if (pos == device) {
829                 dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
830                 return;
831         }
832         spin_lock(get_ccwdev_lock(pos->cdev));
833         dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
834         spin_unlock(get_ccwdev_lock(pos->cdev));
835 }
836
837 /*
838  * This function is called in interrupt context, so the
839  * cdev lock for device is already locked!
840  */
841 static void _stop_all_devices_on_lcu(struct alias_lcu *lcu,
842                                      struct dasd_device *device)
843 {
844         struct alias_pav_group *pavgroup;
845         struct dasd_device *pos;
846
847         list_for_each_entry(pos, &lcu->active_devices, alias_list)
848                 __stop_device_on_lcu(device, pos);
849         list_for_each_entry(pos, &lcu->inactive_devices, alias_list)
850                 __stop_device_on_lcu(device, pos);
851         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
852                 list_for_each_entry(pos, &pavgroup->baselist, alias_list)
853                         __stop_device_on_lcu(device, pos);
854                 list_for_each_entry(pos, &pavgroup->aliaslist, alias_list)
855                         __stop_device_on_lcu(device, pos);
856         }
857 }
858
859 static void _unstop_all_devices_on_lcu(struct alias_lcu *lcu)
860 {
861         struct alias_pav_group *pavgroup;
862         struct dasd_device *device;
863         unsigned long flags;
864
865         list_for_each_entry(device, &lcu->active_devices, alias_list) {
866                 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
867                 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
868                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
869         }
870
871         list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
872                 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
873                 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
874                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
875         }
876
877         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
878                 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
879                         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
880                         dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
881                         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
882                                                flags);
883                 }
884                 list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
885                         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
886                         dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
887                         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
888                                                flags);
889                 }
890         }
891 }
892
893 static void summary_unit_check_handling_work(struct work_struct *work)
894 {
895         struct alias_lcu *lcu;
896         struct summary_unit_check_work_data *suc_data;
897         unsigned long flags;
898         struct dasd_device *device;
899
900         suc_data = container_of(work, struct summary_unit_check_work_data,
901                                 worker);
902         lcu = container_of(suc_data, struct alias_lcu, suc_data);
903         device = suc_data->device;
904
905         /* 1. flush alias devices */
906         flush_all_alias_devices_on_lcu(lcu);
907
908         /* 2. reset summary unit check */
909         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
910         dasd_device_remove_stop_bits(device,
911                                      (DASD_STOPPED_SU | DASD_STOPPED_PENDING));
912         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
913         reset_summary_unit_check(lcu, device, suc_data->reason);
914
915         spin_lock_irqsave(&lcu->lock, flags);
916         _unstop_all_devices_on_lcu(lcu);
917         _restart_all_base_devices_on_lcu(lcu);
918         /* 3. read new alias configuration */
919         _schedule_lcu_update(lcu, device);
920         lcu->suc_data.device = NULL;
921         spin_unlock_irqrestore(&lcu->lock, flags);
922 }
923
924 /*
925  * note: this will be called from int handler context (cdev locked)
926  */
927 void dasd_alias_handle_summary_unit_check(struct dasd_device *device,
928                                           struct irb *irb)
929 {
930         struct alias_lcu *lcu;
931         char reason;
932         struct dasd_eckd_private *private;
933         char *sense;
934
935         private = (struct dasd_eckd_private *) device->private;
936
937         sense = dasd_get_sense(irb);
938         if (sense) {
939                 reason = sense[8];
940                 DBF_DEV_EVENT(DBF_NOTICE, device, "%s %x",
941                             "eckd handle summary unit check: reason", reason);
942         } else {
943                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
944                             "eckd handle summary unit check:"
945                             " no reason code available");
946                 return;
947         }
948
949         lcu = private->lcu;
950         if (!lcu) {
951                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
952                             "device not ready to handle summary"
953                             " unit check (no lcu structure)");
954                 return;
955         }
956         spin_lock(&lcu->lock);
957         _stop_all_devices_on_lcu(lcu, device);
958         /* prepare for lcu_update */
959         private->lcu->flags |= NEED_UAC_UPDATE | UPDATE_PENDING;
960         /* If this device is about to be removed just return and wait for
961          * the next interrupt on a different device
962          */
963         if (list_empty(&device->alias_list)) {
964                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
965                             "device is in offline processing,"
966                             " don't do summary unit check handling");
967                 spin_unlock(&lcu->lock);
968                 return;
969         }
970         if (lcu->suc_data.device) {
971                 /* already scheduled or running */
972                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
973                             "previous instance of summary unit check worker"
974                             " still pending");
975                 spin_unlock(&lcu->lock);
976                 return ;
977         }
978         lcu->suc_data.reason = reason;
979         lcu->suc_data.device = device;
980         spin_unlock(&lcu->lock);
981         schedule_work(&lcu->suc_data.worker);
982 };