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