| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Common code for the NVMe target. |
| 4 |
* Copyright (c) 2015-2016 HGST, a Western Digital Company. |
| 5 |
*/ |
| 6 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 |
#include <linux/hex.h> |
| 8 |
#include <linux/module.h> |
| 9 |
#include <linux/random.h> |
| 10 |
#include <linux/rculist.h> |
| 11 |
#include <linux/pci-p2pdma.h> |
| 12 |
#include <linux/scatterlist.h> |
| 13 |
|
| 14 |
#include <generated/utsrelease.h> |
| 15 |
|
| 16 |
#define CREATE_TRACE_POINTS |
| 17 |
#include "trace.h" |
| 18 |
|
| 19 |
#include "nvmet.h" |
| 20 |
#include "debugfs.h" |
| 21 |
|
| 22 |
struct kmem_cache *nvmet_bvec_cache; |
| 23 |
struct workqueue_struct *buffered_io_wq; |
| 24 |
struct workqueue_struct *zbd_wq; |
| 25 |
static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX]; |
| 26 |
static DEFINE_IDA(cntlid_ida); |
| 27 |
|
| 28 |
struct workqueue_struct *nvmet_wq; |
| 29 |
EXPORT_SYMBOL_GPL(nvmet_wq); |
| 30 |
struct workqueue_struct *nvmet_aen_wq; |
| 31 |
EXPORT_SYMBOL_GPL(nvmet_aen_wq); |
| 32 |
|
| 33 |
/* |
| 34 |
* This read/write semaphore is used to synchronize access to configuration |
| 35 |
* information on a target system that will result in discovery log page |
| 36 |
* information change for at least one host. |
| 37 |
* The full list of resources to protected by this semaphore is: |
| 38 |
* |
| 39 |
* - subsystems list |
| 40 |
* - per-subsystem allowed hosts list |
| 41 |
* - allow_any_host subsystem attribute |
| 42 |
* - nvmet_genctr |
| 43 |
* - the nvmet_transports array |
| 44 |
* |
| 45 |
* When updating any of those lists/structures write lock should be obtained, |
| 46 |
* while when reading (populating discovery log page or checking host-subsystem |
| 47 |
* link) read lock is obtained to allow concurrent reads. |
| 48 |
*/ |
| 49 |
DECLARE_RWSEM(nvmet_config_sem); |
| 50 |
|
| 51 |
u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1]; |
| 52 |
u64 nvmet_ana_chgcnt; |
| 53 |
DECLARE_RWSEM(nvmet_ana_sem); |
| 54 |
|
| 55 |
inline u16 errno_to_nvme_status(struct nvmet_req *req, int errno) |
| 56 |
{ |
| 57 |
switch (errno) { |
| 58 |
case 0: |
| 59 |
return NVME_SC_SUCCESS; |
| 60 |
case -ENOSPC: |
| 61 |
req->error_loc = offsetof(struct nvme_rw_command, length); |
| 62 |
return NVME_SC_CAP_EXCEEDED | NVME_STATUS_DNR; |
| 63 |
case -EREMOTEIO: |
| 64 |
req->error_loc = offsetof(struct nvme_rw_command, slba); |
| 65 |
return NVME_SC_LBA_RANGE | NVME_STATUS_DNR; |
| 66 |
case -EOPNOTSUPP: |
| 67 |
req->error_loc = offsetof(struct nvme_common_command, opcode); |
| 68 |
return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR; |
| 69 |
case -ENODATA: |
| 70 |
req->error_loc = offsetof(struct nvme_rw_command, nsid); |
| 71 |
return NVME_SC_ACCESS_DENIED; |
| 72 |
case -EIO: |
| 73 |
fallthrough; |
| 74 |
default: |
| 75 |
req->error_loc = offsetof(struct nvme_common_command, opcode); |
| 76 |
return NVME_SC_INTERNAL | NVME_STATUS_DNR; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
u16 nvmet_report_invalid_opcode(struct nvmet_req *req) |
| 81 |
{ |
| 82 |
pr_debug("unhandled cmd %d on qid %d\n", req->cmd->common.opcode, |
| 83 |
req->sq->qid); |
| 84 |
|
| 85 |
req->error_loc = offsetof(struct nvme_common_command, opcode); |
| 86 |
return NVME_SC_INVALID_OPCODE | NVME_STATUS_DNR; |
| 87 |
} |
| 88 |
|
| 89 |
static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port, |
| 90 |
const char *subsysnqn); |
| 91 |
|
| 92 |
u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf, |
| 93 |
size_t len) |
| 94 |
{ |
| 95 |
if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len) { |
| 96 |
req->error_loc = offsetof(struct nvme_common_command, dptr); |
| 97 |
return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR; |
| 98 |
} |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len) |
| 103 |
{ |
| 104 |
if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len) { |
| 105 |
req->error_loc = offsetof(struct nvme_common_command, dptr); |
| 106 |
return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR; |
| 107 |
} |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len) |
| 112 |
{ |
| 113 |
if (sg_zero_buffer(req->sg, req->sg_cnt, len, off) != len) { |
| 114 |
req->error_loc = offsetof(struct nvme_common_command, dptr); |
| 115 |
return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR; |
| 116 |
} |
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
static u32 nvmet_max_nsid(struct nvmet_subsys *subsys) |
| 121 |
{ |
| 122 |
struct nvmet_ns *cur; |
| 123 |
unsigned long idx; |
| 124 |
u32 nsid = 0; |
| 125 |
|
| 126 |
nvmet_for_each_enabled_ns(&subsys->namespaces, idx, cur) |
| 127 |
nsid = cur->nsid; |
| 128 |
|
| 129 |
return nsid; |
| 130 |
} |
| 131 |
|
| 132 |
static u32 nvmet_async_event_result(struct nvmet_async_event *aen) |
| 133 |
{ |
| 134 |
return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16); |
| 135 |
} |
| 136 |
|
| 137 |
static void nvmet_async_events_failall(struct nvmet_ctrl *ctrl) |
| 138 |
{ |
| 139 |
struct nvmet_req *req; |
| 140 |
|
| 141 |
mutex_lock(&ctrl->lock); |
| 142 |
while (ctrl->nr_async_event_cmds) { |
| 143 |
req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds]; |
| 144 |
mutex_unlock(&ctrl->lock); |
| 145 |
nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_STATUS_DNR); |
| 146 |
mutex_lock(&ctrl->lock); |
| 147 |
} |
| 148 |
mutex_unlock(&ctrl->lock); |
| 149 |
} |
| 150 |
|
| 151 |
static void nvmet_async_events_process(struct nvmet_ctrl *ctrl) |
| 152 |
{ |
| 153 |
struct nvmet_async_event *aen; |
| 154 |
struct nvmet_req *req; |
| 155 |
|
| 156 |
mutex_lock(&ctrl->lock); |
| 157 |
while (ctrl->nr_async_event_cmds && !list_empty(&ctrl->async_events)) { |
| 158 |
aen = list_first_entry(&ctrl->async_events, |
| 159 |
struct nvmet_async_event, entry); |
| 160 |
req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds]; |
| 161 |
nvmet_set_result(req, nvmet_async_event_result(aen)); |
| 162 |
|
| 163 |
list_del(&aen->entry); |
| 164 |
kfree(aen); |
| 165 |
|
| 166 |
mutex_unlock(&ctrl->lock); |
| 167 |
trace_nvmet_async_event(ctrl, req->cqe->result.u32); |
| 168 |
nvmet_req_complete(req, 0); |
| 169 |
mutex_lock(&ctrl->lock); |
| 170 |
} |
| 171 |
mutex_unlock(&ctrl->lock); |
| 172 |
} |
| 173 |
|
| 174 |
static void nvmet_async_events_free(struct nvmet_ctrl *ctrl) |
| 175 |
{ |
| 176 |
struct nvmet_async_event *aen, *tmp; |
| 177 |
|
| 178 |
mutex_lock(&ctrl->lock); |
| 179 |
list_for_each_entry_safe(aen, tmp, &ctrl->async_events, entry) { |
| 180 |
list_del(&aen->entry); |
| 181 |
kfree(aen); |
| 182 |
} |
| 183 |
mutex_unlock(&ctrl->lock); |
| 184 |
} |
| 185 |
|
| 186 |
static void nvmet_async_event_work(struct work_struct *work) |
| 187 |
{ |
| 188 |
struct nvmet_ctrl *ctrl = |
| 189 |
container_of(work, struct nvmet_ctrl, async_event_work); |
| 190 |
|
| 191 |
nvmet_async_events_process(ctrl); |
| 192 |
} |
| 193 |
|
| 194 |
void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type, |
| 195 |
u8 event_info, u8 log_page) |
| 196 |
{ |
| 197 |
struct nvmet_async_event *aen; |
| 198 |
|
| 199 |
aen = kmalloc_obj(*aen); |
| 200 |
if (!aen) |
| 201 |
return; |
| 202 |
|
| 203 |
aen->event_type = event_type; |
| 204 |
aen->event_info = event_info; |
| 205 |
aen->log_page = log_page; |
| 206 |
|
| 207 |
mutex_lock(&ctrl->lock); |
| 208 |
list_add_tail(&aen->entry, &ctrl->async_events); |
| 209 |
mutex_unlock(&ctrl->lock); |
| 210 |
|
| 211 |
queue_work(nvmet_aen_wq, &ctrl->async_event_work); |
| 212 |
} |
| 213 |
|
| 214 |
static void nvmet_add_to_changed_ns_log(struct nvmet_ctrl *ctrl, __le32 nsid) |
| 215 |
{ |
| 216 |
u32 i; |
| 217 |
|
| 218 |
mutex_lock(&ctrl->lock); |
| 219 |
if (ctrl->nr_changed_ns > NVME_MAX_CHANGED_NAMESPACES) |
| 220 |
goto out_unlock; |
| 221 |
|
| 222 |
for (i = 0; i < ctrl->nr_changed_ns; i++) { |
| 223 |
if (ctrl->changed_ns_list[i] == nsid) |
| 224 |
goto out_unlock; |
| 225 |
} |
| 226 |
|
| 227 |
if (ctrl->nr_changed_ns == NVME_MAX_CHANGED_NAMESPACES) { |
| 228 |
ctrl->changed_ns_list[0] = cpu_to_le32(0xffffffff); |
| 229 |
ctrl->nr_changed_ns = U32_MAX; |
| 230 |
goto out_unlock; |
| 231 |
} |
| 232 |
|
| 233 |
ctrl->changed_ns_list[ctrl->nr_changed_ns++] = nsid; |
| 234 |
out_unlock: |
| 235 |
mutex_unlock(&ctrl->lock); |
| 236 |
} |
| 237 |
|
| 238 |
void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid) |
| 239 |
{ |
| 240 |
struct nvmet_ctrl *ctrl; |
| 241 |
|
| 242 |
lockdep_assert_held(&subsys->lock); |
| 243 |
|
| 244 |
list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { |
| 245 |
nvmet_add_to_changed_ns_log(ctrl, cpu_to_le32(nsid)); |
| 246 |
if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_NS_ATTR)) |
| 247 |
continue; |
| 248 |
nvmet_add_async_event(ctrl, NVME_AER_NOTICE, |
| 249 |
NVME_AER_NOTICE_NS_CHANGED, |
| 250 |
NVME_LOG_CHANGED_NS); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
void nvmet_send_ana_event(struct nvmet_subsys *subsys, |
| 255 |
struct nvmet_port *port) |
| 256 |
{ |
| 257 |
struct nvmet_ctrl *ctrl; |
| 258 |
|
| 259 |
mutex_lock(&subsys->lock); |
| 260 |
list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { |
| 261 |
if (port && ctrl->port != port) |
| 262 |
continue; |
| 263 |
if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_ANA_CHANGE)) |
| 264 |
continue; |
| 265 |
nvmet_add_async_event(ctrl, NVME_AER_NOTICE, |
| 266 |
NVME_AER_NOTICE_ANA, NVME_LOG_ANA); |
| 267 |
} |
| 268 |
mutex_unlock(&subsys->lock); |
| 269 |
} |
| 270 |
|
| 271 |
void nvmet_port_send_ana_event(struct nvmet_port *port) |
| 272 |
{ |
| 273 |
struct nvmet_subsys_link *p; |
| 274 |
|
| 275 |
down_read(&nvmet_config_sem); |
| 276 |
list_for_each_entry(p, &port->subsystems, entry) |
| 277 |
nvmet_send_ana_event(p->subsys, port); |
| 278 |
up_read(&nvmet_config_sem); |
| 279 |
} |
| 280 |
|
| 281 |
int nvmet_register_transport(const struct nvmet_fabrics_ops *ops) |
| 282 |
{ |
| 283 |
int ret = 0; |
| 284 |
|
| 285 |
down_write(&nvmet_config_sem); |
| 286 |
if (nvmet_transports[ops->type]) |
| 287 |
ret = -EINVAL; |
| 288 |
else |
| 289 |
nvmet_transports[ops->type] = ops; |
| 290 |
up_write(&nvmet_config_sem); |
| 291 |
|
| 292 |
return ret; |
| 293 |
} |
| 294 |
EXPORT_SYMBOL_GPL(nvmet_register_transport); |
| 295 |
|
| 296 |
void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops) |
| 297 |
{ |
| 298 |
down_write(&nvmet_config_sem); |
| 299 |
nvmet_transports[ops->type] = NULL; |
| 300 |
up_write(&nvmet_config_sem); |
| 301 |
} |
| 302 |
EXPORT_SYMBOL_GPL(nvmet_unregister_transport); |
| 303 |
|
| 304 |
void nvmet_port_del_ctrls(struct nvmet_port *port, struct nvmet_subsys *subsys) |
| 305 |
{ |
| 306 |
struct nvmet_ctrl *ctrl; |
| 307 |
|
| 308 |
mutex_lock(&subsys->lock); |
| 309 |
list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { |
| 310 |
if (ctrl->port == port) |
| 311 |
ctrl->ops->delete_ctrl(ctrl); |
| 312 |
} |
| 313 |
mutex_unlock(&subsys->lock); |
| 314 |
} |
| 315 |
|
| 316 |
int nvmet_enable_port(struct nvmet_port *port) |
| 317 |
{ |
| 318 |
const struct nvmet_fabrics_ops *ops; |
| 319 |
int ret; |
| 320 |
|
| 321 |
lockdep_assert_held(&nvmet_config_sem); |
| 322 |
|
| 323 |
if (port->disc_addr.trtype == NVMF_TRTYPE_MAX) |
| 324 |
return -EINVAL; |
| 325 |
|
| 326 |
ops = nvmet_transports[port->disc_addr.trtype]; |
| 327 |
if (!ops) { |
| 328 |
up_write(&nvmet_config_sem); |
| 329 |
request_module("nvmet-transport-%d", port->disc_addr.trtype); |
| 330 |
down_write(&nvmet_config_sem); |
| 331 |
ops = nvmet_transports[port->disc_addr.trtype]; |
| 332 |
if (!ops) { |
| 333 |
pr_err("transport type %d not supported\n", |
| 334 |
port->disc_addr.trtype); |
| 335 |
return -EINVAL; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
if (!try_module_get(ops->owner)) |
| 340 |
return -EINVAL; |
| 341 |
|
| 342 |
/* |
| 343 |
* If the user requested PI support and the transport isn't pi capable, |
| 344 |
* don't enable the port. |
| 345 |
*/ |
| 346 |
if (port->pi_enable && !(ops->flags & NVMF_METADATA_SUPPORTED)) { |
| 347 |
pr_err("T10-PI is not supported by transport type %d\n", |
| 348 |
port->disc_addr.trtype); |
| 349 |
ret = -EINVAL; |
| 350 |
goto out_put; |
| 351 |
} |
| 352 |
|
| 353 |
ret = ops->add_port(port); |
| 354 |
if (ret) |
| 355 |
goto out_put; |
| 356 |
|
| 357 |
/* If the transport didn't set inline_data_size, then disable it. */ |
| 358 |
if (port->inline_data_size < 0) |
| 359 |
port->inline_data_size = 0; |
| 360 |
|
| 361 |
/* |
| 362 |
* If the transport didn't set the max_queue_size properly, then clamp |
| 363 |
* it to the target limits. Also set default values in case the |
| 364 |
* transport didn't set it at all. |
| 365 |
*/ |
| 366 |
if (port->max_queue_size < 0) |
| 367 |
port->max_queue_size = NVMET_MAX_QUEUE_SIZE; |
| 368 |
else |
| 369 |
port->max_queue_size = clamp_t(int, port->max_queue_size, |
| 370 |
NVMET_MIN_QUEUE_SIZE, |
| 371 |
NVMET_MAX_QUEUE_SIZE); |
| 372 |
|
| 373 |
port->enabled = true; |
| 374 |
port->tr_ops = ops; |
| 375 |
return 0; |
| 376 |
|
| 377 |
out_put: |
| 378 |
module_put(ops->owner); |
| 379 |
return ret; |
| 380 |
} |
| 381 |
|
| 382 |
void nvmet_disable_port(struct nvmet_port *port) |
| 383 |
{ |
| 384 |
const struct nvmet_fabrics_ops *ops; |
| 385 |
|
| 386 |
lockdep_assert_held(&nvmet_config_sem); |
| 387 |
|
| 388 |
port->enabled = false; |
| 389 |
port->tr_ops = NULL; |
| 390 |
|
| 391 |
ops = nvmet_transports[port->disc_addr.trtype]; |
| 392 |
ops->remove_port(port); |
| 393 |
module_put(ops->owner); |
| 394 |
} |
| 395 |
|
| 396 |
static void nvmet_keep_alive_timer(struct work_struct *work) |
| 397 |
{ |
| 398 |
struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work), |
| 399 |
struct nvmet_ctrl, ka_work); |
| 400 |
bool reset_tbkas = ctrl->reset_tbkas; |
| 401 |
|
| 402 |
ctrl->reset_tbkas = false; |
| 403 |
if (reset_tbkas) { |
| 404 |
pr_debug("ctrl %d reschedule traffic based keep-alive timer\n", |
| 405 |
ctrl->cntlid); |
| 406 |
queue_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ); |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n", |
| 411 |
ctrl->cntlid, ctrl->kato); |
| 412 |
|
| 413 |
nvmet_ctrl_fatal_error(ctrl); |
| 414 |
} |
| 415 |
|
| 416 |
void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl) |
| 417 |
{ |
| 418 |
if (unlikely(ctrl->kato == 0)) |
| 419 |
return; |
| 420 |
|
| 421 |
pr_debug("ctrl %d start keep-alive timer for %d secs\n", |
| 422 |
ctrl->cntlid, ctrl->kato); |
| 423 |
|
| 424 |
queue_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ); |
| 425 |
} |
| 426 |
|
| 427 |
void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl) |
| 428 |
{ |
| 429 |
if (unlikely(ctrl->kato == 0)) |
| 430 |
return; |
| 431 |
|
| 432 |
pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid); |
| 433 |
|
| 434 |
cancel_delayed_work_sync(&ctrl->ka_work); |
| 435 |
} |
| 436 |
|
| 437 |
u16 nvmet_req_find_ns(struct nvmet_req *req) |
| 438 |
{ |
| 439 |
u32 nsid = le32_to_cpu(req->cmd->common.nsid); |
| 440 |
struct nvmet_subsys *subsys = nvmet_req_subsys(req); |
| 441 |
|
| 442 |
req->ns = xa_load(&subsys->namespaces, nsid); |
| 443 |
if (unlikely(!req->ns || !req->ns->enabled)) { |
| 444 |
req->error_loc = offsetof(struct nvme_common_command, nsid); |
| 445 |
if (!req->ns) /* ns doesn't exist! */ |
| 446 |
return NVME_SC_INVALID_NS | NVME_STATUS_DNR; |
| 447 |
|
| 448 |
/* ns exists but it's disabled */ |
| 449 |
req->ns = NULL; |
| 450 |
return NVME_SC_INTERNAL_PATH_ERROR; |
| 451 |
} |
| 452 |
|
| 453 |
percpu_ref_get(&req->ns->ref); |
| 454 |
return NVME_SC_SUCCESS; |
| 455 |
} |
| 456 |
|
| 457 |
static void nvmet_destroy_namespace(struct percpu_ref *ref) |
| 458 |
{ |
| 459 |
struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref); |
| 460 |
|
| 461 |
complete(&ns->disable_done); |
| 462 |
} |
| 463 |
|
| 464 |
void nvmet_put_namespace(struct nvmet_ns *ns) |
| 465 |
{ |
| 466 |
percpu_ref_put(&ns->ref); |
| 467 |
} |
| 468 |
|
| 469 |
static void nvmet_ns_dev_disable(struct nvmet_ns *ns) |
| 470 |
{ |
| 471 |
nvmet_bdev_ns_disable(ns); |
| 472 |
nvmet_file_ns_disable(ns); |
| 473 |
} |
| 474 |
|
| 475 |
static int nvmet_p2pmem_ns_enable(struct nvmet_ns *ns) |
| 476 |
{ |
| 477 |
int ret; |
| 478 |
struct pci_dev *p2p_dev; |
| 479 |
|
| 480 |
if (!ns->use_p2pmem) |
| 481 |
return 0; |
| 482 |
|
| 483 |
if (!ns->bdev) { |
| 484 |
pr_err("peer-to-peer DMA is not supported by non-block device namespaces\n"); |
| 485 |
return -EINVAL; |
| 486 |
} |
| 487 |
|
| 488 |
if (!blk_queue_pci_p2pdma(ns->bdev->bd_disk->queue)) { |
| 489 |
pr_err("peer-to-peer DMA is not supported by the driver of %s\n", |
| 490 |
ns->device_path); |
| 491 |
return -EINVAL; |
| 492 |
} |
| 493 |
|
| 494 |
if (ns->p2p_dev) { |
| 495 |
ret = pci_p2pdma_distance(ns->p2p_dev, nvmet_ns_dev(ns), true); |
| 496 |
if (ret < 0) |
| 497 |
return -EINVAL; |
| 498 |
} else { |
| 499 |
/* |
| 500 |
* Right now we just check that there is p2pmem available so |
| 501 |
* we can report an error to the user right away if there |
| 502 |
* is not. We'll find the actual device to use once we |
| 503 |
* setup the controller when the port's device is available. |
| 504 |
*/ |
| 505 |
|
| 506 |
p2p_dev = pci_p2pmem_find(nvmet_ns_dev(ns)); |
| 507 |
if (!p2p_dev) { |
| 508 |
pr_err("no peer-to-peer memory is available for %s\n", |
| 509 |
ns->device_path); |
| 510 |
return -EINVAL; |
| 511 |
} |
| 512 |
|
| 513 |
pci_dev_put(p2p_dev); |
| 514 |
} |
| 515 |
|
| 516 |
return 0; |
| 517 |
} |
| 518 |
|
| 519 |
static void nvmet_p2pmem_ns_add_p2p(struct nvmet_ctrl *ctrl, |
| 520 |
struct nvmet_ns *ns) |
| 521 |
{ |
| 522 |
struct device *clients[2]; |
| 523 |
struct pci_dev *p2p_dev; |
| 524 |
int ret; |
| 525 |
|
| 526 |
lockdep_assert_held(&ctrl->subsys->lock); |
| 527 |
|
| 528 |
if (!ctrl->p2p_client || !ns->use_p2pmem) |
| 529 |
return; |
| 530 |
|
| 531 |
if (ns->p2p_dev) { |
| 532 |
ret = pci_p2pdma_distance(ns->p2p_dev, ctrl->p2p_client, true); |
| 533 |
if (ret < 0) |
| 534 |
return; |
| 535 |
|
| 536 |
p2p_dev = pci_dev_get(ns->p2p_dev); |
| 537 |
} else { |
| 538 |
clients[0] = ctrl->p2p_client; |
| 539 |
clients[1] = nvmet_ns_dev(ns); |
| 540 |
|
| 541 |
p2p_dev = pci_p2pmem_find_many(clients, ARRAY_SIZE(clients)); |
| 542 |
if (!p2p_dev) { |
| 543 |
pr_err("no peer-to-peer memory is available that's supported by %s and %s\n", |
| 544 |
dev_name(ctrl->p2p_client), ns->device_path); |
| 545 |
return; |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
ret = radix_tree_insert(&ctrl->p2p_ns_map, ns->nsid, p2p_dev); |
| 550 |
if (ret < 0) |
| 551 |
pci_dev_put(p2p_dev); |
| 552 |
|
| 553 |
pr_info("using p2pmem on %s for nsid %d\n", pci_name(p2p_dev), |
| 554 |
ns->nsid); |
| 555 |
} |
| 556 |
|
| 557 |
bool nvmet_ns_revalidate(struct nvmet_ns *ns) |
| 558 |
{ |
| 559 |
loff_t oldsize = ns->size; |
| 560 |
|
| 561 |
if (ns->bdev) |
| 562 |
nvmet_bdev_ns_revalidate(ns); |
| 563 |
else |
| 564 |
nvmet_file_ns_revalidate(ns); |
| 565 |
|
| 566 |
return oldsize != ns->size; |
| 567 |
} |
| 568 |
|
| 569 |
int nvmet_ns_enable(struct nvmet_ns *ns) |
| 570 |
{ |
| 571 |
struct nvmet_subsys *subsys = ns->subsys; |
| 572 |
struct nvmet_ctrl *ctrl; |
| 573 |
int ret; |
| 574 |
|
| 575 |
mutex_lock(&subsys->lock); |
| 576 |
ret = 0; |
| 577 |
|
| 578 |
if (nvmet_is_passthru_subsys(subsys)) { |
| 579 |
pr_info("cannot enable both passthru and regular namespaces for a single subsystem"); |
| 580 |
goto out_unlock; |
| 581 |
} |
| 582 |
|
| 583 |
if (ns->enabled) |
| 584 |
goto out_unlock; |
| 585 |
|
| 586 |
ret = nvmet_bdev_ns_enable(ns); |
| 587 |
if (ret == -ENOTBLK) |
| 588 |
ret = nvmet_file_ns_enable(ns); |
| 589 |
if (ret) |
| 590 |
goto out_unlock; |
| 591 |
|
| 592 |
ret = nvmet_p2pmem_ns_enable(ns); |
| 593 |
if (ret) |
| 594 |
goto out_dev_disable; |
| 595 |
|
| 596 |
list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) |
| 597 |
nvmet_p2pmem_ns_add_p2p(ctrl, ns); |
| 598 |
|
| 599 |
if (ns->pr.enable) { |
| 600 |
ret = nvmet_pr_init_ns(ns); |
| 601 |
if (ret) |
| 602 |
goto out_dev_put; |
| 603 |
} |
| 604 |
|
| 605 |
if (percpu_ref_init(&ns->ref, nvmet_destroy_namespace, 0, GFP_KERNEL)) |
| 606 |
goto out_pr_exit; |
| 607 |
|
| 608 |
nvmet_ns_changed(subsys, ns->nsid); |
| 609 |
ns->enabled = true; |
| 610 |
xa_set_mark(&subsys->namespaces, ns->nsid, NVMET_NS_ENABLED); |
| 611 |
ret = 0; |
| 612 |
out_unlock: |
| 613 |
mutex_unlock(&subsys->lock); |
| 614 |
return ret; |
| 615 |
out_pr_exit: |
| 616 |
if (ns->pr.enable) |
| 617 |
nvmet_pr_exit_ns(ns); |
| 618 |
out_dev_put: |
| 619 |
list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) |
| 620 |
pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid)); |
| 621 |
out_dev_disable: |
| 622 |
nvmet_ns_dev_disable(ns); |
| 623 |
goto out_unlock; |
| 624 |
} |
| 625 |
|
| 626 |
void nvmet_ns_disable(struct nvmet_ns *ns) |
| 627 |
{ |
| 628 |
struct nvmet_subsys *subsys = ns->subsys; |
| 629 |
struct nvmet_ctrl *ctrl; |
| 630 |
|
| 631 |
mutex_lock(&subsys->lock); |
| 632 |
if (!ns->enabled) |
| 633 |
goto out_unlock; |
| 634 |
|
| 635 |
ns->enabled = false; |
| 636 |
xa_clear_mark(&subsys->namespaces, ns->nsid, NVMET_NS_ENABLED); |
| 637 |
|
| 638 |
list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) |
| 639 |
pci_dev_put(radix_tree_delete(&ctrl->p2p_ns_map, ns->nsid)); |
| 640 |
|
| 641 |
mutex_unlock(&subsys->lock); |
| 642 |
|
| 643 |
/* |
| 644 |
* Now that we removed the namespaces from the lookup list, we |
| 645 |
* can kill the per_cpu ref and wait for any remaining references |
| 646 |
* to be dropped, as well as a RCU grace period for anyone only |
| 647 |
* using the namespace under rcu_read_lock(). Note that we can't |
| 648 |
* use call_rcu here as we need to ensure the namespaces have |
| 649 |
* been fully destroyed before unloading the module. |
| 650 |
*/ |
| 651 |
percpu_ref_kill(&ns->ref); |
| 652 |
synchronize_rcu(); |
| 653 |
wait_for_completion(&ns->disable_done); |
| 654 |
percpu_ref_exit(&ns->ref); |
| 655 |
|
| 656 |
if (ns->pr.enable) |
| 657 |
nvmet_pr_exit_ns(ns); |
| 658 |
|
| 659 |
mutex_lock(&subsys->lock); |
| 660 |
nvmet_ns_changed(subsys, ns->nsid); |
| 661 |
nvmet_ns_dev_disable(ns); |
| 662 |
out_unlock: |
| 663 |
mutex_unlock(&subsys->lock); |
| 664 |
} |
| 665 |
|
| 666 |
void nvmet_ns_free(struct nvmet_ns *ns) |
| 667 |
{ |
| 668 |
struct nvmet_subsys *subsys = ns->subsys; |
| 669 |
|
| 670 |
nvmet_ns_disable(ns); |
| 671 |
|
| 672 |
mutex_lock(&subsys->lock); |
| 673 |
|
| 674 |
xa_erase(&subsys->namespaces, ns->nsid); |
| 675 |
if (ns->nsid == subsys->max_nsid) |
| 676 |
subsys->max_nsid = nvmet_max_nsid(subsys); |
| 677 |
|
| 678 |
subsys->nr_namespaces--; |
| 679 |
mutex_unlock(&subsys->lock); |
| 680 |
|
| 681 |
down_write(&nvmet_ana_sem); |
| 682 |
nvmet_ana_group_enabled[ns->anagrpid]--; |
| 683 |
up_write(&nvmet_ana_sem); |
| 684 |
|
| 685 |
kfree(ns->device_path); |
| 686 |
kfree(ns); |
| 687 |
} |
| 688 |
|
| 689 |
struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid) |
| 690 |
{ |
| 691 |
struct nvmet_ns *ns; |
| 692 |
|
| 693 |
mutex_lock(&subsys->lock); |
| 694 |
|
| 695 |
if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES) |
| 696 |
goto out_unlock; |
| 697 |
|
| 698 |
ns = kzalloc_obj(*ns); |
| 699 |
if (!ns) |
| 700 |
goto out_unlock; |
| 701 |
|
| 702 |
init_completion(&ns->disable_done); |
| 703 |
|
| 704 |
ns->nsid = nsid; |
| 705 |
ns->subsys = subsys; |
| 706 |
|
| 707 |
if (ns->nsid > subsys->max_nsid) |
| 708 |
subsys->max_nsid = nsid; |
| 709 |
|
| 710 |
if (xa_insert(&subsys->namespaces, ns->nsid, ns, GFP_KERNEL)) |
| 711 |
goto out_exit; |
| 712 |
|
| 713 |
subsys->nr_namespaces++; |
| 714 |
|
| 715 |
mutex_unlock(&subsys->lock); |
| 716 |
|
| 717 |
down_write(&nvmet_ana_sem); |
| 718 |
ns->anagrpid = NVMET_DEFAULT_ANA_GRPID; |
| 719 |
nvmet_ana_group_enabled[ns->anagrpid]++; |
| 720 |
up_write(&nvmet_ana_sem); |
| 721 |
|
| 722 |
uuid_gen(&ns->uuid); |
| 723 |
ns->buffered_io = false; |
| 724 |
ns->csi = NVME_CSI_NVM; |
| 725 |
|
| 726 |
return ns; |
| 727 |
out_exit: |
| 728 |
subsys->max_nsid = nvmet_max_nsid(subsys); |
| 729 |
kfree(ns); |
| 730 |
out_unlock: |
| 731 |
mutex_unlock(&subsys->lock); |
| 732 |
return NULL; |
| 733 |
} |
| 734 |
|
| 735 |
static void nvmet_update_sq_head(struct nvmet_req *req) |
| 736 |
{ |
| 737 |
if (req->sq->size) { |
| 738 |
u32 old_sqhd, new_sqhd; |
| 739 |
|
| 740 |
old_sqhd = READ_ONCE(req->sq->sqhd); |
| 741 |
do { |
| 742 |
new_sqhd = (old_sqhd + 1) % req->sq->size; |
| 743 |
} while (!try_cmpxchg(&req->sq->sqhd, &old_sqhd, new_sqhd)); |
| 744 |
} |
| 745 |
req->cqe->sq_head = cpu_to_le16(req->sq->sqhd & 0x0000FFFF); |
| 746 |
} |
| 747 |
|
| 748 |
static void nvmet_set_error(struct nvmet_req *req, u16 status) |
| 749 |
{ |
| 750 |
struct nvmet_ctrl *ctrl = req->sq->ctrl; |
| 751 |
struct nvme_error_slot *new_error_slot; |
| 752 |
unsigned long flags; |
| 753 |
|
| 754 |
req->cqe->status = cpu_to_le16(status << 1); |
| 755 |
|
| 756 |
if (!ctrl || req->error_loc == NVMET_NO_ERROR_LOC) |
| 757 |
return; |
| 758 |
|
| 759 |
spin_lock_irqsave(&ctrl->error_lock, flags); |
| 760 |
ctrl->err_counter++; |
| 761 |
new_error_slot = |
| 762 |
&ctrl->slots[ctrl->err_counter % NVMET_ERROR_LOG_SLOTS]; |
| 763 |
|
| 764 |
new_error_slot->error_count = cpu_to_le64(ctrl->err_counter); |
| 765 |
new_error_slot->sqid = cpu_to_le16(req->sq->qid); |
| 766 |
new_error_slot->cmdid = cpu_to_le16(req->cmd->common.command_id); |
| 767 |
new_error_slot->status_field = cpu_to_le16(status << 1); |
| 768 |
new_error_slot->param_error_location = cpu_to_le16(req->error_loc); |
| 769 |
new_error_slot->lba = cpu_to_le64(req->error_slba); |
| 770 |
new_error_slot->nsid = req->cmd->common.nsid; |
| 771 |
spin_unlock_irqrestore(&ctrl->error_lock, flags); |
| 772 |
|
| 773 |
/* set the more bit for this request */ |
| 774 |
req->cqe->status |= cpu_to_le16(1 << 14); |
| 775 |
} |
| 776 |
|
| 777 |
static void __nvmet_req_complete(struct nvmet_req *req, u16 status) |
| 778 |
{ |
| 779 |
struct nvmet_ns *ns = req->ns; |
| 780 |
struct nvmet_pr_per_ctrl_ref *pc_ref = req->pc_ref; |
| 781 |
|
| 782 |
if (!req->sq->sqhd_disabled) |
| 783 |
nvmet_update_sq_head(req); |
| 784 |
req->cqe->sq_id = cpu_to_le16(req->sq->qid); |
| 785 |
req->cqe->command_id = req->cmd->common.command_id; |
| 786 |
|
| 787 |
if (unlikely(status)) |
| 788 |
nvmet_set_error(req, status); |
| 789 |
|
| 790 |
trace_nvmet_req_complete(req); |
| 791 |
|
| 792 |
req->ops->queue_response(req); |
| 793 |
|
| 794 |
if (pc_ref) |
| 795 |
nvmet_pr_put_ns_pc_ref(pc_ref); |
| 796 |
if (ns) |
| 797 |
nvmet_put_namespace(ns); |
| 798 |
} |
| 799 |
|
| 800 |
void nvmet_req_complete(struct nvmet_req *req, u16 status) |
| 801 |
{ |
| 802 |
struct nvmet_sq *sq = req->sq; |
| 803 |
|
| 804 |
__nvmet_req_complete(req, status); |
| 805 |
percpu_ref_put(&sq->ref); |
| 806 |
} |
| 807 |
EXPORT_SYMBOL_GPL(nvmet_req_complete); |
| 808 |
|
| 809 |
void nvmet_cq_init(struct nvmet_cq *cq) |
| 810 |
{ |
| 811 |
refcount_set(&cq->ref, 1); |
| 812 |
} |
| 813 |
EXPORT_SYMBOL_GPL(nvmet_cq_init); |
| 814 |
|
| 815 |
bool nvmet_cq_get(struct nvmet_cq *cq) |
| 816 |
{ |
| 817 |
return refcount_inc_not_zero(&cq->ref); |
| 818 |
} |
| 819 |
EXPORT_SYMBOL_GPL(nvmet_cq_get); |
| 820 |
|
| 821 |
void nvmet_cq_put(struct nvmet_cq *cq) |
| 822 |
{ |
| 823 |
if (refcount_dec_and_test(&cq->ref)) |
| 824 |
nvmet_cq_destroy(cq); |
| 825 |
} |
| 826 |
EXPORT_SYMBOL_GPL(nvmet_cq_put); |
| 827 |
|
| 828 |
void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, |
| 829 |
u16 qid, u16 size) |
| 830 |
{ |
| 831 |
cq->qid = qid; |
| 832 |
cq->size = size; |
| 833 |
|
| 834 |
ctrl->cqs[qid] = cq; |
| 835 |
} |
| 836 |
|
| 837 |
void nvmet_cq_destroy(struct nvmet_cq *cq) |
| 838 |
{ |
| 839 |
struct nvmet_ctrl *ctrl = cq->ctrl; |
| 840 |
|
| 841 |
if (ctrl) { |
| 842 |
ctrl->cqs[cq->qid] = NULL; |
| 843 |
nvmet_ctrl_put(cq->ctrl); |
| 844 |
cq->ctrl = NULL; |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, |
| 849 |
u16 qid, u16 size) |
| 850 |
{ |
| 851 |
sq->sqhd = 0; |
| 852 |
sq->qid = qid; |
| 853 |
sq->size = size; |
| 854 |
|
| 855 |
ctrl->sqs[qid] = sq; |
| 856 |
} |
| 857 |
|
| 858 |
static void nvmet_confirm_sq(struct percpu_ref *ref) |
| 859 |
{ |
| 860 |
struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref); |
| 861 |
|
| 862 |
complete(&sq->confirm_done); |
| 863 |
} |
| 864 |
|
| 865 |
u16 nvmet_check_cqid(struct nvmet_ctrl *ctrl, u16 cqid, bool create) |
| 866 |
{ |
| 867 |
if (!ctrl->cqs) |
| 868 |
return NVME_SC_INTERNAL | NVME_STATUS_DNR; |
| 869 |
|
| 870 |
if (cqid > ctrl->subsys->max_qid) |
| 871 |
return NVME_SC_QID_INVALID | NVME_STATUS_DNR; |
| 872 |
|
| 873 |
if ((create && ctrl->cqs[cqid]) || (!create && !ctrl->cqs[cqid])) |
| 874 |
return NVME_SC_QID_INVALID | NVME_STATUS_DNR; |
| 875 |
|
| 876 |
return NVME_SC_SUCCESS; |
| 877 |
} |
| 878 |
|
| 879 |
u16 nvmet_check_io_cqid(struct nvmet_ctrl *ctrl, u16 cqid, bool create) |
| 880 |
{ |
| 881 |
if (!cqid) |
| 882 |
return NVME_SC_QID_INVALID | NVME_STATUS_DNR; |
| 883 |
return nvmet_check_cqid(ctrl, cqid, create); |
| 884 |
} |
| 885 |
|
| 886 |
bool nvmet_cq_in_use(struct nvmet_cq *cq) |
| 887 |
{ |
| 888 |
return refcount_read(&cq->ref) > 1; |
| 889 |
} |
| 890 |
EXPORT_SYMBOL_GPL(nvmet_cq_in_use); |
| 891 |
|
| 892 |
u16 nvmet_cq_create(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, |
| 893 |
u16 qid, u16 size) |
| 894 |
{ |
| 895 |
u16 status; |
| 896 |
|
| 897 |
status = nvmet_check_cqid(ctrl, qid, true); |
| 898 |
if (status != NVME_SC_SUCCESS) |
| 899 |
return status; |
| 900 |
|
| 901 |
if (!kref_get_unless_zero(&ctrl->ref)) |
| 902 |
return NVME_SC_INTERNAL | NVME_STATUS_DNR; |
| 903 |
cq->ctrl = ctrl; |
| 904 |
|
| 905 |
nvmet_cq_init(cq); |
| 906 |
nvmet_cq_setup(ctrl, cq, qid, size); |
| 907 |
|
| 908 |
return NVME_SC_SUCCESS; |
| 909 |
} |
| 910 |
EXPORT_SYMBOL_GPL(nvmet_cq_create); |
| 911 |
|
| 912 |
u16 nvmet_check_sqid(struct nvmet_ctrl *ctrl, u16 sqid, |
| 913 |
bool create) |
| 914 |
{ |
| 915 |
if (!ctrl->sqs) |
| 916 |
return NVME_SC_INTERNAL | NVME_STATUS_DNR; |
| 917 |
|
| 918 |
if (sqid > ctrl->subsys->max_qid) |
| 919 |
return NVME_SC_QID_INVALID | NVME_STATUS_DNR; |
| 920 |
|
| 921 |
if ((create && ctrl->sqs[sqid]) || |
| 922 |
(!create && !ctrl->sqs[sqid])) |
| 923 |
return NVME_SC_QID_INVALID | NVME_STATUS_DNR; |
| 924 |
|
| 925 |
return NVME_SC_SUCCESS; |
| 926 |
} |
| 927 |
|
| 928 |
u16 nvmet_sq_create(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq, |
| 929 |
struct nvmet_cq *cq, u16 sqid, u16 size) |
| 930 |
{ |
| 931 |
u16 status; |
| 932 |
int ret; |
| 933 |
|
| 934 |
if (!kref_get_unless_zero(&ctrl->ref)) |
| 935 |
return NVME_SC_INTERNAL | NVME_STATUS_DNR; |
| 936 |
|
| 937 |
status = nvmet_check_sqid(ctrl, sqid, true); |
| 938 |
if (status != NVME_SC_SUCCESS) |
| 939 |
return status; |
| 940 |
|
| 941 |
ret = nvmet_sq_init(sq, cq); |
| 942 |
if (ret) { |
| 943 |
status = NVME_SC_INTERNAL | NVME_STATUS_DNR; |
| 944 |
goto ctrl_put; |
| 945 |
} |
| 946 |
|
| 947 |
nvmet_sq_setup(ctrl, sq, sqid, size); |
| 948 |
sq->ctrl = ctrl; |
| 949 |
|
| 950 |
return NVME_SC_SUCCESS; |
| 951 |
|
| 952 |
ctrl_put: |
| 953 |
nvmet_ctrl_put(ctrl); |
| 954 |
return status; |
| 955 |
} |
| 956 |
EXPORT_SYMBOL_GPL(nvmet_sq_create); |
| 957 |
|
| 958 |
void nvmet_sq_destroy(struct nvmet_sq *sq) |
| 959 |
{ |
| 960 |
struct nvmet_ctrl *ctrl = sq->ctrl; |
| 961 |
|
| 962 |
/* |
| 963 |
* If this is the admin queue, complete all AERs so that our |
| 964 |
* queue doesn't have outstanding requests on it. |
| 965 |
*/ |
| 966 |
if (ctrl && ctrl->sqs && ctrl->sqs[0] == sq) |
| 967 |
nvmet_async_events_failall(ctrl); |
| 968 |
percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq); |
| 969 |
wait_for_completion(&sq->confirm_done); |
| 970 |
wait_for_completion(&sq->free_done); |
| 971 |
percpu_ref_exit(&sq->ref); |
| 972 |
nvmet_auth_sq_free(sq); |
| 973 |
nvmet_cq_put(sq->cq); |
| 974 |
|
| 975 |
/* |
| 976 |
* we must reference the ctrl again after waiting for inflight IO |
| 977 |
* to complete. Because admin connect may have sneaked in after we |
| 978 |
* store sq->ctrl locally, but before we killed the percpu_ref. the |
| 979 |
* admin connect allocates and assigns sq->ctrl, which now needs a |
| 980 |
* final ref put, as this ctrl is going away. |
| 981 |
*/ |
| 982 |
ctrl = sq->ctrl; |
| 983 |
|
| 984 |
if (ctrl) { |
| 985 |
/* |
| 986 |
* The teardown flow may take some time, and the host may not |
| 987 |
* send us keep-alive during this period, hence reset the |
| 988 |
* traffic based keep-alive timer so we don't trigger a |
| 989 |
* controller teardown as a result of a keep-alive expiration. |
| 990 |
*/ |
| 991 |
ctrl->reset_tbkas = true; |
| 992 |
sq->ctrl->sqs[sq->qid] = NULL; |
| 993 |
nvmet_ctrl_put(ctrl); |
| 994 |
sq->ctrl = NULL; /* allows reusing the queue later */ |
| 995 |
} |
| 996 |
} |
| 997 |
EXPORT_SYMBOL_GPL(nvmet_sq_destroy); |
| 998 |
|
| 999 |
static void nvmet_sq_free(struct percpu_ref *ref) |
| 1000 |
{ |
| 1001 |
struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref); |
| 1002 |
|
| 1003 |
complete(&sq->free_done); |
| 1004 |
} |
| 1005 |
|
| 1006 |
int nvmet_sq_init(struct nvmet_sq *sq, struct nvmet_cq *cq) |
| 1007 |
{ |
| 1008 |
int ret; |
| 1009 |
|
| 1010 |
if (!nvmet_cq_get(cq)) |
| 1011 |
return -EINVAL; |
| 1012 |
|
| 1013 |
ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL); |
| 1014 |
if (ret) { |
| 1015 |
pr_err("percpu_ref init failed!\n"); |
| 1016 |
nvmet_cq_put(cq); |
| 1017 |
return ret; |
| 1018 |
} |
| 1019 |
init_completion(&sq->free_done); |
| 1020 |
init_completion(&sq->confirm_done); |
| 1021 |
nvmet_auth_sq_init(sq); |
| 1022 |
sq->cq = cq; |
| 1023 |
|
| 1024 |
return 0; |
| 1025 |
} |
| 1026 |
EXPORT_SYMBOL_GPL(nvmet_sq_init); |
| 1027 |
|
| 1028 |
static inline u16 nvmet_check_ana_state(struct nvmet_port *port, |
| 1029 |
struct nvmet_ns *ns) |
| 1030 |
{ |
| 1031 |
enum nvme_ana_state state = port->ana_state[ns->anagrpid]; |
| 1032 |
|
| 1033 |
if (unlikely(state == NVME_ANA_INACCESSIBLE)) |
| 1034 |
return NVME_SC_ANA_INACCESSIBLE; |
| 1035 |
if (unlikely(state == NVME_ANA_PERSISTENT_LOSS)) |
| 1036 |
return NVME_SC_ANA_PERSISTENT_LOSS; |
| 1037 |
if (unlikely(state == NVME_ANA_CHANGE)) |
| 1038 |
return NVME_SC_ANA_TRANSITION; |
| 1039 |
return 0; |
| 1040 |
} |
| 1041 |
|
| 1042 |
static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req) |
| 1043 |
{ |
| 1044 |
if (unlikely(req->ns->readonly)) { |
| 1045 |
switch (req->cmd->common.opcode) { |
| 1046 |
case nvme_cmd_read: |
| 1047 |
case nvme_cmd_flush: |
| 1048 |
break; |
| 1049 |
default: |
| 1050 |
return NVME_SC_NS_WRITE_PROTECTED; |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
return 0; |
| 1055 |
} |
| 1056 |
|
| 1057 |
static u32 nvmet_io_cmd_transfer_len(struct nvmet_req *req) |
| 1058 |
{ |
| 1059 |
struct nvme_command *cmd = req->cmd; |
| 1060 |
u32 metadata_len = 0; |
| 1061 |
|
| 1062 |
if (nvme_is_fabrics(cmd)) |
| 1063 |
return nvmet_fabrics_io_cmd_data_len(req); |
| 1064 |
|
| 1065 |
if (!req->ns) |
| 1066 |
return 0; |
| 1067 |
|
| 1068 |
switch (req->cmd->common.opcode) { |
| 1069 |
case nvme_cmd_read: |
| 1070 |
case nvme_cmd_write: |
| 1071 |
case nvme_cmd_zone_append: |
| 1072 |
if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns)) |
| 1073 |
metadata_len = nvmet_rw_metadata_len(req); |
| 1074 |
return nvmet_rw_data_len(req) + metadata_len; |
| 1075 |
case nvme_cmd_dsm: |
| 1076 |
return nvmet_dsm_len(req); |
| 1077 |
case nvme_cmd_zone_mgmt_recv: |
| 1078 |
return (le32_to_cpu(req->cmd->zmr.numd) + 1) << 2; |
| 1079 |
default: |
| 1080 |
return 0; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
static u16 nvmet_parse_io_cmd(struct nvmet_req *req) |
| 1085 |
{ |
| 1086 |
struct nvme_command *cmd = req->cmd; |
| 1087 |
u16 ret; |
| 1088 |
|
| 1089 |
if (nvme_is_fabrics(cmd)) |
| 1090 |
return nvmet_parse_fabrics_io_cmd(req); |
| 1091 |
|
| 1092 |
if (unlikely(!nvmet_check_auth_status(req))) |
| 1093 |
return NVME_SC_AUTH_REQUIRED | NVME_STATUS_DNR; |
| 1094 |
|
| 1095 |
ret = nvmet_check_ctrl_status(req); |
| 1096 |
if (unlikely(ret)) |
| 1097 |
return ret; |
| 1098 |
|
| 1099 |
if (nvmet_is_passthru_req(req)) |
| 1100 |
return nvmet_parse_passthru_io_cmd(req); |
| 1101 |
|
| 1102 |
ret = nvmet_req_find_ns(req); |
| 1103 |
if (unlikely(ret)) |
| 1104 |
return ret; |
| 1105 |
|
| 1106 |
ret = nvmet_check_ana_state(req->port, req->ns); |
| 1107 |
if (unlikely(ret)) { |
| 1108 |
req->error_loc = offsetof(struct nvme_common_command, nsid); |
| 1109 |
return ret; |
| 1110 |
} |
| 1111 |
ret = nvmet_io_cmd_check_access(req); |
| 1112 |
if (unlikely(ret)) { |
| 1113 |
req->error_loc = offsetof(struct nvme_common_command, nsid); |
| 1114 |
return ret; |
| 1115 |
} |
| 1116 |
|
| 1117 |
if (req->ns->pr.enable) { |
| 1118 |
ret = nvmet_parse_pr_cmd(req); |
| 1119 |
if (!ret) |
| 1120 |
return ret; |
| 1121 |
} |
| 1122 |
|
| 1123 |
switch (req->ns->csi) { |
| 1124 |
case NVME_CSI_NVM: |
| 1125 |
if (req->ns->file) |
| 1126 |
ret = nvmet_file_parse_io_cmd(req); |
| 1127 |
else |
| 1128 |
ret = nvmet_bdev_parse_io_cmd(req); |
| 1129 |
break; |
| 1130 |
case NVME_CSI_ZNS: |
| 1131 |
if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) |
| 1132 |
ret = nvmet_bdev_zns_parse_io_cmd(req); |
| 1133 |
else |
| 1134 |
ret = NVME_SC_INVALID_IO_CMD_SET; |
| 1135 |
break; |
| 1136 |
default: |
| 1137 |
ret = NVME_SC_INVALID_IO_CMD_SET; |
| 1138 |
} |
| 1139 |
if (ret) |
| 1140 |
return ret; |
| 1141 |
|
| 1142 |
if (req->ns->pr.enable) { |
| 1143 |
ret = nvmet_pr_check_cmd_access(req); |
| 1144 |
if (ret) |
| 1145 |
return ret; |
| 1146 |
|
| 1147 |
ret = nvmet_pr_get_ns_pc_ref(req); |
| 1148 |
} |
| 1149 |
return ret; |
| 1150 |
} |
| 1151 |
|
| 1152 |
bool nvmet_req_init(struct nvmet_req *req, struct nvmet_sq *sq, |
| 1153 |
const struct nvmet_fabrics_ops *ops) |
| 1154 |
{ |
| 1155 |
u8 flags = req->cmd->common.flags; |
| 1156 |
u16 status; |
| 1157 |
|
| 1158 |
req->cq = sq->cq; |
| 1159 |
req->sq = sq; |
| 1160 |
req->ops = ops; |
| 1161 |
req->sg = NULL; |
| 1162 |
req->metadata_sg = NULL; |
| 1163 |
req->sg_cnt = 0; |
| 1164 |
req->metadata_sg_cnt = 0; |
| 1165 |
req->transfer_len = 0; |
| 1166 |
req->metadata_len = 0; |
| 1167 |
req->cqe->result.u64 = 0; |
| 1168 |
req->cqe->status = 0; |
| 1169 |
req->cqe->sq_head = 0; |
| 1170 |
req->ns = NULL; |
| 1171 |
req->error_loc = NVMET_NO_ERROR_LOC; |
| 1172 |
req->error_slba = 0; |
| 1173 |
req->pc_ref = NULL; |
| 1174 |
|
| 1175 |
/* no support for fused commands yet */ |
| 1176 |
if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) { |
| 1177 |
req->error_loc = offsetof(struct nvme_common_command, flags); |
| 1178 |
status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; |
| 1179 |
goto fail; |
| 1180 |
} |
| 1181 |
|
| 1182 |
/* |
| 1183 |
* For fabrics, PSDT field shall describe metadata pointer (MPTR) that |
| 1184 |
* contains an address of a single contiguous physical buffer that is |
| 1185 |
* byte aligned. For PCI controllers, this is optional so not enforced. |
| 1186 |
*/ |
| 1187 |
if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) { |
| 1188 |
if (!req->sq->ctrl || !nvmet_is_pci_ctrl(req->sq->ctrl)) { |
| 1189 |
req->error_loc = |
| 1190 |
offsetof(struct nvme_common_command, flags); |
| 1191 |
status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; |
| 1192 |
goto fail; |
| 1193 |
} |
| 1194 |
} |
| 1195 |
|
| 1196 |
if (unlikely(!req->sq->ctrl)) |
| 1197 |
/* will return an error for any non-connect command: */ |
| 1198 |
status = nvmet_parse_connect_cmd(req); |
| 1199 |
else if (likely(req->sq->qid != 0)) |
| 1200 |
status = nvmet_parse_io_cmd(req); |
| 1201 |
else |
| 1202 |
status = nvmet_parse_admin_cmd(req); |
| 1203 |
|
| 1204 |
if (status) |
| 1205 |
goto fail; |
| 1206 |
|
| 1207 |
trace_nvmet_req_init(req, req->cmd); |
| 1208 |
|
| 1209 |
if (unlikely(!percpu_ref_tryget_live(&sq->ref))) { |
| 1210 |
status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; |
| 1211 |
goto fail; |
| 1212 |
} |
| 1213 |
|
| 1214 |
if (sq->ctrl) |
| 1215 |
sq->ctrl->reset_tbkas = true; |
| 1216 |
|
| 1217 |
return true; |
| 1218 |
|
| 1219 |
fail: |
| 1220 |
__nvmet_req_complete(req, status); |
| 1221 |
return false; |
| 1222 |
} |
| 1223 |
EXPORT_SYMBOL_GPL(nvmet_req_init); |
| 1224 |
|
| 1225 |
void nvmet_req_uninit(struct nvmet_req *req) |
| 1226 |
{ |
| 1227 |
percpu_ref_put(&req->sq->ref); |
| 1228 |
if (req->pc_ref) |
| 1229 |
nvmet_pr_put_ns_pc_ref(req->pc_ref); |
| 1230 |
if (req->ns) |
| 1231 |
nvmet_put_namespace(req->ns); |
| 1232 |
} |
| 1233 |
EXPORT_SYMBOL_GPL(nvmet_req_uninit); |
| 1234 |
|
| 1235 |
size_t nvmet_req_transfer_len(struct nvmet_req *req) |
| 1236 |
{ |
| 1237 |
if (likely(req->sq->qid != 0)) |
| 1238 |
return nvmet_io_cmd_transfer_len(req); |
| 1239 |
if (unlikely(!req->sq->ctrl)) |
| 1240 |
return nvmet_connect_cmd_data_len(req); |
| 1241 |
return nvmet_admin_cmd_data_len(req); |
| 1242 |
} |
| 1243 |
EXPORT_SYMBOL_GPL(nvmet_req_transfer_len); |
| 1244 |
|
| 1245 |
bool nvmet_check_transfer_len(struct nvmet_req *req, size_t len) |
| 1246 |
{ |
| 1247 |
if (unlikely(len != req->transfer_len)) { |
| 1248 |
u16 status; |
| 1249 |
|
| 1250 |
req->error_loc = offsetof(struct nvme_common_command, dptr); |
| 1251 |
if (req->cmd->common.flags & NVME_CMD_SGL_ALL) |
| 1252 |
status = NVME_SC_SGL_INVALID_DATA; |
| 1253 |
else |
| 1254 |
status = NVME_SC_INVALID_FIELD; |
| 1255 |
nvmet_req_complete(req, status | NVME_STATUS_DNR); |
| 1256 |
return false; |
| 1257 |
} |
| 1258 |
|
| 1259 |
return true; |
| 1260 |
} |
| 1261 |
EXPORT_SYMBOL_GPL(nvmet_check_transfer_len); |
| 1262 |
|
| 1263 |
bool nvmet_check_data_len_lte(struct nvmet_req *req, size_t data_len) |
| 1264 |
{ |
| 1265 |
if (unlikely(data_len > req->transfer_len)) { |
| 1266 |
u16 status; |
| 1267 |
|
| 1268 |
req->error_loc = offsetof(struct nvme_common_command, dptr); |
| 1269 |
if (req->cmd->common.flags & NVME_CMD_SGL_ALL) |
| 1270 |
status = NVME_SC_SGL_INVALID_DATA; |
| 1271 |
else |
| 1272 |
status = NVME_SC_INVALID_FIELD; |
| 1273 |
nvmet_req_complete(req, status | NVME_STATUS_DNR); |
| 1274 |
return false; |
| 1275 |
} |
| 1276 |
|
| 1277 |
return true; |
| 1278 |
} |
| 1279 |
|
| 1280 |
static unsigned int nvmet_data_transfer_len(struct nvmet_req *req) |
| 1281 |
{ |
| 1282 |
return req->transfer_len - req->metadata_len; |
| 1283 |
} |
| 1284 |
|
| 1285 |
static int nvmet_req_alloc_p2pmem_sgls(struct pci_dev *p2p_dev, |
| 1286 |
struct nvmet_req *req) |
| 1287 |
{ |
| 1288 |
req->sg = pci_p2pmem_alloc_sgl(p2p_dev, &req->sg_cnt, |
| 1289 |
nvmet_data_transfer_len(req)); |
| 1290 |
if (!req->sg) |
| 1291 |
goto out_err; |
| 1292 |
|
| 1293 |
if (req->metadata_len) { |
| 1294 |
req->metadata_sg = pci_p2pmem_alloc_sgl(p2p_dev, |
| 1295 |
&req->metadata_sg_cnt, req->metadata_len); |
| 1296 |
if (!req->metadata_sg) |
| 1297 |
goto out_free_sg; |
| 1298 |
} |
| 1299 |
|
| 1300 |
req->p2p_dev = p2p_dev; |
| 1301 |
|
| 1302 |
return 0; |
| 1303 |
out_free_sg: |
| 1304 |
pci_p2pmem_free_sgl(req->p2p_dev, req->sg); |
| 1305 |
out_err: |
| 1306 |
return -ENOMEM; |
| 1307 |
} |
| 1308 |
|
| 1309 |
static struct pci_dev *nvmet_req_find_p2p_dev(struct nvmet_req *req) |
| 1310 |
{ |
| 1311 |
if (!IS_ENABLED(CONFIG_PCI_P2PDMA) || |
| 1312 |
!req->sq->ctrl || !req->sq->qid || !req->ns) |
| 1313 |
return NULL; |
| 1314 |
return radix_tree_lookup(&req->sq->ctrl->p2p_ns_map, req->ns->nsid); |
| 1315 |
} |
| 1316 |
|
| 1317 |
int nvmet_req_alloc_sgls(struct nvmet_req *req) |
| 1318 |
{ |
| 1319 |
struct pci_dev *p2p_dev = nvmet_req_find_p2p_dev(req); |
| 1320 |
|
| 1321 |
if (p2p_dev && !nvmet_req_alloc_p2pmem_sgls(p2p_dev, req)) |
| 1322 |
return 0; |
| 1323 |
|
| 1324 |
req->sg = sgl_alloc(nvmet_data_transfer_len(req), GFP_KERNEL, |
| 1325 |
&req->sg_cnt); |
| 1326 |
if (unlikely(!req->sg)) |
| 1327 |
goto out; |
| 1328 |
|
| 1329 |
if (req->metadata_len) { |
| 1330 |
req->metadata_sg = sgl_alloc(req->metadata_len, GFP_KERNEL, |
| 1331 |
&req->metadata_sg_cnt); |
| 1332 |
if (unlikely(!req->metadata_sg)) |
| 1333 |
goto out_free; |
| 1334 |
} |
| 1335 |
|
| 1336 |
return 0; |
| 1337 |
out_free: |
| 1338 |
sgl_free(req->sg); |
| 1339 |
out: |
| 1340 |
return -ENOMEM; |
| 1341 |
} |
| 1342 |
EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgls); |
| 1343 |
|
| 1344 |
void nvmet_req_free_sgls(struct nvmet_req *req) |
| 1345 |
{ |
| 1346 |
if (req->p2p_dev) { |
| 1347 |
pci_p2pmem_free_sgl(req->p2p_dev, req->sg); |
| 1348 |
if (req->metadata_sg) |
| 1349 |
pci_p2pmem_free_sgl(req->p2p_dev, req->metadata_sg); |
| 1350 |
req->p2p_dev = NULL; |
| 1351 |
} else { |
| 1352 |
sgl_free(req->sg); |
| 1353 |
if (req->metadata_sg) |
| 1354 |
sgl_free(req->metadata_sg); |
| 1355 |
} |
| 1356 |
|
| 1357 |
req->sg = NULL; |
| 1358 |
req->metadata_sg = NULL; |
| 1359 |
req->sg_cnt = 0; |
| 1360 |
req->metadata_sg_cnt = 0; |
| 1361 |
} |
| 1362 |
EXPORT_SYMBOL_GPL(nvmet_req_free_sgls); |
| 1363 |
|
| 1364 |
static inline bool nvmet_css_supported(u8 cc_css) |
| 1365 |
{ |
| 1366 |
switch (cc_css << NVME_CC_CSS_SHIFT) { |
| 1367 |
case NVME_CC_CSS_NVM: |
| 1368 |
case NVME_CC_CSS_CSI: |
| 1369 |
return true; |
| 1370 |
default: |
| 1371 |
return false; |
| 1372 |
} |
| 1373 |
} |
| 1374 |
|
| 1375 |
static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl) |
| 1376 |
{ |
| 1377 |
lockdep_assert_held(&ctrl->lock); |
| 1378 |
|
| 1379 |
/* |
| 1380 |
* Only I/O controllers should verify iosqes,iocqes. |
| 1381 |
* Strictly speaking, the spec says a discovery controller |
| 1382 |
* should verify iosqes,iocqes are zeroed, however that |
| 1383 |
* would break backwards compatibility, so don't enforce it. |
| 1384 |
*/ |
| 1385 |
if (!nvmet_is_disc_subsys(ctrl->subsys) && |
| 1386 |
(nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES || |
| 1387 |
nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES)) { |
| 1388 |
ctrl->csts = NVME_CSTS_CFS; |
| 1389 |
return; |
| 1390 |
} |
| 1391 |
|
| 1392 |
if (nvmet_cc_mps(ctrl->cc) != 0 || |
| 1393 |
nvmet_cc_ams(ctrl->cc) != 0 || |
| 1394 |
!nvmet_css_supported(nvmet_cc_css(ctrl->cc))) { |
| 1395 |
ctrl->csts = NVME_CSTS_CFS; |
| 1396 |
return; |
| 1397 |
} |
| 1398 |
|
| 1399 |
ctrl->csts = NVME_CSTS_RDY; |
| 1400 |
|
| 1401 |
/* |
| 1402 |
* Controllers that are not yet enabled should not really enforce the |
| 1403 |
* keep alive timeout, but we still want to track a timeout and cleanup |
| 1404 |
* in case a host died before it enabled the controller. Hence, simply |
| 1405 |
* reset the keep alive timer when the controller is enabled. |
| 1406 |
*/ |
| 1407 |
if (ctrl->kato) |
| 1408 |
mod_delayed_work(nvmet_wq, &ctrl->ka_work, ctrl->kato * HZ); |
| 1409 |
} |
| 1410 |
|
| 1411 |
static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl) |
| 1412 |
{ |
| 1413 |
lockdep_assert_held(&ctrl->lock); |
| 1414 |
|
| 1415 |
/* XXX: tear down queues? */ |
| 1416 |
ctrl->csts &= ~NVME_CSTS_RDY; |
| 1417 |
ctrl->cc = 0; |
| 1418 |
} |
| 1419 |
|
| 1420 |
void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new) |
| 1421 |
{ |
| 1422 |
u32 old; |
| 1423 |
|
| 1424 |
mutex_lock(&ctrl->lock); |
| 1425 |
old = ctrl->cc; |
| 1426 |
ctrl->cc = new; |
| 1427 |
|
| 1428 |
if (nvmet_cc_en(new) && !nvmet_cc_en(old)) |
| 1429 |
nvmet_start_ctrl(ctrl); |
| 1430 |
if (!nvmet_cc_en(new) && nvmet_cc_en(old)) |
| 1431 |
nvmet_clear_ctrl(ctrl); |
| 1432 |
if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) { |
| 1433 |
nvmet_clear_ctrl(ctrl); |
| 1434 |
ctrl->csts |= NVME_CSTS_SHST_CMPLT; |
| 1435 |
} |
| 1436 |
if (!nvmet_cc_shn(new) && nvmet_cc_shn(old)) |
| 1437 |
ctrl->csts &= ~NVME_CSTS_SHST_CMPLT; |
| 1438 |
mutex_unlock(&ctrl->lock); |
| 1439 |
} |
| 1440 |
EXPORT_SYMBOL_GPL(nvmet_update_cc); |
| 1441 |
|
| 1442 |
static void nvmet_init_cap(struct nvmet_ctrl *ctrl) |
| 1443 |
{ |
| 1444 |
/* command sets supported: NVMe command set: */ |
| 1445 |
ctrl->cap = (1ULL << 37); |
| 1446 |
/* Controller supports one or more I/O Command Sets */ |
| 1447 |
ctrl->cap |= (1ULL << 43); |
| 1448 |
/* CC.EN timeout in 500msec units: */ |
| 1449 |
ctrl->cap |= (15ULL << 24); |
| 1450 |
/* maximum queue entries supported: */ |
| 1451 |
if (ctrl->ops->get_max_queue_size) |
| 1452 |
ctrl->cap |= min_t(u16, ctrl->ops->get_max_queue_size(ctrl), |
| 1453 |
ctrl->port->max_queue_size) - 1; |
| 1454 |
else |
| 1455 |
ctrl->cap |= ctrl->port->max_queue_size - 1; |
| 1456 |
|
| 1457 |
if (nvmet_is_passthru_subsys(ctrl->subsys)) |
| 1458 |
nvmet_passthrough_override_cap(ctrl); |
| 1459 |
} |
| 1460 |
|
| 1461 |
struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn, |
| 1462 |
const char *hostnqn, u16 cntlid, |
| 1463 |
struct nvmet_req *req) |
| 1464 |
{ |
| 1465 |
struct nvmet_ctrl *ctrl = NULL; |
| 1466 |
struct nvmet_subsys *subsys; |
| 1467 |
|
| 1468 |
subsys = nvmet_find_get_subsys(req->port, subsysnqn); |
| 1469 |
if (!subsys) { |
| 1470 |
pr_warn("connect request for invalid subsystem %s!\n", |
| 1471 |
subsysnqn); |
| 1472 |
req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn); |
| 1473 |
goto out; |
| 1474 |
} |
| 1475 |
|
| 1476 |
mutex_lock(&subsys->lock); |
| 1477 |
list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) { |
| 1478 |
if (ctrl->cntlid == cntlid) { |
| 1479 |
if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) { |
| 1480 |
pr_warn("hostnqn mismatch.\n"); |
| 1481 |
continue; |
| 1482 |
} |
| 1483 |
if (!kref_get_unless_zero(&ctrl->ref)) |
| 1484 |
continue; |
| 1485 |
|
| 1486 |
/* ctrl found */ |
| 1487 |
goto found; |
| 1488 |
} |
| 1489 |
} |
| 1490 |
|
| 1491 |
ctrl = NULL; /* ctrl not found */ |
| 1492 |
pr_warn("could not find controller %d for subsys %s / host %s\n", |
| 1493 |
cntlid, subsysnqn, hostnqn); |
| 1494 |
req->cqe->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid); |
| 1495 |
|
| 1496 |
found: |
| 1497 |
mutex_unlock(&subsys->lock); |
| 1498 |
nvmet_subsys_put(subsys); |
| 1499 |
out: |
| 1500 |
return ctrl; |
| 1501 |
} |
| 1502 |
|
| 1503 |
u16 nvmet_check_ctrl_status(struct nvmet_req *req) |
| 1504 |
{ |
| 1505 |
if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) { |
| 1506 |
pr_err("got cmd %d while CC.EN == 0 on qid = %d\n", |
| 1507 |
req->cmd->common.opcode, req->sq->qid); |
| 1508 |
return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR; |
| 1509 |
} |
| 1510 |
|
| 1511 |
if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) { |
| 1512 |
pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n", |
| 1513 |
req->cmd->common.opcode, req->sq->qid); |
| 1514 |
return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR; |
| 1515 |
} |
| 1516 |
|
| 1517 |
if (unlikely(!nvmet_check_auth_status(req))) { |
| 1518 |
pr_warn("qid %d not authenticated\n", req->sq->qid); |
| 1519 |
return NVME_SC_AUTH_REQUIRED | NVME_STATUS_DNR; |
| 1520 |
} |
| 1521 |
return 0; |
| 1522 |
} |
| 1523 |
|
| 1524 |
bool nvmet_host_allowed(struct nvmet_subsys *subsys, const char *hostnqn) |
| 1525 |
{ |
| 1526 |
struct nvmet_host_link *p; |
| 1527 |
|
| 1528 |
lockdep_assert_held(&nvmet_config_sem); |
| 1529 |
|
| 1530 |
if (subsys->allow_any_host) |
| 1531 |
return true; |
| 1532 |
|
| 1533 |
if (nvmet_is_disc_subsys(subsys)) /* allow all access to disc subsys */ |
| 1534 |
return true; |
| 1535 |
|
| 1536 |
list_for_each_entry(p, &subsys->hosts, entry) { |
| 1537 |
if (!strcmp(nvmet_host_name(p->host), hostnqn)) |
| 1538 |
return true; |
| 1539 |
} |
| 1540 |
|
| 1541 |
return false; |
| 1542 |
} |
| 1543 |
|
| 1544 |
static void nvmet_setup_p2p_ns_map(struct nvmet_ctrl *ctrl, |
| 1545 |
struct device *p2p_client) |
| 1546 |
{ |
| 1547 |
struct nvmet_ns *ns; |
| 1548 |
unsigned long idx; |
| 1549 |
|
| 1550 |
lockdep_assert_held(&ctrl->subsys->lock); |
| 1551 |
|
| 1552 |
if (!p2p_client) |
| 1553 |
return; |
| 1554 |
|
| 1555 |
ctrl->p2p_client = get_device(p2p_client); |
| 1556 |
|
| 1557 |
nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) |
| 1558 |
nvmet_p2pmem_ns_add_p2p(ctrl, ns); |
| 1559 |
} |
| 1560 |
|
| 1561 |
static void nvmet_release_p2p_ns_map(struct nvmet_ctrl *ctrl) |
| 1562 |
{ |
| 1563 |
struct radix_tree_iter iter; |
| 1564 |
void __rcu **slot; |
| 1565 |
|
| 1566 |
lockdep_assert_held(&ctrl->subsys->lock); |
| 1567 |
|
| 1568 |
radix_tree_for_each_slot(slot, &ctrl->p2p_ns_map, &iter, 0) |
| 1569 |
pci_dev_put(radix_tree_deref_slot(slot)); |
| 1570 |
|
| 1571 |
put_device(ctrl->p2p_client); |
| 1572 |
} |
| 1573 |
|
| 1574 |
static void nvmet_fatal_error_handler(struct work_struct *work) |
| 1575 |
{ |
| 1576 |
struct nvmet_ctrl *ctrl = |
| 1577 |
container_of(work, struct nvmet_ctrl, fatal_err_work); |
| 1578 |
|
| 1579 |
pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid); |
| 1580 |
ctrl->ops->delete_ctrl(ctrl); |
| 1581 |
} |
| 1582 |
|
| 1583 |
struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args) |
| 1584 |
{ |
| 1585 |
struct nvmet_subsys *subsys; |
| 1586 |
struct nvmet_ctrl *ctrl; |
| 1587 |
u32 kato = args->kato; |
| 1588 |
u8 dhchap_status; |
| 1589 |
int ret; |
| 1590 |
|
| 1591 |
args->status = NVME_SC_CONNECT_INVALID_PARAM | NVME_STATUS_DNR; |
| 1592 |
subsys = nvmet_find_get_subsys(args->port, args->subsysnqn); |
| 1593 |
if (!subsys) { |
| 1594 |
pr_warn("connect request for invalid subsystem %s!\n", |
| 1595 |
args->subsysnqn); |
| 1596 |
args->result = IPO_IATTR_CONNECT_DATA(subsysnqn); |
| 1597 |
args->error_loc = offsetof(struct nvme_common_command, dptr); |
| 1598 |
return NULL; |
| 1599 |
} |
| 1600 |
|
| 1601 |
down_read(&nvmet_config_sem); |
| 1602 |
if (!nvmet_host_allowed(subsys, args->hostnqn)) { |
| 1603 |
pr_info("connect by host %s for subsystem %s not allowed\n", |
| 1604 |
args->hostnqn, args->subsysnqn); |
| 1605 |
args->result = IPO_IATTR_CONNECT_DATA(hostnqn); |
| 1606 |
up_read(&nvmet_config_sem); |
| 1607 |
args->status = NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR; |
| 1608 |
args->error_loc = offsetof(struct nvme_common_command, dptr); |
| 1609 |
goto out_put_subsystem; |
| 1610 |
} |
| 1611 |
up_read(&nvmet_config_sem); |
| 1612 |
|
| 1613 |
args->status = NVME_SC_INTERNAL; |
| 1614 |
ctrl = kzalloc_obj(*ctrl); |
| 1615 |
if (!ctrl) |
| 1616 |
goto out_put_subsystem; |
| 1617 |
mutex_init(&ctrl->lock); |
| 1618 |
|
| 1619 |
ctrl->port = args->port; |
| 1620 |
ctrl->ops = args->ops; |
| 1621 |
|
| 1622 |
#ifdef CONFIG_NVME_TARGET_PASSTHRU |
| 1623 |
/* By default, set loop targets to clear IDS by default */ |
| 1624 |
if (ctrl->port->disc_addr.trtype == NVMF_TRTYPE_LOOP) |
| 1625 |
subsys->clear_ids = 1; |
| 1626 |
#endif |
| 1627 |
|
| 1628 |
INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work); |
| 1629 |
INIT_LIST_HEAD(&ctrl->async_events); |
| 1630 |
INIT_RADIX_TREE(&ctrl->p2p_ns_map, GFP_KERNEL); |
| 1631 |
INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler); |
| 1632 |
INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer); |
| 1633 |
|
| 1634 |
memcpy(ctrl->hostnqn, args->hostnqn, NVMF_NQN_SIZE); |
| 1635 |
|
| 1636 |
kref_init(&ctrl->ref); |
| 1637 |
ctrl->subsys = subsys; |
| 1638 |
ctrl->pi_support = ctrl->port->pi_enable && ctrl->subsys->pi_support; |
| 1639 |
nvmet_init_cap(ctrl); |
| 1640 |
WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL); |
| 1641 |
|
| 1642 |
ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES, |
| 1643 |
sizeof(__le32), GFP_KERNEL); |
| 1644 |
if (!ctrl->changed_ns_list) |
| 1645 |
goto out_free_ctrl; |
| 1646 |
|
| 1647 |
ctrl->sqs = kzalloc_objs(struct nvmet_sq *, subsys->max_qid + 1); |
| 1648 |
if (!ctrl->sqs) |
| 1649 |
goto out_free_changed_ns_list; |
| 1650 |
|
| 1651 |
ctrl->cqs = kzalloc_objs(struct nvmet_cq *, subsys->max_qid + 1); |
| 1652 |
if (!ctrl->cqs) |
| 1653 |
goto out_free_sqs; |
| 1654 |
|
| 1655 |
ret = ida_alloc_range(&cntlid_ida, |
| 1656 |
subsys->cntlid_min, subsys->cntlid_max, |
| 1657 |
GFP_KERNEL); |
| 1658 |
if (ret < 0) { |
| 1659 |
args->status = NVME_SC_CONNECT_CTRL_BUSY | NVME_STATUS_DNR; |
| 1660 |
goto out_free_cqs; |
| 1661 |
} |
| 1662 |
ctrl->cntlid = ret; |
| 1663 |
|
| 1664 |
/* |
| 1665 |
* Discovery controllers may use some arbitrary high value |
| 1666 |
* in order to cleanup stale discovery sessions |
| 1667 |
*/ |
| 1668 |
if (nvmet_is_disc_subsys(ctrl->subsys) && !kato) |
| 1669 |
kato = NVMET_DISC_KATO_MS; |
| 1670 |
|
| 1671 |
/* keep-alive timeout in seconds */ |
| 1672 |
ctrl->kato = DIV_ROUND_UP(kato, 1000); |
| 1673 |
|
| 1674 |
ctrl->err_counter = 0; |
| 1675 |
spin_lock_init(&ctrl->error_lock); |
| 1676 |
|
| 1677 |
nvmet_start_keep_alive_timer(ctrl); |
| 1678 |
|
| 1679 |
mutex_lock(&subsys->lock); |
| 1680 |
ret = nvmet_ctrl_init_pr(ctrl); |
| 1681 |
if (ret) |
| 1682 |
goto init_pr_fail; |
| 1683 |
list_add_tail(&ctrl->subsys_entry, &subsys->ctrls); |
| 1684 |
nvmet_setup_p2p_ns_map(ctrl, args->p2p_client); |
| 1685 |
nvmet_debugfs_ctrl_setup(ctrl); |
| 1686 |
mutex_unlock(&subsys->lock); |
| 1687 |
|
| 1688 |
if (args->hostid) |
| 1689 |
uuid_copy(&ctrl->hostid, args->hostid); |
| 1690 |
|
| 1691 |
dhchap_status = nvmet_setup_auth(ctrl, args->sq); |
| 1692 |
if (dhchap_status) { |
| 1693 |
pr_err("Failed to setup authentication, dhchap status %u\n", |
| 1694 |
dhchap_status); |
| 1695 |
nvmet_ctrl_put(ctrl); |
| 1696 |
if (dhchap_status == NVME_AUTH_DHCHAP_FAILURE_FAILED) |
| 1697 |
args->status = |
| 1698 |
NVME_SC_CONNECT_INVALID_HOST | NVME_STATUS_DNR; |
| 1699 |
else |
| 1700 |
args->status = NVME_SC_INTERNAL; |
| 1701 |
return NULL; |
| 1702 |
} |
| 1703 |
|
| 1704 |
args->status = NVME_SC_SUCCESS; |
| 1705 |
|
| 1706 |
pr_info("Created %s controller %d for subsystem %s for NQN %s%s%s%s.\n", |
| 1707 |
nvmet_is_disc_subsys(ctrl->subsys) ? "discovery" : "nvm", |
| 1708 |
ctrl->cntlid, ctrl->subsys->subsysnqn, ctrl->hostnqn, |
| 1709 |
ctrl->pi_support ? " T10-PI is enabled" : "", |
| 1710 |
nvmet_has_auth(ctrl, args->sq) ? " with DH-HMAC-CHAP" : "", |
| 1711 |
nvmet_queue_tls_keyid(args->sq) ? ", TLS" : ""); |
| 1712 |
|
| 1713 |
return ctrl; |
| 1714 |
|
| 1715 |
init_pr_fail: |
| 1716 |
mutex_unlock(&subsys->lock); |
| 1717 |
nvmet_stop_keep_alive_timer(ctrl); |
| 1718 |
ida_free(&cntlid_ida, ctrl->cntlid); |
| 1719 |
out_free_cqs: |
| 1720 |
kfree(ctrl->cqs); |
| 1721 |
out_free_sqs: |
| 1722 |
kfree(ctrl->sqs); |
| 1723 |
out_free_changed_ns_list: |
| 1724 |
kfree(ctrl->changed_ns_list); |
| 1725 |
out_free_ctrl: |
| 1726 |
kfree(ctrl); |
| 1727 |
out_put_subsystem: |
| 1728 |
nvmet_subsys_put(subsys); |
| 1729 |
return NULL; |
| 1730 |
} |
| 1731 |
EXPORT_SYMBOL_GPL(nvmet_alloc_ctrl); |
| 1732 |
|
| 1733 |
static void nvmet_ctrl_free(struct kref *ref) |
| 1734 |
{ |
| 1735 |
struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref); |
| 1736 |
struct nvmet_subsys *subsys = ctrl->subsys; |
| 1737 |
|
| 1738 |
mutex_lock(&subsys->lock); |
| 1739 |
nvmet_ctrl_destroy_pr(ctrl); |
| 1740 |
nvmet_release_p2p_ns_map(ctrl); |
| 1741 |
list_del(&ctrl->subsys_entry); |
| 1742 |
mutex_unlock(&subsys->lock); |
| 1743 |
|
| 1744 |
nvmet_stop_keep_alive_timer(ctrl); |
| 1745 |
|
| 1746 |
cancel_work_sync(&ctrl->async_event_work); |
| 1747 |
cancel_work_sync(&ctrl->fatal_err_work); |
| 1748 |
|
| 1749 |
nvmet_destroy_auth(ctrl); |
| 1750 |
|
| 1751 |
nvmet_debugfs_ctrl_free(ctrl); |
| 1752 |
|
| 1753 |
ida_free(&cntlid_ida, ctrl->cntlid); |
| 1754 |
|
| 1755 |
nvmet_async_events_free(ctrl); |
| 1756 |
kfree(ctrl->sqs); |
| 1757 |
kfree(ctrl->cqs); |
| 1758 |
kfree(ctrl->changed_ns_list); |
| 1759 |
kfree(ctrl); |
| 1760 |
|
| 1761 |
nvmet_subsys_put(subsys); |
| 1762 |
} |
| 1763 |
|
| 1764 |
void nvmet_ctrl_put(struct nvmet_ctrl *ctrl) |
| 1765 |
{ |
| 1766 |
kref_put(&ctrl->ref, nvmet_ctrl_free); |
| 1767 |
} |
| 1768 |
EXPORT_SYMBOL_GPL(nvmet_ctrl_put); |
| 1769 |
|
| 1770 |
void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl) |
| 1771 |
{ |
| 1772 |
mutex_lock(&ctrl->lock); |
| 1773 |
if (!(ctrl->csts & NVME_CSTS_CFS)) { |
| 1774 |
ctrl->csts |= NVME_CSTS_CFS; |
| 1775 |
queue_work(nvmet_wq, &ctrl->fatal_err_work); |
| 1776 |
} |
| 1777 |
mutex_unlock(&ctrl->lock); |
| 1778 |
} |
| 1779 |
EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error); |
| 1780 |
|
| 1781 |
ssize_t nvmet_ctrl_host_traddr(struct nvmet_ctrl *ctrl, |
| 1782 |
char *traddr, size_t traddr_len) |
| 1783 |
{ |
| 1784 |
if (!ctrl->ops->host_traddr) |
| 1785 |
return -EOPNOTSUPP; |
| 1786 |
return ctrl->ops->host_traddr(ctrl, traddr, traddr_len); |
| 1787 |
} |
| 1788 |
|
| 1789 |
static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port, |
| 1790 |
const char *subsysnqn) |
| 1791 |
{ |
| 1792 |
struct nvmet_subsys_link *p; |
| 1793 |
|
| 1794 |
if (!port) |
| 1795 |
return NULL; |
| 1796 |
|
| 1797 |
if (!strcmp(NVME_DISC_SUBSYS_NAME, subsysnqn)) { |
| 1798 |
if (!kref_get_unless_zero(&nvmet_disc_subsys->ref)) |
| 1799 |
return NULL; |
| 1800 |
return nvmet_disc_subsys; |
| 1801 |
} |
| 1802 |
|
| 1803 |
down_read(&nvmet_config_sem); |
| 1804 |
if (!strncmp(nvmet_disc_subsys->subsysnqn, subsysnqn, |
| 1805 |
NVMF_NQN_SIZE)) { |
| 1806 |
if (kref_get_unless_zero(&nvmet_disc_subsys->ref)) { |
| 1807 |
up_read(&nvmet_config_sem); |
| 1808 |
return nvmet_disc_subsys; |
| 1809 |
} |
| 1810 |
} |
| 1811 |
list_for_each_entry(p, &port->subsystems, entry) { |
| 1812 |
if (!strncmp(p->subsys->subsysnqn, subsysnqn, |
| 1813 |
NVMF_NQN_SIZE)) { |
| 1814 |
if (!kref_get_unless_zero(&p->subsys->ref)) |
| 1815 |
break; |
| 1816 |
up_read(&nvmet_config_sem); |
| 1817 |
return p->subsys; |
| 1818 |
} |
| 1819 |
} |
| 1820 |
up_read(&nvmet_config_sem); |
| 1821 |
return NULL; |
| 1822 |
} |
| 1823 |
|
| 1824 |
struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, |
| 1825 |
enum nvme_subsys_type type) |
| 1826 |
{ |
| 1827 |
struct nvmet_subsys *subsys; |
| 1828 |
char serial[NVMET_SN_MAX_SIZE / 2]; |
| 1829 |
int ret; |
| 1830 |
|
| 1831 |
subsys = kzalloc_obj(*subsys); |
| 1832 |
if (!subsys) |
| 1833 |
return ERR_PTR(-ENOMEM); |
| 1834 |
|
| 1835 |
subsys->ver = NVMET_DEFAULT_VS; |
| 1836 |
/* generate a random serial number as our controllers are ephemeral: */ |
| 1837 |
get_random_bytes(&serial, sizeof(serial)); |
| 1838 |
bin2hex(subsys->serial, &serial, sizeof(serial)); |
| 1839 |
|
| 1840 |
subsys->model_number = kstrdup(NVMET_DEFAULT_CTRL_MODEL, GFP_KERNEL); |
| 1841 |
if (!subsys->model_number) { |
| 1842 |
ret = -ENOMEM; |
| 1843 |
goto free_subsys; |
| 1844 |
} |
| 1845 |
|
| 1846 |
subsys->ieee_oui = 0; |
| 1847 |
|
| 1848 |
subsys->firmware_rev = kstrndup(UTS_RELEASE, NVMET_FR_MAX_SIZE, GFP_KERNEL); |
| 1849 |
if (!subsys->firmware_rev) { |
| 1850 |
ret = -ENOMEM; |
| 1851 |
goto free_mn; |
| 1852 |
} |
| 1853 |
|
| 1854 |
switch (type) { |
| 1855 |
case NVME_NQN_NVME: |
| 1856 |
subsys->max_qid = NVMET_NR_QUEUES; |
| 1857 |
break; |
| 1858 |
case NVME_NQN_DISC: |
| 1859 |
case NVME_NQN_CURR: |
| 1860 |
subsys->max_qid = 0; |
| 1861 |
break; |
| 1862 |
default: |
| 1863 |
pr_err("%s: Unknown Subsystem type - %d\n", __func__, type); |
| 1864 |
ret = -EINVAL; |
| 1865 |
goto free_fr; |
| 1866 |
} |
| 1867 |
subsys->type = type; |
| 1868 |
subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE, |
| 1869 |
GFP_KERNEL); |
| 1870 |
if (!subsys->subsysnqn) { |
| 1871 |
ret = -ENOMEM; |
| 1872 |
goto free_fr; |
| 1873 |
} |
| 1874 |
subsys->cntlid_min = NVME_CNTLID_MIN; |
| 1875 |
subsys->cntlid_max = NVME_CNTLID_MAX; |
| 1876 |
kref_init(&subsys->ref); |
| 1877 |
|
| 1878 |
mutex_init(&subsys->lock); |
| 1879 |
xa_init(&subsys->namespaces); |
| 1880 |
INIT_LIST_HEAD(&subsys->ctrls); |
| 1881 |
INIT_LIST_HEAD(&subsys->hosts); |
| 1882 |
|
| 1883 |
ret = nvmet_debugfs_subsys_setup(subsys); |
| 1884 |
if (ret) |
| 1885 |
goto free_subsysnqn; |
| 1886 |
|
| 1887 |
return subsys; |
| 1888 |
|
| 1889 |
free_subsysnqn: |
| 1890 |
kfree(subsys->subsysnqn); |
| 1891 |
free_fr: |
| 1892 |
kfree(subsys->firmware_rev); |
| 1893 |
free_mn: |
| 1894 |
kfree(subsys->model_number); |
| 1895 |
free_subsys: |
| 1896 |
kfree(subsys); |
| 1897 |
return ERR_PTR(ret); |
| 1898 |
} |
| 1899 |
|
| 1900 |
static void nvmet_subsys_free(struct kref *ref) |
| 1901 |
{ |
| 1902 |
struct nvmet_subsys *subsys = |
| 1903 |
container_of(ref, struct nvmet_subsys, ref); |
| 1904 |
|
| 1905 |
WARN_ON_ONCE(!list_empty(&subsys->ctrls)); |
| 1906 |
WARN_ON_ONCE(!list_empty(&subsys->hosts)); |
| 1907 |
WARN_ON_ONCE(!xa_empty(&subsys->namespaces)); |
| 1908 |
|
| 1909 |
nvmet_debugfs_subsys_free(subsys); |
| 1910 |
|
| 1911 |
xa_destroy(&subsys->namespaces); |
| 1912 |
nvmet_passthru_subsys_free(subsys); |
| 1913 |
|
| 1914 |
kfree(subsys->subsysnqn); |
| 1915 |
kfree(subsys->model_number); |
| 1916 |
kfree(subsys->firmware_rev); |
| 1917 |
kfree(subsys); |
| 1918 |
} |
| 1919 |
|
| 1920 |
void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys) |
| 1921 |
{ |
| 1922 |
struct nvmet_ctrl *ctrl; |
| 1923 |
|
| 1924 |
mutex_lock(&subsys->lock); |
| 1925 |
list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) |
| 1926 |
ctrl->ops->delete_ctrl(ctrl); |
| 1927 |
mutex_unlock(&subsys->lock); |
| 1928 |
} |
| 1929 |
|
| 1930 |
void nvmet_subsys_put(struct nvmet_subsys *subsys) |
| 1931 |
{ |
| 1932 |
kref_put(&subsys->ref, nvmet_subsys_free); |
| 1933 |
} |
| 1934 |
|
| 1935 |
static int __init nvmet_init(void) |
| 1936 |
{ |
| 1937 |
int error = -ENOMEM; |
| 1938 |
|
| 1939 |
nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1; |
| 1940 |
|
| 1941 |
nvmet_bvec_cache = kmem_cache_create("nvmet-bvec", |
| 1942 |
NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec), 0, |
| 1943 |
SLAB_HWCACHE_ALIGN, NULL); |
| 1944 |
if (!nvmet_bvec_cache) |
| 1945 |
return -ENOMEM; |
| 1946 |
|
| 1947 |
zbd_wq = alloc_workqueue("nvmet-zbd-wq", WQ_MEM_RECLAIM, 0); |
| 1948 |
if (!zbd_wq) |
| 1949 |
goto out_destroy_bvec_cache; |
| 1950 |
|
| 1951 |
buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq", |
| 1952 |
WQ_MEM_RECLAIM, 0); |
| 1953 |
if (!buffered_io_wq) |
| 1954 |
goto out_free_zbd_work_queue; |
| 1955 |
|
| 1956 |
nvmet_wq = alloc_workqueue("nvmet-wq", |
| 1957 |
WQ_MEM_RECLAIM | WQ_UNBOUND | WQ_SYSFS, 0); |
| 1958 |
if (!nvmet_wq) |
| 1959 |
goto out_free_buffered_work_queue; |
| 1960 |
|
| 1961 |
nvmet_aen_wq = alloc_workqueue("nvmet-aen-wq", |
| 1962 |
WQ_MEM_RECLAIM | WQ_UNBOUND, 0); |
| 1963 |
if (!nvmet_aen_wq) |
| 1964 |
goto out_free_nvmet_work_queue; |
| 1965 |
|
| 1966 |
error = nvmet_init_debugfs(); |
| 1967 |
if (error) |
| 1968 |
goto out_free_nvmet_aen_work_queue; |
| 1969 |
|
| 1970 |
error = nvmet_init_discovery(); |
| 1971 |
if (error) |
| 1972 |
goto out_exit_debugfs; |
| 1973 |
|
| 1974 |
error = nvmet_init_configfs(); |
| 1975 |
if (error) |
| 1976 |
goto out_exit_discovery; |
| 1977 |
|
| 1978 |
return 0; |
| 1979 |
|
| 1980 |
out_exit_discovery: |
| 1981 |
nvmet_exit_discovery(); |
| 1982 |
out_exit_debugfs: |
| 1983 |
nvmet_exit_debugfs(); |
| 1984 |
out_free_nvmet_aen_work_queue: |
| 1985 |
destroy_workqueue(nvmet_aen_wq); |
| 1986 |
out_free_nvmet_work_queue: |
| 1987 |
destroy_workqueue(nvmet_wq); |
| 1988 |
out_free_buffered_work_queue: |
| 1989 |
destroy_workqueue(buffered_io_wq); |
| 1990 |
out_free_zbd_work_queue: |
| 1991 |
destroy_workqueue(zbd_wq); |
| 1992 |
out_destroy_bvec_cache: |
| 1993 |
kmem_cache_destroy(nvmet_bvec_cache); |
| 1994 |
return error; |
| 1995 |
} |
| 1996 |
|
| 1997 |
static void __exit nvmet_exit(void) |
| 1998 |
{ |
| 1999 |
nvmet_exit_configfs(); |
| 2000 |
nvmet_exit_discovery(); |
| 2001 |
nvmet_exit_debugfs(); |
| 2002 |
ida_destroy(&cntlid_ida); |
| 2003 |
destroy_workqueue(nvmet_aen_wq); |
| 2004 |
destroy_workqueue(nvmet_wq); |
| 2005 |
destroy_workqueue(buffered_io_wq); |
| 2006 |
destroy_workqueue(zbd_wq); |
| 2007 |
kmem_cache_destroy(nvmet_bvec_cache); |
| 2008 |
|
| 2009 |
BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024); |
| 2010 |
BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024); |
| 2011 |
} |
| 2012 |
|
| 2013 |
module_init(nvmet_init); |
| 2014 |
module_exit(nvmet_exit); |
| 2015 |
|
| 2016 |
MODULE_DESCRIPTION("NVMe target core framework"); |
| 2017 |
MODULE_LICENSE("GPL v2"); |