1 // SPDX-License-Identifier: GPL-2.0
3 * NVMe over Fabrics TCP host.
4 * Copyright (c) 2018 Lightbits Labs. All rights reserved.
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>
14 #include <linux/blk-mq.h>
15 #include <crypto/hash.h>
20 struct nvme_tcp_queue;
22 enum nvme_tcp_send_state {
23 NVME_TCP_SEND_CMD_PDU = 0,
24 NVME_TCP_SEND_H2C_PDU,
29 struct nvme_tcp_request {
30 struct nvme_request req;
32 struct nvme_tcp_queue *queue;
37 struct list_head entry;
46 enum nvme_tcp_send_state state;
49 enum nvme_tcp_queue_flags {
50 NVME_TCP_Q_ALLOCATED = 0,
54 enum nvme_tcp_recv_state {
55 NVME_TCP_RECV_PDU = 0,
61 struct nvme_tcp_queue {
63 struct work_struct io_work;
67 struct list_head send_list;
73 size_t data_remaining;
74 size_t ddgst_remaining;
77 struct nvme_tcp_request *request;
80 size_t cmnd_capsule_len;
81 struct nvme_tcp_ctrl *ctrl;
87 struct ahash_request *rcv_hash;
88 struct ahash_request *snd_hash;
92 struct page_frag_cache pf_cache;
94 void (*state_change)(struct sock *);
95 void (*data_ready)(struct sock *);
96 void (*write_space)(struct sock *);
99 struct nvme_tcp_ctrl {
100 /* read only in the hot path */
101 struct nvme_tcp_queue *queues;
102 struct blk_mq_tag_set tag_set;
104 /* other member variables */
105 struct list_head list;
106 struct blk_mq_tag_set admin_tag_set;
107 struct sockaddr_storage addr;
108 struct sockaddr_storage src_addr;
109 struct nvme_ctrl ctrl;
111 struct work_struct err_work;
112 struct delayed_work connect_work;
113 struct nvme_tcp_request async_req;
116 static LIST_HEAD(nvme_tcp_ctrl_list);
117 static DEFINE_MUTEX(nvme_tcp_ctrl_mutex);
118 static struct workqueue_struct *nvme_tcp_wq;
119 static struct blk_mq_ops nvme_tcp_mq_ops;
120 static struct blk_mq_ops nvme_tcp_admin_mq_ops;
122 static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl)
124 return container_of(ctrl, struct nvme_tcp_ctrl, ctrl);
127 static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
129 return queue - queue->ctrl->queues;
132 static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue)
134 u32 queue_idx = nvme_tcp_queue_id(queue);
137 return queue->ctrl->admin_tag_set.tags[queue_idx];
138 return queue->ctrl->tag_set.tags[queue_idx - 1];
141 static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue)
143 return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
146 static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue)
148 return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
151 static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_queue *queue)
153 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
156 static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req)
158 return req == &req->queue->ctrl->async_req;
161 static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req)
166 if (unlikely(nvme_tcp_async_req(req)))
167 return false; /* async events don't have a request */
169 rq = blk_mq_rq_from_pdu(req);
170 bytes = blk_rq_payload_bytes(rq);
172 return rq_data_dir(rq) == WRITE && bytes &&
173 bytes <= nvme_tcp_inline_data_size(req->queue);
176 static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req)
178 return req->iter.bvec->bv_page;
181 static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req)
183 return req->iter.bvec->bv_offset + req->iter.iov_offset;
186 static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req)
188 return min_t(size_t, req->iter.bvec->bv_len - req->iter.iov_offset,
189 req->pdu_len - req->pdu_sent);
192 static inline size_t nvme_tcp_req_offset(struct nvme_tcp_request *req)
194 return req->iter.iov_offset;
197 static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req)
199 return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ?
200 req->pdu_len - req->pdu_sent : 0;
203 static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
206 return nvme_tcp_pdu_data_left(req) <= len;
209 static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
212 struct request *rq = blk_mq_rq_from_pdu(req);
218 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
219 vec = &rq->special_vec;
221 size = blk_rq_payload_bytes(rq);
224 struct bio *bio = req->curr_bio;
226 vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
227 nsegs = bio_segments(bio);
228 size = bio->bi_iter.bi_size;
229 offset = bio->bi_iter.bi_bvec_done;
232 iov_iter_bvec(&req->iter, dir, vec, nsegs, size);
233 req->iter.iov_offset = offset;
236 static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req,
239 req->data_sent += len;
240 req->pdu_sent += len;
241 iov_iter_advance(&req->iter, len);
242 if (!iov_iter_count(&req->iter) &&
243 req->data_sent < req->data_len) {
244 req->curr_bio = req->curr_bio->bi_next;
245 nvme_tcp_init_iter(req, WRITE);
249 static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req)
251 struct nvme_tcp_queue *queue = req->queue;
253 spin_lock(&queue->lock);
254 list_add_tail(&req->entry, &queue->send_list);
255 spin_unlock(&queue->lock);
257 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
260 static inline struct nvme_tcp_request *
261 nvme_tcp_fetch_request(struct nvme_tcp_queue *queue)
263 struct nvme_tcp_request *req;
265 spin_lock(&queue->lock);
266 req = list_first_entry_or_null(&queue->send_list,
267 struct nvme_tcp_request, entry);
269 list_del(&req->entry);
270 spin_unlock(&queue->lock);
275 static inline void nvme_tcp_ddgst_final(struct ahash_request *hash, u32 *dgst)
277 ahash_request_set_crypt(hash, NULL, (u8 *)dgst, 0);
278 crypto_ahash_final(hash);
281 static inline void nvme_tcp_ddgst_update(struct ahash_request *hash,
282 struct page *page, off_t off, size_t len)
284 struct scatterlist sg;
286 sg_init_marker(&sg, 1);
287 sg_set_page(&sg, page, len, off);
288 ahash_request_set_crypt(hash, &sg, NULL, len);
289 crypto_ahash_update(hash);
292 static inline void nvme_tcp_hdgst(struct ahash_request *hash,
293 void *pdu, size_t len)
295 struct scatterlist sg;
297 sg_init_one(&sg, pdu, len);
298 ahash_request_set_crypt(hash, &sg, pdu + len, len);
299 crypto_ahash_digest(hash);
302 static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue,
303 void *pdu, size_t pdu_len)
305 struct nvme_tcp_hdr *hdr = pdu;
309 if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
310 dev_err(queue->ctrl->ctrl.device,
311 "queue %d: header digest flag is cleared\n",
312 nvme_tcp_queue_id(queue));
316 recv_digest = *(__le32 *)(pdu + hdr->hlen);
317 nvme_tcp_hdgst(queue->rcv_hash, pdu, pdu_len);
318 exp_digest = *(__le32 *)(pdu + hdr->hlen);
319 if (recv_digest != exp_digest) {
320 dev_err(queue->ctrl->ctrl.device,
321 "header digest error: recv %#x expected %#x\n",
322 le32_to_cpu(recv_digest), le32_to_cpu(exp_digest));
329 static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu)
331 struct nvme_tcp_hdr *hdr = pdu;
332 u8 digest_len = nvme_tcp_hdgst_len(queue);
335 len = le32_to_cpu(hdr->plen) - hdr->hlen -
336 ((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0);
338 if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
339 dev_err(queue->ctrl->ctrl.device,
340 "queue %d: data digest flag is cleared\n",
341 nvme_tcp_queue_id(queue));
344 crypto_ahash_init(queue->rcv_hash);
349 static void nvme_tcp_exit_request(struct blk_mq_tag_set *set,
350 struct request *rq, unsigned int hctx_idx)
352 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
354 page_frag_free(req->pdu);
357 static int nvme_tcp_init_request(struct blk_mq_tag_set *set,
358 struct request *rq, unsigned int hctx_idx,
359 unsigned int numa_node)
361 struct nvme_tcp_ctrl *ctrl = set->driver_data;
362 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
363 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
364 struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx];
365 u8 hdgst = nvme_tcp_hdgst_len(queue);
367 req->pdu = page_frag_alloc(&queue->pf_cache,
368 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
369 GFP_KERNEL | __GFP_ZERO);
374 nvme_req(rq)->ctrl = &ctrl->ctrl;
379 static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
380 unsigned int hctx_idx)
382 struct nvme_tcp_ctrl *ctrl = data;
383 struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1];
385 hctx->driver_data = queue;
389 static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
390 unsigned int hctx_idx)
392 struct nvme_tcp_ctrl *ctrl = data;
393 struct nvme_tcp_queue *queue = &ctrl->queues[0];
395 hctx->driver_data = queue;
399 static enum nvme_tcp_recv_state
400 nvme_tcp_recv_state(struct nvme_tcp_queue *queue)
402 return (queue->pdu_remaining) ? NVME_TCP_RECV_PDU :
403 (queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST :
407 static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
409 queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) +
410 nvme_tcp_hdgst_len(queue);
411 queue->pdu_offset = 0;
412 queue->data_remaining = -1;
413 queue->ddgst_remaining = 0;
416 static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
418 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
421 queue_work(nvme_wq, &to_tcp_ctrl(ctrl)->err_work);
424 static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue,
425 struct nvme_completion *cqe)
429 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), cqe->command_id);
431 dev_err(queue->ctrl->ctrl.device,
432 "queue %d tag 0x%x not found\n",
433 nvme_tcp_queue_id(queue), cqe->command_id);
434 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
438 nvme_end_request(rq, cqe->status, cqe->result);
443 static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
444 struct nvme_tcp_data_pdu *pdu)
448 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
450 dev_err(queue->ctrl->ctrl.device,
451 "queue %d tag %#x not found\n",
452 nvme_tcp_queue_id(queue), pdu->command_id);
456 if (!blk_rq_payload_bytes(rq)) {
457 dev_err(queue->ctrl->ctrl.device,
458 "queue %d tag %#x unexpected data\n",
459 nvme_tcp_queue_id(queue), rq->tag);
463 queue->data_remaining = le32_to_cpu(pdu->data_length);
469 static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue,
470 struct nvme_tcp_rsp_pdu *pdu)
472 struct nvme_completion *cqe = &pdu->cqe;
476 * AEN requests are special as they don't time out and can
477 * survive any kind of queue freeze and often don't respond to
478 * aborts. We don't even bother to allocate a struct request
479 * for them but rather special case them here.
481 if (unlikely(nvme_tcp_queue_id(queue) == 0 &&
482 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
483 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
486 ret = nvme_tcp_process_nvme_cqe(queue, cqe);
491 static int nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req,
492 struct nvme_tcp_r2t_pdu *pdu)
494 struct nvme_tcp_data_pdu *data = req->pdu;
495 struct nvme_tcp_queue *queue = req->queue;
496 struct request *rq = blk_mq_rq_from_pdu(req);
497 u8 hdgst = nvme_tcp_hdgst_len(queue);
498 u8 ddgst = nvme_tcp_ddgst_len(queue);
500 req->pdu_len = le32_to_cpu(pdu->r2t_length);
503 if (unlikely(req->data_sent + req->pdu_len > req->data_len)) {
504 dev_err(queue->ctrl->ctrl.device,
505 "req %d r2t len %u exceeded data len %u (%zu sent)\n",
506 rq->tag, req->pdu_len, req->data_len,
511 if (unlikely(le32_to_cpu(pdu->r2t_offset) < req->data_sent)) {
512 dev_err(queue->ctrl->ctrl.device,
513 "req %d unexpected r2t offset %u (expected %zu)\n",
514 rq->tag, le32_to_cpu(pdu->r2t_offset),
519 memset(data, 0, sizeof(*data));
520 data->hdr.type = nvme_tcp_h2c_data;
521 data->hdr.flags = NVME_TCP_F_DATA_LAST;
522 if (queue->hdr_digest)
523 data->hdr.flags |= NVME_TCP_F_HDGST;
524 if (queue->data_digest)
525 data->hdr.flags |= NVME_TCP_F_DDGST;
526 data->hdr.hlen = sizeof(*data);
527 data->hdr.pdo = data->hdr.hlen + hdgst;
529 cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst);
530 data->ttag = pdu->ttag;
531 data->command_id = rq->tag;
532 data->data_offset = cpu_to_le32(req->data_sent);
533 data->data_length = cpu_to_le32(req->pdu_len);
537 static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
538 struct nvme_tcp_r2t_pdu *pdu)
540 struct nvme_tcp_request *req;
544 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
546 dev_err(queue->ctrl->ctrl.device,
547 "queue %d tag %#x not found\n",
548 nvme_tcp_queue_id(queue), pdu->command_id);
551 req = blk_mq_rq_to_pdu(rq);
553 ret = nvme_tcp_setup_h2c_data_pdu(req, pdu);
557 req->state = NVME_TCP_SEND_H2C_PDU;
560 nvme_tcp_queue_request(req);
565 static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
566 unsigned int *offset, size_t *len)
568 struct nvme_tcp_hdr *hdr;
569 char *pdu = queue->pdu;
570 size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
573 ret = skb_copy_bits(skb, *offset,
574 &pdu[queue->pdu_offset], rcv_len);
578 queue->pdu_remaining -= rcv_len;
579 queue->pdu_offset += rcv_len;
582 if (queue->pdu_remaining)
586 if (queue->hdr_digest) {
587 ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen);
593 if (queue->data_digest) {
594 ret = nvme_tcp_check_ddgst(queue, queue->pdu);
600 case nvme_tcp_c2h_data:
601 ret = nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu);
604 nvme_tcp_init_recv_ctx(queue);
605 ret = nvme_tcp_handle_comp(queue, (void *)queue->pdu);
608 nvme_tcp_init_recv_ctx(queue);
609 ret = nvme_tcp_handle_r2t(queue, (void *)queue->pdu);
612 dev_err(queue->ctrl->ctrl.device,
613 "unsupported pdu type (%d)\n", hdr->type);
620 static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb,
621 unsigned int *offset, size_t *len)
623 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
624 struct nvme_tcp_request *req;
627 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
629 dev_err(queue->ctrl->ctrl.device,
630 "queue %d tag %#x not found\n",
631 nvme_tcp_queue_id(queue), pdu->command_id);
634 req = blk_mq_rq_to_pdu(rq);
639 recv_len = min_t(size_t, *len, queue->data_remaining);
643 if (!iov_iter_count(&req->iter)) {
644 req->curr_bio = req->curr_bio->bi_next;
647 * If we don`t have any bios it means that controller
648 * sent more data than we requested, hence error
650 if (!req->curr_bio) {
651 dev_err(queue->ctrl->ctrl.device,
652 "queue %d no space in request %#x",
653 nvme_tcp_queue_id(queue), rq->tag);
654 nvme_tcp_init_recv_ctx(queue);
657 nvme_tcp_init_iter(req, READ);
660 /* we can read only from what is left in this bio */
661 recv_len = min_t(size_t, recv_len,
662 iov_iter_count(&req->iter));
664 if (queue->data_digest)
665 ret = skb_copy_and_hash_datagram_iter(skb, *offset,
666 &req->iter, recv_len, queue->rcv_hash);
668 ret = skb_copy_datagram_iter(skb, *offset,
669 &req->iter, recv_len);
671 dev_err(queue->ctrl->ctrl.device,
672 "queue %d failed to copy request %#x data",
673 nvme_tcp_queue_id(queue), rq->tag);
679 queue->data_remaining -= recv_len;
682 if (!queue->data_remaining) {
683 if (queue->data_digest) {
684 nvme_tcp_ddgst_final(queue->rcv_hash, &queue->exp_ddgst);
685 queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH;
687 nvme_tcp_init_recv_ctx(queue);
694 static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue,
695 struct sk_buff *skb, unsigned int *offset, size_t *len)
697 char *ddgst = (char *)&queue->recv_ddgst;
698 size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining);
699 off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining;
702 ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len);
706 queue->ddgst_remaining -= recv_len;
709 if (queue->ddgst_remaining)
712 if (queue->recv_ddgst != queue->exp_ddgst) {
713 dev_err(queue->ctrl->ctrl.device,
714 "data digest error: recv %#x expected %#x\n",
715 le32_to_cpu(queue->recv_ddgst),
716 le32_to_cpu(queue->exp_ddgst));
720 nvme_tcp_init_recv_ctx(queue);
724 static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,
725 unsigned int offset, size_t len)
727 struct nvme_tcp_queue *queue = desc->arg.data;
728 size_t consumed = len;
732 switch (nvme_tcp_recv_state(queue)) {
733 case NVME_TCP_RECV_PDU:
734 result = nvme_tcp_recv_pdu(queue, skb, &offset, &len);
736 case NVME_TCP_RECV_DATA:
737 result = nvme_tcp_recv_data(queue, skb, &offset, &len);
739 case NVME_TCP_RECV_DDGST:
740 result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len);
746 dev_err(queue->ctrl->ctrl.device,
747 "receive failed: %d\n", result);
748 queue->rd_enabled = false;
749 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
757 static void nvme_tcp_data_ready(struct sock *sk)
759 struct nvme_tcp_queue *queue;
761 read_lock(&sk->sk_callback_lock);
762 queue = sk->sk_user_data;
763 if (likely(queue && queue->rd_enabled))
764 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
765 read_unlock(&sk->sk_callback_lock);
768 static void nvme_tcp_write_space(struct sock *sk)
770 struct nvme_tcp_queue *queue;
772 read_lock_bh(&sk->sk_callback_lock);
773 queue = sk->sk_user_data;
774 if (likely(queue && sk_stream_is_writeable(sk))) {
775 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
776 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
778 read_unlock_bh(&sk->sk_callback_lock);
781 static void nvme_tcp_state_change(struct sock *sk)
783 struct nvme_tcp_queue *queue;
785 read_lock(&sk->sk_callback_lock);
786 queue = sk->sk_user_data;
790 switch (sk->sk_state) {
797 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
800 dev_info(queue->ctrl->ctrl.device,
801 "queue %d socket state %d\n",
802 nvme_tcp_queue_id(queue), sk->sk_state);
805 queue->state_change(sk);
807 read_unlock(&sk->sk_callback_lock);
810 static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue)
812 queue->request = NULL;
815 static void nvme_tcp_fail_request(struct nvme_tcp_request *req)
817 union nvme_result res = {};
819 nvme_end_request(blk_mq_rq_from_pdu(req),
820 NVME_SC_DATA_XFER_ERROR, res);
823 static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
825 struct nvme_tcp_queue *queue = req->queue;
828 struct page *page = nvme_tcp_req_cur_page(req);
829 size_t offset = nvme_tcp_req_cur_offset(req);
830 size_t len = nvme_tcp_req_cur_length(req);
831 bool last = nvme_tcp_pdu_last_send(req, len);
832 int ret, flags = MSG_DONTWAIT;
834 if (last && !queue->data_digest)
839 ret = kernel_sendpage(queue->sock, page, offset, len, flags);
843 nvme_tcp_advance_req(req, ret);
844 if (queue->data_digest)
845 nvme_tcp_ddgst_update(queue->snd_hash, page,
848 /* fully successful last write*/
849 if (last && ret == len) {
850 if (queue->data_digest) {
851 nvme_tcp_ddgst_final(queue->snd_hash,
853 req->state = NVME_TCP_SEND_DDGST;
856 nvme_tcp_done_send_req(queue);
864 static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
866 struct nvme_tcp_queue *queue = req->queue;
867 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
868 bool inline_data = nvme_tcp_has_inline_data(req);
869 int flags = MSG_DONTWAIT | (inline_data ? MSG_MORE : MSG_EOR);
870 u8 hdgst = nvme_tcp_hdgst_len(queue);
871 int len = sizeof(*pdu) + hdgst - req->offset;
874 if (queue->hdr_digest && !req->offset)
875 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
877 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
878 offset_in_page(pdu) + req->offset, len, flags);
879 if (unlikely(ret <= 0))
885 req->state = NVME_TCP_SEND_DATA;
886 if (queue->data_digest)
887 crypto_ahash_init(queue->snd_hash);
888 nvme_tcp_init_iter(req, WRITE);
890 nvme_tcp_done_send_req(queue);
899 static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
901 struct nvme_tcp_queue *queue = req->queue;
902 struct nvme_tcp_data_pdu *pdu = req->pdu;
903 u8 hdgst = nvme_tcp_hdgst_len(queue);
904 int len = sizeof(*pdu) - req->offset + hdgst;
907 if (queue->hdr_digest && !req->offset)
908 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
910 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
911 offset_in_page(pdu) + req->offset, len,
912 MSG_DONTWAIT | MSG_MORE);
913 if (unlikely(ret <= 0))
918 req->state = NVME_TCP_SEND_DATA;
919 if (queue->data_digest)
920 crypto_ahash_init(queue->snd_hash);
922 nvme_tcp_init_iter(req, WRITE);
930 static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
932 struct nvme_tcp_queue *queue = req->queue;
934 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_EOR };
936 .iov_base = &req->ddgst + req->offset,
937 .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset
940 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
941 if (unlikely(ret <= 0))
944 if (req->offset + ret == NVME_TCP_DIGEST_LENGTH) {
945 nvme_tcp_done_send_req(queue);
953 static int nvme_tcp_try_send(struct nvme_tcp_queue *queue)
955 struct nvme_tcp_request *req;
958 if (!queue->request) {
959 queue->request = nvme_tcp_fetch_request(queue);
963 req = queue->request;
965 if (req->state == NVME_TCP_SEND_CMD_PDU) {
966 ret = nvme_tcp_try_send_cmd_pdu(req);
969 if (!nvme_tcp_has_inline_data(req))
973 if (req->state == NVME_TCP_SEND_H2C_PDU) {
974 ret = nvme_tcp_try_send_data_pdu(req);
979 if (req->state == NVME_TCP_SEND_DATA) {
980 ret = nvme_tcp_try_send_data(req);
985 if (req->state == NVME_TCP_SEND_DDGST)
986 ret = nvme_tcp_try_send_ddgst(req);
993 static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue)
995 struct sock *sk = queue->sock->sk;
996 read_descriptor_t rd_desc;
999 rd_desc.arg.data = queue;
1002 consumed = tcp_read_sock(sk, &rd_desc, nvme_tcp_recv_skb);
1007 static void nvme_tcp_io_work(struct work_struct *w)
1009 struct nvme_tcp_queue *queue =
1010 container_of(w, struct nvme_tcp_queue, io_work);
1011 unsigned long start = jiffies + msecs_to_jiffies(1);
1014 bool pending = false;
1017 result = nvme_tcp_try_send(queue);
1020 } else if (unlikely(result < 0)) {
1021 dev_err(queue->ctrl->ctrl.device,
1022 "failed to send request %d\n", result);
1023 if (result != -EPIPE)
1024 nvme_tcp_fail_request(queue->request);
1025 nvme_tcp_done_send_req(queue);
1029 result = nvme_tcp_try_recv(queue);
1036 } while (time_after(jiffies, start)); /* quota is exhausted */
1038 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
1041 static void nvme_tcp_free_crypto(struct nvme_tcp_queue *queue)
1043 struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
1045 ahash_request_free(queue->rcv_hash);
1046 ahash_request_free(queue->snd_hash);
1047 crypto_free_ahash(tfm);
1050 static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue *queue)
1052 struct crypto_ahash *tfm;
1054 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
1056 return PTR_ERR(tfm);
1058 queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1059 if (!queue->snd_hash)
1061 ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
1063 queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1064 if (!queue->rcv_hash)
1066 ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
1070 ahash_request_free(queue->snd_hash);
1072 crypto_free_ahash(tfm);
1076 static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl)
1078 struct nvme_tcp_request *async = &ctrl->async_req;
1080 page_frag_free(async->pdu);
1083 static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
1085 struct nvme_tcp_queue *queue = &ctrl->queues[0];
1086 struct nvme_tcp_request *async = &ctrl->async_req;
1087 u8 hdgst = nvme_tcp_hdgst_len(queue);
1089 async->pdu = page_frag_alloc(&queue->pf_cache,
1090 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
1091 GFP_KERNEL | __GFP_ZERO);
1095 async->queue = &ctrl->queues[0];
1099 static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
1101 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1102 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1104 if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
1107 if (queue->hdr_digest || queue->data_digest)
1108 nvme_tcp_free_crypto(queue);
1110 sock_release(queue->sock);
1114 static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
1116 struct nvme_tcp_icreq_pdu *icreq;
1117 struct nvme_tcp_icresp_pdu *icresp;
1118 struct msghdr msg = {};
1120 bool ctrl_hdgst, ctrl_ddgst;
1123 icreq = kzalloc(sizeof(*icreq), GFP_KERNEL);
1127 icresp = kzalloc(sizeof(*icresp), GFP_KERNEL);
1133 icreq->hdr.type = nvme_tcp_icreq;
1134 icreq->hdr.hlen = sizeof(*icreq);
1136 icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen);
1137 icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
1138 icreq->maxr2t = 0; /* single inflight r2t supported */
1139 icreq->hpda = 0; /* no alignment constraint */
1140 if (queue->hdr_digest)
1141 icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
1142 if (queue->data_digest)
1143 icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
1145 iov.iov_base = icreq;
1146 iov.iov_len = sizeof(*icreq);
1147 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1151 memset(&msg, 0, sizeof(msg));
1152 iov.iov_base = icresp;
1153 iov.iov_len = sizeof(*icresp);
1154 ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1155 iov.iov_len, msg.msg_flags);
1160 if (icresp->hdr.type != nvme_tcp_icresp) {
1161 pr_err("queue %d: bad type returned %d\n",
1162 nvme_tcp_queue_id(queue), icresp->hdr.type);
1166 if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) {
1167 pr_err("queue %d: bad pdu length returned %d\n",
1168 nvme_tcp_queue_id(queue), icresp->hdr.plen);
1172 if (icresp->pfv != NVME_TCP_PFV_1_0) {
1173 pr_err("queue %d: bad pfv returned %d\n",
1174 nvme_tcp_queue_id(queue), icresp->pfv);
1178 ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE);
1179 if ((queue->data_digest && !ctrl_ddgst) ||
1180 (!queue->data_digest && ctrl_ddgst)) {
1181 pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
1182 nvme_tcp_queue_id(queue),
1183 queue->data_digest ? "enabled" : "disabled",
1184 ctrl_ddgst ? "enabled" : "disabled");
1188 ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE);
1189 if ((queue->hdr_digest && !ctrl_hdgst) ||
1190 (!queue->hdr_digest && ctrl_hdgst)) {
1191 pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
1192 nvme_tcp_queue_id(queue),
1193 queue->hdr_digest ? "enabled" : "disabled",
1194 ctrl_hdgst ? "enabled" : "disabled");
1198 if (icresp->cpda != 0) {
1199 pr_err("queue %d: unsupported cpda returned %d\n",
1200 nvme_tcp_queue_id(queue), icresp->cpda);
1212 static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl,
1213 int qid, size_t queue_size)
1215 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1216 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1217 struct linger sol = { .l_onoff = 1, .l_linger = 0 };
1218 int ret, opt, rcv_pdu_size;
1221 INIT_LIST_HEAD(&queue->send_list);
1222 spin_lock_init(&queue->lock);
1223 INIT_WORK(&queue->io_work, nvme_tcp_io_work);
1224 queue->queue_size = queue_size;
1227 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1229 queue->cmnd_capsule_len = sizeof(struct nvme_command) +
1230 NVME_TCP_ADMIN_CCSZ;
1232 ret = sock_create(ctrl->addr.ss_family, SOCK_STREAM,
1233 IPPROTO_TCP, &queue->sock);
1235 dev_err(ctrl->ctrl.device,
1236 "failed to create socket: %d\n", ret);
1240 /* Single syn retry */
1242 ret = kernel_setsockopt(queue->sock, IPPROTO_TCP, TCP_SYNCNT,
1243 (char *)&opt, sizeof(opt));
1245 dev_err(ctrl->ctrl.device,
1246 "failed to set TCP_SYNCNT sock opt %d\n", ret);
1250 /* Set TCP no delay */
1252 ret = kernel_setsockopt(queue->sock, IPPROTO_TCP,
1253 TCP_NODELAY, (char *)&opt, sizeof(opt));
1255 dev_err(ctrl->ctrl.device,
1256 "failed to set TCP_NODELAY sock opt %d\n", ret);
1261 * Cleanup whatever is sitting in the TCP transmit queue on socket
1262 * close. This is done to prevent stale data from being sent should
1263 * the network connection be restored before TCP times out.
1265 ret = kernel_setsockopt(queue->sock, SOL_SOCKET, SO_LINGER,
1266 (char *)&sol, sizeof(sol));
1268 dev_err(ctrl->ctrl.device,
1269 "failed to set SO_LINGER sock opt %d\n", ret);
1273 queue->sock->sk->sk_allocation = GFP_ATOMIC;
1274 queue->io_cpu = (qid == 0) ? 0 : qid - 1;
1275 queue->request = NULL;
1276 queue->data_remaining = 0;
1277 queue->ddgst_remaining = 0;
1278 queue->pdu_remaining = 0;
1279 queue->pdu_offset = 0;
1280 sk_set_memalloc(queue->sock->sk);
1282 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) {
1283 ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr,
1284 sizeof(ctrl->src_addr));
1286 dev_err(ctrl->ctrl.device,
1287 "failed to bind queue %d socket %d\n",
1293 queue->hdr_digest = nctrl->opts->hdr_digest;
1294 queue->data_digest = nctrl->opts->data_digest;
1295 if (queue->hdr_digest || queue->data_digest) {
1296 ret = nvme_tcp_alloc_crypto(queue);
1298 dev_err(ctrl->ctrl.device,
1299 "failed to allocate queue %d crypto\n", qid);
1304 rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) +
1305 nvme_tcp_hdgst_len(queue);
1306 queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL);
1312 dev_dbg(ctrl->ctrl.device, "connecting queue %d\n",
1313 nvme_tcp_queue_id(queue));
1315 ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr,
1316 sizeof(ctrl->addr), 0);
1318 dev_err(ctrl->ctrl.device,
1319 "failed to connect socket: %d\n", ret);
1323 ret = nvme_tcp_init_connection(queue);
1325 goto err_init_connect;
1327 queue->rd_enabled = true;
1328 set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags);
1329 nvme_tcp_init_recv_ctx(queue);
1331 write_lock_bh(&queue->sock->sk->sk_callback_lock);
1332 queue->sock->sk->sk_user_data = queue;
1333 queue->state_change = queue->sock->sk->sk_state_change;
1334 queue->data_ready = queue->sock->sk->sk_data_ready;
1335 queue->write_space = queue->sock->sk->sk_write_space;
1336 queue->sock->sk->sk_data_ready = nvme_tcp_data_ready;
1337 queue->sock->sk->sk_state_change = nvme_tcp_state_change;
1338 queue->sock->sk->sk_write_space = nvme_tcp_write_space;
1339 write_unlock_bh(&queue->sock->sk->sk_callback_lock);
1344 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1348 if (queue->hdr_digest || queue->data_digest)
1349 nvme_tcp_free_crypto(queue);
1351 sock_release(queue->sock);
1356 static void nvme_tcp_restore_sock_calls(struct nvme_tcp_queue *queue)
1358 struct socket *sock = queue->sock;
1360 write_lock_bh(&sock->sk->sk_callback_lock);
1361 sock->sk->sk_user_data = NULL;
1362 sock->sk->sk_data_ready = queue->data_ready;
1363 sock->sk->sk_state_change = queue->state_change;
1364 sock->sk->sk_write_space = queue->write_space;
1365 write_unlock_bh(&sock->sk->sk_callback_lock);
1368 static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
1370 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1371 nvme_tcp_restore_sock_calls(queue);
1372 cancel_work_sync(&queue->io_work);
1375 static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
1377 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1378 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1380 if (!test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
1383 __nvme_tcp_stop_queue(queue);
1386 static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
1388 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1392 ret = nvmf_connect_io_queue(nctrl, idx);
1394 ret = nvmf_connect_admin_queue(nctrl);
1397 set_bit(NVME_TCP_Q_LIVE, &ctrl->queues[idx].flags);
1399 __nvme_tcp_stop_queue(&ctrl->queues[idx]);
1400 dev_err(nctrl->device,
1401 "failed to connect queue: %d ret=%d\n", idx, ret);
1406 static struct blk_mq_tag_set *nvme_tcp_alloc_tagset(struct nvme_ctrl *nctrl,
1409 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1410 struct blk_mq_tag_set *set;
1414 set = &ctrl->admin_tag_set;
1415 memset(set, 0, sizeof(*set));
1416 set->ops = &nvme_tcp_admin_mq_ops;
1417 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
1418 set->reserved_tags = 2; /* connect + keep-alive */
1419 set->numa_node = NUMA_NO_NODE;
1420 set->cmd_size = sizeof(struct nvme_tcp_request);
1421 set->driver_data = ctrl;
1422 set->nr_hw_queues = 1;
1423 set->timeout = ADMIN_TIMEOUT;
1425 set = &ctrl->tag_set;
1426 memset(set, 0, sizeof(*set));
1427 set->ops = &nvme_tcp_mq_ops;
1428 set->queue_depth = nctrl->sqsize + 1;
1429 set->reserved_tags = 1; /* fabric connect */
1430 set->numa_node = NUMA_NO_NODE;
1431 set->flags = BLK_MQ_F_SHOULD_MERGE;
1432 set->cmd_size = sizeof(struct nvme_tcp_request);
1433 set->driver_data = ctrl;
1434 set->nr_hw_queues = nctrl->queue_count - 1;
1435 set->timeout = NVME_IO_TIMEOUT;
1438 ret = blk_mq_alloc_tag_set(set);
1440 return ERR_PTR(ret);
1445 static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl)
1447 if (to_tcp_ctrl(ctrl)->async_req.pdu) {
1448 nvme_tcp_free_async_req(to_tcp_ctrl(ctrl));
1449 to_tcp_ctrl(ctrl)->async_req.pdu = NULL;
1452 nvme_tcp_free_queue(ctrl, 0);
1455 static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl)
1459 for (i = 1; i < ctrl->queue_count; i++)
1460 nvme_tcp_free_queue(ctrl, i);
1463 static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl)
1467 for (i = 1; i < ctrl->queue_count; i++)
1468 nvme_tcp_stop_queue(ctrl, i);
1471 static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl)
1475 for (i = 1; i < ctrl->queue_count; i++) {
1476 ret = nvme_tcp_start_queue(ctrl, i);
1478 goto out_stop_queues;
1484 for (i--; i >= 1; i--)
1485 nvme_tcp_stop_queue(ctrl, i);
1489 static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
1493 ret = nvme_tcp_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
1497 ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
1499 goto out_free_queue;
1504 nvme_tcp_free_queue(ctrl, 0);
1508 static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
1512 for (i = 1; i < ctrl->queue_count; i++) {
1513 ret = nvme_tcp_alloc_queue(ctrl, i,
1516 goto out_free_queues;
1522 for (i--; i >= 1; i--)
1523 nvme_tcp_free_queue(ctrl, i);
1528 static unsigned int nvme_tcp_nr_io_queues(struct nvme_ctrl *ctrl)
1530 return min(ctrl->queue_count - 1, num_online_cpus());
1533 static int nvme_alloc_io_queues(struct nvme_ctrl *ctrl)
1535 unsigned int nr_io_queues;
1538 nr_io_queues = nvme_tcp_nr_io_queues(ctrl);
1539 ret = nvme_set_queue_count(ctrl, &nr_io_queues);
1543 ctrl->queue_count = nr_io_queues + 1;
1544 if (ctrl->queue_count < 2)
1547 dev_info(ctrl->device,
1548 "creating %d I/O queues.\n", nr_io_queues);
1550 return nvme_tcp_alloc_io_queues(ctrl);
1553 static void nvme_tcp_destroy_io_queues(struct nvme_ctrl *ctrl, bool remove)
1555 nvme_tcp_stop_io_queues(ctrl);
1557 if (ctrl->ops->flags & NVME_F_FABRICS)
1558 blk_cleanup_queue(ctrl->connect_q);
1559 blk_mq_free_tag_set(ctrl->tagset);
1561 nvme_tcp_free_io_queues(ctrl);
1564 static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
1568 ret = nvme_alloc_io_queues(ctrl);
1573 ctrl->tagset = nvme_tcp_alloc_tagset(ctrl, false);
1574 if (IS_ERR(ctrl->tagset)) {
1575 ret = PTR_ERR(ctrl->tagset);
1576 goto out_free_io_queues;
1579 if (ctrl->ops->flags & NVME_F_FABRICS) {
1580 ctrl->connect_q = blk_mq_init_queue(ctrl->tagset);
1581 if (IS_ERR(ctrl->connect_q)) {
1582 ret = PTR_ERR(ctrl->connect_q);
1583 goto out_free_tag_set;
1587 blk_mq_update_nr_hw_queues(ctrl->tagset,
1588 ctrl->queue_count - 1);
1591 ret = nvme_tcp_start_io_queues(ctrl);
1593 goto out_cleanup_connect_q;
1597 out_cleanup_connect_q:
1598 if (new && (ctrl->ops->flags & NVME_F_FABRICS))
1599 blk_cleanup_queue(ctrl->connect_q);
1602 blk_mq_free_tag_set(ctrl->tagset);
1604 nvme_tcp_free_io_queues(ctrl);
1608 static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl *ctrl, bool remove)
1610 nvme_tcp_stop_queue(ctrl, 0);
1612 free_opal_dev(ctrl->opal_dev);
1613 blk_cleanup_queue(ctrl->admin_q);
1614 blk_mq_free_tag_set(ctrl->admin_tagset);
1616 nvme_tcp_free_admin_queue(ctrl);
1619 static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
1623 error = nvme_tcp_alloc_admin_queue(ctrl);
1628 ctrl->admin_tagset = nvme_tcp_alloc_tagset(ctrl, true);
1629 if (IS_ERR(ctrl->admin_tagset)) {
1630 error = PTR_ERR(ctrl->admin_tagset);
1631 goto out_free_queue;
1634 ctrl->admin_q = blk_mq_init_queue(ctrl->admin_tagset);
1635 if (IS_ERR(ctrl->admin_q)) {
1636 error = PTR_ERR(ctrl->admin_q);
1637 goto out_free_tagset;
1641 error = nvme_tcp_start_queue(ctrl, 0);
1643 goto out_cleanup_queue;
1645 error = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &ctrl->cap);
1647 dev_err(ctrl->device,
1648 "prop_get NVME_REG_CAP failed\n");
1649 goto out_stop_queue;
1652 ctrl->sqsize = min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->sqsize);
1654 error = nvme_enable_ctrl(ctrl, ctrl->cap);
1656 goto out_stop_queue;
1658 error = nvme_init_identify(ctrl);
1660 goto out_stop_queue;
1665 nvme_tcp_stop_queue(ctrl, 0);
1668 blk_cleanup_queue(ctrl->admin_q);
1671 blk_mq_free_tag_set(ctrl->admin_tagset);
1673 nvme_tcp_free_admin_queue(ctrl);
1677 static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
1680 blk_mq_quiesce_queue(ctrl->admin_q);
1681 nvme_tcp_stop_queue(ctrl, 0);
1682 blk_mq_tagset_busy_iter(ctrl->admin_tagset, nvme_cancel_request, ctrl);
1683 blk_mq_unquiesce_queue(ctrl->admin_q);
1684 nvme_tcp_destroy_admin_queue(ctrl, remove);
1687 static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
1690 if (ctrl->queue_count <= 1)
1692 nvme_stop_queues(ctrl);
1693 nvme_tcp_stop_io_queues(ctrl);
1694 blk_mq_tagset_busy_iter(ctrl->tagset, nvme_cancel_request, ctrl);
1696 nvme_start_queues(ctrl);
1697 nvme_tcp_destroy_io_queues(ctrl, remove);
1700 static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl)
1702 /* If we are resetting/deleting then do nothing */
1703 if (ctrl->state != NVME_CTRL_CONNECTING) {
1704 WARN_ON_ONCE(ctrl->state == NVME_CTRL_NEW ||
1705 ctrl->state == NVME_CTRL_LIVE);
1709 if (nvmf_should_reconnect(ctrl)) {
1710 dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
1711 ctrl->opts->reconnect_delay);
1712 queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
1713 ctrl->opts->reconnect_delay * HZ);
1715 dev_info(ctrl->device, "Removing controller...\n");
1716 nvme_delete_ctrl(ctrl);
1720 static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
1722 struct nvmf_ctrl_options *opts = ctrl->opts;
1725 ret = nvme_tcp_configure_admin_queue(ctrl, new);
1730 dev_err(ctrl->device, "icdoff is not supported!\n");
1734 if (opts->queue_size > ctrl->sqsize + 1)
1735 dev_warn(ctrl->device,
1736 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1737 opts->queue_size, ctrl->sqsize + 1);
1739 if (ctrl->sqsize + 1 > ctrl->maxcmd) {
1740 dev_warn(ctrl->device,
1741 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1742 ctrl->sqsize + 1, ctrl->maxcmd);
1743 ctrl->sqsize = ctrl->maxcmd - 1;
1746 if (ctrl->queue_count > 1) {
1747 ret = nvme_tcp_configure_io_queues(ctrl, new);
1752 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
1753 /* state change failure is ok if we're in DELETING state */
1754 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
1759 nvme_start_ctrl(ctrl);
1763 if (ctrl->queue_count > 1)
1764 nvme_tcp_destroy_io_queues(ctrl, new);
1766 nvme_tcp_stop_queue(ctrl, 0);
1767 nvme_tcp_destroy_admin_queue(ctrl, new);
1771 static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
1773 struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
1774 struct nvme_tcp_ctrl, connect_work);
1775 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
1777 ++ctrl->nr_reconnects;
1779 if (nvme_tcp_setup_ctrl(ctrl, false))
1782 dev_info(ctrl->device, "Successfully reconnected (%d attepmpt)\n",
1783 ctrl->nr_reconnects);
1785 ctrl->nr_reconnects = 0;
1790 dev_info(ctrl->device, "Failed reconnect attempt %d\n",
1791 ctrl->nr_reconnects);
1792 nvme_tcp_reconnect_or_remove(ctrl);
1795 static void nvme_tcp_error_recovery_work(struct work_struct *work)
1797 struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
1798 struct nvme_tcp_ctrl, err_work);
1799 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
1801 nvme_stop_keep_alive(ctrl);
1802 nvme_tcp_teardown_io_queues(ctrl, false);
1803 /* unquiesce to fail fast pending requests */
1804 nvme_start_queues(ctrl);
1805 nvme_tcp_teardown_admin_queue(ctrl, false);
1807 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
1808 /* state change failure is ok if we're in DELETING state */
1809 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
1813 nvme_tcp_reconnect_or_remove(ctrl);
1816 static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
1818 nvme_tcp_teardown_io_queues(ctrl, shutdown);
1820 nvme_shutdown_ctrl(ctrl);
1822 nvme_disable_ctrl(ctrl, ctrl->cap);
1823 nvme_tcp_teardown_admin_queue(ctrl, shutdown);
1826 static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
1828 nvme_tcp_teardown_ctrl(ctrl, true);
1831 static void nvme_reset_ctrl_work(struct work_struct *work)
1833 struct nvme_ctrl *ctrl =
1834 container_of(work, struct nvme_ctrl, reset_work);
1836 nvme_stop_ctrl(ctrl);
1837 nvme_tcp_teardown_ctrl(ctrl, false);
1839 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
1840 /* state change failure is ok if we're in DELETING state */
1841 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
1845 if (nvme_tcp_setup_ctrl(ctrl, false))
1851 ++ctrl->nr_reconnects;
1852 nvme_tcp_reconnect_or_remove(ctrl);
1855 static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl)
1857 cancel_work_sync(&to_tcp_ctrl(ctrl)->err_work);
1858 cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
1861 static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
1863 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1865 if (list_empty(&ctrl->list))
1868 mutex_lock(&nvme_tcp_ctrl_mutex);
1869 list_del(&ctrl->list);
1870 mutex_unlock(&nvme_tcp_ctrl_mutex);
1872 nvmf_free_options(nctrl->opts);
1874 kfree(ctrl->queues);
1878 static void nvme_tcp_set_sg_null(struct nvme_command *c)
1880 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1884 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
1885 NVME_SGL_FMT_TRANSPORT_A;
1888 static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
1889 struct nvme_command *c, u32 data_len)
1891 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1893 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1894 sg->length = cpu_to_le32(data_len);
1895 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1898 static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
1901 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1904 sg->length = cpu_to_le32(data_len);
1905 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
1906 NVME_SGL_FMT_TRANSPORT_A;
1909 static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
1911 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
1912 struct nvme_tcp_queue *queue = &ctrl->queues[0];
1913 struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
1914 struct nvme_command *cmd = &pdu->cmd;
1915 u8 hdgst = nvme_tcp_hdgst_len(queue);
1917 memset(pdu, 0, sizeof(*pdu));
1918 pdu->hdr.type = nvme_tcp_cmd;
1919 if (queue->hdr_digest)
1920 pdu->hdr.flags |= NVME_TCP_F_HDGST;
1921 pdu->hdr.hlen = sizeof(*pdu);
1922 pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
1924 cmd->common.opcode = nvme_admin_async_event;
1925 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1926 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1927 nvme_tcp_set_sg_null(cmd);
1929 ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
1930 ctrl->async_req.offset = 0;
1931 ctrl->async_req.curr_bio = NULL;
1932 ctrl->async_req.data_len = 0;
1934 nvme_tcp_queue_request(&ctrl->async_req);
1937 static enum blk_eh_timer_return
1938 nvme_tcp_timeout(struct request *rq, bool reserved)
1940 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
1941 struct nvme_tcp_ctrl *ctrl = req->queue->ctrl;
1942 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
1944 dev_dbg(ctrl->ctrl.device,
1945 "queue %d: timeout request %#x type %d\n",
1946 nvme_tcp_queue_id(req->queue), rq->tag,
1949 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
1950 union nvme_result res = {};
1952 nvme_req(rq)->flags |= NVME_REQ_CANCELLED;
1953 nvme_end_request(rq, NVME_SC_ABORT_REQ, res);
1957 /* queue error recovery */
1958 nvme_tcp_error_recovery(&ctrl->ctrl);
1960 return BLK_EH_RESET_TIMER;
1963 static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
1966 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
1967 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
1968 struct nvme_command *c = &pdu->cmd;
1970 c->common.flags |= NVME_CMD_SGL_METABUF;
1972 if (rq_data_dir(rq) == WRITE && req->data_len &&
1973 req->data_len <= nvme_tcp_inline_data_size(queue))
1974 nvme_tcp_set_sg_inline(queue, c, req->data_len);
1976 nvme_tcp_set_sg_host_data(c, req->data_len);
1981 static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
1984 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
1985 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
1986 struct nvme_tcp_queue *queue = req->queue;
1987 u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
1990 ret = nvme_setup_cmd(ns, rq, &pdu->cmd);
1994 req->state = NVME_TCP_SEND_CMD_PDU;
1999 req->data_len = blk_rq_payload_bytes(rq);
2000 req->curr_bio = rq->bio;
2002 if (rq_data_dir(rq) == WRITE &&
2003 req->data_len <= nvme_tcp_inline_data_size(queue))
2004 req->pdu_len = req->data_len;
2005 else if (req->curr_bio)
2006 nvme_tcp_init_iter(req, READ);
2008 pdu->hdr.type = nvme_tcp_cmd;
2010 if (queue->hdr_digest)
2011 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2012 if (queue->data_digest && req->pdu_len) {
2013 pdu->hdr.flags |= NVME_TCP_F_DDGST;
2014 ddgst = nvme_tcp_ddgst_len(queue);
2016 pdu->hdr.hlen = sizeof(*pdu);
2017 pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
2019 cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
2021 ret = nvme_tcp_map_data(queue, rq);
2022 if (unlikely(ret)) {
2023 dev_err(queue->ctrl->ctrl.device,
2024 "Failed to map data (%d)\n", ret);
2031 static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
2032 const struct blk_mq_queue_data *bd)
2034 struct nvme_ns *ns = hctx->queue->queuedata;
2035 struct nvme_tcp_queue *queue = hctx->driver_data;
2036 struct request *rq = bd->rq;
2037 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2038 bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
2041 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2042 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
2044 ret = nvme_tcp_setup_cmd_pdu(ns, rq);
2048 blk_mq_start_request(rq);
2050 nvme_tcp_queue_request(req);
2055 static struct blk_mq_ops nvme_tcp_mq_ops = {
2056 .queue_rq = nvme_tcp_queue_rq,
2057 .complete = nvme_complete_rq,
2058 .init_request = nvme_tcp_init_request,
2059 .exit_request = nvme_tcp_exit_request,
2060 .init_hctx = nvme_tcp_init_hctx,
2061 .timeout = nvme_tcp_timeout,
2064 static struct blk_mq_ops nvme_tcp_admin_mq_ops = {
2065 .queue_rq = nvme_tcp_queue_rq,
2066 .complete = nvme_complete_rq,
2067 .init_request = nvme_tcp_init_request,
2068 .exit_request = nvme_tcp_exit_request,
2069 .init_hctx = nvme_tcp_init_admin_hctx,
2070 .timeout = nvme_tcp_timeout,
2073 static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
2075 .module = THIS_MODULE,
2076 .flags = NVME_F_FABRICS,
2077 .reg_read32 = nvmf_reg_read32,
2078 .reg_read64 = nvmf_reg_read64,
2079 .reg_write32 = nvmf_reg_write32,
2080 .free_ctrl = nvme_tcp_free_ctrl,
2081 .submit_async_event = nvme_tcp_submit_async_event,
2082 .delete_ctrl = nvme_tcp_delete_ctrl,
2083 .get_address = nvmf_get_address,
2084 .stop_ctrl = nvme_tcp_stop_ctrl,
2088 nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
2090 struct nvme_tcp_ctrl *ctrl;
2093 mutex_lock(&nvme_tcp_ctrl_mutex);
2094 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
2095 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2099 mutex_unlock(&nvme_tcp_ctrl_mutex);
2104 static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
2105 struct nvmf_ctrl_options *opts)
2107 struct nvme_tcp_ctrl *ctrl;
2110 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2112 return ERR_PTR(-ENOMEM);
2114 INIT_LIST_HEAD(&ctrl->list);
2115 ctrl->ctrl.opts = opts;
2116 ctrl->ctrl.queue_count = opts->nr_io_queues + 1; /* +1 for admin queue */
2117 ctrl->ctrl.sqsize = opts->queue_size - 1;
2118 ctrl->ctrl.kato = opts->kato;
2120 INIT_DELAYED_WORK(&ctrl->connect_work,
2121 nvme_tcp_reconnect_ctrl_work);
2122 INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
2123 INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
2125 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2127 kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
2128 if (!opts->trsvcid) {
2132 opts->mask |= NVMF_OPT_TRSVCID;
2135 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2136 opts->traddr, opts->trsvcid, &ctrl->addr);
2138 pr_err("malformed address passed: %s:%s\n",
2139 opts->traddr, opts->trsvcid);
2143 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2144 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2145 opts->host_traddr, NULL, &ctrl->src_addr);
2147 pr_err("malformed src address passed: %s\n",
2153 if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
2158 ctrl->queues = kcalloc(opts->nr_io_queues + 1, sizeof(*ctrl->queues),
2160 if (!ctrl->queues) {
2165 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
2167 goto out_kfree_queues;
2169 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2172 goto out_uninit_ctrl;
2175 ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
2177 goto out_uninit_ctrl;
2179 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
2180 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2182 nvme_get_ctrl(&ctrl->ctrl);
2184 mutex_lock(&nvme_tcp_ctrl_mutex);
2185 list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
2186 mutex_unlock(&nvme_tcp_ctrl_mutex);
2191 nvme_uninit_ctrl(&ctrl->ctrl);
2192 nvme_put_ctrl(&ctrl->ctrl);
2195 return ERR_PTR(ret);
2197 kfree(ctrl->queues);
2200 return ERR_PTR(ret);
2203 static struct nvmf_transport_ops nvme_tcp_transport = {
2205 .module = THIS_MODULE,
2206 .required_opts = NVMF_OPT_TRADDR,
2207 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2208 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2209 NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST,
2210 .create_ctrl = nvme_tcp_create_ctrl,
2213 static int __init nvme_tcp_init_module(void)
2215 nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq",
2216 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2220 nvmf_register_transport(&nvme_tcp_transport);
2224 static void __exit nvme_tcp_cleanup_module(void)
2226 struct nvme_tcp_ctrl *ctrl;
2228 nvmf_unregister_transport(&nvme_tcp_transport);
2230 mutex_lock(&nvme_tcp_ctrl_mutex);
2231 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
2232 nvme_delete_ctrl(&ctrl->ctrl);
2233 mutex_unlock(&nvme_tcp_ctrl_mutex);
2234 flush_workqueue(nvme_delete_wq);
2236 destroy_workqueue(nvme_tcp_wq);
2239 module_init(nvme_tcp_init_module);
2240 module_exit(nvme_tcp_cleanup_module);
2242 MODULE_LICENSE("GPL v2");