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