blob: 4ceb28675fdf63b4b1dba86b0e790b925bf6b57b [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;
Daniel Wagner1ba2e502021-08-30 15:36:26 +020048 __le16 status;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080049 struct list_head entry;
Sagi Grimberg15ec9282020-06-18 17:30:22 -070050 struct llist_node lentry;
Christoph Hellwiga7273d42018-12-13 09:46:59 +010051 __le32 ddgst;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080052
53 struct bio *curr_bio;
54 struct iov_iter iter;
55
56 /* send state */
57 size_t offset;
58 size_t data_sent;
59 enum nvme_tcp_send_state state;
60};
61
62enum nvme_tcp_queue_flags {
63 NVME_TCP_Q_ALLOCATED = 0,
64 NVME_TCP_Q_LIVE = 1,
Sagi Grimberg72e5d752020-05-01 14:25:44 -070065 NVME_TCP_Q_POLLING = 2,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080066};
67
68enum nvme_tcp_recv_state {
69 NVME_TCP_RECV_PDU = 0,
70 NVME_TCP_RECV_DATA,
71 NVME_TCP_RECV_DDGST,
72};
73
74struct nvme_tcp_ctrl;
75struct nvme_tcp_queue {
76 struct socket *sock;
77 struct work_struct io_work;
78 int io_cpu;
79
Chao Leng9ebbfe42021-01-14 17:09:26 +080080 struct mutex queue_lock;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -070081 struct mutex send_mutex;
Sagi Grimberg15ec9282020-06-18 17:30:22 -070082 struct llist_head req_list;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080083 struct list_head send_list;
Sagi Grimberg122e5b92020-06-18 17:30:24 -070084 bool more_requests;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080085
86 /* recv state */
87 void *pdu;
88 int pdu_remaining;
89 int pdu_offset;
90 size_t data_remaining;
91 size_t ddgst_remaining;
Sagi Grimberg1a9460c2019-07-03 14:08:04 -070092 unsigned int nr_cqe;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080093
94 /* send state */
95 struct nvme_tcp_request *request;
96
97 int queue_size;
98 size_t cmnd_capsule_len;
99 struct nvme_tcp_ctrl *ctrl;
100 unsigned long flags;
101 bool rd_enabled;
102
103 bool hdr_digest;
104 bool data_digest;
105 struct ahash_request *rcv_hash;
106 struct ahash_request *snd_hash;
107 __le32 exp_ddgst;
108 __le32 recv_ddgst;
109
110 struct page_frag_cache pf_cache;
111
112 void (*state_change)(struct sock *);
113 void (*data_ready)(struct sock *);
114 void (*write_space)(struct sock *);
115};
116
117struct nvme_tcp_ctrl {
118 /* read only in the hot path */
119 struct nvme_tcp_queue *queues;
120 struct blk_mq_tag_set tag_set;
121
122 /* other member variables */
123 struct list_head list;
124 struct blk_mq_tag_set admin_tag_set;
125 struct sockaddr_storage addr;
126 struct sockaddr_storage src_addr;
127 struct nvme_ctrl ctrl;
128
129 struct work_struct err_work;
130 struct delayed_work connect_work;
131 struct nvme_tcp_request async_req;
Sagi Grimberg64861992019-05-28 22:49:05 -0700132 u32 io_queues[HCTX_MAX_TYPES];
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800133};
134
135static LIST_HEAD(nvme_tcp_ctrl_list);
136static DEFINE_MUTEX(nvme_tcp_ctrl_mutex);
137static struct workqueue_struct *nvme_tcp_wq;
Rikard Falkeborn6acbd962020-05-29 00:25:07 +0200138static const struct blk_mq_ops nvme_tcp_mq_ops;
139static const struct blk_mq_ops nvme_tcp_admin_mq_ops;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700140static int nvme_tcp_try_send(struct nvme_tcp_queue *queue);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800141
142static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl)
143{
144 return container_of(ctrl, struct nvme_tcp_ctrl, ctrl);
145}
146
147static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
148{
149 return queue - queue->ctrl->queues;
150}
151
152static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue)
153{
154 u32 queue_idx = nvme_tcp_queue_id(queue);
155
156 if (queue_idx == 0)
157 return queue->ctrl->admin_tag_set.tags[queue_idx];
158 return queue->ctrl->tag_set.tags[queue_idx - 1];
159}
160
161static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue)
162{
163 return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
164}
165
166static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue)
167{
168 return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
169}
170
171static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_queue *queue)
172{
173 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
174}
175
176static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req)
177{
178 return req == &req->queue->ctrl->async_req;
179}
180
181static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req)
182{
183 struct request *rq;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800184
185 if (unlikely(nvme_tcp_async_req(req)))
186 return false; /* async events don't have a request */
187
188 rq = blk_mq_rq_from_pdu(req);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800189
Sagi Grimberg25e5cb72020-03-23 15:06:30 -0700190 return rq_data_dir(rq) == WRITE && req->data_len &&
191 req->data_len <= nvme_tcp_inline_data_size(req->queue);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800192}
193
194static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req)
195{
196 return req->iter.bvec->bv_page;
197}
198
199static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req)
200{
201 return req->iter.bvec->bv_offset + req->iter.iov_offset;
202}
203
204static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req)
205{
Sagi Grimbergca1ff672021-01-13 13:56:57 -0800206 return min_t(size_t, iov_iter_single_seg_count(&req->iter),
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800207 req->pdu_len - req->pdu_sent);
208}
209
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800210static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req)
211{
212 return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ?
213 req->pdu_len - req->pdu_sent : 0;
214}
215
216static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
217 int len)
218{
219 return nvme_tcp_pdu_data_left(req) <= len;
220}
221
222static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
223 unsigned int dir)
224{
225 struct request *rq = blk_mq_rq_from_pdu(req);
226 struct bio_vec *vec;
227 unsigned int size;
Sagi Grimberg0dc9eda2021-01-14 13:15:26 -0800228 int nr_bvec;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800229 size_t offset;
230
231 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
232 vec = &rq->special_vec;
Sagi Grimberg0dc9eda2021-01-14 13:15:26 -0800233 nr_bvec = 1;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800234 size = blk_rq_payload_bytes(rq);
235 offset = 0;
236 } else {
237 struct bio *bio = req->curr_bio;
Sagi Grimberg0dc9eda2021-01-14 13:15:26 -0800238 struct bvec_iter bi;
239 struct bio_vec bv;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800240
241 vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
Sagi Grimberg0dc9eda2021-01-14 13:15:26 -0800242 nr_bvec = 0;
243 bio_for_each_bvec(bv, bio, bi) {
244 nr_bvec++;
245 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800246 size = bio->bi_iter.bi_size;
247 offset = bio->bi_iter.bi_bvec_done;
248 }
249
Sagi Grimberg0dc9eda2021-01-14 13:15:26 -0800250 iov_iter_bvec(&req->iter, dir, vec, nr_bvec, size);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800251 req->iter.iov_offset = offset;
252}
253
254static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req,
255 int len)
256{
257 req->data_sent += len;
258 req->pdu_sent += len;
259 iov_iter_advance(&req->iter, len);
260 if (!iov_iter_count(&req->iter) &&
261 req->data_sent < req->data_len) {
262 req->curr_bio = req->curr_bio->bi_next;
263 nvme_tcp_init_iter(req, WRITE);
264 }
265}
266
Sagi Grimberg5c11f7d2020-12-21 00:03:39 -0800267static inline void nvme_tcp_send_all(struct nvme_tcp_queue *queue)
268{
269 int ret;
270
271 /* drain the send queue as much as we can... */
272 do {
273 ret = nvme_tcp_try_send(queue);
274 } while (ret > 0);
275}
276
Keith Busch70f437f2021-09-09 08:54:52 -0700277static inline bool nvme_tcp_queue_more(struct nvme_tcp_queue *queue)
278{
279 return !list_empty(&queue->send_list) ||
280 !llist_empty(&queue->req_list) || queue->more_requests;
281}
282
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700283static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req,
Sagi Grimberg86f03482020-06-18 17:30:23 -0700284 bool sync, bool last)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800285{
286 struct nvme_tcp_queue *queue = req->queue;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700287 bool empty;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800288
Sagi Grimberg15ec9282020-06-18 17:30:22 -0700289 empty = llist_add(&req->lentry, &queue->req_list) &&
290 list_empty(&queue->send_list) && !queue->request;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800291
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700292 /*
293 * if we're the first on the send_list and we can try to send
294 * directly, otherwise queue io_work. Also, only do that if we
295 * are on the same cpu, so we don't introduce contention.
296 */
Sagi Grimbergbb833372021-03-15 13:53:47 -0700297 if (queue->io_cpu == raw_smp_processor_id() &&
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700298 sync && empty && mutex_trylock(&queue->send_mutex)) {
Sagi Grimberg122e5b92020-06-18 17:30:24 -0700299 queue->more_requests = !last;
Sagi Grimberg5c11f7d2020-12-21 00:03:39 -0800300 nvme_tcp_send_all(queue);
Sagi Grimberg122e5b92020-06-18 17:30:24 -0700301 queue->more_requests = false;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700302 mutex_unlock(&queue->send_mutex);
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -0700303 }
Keith Busch70f437f2021-09-09 08:54:52 -0700304
305 if (last && nvme_tcp_queue_more(queue))
306 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800307}
308
Sagi Grimberg15ec9282020-06-18 17:30:22 -0700309static void nvme_tcp_process_req_list(struct nvme_tcp_queue *queue)
310{
311 struct nvme_tcp_request *req;
312 struct llist_node *node;
313
314 for (node = llist_del_all(&queue->req_list); node; node = node->next) {
315 req = llist_entry(node, struct nvme_tcp_request, lentry);
316 list_add(&req->entry, &queue->send_list);
317 }
318}
319
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800320static inline struct nvme_tcp_request *
321nvme_tcp_fetch_request(struct nvme_tcp_queue *queue)
322{
323 struct nvme_tcp_request *req;
324
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800325 req = list_first_entry_or_null(&queue->send_list,
326 struct nvme_tcp_request, entry);
Sagi Grimberg15ec9282020-06-18 17:30:22 -0700327 if (!req) {
328 nvme_tcp_process_req_list(queue);
329 req = list_first_entry_or_null(&queue->send_list,
330 struct nvme_tcp_request, entry);
331 if (unlikely(!req))
332 return NULL;
333 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800334
Sagi Grimberg15ec9282020-06-18 17:30:22 -0700335 list_del(&req->entry);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800336 return req;
337}
338
Christoph Hellwiga7273d42018-12-13 09:46:59 +0100339static inline void nvme_tcp_ddgst_final(struct ahash_request *hash,
340 __le32 *dgst)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800341{
342 ahash_request_set_crypt(hash, NULL, (u8 *)dgst, 0);
343 crypto_ahash_final(hash);
344}
345
346static inline void nvme_tcp_ddgst_update(struct ahash_request *hash,
347 struct page *page, off_t off, size_t len)
348{
349 struct scatterlist sg;
350
351 sg_init_marker(&sg, 1);
352 sg_set_page(&sg, page, len, off);
353 ahash_request_set_crypt(hash, &sg, NULL, len);
354 crypto_ahash_update(hash);
355}
356
357static inline void nvme_tcp_hdgst(struct ahash_request *hash,
358 void *pdu, size_t len)
359{
360 struct scatterlist sg;
361
362 sg_init_one(&sg, pdu, len);
363 ahash_request_set_crypt(hash, &sg, pdu + len, len);
364 crypto_ahash_digest(hash);
365}
366
367static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue,
368 void *pdu, size_t pdu_len)
369{
370 struct nvme_tcp_hdr *hdr = pdu;
371 __le32 recv_digest;
372 __le32 exp_digest;
373
374 if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
375 dev_err(queue->ctrl->ctrl.device,
376 "queue %d: header digest flag is cleared\n",
377 nvme_tcp_queue_id(queue));
378 return -EPROTO;
379 }
380
381 recv_digest = *(__le32 *)(pdu + hdr->hlen);
382 nvme_tcp_hdgst(queue->rcv_hash, pdu, pdu_len);
383 exp_digest = *(__le32 *)(pdu + hdr->hlen);
384 if (recv_digest != exp_digest) {
385 dev_err(queue->ctrl->ctrl.device,
386 "header digest error: recv %#x expected %#x\n",
387 le32_to_cpu(recv_digest), le32_to_cpu(exp_digest));
388 return -EIO;
389 }
390
391 return 0;
392}
393
394static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu)
395{
396 struct nvme_tcp_hdr *hdr = pdu;
397 u8 digest_len = nvme_tcp_hdgst_len(queue);
398 u32 len;
399
400 len = le32_to_cpu(hdr->plen) - hdr->hlen -
401 ((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0);
402
403 if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
404 dev_err(queue->ctrl->ctrl.device,
405 "queue %d: data digest flag is cleared\n",
406 nvme_tcp_queue_id(queue));
407 return -EPROTO;
408 }
409 crypto_ahash_init(queue->rcv_hash);
410
411 return 0;
412}
413
414static void nvme_tcp_exit_request(struct blk_mq_tag_set *set,
415 struct request *rq, unsigned int hctx_idx)
416{
417 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
418
419 page_frag_free(req->pdu);
420}
421
422static int nvme_tcp_init_request(struct blk_mq_tag_set *set,
423 struct request *rq, unsigned int hctx_idx,
424 unsigned int numa_node)
425{
426 struct nvme_tcp_ctrl *ctrl = set->driver_data;
427 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
Keith Buschf4b9e6c2021-03-17 13:37:03 -0700428 struct nvme_tcp_cmd_pdu *pdu;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800429 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
430 struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx];
431 u8 hdgst = nvme_tcp_hdgst_len(queue);
432
433 req->pdu = page_frag_alloc(&queue->pf_cache,
434 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
435 GFP_KERNEL | __GFP_ZERO);
436 if (!req->pdu)
437 return -ENOMEM;
438
Keith Buschf4b9e6c2021-03-17 13:37:03 -0700439 pdu = req->pdu;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800440 req->queue = queue;
441 nvme_req(rq)->ctrl = &ctrl->ctrl;
Keith Buschf4b9e6c2021-03-17 13:37:03 -0700442 nvme_req(rq)->cmd = &pdu->cmd;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800443
444 return 0;
445}
446
447static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
448 unsigned int hctx_idx)
449{
450 struct nvme_tcp_ctrl *ctrl = data;
451 struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1];
452
453 hctx->driver_data = queue;
454 return 0;
455}
456
457static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
458 unsigned int hctx_idx)
459{
460 struct nvme_tcp_ctrl *ctrl = data;
461 struct nvme_tcp_queue *queue = &ctrl->queues[0];
462
463 hctx->driver_data = queue;
464 return 0;
465}
466
467static enum nvme_tcp_recv_state
468nvme_tcp_recv_state(struct nvme_tcp_queue *queue)
469{
470 return (queue->pdu_remaining) ? NVME_TCP_RECV_PDU :
471 (queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST :
472 NVME_TCP_RECV_DATA;
473}
474
475static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
476{
477 queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) +
478 nvme_tcp_hdgst_len(queue);
479 queue->pdu_offset = 0;
480 queue->data_remaining = -1;
481 queue->ddgst_remaining = 0;
482}
483
484static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
485{
486 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
487 return;
488
Sagi Grimberg236187c2020-07-28 13:16:36 -0700489 dev_warn(ctrl->device, "starting error recovery\n");
Nigel Kirkland97b25122020-02-10 16:01:45 -0800490 queue_work(nvme_reset_wq, &to_tcp_ctrl(ctrl)->err_work);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800491}
492
493static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue,
494 struct nvme_completion *cqe)
495{
Daniel Wagner1ba2e502021-08-30 15:36:26 +0200496 struct nvme_tcp_request *req;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800497 struct request *rq;
498
Sagi Grimberge7006de2021-06-16 14:19:36 -0700499 rq = nvme_find_rq(nvme_tcp_tagset(queue), cqe->command_id);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800500 if (!rq) {
501 dev_err(queue->ctrl->ctrl.device,
Sagi Grimberge7006de2021-06-16 14:19:36 -0700502 "got bad cqe.command_id %#x on queue %d\n",
503 cqe->command_id, nvme_tcp_queue_id(queue));
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800504 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
505 return -EINVAL;
506 }
507
Daniel Wagner1ba2e502021-08-30 15:36:26 +0200508 req = blk_mq_rq_to_pdu(rq);
509 if (req->status == cpu_to_le16(NVME_SC_SUCCESS))
510 req->status = cqe->status;
511
512 if (!nvme_try_complete_req(rq, req->status, cqe->result))
Christoph Hellwigff029452020-06-11 08:44:52 +0200513 nvme_complete_rq(rq);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700514 queue->nr_cqe++;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800515
516 return 0;
517}
518
519static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
520 struct nvme_tcp_data_pdu *pdu)
521{
522 struct request *rq;
523
Sagi Grimberge7006de2021-06-16 14:19:36 -0700524 rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800525 if (!rq) {
526 dev_err(queue->ctrl->ctrl.device,
Sagi Grimberge7006de2021-06-16 14:19:36 -0700527 "got bad c2hdata.command_id %#x on queue %d\n",
528 pdu->command_id, nvme_tcp_queue_id(queue));
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800529 return -ENOENT;
530 }
531
532 if (!blk_rq_payload_bytes(rq)) {
533 dev_err(queue->ctrl->ctrl.device,
534 "queue %d tag %#x unexpected data\n",
535 nvme_tcp_queue_id(queue), rq->tag);
536 return -EIO;
537 }
538
539 queue->data_remaining = le32_to_cpu(pdu->data_length);
540
Sagi Grimberg602d6742019-03-13 18:55:10 +0100541 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS &&
542 unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) {
543 dev_err(queue->ctrl->ctrl.device,
544 "queue %d tag %#x SUCCESS set but not last PDU\n",
545 nvme_tcp_queue_id(queue), rq->tag);
546 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
547 return -EPROTO;
548 }
549
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800550 return 0;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800551}
552
553static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue,
554 struct nvme_tcp_rsp_pdu *pdu)
555{
556 struct nvme_completion *cqe = &pdu->cqe;
557 int ret = 0;
558
559 /*
560 * AEN requests are special as they don't time out and can
561 * survive any kind of queue freeze and often don't respond to
562 * aborts. We don't even bother to allocate a struct request
563 * for them but rather special case them here.
564 */
Israel Rukshin58a8df62019-10-13 19:57:31 +0300565 if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue),
566 cqe->command_id)))
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800567 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
568 &cqe->result);
569 else
570 ret = nvme_tcp_process_nvme_cqe(queue, cqe);
571
572 return ret;
573}
574
Varun Prakash1d3ef9c32021-11-23 16:28:56 +0530575static void nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800576 struct nvme_tcp_r2t_pdu *pdu)
577{
578 struct nvme_tcp_data_pdu *data = req->pdu;
579 struct nvme_tcp_queue *queue = req->queue;
580 struct request *rq = blk_mq_rq_from_pdu(req);
581 u8 hdgst = nvme_tcp_hdgst_len(queue);
582 u8 ddgst = nvme_tcp_ddgst_len(queue);
583
Varun Prakash1d3ef9c32021-11-23 16:28:56 +0530584 req->state = NVME_TCP_SEND_H2C_PDU;
585 req->offset = 0;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800586 req->pdu_len = le32_to_cpu(pdu->r2t_length);
587 req->pdu_sent = 0;
588
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800589 memset(data, 0, sizeof(*data));
590 data->hdr.type = nvme_tcp_h2c_data;
591 data->hdr.flags = NVME_TCP_F_DATA_LAST;
592 if (queue->hdr_digest)
593 data->hdr.flags |= NVME_TCP_F_HDGST;
594 if (queue->data_digest)
595 data->hdr.flags |= NVME_TCP_F_DDGST;
596 data->hdr.hlen = sizeof(*data);
597 data->hdr.pdo = data->hdr.hlen + hdgst;
598 data->hdr.plen =
599 cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst);
600 data->ttag = pdu->ttag;
Sagi Grimberge7006de2021-06-16 14:19:36 -0700601 data->command_id = nvme_cid(rq);
Sagi Grimberge371af02021-09-14 18:38:55 +0300602 data->data_offset = pdu->r2t_offset;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800603 data->data_length = cpu_to_le32(req->pdu_len);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800604}
605
606static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
607 struct nvme_tcp_r2t_pdu *pdu)
608{
609 struct nvme_tcp_request *req;
610 struct request *rq;
Varun Prakash1d3ef9c32021-11-23 16:28:56 +0530611 u32 r2t_length = le32_to_cpu(pdu->r2t_length);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800612
Sagi Grimberge7006de2021-06-16 14:19:36 -0700613 rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800614 if (!rq) {
615 dev_err(queue->ctrl->ctrl.device,
Sagi Grimberge7006de2021-06-16 14:19:36 -0700616 "got bad r2t.command_id %#x on queue %d\n",
617 pdu->command_id, nvme_tcp_queue_id(queue));
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800618 return -ENOENT;
619 }
620 req = blk_mq_rq_to_pdu(rq);
621
Varun Prakash1d3ef9c32021-11-23 16:28:56 +0530622 if (unlikely(!r2t_length)) {
623 dev_err(queue->ctrl->ctrl.device,
624 "req %d r2t len is %u, probably a bug...\n",
625 rq->tag, r2t_length);
626 return -EPROTO;
627 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800628
Varun Prakash1d3ef9c32021-11-23 16:28:56 +0530629 if (unlikely(req->data_sent + r2t_length > req->data_len)) {
630 dev_err(queue->ctrl->ctrl.device,
631 "req %d r2t len %u exceeded data len %u (%zu sent)\n",
632 rq->tag, r2t_length, req->data_len, req->data_sent);
633 return -EPROTO;
634 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800635
Varun Prakash1d3ef9c32021-11-23 16:28:56 +0530636 if (unlikely(le32_to_cpu(pdu->r2t_offset) < req->data_sent)) {
637 dev_err(queue->ctrl->ctrl.device,
638 "req %d unexpected r2t offset %u (expected %zu)\n",
639 rq->tag, le32_to_cpu(pdu->r2t_offset), req->data_sent);
640 return -EPROTO;
641 }
642
643 nvme_tcp_setup_h2c_data_pdu(req, pdu);
Sagi Grimberg86f03482020-06-18 17:30:23 -0700644 nvme_tcp_queue_request(req, false, true);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800645
646 return 0;
647}
648
649static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
650 unsigned int *offset, size_t *len)
651{
652 struct nvme_tcp_hdr *hdr;
653 char *pdu = queue->pdu;
654 size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
655 int ret;
656
657 ret = skb_copy_bits(skb, *offset,
658 &pdu[queue->pdu_offset], rcv_len);
659 if (unlikely(ret))
660 return ret;
661
662 queue->pdu_remaining -= rcv_len;
663 queue->pdu_offset += rcv_len;
664 *offset += rcv_len;
665 *len -= rcv_len;
666 if (queue->pdu_remaining)
667 return 0;
668
669 hdr = queue->pdu;
670 if (queue->hdr_digest) {
671 ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen);
672 if (unlikely(ret))
673 return ret;
674 }
675
676
677 if (queue->data_digest) {
678 ret = nvme_tcp_check_ddgst(queue, queue->pdu);
679 if (unlikely(ret))
680 return ret;
681 }
682
683 switch (hdr->type) {
684 case nvme_tcp_c2h_data:
Sagi Grimberg6be18262019-07-19 12:46:46 -0700685 return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800686 case nvme_tcp_rsp:
687 nvme_tcp_init_recv_ctx(queue);
Sagi Grimberg6be18262019-07-19 12:46:46 -0700688 return nvme_tcp_handle_comp(queue, (void *)queue->pdu);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800689 case nvme_tcp_r2t:
690 nvme_tcp_init_recv_ctx(queue);
Sagi Grimberg6be18262019-07-19 12:46:46 -0700691 return nvme_tcp_handle_r2t(queue, (void *)queue->pdu);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800692 default:
693 dev_err(queue->ctrl->ctrl.device,
694 "unsupported pdu type (%d)\n", hdr->type);
695 return -EINVAL;
696 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800697}
698
Christoph Hellwig988aef9e2019-03-15 08:41:04 +0100699static inline void nvme_tcp_end_request(struct request *rq, u16 status)
Sagi Grimberg602d6742019-03-13 18:55:10 +0100700{
701 union nvme_result res = {};
702
Christoph Hellwig2eb81a32020-08-18 09:11:29 +0200703 if (!nvme_try_complete_req(rq, cpu_to_le16(status << 1), res))
Christoph Hellwigff029452020-06-11 08:44:52 +0200704 nvme_complete_rq(rq);
Sagi Grimberg602d6742019-03-13 18:55:10 +0100705}
706
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800707static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb,
708 unsigned int *offset, size_t *len)
709{
710 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
Sagi Grimberg3b01a9d2021-06-16 14:19:35 -0700711 struct request *rq =
Sagi Grimberge7006de2021-06-16 14:19:36 -0700712 nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
Sagi Grimberg3b01a9d2021-06-16 14:19:35 -0700713 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800714
715 while (true) {
716 int recv_len, ret;
717
718 recv_len = min_t(size_t, *len, queue->data_remaining);
719 if (!recv_len)
720 break;
721
722 if (!iov_iter_count(&req->iter)) {
723 req->curr_bio = req->curr_bio->bi_next;
724
725 /*
726 * If we don`t have any bios it means that controller
727 * sent more data than we requested, hence error
728 */
729 if (!req->curr_bio) {
730 dev_err(queue->ctrl->ctrl.device,
731 "queue %d no space in request %#x",
732 nvme_tcp_queue_id(queue), rq->tag);
733 nvme_tcp_init_recv_ctx(queue);
734 return -EIO;
735 }
736 nvme_tcp_init_iter(req, READ);
737 }
738
739 /* we can read only from what is left in this bio */
740 recv_len = min_t(size_t, recv_len,
741 iov_iter_count(&req->iter));
742
743 if (queue->data_digest)
744 ret = skb_copy_and_hash_datagram_iter(skb, *offset,
745 &req->iter, recv_len, queue->rcv_hash);
746 else
747 ret = skb_copy_datagram_iter(skb, *offset,
748 &req->iter, recv_len);
749 if (ret) {
750 dev_err(queue->ctrl->ctrl.device,
751 "queue %d failed to copy request %#x data",
752 nvme_tcp_queue_id(queue), rq->tag);
753 return ret;
754 }
755
756 *len -= recv_len;
757 *offset += recv_len;
758 queue->data_remaining -= recv_len;
759 }
760
761 if (!queue->data_remaining) {
762 if (queue->data_digest) {
763 nvme_tcp_ddgst_final(queue->rcv_hash, &queue->exp_ddgst);
764 queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH;
765 } else {
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700766 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
Daniel Wagner1ba2e502021-08-30 15:36:26 +0200767 nvme_tcp_end_request(rq,
768 le16_to_cpu(req->status));
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700769 queue->nr_cqe++;
770 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800771 nvme_tcp_init_recv_ctx(queue);
772 }
773 }
774
775 return 0;
776}
777
778static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue,
779 struct sk_buff *skb, unsigned int *offset, size_t *len)
780{
Sagi Grimberg602d6742019-03-13 18:55:10 +0100781 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800782 char *ddgst = (char *)&queue->recv_ddgst;
783 size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining);
784 off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining;
785 int ret;
786
787 ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len);
788 if (unlikely(ret))
789 return ret;
790
791 queue->ddgst_remaining -= recv_len;
792 *offset += recv_len;
793 *len -= recv_len;
794 if (queue->ddgst_remaining)
795 return 0;
796
797 if (queue->recv_ddgst != queue->exp_ddgst) {
Daniel Wagner1ba2e502021-08-30 15:36:26 +0200798 struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
799 pdu->command_id);
800 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
801
802 req->status = cpu_to_le16(NVME_SC_DATA_XFER_ERROR);
803
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800804 dev_err(queue->ctrl->ctrl.device,
805 "data digest error: recv %#x expected %#x\n",
806 le32_to_cpu(queue->recv_ddgst),
807 le32_to_cpu(queue->exp_ddgst));
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800808 }
809
Sagi Grimberg602d6742019-03-13 18:55:10 +0100810 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
Sagi Grimberge7006de2021-06-16 14:19:36 -0700811 struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
812 pdu->command_id);
Daniel Wagner1ba2e502021-08-30 15:36:26 +0200813 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
Sagi Grimberg602d6742019-03-13 18:55:10 +0100814
Daniel Wagner1ba2e502021-08-30 15:36:26 +0200815 nvme_tcp_end_request(rq, le16_to_cpu(req->status));
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700816 queue->nr_cqe++;
Sagi Grimberg602d6742019-03-13 18:55:10 +0100817 }
818
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800819 nvme_tcp_init_recv_ctx(queue);
820 return 0;
821}
822
823static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,
824 unsigned int offset, size_t len)
825{
826 struct nvme_tcp_queue *queue = desc->arg.data;
827 size_t consumed = len;
828 int result;
829
830 while (len) {
831 switch (nvme_tcp_recv_state(queue)) {
832 case NVME_TCP_RECV_PDU:
833 result = nvme_tcp_recv_pdu(queue, skb, &offset, &len);
834 break;
835 case NVME_TCP_RECV_DATA:
836 result = nvme_tcp_recv_data(queue, skb, &offset, &len);
837 break;
838 case NVME_TCP_RECV_DDGST:
839 result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len);
840 break;
841 default:
842 result = -EFAULT;
843 }
844 if (result) {
845 dev_err(queue->ctrl->ctrl.device,
846 "receive failed: %d\n", result);
847 queue->rd_enabled = false;
848 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
849 return result;
850 }
851 }
852
853 return consumed;
854}
855
856static void nvme_tcp_data_ready(struct sock *sk)
857{
858 struct nvme_tcp_queue *queue;
859
Sagi Grimberg386e5e62020-04-30 13:59:32 -0700860 read_lock_bh(&sk->sk_callback_lock);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800861 queue = sk->sk_user_data;
Sagi Grimberg72e5d752020-05-01 14:25:44 -0700862 if (likely(queue && queue->rd_enabled) &&
863 !test_bit(NVME_TCP_Q_POLLING, &queue->flags))
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800864 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
Sagi Grimberg386e5e62020-04-30 13:59:32 -0700865 read_unlock_bh(&sk->sk_callback_lock);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800866}
867
868static void nvme_tcp_write_space(struct sock *sk)
869{
870 struct nvme_tcp_queue *queue;
871
872 read_lock_bh(&sk->sk_callback_lock);
873 queue = sk->sk_user_data;
874 if (likely(queue && sk_stream_is_writeable(sk))) {
875 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
876 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
877 }
878 read_unlock_bh(&sk->sk_callback_lock);
879}
880
881static void nvme_tcp_state_change(struct sock *sk)
882{
883 struct nvme_tcp_queue *queue;
884
Sagi Grimberg8b73b452021-03-21 00:08:48 -0700885 read_lock_bh(&sk->sk_callback_lock);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800886 queue = sk->sk_user_data;
887 if (!queue)
888 goto done;
889
890 switch (sk->sk_state) {
891 case TCP_CLOSE:
892 case TCP_CLOSE_WAIT:
893 case TCP_LAST_ACK:
894 case TCP_FIN_WAIT1:
895 case TCP_FIN_WAIT2:
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800896 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
897 break;
898 default:
899 dev_info(queue->ctrl->ctrl.device,
900 "queue %d socket state %d\n",
901 nvme_tcp_queue_id(queue), sk->sk_state);
902 }
903
904 queue->state_change(sk);
905done:
Sagi Grimberg8b73b452021-03-21 00:08:48 -0700906 read_unlock_bh(&sk->sk_callback_lock);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800907}
908
909static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue)
910{
911 queue->request = NULL;
912}
913
914static void nvme_tcp_fail_request(struct nvme_tcp_request *req)
915{
Sagi Grimberg16686012019-08-02 18:17:52 -0700916 nvme_tcp_end_request(blk_mq_rq_from_pdu(req), NVME_SC_HOST_PATH_ERROR);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800917}
918
919static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
920{
921 struct nvme_tcp_queue *queue = req->queue;
Sagi Grimberg25e1f672021-10-24 10:43:31 +0300922 int req_data_len = req->data_len;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800923
924 while (true) {
925 struct page *page = nvme_tcp_req_cur_page(req);
926 size_t offset = nvme_tcp_req_cur_offset(req);
927 size_t len = nvme_tcp_req_cur_length(req);
928 bool last = nvme_tcp_pdu_last_send(req, len);
Sagi Grimberg25e1f672021-10-24 10:43:31 +0300929 int req_data_sent = req->data_sent;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800930 int ret, flags = MSG_DONTWAIT;
931
Sagi Grimberg122e5b92020-06-18 17:30:24 -0700932 if (last && !queue->data_digest && !nvme_tcp_queue_more(queue))
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800933 flags |= MSG_EOR;
934 else
Sagi Grimberg5bb052d2020-05-04 22:20:01 -0700935 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800936
Coly Li7d4194a2020-10-02 16:27:30 +0800937 if (sendpage_ok(page)) {
938 ret = kernel_sendpage(queue->sock, page, offset, len,
Mikhail Skorzhinskii37c15212019-07-08 12:31:29 +0200939 flags);
940 } else {
Coly Li7d4194a2020-10-02 16:27:30 +0800941 ret = sock_no_sendpage(queue->sock, page, offset, len,
Mikhail Skorzhinskii37c15212019-07-08 12:31:29 +0200942 flags);
943 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800944 if (ret <= 0)
945 return ret;
946
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800947 if (queue->data_digest)
948 nvme_tcp_ddgst_update(queue->snd_hash, page,
949 offset, ret);
950
Sagi Grimberge371af02021-09-14 18:38:55 +0300951 /*
952 * update the request iterator except for the last payload send
953 * in the request where we don't want to modify it as we may
954 * compete with the RX path completing the request.
955 */
Sagi Grimberg25e1f672021-10-24 10:43:31 +0300956 if (req_data_sent + ret < req_data_len)
Sagi Grimberge371af02021-09-14 18:38:55 +0300957 nvme_tcp_advance_req(req, ret);
958
959 /* fully successful last send in current PDU */
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800960 if (last && ret == len) {
961 if (queue->data_digest) {
962 nvme_tcp_ddgst_final(queue->snd_hash,
963 &req->ddgst);
964 req->state = NVME_TCP_SEND_DDGST;
965 req->offset = 0;
966 } else {
967 nvme_tcp_done_send_req(queue);
968 }
969 return 1;
970 }
971 }
972 return -EAGAIN;
973}
974
975static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
976{
977 struct nvme_tcp_queue *queue = req->queue;
978 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
979 bool inline_data = nvme_tcp_has_inline_data(req);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800980 u8 hdgst = nvme_tcp_hdgst_len(queue);
981 int len = sizeof(*pdu) + hdgst - req->offset;
Sagi Grimberg5bb052d2020-05-04 22:20:01 -0700982 int flags = MSG_DONTWAIT;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800983 int ret;
984
Sagi Grimberg122e5b92020-06-18 17:30:24 -0700985 if (inline_data || nvme_tcp_queue_more(queue))
Sagi Grimberg5bb052d2020-05-04 22:20:01 -0700986 flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
987 else
988 flags |= MSG_EOR;
989
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800990 if (queue->hdr_digest && !req->offset)
991 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
992
993 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
994 offset_in_page(pdu) + req->offset, len, flags);
995 if (unlikely(ret <= 0))
996 return ret;
997
998 len -= ret;
999 if (!len) {
1000 if (inline_data) {
1001 req->state = NVME_TCP_SEND_DATA;
1002 if (queue->data_digest)
1003 crypto_ahash_init(queue->snd_hash);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001004 } else {
1005 nvme_tcp_done_send_req(queue);
1006 }
1007 return 1;
1008 }
1009 req->offset += ret;
1010
1011 return -EAGAIN;
1012}
1013
1014static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
1015{
1016 struct nvme_tcp_queue *queue = req->queue;
1017 struct nvme_tcp_data_pdu *pdu = req->pdu;
1018 u8 hdgst = nvme_tcp_hdgst_len(queue);
1019 int len = sizeof(*pdu) - req->offset + hdgst;
1020 int ret;
1021
1022 if (queue->hdr_digest && !req->offset)
1023 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
1024
1025 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
1026 offset_in_page(pdu) + req->offset, len,
Sagi Grimberg5bb052d2020-05-04 22:20:01 -07001027 MSG_DONTWAIT | MSG_MORE | MSG_SENDPAGE_NOTLAST);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001028 if (unlikely(ret <= 0))
1029 return ret;
1030
1031 len -= ret;
1032 if (!len) {
1033 req->state = NVME_TCP_SEND_DATA;
1034 if (queue->data_digest)
1035 crypto_ahash_init(queue->snd_hash);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001036 return 1;
1037 }
1038 req->offset += ret;
1039
1040 return -EAGAIN;
1041}
1042
1043static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
1044{
1045 struct nvme_tcp_queue *queue = req->queue;
Varun Prakashce7723e2021-10-26 19:01:55 +05301046 size_t offset = req->offset;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001047 int ret;
Sagi Grimberg122e5b92020-06-18 17:30:24 -07001048 struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001049 struct kvec iov = {
Varun Prakashd89b9f32021-10-25 22:47:30 +05301050 .iov_base = (u8 *)&req->ddgst + req->offset,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001051 .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset
1052 };
1053
Sagi Grimberg122e5b92020-06-18 17:30:24 -07001054 if (nvme_tcp_queue_more(queue))
1055 msg.msg_flags |= MSG_MORE;
1056 else
1057 msg.msg_flags |= MSG_EOR;
1058
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001059 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1060 if (unlikely(ret <= 0))
1061 return ret;
1062
Varun Prakashce7723e2021-10-26 19:01:55 +05301063 if (offset + ret == NVME_TCP_DIGEST_LENGTH) {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001064 nvme_tcp_done_send_req(queue);
1065 return 1;
1066 }
1067
1068 req->offset += ret;
1069 return -EAGAIN;
1070}
1071
1072static int nvme_tcp_try_send(struct nvme_tcp_queue *queue)
1073{
1074 struct nvme_tcp_request *req;
1075 int ret = 1;
1076
1077 if (!queue->request) {
1078 queue->request = nvme_tcp_fetch_request(queue);
1079 if (!queue->request)
1080 return 0;
1081 }
1082 req = queue->request;
1083
1084 if (req->state == NVME_TCP_SEND_CMD_PDU) {
1085 ret = nvme_tcp_try_send_cmd_pdu(req);
1086 if (ret <= 0)
1087 goto done;
1088 if (!nvme_tcp_has_inline_data(req))
1089 return ret;
1090 }
1091
1092 if (req->state == NVME_TCP_SEND_H2C_PDU) {
1093 ret = nvme_tcp_try_send_data_pdu(req);
1094 if (ret <= 0)
1095 goto done;
1096 }
1097
1098 if (req->state == NVME_TCP_SEND_DATA) {
1099 ret = nvme_tcp_try_send_data(req);
1100 if (ret <= 0)
1101 goto done;
1102 }
1103
1104 if (req->state == NVME_TCP_SEND_DDGST)
1105 ret = nvme_tcp_try_send_ddgst(req);
1106done:
Sagi Grimberg5ff4e112020-02-25 16:43:23 -08001107 if (ret == -EAGAIN) {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001108 ret = 0;
Sagi Grimberg5ff4e112020-02-25 16:43:23 -08001109 } else if (ret < 0) {
1110 dev_err(queue->ctrl->ctrl.device,
1111 "failed to send request %d\n", ret);
1112 if (ret != -EPIPE && ret != -ECONNRESET)
1113 nvme_tcp_fail_request(queue->request);
1114 nvme_tcp_done_send_req(queue);
1115 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001116 return ret;
1117}
1118
1119static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue)
1120{
Potnuri Bharat Teja10407ec2019-07-08 15:22:00 +05301121 struct socket *sock = queue->sock;
1122 struct sock *sk = sock->sk;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001123 read_descriptor_t rd_desc;
1124 int consumed;
1125
1126 rd_desc.arg.data = queue;
1127 rd_desc.count = 1;
1128 lock_sock(sk);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001129 queue->nr_cqe = 0;
Potnuri Bharat Teja10407ec2019-07-08 15:22:00 +05301130 consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001131 release_sock(sk);
1132 return consumed;
1133}
1134
1135static void nvme_tcp_io_work(struct work_struct *w)
1136{
1137 struct nvme_tcp_queue *queue =
1138 container_of(w, struct nvme_tcp_queue, io_work);
Wunderlich, Markddef2952019-09-18 23:36:37 +00001139 unsigned long deadline = jiffies + msecs_to_jiffies(1);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001140
1141 do {
1142 bool pending = false;
1143 int result;
1144
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -07001145 if (mutex_trylock(&queue->send_mutex)) {
1146 result = nvme_tcp_try_send(queue);
1147 mutex_unlock(&queue->send_mutex);
1148 if (result > 0)
1149 pending = true;
1150 else if (unlikely(result < 0))
1151 break;
Keith Busch70f437f2021-09-09 08:54:52 -07001152 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001153
1154 result = nvme_tcp_try_recv(queue);
1155 if (result > 0)
1156 pending = true;
Sagi Grimberg761ad262020-02-25 16:43:24 -08001157 else if (unlikely(result < 0))
Sagi Grimberg39d06079a2020-03-31 22:44:23 -07001158 return;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001159
1160 if (!pending)
1161 return;
1162
Wunderlich, Markddef2952019-09-18 23:36:37 +00001163 } while (!time_after(jiffies, deadline)); /* quota is exhausted */
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001164
1165 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
1166}
1167
1168static void nvme_tcp_free_crypto(struct nvme_tcp_queue *queue)
1169{
1170 struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
1171
1172 ahash_request_free(queue->rcv_hash);
1173 ahash_request_free(queue->snd_hash);
1174 crypto_free_ahash(tfm);
1175}
1176
1177static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue *queue)
1178{
1179 struct crypto_ahash *tfm;
1180
1181 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
1182 if (IS_ERR(tfm))
1183 return PTR_ERR(tfm);
1184
1185 queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1186 if (!queue->snd_hash)
1187 goto free_tfm;
1188 ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
1189
1190 queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1191 if (!queue->rcv_hash)
1192 goto free_snd_hash;
1193 ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
1194
1195 return 0;
1196free_snd_hash:
1197 ahash_request_free(queue->snd_hash);
1198free_tfm:
1199 crypto_free_ahash(tfm);
1200 return -ENOMEM;
1201}
1202
1203static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl)
1204{
1205 struct nvme_tcp_request *async = &ctrl->async_req;
1206
1207 page_frag_free(async->pdu);
1208}
1209
1210static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
1211{
1212 struct nvme_tcp_queue *queue = &ctrl->queues[0];
1213 struct nvme_tcp_request *async = &ctrl->async_req;
1214 u8 hdgst = nvme_tcp_hdgst_len(queue);
1215
1216 async->pdu = page_frag_alloc(&queue->pf_cache,
1217 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
1218 GFP_KERNEL | __GFP_ZERO);
1219 if (!async->pdu)
1220 return -ENOMEM;
1221
1222 async->queue = &ctrl->queues[0];
1223 return 0;
1224}
1225
1226static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
1227{
Maurizio Lombardia5053c92021-11-03 09:18:17 +01001228 struct page *page;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001229 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1230 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1231
1232 if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
1233 return;
1234
1235 if (queue->hdr_digest || queue->data_digest)
1236 nvme_tcp_free_crypto(queue);
1237
Maurizio Lombardia5053c92021-11-03 09:18:17 +01001238 if (queue->pf_cache.va) {
1239 page = virt_to_head_page(queue->pf_cache.va);
1240 __page_frag_cache_drain(page, queue->pf_cache.pagecnt_bias);
1241 queue->pf_cache.va = NULL;
1242 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001243 sock_release(queue->sock);
1244 kfree(queue->pdu);
Keith Buschd48f92c2021-08-06 08:41:43 -07001245 mutex_destroy(&queue->send_mutex);
Chao Leng9ebbfe42021-01-14 17:09:26 +08001246 mutex_destroy(&queue->queue_lock);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001247}
1248
1249static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
1250{
1251 struct nvme_tcp_icreq_pdu *icreq;
1252 struct nvme_tcp_icresp_pdu *icresp;
1253 struct msghdr msg = {};
1254 struct kvec iov;
1255 bool ctrl_hdgst, ctrl_ddgst;
1256 int ret;
1257
1258 icreq = kzalloc(sizeof(*icreq), GFP_KERNEL);
1259 if (!icreq)
1260 return -ENOMEM;
1261
1262 icresp = kzalloc(sizeof(*icresp), GFP_KERNEL);
1263 if (!icresp) {
1264 ret = -ENOMEM;
1265 goto free_icreq;
1266 }
1267
1268 icreq->hdr.type = nvme_tcp_icreq;
1269 icreq->hdr.hlen = sizeof(*icreq);
1270 icreq->hdr.pdo = 0;
1271 icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen);
1272 icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
1273 icreq->maxr2t = 0; /* single inflight r2t supported */
1274 icreq->hpda = 0; /* no alignment constraint */
1275 if (queue->hdr_digest)
1276 icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
1277 if (queue->data_digest)
1278 icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
1279
1280 iov.iov_base = icreq;
1281 iov.iov_len = sizeof(*icreq);
1282 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1283 if (ret < 0)
1284 goto free_icresp;
1285
1286 memset(&msg, 0, sizeof(msg));
1287 iov.iov_base = icresp;
1288 iov.iov_len = sizeof(*icresp);
1289 ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1290 iov.iov_len, msg.msg_flags);
1291 if (ret < 0)
1292 goto free_icresp;
1293
1294 ret = -EINVAL;
1295 if (icresp->hdr.type != nvme_tcp_icresp) {
1296 pr_err("queue %d: bad type returned %d\n",
1297 nvme_tcp_queue_id(queue), icresp->hdr.type);
1298 goto free_icresp;
1299 }
1300
1301 if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) {
1302 pr_err("queue %d: bad pdu length returned %d\n",
1303 nvme_tcp_queue_id(queue), icresp->hdr.plen);
1304 goto free_icresp;
1305 }
1306
1307 if (icresp->pfv != NVME_TCP_PFV_1_0) {
1308 pr_err("queue %d: bad pfv returned %d\n",
1309 nvme_tcp_queue_id(queue), icresp->pfv);
1310 goto free_icresp;
1311 }
1312
1313 ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE);
1314 if ((queue->data_digest && !ctrl_ddgst) ||
1315 (!queue->data_digest && ctrl_ddgst)) {
1316 pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
1317 nvme_tcp_queue_id(queue),
1318 queue->data_digest ? "enabled" : "disabled",
1319 ctrl_ddgst ? "enabled" : "disabled");
1320 goto free_icresp;
1321 }
1322
1323 ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE);
1324 if ((queue->hdr_digest && !ctrl_hdgst) ||
1325 (!queue->hdr_digest && ctrl_hdgst)) {
1326 pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
1327 nvme_tcp_queue_id(queue),
1328 queue->hdr_digest ? "enabled" : "disabled",
1329 ctrl_hdgst ? "enabled" : "disabled");
1330 goto free_icresp;
1331 }
1332
1333 if (icresp->cpda != 0) {
1334 pr_err("queue %d: unsupported cpda returned %d\n",
1335 nvme_tcp_queue_id(queue), icresp->cpda);
1336 goto free_icresp;
1337 }
1338
1339 ret = 0;
1340free_icresp:
1341 kfree(icresp);
1342free_icreq:
1343 kfree(icreq);
1344 return ret;
1345}
1346
Sagi Grimberg40510a62020-02-25 15:53:09 -08001347static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue)
1348{
1349 return nvme_tcp_queue_id(queue) == 0;
1350}
1351
1352static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue)
1353{
1354 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1355 int qid = nvme_tcp_queue_id(queue);
1356
1357 return !nvme_tcp_admin_queue(queue) &&
1358 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT];
1359}
1360
1361static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue)
1362{
1363 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1364 int qid = nvme_tcp_queue_id(queue);
1365
1366 return !nvme_tcp_admin_queue(queue) &&
1367 !nvme_tcp_default_queue(queue) &&
1368 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1369 ctrl->io_queues[HCTX_TYPE_READ];
1370}
1371
1372static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue)
1373{
1374 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1375 int qid = nvme_tcp_queue_id(queue);
1376
1377 return !nvme_tcp_admin_queue(queue) &&
1378 !nvme_tcp_default_queue(queue) &&
1379 !nvme_tcp_read_queue(queue) &&
1380 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1381 ctrl->io_queues[HCTX_TYPE_READ] +
1382 ctrl->io_queues[HCTX_TYPE_POLL];
1383}
1384
1385static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue)
1386{
1387 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1388 int qid = nvme_tcp_queue_id(queue);
1389 int n = 0;
1390
1391 if (nvme_tcp_default_queue(queue))
1392 n = qid - 1;
1393 else if (nvme_tcp_read_queue(queue))
1394 n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] - 1;
1395 else if (nvme_tcp_poll_queue(queue))
1396 n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] -
1397 ctrl->io_queues[HCTX_TYPE_READ] - 1;
1398 queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
1399}
1400
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001401static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl,
1402 int qid, size_t queue_size)
1403{
1404 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1405 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +02001406 int ret, rcv_pdu_size;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001407
Chao Leng9ebbfe42021-01-14 17:09:26 +08001408 mutex_init(&queue->queue_lock);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001409 queue->ctrl = ctrl;
Sagi Grimberg15ec9282020-06-18 17:30:22 -07001410 init_llist_head(&queue->req_list);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001411 INIT_LIST_HEAD(&queue->send_list);
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -07001412 mutex_init(&queue->send_mutex);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001413 INIT_WORK(&queue->io_work, nvme_tcp_io_work);
1414 queue->queue_size = queue_size;
1415
1416 if (qid > 0)
Israel Rukshin9924b032019-08-18 12:08:53 +03001417 queue->cmnd_capsule_len = nctrl->ioccsz * 16;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001418 else
1419 queue->cmnd_capsule_len = sizeof(struct nvme_command) +
1420 NVME_TCP_ADMIN_CCSZ;
1421
1422 ret = sock_create(ctrl->addr.ss_family, SOCK_STREAM,
1423 IPPROTO_TCP, &queue->sock);
1424 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001425 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001426 "failed to create socket: %d\n", ret);
Chao Leng9ebbfe42021-01-14 17:09:26 +08001427 goto err_destroy_mutex;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001428 }
1429
1430 /* Single syn retry */
Christoph Hellwig557eadf2020-05-28 07:12:21 +02001431 tcp_sock_set_syncnt(queue->sock->sk, 1);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001432
1433 /* Set TCP no delay */
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02001434 tcp_sock_set_nodelay(queue->sock->sk);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001435
1436 /*
1437 * Cleanup whatever is sitting in the TCP transmit queue on socket
1438 * close. This is done to prevent stale data from being sent should
1439 * the network connection be restored before TCP times out.
1440 */
Christoph Hellwigc4335942020-05-28 07:12:10 +02001441 sock_no_linger(queue->sock->sk);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001442
Christoph Hellwig6e434962020-05-28 07:12:11 +02001443 if (so_priority > 0)
1444 sock_set_priority(queue->sock->sk, so_priority);
Wunderlich, Mark9912ade2020-01-16 00:46:12 +00001445
Israel Rukshinbb139852019-08-18 12:08:54 +03001446 /* Set socket type of service */
Christoph Hellwig6ebf71b2020-05-28 07:12:26 +02001447 if (nctrl->opts->tos >= 0)
1448 ip_sock_set_tos(queue->sock->sk, nctrl->opts->tos);
Israel Rukshinbb139852019-08-18 12:08:54 +03001449
Sagi Grimbergadc99fd2020-07-23 16:42:26 -07001450 /* Set 10 seconds timeout for icresp recvmsg */
1451 queue->sock->sk->sk_rcvtimeo = 10 * HZ;
1452
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001453 queue->sock->sk->sk_allocation = GFP_ATOMIC;
Sagi Grimberg40510a62020-02-25 15:53:09 -08001454 nvme_tcp_set_queue_io_cpu(queue);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001455 queue->request = NULL;
1456 queue->data_remaining = 0;
1457 queue->ddgst_remaining = 0;
1458 queue->pdu_remaining = 0;
1459 queue->pdu_offset = 0;
1460 sk_set_memalloc(queue->sock->sk);
1461
Israel Rukshin9924b032019-08-18 12:08:53 +03001462 if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001463 ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr,
1464 sizeof(ctrl->src_addr));
1465 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001466 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001467 "failed to bind queue %d socket %d\n",
1468 qid, ret);
1469 goto err_sock;
1470 }
1471 }
1472
Martin Belanger3ede8f72021-05-20 15:09:34 -04001473 if (nctrl->opts->mask & NVMF_OPT_HOST_IFACE) {
1474 char *iface = nctrl->opts->host_iface;
1475 sockptr_t optval = KERNEL_SOCKPTR(iface);
1476
1477 ret = sock_setsockopt(queue->sock, SOL_SOCKET, SO_BINDTODEVICE,
1478 optval, strlen(iface));
1479 if (ret) {
1480 dev_err(nctrl->device,
1481 "failed to bind to interface %s queue %d err %d\n",
1482 iface, qid, ret);
1483 goto err_sock;
1484 }
1485 }
1486
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001487 queue->hdr_digest = nctrl->opts->hdr_digest;
1488 queue->data_digest = nctrl->opts->data_digest;
1489 if (queue->hdr_digest || queue->data_digest) {
1490 ret = nvme_tcp_alloc_crypto(queue);
1491 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001492 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001493 "failed to allocate queue %d crypto\n", qid);
1494 goto err_sock;
1495 }
1496 }
1497
1498 rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) +
1499 nvme_tcp_hdgst_len(queue);
1500 queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL);
1501 if (!queue->pdu) {
1502 ret = -ENOMEM;
1503 goto err_crypto;
1504 }
1505
Israel Rukshin9924b032019-08-18 12:08:53 +03001506 dev_dbg(nctrl->device, "connecting queue %d\n",
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001507 nvme_tcp_queue_id(queue));
1508
1509 ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr,
1510 sizeof(ctrl->addr), 0);
1511 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001512 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001513 "failed to connect socket: %d\n", ret);
1514 goto err_rcv_pdu;
1515 }
1516
1517 ret = nvme_tcp_init_connection(queue);
1518 if (ret)
1519 goto err_init_connect;
1520
1521 queue->rd_enabled = true;
1522 set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags);
1523 nvme_tcp_init_recv_ctx(queue);
1524
1525 write_lock_bh(&queue->sock->sk->sk_callback_lock);
1526 queue->sock->sk->sk_user_data = queue;
1527 queue->state_change = queue->sock->sk->sk_state_change;
1528 queue->data_ready = queue->sock->sk->sk_data_ready;
1529 queue->write_space = queue->sock->sk->sk_write_space;
1530 queue->sock->sk->sk_data_ready = nvme_tcp_data_ready;
1531 queue->sock->sk->sk_state_change = nvme_tcp_state_change;
1532 queue->sock->sk->sk_write_space = nvme_tcp_write_space;
Sebastian Andrzej Siewiorac1c4e12019-10-10 17:34:12 +02001533#ifdef CONFIG_NET_RX_BUSY_POLL
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001534 queue->sock->sk->sk_ll_usec = 1;
Sebastian Andrzej Siewiorac1c4e12019-10-10 17:34:12 +02001535#endif
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001536 write_unlock_bh(&queue->sock->sk->sk_callback_lock);
1537
1538 return 0;
1539
1540err_init_connect:
1541 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1542err_rcv_pdu:
1543 kfree(queue->pdu);
1544err_crypto:
1545 if (queue->hdr_digest || queue->data_digest)
1546 nvme_tcp_free_crypto(queue);
1547err_sock:
1548 sock_release(queue->sock);
1549 queue->sock = NULL;
Chao Leng9ebbfe42021-01-14 17:09:26 +08001550err_destroy_mutex:
Keith Buschd48f92c2021-08-06 08:41:43 -07001551 mutex_destroy(&queue->send_mutex);
Chao Leng9ebbfe42021-01-14 17:09:26 +08001552 mutex_destroy(&queue->queue_lock);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001553 return ret;
1554}
1555
1556static void nvme_tcp_restore_sock_calls(struct nvme_tcp_queue *queue)
1557{
1558 struct socket *sock = queue->sock;
1559
1560 write_lock_bh(&sock->sk->sk_callback_lock);
1561 sock->sk->sk_user_data = NULL;
1562 sock->sk->sk_data_ready = queue->data_ready;
1563 sock->sk->sk_state_change = queue->state_change;
1564 sock->sk->sk_write_space = queue->write_space;
1565 write_unlock_bh(&sock->sk->sk_callback_lock);
1566}
1567
1568static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
1569{
1570 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1571 nvme_tcp_restore_sock_calls(queue);
1572 cancel_work_sync(&queue->io_work);
1573}
1574
1575static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
1576{
1577 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1578 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1579
Chao Leng9ebbfe42021-01-14 17:09:26 +08001580 mutex_lock(&queue->queue_lock);
1581 if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
1582 __nvme_tcp_stop_queue(queue);
1583 mutex_unlock(&queue->queue_lock);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001584}
1585
1586static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
1587{
1588 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1589 int ret;
1590
1591 if (idx)
Keith Buschbe42a332021-06-10 14:44:35 -07001592 ret = nvmf_connect_io_queue(nctrl, idx);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001593 else
1594 ret = nvmf_connect_admin_queue(nctrl);
1595
1596 if (!ret) {
1597 set_bit(NVME_TCP_Q_LIVE, &ctrl->queues[idx].flags);
1598 } else {
Sagi Grimbergf34e2582019-04-29 16:25:48 -07001599 if (test_bit(NVME_TCP_Q_ALLOCATED, &ctrl->queues[idx].flags))
1600 __nvme_tcp_stop_queue(&ctrl->queues[idx]);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001601 dev_err(nctrl->device,
1602 "failed to connect queue: %d ret=%d\n", idx, ret);
1603 }
1604 return ret;
1605}
1606
1607static struct blk_mq_tag_set *nvme_tcp_alloc_tagset(struct nvme_ctrl *nctrl,
1608 bool admin)
1609{
1610 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1611 struct blk_mq_tag_set *set;
1612 int ret;
1613
1614 if (admin) {
1615 set = &ctrl->admin_tag_set;
1616 memset(set, 0, sizeof(*set));
1617 set->ops = &nvme_tcp_admin_mq_ops;
1618 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
Christoph Hellwiged01fee2021-03-03 13:28:22 +01001619 set->reserved_tags = NVMF_RESERVED_TAGS;
Max Gurtovoy610c8232020-06-16 12:34:24 +03001620 set->numa_node = nctrl->numa_node;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -07001621 set->flags = BLK_MQ_F_BLOCKING;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001622 set->cmd_size = sizeof(struct nvme_tcp_request);
1623 set->driver_data = ctrl;
1624 set->nr_hw_queues = 1;
Chaitanya Kulkarnidc96f932020-11-09 16:33:45 -08001625 set->timeout = NVME_ADMIN_TIMEOUT;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001626 } else {
1627 set = &ctrl->tag_set;
1628 memset(set, 0, sizeof(*set));
1629 set->ops = &nvme_tcp_mq_ops;
1630 set->queue_depth = nctrl->sqsize + 1;
Christoph Hellwiged01fee2021-03-03 13:28:22 +01001631 set->reserved_tags = NVMF_RESERVED_TAGS;
Max Gurtovoy610c8232020-06-16 12:34:24 +03001632 set->numa_node = nctrl->numa_node;
Sagi Grimbergdb5ad6b2020-05-01 14:25:45 -07001633 set->flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001634 set->cmd_size = sizeof(struct nvme_tcp_request);
1635 set->driver_data = ctrl;
1636 set->nr_hw_queues = nctrl->queue_count - 1;
1637 set->timeout = NVME_IO_TIMEOUT;
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001638 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001639 }
1640
1641 ret = blk_mq_alloc_tag_set(set);
1642 if (ret)
1643 return ERR_PTR(ret);
1644
1645 return set;
1646}
1647
1648static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl)
1649{
1650 if (to_tcp_ctrl(ctrl)->async_req.pdu) {
David Milburnceb1e082020-09-02 17:42:53 -05001651 cancel_work_sync(&ctrl->async_event_work);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001652 nvme_tcp_free_async_req(to_tcp_ctrl(ctrl));
1653 to_tcp_ctrl(ctrl)->async_req.pdu = NULL;
1654 }
1655
1656 nvme_tcp_free_queue(ctrl, 0);
1657}
1658
1659static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl)
1660{
1661 int i;
1662
1663 for (i = 1; i < ctrl->queue_count; i++)
1664 nvme_tcp_free_queue(ctrl, i);
1665}
1666
1667static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl)
1668{
1669 int i;
1670
1671 for (i = 1; i < ctrl->queue_count; i++)
1672 nvme_tcp_stop_queue(ctrl, i);
1673}
1674
1675static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl)
1676{
1677 int i, ret = 0;
1678
1679 for (i = 1; i < ctrl->queue_count; i++) {
1680 ret = nvme_tcp_start_queue(ctrl, i);
1681 if (ret)
1682 goto out_stop_queues;
1683 }
1684
1685 return 0;
1686
1687out_stop_queues:
1688 for (i--; i >= 1; i--)
1689 nvme_tcp_stop_queue(ctrl, i);
1690 return ret;
1691}
1692
1693static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
1694{
1695 int ret;
1696
1697 ret = nvme_tcp_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
1698 if (ret)
1699 return ret;
1700
1701 ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
1702 if (ret)
1703 goto out_free_queue;
1704
1705 return 0;
1706
1707out_free_queue:
1708 nvme_tcp_free_queue(ctrl, 0);
1709 return ret;
1710}
1711
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001712static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001713{
1714 int i, ret;
1715
1716 for (i = 1; i < ctrl->queue_count; i++) {
1717 ret = nvme_tcp_alloc_queue(ctrl, i,
1718 ctrl->sqsize + 1);
1719 if (ret)
1720 goto out_free_queues;
1721 }
1722
1723 return 0;
1724
1725out_free_queues:
1726 for (i--; i >= 1; i--)
1727 nvme_tcp_free_queue(ctrl, i);
1728
1729 return ret;
1730}
1731
1732static unsigned int nvme_tcp_nr_io_queues(struct nvme_ctrl *ctrl)
1733{
Sagi Grimberg873946f2018-12-11 23:38:57 -08001734 unsigned int nr_io_queues;
1735
1736 nr_io_queues = min(ctrl->opts->nr_io_queues, num_online_cpus());
1737 nr_io_queues += min(ctrl->opts->nr_write_queues, num_online_cpus());
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001738 nr_io_queues += min(ctrl->opts->nr_poll_queues, num_online_cpus());
Sagi Grimberg873946f2018-12-11 23:38:57 -08001739
1740 return nr_io_queues;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001741}
1742
Sagi Grimberg64861992019-05-28 22:49:05 -07001743static void nvme_tcp_set_io_queues(struct nvme_ctrl *nctrl,
1744 unsigned int nr_io_queues)
1745{
1746 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1747 struct nvmf_ctrl_options *opts = nctrl->opts;
1748
1749 if (opts->nr_write_queues && opts->nr_io_queues < nr_io_queues) {
1750 /*
1751 * separate read/write queues
1752 * hand out dedicated default queues only after we have
1753 * sufficient read queues.
1754 */
1755 ctrl->io_queues[HCTX_TYPE_READ] = opts->nr_io_queues;
1756 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
1757 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1758 min(opts->nr_write_queues, nr_io_queues);
1759 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1760 } else {
1761 /*
1762 * shared read/write queues
1763 * either no write queues were requested, or we don't have
1764 * sufficient queue count to have dedicated default queues.
1765 */
1766 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1767 min(opts->nr_io_queues, nr_io_queues);
1768 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1769 }
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001770
1771 if (opts->nr_poll_queues && nr_io_queues) {
1772 /* map dedicated poll queues only if we have queues left */
1773 ctrl->io_queues[HCTX_TYPE_POLL] =
1774 min(opts->nr_poll_queues, nr_io_queues);
1775 }
Sagi Grimberg64861992019-05-28 22:49:05 -07001776}
1777
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001778static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001779{
1780 unsigned int nr_io_queues;
1781 int ret;
1782
1783 nr_io_queues = nvme_tcp_nr_io_queues(ctrl);
1784 ret = nvme_set_queue_count(ctrl, &nr_io_queues);
1785 if (ret)
1786 return ret;
1787
Ruozhu Li664227f2021-08-07 11:50:23 +08001788 if (nr_io_queues == 0) {
Sagi Grimberg72f57242021-03-15 14:04:26 -07001789 dev_err(ctrl->device,
1790 "unable to set any I/O queues\n");
1791 return -ENOMEM;
1792 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001793
Ruozhu Li664227f2021-08-07 11:50:23 +08001794 ctrl->queue_count = nr_io_queues + 1;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001795 dev_info(ctrl->device,
1796 "creating %d I/O queues.\n", nr_io_queues);
1797
Sagi Grimberg64861992019-05-28 22:49:05 -07001798 nvme_tcp_set_io_queues(ctrl, nr_io_queues);
1799
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001800 return __nvme_tcp_alloc_io_queues(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001801}
1802
1803static void nvme_tcp_destroy_io_queues(struct nvme_ctrl *ctrl, bool remove)
1804{
1805 nvme_tcp_stop_io_queues(ctrl);
1806 if (remove) {
Sagi Grimberge85037a2018-12-31 23:58:30 -08001807 blk_cleanup_queue(ctrl->connect_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001808 blk_mq_free_tag_set(ctrl->tagset);
1809 }
1810 nvme_tcp_free_io_queues(ctrl);
1811}
1812
1813static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
1814{
1815 int ret;
1816
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001817 ret = nvme_tcp_alloc_io_queues(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001818 if (ret)
1819 return ret;
1820
1821 if (new) {
1822 ctrl->tagset = nvme_tcp_alloc_tagset(ctrl, false);
1823 if (IS_ERR(ctrl->tagset)) {
1824 ret = PTR_ERR(ctrl->tagset);
1825 goto out_free_io_queues;
1826 }
1827
Sagi Grimberge85037a2018-12-31 23:58:30 -08001828 ctrl->connect_q = blk_mq_init_queue(ctrl->tagset);
1829 if (IS_ERR(ctrl->connect_q)) {
1830 ret = PTR_ERR(ctrl->connect_q);
1831 goto out_free_tag_set;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001832 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001833 }
1834
1835 ret = nvme_tcp_start_io_queues(ctrl);
1836 if (ret)
1837 goto out_cleanup_connect_q;
1838
Sagi Grimberg2875b0a2020-07-24 15:10:12 -07001839 if (!new) {
1840 nvme_start_queues(ctrl);
Sagi Grimberge5c01f42020-07-30 13:25:34 -07001841 if (!nvme_wait_freeze_timeout(ctrl, NVME_IO_TIMEOUT)) {
1842 /*
1843 * If we timed out waiting for freeze we are likely to
1844 * be stuck. Fail the controller initialization just
1845 * to be safe.
1846 */
1847 ret = -ENODEV;
1848 goto out_wait_freeze_timed_out;
1849 }
Sagi Grimberg2875b0a2020-07-24 15:10:12 -07001850 blk_mq_update_nr_hw_queues(ctrl->tagset,
1851 ctrl->queue_count - 1);
1852 nvme_unfreeze(ctrl);
1853 }
1854
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001855 return 0;
1856
Sagi Grimberge5c01f42020-07-30 13:25:34 -07001857out_wait_freeze_timed_out:
1858 nvme_stop_queues(ctrl);
Chao Leng70a99572021-01-21 11:32:38 +08001859 nvme_sync_io_queues(ctrl);
Sagi Grimberge5c01f42020-07-30 13:25:34 -07001860 nvme_tcp_stop_io_queues(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001861out_cleanup_connect_q:
Chao Leng70a99572021-01-21 11:32:38 +08001862 nvme_cancel_tagset(ctrl);
Sagi Grimberge85037a2018-12-31 23:58:30 -08001863 if (new)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001864 blk_cleanup_queue(ctrl->connect_q);
1865out_free_tag_set:
1866 if (new)
1867 blk_mq_free_tag_set(ctrl->tagset);
1868out_free_io_queues:
1869 nvme_tcp_free_io_queues(ctrl);
1870 return ret;
1871}
1872
1873static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl *ctrl, bool remove)
1874{
1875 nvme_tcp_stop_queue(ctrl, 0);
1876 if (remove) {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001877 blk_cleanup_queue(ctrl->admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001878 blk_cleanup_queue(ctrl->fabrics_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001879 blk_mq_free_tag_set(ctrl->admin_tagset);
1880 }
1881 nvme_tcp_free_admin_queue(ctrl);
1882}
1883
1884static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
1885{
1886 int error;
1887
1888 error = nvme_tcp_alloc_admin_queue(ctrl);
1889 if (error)
1890 return error;
1891
1892 if (new) {
1893 ctrl->admin_tagset = nvme_tcp_alloc_tagset(ctrl, true);
1894 if (IS_ERR(ctrl->admin_tagset)) {
1895 error = PTR_ERR(ctrl->admin_tagset);
1896 goto out_free_queue;
1897 }
1898
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001899 ctrl->fabrics_q = blk_mq_init_queue(ctrl->admin_tagset);
1900 if (IS_ERR(ctrl->fabrics_q)) {
1901 error = PTR_ERR(ctrl->fabrics_q);
1902 goto out_free_tagset;
1903 }
1904
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001905 ctrl->admin_q = blk_mq_init_queue(ctrl->admin_tagset);
1906 if (IS_ERR(ctrl->admin_q)) {
1907 error = PTR_ERR(ctrl->admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001908 goto out_cleanup_fabrics_q;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001909 }
1910 }
1911
1912 error = nvme_tcp_start_queue(ctrl, 0);
1913 if (error)
1914 goto out_cleanup_queue;
1915
Sagi Grimbergc0f2f452019-07-22 17:06:53 -07001916 error = nvme_enable_ctrl(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001917 if (error)
1918 goto out_stop_queue;
1919
Ming Lei6ca1d902021-10-14 16:17:06 +08001920 nvme_start_admin_queue(ctrl);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001921
Chaitanya Kulkarnif21c47692021-02-28 18:06:04 -08001922 error = nvme_init_ctrl_finish(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001923 if (error)
Chao Leng70a99572021-01-21 11:32:38 +08001924 goto out_quiesce_queue;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001925
1926 return 0;
1927
Chao Leng70a99572021-01-21 11:32:38 +08001928out_quiesce_queue:
Ming Lei6ca1d902021-10-14 16:17:06 +08001929 nvme_stop_admin_queue(ctrl);
Chao Leng70a99572021-01-21 11:32:38 +08001930 blk_sync_queue(ctrl->admin_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001931out_stop_queue:
1932 nvme_tcp_stop_queue(ctrl, 0);
Chao Leng70a99572021-01-21 11:32:38 +08001933 nvme_cancel_admin_tagset(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001934out_cleanup_queue:
1935 if (new)
1936 blk_cleanup_queue(ctrl->admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001937out_cleanup_fabrics_q:
1938 if (new)
1939 blk_cleanup_queue(ctrl->fabrics_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001940out_free_tagset:
1941 if (new)
1942 blk_mq_free_tag_set(ctrl->admin_tagset);
1943out_free_queue:
1944 nvme_tcp_free_admin_queue(ctrl);
1945 return error;
1946}
1947
1948static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
1949 bool remove)
1950{
Ming Lei6ca1d902021-10-14 16:17:06 +08001951 nvme_stop_admin_queue(ctrl);
Chao Lengd6f66212020-10-22 10:15:15 +08001952 blk_sync_queue(ctrl->admin_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001953 nvme_tcp_stop_queue(ctrl, 0);
Chao Leng563c8152021-01-21 11:32:40 +08001954 nvme_cancel_admin_tagset(ctrl);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001955 if (remove)
Ming Lei6ca1d902021-10-14 16:17:06 +08001956 nvme_start_admin_queue(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001957 nvme_tcp_destroy_admin_queue(ctrl, remove);
1958}
1959
1960static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
1961 bool remove)
1962{
1963 if (ctrl->queue_count <= 1)
Chao Lengd6f66212020-10-22 10:15:15 +08001964 return;
Ming Lei6ca1d902021-10-14 16:17:06 +08001965 nvme_stop_admin_queue(ctrl);
Sagi Grimberg2875b0a2020-07-24 15:10:12 -07001966 nvme_start_freeze(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001967 nvme_stop_queues(ctrl);
Chao Lengd6f66212020-10-22 10:15:15 +08001968 nvme_sync_io_queues(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001969 nvme_tcp_stop_io_queues(ctrl);
Chao Leng563c8152021-01-21 11:32:40 +08001970 nvme_cancel_tagset(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001971 if (remove)
1972 nvme_start_queues(ctrl);
1973 nvme_tcp_destroy_io_queues(ctrl, remove);
1974}
1975
1976static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl)
1977{
1978 /* If we are resetting/deleting then do nothing */
1979 if (ctrl->state != NVME_CTRL_CONNECTING) {
1980 WARN_ON_ONCE(ctrl->state == NVME_CTRL_NEW ||
1981 ctrl->state == NVME_CTRL_LIVE);
1982 return;
1983 }
1984
1985 if (nvmf_should_reconnect(ctrl)) {
1986 dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
1987 ctrl->opts->reconnect_delay);
1988 queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
1989 ctrl->opts->reconnect_delay * HZ);
1990 } else {
1991 dev_info(ctrl->device, "Removing controller...\n");
1992 nvme_delete_ctrl(ctrl);
1993 }
1994}
1995
1996static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
1997{
1998 struct nvmf_ctrl_options *opts = ctrl->opts;
Colin Ian King312910f2019-09-05 15:34:35 +01001999 int ret;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002000
2001 ret = nvme_tcp_configure_admin_queue(ctrl, new);
2002 if (ret)
2003 return ret;
2004
2005 if (ctrl->icdoff) {
Dan Carpenter522af602021-06-05 15:48:16 +03002006 ret = -EOPNOTSUPP;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002007 dev_err(ctrl->device, "icdoff is not supported!\n");
2008 goto destroy_admin;
2009 }
2010
Chaitanya Kulkarni3b540642021-06-09 18:28:26 -07002011 if (!nvme_ctrl_sgl_supported(ctrl)) {
Dan Carpenter522af602021-06-05 15:48:16 +03002012 ret = -EOPNOTSUPP;
Max Gurtovoy73ffcef2021-03-30 23:01:19 +00002013 dev_err(ctrl->device, "Mandatory sgls are not supported!\n");
2014 goto destroy_admin;
2015 }
2016
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002017 if (opts->queue_size > ctrl->sqsize + 1)
2018 dev_warn(ctrl->device,
2019 "queue_size %zu > ctrl sqsize %u, clamping down\n",
2020 opts->queue_size, ctrl->sqsize + 1);
2021
2022 if (ctrl->sqsize + 1 > ctrl->maxcmd) {
2023 dev_warn(ctrl->device,
2024 "sqsize %u > ctrl maxcmd %u, clamping down\n",
2025 ctrl->sqsize + 1, ctrl->maxcmd);
2026 ctrl->sqsize = ctrl->maxcmd - 1;
2027 }
2028
2029 if (ctrl->queue_count > 1) {
2030 ret = nvme_tcp_configure_io_queues(ctrl, new);
2031 if (ret)
2032 goto destroy_admin;
2033 }
2034
2035 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
Israel Rukshinbea54ef2020-03-24 17:29:45 +02002036 /*
Sagi Grimbergecca390e2020-07-22 16:32:19 -07002037 * state change failure is ok if we started ctrl delete,
Israel Rukshinbea54ef2020-03-24 17:29:45 +02002038 * unless we're during creation of a new controller to
2039 * avoid races with teardown flow.
2040 */
Sagi Grimbergecca390e2020-07-22 16:32:19 -07002041 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2042 ctrl->state != NVME_CTRL_DELETING_NOIO);
Israel Rukshinbea54ef2020-03-24 17:29:45 +02002043 WARN_ON_ONCE(new);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002044 ret = -EINVAL;
2045 goto destroy_io;
2046 }
2047
2048 nvme_start_ctrl(ctrl);
2049 return 0;
2050
2051destroy_io:
Chao Leng70a99572021-01-21 11:32:38 +08002052 if (ctrl->queue_count > 1) {
2053 nvme_stop_queues(ctrl);
2054 nvme_sync_io_queues(ctrl);
2055 nvme_tcp_stop_io_queues(ctrl);
2056 nvme_cancel_tagset(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002057 nvme_tcp_destroy_io_queues(ctrl, new);
Chao Leng70a99572021-01-21 11:32:38 +08002058 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002059destroy_admin:
Ming Lei6ca1d902021-10-14 16:17:06 +08002060 nvme_stop_admin_queue(ctrl);
Chao Leng70a99572021-01-21 11:32:38 +08002061 blk_sync_queue(ctrl->admin_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002062 nvme_tcp_stop_queue(ctrl, 0);
Chao Leng70a99572021-01-21 11:32:38 +08002063 nvme_cancel_admin_tagset(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002064 nvme_tcp_destroy_admin_queue(ctrl, new);
2065 return ret;
2066}
2067
2068static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
2069{
2070 struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
2071 struct nvme_tcp_ctrl, connect_work);
2072 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2073
2074 ++ctrl->nr_reconnects;
2075
2076 if (nvme_tcp_setup_ctrl(ctrl, false))
2077 goto requeue;
2078
Colin Ian King56a77d22018-12-14 11:42:43 +00002079 dev_info(ctrl->device, "Successfully reconnected (%d attempt)\n",
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002080 ctrl->nr_reconnects);
2081
2082 ctrl->nr_reconnects = 0;
2083
2084 return;
2085
2086requeue:
2087 dev_info(ctrl->device, "Failed reconnect attempt %d\n",
2088 ctrl->nr_reconnects);
2089 nvme_tcp_reconnect_or_remove(ctrl);
2090}
2091
2092static void nvme_tcp_error_recovery_work(struct work_struct *work)
2093{
2094 struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
2095 struct nvme_tcp_ctrl, err_work);
2096 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2097
2098 nvme_stop_keep_alive(ctrl);
2099 nvme_tcp_teardown_io_queues(ctrl, false);
2100 /* unquiesce to fail fast pending requests */
2101 nvme_start_queues(ctrl);
2102 nvme_tcp_teardown_admin_queue(ctrl, false);
Ming Lei6ca1d902021-10-14 16:17:06 +08002103 nvme_start_admin_queue(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002104
2105 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
Sagi Grimbergecca390e2020-07-22 16:32:19 -07002106 /* state change failure is ok if we started ctrl delete */
2107 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2108 ctrl->state != NVME_CTRL_DELETING_NOIO);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002109 return;
2110 }
2111
2112 nvme_tcp_reconnect_or_remove(ctrl);
2113}
2114
2115static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
2116{
Sagi Grimberg794a4cb2019-01-01 00:19:30 -08002117 cancel_work_sync(&to_tcp_ctrl(ctrl)->err_work);
2118 cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
2119
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002120 nvme_tcp_teardown_io_queues(ctrl, shutdown);
Ming Lei6ca1d902021-10-14 16:17:06 +08002121 nvme_stop_admin_queue(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002122 if (shutdown)
2123 nvme_shutdown_ctrl(ctrl);
2124 else
Sagi Grimbergb5b05042019-07-22 17:06:54 -07002125 nvme_disable_ctrl(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002126 nvme_tcp_teardown_admin_queue(ctrl, shutdown);
2127}
2128
2129static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
2130{
2131 nvme_tcp_teardown_ctrl(ctrl, true);
2132}
2133
2134static void nvme_reset_ctrl_work(struct work_struct *work)
2135{
2136 struct nvme_ctrl *ctrl =
2137 container_of(work, struct nvme_ctrl, reset_work);
2138
2139 nvme_stop_ctrl(ctrl);
2140 nvme_tcp_teardown_ctrl(ctrl, false);
2141
2142 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
Sagi Grimbergecca390e2020-07-22 16:32:19 -07002143 /* state change failure is ok if we started ctrl delete */
2144 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2145 ctrl->state != NVME_CTRL_DELETING_NOIO);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002146 return;
2147 }
2148
2149 if (nvme_tcp_setup_ctrl(ctrl, false))
2150 goto out_fail;
2151
2152 return;
2153
2154out_fail:
2155 ++ctrl->nr_reconnects;
2156 nvme_tcp_reconnect_or_remove(ctrl);
2157}
2158
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002159static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
2160{
2161 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
2162
2163 if (list_empty(&ctrl->list))
2164 goto free_ctrl;
2165
2166 mutex_lock(&nvme_tcp_ctrl_mutex);
2167 list_del(&ctrl->list);
2168 mutex_unlock(&nvme_tcp_ctrl_mutex);
2169
2170 nvmf_free_options(nctrl->opts);
2171free_ctrl:
2172 kfree(ctrl->queues);
2173 kfree(ctrl);
2174}
2175
2176static void nvme_tcp_set_sg_null(struct nvme_command *c)
2177{
2178 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2179
2180 sg->addr = 0;
2181 sg->length = 0;
2182 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2183 NVME_SGL_FMT_TRANSPORT_A;
2184}
2185
2186static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
2187 struct nvme_command *c, u32 data_len)
2188{
2189 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2190
2191 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
2192 sg->length = cpu_to_le32(data_len);
2193 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
2194}
2195
2196static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
2197 u32 data_len)
2198{
2199 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2200
2201 sg->addr = 0;
2202 sg->length = cpu_to_le32(data_len);
2203 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2204 NVME_SGL_FMT_TRANSPORT_A;
2205}
2206
2207static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
2208{
2209 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
2210 struct nvme_tcp_queue *queue = &ctrl->queues[0];
2211 struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
2212 struct nvme_command *cmd = &pdu->cmd;
2213 u8 hdgst = nvme_tcp_hdgst_len(queue);
2214
2215 memset(pdu, 0, sizeof(*pdu));
2216 pdu->hdr.type = nvme_tcp_cmd;
2217 if (queue->hdr_digest)
2218 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2219 pdu->hdr.hlen = sizeof(*pdu);
2220 pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
2221
2222 cmd->common.opcode = nvme_admin_async_event;
2223 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
2224 cmd->common.flags |= NVME_CMD_SGL_METABUF;
2225 nvme_tcp_set_sg_null(cmd);
2226
2227 ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
2228 ctrl->async_req.offset = 0;
2229 ctrl->async_req.curr_bio = NULL;
2230 ctrl->async_req.data_len = 0;
2231
Sagi Grimberg86f03482020-06-18 17:30:23 -07002232 nvme_tcp_queue_request(&ctrl->async_req, true, true);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002233}
2234
Sagi Grimberg236187c2020-07-28 13:16:36 -07002235static void nvme_tcp_complete_timed_out(struct request *rq)
2236{
2237 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2238 struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
2239
Sagi Grimberg236187c2020-07-28 13:16:36 -07002240 nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
Sagi Grimberg0a8a2c852020-10-22 10:15:31 +08002241 if (blk_mq_request_started(rq) && !blk_mq_request_completed(rq)) {
Sagi Grimberg236187c2020-07-28 13:16:36 -07002242 nvme_req(rq)->status = NVME_SC_HOST_ABORTED_CMD;
2243 blk_mq_complete_request(rq);
2244 }
Sagi Grimberg236187c2020-07-28 13:16:36 -07002245}
2246
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002247static enum blk_eh_timer_return
2248nvme_tcp_timeout(struct request *rq, bool reserved)
2249{
2250 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
Sagi Grimberg236187c2020-07-28 13:16:36 -07002251 struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002252 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2253
Sagi Grimberg236187c2020-07-28 13:16:36 -07002254 dev_warn(ctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002255 "queue %d: timeout request %#x type %d\n",
Sagi Grimberg39d57752019-01-08 01:01:30 -08002256 nvme_tcp_queue_id(req->queue), rq->tag, pdu->hdr.type);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002257
Sagi Grimberg236187c2020-07-28 13:16:36 -07002258 if (ctrl->state != NVME_CTRL_LIVE) {
Sagi Grimberg39d57752019-01-08 01:01:30 -08002259 /*
Sagi Grimberg236187c2020-07-28 13:16:36 -07002260 * If we are resetting, connecting or deleting we should
2261 * complete immediately because we may block controller
2262 * teardown or setup sequence
2263 * - ctrl disable/shutdown fabrics requests
2264 * - connect requests
2265 * - initialization admin requests
2266 * - I/O requests that entered after unquiescing and
2267 * the controller stopped responding
2268 *
2269 * All other requests should be cancelled by the error
2270 * recovery work, so it's fine that we fail it here.
Sagi Grimberg39d57752019-01-08 01:01:30 -08002271 */
Sagi Grimberg236187c2020-07-28 13:16:36 -07002272 nvme_tcp_complete_timed_out(rq);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002273 return BLK_EH_DONE;
2274 }
2275
Sagi Grimberg236187c2020-07-28 13:16:36 -07002276 /*
2277 * LIVE state should trigger the normal error recovery which will
2278 * handle completing this request.
2279 */
2280 nvme_tcp_error_recovery(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002281 return BLK_EH_RESET_TIMER;
2282}
2283
2284static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
2285 struct request *rq)
2286{
2287 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2288 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2289 struct nvme_command *c = &pdu->cmd;
2290
2291 c->common.flags |= NVME_CMD_SGL_METABUF;
2292
Sagi Grimberg25e5cb72020-03-23 15:06:30 -07002293 if (!blk_rq_nr_phys_segments(rq))
2294 nvme_tcp_set_sg_null(c);
2295 else if (rq_data_dir(rq) == WRITE &&
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002296 req->data_len <= nvme_tcp_inline_data_size(queue))
2297 nvme_tcp_set_sg_inline(queue, c, req->data_len);
2298 else
2299 nvme_tcp_set_sg_host_data(c, req->data_len);
2300
2301 return 0;
2302}
2303
2304static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
2305 struct request *rq)
2306{
2307 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2308 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2309 struct nvme_tcp_queue *queue = req->queue;
2310 u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
2311 blk_status_t ret;
2312
Keith Buschf4b9e6c2021-03-17 13:37:03 -07002313 ret = nvme_setup_cmd(ns, rq);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002314 if (ret)
2315 return ret;
2316
2317 req->state = NVME_TCP_SEND_CMD_PDU;
Daniel Wagner1ba2e502021-08-30 15:36:26 +02002318 req->status = cpu_to_le16(NVME_SC_SUCCESS);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002319 req->offset = 0;
2320 req->data_sent = 0;
2321 req->pdu_len = 0;
2322 req->pdu_sent = 0;
Sagi Grimberg25e5cb72020-03-23 15:06:30 -07002323 req->data_len = blk_rq_nr_phys_segments(rq) ?
2324 blk_rq_payload_bytes(rq) : 0;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002325 req->curr_bio = rq->bio;
Sagi Grimberge11e5112021-02-10 14:04:00 -08002326 if (req->curr_bio && req->data_len)
Sagi Grimbergcb9b8702021-01-14 13:15:24 -08002327 nvme_tcp_init_iter(req, rq_data_dir(rq));
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002328
2329 if (rq_data_dir(rq) == WRITE &&
2330 req->data_len <= nvme_tcp_inline_data_size(queue))
2331 req->pdu_len = req->data_len;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002332
2333 pdu->hdr.type = nvme_tcp_cmd;
2334 pdu->hdr.flags = 0;
2335 if (queue->hdr_digest)
2336 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2337 if (queue->data_digest && req->pdu_len) {
2338 pdu->hdr.flags |= NVME_TCP_F_DDGST;
2339 ddgst = nvme_tcp_ddgst_len(queue);
2340 }
2341 pdu->hdr.hlen = sizeof(*pdu);
2342 pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
2343 pdu->hdr.plen =
2344 cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
2345
2346 ret = nvme_tcp_map_data(queue, rq);
2347 if (unlikely(ret)) {
Max Gurtovoy28a4cac2019-10-13 19:57:38 +03002348 nvme_cleanup_cmd(rq);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002349 dev_err(queue->ctrl->ctrl.device,
2350 "Failed to map data (%d)\n", ret);
2351 return ret;
2352 }
2353
2354 return 0;
2355}
2356
Sagi Grimberg86f03482020-06-18 17:30:23 -07002357static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx)
2358{
2359 struct nvme_tcp_queue *queue = hctx->driver_data;
2360
2361 if (!llist_empty(&queue->req_list))
2362 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
2363}
2364
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002365static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
2366 const struct blk_mq_queue_data *bd)
2367{
2368 struct nvme_ns *ns = hctx->queue->queuedata;
2369 struct nvme_tcp_queue *queue = hctx->driver_data;
2370 struct request *rq = bd->rq;
2371 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2372 bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
2373 blk_status_t ret;
2374
Tao Chiua9715742021-04-26 10:53:10 +08002375 if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2376 return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002377
2378 ret = nvme_tcp_setup_cmd_pdu(ns, rq);
2379 if (unlikely(ret))
2380 return ret;
2381
2382 blk_mq_start_request(rq);
2383
Sagi Grimberg86f03482020-06-18 17:30:23 -07002384 nvme_tcp_queue_request(req, true, bd->last);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002385
2386 return BLK_STS_OK;
2387}
2388
Sagi Grimberg873946f2018-12-11 23:38:57 -08002389static int nvme_tcp_map_queues(struct blk_mq_tag_set *set)
2390{
2391 struct nvme_tcp_ctrl *ctrl = set->driver_data;
Sagi Grimberg64861992019-05-28 22:49:05 -07002392 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg873946f2018-12-11 23:38:57 -08002393
Sagi Grimberg64861992019-05-28 22:49:05 -07002394 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
Sagi Grimberg873946f2018-12-11 23:38:57 -08002395 /* separate read/write queues */
2396 set->map[HCTX_TYPE_DEFAULT].nr_queues =
Sagi Grimberg64861992019-05-28 22:49:05 -07002397 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2398 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2399 set->map[HCTX_TYPE_READ].nr_queues =
2400 ctrl->io_queues[HCTX_TYPE_READ];
Sagi Grimberg873946f2018-12-11 23:38:57 -08002401 set->map[HCTX_TYPE_READ].queue_offset =
Sagi Grimberg64861992019-05-28 22:49:05 -07002402 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimberg873946f2018-12-11 23:38:57 -08002403 } else {
Sagi Grimberg64861992019-05-28 22:49:05 -07002404 /* shared read/write queues */
Sagi Grimberg873946f2018-12-11 23:38:57 -08002405 set->map[HCTX_TYPE_DEFAULT].nr_queues =
Sagi Grimberg64861992019-05-28 22:49:05 -07002406 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2407 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2408 set->map[HCTX_TYPE_READ].nr_queues =
2409 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimberg873946f2018-12-11 23:38:57 -08002410 set->map[HCTX_TYPE_READ].queue_offset = 0;
2411 }
2412 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
2413 blk_mq_map_queues(&set->map[HCTX_TYPE_READ]);
Sagi Grimberg64861992019-05-28 22:49:05 -07002414
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002415 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
2416 /* map dedicated poll queues only if we have queues left */
2417 set->map[HCTX_TYPE_POLL].nr_queues =
2418 ctrl->io_queues[HCTX_TYPE_POLL];
2419 set->map[HCTX_TYPE_POLL].queue_offset =
2420 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
2421 ctrl->io_queues[HCTX_TYPE_READ];
2422 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
2423 }
2424
Sagi Grimberg64861992019-05-28 22:49:05 -07002425 dev_info(ctrl->ctrl.device,
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002426 "mapped %d/%d/%d default/read/poll queues.\n",
Sagi Grimberg64861992019-05-28 22:49:05 -07002427 ctrl->io_queues[HCTX_TYPE_DEFAULT],
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002428 ctrl->io_queues[HCTX_TYPE_READ],
2429 ctrl->io_queues[HCTX_TYPE_POLL]);
Sagi Grimberg64861992019-05-28 22:49:05 -07002430
Sagi Grimberg873946f2018-12-11 23:38:57 -08002431 return 0;
2432}
2433
Jens Axboe5a72e892021-10-12 09:24:29 -06002434static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002435{
2436 struct nvme_tcp_queue *queue = hctx->driver_data;
2437 struct sock *sk = queue->sock->sk;
2438
Sagi Grimbergf86e5bf2020-03-23 16:43:52 -07002439 if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2440 return 0;
2441
Sagi Grimberg72e5d752020-05-01 14:25:44 -07002442 set_bit(NVME_TCP_Q_POLLING, &queue->flags);
Eric Dumazet3f926af2019-10-23 22:44:51 -07002443 if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue))
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002444 sk_busy_loop(sk, true);
2445 nvme_tcp_try_recv(queue);
Sagi Grimberg72e5d752020-05-01 14:25:44 -07002446 clear_bit(NVME_TCP_Q_POLLING, &queue->flags);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002447 return queue->nr_cqe;
2448}
2449
Rikard Falkeborn6acbd962020-05-29 00:25:07 +02002450static const struct blk_mq_ops nvme_tcp_mq_ops = {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002451 .queue_rq = nvme_tcp_queue_rq,
Sagi Grimberg86f03482020-06-18 17:30:23 -07002452 .commit_rqs = nvme_tcp_commit_rqs,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002453 .complete = nvme_complete_rq,
2454 .init_request = nvme_tcp_init_request,
2455 .exit_request = nvme_tcp_exit_request,
2456 .init_hctx = nvme_tcp_init_hctx,
2457 .timeout = nvme_tcp_timeout,
Sagi Grimberg873946f2018-12-11 23:38:57 -08002458 .map_queues = nvme_tcp_map_queues,
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002459 .poll = nvme_tcp_poll,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002460};
2461
Rikard Falkeborn6acbd962020-05-29 00:25:07 +02002462static const struct blk_mq_ops nvme_tcp_admin_mq_ops = {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002463 .queue_rq = nvme_tcp_queue_rq,
2464 .complete = nvme_complete_rq,
2465 .init_request = nvme_tcp_init_request,
2466 .exit_request = nvme_tcp_exit_request,
2467 .init_hctx = nvme_tcp_init_admin_hctx,
2468 .timeout = nvme_tcp_timeout,
2469};
2470
2471static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
2472 .name = "tcp",
2473 .module = THIS_MODULE,
2474 .flags = NVME_F_FABRICS,
2475 .reg_read32 = nvmf_reg_read32,
2476 .reg_read64 = nvmf_reg_read64,
2477 .reg_write32 = nvmf_reg_write32,
2478 .free_ctrl = nvme_tcp_free_ctrl,
2479 .submit_async_event = nvme_tcp_submit_async_event,
2480 .delete_ctrl = nvme_tcp_delete_ctrl,
2481 .get_address = nvmf_get_address,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002482};
2483
2484static bool
2485nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
2486{
2487 struct nvme_tcp_ctrl *ctrl;
2488 bool found = false;
2489
2490 mutex_lock(&nvme_tcp_ctrl_mutex);
2491 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
2492 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2493 if (found)
2494 break;
2495 }
2496 mutex_unlock(&nvme_tcp_ctrl_mutex);
2497
2498 return found;
2499}
2500
2501static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
2502 struct nvmf_ctrl_options *opts)
2503{
2504 struct nvme_tcp_ctrl *ctrl;
2505 int ret;
2506
2507 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2508 if (!ctrl)
2509 return ERR_PTR(-ENOMEM);
2510
2511 INIT_LIST_HEAD(&ctrl->list);
2512 ctrl->ctrl.opts = opts;
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002513 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2514 opts->nr_poll_queues + 1;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002515 ctrl->ctrl.sqsize = opts->queue_size - 1;
2516 ctrl->ctrl.kato = opts->kato;
2517
2518 INIT_DELAYED_WORK(&ctrl->connect_work,
2519 nvme_tcp_reconnect_ctrl_work);
2520 INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
2521 INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
2522
2523 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2524 opts->trsvcid =
2525 kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
2526 if (!opts->trsvcid) {
2527 ret = -ENOMEM;
2528 goto out_free_ctrl;
2529 }
2530 opts->mask |= NVMF_OPT_TRSVCID;
2531 }
2532
2533 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2534 opts->traddr, opts->trsvcid, &ctrl->addr);
2535 if (ret) {
2536 pr_err("malformed address passed: %s:%s\n",
2537 opts->traddr, opts->trsvcid);
2538 goto out_free_ctrl;
2539 }
2540
2541 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2542 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2543 opts->host_traddr, NULL, &ctrl->src_addr);
2544 if (ret) {
2545 pr_err("malformed src address passed: %s\n",
2546 opts->host_traddr);
2547 goto out_free_ctrl;
2548 }
2549 }
2550
Martin Belanger3ede8f72021-05-20 15:09:34 -04002551 if (opts->mask & NVMF_OPT_HOST_IFACE) {
Prabhakar Kushwaha8b43ced2021-07-13 11:31:56 +02002552 if (!__dev_get_by_name(&init_net, opts->host_iface)) {
Martin Belanger3ede8f72021-05-20 15:09:34 -04002553 pr_err("invalid interface passed: %s\n",
2554 opts->host_iface);
2555 ret = -ENODEV;
2556 goto out_free_ctrl;
2557 }
2558 }
2559
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002560 if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
2561 ret = -EALREADY;
2562 goto out_free_ctrl;
2563 }
2564
Sagi Grimberg873946f2018-12-11 23:38:57 -08002565 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002566 GFP_KERNEL);
2567 if (!ctrl->queues) {
2568 ret = -ENOMEM;
2569 goto out_free_ctrl;
2570 }
2571
2572 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
2573 if (ret)
2574 goto out_kfree_queues;
2575
2576 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2577 WARN_ON_ONCE(1);
2578 ret = -EINTR;
2579 goto out_uninit_ctrl;
2580 }
2581
2582 ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
2583 if (ret)
2584 goto out_uninit_ctrl;
2585
2586 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
Hannes Reineckee5ea42f2021-09-22 08:35:25 +02002587 nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002588
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002589 mutex_lock(&nvme_tcp_ctrl_mutex);
2590 list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
2591 mutex_unlock(&nvme_tcp_ctrl_mutex);
2592
2593 return &ctrl->ctrl;
2594
2595out_uninit_ctrl:
2596 nvme_uninit_ctrl(&ctrl->ctrl);
2597 nvme_put_ctrl(&ctrl->ctrl);
2598 if (ret > 0)
2599 ret = -EIO;
2600 return ERR_PTR(ret);
2601out_kfree_queues:
2602 kfree(ctrl->queues);
2603out_free_ctrl:
2604 kfree(ctrl);
2605 return ERR_PTR(ret);
2606}
2607
2608static struct nvmf_transport_ops nvme_tcp_transport = {
2609 .name = "tcp",
2610 .module = THIS_MODULE,
2611 .required_opts = NVMF_OPT_TRADDR,
2612 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2613 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
Sagi Grimberg873946f2018-12-11 23:38:57 -08002614 NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
Israel Rukshinbb139852019-08-18 12:08:54 +03002615 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
Martin Belanger3ede8f72021-05-20 15:09:34 -04002616 NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002617 .create_ctrl = nvme_tcp_create_ctrl,
2618};
2619
2620static int __init nvme_tcp_init_module(void)
2621{
2622 nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq",
2623 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2624 if (!nvme_tcp_wq)
2625 return -ENOMEM;
2626
2627 nvmf_register_transport(&nvme_tcp_transport);
2628 return 0;
2629}
2630
2631static void __exit nvme_tcp_cleanup_module(void)
2632{
2633 struct nvme_tcp_ctrl *ctrl;
2634
2635 nvmf_unregister_transport(&nvme_tcp_transport);
2636
2637 mutex_lock(&nvme_tcp_ctrl_mutex);
2638 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
2639 nvme_delete_ctrl(&ctrl->ctrl);
2640 mutex_unlock(&nvme_tcp_ctrl_mutex);
2641 flush_workqueue(nvme_delete_wq);
2642
2643 destroy_workqueue(nvme_tcp_wq);
2644}
2645
2646module_init(nvme_tcp_init_module);
2647module_exit(nvme_tcp_cleanup_module);
2648
2649MODULE_LICENSE("GPL v2");