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