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