xref: /freebsd/tests/sys/cam/ctl/ctl.subr (revision d59a76183470685bdf0b88013d2baad1f04f030f)
1# vim: filetype=sh
2
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2024 Axcient
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS DOCUMENTATION IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28load_modules() {
29	if ! kldstat -q -m ctl; then
30		kldload ctl || atf_skip "could not load ctl kernel mod"
31	fi
32	if ! ctladm port -o on -p 0; then
33		atf_skip "could not enable the camsim frontend"
34	fi
35}
36
37find_device() {
38	LUN=$1
39
40	# Rescan camsim
41	# XXX  camsim doesn't update when creating a new device.  Worse, a
42	# rescan won't look for new devices.  So we must disable/re-enable it.
43	# Worse still, enabling it isn't synchronous, so we need a retry loop
44	# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=281000
45	retries=5
46	ctladm port -o off -p 0 >/dev/null
47	ctladm port -o on -p 0 >/dev/null
48	HEXLUN=`printf %x $LUN`
49	while true; do
50		dev=`camcontrol devlist | awk -v lun=$HEXLUN '/FREEBSD CTL/ && $9==lun {split($10, fields, /[,]/); print fields[1];}' | sed 's:[()]::'`
51		if [ -z "$dev" -o ! -c /dev/$dev ]; then
52			retries=$(( $retries - 1 ))
53			if [ $retries -eq 0 ]; then
54				cat lun-create.txt
55				camcontrol devlist
56				atf_fail "Could not find GEOM device"
57			fi
58			sleep 0.1
59			continue
60		fi
61		break
62	done
63	# Ensure that it's actually ready.  camcontrol may report the disk's
64	# ident before it's actually ready to receive commands.  Maybe that's
65	# because all of the GEOM providers must probe it?
66	while true; do
67		dd if=/dev/$dev bs=4096 count=1 of=/dev/null >/dev/null 2>/dev/null && break
68		retries=$(( $retries - 1 ))
69		if [ $retries -eq 0 ]; then
70			atf_fail "Device never became ready"
71		fi
72		sleep 0.1
73	done
74}
75
76# Create a CTL LUN
77create_ramdisk() {
78	EXTRA_ARGS=$*
79
80	atf_check -o save:lun-create.txt ctladm create -b ramdisk -s 1048576 $EXTRA_ARGS
81	atf_check egrep -q "LUN created successfully" lun-create.txt
82	LUN=`awk '/LUN ID:/ {print $NF}' lun-create.txt`
83	if [ -z "$LUN" ]; then
84		atf_fail "Could not find LUN id"
85	fi
86	find_device $LUN
87}
88
89cleanup() {
90	if [ -e "lun-create.txt" ]; then
91		lun_id=`awk '/LUN ID:/ {print $NF}' lun-create.txt`
92		ctladm remove -b ramdisk -l $lun_id > /dev/null
93	fi
94}
95