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
;
57 enum nvme_tcp_send_state state
;
60 enum nvme_tcp_queue_flags
{
61 NVME_TCP_Q_ALLOCATED
= 0,
65 enum nvme_tcp_recv_state
{
66 NVME_TCP_RECV_PDU
= 0,
72 struct nvme_tcp_queue
{
74 struct work_struct io_work
;
78 struct list_head send_list
;
84 size_t data_remaining
;
85 size_t ddgst_remaining
;
89 struct nvme_tcp_request
*request
;
92 size_t cmnd_capsule_len
;
93 struct nvme_tcp_ctrl
*ctrl
;
99 struct ahash_request
*rcv_hash
;
100 struct ahash_request
*snd_hash
;
104 struct page_frag_cache pf_cache
;
106 void (*state_change
)(struct sock
*);
107 void (*data_ready
)(struct sock
*);
108 void (*write_space
)(struct sock
*);
111 struct nvme_tcp_ctrl
{
112 /* read only in the hot path */
113 struct nvme_tcp_queue
*queues
;
114 struct blk_mq_tag_set tag_set
;
116 /* other member variables */
117 struct list_head list
;
118 struct blk_mq_tag_set admin_tag_set
;
119 struct sockaddr_storage addr
;
120 struct sockaddr_storage src_addr
;
121 struct nvme_ctrl ctrl
;
123 struct work_struct err_work
;
124 struct delayed_work connect_work
;
125 struct nvme_tcp_request async_req
;
126 u32 io_queues
[HCTX_MAX_TYPES
];
129 static LIST_HEAD(nvme_tcp_ctrl_list
);
130 static DEFINE_MUTEX(nvme_tcp_ctrl_mutex
);
131 static struct workqueue_struct
*nvme_tcp_wq
;
132 static struct blk_mq_ops nvme_tcp_mq_ops
;
133 static struct blk_mq_ops nvme_tcp_admin_mq_ops
;
135 static inline struct nvme_tcp_ctrl
*to_tcp_ctrl(struct nvme_ctrl
*ctrl
)
137 return container_of(ctrl
, struct nvme_tcp_ctrl
, ctrl
);
140 static inline int nvme_tcp_queue_id(struct nvme_tcp_queue
*queue
)
142 return queue
- queue
->ctrl
->queues
;
145 static inline struct blk_mq_tags
*nvme_tcp_tagset(struct nvme_tcp_queue
*queue
)
147 u32 queue_idx
= nvme_tcp_queue_id(queue
);
150 return queue
->ctrl
->admin_tag_set
.tags
[queue_idx
];
151 return queue
->ctrl
->tag_set
.tags
[queue_idx
- 1];
154 static inline u8
nvme_tcp_hdgst_len(struct nvme_tcp_queue
*queue
)
156 return queue
->hdr_digest
? NVME_TCP_DIGEST_LENGTH
: 0;
159 static inline u8
nvme_tcp_ddgst_len(struct nvme_tcp_queue
*queue
)
161 return queue
->data_digest
? NVME_TCP_DIGEST_LENGTH
: 0;
164 static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_queue
*queue
)
166 return queue
->cmnd_capsule_len
- sizeof(struct nvme_command
);
169 static inline bool nvme_tcp_async_req(struct nvme_tcp_request
*req
)
171 return req
== &req
->queue
->ctrl
->async_req
;
174 static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request
*req
)
178 if (unlikely(nvme_tcp_async_req(req
)))
179 return false; /* async events don't have a request */
181 rq
= blk_mq_rq_from_pdu(req
);
183 return rq_data_dir(rq
) == WRITE
&& req
->data_len
&&
184 req
->data_len
<= nvme_tcp_inline_data_size(req
->queue
);
187 static inline struct page
*nvme_tcp_req_cur_page(struct nvme_tcp_request
*req
)
189 return req
->iter
.bvec
->bv_page
;
192 static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request
*req
)
194 return req
->iter
.bvec
->bv_offset
+ req
->iter
.iov_offset
;
197 static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request
*req
)
199 return min_t(size_t, req
->iter
.bvec
->bv_len
- req
->iter
.iov_offset
,
200 req
->pdu_len
- req
->pdu_sent
);
203 static inline size_t nvme_tcp_req_offset(struct nvme_tcp_request
*req
)
205 return req
->iter
.iov_offset
;
208 static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request
*req
)
210 return rq_data_dir(blk_mq_rq_from_pdu(req
)) == WRITE
?
211 req
->pdu_len
- req
->pdu_sent
: 0;
214 static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request
*req
,
217 return nvme_tcp_pdu_data_left(req
) <= len
;
220 static void nvme_tcp_init_iter(struct nvme_tcp_request
*req
,
223 struct request
*rq
= blk_mq_rq_from_pdu(req
);
229 if (rq
->rq_flags
& RQF_SPECIAL_PAYLOAD
) {
230 vec
= &rq
->special_vec
;
232 size
= blk_rq_payload_bytes(rq
);
235 struct bio
*bio
= req
->curr_bio
;
237 vec
= __bvec_iter_bvec(bio
->bi_io_vec
, bio
->bi_iter
);
238 nsegs
= bio_segments(bio
);
239 size
= bio
->bi_iter
.bi_size
;
240 offset
= bio
->bi_iter
.bi_bvec_done
;
243 iov_iter_bvec(&req
->iter
, dir
, vec
, nsegs
, size
);
244 req
->iter
.iov_offset
= offset
;
247 static inline void nvme_tcp_advance_req(struct nvme_tcp_request
*req
,
250 req
->data_sent
+= len
;
251 req
->pdu_sent
+= len
;
252 iov_iter_advance(&req
->iter
, len
);
253 if (!iov_iter_count(&req
->iter
) &&
254 req
->data_sent
< req
->data_len
) {
255 req
->curr_bio
= req
->curr_bio
->bi_next
;
256 nvme_tcp_init_iter(req
, WRITE
);
260 static inline void nvme_tcp_queue_request(struct nvme_tcp_request
*req
)
262 struct nvme_tcp_queue
*queue
= req
->queue
;
264 spin_lock(&queue
->lock
);
265 list_add_tail(&req
->entry
, &queue
->send_list
);
266 spin_unlock(&queue
->lock
);
268 queue_work_on(queue
->io_cpu
, nvme_tcp_wq
, &queue
->io_work
);
271 static inline struct nvme_tcp_request
*
272 nvme_tcp_fetch_request(struct nvme_tcp_queue
*queue
)
274 struct nvme_tcp_request
*req
;
276 spin_lock(&queue
->lock
);
277 req
= list_first_entry_or_null(&queue
->send_list
,
278 struct nvme_tcp_request
, entry
);
280 list_del(&req
->entry
);
281 spin_unlock(&queue
->lock
);
286 static inline void nvme_tcp_ddgst_final(struct ahash_request
*hash
,
289 ahash_request_set_crypt(hash
, NULL
, (u8
*)dgst
, 0);
290 crypto_ahash_final(hash
);
293 static inline void nvme_tcp_ddgst_update(struct ahash_request
*hash
,
294 struct page
*page
, off_t off
, size_t len
)
296 struct scatterlist sg
;
298 sg_init_marker(&sg
, 1);
299 sg_set_page(&sg
, page
, len
, off
);
300 ahash_request_set_crypt(hash
, &sg
, NULL
, len
);
301 crypto_ahash_update(hash
);
304 static inline void nvme_tcp_hdgst(struct ahash_request
*hash
,
305 void *pdu
, size_t len
)
307 struct scatterlist sg
;
309 sg_init_one(&sg
, pdu
, len
);
310 ahash_request_set_crypt(hash
, &sg
, pdu
+ len
, len
);
311 crypto_ahash_digest(hash
);
314 static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue
*queue
,
315 void *pdu
, size_t pdu_len
)
317 struct nvme_tcp_hdr
*hdr
= pdu
;
321 if (unlikely(!(hdr
->flags
& NVME_TCP_F_HDGST
))) {
322 dev_err(queue
->ctrl
->ctrl
.device
,
323 "queue %d: header digest flag is cleared\n",
324 nvme_tcp_queue_id(queue
));
328 recv_digest
= *(__le32
*)(pdu
+ hdr
->hlen
);
329 nvme_tcp_hdgst(queue
->rcv_hash
, pdu
, pdu_len
);
330 exp_digest
= *(__le32
*)(pdu
+ hdr
->hlen
);
331 if (recv_digest
!= exp_digest
) {
332 dev_err(queue
->ctrl
->ctrl
.device
,
333 "header digest error: recv %#x expected %#x\n",
334 le32_to_cpu(recv_digest
), le32_to_cpu(exp_digest
));
341 static int nvme_tcp_check_ddgst(struct nvme_tcp_queue
*queue
, void *pdu
)
343 struct nvme_tcp_hdr
*hdr
= pdu
;
344 u8 digest_len
= nvme_tcp_hdgst_len(queue
);
347 len
= le32_to_cpu(hdr
->plen
) - hdr
->hlen
-
348 ((hdr
->flags
& NVME_TCP_F_HDGST
) ? digest_len
: 0);
350 if (unlikely(len
&& !(hdr
->flags
& NVME_TCP_F_DDGST
))) {
351 dev_err(queue
->ctrl
->ctrl
.device
,
352 "queue %d: data digest flag is cleared\n",
353 nvme_tcp_queue_id(queue
));
356 crypto_ahash_init(queue
->rcv_hash
);
361 static void nvme_tcp_exit_request(struct blk_mq_tag_set
*set
,
362 struct request
*rq
, unsigned int hctx_idx
)
364 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
366 page_frag_free(req
->pdu
);
369 static int nvme_tcp_init_request(struct blk_mq_tag_set
*set
,
370 struct request
*rq
, unsigned int hctx_idx
,
371 unsigned int numa_node
)
373 struct nvme_tcp_ctrl
*ctrl
= set
->driver_data
;
374 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
375 int queue_idx
= (set
== &ctrl
->tag_set
) ? hctx_idx
+ 1 : 0;
376 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[queue_idx
];
377 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
379 req
->pdu
= page_frag_alloc(&queue
->pf_cache
,
380 sizeof(struct nvme_tcp_cmd_pdu
) + hdgst
,
381 GFP_KERNEL
| __GFP_ZERO
);
386 nvme_req(rq
)->ctrl
= &ctrl
->ctrl
;
391 static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
392 unsigned int hctx_idx
)
394 struct nvme_tcp_ctrl
*ctrl
= data
;
395 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[hctx_idx
+ 1];
397 hctx
->driver_data
= queue
;
401 static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx
*hctx
, void *data
,
402 unsigned int hctx_idx
)
404 struct nvme_tcp_ctrl
*ctrl
= data
;
405 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[0];
407 hctx
->driver_data
= queue
;
411 static enum nvme_tcp_recv_state
412 nvme_tcp_recv_state(struct nvme_tcp_queue
*queue
)
414 return (queue
->pdu_remaining
) ? NVME_TCP_RECV_PDU
:
415 (queue
->ddgst_remaining
) ? NVME_TCP_RECV_DDGST
:
419 static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue
*queue
)
421 queue
->pdu_remaining
= sizeof(struct nvme_tcp_rsp_pdu
) +
422 nvme_tcp_hdgst_len(queue
);
423 queue
->pdu_offset
= 0;
424 queue
->data_remaining
= -1;
425 queue
->ddgst_remaining
= 0;
428 static void nvme_tcp_error_recovery(struct nvme_ctrl
*ctrl
)
430 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_RESETTING
))
433 queue_work(nvme_reset_wq
, &to_tcp_ctrl(ctrl
)->err_work
);
436 static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue
*queue
,
437 struct nvme_completion
*cqe
)
441 rq
= blk_mq_tag_to_rq(nvme_tcp_tagset(queue
), cqe
->command_id
);
443 dev_err(queue
->ctrl
->ctrl
.device
,
444 "queue %d tag 0x%x not found\n",
445 nvme_tcp_queue_id(queue
), cqe
->command_id
);
446 nvme_tcp_error_recovery(&queue
->ctrl
->ctrl
);
450 nvme_end_request(rq
, cqe
->status
, cqe
->result
);
456 static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue
*queue
,
457 struct nvme_tcp_data_pdu
*pdu
)
461 rq
= blk_mq_tag_to_rq(nvme_tcp_tagset(queue
), pdu
->command_id
);
463 dev_err(queue
->ctrl
->ctrl
.device
,
464 "queue %d tag %#x not found\n",
465 nvme_tcp_queue_id(queue
), pdu
->command_id
);
469 if (!blk_rq_payload_bytes(rq
)) {
470 dev_err(queue
->ctrl
->ctrl
.device
,
471 "queue %d tag %#x unexpected data\n",
472 nvme_tcp_queue_id(queue
), rq
->tag
);
476 queue
->data_remaining
= le32_to_cpu(pdu
->data_length
);
478 if (pdu
->hdr
.flags
& NVME_TCP_F_DATA_SUCCESS
&&
479 unlikely(!(pdu
->hdr
.flags
& NVME_TCP_F_DATA_LAST
))) {
480 dev_err(queue
->ctrl
->ctrl
.device
,
481 "queue %d tag %#x SUCCESS set but not last PDU\n",
482 nvme_tcp_queue_id(queue
), rq
->tag
);
483 nvme_tcp_error_recovery(&queue
->ctrl
->ctrl
);
490 static int nvme_tcp_handle_comp(struct nvme_tcp_queue
*queue
,
491 struct nvme_tcp_rsp_pdu
*pdu
)
493 struct nvme_completion
*cqe
= &pdu
->cqe
;
497 * AEN requests are special as they don't time out and can
498 * survive any kind of queue freeze and often don't respond to
499 * aborts. We don't even bother to allocate a struct request
500 * for them but rather special case them here.
502 if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue
),
504 nvme_complete_async_event(&queue
->ctrl
->ctrl
, cqe
->status
,
507 ret
= nvme_tcp_process_nvme_cqe(queue
, cqe
);
512 static int nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request
*req
,
513 struct nvme_tcp_r2t_pdu
*pdu
)
515 struct nvme_tcp_data_pdu
*data
= req
->pdu
;
516 struct nvme_tcp_queue
*queue
= req
->queue
;
517 struct request
*rq
= blk_mq_rq_from_pdu(req
);
518 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
519 u8 ddgst
= nvme_tcp_ddgst_len(queue
);
521 req
->pdu_len
= le32_to_cpu(pdu
->r2t_length
);
524 if (unlikely(req
->data_sent
+ req
->pdu_len
> req
->data_len
)) {
525 dev_err(queue
->ctrl
->ctrl
.device
,
526 "req %d r2t len %u exceeded data len %u (%zu sent)\n",
527 rq
->tag
, req
->pdu_len
, req
->data_len
,
532 if (unlikely(le32_to_cpu(pdu
->r2t_offset
) < req
->data_sent
)) {
533 dev_err(queue
->ctrl
->ctrl
.device
,
534 "req %d unexpected r2t offset %u (expected %zu)\n",
535 rq
->tag
, le32_to_cpu(pdu
->r2t_offset
),
540 memset(data
, 0, sizeof(*data
));
541 data
->hdr
.type
= nvme_tcp_h2c_data
;
542 data
->hdr
.flags
= NVME_TCP_F_DATA_LAST
;
543 if (queue
->hdr_digest
)
544 data
->hdr
.flags
|= NVME_TCP_F_HDGST
;
545 if (queue
->data_digest
)
546 data
->hdr
.flags
|= NVME_TCP_F_DDGST
;
547 data
->hdr
.hlen
= sizeof(*data
);
548 data
->hdr
.pdo
= data
->hdr
.hlen
+ hdgst
;
550 cpu_to_le32(data
->hdr
.hlen
+ hdgst
+ req
->pdu_len
+ ddgst
);
551 data
->ttag
= pdu
->ttag
;
552 data
->command_id
= rq
->tag
;
553 data
->data_offset
= cpu_to_le32(req
->data_sent
);
554 data
->data_length
= cpu_to_le32(req
->pdu_len
);
558 static int nvme_tcp_handle_r2t(struct nvme_tcp_queue
*queue
,
559 struct nvme_tcp_r2t_pdu
*pdu
)
561 struct nvme_tcp_request
*req
;
565 rq
= blk_mq_tag_to_rq(nvme_tcp_tagset(queue
), pdu
->command_id
);
567 dev_err(queue
->ctrl
->ctrl
.device
,
568 "queue %d tag %#x not found\n",
569 nvme_tcp_queue_id(queue
), pdu
->command_id
);
572 req
= blk_mq_rq_to_pdu(rq
);
574 ret
= nvme_tcp_setup_h2c_data_pdu(req
, pdu
);
578 req
->state
= NVME_TCP_SEND_H2C_PDU
;
581 nvme_tcp_queue_request(req
);
586 static int nvme_tcp_recv_pdu(struct nvme_tcp_queue
*queue
, struct sk_buff
*skb
,
587 unsigned int *offset
, size_t *len
)
589 struct nvme_tcp_hdr
*hdr
;
590 char *pdu
= queue
->pdu
;
591 size_t rcv_len
= min_t(size_t, *len
, queue
->pdu_remaining
);
594 ret
= skb_copy_bits(skb
, *offset
,
595 &pdu
[queue
->pdu_offset
], rcv_len
);
599 queue
->pdu_remaining
-= rcv_len
;
600 queue
->pdu_offset
+= rcv_len
;
603 if (queue
->pdu_remaining
)
607 if (queue
->hdr_digest
) {
608 ret
= nvme_tcp_verify_hdgst(queue
, queue
->pdu
, hdr
->hlen
);
614 if (queue
->data_digest
) {
615 ret
= nvme_tcp_check_ddgst(queue
, queue
->pdu
);
621 case nvme_tcp_c2h_data
:
622 return nvme_tcp_handle_c2h_data(queue
, (void *)queue
->pdu
);
624 nvme_tcp_init_recv_ctx(queue
);
625 return nvme_tcp_handle_comp(queue
, (void *)queue
->pdu
);
627 nvme_tcp_init_recv_ctx(queue
);
628 return nvme_tcp_handle_r2t(queue
, (void *)queue
->pdu
);
630 dev_err(queue
->ctrl
->ctrl
.device
,
631 "unsupported pdu type (%d)\n", hdr
->type
);
636 static inline void nvme_tcp_end_request(struct request
*rq
, u16 status
)
638 union nvme_result res
= {};
640 nvme_end_request(rq
, cpu_to_le16(status
<< 1), res
);
643 static int nvme_tcp_recv_data(struct nvme_tcp_queue
*queue
, struct sk_buff
*skb
,
644 unsigned int *offset
, size_t *len
)
646 struct nvme_tcp_data_pdu
*pdu
= (void *)queue
->pdu
;
647 struct nvme_tcp_request
*req
;
650 rq
= blk_mq_tag_to_rq(nvme_tcp_tagset(queue
), pdu
->command_id
);
652 dev_err(queue
->ctrl
->ctrl
.device
,
653 "queue %d tag %#x not found\n",
654 nvme_tcp_queue_id(queue
), pdu
->command_id
);
657 req
= blk_mq_rq_to_pdu(rq
);
662 recv_len
= min_t(size_t, *len
, queue
->data_remaining
);
666 if (!iov_iter_count(&req
->iter
)) {
667 req
->curr_bio
= req
->curr_bio
->bi_next
;
670 * If we don`t have any bios it means that controller
671 * sent more data than we requested, hence error
673 if (!req
->curr_bio
) {
674 dev_err(queue
->ctrl
->ctrl
.device
,
675 "queue %d no space in request %#x",
676 nvme_tcp_queue_id(queue
), rq
->tag
);
677 nvme_tcp_init_recv_ctx(queue
);
680 nvme_tcp_init_iter(req
, READ
);
683 /* we can read only from what is left in this bio */
684 recv_len
= min_t(size_t, recv_len
,
685 iov_iter_count(&req
->iter
));
687 if (queue
->data_digest
)
688 ret
= skb_copy_and_hash_datagram_iter(skb
, *offset
,
689 &req
->iter
, recv_len
, queue
->rcv_hash
);
691 ret
= skb_copy_datagram_iter(skb
, *offset
,
692 &req
->iter
, recv_len
);
694 dev_err(queue
->ctrl
->ctrl
.device
,
695 "queue %d failed to copy request %#x data",
696 nvme_tcp_queue_id(queue
), rq
->tag
);
702 queue
->data_remaining
-= recv_len
;
705 if (!queue
->data_remaining
) {
706 if (queue
->data_digest
) {
707 nvme_tcp_ddgst_final(queue
->rcv_hash
, &queue
->exp_ddgst
);
708 queue
->ddgst_remaining
= NVME_TCP_DIGEST_LENGTH
;
710 if (pdu
->hdr
.flags
& NVME_TCP_F_DATA_SUCCESS
) {
711 nvme_tcp_end_request(rq
, NVME_SC_SUCCESS
);
714 nvme_tcp_init_recv_ctx(queue
);
721 static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue
*queue
,
722 struct sk_buff
*skb
, unsigned int *offset
, size_t *len
)
724 struct nvme_tcp_data_pdu
*pdu
= (void *)queue
->pdu
;
725 char *ddgst
= (char *)&queue
->recv_ddgst
;
726 size_t recv_len
= min_t(size_t, *len
, queue
->ddgst_remaining
);
727 off_t off
= NVME_TCP_DIGEST_LENGTH
- queue
->ddgst_remaining
;
730 ret
= skb_copy_bits(skb
, *offset
, &ddgst
[off
], recv_len
);
734 queue
->ddgst_remaining
-= recv_len
;
737 if (queue
->ddgst_remaining
)
740 if (queue
->recv_ddgst
!= queue
->exp_ddgst
) {
741 dev_err(queue
->ctrl
->ctrl
.device
,
742 "data digest error: recv %#x expected %#x\n",
743 le32_to_cpu(queue
->recv_ddgst
),
744 le32_to_cpu(queue
->exp_ddgst
));
748 if (pdu
->hdr
.flags
& NVME_TCP_F_DATA_SUCCESS
) {
749 struct request
*rq
= blk_mq_tag_to_rq(nvme_tcp_tagset(queue
),
752 nvme_tcp_end_request(rq
, NVME_SC_SUCCESS
);
756 nvme_tcp_init_recv_ctx(queue
);
760 static int nvme_tcp_recv_skb(read_descriptor_t
*desc
, struct sk_buff
*skb
,
761 unsigned int offset
, size_t len
)
763 struct nvme_tcp_queue
*queue
= desc
->arg
.data
;
764 size_t consumed
= len
;
768 switch (nvme_tcp_recv_state(queue
)) {
769 case NVME_TCP_RECV_PDU
:
770 result
= nvme_tcp_recv_pdu(queue
, skb
, &offset
, &len
);
772 case NVME_TCP_RECV_DATA
:
773 result
= nvme_tcp_recv_data(queue
, skb
, &offset
, &len
);
775 case NVME_TCP_RECV_DDGST
:
776 result
= nvme_tcp_recv_ddgst(queue
, skb
, &offset
, &len
);
782 dev_err(queue
->ctrl
->ctrl
.device
,
783 "receive failed: %d\n", result
);
784 queue
->rd_enabled
= false;
785 nvme_tcp_error_recovery(&queue
->ctrl
->ctrl
);
793 static void nvme_tcp_data_ready(struct sock
*sk
)
795 struct nvme_tcp_queue
*queue
;
797 read_lock_bh(&sk
->sk_callback_lock
);
798 queue
= sk
->sk_user_data
;
799 if (likely(queue
&& queue
->rd_enabled
))
800 queue_work_on(queue
->io_cpu
, nvme_tcp_wq
, &queue
->io_work
);
801 read_unlock_bh(&sk
->sk_callback_lock
);
804 static void nvme_tcp_write_space(struct sock
*sk
)
806 struct nvme_tcp_queue
*queue
;
808 read_lock_bh(&sk
->sk_callback_lock
);
809 queue
= sk
->sk_user_data
;
810 if (likely(queue
&& sk_stream_is_writeable(sk
))) {
811 clear_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
812 queue_work_on(queue
->io_cpu
, nvme_tcp_wq
, &queue
->io_work
);
814 read_unlock_bh(&sk
->sk_callback_lock
);
817 static void nvme_tcp_state_change(struct sock
*sk
)
819 struct nvme_tcp_queue
*queue
;
821 read_lock(&sk
->sk_callback_lock
);
822 queue
= sk
->sk_user_data
;
826 switch (sk
->sk_state
) {
833 nvme_tcp_error_recovery(&queue
->ctrl
->ctrl
);
836 dev_info(queue
->ctrl
->ctrl
.device
,
837 "queue %d socket state %d\n",
838 nvme_tcp_queue_id(queue
), sk
->sk_state
);
841 queue
->state_change(sk
);
843 read_unlock(&sk
->sk_callback_lock
);
846 static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue
*queue
)
848 queue
->request
= NULL
;
851 static void nvme_tcp_fail_request(struct nvme_tcp_request
*req
)
853 nvme_tcp_end_request(blk_mq_rq_from_pdu(req
), NVME_SC_HOST_PATH_ERROR
);
856 static int nvme_tcp_try_send_data(struct nvme_tcp_request
*req
)
858 struct nvme_tcp_queue
*queue
= req
->queue
;
861 struct page
*page
= nvme_tcp_req_cur_page(req
);
862 size_t offset
= nvme_tcp_req_cur_offset(req
);
863 size_t len
= nvme_tcp_req_cur_length(req
);
864 bool last
= nvme_tcp_pdu_last_send(req
, len
);
865 int ret
, flags
= MSG_DONTWAIT
;
867 if (last
&& !queue
->data_digest
)
872 /* can't zcopy slab pages */
873 if (unlikely(PageSlab(page
))) {
874 ret
= sock_no_sendpage(queue
->sock
, page
, offset
, len
,
877 ret
= kernel_sendpage(queue
->sock
, page
, offset
, len
,
883 nvme_tcp_advance_req(req
, ret
);
884 if (queue
->data_digest
)
885 nvme_tcp_ddgst_update(queue
->snd_hash
, page
,
888 /* fully successful last write*/
889 if (last
&& ret
== len
) {
890 if (queue
->data_digest
) {
891 nvme_tcp_ddgst_final(queue
->snd_hash
,
893 req
->state
= NVME_TCP_SEND_DDGST
;
896 nvme_tcp_done_send_req(queue
);
904 static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request
*req
)
906 struct nvme_tcp_queue
*queue
= req
->queue
;
907 struct nvme_tcp_cmd_pdu
*pdu
= req
->pdu
;
908 bool inline_data
= nvme_tcp_has_inline_data(req
);
909 int flags
= MSG_DONTWAIT
| (inline_data
? MSG_MORE
: MSG_EOR
);
910 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
911 int len
= sizeof(*pdu
) + hdgst
- req
->offset
;
914 if (queue
->hdr_digest
&& !req
->offset
)
915 nvme_tcp_hdgst(queue
->snd_hash
, pdu
, sizeof(*pdu
));
917 ret
= kernel_sendpage(queue
->sock
, virt_to_page(pdu
),
918 offset_in_page(pdu
) + req
->offset
, len
, flags
);
919 if (unlikely(ret
<= 0))
925 req
->state
= NVME_TCP_SEND_DATA
;
926 if (queue
->data_digest
)
927 crypto_ahash_init(queue
->snd_hash
);
928 nvme_tcp_init_iter(req
, WRITE
);
930 nvme_tcp_done_send_req(queue
);
939 static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request
*req
)
941 struct nvme_tcp_queue
*queue
= req
->queue
;
942 struct nvme_tcp_data_pdu
*pdu
= req
->pdu
;
943 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
944 int len
= sizeof(*pdu
) - req
->offset
+ hdgst
;
947 if (queue
->hdr_digest
&& !req
->offset
)
948 nvme_tcp_hdgst(queue
->snd_hash
, pdu
, sizeof(*pdu
));
950 ret
= kernel_sendpage(queue
->sock
, virt_to_page(pdu
),
951 offset_in_page(pdu
) + req
->offset
, len
,
952 MSG_DONTWAIT
| MSG_MORE
);
953 if (unlikely(ret
<= 0))
958 req
->state
= NVME_TCP_SEND_DATA
;
959 if (queue
->data_digest
)
960 crypto_ahash_init(queue
->snd_hash
);
962 nvme_tcp_init_iter(req
, WRITE
);
970 static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request
*req
)
972 struct nvme_tcp_queue
*queue
= req
->queue
;
974 struct msghdr msg
= { .msg_flags
= MSG_DONTWAIT
| MSG_EOR
};
976 .iov_base
= &req
->ddgst
+ req
->offset
,
977 .iov_len
= NVME_TCP_DIGEST_LENGTH
- req
->offset
980 ret
= kernel_sendmsg(queue
->sock
, &msg
, &iov
, 1, iov
.iov_len
);
981 if (unlikely(ret
<= 0))
984 if (req
->offset
+ ret
== NVME_TCP_DIGEST_LENGTH
) {
985 nvme_tcp_done_send_req(queue
);
993 static int nvme_tcp_try_send(struct nvme_tcp_queue
*queue
)
995 struct nvme_tcp_request
*req
;
998 if (!queue
->request
) {
999 queue
->request
= nvme_tcp_fetch_request(queue
);
1000 if (!queue
->request
)
1003 req
= queue
->request
;
1005 if (req
->state
== NVME_TCP_SEND_CMD_PDU
) {
1006 ret
= nvme_tcp_try_send_cmd_pdu(req
);
1009 if (!nvme_tcp_has_inline_data(req
))
1013 if (req
->state
== NVME_TCP_SEND_H2C_PDU
) {
1014 ret
= nvme_tcp_try_send_data_pdu(req
);
1019 if (req
->state
== NVME_TCP_SEND_DATA
) {
1020 ret
= nvme_tcp_try_send_data(req
);
1025 if (req
->state
== NVME_TCP_SEND_DDGST
)
1026 ret
= nvme_tcp_try_send_ddgst(req
);
1028 if (ret
== -EAGAIN
) {
1030 } else if (ret
< 0) {
1031 dev_err(queue
->ctrl
->ctrl
.device
,
1032 "failed to send request %d\n", ret
);
1033 if (ret
!= -EPIPE
&& ret
!= -ECONNRESET
)
1034 nvme_tcp_fail_request(queue
->request
);
1035 nvme_tcp_done_send_req(queue
);
1040 static int nvme_tcp_try_recv(struct nvme_tcp_queue
*queue
)
1042 struct socket
*sock
= queue
->sock
;
1043 struct sock
*sk
= sock
->sk
;
1044 read_descriptor_t rd_desc
;
1047 rd_desc
.arg
.data
= queue
;
1051 consumed
= sock
->ops
->read_sock(sk
, &rd_desc
, nvme_tcp_recv_skb
);
1056 static void nvme_tcp_io_work(struct work_struct
*w
)
1058 struct nvme_tcp_queue
*queue
=
1059 container_of(w
, struct nvme_tcp_queue
, io_work
);
1060 unsigned long deadline
= jiffies
+ msecs_to_jiffies(1);
1063 bool pending
= false;
1066 result
= nvme_tcp_try_send(queue
);
1069 else if (unlikely(result
< 0))
1072 result
= nvme_tcp_try_recv(queue
);
1075 else if (unlikely(result
< 0))
1081 } while (!time_after(jiffies
, deadline
)); /* quota is exhausted */
1083 queue_work_on(queue
->io_cpu
, nvme_tcp_wq
, &queue
->io_work
);
1086 static void nvme_tcp_free_crypto(struct nvme_tcp_queue
*queue
)
1088 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(queue
->rcv_hash
);
1090 ahash_request_free(queue
->rcv_hash
);
1091 ahash_request_free(queue
->snd_hash
);
1092 crypto_free_ahash(tfm
);
1095 static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue
*queue
)
1097 struct crypto_ahash
*tfm
;
1099 tfm
= crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC
);
1101 return PTR_ERR(tfm
);
1103 queue
->snd_hash
= ahash_request_alloc(tfm
, GFP_KERNEL
);
1104 if (!queue
->snd_hash
)
1106 ahash_request_set_callback(queue
->snd_hash
, 0, NULL
, NULL
);
1108 queue
->rcv_hash
= ahash_request_alloc(tfm
, GFP_KERNEL
);
1109 if (!queue
->rcv_hash
)
1111 ahash_request_set_callback(queue
->rcv_hash
, 0, NULL
, NULL
);
1115 ahash_request_free(queue
->snd_hash
);
1117 crypto_free_ahash(tfm
);
1121 static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl
*ctrl
)
1123 struct nvme_tcp_request
*async
= &ctrl
->async_req
;
1125 page_frag_free(async
->pdu
);
1128 static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl
*ctrl
)
1130 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[0];
1131 struct nvme_tcp_request
*async
= &ctrl
->async_req
;
1132 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
1134 async
->pdu
= page_frag_alloc(&queue
->pf_cache
,
1135 sizeof(struct nvme_tcp_cmd_pdu
) + hdgst
,
1136 GFP_KERNEL
| __GFP_ZERO
);
1140 async
->queue
= &ctrl
->queues
[0];
1144 static void nvme_tcp_free_queue(struct nvme_ctrl
*nctrl
, int qid
)
1146 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1147 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[qid
];
1149 if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED
, &queue
->flags
))
1152 if (queue
->hdr_digest
|| queue
->data_digest
)
1153 nvme_tcp_free_crypto(queue
);
1155 sock_release(queue
->sock
);
1159 static int nvme_tcp_init_connection(struct nvme_tcp_queue
*queue
)
1161 struct nvme_tcp_icreq_pdu
*icreq
;
1162 struct nvme_tcp_icresp_pdu
*icresp
;
1163 struct msghdr msg
= {};
1165 bool ctrl_hdgst
, ctrl_ddgst
;
1168 icreq
= kzalloc(sizeof(*icreq
), GFP_KERNEL
);
1172 icresp
= kzalloc(sizeof(*icresp
), GFP_KERNEL
);
1178 icreq
->hdr
.type
= nvme_tcp_icreq
;
1179 icreq
->hdr
.hlen
= sizeof(*icreq
);
1181 icreq
->hdr
.plen
= cpu_to_le32(icreq
->hdr
.hlen
);
1182 icreq
->pfv
= cpu_to_le16(NVME_TCP_PFV_1_0
);
1183 icreq
->maxr2t
= 0; /* single inflight r2t supported */
1184 icreq
->hpda
= 0; /* no alignment constraint */
1185 if (queue
->hdr_digest
)
1186 icreq
->digest
|= NVME_TCP_HDR_DIGEST_ENABLE
;
1187 if (queue
->data_digest
)
1188 icreq
->digest
|= NVME_TCP_DATA_DIGEST_ENABLE
;
1190 iov
.iov_base
= icreq
;
1191 iov
.iov_len
= sizeof(*icreq
);
1192 ret
= kernel_sendmsg(queue
->sock
, &msg
, &iov
, 1, iov
.iov_len
);
1196 memset(&msg
, 0, sizeof(msg
));
1197 iov
.iov_base
= icresp
;
1198 iov
.iov_len
= sizeof(*icresp
);
1199 ret
= kernel_recvmsg(queue
->sock
, &msg
, &iov
, 1,
1200 iov
.iov_len
, msg
.msg_flags
);
1205 if (icresp
->hdr
.type
!= nvme_tcp_icresp
) {
1206 pr_err("queue %d: bad type returned %d\n",
1207 nvme_tcp_queue_id(queue
), icresp
->hdr
.type
);
1211 if (le32_to_cpu(icresp
->hdr
.plen
) != sizeof(*icresp
)) {
1212 pr_err("queue %d: bad pdu length returned %d\n",
1213 nvme_tcp_queue_id(queue
), icresp
->hdr
.plen
);
1217 if (icresp
->pfv
!= NVME_TCP_PFV_1_0
) {
1218 pr_err("queue %d: bad pfv returned %d\n",
1219 nvme_tcp_queue_id(queue
), icresp
->pfv
);
1223 ctrl_ddgst
= !!(icresp
->digest
& NVME_TCP_DATA_DIGEST_ENABLE
);
1224 if ((queue
->data_digest
&& !ctrl_ddgst
) ||
1225 (!queue
->data_digest
&& ctrl_ddgst
)) {
1226 pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
1227 nvme_tcp_queue_id(queue
),
1228 queue
->data_digest
? "enabled" : "disabled",
1229 ctrl_ddgst
? "enabled" : "disabled");
1233 ctrl_hdgst
= !!(icresp
->digest
& NVME_TCP_HDR_DIGEST_ENABLE
);
1234 if ((queue
->hdr_digest
&& !ctrl_hdgst
) ||
1235 (!queue
->hdr_digest
&& ctrl_hdgst
)) {
1236 pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
1237 nvme_tcp_queue_id(queue
),
1238 queue
->hdr_digest
? "enabled" : "disabled",
1239 ctrl_hdgst
? "enabled" : "disabled");
1243 if (icresp
->cpda
!= 0) {
1244 pr_err("queue %d: unsupported cpda returned %d\n",
1245 nvme_tcp_queue_id(queue
), icresp
->cpda
);
1257 static bool nvme_tcp_admin_queue(struct nvme_tcp_queue
*queue
)
1259 return nvme_tcp_queue_id(queue
) == 0;
1262 static bool nvme_tcp_default_queue(struct nvme_tcp_queue
*queue
)
1264 struct nvme_tcp_ctrl
*ctrl
= queue
->ctrl
;
1265 int qid
= nvme_tcp_queue_id(queue
);
1267 return !nvme_tcp_admin_queue(queue
) &&
1268 qid
< 1 + ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
1271 static bool nvme_tcp_read_queue(struct nvme_tcp_queue
*queue
)
1273 struct nvme_tcp_ctrl
*ctrl
= queue
->ctrl
;
1274 int qid
= nvme_tcp_queue_id(queue
);
1276 return !nvme_tcp_admin_queue(queue
) &&
1277 !nvme_tcp_default_queue(queue
) &&
1278 qid
< 1 + ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] +
1279 ctrl
->io_queues
[HCTX_TYPE_READ
];
1282 static bool nvme_tcp_poll_queue(struct nvme_tcp_queue
*queue
)
1284 struct nvme_tcp_ctrl
*ctrl
= queue
->ctrl
;
1285 int qid
= nvme_tcp_queue_id(queue
);
1287 return !nvme_tcp_admin_queue(queue
) &&
1288 !nvme_tcp_default_queue(queue
) &&
1289 !nvme_tcp_read_queue(queue
) &&
1290 qid
< 1 + ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] +
1291 ctrl
->io_queues
[HCTX_TYPE_READ
] +
1292 ctrl
->io_queues
[HCTX_TYPE_POLL
];
1295 static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue
*queue
)
1297 struct nvme_tcp_ctrl
*ctrl
= queue
->ctrl
;
1298 int qid
= nvme_tcp_queue_id(queue
);
1301 if (nvme_tcp_default_queue(queue
))
1303 else if (nvme_tcp_read_queue(queue
))
1304 n
= qid
- ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] - 1;
1305 else if (nvme_tcp_poll_queue(queue
))
1306 n
= qid
- ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] -
1307 ctrl
->io_queues
[HCTX_TYPE_READ
] - 1;
1308 queue
->io_cpu
= cpumask_next_wrap(n
- 1, cpu_online_mask
, -1, false);
1311 static int nvme_tcp_alloc_queue(struct nvme_ctrl
*nctrl
,
1312 int qid
, size_t queue_size
)
1314 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1315 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[qid
];
1316 struct linger sol
= { .l_onoff
= 1, .l_linger
= 0 };
1317 int ret
, opt
, rcv_pdu_size
;
1320 INIT_LIST_HEAD(&queue
->send_list
);
1321 spin_lock_init(&queue
->lock
);
1322 INIT_WORK(&queue
->io_work
, nvme_tcp_io_work
);
1323 queue
->queue_size
= queue_size
;
1326 queue
->cmnd_capsule_len
= nctrl
->ioccsz
* 16;
1328 queue
->cmnd_capsule_len
= sizeof(struct nvme_command
) +
1329 NVME_TCP_ADMIN_CCSZ
;
1331 ret
= sock_create(ctrl
->addr
.ss_family
, SOCK_STREAM
,
1332 IPPROTO_TCP
, &queue
->sock
);
1334 dev_err(nctrl
->device
,
1335 "failed to create socket: %d\n", ret
);
1339 /* Single syn retry */
1341 ret
= kernel_setsockopt(queue
->sock
, IPPROTO_TCP
, TCP_SYNCNT
,
1342 (char *)&opt
, sizeof(opt
));
1344 dev_err(nctrl
->device
,
1345 "failed to set TCP_SYNCNT sock opt %d\n", ret
);
1349 /* Set TCP no delay */
1351 ret
= kernel_setsockopt(queue
->sock
, IPPROTO_TCP
,
1352 TCP_NODELAY
, (char *)&opt
, sizeof(opt
));
1354 dev_err(nctrl
->device
,
1355 "failed to set TCP_NODELAY sock opt %d\n", ret
);
1360 * Cleanup whatever is sitting in the TCP transmit queue on socket
1361 * close. This is done to prevent stale data from being sent should
1362 * the network connection be restored before TCP times out.
1364 ret
= kernel_setsockopt(queue
->sock
, SOL_SOCKET
, SO_LINGER
,
1365 (char *)&sol
, sizeof(sol
));
1367 dev_err(nctrl
->device
,
1368 "failed to set SO_LINGER sock opt %d\n", ret
);
1372 if (so_priority
> 0) {
1373 ret
= kernel_setsockopt(queue
->sock
, SOL_SOCKET
, SO_PRIORITY
,
1374 (char *)&so_priority
, sizeof(so_priority
));
1376 dev_err(ctrl
->ctrl
.device
,
1377 "failed to set SO_PRIORITY sock opt, ret %d\n",
1383 /* Set socket type of service */
1384 if (nctrl
->opts
->tos
>= 0) {
1385 opt
= nctrl
->opts
->tos
;
1386 ret
= kernel_setsockopt(queue
->sock
, SOL_IP
, IP_TOS
,
1387 (char *)&opt
, sizeof(opt
));
1389 dev_err(nctrl
->device
,
1390 "failed to set IP_TOS sock opt %d\n", ret
);
1395 queue
->sock
->sk
->sk_allocation
= GFP_ATOMIC
;
1396 nvme_tcp_set_queue_io_cpu(queue
);
1397 queue
->request
= NULL
;
1398 queue
->data_remaining
= 0;
1399 queue
->ddgst_remaining
= 0;
1400 queue
->pdu_remaining
= 0;
1401 queue
->pdu_offset
= 0;
1402 sk_set_memalloc(queue
->sock
->sk
);
1404 if (nctrl
->opts
->mask
& NVMF_OPT_HOST_TRADDR
) {
1405 ret
= kernel_bind(queue
->sock
, (struct sockaddr
*)&ctrl
->src_addr
,
1406 sizeof(ctrl
->src_addr
));
1408 dev_err(nctrl
->device
,
1409 "failed to bind queue %d socket %d\n",
1415 queue
->hdr_digest
= nctrl
->opts
->hdr_digest
;
1416 queue
->data_digest
= nctrl
->opts
->data_digest
;
1417 if (queue
->hdr_digest
|| queue
->data_digest
) {
1418 ret
= nvme_tcp_alloc_crypto(queue
);
1420 dev_err(nctrl
->device
,
1421 "failed to allocate queue %d crypto\n", qid
);
1426 rcv_pdu_size
= sizeof(struct nvme_tcp_rsp_pdu
) +
1427 nvme_tcp_hdgst_len(queue
);
1428 queue
->pdu
= kmalloc(rcv_pdu_size
, GFP_KERNEL
);
1434 dev_dbg(nctrl
->device
, "connecting queue %d\n",
1435 nvme_tcp_queue_id(queue
));
1437 ret
= kernel_connect(queue
->sock
, (struct sockaddr
*)&ctrl
->addr
,
1438 sizeof(ctrl
->addr
), 0);
1440 dev_err(nctrl
->device
,
1441 "failed to connect socket: %d\n", ret
);
1445 ret
= nvme_tcp_init_connection(queue
);
1447 goto err_init_connect
;
1449 queue
->rd_enabled
= true;
1450 set_bit(NVME_TCP_Q_ALLOCATED
, &queue
->flags
);
1451 nvme_tcp_init_recv_ctx(queue
);
1453 write_lock_bh(&queue
->sock
->sk
->sk_callback_lock
);
1454 queue
->sock
->sk
->sk_user_data
= queue
;
1455 queue
->state_change
= queue
->sock
->sk
->sk_state_change
;
1456 queue
->data_ready
= queue
->sock
->sk
->sk_data_ready
;
1457 queue
->write_space
= queue
->sock
->sk
->sk_write_space
;
1458 queue
->sock
->sk
->sk_data_ready
= nvme_tcp_data_ready
;
1459 queue
->sock
->sk
->sk_state_change
= nvme_tcp_state_change
;
1460 queue
->sock
->sk
->sk_write_space
= nvme_tcp_write_space
;
1461 #ifdef CONFIG_NET_RX_BUSY_POLL
1462 queue
->sock
->sk
->sk_ll_usec
= 1;
1464 write_unlock_bh(&queue
->sock
->sk
->sk_callback_lock
);
1469 kernel_sock_shutdown(queue
->sock
, SHUT_RDWR
);
1473 if (queue
->hdr_digest
|| queue
->data_digest
)
1474 nvme_tcp_free_crypto(queue
);
1476 sock_release(queue
->sock
);
1481 static void nvme_tcp_restore_sock_calls(struct nvme_tcp_queue
*queue
)
1483 struct socket
*sock
= queue
->sock
;
1485 write_lock_bh(&sock
->sk
->sk_callback_lock
);
1486 sock
->sk
->sk_user_data
= NULL
;
1487 sock
->sk
->sk_data_ready
= queue
->data_ready
;
1488 sock
->sk
->sk_state_change
= queue
->state_change
;
1489 sock
->sk
->sk_write_space
= queue
->write_space
;
1490 write_unlock_bh(&sock
->sk
->sk_callback_lock
);
1493 static void __nvme_tcp_stop_queue(struct nvme_tcp_queue
*queue
)
1495 kernel_sock_shutdown(queue
->sock
, SHUT_RDWR
);
1496 nvme_tcp_restore_sock_calls(queue
);
1497 cancel_work_sync(&queue
->io_work
);
1500 static void nvme_tcp_stop_queue(struct nvme_ctrl
*nctrl
, int qid
)
1502 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1503 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[qid
];
1505 if (!test_and_clear_bit(NVME_TCP_Q_LIVE
, &queue
->flags
))
1508 __nvme_tcp_stop_queue(queue
);
1511 static int nvme_tcp_start_queue(struct nvme_ctrl
*nctrl
, int idx
)
1513 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1517 ret
= nvmf_connect_io_queue(nctrl
, idx
, false);
1519 ret
= nvmf_connect_admin_queue(nctrl
);
1522 set_bit(NVME_TCP_Q_LIVE
, &ctrl
->queues
[idx
].flags
);
1524 if (test_bit(NVME_TCP_Q_ALLOCATED
, &ctrl
->queues
[idx
].flags
))
1525 __nvme_tcp_stop_queue(&ctrl
->queues
[idx
]);
1526 dev_err(nctrl
->device
,
1527 "failed to connect queue: %d ret=%d\n", idx
, ret
);
1532 static struct blk_mq_tag_set
*nvme_tcp_alloc_tagset(struct nvme_ctrl
*nctrl
,
1535 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1536 struct blk_mq_tag_set
*set
;
1540 set
= &ctrl
->admin_tag_set
;
1541 memset(set
, 0, sizeof(*set
));
1542 set
->ops
= &nvme_tcp_admin_mq_ops
;
1543 set
->queue_depth
= NVME_AQ_MQ_TAG_DEPTH
;
1544 set
->reserved_tags
= 2; /* connect + keep-alive */
1545 set
->numa_node
= NUMA_NO_NODE
;
1546 set
->cmd_size
= sizeof(struct nvme_tcp_request
);
1547 set
->driver_data
= ctrl
;
1548 set
->nr_hw_queues
= 1;
1549 set
->timeout
= ADMIN_TIMEOUT
;
1551 set
= &ctrl
->tag_set
;
1552 memset(set
, 0, sizeof(*set
));
1553 set
->ops
= &nvme_tcp_mq_ops
;
1554 set
->queue_depth
= nctrl
->sqsize
+ 1;
1555 set
->reserved_tags
= 1; /* fabric connect */
1556 set
->numa_node
= NUMA_NO_NODE
;
1557 set
->flags
= BLK_MQ_F_SHOULD_MERGE
;
1558 set
->cmd_size
= sizeof(struct nvme_tcp_request
);
1559 set
->driver_data
= ctrl
;
1560 set
->nr_hw_queues
= nctrl
->queue_count
- 1;
1561 set
->timeout
= NVME_IO_TIMEOUT
;
1562 set
->nr_maps
= nctrl
->opts
->nr_poll_queues
? HCTX_MAX_TYPES
: 2;
1565 ret
= blk_mq_alloc_tag_set(set
);
1567 return ERR_PTR(ret
);
1572 static void nvme_tcp_free_admin_queue(struct nvme_ctrl
*ctrl
)
1574 if (to_tcp_ctrl(ctrl
)->async_req
.pdu
) {
1575 nvme_tcp_free_async_req(to_tcp_ctrl(ctrl
));
1576 to_tcp_ctrl(ctrl
)->async_req
.pdu
= NULL
;
1579 nvme_tcp_free_queue(ctrl
, 0);
1582 static void nvme_tcp_free_io_queues(struct nvme_ctrl
*ctrl
)
1586 for (i
= 1; i
< ctrl
->queue_count
; i
++)
1587 nvme_tcp_free_queue(ctrl
, i
);
1590 static void nvme_tcp_stop_io_queues(struct nvme_ctrl
*ctrl
)
1594 for (i
= 1; i
< ctrl
->queue_count
; i
++)
1595 nvme_tcp_stop_queue(ctrl
, i
);
1598 static int nvme_tcp_start_io_queues(struct nvme_ctrl
*ctrl
)
1602 for (i
= 1; i
< ctrl
->queue_count
; i
++) {
1603 ret
= nvme_tcp_start_queue(ctrl
, i
);
1605 goto out_stop_queues
;
1611 for (i
--; i
>= 1; i
--)
1612 nvme_tcp_stop_queue(ctrl
, i
);
1616 static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl
*ctrl
)
1620 ret
= nvme_tcp_alloc_queue(ctrl
, 0, NVME_AQ_DEPTH
);
1624 ret
= nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl
));
1626 goto out_free_queue
;
1631 nvme_tcp_free_queue(ctrl
, 0);
1635 static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl
*ctrl
)
1639 for (i
= 1; i
< ctrl
->queue_count
; i
++) {
1640 ret
= nvme_tcp_alloc_queue(ctrl
, i
,
1643 goto out_free_queues
;
1649 for (i
--; i
>= 1; i
--)
1650 nvme_tcp_free_queue(ctrl
, i
);
1655 static unsigned int nvme_tcp_nr_io_queues(struct nvme_ctrl
*ctrl
)
1657 unsigned int nr_io_queues
;
1659 nr_io_queues
= min(ctrl
->opts
->nr_io_queues
, num_online_cpus());
1660 nr_io_queues
+= min(ctrl
->opts
->nr_write_queues
, num_online_cpus());
1661 nr_io_queues
+= min(ctrl
->opts
->nr_poll_queues
, num_online_cpus());
1663 return nr_io_queues
;
1666 static void nvme_tcp_set_io_queues(struct nvme_ctrl
*nctrl
,
1667 unsigned int nr_io_queues
)
1669 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
1670 struct nvmf_ctrl_options
*opts
= nctrl
->opts
;
1672 if (opts
->nr_write_queues
&& opts
->nr_io_queues
< nr_io_queues
) {
1674 * separate read/write queues
1675 * hand out dedicated default queues only after we have
1676 * sufficient read queues.
1678 ctrl
->io_queues
[HCTX_TYPE_READ
] = opts
->nr_io_queues
;
1679 nr_io_queues
-= ctrl
->io_queues
[HCTX_TYPE_READ
];
1680 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] =
1681 min(opts
->nr_write_queues
, nr_io_queues
);
1682 nr_io_queues
-= ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
1685 * shared read/write queues
1686 * either no write queues were requested, or we don't have
1687 * sufficient queue count to have dedicated default queues.
1689 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] =
1690 min(opts
->nr_io_queues
, nr_io_queues
);
1691 nr_io_queues
-= ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
1694 if (opts
->nr_poll_queues
&& nr_io_queues
) {
1695 /* map dedicated poll queues only if we have queues left */
1696 ctrl
->io_queues
[HCTX_TYPE_POLL
] =
1697 min(opts
->nr_poll_queues
, nr_io_queues
);
1701 static int nvme_tcp_alloc_io_queues(struct nvme_ctrl
*ctrl
)
1703 unsigned int nr_io_queues
;
1706 nr_io_queues
= nvme_tcp_nr_io_queues(ctrl
);
1707 ret
= nvme_set_queue_count(ctrl
, &nr_io_queues
);
1711 ctrl
->queue_count
= nr_io_queues
+ 1;
1712 if (ctrl
->queue_count
< 2)
1715 dev_info(ctrl
->device
,
1716 "creating %d I/O queues.\n", nr_io_queues
);
1718 nvme_tcp_set_io_queues(ctrl
, nr_io_queues
);
1720 return __nvme_tcp_alloc_io_queues(ctrl
);
1723 static void nvme_tcp_destroy_io_queues(struct nvme_ctrl
*ctrl
, bool remove
)
1725 nvme_tcp_stop_io_queues(ctrl
);
1727 blk_cleanup_queue(ctrl
->connect_q
);
1728 blk_mq_free_tag_set(ctrl
->tagset
);
1730 nvme_tcp_free_io_queues(ctrl
);
1733 static int nvme_tcp_configure_io_queues(struct nvme_ctrl
*ctrl
, bool new)
1737 ret
= nvme_tcp_alloc_io_queues(ctrl
);
1742 ctrl
->tagset
= nvme_tcp_alloc_tagset(ctrl
, false);
1743 if (IS_ERR(ctrl
->tagset
)) {
1744 ret
= PTR_ERR(ctrl
->tagset
);
1745 goto out_free_io_queues
;
1748 ctrl
->connect_q
= blk_mq_init_queue(ctrl
->tagset
);
1749 if (IS_ERR(ctrl
->connect_q
)) {
1750 ret
= PTR_ERR(ctrl
->connect_q
);
1751 goto out_free_tag_set
;
1754 blk_mq_update_nr_hw_queues(ctrl
->tagset
,
1755 ctrl
->queue_count
- 1);
1758 ret
= nvme_tcp_start_io_queues(ctrl
);
1760 goto out_cleanup_connect_q
;
1764 out_cleanup_connect_q
:
1766 blk_cleanup_queue(ctrl
->connect_q
);
1769 blk_mq_free_tag_set(ctrl
->tagset
);
1771 nvme_tcp_free_io_queues(ctrl
);
1775 static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl
*ctrl
, bool remove
)
1777 nvme_tcp_stop_queue(ctrl
, 0);
1779 blk_cleanup_queue(ctrl
->admin_q
);
1780 blk_cleanup_queue(ctrl
->fabrics_q
);
1781 blk_mq_free_tag_set(ctrl
->admin_tagset
);
1783 nvme_tcp_free_admin_queue(ctrl
);
1786 static int nvme_tcp_configure_admin_queue(struct nvme_ctrl
*ctrl
, bool new)
1790 error
= nvme_tcp_alloc_admin_queue(ctrl
);
1795 ctrl
->admin_tagset
= nvme_tcp_alloc_tagset(ctrl
, true);
1796 if (IS_ERR(ctrl
->admin_tagset
)) {
1797 error
= PTR_ERR(ctrl
->admin_tagset
);
1798 goto out_free_queue
;
1801 ctrl
->fabrics_q
= blk_mq_init_queue(ctrl
->admin_tagset
);
1802 if (IS_ERR(ctrl
->fabrics_q
)) {
1803 error
= PTR_ERR(ctrl
->fabrics_q
);
1804 goto out_free_tagset
;
1807 ctrl
->admin_q
= blk_mq_init_queue(ctrl
->admin_tagset
);
1808 if (IS_ERR(ctrl
->admin_q
)) {
1809 error
= PTR_ERR(ctrl
->admin_q
);
1810 goto out_cleanup_fabrics_q
;
1814 error
= nvme_tcp_start_queue(ctrl
, 0);
1816 goto out_cleanup_queue
;
1818 error
= nvme_enable_ctrl(ctrl
);
1820 goto out_stop_queue
;
1822 blk_mq_unquiesce_queue(ctrl
->admin_q
);
1824 error
= nvme_init_identify(ctrl
);
1826 goto out_stop_queue
;
1831 nvme_tcp_stop_queue(ctrl
, 0);
1834 blk_cleanup_queue(ctrl
->admin_q
);
1835 out_cleanup_fabrics_q
:
1837 blk_cleanup_queue(ctrl
->fabrics_q
);
1840 blk_mq_free_tag_set(ctrl
->admin_tagset
);
1842 nvme_tcp_free_admin_queue(ctrl
);
1846 static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl
*ctrl
,
1849 blk_mq_quiesce_queue(ctrl
->admin_q
);
1850 nvme_tcp_stop_queue(ctrl
, 0);
1851 if (ctrl
->admin_tagset
) {
1852 blk_mq_tagset_busy_iter(ctrl
->admin_tagset
,
1853 nvme_cancel_request
, ctrl
);
1854 blk_mq_tagset_wait_completed_request(ctrl
->admin_tagset
);
1857 blk_mq_unquiesce_queue(ctrl
->admin_q
);
1858 nvme_tcp_destroy_admin_queue(ctrl
, remove
);
1861 static void nvme_tcp_teardown_io_queues(struct nvme_ctrl
*ctrl
,
1864 if (ctrl
->queue_count
<= 1)
1866 nvme_stop_queues(ctrl
);
1867 nvme_tcp_stop_io_queues(ctrl
);
1869 blk_mq_tagset_busy_iter(ctrl
->tagset
,
1870 nvme_cancel_request
, ctrl
);
1871 blk_mq_tagset_wait_completed_request(ctrl
->tagset
);
1874 nvme_start_queues(ctrl
);
1875 nvme_tcp_destroy_io_queues(ctrl
, remove
);
1878 static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl
*ctrl
)
1880 /* If we are resetting/deleting then do nothing */
1881 if (ctrl
->state
!= NVME_CTRL_CONNECTING
) {
1882 WARN_ON_ONCE(ctrl
->state
== NVME_CTRL_NEW
||
1883 ctrl
->state
== NVME_CTRL_LIVE
);
1887 if (nvmf_should_reconnect(ctrl
)) {
1888 dev_info(ctrl
->device
, "Reconnecting in %d seconds...\n",
1889 ctrl
->opts
->reconnect_delay
);
1890 queue_delayed_work(nvme_wq
, &to_tcp_ctrl(ctrl
)->connect_work
,
1891 ctrl
->opts
->reconnect_delay
* HZ
);
1893 dev_info(ctrl
->device
, "Removing controller...\n");
1894 nvme_delete_ctrl(ctrl
);
1898 static int nvme_tcp_setup_ctrl(struct nvme_ctrl
*ctrl
, bool new)
1900 struct nvmf_ctrl_options
*opts
= ctrl
->opts
;
1903 ret
= nvme_tcp_configure_admin_queue(ctrl
, new);
1908 dev_err(ctrl
->device
, "icdoff is not supported!\n");
1912 if (opts
->queue_size
> ctrl
->sqsize
+ 1)
1913 dev_warn(ctrl
->device
,
1914 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1915 opts
->queue_size
, ctrl
->sqsize
+ 1);
1917 if (ctrl
->sqsize
+ 1 > ctrl
->maxcmd
) {
1918 dev_warn(ctrl
->device
,
1919 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1920 ctrl
->sqsize
+ 1, ctrl
->maxcmd
);
1921 ctrl
->sqsize
= ctrl
->maxcmd
- 1;
1924 if (ctrl
->queue_count
> 1) {
1925 ret
= nvme_tcp_configure_io_queues(ctrl
, new);
1930 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_LIVE
)) {
1932 * state change failure is ok if we're in DELETING state,
1933 * unless we're during creation of a new controller to
1934 * avoid races with teardown flow.
1936 WARN_ON_ONCE(ctrl
->state
!= NVME_CTRL_DELETING
);
1942 nvme_start_ctrl(ctrl
);
1946 if (ctrl
->queue_count
> 1)
1947 nvme_tcp_destroy_io_queues(ctrl
, new);
1949 nvme_tcp_stop_queue(ctrl
, 0);
1950 nvme_tcp_destroy_admin_queue(ctrl
, new);
1954 static void nvme_tcp_reconnect_ctrl_work(struct work_struct
*work
)
1956 struct nvme_tcp_ctrl
*tcp_ctrl
= container_of(to_delayed_work(work
),
1957 struct nvme_tcp_ctrl
, connect_work
);
1958 struct nvme_ctrl
*ctrl
= &tcp_ctrl
->ctrl
;
1960 ++ctrl
->nr_reconnects
;
1962 if (nvme_tcp_setup_ctrl(ctrl
, false))
1965 dev_info(ctrl
->device
, "Successfully reconnected (%d attempt)\n",
1966 ctrl
->nr_reconnects
);
1968 ctrl
->nr_reconnects
= 0;
1973 dev_info(ctrl
->device
, "Failed reconnect attempt %d\n",
1974 ctrl
->nr_reconnects
);
1975 nvme_tcp_reconnect_or_remove(ctrl
);
1978 static void nvme_tcp_error_recovery_work(struct work_struct
*work
)
1980 struct nvme_tcp_ctrl
*tcp_ctrl
= container_of(work
,
1981 struct nvme_tcp_ctrl
, err_work
);
1982 struct nvme_ctrl
*ctrl
= &tcp_ctrl
->ctrl
;
1984 nvme_stop_keep_alive(ctrl
);
1985 nvme_tcp_teardown_io_queues(ctrl
, false);
1986 /* unquiesce to fail fast pending requests */
1987 nvme_start_queues(ctrl
);
1988 nvme_tcp_teardown_admin_queue(ctrl
, false);
1989 blk_mq_unquiesce_queue(ctrl
->admin_q
);
1991 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_CONNECTING
)) {
1992 /* state change failure is ok if we're in DELETING state */
1993 WARN_ON_ONCE(ctrl
->state
!= NVME_CTRL_DELETING
);
1997 nvme_tcp_reconnect_or_remove(ctrl
);
2000 static void nvme_tcp_teardown_ctrl(struct nvme_ctrl
*ctrl
, bool shutdown
)
2002 cancel_work_sync(&to_tcp_ctrl(ctrl
)->err_work
);
2003 cancel_delayed_work_sync(&to_tcp_ctrl(ctrl
)->connect_work
);
2005 nvme_tcp_teardown_io_queues(ctrl
, shutdown
);
2006 blk_mq_quiesce_queue(ctrl
->admin_q
);
2008 nvme_shutdown_ctrl(ctrl
);
2010 nvme_disable_ctrl(ctrl
);
2011 nvme_tcp_teardown_admin_queue(ctrl
, shutdown
);
2014 static void nvme_tcp_delete_ctrl(struct nvme_ctrl
*ctrl
)
2016 nvme_tcp_teardown_ctrl(ctrl
, true);
2019 static void nvme_reset_ctrl_work(struct work_struct
*work
)
2021 struct nvme_ctrl
*ctrl
=
2022 container_of(work
, struct nvme_ctrl
, reset_work
);
2024 nvme_stop_ctrl(ctrl
);
2025 nvme_tcp_teardown_ctrl(ctrl
, false);
2027 if (!nvme_change_ctrl_state(ctrl
, NVME_CTRL_CONNECTING
)) {
2028 /* state change failure is ok if we're in DELETING state */
2029 WARN_ON_ONCE(ctrl
->state
!= NVME_CTRL_DELETING
);
2033 if (nvme_tcp_setup_ctrl(ctrl
, false))
2039 ++ctrl
->nr_reconnects
;
2040 nvme_tcp_reconnect_or_remove(ctrl
);
2043 static void nvme_tcp_free_ctrl(struct nvme_ctrl
*nctrl
)
2045 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(nctrl
);
2047 if (list_empty(&ctrl
->list
))
2050 mutex_lock(&nvme_tcp_ctrl_mutex
);
2051 list_del(&ctrl
->list
);
2052 mutex_unlock(&nvme_tcp_ctrl_mutex
);
2054 nvmf_free_options(nctrl
->opts
);
2056 kfree(ctrl
->queues
);
2060 static void nvme_tcp_set_sg_null(struct nvme_command
*c
)
2062 struct nvme_sgl_desc
*sg
= &c
->common
.dptr
.sgl
;
2066 sg
->type
= (NVME_TRANSPORT_SGL_DATA_DESC
<< 4) |
2067 NVME_SGL_FMT_TRANSPORT_A
;
2070 static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue
*queue
,
2071 struct nvme_command
*c
, u32 data_len
)
2073 struct nvme_sgl_desc
*sg
= &c
->common
.dptr
.sgl
;
2075 sg
->addr
= cpu_to_le64(queue
->ctrl
->ctrl
.icdoff
);
2076 sg
->length
= cpu_to_le32(data_len
);
2077 sg
->type
= (NVME_SGL_FMT_DATA_DESC
<< 4) | NVME_SGL_FMT_OFFSET
;
2080 static void nvme_tcp_set_sg_host_data(struct nvme_command
*c
,
2083 struct nvme_sgl_desc
*sg
= &c
->common
.dptr
.sgl
;
2086 sg
->length
= cpu_to_le32(data_len
);
2087 sg
->type
= (NVME_TRANSPORT_SGL_DATA_DESC
<< 4) |
2088 NVME_SGL_FMT_TRANSPORT_A
;
2091 static void nvme_tcp_submit_async_event(struct nvme_ctrl
*arg
)
2093 struct nvme_tcp_ctrl
*ctrl
= to_tcp_ctrl(arg
);
2094 struct nvme_tcp_queue
*queue
= &ctrl
->queues
[0];
2095 struct nvme_tcp_cmd_pdu
*pdu
= ctrl
->async_req
.pdu
;
2096 struct nvme_command
*cmd
= &pdu
->cmd
;
2097 u8 hdgst
= nvme_tcp_hdgst_len(queue
);
2099 memset(pdu
, 0, sizeof(*pdu
));
2100 pdu
->hdr
.type
= nvme_tcp_cmd
;
2101 if (queue
->hdr_digest
)
2102 pdu
->hdr
.flags
|= NVME_TCP_F_HDGST
;
2103 pdu
->hdr
.hlen
= sizeof(*pdu
);
2104 pdu
->hdr
.plen
= cpu_to_le32(pdu
->hdr
.hlen
+ hdgst
);
2106 cmd
->common
.opcode
= nvme_admin_async_event
;
2107 cmd
->common
.command_id
= NVME_AQ_BLK_MQ_DEPTH
;
2108 cmd
->common
.flags
|= NVME_CMD_SGL_METABUF
;
2109 nvme_tcp_set_sg_null(cmd
);
2111 ctrl
->async_req
.state
= NVME_TCP_SEND_CMD_PDU
;
2112 ctrl
->async_req
.offset
= 0;
2113 ctrl
->async_req
.curr_bio
= NULL
;
2114 ctrl
->async_req
.data_len
= 0;
2116 nvme_tcp_queue_request(&ctrl
->async_req
);
2119 static enum blk_eh_timer_return
2120 nvme_tcp_timeout(struct request
*rq
, bool reserved
)
2122 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
2123 struct nvme_tcp_ctrl
*ctrl
= req
->queue
->ctrl
;
2124 struct nvme_tcp_cmd_pdu
*pdu
= req
->pdu
;
2127 * Restart the timer if a controller reset is already scheduled. Any
2128 * timed out commands would be handled before entering the connecting
2131 if (ctrl
->ctrl
.state
== NVME_CTRL_RESETTING
)
2132 return BLK_EH_RESET_TIMER
;
2134 dev_warn(ctrl
->ctrl
.device
,
2135 "queue %d: timeout request %#x type %d\n",
2136 nvme_tcp_queue_id(req
->queue
), rq
->tag
, pdu
->hdr
.type
);
2138 if (ctrl
->ctrl
.state
!= NVME_CTRL_LIVE
) {
2140 * Teardown immediately if controller times out while starting
2141 * or we are already started error recovery. all outstanding
2142 * requests are completed on shutdown, so we return BLK_EH_DONE.
2144 flush_work(&ctrl
->err_work
);
2145 nvme_tcp_teardown_io_queues(&ctrl
->ctrl
, false);
2146 nvme_tcp_teardown_admin_queue(&ctrl
->ctrl
, false);
2150 dev_warn(ctrl
->ctrl
.device
, "starting error recovery\n");
2151 nvme_tcp_error_recovery(&ctrl
->ctrl
);
2153 return BLK_EH_RESET_TIMER
;
2156 static blk_status_t
nvme_tcp_map_data(struct nvme_tcp_queue
*queue
,
2159 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
2160 struct nvme_tcp_cmd_pdu
*pdu
= req
->pdu
;
2161 struct nvme_command
*c
= &pdu
->cmd
;
2163 c
->common
.flags
|= NVME_CMD_SGL_METABUF
;
2165 if (!blk_rq_nr_phys_segments(rq
))
2166 nvme_tcp_set_sg_null(c
);
2167 else if (rq_data_dir(rq
) == WRITE
&&
2168 req
->data_len
<= nvme_tcp_inline_data_size(queue
))
2169 nvme_tcp_set_sg_inline(queue
, c
, req
->data_len
);
2171 nvme_tcp_set_sg_host_data(c
, req
->data_len
);
2176 static blk_status_t
nvme_tcp_setup_cmd_pdu(struct nvme_ns
*ns
,
2179 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
2180 struct nvme_tcp_cmd_pdu
*pdu
= req
->pdu
;
2181 struct nvme_tcp_queue
*queue
= req
->queue
;
2182 u8 hdgst
= nvme_tcp_hdgst_len(queue
), ddgst
= 0;
2185 ret
= nvme_setup_cmd(ns
, rq
, &pdu
->cmd
);
2189 req
->state
= NVME_TCP_SEND_CMD_PDU
;
2194 req
->data_len
= blk_rq_nr_phys_segments(rq
) ?
2195 blk_rq_payload_bytes(rq
) : 0;
2196 req
->curr_bio
= rq
->bio
;
2198 if (rq_data_dir(rq
) == WRITE
&&
2199 req
->data_len
<= nvme_tcp_inline_data_size(queue
))
2200 req
->pdu_len
= req
->data_len
;
2201 else if (req
->curr_bio
)
2202 nvme_tcp_init_iter(req
, READ
);
2204 pdu
->hdr
.type
= nvme_tcp_cmd
;
2206 if (queue
->hdr_digest
)
2207 pdu
->hdr
.flags
|= NVME_TCP_F_HDGST
;
2208 if (queue
->data_digest
&& req
->pdu_len
) {
2209 pdu
->hdr
.flags
|= NVME_TCP_F_DDGST
;
2210 ddgst
= nvme_tcp_ddgst_len(queue
);
2212 pdu
->hdr
.hlen
= sizeof(*pdu
);
2213 pdu
->hdr
.pdo
= req
->pdu_len
? pdu
->hdr
.hlen
+ hdgst
: 0;
2215 cpu_to_le32(pdu
->hdr
.hlen
+ hdgst
+ req
->pdu_len
+ ddgst
);
2217 ret
= nvme_tcp_map_data(queue
, rq
);
2218 if (unlikely(ret
)) {
2219 nvme_cleanup_cmd(rq
);
2220 dev_err(queue
->ctrl
->ctrl
.device
,
2221 "Failed to map data (%d)\n", ret
);
2228 static blk_status_t
nvme_tcp_queue_rq(struct blk_mq_hw_ctx
*hctx
,
2229 const struct blk_mq_queue_data
*bd
)
2231 struct nvme_ns
*ns
= hctx
->queue
->queuedata
;
2232 struct nvme_tcp_queue
*queue
= hctx
->driver_data
;
2233 struct request
*rq
= bd
->rq
;
2234 struct nvme_tcp_request
*req
= blk_mq_rq_to_pdu(rq
);
2235 bool queue_ready
= test_bit(NVME_TCP_Q_LIVE
, &queue
->flags
);
2238 if (!nvmf_check_ready(&queue
->ctrl
->ctrl
, rq
, queue_ready
))
2239 return nvmf_fail_nonready_command(&queue
->ctrl
->ctrl
, rq
);
2241 ret
= nvme_tcp_setup_cmd_pdu(ns
, rq
);
2245 blk_mq_start_request(rq
);
2247 nvme_tcp_queue_request(req
);
2252 static int nvme_tcp_map_queues(struct blk_mq_tag_set
*set
)
2254 struct nvme_tcp_ctrl
*ctrl
= set
->driver_data
;
2255 struct nvmf_ctrl_options
*opts
= ctrl
->ctrl
.opts
;
2257 if (opts
->nr_write_queues
&& ctrl
->io_queues
[HCTX_TYPE_READ
]) {
2258 /* separate read/write queues */
2259 set
->map
[HCTX_TYPE_DEFAULT
].nr_queues
=
2260 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
2261 set
->map
[HCTX_TYPE_DEFAULT
].queue_offset
= 0;
2262 set
->map
[HCTX_TYPE_READ
].nr_queues
=
2263 ctrl
->io_queues
[HCTX_TYPE_READ
];
2264 set
->map
[HCTX_TYPE_READ
].queue_offset
=
2265 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
2267 /* shared read/write queues */
2268 set
->map
[HCTX_TYPE_DEFAULT
].nr_queues
=
2269 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
2270 set
->map
[HCTX_TYPE_DEFAULT
].queue_offset
= 0;
2271 set
->map
[HCTX_TYPE_READ
].nr_queues
=
2272 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
];
2273 set
->map
[HCTX_TYPE_READ
].queue_offset
= 0;
2275 blk_mq_map_queues(&set
->map
[HCTX_TYPE_DEFAULT
]);
2276 blk_mq_map_queues(&set
->map
[HCTX_TYPE_READ
]);
2278 if (opts
->nr_poll_queues
&& ctrl
->io_queues
[HCTX_TYPE_POLL
]) {
2279 /* map dedicated poll queues only if we have queues left */
2280 set
->map
[HCTX_TYPE_POLL
].nr_queues
=
2281 ctrl
->io_queues
[HCTX_TYPE_POLL
];
2282 set
->map
[HCTX_TYPE_POLL
].queue_offset
=
2283 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
] +
2284 ctrl
->io_queues
[HCTX_TYPE_READ
];
2285 blk_mq_map_queues(&set
->map
[HCTX_TYPE_POLL
]);
2288 dev_info(ctrl
->ctrl
.device
,
2289 "mapped %d/%d/%d default/read/poll queues.\n",
2290 ctrl
->io_queues
[HCTX_TYPE_DEFAULT
],
2291 ctrl
->io_queues
[HCTX_TYPE_READ
],
2292 ctrl
->io_queues
[HCTX_TYPE_POLL
]);
2297 static int nvme_tcp_poll(struct blk_mq_hw_ctx
*hctx
)
2299 struct nvme_tcp_queue
*queue
= hctx
->driver_data
;
2300 struct sock
*sk
= queue
->sock
->sk
;
2302 if (!test_bit(NVME_TCP_Q_LIVE
, &queue
->flags
))
2305 if (sk_can_busy_loop(sk
) && skb_queue_empty_lockless(&sk
->sk_receive_queue
))
2306 sk_busy_loop(sk
, true);
2307 nvme_tcp_try_recv(queue
);
2308 return queue
->nr_cqe
;
2311 static struct blk_mq_ops nvme_tcp_mq_ops
= {
2312 .queue_rq
= nvme_tcp_queue_rq
,
2313 .complete
= nvme_complete_rq
,
2314 .init_request
= nvme_tcp_init_request
,
2315 .exit_request
= nvme_tcp_exit_request
,
2316 .init_hctx
= nvme_tcp_init_hctx
,
2317 .timeout
= nvme_tcp_timeout
,
2318 .map_queues
= nvme_tcp_map_queues
,
2319 .poll
= nvme_tcp_poll
,
2322 static struct blk_mq_ops nvme_tcp_admin_mq_ops
= {
2323 .queue_rq
= nvme_tcp_queue_rq
,
2324 .complete
= nvme_complete_rq
,
2325 .init_request
= nvme_tcp_init_request
,
2326 .exit_request
= nvme_tcp_exit_request
,
2327 .init_hctx
= nvme_tcp_init_admin_hctx
,
2328 .timeout
= nvme_tcp_timeout
,
2331 static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops
= {
2333 .module
= THIS_MODULE
,
2334 .flags
= NVME_F_FABRICS
,
2335 .reg_read32
= nvmf_reg_read32
,
2336 .reg_read64
= nvmf_reg_read64
,
2337 .reg_write32
= nvmf_reg_write32
,
2338 .free_ctrl
= nvme_tcp_free_ctrl
,
2339 .submit_async_event
= nvme_tcp_submit_async_event
,
2340 .delete_ctrl
= nvme_tcp_delete_ctrl
,
2341 .get_address
= nvmf_get_address
,
2345 nvme_tcp_existing_controller(struct nvmf_ctrl_options
*opts
)
2347 struct nvme_tcp_ctrl
*ctrl
;
2350 mutex_lock(&nvme_tcp_ctrl_mutex
);
2351 list_for_each_entry(ctrl
, &nvme_tcp_ctrl_list
, list
) {
2352 found
= nvmf_ip_options_match(&ctrl
->ctrl
, opts
);
2356 mutex_unlock(&nvme_tcp_ctrl_mutex
);
2361 static struct nvme_ctrl
*nvme_tcp_create_ctrl(struct device
*dev
,
2362 struct nvmf_ctrl_options
*opts
)
2364 struct nvme_tcp_ctrl
*ctrl
;
2367 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
2369 return ERR_PTR(-ENOMEM
);
2371 INIT_LIST_HEAD(&ctrl
->list
);
2372 ctrl
->ctrl
.opts
= opts
;
2373 ctrl
->ctrl
.queue_count
= opts
->nr_io_queues
+ opts
->nr_write_queues
+
2374 opts
->nr_poll_queues
+ 1;
2375 ctrl
->ctrl
.sqsize
= opts
->queue_size
- 1;
2376 ctrl
->ctrl
.kato
= opts
->kato
;
2378 INIT_DELAYED_WORK(&ctrl
->connect_work
,
2379 nvme_tcp_reconnect_ctrl_work
);
2380 INIT_WORK(&ctrl
->err_work
, nvme_tcp_error_recovery_work
);
2381 INIT_WORK(&ctrl
->ctrl
.reset_work
, nvme_reset_ctrl_work
);
2383 if (!(opts
->mask
& NVMF_OPT_TRSVCID
)) {
2385 kstrdup(__stringify(NVME_TCP_DISC_PORT
), GFP_KERNEL
);
2386 if (!opts
->trsvcid
) {
2390 opts
->mask
|= NVMF_OPT_TRSVCID
;
2393 ret
= inet_pton_with_scope(&init_net
, AF_UNSPEC
,
2394 opts
->traddr
, opts
->trsvcid
, &ctrl
->addr
);
2396 pr_err("malformed address passed: %s:%s\n",
2397 opts
->traddr
, opts
->trsvcid
);
2401 if (opts
->mask
& NVMF_OPT_HOST_TRADDR
) {
2402 ret
= inet_pton_with_scope(&init_net
, AF_UNSPEC
,
2403 opts
->host_traddr
, NULL
, &ctrl
->src_addr
);
2405 pr_err("malformed src address passed: %s\n",
2411 if (!opts
->duplicate_connect
&& nvme_tcp_existing_controller(opts
)) {
2416 ctrl
->queues
= kcalloc(ctrl
->ctrl
.queue_count
, sizeof(*ctrl
->queues
),
2418 if (!ctrl
->queues
) {
2423 ret
= nvme_init_ctrl(&ctrl
->ctrl
, dev
, &nvme_tcp_ctrl_ops
, 0);
2425 goto out_kfree_queues
;
2427 if (!nvme_change_ctrl_state(&ctrl
->ctrl
, NVME_CTRL_CONNECTING
)) {
2430 goto out_uninit_ctrl
;
2433 ret
= nvme_tcp_setup_ctrl(&ctrl
->ctrl
, true);
2435 goto out_uninit_ctrl
;
2437 dev_info(ctrl
->ctrl
.device
, "new ctrl: NQN \"%s\", addr %pISp\n",
2438 ctrl
->ctrl
.opts
->subsysnqn
, &ctrl
->addr
);
2440 mutex_lock(&nvme_tcp_ctrl_mutex
);
2441 list_add_tail(&ctrl
->list
, &nvme_tcp_ctrl_list
);
2442 mutex_unlock(&nvme_tcp_ctrl_mutex
);
2447 nvme_uninit_ctrl(&ctrl
->ctrl
);
2448 nvme_put_ctrl(&ctrl
->ctrl
);
2451 return ERR_PTR(ret
);
2453 kfree(ctrl
->queues
);
2456 return ERR_PTR(ret
);
2459 static struct nvmf_transport_ops nvme_tcp_transport
= {
2461 .module
= THIS_MODULE
,
2462 .required_opts
= NVMF_OPT_TRADDR
,
2463 .allowed_opts
= NVMF_OPT_TRSVCID
| NVMF_OPT_RECONNECT_DELAY
|
2464 NVMF_OPT_HOST_TRADDR
| NVMF_OPT_CTRL_LOSS_TMO
|
2465 NVMF_OPT_HDR_DIGEST
| NVMF_OPT_DATA_DIGEST
|
2466 NVMF_OPT_NR_WRITE_QUEUES
| NVMF_OPT_NR_POLL_QUEUES
|
2468 .create_ctrl
= nvme_tcp_create_ctrl
,
2471 static int __init
nvme_tcp_init_module(void)
2473 nvme_tcp_wq
= alloc_workqueue("nvme_tcp_wq",
2474 WQ_MEM_RECLAIM
| WQ_HIGHPRI
, 0);
2478 nvmf_register_transport(&nvme_tcp_transport
);
2482 static void __exit
nvme_tcp_cleanup_module(void)
2484 struct nvme_tcp_ctrl
*ctrl
;
2486 nvmf_unregister_transport(&nvme_tcp_transport
);
2488 mutex_lock(&nvme_tcp_ctrl_mutex
);
2489 list_for_each_entry(ctrl
, &nvme_tcp_ctrl_list
, list
)
2490 nvme_delete_ctrl(&ctrl
->ctrl
);
2491 mutex_unlock(&nvme_tcp_ctrl_mutex
);
2492 flush_workqueue(nvme_delete_wq
);
2494 destroy_workqueue(nvme_tcp_wq
);
2497 module_init(nvme_tcp_init_module
);
2498 module_exit(nvme_tcp_cleanup_module
);
2500 MODULE_LICENSE("GPL v2");