drivers/scsi/scsi_lib_test.c

Source file repositories/reference/linux-study-clean/drivers/scsi/scsi_lib_test.c

File Facts

System
Linux kernel
Corpus path
drivers/scsi/scsi_lib_test.c
Extension
.c
Size
9708 bytes
Lines
338
Domain
Driver Families
Bucket
drivers/scsi
Inferred role
Driver Families: implementation source
Status
source implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/*
 * KUnit tests for scsi_lib.c.
 *
 * Copyright (C) 2023, Oracle Corporation
 */
#include <kunit/test.h>

#include <scsi/scsi_proto.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>

#define SCSI_LIB_TEST_MAX_ALLOWED 3
#define SCSI_LIB_TEST_TOTAL_MAX_ALLOWED 5

static void scsi_lib_test_multiple_sense(struct kunit *test)
{
	struct scsi_failure multiple_sense_failure_defs[] = {
		{
			.sense = DATA_PROTECT,
			.asc = 0x1,
			.ascq = 0x1,
			.result = SAM_STAT_CHECK_CONDITION,
		},
		{
			.sense = UNIT_ATTENTION,
			.asc = 0x11,
			.ascq = 0x0,
			.allowed = SCSI_LIB_TEST_MAX_ALLOWED,
			.result = SAM_STAT_CHECK_CONDITION,
		},
		{
			.sense = NOT_READY,
			.asc = 0x11,
			.ascq = 0x22,
			.allowed = SCSI_LIB_TEST_MAX_ALLOWED,
			.result = SAM_STAT_CHECK_CONDITION,
		},
		{
			.sense = ABORTED_COMMAND,
			.asc = 0x11,
			.ascq = SCMD_FAILURE_ASCQ_ANY,
			.allowed = SCSI_LIB_TEST_MAX_ALLOWED,
			.result = SAM_STAT_CHECK_CONDITION,
		},
		{
			.sense = HARDWARE_ERROR,
			.asc = SCMD_FAILURE_ASC_ANY,
			.allowed = SCSI_LIB_TEST_MAX_ALLOWED,
			.result = SAM_STAT_CHECK_CONDITION,
		},
		{
			.sense = ILLEGAL_REQUEST,
			.asc = 0x91,
			.ascq = 0x36,
			.allowed = SCSI_LIB_TEST_MAX_ALLOWED,
			.result = SAM_STAT_CHECK_CONDITION,
		},
		{}
	};
	struct scsi_failures failures = {
		.failure_definitions = multiple_sense_failure_defs,
	};
	u8 sense[SCSI_SENSE_BUFFERSIZE] = {};
	struct scsi_cmnd sc = {
		.sense_buffer = sense,
	};
	int i;

	/* Success */
	sc.result = 0;
	KUNIT_EXPECT_EQ(test, 0, scsi_check_passthrough(&sc, &failures));
	KUNIT_EXPECT_EQ(test, 0, scsi_check_passthrough(&sc, NULL));
	/* Command failed but caller did not pass in a failures array */
	scsi_build_sense(&sc, 0, ILLEGAL_REQUEST, 0x91, 0x36);
	KUNIT_EXPECT_EQ(test, 0, scsi_check_passthrough(&sc, NULL));
	/* Match end of array */
	scsi_build_sense(&sc, 0, ILLEGAL_REQUEST, 0x91, 0x36);
	KUNIT_EXPECT_EQ(test, -EAGAIN, scsi_check_passthrough(&sc, &failures));
	/* Basic match in array */
	scsi_build_sense(&sc, 0, UNIT_ATTENTION, 0x11, 0x0);
	KUNIT_EXPECT_EQ(test, -EAGAIN, scsi_check_passthrough(&sc, &failures));
	/* No matching sense entry */
	scsi_build_sense(&sc, 0, MISCOMPARE, 0x11, 0x11);
	KUNIT_EXPECT_EQ(test, 0, scsi_check_passthrough(&sc, &failures));
	/* Match using SCMD_FAILURE_ASCQ_ANY */
	scsi_build_sense(&sc, 0, ABORTED_COMMAND, 0x11, 0x22);
	KUNIT_EXPECT_EQ(test, -EAGAIN, scsi_check_passthrough(&sc, &failures));
	/* Fail to match */
	scsi_build_sense(&sc, 0, ABORTED_COMMAND, 0x22, 0x22);

Annotation

Implementation Notes