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