From nobody Thu Dec 18 09:27:34 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E17FC1A286 for ; Tue, 2 Jul 2024 02:47:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719888465; cv=none; b=QRvxtZ/th06Sfd96/QaYM6p4ecM7/6uy2XQUZ9wvKX4nazGut5s0hZad8s4wT43LdCc1aKC7bOw2Bn/lastlbwBgzzrjVcL7K3JYQb80SZfo0VK92Lr/oqNICuA7vKlas6WXFcOV/bamsVU9vhQQ33LEW9cT9X+IKMneZs0ePP0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719888465; c=relaxed/simple; bh=BMiyKMLwYO4ZPYOP19tvEv3Gb8DtBupJrrjIS0a8MKs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hVOVnjBFqBcYNC8z3ismtLBFbEeueQ2r/Xk5+FIHqqxISR2Zpm37dXyjRalvkKItK8CSHtWVZJC0RHoMOFnrmmDrUhdsNgGDjfAfhB2nQ2DwE50ijls+tR0nxNcB3k/AlEtZSEUdFyDdODcmjgIu50+v15VkPFj2fNQLRbv2NWY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ipylypiv.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jmQNRHd1; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ipylypiv.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jmQNRHd1" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-71ac764062eso3416563a12.0 for ; Mon, 01 Jul 2024 19:47:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1719888463; x=1720493263; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4oDyu1t8Xgx68nOCglw7QJXgCM7MJOxOJdmoGHyawoE=; b=jmQNRHd1h2gdjbHaftwZc3f7TqhCnGEl9fuKWmjLdTS7mx4vUn6rhehSw3gA0fyKv0 cDaCfCbXp1ty/TsnfSbfaPdznVpq2p+24CMRhngpJYt8QtcKf8V+3KhSXaUKumUsnyKN zDfC++oauGoKaihvRUbuyGDaImK5UlW+VB5A27+9E6aqglWxpW+zXx5uB9t760Eta+d9 QLPDpK7dq0O/gB/DKqJWYDDQiJmLifQFYUm6wB3JeFTryw2Tjnr9lbY/Tfk0zubuA5+T apLxAxEuNvrMpQ502H9vFOXUohn5VV6vW4ynZ8C8n4gOx4EsxFE3wWYxOon5l2eXRNhe judQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719888463; x=1720493263; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4oDyu1t8Xgx68nOCglw7QJXgCM7MJOxOJdmoGHyawoE=; b=fDDnPdfdzm4bP2w9Hm2weWxKKOGF9mdh8Lr38b6NcEntKROG3i7fI9oEh+1B2Rwa3v Yf0m6+FiEqgTAlGfhLOnL6B4ZdQ7w+FDlZUeT8Dw+Nq5Xe0QmN044j3lE/W/ozoHuURh zLhEJN86vZCkV2WESS0La5a11mFy1vheIWtQ1E9vlMrYBAEuoGsr4q5qjhJXOjeh4bL6 Fvum94JAX6pj4trnV+WeSkvH1jdsrHUT4R/E2TNJ71EDoLLGfYFIt8/8L3dyMSSPSM37 /erD3G92GhecrFG+KlXb1dq2qH6cS1QevTOmqs3X/q30T0fmJTsdp6w0HBtBq9dHZ2nI JS5w== X-Forwarded-Encrypted: i=1; AJvYcCWzCPTfiHKQGrLoUD31qKnRs6sLiDaTebFba4vTDbrFd7+0V3ZWldkkDbMZHk931CeGTgavlGaIGPzXoWcbeLkv2mqiDExwFSuIyRi6 X-Gm-Message-State: AOJu0YzMYYI0YTAM5NjzQJiPXbygaEI2Gfj8Ul0HkvkMb/8roQGsdmDR zkd227vDA6hpXj1agf3T1wo2jH9Z+wsteujaoFk0AKQSA0PiYk2RbvWlSYJlE+xs2Cq/XvbklJI Op7EqIa6QBQ== X-Google-Smtp-Source: AGHT+IFBvwUGGAdmLQ5Bq25tNXwFEG7MnA2qkz6mLuAQxc8eTODP7wPfu4gagCH1EcQOqtBjBzmTe9QdVanGhw== X-Received: from ip.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:57f3]) (user=ipylypiv job=sendgmr) by 2002:a63:6384:0:b0:6ea:a4f3:14a3 with SMTP id 41be03b00d2f7-74ceedd8de4mr5115a12.4.1719888463253; Mon, 01 Jul 2024 19:47:43 -0700 (PDT) Date: Tue, 2 Jul 2024 02:47:30 +0000 In-Reply-To: <20240702024735.1152293-1-ipylypiv@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240702024735.1152293-1-ipylypiv@google.com> X-Mailer: git-send-email 2.45.2.803.g4e1b14247a-goog Message-ID: <20240702024735.1152293-3-ipylypiv@google.com> Subject: [PATCH v5 2/7] ata: libata-scsi: Do not overwrite valid sense data when CK_COND=1 From: Igor Pylypiv To: Damien Le Moal , Niklas Cassel Cc: Tejun Heo , Hannes Reinecke , linux-ide@vger.kernel.org, linux-kernel@vger.kernel.org, Igor Pylypiv , stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Current ata_gen_passthru_sense() code performs two actions: 1. Generates sense data based on the ATA 'status' and ATA 'error' fields. 2. Populates "ATA Status Return sense data descriptor" / "Fixed format sense data" with ATA taskfile fields. The problem is that #1 generates sense data even when a valid sense data is already present (ATA_QCFLAG_SENSE_VALID is set). Factoring out #2 into a separate function allows us to generate sense data only when there is no valid sense data (ATA_QCFLAG_SENSE_VALID is not set). As a bonus, we can now delete a FIXME comment in atapi_qc_complete() which states that we don't want to translate taskfile registers into sense descriptors for ATAPI. Additionally, always set SAM_STAT_CHECK_CONDITION when CK_COND=3D1 because SAT specification mandates that SATL shall return CHECK CONDITION if the CK_COND bit is set. The ATA PASS-THROUGH handling logic in ata_scsi_qc_complete() is hard to read/understand. Improve the readability of the code by moving checks into self-explanatory boolean variables. Cc: stable@vger.kernel.org # 4.19+ Co-developed-by: Niklas Cassel Signed-off-by: Niklas Cassel Reviewed-by: Hannes Reinecke Reviewed-by: Damien Le Moal Reviewed-by: Niklas Cassel Signed-off-by: Igor Pylypiv --- drivers/ata/libata-scsi.c | 169 +++++++++++++++++++++----------------- 1 file changed, 92 insertions(+), 77 deletions(-) diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c index a9e44ad4c2de..b59cbb5ce5a6 100644 --- a/drivers/ata/libata-scsi.c +++ b/drivers/ata/libata-scsi.c @@ -230,6 +230,80 @@ void ata_scsi_set_sense_information(struct ata_device = *dev, SCSI_SENSE_BUFFERSIZE, information); } =20 +/** + * ata_scsi_set_passthru_sense_fields - Set ATA fields in sense buffer + * @qc: ATA PASS-THROUGH command. + * + * Populates "ATA Status Return sense data descriptor" / "Fixed format + * sense data" with ATA taskfile fields. + * + * LOCKING: + * None. + */ +static void ata_scsi_set_passthru_sense_fields(struct ata_queued_cmd *qc) +{ + struct scsi_cmnd *cmd =3D qc->scsicmd; + struct ata_taskfile *tf =3D &qc->result_tf; + unsigned char *sb =3D cmd->sense_buffer; + + if ((sb[0] & 0x7f) >=3D 0x72) { + unsigned char *desc; + u8 len; + + /* descriptor format */ + len =3D sb[7]; + desc =3D (char *)scsi_sense_desc_find(sb, len + 8, 9); + if (!desc) { + if (SCSI_SENSE_BUFFERSIZE < len + 14) + return; + sb[7] =3D len + 14; + desc =3D sb + 8 + len; + } + desc[0] =3D 9; + desc[1] =3D 12; + /* + * Copy registers into sense buffer. + */ + desc[2] =3D 0x00; + desc[3] =3D tf->error; + desc[5] =3D tf->nsect; + desc[7] =3D tf->lbal; + desc[9] =3D tf->lbam; + desc[11] =3D tf->lbah; + desc[12] =3D tf->device; + desc[13] =3D tf->status; + + /* + * Fill in Extend bit, and the high order bytes + * if applicable. + */ + if (tf->flags & ATA_TFLAG_LBA48) { + desc[2] |=3D 0x01; + desc[4] =3D tf->hob_nsect; + desc[6] =3D tf->hob_lbal; + desc[8] =3D tf->hob_lbam; + desc[10] =3D tf->hob_lbah; + } + } else { + /* Fixed sense format */ + sb[0] |=3D 0x80; + sb[3] =3D tf->error; + sb[4] =3D tf->status; + sb[5] =3D tf->device; + sb[6] =3D tf->nsect; + if (tf->flags & ATA_TFLAG_LBA48) { + sb[8] |=3D 0x80; + if (tf->hob_nsect) + sb[8] |=3D 0x40; + if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah) + sb[8] |=3D 0x20; + } + sb[9] =3D tf->lbal; + sb[10] =3D tf->lbam; + sb[11] =3D tf->lbah; + } +} + static void ata_scsi_set_invalid_field(struct ata_device *dev, struct scsi_cmnd *cmd, u16 field, u8 bit) { @@ -837,10 +911,8 @@ static void ata_to_sense_error(unsigned id, u8 drv_sta= t, u8 drv_err, u8 *sk, * ata_gen_passthru_sense - Generate check condition sense block. * @qc: Command that completed. * - * This function is specific to the ATA descriptor format sense - * block specified for the ATA pass through commands. Regardless - * of whether the command errored or not, return a sense - * block. Copy all controller registers into the sense + * This function is specific to the ATA pass through commands. + * Regardless of whether the command errored or not, return a sense * block. If there was no error, we get the request from an ATA * passthrough command, so we use the following sense data: * sk =3D RECOVERED ERROR @@ -875,63 +947,6 @@ static void ata_gen_passthru_sense(struct ata_queued_c= md *qc) */ scsi_build_sense(cmd, 1, RECOVERED_ERROR, 0, 0x1D); } - - if ((sb[0] & 0x7f) >=3D 0x72) { - unsigned char *desc; - u8 len; - - /* descriptor format */ - len =3D sb[7]; - desc =3D (char *)scsi_sense_desc_find(sb, len + 8, 9); - if (!desc) { - if (SCSI_SENSE_BUFFERSIZE < len + 14) - return; - sb[7] =3D len + 14; - desc =3D sb + 8 + len; - } - desc[0] =3D 9; - desc[1] =3D 12; - /* - * Copy registers into sense buffer. - */ - desc[2] =3D 0x00; - desc[3] =3D tf->error; - desc[5] =3D tf->nsect; - desc[7] =3D tf->lbal; - desc[9] =3D tf->lbam; - desc[11] =3D tf->lbah; - desc[12] =3D tf->device; - desc[13] =3D tf->status; - - /* - * Fill in Extend bit, and the high order bytes - * if applicable. - */ - if (tf->flags & ATA_TFLAG_LBA48) { - desc[2] |=3D 0x01; - desc[4] =3D tf->hob_nsect; - desc[6] =3D tf->hob_lbal; - desc[8] =3D tf->hob_lbam; - desc[10] =3D tf->hob_lbah; - } - } else { - /* Fixed sense format */ - sb[0] |=3D 0x80; - sb[3] =3D tf->error; - sb[4] =3D tf->status; - sb[5] =3D tf->device; - sb[6] =3D tf->nsect; - if (tf->flags & ATA_TFLAG_LBA48) { - sb[8] |=3D 0x80; - if (tf->hob_nsect) - sb[8] |=3D 0x40; - if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah) - sb[8] |=3D 0x20; - } - sb[9] =3D tf->lbal; - sb[10] =3D tf->lbam; - sb[11] =3D tf->lbah; - } } =20 /** @@ -1632,26 +1647,32 @@ static void ata_scsi_qc_complete(struct ata_queued_= cmd *qc) { struct scsi_cmnd *cmd =3D qc->scsicmd; u8 *cdb =3D cmd->cmnd; - int need_sense =3D (qc->err_mask !=3D 0) && - !(qc->flags & ATA_QCFLAG_SENSE_VALID); + bool have_sense =3D qc->flags & ATA_QCFLAG_SENSE_VALID; + bool is_ata_passthru =3D cdb[0] =3D=3D ATA_16 || cdb[0] =3D=3D ATA_12; + bool is_ck_cond_request =3D cdb[2] & 0x20; + bool is_error =3D qc->err_mask !=3D 0; =20 /* For ATA pass thru (SAT) commands, generate a sense block if * user mandated it or if there's an error. Note that if we - * generate because the user forced us to [CK_COND =3D1], a check + * generate because the user forced us to [CK_COND=3D1], a check * condition is generated and the ATA register values are returned * whether the command completed successfully or not. If there - * was no error, we use the following sense data: + * was no error, and CK_COND=3D1, we use the following sense data: * sk =3D RECOVERED ERROR * asc,ascq =3D ATA PASS-THROUGH INFORMATION AVAILABLE */ - if (((cdb[0] =3D=3D ATA_16) || (cdb[0] =3D=3D ATA_12)) && - ((cdb[2] & 0x20) || need_sense)) - ata_gen_passthru_sense(qc); - else if (need_sense) + if (is_ata_passthru && (is_ck_cond_request || is_error || have_sense)) { + if (!have_sense) + ata_gen_passthru_sense(qc); + ata_scsi_set_passthru_sense_fields(qc); + if (is_ck_cond_request) + set_status_byte(qc->scsicmd, SAM_STAT_CHECK_CONDITION); + } else if (is_error && !have_sense) { ata_gen_ata_sense(qc); - else + } else { /* Keep the SCSI ML and status byte, clear host byte. */ cmd->result &=3D 0x0000ffff; + } =20 ata_qc_done(qc); } @@ -2590,14 +2611,8 @@ static void atapi_qc_complete(struct ata_queued_cmd = *qc) /* handle completion from EH */ if (unlikely(err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID)) { =20 - if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) { - /* FIXME: not quite right; we don't want the - * translation of taskfile registers into a - * sense descriptors, since that's only - * correct for ATA, not ATAPI - */ + if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) ata_gen_passthru_sense(qc); - } =20 /* SCSI EH automatically locks door if sdev->locked is * set. Sometimes door lock request continues to --=20 2.45.2.803.g4e1b14247a-goog