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