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