GNU Linux-libre 4.4.284-gnu1
[releases.git] / drivers / s390 / block / dasd_alias.c
1 /*
2  * PAV alias management for the DASD ECKD discipline
3  *
4  * Copyright IBM Corp. 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         struct dasd_uid uid;
193
194         private = (struct dasd_eckd_private *) device->private;
195
196         device->discipline->get_uid(device, &uid);
197         spin_lock_irqsave(&aliastree.lock, flags);
198         server = _find_server(&uid);
199         if (!server) {
200                 spin_unlock_irqrestore(&aliastree.lock, flags);
201                 newserver = _allocate_server(&uid);
202                 if (IS_ERR(newserver))
203                         return PTR_ERR(newserver);
204                 spin_lock_irqsave(&aliastree.lock, flags);
205                 server = _find_server(&uid);
206                 if (!server) {
207                         list_add(&newserver->server, &aliastree.serverlist);
208                         server = newserver;
209                 } else {
210                         /* someone was faster */
211                         _free_server(newserver);
212                 }
213         }
214
215         lcu = _find_lcu(server, &uid);
216         if (!lcu) {
217                 spin_unlock_irqrestore(&aliastree.lock, flags);
218                 newlcu = _allocate_lcu(&uid);
219                 if (IS_ERR(newlcu))
220                         return PTR_ERR(newlcu);
221                 spin_lock_irqsave(&aliastree.lock, flags);
222                 lcu = _find_lcu(server, &uid);
223                 if (!lcu) {
224                         list_add(&newlcu->lcu, &server->lculist);
225                         lcu = newlcu;
226                 } else {
227                         /* someone was faster */
228                         _free_lcu(newlcu);
229                 }
230         }
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
237         return 0;
238 }
239
240 /*
241  * 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  */
245 void 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;
252         struct dasd_uid uid;
253
254         private = (struct dasd_eckd_private *) device->private;
255         lcu = private->lcu;
256         /* nothing to do if already disconnected */
257         if (!lcu)
258                 return;
259         device->discipline->get_uid(device, &uid);
260         spin_lock_irqsave(&lcu->lock, flags);
261         /* make sure that the workers don't use this device */
262         if (device == lcu->suc_data.device) {
263                 spin_unlock_irqrestore(&lcu->lock, flags);
264                 cancel_work_sync(&lcu->suc_data.worker);
265                 spin_lock_irqsave(&lcu->lock, flags);
266                 if (device == lcu->suc_data.device) {
267                         dasd_put_device(device);
268                         lcu->suc_data.device = NULL;
269                 }
270         }
271         was_pending = 0;
272         if (device == lcu->ruac_data.device) {
273                 spin_unlock_irqrestore(&lcu->lock, flags);
274                 was_pending = 1;
275                 cancel_delayed_work_sync(&lcu->ruac_data.dwork);
276                 spin_lock_irqsave(&lcu->lock, flags);
277                 if (device == lcu->ruac_data.device) {
278                         dasd_put_device(device);
279                         lcu->ruac_data.device = NULL;
280                 }
281         }
282         private->lcu = NULL;
283         spin_unlock_irqrestore(&lcu->lock, flags);
284
285         spin_lock_irqsave(&aliastree.lock, flags);
286         spin_lock(&lcu->lock);
287         list_del_init(&device->alias_list);
288         if (list_empty(&lcu->grouplist) &&
289             list_empty(&lcu->active_devices) &&
290             list_empty(&lcu->inactive_devices)) {
291                 list_del(&lcu->lcu);
292                 spin_unlock(&lcu->lock);
293                 _free_lcu(lcu);
294                 lcu = NULL;
295         } else {
296                 if (was_pending)
297                         _schedule_lcu_update(lcu, NULL);
298                 spin_unlock(&lcu->lock);
299         }
300         server = _find_server(&uid);
301         if (server && list_empty(&server->lculist)) {
302                 list_del(&server->server);
303                 _free_server(server);
304         }
305         spin_unlock_irqrestore(&aliastree.lock, flags);
306 }
307
308 /*
309  * This function assumes that the unit address configuration stored
310  * in the lcu is up to date and will update the device uid before
311  * adding it to a pav group.
312  */
313
314 static int _add_device_to_lcu(struct alias_lcu *lcu,
315                               struct dasd_device *device,
316                               struct dasd_device *pos)
317 {
318
319         struct dasd_eckd_private *private;
320         struct alias_pav_group *group;
321         struct dasd_uid uid;
322         unsigned long flags;
323
324         private = (struct dasd_eckd_private *) device->private;
325
326         /* only lock if not already locked */
327         if (device != pos)
328                 spin_lock_irqsave_nested(get_ccwdev_lock(device->cdev), flags,
329                                          CDEV_NESTED_SECOND);
330         private->uid.type = lcu->uac->unit[private->uid.real_unit_addr].ua_type;
331         private->uid.base_unit_addr =
332                 lcu->uac->unit[private->uid.real_unit_addr].base_ua;
333         uid = private->uid;
334
335         if (device != pos)
336                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
337
338         /* if we have no PAV anyway, we don't need to bother with PAV groups */
339         if (lcu->pav == NO_PAV) {
340                 list_move(&device->alias_list, &lcu->active_devices);
341                 return 0;
342         }
343
344         group = _find_group(lcu, &uid);
345         if (!group) {
346                 group = kzalloc(sizeof(*group), GFP_ATOMIC);
347                 if (!group)
348                         return -ENOMEM;
349                 memcpy(group->uid.vendor, uid.vendor, sizeof(uid.vendor));
350                 memcpy(group->uid.serial, uid.serial, sizeof(uid.serial));
351                 group->uid.ssid = uid.ssid;
352                 if (uid.type == UA_BASE_DEVICE)
353                         group->uid.base_unit_addr = uid.real_unit_addr;
354                 else
355                         group->uid.base_unit_addr = uid.base_unit_addr;
356                 memcpy(group->uid.vduit, uid.vduit, sizeof(uid.vduit));
357                 INIT_LIST_HEAD(&group->group);
358                 INIT_LIST_HEAD(&group->baselist);
359                 INIT_LIST_HEAD(&group->aliaslist);
360                 list_add(&group->group, &lcu->grouplist);
361         }
362         if (uid.type == UA_BASE_DEVICE)
363                 list_move(&device->alias_list, &group->baselist);
364         else
365                 list_move(&device->alias_list, &group->aliaslist);
366         private->pavgroup = group;
367         return 0;
368 };
369
370 static void _remove_device_from_lcu(struct alias_lcu *lcu,
371                                     struct dasd_device *device)
372 {
373         struct dasd_eckd_private *private;
374         struct alias_pav_group *group;
375
376         private = (struct dasd_eckd_private *) device->private;
377         list_move(&device->alias_list, &lcu->inactive_devices);
378         group = private->pavgroup;
379         if (!group)
380                 return;
381         private->pavgroup = NULL;
382         if (list_empty(&group->baselist) && list_empty(&group->aliaslist)) {
383                 list_del(&group->group);
384                 kfree(group);
385                 return;
386         }
387         if (group->next == device)
388                 group->next = NULL;
389 };
390
391 static int
392 suborder_not_supported(struct dasd_ccw_req *cqr)
393 {
394         char *sense;
395         char reason;
396         char msg_format;
397         char msg_no;
398
399         /*
400          * intrc values ENODEV, ENOLINK and EPERM
401          * will be optained from sleep_on to indicate that no
402          * IO operation can be started
403          */
404         if (cqr->intrc == -ENODEV)
405                 return 1;
406
407         if (cqr->intrc == -ENOLINK)
408                 return 1;
409
410         if (cqr->intrc == -EPERM)
411                 return 1;
412
413         sense = dasd_get_sense(&cqr->irb);
414         if (!sense)
415                 return 0;
416
417         reason = sense[0];
418         msg_format = (sense[7] & 0xF0);
419         msg_no = (sense[7] & 0x0F);
420
421         /* command reject, Format 0 MSG 4 - invalid parameter */
422         if ((reason == 0x80) && (msg_format == 0x00) && (msg_no == 0x04))
423                 return 1;
424
425         return 0;
426 }
427
428 static int read_unit_address_configuration(struct dasd_device *device,
429                                            struct alias_lcu *lcu)
430 {
431         struct dasd_psf_prssd_data *prssdp;
432         struct dasd_ccw_req *cqr;
433         struct ccw1 *ccw;
434         int rc;
435         unsigned long flags;
436
437         cqr = dasd_kmalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
438                                    (sizeof(struct dasd_psf_prssd_data)),
439                                    device);
440         if (IS_ERR(cqr))
441                 return PTR_ERR(cqr);
442         cqr->startdev = device;
443         cqr->memdev = device;
444         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
445         cqr->retries = 10;
446         cqr->expires = 20 * HZ;
447
448         /* Prepare for Read Subsystem Data */
449         prssdp = (struct dasd_psf_prssd_data *) cqr->data;
450         memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
451         prssdp->order = PSF_ORDER_PRSSD;
452         prssdp->suborder = 0x0e;        /* Read unit address configuration */
453         /* all other bytes of prssdp must be zero */
454
455         ccw = cqr->cpaddr;
456         ccw->cmd_code = DASD_ECKD_CCW_PSF;
457         ccw->count = sizeof(struct dasd_psf_prssd_data);
458         ccw->flags |= CCW_FLAG_CC;
459         ccw->cda = (__u32)(addr_t) prssdp;
460
461         /* Read Subsystem Data - feature codes */
462         memset(lcu->uac, 0, sizeof(*(lcu->uac)));
463
464         ccw++;
465         ccw->cmd_code = DASD_ECKD_CCW_RSSD;
466         ccw->count = sizeof(*(lcu->uac));
467         ccw->cda = (__u32)(addr_t) lcu->uac;
468
469         cqr->buildclk = get_tod_clock();
470         cqr->status = DASD_CQR_FILLED;
471
472         /* need to unset flag here to detect race with summary unit check */
473         spin_lock_irqsave(&lcu->lock, flags);
474         lcu->flags &= ~NEED_UAC_UPDATE;
475         spin_unlock_irqrestore(&lcu->lock, flags);
476
477         rc = dasd_sleep_on(cqr);
478         if (!rc)
479                 goto out;
480
481         if (suborder_not_supported(cqr)) {
482                 /* suborder not supported or device unusable for IO */
483                 rc = -EOPNOTSUPP;
484         } else {
485                 /* IO failed but should be retried */
486                 spin_lock_irqsave(&lcu->lock, flags);
487                 lcu->flags |= NEED_UAC_UPDATE;
488                 spin_unlock_irqrestore(&lcu->lock, flags);
489         }
490 out:
491         dasd_kfree_request(cqr, cqr->memdev);
492         return rc;
493 }
494
495 static int _lcu_update(struct dasd_device *refdev, struct alias_lcu *lcu)
496 {
497         unsigned long flags;
498         struct alias_pav_group *pavgroup, *tempgroup;
499         struct dasd_device *device, *tempdev;
500         int i, rc;
501         struct dasd_eckd_private *private;
502
503         spin_lock_irqsave(&lcu->lock, flags);
504         list_for_each_entry_safe(pavgroup, tempgroup, &lcu->grouplist, group) {
505                 list_for_each_entry_safe(device, tempdev, &pavgroup->baselist,
506                                          alias_list) {
507                         list_move(&device->alias_list, &lcu->active_devices);
508                         private = (struct dasd_eckd_private *) device->private;
509                         private->pavgroup = NULL;
510                 }
511                 list_for_each_entry_safe(device, tempdev, &pavgroup->aliaslist,
512                                          alias_list) {
513                         list_move(&device->alias_list, &lcu->active_devices);
514                         private = (struct dasd_eckd_private *) device->private;
515                         private->pavgroup = NULL;
516                 }
517                 list_del(&pavgroup->group);
518                 kfree(pavgroup);
519         }
520         spin_unlock_irqrestore(&lcu->lock, flags);
521
522         rc = read_unit_address_configuration(refdev, lcu);
523         if (rc)
524                 return rc;
525
526         /* need to take cdev lock before lcu lock */
527         spin_lock_irqsave_nested(get_ccwdev_lock(refdev->cdev), flags,
528                                  CDEV_NESTED_FIRST);
529         spin_lock(&lcu->lock);
530         lcu->pav = NO_PAV;
531         for (i = 0; i < MAX_DEVICES_PER_LCU; ++i) {
532                 switch (lcu->uac->unit[i].ua_type) {
533                 case UA_BASE_PAV_ALIAS:
534                         lcu->pav = BASE_PAV;
535                         break;
536                 case UA_HYPER_PAV_ALIAS:
537                         lcu->pav = HYPER_PAV;
538                         break;
539                 }
540                 if (lcu->pav != NO_PAV)
541                         break;
542         }
543
544         list_for_each_entry_safe(device, tempdev, &lcu->active_devices,
545                                  alias_list) {
546                 _add_device_to_lcu(lcu, device, refdev);
547         }
548         spin_unlock(&lcu->lock);
549         spin_unlock_irqrestore(get_ccwdev_lock(refdev->cdev), flags);
550         return 0;
551 }
552
553 static void lcu_update_work(struct work_struct *work)
554 {
555         struct alias_lcu *lcu;
556         struct read_uac_work_data *ruac_data;
557         struct dasd_device *device;
558         unsigned long flags;
559         int rc;
560
561         ruac_data = container_of(work, struct read_uac_work_data, dwork.work);
562         lcu = container_of(ruac_data, struct alias_lcu, ruac_data);
563         device = ruac_data->device;
564         rc = _lcu_update(device, lcu);
565         /*
566          * Need to check flags again, as there could have been another
567          * prepare_update or a new device a new device while we were still
568          * processing the data
569          */
570         spin_lock_irqsave(&lcu->lock, flags);
571         if ((rc && (rc != -EOPNOTSUPP)) || (lcu->flags & NEED_UAC_UPDATE)) {
572                 DBF_DEV_EVENT(DBF_WARNING, device, "could not update"
573                             " alias data in lcu (rc = %d), retry later", rc);
574                 if (!schedule_delayed_work(&lcu->ruac_data.dwork, 30*HZ))
575                         dasd_put_device(device);
576         } else {
577                 dasd_put_device(device);
578                 lcu->ruac_data.device = NULL;
579                 lcu->flags &= ~UPDATE_PENDING;
580         }
581         spin_unlock_irqrestore(&lcu->lock, flags);
582 }
583
584 static int _schedule_lcu_update(struct alias_lcu *lcu,
585                                 struct dasd_device *device)
586 {
587         struct dasd_device *usedev = NULL;
588         struct alias_pav_group *group;
589
590         lcu->flags |= NEED_UAC_UPDATE;
591         if (lcu->ruac_data.device) {
592                 /* already scheduled or running */
593                 return 0;
594         }
595         if (device && !list_empty(&device->alias_list))
596                 usedev = device;
597
598         if (!usedev && !list_empty(&lcu->grouplist)) {
599                 group = list_first_entry(&lcu->grouplist,
600                                          struct alias_pav_group, group);
601                 if (!list_empty(&group->baselist))
602                         usedev = list_first_entry(&group->baselist,
603                                                   struct dasd_device,
604                                                   alias_list);
605                 else if (!list_empty(&group->aliaslist))
606                         usedev = list_first_entry(&group->aliaslist,
607                                                   struct dasd_device,
608                                                   alias_list);
609         }
610         if (!usedev && !list_empty(&lcu->active_devices)) {
611                 usedev = list_first_entry(&lcu->active_devices,
612                                           struct dasd_device, alias_list);
613         }
614         /*
615          * if we haven't found a proper device yet, give up for now, the next
616          * device that will be set active will trigger an lcu update
617          */
618         if (!usedev)
619                 return -EINVAL;
620         dasd_get_device(usedev);
621         lcu->ruac_data.device = usedev;
622         if (!schedule_delayed_work(&lcu->ruac_data.dwork, 0))
623                 dasd_put_device(usedev);
624         return 0;
625 }
626
627 int dasd_alias_add_device(struct dasd_device *device)
628 {
629         struct dasd_eckd_private *private;
630         struct alias_lcu *lcu;
631         unsigned long flags;
632         int rc;
633
634         private = (struct dasd_eckd_private *) device->private;
635         lcu = private->lcu;
636         rc = 0;
637
638         /* need to take cdev lock before lcu lock */
639         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
640         spin_lock(&lcu->lock);
641         if (!(lcu->flags & UPDATE_PENDING)) {
642                 rc = _add_device_to_lcu(lcu, device, device);
643                 if (rc)
644                         lcu->flags |= UPDATE_PENDING;
645         }
646         if (lcu->flags & UPDATE_PENDING) {
647                 list_move(&device->alias_list, &lcu->active_devices);
648                 private->pavgroup = NULL;
649                 _schedule_lcu_update(lcu, device);
650         }
651         spin_unlock(&lcu->lock);
652         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
653         return rc;
654 }
655
656 int dasd_alias_update_add_device(struct dasd_device *device)
657 {
658         struct dasd_eckd_private *private;
659         private = (struct dasd_eckd_private *) device->private;
660         private->lcu->flags |= UPDATE_PENDING;
661         return dasd_alias_add_device(device);
662 }
663
664 int dasd_alias_remove_device(struct dasd_device *device)
665 {
666         struct dasd_eckd_private *private;
667         struct alias_lcu *lcu;
668         unsigned long flags;
669
670         private = (struct dasd_eckd_private *) device->private;
671         lcu = private->lcu;
672         /* nothing to do if already removed */
673         if (!lcu)
674                 return 0;
675         spin_lock_irqsave(&lcu->lock, flags);
676         _remove_device_from_lcu(lcu, device);
677         spin_unlock_irqrestore(&lcu->lock, flags);
678         return 0;
679 }
680
681 struct dasd_device *dasd_alias_get_start_dev(struct dasd_device *base_device)
682 {
683
684         struct dasd_device *alias_device;
685         struct alias_pav_group *group;
686         struct alias_lcu *lcu;
687         struct dasd_eckd_private *private, *alias_priv;
688         unsigned long flags;
689
690         private = (struct dasd_eckd_private *) base_device->private;
691         group = private->pavgroup;
692         lcu = private->lcu;
693         if (!group || !lcu)
694                 return NULL;
695         if (lcu->pav == NO_PAV ||
696             lcu->flags & (NEED_UAC_UPDATE | UPDATE_PENDING))
697                 return NULL;
698         if (unlikely(!(private->features.feature[8] & 0x01))) {
699                 /*
700                  * PAV enabled but prefix not, very unlikely
701                  * seems to be a lost pathgroup
702                  * use base device to do IO
703                  */
704                 DBF_DEV_EVENT(DBF_ERR, base_device, "%s",
705                               "Prefix not enabled with PAV enabled\n");
706                 return NULL;
707         }
708
709         spin_lock_irqsave(&lcu->lock, flags);
710         alias_device = group->next;
711         if (!alias_device) {
712                 if (list_empty(&group->aliaslist)) {
713                         spin_unlock_irqrestore(&lcu->lock, flags);
714                         return NULL;
715                 } else {
716                         alias_device = list_first_entry(&group->aliaslist,
717                                                         struct dasd_device,
718                                                         alias_list);
719                 }
720         }
721         if (list_is_last(&alias_device->alias_list, &group->aliaslist))
722                 group->next = list_first_entry(&group->aliaslist,
723                                                struct dasd_device, alias_list);
724         else
725                 group->next = list_first_entry(&alias_device->alias_list,
726                                                struct dasd_device, alias_list);
727         spin_unlock_irqrestore(&lcu->lock, flags);
728         alias_priv = (struct dasd_eckd_private *) alias_device->private;
729         if ((alias_priv->count < private->count) && !alias_device->stopped &&
730             !test_bit(DASD_FLAG_OFFLINE, &alias_device->flags))
731                 return alias_device;
732         else
733                 return NULL;
734 }
735
736 /*
737  * Summary unit check handling depends on the way alias devices
738  * are handled so it is done here rather then in dasd_eckd.c
739  */
740 static int reset_summary_unit_check(struct alias_lcu *lcu,
741                                     struct dasd_device *device,
742                                     char reason)
743 {
744         struct dasd_ccw_req *cqr;
745         int rc = 0;
746         struct ccw1 *ccw;
747
748         cqr = lcu->rsu_cqr;
749         strncpy((char *) &cqr->magic, "ECKD", 4);
750         ASCEBC((char *) &cqr->magic, 4);
751         ccw = cqr->cpaddr;
752         ccw->cmd_code = DASD_ECKD_CCW_RSCK;
753         ccw->flags = CCW_FLAG_SLI;
754         ccw->count = 16;
755         ccw->cda = (__u32)(addr_t) cqr->data;
756         ((char *)cqr->data)[0] = reason;
757
758         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
759         cqr->retries = 255;     /* set retry counter to enable basic ERP */
760         cqr->startdev = device;
761         cqr->memdev = device;
762         cqr->block = NULL;
763         cqr->expires = 5 * HZ;
764         cqr->buildclk = get_tod_clock();
765         cqr->status = DASD_CQR_FILLED;
766
767         rc = dasd_sleep_on_immediatly(cqr);
768         return rc;
769 }
770
771 static void _restart_all_base_devices_on_lcu(struct alias_lcu *lcu)
772 {
773         struct alias_pav_group *pavgroup;
774         struct dasd_device *device;
775         struct dasd_eckd_private *private;
776         unsigned long flags;
777
778         /* active and inactive list can contain alias as well as base devices */
779         list_for_each_entry(device, &lcu->active_devices, alias_list) {
780                 private = (struct dasd_eckd_private *) device->private;
781                 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
782                 if (private->uid.type != UA_BASE_DEVICE) {
783                         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
784                                                flags);
785                         continue;
786                 }
787                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
788                 dasd_schedule_block_bh(device->block);
789                 dasd_schedule_device_bh(device);
790         }
791         list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
792                 private = (struct dasd_eckd_private *) device->private;
793                 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
794                 if (private->uid.type != UA_BASE_DEVICE) {
795                         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
796                                                flags);
797                         continue;
798                 }
799                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
800                 dasd_schedule_block_bh(device->block);
801                 dasd_schedule_device_bh(device);
802         }
803         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
804                 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
805                         dasd_schedule_block_bh(device->block);
806                         dasd_schedule_device_bh(device);
807                 }
808         }
809 }
810
811 static void flush_all_alias_devices_on_lcu(struct alias_lcu *lcu)
812 {
813         struct alias_pav_group *pavgroup;
814         struct dasd_device *device, *temp;
815         struct dasd_eckd_private *private;
816         int rc;
817         unsigned long flags;
818         LIST_HEAD(active);
819
820         /*
821          * Problem here ist that dasd_flush_device_queue may wait
822          * for termination of a request to complete. We can't keep
823          * the lcu lock during that time, so we must assume that
824          * the lists may have changed.
825          * Idea: first gather all active alias devices in a separate list,
826          * then flush the first element of this list unlocked, and afterwards
827          * check if it is still on the list before moving it to the
828          * active_devices list.
829          */
830
831         spin_lock_irqsave(&lcu->lock, flags);
832         list_for_each_entry_safe(device, temp, &lcu->active_devices,
833                                  alias_list) {
834                 private = (struct dasd_eckd_private *) device->private;
835                 if (private->uid.type == UA_BASE_DEVICE)
836                         continue;
837                 list_move(&device->alias_list, &active);
838         }
839
840         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
841                 list_splice_init(&pavgroup->aliaslist, &active);
842         }
843         while (!list_empty(&active)) {
844                 device = list_first_entry(&active, struct dasd_device,
845                                           alias_list);
846                 spin_unlock_irqrestore(&lcu->lock, flags);
847                 rc = dasd_flush_device_queue(device);
848                 spin_lock_irqsave(&lcu->lock, flags);
849                 /*
850                  * only move device around if it wasn't moved away while we
851                  * were waiting for the flush
852                  */
853                 if (device == list_first_entry(&active,
854                                                struct dasd_device, alias_list)) {
855                         list_move(&device->alias_list, &lcu->active_devices);
856                         private = (struct dasd_eckd_private *) device->private;
857                         private->pavgroup = NULL;
858                 }
859         }
860         spin_unlock_irqrestore(&lcu->lock, flags);
861 }
862
863 static void __stop_device_on_lcu(struct dasd_device *device,
864                                  struct dasd_device *pos)
865 {
866         /* If pos == device then device is already locked! */
867         if (pos == device) {
868                 dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
869                 return;
870         }
871         spin_lock(get_ccwdev_lock(pos->cdev));
872         dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
873         spin_unlock(get_ccwdev_lock(pos->cdev));
874 }
875
876 /*
877  * This function is called in interrupt context, so the
878  * cdev lock for device is already locked!
879  */
880 static void _stop_all_devices_on_lcu(struct alias_lcu *lcu,
881                                      struct dasd_device *device)
882 {
883         struct alias_pav_group *pavgroup;
884         struct dasd_device *pos;
885
886         list_for_each_entry(pos, &lcu->active_devices, alias_list)
887                 __stop_device_on_lcu(device, pos);
888         list_for_each_entry(pos, &lcu->inactive_devices, alias_list)
889                 __stop_device_on_lcu(device, pos);
890         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
891                 list_for_each_entry(pos, &pavgroup->baselist, alias_list)
892                         __stop_device_on_lcu(device, pos);
893                 list_for_each_entry(pos, &pavgroup->aliaslist, alias_list)
894                         __stop_device_on_lcu(device, pos);
895         }
896 }
897
898 static void _unstop_all_devices_on_lcu(struct alias_lcu *lcu)
899 {
900         struct alias_pav_group *pavgroup;
901         struct dasd_device *device;
902         unsigned long flags;
903
904         list_for_each_entry(device, &lcu->active_devices, alias_list) {
905                 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
906                 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
907                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
908         }
909
910         list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
911                 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
912                 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
913                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
914         }
915
916         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
917                 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
918                         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
919                         dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
920                         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
921                                                flags);
922                 }
923                 list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
924                         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
925                         dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
926                         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
927                                                flags);
928                 }
929         }
930 }
931
932 static void summary_unit_check_handling_work(struct work_struct *work)
933 {
934         struct alias_lcu *lcu;
935         struct summary_unit_check_work_data *suc_data;
936         unsigned long flags;
937         struct dasd_device *device;
938
939         suc_data = container_of(work, struct summary_unit_check_work_data,
940                                 worker);
941         lcu = container_of(suc_data, struct alias_lcu, suc_data);
942         device = suc_data->device;
943
944         /* 1. flush alias devices */
945         flush_all_alias_devices_on_lcu(lcu);
946
947         /* 2. reset summary unit check */
948         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
949         dasd_device_remove_stop_bits(device,
950                                      (DASD_STOPPED_SU | DASD_STOPPED_PENDING));
951         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
952         reset_summary_unit_check(lcu, device, suc_data->reason);
953
954         spin_lock_irqsave(&lcu->lock, flags);
955         _unstop_all_devices_on_lcu(lcu);
956         _restart_all_base_devices_on_lcu(lcu);
957         /* 3. read new alias configuration */
958         _schedule_lcu_update(lcu, device);
959         lcu->suc_data.device = NULL;
960         dasd_put_device(device);
961         spin_unlock_irqrestore(&lcu->lock, flags);
962 }
963
964 /*
965  * note: this will be called from int handler context (cdev locked)
966  */
967 void dasd_alias_handle_summary_unit_check(struct dasd_device *device,
968                                           struct irb *irb)
969 {
970         struct alias_lcu *lcu;
971         char reason;
972         struct dasd_eckd_private *private;
973         char *sense;
974
975         private = (struct dasd_eckd_private *) device->private;
976
977         sense = dasd_get_sense(irb);
978         if (sense) {
979                 reason = sense[8];
980                 DBF_DEV_EVENT(DBF_NOTICE, device, "%s %x",
981                             "eckd handle summary unit check: reason", reason);
982         } else {
983                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
984                             "eckd handle summary unit check:"
985                             " no reason code available");
986                 return;
987         }
988
989         lcu = private->lcu;
990         if (!lcu) {
991                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
992                             "device not ready to handle summary"
993                             " unit check (no lcu structure)");
994                 return;
995         }
996         spin_lock(&lcu->lock);
997         _stop_all_devices_on_lcu(lcu, device);
998         /* prepare for lcu_update */
999         private->lcu->flags |= NEED_UAC_UPDATE | UPDATE_PENDING;
1000         /* If this device is about to be removed just return and wait for
1001          * the next interrupt on a different device
1002          */
1003         if (list_empty(&device->alias_list)) {
1004                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1005                             "device is in offline processing,"
1006                             " don't do summary unit check handling");
1007                 spin_unlock(&lcu->lock);
1008                 return;
1009         }
1010         if (lcu->suc_data.device) {
1011                 /* already scheduled or running */
1012                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
1013                             "previous instance of summary unit check worker"
1014                             " still pending");
1015                 spin_unlock(&lcu->lock);
1016                 return ;
1017         }
1018         lcu->suc_data.reason = reason;
1019         lcu->suc_data.device = device;
1020         dasd_get_device(device);
1021         spin_unlock(&lcu->lock);
1022         if (!schedule_work(&lcu->suc_data.worker))
1023                 dasd_put_device(device);
1024 };