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