Merge branch 'for-4.8/block' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm...
[cascardo/linux.git] / drivers / nvme / host / core.c
index 959173b..500b790 100644 (file)
@@ -30,6 +30,7 @@
 #include <asm/unaligned.h>
 
 #include "nvme.h"
+#include "fabrics.h"
 
 #define NVME_MINORS            (1U << MINORBITS)
 
@@ -85,7 +86,9 @@ bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
        switch (new_state) {
        case NVME_CTRL_LIVE:
                switch (old_state) {
+               case NVME_CTRL_NEW:
                case NVME_CTRL_RESETTING:
+               case NVME_CTRL_RECONNECTING:
                        changed = true;
                        /* FALLTHRU */
                default:
@@ -95,6 +98,16 @@ bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
        case NVME_CTRL_RESETTING:
                switch (old_state) {
                case NVME_CTRL_NEW:
+               case NVME_CTRL_LIVE:
+               case NVME_CTRL_RECONNECTING:
+                       changed = true;
+                       /* FALLTHRU */
+               default:
+                       break;
+               }
+               break;
+       case NVME_CTRL_RECONNECTING:
+               switch (old_state) {
                case NVME_CTRL_LIVE:
                        changed = true;
                        /* FALLTHRU */
@@ -106,6 +119,7 @@ bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
                switch (old_state) {
                case NVME_CTRL_LIVE:
                case NVME_CTRL_RESETTING:
+               case NVME_CTRL_RECONNECTING:
                        changed = true;
                        /* FALLTHRU */
                default:
@@ -191,11 +205,16 @@ void nvme_requeue_req(struct request *req)
 EXPORT_SYMBOL_GPL(nvme_requeue_req);
 
 struct request *nvme_alloc_request(struct request_queue *q,
-               struct nvme_command *cmd, unsigned int flags)
+               struct nvme_command *cmd, unsigned int flags, int qid)
 {
        struct request *req;
 
-       req = blk_mq_alloc_request(q, nvme_is_write(cmd), flags);
+       if (qid == NVME_QID_ANY) {
+               req = blk_mq_alloc_request(q, nvme_is_write(cmd), flags);
+       } else {
+               req = blk_mq_alloc_request_hctx(q, nvme_is_write(cmd), flags,
+                               qid ? qid - 1 : 0);
+       }
        if (IS_ERR(req))
                return req;
 
@@ -323,12 +342,12 @@ EXPORT_SYMBOL_GPL(nvme_setup_cmd);
  */
 int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
                struct nvme_completion *cqe, void *buffer, unsigned bufflen,
-               unsigned timeout)
+               unsigned timeout, int qid, int at_head, int flags)
 {
        struct request *req;
        int ret;
 
-       req = nvme_alloc_request(q, cmd, 0);
+       req = nvme_alloc_request(q, cmd, flags, qid);
        if (IS_ERR(req))
                return PTR_ERR(req);
 
@@ -341,17 +360,19 @@ int __nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
                        goto out;
        }
 
-       blk_execute_rq(req->q, NULL, req, 0);
+       blk_execute_rq(req->q, NULL, req, at_head);
        ret = req->errors;
  out:
        blk_mq_free_request(req);
        return ret;
 }
+EXPORT_SYMBOL_GPL(__nvme_submit_sync_cmd);
 
 int nvme_submit_sync_cmd(struct request_queue *q, struct nvme_command *cmd,
                void *buffer, unsigned bufflen)
 {
-       return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0);
+       return __nvme_submit_sync_cmd(q, cmd, NULL, buffer, bufflen, 0,
+                       NVME_QID_ANY, 0, 0);
 }
 EXPORT_SYMBOL_GPL(nvme_submit_sync_cmd);
 
@@ -369,7 +390,7 @@ int __nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
        void *meta = NULL;
        int ret;
 
-       req = nvme_alloc_request(q, cmd, 0);
+       req = nvme_alloc_request(q, cmd, 0, NVME_QID_ANY);
        if (IS_ERR(req))
                return PTR_ERR(req);
 
@@ -455,6 +476,74 @@ int nvme_submit_user_cmd(struct request_queue *q, struct nvme_command *cmd,
                        result, timeout);
 }
 
