xref: /linux/drivers/nvme/target/tcp.c (revision d6053666ef2b6631ef8f265f49ff2cc0f4d45c50)
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/slab.h>
10 #include <linux/err.h>
11 #include <linux/nvme-tcp.h>
12 #include <net/sock.h>
13 #include <net/tcp.h>
14 #include <linux/inet.h>
15 #include <linux/llist.h>
16 #include <crypto/hash.h>
17 #include <trace/events/sock.h>
18 
19 #include "nvmet.h"
20 
21 #define NVMET_TCP_DEF_INLINE_DATA_SIZE	(4 * PAGE_SIZE)
22 
23 /* Define the socket priority to use for connections were it is desirable
24  * that the NIC consider performing optimized packet processing or filtering.
25  * A non-zero value being sufficient to indicate general consideration of any
26  * possible optimization.  Making it a module param allows for alternative
27  * values that may be unique for some NIC implementations.
28  */
29 static int so_priority;
30 module_param(so_priority, int, 0644);
31 MODULE_PARM_DESC(so_priority, "nvmet tcp socket optimize priority");
32 
33 /* Define a time period (in usecs) that io_work() shall sample an activated
34  * queue before determining it to be idle.  This optional module behavior
35  * can enable NIC solutions that support socket optimized packet processing
36  * using advanced interrupt moderation techniques.
37  */
38 static int idle_poll_period_usecs;
39 module_param(idle_poll_period_usecs, int, 0644);
40 MODULE_PARM_DESC(idle_poll_period_usecs,
41 		"nvmet tcp io_work poll till idle time period in usecs");
42 
43 #define NVMET_TCP_RECV_BUDGET		8
44 #define NVMET_TCP_SEND_BUDGET		8
45 #define NVMET_TCP_IO_WORK_BUDGET	64
46 
47 enum nvmet_tcp_send_state {
48 	NVMET_TCP_SEND_DATA_PDU,
49 	NVMET_TCP_SEND_DATA,
50 	NVMET_TCP_SEND_R2T,
51 	NVMET_TCP_SEND_DDGST,
52 	NVMET_TCP_SEND_RESPONSE
53 };
54 
55 enum nvmet_tcp_recv_state {
56 	NVMET_TCP_RECV_PDU,
57 	NVMET_TCP_RECV_DATA,
58 	NVMET_TCP_RECV_DDGST,
59 	NVMET_TCP_RECV_ERR,
60 };
61 
62 enum {
63 	NVMET_TCP_F_INIT_FAILED = (1 << 0),
64 };
65 
66 struct nvmet_tcp_cmd {
67 	struct nvmet_tcp_queue		*queue;
68 	struct nvmet_req		req;
69 
70 	struct nvme_tcp_cmd_pdu		*cmd_pdu;
71 	struct nvme_tcp_rsp_pdu		*rsp_pdu;
72 	struct nvme_tcp_data_pdu	*data_pdu;
73 	struct nvme_tcp_r2t_pdu		*r2t_pdu;
74 
75 	u32				rbytes_done;
76 	u32				wbytes_done;
77 
78 	u32				pdu_len;
79 	u32				pdu_recv;
80 	int				sg_idx;
81 	struct msghdr			recv_msg;
82 	struct bio_vec			*iov;
83 	u32				flags;
84 
85 	struct list_head		entry;
86 	struct llist_node		lentry;
87 
88 	/* send state */
89 	u32				offset;
90 	struct scatterlist		*cur_sg;
91 	enum nvmet_tcp_send_state	state;
92 
93 	__le32				exp_ddgst;
94 	__le32				recv_ddgst;
95 };
96 
97 enum nvmet_tcp_queue_state {
98 	NVMET_TCP_Q_CONNECTING,
99 	NVMET_TCP_Q_LIVE,
100 	NVMET_TCP_Q_DISCONNECTING,
101 };
102 
103 struct nvmet_tcp_queue {
104 	struct socket		*sock;
105 	struct nvmet_tcp_port	*port;
106 	struct work_struct	io_work;
107 	struct nvmet_cq		nvme_cq;
108 	struct nvmet_sq		nvme_sq;
109 
110 	/* send state */
111 	struct nvmet_tcp_cmd	*cmds;
112 	unsigned int		nr_cmds;
113 	struct list_head	free_list;
114 	struct llist_head	resp_list;
115 	struct list_head	resp_send_list;
116 	int			send_list_len;
117 	struct nvmet_tcp_cmd	*snd_cmd;
118 
119 	/* recv state */
120 	int			offset;
121 	int			left;
122 	enum nvmet_tcp_recv_state rcv_state;
123 	struct nvmet_tcp_cmd	*cmd;
124 	union nvme_tcp_pdu	pdu;
125 
126 	/* digest state */
127 	bool			hdr_digest;
128 	bool			data_digest;
129 	struct ahash_request	*snd_hash;
130 	struct ahash_request	*rcv_hash;
131 
132 	unsigned long           poll_end;
133 
134 	spinlock_t		state_lock;
135 	enum nvmet_tcp_queue_state state;
136 
137 	struct sockaddr_storage	sockaddr;
138 	struct sockaddr_storage	sockaddr_peer;
139 	struct work_struct	release_work;
140 
141 	int			idx;
142 	struct list_head	queue_list;
143 
144 	struct nvmet_tcp_cmd	connect;
145 
146 	struct page_frag_cache	pf_cache;
147 
148 	void (*data_ready)(struct sock *);
149 	void (*state_change)(struct sock *);
150 	void (*write_space)(struct sock *);
151 };
152 
153 struct nvmet_tcp_port {
154 	struct socket		*sock;
155 	struct work_struct	accept_work;
156 	struct nvmet_port	*nport;
157 	struct sockaddr_storage addr;
158 	void (*data_ready)(struct sock *);
159 };
160 
161 static DEFINE_IDA(nvmet_tcp_queue_ida);
162 static LIST_HEAD(nvmet_tcp_queue_list);
163 static DEFINE_MUTEX(nvmet_tcp_queue_mutex);
164 
165 static struct workqueue_struct *nvmet_tcp_wq;
166 static const struct nvmet_fabrics_ops nvmet_tcp_ops;
167 static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c);
168 static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd);
169 
170 static inline u16 nvmet_tcp_cmd_tag(struct nvmet_tcp_queue *queue,
171 		struct nvmet_tcp_cmd *cmd)
172 {
173 	if (unlikely(!queue->nr_cmds)) {
174 		/* We didn't allocate cmds yet, send 0xffff */
175 		return USHRT_MAX;
176 	}
177 
178 	return cmd - queue->cmds;
179 }
180 
181 static inline bool nvmet_tcp_has_data_in(struct nvmet_tcp_cmd *cmd)
182 {
183 	return nvme_is_write(cmd->req.cmd) &&
184 		cmd->rbytes_done < cmd->req.transfer_len;
185 }
186 
187 static inline bool nvmet_tcp_need_data_in(struct nvmet_tcp_cmd *cmd)
188 {
189 	return nvmet_tcp_has_data_in(cmd) && !cmd->req.cqe->status;
190 }
191 
192 static inline bool nvmet_tcp_need_data_out(struct nvmet_tcp_cmd *cmd)
193 {
194 	return !nvme_is_write(cmd->req.cmd) &&
195 		cmd->req.transfer_len > 0 &&
196 		!cmd->req.cqe->status;
197 }
198 
199 static inline bool nvmet_tcp_has_inline_data(struct nvmet_tcp_cmd *cmd)
200 {
201 	return nvme_is_write(cmd->req.cmd) && cmd->pdu_len &&
202 		!cmd->rbytes_done;
203 }
204 
205 static inline struct nvmet_tcp_cmd *
206 nvmet_tcp_get_cmd(struct nvmet_tcp_queue *queue)
207 {
208 	struct nvmet_tcp_cmd *cmd;
209 
210 	cmd = list_first_entry_or_null(&queue->free_list,
211 				struct nvmet_tcp_cmd, entry);
212 	if (!cmd)
213 		return NULL;
214 	list_del_init(&cmd->entry);
215 
216 	cmd->rbytes_done = cmd->wbytes_done = 0;
217 	cmd->pdu_len = 0;
218 	cmd->pdu_recv = 0;
219 	cmd->iov = NULL;
220 	cmd->flags = 0;
221 	return cmd;
222 }
223 
224 static inline void nvmet_tcp_put_cmd(struct nvmet_tcp_cmd *cmd)
225 {
226 	if (unlikely(cmd == &cmd->queue->connect))
227 		return;
228 
229 	list_add_tail(&cmd->entry, &cmd->queue->free_list);
230 }
231 
232 static inline int queue_cpu(struct nvmet_tcp_queue *queue)
233 {
234 	return queue->sock->sk->sk_incoming_cpu;
235 }
236 
237 static inline u8 nvmet_tcp_hdgst_len(struct nvmet_tcp_queue *queue)
238 {
239 	return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
240 }
241 
242 static inline u8 nvmet_tcp_ddgst_len(struct nvmet_tcp_queue *queue)
243 {
244 	return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
245 }
246 
247 static inline void nvmet_tcp_hdgst(struct ahash_request *hash,
248 		void *pdu, size_t len)
249 {
250 	struct scatterlist sg;
251 
252 	sg_init_one(&sg, pdu, len);
253 	ahash_request_set_crypt(hash, &sg, pdu + len, len);
254 	crypto_ahash_digest(hash);
255 }
256 
257 static int nvmet_tcp_verify_hdgst(struct nvmet_tcp_queue *queue,
258 	void *pdu, size_t len)
259 {
260 	struct nvme_tcp_hdr *hdr = pdu;
261 	__le32 recv_digest;
262 	__le32 exp_digest;
263 
264 	if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
265 		pr_err("queue %d: header digest enabled but no header digest\n",
266 			queue->idx);
267 		return -EPROTO;
268 	}
269 
270 	recv_digest = *(__le32 *)(pdu + hdr->hlen);
271 	nvmet_tcp_hdgst(queue->rcv_hash, pdu, len);
272 	exp_digest = *(__le32 *)(pdu + hdr->hlen);
273 	if (recv_digest != exp_digest) {
274 		pr_err("queue %d: header digest error: recv %#x expected %#x\n",
275 			queue->idx, le32_to_cpu(recv_digest),
276 			le32_to_cpu(exp_digest));
277 		return -EPROTO;
278 	}
279 
280 	return 0;
281 }
282 
283 static int nvmet_tcp_check_ddgst(struct nvmet_tcp_queue *queue, void *pdu)
284 {
285 	struct nvme_tcp_hdr *hdr = pdu;
286 	u8 digest_len = nvmet_tcp_hdgst_len(queue);
287 	u32 len;
288 
289 	len = le32_to_cpu(hdr->plen) - hdr->hlen -
290 		(hdr->flags & NVME_TCP_F_HDGST ? digest_len : 0);
291 
292 	if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
293 		pr_err("queue %d: data digest flag is cleared\n", queue->idx);
294 		return -EPROTO;
295 	}
296 
297 	return 0;
298 }
299 
300 static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd)
301 {
302 	kfree(cmd->iov);
303 	sgl_free(cmd->req.sg);
304 	cmd->iov = NULL;
305 	cmd->req.sg = NULL;
306 }
307 
308 static void nvmet_tcp_build_pdu_iovec(struct nvmet_tcp_cmd *cmd)
309 {
310 	struct bio_vec *iov = cmd->iov;
311 	struct scatterlist *sg;
312 	u32 length, offset, sg_offset;
313 	int nr_pages;
314 
315 	length = cmd->pdu_len;
316 	nr_pages = DIV_ROUND_UP(length, PAGE_SIZE);
317 	offset = cmd->rbytes_done;
318 	cmd->sg_idx = offset / PAGE_SIZE;
319 	sg_offset = offset % PAGE_SIZE;
320 	sg = &cmd->req.sg[cmd->sg_idx];
321 
322 	while (length) {
323 		u32 iov_len = min_t(u32, length, sg->length - sg_offset);
324 
325 		bvec_set_page(iov, sg_page(sg), sg->length,
326 				sg->offset + sg_offset);
327 
328 		length -= iov_len;
329 		sg = sg_next(sg);
330 		iov++;
331 		sg_offset = 0;
332 	}
333 
334 	iov_iter_bvec(&cmd->recv_msg.msg_iter, ITER_DEST, cmd->iov,
335 		      nr_pages, cmd->pdu_len);
336 }
337 
338 static void nvmet_tcp_fatal_error(struct nvmet_tcp_queue *queue)
339 {
340 	queue->rcv_state = NVMET_TCP_RECV_ERR;
341 	if (queue->nvme_sq.ctrl)
342 		nvmet_ctrl_fatal_error(queue->nvme_sq.ctrl);
343 	else
344 		kernel_sock_shutdown(queue->sock, SHUT_RDWR);
345 }
346 
347 static void nvmet_tcp_socket_error(struct nvmet_tcp_queue *queue, int status)
348 {
349 	if (status == -EPIPE || status == -ECONNRESET)
350 		kernel_sock_shutdown(queue->sock, SHUT_RDWR);
351 	else
352 		nvmet_tcp_fatal_error(queue);
353 }
354 
355 static int nvmet_tcp_map_data(struct nvmet_tcp_cmd *cmd)
356 {
357 	struct nvme_sgl_desc *sgl = &cmd->req.cmd->common.dptr.sgl;
358 	u32 len = le32_to_cpu(sgl->length);
359 
360 	if (!len)
361 		return 0;
362 
363 	if (sgl->type == ((NVME_SGL_FMT_DATA_DESC << 4) |
364 			  NVME_SGL_FMT_OFFSET)) {
365 		if (!nvme_is_write(cmd->req.cmd))
366 			return NVME_SC_INVALID_FIELD | NVME_SC_DNR;
367 
368 		if (len > cmd->req.port->inline_data_size)
369 			return NVME_SC_SGL_INVALID_OFFSET | NVME_SC_DNR;
370 		cmd->pdu_len = len;
371 	}
372 	cmd->req.transfer_len += len;
373 
374 	cmd->req.sg = sgl_alloc(len, GFP_KERNEL, &cmd->req.sg_cnt);
375 	if (!cmd->req.sg)
376 		return NVME_SC_INTERNAL;
377 	cmd->cur_sg = cmd->req.sg;
378 
379 	if (nvmet_tcp_has_data_in(cmd)) {
380 		cmd->iov = kmalloc_array(cmd->req.sg_cnt,
381 				sizeof(*cmd->iov), GFP_KERNEL);
382 		if (!cmd->iov)
383 			goto err;
384 	}
385 
386 	return 0;
387 err:
388 	nvmet_tcp_free_cmd_buffers(cmd);
389 	return NVME_SC_INTERNAL;
390 }
391 
392 static void nvmet_tcp_calc_ddgst(struct ahash_request *hash,
393 		struct nvmet_tcp_cmd *cmd)
394 {
395 	ahash_request_set_crypt(hash, cmd->req.sg,
396 		(void *)&cmd->exp_ddgst, cmd->req.transfer_len);
397 	crypto_ahash_digest(hash);
398 }
399 
400 static void nvmet_setup_c2h_data_pdu(struct nvmet_tcp_cmd *cmd)
401 {
402 	struct nvme_tcp_data_pdu *pdu = cmd->data_pdu;
403 	struct nvmet_tcp_queue *queue = cmd->queue;
404 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
405 	u8 ddgst = nvmet_tcp_ddgst_len(cmd->queue);
406 
407 	cmd->offset = 0;
408 	cmd->state = NVMET_TCP_SEND_DATA_PDU;
409 
410 	pdu->hdr.type = nvme_tcp_c2h_data;
411 	pdu->hdr.flags = NVME_TCP_F_DATA_LAST | (queue->nvme_sq.sqhd_disabled ?
412 						NVME_TCP_F_DATA_SUCCESS : 0);
413 	pdu->hdr.hlen = sizeof(*pdu);
414 	pdu->hdr.pdo = pdu->hdr.hlen + hdgst;
415 	pdu->hdr.plen =
416 		cpu_to_le32(pdu->hdr.hlen + hdgst +
417 				cmd->req.transfer_len + ddgst);
418 	pdu->command_id = cmd->req.cqe->command_id;
419 	pdu->data_length = cpu_to_le32(cmd->req.transfer_len);
420 	pdu->data_offset = cpu_to_le32(cmd->wbytes_done);
421 
422 	if (queue->data_digest) {
423 		pdu->hdr.flags |= NVME_TCP_F_DDGST;
424 		nvmet_tcp_calc_ddgst(queue->snd_hash, cmd);
425 	}
426 
427 	if (cmd->queue->hdr_digest) {
428 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
429 		nvmet_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
430 	}
431 }
432 
433 static void nvmet_setup_r2t_pdu(struct nvmet_tcp_cmd *cmd)
434 {
435 	struct nvme_tcp_r2t_pdu *pdu = cmd->r2t_pdu;
436 	struct nvmet_tcp_queue *queue = cmd->queue;
437 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
438 
439 	cmd->offset = 0;
440 	cmd->state = NVMET_TCP_SEND_R2T;
441 
442 	pdu->hdr.type = nvme_tcp_r2t;
443 	pdu->hdr.flags = 0;
444 	pdu->hdr.hlen = sizeof(*pdu);
445 	pdu->hdr.pdo = 0;
446 	pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
447 
448 	pdu->command_id = cmd->req.cmd->common.command_id;
449 	pdu->ttag = nvmet_tcp_cmd_tag(cmd->queue, cmd);
450 	pdu->r2t_length = cpu_to_le32(cmd->req.transfer_len - cmd->rbytes_done);
451 	pdu->r2t_offset = cpu_to_le32(cmd->rbytes_done);
452 	if (cmd->queue->hdr_digest) {
453 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
454 		nvmet_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
455 	}
456 }
457 
458 static void nvmet_setup_response_pdu(struct nvmet_tcp_cmd *cmd)
459 {
460 	struct nvme_tcp_rsp_pdu *pdu = cmd->rsp_pdu;
461 	struct nvmet_tcp_queue *queue = cmd->queue;
462 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
463 
464 	cmd->offset = 0;
465 	cmd->state = NVMET_TCP_SEND_RESPONSE;
466 
467 	pdu->hdr.type = nvme_tcp_rsp;
468 	pdu->hdr.flags = 0;
469 	pdu->hdr.hlen = sizeof(*pdu);
470 	pdu->hdr.pdo = 0;
471 	pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
472 	if (cmd->queue->hdr_digest) {
473 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
474 		nvmet_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
475 	}
476 }
477 
478 static void nvmet_tcp_process_resp_list(struct nvmet_tcp_queue *queue)
479 {
480 	struct llist_node *node;
481 	struct nvmet_tcp_cmd *cmd;
482 
483 	for (node = llist_del_all(&queue->resp_list); node; node = node->next) {
484 		cmd = llist_entry(node, struct nvmet_tcp_cmd, lentry);
485 		list_add(&cmd->entry, &queue->resp_send_list);
486 		queue->send_list_len++;
487 	}
488 }
489 
490 static struct nvmet_tcp_cmd *nvmet_tcp_fetch_cmd(struct nvmet_tcp_queue *queue)
491 {
492 	queue->snd_cmd = list_first_entry_or_null(&queue->resp_send_list,
493 				struct nvmet_tcp_cmd, entry);
494 	if (!queue->snd_cmd) {
495 		nvmet_tcp_process_resp_list(queue);
496 		queue->snd_cmd =
497 			list_first_entry_or_null(&queue->resp_send_list,
498 					struct nvmet_tcp_cmd, entry);
499 		if (unlikely(!queue->snd_cmd))
500 			return NULL;
501 	}
502 
503 	list_del_init(&queue->snd_cmd->entry);
504 	queue->send_list_len--;
505 
506 	if (nvmet_tcp_need_data_out(queue->snd_cmd))
507 		nvmet_setup_c2h_data_pdu(queue->snd_cmd);
508 	else if (nvmet_tcp_need_data_in(queue->snd_cmd))
509 		nvmet_setup_r2t_pdu(queue->snd_cmd);
510 	else
511 		nvmet_setup_response_pdu(queue->snd_cmd);
512 
513 	return queue->snd_cmd;
514 }
515 
516 static void nvmet_tcp_queue_response(struct nvmet_req *req)
517 {
518 	struct nvmet_tcp_cmd *cmd =
519 		container_of(req, struct nvmet_tcp_cmd, req);
520 	struct nvmet_tcp_queue	*queue = cmd->queue;
521 	struct nvme_sgl_desc *sgl;
522 	u32 len;
523 
524 	if (unlikely(cmd == queue->cmd)) {
525 		sgl = &cmd->req.cmd->common.dptr.sgl;
526 		len = le32_to_cpu(sgl->length);
527 
528 		/*
529 		 * Wait for inline data before processing the response.
530 		 * Avoid using helpers, this might happen before
531 		 * nvmet_req_init is completed.
532 		 */
533 		if (queue->rcv_state == NVMET_TCP_RECV_PDU &&
534 		    len && len <= cmd->req.port->inline_data_size &&
535 		    nvme_is_write(cmd->req.cmd))
536 			return;
537 	}
538 
539 	llist_add(&cmd->lentry, &queue->resp_list);
540 	queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &cmd->queue->io_work);
541 }
542 
543 static void nvmet_tcp_execute_request(struct nvmet_tcp_cmd *cmd)
544 {
545 	if (unlikely(cmd->flags & NVMET_TCP_F_INIT_FAILED))
546 		nvmet_tcp_queue_response(&cmd->req);
547 	else
548 		cmd->req.execute(&cmd->req);
549 }
550 
551 static int nvmet_try_send_data_pdu(struct nvmet_tcp_cmd *cmd)
552 {
553 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
554 	int left = sizeof(*cmd->data_pdu) - cmd->offset + hdgst;
555 	int ret;
556 
557 	ret = kernel_sendpage(cmd->queue->sock, virt_to_page(cmd->data_pdu),
558 			offset_in_page(cmd->data_pdu) + cmd->offset,
559 			left, MSG_DONTWAIT | MSG_MORE | MSG_SENDPAGE_NOTLAST);
560 	if (ret <= 0)
561 		return ret;
562 
563 	cmd->offset += ret;
564 	left -= ret;
565 
566 	if (left)
567 		return -EAGAIN;
568 
569 	cmd->state = NVMET_TCP_SEND_DATA;
570 	cmd->offset  = 0;
571 	return 1;
572 }
573 
574 static int nvmet_try_send_data(struct nvmet_tcp_cmd *cmd, bool last_in_batch)
575 {
576 	struct nvmet_tcp_queue *queue = cmd->queue;
577 	int ret;
578 
579 	while (cmd->cur_sg) {
580 		struct page *page = sg_page(cmd->cur_sg);
581 		u32 left = cmd->cur_sg->length - cmd->offset;
582 		int flags = MSG_DONTWAIT;
583 
584 		if ((!last_in_batch && cmd->queue->send_list_len) ||
585 		    cmd->wbytes_done + left < cmd->req.transfer_len ||
586 		    queue->data_digest || !queue->nvme_sq.sqhd_disabled)
587 			flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
588 
589 		ret = kernel_sendpage(cmd->queue->sock, page, cmd->offset,
590 					left, flags);
591 		if (ret <= 0)
592 			return ret;
593 
594 		cmd->offset += ret;
595 		cmd->wbytes_done += ret;
596 
597 		/* Done with sg?*/
598 		if (cmd->offset == cmd->cur_sg->length) {
599 			cmd->cur_sg = sg_next(cmd->cur_sg);
600 			cmd->offset = 0;
601 		}
602 	}
603 
604 	if (queue->data_digest) {
605 		cmd->state = NVMET_TCP_SEND_DDGST;
606 		cmd->offset = 0;
607 	} else {
608 		if (queue->nvme_sq.sqhd_disabled) {
609 			cmd->queue->snd_cmd = NULL;
610 			nvmet_tcp_put_cmd(cmd);
611 		} else {
612 			nvmet_setup_response_pdu(cmd);
613 		}
614 	}
615 
616 	if (queue->nvme_sq.sqhd_disabled)
617 		nvmet_tcp_free_cmd_buffers(cmd);
618 
619 	return 1;
620 
621 }
622 
623 static int nvmet_try_send_response(struct nvmet_tcp_cmd *cmd,
624 		bool last_in_batch)
625 {
626 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
627 	int left = sizeof(*cmd->rsp_pdu) - cmd->offset + hdgst;
628 	int flags = MSG_DONTWAIT;
629 	int ret;
630 
631 	if (!last_in_batch && cmd->queue->send_list_len)
632 		flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
633 	else
634 		flags |= MSG_EOR;
635 
636 	ret = kernel_sendpage(cmd->queue->sock, virt_to_page(cmd->rsp_pdu),
637 		offset_in_page(cmd->rsp_pdu) + cmd->offset, left, flags);
638 	if (ret <= 0)
639 		return ret;
640 	cmd->offset += ret;
641 	left -= ret;
642 
643 	if (left)
644 		return -EAGAIN;
645 
646 	nvmet_tcp_free_cmd_buffers(cmd);
647 	cmd->queue->snd_cmd = NULL;
648 	nvmet_tcp_put_cmd(cmd);
649 	return 1;
650 }
651 
652 static int nvmet_try_send_r2t(struct nvmet_tcp_cmd *cmd, bool last_in_batch)
653 {
654 	u8 hdgst = nvmet_tcp_hdgst_len(cmd->queue);
655 	int left = sizeof(*cmd->r2t_pdu) - cmd->offset + hdgst;
656 	int flags = MSG_DONTWAIT;
657 	int ret;
658 
659 	if (!last_in_batch && cmd->queue->send_list_len)
660 		flags |= MSG_MORE | MSG_SENDPAGE_NOTLAST;
661 	else
662 		flags |= MSG_EOR;
663 
664 	ret = kernel_sendpage(cmd->queue->sock, virt_to_page(cmd->r2t_pdu),
665 		offset_in_page(cmd->r2t_pdu) + cmd->offset, left, flags);
666 	if (ret <= 0)
667 		return ret;
668 	cmd->offset += ret;
669 	left -= ret;
670 
671 	if (left)
672 		return -EAGAIN;
673 
674 	cmd->queue->snd_cmd = NULL;
675 	return 1;
676 }
677 
678 static int nvmet_try_send_ddgst(struct nvmet_tcp_cmd *cmd, bool last_in_batch)
679 {
680 	struct nvmet_tcp_queue *queue = cmd->queue;
681 	int left = NVME_TCP_DIGEST_LENGTH - cmd->offset;
682 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
683 	struct kvec iov = {
684 		.iov_base = (u8 *)&cmd->exp_ddgst + cmd->offset,
685 		.iov_len = left
686 	};
687 	int ret;
688 
689 	if (!last_in_batch && cmd->queue->send_list_len)
690 		msg.msg_flags |= MSG_MORE;
691 	else
692 		msg.msg_flags |= MSG_EOR;
693 
694 	ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
695 	if (unlikely(ret <= 0))
696 		return ret;
697 
698 	cmd->offset += ret;
699 	left -= ret;
700 
701 	if (left)
702 		return -EAGAIN;
703 
704 	if (queue->nvme_sq.sqhd_disabled) {
705 		cmd->queue->snd_cmd = NULL;
706 		nvmet_tcp_put_cmd(cmd);
707 	} else {
708 		nvmet_setup_response_pdu(cmd);
709 	}
710 	return 1;
711 }
712 
713 static int nvmet_tcp_try_send_one(struct nvmet_tcp_queue *queue,
714 		bool last_in_batch)
715 {
716 	struct nvmet_tcp_cmd *cmd = queue->snd_cmd;
717 	int ret = 0;
718 
719 	if (!cmd || queue->state == NVMET_TCP_Q_DISCONNECTING) {
720 		cmd = nvmet_tcp_fetch_cmd(queue);
721 		if (unlikely(!cmd))
722 			return 0;
723 	}
724 
725 	if (cmd->state == NVMET_TCP_SEND_DATA_PDU) {
726 		ret = nvmet_try_send_data_pdu(cmd);
727 		if (ret <= 0)
728 			goto done_send;
729 	}
730 
731 	if (cmd->state == NVMET_TCP_SEND_DATA) {
732 		ret = nvmet_try_send_data(cmd, last_in_batch);
733 		if (ret <= 0)
734 			goto done_send;
735 	}
736 
737 	if (cmd->state == NVMET_TCP_SEND_DDGST) {
738 		ret = nvmet_try_send_ddgst(cmd, last_in_batch);
739 		if (ret <= 0)
740 			goto done_send;
741 	}
742 
743 	if (cmd->state == NVMET_TCP_SEND_R2T) {
744 		ret = nvmet_try_send_r2t(cmd, last_in_batch);
745 		if (ret <= 0)
746 			goto done_send;
747 	}
748 
749 	if (cmd->state == NVMET_TCP_SEND_RESPONSE)
750 		ret = nvmet_try_send_response(cmd, last_in_batch);
751 
752 done_send:
753 	if (ret < 0) {
754 		if (ret == -EAGAIN)
755 			return 0;
756 		return ret;
757 	}
758 
759 	return 1;
760 }
761 
762 static int nvmet_tcp_try_send(struct nvmet_tcp_queue *queue,
763 		int budget, int *sends)
764 {
765 	int i, ret = 0;
766 
767 	for (i = 0; i < budget; i++) {
768 		ret = nvmet_tcp_try_send_one(queue, i == budget - 1);
769 		if (unlikely(ret < 0)) {
770 			nvmet_tcp_socket_error(queue, ret);
771 			goto done;
772 		} else if (ret == 0) {
773 			break;
774 		}
775 		(*sends)++;
776 	}
777 done:
778 	return ret;
779 }
780 
781 static void nvmet_prepare_receive_pdu(struct nvmet_tcp_queue *queue)
782 {
783 	queue->offset = 0;
784 	queue->left = sizeof(struct nvme_tcp_hdr);
785 	queue->cmd = NULL;
786 	queue->rcv_state = NVMET_TCP_RECV_PDU;
787 }
788 
789 static void nvmet_tcp_free_crypto(struct nvmet_tcp_queue *queue)
790 {
791 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
792 
793 	ahash_request_free(queue->rcv_hash);
794 	ahash_request_free(queue->snd_hash);
795 	crypto_free_ahash(tfm);
796 }
797 
798 static int nvmet_tcp_alloc_crypto(struct nvmet_tcp_queue *queue)
799 {
800 	struct crypto_ahash *tfm;
801 
802 	tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
803 	if (IS_ERR(tfm))
804 		return PTR_ERR(tfm);
805 
806 	queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
807 	if (!queue->snd_hash)
808 		goto free_tfm;
809 	ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
810 
811 	queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
812 	if (!queue->rcv_hash)
813 		goto free_snd_hash;
814 	ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
815 
816 	return 0;
817 free_snd_hash:
818 	ahash_request_free(queue->snd_hash);
819 free_tfm:
820 	crypto_free_ahash(tfm);
821 	return -ENOMEM;
822 }
823 
824 
825 static int nvmet_tcp_handle_icreq(struct nvmet_tcp_queue *queue)
826 {
827 	struct nvme_tcp_icreq_pdu *icreq = &queue->pdu.icreq;
828 	struct nvme_tcp_icresp_pdu *icresp = &queue->pdu.icresp;
829 	struct msghdr msg = {};
830 	struct kvec iov;
831 	int ret;
832 
833 	if (le32_to_cpu(icreq->hdr.plen) != sizeof(struct nvme_tcp_icreq_pdu)) {
834 		pr_err("bad nvme-tcp pdu length (%d)\n",
835 			le32_to_cpu(icreq->hdr.plen));
836 		nvmet_tcp_fatal_error(queue);
837 	}
838 
839 	if (icreq->pfv != NVME_TCP_PFV_1_0) {
840 		pr_err("queue %d: bad pfv %d\n", queue->idx, icreq->pfv);
841 		return -EPROTO;
842 	}
843 
844 	if (icreq->hpda != 0) {
845 		pr_err("queue %d: unsupported hpda %d\n", queue->idx,
846 			icreq->hpda);
847 		return -EPROTO;
848 	}
849 
850 	queue->hdr_digest = !!(icreq->digest & NVME_TCP_HDR_DIGEST_ENABLE);
851 	queue->data_digest = !!(icreq->digest & NVME_TCP_DATA_DIGEST_ENABLE);
852 	if (queue->hdr_digest || queue->data_digest) {
853 		ret = nvmet_tcp_alloc_crypto(queue);
854 		if (ret)
855 			return ret;
856 	}
857 
858 	memset(icresp, 0, sizeof(*icresp));
859 	icresp->hdr.type = nvme_tcp_icresp;
860 	icresp->hdr.hlen = sizeof(*icresp);
861 	icresp->hdr.pdo = 0;
862 	icresp->hdr.plen = cpu_to_le32(icresp->hdr.hlen);
863 	icresp->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
864 	icresp->maxdata = cpu_to_le32(0x400000); /* 16M arbitrary limit */
865 	icresp->cpda = 0;
866 	if (queue->hdr_digest)
867 		icresp->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
868 	if (queue->data_digest)
869 		icresp->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
870 
871 	iov.iov_base = icresp;
872 	iov.iov_len = sizeof(*icresp);
873 	ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
874 	if (ret < 0)
875 		goto free_crypto;
876 
877 	queue->state = NVMET_TCP_Q_LIVE;
878 	nvmet_prepare_receive_pdu(queue);
879 	return 0;
880 free_crypto:
881 	if (queue->hdr_digest || queue->data_digest)
882 		nvmet_tcp_free_crypto(queue);
883 	return ret;
884 }
885 
886 static void nvmet_tcp_handle_req_failure(struct nvmet_tcp_queue *queue,
887 		struct nvmet_tcp_cmd *cmd, struct nvmet_req *req)
888 {
889 	size_t data_len = le32_to_cpu(req->cmd->common.dptr.sgl.length);
890 	int ret;
891 
892 	/*
893 	 * This command has not been processed yet, hence we are trying to
894 	 * figure out if there is still pending data left to receive. If
895 	 * we don't, we can simply prepare for the next pdu and bail out,
896 	 * otherwise we will need to prepare a buffer and receive the
897 	 * stale data before continuing forward.
898 	 */
899 	if (!nvme_is_write(cmd->req.cmd) || !data_len ||
900 	    data_len > cmd->req.port->inline_data_size) {
901 		nvmet_prepare_receive_pdu(queue);
902 		return;
903 	}
904 
905 	ret = nvmet_tcp_map_data(cmd);
906 	if (unlikely(ret)) {
907 		pr_err("queue %d: failed to map data\n", queue->idx);
908 		nvmet_tcp_fatal_error(queue);
909 		return;
910 	}
911 
912 	queue->rcv_state = NVMET_TCP_RECV_DATA;
913 	nvmet_tcp_build_pdu_iovec(cmd);
914 	cmd->flags |= NVMET_TCP_F_INIT_FAILED;
915 }
916 
917 static int nvmet_tcp_handle_h2c_data_pdu(struct nvmet_tcp_queue *queue)
918 {
919 	struct nvme_tcp_data_pdu *data = &queue->pdu.data;
920 	struct nvmet_tcp_cmd *cmd;
921 
922 	if (likely(queue->nr_cmds)) {
923 		if (unlikely(data->ttag >= queue->nr_cmds)) {
924 			pr_err("queue %d: received out of bound ttag %u, nr_cmds %u\n",
925 				queue->idx, data->ttag, queue->nr_cmds);
926 			nvmet_tcp_fatal_error(queue);
927 			return -EPROTO;
928 		}
929 		cmd = &queue->cmds[data->ttag];
930 	} else {
931 		cmd = &queue->connect;
932 	}
933 
934 	if (le32_to_cpu(data->data_offset) != cmd->rbytes_done) {
935 		pr_err("ttag %u unexpected data offset %u (expected %u)\n",
936 			data->ttag, le32_to_cpu(data->data_offset),
937 			cmd->rbytes_done);
938 		/* FIXME: use path and transport errors */
939 		nvmet_req_complete(&cmd->req,
940 			NVME_SC_INVALID_FIELD | NVME_SC_DNR);
941 		return -EPROTO;
942 	}
943 
944 	cmd->pdu_len = le32_to_cpu(data->data_length);
945 	cmd->pdu_recv = 0;
946 	nvmet_tcp_build_pdu_iovec(cmd);
947 	queue->cmd = cmd;
948 	queue->rcv_state = NVMET_TCP_RECV_DATA;
949 
950 	return 0;
951 }
952 
953 static int nvmet_tcp_done_recv_pdu(struct nvmet_tcp_queue *queue)
954 {
955 	struct nvme_tcp_hdr *hdr = &queue->pdu.cmd.hdr;
956 	struct nvme_command *nvme_cmd = &queue->pdu.cmd.cmd;
957 	struct nvmet_req *req;
958 	int ret;
959 
960 	if (unlikely(queue->state == NVMET_TCP_Q_CONNECTING)) {
961 		if (hdr->type != nvme_tcp_icreq) {
962 			pr_err("unexpected pdu type (%d) before icreq\n",
963 				hdr->type);
964 			nvmet_tcp_fatal_error(queue);
965 			return -EPROTO;
966 		}
967 		return nvmet_tcp_handle_icreq(queue);
968 	}
969 
970 	if (unlikely(hdr->type == nvme_tcp_icreq)) {
971 		pr_err("queue %d: received icreq pdu in state %d\n",
972 			queue->idx, queue->state);
973 		nvmet_tcp_fatal_error(queue);
974 		return -EPROTO;
975 	}
976 
977 	if (hdr->type == nvme_tcp_h2c_data) {
978 		ret = nvmet_tcp_handle_h2c_data_pdu(queue);
979 		if (unlikely(ret))
980 			return ret;
981 		return 0;
982 	}
983 
984 	queue->cmd = nvmet_tcp_get_cmd(queue);
985 	if (unlikely(!queue->cmd)) {
986 		/* This should never happen */
987 		pr_err("queue %d: out of commands (%d) send_list_len: %d, opcode: %d",
988 			queue->idx, queue->nr_cmds, queue->send_list_len,
989 			nvme_cmd->common.opcode);
990 		nvmet_tcp_fatal_error(queue);
991 		return -ENOMEM;
992 	}
993 
994 	req = &queue->cmd->req;
995 	memcpy(req->cmd, nvme_cmd, sizeof(*nvme_cmd));
996 
997 	if (unlikely(!nvmet_req_init(req, &queue->nvme_cq,
998 			&queue->nvme_sq, &nvmet_tcp_ops))) {
999 		pr_err("failed cmd %p id %d opcode %d, data_len: %d\n",
1000 			req->cmd, req->cmd->common.command_id,
1001 			req->cmd->common.opcode,
1002 			le32_to_cpu(req->cmd->common.dptr.sgl.length));
1003 
1004 		nvmet_tcp_handle_req_failure(queue, queue->cmd, req);
1005 		return 0;
1006 	}
1007 
1008 	ret = nvmet_tcp_map_data(queue->cmd);
1009 	if (unlikely(ret)) {
1010 		pr_err("queue %d: failed to map data\n", queue->idx);
1011 		if (nvmet_tcp_has_inline_data(queue->cmd))
1012 			nvmet_tcp_fatal_error(queue);
1013 		else
1014 			nvmet_req_complete(req, ret);
1015 		ret = -EAGAIN;
1016 		goto out;
1017 	}
1018 
1019 	if (nvmet_tcp_need_data_in(queue->cmd)) {
1020 		if (nvmet_tcp_has_inline_data(queue->cmd)) {
1021 			queue->rcv_state = NVMET_TCP_RECV_DATA;
1022 			nvmet_tcp_build_pdu_iovec(queue->cmd);
1023 			return 0;
1024 		}
1025 		/* send back R2T */
1026 		nvmet_tcp_queue_response(&queue->cmd->req);
1027 		goto out;
1028 	}
1029 
1030 	queue->cmd->req.execute(&queue->cmd->req);
1031 out:
1032 	nvmet_prepare_receive_pdu(queue);
1033 	return ret;
1034 }
1035 
1036 static const u8 nvme_tcp_pdu_sizes[] = {
1037 	[nvme_tcp_icreq]	= sizeof(struct nvme_tcp_icreq_pdu),
1038 	[nvme_tcp_cmd]		= sizeof(struct nvme_tcp_cmd_pdu),
1039 	[nvme_tcp_h2c_data]	= sizeof(struct nvme_tcp_data_pdu),
1040 };
1041 
1042 static inline u8 nvmet_tcp_pdu_size(u8 type)
1043 {
1044 	size_t idx = type;
1045 
1046 	return (idx < ARRAY_SIZE(nvme_tcp_pdu_sizes) &&
1047 		nvme_tcp_pdu_sizes[idx]) ?
1048 			nvme_tcp_pdu_sizes[idx] : 0;
1049 }
1050 
1051 static inline bool nvmet_tcp_pdu_valid(u8 type)
1052 {
1053 	switch (type) {
1054 	case nvme_tcp_icreq:
1055 	case nvme_tcp_cmd:
1056 	case nvme_tcp_h2c_data:
1057 		/* fallthru */
1058 		return true;
1059 	}
1060 
1061 	return false;
1062 }
1063 
1064 static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue)
1065 {
1066 	struct nvme_tcp_hdr *hdr = &queue->pdu.cmd.hdr;
1067 	int len;
1068 	struct kvec iov;
1069 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
1070 
1071 recv:
1072 	iov.iov_base = (void *)&queue->pdu + queue->offset;
1073 	iov.iov_len = queue->left;
1074 	len = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1075 			iov.iov_len, msg.msg_flags);
1076 	if (unlikely(len < 0))
1077 		return len;
1078 
1079 	queue->offset += len;
1080 	queue->left -= len;
1081 	if (queue->left)
1082 		return -EAGAIN;
1083 
1084 	if (queue->offset == sizeof(struct nvme_tcp_hdr)) {
1085 		u8 hdgst = nvmet_tcp_hdgst_len(queue);
1086 
1087 		if (unlikely(!nvmet_tcp_pdu_valid(hdr->type))) {
1088 			pr_err("unexpected pdu type %d\n", hdr->type);
1089 			nvmet_tcp_fatal_error(queue);
1090 			return -EIO;
1091 		}
1092 
1093 		if (unlikely(hdr->hlen != nvmet_tcp_pdu_size(hdr->type))) {
1094 			pr_err("pdu %d bad hlen %d\n", hdr->type, hdr->hlen);
1095 			return -EIO;
1096 		}
1097 
1098 		queue->left = hdr->hlen - queue->offset + hdgst;
1099 		goto recv;
1100 	}
1101 
1102 	if (queue->hdr_digest &&
1103 	    nvmet_tcp_verify_hdgst(queue, &queue->pdu, hdr->hlen)) {
1104 		nvmet_tcp_fatal_error(queue); /* fatal */
1105 		return -EPROTO;
1106 	}
1107 
1108 	if (queue->data_digest &&
1109 	    nvmet_tcp_check_ddgst(queue, &queue->pdu)) {
1110 		nvmet_tcp_fatal_error(queue); /* fatal */
1111 		return -EPROTO;
1112 	}
1113 
1114 	return nvmet_tcp_done_recv_pdu(queue);
1115 }
1116 
1117 static void nvmet_tcp_prep_recv_ddgst(struct nvmet_tcp_cmd *cmd)
1118 {
1119 	struct nvmet_tcp_queue *queue = cmd->queue;
1120 
1121 	nvmet_tcp_calc_ddgst(queue->rcv_hash, cmd);
1122 	queue->offset = 0;
1123 	queue->left = NVME_TCP_DIGEST_LENGTH;
1124 	queue->rcv_state = NVMET_TCP_RECV_DDGST;
1125 }
1126 
1127 static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue)
1128 {
1129 	struct nvmet_tcp_cmd  *cmd = queue->cmd;
1130 	int ret;
1131 
1132 	while (msg_data_left(&cmd->recv_msg)) {
1133 		ret = sock_recvmsg(cmd->queue->sock, &cmd->recv_msg,
1134 			cmd->recv_msg.msg_flags);
1135 		if (ret <= 0)
1136 			return ret;
1137 
1138 		cmd->pdu_recv += ret;
1139 		cmd->rbytes_done += ret;
1140 	}
1141 
1142 	if (queue->data_digest) {
1143 		nvmet_tcp_prep_recv_ddgst(cmd);
1144 		return 0;
1145 	}
1146 
1147 	if (cmd->rbytes_done == cmd->req.transfer_len)
1148 		nvmet_tcp_execute_request(cmd);
1149 
1150 	nvmet_prepare_receive_pdu(queue);
1151 	return 0;
1152 }
1153 
1154 static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue)
1155 {
1156 	struct nvmet_tcp_cmd *cmd = queue->cmd;
1157 	int ret;
1158 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
1159 	struct kvec iov = {
1160 		.iov_base = (void *)&cmd->recv_ddgst + queue->offset,
1161 		.iov_len = queue->left
1162 	};
1163 
1164 	ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1165 			iov.iov_len, msg.msg_flags);
1166 	if (unlikely(ret < 0))
1167 		return ret;
1168 
1169 	queue->offset += ret;
1170 	queue->left -= ret;
1171 	if (queue->left)
1172 		return -EAGAIN;
1173 
1174 	if (queue->data_digest && cmd->exp_ddgst != cmd->recv_ddgst) {
1175 		pr_err("queue %d: cmd %d pdu (%d) data digest error: recv %#x expected %#x\n",
1176 			queue->idx, cmd->req.cmd->common.command_id,
1177 			queue->pdu.cmd.hdr.type, le32_to_cpu(cmd->recv_ddgst),
1178 			le32_to_cpu(cmd->exp_ddgst));
1179 		nvmet_req_uninit(&cmd->req);
1180 		nvmet_tcp_free_cmd_buffers(cmd);
1181 		nvmet_tcp_fatal_error(queue);
1182 		ret = -EPROTO;
1183 		goto out;
1184 	}
1185 
1186 	if (cmd->rbytes_done == cmd->req.transfer_len)
1187 		nvmet_tcp_execute_request(cmd);
1188 
1189 	ret = 0;
1190 out:
1191 	nvmet_prepare_receive_pdu(queue);
1192 	return ret;
1193 }
1194 
1195 static int nvmet_tcp_try_recv_one(struct nvmet_tcp_queue *queue)
1196 {
1197 	int result = 0;
1198 
1199 	if (unlikely(queue->rcv_state == NVMET_TCP_RECV_ERR))
1200 		return 0;
1201 
1202 	if (queue->rcv_state == NVMET_TCP_RECV_PDU) {
1203 		result = nvmet_tcp_try_recv_pdu(queue);
1204 		if (result != 0)
1205 			goto done_recv;
1206 	}
1207 
1208 	if (queue->rcv_state == NVMET_TCP_RECV_DATA) {
1209 		result = nvmet_tcp_try_recv_data(queue);
1210 		if (result != 0)
1211 			goto done_recv;
1212 	}
1213 
1214 	if (queue->rcv_state == NVMET_TCP_RECV_DDGST) {
1215 		result = nvmet_tcp_try_recv_ddgst(queue);
1216 		if (result != 0)
1217 			goto done_recv;
1218 	}
1219 
1220 done_recv:
1221 	if (result < 0) {
1222 		if (result == -EAGAIN)
1223 			return 0;
1224 		return result;
1225 	}
1226 	return 1;
1227 }
1228 
1229 static int nvmet_tcp_try_recv(struct nvmet_tcp_queue *queue,
1230 		int budget, int *recvs)
1231 {
1232 	int i, ret = 0;
1233 
1234 	for (i = 0; i < budget; i++) {
1235 		ret = nvmet_tcp_try_recv_one(queue);
1236 		if (unlikely(ret < 0)) {
1237 			nvmet_tcp_socket_error(queue, ret);
1238 			goto done;
1239 		} else if (ret == 0) {
1240 			break;
1241 		}
1242 		(*recvs)++;
1243 	}
1244 done:
1245 	return ret;
1246 }
1247 
1248 static void nvmet_tcp_schedule_release_queue(struct nvmet_tcp_queue *queue)
1249 {
1250 	spin_lock(&queue->state_lock);
1251 	if (queue->state != NVMET_TCP_Q_DISCONNECTING) {
1252 		queue->state = NVMET_TCP_Q_DISCONNECTING;
1253 		queue_work(nvmet_wq, &queue->release_work);
1254 	}
1255 	spin_unlock(&queue->state_lock);
1256 }
1257 
1258 static inline void nvmet_tcp_arm_queue_deadline(struct nvmet_tcp_queue *queue)
1259 {
1260 	queue->poll_end = jiffies + usecs_to_jiffies(idle_poll_period_usecs);
1261 }
1262 
1263 static bool nvmet_tcp_check_queue_deadline(struct nvmet_tcp_queue *queue,
1264 		int ops)
1265 {
1266 	if (!idle_poll_period_usecs)
1267 		return false;
1268 
1269 	if (ops)
1270 		nvmet_tcp_arm_queue_deadline(queue);
1271 
1272 	return !time_after(jiffies, queue->poll_end);
1273 }
1274 
1275 static void nvmet_tcp_io_work(struct work_struct *w)
1276 {
1277 	struct nvmet_tcp_queue *queue =
1278 		container_of(w, struct nvmet_tcp_queue, io_work);
1279 	bool pending;
1280 	int ret, ops = 0;
1281 
1282 	do {
1283 		pending = false;
1284 
1285 		ret = nvmet_tcp_try_recv(queue, NVMET_TCP_RECV_BUDGET, &ops);
1286 		if (ret > 0)
1287 			pending = true;
1288 		else if (ret < 0)
1289 			return;
1290 
1291 		ret = nvmet_tcp_try_send(queue, NVMET_TCP_SEND_BUDGET, &ops);
1292 		if (ret > 0)
1293 			pending = true;
1294 		else if (ret < 0)
1295 			return;
1296 
1297 	} while (pending && ops < NVMET_TCP_IO_WORK_BUDGET);
1298 
1299 	/*
1300 	 * Requeue the worker if idle deadline period is in progress or any
1301 	 * ops activity was recorded during the do-while loop above.
1302 	 */
1303 	if (nvmet_tcp_check_queue_deadline(queue, ops) || pending)
1304 		queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &queue->io_work);
1305 }
1306 
1307 static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue,
1308 		struct nvmet_tcp_cmd *c)
1309 {
1310 	u8 hdgst = nvmet_tcp_hdgst_len(queue);
1311 
1312 	c->queue = queue;
1313 	c->req.port = queue->port->nport;
1314 
1315 	c->cmd_pdu = page_frag_alloc(&queue->pf_cache,
1316 			sizeof(*c->cmd_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1317 	if (!c->cmd_pdu)
1318 		return -ENOMEM;
1319 	c->req.cmd = &c->cmd_pdu->cmd;
1320 
1321 	c->rsp_pdu = page_frag_alloc(&queue->pf_cache,
1322 			sizeof(*c->rsp_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1323 	if (!c->rsp_pdu)
1324 		goto out_free_cmd;
1325 	c->req.cqe = &c->rsp_pdu->cqe;
1326 
1327 	c->data_pdu = page_frag_alloc(&queue->pf_cache,
1328 			sizeof(*c->data_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1329 	if (!c->data_pdu)
1330 		goto out_free_rsp;
1331 
1332 	c->r2t_pdu = page_frag_alloc(&queue->pf_cache,
1333 			sizeof(*c->r2t_pdu) + hdgst, GFP_KERNEL | __GFP_ZERO);
1334 	if (!c->r2t_pdu)
1335 		goto out_free_data;
1336 
1337 	c->recv_msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1338 
1339 	list_add_tail(&c->entry, &queue->free_list);
1340 
1341 	return 0;
1342 out_free_data:
1343 	page_frag_free(c->data_pdu);
1344 out_free_rsp:
1345 	page_frag_free(c->rsp_pdu);
1346 out_free_cmd:
1347 	page_frag_free(c->cmd_pdu);
1348 	return -ENOMEM;
1349 }
1350 
1351 static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c)
1352 {
1353 	page_frag_free(c->r2t_pdu);
1354 	page_frag_free(c->data_pdu);
1355 	page_frag_free(c->rsp_pdu);
1356 	page_frag_free(c->cmd_pdu);
1357 }
1358 
1359 static int nvmet_tcp_alloc_cmds(struct nvmet_tcp_queue *queue)
1360 {
1361 	struct nvmet_tcp_cmd *cmds;
1362 	int i, ret = -EINVAL, nr_cmds = queue->nr_cmds;
1363 
1364 	cmds = kcalloc(nr_cmds, sizeof(struct nvmet_tcp_cmd), GFP_KERNEL);
1365 	if (!cmds)
1366 		goto out;
1367 
1368 	for (i = 0; i < nr_cmds; i++) {
1369 		ret = nvmet_tcp_alloc_cmd(queue, cmds + i);
1370 		if (ret)
1371 			goto out_free;
1372 	}
1373 
1374 	queue->cmds = cmds;
1375 
1376 	return 0;
1377 out_free:
1378 	while (--i >= 0)
1379 		nvmet_tcp_free_cmd(cmds + i);
1380 	kfree(cmds);
1381 out:
1382 	return ret;
1383 }
1384 
1385 static void nvmet_tcp_free_cmds(struct nvmet_tcp_queue *queue)
1386 {
1387 	struct nvmet_tcp_cmd *cmds = queue->cmds;
1388 	int i;
1389 
1390 	for (i = 0; i < queue->nr_cmds; i++)
1391 		nvmet_tcp_free_cmd(cmds + i);
1392 
1393 	nvmet_tcp_free_cmd(&queue->connect);
1394 	kfree(cmds);
1395 }
1396 
1397 static void nvmet_tcp_restore_socket_callbacks(struct nvmet_tcp_queue *queue)
1398 {
1399 	struct socket *sock = queue->sock;
1400 
1401 	write_lock_bh(&sock->sk->sk_callback_lock);
1402 	sock->sk->sk_data_ready =  queue->data_ready;
1403 	sock->sk->sk_state_change = queue->state_change;
1404 	sock->sk->sk_write_space = queue->write_space;
1405 	sock->sk->sk_user_data = NULL;
1406 	write_unlock_bh(&sock->sk->sk_callback_lock);
1407 }
1408 
1409 static void nvmet_tcp_uninit_data_in_cmds(struct nvmet_tcp_queue *queue)
1410 {
1411 	struct nvmet_tcp_cmd *cmd = queue->cmds;
1412 	int i;
1413 
1414 	for (i = 0; i < queue->nr_cmds; i++, cmd++) {
1415 		if (nvmet_tcp_need_data_in(cmd))
1416 			nvmet_req_uninit(&cmd->req);
1417 	}
1418 
1419 	if (!queue->nr_cmds && nvmet_tcp_need_data_in(&queue->connect)) {
1420 		/* failed in connect */
1421 		nvmet_req_uninit(&queue->connect.req);
1422 	}
1423 }
1424 
1425 static void nvmet_tcp_free_cmd_data_in_buffers(struct nvmet_tcp_queue *queue)
1426 {
1427 	struct nvmet_tcp_cmd *cmd = queue->cmds;
1428 	int i;
1429 
1430 	for (i = 0; i < queue->nr_cmds; i++, cmd++) {
1431 		if (nvmet_tcp_need_data_in(cmd))
1432 			nvmet_tcp_free_cmd_buffers(cmd);
1433 	}
1434 
1435 	if (!queue->nr_cmds && nvmet_tcp_need_data_in(&queue->connect))
1436 		nvmet_tcp_free_cmd_buffers(&queue->connect);
1437 }
1438 
1439 static void nvmet_tcp_release_queue_work(struct work_struct *w)
1440 {
1441 	struct page *page;
1442 	struct nvmet_tcp_queue *queue =
1443 		container_of(w, struct nvmet_tcp_queue, release_work);
1444 
1445 	mutex_lock(&nvmet_tcp_queue_mutex);
1446 	list_del_init(&queue->queue_list);
1447 	mutex_unlock(&nvmet_tcp_queue_mutex);
1448 
1449 	nvmet_tcp_restore_socket_callbacks(queue);
1450 	cancel_work_sync(&queue->io_work);
1451 	/* stop accepting incoming data */
1452 	queue->rcv_state = NVMET_TCP_RECV_ERR;
1453 
1454 	nvmet_tcp_uninit_data_in_cmds(queue);
1455 	nvmet_sq_destroy(&queue->nvme_sq);
1456 	cancel_work_sync(&queue->io_work);
1457 	nvmet_tcp_free_cmd_data_in_buffers(queue);
1458 	sock_release(queue->sock);
1459 	nvmet_tcp_free_cmds(queue);
1460 	if (queue->hdr_digest || queue->data_digest)
1461 		nvmet_tcp_free_crypto(queue);
1462 	ida_free(&nvmet_tcp_queue_ida, queue->idx);
1463 
1464 	page = virt_to_head_page(queue->pf_cache.va);
1465 	__page_frag_cache_drain(page, queue->pf_cache.pagecnt_bias);
1466 	kfree(queue);
1467 }
1468 
1469 static void nvmet_tcp_data_ready(struct sock *sk)
1470 {
1471 	struct nvmet_tcp_queue *queue;
1472 
1473 	trace_sk_data_ready(sk);
1474 
1475 	read_lock_bh(&sk->sk_callback_lock);
1476 	queue = sk->sk_user_data;
1477 	if (likely(queue))
1478 		queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &queue->io_work);
1479 	read_unlock_bh(&sk->sk_callback_lock);
1480 }
1481 
1482 static void nvmet_tcp_write_space(struct sock *sk)
1483 {
1484 	struct nvmet_tcp_queue *queue;
1485 
1486 	read_lock_bh(&sk->sk_callback_lock);
1487 	queue = sk->sk_user_data;
1488 	if (unlikely(!queue))
1489 		goto out;
1490 
1491 	if (unlikely(queue->state == NVMET_TCP_Q_CONNECTING)) {
1492 		queue->write_space(sk);
1493 		goto out;
1494 	}
1495 
1496 	if (sk_stream_is_writeable(sk)) {
1497 		clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1498 		queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &queue->io_work);
1499 	}
1500 out:
1501 	read_unlock_bh(&sk->sk_callback_lock);
1502 }
1503 
1504 static void nvmet_tcp_state_change(struct sock *sk)
1505 {
1506 	struct nvmet_tcp_queue *queue;
1507 
1508 	read_lock_bh(&sk->sk_callback_lock);
1509 	queue = sk->sk_user_data;
1510 	if (!queue)
1511 		goto done;
1512 
1513 	switch (sk->sk_state) {
1514 	case TCP_FIN_WAIT2:
1515 	case TCP_LAST_ACK:
1516 		break;
1517 	case TCP_FIN_WAIT1:
1518 	case TCP_CLOSE_WAIT:
1519 	case TCP_CLOSE:
1520 		/* FALLTHRU */
1521 		nvmet_tcp_schedule_release_queue(queue);
1522 		break;
1523 	default:
1524 		pr_warn("queue %d unhandled state %d\n",
1525 			queue->idx, sk->sk_state);
1526 	}
1527 done:
1528 	read_unlock_bh(&sk->sk_callback_lock);
1529 }
1530 
1531 static int nvmet_tcp_set_queue_sock(struct nvmet_tcp_queue *queue)
1532 {
1533 	struct socket *sock = queue->sock;
1534 	struct inet_sock *inet = inet_sk(sock->sk);
1535 	int ret;
1536 
1537 	ret = kernel_getsockname(sock,
1538 		(struct sockaddr *)&queue->sockaddr);
1539 	if (ret < 0)
1540 		return ret;
1541 
1542 	ret = kernel_getpeername(sock,
1543 		(struct sockaddr *)&queue->sockaddr_peer);
1544 	if (ret < 0)
1545 		return ret;
1546 
1547 	/*
1548 	 * Cleanup whatever is sitting in the TCP transmit queue on socket
1549 	 * close. This is done to prevent stale data from being sent should
1550 	 * the network connection be restored before TCP times out.
1551 	 */
1552 	sock_no_linger(sock->sk);
1553 
1554 	if (so_priority > 0)
1555 		sock_set_priority(sock->sk, so_priority);
1556 
1557 	/* Set socket type of service */
1558 	if (inet->rcv_tos > 0)
1559 		ip_sock_set_tos(sock->sk, inet->rcv_tos);
1560 
1561 	ret = 0;
1562 	write_lock_bh(&sock->sk->sk_callback_lock);
1563 	if (sock->sk->sk_state != TCP_ESTABLISHED) {
1564 		/*
1565 		 * If the socket is already closing, don't even start
1566 		 * consuming it
1567 		 */
1568 		ret = -ENOTCONN;
1569 	} else {
1570 		sock->sk->sk_user_data = queue;
1571 		queue->data_ready = sock->sk->sk_data_ready;
1572 		sock->sk->sk_data_ready = nvmet_tcp_data_ready;
1573 		queue->state_change = sock->sk->sk_state_change;
1574 		sock->sk->sk_state_change = nvmet_tcp_state_change;
1575 		queue->write_space = sock->sk->sk_write_space;
1576 		sock->sk->sk_write_space = nvmet_tcp_write_space;
1577 		if (idle_poll_period_usecs)
1578 			nvmet_tcp_arm_queue_deadline(queue);
1579 		queue_work_on(queue_cpu(queue), nvmet_tcp_wq, &queue->io_work);
1580 	}
1581 	write_unlock_bh(&sock->sk->sk_callback_lock);
1582 
1583 	return ret;
1584 }
1585 
1586 static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,
1587 		struct socket *newsock)
1588 {
1589 	struct nvmet_tcp_queue *queue;
1590 	int ret;
1591 
1592 	queue = kzalloc(sizeof(*queue), GFP_KERNEL);
1593 	if (!queue)
1594 		return -ENOMEM;
1595 
1596 	INIT_WORK(&queue->release_work, nvmet_tcp_release_queue_work);
1597 	INIT_WORK(&queue->io_work, nvmet_tcp_io_work);
1598 	queue->sock = newsock;
1599 	queue->port = port;
1600 	queue->nr_cmds = 0;
1601 	spin_lock_init(&queue->state_lock);
1602 	queue->state = NVMET_TCP_Q_CONNECTING;
1603 	INIT_LIST_HEAD(&queue->free_list);
1604 	init_llist_head(&queue->resp_list);
1605 	INIT_LIST_HEAD(&queue->resp_send_list);
1606 
1607 	queue->idx = ida_alloc(&nvmet_tcp_queue_ida, GFP_KERNEL);
1608 	if (queue->idx < 0) {
1609 		ret = queue->idx;
1610 		goto out_free_queue;
1611 	}
1612 
1613 	ret = nvmet_tcp_alloc_cmd(queue, &queue->connect);
1614 	if (ret)
1615 		goto out_ida_remove;
1616 
1617 	ret = nvmet_sq_init(&queue->nvme_sq);
1618 	if (ret)
1619 		goto out_free_connect;
1620 
1621 	nvmet_prepare_receive_pdu(queue);
1622 
1623 	mutex_lock(&nvmet_tcp_queue_mutex);
1624 	list_add_tail(&queue->queue_list, &nvmet_tcp_queue_list);
1625 	mutex_unlock(&nvmet_tcp_queue_mutex);
1626 
1627 	ret = nvmet_tcp_set_queue_sock(queue);
1628 	if (ret)
1629 		goto out_destroy_sq;
1630 
1631 	return 0;
1632 out_destroy_sq:
1633 	mutex_lock(&nvmet_tcp_queue_mutex);
1634 	list_del_init(&queue->queue_list);
1635 	mutex_unlock(&nvmet_tcp_queue_mutex);
1636 	nvmet_sq_destroy(&queue->nvme_sq);
1637 out_free_connect:
1638 	nvmet_tcp_free_cmd(&queue->connect);
1639 out_ida_remove:
1640 	ida_free(&nvmet_tcp_queue_ida, queue->idx);
1641 out_free_queue:
1642 	kfree(queue);
1643 	return ret;
1644 }
1645 
1646 static void nvmet_tcp_accept_work(struct work_struct *w)
1647 {
1648 	struct nvmet_tcp_port *port =
1649 		container_of(w, struct nvmet_tcp_port, accept_work);
1650 	struct socket *newsock;
1651 	int ret;
1652 
1653 	while (true) {
1654 		ret = kernel_accept(port->sock, &newsock, O_NONBLOCK);
1655 		if (ret < 0) {
1656 			if (ret != -EAGAIN)
1657 				pr_warn("failed to accept err=%d\n", ret);
1658 			return;
1659 		}
1660 		ret = nvmet_tcp_alloc_queue(port, newsock);
1661 		if (ret) {
1662 			pr_err("failed to allocate queue\n");
1663 			sock_release(newsock);
1664 		}
1665 	}
1666 }
1667 
1668 static void nvmet_tcp_listen_data_ready(struct sock *sk)
1669 {
1670 	struct nvmet_tcp_port *port;
1671 
1672 	trace_sk_data_ready(sk);
1673 
1674 	read_lock_bh(&sk->sk_callback_lock);
1675 	port = sk->sk_user_data;
1676 	if (!port)
1677 		goto out;
1678 
1679 	if (sk->sk_state == TCP_LISTEN)
1680 		queue_work(nvmet_wq, &port->accept_work);
1681 out:
1682 	read_unlock_bh(&sk->sk_callback_lock);
1683 }
1684 
1685 static int nvmet_tcp_add_port(struct nvmet_port *nport)
1686 {
1687 	struct nvmet_tcp_port *port;
1688 	__kernel_sa_family_t af;
1689 	int ret;
1690 
1691 	port = kzalloc(sizeof(*port), GFP_KERNEL);
1692 	if (!port)
1693 		return -ENOMEM;
1694 
1695 	switch (nport->disc_addr.adrfam) {
1696 	case NVMF_ADDR_FAMILY_IP4:
1697 		af = AF_INET;
1698 		break;
1699 	case NVMF_ADDR_FAMILY_IP6:
1700 		af = AF_INET6;
1701 		break;
1702 	default:
1703 		pr_err("address family %d not supported\n",
1704 				nport->disc_addr.adrfam);
1705 		ret = -EINVAL;
1706 		goto err_port;
1707 	}
1708 
1709 	ret = inet_pton_with_scope(&init_net, af, nport->disc_addr.traddr,
1710 			nport->disc_addr.trsvcid, &port->addr);
1711 	if (ret) {
1712 		pr_err("malformed ip/port passed: %s:%s\n",
1713 			nport->disc_addr.traddr, nport->disc_addr.trsvcid);
1714 		goto err_port;
1715 	}
1716 
1717 	port->nport = nport;
1718 	INIT_WORK(&port->accept_work, nvmet_tcp_accept_work);
1719 	if (port->nport->inline_data_size < 0)
1720 		port->nport->inline_data_size = NVMET_TCP_DEF_INLINE_DATA_SIZE;
1721 
1722 	ret = sock_create(port->addr.ss_family, SOCK_STREAM,
1723 				IPPROTO_TCP, &port->sock);
1724 	if (ret) {
1725 		pr_err("failed to create a socket\n");
1726 		goto err_port;
1727 	}
1728 
1729 	port->sock->sk->sk_user_data = port;
1730 	port->data_ready = port->sock->sk->sk_data_ready;
1731 	port->sock->sk->sk_data_ready = nvmet_tcp_listen_data_ready;
1732 	sock_set_reuseaddr(port->sock->sk);
1733 	tcp_sock_set_nodelay(port->sock->sk);
1734 	if (so_priority > 0)
1735 		sock_set_priority(port->sock->sk, so_priority);
1736 
1737 	ret = kernel_bind(port->sock, (struct sockaddr *)&port->addr,
1738 			sizeof(port->addr));
1739 	if (ret) {
1740 		pr_err("failed to bind port socket %d\n", ret);
1741 		goto err_sock;
1742 	}
1743 
1744 	ret = kernel_listen(port->sock, 128);
1745 	if (ret) {
1746 		pr_err("failed to listen %d on port sock\n", ret);
1747 		goto err_sock;
1748 	}
1749 
1750 	nport->priv = port;
1751 	pr_info("enabling port %d (%pISpc)\n",
1752 		le16_to_cpu(nport->disc_addr.portid), &port->addr);
1753 
1754 	return 0;
1755 
1756 err_sock:
1757 	sock_release(port->sock);
1758 err_port:
1759 	kfree(port);
1760 	return ret;
1761 }
1762 
1763 static void nvmet_tcp_destroy_port_queues(struct nvmet_tcp_port *port)
1764 {
1765 	struct nvmet_tcp_queue *queue;
1766 
1767 	mutex_lock(&nvmet_tcp_queue_mutex);
1768 	list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list)
1769 		if (queue->port == port)
1770 			kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1771 	mutex_unlock(&nvmet_tcp_queue_mutex);
1772 }
1773 
1774 static void nvmet_tcp_remove_port(struct nvmet_port *nport)
1775 {
1776 	struct nvmet_tcp_port *port = nport->priv;
1777 
1778 	write_lock_bh(&port->sock->sk->sk_callback_lock);
1779 	port->sock->sk->sk_data_ready = port->data_ready;
1780 	port->sock->sk->sk_user_data = NULL;
1781 	write_unlock_bh(&port->sock->sk->sk_callback_lock);
1782 	cancel_work_sync(&port->accept_work);
1783 	/*
1784 	 * Destroy the remaining queues, which are not belong to any
1785 	 * controller yet.
1786 	 */
1787 	nvmet_tcp_destroy_port_queues(port);
1788 
1789 	sock_release(port->sock);
1790 	kfree(port);
1791 }
1792 
1793 static void nvmet_tcp_delete_ctrl(struct nvmet_ctrl *ctrl)
1794 {
1795 	struct nvmet_tcp_queue *queue;
1796 
1797 	mutex_lock(&nvmet_tcp_queue_mutex);
1798 	list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list)
1799 		if (queue->nvme_sq.ctrl == ctrl)
1800 			kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1801 	mutex_unlock(&nvmet_tcp_queue_mutex);
1802 }
1803 
1804 static u16 nvmet_tcp_install_queue(struct nvmet_sq *sq)
1805 {
1806 	struct nvmet_tcp_queue *queue =
1807 		container_of(sq, struct nvmet_tcp_queue, nvme_sq);
1808 
1809 	if (sq->qid == 0) {
1810 		/* Let inflight controller teardown complete */
1811 		flush_workqueue(nvmet_wq);
1812 	}
1813 
1814 	queue->nr_cmds = sq->size * 2;
1815 	if (nvmet_tcp_alloc_cmds(queue))
1816 		return NVME_SC_INTERNAL;
1817 	return 0;
1818 }
1819 
1820 static void nvmet_tcp_disc_port_addr(struct nvmet_req *req,
1821 		struct nvmet_port *nport, char *traddr)
1822 {
1823 	struct nvmet_tcp_port *port = nport->priv;
1824 
1825 	if (inet_addr_is_any((struct sockaddr *)&port->addr)) {
1826 		struct nvmet_tcp_cmd *cmd =
1827 			container_of(req, struct nvmet_tcp_cmd, req);
1828 		struct nvmet_tcp_queue *queue = cmd->queue;
1829 
1830 		sprintf(traddr, "%pISc", (struct sockaddr *)&queue->sockaddr);
1831 	} else {
1832 		memcpy(traddr, nport->disc_addr.traddr, NVMF_TRADDR_SIZE);
1833 	}
1834 }
1835 
1836 static const struct nvmet_fabrics_ops nvmet_tcp_ops = {
1837 	.owner			= THIS_MODULE,
1838 	.type			= NVMF_TRTYPE_TCP,
1839 	.msdbd			= 1,
1840 	.add_port		= nvmet_tcp_add_port,
1841 	.remove_port		= nvmet_tcp_remove_port,
1842 	.queue_response		= nvmet_tcp_queue_response,
1843 	.delete_ctrl		= nvmet_tcp_delete_ctrl,
1844 	.install_queue		= nvmet_tcp_install_queue,
1845 	.disc_traddr		= nvmet_tcp_disc_port_addr,
1846 };
1847 
1848 static int __init nvmet_tcp_init(void)
1849 {
1850 	int ret;
1851 
1852 	nvmet_tcp_wq = alloc_workqueue("nvmet_tcp_wq",
1853 				WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
1854 	if (!nvmet_tcp_wq)
1855 		return -ENOMEM;
1856 
1857 	ret = nvmet_register_transport(&nvmet_tcp_ops);
1858 	if (ret)
1859 		goto err;
1860 
1861 	return 0;
1862 err:
1863 	destroy_workqueue(nvmet_tcp_wq);
1864 	return ret;
1865 }
1866 
1867 static void __exit nvmet_tcp_exit(void)
1868 {
1869 	struct nvmet_tcp_queue *queue;
1870 
1871 	nvmet_unregister_transport(&nvmet_tcp_ops);
1872 
1873 	flush_workqueue(nvmet_wq);
1874 	mutex_lock(&nvmet_tcp_queue_mutex);
1875 	list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list)
1876 		kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1877 	mutex_unlock(&nvmet_tcp_queue_mutex);
1878 	flush_workqueue(nvmet_wq);
1879 
1880 	destroy_workqueue(nvmet_tcp_wq);
1881 }
1882 
1883 module_init(nvmet_tcp_init);
1884 module_exit(nvmet_tcp_exit);
1885 
1886 MODULE_LICENSE("GPL v2");
1887 MODULE_ALIAS("nvmet-transport-3"); /* 3 == NVMF_TRTYPE_TCP */
1888