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