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