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