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>
16 #include <net/busy_poll.h>
21 struct nvme_tcp_queue;
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.
29 static int so_priority;
30 module_param(so_priority, int, 0644);
31 MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority");
33 #ifdef CONFIG_DEBUG_LOCK_ALLOC
34 /* lockdep can detect a circular dependency of the form
35 * sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock
36 * because dependencies are tracked for both nvme-tcp and user contexts. Using
37 * a separate class prevents lockdep from conflating nvme-tcp socket use with
38 * user-space socket API use.
40 static struct lock_class_key nvme_tcp_sk_key[2];
41 static struct lock_class_key nvme_tcp_slock_key[2];
43 static void nvme_tcp_reclassify_socket(struct socket *sock)
45 struct sock *sk = sock->sk;
47 if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
50 switch (sk->sk_family) {
52 sock_lock_init_class_and_name(sk, "slock-AF_INET-NVME",
53 &nvme_tcp_slock_key[0],
54 "sk_lock-AF_INET-NVME",
58 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NVME",
59 &nvme_tcp_slock_key[1],
60 "sk_lock-AF_INET6-NVME",
68 static void nvme_tcp_reclassify_socket(struct socket *sock) { }
71 enum nvme_tcp_send_state {
72 NVME_TCP_SEND_CMD_PDU = 0,
73 NVME_TCP_SEND_H2C_PDU,
78 struct nvme_tcp_request {
79 struct nvme_request req;
81 struct nvme_tcp_queue *queue;
87 struct list_head entry;
88 struct llist_node lentry;
97 enum nvme_tcp_send_state state;
100 enum nvme_tcp_queue_flags {
101 NVME_TCP_Q_ALLOCATED = 0,
103 NVME_TCP_Q_POLLING = 2,
106 enum nvme_tcp_recv_state {
107 NVME_TCP_RECV_PDU = 0,
112 struct nvme_tcp_ctrl;
113 struct nvme_tcp_queue {
115 struct work_struct io_work;
118 struct mutex queue_lock;
119 struct mutex send_mutex;
120 struct llist_head req_list;
121 struct list_head send_list;
128 size_t data_remaining;
129 size_t ddgst_remaining;
133 struct nvme_tcp_request *request;
136 size_t cmnd_capsule_len;
137 struct nvme_tcp_ctrl *ctrl;
143 struct ahash_request *rcv_hash;
144 struct ahash_request *snd_hash;
148 struct page_frag_cache pf_cache;
150 void (*state_change)(struct sock *);
151 void (*data_ready)(struct sock *);
152 void (*write_space)(struct sock *);
155 struct nvme_tcp_ctrl {
156 /* read only in the hot path */
157 struct nvme_tcp_queue *queues;
158 struct blk_mq_tag_set tag_set;
160 /* other member variables */
161 struct list_head list;
162 struct blk_mq_tag_set admin_tag_set;
163 struct sockaddr_storage addr;
164 struct sockaddr_storage src_addr;
165 struct nvme_ctrl ctrl;
167 struct work_struct err_work;
168 struct delayed_work connect_work;
169 struct nvme_tcp_request async_req;
170 u32 io_queues[HCTX_MAX_TYPES];
173 static LIST_HEAD(nvme_tcp_ctrl_list);
174 static DEFINE_MUTEX(nvme_tcp_ctrl_mutex);
175 static struct workqueue_struct *nvme_tcp_wq;
176 static const struct blk_mq_ops nvme_tcp_mq_ops;
177 static const struct blk_mq_ops nvme_tcp_admin_mq_ops;
178 static int nvme_tcp_try_send(struct nvme_tcp_queue *queue);
180 static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl)
182 return container_of(ctrl, struct nvme_tcp_ctrl, ctrl);
185 static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
187 return queue - queue->ctrl->queues;
190 static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue)
192 u32 queue_idx = nvme_tcp_queue_id(queue);
195 return queue->ctrl->admin_tag_set.tags[queue_idx];
196 return queue->ctrl->tag_set.tags[queue_idx - 1];
199 static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue)
201 return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
204 static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue)
206 return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
209 static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_queue *queue)
211 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
214 static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req)
216 return req == &req->queue->ctrl->async_req;
219 static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req)
223 if (unlikely(nvme_tcp_async_req(req)))
224 return false; /* async events don't have a request */
226 rq = blk_mq_rq_from_pdu(req);
228 return rq_data_dir(rq) == WRITE && req->data_len &&
229 req->data_len <= nvme_tcp_inline_data_size(req->queue);
232 static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req)
234 return req->iter.bvec->bv_page;
237 static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req)
239 return req->iter.bvec->bv_offset + req->iter.iov_offset;
242 static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req)
244 return min_t(size_t, iov_iter_single_seg_count(&req->iter),
245 req->pdu_len - req->pdu_sent);
248 static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req)
250 return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ?
251 req->pdu_len - req->pdu_sent : 0;
254 static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
257 return nvme_tcp_pdu_data_left(req) <= len;
260 static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
263 struct request *rq = blk_mq_rq_from_pdu(req);
269 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
270 vec = &rq->special_vec;
272 size = blk_rq_payload_bytes(rq);
275 struct bio *bio = req->curr_bio;
279 vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
281 bio_for_each_bvec(bv, bio, bi) {
284 size = bio->bi_iter.bi_size;
285 offset = bio->bi_iter.bi_bvec_done;
288 iov_iter_bvec(&req->iter, dir, vec, nr_bvec, size);
289 req->iter.iov_offset = offset;
292 static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req,
295 req->data_sent += len;
296 req->pdu_sent += len;
297 iov_iter_advance(&req->iter, len);
298 if (!iov_iter_count(&req->iter) &&
299 req->data_sent < req->data_len) {
300 req->curr_bio = req->curr_bio->bi_next;
301 nvme_tcp_init_iter(req, WRITE);
305 static inline void nvme_tcp_send_all(struct nvme_tcp_queue *queue)
309 /* drain the send queue as much as we can... */
311 ret = nvme_tcp_try_send(queue);
315 static inline bool nvme_tcp_queue_more(struct nvme_tcp_queue *queue)
317 return !list_empty(&queue->send_list) ||
318 !llist_empty(&queue->req_list) || queue->more_requests;
321 static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req,
322 bool sync, bool last)
324 struct nvme_tcp_queue *queue = req->queue;
327 empty = llist_add(&req->lentry, &queue->req_list) &&
328 list_empty(&queue->send_list) && !queue->request;
331 * if we're the first on the send_list and we can try to send
332 * directly, otherwise queue io_work. Also, only do that if we
333 * are on the same cpu, so we don't introduce contention.
335 if (queue->io_cpu == raw_smp_processor_id() &&
336 sync && empty && mutex_trylock(&queue->send_mutex)) {
337 queue->more_requests = !last;
338 nvme_tcp_send_all(queue);
339 queue->more_requests = false;
340 mutex_unlock(&queue->send_mutex);
343 if (last && nvme_tcp_queue_more(queue))
344 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
347 static void nvme_tcp_process_req_list(struct nvme_tcp_queue *queue)
349 struct nvme_tcp_request *req;
350 struct llist_node *node;
352 for (node = llist_del_all(&queue->req_list); node; node = node->next) {
353 req = llist_entry(node, struct nvme_tcp_request, lentry);
354 list_add(&req->entry, &queue->send_list);
358 static inline struct nvme_tcp_request *
359 nvme_tcp_fetch_request(struct nvme_tcp_queue *queue)
361 struct nvme_tcp_request *req;
363 req = list_first_entry_or_null(&queue->send_list,
364 struct nvme_tcp_request, entry);
366 nvme_tcp_process_req_list(queue);
367 req = list_first_entry_or_null(&queue->send_list,
368 struct nvme_tcp_request, entry);
373 list_del(&req->entry);
377 static inline void nvme_tcp_ddgst_final(struct ahash_request *hash,
380 ahash_request_set_crypt(hash, NULL, (u8 *)dgst, 0);
381 crypto_ahash_final(hash);
384 static inline void nvme_tcp_ddgst_update(struct ahash_request *hash,
385 struct page *page, off_t off, size_t len)
387 struct scatterlist sg;
389 sg_init_marker(&sg, 1);
390 sg_set_page(&sg, page, len, off);
391 ahash_request_set_crypt(hash, &sg, NULL, len);
392 crypto_ahash_update(hash);
395 static inline void nvme_tcp_hdgst(struct ahash_request *hash,
396 void *pdu, size_t len)
398 struct scatterlist sg;
400 sg_init_one(&sg, pdu, len);
401 ahash_request_set_crypt(hash, &sg, pdu + len, len);
402 crypto_ahash_digest(hash);
405 static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue,
406 void *pdu, size_t pdu_len)
408 struct nvme_tcp_hdr *hdr = pdu;
412 if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
413 dev_err(queue->ctrl->ctrl.device,
414 "queue %d: header digest flag is cleared\n",
415 nvme_tcp_queue_id(queue));
419 recv_digest = *(__le32 *)(pdu + hdr->hlen);
420 nvme_tcp_hdgst(queue->rcv_hash, pdu, pdu_len);
421 exp_digest = *(__le32 *)(pdu + hdr->hlen);
422 if (recv_digest != exp_digest) {
423 dev_err(queue->ctrl->ctrl.device,
424 "header digest error: recv %#x expected %#x\n",
425 le32_to_cpu(recv_digest), le32_to_cpu(exp_digest));
432 static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu)
434 struct nvme_tcp_hdr *hdr = pdu;
435 u8 digest_len = nvme_tcp_hdgst_len(queue);
438 len = le32_to_cpu(hdr->plen) - hdr->hlen -
439 ((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0);
441 if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
442 dev_err(queue->ctrl->ctrl.device,
443 "queue %d: data digest flag is cleared\n",
444 nvme_tcp_queue_id(queue));
447 crypto_ahash_init(queue->rcv_hash);
452 static void nvme_tcp_exit_request(struct blk_mq_tag_set *set,
453 struct request *rq, unsigned int hctx_idx)
455 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
457 page_frag_free(req->pdu);
460 static int nvme_tcp_init_request(struct blk_mq_tag_set *set,
461 struct request *rq, unsigned int hctx_idx,
462 unsigned int numa_node)
464 struct nvme_tcp_ctrl *ctrl = set->driver_data;
465 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
466 struct nvme_tcp_cmd_pdu *pdu;
467 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
468 struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx];
469 u8 hdgst = nvme_tcp_hdgst_len(queue);
471 req->pdu = page_frag_alloc(&queue->pf_cache,
472 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
473 GFP_KERNEL | __GFP_ZERO);
479 nvme_req(rq)->ctrl = &ctrl->ctrl;
480 nvme_req(rq)->cmd = &pdu->cmd;
485 static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
486 unsigned int hctx_idx)
488 struct nvme_tcp_ctrl *ctrl = data;
489 struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1];
491 hctx->driver_data = queue;
495 static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
496 unsigned int hctx_idx)
498 struct nvme_tcp_ctrl *ctrl = data;
499 struct nvme_tcp_queue *queue = &ctrl->queues[0];
501 hctx->driver_data = queue;
505 static enum nvme_tcp_recv_state
506 nvme_tcp_recv_state(struct nvme_tcp_queue *queue)
508 return (queue->pdu_remaining) ? NVME_TCP_RECV_PDU :
509 (queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST :
513 static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
515 queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) +
516 nvme_tcp_hdgst_len(queue);
517 queue->pdu_offset = 0;
518 queue->data_remaining = -1;
519 queue->ddgst_remaining = 0;
522 static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
524 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
527 dev_warn(ctrl->device, "starting error recovery\n");
528 queue_work(nvme_reset_wq, &to_tcp_ctrl(ctrl)->err_work);
531 static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue,
532 struct nvme_completion *cqe)
534 struct nvme_tcp_request *req;
537 rq = nvme_find_rq(nvme_tcp_tagset(queue), cqe->command_id);
539 dev_err(queue->ctrl->ctrl.device,
540 "got bad cqe.command_id %#x on queue %d\n",
541 cqe->command_id, nvme_tcp_queue_id(queue));
542 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
546 req = blk_mq_rq_to_pdu(rq);
547 if (req->status == cpu_to_le16(NVME_SC_SUCCESS))
548 req->status = cqe->status;
550 if (!nvme_try_complete_req(rq, req->status, cqe->result))
551 nvme_complete_rq(rq);
557 static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
558 struct nvme_tcp_data_pdu *pdu)
562 rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
564 dev_err(queue->ctrl->ctrl.device,
565 "got bad c2hdata.command_id %#x on queue %d\n",
566 pdu->command_id, nvme_tcp_queue_id(queue));
570 if (!blk_rq_payload_bytes(rq)) {
571 dev_err(queue->ctrl->ctrl.device,
572 "queue %d tag %#x unexpected data\n",
573 nvme_tcp_queue_id(queue), rq->tag);
577 queue->data_remaining = le32_to_cpu(pdu->data_length);
579 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS &&
580 unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) {
581 dev_err(queue->ctrl->ctrl.device,
582 "queue %d tag %#x SUCCESS set but not last PDU\n",
583 nvme_tcp_queue_id(queue), rq->tag);
584 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
591 static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue,
592 struct nvme_tcp_rsp_pdu *pdu)
594 struct nvme_completion *cqe = &pdu->cqe;
598 * AEN requests are special as they don't time out and can
599 * survive any kind of queue freeze and often don't respond to
600 * aborts. We don't even bother to allocate a struct request
601 * for them but rather special case them here.
603 if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue),
605 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
608 ret = nvme_tcp_process_nvme_cqe(queue, cqe);
613 static int nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req,
614 struct nvme_tcp_r2t_pdu *pdu)
616 struct nvme_tcp_data_pdu *data = req->pdu;
617 struct nvme_tcp_queue *queue = req->queue;
618 struct request *rq = blk_mq_rq_from_pdu(req);
619 u8 hdgst = nvme_tcp_hdgst_len(queue);
620 u8 ddgst = nvme_tcp_ddgst_len(queue);
622 req->pdu_len = le32_to_cpu(pdu->r2t_length);
625 if (unlikely(!req->pdu_len)) {
626 dev_err(queue->ctrl->ctrl.device,
627 "req %d r2t len is %u, probably a bug...\n",
628 rq->tag, req->pdu_len);
632 if (unlikely(req->data_sent + req->pdu_len > req->data_len)) {
633 dev_err(queue->ctrl->ctrl.device,
634 "req %d r2t len %u exceeded data len %u (%zu sent)\n",
635 rq->tag, req->pdu_len, req->data_len,
640 if (unlikely(le32_to_cpu(pdu->r2t_offset) < req->data_sent)) {
641 dev_err(queue->ctrl->ctrl.device,
642 "req %d unexpected r2t offset %u (expected %zu)\n",
643 rq->tag, le32_to_cpu(pdu->r2t_offset),
648 memset(data, 0, sizeof(*data));
649 data->hdr.type = nvme_tcp_h2c_data;
650 data->hdr.flags = NVME_TCP_F_DATA_LAST;
651 if (queue->hdr_digest)
652 data->hdr.flags |= NVME_TCP_F_HDGST;
653 if (queue->data_digest)
654 data->hdr.flags |= NVME_TCP_F_DDGST;
655 data->hdr.hlen = sizeof(*data);
656 data->hdr.pdo = data->hdr.hlen + hdgst;
658 cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst);
659 data->ttag = pdu->ttag;
660 data->command_id = nvme_cid(rq);
661 data->data_offset = pdu->r2t_offset;
662 data->data_length = cpu_to_le32(req->pdu_len);
666 static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
667 struct nvme_tcp_r2t_pdu *pdu)
669 struct nvme_tcp_request *req;
673 rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
675 dev_err(queue->ctrl->ctrl.device,
676 "got bad r2t.command_id %#x on queue %d\n",
677 pdu->command_id, nvme_tcp_queue_id(queue));
680 req = blk_mq_rq_to_pdu(rq);
682 ret = nvme_tcp_setup_h2c_data_pdu(req, pdu);
686 req->state = NVME_TCP_SEND_H2C_PDU;
689 nvme_tcp_queue_request(req, false, true);
694 static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
695 unsigned int *offset, size_t *len)
697 struct nvme_tcp_hdr *hdr;
698 char *pdu = queue->pdu;
699 size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
702 ret = skb_copy_bits(skb, *offset,
703 &pdu[queue->pdu_offset], rcv_len);
707 queue->pdu_remaining -= rcv_len;
708 queue->pdu_offset += rcv_len;
711 if (queue->pdu_remaining)
715 if (queue->hdr_digest) {
716 ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen);
722 if (queue->data_digest) {
723 ret = nvme_tcp_check_ddgst(queue, queue->pdu);
729 case nvme_tcp_c2h_data:
730 return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu);
732 nvme_tcp_init_recv_ctx(queue);
733 return nvme_tcp_handle_comp(queue, (void *)queue->pdu);
735 nvme_tcp_init_recv_ctx(queue);
736 return nvme_tcp_handle_r2t(queue, (void *)queue->pdu);
738 dev_err(queue->ctrl->ctrl.device,
739 "unsupported pdu type (%d)\n", hdr->type);
744 static inline void nvme_tcp_end_request(struct request *rq, u16 status)
746 union nvme_result res = {};
748 if (!nvme_try_complete_req(rq, cpu_to_le16(status << 1), res))
749 nvme_complete_rq(rq);
752 static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb,
753 unsigned int *offset, size_t *len)
755 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
757 nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
758 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
763 recv_len = min_t(size_t, *len, queue->data_remaining);
767 if (!iov_iter_count(&req->iter)) {
768 req->curr_bio = req->curr_bio->bi_next;
771 * If we don`t have any bios it means that controller
772 * sent more data than we requested, hence error
774 if (!req->curr_bio) {
775 dev_err(queue->ctrl->ctrl.device,
776 "queue %d no space in request %#x",
777 nvme_tcp_queue_id(queue), rq->tag);
778 nvme_tcp_init_recv_ctx(queue);
781 nvme_tcp_init_iter(req, READ);
784 /* we can read only from what is left in this bio */
785 recv_len = min_t(size_t, recv_len,
786 iov_iter_count(&req->iter));
788 if (queue->data_digest)
789 ret = skb_copy_and_hash_datagram_iter(skb, *offset,
790 &req->iter, recv_len, queue->rcv_hash);
792 ret = skb_copy_datagram_iter(skb, *offset,
793 &req->iter, recv_len);
795 dev_err(queue->ctrl->ctrl.device,
796 "queue %d failed to copy request %#x data",
797 nvme_tcp_queue_id(queue), rq->tag);
803 queue->data_remaining -= recv_len;
806 if (!queue->data_remaining) {
807 if (queue->data_digest) {
808 nvme_tcp_ddgst_final(queue->rcv_hash, &queue->exp_ddgst);
809 queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH;
811 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
812 nvme_tcp_end_request(rq,
813 le16_to_cpu(req->status));
816 nvme_tcp_init_recv_ctx(queue);
823 static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue,
824 struct sk_buff *skb, unsigned int *offset, size_t *len)
826 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
827 char *ddgst = (char *)&queue->recv_ddgst;
828 size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining);
829 off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining;
832 ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len);
836 queue->ddgst_remaining -= recv_len;
839 if (queue->ddgst_remaining)
842 if (queue->recv_ddgst != queue->exp_ddgst) {
843 struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
845 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
847 req->status = cpu_to_le16(NVME_SC_DATA_XFER_ERROR);
849 dev_err(queue->ctrl->ctrl.device,
850 "data digest error: recv %#x expected %#x\n",
851 le32_to_cpu(queue->recv_ddgst),
852 le32_to_cpu(queue->exp_ddgst));
855 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
856 struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
858 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
860 nvme_tcp_end_request(rq, le16_to_cpu(req->status));
864 nvme_tcp_init_recv_ctx(queue);
868 static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,
869 unsigned int offset, size_t len)
871 struct nvme_tcp_queue *queue = desc->arg.data;
872 size_t consumed = len;
876 switch (nvme_tcp_recv_state(queue)) {
877 case NVME_TCP_RECV_PDU:
878 result = nvme_tcp_recv_pdu(queue, skb, &offset, &len);
880 case NVME_TCP_RECV_DATA:
881 result = nvme_tcp_recv_data(queue, skb, &offset, &len);
883 case NVME_TCP_RECV_DDGST:
884 result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len);
890 dev_err(queue->ctrl->ctrl.device,
891 "receive failed: %d\n", result);
892 queue->rd_enabled = false;
893 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
901 static void nvme_tcp_data_ready(struct sock *sk)
903 struct nvme_tcp_queue *queue;
905 read_lock_bh(&sk->sk_callback_lock);
906 queue = sk->sk_user_data;
907 if (likely(queue && queue->rd_enabled) &&
908 !test_bit(NVME_TCP_Q_POLLING, &queue->flags))
909 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
910 read_unlock_bh(&sk->sk_callback_lock);
913 static void nvme_tcp_write_space(struct sock *sk)
915 struct nvme_tcp_queue *queue;
917 read_lock_bh(&sk->sk_callback_lock);
918 queue = sk->sk_user_data;
919 if (likely(queue && sk_stream_is_writeable(sk))) {
920 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
921 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
923 read_unlock_bh(&sk->sk_callback_lock);
926 static void nvme_tcp_state_change(struct sock *sk)
928 struct nvme_tcp_queue *queue;
930 read_lock_bh(&sk->sk_callback_lock);
931 queue = sk->sk_user_data;
935 switch (sk->sk_state) {
941 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
944 dev_info(queue->ctrl->ctrl.device,
945 "queue %d socket state %d\n",
946 nvme_tcp_queue_id(queue), sk->sk_state);
949 queue->state_change(sk);
951 read_unlock_bh(&sk->sk_callback_lock);
954 static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue)
956 queue->request = NULL;
959 static void nvme_tcp_fail_request(struct nvme_tcp_request *req)
961 if (nvme_tcp_async_req(req)) {
962 union nvme_result res = {};
964 nvme_complete_async_event(&req->queue->ctrl->ctrl,
965 cpu_to_le16(NVME_SC_HOST_PATH_ERROR), &res);
967 nvme_tcp_end_request(blk_mq_rq_from_pdu(req),
968 NVME_SC_HOST_PATH_ERROR);
972 static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
974 struct nvme_tcp_queue *queue = req->queue;
975 int req_data_len = req->data_len;
978 struct page *page = nvme_tcp_req_cur_page(req);
979 size_t offset = nvme_tcp_req_cur_offset(req);
980 size_t len = nvme_tcp_req_cur_length(req);
981 bool last = nvme_tcp_pdu_last_send(req, len);
982 int req_data_sent = req->data_sent;
983 int ret, flags = MSG_DONTWAIT;
985 if (last && !queue->data_digest && !nvme_tcp_queue_more(queue))
988 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
990 if (sendpage_ok(page)) {
991 ret = kernel_sendpage(queue->sock, page, offset, len,
994 ret = sock_no_sendpage(queue->sock, page, offset, len,
1000 if (queue->data_digest)
1001 nvme_tcp_ddgst_update(queue->snd_hash, page,
1005 * update the request iterator except for the last payload send
1006 * in the request where we don't want to modify it as we may
1007 * compete with the RX path completing the request.
1009 if (req_data_sent + ret < req_data_len)
1010 nvme_tcp_advance_req(req, ret);
1012 /* fully successful last send in current PDU */
1013 if (last && ret == len) {
1014 if (queue->data_digest) {
1015 nvme_tcp_ddgst_final(queue->snd_hash,
1017 req->state = NVME_TCP_SEND_DDGST;
1020 nvme_tcp_done_send_req(queue);
1028 static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
1030 struct nvme_tcp_queue *queue = req->queue;
1031 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
1032 bool inline_data = nvme_tcp_has_inline_data(req);
1033 u8 hdgst = nvme_tcp_hdgst_len(queue);
1034 int len = sizeof(*pdu) + hdgst - req->offset;
1035 int flags = MSG_DONTWAIT;
1038 if (inline_data || nvme_tcp_queue_more(queue))
1039 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
1043 if (queue->hdr_digest && !req->offset)
1044 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
1046 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
1047 offset_in_page(pdu) + req->offset, len, flags);
1048 if (unlikely(ret <= 0))
1054 req->state = NVME_TCP_SEND_DATA;
1055 if (queue->data_digest)
1056 crypto_ahash_init(queue->snd_hash);
1058 nvme_tcp_done_send_req(queue);
1067 static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
1069 struct nvme_tcp_queue *queue = req->queue;
1070 struct nvme_tcp_data_pdu *pdu = req->pdu;
1071 u8 hdgst = nvme_tcp_hdgst_len(queue);
1072 int len = sizeof(*pdu) - req->offset + hdgst;
1075 if (queue->hdr_digest && !req->offset)
1076 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
1078 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
1079 offset_in_page(pdu) + req->offset, len,
1080 MSG_DONTWAIT | MSG_MORE | MSG_SENDPAGE_NOTLAST);
1081 if (unlikely(ret <= 0))
1086 req->state = NVME_TCP_SEND_DATA;
1087 if (queue->data_digest)
1088 crypto_ahash_init(queue->snd_hash);
1096 static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
1098 struct nvme_tcp_queue *queue = req->queue;
1099 size_t offset = req->offset;
1101 struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
1103 .iov_base = (u8 *)&req->ddgst + req->offset,
1104 .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset
1107 if (nvme_tcp_queue_more(queue))
1108 msg.msg_flags |= MSG_MORE;
1110 msg.msg_flags |= MSG_EOR;
1112 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1113 if (unlikely(ret <= 0))
1116 if (offset + ret == NVME_TCP_DIGEST_LENGTH) {
1117 nvme_tcp_done_send_req(queue);
1125 static int nvme_tcp_try_send(struct nvme_tcp_queue *queue)
1127 struct nvme_tcp_request *req;
1130 if (!queue->request) {
1131 queue->request = nvme_tcp_fetch_request(queue);
1132 if (!queue->request)
1135 req = queue->request;
1137 if (req->state == NVME_TCP_SEND_CMD_PDU) {
1138 ret = nvme_tcp_try_send_cmd_pdu(req);
1141 if (!nvme_tcp_has_inline_data(req))
1145 if (req->state == NVME_TCP_SEND_H2C_PDU) {
1146 ret = nvme_tcp_try_send_data_pdu(req);
1151 if (req->state == NVME_TCP_SEND_DATA) {
1152 ret = nvme_tcp_try_send_data(req);
1157 if (req->state == NVME_TCP_SEND_DDGST)
1158 ret = nvme_tcp_try_send_ddgst(req);
1160 if (ret == -EAGAIN) {
1162 } else if (ret < 0) {
1163 dev_err(queue->ctrl->ctrl.device,
1164 "failed to send request %d\n", ret);
1165 nvme_tcp_fail_request(queue->request);
1166 nvme_tcp_done_send_req(queue);
1171 static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue)
1173 struct socket *sock = queue->sock;
1174 struct sock *sk = sock->sk;
1175 read_descriptor_t rd_desc;
1178 rd_desc.arg.data = queue;
1182 consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb);
1187 static void nvme_tcp_io_work(struct work_struct *w)
1189 struct nvme_tcp_queue *queue =
1190 container_of(w, struct nvme_tcp_queue, io_work);
1191 unsigned long deadline = jiffies + msecs_to_jiffies(1);
1194 bool pending = false;
1197 if (mutex_trylock(&queue->send_mutex)) {
1198 result = nvme_tcp_try_send(queue);
1199 mutex_unlock(&queue->send_mutex);
1202 else if (unlikely(result < 0))
1206 result = nvme_tcp_try_recv(queue);
1209 else if (unlikely(result < 0))
1212 if (!pending || !queue->rd_enabled)
1215 } while (!time_after(jiffies, deadline)); /* quota is exhausted */
1217 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
1220 static void nvme_tcp_free_crypto(struct nvme_tcp_queue *queue)
1222 struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
1224 ahash_request_free(queue->rcv_hash);
1225 ahash_request_free(queue->snd_hash);
1226 crypto_free_ahash(tfm);
1229 static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue *queue)
1231 struct crypto_ahash *tfm;
1233 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
1235 return PTR_ERR(tfm);
1237 queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1238 if (!queue->snd_hash)
1240 ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
1242 queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1243 if (!queue->rcv_hash)
1245 ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
1249 ahash_request_free(queue->snd_hash);
1251 crypto_free_ahash(tfm);
1255 static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl)
1257 struct nvme_tcp_request *async = &ctrl->async_req;
1259 page_frag_free(async->pdu);
1262 static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
1264 struct nvme_tcp_queue *queue = &ctrl->queues[0];
1265 struct nvme_tcp_request *async = &ctrl->async_req;
1266 u8 hdgst = nvme_tcp_hdgst_len(queue);
1268 async->pdu = page_frag_alloc(&queue->pf_cache,
1269 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
1270 GFP_KERNEL | __GFP_ZERO);
1274 async->queue = &ctrl->queues[0];
1278 static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
1280 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1281 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1283 if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
1286 if (queue->hdr_digest || queue->data_digest)
1287 nvme_tcp_free_crypto(queue);
1289 sock_release(queue->sock);
1291 mutex_destroy(&queue->send_mutex);
1292 mutex_destroy(&queue->queue_lock);
1295 static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
1297 struct nvme_tcp_icreq_pdu *icreq;
1298 struct nvme_tcp_icresp_pdu *icresp;
1299 struct msghdr msg = {};
1301 bool ctrl_hdgst, ctrl_ddgst;
1304 icreq = kzalloc(sizeof(*icreq), GFP_KERNEL);
1308 icresp = kzalloc(sizeof(*icresp), GFP_KERNEL);
1314 icreq->hdr.type = nvme_tcp_icreq;
1315 icreq->hdr.hlen = sizeof(*icreq);
1317 icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen);
1318 icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
1319 icreq->maxr2t = 0; /* single inflight r2t supported */
1320 icreq->hpda = 0; /* no alignment constraint */
1321 if (queue->hdr_digest)
1322 icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
1323 if (queue->data_digest)
1324 icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
1326 iov.iov_base = icreq;
1327 iov.iov_len = sizeof(*icreq);
1328 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1332 memset(&msg, 0, sizeof(msg));
1333 iov.iov_base = icresp;
1334 iov.iov_len = sizeof(*icresp);
1335 ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1336 iov.iov_len, msg.msg_flags);
1341 if (icresp->hdr.type != nvme_tcp_icresp) {
1342 pr_err("queue %d: bad type returned %d\n",
1343 nvme_tcp_queue_id(queue), icresp->hdr.type);
1347 if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) {
1348 pr_err("queue %d: bad pdu length returned %d\n",
1349 nvme_tcp_queue_id(queue), icresp->hdr.plen);
1353 if (icresp->pfv != NVME_TCP_PFV_1_0) {
1354 pr_err("queue %d: bad pfv returned %d\n",
1355 nvme_tcp_queue_id(queue), icresp->pfv);
1359 ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE);
1360 if ((queue->data_digest && !ctrl_ddgst) ||
1361 (!queue->data_digest && ctrl_ddgst)) {
1362 pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
1363 nvme_tcp_queue_id(queue),
1364 queue->data_digest ? "enabled" : "disabled",
1365 ctrl_ddgst ? "enabled" : "disabled");
1369 ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE);
1370 if ((queue->hdr_digest && !ctrl_hdgst) ||
1371 (!queue->hdr_digest && ctrl_hdgst)) {
1372 pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
1373 nvme_tcp_queue_id(queue),
1374 queue->hdr_digest ? "enabled" : "disabled",
1375 ctrl_hdgst ? "enabled" : "disabled");
1379 if (icresp->cpda != 0) {
1380 pr_err("queue %d: unsupported cpda returned %d\n",
1381 nvme_tcp_queue_id(queue), icresp->cpda);
1393 static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue)
1395 return nvme_tcp_queue_id(queue) == 0;
1398 static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue)
1400 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1401 int qid = nvme_tcp_queue_id(queue);
1403 return !nvme_tcp_admin_queue(queue) &&
1404 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT];
1407 static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue)
1409 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1410 int qid = nvme_tcp_queue_id(queue);
1412 return !nvme_tcp_admin_queue(queue) &&
1413 !nvme_tcp_default_queue(queue) &&
1414 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1415 ctrl->io_queues[HCTX_TYPE_READ];
1418 static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue)
1420 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1421 int qid = nvme_tcp_queue_id(queue);
1423 return !nvme_tcp_admin_queue(queue) &&
1424 !nvme_tcp_default_queue(queue) &&
1425 !nvme_tcp_read_queue(queue) &&
1426 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1427 ctrl->io_queues[HCTX_TYPE_READ] +
1428 ctrl->io_queues[HCTX_TYPE_POLL];
1431 static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue)
1433 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1434 int qid = nvme_tcp_queue_id(queue);
1437 if (nvme_tcp_default_queue(queue))
1439 else if (nvme_tcp_read_queue(queue))
1440 n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] - 1;
1441 else if (nvme_tcp_poll_queue(queue))
1442 n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] -
1443 ctrl->io_queues[HCTX_TYPE_READ] - 1;
1444 queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
1447 static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl,
1448 int qid, size_t queue_size)
1450 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1451 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1452 int ret, rcv_pdu_size;
1454 mutex_init(&queue->queue_lock);
1456 init_llist_head(&queue->req_list);
1457 INIT_LIST_HEAD(&queue->send_list);
1458 mutex_init(&queue->send_mutex);
1459 INIT_WORK(&queue->io_work, nvme_tcp_io_work);
1460 queue->queue_size = queue_size;
1463 queue->cmnd_capsule_len = nctrl->ioccsz * 16;
1465 queue->cmnd_capsule_len = sizeof(struct nvme_command) +
1466 NVME_TCP_ADMIN_CCSZ;
1468 ret = sock_create(ctrl->addr.ss_family, SOCK_STREAM,
1469 IPPROTO_TCP, &queue->sock);
1471 dev_err(nctrl->device,
1472 "failed to create socket: %d\n", ret);
1473 goto err_destroy_mutex;
1476 nvme_tcp_reclassify_socket(queue->sock);
1478 /* Single syn retry */
1479 tcp_sock_set_syncnt(queue->sock->sk, 1);
1481 /* Set TCP no delay */
1482 tcp_sock_set_nodelay(queue->sock->sk);
1485 * Cleanup whatever is sitting in the TCP transmit queue on socket
1486 * close. This is done to prevent stale data from being sent should
1487 * the network connection be restored before TCP times out.
1489 sock_no_linger(queue->sock->sk);
1491 if (so_priority > 0)
1492 sock_set_priority(queue->sock->sk, so_priority);
1494 /* Set socket type of service */
1495 if (nctrl->opts->tos >= 0)
1496 ip_sock_set_tos(queue->sock->sk, nctrl->opts->tos);
1498 /* Set 10 seconds timeout for icresp recvmsg */
1499 queue->sock->sk->sk_rcvtimeo = 10 * HZ;
1501 queue->sock->sk->sk_allocation = GFP_ATOMIC;
1502 nvme_tcp_set_queue_io_cpu(queue);
1503 queue->request = NULL;
1504 queue->data_remaining = 0;
1505 queue->ddgst_remaining = 0;
1506 queue->pdu_remaining = 0;
1507 queue->pdu_offset = 0;
1508 sk_set_memalloc(queue->sock->sk);
1510 if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) {
1511 ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr,
1512 sizeof(ctrl->src_addr));
1514 dev_err(nctrl->device,
1515 "failed to bind queue %d socket %d\n",
1521 if (nctrl->opts->mask & NVMF_OPT_HOST_IFACE) {
1522 char *iface = nctrl->opts->host_iface;
1523 sockptr_t optval = KERNEL_SOCKPTR(iface);
1525 ret = sock_setsockopt(queue->sock, SOL_SOCKET, SO_BINDTODEVICE,
1526 optval, strlen(iface));
1528 dev_err(nctrl->device,
1529 "failed to bind to interface %s queue %d err %d\n",
1535 queue->hdr_digest = nctrl->opts->hdr_digest;
1536 queue->data_digest = nctrl->opts->data_digest;
1537 if (queue->hdr_digest || queue->data_digest) {
1538 ret = nvme_tcp_alloc_crypto(queue);
1540 dev_err(nctrl->device,
1541 "failed to allocate queue %d crypto\n", qid);
1546 rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) +
1547 nvme_tcp_hdgst_len(queue);
1548 queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL);
1554 dev_dbg(nctrl->device, "connecting queue %d\n",
1555 nvme_tcp_queue_id(queue));
1557 ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr,
1558 sizeof(ctrl->addr), 0);
1560 dev_err(nctrl->device,
1561 "failed to connect socket: %d\n", ret);
1565 ret = nvme_tcp_init_connection(queue);
1567 goto err_init_connect;
1569 queue->rd_enabled = true;
1570 set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags);
1571 nvme_tcp_init_recv_ctx(queue);
1573 write_lock_bh(&queue->sock->sk->sk_callback_lock);
1574 queue->sock->sk->sk_user_data = queue;
1575 queue->state_change = queue->sock->sk->sk_state_change;
1576 queue->data_ready = queue->sock->sk->sk_data_ready;
1577 queue->write_space = queue->sock->sk->sk_write_space;
1578 queue->sock->sk->sk_data_ready = nvme_tcp_data_ready;
1579 queue->sock->sk->sk_state_change = nvme_tcp_state_change;
1580 queue->sock->sk->sk_write_space = nvme_tcp_write_space;
1581 #ifdef CONFIG_NET_RX_BUSY_POLL
1582 queue->sock->sk->sk_ll_usec = 1;
1584 write_unlock_bh(&queue->sock->sk->sk_callback_lock);
1589 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1593 if (queue->hdr_digest || queue->data_digest)
1594 nvme_tcp_free_crypto(queue);
1596 sock_release(queue->sock);
1599 mutex_destroy(&queue->send_mutex);
1600 mutex_destroy(&queue->queue_lock);
1604 static void nvme_tcp_restore_sock_calls(struct nvme_tcp_queue *queue)
1606 struct socket *sock = queue->sock;
1608 write_lock_bh(&sock->sk->sk_callback_lock);
1609 sock->sk->sk_user_data = NULL;
1610 sock->sk->sk_data_ready = queue->data_ready;
1611 sock->sk->sk_state_change = queue->state_change;
1612 sock->sk->sk_write_space = queue->write_space;
1613 write_unlock_bh(&sock->sk->sk_callback_lock);
1616 static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
1618 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1619 nvme_tcp_restore_sock_calls(queue);
1620 cancel_work_sync(&queue->io_work);
1623 static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
1625 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1626 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1628 mutex_lock(&queue->queue_lock);
1629 if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
1630 __nvme_tcp_stop_queue(queue);
1631 mutex_unlock(&queue->queue_lock);
1634 static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
1636 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1640 ret = nvmf_connect_io_queue(nctrl, idx);
1642 ret = nvmf_connect_admin_queue(nctrl);
1645 set_bit(NVME_TCP_Q_LIVE, &ctrl->queues[idx].flags);
1647 if (test_bit(NVME_TCP_Q_ALLOCATED, &ctrl->queues[idx].flags))
1648 __nvme_tcp_stop_queue(&ctrl->queues[idx]);
1649 dev_err(nctrl->device,
1650 "failed to connect queue: %d ret=%d\n", idx, ret);
1655 static struct blk_mq_tag_set *nvme_tcp_alloc_tagset(struct nvme_ctrl *nctrl,
1658 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1659 struct blk_mq_tag_set *set;
1663 set = &ctrl->admin_tag_set;
1664 memset(set, 0, sizeof(*set));
1665 set->ops = &nvme_tcp_admin_mq_ops;
1666 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
1667 set->reserved_tags = NVMF_RESERVED_TAGS;
1668 set->numa_node = nctrl->numa_node;
1669 set->flags = BLK_MQ_F_BLOCKING;
1670 set->cmd_size = sizeof(struct nvme_tcp_request);
1671 set->driver_data = ctrl;
1672 set->nr_hw_queues = 1;
1673 set->timeout = NVME_ADMIN_TIMEOUT;
1675 set = &ctrl->tag_set;
1676 memset(set, 0, sizeof(*set));
1677 set->ops = &nvme_tcp_mq_ops;
1678 set->queue_depth = nctrl->sqsize + 1;
1679 set->reserved_tags = NVMF_RESERVED_TAGS;
1680 set->numa_node = nctrl->numa_node;
1681 set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
1682 set->cmd_size = sizeof(struct nvme_tcp_request);
1683 set->driver_data = ctrl;
1684 set->nr_hw_queues = nctrl->queue_count - 1;
1685 set->timeout = NVME_IO_TIMEOUT;
1686 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
1689 ret = blk_mq_alloc_tag_set(set);
1691 return ERR_PTR(ret);
1696 static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl)
1698 if (to_tcp_ctrl(ctrl)->async_req.pdu) {
1699 cancel_work_sync(&ctrl->async_event_work);
1700 nvme_tcp_free_async_req(to_tcp_ctrl(ctrl));
1701 to_tcp_ctrl(ctrl)->async_req.pdu = NULL;
1704 nvme_tcp_free_queue(ctrl, 0);
1707 static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl)
1711 for (i = 1; i < ctrl->queue_count; i++)
1712 nvme_tcp_free_queue(ctrl, i);
1715 static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl)
1719 for (i = 1; i < ctrl->queue_count; i++)
1720 nvme_tcp_stop_queue(ctrl, i);
1723 static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl)
1727 for (i = 1; i < ctrl->queue_count; i++) {
1728 ret = nvme_tcp_start_queue(ctrl, i);
1730 goto out_stop_queues;
1736 for (i--; i >= 1; i--)
1737 nvme_tcp_stop_queue(ctrl, i);
1741 static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
1745 ret = nvme_tcp_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
1749 ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
1751 goto out_free_queue;
1756 nvme_tcp_free_queue(ctrl, 0);
1760 static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
1764 for (i = 1; i < ctrl->queue_count; i++) {
1765 ret = nvme_tcp_alloc_queue(ctrl, i,
1768 goto out_free_queues;
1774 for (i--; i >= 1; i--)
1775 nvme_tcp_free_queue(ctrl, i);
1780 static unsigned int nvme_tcp_nr_io_queues(struct nvme_ctrl *ctrl)
1782 unsigned int nr_io_queues;
1784 nr_io_queues = min(ctrl->opts->nr_io_queues, num_online_cpus());
1785 nr_io_queues += min(ctrl->opts->nr_write_queues, num_online_cpus());
1786 nr_io_queues += min(ctrl->opts->nr_poll_queues, num_online_cpus());
1788 return nr_io_queues;
1791 static void nvme_tcp_set_io_queues(struct nvme_ctrl *nctrl,
1792 unsigned int nr_io_queues)
1794 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1795 struct nvmf_ctrl_options *opts = nctrl->opts;
1797 if (opts->nr_write_queues && opts->nr_io_queues < nr_io_queues) {
1799 * separate read/write queues
1800 * hand out dedicated default queues only after we have
1801 * sufficient read queues.
1803 ctrl->io_queues[HCTX_TYPE_READ] = opts->nr_io_queues;
1804 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
1805 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1806 min(opts->nr_write_queues, nr_io_queues);
1807 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1810 * shared read/write queues
1811 * either no write queues were requested, or we don't have
1812 * sufficient queue count to have dedicated default queues.
1814 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1815 min(opts->nr_io_queues, nr_io_queues);
1816 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1819 if (opts->nr_poll_queues && nr_io_queues) {
1820 /* map dedicated poll queues only if we have queues left */
1821 ctrl->io_queues[HCTX_TYPE_POLL] =
1822 min(opts->nr_poll_queues, nr_io_queues);
1826 static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
1828 unsigned int nr_io_queues;
1831 nr_io_queues = nvme_tcp_nr_io_queues(ctrl);
1832 ret = nvme_set_queue_count(ctrl, &nr_io_queues);
1836 if (nr_io_queues == 0) {
1837 dev_err(ctrl->device,
1838 "unable to set any I/O queues\n");
1842 ctrl->queue_count = nr_io_queues + 1;
1843 dev_info(ctrl->device,
1844 "creating %d I/O queues.\n", nr_io_queues);
1846 nvme_tcp_set_io_queues(ctrl, nr_io_queues);
1848 return __nvme_tcp_alloc_io_queues(ctrl);
1851 static void nvme_tcp_destroy_io_queues(struct nvme_ctrl *ctrl, bool remove)
1853 nvme_tcp_stop_io_queues(ctrl);
1855 blk_cleanup_queue(ctrl->connect_q);
1856 blk_mq_free_tag_set(ctrl->tagset);
1858 nvme_tcp_free_io_queues(ctrl);
1861 static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
1865 ret = nvme_tcp_alloc_io_queues(ctrl);
1870 ctrl->tagset = nvme_tcp_alloc_tagset(ctrl, false);
1871 if (IS_ERR(ctrl->tagset)) {
1872 ret = PTR_ERR(ctrl->tagset);
1873 goto out_free_io_queues;
1876 ctrl->connect_q = blk_mq_init_queue(ctrl->tagset);
1877 if (IS_ERR(ctrl->connect_q)) {
1878 ret = PTR_ERR(ctrl->connect_q);
1879 goto out_free_tag_set;
1883 ret = nvme_tcp_start_io_queues(ctrl);
1885 goto out_cleanup_connect_q;
1888 nvme_start_queues(ctrl);
1889 if (!nvme_wait_freeze_timeout(ctrl, NVME_IO_TIMEOUT)) {
1891 * If we timed out waiting for freeze we are likely to
1892 * be stuck. Fail the controller initialization just
1896 goto out_wait_freeze_timed_out;
1898 blk_mq_update_nr_hw_queues(ctrl->tagset,
1899 ctrl->queue_count - 1);
1900 nvme_unfreeze(ctrl);
1905 out_wait_freeze_timed_out:
1906 nvme_stop_queues(ctrl);
1907 nvme_sync_io_queues(ctrl);
1908 nvme_tcp_stop_io_queues(ctrl);
1909 out_cleanup_connect_q:
1910 nvme_cancel_tagset(ctrl);
1912 blk_cleanup_queue(ctrl->connect_q);
1915 blk_mq_free_tag_set(ctrl->tagset);
1917 nvme_tcp_free_io_queues(ctrl);
1921 static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl *ctrl, bool remove)
1923 nvme_tcp_stop_queue(ctrl, 0);
1925 blk_cleanup_queue(ctrl->admin_q);
1926 blk_cleanup_queue(ctrl->fabrics_q);
1927 blk_mq_free_tag_set(ctrl->admin_tagset);
1929 nvme_tcp_free_admin_queue(ctrl);
1932 static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
1936 error = nvme_tcp_alloc_admin_queue(ctrl);
1941 ctrl->admin_tagset = nvme_tcp_alloc_tagset(ctrl, true);
1942 if (IS_ERR(ctrl->admin_tagset)) {
1943 error = PTR_ERR(ctrl->admin_tagset);
1944 goto out_free_queue;
1947 ctrl->fabrics_q = blk_mq_init_queue(ctrl->admin_tagset);
1948 if (IS_ERR(ctrl->fabrics_q)) {
1949 error = PTR_ERR(ctrl->fabrics_q);
1950 goto out_free_tagset;
1953 ctrl->admin_q = blk_mq_init_queue(ctrl->admin_tagset);
1954 if (IS_ERR(ctrl->admin_q)) {
1955 error = PTR_ERR(ctrl->admin_q);
1956 goto out_cleanup_fabrics_q;
1960 error = nvme_tcp_start_queue(ctrl, 0);
1962 goto out_cleanup_queue;
1964 error = nvme_enable_ctrl(ctrl);
1966 goto out_stop_queue;
1968 blk_mq_unquiesce_queue(ctrl->admin_q);
1970 error = nvme_init_ctrl_finish(ctrl);
1972 goto out_quiesce_queue;
1977 blk_mq_quiesce_queue(ctrl->admin_q);
1978 blk_sync_queue(ctrl->admin_q);
1980 nvme_tcp_stop_queue(ctrl, 0);
1981 nvme_cancel_admin_tagset(ctrl);
1984 blk_cleanup_queue(ctrl->admin_q);
1985 out_cleanup_fabrics_q:
1987 blk_cleanup_queue(ctrl->fabrics_q);
1990 blk_mq_free_tag_set(ctrl->admin_tagset);
1992 nvme_tcp_free_admin_queue(ctrl);
1996 static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
1999 blk_mq_quiesce_queue(ctrl->admin_q);
2000 blk_sync_queue(ctrl->admin_q);
2001 nvme_tcp_stop_queue(ctrl, 0);
2002 nvme_cancel_admin_tagset(ctrl);
2004 blk_mq_unquiesce_queue(ctrl->admin_q);
2005 nvme_tcp_destroy_admin_queue(ctrl, remove);
2008 static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
2011 if (ctrl->queue_count <= 1)
2013 blk_mq_quiesce_queue(ctrl->admin_q);
2014 nvme_start_freeze(ctrl);
2015 nvme_stop_queues(ctrl);
2016 nvme_sync_io_queues(ctrl);
2017 nvme_tcp_stop_io_queues(ctrl);
2018 nvme_cancel_tagset(ctrl);
2020 nvme_start_queues(ctrl);
2021 nvme_tcp_destroy_io_queues(ctrl, remove);
2024 static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl)
2026 /* If we are resetting/deleting then do nothing */
2027 if (ctrl->state != NVME_CTRL_CONNECTING) {
2028 WARN_ON_ONCE(ctrl->state == NVME_CTRL_NEW ||
2029 ctrl->state == NVME_CTRL_LIVE);
2033 if (nvmf_should_reconnect(ctrl)) {
2034 dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
2035 ctrl->opts->reconnect_delay);
2036 queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
2037 ctrl->opts->reconnect_delay * HZ);
2039 dev_info(ctrl->device, "Removing controller...\n");
2040 nvme_delete_ctrl(ctrl);
2044 static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
2046 struct nvmf_ctrl_options *opts = ctrl->opts;
2049 ret = nvme_tcp_configure_admin_queue(ctrl, new);
2055 dev_err(ctrl->device, "icdoff is not supported!\n");
2059 if (!nvme_ctrl_sgl_supported(ctrl)) {
2061 dev_err(ctrl->device, "Mandatory sgls are not supported!\n");
2065 if (opts->queue_size > ctrl->sqsize + 1)
2066 dev_warn(ctrl->device,
2067 "queue_size %zu > ctrl sqsize %u, clamping down\n",
2068 opts->queue_size, ctrl->sqsize + 1);
2070 if (ctrl->sqsize + 1 > ctrl->maxcmd) {
2071 dev_warn(ctrl->device,
2072 "sqsize %u > ctrl maxcmd %u, clamping down\n",
2073 ctrl->sqsize + 1, ctrl->maxcmd);
2074 ctrl->sqsize = ctrl->maxcmd - 1;
2077 if (ctrl->queue_count > 1) {
2078 ret = nvme_tcp_configure_io_queues(ctrl, new);
2083 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
2085 * state change failure is ok if we started ctrl delete,
2086 * unless we're during creation of a new controller to
2087 * avoid races with teardown flow.
2089 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2090 ctrl->state != NVME_CTRL_DELETING_NOIO);
2096 nvme_start_ctrl(ctrl);
2100 if (ctrl->queue_count > 1) {
2101 nvme_stop_queues(ctrl);
2102 nvme_sync_io_queues(ctrl);
2103 nvme_tcp_stop_io_queues(ctrl);
2104 nvme_cancel_tagset(ctrl);
2105 nvme_tcp_destroy_io_queues(ctrl, new);
2108 blk_mq_quiesce_queue(ctrl->admin_q);
2109 blk_sync_queue(ctrl->admin_q);
2110 nvme_tcp_stop_queue(ctrl, 0);
2111 nvme_cancel_admin_tagset(ctrl);
2112 nvme_tcp_destroy_admin_queue(ctrl, new);
2116 static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
2118 struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
2119 struct nvme_tcp_ctrl, connect_work);
2120 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2122 ++ctrl->nr_reconnects;
2124 if (nvme_tcp_setup_ctrl(ctrl, false))
2127 dev_info(ctrl->device, "Successfully reconnected (%d attempt)\n",
2128 ctrl->nr_reconnects);
2130 ctrl->nr_reconnects = 0;
2135 dev_info(ctrl->device, "Failed reconnect attempt %d\n",
2136 ctrl->nr_reconnects);
2137 nvme_tcp_reconnect_or_remove(ctrl);
2140 static void nvme_tcp_error_recovery_work(struct work_struct *work)
2142 struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
2143 struct nvme_tcp_ctrl, err_work);
2144 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2146 nvme_stop_keep_alive(ctrl);
2147 flush_work(&ctrl->async_event_work);
2148 nvme_tcp_teardown_io_queues(ctrl, false);
2149 /* unquiesce to fail fast pending requests */
2150 nvme_start_queues(ctrl);
2151 nvme_tcp_teardown_admin_queue(ctrl, false);
2152 blk_mq_unquiesce_queue(ctrl->admin_q);
2154 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2155 /* state change failure is ok if we started ctrl delete */
2156 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2157 ctrl->state != NVME_CTRL_DELETING_NOIO);
2161 nvme_tcp_reconnect_or_remove(ctrl);
2164 static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
2166 nvme_tcp_teardown_io_queues(ctrl, shutdown);
2167 blk_mq_quiesce_queue(ctrl->admin_q);
2169 nvme_shutdown_ctrl(ctrl);
2171 nvme_disable_ctrl(ctrl);
2172 nvme_tcp_teardown_admin_queue(ctrl, shutdown);
2175 static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
2177 nvme_tcp_teardown_ctrl(ctrl, true);
2180 static void nvme_reset_ctrl_work(struct work_struct *work)
2182 struct nvme_ctrl *ctrl =
2183 container_of(work, struct nvme_ctrl, reset_work);
2185 nvme_stop_ctrl(ctrl);
2186 nvme_tcp_teardown_ctrl(ctrl, false);
2188 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2189 /* state change failure is ok if we started ctrl delete */
2190 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2191 ctrl->state != NVME_CTRL_DELETING_NOIO);
2195 if (nvme_tcp_setup_ctrl(ctrl, false))
2201 ++ctrl->nr_reconnects;
2202 nvme_tcp_reconnect_or_remove(ctrl);
2205 static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl)
2207 cancel_work_sync(&to_tcp_ctrl(ctrl)->err_work);
2208 cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
2211 static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
2213 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
2215 if (list_empty(&ctrl->list))
2218 mutex_lock(&nvme_tcp_ctrl_mutex);
2219 list_del(&ctrl->list);
2220 mutex_unlock(&nvme_tcp_ctrl_mutex);
2222 nvmf_free_options(nctrl->opts);
2224 kfree(ctrl->queues);
2228 static void nvme_tcp_set_sg_null(struct nvme_command *c)
2230 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2234 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2235 NVME_SGL_FMT_TRANSPORT_A;
2238 static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
2239 struct nvme_command *c, u32 data_len)
2241 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2243 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
2244 sg->length = cpu_to_le32(data_len);
2245 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
2248 static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
2251 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2254 sg->length = cpu_to_le32(data_len);
2255 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2256 NVME_SGL_FMT_TRANSPORT_A;
2259 static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
2261 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
2262 struct nvme_tcp_queue *queue = &ctrl->queues[0];
2263 struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
2264 struct nvme_command *cmd = &pdu->cmd;
2265 u8 hdgst = nvme_tcp_hdgst_len(queue);
2267 memset(pdu, 0, sizeof(*pdu));
2268 pdu->hdr.type = nvme_tcp_cmd;
2269 if (queue->hdr_digest)
2270 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2271 pdu->hdr.hlen = sizeof(*pdu);
2272 pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
2274 cmd->common.opcode = nvme_admin_async_event;
2275 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
2276 cmd->common.flags |= NVME_CMD_SGL_METABUF;
2277 nvme_tcp_set_sg_null(cmd);
2279 ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
2280 ctrl->async_req.offset = 0;
2281 ctrl->async_req.curr_bio = NULL;
2282 ctrl->async_req.data_len = 0;
2284 nvme_tcp_queue_request(&ctrl->async_req, true, true);
2287 static void nvme_tcp_complete_timed_out(struct request *rq)
2289 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2290 struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
2292 nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
2293 if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
2294 nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD;
2295 blk_mq_complete_request(rq);
2299 static enum blk_eh_timer_return
2300 nvme_tcp_timeout(struct request *rq, bool reserved)
2302 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2303 struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
2304 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2306 dev_warn(ctrl->device,
2307 "queue %d: timeout request %#x type %d\n",
2308 nvme_tcp_queue_id(req->queue), rq->tag, pdu->hdr.type);
2310 if (ctrl->state != NVME_CTRL_LIVE) {
2312 * If we are resetting, connecting or deleting we should
2313 * complete immediately because we may block controller
2314 * teardown or setup sequence
2315 * - ctrl disable/shutdown fabrics requests
2316 * - connect requests
2317 * - initialization admin requests
2318 * - I/O requests that entered after unquiescing and
2319 * the controller stopped responding
2321 * All other requests should be cancelled by the error
2322 * recovery work, so it's fine that we fail it here.
2324 nvme_tcp_complete_timed_out(rq);
2329 * LIVE state should trigger the normal error recovery which will
2330 * handle completing this request.
2332 nvme_tcp_error_recovery(ctrl);
2333 return BLK_EH_RESET_TIMER;
2336 static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
2339 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2340 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2341 struct nvme_command *c = &pdu->cmd;
2343 c->common.flags |= NVME_CMD_SGL_METABUF;
2345 if (!blk_rq_nr_phys_segments(rq))
2346 nvme_tcp_set_sg_null(c);
2347 else if (rq_data_dir(rq) == WRITE &&
2348 req->data_len <= nvme_tcp_inline_data_size(queue))
2349 nvme_tcp_set_sg_inline(queue, c, req->data_len);
2351 nvme_tcp_set_sg_host_data(c, req->data_len);
2356 static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
2359 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2360 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2361 struct nvme_tcp_queue *queue = req->queue;
2362 u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
2365 ret = nvme_setup_cmd(ns, rq);
2369 req->state = NVME_TCP_SEND_CMD_PDU;
2370 req->status = cpu_to_le16(NVME_SC_SUCCESS);
2375 req->data_len = blk_rq_nr_phys_segments(rq) ?
2376 blk_rq_payload_bytes(rq) : 0;
2377 req->curr_bio = rq->bio;
2378 if (req->curr_bio && req->data_len)
2379 nvme_tcp_init_iter(req, rq_data_dir(rq));
2381 if (rq_data_dir(rq) == WRITE &&
2382 req->data_len <= nvme_tcp_inline_data_size(queue))
2383 req->pdu_len = req->data_len;
2385 pdu->hdr.type = nvme_tcp_cmd;
2387 if (queue->hdr_digest)
2388 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2389 if (queue->data_digest && req->pdu_len) {
2390 pdu->hdr.flags |= NVME_TCP_F_DDGST;
2391 ddgst = nvme_tcp_ddgst_len(queue);
2393 pdu->hdr.hlen = sizeof(*pdu);
2394 pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
2396 cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
2398 ret = nvme_tcp_map_data(queue, rq);
2399 if (unlikely(ret)) {
2400 nvme_cleanup_cmd(rq);
2401 dev_err(queue->ctrl->ctrl.device,
2402 "Failed to map data (%d)\n", ret);
2409 static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx)
2411 struct nvme_tcp_queue *queue = hctx->driver_data;
2413 if (!llist_empty(&queue->req_list))
2414 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
2417 static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
2418 const struct blk_mq_queue_data *bd)
2420 struct nvme_ns *ns = hctx->queue->queuedata;
2421 struct nvme_tcp_queue *queue = hctx->driver_data;
2422 struct request *rq = bd->rq;
2423 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2424 bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
2427 if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2428 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
2430 ret = nvme_tcp_setup_cmd_pdu(ns, rq);
2434 blk_mq_start_request(rq);
2436 nvme_tcp_queue_request(req, true, bd->last);
2441 static int nvme_tcp_map_queues(struct blk_mq_tag_set *set)
2443 struct nvme_tcp_ctrl *ctrl = set->driver_data;
2444 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2446 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
2447 /* separate read/write queues */
2448 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2449 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2450 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2451 set->map[HCTX_TYPE_READ].nr_queues =
2452 ctrl->io_queues[HCTX_TYPE_READ];
2453 set->map[HCTX_TYPE_READ].queue_offset =
2454 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2456 /* shared read/write queues */
2457 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2458 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2459 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2460 set->map[HCTX_TYPE_READ].nr_queues =
2461 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2462 set->map[HCTX_TYPE_READ].queue_offset = 0;
2464 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
2465 blk_mq_map_queues(&set->map[HCTX_TYPE_READ]);
2467 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
2468 /* map dedicated poll queues only if we have queues left */
2469 set->map[HCTX_TYPE_POLL].nr_queues =
2470 ctrl->io_queues[HCTX_TYPE_POLL];
2471 set->map[HCTX_TYPE_POLL].queue_offset =
2472 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
2473 ctrl->io_queues[HCTX_TYPE_READ];
2474 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
2477 dev_info(ctrl->ctrl.device,
2478 "mapped %d/%d/%d default/read/poll queues.\n",
2479 ctrl->io_queues[HCTX_TYPE_DEFAULT],
2480 ctrl->io_queues[HCTX_TYPE_READ],
2481 ctrl->io_queues[HCTX_TYPE_POLL]);
2486 static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx)
2488 struct nvme_tcp_queue *queue = hctx->driver_data;
2489 struct sock *sk = queue->sock->sk;
2491 if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2494 set_bit(NVME_TCP_Q_POLLING, &queue->flags);
2495 if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue))
2496 sk_busy_loop(sk, true);
2497 nvme_tcp_try_recv(queue);
2498 clear_bit(NVME_TCP_Q_POLLING, &queue->flags);
2499 return queue->nr_cqe;
2502 static const struct blk_mq_ops nvme_tcp_mq_ops = {
2503 .queue_rq = nvme_tcp_queue_rq,
2504 .commit_rqs = nvme_tcp_commit_rqs,
2505 .complete = nvme_complete_rq,
2506 .init_request = nvme_tcp_init_request,
2507 .exit_request = nvme_tcp_exit_request,
2508 .init_hctx = nvme_tcp_init_hctx,
2509 .timeout = nvme_tcp_timeout,
2510 .map_queues = nvme_tcp_map_queues,
2511 .poll = nvme_tcp_poll,
2514 static const struct blk_mq_ops nvme_tcp_admin_mq_ops = {
2515 .queue_rq = nvme_tcp_queue_rq,
2516 .complete = nvme_complete_rq,
2517 .init_request = nvme_tcp_init_request,
2518 .exit_request = nvme_tcp_exit_request,
2519 .init_hctx = nvme_tcp_init_admin_hctx,
2520 .timeout = nvme_tcp_timeout,
2523 static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
2525 .module = THIS_MODULE,
2526 .flags = NVME_F_FABRICS,
2527 .reg_read32 = nvmf_reg_read32,
2528 .reg_read64 = nvmf_reg_read64,
2529 .reg_write32 = nvmf_reg_write32,
2530 .free_ctrl = nvme_tcp_free_ctrl,
2531 .submit_async_event = nvme_tcp_submit_async_event,
2532 .delete_ctrl = nvme_tcp_delete_ctrl,
2533 .get_address = nvmf_get_address,
2534 .stop_ctrl = nvme_tcp_stop_ctrl,
2538 nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
2540 struct nvme_tcp_ctrl *ctrl;
2543 mutex_lock(&nvme_tcp_ctrl_mutex);
2544 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
2545 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2549 mutex_unlock(&nvme_tcp_ctrl_mutex);
2554 static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
2555 struct nvmf_ctrl_options *opts)
2557 struct nvme_tcp_ctrl *ctrl;
2560 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2562 return ERR_PTR(-ENOMEM);
2564 INIT_LIST_HEAD(&ctrl->list);
2565 ctrl->ctrl.opts = opts;
2566 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2567 opts->nr_poll_queues + 1;
2568 ctrl->ctrl.sqsize = opts->queue_size - 1;
2569 ctrl->ctrl.kato = opts->kato;
2571 INIT_DELAYED_WORK(&ctrl->connect_work,
2572 nvme_tcp_reconnect_ctrl_work);
2573 INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
2574 INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
2576 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2578 kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
2579 if (!opts->trsvcid) {
2583 opts->mask |= NVMF_OPT_TRSVCID;
2586 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2587 opts->traddr, opts->trsvcid, &ctrl->addr);
2589 pr_err("malformed address passed: %s:%s\n",
2590 opts->traddr, opts->trsvcid);
2594 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2595 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2596 opts->host_traddr, NULL, &ctrl->src_addr);
2598 pr_err("malformed src address passed: %s\n",
2604 if (opts->mask & NVMF_OPT_HOST_IFACE) {
2605 if (!__dev_get_by_name(&init_net, opts->host_iface)) {
2606 pr_err("invalid interface passed: %s\n",
2613 if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
2618 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
2620 if (!ctrl->queues) {
2625 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
2627 goto out_kfree_queues;
2629 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2632 goto out_uninit_ctrl;
2635 ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
2637 goto out_uninit_ctrl;
2639 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
2640 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2642 mutex_lock(&nvme_tcp_ctrl_mutex);
2643 list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
2644 mutex_unlock(&nvme_tcp_ctrl_mutex);
2649 nvme_uninit_ctrl(&ctrl->ctrl);
2650 nvme_put_ctrl(&ctrl->ctrl);
2653 return ERR_PTR(ret);
2655 kfree(ctrl->queues);
2658 return ERR_PTR(ret);
2661 static struct nvmf_transport_ops nvme_tcp_transport = {
2663 .module = THIS_MODULE,
2664 .required_opts = NVMF_OPT_TRADDR,
2665 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2666 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2667 NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
2668 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2669 NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE,
2670 .create_ctrl = nvme_tcp_create_ctrl,
2673 static int __init nvme_tcp_init_module(void)
2675 nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq",
2676 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2680 nvmf_register_transport(&nvme_tcp_transport);
2684 static void __exit nvme_tcp_cleanup_module(void)
2686 struct nvme_tcp_ctrl *ctrl;
2688 nvmf_unregister_transport(&nvme_tcp_transport);
2690 mutex_lock(&nvme_tcp_ctrl_mutex);
2691 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
2692 nvme_delete_ctrl(&ctrl->ctrl);
2693 mutex_unlock(&nvme_tcp_ctrl_mutex);
2694 flush_workqueue(nvme_delete_wq);
2696 destroy_workqueue(nvme_tcp_wq);
2699 module_init(nvme_tcp_init_module);
2700 module_exit(nvme_tcp_cleanup_module);
2702 MODULE_LICENSE("GPL v2");