xref: /linux/drivers/nvme/target/tcp.c (revision 9dac3b1aa2a99e126a5504b7f5bc781c8b2771e0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NVMe over Fabrics TCP target.
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/jiffies.h>
10 #include <linux/slab.h>
11 #include <linux/crc32c.h>
12 #include <linux/err.h>
13 #include <linux/nvme-tcp.h>
14 #include <linux/nvme-keyring.h>
15 #include <net/sock.h>
16 #include <net/tcp.h>
17 #include <net/tls.h>
18 #include <net/tls_prot.h>
19 #include <net/handshake.h>
20 #include <linux/inet.h>
21 #include <linux/llist.h>
22 #include <trace/events/sock.h>
23 
24 #include "nvmet.h"
25 
26 #define NVMET_TCP_DEF_INLINE_DATA_SIZE	(4 * PAGE_SIZE)
27 #define NVMET_TCP_MAXH2CDATA		0x400000 /* 16M arbitrary limit */
28 #define NVMET_TCP_BACKLOG 128
29 
30 static int param_store_val(const char *str, int *val, int min, int max)
31 {
32 	int ret, new_val;
33 
34 	ret = kstrtoint(str, 10, &new_val);
35 	if (ret)
36 		return -EINVAL;
37 
38 	if (new_val < min || new_val > max)
39 		return -EINVAL;
40 
41 	*val = new_val;
42 	return 0;
43 }
44 
45 static int set_params(const char *str, const struct kernel_param *kp)
46 {
47 	return param_store_val(str, kp->arg, 0, INT_MAX);
48 }
49 
50 static const struct kernel_param_ops set_param_ops = {
51 	.set	= set_params,
52 	.get	= param_get_int,
53 };
54 
55 /* Define the socket priority to use for connections were it is desirable
56  * that the NIC consider performing optimized packet processing or filtering.
57  * A non-zero value being sufficient to indicate general consideration of any
58  * possible optimization.  Making it a module param allows for alternative
59  * values that may be unique for some NIC implementations.
60  */
61 static int so_priority;
62 device_param_cb(so_priority, &set_param_ops, &so_priority, 0644);
63 MODULE_PARM_DESC(so_priority, "nvmet tcp socket optimize priority: Default 0");
64 
65 /* Define a time period (in usecs) that io_work() shall sample an activated
66  * queue before determining it to be idle.  This optional module behavior
67  * can enable NIC solutions that support socket optimized packet processing
68  * using advanced interrupt moderation techniques.
69  */
70 static int idle_poll_period_usecs;
71 device_param_cb(idle_poll_period_usecs, &set_param_ops,
72 		&idle_poll_period_usecs, 0644);
73 MODULE_PARM_DESC(idle_poll_period_usecs,
74 		"nvmet tcp io_work poll till idle time period in usecs: Default 0");
75 
76 #ifdef CONFIG_NVME_TARGET_TCP_TLS
77 /*
78  * TLS handshake timeout
79  */
80 static unsigned int tls_handshake_timeout = 10;
81 
82 static int tls_handshake_timeout_set(const char *val,
83 				     const struct kernel_param *kp)
84 {
85 	return param_set_uint_minmax(val, kp, 0,
86 		min_t(unsigned long, UINT_MAX / MSEC_PER_SEC,
87 		      MAX_JIFFY_OFFSET / HZ));
88 }
89 
90 static const struct kernel_param_ops tls_handshake_timeout_ops = {
91 	.set = tls_handshake_timeout_set,
92 	.get = param_get_uint,
93 };
94 
95 module_param_cb(tls_handshake_timeout, &tls_handshake_timeout_ops,
96 		&tls_handshake_timeout, 0644);
97 __MODULE_PARM_TYPE(tls_handshake_timeout, "uint");
98 MODULE_PARM_DESC(tls_handshake_timeout,
99 		 "nvme TLS handshake timeout in seconds (default 10)");
100 #endif
101 
102 #define NVMET_TCP_RECV_BUDGET		8
103 #define NVMET_TCP_SEND_BUDGET		8
104 #define NVMET_TCP_IO_WORK_BUDGET	64
105 #define NVMET_TCP_ACCEPT_BUDGET		16
106 
107 enum nvmet_tcp_send_state {
108 	NVMET_TCP_SEND_DATA_PDU,
109 	NVMET_TCP_SEND_DATA,
110 	NVMET_TCP_SEND_R2T,
111 	NVMET_TCP_SEND_DDGST,
112 	NVMET_TCP_SEND_RESPONSE
113 };
114 
115 enum nvmet_tcp_recv_state {
116 	NVMET_TCP_RECV_PDU,
117 	NVMET_TCP_RECV_DATA,
118 	NVMET_TCP_RECV_DDGST,
119 	NVMET_TCP_RECV_ERR,
120 };
121 
122 enum {
123 	NVMET_TCP_F_INIT_FAILED = (1 << 0),
124 	NVMET_TCP_F_R2T_SENT	= (1 << 1),
125 };
126 
127 struct nvmet_tcp_cmd {
128 	struct nvmet_tcp_queue		*queue;
129 	struct nvmet_req		req;
130 
131 	struct nvme_tcp_cmd_pdu		*cmd_pdu;
132 	struct nvme_tcp_rsp_pdu		*rsp_pdu;
133 	struct nvme_tcp_data_pdu	*data_pdu;
134 	struct nvme_tcp_r2t_pdu		*r2t_pdu;
135 
136 	u32				rbytes_done;
137 	u32				wbytes_done;
138 
139 	u32				pdu_len;
140 	u32				pdu_recv;
141 	int				sg_idx;
142 	char				recv_cbuf[CMSG_LEN(sizeof(char))];
143 	struct msghdr			recv_msg;
144 	struct bio_vec			*iov;
145 	u32				flags;
146 
147 	struct list_head		entry;
148 	struct llist_node		lentry;
149 
150 	/* send state */
151 	u32				offset;
152 	struct scatterlist		*cur_sg;
153 	enum nvmet_tcp_send_state	state;
154 
155 	__le32				exp_ddgst;
156 	__le32				recv_ddgst;
157 };
158 
159 enum nvmet_tcp_queue_state {
160 	NVMET_TCP_Q_CONNECTING,
161 	NVMET_TCP_Q_TLS_HANDSHAKE,
162 	NVMET_TCP_Q_LIVE,
163 	NVMET_TCP_Q_DISCONNECTING,
164 	NVMET_TCP_Q_FAILED,
165 };
166 
167 struct nvmet_tcp_queue {
168 	struct socket		*sock;
169 	struct nvmet_tcp_port	*port;
170 	struct nvmet_port	*nport;
171 	struct work_struct	io_work;
172 	struct nvmet_cq		nvme_cq;
173 	struct nvmet_sq		nvme_sq;
174 	struct kref		kref;
175 
176 	/* send state */
177 	struct nvmet_tcp_cmd	*cmds;
178 	unsigned int		nr_cmds;
179 	struct list_head	free_list;
180 	struct llist_head	resp_list;
181 	struct list_head	resp_send_list;
182 	int			send_list_len;
183 	struct nvmet_tcp_cmd	*snd_cmd;
184 
185 	/* recv state */
186 	int			offset;
187 	int			left;
188 	enum nvmet_tcp_recv_state rcv_state;
189 	struct nvmet_tcp_cmd	*cmd;
190 	union nvme_tcp_pdu	pdu;
191 
192 	/* digest state */
193 	bool			hdr_digest;
194 	bool			data_digest;
195 
196 	/* TLS state */
197 	key_serial_t		tls_pskid;
198 	struct delayed_work	tls_handshake_tmo_work;
199 
200 	unsigned long           poll_end;
201 
202 	spinlock_t		state_lock;
203 	enum nvmet_tcp_queue_state state;
204 
205 	struct sockaddr_storage	sockaddr;
206 	struct sockaddr_storage	sockaddr_peer;
207 	struct work_struct	release_work;
208 
209 	int			idx;
210 	struct list_head	queue_list;
211 
212 	struct nvmet_tcp_cmd	connect;
213 
214 	struct page_frag_cache	pf_cache;
215 
216 	void (*data_ready)(struct sock *);
217 	void (*state_change)(struct sock *);
218 	void (*write_space)(struct sock *);
219 };
220 
221 struct nvmet_tcp_port {
222 	struct socket		*sock;
223 	struct work_struct	accept_work;
224 	struct nvmet_port	*nport;
225 	struct sockaddr_storage addr;
226 	void (*data_ready)(struct sock *);
227 };
228 
229 static DEFINE_IDA(nvmet_tcp_queue_ida);
230 static LIST_HEAD(nvmet_tcp_queue_list);
231 static DEFINE_MUTEX(nvmet_tcp_queue_mutex);
232 
233 static struct workqueue_struct *nvmet_tcp_wq;
234 static const struct nvmet_fabrics_ops nvmet_tcp_ops;
235 static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c);
236 static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd);
237 
238 static inline u16 nvmet_tcp_cmd_tag(struct nvmet_tcp_queue *queue,
239 		struct nvmet_tcp_cmd *cmd)
240 {
241 	if (unlikely(!queue->nr_cmds)) {
242 		/* We didn't allocate cmds yet, send 0xffff */
243 		return USHRT_MAX;
244 	}
245 
246 	return cmd - queue->cmds;
247 }
248 
249 static inline bool nvmet_tcp_has_data_in(struct nvmet_tcp_cmd *cmd)
250 {
251 	return nvme_is_write(cmd->req.cmd) &&
252 		cmd->rbytes_done < cmd->req.transfer_len;
253 }
254 
255 static inline bool nvmet_tcp_need_data_in(struct nvmet_tcp_cmd *cmd)
256 {
257 	return nvmet_tcp_has_data_in(cmd) && !cmd->req.cqe->status;
258 }
259 
260 static inline bool nvmet_tcp_need_data_out(struct nvmet_tcp_cmd *cmd)
261 {
262 	return !nvme_is_write(cmd->req.cmd) &&
263 		cmd->req.transfer_len > 0 &&
264 		!cmd->req.cqe->status;
265 }
266 
267 static inline bool nvmet_tcp_has_inline_data(struct nvmet_tcp_cmd *cmd)
268 {
269 	return nvme_is_write(cmd->req.cmd) && cmd->pdu_len &&
270 		!cmd->rbytes_done;
271 }
272 
273 /*
274  * Check whether the SGL of @cmd carries in-capsule data that the host
275  * will send right after the command PDU.  Only the offset form of the
276  * data block descriptor is in-capsule; a regular transport SGL is
277  * fetched through an R2T instead.
278  */
279 static inline bool nvmet_tcp_sgl_is_inline(struct nvmet_tcp_cmd *cmd)
280 {
281 	struct nvme_sgl_desc *sgl = &cmd->req.cmd->common.dptr.sgl;
282 	u32 len = le32_to_cpu(sgl->length);
283 
284 	if (sgl->type != ((NVME_SGL_FMT_DATA_DESC << 4) |
285 			  NVME_SGL_FMT_OFFSET))
286 		return false;
287 
288 	if (!nvme_is_write(cmd->req.cmd))
289 		return false;
290 
291 	if (!len)
292 		return false;
293 
294 	return len <= cmd->req.port->inline_data_size;
295 }
296 
297 static inline struct nvmet_tcp_cmd *
298 nvmet_tcp_get_cmd(struct nvmet_tcp_queue *queue)
299 {
300 	struct nvmet_tcp_cmd *cmd;
301 
302 	cmd = list_first_entry_or_null(&queue->free_list,
303 				struct nvmet_tcp_cmd, entry);
304 	if (!cmd)
305 		return NULL;
306 	list_del_init(&cmd->entry);
307 
308 	cmd->rbytes_done = cmd->wbytes_done = 0;
309 	cmd->pdu_len = 0;
310 	cmd->pdu_recv = 0;
311 	cmd->iov = NULL;
312 	cmd->flags = 0;
313 	return cmd;
314 }
315 
316 static inline void nvmet_tcp_put_cmd(struct nvmet_tcp_cmd *cmd)
317 {
318 	if (unlikely(cmd == &cmd->queue->connect))
319 		return;
320 
321 	list_add_tail(&cmd->entry, &cmd->queue->free_list);
322 }
323 
324 static inline int queue_cpu(struct nvmet_tcp_queue *queue)
325 {
326 	return queue->sock->sk->sk_incoming_cpu;
327 }
328 
329 static inline u8 nvmet_tcp_hdgst_len(struct nvmet_tcp_queue *queue)
330 {
331 	return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
332 }
333 
334 static inline u8 nvmet_tcp_ddgst_len(struct nvmet_tcp_queue *queue)
335 {
336 	return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
337 }
338 
339 static inline void nvmet_tcp_hdgst(void *pdu, size_t len)
340 {
341 	put_unaligned_le32(~crc32c(~0, pdu, len), pdu + len);
342 }
343 
344 static int nvmet_tcp_verify_hdgst(struct nvmet_tcp_queue *queue,
345 	void *pdu, size_t len)
346 {
347 	struct nvme_tcp_hdr *hdr = pdu;
348 	__le32 recv_digest;
349 	__le32 exp_digest;
350 
351 	if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
352 		pr_err("queue %d: header digest enabled but no header digest\n",
353 			queue->idx);
354 		return -EPROTO;
355 	}
356 
357 	recv_digest = *(__le32 *)(pdu + hdr->hlen);
358 	nvmet_tcp_hdgst(pdu, len);
359 	exp_digest = *(__le32 *)(pdu + hdr->hlen);
360 	if (recv_digest != exp_digest) {
361 		pr_err("queue %d: header digest error: recv %#x expected %#x\n",
362 			queue->idx, le32_to_cpu(recv_digest),
363 			le32_to_cpu(exp_digest));
364 		return -EPROTO;
365 	}
366 
367 	return 0;
368 }
369 
370 static int nvmet_tcp_check_ddgst(struct nvmet_tcp_queue *queue, void *pdu)
371 {
372 	struct nvme_tcp_hdr *hdr = pdu;
373 	u8 digest_len = nvmet_tcp_hdgst_len(queue);
374 	u32 len;
375 
376 	len = le32_to_cpu(hdr->plen) - hdr->hlen -
377 		(hdr->flags & NVME_TCP_F_HDGST ? digest_len : 0);
378 
379 	if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
380 		pr_err("queue %d: data digest flag is cleared\n", queue->idx);
381 		return -EPROTO;
382 	}
383 
384 	return 0;
385 }
386 
387 /* If cmd buffers are NULL, no operation is performed */
388 static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd)
389 {
390 	kfree(cmd->iov);
391 	sgl_free(cmd->req.sg);
392 	cmd->iov = NULL;
393 	cmd->req.sg = NULL;
394 }
395 
396 static int nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd)
397 {
398 	struct bio_vec *iov = cmd->iov;
399 	struct scatterlist *sg;
400 	u32 length, offset, sg_offset;
401 	unsigned int sg_remaining;
402 	int nr_pages;
403 
404 	length = cmd->pdu_len;
405 	nr_pages = DIV_ROUND_UP(length, PAGE_SIZE);
406 	offset = cmd->rbytes_done;
407 	cmd->sg_idx = offset / PAGE_SIZE;
408 	sg_offset = offset % PAGE_SIZE;
409 	if (!cmd->req.sg_cnt || cmd->sg_idx >= cmd->req.sg_cnt)
410 		return -EPROTO;
411 
412 	sg = &cmd->req.sg[cmd->sg_idx];
413 	sg_remaining = cmd->req.sg_cnt - cmd->sg_idx;
414 
415 	while (length) {
416 		if (!sg_remaining)
417 			return -EPROTO;
418 
419 		if (!sg->length || sg->length <= sg_offset)
420 			return -EPROTO;
421 
422 		u32 iov_len = min_t(u32, length, sg->length - sg_offset);
423 
424 		bvec_set_page(iov, sg_page(sg), iov_len,
425 				sg->offset + sg_offset);
426 
427 		length -= iov_len;
428 		sg = sg_next(sg);
429 		sg_remaining--;
430 		iov++;
431 		sg_offset = 0;
432 	}
433 
434 	iov_iter_bvec(&cmd->recv_msg.msg_iter, ITER_DEST, cmd->iov,
435 		      nr_pages, cmd->pdu_len);
436 	return 0;
437 }
438 
439 static void nvmet_tcp_socket_error(struct nvmet_tcp_queue *queue, int status)
440 {
441 	/*
442 	 * Keep rcv_state at RECV_ERR even for the internal -ESHUTDOWN path.
443 	 * nvmet_tcp_handle_icreq() can return -ESHUTDOWN after the ICReq has
444 	 * already been consumed and queue teardown has started.
445 	 *
446 	 * If nvmet_tcp_data_ready() or nvmet_tcp_write_space() queues
447 	 * nvmet_tcp_io_work() again before nvmet_tcp_release_queue_work()
448 	 * cancels it, the queue must not keep that old receive state.
449 	 * Otherwise the next nvmet_tcp_io_work() run can reach
450 	 * nvmet_tcp_done_recv_pdu() and try to handle the same ICReq again.
451 	 *
452 	 * That is why queue->rcv_state needs to be updated before we return.
453 	 */
454 	queue->rcv_state = NVMET_TCP_RECV_ERR;
455 	if (status == -EPIPE || status == -ECONNRESET || !queue->nvme_sq.ctrl)
456 		kernel_sock_shutdown(queue->sock, SHUT_RDWR);
457 	else
458 		nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl);
459 }
460 
461 static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd)
462 {
463 	struct nvme_sgl_desc *sgl = &cmd->req.cmd->common.dptr.sgl;
464 	u32 len = le32_to_cpu(sgl->length);
465 
466 	if (!len)
467 		return 0;
468 
469 	/*
470 	 * inline_data_size only bounds the in-capsule (type 0x01) SGL
471 	 * descriptor below. A non-inline transport SGL data-block
472 	 * descriptor skips that check entirely and would otherwise reach
473 	 * sgl_alloc() with an attacker-controlled len of up to 4 GiB,
474 	 * pinning that much kernel memory for a command that may never
475 	 * complete. Bound every descriptor type here, before allocating
476 	 * anything, using the same ceiling this file already applies to
477 	 * per-PDU H2C data.
478 	 */
479 	if (len > NVMET_TCP_MAXH2CDATA)
480 		return NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR;
481 
482 	if (sgl->type == ((NVME_SGL_FMT_DATA_DESC << 4) |
483 			  NVME_SGL_FMT_OFFSET)) {
484 		if (!nvme_is_write(cmd->req.cmd))
485 			return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
486 
487 		if (len > cmd->req.port->inline_data_size)
488 			return NVME_SC_SGL_INVALID_OFFSET | NVME_STATUS_DNR;
489 		cmd->pdu_len = len;
490 	}
491 	cmd->req.transfer_len += len;
492 
493 	cmd->req.sg = sgl_alloc(len, GFP_KERNEL | __GFP_NOWARN,
494 				&cmd->req.sg_cnt);
495 	if (!cmd->req.sg)
496 		return NVME_SC_INTERNAL;
497 	cmd->cur_sg = cmd->req.sg;
498 
499 	if (nvmet_tcp_has_data_in(cmd)) {
500 		cmd->iov = kmalloc_objs(*cmd->iov, cmd->req.sg_cnt,
501 					GFP_KERNEL | __GFP_NOWARN);
502 		if (!cmd->iov)
503 			goto err;
504 	}
505 
506 	return 0;
507 err:
508 	nvmet_tcp_free_cmd_buffers(cmd);
509 	return NVME_SC_INTERNAL;
510 }
511 
512 static void nvmet_tcp_calc_ddgst(struct nvmet_tcp_cmd *cmd)
513 {
514 	size_t total_len = cmd->req.transfer_len;
515 	struct scatterlist *sg = cmd->req.sg;
516 	u32 crc = ~0;
517 
518 	while (total_len) {
519 		size_t len = min_t(size_t, total_len, sg->length);
520 
521 		/*
522 		 * Note that the scatterlist does not contain any highmem pages,
523 		 * as it was allocated by sgl_alloc() with GFP_KERNEL.
524 		 */
525 		crc = crc32c(crc, sg_virt(sg), len);
526 		total_len -= len;
527 		sg = sg_next(sg);
528 	}
529 	cmd->exp_ddgst = cpu_to_le32(~crc);
530 }
531 
532 static void nvmet_setup_c2h_data_pdu(struct nvmet_tcp_cmd *cmd)
533 {
534 	struct nvme_tcp_data_pdu *pdu = cmd->data_pdu;
535 	struct nvmet_tcp_queue *queue = cmd->queue;
536 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
537 	u8 ddgst = nvmet_tcp_ddgst_len(cmd->queue);
538 
539 	cmd->offset = 0;
540 	cmd->state = NVMET_TCP_SEND_DATA_PDU;
541 
542 	pdu->hdr.type = nvme_tcp_c2h_data;
543 	pdu->hdr.flags = NVME_TCP_F_DATA_LAST | (queue->nvme_sq.sqhd_disabled ?
544 						NVME_TCP_F_DATA_SUCCESS : 0);
545 	pdu->hdr.hlen = sizeof(*pdu);
546 	pdu->hdr.pdo = pdu->hdr.hlen + hdgst;
547 	pdu->hdr.plen =
548 		cpu_to_le32(pdu->hdr.hlen + hdgst +
549 				cmd->req.transfer_len + ddgst);
550 	pdu->command_id = cmd->req.cqe->command_id;
551 	pdu->data_length = cpu_to_le32(cmd->req.transfer_len);
552 	pdu->data_offset = cpu_to_le32(cmd->wbytes_done);
553 
554 	if (queue->data_digest) {
555 		pdu->hdr.flags |= NVME_TCP_F_DDGST;
556 		nvmet_tcp_calc_ddgst(cmd);
557 	}
558 
559 	if (cmd->queue->hdr_digest) {
560 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
561 		nvmet_tcp_hdgst(pdu, sizeof(*pdu));
562 	}
563 }
564 
565 static void nvmet_setup_r2t_pdu(struct nvmet_tcp_cmd *cmd)
566 {
567 	struct nvme_tcp_r2t_pdu *pdu = cmd->r2t_pdu;
568 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
569 
570 	cmd->offset = 0;
571 	cmd->state = NVMET_TCP_SEND_R2T;
572 
573 	pdu->hdr.type = nvme_tcp_r2t;
574 	pdu->hdr.flags = 0;
575 	pdu->hdr.hlen = sizeof(*pdu);
576 	pdu->hdr.pdo = 0;
577 	pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
578 
579 	pdu->command_id = cmd->req.cmd->common.command_id;
580 	pdu->ttag = nvmet_tcp_cmd_tag(cmd->queue, cmd);
581 	pdu->r2t_length = cpu_to_le32(cmd->req.transfer_len - cmd->rbytes_done);
582 	pdu->r2t_offset = cpu_to_le32(cmd->rbytes_done);
583 	if (cmd->queue->hdr_digest) {
584 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
585 		nvmet_tcp_hdgst(pdu, sizeof(*pdu));
586 	}
587 }
588 
589 static void nvmet_setup_response_pdu(struct nvmet_tcp_cmd *cmd)
590 {
591 	struct nvme_tcp_rsp_pdu *pdu = cmd->rsp_pdu;
592 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
593 
594 	cmd->offset = 0;
595 	cmd->state = NVMET_TCP_SEND_RESPONSE;
596 
597 	pdu->hdr.type = nvme_tcp_rsp;
598 	pdu->hdr.flags = 0;
599 	pdu->hdr.hlen = sizeof(*pdu);
600 	pdu->hdr.pdo = 0;
601 	pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
602 	if (cmd->queue->hdr_digest) {
603 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
604 		nvmet_tcp_hdgst(pdu, sizeof(*pdu));
605 	}
606 }
607 
608 static void nvmet_tcp_process_resp_list(struct nvmet_tcp_queue *queue)
609 {
610 	struct llist_node *node;
611 	struct nvmet_tcp_cmd *cmd;
612 
613 	for (node = llist_del_all(&queue->resp_list); node; node = node->next) {
614 		cmd = llist_entry(node, struct nvmet_tcp_cmd, lentry);
615 		list_add(&cmd->entry, &queue->resp_send_list);
616 		queue->send_list_len++;
617 	}
618 }
619 
620 static struct nvmet_tcp_cmd *nvmet_tcp_fetch_cmd(struct nvmet_tcp_queue *queue)
621 {
622 	queue->snd_cmd = list_first_entry_or_null(&queue->resp_send_list,
623 				struct nvmet_tcp_cmd, entry);
624 	if (!queue->snd_cmd) {
625 		nvmet_tcp_process_resp_list(queue);
626 		queue->snd_cmd =
627 			list_first_entry_or_null(&queue->resp_send_list,
628 					struct nvmet_tcp_cmd, entry);
629 		if (unlikely(!queue->snd_cmd))
630 			return NULL;
631 	}
632 
633 	list_del_init(&queue->snd_cmd->entry);
634 	queue->send_list_len--;
635 
636 	if (nvmet_tcp_need_data_out(queue->snd_cmd))
637 		nvmet_setup_c2h_data_pdu(queue->snd_cmd);
638 	else if (nvmet_tcp_need_data_in(queue->snd_cmd))
639 		nvmet_setup_r2t_pdu(queue->snd_cmd);
640 	else
641 		nvmet_setup_response_pdu(queue->snd_cmd);
642 
643 	return queue->snd_cmd;
644 }
645 
646 static void nvmet_tcp_queue_response(struct nvmet_req *req)
647 {
648 	struct nvmet_tcp_cmd *cmd =
649 		container_of(req, struct nvmet_tcp_cmd, req);
650 	struct nvmet_tcp_queue	*queue = cmd->queue;
651 	enum nvmet_tcp_recv_state queue_state;
652 	struct nvmet_tcp_cmd *queue_cmd;
653 
654 	/* Pairs with store_release in nvmet_prepare_receive_pdu() */
655 	queue_state = smp_load_acquire(&queue->rcv_state);
656 	queue_cmd = READ_ONCE(queue->cmd);
657 
658 	if (unlikely(cmd == queue_cmd)) {
659 		/*
660 		 * Wait for in-capsule data before processing the response.
661 		 * This can happen before nvmet_req_init() has completed, so
662 		 * only look at the SGL descriptor itself.
663 		 */
664 		if (queue_state == NVMET_TCP_RECV_PDU &&
665 		    nvmet_tcp_sgl_is_inline(cmd))
666 			return;
667 	}
668 
669 	llist_add(&cmd->lentry, &queue->resp_list);
670 	queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &cmd->queue->io_work);
671 }
672 
673 static void nvmet_tcp_execute_request(struct nvmet_tcp_cmd *cmd)
674 {
675 	if (unlikely(cmd->flags & NVMET_TCP_F_INIT_FAILED))
676 		nvmet_tcp_queue_response(&cmd->req);
677 	else
678 		cmd->req.execute(&cmd->req);
679 }
680 
681 static int nvmet_try_send_data_pdu(struct nvmet_tcp_cmd *cmd)
682 {
683 	struct msghdr msg = {
684 		.msg_flags = MSG_DONTWAIT | MSG_MORE | MSG_SPLICE_PAGES,
685 	};
686 	struct bio_vec bvec;
687 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
688 	int left = sizeof(*cmd->data_pdu) - cmd->offset + hdgst;
689 	int ret;
690 
691 	bvec_set_virt(&bvec, (void *)cmd->data_pdu + cmd->offset, left);
692 	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, left);
693 	ret = sock_sendmsg(cmd->queue->sock, &msg);
694 	if (ret <= 0)
695 		return ret;
696 
697 	cmd->offset += ret;
698 	left -= ret;
699 
700 	if (left)
701 		return -EAGAIN;
702 
703 	cmd->state = NVMET_TCP_SEND_DATA;
704 	cmd->offset  = 0;
705 	return 1;
706 }
707 
708 static int nvmet_try_send_data(struct nvmet_tcp_cmd *cmd, bool last_in_batch)
709 {
710 	struct nvmet_tcp_queue *queue = cmd->queue;
711 	int ret;
712 
713 	while (cmd->cur_sg) {
714 		struct msghdr msg = {
715 			.msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES,
716 		};
717 		struct page *page = sg_page(cmd->cur_sg);
718 		struct bio_vec bvec;
719 		u32 left = cmd->cur_sg->length - cmd->offset;
720 
721 		if ((!last_in_batch && cmd->queue->send_list_len) ||
722 		    cmd->wbytes_done + left < cmd->req.transfer_len ||
723 		    queue->data_digest || !queue->nvme_sq.sqhd_disabled)
724 			msg.msg_flags |= MSG_MORE;
725 
726 		bvec_set_page(&bvec, page, left, cmd->offset);
727 		iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, left);
728 		ret = sock_sendmsg(cmd->queue->sock, &msg);
729 		if (ret <= 0)
730 			return ret;
731 
732 		cmd->offset += ret;
733 		cmd->wbytes_done += ret;
734 
735 		/* Done with sg?*/
736 		if (cmd->offset == cmd->cur_sg->length) {
737 			cmd->cur_sg = sg_next(cmd->cur_sg);
738 			cmd->offset = 0;
739 		}
740 	}
741 
742 	if (queue->data_digest) {
743 		cmd->state = NVMET_TCP_SEND_DDGST;
744 		cmd->offset = 0;
745 	} else {
746 		if (queue->nvme_sq.sqhd_disabled) {
747 			cmd->queue->snd_cmd = NULL;
748 			nvmet_tcp_put_cmd(cmd);
749 		} else {
750 			nvmet_setup_response_pdu(cmd);
751 		}
752 	}
753 
754 	if (queue->nvme_sq.sqhd_disabled)
755 		nvmet_tcp_free_cmd_buffers(cmd);
756 
757 	return 1;
758 
759 }
760 
761 static int nvmet_try_send_response(struct nvmet_tcp_cmd *cmd,
762 		bool last_in_batch)
763 {
764 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES, };
765 	struct bio_vec bvec;
766 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
767 	int left = sizeof(*cmd->rsp_pdu) - cmd->offset + hdgst;
768 	int ret;
769 
770 	if (!last_in_batch && cmd->queue->send_list_len)
771 		msg.msg_flags |= MSG_MORE;
772 	else
773 		msg.msg_flags |= MSG_EOR;
774 
775 	bvec_set_virt(&bvec, (void *)cmd->rsp_pdu + cmd->offset, left);
776 	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, left);
777 	ret = sock_sendmsg(cmd->queue->sock, &msg);
778 	if (ret <= 0)
779 		return ret;
780 	cmd->offset += ret;
781 	left -= ret;
782 
783 	if (left)
784 		return -EAGAIN;
785 
786 	nvmet_tcp_free_cmd_buffers(cmd);
787 	cmd->queue->snd_cmd = NULL;
788 	nvmet_tcp_put_cmd(cmd);
789 	return 1;
790 }
791 
792 static int nvmet_try_send_r2t(struct nvmet_tcp_cmd *cmd, bool last_in_batch)
793 {
794 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES, };
795 	struct bio_vec bvec;
796 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
797 	int left = sizeof(*cmd->r2t_pdu) - cmd->offset + hdgst;
798 	int ret;
799 
800 	if (!last_in_batch && cmd->queue->send_list_len)
801 		msg.msg_flags |= MSG_MORE;
802 	else
803 		msg.msg_flags |= MSG_EOR;
804 
805 	bvec_set_virt(&bvec, (void *)cmd->r2t_pdu + cmd->offset, left);
806 	iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, left);
807 	ret = sock_sendmsg(cmd->queue->sock, &msg);
808 	if (ret <= 0)
809 		return ret;
810 	cmd->offset += ret;
811 	left -= ret;
812 
813 	if (left)
814 		return -EAGAIN;
815 
816 	cmd->queue->snd_cmd = NULL;
817 	cmd->flags |= NVMET_TCP_F_R2T_SENT;
818 	return 1;
819 }
820 
821 static int nvmet_try_send_ddgst(struct nvmet_tcp_cmd *cmd, bool last_in_batch)
822 {
823 	struct nvmet_tcp_queue *queue = cmd->queue;
824 	int left = NVME_TCP_DIGEST_LENGTH - cmd->offset;
825 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
826 	struct kvec iov = {
827 		.iov_base = (u8 *)&cmd->exp_ddgst + cmd->offset,
828 		.iov_len = left
829 	};
830 	int ret;
831 
832 	if (!last_in_batch && cmd->queue->send_list_len)
833 		msg.msg_flags |= MSG_MORE;
834 	else
835 		msg.msg_flags |= MSG_EOR;
836 
837 	ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
838 	if (unlikely(ret <= 0))
839 		return ret;
840 
841 	cmd->offset += ret;
842 	left -= ret;
843 
844 	if (left)
845 		return -EAGAIN;
846 
847 	if (queue->nvme_sq.sqhd_disabled) {
848 		cmd->queue->snd_cmd = NULL;
849 		nvmet_tcp_put_cmd(cmd);
850 	} else {
851 		nvmet_setup_response_pdu(cmd);
852 	}
853 	return 1;
854 }
855 
856 static int nvmet_tcp_try_send_one(struct nvmet_tcp_queue *queue,
857 		bool last_in_batch)
858 {
859 	struct nvmet_tcp_cmd *cmd = queue->snd_cmd;
860 	int ret = 0;
861 
862 	if (!cmd || queue->state == NVMET_TCP_Q_DISCONNECTING) {
863 		cmd = nvmet_tcp_fetch_cmd(queue);
864 		if (unlikely(!cmd))
865 			return 0;
866 	}
867 
868 	if (cmd->state == NVMET_TCP_SEND_DATA_PDU) {
869 		ret = nvmet_try_send_data_pdu(cmd);
870 		if (ret <= 0)
871 			goto done_send;
872 	}
873 
874 	if (cmd->state == NVMET_TCP_SEND_DATA) {
875 		ret = nvmet_try_send_data(cmd, last_in_batch);
876 		if (ret <= 0)
877 			goto done_send;
878 	}
879 
880 	if (cmd->state == NVMET_TCP_SEND_DDGST) {
881 		ret = nvmet_try_send_ddgst(cmd, last_in_batch);
882 		if (ret <= 0)
883 			goto done_send;
884 	}
885 
886 	if (cmd->state == NVMET_TCP_SEND_R2T) {
887 		ret = nvmet_try_send_r2t(cmd, last_in_batch);
888 		if (ret <= 0)
889 			goto done_send;
890 	}
891 
892 	if (cmd->state == NVMET_TCP_SEND_RESPONSE)
893 		ret = nvmet_try_send_response(cmd, last_in_batch);
894 
895 done_send:
896 	if (ret < 0) {
897 		if (ret == -EAGAIN)
898 			return 0;
899 		return ret;
900 	}
901 
902 	return 1;
903 }
904 
905 static int nvmet_tcp_try_send(struct nvmet_tcp_queue *queue,
906 		int budget, int *sends)
907 {
908 	int i, ret = 0;
909 
910 	for (i = 0; i < budget; i++) {
911 		ret = nvmet_tcp_try_send_one(queue, i == budget - 1);
912 		if (unlikely(ret < 0)) {
913 			nvmet_tcp_socket_error(queue, ret);
914 			goto done;
915 		} else if (ret == 0) {
916 			break;
917 		}
918 		(*sends)++;
919 	}
920 done:
921 	return ret;
922 }
923 
924 static void nvmet_prepare_receive_pdu(struct nvmet_tcp_queue *queue)
925 {
926 	queue->offset = 0;
927 	queue->left = sizeof(struct nvme_tcp_hdr);
928 	WRITE_ONCE(queue->cmd, NULL);
929 	/* Ensure rcv_state is visible only after queue->cmd is set */
930 	smp_store_release(&queue->rcv_state, NVMET_TCP_RECV_PDU);
931 }
932 
933 static int nvmet_tcp_handle_icreq(struct nvmet_tcp_queue *queue)
934 {
935 	struct nvme_tcp_icreq_pdu *icreq = &queue->pdu.icreq;
936 	struct nvme_tcp_icresp_pdu *icresp = &queue->pdu.icresp;
937 	struct msghdr msg = {};
938 	struct kvec iov;
939 	int ret;
940 
941 	if (le32_to_cpu(icreq->hdr.plen) != sizeof(struct nvme_tcp_icreq_pdu)) {
942 		pr_err("bad nvme-tcp pdu length (%d)\n",
943 			le32_to_cpu(icreq->hdr.plen));
944 		return -EPROTO;
945 	}
946 
947 	if (icreq->pfv != NVME_TCP_PFV_1_0) {
948 		pr_err("queue %d: bad pfv %d\n", queue->idx, icreq->pfv);
949 		return -EPROTO;
950 	}
951 
952 	if (icreq->hpda != 0) {
953 		pr_err("queue %d: unsupported hpda %d\n", queue->idx,
954 			icreq->hpda);
955 		return -EPROTO;
956 	}
957 
958 	queue->hdr_digest = !!(icreq->digest & NVME_TCP_HDR_DIGEST_ENABLE);
959 	queue->data_digest = !!(icreq->digest & NVME_TCP_DATA_DIGEST_ENABLE);
960 
961 	memset(icresp, 0, sizeof(*icresp));
962 	icresp->hdr.type = nvme_tcp_icresp;
963 	icresp->hdr.hlen = sizeof(*icresp);
964 	icresp->hdr.pdo = 0;
965 	icresp->hdr.plen = cpu_to_le32(icresp->hdr.hlen);
966 	icresp->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
967 	icresp->maxdata = cpu_to_le32(NVMET_TCP_MAXH2CDATA);
968 	icresp->cpda = 0;
969 	if (queue->hdr_digest)
970 		icresp->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
971 	if (queue->data_digest)
972 		icresp->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
973 
974 	iov.iov_base = icresp;
975 	iov.iov_len = sizeof(*icresp);
976 	ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
977 	if (ret < 0) {
978 		spin_lock_bh(&queue->state_lock);
979 		if (queue->state == NVMET_TCP_Q_DISCONNECTING) {
980 			spin_unlock_bh(&queue->state_lock);
981 			return -ESHUTDOWN;
982 		}
983 		queue->state = NVMET_TCP_Q_FAILED;
984 		spin_unlock_bh(&queue->state_lock);
985 		return ret; /* queue removal will cleanup */
986 	}
987 
988 	spin_lock_bh(&queue->state_lock);
989 	if (queue->state == NVMET_TCP_Q_DISCONNECTING) {
990 		spin_unlock_bh(&queue->state_lock);
991 		/* Tell nvmet_tcp_socket_error() teardown is in progress. */
992 		return -ESHUTDOWN;
993 	}
994 	queue->state = NVMET_TCP_Q_LIVE;
995 	spin_unlock_bh(&queue->state_lock);
996 	nvmet_prepare_receive_pdu(queue);
997 	return 0;
998 }
999 
1000 static int nvmet_tcp_handle_req_failure(struct nvmet_tcp_queue *queue,
1001 		struct nvmet_tcp_cmd *cmd, struct nvmet_req *req)
1002 {
1003 	int ret;
1004 
1005 	/*
1006 	 * This command has not been processed yet, hence we are trying to
1007 	 * figure out if there is still pending data left to receive. If
1008 	 * we don't, we can simply prepare for the next pdu and bail out,
1009 	 * otherwise we will need to prepare a buffer and receive the
1010 	 * stale data before continuing forward.
1011 	 */
1012 	if (!nvmet_tcp_sgl_is_inline(cmd)) {
1013 		nvmet_prepare_receive_pdu(queue);
1014 		return 0;
1015 	}
1016 
1017 	ret = nvmet_tcp_map_data(cmd);
1018 	if (unlikely(ret)) {
1019 		pr_err("queue %d: failed to map data\n", queue->idx);
1020 		return -EPROTO;
1021 	}
1022 
1023 	queue->rcv_state = NVMET_TCP_RECV_DATA;
1024 	cmd->flags |= NVMET_TCP_F_INIT_FAILED;
1025 	ret = nvmet_tcp_build_pdu_iovec(cmd);
1026 	if (unlikely(ret))
1027 		pr_err("queue %d: failed to build PDU iovec\n", queue->idx);
1028 
1029 	return ret;
1030 }
1031 
1032 static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue)
1033 {
1034 	struct nvme_tcp_data_pdu *data = &queue->pdu.data;
1035 	struct nvmet_tcp_cmd *cmd;
1036 	unsigned int exp_data_len;
1037 
1038 	if (likely(queue->nr_cmds)) {
1039 		if (unlikely(data->ttag >= queue->nr_cmds)) {
1040 			pr_err("queue %d: received out of bound ttag %u, nr_cmds %u\n",
1041 				queue->idx, data->ttag, queue->nr_cmds);
1042 			goto err_proto;
1043 		}
1044 		cmd = &queue->cmds[data->ttag];
1045 	} else {
1046 		cmd = &queue->connect;
1047 	}
1048 
1049 	if (unlikely(!(cmd->flags & NVMET_TCP_F_R2T_SENT))) {
1050 		pr_err("queue %d: unsolicited H2CData (ttag %u)\n",
1051 		       queue->idx, data->ttag);
1052 		goto err_proto;
1053 	}
1054 
1055 	if (le32_to_cpu(data->data_offset) != cmd->rbytes_done) {
1056 		pr_err("ttag %u unexpected data offset %u (expected %u)\n",
1057 			data->ttag, le32_to_cpu(data->data_offset),
1058 			cmd->rbytes_done);
1059 		goto err_proto;
1060 	}
1061 
1062 	exp_data_len = le32_to_cpu(data->hdr.plen) -
1063 			nvmet_tcp_hdgst_len(queue) -
1064 			nvmet_tcp_ddgst_len(queue) -
1065 			sizeof(*data);
1066 
1067 	cmd->pdu_len = le32_to_cpu(data->data_length);
1068 	if (unlikely(cmd->pdu_len != exp_data_len ||
1069 		     cmd->pdu_len == 0 ||
1070 		     cmd->pdu_len > NVMET_TCP_MAXH2CDATA)) {
1071 		pr_err("H2CData PDU len %u is invalid\n", cmd->pdu_len);
1072 		goto err_proto;
1073 	}
1074        /*
1075 	* Ensure command data structures are initialized. We must check both
1076 	* cmd->req.sg and cmd->iov because they can have different NULL states:
1077 	* - Uninitialized commands: both NULL
1078 	* - READ commands: cmd->req.sg allocated, cmd->iov NULL
1079 	* - WRITE commands: both allocated
1080 	*/
1081 	if (unlikely(!cmd->req.sg || !cmd->iov)) {
1082 		pr_err("queue %d: H2CData PDU received for invalid command state (ttag %u)\n",
1083 			queue->idx, data->ttag);
1084 		goto err_proto;
1085 	}
1086 	cmd->pdu_recv = 0;
1087 	if (unlikely(nvmet_tcp_build_pdu_iovec(cmd))) {
1088 		pr_err("queue %d: failed to build PDU iovec\n", queue->idx);
1089 		goto err_proto;
1090 	}
1091 	queue->cmd = cmd;
1092 	queue->rcv_state = NVMET_TCP_RECV_DATA;
1093 
1094 	return 0;
1095 
1096 err_proto:
1097 	/* FIXME: use proper transport errors */
1098 	return -EPROTO;
1099 }
1100 
1101 static int nvmet_tcp_done_recv_pdu(struct nvmet_tcp_queue *queue)
1102 {
1103 	struct nvme_tcp_hdr *hdr = &queue->pdu.cmd.hdr;
1104 	struct nvme_command *nvme_cmd = &queue->pdu.cmd.cmd;
1105 	struct nvmet_req *req;
1106 	int ret;
1107 
1108 	if (unlikely(queue->state == NVMET_TCP_Q_CONNECTING)) {
1109 		if (hdr->type != nvme_tcp_icreq) {
1110 			pr_err("unexpected pdu type (%d) before icreq\n",
1111 				hdr->type);
1112 			return -EPROTO;
1113 		}
1114 		return nvmet_tcp_handle_icreq(queue);
1115 	}
1116 
1117 	if (unlikely(hdr->type == nvme_tcp_icreq)) {
1118 		pr_err("queue %d: received icreq pdu in state %d\n",
1119 			queue->idx, queue->state);
1120 		return -EPROTO;
1121 	}
1122 
1123 	if (hdr->type == nvme_tcp_h2c_data) {
1124 		ret = nvmet_tcp_handle_h2c_data_pdu(queue);
1125 		if (unlikely(ret))
1126 			return ret;
1127 		return 0;
1128 	}
1129 
1130 	queue->cmd = nvmet_tcp_get_cmd(queue);
1131 	if (unlikely(!queue->cmd)) {
1132 		/* This should never happen */
1133 		pr_err("queue %d: out of commands (%d) send_list_len: %d, opcode: %d",
1134 			queue->idx, queue->nr_cmds, queue->send_list_len,
1135 			nvme_cmd->common.opcode);
1136 		return -ENOMEM;
1137 	}
1138 
1139 	req = &queue->cmd->req;
1140 	memcpy(req->cmd, nvme_cmd, sizeof(*nvme_cmd));
1141 
1142 	if (unlikely(!nvmet_req_init(req, &queue->nvme_sq, &nvmet_tcp_ops))) {
1143 		pr_err("failed cmd %p id %d opcode %d, data_len: %d, status: %04x\n",
1144 			req->cmd, req->cmd->common.command_id,
1145 			req->cmd->common.opcode,
1146 			le32_to_cpu(req->cmd->common.dptr.sgl.length),
1147 			le16_to_cpu(req->cqe->status));
1148 
1149 		return nvmet_tcp_handle_req_failure(queue, queue->cmd, req);
1150 	}
1151 
1152 	ret = nvmet_tcp_map_data(queue->cmd);
1153 	if (unlikely(ret)) {
1154 		pr_err("queue %d: failed to map data\n", queue->idx);
1155 		if (nvmet_tcp_has_inline_data(queue->cmd))
1156 			return -EPROTO;
1157 
1158 		nvmet_req_complete(req, ret);
1159 		ret = -EAGAIN;
1160 		goto out;
1161 	}
1162 
1163 	if (nvmet_tcp_need_data_in(queue->cmd)) {
1164 		if (nvmet_tcp_has_inline_data(queue->cmd)) {
1165 			queue->rcv_state = NVMET_TCP_RECV_DATA;
1166 			ret = nvmet_tcp_build_pdu_iovec(queue->cmd);
1167 			if (unlikely(ret))
1168 				pr_err("queue %d: failed to build PDU iovec\n",
1169 					queue->idx);
1170 			return ret;
1171 		}
1172 		/* send back R2T */
1173 		nvmet_tcp_queue_response(&queue->cmd->req);
1174 		goto out;
1175 	}
1176 
1177 	queue->cmd->req.execute(&queue->cmd->req);
1178 out:
1179 	nvmet_prepare_receive_pdu(queue);
1180 	return ret;
1181 }
1182 
1183 static const u8 nvme_tcp_pdu_sizes[] = {
1184 	[nvme_tcp_icreq]	= sizeof(struct nvme_tcp_icreq_pdu),
1185 	[nvme_tcp_cmd]		= sizeof(struct nvme_tcp_cmd_pdu),
1186 	[nvme_tcp_h2c_data]	= sizeof(struct nvme_tcp_data_pdu),
1187 };
1188 
1189 static inline u8 nvmet_tcp_pdu_size(u8 type)
1190 {
1191 	size_t idx = type;
1192 
1193 	return (idx < ARRAY_SIZE(nvme_tcp_pdu_sizes) &&
1194 		nvme_tcp_pdu_sizes[idx]) ?
1195 			nvme_tcp_pdu_sizes[idx] : 0;
1196 }
1197 
1198 static inline bool nvmet_tcp_pdu_valid(u8 type)
1199 {
1200 	switch (type) {
1201 	case nvme_tcp_icreq:
1202 	case nvme_tcp_cmd:
1203 	case nvme_tcp_h2c_data:
1204 		/* fallthru */
1205 		return true;
1206 	}
1207 
1208 	return false;
1209 }
1210 
1211 static int nvmet_tcp_tls_record_ok(struct nvmet_tcp_queue *queue,
1212 		struct msghdr *msg, char *cbuf)
1213 {
1214 	struct cmsghdr *cmsg = (struct cmsghdr *)cbuf;
1215 	u8 ctype, level, description;
1216 	int ret = 0;
1217 
1218 	ctype = tls_get_record_type(queue->sock->sk, cmsg);
1219 	switch (ctype) {
1220 	case 0:
1221 		break;
1222 	case TLS_RECORD_TYPE_DATA:
1223 		break;
1224 	case TLS_RECORD_TYPE_ALERT:
1225 		tls_alert_recv(queue->sock->sk, msg, &level, &description);
1226 		if (level == TLS_ALERT_LEVEL_FATAL) {
1227 			pr_err("queue %d: TLS Alert desc %u\n",
1228 			       queue->idx, description);
1229 			ret = -ENOTCONN;
1230 		} else {
1231 			pr_warn("queue %d: TLS Alert desc %u\n",
1232 			       queue->idx, description);
1233 			ret = -EAGAIN;
1234 		}
1235 		break;
1236 	default:
1237 		/* discard this record type */
1238 		pr_err("queue %d: TLS record %d unhandled\n",
1239 		       queue->idx, ctype);
1240 		ret = -EAGAIN;
1241 		break;
1242 	}
1243 	return ret;
1244 }
1245 
1246 static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue)
1247 {
1248 	struct nvme_tcp_hdr *hdr = &queue->pdu.cmd.hdr;
1249 	int len, ret;
1250 	struct kvec iov;
1251 	char cbuf[CMSG_LEN(sizeof(char))] = {};
1252 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
1253 
1254 recv:
1255 	iov.iov_base = (void *)&queue->pdu + queue->offset;
1256 	iov.iov_len = queue->left;
1257 	if (queue->tls_pskid) {
1258 		msg.msg_control = cbuf;
1259 		msg.msg_controllen = sizeof(cbuf);
1260 	}
1261 	len = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1262 			iov.iov_len, msg.msg_flags);
1263 	if (unlikely(len < 0))
1264 		return len;
1265 	if (queue->tls_pskid) {
1266 		ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
1267 		if (ret < 0)
1268 			return ret;
1269 	}
1270 
1271 	queue->offset += len;
1272 	queue->left -= len;
1273 	if (queue->left)
1274 		return -EAGAIN;
1275 
1276 	if (queue->offset == sizeof(struct nvme_tcp_hdr)) {
1277 		u8 hdgst = nvmet_tcp_hdgst_len(queue);
1278 
1279 		if (unlikely(!nvmet_tcp_pdu_valid(hdr->type))) {
1280 			pr_err("unexpected pdu type %d\n", hdr->type);
1281 			return -EIO;
1282 		}
1283 
1284 		if (unlikely(hdr->hlen != nvmet_tcp_pdu_size(hdr->type))) {
1285 			pr_err("pdu %d bad hlen %d\n", hdr->type, hdr->hlen);
1286 			return -EIO;
1287 		}
1288 
1289 		queue->left = hdr->hlen - queue->offset + hdgst;
1290 		if (queue->left > sizeof(queue->pdu) - queue->offset)
1291 			return -EPROTO;
1292 		goto recv;
1293 	}
1294 
1295 	if (queue->hdr_digest &&
1296 	    nvmet_tcp_verify_hdgst(queue, &queue->pdu, hdr->hlen))
1297 		return -EPROTO;
1298 
1299 	if (queue->data_digest &&
1300 	    nvmet_tcp_check_ddgst(queue, &queue->pdu))
1301 		return -EPROTO;
1302 
1303 	return nvmet_tcp_done_recv_pdu(queue);
1304 }
1305 
1306 static void nvmet_tcp_prep_recv_ddgst(struct nvmet_tcp_cmd *cmd)
1307 {
1308 	struct nvmet_tcp_queue *queue = cmd->queue;
1309 
1310 	nvmet_tcp_calc_ddgst(cmd);
1311 	queue->offset = 0;
1312 	queue->left = NVME_TCP_DIGEST_LENGTH;
1313 	queue->rcv_state = NVMET_TCP_RECV_DDGST;
1314 }
1315 
1316 static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue)
1317 {
1318 	struct nvmet_tcp_cmd  *cmd = queue->cmd;
1319 	int len, ret;
1320 
1321 	while (msg_data_left(&cmd->recv_msg)) {
1322 		if (queue->tls_pskid) {
1323 			/* The last receive has used it up */
1324 			cmd->recv_msg.msg_control = cmd->recv_cbuf;
1325 			cmd->recv_msg.msg_controllen = sizeof(cmd->recv_cbuf);
1326 		}
1327 		len = sock_recvmsg(cmd->queue->sock, &cmd->recv_msg,
1328 			cmd->recv_msg.msg_flags);
1329 		if (len <= 0)
1330 			return len;
1331 		if (queue->tls_pskid) {
1332 			ret = nvmet_tcp_tls_record_ok(cmd->queue,
1333 					&cmd->recv_msg, cmd->recv_cbuf);
1334 			if (ret < 0)
1335 				return ret;
1336 		}
1337 
1338 		cmd->pdu_recv += len;
1339 		cmd->rbytes_done += len;
1340 	}
1341 
1342 	if (queue->data_digest) {
1343 		nvmet_tcp_prep_recv_ddgst(cmd);
1344 		return 0;
1345 	}
1346 
1347 	if (cmd->rbytes_done == cmd->req.transfer_len)
1348 		nvmet_tcp_execute_request(cmd);
1349 
1350 	nvmet_prepare_receive_pdu(queue);
1351 	return 0;
1352 }
1353 
1354 static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue)
1355 {
1356 	struct nvmet_tcp_cmd *cmd = queue->cmd;
1357 	int ret, len;
1358 	char cbuf[CMSG_LEN(sizeof(char))] = {};
1359 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
1360 	struct kvec iov = {
1361 		.iov_base = (void *)&cmd->recv_ddgst + queue->offset,
1362 		.iov_len = queue->left
1363 	};
1364 
1365 	if (queue->tls_pskid) {
1366 		msg.msg_control = cbuf;
1367 		msg.msg_controllen = sizeof(cbuf);
1368 	}
1369 	len = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1370 			iov.iov_len, msg.msg_flags);
1371 	if (unlikely(len < 0))
1372 		return len;
1373 	if (queue->tls_pskid) {
1374 		ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
1375 		if (ret < 0)
1376 			return ret;
1377 	}
1378 
1379 	queue->offset += len;
1380 	queue->left -= len;
1381 	if (queue->left)
1382 		return -EAGAIN;
1383 
1384 	if (queue->data_digest && cmd->exp_ddgst != cmd->recv_ddgst) {
1385 		pr_err("queue %d: cmd %d pdu (%d) data digest error: recv %#x expected %#x\n",
1386 			queue->idx, cmd->req.cmd->common.command_id,
1387 			queue->pdu.cmd.hdr.type, le32_to_cpu(cmd->recv_ddgst),
1388 			le32_to_cpu(cmd->exp_ddgst));
1389 		if (!(cmd->flags & NVMET_TCP_F_INIT_FAILED)) {
1390 			cmd->req.cqe->status = NVME_SC_CMD_SEQ_ERROR;
1391 			nvmet_req_uninit(&cmd->req);
1392 		}
1393 		nvmet_tcp_free_cmd_buffers(cmd);
1394 		ret = -EPROTO;
1395 		goto out;
1396 	}
1397 
1398 	if (cmd->rbytes_done == cmd->req.transfer_len)
1399 		nvmet_tcp_execute_request(cmd);
1400 
1401 	ret = 0;
1402 out:
1403 	nvmet_prepare_receive_pdu(queue);
1404 	return ret;
1405 }
1406 
1407 static int nvmet_tcp_try_recv_one(struct nvmet_tcp_queue *queue)
1408 {
1409 	int result = 0;
1410 
1411 	if (unlikely(queue->rcv_state == NVMET_TCP_RECV_ERR))
1412 		return 0;
1413 
1414 	if (queue->rcv_state == NVMET_TCP_RECV_PDU) {
1415 		result = nvmet_tcp_try_recv_pdu(queue);
1416 		if (result != 0)
1417 			goto done_recv;
1418 	}
1419 
1420 	if (queue->rcv_state == NVMET_TCP_RECV_DATA) {
1421 		result = nvmet_tcp_try_recv_data(queue);
1422 		if (result != 0)
1423 			goto done_recv;
1424 	}
1425 
1426 	if (queue->rcv_state == NVMET_TCP_RECV_DDGST) {
1427 		result = nvmet_tcp_try_recv_ddgst(queue);
1428 		if (result != 0)
1429 			goto done_recv;
1430 	}
1431 
1432 done_recv:
1433 	if (result < 0) {
1434 		if (result == -EAGAIN)
1435 			return 0;
1436 		return result;
1437 	}
1438 	return 1;
1439 }
1440 
1441 static int nvmet_tcp_try_recv(struct nvmet_tcp_queue *queue,
1442 		int budget, int *recvs)
1443 {
1444 	int i, ret = 0;
1445 
1446 	for (i = 0; i < budget; i++) {
1447 		ret = nvmet_tcp_try_recv_one(queue);
1448 		if (unlikely(ret < 0)) {
1449 			nvmet_tcp_socket_error(queue, ret);
1450 			goto done;
1451 		} else if (ret == 0) {
1452 			break;
1453 		}
1454 		(*recvs)++;
1455 	}
1456 done:
1457 	return ret;
1458 }
1459 
1460 static void nvmet_tcp_release_queue(struct kref *kref)
1461 {
1462 	struct nvmet_tcp_queue *queue =
1463 		container_of(kref, struct nvmet_tcp_queue, kref);
1464 
1465 	WARN_ON(queue->state != NVMET_TCP_Q_DISCONNECTING);
1466 	queue_work(nvmet_wq, &queue->release_work);
1467 }
1468 
1469 static void nvmet_tcp_schedule_release_queue(struct nvmet_tcp_queue *queue)
1470 {
1471 	spin_lock_bh(&queue->state_lock);
1472 	if (queue->state == NVMET_TCP_Q_TLS_HANDSHAKE) {
1473 		/* Socket closed during handshake */
1474 		tls_handshake_cancel(queue->sock->sk);
1475 	}
1476 	if (queue->state != NVMET_TCP_Q_DISCONNECTING) {
1477 		queue->state = NVMET_TCP_Q_DISCONNECTING;
1478 		kref_put(&queue->kref, nvmet_tcp_release_queue);
1479 	}
1480 	spin_unlock_bh(&queue->state_lock);
1481 }
1482 
1483 static inline void nvmet_tcp_arm_queue_deadline(struct nvmet_tcp_queue *queue)
1484 {
1485 	queue->poll_end = jiffies + usecs_to_jiffies(idle_poll_period_usecs);
1486 }
1487 
1488 static bool nvmet_tcp_check_queue_deadline(struct nvmet_tcp_queue *queue,
1489 		int ops)
1490 {
1491 	if (!idle_poll_period_usecs)
1492 		return false;
1493 
1494 	if (ops)
1495 		nvmet_tcp_arm_queue_deadline(queue);
1496 
1497 	return !time_after(jiffies, queue->poll_end);
1498 }
1499 
1500 static void nvmet_tcp_io_work(struct work_struct *w)
1501 {
1502 	struct nvmet_tcp_queue *queue =
1503 		container_of(w, struct nvmet_tcp_queue, io_work);
1504 	bool pending;
1505 	int ret, ops = 0;
1506 
1507 	do {
1508 		pending = false;
1509 
1510 		ret = nvmet_tcp_try_recv(queue, NVMET_TCP_RECV_BUDGET, &ops);
1511 		if (ret > 0)
1512 			pending = true;
1513 		else if (ret < 0)
1514 			return;
1515 
1516 		ret = nvmet_tcp_try_send(queue, NVMET_TCP_SEND_BUDGET, &ops);
1517 		if (ret > 0)
1518 			pending = true;
1519 		else if (ret < 0)
1520 			return;
1521 
1522 	} while (pending && ops < NVMET_TCP_IO_WORK_BUDGET);
1523 
1524 	/*
1525 	 * Requeue the worker if idle deadline period is in progress or any
1526 	 * ops activity was recorded during the do-while loop above.
1527 	 */
1528 	if (nvmet_tcp_check_queue_deadline(queue, ops) || pending)
1529 		queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &queue->io_work);
1530 }
1531 
1532 static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue,
1533 		struct nvmet_tcp_cmd *c)
1534 {
1535 	u8 hdgst = nvmet_tcp_hdgst_len(queue);
1536 
1537 	c->queue = queue;
1538 	c->req.port = queue->nport;
1539 
1540 	c->cmd_pdu = page_frag_alloc(&queue->pf_cache,
1541 			sizeof(*c->cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1542 	if (!c->cmd_pdu)
1543 		return -ENOMEM;
1544 	c->req.cmd = &c->cmd_pdu->cmd;
1545 
1546 	c->rsp_pdu = page_frag_alloc(&queue->pf_cache,
1547 			sizeof(*c->rsp_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1548 	if (!c->rsp_pdu)
1549 		goto out_free_cmd;
1550 	c->req.cqe = &c->rsp_pdu->cqe;
1551 
1552 	c->data_pdu = page_frag_alloc(&queue->pf_cache,
1553 			sizeof(*c->data_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1554 	if (!c->data_pdu)
1555 		goto out_free_rsp;
1556 
1557 	c->r2t_pdu = page_frag_alloc(&queue->pf_cache,
1558 			sizeof(*c->r2t_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1559 	if (!c->r2t_pdu)
1560 		goto out_free_data;
1561 
1562 	if (queue->state == NVMET_TCP_Q_TLS_HANDSHAKE) {
1563 		c->recv_msg.msg_control = c->recv_cbuf;
1564 		c->recv_msg.msg_controllen = sizeof(c->recv_cbuf);
1565 	}
1566 	c->recv_msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1567 
1568 	list_add_tail(&c->entry, &queue->free_list);
1569 
1570 	return 0;
1571 out_free_data:
1572 	page_frag_free(c->data_pdu);
1573 out_free_rsp:
1574 	page_frag_free(c->rsp_pdu);
1575 out_free_cmd:
1576 	page_frag_free(c->cmd_pdu);
1577 	return -ENOMEM;
1578 }
1579 
1580 static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c)
1581 {
1582 	page_frag_free(c->r2t_pdu);
1583 	page_frag_free(c->data_pdu);
1584 	page_frag_free(c->rsp_pdu);
1585 	page_frag_free(c->cmd_pdu);
1586 }
1587 
1588 static int nvmet_tcp_alloc_cmds(struct nvmet_tcp_queue *queue)
1589 {
1590 	struct nvmet_tcp_cmd *cmds;
1591 	int i, ret = -EINVAL, nr_cmds = queue->nr_cmds;
1592 
1593 	cmds = kvzalloc_objs(struct nvmet_tcp_cmd, nr_cmds);
1594 	if (!cmds)
1595 		goto out;
1596 
1597 	for (i = 0; i < nr_cmds; i++) {
1598 		ret = nvmet_tcp_alloc_cmd(queue, cmds + i);
1599 		if (ret)
1600 			goto out_free;
1601 	}
1602 
1603 	queue->cmds = cmds;
1604 
1605 	return 0;
1606 out_free:
1607 	while (--i >= 0)
1608 		nvmet_tcp_free_cmd(cmds + i);
1609 	kvfree(cmds);
1610 out:
1611 	return ret;
1612 }
1613 
1614 static void nvmet_tcp_free_cmds(struct nvmet_tcp_queue *queue)
1615 {
1616 	struct nvmet_tcp_cmd *cmds = queue->cmds;
1617 	int i;
1618 
1619 	for (i = 0; i < queue->nr_cmds; i++)
1620 		nvmet_tcp_free_cmd(cmds + i);
1621 
1622 	nvmet_tcp_free_cmd(&queue->connect);
1623 	kvfree(cmds);
1624 }
1625 
1626 static void nvmet_tcp_restore_socket_callbacks(struct nvmet_tcp_queue *queue)
1627 {
1628 	struct socket *sock = queue->sock;
1629 
1630 	if (!queue->state_change)
1631 		return;
1632 
1633 	write_lock_bh(&sock->sk->sk_callback_lock);
1634 	sock->sk->sk_data_ready =  queue->data_ready;
1635 	sock->sk->sk_state_change = queue->state_change;
1636 	sock->sk->sk_write_space = queue->write_space;
1637 	sock->sk->sk_user_data = NULL;
1638 	write_unlock_bh(&sock->sk->sk_callback_lock);
1639 }
1640 
1641 static void nvmet_tcp_uninit_data_in_cmds(struct nvmet_tcp_queue *queue)
1642 {
1643 	struct nvmet_tcp_cmd *cmd = queue->cmds;
1644 	int i;
1645 
1646 	for (i = 0; i < queue->nr_cmds; i++, cmd++) {
1647 		if (nvmet_tcp_need_data_in(cmd))
1648 			nvmet_req_uninit(&cmd->req);
1649 	}
1650 
1651 	if (!queue->nr_cmds && nvmet_tcp_need_data_in(&queue->connect)) {
1652 		/* failed in connect */
1653 		nvmet_req_uninit(&queue->connect.req);
1654 	}
1655 }
1656 
1657 static void nvmet_tcp_free_cmd_data_in_buffers(struct nvmet_tcp_queue *queue)
1658 {
1659 	struct nvmet_tcp_cmd *cmd = queue->cmds;
1660 	int i;
1661 
1662 	for (i = 0; i < queue->nr_cmds; i++, cmd++)
1663 		nvmet_tcp_free_cmd_buffers(cmd);
1664 	nvmet_tcp_free_cmd_buffers(&queue->connect);
1665 }
1666 
1667 static void nvmet_tcp_release_queue_work(struct work_struct *w)
1668 {
1669 	struct nvmet_tcp_queue *queue =
1670 		container_of(w, struct nvmet_tcp_queue, release_work);
1671 
1672 	mutex_lock(&nvmet_tcp_queue_mutex);
1673 	list_del_init(&queue->queue_list);
1674 	mutex_unlock(&nvmet_tcp_queue_mutex);
1675 
1676 	nvmet_tcp_restore_socket_callbacks(queue);
1677 	cancel_delayed_work_sync(&queue->tls_handshake_tmo_work);
1678 	cancel_work_sync(&queue->io_work);
1679 	/* stop accepting incoming data */
1680 	queue->rcv_state = NVMET_TCP_RECV_ERR;
1681 
1682 	nvmet_sq_put_tls_key(&queue->nvme_sq);
1683 	nvmet_tcp_uninit_data_in_cmds(queue);
1684 	nvmet_sq_destroy(&queue->nvme_sq);
1685 	nvmet_cq_put(&queue->nvme_cq);
1686 	cancel_work_sync(&queue->io_work);
1687 	nvmet_tcp_free_cmd_data_in_buffers(queue);
1688 	/* ->sock will be released by fput() */
1689 	fput(queue->sock->file);
1690 	nvmet_tcp_free_cmds(queue);
1691 	ida_free(&nvmet_tcp_queue_ida, queue->idx);
1692 	page_frag_cache_drain(&queue->pf_cache);
1693 	kfree(queue);
1694 }
1695 
1696 static void nvmet_tcp_data_ready(struct sock *sk)
1697 {
1698 	struct nvmet_tcp_queue *queue;
1699 
1700 	trace_sk_data_ready(sk);
1701 
1702 	read_lock_bh(&sk->sk_callback_lock);
1703 	queue = sk->sk_user_data;
1704 	if (likely(queue)) {
1705 		if (queue->data_ready)
1706 			queue->data_ready(sk);
1707 		if (queue->state != NVMET_TCP_Q_TLS_HANDSHAKE)
1708 			queue_work_on(queue_cpu(queue), nvmet_tcp_wq,
1709 				      &queue->io_work);
1710 	}
1711 	read_unlock_bh(&sk->sk_callback_lock);
1712 }
1713 
1714 static void nvmet_tcp_write_space(struct sock *sk)
1715 {
1716 	struct nvmet_tcp_queue *queue;
1717 
1718 	read_lock_bh(&sk->sk_callback_lock);
1719 	queue = sk->sk_user_data;
1720 	if (unlikely(!queue))
1721 		goto out;
1722 
1723 	if (unlikely(queue->state == NVMET_TCP_Q_CONNECTING)) {
1724 		queue->write_space(sk);
1725 		goto out;
1726 	}
1727 
1728 	if (sk_stream_is_writeable(sk)) {
1729 		sk_clear_nospace(sk);
1730 		queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &queue->io_work);
1731 	}
1732 out:
1733 	read_unlock_bh(&sk->sk_callback_lock);
1734 }
1735 
1736 static void nvmet_tcp_state_change(struct sock *sk)
1737 {
1738 	struct nvmet_tcp_queue *queue;
1739 
1740 	read_lock_bh(&sk->sk_callback_lock);
1741 	queue = sk->sk_user_data;
1742 	if (!queue)
1743 		goto done;
1744 
1745 	switch (sk->sk_state) {
1746 	case TCP_FIN_WAIT2:
1747 	case TCP_LAST_ACK:
1748 	case TCP_CLOSING:
1749 		break;
1750 	case TCP_FIN_WAIT1:
1751 	case TCP_CLOSE_WAIT:
1752 	case TCP_CLOSE:
1753 		/* FALLTHRU */
1754 		nvmet_tcp_schedule_release_queue(queue);
1755 		break;
1756 	default:
1757 		pr_warn("queue %d unhandled state %d\n",
1758 			queue->idx, sk->sk_state);
1759 	}
1760 done:
1761 	read_unlock_bh(&sk->sk_callback_lock);
1762 }
1763 
1764 static int nvmet_tcp_set_queue_sock(struct nvmet_tcp_queue *queue)
1765 {
1766 	struct socket *sock = queue->sock;
1767 	struct inet_sock *inet = inet_sk(sock->sk);
1768 	int ret;
1769 
1770 	ret = kernel_getsockname(sock,
1771 		(struct sockaddr *)&queue->sockaddr);
1772 	if (ret < 0)
1773 		return ret;
1774 
1775 	ret = kernel_getpeername(sock,
1776 		(struct sockaddr *)&queue->sockaddr_peer);
1777 	if (ret < 0)
1778 		return ret;
1779 
1780 	/*
1781 	 * Cleanup whatever is sitting in the TCP transmit queue on socket
1782 	 * close. This is done to prevent stale data from being sent should
1783 	 * the network connection be restored before TCP times out.
1784 	 */
1785 	sock_no_linger(sock->sk);
1786 
1787 	if (so_priority > 0)
1788 		sock_set_priority(sock->sk, so_priority);
1789 
1790 	/* Set socket type of service */
1791 	if (inet->rcv_tos > 0)
1792 		ip_sock_set_tos(sock->sk, inet->rcv_tos);
1793 
1794 	ret = 0;
1795 	write_lock_bh(&sock->sk->sk_callback_lock);
1796 	if (sock->sk->sk_state != TCP_ESTABLISHED) {
1797 		/*
1798 		 * If the socket is already closing, don't even start
1799 		 * consuming it
1800 		 */
1801 		ret = -ENOTCONN;
1802 	} else {
1803 		sock->sk->sk_user_data = queue;
1804 		queue->data_ready = sock->sk->sk_data_ready;
1805 		sock->sk->sk_data_ready = nvmet_tcp_data_ready;
1806 		queue->state_change = sock->sk->sk_state_change;
1807 		sock->sk->sk_state_change = nvmet_tcp_state_change;
1808 		queue->write_space = sock->sk->sk_write_space;
1809 		sock->sk->sk_write_space = nvmet_tcp_write_space;
1810 		if (idle_poll_period_usecs)
1811 			nvmet_tcp_arm_queue_deadline(queue);
1812 		queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &queue->io_work);
1813 	}
1814 	write_unlock_bh(&sock->sk->sk_callback_lock);
1815 
1816 	return ret;
1817 }
1818 
1819 #ifdef CONFIG_NVME_TARGET_TCP_TLS
1820 static int nvmet_tcp_try_peek_pdu(struct nvmet_tcp_queue *queue)
1821 {
1822 	struct nvme_tcp_hdr *hdr = &queue->pdu.cmd.hdr;
1823 	int len, ret;
1824 	struct kvec iov = {
1825 		.iov_base = (u8 *)&queue->pdu + queue->offset,
1826 		.iov_len = sizeof(struct nvme_tcp_hdr),
1827 	};
1828 	char cbuf[CMSG_LEN(sizeof(char))] = {};
1829 	struct msghdr msg = {
1830 		.msg_control = cbuf,
1831 		.msg_controllen = sizeof(cbuf),
1832 		.msg_flags = MSG_PEEK,
1833 	};
1834 
1835 	if (nvmet_port_secure_channel_required(queue->port->nport))
1836 		return 0;
1837 
1838 	len = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1839 			iov.iov_len, msg.msg_flags);
1840 	if (unlikely(len < 0)) {
1841 		pr_debug("queue %d: peek error %d\n",
1842 			 queue->idx, len);
1843 		return len;
1844 	}
1845 
1846 	ret = nvmet_tcp_tls_record_ok(queue, &msg, cbuf);
1847 	if (ret < 0)
1848 		return ret;
1849 
1850 	if (len < sizeof(struct nvme_tcp_hdr)) {
1851 		pr_debug("queue %d: short read, %d bytes missing\n",
1852 			 queue->idx, (int)iov.iov_len - len);
1853 		return -EAGAIN;
1854 	}
1855 	pr_debug("queue %d: hdr type %d hlen %d plen %d size %d\n",
1856 		 queue->idx, hdr->type, hdr->hlen, hdr->plen,
1857 		 (int)sizeof(struct nvme_tcp_icreq_pdu));
1858 	if (hdr->type == nvme_tcp_icreq &&
1859 	    hdr->hlen == sizeof(struct nvme_tcp_icreq_pdu) &&
1860 	    hdr->plen == cpu_to_le32(sizeof(struct nvme_tcp_icreq_pdu))) {
1861 		pr_debug("queue %d: icreq detected\n",
1862 			 queue->idx);
1863 		return len;
1864 	}
1865 	return 0;
1866 }
1867 
1868 static int nvmet_tcp_tls_key_lookup(struct nvmet_tcp_queue *queue,
1869 				    key_serial_t peerid)
1870 {
1871 	struct key *tls_key = nvme_tls_key_lookup(peerid);
1872 	int status = 0;
1873 
1874 	if (IS_ERR(tls_key)) {
1875 		pr_warn("%s: queue %d failed to lookup key %x\n",
1876 			__func__, queue->idx, peerid);
1877 		spin_lock_bh(&queue->state_lock);
1878 		queue->state = NVMET_TCP_Q_FAILED;
1879 		spin_unlock_bh(&queue->state_lock);
1880 		status = PTR_ERR(tls_key);
1881 	} else {
1882 		pr_debug("%s: queue %d using TLS PSK %x\n",
1883 			 __func__, queue->idx, peerid);
1884 		queue->nvme_sq.tls_key = tls_key;
1885 	}
1886 	return status;
1887 }
1888 
1889 static void nvmet_tcp_tls_handshake_done(void *data, int status,
1890 					 key_serial_t peerid)
1891 {
1892 	struct nvmet_tcp_queue *queue = data;
1893 
1894 	pr_debug("queue %d: TLS handshake done, key %x, status %d\n",
1895 		 queue->idx, peerid, status);
1896 	spin_lock_bh(&queue->state_lock);
1897 	if (WARN_ON(queue->state != NVMET_TCP_Q_TLS_HANDSHAKE)) {
1898 		spin_unlock_bh(&queue->state_lock);
1899 		return;
1900 	}
1901 	if (!status) {
1902 		queue->tls_pskid = peerid;
1903 		queue->state = NVMET_TCP_Q_CONNECTING;
1904 	} else
1905 		queue->state = NVMET_TCP_Q_FAILED;
1906 	spin_unlock_bh(&queue->state_lock);
1907 
1908 	cancel_delayed_work_sync(&queue->tls_handshake_tmo_work);
1909 
1910 	if (!status)
1911 		status = nvmet_tcp_tls_key_lookup(queue, peerid);
1912 
1913 	if (!status)
1914 		status = nvmet_tcp_set_queue_sock(queue);
1915 
1916 	if (status)
1917 		nvmet_tcp_schedule_release_queue(queue);
1918 	kref_put(&queue->kref, nvmet_tcp_release_queue);
1919 }
1920 
1921 static void nvmet_tcp_tls_handshake_timeout(struct work_struct *w)
1922 {
1923 	struct nvmet_tcp_queue *queue = container_of(to_delayed_work(w),
1924 			struct nvmet_tcp_queue, tls_handshake_tmo_work);
1925 
1926 	pr_warn("queue %d: TLS handshake timeout\n", queue->idx);
1927 	/*
1928 	 * If tls_handshake_cancel() fails we've lost the race with
1929 	 * nvmet_tcp_tls_handshake_done() */
1930 	if (!tls_handshake_cancel(queue->sock->sk))
1931 		return;
1932 	spin_lock_bh(&queue->state_lock);
1933 	if (WARN_ON(queue->state != NVMET_TCP_Q_TLS_HANDSHAKE)) {
1934 		spin_unlock_bh(&queue->state_lock);
1935 		return;
1936 	}
1937 	queue->state = NVMET_TCP_Q_FAILED;
1938 	spin_unlock_bh(&queue->state_lock);
1939 	nvmet_tcp_schedule_release_queue(queue);
1940 	kref_put(&queue->kref, nvmet_tcp_release_queue);
1941 }
1942 
1943 static int nvmet_tcp_tls_handshake(struct nvmet_tcp_queue *queue)
1944 {
1945 	int ret = -EOPNOTSUPP;
1946 	struct tls_handshake_args args;
1947 	unsigned int timeout = READ_ONCE(tls_handshake_timeout);
1948 
1949 	if (queue->state != NVMET_TCP_Q_TLS_HANDSHAKE) {
1950 		pr_warn("cannot start TLS in state %d\n", queue->state);
1951 		return -EINVAL;
1952 	}
1953 
1954 	kref_get(&queue->kref);
1955 	pr_debug("queue %d: TLS ServerHello\n", queue->idx);
1956 	memset(&args, 0, sizeof(args));
1957 	args.ta_sock = queue->sock;
1958 	args.ta_done = nvmet_tcp_tls_handshake_done;
1959 	args.ta_data = queue;
1960 	args.ta_keyring = key_serial(queue->port->nport->keyring);
1961 	args.ta_timeout_ms = timeout * MSEC_PER_SEC;
1962 
1963 	ret = tls_server_hello_psk(&args, GFP_KERNEL);
1964 	if (ret) {
1965 		kref_put(&queue->kref, nvmet_tcp_release_queue);
1966 		pr_err("failed to start TLS, err=%d\n", ret);
1967 	} else {
1968 		queue_delayed_work(nvmet_wq, &queue->tls_handshake_tmo_work,
1969 				   (unsigned long)timeout * HZ);
1970 	}
1971 	return ret;
1972 }
1973 #else
1974 static void nvmet_tcp_tls_handshake_timeout(struct work_struct *w) {}
1975 #endif
1976 
1977 static void nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,
1978 		struct socket *newsock)
1979 {
1980 	struct nvmet_tcp_queue *queue;
1981 	struct file *sock_file = NULL;
1982 	int ret;
1983 
1984 	queue = kzalloc_obj(*queue);
1985 	if (!queue) {
1986 		ret = -ENOMEM;
1987 		goto out_release;
1988 	}
1989 
1990 	INIT_WORK(&queue->release_work, nvmet_tcp_release_queue_work);
1991 	INIT_WORK(&queue->io_work, nvmet_tcp_io_work);
1992 	kref_init(&queue->kref);
1993 	queue->sock = newsock;
1994 	queue->port = port;
1995 	/* The port may be gone when the commands are allocated */
1996 	queue->nport = port->nport;
1997 	queue->nr_cmds = 0;
1998 	spin_lock_init(&queue->state_lock);
1999 	if (queue->port->nport->disc_addr.tsas.tcp.sectype ==
2000 	    NVMF_TCP_SECTYPE_TLS13)
2001 		queue->state = NVMET_TCP_Q_TLS_HANDSHAKE;
2002 	else
2003 		queue->state = NVMET_TCP_Q_CONNECTING;
2004 	INIT_LIST_HEAD(&queue->free_list);
2005 	init_llist_head(&queue->resp_list);
2006 	INIT_LIST_HEAD(&queue->resp_send_list);
2007 
2008 	sock_file = sock_alloc_file(queue->sock, O_CLOEXEC, NULL);
2009 	if (IS_ERR(sock_file)) {
2010 		ret = PTR_ERR(sock_file);
2011 		goto out_free_queue;
2012 	}
2013 
2014 	queue->idx = ida_alloc(&nvmet_tcp_queue_ida, GFP_KERNEL);
2015 	if (queue->idx < 0) {
2016 		ret = queue->idx;
2017 		goto out_sock;
2018 	}
2019 
2020 	ret = nvmet_tcp_alloc_cmd(queue, &queue->connect);
2021 	if (ret)
2022 		goto out_ida_remove;
2023 
2024 	nvmet_cq_init(&queue->nvme_cq);
2025 	ret = nvmet_sq_init(&queue->nvme_sq, &queue->nvme_cq);
2026 	if (ret)
2027 		goto out_free_connect;
2028 
2029 	nvmet_prepare_receive_pdu(queue);
2030 
2031 	mutex_lock(&nvmet_tcp_queue_mutex);
2032 	list_add_tail(&queue->queue_list, &nvmet_tcp_queue_list);
2033 	mutex_unlock(&nvmet_tcp_queue_mutex);
2034 
2035 	INIT_DELAYED_WORK(&queue->tls_handshake_tmo_work,
2036 			  nvmet_tcp_tls_handshake_timeout);
2037 #ifdef CONFIG_NVME_TARGET_TCP_TLS
2038 	if (queue->state == NVMET_TCP_Q_TLS_HANDSHAKE) {
2039 		struct sock *sk = queue->sock->sk;
2040 
2041 		/* Restore the default callbacks before starting upcall */
2042 		write_lock_bh(&sk->sk_callback_lock);
2043 		sk->sk_user_data = NULL;
2044 		sk->sk_data_ready = port->data_ready;
2045 		write_unlock_bh(&sk->sk_callback_lock);
2046 		if (!nvmet_tcp_try_peek_pdu(queue)) {
2047 			if (!nvmet_tcp_tls_handshake(queue))
2048 				return;
2049 			/* TLS handshake failed, terminate the connection */
2050 			goto out_destroy_sq;
2051 		}
2052 		/* Not a TLS connection, continue with normal processing */
2053 		queue->state = NVMET_TCP_Q_CONNECTING;
2054 	}
2055 #endif
2056 
2057 	ret = nvmet_tcp_set_queue_sock(queue);
2058 	if (ret)
2059 		goto out_destroy_sq;
2060 
2061 	return;
2062 out_destroy_sq:
2063 	mutex_lock(&nvmet_tcp_queue_mutex);
2064 	list_del_init(&queue->queue_list);
2065 	mutex_unlock(&nvmet_tcp_queue_mutex);
2066 	nvmet_sq_destroy(&queue->nvme_sq);
2067 out_free_connect:
2068 	nvmet_cq_put(&queue->nvme_cq);
2069 	nvmet_tcp_free_cmd(&queue->connect);
2070 out_ida_remove:
2071 	ida_free(&nvmet_tcp_queue_ida, queue->idx);
2072 	/*
2073 	 * Drain the page fragment cache if any allocations were done.
2074 	 * The first allocation using pf_cache is nvmet_tcp_alloc_cmd()
2075 	 * for queue->connect after ida_alloc().
2076 	 */
2077 	page_frag_cache_drain(&queue->pf_cache);
2078 out_sock:
2079 	fput(queue->sock->file);
2080 out_free_queue:
2081 	kfree(queue);
2082 out_release:
2083 	pr_err("failed to allocate queue, error %d\n", ret);
2084 	if (!sock_file)
2085 		sock_release(newsock);
2086 }
2087 
2088 static void nvmet_tcp_accept_work(struct work_struct *w)
2089 {
2090 	struct nvmet_tcp_port *port =
2091 		container_of(w, struct nvmet_tcp_port, accept_work);
2092 	struct socket *newsock;
2093 	int ret, i;
2094 
2095 	for (i = 0; i < NVMET_TCP_ACCEPT_BUDGET; i++) {
2096 		ret = kernel_accept(port->sock, &newsock, O_NONBLOCK);
2097 		if (ret < 0) {
2098 			if (ret != -EAGAIN)
2099 				pr_warn("failed to accept err=%d\n", ret);
2100 			return;
2101 		}
2102 		nvmet_tcp_alloc_queue(port, newsock);
2103 	}
2104 
2105 	/* There may be more. Come to an end for those who flush nvmet_wq. */
2106 	queue_work(nvmet_wq, &port->accept_work);
2107 }
2108 
2109 static void nvmet_tcp_listen_data_ready(struct sock *sk)
2110 {
2111 	struct nvmet_tcp_port *port;
2112 
2113 	trace_sk_data_ready(sk);
2114 
2115 	if (sk->sk_state != TCP_LISTEN)
2116 		return;
2117 
2118 	read_lock_bh(&sk->sk_callback_lock);
2119 	port = sk->sk_user_data;
2120 	if (port)
2121 		queue_work(nvmet_wq, &port->accept_work);
2122 	read_unlock_bh(&sk->sk_callback_lock);
2123 }
2124 
2125 static int nvmet_tcp_add_port(struct nvmet_port *nport)
2126 {
2127 	struct nvmet_tcp_port *port;
2128 	__kernel_sa_family_t af;
2129 	int ret;
2130 
2131 	port = kzalloc_obj(*port);
2132 	if (!port)
2133 		return -ENOMEM;
2134 
2135 	switch (nport->disc_addr.adrfam) {
2136 	case NVMF_ADDR_FAMILY_IP4:
2137 		af = AF_INET;
2138 		break;
2139 	case NVMF_ADDR_FAMILY_IP6:
2140 		af = AF_INET6;
2141 		break;
2142 	default:
2143 		pr_err("address family %d not supported\n",
2144 				nport->disc_addr.adrfam);
2145 		ret = -EINVAL;
2146 		goto err_port;
2147 	}
2148 
2149 	ret = inet_pton_with_scope(&init_net, af, nport->disc_addr.traddr,
2150 			nport->disc_addr.trsvcid, &port->addr);
2151 	if (ret) {
2152 		pr_err("malformed ip/port passed: %s:%s\n",
2153 			nport->disc_addr.traddr, nport->disc_addr.trsvcid);
2154 		goto err_port;
2155 	}
2156 
2157 	port->nport = nport;
2158 	INIT_WORK(&port->accept_work, nvmet_tcp_accept_work);
2159 	if (port->nport->inline_data_size < 0)
2160 		port->nport->inline_data_size = NVMET_TCP_DEF_INLINE_DATA_SIZE;
2161 
2162 	ret = sock_create(port->addr.ss_family, SOCK_STREAM,
2163 				IPPROTO_TCP, &port->sock);
2164 	if (ret) {
2165 		pr_err("failed to create a socket\n");
2166 		goto err_port;
2167 	}
2168 
2169 	port->sock->sk->sk_user_data = port;
2170 	port->data_ready = port->sock->sk->sk_data_ready;
2171 	port->sock->sk->sk_data_ready = nvmet_tcp_listen_data_ready;
2172 	sock_set_reuseaddr(port->sock->sk);
2173 	tcp_sock_set_nodelay(port->sock->sk);
2174 	if (so_priority > 0)
2175 		sock_set_priority(port->sock->sk, so_priority);
2176 
2177 	ret = kernel_bind(port->sock, (struct sockaddr_unsized *)&port->addr,
2178 			sizeof(port->addr));
2179 	if (ret) {
2180 		pr_err("failed to bind port socket %d\n", ret);
2181 		goto err_sock;
2182 	}
2183 
2184 	ret = kernel_listen(port->sock, NVMET_TCP_BACKLOG);
2185 	if (ret) {
2186 		pr_err("failed to listen %d on port sock\n", ret);
2187 		goto err_sock;
2188 	}
2189 
2190 	nport->priv = port;
2191 	pr_info("enabling port %d (%pISpc)\n",
2192 		le16_to_cpu(nport->disc_addr.portid), &port->addr);
2193 
2194 	return 0;
2195 
2196 err_sock:
2197 	sock_release(port->sock);
2198 err_port:
2199 	kfree(port);
2200 	return ret;
2201 }
2202 
2203 static void nvmet_tcp_destroy_port_queues(struct nvmet_tcp_port *port)
2204 {
2205 	struct nvmet_tcp_queue *queue;
2206 
2207 	mutex_lock(&nvmet_tcp_queue_mutex);
2208 	list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list)
2209 		if (queue->port == port)
2210 			kernel_sock_shutdown(queue->sock, SHUT_RDWR);
2211 	mutex_unlock(&nvmet_tcp_queue_mutex);
2212 }
2213 
2214 static void nvmet_tcp_remove_port(struct nvmet_port *nport)
2215 {
2216 	struct nvmet_tcp_port *port = nport->priv;
2217 
2218 	write_lock_bh(&port->sock->sk->sk_callback_lock);
2219 	port->sock->sk->sk_data_ready = port->data_ready;
2220 	port->sock->sk->sk_user_data = NULL;
2221 	write_unlock_bh(&port->sock->sk->sk_callback_lock);
2222 	/* The accept work goes on for as long as there is something to accept */
2223 	kernel_sock_shutdown(port->sock, SHUT_RDWR);
2224 	cancel_work_sync(&port->accept_work);
2225 	/*
2226 	 * Destroy the remaining queues, which are not belong to any
2227 	 * controller yet.
2228 	 */
2229 	nvmet_tcp_destroy_port_queues(port);
2230 
2231 	sock_release(port->sock);
2232 	kfree(port);
2233 }
2234 
2235 static void nvmet_tcp_delete_ctrl(struct nvmet_ctrl *ctrl)
2236 {
2237 	struct nvmet_tcp_queue *queue;
2238 
2239 	mutex_lock(&nvmet_tcp_queue_mutex);
2240 	list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list)
2241 		if (queue->nvme_sq.ctrl == ctrl)
2242 			kernel_sock_shutdown(queue->sock, SHUT_RDWR);
2243 	mutex_unlock(&nvmet_tcp_queue_mutex);
2244 }
2245 
2246 static u16 nvmet_tcp_install_queue(struct nvmet_sq *sq)
2247 {
2248 	struct nvmet_tcp_queue *queue =
2249 		container_of(sq, struct nvmet_tcp_queue, nvme_sq);
2250 
2251 	if (sq->qid == 0) {
2252 		struct nvmet_tcp_queue *q;
2253 		int pending = 0;
2254 
2255 		/* Check for pending controller teardown */
2256 		mutex_lock(&nvmet_tcp_queue_mutex);
2257 		list_for_each_entry(q, &nvmet_tcp_queue_list, queue_list) {
2258 			if (q->nvme_sq.ctrl == sq->ctrl &&
2259 			    q->state == NVMET_TCP_Q_DISCONNECTING)
2260 				pending++;
2261 		}
2262 		mutex_unlock(&nvmet_tcp_queue_mutex);
2263 		if (pending > NVMET_TCP_BACKLOG)
2264 			return NVME_SC_CONNECT_CTRL_BUSY;
2265 	}
2266 
2267 	queue->nr_cmds = sq->size * 2;
2268 	if (nvmet_tcp_alloc_cmds(queue)) {
2269 		queue->nr_cmds = 0;
2270 		return NVME_SC_INTERNAL;
2271 	}
2272 	return 0;
2273 }
2274 
2275 static void nvmet_tcp_disc_port_addr(struct nvmet_req *req,
2276 		struct nvmet_port *nport, char *traddr)
2277 {
2278 	struct nvmet_tcp_port *port = nport->priv;
2279 
2280 	if (inet_addr_is_any(&port->addr)) {
2281 		struct nvmet_tcp_cmd *cmd =
2282 			container_of(req, struct nvmet_tcp_cmd, req);
2283 		struct nvmet_tcp_queue *queue = cmd->queue;
2284 
2285 		sprintf(traddr, "%pISc", (struct sockaddr *)&queue->sockaddr);
2286 	} else {
2287 		memcpy(traddr, nport->disc_addr.traddr, NVMF_TRADDR_SIZE);
2288 	}
2289 }
2290 
2291 static ssize_t nvmet_tcp_host_port_addr(struct nvmet_ctrl *ctrl,
2292 			char *traddr, size_t traddr_len)
2293 {
2294 	struct nvmet_sq *sq = ctrl->sqs[0];
2295 	struct nvmet_tcp_queue *queue =
2296 		container_of(sq, struct nvmet_tcp_queue, nvme_sq);
2297 
2298 	if (queue->sockaddr_peer.ss_family == AF_UNSPEC)
2299 		return -EINVAL;
2300 	return snprintf(traddr, traddr_len, "%pISc",
2301 			(struct sockaddr *)&queue->sockaddr_peer);
2302 }
2303 
2304 static const struct nvmet_fabrics_ops nvmet_tcp_ops = {
2305 	.owner			= THIS_MODULE,
2306 	.type			= NVMF_TRTYPE_TCP,
2307 	.msdbd			= 1,
2308 	.add_port		= nvmet_tcp_add_port,
2309 	.remove_port		= nvmet_tcp_remove_port,
2310 	.queue_response		= nvmet_tcp_queue_response,
2311 	.delete_ctrl		= nvmet_tcp_delete_ctrl,
2312 	.install_queue		= nvmet_tcp_install_queue,
2313 	.disc_traddr		= nvmet_tcp_disc_port_addr,
2314 	.host_traddr		= nvmet_tcp_host_port_addr,
2315 };
2316 
2317 static int __init nvmet_tcp_init(void)
2318 {
2319 	int ret;
2320 
2321 	nvmet_tcp_wq = alloc_workqueue("nvmet_tcp_wq",
2322 				WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_PERCPU, 0);
2323 	if (!nvmet_tcp_wq)
2324 		return -ENOMEM;
2325 
2326 	ret = nvmet_register_transport(&nvmet_tcp_ops);
2327 	if (ret)
2328 		goto err;
2329 
2330 	return 0;
2331 err:
2332 	destroy_workqueue(nvmet_tcp_wq);
2333 	return ret;
2334 }
2335 
2336 static void __exit nvmet_tcp_exit(void)
2337 {
2338 	struct nvmet_tcp_queue *queue;
2339 
2340 	nvmet_unregister_transport(&nvmet_tcp_ops);
2341 
2342 	flush_workqueue(nvmet_wq);
2343 	mutex_lock(&nvmet_tcp_queue_mutex);
2344 	list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list)
2345 		kernel_sock_shutdown(queue->sock, SHUT_RDWR);
2346 	mutex_unlock(&nvmet_tcp_queue_mutex);
2347 	flush_workqueue(nvmet_wq);
2348 
2349 	destroy_workqueue(nvmet_tcp_wq);
2350 	ida_destroy(&nvmet_tcp_queue_ida);
2351 }
2352 
2353 module_init(nvmet_tcp_init);
2354 module_exit(nvmet_tcp_exit);
2355 
2356 MODULE_DESCRIPTION("NVMe target TCP transport driver");
2357 MODULE_LICENSE("GPL v2");
2358 MODULE_ALIAS("nvmet-transport-3"); /* 3 == NVMF_TRTYPE_TCP */
2359