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 void nvme_tcp_handle_c2h_term(struct nvme_tcp_queue *queue, 767 struct nvme_tcp_term_pdu *pdu) 768 { 769 u16 fes; 770 const char *msg; 771 u32 plen = le32_to_cpu(pdu->hdr.plen); 772 773 static const char * const msg_table[] = { 774 [NVME_TCP_FES_INVALID_PDU_HDR] = "Invalid PDU Header Field", 775 [NVME_TCP_FES_PDU_SEQ_ERR] = "PDU Sequence Error", 776 [NVME_TCP_FES_HDR_DIGEST_ERR] = "Header Digest Error", 777 [NVME_TCP_FES_DATA_OUT_OF_RANGE] = "Data Transfer Out Of Range", 778 [NVME_TCP_FES_R2T_LIMIT_EXCEEDED] = "R2T Limit Exceeded", 779 [NVME_TCP_FES_UNSUPPORTED_PARAM] = "Unsupported Parameter", 780 }; 781 782 if (plen < NVME_TCP_MIN_C2HTERM_PLEN || 783 plen > NVME_TCP_MAX_C2HTERM_PLEN) { 784 dev_err(queue->ctrl->ctrl.device, 785 "Received a malformed C2HTermReq PDU (plen = %u)\n", 786 plen); 787 return; 788 } 789 790 fes = le16_to_cpu(pdu->fes); 791 if (fes && fes < ARRAY_SIZE(msg_table)) 792 msg = msg_table[fes]; 793 else 794 msg = "Unknown"; 795 796 dev_err(queue->ctrl->ctrl.device, 797 "Received C2HTermReq (FES = %s)\n", msg); 798 } 799 800 static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb, 801 unsigned int *offset, size_t *len) 802 { 803 struct nvme_tcp_hdr *hdr; 804 char *pdu = queue->pdu; 805 size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining); 806 int ret; 807 808 ret = skb_copy_bits(skb, *offset, 809 &pdu[queue->pdu_offset], rcv_len); 810 if (unlikely(ret)) 811 return ret; 812 813 queue->pdu_remaining -= rcv_len; 814 queue->pdu_offset += rcv_len; 815 *offset += rcv_len; 816 *len -= rcv_len; 817 if (queue->pdu_remaining) 818 return 0; 819 820 hdr = queue->pdu; 821 if (unlikely(hdr->type == nvme_tcp_c2h_term)) { 822 /* 823 * C2HTermReq never includes Header or Data digests. 824 * Skip the checks. 825 */ 826 nvme_tcp_handle_c2h_term(queue, (void *)queue->pdu); 827 return -EINVAL; 828 } 829 830 if (queue->hdr_digest) { 831 ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen); 832 if (unlikely(ret)) 833 return ret; 834 } 835 836 837 if (queue->data_digest) { 838 ret = nvme_tcp_check_ddgst(queue, queue->pdu); 839 if (unlikely(ret)) 840 return ret; 841 } 842 843 switch (hdr->type) { 844 case nvme_tcp_c2h_data: 845 return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu); 846 case nvme_tcp_rsp: 847 nvme_tcp_init_recv_ctx(queue); 848 return nvme_tcp_handle_comp(queue, (void *)queue->pdu); 849 case nvme_tcp_r2t: 850 nvme_tcp_init_recv_ctx(queue); 851 return nvme_tcp_handle_r2t(queue, (void *)queue->pdu); 852 default: 853 dev_err(queue->ctrl->ctrl.device, 854 "unsupported pdu type (%d)\n", hdr->type); 855 return -EINVAL; 856 } 857 } 858 859 static inline void nvme_tcp_end_request(struct request *rq, u16 status) 860 { 861 union nvme_result res = {}; 862 863 if (!nvme_try_complete_req(rq, cpu_to_le16(status << 1), res)) 864 nvme_complete_rq(rq); 865 } 866 867 static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb, 868 unsigned int *offset, size_t *len) 869 { 870 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu; 871 struct request *rq = 872 nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id); 873 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 874 875 while (true) { 876 int recv_len, ret; 877 878 recv_len = min_t(size_t, *len, queue->data_remaining); 879 if (!recv_len) 880 break; 881 882 if (!iov_iter_count(&req->iter)) { 883 req->curr_bio = req->curr_bio->bi_next; 884 885 /* 886 * If we don`t have any bios it means that controller 887 * sent more data than we requested, hence error 888 */ 889 if (!req->curr_bio) { 890 dev_err(queue->ctrl->ctrl.device, 891 "queue %d no space in request %#x", 892 nvme_tcp_queue_id(queue), rq->tag); 893 nvme_tcp_init_recv_ctx(queue); 894 return -EIO; 895 } 896 nvme_tcp_init_iter(req, ITER_DEST); 897 } 898 899 /* we can read only from what is left in this bio */ 900 recv_len = min_t(size_t, recv_len, 901 iov_iter_count(&req->iter)); 902 903 if (queue->data_digest) 904 ret = skb_copy_and_hash_datagram_iter(skb, *offset, 905 &req->iter, recv_len, queue->rcv_hash); 906 else 907 ret = skb_copy_datagram_iter(skb, *offset, 908 &req->iter, recv_len); 909 if (ret) { 910 dev_err(queue->ctrl->ctrl.device, 911 "queue %d failed to copy request %#x data", 912 nvme_tcp_queue_id(queue), rq->tag); 913 return ret; 914 } 915 916 *len -= recv_len; 917 *offset += recv_len; 918 queue->data_remaining -= recv_len; 919 } 920 921 if (!queue->data_remaining) { 922 if (queue->data_digest) { 923 nvme_tcp_ddgst_final(queue->rcv_hash, &queue->exp_ddgst); 924 queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH; 925 } else { 926 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) { 927 nvme_tcp_end_request(rq, 928 le16_to_cpu(req->status)); 929 queue->nr_cqe++; 930 } 931 nvme_tcp_init_recv_ctx(queue); 932 } 933 } 934 935 return 0; 936 } 937 938 static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue, 939 struct sk_buff *skb, unsigned int *offset, size_t *len) 940 { 941 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu; 942 char *ddgst = (char *)&queue->recv_ddgst; 943 size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining); 944 off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining; 945 int ret; 946 947 ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len); 948 if (unlikely(ret)) 949 return ret; 950 951 queue->ddgst_remaining -= recv_len; 952 *offset += recv_len; 953 *len -= recv_len; 954 if (queue->ddgst_remaining) 955 return 0; 956 957 if (queue->recv_ddgst != queue->exp_ddgst) { 958 struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue), 959 pdu->command_id); 960 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 961 962 req->status = cpu_to_le16(NVME_SC_DATA_XFER_ERROR); 963 964 dev_err(queue->ctrl->ctrl.device, 965 "data digest error: recv %#x expected %#x\n", 966 le32_to_cpu(queue->recv_ddgst), 967 le32_to_cpu(queue->exp_ddgst)); 968 } 969 970 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) { 971 struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue), 972 pdu->command_id); 973 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 974 975 nvme_tcp_end_request(rq, le16_to_cpu(req->status)); 976 queue->nr_cqe++; 977 } 978 979 nvme_tcp_init_recv_ctx(queue); 980 return 0; 981 } 982 983 static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb, 984 unsigned int offset, size_t len) 985 { 986 struct nvme_tcp_queue *queue = desc->arg.data; 987 size_t consumed = len; 988 int result; 989 990 if (unlikely(!queue->rd_enabled)) 991 return -EFAULT; 992 993 while (len) { 994 switch (nvme_tcp_recv_state(queue)) { 995 case NVME_TCP_RECV_PDU: 996 result = nvme_tcp_recv_pdu(queue, skb, &offset, &len); 997 break; 998 case NVME_TCP_RECV_DATA: 999 result = nvme_tcp_recv_data(queue, skb, &offset, &len); 1000 break; 1001 case NVME_TCP_RECV_DDGST: 1002 result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len); 1003 break; 1004 default: 1005 result = -EFAULT; 1006 } 1007 if (result) { 1008 dev_err(queue->ctrl->ctrl.device, 1009 "receive failed: %d\n", result); 1010 queue->rd_enabled = false; 1011 nvme_tcp_error_recovery(&queue->ctrl->ctrl); 1012 return result; 1013 } 1014 } 1015 1016 return consumed; 1017 } 1018 1019 static void nvme_tcp_data_ready(struct sock *sk) 1020 { 1021 struct nvme_tcp_queue *queue; 1022 1023 trace_sk_data_ready(sk); 1024 1025 read_lock_bh(&sk->sk_callback_lock); 1026 queue = sk->sk_user_data; 1027 if (likely(queue && queue->rd_enabled) && 1028 !test_bit(NVME_TCP_Q_POLLING, &queue->flags)) 1029 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); 1030 read_unlock_bh(&sk->sk_callback_lock); 1031 } 1032 1033 static void nvme_tcp_write_space(struct sock *sk) 1034 { 1035 struct nvme_tcp_queue *queue; 1036 1037 read_lock_bh(&sk->sk_callback_lock); 1038 queue = sk->sk_user_data; 1039 if (likely(queue && sk_stream_is_writeable(sk))) { 1040 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1041 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); 1042 } 1043 read_unlock_bh(&sk->sk_callback_lock); 1044 } 1045 1046 static void nvme_tcp_state_change(struct sock *sk) 1047 { 1048 struct nvme_tcp_queue *queue; 1049 1050 read_lock_bh(&sk->sk_callback_lock); 1051 queue = sk->sk_user_data; 1052 if (!queue) 1053 goto done; 1054 1055 switch (sk->sk_state) { 1056 case TCP_CLOSE: 1057 case TCP_CLOSE_WAIT: 1058 case TCP_LAST_ACK: 1059 case TCP_FIN_WAIT1: 1060 case TCP_FIN_WAIT2: 1061 nvme_tcp_error_recovery(&queue->ctrl->ctrl); 1062 break; 1063 default: 1064 dev_info(queue->ctrl->ctrl.device, 1065 "queue %d socket state %d\n", 1066 nvme_tcp_queue_id(queue), sk->sk_state); 1067 } 1068 1069 queue->state_change(sk); 1070 done: 1071 read_unlock_bh(&sk->sk_callback_lock); 1072 } 1073 1074 static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue) 1075 { 1076 queue->request = NULL; 1077 } 1078 1079 static void nvme_tcp_fail_request(struct nvme_tcp_request *req) 1080 { 1081 if (nvme_tcp_async_req(req)) { 1082 union nvme_result res = {}; 1083 1084 nvme_complete_async_event(&req->queue->ctrl->ctrl, 1085 cpu_to_le16(NVME_SC_HOST_PATH_ERROR), &res); 1086 } else { 1087 nvme_tcp_end_request(blk_mq_rq_from_pdu(req), 1088 NVME_SC_HOST_PATH_ERROR); 1089 } 1090 } 1091 1092 static int nvme_tcp_try_send_data(struct nvme_tcp_request *req) 1093 { 1094 struct nvme_tcp_queue *queue = req->queue; 1095 int req_data_len = req->data_len; 1096 u32 h2cdata_left = req->h2cdata_left; 1097 1098 while (true) { 1099 struct bio_vec bvec; 1100 struct msghdr msg = { 1101 .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES, 1102 }; 1103 struct page *page = nvme_tcp_req_cur_page(req); 1104 size_t offset = nvme_tcp_req_cur_offset(req); 1105 size_t len = nvme_tcp_req_cur_length(req); 1106 bool last = nvme_tcp_pdu_last_send(req, len); 1107 int req_data_sent = req->data_sent; 1108 int ret; 1109 1110 if (last && !queue->data_digest && !nvme_tcp_queue_more(queue)) 1111 msg.msg_flags |= MSG_EOR; 1112 else 1113 msg.msg_flags |= MSG_MORE; 1114 1115 if (!sendpages_ok(page, len, offset)) 1116 msg.msg_flags &= ~MSG_SPLICE_PAGES; 1117 1118 bvec_set_page(&bvec, page, len, offset); 1119 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len); 1120 ret = sock_sendmsg(queue->sock, &msg); 1121 if (ret <= 0) 1122 return ret; 1123 1124 if (queue->data_digest) 1125 nvme_tcp_ddgst_update(queue->snd_hash, page, 1126 offset, ret); 1127 1128 /* 1129 * update the request iterator except for the last payload send 1130 * in the request where we don't want to modify it as we may 1131 * compete with the RX path completing the request. 1132 */ 1133 if (req_data_sent + ret < req_data_len) 1134 nvme_tcp_advance_req(req, ret); 1135 1136 /* fully successful last send in current PDU */ 1137 if (last && ret == len) { 1138 if (queue->data_digest) { 1139 nvme_tcp_ddgst_final(queue->snd_hash, 1140 &req->ddgst); 1141 req->state = NVME_TCP_SEND_DDGST; 1142 req->offset = 0; 1143 } else { 1144 if (h2cdata_left) 1145 nvme_tcp_setup_h2c_data_pdu(req); 1146 else 1147 nvme_tcp_done_send_req(queue); 1148 } 1149 return 1; 1150 } 1151 } 1152 return -EAGAIN; 1153 } 1154 1155 static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req) 1156 { 1157 struct nvme_tcp_queue *queue = req->queue; 1158 struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); 1159 struct bio_vec bvec; 1160 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES, }; 1161 bool inline_data = nvme_tcp_has_inline_data(req); 1162 u8 hdgst = nvme_tcp_hdgst_len(queue); 1163 int len = sizeof(*pdu) + hdgst - req->offset; 1164 int ret; 1165 1166 if (inline_data || nvme_tcp_queue_more(queue)) 1167 msg.msg_flags |= MSG_MORE; 1168 else 1169 msg.msg_flags |= MSG_EOR; 1170 1171 if (queue->hdr_digest && !req->offset) 1172 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu)); 1173 1174 bvec_set_virt(&bvec, (void *)pdu + req->offset, len); 1175 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len); 1176 ret = sock_sendmsg(queue->sock, &msg); 1177 if (unlikely(ret <= 0)) 1178 return ret; 1179 1180 len -= ret; 1181 if (!len) { 1182 if (inline_data) { 1183 req->state = NVME_TCP_SEND_DATA; 1184 if (queue->data_digest) 1185 crypto_ahash_init(queue->snd_hash); 1186 } else { 1187 nvme_tcp_done_send_req(queue); 1188 } 1189 return 1; 1190 } 1191 req->offset += ret; 1192 1193 return -EAGAIN; 1194 } 1195 1196 static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req) 1197 { 1198 struct nvme_tcp_queue *queue = req->queue; 1199 struct nvme_tcp_data_pdu *pdu = nvme_tcp_req_data_pdu(req); 1200 struct bio_vec bvec; 1201 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_MORE, }; 1202 u8 hdgst = nvme_tcp_hdgst_len(queue); 1203 int len = sizeof(*pdu) - req->offset + hdgst; 1204 int ret; 1205 1206 if (queue->hdr_digest && !req->offset) 1207 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu)); 1208 1209 if (!req->h2cdata_left) 1210 msg.msg_flags |= MSG_SPLICE_PAGES; 1211 1212 bvec_set_virt(&bvec, (void *)pdu + req->offset, len); 1213 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len); 1214 ret = sock_sendmsg(queue->sock, &msg); 1215 if (unlikely(ret <= 0)) 1216 return ret; 1217 1218 len -= ret; 1219 if (!len) { 1220 req->state = NVME_TCP_SEND_DATA; 1221 if (queue->data_digest) 1222 crypto_ahash_init(queue->snd_hash); 1223 return 1; 1224 } 1225 req->offset += ret; 1226 1227 return -EAGAIN; 1228 } 1229 1230 static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req) 1231 { 1232 struct nvme_tcp_queue *queue = req->queue; 1233 size_t offset = req->offset; 1234 u32 h2cdata_left = req->h2cdata_left; 1235 int ret; 1236 struct msghdr msg = { .msg_flags = MSG_DONTWAIT }; 1237 struct kvec iov = { 1238 .iov_base = (u8 *)&req->ddgst + req->offset, 1239 .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset 1240 }; 1241 1242 if (nvme_tcp_queue_more(queue)) 1243 msg.msg_flags |= MSG_MORE; 1244 else 1245 msg.msg_flags |= MSG_EOR; 1246 1247 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len); 1248 if (unlikely(ret <= 0)) 1249 return ret; 1250 1251 if (offset + ret == NVME_TCP_DIGEST_LENGTH) { 1252 if (h2cdata_left) 1253 nvme_tcp_setup_h2c_data_pdu(req); 1254 else 1255 nvme_tcp_done_send_req(queue); 1256 return 1; 1257 } 1258 1259 req->offset += ret; 1260 return -EAGAIN; 1261 } 1262 1263 static int nvme_tcp_try_send(struct nvme_tcp_queue *queue) 1264 { 1265 struct nvme_tcp_request *req; 1266 unsigned int noreclaim_flag; 1267 int ret = 1; 1268 1269 if (!queue->request) { 1270 queue->request = nvme_tcp_fetch_request(queue); 1271 if (!queue->request) 1272 return 0; 1273 } 1274 req = queue->request; 1275 1276 noreclaim_flag = memalloc_noreclaim_save(); 1277 if (req->state == NVME_TCP_SEND_CMD_PDU) { 1278 ret = nvme_tcp_try_send_cmd_pdu(req); 1279 if (ret <= 0) 1280 goto done; 1281 if (!nvme_tcp_has_inline_data(req)) 1282 goto out; 1283 } 1284 1285 if (req->state == NVME_TCP_SEND_H2C_PDU) { 1286 ret = nvme_tcp_try_send_data_pdu(req); 1287 if (ret <= 0) 1288 goto done; 1289 } 1290 1291 if (req->state == NVME_TCP_SEND_DATA) { 1292 ret = nvme_tcp_try_send_data(req); 1293 if (ret <= 0) 1294 goto done; 1295 } 1296 1297 if (req->state == NVME_TCP_SEND_DDGST) 1298 ret = nvme_tcp_try_send_ddgst(req); 1299 done: 1300 if (ret == -EAGAIN) { 1301 ret = 0; 1302 } else if (ret < 0) { 1303 dev_err(queue->ctrl->ctrl.device, 1304 "failed to send request %d\n", ret); 1305 nvme_tcp_fail_request(queue->request); 1306 nvme_tcp_done_send_req(queue); 1307 } 1308 out: 1309 memalloc_noreclaim_restore(noreclaim_flag); 1310 return ret; 1311 } 1312 1313 static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue) 1314 { 1315 struct socket *sock = queue->sock; 1316 struct sock *sk = sock->sk; 1317 read_descriptor_t rd_desc; 1318 int consumed; 1319 1320 rd_desc.arg.data = queue; 1321 rd_desc.count = 1; 1322 lock_sock(sk); 1323 queue->nr_cqe = 0; 1324 consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb); 1325 release_sock(sk); 1326 return consumed; 1327 } 1328 1329 static void nvme_tcp_io_work(struct work_struct *w) 1330 { 1331 struct nvme_tcp_queue *queue = 1332 container_of(w, struct nvme_tcp_queue, io_work); 1333 unsigned long deadline = jiffies + msecs_to_jiffies(1); 1334 1335 do { 1336 bool pending = false; 1337 int result; 1338 1339 if (mutex_trylock(&queue->send_mutex)) { 1340 result = nvme_tcp_try_send(queue); 1341 mutex_unlock(&queue->send_mutex); 1342 if (result > 0) 1343 pending = true; 1344 else if (unlikely(result < 0)) 1345 break; 1346 } 1347 1348 result = nvme_tcp_try_recv(queue); 1349 if (result > 0) 1350 pending = true; 1351 else if (unlikely(result < 0)) 1352 return; 1353 1354 if (!pending || !queue->rd_enabled) 1355 return; 1356 1357 } while (!time_after(jiffies, deadline)); /* quota is exhausted */ 1358 1359 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); 1360 } 1361 1362 static void nvme_tcp_free_crypto(struct nvme_tcp_queue *queue) 1363 { 1364 struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash); 1365 1366 ahash_request_free(queue->rcv_hash); 1367 ahash_request_free(queue->snd_hash); 1368 crypto_free_ahash(tfm); 1369 } 1370 1371 static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue *queue) 1372 { 1373 struct crypto_ahash *tfm; 1374 1375 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC); 1376 if (IS_ERR(tfm)) 1377 return PTR_ERR(tfm); 1378 1379 queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL); 1380 if (!queue->snd_hash) 1381 goto free_tfm; 1382 ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL); 1383 1384 queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL); 1385 if (!queue->rcv_hash) 1386 goto free_snd_hash; 1387 ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL); 1388 1389 return 0; 1390 free_snd_hash: 1391 ahash_request_free(queue->snd_hash); 1392 free_tfm: 1393 crypto_free_ahash(tfm); 1394 return -ENOMEM; 1395 } 1396 1397 static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl) 1398 { 1399 struct nvme_tcp_request *async = &ctrl->async_req; 1400 1401 page_frag_free(async->pdu); 1402 } 1403 1404 static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl) 1405 { 1406 struct nvme_tcp_queue *queue = &ctrl->queues[0]; 1407 struct nvme_tcp_request *async = &ctrl->async_req; 1408 u8 hdgst = nvme_tcp_hdgst_len(queue); 1409 1410 async->pdu = page_frag_alloc(&queue->pf_cache, 1411 sizeof(struct nvme_tcp_cmd_pdu) + hdgst, 1412 GFP_KERNEL | __GFP_ZERO); 1413 if (!async->pdu) 1414 return -ENOMEM; 1415 1416 async->queue = &ctrl->queues[0]; 1417 return 0; 1418 } 1419 1420 static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid) 1421 { 1422 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 1423 struct nvme_tcp_queue *queue = &ctrl->queues[qid]; 1424 unsigned int noreclaim_flag; 1425 1426 if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) 1427 return; 1428 1429 if (queue->hdr_digest || queue->data_digest) 1430 nvme_tcp_free_crypto(queue); 1431 1432 page_frag_cache_drain(&queue->pf_cache); 1433 1434 noreclaim_flag = memalloc_noreclaim_save(); 1435 /* ->sock will be released by fput() */ 1436 fput(queue->sock->file); 1437 queue->sock = NULL; 1438 memalloc_noreclaim_restore(noreclaim_flag); 1439 1440 kfree(queue->pdu); 1441 mutex_destroy(&queue->send_mutex); 1442 mutex_destroy(&queue->queue_lock); 1443 } 1444 1445 static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue) 1446 { 1447 struct nvme_tcp_icreq_pdu *icreq; 1448 struct nvme_tcp_icresp_pdu *icresp; 1449 char cbuf[CMSG_LEN(sizeof(char))] = {}; 1450 u8 ctype; 1451 struct msghdr msg = {}; 1452 struct kvec iov; 1453 bool ctrl_hdgst, ctrl_ddgst; 1454 u32 maxh2cdata; 1455 int ret; 1456 1457 icreq = kzalloc(sizeof(*icreq), GFP_KERNEL); 1458 if (!icreq) 1459 return -ENOMEM; 1460 1461 icresp = kzalloc(sizeof(*icresp), GFP_KERNEL); 1462 if (!icresp) { 1463 ret = -ENOMEM; 1464 goto free_icreq; 1465 } 1466 1467 icreq->hdr.type = nvme_tcp_icreq; 1468 icreq->hdr.hlen = sizeof(*icreq); 1469 icreq->hdr.pdo = 0; 1470 icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen); 1471 icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0); 1472 icreq->maxr2t = 0; /* single inflight r2t supported */ 1473 icreq->hpda = 0; /* no alignment constraint */ 1474 if (queue->hdr_digest) 1475 icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE; 1476 if (queue->data_digest) 1477 icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE; 1478 1479 iov.iov_base = icreq; 1480 iov.iov_len = sizeof(*icreq); 1481 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len); 1482 if (ret < 0) { 1483 pr_warn("queue %d: failed to send icreq, error %d\n", 1484 nvme_tcp_queue_id(queue), ret); 1485 goto free_icresp; 1486 } 1487 1488 memset(&msg, 0, sizeof(msg)); 1489 iov.iov_base = icresp; 1490 iov.iov_len = sizeof(*icresp); 1491 if (nvme_tcp_queue_tls(queue)) { 1492 msg.msg_control = cbuf; 1493 msg.msg_controllen = sizeof(cbuf); 1494 } 1495 msg.msg_flags = MSG_WAITALL; 1496 ret = kernel_recvmsg(queue->sock, &msg, &iov, 1, 1497 iov.iov_len, msg.msg_flags); 1498 if (ret < sizeof(*icresp)) { 1499 pr_warn("queue %d: failed to receive icresp, error %d\n", 1500 nvme_tcp_queue_id(queue), ret); 1501 if (ret >= 0) 1502 ret = -ECONNRESET; 1503 goto free_icresp; 1504 } 1505 ret = -ENOTCONN; 1506 if (nvme_tcp_queue_tls(queue)) { 1507 ctype = tls_get_record_type(queue->sock->sk, 1508 (struct cmsghdr *)cbuf); 1509 if (ctype != TLS_RECORD_TYPE_DATA) { 1510 pr_err("queue %d: unhandled TLS record %d\n", 1511 nvme_tcp_queue_id(queue), ctype); 1512 goto free_icresp; 1513 } 1514 } 1515 ret = -EINVAL; 1516 if (icresp->hdr.type != nvme_tcp_icresp) { 1517 pr_err("queue %d: bad type returned %d\n", 1518 nvme_tcp_queue_id(queue), icresp->hdr.type); 1519 goto free_icresp; 1520 } 1521 1522 if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) { 1523 pr_err("queue %d: bad pdu length returned %d\n", 1524 nvme_tcp_queue_id(queue), icresp->hdr.plen); 1525 goto free_icresp; 1526 } 1527 1528 if (icresp->pfv != NVME_TCP_PFV_1_0) { 1529 pr_err("queue %d: bad pfv returned %d\n", 1530 nvme_tcp_queue_id(queue), icresp->pfv); 1531 goto free_icresp; 1532 } 1533 1534 ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE); 1535 if ((queue->data_digest && !ctrl_ddgst) || 1536 (!queue->data_digest && ctrl_ddgst)) { 1537 pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n", 1538 nvme_tcp_queue_id(queue), 1539 queue->data_digest ? "enabled" : "disabled", 1540 ctrl_ddgst ? "enabled" : "disabled"); 1541 goto free_icresp; 1542 } 1543 1544 ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE); 1545 if ((queue->hdr_digest && !ctrl_hdgst) || 1546 (!queue->hdr_digest && ctrl_hdgst)) { 1547 pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n", 1548 nvme_tcp_queue_id(queue), 1549 queue->hdr_digest ? "enabled" : "disabled", 1550 ctrl_hdgst ? "enabled" : "disabled"); 1551 goto free_icresp; 1552 } 1553 1554 if (icresp->cpda != 0) { 1555 pr_err("queue %d: unsupported cpda returned %d\n", 1556 nvme_tcp_queue_id(queue), icresp->cpda); 1557 goto free_icresp; 1558 } 1559 1560 maxh2cdata = le32_to_cpu(icresp->maxdata); 1561 if ((maxh2cdata % 4) || (maxh2cdata < NVME_TCP_MIN_MAXH2CDATA)) { 1562 pr_err("queue %d: invalid maxh2cdata returned %u\n", 1563 nvme_tcp_queue_id(queue), maxh2cdata); 1564 goto free_icresp; 1565 } 1566 queue->maxh2cdata = maxh2cdata; 1567 1568 ret = 0; 1569 free_icresp: 1570 kfree(icresp); 1571 free_icreq: 1572 kfree(icreq); 1573 return ret; 1574 } 1575 1576 static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue) 1577 { 1578 return nvme_tcp_queue_id(queue) == 0; 1579 } 1580 1581 static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue) 1582 { 1583 struct nvme_tcp_ctrl *ctrl = queue->ctrl; 1584 int qid = nvme_tcp_queue_id(queue); 1585 1586 return !nvme_tcp_admin_queue(queue) && 1587 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT]; 1588 } 1589 1590 static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue) 1591 { 1592 struct nvme_tcp_ctrl *ctrl = queue->ctrl; 1593 int qid = nvme_tcp_queue_id(queue); 1594 1595 return !nvme_tcp_admin_queue(queue) && 1596 !nvme_tcp_default_queue(queue) && 1597 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] + 1598 ctrl->io_queues[HCTX_TYPE_READ]; 1599 } 1600 1601 static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue) 1602 { 1603 struct nvme_tcp_ctrl *ctrl = queue->ctrl; 1604 int qid = nvme_tcp_queue_id(queue); 1605 1606 return !nvme_tcp_admin_queue(queue) && 1607 !nvme_tcp_default_queue(queue) && 1608 !nvme_tcp_read_queue(queue) && 1609 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] + 1610 ctrl->io_queues[HCTX_TYPE_READ] + 1611 ctrl->io_queues[HCTX_TYPE_POLL]; 1612 } 1613 1614 /* 1615 * Track the number of queues assigned to each cpu using a global per-cpu 1616 * counter and select the least used cpu from the mq_map. Our goal is to spread 1617 * different controllers I/O threads across different cpu cores. 1618 * 1619 * Note that the accounting is not 100% perfect, but we don't need to be, we're 1620 * simply putting our best effort to select the best candidate cpu core that we 1621 * find at any given point. 1622 */ 1623 static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue) 1624 { 1625 struct nvme_tcp_ctrl *ctrl = queue->ctrl; 1626 struct blk_mq_tag_set *set = &ctrl->tag_set; 1627 int qid = nvme_tcp_queue_id(queue) - 1; 1628 unsigned int *mq_map = NULL; 1629 int cpu, min_queues = INT_MAX, io_cpu; 1630 1631 if (wq_unbound) 1632 goto out; 1633 1634 if (nvme_tcp_default_queue(queue)) 1635 mq_map = set->map[HCTX_TYPE_DEFAULT].mq_map; 1636 else if (nvme_tcp_read_queue(queue)) 1637 mq_map = set->map[HCTX_TYPE_READ].mq_map; 1638 else if (nvme_tcp_poll_queue(queue)) 1639 mq_map = set->map[HCTX_TYPE_POLL].mq_map; 1640 1641 if (WARN_ON(!mq_map)) 1642 goto out; 1643 1644 /* Search for the least used cpu from the mq_map */ 1645 io_cpu = WORK_CPU_UNBOUND; 1646 for_each_online_cpu(cpu) { 1647 int num_queues = atomic_read(&nvme_tcp_cpu_queues[cpu]); 1648 1649 if (mq_map[cpu] != qid) 1650 continue; 1651 if (num_queues < min_queues) { 1652 io_cpu = cpu; 1653 min_queues = num_queues; 1654 } 1655 } 1656 if (io_cpu != WORK_CPU_UNBOUND) { 1657 queue->io_cpu = io_cpu; 1658 atomic_inc(&nvme_tcp_cpu_queues[io_cpu]); 1659 set_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags); 1660 } 1661 out: 1662 dev_dbg(ctrl->ctrl.device, "queue %d: using cpu %d\n", 1663 qid, queue->io_cpu); 1664 } 1665 1666 static void nvme_tcp_tls_done(void *data, int status, key_serial_t pskid) 1667 { 1668 struct nvme_tcp_queue *queue = data; 1669 struct nvme_tcp_ctrl *ctrl = queue->ctrl; 1670 int qid = nvme_tcp_queue_id(queue); 1671 struct key *tls_key; 1672 1673 dev_dbg(ctrl->ctrl.device, "queue %d: TLS handshake done, key %x, status %d\n", 1674 qid, pskid, status); 1675 1676 if (status) { 1677 queue->tls_err = -status; 1678 goto out_complete; 1679 } 1680 1681 tls_key = nvme_tls_key_lookup(pskid); 1682 if (IS_ERR(tls_key)) { 1683 dev_warn(ctrl->ctrl.device, "queue %d: Invalid key %x\n", 1684 qid, pskid); 1685 queue->tls_err = -ENOKEY; 1686 } else { 1687 queue->tls_enabled = true; 1688 if (qid == 0) 1689 ctrl->ctrl.tls_pskid = key_serial(tls_key); 1690 key_put(tls_key); 1691 queue->tls_err = 0; 1692 } 1693 1694 out_complete: 1695 complete(&queue->tls_complete); 1696 } 1697 1698 static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl, 1699 struct nvme_tcp_queue *queue, 1700 key_serial_t pskid) 1701 { 1702 int qid = nvme_tcp_queue_id(queue); 1703 int ret; 1704 struct tls_handshake_args args; 1705 unsigned long tmo = tls_handshake_timeout * HZ; 1706 key_serial_t keyring = nvme_keyring_id(); 1707 1708 dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n", 1709 qid, pskid); 1710 memset(&args, 0, sizeof(args)); 1711 args.ta_sock = queue->sock; 1712 args.ta_done = nvme_tcp_tls_done; 1713 args.ta_data = queue; 1714 args.ta_my_peerids[0] = pskid; 1715 args.ta_num_peerids = 1; 1716 if (nctrl->opts->keyring) 1717 keyring = key_serial(nctrl->opts->keyring); 1718 args.ta_keyring = keyring; 1719 args.ta_timeout_ms = tls_handshake_timeout * 1000; 1720 queue->tls_err = -EOPNOTSUPP; 1721 init_completion(&queue->tls_complete); 1722 ret = tls_client_hello_psk(&args, GFP_KERNEL); 1723 if (ret) { 1724 dev_err(nctrl->device, "queue %d: failed to start TLS: %d\n", 1725 qid, ret); 1726 return ret; 1727 } 1728 ret = wait_for_completion_interruptible_timeout(&queue->tls_complete, tmo); 1729 if (ret <= 0) { 1730 if (ret == 0) 1731 ret = -ETIMEDOUT; 1732 1733 dev_err(nctrl->device, 1734 "queue %d: TLS handshake failed, error %d\n", 1735 qid, ret); 1736 tls_handshake_cancel(queue->sock->sk); 1737 } else { 1738 dev_dbg(nctrl->device, 1739 "queue %d: TLS handshake complete, error %d\n", 1740 qid, queue->tls_err); 1741 ret = queue->tls_err; 1742 } 1743 return ret; 1744 } 1745 1746 static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid, 1747 key_serial_t pskid) 1748 { 1749 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 1750 struct nvme_tcp_queue *queue = &ctrl->queues[qid]; 1751 int ret, rcv_pdu_size; 1752 struct file *sock_file; 1753 1754 mutex_init(&queue->queue_lock); 1755 queue->ctrl = ctrl; 1756 init_llist_head(&queue->req_list); 1757 INIT_LIST_HEAD(&queue->send_list); 1758 mutex_init(&queue->send_mutex); 1759 INIT_WORK(&queue->io_work, nvme_tcp_io_work); 1760 1761 if (qid > 0) 1762 queue->cmnd_capsule_len = nctrl->ioccsz * 16; 1763 else 1764 queue->cmnd_capsule_len = sizeof(struct nvme_command) + 1765 NVME_TCP_ADMIN_CCSZ; 1766 1767 ret = sock_create(ctrl->addr.ss_family, SOCK_STREAM, 1768 IPPROTO_TCP, &queue->sock); 1769 if (ret) { 1770 dev_err(nctrl->device, 1771 "failed to create socket: %d\n", ret); 1772 goto err_destroy_mutex; 1773 } 1774 1775 sock_file = sock_alloc_file(queue->sock, O_CLOEXEC, NULL); 1776 if (IS_ERR(sock_file)) { 1777 ret = PTR_ERR(sock_file); 1778 goto err_destroy_mutex; 1779 } 1780 nvme_tcp_reclassify_socket(queue->sock); 1781 1782 /* Single syn retry */ 1783 tcp_sock_set_syncnt(queue->sock->sk, 1); 1784 1785 /* Set TCP no delay */ 1786 tcp_sock_set_nodelay(queue->sock->sk); 1787 1788 /* 1789 * Cleanup whatever is sitting in the TCP transmit queue on socket 1790 * close. This is done to prevent stale data from being sent should 1791 * the network connection be restored before TCP times out. 1792 */ 1793 sock_no_linger(queue->sock->sk); 1794 1795 if (so_priority > 0) 1796 sock_set_priority(queue->sock->sk, so_priority); 1797 1798 /* Set socket type of service */ 1799 if (nctrl->opts->tos >= 0) 1800 ip_sock_set_tos(queue->sock->sk, nctrl->opts->tos); 1801 1802 /* Set 10 seconds timeout for icresp recvmsg */ 1803 queue->sock->sk->sk_rcvtimeo = 10 * HZ; 1804 1805 queue->sock->sk->sk_allocation = GFP_ATOMIC; 1806 queue->sock->sk->sk_use_task_frag = false; 1807 queue->io_cpu = WORK_CPU_UNBOUND; 1808 queue->request = NULL; 1809 queue->data_remaining = 0; 1810 queue->ddgst_remaining = 0; 1811 queue->pdu_remaining = 0; 1812 queue->pdu_offset = 0; 1813 sk_set_memalloc(queue->sock->sk); 1814 1815 if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) { 1816 ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr, 1817 sizeof(ctrl->src_addr)); 1818 if (ret) { 1819 dev_err(nctrl->device, 1820 "failed to bind queue %d socket %d\n", 1821 qid, ret); 1822 goto err_sock; 1823 } 1824 } 1825 1826 if (nctrl->opts->mask & NVMF_OPT_HOST_IFACE) { 1827 char *iface = nctrl->opts->host_iface; 1828 sockptr_t optval = KERNEL_SOCKPTR(iface); 1829 1830 ret = sock_setsockopt(queue->sock, SOL_SOCKET, SO_BINDTODEVICE, 1831 optval, strlen(iface)); 1832 if (ret) { 1833 dev_err(nctrl->device, 1834 "failed to bind to interface %s queue %d err %d\n", 1835 iface, qid, ret); 1836 goto err_sock; 1837 } 1838 } 1839 1840 queue->hdr_digest = nctrl->opts->hdr_digest; 1841 queue->data_digest = nctrl->opts->data_digest; 1842 if (queue->hdr_digest || queue->data_digest) { 1843 ret = nvme_tcp_alloc_crypto(queue); 1844 if (ret) { 1845 dev_err(nctrl->device, 1846 "failed to allocate queue %d crypto\n", qid); 1847 goto err_sock; 1848 } 1849 } 1850 1851 rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) + 1852 nvme_tcp_hdgst_len(queue); 1853 queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL); 1854 if (!queue->pdu) { 1855 ret = -ENOMEM; 1856 goto err_crypto; 1857 } 1858 1859 dev_dbg(nctrl->device, "connecting queue %d\n", 1860 nvme_tcp_queue_id(queue)); 1861 1862 ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr, 1863 sizeof(ctrl->addr), 0); 1864 if (ret) { 1865 dev_err(nctrl->device, 1866 "failed to connect socket: %d\n", ret); 1867 goto err_rcv_pdu; 1868 } 1869 1870 /* If PSKs are configured try to start TLS */ 1871 if (nvme_tcp_tls_configured(nctrl) && pskid) { 1872 ret = nvme_tcp_start_tls(nctrl, queue, pskid); 1873 if (ret) 1874 goto err_init_connect; 1875 } 1876 1877 ret = nvme_tcp_init_connection(queue); 1878 if (ret) 1879 goto err_init_connect; 1880 1881 set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags); 1882 1883 return 0; 1884 1885 err_init_connect: 1886 kernel_sock_shutdown(queue->sock, SHUT_RDWR); 1887 err_rcv_pdu: 1888 kfree(queue->pdu); 1889 err_crypto: 1890 if (queue->hdr_digest || queue->data_digest) 1891 nvme_tcp_free_crypto(queue); 1892 err_sock: 1893 /* ->sock will be released by fput() */ 1894 fput(queue->sock->file); 1895 queue->sock = NULL; 1896 err_destroy_mutex: 1897 mutex_destroy(&queue->send_mutex); 1898 mutex_destroy(&queue->queue_lock); 1899 return ret; 1900 } 1901 1902 static void nvme_tcp_restore_sock_ops(struct nvme_tcp_queue *queue) 1903 { 1904 struct socket *sock = queue->sock; 1905 1906 write_lock_bh(&sock->sk->sk_callback_lock); 1907 sock->sk->sk_user_data = NULL; 1908 sock->sk->sk_data_ready = queue->data_ready; 1909 sock->sk->sk_state_change = queue->state_change; 1910 sock->sk->sk_write_space = queue->write_space; 1911 write_unlock_bh(&sock->sk->sk_callback_lock); 1912 } 1913 1914 static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue) 1915 { 1916 kernel_sock_shutdown(queue->sock, SHUT_RDWR); 1917 nvme_tcp_restore_sock_ops(queue); 1918 cancel_work_sync(&queue->io_work); 1919 } 1920 1921 static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid) 1922 { 1923 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 1924 struct nvme_tcp_queue *queue = &ctrl->queues[qid]; 1925 1926 if (!test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) 1927 return; 1928 1929 if (test_and_clear_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags)) 1930 atomic_dec(&nvme_tcp_cpu_queues[queue->io_cpu]); 1931 1932 mutex_lock(&queue->queue_lock); 1933 if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags)) 1934 __nvme_tcp_stop_queue(queue); 1935 /* Stopping the queue will disable TLS */ 1936 queue->tls_enabled = false; 1937 mutex_unlock(&queue->queue_lock); 1938 } 1939 1940 static void nvme_tcp_setup_sock_ops(struct nvme_tcp_queue *queue) 1941 { 1942 write_lock_bh(&queue->sock->sk->sk_callback_lock); 1943 queue->sock->sk->sk_user_data = queue; 1944 queue->state_change = queue->sock->sk->sk_state_change; 1945 queue->data_ready = queue->sock->sk->sk_data_ready; 1946 queue->write_space = queue->sock->sk->sk_write_space; 1947 queue->sock->sk->sk_data_ready = nvme_tcp_data_ready; 1948 queue->sock->sk->sk_state_change = nvme_tcp_state_change; 1949 queue->sock->sk->sk_write_space = nvme_tcp_write_space; 1950 #ifdef CONFIG_NET_RX_BUSY_POLL 1951 queue->sock->sk->sk_ll_usec = 1; 1952 #endif 1953 write_unlock_bh(&queue->sock->sk->sk_callback_lock); 1954 } 1955 1956 static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx) 1957 { 1958 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 1959 struct nvme_tcp_queue *queue = &ctrl->queues[idx]; 1960 int ret; 1961 1962 queue->rd_enabled = true; 1963 nvme_tcp_init_recv_ctx(queue); 1964 nvme_tcp_setup_sock_ops(queue); 1965 1966 if (idx) { 1967 nvme_tcp_set_queue_io_cpu(queue); 1968 ret = nvmf_connect_io_queue(nctrl, idx); 1969 } else 1970 ret = nvmf_connect_admin_queue(nctrl); 1971 1972 if (!ret) { 1973 set_bit(NVME_TCP_Q_LIVE, &queue->flags); 1974 } else { 1975 if (test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) 1976 __nvme_tcp_stop_queue(queue); 1977 dev_err(nctrl->device, 1978 "failed to connect queue: %d ret=%d\n", idx, ret); 1979 } 1980 return ret; 1981 } 1982 1983 static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl) 1984 { 1985 if (to_tcp_ctrl(ctrl)->async_req.pdu) { 1986 cancel_work_sync(&ctrl->async_event_work); 1987 nvme_tcp_free_async_req(to_tcp_ctrl(ctrl)); 1988 to_tcp_ctrl(ctrl)->async_req.pdu = NULL; 1989 } 1990 1991 nvme_tcp_free_queue(ctrl, 0); 1992 } 1993 1994 static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl) 1995 { 1996 int i; 1997 1998 for (i = 1; i < ctrl->queue_count; i++) 1999 nvme_tcp_free_queue(ctrl, i); 2000 } 2001 2002 static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl) 2003 { 2004 int i; 2005 2006 for (i = 1; i < ctrl->queue_count; i++) 2007 nvme_tcp_stop_queue(ctrl, i); 2008 } 2009 2010 static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl, 2011 int first, int last) 2012 { 2013 int i, ret; 2014 2015 for (i = first; i < last; i++) { 2016 ret = nvme_tcp_start_queue(ctrl, i); 2017 if (ret) 2018 goto out_stop_queues; 2019 } 2020 2021 return 0; 2022 2023 out_stop_queues: 2024 for (i--; i >= first; i--) 2025 nvme_tcp_stop_queue(ctrl, i); 2026 return ret; 2027 } 2028 2029 static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) 2030 { 2031 int ret; 2032 key_serial_t pskid = 0; 2033 2034 if (nvme_tcp_tls_configured(ctrl)) { 2035 if (ctrl->opts->tls_key) 2036 pskid = key_serial(ctrl->opts->tls_key); 2037 else { 2038 pskid = nvme_tls_psk_default(ctrl->opts->keyring, 2039 ctrl->opts->host->nqn, 2040 ctrl->opts->subsysnqn); 2041 if (!pskid) { 2042 dev_err(ctrl->device, "no valid PSK found\n"); 2043 return -ENOKEY; 2044 } 2045 } 2046 } 2047 2048 ret = nvme_tcp_alloc_queue(ctrl, 0, pskid); 2049 if (ret) 2050 return ret; 2051 2052 ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl)); 2053 if (ret) 2054 goto out_free_queue; 2055 2056 return 0; 2057 2058 out_free_queue: 2059 nvme_tcp_free_queue(ctrl, 0); 2060 return ret; 2061 } 2062 2063 static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl) 2064 { 2065 int i, ret; 2066 2067 if (nvme_tcp_tls_configured(ctrl) && !ctrl->tls_pskid) { 2068 dev_err(ctrl->device, "no PSK negotiated\n"); 2069 return -ENOKEY; 2070 } 2071 2072 for (i = 1; i < ctrl->queue_count; i++) { 2073 ret = nvme_tcp_alloc_queue(ctrl, i, 2074 ctrl->tls_pskid); 2075 if (ret) 2076 goto out_free_queues; 2077 } 2078 2079 return 0; 2080 2081 out_free_queues: 2082 for (i--; i >= 1; i--) 2083 nvme_tcp_free_queue(ctrl, i); 2084 2085 return ret; 2086 } 2087 2088 static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl) 2089 { 2090 unsigned int nr_io_queues; 2091 int ret; 2092 2093 nr_io_queues = nvmf_nr_io_queues(ctrl->opts); 2094 ret = nvme_set_queue_count(ctrl, &nr_io_queues); 2095 if (ret) 2096 return ret; 2097 2098 if (nr_io_queues == 0) { 2099 dev_err(ctrl->device, 2100 "unable to set any I/O queues\n"); 2101 return -ENOMEM; 2102 } 2103 2104 ctrl->queue_count = nr_io_queues + 1; 2105 dev_info(ctrl->device, 2106 "creating %d I/O queues.\n", nr_io_queues); 2107 2108 nvmf_set_io_queues(ctrl->opts, nr_io_queues, 2109 to_tcp_ctrl(ctrl)->io_queues); 2110 return __nvme_tcp_alloc_io_queues(ctrl); 2111 } 2112 2113 static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new) 2114 { 2115 int ret, nr_queues; 2116 2117 ret = nvme_tcp_alloc_io_queues(ctrl); 2118 if (ret) 2119 return ret; 2120 2121 if (new) { 2122 ret = nvme_alloc_io_tag_set(ctrl, &to_tcp_ctrl(ctrl)->tag_set, 2123 &nvme_tcp_mq_ops, 2124 ctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2, 2125 sizeof(struct nvme_tcp_request)); 2126 if (ret) 2127 goto out_free_io_queues; 2128 } 2129 2130 /* 2131 * Only start IO queues for which we have allocated the tagset 2132 * and limitted it to the available queues. On reconnects, the 2133 * queue number might have changed. 2134 */ 2135 nr_queues = min(ctrl->tagset->nr_hw_queues + 1, ctrl->queue_count); 2136 ret = nvme_tcp_start_io_queues(ctrl, 1, nr_queues); 2137 if (ret) 2138 goto out_cleanup_connect_q; 2139 2140 if (!new) { 2141 nvme_start_freeze(ctrl); 2142 nvme_unquiesce_io_queues(ctrl); 2143 if (!nvme_wait_freeze_timeout(ctrl, NVME_IO_TIMEOUT)) { 2144 /* 2145 * If we timed out waiting for freeze we are likely to 2146 * be stuck. Fail the controller initialization just 2147 * to be safe. 2148 */ 2149 ret = -ENODEV; 2150 nvme_unfreeze(ctrl); 2151 goto out_wait_freeze_timed_out; 2152 } 2153 blk_mq_update_nr_hw_queues(ctrl->tagset, 2154 ctrl->queue_count - 1); 2155 nvme_unfreeze(ctrl); 2156 } 2157 2158 /* 2159 * If the number of queues has increased (reconnect case) 2160 * start all new queues now. 2161 */ 2162 ret = nvme_tcp_start_io_queues(ctrl, nr_queues, 2163 ctrl->tagset->nr_hw_queues + 1); 2164 if (ret) 2165 goto out_wait_freeze_timed_out; 2166 2167 return 0; 2168 2169 out_wait_freeze_timed_out: 2170 nvme_quiesce_io_queues(ctrl); 2171 nvme_sync_io_queues(ctrl); 2172 nvme_tcp_stop_io_queues(ctrl); 2173 out_cleanup_connect_q: 2174 nvme_cancel_tagset(ctrl); 2175 if (new) 2176 nvme_remove_io_tag_set(ctrl); 2177 out_free_io_queues: 2178 nvme_tcp_free_io_queues(ctrl); 2179 return ret; 2180 } 2181 2182 static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new) 2183 { 2184 int error; 2185 2186 error = nvme_tcp_alloc_admin_queue(ctrl); 2187 if (error) 2188 return error; 2189 2190 if (new) { 2191 error = nvme_alloc_admin_tag_set(ctrl, 2192 &to_tcp_ctrl(ctrl)->admin_tag_set, 2193 &nvme_tcp_admin_mq_ops, 2194 sizeof(struct nvme_tcp_request)); 2195 if (error) 2196 goto out_free_queue; 2197 } 2198 2199 error = nvme_tcp_start_queue(ctrl, 0); 2200 if (error) 2201 goto out_cleanup_tagset; 2202 2203 error = nvme_enable_ctrl(ctrl); 2204 if (error) 2205 goto out_stop_queue; 2206 2207 nvme_unquiesce_admin_queue(ctrl); 2208 2209 error = nvme_init_ctrl_finish(ctrl, false); 2210 if (error) 2211 goto out_quiesce_queue; 2212 2213 return 0; 2214 2215 out_quiesce_queue: 2216 nvme_quiesce_admin_queue(ctrl); 2217 blk_sync_queue(ctrl->admin_q); 2218 out_stop_queue: 2219 nvme_tcp_stop_queue(ctrl, 0); 2220 nvme_cancel_admin_tagset(ctrl); 2221 out_cleanup_tagset: 2222 if (new) 2223 nvme_remove_admin_tag_set(ctrl); 2224 out_free_queue: 2225 nvme_tcp_free_admin_queue(ctrl); 2226 return error; 2227 } 2228 2229 static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl, 2230 bool remove) 2231 { 2232 nvme_quiesce_admin_queue(ctrl); 2233 blk_sync_queue(ctrl->admin_q); 2234 nvme_tcp_stop_queue(ctrl, 0); 2235 nvme_cancel_admin_tagset(ctrl); 2236 if (remove) { 2237 nvme_unquiesce_admin_queue(ctrl); 2238 nvme_remove_admin_tag_set(ctrl); 2239 } 2240 nvme_tcp_free_admin_queue(ctrl); 2241 if (ctrl->tls_pskid) { 2242 dev_dbg(ctrl->device, "Wipe negotiated TLS_PSK %08x\n", 2243 ctrl->tls_pskid); 2244 ctrl->tls_pskid = 0; 2245 } 2246 } 2247 2248 static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl, 2249 bool remove) 2250 { 2251 if (ctrl->queue_count <= 1) 2252 return; 2253 nvme_quiesce_io_queues(ctrl); 2254 nvme_sync_io_queues(ctrl); 2255 nvme_tcp_stop_io_queues(ctrl); 2256 nvme_cancel_tagset(ctrl); 2257 if (remove) { 2258 nvme_unquiesce_io_queues(ctrl); 2259 nvme_remove_io_tag_set(ctrl); 2260 } 2261 nvme_tcp_free_io_queues(ctrl); 2262 } 2263 2264 static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl, 2265 int status) 2266 { 2267 enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); 2268 2269 /* If we are resetting/deleting then do nothing */ 2270 if (state != NVME_CTRL_CONNECTING) { 2271 WARN_ON_ONCE(state == NVME_CTRL_NEW || state == NVME_CTRL_LIVE); 2272 return; 2273 } 2274 2275 if (nvmf_should_reconnect(ctrl, status)) { 2276 dev_info(ctrl->device, "Reconnecting in %d seconds...\n", 2277 ctrl->opts->reconnect_delay); 2278 queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work, 2279 ctrl->opts->reconnect_delay * HZ); 2280 } else { 2281 dev_info(ctrl->device, "Removing controller (%d)...\n", 2282 status); 2283 nvme_delete_ctrl(ctrl); 2284 } 2285 } 2286 2287 static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new) 2288 { 2289 struct nvmf_ctrl_options *opts = ctrl->opts; 2290 int ret; 2291 2292 ret = nvme_tcp_configure_admin_queue(ctrl, new); 2293 if (ret) 2294 return ret; 2295 2296 if (ctrl->icdoff) { 2297 ret = -EOPNOTSUPP; 2298 dev_err(ctrl->device, "icdoff is not supported!\n"); 2299 goto destroy_admin; 2300 } 2301 2302 if (!nvme_ctrl_sgl_supported(ctrl)) { 2303 ret = -EOPNOTSUPP; 2304 dev_err(ctrl->device, "Mandatory sgls are not supported!\n"); 2305 goto destroy_admin; 2306 } 2307 2308 if (opts->queue_size > ctrl->sqsize + 1) 2309 dev_warn(ctrl->device, 2310 "queue_size %zu > ctrl sqsize %u, clamping down\n", 2311 opts->queue_size, ctrl->sqsize + 1); 2312 2313 if (ctrl->sqsize + 1 > ctrl->maxcmd) { 2314 dev_warn(ctrl->device, 2315 "sqsize %u > ctrl maxcmd %u, clamping down\n", 2316 ctrl->sqsize + 1, ctrl->maxcmd); 2317 ctrl->sqsize = ctrl->maxcmd - 1; 2318 } 2319 2320 if (ctrl->queue_count > 1) { 2321 ret = nvme_tcp_configure_io_queues(ctrl, new); 2322 if (ret) 2323 goto destroy_admin; 2324 } 2325 2326 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) { 2327 /* 2328 * state change failure is ok if we started ctrl delete, 2329 * unless we're during creation of a new controller to 2330 * avoid races with teardown flow. 2331 */ 2332 enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); 2333 2334 WARN_ON_ONCE(state != NVME_CTRL_DELETING && 2335 state != NVME_CTRL_DELETING_NOIO); 2336 WARN_ON_ONCE(new); 2337 ret = -EINVAL; 2338 goto destroy_io; 2339 } 2340 2341 nvme_start_ctrl(ctrl); 2342 return 0; 2343 2344 destroy_io: 2345 if (ctrl->queue_count > 1) { 2346 nvme_quiesce_io_queues(ctrl); 2347 nvme_sync_io_queues(ctrl); 2348 nvme_tcp_stop_io_queues(ctrl); 2349 nvme_cancel_tagset(ctrl); 2350 if (new) 2351 nvme_remove_io_tag_set(ctrl); 2352 nvme_tcp_free_io_queues(ctrl); 2353 } 2354 destroy_admin: 2355 nvme_stop_keep_alive(ctrl); 2356 nvme_tcp_teardown_admin_queue(ctrl, new); 2357 return ret; 2358 } 2359 2360 static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work) 2361 { 2362 struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work), 2363 struct nvme_tcp_ctrl, connect_work); 2364 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl; 2365 int ret; 2366 2367 ++ctrl->nr_reconnects; 2368 2369 ret = nvme_tcp_setup_ctrl(ctrl, false); 2370 if (ret) 2371 goto requeue; 2372 2373 dev_info(ctrl->device, "Successfully reconnected (attempt %d/%d)\n", 2374 ctrl->nr_reconnects, ctrl->opts->max_reconnects); 2375 2376 ctrl->nr_reconnects = 0; 2377 2378 return; 2379 2380 requeue: 2381 dev_info(ctrl->device, "Failed reconnect attempt %d/%d\n", 2382 ctrl->nr_reconnects, ctrl->opts->max_reconnects); 2383 nvme_tcp_reconnect_or_remove(ctrl, ret); 2384 } 2385 2386 static void nvme_tcp_error_recovery_work(struct work_struct *work) 2387 { 2388 struct nvme_tcp_ctrl *tcp_ctrl = container_of(work, 2389 struct nvme_tcp_ctrl, err_work); 2390 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl; 2391 2392 nvme_stop_keep_alive(ctrl); 2393 flush_work(&ctrl->async_event_work); 2394 nvme_tcp_teardown_io_queues(ctrl, false); 2395 /* unquiesce to fail fast pending requests */ 2396 nvme_unquiesce_io_queues(ctrl); 2397 nvme_tcp_teardown_admin_queue(ctrl, false); 2398 nvme_unquiesce_admin_queue(ctrl); 2399 nvme_auth_stop(ctrl); 2400 2401 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) { 2402 /* state change failure is ok if we started ctrl delete */ 2403 enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); 2404 2405 WARN_ON_ONCE(state != NVME_CTRL_DELETING && 2406 state != NVME_CTRL_DELETING_NOIO); 2407 return; 2408 } 2409 2410 nvme_tcp_reconnect_or_remove(ctrl, 0); 2411 } 2412 2413 static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown) 2414 { 2415 nvme_tcp_teardown_io_queues(ctrl, shutdown); 2416 nvme_quiesce_admin_queue(ctrl); 2417 nvme_disable_ctrl(ctrl, shutdown); 2418 nvme_tcp_teardown_admin_queue(ctrl, shutdown); 2419 } 2420 2421 static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl) 2422 { 2423 nvme_tcp_teardown_ctrl(ctrl, true); 2424 } 2425 2426 static void nvme_reset_ctrl_work(struct work_struct *work) 2427 { 2428 struct nvme_ctrl *ctrl = 2429 container_of(work, struct nvme_ctrl, reset_work); 2430 int ret; 2431 2432 nvme_stop_ctrl(ctrl); 2433 nvme_tcp_teardown_ctrl(ctrl, false); 2434 2435 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) { 2436 /* state change failure is ok if we started ctrl delete */ 2437 enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); 2438 2439 WARN_ON_ONCE(state != NVME_CTRL_DELETING && 2440 state != NVME_CTRL_DELETING_NOIO); 2441 return; 2442 } 2443 2444 ret = nvme_tcp_setup_ctrl(ctrl, false); 2445 if (ret) 2446 goto out_fail; 2447 2448 return; 2449 2450 out_fail: 2451 ++ctrl->nr_reconnects; 2452 nvme_tcp_reconnect_or_remove(ctrl, ret); 2453 } 2454 2455 static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl) 2456 { 2457 flush_work(&to_tcp_ctrl(ctrl)->err_work); 2458 cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work); 2459 } 2460 2461 static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl) 2462 { 2463 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); 2464 2465 if (list_empty(&ctrl->list)) 2466 goto free_ctrl; 2467 2468 mutex_lock(&nvme_tcp_ctrl_mutex); 2469 list_del(&ctrl->list); 2470 mutex_unlock(&nvme_tcp_ctrl_mutex); 2471 2472 nvmf_free_options(nctrl->opts); 2473 free_ctrl: 2474 kfree(ctrl->queues); 2475 kfree(ctrl); 2476 } 2477 2478 static void nvme_tcp_set_sg_null(struct nvme_command *c) 2479 { 2480 struct nvme_sgl_desc *sg = &c->common.dptr.sgl; 2481 2482 sg->addr = 0; 2483 sg->length = 0; 2484 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) | 2485 NVME_SGL_FMT_TRANSPORT_A; 2486 } 2487 2488 static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue, 2489 struct nvme_command *c, u32 data_len) 2490 { 2491 struct nvme_sgl_desc *sg = &c->common.dptr.sgl; 2492 2493 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); 2494 sg->length = cpu_to_le32(data_len); 2495 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; 2496 } 2497 2498 static void nvme_tcp_set_sg_host_data(struct nvme_command *c, 2499 u32 data_len) 2500 { 2501 struct nvme_sgl_desc *sg = &c->common.dptr.sgl; 2502 2503 sg->addr = 0; 2504 sg->length = cpu_to_le32(data_len); 2505 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) | 2506 NVME_SGL_FMT_TRANSPORT_A; 2507 } 2508 2509 static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg) 2510 { 2511 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg); 2512 struct nvme_tcp_queue *queue = &ctrl->queues[0]; 2513 struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu; 2514 struct nvme_command *cmd = &pdu->cmd; 2515 u8 hdgst = nvme_tcp_hdgst_len(queue); 2516 2517 memset(pdu, 0, sizeof(*pdu)); 2518 pdu->hdr.type = nvme_tcp_cmd; 2519 if (queue->hdr_digest) 2520 pdu->hdr.flags |= NVME_TCP_F_HDGST; 2521 pdu->hdr.hlen = sizeof(*pdu); 2522 pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst); 2523 2524 cmd->common.opcode = nvme_admin_async_event; 2525 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH; 2526 cmd->common.flags |= NVME_CMD_SGL_METABUF; 2527 nvme_tcp_set_sg_null(cmd); 2528 2529 ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU; 2530 ctrl->async_req.offset = 0; 2531 ctrl->async_req.curr_bio = NULL; 2532 ctrl->async_req.data_len = 0; 2533 2534 nvme_tcp_queue_request(&ctrl->async_req, true, true); 2535 } 2536 2537 static void nvme_tcp_complete_timed_out(struct request *rq) 2538 { 2539 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 2540 struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl; 2541 2542 nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue)); 2543 nvmf_complete_timed_out_request(rq); 2544 } 2545 2546 static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq) 2547 { 2548 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 2549 struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl; 2550 struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); 2551 struct nvme_command *cmd = &pdu->cmd; 2552 int qid = nvme_tcp_queue_id(req->queue); 2553 2554 dev_warn(ctrl->device, 2555 "I/O tag %d (%04x) type %d opcode %#x (%s) QID %d timeout\n", 2556 rq->tag, nvme_cid(rq), pdu->hdr.type, cmd->common.opcode, 2557 nvme_fabrics_opcode_str(qid, cmd), qid); 2558 2559 if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) { 2560 /* 2561 * If we are resetting, connecting or deleting we should 2562 * complete immediately because we may block controller 2563 * teardown or setup sequence 2564 * - ctrl disable/shutdown fabrics requests 2565 * - connect requests 2566 * - initialization admin requests 2567 * - I/O requests that entered after unquiescing and 2568 * the controller stopped responding 2569 * 2570 * All other requests should be cancelled by the error 2571 * recovery work, so it's fine that we fail it here. 2572 */ 2573 nvme_tcp_complete_timed_out(rq); 2574 return BLK_EH_DONE; 2575 } 2576 2577 /* 2578 * LIVE state should trigger the normal error recovery which will 2579 * handle completing this request. 2580 */ 2581 nvme_tcp_error_recovery(ctrl); 2582 return BLK_EH_RESET_TIMER; 2583 } 2584 2585 static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue, 2586 struct request *rq) 2587 { 2588 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 2589 struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); 2590 struct nvme_command *c = &pdu->cmd; 2591 2592 c->common.flags |= NVME_CMD_SGL_METABUF; 2593 2594 if (!blk_rq_nr_phys_segments(rq)) 2595 nvme_tcp_set_sg_null(c); 2596 else if (rq_data_dir(rq) == WRITE && 2597 req->data_len <= nvme_tcp_inline_data_size(req)) 2598 nvme_tcp_set_sg_inline(queue, c, req->data_len); 2599 else 2600 nvme_tcp_set_sg_host_data(c, req->data_len); 2601 2602 return 0; 2603 } 2604 2605 static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns, 2606 struct request *rq) 2607 { 2608 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 2609 struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); 2610 struct nvme_tcp_queue *queue = req->queue; 2611 u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0; 2612 blk_status_t ret; 2613 2614 ret = nvme_setup_cmd(ns, rq); 2615 if (ret) 2616 return ret; 2617 2618 req->state = NVME_TCP_SEND_CMD_PDU; 2619 req->status = cpu_to_le16(NVME_SC_SUCCESS); 2620 req->offset = 0; 2621 req->data_sent = 0; 2622 req->pdu_len = 0; 2623 req->pdu_sent = 0; 2624 req->h2cdata_left = 0; 2625 req->data_len = blk_rq_nr_phys_segments(rq) ? 2626 blk_rq_payload_bytes(rq) : 0; 2627 req->curr_bio = rq->bio; 2628 if (req->curr_bio && req->data_len) 2629 nvme_tcp_init_iter(req, rq_data_dir(rq)); 2630 2631 if (rq_data_dir(rq) == WRITE && 2632 req->data_len <= nvme_tcp_inline_data_size(req)) 2633 req->pdu_len = req->data_len; 2634 2635 pdu->hdr.type = nvme_tcp_cmd; 2636 pdu->hdr.flags = 0; 2637 if (queue->hdr_digest) 2638 pdu->hdr.flags |= NVME_TCP_F_HDGST; 2639 if (queue->data_digest && req->pdu_len) { 2640 pdu->hdr.flags |= NVME_TCP_F_DDGST; 2641 ddgst = nvme_tcp_ddgst_len(queue); 2642 } 2643 pdu->hdr.hlen = sizeof(*pdu); 2644 pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0; 2645 pdu->hdr.plen = 2646 cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst); 2647 2648 ret = nvme_tcp_map_data(queue, rq); 2649 if (unlikely(ret)) { 2650 nvme_cleanup_cmd(rq); 2651 dev_err(queue->ctrl->ctrl.device, 2652 "Failed to map data (%d)\n", ret); 2653 return ret; 2654 } 2655 2656 return 0; 2657 } 2658 2659 static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx) 2660 { 2661 struct nvme_tcp_queue *queue = hctx->driver_data; 2662 2663 if (!llist_empty(&queue->req_list)) 2664 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); 2665 } 2666 2667 static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx, 2668 const struct blk_mq_queue_data *bd) 2669 { 2670 struct nvme_ns *ns = hctx->queue->queuedata; 2671 struct nvme_tcp_queue *queue = hctx->driver_data; 2672 struct request *rq = bd->rq; 2673 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); 2674 bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags); 2675 blk_status_t ret; 2676 2677 if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready)) 2678 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq); 2679 2680 ret = nvme_tcp_setup_cmd_pdu(ns, rq); 2681 if (unlikely(ret)) 2682 return ret; 2683 2684 nvme_start_request(rq); 2685 2686 nvme_tcp_queue_request(req, true, bd->last); 2687 2688 return BLK_STS_OK; 2689 } 2690 2691 static void nvme_tcp_map_queues(struct blk_mq_tag_set *set) 2692 { 2693 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data); 2694 2695 nvmf_map_queues(set, &ctrl->ctrl, ctrl->io_queues); 2696 } 2697 2698 static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob) 2699 { 2700 struct nvme_tcp_queue *queue = hctx->driver_data; 2701 struct sock *sk = queue->sock->sk; 2702 2703 if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags)) 2704 return 0; 2705 2706 set_bit(NVME_TCP_Q_POLLING, &queue->flags); 2707 if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue)) 2708 sk_busy_loop(sk, true); 2709 nvme_tcp_try_recv(queue); 2710 clear_bit(NVME_TCP_Q_POLLING, &queue->flags); 2711 return queue->nr_cqe; 2712 } 2713 2714 static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size) 2715 { 2716 struct nvme_tcp_queue *queue = &to_tcp_ctrl(ctrl)->queues[0]; 2717 struct sockaddr_storage src_addr; 2718 int ret, len; 2719 2720 len = nvmf_get_address(ctrl, buf, size); 2721 2722 if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags)) 2723 return len; 2724 2725 mutex_lock(&queue->queue_lock); 2726 2727 ret = kernel_getsockname(queue->sock, (struct sockaddr *)&src_addr); 2728 if (ret > 0) { 2729 if (len > 0) 2730 len--; /* strip trailing newline */ 2731 len += scnprintf(buf + len, size - len, "%ssrc_addr=%pISc\n", 2732 (len) ? "," : "", &src_addr); 2733 } 2734 2735 mutex_unlock(&queue->queue_lock); 2736 2737 return len; 2738 } 2739 2740 static const struct blk_mq_ops nvme_tcp_mq_ops = { 2741 .queue_rq = nvme_tcp_queue_rq, 2742 .commit_rqs = nvme_tcp_commit_rqs, 2743 .complete = nvme_complete_rq, 2744 .init_request = nvme_tcp_init_request, 2745 .exit_request = nvme_tcp_exit_request, 2746 .init_hctx = nvme_tcp_init_hctx, 2747 .timeout = nvme_tcp_timeout, 2748 .map_queues = nvme_tcp_map_queues, 2749 .poll = nvme_tcp_poll, 2750 }; 2751 2752 static const struct blk_mq_ops nvme_tcp_admin_mq_ops = { 2753 .queue_rq = nvme_tcp_queue_rq, 2754 .complete = nvme_complete_rq, 2755 .init_request = nvme_tcp_init_request, 2756 .exit_request = nvme_tcp_exit_request, 2757 .init_hctx = nvme_tcp_init_admin_hctx, 2758 .timeout = nvme_tcp_timeout, 2759 }; 2760 2761 static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = { 2762 .name = "tcp", 2763 .module = THIS_MODULE, 2764 .flags = NVME_F_FABRICS | NVME_F_BLOCKING, 2765 .reg_read32 = nvmf_reg_read32, 2766 .reg_read64 = nvmf_reg_read64, 2767 .reg_write32 = nvmf_reg_write32, 2768 .subsystem_reset = nvmf_subsystem_reset, 2769 .free_ctrl = nvme_tcp_free_ctrl, 2770 .submit_async_event = nvme_tcp_submit_async_event, 2771 .delete_ctrl = nvme_tcp_delete_ctrl, 2772 .get_address = nvme_tcp_get_address, 2773 .stop_ctrl = nvme_tcp_stop_ctrl, 2774 }; 2775 2776 static bool 2777 nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts) 2778 { 2779 struct nvme_tcp_ctrl *ctrl; 2780 bool found = false; 2781 2782 mutex_lock(&nvme_tcp_ctrl_mutex); 2783 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) { 2784 found = nvmf_ip_options_match(&ctrl->ctrl, opts); 2785 if (found) 2786 break; 2787 } 2788 mutex_unlock(&nvme_tcp_ctrl_mutex); 2789 2790 return found; 2791 } 2792 2793 static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev, 2794 struct nvmf_ctrl_options *opts) 2795 { 2796 struct nvme_tcp_ctrl *ctrl; 2797 int ret; 2798 2799 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); 2800 if (!ctrl) 2801 return ERR_PTR(-ENOMEM); 2802 2803 INIT_LIST_HEAD(&ctrl->list); 2804 ctrl->ctrl.opts = opts; 2805 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues + 2806 opts->nr_poll_queues + 1; 2807 ctrl->ctrl.sqsize = opts->queue_size - 1; 2808 ctrl->ctrl.kato = opts->kato; 2809 2810 INIT_DELAYED_WORK(&ctrl->connect_work, 2811 nvme_tcp_reconnect_ctrl_work); 2812 INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work); 2813 INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work); 2814 2815 if (!(opts->mask & NVMF_OPT_TRSVCID)) { 2816 opts->trsvcid = 2817 kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL); 2818 if (!opts->trsvcid) { 2819 ret = -ENOMEM; 2820 goto out_free_ctrl; 2821 } 2822 opts->mask |= NVMF_OPT_TRSVCID; 2823 } 2824 2825 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 2826 opts->traddr, opts->trsvcid, &ctrl->addr); 2827 if (ret) { 2828 pr_err("malformed address passed: %s:%s\n", 2829 opts->traddr, opts->trsvcid); 2830 goto out_free_ctrl; 2831 } 2832 2833 if (opts->mask & NVMF_OPT_HOST_TRADDR) { 2834 ret = inet_pton_with_scope(&init_net, AF_UNSPEC, 2835 opts->host_traddr, NULL, &ctrl->src_addr); 2836 if (ret) { 2837 pr_err("malformed src address passed: %s\n", 2838 opts->host_traddr); 2839 goto out_free_ctrl; 2840 } 2841 } 2842 2843 if (opts->mask & NVMF_OPT_HOST_IFACE) { 2844 if (!__dev_get_by_name(&init_net, opts->host_iface)) { 2845 pr_err("invalid interface passed: %s\n", 2846 opts->host_iface); 2847 ret = -ENODEV; 2848 goto out_free_ctrl; 2849 } 2850 } 2851 2852 if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) { 2853 ret = -EALREADY; 2854 goto out_free_ctrl; 2855 } 2856 2857 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues), 2858 GFP_KERNEL); 2859 if (!ctrl->queues) { 2860 ret = -ENOMEM; 2861 goto out_free_ctrl; 2862 } 2863 2864 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0); 2865 if (ret) 2866 goto out_kfree_queues; 2867 2868 return ctrl; 2869 out_kfree_queues: 2870 kfree(ctrl->queues); 2871 out_free_ctrl: 2872 kfree(ctrl); 2873 return ERR_PTR(ret); 2874 } 2875 2876 static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev, 2877 struct nvmf_ctrl_options *opts) 2878 { 2879 struct nvme_tcp_ctrl *ctrl; 2880 int ret; 2881 2882 ctrl = nvme_tcp_alloc_ctrl(dev, opts); 2883 if (IS_ERR(ctrl)) 2884 return ERR_CAST(ctrl); 2885 2886 ret = nvme_add_ctrl(&ctrl->ctrl); 2887 if (ret) 2888 goto out_put_ctrl; 2889 2890 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { 2891 WARN_ON_ONCE(1); 2892 ret = -EINTR; 2893 goto out_uninit_ctrl; 2894 } 2895 2896 ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true); 2897 if (ret) 2898 goto out_uninit_ctrl; 2899 2900 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp, hostnqn: %s\n", 2901 nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr, opts->host->nqn); 2902 2903 mutex_lock(&nvme_tcp_ctrl_mutex); 2904 list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list); 2905 mutex_unlock(&nvme_tcp_ctrl_mutex); 2906 2907 return &ctrl->ctrl; 2908 2909 out_uninit_ctrl: 2910 nvme_uninit_ctrl(&ctrl->ctrl); 2911 out_put_ctrl: 2912 nvme_put_ctrl(&ctrl->ctrl); 2913 if (ret > 0) 2914 ret = -EIO; 2915 return ERR_PTR(ret); 2916 } 2917 2918 static struct nvmf_transport_ops nvme_tcp_transport = { 2919 .name = "tcp", 2920 .module = THIS_MODULE, 2921 .required_opts = NVMF_OPT_TRADDR, 2922 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY | 2923 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO | 2924 NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST | 2925 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES | 2926 NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS | 2927 NVMF_OPT_KEYRING | NVMF_OPT_TLS_KEY, 2928 .create_ctrl = nvme_tcp_create_ctrl, 2929 }; 2930 2931 static int __init nvme_tcp_init_module(void) 2932 { 2933 unsigned int wq_flags = WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_SYSFS; 2934 int cpu; 2935 2936 BUILD_BUG_ON(sizeof(struct nvme_tcp_hdr) != 8); 2937 BUILD_BUG_ON(sizeof(struct nvme_tcp_cmd_pdu) != 72); 2938 BUILD_BUG_ON(sizeof(struct nvme_tcp_data_pdu) != 24); 2939 BUILD_BUG_ON(sizeof(struct nvme_tcp_rsp_pdu) != 24); 2940 BUILD_BUG_ON(sizeof(struct nvme_tcp_r2t_pdu) != 24); 2941 BUILD_BUG_ON(sizeof(struct nvme_tcp_icreq_pdu) != 128); 2942 BUILD_BUG_ON(sizeof(struct nvme_tcp_icresp_pdu) != 128); 2943 BUILD_BUG_ON(sizeof(struct nvme_tcp_term_pdu) != 24); 2944 2945 if (wq_unbound) 2946 wq_flags |= WQ_UNBOUND; 2947 2948 nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq", wq_flags, 0); 2949 if (!nvme_tcp_wq) 2950 return -ENOMEM; 2951 2952 for_each_possible_cpu(cpu) 2953 atomic_set(&nvme_tcp_cpu_queues[cpu], 0); 2954 2955 nvmf_register_transport(&nvme_tcp_transport); 2956 return 0; 2957 } 2958 2959 static void __exit nvme_tcp_cleanup_module(void) 2960 { 2961 struct nvme_tcp_ctrl *ctrl; 2962 2963 nvmf_unregister_transport(&nvme_tcp_transport); 2964 2965 mutex_lock(&nvme_tcp_ctrl_mutex); 2966 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) 2967 nvme_delete_ctrl(&ctrl->ctrl); 2968 mutex_unlock(&nvme_tcp_ctrl_mutex); 2969 flush_workqueue(nvme_delete_wq); 2970 2971 destroy_workqueue(nvme_tcp_wq); 2972 } 2973 2974 module_init(nvme_tcp_init_module); 2975 module_exit(nvme_tcp_cleanup_module); 2976 2977 MODULE_DESCRIPTION("NVMe host TCP transport driver"); 2978 MODULE_LICENSE("GPL v2"); 2979