scsi: scsi_transport_fc: Add store capability to rport port_state in sysfs
authorMuneendra Kumar <muneendra.kumar@broadcom.com>
Wed, 6 Jan 2021 21:49:07 +0000 (03:19 +0530)
committerMartin K. Petersen <martin.petersen@oracle.com>
Fri, 15 Jan 2021 03:55:17 +0000 (22:55 -0500)
Add store capability to the rport port_state using sysfs under
fc_remote_ports/rport-*/port_state.

With this the user can move the port_state from Marginal->Online and
Online->Marginal.

 - Marginal: This interface will set SCMD_NORETRIES_ABORT bit in
   scmd->state for all the pending I/Os on the SCSI device associated with
   target port.

 - Online: This interface will clear SCMD_NORETRIES_ABORT bit in
   scmd->state for all the pending I/Os on the SCSI device associated with
   target port.

The following interface is provided to set the port state to Marginal and
Online respectively:

echo "Marginal" >> /sys/class/fc_remote_ports/rport-X\:Y-Z/port_state
echo "Online" >> /sys/class/fc_remote_ports/rport-X\:Y-Z/port_state

Link: https://lore.kernel.org/r/1609969748-17684-5-git-send-email-muneendra.kumar@broadcom.com
Reviewed-by: Himanshu Madhani <himanshu.madhani@oracle.com>
Reviewed-by: Ewan D. Milne <emilne@redhat.com>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Muneendra Kumar <muneendra.kumar@broadcom.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
drivers/scsi/scsi_transport_fc.c

index ffd2519..da5b503 100644 (file)
@@ -1238,7 +1238,59 @@ show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
 static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
                show_fc_rport_roles, NULL);
 
-fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
+static ssize_t fc_rport_set_marginal_state(struct device *dev,
+                                               struct device_attribute *attr,
+                                               const char *buf, size_t count)
+{
+       struct fc_rport *rport = transport_class_to_rport(dev);
+       enum fc_port_state port_state;
+       int ret = 0;
+
+       ret = get_fc_port_state_match(buf, &port_state);
+       if (ret)
+               return -EINVAL;
+       if (port_state == FC_PORTSTATE_MARGINAL) {
+               /*
+                * Change the state to Marginal only if the
+                * current rport state is Online
+                * Allow only Online->Marginal
+                */
+               if (rport->port_state == FC_PORTSTATE_ONLINE)
+                       rport->port_state = port_state;
+               else
+                       return -EINVAL;
+       } else if (port_state == FC_PORTSTATE_ONLINE) {
+               /*
+                * Change the state to Online only if the
+                * current rport state is Marginal
+                * Allow only Marginal->Online
+                */
+               if (rport->port_state == FC_PORTSTATE_MARGINAL)
+                       rport->port_state = port_state;
+               else
+                       return -EINVAL;
+       } else
+               return -EINVAL;
+       return count;
+}
+
+static ssize_t
+show_fc_rport_port_state(struct device *dev,
+                               struct device_attribute *attr, char *buf)
+{
+       const char *name;
+       struct fc_rport *rport = transport_class_to_rport(dev);
+
+       name = get_fc_port_state_name(rport->port_state);
+       if (!name)
+               return -EINVAL;
+
+       return snprintf(buf, 20, "%s\n", name);
+}
+
+static FC_DEVICE_ATTR(rport, port_state, 0444 | 0200,
+                       show_fc_rport_port_state, fc_rport_set_marginal_state);
+
 fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
 
 /*
@@ -2681,7 +2733,7 @@ fc_attach_transport(struct fc_function_template *ft)
        SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
        SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
        SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
-       SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
+       SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(port_state);
        SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
        SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);