]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/nvme/host/tcp.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / drivers / nvme / host / tcp.c
CommitLineData
3f2304f8
SG
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * NVMe over Fabrics TCP host.
4 * Copyright (c) 2018 Lightbits Labs. All rights reserved.
5 */
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>
12#include <net/sock.h>
13#include <net/tcp.h>
14#include <linux/blk-mq.h>
15#include <crypto/hash.h>
16
17#include "nvme.h"
18#include "fabrics.h"
19
20struct nvme_tcp_queue;
21
22enum nvme_tcp_send_state {
23 NVME_TCP_SEND_CMD_PDU = 0,
24 NVME_TCP_SEND_H2C_PDU,
25 NVME_TCP_SEND_DATA,
26 NVME_TCP_SEND_DDGST,
27};
28
29struct nvme_tcp_request {
30 struct nvme_request req;
31 void *pdu;
32 struct nvme_tcp_queue *queue;
33 u32 data_len;
34 u32 pdu_len;
35 u32 pdu_sent;
36 u16 ttag;
37 struct list_head entry;
a7273d40 38 __le32 ddgst;
3f2304f8
SG
39
40 struct bio *curr_bio;
41 struct iov_iter iter;
42
43 /* send state */
44 size_t offset;
45 size_t data_sent;
46 enum nvme_tcp_send_state state;
47};
48
49enum nvme_tcp_queue_flags {
50 NVME_TCP_Q_ALLOCATED = 0,
51 NVME_TCP_Q_LIVE = 1,
52};
53
54enum nvme_tcp_recv_state {
55 NVME_TCP_RECV_PDU = 0,
56 NVME_TCP_RECV_DATA,
57 NVME_TCP_RECV_DDGST,
58};
59
60struct nvme_tcp_ctrl;
61struct nvme_tcp_queue {
62 struct socket *sock;
63 struct work_struct io_work;
64 int io_cpu;
65
66 spinlock_t lock;
67 struct list_head send_list;
68
69 /* recv state */
70 void *pdu;
71 int pdu_remaining;
72 int pdu_offset;
73 size_t data_remaining;
74 size_t ddgst_remaining;
75
76 /* send state */
77 struct nvme_tcp_request *request;
78
79 int queue_size;
80 size_t cmnd_capsule_len;
81 struct nvme_tcp_ctrl *ctrl;
82 unsigned long flags;
83 bool rd_enabled;
84
85 bool hdr_digest;
86 bool data_digest;
87 struct ahash_request *rcv_hash;
88 struct ahash_request *snd_hash;
89 __le32 exp_ddgst;
90 __le32 recv_ddgst;
91
92 struct page_frag_cache pf_cache;
93
94 void (*state_change)(struct sock *);
95 void (*data_ready)(struct sock *);
96 void (*write_space)(struct sock *);
97};
98
99struct nvme_tcp_ctrl {
100 /* read only in the hot path */
101 struct nvme_tcp_queue *queues;
102 struct blk_mq_tag_set tag_set;
103
104 /* other member variables */
105 struct list_head list;
106 struct blk_mq_tag_set admin_tag_set;
107 struct sockaddr_storage addr;
108 struct sockaddr_storage src_addr;
109 struct nvme_ctrl ctrl;
110
111 struct work_struct err_work;
112 struct delayed_work connect_work;
113 struct nvme_tcp_request async_req;
64861993 114 u32 io_queues[HCTX_MAX_TYPES];
3f2304f8
SG
115};
116
117static LIST_HEAD(nvme_tcp_ctrl_list);
118static DEFINE_MUTEX(nvme_tcp_ctrl_mutex);
119static struct workqueue_struct *nvme_tcp_wq;
120static struct blk_mq_ops nvme_tcp_mq_ops;
121static struct blk_mq_ops nvme_tcp_admin_mq_ops;
122
123static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl)
124{
125 return container_of(ctrl, struct nvme_tcp_ctrl, ctrl);
126}
127
128static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
129{
130 return queue - queue->ctrl->queues;
131}
132
133static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue)
134{
135 u32 queue_idx = nvme_tcp_queue_id(queue);
136
137 if (queue_idx == 0)
138 return queue->ctrl->admin_tag_set.tags[queue_idx];
139 return queue->ctrl->tag_set.tags[queue_idx - 1];
140}
141
142static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue)
143{
144 return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
145}
146
147static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue)
148{
149 return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
150}
151
152static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_queue *queue)
153{
154 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
155}
156
157static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req)
158{
159 return req == &req->queue->ctrl->async_req;
160}
161
162static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req)
163{
164 struct request *rq;
165 unsigned int bytes;
166
167 if (unlikely(nvme_tcp_async_req(req)))
168 return false; /* async events don't have a request */
169
170 rq = blk_mq_rq_from_pdu(req);
171 bytes = blk_rq_payload_bytes(rq);
172
173 return rq_data_dir(rq) == WRITE && bytes &&
174 bytes <= nvme_tcp_inline_data_size(req->queue);
175}
176
177static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req)
178{
179 return req->iter.bvec->bv_page;
180}
181
182static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req)
183{
184 return req->iter.bvec->bv_offset + req->iter.iov_offset;
185}
186
187static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req)
188{
189 return min_t(size_t, req->iter.bvec->bv_len - req->iter.iov_offset,
190 req->pdu_len - req->pdu_sent);
191}
192
193static inline size_t nvme_tcp_req_offset(struct nvme_tcp_request *req)
194{
195 return req->iter.iov_offset;
196}
197
198static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req)
199{
200 return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ?
201 req->pdu_len - req->pdu_sent : 0;
202}
203
204static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
205 int len)
206{
207 return nvme_tcp_pdu_data_left(req) <= len;
208}
209
210static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
211 unsigned int dir)
212{
213 struct request *rq = blk_mq_rq_from_pdu(req);
214 struct bio_vec *vec;
215 unsigned int size;
216 int nsegs;
217 size_t offset;
218
219 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
220 vec = &rq->special_vec;
221 nsegs = 1;
222 size = blk_rq_payload_bytes(rq);
223 offset = 0;
224 } else {
225 struct bio *bio = req->curr_bio;
226
227 vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
228 nsegs = bio_segments(bio);
229 size = bio->bi_iter.bi_size;
230 offset = bio->bi_iter.bi_bvec_done;
231 }
232
233 iov_iter_bvec(&req->iter, dir, vec, nsegs, size);
234 req->iter.iov_offset = offset;
235}
236
237static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req,
238 int len)
239{
240 req->data_sent += len;
241 req->pdu_sent += len;
242 iov_iter_advance(&req->iter, len);
243 if (!iov_iter_count(&req->iter) &&
244 req->data_sent < req->data_len) {
245 req->curr_bio = req->curr_bio->bi_next;
246 nvme_tcp_init_iter(req, WRITE);
247 }
248}
249
250static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req)
251{
252 struct nvme_tcp_queue *queue = req->queue;
253
254 spin_lock(&queue->lock);
255 list_add_tail(&req->entry, &queue->send_list);
256 spin_unlock(&queue->lock);
257
258 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
259}
260
261static inline struct nvme_tcp_request *
262nvme_tcp_fetch_request(struct nvme_tcp_queue *queue)
263{
264 struct nvme_tcp_request *req;
265
266 spin_lock(&queue->lock);
267 req = list_first_entry_or_null(&queue->send_list,
268 struct nvme_tcp_request, entry);
269 if (req)
270 list_del(&req->entry);
271 spin_unlock(&queue->lock);
272
273 return req;
274}
275
a7273d40
CH
276static inline void nvme_tcp_ddgst_final(struct ahash_request *hash,
277 __le32 *dgst)
3f2304f8
SG
278{
279 ahash_request_set_crypt(hash, NULL, (u8 *)dgst, 0);
280 crypto_ahash_final(hash);
281}
282
283static inline void nvme_tcp_ddgst_update(struct ahash_request *hash,
284 struct page *page, off_t off, size_t len)
285{
286 struct scatterlist sg;
287
288 sg_init_marker(&sg, 1);
289 sg_set_page(&sg, page, len, off);
290 ahash_request_set_crypt(hash, &sg, NULL, len);
291 crypto_ahash_update(hash);
292}
293
294static inline void nvme_tcp_hdgst(struct ahash_request *hash,
295 void *pdu, size_t len)
296{
297 struct scatterlist sg;
298
299 sg_init_one(&sg, pdu, len);
300 ahash_request_set_crypt(hash, &sg, pdu + len, len);
301 crypto_ahash_digest(hash);
302}
303
304static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue,
305 void *pdu, size_t pdu_len)
306{
307 struct nvme_tcp_hdr *hdr = pdu;
308 __le32 recv_digest;
309 __le32 exp_digest;
310
311 if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
312 dev_err(queue->ctrl->ctrl.device,
313 "queue %d: header digest flag is cleared\n",
314 nvme_tcp_queue_id(queue));
315 return -EPROTO;
316 }
317
318 recv_digest = *(__le32 *)(pdu + hdr->hlen);
319 nvme_tcp_hdgst(queue->rcv_hash, pdu, pdu_len);
320 exp_digest = *(__le32 *)(pdu + hdr->hlen);
321 if (recv_digest != exp_digest) {
322 dev_err(queue->ctrl->ctrl.device,
323 "header digest error: recv %#x expected %#x\n",
324 le32_to_cpu(recv_digest), le32_to_cpu(exp_digest));
325 return -EIO;
326 }
327
328 return 0;
329}
330
331static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu)
332{
333 struct nvme_tcp_hdr *hdr = pdu;
334 u8 digest_len = nvme_tcp_hdgst_len(queue);
335 u32 len;
336
337 len = le32_to_cpu(hdr->plen) - hdr->hlen -
338 ((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0);
339
340 if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
341 dev_err(queue->ctrl->ctrl.device,
342 "queue %d: data digest flag is cleared\n",
343 nvme_tcp_queue_id(queue));
344 return -EPROTO;
345 }
346 crypto_ahash_init(queue->rcv_hash);
347
348 return 0;
349}
350
351static void nvme_tcp_exit_request(struct blk_mq_tag_set *set,
352 struct request *rq, unsigned int hctx_idx)
353{
354 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
355
356 page_frag_free(req->pdu);
357}
358
359static int nvme_tcp_init_request(struct blk_mq_tag_set *set,
360 struct request *rq, unsigned int hctx_idx,
361 unsigned int numa_node)
362{
363 struct nvme_tcp_ctrl *ctrl = set->driver_data;
364 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
365 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
366 struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx];
367 u8 hdgst = nvme_tcp_hdgst_len(queue);
368
369 req->pdu = page_frag_alloc(&queue->pf_cache,
370 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
371 GFP_KERNEL | __GFP_ZERO);
372 if (!req->pdu)
373 return -ENOMEM;
374
375 req->queue = queue;
376 nvme_req(rq)->ctrl = &ctrl->ctrl;
377
378 return 0;
379}
380
381static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
382 unsigned int hctx_idx)
383{
384 struct nvme_tcp_ctrl *ctrl = data;
385 struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1];
386
387 hctx->driver_data = queue;
388 return 0;
389}
390
391static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
392 unsigned int hctx_idx)
393{
394 struct nvme_tcp_ctrl *ctrl = data;
395 struct nvme_tcp_queue *queue = &ctrl->queues[0];
396
397 hctx->driver_data = queue;
398 return 0;
399}
400
401static enum nvme_tcp_recv_state
402nvme_tcp_recv_state(struct nvme_tcp_queue *queue)
403{
404 return (queue->pdu_remaining) ? NVME_TCP_RECV_PDU :
405 (queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST :
406 NVME_TCP_RECV_DATA;
407}
408
409static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
410{
411 queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) +
412 nvme_tcp_hdgst_len(queue);
413 queue->pdu_offset = 0;
414 queue->data_remaining = -1;
415 queue->ddgst_remaining = 0;
416}
417
418static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
419{
420 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
421 return;
422
423 queue_work(nvme_wq, &to_tcp_ctrl(ctrl)->err_work);
424}
425
426static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue,
427 struct nvme_completion *cqe)
428{
429 struct request *rq;
430
431 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), cqe->command_id);
432 if (!rq) {
433 dev_err(queue->ctrl->ctrl.device,
434 "queue %d tag 0x%x not found\n",
435 nvme_tcp_queue_id(queue), cqe->command_id);
436 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
437 return -EINVAL;
438 }
439
440 nvme_end_request(rq, cqe->status, cqe->result);
441
442 return 0;
443}
444
445static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
446 struct nvme_tcp_data_pdu *pdu)
447{
448 struct request *rq;
449
450 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
451 if (!rq) {
452 dev_err(queue->ctrl->ctrl.device,
453 "queue %d tag %#x not found\n",
454 nvme_tcp_queue_id(queue), pdu->command_id);
455 return -ENOENT;
456 }
457
458 if (!blk_rq_payload_bytes(rq)) {
459 dev_err(queue->ctrl->ctrl.device,
460 "queue %d tag %#x unexpected data\n",
461 nvme_tcp_queue_id(queue), rq->tag);
462 return -EIO;
463 }
464
465 queue->data_remaining = le32_to_cpu(pdu->data_length);
466
602d674c
SG
467 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS &&
468 unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) {
469 dev_err(queue->ctrl->ctrl.device,
470 "queue %d tag %#x SUCCESS set but not last PDU\n",
471 nvme_tcp_queue_id(queue), rq->tag);
472 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
473 return -EPROTO;
474 }
475
3f2304f8 476 return 0;
3f2304f8
SG
477}
478
479static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue,
480 struct nvme_tcp_rsp_pdu *pdu)
481{
482 struct nvme_completion *cqe = &pdu->cqe;
483 int ret = 0;
484
485 /*
486 * AEN requests are special as they don't time out and can
487 * survive any kind of queue freeze and often don't respond to
488 * aborts. We don't even bother to allocate a struct request
489 * for them but rather special case them here.
490 */
491 if (unlikely(nvme_tcp_queue_id(queue) == 0 &&
492 cqe->command_id >= NVME_AQ_BLK_MQ_DEPTH))
493 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
494 &cqe->result);
495 else
496 ret = nvme_tcp_process_nvme_cqe(queue, cqe);
497
498 return ret;
499}
500
501static int nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req,
502 struct nvme_tcp_r2t_pdu *pdu)
503{
504 struct nvme_tcp_data_pdu *data = req->pdu;
505 struct nvme_tcp_queue *queue = req->queue;
506 struct request *rq = blk_mq_rq_from_pdu(req);
507 u8 hdgst = nvme_tcp_hdgst_len(queue);
508 u8 ddgst = nvme_tcp_ddgst_len(queue);
509
510 req->pdu_len = le32_to_cpu(pdu->r2t_length);
511 req->pdu_sent = 0;
512
513 if (unlikely(req->data_sent + req->pdu_len > req->data_len)) {
514 dev_err(queue->ctrl->ctrl.device,
515 "req %d r2t len %u exceeded data len %u (%zu sent)\n",
516 rq->tag, req->pdu_len, req->data_len,
517 req->data_sent);
518 return -EPROTO;
519 }
520
521 if (unlikely(le32_to_cpu(pdu->r2t_offset) < req->data_sent)) {
522 dev_err(queue->ctrl->ctrl.device,
523 "req %d unexpected r2t offset %u (expected %zu)\n",
524 rq->tag, le32_to_cpu(pdu->r2t_offset),
525 req->data_sent);
526 return -EPROTO;
527 }
528
529 memset(data, 0, sizeof(*data));
530 data->hdr.type = nvme_tcp_h2c_data;
531 data->hdr.flags = NVME_TCP_F_DATA_LAST;
532 if (queue->hdr_digest)
533 data->hdr.flags |= NVME_TCP_F_HDGST;
534 if (queue->data_digest)
535 data->hdr.flags |= NVME_TCP_F_DDGST;
536 data->hdr.hlen = sizeof(*data);
537 data->hdr.pdo = data->hdr.hlen + hdgst;
538 data->hdr.plen =
539 cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst);
540 data->ttag = pdu->ttag;
541 data->command_id = rq->tag;
542 data->data_offset = cpu_to_le32(req->data_sent);
543 data->data_length = cpu_to_le32(req->pdu_len);
544 return 0;
545}
546
547static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
548 struct nvme_tcp_r2t_pdu *pdu)
549{
550 struct nvme_tcp_request *req;
551 struct request *rq;
552 int ret;
553
554 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
555 if (!rq) {
556 dev_err(queue->ctrl->ctrl.device,
557 "queue %d tag %#x not found\n",
558 nvme_tcp_queue_id(queue), pdu->command_id);
559 return -ENOENT;
560 }
561 req = blk_mq_rq_to_pdu(rq);
562
563 ret = nvme_tcp_setup_h2c_data_pdu(req, pdu);
564 if (unlikely(ret))
565 return ret;
566
567 req->state = NVME_TCP_SEND_H2C_PDU;
568 req->offset = 0;
569
570 nvme_tcp_queue_request(req);
571
572 return 0;
573}
574
575static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
576 unsigned int *offset, size_t *len)
577{
578 struct nvme_tcp_hdr *hdr;
579 char *pdu = queue->pdu;
580 size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
581 int ret;
582
583 ret = skb_copy_bits(skb, *offset,
584 &pdu[queue->pdu_offset], rcv_len);
585 if (unlikely(ret))
586 return ret;
587
588 queue->pdu_remaining -= rcv_len;
589 queue->pdu_offset += rcv_len;
590 *offset += rcv_len;
591 *len -= rcv_len;
592 if (queue->pdu_remaining)
593 return 0;
594
595 hdr = queue->pdu;
596 if (queue->hdr_digest) {
597 ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen);
598 if (unlikely(ret))
599 return ret;
600 }
601
602
603 if (queue->data_digest) {
604 ret = nvme_tcp_check_ddgst(queue, queue->pdu);
605 if (unlikely(ret))
606 return ret;
607 }
608
609 switch (hdr->type) {
610 case nvme_tcp_c2h_data:
611 ret = nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu);
612 break;
613 case nvme_tcp_rsp:
614 nvme_tcp_init_recv_ctx(queue);
615 ret = nvme_tcp_handle_comp(queue, (void *)queue->pdu);
616 break;
617 case nvme_tcp_r2t:
618 nvme_tcp_init_recv_ctx(queue);
619 ret = nvme_tcp_handle_r2t(queue, (void *)queue->pdu);
620 break;
621 default:
622 dev_err(queue->ctrl->ctrl.device,
623 "unsupported pdu type (%d)\n", hdr->type);
624 return -EINVAL;
625 }
626
627 return ret;
628}
629
988aef9e 630static inline void nvme_tcp_end_request(struct request *rq, u16 status)
602d674c
SG
631{
632 union nvme_result res = {};
633
634 nvme_end_request(rq, cpu_to_le16(status << 1), res);
635}
636
3f2304f8
SG
637static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb,
638 unsigned int *offset, size_t *len)
639{
640 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
641 struct nvme_tcp_request *req;
642 struct request *rq;
643
644 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
645 if (!rq) {
646 dev_err(queue->ctrl->ctrl.device,
647 "queue %d tag %#x not found\n",
648 nvme_tcp_queue_id(queue), pdu->command_id);
649 return -ENOENT;
650 }
651 req = blk_mq_rq_to_pdu(rq);
652
653 while (true) {
654 int recv_len, ret;
655
656 recv_len = min_t(size_t, *len, queue->data_remaining);
657 if (!recv_len)
658 break;
659
660 if (!iov_iter_count(&req->iter)) {
661 req->curr_bio = req->curr_bio->bi_next;
662
663 /*
664 * If we don`t have any bios it means that controller
665 * sent more data than we requested, hence error
666 */
667 if (!req->curr_bio) {
668 dev_err(queue->ctrl->ctrl.device,
669 "queue %d no space in request %#x",
670 nvme_tcp_queue_id(queue), rq->tag);
671 nvme_tcp_init_recv_ctx(queue);
672 return -EIO;
673 }
674 nvme_tcp_init_iter(req, READ);
675 }
676
677 /* we can read only from what is left in this bio */
678 recv_len = min_t(size_t, recv_len,
679 iov_iter_count(&req->iter));
680
681 if (queue->data_digest)
682 ret = skb_copy_and_hash_datagram_iter(skb, *offset,
683 &req->iter, recv_len, queue->rcv_hash);
684 else
685 ret = skb_copy_datagram_iter(skb, *offset,
686 &req->iter, recv_len);
687 if (ret) {
688 dev_err(queue->ctrl->ctrl.device,
689 "queue %d failed to copy request %#x data",
690 nvme_tcp_queue_id(queue), rq->tag);
691 return ret;
692 }
693
694 *len -= recv_len;
695 *offset += recv_len;
696 queue->data_remaining -= recv_len;
697 }
698
699 if (!queue->data_remaining) {
700 if (queue->data_digest) {
701 nvme_tcp_ddgst_final(queue->rcv_hash, &queue->exp_ddgst);
702 queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH;
703 } else {
602d674c
SG
704 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS)
705 nvme_tcp_end_request(rq, NVME_SC_SUCCESS);
3f2304f8
SG
706 nvme_tcp_init_recv_ctx(queue);
707 }
708 }
709
710 return 0;
711}
712
713static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue,
714 struct sk_buff *skb, unsigned int *offset, size_t *len)
715{
602d674c 716 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
3f2304f8
SG
717 char *ddgst = (char *)&queue->recv_ddgst;
718 size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining);
719 off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining;
720 int ret;
721
722 ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len);
723 if (unlikely(ret))
724 return ret;
725
726 queue->ddgst_remaining -= recv_len;
727 *offset += recv_len;
728 *len -= recv_len;
729 if (queue->ddgst_remaining)
730 return 0;
731
732 if (queue->recv_ddgst != queue->exp_ddgst) {
733 dev_err(queue->ctrl->ctrl.device,
734 "data digest error: recv %#x expected %#x\n",
735 le32_to_cpu(queue->recv_ddgst),
736 le32_to_cpu(queue->exp_ddgst));
737 return -EIO;
738 }
739
602d674c
SG
740 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
741 struct request *rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue),
742 pdu->command_id);
743
744 nvme_tcp_end_request(rq, NVME_SC_SUCCESS);
745 }
746
3f2304f8
SG
747 nvme_tcp_init_recv_ctx(queue);
748 return 0;
749}
750
751static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,
752 unsigned int offset, size_t len)
753{
754 struct nvme_tcp_queue *queue = desc->arg.data;
755 size_t consumed = len;
756 int result;
757
758 while (len) {
759 switch (nvme_tcp_recv_state(queue)) {
760 case NVME_TCP_RECV_PDU:
761 result = nvme_tcp_recv_pdu(queue, skb, &offset, &len);
762 break;
763 case NVME_TCP_RECV_DATA:
764 result = nvme_tcp_recv_data(queue, skb, &offset, &len);
765 break;
766 case NVME_TCP_RECV_DDGST:
767 result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len);
768 break;
769 default:
770 result = -EFAULT;
771 }
772 if (result) {
773 dev_err(queue->ctrl->ctrl.device,
774 "receive failed: %d\n", result);
775 queue->rd_enabled = false;
776 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
777 return result;
778 }
779 }
780
781 return consumed;
782}
783
784static void nvme_tcp_data_ready(struct sock *sk)
785{
786 struct nvme_tcp_queue *queue;
787
788 read_lock(&sk->sk_callback_lock);
789 queue = sk->sk_user_data;
790 if (likely(queue && queue->rd_enabled))
791 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
792 read_unlock(&sk->sk_callback_lock);
793}
794
795static void nvme_tcp_write_space(struct sock *sk)
796{
797 struct nvme_tcp_queue *queue;
798
799 read_lock_bh(&sk->sk_callback_lock);
800 queue = sk->sk_user_data;
801 if (likely(queue && sk_stream_is_writeable(sk))) {
802 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
803 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
804 }
805 read_unlock_bh(&sk->sk_callback_lock);
806}
807
808static void nvme_tcp_state_change(struct sock *sk)
809{
810 struct nvme_tcp_queue *queue;
811
812 read_lock(&sk->sk_callback_lock);
813 queue = sk->sk_user_data;
814 if (!queue)
815 goto done;
816
817 switch (sk->sk_state) {
818 case TCP_CLOSE:
819 case TCP_CLOSE_WAIT:
820 case TCP_LAST_ACK:
821 case TCP_FIN_WAIT1:
822 case TCP_FIN_WAIT2:
823 /* fallthrough */
824 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
825 break;
826 default:
827 dev_info(queue->ctrl->ctrl.device,
828 "queue %d socket state %d\n",
829 nvme_tcp_queue_id(queue), sk->sk_state);
830 }
831
832 queue->state_change(sk);
833done:
834 read_unlock(&sk->sk_callback_lock);
835}
836
837static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue)
838{
839 queue->request = NULL;
840}
841
842static void nvme_tcp_fail_request(struct nvme_tcp_request *req)
843{
602d674c 844 nvme_tcp_end_request(blk_mq_rq_from_pdu(req), NVME_SC_DATA_XFER_ERROR);
3f2304f8
SG
845}
846
847static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
848{
849 struct nvme_tcp_queue *queue = req->queue;
850
851 while (true) {
852 struct page *page = nvme_tcp_req_cur_page(req);
853 size_t offset = nvme_tcp_req_cur_offset(req);
854 size_t len = nvme_tcp_req_cur_length(req);
855 bool last = nvme_tcp_pdu_last_send(req, len);
856 int ret, flags = MSG_DONTWAIT;
857
858 if (last && !queue->data_digest)
859 flags |= MSG_EOR;
860 else
861 flags |= MSG_MORE;
862
863 ret = kernel_sendpage(queue->sock, page, offset, len, flags);
864 if (ret <= 0)
865 return ret;
866
867 nvme_tcp_advance_req(req, ret);
868 if (queue->data_digest)
869 nvme_tcp_ddgst_update(queue->snd_hash, page,
870 offset, ret);
871
872 /* fully successful last write*/
873 if (last && ret == len) {
874 if (queue->data_digest) {
875 nvme_tcp_ddgst_final(queue->snd_hash,
876 &req->ddgst);
877 req->state = NVME_TCP_SEND_DDGST;
878 req->offset = 0;
879 } else {
880 nvme_tcp_done_send_req(queue);
881 }
882 return 1;
883 }
884 }
885 return -EAGAIN;
886}
887
888static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
889{
890 struct nvme_tcp_queue *queue = req->queue;
891 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
892 bool inline_data = nvme_tcp_has_inline_data(req);
893 int flags = MSG_DONTWAIT | (inline_data ? MSG_MORE : MSG_EOR);
894 u8 hdgst = nvme_tcp_hdgst_len(queue);
895 int len = sizeof(*pdu) + hdgst - req->offset;
896 int ret;
897
898 if (queue->hdr_digest && !req->offset)
899 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
900
901 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
902 offset_in_page(pdu) + req->offset, len, flags);
903 if (unlikely(ret <= 0))
904 return ret;
905
906 len -= ret;
907 if (!len) {
908 if (inline_data) {
909 req->state = NVME_TCP_SEND_DATA;
910 if (queue->data_digest)
911 crypto_ahash_init(queue->snd_hash);
912 nvme_tcp_init_iter(req, WRITE);
913 } else {
914 nvme_tcp_done_send_req(queue);
915 }
916 return 1;
917 }
918 req->offset += ret;
919
920 return -EAGAIN;
921}
922
923static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
924{
925 struct nvme_tcp_queue *queue = req->queue;
926 struct nvme_tcp_data_pdu *pdu = req->pdu;
927 u8 hdgst = nvme_tcp_hdgst_len(queue);
928 int len = sizeof(*pdu) - req->offset + hdgst;
929 int ret;
930
931 if (queue->hdr_digest && !req->offset)
932 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
933
934 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
935 offset_in_page(pdu) + req->offset, len,
936 MSG_DONTWAIT | MSG_MORE);
937 if (unlikely(ret <= 0))
938 return ret;
939
940 len -= ret;
941 if (!len) {
942 req->state = NVME_TCP_SEND_DATA;
943 if (queue->data_digest)
944 crypto_ahash_init(queue->snd_hash);
945 if (!req->data_sent)
946 nvme_tcp_init_iter(req, WRITE);
947 return 1;
948 }
949 req->offset += ret;
950
951 return -EAGAIN;
952}
953
954static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
955{
956 struct nvme_tcp_queue *queue = req->queue;
957 int ret;
958 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_EOR };
959 struct kvec iov = {
960 .iov_base = &req->ddgst + req->offset,
961 .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset
962 };
963
964 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
965 if (unlikely(ret <= 0))
966 return ret;
967
968 if (req->offset + ret == NVME_TCP_DIGEST_LENGTH) {
969 nvme_tcp_done_send_req(queue);
970 return 1;
971 }
972
973 req->offset += ret;
974 return -EAGAIN;
975}
976
977static int nvme_tcp_try_send(struct nvme_tcp_queue *queue)
978{
979 struct nvme_tcp_request *req;
980 int ret = 1;
981
982 if (!queue->request) {
983 queue->request = nvme_tcp_fetch_request(queue);
984 if (!queue->request)
985 return 0;
986 }
987 req = queue->request;
988
989 if (req->state == NVME_TCP_SEND_CMD_PDU) {
990 ret = nvme_tcp_try_send_cmd_pdu(req);
991 if (ret <= 0)
992 goto done;
993 if (!nvme_tcp_has_inline_data(req))
994 return ret;
995 }
996
997 if (req->state == NVME_TCP_SEND_H2C_PDU) {
998 ret = nvme_tcp_try_send_data_pdu(req);
999 if (ret <= 0)
1000 goto done;
1001 }
1002
1003 if (req->state == NVME_TCP_SEND_DATA) {
1004 ret = nvme_tcp_try_send_data(req);
1005 if (ret <= 0)
1006 goto done;
1007 }
1008
1009 if (req->state == NVME_TCP_SEND_DDGST)
1010 ret = nvme_tcp_try_send_ddgst(req);
1011done:
1012 if (ret == -EAGAIN)
1013 ret = 0;
1014 return ret;
1015}
1016
1017static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue)
1018{
1019 struct sock *sk = queue->sock->sk;
1020 read_descriptor_t rd_desc;
1021 int consumed;
1022
1023 rd_desc.arg.data = queue;
1024 rd_desc.count = 1;
1025 lock_sock(sk);
1026 consumed = tcp_read_sock(sk, &rd_desc, nvme_tcp_recv_skb);
1027 release_sock(sk);
1028 return consumed;
1029}
1030
1031static void nvme_tcp_io_work(struct work_struct *w)
1032{
1033 struct nvme_tcp_queue *queue =
1034 container_of(w, struct nvme_tcp_queue, io_work);
1035 unsigned long start = jiffies + msecs_to_jiffies(1);
1036
1037 do {
1038 bool pending = false;
1039 int result;
1040
1041 result = nvme_tcp_try_send(queue);
1042 if (result > 0) {
1043 pending = true;
1044 } else if (unlikely(result < 0)) {
1045 dev_err(queue->ctrl->ctrl.device,
1046 "failed to send request %d\n", result);
1047 if (result != -EPIPE)
1048 nvme_tcp_fail_request(queue->request);
1049 nvme_tcp_done_send_req(queue);
1050 return;
1051 }
1052
1053 result = nvme_tcp_try_recv(queue);
1054 if (result > 0)
1055 pending = true;
1056
1057 if (!pending)
1058 return;
1059
1060 } while (time_after(jiffies, start)); /* quota is exhausted */
1061
1062 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
1063}
1064
1065static void nvme_tcp_free_crypto(struct nvme_tcp_queue *queue)
1066{
1067 struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
1068
1069 ahash_request_free(queue->rcv_hash);
1070 ahash_request_free(queue->snd_hash);
1071 crypto_free_ahash(tfm);
1072}
1073
1074static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue *queue)
1075{
1076 struct crypto_ahash *tfm;
1077
1078 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
1079 if (IS_ERR(tfm))
1080 return PTR_ERR(tfm);
1081
1082 queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1083 if (!queue->snd_hash)
1084 goto free_tfm;
1085 ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
1086
1087 queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1088 if (!queue->rcv_hash)
1089 goto free_snd_hash;
1090 ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
1091
1092 return 0;
1093free_snd_hash:
1094 ahash_request_free(queue->snd_hash);
1095free_tfm:
1096 crypto_free_ahash(tfm);
1097 return -ENOMEM;
1098}
1099
1100static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl)
1101{
1102 struct nvme_tcp_request *async = &ctrl->async_req;
1103
1104 page_frag_free(async->pdu);
1105}
1106
1107static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
1108{
1109 struct nvme_tcp_queue *queue = &ctrl->queues[0];
1110 struct nvme_tcp_request *async = &ctrl->async_req;
1111 u8 hdgst = nvme_tcp_hdgst_len(queue);
1112
1113 async->pdu = page_frag_alloc(&queue->pf_cache,
1114 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
1115 GFP_KERNEL | __GFP_ZERO);
1116 if (!async->pdu)
1117 return -ENOMEM;
1118
1119 async->queue = &ctrl->queues[0];
1120 return 0;
1121}
1122
1123static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
1124{
1125 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1126 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1127
1128 if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
1129 return;
1130
1131 if (queue->hdr_digest || queue->data_digest)
1132 nvme_tcp_free_crypto(queue);
1133
1134 sock_release(queue->sock);
1135 kfree(queue->pdu);
1136}
1137
1138static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
1139{
1140 struct nvme_tcp_icreq_pdu *icreq;
1141 struct nvme_tcp_icresp_pdu *icresp;
1142 struct msghdr msg = {};
1143 struct kvec iov;
1144 bool ctrl_hdgst, ctrl_ddgst;
1145 int ret;
1146
1147 icreq = kzalloc(sizeof(*icreq), GFP_KERNEL);
1148 if (!icreq)
1149 return -ENOMEM;
1150
1151 icresp = kzalloc(sizeof(*icresp), GFP_KERNEL);
1152 if (!icresp) {
1153 ret = -ENOMEM;
1154 goto free_icreq;
1155 }
1156
1157 icreq->hdr.type = nvme_tcp_icreq;
1158 icreq->hdr.hlen = sizeof(*icreq);
1159 icreq->hdr.pdo = 0;
1160 icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen);
1161 icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
1162 icreq->maxr2t = 0; /* single inflight r2t supported */
1163 icreq->hpda = 0; /* no alignment constraint */
1164 if (queue->hdr_digest)
1165 icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
1166 if (queue->data_digest)
1167 icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
1168
1169 iov.iov_base = icreq;
1170 iov.iov_len = sizeof(*icreq);
1171 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1172 if (ret < 0)
1173 goto free_icresp;
1174
1175 memset(&msg, 0, sizeof(msg));
1176 iov.iov_base = icresp;
1177 iov.iov_len = sizeof(*icresp);
1178 ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1179 iov.iov_len, msg.msg_flags);
1180 if (ret < 0)
1181 goto free_icresp;
1182
1183 ret = -EINVAL;
1184 if (icresp->hdr.type != nvme_tcp_icresp) {
1185 pr_err("queue %d: bad type returned %d\n",
1186 nvme_tcp_queue_id(queue), icresp->hdr.type);
1187 goto free_icresp;
1188 }
1189
1190 if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) {
1191 pr_err("queue %d: bad pdu length returned %d\n",
1192 nvme_tcp_queue_id(queue), icresp->hdr.plen);
1193 goto free_icresp;
1194 }
1195
1196 if (icresp->pfv != NVME_TCP_PFV_1_0) {
1197 pr_err("queue %d: bad pfv returned %d\n",
1198 nvme_tcp_queue_id(queue), icresp->pfv);
1199 goto free_icresp;
1200 }
1201
1202 ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE);
1203 if ((queue->data_digest && !ctrl_ddgst) ||
1204 (!queue->data_digest && ctrl_ddgst)) {
1205 pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
1206 nvme_tcp_queue_id(queue),
1207 queue->data_digest ? "enabled" : "disabled",
1208 ctrl_ddgst ? "enabled" : "disabled");
1209 goto free_icresp;
1210 }
1211
1212 ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE);
1213 if ((queue->hdr_digest && !ctrl_hdgst) ||
1214 (!queue->hdr_digest && ctrl_hdgst)) {
1215 pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
1216 nvme_tcp_queue_id(queue),
1217 queue->hdr_digest ? "enabled" : "disabled",
1218 ctrl_hdgst ? "enabled" : "disabled");
1219 goto free_icresp;
1220 }
1221
1222 if (icresp->cpda != 0) {
1223 pr_err("queue %d: unsupported cpda returned %d\n",
1224 nvme_tcp_queue_id(queue), icresp->cpda);
1225 goto free_icresp;
1226 }
1227
1228 ret = 0;
1229free_icresp:
1230 kfree(icresp);
1231free_icreq:
1232 kfree(icreq);
1233 return ret;
1234}
1235
1236static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl,
1237 int qid, size_t queue_size)
1238{
1239 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1240 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1241 struct linger sol = { .l_onoff = 1, .l_linger = 0 };
873946f4 1242 int ret, opt, rcv_pdu_size, n;
3f2304f8
SG
1243
1244 queue->ctrl = ctrl;
1245 INIT_LIST_HEAD(&queue->send_list);
1246 spin_lock_init(&queue->lock);
1247 INIT_WORK(&queue->io_work, nvme_tcp_io_work);
1248 queue->queue_size = queue_size;
1249
1250 if (qid > 0)
1251 queue->cmnd_capsule_len = ctrl->ctrl.ioccsz * 16;
1252 else
1253 queue->cmnd_capsule_len = sizeof(struct nvme_command) +
1254 NVME_TCP_ADMIN_CCSZ;
1255
1256 ret = sock_create(ctrl->addr.ss_family, SOCK_STREAM,
1257 IPPROTO_TCP, &queue->sock);
1258 if (ret) {
1259 dev_err(ctrl->ctrl.device,
1260 "failed to create socket: %d\n", ret);
1261 return ret;
1262 }
1263
1264 /* Single syn retry */
1265 opt = 1;
1266 ret = kernel_setsockopt(queue->sock, IPPROTO_TCP, TCP_SYNCNT,
1267 (char *)&opt, sizeof(opt));
1268 if (ret) {
1269 dev_err(ctrl->ctrl.device,
1270 "failed to set TCP_SYNCNT sock opt %d\n", ret);
1271 goto err_sock;
1272 }
1273
1274 /* Set TCP no delay */
1275 opt = 1;
1276 ret = kernel_setsockopt(queue->sock, IPPROTO_TCP,
1277 TCP_NODELAY, (char *)&opt, sizeof(opt));
1278 if (ret) {
1279 dev_err(ctrl->ctrl.device,
1280 "failed to set TCP_NODELAY sock opt %d\n", ret);
1281 goto err_sock;
1282 }
1283
1284 /*
1285 * Cleanup whatever is sitting in the TCP transmit queue on socket
1286 * close. This is done to prevent stale data from being sent should
1287 * the network connection be restored before TCP times out.
1288 */
1289 ret = kernel_setsockopt(queue->sock, SOL_SOCKET, SO_LINGER,
1290 (char *)&sol, sizeof(sol));
1291 if (ret) {
1292 dev_err(ctrl->ctrl.device,
1293 "failed to set SO_LINGER sock opt %d\n", ret);
1294 goto err_sock;
1295 }
1296
1297 queue->sock->sk->sk_allocation = GFP_ATOMIC;
873946f4
SG
1298 if (!qid)
1299 n = 0;
1300 else
1301 n = (qid - 1) % num_online_cpus();
1302 queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
3f2304f8
SG
1303 queue->request = NULL;
1304 queue->data_remaining = 0;
1305 queue->ddgst_remaining = 0;
1306 queue->pdu_remaining = 0;
1307 queue->pdu_offset = 0;
1308 sk_set_memalloc(queue->sock->sk);
1309
1310 if (ctrl->ctrl.opts->mask & NVMF_OPT_HOST_TRADDR) {
1311 ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr,
1312 sizeof(ctrl->src_addr));
1313 if (ret) {
1314 dev_err(ctrl->ctrl.device,
1315 "failed to bind queue %d socket %d\n",
1316 qid, ret);
1317 goto err_sock;
1318 }
1319 }
1320
1321 queue->hdr_digest = nctrl->opts->hdr_digest;
1322 queue->data_digest = nctrl->opts->data_digest;
1323 if (queue->hdr_digest || queue->data_digest) {
1324 ret = nvme_tcp_alloc_crypto(queue);
1325 if (ret) {
1326 dev_err(ctrl->ctrl.device,
1327 "failed to allocate queue %d crypto\n", qid);
1328 goto err_sock;
1329 }
1330 }
1331
1332 rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) +
1333 nvme_tcp_hdgst_len(queue);
1334 queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL);
1335 if (!queue->pdu) {
1336 ret = -ENOMEM;
1337 goto err_crypto;
1338 }
1339
1340 dev_dbg(ctrl->ctrl.device, "connecting queue %d\n",
1341 nvme_tcp_queue_id(queue));
1342
1343 ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr,
1344 sizeof(ctrl->addr), 0);
1345 if (ret) {
1346 dev_err(ctrl->ctrl.device,
1347 "failed to connect socket: %d\n", ret);
1348 goto err_rcv_pdu;
1349 }
1350
1351 ret = nvme_tcp_init_connection(queue);
1352 if (ret)
1353 goto err_init_connect;
1354
1355 queue->rd_enabled = true;
1356 set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags);
1357 nvme_tcp_init_recv_ctx(queue);
1358
1359 write_lock_bh(&queue->sock->sk->sk_callback_lock);
1360 queue->sock->sk->sk_user_data = queue;
1361 queue->state_change = queue->sock->sk->sk_state_change;
1362 queue->data_ready = queue->sock->sk->sk_data_ready;
1363 queue->write_space = queue->sock->sk->sk_write_space;
1364 queue->sock->sk->sk_data_ready = nvme_tcp_data_ready;
1365 queue->sock->sk->sk_state_change = nvme_tcp_state_change;
1366 queue->sock->sk->sk_write_space = nvme_tcp_write_space;
1367 write_unlock_bh(&queue->sock->sk->sk_callback_lock);
1368
1369 return 0;
1370
1371err_init_connect:
1372 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1373err_rcv_pdu:
1374 kfree(queue->pdu);
1375err_crypto:
1376 if (queue->hdr_digest || queue->data_digest)
1377 nvme_tcp_free_crypto(queue);
1378err_sock:
1379 sock_release(queue->sock);
1380 queue->sock = NULL;
1381 return ret;
1382}
1383
1384static void nvme_tcp_restore_sock_calls(struct nvme_tcp_queue *queue)
1385{
1386 struct socket *sock = queue->sock;
1387
1388 write_lock_bh(&sock->sk->sk_callback_lock);
1389 sock->sk->sk_user_data = NULL;
1390 sock->sk->sk_data_ready = queue->data_ready;
1391 sock->sk->sk_state_change = queue->state_change;
1392 sock->sk->sk_write_space = queue->write_space;
1393 write_unlock_bh(&sock->sk->sk_callback_lock);
1394}
1395
1396static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
1397{
1398 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1399 nvme_tcp_restore_sock_calls(queue);
1400 cancel_work_sync(&queue->io_work);
1401}
1402
1403static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
1404{
1405 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1406 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1407
1408 if (!test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
1409 return;
1410
1411 __nvme_tcp_stop_queue(queue);
1412}
1413
1414static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
1415{
1416 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1417 int ret;
1418
1419 if (idx)
26c68227 1420 ret = nvmf_connect_io_queue(nctrl, idx, false);
3f2304f8
SG
1421 else
1422 ret = nvmf_connect_admin_queue(nctrl);
1423
1424 if (!ret) {
1425 set_bit(NVME_TCP_Q_LIVE, &ctrl->queues[idx].flags);
1426 } else {
f34e2589
SG
1427 if (test_bit(NVME_TCP_Q_ALLOCATED, &ctrl->queues[idx].flags))
1428 __nvme_tcp_stop_queue(&ctrl->queues[idx]);
3f2304f8
SG
1429 dev_err(nctrl->device,
1430 "failed to connect queue: %d ret=%d\n", idx, ret);
1431 }
1432 return ret;
1433}
1434
1435static struct blk_mq_tag_set *nvme_tcp_alloc_tagset(struct nvme_ctrl *nctrl,
1436 bool admin)
1437{
1438 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1439 struct blk_mq_tag_set *set;
1440 int ret;
1441
1442 if (admin) {
1443 set = &ctrl->admin_tag_set;
1444 memset(set, 0, sizeof(*set));
1445 set->ops = &nvme_tcp_admin_mq_ops;
1446 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
1447 set->reserved_tags = 2; /* connect + keep-alive */
1448 set->numa_node = NUMA_NO_NODE;
1449 set->cmd_size = sizeof(struct nvme_tcp_request);
1450 set->driver_data = ctrl;
1451 set->nr_hw_queues = 1;
1452 set->timeout = ADMIN_TIMEOUT;
1453 } else {
1454 set = &ctrl->tag_set;
1455 memset(set, 0, sizeof(*set));
1456 set->ops = &nvme_tcp_mq_ops;
1457 set->queue_depth = nctrl->sqsize + 1;
1458 set->reserved_tags = 1; /* fabric connect */
1459 set->numa_node = NUMA_NO_NODE;
1460 set->flags = BLK_MQ_F_SHOULD_MERGE;
1461 set->cmd_size = sizeof(struct nvme_tcp_request);
1462 set->driver_data = ctrl;
1463 set->nr_hw_queues = nctrl->queue_count - 1;
1464 set->timeout = NVME_IO_TIMEOUT;
873946f4 1465 set->nr_maps = 2 /* default + read */;
3f2304f8
SG
1466 }
1467
1468 ret = blk_mq_alloc_tag_set(set);
1469 if (ret)
1470 return ERR_PTR(ret);
1471
1472 return set;
1473}
1474
1475static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl)
1476{
1477 if (to_tcp_ctrl(ctrl)->async_req.pdu) {
1478 nvme_tcp_free_async_req(to_tcp_ctrl(ctrl));
1479 to_tcp_ctrl(ctrl)->async_req.pdu = NULL;
1480 }
1481
1482 nvme_tcp_free_queue(ctrl, 0);
1483}
1484
1485static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl)
1486{
1487 int i;
1488
1489 for (i = 1; i < ctrl->queue_count; i++)
1490 nvme_tcp_free_queue(ctrl, i);
1491}
1492
1493static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl)
1494{
1495 int i;
1496
1497 for (i = 1; i < ctrl->queue_count; i++)
1498 nvme_tcp_stop_queue(ctrl, i);
1499}
1500
1501static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl)
1502{
1503 int i, ret = 0;
1504
1505 for (i = 1; i < ctrl->queue_count; i++) {
1506 ret = nvme_tcp_start_queue(ctrl, i);
1507 if (ret)
1508 goto out_stop_queues;
1509 }
1510
1511 return 0;
1512
1513out_stop_queues:
1514 for (i--; i >= 1; i--)
1515 nvme_tcp_stop_queue(ctrl, i);
1516 return ret;
1517}
1518
1519static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
1520{
1521 int ret;
1522
1523 ret = nvme_tcp_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
1524 if (ret)
1525 return ret;
1526
1527 ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
1528 if (ret)
1529 goto out_free_queue;
1530
1531 return 0;
1532
1533out_free_queue:
1534 nvme_tcp_free_queue(ctrl, 0);
1535 return ret;
1536}
1537
efb973b1 1538static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
3f2304f8
SG
1539{
1540 int i, ret;
1541
1542 for (i = 1; i < ctrl->queue_count; i++) {
1543 ret = nvme_tcp_alloc_queue(ctrl, i,
1544 ctrl->sqsize + 1);
1545 if (ret)
1546 goto out_free_queues;
1547 }
1548
1549 return 0;
1550
1551out_free_queues:
1552 for (i--; i >= 1; i--)
1553 nvme_tcp_free_queue(ctrl, i);
1554
1555 return ret;
1556}
1557
1558static unsigned int nvme_tcp_nr_io_queues(struct nvme_ctrl *ctrl)
1559{
873946f4
SG
1560 unsigned int nr_io_queues;
1561
1562 nr_io_queues = min(ctrl->opts->nr_io_queues, num_online_cpus());
1563 nr_io_queues += min(ctrl->opts->nr_write_queues, num_online_cpus());
1564
1565 return nr_io_queues;
3f2304f8
SG
1566}
1567
64861993
SG
1568static void nvme_tcp_set_io_queues(struct nvme_ctrl *nctrl,
1569 unsigned int nr_io_queues)
1570{
1571 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1572 struct nvmf_ctrl_options *opts = nctrl->opts;
1573
1574 if (opts->nr_write_queues && opts->nr_io_queues < nr_io_queues) {
1575 /*
1576 * separate read/write queues
1577 * hand out dedicated default queues only after we have
1578 * sufficient read queues.
1579 */
1580 ctrl->io_queues[HCTX_TYPE_READ] = opts->nr_io_queues;
1581 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
1582 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1583 min(opts->nr_write_queues, nr_io_queues);
1584 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1585 } else {
1586 /*
1587 * shared read/write queues
1588 * either no write queues were requested, or we don't have
1589 * sufficient queue count to have dedicated default queues.
1590 */
1591 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1592 min(opts->nr_io_queues, nr_io_queues);
1593 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1594 }
1595}
1596
efb973b1 1597static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
3f2304f8
SG
1598{
1599 unsigned int nr_io_queues;
1600 int ret;
1601
1602 nr_io_queues = nvme_tcp_nr_io_queues(ctrl);
1603 ret = nvme_set_queue_count(ctrl, &nr_io_queues);
1604 if (ret)
1605 return ret;
1606
1607 ctrl->queue_count = nr_io_queues + 1;
1608 if (ctrl->queue_count < 2)
1609 return 0;
1610
1611 dev_info(ctrl->device,
1612 "creating %d I/O queues.\n", nr_io_queues);
1613
64861993
SG
1614 nvme_tcp_set_io_queues(ctrl, nr_io_queues);
1615
efb973b1 1616 return __nvme_tcp_alloc_io_queues(ctrl);
3f2304f8
SG
1617}
1618
1619static void nvme_tcp_destroy_io_queues(struct nvme_ctrl *ctrl, bool remove)
1620{
1621 nvme_tcp_stop_io_queues(ctrl);
1622 if (remove) {
e85037a2 1623 blk_cleanup_queue(ctrl->connect_q);
3f2304f8
SG
1624 blk_mq_free_tag_set(ctrl->tagset);
1625 }
1626 nvme_tcp_free_io_queues(ctrl);
1627}
1628
1629static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
1630{
1631 int ret;
1632
efb973b1 1633 ret = nvme_tcp_alloc_io_queues(ctrl);
3f2304f8
SG
1634 if (ret)
1635 return ret;
1636
1637 if (new) {
1638 ctrl->tagset = nvme_tcp_alloc_tagset(ctrl, false);
1639 if (IS_ERR(ctrl->tagset)) {
1640 ret = PTR_ERR(ctrl->tagset);
1641 goto out_free_io_queues;
1642 }
1643
e85037a2
SG
1644 ctrl->connect_q = blk_mq_init_queue(ctrl->tagset);
1645 if (IS_ERR(ctrl->connect_q)) {
1646 ret = PTR_ERR(ctrl->connect_q);
1647 goto out_free_tag_set;
3f2304f8
SG
1648 }
1649 } else {
1650 blk_mq_update_nr_hw_queues(ctrl->tagset,
1651 ctrl->queue_count - 1);
1652 }
1653
1654 ret = nvme_tcp_start_io_queues(ctrl);
1655 if (ret)
1656 goto out_cleanup_connect_q;
1657
1658 return 0;
1659
1660out_cleanup_connect_q:
e85037a2 1661 if (new)
3f2304f8
SG
1662 blk_cleanup_queue(ctrl->connect_q);
1663out_free_tag_set:
1664 if (new)
1665 blk_mq_free_tag_set(ctrl->tagset);
1666out_free_io_queues:
1667 nvme_tcp_free_io_queues(ctrl);
1668 return ret;
1669}
1670
1671static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl *ctrl, bool remove)
1672{
1673 nvme_tcp_stop_queue(ctrl, 0);
1674 if (remove) {
3f2304f8
SG
1675 blk_cleanup_queue(ctrl->admin_q);
1676 blk_mq_free_tag_set(ctrl->admin_tagset);
1677 }
1678 nvme_tcp_free_admin_queue(ctrl);
1679}
1680
1681static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
1682{
1683 int error;
1684
1685 error = nvme_tcp_alloc_admin_queue(ctrl);
1686 if (error)
1687 return error;
1688
1689 if (new) {
1690 ctrl->admin_tagset = nvme_tcp_alloc_tagset(ctrl, true);
1691 if (IS_ERR(ctrl->admin_tagset)) {
1692 error = PTR_ERR(ctrl->admin_tagset);
1693 goto out_free_queue;
1694 }
1695
1696 ctrl->admin_q = blk_mq_init_queue(ctrl->admin_tagset);
1697 if (IS_ERR(ctrl->admin_q)) {
1698 error = PTR_ERR(ctrl->admin_q);
1699 goto out_free_tagset;
1700 }
1701 }
1702
1703 error = nvme_tcp_start_queue(ctrl, 0);
1704 if (error)
1705 goto out_cleanup_queue;
1706
1707 error = ctrl->ops->reg_read64(ctrl, NVME_REG_CAP, &ctrl->cap);
1708 if (error) {
1709 dev_err(ctrl->device,
1710 "prop_get NVME_REG_CAP failed\n");
1711 goto out_stop_queue;
1712 }
1713
1714 ctrl->sqsize = min_t(int, NVME_CAP_MQES(ctrl->cap), ctrl->sqsize);
1715
1716 error = nvme_enable_ctrl(ctrl, ctrl->cap);
1717 if (error)
1718 goto out_stop_queue;
1719
1720 error = nvme_init_identify(ctrl);
1721 if (error)
1722 goto out_stop_queue;
1723
1724 return 0;
1725
1726out_stop_queue:
1727 nvme_tcp_stop_queue(ctrl, 0);
1728out_cleanup_queue:
1729 if (new)
1730 blk_cleanup_queue(ctrl->admin_q);
1731out_free_tagset:
1732 if (new)
1733 blk_mq_free_tag_set(ctrl->admin_tagset);
1734out_free_queue:
1735 nvme_tcp_free_admin_queue(ctrl);
1736 return error;
1737}
1738
1739static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
1740 bool remove)
1741{
1742 blk_mq_quiesce_queue(ctrl->admin_q);
1743 nvme_tcp_stop_queue(ctrl, 0);
7a425896
SG
1744 if (ctrl->admin_tagset)
1745 blk_mq_tagset_busy_iter(ctrl->admin_tagset,
1746 nvme_cancel_request, ctrl);
3f2304f8
SG
1747 blk_mq_unquiesce_queue(ctrl->admin_q);
1748 nvme_tcp_destroy_admin_queue(ctrl, remove);
1749}
1750
1751static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
1752 bool remove)
1753{
1754 if (ctrl->queue_count <= 1)
1755 return;
1756 nvme_stop_queues(ctrl);
1757 nvme_tcp_stop_io_queues(ctrl);
7a425896
SG
1758 if (ctrl->tagset)
1759 blk_mq_tagset_busy_iter(ctrl->tagset,
1760 nvme_cancel_request, ctrl);
3f2304f8
SG
1761 if (remove)
1762 nvme_start_queues(ctrl);
1763 nvme_tcp_destroy_io_queues(ctrl, remove);
1764}
1765
1766static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl)
1767{
1768 /* If we are resetting/deleting then do nothing */
1769 if (ctrl->state != NVME_CTRL_CONNECTING) {
1770 WARN_ON_ONCE(ctrl->state == NVME_CTRL_NEW ||
1771 ctrl->state == NVME_CTRL_LIVE);
1772 return;
1773 }
1774
1775 if (nvmf_should_reconnect(ctrl)) {
1776 dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
1777 ctrl->opts->reconnect_delay);
1778 queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
1779 ctrl->opts->reconnect_delay * HZ);
1780 } else {
1781 dev_info(ctrl->device, "Removing controller...\n");
1782 nvme_delete_ctrl(ctrl);
1783 }
1784}
1785
1786static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
1787{
1788 struct nvmf_ctrl_options *opts = ctrl->opts;
1789 int ret = -EINVAL;
1790
1791 ret = nvme_tcp_configure_admin_queue(ctrl, new);
1792 if (ret)
1793 return ret;
1794
1795 if (ctrl->icdoff) {
1796 dev_err(ctrl->device, "icdoff is not supported!\n");
1797 goto destroy_admin;
1798 }
1799
1800 if (opts->queue_size > ctrl->sqsize + 1)
1801 dev_warn(ctrl->device,
1802 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1803 opts->queue_size, ctrl->sqsize + 1);
1804
1805 if (ctrl->sqsize + 1 > ctrl->maxcmd) {
1806 dev_warn(ctrl->device,
1807 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1808 ctrl->sqsize + 1, ctrl->maxcmd);
1809 ctrl->sqsize = ctrl->maxcmd - 1;
1810 }
1811
1812 if (ctrl->queue_count > 1) {
1813 ret = nvme_tcp_configure_io_queues(ctrl, new);
1814 if (ret)
1815 goto destroy_admin;
1816 }
1817
1818 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
1819 /* state change failure is ok if we're in DELETING state */
1820 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
1821 ret = -EINVAL;
1822 goto destroy_io;
1823 }
1824
1825 nvme_start_ctrl(ctrl);
1826 return 0;
1827
1828destroy_io:
1829 if (ctrl->queue_count > 1)
1830 nvme_tcp_destroy_io_queues(ctrl, new);
1831destroy_admin:
1832 nvme_tcp_stop_queue(ctrl, 0);
1833 nvme_tcp_destroy_admin_queue(ctrl, new);
1834 return ret;
1835}
1836
1837static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
1838{
1839 struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
1840 struct nvme_tcp_ctrl, connect_work);
1841 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
1842
1843 ++ctrl->nr_reconnects;
1844
1845 if (nvme_tcp_setup_ctrl(ctrl, false))
1846 goto requeue;
1847
56a77d26 1848 dev_info(ctrl->device, "Successfully reconnected (%d attempt)\n",
3f2304f8
SG
1849 ctrl->nr_reconnects);
1850
1851 ctrl->nr_reconnects = 0;
1852
1853 return;
1854
1855requeue:
1856 dev_info(ctrl->device, "Failed reconnect attempt %d\n",
1857 ctrl->nr_reconnects);
1858 nvme_tcp_reconnect_or_remove(ctrl);
1859}
1860
1861static void nvme_tcp_error_recovery_work(struct work_struct *work)
1862{
1863 struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
1864 struct nvme_tcp_ctrl, err_work);
1865 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
1866
1867 nvme_stop_keep_alive(ctrl);
1868 nvme_tcp_teardown_io_queues(ctrl, false);
1869 /* unquiesce to fail fast pending requests */
1870 nvme_start_queues(ctrl);
1871 nvme_tcp_teardown_admin_queue(ctrl, false);
1872
1873 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
1874 /* state change failure is ok if we're in DELETING state */
1875 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
1876 return;
1877 }
1878
1879 nvme_tcp_reconnect_or_remove(ctrl);
1880}
1881
1882static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
1883{
794a4cb3
SG
1884 cancel_work_sync(&to_tcp_ctrl(ctrl)->err_work);
1885 cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
1886
3f2304f8
SG
1887 nvme_tcp_teardown_io_queues(ctrl, shutdown);
1888 if (shutdown)
1889 nvme_shutdown_ctrl(ctrl);
1890 else
1891 nvme_disable_ctrl(ctrl, ctrl->cap);
1892 nvme_tcp_teardown_admin_queue(ctrl, shutdown);
1893}
1894
1895static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
1896{
1897 nvme_tcp_teardown_ctrl(ctrl, true);
1898}
1899
1900static void nvme_reset_ctrl_work(struct work_struct *work)
1901{
1902 struct nvme_ctrl *ctrl =
1903 container_of(work, struct nvme_ctrl, reset_work);
1904
1905 nvme_stop_ctrl(ctrl);
1906 nvme_tcp_teardown_ctrl(ctrl, false);
1907
1908 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
1909 /* state change failure is ok if we're in DELETING state */
1910 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
1911 return;
1912 }
1913
1914 if (nvme_tcp_setup_ctrl(ctrl, false))
1915 goto out_fail;
1916
1917 return;
1918
1919out_fail:
1920 ++ctrl->nr_reconnects;
1921 nvme_tcp_reconnect_or_remove(ctrl);
1922}
1923
3f2304f8
SG
1924static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
1925{
1926 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1927
1928 if (list_empty(&ctrl->list))
1929 goto free_ctrl;
1930
1931 mutex_lock(&nvme_tcp_ctrl_mutex);
1932 list_del(&ctrl->list);
1933 mutex_unlock(&nvme_tcp_ctrl_mutex);
1934
1935 nvmf_free_options(nctrl->opts);
1936free_ctrl:
1937 kfree(ctrl->queues);
1938 kfree(ctrl);
1939}
1940
1941static void nvme_tcp_set_sg_null(struct nvme_command *c)
1942{
1943 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1944
1945 sg->addr = 0;
1946 sg->length = 0;
1947 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
1948 NVME_SGL_FMT_TRANSPORT_A;
1949}
1950
1951static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
1952 struct nvme_command *c, u32 data_len)
1953{
1954 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1955
1956 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
1957 sg->length = cpu_to_le32(data_len);
1958 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
1959}
1960
1961static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
1962 u32 data_len)
1963{
1964 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
1965
1966 sg->addr = 0;
1967 sg->length = cpu_to_le32(data_len);
1968 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
1969 NVME_SGL_FMT_TRANSPORT_A;
1970}
1971
1972static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
1973{
1974 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
1975 struct nvme_tcp_queue *queue = &ctrl->queues[0];
1976 struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
1977 struct nvme_command *cmd = &pdu->cmd;
1978 u8 hdgst = nvme_tcp_hdgst_len(queue);
1979
1980 memset(pdu, 0, sizeof(*pdu));
1981 pdu->hdr.type = nvme_tcp_cmd;
1982 if (queue->hdr_digest)
1983 pdu->hdr.flags |= NVME_TCP_F_HDGST;
1984 pdu->hdr.hlen = sizeof(*pdu);
1985 pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
1986
1987 cmd->common.opcode = nvme_admin_async_event;
1988 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
1989 cmd->common.flags |= NVME_CMD_SGL_METABUF;
1990 nvme_tcp_set_sg_null(cmd);
1991
1992 ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
1993 ctrl->async_req.offset = 0;
1994 ctrl->async_req.curr_bio = NULL;
1995 ctrl->async_req.data_len = 0;
1996
1997 nvme_tcp_queue_request(&ctrl->async_req);
1998}
1999
2000static enum blk_eh_timer_return
2001nvme_tcp_timeout(struct request *rq, bool reserved)
2002{
2003 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2004 struct nvme_tcp_ctrl *ctrl = req->queue->ctrl;
2005 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2006
39d57757 2007 dev_warn(ctrl->ctrl.device,
3f2304f8 2008 "queue %d: timeout request %#x type %d\n",
39d57757 2009 nvme_tcp_queue_id(req->queue), rq->tag, pdu->hdr.type);
3f2304f8
SG
2010
2011 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
39d57757
SG
2012 /*
2013 * Teardown immediately if controller times out while starting
2014 * or we are already started error recovery. all outstanding
2015 * requests are completed on shutdown, so we return BLK_EH_DONE.
2016 */
2017 flush_work(&ctrl->err_work);
2018 nvme_tcp_teardown_io_queues(&ctrl->ctrl, false);
2019 nvme_tcp_teardown_admin_queue(&ctrl->ctrl, false);
3f2304f8
SG
2020 return BLK_EH_DONE;
2021 }
2022
39d57757 2023 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
3f2304f8
SG
2024 nvme_tcp_error_recovery(&ctrl->ctrl);
2025
2026 return BLK_EH_RESET_TIMER;
2027}
2028
2029static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
2030 struct request *rq)
2031{
2032 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2033 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2034 struct nvme_command *c = &pdu->cmd;
2035
2036 c->common.flags |= NVME_CMD_SGL_METABUF;
2037
2038 if (rq_data_dir(rq) == WRITE && req->data_len &&
2039 req->data_len <= nvme_tcp_inline_data_size(queue))
2040 nvme_tcp_set_sg_inline(queue, c, req->data_len);
2041 else
2042 nvme_tcp_set_sg_host_data(c, req->data_len);
2043
2044 return 0;
2045}
2046
2047static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
2048 struct request *rq)
2049{
2050 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2051 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2052 struct nvme_tcp_queue *queue = req->queue;
2053 u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
2054 blk_status_t ret;
2055
2056 ret = nvme_setup_cmd(ns, rq, &pdu->cmd);
2057 if (ret)
2058 return ret;
2059
2060 req->state = NVME_TCP_SEND_CMD_PDU;
2061 req->offset = 0;
2062 req->data_sent = 0;
2063 req->pdu_len = 0;
2064 req->pdu_sent = 0;
2065 req->data_len = blk_rq_payload_bytes(rq);
2066 req->curr_bio = rq->bio;
2067
2068 if (rq_data_dir(rq) == WRITE &&
2069 req->data_len <= nvme_tcp_inline_data_size(queue))
2070 req->pdu_len = req->data_len;
2071 else if (req->curr_bio)
2072 nvme_tcp_init_iter(req, READ);
2073
2074 pdu->hdr.type = nvme_tcp_cmd;
2075 pdu->hdr.flags = 0;
2076 if (queue->hdr_digest)
2077 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2078 if (queue->data_digest && req->pdu_len) {
2079 pdu->hdr.flags |= NVME_TCP_F_DDGST;
2080 ddgst = nvme_tcp_ddgst_len(queue);
2081 }
2082 pdu->hdr.hlen = sizeof(*pdu);
2083 pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
2084 pdu->hdr.plen =
2085 cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
2086
2087 ret = nvme_tcp_map_data(queue, rq);
2088 if (unlikely(ret)) {
2089 dev_err(queue->ctrl->ctrl.device,
2090 "Failed to map data (%d)\n", ret);
2091 return ret;
2092 }
2093
2094 return 0;
2095}
2096
2097static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
2098 const struct blk_mq_queue_data *bd)
2099{
2100 struct nvme_ns *ns = hctx->queue->queuedata;
2101 struct nvme_tcp_queue *queue = hctx->driver_data;
2102 struct request *rq = bd->rq;
2103 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2104 bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
2105 blk_status_t ret;
2106
2107 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2108 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
2109
2110 ret = nvme_tcp_setup_cmd_pdu(ns, rq);
2111 if (unlikely(ret))
2112 return ret;
2113
2114 blk_mq_start_request(rq);
2115
2116 nvme_tcp_queue_request(req);
2117
2118 return BLK_STS_OK;
2119}
2120
873946f4
SG
2121static int nvme_tcp_map_queues(struct blk_mq_tag_set *set)
2122{
2123 struct nvme_tcp_ctrl *ctrl = set->driver_data;
64861993 2124 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
873946f4 2125
64861993 2126 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
873946f4
SG
2127 /* separate read/write queues */
2128 set->map[HCTX_TYPE_DEFAULT].nr_queues =
64861993
SG
2129 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2130 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2131 set->map[HCTX_TYPE_READ].nr_queues =
2132 ctrl->io_queues[HCTX_TYPE_READ];
873946f4 2133 set->map[HCTX_TYPE_READ].queue_offset =
64861993 2134 ctrl->io_queues[HCTX_TYPE_DEFAULT];
873946f4 2135 } else {
64861993 2136 /* shared read/write queues */
873946f4 2137 set->map[HCTX_TYPE_DEFAULT].nr_queues =
64861993
SG
2138 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2139 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2140 set->map[HCTX_TYPE_READ].nr_queues =
2141 ctrl->io_queues[HCTX_TYPE_DEFAULT];
873946f4
SG
2142 set->map[HCTX_TYPE_READ].queue_offset = 0;
2143 }
2144 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
2145 blk_mq_map_queues(&set->map[HCTX_TYPE_READ]);
64861993
SG
2146
2147 dev_info(ctrl->ctrl.device,
2148 "mapped %d/%d default/read queues.\n",
2149 ctrl->io_queues[HCTX_TYPE_DEFAULT],
2150 ctrl->io_queues[HCTX_TYPE_READ]);
2151
873946f4
SG
2152 return 0;
2153}
2154
3f2304f8
SG
2155static struct blk_mq_ops nvme_tcp_mq_ops = {
2156 .queue_rq = nvme_tcp_queue_rq,
2157 .complete = nvme_complete_rq,
2158 .init_request = nvme_tcp_init_request,
2159 .exit_request = nvme_tcp_exit_request,
2160 .init_hctx = nvme_tcp_init_hctx,
2161 .timeout = nvme_tcp_timeout,
873946f4 2162 .map_queues = nvme_tcp_map_queues,
3f2304f8
SG
2163};
2164
2165static struct blk_mq_ops nvme_tcp_admin_mq_ops = {
2166 .queue_rq = nvme_tcp_queue_rq,
2167 .complete = nvme_complete_rq,
2168 .init_request = nvme_tcp_init_request,
2169 .exit_request = nvme_tcp_exit_request,
2170 .init_hctx = nvme_tcp_init_admin_hctx,
2171 .timeout = nvme_tcp_timeout,
2172};
2173
2174static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
2175 .name = "tcp",
2176 .module = THIS_MODULE,
2177 .flags = NVME_F_FABRICS,
2178 .reg_read32 = nvmf_reg_read32,
2179 .reg_read64 = nvmf_reg_read64,
2180 .reg_write32 = nvmf_reg_write32,
2181 .free_ctrl = nvme_tcp_free_ctrl,
2182 .submit_async_event = nvme_tcp_submit_async_event,
2183 .delete_ctrl = nvme_tcp_delete_ctrl,
2184 .get_address = nvmf_get_address,
3f2304f8
SG
2185};
2186
2187static bool
2188nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
2189{
2190 struct nvme_tcp_ctrl *ctrl;
2191 bool found = false;
2192
2193 mutex_lock(&nvme_tcp_ctrl_mutex);
2194 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
2195 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2196 if (found)
2197 break;
2198 }
2199 mutex_unlock(&nvme_tcp_ctrl_mutex);
2200
2201 return found;
2202}
2203
2204static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
2205 struct nvmf_ctrl_options *opts)
2206{
2207 struct nvme_tcp_ctrl *ctrl;
2208 int ret;
2209
2210 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2211 if (!ctrl)
2212 return ERR_PTR(-ENOMEM);
2213
2214 INIT_LIST_HEAD(&ctrl->list);
2215 ctrl->ctrl.opts = opts;
873946f4 2216 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues + 1;
3f2304f8
SG
2217 ctrl->ctrl.sqsize = opts->queue_size - 1;
2218 ctrl->ctrl.kato = opts->kato;
2219
2220 INIT_DELAYED_WORK(&ctrl->connect_work,
2221 nvme_tcp_reconnect_ctrl_work);
2222 INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
2223 INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
2224
2225 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2226 opts->trsvcid =
2227 kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
2228 if (!opts->trsvcid) {
2229 ret = -ENOMEM;
2230 goto out_free_ctrl;
2231 }
2232 opts->mask |= NVMF_OPT_TRSVCID;
2233 }
2234
2235 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2236 opts->traddr, opts->trsvcid, &ctrl->addr);
2237 if (ret) {
2238 pr_err("malformed address passed: %s:%s\n",
2239 opts->traddr, opts->trsvcid);
2240 goto out_free_ctrl;
2241 }
2242
2243 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2244 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2245 opts->host_traddr, NULL, &ctrl->src_addr);
2246 if (ret) {
2247 pr_err("malformed src address passed: %s\n",
2248 opts->host_traddr);
2249 goto out_free_ctrl;
2250 }
2251 }
2252
2253 if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
2254 ret = -EALREADY;
2255 goto out_free_ctrl;
2256 }
2257
873946f4 2258 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
3f2304f8
SG
2259 GFP_KERNEL);
2260 if (!ctrl->queues) {
2261 ret = -ENOMEM;
2262 goto out_free_ctrl;
2263 }
2264
2265 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
2266 if (ret)
2267 goto out_kfree_queues;
2268
2269 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2270 WARN_ON_ONCE(1);
2271 ret = -EINTR;
2272 goto out_uninit_ctrl;
2273 }
2274
2275 ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
2276 if (ret)
2277 goto out_uninit_ctrl;
2278
2279 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
2280 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2281
2282 nvme_get_ctrl(&ctrl->ctrl);
2283
2284 mutex_lock(&nvme_tcp_ctrl_mutex);
2285 list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
2286 mutex_unlock(&nvme_tcp_ctrl_mutex);
2287
2288 return &ctrl->ctrl;
2289
2290out_uninit_ctrl:
2291 nvme_uninit_ctrl(&ctrl->ctrl);
2292 nvme_put_ctrl(&ctrl->ctrl);
2293 if (ret > 0)
2294 ret = -EIO;
2295 return ERR_PTR(ret);
2296out_kfree_queues:
2297 kfree(ctrl->queues);
2298out_free_ctrl:
2299 kfree(ctrl);
2300 return ERR_PTR(ret);
2301}
2302
2303static struct nvmf_transport_ops nvme_tcp_transport = {
2304 .name = "tcp",
2305 .module = THIS_MODULE,
2306 .required_opts = NVMF_OPT_TRADDR,
2307 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2308 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
873946f4
SG
2309 NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
2310 NVMF_OPT_NR_WRITE_QUEUES,
3f2304f8
SG
2311 .create_ctrl = nvme_tcp_create_ctrl,
2312};
2313
2314static int __init nvme_tcp_init_module(void)
2315{
2316 nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq",
2317 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2318 if (!nvme_tcp_wq)
2319 return -ENOMEM;
2320
2321 nvmf_register_transport(&nvme_tcp_transport);
2322 return 0;
2323}
2324
2325static void __exit nvme_tcp_cleanup_module(void)
2326{
2327 struct nvme_tcp_ctrl *ctrl;
2328
2329 nvmf_unregister_transport(&nvme_tcp_transport);
2330
2331 mutex_lock(&nvme_tcp_ctrl_mutex);
2332 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
2333 nvme_delete_ctrl(&ctrl->ctrl);
2334 mutex_unlock(&nvme_tcp_ctrl_mutex);
2335 flush_workqueue(nvme_delete_wq);
2336
2337 destroy_workqueue(nvme_tcp_wq);
2338}
2339
2340module_init(nvme_tcp_init_module);
2341module_exit(nvme_tcp_cleanup_module);
2342
2343MODULE_LICENSE("GPL v2");