blob: a307972d33a02903a85e97aa29cec3ab16894dae [file] [log] [blame]
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * NVMe over Fabrics TCP host.
4 * Copyright (c) 2018 Lightbits Labs. All rights reserved.
5 */
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/err.h>
11#include <linux/nvme-tcp.h>
12#include <net/sock.h>
13#include <net/tcp.h>
14#include <linux/blk-mq.h>
15#include <crypto/hash.h>
Sagi Grimberg1a9460c2019-07-03 14:08:04 -070016#include <net/busy_poll.h>
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080017
18#include "nvme.h"
19#include "fabrics.h"
20
21struct nvme_tcp_queue;
22
Wunderlich, Mark9912ade2020-01-16 00:46:12 +000023/* Define the socket priority to use for connections were it is desirable
24 * that the NIC consider performing optimized packet processing or filtering.
25 * A non-zero value being sufficient to indicate general consideration of any
26 * possible optimization. Making it a module param allows for alternative
27 * values that may be unique for some NIC implementations.
28 */
29static int so_priority;
30module_param(so_priority, int, 0644);
31MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority");
32
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080033enum nvme_tcp_send_state {
34 NVME_TCP_SEND_CMD_PDU = 0,
35 NVME_TCP_SEND_H2C_PDU,
36 NVME_TCP_SEND_DATA,
37 NVME_TCP_SEND_DDGST,
38};
39
40struct nvme_tcp_request {
41 struct nvme_request req;
42 void *pdu;
43 struct nvme_tcp_queue *queue;
44 u32 data_len;
45 u32 pdu_len;
46 u32 pdu_sent;
47 u16 ttag;
48 struct list_head entry;
Christoph Hellwiga7273d42018-12-13 09:46:59 +010049 __le32 ddgst;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080050
51 struct bio *curr_bio;
52 struct iov_iter iter;
53
54 /* send state */
55 size_t offset;
56 size_t data_sent;
57 enum nvme_tcp_send_state state;
58};
59
60enum nvme_tcp_queue_flags {
61 NVME_TCP_Q_ALLOCATED = 0,
62 NVME_TCP_Q_LIVE = 1,
63};
64
65enum nvme_tcp_recv_state {
66 NVME_TCP_RECV_PDU = 0,
67 NVME_TCP_RECV_DATA,
68 NVME_TCP_RECV_DDGST,
69};
70
71struct nvme_tcp_ctrl;
72struct nvme_tcp_queue {
73 struct socket *sock;
74 struct work_struct io_work;
75 int io_cpu;
76
77 spinlock_t lock;
78 struct list_head send_list;
79
80 /* recv state */
81 void *pdu;
82 int pdu_remaining;
83 int pdu_offset;
84 size_t data_remaining;
85 size_t ddgst_remaining;
Sagi Grimberg1a9460c2019-07-03 14:08:04 -070086 unsigned int nr_cqe;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -080087
88 /* send state */
89 struct nvme_tcp_request *request;
90
91 int queue_size;
92 size_t cmnd_capsule_len;
93 struct nvme_tcp_ctrl *ctrl;
94 unsigned long flags;
95 bool rd_enabled;
96
97 bool hdr_digest;
98 bool data_digest;
99 struct ahash_request *rcv_hash;
100 struct ahash_request *snd_hash;
101 __le32 exp_ddgst;
102 __le32 recv_ddgst;
103
104 struct page_frag_cache pf_cache;
105
106 void (*state_change)(struct sock *);
107 void (*data_ready)(struct sock *);
108 void (*write_space)(struct sock *);
109};
110
111struct nvme_tcp_ctrl {
112 /* read only in the hot path */
113 struct nvme_tcp_queue *queues;
114 struct blk_mq_tag_set tag_set;
115
116 /* other member variables */
117 struct list_head list;
118 struct blk_mq_tag_set admin_tag_set;
119 struct sockaddr_storage addr;
120 struct sockaddr_storage src_addr;
121 struct nvme_ctrl ctrl;
122
123 struct work_struct err_work;
124 struct delayed_work connect_work;
125 struct nvme_tcp_request async_req;
Sagi Grimberg64861992019-05-28 22:49:05 -0700126 u32 io_queues[HCTX_MAX_TYPES];
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800127};
128
129static LIST_HEAD(nvme_tcp_ctrl_list);
130static DEFINE_MUTEX(nvme_tcp_ctrl_mutex);
131static struct workqueue_struct *nvme_tcp_wq;
132static struct blk_mq_ops nvme_tcp_mq_ops;
133static struct blk_mq_ops nvme_tcp_admin_mq_ops;
134
135static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl)
136{
137 return container_of(ctrl, struct nvme_tcp_ctrl, ctrl);
138}
139
140static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
141{
142 return queue - queue->ctrl->queues;
143}
144
145static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue)
146{
147 u32 queue_idx = nvme_tcp_queue_id(queue);
148
149 if (queue_idx == 0)
150 return queue->ctrl->admin_tag_set.tags[queue_idx];
151 return queue->ctrl->tag_set.tags[queue_idx - 1];
152}
153
154static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue)
155{
156 return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
157}
158
159static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue)
160{
161 return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
162}
163
164static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_queue *queue)
165{
166 return queue->cmnd_capsule_len - sizeof(struct nvme_command);
167}
168
169static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req)
170{
171 return req == &req->queue->ctrl->async_req;
172}
173
174static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req)
175{
176 struct request *rq;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800177
178 if (unlikely(nvme_tcp_async_req(req)))
179 return false; /* async events don't have a request */
180
181 rq = blk_mq_rq_from_pdu(req);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800182
Sagi Grimberg25e5cb72020-03-23 15:06:30 -0700183 return rq_data_dir(rq) == WRITE && req->data_len &&
184 req->data_len <= nvme_tcp_inline_data_size(req->queue);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800185}
186
187static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req)
188{
189 return req->iter.bvec->bv_page;
190}
191
192static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req)
193{
194 return req->iter.bvec->bv_offset + req->iter.iov_offset;
195}
196
197static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req)
198{
199 return min_t(size_t, req->iter.bvec->bv_len - req->iter.iov_offset,
200 req->pdu_len - req->pdu_sent);
201}
202
203static inline size_t nvme_tcp_req_offset(struct nvme_tcp_request *req)
204{
205 return req->iter.iov_offset;
206}
207
208static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req)
209{
210 return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ?
211 req->pdu_len - req->pdu_sent : 0;
212}
213
214static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
215 int len)
216{
217 return nvme_tcp_pdu_data_left(req) <= len;
218}
219
220static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
221 unsigned int dir)
222{
223 struct request *rq = blk_mq_rq_from_pdu(req);
224 struct bio_vec *vec;
225 unsigned int size;
226 int nsegs;
227 size_t offset;
228
229 if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
230 vec = &rq->special_vec;
231 nsegs = 1;
232 size = blk_rq_payload_bytes(rq);
233 offset = 0;
234 } else {
235 struct bio *bio = req->curr_bio;
236
237 vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
238 nsegs = bio_segments(bio);
239 size = bio->bi_iter.bi_size;
240 offset = bio->bi_iter.bi_bvec_done;
241 }
242
243 iov_iter_bvec(&req->iter, dir, vec, nsegs, size);
244 req->iter.iov_offset = offset;
245}
246
247static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req,
248 int len)
249{
250 req->data_sent += len;
251 req->pdu_sent += len;
252 iov_iter_advance(&req->iter, len);
253 if (!iov_iter_count(&req->iter) &&
254 req->data_sent < req->data_len) {
255 req->curr_bio = req->curr_bio->bi_next;
256 nvme_tcp_init_iter(req, WRITE);
257 }
258}
259
260static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req)
261{
262 struct nvme_tcp_queue *queue = req->queue;
263
264 spin_lock(&queue->lock);
265 list_add_tail(&req->entry, &queue->send_list);
266 spin_unlock(&queue->lock);
267
268 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
269}
270
271static inline struct nvme_tcp_request *
272nvme_tcp_fetch_request(struct nvme_tcp_queue *queue)
273{
274 struct nvme_tcp_request *req;
275
276 spin_lock(&queue->lock);
277 req = list_first_entry_or_null(&queue->send_list,
278 struct nvme_tcp_request, entry);
279 if (req)
280 list_del(&req->entry);
281 spin_unlock(&queue->lock);
282
283 return req;
284}
285
Christoph Hellwiga7273d42018-12-13 09:46:59 +0100286static inline void nvme_tcp_ddgst_final(struct ahash_request *hash,
287 __le32 *dgst)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800288{
289 ahash_request_set_crypt(hash, NULL, (u8 *)dgst, 0);
290 crypto_ahash_final(hash);
291}
292
293static inline void nvme_tcp_ddgst_update(struct ahash_request *hash,
294 struct page *page, off_t off, size_t len)
295{
296 struct scatterlist sg;
297
298 sg_init_marker(&sg, 1);
299 sg_set_page(&sg, page, len, off);
300 ahash_request_set_crypt(hash, &sg, NULL, len);
301 crypto_ahash_update(hash);
302}
303
304static inline void nvme_tcp_hdgst(struct ahash_request *hash,
305 void *pdu, size_t len)
306{
307 struct scatterlist sg;
308
309 sg_init_one(&sg, pdu, len);
310 ahash_request_set_crypt(hash, &sg, pdu + len, len);
311 crypto_ahash_digest(hash);
312}
313
314static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue,
315 void *pdu, size_t pdu_len)
316{
317 struct nvme_tcp_hdr *hdr = pdu;
318 __le32 recv_digest;
319 __le32 exp_digest;
320
321 if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
322 dev_err(queue->ctrl->ctrl.device,
323 "queue %d: header digest flag is cleared\n",
324 nvme_tcp_queue_id(queue));
325 return -EPROTO;
326 }
327
328 recv_digest = *(__le32 *)(pdu + hdr->hlen);
329 nvme_tcp_hdgst(queue->rcv_hash, pdu, pdu_len);
330 exp_digest = *(__le32 *)(pdu + hdr->hlen);
331 if (recv_digest != exp_digest) {
332 dev_err(queue->ctrl->ctrl.device,
333 "header digest error: recv %#x expected %#x\n",
334 le32_to_cpu(recv_digest), le32_to_cpu(exp_digest));
335 return -EIO;
336 }
337
338 return 0;
339}
340
341static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu)
342{
343 struct nvme_tcp_hdr *hdr = pdu;
344 u8 digest_len = nvme_tcp_hdgst_len(queue);
345 u32 len;
346
347 len = le32_to_cpu(hdr->plen) - hdr->hlen -
348 ((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0);
349
350 if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
351 dev_err(queue->ctrl->ctrl.device,
352 "queue %d: data digest flag is cleared\n",
353 nvme_tcp_queue_id(queue));
354 return -EPROTO;
355 }
356 crypto_ahash_init(queue->rcv_hash);
357
358 return 0;
359}
360
361static void nvme_tcp_exit_request(struct blk_mq_tag_set *set,
362 struct request *rq, unsigned int hctx_idx)
363{
364 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
365
366 page_frag_free(req->pdu);
367}
368
369static int nvme_tcp_init_request(struct blk_mq_tag_set *set,
370 struct request *rq, unsigned int hctx_idx,
371 unsigned int numa_node)
372{
373 struct nvme_tcp_ctrl *ctrl = set->driver_data;
374 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
375 int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
376 struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx];
377 u8 hdgst = nvme_tcp_hdgst_len(queue);
378
379 req->pdu = page_frag_alloc(&queue->pf_cache,
380 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
381 GFP_KERNEL | __GFP_ZERO);
382 if (!req->pdu)
383 return -ENOMEM;
384
385 req->queue = queue;
386 nvme_req(rq)->ctrl = &ctrl->ctrl;
387
388 return 0;
389}
390
391static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
392 unsigned int hctx_idx)
393{
394 struct nvme_tcp_ctrl *ctrl = data;
395 struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1];
396
397 hctx->driver_data = queue;
398 return 0;
399}
400
401static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
402 unsigned int hctx_idx)
403{
404 struct nvme_tcp_ctrl *ctrl = data;
405 struct nvme_tcp_queue *queue = &ctrl->queues[0];
406
407 hctx->driver_data = queue;
408 return 0;
409}
410
411static enum nvme_tcp_recv_state
412nvme_tcp_recv_state(struct nvme_tcp_queue *queue)
413{
414 return (queue->pdu_remaining) ? NVME_TCP_RECV_PDU :
415 (queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST :
416 NVME_TCP_RECV_DATA;
417}
418
419static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
420{
421 queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) +
422 nvme_tcp_hdgst_len(queue);
423 queue->pdu_offset = 0;
424 queue->data_remaining = -1;
425 queue->ddgst_remaining = 0;
426}
427
428static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
429{
430 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
431 return;
432
Nigel Kirkland97b25122020-02-10 16:01:45 -0800433 queue_work(nvme_reset_wq, &to_tcp_ctrl(ctrl)->err_work);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800434}
435
436static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue,
437 struct nvme_completion *cqe)
438{
439 struct request *rq;
440
441 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), cqe->command_id);
442 if (!rq) {
443 dev_err(queue->ctrl->ctrl.device,
444 "queue %d tag 0x%x not found\n",
445 nvme_tcp_queue_id(queue), cqe->command_id);
446 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
447 return -EINVAL;
448 }
449
450 nvme_end_request(rq, cqe->status, cqe->result);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700451 queue->nr_cqe++;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800452
453 return 0;
454}
455
456static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
457 struct nvme_tcp_data_pdu *pdu)
458{
459 struct request *rq;
460
461 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
462 if (!rq) {
463 dev_err(queue->ctrl->ctrl.device,
464 "queue %d tag %#x not found\n",
465 nvme_tcp_queue_id(queue), pdu->command_id);
466 return -ENOENT;
467 }
468
469 if (!blk_rq_payload_bytes(rq)) {
470 dev_err(queue->ctrl->ctrl.device,
471 "queue %d tag %#x unexpected data\n",
472 nvme_tcp_queue_id(queue), rq->tag);
473 return -EIO;
474 }
475
476 queue->data_remaining = le32_to_cpu(pdu->data_length);
477
Sagi Grimberg602d6742019-03-13 18:55:10 +0100478 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS &&
479 unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) {
480 dev_err(queue->ctrl->ctrl.device,
481 "queue %d tag %#x SUCCESS set but not last PDU\n",
482 nvme_tcp_queue_id(queue), rq->tag);
483 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
484 return -EPROTO;
485 }
486
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800487 return 0;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800488}
489
490static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue,
491 struct nvme_tcp_rsp_pdu *pdu)
492{
493 struct nvme_completion *cqe = &pdu->cqe;
494 int ret = 0;
495
496 /*
497 * AEN requests are special as they don't time out and can
498 * survive any kind of queue freeze and often don't respond to
499 * aborts. We don't even bother to allocate a struct request
500 * for them but rather special case them here.
501 */
Israel Rukshin58a8df62019-10-13 19:57:31 +0300502 if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue),
503 cqe->command_id)))
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800504 nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
505 &cqe->result);
506 else
507 ret = nvme_tcp_process_nvme_cqe(queue, cqe);
508
509 return ret;
510}
511
512static int nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req,
513 struct nvme_tcp_r2t_pdu *pdu)
514{
515 struct nvme_tcp_data_pdu *data = req->pdu;
516 struct nvme_tcp_queue *queue = req->queue;
517 struct request *rq = blk_mq_rq_from_pdu(req);
518 u8 hdgst = nvme_tcp_hdgst_len(queue);
519 u8 ddgst = nvme_tcp_ddgst_len(queue);
520
521 req->pdu_len = le32_to_cpu(pdu->r2t_length);
522 req->pdu_sent = 0;
523
524 if (unlikely(req->data_sent + req->pdu_len > req->data_len)) {
525 dev_err(queue->ctrl->ctrl.device,
526 "req %d r2t len %u exceeded data len %u (%zu sent)\n",
527 rq->tag, req->pdu_len, req->data_len,
528 req->data_sent);
529 return -EPROTO;
530 }
531
532 if (unlikely(le32_to_cpu(pdu->r2t_offset) < req->data_sent)) {
533 dev_err(queue->ctrl->ctrl.device,
534 "req %d unexpected r2t offset %u (expected %zu)\n",
535 rq->tag, le32_to_cpu(pdu->r2t_offset),
536 req->data_sent);
537 return -EPROTO;
538 }
539
540 memset(data, 0, sizeof(*data));
541 data->hdr.type = nvme_tcp_h2c_data;
542 data->hdr.flags = NVME_TCP_F_DATA_LAST;
543 if (queue->hdr_digest)
544 data->hdr.flags |= NVME_TCP_F_HDGST;
545 if (queue->data_digest)
546 data->hdr.flags |= NVME_TCP_F_DDGST;
547 data->hdr.hlen = sizeof(*data);
548 data->hdr.pdo = data->hdr.hlen + hdgst;
549 data->hdr.plen =
550 cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst);
551 data->ttag = pdu->ttag;
552 data->command_id = rq->tag;
553 data->data_offset = cpu_to_le32(req->data_sent);
554 data->data_length = cpu_to_le32(req->pdu_len);
555 return 0;
556}
557
558static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
559 struct nvme_tcp_r2t_pdu *pdu)
560{
561 struct nvme_tcp_request *req;
562 struct request *rq;
563 int ret;
564
565 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
566 if (!rq) {
567 dev_err(queue->ctrl->ctrl.device,
568 "queue %d tag %#x not found\n",
569 nvme_tcp_queue_id(queue), pdu->command_id);
570 return -ENOENT;
571 }
572 req = blk_mq_rq_to_pdu(rq);
573
574 ret = nvme_tcp_setup_h2c_data_pdu(req, pdu);
575 if (unlikely(ret))
576 return ret;
577
578 req->state = NVME_TCP_SEND_H2C_PDU;
579 req->offset = 0;
580
581 nvme_tcp_queue_request(req);
582
583 return 0;
584}
585
586static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
587 unsigned int *offset, size_t *len)
588{
589 struct nvme_tcp_hdr *hdr;
590 char *pdu = queue->pdu;
591 size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
592 int ret;
593
594 ret = skb_copy_bits(skb, *offset,
595 &pdu[queue->pdu_offset], rcv_len);
596 if (unlikely(ret))
597 return ret;
598
599 queue->pdu_remaining -= rcv_len;
600 queue->pdu_offset += rcv_len;
601 *offset += rcv_len;
602 *len -= rcv_len;
603 if (queue->pdu_remaining)
604 return 0;
605
606 hdr = queue->pdu;
607 if (queue->hdr_digest) {
608 ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen);
609 if (unlikely(ret))
610 return ret;
611 }
612
613
614 if (queue->data_digest) {
615 ret = nvme_tcp_check_ddgst(queue, queue->pdu);
616 if (unlikely(ret))
617 return ret;
618 }
619
620 switch (hdr->type) {
621 case nvme_tcp_c2h_data:
Sagi Grimberg6be18262019-07-19 12:46:46 -0700622 return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800623 case nvme_tcp_rsp:
624 nvme_tcp_init_recv_ctx(queue);
Sagi Grimberg6be18262019-07-19 12:46:46 -0700625 return nvme_tcp_handle_comp(queue, (void *)queue->pdu);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800626 case nvme_tcp_r2t:
627 nvme_tcp_init_recv_ctx(queue);
Sagi Grimberg6be18262019-07-19 12:46:46 -0700628 return nvme_tcp_handle_r2t(queue, (void *)queue->pdu);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800629 default:
630 dev_err(queue->ctrl->ctrl.device,
631 "unsupported pdu type (%d)\n", hdr->type);
632 return -EINVAL;
633 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800634}
635
Christoph Hellwig988aef9e2019-03-15 08:41:04 +0100636static inline void nvme_tcp_end_request(struct request *rq, u16 status)
Sagi Grimberg602d6742019-03-13 18:55:10 +0100637{
638 union nvme_result res = {};
639
640 nvme_end_request(rq, cpu_to_le16(status << 1), res);
641}
642
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800643static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb,
644 unsigned int *offset, size_t *len)
645{
646 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
647 struct nvme_tcp_request *req;
648 struct request *rq;
649
650 rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
651 if (!rq) {
652 dev_err(queue->ctrl->ctrl.device,
653 "queue %d tag %#x not found\n",
654 nvme_tcp_queue_id(queue), pdu->command_id);
655 return -ENOENT;
656 }
657 req = blk_mq_rq_to_pdu(rq);
658
659 while (true) {
660 int recv_len, ret;
661
662 recv_len = min_t(size_t, *len, queue->data_remaining);
663 if (!recv_len)
664 break;
665
666 if (!iov_iter_count(&req->iter)) {
667 req->curr_bio = req->curr_bio->bi_next;
668
669 /*
670 * If we don`t have any bios it means that controller
671 * sent more data than we requested, hence error
672 */
673 if (!req->curr_bio) {
674 dev_err(queue->ctrl->ctrl.device,
675 "queue %d no space in request %#x",
676 nvme_tcp_queue_id(queue), rq->tag);
677 nvme_tcp_init_recv_ctx(queue);
678 return -EIO;
679 }
680 nvme_tcp_init_iter(req, READ);
681 }
682
683 /* we can read only from what is left in this bio */
684 recv_len = min_t(size_t, recv_len,
685 iov_iter_count(&req->iter));
686
687 if (queue->data_digest)
688 ret = skb_copy_and_hash_datagram_iter(skb, *offset,
689 &req->iter, recv_len, queue->rcv_hash);
690 else
691 ret = skb_copy_datagram_iter(skb, *offset,
692 &req->iter, recv_len);
693 if (ret) {
694 dev_err(queue->ctrl->ctrl.device,
695 "queue %d failed to copy request %#x data",
696 nvme_tcp_queue_id(queue), rq->tag);
697 return ret;
698 }
699
700 *len -= recv_len;
701 *offset += recv_len;
702 queue->data_remaining -= recv_len;
703 }
704
705 if (!queue->data_remaining) {
706 if (queue->data_digest) {
707 nvme_tcp_ddgst_final(queue->rcv_hash, &queue->exp_ddgst);
708 queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH;
709 } else {
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700710 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
Sagi Grimberg602d6742019-03-13 18:55:10 +0100711 nvme_tcp_end_request(rq, NVME_SC_SUCCESS);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700712 queue->nr_cqe++;
713 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800714 nvme_tcp_init_recv_ctx(queue);
715 }
716 }
717
718 return 0;
719}
720
721static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue,
722 struct sk_buff *skb, unsigned int *offset, size_t *len)
723{
Sagi Grimberg602d6742019-03-13 18:55:10 +0100724 struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800725 char *ddgst = (char *)&queue->recv_ddgst;
726 size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining);
727 off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining;
728 int ret;
729
730 ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len);
731 if (unlikely(ret))
732 return ret;
733
734 queue->ddgst_remaining -= recv_len;
735 *offset += recv_len;
736 *len -= recv_len;
737 if (queue->ddgst_remaining)
738 return 0;
739
740 if (queue->recv_ddgst != queue->exp_ddgst) {
741 dev_err(queue->ctrl->ctrl.device,
742 "data digest error: recv %#x expected %#x\n",
743 le32_to_cpu(queue->recv_ddgst),
744 le32_to_cpu(queue->exp_ddgst));
745 return -EIO;
746 }
747
Sagi Grimberg602d6742019-03-13 18:55:10 +0100748 if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
749 struct request *rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue),
750 pdu->command_id);
751
752 nvme_tcp_end_request(rq, NVME_SC_SUCCESS);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -0700753 queue->nr_cqe++;
Sagi Grimberg602d6742019-03-13 18:55:10 +0100754 }
755
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800756 nvme_tcp_init_recv_ctx(queue);
757 return 0;
758}
759
760static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,
761 unsigned int offset, size_t len)
762{
763 struct nvme_tcp_queue *queue = desc->arg.data;
764 size_t consumed = len;
765 int result;
766
767 while (len) {
768 switch (nvme_tcp_recv_state(queue)) {
769 case NVME_TCP_RECV_PDU:
770 result = nvme_tcp_recv_pdu(queue, skb, &offset, &len);
771 break;
772 case NVME_TCP_RECV_DATA:
773 result = nvme_tcp_recv_data(queue, skb, &offset, &len);
774 break;
775 case NVME_TCP_RECV_DDGST:
776 result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len);
777 break;
778 default:
779 result = -EFAULT;
780 }
781 if (result) {
782 dev_err(queue->ctrl->ctrl.device,
783 "receive failed: %d\n", result);
784 queue->rd_enabled = false;
785 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
786 return result;
787 }
788 }
789
790 return consumed;
791}
792
793static void nvme_tcp_data_ready(struct sock *sk)
794{
795 struct nvme_tcp_queue *queue;
796
797 read_lock(&sk->sk_callback_lock);
798 queue = sk->sk_user_data;
799 if (likely(queue && queue->rd_enabled))
800 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
801 read_unlock(&sk->sk_callback_lock);
802}
803
804static void nvme_tcp_write_space(struct sock *sk)
805{
806 struct nvme_tcp_queue *queue;
807
808 read_lock_bh(&sk->sk_callback_lock);
809 queue = sk->sk_user_data;
810 if (likely(queue && sk_stream_is_writeable(sk))) {
811 clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
812 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
813 }
814 read_unlock_bh(&sk->sk_callback_lock);
815}
816
817static void nvme_tcp_state_change(struct sock *sk)
818{
819 struct nvme_tcp_queue *queue;
820
821 read_lock(&sk->sk_callback_lock);
822 queue = sk->sk_user_data;
823 if (!queue)
824 goto done;
825
826 switch (sk->sk_state) {
827 case TCP_CLOSE:
828 case TCP_CLOSE_WAIT:
829 case TCP_LAST_ACK:
830 case TCP_FIN_WAIT1:
831 case TCP_FIN_WAIT2:
832 /* fallthrough */
833 nvme_tcp_error_recovery(&queue->ctrl->ctrl);
834 break;
835 default:
836 dev_info(queue->ctrl->ctrl.device,
837 "queue %d socket state %d\n",
838 nvme_tcp_queue_id(queue), sk->sk_state);
839 }
840
841 queue->state_change(sk);
842done:
843 read_unlock(&sk->sk_callback_lock);
844}
845
846static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue)
847{
848 queue->request = NULL;
849}
850
851static void nvme_tcp_fail_request(struct nvme_tcp_request *req)
852{
Sagi Grimberg16686012019-08-02 18:17:52 -0700853 nvme_tcp_end_request(blk_mq_rq_from_pdu(req), NVME_SC_HOST_PATH_ERROR);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800854}
855
856static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
857{
858 struct nvme_tcp_queue *queue = req->queue;
859
860 while (true) {
861 struct page *page = nvme_tcp_req_cur_page(req);
862 size_t offset = nvme_tcp_req_cur_offset(req);
863 size_t len = nvme_tcp_req_cur_length(req);
864 bool last = nvme_tcp_pdu_last_send(req, len);
865 int ret, flags = MSG_DONTWAIT;
866
867 if (last && !queue->data_digest)
868 flags |= MSG_EOR;
869 else
870 flags |= MSG_MORE;
871
Mikhail Skorzhinskii37c15212019-07-08 12:31:29 +0200872 /* can't zcopy slab pages */
873 if (unlikely(PageSlab(page))) {
874 ret = sock_no_sendpage(queue->sock, page, offset, len,
875 flags);
876 } else {
877 ret = kernel_sendpage(queue->sock, page, offset, len,
878 flags);
879 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -0800880 if (ret <= 0)
881 return ret;
882
883 nvme_tcp_advance_req(req, ret);
884 if (queue->data_digest)
885 nvme_tcp_ddgst_update(queue->snd_hash, page,
886 offset, ret);
887
888 /* fully successful last write*/
889 if (last && ret == len) {
890 if (queue->data_digest) {
891 nvme_tcp_ddgst_final(queue->snd_hash,
892 &req->ddgst);
893 req->state = NVME_TCP_SEND_DDGST;
894 req->offset = 0;
895 } else {
896 nvme_tcp_done_send_req(queue);
897 }
898 return 1;
899 }
900 }
901 return -EAGAIN;
902}
903
904static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
905{
906 struct nvme_tcp_queue *queue = req->queue;
907 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
908 bool inline_data = nvme_tcp_has_inline_data(req);
909 int flags = MSG_DONTWAIT | (inline_data ? MSG_MORE : MSG_EOR);
910 u8 hdgst = nvme_tcp_hdgst_len(queue);
911 int len = sizeof(*pdu) + hdgst - req->offset;
912 int ret;
913
914 if (queue->hdr_digest && !req->offset)
915 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
916
917 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
918 offset_in_page(pdu) + req->offset, len, flags);
919 if (unlikely(ret <= 0))
920 return ret;
921
922 len -= ret;
923 if (!len) {
924 if (inline_data) {
925 req->state = NVME_TCP_SEND_DATA;
926 if (queue->data_digest)
927 crypto_ahash_init(queue->snd_hash);
928 nvme_tcp_init_iter(req, WRITE);
929 } else {
930 nvme_tcp_done_send_req(queue);
931 }
932 return 1;
933 }
934 req->offset += ret;
935
936 return -EAGAIN;
937}
938
939static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
940{
941 struct nvme_tcp_queue *queue = req->queue;
942 struct nvme_tcp_data_pdu *pdu = req->pdu;
943 u8 hdgst = nvme_tcp_hdgst_len(queue);
944 int len = sizeof(*pdu) - req->offset + hdgst;
945 int ret;
946
947 if (queue->hdr_digest && !req->offset)
948 nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
949
950 ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
951 offset_in_page(pdu) + req->offset, len,
952 MSG_DONTWAIT | MSG_MORE);
953 if (unlikely(ret <= 0))
954 return ret;
955
956 len -= ret;
957 if (!len) {
958 req->state = NVME_TCP_SEND_DATA;
959 if (queue->data_digest)
960 crypto_ahash_init(queue->snd_hash);
961 if (!req->data_sent)
962 nvme_tcp_init_iter(req, WRITE);
963 return 1;
964 }
965 req->offset += ret;
966
967 return -EAGAIN;
968}
969
970static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
971{
972 struct nvme_tcp_queue *queue = req->queue;
973 int ret;
974 struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_EOR };
975 struct kvec iov = {
976 .iov_base = &req->ddgst + req->offset,
977 .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset
978 };
979
980 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
981 if (unlikely(ret <= 0))
982 return ret;
983
984 if (req->offset + ret == NVME_TCP_DIGEST_LENGTH) {
985 nvme_tcp_done_send_req(queue);
986 return 1;
987 }
988
989 req->offset += ret;
990 return -EAGAIN;
991}
992
993static int nvme_tcp_try_send(struct nvme_tcp_queue *queue)
994{
995 struct nvme_tcp_request *req;
996 int ret = 1;
997
998 if (!queue->request) {
999 queue->request = nvme_tcp_fetch_request(queue);
1000 if (!queue->request)
1001 return 0;
1002 }
1003 req = queue->request;
1004
1005 if (req->state == NVME_TCP_SEND_CMD_PDU) {
1006 ret = nvme_tcp_try_send_cmd_pdu(req);
1007 if (ret <= 0)
1008 goto done;
1009 if (!nvme_tcp_has_inline_data(req))
1010 return ret;
1011 }
1012
1013 if (req->state == NVME_TCP_SEND_H2C_PDU) {
1014 ret = nvme_tcp_try_send_data_pdu(req);
1015 if (ret <= 0)
1016 goto done;
1017 }
1018
1019 if (req->state == NVME_TCP_SEND_DATA) {
1020 ret = nvme_tcp_try_send_data(req);
1021 if (ret <= 0)
1022 goto done;
1023 }
1024
1025 if (req->state == NVME_TCP_SEND_DDGST)
1026 ret = nvme_tcp_try_send_ddgst(req);
1027done:
Sagi Grimberg5ff4e112020-02-25 16:43:23 -08001028 if (ret == -EAGAIN) {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001029 ret = 0;
Sagi Grimberg5ff4e112020-02-25 16:43:23 -08001030 } else if (ret < 0) {
1031 dev_err(queue->ctrl->ctrl.device,
1032 "failed to send request %d\n", ret);
1033 if (ret != -EPIPE && ret != -ECONNRESET)
1034 nvme_tcp_fail_request(queue->request);
1035 nvme_tcp_done_send_req(queue);
1036 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001037 return ret;
1038}
1039
1040static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue)
1041{
Potnuri Bharat Teja10407ec2019-07-08 15:22:00 +05301042 struct socket *sock = queue->sock;
1043 struct sock *sk = sock->sk;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001044 read_descriptor_t rd_desc;
1045 int consumed;
1046
1047 rd_desc.arg.data = queue;
1048 rd_desc.count = 1;
1049 lock_sock(sk);
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001050 queue->nr_cqe = 0;
Potnuri Bharat Teja10407ec2019-07-08 15:22:00 +05301051 consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001052 release_sock(sk);
1053 return consumed;
1054}
1055
1056static void nvme_tcp_io_work(struct work_struct *w)
1057{
1058 struct nvme_tcp_queue *queue =
1059 container_of(w, struct nvme_tcp_queue, io_work);
Wunderlich, Markddef2952019-09-18 23:36:37 +00001060 unsigned long deadline = jiffies + msecs_to_jiffies(1);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001061
1062 do {
1063 bool pending = false;
1064 int result;
1065
1066 result = nvme_tcp_try_send(queue);
Sagi Grimberg5ff4e112020-02-25 16:43:23 -08001067 if (result > 0)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001068 pending = true;
Sagi Grimberg5ff4e112020-02-25 16:43:23 -08001069 else if (unlikely(result < 0))
1070 break;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001071
1072 result = nvme_tcp_try_recv(queue);
1073 if (result > 0)
1074 pending = true;
Sagi Grimberg761ad262020-02-25 16:43:24 -08001075 else if (unlikely(result < 0))
Sagi Grimberg39d06079a2020-03-31 22:44:23 -07001076 return;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001077
1078 if (!pending)
1079 return;
1080
Wunderlich, Markddef2952019-09-18 23:36:37 +00001081 } while (!time_after(jiffies, deadline)); /* quota is exhausted */
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001082
1083 queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
1084}
1085
1086static void nvme_tcp_free_crypto(struct nvme_tcp_queue *queue)
1087{
1088 struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
1089
1090 ahash_request_free(queue->rcv_hash);
1091 ahash_request_free(queue->snd_hash);
1092 crypto_free_ahash(tfm);
1093}
1094
1095static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue *queue)
1096{
1097 struct crypto_ahash *tfm;
1098
1099 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
1100 if (IS_ERR(tfm))
1101 return PTR_ERR(tfm);
1102
1103 queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1104 if (!queue->snd_hash)
1105 goto free_tfm;
1106 ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
1107
1108 queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1109 if (!queue->rcv_hash)
1110 goto free_snd_hash;
1111 ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
1112
1113 return 0;
1114free_snd_hash:
1115 ahash_request_free(queue->snd_hash);
1116free_tfm:
1117 crypto_free_ahash(tfm);
1118 return -ENOMEM;
1119}
1120
1121static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl)
1122{
1123 struct nvme_tcp_request *async = &ctrl->async_req;
1124
1125 page_frag_free(async->pdu);
1126}
1127
1128static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
1129{
1130 struct nvme_tcp_queue *queue = &ctrl->queues[0];
1131 struct nvme_tcp_request *async = &ctrl->async_req;
1132 u8 hdgst = nvme_tcp_hdgst_len(queue);
1133
1134 async->pdu = page_frag_alloc(&queue->pf_cache,
1135 sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
1136 GFP_KERNEL | __GFP_ZERO);
1137 if (!async->pdu)
1138 return -ENOMEM;
1139
1140 async->queue = &ctrl->queues[0];
1141 return 0;
1142}
1143
1144static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
1145{
1146 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1147 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1148
1149 if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
1150 return;
1151
1152 if (queue->hdr_digest || queue->data_digest)
1153 nvme_tcp_free_crypto(queue);
1154
1155 sock_release(queue->sock);
1156 kfree(queue->pdu);
1157}
1158
1159static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
1160{
1161 struct nvme_tcp_icreq_pdu *icreq;
1162 struct nvme_tcp_icresp_pdu *icresp;
1163 struct msghdr msg = {};
1164 struct kvec iov;
1165 bool ctrl_hdgst, ctrl_ddgst;
1166 int ret;
1167
1168 icreq = kzalloc(sizeof(*icreq), GFP_KERNEL);
1169 if (!icreq)
1170 return -ENOMEM;
1171
1172 icresp = kzalloc(sizeof(*icresp), GFP_KERNEL);
1173 if (!icresp) {
1174 ret = -ENOMEM;
1175 goto free_icreq;
1176 }
1177
1178 icreq->hdr.type = nvme_tcp_icreq;
1179 icreq->hdr.hlen = sizeof(*icreq);
1180 icreq->hdr.pdo = 0;
1181 icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen);
1182 icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
1183 icreq->maxr2t = 0; /* single inflight r2t supported */
1184 icreq->hpda = 0; /* no alignment constraint */
1185 if (queue->hdr_digest)
1186 icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
1187 if (queue->data_digest)
1188 icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
1189
1190 iov.iov_base = icreq;
1191 iov.iov_len = sizeof(*icreq);
1192 ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1193 if (ret < 0)
1194 goto free_icresp;
1195
1196 memset(&msg, 0, sizeof(msg));
1197 iov.iov_base = icresp;
1198 iov.iov_len = sizeof(*icresp);
1199 ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1200 iov.iov_len, msg.msg_flags);
1201 if (ret < 0)
1202 goto free_icresp;
1203
1204 ret = -EINVAL;
1205 if (icresp->hdr.type != nvme_tcp_icresp) {
1206 pr_err("queue %d: bad type returned %d\n",
1207 nvme_tcp_queue_id(queue), icresp->hdr.type);
1208 goto free_icresp;
1209 }
1210
1211 if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) {
1212 pr_err("queue %d: bad pdu length returned %d\n",
1213 nvme_tcp_queue_id(queue), icresp->hdr.plen);
1214 goto free_icresp;
1215 }
1216
1217 if (icresp->pfv != NVME_TCP_PFV_1_0) {
1218 pr_err("queue %d: bad pfv returned %d\n",
1219 nvme_tcp_queue_id(queue), icresp->pfv);
1220 goto free_icresp;
1221 }
1222
1223 ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE);
1224 if ((queue->data_digest && !ctrl_ddgst) ||
1225 (!queue->data_digest && ctrl_ddgst)) {
1226 pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
1227 nvme_tcp_queue_id(queue),
1228 queue->data_digest ? "enabled" : "disabled",
1229 ctrl_ddgst ? "enabled" : "disabled");
1230 goto free_icresp;
1231 }
1232
1233 ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE);
1234 if ((queue->hdr_digest && !ctrl_hdgst) ||
1235 (!queue->hdr_digest && ctrl_hdgst)) {
1236 pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
1237 nvme_tcp_queue_id(queue),
1238 queue->hdr_digest ? "enabled" : "disabled",
1239 ctrl_hdgst ? "enabled" : "disabled");
1240 goto free_icresp;
1241 }
1242
1243 if (icresp->cpda != 0) {
1244 pr_err("queue %d: unsupported cpda returned %d\n",
1245 nvme_tcp_queue_id(queue), icresp->cpda);
1246 goto free_icresp;
1247 }
1248
1249 ret = 0;
1250free_icresp:
1251 kfree(icresp);
1252free_icreq:
1253 kfree(icreq);
1254 return ret;
1255}
1256
Sagi Grimberg40510a62020-02-25 15:53:09 -08001257static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue)
1258{
1259 return nvme_tcp_queue_id(queue) == 0;
1260}
1261
1262static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue)
1263{
1264 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1265 int qid = nvme_tcp_queue_id(queue);
1266
1267 return !nvme_tcp_admin_queue(queue) &&
1268 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT];
1269}
1270
1271static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue)
1272{
1273 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1274 int qid = nvme_tcp_queue_id(queue);
1275
1276 return !nvme_tcp_admin_queue(queue) &&
1277 !nvme_tcp_default_queue(queue) &&
1278 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1279 ctrl->io_queues[HCTX_TYPE_READ];
1280}
1281
1282static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue)
1283{
1284 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1285 int qid = nvme_tcp_queue_id(queue);
1286
1287 return !nvme_tcp_admin_queue(queue) &&
1288 !nvme_tcp_default_queue(queue) &&
1289 !nvme_tcp_read_queue(queue) &&
1290 qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1291 ctrl->io_queues[HCTX_TYPE_READ] +
1292 ctrl->io_queues[HCTX_TYPE_POLL];
1293}
1294
1295static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue)
1296{
1297 struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1298 int qid = nvme_tcp_queue_id(queue);
1299 int n = 0;
1300
1301 if (nvme_tcp_default_queue(queue))
1302 n = qid - 1;
1303 else if (nvme_tcp_read_queue(queue))
1304 n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] - 1;
1305 else if (nvme_tcp_poll_queue(queue))
1306 n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] -
1307 ctrl->io_queues[HCTX_TYPE_READ] - 1;
1308 queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
1309}
1310
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001311static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl,
1312 int qid, size_t queue_size)
1313{
1314 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1315 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
Sagi Grimberg40510a62020-02-25 15:53:09 -08001316 int ret, opt, rcv_pdu_size;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001317
1318 queue->ctrl = ctrl;
1319 INIT_LIST_HEAD(&queue->send_list);
1320 spin_lock_init(&queue->lock);
1321 INIT_WORK(&queue->io_work, nvme_tcp_io_work);
1322 queue->queue_size = queue_size;
1323
1324 if (qid > 0)
Israel Rukshin9924b032019-08-18 12:08:53 +03001325 queue->cmnd_capsule_len = nctrl->ioccsz * 16;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001326 else
1327 queue->cmnd_capsule_len = sizeof(struct nvme_command) +
1328 NVME_TCP_ADMIN_CCSZ;
1329
1330 ret = sock_create(ctrl->addr.ss_family, SOCK_STREAM,
1331 IPPROTO_TCP, &queue->sock);
1332 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001333 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001334 "failed to create socket: %d\n", ret);
1335 return ret;
1336 }
1337
1338 /* Single syn retry */
1339 opt = 1;
1340 ret = kernel_setsockopt(queue->sock, IPPROTO_TCP, TCP_SYNCNT,
1341 (char *)&opt, sizeof(opt));
1342 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001343 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001344 "failed to set TCP_SYNCNT sock opt %d\n", ret);
1345 goto err_sock;
1346 }
1347
1348 /* Set TCP no delay */
1349 opt = 1;
1350 ret = kernel_setsockopt(queue->sock, IPPROTO_TCP,
1351 TCP_NODELAY, (char *)&opt, sizeof(opt));
1352 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001353 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001354 "failed to set TCP_NODELAY sock opt %d\n", ret);
1355 goto err_sock;
1356 }
1357
1358 /*
1359 * Cleanup whatever is sitting in the TCP transmit queue on socket
1360 * close. This is done to prevent stale data from being sent should
1361 * the network connection be restored before TCP times out.
1362 */
Christoph Hellwigc4335942020-05-28 07:12:10 +02001363 sock_no_linger(queue->sock->sk);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001364
Christoph Hellwig6e434962020-05-28 07:12:11 +02001365 if (so_priority > 0)
1366 sock_set_priority(queue->sock->sk, so_priority);
Wunderlich, Mark9912ade2020-01-16 00:46:12 +00001367
Israel Rukshinbb139852019-08-18 12:08:54 +03001368 /* Set socket type of service */
1369 if (nctrl->opts->tos >= 0) {
1370 opt = nctrl->opts->tos;
1371 ret = kernel_setsockopt(queue->sock, SOL_IP, IP_TOS,
1372 (char *)&opt, sizeof(opt));
1373 if (ret) {
1374 dev_err(nctrl->device,
1375 "failed to set IP_TOS sock opt %d\n", ret);
1376 goto err_sock;
1377 }
1378 }
1379
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001380 queue->sock->sk->sk_allocation = GFP_ATOMIC;
Sagi Grimberg40510a62020-02-25 15:53:09 -08001381 nvme_tcp_set_queue_io_cpu(queue);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001382 queue->request = NULL;
1383 queue->data_remaining = 0;
1384 queue->ddgst_remaining = 0;
1385 queue->pdu_remaining = 0;
1386 queue->pdu_offset = 0;
1387 sk_set_memalloc(queue->sock->sk);
1388
Israel Rukshin9924b032019-08-18 12:08:53 +03001389 if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001390 ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr,
1391 sizeof(ctrl->src_addr));
1392 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001393 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001394 "failed to bind queue %d socket %d\n",
1395 qid, ret);
1396 goto err_sock;
1397 }
1398 }
1399
1400 queue->hdr_digest = nctrl->opts->hdr_digest;
1401 queue->data_digest = nctrl->opts->data_digest;
1402 if (queue->hdr_digest || queue->data_digest) {
1403 ret = nvme_tcp_alloc_crypto(queue);
1404 if (ret) {
Israel Rukshin9924b032019-08-18 12:08:53 +03001405 dev_err(nctrl->device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001406 "failed to allocate queue %d crypto\n", qid);
1407 goto err_sock;
1408 }
1409 }
1410
1411 rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) +
1412 nvme_tcp_hdgst_len(queue);
1413 queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL);
1414 if (!queue->pdu) {
1415 ret = -ENOMEM;
1416 goto err_crypto;
1417 }
1418
Israel Rukshin9924b032019-08-18 12:08:53 +03001419 dev_dbg(nctrl->device, "connecting queue %d\n",
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001420 nvme_tcp_queue_id(queue));
1421
1422 ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr,
1423 sizeof(ctrl->addr), 0);
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 connect socket: %d\n", ret);
1427 goto err_rcv_pdu;
1428 }
1429
1430 ret = nvme_tcp_init_connection(queue);
1431 if (ret)
1432 goto err_init_connect;
1433
1434 queue->rd_enabled = true;
1435 set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags);
1436 nvme_tcp_init_recv_ctx(queue);
1437
1438 write_lock_bh(&queue->sock->sk->sk_callback_lock);
1439 queue->sock->sk->sk_user_data = queue;
1440 queue->state_change = queue->sock->sk->sk_state_change;
1441 queue->data_ready = queue->sock->sk->sk_data_ready;
1442 queue->write_space = queue->sock->sk->sk_write_space;
1443 queue->sock->sk->sk_data_ready = nvme_tcp_data_ready;
1444 queue->sock->sk->sk_state_change = nvme_tcp_state_change;
1445 queue->sock->sk->sk_write_space = nvme_tcp_write_space;
Sebastian Andrzej Siewiorac1c4e12019-10-10 17:34:12 +02001446#ifdef CONFIG_NET_RX_BUSY_POLL
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001447 queue->sock->sk->sk_ll_usec = 1;
Sebastian Andrzej Siewiorac1c4e12019-10-10 17:34:12 +02001448#endif
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001449 write_unlock_bh(&queue->sock->sk->sk_callback_lock);
1450
1451 return 0;
1452
1453err_init_connect:
1454 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1455err_rcv_pdu:
1456 kfree(queue->pdu);
1457err_crypto:
1458 if (queue->hdr_digest || queue->data_digest)
1459 nvme_tcp_free_crypto(queue);
1460err_sock:
1461 sock_release(queue->sock);
1462 queue->sock = NULL;
1463 return ret;
1464}
1465
1466static void nvme_tcp_restore_sock_calls(struct nvme_tcp_queue *queue)
1467{
1468 struct socket *sock = queue->sock;
1469
1470 write_lock_bh(&sock->sk->sk_callback_lock);
1471 sock->sk->sk_user_data = NULL;
1472 sock->sk->sk_data_ready = queue->data_ready;
1473 sock->sk->sk_state_change = queue->state_change;
1474 sock->sk->sk_write_space = queue->write_space;
1475 write_unlock_bh(&sock->sk->sk_callback_lock);
1476}
1477
1478static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
1479{
1480 kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1481 nvme_tcp_restore_sock_calls(queue);
1482 cancel_work_sync(&queue->io_work);
1483}
1484
1485static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
1486{
1487 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1488 struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1489
1490 if (!test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
1491 return;
1492
1493 __nvme_tcp_stop_queue(queue);
1494}
1495
1496static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
1497{
1498 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1499 int ret;
1500
1501 if (idx)
Sagi Grimberg26c68222018-12-14 11:06:08 -08001502 ret = nvmf_connect_io_queue(nctrl, idx, false);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001503 else
1504 ret = nvmf_connect_admin_queue(nctrl);
1505
1506 if (!ret) {
1507 set_bit(NVME_TCP_Q_LIVE, &ctrl->queues[idx].flags);
1508 } else {
Sagi Grimbergf34e2582019-04-29 16:25:48 -07001509 if (test_bit(NVME_TCP_Q_ALLOCATED, &ctrl->queues[idx].flags))
1510 __nvme_tcp_stop_queue(&ctrl->queues[idx]);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001511 dev_err(nctrl->device,
1512 "failed to connect queue: %d ret=%d\n", idx, ret);
1513 }
1514 return ret;
1515}
1516
1517static struct blk_mq_tag_set *nvme_tcp_alloc_tagset(struct nvme_ctrl *nctrl,
1518 bool admin)
1519{
1520 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1521 struct blk_mq_tag_set *set;
1522 int ret;
1523
1524 if (admin) {
1525 set = &ctrl->admin_tag_set;
1526 memset(set, 0, sizeof(*set));
1527 set->ops = &nvme_tcp_admin_mq_ops;
1528 set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
1529 set->reserved_tags = 2; /* connect + keep-alive */
1530 set->numa_node = NUMA_NO_NODE;
1531 set->cmd_size = sizeof(struct nvme_tcp_request);
1532 set->driver_data = ctrl;
1533 set->nr_hw_queues = 1;
1534 set->timeout = ADMIN_TIMEOUT;
1535 } else {
1536 set = &ctrl->tag_set;
1537 memset(set, 0, sizeof(*set));
1538 set->ops = &nvme_tcp_mq_ops;
1539 set->queue_depth = nctrl->sqsize + 1;
1540 set->reserved_tags = 1; /* fabric connect */
1541 set->numa_node = NUMA_NO_NODE;
1542 set->flags = BLK_MQ_F_SHOULD_MERGE;
1543 set->cmd_size = sizeof(struct nvme_tcp_request);
1544 set->driver_data = ctrl;
1545 set->nr_hw_queues = nctrl->queue_count - 1;
1546 set->timeout = NVME_IO_TIMEOUT;
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001547 set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001548 }
1549
1550 ret = blk_mq_alloc_tag_set(set);
1551 if (ret)
1552 return ERR_PTR(ret);
1553
1554 return set;
1555}
1556
1557static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl)
1558{
1559 if (to_tcp_ctrl(ctrl)->async_req.pdu) {
1560 nvme_tcp_free_async_req(to_tcp_ctrl(ctrl));
1561 to_tcp_ctrl(ctrl)->async_req.pdu = NULL;
1562 }
1563
1564 nvme_tcp_free_queue(ctrl, 0);
1565}
1566
1567static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl)
1568{
1569 int i;
1570
1571 for (i = 1; i < ctrl->queue_count; i++)
1572 nvme_tcp_free_queue(ctrl, i);
1573}
1574
1575static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl)
1576{
1577 int i;
1578
1579 for (i = 1; i < ctrl->queue_count; i++)
1580 nvme_tcp_stop_queue(ctrl, i);
1581}
1582
1583static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl)
1584{
1585 int i, ret = 0;
1586
1587 for (i = 1; i < ctrl->queue_count; i++) {
1588 ret = nvme_tcp_start_queue(ctrl, i);
1589 if (ret)
1590 goto out_stop_queues;
1591 }
1592
1593 return 0;
1594
1595out_stop_queues:
1596 for (i--; i >= 1; i--)
1597 nvme_tcp_stop_queue(ctrl, i);
1598 return ret;
1599}
1600
1601static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
1602{
1603 int ret;
1604
1605 ret = nvme_tcp_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
1606 if (ret)
1607 return ret;
1608
1609 ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
1610 if (ret)
1611 goto out_free_queue;
1612
1613 return 0;
1614
1615out_free_queue:
1616 nvme_tcp_free_queue(ctrl, 0);
1617 return ret;
1618}
1619
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001620static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001621{
1622 int i, ret;
1623
1624 for (i = 1; i < ctrl->queue_count; i++) {
1625 ret = nvme_tcp_alloc_queue(ctrl, i,
1626 ctrl->sqsize + 1);
1627 if (ret)
1628 goto out_free_queues;
1629 }
1630
1631 return 0;
1632
1633out_free_queues:
1634 for (i--; i >= 1; i--)
1635 nvme_tcp_free_queue(ctrl, i);
1636
1637 return ret;
1638}
1639
1640static unsigned int nvme_tcp_nr_io_queues(struct nvme_ctrl *ctrl)
1641{
Sagi Grimberg873946f2018-12-11 23:38:57 -08001642 unsigned int nr_io_queues;
1643
1644 nr_io_queues = min(ctrl->opts->nr_io_queues, num_online_cpus());
1645 nr_io_queues += min(ctrl->opts->nr_write_queues, num_online_cpus());
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001646 nr_io_queues += min(ctrl->opts->nr_poll_queues, num_online_cpus());
Sagi Grimberg873946f2018-12-11 23:38:57 -08001647
1648 return nr_io_queues;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001649}
1650
Sagi Grimberg64861992019-05-28 22:49:05 -07001651static void nvme_tcp_set_io_queues(struct nvme_ctrl *nctrl,
1652 unsigned int nr_io_queues)
1653{
1654 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1655 struct nvmf_ctrl_options *opts = nctrl->opts;
1656
1657 if (opts->nr_write_queues && opts->nr_io_queues < nr_io_queues) {
1658 /*
1659 * separate read/write queues
1660 * hand out dedicated default queues only after we have
1661 * sufficient read queues.
1662 */
1663 ctrl->io_queues[HCTX_TYPE_READ] = opts->nr_io_queues;
1664 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
1665 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1666 min(opts->nr_write_queues, nr_io_queues);
1667 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1668 } else {
1669 /*
1670 * shared read/write queues
1671 * either no write queues were requested, or we don't have
1672 * sufficient queue count to have dedicated default queues.
1673 */
1674 ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1675 min(opts->nr_io_queues, nr_io_queues);
1676 nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1677 }
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07001678
1679 if (opts->nr_poll_queues && nr_io_queues) {
1680 /* map dedicated poll queues only if we have queues left */
1681 ctrl->io_queues[HCTX_TYPE_POLL] =
1682 min(opts->nr_poll_queues, nr_io_queues);
1683 }
Sagi Grimberg64861992019-05-28 22:49:05 -07001684}
1685
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001686static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001687{
1688 unsigned int nr_io_queues;
1689 int ret;
1690
1691 nr_io_queues = nvme_tcp_nr_io_queues(ctrl);
1692 ret = nvme_set_queue_count(ctrl, &nr_io_queues);
1693 if (ret)
1694 return ret;
1695
1696 ctrl->queue_count = nr_io_queues + 1;
1697 if (ctrl->queue_count < 2)
1698 return 0;
1699
1700 dev_info(ctrl->device,
1701 "creating %d I/O queues.\n", nr_io_queues);
1702
Sagi Grimberg64861992019-05-28 22:49:05 -07001703 nvme_tcp_set_io_queues(ctrl, nr_io_queues);
1704
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001705 return __nvme_tcp_alloc_io_queues(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001706}
1707
1708static void nvme_tcp_destroy_io_queues(struct nvme_ctrl *ctrl, bool remove)
1709{
1710 nvme_tcp_stop_io_queues(ctrl);
1711 if (remove) {
Sagi Grimberge85037a2018-12-31 23:58:30 -08001712 blk_cleanup_queue(ctrl->connect_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001713 blk_mq_free_tag_set(ctrl->tagset);
1714 }
1715 nvme_tcp_free_io_queues(ctrl);
1716}
1717
1718static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
1719{
1720 int ret;
1721
Sagi Grimbergefb973b2019-04-24 11:53:19 -07001722 ret = nvme_tcp_alloc_io_queues(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001723 if (ret)
1724 return ret;
1725
1726 if (new) {
1727 ctrl->tagset = nvme_tcp_alloc_tagset(ctrl, false);
1728 if (IS_ERR(ctrl->tagset)) {
1729 ret = PTR_ERR(ctrl->tagset);
1730 goto out_free_io_queues;
1731 }
1732
Sagi Grimberge85037a2018-12-31 23:58:30 -08001733 ctrl->connect_q = blk_mq_init_queue(ctrl->tagset);
1734 if (IS_ERR(ctrl->connect_q)) {
1735 ret = PTR_ERR(ctrl->connect_q);
1736 goto out_free_tag_set;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001737 }
1738 } else {
1739 blk_mq_update_nr_hw_queues(ctrl->tagset,
1740 ctrl->queue_count - 1);
1741 }
1742
1743 ret = nvme_tcp_start_io_queues(ctrl);
1744 if (ret)
1745 goto out_cleanup_connect_q;
1746
1747 return 0;
1748
1749out_cleanup_connect_q:
Sagi Grimberge85037a2018-12-31 23:58:30 -08001750 if (new)
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001751 blk_cleanup_queue(ctrl->connect_q);
1752out_free_tag_set:
1753 if (new)
1754 blk_mq_free_tag_set(ctrl->tagset);
1755out_free_io_queues:
1756 nvme_tcp_free_io_queues(ctrl);
1757 return ret;
1758}
1759
1760static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl *ctrl, bool remove)
1761{
1762 nvme_tcp_stop_queue(ctrl, 0);
1763 if (remove) {
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001764 blk_cleanup_queue(ctrl->admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001765 blk_cleanup_queue(ctrl->fabrics_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001766 blk_mq_free_tag_set(ctrl->admin_tagset);
1767 }
1768 nvme_tcp_free_admin_queue(ctrl);
1769}
1770
1771static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
1772{
1773 int error;
1774
1775 error = nvme_tcp_alloc_admin_queue(ctrl);
1776 if (error)
1777 return error;
1778
1779 if (new) {
1780 ctrl->admin_tagset = nvme_tcp_alloc_tagset(ctrl, true);
1781 if (IS_ERR(ctrl->admin_tagset)) {
1782 error = PTR_ERR(ctrl->admin_tagset);
1783 goto out_free_queue;
1784 }
1785
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001786 ctrl->fabrics_q = blk_mq_init_queue(ctrl->admin_tagset);
1787 if (IS_ERR(ctrl->fabrics_q)) {
1788 error = PTR_ERR(ctrl->fabrics_q);
1789 goto out_free_tagset;
1790 }
1791
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001792 ctrl->admin_q = blk_mq_init_queue(ctrl->admin_tagset);
1793 if (IS_ERR(ctrl->admin_q)) {
1794 error = PTR_ERR(ctrl->admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001795 goto out_cleanup_fabrics_q;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001796 }
1797 }
1798
1799 error = nvme_tcp_start_queue(ctrl, 0);
1800 if (error)
1801 goto out_cleanup_queue;
1802
Sagi Grimbergc0f2f452019-07-22 17:06:53 -07001803 error = nvme_enable_ctrl(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001804 if (error)
1805 goto out_stop_queue;
1806
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001807 blk_mq_unquiesce_queue(ctrl->admin_q);
1808
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001809 error = nvme_init_identify(ctrl);
1810 if (error)
1811 goto out_stop_queue;
1812
1813 return 0;
1814
1815out_stop_queue:
1816 nvme_tcp_stop_queue(ctrl, 0);
1817out_cleanup_queue:
1818 if (new)
1819 blk_cleanup_queue(ctrl->admin_q);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001820out_cleanup_fabrics_q:
1821 if (new)
1822 blk_cleanup_queue(ctrl->fabrics_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001823out_free_tagset:
1824 if (new)
1825 blk_mq_free_tag_set(ctrl->admin_tagset);
1826out_free_queue:
1827 nvme_tcp_free_admin_queue(ctrl);
1828 return error;
1829}
1830
1831static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
1832 bool remove)
1833{
1834 blk_mq_quiesce_queue(ctrl->admin_q);
1835 nvme_tcp_stop_queue(ctrl, 0);
Ming Lei622b8b62019-07-24 11:48:42 +08001836 if (ctrl->admin_tagset) {
Sagi Grimberg7a425892019-04-24 11:53:17 -07001837 blk_mq_tagset_busy_iter(ctrl->admin_tagset,
1838 nvme_cancel_request, ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +08001839 blk_mq_tagset_wait_completed_request(ctrl->admin_tagset);
1840 }
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001841 if (remove)
1842 blk_mq_unquiesce_queue(ctrl->admin_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001843 nvme_tcp_destroy_admin_queue(ctrl, remove);
1844}
1845
1846static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
1847 bool remove)
1848{
1849 if (ctrl->queue_count <= 1)
1850 return;
1851 nvme_stop_queues(ctrl);
1852 nvme_tcp_stop_io_queues(ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +08001853 if (ctrl->tagset) {
Sagi Grimberg7a425892019-04-24 11:53:17 -07001854 blk_mq_tagset_busy_iter(ctrl->tagset,
1855 nvme_cancel_request, ctrl);
Ming Lei622b8b62019-07-24 11:48:42 +08001856 blk_mq_tagset_wait_completed_request(ctrl->tagset);
1857 }
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001858 if (remove)
1859 nvme_start_queues(ctrl);
1860 nvme_tcp_destroy_io_queues(ctrl, remove);
1861}
1862
1863static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl)
1864{
1865 /* If we are resetting/deleting then do nothing */
1866 if (ctrl->state != NVME_CTRL_CONNECTING) {
1867 WARN_ON_ONCE(ctrl->state == NVME_CTRL_NEW ||
1868 ctrl->state == NVME_CTRL_LIVE);
1869 return;
1870 }
1871
1872 if (nvmf_should_reconnect(ctrl)) {
1873 dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
1874 ctrl->opts->reconnect_delay);
1875 queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
1876 ctrl->opts->reconnect_delay * HZ);
1877 } else {
1878 dev_info(ctrl->device, "Removing controller...\n");
1879 nvme_delete_ctrl(ctrl);
1880 }
1881}
1882
1883static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
1884{
1885 struct nvmf_ctrl_options *opts = ctrl->opts;
Colin Ian King312910f2019-09-05 15:34:35 +01001886 int ret;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001887
1888 ret = nvme_tcp_configure_admin_queue(ctrl, new);
1889 if (ret)
1890 return ret;
1891
1892 if (ctrl->icdoff) {
1893 dev_err(ctrl->device, "icdoff is not supported!\n");
1894 goto destroy_admin;
1895 }
1896
1897 if (opts->queue_size > ctrl->sqsize + 1)
1898 dev_warn(ctrl->device,
1899 "queue_size %zu > ctrl sqsize %u, clamping down\n",
1900 opts->queue_size, ctrl->sqsize + 1);
1901
1902 if (ctrl->sqsize + 1 > ctrl->maxcmd) {
1903 dev_warn(ctrl->device,
1904 "sqsize %u > ctrl maxcmd %u, clamping down\n",
1905 ctrl->sqsize + 1, ctrl->maxcmd);
1906 ctrl->sqsize = ctrl->maxcmd - 1;
1907 }
1908
1909 if (ctrl->queue_count > 1) {
1910 ret = nvme_tcp_configure_io_queues(ctrl, new);
1911 if (ret)
1912 goto destroy_admin;
1913 }
1914
1915 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
Israel Rukshinbea54ef2020-03-24 17:29:45 +02001916 /*
1917 * state change failure is ok if we're in DELETING state,
1918 * unless we're during creation of a new controller to
1919 * avoid races with teardown flow.
1920 */
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001921 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
Israel Rukshinbea54ef2020-03-24 17:29:45 +02001922 WARN_ON_ONCE(new);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001923 ret = -EINVAL;
1924 goto destroy_io;
1925 }
1926
1927 nvme_start_ctrl(ctrl);
1928 return 0;
1929
1930destroy_io:
1931 if (ctrl->queue_count > 1)
1932 nvme_tcp_destroy_io_queues(ctrl, new);
1933destroy_admin:
1934 nvme_tcp_stop_queue(ctrl, 0);
1935 nvme_tcp_destroy_admin_queue(ctrl, new);
1936 return ret;
1937}
1938
1939static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
1940{
1941 struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
1942 struct nvme_tcp_ctrl, connect_work);
1943 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
1944
1945 ++ctrl->nr_reconnects;
1946
1947 if (nvme_tcp_setup_ctrl(ctrl, false))
1948 goto requeue;
1949
Colin Ian King56a77d22018-12-14 11:42:43 +00001950 dev_info(ctrl->device, "Successfully reconnected (%d attempt)\n",
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001951 ctrl->nr_reconnects);
1952
1953 ctrl->nr_reconnects = 0;
1954
1955 return;
1956
1957requeue:
1958 dev_info(ctrl->device, "Failed reconnect attempt %d\n",
1959 ctrl->nr_reconnects);
1960 nvme_tcp_reconnect_or_remove(ctrl);
1961}
1962
1963static void nvme_tcp_error_recovery_work(struct work_struct *work)
1964{
1965 struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
1966 struct nvme_tcp_ctrl, err_work);
1967 struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
1968
1969 nvme_stop_keep_alive(ctrl);
1970 nvme_tcp_teardown_io_queues(ctrl, false);
1971 /* unquiesce to fail fast pending requests */
1972 nvme_start_queues(ctrl);
1973 nvme_tcp_teardown_admin_queue(ctrl, false);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001974 blk_mq_unquiesce_queue(ctrl->admin_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001975
1976 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
1977 /* state change failure is ok if we're in DELETING state */
1978 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
1979 return;
1980 }
1981
1982 nvme_tcp_reconnect_or_remove(ctrl);
1983}
1984
1985static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
1986{
Sagi Grimberg794a4cb2019-01-01 00:19:30 -08001987 cancel_work_sync(&to_tcp_ctrl(ctrl)->err_work);
1988 cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
1989
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001990 nvme_tcp_teardown_io_queues(ctrl, shutdown);
Sagi Grimberge7832cb2019-08-02 19:33:59 -07001991 blk_mq_quiesce_queue(ctrl->admin_q);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001992 if (shutdown)
1993 nvme_shutdown_ctrl(ctrl);
1994 else
Sagi Grimbergb5b05042019-07-22 17:06:54 -07001995 nvme_disable_ctrl(ctrl);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08001996 nvme_tcp_teardown_admin_queue(ctrl, shutdown);
1997}
1998
1999static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
2000{
2001 nvme_tcp_teardown_ctrl(ctrl, true);
2002}
2003
2004static void nvme_reset_ctrl_work(struct work_struct *work)
2005{
2006 struct nvme_ctrl *ctrl =
2007 container_of(work, struct nvme_ctrl, reset_work);
2008
2009 nvme_stop_ctrl(ctrl);
2010 nvme_tcp_teardown_ctrl(ctrl, false);
2011
2012 if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2013 /* state change failure is ok if we're in DELETING state */
2014 WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
2015 return;
2016 }
2017
2018 if (nvme_tcp_setup_ctrl(ctrl, false))
2019 goto out_fail;
2020
2021 return;
2022
2023out_fail:
2024 ++ctrl->nr_reconnects;
2025 nvme_tcp_reconnect_or_remove(ctrl);
2026}
2027
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002028static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
2029{
2030 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
2031
2032 if (list_empty(&ctrl->list))
2033 goto free_ctrl;
2034
2035 mutex_lock(&nvme_tcp_ctrl_mutex);
2036 list_del(&ctrl->list);
2037 mutex_unlock(&nvme_tcp_ctrl_mutex);
2038
2039 nvmf_free_options(nctrl->opts);
2040free_ctrl:
2041 kfree(ctrl->queues);
2042 kfree(ctrl);
2043}
2044
2045static void nvme_tcp_set_sg_null(struct nvme_command *c)
2046{
2047 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2048
2049 sg->addr = 0;
2050 sg->length = 0;
2051 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2052 NVME_SGL_FMT_TRANSPORT_A;
2053}
2054
2055static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
2056 struct nvme_command *c, u32 data_len)
2057{
2058 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2059
2060 sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
2061 sg->length = cpu_to_le32(data_len);
2062 sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
2063}
2064
2065static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
2066 u32 data_len)
2067{
2068 struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2069
2070 sg->addr = 0;
2071 sg->length = cpu_to_le32(data_len);
2072 sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2073 NVME_SGL_FMT_TRANSPORT_A;
2074}
2075
2076static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
2077{
2078 struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
2079 struct nvme_tcp_queue *queue = &ctrl->queues[0];
2080 struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
2081 struct nvme_command *cmd = &pdu->cmd;
2082 u8 hdgst = nvme_tcp_hdgst_len(queue);
2083
2084 memset(pdu, 0, sizeof(*pdu));
2085 pdu->hdr.type = nvme_tcp_cmd;
2086 if (queue->hdr_digest)
2087 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2088 pdu->hdr.hlen = sizeof(*pdu);
2089 pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
2090
2091 cmd->common.opcode = nvme_admin_async_event;
2092 cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
2093 cmd->common.flags |= NVME_CMD_SGL_METABUF;
2094 nvme_tcp_set_sg_null(cmd);
2095
2096 ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
2097 ctrl->async_req.offset = 0;
2098 ctrl->async_req.curr_bio = NULL;
2099 ctrl->async_req.data_len = 0;
2100
2101 nvme_tcp_queue_request(&ctrl->async_req);
2102}
2103
2104static enum blk_eh_timer_return
2105nvme_tcp_timeout(struct request *rq, bool reserved)
2106{
2107 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2108 struct nvme_tcp_ctrl *ctrl = req->queue->ctrl;
2109 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2110
Keith Busch92b98e82019-09-05 08:09:33 -06002111 /*
2112 * Restart the timer if a controller reset is already scheduled. Any
2113 * timed out commands would be handled before entering the connecting
2114 * state.
2115 */
2116 if (ctrl->ctrl.state == NVME_CTRL_RESETTING)
2117 return BLK_EH_RESET_TIMER;
2118
Sagi Grimberg39d57752019-01-08 01:01:30 -08002119 dev_warn(ctrl->ctrl.device,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002120 "queue %d: timeout request %#x type %d\n",
Sagi Grimberg39d57752019-01-08 01:01:30 -08002121 nvme_tcp_queue_id(req->queue), rq->tag, pdu->hdr.type);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002122
2123 if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
Sagi Grimberg39d57752019-01-08 01:01:30 -08002124 /*
2125 * Teardown immediately if controller times out while starting
2126 * or we are already started error recovery. all outstanding
2127 * requests are completed on shutdown, so we return BLK_EH_DONE.
2128 */
2129 flush_work(&ctrl->err_work);
2130 nvme_tcp_teardown_io_queues(&ctrl->ctrl, false);
2131 nvme_tcp_teardown_admin_queue(&ctrl->ctrl, false);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002132 return BLK_EH_DONE;
2133 }
2134
Sagi Grimberg39d57752019-01-08 01:01:30 -08002135 dev_warn(ctrl->ctrl.device, "starting error recovery\n");
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002136 nvme_tcp_error_recovery(&ctrl->ctrl);
2137
2138 return BLK_EH_RESET_TIMER;
2139}
2140
2141static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
2142 struct request *rq)
2143{
2144 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2145 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2146 struct nvme_command *c = &pdu->cmd;
2147
2148 c->common.flags |= NVME_CMD_SGL_METABUF;
2149
Sagi Grimberg25e5cb72020-03-23 15:06:30 -07002150 if (!blk_rq_nr_phys_segments(rq))
2151 nvme_tcp_set_sg_null(c);
2152 else if (rq_data_dir(rq) == WRITE &&
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002153 req->data_len <= nvme_tcp_inline_data_size(queue))
2154 nvme_tcp_set_sg_inline(queue, c, req->data_len);
2155 else
2156 nvme_tcp_set_sg_host_data(c, req->data_len);
2157
2158 return 0;
2159}
2160
2161static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
2162 struct request *rq)
2163{
2164 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2165 struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2166 struct nvme_tcp_queue *queue = req->queue;
2167 u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
2168 blk_status_t ret;
2169
2170 ret = nvme_setup_cmd(ns, rq, &pdu->cmd);
2171 if (ret)
2172 return ret;
2173
2174 req->state = NVME_TCP_SEND_CMD_PDU;
2175 req->offset = 0;
2176 req->data_sent = 0;
2177 req->pdu_len = 0;
2178 req->pdu_sent = 0;
Sagi Grimberg25e5cb72020-03-23 15:06:30 -07002179 req->data_len = blk_rq_nr_phys_segments(rq) ?
2180 blk_rq_payload_bytes(rq) : 0;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002181 req->curr_bio = rq->bio;
2182
2183 if (rq_data_dir(rq) == WRITE &&
2184 req->data_len <= nvme_tcp_inline_data_size(queue))
2185 req->pdu_len = req->data_len;
2186 else if (req->curr_bio)
2187 nvme_tcp_init_iter(req, READ);
2188
2189 pdu->hdr.type = nvme_tcp_cmd;
2190 pdu->hdr.flags = 0;
2191 if (queue->hdr_digest)
2192 pdu->hdr.flags |= NVME_TCP_F_HDGST;
2193 if (queue->data_digest && req->pdu_len) {
2194 pdu->hdr.flags |= NVME_TCP_F_DDGST;
2195 ddgst = nvme_tcp_ddgst_len(queue);
2196 }
2197 pdu->hdr.hlen = sizeof(*pdu);
2198 pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
2199 pdu->hdr.plen =
2200 cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
2201
2202 ret = nvme_tcp_map_data(queue, rq);
2203 if (unlikely(ret)) {
Max Gurtovoy28a4cac2019-10-13 19:57:38 +03002204 nvme_cleanup_cmd(rq);
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002205 dev_err(queue->ctrl->ctrl.device,
2206 "Failed to map data (%d)\n", ret);
2207 return ret;
2208 }
2209
2210 return 0;
2211}
2212
2213static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
2214 const struct blk_mq_queue_data *bd)
2215{
2216 struct nvme_ns *ns = hctx->queue->queuedata;
2217 struct nvme_tcp_queue *queue = hctx->driver_data;
2218 struct request *rq = bd->rq;
2219 struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2220 bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
2221 blk_status_t ret;
2222
2223 if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2224 return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
2225
2226 ret = nvme_tcp_setup_cmd_pdu(ns, rq);
2227 if (unlikely(ret))
2228 return ret;
2229
2230 blk_mq_start_request(rq);
2231
2232 nvme_tcp_queue_request(req);
2233
2234 return BLK_STS_OK;
2235}
2236
Sagi Grimberg873946f2018-12-11 23:38:57 -08002237static int nvme_tcp_map_queues(struct blk_mq_tag_set *set)
2238{
2239 struct nvme_tcp_ctrl *ctrl = set->driver_data;
Sagi Grimberg64861992019-05-28 22:49:05 -07002240 struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
Sagi Grimberg873946f2018-12-11 23:38:57 -08002241
Sagi Grimberg64861992019-05-28 22:49:05 -07002242 if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
Sagi Grimberg873946f2018-12-11 23:38:57 -08002243 /* separate read/write queues */
2244 set->map[HCTX_TYPE_DEFAULT].nr_queues =
Sagi Grimberg64861992019-05-28 22:49:05 -07002245 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2246 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2247 set->map[HCTX_TYPE_READ].nr_queues =
2248 ctrl->io_queues[HCTX_TYPE_READ];
Sagi Grimberg873946f2018-12-11 23:38:57 -08002249 set->map[HCTX_TYPE_READ].queue_offset =
Sagi Grimberg64861992019-05-28 22:49:05 -07002250 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimberg873946f2018-12-11 23:38:57 -08002251 } else {
Sagi Grimberg64861992019-05-28 22:49:05 -07002252 /* shared read/write queues */
Sagi Grimberg873946f2018-12-11 23:38:57 -08002253 set->map[HCTX_TYPE_DEFAULT].nr_queues =
Sagi Grimberg64861992019-05-28 22:49:05 -07002254 ctrl->io_queues[HCTX_TYPE_DEFAULT];
2255 set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2256 set->map[HCTX_TYPE_READ].nr_queues =
2257 ctrl->io_queues[HCTX_TYPE_DEFAULT];
Sagi Grimberg873946f2018-12-11 23:38:57 -08002258 set->map[HCTX_TYPE_READ].queue_offset = 0;
2259 }
2260 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
2261 blk_mq_map_queues(&set->map[HCTX_TYPE_READ]);
Sagi Grimberg64861992019-05-28 22:49:05 -07002262
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002263 if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
2264 /* map dedicated poll queues only if we have queues left */
2265 set->map[HCTX_TYPE_POLL].nr_queues =
2266 ctrl->io_queues[HCTX_TYPE_POLL];
2267 set->map[HCTX_TYPE_POLL].queue_offset =
2268 ctrl->io_queues[HCTX_TYPE_DEFAULT] +
2269 ctrl->io_queues[HCTX_TYPE_READ];
2270 blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
2271 }
2272
Sagi Grimberg64861992019-05-28 22:49:05 -07002273 dev_info(ctrl->ctrl.device,
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002274 "mapped %d/%d/%d default/read/poll queues.\n",
Sagi Grimberg64861992019-05-28 22:49:05 -07002275 ctrl->io_queues[HCTX_TYPE_DEFAULT],
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002276 ctrl->io_queues[HCTX_TYPE_READ],
2277 ctrl->io_queues[HCTX_TYPE_POLL]);
Sagi Grimberg64861992019-05-28 22:49:05 -07002278
Sagi Grimberg873946f2018-12-11 23:38:57 -08002279 return 0;
2280}
2281
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002282static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx)
2283{
2284 struct nvme_tcp_queue *queue = hctx->driver_data;
2285 struct sock *sk = queue->sock->sk;
2286
Sagi Grimbergf86e5bf2020-03-23 16:43:52 -07002287 if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2288 return 0;
2289
Eric Dumazet3f926af2019-10-23 22:44:51 -07002290 if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue))
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002291 sk_busy_loop(sk, true);
2292 nvme_tcp_try_recv(queue);
2293 return queue->nr_cqe;
2294}
2295
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002296static struct blk_mq_ops nvme_tcp_mq_ops = {
2297 .queue_rq = nvme_tcp_queue_rq,
2298 .complete = nvme_complete_rq,
2299 .init_request = nvme_tcp_init_request,
2300 .exit_request = nvme_tcp_exit_request,
2301 .init_hctx = nvme_tcp_init_hctx,
2302 .timeout = nvme_tcp_timeout,
Sagi Grimberg873946f2018-12-11 23:38:57 -08002303 .map_queues = nvme_tcp_map_queues,
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002304 .poll = nvme_tcp_poll,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002305};
2306
2307static struct blk_mq_ops nvme_tcp_admin_mq_ops = {
2308 .queue_rq = nvme_tcp_queue_rq,
2309 .complete = nvme_complete_rq,
2310 .init_request = nvme_tcp_init_request,
2311 .exit_request = nvme_tcp_exit_request,
2312 .init_hctx = nvme_tcp_init_admin_hctx,
2313 .timeout = nvme_tcp_timeout,
2314};
2315
2316static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
2317 .name = "tcp",
2318 .module = THIS_MODULE,
2319 .flags = NVME_F_FABRICS,
2320 .reg_read32 = nvmf_reg_read32,
2321 .reg_read64 = nvmf_reg_read64,
2322 .reg_write32 = nvmf_reg_write32,
2323 .free_ctrl = nvme_tcp_free_ctrl,
2324 .submit_async_event = nvme_tcp_submit_async_event,
2325 .delete_ctrl = nvme_tcp_delete_ctrl,
2326 .get_address = nvmf_get_address,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002327};
2328
2329static bool
2330nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
2331{
2332 struct nvme_tcp_ctrl *ctrl;
2333 bool found = false;
2334
2335 mutex_lock(&nvme_tcp_ctrl_mutex);
2336 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
2337 found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2338 if (found)
2339 break;
2340 }
2341 mutex_unlock(&nvme_tcp_ctrl_mutex);
2342
2343 return found;
2344}
2345
2346static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
2347 struct nvmf_ctrl_options *opts)
2348{
2349 struct nvme_tcp_ctrl *ctrl;
2350 int ret;
2351
2352 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2353 if (!ctrl)
2354 return ERR_PTR(-ENOMEM);
2355
2356 INIT_LIST_HEAD(&ctrl->list);
2357 ctrl->ctrl.opts = opts;
Sagi Grimberg1a9460c2019-07-03 14:08:04 -07002358 ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2359 opts->nr_poll_queues + 1;
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002360 ctrl->ctrl.sqsize = opts->queue_size - 1;
2361 ctrl->ctrl.kato = opts->kato;
2362
2363 INIT_DELAYED_WORK(&ctrl->connect_work,
2364 nvme_tcp_reconnect_ctrl_work);
2365 INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
2366 INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
2367
2368 if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2369 opts->trsvcid =
2370 kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
2371 if (!opts->trsvcid) {
2372 ret = -ENOMEM;
2373 goto out_free_ctrl;
2374 }
2375 opts->mask |= NVMF_OPT_TRSVCID;
2376 }
2377
2378 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2379 opts->traddr, opts->trsvcid, &ctrl->addr);
2380 if (ret) {
2381 pr_err("malformed address passed: %s:%s\n",
2382 opts->traddr, opts->trsvcid);
2383 goto out_free_ctrl;
2384 }
2385
2386 if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2387 ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2388 opts->host_traddr, NULL, &ctrl->src_addr);
2389 if (ret) {
2390 pr_err("malformed src address passed: %s\n",
2391 opts->host_traddr);
2392 goto out_free_ctrl;
2393 }
2394 }
2395
2396 if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
2397 ret = -EALREADY;
2398 goto out_free_ctrl;
2399 }
2400
Sagi Grimberg873946f2018-12-11 23:38:57 -08002401 ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002402 GFP_KERNEL);
2403 if (!ctrl->queues) {
2404 ret = -ENOMEM;
2405 goto out_free_ctrl;
2406 }
2407
2408 ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
2409 if (ret)
2410 goto out_kfree_queues;
2411
2412 if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2413 WARN_ON_ONCE(1);
2414 ret = -EINTR;
2415 goto out_uninit_ctrl;
2416 }
2417
2418 ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
2419 if (ret)
2420 goto out_uninit_ctrl;
2421
2422 dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
2423 ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2424
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002425 mutex_lock(&nvme_tcp_ctrl_mutex);
2426 list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
2427 mutex_unlock(&nvme_tcp_ctrl_mutex);
2428
2429 return &ctrl->ctrl;
2430
2431out_uninit_ctrl:
2432 nvme_uninit_ctrl(&ctrl->ctrl);
2433 nvme_put_ctrl(&ctrl->ctrl);
2434 if (ret > 0)
2435 ret = -EIO;
2436 return ERR_PTR(ret);
2437out_kfree_queues:
2438 kfree(ctrl->queues);
2439out_free_ctrl:
2440 kfree(ctrl);
2441 return ERR_PTR(ret);
2442}
2443
2444static struct nvmf_transport_ops nvme_tcp_transport = {
2445 .name = "tcp",
2446 .module = THIS_MODULE,
2447 .required_opts = NVMF_OPT_TRADDR,
2448 .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2449 NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
Sagi Grimberg873946f2018-12-11 23:38:57 -08002450 NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
Israel Rukshinbb139852019-08-18 12:08:54 +03002451 NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2452 NVMF_OPT_TOS,
Sagi Grimberg3f2304f2018-12-03 17:52:17 -08002453 .create_ctrl = nvme_tcp_create_ctrl,
2454};
2455
2456static int __init nvme_tcp_init_module(void)
2457{
2458 nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq",
2459 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2460 if (!nvme_tcp_wq)
2461 return -ENOMEM;
2462
2463 nvmf_register_transport(&nvme_tcp_transport);
2464 return 0;
2465}
2466
2467static void __exit nvme_tcp_cleanup_module(void)
2468{
2469 struct nvme_tcp_ctrl *ctrl;
2470
2471 nvmf_unregister_transport(&nvme_tcp_transport);
2472
2473 mutex_lock(&nvme_tcp_ctrl_mutex);
2474 list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
2475 nvme_delete_ctrl(&ctrl->ctrl);
2476 mutex_unlock(&nvme_tcp_ctrl_mutex);
2477 flush_workqueue(nvme_delete_wq);
2478
2479 destroy_workqueue(nvme_tcp_wq);
2480}
2481
2482module_init(nvme_tcp_init_module);
2483module_exit(nvme_tcp_cleanup_module);
2484
2485MODULE_LICENSE("GPL v2");