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