1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * NVMe over Fabrics TCP host. 4 * Copyright (c) 2018 Lightbits Labs. All rights reserved. 5 */ 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 #include <linux/module.h> 8 #include <linux/init.h> 9 #include <linux/slab.h> 10 #include <linux/err.h> 11 #include <linux/crc32.h> 12 #include <linux/nvme-tcp.h> 13 #include <linux/nvme-keyring.h> 14 #include <net/sock.h> 15 #include <net/tcp.h> 16 #include <net/tls.h> 17 #include <net/tls_prot.h> 18 #include <net/handshake.h> 19 #include <linux/blk-mq.h> 20 #include <net/busy_poll.h> 21 #include <trace/events/sock.h> 22 23 #include "nvme.h" 24 #include "fabrics.h" 25 26 struct nvme_tcp_queue; 27 28 /* Define the socket priority to use for connections were it is desirable 29 * that the NIC consider performing optimized packet processing or filtering. 30 * A non-zero value being sufficient to indicate general consideration of any 31 * possible optimization. Making it a module param allows for alternative 32 * values that may be unique for some NIC implementations. 33 */ 34 static int so_priority; 35 module_param(so_priority, int, 0644); 36 MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority"); 37 38 /* 39 * Use the unbound workqueue for nvme_tcp_wq, then we can set the cpu affinity 40 * from sysfs. 41 */ 42 static bool wq_unbound; 43 module_param(wq_unbound, bool, 0644); 44 MODULE_PARM_DESC(wq_unbound, "Use unbound workqueue for nvme-tcp IO context (default false)"); 45 46 /* 47 * TLS handshake timeout 48 */ 49 static int tls_handshake_timeout = 10; 50 #ifdef CONFIG_NVME_TCP_TLS 51 module_param(tls_handshake_timeout, int, 0644); 52 MODULE_PARM_DESC(tls_handshake_timeout, 53 "nvme TLS handshake timeout in seconds (default 10)"); 54 #endif 55 56 static atomic_t nvme_tcp_cpu_queues[NR_CPUS]; 57 58 #ifdef CONFIG_DEBUG_LOCK_ALLOC 59 /* lockdep can detect a circular dependency of the form 60 * sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock 61 * because dependencies are tracked for both nvme-tcp and user contexts. Using 62 * a separate class prevents lockdep from conflating nvme-tcp socket use with 63 * user-space socket API use. 64 */ 65 static struct lock_class_key nvme_tcp_sk_key[2]; 66 static struct lock_class_key nvme_tcp_slock_key[2]; 67 68 static void nvme_tcp_reclassify_socket(struct socket *sock) 69 { 70 struct sock *sk = sock->sk; 71 72 if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) 73 return; 74 75 switch (sk->sk_family) { 76 case AF_INET: 77 sock_lock_init_class_and_name(sk, "slock-AF_INET-NVME", 78 &nvme_tcp_slock_key[0], 79 "sk_lock-AF_INET-NVME", 80 &nvme_tcp_sk_key[0]); 81 break; 82 case AF_INET6: 83 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NVME", 84 &nvme_tcp_slock_key[1], 85 "sk_lock-AF_INET6-NVME", 86 &nvme_tcp_sk_key[1]); 87 break; 88 default: 89 WARN_ON_ONCE(1); 90 } 91 } 92 #else 93 static void nvme_tcp_reclassify_socket(struct socket *sock) { } 94 #endif 95 96 enum nvme_tcp_send_state { 97 NVME_TCP_SEND_CMD_PDU = 0, 98 NVME_TCP_SEND_H2C_PDU, 99 NVME_TCP_SEND_DATA, 100 NVME_TCP_SEND_DDGST, 101 }; 102 103 struct nvme_tcp_request { 104 struct nvme_request req; 105 void *pdu; 106 struct nvme_tcp_queue *queue; 107 u32 data_len; 108 u32 pdu_len; 109 u32 pdu_sent; 110 u32 h2cdata_left; 111 u32 h2cdata_offset; 112 u16 ttag; 113 __le16 status; 114 struct list_head entry; 115 struct llist_node lentry; 116 __le32 ddgst; 117 118 struct bio *curr_bio; 119 struct iov_iter iter; 120 121 /* send state */ 122 size_t offset; 123 size_t data_sent; 124 enum nvme_tcp_send_state state; 125 }; 126 127 enum nvme_tcp_queue_flags { 128 NVME_TCP_Q_ALLOCATED = 0, 129 NVME_TCP_Q_LIVE = 1, 130 NVME_TCP_Q_POLLING = 2, 131 NVME_TCP_Q_IO_CPU_SET = 3, 132 }; 133 134 enum nvme_tcp_recv_state { 135 NVME_TCP_RECV_PDU = 0, 136 NVME_TCP_RECV_DATA, 137 NVME_TCP_RECV_DDGST, 138 }; 139 140 struct nvme_tcp_ctrl; 141 struct nvme_tcp_queue { 142 struct socket *sock; 143 struct work_struct io_work; 144 int io_cpu; 145 146 struct mutex queue_lock; 147 struct mutex send_mutex; 148 struct llist_head req_list; 149 struct list_head send_list; 150 151 /* recv state */ 152 void *pdu; 153 int pdu_remaining; 154 int pdu_offset; 155 size_t data_remaining; 156 size_t ddgst_remaining; 157 unsigned int nr_cqe; 158 159 /* send state */ 160 struct nvme_tcp_request *request; 161 162 u32 maxh2cdata; 163 size_t cmnd_capsule_len; 164 struct nvme_tcp_ctrl *ctrl; 165 unsigned long flags; 166 bool rd_enabled; 167 168 bool hdr_digest; 169 bool data_digest; 170 bool tls_enabled; 171 u32 rcv_crc; 172 u32 snd_crc; 173 __le32 exp_ddgst; 174 __le32 recv_ddgst; 175 struct completion tls_complete; 176 int tls_err; 177 struct page_frag_cache pf_cache; 178 179 void (*state_change)(struct sock *); 180 void (*data_ready)(struct sock *); 181 void (*write_space)(struct sock *); 182 }; 183 184 struct nvme_tcp_ctrl { 185 /* read only in the hot path */ 186 struct nvme_tcp_queue *queues; 187 struct blk_mq_tag_set tag_set; 188 189 /* other member variables */ 190 struct list_head list; 191 struct blk_mq_tag_set admin_tag_set; 192 struct sockaddr_storage addr; 193 struct sockaddr_storage src_addr; 194 struct nvme_ctrl ctrl; 195 196 struct work_struct err_work; 197 struct delayed_work connect_work; 198 struct nvme_tcp_request async_req; 199 u32 io_queues[HCTX_MAX_TYPES]; 200 }; 201 202 static LIST_HEAD(nvme_tcp_ctrl_list); 203 static DEFINE_MUTEX(nvme_tcp_ctrl_mutex); 204 static struct workqueue_struct *nvme_tcp_wq; 205 static const struct blk_mq_ops nvme_tcp_mq_ops; 206 static const struct blk_mq_ops nvme_tcp_admin_mq_ops; 207 static int nvme_tcp_try_send(struct nvme_tcp_queue *queue); 208 209 static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl) 210 { 211 return container_of(ctrl, struct nvme_tcp_ctrl, ctrl); 212 } 213 214 static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue) 215 { 216 return queue - queue->ctrl->queues; 217 } 218 219 static inline bool nvme_tcp_recv_pdu_supported(enum nvme_tcp_pdu_type type) 220 { 221 switch (type) { 222 case nvme_tcp_c2h_term: 223 case nvme_tcp_c2h_data: 224 case nvme_tcp_r2t: 225 case nvme_tcp_rsp: 226 return true; 227 default: 228 return false; 229 } 230 } 231 232 /* 233 * Check if the queue is TLS encrypted 234 */ 235 static inline bool nvme_tcp_queue_tls(struct nvme_tcp_queue *queue) 236 { 237 if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) 238 return 0; 239 240 return queue->tls_enabled; 241 } 242 243 /* 244 * Check if TLS is configured for the controller. 245 */ 246 static inline bool nvme_tcp_tls_configured(struct nvme_ctrl *ctrl) 247 { 248 if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) 249 return 0; 250 251 return ctrl->opts->tls || ctrl->opts->concat; 252 } 253 254 static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue) 255 { 256 u32 queue_idx = nvme_tcp_queue_id(queue); 257 258 if (queue_idx == 0) 259 return queue->ctrl->admin_tag_set.tags[queue_idx]; 260 return queue->ctrl->tag_set.tags[queue_idx - 1]; 261 } 262 263 static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue) 264 { 265 return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0; 266 } 267 268 static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue) 269 { 270 return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0; 271 } 272 273 static inline void *nvme_tcp_req_cmd_pdu(struct nvme_tcp_request *req) 274 { 275 return req->pdu; 276 } 277 278 static inline void *nvme_tcp_req_data_pdu(struct nvme_tcp_request *req) 279 { 280 /* use the pdu space in the back for the data pdu */ 281 return req->pdu + sizeof(struct nvme_tcp_cmd_pdu) - 282 sizeof(struct nvme_tcp_data_pdu); 283 } 284 285 static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_request *req) 286 { 287 if (nvme_is_fabrics(req->req.cmd)) 288 return NVME_TCP_ADMIN_CCSZ; 289 return req->queue->cmnd_capsule_len - sizeof(struct nvme_command); 290 } 291 292 static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req) 293 { 294 return req == &req->queue->ctrl->async_req; 295 } 296 297 static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req) 298 { 299 struct request *rq; 300 301 if (unlikely(nvme_tcp_async_req(req))) 302 return false; /* async events don't have a request */ 303 304 rq = blk_mq_rq_from_pdu(req); 305 306 return rq_data_dir(rq) == WRITE && req->data_len && 307 req->data_len <= nvme_tcp_inline_data_size(req); 308 } 309 310 static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req) 311 { 312 return req->iter.bvec->bv_page; 313 } 314 315 static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req) 316 { 317 return req->iter.bvec->bv_offset + req->iter.iov_offset; 318 } 319 320 static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req) 321 { 322 return min_t(size_t, iov_iter_single_seg_count(&req->iter), 323 req->pdu_len - req->pdu_sent); 324 } 325 326 static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req) 327 { 328 return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ? 329 req->pdu_len - req->pdu_sent : 0; 330 } 331 332 static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req, 333 int len) 334 { 335 return nvme_tcp_pdu_data_left(req) <= len; 336 } 337 338 static void nvme_tcp_init_iter(struct nvme_tcp_request *req, 339 unsigned int dir) 340 { 341 struct request *rq = blk_mq_rq_from_pdu(req); 342 struct bio_vec *vec; 343 unsigned int size; 344 int nr_bvec; 345 size_t offset; 346 347 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) { 348 vec = &rq->special_vec; 349 nr_bvec = 1; 350 size = blk_rq_payload_bytes(rq); 351 offset = 0; 352 } else { 353 struct bio *bio = req->curr_bio; 354 struct bvec_iter bi; 355 struct bio_vec bv; 356 357 vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); 358 nr_bvec = 0; 359 bio_for_each_bvec(bv, bio, bi) { 360 nr_bvec++; 361 } 362 size = bio->bi_iter.bi_size; 363 offset = bio->bi_iter.bi_bvec_done; 364 } 365 366 iov_iter_bvec(&req->iter, dir, vec, nr_bvec, size); 367 req->iter.iov_offset = offset; 368 } 369 370 static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req, 371 int len) 372 { 373 req->data_sent += len; 374 req->pdu_sent += len; 375 iov_iter_advance(&req->iter, len); 376 if (!iov_iter_count(&req->iter) && 377 req->data_sent < req->data_len) { 378 req->curr_bio = req->curr_bio->bi_next; 379 nvme_tcp_init_iter(req, ITER_SOURCE); 380 } 381 } 382 383 static inline void nvme_tcp_send_all(struct nvme_tcp_queue *queue) 384 { 385 int ret; 386 387 /* drain the send queue as much as we can... */ 388 do { 389 ret = nvme_tcp_try_send(queue); 390 } while (ret > 0); 391 } 392 393 static inline bool nvme_tcp_queue_has_pending(struct nvme_tcp_queue *queue) 394 { 395 return !list_empty(&queue->send_list) || 396 !llist_empty(&queue->req_list); 397 } 398 399 static inline bool nvme_tcp_queue_more(struct nvme_tcp_queue *queue) 400 { 401 return !nvme_tcp_queue_tls(queue) && 402 nvme_tcp_queue_has_pending(queue); 403 } 404 405 static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req, 406 bool last) 407 { 408 struct nvme_tcp_queue *queue = req->queue; 409 bool empty; 410 411 empty = llist_add(&req->lentry, &queue->req_list) && 412 list_empty(&queue->send_list) && !queue->request; 413 414 /* 415 * if we're the first on the send_list and we can try to send 416 * directly, otherwise queue io_work. Also, only do that if we 417 * are on the same cpu, so we don't introduce contention. 418 */ 419 if (queue->io_cpu == raw_smp_processor_id() && 420 empty && mutex_trylock(&queue->send_mutex)) { 421 nvme_tcp_send_all(queue); 422 mutex_unlock(&queue->send_mutex); 423 } 424 425 if (last && nvme_tcp_queue_has_pending(queue)) 426 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); 427 } 428 429 static void nvme_tcp_process_req_list(struct nvme_tcp_queue *queue) 430 { 431 struct nvme_tcp_request *req; 432 struct llist_node *node; 433 434 for (node = llist_del_all(&queue->req_list); node; node = node->next) { 435 req = llist_entry(node, struct nvme_tcp_request, lentry); 436 list_add(&req->entry, &queue->send_list); 437 } 438 } 439 440 static inline struct nvme_tcp_request * 441 nvme_tcp_fetch_request(struct nvme_tcp_queue *queue) 442 { 443 struct nvme_tcp_request *req; 444 445 req = list_first_entry_or_null(&queue->send_list, 446 struct nvme_tcp_request, entry); 447 if (!req) { 448 nvme_tcp_process_req_list(queue); 449 req = list_first_entry_or_null(&queue->send_list, 450 struct nvme_tcp_request, entry); 451 if (unlikely(!req)) 452 return NULL; 453 } 454 455 list_del_init(&req->entry); 456 init_llist_node(&req->lentry); 457 return req; 458 } 459 460 #define NVME_TCP_CRC_SEED (~0) 461 462 static inline void nvme_tcp_ddgst_update(u32 *crcp, 463 struct page *page, size_t off, size_t len) 464 { 465 page += off / PAGE_SIZE; 466 off %= PAGE_SIZE; 467 while (len) { 468 const void *vaddr = kmap_local_page(page); 469 size_t n = min(len, (size_t)PAGE_SIZE - off); 470 471 *crcp = crc32c(*crcp, vaddr + off, n); 472 kunmap_local(vaddr); 473 page++; 474 off = 0; 475 len -= n; 476 } 477 } 478 479 static inline __le32 nvme_tcp_ddgst_final(u32 crc) 480 { 481 return cpu_to_le32(~crc); 482 } 483 484 static inline __le32 nvme_tcp_hdgst(const void *pdu, size_t len) 485 { 486 return cpu_to_le32(~crc32c(NVME_TCP_CRC_SEED, pdu, len)); 487 } 488 489 static inline void nvme_tcp_set_hdgst(void *pdu, size_t len) 490 { 491 *(__le32 *)(pdu + len) = nvme_tcp_hdgst(pdu, len); 492 } 493 494 static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue, 495 void *pdu, size_t pdu_len) 496 { 497 struct nvme_tcp_hdr *hdr = pdu; 498 __le32 recv_digest; 499 __le32 exp_digest; 500 501 if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) { 502 dev_err(queue->ctrl->ctrl.device, 503 "queue %d: header digest flag is cleared\n", 504 nvme_tcp_queue_id(queue)); 505 return -EPROTO; 506 } 507 508 recv_digest = *(__le32 *)(pdu + hdr->hlen); 509 exp_digest = nvme_tcp_hdgst(pdu, pdu_len); 510 if (recv_digest != exp_digest) { 511 dev_err(queue->ctrl->ctrl.device, 512 "header digest error: recv %#x expected %#x\n", 513 le32_to_cpu(recv_digest), le32_to_cpu(exp_digest)); 514 return -EIO; 515 } 516 517 return 0; 518 } 519 520 static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu) 521 { 522 struct nvme_tcp_hdr *hdr = pdu; 523 u8 digest_len = nvme_tcp_hdgst_len(queue); 524 u32 len; 525 526 len = le32_to_cpu(hdr->plen) - hdr->hlen - 527 ((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0); 528 529 if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) { 530 dev_err(queue->ctrl->ctrl.device, 531 "queue %d: data digest flag is cleared\n", 532 nvme_tcp_queue_id(queue)); 533 return -EPROTO; 534 } 535 queue->rcv_crc = NVME_TCP_CRC_SEED; 536 537 return 0; 538 } 539 540 static void nvme_tcp_exit_request(struct blk_mq_tag_set *set, 541 struct request *rq, unsigned int hctx_idx) 542 { 543 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 544 545 page_frag_free(req->pdu); 546 } 547 548 static int nvme_tcp_init_request(struct blk_mq_tag_set *set, 549 struct request *rq, unsigned int hctx_idx, 550 unsigned int numa_node) 551 { 552 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data); 553 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 554 struct nvme_tcp_cmd_pdu *pdu; 555 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0; 556 struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx]; 557 u8 hdgst = nvme_tcp_hdgst_len(queue); 558 559 req->pdu = page_frag_alloc(&queue->pf_cache, 560 sizeof(struct nvme_tcp_cmd_pdu) + hdgst, 561 GFP_KERNEL | __GFP_ZERO); 562 if (!req->pdu) 563 return -ENOMEM; 564 565 pdu = req->pdu; 566 req->queue = queue; 567 nvme_req(rq)->ctrl = &ctrl->ctrl; 568 nvme_req(rq)->cmd = &pdu->cmd; 569 init_llist_node(&req->lentry); 570 INIT_LIST_HEAD(&req->entry); 571 572 return 0; 573 } 574 575 static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, 576 unsigned int hctx_idx) 577 { 578 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(data); 579 struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1]; 580 581 hctx->driver_data = queue; 582 return 0; 583 } 584 585 static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, 586 unsigned int hctx_idx) 587 { 588 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(data); 589 struct nvme_tcp_queue *queue = &ctrl->queues[0]; 590 591 hctx->driver_data = queue; 592 return 0; 593 } 594 595 static enum nvme_tcp_recv_state 596 nvme_tcp_recv_state(struct nvme_tcp_queue *queue) 597 { 598 return (queue->pdu_remaining) ? NVME_TCP_RECV_PDU : 599 (queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST : 600 NVME_TCP_RECV_DATA; 601 } 602 603 static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue) 604 { 605 queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) + 606 nvme_tcp_hdgst_len(queue); 607 queue->pdu_offset = 0; 608 queue->data_remaining = -1; 609 queue->ddgst_remaining = 0; 610 } 611 612 static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl) 613 { 614 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING)) 615 return; 616 617 dev_warn(ctrl->device, "starting error recovery\n"); 618 queue_work(nvme_reset_wq, &to_tcp_ctrl(ctrl)->err_work); 619 } 620 621 static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue, 622 struct nvme_completion *cqe) 623 { 624 struct nvme_tcp_request *req; 625 struct request *rq; 626 627 rq = nvme_find_rq(nvme_tcp_tagset(queue), cqe->command_id); 628 if (!rq) { 629 dev_err(queue->ctrl->ctrl.device, 630 "got bad cqe.command_id %#x on queue %d\n", 631 cqe->command_id, nvme_tcp_queue_id(queue)); 632 nvme_tcp_error_recovery(&queue->ctrl->ctrl); 633 return -EINVAL; 634 } 635 636 req = blk_mq_rq_to_pdu(rq); 637 if (req->status == cpu_to_le16(NVME_SC_SUCCESS)) 638 req->status = cqe->status; 639 640 if (!nvme_try_complete_req(rq, req->status, cqe->result)) 641 nvme_complete_rq(rq); 642 queue->nr_cqe++; 643 644 return 0; 645 } 646 647 static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue, 648 struct nvme_tcp_data_pdu *pdu) 649 { 650 struct request *rq; 651 652 rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id); 653 if (!rq) { 654 dev_err(queue->ctrl->ctrl.device, 655 "got bad c2hdata.command_id %#x on queue %d\n", 656 pdu->command_id, nvme_tcp_queue_id(queue)); 657 return -ENOENT; 658 } 659 660 if (!blk_rq_payload_bytes(rq)) { 661 dev_err(queue->ctrl->ctrl.device, 662 "queue %d tag %#x unexpected data\n", 663 nvme_tcp_queue_id(queue), rq->tag); 664 return -EIO; 665 } 666 667 queue->data_remaining = le32_to_cpu(pdu->data_length); 668 669 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS && 670 unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) { 671 dev_err(queue->ctrl->ctrl.device, 672 "queue %d tag %#x SUCCESS set but not last PDU\n", 673 nvme_tcp_queue_id(queue), rq->tag); 674 nvme_tcp_error_recovery(&queue->ctrl->ctrl); 675 return -EPROTO; 676 } 677 678 return 0; 679 } 680 681 static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue, 682 struct nvme_tcp_rsp_pdu *pdu) 683 { 684 struct nvme_completion *cqe = &pdu->cqe; 685 int ret = 0; 686 687 /* 688 * AEN requests are special as they don't time out and can 689 * survive any kind of queue freeze and often don't respond to 690 * aborts. We don't even bother to allocate a struct request 691 * for them but rather special case them here. 692 */ 693 if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue), 694 cqe->command_id))) 695 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, 696 &cqe->result); 697 else 698 ret = nvme_tcp_process_nvme_cqe(queue, cqe); 699 700 return ret; 701 } 702 703 static void nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req) 704 { 705 struct nvme_tcp_data_pdu *data = nvme_tcp_req_data_pdu(req); 706 struct nvme_tcp_queue *queue = req->queue; 707 struct request *rq = blk_mq_rq_from_pdu(req); 708 u32 h2cdata_sent = req->pdu_len; 709 u8 hdgst = nvme_tcp_hdgst_len(queue); 710 u8 ddgst = nvme_tcp_ddgst_len(queue); 711 712 req->state = NVME_TCP_SEND_H2C_PDU; 713 req->offset = 0; 714 req->pdu_len = min(req->h2cdata_left, queue->maxh2cdata); 715 req->pdu_sent = 0; 716 req->h2cdata_left -= req->pdu_len; 717 req->h2cdata_offset += h2cdata_sent; 718 719 memset(data, 0, sizeof(*data)); 720 data->hdr.type = nvme_tcp_h2c_data; 721 if (!req->h2cdata_left) 722 data->hdr.flags = NVME_TCP_F_DATA_LAST; 723 if (queue->hdr_digest) 724 data->hdr.flags |= NVME_TCP_F_HDGST; 725 if (queue->data_digest) 726 data->hdr.flags |= NVME_TCP_F_DDGST; 727 data->hdr.hlen = sizeof(*data); 728 data->hdr.pdo = data->hdr.hlen + hdgst; 729 data->hdr.plen = 730 cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst); 731 data->ttag = req->ttag; 732 data->command_id = nvme_cid(rq); 733 data->data_offset = cpu_to_le32(req->h2cdata_offset); 734 data->data_length = cpu_to_le32(req->pdu_len); 735 } 736 737 static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue, 738 struct nvme_tcp_r2t_pdu *pdu) 739 { 740 struct nvme_tcp_request *req; 741 struct request *rq; 742 u32 r2t_length = le32_to_cpu(pdu->r2t_length); 743 u32 r2t_offset = le32_to_cpu(pdu->r2t_offset); 744 745 rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id); 746 if (!rq) { 747 dev_err(queue->ctrl->ctrl.device, 748 "got bad r2t.command_id %#x on queue %d\n", 749 pdu->command_id, nvme_tcp_queue_id(queue)); 750 return -ENOENT; 751 } 752 req = blk_mq_rq_to_pdu(rq); 753 754 if (unlikely(!r2t_length)) { 755 dev_err(queue->ctrl->ctrl.device, 756 "req %d r2t len is %u, probably a bug...\n", 757 rq->tag, r2t_length); 758 return -EPROTO; 759 } 760 761 if (unlikely(req->data_sent + r2t_length > req->data_len)) { 762 dev_err(queue->ctrl->ctrl.device, 763 "req %d r2t len %u exceeded data len %u (%zu sent)\n", 764 rq->tag, r2t_length, req->data_len, req->data_sent); 765 return -EPROTO; 766 } 767 768 if (unlikely(r2t_offset < req->data_sent)) { 769 dev_err(queue->ctrl->ctrl.device, 770 "req %d unexpected r2t offset %u (expected %zu)\n", 771 rq->tag, r2t_offset, req->data_sent); 772 return -EPROTO; 773 } 774 775 if (llist_on_list(&req->lentry) || 776 !list_empty(&req->entry)) { 777 dev_err(queue->ctrl->ctrl.device, 778 "req %d unexpected r2t while processing request\n", 779 rq->tag); 780 return -EPROTO; 781 } 782 783 req->pdu_len = 0; 784 req->h2cdata_left = r2t_length; 785 req->h2cdata_offset = r2t_offset; 786 req->ttag = pdu->ttag; 787 788 nvme_tcp_setup_h2c_data_pdu(req); 789 790 llist_add(&req->lentry, &queue->req_list); 791 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); 792 793 return 0; 794 } 795 796 static void nvme_tcp_handle_c2h_term(struct nvme_tcp_queue *queue, 797 struct nvme_tcp_term_pdu *pdu) 798 { 799 u16 fes; 800 const char *msg; 801 u32 plen = le32_to_cpu(pdu->hdr.plen); 802 803 static const char * const msg_table[] = { 804 [NVME_TCP_FES_INVALID_PDU_HDR] = "Invalid PDU Header Field", 805 [NVME_TCP_FES_PDU_SEQ_ERR] = "PDU Sequence Error", 806 [NVME_TCP_FES_HDR_DIGEST_ERR] = "Header Digest Error", 807 [NVME_TCP_FES_DATA_OUT_OF_RANGE] = "Data Transfer Out Of Range", 808 [NVME_TCP_FES_DATA_LIMIT_EXCEEDED] = "Data Transfer Limit Exceeded", 809 [NVME_TCP_FES_UNSUPPORTED_PARAM] = "Unsupported Parameter", 810 }; 811 812 if (plen < NVME_TCP_MIN_C2HTERM_PLEN || 813 plen > NVME_TCP_MAX_C2HTERM_PLEN) { 814 dev_err(queue->ctrl->ctrl.device, 815 "Received a malformed C2HTermReq PDU (plen = %u)\n", 816 plen); 817 return; 818 } 819 820 fes = le16_to_cpu(pdu->fes); 821 if (fes && fes < ARRAY_SIZE(msg_table)) 822 msg = msg_table[fes]; 823 else 824 msg = "Unknown"; 825 826 dev_err(queue->ctrl->ctrl.device, 827 "Received C2HTermReq (FES = %s)\n", msg); 828 } 829 830 static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb, 831 unsigned int *offset, size_t *len) 832 { 833 struct nvme_tcp_hdr *hdr; 834 char *pdu = queue->pdu; 835 size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining); 836 int ret; 837 838 ret = skb_copy_bits(skb, *offset, 839 &pdu[queue->pdu_offset], rcv_len); 840 if (unlikely(ret)) 841 return ret; 842 843 queue->pdu_remaining -= rcv_len; 844 queue->pdu_offset += rcv_len; 845 *offset += rcv_len; 846 *len -= rcv_len; 847 if (queue->pdu_remaining) 848 return 0; 849 850 hdr = queue->pdu; 851 if (unlikely(hdr->hlen != sizeof(struct nvme_tcp_rsp_pdu))) { 852 if (!nvme_tcp_recv_pdu_supported(hdr->type)) 853 goto unsupported_pdu; 854 855 dev_err(queue->ctrl->ctrl.device, 856 "pdu type %d has unexpected header length (%d)\n", 857 hdr->type, hdr->hlen); 858 return -EPROTO; 859 } 860 861 if (unlikely(hdr->type == nvme_tcp_c2h_term)) { 862 /* 863 * C2HTermReq never includes Header or Data digests. 864 * Skip the checks. 865 */ 866 nvme_tcp_handle_c2h_term(queue, (void *)queue->pdu); 867 return -EINVAL; 868 } 869 870 if (queue->hdr_digest) { 871 ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen); 872 if (unlikely(ret)) 873 return ret; 874 } 875 876 877 if (queue->data_digest) { 878 ret = nvme_tcp_check_ddgst(queue, queue->pdu); 879 if (unlikely(ret)) 880 return ret; 881 } 882 883 switch (hdr->type) { 884 case nvme_tcp_c2h_data: 885 return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu); 886 case nvme_tcp_rsp: 887 nvme_tcp_init_recv_ctx(queue); 888 return nvme_tcp_handle_comp(queue, (void *)queue->pdu); 889 case nvme_tcp_r2t: 890 nvme_tcp_init_recv_ctx(queue); 891 return nvme_tcp_handle_r2t(queue, (void *)queue->pdu); 892 default: 893 goto unsupported_pdu; 894 } 895 896 unsupported_pdu: 897 dev_err(queue->ctrl->ctrl.device, 898 "unsupported pdu type (%d)\n", hdr->type); 899 return -EINVAL; 900 } 901 902 static inline void nvme_tcp_end_request(struct request *rq, u16 status) 903 { 904 union nvme_result res = {}; 905 906 if (!nvme_try_complete_req(rq, cpu_to_le16(status << 1), res)) 907 nvme_complete_rq(rq); 908 } 909 910 static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb, 911 unsigned int *offset, size_t *len) 912 { 913 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu; 914 struct request *rq = 915 nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id); 916 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 917 918 while (true) { 919 int recv_len, ret; 920 921 recv_len = min_t(size_t, *len, queue->data_remaining); 922 if (!recv_len) 923 break; 924 925 if (!iov_iter_count(&req->iter)) { 926 req->curr_bio = req->curr_bio->bi_next; 927 928 /* 929 * If we don`t have any bios it means that controller 930 * sent more data than we requested, hence error 931 */ 932 if (!req->curr_bio) { 933 dev_err(queue->ctrl->ctrl.device, 934 "queue %d no space in request %#x", 935 nvme_tcp_queue_id(queue), rq->tag); 936 nvme_tcp_init_recv_ctx(queue); 937 return -EIO; 938 } 939 nvme_tcp_init_iter(req, ITER_DEST); 940 } 941 942 /* we can read only from what is left in this bio */ 943 recv_len = min_t(size_t, recv_len, 944 iov_iter_count(&req->iter)); 945 946 if (queue->data_digest) 947 ret = skb_copy_and_crc32c_datagram_iter(skb, *offset, 948 &req->iter, recv_len, &queue->rcv_crc); 949 else 950 ret = skb_copy_datagram_iter(skb, *offset, 951 &req->iter, recv_len); 952 if (ret) { 953 dev_err(queue->ctrl->ctrl.device, 954 "queue %d failed to copy request %#x data", 955 nvme_tcp_queue_id(queue), rq->tag); 956 return ret; 957 } 958 959 *len -= recv_len; 960 *offset += recv_len; 961 queue->data_remaining -= recv_len; 962 } 963 964 if (!queue->data_remaining) { 965 if (queue->data_digest) { 966 queue->exp_ddgst = nvme_tcp_ddgst_final(queue->rcv_crc); 967 queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH; 968 } else { 969 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) { 970 nvme_tcp_end_request(rq, 971 le16_to_cpu(req->status)); 972 queue->nr_cqe++; 973 } 974 nvme_tcp_init_recv_ctx(queue); 975 } 976 } 977 978 return 0; 979 } 980 981 static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue, 982 struct sk_buff *skb, unsigned int *offset, size_t *len) 983 { 984 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu; 985 char *ddgst = (char *)&queue->recv_ddgst; 986 size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining); 987 off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining; 988 int ret; 989 990 ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len); 991 if (unlikely(ret)) 992 return ret; 993 994 queue->ddgst_remaining -= recv_len; 995 *offset += recv_len; 996 *len -= recv_len; 997 if (queue->ddgst_remaining) 998 return 0; 999 1000 if (queue->recv_ddgst != queue->exp_ddgst) { 1001 struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue), 1002 pdu->command_id); 1003 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 1004 1005 req->status = cpu_to_le16(NVME_SC_DATA_XFER_ERROR); 1006 1007 dev_err(queue->ctrl->ctrl.device, 1008 "data digest error: recv %#x expected %#x\n", 1009 le32_to_cpu(queue->recv_ddgst), 1010 le32_to_cpu(queue->exp_ddgst)); 1011 } 1012 1013 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) { 1014 struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue), 1015 pdu->command_id); 1016 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 1017 1018 nvme_tcp_end_request(rq, le16_to_cpu(req->status)); 1019 queue->nr_cqe++; 1020 } 1021 1022 nvme_tcp_init_recv_ctx(queue); 1023 return 0; 1024 } 1025 1026 static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb, 1027 unsigned int offset, size_t len) 1028 { 1029 struct nvme_tcp_queue *queue = desc->arg.data; 1030 size_t consumed = len; 1031 int result; 1032 1033 if (unlikely(!queue->rd_enabled)) 1034 return -EFAULT; 1035 1036 while (len) { 1037 switch (nvme_tcp_recv_state(queue)) { 1038 case NVME_TCP_RECV_PDU: 1039 result = nvme_tcp_recv_pdu(queue, skb, &offset, &len); 1040 break; 1041 case NVME_TCP_RECV_DATA: 1042 result = nvme_tcp_recv_data(queue, skb, &offset, &len); 1043 break; 1044 case NVME_TCP_RECV_DDGST: 1045 result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len); 1046 break; 1047 default: 1048 result = -EFAULT; 1049 } 1050 if (result) { 1051 dev_err(queue->ctrl->ctrl.device, 1052 "receive failed: %d\n", result); 1053 queue->rd_enabled = false; 1054 nvme_tcp_error_recovery(&queue->ctrl->ctrl); 1055 return result; 1056 } 1057 } 1058 1059 return consumed; 1060 } 1061 1062 static void nvme_tcp_data_ready(struct sock *sk) 1063 { 1064 struct nvme_tcp_queue *queue; 1065 1066 trace_sk_data_ready(sk); 1067 1068 read_lock_bh(&sk->sk_callback_lock); 1069 queue = sk->sk_user_data; 1070 if (likely(queue && queue->rd_enabled) && 1071 !test_bit(NVME_TCP_Q_POLLING, &queue->flags)) 1072 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); 1073 read_unlock_bh(&sk->sk_callback_lock); 1074 } 1075 1076 static void nvme_tcp_write_space(struct sock *sk) 1077 { 1078 struct nvme_tcp_queue *queue; 1079 1080 read_lock_bh(&sk->sk_callback_lock); 1081 queue = sk->sk_user_data; 1082 if (likely(queue && sk_stream_is_writeable(sk))) { 1083 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1084 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); 1085 } 1086 read_unlock_bh(&sk->sk_callback_lock); 1087 } 1088 1089 static void nvme_tcp_state_change(struct sock *sk) 1090 { 1091 struct nvme_tcp_queue *queue; 1092 1093 read_lock_bh(&sk->sk_callback_lock); 1094 queue = sk->sk_user_data; 1095 if (!queue) 1096 goto done; 1097 1098 switch (sk->sk_state) { 1099 case TCP_CLOSE: 1100 case TCP_CLOSE_WAIT: 1101 case TCP_LAST_ACK: 1102 case TCP_FIN_WAIT1: 1103 case TCP_FIN_WAIT2: 1104 nvme_tcp_error_recovery(&queue->ctrl->ctrl); 1105 break; 1106 default: 1107 dev_info(queue->ctrl->ctrl.device, 1108 "queue %d socket state %d\n", 1109 nvme_tcp_queue_id(queue), sk->sk_state); 1110 } 1111 1112 queue->state_change(sk); 1113 done: 1114 read_unlock_bh(&sk->sk_callback_lock); 1115 } 1116 1117 static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue) 1118 { 1119 queue->request = NULL; 1120 } 1121 1122 static void nvme_tcp_fail_request(struct nvme_tcp_request *req) 1123 { 1124 if (nvme_tcp_async_req(req)) { 1125 union nvme_result res = {}; 1126 1127 nvme_complete_async_event(&req->queue->ctrl->ctrl, 1128 cpu_to_le16(NVME_SC_HOST_PATH_ERROR), &res); 1129 } else { 1130 nvme_tcp_end_request(blk_mq_rq_from_pdu(req), 1131 NVME_SC_HOST_PATH_ERROR); 1132 } 1133 } 1134 1135 static int nvme_tcp_try_send_data(struct nvme_tcp_request *req) 1136 { 1137 struct nvme_tcp_queue *queue = req->queue; 1138 int req_data_len = req->data_len; 1139 u32 h2cdata_left = req->h2cdata_left; 1140 1141 while (true) { 1142 struct bio_vec bvec; 1143 struct msghdr msg = { 1144 .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES, 1145 }; 1146 struct page *page = nvme_tcp_req_cur_page(req); 1147 size_t offset = nvme_tcp_req_cur_offset(req); 1148 size_t len = nvme_tcp_req_cur_length(req); 1149 bool last = nvme_tcp_pdu_last_send(req, len); 1150 int req_data_sent = req->data_sent; 1151 int ret; 1152 1153 if (last && !queue->data_digest && !nvme_tcp_queue_more(queue)) 1154 msg.msg_flags |= MSG_EOR; 1155 else 1156 msg.msg_flags |= MSG_MORE; 1157 1158 if (!sendpages_ok(page, len, offset)) 1159 msg.msg_flags &= ~MSG_SPLICE_PAGES; 1160 1161 bvec_set_page(&bvec, page, len, offset); 1162 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len); 1163 ret = sock_sendmsg(queue->sock, &msg); 1164 if (ret <= 0) 1165 return ret; 1166 1167 if (queue->data_digest) 1168 nvme_tcp_ddgst_update(&queue->snd_crc, page, 1169 offset, ret); 1170 1171 /* 1172 * update the request iterator except for the last payload send 1173 * in the request where we don't want to modify it as we may 1174 * compete with the RX path completing the request. 1175 */ 1176 if (req_data_sent + ret < req_data_len) 1177 nvme_tcp_advance_req(req, ret); 1178 1179 /* fully successful last send in current PDU */ 1180 if (last && ret == len) { 1181 if (queue->data_digest) { 1182 req->ddgst = 1183 nvme_tcp_ddgst_final(queue->snd_crc); 1184 req->state = NVME_TCP_SEND_DDGST; 1185 req->offset = 0; 1186 } else { 1187 if (h2cdata_left) 1188 nvme_tcp_setup_h2c_data_pdu(req); 1189 else 1190 nvme_tcp_done_send_req(queue); 1191 } 1192 return 1; 1193 } 1194 } 1195 return -EAGAIN; 1196 } 1197 1198 static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req) 1199 { 1200 struct nvme_tcp_queue *queue = req->queue; 1201 struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); 1202 struct bio_vec bvec; 1203 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES, }; 1204 bool inline_data = nvme_tcp_has_inline_data(req); 1205 u8 hdgst = nvme_tcp_hdgst_len(queue); 1206 int len = sizeof(*pdu) + hdgst - req->offset; 1207 int ret; 1208 1209 if (inline_data || nvme_tcp_queue_more(queue)) 1210 msg.msg_flags |= MSG_MORE; 1211 else 1212 msg.msg_flags |= MSG_EOR; 1213 1214 if (queue->hdr_digest && !req->offset) 1215 nvme_tcp_set_hdgst(pdu, sizeof(*pdu)); 1216 1217 bvec_set_virt(&bvec, (void *)pdu + req->offset, len); 1218 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len); 1219 ret = sock_sendmsg(queue->sock, &msg); 1220 if (unlikely(ret <= 0)) 1221 return ret; 1222 1223 len -= ret; 1224 if (!len) { 1225 if (inline_data) { 1226 req->state = NVME_TCP_SEND_DATA; 1227 if (queue->data_digest) 1228 queue->snd_crc = NVME_TCP_CRC_SEED; 1229 } else { 1230 nvme_tcp_done_send_req(queue); 1231 } 1232 return 1; 1233 } 1234 req->offset += ret; 1235 1236 return -EAGAIN; 1237 } 1238 1239 static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req) 1240 { 1241 struct nvme_tcp_queue *queue = req->queue; 1242 struct nvme_tcp_data_pdu *pdu = nvme_tcp_req_data_pdu(req); 1243 struct bio_vec bvec; 1244 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_MORE, }; 1245 u8 hdgst = nvme_tcp_hdgst_len(queue); 1246 int len = sizeof(*pdu) - req->offset + hdgst; 1247 int ret; 1248 1249 if (queue->hdr_digest && !req->offset) 1250 nvme_tcp_set_hdgst(pdu, sizeof(*pdu)); 1251 1252 if (!req->h2cdata_left) 1253 msg.msg_flags |= MSG_SPLICE_PAGES; 1254 1255 bvec_set_virt(&bvec, (void *)pdu + req->offset, len); 1256 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len); 1257 ret = sock_sendmsg(queue->sock, &msg); 1258 if (unlikely(ret <= 0)) 1259 return ret; 1260 1261 len -= ret; 1262 if (!len) { 1263 req->state = NVME_TCP_SEND_DATA; 1264 if (queue->data_digest) 1265 queue->snd_crc = NVME_TCP_CRC_SEED; 1266 return 1; 1267 } 1268 req->offset += ret; 1269 1270 return -EAGAIN; 1271 } 1272 1273 static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req) 1274 { 1275 struct nvme_tcp_queue *queue = req->queue; 1276 size_t offset = req->offset; 1277 u32 h2cdata_left = req->h2cdata_left; 1278 int ret; 1279 struct msghdr msg = { .msg_flags = MSG_DONTWAIT }; 1280 struct kvec iov = { 1281 .iov_base = (u8 *)&req->ddgst + req->offset, 1282 .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset 1283 }; 1284 1285 if (nvme_tcp_queue_more(queue)) 1286 msg.msg_flags |= MSG_MORE; 1287 else 1288 msg.msg_flags |= MSG_EOR; 1289 1290 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len); 1291 if (unlikely(ret <= 0)) 1292 return ret; 1293 1294 if (offset + ret == NVME_TCP_DIGEST_LENGTH) { 1295 if (h2cdata_left) 1296 nvme_tcp_setup_h2c_data_pdu(req); 1297 else 1298 nvme_tcp_done_send_req(queue); 1299 return 1; 1300 } 1301 1302 req->offset += ret; 1303 return -EAGAIN; 1304 } 1305 1306 static int nvme_tcp_try_send(struct nvme_tcp_queue *queue) 1307 { 1308 struct nvme_tcp_request *req; 1309 unsigned int noreclaim_flag; 1310 int ret = 1; 1311 1312 if (!queue->request) { 1313 queue->request = nvme_tcp_fetch_request(queue); 1314 if (!queue->request) 1315 return 0; 1316 } 1317 req = queue->request; 1318 1319 noreclaim_flag = memalloc_noreclaim_save(); 1320 if (req->state == NVME_TCP_SEND_CMD_PDU) { 1321 ret = nvme_tcp_try_send_cmd_pdu(req); 1322 if (ret <= 0) 1323 goto done; 1324 if (!nvme_tcp_has_inline_data(req)) 1325 goto out; 1326 } 1327 1328 if (req->state == NVME_TCP_SEND_H2C_PDU) { 1329 ret = nvme_tcp_try_send_data_pdu(req); 1330 if (ret <= 0) 1331 goto done; 1332 } 1333 1334 if (req->state == NVME_TCP_SEND_DATA) { 1335 ret = nvme_tcp_try_send_data(req); 1336 if (ret <= 0) 1337 goto done; 1338 } 1339 1340 if (req->state == NVME_TCP_SEND_DDGST) 1341 ret = nvme_tcp_try_send_ddgst(req); 1342 done: 1343 if (ret == -EAGAIN) { 1344 ret = 0; 1345 } else if (ret < 0) { 1346 dev_err(queue->ctrl->ctrl.device, 1347 "failed to send request %d\n", ret); 1348 nvme_tcp_fail_request(queue->request); 1349 nvme_tcp_done_send_req(queue); 1350 } 1351 out: 1352 memalloc_noreclaim_restore(noreclaim_flag); 1353 return ret; 1354 } 1355 1356 static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue) 1357 { 1358 struct socket *sock = queue->sock; 1359 struct sock *sk = sock->sk; 1360 read_descriptor_t rd_desc; 1361 int consumed; 1362 1363 rd_desc.arg.data = queue; 1364 rd_desc.count = 1; 1365 lock_sock(sk); 1366 queue->nr_cqe = 0; 1367 consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb); 1368 release_sock(sk); 1369 return consumed == -EAGAIN ? 0 : consumed; 1370 } 1371 1372 static void nvme_tcp_io_work(struct work_struct *w) 1373 { 1374 struct nvme_tcp_queue *queue = 1375 container_of(w, struct nvme_tcp_queue, io_work); 1376 unsigned long deadline = jiffies + msecs_to_jiffies(1); 1377 1378 do { 1379 bool pending = false; 1380 int result; 1381 1382 if (mutex_trylock(&queue->send_mutex)) { 1383 result = nvme_tcp_try_send(queue); 1384 mutex_unlock(&queue->send_mutex); 1385 if (result > 0) 1386 pending = true; 1387 else if (unlikely(result < 0)) 1388 break; 1389 } 1390 1391 result = nvme_tcp_try_recv(queue); 1392 if (result > 0) 1393 pending = true; 1394 else if (unlikely(result < 0)) 1395 return; 1396 1397 /* did we get some space after spending time in recv? */ 1398 if (nvme_tcp_queue_has_pending(queue) && 1399 sk_stream_is_writeable(queue->sock->sk)) 1400 pending = true; 1401 1402 if (!pending || !queue->rd_enabled) 1403 return; 1404 1405 } while (!time_after(jiffies, deadline)); /* quota is exhausted */ 1406 1407 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); 1408 } 1409 1410 static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl) 1411 { 1412 struct nvme_tcp_request *async = &ctrl->async_req; 1413 1414 page_frag_free(async->pdu); 1415 } 1416 1417 static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl) 1418 { 1419 struct nvme_tcp_queue *queue = &ctrl->queues[0]; 1420 struct nvme_tcp_request *async = &ctrl->async_req; 1421 u8 hdgst = nvme_tcp_hdgst_len(queue); 1422 1423 async->pdu = page_frag_alloc(&queue->pf_cache, 1424 sizeof(struct nvme_tcp_cmd_pdu) + hdgst, 1425 GFP_KERNEL | __GFP_ZERO); 1426 if (!async->pdu) 1427 return -ENOMEM; 1428 1429 async->queue = &ctrl->queues[0]; 1430 return 0; 1431 } 1432 1433 static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid) 1434 { 1435 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 1436 struct nvme_tcp_queue *queue = &ctrl->queues[qid]; 1437 unsigned int noreclaim_flag; 1438 1439 if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) 1440 return; 1441 1442 page_frag_cache_drain(&queue->pf_cache); 1443 1444 noreclaim_flag = memalloc_noreclaim_save(); 1445 /* ->sock will be released by fput() */ 1446 fput(queue->sock->file); 1447 queue->sock = NULL; 1448 memalloc_noreclaim_restore(noreclaim_flag); 1449 1450 kfree(queue->pdu); 1451 mutex_destroy(&queue->send_mutex); 1452 mutex_destroy(&queue->queue_lock); 1453 } 1454 1455 static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue) 1456 { 1457 struct nvme_tcp_icreq_pdu *icreq; 1458 struct nvme_tcp_icresp_pdu *icresp; 1459 char cbuf[CMSG_LEN(sizeof(char))] = {}; 1460 u8 ctype; 1461 struct msghdr msg = {}; 1462 struct kvec iov; 1463 bool ctrl_hdgst, ctrl_ddgst; 1464 u32 maxh2cdata; 1465 int ret; 1466 1467 icreq = kzalloc(sizeof(*icreq), GFP_KERNEL); 1468 if (!icreq) 1469 return -ENOMEM; 1470 1471 icresp = kzalloc(sizeof(*icresp), GFP_KERNEL); 1472 if (!icresp) { 1473 ret = -ENOMEM; 1474 goto free_icreq; 1475 } 1476 1477 icreq->hdr.type = nvme_tcp_icreq; 1478 icreq->hdr.hlen = sizeof(*icreq); 1479 icreq->hdr.pdo = 0; 1480 icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen); 1481 icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0); 1482 icreq->maxr2t = 0; /* single inflight r2t supported */ 1483 icreq->hpda = 0; /* no alignment constraint */ 1484 if (queue->hdr_digest) 1485 icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE; 1486 if (queue->data_digest) 1487 icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE; 1488 1489 iov.iov_base = icreq; 1490 iov.iov_len = sizeof(*icreq); 1491 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len); 1492 if (ret < 0) { 1493 pr_warn("queue %d: failed to send icreq, error %d\n", 1494 nvme_tcp_queue_id(queue), ret); 1495 goto free_icresp; 1496 } 1497 1498 memset(&msg, 0, sizeof(msg)); 1499 iov.iov_base = icresp; 1500 iov.iov_len = sizeof(*icresp); 1501 if (nvme_tcp_queue_tls(queue)) { 1502 msg.msg_control = cbuf; 1503 msg.msg_controllen = sizeof(cbuf); 1504 } 1505 msg.msg_flags = MSG_WAITALL; 1506 ret = kernel_recvmsg(queue->sock, &msg, &iov, 1, 1507 iov.iov_len, msg.msg_flags); 1508 if (ret >= 0 && ret < sizeof(*icresp)) 1509 ret = -ECONNRESET; 1510 if (ret < 0) { 1511 pr_warn("queue %d: failed to receive icresp, error %d\n", 1512 nvme_tcp_queue_id(queue), ret); 1513 goto free_icresp; 1514 } 1515 ret = -ENOTCONN; 1516 if (nvme_tcp_queue_tls(queue)) { 1517 ctype = tls_get_record_type(queue->sock->sk, 1518 (struct cmsghdr *)cbuf); 1519 if (ctype != TLS_RECORD_TYPE_DATA) { 1520 pr_err("queue %d: unhandled TLS record %d\n", 1521 nvme_tcp_queue_id(queue), ctype); 1522 goto free_icresp; 1523 } 1524 } 1525 ret = -EINVAL; 1526 if (icresp->hdr.type != nvme_tcp_icresp) { 1527 pr_err("queue %d: bad type returned %d\n", 1528 nvme_tcp_queue_id(queue), icresp->hdr.type); 1529 goto free_icresp; 1530 } 1531 1532 if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) { 1533 pr_err("queue %d: bad pdu length returned %d\n", 1534 nvme_tcp_queue_id(queue), icresp->hdr.plen); 1535 goto free_icresp; 1536 } 1537 1538 if (icresp->pfv != NVME_TCP_PFV_1_0) { 1539 pr_err("queue %d: bad pfv returned %d\n", 1540 nvme_tcp_queue_id(queue), icresp->pfv); 1541 goto free_icresp; 1542 } 1543 1544 ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE); 1545 if ((queue->data_digest && !ctrl_ddgst) || 1546 (!queue->data_digest && ctrl_ddgst)) { 1547 pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n", 1548 nvme_tcp_queue_id(queue), 1549 queue->data_digest ? "enabled" : "disabled", 1550 ctrl_ddgst ? "enabled" : "disabled"); 1551 goto free_icresp; 1552 } 1553 1554 ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE); 1555 if ((queue->hdr_digest && !ctrl_hdgst) || 1556 (!queue->hdr_digest && ctrl_hdgst)) { 1557 pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n", 1558 nvme_tcp_queue_id(queue), 1559 queue->hdr_digest ? "enabled" : "disabled", 1560 ctrl_hdgst ? "enabled" : "disabled"); 1561 goto free_icresp; 1562 } 1563 1564 if (icresp->cpda != 0) { 1565 pr_err("queue %d: unsupported cpda returned %d\n", 1566 nvme_tcp_queue_id(queue), icresp->cpda); 1567 goto free_icresp; 1568 } 1569 1570 maxh2cdata = le32_to_cpu(icresp->maxdata); 1571 if ((maxh2cdata % 4) || (maxh2cdata < NVME_TCP_MIN_MAXH2CDATA)) { 1572 pr_err("queue %d: invalid maxh2cdata returned %u\n", 1573 nvme_tcp_queue_id(queue), maxh2cdata); 1574 goto free_icresp; 1575 } 1576 queue->maxh2cdata = maxh2cdata; 1577 1578 ret = 0; 1579 free_icresp: 1580 kfree(icresp); 1581 free_icreq: 1582 kfree(icreq); 1583 return ret; 1584 } 1585 1586 static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue) 1587 { 1588 return nvme_tcp_queue_id(queue) == 0; 1589 } 1590 1591 static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue) 1592 { 1593 struct nvme_tcp_ctrl *ctrl = queue->ctrl; 1594 int qid = nvme_tcp_queue_id(queue); 1595 1596 return !nvme_tcp_admin_queue(queue) && 1597 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT]; 1598 } 1599 1600 static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue) 1601 { 1602 struct nvme_tcp_ctrl *ctrl = queue->ctrl; 1603 int qid = nvme_tcp_queue_id(queue); 1604 1605 return !nvme_tcp_admin_queue(queue) && 1606 !nvme_tcp_default_queue(queue) && 1607 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] + 1608 ctrl->io_queues[HCTX_TYPE_READ]; 1609 } 1610 1611 static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue) 1612 { 1613 struct nvme_tcp_ctrl *ctrl = queue->ctrl; 1614 int qid = nvme_tcp_queue_id(queue); 1615 1616 return !nvme_tcp_admin_queue(queue) && 1617 !nvme_tcp_default_queue(queue) && 1618 !nvme_tcp_read_queue(queue) && 1619 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] + 1620 ctrl->io_queues[HCTX_TYPE_READ] + 1621 ctrl->io_queues[HCTX_TYPE_POLL]; 1622 } 1623 1624 /* 1625 * Track the number of queues assigned to each cpu using a global per-cpu 1626 * counter and select the least used cpu from the mq_map. Our goal is to spread 1627 * different controllers I/O threads across different cpu cores. 1628 * 1629 * Note that the accounting is not 100% perfect, but we don't need to be, we're 1630 * simply putting our best effort to select the best candidate cpu core that we 1631 * find at any given point. 1632 */ 1633 static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue) 1634 { 1635 struct nvme_tcp_ctrl *ctrl = queue->ctrl; 1636 struct blk_mq_tag_set *set = &ctrl->tag_set; 1637 int qid = nvme_tcp_queue_id(queue) - 1; 1638 unsigned int *mq_map = NULL; 1639 int cpu, min_queues = INT_MAX, io_cpu; 1640 1641 if (wq_unbound) 1642 goto out; 1643 1644 if (nvme_tcp_default_queue(queue)) 1645 mq_map = set->map[HCTX_TYPE_DEFAULT].mq_map; 1646 else if (nvme_tcp_read_queue(queue)) 1647 mq_map = set->map[HCTX_TYPE_READ].mq_map; 1648 else if (nvme_tcp_poll_queue(queue)) 1649 mq_map = set->map[HCTX_TYPE_POLL].mq_map; 1650 1651 if (WARN_ON(!mq_map)) 1652 goto out; 1653 1654 /* Search for the least used cpu from the mq_map */ 1655 io_cpu = WORK_CPU_UNBOUND; 1656 for_each_online_cpu(cpu) { 1657 int num_queues = atomic_read(&nvme_tcp_cpu_queues[cpu]); 1658 1659 if (mq_map[cpu] != qid) 1660 continue; 1661 if (num_queues < min_queues) { 1662 io_cpu = cpu; 1663 min_queues = num_queues; 1664 } 1665 } 1666 if (io_cpu != WORK_CPU_UNBOUND) { 1667 queue->io_cpu = io_cpu; 1668 atomic_inc(&nvme_tcp_cpu_queues[io_cpu]); 1669 set_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags); 1670 } 1671 out: 1672 dev_dbg(ctrl->ctrl.device, "queue %d: using cpu %d\n", 1673 qid, queue->io_cpu); 1674 } 1675 1676 static void nvme_tcp_tls_done(void *data, int status, key_serial_t pskid) 1677 { 1678 struct nvme_tcp_queue *queue = data; 1679 struct nvme_tcp_ctrl *ctrl = queue->ctrl; 1680 int qid = nvme_tcp_queue_id(queue); 1681 struct key *tls_key; 1682 1683 dev_dbg(ctrl->ctrl.device, "queue %d: TLS handshake done, key %x, status %d\n", 1684 qid, pskid, status); 1685 1686 if (status) { 1687 queue->tls_err = -status; 1688 goto out_complete; 1689 } 1690 1691 tls_key = nvme_tls_key_lookup(pskid); 1692 if (IS_ERR(tls_key)) { 1693 dev_warn(ctrl->ctrl.device, "queue %d: Invalid key %x\n", 1694 qid, pskid); 1695 queue->tls_err = -ENOKEY; 1696 } else { 1697 queue->tls_enabled = true; 1698 if (qid == 0) 1699 ctrl->ctrl.tls_pskid = key_serial(tls_key); 1700 key_put(tls_key); 1701 queue->tls_err = 0; 1702 } 1703 1704 out_complete: 1705 complete(&queue->tls_complete); 1706 } 1707 1708 static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl, 1709 struct nvme_tcp_queue *queue, 1710 key_serial_t pskid) 1711 { 1712 int qid = nvme_tcp_queue_id(queue); 1713 int ret; 1714 struct tls_handshake_args args; 1715 unsigned long tmo = tls_handshake_timeout * HZ; 1716 key_serial_t keyring = nvme_keyring_id(); 1717 1718 dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n", 1719 qid, pskid); 1720 memset(&args, 0, sizeof(args)); 1721 args.ta_sock = queue->sock; 1722 args.ta_done = nvme_tcp_tls_done; 1723 args.ta_data = queue; 1724 args.ta_my_peerids[0] = pskid; 1725 args.ta_num_peerids = 1; 1726 if (nctrl->opts->keyring) 1727 keyring = key_serial(nctrl->opts->keyring); 1728 args.ta_keyring = keyring; 1729 args.ta_timeout_ms = tls_handshake_timeout * 1000; 1730 queue->tls_err = -EOPNOTSUPP; 1731 init_completion(&queue->tls_complete); 1732 ret = tls_client_hello_psk(&args, GFP_KERNEL); 1733 if (ret) { 1734 dev_err(nctrl->device, "queue %d: failed to start TLS: %d\n", 1735 qid, ret); 1736 return ret; 1737 } 1738 ret = wait_for_completion_interruptible_timeout(&queue->tls_complete, tmo); 1739 if (ret <= 0) { 1740 if (ret == 0) 1741 ret = -ETIMEDOUT; 1742 1743 dev_err(nctrl->device, 1744 "queue %d: TLS handshake failed, error %d\n", 1745 qid, ret); 1746 tls_handshake_cancel(queue->sock->sk); 1747 } else { 1748 if (queue->tls_err) { 1749 dev_err(nctrl->device, 1750 "queue %d: TLS handshake complete, error %d\n", 1751 qid, queue->tls_err); 1752 } else { 1753 dev_dbg(nctrl->device, 1754 "queue %d: TLS handshake complete\n", qid); 1755 } 1756 ret = queue->tls_err; 1757 } 1758 return ret; 1759 } 1760 1761 static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid, 1762 key_serial_t pskid) 1763 { 1764 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 1765 struct nvme_tcp_queue *queue = &ctrl->queues[qid]; 1766 int ret, rcv_pdu_size; 1767 struct file *sock_file; 1768 1769 mutex_init(&queue->queue_lock); 1770 queue->ctrl = ctrl; 1771 init_llist_head(&queue->req_list); 1772 INIT_LIST_HEAD(&queue->send_list); 1773 mutex_init(&queue->send_mutex); 1774 INIT_WORK(&queue->io_work, nvme_tcp_io_work); 1775 1776 if (qid > 0) 1777 queue->cmnd_capsule_len = nctrl->ioccsz * 16; 1778 else 1779 queue->cmnd_capsule_len = sizeof(struct nvme_command) + 1780 NVME_TCP_ADMIN_CCSZ; 1781 1782 ret = sock_create_kern(current->nsproxy->net_ns, 1783 ctrl->addr.ss_family, SOCK_STREAM, 1784 IPPROTO_TCP, &queue->sock); 1785 if (ret) { 1786 dev_err(nctrl->device, 1787 "failed to create socket: %d\n", ret); 1788 goto err_destroy_mutex; 1789 } 1790 1791 sock_file = sock_alloc_file(queue->sock, O_CLOEXEC, NULL); 1792 if (IS_ERR(sock_file)) { 1793 ret = PTR_ERR(sock_file); 1794 goto err_destroy_mutex; 1795 } 1796 1797 sk_net_refcnt_upgrade(queue->sock->sk); 1798 nvme_tcp_reclassify_socket(queue->sock); 1799 1800 /* Single syn retry */ 1801 tcp_sock_set_syncnt(queue->sock->sk, 1); 1802 1803 /* Set TCP no delay */ 1804 tcp_sock_set_nodelay(queue->sock->sk); 1805 1806 /* 1807 * Cleanup whatever is sitting in the TCP transmit queue on socket 1808 * close. This is done to prevent stale data from being sent should 1809 * the network connection be restored before TCP times out. 1810 */ 1811 sock_no_linger(queue->sock->sk); 1812 1813 if (so_priority > 0) 1814 sock_set_priority(queue->sock->sk, so_priority); 1815 1816 /* Set socket type of service */ 1817 if (nctrl->opts->tos >= 0) 1818 ip_sock_set_tos(queue->sock->sk, nctrl->opts->tos); 1819 1820 /* Set 10 seconds timeout for icresp recvmsg */ 1821 queue->sock->sk->sk_rcvtimeo = 10 * HZ; 1822 1823 queue->sock->sk->sk_allocation = GFP_ATOMIC; 1824 queue->sock->sk->sk_use_task_frag = false; 1825 queue->io_cpu = WORK_CPU_UNBOUND; 1826 queue->request = NULL; 1827 queue->data_remaining = 0; 1828 queue->ddgst_remaining = 0; 1829 queue->pdu_remaining = 0; 1830 queue->pdu_offset = 0; 1831 sk_set_memalloc(queue->sock->sk); 1832 1833 if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) { 1834 ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr, 1835 sizeof(ctrl->src_addr)); 1836 if (ret) { 1837 dev_err(nctrl->device, 1838 "failed to bind queue %d socket %d\n", 1839 qid, ret); 1840 goto err_sock; 1841 } 1842 } 1843 1844 if (nctrl->opts->mask & NVMF_OPT_HOST_IFACE) { 1845 char *iface = nctrl->opts->host_iface; 1846 sockptr_t optval = KERNEL_SOCKPTR(iface); 1847 1848 ret = sock_setsockopt(queue->sock, SOL_SOCKET, SO_BINDTODEVICE, 1849 optval, strlen(iface)); 1850 if (ret) { 1851 dev_err(nctrl->device, 1852 "failed to bind to interface %s queue %d err %d\n", 1853 iface, qid, ret); 1854 goto err_sock; 1855 } 1856 } 1857 1858 queue->hdr_digest = nctrl->opts->hdr_digest; 1859 queue->data_digest = nctrl->opts->data_digest; 1860 1861 rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) + 1862 nvme_tcp_hdgst_len(queue); 1863 queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL); 1864 if (!queue->pdu) { 1865 ret = -ENOMEM; 1866 goto err_sock; 1867 } 1868 1869 dev_dbg(nctrl->device, "connecting queue %d\n", 1870 nvme_tcp_queue_id(queue)); 1871 1872 ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr, 1873 sizeof(ctrl->addr), 0); 1874 if (ret) { 1875 dev_err(nctrl->device, 1876 "failed to connect socket: %d\n", ret); 1877 goto err_rcv_pdu; 1878 } 1879 1880 /* If PSKs are configured try to start TLS */ 1881 if (nvme_tcp_tls_configured(nctrl) && pskid) { 1882 ret = nvme_tcp_start_tls(nctrl, queue, pskid); 1883 if (ret) 1884 goto err_init_connect; 1885 } 1886 1887 ret = nvme_tcp_init_connection(queue); 1888 if (ret) 1889 goto err_init_connect; 1890 1891 set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags); 1892 1893 return 0; 1894 1895 err_init_connect: 1896 kernel_sock_shutdown(queue->sock, SHUT_RDWR); 1897 err_rcv_pdu: 1898 kfree(queue->pdu); 1899 err_sock: 1900 /* ->sock will be released by fput() */ 1901 fput(queue->sock->file); 1902 queue->sock = NULL; 1903 err_destroy_mutex: 1904 mutex_destroy(&queue->send_mutex); 1905 mutex_destroy(&queue->queue_lock); 1906 return ret; 1907 } 1908 1909 static void nvme_tcp_restore_sock_ops(struct nvme_tcp_queue *queue) 1910 { 1911 struct socket *sock = queue->sock; 1912 1913 write_lock_bh(&sock->sk->sk_callback_lock); 1914 sock->sk->sk_user_data = NULL; 1915 sock->sk->sk_data_ready = queue->data_ready; 1916 sock->sk->sk_state_change = queue->state_change; 1917 sock->sk->sk_write_space = queue->write_space; 1918 write_unlock_bh(&sock->sk->sk_callback_lock); 1919 } 1920 1921 static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue) 1922 { 1923 kernel_sock_shutdown(queue->sock, SHUT_RDWR); 1924 nvme_tcp_restore_sock_ops(queue); 1925 cancel_work_sync(&queue->io_work); 1926 } 1927 1928 static void nvme_tcp_stop_queue_nowait(struct nvme_ctrl *nctrl, int qid) 1929 { 1930 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 1931 struct nvme_tcp_queue *queue = &ctrl->queues[qid]; 1932 1933 if (!test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) 1934 return; 1935 1936 if (test_and_clear_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags)) 1937 atomic_dec(&nvme_tcp_cpu_queues[queue->io_cpu]); 1938 1939 mutex_lock(&queue->queue_lock); 1940 if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags)) 1941 __nvme_tcp_stop_queue(queue); 1942 /* Stopping the queue will disable TLS */ 1943 queue->tls_enabled = false; 1944 mutex_unlock(&queue->queue_lock); 1945 } 1946 1947 static void nvme_tcp_wait_queue(struct nvme_ctrl *nctrl, int qid) 1948 { 1949 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 1950 struct nvme_tcp_queue *queue = &ctrl->queues[qid]; 1951 int timeout = 100; 1952 1953 while (timeout > 0) { 1954 if (!test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags) || 1955 !sk_wmem_alloc_get(queue->sock->sk)) 1956 return; 1957 msleep(2); 1958 timeout -= 2; 1959 } 1960 dev_warn(nctrl->device, 1961 "qid %d: timeout draining sock wmem allocation expired\n", 1962 qid); 1963 } 1964 1965 static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid) 1966 { 1967 nvme_tcp_stop_queue_nowait(nctrl, qid); 1968 nvme_tcp_wait_queue(nctrl, qid); 1969 } 1970 1971 1972 static void nvme_tcp_setup_sock_ops(struct nvme_tcp_queue *queue) 1973 { 1974 write_lock_bh(&queue->sock->sk->sk_callback_lock); 1975 queue->sock->sk->sk_user_data = queue; 1976 queue->state_change = queue->sock->sk->sk_state_change; 1977 queue->data_ready = queue->sock->sk->sk_data_ready; 1978 queue->write_space = queue->sock->sk->sk_write_space; 1979 queue->sock->sk->sk_data_ready = nvme_tcp_data_ready; 1980 queue->sock->sk->sk_state_change = nvme_tcp_state_change; 1981 queue->sock->sk->sk_write_space = nvme_tcp_write_space; 1982 #ifdef CONFIG_NET_RX_BUSY_POLL 1983 queue->sock->sk->sk_ll_usec = 1; 1984 #endif 1985 write_unlock_bh(&queue->sock->sk->sk_callback_lock); 1986 } 1987 1988 static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx) 1989 { 1990 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 1991 struct nvme_tcp_queue *queue = &ctrl->queues[idx]; 1992 int ret; 1993 1994 queue->rd_enabled = true; 1995 nvme_tcp_init_recv_ctx(queue); 1996 nvme_tcp_setup_sock_ops(queue); 1997 1998 if (idx) { 1999 nvme_tcp_set_queue_io_cpu(queue); 2000 ret = nvmf_connect_io_queue(nctrl, idx); 2001 } else 2002 ret = nvmf_connect_admin_queue(nctrl); 2003 2004 if (!ret) { 2005 set_bit(NVME_TCP_Q_LIVE, &queue->flags); 2006 } else { 2007 if (test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) 2008 __nvme_tcp_stop_queue(queue); 2009 dev_err(nctrl->device, 2010 "failed to connect queue: %d ret=%d\n", idx, ret); 2011 } 2012 return ret; 2013 } 2014 2015 static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl) 2016 { 2017 if (to_tcp_ctrl(ctrl)->async_req.pdu) { 2018 cancel_work_sync(&ctrl->async_event_work); 2019 nvme_tcp_free_async_req(to_tcp_ctrl(ctrl)); 2020 to_tcp_ctrl(ctrl)->async_req.pdu = NULL; 2021 } 2022 2023 nvme_tcp_free_queue(ctrl, 0); 2024 } 2025 2026 static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl) 2027 { 2028 int i; 2029 2030 for (i = 1; i < ctrl->queue_count; i++) 2031 nvme_tcp_free_queue(ctrl, i); 2032 } 2033 2034 static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl) 2035 { 2036 int i; 2037 2038 for (i = 1; i < ctrl->queue_count; i++) 2039 nvme_tcp_stop_queue_nowait(ctrl, i); 2040 for (i = 1; i < ctrl->queue_count; i++) 2041 nvme_tcp_wait_queue(ctrl, i); 2042 } 2043 2044 static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl, 2045 int first, int last) 2046 { 2047 int i, ret; 2048 2049 for (i = first; i < last; i++) { 2050 ret = nvme_tcp_start_queue(ctrl, i); 2051 if (ret) 2052 goto out_stop_queues; 2053 } 2054 2055 return 0; 2056 2057 out_stop_queues: 2058 for (i--; i >= first; i--) 2059 nvme_tcp_stop_queue(ctrl, i); 2060 return ret; 2061 } 2062 2063 static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) 2064 { 2065 int ret; 2066 key_serial_t pskid = 0; 2067 2068 if (nvme_tcp_tls_configured(ctrl)) { 2069 if (ctrl->opts->tls_key) 2070 pskid = key_serial(ctrl->opts->tls_key); 2071 else if (ctrl->opts->tls) { 2072 pskid = nvme_tls_psk_default(ctrl->opts->keyring, 2073 ctrl->opts->host->nqn, 2074 ctrl->opts->subsysnqn); 2075 if (!pskid) { 2076 dev_err(ctrl->device, "no valid PSK found\n"); 2077 return -ENOKEY; 2078 } 2079 } 2080 } 2081 2082 ret = nvme_tcp_alloc_queue(ctrl, 0, pskid); 2083 if (ret) 2084 return ret; 2085 2086 ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl)); 2087 if (ret) 2088 goto out_free_queue; 2089 2090 return 0; 2091 2092 out_free_queue: 2093 nvme_tcp_free_queue(ctrl, 0); 2094 return ret; 2095 } 2096 2097 static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl) 2098 { 2099 int i, ret; 2100 2101 if (nvme_tcp_tls_configured(ctrl)) { 2102 if (ctrl->opts->concat) { 2103 /* 2104 * The generated PSK is stored in the 2105 * fabric options 2106 */ 2107 if (!ctrl->opts->tls_key) { 2108 dev_err(ctrl->device, "no PSK generated\n"); 2109 return -ENOKEY; 2110 } 2111 if (ctrl->tls_pskid && 2112 ctrl->tls_pskid != key_serial(ctrl->opts->tls_key)) { 2113 dev_err(ctrl->device, "Stale PSK id %08x\n", ctrl->tls_pskid); 2114 ctrl->tls_pskid = 0; 2115 } 2116 } else if (!ctrl->tls_pskid) { 2117 dev_err(ctrl->device, "no PSK negotiated\n"); 2118 return -ENOKEY; 2119 } 2120 } 2121 2122 for (i = 1; i < ctrl->queue_count; i++) { 2123 ret = nvme_tcp_alloc_queue(ctrl, i, 2124 ctrl->tls_pskid); 2125 if (ret) 2126 goto out_free_queues; 2127 } 2128 2129 return 0; 2130 2131 out_free_queues: 2132 for (i--; i >= 1; i--) 2133 nvme_tcp_free_queue(ctrl, i); 2134 2135 return ret; 2136 } 2137 2138 static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl) 2139 { 2140 unsigned int nr_io_queues; 2141 int ret; 2142 2143 nr_io_queues = nvmf_nr_io_queues(ctrl->opts); 2144 ret = nvme_set_queue_count(ctrl, &nr_io_queues); 2145 if (ret) 2146 return ret; 2147 2148 if (nr_io_queues == 0) { 2149 dev_err(ctrl->device, 2150 "unable to set any I/O queues\n"); 2151 return -ENOMEM; 2152 } 2153 2154 ctrl->queue_count = nr_io_queues + 1; 2155 dev_info(ctrl->device, 2156 "creating %d I/O queues.\n", nr_io_queues); 2157 2158 nvmf_set_io_queues(ctrl->opts, nr_io_queues, 2159 to_tcp_ctrl(ctrl)->io_queues); 2160 return __nvme_tcp_alloc_io_queues(ctrl); 2161 } 2162 2163 static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new) 2164 { 2165 int ret, nr_queues; 2166 2167 ret = nvme_tcp_alloc_io_queues(ctrl); 2168 if (ret) 2169 return ret; 2170 2171 if (new) { 2172 ret = nvme_alloc_io_tag_set(ctrl, &to_tcp_ctrl(ctrl)->tag_set, 2173 &nvme_tcp_mq_ops, 2174 ctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2, 2175 sizeof(struct nvme_tcp_request)); 2176 if (ret) 2177 goto out_free_io_queues; 2178 } 2179 2180 /* 2181 * Only start IO queues for which we have allocated the tagset 2182 * and limited it to the available queues. On reconnects, the 2183 * queue number might have changed. 2184 */ 2185 nr_queues = min(ctrl->tagset->nr_hw_queues + 1, ctrl->queue_count); 2186 ret = nvme_tcp_start_io_queues(ctrl, 1, nr_queues); 2187 if (ret) 2188 goto out_cleanup_connect_q; 2189 2190 if (!new) { 2191 nvme_start_freeze(ctrl); 2192 nvme_unquiesce_io_queues(ctrl); 2193 if (!nvme_wait_freeze_timeout(ctrl, NVME_IO_TIMEOUT)) { 2194 /* 2195 * If we timed out waiting for freeze we are likely to 2196 * be stuck. Fail the controller initialization just 2197 * to be safe. 2198 */ 2199 ret = -ENODEV; 2200 nvme_unfreeze(ctrl); 2201 goto out_wait_freeze_timed_out; 2202 } 2203 blk_mq_update_nr_hw_queues(ctrl->tagset, 2204 ctrl->queue_count - 1); 2205 nvme_unfreeze(ctrl); 2206 } 2207 2208 /* 2209 * If the number of queues has increased (reconnect case) 2210 * start all new queues now. 2211 */ 2212 ret = nvme_tcp_start_io_queues(ctrl, nr_queues, 2213 ctrl->tagset->nr_hw_queues + 1); 2214 if (ret) 2215 goto out_wait_freeze_timed_out; 2216 2217 return 0; 2218 2219 out_wait_freeze_timed_out: 2220 nvme_quiesce_io_queues(ctrl); 2221 nvme_sync_io_queues(ctrl); 2222 nvme_tcp_stop_io_queues(ctrl); 2223 out_cleanup_connect_q: 2224 nvme_cancel_tagset(ctrl); 2225 if (new) 2226 nvme_remove_io_tag_set(ctrl); 2227 out_free_io_queues: 2228 nvme_tcp_free_io_queues(ctrl); 2229 return ret; 2230 } 2231 2232 static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new) 2233 { 2234 int error; 2235 2236 error = nvme_tcp_alloc_admin_queue(ctrl); 2237 if (error) 2238 return error; 2239 2240 if (new) { 2241 error = nvme_alloc_admin_tag_set(ctrl, 2242 &to_tcp_ctrl(ctrl)->admin_tag_set, 2243 &nvme_tcp_admin_mq_ops, 2244 sizeof(struct nvme_tcp_request)); 2245 if (error) 2246 goto out_free_queue; 2247 } 2248 2249 error = nvme_tcp_start_queue(ctrl, 0); 2250 if (error) 2251 goto out_cleanup_tagset; 2252 2253 error = nvme_enable_ctrl(ctrl); 2254 if (error) 2255 goto out_stop_queue; 2256 2257 nvme_unquiesce_admin_queue(ctrl); 2258 2259 error = nvme_init_ctrl_finish(ctrl, false); 2260 if (error) 2261 goto out_quiesce_queue; 2262 2263 return 0; 2264 2265 out_quiesce_queue: 2266 nvme_quiesce_admin_queue(ctrl); 2267 blk_sync_queue(ctrl->admin_q); 2268 out_stop_queue: 2269 nvme_tcp_stop_queue(ctrl, 0); 2270 nvme_cancel_admin_tagset(ctrl); 2271 out_cleanup_tagset: 2272 if (new) 2273 nvme_remove_admin_tag_set(ctrl); 2274 out_free_queue: 2275 nvme_tcp_free_admin_queue(ctrl); 2276 return error; 2277 } 2278 2279 static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl, 2280 bool remove) 2281 { 2282 nvme_quiesce_admin_queue(ctrl); 2283 blk_sync_queue(ctrl->admin_q); 2284 nvme_tcp_stop_queue(ctrl, 0); 2285 nvme_cancel_admin_tagset(ctrl); 2286 if (remove) { 2287 nvme_unquiesce_admin_queue(ctrl); 2288 nvme_remove_admin_tag_set(ctrl); 2289 } 2290 nvme_tcp_free_admin_queue(ctrl); 2291 if (ctrl->tls_pskid) { 2292 dev_dbg(ctrl->device, "Wipe negotiated TLS_PSK %08x\n", 2293 ctrl->tls_pskid); 2294 ctrl->tls_pskid = 0; 2295 } 2296 } 2297 2298 static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl, 2299 bool remove) 2300 { 2301 if (ctrl->queue_count <= 1) 2302 return; 2303 nvme_quiesce_io_queues(ctrl); 2304 nvme_sync_io_queues(ctrl); 2305 nvme_tcp_stop_io_queues(ctrl); 2306 nvme_cancel_tagset(ctrl); 2307 if (remove) { 2308 nvme_unquiesce_io_queues(ctrl); 2309 nvme_remove_io_tag_set(ctrl); 2310 } 2311 nvme_tcp_free_io_queues(ctrl); 2312 } 2313 2314 static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl, 2315 int status) 2316 { 2317 enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); 2318 2319 /* If we are resetting/deleting then do nothing */ 2320 if (state != NVME_CTRL_CONNECTING) { 2321 WARN_ON_ONCE(state == NVME_CTRL_NEW || state == NVME_CTRL_LIVE); 2322 return; 2323 } 2324 2325 if (nvmf_should_reconnect(ctrl, status)) { 2326 dev_info(ctrl->device, "Reconnecting in %d seconds...\n", 2327 ctrl->opts->reconnect_delay); 2328 queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work, 2329 ctrl->opts->reconnect_delay * HZ); 2330 } else { 2331 dev_info(ctrl->device, "Removing controller (%d)...\n", 2332 status); 2333 nvme_delete_ctrl(ctrl); 2334 } 2335 } 2336 2337 /* 2338 * The TLS key is set by secure concatenation after negotiation has been 2339 * completed on the admin queue. We need to revoke the key when: 2340 * - concatenation is enabled (otherwise it's a static key set by the user) 2341 * and 2342 * - the generated key is present in ctrl->tls_key (otherwise there's nothing 2343 * to revoke) 2344 * and 2345 * - a valid PSK key ID has been set in ctrl->tls_pskid (otherwise TLS 2346 * negotiation has not run). 2347 * 2348 * We cannot always revoke the key as nvme_tcp_alloc_admin_queue() is called 2349 * twice during secure concatenation, once on a 'normal' connection to run the 2350 * DH-HMAC-CHAP negotiation (which generates the key, so it _must not_ be set), 2351 * and once after the negotiation (which uses the key, so it _must_ be set). 2352 */ 2353 static bool nvme_tcp_key_revoke_needed(struct nvme_ctrl *ctrl) 2354 { 2355 return ctrl->opts->concat && ctrl->opts->tls_key && ctrl->tls_pskid; 2356 } 2357 2358 static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new) 2359 { 2360 struct nvmf_ctrl_options *opts = ctrl->opts; 2361 int ret; 2362 2363 ret = nvme_tcp_configure_admin_queue(ctrl, new); 2364 if (ret) 2365 return ret; 2366 2367 if (ctrl->opts->concat && !ctrl->tls_pskid) { 2368 /* See comments for nvme_tcp_key_revoke_needed() */ 2369 dev_dbg(ctrl->device, "restart admin queue for secure concatenation\n"); 2370 nvme_stop_keep_alive(ctrl); 2371 nvme_tcp_teardown_admin_queue(ctrl, false); 2372 ret = nvme_tcp_configure_admin_queue(ctrl, false); 2373 if (ret) 2374 goto destroy_admin; 2375 } 2376 2377 if (ctrl->icdoff) { 2378 ret = -EOPNOTSUPP; 2379 dev_err(ctrl->device, "icdoff is not supported!\n"); 2380 goto destroy_admin; 2381 } 2382 2383 if (!nvme_ctrl_sgl_supported(ctrl)) { 2384 ret = -EOPNOTSUPP; 2385 dev_err(ctrl->device, "Mandatory sgls are not supported!\n"); 2386 goto destroy_admin; 2387 } 2388 2389 if (opts->queue_size > ctrl->sqsize + 1) 2390 dev_warn(ctrl->device, 2391 "queue_size %zu > ctrl sqsize %u, clamping down\n", 2392 opts->queue_size, ctrl->sqsize + 1); 2393 2394 if (ctrl->sqsize + 1 > ctrl->maxcmd) { 2395 dev_warn(ctrl->device, 2396 "sqsize %u > ctrl maxcmd %u, clamping down\n", 2397 ctrl->sqsize + 1, ctrl->maxcmd); 2398 ctrl->sqsize = ctrl->maxcmd - 1; 2399 } 2400 2401 if (ctrl->queue_count > 1) { 2402 ret = nvme_tcp_configure_io_queues(ctrl, new); 2403 if (ret) 2404 goto destroy_admin; 2405 } 2406 2407 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) { 2408 /* 2409 * state change failure is ok if we started ctrl delete, 2410 * unless we're during creation of a new controller to 2411 * avoid races with teardown flow. 2412 */ 2413 enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); 2414 2415 WARN_ON_ONCE(state != NVME_CTRL_DELETING && 2416 state != NVME_CTRL_DELETING_NOIO); 2417 WARN_ON_ONCE(new); 2418 ret = -EINVAL; 2419 goto destroy_io; 2420 } 2421 2422 nvme_start_ctrl(ctrl); 2423 return 0; 2424 2425 destroy_io: 2426 if (ctrl->queue_count > 1) { 2427 nvme_quiesce_io_queues(ctrl); 2428 nvme_sync_io_queues(ctrl); 2429 nvme_tcp_stop_io_queues(ctrl); 2430 nvme_cancel_tagset(ctrl); 2431 if (new) 2432 nvme_remove_io_tag_set(ctrl); 2433 nvme_tcp_free_io_queues(ctrl); 2434 } 2435 destroy_admin: 2436 nvme_stop_keep_alive(ctrl); 2437 nvme_tcp_teardown_admin_queue(ctrl, new); 2438 return ret; 2439 } 2440 2441 static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work) 2442 { 2443 struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work), 2444 struct nvme_tcp_ctrl, connect_work); 2445 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl; 2446 int ret; 2447 2448 ++ctrl->nr_reconnects; 2449 2450 ret = nvme_tcp_setup_ctrl(ctrl, false); 2451 if (ret) 2452 goto requeue; 2453 2454 dev_info(ctrl->device, "Successfully reconnected (attempt %d/%d)\n", 2455 ctrl->nr_reconnects, ctrl->opts->max_reconnects); 2456 2457 ctrl->nr_reconnects = 0; 2458 2459 return; 2460 2461 requeue: 2462 dev_info(ctrl->device, "Failed reconnect attempt %d/%d\n", 2463 ctrl->nr_reconnects, ctrl->opts->max_reconnects); 2464 nvme_tcp_reconnect_or_remove(ctrl, ret); 2465 } 2466 2467 static void nvme_tcp_error_recovery_work(struct work_struct *work) 2468 { 2469 struct nvme_tcp_ctrl *tcp_ctrl = container_of(work, 2470 struct nvme_tcp_ctrl, err_work); 2471 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl; 2472 2473 if (nvme_tcp_key_revoke_needed(ctrl)) 2474 nvme_auth_revoke_tls_key(ctrl); 2475 nvme_stop_keep_alive(ctrl); 2476 flush_work(&ctrl->async_event_work); 2477 nvme_tcp_teardown_io_queues(ctrl, false); 2478 /* unquiesce to fail fast pending requests */ 2479 nvme_unquiesce_io_queues(ctrl); 2480 nvme_tcp_teardown_admin_queue(ctrl, false); 2481 nvme_unquiesce_admin_queue(ctrl); 2482 nvme_auth_stop(ctrl); 2483 2484 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) { 2485 /* state change failure is ok if we started ctrl delete */ 2486 enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); 2487 2488 WARN_ON_ONCE(state != NVME_CTRL_DELETING && 2489 state != NVME_CTRL_DELETING_NOIO); 2490 return; 2491 } 2492 2493 nvme_tcp_reconnect_or_remove(ctrl, 0); 2494 } 2495 2496 static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown) 2497 { 2498 nvme_tcp_teardown_io_queues(ctrl, shutdown); 2499 nvme_quiesce_admin_queue(ctrl); 2500 nvme_disable_ctrl(ctrl, shutdown); 2501 nvme_tcp_teardown_admin_queue(ctrl, shutdown); 2502 } 2503 2504 static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl) 2505 { 2506 nvme_tcp_teardown_ctrl(ctrl, true); 2507 } 2508 2509 static void nvme_reset_ctrl_work(struct work_struct *work) 2510 { 2511 struct nvme_ctrl *ctrl = 2512 container_of(work, struct nvme_ctrl, reset_work); 2513 int ret; 2514 2515 if (nvme_tcp_key_revoke_needed(ctrl)) 2516 nvme_auth_revoke_tls_key(ctrl); 2517 nvme_stop_ctrl(ctrl); 2518 nvme_tcp_teardown_ctrl(ctrl, false); 2519 2520 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) { 2521 /* state change failure is ok if we started ctrl delete */ 2522 enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); 2523 2524 WARN_ON_ONCE(state != NVME_CTRL_DELETING && 2525 state != NVME_CTRL_DELETING_NOIO); 2526 return; 2527 } 2528 2529 ret = nvme_tcp_setup_ctrl(ctrl, false); 2530 if (ret) 2531 goto out_fail; 2532 2533 return; 2534 2535 out_fail: 2536 ++ctrl->nr_reconnects; 2537 nvme_tcp_reconnect_or_remove(ctrl, ret); 2538 } 2539 2540 static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl) 2541 { 2542 flush_work(&to_tcp_ctrl(ctrl)->err_work); 2543 cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work); 2544 } 2545 2546 static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl) 2547 { 2548 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 2549 2550 if (list_empty(&ctrl->list)) 2551 goto free_ctrl; 2552 2553 mutex_lock(&nvme_tcp_ctrl_mutex); 2554 list_del(&ctrl->list); 2555 mutex_unlock(&nvme_tcp_ctrl_mutex); 2556 2557 nvmf_free_options(nctrl->opts); 2558 free_ctrl: 2559 kfree(ctrl->queues); 2560 kfree(ctrl); 2561 } 2562 2563 static void nvme_tcp_set_sg_null(struct nvme_command *c) 2564 { 2565 struct nvme_sgl_desc *sg = &c->common.dptr.sgl; 2566 2567 sg->addr = 0; 2568 sg->length = 0; 2569 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) | 2570 NVME_SGL_FMT_TRANSPORT_A; 2571 } 2572 2573 static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue, 2574 struct nvme_command *c, u32 data_len) 2575 { 2576 struct nvme_sgl_desc *sg = &c->common.dptr.sgl; 2577 2578 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); 2579 sg->length = cpu_to_le32(data_len); 2580 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; 2581 } 2582 2583 static void nvme_tcp_set_sg_host_data(struct nvme_command *c, 2584 u32 data_len) 2585 { 2586 struct nvme_sgl_desc *sg = &c->common.dptr.sgl; 2587 2588 sg->addr = 0; 2589 sg->length = cpu_to_le32(data_len); 2590 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) | 2591 NVME_SGL_FMT_TRANSPORT_A; 2592 } 2593 2594 static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg) 2595 { 2596 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg); 2597 struct nvme_tcp_queue *queue = &ctrl->queues[0]; 2598 struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu; 2599 struct nvme_command *cmd = &pdu->cmd; 2600 u8 hdgst = nvme_tcp_hdgst_len(queue); 2601 2602 memset(pdu, 0, sizeof(*pdu)); 2603 pdu->hdr.type = nvme_tcp_cmd; 2604 if (queue->hdr_digest) 2605 pdu->hdr.flags |= NVME_TCP_F_HDGST; 2606 pdu->hdr.hlen = sizeof(*pdu); 2607 pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst); 2608 2609 cmd->common.opcode = nvme_admin_async_event; 2610 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH; 2611 cmd->common.flags |= NVME_CMD_SGL_METABUF; 2612 nvme_tcp_set_sg_null(cmd); 2613 2614 ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU; 2615 ctrl->async_req.offset = 0; 2616 ctrl->async_req.curr_bio = NULL; 2617 ctrl->async_req.data_len = 0; 2618 init_llist_node(&ctrl->async_req.lentry); 2619 INIT_LIST_HEAD(&ctrl->async_req.entry); 2620 2621 nvme_tcp_queue_request(&ctrl->async_req, true); 2622 } 2623 2624 static void nvme_tcp_complete_timed_out(struct request *rq) 2625 { 2626 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 2627 struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl; 2628 2629 nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue)); 2630 nvmf_complete_timed_out_request(rq); 2631 } 2632 2633 static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq) 2634 { 2635 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 2636 struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl; 2637 struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); 2638 struct nvme_command *cmd = &pdu->cmd; 2639 int qid = nvme_tcp_queue_id(req->queue); 2640 2641 dev_warn(ctrl->device, 2642 "I/O tag %d (%04x) type %d opcode %#x (%s) QID %d timeout\n", 2643 rq->tag, nvme_cid(rq), pdu->hdr.type, cmd->common.opcode, 2644 nvme_fabrics_opcode_str(qid, cmd), qid); 2645 2646 if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) { 2647 /* 2648 * If we are resetting, connecting or deleting we should 2649 * complete immediately because we may block controller 2650 * teardown or setup sequence 2651 * - ctrl disable/shutdown fabrics requests 2652 * - connect requests 2653 * - initialization admin requests 2654 * - I/O requests that entered after unquiescing and 2655 * the controller stopped responding 2656 * 2657 * All other requests should be cancelled by the error 2658 * recovery work, so it's fine that we fail it here. 2659 */ 2660 nvme_tcp_complete_timed_out(rq); 2661 return BLK_EH_DONE; 2662 } 2663 2664 /* 2665 * LIVE state should trigger the normal error recovery which will 2666 * handle completing this request. 2667 */ 2668 nvme_tcp_error_recovery(ctrl); 2669 return BLK_EH_RESET_TIMER; 2670 } 2671 2672 static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue, 2673 struct request *rq) 2674 { 2675 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 2676 struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); 2677 struct nvme_command *c = &pdu->cmd; 2678 2679 c->common.flags |= NVME_CMD_SGL_METABUF; 2680 2681 if (!blk_rq_nr_phys_segments(rq)) 2682 nvme_tcp_set_sg_null(c); 2683 else if (rq_data_dir(rq) == WRITE && 2684 req->data_len <= nvme_tcp_inline_data_size(req)) 2685 nvme_tcp_set_sg_inline(queue, c, req->data_len); 2686 else 2687 nvme_tcp_set_sg_host_data(c, req->data_len); 2688 2689 return 0; 2690 } 2691 2692 static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns, 2693 struct request *rq) 2694 { 2695 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 2696 struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); 2697 struct nvme_tcp_queue *queue = req->queue; 2698 u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0; 2699 blk_status_t ret; 2700 2701 ret = nvme_setup_cmd(ns, rq); 2702 if (ret) 2703 return ret; 2704 2705 req->state = NVME_TCP_SEND_CMD_PDU; 2706 req->status = cpu_to_le16(NVME_SC_SUCCESS); 2707 req->offset = 0; 2708 req->data_sent = 0; 2709 req->pdu_len = 0; 2710 req->pdu_sent = 0; 2711 req->h2cdata_left = 0; 2712 req->data_len = blk_rq_nr_phys_segments(rq) ? 2713 blk_rq_payload_bytes(rq) : 0; 2714 req->curr_bio = rq->bio; 2715 if (req->curr_bio && req->data_len) 2716 nvme_tcp_init_iter(req, rq_data_dir(rq)); 2717 2718 if (rq_data_dir(rq) == WRITE && 2719 req->data_len <= nvme_tcp_inline_data_size(req)) 2720 req->pdu_len = req->data_len; 2721 2722 pdu->hdr.type = nvme_tcp_cmd; 2723 pdu->hdr.flags = 0; 2724 if (queue->hdr_digest) 2725 pdu->hdr.flags |= NVME_TCP_F_HDGST; 2726 if (queue->data_digest && req->pdu_len) { 2727 pdu->hdr.flags |= NVME_TCP_F_DDGST; 2728 ddgst = nvme_tcp_ddgst_len(queue); 2729 } 2730 pdu->hdr.hlen = sizeof(*pdu); 2731 pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0; 2732 pdu->hdr.plen = 2733 cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst); 2734 2735 ret = nvme_tcp_map_data(queue, rq); 2736 if (unlikely(ret)) { 2737 nvme_cleanup_cmd(rq); 2738 dev_err(queue->ctrl->ctrl.device, 2739 "Failed to map data (%d)\n", ret); 2740 return ret; 2741 } 2742 2743 return 0; 2744 } 2745 2746 static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx) 2747 { 2748 struct nvme_tcp_queue *queue = hctx->driver_data; 2749 2750 if (!llist_empty(&queue->req_list)) 2751 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); 2752 } 2753 2754 static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx, 2755 const struct blk_mq_queue_data *bd) 2756 { 2757 struct nvme_ns *ns = hctx->queue->queuedata; 2758 struct nvme_tcp_queue *queue = hctx->driver_data; 2759 struct request *rq = bd->rq; 2760 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 2761 bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags); 2762 blk_status_t ret; 2763 2764 if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready)) 2765 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq); 2766 2767 ret = nvme_tcp_setup_cmd_pdu(ns, rq); 2768 if (unlikely(ret)) 2769 return ret; 2770 2771 nvme_start_request(rq); 2772 2773 nvme_tcp_queue_request(req, bd->last); 2774 2775 return BLK_STS_OK; 2776 } 2777 2778 static void nvme_tcp_map_queues(struct blk_mq_tag_set *set) 2779 { 2780 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data); 2781 2782 nvmf_map_queues(set, &ctrl->ctrl, ctrl->io_queues); 2783 } 2784 2785 static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob) 2786 { 2787 struct nvme_tcp_queue *queue = hctx->driver_data; 2788 struct sock *sk = queue->sock->sk; 2789 int ret; 2790 2791 if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags)) 2792 return 0; 2793 2794 set_bit(NVME_TCP_Q_POLLING, &queue->flags); 2795 if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue)) 2796 sk_busy_loop(sk, true); 2797 ret = nvme_tcp_try_recv(queue); 2798 clear_bit(NVME_TCP_Q_POLLING, &queue->flags); 2799 return ret < 0 ? ret : queue->nr_cqe; 2800 } 2801 2802 static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size) 2803 { 2804 struct nvme_tcp_queue *queue = &to_tcp_ctrl(ctrl)->queues[0]; 2805 struct sockaddr_storage src_addr; 2806 int ret, len; 2807 2808 len = nvmf_get_address(ctrl, buf, size); 2809 2810 if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags)) 2811 return len; 2812 2813 mutex_lock(&queue->queue_lock); 2814 2815 ret = kernel_getsockname(queue->sock, (struct sockaddr *)&src_addr); 2816 if (ret > 0) { 2817 if (len > 0) 2818 len--; /* strip trailing newline */ 2819 len += scnprintf(buf + len, size - len, "%ssrc_addr=%pISc\n", 2820 (len) ? "," : "", &src_addr); 2821 } 2822 2823 mutex_unlock(&queue->queue_lock); 2824 2825 return len; 2826 } 2827 2828 static const struct blk_mq_ops nvme_tcp_mq_ops = { 2829 .queue_rq = nvme_tcp_queue_rq, 2830 .commit_rqs = nvme_tcp_commit_rqs, 2831 .complete = nvme_complete_rq, 2832 .init_request = nvme_tcp_init_request, 2833 .exit_request = nvme_tcp_exit_request, 2834 .init_hctx = nvme_tcp_init_hctx, 2835 .timeout = nvme_tcp_timeout, 2836 .map_queues = nvme_tcp_map_queues, 2837 .poll = nvme_tcp_poll, 2838 }; 2839 2840 static const struct blk_mq_ops nvme_tcp_admin_mq_ops = { 2841 .queue_rq = nvme_tcp_queue_rq, 2842 .complete = nvme_complete_rq, 2843 .init_request = nvme_tcp_init_request, 2844 .exit_request = nvme_tcp_exit_request, 2845 .init_hctx = nvme_tcp_init_admin_hctx, 2846 .timeout = nvme_tcp_timeout, 2847 }; 2848 2849 static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = { 2850 .name = "tcp", 2851 .module = THIS_MODULE, 2852 .flags = NVME_F_FABRICS | NVME_F_BLOCKING, 2853 .reg_read32 = nvmf_reg_read32, 2854 .reg_read64 = nvmf_reg_read64, 2855 .reg_write32 = nvmf_reg_write32, 2856 .subsystem_reset = nvmf_subsystem_reset, 2857 .free_ctrl = nvme_tcp_free_ctrl, 2858 .submit_async_event = nvme_tcp_submit_async_event, 2859 .delete_ctrl = nvme_tcp_delete_ctrl, 2860 .get_address = nvme_tcp_get_address, 2861 .stop_ctrl = nvme_tcp_stop_ctrl, 2862 }; 2863 2864 static bool 2865 nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts) 2866 { 2867 struct nvme_tcp_ctrl *ctrl; 2868 bool found = false; 2869 2870 mutex_lock(&nvme_tcp_ctrl_mutex); 2871 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) { 2872 found = nvmf_ip_options_match(&ctrl->ctrl, opts); 2873 if (found) 2874 break; 2875 } 2876 mutex_unlock(&nvme_tcp_ctrl_mutex); 2877 2878 return found; 2879 } 2880 2881 static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev, 2882 struct nvmf_ctrl_options *opts) 2883 { 2884 struct nvme_tcp_ctrl *ctrl; 2885 int ret; 2886 2887 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); 2888 if (!ctrl) 2889 return ERR_PTR(-ENOMEM); 2890 2891 INIT_LIST_HEAD(&ctrl->list); 2892 ctrl->ctrl.opts = opts; 2893 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues + 2894 opts->nr_poll_queues + 1; 2895 ctrl->ctrl.sqsize = opts->queue_size - 1; 2896 ctrl->ctrl.kato = opts->kato; 2897 2898 INIT_DELAYED_WORK(&ctrl->connect_work, 2899 nvme_tcp_reconnect_ctrl_work); 2900 INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work); 2901 INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work); 2902 2903 if (!(opts->mask & NVMF_OPT_TRSVCID)) { 2904 opts->trsvcid = 2905 kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL); 2906 if (!opts->trsvcid) { 2907 ret = -ENOMEM; 2908 goto out_free_ctrl; 2909 } 2910 opts->mask |= NVMF_OPT_TRSVCID; 2911 } 2912 2913 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 2914 opts->traddr, opts->trsvcid, &ctrl->addr); 2915 if (ret) { 2916 pr_err("malformed address passed: %s:%s\n", 2917 opts->traddr, opts->trsvcid); 2918 goto out_free_ctrl; 2919 } 2920 2921 if (opts->mask & NVMF_OPT_HOST_TRADDR) { 2922 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 2923 opts->host_traddr, NULL, &ctrl->src_addr); 2924 if (ret) { 2925 pr_err("malformed src address passed: %s\n", 2926 opts->host_traddr); 2927 goto out_free_ctrl; 2928 } 2929 } 2930 2931 if (opts->mask & NVMF_OPT_HOST_IFACE) { 2932 if (!__dev_get_by_name(&init_net, opts->host_iface)) { 2933 pr_err("invalid interface passed: %s\n", 2934 opts->host_iface); 2935 ret = -ENODEV; 2936 goto out_free_ctrl; 2937 } 2938 } 2939 2940 if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) { 2941 ret = -EALREADY; 2942 goto out_free_ctrl; 2943 } 2944 2945 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues), 2946 GFP_KERNEL); 2947 if (!ctrl->queues) { 2948 ret = -ENOMEM; 2949 goto out_free_ctrl; 2950 } 2951 2952 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0); 2953 if (ret) 2954 goto out_kfree_queues; 2955 2956 return ctrl; 2957 out_kfree_queues: 2958 kfree(ctrl->queues); 2959 out_free_ctrl: 2960 kfree(ctrl); 2961 return ERR_PTR(ret); 2962 } 2963 2964 static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev, 2965 struct nvmf_ctrl_options *opts) 2966 { 2967 struct nvme_tcp_ctrl *ctrl; 2968 int ret; 2969 2970 ctrl = nvme_tcp_alloc_ctrl(dev, opts); 2971 if (IS_ERR(ctrl)) 2972 return ERR_CAST(ctrl); 2973 2974 ret = nvme_add_ctrl(&ctrl->ctrl); 2975 if (ret) 2976 goto out_put_ctrl; 2977 2978 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { 2979 WARN_ON_ONCE(1); 2980 ret = -EINTR; 2981 goto out_uninit_ctrl; 2982 } 2983 2984 ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true); 2985 if (ret) 2986 goto out_uninit_ctrl; 2987 2988 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp, hostnqn: %s\n", 2989 nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr, opts->host->nqn); 2990 2991 mutex_lock(&nvme_tcp_ctrl_mutex); 2992 list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list); 2993 mutex_unlock(&nvme_tcp_ctrl_mutex); 2994 2995 return &ctrl->ctrl; 2996 2997 out_uninit_ctrl: 2998 nvme_uninit_ctrl(&ctrl->ctrl); 2999 out_put_ctrl: 3000 nvme_put_ctrl(&ctrl->ctrl); 3001 if (ret > 0) 3002 ret = -EIO; 3003 return ERR_PTR(ret); 3004 } 3005 3006 static struct nvmf_transport_ops nvme_tcp_transport = { 3007 .name = "tcp", 3008 .module = THIS_MODULE, 3009 .required_opts = NVMF_OPT_TRADDR, 3010 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY | 3011 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO | 3012 NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST | 3013 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES | 3014 NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS | 3015 NVMF_OPT_KEYRING | NVMF_OPT_TLS_KEY | NVMF_OPT_CONCAT, 3016 .create_ctrl = nvme_tcp_create_ctrl, 3017 }; 3018 3019 static int __init nvme_tcp_init_module(void) 3020 { 3021 unsigned int wq_flags = WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_SYSFS; 3022 int cpu; 3023 3024 BUILD_BUG_ON(sizeof(struct nvme_tcp_hdr) != 8); 3025 BUILD_BUG_ON(sizeof(struct nvme_tcp_cmd_pdu) != 72); 3026 BUILD_BUG_ON(sizeof(struct nvme_tcp_data_pdu) != 24); 3027 BUILD_BUG_ON(sizeof(struct nvme_tcp_rsp_pdu) != 24); 3028 BUILD_BUG_ON(sizeof(struct nvme_tcp_r2t_pdu) != 24); 3029 BUILD_BUG_ON(sizeof(struct nvme_tcp_icreq_pdu) != 128); 3030 BUILD_BUG_ON(sizeof(struct nvme_tcp_icresp_pdu) != 128); 3031 BUILD_BUG_ON(sizeof(struct nvme_tcp_term_pdu) != 24); 3032 3033 if (wq_unbound) 3034 wq_flags |= WQ_UNBOUND; 3035 3036 nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq", wq_flags, 0); 3037 if (!nvme_tcp_wq) 3038 return -ENOMEM; 3039 3040 for_each_possible_cpu(cpu) 3041 atomic_set(&nvme_tcp_cpu_queues[cpu], 0); 3042 3043 nvmf_register_transport(&nvme_tcp_transport); 3044 return 0; 3045 } 3046 3047 static void __exit nvme_tcp_cleanup_module(void) 3048 { 3049 struct nvme_tcp_ctrl *ctrl; 3050 3051 nvmf_unregister_transport(&nvme_tcp_transport); 3052 3053 mutex_lock(&nvme_tcp_ctrl_mutex); 3054 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) 3055 nvme_delete_ctrl(&ctrl->ctrl); 3056 mutex_unlock(&nvme_tcp_ctrl_mutex); 3057 flush_workqueue(nvme_delete_wq); 3058 3059 destroy_workqueue(nvme_tcp_wq); 3060 } 3061 3062 module_init(nvme_tcp_init_module); 3063 module_exit(nvme_tcp_cleanup_module); 3064 3065 MODULE_DESCRIPTION("NVMe host TCP transport driver"); 3066 MODULE_LICENSE("GPL v2"); 3067