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