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