blob: a44d8ace3a8193053cfd2a16ffd26bc570a54840 [file] [log] [blame]
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001// 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>
Sagi Grimberg1a9460c2019-07-03 14:08:04 -070016#include <net/busy_poll.h>
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080017
18#include "nvme.h"
19#include "fabrics.h"
20
21struct nvme_tcp_queue;
22
Wunderlich, Mark9912ade2020-01-16 00:46:12 +000023/* 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.
28 */
29static int so_priority;
30module_param(so_priority, int, 0644);
31MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority");
32
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080033enum nvme_tcp_send_state {
34 NVME_TCP_SEND_CMD_PDU = 0,
35 NVME_TCP_SEND_H2C_PDU,
36 NVME_TCP_SEND_DATA,
37 NVME_TCP_SEND_DDGST,
38};
39
40struct nvme_tcp_request {
41 struct nvme_request req;
42 void *pdu;
43 struct nvme_tcp_queue *queue;
44 u32 data_len;
45 u32 pdu_len;
46 u32 pdu_sent;
47 u16 ttag;
48 struct list_head entry;
Sagi Grimberg15ec9282020-06-18 17:30:22 -070049 struct llist_node lentry;
Christoph Hellwiga7273d42018-12-13 09:46:59 +010050 __le32 ddgst;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080051
52 struct bio *curr_bio;
53 struct iov_iter iter;
54
55 /* send state */
56 size_t offset;
57 size_t data_sent;
58 enum nvme_tcp_send_state state;
59};
60
61enum nvme_tcp_queue_flags {
62 NVME_TCP_Q_ALLOCATED = 0,
63 NVME_TCP_Q_LIVE = 1,
Sagi Grimberg72e5d752020-05-01 14:25:44 -070064 NVME_TCP_Q_POLLING = 2,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080065};
66
67enum nvme_tcp_recv_state {
68 NVME_TCP_RECV_PDU = 0,
69 NVME_TCP_RECV_DATA,
70 NVME_TCP_RECV_DDGST,
71};
72
73struct nvme_tcp_ctrl;
74struct nvme_tcp_queue {
75 struct socket *sock;
76 struct work_struct io_work;
77 int io_cpu;
78
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -070079 struct mutex send_mutex;
Sagi Grimberg15ec9282020-06-18 17:30:22 -070080 struct llist_head req_list;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080081 struct list_head send_list;
Sagi Grimberg122e5b92020-06-18 17:30:24 -070082 bool more_requests;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080083
84 /* recv state */
85 void *pdu;
86 int pdu_remaining;
87 int pdu_offset;
88 size_t data_remaining;
89 size_t ddgst_remaining;
Sagi Grimberg1a9460c2019-07-03 14:08:04 -070090 unsigned int nr_cqe;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080091
92 /* send state */
93 struct nvme_tcp_request *request;
94
95 int queue_size;
96 size_t cmnd_capsule_len;
97 struct nvme_tcp_ctrl *ctrl;
98 unsigned long flags;
99 bool rd_enabled;
100
101 bool hdr_digest;
102 bool data_digest;
103 struct ahash_request *rcv_hash;
104 struct ahash_request *snd_hash;
105 __le32 exp_ddgst;
106 __le32 recv_ddgst;
107
108 struct page_frag_cache pf_cache;
109
110 void (*state_change)(struct sock *);
111 void (*data_ready)(struct sock *);
112 void (*write_space)(struct sock *);
113};
114
115struct nvme_tcp_ctrl {
116 /* read only in the hot path */
117 struct nvme_tcp_queue *queues;
118 struct blk_mq_tag_set tag_set;
119
120 /* other member variables */
121 struct list_head list;
122 struct blk_mq_tag_set admin_tag_set;
123 struct sockaddr_storage addr;
124 struct sockaddr_storage src_addr;
125 struct nvme_ctrl ctrl;
126
127 struct work_struct err_work;
128 struct delayed_work connect_work;
129 struct nvme_tcp_request async_req;
Sagi Grimberg64861992019-05-28 22:49:05 -0700130 u32 io_queues[HCTX_MAX_TYPES];
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800131};
132
133static LIST_HEAD(nvme_tcp_ctrl_list);
134static DEFINE_MUTEX(nvme_tcp_ctrl_mutex);
135static struct workqueue_struct *nvme_tcp_wq;
Rikard Falkeborn6acbd962020-05-29 00:25:07 +0200136static const struct blk_mq_ops nvme_tcp_mq_ops;
137static const struct blk_mq_ops nvme_tcp_admin_mq_ops;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700138static int nvme_tcp_try_send(struct nvme_tcp_queue *queue);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800139
140static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl)
141{
142 return container_of(ctrl, struct nvme_tcp_ctrl, ctrl);
143}
144
145static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
146{
147 return queue - queue->ctrl->queues;
148}
149
150static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue)
151{
152 u32 queue_idx = nvme_tcp_queue_id(queue);
153
154 if (queue_idx == 0)
155 return queue->ctrl->admin_tag_set.tags[queue_idx];
156 return queue->ctrl->tag_set.tags[queue_idx - 1];
157}
158
159static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue)
160{
161 return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
162}
163
164static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue)
165{
166 return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
167}
168
169static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_queue *queue)
170{
171 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
172}
173
174static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req)
175{
176 return req == &req->queue->ctrl->async_req;
177}
178
179static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req)
180{
181 struct request *rq;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800182
183 if (unlikely(nvme_tcp_async_req(req)))
184 return false; /* async events don't have a request */
185
186 rq = blk_mq_rq_from_pdu(req);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800187
Sagi Grimberg25e5cb72020-03-23 15:06:30 -0700188 return rq_data_dir(rq) == WRITE && req->data_len &&
189 req->data_len <= nvme_tcp_inline_data_size(req->queue);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800190}
191
192static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req)
193{
194 return req->iter.bvec->bv_page;
195}
196
197static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req)
198{
199 return req->iter.bvec->bv_offset + req->iter.iov_offset;
200}
201
202static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req)
203{
204 return min_t(size_t, req->iter.bvec->bv_len - req->iter.iov_offset,
205 req->pdu_len - req->pdu_sent);
206}
207
208static inline size_t nvme_tcp_req_offset(struct nvme_tcp_request *req)
209{
210 return req->iter.iov_offset;
211}
212
213static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req)
214{
215 return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ?
216 req->pdu_len - req->pdu_sent : 0;
217}
218
219static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
220 int len)
221{
222 return nvme_tcp_pdu_data_left(req) <= len;
223}
224
225static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
226 unsigned int dir)
227{
228 struct request *rq = blk_mq_rq_from_pdu(req);
229 struct bio_vec *vec;
230 unsigned int size;
231 int nsegs;
232 size_t offset;
233
234 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
235 vec = &rq->special_vec;
236 nsegs = 1;
237 size = blk_rq_payload_bytes(rq);
238 offset = 0;
239 } else {
240 struct bio *bio = req->curr_bio;
241
242 vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
243 nsegs = bio_segments(bio);
244 size = bio->bi_iter.bi_size;
245 offset = bio->bi_iter.bi_bvec_done;
246 }
247
248 iov_iter_bvec(&req->iter, dir, vec, nsegs, size);
249 req->iter.iov_offset = offset;
250}
251
252static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req,
253 int len)
254{
255 req->data_sent += len;
256 req->pdu_sent += len;
257 iov_iter_advance(&req->iter, len);
258 if (!iov_iter_count(&req->iter) &&
259 req->data_sent < req->data_len) {
260 req->curr_bio = req->curr_bio->bi_next;
261 nvme_tcp_init_iter(req, WRITE);
262 }
263}
264
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700265static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req,
Sagi Grimberg86f03482020-06-18 17:30:23 -0700266 bool sync, bool last)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800267{
268 struct nvme_tcp_queue *queue = req->queue;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700269 bool empty;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800270
Sagi Grimberg15ec9282020-06-18 17:30:22 -0700271 empty = llist_add(&req->lentry, &queue->req_list) &&
272 list_empty(&queue->send_list) && !queue->request;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800273
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700274 /*
275 * if we're the first on the send_list and we can try to send
276 * directly, otherwise queue io_work. Also, only do that if we
277 * are on the same cpu, so we don't introduce contention.
278 */
279 if (queue->io_cpu == smp_processor_id() &&
280 sync && empty && mutex_trylock(&queue->send_mutex)) {
Sagi Grimberg122e5b92020-06-18 17:30:24 -0700281 queue->more_requests = !last;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700282 nvme_tcp_try_send(queue);
Sagi Grimberg122e5b92020-06-18 17:30:24 -0700283 queue->more_requests = false;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700284 mutex_unlock(&queue->send_mutex);
Sagi Grimberg86f03482020-06-18 17:30:23 -0700285 } else if (last) {
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700286 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
287 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800288}
289
Sagi Grimberg15ec9282020-06-18 17:30:22 -0700290static void nvme_tcp_process_req_list(struct nvme_tcp_queue *queue)
291{
292 struct nvme_tcp_request *req;
293 struct llist_node *node;
294
295 for (node = llist_del_all(&queue->req_list); node; node = node->next) {
296 req = llist_entry(node, struct nvme_tcp_request, lentry);
297 list_add(&req->entry, &queue->send_list);
298 }
299}
300
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800301static inline struct nvme_tcp_request *
302nvme_tcp_fetch_request(struct nvme_tcp_queue *queue)
303{
304 struct nvme_tcp_request *req;
305
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800306 req = list_first_entry_or_null(&queue->send_list,
307 struct nvme_tcp_request, entry);
Sagi Grimberg15ec9282020-06-18 17:30:22 -0700308 if (!req) {
309 nvme_tcp_process_req_list(queue);
310 req = list_first_entry_or_null(&queue->send_list,
311 struct nvme_tcp_request, entry);
312 if (unlikely(!req))
313 return NULL;
314 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800315
Sagi Grimberg15ec9282020-06-18 17:30:22 -0700316 list_del(&req->entry);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800317 return req;
318}
319
Christoph Hellwiga7273d42018-12-13 09:46:59 +0100320static inline void nvme_tcp_ddgst_final(struct ahash_request *hash,
321 __le32 *dgst)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800322{
323 ahash_request_set_crypt(hash, NULL, (u8 *)dgst, 0);
324 crypto_ahash_final(hash);
325}
326
327static inline void nvme_tcp_ddgst_update(struct ahash_request *hash,
328 struct page *page, off_t off, size_t len)
329{
330 struct scatterlist sg;
331
332 sg_init_marker(&sg, 1);
333 sg_set_page(&sg, page, len, off);
334 ahash_request_set_crypt(hash, &sg, NULL, len);
335 crypto_ahash_update(hash);
336}
337
338static inline void nvme_tcp_hdgst(struct ahash_request *hash,
339 void *pdu, size_t len)
340{
341 struct scatterlist sg;
342
343 sg_init_one(&sg, pdu, len);
344 ahash_request_set_crypt(hash, &sg, pdu + len, len);
345 crypto_ahash_digest(hash);
346}
347
348static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue,
349 void *pdu, size_t pdu_len)
350{
351 struct nvme_tcp_hdr *hdr = pdu;
352 __le32 recv_digest;
353 __le32 exp_digest;
354
355 if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
356 dev_err(queue->ctrl->ctrl.device,
357 "queue %d: header digest flag is cleared\n",
358 nvme_tcp_queue_id(queue));
359 return -EPROTO;
360 }
361
362 recv_digest = *(__le32 *)(pdu + hdr->hlen);
363 nvme_tcp_hdgst(queue->rcv_hash, pdu, pdu_len);
364 exp_digest = *(__le32 *)(pdu + hdr->hlen);
365 if (recv_digest != exp_digest) {
366 dev_err(queue->ctrl->ctrl.device,
367 "header digest error: recv %#x expected %#x\n",
368 le32_to_cpu(recv_digest), le32_to_cpu(exp_digest));
369 return -EIO;
370 }
371
372 return 0;
373}
374
375static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu)
376{
377 struct nvme_tcp_hdr *hdr = pdu;
378 u8 digest_len = nvme_tcp_hdgst_len(queue);
379 u32 len;
380
381 len = le32_to_cpu(hdr->plen) - hdr->hlen -
382 ((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0);
383
384 if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
385 dev_err(queue->ctrl->ctrl.device,
386 "queue %d: data digest flag is cleared\n",
387 nvme_tcp_queue_id(queue));
388 return -EPROTO;
389 }
390 crypto_ahash_init(queue->rcv_hash);
391
392 return 0;
393}
394
395static void nvme_tcp_exit_request(struct blk_mq_tag_set *set,
396 struct request *rq, unsigned int hctx_idx)
397{
398 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
399
400 page_frag_free(req->pdu);
401}
402
403static int nvme_tcp_init_request(struct blk_mq_tag_set *set,
404 struct request *rq, unsigned int hctx_idx,
405 unsigned int numa_node)
406{
407 struct nvme_tcp_ctrl *ctrl = set->driver_data;
408 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
409 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
410 struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx];
411 u8 hdgst = nvme_tcp_hdgst_len(queue);
412
413 req->pdu = page_frag_alloc(&queue->pf_cache,
414 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
415 GFP_KERNEL | __GFP_ZERO);
416 if (!req->pdu)
417 return -ENOMEM;
418
419 req->queue = queue;
420 nvme_req(rq)->ctrl = &ctrl->ctrl;
421
422 return 0;
423}
424
425static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
426 unsigned int hctx_idx)
427{
428 struct nvme_tcp_ctrl *ctrl = data;
429 struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1];
430
431 hctx->driver_data = queue;
432 return 0;
433}
434
435static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
436 unsigned int hctx_idx)
437{
438 struct nvme_tcp_ctrl *ctrl = data;
439 struct nvme_tcp_queue *queue = &ctrl->queues[0];
440
441 hctx->driver_data = queue;
442 return 0;
443}
444
445static enum nvme_tcp_recv_state
446nvme_tcp_recv_state(struct nvme_tcp_queue *queue)
447{
448 return (queue->pdu_remaining) ? NVME_TCP_RECV_PDU :
449 (queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST :
450 NVME_TCP_RECV_DATA;
451}
452
453static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
454{
455 queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) +
456 nvme_tcp_hdgst_len(queue);
457 queue->pdu_offset = 0;
458 queue->data_remaining = -1;
459 queue->ddgst_remaining = 0;
460}
461
462static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
463{
464 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
465 return;
466
Nigel Kirkland97b25122020-02-10 16:01:45 -0800467 queue_work(nvme_reset_wq, &to_tcp_ctrl(ctrl)->err_work);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800468}
469
470static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue,
471 struct nvme_completion *cqe)
472{
473 struct request *rq;
474
475 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), cqe->command_id);
476 if (!rq) {
477 dev_err(queue->ctrl->ctrl.device,
478 "queue %d tag 0x%x not found\n",
479 nvme_tcp_queue_id(queue), cqe->command_id);
480 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
481 return -EINVAL;
482 }
483
Christoph Hellwig2eb81a32020-08-18 09:11:29 +0200484 if (!nvme_try_complete_req(rq, cqe->status, cqe->result))
Christoph Hellwigff029452020-06-11 08:44:52 +0200485 nvme_complete_rq(rq);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700486 queue->nr_cqe++;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800487
488 return 0;
489}
490
491static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
492 struct nvme_tcp_data_pdu *pdu)
493{
494 struct request *rq;
495
496 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
497 if (!rq) {
498 dev_err(queue->ctrl->ctrl.device,
499 "queue %d tag %#x not found\n",
500 nvme_tcp_queue_id(queue), pdu->command_id);
501 return -ENOENT;
502 }
503
504 if (!blk_rq_payload_bytes(rq)) {
505 dev_err(queue->ctrl->ctrl.device,
506 "queue %d tag %#x unexpected data\n",
507 nvme_tcp_queue_id(queue), rq->tag);
508 return -EIO;
509 }
510
511 queue->data_remaining = le32_to_cpu(pdu->data_length);
512
Sagi Grimberg602d6742019-03-13 18:55:10 +0100513 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS &&
514 unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) {
515 dev_err(queue->ctrl->ctrl.device,
516 "queue %d tag %#x SUCCESS set but not last PDU\n",
517 nvme_tcp_queue_id(queue), rq->tag);
518 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
519 return -EPROTO;
520 }
521
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800522 return 0;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800523}
524
525static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue,
526 struct nvme_tcp_rsp_pdu *pdu)
527{
528 struct nvme_completion *cqe = &pdu->cqe;
529 int ret = 0;
530
531 /*
532 * AEN requests are special as they don't time out and can
533 * survive any kind of queue freeze and often don't respond to
534 * aborts. We don't even bother to allocate a struct request
535 * for them but rather special case them here.
536 */
Israel Rukshin58a8df62019-10-13 19:57:31 +0300537 if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue),
538 cqe->command_id)))
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800539 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
540 &cqe->result);
541 else
542 ret = nvme_tcp_process_nvme_cqe(queue, cqe);
543
544 return ret;
545}
546
547static int nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req,
548 struct nvme_tcp_r2t_pdu *pdu)
549{
550 struct nvme_tcp_data_pdu *data = req->pdu;
551 struct nvme_tcp_queue *queue = req->queue;
552 struct request *rq = blk_mq_rq_from_pdu(req);
553 u8 hdgst = nvme_tcp_hdgst_len(queue);
554 u8 ddgst = nvme_tcp_ddgst_len(queue);
555
556 req->pdu_len = le32_to_cpu(pdu->r2t_length);
557 req->pdu_sent = 0;
558
559 if (unlikely(req->data_sent + req->pdu_len > req->data_len)) {
560 dev_err(queue->ctrl->ctrl.device,
561 "req %d r2t len %u exceeded data len %u (%zu sent)\n",
562 rq->tag, req->pdu_len, req->data_len,
563 req->data_sent);
564 return -EPROTO;
565 }
566
567 if (unlikely(le32_to_cpu(pdu->r2t_offset) < req->data_sent)) {
568 dev_err(queue->ctrl->ctrl.device,
569 "req %d unexpected r2t offset %u (expected %zu)\n",
570 rq->tag, le32_to_cpu(pdu->r2t_offset),
571 req->data_sent);
572 return -EPROTO;
573 }
574
575 memset(data, 0, sizeof(*data));
576 data->hdr.type = nvme_tcp_h2c_data;
577 data->hdr.flags = NVME_TCP_F_DATA_LAST;
578 if (queue->hdr_digest)
579 data->hdr.flags |= NVME_TCP_F_HDGST;
580 if (queue->data_digest)
581 data->hdr.flags |= NVME_TCP_F_DDGST;
582 data->hdr.hlen = sizeof(*data);
583 data->hdr.pdo = data->hdr.hlen + hdgst;
584 data->hdr.plen =
585 cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst);
586 data->ttag = pdu->ttag;
587 data->command_id = rq->tag;
588 data->data_offset = cpu_to_le32(req->data_sent);
589 data->data_length = cpu_to_le32(req->pdu_len);
590 return 0;
591}
592
593static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
594 struct nvme_tcp_r2t_pdu *pdu)
595{
596 struct nvme_tcp_request *req;
597 struct request *rq;
598 int ret;
599
600 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
601 if (!rq) {
602 dev_err(queue->ctrl->ctrl.device,
603 "queue %d tag %#x not found\n",
604 nvme_tcp_queue_id(queue), pdu->command_id);
605 return -ENOENT;
606 }
607 req = blk_mq_rq_to_pdu(rq);
608
609 ret = nvme_tcp_setup_h2c_data_pdu(req, pdu);
610 if (unlikely(ret))
611 return ret;
612
613 req->state = NVME_TCP_SEND_H2C_PDU;
614 req->offset = 0;
615
Sagi Grimberg86f03482020-06-18 17:30:23 -0700616 nvme_tcp_queue_request(req, false, true);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800617
618 return 0;
619}
620
621static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
622 unsigned int *offset, size_t *len)
623{
624 struct nvme_tcp_hdr *hdr;
625 char *pdu = queue->pdu;
626 size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
627 int ret;
628
629 ret = skb_copy_bits(skb, *offset,
630 &pdu[queue->pdu_offset], rcv_len);
631 if (unlikely(ret))
632 return ret;
633
634 queue->pdu_remaining -= rcv_len;
635 queue->pdu_offset += rcv_len;
636 *offset += rcv_len;
637 *len -= rcv_len;
638 if (queue->pdu_remaining)
639 return 0;
640
641 hdr = queue->pdu;
642 if (queue->hdr_digest) {
643 ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen);
644 if (unlikely(ret))
645 return ret;
646 }
647
648
649 if (queue->data_digest) {
650 ret = nvme_tcp_check_ddgst(queue, queue->pdu);
651 if (unlikely(ret))
652 return ret;
653 }
654
655 switch (hdr->type) {
656 case nvme_tcp_c2h_data:
Sagi Grimberg6be18262019-07-19 12:46:46 -0700657 return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800658 case nvme_tcp_rsp:
659 nvme_tcp_init_recv_ctx(queue);
Sagi Grimberg6be18262019-07-19 12:46:46 -0700660 return nvme_tcp_handle_comp(queue, (void *)queue->pdu);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800661 case nvme_tcp_r2t:
662 nvme_tcp_init_recv_ctx(queue);
Sagi Grimberg6be18262019-07-19 12:46:46 -0700663 return nvme_tcp_handle_r2t(queue, (void *)queue->pdu);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800664 default:
665 dev_err(queue->ctrl->ctrl.device,
666 "unsupported pdu type (%d)\n", hdr->type);
667 return -EINVAL;
668 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800669}
670
Christoph Hellwig988aef9e2019-03-15 08:41:04 +0100671static inline void nvme_tcp_end_request(struct request *rq, u16 status)
Sagi Grimberg602d6742019-03-13 18:55:10 +0100672{
673 union nvme_result res = {};
674
Christoph Hellwig2eb81a32020-08-18 09:11:29 +0200675 if (!nvme_try_complete_req(rq, cpu_to_le16(status << 1), res))
Christoph Hellwigff029452020-06-11 08:44:52 +0200676 nvme_complete_rq(rq);
Sagi Grimberg602d6742019-03-13 18:55:10 +0100677}
678
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800679static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb,
680 unsigned int *offset, size_t *len)
681{
682 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
683 struct nvme_tcp_request *req;
684 struct request *rq;
685
686 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
687 if (!rq) {
688 dev_err(queue->ctrl->ctrl.device,
689 "queue %d tag %#x not found\n",
690 nvme_tcp_queue_id(queue), pdu->command_id);
691 return -ENOENT;
692 }
693 req = blk_mq_rq_to_pdu(rq);
694
695 while (true) {
696 int recv_len, ret;
697
698 recv_len = min_t(size_t, *len, queue->data_remaining);
699 if (!recv_len)
700 break;
701
702 if (!iov_iter_count(&req->iter)) {
703 req->curr_bio = req->curr_bio->bi_next;
704
705 /*
706 * If we don`t have any bios it means that controller
707 * sent more data than we requested, hence error
708 */
709 if (!req->curr_bio) {
710 dev_err(queue->ctrl->ctrl.device,
711 "queue %d no space in request %#x",
712 nvme_tcp_queue_id(queue), rq->tag);
713 nvme_tcp_init_recv_ctx(queue);
714 return -EIO;
715 }
716 nvme_tcp_init_iter(req, READ);
717 }
718
719 /* we can read only from what is left in this bio */
720 recv_len = min_t(size_t, recv_len,
721 iov_iter_count(&req->iter));
722
723 if (queue->data_digest)
724 ret = skb_copy_and_hash_datagram_iter(skb, *offset,
725 &req->iter, recv_len, queue->rcv_hash);
726 else
727 ret = skb_copy_datagram_iter(skb, *offset,
728 &req->iter, recv_len);
729 if (ret) {
730 dev_err(queue->ctrl->ctrl.device,
731 "queue %d failed to copy request %#x data",
732 nvme_tcp_queue_id(queue), rq->tag);
733 return ret;
734 }
735
736 *len -= recv_len;
737 *offset += recv_len;
738 queue->data_remaining -= recv_len;
739 }
740
741 if (!queue->data_remaining) {
742 if (queue->data_digest) {
743 nvme_tcp_ddgst_final(queue->rcv_hash, &queue->exp_ddgst);
744 queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH;
745 } else {
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700746 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
Sagi Grimberg602d6742019-03-13 18:55:10 +0100747 nvme_tcp_end_request(rq, NVME_SC_SUCCESS);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700748 queue->nr_cqe++;
749 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800750 nvme_tcp_init_recv_ctx(queue);
751 }
752 }
753
754 return 0;
755}
756
757static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue,
758 struct sk_buff *skb, unsigned int *offset, size_t *len)
759{
Sagi Grimberg602d6742019-03-13 18:55:10 +0100760 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800761 char *ddgst = (char *)&queue->recv_ddgst;
762 size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining);
763 off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining;
764 int ret;
765
766 ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len);
767 if (unlikely(ret))
768 return ret;
769
770 queue->ddgst_remaining -= recv_len;
771 *offset += recv_len;
772 *len -= recv_len;
773 if (queue->ddgst_remaining)
774 return 0;
775
776 if (queue->recv_ddgst != queue->exp_ddgst) {
777 dev_err(queue->ctrl->ctrl.device,
778 "data digest error: recv %#x expected %#x\n",
779 le32_to_cpu(queue->recv_ddgst),
780 le32_to_cpu(queue->exp_ddgst));
781 return -EIO;
782 }
783
Sagi Grimberg602d6742019-03-13 18:55:10 +0100784 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
785 struct request *rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue),
786 pdu->command_id);
787
788 nvme_tcp_end_request(rq, NVME_SC_SUCCESS);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700789 queue->nr_cqe++;
Sagi Grimberg602d6742019-03-13 18:55:10 +0100790 }
791
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800792 nvme_tcp_init_recv_ctx(queue);
793 return 0;
794}
795
796static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,
797 unsigned int offset, size_t len)
798{
799 struct nvme_tcp_queue *queue = desc->arg.data;
800 size_t consumed = len;
801 int result;
802
803 while (len) {
804 switch (nvme_tcp_recv_state(queue)) {
805 case NVME_TCP_RECV_PDU:
806 result = nvme_tcp_recv_pdu(queue, skb, &offset, &len);
807 break;
808 case NVME_TCP_RECV_DATA:
809 result = nvme_tcp_recv_data(queue, skb, &offset, &len);
810 break;
811 case NVME_TCP_RECV_DDGST:
812 result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len);
813 break;
814 default:
815 result = -EFAULT;
816 }
817 if (result) {
818 dev_err(queue->ctrl->ctrl.device,
819 "receive failed: %d\n", result);
820 queue->rd_enabled = false;
821 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
822 return result;
823 }
824 }
825
826 return consumed;
827}
828
829static void nvme_tcp_data_ready(struct sock *sk)
830{
831 struct nvme_tcp_queue *queue;
832
Sagi Grimberg386e5e62020-04-30 13:59:32 -0700833 read_lock_bh(&sk->sk_callback_lock);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800834 queue = sk->sk_user_data;
Sagi Grimberg72e5d752020-05-01 14:25:44 -0700835 if (likely(queue && queue->rd_enabled) &&
836 !test_bit(NVME_TCP_Q_POLLING, &queue->flags))
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800837 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
Sagi Grimberg386e5e62020-04-30 13:59:32 -0700838 read_unlock_bh(&sk->sk_callback_lock);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800839}
840
841static void nvme_tcp_write_space(struct sock *sk)
842{
843 struct nvme_tcp_queue *queue;
844
845 read_lock_bh(&sk->sk_callback_lock);
846 queue = sk->sk_user_data;
847 if (likely(queue && sk_stream_is_writeable(sk))) {
848 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
849 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
850 }
851 read_unlock_bh(&sk->sk_callback_lock);
852}
853
854static void nvme_tcp_state_change(struct sock *sk)
855{
856 struct nvme_tcp_queue *queue;
857
858 read_lock(&sk->sk_callback_lock);
859 queue = sk->sk_user_data;
860 if (!queue)
861 goto done;
862
863 switch (sk->sk_state) {
864 case TCP_CLOSE:
865 case TCP_CLOSE_WAIT:
866 case TCP_LAST_ACK:
867 case TCP_FIN_WAIT1:
868 case TCP_FIN_WAIT2:
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800869 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
870 break;
871 default:
872 dev_info(queue->ctrl->ctrl.device,
873 "queue %d socket state %d\n",
874 nvme_tcp_queue_id(queue), sk->sk_state);
875 }
876
877 queue->state_change(sk);
878done:
879 read_unlock(&sk->sk_callback_lock);
880}
881
Sagi Grimberg122e5b92020-06-18 17:30:24 -0700882static inline bool nvme_tcp_queue_more(struct nvme_tcp_queue *queue)
883{
884 return !list_empty(&queue->send_list) ||
885 !llist_empty(&queue->req_list) || queue->more_requests;
886}
887
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800888static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue)
889{
890 queue->request = NULL;
891}
892
893static void nvme_tcp_fail_request(struct nvme_tcp_request *req)
894{
Sagi Grimberg16686012019-08-02 18:17:52 -0700895 nvme_tcp_end_request(blk_mq_rq_from_pdu(req), NVME_SC_HOST_PATH_ERROR);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800896}
897
898static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
899{
900 struct nvme_tcp_queue *queue = req->queue;
901
902 while (true) {
903 struct page *page = nvme_tcp_req_cur_page(req);
904 size_t offset = nvme_tcp_req_cur_offset(req);
905 size_t len = nvme_tcp_req_cur_length(req);
906 bool last = nvme_tcp_pdu_last_send(req, len);
907 int ret, flags = MSG_DONTWAIT;
908
Sagi Grimberg122e5b92020-06-18 17:30:24 -0700909 if (last && !queue->data_digest && !nvme_tcp_queue_more(queue))
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800910 flags |= MSG_EOR;
911 else
Sagi Grimberg5bb052d2020-05-04 22:20:01 -0700912 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800913
Mikhail Skorzhinskii37c15212019-07-08 12:31:29 +0200914 /* can't zcopy slab pages */
915 if (unlikely(PageSlab(page))) {
916 ret = sock_no_sendpage(queue->sock, page, offset, len,
917 flags);
918 } else {
919 ret = kernel_sendpage(queue->sock, page, offset, len,
920 flags);
921 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800922 if (ret <= 0)
923 return ret;
924
925 nvme_tcp_advance_req(req, ret);
926 if (queue->data_digest)
927 nvme_tcp_ddgst_update(queue->snd_hash, page,
928 offset, ret);
929
930 /* fully successful last write*/
931 if (last && ret == len) {
932 if (queue->data_digest) {
933 nvme_tcp_ddgst_final(queue->snd_hash,
934 &req->ddgst);
935 req->state = NVME_TCP_SEND_DDGST;
936 req->offset = 0;
937 } else {
938 nvme_tcp_done_send_req(queue);
939 }
940 return 1;
941 }
942 }
943 return -EAGAIN;
944}
945
946static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
947{
948 struct nvme_tcp_queue *queue = req->queue;
949 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
950 bool inline_data = nvme_tcp_has_inline_data(req);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800951 u8 hdgst = nvme_tcp_hdgst_len(queue);
952 int len = sizeof(*pdu) + hdgst - req->offset;
Sagi Grimberg5bb052d2020-05-04 22:20:01 -0700953 int flags = MSG_DONTWAIT;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800954 int ret;
955
Sagi Grimberg122e5b92020-06-18 17:30:24 -0700956 if (inline_data || nvme_tcp_queue_more(queue))
Sagi Grimberg5bb052d2020-05-04 22:20:01 -0700957 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
958 else
959 flags |= MSG_EOR;
960
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800961 if (queue->hdr_digest && !req->offset)
962 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
963
964 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
965 offset_in_page(pdu) + req->offset, len, flags);
966 if (unlikely(ret <= 0))
967 return ret;
968
969 len -= ret;
970 if (!len) {
971 if (inline_data) {
972 req->state = NVME_TCP_SEND_DATA;
973 if (queue->data_digest)
974 crypto_ahash_init(queue->snd_hash);
975 nvme_tcp_init_iter(req, WRITE);
976 } else {
977 nvme_tcp_done_send_req(queue);
978 }
979 return 1;
980 }
981 req->offset += ret;
982
983 return -EAGAIN;
984}
985
986static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
987{
988 struct nvme_tcp_queue *queue = req->queue;
989 struct nvme_tcp_data_pdu *pdu = req->pdu;
990 u8 hdgst = nvme_tcp_hdgst_len(queue);
991 int len = sizeof(*pdu) - req->offset + hdgst;
992 int ret;
993
994 if (queue->hdr_digest && !req->offset)
995 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
996
997 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
998 offset_in_page(pdu) + req->offset, len,
Sagi Grimberg5bb052d2020-05-04 22:20:01 -0700999 MSG_DONTWAIT | MSG_MORE | MSG_SENDPAGE_NOTLAST);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001000 if (unlikely(ret <= 0))
1001 return ret;
1002
1003 len -= ret;
1004 if (!len) {
1005 req->state = NVME_TCP_SEND_DATA;
1006 if (queue->data_digest)
1007 crypto_ahash_init(queue->snd_hash);
1008 if (!req->data_sent)
1009 nvme_tcp_init_iter(req, WRITE);
1010 return 1;
1011 }
1012 req->offset += ret;
1013
1014 return -EAGAIN;
1015}
1016
1017static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
1018{
1019 struct nvme_tcp_queue *queue = req->queue;
1020 int ret;
Sagi Grimberg122e5b92020-06-18 17:30:24 -07001021 struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001022 struct kvec iov = {
1023 .iov_base = &req->ddgst + req->offset,
1024 .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset
1025 };
1026
Sagi Grimberg122e5b92020-06-18 17:30:24 -07001027 if (nvme_tcp_queue_more(queue))
1028 msg.msg_flags |= MSG_MORE;
1029 else
1030 msg.msg_flags |= MSG_EOR;
1031
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001032 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1033 if (unlikely(ret <= 0))
1034 return ret;
1035
1036 if (req->offset + ret == NVME_TCP_DIGEST_LENGTH) {
1037 nvme_tcp_done_send_req(queue);
1038 return 1;
1039 }
1040
1041 req->offset += ret;
1042 return -EAGAIN;
1043}
1044
1045static int nvme_tcp_try_send(struct nvme_tcp_queue *queue)
1046{
1047 struct nvme_tcp_request *req;
1048 int ret = 1;
1049
1050 if (!queue->request) {
1051 queue->request = nvme_tcp_fetch_request(queue);
1052 if (!queue->request)
1053 return 0;
1054 }
1055 req = queue->request;
1056
1057 if (req->state == NVME_TCP_SEND_CMD_PDU) {
1058 ret = nvme_tcp_try_send_cmd_pdu(req);
1059 if (ret <= 0)
1060 goto done;
1061 if (!nvme_tcp_has_inline_data(req))
1062 return ret;
1063 }
1064
1065 if (req->state == NVME_TCP_SEND_H2C_PDU) {
1066 ret = nvme_tcp_try_send_data_pdu(req);
1067 if (ret <= 0)
1068 goto done;
1069 }
1070
1071 if (req->state == NVME_TCP_SEND_DATA) {
1072 ret = nvme_tcp_try_send_data(req);
1073 if (ret <= 0)
1074 goto done;
1075 }
1076
1077 if (req->state == NVME_TCP_SEND_DDGST)
1078 ret = nvme_tcp_try_send_ddgst(req);
1079done:
Sagi Grimberg5ff4e112020-02-25 16:43:23 -08001080 if (ret == -EAGAIN) {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001081 ret = 0;
Sagi Grimberg5ff4e112020-02-25 16:43:23 -08001082 } else if (ret < 0) {
1083 dev_err(queue->ctrl->ctrl.device,
1084 "failed to send request %d\n", ret);
1085 if (ret != -EPIPE && ret != -ECONNRESET)
1086 nvme_tcp_fail_request(queue->request);
1087 nvme_tcp_done_send_req(queue);
1088 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001089 return ret;
1090}
1091
1092static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue)
1093{
Potnuri Bharat Teja10407ec2019-07-08 15:22:00 +05301094 struct socket *sock = queue->sock;
1095 struct sock *sk = sock->sk;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001096 read_descriptor_t rd_desc;
1097 int consumed;
1098
1099 rd_desc.arg.data = queue;
1100 rd_desc.count = 1;
1101 lock_sock(sk);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001102 queue->nr_cqe = 0;
Potnuri Bharat Teja10407ec2019-07-08 15:22:00 +05301103 consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001104 release_sock(sk);
1105 return consumed;
1106}
1107
1108static void nvme_tcp_io_work(struct work_struct *w)
1109{
1110 struct nvme_tcp_queue *queue =
1111 container_of(w, struct nvme_tcp_queue, io_work);
Wunderlich, Markddef2952019-09-18 23:36:37 +00001112 unsigned long deadline = jiffies + msecs_to_jiffies(1);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001113
1114 do {
1115 bool pending = false;
1116 int result;
1117
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -07001118 if (mutex_trylock(&queue->send_mutex)) {
1119 result = nvme_tcp_try_send(queue);
1120 mutex_unlock(&queue->send_mutex);
1121 if (result > 0)
1122 pending = true;
1123 else if (unlikely(result < 0))
1124 break;
1125 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001126
1127 result = nvme_tcp_try_recv(queue);
1128 if (result > 0)
1129 pending = true;
Sagi Grimberg761ad262020-02-25 16:43:24 -08001130 else if (unlikely(result < 0))
Sagi Grimberg39d06079a2020-03-31 22:44:23 -07001131 return;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001132
1133 if (!pending)
1134 return;
1135
Wunderlich, Markddef2952019-09-18 23:36:37 +00001136 } while (!time_after(jiffies, deadline)); /* quota is exhausted */
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001137
1138 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
1139}
1140
1141static void nvme_tcp_free_crypto(struct nvme_tcp_queue *queue)
1142{
1143 struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
1144
1145 ahash_request_free(queue->rcv_hash);
1146 ahash_request_free(queue->snd_hash);
1147 crypto_free_ahash(tfm);
1148}
1149
1150static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue *queue)
1151{
1152 struct crypto_ahash *tfm;
1153
1154 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
1155 if (IS_ERR(tfm))
1156 return PTR_ERR(tfm);
1157
1158 queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1159 if (!queue->snd_hash)
1160 goto free_tfm;
1161 ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
1162
1163 queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1164 if (!queue->rcv_hash)
1165 goto free_snd_hash;
1166 ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
1167
1168 return 0;
1169free_snd_hash:
1170 ahash_request_free(queue->snd_hash);
1171free_tfm:
1172 crypto_free_ahash(tfm);
1173 return -ENOMEM;
1174}
1175
1176static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl)
1177{
1178 struct nvme_tcp_request *async = &ctrl->async_req;
1179
1180 page_frag_free(async->pdu);
1181}
1182
1183static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
1184{
1185 struct nvme_tcp_queue *queue = &ctrl->queues[0];
1186 struct nvme_tcp_request *async = &ctrl->async_req;
1187 u8 hdgst = nvme_tcp_hdgst_len(queue);
1188
1189 async->pdu = page_frag_alloc(&queue->pf_cache,
1190 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
1191 GFP_KERNEL | __GFP_ZERO);
1192 if (!async->pdu)
1193 return -ENOMEM;
1194
1195 async->queue = &ctrl->queues[0];
1196 return 0;
1197}
1198
1199static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
1200{
1201 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1202 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1203
1204 if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
1205 return;
1206
1207 if (queue->hdr_digest || queue->data_digest)
1208 nvme_tcp_free_crypto(queue);
1209
1210 sock_release(queue->sock);
1211 kfree(queue->pdu);
1212}
1213
1214static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
1215{
1216 struct nvme_tcp_icreq_pdu *icreq;
1217 struct nvme_tcp_icresp_pdu *icresp;
1218 struct msghdr msg = {};
1219 struct kvec iov;
1220 bool ctrl_hdgst, ctrl_ddgst;
1221 int ret;
1222
1223 icreq = kzalloc(sizeof(*icreq), GFP_KERNEL);
1224 if (!icreq)
1225 return -ENOMEM;
1226
1227 icresp = kzalloc(sizeof(*icresp), GFP_KERNEL);
1228 if (!icresp) {
1229 ret = -ENOMEM;
1230 goto free_icreq;
1231 }
1232
1233 icreq->hdr.type = nvme_tcp_icreq;
1234 icreq->hdr.hlen = sizeof(*icreq);
1235 icreq->hdr.pdo = 0;
1236 icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen);
1237 icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
1238 icreq->maxr2t = 0; /* single inflight r2t supported */
1239 icreq->hpda = 0; /* no alignment constraint */
1240 if (queue->hdr_digest)
1241 icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
1242 if (queue->data_digest)
1243 icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
1244
1245 iov.iov_base = icreq;
1246 iov.iov_len = sizeof(*icreq);
1247 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1248 if (ret < 0)
1249 goto free_icresp;
1250
1251 memset(&msg, 0, sizeof(msg));
1252 iov.iov_base = icresp;
1253 iov.iov_len = sizeof(*icresp);
1254 ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1255 iov.iov_len, msg.msg_flags);
1256 if (ret < 0)
1257 goto free_icresp;
1258
1259 ret = -EINVAL;
1260 if (icresp->hdr.type != nvme_tcp_icresp) {
1261 pr_err("queue %d: bad type returned %d\n",
1262 nvme_tcp_queue_id(queue), icresp->hdr.type);
1263 goto free_icresp;
1264 }
1265
1266 if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) {
1267 pr_err("queue %d: bad pdu length returned %d\n",
1268 nvme_tcp_queue_id(queue), icresp->hdr.plen);
1269 goto free_icresp;
1270 }
1271
1272 if (icresp->pfv != NVME_TCP_PFV_1_0) {
1273 pr_err("queue %d: bad pfv returned %d\n",
1274 nvme_tcp_queue_id(queue), icresp->pfv);
1275 goto free_icresp;
1276 }
1277
1278 ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE);
1279 if ((queue->data_digest && !ctrl_ddgst) ||
1280 (!queue->data_digest && ctrl_ddgst)) {
1281 pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
1282 nvme_tcp_queue_id(queue),
1283 queue->data_digest ? "enabled" : "disabled",
1284 ctrl_ddgst ? "enabled" : "disabled");
1285 goto free_icresp;
1286 }
1287
1288 ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE);
1289 if ((queue->hdr_digest && !ctrl_hdgst) ||
1290 (!queue->hdr_digest && ctrl_hdgst)) {
1291 pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
1292 nvme_tcp_queue_id(queue),
1293 queue->hdr_digest ? "enabled" : "disabled",
1294 ctrl_hdgst ? "enabled" : "disabled");
1295 goto free_icresp;
1296 }
1297
1298 if (icresp->cpda != 0) {
1299 pr_err("queue %d: unsupported cpda returned %d\n",
1300 nvme_tcp_queue_id(queue), icresp->cpda);
1301 goto free_icresp;
1302 }
1303
1304 ret = 0;
1305free_icresp:
1306 kfree(icresp);
1307free_icreq:
1308 kfree(icreq);
1309 return ret;
1310}
1311
Sagi Grimberg40510a62020-02-25 15:53:09 -08001312static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue)
1313{
1314 return nvme_tcp_queue_id(queue) == 0;
1315}
1316
1317static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue)
1318{
1319 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1320 int qid = nvme_tcp_queue_id(queue);
1321
1322 return !nvme_tcp_admin_queue(queue) &&
1323 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT];
1324}
1325
1326static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue)
1327{
1328 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1329 int qid = nvme_tcp_queue_id(queue);
1330
1331 return !nvme_tcp_admin_queue(queue) &&
1332 !nvme_tcp_default_queue(queue) &&
1333 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1334 ctrl->io_queues[HCTX_TYPE_READ];
1335}
1336
1337static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue)
1338{
1339 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1340 int qid = nvme_tcp_queue_id(queue);
1341
1342 return !nvme_tcp_admin_queue(queue) &&
1343 !nvme_tcp_default_queue(queue) &&
1344 !nvme_tcp_read_queue(queue) &&
1345 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1346 ctrl->io_queues[HCTX_TYPE_READ] +
1347 ctrl->io_queues[HCTX_TYPE_POLL];
1348}
1349
1350static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue)
1351{
1352 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1353 int qid = nvme_tcp_queue_id(queue);
1354 int n = 0;
1355
1356 if (nvme_tcp_default_queue(queue))
1357 n = qid - 1;
1358 else if (nvme_tcp_read_queue(queue))
1359 n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] - 1;
1360 else if (nvme_tcp_poll_queue(queue))
1361 n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] -
1362 ctrl->io_queues[HCTX_TYPE_READ] - 1;
1363 queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
1364}
1365
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001366static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl,
1367 int qid, size_t queue_size)
1368{
1369 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1370 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +02001371 int ret, rcv_pdu_size;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001372
1373 queue->ctrl = ctrl;
Sagi Grimberg15ec9282020-06-18 17:30:22 -07001374 init_llist_head(&queue->req_list);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001375 INIT_LIST_HEAD(&queue->send_list);
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -07001376 mutex_init(&queue->send_mutex);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001377 INIT_WORK(&queue->io_work, nvme_tcp_io_work);
1378 queue->queue_size = queue_size;
1379
1380 if (qid > 0)
Israel Rukshin9924b032019-08-18 12:08:53 +03001381 queue->cmnd_capsule_len = nctrl->ioccsz * 16;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001382 else
1383 queue->cmnd_capsule_len = sizeof(struct nvme_command) +
1384 NVME_TCP_ADMIN_CCSZ;
1385
1386 ret = sock_create(ctrl->addr.ss_family, SOCK_STREAM,
1387 IPPROTO_TCP, &queue->sock);
1388 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001389 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001390 "failed to create socket: %d\n", ret);
1391 return ret;
1392 }
1393
1394 /* Single syn retry */
Christoph Hellwig557eadf2020-05-28 07:12:21 +02001395 tcp_sock_set_syncnt(queue->sock->sk, 1);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001396
1397 /* Set TCP no delay */
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02001398 tcp_sock_set_nodelay(queue->sock->sk);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001399
1400 /*
1401 * Cleanup whatever is sitting in the TCP transmit queue on socket
1402 * close. This is done to prevent stale data from being sent should
1403 * the network connection be restored before TCP times out.
1404 */
Christoph Hellwigc4335942020-05-28 07:12:10 +02001405 sock_no_linger(queue->sock->sk);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001406
Christoph Hellwig6e434962020-05-28 07:12:11 +02001407 if (so_priority > 0)
1408 sock_set_priority(queue->sock->sk, so_priority);
Wunderlich, Mark9912ade2020-01-16 00:46:12 +00001409
Israel Rukshinbb139852019-08-18 12:08:54 +03001410 /* Set socket type of service */
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +02001411 if (nctrl->opts->tos >= 0)
1412 ip_sock_set_tos(queue->sock->sk, nctrl->opts->tos);
Israel Rukshinbb139852019-08-18 12:08:54 +03001413
Sagi Grimbergadc99fd2020-07-23 16:42:26 -07001414 /* Set 10 seconds timeout for icresp recvmsg */
1415 queue->sock->sk->sk_rcvtimeo = 10 * HZ;
1416
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001417 queue->sock->sk->sk_allocation = GFP_ATOMIC;
Sagi Grimberg40510a62020-02-25 15:53:09 -08001418 nvme_tcp_set_queue_io_cpu(queue);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001419 queue->request = NULL;
1420 queue->data_remaining = 0;
1421 queue->ddgst_remaining = 0;
1422 queue->pdu_remaining = 0;
1423 queue->pdu_offset = 0;
1424 sk_set_memalloc(queue->sock->sk);
1425
Israel Rukshin9924b032019-08-18 12:08:53 +03001426 if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001427 ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr,
1428 sizeof(ctrl->src_addr));
1429 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001430 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001431 "failed to bind queue %d socket %d\n",
1432 qid, ret);
1433 goto err_sock;
1434 }
1435 }
1436
1437 queue->hdr_digest = nctrl->opts->hdr_digest;
1438 queue->data_digest = nctrl->opts->data_digest;
1439 if (queue->hdr_digest || queue->data_digest) {
1440 ret = nvme_tcp_alloc_crypto(queue);
1441 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001442 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001443 "failed to allocate queue %d crypto\n", qid);
1444 goto err_sock;
1445 }
1446 }
1447
1448 rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) +
1449 nvme_tcp_hdgst_len(queue);
1450 queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL);
1451 if (!queue->pdu) {
1452 ret = -ENOMEM;
1453 goto err_crypto;
1454 }
1455
Israel Rukshin9924b032019-08-18 12:08:53 +03001456 dev_dbg(nctrl->device, "connecting queue %d\n",
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001457 nvme_tcp_queue_id(queue));
1458
1459 ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr,
1460 sizeof(ctrl->addr), 0);
1461 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001462 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001463 "failed to connect socket: %d\n", ret);
1464 goto err_rcv_pdu;
1465 }
1466
1467 ret = nvme_tcp_init_connection(queue);
1468 if (ret)
1469 goto err_init_connect;
1470
1471 queue->rd_enabled = true;
1472 set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags);
1473 nvme_tcp_init_recv_ctx(queue);
1474
1475 write_lock_bh(&queue->sock->sk->sk_callback_lock);
1476 queue->sock->sk->sk_user_data = queue;
1477 queue->state_change = queue->sock->sk->sk_state_change;
1478 queue->data_ready = queue->sock->sk->sk_data_ready;
1479 queue->write_space = queue->sock->sk->sk_write_space;
1480 queue->sock->sk->sk_data_ready = nvme_tcp_data_ready;
1481 queue->sock->sk->sk_state_change = nvme_tcp_state_change;
1482 queue->sock->sk->sk_write_space = nvme_tcp_write_space;
Sebastian Andrzej Siewiorac1c4e12019-10-10 17:34:12 +02001483#ifdef CONFIG_NET_RX_BUSY_POLL
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001484 queue->sock->sk->sk_ll_usec = 1;
Sebastian Andrzej Siewiorac1c4e12019-10-10 17:34:12 +02001485#endif
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001486 write_unlock_bh(&queue->sock->sk->sk_callback_lock);
1487
1488 return 0;
1489
1490err_init_connect:
1491 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1492err_rcv_pdu:
1493 kfree(queue->pdu);
1494err_crypto:
1495 if (queue->hdr_digest || queue->data_digest)
1496 nvme_tcp_free_crypto(queue);
1497err_sock:
1498 sock_release(queue->sock);
1499 queue->sock = NULL;
1500 return ret;
1501}
1502
1503static void nvme_tcp_restore_sock_calls(struct nvme_tcp_queue *queue)
1504{
1505 struct socket *sock = queue->sock;
1506
1507 write_lock_bh(&sock->sk->sk_callback_lock);
1508 sock->sk->sk_user_data = NULL;
1509 sock->sk->sk_data_ready = queue->data_ready;
1510 sock->sk->sk_state_change = queue->state_change;
1511 sock->sk->sk_write_space = queue->write_space;
1512 write_unlock_bh(&sock->sk->sk_callback_lock);
1513}
1514
1515static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
1516{
1517 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1518 nvme_tcp_restore_sock_calls(queue);
1519 cancel_work_sync(&queue->io_work);
1520}
1521
1522static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
1523{
1524 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1525 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1526
1527 if (!test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
1528 return;
1529
1530 __nvme_tcp_stop_queue(queue);
1531}
1532
1533static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
1534{
1535 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1536 int ret;
1537
1538 if (idx)
Sagi Grimberg26c68222018-12-14 11:06:08 -08001539 ret = nvmf_connect_io_queue(nctrl, idx, false);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001540 else
1541 ret = nvmf_connect_admin_queue(nctrl);
1542
1543 if (!ret) {
1544 set_bit(NVME_TCP_Q_LIVE, &ctrl->queues[idx].flags);
1545 } else {
Sagi Grimbergf34e2582019-04-29 16:25:48 -07001546 if (test_bit(NVME_TCP_Q_ALLOCATED, &ctrl->queues[idx].flags))
1547 __nvme_tcp_stop_queue(&ctrl->queues[idx]);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001548 dev_err(nctrl->device,
1549 "failed to connect queue: %d ret=%d\n", idx, ret);
1550 }
1551 return ret;
1552}
1553
1554static struct blk_mq_tag_set *nvme_tcp_alloc_tagset(struct nvme_ctrl *nctrl,
1555 bool admin)
1556{
1557 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1558 struct blk_mq_tag_set *set;
1559 int ret;
1560
1561 if (admin) {
1562 set = &ctrl->admin_tag_set;
1563 memset(set, 0, sizeof(*set));
1564 set->ops = &nvme_tcp_admin_mq_ops;
1565 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
1566 set->reserved_tags = 2; /* connect + keep-alive */
Max Gurtovoy610c8232020-06-16 12:34:24 +03001567 set->numa_node = nctrl->numa_node;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -07001568 set->flags = BLK_MQ_F_BLOCKING;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001569 set->cmd_size = sizeof(struct nvme_tcp_request);
1570 set->driver_data = ctrl;
1571 set->nr_hw_queues = 1;
1572 set->timeout = ADMIN_TIMEOUT;
1573 } else {
1574 set = &ctrl->tag_set;
1575 memset(set, 0, sizeof(*set));
1576 set->ops = &nvme_tcp_mq_ops;
1577 set->queue_depth = nctrl->sqsize + 1;
1578 set->reserved_tags = 1; /* fabric connect */
Max Gurtovoy610c8232020-06-16 12:34:24 +03001579 set->numa_node = nctrl->numa_node;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -07001580 set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001581 set->cmd_size = sizeof(struct nvme_tcp_request);
1582 set->driver_data = ctrl;
1583 set->nr_hw_queues = nctrl->queue_count - 1;
1584 set->timeout = NVME_IO_TIMEOUT;
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001585 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001586 }
1587
1588 ret = blk_mq_alloc_tag_set(set);
1589 if (ret)
1590 return ERR_PTR(ret);
1591
1592 return set;
1593}
1594
1595static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl)
1596{
1597 if (to_tcp_ctrl(ctrl)->async_req.pdu) {
1598 nvme_tcp_free_async_req(to_tcp_ctrl(ctrl));
1599 to_tcp_ctrl(ctrl)->async_req.pdu = NULL;
1600 }
1601
1602 nvme_tcp_free_queue(ctrl, 0);
1603}
1604
1605static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl)
1606{
1607 int i;
1608
1609 for (i = 1; i < ctrl->queue_count; i++)
1610 nvme_tcp_free_queue(ctrl, i);
1611}
1612
1613static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl)
1614{
1615 int i;
1616
1617 for (i = 1; i < ctrl->queue_count; i++)
1618 nvme_tcp_stop_queue(ctrl, i);
1619}
1620
1621static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl)
1622{
1623 int i, ret = 0;
1624
1625 for (i = 1; i < ctrl->queue_count; i++) {
1626 ret = nvme_tcp_start_queue(ctrl, i);
1627 if (ret)
1628 goto out_stop_queues;
1629 }
1630
1631 return 0;
1632
1633out_stop_queues:
1634 for (i--; i >= 1; i--)
1635 nvme_tcp_stop_queue(ctrl, i);
1636 return ret;
1637}
1638
1639static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
1640{
1641 int ret;
1642
1643 ret = nvme_tcp_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
1644 if (ret)
1645 return ret;
1646
1647 ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
1648 if (ret)
1649 goto out_free_queue;
1650
1651 return 0;
1652
1653out_free_queue:
1654 nvme_tcp_free_queue(ctrl, 0);
1655 return ret;
1656}
1657
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001658static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001659{
1660 int i, ret;
1661
1662 for (i = 1; i < ctrl->queue_count; i++) {
1663 ret = nvme_tcp_alloc_queue(ctrl, i,
1664 ctrl->sqsize + 1);
1665 if (ret)
1666 goto out_free_queues;
1667 }
1668
1669 return 0;
1670
1671out_free_queues:
1672 for (i--; i >= 1; i--)
1673 nvme_tcp_free_queue(ctrl, i);
1674
1675 return ret;
1676}
1677
1678static unsigned int nvme_tcp_nr_io_queues(struct nvme_ctrl *ctrl)
1679{
Sagi Grimberg873946f2018-12-11 23:38:57 -08001680 unsigned int nr_io_queues;
1681
1682 nr_io_queues = min(ctrl->opts->nr_io_queues, num_online_cpus());
1683 nr_io_queues += min(ctrl->opts->nr_write_queues, num_online_cpus());
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001684 nr_io_queues += min(ctrl->opts->nr_poll_queues, num_online_cpus());
Sagi Grimberg873946f2018-12-11 23:38:57 -08001685
1686 return nr_io_queues;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001687}
1688
Sagi Grimberg64861992019-05-28 22:49:05 -07001689static void nvme_tcp_set_io_queues(struct nvme_ctrl *nctrl,
1690 unsigned int nr_io_queues)
1691{
1692 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1693 struct nvmf_ctrl_options *opts = nctrl->opts;
1694
1695 if (opts->nr_write_queues && opts->nr_io_queues < nr_io_queues) {
1696 /*
1697 * separate read/write queues
1698 * hand out dedicated default queues only after we have
1699 * sufficient read queues.
1700 */
1701 ctrl->io_queues[HCTX_TYPE_READ] = opts->nr_io_queues;
1702 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
1703 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1704 min(opts->nr_write_queues, nr_io_queues);
1705 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1706 } else {
1707 /*
1708 * shared read/write queues
1709 * either no write queues were requested, or we don't have
1710 * sufficient queue count to have dedicated default queues.
1711 */
1712 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1713 min(opts->nr_io_queues, nr_io_queues);
1714 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1715 }
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001716
1717 if (opts->nr_poll_queues && nr_io_queues) {
1718 /* map dedicated poll queues only if we have queues left */
1719 ctrl->io_queues[HCTX_TYPE_POLL] =
1720 min(opts->nr_poll_queues, nr_io_queues);
1721 }
Sagi Grimberg64861992019-05-28 22:49:05 -07001722}
1723
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001724static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001725{
1726 unsigned int nr_io_queues;
1727 int ret;
1728
1729 nr_io_queues = nvme_tcp_nr_io_queues(ctrl);
1730 ret = nvme_set_queue_count(ctrl, &nr_io_queues);
1731 if (ret)
1732 return ret;
1733
1734 ctrl->queue_count = nr_io_queues + 1;
1735 if (ctrl->queue_count < 2)
1736 return 0;
1737
1738 dev_info(ctrl->device,
1739 "creating %d I/O queues.\n", nr_io_queues);
1740
Sagi Grimberg64861992019-05-28 22:49:05 -07001741 nvme_tcp_set_io_queues(ctrl, nr_io_queues);
1742
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001743 return __nvme_tcp_alloc_io_queues(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001744}
1745
1746static void nvme_tcp_destroy_io_queues(struct nvme_ctrl *ctrl, bool remove)
1747{
1748 nvme_tcp_stop_io_queues(ctrl);
1749 if (remove) {
Sagi Grimberge85037a2018-12-31 23:58:30 -08001750 blk_cleanup_queue(ctrl->connect_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001751 blk_mq_free_tag_set(ctrl->tagset);
1752 }
1753 nvme_tcp_free_io_queues(ctrl);
1754}
1755
1756static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
1757{
1758 int ret;
1759
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001760 ret = nvme_tcp_alloc_io_queues(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001761 if (ret)
1762 return ret;
1763
1764 if (new) {
1765 ctrl->tagset = nvme_tcp_alloc_tagset(ctrl, false);
1766 if (IS_ERR(ctrl->tagset)) {
1767 ret = PTR_ERR(ctrl->tagset);
1768 goto out_free_io_queues;
1769 }
1770
Sagi Grimberge85037a2018-12-31 23:58:30 -08001771 ctrl->connect_q = blk_mq_init_queue(ctrl->tagset);
1772 if (IS_ERR(ctrl->connect_q)) {
1773 ret = PTR_ERR(ctrl->connect_q);
1774 goto out_free_tag_set;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001775 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001776 }
1777
1778 ret = nvme_tcp_start_io_queues(ctrl);
1779 if (ret)
1780 goto out_cleanup_connect_q;
1781
Sagi Grimberg2875b0a2020-07-24 15:10:12 -07001782 if (!new) {
1783 nvme_start_queues(ctrl);
1784 nvme_wait_freeze(ctrl);
1785 blk_mq_update_nr_hw_queues(ctrl->tagset,
1786 ctrl->queue_count - 1);
1787 nvme_unfreeze(ctrl);
1788 }
1789
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001790 return 0;
1791
1792out_cleanup_connect_q:
Sagi Grimberge85037a2018-12-31 23:58:30 -08001793 if (new)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001794 blk_cleanup_queue(ctrl->connect_q);
1795out_free_tag_set:
1796 if (new)
1797 blk_mq_free_tag_set(ctrl->tagset);
1798out_free_io_queues:
1799 nvme_tcp_free_io_queues(ctrl);
1800 return ret;
1801}
1802
1803static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl *ctrl, bool remove)
1804{
1805 nvme_tcp_stop_queue(ctrl, 0);
1806 if (remove) {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001807 blk_cleanup_queue(ctrl->admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001808 blk_cleanup_queue(ctrl->fabrics_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001809 blk_mq_free_tag_set(ctrl->admin_tagset);
1810 }
1811 nvme_tcp_free_admin_queue(ctrl);
1812}
1813
1814static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
1815{
1816 int error;
1817
1818 error = nvme_tcp_alloc_admin_queue(ctrl);
1819 if (error)
1820 return error;
1821
1822 if (new) {
1823 ctrl->admin_tagset = nvme_tcp_alloc_tagset(ctrl, true);
1824 if (IS_ERR(ctrl->admin_tagset)) {
1825 error = PTR_ERR(ctrl->admin_tagset);
1826 goto out_free_queue;
1827 }
1828
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001829 ctrl->fabrics_q = blk_mq_init_queue(ctrl->admin_tagset);
1830 if (IS_ERR(ctrl->fabrics_q)) {
1831 error = PTR_ERR(ctrl->fabrics_q);
1832 goto out_free_tagset;
1833 }
1834
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001835 ctrl->admin_q = blk_mq_init_queue(ctrl->admin_tagset);
1836 if (IS_ERR(ctrl->admin_q)) {
1837 error = PTR_ERR(ctrl->admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001838 goto out_cleanup_fabrics_q;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001839 }
1840 }
1841
1842 error = nvme_tcp_start_queue(ctrl, 0);
1843 if (error)
1844 goto out_cleanup_queue;
1845
Sagi Grimbergc0f2f452019-07-22 17:06:53 -07001846 error = nvme_enable_ctrl(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001847 if (error)
1848 goto out_stop_queue;
1849
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001850 blk_mq_unquiesce_queue(ctrl->admin_q);
1851
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001852 error = nvme_init_identify(ctrl);
1853 if (error)
1854 goto out_stop_queue;
1855
1856 return 0;
1857
1858out_stop_queue:
1859 nvme_tcp_stop_queue(ctrl, 0);
1860out_cleanup_queue:
1861 if (new)
1862 blk_cleanup_queue(ctrl->admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001863out_cleanup_fabrics_q:
1864 if (new)
1865 blk_cleanup_queue(ctrl->fabrics_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001866out_free_tagset:
1867 if (new)
1868 blk_mq_free_tag_set(ctrl->admin_tagset);
1869out_free_queue:
1870 nvme_tcp_free_admin_queue(ctrl);
1871 return error;
1872}
1873
1874static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
1875 bool remove)
1876{
1877 blk_mq_quiesce_queue(ctrl->admin_q);
1878 nvme_tcp_stop_queue(ctrl, 0);
Ming Lei622b8b62019-07-24 11:48:42 +08001879 if (ctrl->admin_tagset) {
Sagi Grimberg7a425892019-04-24 11:53:17 -07001880 blk_mq_tagset_busy_iter(ctrl->admin_tagset,
1881 nvme_cancel_request, ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +08001882 blk_mq_tagset_wait_completed_request(ctrl->admin_tagset);
1883 }
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001884 if (remove)
1885 blk_mq_unquiesce_queue(ctrl->admin_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001886 nvme_tcp_destroy_admin_queue(ctrl, remove);
1887}
1888
1889static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
1890 bool remove)
1891{
1892 if (ctrl->queue_count <= 1)
1893 return;
Sagi Grimberg2875b0a2020-07-24 15:10:12 -07001894 nvme_start_freeze(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001895 nvme_stop_queues(ctrl);
1896 nvme_tcp_stop_io_queues(ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +08001897 if (ctrl->tagset) {
Sagi Grimberg7a425892019-04-24 11:53:17 -07001898 blk_mq_tagset_busy_iter(ctrl->tagset,
1899 nvme_cancel_request, ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +08001900 blk_mq_tagset_wait_completed_request(ctrl->tagset);
1901 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001902 if (remove)
1903 nvme_start_queues(ctrl);
1904 nvme_tcp_destroy_io_queues(ctrl, remove);
1905}
1906
1907static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl)
1908{
1909 /* If we are resetting/deleting then do nothing */
1910 if (ctrl->state != NVME_CTRL_CONNECTING) {
1911 WARN_ON_ONCE(ctrl->state == NVME_CTRL_NEW ||
1912 ctrl->state == NVME_CTRL_LIVE);
1913 return;
1914 }
1915
1916 if (nvmf_should_reconnect(ctrl)) {
1917 dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
1918 ctrl->opts->reconnect_delay);
1919 queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
1920 ctrl->opts->reconnect_delay * HZ);
1921 } else {
1922 dev_info(ctrl->device, "Removing controller...\n");
1923 nvme_delete_ctrl(ctrl);
1924 }
1925}
1926
1927static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
1928{
1929 struct nvmf_ctrl_options *opts = ctrl->opts;
Colin Ian King312910f2019-09-05 15:34:35 +01001930 int ret;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001931
1932 ret = nvme_tcp_configure_admin_queue(ctrl, new);
1933 if (ret)
1934 return ret;
1935
1936 if (ctrl->icdoff) {
1937 dev_err(ctrl->device, "icdoff is not supported!\n");
1938 goto destroy_admin;
1939 }
1940
1941 if (opts->queue_size > ctrl->sqsize + 1)
1942 dev_warn(ctrl->device,
1943 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1944 opts->queue_size, ctrl->sqsize + 1);
1945
1946 if (ctrl->sqsize + 1 > ctrl->maxcmd) {
1947 dev_warn(ctrl->device,
1948 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1949 ctrl->sqsize + 1, ctrl->maxcmd);
1950 ctrl->sqsize = ctrl->maxcmd - 1;
1951 }
1952
1953 if (ctrl->queue_count > 1) {
1954 ret = nvme_tcp_configure_io_queues(ctrl, new);
1955 if (ret)
1956 goto destroy_admin;
1957 }
1958
1959 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
Israel Rukshinbea54ef2020-03-24 17:29:45 +02001960 /*
Sagi Grimbergecca390e2020-07-22 16:32:19 -07001961 * state change failure is ok if we started ctrl delete,
Israel Rukshinbea54ef2020-03-24 17:29:45 +02001962 * unless we're during creation of a new controller to
1963 * avoid races with teardown flow.
1964 */
Sagi Grimbergecca390e2020-07-22 16:32:19 -07001965 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
1966 ctrl->state != NVME_CTRL_DELETING_NOIO);
Israel Rukshinbea54ef2020-03-24 17:29:45 +02001967 WARN_ON_ONCE(new);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001968 ret = -EINVAL;
1969 goto destroy_io;
1970 }
1971
1972 nvme_start_ctrl(ctrl);
1973 return 0;
1974
1975destroy_io:
1976 if (ctrl->queue_count > 1)
1977 nvme_tcp_destroy_io_queues(ctrl, new);
1978destroy_admin:
1979 nvme_tcp_stop_queue(ctrl, 0);
1980 nvme_tcp_destroy_admin_queue(ctrl, new);
1981 return ret;
1982}
1983
1984static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
1985{
1986 struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
1987 struct nvme_tcp_ctrl, connect_work);
1988 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
1989
1990 ++ctrl->nr_reconnects;
1991
1992 if (nvme_tcp_setup_ctrl(ctrl, false))
1993 goto requeue;
1994
Colin Ian King56a77d22018-12-14 11:42:43 +00001995 dev_info(ctrl->device, "Successfully reconnected (%d attempt)\n",
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001996 ctrl->nr_reconnects);
1997
1998 ctrl->nr_reconnects = 0;
1999
2000 return;
2001
2002requeue:
2003 dev_info(ctrl->device, "Failed reconnect attempt %d\n",
2004 ctrl->nr_reconnects);
2005 nvme_tcp_reconnect_or_remove(ctrl);
2006}
2007
2008static void nvme_tcp_error_recovery_work(struct work_struct *work)
2009{
2010 struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
2011 struct nvme_tcp_ctrl, err_work);
2012 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2013
2014 nvme_stop_keep_alive(ctrl);
2015 nvme_tcp_teardown_io_queues(ctrl, false);
2016 /* unquiesce to fail fast pending requests */
2017 nvme_start_queues(ctrl);
2018 nvme_tcp_teardown_admin_queue(ctrl, false);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07002019 blk_mq_unquiesce_queue(ctrl->admin_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002020
2021 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
Sagi Grimbergecca390e2020-07-22 16:32:19 -07002022 /* state change failure is ok if we started ctrl delete */
2023 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2024 ctrl->state != NVME_CTRL_DELETING_NOIO);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002025 return;
2026 }
2027
2028 nvme_tcp_reconnect_or_remove(ctrl);
2029}
2030
2031static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
2032{
Sagi Grimberg794a4cb2019-01-01 00:19:30 -08002033 cancel_work_sync(&to_tcp_ctrl(ctrl)->err_work);
2034 cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
2035
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002036 nvme_tcp_teardown_io_queues(ctrl, shutdown);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07002037 blk_mq_quiesce_queue(ctrl->admin_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002038 if (shutdown)
2039 nvme_shutdown_ctrl(ctrl);
2040 else
Sagi Grimbergb5b05042019-07-22 17:06:54 -07002041 nvme_disable_ctrl(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002042 nvme_tcp_teardown_admin_queue(ctrl, shutdown);
2043}
2044
2045static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
2046{
2047 nvme_tcp_teardown_ctrl(ctrl, true);
2048}
2049
2050static void nvme_reset_ctrl_work(struct work_struct *work)
2051{
2052 struct nvme_ctrl *ctrl =
2053 container_of(work, struct nvme_ctrl, reset_work);
2054
2055 nvme_stop_ctrl(ctrl);
2056 nvme_tcp_teardown_ctrl(ctrl, false);
2057
2058 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
Sagi Grimbergecca390e2020-07-22 16:32:19 -07002059 /* state change failure is ok if we started ctrl delete */
2060 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2061 ctrl->state != NVME_CTRL_DELETING_NOIO);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002062 return;
2063 }
2064
2065 if (nvme_tcp_setup_ctrl(ctrl, false))
2066 goto out_fail;
2067
2068 return;
2069
2070out_fail:
2071 ++ctrl->nr_reconnects;
2072 nvme_tcp_reconnect_or_remove(ctrl);
2073}
2074
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002075static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
2076{
2077 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
2078
2079 if (list_empty(&ctrl->list))
2080 goto free_ctrl;
2081
2082 mutex_lock(&nvme_tcp_ctrl_mutex);
2083 list_del(&ctrl->list);
2084 mutex_unlock(&nvme_tcp_ctrl_mutex);
2085
2086 nvmf_free_options(nctrl->opts);
2087free_ctrl:
2088 kfree(ctrl->queues);
2089 kfree(ctrl);
2090}
2091
2092static void nvme_tcp_set_sg_null(struct nvme_command *c)
2093{
2094 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2095
2096 sg->addr = 0;
2097 sg->length = 0;
2098 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2099 NVME_SGL_FMT_TRANSPORT_A;
2100}
2101
2102static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
2103 struct nvme_command *c, u32 data_len)
2104{
2105 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2106
2107 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
2108 sg->length = cpu_to_le32(data_len);
2109 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
2110}
2111
2112static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
2113 u32 data_len)
2114{
2115 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2116
2117 sg->addr = 0;
2118 sg->length = cpu_to_le32(data_len);
2119 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2120 NVME_SGL_FMT_TRANSPORT_A;
2121}
2122
2123static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
2124{
2125 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
2126 struct nvme_tcp_queue *queue = &ctrl->queues[0];
2127 struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
2128 struct nvme_command *cmd = &pdu->cmd;
2129 u8 hdgst = nvme_tcp_hdgst_len(queue);
2130
2131 memset(pdu, 0, sizeof(*pdu));
2132 pdu->hdr.type = nvme_tcp_cmd;
2133 if (queue->hdr_digest)
2134 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2135 pdu->hdr.hlen = sizeof(*pdu);
2136 pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
2137
2138 cmd->common.opcode = nvme_admin_async_event;
2139 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
2140 cmd->common.flags |= NVME_CMD_SGL_METABUF;
2141 nvme_tcp_set_sg_null(cmd);
2142
2143 ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
2144 ctrl->async_req.offset = 0;
2145 ctrl->async_req.curr_bio = NULL;
2146 ctrl->async_req.data_len = 0;
2147
Sagi Grimberg86f03482020-06-18 17:30:23 -07002148 nvme_tcp_queue_request(&ctrl->async_req, true, true);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002149}
2150
2151static enum blk_eh_timer_return
2152nvme_tcp_timeout(struct request *rq, bool reserved)
2153{
2154 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2155 struct nvme_tcp_ctrl *ctrl = req->queue->ctrl;
2156 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2157
Keith Busch92b98e82019-09-05 08:09:33 -06002158 /*
2159 * Restart the timer if a controller reset is already scheduled. Any
2160 * timed out commands would be handled before entering the connecting
2161 * state.
2162 */
2163 if (ctrl->ctrl.state == NVME_CTRL_RESETTING)
2164 return BLK_EH_RESET_TIMER;
2165
Sagi Grimberg39d57752019-01-08 01:01:30 -08002166 dev_warn(ctrl->ctrl.device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002167 "queue %d: timeout request %#x type %d\n",
Sagi Grimberg39d57752019-01-08 01:01:30 -08002168 nvme_tcp_queue_id(req->queue), rq->tag, pdu->hdr.type);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002169
2170 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
Sagi Grimberg39d57752019-01-08 01:01:30 -08002171 /*
2172 * Teardown immediately if controller times out while starting
2173 * or we are already started error recovery. all outstanding
2174 * requests are completed on shutdown, so we return BLK_EH_DONE.
2175 */
2176 flush_work(&ctrl->err_work);
2177 nvme_tcp_teardown_io_queues(&ctrl->ctrl, false);
2178 nvme_tcp_teardown_admin_queue(&ctrl->ctrl, false);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002179 return BLK_EH_DONE;
2180 }
2181
Sagi Grimberg39d57752019-01-08 01:01:30 -08002182 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002183 nvme_tcp_error_recovery(&ctrl->ctrl);
2184
2185 return BLK_EH_RESET_TIMER;
2186}
2187
2188static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
2189 struct request *rq)
2190{
2191 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2192 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2193 struct nvme_command *c = &pdu->cmd;
2194
2195 c->common.flags |= NVME_CMD_SGL_METABUF;
2196
Sagi Grimberg25e5cb72020-03-23 15:06:30 -07002197 if (!blk_rq_nr_phys_segments(rq))
2198 nvme_tcp_set_sg_null(c);
2199 else if (rq_data_dir(rq) == WRITE &&
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002200 req->data_len <= nvme_tcp_inline_data_size(queue))
2201 nvme_tcp_set_sg_inline(queue, c, req->data_len);
2202 else
2203 nvme_tcp_set_sg_host_data(c, req->data_len);
2204
2205 return 0;
2206}
2207
2208static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
2209 struct request *rq)
2210{
2211 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2212 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2213 struct nvme_tcp_queue *queue = req->queue;
2214 u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
2215 blk_status_t ret;
2216
2217 ret = nvme_setup_cmd(ns, rq, &pdu->cmd);
2218 if (ret)
2219 return ret;
2220
2221 req->state = NVME_TCP_SEND_CMD_PDU;
2222 req->offset = 0;
2223 req->data_sent = 0;
2224 req->pdu_len = 0;
2225 req->pdu_sent = 0;
Sagi Grimberg25e5cb72020-03-23 15:06:30 -07002226 req->data_len = blk_rq_nr_phys_segments(rq) ?
2227 blk_rq_payload_bytes(rq) : 0;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002228 req->curr_bio = rq->bio;
2229
2230 if (rq_data_dir(rq) == WRITE &&
2231 req->data_len <= nvme_tcp_inline_data_size(queue))
2232 req->pdu_len = req->data_len;
2233 else if (req->curr_bio)
2234 nvme_tcp_init_iter(req, READ);
2235
2236 pdu->hdr.type = nvme_tcp_cmd;
2237 pdu->hdr.flags = 0;
2238 if (queue->hdr_digest)
2239 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2240 if (queue->data_digest && req->pdu_len) {
2241 pdu->hdr.flags |= NVME_TCP_F_DDGST;
2242 ddgst = nvme_tcp_ddgst_len(queue);
2243 }
2244 pdu->hdr.hlen = sizeof(*pdu);
2245 pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
2246 pdu->hdr.plen =
2247 cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
2248
2249 ret = nvme_tcp_map_data(queue, rq);
2250 if (unlikely(ret)) {
Max Gurtovoy28a4cac2019-10-13 19:57:38 +03002251 nvme_cleanup_cmd(rq);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002252 dev_err(queue->ctrl->ctrl.device,
2253 "Failed to map data (%d)\n", ret);
2254 return ret;
2255 }
2256
2257 return 0;
2258}
2259
Sagi Grimberg86f03482020-06-18 17:30:23 -07002260static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx)
2261{
2262 struct nvme_tcp_queue *queue = hctx->driver_data;
2263
2264 if (!llist_empty(&queue->req_list))
2265 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
2266}
2267
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002268static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
2269 const struct blk_mq_queue_data *bd)
2270{
2271 struct nvme_ns *ns = hctx->queue->queuedata;
2272 struct nvme_tcp_queue *queue = hctx->driver_data;
2273 struct request *rq = bd->rq;
2274 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2275 bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
2276 blk_status_t ret;
2277
2278 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2279 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
2280
2281 ret = nvme_tcp_setup_cmd_pdu(ns, rq);
2282 if (unlikely(ret))
2283 return ret;
2284
2285 blk_mq_start_request(rq);
2286
Sagi Grimberg86f03482020-06-18 17:30:23 -07002287 nvme_tcp_queue_request(req, true, bd->last);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002288
2289 return BLK_STS_OK;
2290}
2291
Sagi Grimberg873946f2018-12-11 23:38:57 -08002292static int nvme_tcp_map_queues(struct blk_mq_tag_set *set)
2293{
2294 struct nvme_tcp_ctrl *ctrl = set->driver_data;
Sagi Grimberg64861992019-05-28 22:49:05 -07002295 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg873946f2018-12-11 23:38:57 -08002296
Sagi Grimberg64861992019-05-28 22:49:05 -07002297 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
Sagi Grimberg873946f2018-12-11 23:38:57 -08002298 /* separate read/write queues */
2299 set->map[HCTX_TYPE_DEFAULT].nr_queues =
Sagi Grimberg64861992019-05-28 22:49:05 -07002300 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2301 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2302 set->map[HCTX_TYPE_READ].nr_queues =
2303 ctrl->io_queues[HCTX_TYPE_READ];
Sagi Grimberg873946f2018-12-11 23:38:57 -08002304 set->map[HCTX_TYPE_READ].queue_offset =
Sagi Grimberg64861992019-05-28 22:49:05 -07002305 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimberg873946f2018-12-11 23:38:57 -08002306 } else {
Sagi Grimberg64861992019-05-28 22:49:05 -07002307 /* shared read/write queues */
Sagi Grimberg873946f2018-12-11 23:38:57 -08002308 set->map[HCTX_TYPE_DEFAULT].nr_queues =
Sagi Grimberg64861992019-05-28 22:49:05 -07002309 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2310 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2311 set->map[HCTX_TYPE_READ].nr_queues =
2312 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimberg873946f2018-12-11 23:38:57 -08002313 set->map[HCTX_TYPE_READ].queue_offset = 0;
2314 }
2315 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
2316 blk_mq_map_queues(&set->map[HCTX_TYPE_READ]);
Sagi Grimberg64861992019-05-28 22:49:05 -07002317
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002318 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
2319 /* map dedicated poll queues only if we have queues left */
2320 set->map[HCTX_TYPE_POLL].nr_queues =
2321 ctrl->io_queues[HCTX_TYPE_POLL];
2322 set->map[HCTX_TYPE_POLL].queue_offset =
2323 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
2324 ctrl->io_queues[HCTX_TYPE_READ];
2325 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
2326 }
2327
Sagi Grimberg64861992019-05-28 22:49:05 -07002328 dev_info(ctrl->ctrl.device,
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002329 "mapped %d/%d/%d default/read/poll queues.\n",
Sagi Grimberg64861992019-05-28 22:49:05 -07002330 ctrl->io_queues[HCTX_TYPE_DEFAULT],
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002331 ctrl->io_queues[HCTX_TYPE_READ],
2332 ctrl->io_queues[HCTX_TYPE_POLL]);
Sagi Grimberg64861992019-05-28 22:49:05 -07002333
Sagi Grimberg873946f2018-12-11 23:38:57 -08002334 return 0;
2335}
2336
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002337static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx)
2338{
2339 struct nvme_tcp_queue *queue = hctx->driver_data;
2340 struct sock *sk = queue->sock->sk;
2341
Sagi Grimbergf86e5bf2020-03-23 16:43:52 -07002342 if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2343 return 0;
2344
Sagi Grimberg72e5d752020-05-01 14:25:44 -07002345 set_bit(NVME_TCP_Q_POLLING, &queue->flags);
Eric Dumazet3f926af2019-10-23 22:44:51 -07002346 if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue))
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002347 sk_busy_loop(sk, true);
2348 nvme_tcp_try_recv(queue);
Sagi Grimberg72e5d752020-05-01 14:25:44 -07002349 clear_bit(NVME_TCP_Q_POLLING, &queue->flags);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002350 return queue->nr_cqe;
2351}
2352
Rikard Falkeborn6acbd962020-05-29 00:25:07 +02002353static const struct blk_mq_ops nvme_tcp_mq_ops = {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002354 .queue_rq = nvme_tcp_queue_rq,
Sagi Grimberg86f03482020-06-18 17:30:23 -07002355 .commit_rqs = nvme_tcp_commit_rqs,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002356 .complete = nvme_complete_rq,
2357 .init_request = nvme_tcp_init_request,
2358 .exit_request = nvme_tcp_exit_request,
2359 .init_hctx = nvme_tcp_init_hctx,
2360 .timeout = nvme_tcp_timeout,
Sagi Grimberg873946f2018-12-11 23:38:57 -08002361 .map_queues = nvme_tcp_map_queues,
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002362 .poll = nvme_tcp_poll,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002363};
2364
Rikard Falkeborn6acbd962020-05-29 00:25:07 +02002365static const struct blk_mq_ops nvme_tcp_admin_mq_ops = {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002366 .queue_rq = nvme_tcp_queue_rq,
2367 .complete = nvme_complete_rq,
2368 .init_request = nvme_tcp_init_request,
2369 .exit_request = nvme_tcp_exit_request,
2370 .init_hctx = nvme_tcp_init_admin_hctx,
2371 .timeout = nvme_tcp_timeout,
2372};
2373
2374static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
2375 .name = "tcp",
2376 .module = THIS_MODULE,
2377 .flags = NVME_F_FABRICS,
2378 .reg_read32 = nvmf_reg_read32,
2379 .reg_read64 = nvmf_reg_read64,
2380 .reg_write32 = nvmf_reg_write32,
2381 .free_ctrl = nvme_tcp_free_ctrl,
2382 .submit_async_event = nvme_tcp_submit_async_event,
2383 .delete_ctrl = nvme_tcp_delete_ctrl,
2384 .get_address = nvmf_get_address,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002385};
2386
2387static bool
2388nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
2389{
2390 struct nvme_tcp_ctrl *ctrl;
2391 bool found = false;
2392
2393 mutex_lock(&nvme_tcp_ctrl_mutex);
2394 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
2395 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2396 if (found)
2397 break;
2398 }
2399 mutex_unlock(&nvme_tcp_ctrl_mutex);
2400
2401 return found;
2402}
2403
2404static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
2405 struct nvmf_ctrl_options *opts)
2406{
2407 struct nvme_tcp_ctrl *ctrl;
2408 int ret;
2409
2410 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2411 if (!ctrl)
2412 return ERR_PTR(-ENOMEM);
2413
2414 INIT_LIST_HEAD(&ctrl->list);
2415 ctrl->ctrl.opts = opts;
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002416 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2417 opts->nr_poll_queues + 1;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002418 ctrl->ctrl.sqsize = opts->queue_size - 1;
2419 ctrl->ctrl.kato = opts->kato;
2420
2421 INIT_DELAYED_WORK(&ctrl->connect_work,
2422 nvme_tcp_reconnect_ctrl_work);
2423 INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
2424 INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
2425
2426 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2427 opts->trsvcid =
2428 kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
2429 if (!opts->trsvcid) {
2430 ret = -ENOMEM;
2431 goto out_free_ctrl;
2432 }
2433 opts->mask |= NVMF_OPT_TRSVCID;
2434 }
2435
2436 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2437 opts->traddr, opts->trsvcid, &ctrl->addr);
2438 if (ret) {
2439 pr_err("malformed address passed: %s:%s\n",
2440 opts->traddr, opts->trsvcid);
2441 goto out_free_ctrl;
2442 }
2443
2444 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2445 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2446 opts->host_traddr, NULL, &ctrl->src_addr);
2447 if (ret) {
2448 pr_err("malformed src address passed: %s\n",
2449 opts->host_traddr);
2450 goto out_free_ctrl;
2451 }
2452 }
2453
2454 if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
2455 ret = -EALREADY;
2456 goto out_free_ctrl;
2457 }
2458
Sagi Grimberg873946f2018-12-11 23:38:57 -08002459 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002460 GFP_KERNEL);
2461 if (!ctrl->queues) {
2462 ret = -ENOMEM;
2463 goto out_free_ctrl;
2464 }
2465
2466 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
2467 if (ret)
2468 goto out_kfree_queues;
2469
2470 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2471 WARN_ON_ONCE(1);
2472 ret = -EINTR;
2473 goto out_uninit_ctrl;
2474 }
2475
2476 ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
2477 if (ret)
2478 goto out_uninit_ctrl;
2479
2480 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
2481 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2482
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002483 mutex_lock(&nvme_tcp_ctrl_mutex);
2484 list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
2485 mutex_unlock(&nvme_tcp_ctrl_mutex);
2486
2487 return &ctrl->ctrl;
2488
2489out_uninit_ctrl:
2490 nvme_uninit_ctrl(&ctrl->ctrl);
2491 nvme_put_ctrl(&ctrl->ctrl);
2492 if (ret > 0)
2493 ret = -EIO;
2494 return ERR_PTR(ret);
2495out_kfree_queues:
2496 kfree(ctrl->queues);
2497out_free_ctrl:
2498 kfree(ctrl);
2499 return ERR_PTR(ret);
2500}
2501
2502static struct nvmf_transport_ops nvme_tcp_transport = {
2503 .name = "tcp",
2504 .module = THIS_MODULE,
2505 .required_opts = NVMF_OPT_TRADDR,
2506 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2507 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
Sagi Grimberg873946f2018-12-11 23:38:57 -08002508 NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
Israel Rukshinbb139852019-08-18 12:08:54 +03002509 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2510 NVMF_OPT_TOS,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002511 .create_ctrl = nvme_tcp_create_ctrl,
2512};
2513
2514static int __init nvme_tcp_init_module(void)
2515{
2516 nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq",
2517 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2518 if (!nvme_tcp_wq)
2519 return -ENOMEM;
2520
2521 nvmf_register_transport(&nvme_tcp_transport);
2522 return 0;
2523}
2524
2525static void __exit nvme_tcp_cleanup_module(void)
2526{
2527 struct nvme_tcp_ctrl *ctrl;
2528
2529 nvmf_unregister_transport(&nvme_tcp_transport);
2530
2531 mutex_lock(&nvme_tcp_ctrl_mutex);
2532 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
2533 nvme_delete_ctrl(&ctrl->ctrl);
2534 mutex_unlock(&nvme_tcp_ctrl_mutex);
2535 flush_workqueue(nvme_delete_wq);
2536
2537 destroy_workqueue(nvme_tcp_wq);
2538}
2539
2540module_init(nvme_tcp_init_module);
2541module_exit(nvme_tcp_cleanup_module);
2542
2543MODULE_LICENSE("GPL v2");