xref: /linux/drivers/nvme/host/tcp.c (revision 09d7ff0694ea133c50ad905fd6e548c13f8af458)
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/crc32.h>
12 #include <linux/nvme-tcp.h>
13 #include <linux/nvme-keyring.h>
14 #include <net/sock.h>
15 #include <net/tcp.h>
16 #include <net/tls.h>
17 #include <net/tls_prot.h>
18 #include <net/handshake.h>
19 #include <linux/blk-mq.h>
20 #include <net/busy_poll.h>
21 #include <trace/events/sock.h>
22 
23 #include "nvme.h"
24 #include "fabrics.h"
25 
26 struct nvme_tcp_queue;
27 
28 /* Define the socket priority to use for connections were it is desirable
29  * that the NIC consider performing optimized packet processing or filtering.
30  * A non-zero value being sufficient to indicate general consideration of any
31  * possible optimization.  Making it a module param allows for alternative
32  * values that may be unique for some NIC implementations.
33  */
34 static int so_priority;
35 module_param(so_priority, int, 0644);
36 MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority");
37 
38 /*
39  * Use the unbound workqueue for nvme_tcp_wq, then we can set the cpu affinity
40  * from sysfs.
41  */
42 static bool wq_unbound;
43 module_param(wq_unbound, bool, 0644);
44 MODULE_PARM_DESC(wq_unbound, "Use unbound workqueue for nvme-tcp IO context (default false)");
45 
46 /*
47  * TLS handshake timeout
48  */
49 static int tls_handshake_timeout = 10;
50 #ifdef CONFIG_NVME_TCP_TLS
51 module_param(tls_handshake_timeout, int, 0644);
52 MODULE_PARM_DESC(tls_handshake_timeout,
53 		 "nvme TLS handshake timeout in seconds (default 10)");
54 #endif
55 
56 static atomic_t nvme_tcp_cpu_queues[NR_CPUS];
57 
58 #ifdef CONFIG_DEBUG_LOCK_ALLOC
59 /* lockdep can detect a circular dependency of the form
60  *   sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock
61  * because dependencies are tracked for both nvme-tcp and user contexts. Using
62  * a separate class prevents lockdep from conflating nvme-tcp socket use with
63  * user-space socket API use.
64  */
65 static struct lock_class_key nvme_tcp_sk_key[2];
66 static struct lock_class_key nvme_tcp_slock_key[2];
67 
68 static void nvme_tcp_reclassify_socket(struct socket *sock)
69 {
70 	struct sock *sk = sock->sk;
71 
72 	if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
73 		return;
74 
75 	switch (sk->sk_family) {
76 	case AF_INET:
77 		sock_lock_init_class_and_name(sk, "slock-AF_INET-NVME",
78 					      &nvme_tcp_slock_key[0],
79 					      "sk_lock-AF_INET-NVME",
80 					      &nvme_tcp_sk_key[0]);
81 		break;
82 	case AF_INET6:
83 		sock_lock_init_class_and_name(sk, "slock-AF_INET6-NVME",
84 					      &nvme_tcp_slock_key[1],
85 					      "sk_lock-AF_INET6-NVME",
86 					      &nvme_tcp_sk_key[1]);
87 		break;
88 	default:
89 		WARN_ON_ONCE(1);
90 	}
91 }
92 #else
93 static void nvme_tcp_reclassify_socket(struct socket *sock) { }
94 #endif
95 
96 enum nvme_tcp_send_state {
97 	NVME_TCP_SEND_CMD_PDU = 0,
98 	NVME_TCP_SEND_H2C_PDU,
99 	NVME_TCP_SEND_DATA,
100 	NVME_TCP_SEND_DDGST,
101 };
102 
103 struct nvme_tcp_request {
104 	struct nvme_request	req;
105 	void			*pdu;
106 	struct nvme_tcp_queue	*queue;
107 	u32			data_len;
108 	u32			pdu_len;
109 	u32			pdu_sent;
110 	u32			h2cdata_left;
111 	u32			h2cdata_offset;
112 	u16			ttag;
113 	__le16			status;
114 	struct list_head	entry;
115 	struct llist_node	lentry;
116 	__le32			ddgst;
117 
118 	struct bio		*curr_bio;
119 	struct iov_iter		iter;
120 
121 	/* send state */
122 	size_t			offset;
123 	size_t			data_sent;
124 	enum nvme_tcp_send_state state;
125 };
126 
127 enum nvme_tcp_queue_flags {
128 	NVME_TCP_Q_ALLOCATED	= 0,
129 	NVME_TCP_Q_LIVE		= 1,
130 	NVME_TCP_Q_POLLING	= 2,
131 	NVME_TCP_Q_IO_CPU_SET	= 3,
132 };
133 
134 enum nvme_tcp_recv_state {
135 	NVME_TCP_RECV_PDU = 0,
136 	NVME_TCP_RECV_DATA,
137 	NVME_TCP_RECV_DDGST,
138 };
139 
140 struct nvme_tcp_ctrl;
141 struct nvme_tcp_queue {
142 	struct socket		*sock;
143 	struct work_struct	io_work;
144 	int			io_cpu;
145 
146 	struct mutex		queue_lock;
147 	struct mutex		send_mutex;
148 	struct llist_head	req_list;
149 	struct list_head	send_list;
150 
151 	/* recv state */
152 	void			*pdu;
153 	int			pdu_remaining;
154 	int			pdu_offset;
155 	size_t			data_remaining;
156 	size_t			ddgst_remaining;
157 	unsigned int		nr_cqe;
158 
159 	/* send state */
160 	struct nvme_tcp_request *request;
161 
162 	u32			maxh2cdata;
163 	size_t			cmnd_capsule_len;
164 	struct nvme_tcp_ctrl	*ctrl;
165 	unsigned long		flags;
166 	bool			rd_enabled;
167 
168 	bool			hdr_digest;
169 	bool			data_digest;
170 	bool			tls_enabled;
171 	u32			rcv_crc;
172 	u32			snd_crc;
173 	__le32			exp_ddgst;
174 	__le32			recv_ddgst;
175 	struct completion       tls_complete;
176 	int                     tls_err;
177 	struct page_frag_cache	pf_cache;
178 
179 	void (*state_change)(struct sock *);
180 	void (*data_ready)(struct sock *);
181 	void (*write_space)(struct sock *);
182 };
183 
184 struct nvme_tcp_ctrl {
185 	/* read only in the hot path */
186 	struct nvme_tcp_queue	*queues;
187 	struct blk_mq_tag_set	tag_set;
188 
189 	/* other member variables */
190 	struct list_head	list;
191 	struct blk_mq_tag_set	admin_tag_set;
192 	struct sockaddr_storage addr;
193 	struct sockaddr_storage src_addr;
194 	struct nvme_ctrl	ctrl;
195 
196 	struct work_struct	err_work;
197 	struct delayed_work	connect_work;
198 	struct nvme_tcp_request async_req;
199 	u32			io_queues[HCTX_MAX_TYPES];
200 };
201 
202 static LIST_HEAD(nvme_tcp_ctrl_list);
203 static DEFINE_MUTEX(nvme_tcp_ctrl_mutex);
204 static struct workqueue_struct *nvme_tcp_wq;
205 static const struct blk_mq_ops nvme_tcp_mq_ops;
206 static const struct blk_mq_ops nvme_tcp_admin_mq_ops;
207 static int nvme_tcp_try_send(struct nvme_tcp_queue *queue);
208 
209 static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl)
210 {
211 	return container_of(ctrl, struct nvme_tcp_ctrl, ctrl);
212 }
213 
214 static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
215 {
216 	return queue - queue->ctrl->queues;
217 }
218 
219 static inline bool nvme_tcp_recv_pdu_supported(enum nvme_tcp_pdu_type type)
220 {
221 	switch (type) {
222 	case nvme_tcp_c2h_term:
223 	case nvme_tcp_c2h_data:
224 	case nvme_tcp_r2t:
225 	case nvme_tcp_rsp:
226 		return true;
227 	default:
228 		return false;
229 	}
230 }
231 
232 /*
233  * Check if the queue is TLS encrypted
234  */
235 static inline bool nvme_tcp_queue_tls(struct nvme_tcp_queue *queue)
236 {
237 	if (!IS_ENABLED(CONFIG_NVME_TCP_TLS))
238 		return 0;
239 
240 	return queue->tls_enabled;
241 }
242 
243 /*
244  * Check if TLS is configured for the controller.
245  */
246 static inline bool nvme_tcp_tls_configured(struct nvme_ctrl *ctrl)
247 {
248 	if (!IS_ENABLED(CONFIG_NVME_TCP_TLS))
249 		return 0;
250 
251 	return ctrl->opts->tls || ctrl->opts->concat;
252 }
253 
254 static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue)
255 {
256 	u32 queue_idx = nvme_tcp_queue_id(queue);
257 
258 	if (queue_idx == 0)
259 		return queue->ctrl->admin_tag_set.tags[queue_idx];
260 	return queue->ctrl->tag_set.tags[queue_idx - 1];
261 }
262 
263 static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue)
264 {
265 	return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
266 }
267 
268 static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue)
269 {
270 	return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
271 }
272 
273 static inline void *nvme_tcp_req_cmd_pdu(struct nvme_tcp_request *req)
274 {
275 	return req->pdu;
276 }
277 
278 static inline void *nvme_tcp_req_data_pdu(struct nvme_tcp_request *req)
279 {
280 	/* use the pdu space in the back for the data pdu */
281 	return req->pdu + sizeof(struct nvme_tcp_cmd_pdu) -
282 		sizeof(struct nvme_tcp_data_pdu);
283 }
284 
285 static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_request *req)
286 {
287 	if (nvme_is_fabrics(req->req.cmd))
288 		return NVME_TCP_ADMIN_CCSZ;
289 	return req->queue->cmnd_capsule_len - sizeof(struct nvme_command);
290 }
291 
292 static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req)
293 {
294 	return req == &req->queue->ctrl->async_req;
295 }
296 
297 static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req)
298 {
299 	struct request *rq;
300 
301 	if (unlikely(nvme_tcp_async_req(req)))
302 		return false; /* async events don't have a request */
303 
304 	rq = blk_mq_rq_from_pdu(req);
305 
306 	return rq_data_dir(rq) == WRITE && req->data_len &&
307 		req->data_len <= nvme_tcp_inline_data_size(req);
308 }
309 
310 static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req)
311 {
312 	return req->iter.bvec->bv_page;
313 }
314 
315 static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req)
316 {
317 	return req->iter.bvec->bv_offset + req->iter.iov_offset;
318 }
319 
320 static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req)
321 {
322 	return min_t(size_t, iov_iter_single_seg_count(&req->iter),
323 			req->pdu_len - req->pdu_sent);
324 }
325 
326 static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req)
327 {
328 	return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ?
329 			req->pdu_len - req->pdu_sent : 0;
330 }
331 
332 static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
333 		int len)
334 {
335 	return nvme_tcp_pdu_data_left(req) <= len;
336 }
337 
338 static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
339 		unsigned int dir)
340 {
341 	struct request *rq = blk_mq_rq_from_pdu(req);
342 	struct bio_vec *vec;
343 	unsigned int size;
344 	int nr_bvec;
345 	size_t offset;
346 
347 	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
348 		vec = &rq->special_vec;
349 		nr_bvec = 1;
350 		size = blk_rq_payload_bytes(rq);
351 		offset = 0;
352 	} else {
353 		struct bio *bio = req->curr_bio;
354 		struct bvec_iter bi;
355 		struct bio_vec bv;
356 
357 		vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
358 		nr_bvec = 0;
359 		bio_for_each_bvec(bv, bio, bi) {
360 			nr_bvec++;
361 		}
362 		size = bio->bi_iter.bi_size;
363 		offset = bio->bi_iter.bi_bvec_done;
364 	}
365 
366 	iov_iter_bvec(&req->iter, dir, vec, nr_bvec, size);
367 	req->iter.iov_offset = offset;
368 }
369 
370 static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req,
371 		int len)
372 {
373 	req->data_sent += len;
374 	req->pdu_sent += len;
375 	iov_iter_advance(&req->iter, len);
376 	if (!iov_iter_count(&req->iter) &&
377 	    req->data_sent < req->data_len) {
378 		req->curr_bio = req->curr_bio->bi_next;
379 		nvme_tcp_init_iter(req, ITER_SOURCE);
380 	}
381 }
382 
383 static inline void nvme_tcp_send_all(struct nvme_tcp_queue *queue)
384 {
385 	int ret;
386 
387 	/* drain the send queue as much as we can... */
388 	do {
389 		ret = nvme_tcp_try_send(queue);
390 	} while (ret > 0);
391 }
392 
393 static inline bool nvme_tcp_queue_has_pending(struct nvme_tcp_queue *queue)
394 {
395 	return !list_empty(&queue->send_list) ||
396 		!llist_empty(&queue->req_list);
397 }
398 
399 static inline bool nvme_tcp_queue_more(struct nvme_tcp_queue *queue)
400 {
401 	return !nvme_tcp_queue_tls(queue) &&
402 		nvme_tcp_queue_has_pending(queue);
403 }
404 
405 static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req,
406 		bool sync, bool last)
407 {
408 	struct nvme_tcp_queue *queue = req->queue;
409 	bool empty;
410 
411 	empty = llist_add(&req->lentry, &queue->req_list) &&
412 		list_empty(&queue->send_list) && !queue->request;
413 
414 	/*
415 	 * if we're the first on the send_list and we can try to send
416 	 * directly, otherwise queue io_work. Also, only do that if we
417 	 * are on the same cpu, so we don't introduce contention.
418 	 */
419 	if (queue->io_cpu == raw_smp_processor_id() &&
420 	    sync && empty && mutex_trylock(&queue->send_mutex)) {
421 		nvme_tcp_send_all(queue);
422 		mutex_unlock(&queue->send_mutex);
423 	}
424 
425 	if (last && nvme_tcp_queue_has_pending(queue))
426 		queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
427 }
428 
429 static void nvme_tcp_process_req_list(struct nvme_tcp_queue *queue)
430 {
431 	struct nvme_tcp_request *req;
432 	struct llist_node *node;
433 
434 	for (node = llist_del_all(&queue->req_list); node; node = node->next) {
435 		req = llist_entry(node, struct nvme_tcp_request, lentry);
436 		list_add(&req->entry, &queue->send_list);
437 	}
438 }
439 
440 static inline struct nvme_tcp_request *
441 nvme_tcp_fetch_request(struct nvme_tcp_queue *queue)
442 {
443 	struct nvme_tcp_request *req;
444 
445 	req = list_first_entry_or_null(&queue->send_list,
446 			struct nvme_tcp_request, entry);
447 	if (!req) {
448 		nvme_tcp_process_req_list(queue);
449 		req = list_first_entry_or_null(&queue->send_list,
450 				struct nvme_tcp_request, entry);
451 		if (unlikely(!req))
452 			return NULL;
453 	}
454 
455 	list_del(&req->entry);
456 	return req;
457 }
458 
459 #define NVME_TCP_CRC_SEED (~0)
460 
461 static inline void nvme_tcp_ddgst_update(u32 *crcp,
462 		struct page *page, size_t off, size_t len)
463 {
464 	page += off / PAGE_SIZE;
465 	off %= PAGE_SIZE;
466 	while (len) {
467 		const void *vaddr = kmap_local_page(page);
468 		size_t n = min(len, (size_t)PAGE_SIZE - off);
469 
470 		*crcp = crc32c(*crcp, vaddr + off, n);
471 		kunmap_local(vaddr);
472 		page++;
473 		off = 0;
474 		len -= n;
475 	}
476 }
477 
478 static inline __le32 nvme_tcp_ddgst_final(u32 crc)
479 {
480 	return cpu_to_le32(~crc);
481 }
482 
483 static inline __le32 nvme_tcp_hdgst(const void *pdu, size_t len)
484 {
485 	return cpu_to_le32(~crc32c(NVME_TCP_CRC_SEED, pdu, len));
486 }
487 
488 static inline void nvme_tcp_set_hdgst(void *pdu, size_t len)
489 {
490 	*(__le32 *)(pdu + len) = nvme_tcp_hdgst(pdu, len);
491 }
492 
493 static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue,
494 		void *pdu, size_t pdu_len)
495 {
496 	struct nvme_tcp_hdr *hdr = pdu;
497 	__le32 recv_digest;
498 	__le32 exp_digest;
499 
500 	if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
501 		dev_err(queue->ctrl->ctrl.device,
502 			"queue %d: header digest flag is cleared\n",
503 			nvme_tcp_queue_id(queue));
504 		return -EPROTO;
505 	}
506 
507 	recv_digest = *(__le32 *)(pdu + hdr->hlen);
508 	exp_digest = nvme_tcp_hdgst(pdu, pdu_len);
509 	if (recv_digest != exp_digest) {
510 		dev_err(queue->ctrl->ctrl.device,
511 			"header digest error: recv %#x expected %#x\n",
512 			le32_to_cpu(recv_digest), le32_to_cpu(exp_digest));
513 		return -EIO;
514 	}
515 
516 	return 0;
517 }
518 
519 static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu)
520 {
521 	struct nvme_tcp_hdr *hdr = pdu;
522 	u8 digest_len = nvme_tcp_hdgst_len(queue);
523 	u32 len;
524 
525 	len = le32_to_cpu(hdr->plen) - hdr->hlen -
526 		((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0);
527 
528 	if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
529 		dev_err(queue->ctrl->ctrl.device,
530 			"queue %d: data digest flag is cleared\n",
531 		nvme_tcp_queue_id(queue));
532 		return -EPROTO;
533 	}
534 	queue->rcv_crc = NVME_TCP_CRC_SEED;
535 
536 	return 0;
537 }
538 
539 static void nvme_tcp_exit_request(struct blk_mq_tag_set *set,
540 		struct request *rq, unsigned int hctx_idx)
541 {
542 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
543 
544 	page_frag_free(req->pdu);
545 }
546 
547 static int nvme_tcp_init_request(struct blk_mq_tag_set *set,
548 		struct request *rq, unsigned int hctx_idx,
549 		unsigned int numa_node)
550 {
551 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data);
552 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
553 	struct nvme_tcp_cmd_pdu *pdu;
554 	int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
555 	struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx];
556 	u8 hdgst = nvme_tcp_hdgst_len(queue);
557 
558 	req->pdu = page_frag_alloc(&queue->pf_cache,
559 		sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
560 		GFP_KERNEL | __GFP_ZERO);
561 	if (!req->pdu)
562 		return -ENOMEM;
563 
564 	pdu = req->pdu;
565 	req->queue = queue;
566 	nvme_req(rq)->ctrl = &ctrl->ctrl;
567 	nvme_req(rq)->cmd = &pdu->cmd;
568 
569 	return 0;
570 }
571 
572 static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
573 		unsigned int hctx_idx)
574 {
575 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(data);
576 	struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1];
577 
578 	hctx->driver_data = queue;
579 	return 0;
580 }
581 
582 static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
583 		unsigned int hctx_idx)
584 {
585 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(data);
586 	struct nvme_tcp_queue *queue = &ctrl->queues[0];
587 
588 	hctx->driver_data = queue;
589 	return 0;
590 }
591 
592 static enum nvme_tcp_recv_state
593 nvme_tcp_recv_state(struct nvme_tcp_queue *queue)
594 {
595 	return  (queue->pdu_remaining) ? NVME_TCP_RECV_PDU :
596 		(queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST :
597 		NVME_TCP_RECV_DATA;
598 }
599 
600 static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
601 {
602 	queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) +
603 				nvme_tcp_hdgst_len(queue);
604 	queue->pdu_offset = 0;
605 	queue->data_remaining = -1;
606 	queue->ddgst_remaining = 0;
607 }
608 
609 static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
610 {
611 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
612 		return;
613 
614 	dev_warn(ctrl->device, "starting error recovery\n");
615 	queue_work(nvme_reset_wq, &to_tcp_ctrl(ctrl)->err_work);
616 }
617 
618 static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue,
619 		struct nvme_completion *cqe)
620 {
621 	struct nvme_tcp_request *req;
622 	struct request *rq;
623 
624 	rq = nvme_find_rq(nvme_tcp_tagset(queue), cqe->command_id);
625 	if (!rq) {
626 		dev_err(queue->ctrl->ctrl.device,
627 			"got bad cqe.command_id %#x on queue %d\n",
628 			cqe->command_id, nvme_tcp_queue_id(queue));
629 		nvme_tcp_error_recovery(&queue->ctrl->ctrl);
630 		return -EINVAL;
631 	}
632 
633 	req = blk_mq_rq_to_pdu(rq);
634 	if (req->status == cpu_to_le16(NVME_SC_SUCCESS))
635 		req->status = cqe->status;
636 
637 	if (!nvme_try_complete_req(rq, req->status, cqe->result))
638 		nvme_complete_rq(rq);
639 	queue->nr_cqe++;
640 
641 	return 0;
642 }
643 
644 static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
645 		struct nvme_tcp_data_pdu *pdu)
646 {
647 	struct request *rq;
648 
649 	rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
650 	if (!rq) {
651 		dev_err(queue->ctrl->ctrl.device,
652 			"got bad c2hdata.command_id %#x on queue %d\n",
653 			pdu->command_id, nvme_tcp_queue_id(queue));
654 		return -ENOENT;
655 	}
656 
657 	if (!blk_rq_payload_bytes(rq)) {
658 		dev_err(queue->ctrl->ctrl.device,
659 			"queue %d tag %#x unexpected data\n",
660 			nvme_tcp_queue_id(queue), rq->tag);
661 		return -EIO;
662 	}
663 
664 	queue->data_remaining = le32_to_cpu(pdu->data_length);
665 
666 	if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS &&
667 	    unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) {
668 		dev_err(queue->ctrl->ctrl.device,
669 			"queue %d tag %#x SUCCESS set but not last PDU\n",
670 			nvme_tcp_queue_id(queue), rq->tag);
671 		nvme_tcp_error_recovery(&queue->ctrl->ctrl);
672 		return -EPROTO;
673 	}
674 
675 	return 0;
676 }
677 
678 static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue,
679 		struct nvme_tcp_rsp_pdu *pdu)
680 {
681 	struct nvme_completion *cqe = &pdu->cqe;
682 	int ret = 0;
683 
684 	/*
685 	 * AEN requests are special as they don't time out and can
686 	 * survive any kind of queue freeze and often don't respond to
687 	 * aborts.  We don't even bother to allocate a struct request
688 	 * for them but rather special case them here.
689 	 */
690 	if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue),
691 				     cqe->command_id)))
692 		nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
693 				&cqe->result);
694 	else
695 		ret = nvme_tcp_process_nvme_cqe(queue, cqe);
696 
697 	return ret;
698 }
699 
700 static void nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req)
701 {
702 	struct nvme_tcp_data_pdu *data = nvme_tcp_req_data_pdu(req);
703 	struct nvme_tcp_queue *queue = req->queue;
704 	struct request *rq = blk_mq_rq_from_pdu(req);
705 	u32 h2cdata_sent = req->pdu_len;
706 	u8 hdgst = nvme_tcp_hdgst_len(queue);
707 	u8 ddgst = nvme_tcp_ddgst_len(queue);
708 
709 	req->state = NVME_TCP_SEND_H2C_PDU;
710 	req->offset = 0;
711 	req->pdu_len = min(req->h2cdata_left, queue->maxh2cdata);
712 	req->pdu_sent = 0;
713 	req->h2cdata_left -= req->pdu_len;
714 	req->h2cdata_offset += h2cdata_sent;
715 
716 	memset(data, 0, sizeof(*data));
717 	data->hdr.type = nvme_tcp_h2c_data;
718 	if (!req->h2cdata_left)
719 		data->hdr.flags = NVME_TCP_F_DATA_LAST;
720 	if (queue->hdr_digest)
721 		data->hdr.flags |= NVME_TCP_F_HDGST;
722 	if (queue->data_digest)
723 		data->hdr.flags |= NVME_TCP_F_DDGST;
724 	data->hdr.hlen = sizeof(*data);
725 	data->hdr.pdo = data->hdr.hlen + hdgst;
726 	data->hdr.plen =
727 		cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst);
728 	data->ttag = req->ttag;
729 	data->command_id = nvme_cid(rq);
730 	data->data_offset = cpu_to_le32(req->h2cdata_offset);
731 	data->data_length = cpu_to_le32(req->pdu_len);
732 }
733 
734 static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
735 		struct nvme_tcp_r2t_pdu *pdu)
736 {
737 	struct nvme_tcp_request *req;
738 	struct request *rq;
739 	u32 r2t_length = le32_to_cpu(pdu->r2t_length);
740 	u32 r2t_offset = le32_to_cpu(pdu->r2t_offset);
741 
742 	rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
743 	if (!rq) {
744 		dev_err(queue->ctrl->ctrl.device,
745 			"got bad r2t.command_id %#x on queue %d\n",
746 			pdu->command_id, nvme_tcp_queue_id(queue));
747 		return -ENOENT;
748 	}
749 	req = blk_mq_rq_to_pdu(rq);
750 
751 	if (unlikely(!r2t_length)) {
752 		dev_err(queue->ctrl->ctrl.device,
753 			"req %d r2t len is %u, probably a bug...\n",
754 			rq->tag, r2t_length);
755 		return -EPROTO;
756 	}
757 
758 	if (unlikely(req->data_sent + r2t_length > req->data_len)) {
759 		dev_err(queue->ctrl->ctrl.device,
760 			"req %d r2t len %u exceeded data len %u (%zu sent)\n",
761 			rq->tag, r2t_length, req->data_len, req->data_sent);
762 		return -EPROTO;
763 	}
764 
765 	if (unlikely(r2t_offset < req->data_sent)) {
766 		dev_err(queue->ctrl->ctrl.device,
767 			"req %d unexpected r2t offset %u (expected %zu)\n",
768 			rq->tag, r2t_offset, req->data_sent);
769 		return -EPROTO;
770 	}
771 
772 	req->pdu_len = 0;
773 	req->h2cdata_left = r2t_length;
774 	req->h2cdata_offset = r2t_offset;
775 	req->ttag = pdu->ttag;
776 
777 	nvme_tcp_setup_h2c_data_pdu(req);
778 	nvme_tcp_queue_request(req, false, true);
779 
780 	return 0;
781 }
782 
783 static void nvme_tcp_handle_c2h_term(struct nvme_tcp_queue *queue,
784 		struct nvme_tcp_term_pdu *pdu)
785 {
786 	u16 fes;
787 	const char *msg;
788 	u32 plen = le32_to_cpu(pdu->hdr.plen);
789 
790 	static const char * const msg_table[] = {
791 		[NVME_TCP_FES_INVALID_PDU_HDR] = "Invalid PDU Header Field",
792 		[NVME_TCP_FES_PDU_SEQ_ERR] = "PDU Sequence Error",
793 		[NVME_TCP_FES_HDR_DIGEST_ERR] = "Header Digest Error",
794 		[NVME_TCP_FES_DATA_OUT_OF_RANGE] = "Data Transfer Out Of Range",
795 		[NVME_TCP_FES_DATA_LIMIT_EXCEEDED] = "Data Transfer Limit Exceeded",
796 		[NVME_TCP_FES_UNSUPPORTED_PARAM] = "Unsupported Parameter",
797 	};
798 
799 	if (plen < NVME_TCP_MIN_C2HTERM_PLEN ||
800 	    plen > NVME_TCP_MAX_C2HTERM_PLEN) {
801 		dev_err(queue->ctrl->ctrl.device,
802 			"Received a malformed C2HTermReq PDU (plen = %u)\n",
803 			plen);
804 		return;
805 	}
806 
807 	fes = le16_to_cpu(pdu->fes);
808 	if (fes && fes < ARRAY_SIZE(msg_table))
809 		msg = msg_table[fes];
810 	else
811 		msg = "Unknown";
812 
813 	dev_err(queue->ctrl->ctrl.device,
814 		"Received C2HTermReq (FES = %s)\n", msg);
815 }
816 
817 static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
818 		unsigned int *offset, size_t *len)
819 {
820 	struct nvme_tcp_hdr *hdr;
821 	char *pdu = queue->pdu;
822 	size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
823 	int ret;
824 
825 	ret = skb_copy_bits(skb, *offset,
826 		&pdu[queue->pdu_offset], rcv_len);
827 	if (unlikely(ret))
828 		return ret;
829 
830 	queue->pdu_remaining -= rcv_len;
831 	queue->pdu_offset += rcv_len;
832 	*offset += rcv_len;
833 	*len -= rcv_len;
834 	if (queue->pdu_remaining)
835 		return 0;
836 
837 	hdr = queue->pdu;
838 	if (unlikely(hdr->hlen != sizeof(struct nvme_tcp_rsp_pdu))) {
839 		if (!nvme_tcp_recv_pdu_supported(hdr->type))
840 			goto unsupported_pdu;
841 
842 		dev_err(queue->ctrl->ctrl.device,
843 			"pdu type %d has unexpected header length (%d)\n",
844 			hdr->type, hdr->hlen);
845 		return -EPROTO;
846 	}
847 
848 	if (unlikely(hdr->type == nvme_tcp_c2h_term)) {
849 		/*
850 		 * C2HTermReq never includes Header or Data digests.
851 		 * Skip the checks.
852 		 */
853 		nvme_tcp_handle_c2h_term(queue, (void *)queue->pdu);
854 		return -EINVAL;
855 	}
856 
857 	if (queue->hdr_digest) {
858 		ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen);
859 		if (unlikely(ret))
860 			return ret;
861 	}
862 
863 
864 	if (queue->data_digest) {
865 		ret = nvme_tcp_check_ddgst(queue, queue->pdu);
866 		if (unlikely(ret))
867 			return ret;
868 	}
869 
870 	switch (hdr->type) {
871 	case nvme_tcp_c2h_data:
872 		return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu);
873 	case nvme_tcp_rsp:
874 		nvme_tcp_init_recv_ctx(queue);
875 		return nvme_tcp_handle_comp(queue, (void *)queue->pdu);
876 	case nvme_tcp_r2t:
877 		nvme_tcp_init_recv_ctx(queue);
878 		return nvme_tcp_handle_r2t(queue, (void *)queue->pdu);
879 	default:
880 		goto unsupported_pdu;
881 	}
882 
883 unsupported_pdu:
884 	dev_err(queue->ctrl->ctrl.device,
885 		"unsupported pdu type (%d)\n", hdr->type);
886 	return -EINVAL;
887 }
888 
889 static inline void nvme_tcp_end_request(struct request *rq, u16 status)
890 {
891 	union nvme_result res = {};
892 
893 	if (!nvme_try_complete_req(rq, cpu_to_le16(status << 1), res))
894 		nvme_complete_rq(rq);
895 }
896 
897 static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb,
898 			      unsigned int *offset, size_t *len)
899 {
900 	struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
901 	struct request *rq =
902 		nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
903 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
904 
905 	while (true) {
906 		int recv_len, ret;
907 
908 		recv_len = min_t(size_t, *len, queue->data_remaining);
909 		if (!recv_len)
910 			break;
911 
912 		if (!iov_iter_count(&req->iter)) {
913 			req->curr_bio = req->curr_bio->bi_next;
914 
915 			/*
916 			 * If we don`t have any bios it means that controller
917 			 * sent more data than we requested, hence error
918 			 */
919 			if (!req->curr_bio) {
920 				dev_err(queue->ctrl->ctrl.device,
921 					"queue %d no space in request %#x",
922 					nvme_tcp_queue_id(queue), rq->tag);
923 				nvme_tcp_init_recv_ctx(queue);
924 				return -EIO;
925 			}
926 			nvme_tcp_init_iter(req, ITER_DEST);
927 		}
928 
929 		/* we can read only from what is left in this bio */
930 		recv_len = min_t(size_t, recv_len,
931 				iov_iter_count(&req->iter));
932 
933 		if (queue->data_digest)
934 			ret = skb_copy_and_crc32c_datagram_iter(skb, *offset,
935 				&req->iter, recv_len, &queue->rcv_crc);
936 		else
937 			ret = skb_copy_datagram_iter(skb, *offset,
938 					&req->iter, recv_len);
939 		if (ret) {
940 			dev_err(queue->ctrl->ctrl.device,
941 				"queue %d failed to copy request %#x data",
942 				nvme_tcp_queue_id(queue), rq->tag);
943 			return ret;
944 		}
945 
946 		*len -= recv_len;
947 		*offset += recv_len;
948 		queue->data_remaining -= recv_len;
949 	}
950 
951 	if (!queue->data_remaining) {
952 		if (queue->data_digest) {
953 			queue->exp_ddgst = nvme_tcp_ddgst_final(queue->rcv_crc);
954 			queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH;
955 		} else {
956 			if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
957 				nvme_tcp_end_request(rq,
958 						le16_to_cpu(req->status));
959 				queue->nr_cqe++;
960 			}
961 			nvme_tcp_init_recv_ctx(queue);
962 		}
963 	}
964 
965 	return 0;
966 }
967 
968 static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue,
969 		struct sk_buff *skb, unsigned int *offset, size_t *len)
970 {
971 	struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
972 	char *ddgst = (char *)&queue->recv_ddgst;
973 	size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining);
974 	off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining;
975 	int ret;
976 
977 	ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len);
978 	if (unlikely(ret))
979 		return ret;
980 
981 	queue->ddgst_remaining -= recv_len;
982 	*offset += recv_len;
983 	*len -= recv_len;
984 	if (queue->ddgst_remaining)
985 		return 0;
986 
987 	if (queue->recv_ddgst != queue->exp_ddgst) {
988 		struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
989 					pdu->command_id);
990 		struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
991 
992 		req->status = cpu_to_le16(NVME_SC_DATA_XFER_ERROR);
993 
994 		dev_err(queue->ctrl->ctrl.device,
995 			"data digest error: recv %#x expected %#x\n",
996 			le32_to_cpu(queue->recv_ddgst),
997 			le32_to_cpu(queue->exp_ddgst));
998 	}
999 
1000 	if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
1001 		struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
1002 					pdu->command_id);
1003 		struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
1004 
1005 		nvme_tcp_end_request(rq, le16_to_cpu(req->status));
1006 		queue->nr_cqe++;
1007 	}
1008 
1009 	nvme_tcp_init_recv_ctx(queue);
1010 	return 0;
1011 }
1012 
1013 static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,
1014 			     unsigned int offset, size_t len)
1015 {
1016 	struct nvme_tcp_queue *queue = desc->arg.data;
1017 	size_t consumed = len;
1018 	int result;
1019 
1020 	if (unlikely(!queue->rd_enabled))
1021 		return -EFAULT;
1022 
1023 	while (len) {
1024 		switch (nvme_tcp_recv_state(queue)) {
1025 		case NVME_TCP_RECV_PDU:
1026 			result = nvme_tcp_recv_pdu(queue, skb, &offset, &len);
1027 			break;
1028 		case NVME_TCP_RECV_DATA:
1029 			result = nvme_tcp_recv_data(queue, skb, &offset, &len);
1030 			break;
1031 		case NVME_TCP_RECV_DDGST:
1032 			result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len);
1033 			break;
1034 		default:
1035 			result = -EFAULT;
1036 		}
1037 		if (result) {
1038 			dev_err(queue->ctrl->ctrl.device,
1039 				"receive failed:  %d\n", result);
1040 			queue->rd_enabled = false;
1041 			nvme_tcp_error_recovery(&queue->ctrl->ctrl);
1042 			return result;
1043 		}
1044 	}
1045 
1046 	return consumed;
1047 }
1048 
1049 static void nvme_tcp_data_ready(struct sock *sk)
1050 {
1051 	struct nvme_tcp_queue *queue;
1052 
1053 	trace_sk_data_ready(sk);
1054 
1055 	read_lock_bh(&sk->sk_callback_lock);
1056 	queue = sk->sk_user_data;
1057 	if (likely(queue && queue->rd_enabled) &&
1058 	    !test_bit(NVME_TCP_Q_POLLING, &queue->flags))
1059 		queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
1060 	read_unlock_bh(&sk->sk_callback_lock);
1061 }
1062 
1063 static void nvme_tcp_write_space(struct sock *sk)
1064 {
1065 	struct nvme_tcp_queue *queue;
1066 
1067 	read_lock_bh(&sk->sk_callback_lock);
1068 	queue = sk->sk_user_data;
1069 	if (likely(queue && sk_stream_is_writeable(sk))) {
1070 		clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1071 		queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
1072 	}
1073 	read_unlock_bh(&sk->sk_callback_lock);
1074 }
1075 
1076 static void nvme_tcp_state_change(struct sock *sk)
1077 {
1078 	struct nvme_tcp_queue *queue;
1079 
1080 	read_lock_bh(&sk->sk_callback_lock);
1081 	queue = sk->sk_user_data;
1082 	if (!queue)
1083 		goto done;
1084 
1085 	switch (sk->sk_state) {
1086 	case TCP_CLOSE:
1087 	case TCP_CLOSE_WAIT:
1088 	case TCP_LAST_ACK:
1089 	case TCP_FIN_WAIT1:
1090 	case TCP_FIN_WAIT2:
1091 		nvme_tcp_error_recovery(&queue->ctrl->ctrl);
1092 		break;
1093 	default:
1094 		dev_info(queue->ctrl->ctrl.device,
1095 			"queue %d socket state %d\n",
1096 			nvme_tcp_queue_id(queue), sk->sk_state);
1097 	}
1098 
1099 	queue->state_change(sk);
1100 done:
1101 	read_unlock_bh(&sk->sk_callback_lock);
1102 }
1103 
1104 static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue)
1105 {
1106 	queue->request = NULL;
1107 }
1108 
1109 static void nvme_tcp_fail_request(struct nvme_tcp_request *req)
1110 {
1111 	if (nvme_tcp_async_req(req)) {
1112 		union nvme_result res = {};
1113 
1114 		nvme_complete_async_event(&req->queue->ctrl->ctrl,
1115 				cpu_to_le16(NVME_SC_HOST_PATH_ERROR), &res);
1116 	} else {
1117 		nvme_tcp_end_request(blk_mq_rq_from_pdu(req),
1118 				NVME_SC_HOST_PATH_ERROR);
1119 	}
1120 }
1121 
1122 static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
1123 {
1124 	struct nvme_tcp_queue *queue = req->queue;
1125 	int req_data_len = req->data_len;
1126 	u32 h2cdata_left = req->h2cdata_left;
1127 
1128 	while (true) {
1129 		struct bio_vec bvec;
1130 		struct msghdr msg = {
1131 			.msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES,
1132 		};
1133 		struct page *page = nvme_tcp_req_cur_page(req);
1134 		size_t offset = nvme_tcp_req_cur_offset(req);
1135 		size_t len = nvme_tcp_req_cur_length(req);
1136 		bool last = nvme_tcp_pdu_last_send(req, len);
1137 		int req_data_sent = req->data_sent;
1138 		int ret;
1139 
1140 		if (last && !queue->data_digest && !nvme_tcp_queue_more(queue))
1141 			msg.msg_flags |= MSG_EOR;
1142 		else
1143 			msg.msg_flags |= MSG_MORE;
1144 
1145 		if (!sendpages_ok(page, len, offset))
1146 			msg.msg_flags &= ~MSG_SPLICE_PAGES;
1147 
1148 		bvec_set_page(&bvec, page, len, offset);
1149 		iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
1150 		ret = sock_sendmsg(queue->sock, &msg);
1151 		if (ret <= 0)
1152 			return ret;
1153 
1154 		if (queue->data_digest)
1155 			nvme_tcp_ddgst_update(&queue->snd_crc, page,
1156 					offset, ret);
1157 
1158 		/*
1159 		 * update the request iterator except for the last payload send
1160 		 * in the request where we don't want to modify it as we may
1161 		 * compete with the RX path completing the request.
1162 		 */
1163 		if (req_data_sent + ret < req_data_len)
1164 			nvme_tcp_advance_req(req, ret);
1165 
1166 		/* fully successful last send in current PDU */
1167 		if (last && ret == len) {
1168 			if (queue->data_digest) {
1169 				req->ddgst =
1170 					nvme_tcp_ddgst_final(queue->snd_crc);
1171 				req->state = NVME_TCP_SEND_DDGST;
1172 				req->offset = 0;
1173 			} else {
1174 				if (h2cdata_left)
1175 					nvme_tcp_setup_h2c_data_pdu(req);
1176 				else
1177 					nvme_tcp_done_send_req(queue);
1178 			}
1179 			return 1;
1180 		}
1181 	}
1182 	return -EAGAIN;
1183 }
1184 
1185 static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
1186 {
1187 	struct nvme_tcp_queue *queue = req->queue;
1188 	struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
1189 	struct bio_vec bvec;
1190 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES, };
1191 	bool inline_data = nvme_tcp_has_inline_data(req);
1192 	u8 hdgst = nvme_tcp_hdgst_len(queue);
1193 	int len = sizeof(*pdu) + hdgst - req->offset;
1194 	int ret;
1195 
1196 	if (inline_data || nvme_tcp_queue_more(queue))
1197 		msg.msg_flags |= MSG_MORE;
1198 	else
1199 		msg.msg_flags |= MSG_EOR;
1200 
1201 	if (queue->hdr_digest && !req->offset)
1202 		nvme_tcp_set_hdgst(pdu, sizeof(*pdu));
1203 
1204 	bvec_set_virt(&bvec, (void *)pdu + req->offset, len);
1205 	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
1206 	ret = sock_sendmsg(queue->sock, &msg);
1207 	if (unlikely(ret <= 0))
1208 		return ret;
1209 
1210 	len -= ret;
1211 	if (!len) {
1212 		if (inline_data) {
1213 			req->state = NVME_TCP_SEND_DATA;
1214 			if (queue->data_digest)
1215 				queue->snd_crc = NVME_TCP_CRC_SEED;
1216 		} else {
1217 			nvme_tcp_done_send_req(queue);
1218 		}
1219 		return 1;
1220 	}
1221 	req->offset += ret;
1222 
1223 	return -EAGAIN;
1224 }
1225 
1226 static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
1227 {
1228 	struct nvme_tcp_queue *queue = req->queue;
1229 	struct nvme_tcp_data_pdu *pdu = nvme_tcp_req_data_pdu(req);
1230 	struct bio_vec bvec;
1231 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_MORE, };
1232 	u8 hdgst = nvme_tcp_hdgst_len(queue);
1233 	int len = sizeof(*pdu) - req->offset + hdgst;
1234 	int ret;
1235 
1236 	if (queue->hdr_digest && !req->offset)
1237 		nvme_tcp_set_hdgst(pdu, sizeof(*pdu));
1238 
1239 	if (!req->h2cdata_left)
1240 		msg.msg_flags |= MSG_SPLICE_PAGES;
1241 
1242 	bvec_set_virt(&bvec, (void *)pdu + req->offset, len);
1243 	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
1244 	ret = sock_sendmsg(queue->sock, &msg);
1245 	if (unlikely(ret <= 0))
1246 		return ret;
1247 
1248 	len -= ret;
1249 	if (!len) {
1250 		req->state = NVME_TCP_SEND_DATA;
1251 		if (queue->data_digest)
1252 			queue->snd_crc = NVME_TCP_CRC_SEED;
1253 		return 1;
1254 	}
1255 	req->offset += ret;
1256 
1257 	return -EAGAIN;
1258 }
1259 
1260 static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
1261 {
1262 	struct nvme_tcp_queue *queue = req->queue;
1263 	size_t offset = req->offset;
1264 	u32 h2cdata_left = req->h2cdata_left;
1265 	int ret;
1266 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
1267 	struct kvec iov = {
1268 		.iov_base = (u8 *)&req->ddgst + req->offset,
1269 		.iov_len = NVME_TCP_DIGEST_LENGTH - req->offset
1270 	};
1271 
1272 	if (nvme_tcp_queue_more(queue))
1273 		msg.msg_flags |= MSG_MORE;
1274 	else
1275 		msg.msg_flags |= MSG_EOR;
1276 
1277 	ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1278 	if (unlikely(ret <= 0))
1279 		return ret;
1280 
1281 	if (offset + ret == NVME_TCP_DIGEST_LENGTH) {
1282 		if (h2cdata_left)
1283 			nvme_tcp_setup_h2c_data_pdu(req);
1284 		else
1285 			nvme_tcp_done_send_req(queue);
1286 		return 1;
1287 	}
1288 
1289 	req->offset += ret;
1290 	return -EAGAIN;
1291 }
1292 
1293 static int nvme_tcp_try_send(struct nvme_tcp_queue *queue)
1294 {
1295 	struct nvme_tcp_request *req;
1296 	unsigned int noreclaim_flag;
1297 	int ret = 1;
1298 
1299 	if (!queue->request) {
1300 		queue->request = nvme_tcp_fetch_request(queue);
1301 		if (!queue->request)
1302 			return 0;
1303 	}
1304 	req = queue->request;
1305 
1306 	noreclaim_flag = memalloc_noreclaim_save();
1307 	if (req->state == NVME_TCP_SEND_CMD_PDU) {
1308 		ret = nvme_tcp_try_send_cmd_pdu(req);
1309 		if (ret <= 0)
1310 			goto done;
1311 		if (!nvme_tcp_has_inline_data(req))
1312 			goto out;
1313 	}
1314 
1315 	if (req->state == NVME_TCP_SEND_H2C_PDU) {
1316 		ret = nvme_tcp_try_send_data_pdu(req);
1317 		if (ret <= 0)
1318 			goto done;
1319 	}
1320 
1321 	if (req->state == NVME_TCP_SEND_DATA) {
1322 		ret = nvme_tcp_try_send_data(req);
1323 		if (ret <= 0)
1324 			goto done;
1325 	}
1326 
1327 	if (req->state == NVME_TCP_SEND_DDGST)
1328 		ret = nvme_tcp_try_send_ddgst(req);
1329 done:
1330 	if (ret == -EAGAIN) {
1331 		ret = 0;
1332 	} else if (ret < 0) {
1333 		dev_err(queue->ctrl->ctrl.device,
1334 			"failed to send request %d\n", ret);
1335 		nvme_tcp_fail_request(queue->request);
1336 		nvme_tcp_done_send_req(queue);
1337 	}
1338 out:
1339 	memalloc_noreclaim_restore(noreclaim_flag);
1340 	return ret;
1341 }
1342 
1343 static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue)
1344 {
1345 	struct socket *sock = queue->sock;
1346 	struct sock *sk = sock->sk;
1347 	read_descriptor_t rd_desc;
1348 	int consumed;
1349 
1350 	rd_desc.arg.data = queue;
1351 	rd_desc.count = 1;
1352 	lock_sock(sk);
1353 	queue->nr_cqe = 0;
1354 	consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb);
1355 	release_sock(sk);
1356 	return consumed;
1357 }
1358 
1359 static void nvme_tcp_io_work(struct work_struct *w)
1360 {
1361 	struct nvme_tcp_queue *queue =
1362 		container_of(w, struct nvme_tcp_queue, io_work);
1363 	unsigned long deadline = jiffies + msecs_to_jiffies(1);
1364 
1365 	do {
1366 		bool pending = false;
1367 		int result;
1368 
1369 		if (mutex_trylock(&queue->send_mutex)) {
1370 			result = nvme_tcp_try_send(queue);
1371 			mutex_unlock(&queue->send_mutex);
1372 			if (result > 0)
1373 				pending = true;
1374 			else if (unlikely(result < 0))
1375 				break;
1376 		}
1377 
1378 		result = nvme_tcp_try_recv(queue);
1379 		if (result > 0)
1380 			pending = true;
1381 		else if (unlikely(result < 0))
1382 			return;
1383 
1384 		if (!pending || !queue->rd_enabled)
1385 			return;
1386 
1387 	} while (!time_after(jiffies, deadline)); /* quota is exhausted */
1388 
1389 	queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
1390 }
1391 
1392 static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl)
1393 {
1394 	struct nvme_tcp_request *async = &ctrl->async_req;
1395 
1396 	page_frag_free(async->pdu);
1397 }
1398 
1399 static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
1400 {
1401 	struct nvme_tcp_queue *queue = &ctrl->queues[0];
1402 	struct nvme_tcp_request *async = &ctrl->async_req;
1403 	u8 hdgst = nvme_tcp_hdgst_len(queue);
1404 
1405 	async->pdu = page_frag_alloc(&queue->pf_cache,
1406 		sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
1407 		GFP_KERNEL | __GFP_ZERO);
1408 	if (!async->pdu)
1409 		return -ENOMEM;
1410 
1411 	async->queue = &ctrl->queues[0];
1412 	return 0;
1413 }
1414 
1415 static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
1416 {
1417 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1418 	struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1419 	unsigned int noreclaim_flag;
1420 
1421 	if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
1422 		return;
1423 
1424 	page_frag_cache_drain(&queue->pf_cache);
1425 
1426 	noreclaim_flag = memalloc_noreclaim_save();
1427 	/* ->sock will be released by fput() */
1428 	fput(queue->sock->file);
1429 	queue->sock = NULL;
1430 	memalloc_noreclaim_restore(noreclaim_flag);
1431 
1432 	kfree(queue->pdu);
1433 	mutex_destroy(&queue->send_mutex);
1434 	mutex_destroy(&queue->queue_lock);
1435 }
1436 
1437 static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
1438 {
1439 	struct nvme_tcp_icreq_pdu *icreq;
1440 	struct nvme_tcp_icresp_pdu *icresp;
1441 	char cbuf[CMSG_LEN(sizeof(char))] = {};
1442 	u8 ctype;
1443 	struct msghdr msg = {};
1444 	struct kvec iov;
1445 	bool ctrl_hdgst, ctrl_ddgst;
1446 	u32 maxh2cdata;
1447 	int ret;
1448 
1449 	icreq = kzalloc(sizeof(*icreq), GFP_KERNEL);
1450 	if (!icreq)
1451 		return -ENOMEM;
1452 
1453 	icresp = kzalloc(sizeof(*icresp), GFP_KERNEL);
1454 	if (!icresp) {
1455 		ret = -ENOMEM;
1456 		goto free_icreq;
1457 	}
1458 
1459 	icreq->hdr.type = nvme_tcp_icreq;
1460 	icreq->hdr.hlen = sizeof(*icreq);
1461 	icreq->hdr.pdo = 0;
1462 	icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen);
1463 	icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
1464 	icreq->maxr2t = 0; /* single inflight r2t supported */
1465 	icreq->hpda = 0; /* no alignment constraint */
1466 	if (queue->hdr_digest)
1467 		icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
1468 	if (queue->data_digest)
1469 		icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
1470 
1471 	iov.iov_base = icreq;
1472 	iov.iov_len = sizeof(*icreq);
1473 	ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1474 	if (ret < 0) {
1475 		pr_warn("queue %d: failed to send icreq, error %d\n",
1476 			nvme_tcp_queue_id(queue), ret);
1477 		goto free_icresp;
1478 	}
1479 
1480 	memset(&msg, 0, sizeof(msg));
1481 	iov.iov_base = icresp;
1482 	iov.iov_len = sizeof(*icresp);
1483 	if (nvme_tcp_queue_tls(queue)) {
1484 		msg.msg_control = cbuf;
1485 		msg.msg_controllen = sizeof(cbuf);
1486 	}
1487 	msg.msg_flags = MSG_WAITALL;
1488 	ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1489 			iov.iov_len, msg.msg_flags);
1490 	if (ret >= 0 && ret < sizeof(*icresp))
1491 		ret = -ECONNRESET;
1492 	if (ret < 0) {
1493 		pr_warn("queue %d: failed to receive icresp, error %d\n",
1494 			nvme_tcp_queue_id(queue), ret);
1495 		goto free_icresp;
1496 	}
1497 	ret = -ENOTCONN;
1498 	if (nvme_tcp_queue_tls(queue)) {
1499 		ctype = tls_get_record_type(queue->sock->sk,
1500 					    (struct cmsghdr *)cbuf);
1501 		if (ctype != TLS_RECORD_TYPE_DATA) {
1502 			pr_err("queue %d: unhandled TLS record %d\n",
1503 			       nvme_tcp_queue_id(queue), ctype);
1504 			goto free_icresp;
1505 		}
1506 	}
1507 	ret = -EINVAL;
1508 	if (icresp->hdr.type != nvme_tcp_icresp) {
1509 		pr_err("queue %d: bad type returned %d\n",
1510 			nvme_tcp_queue_id(queue), icresp->hdr.type);
1511 		goto free_icresp;
1512 	}
1513 
1514 	if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) {
1515 		pr_err("queue %d: bad pdu length returned %d\n",
1516 			nvme_tcp_queue_id(queue), icresp->hdr.plen);
1517 		goto free_icresp;
1518 	}
1519 
1520 	if (icresp->pfv != NVME_TCP_PFV_1_0) {
1521 		pr_err("queue %d: bad pfv returned %d\n",
1522 			nvme_tcp_queue_id(queue), icresp->pfv);
1523 		goto free_icresp;
1524 	}
1525 
1526 	ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE);
1527 	if ((queue->data_digest && !ctrl_ddgst) ||
1528 	    (!queue->data_digest && ctrl_ddgst)) {
1529 		pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
1530 			nvme_tcp_queue_id(queue),
1531 			queue->data_digest ? "enabled" : "disabled",
1532 			ctrl_ddgst ? "enabled" : "disabled");
1533 		goto free_icresp;
1534 	}
1535 
1536 	ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE);
1537 	if ((queue->hdr_digest && !ctrl_hdgst) ||
1538 	    (!queue->hdr_digest && ctrl_hdgst)) {
1539 		pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
1540 			nvme_tcp_queue_id(queue),
1541 			queue->hdr_digest ? "enabled" : "disabled",
1542 			ctrl_hdgst ? "enabled" : "disabled");
1543 		goto free_icresp;
1544 	}
1545 
1546 	if (icresp->cpda != 0) {
1547 		pr_err("queue %d: unsupported cpda returned %d\n",
1548 			nvme_tcp_queue_id(queue), icresp->cpda);
1549 		goto free_icresp;
1550 	}
1551 
1552 	maxh2cdata = le32_to_cpu(icresp->maxdata);
1553 	if ((maxh2cdata % 4) || (maxh2cdata < NVME_TCP_MIN_MAXH2CDATA)) {
1554 		pr_err("queue %d: invalid maxh2cdata returned %u\n",
1555 		       nvme_tcp_queue_id(queue), maxh2cdata);
1556 		goto free_icresp;
1557 	}
1558 	queue->maxh2cdata = maxh2cdata;
1559 
1560 	ret = 0;
1561 free_icresp:
1562 	kfree(icresp);
1563 free_icreq:
1564 	kfree(icreq);
1565 	return ret;
1566 }
1567 
1568 static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue)
1569 {
1570 	return nvme_tcp_queue_id(queue) == 0;
1571 }
1572 
1573 static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue)
1574 {
1575 	struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1576 	int qid = nvme_tcp_queue_id(queue);
1577 
1578 	return !nvme_tcp_admin_queue(queue) &&
1579 		qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT];
1580 }
1581 
1582 static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue)
1583 {
1584 	struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1585 	int qid = nvme_tcp_queue_id(queue);
1586 
1587 	return !nvme_tcp_admin_queue(queue) &&
1588 		!nvme_tcp_default_queue(queue) &&
1589 		qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1590 			  ctrl->io_queues[HCTX_TYPE_READ];
1591 }
1592 
1593 static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue)
1594 {
1595 	struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1596 	int qid = nvme_tcp_queue_id(queue);
1597 
1598 	return !nvme_tcp_admin_queue(queue) &&
1599 		!nvme_tcp_default_queue(queue) &&
1600 		!nvme_tcp_read_queue(queue) &&
1601 		qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1602 			  ctrl->io_queues[HCTX_TYPE_READ] +
1603 			  ctrl->io_queues[HCTX_TYPE_POLL];
1604 }
1605 
1606 /*
1607  * Track the number of queues assigned to each cpu using a global per-cpu
1608  * counter and select the least used cpu from the mq_map. Our goal is to spread
1609  * different controllers I/O threads across different cpu cores.
1610  *
1611  * Note that the accounting is not 100% perfect, but we don't need to be, we're
1612  * simply putting our best effort to select the best candidate cpu core that we
1613  * find at any given point.
1614  */
1615 static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue)
1616 {
1617 	struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1618 	struct blk_mq_tag_set *set = &ctrl->tag_set;
1619 	int qid = nvme_tcp_queue_id(queue) - 1;
1620 	unsigned int *mq_map = NULL;
1621 	int cpu, min_queues = INT_MAX, io_cpu;
1622 
1623 	if (wq_unbound)
1624 		goto out;
1625 
1626 	if (nvme_tcp_default_queue(queue))
1627 		mq_map = set->map[HCTX_TYPE_DEFAULT].mq_map;
1628 	else if (nvme_tcp_read_queue(queue))
1629 		mq_map = set->map[HCTX_TYPE_READ].mq_map;
1630 	else if (nvme_tcp_poll_queue(queue))
1631 		mq_map = set->map[HCTX_TYPE_POLL].mq_map;
1632 
1633 	if (WARN_ON(!mq_map))
1634 		goto out;
1635 
1636 	/* Search for the least used cpu from the mq_map */
1637 	io_cpu = WORK_CPU_UNBOUND;
1638 	for_each_online_cpu(cpu) {
1639 		int num_queues = atomic_read(&nvme_tcp_cpu_queues[cpu]);
1640 
1641 		if (mq_map[cpu] != qid)
1642 			continue;
1643 		if (num_queues < min_queues) {
1644 			io_cpu = cpu;
1645 			min_queues = num_queues;
1646 		}
1647 	}
1648 	if (io_cpu != WORK_CPU_UNBOUND) {
1649 		queue->io_cpu = io_cpu;
1650 		atomic_inc(&nvme_tcp_cpu_queues[io_cpu]);
1651 		set_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags);
1652 	}
1653 out:
1654 	dev_dbg(ctrl->ctrl.device, "queue %d: using cpu %d\n",
1655 		qid, queue->io_cpu);
1656 }
1657 
1658 static void nvme_tcp_tls_done(void *data, int status, key_serial_t pskid)
1659 {
1660 	struct nvme_tcp_queue *queue = data;
1661 	struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1662 	int qid = nvme_tcp_queue_id(queue);
1663 	struct key *tls_key;
1664 
1665 	dev_dbg(ctrl->ctrl.device, "queue %d: TLS handshake done, key %x, status %d\n",
1666 		qid, pskid, status);
1667 
1668 	if (status) {
1669 		queue->tls_err = -status;
1670 		goto out_complete;
1671 	}
1672 
1673 	tls_key = nvme_tls_key_lookup(pskid);
1674 	if (IS_ERR(tls_key)) {
1675 		dev_warn(ctrl->ctrl.device, "queue %d: Invalid key %x\n",
1676 			 qid, pskid);
1677 		queue->tls_err = -ENOKEY;
1678 	} else {
1679 		queue->tls_enabled = true;
1680 		if (qid == 0)
1681 			ctrl->ctrl.tls_pskid = key_serial(tls_key);
1682 		key_put(tls_key);
1683 		queue->tls_err = 0;
1684 	}
1685 
1686 out_complete:
1687 	complete(&queue->tls_complete);
1688 }
1689 
1690 static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl,
1691 			      struct nvme_tcp_queue *queue,
1692 			      key_serial_t pskid)
1693 {
1694 	int qid = nvme_tcp_queue_id(queue);
1695 	int ret;
1696 	struct tls_handshake_args args;
1697 	unsigned long tmo = tls_handshake_timeout * HZ;
1698 	key_serial_t keyring = nvme_keyring_id();
1699 
1700 	dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n",
1701 		qid, pskid);
1702 	memset(&args, 0, sizeof(args));
1703 	args.ta_sock = queue->sock;
1704 	args.ta_done = nvme_tcp_tls_done;
1705 	args.ta_data = queue;
1706 	args.ta_my_peerids[0] = pskid;
1707 	args.ta_num_peerids = 1;
1708 	if (nctrl->opts->keyring)
1709 		keyring = key_serial(nctrl->opts->keyring);
1710 	args.ta_keyring = keyring;
1711 	args.ta_timeout_ms = tls_handshake_timeout * 1000;
1712 	queue->tls_err = -EOPNOTSUPP;
1713 	init_completion(&queue->tls_complete);
1714 	ret = tls_client_hello_psk(&args, GFP_KERNEL);
1715 	if (ret) {
1716 		dev_err(nctrl->device, "queue %d: failed to start TLS: %d\n",
1717 			qid, ret);
1718 		return ret;
1719 	}
1720 	ret = wait_for_completion_interruptible_timeout(&queue->tls_complete, tmo);
1721 	if (ret <= 0) {
1722 		if (ret == 0)
1723 			ret = -ETIMEDOUT;
1724 
1725 		dev_err(nctrl->device,
1726 			"queue %d: TLS handshake failed, error %d\n",
1727 			qid, ret);
1728 		tls_handshake_cancel(queue->sock->sk);
1729 	} else {
1730 		dev_dbg(nctrl->device,
1731 			"queue %d: TLS handshake complete, error %d\n",
1732 			qid, queue->tls_err);
1733 		ret = queue->tls_err;
1734 	}
1735 	return ret;
1736 }
1737 
1738 static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid,
1739 				key_serial_t pskid)
1740 {
1741 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1742 	struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1743 	int ret, rcv_pdu_size;
1744 	struct file *sock_file;
1745 
1746 	mutex_init(&queue->queue_lock);
1747 	queue->ctrl = ctrl;
1748 	init_llist_head(&queue->req_list);
1749 	INIT_LIST_HEAD(&queue->send_list);
1750 	mutex_init(&queue->send_mutex);
1751 	INIT_WORK(&queue->io_work, nvme_tcp_io_work);
1752 
1753 	if (qid > 0)
1754 		queue->cmnd_capsule_len = nctrl->ioccsz * 16;
1755 	else
1756 		queue->cmnd_capsule_len = sizeof(struct nvme_command) +
1757 						NVME_TCP_ADMIN_CCSZ;
1758 
1759 	ret = sock_create_kern(current->nsproxy->net_ns,
1760 			ctrl->addr.ss_family, SOCK_STREAM,
1761 			IPPROTO_TCP, &queue->sock);
1762 	if (ret) {
1763 		dev_err(nctrl->device,
1764 			"failed to create socket: %d\n", ret);
1765 		goto err_destroy_mutex;
1766 	}
1767 
1768 	sock_file = sock_alloc_file(queue->sock, O_CLOEXEC, NULL);
1769 	if (IS_ERR(sock_file)) {
1770 		ret = PTR_ERR(sock_file);
1771 		goto err_destroy_mutex;
1772 	}
1773 
1774 	sk_net_refcnt_upgrade(queue->sock->sk);
1775 	nvme_tcp_reclassify_socket(queue->sock);
1776 
1777 	/* Single syn retry */
1778 	tcp_sock_set_syncnt(queue->sock->sk, 1);
1779 
1780 	/* Set TCP no delay */
1781 	tcp_sock_set_nodelay(queue->sock->sk);
1782 
1783 	/*
1784 	 * Cleanup whatever is sitting in the TCP transmit queue on socket
1785 	 * close. This is done to prevent stale data from being sent should
1786 	 * the network connection be restored before TCP times out.
1787 	 */
1788 	sock_no_linger(queue->sock->sk);
1789 
1790 	if (so_priority > 0)
1791 		sock_set_priority(queue->sock->sk, so_priority);
1792 
1793 	/* Set socket type of service */
1794 	if (nctrl->opts->tos >= 0)
1795 		ip_sock_set_tos(queue->sock->sk, nctrl->opts->tos);
1796 
1797 	/* Set 10 seconds timeout for icresp recvmsg */
1798 	queue->sock->sk->sk_rcvtimeo = 10 * HZ;
1799 
1800 	queue->sock->sk->sk_allocation = GFP_ATOMIC;
1801 	queue->sock->sk->sk_use_task_frag = false;
1802 	queue->io_cpu = WORK_CPU_UNBOUND;
1803 	queue->request = NULL;
1804 	queue->data_remaining = 0;
1805 	queue->ddgst_remaining = 0;
1806 	queue->pdu_remaining = 0;
1807 	queue->pdu_offset = 0;
1808 	sk_set_memalloc(queue->sock->sk);
1809 
1810 	if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) {
1811 		ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr,
1812 			sizeof(ctrl->src_addr));
1813 		if (ret) {
1814 			dev_err(nctrl->device,
1815 				"failed to bind queue %d socket %d\n",
1816 				qid, ret);
1817 			goto err_sock;
1818 		}
1819 	}
1820 
1821 	if (nctrl->opts->mask & NVMF_OPT_HOST_IFACE) {
1822 		char *iface = nctrl->opts->host_iface;
1823 		sockptr_t optval = KERNEL_SOCKPTR(iface);
1824 
1825 		ret = sock_setsockopt(queue->sock, SOL_SOCKET, SO_BINDTODEVICE,
1826 				      optval, strlen(iface));
1827 		if (ret) {
1828 			dev_err(nctrl->device,
1829 			  "failed to bind to interface %s queue %d err %d\n",
1830 			  iface, qid, ret);
1831 			goto err_sock;
1832 		}
1833 	}
1834 
1835 	queue->hdr_digest = nctrl->opts->hdr_digest;
1836 	queue->data_digest = nctrl->opts->data_digest;
1837 
1838 	rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) +
1839 			nvme_tcp_hdgst_len(queue);
1840 	queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL);
1841 	if (!queue->pdu) {
1842 		ret = -ENOMEM;
1843 		goto err_sock;
1844 	}
1845 
1846 	dev_dbg(nctrl->device, "connecting queue %d\n",
1847 			nvme_tcp_queue_id(queue));
1848 
1849 	ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr,
1850 		sizeof(ctrl->addr), 0);
1851 	if (ret) {
1852 		dev_err(nctrl->device,
1853 			"failed to connect socket: %d\n", ret);
1854 		goto err_rcv_pdu;
1855 	}
1856 
1857 	/* If PSKs are configured try to start TLS */
1858 	if (nvme_tcp_tls_configured(nctrl) && pskid) {
1859 		ret = nvme_tcp_start_tls(nctrl, queue, pskid);
1860 		if (ret)
1861 			goto err_init_connect;
1862 	}
1863 
1864 	ret = nvme_tcp_init_connection(queue);
1865 	if (ret)
1866 		goto err_init_connect;
1867 
1868 	set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags);
1869 
1870 	return 0;
1871 
1872 err_init_connect:
1873 	kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1874 err_rcv_pdu:
1875 	kfree(queue->pdu);
1876 err_sock:
1877 	/* ->sock will be released by fput() */
1878 	fput(queue->sock->file);
1879 	queue->sock = NULL;
1880 err_destroy_mutex:
1881 	mutex_destroy(&queue->send_mutex);
1882 	mutex_destroy(&queue->queue_lock);
1883 	return ret;
1884 }
1885 
1886 static void nvme_tcp_restore_sock_ops(struct nvme_tcp_queue *queue)
1887 {
1888 	struct socket *sock = queue->sock;
1889 
1890 	write_lock_bh(&sock->sk->sk_callback_lock);
1891 	sock->sk->sk_user_data  = NULL;
1892 	sock->sk->sk_data_ready = queue->data_ready;
1893 	sock->sk->sk_state_change = queue->state_change;
1894 	sock->sk->sk_write_space  = queue->write_space;
1895 	write_unlock_bh(&sock->sk->sk_callback_lock);
1896 }
1897 
1898 static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
1899 {
1900 	kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1901 	nvme_tcp_restore_sock_ops(queue);
1902 	cancel_work_sync(&queue->io_work);
1903 }
1904 
1905 static void nvme_tcp_stop_queue_nowait(struct nvme_ctrl *nctrl, int qid)
1906 {
1907 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1908 	struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1909 
1910 	if (!test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
1911 		return;
1912 
1913 	if (test_and_clear_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags))
1914 		atomic_dec(&nvme_tcp_cpu_queues[queue->io_cpu]);
1915 
1916 	mutex_lock(&queue->queue_lock);
1917 	if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
1918 		__nvme_tcp_stop_queue(queue);
1919 	/* Stopping the queue will disable TLS */
1920 	queue->tls_enabled = false;
1921 	mutex_unlock(&queue->queue_lock);
1922 }
1923 
1924 static void nvme_tcp_wait_queue(struct nvme_ctrl *nctrl, int qid)
1925 {
1926 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1927 	struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1928 	int timeout = 100;
1929 
1930 	while (timeout > 0) {
1931 		if (!test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags) ||
1932 		    !sk_wmem_alloc_get(queue->sock->sk))
1933 			return;
1934 		msleep(2);
1935 		timeout -= 2;
1936 	}
1937 	dev_warn(nctrl->device,
1938 		 "qid %d: timeout draining sock wmem allocation expired\n",
1939 		 qid);
1940 }
1941 
1942 static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
1943 {
1944 	nvme_tcp_stop_queue_nowait(nctrl, qid);
1945 	nvme_tcp_wait_queue(nctrl, qid);
1946 }
1947 
1948 
1949 static void nvme_tcp_setup_sock_ops(struct nvme_tcp_queue *queue)
1950 {
1951 	write_lock_bh(&queue->sock->sk->sk_callback_lock);
1952 	queue->sock->sk->sk_user_data = queue;
1953 	queue->state_change = queue->sock->sk->sk_state_change;
1954 	queue->data_ready = queue->sock->sk->sk_data_ready;
1955 	queue->write_space = queue->sock->sk->sk_write_space;
1956 	queue->sock->sk->sk_data_ready = nvme_tcp_data_ready;
1957 	queue->sock->sk->sk_state_change = nvme_tcp_state_change;
1958 	queue->sock->sk->sk_write_space = nvme_tcp_write_space;
1959 #ifdef CONFIG_NET_RX_BUSY_POLL
1960 	queue->sock->sk->sk_ll_usec = 1;
1961 #endif
1962 	write_unlock_bh(&queue->sock->sk->sk_callback_lock);
1963 }
1964 
1965 static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
1966 {
1967 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1968 	struct nvme_tcp_queue *queue = &ctrl->queues[idx];
1969 	int ret;
1970 
1971 	queue->rd_enabled = true;
1972 	nvme_tcp_init_recv_ctx(queue);
1973 	nvme_tcp_setup_sock_ops(queue);
1974 
1975 	if (idx) {
1976 		nvme_tcp_set_queue_io_cpu(queue);
1977 		ret = nvmf_connect_io_queue(nctrl, idx);
1978 	} else
1979 		ret = nvmf_connect_admin_queue(nctrl);
1980 
1981 	if (!ret) {
1982 		set_bit(NVME_TCP_Q_LIVE, &queue->flags);
1983 	} else {
1984 		if (test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
1985 			__nvme_tcp_stop_queue(queue);
1986 		dev_err(nctrl->device,
1987 			"failed to connect queue: %d ret=%d\n", idx, ret);
1988 	}
1989 	return ret;
1990 }
1991 
1992 static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl)
1993 {
1994 	if (to_tcp_ctrl(ctrl)->async_req.pdu) {
1995 		cancel_work_sync(&ctrl->async_event_work);
1996 		nvme_tcp_free_async_req(to_tcp_ctrl(ctrl));
1997 		to_tcp_ctrl(ctrl)->async_req.pdu = NULL;
1998 	}
1999 
2000 	nvme_tcp_free_queue(ctrl, 0);
2001 }
2002 
2003 static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl)
2004 {
2005 	int i;
2006 
2007 	for (i = 1; i < ctrl->queue_count; i++)
2008 		nvme_tcp_free_queue(ctrl, i);
2009 }
2010 
2011 static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl)
2012 {
2013 	int i;
2014 
2015 	for (i = 1; i < ctrl->queue_count; i++)
2016 		nvme_tcp_stop_queue_nowait(ctrl, i);
2017 	for (i = 1; i < ctrl->queue_count; i++)
2018 		nvme_tcp_wait_queue(ctrl, i);
2019 }
2020 
2021 static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl,
2022 				    int first, int last)
2023 {
2024 	int i, ret;
2025 
2026 	for (i = first; i < last; i++) {
2027 		ret = nvme_tcp_start_queue(ctrl, i);
2028 		if (ret)
2029 			goto out_stop_queues;
2030 	}
2031 
2032 	return 0;
2033 
2034 out_stop_queues:
2035 	for (i--; i >= first; i--)
2036 		nvme_tcp_stop_queue(ctrl, i);
2037 	return ret;
2038 }
2039 
2040 static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
2041 {
2042 	int ret;
2043 	key_serial_t pskid = 0;
2044 
2045 	if (nvme_tcp_tls_configured(ctrl)) {
2046 		if (ctrl->opts->tls_key)
2047 			pskid = key_serial(ctrl->opts->tls_key);
2048 		else if (ctrl->opts->tls) {
2049 			pskid = nvme_tls_psk_default(ctrl->opts->keyring,
2050 						      ctrl->opts->host->nqn,
2051 						      ctrl->opts->subsysnqn);
2052 			if (!pskid) {
2053 				dev_err(ctrl->device, "no valid PSK found\n");
2054 				return -ENOKEY;
2055 			}
2056 		}
2057 	}
2058 
2059 	ret = nvme_tcp_alloc_queue(ctrl, 0, pskid);
2060 	if (ret)
2061 		return ret;
2062 
2063 	ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
2064 	if (ret)
2065 		goto out_free_queue;
2066 
2067 	return 0;
2068 
2069 out_free_queue:
2070 	nvme_tcp_free_queue(ctrl, 0);
2071 	return ret;
2072 }
2073 
2074 static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
2075 {
2076 	int i, ret;
2077 
2078 	if (nvme_tcp_tls_configured(ctrl)) {
2079 		if (ctrl->opts->concat) {
2080 			/*
2081 			 * The generated PSK is stored in the
2082 			 * fabric options
2083 			 */
2084 			if (!ctrl->opts->tls_key) {
2085 				dev_err(ctrl->device, "no PSK generated\n");
2086 				return -ENOKEY;
2087 			}
2088 			if (ctrl->tls_pskid &&
2089 			    ctrl->tls_pskid != key_serial(ctrl->opts->tls_key)) {
2090 				dev_err(ctrl->device, "Stale PSK id %08x\n", ctrl->tls_pskid);
2091 				ctrl->tls_pskid = 0;
2092 			}
2093 		} else if (!ctrl->tls_pskid) {
2094 			dev_err(ctrl->device, "no PSK negotiated\n");
2095 			return -ENOKEY;
2096 		}
2097 	}
2098 
2099 	for (i = 1; i < ctrl->queue_count; i++) {
2100 		ret = nvme_tcp_alloc_queue(ctrl, i,
2101 				ctrl->tls_pskid);
2102 		if (ret)
2103 			goto out_free_queues;
2104 	}
2105 
2106 	return 0;
2107 
2108 out_free_queues:
2109 	for (i--; i >= 1; i--)
2110 		nvme_tcp_free_queue(ctrl, i);
2111 
2112 	return ret;
2113 }
2114 
2115 static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
2116 {
2117 	unsigned int nr_io_queues;
2118 	int ret;
2119 
2120 	nr_io_queues = nvmf_nr_io_queues(ctrl->opts);
2121 	ret = nvme_set_queue_count(ctrl, &nr_io_queues);
2122 	if (ret)
2123 		return ret;
2124 
2125 	if (nr_io_queues == 0) {
2126 		dev_err(ctrl->device,
2127 			"unable to set any I/O queues\n");
2128 		return -ENOMEM;
2129 	}
2130 
2131 	ctrl->queue_count = nr_io_queues + 1;
2132 	dev_info(ctrl->device,
2133 		"creating %d I/O queues.\n", nr_io_queues);
2134 
2135 	nvmf_set_io_queues(ctrl->opts, nr_io_queues,
2136 			   to_tcp_ctrl(ctrl)->io_queues);
2137 	return __nvme_tcp_alloc_io_queues(ctrl);
2138 }
2139 
2140 static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
2141 {
2142 	int ret, nr_queues;
2143 
2144 	ret = nvme_tcp_alloc_io_queues(ctrl);
2145 	if (ret)
2146 		return ret;
2147 
2148 	if (new) {
2149 		ret = nvme_alloc_io_tag_set(ctrl, &to_tcp_ctrl(ctrl)->tag_set,
2150 				&nvme_tcp_mq_ops,
2151 				ctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2,
2152 				sizeof(struct nvme_tcp_request));
2153 		if (ret)
2154 			goto out_free_io_queues;
2155 	}
2156 
2157 	/*
2158 	 * Only start IO queues for which we have allocated the tagset
2159 	 * and limitted it to the available queues. On reconnects, the
2160 	 * queue number might have changed.
2161 	 */
2162 	nr_queues = min(ctrl->tagset->nr_hw_queues + 1, ctrl->queue_count);
2163 	ret = nvme_tcp_start_io_queues(ctrl, 1, nr_queues);
2164 	if (ret)
2165 		goto out_cleanup_connect_q;
2166 
2167 	if (!new) {
2168 		nvme_start_freeze(ctrl);
2169 		nvme_unquiesce_io_queues(ctrl);
2170 		if (!nvme_wait_freeze_timeout(ctrl, NVME_IO_TIMEOUT)) {
2171 			/*
2172 			 * If we timed out waiting for freeze we are likely to
2173 			 * be stuck.  Fail the controller initialization just
2174 			 * to be safe.
2175 			 */
2176 			ret = -ENODEV;
2177 			nvme_unfreeze(ctrl);
2178 			goto out_wait_freeze_timed_out;
2179 		}
2180 		blk_mq_update_nr_hw_queues(ctrl->tagset,
2181 			ctrl->queue_count - 1);
2182 		nvme_unfreeze(ctrl);
2183 	}
2184 
2185 	/*
2186 	 * If the number of queues has increased (reconnect case)
2187 	 * start all new queues now.
2188 	 */
2189 	ret = nvme_tcp_start_io_queues(ctrl, nr_queues,
2190 				       ctrl->tagset->nr_hw_queues + 1);
2191 	if (ret)
2192 		goto out_wait_freeze_timed_out;
2193 
2194 	return 0;
2195 
2196 out_wait_freeze_timed_out:
2197 	nvme_quiesce_io_queues(ctrl);
2198 	nvme_sync_io_queues(ctrl);
2199 	nvme_tcp_stop_io_queues(ctrl);
2200 out_cleanup_connect_q:
2201 	nvme_cancel_tagset(ctrl);
2202 	if (new)
2203 		nvme_remove_io_tag_set(ctrl);
2204 out_free_io_queues:
2205 	nvme_tcp_free_io_queues(ctrl);
2206 	return ret;
2207 }
2208 
2209 static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
2210 {
2211 	int error;
2212 
2213 	error = nvme_tcp_alloc_admin_queue(ctrl);
2214 	if (error)
2215 		return error;
2216 
2217 	if (new) {
2218 		error = nvme_alloc_admin_tag_set(ctrl,
2219 				&to_tcp_ctrl(ctrl)->admin_tag_set,
2220 				&nvme_tcp_admin_mq_ops,
2221 				sizeof(struct nvme_tcp_request));
2222 		if (error)
2223 			goto out_free_queue;
2224 	}
2225 
2226 	error = nvme_tcp_start_queue(ctrl, 0);
2227 	if (error)
2228 		goto out_cleanup_tagset;
2229 
2230 	error = nvme_enable_ctrl(ctrl);
2231 	if (error)
2232 		goto out_stop_queue;
2233 
2234 	nvme_unquiesce_admin_queue(ctrl);
2235 
2236 	error = nvme_init_ctrl_finish(ctrl, false);
2237 	if (error)
2238 		goto out_quiesce_queue;
2239 
2240 	return 0;
2241 
2242 out_quiesce_queue:
2243 	nvme_quiesce_admin_queue(ctrl);
2244 	blk_sync_queue(ctrl->admin_q);
2245 out_stop_queue:
2246 	nvme_tcp_stop_queue(ctrl, 0);
2247 	nvme_cancel_admin_tagset(ctrl);
2248 out_cleanup_tagset:
2249 	if (new)
2250 		nvme_remove_admin_tag_set(ctrl);
2251 out_free_queue:
2252 	nvme_tcp_free_admin_queue(ctrl);
2253 	return error;
2254 }
2255 
2256 static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
2257 		bool remove)
2258 {
2259 	nvme_quiesce_admin_queue(ctrl);
2260 	blk_sync_queue(ctrl->admin_q);
2261 	nvme_tcp_stop_queue(ctrl, 0);
2262 	nvme_cancel_admin_tagset(ctrl);
2263 	if (remove) {
2264 		nvme_unquiesce_admin_queue(ctrl);
2265 		nvme_remove_admin_tag_set(ctrl);
2266 	}
2267 	nvme_tcp_free_admin_queue(ctrl);
2268 	if (ctrl->tls_pskid) {
2269 		dev_dbg(ctrl->device, "Wipe negotiated TLS_PSK %08x\n",
2270 			ctrl->tls_pskid);
2271 		ctrl->tls_pskid = 0;
2272 	}
2273 }
2274 
2275 static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
2276 		bool remove)
2277 {
2278 	if (ctrl->queue_count <= 1)
2279 		return;
2280 	nvme_quiesce_io_queues(ctrl);
2281 	nvme_sync_io_queues(ctrl);
2282 	nvme_tcp_stop_io_queues(ctrl);
2283 	nvme_cancel_tagset(ctrl);
2284 	if (remove) {
2285 		nvme_unquiesce_io_queues(ctrl);
2286 		nvme_remove_io_tag_set(ctrl);
2287 	}
2288 	nvme_tcp_free_io_queues(ctrl);
2289 }
2290 
2291 static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl,
2292 		int status)
2293 {
2294 	enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
2295 
2296 	/* If we are resetting/deleting then do nothing */
2297 	if (state != NVME_CTRL_CONNECTING) {
2298 		WARN_ON_ONCE(state == NVME_CTRL_NEW || state == NVME_CTRL_LIVE);
2299 		return;
2300 	}
2301 
2302 	if (nvmf_should_reconnect(ctrl, status)) {
2303 		dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
2304 			ctrl->opts->reconnect_delay);
2305 		queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
2306 				ctrl->opts->reconnect_delay * HZ);
2307 	} else {
2308 		dev_info(ctrl->device, "Removing controller (%d)...\n",
2309 			 status);
2310 		nvme_delete_ctrl(ctrl);
2311 	}
2312 }
2313 
2314 /*
2315  * The TLS key is set by secure concatenation after negotiation has been
2316  * completed on the admin queue. We need to revoke the key when:
2317  * - concatenation is enabled (otherwise it's a static key set by the user)
2318  * and
2319  * - the generated key is present in ctrl->tls_key (otherwise there's nothing
2320  *   to revoke)
2321  * and
2322  * - a valid PSK key ID has been set in ctrl->tls_pskid (otherwise TLS
2323  *   negotiation has not run).
2324  *
2325  * We cannot always revoke the key as nvme_tcp_alloc_admin_queue() is called
2326  * twice during secure concatenation, once on a 'normal' connection to run the
2327  * DH-HMAC-CHAP negotiation (which generates the key, so it _must not_ be set),
2328  * and once after the negotiation (which uses the key, so it _must_ be set).
2329  */
2330 static bool nvme_tcp_key_revoke_needed(struct nvme_ctrl *ctrl)
2331 {
2332 	return ctrl->opts->concat && ctrl->opts->tls_key && ctrl->tls_pskid;
2333 }
2334 
2335 static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
2336 {
2337 	struct nvmf_ctrl_options *opts = ctrl->opts;
2338 	int ret;
2339 
2340 	ret = nvme_tcp_configure_admin_queue(ctrl, new);
2341 	if (ret)
2342 		return ret;
2343 
2344 	if (ctrl->opts && ctrl->opts->concat && !ctrl->tls_pskid) {
2345 		/* See comments for nvme_tcp_key_revoke_needed() */
2346 		dev_dbg(ctrl->device, "restart admin queue for secure concatenation\n");
2347 		nvme_stop_keep_alive(ctrl);
2348 		nvme_tcp_teardown_admin_queue(ctrl, false);
2349 		ret = nvme_tcp_configure_admin_queue(ctrl, false);
2350 		if (ret)
2351 			return ret;
2352 	}
2353 
2354 	if (ctrl->icdoff) {
2355 		ret = -EOPNOTSUPP;
2356 		dev_err(ctrl->device, "icdoff is not supported!\n");
2357 		goto destroy_admin;
2358 	}
2359 
2360 	if (!nvme_ctrl_sgl_supported(ctrl)) {
2361 		ret = -EOPNOTSUPP;
2362 		dev_err(ctrl->device, "Mandatory sgls are not supported!\n");
2363 		goto destroy_admin;
2364 	}
2365 
2366 	if (opts->queue_size > ctrl->sqsize + 1)
2367 		dev_warn(ctrl->device,
2368 			"queue_size %zu > ctrl sqsize %u, clamping down\n",
2369 			opts->queue_size, ctrl->sqsize + 1);
2370 
2371 	if (ctrl->sqsize + 1 > ctrl->maxcmd) {
2372 		dev_warn(ctrl->device,
2373 			"sqsize %u > ctrl maxcmd %u, clamping down\n",
2374 			ctrl->sqsize + 1, ctrl->maxcmd);
2375 		ctrl->sqsize = ctrl->maxcmd - 1;
2376 	}
2377 
2378 	if (ctrl->queue_count > 1) {
2379 		ret = nvme_tcp_configure_io_queues(ctrl, new);
2380 		if (ret)
2381 			goto destroy_admin;
2382 	}
2383 
2384 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
2385 		/*
2386 		 * state change failure is ok if we started ctrl delete,
2387 		 * unless we're during creation of a new controller to
2388 		 * avoid races with teardown flow.
2389 		 */
2390 		enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
2391 
2392 		WARN_ON_ONCE(state != NVME_CTRL_DELETING &&
2393 			     state != NVME_CTRL_DELETING_NOIO);
2394 		WARN_ON_ONCE(new);
2395 		ret = -EINVAL;
2396 		goto destroy_io;
2397 	}
2398 
2399 	nvme_start_ctrl(ctrl);
2400 	return 0;
2401 
2402 destroy_io:
2403 	if (ctrl->queue_count > 1) {
2404 		nvme_quiesce_io_queues(ctrl);
2405 		nvme_sync_io_queues(ctrl);
2406 		nvme_tcp_stop_io_queues(ctrl);
2407 		nvme_cancel_tagset(ctrl);
2408 		if (new)
2409 			nvme_remove_io_tag_set(ctrl);
2410 		nvme_tcp_free_io_queues(ctrl);
2411 	}
2412 destroy_admin:
2413 	nvme_stop_keep_alive(ctrl);
2414 	nvme_tcp_teardown_admin_queue(ctrl, new);
2415 	return ret;
2416 }
2417 
2418 static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
2419 {
2420 	struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
2421 			struct nvme_tcp_ctrl, connect_work);
2422 	struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2423 	int ret;
2424 
2425 	++ctrl->nr_reconnects;
2426 
2427 	ret = nvme_tcp_setup_ctrl(ctrl, false);
2428 	if (ret)
2429 		goto requeue;
2430 
2431 	dev_info(ctrl->device, "Successfully reconnected (attempt %d/%d)\n",
2432 		 ctrl->nr_reconnects, ctrl->opts->max_reconnects);
2433 
2434 	ctrl->nr_reconnects = 0;
2435 
2436 	return;
2437 
2438 requeue:
2439 	dev_info(ctrl->device, "Failed reconnect attempt %d/%d\n",
2440 		 ctrl->nr_reconnects, ctrl->opts->max_reconnects);
2441 	nvme_tcp_reconnect_or_remove(ctrl, ret);
2442 }
2443 
2444 static void nvme_tcp_error_recovery_work(struct work_struct *work)
2445 {
2446 	struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
2447 				struct nvme_tcp_ctrl, err_work);
2448 	struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2449 
2450 	if (nvme_tcp_key_revoke_needed(ctrl))
2451 		nvme_auth_revoke_tls_key(ctrl);
2452 	nvme_stop_keep_alive(ctrl);
2453 	flush_work(&ctrl->async_event_work);
2454 	nvme_tcp_teardown_io_queues(ctrl, false);
2455 	/* unquiesce to fail fast pending requests */
2456 	nvme_unquiesce_io_queues(ctrl);
2457 	nvme_tcp_teardown_admin_queue(ctrl, false);
2458 	nvme_unquiesce_admin_queue(ctrl);
2459 	nvme_auth_stop(ctrl);
2460 
2461 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2462 		/* state change failure is ok if we started ctrl delete */
2463 		enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
2464 
2465 		WARN_ON_ONCE(state != NVME_CTRL_DELETING &&
2466 			     state != NVME_CTRL_DELETING_NOIO);
2467 		return;
2468 	}
2469 
2470 	nvme_tcp_reconnect_or_remove(ctrl, 0);
2471 }
2472 
2473 static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
2474 {
2475 	nvme_tcp_teardown_io_queues(ctrl, shutdown);
2476 	nvme_quiesce_admin_queue(ctrl);
2477 	nvme_disable_ctrl(ctrl, shutdown);
2478 	nvme_tcp_teardown_admin_queue(ctrl, shutdown);
2479 }
2480 
2481 static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
2482 {
2483 	nvme_tcp_teardown_ctrl(ctrl, true);
2484 }
2485 
2486 static void nvme_reset_ctrl_work(struct work_struct *work)
2487 {
2488 	struct nvme_ctrl *ctrl =
2489 		container_of(work, struct nvme_ctrl, reset_work);
2490 	int ret;
2491 
2492 	if (nvme_tcp_key_revoke_needed(ctrl))
2493 		nvme_auth_revoke_tls_key(ctrl);
2494 	nvme_stop_ctrl(ctrl);
2495 	nvme_tcp_teardown_ctrl(ctrl, false);
2496 
2497 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2498 		/* state change failure is ok if we started ctrl delete */
2499 		enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
2500 
2501 		WARN_ON_ONCE(state != NVME_CTRL_DELETING &&
2502 			     state != NVME_CTRL_DELETING_NOIO);
2503 		return;
2504 	}
2505 
2506 	ret = nvme_tcp_setup_ctrl(ctrl, false);
2507 	if (ret)
2508 		goto out_fail;
2509 
2510 	return;
2511 
2512 out_fail:
2513 	++ctrl->nr_reconnects;
2514 	nvme_tcp_reconnect_or_remove(ctrl, ret);
2515 }
2516 
2517 static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl)
2518 {
2519 	flush_work(&to_tcp_ctrl(ctrl)->err_work);
2520 	cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
2521 }
2522 
2523 static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
2524 {
2525 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
2526 
2527 	if (list_empty(&ctrl->list))
2528 		goto free_ctrl;
2529 
2530 	mutex_lock(&nvme_tcp_ctrl_mutex);
2531 	list_del(&ctrl->list);
2532 	mutex_unlock(&nvme_tcp_ctrl_mutex);
2533 
2534 	nvmf_free_options(nctrl->opts);
2535 free_ctrl:
2536 	kfree(ctrl->queues);
2537 	kfree(ctrl);
2538 }
2539 
2540 static void nvme_tcp_set_sg_null(struct nvme_command *c)
2541 {
2542 	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2543 
2544 	sg->addr = 0;
2545 	sg->length = 0;
2546 	sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2547 			NVME_SGL_FMT_TRANSPORT_A;
2548 }
2549 
2550 static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
2551 		struct nvme_command *c, u32 data_len)
2552 {
2553 	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2554 
2555 	sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
2556 	sg->length = cpu_to_le32(data_len);
2557 	sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
2558 }
2559 
2560 static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
2561 		u32 data_len)
2562 {
2563 	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2564 
2565 	sg->addr = 0;
2566 	sg->length = cpu_to_le32(data_len);
2567 	sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2568 			NVME_SGL_FMT_TRANSPORT_A;
2569 }
2570 
2571 static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
2572 {
2573 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
2574 	struct nvme_tcp_queue *queue = &ctrl->queues[0];
2575 	struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
2576 	struct nvme_command *cmd = &pdu->cmd;
2577 	u8 hdgst = nvme_tcp_hdgst_len(queue);
2578 
2579 	memset(pdu, 0, sizeof(*pdu));
2580 	pdu->hdr.type = nvme_tcp_cmd;
2581 	if (queue->hdr_digest)
2582 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
2583 	pdu->hdr.hlen = sizeof(*pdu);
2584 	pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
2585 
2586 	cmd->common.opcode = nvme_admin_async_event;
2587 	cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
2588 	cmd->common.flags |= NVME_CMD_SGL_METABUF;
2589 	nvme_tcp_set_sg_null(cmd);
2590 
2591 	ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
2592 	ctrl->async_req.offset = 0;
2593 	ctrl->async_req.curr_bio = NULL;
2594 	ctrl->async_req.data_len = 0;
2595 
2596 	nvme_tcp_queue_request(&ctrl->async_req, true, true);
2597 }
2598 
2599 static void nvme_tcp_complete_timed_out(struct request *rq)
2600 {
2601 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2602 	struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
2603 
2604 	nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
2605 	nvmf_complete_timed_out_request(rq);
2606 }
2607 
2608 static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq)
2609 {
2610 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2611 	struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
2612 	struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
2613 	struct nvme_command *cmd = &pdu->cmd;
2614 	int qid = nvme_tcp_queue_id(req->queue);
2615 
2616 	dev_warn(ctrl->device,
2617 		 "I/O tag %d (%04x) type %d opcode %#x (%s) QID %d timeout\n",
2618 		 rq->tag, nvme_cid(rq), pdu->hdr.type, cmd->common.opcode,
2619 		 nvme_fabrics_opcode_str(qid, cmd), qid);
2620 
2621 	if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) {
2622 		/*
2623 		 * If we are resetting, connecting or deleting we should
2624 		 * complete immediately because we may block controller
2625 		 * teardown or setup sequence
2626 		 * - ctrl disable/shutdown fabrics requests
2627 		 * - connect requests
2628 		 * - initialization admin requests
2629 		 * - I/O requests that entered after unquiescing and
2630 		 *   the controller stopped responding
2631 		 *
2632 		 * All other requests should be cancelled by the error
2633 		 * recovery work, so it's fine that we fail it here.
2634 		 */
2635 		nvme_tcp_complete_timed_out(rq);
2636 		return BLK_EH_DONE;
2637 	}
2638 
2639 	/*
2640 	 * LIVE state should trigger the normal error recovery which will
2641 	 * handle completing this request.
2642 	 */
2643 	nvme_tcp_error_recovery(ctrl);
2644 	return BLK_EH_RESET_TIMER;
2645 }
2646 
2647 static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
2648 			struct request *rq)
2649 {
2650 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2651 	struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
2652 	struct nvme_command *c = &pdu->cmd;
2653 
2654 	c->common.flags |= NVME_CMD_SGL_METABUF;
2655 
2656 	if (!blk_rq_nr_phys_segments(rq))
2657 		nvme_tcp_set_sg_null(c);
2658 	else if (rq_data_dir(rq) == WRITE &&
2659 	    req->data_len <= nvme_tcp_inline_data_size(req))
2660 		nvme_tcp_set_sg_inline(queue, c, req->data_len);
2661 	else
2662 		nvme_tcp_set_sg_host_data(c, req->data_len);
2663 
2664 	return 0;
2665 }
2666 
2667 static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
2668 		struct request *rq)
2669 {
2670 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2671 	struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
2672 	struct nvme_tcp_queue *queue = req->queue;
2673 	u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
2674 	blk_status_t ret;
2675 
2676 	ret = nvme_setup_cmd(ns, rq);
2677 	if (ret)
2678 		return ret;
2679 
2680 	req->state = NVME_TCP_SEND_CMD_PDU;
2681 	req->status = cpu_to_le16(NVME_SC_SUCCESS);
2682 	req->offset = 0;
2683 	req->data_sent = 0;
2684 	req->pdu_len = 0;
2685 	req->pdu_sent = 0;
2686 	req->h2cdata_left = 0;
2687 	req->data_len = blk_rq_nr_phys_segments(rq) ?
2688 				blk_rq_payload_bytes(rq) : 0;
2689 	req->curr_bio = rq->bio;
2690 	if (req->curr_bio && req->data_len)
2691 		nvme_tcp_init_iter(req, rq_data_dir(rq));
2692 
2693 	if (rq_data_dir(rq) == WRITE &&
2694 	    req->data_len <= nvme_tcp_inline_data_size(req))
2695 		req->pdu_len = req->data_len;
2696 
2697 	pdu->hdr.type = nvme_tcp_cmd;
2698 	pdu->hdr.flags = 0;
2699 	if (queue->hdr_digest)
2700 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
2701 	if (queue->data_digest && req->pdu_len) {
2702 		pdu->hdr.flags |= NVME_TCP_F_DDGST;
2703 		ddgst = nvme_tcp_ddgst_len(queue);
2704 	}
2705 	pdu->hdr.hlen = sizeof(*pdu);
2706 	pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
2707 	pdu->hdr.plen =
2708 		cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
2709 
2710 	ret = nvme_tcp_map_data(queue, rq);
2711 	if (unlikely(ret)) {
2712 		nvme_cleanup_cmd(rq);
2713 		dev_err(queue->ctrl->ctrl.device,
2714 			"Failed to map data (%d)\n", ret);
2715 		return ret;
2716 	}
2717 
2718 	return 0;
2719 }
2720 
2721 static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx)
2722 {
2723 	struct nvme_tcp_queue *queue = hctx->driver_data;
2724 
2725 	if (!llist_empty(&queue->req_list))
2726 		queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
2727 }
2728 
2729 static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
2730 		const struct blk_mq_queue_data *bd)
2731 {
2732 	struct nvme_ns *ns = hctx->queue->queuedata;
2733 	struct nvme_tcp_queue *queue = hctx->driver_data;
2734 	struct request *rq = bd->rq;
2735 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2736 	bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
2737 	blk_status_t ret;
2738 
2739 	if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2740 		return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
2741 
2742 	ret = nvme_tcp_setup_cmd_pdu(ns, rq);
2743 	if (unlikely(ret))
2744 		return ret;
2745 
2746 	nvme_start_request(rq);
2747 
2748 	nvme_tcp_queue_request(req, true, bd->last);
2749 
2750 	return BLK_STS_OK;
2751 }
2752 
2753 static void nvme_tcp_map_queues(struct blk_mq_tag_set *set)
2754 {
2755 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data);
2756 
2757 	nvmf_map_queues(set, &ctrl->ctrl, ctrl->io_queues);
2758 }
2759 
2760 static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
2761 {
2762 	struct nvme_tcp_queue *queue = hctx->driver_data;
2763 	struct sock *sk = queue->sock->sk;
2764 	int ret;
2765 
2766 	if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2767 		return 0;
2768 
2769 	set_bit(NVME_TCP_Q_POLLING, &queue->flags);
2770 	if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue))
2771 		sk_busy_loop(sk, true);
2772 	ret = nvme_tcp_try_recv(queue);
2773 	clear_bit(NVME_TCP_Q_POLLING, &queue->flags);
2774 	return ret < 0 ? ret : queue->nr_cqe;
2775 }
2776 
2777 static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
2778 {
2779 	struct nvme_tcp_queue *queue = &to_tcp_ctrl(ctrl)->queues[0];
2780 	struct sockaddr_storage src_addr;
2781 	int ret, len;
2782 
2783 	len = nvmf_get_address(ctrl, buf, size);
2784 
2785 	if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2786 		return len;
2787 
2788 	mutex_lock(&queue->queue_lock);
2789 
2790 	ret = kernel_getsockname(queue->sock, (struct sockaddr *)&src_addr);
2791 	if (ret > 0) {
2792 		if (len > 0)
2793 			len--; /* strip trailing newline */
2794 		len += scnprintf(buf + len, size - len, "%ssrc_addr=%pISc\n",
2795 				(len) ? "," : "", &src_addr);
2796 	}
2797 
2798 	mutex_unlock(&queue->queue_lock);
2799 
2800 	return len;
2801 }
2802 
2803 static const struct blk_mq_ops nvme_tcp_mq_ops = {
2804 	.queue_rq	= nvme_tcp_queue_rq,
2805 	.commit_rqs	= nvme_tcp_commit_rqs,
2806 	.complete	= nvme_complete_rq,
2807 	.init_request	= nvme_tcp_init_request,
2808 	.exit_request	= nvme_tcp_exit_request,
2809 	.init_hctx	= nvme_tcp_init_hctx,
2810 	.timeout	= nvme_tcp_timeout,
2811 	.map_queues	= nvme_tcp_map_queues,
2812 	.poll		= nvme_tcp_poll,
2813 };
2814 
2815 static const struct blk_mq_ops nvme_tcp_admin_mq_ops = {
2816 	.queue_rq	= nvme_tcp_queue_rq,
2817 	.complete	= nvme_complete_rq,
2818 	.init_request	= nvme_tcp_init_request,
2819 	.exit_request	= nvme_tcp_exit_request,
2820 	.init_hctx	= nvme_tcp_init_admin_hctx,
2821 	.timeout	= nvme_tcp_timeout,
2822 };
2823 
2824 static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
2825 	.name			= "tcp",
2826 	.module			= THIS_MODULE,
2827 	.flags			= NVME_F_FABRICS | NVME_F_BLOCKING,
2828 	.reg_read32		= nvmf_reg_read32,
2829 	.reg_read64		= nvmf_reg_read64,
2830 	.reg_write32		= nvmf_reg_write32,
2831 	.subsystem_reset	= nvmf_subsystem_reset,
2832 	.free_ctrl		= nvme_tcp_free_ctrl,
2833 	.submit_async_event	= nvme_tcp_submit_async_event,
2834 	.delete_ctrl		= nvme_tcp_delete_ctrl,
2835 	.get_address		= nvme_tcp_get_address,
2836 	.stop_ctrl		= nvme_tcp_stop_ctrl,
2837 };
2838 
2839 static bool
2840 nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
2841 {
2842 	struct nvme_tcp_ctrl *ctrl;
2843 	bool found = false;
2844 
2845 	mutex_lock(&nvme_tcp_ctrl_mutex);
2846 	list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
2847 		found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2848 		if (found)
2849 			break;
2850 	}
2851 	mutex_unlock(&nvme_tcp_ctrl_mutex);
2852 
2853 	return found;
2854 }
2855 
2856 static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev,
2857 		struct nvmf_ctrl_options *opts)
2858 {
2859 	struct nvme_tcp_ctrl *ctrl;
2860 	int ret;
2861 
2862 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2863 	if (!ctrl)
2864 		return ERR_PTR(-ENOMEM);
2865 
2866 	INIT_LIST_HEAD(&ctrl->list);
2867 	ctrl->ctrl.opts = opts;
2868 	ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2869 				opts->nr_poll_queues + 1;
2870 	ctrl->ctrl.sqsize = opts->queue_size - 1;
2871 	ctrl->ctrl.kato = opts->kato;
2872 
2873 	INIT_DELAYED_WORK(&ctrl->connect_work,
2874 			nvme_tcp_reconnect_ctrl_work);
2875 	INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
2876 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
2877 
2878 	if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2879 		opts->trsvcid =
2880 			kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
2881 		if (!opts->trsvcid) {
2882 			ret = -ENOMEM;
2883 			goto out_free_ctrl;
2884 		}
2885 		opts->mask |= NVMF_OPT_TRSVCID;
2886 	}
2887 
2888 	ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2889 			opts->traddr, opts->trsvcid, &ctrl->addr);
2890 	if (ret) {
2891 		pr_err("malformed address passed: %s:%s\n",
2892 			opts->traddr, opts->trsvcid);
2893 		goto out_free_ctrl;
2894 	}
2895 
2896 	if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2897 		ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2898 			opts->host_traddr, NULL, &ctrl->src_addr);
2899 		if (ret) {
2900 			pr_err("malformed src address passed: %s\n",
2901 			       opts->host_traddr);
2902 			goto out_free_ctrl;
2903 		}
2904 	}
2905 
2906 	if (opts->mask & NVMF_OPT_HOST_IFACE) {
2907 		if (!__dev_get_by_name(&init_net, opts->host_iface)) {
2908 			pr_err("invalid interface passed: %s\n",
2909 			       opts->host_iface);
2910 			ret = -ENODEV;
2911 			goto out_free_ctrl;
2912 		}
2913 	}
2914 
2915 	if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
2916 		ret = -EALREADY;
2917 		goto out_free_ctrl;
2918 	}
2919 
2920 	ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
2921 				GFP_KERNEL);
2922 	if (!ctrl->queues) {
2923 		ret = -ENOMEM;
2924 		goto out_free_ctrl;
2925 	}
2926 
2927 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
2928 	if (ret)
2929 		goto out_kfree_queues;
2930 
2931 	return ctrl;
2932 out_kfree_queues:
2933 	kfree(ctrl->queues);
2934 out_free_ctrl:
2935 	kfree(ctrl);
2936 	return ERR_PTR(ret);
2937 }
2938 
2939 static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
2940 		struct nvmf_ctrl_options *opts)
2941 {
2942 	struct nvme_tcp_ctrl *ctrl;
2943 	int ret;
2944 
2945 	ctrl = nvme_tcp_alloc_ctrl(dev, opts);
2946 	if (IS_ERR(ctrl))
2947 		return ERR_CAST(ctrl);
2948 
2949 	ret = nvme_add_ctrl(&ctrl->ctrl);
2950 	if (ret)
2951 		goto out_put_ctrl;
2952 
2953 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2954 		WARN_ON_ONCE(1);
2955 		ret = -EINTR;
2956 		goto out_uninit_ctrl;
2957 	}
2958 
2959 	ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
2960 	if (ret)
2961 		goto out_uninit_ctrl;
2962 
2963 	dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp, hostnqn: %s\n",
2964 		nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr, opts->host->nqn);
2965 
2966 	mutex_lock(&nvme_tcp_ctrl_mutex);
2967 	list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
2968 	mutex_unlock(&nvme_tcp_ctrl_mutex);
2969 
2970 	return &ctrl->ctrl;
2971 
2972 out_uninit_ctrl:
2973 	nvme_uninit_ctrl(&ctrl->ctrl);
2974 out_put_ctrl:
2975 	nvme_put_ctrl(&ctrl->ctrl);
2976 	if (ret > 0)
2977 		ret = -EIO;
2978 	return ERR_PTR(ret);
2979 }
2980 
2981 static struct nvmf_transport_ops nvme_tcp_transport = {
2982 	.name		= "tcp",
2983 	.module		= THIS_MODULE,
2984 	.required_opts	= NVMF_OPT_TRADDR,
2985 	.allowed_opts	= NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2986 			  NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2987 			  NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
2988 			  NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2989 			  NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS |
2990 			  NVMF_OPT_KEYRING | NVMF_OPT_TLS_KEY | NVMF_OPT_CONCAT,
2991 	.create_ctrl	= nvme_tcp_create_ctrl,
2992 };
2993 
2994 static int __init nvme_tcp_init_module(void)
2995 {
2996 	unsigned int wq_flags = WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_SYSFS;
2997 	int cpu;
2998 
2999 	BUILD_BUG_ON(sizeof(struct nvme_tcp_hdr) != 8);
3000 	BUILD_BUG_ON(sizeof(struct nvme_tcp_cmd_pdu) != 72);
3001 	BUILD_BUG_ON(sizeof(struct nvme_tcp_data_pdu) != 24);
3002 	BUILD_BUG_ON(sizeof(struct nvme_tcp_rsp_pdu) != 24);
3003 	BUILD_BUG_ON(sizeof(struct nvme_tcp_r2t_pdu) != 24);
3004 	BUILD_BUG_ON(sizeof(struct nvme_tcp_icreq_pdu) != 128);
3005 	BUILD_BUG_ON(sizeof(struct nvme_tcp_icresp_pdu) != 128);
3006 	BUILD_BUG_ON(sizeof(struct nvme_tcp_term_pdu) != 24);
3007 
3008 	if (wq_unbound)
3009 		wq_flags |= WQ_UNBOUND;
3010 
3011 	nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq", wq_flags, 0);
3012 	if (!nvme_tcp_wq)
3013 		return -ENOMEM;
3014 
3015 	for_each_possible_cpu(cpu)
3016 		atomic_set(&nvme_tcp_cpu_queues[cpu], 0);
3017 
3018 	nvmf_register_transport(&nvme_tcp_transport);
3019 	return 0;
3020 }
3021 
3022 static void __exit nvme_tcp_cleanup_module(void)
3023 {
3024 	struct nvme_tcp_ctrl *ctrl;
3025 
3026 	nvmf_unregister_transport(&nvme_tcp_transport);
3027 
3028 	mutex_lock(&nvme_tcp_ctrl_mutex);
3029 	list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
3030 		nvme_delete_ctrl(&ctrl->ctrl);
3031 	mutex_unlock(&nvme_tcp_ctrl_mutex);
3032 	flush_workqueue(nvme_delete_wq);
3033 
3034 	destroy_workqueue(nvme_tcp_wq);
3035 }
3036 
3037 module_init(nvme_tcp_init_module);
3038 module_exit(nvme_tcp_cleanup_module);
3039 
3040 MODULE_DESCRIPTION("NVMe host TCP transport driver");
3041 MODULE_LICENSE("GPL v2");
3042