xref: /openbmc/linux/drivers/nvme/host/tcp.c (revision ffcdf473)
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 unsigned int nvme_tcp_nr_io_queues(struct nvme_ctrl *ctrl)
1806 {
1807 	unsigned int nr_io_queues;
1808 
1809 	nr_io_queues = min(ctrl->opts->nr_io_queues, num_online_cpus());
1810 	nr_io_queues += min(ctrl->opts->nr_write_queues, num_online_cpus());
1811 	nr_io_queues += min(ctrl->opts->nr_poll_queues, num_online_cpus());
1812 
1813 	return nr_io_queues;
1814 }
1815 
1816 static void nvme_tcp_set_io_queues(struct nvme_ctrl *nctrl,
1817 		unsigned int nr_io_queues)
1818 {
1819 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
1820 	struct nvmf_ctrl_options *opts = nctrl->opts;
1821 
1822 	if (opts->nr_write_queues && opts->nr_io_queues < nr_io_queues) {
1823 		/*
1824 		 * separate read/write queues
1825 		 * hand out dedicated default queues only after we have
1826 		 * sufficient read queues.
1827 		 */
1828 		ctrl->io_queues[HCTX_TYPE_READ] = opts->nr_io_queues;
1829 		nr_io_queues -= ctrl->io_queues[HCTX_TYPE_READ];
1830 		ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1831 			min(opts->nr_write_queues, nr_io_queues);
1832 		nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1833 	} else {
1834 		/*
1835 		 * shared read/write queues
1836 		 * either no write queues were requested, or we don't have
1837 		 * sufficient queue count to have dedicated default queues.
1838 		 */
1839 		ctrl->io_queues[HCTX_TYPE_DEFAULT] =
1840 			min(opts->nr_io_queues, nr_io_queues);
1841 		nr_io_queues -= ctrl->io_queues[HCTX_TYPE_DEFAULT];
1842 	}
1843 
1844 	if (opts->nr_poll_queues && nr_io_queues) {
1845 		/* map dedicated poll queues only if we have queues left */
1846 		ctrl->io_queues[HCTX_TYPE_POLL] =
1847 			min(opts->nr_poll_queues, nr_io_queues);
1848 	}
1849 }
1850 
1851 static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
1852 {
1853 	unsigned int nr_io_queues;
1854 	int ret;
1855 
1856 	nr_io_queues = nvme_tcp_nr_io_queues(ctrl);
1857 	ret = nvme_set_queue_count(ctrl, &nr_io_queues);
1858 	if (ret)
1859 		return ret;
1860 
1861 	if (nr_io_queues == 0) {
1862 		dev_err(ctrl->device,
1863 			"unable to set any I/O queues\n");
1864 		return -ENOMEM;
1865 	}
1866 
1867 	ctrl->queue_count = nr_io_queues + 1;
1868 	dev_info(ctrl->device,
1869 		"creating %d I/O queues.\n", nr_io_queues);
1870 
1871 	nvme_tcp_set_io_queues(ctrl, nr_io_queues);
1872 
1873 	return __nvme_tcp_alloc_io_queues(ctrl);
1874 }
1875 
1876 static void nvme_tcp_destroy_io_queues(struct nvme_ctrl *ctrl, bool remove)
1877 {
1878 	nvme_tcp_stop_io_queues(ctrl);
1879 	if (remove)
1880 		nvme_remove_io_tag_set(ctrl);
1881 	nvme_tcp_free_io_queues(ctrl);
1882 }
1883 
1884 static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
1885 {
1886 	int ret, nr_queues;
1887 
1888 	ret = nvme_tcp_alloc_io_queues(ctrl);
1889 	if (ret)
1890 		return ret;
1891 
1892 	if (new) {
1893 		ret = nvme_alloc_io_tag_set(ctrl, &to_tcp_ctrl(ctrl)->tag_set,
1894 				&nvme_tcp_mq_ops,
1895 				ctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2,
1896 				sizeof(struct nvme_tcp_request));
1897 		if (ret)
1898 			goto out_free_io_queues;
1899 	}
1900 
1901 	/*
1902 	 * Only start IO queues for which we have allocated the tagset
1903 	 * and limitted it to the available queues. On reconnects, the
1904 	 * queue number might have changed.
1905 	 */
1906 	nr_queues = min(ctrl->tagset->nr_hw_queues + 1, ctrl->queue_count);
1907 	ret = nvme_tcp_start_io_queues(ctrl, 1, nr_queues);
1908 	if (ret)
1909 		goto out_cleanup_connect_q;
1910 
1911 	if (!new) {
1912 		nvme_unquiesce_io_queues(ctrl);
1913 		if (!nvme_wait_freeze_timeout(ctrl, NVME_IO_TIMEOUT)) {
1914 			/*
1915 			 * If we timed out waiting for freeze we are likely to
1916 			 * be stuck.  Fail the controller initialization just
1917 			 * to be safe.
1918 			 */
1919 			ret = -ENODEV;
1920 			goto out_wait_freeze_timed_out;
1921 		}
1922 		blk_mq_update_nr_hw_queues(ctrl->tagset,
1923 			ctrl->queue_count - 1);
1924 		nvme_unfreeze(ctrl);
1925 	}
1926 
1927 	/*
1928 	 * If the number of queues has increased (reconnect case)
1929 	 * start all new queues now.
1930 	 */
1931 	ret = nvme_tcp_start_io_queues(ctrl, nr_queues,
1932 				       ctrl->tagset->nr_hw_queues + 1);
1933 	if (ret)
1934 		goto out_wait_freeze_timed_out;
1935 
1936 	return 0;
1937 
1938 out_wait_freeze_timed_out:
1939 	nvme_quiesce_io_queues(ctrl);
1940 	nvme_sync_io_queues(ctrl);
1941 	nvme_tcp_stop_io_queues(ctrl);
1942 out_cleanup_connect_q:
1943 	nvme_cancel_tagset(ctrl);
1944 	if (new)
1945 		nvme_remove_io_tag_set(ctrl);
1946 out_free_io_queues:
1947 	nvme_tcp_free_io_queues(ctrl);
1948 	return ret;
1949 }
1950 
1951 static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl *ctrl, bool remove)
1952 {
1953 	nvme_tcp_stop_queue(ctrl, 0);
1954 	if (remove)
1955 		nvme_remove_admin_tag_set(ctrl);
1956 	nvme_tcp_free_admin_queue(ctrl);
1957 }
1958 
1959 static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
1960 {
1961 	int error;
1962 
1963 	error = nvme_tcp_alloc_admin_queue(ctrl);
1964 	if (error)
1965 		return error;
1966 
1967 	if (new) {
1968 		error = nvme_alloc_admin_tag_set(ctrl,
1969 				&to_tcp_ctrl(ctrl)->admin_tag_set,
1970 				&nvme_tcp_admin_mq_ops,
1971 				sizeof(struct nvme_tcp_request));
1972 		if (error)
1973 			goto out_free_queue;
1974 	}
1975 
1976 	error = nvme_tcp_start_queue(ctrl, 0);
1977 	if (error)
1978 		goto out_cleanup_tagset;
1979 
1980 	error = nvme_enable_ctrl(ctrl);
1981 	if (error)
1982 		goto out_stop_queue;
1983 
1984 	nvme_unquiesce_admin_queue(ctrl);
1985 
1986 	error = nvme_init_ctrl_finish(ctrl, false);
1987 	if (error)
1988 		goto out_quiesce_queue;
1989 
1990 	return 0;
1991 
1992 out_quiesce_queue:
1993 	nvme_quiesce_admin_queue(ctrl);
1994 	blk_sync_queue(ctrl->admin_q);
1995 out_stop_queue:
1996 	nvme_tcp_stop_queue(ctrl, 0);
1997 	nvme_cancel_admin_tagset(ctrl);
1998 out_cleanup_tagset:
1999 	if (new)
2000 		nvme_remove_admin_tag_set(ctrl);
2001 out_free_queue:
2002 	nvme_tcp_free_admin_queue(ctrl);
2003 	return error;
2004 }
2005 
2006 static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
2007 		bool remove)
2008 {
2009 	nvme_quiesce_admin_queue(ctrl);
2010 	blk_sync_queue(ctrl->admin_q);
2011 	nvme_tcp_stop_queue(ctrl, 0);
2012 	nvme_cancel_admin_tagset(ctrl);
2013 	if (remove)
2014 		nvme_unquiesce_admin_queue(ctrl);
2015 	nvme_tcp_destroy_admin_queue(ctrl, remove);
2016 }
2017 
2018 static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
2019 		bool remove)
2020 {
2021 	if (ctrl->queue_count <= 1)
2022 		return;
2023 	nvme_quiesce_admin_queue(ctrl);
2024 	nvme_start_freeze(ctrl);
2025 	nvme_quiesce_io_queues(ctrl);
2026 	nvme_sync_io_queues(ctrl);
2027 	nvme_tcp_stop_io_queues(ctrl);
2028 	nvme_cancel_tagset(ctrl);
2029 	if (remove)
2030 		nvme_unquiesce_io_queues(ctrl);
2031 	nvme_tcp_destroy_io_queues(ctrl, remove);
2032 }
2033 
2034 static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl)
2035 {
2036 	/* If we are resetting/deleting then do nothing */
2037 	if (ctrl->state != NVME_CTRL_CONNECTING) {
2038 		WARN_ON_ONCE(ctrl->state == NVME_CTRL_NEW ||
2039 			ctrl->state == NVME_CTRL_LIVE);
2040 		return;
2041 	}
2042 
2043 	if (nvmf_should_reconnect(ctrl)) {
2044 		dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
2045 			ctrl->opts->reconnect_delay);
2046 		queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
2047 				ctrl->opts->reconnect_delay * HZ);
2048 	} else {
2049 		dev_info(ctrl->device, "Removing controller...\n");
2050 		nvme_delete_ctrl(ctrl);
2051 	}
2052 }
2053 
2054 static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
2055 {
2056 	struct nvmf_ctrl_options *opts = ctrl->opts;
2057 	int ret;
2058 
2059 	ret = nvme_tcp_configure_admin_queue(ctrl, new);
2060 	if (ret)
2061 		return ret;
2062 
2063 	if (ctrl->icdoff) {
2064 		ret = -EOPNOTSUPP;
2065 		dev_err(ctrl->device, "icdoff is not supported!\n");
2066 		goto destroy_admin;
2067 	}
2068 
2069 	if (!nvme_ctrl_sgl_supported(ctrl)) {
2070 		ret = -EOPNOTSUPP;
2071 		dev_err(ctrl->device, "Mandatory sgls are not supported!\n");
2072 		goto destroy_admin;
2073 	}
2074 
2075 	if (opts->queue_size > ctrl->sqsize + 1)
2076 		dev_warn(ctrl->device,
2077 			"queue_size %zu > ctrl sqsize %u, clamping down\n",
2078 			opts->queue_size, ctrl->sqsize + 1);
2079 
2080 	if (ctrl->sqsize + 1 > ctrl->maxcmd) {
2081 		dev_warn(ctrl->device,
2082 			"sqsize %u > ctrl maxcmd %u, clamping down\n",
2083 			ctrl->sqsize + 1, ctrl->maxcmd);
2084 		ctrl->sqsize = ctrl->maxcmd - 1;
2085 	}
2086 
2087 	if (ctrl->queue_count > 1) {
2088 		ret = nvme_tcp_configure_io_queues(ctrl, new);
2089 		if (ret)
2090 			goto destroy_admin;
2091 	}
2092 
2093 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
2094 		/*
2095 		 * state change failure is ok if we started ctrl delete,
2096 		 * unless we're during creation of a new controller to
2097 		 * avoid races with teardown flow.
2098 		 */
2099 		WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2100 			     ctrl->state != NVME_CTRL_DELETING_NOIO);
2101 		WARN_ON_ONCE(new);
2102 		ret = -EINVAL;
2103 		goto destroy_io;
2104 	}
2105 
2106 	nvme_start_ctrl(ctrl);
2107 	return 0;
2108 
2109 destroy_io:
2110 	if (ctrl->queue_count > 1) {
2111 		nvme_quiesce_io_queues(ctrl);
2112 		nvme_sync_io_queues(ctrl);
2113 		nvme_tcp_stop_io_queues(ctrl);
2114 		nvme_cancel_tagset(ctrl);
2115 		nvme_tcp_destroy_io_queues(ctrl, new);
2116 	}
2117 destroy_admin:
2118 	nvme_quiesce_admin_queue(ctrl);
2119 	blk_sync_queue(ctrl->admin_q);
2120 	nvme_tcp_stop_queue(ctrl, 0);
2121 	nvme_cancel_admin_tagset(ctrl);
2122 	nvme_tcp_destroy_admin_queue(ctrl, new);
2123 	return ret;
2124 }
2125 
2126 static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
2127 {
2128 	struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
2129 			struct nvme_tcp_ctrl, connect_work);
2130 	struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2131 
2132 	++ctrl->nr_reconnects;
2133 
2134 	if (nvme_tcp_setup_ctrl(ctrl, false))
2135 		goto requeue;
2136 
2137 	dev_info(ctrl->device, "Successfully reconnected (%d attempt)\n",
2138 			ctrl->nr_reconnects);
2139 
2140 	ctrl->nr_reconnects = 0;
2141 
2142 	return;
2143 
2144 requeue:
2145 	dev_info(ctrl->device, "Failed reconnect attempt %d\n",
2146 			ctrl->nr_reconnects);
2147 	nvme_tcp_reconnect_or_remove(ctrl);
2148 }
2149 
2150 static void nvme_tcp_error_recovery_work(struct work_struct *work)
2151 {
2152 	struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
2153 				struct nvme_tcp_ctrl, err_work);
2154 	struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2155 
2156 	nvme_stop_keep_alive(ctrl);
2157 	flush_work(&ctrl->async_event_work);
2158 	nvme_tcp_teardown_io_queues(ctrl, false);
2159 	/* unquiesce to fail fast pending requests */
2160 	nvme_unquiesce_io_queues(ctrl);
2161 	nvme_tcp_teardown_admin_queue(ctrl, false);
2162 	nvme_unquiesce_admin_queue(ctrl);
2163 	nvme_auth_stop(ctrl);
2164 
2165 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2166 		/* state change failure is ok if we started ctrl delete */
2167 		WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2168 			     ctrl->state != NVME_CTRL_DELETING_NOIO);
2169 		return;
2170 	}
2171 
2172 	nvme_tcp_reconnect_or_remove(ctrl);
2173 }
2174 
2175 static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
2176 {
2177 	nvme_tcp_teardown_io_queues(ctrl, shutdown);
2178 	nvme_quiesce_admin_queue(ctrl);
2179 	nvme_disable_ctrl(ctrl, shutdown);
2180 	nvme_tcp_teardown_admin_queue(ctrl, shutdown);
2181 }
2182 
2183 static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
2184 {
2185 	nvme_tcp_teardown_ctrl(ctrl, true);
2186 }
2187 
2188 static void nvme_reset_ctrl_work(struct work_struct *work)
2189 {
2190 	struct nvme_ctrl *ctrl =
2191 		container_of(work, struct nvme_ctrl, reset_work);
2192 
2193 	nvme_stop_ctrl(ctrl);
2194 	nvme_tcp_teardown_ctrl(ctrl, false);
2195 
2196 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2197 		/* state change failure is ok if we started ctrl delete */
2198 		WARN_ON_ONCE(ctrl->state != NVME_CTRL_DELETING &&
2199 			     ctrl->state != NVME_CTRL_DELETING_NOIO);
2200 		return;
2201 	}
2202 
2203 	if (nvme_tcp_setup_ctrl(ctrl, false))
2204 		goto out_fail;
2205 
2206 	return;
2207 
2208 out_fail:
2209 	++ctrl->nr_reconnects;
2210 	nvme_tcp_reconnect_or_remove(ctrl);
2211 }
2212 
2213 static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl)
2214 {
2215 	flush_work(&to_tcp_ctrl(ctrl)->err_work);
2216 	cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
2217 }
2218 
2219 static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
2220 {
2221 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
2222 
2223 	if (list_empty(&ctrl->list))
2224 		goto free_ctrl;
2225 
2226 	mutex_lock(&nvme_tcp_ctrl_mutex);
2227 	list_del(&ctrl->list);
2228 	mutex_unlock(&nvme_tcp_ctrl_mutex);
2229 
2230 	nvmf_free_options(nctrl->opts);
2231 free_ctrl:
2232 	kfree(ctrl->queues);
2233 	kfree(ctrl);
2234 }
2235 
2236 static void nvme_tcp_set_sg_null(struct nvme_command *c)
2237 {
2238 	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2239 
2240 	sg->addr = 0;
2241 	sg->length = 0;
2242 	sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2243 			NVME_SGL_FMT_TRANSPORT_A;
2244 }
2245 
2246 static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
2247 		struct nvme_command *c, u32 data_len)
2248 {
2249 	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2250 
2251 	sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
2252 	sg->length = cpu_to_le32(data_len);
2253 	sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
2254 }
2255 
2256 static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
2257 		u32 data_len)
2258 {
2259 	struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
2260 
2261 	sg->addr = 0;
2262 	sg->length = cpu_to_le32(data_len);
2263 	sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
2264 			NVME_SGL_FMT_TRANSPORT_A;
2265 }
2266 
2267 static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
2268 {
2269 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
2270 	struct nvme_tcp_queue *queue = &ctrl->queues[0];
2271 	struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
2272 	struct nvme_command *cmd = &pdu->cmd;
2273 	u8 hdgst = nvme_tcp_hdgst_len(queue);
2274 
2275 	memset(pdu, 0, sizeof(*pdu));
2276 	pdu->hdr.type = nvme_tcp_cmd;
2277 	if (queue->hdr_digest)
2278 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
2279 	pdu->hdr.hlen = sizeof(*pdu);
2280 	pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
2281 
2282 	cmd->common.opcode = nvme_admin_async_event;
2283 	cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
2284 	cmd->common.flags |= NVME_CMD_SGL_METABUF;
2285 	nvme_tcp_set_sg_null(cmd);
2286 
2287 	ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
2288 	ctrl->async_req.offset = 0;
2289 	ctrl->async_req.curr_bio = NULL;
2290 	ctrl->async_req.data_len = 0;
2291 
2292 	nvme_tcp_queue_request(&ctrl->async_req, true, true);
2293 }
2294 
2295 static void nvme_tcp_complete_timed_out(struct request *rq)
2296 {
2297 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2298 	struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
2299 
2300 	nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
2301 	nvmf_complete_timed_out_request(rq);
2302 }
2303 
2304 static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq)
2305 {
2306 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2307 	struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
2308 	struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
2309 	u8 opc = pdu->cmd.common.opcode, fctype = pdu->cmd.fabrics.fctype;
2310 	int qid = nvme_tcp_queue_id(req->queue);
2311 
2312 	dev_warn(ctrl->device,
2313 		"queue %d: timeout cid %#x type %d opcode %#x (%s)\n",
2314 		nvme_tcp_queue_id(req->queue), nvme_cid(rq), pdu->hdr.type,
2315 		opc, nvme_opcode_str(qid, opc, fctype));
2316 
2317 	if (ctrl->state != NVME_CTRL_LIVE) {
2318 		/*
2319 		 * If we are resetting, connecting or deleting we should
2320 		 * complete immediately because we may block controller
2321 		 * teardown or setup sequence
2322 		 * - ctrl disable/shutdown fabrics requests
2323 		 * - connect requests
2324 		 * - initialization admin requests
2325 		 * - I/O requests that entered after unquiescing and
2326 		 *   the controller stopped responding
2327 		 *
2328 		 * All other requests should be cancelled by the error
2329 		 * recovery work, so it's fine that we fail it here.
2330 		 */
2331 		nvme_tcp_complete_timed_out(rq);
2332 		return BLK_EH_DONE;
2333 	}
2334 
2335 	/*
2336 	 * LIVE state should trigger the normal error recovery which will
2337 	 * handle completing this request.
2338 	 */
2339 	nvme_tcp_error_recovery(ctrl);
2340 	return BLK_EH_RESET_TIMER;
2341 }
2342 
2343 static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
2344 			struct request *rq)
2345 {
2346 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2347 	struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
2348 	struct nvme_command *c = &pdu->cmd;
2349 
2350 	c->common.flags |= NVME_CMD_SGL_METABUF;
2351 
2352 	if (!blk_rq_nr_phys_segments(rq))
2353 		nvme_tcp_set_sg_null(c);
2354 	else if (rq_data_dir(rq) == WRITE &&
2355 	    req->data_len <= nvme_tcp_inline_data_size(req))
2356 		nvme_tcp_set_sg_inline(queue, c, req->data_len);
2357 	else
2358 		nvme_tcp_set_sg_host_data(c, req->data_len);
2359 
2360 	return 0;
2361 }
2362 
2363 static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
2364 		struct request *rq)
2365 {
2366 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2367 	struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
2368 	struct nvme_tcp_queue *queue = req->queue;
2369 	u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
2370 	blk_status_t ret;
2371 
2372 	ret = nvme_setup_cmd(ns, rq);
2373 	if (ret)
2374 		return ret;
2375 
2376 	req->state = NVME_TCP_SEND_CMD_PDU;
2377 	req->status = cpu_to_le16(NVME_SC_SUCCESS);
2378 	req->offset = 0;
2379 	req->data_sent = 0;
2380 	req->pdu_len = 0;
2381 	req->pdu_sent = 0;
2382 	req->h2cdata_left = 0;
2383 	req->data_len = blk_rq_nr_phys_segments(rq) ?
2384 				blk_rq_payload_bytes(rq) : 0;
2385 	req->curr_bio = rq->bio;
2386 	if (req->curr_bio && req->data_len)
2387 		nvme_tcp_init_iter(req, rq_data_dir(rq));
2388 
2389 	if (rq_data_dir(rq) == WRITE &&
2390 	    req->data_len <= nvme_tcp_inline_data_size(req))
2391 		req->pdu_len = req->data_len;
2392 
2393 	pdu->hdr.type = nvme_tcp_cmd;
2394 	pdu->hdr.flags = 0;
2395 	if (queue->hdr_digest)
2396 		pdu->hdr.flags |= NVME_TCP_F_HDGST;
2397 	if (queue->data_digest && req->pdu_len) {
2398 		pdu->hdr.flags |= NVME_TCP_F_DDGST;
2399 		ddgst = nvme_tcp_ddgst_len(queue);
2400 	}
2401 	pdu->hdr.hlen = sizeof(*pdu);
2402 	pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
2403 	pdu->hdr.plen =
2404 		cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
2405 
2406 	ret = nvme_tcp_map_data(queue, rq);
2407 	if (unlikely(ret)) {
2408 		nvme_cleanup_cmd(rq);
2409 		dev_err(queue->ctrl->ctrl.device,
2410 			"Failed to map data (%d)\n", ret);
2411 		return ret;
2412 	}
2413 
2414 	return 0;
2415 }
2416 
2417 static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx)
2418 {
2419 	struct nvme_tcp_queue *queue = hctx->driver_data;
2420 
2421 	if (!llist_empty(&queue->req_list))
2422 		queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
2423 }
2424 
2425 static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
2426 		const struct blk_mq_queue_data *bd)
2427 {
2428 	struct nvme_ns *ns = hctx->queue->queuedata;
2429 	struct nvme_tcp_queue *queue = hctx->driver_data;
2430 	struct request *rq = bd->rq;
2431 	struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2432 	bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
2433 	blk_status_t ret;
2434 
2435 	if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2436 		return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
2437 
2438 	ret = nvme_tcp_setup_cmd_pdu(ns, rq);
2439 	if (unlikely(ret))
2440 		return ret;
2441 
2442 	nvme_start_request(rq);
2443 
2444 	nvme_tcp_queue_request(req, true, bd->last);
2445 
2446 	return BLK_STS_OK;
2447 }
2448 
2449 static void nvme_tcp_map_queues(struct blk_mq_tag_set *set)
2450 {
2451 	struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data);
2452 	struct nvmf_ctrl_options *opts = ctrl->ctrl.opts;
2453 
2454 	if (opts->nr_write_queues && ctrl->io_queues[HCTX_TYPE_READ]) {
2455 		/* separate read/write queues */
2456 		set->map[HCTX_TYPE_DEFAULT].nr_queues =
2457 			ctrl->io_queues[HCTX_TYPE_DEFAULT];
2458 		set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2459 		set->map[HCTX_TYPE_READ].nr_queues =
2460 			ctrl->io_queues[HCTX_TYPE_READ];
2461 		set->map[HCTX_TYPE_READ].queue_offset =
2462 			ctrl->io_queues[HCTX_TYPE_DEFAULT];
2463 	} else {
2464 		/* shared read/write queues */
2465 		set->map[HCTX_TYPE_DEFAULT].nr_queues =
2466 			ctrl->io_queues[HCTX_TYPE_DEFAULT];
2467 		set->map[HCTX_TYPE_DEFAULT].queue_offset = 0;
2468 		set->map[HCTX_TYPE_READ].nr_queues =
2469 			ctrl->io_queues[HCTX_TYPE_DEFAULT];
2470 		set->map[HCTX_TYPE_READ].queue_offset = 0;
2471 	}
2472 	blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
2473 	blk_mq_map_queues(&set->map[HCTX_TYPE_READ]);
2474 
2475 	if (opts->nr_poll_queues && ctrl->io_queues[HCTX_TYPE_POLL]) {
2476 		/* map dedicated poll queues only if we have queues left */
2477 		set->map[HCTX_TYPE_POLL].nr_queues =
2478 				ctrl->io_queues[HCTX_TYPE_POLL];
2479 		set->map[HCTX_TYPE_POLL].queue_offset =
2480 			ctrl->io_queues[HCTX_TYPE_DEFAULT] +
2481 			ctrl->io_queues[HCTX_TYPE_READ];
2482 		blk_mq_map_queues(&set->map[HCTX_TYPE_POLL]);
2483 	}
2484 
2485 	dev_info(ctrl->ctrl.device,
2486 		"mapped %d/%d/%d default/read/poll queues.\n",
2487 		ctrl->io_queues[HCTX_TYPE_DEFAULT],
2488 		ctrl->io_queues[HCTX_TYPE_READ],
2489 		ctrl->io_queues[HCTX_TYPE_POLL]);
2490 }
2491 
2492 static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
2493 {
2494 	struct nvme_tcp_queue *queue = hctx->driver_data;
2495 	struct sock *sk = queue->sock->sk;
2496 
2497 	if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2498 		return 0;
2499 
2500 	set_bit(NVME_TCP_Q_POLLING, &queue->flags);
2501 	if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue))
2502 		sk_busy_loop(sk, true);
2503 	nvme_tcp_try_recv(queue);
2504 	clear_bit(NVME_TCP_Q_POLLING, &queue->flags);
2505 	return queue->nr_cqe;
2506 }
2507 
2508 static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
2509 {
2510 	struct nvme_tcp_queue *queue = &to_tcp_ctrl(ctrl)->queues[0];
2511 	struct sockaddr_storage src_addr;
2512 	int ret, len;
2513 
2514 	len = nvmf_get_address(ctrl, buf, size);
2515 
2516 	mutex_lock(&queue->queue_lock);
2517 
2518 	if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2519 		goto done;
2520 	ret = kernel_getsockname(queue->sock, (struct sockaddr *)&src_addr);
2521 	if (ret > 0) {
2522 		if (len > 0)
2523 			len--; /* strip trailing newline */
2524 		len += scnprintf(buf + len, size - len, "%ssrc_addr=%pISc\n",
2525 				(len) ? "," : "", &src_addr);
2526 	}
2527 done:
2528 	mutex_unlock(&queue->queue_lock);
2529 
2530 	return len;
2531 }
2532 
2533 static const struct blk_mq_ops nvme_tcp_mq_ops = {
2534 	.queue_rq	= nvme_tcp_queue_rq,
2535 	.commit_rqs	= nvme_tcp_commit_rqs,
2536 	.complete	= nvme_complete_rq,
2537 	.init_request	= nvme_tcp_init_request,
2538 	.exit_request	= nvme_tcp_exit_request,
2539 	.init_hctx	= nvme_tcp_init_hctx,
2540 	.timeout	= nvme_tcp_timeout,
2541 	.map_queues	= nvme_tcp_map_queues,
2542 	.poll		= nvme_tcp_poll,
2543 };
2544 
2545 static const struct blk_mq_ops nvme_tcp_admin_mq_ops = {
2546 	.queue_rq	= nvme_tcp_queue_rq,
2547 	.complete	= nvme_complete_rq,
2548 	.init_request	= nvme_tcp_init_request,
2549 	.exit_request	= nvme_tcp_exit_request,
2550 	.init_hctx	= nvme_tcp_init_admin_hctx,
2551 	.timeout	= nvme_tcp_timeout,
2552 };
2553 
2554 static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
2555 	.name			= "tcp",
2556 	.module			= THIS_MODULE,
2557 	.flags			= NVME_F_FABRICS | NVME_F_BLOCKING,
2558 	.reg_read32		= nvmf_reg_read32,
2559 	.reg_read64		= nvmf_reg_read64,
2560 	.reg_write32		= nvmf_reg_write32,
2561 	.free_ctrl		= nvme_tcp_free_ctrl,
2562 	.submit_async_event	= nvme_tcp_submit_async_event,
2563 	.delete_ctrl		= nvme_tcp_delete_ctrl,
2564 	.get_address		= nvme_tcp_get_address,
2565 	.stop_ctrl		= nvme_tcp_stop_ctrl,
2566 };
2567 
2568 static bool
2569 nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
2570 {
2571 	struct nvme_tcp_ctrl *ctrl;
2572 	bool found = false;
2573 
2574 	mutex_lock(&nvme_tcp_ctrl_mutex);
2575 	list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
2576 		found = nvmf_ip_options_match(&ctrl->ctrl, opts);
2577 		if (found)
2578 			break;
2579 	}
2580 	mutex_unlock(&nvme_tcp_ctrl_mutex);
2581 
2582 	return found;
2583 }
2584 
2585 static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
2586 		struct nvmf_ctrl_options *opts)
2587 {
2588 	struct nvme_tcp_ctrl *ctrl;
2589 	int ret;
2590 
2591 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
2592 	if (!ctrl)
2593 		return ERR_PTR(-ENOMEM);
2594 
2595 	INIT_LIST_HEAD(&ctrl->list);
2596 	ctrl->ctrl.opts = opts;
2597 	ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
2598 				opts->nr_poll_queues + 1;
2599 	ctrl->ctrl.sqsize = opts->queue_size - 1;
2600 	ctrl->ctrl.kato = opts->kato;
2601 
2602 	INIT_DELAYED_WORK(&ctrl->connect_work,
2603 			nvme_tcp_reconnect_ctrl_work);
2604 	INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
2605 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
2606 
2607 	if (!(opts->mask & NVMF_OPT_TRSVCID)) {
2608 		opts->trsvcid =
2609 			kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
2610 		if (!opts->trsvcid) {
2611 			ret = -ENOMEM;
2612 			goto out_free_ctrl;
2613 		}
2614 		opts->mask |= NVMF_OPT_TRSVCID;
2615 	}
2616 
2617 	ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2618 			opts->traddr, opts->trsvcid, &ctrl->addr);
2619 	if (ret) {
2620 		pr_err("malformed address passed: %s:%s\n",
2621 			opts->traddr, opts->trsvcid);
2622 		goto out_free_ctrl;
2623 	}
2624 
2625 	if (opts->mask & NVMF_OPT_HOST_TRADDR) {
2626 		ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
2627 			opts->host_traddr, NULL, &ctrl->src_addr);
2628 		if (ret) {
2629 			pr_err("malformed src address passed: %s\n",
2630 			       opts->host_traddr);
2631 			goto out_free_ctrl;
2632 		}
2633 	}
2634 
2635 	if (opts->mask & NVMF_OPT_HOST_IFACE) {
2636 		if (!__dev_get_by_name(&init_net, opts->host_iface)) {
2637 			pr_err("invalid interface passed: %s\n",
2638 			       opts->host_iface);
2639 			ret = -ENODEV;
2640 			goto out_free_ctrl;
2641 		}
2642 	}
2643 
2644 	if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
2645 		ret = -EALREADY;
2646 		goto out_free_ctrl;
2647 	}
2648 
2649 	ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
2650 				GFP_KERNEL);
2651 	if (!ctrl->queues) {
2652 		ret = -ENOMEM;
2653 		goto out_free_ctrl;
2654 	}
2655 
2656 	ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
2657 	if (ret)
2658 		goto out_kfree_queues;
2659 
2660 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
2661 		WARN_ON_ONCE(1);
2662 		ret = -EINTR;
2663 		goto out_uninit_ctrl;
2664 	}
2665 
2666 	ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
2667 	if (ret)
2668 		goto out_uninit_ctrl;
2669 
2670 	dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp\n",
2671 		nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr);
2672 
2673 	mutex_lock(&nvme_tcp_ctrl_mutex);
2674 	list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
2675 	mutex_unlock(&nvme_tcp_ctrl_mutex);
2676 
2677 	return &ctrl->ctrl;
2678 
2679 out_uninit_ctrl:
2680 	nvme_uninit_ctrl(&ctrl->ctrl);
2681 	nvme_put_ctrl(&ctrl->ctrl);
2682 	if (ret > 0)
2683 		ret = -EIO;
2684 	return ERR_PTR(ret);
2685 out_kfree_queues:
2686 	kfree(ctrl->queues);
2687 out_free_ctrl:
2688 	kfree(ctrl);
2689 	return ERR_PTR(ret);
2690 }
2691 
2692 static struct nvmf_transport_ops nvme_tcp_transport = {
2693 	.name		= "tcp",
2694 	.module		= THIS_MODULE,
2695 	.required_opts	= NVMF_OPT_TRADDR,
2696 	.allowed_opts	= NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
2697 			  NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2698 			  NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
2699 			  NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2700 			  NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE,
2701 	.create_ctrl	= nvme_tcp_create_ctrl,
2702 };
2703 
2704 static int __init nvme_tcp_init_module(void)
2705 {
2706 	BUILD_BUG_ON(sizeof(struct nvme_tcp_hdr) != 8);
2707 	BUILD_BUG_ON(sizeof(struct nvme_tcp_cmd_pdu) != 72);
2708 	BUILD_BUG_ON(sizeof(struct nvme_tcp_data_pdu) != 24);
2709 	BUILD_BUG_ON(sizeof(struct nvme_tcp_rsp_pdu) != 24);
2710 	BUILD_BUG_ON(sizeof(struct nvme_tcp_r2t_pdu) != 24);
2711 	BUILD_BUG_ON(sizeof(struct nvme_tcp_icreq_pdu) != 128);
2712 	BUILD_BUG_ON(sizeof(struct nvme_tcp_icresp_pdu) != 128);
2713 	BUILD_BUG_ON(sizeof(struct nvme_tcp_term_pdu) != 24);
2714 
2715 	nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq",
2716 			WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2717 	if (!nvme_tcp_wq)
2718 		return -ENOMEM;
2719 
2720 	nvmf_register_transport(&nvme_tcp_transport);
2721 	return 0;
2722 }
2723 
2724 static void __exit nvme_tcp_cleanup_module(void)
2725 {
2726 	struct nvme_tcp_ctrl *ctrl;
2727 
2728 	nvmf_unregister_transport(&nvme_tcp_transport);
2729 
2730 	mutex_lock(&nvme_tcp_ctrl_mutex);
2731 	list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
2732 		nvme_delete_ctrl(&ctrl->ctrl);
2733 	mutex_unlock(&nvme_tcp_ctrl_mutex);
2734 	flush_workqueue(nvme_delete_wq);
2735 
2736 	destroy_workqueue(nvme_tcp_wq);
2737 }
2738 
2739 module_init(nvme_tcp_init_module);
2740 module_exit(nvme_tcp_cleanup_module);
2741 
2742 MODULE_LICENSE("GPL v2");
2743