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