+static void nvme_keep_alive_end_io(struct request *rq, int error)
+{
+       struct nvme_ctrl *ctrl = rq->end_io_data;
+
+       blk_mq_free_request(rq);
+
+       if (error) {
+               dev_err(ctrl->device,
+                       "failed nvme_keep_alive_end_io error=%d\n", error);
+               return;
+       }
+
+       schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
+}
+
+static int nvme_keep_alive(struct nvme_ctrl *ctrl)
+{
+       struct nvme_command c;
+       struct request *rq;
+
+       memset(&c, 0, sizeof(c));
+       c.common.opcode = nvme_admin_keep_alive;
+
+       rq = nvme_alloc_request(ctrl->admin_q, &c, BLK_MQ_REQ_RESERVED,
+                       NVME_QID_ANY);
+       if (IS_ERR(rq))
+               return PTR_ERR(rq);
+
+       rq->timeout = ctrl->kato * HZ;
+       rq->end_io_data = ctrl;
+
+       blk_execute_rq_nowait(rq->q, NULL, rq, 0, nvme_keep_alive_end_io);
+
+       return 0;
+}
+
+static void nvme_keep_alive_work(struct work_struct *work)
+{
+       struct nvme_ctrl *ctrl = container_of(to_delayed_work(work),
+                       struct nvme_ctrl, ka_work);
+
+       if (nvme_keep_alive(ctrl)) {
+               /* allocation failure, reset the controller */
+               dev_err(ctrl->device, "keep-alive failed\n");
+               ctrl->ops->reset_ctrl(ctrl);
+               return;
+       }
+}
+
+void nvme_start_keep_alive(struct nvme_ctrl *ctrl)
+{
+       if (unlikely(ctrl->kato == 0))
+               return;
+
+       INIT_DELAYED_WORK(&ctrl->ka_work, nvme_keep_alive_work);
+       schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
+}
+EXPORT_SYMBOL_GPL(nvme_start_keep_alive);
+
+void nvme_stop_keep_alive(struct nvme_ctrl *ctrl)
+{
+       if (unlikely(ctrl->kato == 0))
+               return;
+
+       cancel_delayed_work_sync(&ctrl->ka_work);
+}
+EXPORT_SYMBOL_GPL(nvme_stop_keep_alive);
+
 int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
 {
        struct nvme_command c = { };
@@ -516,10 +605,11 @@ int nvme_get_features(struct nvme_ctrl *dev, unsigned fid, unsigned nsid,
        memset(&c, 0, sizeof(c));
        c.features.opcode = nvme_admin_get_features;
        c.features.nsid = cpu_to_le32(nsid);
-       c.features.prp1 = cpu_to_le64(dma_addr);
+       c.features.dptr.prp1 = cpu_to_le64(dma_addr);
        c.features.fid = cpu_to_le32(fid);
 
-       ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0);
+       ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0,
+                       NVME_QID_ANY, 0, 0);
        if (ret >= 0)
                *result = le32_to_cpu(cqe.result);
        return ret;
@@ -534,11 +624,12 @@ int nvme_set_features(struct nvme_ctrl *dev, unsigned fid, unsigned dword11,
 
        memset(&c, 0, sizeof(c));
        c.features.opcode = nvme_admin_set_features;
-       c.features.prp1 = cpu_to_le64(dma_addr);
+       c.features.dptr.prp1 = cpu_to_le64(dma_addr);
        c.features.fid = cpu_to_le32(fid);
        c.features.dword11 = cpu_to_le32(dword11);
 
-       ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0);
+       ret = __nvme_submit_sync_cmd(dev->admin_q, &c, &cqe, NULL, 0, 0,
+                       NVME_QID_ANY, 0, 0);
        if (ret >= 0)
                *result = le32_to_cpu(cqe.result);
        return ret;
@@ -1168,9 +1259,33 @@ int nvme_init_identify(struct nvme_ctrl *ctrl)
        }
 
        nvme_set_queue_limits(ctrl, ctrl->admin_q);
