Linux Audio

Check our new training course

Yocto distribution development and maintenance

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