162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *    Copyright IBM Corp. 2002
462306a36Sopenharmony_ci *    Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
562306a36Sopenharmony_ci *		 Martin Schwidefsky (schwidefsky@de.ibm.com)
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Status accumulation and basic sense functions.
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/module.h>
1162306a36Sopenharmony_ci#include <linux/init.h>
1262306a36Sopenharmony_ci#include <linux/io.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <asm/ccwdev.h>
1562306a36Sopenharmony_ci#include <asm/cio.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include "cio.h"
1862306a36Sopenharmony_ci#include "cio_debug.h"
1962306a36Sopenharmony_ci#include "css.h"
2062306a36Sopenharmony_ci#include "device.h"
2162306a36Sopenharmony_ci#include "ioasm.h"
2262306a36Sopenharmony_ci#include "io_sch.h"
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/*
2562306a36Sopenharmony_ci * Check for any kind of channel or interface control check but don't
2662306a36Sopenharmony_ci * issue the message for the console device
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_cistatic void
2962306a36Sopenharmony_ciccw_device_msg_control_check(struct ccw_device *cdev, struct irb *irb)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	struct subchannel *sch = to_subchannel(cdev->dev.parent);
3262306a36Sopenharmony_ci	char dbf_text[15];
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	if (!scsw_is_valid_cstat(&irb->scsw) ||
3562306a36Sopenharmony_ci	    !(scsw_cstat(&irb->scsw) & (SCHN_STAT_CHN_DATA_CHK |
3662306a36Sopenharmony_ci	      SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK)))
3762306a36Sopenharmony_ci		return;
3862306a36Sopenharmony_ci	CIO_MSG_EVENT(0, "Channel-Check or Interface-Control-Check "
3962306a36Sopenharmony_ci		      "received"
4062306a36Sopenharmony_ci		      " ... device %04x on subchannel 0.%x.%04x, dev_stat "
4162306a36Sopenharmony_ci		      ": %02X sch_stat : %02X\n",
4262306a36Sopenharmony_ci		      cdev->private->dev_id.devno, sch->schid.ssid,
4362306a36Sopenharmony_ci		      sch->schid.sch_no,
4462306a36Sopenharmony_ci		      scsw_dstat(&irb->scsw), scsw_cstat(&irb->scsw));
4562306a36Sopenharmony_ci	sprintf(dbf_text, "chk%x", sch->schid.sch_no);
4662306a36Sopenharmony_ci	CIO_TRACE_EVENT(0, dbf_text);
4762306a36Sopenharmony_ci	CIO_HEX_EVENT(0, irb, sizeof(struct irb));
4862306a36Sopenharmony_ci}
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/*
5162306a36Sopenharmony_ci * Some paths became not operational (pno bit in scsw is set).
5262306a36Sopenharmony_ci */
5362306a36Sopenharmony_cistatic void
5462306a36Sopenharmony_ciccw_device_path_notoper(struct ccw_device *cdev)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	struct subchannel *sch;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	sch = to_subchannel(cdev->dev.parent);
5962306a36Sopenharmony_ci	if (cio_update_schib(sch))
6062306a36Sopenharmony_ci		goto doverify;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	CIO_MSG_EVENT(0, "%s(0.%x.%04x) - path(s) %02x are "
6362306a36Sopenharmony_ci		      "not operational \n", __func__,
6462306a36Sopenharmony_ci		      sch->schid.ssid, sch->schid.sch_no,
6562306a36Sopenharmony_ci		      sch->schib.pmcw.pnom);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	sch->lpm &= ~sch->schib.pmcw.pnom;
6862306a36Sopenharmony_cidoverify:
6962306a36Sopenharmony_ci	cdev->private->flags.doverify = 1;
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/*
7362306a36Sopenharmony_ci * Copy valid bits from the extended control word to device irb.
7462306a36Sopenharmony_ci */
7562306a36Sopenharmony_cistatic void
7662306a36Sopenharmony_ciccw_device_accumulate_ecw(struct ccw_device *cdev, struct irb *irb)
7762306a36Sopenharmony_ci{
7862306a36Sopenharmony_ci	/*
7962306a36Sopenharmony_ci	 * Copy extended control bit if it is valid... yes there
8062306a36Sopenharmony_ci	 * are condition that have to be met for the extended control
8162306a36Sopenharmony_ci	 * bit to have meaning. Sick.
8262306a36Sopenharmony_ci	 */
8362306a36Sopenharmony_ci	cdev->private->dma_area->irb.scsw.cmd.ectl = 0;
8462306a36Sopenharmony_ci	if ((irb->scsw.cmd.stctl & SCSW_STCTL_ALERT_STATUS) &&
8562306a36Sopenharmony_ci	    !(irb->scsw.cmd.stctl & SCSW_STCTL_INTER_STATUS))
8662306a36Sopenharmony_ci		cdev->private->dma_area->irb.scsw.cmd.ectl = irb->scsw.cmd.ectl;
8762306a36Sopenharmony_ci	/* Check if extended control word is valid. */
8862306a36Sopenharmony_ci	if (!cdev->private->dma_area->irb.scsw.cmd.ectl)
8962306a36Sopenharmony_ci		return;
9062306a36Sopenharmony_ci	/* Copy concurrent sense / model dependent information. */
9162306a36Sopenharmony_ci	memcpy(&cdev->private->dma_area->irb.ecw, irb->ecw, sizeof(irb->ecw));
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/*
9562306a36Sopenharmony_ci * Check if extended status word is valid.
9662306a36Sopenharmony_ci */
9762306a36Sopenharmony_cistatic int
9862306a36Sopenharmony_ciccw_device_accumulate_esw_valid(struct irb *irb)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	if (!irb->scsw.cmd.eswf &&
10162306a36Sopenharmony_ci	    (irb->scsw.cmd.stctl == SCSW_STCTL_STATUS_PEND))
10262306a36Sopenharmony_ci		return 0;
10362306a36Sopenharmony_ci	if (irb->scsw.cmd.stctl ==
10462306a36Sopenharmony_ci			(SCSW_STCTL_INTER_STATUS|SCSW_STCTL_STATUS_PEND) &&
10562306a36Sopenharmony_ci	    !(irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
10662306a36Sopenharmony_ci		return 0;
10762306a36Sopenharmony_ci	return 1;
10862306a36Sopenharmony_ci}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci/*
11162306a36Sopenharmony_ci * Copy valid bits from the extended status word to device irb.
11262306a36Sopenharmony_ci */
11362306a36Sopenharmony_cistatic void
11462306a36Sopenharmony_ciccw_device_accumulate_esw(struct ccw_device *cdev, struct irb *irb)
11562306a36Sopenharmony_ci{
11662306a36Sopenharmony_ci	struct irb *cdev_irb;
11762306a36Sopenharmony_ci	struct sublog *cdev_sublog, *sublog;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	if (!ccw_device_accumulate_esw_valid(irb))
12062306a36Sopenharmony_ci		return;
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	cdev_irb = &cdev->private->dma_area->irb;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	/* Copy last path used mask. */
12562306a36Sopenharmony_ci	cdev_irb->esw.esw1.lpum = irb->esw.esw1.lpum;
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	/* Copy subchannel logout information if esw is of format 0. */
12862306a36Sopenharmony_ci	if (irb->scsw.cmd.eswf) {
12962306a36Sopenharmony_ci		cdev_sublog = &cdev_irb->esw.esw0.sublog;
13062306a36Sopenharmony_ci		sublog = &irb->esw.esw0.sublog;
13162306a36Sopenharmony_ci		/* Copy extended status flags. */
13262306a36Sopenharmony_ci		cdev_sublog->esf = sublog->esf;
13362306a36Sopenharmony_ci		/*
13462306a36Sopenharmony_ci		 * Copy fields that have a meaning for channel data check
13562306a36Sopenharmony_ci		 * channel control check and interface control check.
13662306a36Sopenharmony_ci		 */
13762306a36Sopenharmony_ci		if (irb->scsw.cmd.cstat & (SCHN_STAT_CHN_DATA_CHK |
13862306a36Sopenharmony_ci				       SCHN_STAT_CHN_CTRL_CHK |
13962306a36Sopenharmony_ci				       SCHN_STAT_INTF_CTRL_CHK)) {
14062306a36Sopenharmony_ci			/* Copy ancillary report bit. */
14162306a36Sopenharmony_ci			cdev_sublog->arep = sublog->arep;
14262306a36Sopenharmony_ci			/* Copy field-validity-flags. */
14362306a36Sopenharmony_ci			cdev_sublog->fvf = sublog->fvf;
14462306a36Sopenharmony_ci			/* Copy storage access code. */
14562306a36Sopenharmony_ci			cdev_sublog->sacc = sublog->sacc;
14662306a36Sopenharmony_ci			/* Copy termination code. */
14762306a36Sopenharmony_ci			cdev_sublog->termc = sublog->termc;
14862306a36Sopenharmony_ci			/* Copy sequence code. */
14962306a36Sopenharmony_ci			cdev_sublog->seqc = sublog->seqc;
15062306a36Sopenharmony_ci		}
15162306a36Sopenharmony_ci		/* Copy device status check. */
15262306a36Sopenharmony_ci		cdev_sublog->devsc = sublog->devsc;
15362306a36Sopenharmony_ci		/* Copy secondary error. */
15462306a36Sopenharmony_ci		cdev_sublog->serr = sublog->serr;
15562306a36Sopenharmony_ci		/* Copy i/o-error alert. */
15662306a36Sopenharmony_ci		cdev_sublog->ioerr = sublog->ioerr;
15762306a36Sopenharmony_ci		/* Copy channel path timeout bit. */
15862306a36Sopenharmony_ci		if (irb->scsw.cmd.cstat & SCHN_STAT_INTF_CTRL_CHK)
15962306a36Sopenharmony_ci			cdev_irb->esw.esw0.erw.cpt = irb->esw.esw0.erw.cpt;
16062306a36Sopenharmony_ci		/* Copy failing storage address validity flag. */
16162306a36Sopenharmony_ci		cdev_irb->esw.esw0.erw.fsavf = irb->esw.esw0.erw.fsavf;
16262306a36Sopenharmony_ci		if (cdev_irb->esw.esw0.erw.fsavf) {
16362306a36Sopenharmony_ci			/* ... and copy the failing storage address. */
16462306a36Sopenharmony_ci			memcpy(cdev_irb->esw.esw0.faddr, irb->esw.esw0.faddr,
16562306a36Sopenharmony_ci			       sizeof (irb->esw.esw0.faddr));
16662306a36Sopenharmony_ci			/* ... and copy the failing storage address format. */
16762306a36Sopenharmony_ci			cdev_irb->esw.esw0.erw.fsaf = irb->esw.esw0.erw.fsaf;
16862306a36Sopenharmony_ci		}
16962306a36Sopenharmony_ci		/* Copy secondary ccw address validity bit. */
17062306a36Sopenharmony_ci		cdev_irb->esw.esw0.erw.scavf = irb->esw.esw0.erw.scavf;
17162306a36Sopenharmony_ci		if (irb->esw.esw0.erw.scavf)
17262306a36Sopenharmony_ci			/* ... and copy the secondary ccw address. */
17362306a36Sopenharmony_ci			cdev_irb->esw.esw0.saddr = irb->esw.esw0.saddr;
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	}
17662306a36Sopenharmony_ci	/* FIXME: DCTI for format 2? */
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	/* Copy authorization bit. */
17962306a36Sopenharmony_ci	cdev_irb->esw.esw0.erw.auth = irb->esw.esw0.erw.auth;
18062306a36Sopenharmony_ci	/* Copy path verification required flag. */
18162306a36Sopenharmony_ci	cdev_irb->esw.esw0.erw.pvrf = irb->esw.esw0.erw.pvrf;
18262306a36Sopenharmony_ci	if (irb->esw.esw0.erw.pvrf)
18362306a36Sopenharmony_ci		cdev->private->flags.doverify = 1;
18462306a36Sopenharmony_ci	/* Copy concurrent sense bit. */
18562306a36Sopenharmony_ci	cdev_irb->esw.esw0.erw.cons = irb->esw.esw0.erw.cons;
18662306a36Sopenharmony_ci	if (irb->esw.esw0.erw.cons)
18762306a36Sopenharmony_ci		cdev_irb->esw.esw0.erw.scnt = irb->esw.esw0.erw.scnt;
18862306a36Sopenharmony_ci}
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci/*
19162306a36Sopenharmony_ci * Accumulate status from irb to devstat.
19262306a36Sopenharmony_ci */
19362306a36Sopenharmony_civoid
19462306a36Sopenharmony_ciccw_device_accumulate_irb(struct ccw_device *cdev, struct irb *irb)
19562306a36Sopenharmony_ci{
19662306a36Sopenharmony_ci	struct irb *cdev_irb;
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	/*
19962306a36Sopenharmony_ci	 * Check if the status pending bit is set in stctl.
20062306a36Sopenharmony_ci	 * If not, the remaining bit have no meaning and we must ignore them.
20162306a36Sopenharmony_ci	 * The esw is not meaningful as well...
20262306a36Sopenharmony_ci	 */
20362306a36Sopenharmony_ci	if (!(scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))
20462306a36Sopenharmony_ci		return;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	/* Check for channel checks and interface control checks. */
20762306a36Sopenharmony_ci	ccw_device_msg_control_check(cdev, irb);
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	/* Check for path not operational. */
21062306a36Sopenharmony_ci	if (scsw_is_valid_pno(&irb->scsw) && scsw_pno(&irb->scsw))
21162306a36Sopenharmony_ci		ccw_device_path_notoper(cdev);
21262306a36Sopenharmony_ci	/* No irb accumulation for transport mode irbs. */
21362306a36Sopenharmony_ci	if (scsw_is_tm(&irb->scsw)) {
21462306a36Sopenharmony_ci		memcpy(&cdev->private->dma_area->irb, irb, sizeof(struct irb));
21562306a36Sopenharmony_ci		return;
21662306a36Sopenharmony_ci	}
21762306a36Sopenharmony_ci	/*
21862306a36Sopenharmony_ci	 * Don't accumulate unsolicited interrupts.
21962306a36Sopenharmony_ci	 */
22062306a36Sopenharmony_ci	if (!scsw_is_solicited(&irb->scsw))
22162306a36Sopenharmony_ci		return;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	cdev_irb = &cdev->private->dma_area->irb;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	/*
22662306a36Sopenharmony_ci	 * If the clear function had been performed, all formerly pending
22762306a36Sopenharmony_ci	 * status at the subchannel has been cleared and we must not pass
22862306a36Sopenharmony_ci	 * intermediate accumulated status to the device driver.
22962306a36Sopenharmony_ci	 */
23062306a36Sopenharmony_ci	if (irb->scsw.cmd.fctl & SCSW_FCTL_CLEAR_FUNC)
23162306a36Sopenharmony_ci		memset(&cdev->private->dma_area->irb, 0, sizeof(struct irb));
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci	/* Copy bits which are valid only for the start function. */
23462306a36Sopenharmony_ci	if (irb->scsw.cmd.fctl & SCSW_FCTL_START_FUNC) {
23562306a36Sopenharmony_ci		/* Copy key. */
23662306a36Sopenharmony_ci		cdev_irb->scsw.cmd.key = irb->scsw.cmd.key;
23762306a36Sopenharmony_ci		/* Copy suspend control bit. */
23862306a36Sopenharmony_ci		cdev_irb->scsw.cmd.sctl = irb->scsw.cmd.sctl;
23962306a36Sopenharmony_ci		/* Accumulate deferred condition code. */
24062306a36Sopenharmony_ci		cdev_irb->scsw.cmd.cc |= irb->scsw.cmd.cc;
24162306a36Sopenharmony_ci		/* Copy ccw format bit. */
24262306a36Sopenharmony_ci		cdev_irb->scsw.cmd.fmt = irb->scsw.cmd.fmt;
24362306a36Sopenharmony_ci		/* Copy prefetch bit. */
24462306a36Sopenharmony_ci		cdev_irb->scsw.cmd.pfch = irb->scsw.cmd.pfch;
24562306a36Sopenharmony_ci		/* Copy initial-status-interruption-control. */
24662306a36Sopenharmony_ci		cdev_irb->scsw.cmd.isic = irb->scsw.cmd.isic;
24762306a36Sopenharmony_ci		/* Copy address limit checking control. */
24862306a36Sopenharmony_ci		cdev_irb->scsw.cmd.alcc = irb->scsw.cmd.alcc;
24962306a36Sopenharmony_ci		/* Copy suppress suspend bit. */
25062306a36Sopenharmony_ci		cdev_irb->scsw.cmd.ssi = irb->scsw.cmd.ssi;
25162306a36Sopenharmony_ci	}
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	/* Take care of the extended control bit and extended control word. */
25462306a36Sopenharmony_ci	ccw_device_accumulate_ecw(cdev, irb);
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	/* Accumulate function control. */
25762306a36Sopenharmony_ci	cdev_irb->scsw.cmd.fctl |= irb->scsw.cmd.fctl;
25862306a36Sopenharmony_ci	/* Copy activity control. */
25962306a36Sopenharmony_ci	cdev_irb->scsw.cmd.actl = irb->scsw.cmd.actl;
26062306a36Sopenharmony_ci	/* Accumulate status control. */
26162306a36Sopenharmony_ci	cdev_irb->scsw.cmd.stctl |= irb->scsw.cmd.stctl;
26262306a36Sopenharmony_ci	/*
26362306a36Sopenharmony_ci	 * Copy ccw address if it is valid. This is a bit simplified
26462306a36Sopenharmony_ci	 * but should be close enough for all practical purposes.
26562306a36Sopenharmony_ci	 */
26662306a36Sopenharmony_ci	if ((irb->scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) ||
26762306a36Sopenharmony_ci	    ((irb->scsw.cmd.stctl ==
26862306a36Sopenharmony_ci	      (SCSW_STCTL_INTER_STATUS|SCSW_STCTL_STATUS_PEND)) &&
26962306a36Sopenharmony_ci	     (irb->scsw.cmd.actl & SCSW_ACTL_DEVACT) &&
27062306a36Sopenharmony_ci	     (irb->scsw.cmd.actl & SCSW_ACTL_SCHACT)) ||
27162306a36Sopenharmony_ci	    (irb->scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
27262306a36Sopenharmony_ci		cdev_irb->scsw.cmd.cpa = irb->scsw.cmd.cpa;
27362306a36Sopenharmony_ci	/* Accumulate device status, but not the device busy flag. */
27462306a36Sopenharmony_ci	cdev_irb->scsw.cmd.dstat &= ~DEV_STAT_BUSY;
27562306a36Sopenharmony_ci	/* dstat is not always valid. */
27662306a36Sopenharmony_ci	if (irb->scsw.cmd.stctl &
27762306a36Sopenharmony_ci	    (SCSW_STCTL_PRIM_STATUS | SCSW_STCTL_SEC_STATUS
27862306a36Sopenharmony_ci	     | SCSW_STCTL_INTER_STATUS | SCSW_STCTL_ALERT_STATUS))
27962306a36Sopenharmony_ci		cdev_irb->scsw.cmd.dstat |= irb->scsw.cmd.dstat;
28062306a36Sopenharmony_ci	/* Accumulate subchannel status. */
28162306a36Sopenharmony_ci	cdev_irb->scsw.cmd.cstat |= irb->scsw.cmd.cstat;
28262306a36Sopenharmony_ci	/* Copy residual count if it is valid. */
28362306a36Sopenharmony_ci	if ((irb->scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
28462306a36Sopenharmony_ci	    (irb->scsw.cmd.cstat & ~(SCHN_STAT_PCI | SCHN_STAT_INCORR_LEN))
28562306a36Sopenharmony_ci	     == 0)
28662306a36Sopenharmony_ci		cdev_irb->scsw.cmd.count = irb->scsw.cmd.count;
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci	/* Take care of bits in the extended status word. */
28962306a36Sopenharmony_ci	ccw_device_accumulate_esw(cdev, irb);
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	/*
29262306a36Sopenharmony_ci	 * Check whether we must issue a SENSE CCW ourselves if there is no
29362306a36Sopenharmony_ci	 * concurrent sense facility installed for the subchannel.
29462306a36Sopenharmony_ci	 * No sense is required if no delayed sense is pending
29562306a36Sopenharmony_ci	 * and we did not get a unit check without sense information.
29662306a36Sopenharmony_ci	 *
29762306a36Sopenharmony_ci	 * Note: We should check for ioinfo[irq]->flags.consns but VM
29862306a36Sopenharmony_ci	 *	 violates the ESA/390 architecture and doesn't present an
29962306a36Sopenharmony_ci	 *	 operand exception for virtual devices without concurrent
30062306a36Sopenharmony_ci	 *	 sense facility available/supported when enabling the
30162306a36Sopenharmony_ci	 *	 concurrent sense facility.
30262306a36Sopenharmony_ci	 */
30362306a36Sopenharmony_ci	if ((cdev_irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
30462306a36Sopenharmony_ci	    !(cdev_irb->esw.esw0.erw.cons))
30562306a36Sopenharmony_ci		cdev->private->flags.dosense = 1;
30662306a36Sopenharmony_ci}
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci/*
30962306a36Sopenharmony_ci * Do a basic sense.
31062306a36Sopenharmony_ci */
31162306a36Sopenharmony_ciint
31262306a36Sopenharmony_ciccw_device_do_sense(struct ccw_device *cdev, struct irb *irb)
31362306a36Sopenharmony_ci{
31462306a36Sopenharmony_ci	struct subchannel *sch;
31562306a36Sopenharmony_ci	struct ccw1 *sense_ccw;
31662306a36Sopenharmony_ci	int rc;
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci	sch = to_subchannel(cdev->dev.parent);
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	/* A sense is required, can we do it now ? */
32162306a36Sopenharmony_ci	if (scsw_actl(&irb->scsw) & (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT))
32262306a36Sopenharmony_ci		/*
32362306a36Sopenharmony_ci		 * we received an Unit Check but we have no final
32462306a36Sopenharmony_ci		 *  status yet, therefore we must delay the SENSE
32562306a36Sopenharmony_ci		 *  processing. We must not report this intermediate
32662306a36Sopenharmony_ci		 *  status to the device interrupt handler.
32762306a36Sopenharmony_ci		 */
32862306a36Sopenharmony_ci		return -EBUSY;
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	/*
33162306a36Sopenharmony_ci	 * We have ending status but no sense information. Do a basic sense.
33262306a36Sopenharmony_ci	 */
33362306a36Sopenharmony_ci	sense_ccw = &to_io_private(sch)->dma_area->sense_ccw;
33462306a36Sopenharmony_ci	sense_ccw->cmd_code = CCW_CMD_BASIC_SENSE;
33562306a36Sopenharmony_ci	sense_ccw->cda = virt_to_phys(cdev->private->dma_area->irb.ecw);
33662306a36Sopenharmony_ci	sense_ccw->count = SENSE_MAX_COUNT;
33762306a36Sopenharmony_ci	sense_ccw->flags = CCW_FLAG_SLI;
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci	rc = cio_start(sch, sense_ccw, 0xff);
34062306a36Sopenharmony_ci	if (rc == -ENODEV || rc == -EACCES)
34162306a36Sopenharmony_ci		dev_fsm_event(cdev, DEV_EVENT_VERIFY);
34262306a36Sopenharmony_ci	return rc;
34362306a36Sopenharmony_ci}
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci/*
34662306a36Sopenharmony_ci * Add information from basic sense to devstat.
34762306a36Sopenharmony_ci */
34862306a36Sopenharmony_civoid
34962306a36Sopenharmony_ciccw_device_accumulate_basic_sense(struct ccw_device *cdev, struct irb *irb)
35062306a36Sopenharmony_ci{
35162306a36Sopenharmony_ci	/*
35262306a36Sopenharmony_ci	 * Check if the status pending bit is set in stctl.
35362306a36Sopenharmony_ci	 * If not, the remaining bit have no meaning and we must ignore them.
35462306a36Sopenharmony_ci	 * The esw is not meaningful as well...
35562306a36Sopenharmony_ci	 */
35662306a36Sopenharmony_ci	if (!(scsw_stctl(&irb->scsw) & SCSW_STCTL_STATUS_PEND))
35762306a36Sopenharmony_ci		return;
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci	/* Check for channel checks and interface control checks. */
36062306a36Sopenharmony_ci	ccw_device_msg_control_check(cdev, irb);
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	/* Check for path not operational. */
36362306a36Sopenharmony_ci	if (scsw_is_valid_pno(&irb->scsw) && scsw_pno(&irb->scsw))
36462306a36Sopenharmony_ci		ccw_device_path_notoper(cdev);
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci	if (!(irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) &&
36762306a36Sopenharmony_ci	    (irb->scsw.cmd.dstat & DEV_STAT_CHN_END)) {
36862306a36Sopenharmony_ci		cdev->private->dma_area->irb.esw.esw0.erw.cons = 1;
36962306a36Sopenharmony_ci		cdev->private->flags.dosense = 0;
37062306a36Sopenharmony_ci	}
37162306a36Sopenharmony_ci	/* Check if path verification is required. */
37262306a36Sopenharmony_ci	if (ccw_device_accumulate_esw_valid(irb) &&
37362306a36Sopenharmony_ci	    irb->esw.esw0.erw.pvrf)
37462306a36Sopenharmony_ci		cdev->private->flags.doverify = 1;
37562306a36Sopenharmony_ci}
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci/*
37862306a36Sopenharmony_ci * This function accumulates the status into the private devstat and
37962306a36Sopenharmony_ci * starts a basic sense if one is needed.
38062306a36Sopenharmony_ci */
38162306a36Sopenharmony_ciint
38262306a36Sopenharmony_ciccw_device_accumulate_and_sense(struct ccw_device *cdev, struct irb *irb)
38362306a36Sopenharmony_ci{
38462306a36Sopenharmony_ci	ccw_device_accumulate_irb(cdev, irb);
38562306a36Sopenharmony_ci	if ((irb->scsw.cmd.actl  & (SCSW_ACTL_DEVACT | SCSW_ACTL_SCHACT)) != 0)
38662306a36Sopenharmony_ci		return -EBUSY;
38762306a36Sopenharmony_ci	/* Check for basic sense. */
38862306a36Sopenharmony_ci	if (cdev->private->flags.dosense &&
38962306a36Sopenharmony_ci	    !(irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK)) {
39062306a36Sopenharmony_ci		cdev->private->dma_area->irb.esw.esw0.erw.cons = 1;
39162306a36Sopenharmony_ci		cdev->private->flags.dosense = 0;
39262306a36Sopenharmony_ci		return 0;
39362306a36Sopenharmony_ci	}
39462306a36Sopenharmony_ci	if (cdev->private->flags.dosense) {
39562306a36Sopenharmony_ci		ccw_device_do_sense(cdev, irb);
39662306a36Sopenharmony_ci		return -EBUSY;
39762306a36Sopenharmony_ci	}
39862306a36Sopenharmony_ci	return 0;
39962306a36Sopenharmony_ci}
40062306a36Sopenharmony_ci
401