#!/bin/bash
# SPDX-License-Identifier: GPL-3.0+
# Copyright (C) 2018 Omar Sandoval
#
# Test that changing the cache type on a SCSI disk succeeds. Regression test
# for commit d772a65d8a6c ("Revert "scsi: core: avoid host-wide host_busy
# counter for scsi_mq""). Without the revert, this test will hang.

. tests/scsi/rc

DESCRIPTION="toggle SCSI cache type"

QUICK=1

device_requires() {
	_test_dev_is_scsi_disk
}

test_device() {
	echo "Running ${TEST_NAME}"

	local cache_types=(
		"write through"
		"none"
		"write back"
		"write back, no read (daft)"
	)
	local cache_type
	local original_cache_type

	original_cache_type="$(cat "${TEST_DEV_SYSFS}"/device/scsi_disk/*/cache_type)"
	for cache_type in "${cache_types[@]}"; do
		echo "$cache_type" > "${TEST_DEV_SYSFS}"/device/scsi_disk/*/cache_type
		cat "${TEST_DEV_SYSFS}"/device/scsi_disk/*/cache_type
	done
	echo "$original_cache_type" > "${TEST_DEV_SYSFS}"/device/scsi_disk/*/cache_type

	echo "Test complete"
}
