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