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;
127 size_t data_remaining;
128 size_t ddgst_remaining;
132 struct nvme_tcp_request *request;
135 size_t cmnd_capsule_len;
136 struct nvme_tcp_ctrl *ctrl;
142 struct ahash_request *rcv_hash;
143 struct ahash_request *snd_hash;
147 struct page_frag_cache pf_cache;
149 void (*state_change)(struct sock *);
150 void (*data_ready)(struct sock *);
151 void (*write_space)(struct sock *);
154 struct nvme_tcp_ctrl {
155 /* read only in the hot path */
156 struct nvme_tcp_queue *queues;
157 struct blk_mq_tag_set tag_set;
159 /* other member variables */
160 struct list_head list;
161 struct blk_mq_tag_set admin_tag_set;
162 struct sockaddr_storage addr;
163 struct sockaddr_storage src_addr;
164 struct nvme_ctrl ctrl;
166 struct work_struct err_work;
167 struct delayed_work connect_work;
168 struct nvme_tcp_request async_req;
169 u32 io_queues[HCTX_MAX_TYPES];
172 static LIST_HEAD(nvme_tcp_ctrl_list);
173 static DEFINE_MUTEX(nvme_tcp_ctrl_mutex);
174 static struct workqueue_struct *nvme_tcp_wq;
175 static const struct blk_mq_ops nvme_tcp_mq_ops;
176 static const struct blk_mq_ops nvme_tcp_admin_mq_ops;
177 static int nvme_tcp_try_send(struct nvme_tcp_queue *queue);
179 static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl)
181 return container_of(ctrl, struct nvme_tcp_ctrl, ctrl);
184 static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
186 return queue - queue->ctrl->queues;
189 static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue)
191 u32 queue_idx = nvme_tcp_queue_id(queue);
194 return queue->ctrl->admin_tag_set.tags[queue_idx];
195 return queue->ctrl->tag_set.tags[queue_idx - 1];
198 static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue)
200 return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
203 static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue)
205 return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
208 static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_queue *queue)
210 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
213 static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req)
215 return req == &req->queue->ctrl->async_req;
218 static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req)
222 if (unlikely(nvme_tcp_async_req(req)))
223 return false; /* async events don't have a request */
225 rq = blk_mq_rq_from_pdu(req);
227 return rq_data_dir(rq) == WRITE && req->data_len &&
228 req->data_len <= nvme_tcp_inline_data_size(req->queue);
231 static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req)
233 return req->iter.bvec->bv_page;
236 static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req)
238 return req->iter.bvec->bv_offset + req->iter.iov_offset;
241 static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req)
243 return min_t(size_t, iov_iter_single_seg_count(&req->iter),
244 req->pdu_len - req->pdu_sent);
247 static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req)
249 return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ?
250 req->pdu_len - req->pdu_sent : 0;
253 static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
256 return nvme_tcp_pdu_data_left(req) <= len;
259 static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
262 struct request *rq = blk_mq_rq_from_pdu(req);
268 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
269 vec = &rq->special_vec;
271 size = blk_rq_payload_bytes(rq);
274 struct bio *bio = req->curr_bio;
278 vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
280 bio_for_each_bvec(bv, bio, bi) {
283 size = bio->bi_iter.bi_size;
284 offset = bio->bi_iter.bi_bvec_done;
287 iov_iter_bvec(&req->iter, dir, vec, nr_bvec, size);
288 req->iter.iov_offset = offset;
291 static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req,
294 req->data_sent += len;
295 req->pdu_sent += len;
296 iov_iter_advance(&req->iter, len);
297 if (!iov_iter_count(&req->iter) &&
298 req->data_sent < req->data_len) {
299 req->curr_bio = req->curr_bio->bi_next;
300 nvme_tcp_init_iter(req, WRITE);
304 static inline void nvme_tcp_send_all(struct nvme_tcp_queue *queue)
308 /* drain the send queue as much as we can... */
310 ret = nvme_tcp_try_send(queue);
314 static inline bool nvme_tcp_queue_more(struct nvme_tcp_queue *queue)
316 return !list_empty(&queue->send_list) ||
317 !llist_empty(&queue->req_list);
320 static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req,
321 bool sync, bool last)
323 struct nvme_tcp_queue *queue = req->queue;
326 empty = llist_add(&req->lentry, &queue->req_list) &&
327 list_empty(&queue->send_list) && !queue->request;
330 * if we're the first on the send_list and we can try to send
331 * directly, otherwise queue io_work. Also, only do that if we
332 * are on the same cpu, so we don't introduce contention.
334 if (queue->io_cpu == raw_smp_processor_id() &&
335 sync && empty && mutex_trylock(&queue->send_mutex)) {
336 nvme_tcp_send_all(queue);
337 mutex_unlock(&queue->send_mutex);
340 if (last && nvme_tcp_queue_more(queue))
341 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
344 static void nvme_tcp_process_req_list(struct nvme_tcp_queue *queue)
346 struct nvme_tcp_request *req;
347 struct llist_node *node;
349 for (node = llist_del_all(&queue->req_list); node; node = node->next) {
350 req = llist_entry(node, struct nvme_tcp_request, lentry);
351 list_add(&req->entry, &queue->send_list);
355 static inline struct nvme_tcp_request *
356 nvme_tcp_fetch_request(struct nvme_tcp_queue *queue)
358 struct nvme_tcp_request *req;
360 req = list_first_entry_or_null(&queue->send_list,
361 struct nvme_tcp_request, entry);
363 nvme_tcp_process_req_list(queue);
364 req = list_first_entry_or_null(&queue->send_list,
365 struct nvme_tcp_request, entry);
370 list_del(&req->entry);
374 static inline void nvme_tcp_ddgst_final(struct ahash_request *hash,
377 ahash_request_set_crypt(hash, NULL, (u8 *)dgst, 0);
378 crypto_ahash_final(hash);
381 static inline void nvme_tcp_ddgst_update(struct ahash_request *hash,
382 struct page *page, off_t off, size_t len)
384 struct scatterlist sg;
386 sg_init_marker(&sg, 1);
387 sg_set_page(&sg, page, len, off);
388 ahash_request_set_crypt(hash, &sg, NULL, len);
389 crypto_ahash_update(hash);
392 static inline void nvme_tcp_hdgst(struct ahash_request *hash,
393 void *pdu, size_t len)
395 struct scatterlist sg;
397 sg_init_one(&sg, pdu, len);
398 ahash_request_set_crypt(hash, &sg, pdu + len, len);
399 crypto_ahash_digest(hash);
402 static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue,
403 void *pdu, size_t pdu_len)
405 struct nvme_tcp_hdr *hdr = pdu;
409 if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
410 dev_err(queue->ctrl->ctrl.device,
411 "queue %d: header digest flag is cleared\n",
412 nvme_tcp_queue_id(queue));
416 recv_digest = *(__le32 *)(pdu + hdr->hlen);
417 nvme_tcp_hdgst(queue->rcv_hash, pdu, pdu_len);
418 exp_digest = *(__le32 *)(pdu + hdr->hlen);
419 if (recv_digest != exp_digest) {
420 dev_err(queue->ctrl->ctrl.device,
421 "header digest error: recv %#x expected %#x\n",
422 le32_to_cpu(recv_digest), le32_to_cpu(exp_digest));
429 static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu)
431 struct nvme_tcp_hdr *hdr = pdu;
432 u8 digest_len = nvme_tcp_hdgst_len(queue);
435 len = le32_to_cpu(hdr->plen) - hdr->hlen -
436 ((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0);
438 if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
439 dev_err(queue->ctrl->ctrl.device,
440 "queue %d: data digest flag is cleared\n",
441 nvme_tcp_queue_id(queue));
444 crypto_ahash_init(queue->rcv_hash);
449 static void nvme_tcp_exit_request(struct blk_mq_tag_set *set,
450 struct request *rq, unsigned int hctx_idx)
452 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
454 page_frag_free(req->pdu);
457 static int nvme_tcp_init_request(struct blk_mq_tag_set *set,
458 struct request *rq, unsigned int hctx_idx,
459 unsigned int numa_node)
461 struct nvme_tcp_ctrl *ctrl = set->driver_data;
462 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
463 struct nvme_tcp_cmd_pdu *pdu;
464 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
465 struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx];
466 u8 hdgst = nvme_tcp_hdgst_len(queue);
468 req->pdu = page_frag_alloc(&queue->pf_cache,
469 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
470 GFP_KERNEL | __GFP_ZERO);
476 nvme_req(rq)->ctrl = &ctrl->ctrl;
477 nvme_req(rq)->cmd = &pdu->cmd;
482 static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
483 unsigned int hctx_idx)
485 struct nvme_tcp_ctrl *ctrl = data;
486 struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1];
488 hctx->driver_data = queue;
492 static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
493 unsigned int hctx_idx)
495 struct nvme_tcp_ctrl *ctrl = data;
496 struct nvme_tcp_queue *queue = &ctrl->queues[0];
498 hctx->driver_data = queue;
502 static enum nvme_tcp_recv_state
503 nvme_tcp_recv_state(struct nvme_tcp_queue *queue)
505 return (queue->pdu_remaining) ? NVME_TCP_RECV_PDU :
506 (queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST :
510 static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
512 queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) +
513 nvme_tcp_hdgst_len(queue);
514 queue->pdu_offset = 0;
515 queue->data_remaining = -1;
516 queue->ddgst_remaining = 0;
519 static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
521 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
524 dev_warn(ctrl->device, "starting error recovery\n");
525 queue_work(nvme_reset_wq, &to_tcp_ctrl(ctrl)->err_work);
528 static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue,
529 struct nvme_completion *cqe)
531 struct nvme_tcp_request *req;
534 rq = nvme_find_rq(nvme_tcp_tagset(queue), cqe->command_id);
536 dev_err(queue->ctrl->ctrl.device,
537 "got bad cqe.command_id %#x on queue %d\n",
538 cqe->command_id, nvme_tcp_queue_id(queue));
539 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
543 req = blk_mq_rq_to_pdu(rq);
544 if (req->status == cpu_to_le16(NVME_SC_SUCCESS))
545 req->status = cqe->status;
547 if (!nvme_try_complete_req(rq, req->status, cqe->result))
548 nvme_complete_rq(rq);
554 static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
555 struct nvme_tcp_data_pdu *pdu)
559 rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
561 dev_err(queue->ctrl->ctrl.device,
562 "got bad c2hdata.command_id %#x on queue %d\n",
563 pdu->command_id, nvme_tcp_queue_id(queue));
567 if (!blk_rq_payload_bytes(rq)) {
568 dev_err(queue->ctrl->ctrl.device,
569 "queue %d tag %#x unexpected data\n",
570 nvme_tcp_queue_id(queue), rq->tag);
574 queue->data_remaining = le32_to_cpu(pdu->data_length);
576 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS &&
577 unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) {
578 dev_err(queue->ctrl->ctrl.device,
579 "queue %d tag %#x SUCCESS set but not last PDU\n",
580 nvme_tcp_queue_id(queue), rq->tag);
581 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
588 static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue,
589 struct nvme_tcp_rsp_pdu *pdu)
591 struct nvme_completion *cqe = &pdu->cqe;
595 * AEN requests are special as they don't time out and can
596 * survive any kind of queue freeze and often don't respond to
597 * aborts. We don't even bother to allocate a struct request
598 * for them but rather special case them here.
600 if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue),
602 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
605 ret = nvme_tcp_process_nvme_cqe(queue, cqe);
610 static int nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req,
611 struct nvme_tcp_r2t_pdu *pdu)
613 struct nvme_tcp_data_pdu *data = req->pdu;
614 struct nvme_tcp_queue *queue = req->queue;
615 struct request *rq = blk_mq_rq_from_pdu(req);
616 u8 hdgst = nvme_tcp_hdgst_len(queue);
617 u8 ddgst = nvme_tcp_ddgst_len(queue);
619 req->pdu_len = le32_to_cpu(pdu->r2t_length);
622 if (unlikely(!req->pdu_len)) {
623 dev_err(queue->ctrl->ctrl.device,
624 "req %d r2t len is %u, probably a bug...\n",
625 rq->tag, req->pdu_len);
629 if (unlikely(req->data_sent + req->pdu_len > req->data_len)) {
630 dev_err(queue->ctrl->ctrl.device,
631 "req %d r2t len %u exceeded data len %u (%zu sent)\n",
632 rq->tag, req->pdu_len, req->data_len,
637 if (unlikely(le32_to_cpu(pdu->r2t_offset) < req->data_sent)) {
638 dev_err(queue->ctrl->ctrl.device,
639 "req %d unexpected r2t offset %u (expected %zu)\n",
640 rq->tag, le32_to_cpu(pdu->r2t_offset),
645 memset(data, 0, sizeof(*data));
646 data->hdr.type = nvme_tcp_h2c_data;
647 data->hdr.flags = NVME_TCP_F_DATA_LAST;
648 if (queue->hdr_digest)
649 data->hdr.flags |= NVME_TCP_F_HDGST;
650 if (queue->data_digest)
651 data->hdr.flags |= NVME_TCP_F_DDGST;
652 data->hdr.hlen = sizeof(*data);
653 data->hdr.pdo = data->hdr.hlen + hdgst;
655 cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst);
656 data->ttag = pdu->ttag;
657 data->command_id = nvme_cid(rq);
658 data->data_offset = pdu->r2t_offset;
659 data->data_length = cpu_to_le32(req->pdu_len);
663 static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
664 struct nvme_tcp_r2t_pdu *pdu)
666 struct nvme_tcp_request *req;
670 rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
672 dev_err(queue->ctrl->ctrl.device,
673 "got bad r2t.command_id %#x on queue %d\n",
674 pdu->command_id, nvme_tcp_queue_id(queue));
677 req = blk_mq_rq_to_pdu(rq);
679 ret = nvme_tcp_setup_h2c_data_pdu(req, pdu);
683 req->state = NVME_TCP_SEND_H2C_PDU;
686 nvme_tcp_queue_request(req, false, true);
691 static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
692 unsigned int *offset, size_t *len)
694 struct nvme_tcp_hdr *hdr;
695 char *pdu = queue->pdu;
696 size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
699 ret = skb_copy_bits(skb, *offset,
700 &pdu[queue->pdu_offset], rcv_len);
704 queue->pdu_remaining -= rcv_len;
705 queue->pdu_offset += rcv_len;
708 if (queue->pdu_remaining)
712 if (queue->hdr_digest) {
713 ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen);
719 if (queue->data_digest) {
720 ret = nvme_tcp_check_ddgst(queue, queue->pdu);
726 case nvme_tcp_c2h_data:
727 return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu);
729 nvme_tcp_init_recv_ctx(queue);
730 return nvme_tcp_handle_comp(queue, (void *)queue->pdu);
732 nvme_tcp_init_recv_ctx(queue);
733 return nvme_tcp_handle_r2t(queue, (void *)queue->pdu);
735 dev_err(queue->ctrl->ctrl.device,
736 "unsupported pdu type (%d)\n", hdr->type);
741 static inline void nvme_tcp_end_request(struct request *rq, u16 status)
743 union nvme_result res = {};
745 if (!nvme_try_complete_req(rq, cpu_to_le16(status << 1), res))
746 nvme_complete_rq(rq);
749 static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb,
750 unsigned int *offset, size_t *len)
752 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
754 nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
755 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
760 recv_len = min_t(size_t, *len, queue->data_remaining);
764 if (!iov_iter_count(&req->iter)) {
765 req->curr_bio = req->curr_bio->bi_next;
768 * If we don`t have any bios it means that controller
769 * sent more data than we requested, hence error
771 if (!req->curr_bio) {
772 dev_err(queue->ctrl->ctrl.device,
773 "queue %d no space in request %#x",
774 nvme_tcp_queue_id(queue), rq->tag);
775 nvme_tcp_init_recv_ctx(queue);
778 nvme_tcp_init_iter(req, READ);
781 /* we can read only from what is left in this bio */
782 recv_len = min_t(size_t, recv_len,
783 iov_iter_count(&req->iter));
785 if (queue->data_digest)
786 ret = skb_copy_and_hash_datagram_iter(skb, *offset,
787 &req->iter, recv_len, queue->rcv_hash);
789 ret = skb_copy_datagram_iter(skb, *offset,
790 &req->iter, recv_len);
792 dev_err(queue->ctrl->ctrl.device,
793 "queue %d failed to copy request %#x data",
794 nvme_tcp_queue_id(queue), rq->tag);
800 queue->data_remaining -= recv_len;
803 if (!queue->data_remaining) {
804 if (queue->data_digest) {
805 nvme_tcp_ddgst_final(queue->rcv_hash, &queue->exp_ddgst);
806 queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH;
808 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
809 nvme_tcp_end_request(rq,
810 le16_to_cpu(req->status));
813 nvme_tcp_init_recv_ctx(queue);
820 static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue,
821 struct sk_buff *skb, unsigned int *offset, size_t *len)
823 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
824 char *ddgst = (char *)&queue->recv_ddgst;
825 size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining);
826 off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining;
829 ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len);
833 queue->ddgst_remaining -= recv_len;
836 if (queue->ddgst_remaining)
839 if (queue->recv_ddgst != queue->exp_ddgst) {
840 struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
842 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
844 req->status = cpu_to_le16(NVME_SC_DATA_XFER_ERROR);
846 dev_err(queue->ctrl->ctrl.device,
847 "data digest error: recv %#x expected %#x\n",
848 le32_to_cpu(queue->recv_ddgst),
849 le32_to_cpu(queue->exp_ddgst));
852 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
853 struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
855 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
857 nvme_tcp_end_request(rq, le16_to_cpu(req->status));
861 nvme_tcp_init_recv_ctx(queue);
865 static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,
866 unsigned int offset, size_t len)
868 struct nvme_tcp_queue *queue = desc->arg.data;
869 size_t consumed = len;
873 switch (nvme_tcp_recv_state(queue)) {
874 case NVME_TCP_RECV_PDU:
875 result = nvme_tcp_recv_pdu(queue, skb, &offset, &len);
877 case NVME_TCP_RECV_DATA:
878 result = nvme_tcp_recv_data(queue, skb, &offset, &len);
880 case NVME_TCP_RECV_DDGST:
881 result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len);
887 dev_err(queue->ctrl->ctrl.device,
888 "receive failed: %d\n", result);
889 queue->rd_enabled = false;
890 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
898 static void nvme_tcp_data_ready(struct sock *sk)
900 struct nvme_tcp_queue *queue;
902 read_lock_bh(&sk->sk_callback_lock);
903 queue = sk->sk_user_data;
904 if (likely(queue && queue->rd_enabled) &&
905 !test_bit(NVME_TCP_Q_POLLING, &queue->flags))
906 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
907 read_unlock_bh(&sk->sk_callback_lock);
910 static void nvme_tcp_write_space(struct sock *sk)
912 struct nvme_tcp_queue *queue;
914 read_lock_bh(&sk->sk_callback_lock);
915 queue = sk->sk_user_data;
916 if (likely(queue && sk_stream_is_writeable(sk))) {
917 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
918 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
920 read_unlock_bh(&sk->sk_callback_lock);
923 static void nvme_tcp_state_change(struct sock *sk)
925 struct nvme_tcp_queue *queue;
927 read_lock_bh(&sk->sk_callback_lock);
928 queue = sk->sk_user_data;
932 switch (sk->sk_state) {
938 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
941 dev_info(queue->ctrl->ctrl.device,
942 "queue %d socket state %d\n",
943 nvme_tcp_queue_id(queue), sk->sk_state);
946 queue->state_change(sk);
948 read_unlock_bh(&sk->sk_callback_lock);
951 static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue)
953 queue->request = NULL;
956 static void nvme_tcp_fail_request(struct nvme_tcp_request *req)
958 if (nvme_tcp_async_req(req)) {
959 union nvme_result res = {};
961 nvme_complete_async_event(&req->queue->ctrl->ctrl,
962 cpu_to_le16(NVME_SC_HOST_PATH_ERROR), &res);
964 nvme_tcp_end_request(blk_mq_rq_from_pdu(req),
965 NVME_SC_HOST_PATH_ERROR);
969 static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
971 struct nvme_tcp_queue *queue = req->queue;
972 int req_data_len = req->data_len;
975 struct page *page = nvme_tcp_req_cur_page(req);
976 size_t offset = nvme_tcp_req_cur_offset(req);
977 size_t len = nvme_tcp_req_cur_length(req);
978 bool last = nvme_tcp_pdu_last_send(req, len);
979 int req_data_sent = req->data_sent;
980 int ret, flags = MSG_DONTWAIT;
982 if (last && !queue->data_digest && !nvme_tcp_queue_more(queue))
985 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
987 if (sendpage_ok(page)) {
988 ret = kernel_sendpage(queue->sock, page, offset, len,
991 ret = sock_no_sendpage(queue->sock, page, offset, len,
997 if (queue->data_digest)
998 nvme_tcp_ddgst_update(queue->snd_hash, page,
1002 * update the request iterator except for the last payload send
1003 * in the request where we don't want to modify it as we may
1004 * compete with the RX path completing the request.
1006 if (req_data_sent + ret < req_data_len)
1007 nvme_tcp_advance_req(req, ret);
1009 /* fully successful last send in current PDU */
1010 if (last && ret == len) {
1011 if (queue->data_digest) {
1012 nvme_tcp_ddgst_final(queue->snd_hash,
1014 req->state = NVME_TCP_SEND_DDGST;
1017 nvme_tcp_done_send_req(queue);
1025 static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
1027 struct nvme_tcp_queue *queue = req->queue;
1028 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
1029 bool inline_data = nvme_tcp_has_inline_data(req);
1030 u8 hdgst = nvme_tcp_hdgst_len(queue);
1031 int len = sizeof(*pdu) + hdgst - req->offset;
1032 int flags = MSG_DONTWAIT;
1035 if (inline_data || nvme_tcp_queue_more(queue))
1036 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
1040 if (queue->hdr_digest && !req->offset)
1041 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
1043 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
1044 offset_in_page(pdu) + req->offset, len, flags);
1045 if (unlikely(ret <= 0))
1051 req->state = NVME_TCP_SEND_DATA;
1052 if (queue->data_digest)
1053 crypto_ahash_init(queue->snd_hash);
1055 nvme_tcp_done_send_req(queue);
1064 static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
1066 struct nvme_tcp_queue *queue = req->queue;
1067 struct nvme_tcp_data_pdu *pdu = req->pdu;
1068 u8 hdgst = nvme_tcp_hdgst_len(queue);
1069 int len = sizeof(*pdu) - req->offset + hdgst;
1072 if (queue->hdr_digest && !req->offset)
1073 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
1075 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
1076 offset_in_page(pdu) + req->offset, len,
1077 MSG_DONTWAIT | MSG_MORE | MSG_SENDPAGE_NOTLAST);
1078 if (unlikely(ret <= 0))
1083 req->state = NVME_TCP_SEND_DATA;
1084 if (queue->data_digest)
1085 crypto_ahash_init(queue->snd_hash);
1093 static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
1095 struct nvme_tcp_queue *queue = req->queue;
1096 size_t offset = req->offset;
1098 struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
1100 .iov_base = (u8 *)&req->ddgst + req->offset,
1101 .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset
1104 if (nvme_tcp_queue_more(queue))
1105 msg.msg_flags |= MSG_MORE;
1107 msg.msg_flags |= MSG_EOR;
1109 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1110 if (unlikely(ret <= 0))
1113 if (offset + ret == NVME_TCP_DIGEST_LENGTH) {
1114 nvme_tcp_done_send_req(queue);
1122 static int nvme_tcp_try_send(struct nvme_tcp_queue *queue)
1124 struct nvme_tcp_request *req;
1127 if (!queue->request) {
1128 queue->request = nvme_tcp_fetch_request(queue);
1129 if (!queue->request)
1132 req = queue->request;
1134 if (req->state == NVME_TCP_SEND_CMD_PDU) {
1135 ret = nvme_tcp_try_send_cmd_pdu(req);
1138 if (!nvme_tcp_has_inline_data(req))
1142 if (req->state == NVME_TCP_SEND_H2C_PDU) {
1143 ret = nvme_tcp_try_send_data_pdu(req);
1148 if (req->state == NVME_TCP_SEND_DATA) {
1149 ret = nvme_tcp_try_send_data(req);
1154 if (req->state == NVME_TCP_SEND_DDGST)
1155 ret = nvme_tcp_try_send_ddgst(req);
1157 if (ret == -EAGAIN) {
1159 } else if (ret < 0) {
1160 dev_err(queue->ctrl->ctrl.device,
1161 "failed to send request %d\n", ret);
1162 nvme_tcp_fail_request(queue->request);
1163 nvme_tcp_done_send_req(queue);
1168 static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue)
1170 struct socket *sock = queue->sock;
1171 struct sock *sk = sock->sk;
1172 read_descriptor_t rd_desc;
1175 rd_desc.arg.data = queue;
1179 consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb);
1184 static void nvme_tcp_io_work(struct work_struct *w)
1186 struct nvme_tcp_queue *queue =
1187 container_of(w, struct nvme_tcp_queue, io_work);
1188 unsigned long deadline = jiffies + msecs_to_jiffies(1);
1191 bool pending = false;
1194 if (mutex_trylock(&queue->send_mutex)) {
1195 result = nvme_tcp_try_send(queue);
1196 mutex_unlock(&queue->send_mutex);
1199 else if (unlikely(result < 0))
1203 result = nvme_tcp_try_recv(queue);
1206 else if (unlikely(result < 0))
1209 if (!pending || !queue->rd_enabled)
1212 } while (!time_after(jiffies, deadline)); /* quota is exhausted */
1214 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
1217 static void nvme_tcp_free_crypto(struct nvme_tcp_queue *queue)
1219 struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
1221 ahash_request_free(queue->rcv_hash);
1222 ahash_request_free(queue->snd_hash);
1223 crypto_free_ahash(tfm);
1226 static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue *queue)
1228 struct crypto_ahash *tfm;
1230 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
1232 return PTR_ERR(tfm);
1234 queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1235 if (!queue->snd_hash)
1237 ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
1239 queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1240 if (!queue->rcv_hash)
1242 ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
1246 ahash_request_free(queue->snd_hash);
1248 crypto_free_ahash(tfm);
1252 static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl)
1254 struct nvme_tcp_request *async = &ctrl->async_req;
1256 page_frag_free(async->pdu);
1259 static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
1261 struct nvme_tcp_queue *queue = &ctrl->queues[0];
1262 struct nvme_tcp_request *async = &ctrl->async_req;
1263 u8 hdgst = nvme_tcp_hdgst_len(queue);
1265 async->pdu = page_frag_alloc(&queue->pf_cache,
1266 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
1267 GFP_KERNEL | __GFP_ZERO);
1271 async->queue = &ctrl->queues[0];
1275 static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
1277 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1278 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1280 if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
1283 if (queue->hdr_digest || queue->data_digest)
1284 nvme_tcp_free_crypto(queue);
1286 sock_release(queue->sock);
1288 mutex_destroy(&queue->send_mutex);
1289 mutex_destroy(&queue->queue_lock);
1292 static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
1294 struct nvme_tcp_icreq_pdu *icreq;
1295 struct nvme_tcp_icresp_pdu *icresp;
1296 struct msghdr msg = {};
1298 bool ctrl_hdgst, ctrl_ddgst;
1301 icreq = kzalloc(sizeof(*icreq), GFP_KERNEL);
1305 icresp = kzalloc(sizeof(*icresp), GFP_KERNEL);
1311 icreq->hdr.type = nvme_tcp_icreq;
1312 icreq->hdr.hlen = sizeof(*icreq);
1314 icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen);
1315 icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
1316 icreq->maxr2t = 0; /* single inflight r2t supported */
1317 icreq->hpda = 0; /* no alignment constraint */
1318 if (queue->hdr_digest)
1319 icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
1320 if (queue->data_digest)
1321 icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
1323 iov.iov_base = icreq;
1324 iov.iov_len = sizeof(*icreq);
1325 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1329 memset(&msg, 0, sizeof(msg));
1330 iov.iov_base = icresp;
1331 iov.iov_len = sizeof(*icresp);
1332 ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1333 iov.iov_len, msg.msg_flags);
1338 if (icresp->hdr.type != nvme_tcp_icresp) {
1339 pr_err("queue %d: bad type returned %d\n",
1340 nvme_tcp_queue_id(queue), icresp->hdr.type);
1344 if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) {
1345 pr_err("queue %d: bad pdu length returned %d\n",
1346 nvme_tcp_queue_id(queue), icresp->hdr.plen);
1350 if (icresp->pfv != NVME_TCP_PFV_1_0) {
1351 pr_err("queue %d: bad pfv returned %d\n",
1352 nvme_tcp_queue_id(queue), icresp->pfv);
1356 ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE);
1357 if ((queue->data_digest && !ctrl_ddgst) ||
1358 (!queue->data_digest && ctrl_ddgst)) {
1359 pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
1360 nvme_tcp_queue_id(queue),
1361 queue->data_digest ? "enabled" : "disabled",
1362 ctrl_ddgst ? "enabled" : "disabled");
1366 ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE);
1367 if ((queue->hdr_digest && !ctrl_hdgst) ||
1368 (!queue->hdr_digest && ctrl_hdgst)) {
1369 pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
1370 nvme_tcp_queue_id(queue),
1371 queue->hdr_digest ? "enabled" : "disabled",
1372 ctrl_hdgst ? "enabled" : "disabled");
1376 if (icresp->cpda != 0) {
1377 pr_err("queue %d: unsupported cpda returned %d\n",
1378 nvme_tcp_queue_id(queue), icresp->cpda);
1390 static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue)
1392 return nvme_tcp_queue_id(queue) == 0;
1395 static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue)
1397 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1398 int qid = nvme_tcp_queue_id(queue);
1400 return !nvme_tcp_admin_queue(queue) &&
1401 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT];
1404 static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue)
1406 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1407 int qid = nvme_tcp_queue_id(queue);
1409 return !nvme_tcp_admin_queue(queue) &&
1410 !nvme_tcp_default_queue(queue) &&
1411 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1412 ctrl->io_queues[HCTX_TYPE_READ];
1415 static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue)
1417 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1418 int qid = nvme_tcp_queue_id(queue);
1420 return !nvme_tcp_admin_queue(queue) &&
1421 !nvme_tcp_default_queue(queue) &&
1422 !nvme_tcp_read_queue(queue) &&
1423 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1424 ctrl->io_queues[HCTX_TYPE_READ] +
1425 ctrl->io_queues[HCTX_TYPE_POLL];
1428 static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue)
1430 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1431 int qid = nvme_tcp_queue_id(queue);
1434 if (nvme_tcp_default_queue(queue))
1436 else if (nvme_tcp_read_queue(queue))
1437 n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] - 1;
1438 else if (nvme_tcp_poll_queue(queue))
1439 n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] -
1440 ctrl->io_queues[HCTX_TYPE_READ] - 1;
1441 queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
1444 static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl,
1445 int qid, size_t queue_size)
1447 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1448 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1449 int ret, rcv_pdu_size;
1451 mutex_init(&queue->queue_lock);
1453 init_llist_head(&queue->req_list);
1454 INIT_LIST_HEAD(&queue->send_list);
1455 mutex_init(&queue->send_mutex);
1456 INIT_WORK(&queue->io_work, nvme_tcp_io_work);
1457 queue->queue_size = queue_size;
1460 queue->cmnd_capsule_len = nctrl->ioccsz * 16;
1462 queue->cmnd_capsule_len = sizeof(struct nvme_command) +
1463 NVME_TCP_ADMIN_CCSZ;
1465 ret = sock_create(ctrl->addr.ss_family, SOCK_STREAM,
1466 IPPROTO_TCP, &queue->sock);
1468 dev_err(nctrl->device,
1469 "failed to create socket: %d\n", ret);
1470 goto err_destroy_mutex;
1473 nvme_tcp_reclassify_socket(queue->sock);
1475 /* Single syn retry */
1476 tcp_sock_set_syncnt(queue->sock->sk, 1);
1478 /* Set TCP no delay */
1479 tcp_sock_set_nodelay(queue->sock->sk);
1482 * Cleanup whatever is sitting in the TCP transmit queue on socket
1483 * close. This is done to prevent stale data from being sent should
1484 * the network connection be restored before TCP times out.
1486 sock_no_linger(queue->sock->sk);
1488 if (so_priority > 0)
1489 sock_set_priority(queue->sock->sk, so_priority);
1491 /* Set socket type of service */
1492 if (nctrl->opts->tos >= 0)
1493 ip_sock_set_tos(queue->sock->sk, nctrl->opts->tos);
1495 /* Set 10 seconds timeout for icresp recvmsg */
1496 queue->sock->sk->sk_rcvtimeo = 10 * HZ;
1498 queue->sock->sk->sk_allocation = GFP_ATOMIC;
1499 nvme_tcp_set_queue_io_cpu(queue);
1500 queue->request = NULL;
1501 queue->data_remaining = 0;
1502 queue->ddgst_remaining = 0;
1503 queue->pdu_remaining = 0;
1504 queue->pdu_offset = 0;
1505 sk_set_memalloc(queue->sock->sk);
1507 if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) {
1508 ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr,
1509 sizeof(ctrl->src_addr));
1511 dev_err(nctrl->device,
1512 "failed to bind queue %d socket %d\n",
1518 if (nctrl->opts->mask & NVMF_OPT_HOST_IFACE) {
1519 char *iface = nctrl->opts->host_iface;
1520 sockptr_t optval = KERNEL_SOCKPTR(iface);
1522 ret = sock_setsockopt(queue->sock, SOL_SOCKET, SO_BINDTODEVICE,
1523 optval, strlen(iface));
1525 dev_err(nctrl->device,
1526 "failed to bind to interface %s queue %d err %d\n",
1532 queue->hdr_digest = nctrl->opts->hdr_digest;
1533 queue->data_digest = nctrl->opts->data_digest;
1534 if (queue->hdr_digest || queue->data_digest) {
1535 ret = nvme_tcp_alloc_crypto(queue);
1537 dev_err(nctrl->device,
1538 "failed to allocate queue %d crypto\n", qid);
1543 rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) +
1544 nvme_tcp_hdgst_len(queue);
1545 queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL);
1551 dev_dbg(nctrl->device, "connecting queue %d\n",
1552 nvme_tcp_queue_id(queue));
1554 ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr,
1555 sizeof(ctrl->addr), 0);
1557 dev_err(nctrl->device,
1558 "failed to connect socket: %d\n", ret);
1562 ret = nvme_tcp_init_connection(queue);
1564 goto err_init_connect;
1566 queue->rd_enabled = true;
1567 set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags);
1568 nvme_tcp_init_recv_ctx(queue);
1570 write_lock_bh(&queue->sock->sk->sk_callback_lock);
1571 queue->sock->sk->sk_user_data = queue;
1572 queue->state_change = queue->sock->sk->sk_state_change;
1573 queue->data_ready = queue->sock->sk->sk_data_ready;
1574 queue->write_space = queue->sock->sk->sk_write_space;
1575 queue->sock->sk->sk_data_ready = nvme_tcp_data_ready;
1576 queue->sock->sk->sk_state_change = nvme_tcp_state_change;
1577 queue->sock->sk->sk_write_space = nvme_tcp_write_space;
1578 #ifdef CONFIG_NET_RX_BUSY_POLL
1579 queue->sock->sk->sk_ll_usec = 1;
1581 write_unlock_bh(&queue->sock->sk->sk_callback_lock);
1586 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1590 if (queue->hdr_digest || queue->data_digest)
1591 nvme_tcp_free_crypto(queue);
1593 sock_release(queue->sock);
1596 mutex_destroy(&queue->send_mutex);
1597 mutex_destroy(&queue->queue_lock);
1601 static void nvme_tcp_restore_sock_calls(struct nvme_tcp_queue *queue)
1603 struct socket *sock = queue->sock;
1605 write_lock_bh(&sock->sk->sk_callback_lock);
1606 sock->sk->sk_user_data = NULL;
1607 sock->sk->sk_data_ready = queue->data_ready;
1608 sock->sk->sk_state_change = queue->state_change;
1609 sock->sk->sk_write_space = queue->write_space;
1610 write_unlock_bh(&sock->sk->sk_callback_lock);
1613 static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
1615 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1616 nvme_tcp_restore_sock_calls(queue);
1617 cancel_work_sync(&queue->io_work);
1620 static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
1622 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1623 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1625 mutex_lock(&queue->queue_lock);
1626 if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
1627 __nvme_tcp_stop_queue(queue);
1628 mutex_unlock(&queue->queue_lock);
1631 static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
1633 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1637 ret = nvmf_connect_io_queue(nctrl, idx);
1639 ret = nvmf_connect_admin_queue(nctrl);
1642 set_bit(NVME_TCP_Q_LIVE, &ctrl->queues[idx].flags);
1644 if (test_bit(NVME_TCP_Q_ALLOCATED, &ctrl->queues[idx].flags))
1645 __nvme_tcp_stop_queue(&ctrl->queues[idx]);
1646 dev_err(nctrl->device,
1647 "failed to connect queue: %d ret=%d\n", idx, ret);
1652 static struct blk_mq_tag_set *nvme_tcp_alloc_tagset(struct nvme_ctrl *nctrl,
1655 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1656 struct blk_mq_tag_set *set;
1660 set = &ctrl->admin_tag_set;
1661 memset(set, 0, sizeof(*set));
1662 set->ops = &nvme_tcp_admin_mq_ops;
1663 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
1664 set->reserved_tags = NVMF_RESERVED_TAGS;
1665 set->numa_node = nctrl->numa_node;
1666 set->flags = BLK_MQ_F_BLOCKING;
1667 set->cmd_size = sizeof(struct nvme_tcp_request);
1668 set->driver_data = ctrl;
1669 set->nr_hw_queues = 1;
1670 set->timeout = NVME_ADMIN_TIMEOUT;
1672 set = &ctrl->tag_set;
1673 memset(set, 0, sizeof(*set));
1674 set->ops = &nvme_tcp_mq_ops;
1675 set->queue_depth = nctrl->sqsize + 1;
1676 set->reserved_tags = NVMF_RESERVED_TAGS;
1677 set->numa_node = nctrl->numa_node;
1678 set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
1679 set->cmd_size = sizeof(struct nvme_tcp_request);
1680 set->driver_data = ctrl;
1681 set->nr_hw_queues = nctrl->queue_count - 1;
1682 set->timeout = NVME_IO_TIMEOUT;
1683 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
1686 ret = blk_mq_alloc_tag_set(set);
1688 return ERR_PTR(ret);
1693 static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl)
1695 if (to_tcp_ctrl(ctrl)->async_req.pdu) {
1696 cancel_work_sync(&ctrl->async_event_work);
1697 nvme_tcp_free_async_req(to_tcp_ctrl(ctrl));
1698 to_tcp_ctrl(ctrl)->async_req.pdu = NULL;
1701 nvme_tcp_free_queue(ctrl, 0);
1704 static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl)
1708 for (i = 1; i < ctrl->queue_count; i++)
1709 nvme_tcp_free_queue(ctrl, i);
1712 static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl)
1716 for (i = 1; i < ctrl->queue_count; i++)
1717 nvme_tcp_stop_queue(ctrl, i);
1720 static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl)
1724 for (i = 1; i < ctrl->queue_count; i++) {
1725 ret = nvme_tcp_start_queue(ctrl, i);
1727 goto out_stop_queues;
1733 for (i--; i >= 1; i--)
1734 nvme_tcp_stop_queue(ctrl, i);
1738 static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
1742 ret = nvme_tcp_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
1746 ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
1748 goto out_free_queue;
1753 nvme_tcp_free_queue(ctrl, 0);
1757 static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
1761 for (i = 1; i < ctrl->queue_count; i++) {
1762 ret = nvme_tcp_alloc_queue(ctrl, i,
1765 goto out_free_queues;
1771 for (i--; i >= 1; i--)
1772 nvme_tcp_free_queue(ctrl, i);
1777 static unsigned int nvme_tcp_nr_io_queues(struct nvme_ctrl *ctrl)
1779 unsigned int nr_io_queues;
1781 nr_io_queues = min(ctrl->opts->nr_io_queues, num_online_cpus());
1782 nr_io_queues += min(ctrl->opts->nr_write_queues, num_online_cpus());
1783 nr_io_queues += min(ctrl->opts->nr_poll_queues, num_online_cpus());
1785 return nr_io_queues;
1788 static void nvme_tcp_set_io_queues(struct nvme_ctrl *nctrl,
1789 unsigned int nr_io_queues)
1791 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1792 struct nvmf_ctrl_options *opts = nctrl->opts;
1794 if (opts->nr_write_queues && opts->nr_io_queues < nr_io_queues) {
1796 * separate read/write queues
1797 * hand out dedicated default queues only after we have
1798 * sufficient read queues.
1800 ctrl->io_queues[HCTX_TYPE_READ] = opts->nr_io_queues;
1801 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
1802 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1803 min(opts->nr_write_queues, nr_io_queues);
1804 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1807 * shared read/write queues
1808 * either no write queues were requested, or we don't have
1809 * sufficient queue count to have dedicated default queues.
1811 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1812 min(opts->nr_io_queues, nr_io_queues);
1813 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1816 if (opts->nr_poll_queues && nr_io_queues) {
1817 /* map dedicated poll queues only if we have queues left */
1818 ctrl->io_queues[HCTX_TYPE_POLL] =
1819 min(opts->nr_poll_queues, nr_io_queues);
1823 static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
1825 unsigned int nr_io_queues;
1828 nr_io_queues = nvme_tcp_nr_io_queues(ctrl);
1829 ret = nvme_set_queue_count(ctrl, &nr_io_queues);
1833 if (nr_io_queues == 0) {
1834 dev_err(ctrl->device,
1835 "unable to set any I/O queues\n");
1839 ctrl->queue_count = nr_io_queues + 1;
1840 dev_info(ctrl->device,
1841 "creating %d I/O queues.\n", nr_io_queues);
1843 nvme_tcp_set_io_queues(ctrl, nr_io_queues);
1845 return __nvme_tcp_alloc_io_queues(ctrl);
1848 static void nvme_tcp_destroy_io_queues(struct nvme_ctrl *ctrl, bool remove)
1850 nvme_tcp_stop_io_queues(ctrl);
1852 blk_cleanup_queue(ctrl->connect_q);
1853 blk_mq_free_tag_set(ctrl->tagset);
1855 nvme_tcp_free_io_queues(ctrl);
1858 static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
1862 ret = nvme_tcp_alloc_io_queues(ctrl);
1867 ctrl->tagset = nvme_tcp_alloc_tagset(ctrl, false);
1868 if (IS_ERR(ctrl->tagset)) {
1869 ret = PTR_ERR(ctrl->tagset);
1870 goto out_free_io_queues;
1873 ctrl->connect_q = blk_mq_init_queue(ctrl->tagset);
1874 if (IS_ERR(ctrl->connect_q)) {
1875 ret = PTR_ERR(ctrl->connect_q);
1876 goto out_free_tag_set;
1880 ret = nvme_tcp_start_io_queues(ctrl);
1882 goto out_cleanup_connect_q;
1885 nvme_start_queues(ctrl);
1886 if (!nvme_wait_freeze_timeout(ctrl, NVME_IO_TIMEOUT)) {
1888 * If we timed out waiting for freeze we are likely to
1889 * be stuck. Fail the controller initialization just
1893 goto out_wait_freeze_timed_out;
1895 blk_mq_update_nr_hw_queues(ctrl->tagset,
1896 ctrl->queue_count - 1);
1897 nvme_unfreeze(ctrl);
1902 out_wait_freeze_timed_out:
1903 nvme_stop_queues(ctrl);
1904 nvme_sync_io_queues(ctrl);
1905 nvme_tcp_stop_io_queues(ctrl);
1906 out_cleanup_connect_q:
1907 nvme_cancel_tagset(ctrl);
1909 blk_cleanup_queue(ctrl->connect_q);
1912 blk_mq_free_tag_set(ctrl->tagset);
1914 nvme_tcp_free_io_queues(ctrl);
1918 static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl *ctrl, bool remove)
1920 nvme_tcp_stop_queue(ctrl, 0);
1922 blk_cleanup_queue(ctrl->admin_q);
1923 blk_cleanup_queue(ctrl->fabrics_q);
1924 blk_mq_free_tag_set(ctrl->admin_tagset);
1926 nvme_tcp_free_admin_queue(ctrl);
1929 static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
1933 error = nvme_tcp_alloc_admin_queue(ctrl);
1938 ctrl->admin_tagset = nvme_tcp_alloc_tagset(ctrl, true);
1939 if (IS_ERR(ctrl->admin_tagset)) {
1940 error = PTR_ERR(ctrl->admin_tagset);
1941 goto out_free_queue;
1944 ctrl->fabrics_q = blk_mq_init_queue(ctrl->admin_tagset);
1945 if (IS_ERR(ctrl->fabrics_q)) {
1946 error = PTR_ERR(ctrl->fabrics_q);
1947 goto out_free_tagset;
1950 ctrl->admin_q = blk_mq_init_queue(ctrl->admin_tagset);
1951 if (IS_ERR(ctrl->admin_q)) {
1952 error = PTR_ERR(ctrl->admin_q);
1953 goto out_cleanup_fabrics_q;
1957 error = nvme_tcp_start_queue(ctrl, 0);
1959 goto out_cleanup_queue;
1961 error = nvme_enable_ctrl(ctrl);
1963 goto out_stop_queue;
1965 blk_mq_unquiesce_queue(ctrl->admin_q);
1967 error = nvme_init_ctrl_finish(ctrl);
1969 goto out_quiesce_queue;
1974 blk_mq_quiesce_queue(ctrl->admin_q);
1975 blk_sync_queue(ctrl->admin_q);
1977 nvme_tcp_stop_queue(ctrl, 0);
1978 nvme_cancel_admin_tagset(ctrl);
1981 blk_cleanup_queue(ctrl->admin_q);
1982 out_cleanup_fabrics_q:
1984 blk_cleanup_queue(ctrl->fabrics_q);
1987 blk_mq_free_tag_set(ctrl->admin_tagset);
1989 nvme_tcp_free_admin_queue(ctrl);
1993 static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
1996 blk_mq_quiesce_queue(ctrl->admin_q);
1997 blk_sync_queue(ctrl->admin_q);
1998 nvme_tcp_stop_queue(ctrl, 0);
1999 nvme_cancel_admin_tagset(ctrl);
2001 blk_mq_unquiesce_queue(ctrl->admin_q);
2002 nvme_tcp_destroy_admin_queue(ctrl, remove);
2005 static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
2008 if (ctrl->queue_count <= 1)
2010 blk_mq_quiesce_queue(ctrl->admin_q);
2011 nvme_start_freeze(ctrl);
2012 nvme_stop_queues(ctrl);
2013 nvme_sync_io_queues(ctrl);
2014 nvme_tcp_stop_io_queues(ctrl);
2015 nvme_cancel_tagset(ctrl);
2017 nvme_start_queues(ctrl);
2018 nvme_tcp_destroy_io_queues(ctrl, remove);
2021 static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl)
2023 /* If we are resetting/deleting then do nothing */
2024 if (ctrl->state != NVME_CTRL_CONNECTING) {
2025 WARN_ON_ONCE(ctrl->state == NVME_CTRL_NEW ||
2026 ctrl->state == NVME_CTRL_LIVE);
2030 if (nvmf_should_reconnect(ctrl)) {
2031 dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
2032 ctrl->opts->reconnect_delay);
2033 queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
2034 ctrl->opts->reconnect_delay * HZ);
2036 dev_info(ctrl->device, "Removing controller...\n");
2037 nvme_delete_ctrl(ctrl);
2041 static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
2043 struct nvmf_ctrl_options *opts = ctrl->opts;
2046 ret = nvme_tcp_configure_admin_queue(ctrl, new);
2052 dev_err(ctrl->device, "icdoff is not supported!\n");
2056 if (!nvme_ctrl_sgl_supported(ctrl)) {
2058 dev_err(ctrl->device, "Mandatory sgls are not supported!\n");
2062 if (opts->queue_size > ctrl->sqsize + 1)
2063 dev_warn(ctrl->device,
2064 "queue_size %zu > ctrl sqsize %u, clamping down\n",
2065 opts->queue_size, ctrl->sqsize + 1);
2067 if (ctrl->sqsize + 1 > ctrl->maxcmd) {
2068 dev_warn(ctrl->device,
2069 "sqsize %u > ctrl maxcmd %u, clamping down\n",
2070 ctrl->sqsize + 1, ctrl->maxcmd);
2071 ctrl->sqsize = ctrl->maxcmd - 1;
2074 if (ctrl->queue_count > 1) {
2075 ret = nvme_tcp_configure_io_queues(ctrl, new);
2080 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
2082 * state change failure is ok if we started ctrl delete,
2083 * unless we're during creation of a new controller to
2084 * avoid races with teardown flow.
2086 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2087 ctrl->state != NVME_CTRL_DELETING_NOIO);
2093 nvme_start_ctrl(ctrl);
2097 if (ctrl->queue_count > 1) {
2098 nvme_stop_queues(ctrl);
2099 nvme_sync_io_queues(ctrl);
2100 nvme_tcp_stop_io_queues(ctrl);
2101 nvme_cancel_tagset(ctrl);
2102 nvme_tcp_destroy_io_queues(ctrl, new);
2105 blk_mq_quiesce_queue(ctrl->admin_q);
2106 blk_sync_queue(ctrl->admin_q);
2107 nvme_tcp_stop_queue(ctrl, 0);
2108 nvme_cancel_admin_tagset(ctrl);
2109 nvme_tcp_destroy_admin_queue(ctrl, new);
2113 static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
2115 struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
2116 struct nvme_tcp_ctrl, connect_work);
2117 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2119 ++ctrl->nr_reconnects;
2121 if (nvme_tcp_setup_ctrl(ctrl, false))
2124 dev_info(ctrl->device, "Successfully reconnected (%d attempt)\n",
2125 ctrl->nr_reconnects);
2127 ctrl->nr_reconnects = 0;
2132 dev_info(ctrl->device, "Failed reconnect attempt %d\n",
2133 ctrl->nr_reconnects);
2134 nvme_tcp_reconnect_or_remove(ctrl);
2137 static void nvme_tcp_error_recovery_work(struct work_struct *work)
2139 struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
2140 struct nvme_tcp_ctrl, err_work);
2141 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2143 nvme_stop_keep_alive(ctrl);
2144 flush_work(&ctrl->async_event_work);
2145 nvme_tcp_teardown_io_queues(ctrl, false);
2146 /* unquiesce to fail fast pending requests */
2147 nvme_start_queues(ctrl);
2148 nvme_tcp_teardown_admin_queue(ctrl, false);
2149 blk_mq_unquiesce_queue(ctrl->admin_q);
2151 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2152 /* state change failure is ok if we started ctrl delete */
2153 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2154 ctrl->state != NVME_CTRL_DELETING_NOIO);
2158 nvme_tcp_reconnect_or_remove(ctrl);
2161 static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
2163 nvme_tcp_teardown_io_queues(ctrl, shutdown);
2164 blk_mq_quiesce_queue(ctrl->admin_q);
2166 nvme_shutdown_ctrl(ctrl);
2168 nvme_disable_ctrl(ctrl);
2169 nvme_tcp_teardown_admin_queue(ctrl, shutdown);
2172 static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
2174 nvme_tcp_teardown_ctrl(ctrl, true);
2177 static void nvme_reset_ctrl_work(struct work_struct *work)
2179 struct nvme_ctrl *ctrl =
2180 container_of(work, struct nvme_ctrl, reset_work);
2182 nvme_stop_ctrl(ctrl);
2183 nvme_tcp_teardown_ctrl(ctrl, false);
2185 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2186 /* state change failure is ok if we started ctrl delete */
2187 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2188 ctrl->state != NVME_CTRL_DELETING_NOIO);
2192 if (nvme_tcp_setup_ctrl(ctrl, false))
2198 ++ctrl->nr_reconnects;
2199 nvme_tcp_reconnect_or_remove(ctrl);
2202 static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl)
2204 cancel_work_sync(&to_tcp_ctrl(ctrl)->err_work);
2205 cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
2208 static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
2210 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
2212 if (list_empty(&ctrl->list))
2215 mutex_lock(&nvme_tcp_ctrl_mutex);
2216 list_del(&ctrl->list);
2217 mutex_unlock(&nvme_tcp_ctrl_mutex);
2219 nvmf_free_options(nctrl->opts);
2221 kfree(ctrl->queues);
2225 static void nvme_tcp_set_sg_null(struct nvme_command *c)
2227 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2231 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2232 NVME_SGL_FMT_TRANSPORT_A;
2235 static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
2236 struct nvme_command *c, u32 data_len)
2238 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2240 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
2241 sg->length = cpu_to_le32(data_len);
2242 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
2245 static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
2248 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2251 sg->length = cpu_to_le32(data_len);
2252 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2253 NVME_SGL_FMT_TRANSPORT_A;
2256 static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
2258 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
2259 struct nvme_tcp_queue *queue = &ctrl->queues[0];
2260 struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
2261 struct nvme_command *cmd = &pdu->cmd;
2262 u8 hdgst = nvme_tcp_hdgst_len(queue);
2264 memset(pdu, 0, sizeof(*pdu));
2265 pdu->hdr.type = nvme_tcp_cmd;
2266 if (queue->hdr_digest)
2267 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2268 pdu->hdr.hlen = sizeof(*pdu);
2269 pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
2271 cmd->common.opcode = nvme_admin_async_event;
2272 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
2273 cmd->common.flags |= NVME_CMD_SGL_METABUF;
2274 nvme_tcp_set_sg_null(cmd);
2276 ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
2277 ctrl->async_req.offset = 0;
2278 ctrl->async_req.curr_bio = NULL;
2279 ctrl->async_req.data_len = 0;
2281 nvme_tcp_queue_request(&ctrl->async_req, true, true);
2284 static void nvme_tcp_complete_timed_out(struct request *rq)
2286 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2287 struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
2289 nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
2290 if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
2291 nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD;
2292 blk_mq_complete_request(rq);
2296 static enum blk_eh_timer_return
2297 nvme_tcp_timeout(struct request *rq, bool reserved)
2299 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2300 struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
2301 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2303 dev_warn(ctrl->device,
2304 "queue %d: timeout request %#x type %d\n",
2305 nvme_tcp_queue_id(req->queue), rq->tag, pdu->hdr.type);
2307 if (ctrl->state != NVME_CTRL_LIVE) {
2309 * If we are resetting, connecting or deleting we should
2310 * complete immediately because we may block controller
2311 * teardown or setup sequence
2312 * - ctrl disable/shutdown fabrics requests
2313 * - connect requests
2314 * - initialization admin requests
2315 * - I/O requests that entered after unquiescing and
2316 * the controller stopped responding
2318 * All other requests should be cancelled by the error
2319 * recovery work, so it's fine that we fail it here.
2321 nvme_tcp_complete_timed_out(rq);
2326 * LIVE state should trigger the normal error recovery which will
2327 * handle completing this request.
2329 nvme_tcp_error_recovery(ctrl);
2330 return BLK_EH_RESET_TIMER;
2333 static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
2336 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2337 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2338 struct nvme_command *c = &pdu->cmd;
2340 c->common.flags |= NVME_CMD_SGL_METABUF;
2342 if (!blk_rq_nr_phys_segments(rq))
2343 nvme_tcp_set_sg_null(c);
2344 else if (rq_data_dir(rq) == WRITE &&
2345 req->data_len <= nvme_tcp_inline_data_size(queue))
2346 nvme_tcp_set_sg_inline(queue, c, req->data_len);
2348 nvme_tcp_set_sg_host_data(c, req->data_len);
2353 static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
2356 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2357 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2358 struct nvme_tcp_queue *queue = req->queue;
2359 u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
2362 ret = nvme_setup_cmd(ns, rq);
2366 req->state = NVME_TCP_SEND_CMD_PDU;
2367 req->status = cpu_to_le16(NVME_SC_SUCCESS);
2372 req->data_len = blk_rq_nr_phys_segments(rq) ?
2373 blk_rq_payload_bytes(rq) : 0;
2374 req->curr_bio = rq->bio;
2375 if (req->curr_bio && req->data_len)
2376 nvme_tcp_init_iter(req, rq_data_dir(rq));
2378 if (rq_data_dir(rq) == WRITE &&
2379 req->data_len <= nvme_tcp_inline_data_size(queue))
2380 req->pdu_len = req->data_len;
2382 pdu->hdr.type = nvme_tcp_cmd;
2384 if (queue->hdr_digest)
2385 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2386 if (queue->data_digest && req->pdu_len) {
2387 pdu->hdr.flags |= NVME_TCP_F_DDGST;
2388 ddgst = nvme_tcp_ddgst_len(queue);
2390 pdu->hdr.hlen = sizeof(*pdu);
2391 pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
2393 cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
2395 ret = nvme_tcp_map_data(queue, rq);
2396 if (unlikely(ret)) {
2397 nvme_cleanup_cmd(rq);
2398 dev_err(queue->ctrl->ctrl.device,
2399 "Failed to map data (%d)\n", ret);
2406 static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx)
2408 struct nvme_tcp_queue *queue = hctx->driver_data;
2410 if (!llist_empty(&queue->req_list))
2411 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
2414 static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
2415 const struct blk_mq_queue_data *bd)
2417 struct nvme_ns *ns = hctx->queue->queuedata;
2418 struct nvme_tcp_queue *queue = hctx->driver_data;
2419 struct request *rq = bd->rq;
2420 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2421 bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
2424 if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2425 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
2427 ret = nvme_tcp_setup_cmd_pdu(ns, rq);
2431 blk_mq_start_request(rq);
2433 nvme_tcp_queue_request(req, true, bd->last);
2438 static int nvme_tcp_map_queues(struct blk_mq_tag_set *set)
2440 struct nvme_tcp_ctrl *ctrl = set->driver_data;
2441 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2443 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
2444 /* separate read/write queues */
2445 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2446 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2447 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2448 set->map[HCTX_TYPE_READ].nr_queues =
2449 ctrl->io_queues[HCTX_TYPE_READ];
2450 set->map[HCTX_TYPE_READ].queue_offset =
2451 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2453 /* shared read/write queues */
2454 set->map[HCTX_TYPE_DEFAULT].nr_queues =
2455 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2456 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2457 set->map[HCTX_TYPE_READ].nr_queues =
2458 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2459 set->map[HCTX_TYPE_READ].queue_offset = 0;
2461 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
2462 blk_mq_map_queues(&set->map[HCTX_TYPE_READ]);
2464 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
2465 /* map dedicated poll queues only if we have queues left */
2466 set->map[HCTX_TYPE_POLL].nr_queues =
2467 ctrl->io_queues[HCTX_TYPE_POLL];
2468 set->map[HCTX_TYPE_POLL].queue_offset =
2469 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
2470 ctrl->io_queues[HCTX_TYPE_READ];
2471 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
2474 dev_info(ctrl->ctrl.device,
2475 "mapped %d/%d/%d default/read/poll queues.\n",
2476 ctrl->io_queues[HCTX_TYPE_DEFAULT],
2477 ctrl->io_queues[HCTX_TYPE_READ],
2478 ctrl->io_queues[HCTX_TYPE_POLL]);
2483 static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx)
2485 struct nvme_tcp_queue *queue = hctx->driver_data;
2486 struct sock *sk = queue->sock->sk;
2488 if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2491 set_bit(NVME_TCP_Q_POLLING, &queue->flags);
2492 if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue))
2493 sk_busy_loop(sk, true);
2494 nvme_tcp_try_recv(queue);
2495 clear_bit(NVME_TCP_Q_POLLING, &queue->flags);
2496 return queue->nr_cqe;
2499 static const struct blk_mq_ops nvme_tcp_mq_ops = {
2500 .queue_rq = nvme_tcp_queue_rq,
2501 .commit_rqs = nvme_tcp_commit_rqs,
2502 .complete = nvme_complete_rq,
2503 .init_request = nvme_tcp_init_request,
2504 .exit_request = nvme_tcp_exit_request,
2505 .init_hctx = nvme_tcp_init_hctx,
2506 .timeout = nvme_tcp_timeout,
2507 .map_queues = nvme_tcp_map_queues,
2508 .poll = nvme_tcp_poll,
2511 static const struct blk_mq_ops nvme_tcp_admin_mq_ops = {
2512 .queue_rq = nvme_tcp_queue_rq,
2513 .complete = nvme_complete_rq,
2514 .init_request = nvme_tcp_init_request,
2515 .exit_request = nvme_tcp_exit_request,
2516 .init_hctx = nvme_tcp_init_admin_hctx,
2517 .timeout = nvme_tcp_timeout,
2520 static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
2522 .module = THIS_MODULE,
2523 .flags = NVME_F_FABRICS,
2524 .reg_read32 = nvmf_reg_read32,
2525 .reg_read64 = nvmf_reg_read64,
2526 .reg_write32 = nvmf_reg_write32,
2527 .free_ctrl = nvme_tcp_free_ctrl,
2528 .submit_async_event = nvme_tcp_submit_async_event,
2529 .delete_ctrl = nvme_tcp_delete_ctrl,
2530 .get_address = nvmf_get_address,
2531 .stop_ctrl = nvme_tcp_stop_ctrl,
2535 nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
2537 struct nvme_tcp_ctrl *ctrl;
2540 mutex_lock(&nvme_tcp_ctrl_mutex);
2541 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
2542 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2546 mutex_unlock(&nvme_tcp_ctrl_mutex);
2551 static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
2552 struct nvmf_ctrl_options *opts)
2554 struct nvme_tcp_ctrl *ctrl;
2557 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2559 return ERR_PTR(-ENOMEM);
2561 INIT_LIST_HEAD(&ctrl->list);
2562 ctrl->ctrl.opts = opts;
2563 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2564 opts->nr_poll_queues + 1;
2565 ctrl->ctrl.sqsize = opts->queue_size - 1;
2566 ctrl->ctrl.kato = opts->kato;
2568 INIT_DELAYED_WORK(&ctrl->connect_work,
2569 nvme_tcp_reconnect_ctrl_work);
2570 INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
2571 INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
2573 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2575 kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
2576 if (!opts->trsvcid) {
2580 opts->mask |= NVMF_OPT_TRSVCID;
2583 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2584 opts->traddr, opts->trsvcid, &ctrl->addr);
2586 pr_err("malformed address passed: %s:%s\n",
2587 opts->traddr, opts->trsvcid);
2591 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2592 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2593 opts->host_traddr, NULL, &ctrl->src_addr);
2595 pr_err("malformed src address passed: %s\n",
2601 if (opts->mask & NVMF_OPT_HOST_IFACE) {
2602 if (!__dev_get_by_name(&init_net, opts->host_iface)) {
2603 pr_err("invalid interface passed: %s\n",
2610 if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
2615 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
2617 if (!ctrl->queues) {
2622 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
2624 goto out_kfree_queues;
2626 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2629 goto out_uninit_ctrl;
2632 ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
2634 goto out_uninit_ctrl;
2636 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
2637 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2639 mutex_lock(&nvme_tcp_ctrl_mutex);
2640 list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
2641 mutex_unlock(&nvme_tcp_ctrl_mutex);
2646 nvme_uninit_ctrl(&ctrl->ctrl);
2647 nvme_put_ctrl(&ctrl->ctrl);
2650 return ERR_PTR(ret);
2652 kfree(ctrl->queues);
2655 return ERR_PTR(ret);
2658 static struct nvmf_transport_ops nvme_tcp_transport = {
2660 .module = THIS_MODULE,
2661 .required_opts = NVMF_OPT_TRADDR,
2662 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2663 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2664 NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
2665 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2666 NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE,
2667 .create_ctrl = nvme_tcp_create_ctrl,
2670 static int __init nvme_tcp_init_module(void)
2672 nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq",
2673 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2677 nvmf_register_transport(&nvme_tcp_transport);
2681 static void __exit nvme_tcp_cleanup_module(void)
2683 struct nvme_tcp_ctrl *ctrl;
2685 nvmf_unregister_transport(&nvme_tcp_transport);
2687 mutex_lock(&nvme_tcp_ctrl_mutex);
2688 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
2689 nvme_delete_ctrl(&ctrl->ctrl);
2690 mutex_unlock(&nvme_tcp_ctrl_mutex);
2691 flush_workqueue(nvme_delete_wq);
2693 destroy_workqueue(nvme_tcp_wq);
2696 module_init(nvme_tcp_init_module);
2697 module_exit(nvme_tcp_cleanup_module);
2699 MODULE_LICENSE("GPL v2");