nvme-fc: add uevent for auto-connect
authorJames Smart <jsmart2021@gmail.com>
Thu, 14 Sep 2017 17:38:42 +0000 (10:38 -0700)
committerChristoph Hellwig <hch@lst.de>
Wed, 4 Oct 2017 07:48:20 +0000 (09:48 +0200)
To support auto-connecting to FC-NVME devices upon their dynamic
appearance, add a uevent that can kick off connection scripts.
uevent is posted against the fc_udev device.

patch set tested with the following rule to kick an nvme-cli connect-all
for the FC initiator and FC target ports. This is just an example for
testing and not intended for real life use.

ACTION=="change", SUBSYSTEM=="fc", ENV{FC_EVENT}=="nvmediscovery", \
        ENV{NVMEFC_HOST_TRADDR}=="*", ENV{NVMEFC_TRADDR}=="*", \
RUN+="/bin/sh -c '/usr/local/sbin/nvme connect-all --transport=fc --host-traddr=$env{NVMEFC_HOST_TRADDR} --traddr=$env{NVMEFC_TRADDR} >> /tmp/nvme_fc.log'"

I will post proposed udev/systemd scripts for possible kernel support.

Signed-off-by: James Smart <james.smart@broadcom.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
drivers/nvme/host/fc.c
include/linux/nvme-fc-driver.h

index af075e9989449e97d7b32038d8e30a1cf0875ad6..f546eecb1f82b62e5a8df4f99e43fa3013dbfcb2 100644 (file)
@@ -452,6 +452,36 @@ nvme_fc_unregister_localport(struct nvme_fc_local_port *portptr)
 }
 EXPORT_SYMBOL_GPL(nvme_fc_unregister_localport);
 
+/*
+ * TRADDR strings, per FC-NVME are fixed format:
+ *   "nn-0x<16hexdigits>:pn-0x<16hexdigits>" - 43 characters
+ * udev event will only differ by prefix of what field is
+ * being specified:
+ *    "NVMEFC_HOST_TRADDR=" or "NVMEFC_TRADDR=" - 19 max characters
+ *  19 + 43 + null_fudge = 64 characters
+ */
+#define FCNVME_TRADDR_LENGTH           64
+
+static void
+nvme_fc_signal_discovery_scan(struct nvme_fc_lport *lport,
+               struct nvme_fc_rport *rport)
+{
+       char hostaddr[FCNVME_TRADDR_LENGTH];    /* NVMEFC_HOST_TRADDR=...*/
+       char tgtaddr[FCNVME_TRADDR_LENGTH];     /* NVMEFC_TRADDR=...*/
+       char *envp[4] = { "FC_EVENT=nvmediscovery", hostaddr, tgtaddr, NULL };
+
+       if (!(rport->remoteport.port_role & FC_PORT_ROLE_NVME_DISCOVERY))
+               return;
+
+       snprintf(hostaddr, sizeof(hostaddr),
+               "NVMEFC_HOST_TRADDR=nn-0x%016llx:pn-0x%016llx",
+               lport->localport.node_name, lport->localport.port_name);
+       snprintf(tgtaddr, sizeof(tgtaddr),
+               "NVMEFC_TRADDR=nn-0x%016llx:pn-0x%016llx",
+               rport->remoteport.node_name, rport->remoteport.port_name);
+       kobject_uevent_env(&fc_udev_device->kobj, KOBJ_CHANGE, envp);
+}
+
 /**
  * nvme_fc_register_remoteport - transport entry point called by an
  *                              LLDD to register the existence of a NVME
@@ -516,6 +546,8 @@ nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
        list_add_tail(&newrec->endp_list, &lport->endp_list);
        spin_unlock_irqrestore(&nvme_fc_lock, flags);
 
+       nvme_fc_signal_discovery_scan(lport, newrec);
+
        *portptr = &newrec->remoteport;
        return 0;
 
@@ -634,6 +666,23 @@ nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *portptr)
 }
 EXPORT_SYMBOL_GPL(nvme_fc_unregister_remoteport);
 
+/**
+ * nvme_fc_rescan_remoteport - transport entry point called by an
+ *                              LLDD to request a nvme device rescan.
+ * @remoteport: pointer to the (registered) remote port that is to be
+ *              rescanned.
+ *
+ * Returns: N/A
+ */
+void
+nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport)
+{
+       struct nvme_fc_rport *rport = remoteport_to_rport(remoteport);
+
+       nvme_fc_signal_discovery_scan(rport->lport, rport);
+}
+EXPORT_SYMBOL_GPL(nvme_fc_rescan_remoteport);
+
 
 /* *********************** FC-NVME DMA Handling **************************** */
 
index a726f96010d59d9d99eda4e6d5d5ea9776ebdd18..4ea03b9a5c8cb9e2090fb73eba02e325bf8b02e8 100644 (file)
@@ -446,6 +446,8 @@ int nvme_fc_register_remoteport(struct nvme_fc_local_port *localport,
 
 int nvme_fc_unregister_remoteport(struct nvme_fc_remote_port *remoteport);
 
+void nvme_fc_rescan_remoteport(struct nvme_fc_remote_port *remoteport);
+
 
 
 /*