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