xref: /openbmc/linux/drivers/nvme/host/tcp.c (revision 15e3ae36)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NVMe over Fabrics TCP host.
4  * Copyright (c) 2018 Lightbits Labs. All rights reserved.
5  */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/err.h>
11 #include <linux/nvme-tcp.h>
12 #include <net/sock.h>
13 #include <net/tcp.h>
14 #include <linux/blk-mq.h>
15 #include <crypto/hash.h>
16 #include <net/busy_poll.h>
17 
18 #include "nvme.h"
19 #include "fabrics.h"
20 
21 struct nvme_tcp_queue;
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, "nvme tcp socket optimize priority");
32 
33 enum nvme_tcp_send_state {
34 	NVME_TCP_SEND_CMD_PDU = 0,
35 	NVME_TCP_SEND_H2C_PDU,
36 	NVME_TCP_SEND_DATA,
37 	NVME_TCP_SEND_DDGST,
38 };
39 
40 struct nvme_tcp_request {
41 	struct nvme_request	req;
42 	void			*pdu;
43 	struct nvme_tcp_queue	*queue;
44 	u32			data_len;
45 	u32			pdu_len;
46 	u32			pdu_sent;
47 	u16			ttag;
48 	struct list_head	entry;
49 	__le32			ddgst;
50 
51 	struct bio		*curr_bio;
52 	struct iov_iter		iter;
53 
54 	/* send state */
55 	size_t			offset;
56 	size_t			data_sent;
57 	enum nvme_tcp_send_state state;
58 };
59 
60 enum nvme_tcp_queue_flags {
61 	NVME_TCP_Q_ALLOCATED	= 0,
62 	NVME_TCP_Q_LIVE		= 1,
63 };
64 
65 enum nvme_tcp_recv_state {
66 	NVME_TCP_RECV_PDU = 0,
67 	NVME_TCP_RECV_DATA,
68 	NVME_TCP_RECV_DDGST,
69 };
70 
71 struct nvme_tcp_ctrl;
72 struct nvme_tcp_queue {
73 	struct socket		*sock;
74 	struct work_struct	io_work;
75 	int			io_cpu;
76 
77 	spinlock_t		lock;
78 	struct list_head	send_list;
79 
80 	/* recv state */
81 	void			*pdu;
82 	int			pdu_remaining;
83 	int			pdu_offset;
84 	size_t			data_remaining;
85 	size_t			ddgst_remaining;
86 	unsigned int		nr_cqe;
87 
88 	/* send state */
89 	struct nvme_tcp_request *request;
90 
91 	int			queue_size;
92 	size_t			cmnd_capsule_len;
93 	struct nvme_tcp_ctrl	*ctrl;
94 	unsigned long		flags;
95 	bool			rd_enabled;
96 
97 	bool			hdr_digest;
98 	bool			data_digest;
99 	struct ahash_request	*rcv_hash;
100 	struct ahash_request	*snd_hash;
101 	__le32			exp_ddgst;
102 	__le32			recv_ddgst;
103 
104 	struct page_frag_cache	pf_cache;
105 
106 	void (*state_change)(struct sock *);
107 	void (*data_ready)(struct sock *);
108 	void (*write_space)(struct sock *);
109 };
110 
111 struct nvme_tcp_ctrl {
112 	/* read only in the hot path */
113 	struct nvme_tcp_queue	*queues;
114 	struct blk_mq_tag_set	tag_set;
115 
116 	/* other member variables */
117 	struct list_head	list;
118 	struct blk_mq_tag_set	admin_tag_set;
119 	struct sockaddr_storage addr;
120 	struct sockaddr_storage src_addr;
121 	struct nvme_ctrl	ctrl;
122 
123 	struct work_struct	err_work;
124 	struct delayed_work	connect_work;
125 	struct nvme_tcp_request async_req;
126 	u32			io_queues[HCTX_MAX_TYPES];
127 };
128 
129 static LIST_HEAD(nvme_tcp_ctrl_list);
130 static DEFINE_MUTEX(nvme_tcp_ctrl_mutex);
131 static struct workqueue_struct *nvme_tcp_wq;
132 static struct blk_mq_ops nvme_tcp_mq_ops;
133 static struct blk_mq_ops nvme_tcp_admin_mq_ops;
134 
135 static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl)
136 {
137 	return container_of(ctrl, struct nvme_tcp_ctrl, ctrl);
138 }
139 
140 static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
141 {
142 	return queue - queue->ctrl->queues;
143 }
144 
145 static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue)
146 {
147 	u32 queue_idx = nvme_tcp_queue_id(queue);
148 
149 	if (queue_idx == 0)
150 		return queue->ctrl->admin_tag_set.tags[queue_idx];
151 	return queue->ctrl->tag_set.tags[queue_idx - 1];
152 }
153 
154 static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue)
155 {
156 	return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
157 }
158 
159 static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue)
160 {
161 	return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
162 }
163 
164 static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_queue *queue)
165 {
166 	return queue->cmnd_capsule_len - sizeof(struct nvme_command);
167 }
168 
169 static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req)
170 {
171 	return req == &req->queue->ctrl->async_req;
172 }
173 
174 static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req)
175 {
176 	struct request *rq;
177 
178 	if (unlikely(nvme_tcp_async_req(req)))
179 		return false; /* async events don't have a request */
180 
181 	rq = blk_mq_rq_from_pdu(req);
182 
183 	return rq_data_dir(rq) == WRITE && req->data_len &&
184 		req->data_len <= nvme_tcp_inline_data_size(req->queue);
185 }
186 
187 static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req)
188 {
189 	return req->iter.bvec->bv_page;
190 }
191 
192 static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req)
193 {
194 	return req->iter.bvec->bv_offset + req->iter.iov_offset;
195 }
196 
197 static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req)
198 {
199 	return min_t(size_t, req->iter.bvec->bv_len - req->iter.iov_offset,
200 			req->pdu_len - req->pdu_sent);
201 }
202 
203 static inline size_t nvme_tcp_req_offset(struct nvme_tcp_request *req)
204 {
205 	return req->iter.iov_offset;
206 }
207 
208 static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req)
209 {
210 	return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ?
211 			req->pdu_len - req->pdu_sent : 0;
212 }
213 
214 static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
215 		int len)
216 {
217 	return nvme_tcp_pdu_data_left(req) <= len;
218 }
219 
220 static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
221 		unsigned int dir)
222 {
223 	struct request *rq = blk_mq_rq_from_pdu(req);
224 	struct bio_vec *vec;
225 	unsigned int size;
226 	int nsegs;
227 	size_t offset;
228 
229 	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
230 		vec = &rq->special_vec;
231 		nsegs = 1;
232 		size = blk_rq_payload_bytes(rq);
233 		offset = 0;
234 	} else {
235 		struct bio *bio = req->curr_bio;
236 
237 		vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
238 		nsegs = bio_segments(bio);
239 		size = bio->bi_iter.bi_size;
240 		offset = bio->bi_iter.bi_bvec_done;
241 	}
242 
243 	iov_iter_bvec(&req->iter, dir, vec, nsegs, size);
244 	req->iter.iov_offset = offset;
245 }
246 
247 static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req,
248 		int len)
249 {
250 	req->data_sent += len;
251 	req->pdu_sent += len;
252 	iov_iter_advance(&req->iter, len);
253 	if (!iov_iter_count(&req->iter) &&
254 	    req->data_sent < req->data_len) {
255 		req->curr_bio = req->curr_bio->bi_next;
256 		nvme_tcp_init_iter(req, WRITE);
257 	}
258 }
259 
260 static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req)
261 {
262 	struct nvme_tcp_queue *queue = req->queue;
263 
264 	spin_lock(&queue->lock);
265 	list_add_tail(&req->entry, &queue->send_list);
266 	spin_unlock(&queue->lock);
267 
268 	queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
269 }
270 
271 static inline struct nvme_tcp_request *
272 nvme_tcp_fetch_request(struct nvme_tcp_queue *queue)
273 {
274 	struct nvme_tcp_request *req;
275 
276 	spin_lock(&queue->lock);
277 	req = list_first_entry_or_null(&queue->send_list,
278 			struct nvme_tcp_request, entry);
279 	if (req)
280 		list_del(&req->entry);
281 	spin_unlock(&queue->lock);
282 
283 	return req;
284 }
285 
286 static inline void nvme_tcp_ddgst_final(struct ahash_request *hash,
287 		__le32 *dgst)
288 {
289 	ahash_request_set_crypt(hash, NULL, (u8 *)dgst, 0);
290 	crypto_ahash_final(hash);
291 }
292 
293 static inline void nvme_tcp_ddgst_update(struct ahash_request *hash,
294 		struct page *page, off_t off, size_t len)
295 {
296 	struct scatterlist sg;
297 
298 	sg_init_marker(&sg, 1);
299 	sg_set_page(&sg, page, len, off);
300 	ahash_request_set_crypt(hash, &sg, NULL, len);
301 	crypto_ahash_update(hash);
302 }
303 
304 static inline void nvme_tcp_hdgst(struct ahash_request *hash,
305 		void *pdu, size_t len)
306 {
307 	struct scatterlist sg;
308 
309 	sg_init_one(&sg, pdu, len);
310 	ahash_request_set_crypt(hash, &sg, pdu + len, len);
311 	crypto_ahash_digest(hash);
312 }
313 
314 static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue,
315 		void *pdu, size_t pdu_len)
316 {
317 	struct nvme_tcp_hdr *hdr = pdu;
318 	__le32 recv_digest;
319 	__le32 exp_digest;
320 
321 	if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
322 		dev_err(queue->ctrl->ctrl.device,
323 			"queue %d: header digest flag is cleared\n",
324 			nvme_tcp_queue_id(queue));
325 		return -EPROTO;
326 	}
327 
328 	recv_digest = *(__le32 *)(pdu + hdr->hlen);
329 	nvme_tcp_hdgst(queue->rcv_hash, pdu, pdu_len);
330 	exp_digest = *(__le32 *)(pdu + hdr->hlen);
331 	if (recv_digest != exp_digest) {
332 		dev_err(queue->ctrl->ctrl.device,
333 			"header digest error: recv %#x expected %#x\n",
334 			le32_to_cpu(recv_digest), le32_to_cpu(exp_digest));
335 		return -EIO;
336 	}
337 
338 	return 0;
339 }
340 
341 static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu)
342 {
343 	struct nvme_tcp_hdr *hdr = pdu;
344 	u8 digest_len = nvme_tcp_hdgst_len(queue);
345 	u32 len;
346 
347 	len = le32_to_cpu(hdr->plen) - hdr->hlen -
348 		((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0);
349 
350 	if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
351 		dev_err(queue->ctrl->ctrl.device,
352 			"queue %d: data digest flag is cleared\n",
353 		nvme_tcp_queue_id(queue));
354 		return -EPROTO;
355 	}
356 	crypto_ahash_init(queue->rcv_hash);
357 
358 	return 0;
359 }
360 
361 static void nvme_tcp_exit_request(struct blk_mq_tag_set *set,
362 		struct request *rq, unsigned int hctx_idx)
363 {
364 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
365 
366 	page_frag_free(req->pdu);
367 }
368 
369 static int nvme_tcp_init_request(struct blk_mq_tag_set *set,
370 		struct request *rq, unsigned int hctx_idx,
371 		unsigned int numa_node)
372 {
373 	struct nvme_tcp_ctrl *ctrl = set->driver_data;
374 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
375 	int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
376 	struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx];
377 	u8 hdgst = nvme_tcp_hdgst_len(queue);
378 
379 	req->pdu = page_frag_alloc(&queue->pf_cache,
380 		sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
381 		GFP_KERNEL | __GFP_ZERO);
382 	if (!req->pdu)
383 		return -ENOMEM;
384 
385 	req->queue = queue;
386 	nvme_req(rq)->ctrl = &ctrl->ctrl;
387 
388 	return 0;
389 }
390 
391 static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
392 		unsigned int hctx_idx)
393 {
394 	struct nvme_tcp_ctrl *ctrl = data;
395 	struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1];
396 
397 	hctx->driver_data = queue;
398 	return 0;
399 }
400 
401 static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
402 		unsigned int hctx_idx)
403 {
404 	struct nvme_tcp_ctrl *ctrl = data;
405 	struct nvme_tcp_queue *queue = &ctrl->queues[0];
406 
407 	hctx->driver_data = queue;
408 	return 0;
409 }
410 
411 static enum nvme_tcp_recv_state
412 nvme_tcp_recv_state(struct nvme_tcp_queue *queue)
413 {
414 	return  (queue->pdu_remaining) ? NVME_TCP_RECV_PDU :
415 		(queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST :
416 		NVME_TCP_RECV_DATA;
417 }
418 
419 static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
420 {
421 	queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) +
422 				nvme_tcp_hdgst_len(queue);
423 	queue->pdu_offset = 0;
424 	queue->data_remaining = -1;
425 	queue->ddgst_remaining = 0;
426 }
427 
428 static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
429 {
430 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
431 		return;
432 
433 	queue_work(nvme_reset_wq, &to_tcp_ctrl(ctrl)->err_work);
434 }
435 
436 static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue,
437 		struct nvme_completion *cqe)
438 {
439 	struct request *rq;
440 
441 	rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), cqe->command_id);
442 	if (!rq) {
443 		dev_err(queue->ctrl->ctrl.device,
444 			"queue %d tag 0x%x not found\n",
445 			nvme_tcp_queue_id(queue), cqe->command_id);
446 		nvme_tcp_error_recovery(&queue->ctrl->ctrl);
447 		return -EINVAL;
448 	}
449 
450 	nvme_end_request(rq, cqe->status, cqe->result);
451 	queue->nr_cqe++;
452 
453 	return 0;
454 }
455 
456 static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
457 		struct nvme_tcp_data_pdu *pdu)
458 {
459 	struct request *rq;
460 
461 	rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
462 	if (!rq) {
463 		dev_err(queue->ctrl->ctrl.device,
464 			"queue %d tag %#x not found\n",
465 			nvme_tcp_queue_id(queue), pdu->command_id);
466 		return -ENOENT;
467 	}
468 
469 	if (!blk_rq_payload_bytes(rq)) {
470 		dev_err(queue->ctrl->ctrl.device,
471 			"queue %d tag %#x unexpected data\n",
472 			nvme_tcp_queue_id(queue), rq->tag);
473 		return -EIO;
474 	}
475 
476 	queue->data_remaining = le32_to_cpu(pdu->data_length);
477 
478 	if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS &&
479 	    unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) {
480 		dev_err(queue->ctrl->ctrl.device,
481 			"queue %d tag %#x SUCCESS set but not last PDU\n",
482 			nvme_tcp_queue_id(queue), rq->tag);
483 		nvme_tcp_error_recovery(&queue->ctrl->ctrl);
484 		return -EPROTO;
485 	}
486 
487 	return 0;
488 }
489 
490 static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue,
491 		struct nvme_tcp_rsp_pdu *pdu)
492 {
493 	struct nvme_completion *cqe = &pdu->cqe;
494 	int ret = 0;
495 
496 	/*
497 	 * AEN requests are special as they don't time out and can
498 	 * survive any kind of queue freeze and often don't respond to
499 	 * aborts.  We don't even bother to allocate a struct request
500 	 * for them but rather special case them here.
501 	 */
502 	if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue),
503 				     cqe->command_id)))
504 		nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
505 				&cqe->result);
506 	else
507 		ret = nvme_tcp_process_nvme_cqe(queue, cqe);
508 
509 	return ret;
510 }
511 
512 static int nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req,
513 		struct nvme_tcp_r2t_pdu *pdu)
514 {
515 	struct nvme_tcp_data_pdu *data = req->pdu;
516 	struct nvme_tcp_queue *queue = req->queue;
517 	struct request *rq = blk_mq_rq_from_pdu(req);
518 	u8 hdgst = nvme_tcp_hdgst_len(queue);
519 	u8 ddgst = nvme_tcp_ddgst_len(queue);
520 
521 	req->pdu_len = le32_to_cpu(pdu->r2t_length);
522 	req->pdu_sent = 0;
523 
524 	if (unlikely(req->data_sent + req->pdu_len > req->data_len)) {
525 		dev_err(queue->ctrl->ctrl.device,
526 			"req %d r2t len %u exceeded data len %u (%zu sent)\n",
527 			rq->tag, req->pdu_len, req->data_len,
528 			req->data_sent);
529 		return -EPROTO;
530 	}
531 
532 	if (unlikely(le32_to_cpu(pdu->r2t_offset) < req->data_sent)) {
533 		dev_err(queue->ctrl->ctrl.device,
534 			"req %d unexpected r2t offset %u (expected %zu)\n",
535 			rq->tag, le32_to_cpu(pdu->r2t_offset),
536 			req->data_sent);
537 		return -EPROTO;
538 	}
539 
540 	memset(data, 0, sizeof(*data));
541 	data->hdr.type = nvme_tcp_h2c_data;
542 	data->hdr.flags = NVME_TCP_F_DATA_LAST;
543 	if (queue->hdr_digest)
544 		data->hdr.flags |= NVME_TCP_F_HDGST;
545 	if (queue->data_digest)
546 		data->hdr.flags |= NVME_TCP_F_DDGST;
547 	data->hdr.hlen = sizeof(*data);
548 	data->hdr.pdo = data->hdr.hlen + hdgst;
549 	data->hdr.plen =
550 		cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst);
551 	data->ttag = pdu->ttag;
552 	data->command_id = rq->tag;
553 	data->data_offset = cpu_to_le32(req->data_sent);
554 	data->data_length = cpu_to_le32(req->pdu_len);
555 	return 0;
556 }
557 
558 static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
559 		struct nvme_tcp_r2t_pdu *pdu)
560 {
561 	struct nvme_tcp_request *req;
562 	struct request *rq;
563 	int ret;
564 
565 	rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
566 	if (!rq) {
567 		dev_err(queue->ctrl->ctrl.device,
568 			"queue %d tag %#x not found\n",
569 			nvme_tcp_queue_id(queue), pdu->command_id);
570 		return -ENOENT;
571 	}
572 	req = blk_mq_rq_to_pdu(rq);
573 
574 	ret = nvme_tcp_setup_h2c_data_pdu(req, pdu);
575 	if (unlikely(ret))
576 		return ret;
577 
578 	req->state = NVME_TCP_SEND_H2C_PDU;
579 	req->offset = 0;
580 
581 	nvme_tcp_queue_request(req);
582 
583 	return 0;
584 }
585 
586 static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
587 		unsigned int *offset, size_t *len)
588 {
589 	struct nvme_tcp_hdr *hdr;
590 	char *pdu = queue->pdu;
591 	size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
592 	int ret;
593 
594 	ret = skb_copy_bits(skb, *offset,
595 		&pdu[queue->pdu_offset], rcv_len);
596 	if (unlikely(ret))
597 		return ret;
598 
599 	queue->pdu_remaining -= rcv_len;
600 	queue->pdu_offset += rcv_len;
601 	*offset += rcv_len;
602 	*len -= rcv_len;
603 	if (queue->pdu_remaining)
604 		return 0;
605 
606 	hdr = queue->pdu;
607 	if (queue->hdr_digest) {
608 		ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen);
609 		if (unlikely(ret))
610 			return ret;
611 	}
612 
613 
614 	if (queue->data_digest) {
615 		ret = nvme_tcp_check_ddgst(queue, queue->pdu);
616 		if (unlikely(ret))
617 			return ret;
618 	}
619 
620 	switch (hdr->type) {
621 	case nvme_tcp_c2h_data:
622 		return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu);
623 	case nvme_tcp_rsp:
624 		nvme_tcp_init_recv_ctx(queue);
625 		return nvme_tcp_handle_comp(queue, (void *)queue->pdu);
626 	case nvme_tcp_r2t:
627 		nvme_tcp_init_recv_ctx(queue);
628 		return nvme_tcp_handle_r2t(queue, (void *)queue->pdu);
629 	default:
630 		dev_err(queue->ctrl->ctrl.device,
631 			"unsupported pdu type (%d)\n", hdr->type);
632 		return -EINVAL;
633 	}
634 }
635 
636 static inline void nvme_tcp_end_request(struct request *rq, u16 status)
637 {
638 	union nvme_result res = {};
639 
640 	nvme_end_request(rq, cpu_to_le16(status << 1), res);
641 }
642 
643 static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb,
644 			      unsigned int *offset, size_t *len)
645 {
646 	struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
647 	struct nvme_tcp_request *req;
648 	struct request *rq;
649 
650 	rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
651 	if (!rq) {
652 		dev_err(queue->ctrl->ctrl.device,
653 			"queue %d tag %#x not found\n",
654 			nvme_tcp_queue_id(queue), pdu->command_id);
655 		return -ENOENT;
656 	}
657 	req = blk_mq_rq_to_pdu(rq);
658 
659 	while (true) {
660 		int recv_len, ret;
661 
662 		recv_len = min_t(size_t, *len, queue->data_remaining);
663 		if (!recv_len)
664 			break;
665 
666 		if (!iov_iter_count(&req->iter)) {
667 			req->curr_bio = req->curr_bio->bi_next;
668 
669 			/*
670 			 * If we don`t have any bios it means that controller
671 			 * sent more data than we requested, hence error
672 			 */
673 			if (!req->curr_bio) {
674 				dev_err(queue->ctrl->ctrl.device,
675 					"queue %d no space in request %#x",
676 					nvme_tcp_queue_id(queue), rq->tag);
677 				nvme_tcp_init_recv_ctx(queue);
678 				return -EIO;
679 			}
680 			nvme_tcp_init_iter(req, READ);
681 		}
682 
683 		/* we can read only from what is left in this bio */
684 		recv_len = min_t(size_t, recv_len,
685 				iov_iter_count(&req->iter));
686 
687 		if (queue->data_digest)
688 			ret = skb_copy_and_hash_datagram_iter(skb, *offset,
689 				&req->iter, recv_len, queue->rcv_hash);
690 		else
691 			ret = skb_copy_datagram_iter(skb, *offset,
692 					&req->iter, recv_len);
693 		if (ret) {
694 			dev_err(queue->ctrl->ctrl.device,
695 				"queue %d failed to copy request %#x data",
696 				nvme_tcp_queue_id(queue), rq->tag);
697 			return ret;
698 		}
699 
700 		*len -= recv_len;
701 		*offset += recv_len;
702 		queue->data_remaining -= recv_len;
703 	}
704 
705 	if (!queue->data_remaining) {
706 		if (queue->data_digest) {
707 			nvme_tcp_ddgst_final(queue->rcv_hash, &queue->exp_ddgst);
708 			queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH;
709 		} else {
710 			if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
711 				nvme_tcp_end_request(rq, NVME_SC_SUCCESS);
712 				queue->nr_cqe++;
713 			}
714 			nvme_tcp_init_recv_ctx(queue);
715 		}
716 	}
717 
718 	return 0;
719 }
720 
721 static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue,
722 		struct sk_buff *skb, unsigned int *offset, size_t *len)
723 {
724 	struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
725 	char *ddgst = (char *)&queue->recv_ddgst;
726 	size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining);
727 	off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining;
728 	int ret;
729 
730 	ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len);
731 	if (unlikely(ret))
732 		return ret;
733 
734 	queue->ddgst_remaining -= recv_len;
735 	*offset += recv_len;
736 	*len -= recv_len;
737 	if (queue->ddgst_remaining)
738 		return 0;
739 
740 	if (queue->recv_ddgst != queue->exp_ddgst) {
741 		dev_err(queue->ctrl->ctrl.device,
742 			"data digest error: recv %#x expected %#x\n",
743 			le32_to_cpu(queue->recv_ddgst),
744 			le32_to_cpu(queue->exp_ddgst));
745 		return -EIO;
746 	}
747 
748 	if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
749 		struct request *rq = blk_mq_tag_to_rq(nvme_tcp_tagset(queue),
750 						pdu->command_id);
751 
752 		nvme_tcp_end_request(rq, NVME_SC_SUCCESS);
753 		queue->nr_cqe++;
754 	}
755 
756 	nvme_tcp_init_recv_ctx(queue);
757 	return 0;
758 }
759 
760 static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,
761 			     unsigned int offset, size_t len)
762 {
763 	struct nvme_tcp_queue *queue = desc->arg.data;
764 	size_t consumed = len;
765 	int result;
766 
767 	while (len) {
768 		switch (nvme_tcp_recv_state(queue)) {
769 		case NVME_TCP_RECV_PDU:
770 			result = nvme_tcp_recv_pdu(queue, skb, &offset, &len);
771 			break;
772 		case NVME_TCP_RECV_DATA:
773 			result = nvme_tcp_recv_data(queue, skb, &offset, &len);
774 			break;
775 		case NVME_TCP_RECV_DDGST:
776 			result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len);
777 			break;
778 		default:
779 			result = -EFAULT;
780 		}
781 		if (result) {
782 			dev_err(queue->ctrl->ctrl.device,
783 				"receive failed:  %d\n", result);
784 			queue->rd_enabled = false;
785 			nvme_tcp_error_recovery(&queue->ctrl->ctrl);
786 			return result;
787 		}
788 	}
789 
790 	return consumed;
791 }
792 
793 static void nvme_tcp_data_ready(struct sock *sk)
794 {
795 	struct nvme_tcp_queue *queue;
796 
797 	read_lock(&sk->sk_callback_lock);
798 	queue = sk->sk_user_data;
799 	if (likely(queue && queue->rd_enabled))
800 		queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
801 	read_unlock(&sk->sk_callback_lock);
802 }
803 
804 static void nvme_tcp_write_space(struct sock *sk)
805 {
806 	struct nvme_tcp_queue *queue;
807 
808 	read_lock_bh(&sk->sk_callback_lock);
809 	queue = sk->sk_user_data;
810 	if (likely(queue && sk_stream_is_writeable(sk))) {
811 		clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
812 		queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
813 	}
814 	read_unlock_bh(&sk->sk_callback_lock);
815 }
816 
817 static void nvme_tcp_state_change(struct sock *sk)
818 {
819 	struct nvme_tcp_queue *queue;
820 
821 	read_lock(&sk->sk_callback_lock);
822 	queue = sk->sk_user_data;
823 	if (!queue)
824 		goto done;
825 
826 	switch (sk->sk_state) {
827 	case TCP_CLOSE:
828 	case TCP_CLOSE_WAIT:
829 	case TCP_LAST_ACK:
830 	case TCP_FIN_WAIT1:
831 	case TCP_FIN_WAIT2:
832 		/* fallthrough */
833 		nvme_tcp_error_recovery(&queue->ctrl->ctrl);
834 		break;
835 	default:
836 		dev_info(queue->ctrl->ctrl.device,
837 			"queue %d socket state %d\n",
838 			nvme_tcp_queue_id(queue), sk->sk_state);
839 	}
840 
841 	queue->state_change(sk);
842 done:
843 	read_unlock(&sk->sk_callback_lock);
844 }
845 
846 static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue)
847 {
848 	queue->request = NULL;
849 }
850 
851 static void nvme_tcp_fail_request(struct nvme_tcp_request *req)
852 {
853 	nvme_tcp_end_request(blk_mq_rq_from_pdu(req), NVME_SC_HOST_PATH_ERROR);
854 }
855 
856 static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
857 {
858 	struct nvme_tcp_queue *queue = req->queue;
859 
860 	while (true) {
861 		struct page *page = nvme_tcp_req_cur_page(req);
862 		size_t offset = nvme_tcp_req_cur_offset(req);
863 		size_t len = nvme_tcp_req_cur_length(req);
864 		bool last = nvme_tcp_pdu_last_send(req, len);
865 		int ret, flags = MSG_DONTWAIT;
866 
867 		if (last && !queue->data_digest)
868 			flags |= MSG_EOR;
869 		else
870 			flags |= MSG_MORE;
871 
872 		/* can't zcopy slab pages */
873 		if (unlikely(PageSlab(page))) {
874 			ret = sock_no_sendpage(queue->sock, page, offset, len,
875 					flags);
876 		} else {
877 			ret = kernel_sendpage(queue->sock, page, offset, len,
878 					flags);
879 		}
880 		if (ret <= 0)
881 			return ret;
882 
883 		nvme_tcp_advance_req(req, ret);
884 		if (queue->data_digest)
885 			nvme_tcp_ddgst_update(queue->snd_hash, page,
886 					offset, ret);
887 
888 		/* fully successful last write*/
889 		if (last && ret == len) {
890 			if (queue->data_digest) {
891 				nvme_tcp_ddgst_final(queue->snd_hash,
892 					&req->ddgst);
893 				req->state = NVME_TCP_SEND_DDGST;
894 				req->offset = 0;
895 			} else {
896 				nvme_tcp_done_send_req(queue);
897 			}
898 			return 1;
899 		}
900 	}
901 	return -EAGAIN;
902 }
903 
904 static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
905 {
906 	struct nvme_tcp_queue *queue = req->queue;
907 	struct nvme_tcp_cmd_pdu *pdu = req->pdu;
908 	bool inline_data = nvme_tcp_has_inline_data(req);
909 	int flags = MSG_DONTWAIT | (inline_data ? MSG_MORE : MSG_EOR);
910 	u8 hdgst = nvme_tcp_hdgst_len(queue);
911 	int len = sizeof(*pdu) + hdgst - req->offset;
912 	int ret;
913 
914 	if (queue->hdr_digest && !req->offset)
915 		nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
916 
917 	ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
918 			offset_in_page(pdu) + req->offset, len,  flags);
919 	if (unlikely(ret <= 0))
920 		return ret;
921 
922 	len -= ret;
923 	if (!len) {
924 		if (inline_data) {
925 			req->state = NVME_TCP_SEND_DATA;
926 			if (queue->data_digest)
927 				crypto_ahash_init(queue->snd_hash);
928 			nvme_tcp_init_iter(req, WRITE);
929 		} else {
930 			nvme_tcp_done_send_req(queue);
931 		}
932 		return 1;
933 	}
934 	req->offset += ret;
935 
936 	return -EAGAIN;
937 }
938 
939 static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
940 {
941 	struct nvme_tcp_queue *queue = req->queue;
942 	struct nvme_tcp_data_pdu *pdu = req->pdu;
943 	u8 hdgst = nvme_tcp_hdgst_len(queue);
944 	int len = sizeof(*pdu) - req->offset + hdgst;
945 	int ret;
946 
947 	if (queue->hdr_digest && !req->offset)
948 		nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
949 
950 	ret = kernel_sendpage(queue->sock, virt_to_page(pdu),
951 			offset_in_page(pdu) + req->offset, len,
952 			MSG_DONTWAIT | MSG_MORE);
953 	if (unlikely(ret <= 0))
954 		return ret;
955 
956 	len -= ret;
957 	if (!len) {
958 		req->state = NVME_TCP_SEND_DATA;
959 		if (queue->data_digest)
960 			crypto_ahash_init(queue->snd_hash);
961 		if (!req->data_sent)
962 			nvme_tcp_init_iter(req, WRITE);
963 		return 1;
964 	}
965 	req->offset += ret;
966 
967 	return -EAGAIN;
968 }
969 
970 static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
971 {
972 	struct nvme_tcp_queue *queue = req->queue;
973 	int ret;
974 	struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_EOR };
975 	struct kvec iov = {
976 		.iov_base = &req->ddgst + req->offset,
977 		.iov_len = NVME_TCP_DIGEST_LENGTH - req->offset
978 	};
979 
980 	ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
981 	if (unlikely(ret <= 0))
982 		return ret;
983 
984 	if (req->offset + ret == NVME_TCP_DIGEST_LENGTH) {
985 		nvme_tcp_done_send_req(queue);
986 		return 1;
987 	}
988 
989 	req->offset += ret;
990 	return -EAGAIN;
991 }
992 
993 static int nvme_tcp_try_send(struct nvme_tcp_queue *queue)
994 {
995 	struct nvme_tcp_request *req;
996 	int ret = 1;
997 
998 	if (!queue->request) {
999 		queue->request = nvme_tcp_fetch_request(queue);
1000 		if (!queue->request)
1001 			return 0;
1002 	}
1003 	req = queue->request;
1004 
1005 	if (req->state == NVME_TCP_SEND_CMD_PDU) {
1006 		ret = nvme_tcp_try_send_cmd_pdu(req);
1007 		if (ret <= 0)
1008 			goto done;
1009 		if (!nvme_tcp_has_inline_data(req))
1010 			return ret;
1011 	}
1012 
1013 	if (req->state == NVME_TCP_SEND_H2C_PDU) {
1014 		ret = nvme_tcp_try_send_data_pdu(req);
1015 		if (ret <= 0)
1016 			goto done;
1017 	}
1018 
1019 	if (req->state == NVME_TCP_SEND_DATA) {
1020 		ret = nvme_tcp_try_send_data(req);
1021 		if (ret <= 0)
1022 			goto done;
1023 	}
1024 
1025 	if (req->state == NVME_TCP_SEND_DDGST)
1026 		ret = nvme_tcp_try_send_ddgst(req);
1027 done:
1028 	if (ret == -EAGAIN) {
1029 		ret = 0;
1030 	} else if (ret < 0) {
1031 		dev_err(queue->ctrl->ctrl.device,
1032 			"failed to send request %d\n", ret);
1033 		if (ret != -EPIPE && ret != -ECONNRESET)
1034 			nvme_tcp_fail_request(queue->request);
1035 		nvme_tcp_done_send_req(queue);
1036 	}
1037 	return ret;
1038 }
1039 
1040 static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue)
1041 {
1042 	struct socket *sock = queue->sock;
1043 	struct sock *sk = sock->sk;
1044 	read_descriptor_t rd_desc;
1045 	int consumed;
1046 
1047 	rd_desc.arg.data = queue;
1048 	rd_desc.count = 1;
1049 	lock_sock(sk);
1050 	queue->nr_cqe = 0;
1051 	consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb);
1052 	release_sock(sk);
1053 	return consumed;
1054 }
1055 
1056 static void nvme_tcp_io_work(struct work_struct *w)
1057 {
1058 	struct nvme_tcp_queue *queue =
1059 		container_of(w, struct nvme_tcp_queue, io_work);
1060 	unsigned long deadline = jiffies + msecs_to_jiffies(1);
1061 
1062 	do {
1063 		bool pending = false;
1064 		int result;
1065 
1066 		result = nvme_tcp_try_send(queue);
1067 		if (result > 0)
1068 			pending = true;
1069 		else if (unlikely(result < 0))
1070 			break;
1071 
1072 		result = nvme_tcp_try_recv(queue);
1073 		if (result > 0)
1074 			pending = true;
1075 		else if (unlikely(result < 0))
1076 			return;
1077 
1078 		if (!pending)
1079 			return;
1080 
1081 	} while (!time_after(jiffies, deadline)); /* quota is exhausted */
1082 
1083 	queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
1084 }
1085 
1086 static void nvme_tcp_free_crypto(struct nvme_tcp_queue *queue)
1087 {
1088 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
1089 
1090 	ahash_request_free(queue->rcv_hash);
1091 	ahash_request_free(queue->snd_hash);
1092 	crypto_free_ahash(tfm);
1093 }
1094 
1095 static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue *queue)
1096 {
1097 	struct crypto_ahash *tfm;
1098 
1099 	tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
1100 	if (IS_ERR(tfm))
1101 		return PTR_ERR(tfm);
1102 
1103 	queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1104 	if (!queue->snd_hash)
1105 		goto free_tfm;
1106 	ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
1107 
1108 	queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
1109 	if (!queue->rcv_hash)
1110 		goto free_snd_hash;
1111 	ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
1112 
1113 	return 0;
1114 free_snd_hash:
1115 	ahash_request_free(queue->snd_hash);
1116 free_tfm:
1117 	crypto_free_ahash(tfm);
1118 	return -ENOMEM;
1119 }
1120 
1121 static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl)
1122 {
1123 	struct nvme_tcp_request *async = &ctrl->async_req;
1124 
1125 	page_frag_free(async->pdu);
1126 }
1127 
1128 static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
1129 {
1130 	struct nvme_tcp_queue *queue = &ctrl->queues[0];
1131 	struct nvme_tcp_request *async = &ctrl->async_req;
1132 	u8 hdgst = nvme_tcp_hdgst_len(queue);
1133 
1134 	async->pdu = page_frag_alloc(&queue->pf_cache,
1135 		sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
1136 		GFP_KERNEL | __GFP_ZERO);
1137 	if (!async->pdu)
1138 		return -ENOMEM;
1139 
1140 	async->queue = &ctrl->queues[0];
1141 	return 0;
1142 }
1143 
1144 static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
1145 {
1146 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1147 	struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1148 
1149 	if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
1150 		return;
1151 
1152 	if (queue->hdr_digest || queue->data_digest)
1153 		nvme_tcp_free_crypto(queue);
1154 
1155 	sock_release(queue->sock);
1156 	kfree(queue->pdu);
1157 }
1158 
1159 static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
1160 {
1161 	struct nvme_tcp_icreq_pdu *icreq;
1162 	struct nvme_tcp_icresp_pdu *icresp;
1163 	struct msghdr msg = {};
1164 	struct kvec iov;
1165 	bool ctrl_hdgst, ctrl_ddgst;
1166 	int ret;
1167 
1168 	icreq = kzalloc(sizeof(*icreq), GFP_KERNEL);
1169 	if (!icreq)
1170 		return -ENOMEM;
1171 
1172 	icresp = kzalloc(sizeof(*icresp), GFP_KERNEL);
1173 	if (!icresp) {
1174 		ret = -ENOMEM;
1175 		goto free_icreq;
1176 	}
1177 
1178 	icreq->hdr.type = nvme_tcp_icreq;
1179 	icreq->hdr.hlen = sizeof(*icreq);
1180 	icreq->hdr.pdo = 0;
1181 	icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen);
1182 	icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
1183 	icreq->maxr2t = 0; /* single inflight r2t supported */
1184 	icreq->hpda = 0; /* no alignment constraint */
1185 	if (queue->hdr_digest)
1186 		icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
1187 	if (queue->data_digest)
1188 		icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
1189 
1190 	iov.iov_base = icreq;
1191 	iov.iov_len = sizeof(*icreq);
1192 	ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1193 	if (ret < 0)
1194 		goto free_icresp;
1195 
1196 	memset(&msg, 0, sizeof(msg));
1197 	iov.iov_base = icresp;
1198 	iov.iov_len = sizeof(*icresp);
1199 	ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
1200 			iov.iov_len, msg.msg_flags);
1201 	if (ret < 0)
1202 		goto free_icresp;
1203 
1204 	ret = -EINVAL;
1205 	if (icresp->hdr.type != nvme_tcp_icresp) {
1206 		pr_err("queue %d: bad type returned %d\n",
1207 			nvme_tcp_queue_id(queue), icresp->hdr.type);
1208 		goto free_icresp;
1209 	}
1210 
1211 	if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) {
1212 		pr_err("queue %d: bad pdu length returned %d\n",
1213 			nvme_tcp_queue_id(queue), icresp->hdr.plen);
1214 		goto free_icresp;
1215 	}
1216 
1217 	if (icresp->pfv != NVME_TCP_PFV_1_0) {
1218 		pr_err("queue %d: bad pfv returned %d\n",
1219 			nvme_tcp_queue_id(queue), icresp->pfv);
1220 		goto free_icresp;
1221 	}
1222 
1223 	ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE);
1224 	if ((queue->data_digest && !ctrl_ddgst) ||
1225 	    (!queue->data_digest && ctrl_ddgst)) {
1226 		pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
1227 			nvme_tcp_queue_id(queue),
1228 			queue->data_digest ? "enabled" : "disabled",
1229 			ctrl_ddgst ? "enabled" : "disabled");
1230 		goto free_icresp;
1231 	}
1232 
1233 	ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE);
1234 	if ((queue->hdr_digest && !ctrl_hdgst) ||
1235 	    (!queue->hdr_digest && ctrl_hdgst)) {
1236 		pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
1237 			nvme_tcp_queue_id(queue),
1238 			queue->hdr_digest ? "enabled" : "disabled",
1239 			ctrl_hdgst ? "enabled" : "disabled");
1240 		goto free_icresp;
1241 	}
1242 
1243 	if (icresp->cpda != 0) {
1244 		pr_err("queue %d: unsupported cpda returned %d\n",
1245 			nvme_tcp_queue_id(queue), icresp->cpda);
1246 		goto free_icresp;
1247 	}
1248 
1249 	ret = 0;
1250 free_icresp:
1251 	kfree(icresp);
1252 free_icreq:
1253 	kfree(icreq);
1254 	return ret;
1255 }
1256 
1257 static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue)
1258 {
1259 	return nvme_tcp_queue_id(queue) == 0;
1260 }
1261 
1262 static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue)
1263 {
1264 	struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1265 	int qid = nvme_tcp_queue_id(queue);
1266 
1267 	return !nvme_tcp_admin_queue(queue) &&
1268 		qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT];
1269 }
1270 
1271 static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue)
1272 {
1273 	struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1274 	int qid = nvme_tcp_queue_id(queue);
1275 
1276 	return !nvme_tcp_admin_queue(queue) &&
1277 		!nvme_tcp_default_queue(queue) &&
1278 		qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1279 			  ctrl->io_queues[HCTX_TYPE_READ];
1280 }
1281 
1282 static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue)
1283 {
1284 	struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1285 	int qid = nvme_tcp_queue_id(queue);
1286 
1287 	return !nvme_tcp_admin_queue(queue) &&
1288 		!nvme_tcp_default_queue(queue) &&
1289 		!nvme_tcp_read_queue(queue) &&
1290 		qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
1291 			  ctrl->io_queues[HCTX_TYPE_READ] +
1292 			  ctrl->io_queues[HCTX_TYPE_POLL];
1293 }
1294 
1295 static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue)
1296 {
1297 	struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1298 	int qid = nvme_tcp_queue_id(queue);
1299 	int n = 0;
1300 
1301 	if (nvme_tcp_default_queue(queue))
1302 		n = qid - 1;
1303 	else if (nvme_tcp_read_queue(queue))
1304 		n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] - 1;
1305 	else if (nvme_tcp_poll_queue(queue))
1306 		n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] -
1307 				ctrl->io_queues[HCTX_TYPE_READ] - 1;
1308 	queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
1309 }
1310 
1311 static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl,
1312 		int qid, size_t queue_size)
1313 {
1314 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1315 	struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1316 	struct linger sol = { .l_onoff = 1, .l_linger = 0 };
1317 	int ret, opt, rcv_pdu_size;
1318 
1319 	queue->ctrl = ctrl;
1320 	INIT_LIST_HEAD(&queue->send_list);
1321 	spin_lock_init(&queue->lock);
1322 	INIT_WORK(&queue->io_work, nvme_tcp_io_work);
1323 	queue->queue_size = queue_size;
1324 
1325 	if (qid > 0)
1326 		queue->cmnd_capsule_len = nctrl->ioccsz * 16;
1327 	else
1328 		queue->cmnd_capsule_len = sizeof(struct nvme_command) +
1329 						NVME_TCP_ADMIN_CCSZ;
1330 
1331 	ret = sock_create(ctrl->addr.ss_family, SOCK_STREAM,
1332 			IPPROTO_TCP, &queue->sock);
1333 	if (ret) {
1334 		dev_err(nctrl->device,
1335 			"failed to create socket: %d\n", ret);
1336 		return ret;
1337 	}
1338 
1339 	/* Single syn retry */
1340 	opt = 1;
1341 	ret = kernel_setsockopt(queue->sock, IPPROTO_TCP, TCP_SYNCNT,
1342 			(char *)&opt, sizeof(opt));
1343 	if (ret) {
1344 		dev_err(nctrl->device,
1345 			"failed to set TCP_SYNCNT sock opt %d\n", ret);
1346 		goto err_sock;
1347 	}
1348 
1349 	/* Set TCP no delay */
1350 	opt = 1;
1351 	ret = kernel_setsockopt(queue->sock, IPPROTO_TCP,
1352 			TCP_NODELAY, (char *)&opt, sizeof(opt));
1353 	if (ret) {
1354 		dev_err(nctrl->device,
1355 			"failed to set TCP_NODELAY sock opt %d\n", ret);
1356 		goto err_sock;
1357 	}
1358 
1359 	/*
1360 	 * Cleanup whatever is sitting in the TCP transmit queue on socket
1361 	 * close. This is done to prevent stale data from being sent should
1362 	 * the network connection be restored before TCP times out.
1363 	 */
1364 	ret = kernel_setsockopt(queue->sock, SOL_SOCKET, SO_LINGER,
1365 			(char *)&sol, sizeof(sol));
1366 	if (ret) {
1367 		dev_err(nctrl->device,
1368 			"failed to set SO_LINGER sock opt %d\n", ret);
1369 		goto err_sock;
1370 	}
1371 
1372 	if (so_priority > 0) {
1373 		ret = kernel_setsockopt(queue->sock, SOL_SOCKET, SO_PRIORITY,
1374 				(char *)&so_priority, sizeof(so_priority));
1375 		if (ret) {
1376 			dev_err(ctrl->ctrl.device,
1377 				"failed to set SO_PRIORITY sock opt, ret %d\n",
1378 				ret);
1379 			goto err_sock;
1380 		}
1381 	}
1382 
1383 	/* Set socket type of service */
1384 	if (nctrl->opts->tos >= 0) {
1385 		opt = nctrl->opts->tos;
1386 		ret = kernel_setsockopt(queue->sock, SOL_IP, IP_TOS,
1387 				(char *)&opt, sizeof(opt));
1388 		if (ret) {
1389 			dev_err(nctrl->device,
1390 				"failed to set IP_TOS sock opt %d\n", ret);
1391 			goto err_sock;
1392 		}
1393 	}
1394 
1395 	queue->sock->sk->sk_allocation = GFP_ATOMIC;
1396 	nvme_tcp_set_queue_io_cpu(queue);
1397 	queue->request = NULL;
1398 	queue->data_remaining = 0;
1399 	queue->ddgst_remaining = 0;
1400 	queue->pdu_remaining = 0;
1401 	queue->pdu_offset = 0;
1402 	sk_set_memalloc(queue->sock->sk);
1403 
1404 	if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) {
1405 		ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr,
1406 			sizeof(ctrl->src_addr));
1407 		if (ret) {
1408 			dev_err(nctrl->device,
1409 				"failed to bind queue %d socket %d\n",
1410 				qid, ret);
1411 			goto err_sock;
1412 		}
1413 	}
1414 
1415 	queue->hdr_digest = nctrl->opts->hdr_digest;
1416 	queue->data_digest = nctrl->opts->data_digest;
1417 	if (queue->hdr_digest || queue->data_digest) {
1418 		ret = nvme_tcp_alloc_crypto(queue);
1419 		if (ret) {
1420 			dev_err(nctrl->device,
1421 				"failed to allocate queue %d crypto\n", qid);
1422 			goto err_sock;
1423 		}
1424 	}
1425 
1426 	rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) +
1427 			nvme_tcp_hdgst_len(queue);
1428 	queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL);
1429 	if (!queue->pdu) {
1430 		ret = -ENOMEM;
1431 		goto err_crypto;
1432 	}
1433 
1434 	dev_dbg(nctrl->device, "connecting queue %d\n",
1435 			nvme_tcp_queue_id(queue));
1436 
1437 	ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr,
1438 		sizeof(ctrl->addr), 0);
1439 	if (ret) {
1440 		dev_err(nctrl->device,
1441 			"failed to connect socket: %d\n", ret);
1442 		goto err_rcv_pdu;
1443 	}
1444 
1445 	ret = nvme_tcp_init_connection(queue);
1446 	if (ret)
1447 		goto err_init_connect;
1448 
1449 	queue->rd_enabled = true;
1450 	set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags);
1451 	nvme_tcp_init_recv_ctx(queue);
1452 
1453 	write_lock_bh(&queue->sock->sk->sk_callback_lock);
1454 	queue->sock->sk->sk_user_data = queue;
1455 	queue->state_change = queue->sock->sk->sk_state_change;
1456 	queue->data_ready = queue->sock->sk->sk_data_ready;
1457 	queue->write_space = queue->sock->sk->sk_write_space;
1458 	queue->sock->sk->sk_data_ready = nvme_tcp_data_ready;
1459 	queue->sock->sk->sk_state_change = nvme_tcp_state_change;
1460 	queue->sock->sk->sk_write_space = nvme_tcp_write_space;
1461 #ifdef CONFIG_NET_RX_BUSY_POLL
1462 	queue->sock->sk->sk_ll_usec = 1;
1463 #endif
1464 	write_unlock_bh(&queue->sock->sk->sk_callback_lock);
1465 
1466 	return 0;
1467 
1468 err_init_connect:
1469 	kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1470 err_rcv_pdu:
1471 	kfree(queue->pdu);
1472 err_crypto:
1473 	if (queue->hdr_digest || queue->data_digest)
1474 		nvme_tcp_free_crypto(queue);
1475 err_sock:
1476 	sock_release(queue->sock);
1477 	queue->sock = NULL;
1478 	return ret;
1479 }
1480 
1481 static void nvme_tcp_restore_sock_calls(struct nvme_tcp_queue *queue)
1482 {
1483 	struct socket *sock = queue->sock;
1484 
1485 	write_lock_bh(&sock->sk->sk_callback_lock);
1486 	sock->sk->sk_user_data  = NULL;
1487 	sock->sk->sk_data_ready = queue->data_ready;
1488 	sock->sk->sk_state_change = queue->state_change;
1489 	sock->sk->sk_write_space  = queue->write_space;
1490 	write_unlock_bh(&sock->sk->sk_callback_lock);
1491 }
1492 
1493 static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
1494 {
1495 	kernel_sock_shutdown(queue->sock, SHUT_RDWR);
1496 	nvme_tcp_restore_sock_calls(queue);
1497 	cancel_work_sync(&queue->io_work);
1498 }
1499 
1500 static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
1501 {
1502 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1503 	struct nvme_tcp_queue *queue = &ctrl->queues[qid];
1504 
1505 	if (!test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
1506 		return;
1507 
1508 	__nvme_tcp_stop_queue(queue);
1509 }
1510 
1511 static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
1512 {
1513 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1514 	int ret;
1515 
1516 	if (idx)
1517 		ret = nvmf_connect_io_queue(nctrl, idx, false);
1518 	else
1519 		ret = nvmf_connect_admin_queue(nctrl);
1520 
1521 	if (!ret) {
1522 		set_bit(NVME_TCP_Q_LIVE, &ctrl->queues[idx].flags);
1523 	} else {
1524 		if (test_bit(NVME_TCP_Q_ALLOCATED, &ctrl->queues[idx].flags))
1525 			__nvme_tcp_stop_queue(&ctrl->queues[idx]);
1526 		dev_err(nctrl->device,
1527 			"failed to connect queue: %d ret=%d\n", idx, ret);
1528 	}
1529 	return ret;
1530 }
1531 
1532 static struct blk_mq_tag_set *nvme_tcp_alloc_tagset(struct nvme_ctrl *nctrl,
1533 		bool admin)
1534 {
1535 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1536 	struct blk_mq_tag_set *set;
1537 	int ret;
1538 
1539 	if (admin) {
1540 		set = &ctrl->admin_tag_set;
1541 		memset(set, 0, sizeof(*set));
1542 		set->ops = &nvme_tcp_admin_mq_ops;
1543 		set->queue_depth = NVME_AQ_MQ_TAG_DEPTH;
1544 		set->reserved_tags = 2; /* connect + keep-alive */
1545 		set->numa_node = NUMA_NO_NODE;
1546 		set->cmd_size = sizeof(struct nvme_tcp_request);
1547 		set->driver_data = ctrl;
1548 		set->nr_hw_queues = 1;
1549 		set->timeout = ADMIN_TIMEOUT;
1550 	} else {
1551 		set = &ctrl->tag_set;
1552 		memset(set, 0, sizeof(*set));
1553 		set->ops = &nvme_tcp_mq_ops;
1554 		set->queue_depth = nctrl->sqsize + 1;
1555 		set->reserved_tags = 1; /* fabric connect */
1556 		set->numa_node = NUMA_NO_NODE;
1557 		set->flags = BLK_MQ_F_SHOULD_MERGE;
1558 		set->cmd_size = sizeof(struct nvme_tcp_request);
1559 		set->driver_data = ctrl;
1560 		set->nr_hw_queues = nctrl->queue_count - 1;
1561 		set->timeout = NVME_IO_TIMEOUT;
1562 		set->nr_maps = nctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2;
1563 	}
1564 
1565 	ret = blk_mq_alloc_tag_set(set);
1566 	if (ret)
1567 		return ERR_PTR(ret);
1568 
1569 	return set;
1570 }
1571 
1572 static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl)
1573 {
1574 	if (to_tcp_ctrl(ctrl)->async_req.pdu) {
1575 		nvme_tcp_free_async_req(to_tcp_ctrl(ctrl));
1576 		to_tcp_ctrl(ctrl)->async_req.pdu = NULL;
1577 	}
1578 
1579 	nvme_tcp_free_queue(ctrl, 0);
1580 }
1581 
1582 static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl)
1583 {
1584 	int i;
1585 
1586 	for (i = 1; i < ctrl->queue_count; i++)
1587 		nvme_tcp_free_queue(ctrl, i);
1588 }
1589 
1590 static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl)
1591 {
1592 	int i;
1593 
1594 	for (i = 1; i < ctrl->queue_count; i++)
1595 		nvme_tcp_stop_queue(ctrl, i);
1596 }
1597 
1598 static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl)
1599 {
1600 	int i, ret = 0;
1601 
1602 	for (i = 1; i < ctrl->queue_count; i++) {
1603 		ret = nvme_tcp_start_queue(ctrl, i);
1604 		if (ret)
1605 			goto out_stop_queues;
1606 	}
1607 
1608 	return 0;
1609 
1610 out_stop_queues:
1611 	for (i--; i >= 1; i--)
1612 		nvme_tcp_stop_queue(ctrl, i);
1613 	return ret;
1614 }
1615 
1616 static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
1617 {
1618 	int ret;
1619 
1620 	ret = nvme_tcp_alloc_queue(ctrl, 0, NVME_AQ_DEPTH);
1621 	if (ret)
1622 		return ret;
1623 
1624 	ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
1625 	if (ret)
1626 		goto out_free_queue;
1627 
1628 	return 0;
1629 
1630 out_free_queue:
1631 	nvme_tcp_free_queue(ctrl, 0);
1632 	return ret;
1633 }
1634 
1635 static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
1636 {
1637 	int i, ret;
1638 
1639 	for (i = 1; i < ctrl->queue_count; i++) {
1640 		ret = nvme_tcp_alloc_queue(ctrl, i,
1641 				ctrl->sqsize + 1);
1642 		if (ret)
1643 			goto out_free_queues;
1644 	}
1645 
1646 	return 0;
1647 
1648 out_free_queues:
1649 	for (i--; i >= 1; i--)
1650 		nvme_tcp_free_queue(ctrl, i);
1651 
1652 	return ret;
1653 }
1654 
1655 static unsigned int nvme_tcp_nr_io_queues(struct nvme_ctrl *ctrl)
1656 {
1657 	unsigned int nr_io_queues;
1658 
1659 	nr_io_queues = min(ctrl->opts->nr_io_queues, num_online_cpus());
1660 	nr_io_queues += min(ctrl->opts->nr_write_queues, num_online_cpus());
1661 	nr_io_queues += min(ctrl->opts->nr_poll_queues, num_online_cpus());
1662 
1663 	return nr_io_queues;
1664 }
1665 
1666 static void nvme_tcp_set_io_queues(struct nvme_ctrl *nctrl,
1667 		unsigned int nr_io_queues)
1668 {
1669 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1670 	struct nvmf_ctrl_options *opts = nctrl->opts;
1671 
1672 	if (opts->nr_write_queues && opts->nr_io_queues < nr_io_queues) {
1673 		/*
1674 		 * separate read/write queues
1675 		 * hand out dedicated default queues only after we have
1676 		 * sufficient read queues.
1677 		 */
1678 		ctrl->io_queues[HCTX_TYPE_READ] = opts->nr_io_queues;
1679 		nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
1680 		ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1681 			min(opts->nr_write_queues, nr_io_queues);
1682 		nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1683 	} else {
1684 		/*
1685 		 * shared read/write queues
1686 		 * either no write queues were requested, or we don't have
1687 		 * sufficient queue count to have dedicated default queues.
1688 		 */
1689 		ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1690 			min(opts->nr_io_queues, nr_io_queues);
1691 		nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1692 	}
1693 
1694 	if (opts->nr_poll_queues && nr_io_queues) {
1695 		/* map dedicated poll queues only if we have queues left */
1696 		ctrl->io_queues[HCTX_TYPE_POLL] =
1697 			min(opts->nr_poll_queues, nr_io_queues);
1698 	}
1699 }
1700 
1701 static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
1702 {
1703 	unsigned int nr_io_queues;
1704 	int ret;
1705 
1706 	nr_io_queues = nvme_tcp_nr_io_queues(ctrl);
1707 	ret = nvme_set_queue_count(ctrl, &nr_io_queues);
1708 	if (ret)
1709 		return ret;
1710 
1711 	ctrl->queue_count = nr_io_queues + 1;
1712 	if (ctrl->queue_count < 2)
1713 		return 0;
1714 
1715 	dev_info(ctrl->device,
1716 		"creating %d I/O queues.\n", nr_io_queues);
1717 
1718 	nvme_tcp_set_io_queues(ctrl, nr_io_queues);
1719 
1720 	return __nvme_tcp_alloc_io_queues(ctrl);
1721 }
1722 
1723 static void nvme_tcp_destroy_io_queues(struct nvme_ctrl *ctrl, bool remove)
1724 {
1725 	nvme_tcp_stop_io_queues(ctrl);
1726 	if (remove) {
1727 		blk_cleanup_queue(ctrl->connect_q);
1728 		blk_mq_free_tag_set(ctrl->tagset);
1729 	}
1730 	nvme_tcp_free_io_queues(ctrl);
1731 }
1732 
1733 static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
1734 {
1735 	int ret;
1736 
1737 	ret = nvme_tcp_alloc_io_queues(ctrl);
1738 	if (ret)
1739 		return ret;
1740 
1741 	if (new) {
1742 		ctrl->tagset = nvme_tcp_alloc_tagset(ctrl, false);
1743 		if (IS_ERR(ctrl->tagset)) {
1744 			ret = PTR_ERR(ctrl->tagset);
1745 			goto out_free_io_queues;
1746 		}
1747 
1748 		ctrl->connect_q = blk_mq_init_queue(ctrl->tagset);
1749 		if (IS_ERR(ctrl->connect_q)) {
1750 			ret = PTR_ERR(ctrl->connect_q);
1751 			goto out_free_tag_set;
1752 		}
1753 	} else {
1754 		blk_mq_update_nr_hw_queues(ctrl->tagset,
1755 			ctrl->queue_count - 1);
1756 	}
1757 
1758 	ret = nvme_tcp_start_io_queues(ctrl);
1759 	if (ret)
1760 		goto out_cleanup_connect_q;
1761 
1762 	return 0;
1763 
1764 out_cleanup_connect_q:
1765 	if (new)
1766 		blk_cleanup_queue(ctrl->connect_q);
1767 out_free_tag_set:
1768 	if (new)
1769 		blk_mq_free_tag_set(ctrl->tagset);
1770 out_free_io_queues:
1771 	nvme_tcp_free_io_queues(ctrl);
1772 	return ret;
1773 }
1774 
1775 static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl *ctrl, bool remove)
1776 {
1777 	nvme_tcp_stop_queue(ctrl, 0);
1778 	if (remove) {
1779 		blk_cleanup_queue(ctrl->admin_q);
1780 		blk_cleanup_queue(ctrl->fabrics_q);
1781 		blk_mq_free_tag_set(ctrl->admin_tagset);
1782 	}
1783 	nvme_tcp_free_admin_queue(ctrl);
1784 }
1785 
1786 static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
1787 {
1788 	int error;
1789 
1790 	error = nvme_tcp_alloc_admin_queue(ctrl);
1791 	if (error)
1792 		return error;
1793 
1794 	if (new) {
1795 		ctrl->admin_tagset = nvme_tcp_alloc_tagset(ctrl, true);
1796 		if (IS_ERR(ctrl->admin_tagset)) {
1797 			error = PTR_ERR(ctrl->admin_tagset);
1798 			goto out_free_queue;
1799 		}
1800 
1801 		ctrl->fabrics_q = blk_mq_init_queue(ctrl->admin_tagset);
1802 		if (IS_ERR(ctrl->fabrics_q)) {
1803 			error = PTR_ERR(ctrl->fabrics_q);
1804 			goto out_free_tagset;
1805 		}
1806 
1807 		ctrl->admin_q = blk_mq_init_queue(ctrl->admin_tagset);
1808 		if (IS_ERR(ctrl->admin_q)) {
1809 			error = PTR_ERR(ctrl->admin_q);
1810 			goto out_cleanup_fabrics_q;
1811 		}
1812 	}
1813 
1814 	error = nvme_tcp_start_queue(ctrl, 0);
1815 	if (error)
1816 		goto out_cleanup_queue;
1817 
1818 	error = nvme_enable_ctrl(ctrl);
1819 	if (error)
1820 		goto out_stop_queue;
1821 
1822 	blk_mq_unquiesce_queue(ctrl->admin_q);
1823 
1824 	error = nvme_init_identify(ctrl);
1825 	if (error)
1826 		goto out_stop_queue;
1827 
1828 	return 0;
1829 
1830 out_stop_queue:
1831 	nvme_tcp_stop_queue(ctrl, 0);
1832 out_cleanup_queue:
1833 	if (new)
1834 		blk_cleanup_queue(ctrl->admin_q);
1835 out_cleanup_fabrics_q:
1836 	if (new)
1837 		blk_cleanup_queue(ctrl->fabrics_q);
1838 out_free_tagset:
1839 	if (new)
1840 		blk_mq_free_tag_set(ctrl->admin_tagset);
1841 out_free_queue:
1842 	nvme_tcp_free_admin_queue(ctrl);
1843 	return error;
1844 }
1845 
1846 static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
1847 		bool remove)
1848 {
1849 	blk_mq_quiesce_queue(ctrl->admin_q);
1850 	nvme_tcp_stop_queue(ctrl, 0);
1851 	if (ctrl->admin_tagset) {
1852 		blk_mq_tagset_busy_iter(ctrl->admin_tagset,
1853 			nvme_cancel_request, ctrl);
1854 		blk_mq_tagset_wait_completed_request(ctrl->admin_tagset);
1855 	}
1856 	if (remove)
1857 		blk_mq_unquiesce_queue(ctrl->admin_q);
1858 	nvme_tcp_destroy_admin_queue(ctrl, remove);
1859 }
1860 
1861 static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
1862 		bool remove)
1863 {
1864 	if (ctrl->queue_count <= 1)
1865 		return;
1866 	nvme_stop_queues(ctrl);
1867 	nvme_tcp_stop_io_queues(ctrl);
1868 	if (ctrl->tagset) {
1869 		blk_mq_tagset_busy_iter(ctrl->tagset,
1870 			nvme_cancel_request, ctrl);
1871 		blk_mq_tagset_wait_completed_request(ctrl->tagset);
1872 	}
1873 	if (remove)
1874 		nvme_start_queues(ctrl);
1875 	nvme_tcp_destroy_io_queues(ctrl, remove);
1876 }
1877 
1878 static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl)
1879 {
1880 	/* If we are resetting/deleting then do nothing */
1881 	if (ctrl->state != NVME_CTRL_CONNECTING) {
1882 		WARN_ON_ONCE(ctrl->state == NVME_CTRL_NEW ||
1883 			ctrl->state == NVME_CTRL_LIVE);
1884 		return;
1885 	}
1886 
1887 	if (nvmf_should_reconnect(ctrl)) {
1888 		dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
1889 			ctrl->opts->reconnect_delay);
1890 		queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
1891 				ctrl->opts->reconnect_delay * HZ);
1892 	} else {
1893 		dev_info(ctrl->device, "Removing controller...\n");
1894 		nvme_delete_ctrl(ctrl);
1895 	}
1896 }
1897 
1898 static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
1899 {
1900 	struct nvmf_ctrl_options *opts = ctrl->opts;
1901 	int ret;
1902 
1903 	ret = nvme_tcp_configure_admin_queue(ctrl, new);
1904 	if (ret)
1905 		return ret;
1906 
1907 	if (ctrl->icdoff) {
1908 		dev_err(ctrl->device, "icdoff is not supported!\n");
1909 		goto destroy_admin;
1910 	}
1911 
1912 	if (opts->queue_size > ctrl->sqsize + 1)
1913 		dev_warn(ctrl->device,
1914 			"queue_size %zu > ctrl sqsize %u, clamping down\n",
1915 			opts->queue_size, ctrl->sqsize + 1);
1916 
1917 	if (ctrl->sqsize + 1 > ctrl->maxcmd) {
1918 		dev_warn(ctrl->device,
1919 			"sqsize %u > ctrl maxcmd %u, clamping down\n",
1920 			ctrl->sqsize + 1, ctrl->maxcmd);
1921 		ctrl->sqsize = ctrl->maxcmd - 1;
1922 	}
1923 
1924 	if (ctrl->queue_count > 1) {
1925 		ret = nvme_tcp_configure_io_queues(ctrl, new);
1926 		if (ret)
1927 			goto destroy_admin;
1928 	}
1929 
1930 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
1931 		/*
1932 		 * state change failure is ok if we're in DELETING state,
1933 		 * unless we're during creation of a new controller to
1934 		 * avoid races with teardown flow.
1935 		 */
1936 		WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
1937 		WARN_ON_ONCE(new);
1938 		ret = -EINVAL;
1939 		goto destroy_io;
1940 	}
1941 
1942 	nvme_start_ctrl(ctrl);
1943 	return 0;
1944 
1945 destroy_io:
1946 	if (ctrl->queue_count > 1)
1947 		nvme_tcp_destroy_io_queues(ctrl, new);
1948 destroy_admin:
1949 	nvme_tcp_stop_queue(ctrl, 0);
1950 	nvme_tcp_destroy_admin_queue(ctrl, new);
1951 	return ret;
1952 }
1953 
1954 static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
1955 {
1956 	struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
1957 			struct nvme_tcp_ctrl, connect_work);
1958 	struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
1959 
1960 	++ctrl->nr_reconnects;
1961 
1962 	if (nvme_tcp_setup_ctrl(ctrl, false))
1963 		goto requeue;
1964 
1965 	dev_info(ctrl->device, "Successfully reconnected (%d attempt)\n",
1966 			ctrl->nr_reconnects);
1967 
1968 	ctrl->nr_reconnects = 0;
1969 
1970 	return;
1971 
1972 requeue:
1973 	dev_info(ctrl->device, "Failed reconnect attempt %d\n",
1974 			ctrl->nr_reconnects);
1975 	nvme_tcp_reconnect_or_remove(ctrl);
1976 }
1977 
1978 static void nvme_tcp_error_recovery_work(struct work_struct *work)
1979 {
1980 	struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
1981 				struct nvme_tcp_ctrl, err_work);
1982 	struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
1983 
1984 	nvme_stop_keep_alive(ctrl);
1985 	nvme_tcp_teardown_io_queues(ctrl, false);
1986 	/* unquiesce to fail fast pending requests */
1987 	nvme_start_queues(ctrl);
1988 	nvme_tcp_teardown_admin_queue(ctrl, false);
1989 	blk_mq_unquiesce_queue(ctrl->admin_q);
1990 
1991 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
1992 		/* state change failure is ok if we're in DELETING state */
1993 		WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
1994 		return;
1995 	}
1996 
1997 	nvme_tcp_reconnect_or_remove(ctrl);
1998 }
1999 
2000 static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
2001 {
2002 	cancel_work_sync(&to_tcp_ctrl(ctrl)->err_work);
2003 	cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
2004 
2005 	nvme_tcp_teardown_io_queues(ctrl, shutdown);
2006 	blk_mq_quiesce_queue(ctrl->admin_q);
2007 	if (shutdown)
2008 		nvme_shutdown_ctrl(ctrl);
2009 	else
2010 		nvme_disable_ctrl(ctrl);
2011 	nvme_tcp_teardown_admin_queue(ctrl, shutdown);
2012 }
2013 
2014 static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
2015 {
2016 	nvme_tcp_teardown_ctrl(ctrl, true);
2017 }
2018 
2019 static void nvme_reset_ctrl_work(struct work_struct *work)
2020 {
2021 	struct nvme_ctrl *ctrl =
2022 		container_of(work, struct nvme_ctrl, reset_work);
2023 
2024 	nvme_stop_ctrl(ctrl);
2025 	nvme_tcp_teardown_ctrl(ctrl, false);
2026 
2027 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2028 		/* state change failure is ok if we're in DELETING state */
2029 		WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING);
2030 		return;
2031 	}
2032 
2033 	if (nvme_tcp_setup_ctrl(ctrl, false))
2034 		goto out_fail;
2035 
2036 	return;
2037 
2038 out_fail:
2039 	++ctrl->nr_reconnects;
2040 	nvme_tcp_reconnect_or_remove(ctrl);
2041 }
2042 
2043 static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
2044 {
2045 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
2046 
2047 	if (list_empty(&ctrl->list))
2048 		goto free_ctrl;
2049 
2050 	mutex_lock(&nvme_tcp_ctrl_mutex);
2051 	list_del(&ctrl->list);
2052 	mutex_unlock(&nvme_tcp_ctrl_mutex);
2053 
2054 	nvmf_free_options(nctrl->opts);
2055 free_ctrl:
2056 	kfree(ctrl->queues);
2057 	kfree(ctrl);
2058 }
2059 
2060 static void nvme_tcp_set_sg_null(struct nvme_command *c)
2061 {
2062 	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2063 
2064 	sg->addr = 0;
2065 	sg->length = 0;
2066 	sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2067 			NVME_SGL_FMT_TRANSPORT_A;
2068 }
2069 
2070 static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
2071 		struct nvme_command *c, u32 data_len)
2072 {
2073 	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2074 
2075 	sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
2076 	sg->length = cpu_to_le32(data_len);
2077 	sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
2078 }
2079 
2080 static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
2081 		u32 data_len)
2082 {
2083 	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2084 
2085 	sg->addr = 0;
2086 	sg->length = cpu_to_le32(data_len);
2087 	sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2088 			NVME_SGL_FMT_TRANSPORT_A;
2089 }
2090 
2091 static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
2092 {
2093 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
2094 	struct nvme_tcp_queue *queue = &ctrl->queues[0];
2095 	struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
2096 	struct nvme_command *cmd = &pdu->cmd;
2097 	u8 hdgst = nvme_tcp_hdgst_len(queue);
2098 
2099 	memset(pdu, 0, sizeof(*pdu));
2100 	pdu->hdr.type = nvme_tcp_cmd;
2101 	if (queue->hdr_digest)
2102 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
2103 	pdu->hdr.hlen = sizeof(*pdu);
2104 	pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
2105 
2106 	cmd->common.opcode = nvme_admin_async_event;
2107 	cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
2108 	cmd->common.flags |= NVME_CMD_SGL_METABUF;
2109 	nvme_tcp_set_sg_null(cmd);
2110 
2111 	ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
2112 	ctrl->async_req.offset = 0;
2113 	ctrl->async_req.curr_bio = NULL;
2114 	ctrl->async_req.data_len = 0;
2115 
2116 	nvme_tcp_queue_request(&ctrl->async_req);
2117 }
2118 
2119 static enum blk_eh_timer_return
2120 nvme_tcp_timeout(struct request *rq, bool reserved)
2121 {
2122 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2123 	struct nvme_tcp_ctrl *ctrl = req->queue->ctrl;
2124 	struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2125 
2126 	/*
2127 	 * Restart the timer if a controller reset is already scheduled. Any
2128 	 * timed out commands would be handled before entering the connecting
2129 	 * state.
2130 	 */
2131 	if (ctrl->ctrl.state == NVME_CTRL_RESETTING)
2132 		return BLK_EH_RESET_TIMER;
2133 
2134 	dev_warn(ctrl->ctrl.device,
2135 		"queue %d: timeout request %#x type %d\n",
2136 		nvme_tcp_queue_id(req->queue), rq->tag, pdu->hdr.type);
2137 
2138 	if (ctrl->ctrl.state != NVME_CTRL_LIVE) {
2139 		/*
2140 		 * Teardown immediately if controller times out while starting
2141 		 * or we are already started error recovery. all outstanding
2142 		 * requests are completed on shutdown, so we return BLK_EH_DONE.
2143 		 */
2144 		flush_work(&ctrl->err_work);
2145 		nvme_tcp_teardown_io_queues(&ctrl->ctrl, false);
2146 		nvme_tcp_teardown_admin_queue(&ctrl->ctrl, false);
2147 		return BLK_EH_DONE;
2148 	}
2149 
2150 	dev_warn(ctrl->ctrl.device, "starting error recovery\n");
2151 	nvme_tcp_error_recovery(&ctrl->ctrl);
2152 
2153 	return BLK_EH_RESET_TIMER;
2154 }
2155 
2156 static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
2157 			struct request *rq)
2158 {
2159 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2160 	struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2161 	struct nvme_command *c = &pdu->cmd;
2162 
2163 	c->common.flags |= NVME_CMD_SGL_METABUF;
2164 
2165 	if (!blk_rq_nr_phys_segments(rq))
2166 		nvme_tcp_set_sg_null(c);
2167 	else if (rq_data_dir(rq) == WRITE &&
2168 	    req->data_len <= nvme_tcp_inline_data_size(queue))
2169 		nvme_tcp_set_sg_inline(queue, c, req->data_len);
2170 	else
2171 		nvme_tcp_set_sg_host_data(c, req->data_len);
2172 
2173 	return 0;
2174 }
2175 
2176 static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
2177 		struct request *rq)
2178 {
2179 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2180 	struct nvme_tcp_cmd_pdu *pdu = req->pdu;
2181 	struct nvme_tcp_queue *queue = req->queue;
2182 	u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
2183 	blk_status_t ret;
2184 
2185 	ret = nvme_setup_cmd(ns, rq, &pdu->cmd);
2186 	if (ret)
2187 		return ret;
2188 
2189 	req->state = NVME_TCP_SEND_CMD_PDU;
2190 	req->offset = 0;
2191 	req->data_sent = 0;
2192 	req->pdu_len = 0;
2193 	req->pdu_sent = 0;
2194 	req->data_len = blk_rq_nr_phys_segments(rq) ?
2195 				blk_rq_payload_bytes(rq) : 0;
2196 	req->curr_bio = rq->bio;
2197 
2198 	if (rq_data_dir(rq) == WRITE &&
2199 	    req->data_len <= nvme_tcp_inline_data_size(queue))
2200 		req->pdu_len = req->data_len;
2201 	else if (req->curr_bio)
2202 		nvme_tcp_init_iter(req, READ);
2203 
2204 	pdu->hdr.type = nvme_tcp_cmd;
2205 	pdu->hdr.flags = 0;
2206 	if (queue->hdr_digest)
2207 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
2208 	if (queue->data_digest && req->pdu_len) {
2209 		pdu->hdr.flags |= NVME_TCP_F_DDGST;
2210 		ddgst = nvme_tcp_ddgst_len(queue);
2211 	}
2212 	pdu->hdr.hlen = sizeof(*pdu);
2213 	pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
2214 	pdu->hdr.plen =
2215 		cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
2216 
2217 	ret = nvme_tcp_map_data(queue, rq);
2218 	if (unlikely(ret)) {
2219 		nvme_cleanup_cmd(rq);
2220 		dev_err(queue->ctrl->ctrl.device,
2221 			"Failed to map data (%d)\n", ret);
2222 		return ret;
2223 	}
2224 
2225 	return 0;
2226 }
2227 
2228 static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
2229 		const struct blk_mq_queue_data *bd)
2230 {
2231 	struct nvme_ns *ns = hctx->queue->queuedata;
2232 	struct nvme_tcp_queue *queue = hctx->driver_data;
2233 	struct request *rq = bd->rq;
2234 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2235 	bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
2236 	blk_status_t ret;
2237 
2238 	if (!nvmf_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2239 		return nvmf_fail_nonready_command(&queue->ctrl->ctrl, rq);
2240 
2241 	ret = nvme_tcp_setup_cmd_pdu(ns, rq);
2242 	if (unlikely(ret))
2243 		return ret;
2244 
2245 	blk_mq_start_request(rq);
2246 
2247 	nvme_tcp_queue_request(req);
2248 
2249 	return BLK_STS_OK;
2250 }
2251 
2252 static int nvme_tcp_map_queues(struct blk_mq_tag_set *set)
2253 {
2254 	struct nvme_tcp_ctrl *ctrl = set->driver_data;
2255 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2256 
2257 	if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
2258 		/* separate read/write queues */
2259 		set->map[HCTX_TYPE_DEFAULT].nr_queues =
2260 			ctrl->io_queues[HCTX_TYPE_DEFAULT];
2261 		set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2262 		set->map[HCTX_TYPE_READ].nr_queues =
2263 			ctrl->io_queues[HCTX_TYPE_READ];
2264 		set->map[HCTX_TYPE_READ].queue_offset =
2265 			ctrl->io_queues[HCTX_TYPE_DEFAULT];
2266 	} else {
2267 		/* shared read/write queues */
2268 		set->map[HCTX_TYPE_DEFAULT].nr_queues =
2269 			ctrl->io_queues[HCTX_TYPE_DEFAULT];
2270 		set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2271 		set->map[HCTX_TYPE_READ].nr_queues =
2272 			ctrl->io_queues[HCTX_TYPE_DEFAULT];
2273 		set->map[HCTX_TYPE_READ].queue_offset = 0;
2274 	}
2275 	blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
2276 	blk_mq_map_queues(&set->map[HCTX_TYPE_READ]);
2277 
2278 	if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
2279 		/* map dedicated poll queues only if we have queues left */
2280 		set->map[HCTX_TYPE_POLL].nr_queues =
2281 				ctrl->io_queues[HCTX_TYPE_POLL];
2282 		set->map[HCTX_TYPE_POLL].queue_offset =
2283 			ctrl->io_queues[HCTX_TYPE_DEFAULT] +
2284 			ctrl->io_queues[HCTX_TYPE_READ];
2285 		blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
2286 	}
2287 
2288 	dev_info(ctrl->ctrl.device,
2289 		"mapped %d/%d/%d default/read/poll queues.\n",
2290 		ctrl->io_queues[HCTX_TYPE_DEFAULT],
2291 		ctrl->io_queues[HCTX_TYPE_READ],
2292 		ctrl->io_queues[HCTX_TYPE_POLL]);
2293 
2294 	return 0;
2295 }
2296 
2297 static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx)
2298 {
2299 	struct nvme_tcp_queue *queue = hctx->driver_data;
2300 	struct sock *sk = queue->sock->sk;
2301 
2302 	if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2303 		return 0;
2304 
2305 	if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue))
2306 		sk_busy_loop(sk, true);
2307 	nvme_tcp_try_recv(queue);
2308 	return queue->nr_cqe;
2309 }
2310 
2311 static struct blk_mq_ops nvme_tcp_mq_ops = {
2312 	.queue_rq	= nvme_tcp_queue_rq,
2313 	.complete	= nvme_complete_rq,
2314 	.init_request	= nvme_tcp_init_request,
2315 	.exit_request	= nvme_tcp_exit_request,
2316 	.init_hctx	= nvme_tcp_init_hctx,
2317 	.timeout	= nvme_tcp_timeout,
2318 	.map_queues	= nvme_tcp_map_queues,
2319 	.poll		= nvme_tcp_poll,
2320 };
2321 
2322 static struct blk_mq_ops nvme_tcp_admin_mq_ops = {
2323 	.queue_rq	= nvme_tcp_queue_rq,
2324 	.complete	= nvme_complete_rq,
2325 	.init_request	= nvme_tcp_init_request,
2326 	.exit_request	= nvme_tcp_exit_request,
2327 	.init_hctx	= nvme_tcp_init_admin_hctx,
2328 	.timeout	= nvme_tcp_timeout,
2329 };
2330 
2331 static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
2332 	.name			= "tcp",
2333 	.module			= THIS_MODULE,
2334 	.flags			= NVME_F_FABRICS,
2335 	.reg_read32		= nvmf_reg_read32,
2336 	.reg_read64		= nvmf_reg_read64,
2337 	.reg_write32		= nvmf_reg_write32,
2338 	.free_ctrl		= nvme_tcp_free_ctrl,
2339 	.submit_async_event	= nvme_tcp_submit_async_event,
2340 	.delete_ctrl		= nvme_tcp_delete_ctrl,
2341 	.get_address		= nvmf_get_address,
2342 };
2343 
2344 static bool
2345 nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
2346 {
2347 	struct nvme_tcp_ctrl *ctrl;
2348 	bool found = false;
2349 
2350 	mutex_lock(&nvme_tcp_ctrl_mutex);
2351 	list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
2352 		found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2353 		if (found)
2354 			break;
2355 	}
2356 	mutex_unlock(&nvme_tcp_ctrl_mutex);
2357 
2358 	return found;
2359 }
2360 
2361 static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
2362 		struct nvmf_ctrl_options *opts)
2363 {
2364 	struct nvme_tcp_ctrl *ctrl;
2365 	int ret;
2366 
2367 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2368 	if (!ctrl)
2369 		return ERR_PTR(-ENOMEM);
2370 
2371 	INIT_LIST_HEAD(&ctrl->list);
2372 	ctrl->ctrl.opts = opts;
2373 	ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2374 				opts->nr_poll_queues + 1;
2375 	ctrl->ctrl.sqsize = opts->queue_size - 1;
2376 	ctrl->ctrl.kato = opts->kato;
2377 
2378 	INIT_DELAYED_WORK(&ctrl->connect_work,
2379 			nvme_tcp_reconnect_ctrl_work);
2380 	INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
2381 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
2382 
2383 	if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2384 		opts->trsvcid =
2385 			kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
2386 		if (!opts->trsvcid) {
2387 			ret = -ENOMEM;
2388 			goto out_free_ctrl;
2389 		}
2390 		opts->mask |= NVMF_OPT_TRSVCID;
2391 	}
2392 
2393 	ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2394 			opts->traddr, opts->trsvcid, &ctrl->addr);
2395 	if (ret) {
2396 		pr_err("malformed address passed: %s:%s\n",
2397 			opts->traddr, opts->trsvcid);
2398 		goto out_free_ctrl;
2399 	}
2400 
2401 	if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2402 		ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2403 			opts->host_traddr, NULL, &ctrl->src_addr);
2404 		if (ret) {
2405 			pr_err("malformed src address passed: %s\n",
2406 			       opts->host_traddr);
2407 			goto out_free_ctrl;
2408 		}
2409 	}
2410 
2411 	if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
2412 		ret = -EALREADY;
2413 		goto out_free_ctrl;
2414 	}
2415 
2416 	ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
2417 				GFP_KERNEL);
2418 	if (!ctrl->queues) {
2419 		ret = -ENOMEM;
2420 		goto out_free_ctrl;
2421 	}
2422 
2423 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
2424 	if (ret)
2425 		goto out_kfree_queues;
2426 
2427 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2428 		WARN_ON_ONCE(1);
2429 		ret = -EINTR;
2430 		goto out_uninit_ctrl;
2431 	}
2432 
2433 	ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
2434 	if (ret)
2435 		goto out_uninit_ctrl;
2436 
2437 	dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
2438 		ctrl->ctrl.opts->subsysnqn, &ctrl->addr);
2439 
2440 	mutex_lock(&nvme_tcp_ctrl_mutex);
2441 	list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
2442 	mutex_unlock(&nvme_tcp_ctrl_mutex);
2443 
2444 	return &ctrl->ctrl;
2445 
2446 out_uninit_ctrl:
2447 	nvme_uninit_ctrl(&ctrl->ctrl);
2448 	nvme_put_ctrl(&ctrl->ctrl);
2449 	if (ret > 0)
2450 		ret = -EIO;
2451 	return ERR_PTR(ret);
2452 out_kfree_queues:
2453 	kfree(ctrl->queues);
2454 out_free_ctrl:
2455 	kfree(ctrl);
2456 	return ERR_PTR(ret);
2457 }
2458 
2459 static struct nvmf_transport_ops nvme_tcp_transport = {
2460 	.name		= "tcp",
2461 	.module		= THIS_MODULE,
2462 	.required_opts	= NVMF_OPT_TRADDR,
2463 	.allowed_opts	= NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2464 			  NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2465 			  NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
2466 			  NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2467 			  NVMF_OPT_TOS,
2468 	.create_ctrl	= nvme_tcp_create_ctrl,
2469 };
2470 
2471 static int __init nvme_tcp_init_module(void)
2472 {
2473 	nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq",
2474 			WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2475 	if (!nvme_tcp_wq)
2476 		return -ENOMEM;
2477 
2478 	nvmf_register_transport(&nvme_tcp_transport);
2479 	return 0;
2480 }
2481 
2482 static void __exit nvme_tcp_cleanup_module(void)
2483 {
2484 	struct nvme_tcp_ctrl *ctrl;
2485 
2486 	nvmf_unregister_transport(&nvme_tcp_transport);
2487 
2488 	mutex_lock(&nvme_tcp_ctrl_mutex);
2489 	list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
2490 		nvme_delete_ctrl(&ctrl->ctrl);
2491 	mutex_unlock(&nvme_tcp_ctrl_mutex);
2492 	flush_workqueue(nvme_delete_wq);
2493 
2494 	destroy_workqueue(nvme_tcp_wq);
2495 }
2496 
2497 module_init(nvme_tcp_init_module);
2498 module_exit(nvme_tcp_cleanup_module);
2499 
2500 MODULE_LICENSE("GPL v2");
2501