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