xref: /spdk/scripts/ledctl.sh (revision 5867f4da7cc0d8907a509f765c76a5e41b71cec7)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2023 Intel Corporation
4#  All rights reserved.
5
6usage() {
7	echo "Control LED on an NVMe bdev with NPEM capability."
8	echo "Usage: $(basename $0) [BDEV_NAME] [LED_STATE]"
9	echo ""
10	echo "BDEV_NAME: The name of the SPDK NVMe bdev."
11	echo "LED_STATE: The desired state of the LED. If omitted, the current state will be shown."
12	echo "           Consult ledctl documentation for a list of supported states."
13}
14
15if [[ "$#" -lt 1 || "$#" -gt 2 ]]; then
16	usage
17	exit 1
18fi
19
20if ! command -v "ledctl" > /dev/null 2>&1; then
21	echo "ERROR: ledctl is not found." >&2
22	exit 1
23fi
24
25if ! ledctl --help | grep -q -- "--set-slot"; then
26	echo "ERROR: The installed version of ledctl does not support the --set-slot command." >&2
27	exit 1
28fi
29
30scriptdir=$(dirname $0)
31bdev_name=$1
32led_state=$2
33
34# Find the PCI address of the nvme bdev
35if ! bdev_info=$($scriptdir/rpc.py bdev_get_bdevs -b $bdev_name | jq -r '.[0]' 2> /dev/null); then
36	echo "ERROR: bdev $bdev_name not found." >&2
37	exit 1
38fi
39
40nvme_info=$(echo $bdev_info | jq -r '.driver_specific["nvme"] | select(.)')
41if [ -z "$nvme_info" ]; then
42	echo "ERROR: $bdev_name is not an nvme bdev." >&2
43	exit 1
44fi
45
46nvme_pci_addr=$(echo $nvme_info | jq -r '.[0].pci_address | select(.)')
47if [ -z "$nvme_pci_addr" ]; then
48	echo "ERROR: $bdev_name pci address unknown." >&2
49	exit 1
50fi
51
52# Get a list of slots recognized by ledctl
53npem_slots=$(ledctl -P -c NPEM 2> /dev/null | awk '{print $2}')
54
55# Get the slot that the nvme device is attached to
56devpath=$(realpath --relative-to=/sys/devices /sys/bus/pci/devices/$nvme_pci_addr)
57
58while [ "$devpath" != "." ]; do
59	dev=$(basename $devpath)
60
61	if echo $npem_slots | grep -wq $dev; then
62		slot=$dev
63		break
64	fi
65
66	devpath=$(dirname $devpath)
67done
68
69if [ -z "$slot" ]; then
70	echo "ERROR: $bdev_name not recognized by ledctl as NPEM-capable." >&2
71	exit 1
72fi
73
74# Pass the slot to ledctl
75if [ -z "$led_state" ]; then
76	ledctl -G -c NPEM -p $slot
77else
78	ledctl -S -c NPEM -p $slot -s $led_state
79fi
80