Merge branch 'iommu/largepages' into amd-iommu/2.6.35
[pandora-kernel.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_remove_device(struct dasd_device *device)
646 {
647         struct dasd_eckd_private *private;
648         struct alias_lcu *lcu;
649         unsigned long flags;
650
651         private = (struct dasd_eckd_private *) device->private;
652         lcu = private->lcu;
653         spin_lock_irqsave(&lcu->lock, flags);
654         _remove_device_from_lcu(lcu, device);
655         spin_unlock_irqrestore(&lcu->lock, flags);
656         return 0;
657 }
658
659 struct dasd_device *dasd_alias_get_start_dev(struct dasd_device *base_device)
660 {
661
662         struct dasd_device *alias_device;
663         struct alias_pav_group *group;
664         struct alias_lcu *lcu;
665         struct dasd_eckd_private *private, *alias_priv;
666         unsigned long flags;
667
668         private = (struct dasd_eckd_private *) base_device->private;
669         group = private->pavgroup;
670         lcu = private->lcu;
671         if (!group || !lcu)
672                 return NULL;
673         if (lcu->pav == NO_PAV ||
674             lcu->flags & (NEED_UAC_UPDATE | UPDATE_PENDING))
675                 return NULL;
676
677         spin_lock_irqsave(&lcu->lock, flags);
678         alias_device = group->next;
679         if (!alias_device) {
680                 if (list_empty(&group->aliaslist)) {
681                         spin_unlock_irqrestore(&lcu->lock, flags);
682                         return NULL;
683                 } else {
684                         alias_device = list_first_entry(&group->aliaslist,
685                                                         struct dasd_device,
686                                                         alias_list);
687                 }
688         }
689         if (list_is_last(&alias_device->alias_list, &group->aliaslist))
690                 group->next = list_first_entry(&group->aliaslist,
691                                                struct dasd_device, alias_list);
692         else
693                 group->next = list_first_entry(&alias_device->alias_list,
694                                                struct dasd_device, alias_list);
695         spin_unlock_irqrestore(&lcu->lock, flags);
696         alias_priv = (struct dasd_eckd_private *) alias_device->private;
697         if ((alias_priv->count < private->count) && !alias_device->stopped)
698                 return alias_device;
699         else
700                 return NULL;
701 }
702
703 /*
704  * Summary unit check handling depends on the way alias devices
705  * are handled so it is done here rather then in dasd_eckd.c
706  */
707 static int reset_summary_unit_check(struct alias_lcu *lcu,
708                                     struct dasd_device *device,
709                                     char reason)
710 {
711         struct dasd_ccw_req *cqr;
712         int rc = 0;
713         struct ccw1 *ccw;
714
715         cqr = lcu->rsu_cqr;
716         strncpy((char *) &cqr->magic, "ECKD", 4);
717         ASCEBC((char *) &cqr->magic, 4);
718         ccw = cqr->cpaddr;
719         ccw->cmd_code = DASD_ECKD_CCW_RSCK;
720         ccw->flags = 0 ;
721         ccw->count = 16;
722         ccw->cda = (__u32)(addr_t) cqr->data;
723         ((char *)cqr->data)[0] = reason;
724
725         clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
726         cqr->retries = 255;     /* set retry counter to enable basic ERP */
727         cqr->startdev = device;
728         cqr->memdev = device;
729         cqr->block = NULL;
730         cqr->expires = 5 * HZ;
731         cqr->buildclk = get_clock();
732         cqr->status = DASD_CQR_FILLED;
733
734         rc = dasd_sleep_on_immediatly(cqr);
735         return rc;
736 }
737
738 static void _restart_all_base_devices_on_lcu(struct alias_lcu *lcu)
739 {
740         struct alias_pav_group *pavgroup;
741         struct dasd_device *device;
742         struct dasd_eckd_private *private;
743
744         /* active and inactive list can contain alias as well as base devices */
745         list_for_each_entry(device, &lcu->active_devices, alias_list) {
746                 private = (struct dasd_eckd_private *) device->private;
747                 if (private->uid.type != UA_BASE_DEVICE)
748                         continue;
749                 dasd_schedule_block_bh(device->block);
750                 dasd_schedule_device_bh(device);
751         }
752         list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
753                 private = (struct dasd_eckd_private *) device->private;
754                 if (private->uid.type != UA_BASE_DEVICE)
755                         continue;
756                 dasd_schedule_block_bh(device->block);
757                 dasd_schedule_device_bh(device);
758         }
759         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
760                 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
761                         dasd_schedule_block_bh(device->block);
762                         dasd_schedule_device_bh(device);
763                 }
764         }
765 }
766
767 static void flush_all_alias_devices_on_lcu(struct alias_lcu *lcu)
768 {
769         struct alias_pav_group *pavgroup;
770         struct dasd_device *device, *temp;
771         struct dasd_eckd_private *private;
772         int rc;
773         unsigned long flags;
774         LIST_HEAD(active);
775
776         /*
777          * Problem here ist that dasd_flush_device_queue may wait
778          * for termination of a request to complete. We can't keep
779          * the lcu lock during that time, so we must assume that
780          * the lists may have changed.
781          * Idea: first gather all active alias devices in a separate list,
782          * then flush the first element of this list unlocked, and afterwards
783          * check if it is still on the list before moving it to the
784          * active_devices list.
785          */
786
787         spin_lock_irqsave(&lcu->lock, flags);
788         list_for_each_entry_safe(device, temp, &lcu->active_devices,
789                                  alias_list) {
790                 private = (struct dasd_eckd_private *) device->private;
791                 if (private->uid.type == UA_BASE_DEVICE)
792                         continue;
793                 list_move(&device->alias_list, &active);
794         }
795
796         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
797                 list_splice_init(&pavgroup->aliaslist, &active);
798         }
799         while (!list_empty(&active)) {
800                 device = list_first_entry(&active, struct dasd_device,
801                                           alias_list);
802                 spin_unlock_irqrestore(&lcu->lock, flags);
803                 rc = dasd_flush_device_queue(device);
804                 spin_lock_irqsave(&lcu->lock, flags);
805                 /*
806                  * only move device around if it wasn't moved away while we
807                  * were waiting for the flush
808                  */
809                 if (device == list_first_entry(&active,
810                                                struct dasd_device, alias_list))
811                         list_move(&device->alias_list, &lcu->active_devices);
812         }
813         spin_unlock_irqrestore(&lcu->lock, flags);
814 }
815
816 static void __stop_device_on_lcu(struct dasd_device *device,
817                                  struct dasd_device *pos)
818 {
819         /* If pos == device then device is already locked! */
820         if (pos == device) {
821                 dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
822                 return;
823         }
824         spin_lock(get_ccwdev_lock(pos->cdev));
825         dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
826         spin_unlock(get_ccwdev_lock(pos->cdev));
827 }
828
829 /*
830  * This function is called in interrupt context, so the
831  * cdev lock for device is already locked!
832  */
833 static void _stop_all_devices_on_lcu(struct alias_lcu *lcu,
834                                      struct dasd_device *device)
835 {
836         struct alias_pav_group *pavgroup;
837         struct dasd_device *pos;
838
839         list_for_each_entry(pos, &lcu->active_devices, alias_list)
840                 __stop_device_on_lcu(device, pos);
841         list_for_each_entry(pos, &lcu->inactive_devices, alias_list)
842                 __stop_device_on_lcu(device, pos);
843         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
844                 list_for_each_entry(pos, &pavgroup->baselist, alias_list)
845                         __stop_device_on_lcu(device, pos);
846                 list_for_each_entry(pos, &pavgroup->aliaslist, alias_list)
847                         __stop_device_on_lcu(device, pos);
848         }
849 }
850
851 static void _unstop_all_devices_on_lcu(struct alias_lcu *lcu)
852 {
853         struct alias_pav_group *pavgroup;
854         struct dasd_device *device;
855         unsigned long flags;
856
857         list_for_each_entry(device, &lcu->active_devices, alias_list) {
858                 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
859                 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
860                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
861         }
862
863         list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
864                 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
865                 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
866                 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
867         }
868
869         list_for_each_entry(pavgroup, &lcu->grouplist, group) {
870                 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
871                         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
872                         dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
873                         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
874                                                flags);
875                 }
876                 list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
877                         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
878                         dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
879                         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
880                                                flags);
881                 }
882         }
883 }
884
885 static void summary_unit_check_handling_work(struct work_struct *work)
886 {
887         struct alias_lcu *lcu;
888         struct summary_unit_check_work_data *suc_data;
889         unsigned long flags;
890         struct dasd_device *device;
891
892         suc_data = container_of(work, struct summary_unit_check_work_data,
893                                 worker);
894         lcu = container_of(suc_data, struct alias_lcu, suc_data);
895         device = suc_data->device;
896
897         /* 1. flush alias devices */
898         flush_all_alias_devices_on_lcu(lcu);
899
900         /* 2. reset summary unit check */
901         spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
902         dasd_device_remove_stop_bits(device,
903                                      (DASD_STOPPED_SU | DASD_STOPPED_PENDING));
904         spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
905         reset_summary_unit_check(lcu, device, suc_data->reason);
906
907         spin_lock_irqsave(&lcu->lock, flags);
908         _unstop_all_devices_on_lcu(lcu);
909         _restart_all_base_devices_on_lcu(lcu);
910         /* 3. read new alias configuration */
911         _schedule_lcu_update(lcu, device);
912         lcu->suc_data.device = NULL;
913         spin_unlock_irqrestore(&lcu->lock, flags);
914 }
915
916 /*
917  * note: this will be called from int handler context (cdev locked)
918  */
919 void dasd_alias_handle_summary_unit_check(struct dasd_device *device,
920                                           struct irb *irb)
921 {
922         struct alias_lcu *lcu;
923         char reason;
924         struct dasd_eckd_private *private;
925         char *sense;
926
927         private = (struct dasd_eckd_private *) device->private;
928
929         sense = dasd_get_sense(irb);
930         if (sense) {
931                 reason = sense[8];
932                 DBF_DEV_EVENT(DBF_NOTICE, device, "%s %x",
933                             "eckd handle summary unit check: reason", reason);
934         } else {
935                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
936                             "eckd handle summary unit check:"
937                             " no reason code available");
938                 return;
939         }
940
941         lcu = private->lcu;
942         if (!lcu) {
943                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
944                             "device not ready to handle summary"
945                             " unit check (no lcu structure)");
946                 return;
947         }
948         spin_lock(&lcu->lock);
949         _stop_all_devices_on_lcu(lcu, device);
950         /* prepare for lcu_update */
951         private->lcu->flags |= NEED_UAC_UPDATE | UPDATE_PENDING;
952         /* If this device is about to be removed just return and wait for
953          * the next interrupt on a different device
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                 spin_unlock(&lcu->lock);
960                 return;
961         }
962         if (lcu->suc_data.device) {
963                 /* already scheduled or running */
964                 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
965                             "previous instance of summary unit check worker"
966                             " still pending");
967                 spin_unlock(&lcu->lock);
968                 return ;
969         }
970         lcu->suc_data.reason = reason;
971         lcu->suc_data.device = device;
972         spin_unlock(&lcu->lock);
973         schedule_work(&lcu->suc_data.worker);
974 };