drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / s390 / block / dasd_alias.c
CommitLineData
8e09f215
SW
1/*
2 * PAV alias management for the DASD ECKD discipline
3 *
a53c8fab 4 * Copyright IBM Corp. 2007
8e09f215
SW
5 * Author(s): Stefan Weinhuber <wein@de.ibm.com>
6 */
7
ca99dab0 8#define KMSG_COMPONENT "dasd-eckd"
fc19f381 9
8e09f215 10#include <linux/list.h>
5a0e3ad6 11#include <linux/slab.h>
8e09f215
SW
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
43static void summary_unit_check_handling_work(struct work_struct *);
44static void lcu_update_work(struct work_struct *);
45static int _schedule_lcu_update(struct alias_lcu *, struct dasd_device *);
46
47static struct alias_root aliastree = {
48 .serverlist = LIST_HEAD_INIT(aliastree.serverlist),
49 .lock = __SPIN_LOCK_UNLOCKED(aliastree.lock),
50};
51
52static 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
65static 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
76static 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) {
4abb08c2
SW
97 if (pos->uid.base_unit_addr == search_unit_addr &&
98 !strncmp(pos->uid.vduit, uid->vduit, sizeof(uid->vduit)))
8e09f215
SW
99 return pos;
100 };
101 return NULL;
102}
103
104static 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
118static void _free_server(struct alias_server *server)
119{
120 kfree(server);
121}
122
123static 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);
f4ac1d02 156 init_completion(&lcu->lcu_setup);
8e09f215
SW
157 return lcu;
158
159out_err4:
160 kfree(lcu->rsu_cqr->cpaddr);
161out_err3:
162 kfree(lcu->rsu_cqr);
163out_err2:
164 kfree(lcu->uac);
165out_err1:
166 kfree(lcu);
167 return ERR_PTR(-ENOMEM);
168}
169
170static 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 */
186int 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;
2dedf0d9 192 struct dasd_uid uid;
8e09f215
SW
193
194 private = (struct dasd_eckd_private *) device->private;
2dedf0d9
SH
195
196 device->discipline->get_uid(device, &uid);
8e09f215 197 spin_lock_irqsave(&aliastree.lock, flags);
2dedf0d9 198 server = _find_server(&uid);
8e09f215
SW
199 if (!server) {
200 spin_unlock_irqrestore(&aliastree.lock, flags);
2dedf0d9 201 newserver = _allocate_server(&uid);
8e09f215
SW
202 if (IS_ERR(newserver))
203 return PTR_ERR(newserver);
204 spin_lock_irqsave(&aliastree.lock, flags);
2dedf0d9 205 server = _find_server(&uid);
8e09f215
SW
206 if (!server) {
207 list_add(&newserver->server, &aliastree.serverlist);
208 server = newserver;
8e09f215
SW
209 } else {
210 /* someone was faster */
211 _free_server(newserver);
212 }
213 }
214
2dedf0d9 215 lcu = _find_lcu(server, &uid);
8e09f215
SW
216 if (!lcu) {
217 spin_unlock_irqrestore(&aliastree.lock, flags);
2dedf0d9 218 newlcu = _allocate_lcu(&uid);
8e09f215 219 if (IS_ERR(newlcu))
6d53cfe5 220 return PTR_ERR(newlcu);
8e09f215 221 spin_lock_irqsave(&aliastree.lock, flags);
2dedf0d9 222 lcu = _find_lcu(server, &uid);
8e09f215
SW
223 if (!lcu) {
224 list_add(&newlcu->lcu, &server->lculist);
225 lcu = newlcu;
8e09f215
SW
226 } else {
227 /* someone was faster */
228 _free_lcu(newlcu);
229 }
8e09f215
SW
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
f9f8d02f 237 return 0;
f4ac1d02
SW
238}
239
8e09f215
SW
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 */
245void 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;
2dedf0d9 252 struct dasd_uid uid;
8e09f215
SW
253
254 private = (struct dasd_eckd_private *) device->private;
255 lcu = private->lcu;
f602f6d6
SH
256 /* nothing to do if already disconnected */
257 if (!lcu)
258 return;
2dedf0d9 259 device->discipline->get_uid(device, &uid);
8e09f215
SW
260 spin_lock_irqsave(&lcu->lock, flags);
261 list_del_init(&device->alias_list);
262 /* make sure that the workers don't use this device */
263 if (device == lcu->suc_data.device) {
264 spin_unlock_irqrestore(&lcu->lock, flags);
265 cancel_work_sync(&lcu->suc_data.worker);
266 spin_lock_irqsave(&lcu->lock, flags);
509a4bcc
SH
267 if (device == lcu->suc_data.device) {
268 dasd_put_device(device);
8e09f215 269 lcu->suc_data.device = NULL;
509a4bcc 270 }
8e09f215
SW
271 }
272 was_pending = 0;
273 if (device == lcu->ruac_data.device) {
274 spin_unlock_irqrestore(&lcu->lock, flags);
275 was_pending = 1;
276 cancel_delayed_work_sync(&lcu->ruac_data.dwork);
277 spin_lock_irqsave(&lcu->lock, flags);
509a4bcc
SH
278 if (device == lcu->ruac_data.device) {
279 dasd_put_device(device);
8e09f215 280 lcu->ruac_data.device = NULL;
509a4bcc 281 }
8e09f215
SW
282 }
283 private->lcu = NULL;
284 spin_unlock_irqrestore(&lcu->lock, flags);
285
286 spin_lock_irqsave(&aliastree.lock, flags);
287 spin_lock(&lcu->lock);
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 }
2dedf0d9 300 server = _find_server(&uid);
8e09f215
SW
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 */
2dedf0d9 313
8e09f215 314static int _add_device_to_lcu(struct alias_lcu *lcu,
2dedf0d9
SH
315 struct dasd_device *device,
316 struct dasd_device *pos)
8e09f215
SW
317{
318
319 struct dasd_eckd_private *private;
320 struct alias_pav_group *group;
2dedf0d9
SH
321 struct dasd_uid uid;
322 unsigned long flags;
8e09f215
SW
323
324 private = (struct dasd_eckd_private *) device->private;
2dedf0d9
SH
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);
8e09f215
SW
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
2dedf0d9 344 group = _find_group(lcu, &uid);
8e09f215
SW
345 if (!group) {
346 group = kzalloc(sizeof(*group), GFP_ATOMIC);
347 if (!group)
348 return -ENOMEM;
2dedf0d9
SH
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;
8e09f215 354 else
2dedf0d9
SH
355 group->uid.base_unit_addr = uid.base_unit_addr;
356 memcpy(group->uid.vduit, uid.vduit, sizeof(uid.vduit));
8e09f215
SW
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 }
2dedf0d9 362 if (uid.type == UA_BASE_DEVICE)
8e09f215
SW
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
370static 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
03429f34
SH
391static int
392suborder_not_supported(struct dasd_ccw_req *cqr)
393{
394 char *sense;
395 char reason;
396 char msg_format;
397 char msg_no;
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
8e09f215
SW
414static 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
68b781fe 423 cqr = dasd_kmalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
8e09f215
SW
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
1aae0560 455 cqr->buildclk = get_tod_clock();
8e09f215
SW
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 do {
464 rc = dasd_sleep_on(cqr);
03429f34
SH
465 if (rc && suborder_not_supported(cqr))
466 return -EOPNOTSUPP;
8e09f215
SW
467 } while (rc && (cqr->retries > 0));
468 if (rc) {
469 spin_lock_irqsave(&lcu->lock, flags);
470 lcu->flags |= NEED_UAC_UPDATE;
471 spin_unlock_irqrestore(&lcu->lock, flags);
472 }
473 dasd_kfree_request(cqr, cqr->memdev);
474 return rc;
475}
476
477static int _lcu_update(struct dasd_device *refdev, struct alias_lcu *lcu)
478{
479 unsigned long flags;
480 struct alias_pav_group *pavgroup, *tempgroup;
481 struct dasd_device *device, *tempdev;
482 int i, rc;
483 struct dasd_eckd_private *private;
484
485 spin_lock_irqsave(&lcu->lock, flags);
486 list_for_each_entry_safe(pavgroup, tempgroup, &lcu->grouplist, group) {
487 list_for_each_entry_safe(device, tempdev, &pavgroup->baselist,
488 alias_list) {
489 list_move(&device->alias_list, &lcu->active_devices);
490 private = (struct dasd_eckd_private *) device->private;
491 private->pavgroup = NULL;
492 }
493 list_for_each_entry_safe(device, tempdev, &pavgroup->aliaslist,
494 alias_list) {
495 list_move(&device->alias_list, &lcu->active_devices);
496 private = (struct dasd_eckd_private *) device->private;
497 private->pavgroup = NULL;
498 }
499 list_del(&pavgroup->group);
500 kfree(pavgroup);
501 }
502 spin_unlock_irqrestore(&lcu->lock, flags);
503
504 rc = read_unit_address_configuration(refdev, lcu);
505 if (rc)
506 return rc;
507
2dedf0d9
SH
508 /* need to take cdev lock before lcu lock */
509 spin_lock_irqsave_nested(get_ccwdev_lock(refdev->cdev), flags,
510 CDEV_NESTED_FIRST);
511 spin_lock(&lcu->lock);
8e09f215
SW
512 lcu->pav = NO_PAV;
513 for (i = 0; i < MAX_DEVICES_PER_LCU; ++i) {
514 switch (lcu->uac->unit[i].ua_type) {
515 case UA_BASE_PAV_ALIAS:
516 lcu->pav = BASE_PAV;
517 break;
518 case UA_HYPER_PAV_ALIAS:
519 lcu->pav = HYPER_PAV;
520 break;
521 }
522 if (lcu->pav != NO_PAV)
523 break;
524 }
525
526 list_for_each_entry_safe(device, tempdev, &lcu->active_devices,
527 alias_list) {
2dedf0d9 528 _add_device_to_lcu(lcu, device, refdev);
8e09f215 529 }
2dedf0d9
SH
530 spin_unlock(&lcu->lock);
531 spin_unlock_irqrestore(get_ccwdev_lock(refdev->cdev), flags);
8e09f215
SW
532 return 0;
533}
534
535static void lcu_update_work(struct work_struct *work)
536{
537 struct alias_lcu *lcu;
538 struct read_uac_work_data *ruac_data;
539 struct dasd_device *device;
540 unsigned long flags;
541 int rc;
542
543 ruac_data = container_of(work, struct read_uac_work_data, dwork.work);
544 lcu = container_of(ruac_data, struct alias_lcu, ruac_data);
545 device = ruac_data->device;
546 rc = _lcu_update(device, lcu);
547 /*
548 * Need to check flags again, as there could have been another
549 * prepare_update or a new device a new device while we were still
550 * processing the data
551 */
552 spin_lock_irqsave(&lcu->lock, flags);
03429f34 553 if ((rc && (rc != -EOPNOTSUPP)) || (lcu->flags & NEED_UAC_UPDATE)) {
fc19f381 554 DBF_DEV_EVENT(DBF_WARNING, device, "could not update"
8e09f215 555 " alias data in lcu (rc = %d), retry later", rc);
509a4bcc
SH
556 if (!schedule_delayed_work(&lcu->ruac_data.dwork, 30*HZ))
557 dasd_put_device(device);
8e09f215 558 } else {
509a4bcc 559 dasd_put_device(device);
8e09f215
SW
560 lcu->ruac_data.device = NULL;
561 lcu->flags &= ~UPDATE_PENDING;
562 }
563 spin_unlock_irqrestore(&lcu->lock, flags);
564}
565
566static int _schedule_lcu_update(struct alias_lcu *lcu,
567 struct dasd_device *device)
568{
569 struct dasd_device *usedev = NULL;
570 struct alias_pav_group *group;
571
572 lcu->flags |= NEED_UAC_UPDATE;
573 if (lcu->ruac_data.device) {
574 /* already scheduled or running */
575 return 0;
576 }
577 if (device && !list_empty(&device->alias_list))
578 usedev = device;
579
580 if (!usedev && !list_empty(&lcu->grouplist)) {
581 group = list_first_entry(&lcu->grouplist,
582 struct alias_pav_group, group);
583 if (!list_empty(&group->baselist))
584 usedev = list_first_entry(&group->baselist,
585 struct dasd_device,
586 alias_list);
587 else if (!list_empty(&group->aliaslist))
588 usedev = list_first_entry(&group->aliaslist,
589 struct dasd_device,
590 alias_list);
591 }
592 if (!usedev && !list_empty(&lcu->active_devices)) {
593 usedev = list_first_entry(&lcu->active_devices,
594 struct dasd_device, alias_list);
595 }
596 /*
597 * if we haven't found a proper device yet, give up for now, the next
598 * device that will be set active will trigger an lcu update
599 */
600 if (!usedev)
601 return -EINVAL;
509a4bcc 602 dasd_get_device(usedev);
8e09f215 603 lcu->ruac_data.device = usedev;
509a4bcc
SH
604 if (!schedule_delayed_work(&lcu->ruac_data.dwork, 0))
605 dasd_put_device(usedev);
8e09f215
SW
606 return 0;
607}
608
609int dasd_alias_add_device(struct dasd_device *device)
610{
611 struct dasd_eckd_private *private;
612 struct alias_lcu *lcu;
613 unsigned long flags;
614 int rc;
615
616 private = (struct dasd_eckd_private *) device->private;
617 lcu = private->lcu;
618 rc = 0;
2dedf0d9
SH
619
620 /* need to take cdev lock before lcu lock */
621 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
622 spin_lock(&lcu->lock);
8e09f215 623 if (!(lcu->flags & UPDATE_PENDING)) {
2dedf0d9 624 rc = _add_device_to_lcu(lcu, device, device);
8e09f215
SW
625 if (rc)
626 lcu->flags |= UPDATE_PENDING;
627 }
628 if (lcu->flags & UPDATE_PENDING) {
629 list_move(&device->alias_list, &lcu->active_devices);
630 _schedule_lcu_update(lcu, device);
631 }
2dedf0d9
SH
632 spin_unlock(&lcu->lock);
633 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
8e09f215
SW
634 return rc;
635}
636
501183f2
SH
637int dasd_alias_update_add_device(struct dasd_device *device)
638{
639 struct dasd_eckd_private *private;
640 private = (struct dasd_eckd_private *) device->private;
641 private->lcu->flags |= UPDATE_PENDING;
642 return dasd_alias_add_device(device);
643}
644
8e09f215
SW
645int 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;
f602f6d6
SH
653 /* nothing to do if already removed */
654 if (!lcu)
655 return 0;
8e09f215
SW
656 spin_lock_irqsave(&lcu->lock, flags);
657 _remove_device_from_lcu(lcu, device);
658 spin_unlock_irqrestore(&lcu->lock, flags);
659 return 0;
660}
661
662struct dasd_device *dasd_alias_get_start_dev(struct dasd_device *base_device)
663{
664
665 struct dasd_device *alias_device;
666 struct alias_pav_group *group;
667 struct alias_lcu *lcu;
668 struct dasd_eckd_private *private, *alias_priv;
669 unsigned long flags;
670
671 private = (struct dasd_eckd_private *) base_device->private;
672 group = private->pavgroup;
673 lcu = private->lcu;
674 if (!group || !lcu)
675 return NULL;
676 if (lcu->pav == NO_PAV ||
677 lcu->flags & (NEED_UAC_UPDATE | UPDATE_PENDING))
678 return NULL;
b38f27e8
SH
679 if (unlikely(!(private->features.feature[8] & 0x01))) {
680 /*
681 * PAV enabled but prefix not, very unlikely
682 * seems to be a lost pathgroup
683 * use base device to do IO
684 */
685 DBF_DEV_EVENT(DBF_ERR, base_device, "%s",
686 "Prefix not enabled with PAV enabled\n");
687 return NULL;
688 }
8e09f215
SW
689
690 spin_lock_irqsave(&lcu->lock, flags);
691 alias_device = group->next;
692 if (!alias_device) {
693 if (list_empty(&group->aliaslist)) {
694 spin_unlock_irqrestore(&lcu->lock, flags);
695 return NULL;
696 } else {
697 alias_device = list_first_entry(&group->aliaslist,
698 struct dasd_device,
699 alias_list);
700 }
701 }
702 if (list_is_last(&alias_device->alias_list, &group->aliaslist))
703 group->next = list_first_entry(&group->aliaslist,
704 struct dasd_device, alias_list);
705 else
706 group->next = list_first_entry(&alias_device->alias_list,
707 struct dasd_device, alias_list);
708 spin_unlock_irqrestore(&lcu->lock, flags);
709 alias_priv = (struct dasd_eckd_private *) alias_device->private;
710 if ((alias_priv->count < private->count) && !alias_device->stopped)
711 return alias_device;
712 else
713 return NULL;
714}
715
716/*
717 * Summary unit check handling depends on the way alias devices
718 * are handled so it is done here rather then in dasd_eckd.c
719 */
720static int reset_summary_unit_check(struct alias_lcu *lcu,
721 struct dasd_device *device,
722 char reason)
723{
724 struct dasd_ccw_req *cqr;
725 int rc = 0;
f3eb5384 726 struct ccw1 *ccw;
8e09f215
SW
727
728 cqr = lcu->rsu_cqr;
729 strncpy((char *) &cqr->magic, "ECKD", 4);
730 ASCEBC((char *) &cqr->magic, 4);
f3eb5384
SW
731 ccw = cqr->cpaddr;
732 ccw->cmd_code = DASD_ECKD_CCW_RSCK;
9155d3f2 733 ccw->flags = CCW_FLAG_SLI;
f3eb5384
SW
734 ccw->count = 16;
735 ccw->cda = (__u32)(addr_t) cqr->data;
8e09f215
SW
736 ((char *)cqr->data)[0] = reason;
737
738 clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
739 cqr->retries = 255; /* set retry counter to enable basic ERP */
740 cqr->startdev = device;
741 cqr->memdev = device;
742 cqr->block = NULL;
743 cqr->expires = 5 * HZ;
1aae0560 744 cqr->buildclk = get_tod_clock();
8e09f215
SW
745 cqr->status = DASD_CQR_FILLED;
746
747 rc = dasd_sleep_on_immediatly(cqr);
748 return rc;
749}
750
751static void _restart_all_base_devices_on_lcu(struct alias_lcu *lcu)
752{
753 struct alias_pav_group *pavgroup;
754 struct dasd_device *device;
755 struct dasd_eckd_private *private;
2dedf0d9 756 unsigned long flags;
8e09f215
SW
757
758 /* active and inactive list can contain alias as well as base devices */
759 list_for_each_entry(device, &lcu->active_devices, alias_list) {
760 private = (struct dasd_eckd_private *) device->private;
2dedf0d9
SH
761 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
762 if (private->uid.type != UA_BASE_DEVICE) {
763 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
764 flags);
8e09f215 765 continue;
2dedf0d9
SH
766 }
767 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
8e09f215
SW
768 dasd_schedule_block_bh(device->block);
769 dasd_schedule_device_bh(device);
770 }
771 list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
772 private = (struct dasd_eckd_private *) device->private;
2dedf0d9
SH
773 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
774 if (private->uid.type != UA_BASE_DEVICE) {
775 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
776 flags);
8e09f215 777 continue;
2dedf0d9
SH
778 }
779 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
8e09f215
SW
780 dasd_schedule_block_bh(device->block);
781 dasd_schedule_device_bh(device);
782 }
783 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
784 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
785 dasd_schedule_block_bh(device->block);
786 dasd_schedule_device_bh(device);
787 }
788 }
789}
790
791static void flush_all_alias_devices_on_lcu(struct alias_lcu *lcu)
792{
793 struct alias_pav_group *pavgroup;
794 struct dasd_device *device, *temp;
795 struct dasd_eckd_private *private;
796 int rc;
797 unsigned long flags;
798 LIST_HEAD(active);
799
800 /*
801 * Problem here ist that dasd_flush_device_queue may wait
802 * for termination of a request to complete. We can't keep
803 * the lcu lock during that time, so we must assume that
804 * the lists may have changed.
805 * Idea: first gather all active alias devices in a separate list,
806 * then flush the first element of this list unlocked, and afterwards
807 * check if it is still on the list before moving it to the
808 * active_devices list.
809 */
810
811 spin_lock_irqsave(&lcu->lock, flags);
812 list_for_each_entry_safe(device, temp, &lcu->active_devices,
813 alias_list) {
814 private = (struct dasd_eckd_private *) device->private;
815 if (private->uid.type == UA_BASE_DEVICE)
816 continue;
817 list_move(&device->alias_list, &active);
818 }
819
820 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
821 list_splice_init(&pavgroup->aliaslist, &active);
822 }
823 while (!list_empty(&active)) {
824 device = list_first_entry(&active, struct dasd_device,
825 alias_list);
826 spin_unlock_irqrestore(&lcu->lock, flags);
827 rc = dasd_flush_device_queue(device);
828 spin_lock_irqsave(&lcu->lock, flags);
829 /*
830 * only move device around if it wasn't moved away while we
831 * were waiting for the flush
832 */
833 if (device == list_first_entry(&active,
834 struct dasd_device, alias_list))
835 list_move(&device->alias_list, &lcu->active_devices);
836 }
837 spin_unlock_irqrestore(&lcu->lock, flags);
838}
839
a806170e
HC
840static void __stop_device_on_lcu(struct dasd_device *device,
841 struct dasd_device *pos)
842{
843 /* If pos == device then device is already locked! */
844 if (pos == device) {
eb6e199b 845 dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
a806170e
HC
846 return;
847 }
848 spin_lock(get_ccwdev_lock(pos->cdev));
eb6e199b 849 dasd_device_set_stop_bits(pos, DASD_STOPPED_SU);
a806170e
HC
850 spin_unlock(get_ccwdev_lock(pos->cdev));
851}
852
8e09f215
SW
853/*
854 * This function is called in interrupt context, so the
855 * cdev lock for device is already locked!
856 */
857static void _stop_all_devices_on_lcu(struct alias_lcu *lcu,
858 struct dasd_device *device)
859{
860 struct alias_pav_group *pavgroup;
861 struct dasd_device *pos;
862
a806170e
HC
863 list_for_each_entry(pos, &lcu->active_devices, alias_list)
864 __stop_device_on_lcu(device, pos);
865 list_for_each_entry(pos, &lcu->inactive_devices, alias_list)
866 __stop_device_on_lcu(device, pos);
8e09f215 867 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
a806170e
HC
868 list_for_each_entry(pos, &pavgroup->baselist, alias_list)
869 __stop_device_on_lcu(device, pos);
870 list_for_each_entry(pos, &pavgroup->aliaslist, alias_list)
871 __stop_device_on_lcu(device, pos);
8e09f215
SW
872 }
873}
874
875static void _unstop_all_devices_on_lcu(struct alias_lcu *lcu)
876{
877 struct alias_pav_group *pavgroup;
878 struct dasd_device *device;
879 unsigned long flags;
880
881 list_for_each_entry(device, &lcu->active_devices, alias_list) {
882 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
eb6e199b 883 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
8e09f215
SW
884 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
885 }
886
887 list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
888 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
eb6e199b 889 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
8e09f215
SW
890 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
891 }
892
893 list_for_each_entry(pavgroup, &lcu->grouplist, group) {
894 list_for_each_entry(device, &pavgroup->baselist, alias_list) {
895 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
eb6e199b 896 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
8e09f215
SW
897 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
898 flags);
899 }
900 list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
901 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
eb6e199b 902 dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
8e09f215
SW
903 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev),
904 flags);
905 }
906 }
907}
908
909static void summary_unit_check_handling_work(struct work_struct *work)
910{
911 struct alias_lcu *lcu;
912 struct summary_unit_check_work_data *suc_data;
913 unsigned long flags;
914 struct dasd_device *device;
915
916 suc_data = container_of(work, struct summary_unit_check_work_data,
917 worker);
918 lcu = container_of(suc_data, struct alias_lcu, suc_data);
919 device = suc_data->device;
920
921 /* 1. flush alias devices */
922 flush_all_alias_devices_on_lcu(lcu);
923
924 /* 2. reset summary unit check */
925 spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
eb6e199b
SW
926 dasd_device_remove_stop_bits(device,
927 (DASD_STOPPED_SU | DASD_STOPPED_PENDING));
8e09f215
SW
928 spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
929 reset_summary_unit_check(lcu, device, suc_data->reason);
930
931 spin_lock_irqsave(&lcu->lock, flags);
932 _unstop_all_devices_on_lcu(lcu);
933 _restart_all_base_devices_on_lcu(lcu);
934 /* 3. read new alias configuration */
935 _schedule_lcu_update(lcu, device);
936 lcu->suc_data.device = NULL;
509a4bcc 937 dasd_put_device(device);
8e09f215
SW
938 spin_unlock_irqrestore(&lcu->lock, flags);
939}
940
941/*
942 * note: this will be called from int handler context (cdev locked)
943 */
944void dasd_alias_handle_summary_unit_check(struct dasd_device *device,
945 struct irb *irb)
946{
947 struct alias_lcu *lcu;
948 char reason;
949 struct dasd_eckd_private *private;
f3eb5384 950 char *sense;
8e09f215
SW
951
952 private = (struct dasd_eckd_private *) device->private;
953
f3eb5384
SW
954 sense = dasd_get_sense(irb);
955 if (sense) {
956 reason = sense[8];
957 DBF_DEV_EVENT(DBF_NOTICE, device, "%s %x",
958 "eckd handle summary unit check: reason", reason);
959 } else {
960 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
961 "eckd handle summary unit check:"
962 " no reason code available");
963 return;
964 }
8e09f215
SW
965
966 lcu = private->lcu;
967 if (!lcu) {
fc19f381 968 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
8e09f215
SW
969 "device not ready to handle summary"
970 " unit check (no lcu structure)");
971 return;
972 }
973 spin_lock(&lcu->lock);
974 _stop_all_devices_on_lcu(lcu, device);
975 /* prepare for lcu_update */
976 private->lcu->flags |= NEED_UAC_UPDATE | UPDATE_PENDING;
977 /* If this device is about to be removed just return and wait for
978 * the next interrupt on a different device
979 */
980 if (list_empty(&device->alias_list)) {
fc19f381 981 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
8e09f215
SW
982 "device is in offline processing,"
983 " don't do summary unit check handling");
984 spin_unlock(&lcu->lock);
985 return;
986 }
987 if (lcu->suc_data.device) {
988 /* already scheduled or running */
fc19f381 989 DBF_DEV_EVENT(DBF_WARNING, device, "%s",
8e09f215
SW
990 "previous instance of summary unit check worker"
991 " still pending");
992 spin_unlock(&lcu->lock);
993 return ;
994 }
995 lcu->suc_data.reason = reason;
996 lcu->suc_data.device = device;
509a4bcc 997 dasd_get_device(device);
8e09f215 998 spin_unlock(&lcu->lock);
509a4bcc
SH
999 if (!schedule_work(&lcu->suc_data.worker))
1000 dasd_put_device(device);
8e09f215 1001};