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