+       ctrl->sgls = le32_to_cpu(id->sgls);
+       ctrl->kas = le16_to_cpu(id->kas);
+
+       if (ctrl->ops->is_fabrics) {
+               ctrl->icdoff = le16_to_cpu(id->icdoff);
+               ctrl->ioccsz = le32_to_cpu(id->ioccsz);
+               ctrl->iorcsz = le32_to_cpu(id->iorcsz);
+               ctrl->maxcmd = le16_to_cpu(id->maxcmd);
+
+               /*
+                * In fabrics we need to verify the cntlid matches the
+                * admin connect
+                */
+               if (ctrl->cntlid != le16_to_cpu(id->cntlid))
+                       ret = -EINVAL;
+
+               if (!ctrl->opts->discovery_nqn && !ctrl->kas) {
+                       dev_err(ctrl->dev,
+                               "keep-alive support is mandatory for fabrics\n");
+                       ret = -EINVAL;
+               }
+       } else {
+               ctrl->cntlid = le16_to_cpu(id->cntlid);
+       }
 
        kfree(id);
-       return 0;
+       return ret;
 }
 EXPORT_SYMBOL_GPL(nvme_init_identify);
 
@@ -1352,7 +1467,7 @@ static struct attribute *nvme_ns_attrs[] = {
        NULL,
 };
 
-static umode_t nvme_attrs_are_visible(struct kobject *kobj,
+static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
                struct attribute *a, int n)
 {
        struct device *dev = container_of(kobj, struct device, kobj);
@@ -1371,7 +1486,7 @@ static umode_t nvme_attrs_are_visible(struct kobject *kobj,
 
 static const struct attribute_group nvme_ns_attr_group = {
        .attrs          = nvme_ns_attrs,
-       .is_visible     = nvme_attrs_are_visible,
+       .is_visible     = nvme_ns_attrs_are_visible,
 };
 
 #define nvme_show_str_function(field)                                          \
@@ -1397,6 +1512,49 @@ nvme_show_str_function(serial);
 nvme_show_str_function(firmware_rev);
 nvme_show_int_function(cntlid);
 
+static ssize_t nvme_sysfs_delete(struct device *dev,
+                               struct device_attribute *attr, const char *buf,
+                               size_t count)
+{
+       struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+       if (device_remove_file_self(dev, attr))
+               ctrl->ops->delete_ctrl(ctrl);
+       return count;
+}
+static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
+
+static ssize_t nvme_sysfs_show_transport(struct device *dev,
+                                        struct device_attribute *attr,
+                                        char *buf)
+{
+       struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+       return snprintf(buf, PAGE_SIZE, "%s\n", ctrl->ops->name);
+}
+static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
+
+static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
+                                        struct device_attribute *attr,
+                                        char *buf)
+{
+       struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+       return snprintf(buf, PAGE_SIZE, "%s\n",
+                       ctrl->ops->get_subsysnqn(ctrl));
+}
+static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
+
+static ssize_t nvme_sysfs_show_address(struct device *dev,
+                                        struct device_attribute *attr,
+                                        char *buf)
+{
+       struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+       return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
+}
+static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
+
 static struct attribute *nvme_dev_attrs[] = {
        &dev_attr_reset_controller.attr,
        &dev_attr_rescan_controller.attr,
@@ -1404,11 +1562,38 @@ static struct attribute *nvme_dev_attrs[] = {
        &dev_attr_serial.attr,
        &dev_attr_firmware_rev.attr,
        &dev_attr_cntlid.attr,
+       &dev_attr_delete_controller.attr,
+       &dev_attr_transport.attr,
+       &dev_attr_subsysnqn.attr,
+       &dev_attr_address.attr,
        NULL
 };
 
+#define CHECK_ATTR(ctrl, a, name)              \
+       if ((a) == &dev_attr_##name.attr &&     \
+           !(ctrl)->ops->get_##name)           \
+               return 0
+
+static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
+               struct attribute *a, int n)
+{
+       struct device *dev = container_of(kobj, struct device, kobj);
+       struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+       if (a == &dev_attr_delete_controller.attr) {
+               if (!ctrl->ops->delete_ctrl)
+                       return 0;
+       }
+
+       CHECK_ATTR(ctrl, a, subsysnqn);
+       CHECK_ATTR(ctrl, a, address);
+
+       return a->mode;
+}
+
 static struct attribute_group nvme_dev_attrs_group = {
-       .attrs = nvme_dev_attrs,
+       .attrs          = nvme_dev_attrs,
+       .is_visible     = nvme_dev_attrs_are_visible,
 };
 
 static const struct attribute_group *nvme_dev_attr_groups[] = {