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 enum nvme_tcp_send_state
{
34 NVME_TCP_SEND_CMD_PDU
= 0,
35 NVME_TCP_SEND_H2C_PDU
,
40 struct nvme_tcp_request
{
41 struct nvme_request req
;
43 struct nvme_tcp_queue
*queue
;
48 struct list_head entry
;
49 struct llist_node lentry
;
58 enum nvme_tcp_send_state state
;
61 enum nvme_tcp_queue_flags
{
62 NVME_TCP_Q_ALLOCATED
= 0,
64 NVME_TCP_Q_POLLING
= 2,
67 enum nvme_tcp_recv_state
{
68 NVME_TCP_RECV_PDU
= 0,
74 struct nvme_tcp_queue
{
76 struct work_struct io_work
;
79 struct mutex send_mutex
;
80 struct llist_head req_list
;
81 struct list_head send_list
;
88 size_t data_remaining
;
89 size_t ddgst_remaining
;
93 struct nvme_tcp_request
*request
;
96 size_t cmnd_capsule_len
;
97 struct nvme_tcp_ctrl
*ctrl
;
103 struct ahash_request
*rcv_hash
;
104 struct ahash_request
*snd_hash
;
108 struct page_frag_cache pf_cache
;
110 void (*state_change
)(struct sock
*);
111 void (*data_ready
)(struct sock
*);
112 void (*write_space
)(struct sock
*);
115 struct nvme_tcp_ctrl
{
116 /* read only in the hot path */
117 struct nvme_tcp_queue
*queues
;
118 struct blk_mq_tag_set tag_set
;
120 /* other member variables */
121 struct list_head list
;
122 struct blk_mq_tag_set admin_tag_set
;
123 struct sockaddr_storage addr
;
124 struct sockaddr_storage src_addr
;
125 struct nvme_ctrl ctrl
;
127 struct work_struct err_work
;
128 struct delayed_work connect_work
;
129 struct nvme_tcp_request async_req
;
130 u32 io_queues
[HCTX_MAX_TYPES
];
133 static LIST_HEAD(nvme_tcp_ctrl_list
);
134 static DEFINE_MUTEX(nvme_tcp_ctrl_mutex
);
135 static struct workqueue_struct
*nvme_tcp_wq
;
136 static const struct blk_mq_ops nvme_tcp_mq_ops
;
137 static const struct blk_mq_ops nvme_tcp_admin_mq_ops
;
138 static int nvme_tcp_try_send(struct nvme_tcp_queue
*queue
);
140 static inline struct nvme_tcp_ctrl
*to_tcp_ctrl(struct nvme_ctrl
*ctrl
)
142 return container_of(ctrl
, struct nvme_tcp_ctrl
, ctrl
);
145 static inline int nvme_tcp_queue_id(struct nvme_tcp_queue
*queue
)
147 return queue
- queue
->ctrl
->queues
;
150 static inline struct blk_mq_tags
*nvme_tcp_tagset(struct nvme_tcp_queue
*queue
)
152 u32 queue_idx
= nvme_tcp_queue_id(queue
);
155 return queue
->ctrl
->admin_tag_set
.tags
[queue_idx
];
156 return queue
->ctrl
->tag_set
.tags
[queue_idx
- 1];
159 static inline u8
nvme_tcp_hdgst_len(struct nvme_tcp_queue
*queue
)
161 return queue
->hdr_digest
? NVME_TCP_DIGEST_LENGTH
: 0;
164 static inline u8
nvme_tcp_ddgst_len(struct nvme_tcp_queue
*queue
)
166 return queue
->data_digest
? NVME_TCP_DIGEST_LENGTH
: 0;
169 static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_queue
*queue
)
171 return queue
->cmnd_capsule_len
- sizeof(struct nvme_command
);
174 static inline bool nvme_tcp_async_req(struct nvme_tcp_request
*req
)
176 return req
== &req
->queue
->ctrl
->async_req
;
179 static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request
*req
)
183 if (unlikely(nvme_tcp_async_req(req
)))
184 return false; /* async events don't have a request */
186 rq
= blk_mq_rq_from_pdu(req
);
188 return rq_data_dir(rq
) == WRITE
&& req
->data_len
&&
189 req
->data_len
<= nvme_tcp_inline_data_size(req
->queue
);
192 static inline struct page
*nvme_tcp_req_cur_page(struct nvme_tcp_request
*req
)
194 return req
->iter
.bvec
->bv_page
;
197 static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request
*req
)
199 return req
->iter
.bvec
->bv_offset
+ req
->iter
.iov_offset
;
202 static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request
*req
)
204 return min_t(size_t, req
->iter
.bvec
->bv_len
- req
->iter
.iov_offset
,
205 req
->pdu_len
- req
->pdu_sent
);
208 static inline size_t nvme_tcp_req_offset(struct nvme_tcp_request
*req
)
210 return req
->iter
.iov_offset
;
213 static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request
*req
)
215 return rq_data_dir(blk_mq_rq_from_pdu(req
)) == WRITE
?
216 req
->pdu_len
- req
->pdu_sent
: 0;
219 static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request
*req
,
222 return nvme_tcp_pdu_data_left(req
) <= len
;
225 static void nvme_tcp_init_iter(struct nvme_tcp_request
*req
,
228 struct request
*rq
= blk_mq_rq_from_pdu(req
);
234 if (rq
->rq_flags
& RQF_SPECIAL_PAYLOAD
) {
235 vec
= &rq
->special_vec
;
237 size
= blk_rq_payload_bytes(rq
);
240 struct bio
*bio
= req
->curr_bio
;
242 vec
= __bvec_iter_bvec(bio
->bi_io_vec
, bio
->bi_iter
);
243 nsegs
= bio_segments(bio
);
244 size
= bio
->bi_iter
.bi_size
;
245 offset
= bio
->bi_iter
.bi_bvec_done
;
248 iov_iter_bvec(&req
->iter
, dir
, vec
, nsegs
, size
);
249 req
->iter
.iov_offset
= offset
;
252 static inline void nvme_tcp_advance_req(struct nvme_tcp_request
*req
,
255 req
->data_sent
+= len
;
256 req
->pdu_sent
+= len
;
257 iov_iter_advance(&req
->iter
, len
);
258 if (!iov_iter_count(&req
->iter
) &&
259 req
->data_sent
< req
->data_len
) {
260 req
->curr_bio
= req
->curr_bio
->bi_next
;
261 nvme_tcp_init_iter(req
, WRITE
);
265 static inline void nvme_tcp_send_all(struct nvme_tcp_queue
*queue
)
269 /* drain the send queue as much as we can... */
271 ret
= nvme_tcp_try_send(queue
);
275 static inline void nvme_tcp_queue_request(struct nvme_tcp_request
*req
,
276 bool sync
, bool last
)
278 struct nvme_tcp_queue
*queue
= req
->queue
;
281 empty
= llist_add(&req
->lentry
, &queue
->req_list
) &&
282 list_empty(&queue
->send_list
) && !queue
->request
;
285 * if we're the first on the send_list and we can try to send
286 * directly, otherwise queue io_work. Also, only do that if we
287 * are on the same cpu, so we don't introduce contention.
289 if (queue
->io_cpu
== smp_processor_id() &&
290 sync
&& empty
&& mutex_trylock(&queue
->send_mutex
)) {
291 queue
->more_requests
= !last
;
292 nvme_tcp_send_all(queue
);
293 queue
->more_requests
= false;
294 mutex_unlock(&queue
->send_mutex
);
296 queue_work_on(queue
->io_cpu
, nvme_tcp_wq
, &queue
->io_work
);
300 static void nvme_tcp_process_req_list(struct nvme_tcp_queue
*queue
)
302 struct nvme_tcp_request
*req
;
303 struct llist_node
*node
;
305 for (node
= llist_del_all(&queue
->req_list
); node
; node
= node
->next
) {
306 req
= llist_entry(node
, struct nvme_tcp_request
, lentry
);
307 list_add(&req
->entry
, &queue
->send_list
);
311 static inline struct nvme_tcp_request
*
312 nvme_tcp_fetch_request(struct nvme_tcp_queue
*queue
)
314 struct nvme_tcp_request
*req
;
316 req
= list_first_entry_or_null(&queue
->send_list
,
317 struct nvme_tcp_request
, entry
);
319 nvme_tcp_process_req_list(queue
);
320 req
= list_first_entry_or_null(&queue
->send_list
,
321 struct nvme_tcp_request
, entry
);
326 list_del(&req
->entry
);
330 static inline void nvme_tcp_ddgst_final(struct ahash_request
*hash
,
333 ahash_request_set_crypt(hash
, NULL
, (u8
*)dgst
, 0);
334 crypto_ahash_final(hash
);
337 static inline void nvme_tcp_ddgst_update(struct ahash_request
*hash
,
338 struct page
*page
, off_t off
, size_t len
)
340 struct scatterlist sg
;
342 sg_init_marker(&sg
, 1);
343 sg_set_page(&sg
, page
, len
, off
);
344 ahash_request_set_crypt(hash
, &sg
, NULL
, len
);
345 crypto_ahash_update(hash
);
348 static inline void nvme_tcp_hdgst(struct ahash_request
*hash
,
349 void *pdu
, size_t len
)
351 struct scatterlist sg
;
353 sg_init_one(&sg
, pdu
, len
);
354 ahash_request_set_crypt(hash
, &sg
, pdu
+ len
, len
);
355 crypto_ahash_digest(hash
);
358 static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue
*queue
,
359 void *pdu
, size_t pdu_len
)
361 struct nvme_tcp_hdr
*hdr
= pdu
;
365 if (unlikely(!(hdr
->flags
& NVME_TCP_F_HDGST
))) {
366 dev_err(queue
->ctrl
->ctrl
.device
,
367 "queue %d: header digest flag is cleared\n",
368 nvme_tcp_queue_id(queue
));
372 recv_digest
= *(__le32
*)(pdu
+ hdr
->hlen
);
373 nvme_tcp_hdgst(queue
->rcv_hash
, pdu
, pdu_len
);
374 exp_digest
= *(__le32
*)(pdu
+ hdr
->hlen
);
375 if (recv_digest
!= exp_digest
) {
376 dev_err(queue
->ctrl
->ctrl
.device
,
377 "header digest error: recv %#x expected %#x\n",
378 le32_to_cpu(recv_digest
), le32_to_cpu(exp_digest
));
385 static int nvme_tcp_check_ddgst(struct nvme_tcp_queue
*queue
, void *pdu
)
387 struct nvme_tcp_hdr
*hdr
= pdu
;
388 u8 digest_len
= nvme_tcp_hdgst_len(queue
);
391 len
= le32_to_cpu(hdr
->plen
) - hdr
->hlen
-
392 ((hdr
->flags
& NVME_TCP_F_HDGST
) ? digest_len
: 0);
394 if (unlikely(len
&& !(hdr
->flags
& NVME_TCP_F_DDGST
))) {
395 dev_err(queue
->ctrl
->ctrl
.device
,
396 "queue %d: data digest flag is cleared\n",
397 nvme_tcp_queue_id(queue
));
400 crypto_ahash_init(queue
->rcv_hash
);
405 static void nvme_tcp_exit_request(struct blk_mq_tag_set
*set
,
406 struct request
*rq
, unsigned int hctx_idx
)
408 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
410 page_frag_free(req
->pdu
);
413 static int nvme_tcp_init_request(struct blk_mq_tag_set
*set
,
414 struct request
*rq
, unsigned int hctx_idx
,
415 unsigned int numa_node
)
417 struct nvme_tcp_ctrl
*ctrl
= set
->driver_data
;
418 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
419 int queue_idx
= (set
== &ctrl
->tag_set
) ? hctx_idx
+ 1 : 0;
420 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[queue_idx
];
421 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
423 req
->pdu
= page_frag_alloc(&queue
->pf_cache
,
424 sizeof(struct nvme_tcp_cmd_pdu
) + hdgst
,
425 GFP_KERNEL
| __GFP_ZERO
);
430 nvme_req(rq
)->ctrl
= &ctrl
->ctrl
;
435 static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
436 unsigned int hctx_idx
)
438 struct nvme_tcp_ctrl
*ctrl
= data
;
439 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[hctx_idx
+ 1];
441 hctx
->driver_data
= queue
;
445 static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
446 unsigned int hctx_idx
)
448 struct nvme_tcp_ctrl
*ctrl
= data
;
449 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[0];
451 hctx
->driver_data
= queue
;
455 static enum nvme_tcp_recv_state
456 nvme_tcp_recv_state(struct nvme_tcp_queue
*queue
)
458 return (queue
->pdu_remaining
) ? NVME_TCP_RECV_PDU
:
459 (queue
->ddgst_remaining
) ? NVME_TCP_RECV_DDGST
:
463 static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue
*queue
)
465 queue
->pdu_remaining
= sizeof(struct nvme_tcp_rsp_pdu
) +
466 nvme_tcp_hdgst_len(queue
);
467 queue
->pdu_offset
= 0;
468 queue
->data_remaining
= -1;
469 queue
->ddgst_remaining
= 0;
472 static void nvme_tcp_error_recovery(struct nvme_ctrl
*ctrl
)
474 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_RESETTING
))
477 dev_warn(ctrl
->device
, "starting error recovery\n");
478 queue_work(nvme_reset_wq
, &to_tcp_ctrl(ctrl
)->err_work
);
481 static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue
*queue
,
482 struct nvme_completion
*cqe
)
486 rq
= blk_mq_tag_to_rq(nvme_tcp_tagset(queue
), cqe
->command_id
);
488 dev_err(queue
->ctrl
->ctrl
.device
,
489 "queue %d tag 0x%x not found\n",
490 nvme_tcp_queue_id(queue
), cqe
->command_id
);
491 nvme_tcp_error_recovery(&queue
->ctrl
->ctrl
);
495 if (!nvme_try_complete_req(rq
, cqe
->status
, cqe
->result
))
496 nvme_complete_rq(rq
);
502 static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue
*queue
,
503 struct nvme_tcp_data_pdu
*pdu
)
507 rq
= blk_mq_tag_to_rq(nvme_tcp_tagset(queue
), pdu
->command_id
);
509 dev_err(queue
->ctrl
->ctrl
.device
,
510 "queue %d tag %#x not found\n",
511 nvme_tcp_queue_id(queue
), pdu
->command_id
);
515 if (!blk_rq_payload_bytes(rq
)) {
516 dev_err(queue
->ctrl
->ctrl
.device
,
517 "queue %d tag %#x unexpected data\n",
518 nvme_tcp_queue_id(queue
), rq
->tag
);
522 queue
->data_remaining
= le32_to_cpu(pdu
->data_length
);
524 if (pdu
->hdr
.flags
& NVME_TCP_F_DATA_SUCCESS
&&
525 unlikely(!(pdu
->hdr
.flags
& NVME_TCP_F_DATA_LAST
))) {
526 dev_err(queue
->ctrl
->ctrl
.device
,
527 "queue %d tag %#x SUCCESS set but not last PDU\n",
528 nvme_tcp_queue_id(queue
), rq
->tag
);
529 nvme_tcp_error_recovery(&queue
->ctrl
->ctrl
);
536 static int nvme_tcp_handle_comp(struct nvme_tcp_queue
*queue
,
537 struct nvme_tcp_rsp_pdu
*pdu
)
539 struct nvme_completion
*cqe
= &pdu
->cqe
;
543 * AEN requests are special as they don't time out and can
544 * survive any kind of queue freeze and often don't respond to
545 * aborts. We don't even bother to allocate a struct request
546 * for them but rather special case them here.
548 if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue
),
550 nvme_complete_async_event(&queue
->ctrl
->ctrl
, cqe
->status
,
553 ret
= nvme_tcp_process_nvme_cqe(queue
, cqe
);
558 static int nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request
*req
,
559 struct nvme_tcp_r2t_pdu
*pdu
)
561 struct nvme_tcp_data_pdu
*data
= req
->pdu
;
562 struct nvme_tcp_queue
*queue
= req
->queue
;
563 struct request
*rq
= blk_mq_rq_from_pdu(req
);
564 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
565 u8 ddgst
= nvme_tcp_ddgst_len(queue
);
567 req
->pdu_len
= le32_to_cpu(pdu
->r2t_length
);
570 if (unlikely(req
->data_sent
+ req
->pdu_len
> req
->data_len
)) {
571 dev_err(queue
->ctrl
->ctrl
.device
,
572 "req %d r2t len %u exceeded data len %u (%zu sent)\n",
573 rq
->tag
, req
->pdu_len
, req
->data_len
,
578 if (unlikely(le32_to_cpu(pdu
->r2t_offset
) < req
->data_sent
)) {
579 dev_err(queue
->ctrl
->ctrl
.device
,
580 "req %d unexpected r2t offset %u (expected %zu)\n",
581 rq
->tag
, le32_to_cpu(pdu
->r2t_offset
),
586 memset(data
, 0, sizeof(*data
));
587 data
->hdr
.type
= nvme_tcp_h2c_data
;
588 data
->hdr
.flags
= NVME_TCP_F_DATA_LAST
;
589 if (queue
->hdr_digest
)
590 data
->hdr
.flags
|= NVME_TCP_F_HDGST
;
591 if (queue
->data_digest
)
592 data
->hdr
.flags
|= NVME_TCP_F_DDGST
;
593 data
->hdr
.hlen
= sizeof(*data
);
594 data
->hdr
.pdo
= data
->hdr
.hlen
+ hdgst
;
596 cpu_to_le32(data
->hdr
.hlen
+ hdgst
+ req
->pdu_len
+ ddgst
);
597 data
->ttag
= pdu
->ttag
;
598 data
->command_id
= rq
->tag
;
599 data
->data_offset
= cpu_to_le32(req
->data_sent
);
600 data
->data_length
= cpu_to_le32(req
->pdu_len
);
604 static int nvme_tcp_handle_r2t(struct nvme_tcp_queue
*queue
,
605 struct nvme_tcp_r2t_pdu
*pdu
)
607 struct nvme_tcp_request
*req
;
611 rq
= blk_mq_tag_to_rq(nvme_tcp_tagset(queue
), pdu
->command_id
);
613 dev_err(queue
->ctrl
->ctrl
.device
,
614 "queue %d tag %#x not found\n",
615 nvme_tcp_queue_id(queue
), pdu
->command_id
);
618 req
= blk_mq_rq_to_pdu(rq
);
620 ret
= nvme_tcp_setup_h2c_data_pdu(req
, pdu
);
624 req
->state
= NVME_TCP_SEND_H2C_PDU
;
627 nvme_tcp_queue_request(req
, false, true);
632 static int nvme_tcp_recv_pdu(struct nvme_tcp_queue
*queue
, struct sk_buff
*skb
,
633 unsigned int *offset
, size_t *len
)
635 struct nvme_tcp_hdr
*hdr
;
636 char *pdu
= queue
->pdu
;
637 size_t rcv_len
= min_t(size_t, *len
, queue
->pdu_remaining
);
640 ret
= skb_copy_bits(skb
, *offset
,
641 &pdu
[queue
->pdu_offset
], rcv_len
);
645 queue
->pdu_remaining
-= rcv_len
;
646 queue
->pdu_offset
+= rcv_len
;
649 if (queue
->pdu_remaining
)
653 if (queue
->hdr_digest
) {
654 ret
= nvme_tcp_verify_hdgst(queue
, queue
->pdu
, hdr
->hlen
);
660 if (queue
->data_digest
) {
661 ret
= nvme_tcp_check_ddgst(queue
, queue
->pdu
);
667 case nvme_tcp_c2h_data
:
668 return nvme_tcp_handle_c2h_data(queue
, (void *)queue
->pdu
);
670 nvme_tcp_init_recv_ctx(queue
);
671 return nvme_tcp_handle_comp(queue
, (void *)queue
->pdu
);
673 nvme_tcp_init_recv_ctx(queue
);
674 return nvme_tcp_handle_r2t(queue
, (void *)queue
->pdu
);
676 dev_err(queue
->ctrl
->ctrl
.device
,
677 "unsupported pdu type (%d)\n", hdr
->type
);
682 static inline void nvme_tcp_end_request(struct request
*rq
, u16 status
)
684 union nvme_result res
= {};
686 if (!nvme_try_complete_req(rq
, cpu_to_le16(status
<< 1), res
))
687 nvme_complete_rq(rq
);
690 static int nvme_tcp_recv_data(struct nvme_tcp_queue
*queue
, struct sk_buff
*skb
,
691 unsigned int *offset
, size_t *len
)
693 struct nvme_tcp_data_pdu
*pdu
= (void *)queue
->pdu
;
694 struct nvme_tcp_request
*req
;
697 rq
= blk_mq_tag_to_rq(nvme_tcp_tagset(queue
), pdu
->command_id
);
699 dev_err(queue
->ctrl
->ctrl
.device
,
700 "queue %d tag %#x not found\n",
701 nvme_tcp_queue_id(queue
), pdu
->command_id
);
704 req
= blk_mq_rq_to_pdu(rq
);
709 recv_len
= min_t(size_t, *len
, queue
->data_remaining
);
713 if (!iov_iter_count(&req
->iter
)) {
714 req
->curr_bio
= req
->curr_bio
->bi_next
;
717 * If we don`t have any bios it means that controller
718 * sent more data than we requested, hence error
720 if (!req
->curr_bio
) {
721 dev_err(queue
->ctrl
->ctrl
.device
,
722 "queue %d no space in request %#x",
723 nvme_tcp_queue_id(queue
), rq
->tag
);
724 nvme_tcp_init_recv_ctx(queue
);
727 nvme_tcp_init_iter(req
, READ
);
730 /* we can read only from what is left in this bio */
731 recv_len
= min_t(size_t, recv_len
,
732 iov_iter_count(&req
->iter
));
734 if (queue
->data_digest
)
735 ret
= skb_copy_and_hash_datagram_iter(skb
, *offset
,
736 &req
->iter
, recv_len
, queue
->rcv_hash
);
738 ret
= skb_copy_datagram_iter(skb
, *offset
,
739 &req
->iter
, recv_len
);
741 dev_err(queue
->ctrl
->ctrl
.device
,
742 "queue %d failed to copy request %#x data",
743 nvme_tcp_queue_id(queue
), rq
->tag
);
749 queue
->data_remaining
-= recv_len
;
752 if (!queue
->data_remaining
) {
753 if (queue
->data_digest
) {
754 nvme_tcp_ddgst_final(queue
->rcv_hash
, &queue
->exp_ddgst
);
755 queue
->ddgst_remaining
= NVME_TCP_DIGEST_LENGTH
;
757 if (pdu
->hdr
.flags
& NVME_TCP_F_DATA_SUCCESS
) {
758 nvme_tcp_end_request(rq
, NVME_SC_SUCCESS
);
761 nvme_tcp_init_recv_ctx(queue
);
768 static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue
*queue
,
769 struct sk_buff
*skb
, unsigned int *offset
, size_t *len
)
771 struct nvme_tcp_data_pdu
*pdu
= (void *)queue
->pdu
;
772 char *ddgst
= (char *)&queue
->recv_ddgst
;
773 size_t recv_len
= min_t(size_t, *len
, queue
->ddgst_remaining
);
774 off_t off
= NVME_TCP_DIGEST_LENGTH
- queue
->ddgst_remaining
;
777 ret
= skb_copy_bits(skb
, *offset
, &ddgst
[off
], recv_len
);
781 queue
->ddgst_remaining
-= recv_len
;
784 if (queue
->ddgst_remaining
)
787 if (queue
->recv_ddgst
!= queue
->exp_ddgst
) {
788 dev_err(queue
->ctrl
->ctrl
.device
,
789 "data digest error: recv %#x expected %#x\n",
790 le32_to_cpu(queue
->recv_ddgst
),
791 le32_to_cpu(queue
->exp_ddgst
));
795 if (pdu
->hdr
.flags
& NVME_TCP_F_DATA_SUCCESS
) {
796 struct request
*rq
= blk_mq_tag_to_rq(nvme_tcp_tagset(queue
),
799 nvme_tcp_end_request(rq
, NVME_SC_SUCCESS
);
803 nvme_tcp_init_recv_ctx(queue
);
807 static int nvme_tcp_recv_skb(read_descriptor_t
*desc
, struct sk_buff
*skb
,
808 unsigned int offset
, size_t len
)
810 struct nvme_tcp_queue
*queue
= desc
->arg
.data
;
811 size_t consumed
= len
;
815 switch (nvme_tcp_recv_state(queue
)) {
816 case NVME_TCP_RECV_PDU
:
817 result
= nvme_tcp_recv_pdu(queue
, skb
, &offset
, &len
);
819 case NVME_TCP_RECV_DATA
:
820 result
= nvme_tcp_recv_data(queue
, skb
, &offset
, &len
);
822 case NVME_TCP_RECV_DDGST
:
823 result
= nvme_tcp_recv_ddgst(queue
, skb
, &offset
, &len
);
829 dev_err(queue
->ctrl
->ctrl
.device
,
830 "receive failed: %d\n", result
);
831 queue
->rd_enabled
= false;
832 nvme_tcp_error_recovery(&queue
->ctrl
->ctrl
);
840 static void nvme_tcp_data_ready(struct sock
*sk
)
842 struct nvme_tcp_queue
*queue
;
844 read_lock_bh(&sk
->sk_callback_lock
);
845 queue
= sk
->sk_user_data
;
846 if (likely(queue
&& queue
->rd_enabled
) &&
847 !test_bit(NVME_TCP_Q_POLLING
, &queue
->flags
))
848 queue_work_on(queue
->io_cpu
, nvme_tcp_wq
, &queue
->io_work
);
849 read_unlock_bh(&sk
->sk_callback_lock
);
852 static void nvme_tcp_write_space(struct sock
*sk
)
854 struct nvme_tcp_queue
*queue
;
856 read_lock_bh(&sk
->sk_callback_lock
);
857 queue
= sk
->sk_user_data
;
858 if (likely(queue
&& sk_stream_is_writeable(sk
))) {
859 clear_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
860 queue_work_on(queue
->io_cpu
, nvme_tcp_wq
, &queue
->io_work
);
862 read_unlock_bh(&sk
->sk_callback_lock
);
865 static void nvme_tcp_state_change(struct sock
*sk
)
867 struct nvme_tcp_queue
*queue
;
869 read_lock(&sk
->sk_callback_lock
);
870 queue
= sk
->sk_user_data
;
874 switch (sk
->sk_state
) {
880 nvme_tcp_error_recovery(&queue
->ctrl
->ctrl
);
883 dev_info(queue
->ctrl
->ctrl
.device
,
884 "queue %d socket state %d\n",
885 nvme_tcp_queue_id(queue
), sk
->sk_state
);
888 queue
->state_change(sk
);
890 read_unlock(&sk
->sk_callback_lock
);
893 static inline bool nvme_tcp_queue_more(struct nvme_tcp_queue
*queue
)
895 return !list_empty(&queue
->send_list
) ||
896 !llist_empty(&queue
->req_list
) || queue
->more_requests
;
899 static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue
*queue
)
901 queue
->request
= NULL
;
904 static void nvme_tcp_fail_request(struct nvme_tcp_request
*req
)
906 nvme_tcp_end_request(blk_mq_rq_from_pdu(req
), NVME_SC_HOST_PATH_ERROR
);
909 static int nvme_tcp_try_send_data(struct nvme_tcp_request
*req
)
911 struct nvme_tcp_queue
*queue
= req
->queue
;
914 struct page
*page
= nvme_tcp_req_cur_page(req
);
915 size_t offset
= nvme_tcp_req_cur_offset(req
);
916 size_t len
= nvme_tcp_req_cur_length(req
);
917 bool last
= nvme_tcp_pdu_last_send(req
, len
);
918 int ret
, flags
= MSG_DONTWAIT
;
920 if (last
&& !queue
->data_digest
&& !nvme_tcp_queue_more(queue
))
923 flags
|= MSG_MORE
| MSG_SENDPAGE_NOTLAST
;
925 if (sendpage_ok(page
)) {
926 ret
= kernel_sendpage(queue
->sock
, page
, offset
, len
,
929 ret
= sock_no_sendpage(queue
->sock
, page
, offset
, len
,
935 nvme_tcp_advance_req(req
, ret
);
936 if (queue
->data_digest
)
937 nvme_tcp_ddgst_update(queue
->snd_hash
, page
,
940 /* fully successful last write*/
941 if (last
&& ret
== len
) {
942 if (queue
->data_digest
) {
943 nvme_tcp_ddgst_final(queue
->snd_hash
,
945 req
->state
= NVME_TCP_SEND_DDGST
;
948 nvme_tcp_done_send_req(queue
);
956 static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request
*req
)
958 struct nvme_tcp_queue
*queue
= req
->queue
;
959 struct nvme_tcp_cmd_pdu
*pdu
= req
->pdu
;
960 bool inline_data
= nvme_tcp_has_inline_data(req
);
961 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
962 int len
= sizeof(*pdu
) + hdgst
- req
->offset
;
963 int flags
= MSG_DONTWAIT
;
966 if (inline_data
|| nvme_tcp_queue_more(queue
))
967 flags
|= MSG_MORE
| MSG_SENDPAGE_NOTLAST
;
971 if (queue
->hdr_digest
&& !req
->offset
)
972 nvme_tcp_hdgst(queue
->snd_hash
, pdu
, sizeof(*pdu
));
974 ret
= kernel_sendpage(queue
->sock
, virt_to_page(pdu
),
975 offset_in_page(pdu
) + req
->offset
, len
, flags
);
976 if (unlikely(ret
<= 0))
982 req
->state
= NVME_TCP_SEND_DATA
;
983 if (queue
->data_digest
)
984 crypto_ahash_init(queue
->snd_hash
);
985 nvme_tcp_init_iter(req
, WRITE
);
987 nvme_tcp_done_send_req(queue
);
996 static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request
*req
)
998 struct nvme_tcp_queue
*queue
= req
->queue
;
999 struct nvme_tcp_data_pdu
*pdu
= req
->pdu
;
1000 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
1001 int len
= sizeof(*pdu
) - req
->offset
+ hdgst
;
1004 if (queue
->hdr_digest
&& !req
->offset
)
1005 nvme_tcp_hdgst(queue
->snd_hash
, pdu
, sizeof(*pdu
));
1007 ret
= kernel_sendpage(queue
->sock
, virt_to_page(pdu
),
1008 offset_in_page(pdu
) + req
->offset
, len
,
1009 MSG_DONTWAIT
| MSG_MORE
| MSG_SENDPAGE_NOTLAST
);
1010 if (unlikely(ret
<= 0))
1015 req
->state
= NVME_TCP_SEND_DATA
;
1016 if (queue
->data_digest
)
1017 crypto_ahash_init(queue
->snd_hash
);
1018 if (!req
->data_sent
)
1019 nvme_tcp_init_iter(req
, WRITE
);
1027 static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request
*req
)
1029 struct nvme_tcp_queue
*queue
= req
->queue
;
1031 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
};
1033 .iov_base
= &req
->ddgst
+ req
->offset
,
1034 .iov_len
= NVME_TCP_DIGEST_LENGTH
- req
->offset
1037 if (nvme_tcp_queue_more(queue
))
1038 msg
.msg_flags
|= MSG_MORE
;
1040 msg
.msg_flags
|= MSG_EOR
;
1042 ret
= kernel_sendmsg(queue
->sock
, &msg
, &iov
, 1, iov
.iov_len
);
1043 if (unlikely(ret
<= 0))
1046 if (req
->offset
+ ret
== NVME_TCP_DIGEST_LENGTH
) {
1047 nvme_tcp_done_send_req(queue
);
1055 static int nvme_tcp_try_send(struct nvme_tcp_queue
*queue
)
1057 struct nvme_tcp_request
*req
;
1060 if (!queue
->request
) {
1061 queue
->request
= nvme_tcp_fetch_request(queue
);
1062 if (!queue
->request
)
1065 req
= queue
->request
;
1067 if (req
->state
== NVME_TCP_SEND_CMD_PDU
) {
1068 ret
= nvme_tcp_try_send_cmd_pdu(req
);
1071 if (!nvme_tcp_has_inline_data(req
))
1075 if (req
->state
== NVME_TCP_SEND_H2C_PDU
) {
1076 ret
= nvme_tcp_try_send_data_pdu(req
);
1081 if (req
->state
== NVME_TCP_SEND_DATA
) {
1082 ret
= nvme_tcp_try_send_data(req
);
1087 if (req
->state
== NVME_TCP_SEND_DDGST
)
1088 ret
= nvme_tcp_try_send_ddgst(req
);
1090 if (ret
== -EAGAIN
) {
1092 } else if (ret
< 0) {
1093 dev_err(queue
->ctrl
->ctrl
.device
,
1094 "failed to send request %d\n", ret
);
1095 if (ret
!= -EPIPE
&& ret
!= -ECONNRESET
)
1096 nvme_tcp_fail_request(queue
->request
);
1097 nvme_tcp_done_send_req(queue
);
1102 static int nvme_tcp_try_recv(struct nvme_tcp_queue
*queue
)
1104 struct socket
*sock
= queue
->sock
;
1105 struct sock
*sk
= sock
->sk
;
1106 read_descriptor_t rd_desc
;
1109 rd_desc
.arg
.data
= queue
;
1113 consumed
= sock
->ops
->read_sock(sk
, &rd_desc
, nvme_tcp_recv_skb
);
1118 static void nvme_tcp_io_work(struct work_struct
*w
)
1120 struct nvme_tcp_queue
*queue
=
1121 container_of(w
, struct nvme_tcp_queue
, io_work
);
1122 unsigned long deadline
= jiffies
+ msecs_to_jiffies(1);
1125 bool pending
= false;
1128 if (mutex_trylock(&queue
->send_mutex
)) {
1129 result
= nvme_tcp_try_send(queue
);
1130 mutex_unlock(&queue
->send_mutex
);
1133 else if (unlikely(result
< 0))
1137 result
= nvme_tcp_try_recv(queue
);
1140 else if (unlikely(result
< 0))
1146 } while (!time_after(jiffies
, deadline
)); /* quota is exhausted */
1148 queue_work_on(queue
->io_cpu
, nvme_tcp_wq
, &queue
->io_work
);
1151 static void nvme_tcp_free_crypto(struct nvme_tcp_queue
*queue
)
1153 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(queue
->rcv_hash
);
1155 ahash_request_free(queue
->rcv_hash
);
1156 ahash_request_free(queue
->snd_hash
);
1157 crypto_free_ahash(tfm
);
1160 static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue
*queue
)
1162 struct crypto_ahash
*tfm
;
1164 tfm
= crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC
);
1166 return PTR_ERR(tfm
);
1168 queue
->snd_hash
= ahash_request_alloc(tfm
, GFP_KERNEL
);
1169 if (!queue
->snd_hash
)
1171 ahash_request_set_callback(queue
->snd_hash
, 0, NULL
, NULL
);
1173 queue
->rcv_hash
= ahash_request_alloc(tfm
, GFP_KERNEL
);
1174 if (!queue
->rcv_hash
)
1176 ahash_request_set_callback(queue
->rcv_hash
, 0, NULL
, NULL
);
1180 ahash_request_free(queue
->snd_hash
);
1182 crypto_free_ahash(tfm
);
1186 static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl
*ctrl
)
1188 struct nvme_tcp_request
*async
= &ctrl
->async_req
;
1190 page_frag_free(async
->pdu
);
1193 static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl
*ctrl
)
1195 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[0];
1196 struct nvme_tcp_request
*async
= &ctrl
->async_req
;
1197 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
1199 async
->pdu
= page_frag_alloc(&queue
->pf_cache
,
1200 sizeof(struct nvme_tcp_cmd_pdu
) + hdgst
,
1201 GFP_KERNEL
| __GFP_ZERO
);
1205 async
->queue
= &ctrl
->queues
[0];
1209 static void nvme_tcp_free_queue(struct nvme_ctrl
*nctrl
, int qid
)
1211 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1212 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[qid
];
1214 if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED
, &queue
->flags
))
1217 if (queue
->hdr_digest
|| queue
->data_digest
)
1218 nvme_tcp_free_crypto(queue
);
1220 sock_release(queue
->sock
);
1224 static int nvme_tcp_init_connection(struct nvme_tcp_queue
*queue
)
1226 struct nvme_tcp_icreq_pdu
*icreq
;
1227 struct nvme_tcp_icresp_pdu
*icresp
;
1228 struct msghdr msg
= {};
1230 bool ctrl_hdgst
, ctrl_ddgst
;
1233 icreq
= kzalloc(sizeof(*icreq
), GFP_KERNEL
);
1237 icresp
= kzalloc(sizeof(*icresp
), GFP_KERNEL
);
1243 icreq
->hdr
.type
= nvme_tcp_icreq
;
1244 icreq
->hdr
.hlen
= sizeof(*icreq
);
1246 icreq
->hdr
.plen
= cpu_to_le32(icreq
->hdr
.hlen
);
1247 icreq
->pfv
= cpu_to_le16(NVME_TCP_PFV_1_0
);
1248 icreq
->maxr2t
= 0; /* single inflight r2t supported */
1249 icreq
->hpda
= 0; /* no alignment constraint */
1250 if (queue
->hdr_digest
)
1251 icreq
->digest
|= NVME_TCP_HDR_DIGEST_ENABLE
;
1252 if (queue
->data_digest
)
1253 icreq
->digest
|= NVME_TCP_DATA_DIGEST_ENABLE
;
1255 iov
.iov_base
= icreq
;
1256 iov
.iov_len
= sizeof(*icreq
);
1257 ret
= kernel_sendmsg(queue
->sock
, &msg
, &iov
, 1, iov
.iov_len
);
1261 memset(&msg
, 0, sizeof(msg
));
1262 iov
.iov_base
= icresp
;
1263 iov
.iov_len
= sizeof(*icresp
);
1264 ret
= kernel_recvmsg(queue
->sock
, &msg
, &iov
, 1,
1265 iov
.iov_len
, msg
.msg_flags
);
1270 if (icresp
->hdr
.type
!= nvme_tcp_icresp
) {
1271 pr_err("queue %d: bad type returned %d\n",
1272 nvme_tcp_queue_id(queue
), icresp
->hdr
.type
);
1276 if (le32_to_cpu(icresp
->hdr
.plen
) != sizeof(*icresp
)) {
1277 pr_err("queue %d: bad pdu length returned %d\n",
1278 nvme_tcp_queue_id(queue
), icresp
->hdr
.plen
);
1282 if (icresp
->pfv
!= NVME_TCP_PFV_1_0
) {
1283 pr_err("queue %d: bad pfv returned %d\n",
1284 nvme_tcp_queue_id(queue
), icresp
->pfv
);
1288 ctrl_ddgst
= !!(icresp
->digest
& NVME_TCP_DATA_DIGEST_ENABLE
);
1289 if ((queue
->data_digest
&& !ctrl_ddgst
) ||
1290 (!queue
->data_digest
&& ctrl_ddgst
)) {
1291 pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
1292 nvme_tcp_queue_id(queue
),
1293 queue
->data_digest
? "enabled" : "disabled",
1294 ctrl_ddgst
? "enabled" : "disabled");
1298 ctrl_hdgst
= !!(icresp
->digest
& NVME_TCP_HDR_DIGEST_ENABLE
);
1299 if ((queue
->hdr_digest
&& !ctrl_hdgst
) ||
1300 (!queue
->hdr_digest
&& ctrl_hdgst
)) {
1301 pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
1302 nvme_tcp_queue_id(queue
),
1303 queue
->hdr_digest
? "enabled" : "disabled",
1304 ctrl_hdgst
? "enabled" : "disabled");
1308 if (icresp
->cpda
!= 0) {
1309 pr_err("queue %d: unsupported cpda returned %d\n",
1310 nvme_tcp_queue_id(queue
), icresp
->cpda
);
1322 static bool nvme_tcp_admin_queue(struct nvme_tcp_queue
*queue
)
1324 return nvme_tcp_queue_id(queue
) == 0;
1327 static bool nvme_tcp_default_queue(struct nvme_tcp_queue
*queue
)
1329 struct nvme_tcp_ctrl
*ctrl
= queue
->ctrl
;
1330 int qid
= nvme_tcp_queue_id(queue
);
1332 return !nvme_tcp_admin_queue(queue
) &&
1333 qid
< 1 + ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
1336 static bool nvme_tcp_read_queue(struct nvme_tcp_queue
*queue
)
1338 struct nvme_tcp_ctrl
*ctrl
= queue
->ctrl
;
1339 int qid
= nvme_tcp_queue_id(queue
);
1341 return !nvme_tcp_admin_queue(queue
) &&
1342 !nvme_tcp_default_queue(queue
) &&
1343 qid
< 1 + ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] +
1344 ctrl
->io_queues
[HCTX_TYPE_READ
];
1347 static bool nvme_tcp_poll_queue(struct nvme_tcp_queue
*queue
)
1349 struct nvme_tcp_ctrl
*ctrl
= queue
->ctrl
;
1350 int qid
= nvme_tcp_queue_id(queue
);
1352 return !nvme_tcp_admin_queue(queue
) &&
1353 !nvme_tcp_default_queue(queue
) &&
1354 !nvme_tcp_read_queue(queue
) &&
1355 qid
< 1 + ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] +
1356 ctrl
->io_queues
[HCTX_TYPE_READ
] +
1357 ctrl
->io_queues
[HCTX_TYPE_POLL
];
1360 static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue
*queue
)
1362 struct nvme_tcp_ctrl
*ctrl
= queue
->ctrl
;
1363 int qid
= nvme_tcp_queue_id(queue
);
1366 if (nvme_tcp_default_queue(queue
))
1368 else if (nvme_tcp_read_queue(queue
))
1369 n
= qid
- ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] - 1;
1370 else if (nvme_tcp_poll_queue(queue
))
1371 n
= qid
- ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] -
1372 ctrl
->io_queues
[HCTX_TYPE_READ
] - 1;
1373 queue
->io_cpu
= cpumask_next_wrap(n
- 1, cpu_online_mask
, -1, false);
1376 static int nvme_tcp_alloc_queue(struct nvme_ctrl
*nctrl
,
1377 int qid
, size_t queue_size
)
1379 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1380 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[qid
];
1381 int ret
, rcv_pdu_size
;
1384 init_llist_head(&queue
->req_list
);
1385 INIT_LIST_HEAD(&queue
->send_list
);
1386 mutex_init(&queue
->send_mutex
);
1387 INIT_WORK(&queue
->io_work
, nvme_tcp_io_work
);
1388 queue
->queue_size
= queue_size
;
1391 queue
->cmnd_capsule_len
= nctrl
->ioccsz
* 16;
1393 queue
->cmnd_capsule_len
= sizeof(struct nvme_command
) +
1394 NVME_TCP_ADMIN_CCSZ
;
1396 ret
= sock_create(ctrl
->addr
.ss_family
, SOCK_STREAM
,
1397 IPPROTO_TCP
, &queue
->sock
);
1399 dev_err(nctrl
->device
,
1400 "failed to create socket: %d\n", ret
);
1404 /* Single syn retry */
1405 tcp_sock_set_syncnt(queue
->sock
->sk
, 1);
1407 /* Set TCP no delay */
1408 tcp_sock_set_nodelay(queue
->sock
->sk
);
1411 * Cleanup whatever is sitting in the TCP transmit queue on socket
1412 * close. This is done to prevent stale data from being sent should
1413 * the network connection be restored before TCP times out.
1415 sock_no_linger(queue
->sock
->sk
);
1417 if (so_priority
> 0)
1418 sock_set_priority(queue
->sock
->sk
, so_priority
);
1420 /* Set socket type of service */
1421 if (nctrl
->opts
->tos
>= 0)
1422 ip_sock_set_tos(queue
->sock
->sk
, nctrl
->opts
->tos
);
1424 /* Set 10 seconds timeout for icresp recvmsg */
1425 queue
->sock
->sk
->sk_rcvtimeo
= 10 * HZ
;
1427 queue
->sock
->sk
->sk_allocation
= GFP_ATOMIC
;
1428 nvme_tcp_set_queue_io_cpu(queue
);
1429 queue
->request
= NULL
;
1430 queue
->data_remaining
= 0;
1431 queue
->ddgst_remaining
= 0;
1432 queue
->pdu_remaining
= 0;
1433 queue
->pdu_offset
= 0;
1434 sk_set_memalloc(queue
->sock
->sk
);
1436 if (nctrl
->opts
->mask
& NVMF_OPT_HOST_TRADDR
) {
1437 ret
= kernel_bind(queue
->sock
, (struct sockaddr
*)&ctrl
->src_addr
,
1438 sizeof(ctrl
->src_addr
));
1440 dev_err(nctrl
->device
,
1441 "failed to bind queue %d socket %d\n",
1447 queue
->hdr_digest
= nctrl
->opts
->hdr_digest
;
1448 queue
->data_digest
= nctrl
->opts
->data_digest
;
1449 if (queue
->hdr_digest
|| queue
->data_digest
) {
1450 ret
= nvme_tcp_alloc_crypto(queue
);
1452 dev_err(nctrl
->device
,
1453 "failed to allocate queue %d crypto\n", qid
);
1458 rcv_pdu_size
= sizeof(struct nvme_tcp_rsp_pdu
) +
1459 nvme_tcp_hdgst_len(queue
);
1460 queue
->pdu
= kmalloc(rcv_pdu_size
, GFP_KERNEL
);
1466 dev_dbg(nctrl
->device
, "connecting queue %d\n",
1467 nvme_tcp_queue_id(queue
));
1469 ret
= kernel_connect(queue
->sock
, (struct sockaddr
*)&ctrl
->addr
,
1470 sizeof(ctrl
->addr
), 0);
1472 dev_err(nctrl
->device
,
1473 "failed to connect socket: %d\n", ret
);
1477 ret
= nvme_tcp_init_connection(queue
);
1479 goto err_init_connect
;
1481 queue
->rd_enabled
= true;
1482 set_bit(NVME_TCP_Q_ALLOCATED
, &queue
->flags
);
1483 nvme_tcp_init_recv_ctx(queue
);
1485 write_lock_bh(&queue
->sock
->sk
->sk_callback_lock
);
1486 queue
->sock
->sk
->sk_user_data
= queue
;
1487 queue
->state_change
= queue
->sock
->sk
->sk_state_change
;
1488 queue
->data_ready
= queue
->sock
->sk
->sk_data_ready
;
1489 queue
->write_space
= queue
->sock
->sk
->sk_write_space
;
1490 queue
->sock
->sk
->sk_data_ready
= nvme_tcp_data_ready
;
1491 queue
->sock
->sk
->sk_state_change
= nvme_tcp_state_change
;
1492 queue
->sock
->sk
->sk_write_space
= nvme_tcp_write_space
;
1493 #ifdef CONFIG_NET_RX_BUSY_POLL
1494 queue
->sock
->sk
->sk_ll_usec
= 1;
1496 write_unlock_bh(&queue
->sock
->sk
->sk_callback_lock
);
1501 kernel_sock_shutdown(queue
->sock
, SHUT_RDWR
);
1505 if (queue
->hdr_digest
|| queue
->data_digest
)
1506 nvme_tcp_free_crypto(queue
);
1508 sock_release(queue
->sock
);
1513 static void nvme_tcp_restore_sock_calls(struct nvme_tcp_queue
*queue
)
1515 struct socket
*sock
= queue
->sock
;
1517 write_lock_bh(&sock
->sk
->sk_callback_lock
);
1518 sock
->sk
->sk_user_data
= NULL
;
1519 sock
->sk
->sk_data_ready
= queue
->data_ready
;
1520 sock
->sk
->sk_state_change
= queue
->state_change
;
1521 sock
->sk
->sk_write_space
= queue
->write_space
;
1522 write_unlock_bh(&sock
->sk
->sk_callback_lock
);
1525 static void __nvme_tcp_stop_queue(struct nvme_tcp_queue
*queue
)
1527 kernel_sock_shutdown(queue
->sock
, SHUT_RDWR
);
1528 nvme_tcp_restore_sock_calls(queue
);
1529 cancel_work_sync(&queue
->io_work
);
1532 static void nvme_tcp_stop_queue(struct nvme_ctrl
*nctrl
, int qid
)
1534 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1535 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[qid
];
1537 if (!test_and_clear_bit(NVME_TCP_Q_LIVE
, &queue
->flags
))
1539 __nvme_tcp_stop_queue(queue
);
1542 static int nvme_tcp_start_queue(struct nvme_ctrl
*nctrl
, int idx
)
1544 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1548 ret
= nvmf_connect_io_queue(nctrl
, idx
, false);
1550 ret
= nvmf_connect_admin_queue(nctrl
);
1553 set_bit(NVME_TCP_Q_LIVE
, &ctrl
->queues
[idx
].flags
);
1555 if (test_bit(NVME_TCP_Q_ALLOCATED
, &ctrl
->queues
[idx
].flags
))
1556 __nvme_tcp_stop_queue(&ctrl
->queues
[idx
]);
1557 dev_err(nctrl
->device
,
1558 "failed to connect queue: %d ret=%d\n", idx
, ret
);
1563 static struct blk_mq_tag_set
*nvme_tcp_alloc_tagset(struct nvme_ctrl
*nctrl
,
1566 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1567 struct blk_mq_tag_set
*set
;
1571 set
= &ctrl
->admin_tag_set
;
1572 memset(set
, 0, sizeof(*set
));
1573 set
->ops
= &nvme_tcp_admin_mq_ops
;
1574 set
->queue_depth
= NVME_AQ_MQ_TAG_DEPTH
;
1575 set
->reserved_tags
= 2; /* connect + keep-alive */
1576 set
->numa_node
= nctrl
->numa_node
;
1577 set
->flags
= BLK_MQ_F_BLOCKING
;
1578 set
->cmd_size
= sizeof(struct nvme_tcp_request
);
1579 set
->driver_data
= ctrl
;
1580 set
->nr_hw_queues
= 1;
1581 set
->timeout
= NVME_ADMIN_TIMEOUT
;
1583 set
= &ctrl
->tag_set
;
1584 memset(set
, 0, sizeof(*set
));
1585 set
->ops
= &nvme_tcp_mq_ops
;
1586 set
->queue_depth
= nctrl
->sqsize
+ 1;
1587 set
->reserved_tags
= 1; /* fabric connect */
1588 set
->numa_node
= nctrl
->numa_node
;
1589 set
->flags
= BLK_MQ_F_SHOULD_MERGE
| BLK_MQ_F_BLOCKING
;
1590 set
->cmd_size
= sizeof(struct nvme_tcp_request
);
1591 set
->driver_data
= ctrl
;
1592 set
->nr_hw_queues
= nctrl
->queue_count
- 1;
1593 set
->timeout
= NVME_IO_TIMEOUT
;
1594 set
->nr_maps
= nctrl
->opts
->nr_poll_queues
? HCTX_MAX_TYPES
: 2;
1597 ret
= blk_mq_alloc_tag_set(set
);
1599 return ERR_PTR(ret
);
1604 static void nvme_tcp_free_admin_queue(struct nvme_ctrl
*ctrl
)
1606 if (to_tcp_ctrl(ctrl
)->async_req
.pdu
) {
1607 cancel_work_sync(&ctrl
->async_event_work
);
1608 nvme_tcp_free_async_req(to_tcp_ctrl(ctrl
));
1609 to_tcp_ctrl(ctrl
)->async_req
.pdu
= NULL
;
1612 nvme_tcp_free_queue(ctrl
, 0);
1615 static void nvme_tcp_free_io_queues(struct nvme_ctrl
*ctrl
)
1619 for (i
= 1; i
< ctrl
->queue_count
; i
++)
1620 nvme_tcp_free_queue(ctrl
, i
);
1623 static void nvme_tcp_stop_io_queues(struct nvme_ctrl
*ctrl
)
1627 for (i
= 1; i
< ctrl
->queue_count
; i
++)
1628 nvme_tcp_stop_queue(ctrl
, i
);
1631 static int nvme_tcp_start_io_queues(struct nvme_ctrl
*ctrl
)
1635 for (i
= 1; i
< ctrl
->queue_count
; i
++) {
1636 ret
= nvme_tcp_start_queue(ctrl
, i
);
1638 goto out_stop_queues
;
1644 for (i
--; i
>= 1; i
--)
1645 nvme_tcp_stop_queue(ctrl
, i
);
1649 static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl
*ctrl
)
1653 ret
= nvme_tcp_alloc_queue(ctrl
, 0, NVME_AQ_DEPTH
);
1657 ret
= nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl
));
1659 goto out_free_queue
;
1664 nvme_tcp_free_queue(ctrl
, 0);
1668 static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl
*ctrl
)
1672 for (i
= 1; i
< ctrl
->queue_count
; i
++) {
1673 ret
= nvme_tcp_alloc_queue(ctrl
, i
,
1676 goto out_free_queues
;
1682 for (i
--; i
>= 1; i
--)
1683 nvme_tcp_free_queue(ctrl
, i
);
1688 static unsigned int nvme_tcp_nr_io_queues(struct nvme_ctrl
*ctrl
)
1690 unsigned int nr_io_queues
;
1692 nr_io_queues
= min(ctrl
->opts
->nr_io_queues
, num_online_cpus());
1693 nr_io_queues
+= min(ctrl
->opts
->nr_write_queues
, num_online_cpus());
1694 nr_io_queues
+= min(ctrl
->opts
->nr_poll_queues
, num_online_cpus());
1696 return nr_io_queues
;
1699 static void nvme_tcp_set_io_queues(struct nvme_ctrl
*nctrl
,
1700 unsigned int nr_io_queues
)
1702 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1703 struct nvmf_ctrl_options
*opts
= nctrl
->opts
;
1705 if (opts
->nr_write_queues
&& opts
->nr_io_queues
< nr_io_queues
) {
1707 * separate read/write queues
1708 * hand out dedicated default queues only after we have
1709 * sufficient read queues.
1711 ctrl
->io_queues
[HCTX_TYPE_READ
] = opts
->nr_io_queues
;
1712 nr_io_queues
-= ctrl
->io_queues
[HCTX_TYPE_READ
];
1713 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] =
1714 min(opts
->nr_write_queues
, nr_io_queues
);
1715 nr_io_queues
-= ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
1718 * shared read/write queues
1719 * either no write queues were requested, or we don't have
1720 * sufficient queue count to have dedicated default queues.
1722 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] =
1723 min(opts
->nr_io_queues
, nr_io_queues
);
1724 nr_io_queues
-= ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
1727 if (opts
->nr_poll_queues
&& nr_io_queues
) {
1728 /* map dedicated poll queues only if we have queues left */
1729 ctrl
->io_queues
[HCTX_TYPE_POLL
] =
1730 min(opts
->nr_poll_queues
, nr_io_queues
);
1734 static int nvme_tcp_alloc_io_queues(struct nvme_ctrl
*ctrl
)
1736 unsigned int nr_io_queues
;
1739 nr_io_queues
= nvme_tcp_nr_io_queues(ctrl
);
1740 ret
= nvme_set_queue_count(ctrl
, &nr_io_queues
);
1744 ctrl
->queue_count
= nr_io_queues
+ 1;
1745 if (ctrl
->queue_count
< 2)
1748 dev_info(ctrl
->device
,
1749 "creating %d I/O queues.\n", nr_io_queues
);
1751 nvme_tcp_set_io_queues(ctrl
, nr_io_queues
);
1753 return __nvme_tcp_alloc_io_queues(ctrl
);
1756 static void nvme_tcp_destroy_io_queues(struct nvme_ctrl
*ctrl
, bool remove
)
1758 nvme_tcp_stop_io_queues(ctrl
);
1760 blk_cleanup_queue(ctrl
->connect_q
);
1761 blk_mq_free_tag_set(ctrl
->tagset
);
1763 nvme_tcp_free_io_queues(ctrl
);
1766 static int nvme_tcp_configure_io_queues(struct nvme_ctrl
*ctrl
, bool new)
1770 ret
= nvme_tcp_alloc_io_queues(ctrl
);
1775 ctrl
->tagset
= nvme_tcp_alloc_tagset(ctrl
, false);
1776 if (IS_ERR(ctrl
->tagset
)) {
1777 ret
= PTR_ERR(ctrl
->tagset
);
1778 goto out_free_io_queues
;
1781 ctrl
->connect_q
= blk_mq_init_queue(ctrl
->tagset
);
1782 if (IS_ERR(ctrl
->connect_q
)) {
1783 ret
= PTR_ERR(ctrl
->connect_q
);
1784 goto out_free_tag_set
;
1788 ret
= nvme_tcp_start_io_queues(ctrl
);
1790 goto out_cleanup_connect_q
;
1793 nvme_start_queues(ctrl
);
1794 if (!nvme_wait_freeze_timeout(ctrl
, NVME_IO_TIMEOUT
)) {
1796 * If we timed out waiting for freeze we are likely to
1797 * be stuck. Fail the controller initialization just
1801 goto out_wait_freeze_timed_out
;
1803 blk_mq_update_nr_hw_queues(ctrl
->tagset
,
1804 ctrl
->queue_count
- 1);
1805 nvme_unfreeze(ctrl
);
1810 out_wait_freeze_timed_out
:
1811 nvme_stop_queues(ctrl
);
1812 nvme_tcp_stop_io_queues(ctrl
);
1813 out_cleanup_connect_q
:
1815 blk_cleanup_queue(ctrl
->connect_q
);
1818 blk_mq_free_tag_set(ctrl
->tagset
);
1820 nvme_tcp_free_io_queues(ctrl
);
1824 static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl
*ctrl
, bool remove
)
1826 nvme_tcp_stop_queue(ctrl
, 0);
1828 blk_cleanup_queue(ctrl
->admin_q
);
1829 blk_cleanup_queue(ctrl
->fabrics_q
);
1830 blk_mq_free_tag_set(ctrl
->admin_tagset
);
1832 nvme_tcp_free_admin_queue(ctrl
);
1835 static int nvme_tcp_configure_admin_queue(struct nvme_ctrl
*ctrl
, bool new)
1839 error
= nvme_tcp_alloc_admin_queue(ctrl
);
1844 ctrl
->admin_tagset
= nvme_tcp_alloc_tagset(ctrl
, true);
1845 if (IS_ERR(ctrl
->admin_tagset
)) {
1846 error
= PTR_ERR(ctrl
->admin_tagset
);
1847 goto out_free_queue
;
1850 ctrl
->fabrics_q
= blk_mq_init_queue(ctrl
->admin_tagset
);
1851 if (IS_ERR(ctrl
->fabrics_q
)) {
1852 error
= PTR_ERR(ctrl
->fabrics_q
);
1853 goto out_free_tagset
;
1856 ctrl
->admin_q
= blk_mq_init_queue(ctrl
->admin_tagset
);
1857 if (IS_ERR(ctrl
->admin_q
)) {
1858 error
= PTR_ERR(ctrl
->admin_q
);
1859 goto out_cleanup_fabrics_q
;
1863 error
= nvme_tcp_start_queue(ctrl
, 0);
1865 goto out_cleanup_queue
;
1867 error
= nvme_enable_ctrl(ctrl
);
1869 goto out_stop_queue
;
1871 blk_mq_unquiesce_queue(ctrl
->admin_q
);
1873 error
= nvme_init_identify(ctrl
);
1875 goto out_stop_queue
;
1880 nvme_tcp_stop_queue(ctrl
, 0);
1883 blk_cleanup_queue(ctrl
->admin_q
);
1884 out_cleanup_fabrics_q
:
1886 blk_cleanup_queue(ctrl
->fabrics_q
);
1889 blk_mq_free_tag_set(ctrl
->admin_tagset
);
1891 nvme_tcp_free_admin_queue(ctrl
);
1895 static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl
*ctrl
,
1898 blk_mq_quiesce_queue(ctrl
->admin_q
);
1899 blk_sync_queue(ctrl
->admin_q
);
1900 nvme_tcp_stop_queue(ctrl
, 0);
1901 if (ctrl
->admin_tagset
) {
1902 blk_mq_tagset_busy_iter(ctrl
->admin_tagset
,
1903 nvme_cancel_request
, ctrl
);
1904 blk_mq_tagset_wait_completed_request(ctrl
->admin_tagset
);
1907 blk_mq_unquiesce_queue(ctrl
->admin_q
);
1908 nvme_tcp_destroy_admin_queue(ctrl
, remove
);
1911 static void nvme_tcp_teardown_io_queues(struct nvme_ctrl
*ctrl
,
1914 if (ctrl
->queue_count
<= 1)
1916 blk_mq_quiesce_queue(ctrl
->admin_q
);
1917 nvme_start_freeze(ctrl
);
1918 nvme_stop_queues(ctrl
);
1919 nvme_sync_io_queues(ctrl
);
1920 nvme_tcp_stop_io_queues(ctrl
);
1922 blk_mq_tagset_busy_iter(ctrl
->tagset
,
1923 nvme_cancel_request
, ctrl
);
1924 blk_mq_tagset_wait_completed_request(ctrl
->tagset
);
1927 nvme_start_queues(ctrl
);
1928 nvme_tcp_destroy_io_queues(ctrl
, remove
);
1931 static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl
*ctrl
)
1933 /* If we are resetting/deleting then do nothing */
1934 if (ctrl
->state
!= NVME_CTRL_CONNECTING
) {
1935 WARN_ON_ONCE(ctrl
->state
== NVME_CTRL_NEW
||
1936 ctrl
->state
== NVME_CTRL_LIVE
);
1940 if (nvmf_should_reconnect(ctrl
)) {
1941 dev_info(ctrl
->device
, "Reconnecting in %d seconds...\n",
1942 ctrl
->opts
->reconnect_delay
);
1943 queue_delayed_work(nvme_wq
, &to_tcp_ctrl(ctrl
)->connect_work
,
1944 ctrl
->opts
->reconnect_delay
* HZ
);
1946 dev_info(ctrl
->device
, "Removing controller...\n");
1947 nvme_delete_ctrl(ctrl
);
1951 static int nvme_tcp_setup_ctrl(struct nvme_ctrl
*ctrl
, bool new)
1953 struct nvmf_ctrl_options
*opts
= ctrl
->opts
;
1956 ret
= nvme_tcp_configure_admin_queue(ctrl
, new);
1961 dev_err(ctrl
->device
, "icdoff is not supported!\n");
1965 if (opts
->queue_size
> ctrl
->sqsize
+ 1)
1966 dev_warn(ctrl
->device
,
1967 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1968 opts
->queue_size
, ctrl
->sqsize
+ 1);
1970 if (ctrl
->sqsize
+ 1 > ctrl
->maxcmd
) {
1971 dev_warn(ctrl
->device
,
1972 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1973 ctrl
->sqsize
+ 1, ctrl
->maxcmd
);
1974 ctrl
->sqsize
= ctrl
->maxcmd
- 1;
1977 if (ctrl
->queue_count
> 1) {
1978 ret
= nvme_tcp_configure_io_queues(ctrl
, new);
1983 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_LIVE
)) {
1985 * state change failure is ok if we started ctrl delete,
1986 * unless we're during creation of a new controller to
1987 * avoid races with teardown flow.
1989 WARN_ON_ONCE(ctrl
->state
!= NVME_CTRL_DELETING
&&
1990 ctrl
->state
!= NVME_CTRL_DELETING_NOIO
);
1996 nvme_start_ctrl(ctrl
);
2000 if (ctrl
->queue_count
> 1)
2001 nvme_tcp_destroy_io_queues(ctrl
, new);
2003 nvme_tcp_stop_queue(ctrl
, 0);
2004 nvme_tcp_destroy_admin_queue(ctrl
, new);
2008 static void nvme_tcp_reconnect_ctrl_work(struct work_struct
*work
)
2010 struct nvme_tcp_ctrl
*tcp_ctrl
= container_of(to_delayed_work(work
),
2011 struct nvme_tcp_ctrl
, connect_work
);
2012 struct nvme_ctrl
*ctrl
= &tcp_ctrl
->ctrl
;
2014 ++ctrl
->nr_reconnects
;
2016 if (nvme_tcp_setup_ctrl(ctrl
, false))
2019 dev_info(ctrl
->device
, "Successfully reconnected (%d attempt)\n",
2020 ctrl
->nr_reconnects
);
2022 ctrl
->nr_reconnects
= 0;
2027 dev_info(ctrl
->device
, "Failed reconnect attempt %d\n",
2028 ctrl
->nr_reconnects
);
2029 nvme_tcp_reconnect_or_remove(ctrl
);
2032 static void nvme_tcp_error_recovery_work(struct work_struct
*work
)
2034 struct nvme_tcp_ctrl
*tcp_ctrl
= container_of(work
,
2035 struct nvme_tcp_ctrl
, err_work
);
2036 struct nvme_ctrl
*ctrl
= &tcp_ctrl
->ctrl
;
2038 nvme_stop_keep_alive(ctrl
);
2039 nvme_tcp_teardown_io_queues(ctrl
, false);
2040 /* unquiesce to fail fast pending requests */
2041 nvme_start_queues(ctrl
);
2042 nvme_tcp_teardown_admin_queue(ctrl
, false);
2043 blk_mq_unquiesce_queue(ctrl
->admin_q
);
2045 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_CONNECTING
)) {
2046 /* state change failure is ok if we started ctrl delete */
2047 WARN_ON_ONCE(ctrl
->state
!= NVME_CTRL_DELETING
&&
2048 ctrl
->state
!= NVME_CTRL_DELETING_NOIO
);
2052 nvme_tcp_reconnect_or_remove(ctrl
);
2055 static void nvme_tcp_teardown_ctrl(struct nvme_ctrl
*ctrl
, bool shutdown
)
2057 cancel_work_sync(&to_tcp_ctrl(ctrl
)->err_work
);
2058 cancel_delayed_work_sync(&to_tcp_ctrl(ctrl
)->connect_work
);
2060 nvme_tcp_teardown_io_queues(ctrl
, shutdown
);
2061 blk_mq_quiesce_queue(ctrl
->admin_q
);
2063 nvme_shutdown_ctrl(ctrl
);
2065 nvme_disable_ctrl(ctrl
);
2066 nvme_tcp_teardown_admin_queue(ctrl
, shutdown
);
2069 static void nvme_tcp_delete_ctrl(struct nvme_ctrl
*ctrl
)
2071 nvme_tcp_teardown_ctrl(ctrl
, true);
2074 static void nvme_reset_ctrl_work(struct work_struct
*work
)
2076 struct nvme_ctrl
*ctrl
=
2077 container_of(work
, struct nvme_ctrl
, reset_work
);
2079 nvme_stop_ctrl(ctrl
);
2080 nvme_tcp_teardown_ctrl(ctrl
, false);
2082 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_CONNECTING
)) {
2083 /* state change failure is ok if we started ctrl delete */
2084 WARN_ON_ONCE(ctrl
->state
!= NVME_CTRL_DELETING
&&
2085 ctrl
->state
!= NVME_CTRL_DELETING_NOIO
);
2089 if (nvme_tcp_setup_ctrl(ctrl
, false))
2095 ++ctrl
->nr_reconnects
;
2096 nvme_tcp_reconnect_or_remove(ctrl
);
2099 static void nvme_tcp_free_ctrl(struct nvme_ctrl
*nctrl
)
2101 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
2103 if (list_empty(&ctrl
->list
))
2106 mutex_lock(&nvme_tcp_ctrl_mutex
);
2107 list_del(&ctrl
->list
);
2108 mutex_unlock(&nvme_tcp_ctrl_mutex
);
2110 nvmf_free_options(nctrl
->opts
);
2112 kfree(ctrl
->queues
);
2116 static void nvme_tcp_set_sg_null(struct nvme_command
*c
)
2118 struct nvme_sgl_desc
*sg
= &c
->common
.dptr
.sgl
;
2122 sg
->type
= (NVME_TRANSPORT_SGL_DATA_DESC
<< 4) |
2123 NVME_SGL_FMT_TRANSPORT_A
;
2126 static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue
*queue
,
2127 struct nvme_command
*c
, u32 data_len
)
2129 struct nvme_sgl_desc
*sg
= &c
->common
.dptr
.sgl
;
2131 sg
->addr
= cpu_to_le64(queue
->ctrl
->ctrl
.icdoff
);
2132 sg
->length
= cpu_to_le32(data_len
);
2133 sg
->type
= (NVME_SGL_FMT_DATA_DESC
<< 4) | NVME_SGL_FMT_OFFSET
;
2136 static void nvme_tcp_set_sg_host_data(struct nvme_command
*c
,
2139 struct nvme_sgl_desc
*sg
= &c
->common
.dptr
.sgl
;
2142 sg
->length
= cpu_to_le32(data_len
);
2143 sg
->type
= (NVME_TRANSPORT_SGL_DATA_DESC
<< 4) |
2144 NVME_SGL_FMT_TRANSPORT_A
;
2147 static void nvme_tcp_submit_async_event(struct nvme_ctrl
*arg
)
2149 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(arg
);
2150 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[0];
2151 struct nvme_tcp_cmd_pdu
*pdu
= ctrl
->async_req
.pdu
;
2152 struct nvme_command
*cmd
= &pdu
->cmd
;
2153 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
2155 memset(pdu
, 0, sizeof(*pdu
));
2156 pdu
->hdr
.type
= nvme_tcp_cmd
;
2157 if (queue
->hdr_digest
)
2158 pdu
->hdr
.flags
|= NVME_TCP_F_HDGST
;
2159 pdu
->hdr
.hlen
= sizeof(*pdu
);
2160 pdu
->hdr
.plen
= cpu_to_le32(pdu
->hdr
.hlen
+ hdgst
);
2162 cmd
->common
.opcode
= nvme_admin_async_event
;
2163 cmd
->common
.command_id
= NVME_AQ_BLK_MQ_DEPTH
;
2164 cmd
->common
.flags
|= NVME_CMD_SGL_METABUF
;
2165 nvme_tcp_set_sg_null(cmd
);
2167 ctrl
->async_req
.state
= NVME_TCP_SEND_CMD_PDU
;
2168 ctrl
->async_req
.offset
= 0;
2169 ctrl
->async_req
.curr_bio
= NULL
;
2170 ctrl
->async_req
.data_len
= 0;
2172 nvme_tcp_queue_request(&ctrl
->async_req
, true, true);
2175 static void nvme_tcp_complete_timed_out(struct request
*rq
)
2177 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
2178 struct nvme_ctrl
*ctrl
= &req
->queue
->ctrl
->ctrl
;
2180 nvme_tcp_stop_queue(ctrl
, nvme_tcp_queue_id(req
->queue
));
2181 if (blk_mq_request_started(rq
) && !blk_mq_request_completed(rq
)) {
2182 nvme_req(rq
)->status
= NVME_SC_HOST_ABORTED_CMD
;
2183 blk_mq_complete_request(rq
);
2187 static enum blk_eh_timer_return
2188 nvme_tcp_timeout(struct request
*rq
, bool reserved
)
2190 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
2191 struct nvme_ctrl
*ctrl
= &req
->queue
->ctrl
->ctrl
;
2192 struct nvme_tcp_cmd_pdu
*pdu
= req
->pdu
;
2194 dev_warn(ctrl
->device
,
2195 "queue %d: timeout request %#x type %d\n",
2196 nvme_tcp_queue_id(req
->queue
), rq
->tag
, pdu
->hdr
.type
);
2198 if (ctrl
->state
!= NVME_CTRL_LIVE
) {
2200 * If we are resetting, connecting or deleting we should
2201 * complete immediately because we may block controller
2202 * teardown or setup sequence
2203 * - ctrl disable/shutdown fabrics requests
2204 * - connect requests
2205 * - initialization admin requests
2206 * - I/O requests that entered after unquiescing and
2207 * the controller stopped responding
2209 * All other requests should be cancelled by the error
2210 * recovery work, so it's fine that we fail it here.
2212 nvme_tcp_complete_timed_out(rq
);
2217 * LIVE state should trigger the normal error recovery which will
2218 * handle completing this request.
2220 nvme_tcp_error_recovery(ctrl
);
2221 return BLK_EH_RESET_TIMER
;
2224 static blk_status_t
nvme_tcp_map_data(struct nvme_tcp_queue
*queue
,
2227 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
2228 struct nvme_tcp_cmd_pdu
*pdu
= req
->pdu
;
2229 struct nvme_command
*c
= &pdu
->cmd
;
2231 c
->common
.flags
|= NVME_CMD_SGL_METABUF
;
2233 if (!blk_rq_nr_phys_segments(rq
))
2234 nvme_tcp_set_sg_null(c
);
2235 else if (rq_data_dir(rq
) == WRITE
&&
2236 req
->data_len
<= nvme_tcp_inline_data_size(queue
))
2237 nvme_tcp_set_sg_inline(queue
, c
, req
->data_len
);
2239 nvme_tcp_set_sg_host_data(c
, req
->data_len
);
2244 static blk_status_t
nvme_tcp_setup_cmd_pdu(struct nvme_ns
*ns
,
2247 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
2248 struct nvme_tcp_cmd_pdu
*pdu
= req
->pdu
;
2249 struct nvme_tcp_queue
*queue
= req
->queue
;
2250 u8 hdgst
= nvme_tcp_hdgst_len(queue
), ddgst
= 0;
2253 ret
= nvme_setup_cmd(ns
, rq
, &pdu
->cmd
);
2257 req
->state
= NVME_TCP_SEND_CMD_PDU
;
2262 req
->data_len
= blk_rq_nr_phys_segments(rq
) ?
2263 blk_rq_payload_bytes(rq
) : 0;
2264 req
->curr_bio
= rq
->bio
;
2266 if (rq_data_dir(rq
) == WRITE
&&
2267 req
->data_len
<= nvme_tcp_inline_data_size(queue
))
2268 req
->pdu_len
= req
->data_len
;
2269 else if (req
->curr_bio
)
2270 nvme_tcp_init_iter(req
, READ
);
2272 pdu
->hdr
.type
= nvme_tcp_cmd
;
2274 if (queue
->hdr_digest
)
2275 pdu
->hdr
.flags
|= NVME_TCP_F_HDGST
;
2276 if (queue
->data_digest
&& req
->pdu_len
) {
2277 pdu
->hdr
.flags
|= NVME_TCP_F_DDGST
;
2278 ddgst
= nvme_tcp_ddgst_len(queue
);
2280 pdu
->hdr
.hlen
= sizeof(*pdu
);
2281 pdu
->hdr
.pdo
= req
->pdu_len
? pdu
->hdr
.hlen
+ hdgst
: 0;
2283 cpu_to_le32(pdu
->hdr
.hlen
+ hdgst
+ req
->pdu_len
+ ddgst
);
2285 ret
= nvme_tcp_map_data(queue
, rq
);
2286 if (unlikely(ret
)) {
2287 nvme_cleanup_cmd(rq
);
2288 dev_err(queue
->ctrl
->ctrl
.device
,
2289 "Failed to map data (%d)\n", ret
);
2296 static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx
*hctx
)
2298 struct nvme_tcp_queue
*queue
= hctx
->driver_data
;
2300 if (!llist_empty(&queue
->req_list
))
2301 queue_work_on(queue
->io_cpu
, nvme_tcp_wq
, &queue
->io_work
);
2304 static blk_status_t
nvme_tcp_queue_rq(struct blk_mq_hw_ctx
*hctx
,
2305 const struct blk_mq_queue_data
*bd
)
2307 struct nvme_ns
*ns
= hctx
->queue
->queuedata
;
2308 struct nvme_tcp_queue
*queue
= hctx
->driver_data
;
2309 struct request
*rq
= bd
->rq
;
2310 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
2311 bool queue_ready
= test_bit(NVME_TCP_Q_LIVE
, &queue
->flags
);
2314 if (!nvmf_check_ready(&queue
->ctrl
->ctrl
, rq
, queue_ready
))
2315 return nvmf_fail_nonready_command(&queue
->ctrl
->ctrl
, rq
);
2317 ret
= nvme_tcp_setup_cmd_pdu(ns
, rq
);
2321 blk_mq_start_request(rq
);
2323 nvme_tcp_queue_request(req
, true, bd
->last
);
2328 static int nvme_tcp_map_queues(struct blk_mq_tag_set
*set
)
2330 struct nvme_tcp_ctrl
*ctrl
= set
->driver_data
;
2331 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
2333 if (opts
->nr_write_queues
&& ctrl
->io_queues
[HCTX_TYPE_READ
]) {
2334 /* separate read/write queues */
2335 set
->map
[HCTX_TYPE_DEFAULT
].nr_queues
=
2336 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
2337 set
->map
[HCTX_TYPE_DEFAULT
].queue_offset
= 0;
2338 set
->map
[HCTX_TYPE_READ
].nr_queues
=
2339 ctrl
->io_queues
[HCTX_TYPE_READ
];
2340 set
->map
[HCTX_TYPE_READ
].queue_offset
=
2341 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
2343 /* shared read/write queues */
2344 set
->map
[HCTX_TYPE_DEFAULT
].nr_queues
=
2345 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
2346 set
->map
[HCTX_TYPE_DEFAULT
].queue_offset
= 0;
2347 set
->map
[HCTX_TYPE_READ
].nr_queues
=
2348 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
2349 set
->map
[HCTX_TYPE_READ
].queue_offset
= 0;
2351 blk_mq_map_queues(&set
->map
[HCTX_TYPE_DEFAULT
]);
2352 blk_mq_map_queues(&set
->map
[HCTX_TYPE_READ
]);
2354 if (opts
->nr_poll_queues
&& ctrl
->io_queues
[HCTX_TYPE_POLL
]) {
2355 /* map dedicated poll queues only if we have queues left */
2356 set
->map
[HCTX_TYPE_POLL
].nr_queues
=
2357 ctrl
->io_queues
[HCTX_TYPE_POLL
];
2358 set
->map
[HCTX_TYPE_POLL
].queue_offset
=
2359 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] +
2360 ctrl
->io_queues
[HCTX_TYPE_READ
];
2361 blk_mq_map_queues(&set
->map
[HCTX_TYPE_POLL
]);
2364 dev_info(ctrl
->ctrl
.device
,
2365 "mapped %d/%d/%d default/read/poll queues.\n",
2366 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
],
2367 ctrl
->io_queues
[HCTX_TYPE_READ
],
2368 ctrl
->io_queues
[HCTX_TYPE_POLL
]);
2373 static int nvme_tcp_poll(struct blk_mq_hw_ctx
*hctx
)
2375 struct nvme_tcp_queue
*queue
= hctx
->driver_data
;
2376 struct sock
*sk
= queue
->sock
->sk
;
2378 if (!test_bit(NVME_TCP_Q_LIVE
, &queue
->flags
))
2381 set_bit(NVME_TCP_Q_POLLING
, &queue
->flags
);
2382 if (sk_can_busy_loop(sk
) && skb_queue_empty_lockless(&sk
->sk_receive_queue
))
2383 sk_busy_loop(sk
, true);
2384 nvme_tcp_try_recv(queue
);
2385 clear_bit(NVME_TCP_Q_POLLING
, &queue
->flags
);
2386 return queue
->nr_cqe
;
2389 static const struct blk_mq_ops nvme_tcp_mq_ops
= {
2390 .queue_rq
= nvme_tcp_queue_rq
,
2391 .commit_rqs
= nvme_tcp_commit_rqs
,
2392 .complete
= nvme_complete_rq
,
2393 .init_request
= nvme_tcp_init_request
,
2394 .exit_request
= nvme_tcp_exit_request
,
2395 .init_hctx
= nvme_tcp_init_hctx
,
2396 .timeout
= nvme_tcp_timeout
,
2397 .map_queues
= nvme_tcp_map_queues
,
2398 .poll
= nvme_tcp_poll
,
2401 static const struct blk_mq_ops nvme_tcp_admin_mq_ops
= {
2402 .queue_rq
= nvme_tcp_queue_rq
,
2403 .complete
= nvme_complete_rq
,
2404 .init_request
= nvme_tcp_init_request
,
2405 .exit_request
= nvme_tcp_exit_request
,
2406 .init_hctx
= nvme_tcp_init_admin_hctx
,
2407 .timeout
= nvme_tcp_timeout
,
2410 static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops
= {
2412 .module
= THIS_MODULE
,
2413 .flags
= NVME_F_FABRICS
,
2414 .reg_read32
= nvmf_reg_read32
,
2415 .reg_read64
= nvmf_reg_read64
,
2416 .reg_write32
= nvmf_reg_write32
,
2417 .free_ctrl
= nvme_tcp_free_ctrl
,
2418 .submit_async_event
= nvme_tcp_submit_async_event
,
2419 .delete_ctrl
= nvme_tcp_delete_ctrl
,
2420 .get_address
= nvmf_get_address
,
2424 nvme_tcp_existing_controller(struct nvmf_ctrl_options
*opts
)
2426 struct nvme_tcp_ctrl
*ctrl
;
2429 mutex_lock(&nvme_tcp_ctrl_mutex
);
2430 list_for_each_entry(ctrl
, &nvme_tcp_ctrl_list
, list
) {
2431 found
= nvmf_ip_options_match(&ctrl
->ctrl
, opts
);
2435 mutex_unlock(&nvme_tcp_ctrl_mutex
);
2440 static struct nvme_ctrl
*nvme_tcp_create_ctrl(struct device
*dev
,
2441 struct nvmf_ctrl_options
*opts
)
2443 struct nvme_tcp_ctrl
*ctrl
;
2446 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
2448 return ERR_PTR(-ENOMEM
);
2450 INIT_LIST_HEAD(&ctrl
->list
);
2451 ctrl
->ctrl
.opts
= opts
;
2452 ctrl
->ctrl
.queue_count
= opts
->nr_io_queues
+ opts
->nr_write_queues
+
2453 opts
->nr_poll_queues
+ 1;
2454 ctrl
->ctrl
.sqsize
= opts
->queue_size
- 1;
2455 ctrl
->ctrl
.kato
= opts
->kato
;
2457 INIT_DELAYED_WORK(&ctrl
->connect_work
,
2458 nvme_tcp_reconnect_ctrl_work
);
2459 INIT_WORK(&ctrl
->err_work
, nvme_tcp_error_recovery_work
);
2460 INIT_WORK(&ctrl
->ctrl
.reset_work
, nvme_reset_ctrl_work
);
2462 if (!(opts
->mask
& NVMF_OPT_TRSVCID
)) {
2464 kstrdup(__stringify(NVME_TCP_DISC_PORT
), GFP_KERNEL
);
2465 if (!opts
->trsvcid
) {
2469 opts
->mask
|= NVMF_OPT_TRSVCID
;
2472 ret
= inet_pton_with_scope(&init_net
, AF_UNSPEC
,
2473 opts
->traddr
, opts
->trsvcid
, &ctrl
->addr
);
2475 pr_err("malformed address passed: %s:%s\n",
2476 opts
->traddr
, opts
->trsvcid
);
2480 if (opts
->mask
& NVMF_OPT_HOST_TRADDR
) {
2481 ret
= inet_pton_with_scope(&init_net
, AF_UNSPEC
,
2482 opts
->host_traddr
, NULL
, &ctrl
->src_addr
);
2484 pr_err("malformed src address passed: %s\n",
2490 if (!opts
->duplicate_connect
&& nvme_tcp_existing_controller(opts
)) {
2495 ctrl
->queues
= kcalloc(ctrl
->ctrl
.queue_count
, sizeof(*ctrl
->queues
),
2497 if (!ctrl
->queues
) {
2502 ret
= nvme_init_ctrl(&ctrl
->ctrl
, dev
, &nvme_tcp_ctrl_ops
, 0);
2504 goto out_kfree_queues
;
2506 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_CONNECTING
)) {
2509 goto out_uninit_ctrl
;
2512 ret
= nvme_tcp_setup_ctrl(&ctrl
->ctrl
, true);
2514 goto out_uninit_ctrl
;
2516 dev_info(ctrl
->ctrl
.device
, "new ctrl: NQN \"%s\", addr %pISp\n",
2517 ctrl
->ctrl
.opts
->subsysnqn
, &ctrl
->addr
);
2519 mutex_lock(&nvme_tcp_ctrl_mutex
);
2520 list_add_tail(&ctrl
->list
, &nvme_tcp_ctrl_list
);
2521 mutex_unlock(&nvme_tcp_ctrl_mutex
);
2526 nvme_uninit_ctrl(&ctrl
->ctrl
);
2527 nvme_put_ctrl(&ctrl
->ctrl
);
2530 return ERR_PTR(ret
);
2532 kfree(ctrl
->queues
);
2535 return ERR_PTR(ret
);
2538 static struct nvmf_transport_ops nvme_tcp_transport
= {
2540 .module
= THIS_MODULE
,
2541 .required_opts
= NVMF_OPT_TRADDR
,
2542 .allowed_opts
= NVMF_OPT_TRSVCID
| NVMF_OPT_RECONNECT_DELAY
|
2543 NVMF_OPT_HOST_TRADDR
| NVMF_OPT_CTRL_LOSS_TMO
|
2544 NVMF_OPT_HDR_DIGEST
| NVMF_OPT_DATA_DIGEST
|
2545 NVMF_OPT_NR_WRITE_QUEUES
| NVMF_OPT_NR_POLL_QUEUES
|
2547 .create_ctrl
= nvme_tcp_create_ctrl
,
2550 static int __init
nvme_tcp_init_module(void)
2552 nvme_tcp_wq
= alloc_workqueue("nvme_tcp_wq",
2553 WQ_MEM_RECLAIM
| WQ_HIGHPRI
, 0);
2557 nvmf_register_transport(&nvme_tcp_transport
);
2561 static void __exit
nvme_tcp_cleanup_module(void)
2563 struct nvme_tcp_ctrl
*ctrl
;
2565 nvmf_unregister_transport(&nvme_tcp_transport
);
2567 mutex_lock(&nvme_tcp_ctrl_mutex
);
2568 list_for_each_entry(ctrl
, &nvme_tcp_ctrl_list
, list
)
2569 nvme_delete_ctrl(&ctrl
->ctrl
);
2570 mutex_unlock(&nvme_tcp_ctrl_mutex
);
2571 flush_workqueue(nvme_delete_wq
);
2573 destroy_workqueue(nvme_tcp_wq
);
2576 module_init(nvme_tcp_init_module
);
2577 module_exit(nvme_tcp_cleanup_module
);
2579 MODULE_LICENSE("GPL v2");