13f2304f8SSagi Grimberg // SPDX-License-Identifier: GPL-2.0
23f2304f8SSagi Grimberg /*
33f2304f8SSagi Grimberg * NVMe over Fabrics TCP host.
43f2304f8SSagi Grimberg * Copyright (c) 2018 Lightbits Labs. All rights reserved.
53f2304f8SSagi Grimberg */
63f2304f8SSagi Grimberg #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
73f2304f8SSagi Grimberg #include <linux/module.h>
83f2304f8SSagi Grimberg #include <linux/init.h>
93f2304f8SSagi Grimberg #include <linux/slab.h>
103f2304f8SSagi Grimberg #include <linux/err.h>
11be8e82caSHannes Reinecke #include <linux/key.h>
123f2304f8SSagi Grimberg #include <linux/nvme-tcp.h>
13be8e82caSHannes Reinecke #include <linux/nvme-keyring.h>
143f2304f8SSagi Grimberg #include <net/sock.h>
153f2304f8SSagi Grimberg #include <net/tcp.h>
16be8e82caSHannes Reinecke #include <net/tls.h>
172837966aSHannes Reinecke #include <net/tls_prot.h>
18be8e82caSHannes Reinecke #include <net/handshake.h>
193f2304f8SSagi Grimberg #include <linux/blk-mq.h>
203f2304f8SSagi Grimberg #include <crypto/hash.h>
211a9460ceSSagi Grimberg #include <net/busy_poll.h>
2240e0b090SPeilin Ye #include <trace/events/sock.h>
233f2304f8SSagi Grimberg
243f2304f8SSagi Grimberg #include "nvme.h"
253f2304f8SSagi Grimberg #include "fabrics.h"
263f2304f8SSagi Grimberg
273f2304f8SSagi Grimberg struct nvme_tcp_queue;
283f2304f8SSagi Grimberg
299912ade3SWunderlich, Mark /* Define the socket priority to use for connections were it is desirable
309912ade3SWunderlich, Mark * that the NIC consider performing optimized packet processing or filtering.
319912ade3SWunderlich, Mark * A non-zero value being sufficient to indicate general consideration of any
329912ade3SWunderlich, Mark * possible optimization. Making it a module param allows for alternative
339912ade3SWunderlich, Mark * values that may be unique for some NIC implementations.
349912ade3SWunderlich, Mark */
359912ade3SWunderlich, Mark static int so_priority;
369912ade3SWunderlich, Mark module_param(so_priority, int, 0644);
379912ade3SWunderlich, Mark MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority");
389912ade3SWunderlich, Mark
39be8e82caSHannes Reinecke /*
400c29f9faSLi Feng * Use the unbound workqueue for nvme_tcp_wq, then we can set the cpu affinity
410c29f9faSLi Feng * from sysfs.
420c29f9faSLi Feng */
430c29f9faSLi Feng static bool wq_unbound;
440c29f9faSLi Feng module_param(wq_unbound, bool, 0644);
450c29f9faSLi Feng MODULE_PARM_DESC(wq_unbound, "Use unbound workqueue for nvme-tcp IO context (default false)");
460c29f9faSLi Feng
470c29f9faSLi Feng /*
48be8e82caSHannes Reinecke * TLS handshake timeout
49be8e82caSHannes Reinecke */
50be8e82caSHannes Reinecke static int tls_handshake_timeout = 10;
510e6c4fe7SArnd Bergmann #ifdef CONFIG_NVME_TCP_TLS
52be8e82caSHannes Reinecke module_param(tls_handshake_timeout, int, 0644);
53be8e82caSHannes Reinecke MODULE_PARM_DESC(tls_handshake_timeout,
54be8e82caSHannes Reinecke "nvme TLS handshake timeout in seconds (default 10)");
55be8e82caSHannes Reinecke #endif
56be8e82caSHannes Reinecke
57841aee4dSChris Leech #ifdef CONFIG_DEBUG_LOCK_ALLOC
58841aee4dSChris Leech /* lockdep can detect a circular dependency of the form
59841aee4dSChris Leech * sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock
60841aee4dSChris Leech * because dependencies are tracked for both nvme-tcp and user contexts. Using
61841aee4dSChris Leech * a separate class prevents lockdep from conflating nvme-tcp socket use with
62841aee4dSChris Leech * user-space socket API use.
63841aee4dSChris Leech */
64841aee4dSChris Leech static struct lock_class_key nvme_tcp_sk_key[2];
65841aee4dSChris Leech static struct lock_class_key nvme_tcp_slock_key[2];
66841aee4dSChris Leech
nvme_tcp_reclassify_socket(struct socket * sock)67841aee4dSChris Leech static void nvme_tcp_reclassify_socket(struct socket *sock)
68841aee4dSChris Leech {
69841aee4dSChris Leech struct sock *sk = sock->sk;
70841aee4dSChris Leech
71841aee4dSChris Leech if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
72841aee4dSChris Leech return;
73841aee4dSChris Leech
74841aee4dSChris Leech switch (sk->sk_family) {
75841aee4dSChris Leech case AF_INET:
76841aee4dSChris Leech sock_lock_init_class_and_name(sk, "slock-AF_INET-NVME",
77841aee4dSChris Leech &nvme_tcp_slock_key[0],
78841aee4dSChris Leech "sk_lock-AF_INET-NVME",
79841aee4dSChris Leech &nvme_tcp_sk_key[0]);
80841aee4dSChris Leech break;
81841aee4dSChris Leech case AF_INET6:
82841aee4dSChris Leech sock_lock_init_class_and_name(sk, "slock-AF_INET6-NVME",
83841aee4dSChris Leech &nvme_tcp_slock_key[1],
84841aee4dSChris Leech "sk_lock-AF_INET6-NVME",
85841aee4dSChris Leech &nvme_tcp_sk_key[1]);
86841aee4dSChris Leech break;
87841aee4dSChris Leech default:
88841aee4dSChris Leech WARN_ON_ONCE(1);
89841aee4dSChris Leech }
90841aee4dSChris Leech }
91841aee4dSChris Leech #else
nvme_tcp_reclassify_socket(struct socket * sock)92841aee4dSChris Leech static void nvme_tcp_reclassify_socket(struct socket *sock) { }
93841aee4dSChris Leech #endif
94841aee4dSChris Leech
953f2304f8SSagi Grimberg enum nvme_tcp_send_state {
963f2304f8SSagi Grimberg NVME_TCP_SEND_CMD_PDU = 0,
973f2304f8SSagi Grimberg NVME_TCP_SEND_H2C_PDU,
983f2304f8SSagi Grimberg NVME_TCP_SEND_DATA,
993f2304f8SSagi Grimberg NVME_TCP_SEND_DDGST,
1003f2304f8SSagi Grimberg };
1013f2304f8SSagi Grimberg
1023f2304f8SSagi Grimberg struct nvme_tcp_request {
1033f2304f8SSagi Grimberg struct nvme_request req;
1043f2304f8SSagi Grimberg void *pdu;
1053f2304f8SSagi Grimberg struct nvme_tcp_queue *queue;
1063f2304f8SSagi Grimberg u32 data_len;
1073f2304f8SSagi Grimberg u32 pdu_len;
1083f2304f8SSagi Grimberg u32 pdu_sent;
109c2700d28SVarun Prakash u32 h2cdata_left;
110c2700d28SVarun Prakash u32 h2cdata_offset;
1113f2304f8SSagi Grimberg u16 ttag;
1121ba2e507SDaniel Wagner __le16 status;
1133f2304f8SSagi Grimberg struct list_head entry;
11415ec928aSSagi Grimberg struct llist_node lentry;
115a7273d40SChristoph Hellwig __le32 ddgst;
1163f2304f8SSagi Grimberg
1173f2304f8SSagi Grimberg struct bio *curr_bio;
1183f2304f8SSagi Grimberg struct iov_iter iter;
1193f2304f8SSagi Grimberg
1203f2304f8SSagi Grimberg /* send state */
1213f2304f8SSagi Grimberg size_t offset;
1223f2304f8SSagi Grimberg size_t data_sent;
1233f2304f8SSagi Grimberg enum nvme_tcp_send_state state;
1243f2304f8SSagi Grimberg };
1253f2304f8SSagi Grimberg
1263f2304f8SSagi Grimberg enum nvme_tcp_queue_flags {
1273f2304f8SSagi Grimberg NVME_TCP_Q_ALLOCATED = 0,
1283f2304f8SSagi Grimberg NVME_TCP_Q_LIVE = 1,
12972e5d757SSagi Grimberg NVME_TCP_Q_POLLING = 2,
1303f2304f8SSagi Grimberg };
1313f2304f8SSagi Grimberg
1323f2304f8SSagi Grimberg enum nvme_tcp_recv_state {
1333f2304f8SSagi Grimberg NVME_TCP_RECV_PDU = 0,
1343f2304f8SSagi Grimberg NVME_TCP_RECV_DATA,
1353f2304f8SSagi Grimberg NVME_TCP_RECV_DDGST,
1363f2304f8SSagi Grimberg };
1373f2304f8SSagi Grimberg
1383f2304f8SSagi Grimberg struct nvme_tcp_ctrl;
1393f2304f8SSagi Grimberg struct nvme_tcp_queue {
1403f2304f8SSagi Grimberg struct socket *sock;
1413f2304f8SSagi Grimberg struct work_struct io_work;
1423f2304f8SSagi Grimberg int io_cpu;
1433f2304f8SSagi Grimberg
1449ebbfe49SChao Leng struct mutex queue_lock;
145db5ad6b7SSagi Grimberg struct mutex send_mutex;
14615ec928aSSagi Grimberg struct llist_head req_list;
1473f2304f8SSagi Grimberg struct list_head send_list;
1483f2304f8SSagi Grimberg
1493f2304f8SSagi Grimberg /* recv state */
1503f2304f8SSagi Grimberg void *pdu;
1513f2304f8SSagi Grimberg int pdu_remaining;
1523f2304f8SSagi Grimberg int pdu_offset;
1533f2304f8SSagi Grimberg size_t data_remaining;
1543f2304f8SSagi Grimberg size_t ddgst_remaining;
1551a9460ceSSagi Grimberg unsigned int nr_cqe;
1563f2304f8SSagi Grimberg
1573f2304f8SSagi Grimberg /* send state */
1583f2304f8SSagi Grimberg struct nvme_tcp_request *request;
1593f2304f8SSagi Grimberg
160c2700d28SVarun Prakash u32 maxh2cdata;
1613f2304f8SSagi Grimberg size_t cmnd_capsule_len;
1623f2304f8SSagi Grimberg struct nvme_tcp_ctrl *ctrl;
1633f2304f8SSagi Grimberg unsigned long flags;
1643f2304f8SSagi Grimberg bool rd_enabled;
1653f2304f8SSagi Grimberg
1663f2304f8SSagi Grimberg bool hdr_digest;
1673f2304f8SSagi Grimberg bool data_digest;
16836389576SHannes Reinecke bool tls_enabled;
1693f2304f8SSagi Grimberg struct ahash_request *rcv_hash;
1703f2304f8SSagi Grimberg struct ahash_request *snd_hash;
1713f2304f8SSagi Grimberg __le32 exp_ddgst;
1723f2304f8SSagi Grimberg __le32 recv_ddgst;
173be8e82caSHannes Reinecke struct completion tls_complete;
174be8e82caSHannes Reinecke int tls_err;
1753f2304f8SSagi Grimberg struct page_frag_cache pf_cache;
1763f2304f8SSagi Grimberg
1773f2304f8SSagi Grimberg void (*state_change)(struct sock *);
1783f2304f8SSagi Grimberg void (*data_ready)(struct sock *);
1793f2304f8SSagi Grimberg void (*write_space)(struct sock *);
1803f2304f8SSagi Grimberg };
1813f2304f8SSagi Grimberg
1823f2304f8SSagi Grimberg struct nvme_tcp_ctrl {
1833f2304f8SSagi Grimberg /* read only in the hot path */
1843f2304f8SSagi Grimberg struct nvme_tcp_queue *queues;
1853f2304f8SSagi Grimberg struct blk_mq_tag_set tag_set;
1863f2304f8SSagi Grimberg
1873f2304f8SSagi Grimberg /* other member variables */
1883f2304f8SSagi Grimberg struct list_head list;
1893f2304f8SSagi Grimberg struct blk_mq_tag_set admin_tag_set;
1903f2304f8SSagi Grimberg struct sockaddr_storage addr;
1913f2304f8SSagi Grimberg struct sockaddr_storage src_addr;
1923f2304f8SSagi Grimberg struct nvme_ctrl ctrl;
1933f2304f8SSagi Grimberg
1943f2304f8SSagi Grimberg struct work_struct err_work;
1953f2304f8SSagi Grimberg struct delayed_work connect_work;
1963f2304f8SSagi Grimberg struct nvme_tcp_request async_req;
19764861993SSagi Grimberg u32 io_queues[HCTX_MAX_TYPES];
1983f2304f8SSagi Grimberg };
1993f2304f8SSagi Grimberg
2003f2304f8SSagi Grimberg static LIST_HEAD(nvme_tcp_ctrl_list);
2013f2304f8SSagi Grimberg static DEFINE_MUTEX(nvme_tcp_ctrl_mutex);
2023f2304f8SSagi Grimberg static struct workqueue_struct *nvme_tcp_wq;
2036acbd961SRikard Falkeborn static const struct blk_mq_ops nvme_tcp_mq_ops;
2046acbd961SRikard Falkeborn static const struct blk_mq_ops nvme_tcp_admin_mq_ops;
205db5ad6b7SSagi Grimberg static int nvme_tcp_try_send(struct nvme_tcp_queue *queue);
2063f2304f8SSagi Grimberg
to_tcp_ctrl(struct nvme_ctrl * ctrl)2073f2304f8SSagi Grimberg static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl)
2083f2304f8SSagi Grimberg {
2093f2304f8SSagi Grimberg return container_of(ctrl, struct nvme_tcp_ctrl, ctrl);
2103f2304f8SSagi Grimberg }
2113f2304f8SSagi Grimberg
nvme_tcp_queue_id(struct nvme_tcp_queue * queue)2123f2304f8SSagi Grimberg static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
2133f2304f8SSagi Grimberg {
2143f2304f8SSagi Grimberg return queue - queue->ctrl->queues;
2153f2304f8SSagi Grimberg }
2163f2304f8SSagi Grimberg
21736389576SHannes Reinecke /*
21836389576SHannes Reinecke * Check if the queue is TLS encrypted
21936389576SHannes Reinecke */
nvme_tcp_queue_tls(struct nvme_tcp_queue * queue)22036389576SHannes Reinecke static inline bool nvme_tcp_queue_tls(struct nvme_tcp_queue *queue)
22136389576SHannes Reinecke {
22236389576SHannes Reinecke if (!IS_ENABLED(CONFIG_NVME_TCP_TLS))
22336389576SHannes Reinecke return 0;
22436389576SHannes Reinecke
22536389576SHannes Reinecke return queue->tls_enabled;
22636389576SHannes Reinecke }
22736389576SHannes Reinecke
22836389576SHannes Reinecke /*
22936389576SHannes Reinecke * Check if TLS is configured for the controller.
23036389576SHannes Reinecke */
nvme_tcp_tls_configured(struct nvme_ctrl * ctrl)23136389576SHannes Reinecke static inline bool nvme_tcp_tls_configured(struct nvme_ctrl *ctrl)
2320e6c4fe7SArnd Bergmann {
2330e6c4fe7SArnd Bergmann if (!IS_ENABLED(CONFIG_NVME_TCP_TLS))
2340e6c4fe7SArnd Bergmann return 0;
2350e6c4fe7SArnd Bergmann
2360e6c4fe7SArnd Bergmann return ctrl->opts->tls;
2370e6c4fe7SArnd Bergmann }
2380e6c4fe7SArnd Bergmann
nvme_tcp_tagset(struct nvme_tcp_queue * queue)2393f2304f8SSagi Grimberg static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue)
2403f2304f8SSagi Grimberg {
2413f2304f8SSagi Grimberg u32 queue_idx = nvme_tcp_queue_id(queue);
2423f2304f8SSagi Grimberg
2433f2304f8SSagi Grimberg if (queue_idx == 0)
2443f2304f8SSagi Grimberg return queue->ctrl->admin_tag_set.tags[queue_idx];
2453f2304f8SSagi Grimberg return queue->ctrl->tag_set.tags[queue_idx - 1];
2463f2304f8SSagi Grimberg }
2473f2304f8SSagi Grimberg
nvme_tcp_hdgst_len(struct nvme_tcp_queue * queue)2483f2304f8SSagi Grimberg static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue)
2493f2304f8SSagi Grimberg {
2503f2304f8SSagi Grimberg return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
2513f2304f8SSagi Grimberg }
2523f2304f8SSagi Grimberg
nvme_tcp_ddgst_len(struct nvme_tcp_queue * queue)2533f2304f8SSagi Grimberg static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue)
2543f2304f8SSagi Grimberg {
2553f2304f8SSagi Grimberg return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
2563f2304f8SSagi Grimberg }
2573f2304f8SSagi Grimberg
nvme_tcp_req_cmd_pdu(struct nvme_tcp_request * req)258a3406352SSagi Grimberg static inline void *nvme_tcp_req_cmd_pdu(struct nvme_tcp_request *req)
259a3406352SSagi Grimberg {
260a3406352SSagi Grimberg return req->pdu;
261a3406352SSagi Grimberg }
262a3406352SSagi Grimberg
nvme_tcp_req_data_pdu(struct nvme_tcp_request * req)263a3406352SSagi Grimberg static inline void *nvme_tcp_req_data_pdu(struct nvme_tcp_request *req)
264a3406352SSagi Grimberg {
265a3406352SSagi Grimberg /* use the pdu space in the back for the data pdu */
266a3406352SSagi Grimberg return req->pdu + sizeof(struct nvme_tcp_cmd_pdu) -
267a3406352SSagi Grimberg sizeof(struct nvme_tcp_data_pdu);
268a3406352SSagi Grimberg }
269a3406352SSagi Grimberg
nvme_tcp_inline_data_size(struct nvme_tcp_request * req)27053ee9e29SCaleb Sander static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_request *req)
2713f2304f8SSagi Grimberg {
27253ee9e29SCaleb Sander if (nvme_is_fabrics(req->req.cmd))
27353ee9e29SCaleb Sander return NVME_TCP_ADMIN_CCSZ;
27453ee9e29SCaleb Sander return req->queue->cmnd_capsule_len - sizeof(struct nvme_command);
2753f2304f8SSagi Grimberg }
2763f2304f8SSagi Grimberg
nvme_tcp_async_req(struct nvme_tcp_request * req)2773f2304f8SSagi Grimberg static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req)
2783f2304f8SSagi Grimberg {
2793f2304f8SSagi Grimberg return req == &req->queue->ctrl->async_req;
2803f2304f8SSagi Grimberg }
2813f2304f8SSagi Grimberg
nvme_tcp_has_inline_data(struct nvme_tcp_request * req)2823f2304f8SSagi Grimberg static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req)
2833f2304f8SSagi Grimberg {
2843f2304f8SSagi Grimberg struct request *rq;
2853f2304f8SSagi Grimberg
2863f2304f8SSagi Grimberg if (unlikely(nvme_tcp_async_req(req)))
2873f2304f8SSagi Grimberg return false; /* async events don't have a request */
2883f2304f8SSagi Grimberg
2893f2304f8SSagi Grimberg rq = blk_mq_rq_from_pdu(req);
2903f2304f8SSagi Grimberg
29125e5cb78SSagi Grimberg return rq_data_dir(rq) == WRITE && req->data_len &&
29253ee9e29SCaleb Sander req->data_len <= nvme_tcp_inline_data_size(req);
2933f2304f8SSagi Grimberg }
2943f2304f8SSagi Grimberg
nvme_tcp_req_cur_page(struct nvme_tcp_request * req)2953f2304f8SSagi Grimberg static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req)
2963f2304f8SSagi Grimberg {
2973f2304f8SSagi Grimberg return req->iter.bvec->bv_page;
2983f2304f8SSagi Grimberg }
2993f2304f8SSagi Grimberg
nvme_tcp_req_cur_offset(struct nvme_tcp_request * req)3003f2304f8SSagi Grimberg static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req)
3013f2304f8SSagi Grimberg {
3023f2304f8SSagi Grimberg return req->iter.bvec->bv_offset + req->iter.iov_offset;
3033f2304f8SSagi Grimberg }
3043f2304f8SSagi Grimberg
nvme_tcp_req_cur_length(struct nvme_tcp_request * req)3053f2304f8SSagi Grimberg static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req)
3063f2304f8SSagi Grimberg {
307ca1ff67dSSagi Grimberg return min_t(size_t, iov_iter_single_seg_count(&req->iter),
3083f2304f8SSagi Grimberg req->pdu_len - req->pdu_sent);
3093f2304f8SSagi Grimberg }
3103f2304f8SSagi Grimberg
nvme_tcp_pdu_data_left(struct nvme_tcp_request * req)3113f2304f8SSagi Grimberg static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req)
3123f2304f8SSagi Grimberg {
3133f2304f8SSagi Grimberg return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ?
3143f2304f8SSagi Grimberg req->pdu_len - req->pdu_sent : 0;
3153f2304f8SSagi Grimberg }
3163f2304f8SSagi Grimberg
nvme_tcp_pdu_last_send(struct nvme_tcp_request * req,int len)3173f2304f8SSagi Grimberg static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
3183f2304f8SSagi Grimberg int len)
3193f2304f8SSagi Grimberg {
3203f2304f8SSagi Grimberg return nvme_tcp_pdu_data_left(req) <= len;
3213f2304f8SSagi Grimberg }
3223f2304f8SSagi Grimberg
nvme_tcp_init_iter(struct nvme_tcp_request * req,unsigned int dir)3233f2304f8SSagi Grimberg static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
3243f2304f8SSagi Grimberg unsigned int dir)
3253f2304f8SSagi Grimberg {
3263f2304f8SSagi Grimberg struct request *rq = blk_mq_rq_from_pdu(req);
3273f2304f8SSagi Grimberg struct bio_vec *vec;
3283f2304f8SSagi Grimberg unsigned int size;
3290dc9edafSSagi Grimberg int nr_bvec;
3303f2304f8SSagi Grimberg size_t offset;
3313f2304f8SSagi Grimberg
3323f2304f8SSagi Grimberg if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
3333f2304f8SSagi Grimberg vec = &rq->special_vec;
3340dc9edafSSagi Grimberg nr_bvec = 1;
3353f2304f8SSagi Grimberg size = blk_rq_payload_bytes(rq);
3363f2304f8SSagi Grimberg offset = 0;
3373f2304f8SSagi Grimberg } else {
3383f2304f8SSagi Grimberg struct bio *bio = req->curr_bio;
3390dc9edafSSagi Grimberg struct bvec_iter bi;
3400dc9edafSSagi Grimberg struct bio_vec bv;
3413f2304f8SSagi Grimberg
3423f2304f8SSagi Grimberg vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
3430dc9edafSSagi Grimberg nr_bvec = 0;
3440dc9edafSSagi Grimberg bio_for_each_bvec(bv, bio, bi) {
3450dc9edafSSagi Grimberg nr_bvec++;
3460dc9edafSSagi Grimberg }
3473f2304f8SSagi Grimberg size = bio->bi_iter.bi_size;
3483f2304f8SSagi Grimberg offset = bio->bi_iter.bi_bvec_done;
3493f2304f8SSagi Grimberg }
3503f2304f8SSagi Grimberg
3510dc9edafSSagi Grimberg iov_iter_bvec(&req->iter, dir, vec, nr_bvec, size);
3523f2304f8SSagi Grimberg req->iter.iov_offset = offset;
3533f2304f8SSagi Grimberg }
3543f2304f8SSagi Grimberg
nvme_tcp_advance_req(struct nvme_tcp_request * req,int len)3553f2304f8SSagi Grimberg static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req,
3563f2304f8SSagi Grimberg int len)
3573f2304f8SSagi Grimberg {
3583f2304f8SSagi Grimberg req->data_sent += len;
3593f2304f8SSagi Grimberg req->pdu_sent += len;
3603f2304f8SSagi Grimberg iov_iter_advance(&req->iter, len);
3613f2304f8SSagi Grimberg if (!iov_iter_count(&req->iter) &&
3623f2304f8SSagi Grimberg req->data_sent < req->data_len) {
3633f2304f8SSagi Grimberg req->curr_bio = req->curr_bio->bi_next;
364de4eda9dSAl Viro nvme_tcp_init_iter(req, ITER_SOURCE);
3653f2304f8SSagi Grimberg }
3663f2304f8SSagi Grimberg }
3673f2304f8SSagi Grimberg
nvme_tcp_send_all(struct nvme_tcp_queue * queue)3685c11f7d9SSagi Grimberg static inline void nvme_tcp_send_all(struct nvme_tcp_queue *queue)
3695c11f7d9SSagi Grimberg {
3705c11f7d9SSagi Grimberg int ret;
3715c11f7d9SSagi Grimberg
3725c11f7d9SSagi Grimberg /* drain the send queue as much as we can... */
3735c11f7d9SSagi Grimberg do {
3745c11f7d9SSagi Grimberg ret = nvme_tcp_try_send(queue);
3755c11f7d9SSagi Grimberg } while (ret > 0);
3765c11f7d9SSagi Grimberg }
3775c11f7d9SSagi Grimberg
nvme_tcp_queue_has_pending(struct nvme_tcp_queue * queue)37850abcc17SHannes Reinecke static inline bool nvme_tcp_queue_has_pending(struct nvme_tcp_queue *queue)
37970f437fbSKeith Busch {
38070f437fbSKeith Busch return !list_empty(&queue->send_list) ||
3813770a42bSSagi Grimberg !llist_empty(&queue->req_list);
38270f437fbSKeith Busch }
38370f437fbSKeith Busch
nvme_tcp_queue_more(struct nvme_tcp_queue * queue)38450abcc17SHannes Reinecke static inline bool nvme_tcp_queue_more(struct nvme_tcp_queue *queue)
38550abcc17SHannes Reinecke {
38636389576SHannes Reinecke return !nvme_tcp_queue_tls(queue) &&
38750abcc17SHannes Reinecke nvme_tcp_queue_has_pending(queue);
38850abcc17SHannes Reinecke }
38950abcc17SHannes Reinecke
nvme_tcp_queue_request(struct nvme_tcp_request * req,bool sync,bool last)390db5ad6b7SSagi Grimberg static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req,
39186f0348aSSagi Grimberg bool sync, bool last)
3923f2304f8SSagi Grimberg {
3933f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = req->queue;
394db5ad6b7SSagi Grimberg bool empty;
3953f2304f8SSagi Grimberg
39615ec928aSSagi Grimberg empty = llist_add(&req->lentry, &queue->req_list) &&
39715ec928aSSagi Grimberg list_empty(&queue->send_list) && !queue->request;
3983f2304f8SSagi Grimberg
399db5ad6b7SSagi Grimberg /*
400db5ad6b7SSagi Grimberg * if we're the first on the send_list and we can try to send
401db5ad6b7SSagi Grimberg * directly, otherwise queue io_work. Also, only do that if we
402db5ad6b7SSagi Grimberg * are on the same cpu, so we don't introduce contention.
403db5ad6b7SSagi Grimberg */
404bb833370SSagi Grimberg if (queue->io_cpu == raw_smp_processor_id() &&
405db5ad6b7SSagi Grimberg sync && empty && mutex_trylock(&queue->send_mutex)) {
4065c11f7d9SSagi Grimberg nvme_tcp_send_all(queue);
407db5ad6b7SSagi Grimberg mutex_unlock(&queue->send_mutex);
4083f2304f8SSagi Grimberg }
40970f437fbSKeith Busch
41050abcc17SHannes Reinecke if (last && nvme_tcp_queue_has_pending(queue))
41170f437fbSKeith Busch queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
412db5ad6b7SSagi Grimberg }
4133f2304f8SSagi Grimberg
nvme_tcp_process_req_list(struct nvme_tcp_queue * queue)41415ec928aSSagi Grimberg static void nvme_tcp_process_req_list(struct nvme_tcp_queue *queue)
41515ec928aSSagi Grimberg {
41615ec928aSSagi Grimberg struct nvme_tcp_request *req;
41715ec928aSSagi Grimberg struct llist_node *node;
41815ec928aSSagi Grimberg
41915ec928aSSagi Grimberg for (node = llist_del_all(&queue->req_list); node; node = node->next) {
42015ec928aSSagi Grimberg req = llist_entry(node, struct nvme_tcp_request, lentry);
42115ec928aSSagi Grimberg list_add(&req->entry, &queue->send_list);
42215ec928aSSagi Grimberg }
42315ec928aSSagi Grimberg }
42415ec928aSSagi Grimberg
4253f2304f8SSagi Grimberg static inline struct nvme_tcp_request *
nvme_tcp_fetch_request(struct nvme_tcp_queue * queue)4263f2304f8SSagi Grimberg nvme_tcp_fetch_request(struct nvme_tcp_queue *queue)
4273f2304f8SSagi Grimberg {
4283f2304f8SSagi Grimberg struct nvme_tcp_request *req;
4293f2304f8SSagi Grimberg
4303f2304f8SSagi Grimberg req = list_first_entry_or_null(&queue->send_list,
4313f2304f8SSagi Grimberg struct nvme_tcp_request, entry);
43215ec928aSSagi Grimberg if (!req) {
43315ec928aSSagi Grimberg nvme_tcp_process_req_list(queue);
43415ec928aSSagi Grimberg req = list_first_entry_or_null(&queue->send_list,
43515ec928aSSagi Grimberg struct nvme_tcp_request, entry);
43615ec928aSSagi Grimberg if (unlikely(!req))
43715ec928aSSagi Grimberg return NULL;
43815ec928aSSagi Grimberg }
4393f2304f8SSagi Grimberg
44015ec928aSSagi Grimberg list_del(&req->entry);
4413f2304f8SSagi Grimberg return req;
4423f2304f8SSagi Grimberg }
4433f2304f8SSagi Grimberg
nvme_tcp_ddgst_final(struct ahash_request * hash,__le32 * dgst)444a7273d40SChristoph Hellwig static inline void nvme_tcp_ddgst_final(struct ahash_request *hash,
445a7273d40SChristoph Hellwig __le32 *dgst)
4463f2304f8SSagi Grimberg {
4473f2304f8SSagi Grimberg ahash_request_set_crypt(hash, NULL, (u8 *)dgst, 0);
4483f2304f8SSagi Grimberg crypto_ahash_final(hash);
4493f2304f8SSagi Grimberg }
4503f2304f8SSagi Grimberg
nvme_tcp_ddgst_update(struct ahash_request * hash,struct page * page,off_t off,size_t len)4513f2304f8SSagi Grimberg static inline void nvme_tcp_ddgst_update(struct ahash_request *hash,
4523f2304f8SSagi Grimberg struct page *page, off_t off, size_t len)
4533f2304f8SSagi Grimberg {
4543f2304f8SSagi Grimberg struct scatterlist sg;
4553f2304f8SSagi Grimberg
4565fa9add6SNam Cao sg_init_table(&sg, 1);
4573f2304f8SSagi Grimberg sg_set_page(&sg, page, len, off);
4583f2304f8SSagi Grimberg ahash_request_set_crypt(hash, &sg, NULL, len);
4593f2304f8SSagi Grimberg crypto_ahash_update(hash);
4603f2304f8SSagi Grimberg }
4613f2304f8SSagi Grimberg
nvme_tcp_hdgst(struct ahash_request * hash,void * pdu,size_t len)4623f2304f8SSagi Grimberg static inline void nvme_tcp_hdgst(struct ahash_request *hash,
4633f2304f8SSagi Grimberg void *pdu, size_t len)
4643f2304f8SSagi Grimberg {
4653f2304f8SSagi Grimberg struct scatterlist sg;
4663f2304f8SSagi Grimberg
4673f2304f8SSagi Grimberg sg_init_one(&sg, pdu, len);
4683f2304f8SSagi Grimberg ahash_request_set_crypt(hash, &sg, pdu + len, len);
4693f2304f8SSagi Grimberg crypto_ahash_digest(hash);
4703f2304f8SSagi Grimberg }
4713f2304f8SSagi Grimberg
nvme_tcp_verify_hdgst(struct nvme_tcp_queue * queue,void * pdu,size_t pdu_len)4723f2304f8SSagi Grimberg static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue,
4733f2304f8SSagi Grimberg void *pdu, size_t pdu_len)
4743f2304f8SSagi Grimberg {
4753f2304f8SSagi Grimberg struct nvme_tcp_hdr *hdr = pdu;
4763f2304f8SSagi Grimberg __le32 recv_digest;
4773f2304f8SSagi Grimberg __le32 exp_digest;
4783f2304f8SSagi Grimberg
4793f2304f8SSagi Grimberg if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
4803f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
4813f2304f8SSagi Grimberg "queue %d: header digest flag is cleared\n",
4823f2304f8SSagi Grimberg nvme_tcp_queue_id(queue));
4833f2304f8SSagi Grimberg return -EPROTO;
4843f2304f8SSagi Grimberg }
4853f2304f8SSagi Grimberg
4863f2304f8SSagi Grimberg recv_digest = *(__le32 *)(pdu + hdr->hlen);
4873f2304f8SSagi Grimberg nvme_tcp_hdgst(queue->rcv_hash, pdu, pdu_len);
4883f2304f8SSagi Grimberg exp_digest = *(__le32 *)(pdu + hdr->hlen);
4893f2304f8SSagi Grimberg if (recv_digest != exp_digest) {
4903f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
4913f2304f8SSagi Grimberg "header digest error: recv %#x expected %#x\n",
4923f2304f8SSagi Grimberg le32_to_cpu(recv_digest), le32_to_cpu(exp_digest));
4933f2304f8SSagi Grimberg return -EIO;
4943f2304f8SSagi Grimberg }
4953f2304f8SSagi Grimberg
4963f2304f8SSagi Grimberg return 0;
4973f2304f8SSagi Grimberg }
4983f2304f8SSagi Grimberg
nvme_tcp_check_ddgst(struct nvme_tcp_queue * queue,void * pdu)4993f2304f8SSagi Grimberg static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu)
5003f2304f8SSagi Grimberg {
5013f2304f8SSagi Grimberg struct nvme_tcp_hdr *hdr = pdu;
5023f2304f8SSagi Grimberg u8 digest_len = nvme_tcp_hdgst_len(queue);
5033f2304f8SSagi Grimberg u32 len;
5043f2304f8SSagi Grimberg
5053f2304f8SSagi Grimberg len = le32_to_cpu(hdr->plen) - hdr->hlen -
5063f2304f8SSagi Grimberg ((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0);
5073f2304f8SSagi Grimberg
5083f2304f8SSagi Grimberg if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
5093f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
5103f2304f8SSagi Grimberg "queue %d: data digest flag is cleared\n",
5113f2304f8SSagi Grimberg nvme_tcp_queue_id(queue));
5123f2304f8SSagi Grimberg return -EPROTO;
5133f2304f8SSagi Grimberg }
5143f2304f8SSagi Grimberg crypto_ahash_init(queue->rcv_hash);
5153f2304f8SSagi Grimberg
5163f2304f8SSagi Grimberg return 0;
5173f2304f8SSagi Grimberg }
5183f2304f8SSagi Grimberg
nvme_tcp_exit_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx)5193f2304f8SSagi Grimberg static void nvme_tcp_exit_request(struct blk_mq_tag_set *set,
5203f2304f8SSagi Grimberg struct request *rq, unsigned int hctx_idx)
5213f2304f8SSagi Grimberg {
5223f2304f8SSagi Grimberg struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
5233f2304f8SSagi Grimberg
5243f2304f8SSagi Grimberg page_frag_free(req->pdu);
5253f2304f8SSagi Grimberg }
5263f2304f8SSagi Grimberg
nvme_tcp_init_request(struct blk_mq_tag_set * set,struct request * rq,unsigned int hctx_idx,unsigned int numa_node)5273f2304f8SSagi Grimberg static int nvme_tcp_init_request(struct blk_mq_tag_set *set,
5283f2304f8SSagi Grimberg struct request *rq, unsigned int hctx_idx,
5293f2304f8SSagi Grimberg unsigned int numa_node)
5303f2304f8SSagi Grimberg {
53106427ca0SChristoph Hellwig struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data);
5323f2304f8SSagi Grimberg struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
533f4b9e6c9SKeith Busch struct nvme_tcp_cmd_pdu *pdu;
5343f2304f8SSagi Grimberg int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
5353f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx];
5363f2304f8SSagi Grimberg u8 hdgst = nvme_tcp_hdgst_len(queue);
5373f2304f8SSagi Grimberg
5383f2304f8SSagi Grimberg req->pdu = page_frag_alloc(&queue->pf_cache,
5393f2304f8SSagi Grimberg sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
5403f2304f8SSagi Grimberg GFP_KERNEL | __GFP_ZERO);
5413f2304f8SSagi Grimberg if (!req->pdu)
5423f2304f8SSagi Grimberg return -ENOMEM;
5433f2304f8SSagi Grimberg
544f4b9e6c9SKeith Busch pdu = req->pdu;
5453f2304f8SSagi Grimberg req->queue = queue;
5463f2304f8SSagi Grimberg nvme_req(rq)->ctrl = &ctrl->ctrl;
547f4b9e6c9SKeith Busch nvme_req(rq)->cmd = &pdu->cmd;
5483f2304f8SSagi Grimberg
5493f2304f8SSagi Grimberg return 0;
5503f2304f8SSagi Grimberg }
5513f2304f8SSagi Grimberg
nvme_tcp_init_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)5523f2304f8SSagi Grimberg static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
5533f2304f8SSagi Grimberg unsigned int hctx_idx)
5543f2304f8SSagi Grimberg {
55506427ca0SChristoph Hellwig struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(data);
5563f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1];
5573f2304f8SSagi Grimberg
5583f2304f8SSagi Grimberg hctx->driver_data = queue;
5593f2304f8SSagi Grimberg return 0;
5603f2304f8SSagi Grimberg }
5613f2304f8SSagi Grimberg
nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx * hctx,void * data,unsigned int hctx_idx)5623f2304f8SSagi Grimberg static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
5633f2304f8SSagi Grimberg unsigned int hctx_idx)
5643f2304f8SSagi Grimberg {
56506427ca0SChristoph Hellwig struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(data);
5663f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = &ctrl->queues[0];
5673f2304f8SSagi Grimberg
5683f2304f8SSagi Grimberg hctx->driver_data = queue;
5693f2304f8SSagi Grimberg return 0;
5703f2304f8SSagi Grimberg }
5713f2304f8SSagi Grimberg
5723f2304f8SSagi Grimberg static enum nvme_tcp_recv_state
nvme_tcp_recv_state(struct nvme_tcp_queue * queue)5733f2304f8SSagi Grimberg nvme_tcp_recv_state(struct nvme_tcp_queue *queue)
5743f2304f8SSagi Grimberg {
5753f2304f8SSagi Grimberg return (queue->pdu_remaining) ? NVME_TCP_RECV_PDU :
5763f2304f8SSagi Grimberg (queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST :
5773f2304f8SSagi Grimberg NVME_TCP_RECV_DATA;
5783f2304f8SSagi Grimberg }
5793f2304f8SSagi Grimberg
nvme_tcp_init_recv_ctx(struct nvme_tcp_queue * queue)5803f2304f8SSagi Grimberg static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
5813f2304f8SSagi Grimberg {
5823f2304f8SSagi Grimberg queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) +
5833f2304f8SSagi Grimberg nvme_tcp_hdgst_len(queue);
5843f2304f8SSagi Grimberg queue->pdu_offset = 0;
5853f2304f8SSagi Grimberg queue->data_remaining = -1;
5863f2304f8SSagi Grimberg queue->ddgst_remaining = 0;
5873f2304f8SSagi Grimberg }
5883f2304f8SSagi Grimberg
nvme_tcp_error_recovery(struct nvme_ctrl * ctrl)5893f2304f8SSagi Grimberg static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
5903f2304f8SSagi Grimberg {
5913f2304f8SSagi Grimberg if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
5923f2304f8SSagi Grimberg return;
5933f2304f8SSagi Grimberg
594236187c4SSagi Grimberg dev_warn(ctrl->device, "starting error recovery\n");
59597b2512aSNigel Kirkland queue_work(nvme_reset_wq, &to_tcp_ctrl(ctrl)->err_work);
5963f2304f8SSagi Grimberg }
5973f2304f8SSagi Grimberg
nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue * queue,struct nvme_completion * cqe)5983f2304f8SSagi Grimberg static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue,
5993f2304f8SSagi Grimberg struct nvme_completion *cqe)
6003f2304f8SSagi Grimberg {
6011ba2e507SDaniel Wagner struct nvme_tcp_request *req;
6023f2304f8SSagi Grimberg struct request *rq;
6033f2304f8SSagi Grimberg
604e7006de6SSagi Grimberg rq = nvme_find_rq(nvme_tcp_tagset(queue), cqe->command_id);
6053f2304f8SSagi Grimberg if (!rq) {
6063f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
607e7006de6SSagi Grimberg "got bad cqe.command_id %#x on queue %d\n",
608e7006de6SSagi Grimberg cqe->command_id, nvme_tcp_queue_id(queue));
6093f2304f8SSagi Grimberg nvme_tcp_error_recovery(&queue->ctrl->ctrl);
6103f2304f8SSagi Grimberg return -EINVAL;
6113f2304f8SSagi Grimberg }
6123f2304f8SSagi Grimberg
6131ba2e507SDaniel Wagner req = blk_mq_rq_to_pdu(rq);
6141ba2e507SDaniel Wagner if (req->status == cpu_to_le16(NVME_SC_SUCCESS))
6151ba2e507SDaniel Wagner req->status = cqe->status;
6161ba2e507SDaniel Wagner
6171ba2e507SDaniel Wagner if (!nvme_try_complete_req(rq, req->status, cqe->result))
618ff029451SChristoph Hellwig nvme_complete_rq(rq);
6191a9460ceSSagi Grimberg queue->nr_cqe++;
6203f2304f8SSagi Grimberg
6213f2304f8SSagi Grimberg return 0;
6223f2304f8SSagi Grimberg }
6233f2304f8SSagi Grimberg
nvme_tcp_handle_c2h_data(struct nvme_tcp_queue * queue,struct nvme_tcp_data_pdu * pdu)6243f2304f8SSagi Grimberg static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
6253f2304f8SSagi Grimberg struct nvme_tcp_data_pdu *pdu)
6263f2304f8SSagi Grimberg {
6273f2304f8SSagi Grimberg struct request *rq;
6283f2304f8SSagi Grimberg
629e7006de6SSagi Grimberg rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
6303f2304f8SSagi Grimberg if (!rq) {
6313f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
632e7006de6SSagi Grimberg "got bad c2hdata.command_id %#x on queue %d\n",
633e7006de6SSagi Grimberg pdu->command_id, nvme_tcp_queue_id(queue));
6343f2304f8SSagi Grimberg return -ENOENT;
6353f2304f8SSagi Grimberg }
6363f2304f8SSagi Grimberg
6373f2304f8SSagi Grimberg if (!blk_rq_payload_bytes(rq)) {
6383f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
6393f2304f8SSagi Grimberg "queue %d tag %#x unexpected data\n",
6403f2304f8SSagi Grimberg nvme_tcp_queue_id(queue), rq->tag);
6413f2304f8SSagi Grimberg return -EIO;
6423f2304f8SSagi Grimberg }
6433f2304f8SSagi Grimberg
6443f2304f8SSagi Grimberg queue->data_remaining = le32_to_cpu(pdu->data_length);
6453f2304f8SSagi Grimberg
646602d674cSSagi Grimberg if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS &&
647602d674cSSagi Grimberg unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) {
648602d674cSSagi Grimberg dev_err(queue->ctrl->ctrl.device,
649602d674cSSagi Grimberg "queue %d tag %#x SUCCESS set but not last PDU\n",
650602d674cSSagi Grimberg nvme_tcp_queue_id(queue), rq->tag);
651602d674cSSagi Grimberg nvme_tcp_error_recovery(&queue->ctrl->ctrl);
652602d674cSSagi Grimberg return -EPROTO;
653602d674cSSagi Grimberg }
654602d674cSSagi Grimberg
6553f2304f8SSagi Grimberg return 0;
6563f2304f8SSagi Grimberg }
6573f2304f8SSagi Grimberg
nvme_tcp_handle_comp(struct nvme_tcp_queue * queue,struct nvme_tcp_rsp_pdu * pdu)6583f2304f8SSagi Grimberg static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue,
6593f2304f8SSagi Grimberg struct nvme_tcp_rsp_pdu *pdu)
6603f2304f8SSagi Grimberg {
6613f2304f8SSagi Grimberg struct nvme_completion *cqe = &pdu->cqe;
6623f2304f8SSagi Grimberg int ret = 0;
6633f2304f8SSagi Grimberg
6643f2304f8SSagi Grimberg /*
6653f2304f8SSagi Grimberg * AEN requests are special as they don't time out and can
6663f2304f8SSagi Grimberg * survive any kind of queue freeze and often don't respond to
6673f2304f8SSagi Grimberg * aborts. We don't even bother to allocate a struct request
6683f2304f8SSagi Grimberg * for them but rather special case them here.
6693f2304f8SSagi Grimberg */
67058a8df67SIsrael Rukshin if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue),
67158a8df67SIsrael Rukshin cqe->command_id)))
6723f2304f8SSagi Grimberg nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
6733f2304f8SSagi Grimberg &cqe->result);
6743f2304f8SSagi Grimberg else
6753f2304f8SSagi Grimberg ret = nvme_tcp_process_nvme_cqe(queue, cqe);
6763f2304f8SSagi Grimberg
6773f2304f8SSagi Grimberg return ret;
6783f2304f8SSagi Grimberg }
6793f2304f8SSagi Grimberg
nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request * req)680c2700d28SVarun Prakash static void nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req)
6813f2304f8SSagi Grimberg {
682a3406352SSagi Grimberg struct nvme_tcp_data_pdu *data = nvme_tcp_req_data_pdu(req);
6833f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = req->queue;
6843f2304f8SSagi Grimberg struct request *rq = blk_mq_rq_from_pdu(req);
685c2700d28SVarun Prakash u32 h2cdata_sent = req->pdu_len;
6863f2304f8SSagi Grimberg u8 hdgst = nvme_tcp_hdgst_len(queue);
6873f2304f8SSagi Grimberg u8 ddgst = nvme_tcp_ddgst_len(queue);
6883f2304f8SSagi Grimberg
6891d3ef9c3SVarun Prakash req->state = NVME_TCP_SEND_H2C_PDU;
6901d3ef9c3SVarun Prakash req->offset = 0;
691c2700d28SVarun Prakash req->pdu_len = min(req->h2cdata_left, queue->maxh2cdata);
6923f2304f8SSagi Grimberg req->pdu_sent = 0;
693c2700d28SVarun Prakash req->h2cdata_left -= req->pdu_len;
694c2700d28SVarun Prakash req->h2cdata_offset += h2cdata_sent;
6953f2304f8SSagi Grimberg
6963f2304f8SSagi Grimberg memset(data, 0, sizeof(*data));
6973f2304f8SSagi Grimberg data->hdr.type = nvme_tcp_h2c_data;
698c2700d28SVarun Prakash if (!req->h2cdata_left)
6993f2304f8SSagi Grimberg data->hdr.flags = NVME_TCP_F_DATA_LAST;
7003f2304f8SSagi Grimberg if (queue->hdr_digest)
7013f2304f8SSagi Grimberg data->hdr.flags |= NVME_TCP_F_HDGST;
7023f2304f8SSagi Grimberg if (queue->data_digest)
7033f2304f8SSagi Grimberg data->hdr.flags |= NVME_TCP_F_DDGST;
7043f2304f8SSagi Grimberg data->hdr.hlen = sizeof(*data);
7053f2304f8SSagi Grimberg data->hdr.pdo = data->hdr.hlen + hdgst;
7063f2304f8SSagi Grimberg data->hdr.plen =
7073f2304f8SSagi Grimberg cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst);
708c2700d28SVarun Prakash data->ttag = req->ttag;
709e7006de6SSagi Grimberg data->command_id = nvme_cid(rq);
710c2700d28SVarun Prakash data->data_offset = cpu_to_le32(req->h2cdata_offset);
7113f2304f8SSagi Grimberg data->data_length = cpu_to_le32(req->pdu_len);
7123f2304f8SSagi Grimberg }
7133f2304f8SSagi Grimberg
nvme_tcp_handle_r2t(struct nvme_tcp_queue * queue,struct nvme_tcp_r2t_pdu * pdu)7143f2304f8SSagi Grimberg static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
7153f2304f8SSagi Grimberg struct nvme_tcp_r2t_pdu *pdu)
7163f2304f8SSagi Grimberg {
7173f2304f8SSagi Grimberg struct nvme_tcp_request *req;
7183f2304f8SSagi Grimberg struct request *rq;
7191d3ef9c3SVarun Prakash u32 r2t_length = le32_to_cpu(pdu->r2t_length);
720c2700d28SVarun Prakash u32 r2t_offset = le32_to_cpu(pdu->r2t_offset);
7213f2304f8SSagi Grimberg
722e7006de6SSagi Grimberg rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
7233f2304f8SSagi Grimberg if (!rq) {
7243f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
725e7006de6SSagi Grimberg "got bad r2t.command_id %#x on queue %d\n",
726e7006de6SSagi Grimberg pdu->command_id, nvme_tcp_queue_id(queue));
7273f2304f8SSagi Grimberg return -ENOENT;
7283f2304f8SSagi Grimberg }
7293f2304f8SSagi Grimberg req = blk_mq_rq_to_pdu(rq);
7303f2304f8SSagi Grimberg
7311d3ef9c3SVarun Prakash if (unlikely(!r2t_length)) {
7321d3ef9c3SVarun Prakash dev_err(queue->ctrl->ctrl.device,
7331d3ef9c3SVarun Prakash "req %d r2t len is %u, probably a bug...\n",
7341d3ef9c3SVarun Prakash rq->tag, r2t_length);
7351d3ef9c3SVarun Prakash return -EPROTO;
7361d3ef9c3SVarun Prakash }
7373f2304f8SSagi Grimberg
7381d3ef9c3SVarun Prakash if (unlikely(req->data_sent + r2t_length > req->data_len)) {
7391d3ef9c3SVarun Prakash dev_err(queue->ctrl->ctrl.device,
7401d3ef9c3SVarun Prakash "req %d r2t len %u exceeded data len %u (%zu sent)\n",
7411d3ef9c3SVarun Prakash rq->tag, r2t_length, req->data_len, req->data_sent);
7421d3ef9c3SVarun Prakash return -EPROTO;
7431d3ef9c3SVarun Prakash }
7443f2304f8SSagi Grimberg
745c2700d28SVarun Prakash if (unlikely(r2t_offset < req->data_sent)) {
7461d3ef9c3SVarun Prakash dev_err(queue->ctrl->ctrl.device,
7471d3ef9c3SVarun Prakash "req %d unexpected r2t offset %u (expected %zu)\n",
748c2700d28SVarun Prakash rq->tag, r2t_offset, req->data_sent);
7491d3ef9c3SVarun Prakash return -EPROTO;
7501d3ef9c3SVarun Prakash }
7511d3ef9c3SVarun Prakash
752c2700d28SVarun Prakash req->pdu_len = 0;
753c2700d28SVarun Prakash req->h2cdata_left = r2t_length;
754c2700d28SVarun Prakash req->h2cdata_offset = r2t_offset;
755c2700d28SVarun Prakash req->ttag = pdu->ttag;
756c2700d28SVarun Prakash
757c2700d28SVarun Prakash nvme_tcp_setup_h2c_data_pdu(req);
75886f0348aSSagi Grimberg nvme_tcp_queue_request(req, false, true);
7593f2304f8SSagi Grimberg
7603f2304f8SSagi Grimberg return 0;
7613f2304f8SSagi Grimberg }
7623f2304f8SSagi Grimberg
nvme_tcp_recv_pdu(struct nvme_tcp_queue * queue,struct sk_buff * skb,unsigned int * offset,size_t * len)7633f2304f8SSagi Grimberg static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
7643f2304f8SSagi Grimberg unsigned int *offset, size_t *len)
7653f2304f8SSagi Grimberg {
7663f2304f8SSagi Grimberg struct nvme_tcp_hdr *hdr;
7673f2304f8SSagi Grimberg char *pdu = queue->pdu;
7683f2304f8SSagi Grimberg size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
7693f2304f8SSagi Grimberg int ret;
7703f2304f8SSagi Grimberg
7713f2304f8SSagi Grimberg ret = skb_copy_bits(skb, *offset,
7723f2304f8SSagi Grimberg &pdu[queue->pdu_offset], rcv_len);
7733f2304f8SSagi Grimberg if (unlikely(ret))
7743f2304f8SSagi Grimberg return ret;
7753f2304f8SSagi Grimberg
7763f2304f8SSagi Grimberg queue->pdu_remaining -= rcv_len;
7773f2304f8SSagi Grimberg queue->pdu_offset += rcv_len;
7783f2304f8SSagi Grimberg *offset += rcv_len;
7793f2304f8SSagi Grimberg *len -= rcv_len;
7803f2304f8SSagi Grimberg if (queue->pdu_remaining)
7813f2304f8SSagi Grimberg return 0;
7823f2304f8SSagi Grimberg
7833f2304f8SSagi Grimberg hdr = queue->pdu;
7843f2304f8SSagi Grimberg if (queue->hdr_digest) {
7853f2304f8SSagi Grimberg ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen);
7863f2304f8SSagi Grimberg if (unlikely(ret))
7873f2304f8SSagi Grimberg return ret;
7883f2304f8SSagi Grimberg }
7893f2304f8SSagi Grimberg
7903f2304f8SSagi Grimberg
7913f2304f8SSagi Grimberg if (queue->data_digest) {
7923f2304f8SSagi Grimberg ret = nvme_tcp_check_ddgst(queue, queue->pdu);
7933f2304f8SSagi Grimberg if (unlikely(ret))
7943f2304f8SSagi Grimberg return ret;
7953f2304f8SSagi Grimberg }
7963f2304f8SSagi Grimberg
7973f2304f8SSagi Grimberg switch (hdr->type) {
7983f2304f8SSagi Grimberg case nvme_tcp_c2h_data:
7996be18260SSagi Grimberg return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu);
8003f2304f8SSagi Grimberg case nvme_tcp_rsp:
8013f2304f8SSagi Grimberg nvme_tcp_init_recv_ctx(queue);
8026be18260SSagi Grimberg return nvme_tcp_handle_comp(queue, (void *)queue->pdu);
8033f2304f8SSagi Grimberg case nvme_tcp_r2t:
8043f2304f8SSagi Grimberg nvme_tcp_init_recv_ctx(queue);
8056be18260SSagi Grimberg return nvme_tcp_handle_r2t(queue, (void *)queue->pdu);
8063f2304f8SSagi Grimberg default:
8073f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
8083f2304f8SSagi Grimberg "unsupported pdu type (%d)\n", hdr->type);
8093f2304f8SSagi Grimberg return -EINVAL;
8103f2304f8SSagi Grimberg }
8113f2304f8SSagi Grimberg }
8123f2304f8SSagi Grimberg
nvme_tcp_end_request(struct request * rq,u16 status)813988aef9eSChristoph Hellwig static inline void nvme_tcp_end_request(struct request *rq, u16 status)
814602d674cSSagi Grimberg {
815602d674cSSagi Grimberg union nvme_result res = {};
816602d674cSSagi Grimberg
8172eb81a33SChristoph Hellwig if (!nvme_try_complete_req(rq, cpu_to_le16(status << 1), res))
818ff029451SChristoph Hellwig nvme_complete_rq(rq);
819602d674cSSagi Grimberg }
820602d674cSSagi Grimberg
nvme_tcp_recv_data(struct nvme_tcp_queue * queue,struct sk_buff * skb,unsigned int * offset,size_t * len)8213f2304f8SSagi Grimberg static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb,
8223f2304f8SSagi Grimberg unsigned int *offset, size_t *len)
8233f2304f8SSagi Grimberg {
8243f2304f8SSagi Grimberg struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
8253b01a9d0SSagi Grimberg struct request *rq =
826e7006de6SSagi Grimberg nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
8273b01a9d0SSagi Grimberg struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
8283f2304f8SSagi Grimberg
8293f2304f8SSagi Grimberg while (true) {
8303f2304f8SSagi Grimberg int recv_len, ret;
8313f2304f8SSagi Grimberg
8323f2304f8SSagi Grimberg recv_len = min_t(size_t, *len, queue->data_remaining);
8333f2304f8SSagi Grimberg if (!recv_len)
8343f2304f8SSagi Grimberg break;
8353f2304f8SSagi Grimberg
8363f2304f8SSagi Grimberg if (!iov_iter_count(&req->iter)) {
8373f2304f8SSagi Grimberg req->curr_bio = req->curr_bio->bi_next;
8383f2304f8SSagi Grimberg
8393f2304f8SSagi Grimberg /*
8403f2304f8SSagi Grimberg * If we don`t have any bios it means that controller
8413f2304f8SSagi Grimberg * sent more data than we requested, hence error
8423f2304f8SSagi Grimberg */
8433f2304f8SSagi Grimberg if (!req->curr_bio) {
8443f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
8453f2304f8SSagi Grimberg "queue %d no space in request %#x",
8463f2304f8SSagi Grimberg nvme_tcp_queue_id(queue), rq->tag);
8473f2304f8SSagi Grimberg nvme_tcp_init_recv_ctx(queue);
8483f2304f8SSagi Grimberg return -EIO;
8493f2304f8SSagi Grimberg }
850de4eda9dSAl Viro nvme_tcp_init_iter(req, ITER_DEST);
8513f2304f8SSagi Grimberg }
8523f2304f8SSagi Grimberg
8533f2304f8SSagi Grimberg /* we can read only from what is left in this bio */
8543f2304f8SSagi Grimberg recv_len = min_t(size_t, recv_len,
8553f2304f8SSagi Grimberg iov_iter_count(&req->iter));
8563f2304f8SSagi Grimberg
8573f2304f8SSagi Grimberg if (queue->data_digest)
8583f2304f8SSagi Grimberg ret = skb_copy_and_hash_datagram_iter(skb, *offset,
8593f2304f8SSagi Grimberg &req->iter, recv_len, queue->rcv_hash);
8603f2304f8SSagi Grimberg else
8613f2304f8SSagi Grimberg ret = skb_copy_datagram_iter(skb, *offset,
8623f2304f8SSagi Grimberg &req->iter, recv_len);
8633f2304f8SSagi Grimberg if (ret) {
8643f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
8653f2304f8SSagi Grimberg "queue %d failed to copy request %#x data",
8663f2304f8SSagi Grimberg nvme_tcp_queue_id(queue), rq->tag);
8673f2304f8SSagi Grimberg return ret;
8683f2304f8SSagi Grimberg }
8693f2304f8SSagi Grimberg
8703f2304f8SSagi Grimberg *len -= recv_len;
8713f2304f8SSagi Grimberg *offset += recv_len;
8723f2304f8SSagi Grimberg queue->data_remaining -= recv_len;
8733f2304f8SSagi Grimberg }
8743f2304f8SSagi Grimberg
8753f2304f8SSagi Grimberg if (!queue->data_remaining) {
8763f2304f8SSagi Grimberg if (queue->data_digest) {
8773f2304f8SSagi Grimberg nvme_tcp_ddgst_final(queue->rcv_hash, &queue->exp_ddgst);
8783f2304f8SSagi Grimberg queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH;
8793f2304f8SSagi Grimberg } else {
8801a9460ceSSagi Grimberg if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
8811ba2e507SDaniel Wagner nvme_tcp_end_request(rq,
8821ba2e507SDaniel Wagner le16_to_cpu(req->status));
8831a9460ceSSagi Grimberg queue->nr_cqe++;
8841a9460ceSSagi Grimberg }
8853f2304f8SSagi Grimberg nvme_tcp_init_recv_ctx(queue);
8863f2304f8SSagi Grimberg }
8873f2304f8SSagi Grimberg }
8883f2304f8SSagi Grimberg
8893f2304f8SSagi Grimberg return 0;
8903f2304f8SSagi Grimberg }
8913f2304f8SSagi Grimberg
nvme_tcp_recv_ddgst(struct nvme_tcp_queue * queue,struct sk_buff * skb,unsigned int * offset,size_t * len)8923f2304f8SSagi Grimberg static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue,
8933f2304f8SSagi Grimberg struct sk_buff *skb, unsigned int *offset, size_t *len)
8943f2304f8SSagi Grimberg {
895602d674cSSagi Grimberg struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
8963f2304f8SSagi Grimberg char *ddgst = (char *)&queue->recv_ddgst;
8973f2304f8SSagi Grimberg size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining);
8983f2304f8SSagi Grimberg off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining;
8993f2304f8SSagi Grimberg int ret;
9003f2304f8SSagi Grimberg
9013f2304f8SSagi Grimberg ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len);
9023f2304f8SSagi Grimberg if (unlikely(ret))
9033f2304f8SSagi Grimberg return ret;
9043f2304f8SSagi Grimberg
9053f2304f8SSagi Grimberg queue->ddgst_remaining -= recv_len;
9063f2304f8SSagi Grimberg *offset += recv_len;
9073f2304f8SSagi Grimberg *len -= recv_len;
9083f2304f8SSagi Grimberg if (queue->ddgst_remaining)
9093f2304f8SSagi Grimberg return 0;
9103f2304f8SSagi Grimberg
9113f2304f8SSagi Grimberg if (queue->recv_ddgst != queue->exp_ddgst) {
9121ba2e507SDaniel Wagner struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
9131ba2e507SDaniel Wagner pdu->command_id);
9141ba2e507SDaniel Wagner struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
9151ba2e507SDaniel Wagner
9161ba2e507SDaniel Wagner req->status = cpu_to_le16(NVME_SC_DATA_XFER_ERROR);
9171ba2e507SDaniel Wagner
9183f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
9193f2304f8SSagi Grimberg "data digest error: recv %#x expected %#x\n",
9203f2304f8SSagi Grimberg le32_to_cpu(queue->recv_ddgst),
9213f2304f8SSagi Grimberg le32_to_cpu(queue->exp_ddgst));
9223f2304f8SSagi Grimberg }
9233f2304f8SSagi Grimberg
924602d674cSSagi Grimberg if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
925e7006de6SSagi Grimberg struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
926602d674cSSagi Grimberg pdu->command_id);
9271ba2e507SDaniel Wagner struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
928602d674cSSagi Grimberg
9291ba2e507SDaniel Wagner nvme_tcp_end_request(rq, le16_to_cpu(req->status));
9301a9460ceSSagi Grimberg queue->nr_cqe++;
931602d674cSSagi Grimberg }
932602d674cSSagi Grimberg
9333f2304f8SSagi Grimberg nvme_tcp_init_recv_ctx(queue);
9343f2304f8SSagi Grimberg return 0;
9353f2304f8SSagi Grimberg }
9363f2304f8SSagi Grimberg
nvme_tcp_recv_skb(read_descriptor_t * desc,struct sk_buff * skb,unsigned int offset,size_t len)9373f2304f8SSagi Grimberg static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,
9383f2304f8SSagi Grimberg unsigned int offset, size_t len)
9393f2304f8SSagi Grimberg {
9403f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = desc->arg.data;
9413f2304f8SSagi Grimberg size_t consumed = len;
9423f2304f8SSagi Grimberg int result;
9433f2304f8SSagi Grimberg
944aeacfcefSChris Leech if (unlikely(!queue->rd_enabled))
945aeacfcefSChris Leech return -EFAULT;
946aeacfcefSChris Leech
9473f2304f8SSagi Grimberg while (len) {
9483f2304f8SSagi Grimberg switch (nvme_tcp_recv_state(queue)) {
9493f2304f8SSagi Grimberg case NVME_TCP_RECV_PDU:
9503f2304f8SSagi Grimberg result = nvme_tcp_recv_pdu(queue, skb, &offset, &len);
9513f2304f8SSagi Grimberg break;
9523f2304f8SSagi Grimberg case NVME_TCP_RECV_DATA:
9533f2304f8SSagi Grimberg result = nvme_tcp_recv_data(queue, skb, &offset, &len);
9543f2304f8SSagi Grimberg break;
9553f2304f8SSagi Grimberg case NVME_TCP_RECV_DDGST:
9563f2304f8SSagi Grimberg result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len);
9573f2304f8SSagi Grimberg break;
9583f2304f8SSagi Grimberg default:
9593f2304f8SSagi Grimberg result = -EFAULT;
9603f2304f8SSagi Grimberg }
9613f2304f8SSagi Grimberg if (result) {
9623f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
9633f2304f8SSagi Grimberg "receive failed: %d\n", result);
9643f2304f8SSagi Grimberg queue->rd_enabled = false;
9653f2304f8SSagi Grimberg nvme_tcp_error_recovery(&queue->ctrl->ctrl);
9663f2304f8SSagi Grimberg return result;
9673f2304f8SSagi Grimberg }
9683f2304f8SSagi Grimberg }
9693f2304f8SSagi Grimberg
9703f2304f8SSagi Grimberg return consumed;
9713f2304f8SSagi Grimberg }
9723f2304f8SSagi Grimberg
nvme_tcp_data_ready(struct sock * sk)9733f2304f8SSagi Grimberg static void nvme_tcp_data_ready(struct sock *sk)
9743f2304f8SSagi Grimberg {
9753f2304f8SSagi Grimberg struct nvme_tcp_queue *queue;
9763f2304f8SSagi Grimberg
97740e0b090SPeilin Ye trace_sk_data_ready(sk);
97840e0b090SPeilin Ye
979386e5e6eSSagi Grimberg read_lock_bh(&sk->sk_callback_lock);
9803f2304f8SSagi Grimberg queue = sk->sk_user_data;
98172e5d757SSagi Grimberg if (likely(queue && queue->rd_enabled) &&
98272e5d757SSagi Grimberg !test_bit(NVME_TCP_Q_POLLING, &queue->flags))
9833f2304f8SSagi Grimberg queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
984386e5e6eSSagi Grimberg read_unlock_bh(&sk->sk_callback_lock);
9853f2304f8SSagi Grimberg }
9863f2304f8SSagi Grimberg
nvme_tcp_write_space(struct sock * sk)9873f2304f8SSagi Grimberg static void nvme_tcp_write_space(struct sock *sk)
9883f2304f8SSagi Grimberg {
9893f2304f8SSagi Grimberg struct nvme_tcp_queue *queue;
9903f2304f8SSagi Grimberg
9913f2304f8SSagi Grimberg read_lock_bh(&sk->sk_callback_lock);
9923f2304f8SSagi Grimberg queue = sk->sk_user_data;
9933f2304f8SSagi Grimberg if (likely(queue && sk_stream_is_writeable(sk))) {
9943f2304f8SSagi Grimberg clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
9953f2304f8SSagi Grimberg queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
9963f2304f8SSagi Grimberg }
9973f2304f8SSagi Grimberg read_unlock_bh(&sk->sk_callback_lock);
9983f2304f8SSagi Grimberg }
9993f2304f8SSagi Grimberg
nvme_tcp_state_change(struct sock * sk)10003f2304f8SSagi Grimberg static void nvme_tcp_state_change(struct sock *sk)
10013f2304f8SSagi Grimberg {
10023f2304f8SSagi Grimberg struct nvme_tcp_queue *queue;
10033f2304f8SSagi Grimberg
10048b73b45dSSagi Grimberg read_lock_bh(&sk->sk_callback_lock);
10053f2304f8SSagi Grimberg queue = sk->sk_user_data;
10063f2304f8SSagi Grimberg if (!queue)
10073f2304f8SSagi Grimberg goto done;
10083f2304f8SSagi Grimberg
10093f2304f8SSagi Grimberg switch (sk->sk_state) {
10103f2304f8SSagi Grimberg case TCP_CLOSE:
10113f2304f8SSagi Grimberg case TCP_CLOSE_WAIT:
10123f2304f8SSagi Grimberg case TCP_LAST_ACK:
10133f2304f8SSagi Grimberg case TCP_FIN_WAIT1:
10143f2304f8SSagi Grimberg case TCP_FIN_WAIT2:
10153f2304f8SSagi Grimberg nvme_tcp_error_recovery(&queue->ctrl->ctrl);
10163f2304f8SSagi Grimberg break;
10173f2304f8SSagi Grimberg default:
10183f2304f8SSagi Grimberg dev_info(queue->ctrl->ctrl.device,
10193f2304f8SSagi Grimberg "queue %d socket state %d\n",
10203f2304f8SSagi Grimberg nvme_tcp_queue_id(queue), sk->sk_state);
10213f2304f8SSagi Grimberg }
10223f2304f8SSagi Grimberg
10233f2304f8SSagi Grimberg queue->state_change(sk);
10243f2304f8SSagi Grimberg done:
10258b73b45dSSagi Grimberg read_unlock_bh(&sk->sk_callback_lock);
10263f2304f8SSagi Grimberg }
10273f2304f8SSagi Grimberg
nvme_tcp_done_send_req(struct nvme_tcp_queue * queue)10283f2304f8SSagi Grimberg static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue)
10293f2304f8SSagi Grimberg {
10303f2304f8SSagi Grimberg queue->request = NULL;
10313f2304f8SSagi Grimberg }
10323f2304f8SSagi Grimberg
nvme_tcp_fail_request(struct nvme_tcp_request * req)10333f2304f8SSagi Grimberg static void nvme_tcp_fail_request(struct nvme_tcp_request *req)
10343f2304f8SSagi Grimberg {
103563573807SSagi Grimberg if (nvme_tcp_async_req(req)) {
103663573807SSagi Grimberg union nvme_result res = {};
103763573807SSagi Grimberg
103863573807SSagi Grimberg nvme_complete_async_event(&req->queue->ctrl->ctrl,
103963573807SSagi Grimberg cpu_to_le16(NVME_SC_HOST_PATH_ERROR), &res);
104063573807SSagi Grimberg } else {
104163573807SSagi Grimberg nvme_tcp_end_request(blk_mq_rq_from_pdu(req),
104263573807SSagi Grimberg NVME_SC_HOST_PATH_ERROR);
104363573807SSagi Grimberg }
10443f2304f8SSagi Grimberg }
10453f2304f8SSagi Grimberg
nvme_tcp_try_send_data(struct nvme_tcp_request * req)10463f2304f8SSagi Grimberg static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
10473f2304f8SSagi Grimberg {
10483f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = req->queue;
104925e1f67eSSagi Grimberg int req_data_len = req->data_len;
1050c2700d28SVarun Prakash u32 h2cdata_left = req->h2cdata_left;
10513f2304f8SSagi Grimberg
10523f2304f8SSagi Grimberg while (true) {
105377698878SDavid Howells struct bio_vec bvec;
105477698878SDavid Howells struct msghdr msg = {
105577698878SDavid Howells .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES,
105677698878SDavid Howells };
10573f2304f8SSagi Grimberg struct page *page = nvme_tcp_req_cur_page(req);
10583f2304f8SSagi Grimberg size_t offset = nvme_tcp_req_cur_offset(req);
10593f2304f8SSagi Grimberg size_t len = nvme_tcp_req_cur_length(req);
10603f2304f8SSagi Grimberg bool last = nvme_tcp_pdu_last_send(req, len);
106125e1f67eSSagi Grimberg int req_data_sent = req->data_sent;
106277698878SDavid Howells int ret;
10633f2304f8SSagi Grimberg
1064122e5b9fSSagi Grimberg if (last && !queue->data_digest && !nvme_tcp_queue_more(queue))
106577698878SDavid Howells msg.msg_flags |= MSG_EOR;
10663f2304f8SSagi Grimberg else
106777698878SDavid Howells msg.msg_flags |= MSG_MORE;
10683f2304f8SSagi Grimberg
10696af7331aSOfir Gal if (!sendpages_ok(page, len, offset))
1070c97d3fb9SDavid Howells msg.msg_flags &= ~MSG_SPLICE_PAGES;
107177698878SDavid Howells
107277698878SDavid Howells bvec_set_page(&bvec, page, len, offset);
107377698878SDavid Howells iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
107477698878SDavid Howells ret = sock_sendmsg(queue->sock, &msg);
10753f2304f8SSagi Grimberg if (ret <= 0)
10763f2304f8SSagi Grimberg return ret;
10773f2304f8SSagi Grimberg
10783f2304f8SSagi Grimberg if (queue->data_digest)
10793f2304f8SSagi Grimberg nvme_tcp_ddgst_update(queue->snd_hash, page,
10803f2304f8SSagi Grimberg offset, ret);
10813f2304f8SSagi Grimberg
1082e371af03SSagi Grimberg /*
1083e371af03SSagi Grimberg * update the request iterator except for the last payload send
1084e371af03SSagi Grimberg * in the request where we don't want to modify it as we may
1085e371af03SSagi Grimberg * compete with the RX path completing the request.
1086e371af03SSagi Grimberg */
108725e1f67eSSagi Grimberg if (req_data_sent + ret < req_data_len)
1088e371af03SSagi Grimberg nvme_tcp_advance_req(req, ret);
1089e371af03SSagi Grimberg
1090e371af03SSagi Grimberg /* fully successful last send in current PDU */
10913f2304f8SSagi Grimberg if (last && ret == len) {
10923f2304f8SSagi Grimberg if (queue->data_digest) {
10933f2304f8SSagi Grimberg nvme_tcp_ddgst_final(queue->snd_hash,
10943f2304f8SSagi Grimberg &req->ddgst);
10953f2304f8SSagi Grimberg req->state = NVME_TCP_SEND_DDGST;
10963f2304f8SSagi Grimberg req->offset = 0;
10973f2304f8SSagi Grimberg } else {
1098c2700d28SVarun Prakash if (h2cdata_left)
1099c2700d28SVarun Prakash nvme_tcp_setup_h2c_data_pdu(req);
1100c2700d28SVarun Prakash else
11013f2304f8SSagi Grimberg nvme_tcp_done_send_req(queue);
11023f2304f8SSagi Grimberg }
11033f2304f8SSagi Grimberg return 1;
11043f2304f8SSagi Grimberg }
11053f2304f8SSagi Grimberg }
11063f2304f8SSagi Grimberg return -EAGAIN;
11073f2304f8SSagi Grimberg }
11083f2304f8SSagi Grimberg
nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request * req)11093f2304f8SSagi Grimberg static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
11103f2304f8SSagi Grimberg {
11113f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = req->queue;
1112a3406352SSagi Grimberg struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
111377698878SDavid Howells struct bio_vec bvec;
111477698878SDavid Howells struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES, };
11153f2304f8SSagi Grimberg bool inline_data = nvme_tcp_has_inline_data(req);
11163f2304f8SSagi Grimberg u8 hdgst = nvme_tcp_hdgst_len(queue);
11173f2304f8SSagi Grimberg int len = sizeof(*pdu) + hdgst - req->offset;
11183f2304f8SSagi Grimberg int ret;
11193f2304f8SSagi Grimberg
1120122e5b9fSSagi Grimberg if (inline_data || nvme_tcp_queue_more(queue))
112177698878SDavid Howells msg.msg_flags |= MSG_MORE;
11225bb052d7SSagi Grimberg else
112377698878SDavid Howells msg.msg_flags |= MSG_EOR;
11245bb052d7SSagi Grimberg
11253f2304f8SSagi Grimberg if (queue->hdr_digest && !req->offset)
11263f2304f8SSagi Grimberg nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
11273f2304f8SSagi Grimberg
112877698878SDavid Howells bvec_set_virt(&bvec, (void *)pdu + req->offset, len);
112977698878SDavid Howells iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
113077698878SDavid Howells ret = sock_sendmsg(queue->sock, &msg);
11313f2304f8SSagi Grimberg if (unlikely(ret <= 0))
11323f2304f8SSagi Grimberg return ret;
11333f2304f8SSagi Grimberg
11343f2304f8SSagi Grimberg len -= ret;
11353f2304f8SSagi Grimberg if (!len) {
11363f2304f8SSagi Grimberg if (inline_data) {
11373f2304f8SSagi Grimberg req->state = NVME_TCP_SEND_DATA;
11383f2304f8SSagi Grimberg if (queue->data_digest)
11393f2304f8SSagi Grimberg crypto_ahash_init(queue->snd_hash);
11403f2304f8SSagi Grimberg } else {
11413f2304f8SSagi Grimberg nvme_tcp_done_send_req(queue);
11423f2304f8SSagi Grimberg }
11433f2304f8SSagi Grimberg return 1;
11443f2304f8SSagi Grimberg }
11453f2304f8SSagi Grimberg req->offset += ret;
11463f2304f8SSagi Grimberg
11473f2304f8SSagi Grimberg return -EAGAIN;
11483f2304f8SSagi Grimberg }
11493f2304f8SSagi Grimberg
nvme_tcp_try_send_data_pdu(struct nvme_tcp_request * req)11503f2304f8SSagi Grimberg static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
11513f2304f8SSagi Grimberg {
11523f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = req->queue;
1153a3406352SSagi Grimberg struct nvme_tcp_data_pdu *pdu = nvme_tcp_req_data_pdu(req);
115477698878SDavid Howells struct bio_vec bvec;
115577698878SDavid Howells struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_MORE, };
11563f2304f8SSagi Grimberg u8 hdgst = nvme_tcp_hdgst_len(queue);
11573f2304f8SSagi Grimberg int len = sizeof(*pdu) - req->offset + hdgst;
11583f2304f8SSagi Grimberg int ret;
11593f2304f8SSagi Grimberg
11603f2304f8SSagi Grimberg if (queue->hdr_digest && !req->offset)
11613f2304f8SSagi Grimberg nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
11623f2304f8SSagi Grimberg
1163c2700d28SVarun Prakash if (!req->h2cdata_left)
116477698878SDavid Howells msg.msg_flags |= MSG_SPLICE_PAGES;
116577698878SDavid Howells
116677698878SDavid Howells bvec_set_virt(&bvec, (void *)pdu + req->offset, len);
116777698878SDavid Howells iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
116877698878SDavid Howells ret = sock_sendmsg(queue->sock, &msg);
11693f2304f8SSagi Grimberg if (unlikely(ret <= 0))
11703f2304f8SSagi Grimberg return ret;
11713f2304f8SSagi Grimberg
11723f2304f8SSagi Grimberg len -= ret;
11733f2304f8SSagi Grimberg if (!len) {
11743f2304f8SSagi Grimberg req->state = NVME_TCP_SEND_DATA;
11753f2304f8SSagi Grimberg if (queue->data_digest)
11763f2304f8SSagi Grimberg crypto_ahash_init(queue->snd_hash);
11773f2304f8SSagi Grimberg return 1;
11783f2304f8SSagi Grimberg }
11793f2304f8SSagi Grimberg req->offset += ret;
11803f2304f8SSagi Grimberg
11813f2304f8SSagi Grimberg return -EAGAIN;
11823f2304f8SSagi Grimberg }
11833f2304f8SSagi Grimberg
nvme_tcp_try_send_ddgst(struct nvme_tcp_request * req)11843f2304f8SSagi Grimberg static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
11853f2304f8SSagi Grimberg {
11863f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = req->queue;
1187ce7723e9SVarun Prakash size_t offset = req->offset;
1188c2700d28SVarun Prakash u32 h2cdata_left = req->h2cdata_left;
11893f2304f8SSagi Grimberg int ret;
1190122e5b9fSSagi Grimberg struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
11913f2304f8SSagi Grimberg struct kvec iov = {
1192d89b9f3bSVarun Prakash .iov_base = (u8 *)&req->ddgst + req->offset,
11933f2304f8SSagi Grimberg .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset
11943f2304f8SSagi Grimberg };
11953f2304f8SSagi Grimberg
1196122e5b9fSSagi Grimberg if (nvme_tcp_queue_more(queue))
1197122e5b9fSSagi Grimberg msg.msg_flags |= MSG_MORE;
1198122e5b9fSSagi Grimberg else
1199122e5b9fSSagi Grimberg msg.msg_flags |= MSG_EOR;
1200122e5b9fSSagi Grimberg
12013f2304f8SSagi Grimberg ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
12023f2304f8SSagi Grimberg if (unlikely(ret <= 0))
12033f2304f8SSagi Grimberg return ret;
12043f2304f8SSagi Grimberg
1205ce7723e9SVarun Prakash if (offset + ret == NVME_TCP_DIGEST_LENGTH) {
1206c2700d28SVarun Prakash if (h2cdata_left)
1207c2700d28SVarun Prakash nvme_tcp_setup_h2c_data_pdu(req);
1208c2700d28SVarun Prakash else
12093f2304f8SSagi Grimberg nvme_tcp_done_send_req(queue);
12103f2304f8SSagi Grimberg return 1;
12113f2304f8SSagi Grimberg }
12123f2304f8SSagi Grimberg
12133f2304f8SSagi Grimberg req->offset += ret;
12143f2304f8SSagi Grimberg return -EAGAIN;
12153f2304f8SSagi Grimberg }
12163f2304f8SSagi Grimberg
nvme_tcp_try_send(struct nvme_tcp_queue * queue)12173f2304f8SSagi Grimberg static int nvme_tcp_try_send(struct nvme_tcp_queue *queue)
12183f2304f8SSagi Grimberg {
12193f2304f8SSagi Grimberg struct nvme_tcp_request *req;
122083e1226bSSagi Grimberg unsigned int noreclaim_flag;
12213f2304f8SSagi Grimberg int ret = 1;
12223f2304f8SSagi Grimberg
12233f2304f8SSagi Grimberg if (!queue->request) {
12243f2304f8SSagi Grimberg queue->request = nvme_tcp_fetch_request(queue);
12253f2304f8SSagi Grimberg if (!queue->request)
12263f2304f8SSagi Grimberg return 0;
12273f2304f8SSagi Grimberg }
12283f2304f8SSagi Grimberg req = queue->request;
12293f2304f8SSagi Grimberg
123083e1226bSSagi Grimberg noreclaim_flag = memalloc_noreclaim_save();
12313f2304f8SSagi Grimberg if (req->state == NVME_TCP_SEND_CMD_PDU) {
12323f2304f8SSagi Grimberg ret = nvme_tcp_try_send_cmd_pdu(req);
12333f2304f8SSagi Grimberg if (ret <= 0)
12343f2304f8SSagi Grimberg goto done;
12353f2304f8SSagi Grimberg if (!nvme_tcp_has_inline_data(req))
123683e1226bSSagi Grimberg goto out;
12373f2304f8SSagi Grimberg }
12383f2304f8SSagi Grimberg
12393f2304f8SSagi Grimberg if (req->state == NVME_TCP_SEND_H2C_PDU) {
12403f2304f8SSagi Grimberg ret = nvme_tcp_try_send_data_pdu(req);
12413f2304f8SSagi Grimberg if (ret <= 0)
12423f2304f8SSagi Grimberg goto done;
12433f2304f8SSagi Grimberg }
12443f2304f8SSagi Grimberg
12453f2304f8SSagi Grimberg if (req->state == NVME_TCP_SEND_DATA) {
12463f2304f8SSagi Grimberg ret = nvme_tcp_try_send_data(req);
12473f2304f8SSagi Grimberg if (ret <= 0)
12483f2304f8SSagi Grimberg goto done;
12493f2304f8SSagi Grimberg }
12503f2304f8SSagi Grimberg
12513f2304f8SSagi Grimberg if (req->state == NVME_TCP_SEND_DDGST)
12523f2304f8SSagi Grimberg ret = nvme_tcp_try_send_ddgst(req);
12533f2304f8SSagi Grimberg done:
12545ff4e112SSagi Grimberg if (ret == -EAGAIN) {
12553f2304f8SSagi Grimberg ret = 0;
12565ff4e112SSagi Grimberg } else if (ret < 0) {
12575ff4e112SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
12585ff4e112SSagi Grimberg "failed to send request %d\n", ret);
12595ff4e112SSagi Grimberg nvme_tcp_fail_request(queue->request);
12605ff4e112SSagi Grimberg nvme_tcp_done_send_req(queue);
12615ff4e112SSagi Grimberg }
126283e1226bSSagi Grimberg out:
126383e1226bSSagi Grimberg memalloc_noreclaim_restore(noreclaim_flag);
12643f2304f8SSagi Grimberg return ret;
12653f2304f8SSagi Grimberg }
12663f2304f8SSagi Grimberg
nvme_tcp_try_recv(struct nvme_tcp_queue * queue)12673f2304f8SSagi Grimberg static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue)
12683f2304f8SSagi Grimberg {
126910407ec9SPotnuri Bharat Teja struct socket *sock = queue->sock;
127010407ec9SPotnuri Bharat Teja struct sock *sk = sock->sk;
12713f2304f8SSagi Grimberg read_descriptor_t rd_desc;
12723f2304f8SSagi Grimberg int consumed;
12733f2304f8SSagi Grimberg
12743f2304f8SSagi Grimberg rd_desc.arg.data = queue;
12753f2304f8SSagi Grimberg rd_desc.count = 1;
12763f2304f8SSagi Grimberg lock_sock(sk);
12771a9460ceSSagi Grimberg queue->nr_cqe = 0;
127810407ec9SPotnuri Bharat Teja consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb);
12793f2304f8SSagi Grimberg release_sock(sk);
12803f2304f8SSagi Grimberg return consumed;
12813f2304f8SSagi Grimberg }
12823f2304f8SSagi Grimberg
nvme_tcp_io_work(struct work_struct * w)12833f2304f8SSagi Grimberg static void nvme_tcp_io_work(struct work_struct *w)
12843f2304f8SSagi Grimberg {
12853f2304f8SSagi Grimberg struct nvme_tcp_queue *queue =
12863f2304f8SSagi Grimberg container_of(w, struct nvme_tcp_queue, io_work);
1287ddef2957SWunderlich, Mark unsigned long deadline = jiffies + msecs_to_jiffies(1);
12883f2304f8SSagi Grimberg
12893f2304f8SSagi Grimberg do {
12903f2304f8SSagi Grimberg bool pending = false;
12913f2304f8SSagi Grimberg int result;
12923f2304f8SSagi Grimberg
1293db5ad6b7SSagi Grimberg if (mutex_trylock(&queue->send_mutex)) {
12943f2304f8SSagi Grimberg result = nvme_tcp_try_send(queue);
1295db5ad6b7SSagi Grimberg mutex_unlock(&queue->send_mutex);
12965ff4e112SSagi Grimberg if (result > 0)
12973f2304f8SSagi Grimberg pending = true;
12985ff4e112SSagi Grimberg else if (unlikely(result < 0))
12995ff4e112SSagi Grimberg break;
130070f437fbSKeith Busch }
13013f2304f8SSagi Grimberg
13023f2304f8SSagi Grimberg result = nvme_tcp_try_recv(queue);
13033f2304f8SSagi Grimberg if (result > 0)
13043f2304f8SSagi Grimberg pending = true;
1305761ad26cSSagi Grimberg else if (unlikely(result < 0))
130639d06079SSagi Grimberg return;
13073f2304f8SSagi Grimberg
1308160f3549SSagi Grimberg if (!pending || !queue->rd_enabled)
13093f2304f8SSagi Grimberg return;
13103f2304f8SSagi Grimberg
1311ddef2957SWunderlich, Mark } while (!time_after(jiffies, deadline)); /* quota is exhausted */
13123f2304f8SSagi Grimberg
13133f2304f8SSagi Grimberg queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
13143f2304f8SSagi Grimberg }
13153f2304f8SSagi Grimberg
nvme_tcp_free_crypto(struct nvme_tcp_queue * queue)13163f2304f8SSagi Grimberg static void nvme_tcp_free_crypto(struct nvme_tcp_queue *queue)
13173f2304f8SSagi Grimberg {
13183f2304f8SSagi Grimberg struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
13193f2304f8SSagi Grimberg
13203f2304f8SSagi Grimberg ahash_request_free(queue->rcv_hash);
13213f2304f8SSagi Grimberg ahash_request_free(queue->snd_hash);
13223f2304f8SSagi Grimberg crypto_free_ahash(tfm);
13233f2304f8SSagi Grimberg }
13243f2304f8SSagi Grimberg
nvme_tcp_alloc_crypto(struct nvme_tcp_queue * queue)13253f2304f8SSagi Grimberg static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue *queue)
13263f2304f8SSagi Grimberg {
13273f2304f8SSagi Grimberg struct crypto_ahash *tfm;
13283f2304f8SSagi Grimberg
13293f2304f8SSagi Grimberg tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
13303f2304f8SSagi Grimberg if (IS_ERR(tfm))
13313f2304f8SSagi Grimberg return PTR_ERR(tfm);
13323f2304f8SSagi Grimberg
13333f2304f8SSagi Grimberg queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
13343f2304f8SSagi Grimberg if (!queue->snd_hash)
13353f2304f8SSagi Grimberg goto free_tfm;
13363f2304f8SSagi Grimberg ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
13373f2304f8SSagi Grimberg
13383f2304f8SSagi Grimberg queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
13393f2304f8SSagi Grimberg if (!queue->rcv_hash)
13403f2304f8SSagi Grimberg goto free_snd_hash;
13413f2304f8SSagi Grimberg ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
13423f2304f8SSagi Grimberg
13433f2304f8SSagi Grimberg return 0;
13443f2304f8SSagi Grimberg free_snd_hash:
13453f2304f8SSagi Grimberg ahash_request_free(queue->snd_hash);
13463f2304f8SSagi Grimberg free_tfm:
13473f2304f8SSagi Grimberg crypto_free_ahash(tfm);
13483f2304f8SSagi Grimberg return -ENOMEM;
13493f2304f8SSagi Grimberg }
13503f2304f8SSagi Grimberg
nvme_tcp_free_async_req(struct nvme_tcp_ctrl * ctrl)13513f2304f8SSagi Grimberg static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl)
13523f2304f8SSagi Grimberg {
13533f2304f8SSagi Grimberg struct nvme_tcp_request *async = &ctrl->async_req;
13543f2304f8SSagi Grimberg
13553f2304f8SSagi Grimberg page_frag_free(async->pdu);
13563f2304f8SSagi Grimberg }
13573f2304f8SSagi Grimberg
nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl * ctrl)13583f2304f8SSagi Grimberg static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
13593f2304f8SSagi Grimberg {
13603f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = &ctrl->queues[0];
13613f2304f8SSagi Grimberg struct nvme_tcp_request *async = &ctrl->async_req;
13623f2304f8SSagi Grimberg u8 hdgst = nvme_tcp_hdgst_len(queue);
13633f2304f8SSagi Grimberg
13643f2304f8SSagi Grimberg async->pdu = page_frag_alloc(&queue->pf_cache,
13653f2304f8SSagi Grimberg sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
13663f2304f8SSagi Grimberg GFP_KERNEL | __GFP_ZERO);
13673f2304f8SSagi Grimberg if (!async->pdu)
13683f2304f8SSagi Grimberg return -ENOMEM;
13693f2304f8SSagi Grimberg
13703f2304f8SSagi Grimberg async->queue = &ctrl->queues[0];
13713f2304f8SSagi Grimberg return 0;
13723f2304f8SSagi Grimberg }
13733f2304f8SSagi Grimberg
nvme_tcp_free_queue(struct nvme_ctrl * nctrl,int qid)13743f2304f8SSagi Grimberg static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
13753f2304f8SSagi Grimberg {
13763f2304f8SSagi Grimberg struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
13773f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = &ctrl->queues[qid];
137883e1226bSSagi Grimberg unsigned int noreclaim_flag;
13793f2304f8SSagi Grimberg
13803f2304f8SSagi Grimberg if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
13813f2304f8SSagi Grimberg return;
13823f2304f8SSagi Grimberg
13833f2304f8SSagi Grimberg if (queue->hdr_digest || queue->data_digest)
13843f2304f8SSagi Grimberg nvme_tcp_free_crypto(queue);
13853f2304f8SSagi Grimberg
1386a0727489SYunsheng Lin page_frag_cache_drain(&queue->pf_cache);
138783e1226bSSagi Grimberg
138883e1226bSSagi Grimberg noreclaim_flag = memalloc_noreclaim_save();
1389e40d4eb8SHannes Reinecke /* ->sock will be released by fput() */
1390e40d4eb8SHannes Reinecke fput(queue->sock->file);
1391e40d4eb8SHannes Reinecke queue->sock = NULL;
139283e1226bSSagi Grimberg memalloc_noreclaim_restore(noreclaim_flag);
139383e1226bSSagi Grimberg
13943f2304f8SSagi Grimberg kfree(queue->pdu);
1395d48f92cdSKeith Busch mutex_destroy(&queue->send_mutex);
13969ebbfe49SChao Leng mutex_destroy(&queue->queue_lock);
13973f2304f8SSagi Grimberg }
13983f2304f8SSagi Grimberg
nvme_tcp_init_connection(struct nvme_tcp_queue * queue)13993f2304f8SSagi Grimberg static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
14003f2304f8SSagi Grimberg {
14013f2304f8SSagi Grimberg struct nvme_tcp_icreq_pdu *icreq;
14023f2304f8SSagi Grimberg struct nvme_tcp_icresp_pdu *icresp;
14032837966aSHannes Reinecke char cbuf[CMSG_LEN(sizeof(char))] = {};
14042837966aSHannes Reinecke u8 ctype;
14053f2304f8SSagi Grimberg struct msghdr msg = {};
14063f2304f8SSagi Grimberg struct kvec iov;
14073f2304f8SSagi Grimberg bool ctrl_hdgst, ctrl_ddgst;
1408c2700d28SVarun Prakash u32 maxh2cdata;
14093f2304f8SSagi Grimberg int ret;
14103f2304f8SSagi Grimberg
14113f2304f8SSagi Grimberg icreq = kzalloc(sizeof(*icreq), GFP_KERNEL);
14123f2304f8SSagi Grimberg if (!icreq)
14133f2304f8SSagi Grimberg return -ENOMEM;
14143f2304f8SSagi Grimberg
14153f2304f8SSagi Grimberg icresp = kzalloc(sizeof(*icresp), GFP_KERNEL);
14163f2304f8SSagi Grimberg if (!icresp) {
14173f2304f8SSagi Grimberg ret = -ENOMEM;
14183f2304f8SSagi Grimberg goto free_icreq;
14193f2304f8SSagi Grimberg }
14203f2304f8SSagi Grimberg
14213f2304f8SSagi Grimberg icreq->hdr.type = nvme_tcp_icreq;
14223f2304f8SSagi Grimberg icreq->hdr.hlen = sizeof(*icreq);
14233f2304f8SSagi Grimberg icreq->hdr.pdo = 0;
14243f2304f8SSagi Grimberg icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen);
14253f2304f8SSagi Grimberg icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
14263f2304f8SSagi Grimberg icreq->maxr2t = 0; /* single inflight r2t supported */
14273f2304f8SSagi Grimberg icreq->hpda = 0; /* no alignment constraint */
14283f2304f8SSagi Grimberg if (queue->hdr_digest)
14293f2304f8SSagi Grimberg icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
14303f2304f8SSagi Grimberg if (queue->data_digest)
14313f2304f8SSagi Grimberg icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
14323f2304f8SSagi Grimberg
14333f2304f8SSagi Grimberg iov.iov_base = icreq;
14343f2304f8SSagi Grimberg iov.iov_len = sizeof(*icreq);
14353f2304f8SSagi Grimberg ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
1436e4f4aabbSHannes Reinecke if (ret < 0) {
1437e4f4aabbSHannes Reinecke pr_warn("queue %d: failed to send icreq, error %d\n",
1438e4f4aabbSHannes Reinecke nvme_tcp_queue_id(queue), ret);
14393f2304f8SSagi Grimberg goto free_icresp;
1440e4f4aabbSHannes Reinecke }
14413f2304f8SSagi Grimberg
14423f2304f8SSagi Grimberg memset(&msg, 0, sizeof(msg));
14433f2304f8SSagi Grimberg iov.iov_base = icresp;
14443f2304f8SSagi Grimberg iov.iov_len = sizeof(*icresp);
144536389576SHannes Reinecke if (nvme_tcp_queue_tls(queue)) {
14462837966aSHannes Reinecke msg.msg_control = cbuf;
14472837966aSHannes Reinecke msg.msg_controllen = sizeof(cbuf);
14482837966aSHannes Reinecke }
14493f2304f8SSagi Grimberg ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
14503f2304f8SSagi Grimberg iov.iov_len, msg.msg_flags);
1451e4f4aabbSHannes Reinecke if (ret < 0) {
1452e4f4aabbSHannes Reinecke pr_warn("queue %d: failed to receive icresp, error %d\n",
1453e4f4aabbSHannes Reinecke nvme_tcp_queue_id(queue), ret);
14543f2304f8SSagi Grimberg goto free_icresp;
1455e4f4aabbSHannes Reinecke }
14560e32fdd7SChristophe JAILLET ret = -ENOTCONN;
145736389576SHannes Reinecke if (nvme_tcp_queue_tls(queue)) {
14582837966aSHannes Reinecke ctype = tls_get_record_type(queue->sock->sk,
14592837966aSHannes Reinecke (struct cmsghdr *)cbuf);
14602837966aSHannes Reinecke if (ctype != TLS_RECORD_TYPE_DATA) {
14612837966aSHannes Reinecke pr_err("queue %d: unhandled TLS record %d\n",
14622837966aSHannes Reinecke nvme_tcp_queue_id(queue), ctype);
14630e32fdd7SChristophe JAILLET goto free_icresp;
14642837966aSHannes Reinecke }
14652837966aSHannes Reinecke }
14663f2304f8SSagi Grimberg ret = -EINVAL;
14673f2304f8SSagi Grimberg if (icresp->hdr.type != nvme_tcp_icresp) {
14683f2304f8SSagi Grimberg pr_err("queue %d: bad type returned %d\n",
14693f2304f8SSagi Grimberg nvme_tcp_queue_id(queue), icresp->hdr.type);
14703f2304f8SSagi Grimberg goto free_icresp;
14713f2304f8SSagi Grimberg }
14723f2304f8SSagi Grimberg
14733f2304f8SSagi Grimberg if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) {
14743f2304f8SSagi Grimberg pr_err("queue %d: bad pdu length returned %d\n",
14753f2304f8SSagi Grimberg nvme_tcp_queue_id(queue), icresp->hdr.plen);
14763f2304f8SSagi Grimberg goto free_icresp;
14773f2304f8SSagi Grimberg }
14783f2304f8SSagi Grimberg
14793f2304f8SSagi Grimberg if (icresp->pfv != NVME_TCP_PFV_1_0) {
14803f2304f8SSagi Grimberg pr_err("queue %d: bad pfv returned %d\n",
14813f2304f8SSagi Grimberg nvme_tcp_queue_id(queue), icresp->pfv);
14823f2304f8SSagi Grimberg goto free_icresp;
14833f2304f8SSagi Grimberg }
14843f2304f8SSagi Grimberg
14853f2304f8SSagi Grimberg ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE);
14863f2304f8SSagi Grimberg if ((queue->data_digest && !ctrl_ddgst) ||
14873f2304f8SSagi Grimberg (!queue->data_digest && ctrl_ddgst)) {
14883f2304f8SSagi Grimberg pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
14893f2304f8SSagi Grimberg nvme_tcp_queue_id(queue),
14903f2304f8SSagi Grimberg queue->data_digest ? "enabled" : "disabled",
14913f2304f8SSagi Grimberg ctrl_ddgst ? "enabled" : "disabled");
14923f2304f8SSagi Grimberg goto free_icresp;
14933f2304f8SSagi Grimberg }
14943f2304f8SSagi Grimberg
14953f2304f8SSagi Grimberg ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE);
14963f2304f8SSagi Grimberg if ((queue->hdr_digest && !ctrl_hdgst) ||
14973f2304f8SSagi Grimberg (!queue->hdr_digest && ctrl_hdgst)) {
14983f2304f8SSagi Grimberg pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
14993f2304f8SSagi Grimberg nvme_tcp_queue_id(queue),
15003f2304f8SSagi Grimberg queue->hdr_digest ? "enabled" : "disabled",
15013f2304f8SSagi Grimberg ctrl_hdgst ? "enabled" : "disabled");
15023f2304f8SSagi Grimberg goto free_icresp;
15033f2304f8SSagi Grimberg }
15043f2304f8SSagi Grimberg
15053f2304f8SSagi Grimberg if (icresp->cpda != 0) {
15063f2304f8SSagi Grimberg pr_err("queue %d: unsupported cpda returned %d\n",
15073f2304f8SSagi Grimberg nvme_tcp_queue_id(queue), icresp->cpda);
15083f2304f8SSagi Grimberg goto free_icresp;
15093f2304f8SSagi Grimberg }
15103f2304f8SSagi Grimberg
1511c2700d28SVarun Prakash maxh2cdata = le32_to_cpu(icresp->maxdata);
1512c2700d28SVarun Prakash if ((maxh2cdata % 4) || (maxh2cdata < NVME_TCP_MIN_MAXH2CDATA)) {
1513c2700d28SVarun Prakash pr_err("queue %d: invalid maxh2cdata returned %u\n",
1514c2700d28SVarun Prakash nvme_tcp_queue_id(queue), maxh2cdata);
1515c2700d28SVarun Prakash goto free_icresp;
1516c2700d28SVarun Prakash }
1517c2700d28SVarun Prakash queue->maxh2cdata = maxh2cdata;
1518c2700d28SVarun Prakash
15193f2304f8SSagi Grimberg ret = 0;
15203f2304f8SSagi Grimberg free_icresp:
15213f2304f8SSagi Grimberg kfree(icresp);
15223f2304f8SSagi Grimberg free_icreq:
15233f2304f8SSagi Grimberg kfree(icreq);
15243f2304f8SSagi Grimberg return ret;
15253f2304f8SSagi Grimberg }
15263f2304f8SSagi Grimberg
nvme_tcp_admin_queue(struct nvme_tcp_queue * queue)152740510a63SSagi Grimberg static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue)
152840510a63SSagi Grimberg {
152940510a63SSagi Grimberg return nvme_tcp_queue_id(queue) == 0;
153040510a63SSagi Grimberg }
153140510a63SSagi Grimberg
nvme_tcp_default_queue(struct nvme_tcp_queue * queue)153240510a63SSagi Grimberg static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue)
153340510a63SSagi Grimberg {
153440510a63SSagi Grimberg struct nvme_tcp_ctrl *ctrl = queue->ctrl;
153540510a63SSagi Grimberg int qid = nvme_tcp_queue_id(queue);
153640510a63SSagi Grimberg
153740510a63SSagi Grimberg return !nvme_tcp_admin_queue(queue) &&
153840510a63SSagi Grimberg qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT];
153940510a63SSagi Grimberg }
154040510a63SSagi Grimberg
nvme_tcp_read_queue(struct nvme_tcp_queue * queue)154140510a63SSagi Grimberg static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue)
154240510a63SSagi Grimberg {
154340510a63SSagi Grimberg struct nvme_tcp_ctrl *ctrl = queue->ctrl;
154440510a63SSagi Grimberg int qid = nvme_tcp_queue_id(queue);
154540510a63SSagi Grimberg
154640510a63SSagi Grimberg return !nvme_tcp_admin_queue(queue) &&
154740510a63SSagi Grimberg !nvme_tcp_default_queue(queue) &&
154840510a63SSagi Grimberg qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
154940510a63SSagi Grimberg ctrl->io_queues[HCTX_TYPE_READ];
155040510a63SSagi Grimberg }
155140510a63SSagi Grimberg
nvme_tcp_poll_queue(struct nvme_tcp_queue * queue)155240510a63SSagi Grimberg static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue)
155340510a63SSagi Grimberg {
155440510a63SSagi Grimberg struct nvme_tcp_ctrl *ctrl = queue->ctrl;
155540510a63SSagi Grimberg int qid = nvme_tcp_queue_id(queue);
155640510a63SSagi Grimberg
155740510a63SSagi Grimberg return !nvme_tcp_admin_queue(queue) &&
155840510a63SSagi Grimberg !nvme_tcp_default_queue(queue) &&
155940510a63SSagi Grimberg !nvme_tcp_read_queue(queue) &&
156040510a63SSagi Grimberg qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
156140510a63SSagi Grimberg ctrl->io_queues[HCTX_TYPE_READ] +
156240510a63SSagi Grimberg ctrl->io_queues[HCTX_TYPE_POLL];
156340510a63SSagi Grimberg }
156440510a63SSagi Grimberg
nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue * queue)156540510a63SSagi Grimberg static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue)
156640510a63SSagi Grimberg {
156740510a63SSagi Grimberg struct nvme_tcp_ctrl *ctrl = queue->ctrl;
156840510a63SSagi Grimberg int qid = nvme_tcp_queue_id(queue);
156940510a63SSagi Grimberg int n = 0;
157040510a63SSagi Grimberg
157140510a63SSagi Grimberg if (nvme_tcp_default_queue(queue))
157240510a63SSagi Grimberg n = qid - 1;
157340510a63SSagi Grimberg else if (nvme_tcp_read_queue(queue))
157440510a63SSagi Grimberg n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] - 1;
157540510a63SSagi Grimberg else if (nvme_tcp_poll_queue(queue))
157640510a63SSagi Grimberg n = qid - ctrl->io_queues[HCTX_TYPE_DEFAULT] -
157740510a63SSagi Grimberg ctrl->io_queues[HCTX_TYPE_READ] - 1;
15780c29f9faSLi Feng if (wq_unbound)
15790c29f9faSLi Feng queue->io_cpu = WORK_CPU_UNBOUND;
15800c29f9faSLi Feng else
158140510a63SSagi Grimberg queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false);
158240510a63SSagi Grimberg }
158340510a63SSagi Grimberg
nvme_tcp_tls_done(void * data,int status,key_serial_t pskid)1584be8e82caSHannes Reinecke static void nvme_tcp_tls_done(void *data, int status, key_serial_t pskid)
1585be8e82caSHannes Reinecke {
1586be8e82caSHannes Reinecke struct nvme_tcp_queue *queue = data;
1587be8e82caSHannes Reinecke struct nvme_tcp_ctrl *ctrl = queue->ctrl;
1588be8e82caSHannes Reinecke int qid = nvme_tcp_queue_id(queue);
1589be8e82caSHannes Reinecke struct key *tls_key;
1590be8e82caSHannes Reinecke
1591be8e82caSHannes Reinecke dev_dbg(ctrl->ctrl.device, "queue %d: TLS handshake done, key %x, status %d\n",
1592be8e82caSHannes Reinecke qid, pskid, status);
1593be8e82caSHannes Reinecke
1594be8e82caSHannes Reinecke if (status) {
1595be8e82caSHannes Reinecke queue->tls_err = -status;
1596be8e82caSHannes Reinecke goto out_complete;
1597be8e82caSHannes Reinecke }
1598be8e82caSHannes Reinecke
15995bc46b49SHannes Reinecke tls_key = nvme_tls_key_lookup(pskid);
1600be8e82caSHannes Reinecke if (IS_ERR(tls_key)) {
1601be8e82caSHannes Reinecke dev_warn(ctrl->ctrl.device, "queue %d: Invalid key %x\n",
1602be8e82caSHannes Reinecke qid, pskid);
1603be8e82caSHannes Reinecke queue->tls_err = -ENOKEY;
1604be8e82caSHannes Reinecke } else {
160536389576SHannes Reinecke queue->tls_enabled = true;
160636389576SHannes Reinecke if (qid == 0)
160736389576SHannes Reinecke ctrl->ctrl.tls_pskid = key_serial(tls_key);
160836389576SHannes Reinecke key_put(tls_key);
1609be8e82caSHannes Reinecke queue->tls_err = 0;
1610be8e82caSHannes Reinecke }
1611be8e82caSHannes Reinecke
1612be8e82caSHannes Reinecke out_complete:
1613be8e82caSHannes Reinecke complete(&queue->tls_complete);
1614be8e82caSHannes Reinecke }
1615be8e82caSHannes Reinecke
nvme_tcp_start_tls(struct nvme_ctrl * nctrl,struct nvme_tcp_queue * queue,key_serial_t pskid)1616be8e82caSHannes Reinecke static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl,
1617be8e82caSHannes Reinecke struct nvme_tcp_queue *queue,
1618be8e82caSHannes Reinecke key_serial_t pskid)
1619be8e82caSHannes Reinecke {
1620be8e82caSHannes Reinecke int qid = nvme_tcp_queue_id(queue);
1621be8e82caSHannes Reinecke int ret;
1622be8e82caSHannes Reinecke struct tls_handshake_args args;
1623be8e82caSHannes Reinecke unsigned long tmo = tls_handshake_timeout * HZ;
1624be8e82caSHannes Reinecke key_serial_t keyring = nvme_keyring_id();
1625be8e82caSHannes Reinecke
1626be8e82caSHannes Reinecke dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n",
1627be8e82caSHannes Reinecke qid, pskid);
1628be8e82caSHannes Reinecke memset(&args, 0, sizeof(args));
1629be8e82caSHannes Reinecke args.ta_sock = queue->sock;
1630be8e82caSHannes Reinecke args.ta_done = nvme_tcp_tls_done;
1631be8e82caSHannes Reinecke args.ta_data = queue;
1632be8e82caSHannes Reinecke args.ta_my_peerids[0] = pskid;
1633be8e82caSHannes Reinecke args.ta_num_peerids = 1;
1634adf22c52SHannes Reinecke if (nctrl->opts->keyring)
1635adf22c52SHannes Reinecke keyring = key_serial(nctrl->opts->keyring);
1636be8e82caSHannes Reinecke args.ta_keyring = keyring;
1637be8e82caSHannes Reinecke args.ta_timeout_ms = tls_handshake_timeout * 1000;
1638be8e82caSHannes Reinecke queue->tls_err = -EOPNOTSUPP;
1639be8e82caSHannes Reinecke init_completion(&queue->tls_complete);
1640be8e82caSHannes Reinecke ret = tls_client_hello_psk(&args, GFP_KERNEL);
1641be8e82caSHannes Reinecke if (ret) {
1642be8e82caSHannes Reinecke dev_err(nctrl->device, "queue %d: failed to start TLS: %d\n",
1643be8e82caSHannes Reinecke qid, ret);
1644be8e82caSHannes Reinecke return ret;
1645be8e82caSHannes Reinecke }
1646be8e82caSHannes Reinecke ret = wait_for_completion_interruptible_timeout(&queue->tls_complete, tmo);
1647be8e82caSHannes Reinecke if (ret <= 0) {
1648be8e82caSHannes Reinecke if (ret == 0)
1649be8e82caSHannes Reinecke ret = -ETIMEDOUT;
1650be8e82caSHannes Reinecke
1651be8e82caSHannes Reinecke dev_err(nctrl->device,
1652be8e82caSHannes Reinecke "queue %d: TLS handshake failed, error %d\n",
1653be8e82caSHannes Reinecke qid, ret);
1654be8e82caSHannes Reinecke tls_handshake_cancel(queue->sock->sk);
1655be8e82caSHannes Reinecke } else {
1656be8e82caSHannes Reinecke dev_dbg(nctrl->device,
1657be8e82caSHannes Reinecke "queue %d: TLS handshake complete, error %d\n",
1658be8e82caSHannes Reinecke qid, queue->tls_err);
1659be8e82caSHannes Reinecke ret = queue->tls_err;
1660be8e82caSHannes Reinecke }
1661be8e82caSHannes Reinecke return ret;
1662be8e82caSHannes Reinecke }
1663be8e82caSHannes Reinecke
nvme_tcp_alloc_queue(struct nvme_ctrl * nctrl,int qid,key_serial_t pskid)1664be8e82caSHannes Reinecke static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid,
1665be8e82caSHannes Reinecke key_serial_t pskid)
16663f2304f8SSagi Grimberg {
16673f2304f8SSagi Grimberg struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
16683f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = &ctrl->queues[qid];
16696ebf71baSChristoph Hellwig int ret, rcv_pdu_size;
1670e40d4eb8SHannes Reinecke struct file *sock_file;
16713f2304f8SSagi Grimberg
16729ebbfe49SChao Leng mutex_init(&queue->queue_lock);
16733f2304f8SSagi Grimberg queue->ctrl = ctrl;
167415ec928aSSagi Grimberg init_llist_head(&queue->req_list);
16753f2304f8SSagi Grimberg INIT_LIST_HEAD(&queue->send_list);
1676db5ad6b7SSagi Grimberg mutex_init(&queue->send_mutex);
16773f2304f8SSagi Grimberg INIT_WORK(&queue->io_work, nvme_tcp_io_work);
16783f2304f8SSagi Grimberg
16793f2304f8SSagi Grimberg if (qid > 0)
16809924b030SIsrael Rukshin queue->cmnd_capsule_len = nctrl->ioccsz * 16;
16813f2304f8SSagi Grimberg else
16823f2304f8SSagi Grimberg queue->cmnd_capsule_len = sizeof(struct nvme_command) +
16833f2304f8SSagi Grimberg NVME_TCP_ADMIN_CCSZ;
16843f2304f8SSagi Grimberg
16853f2304f8SSagi Grimberg ret = sock_create(ctrl->addr.ss_family, SOCK_STREAM,
16863f2304f8SSagi Grimberg IPPROTO_TCP, &queue->sock);
16873f2304f8SSagi Grimberg if (ret) {
16889924b030SIsrael Rukshin dev_err(nctrl->device,
16893f2304f8SSagi Grimberg "failed to create socket: %d\n", ret);
16909ebbfe49SChao Leng goto err_destroy_mutex;
16913f2304f8SSagi Grimberg }
16923f2304f8SSagi Grimberg
1693e40d4eb8SHannes Reinecke sock_file = sock_alloc_file(queue->sock, O_CLOEXEC, NULL);
1694e40d4eb8SHannes Reinecke if (IS_ERR(sock_file)) {
1695e40d4eb8SHannes Reinecke ret = PTR_ERR(sock_file);
1696e40d4eb8SHannes Reinecke goto err_destroy_mutex;
1697e40d4eb8SHannes Reinecke }
1698841aee4dSChris Leech nvme_tcp_reclassify_socket(queue->sock);
1699841aee4dSChris Leech
17003f2304f8SSagi Grimberg /* Single syn retry */
1701557eadfcSChristoph Hellwig tcp_sock_set_syncnt(queue->sock->sk, 1);
17023f2304f8SSagi Grimberg
17033f2304f8SSagi Grimberg /* Set TCP no delay */
170412abc5eeSChristoph Hellwig tcp_sock_set_nodelay(queue->sock->sk);
17053f2304f8SSagi Grimberg
17063f2304f8SSagi Grimberg /*
17073f2304f8SSagi Grimberg * Cleanup whatever is sitting in the TCP transmit queue on socket
17083f2304f8SSagi Grimberg * close. This is done to prevent stale data from being sent should
17093f2304f8SSagi Grimberg * the network connection be restored before TCP times out.
17103f2304f8SSagi Grimberg */
1711c433594cSChristoph Hellwig sock_no_linger(queue->sock->sk);
17123f2304f8SSagi Grimberg
17136e434967SChristoph Hellwig if (so_priority > 0)
17146e434967SChristoph Hellwig sock_set_priority(queue->sock->sk, so_priority);
17159912ade3SWunderlich, Mark
1716bb13985dSIsrael Rukshin /* Set socket type of service */
17176ebf71baSChristoph Hellwig if (nctrl->opts->tos >= 0)
17186ebf71baSChristoph Hellwig ip_sock_set_tos(queue->sock->sk, nctrl->opts->tos);
1719bb13985dSIsrael Rukshin
1720adc99fd3SSagi Grimberg /* Set 10 seconds timeout for icresp recvmsg */
1721adc99fd3SSagi Grimberg queue->sock->sk->sk_rcvtimeo = 10 * HZ;
1722adc99fd3SSagi Grimberg
17233f2304f8SSagi Grimberg queue->sock->sk->sk_allocation = GFP_ATOMIC;
172498123866SBenjamin Coddington queue->sock->sk->sk_use_task_frag = false;
172540510a63SSagi Grimberg nvme_tcp_set_queue_io_cpu(queue);
17263f2304f8SSagi Grimberg queue->request = NULL;
17273f2304f8SSagi Grimberg queue->data_remaining = 0;
17283f2304f8SSagi Grimberg queue->ddgst_remaining = 0;
17293f2304f8SSagi Grimberg queue->pdu_remaining = 0;
17303f2304f8SSagi Grimberg queue->pdu_offset = 0;
17313f2304f8SSagi Grimberg sk_set_memalloc(queue->sock->sk);
17323f2304f8SSagi Grimberg
17339924b030SIsrael Rukshin if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) {
17343f2304f8SSagi Grimberg ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr,
17353f2304f8SSagi Grimberg sizeof(ctrl->src_addr));
17363f2304f8SSagi Grimberg if (ret) {
17379924b030SIsrael Rukshin dev_err(nctrl->device,
17383f2304f8SSagi Grimberg "failed to bind queue %d socket %d\n",
17393f2304f8SSagi Grimberg qid, ret);
17403f2304f8SSagi Grimberg goto err_sock;
17413f2304f8SSagi Grimberg }
17423f2304f8SSagi Grimberg }
17433f2304f8SSagi Grimberg
17443ede8f72SMartin Belanger if (nctrl->opts->mask & NVMF_OPT_HOST_IFACE) {
17453ede8f72SMartin Belanger char *iface = nctrl->opts->host_iface;
17463ede8f72SMartin Belanger sockptr_t optval = KERNEL_SOCKPTR(iface);
17473ede8f72SMartin Belanger
17483ede8f72SMartin Belanger ret = sock_setsockopt(queue->sock, SOL_SOCKET, SO_BINDTODEVICE,
17493ede8f72SMartin Belanger optval, strlen(iface));
17503ede8f72SMartin Belanger if (ret) {
17513ede8f72SMartin Belanger dev_err(nctrl->device,
17523ede8f72SMartin Belanger "failed to bind to interface %s queue %d err %d\n",
17533ede8f72SMartin Belanger iface, qid, ret);
17543ede8f72SMartin Belanger goto err_sock;
17553ede8f72SMartin Belanger }
17563ede8f72SMartin Belanger }
17573ede8f72SMartin Belanger
17583f2304f8SSagi Grimberg queue->hdr_digest = nctrl->opts->hdr_digest;
17593f2304f8SSagi Grimberg queue->data_digest = nctrl->opts->data_digest;
17603f2304f8SSagi Grimberg if (queue->hdr_digest || queue->data_digest) {
17613f2304f8SSagi Grimberg ret = nvme_tcp_alloc_crypto(queue);
17623f2304f8SSagi Grimberg if (ret) {
17639924b030SIsrael Rukshin dev_err(nctrl->device,
17643f2304f8SSagi Grimberg "failed to allocate queue %d crypto\n", qid);
17653f2304f8SSagi Grimberg goto err_sock;
17663f2304f8SSagi Grimberg }
17673f2304f8SSagi Grimberg }
17683f2304f8SSagi Grimberg
17693f2304f8SSagi Grimberg rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) +
17703f2304f8SSagi Grimberg nvme_tcp_hdgst_len(queue);
17713f2304f8SSagi Grimberg queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL);
17723f2304f8SSagi Grimberg if (!queue->pdu) {
17733f2304f8SSagi Grimberg ret = -ENOMEM;
17743f2304f8SSagi Grimberg goto err_crypto;
17753f2304f8SSagi Grimberg }
17763f2304f8SSagi Grimberg
17779924b030SIsrael Rukshin dev_dbg(nctrl->device, "connecting queue %d\n",
17783f2304f8SSagi Grimberg nvme_tcp_queue_id(queue));
17793f2304f8SSagi Grimberg
17803f2304f8SSagi Grimberg ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr,
17813f2304f8SSagi Grimberg sizeof(ctrl->addr), 0);
17823f2304f8SSagi Grimberg if (ret) {
17839924b030SIsrael Rukshin dev_err(nctrl->device,
17843f2304f8SSagi Grimberg "failed to connect socket: %d\n", ret);
17853f2304f8SSagi Grimberg goto err_rcv_pdu;
17863f2304f8SSagi Grimberg }
17873f2304f8SSagi Grimberg
1788be8e82caSHannes Reinecke /* If PSKs are configured try to start TLS */
178936389576SHannes Reinecke if (nvme_tcp_tls_configured(nctrl) && pskid) {
1790be8e82caSHannes Reinecke ret = nvme_tcp_start_tls(nctrl, queue, pskid);
1791be8e82caSHannes Reinecke if (ret)
1792be8e82caSHannes Reinecke goto err_init_connect;
1793be8e82caSHannes Reinecke }
1794be8e82caSHannes Reinecke
17953f2304f8SSagi Grimberg ret = nvme_tcp_init_connection(queue);
17963f2304f8SSagi Grimberg if (ret)
17973f2304f8SSagi Grimberg goto err_init_connect;
17983f2304f8SSagi Grimberg
17993f2304f8SSagi Grimberg set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags);
18003f2304f8SSagi Grimberg
18013f2304f8SSagi Grimberg return 0;
18023f2304f8SSagi Grimberg
18033f2304f8SSagi Grimberg err_init_connect:
18043f2304f8SSagi Grimberg kernel_sock_shutdown(queue->sock, SHUT_RDWR);
18053f2304f8SSagi Grimberg err_rcv_pdu:
18063f2304f8SSagi Grimberg kfree(queue->pdu);
18073f2304f8SSagi Grimberg err_crypto:
18083f2304f8SSagi Grimberg if (queue->hdr_digest || queue->data_digest)
18093f2304f8SSagi Grimberg nvme_tcp_free_crypto(queue);
18103f2304f8SSagi Grimberg err_sock:
1811e40d4eb8SHannes Reinecke /* ->sock will be released by fput() */
1812e40d4eb8SHannes Reinecke fput(queue->sock->file);
18133f2304f8SSagi Grimberg queue->sock = NULL;
18149ebbfe49SChao Leng err_destroy_mutex:
1815d48f92cdSKeith Busch mutex_destroy(&queue->send_mutex);
18169ebbfe49SChao Leng mutex_destroy(&queue->queue_lock);
18173f2304f8SSagi Grimberg return ret;
18183f2304f8SSagi Grimberg }
18193f2304f8SSagi Grimberg
nvme_tcp_restore_sock_ops(struct nvme_tcp_queue * queue)182088eaba80SSagi Grimberg static void nvme_tcp_restore_sock_ops(struct nvme_tcp_queue *queue)
18213f2304f8SSagi Grimberg {
18223f2304f8SSagi Grimberg struct socket *sock = queue->sock;
18233f2304f8SSagi Grimberg
18243f2304f8SSagi Grimberg write_lock_bh(&sock->sk->sk_callback_lock);
18253f2304f8SSagi Grimberg sock->sk->sk_user_data = NULL;
18263f2304f8SSagi Grimberg sock->sk->sk_data_ready = queue->data_ready;
18273f2304f8SSagi Grimberg sock->sk->sk_state_change = queue->state_change;
18283f2304f8SSagi Grimberg sock->sk->sk_write_space = queue->write_space;
18293f2304f8SSagi Grimberg write_unlock_bh(&sock->sk->sk_callback_lock);
18303f2304f8SSagi Grimberg }
18313f2304f8SSagi Grimberg
__nvme_tcp_stop_queue(struct nvme_tcp_queue * queue)18323f2304f8SSagi Grimberg static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
18333f2304f8SSagi Grimberg {
18343f2304f8SSagi Grimberg kernel_sock_shutdown(queue->sock, SHUT_RDWR);
183588eaba80SSagi Grimberg nvme_tcp_restore_sock_ops(queue);
18363f2304f8SSagi Grimberg cancel_work_sync(&queue->io_work);
18373f2304f8SSagi Grimberg }
18383f2304f8SSagi Grimberg
nvme_tcp_stop_queue(struct nvme_ctrl * nctrl,int qid)18393f2304f8SSagi Grimberg static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
18403f2304f8SSagi Grimberg {
18413f2304f8SSagi Grimberg struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
18423f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = &ctrl->queues[qid];
18433f2304f8SSagi Grimberg
18442bff487fSMaurizio Lombardi if (!test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
18452bff487fSMaurizio Lombardi return;
18462bff487fSMaurizio Lombardi
18479ebbfe49SChao Leng mutex_lock(&queue->queue_lock);
18489ebbfe49SChao Leng if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
18493f2304f8SSagi Grimberg __nvme_tcp_stop_queue(queue);
185036389576SHannes Reinecke /* Stopping the queue will disable TLS */
185136389576SHannes Reinecke queue->tls_enabled = false;
18529ebbfe49SChao Leng mutex_unlock(&queue->queue_lock);
18533f2304f8SSagi Grimberg }
18543f2304f8SSagi Grimberg
nvme_tcp_setup_sock_ops(struct nvme_tcp_queue * queue)185588eaba80SSagi Grimberg static void nvme_tcp_setup_sock_ops(struct nvme_tcp_queue *queue)
185688eaba80SSagi Grimberg {
185788eaba80SSagi Grimberg write_lock_bh(&queue->sock->sk->sk_callback_lock);
185888eaba80SSagi Grimberg queue->sock->sk->sk_user_data = queue;
185988eaba80SSagi Grimberg queue->state_change = queue->sock->sk->sk_state_change;
186088eaba80SSagi Grimberg queue->data_ready = queue->sock->sk->sk_data_ready;
186188eaba80SSagi Grimberg queue->write_space = queue->sock->sk->sk_write_space;
186288eaba80SSagi Grimberg queue->sock->sk->sk_data_ready = nvme_tcp_data_ready;
186388eaba80SSagi Grimberg queue->sock->sk->sk_state_change = nvme_tcp_state_change;
186488eaba80SSagi Grimberg queue->sock->sk->sk_write_space = nvme_tcp_write_space;
186588eaba80SSagi Grimberg #ifdef CONFIG_NET_RX_BUSY_POLL
186688eaba80SSagi Grimberg queue->sock->sk->sk_ll_usec = 1;
186788eaba80SSagi Grimberg #endif
186888eaba80SSagi Grimberg write_unlock_bh(&queue->sock->sk->sk_callback_lock);
186988eaba80SSagi Grimberg }
187088eaba80SSagi Grimberg
nvme_tcp_start_queue(struct nvme_ctrl * nctrl,int idx)18713f2304f8SSagi Grimberg static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
18723f2304f8SSagi Grimberg {
18733f2304f8SSagi Grimberg struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
187488eaba80SSagi Grimberg struct nvme_tcp_queue *queue = &ctrl->queues[idx];
18753f2304f8SSagi Grimberg int ret;
18763f2304f8SSagi Grimberg
187788eaba80SSagi Grimberg queue->rd_enabled = true;
187888eaba80SSagi Grimberg nvme_tcp_init_recv_ctx(queue);
187988eaba80SSagi Grimberg nvme_tcp_setup_sock_ops(queue);
188088eaba80SSagi Grimberg
18813f2304f8SSagi Grimberg if (idx)
1882be42a33bSKeith Busch ret = nvmf_connect_io_queue(nctrl, idx);
18833f2304f8SSagi Grimberg else
18843f2304f8SSagi Grimberg ret = nvmf_connect_admin_queue(nctrl);
18853f2304f8SSagi Grimberg
18863f2304f8SSagi Grimberg if (!ret) {
188788eaba80SSagi Grimberg set_bit(NVME_TCP_Q_LIVE, &queue->flags);
18883f2304f8SSagi Grimberg } else {
188988eaba80SSagi Grimberg if (test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
189088eaba80SSagi Grimberg __nvme_tcp_stop_queue(queue);
18913f2304f8SSagi Grimberg dev_err(nctrl->device,
18923f2304f8SSagi Grimberg "failed to connect queue: %d ret=%d\n", idx, ret);
18933f2304f8SSagi Grimberg }
18943f2304f8SSagi Grimberg return ret;
18953f2304f8SSagi Grimberg }
18963f2304f8SSagi Grimberg
nvme_tcp_free_admin_queue(struct nvme_ctrl * ctrl)18973f2304f8SSagi Grimberg static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl)
18983f2304f8SSagi Grimberg {
18993f2304f8SSagi Grimberg if (to_tcp_ctrl(ctrl)->async_req.pdu) {
1900ceb1e087SDavid Milburn cancel_work_sync(&ctrl->async_event_work);
19013f2304f8SSagi Grimberg nvme_tcp_free_async_req(to_tcp_ctrl(ctrl));
19023f2304f8SSagi Grimberg to_tcp_ctrl(ctrl)->async_req.pdu = NULL;
19033f2304f8SSagi Grimberg }
19043f2304f8SSagi Grimberg
19053f2304f8SSagi Grimberg nvme_tcp_free_queue(ctrl, 0);
19063f2304f8SSagi Grimberg }
19073f2304f8SSagi Grimberg
nvme_tcp_free_io_queues(struct nvme_ctrl * ctrl)19083f2304f8SSagi Grimberg static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl)
19093f2304f8SSagi Grimberg {
19103f2304f8SSagi Grimberg int i;
19113f2304f8SSagi Grimberg
19123f2304f8SSagi Grimberg for (i = 1; i < ctrl->queue_count; i++)
19133f2304f8SSagi Grimberg nvme_tcp_free_queue(ctrl, i);
19143f2304f8SSagi Grimberg }
19153f2304f8SSagi Grimberg
nvme_tcp_stop_io_queues(struct nvme_ctrl * ctrl)19163f2304f8SSagi Grimberg static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl)
19173f2304f8SSagi Grimberg {
19183f2304f8SSagi Grimberg int i;
19193f2304f8SSagi Grimberg
19203f2304f8SSagi Grimberg for (i = 1; i < ctrl->queue_count; i++)
19213f2304f8SSagi Grimberg nvme_tcp_stop_queue(ctrl, i);
19223f2304f8SSagi Grimberg }
19233f2304f8SSagi Grimberg
nvme_tcp_start_io_queues(struct nvme_ctrl * ctrl,int first,int last)192409035f86SDaniel Wagner static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl,
192509035f86SDaniel Wagner int first, int last)
19263f2304f8SSagi Grimberg {
1927462b8b2dSChaitanya Kulkarni int i, ret;
19283f2304f8SSagi Grimberg
192909035f86SDaniel Wagner for (i = first; i < last; i++) {
19303f2304f8SSagi Grimberg ret = nvme_tcp_start_queue(ctrl, i);
19313f2304f8SSagi Grimberg if (ret)
19323f2304f8SSagi Grimberg goto out_stop_queues;
19333f2304f8SSagi Grimberg }
19343f2304f8SSagi Grimberg
19353f2304f8SSagi Grimberg return 0;
19363f2304f8SSagi Grimberg
19373f2304f8SSagi Grimberg out_stop_queues:
193809035f86SDaniel Wagner for (i--; i >= first; i--)
19393f2304f8SSagi Grimberg nvme_tcp_stop_queue(ctrl, i);
19403f2304f8SSagi Grimberg return ret;
19413f2304f8SSagi Grimberg }
19423f2304f8SSagi Grimberg
nvme_tcp_alloc_admin_queue(struct nvme_ctrl * ctrl)19433f2304f8SSagi Grimberg static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
19443f2304f8SSagi Grimberg {
19453f2304f8SSagi Grimberg int ret;
1946be8e82caSHannes Reinecke key_serial_t pskid = 0;
19473f2304f8SSagi Grimberg
194836389576SHannes Reinecke if (nvme_tcp_tls_configured(ctrl)) {
1949adf22c52SHannes Reinecke if (ctrl->opts->tls_key)
1950adf22c52SHannes Reinecke pskid = key_serial(ctrl->opts->tls_key);
195136389576SHannes Reinecke else {
1952adf22c52SHannes Reinecke pskid = nvme_tls_psk_default(ctrl->opts->keyring,
1953be8e82caSHannes Reinecke ctrl->opts->host->nqn,
1954be8e82caSHannes Reinecke ctrl->opts->subsysnqn);
1955be8e82caSHannes Reinecke if (!pskid) {
1956be8e82caSHannes Reinecke dev_err(ctrl->device, "no valid PSK found\n");
1957ef184b88SGuixin Liu return -ENOKEY;
1958be8e82caSHannes Reinecke }
1959be8e82caSHannes Reinecke }
196036389576SHannes Reinecke }
1961be8e82caSHannes Reinecke
1962be8e82caSHannes Reinecke ret = nvme_tcp_alloc_queue(ctrl, 0, pskid);
19633f2304f8SSagi Grimberg if (ret)
1964ef184b88SGuixin Liu return ret;
19653f2304f8SSagi Grimberg
19663f2304f8SSagi Grimberg ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
19673f2304f8SSagi Grimberg if (ret)
19683f2304f8SSagi Grimberg goto out_free_queue;
19693f2304f8SSagi Grimberg
19703f2304f8SSagi Grimberg return 0;
19713f2304f8SSagi Grimberg
19723f2304f8SSagi Grimberg out_free_queue:
19733f2304f8SSagi Grimberg nvme_tcp_free_queue(ctrl, 0);
19743f2304f8SSagi Grimberg return ret;
19753f2304f8SSagi Grimberg }
19763f2304f8SSagi Grimberg
__nvme_tcp_alloc_io_queues(struct nvme_ctrl * ctrl)1977efb973b1SSagi Grimberg static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
19783f2304f8SSagi Grimberg {
19793f2304f8SSagi Grimberg int i, ret;
19803f2304f8SSagi Grimberg
198136389576SHannes Reinecke if (nvme_tcp_tls_configured(ctrl) && !ctrl->tls_pskid) {
1982be8e82caSHannes Reinecke dev_err(ctrl->device, "no PSK negotiated\n");
1983be8e82caSHannes Reinecke return -ENOKEY;
1984be8e82caSHannes Reinecke }
198536389576SHannes Reinecke
19863f2304f8SSagi Grimberg for (i = 1; i < ctrl->queue_count; i++) {
1987be8e82caSHannes Reinecke ret = nvme_tcp_alloc_queue(ctrl, i,
198836389576SHannes Reinecke ctrl->tls_pskid);
19893f2304f8SSagi Grimberg if (ret)
19903f2304f8SSagi Grimberg goto out_free_queues;
19913f2304f8SSagi Grimberg }
19923f2304f8SSagi Grimberg
19933f2304f8SSagi Grimberg return 0;
19943f2304f8SSagi Grimberg
19953f2304f8SSagi Grimberg out_free_queues:
19963f2304f8SSagi Grimberg for (i--; i >= 1; i--)
19973f2304f8SSagi Grimberg nvme_tcp_free_queue(ctrl, i);
19983f2304f8SSagi Grimberg
19993f2304f8SSagi Grimberg return ret;
20003f2304f8SSagi Grimberg }
20013f2304f8SSagi Grimberg
nvme_tcp_alloc_io_queues(struct nvme_ctrl * ctrl)2002efb973b1SSagi Grimberg static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
20033f2304f8SSagi Grimberg {
20043f2304f8SSagi Grimberg unsigned int nr_io_queues;
20053f2304f8SSagi Grimberg int ret;
20063f2304f8SSagi Grimberg
2007a249d306SKeith Busch nr_io_queues = nvmf_nr_io_queues(ctrl->opts);
20083f2304f8SSagi Grimberg ret = nvme_set_queue_count(ctrl, &nr_io_queues);
20093f2304f8SSagi Grimberg if (ret)
20103f2304f8SSagi Grimberg return ret;
20113f2304f8SSagi Grimberg
2012664227fdSRuozhu Li if (nr_io_queues == 0) {
201372f57242SSagi Grimberg dev_err(ctrl->device,
201472f57242SSagi Grimberg "unable to set any I/O queues\n");
201572f57242SSagi Grimberg return -ENOMEM;
201672f57242SSagi Grimberg }
20173f2304f8SSagi Grimberg
2018664227fdSRuozhu Li ctrl->queue_count = nr_io_queues + 1;
20193f2304f8SSagi Grimberg dev_info(ctrl->device,
20203f2304f8SSagi Grimberg "creating %d I/O queues.\n", nr_io_queues);
20213f2304f8SSagi Grimberg
2022a249d306SKeith Busch nvmf_set_io_queues(ctrl->opts, nr_io_queues,
2023a249d306SKeith Busch to_tcp_ctrl(ctrl)->io_queues);
2024efb973b1SSagi Grimberg return __nvme_tcp_alloc_io_queues(ctrl);
20253f2304f8SSagi Grimberg }
20263f2304f8SSagi Grimberg
nvme_tcp_destroy_io_queues(struct nvme_ctrl * ctrl,bool remove)20273f2304f8SSagi Grimberg static void nvme_tcp_destroy_io_queues(struct nvme_ctrl *ctrl, bool remove)
20283f2304f8SSagi Grimberg {
20293f2304f8SSagi Grimberg nvme_tcp_stop_io_queues(ctrl);
2030de777825SChristoph Hellwig if (remove)
2031de777825SChristoph Hellwig nvme_remove_io_tag_set(ctrl);
20323f2304f8SSagi Grimberg nvme_tcp_free_io_queues(ctrl);
20333f2304f8SSagi Grimberg }
20343f2304f8SSagi Grimberg
nvme_tcp_configure_io_queues(struct nvme_ctrl * ctrl,bool new)20353f2304f8SSagi Grimberg static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
20363f2304f8SSagi Grimberg {
203709035f86SDaniel Wagner int ret, nr_queues;
20383f2304f8SSagi Grimberg
2039efb973b1SSagi Grimberg ret = nvme_tcp_alloc_io_queues(ctrl);
20403f2304f8SSagi Grimberg if (ret)
20413f2304f8SSagi Grimberg return ret;
20423f2304f8SSagi Grimberg
20433f2304f8SSagi Grimberg if (new) {
2044de777825SChristoph Hellwig ret = nvme_alloc_io_tag_set(ctrl, &to_tcp_ctrl(ctrl)->tag_set,
2045de777825SChristoph Hellwig &nvme_tcp_mq_ops,
2046dcef7727SChristoph Hellwig ctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2,
2047de777825SChristoph Hellwig sizeof(struct nvme_tcp_request));
20482f7a7e5dSChristoph Hellwig if (ret)
20493f2304f8SSagi Grimberg goto out_free_io_queues;
20503f2304f8SSagi Grimberg }
20513f2304f8SSagi Grimberg
205209035f86SDaniel Wagner /*
205309035f86SDaniel Wagner * Only start IO queues for which we have allocated the tagset
205409035f86SDaniel Wagner * and limitted it to the available queues. On reconnects, the
205509035f86SDaniel Wagner * queue number might have changed.
205609035f86SDaniel Wagner */
205709035f86SDaniel Wagner nr_queues = min(ctrl->tagset->nr_hw_queues + 1, ctrl->queue_count);
205809035f86SDaniel Wagner ret = nvme_tcp_start_io_queues(ctrl, 1, nr_queues);
20593f2304f8SSagi Grimberg if (ret)
20603f2304f8SSagi Grimberg goto out_cleanup_connect_q;
20613f2304f8SSagi Grimberg
20622875b0aeSSagi Grimberg if (!new) {
206399dc2640SMing Lei nvme_start_freeze(ctrl);
20649f27bd70SChristoph Hellwig nvme_unquiesce_io_queues(ctrl);
2065e5c01f4fSSagi Grimberg if (!nvme_wait_freeze_timeout(ctrl, NVME_IO_TIMEOUT)) {
2066e5c01f4fSSagi Grimberg /*
2067e5c01f4fSSagi Grimberg * If we timed out waiting for freeze we are likely to
2068e5c01f4fSSagi Grimberg * be stuck. Fail the controller initialization just
2069e5c01f4fSSagi Grimberg * to be safe.
2070e5c01f4fSSagi Grimberg */
2071e5c01f4fSSagi Grimberg ret = -ENODEV;
207299dc2640SMing Lei nvme_unfreeze(ctrl);
2073e5c01f4fSSagi Grimberg goto out_wait_freeze_timed_out;
2074e5c01f4fSSagi Grimberg }
20752875b0aeSSagi Grimberg blk_mq_update_nr_hw_queues(ctrl->tagset,
20762875b0aeSSagi Grimberg ctrl->queue_count - 1);
20772875b0aeSSagi Grimberg nvme_unfreeze(ctrl);
20782875b0aeSSagi Grimberg }
20792875b0aeSSagi Grimberg
208009035f86SDaniel Wagner /*
208109035f86SDaniel Wagner * If the number of queues has increased (reconnect case)
208209035f86SDaniel Wagner * start all new queues now.
208309035f86SDaniel Wagner */
208409035f86SDaniel Wagner ret = nvme_tcp_start_io_queues(ctrl, nr_queues,
208509035f86SDaniel Wagner ctrl->tagset->nr_hw_queues + 1);
208609035f86SDaniel Wagner if (ret)
208709035f86SDaniel Wagner goto out_wait_freeze_timed_out;
208809035f86SDaniel Wagner
20893f2304f8SSagi Grimberg return 0;
20903f2304f8SSagi Grimberg
2091e5c01f4fSSagi Grimberg out_wait_freeze_timed_out:
20929f27bd70SChristoph Hellwig nvme_quiesce_io_queues(ctrl);
209370a99574SChao Leng nvme_sync_io_queues(ctrl);
2094e5c01f4fSSagi Grimberg nvme_tcp_stop_io_queues(ctrl);
20953f2304f8SSagi Grimberg out_cleanup_connect_q:
209670a99574SChao Leng nvme_cancel_tagset(ctrl);
2097e85037a2SSagi Grimberg if (new)
2098de777825SChristoph Hellwig nvme_remove_io_tag_set(ctrl);
20993f2304f8SSagi Grimberg out_free_io_queues:
21003f2304f8SSagi Grimberg nvme_tcp_free_io_queues(ctrl);
21013f2304f8SSagi Grimberg return ret;
21023f2304f8SSagi Grimberg }
21033f2304f8SSagi Grimberg
nvme_tcp_destroy_admin_queue(struct nvme_ctrl * ctrl,bool remove)21043f2304f8SSagi Grimberg static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl *ctrl, bool remove)
21053f2304f8SSagi Grimberg {
21063f2304f8SSagi Grimberg nvme_tcp_stop_queue(ctrl, 0);
2107de777825SChristoph Hellwig if (remove)
2108de777825SChristoph Hellwig nvme_remove_admin_tag_set(ctrl);
21093f2304f8SSagi Grimberg nvme_tcp_free_admin_queue(ctrl);
21103f2304f8SSagi Grimberg }
21113f2304f8SSagi Grimberg
nvme_tcp_configure_admin_queue(struct nvme_ctrl * ctrl,bool new)21123f2304f8SSagi Grimberg static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
21133f2304f8SSagi Grimberg {
21143f2304f8SSagi Grimberg int error;
21153f2304f8SSagi Grimberg
21163f2304f8SSagi Grimberg error = nvme_tcp_alloc_admin_queue(ctrl);
21173f2304f8SSagi Grimberg if (error)
21183f2304f8SSagi Grimberg return error;
21193f2304f8SSagi Grimberg
21203f2304f8SSagi Grimberg if (new) {
2121de777825SChristoph Hellwig error = nvme_alloc_admin_tag_set(ctrl,
2122de777825SChristoph Hellwig &to_tcp_ctrl(ctrl)->admin_tag_set,
2123db45e1a5SChristoph Hellwig &nvme_tcp_admin_mq_ops,
2124de777825SChristoph Hellwig sizeof(struct nvme_tcp_request));
21252f7a7e5dSChristoph Hellwig if (error)
21263f2304f8SSagi Grimberg goto out_free_queue;
21273f2304f8SSagi Grimberg }
21283f2304f8SSagi Grimberg
21293f2304f8SSagi Grimberg error = nvme_tcp_start_queue(ctrl, 0);
21303f2304f8SSagi Grimberg if (error)
2131de777825SChristoph Hellwig goto out_cleanup_tagset;
21323f2304f8SSagi Grimberg
2133c0f2f45bSSagi Grimberg error = nvme_enable_ctrl(ctrl);
21343f2304f8SSagi Grimberg if (error)
21353f2304f8SSagi Grimberg goto out_stop_queue;
21363f2304f8SSagi Grimberg
21379f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(ctrl);
2138e7832cb4SSagi Grimberg
213994cc781fSChristoph Hellwig error = nvme_init_ctrl_finish(ctrl, false);
21403f2304f8SSagi Grimberg if (error)
214170a99574SChao Leng goto out_quiesce_queue;
21423f2304f8SSagi Grimberg
21433f2304f8SSagi Grimberg return 0;
21443f2304f8SSagi Grimberg
214570a99574SChao Leng out_quiesce_queue:
21469f27bd70SChristoph Hellwig nvme_quiesce_admin_queue(ctrl);
214770a99574SChao Leng blk_sync_queue(ctrl->admin_q);
21483f2304f8SSagi Grimberg out_stop_queue:
21493f2304f8SSagi Grimberg nvme_tcp_stop_queue(ctrl, 0);
215070a99574SChao Leng nvme_cancel_admin_tagset(ctrl);
2151de777825SChristoph Hellwig out_cleanup_tagset:
21523f2304f8SSagi Grimberg if (new)
2153de777825SChristoph Hellwig nvme_remove_admin_tag_set(ctrl);
21543f2304f8SSagi Grimberg out_free_queue:
21553f2304f8SSagi Grimberg nvme_tcp_free_admin_queue(ctrl);
21563f2304f8SSagi Grimberg return error;
21573f2304f8SSagi Grimberg }
21583f2304f8SSagi Grimberg
nvme_tcp_teardown_admin_queue(struct nvme_ctrl * ctrl,bool remove)21593f2304f8SSagi Grimberg static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
21603f2304f8SSagi Grimberg bool remove)
21613f2304f8SSagi Grimberg {
21629f27bd70SChristoph Hellwig nvme_quiesce_admin_queue(ctrl);
2163d6f66210SChao Leng blk_sync_queue(ctrl->admin_q);
21643f2304f8SSagi Grimberg nvme_tcp_stop_queue(ctrl, 0);
2165563c8158SChao Leng nvme_cancel_admin_tagset(ctrl);
2166e7832cb4SSagi Grimberg if (remove)
21679f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(ctrl);
21683f2304f8SSagi Grimberg nvme_tcp_destroy_admin_queue(ctrl, remove);
216936389576SHannes Reinecke if (ctrl->tls_pskid) {
217036389576SHannes Reinecke dev_dbg(ctrl->device, "Wipe negotiated TLS_PSK %08x\n",
217136389576SHannes Reinecke ctrl->tls_pskid);
217236389576SHannes Reinecke ctrl->tls_pskid = 0;
217336389576SHannes Reinecke }
21743f2304f8SSagi Grimberg }
21753f2304f8SSagi Grimberg
nvme_tcp_teardown_io_queues(struct nvme_ctrl * ctrl,bool remove)21763f2304f8SSagi Grimberg static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
21773f2304f8SSagi Grimberg bool remove)
21783f2304f8SSagi Grimberg {
21793f2304f8SSagi Grimberg if (ctrl->queue_count <= 1)
2180d6f66210SChao Leng return;
21819f27bd70SChristoph Hellwig nvme_quiesce_admin_queue(ctrl);
21829f27bd70SChristoph Hellwig nvme_quiesce_io_queues(ctrl);
2183d6f66210SChao Leng nvme_sync_io_queues(ctrl);
21843f2304f8SSagi Grimberg nvme_tcp_stop_io_queues(ctrl);
2185563c8158SChao Leng nvme_cancel_tagset(ctrl);
21863f2304f8SSagi Grimberg if (remove)
21879f27bd70SChristoph Hellwig nvme_unquiesce_io_queues(ctrl);
21883f2304f8SSagi Grimberg nvme_tcp_destroy_io_queues(ctrl, remove);
21893f2304f8SSagi Grimberg }
21903f2304f8SSagi Grimberg
nvme_tcp_reconnect_or_remove(struct nvme_ctrl * ctrl,int status)2191adfde7edSHannes Reinecke static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl,
2192adfde7edSHannes Reinecke int status)
21933f2304f8SSagi Grimberg {
2194e6e7f7acSKeith Busch enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
2195e6e7f7acSKeith Busch
21963f2304f8SSagi Grimberg /* If we are resetting/deleting then do nothing */
2197e6e7f7acSKeith Busch if (state != NVME_CTRL_CONNECTING) {
2198e6e7f7acSKeith Busch WARN_ON_ONCE(state == NVME_CTRL_NEW || state == NVME_CTRL_LIVE);
21993f2304f8SSagi Grimberg return;
22003f2304f8SSagi Grimberg }
22013f2304f8SSagi Grimberg
2202adfde7edSHannes Reinecke if (nvmf_should_reconnect(ctrl, status)) {
22033f2304f8SSagi Grimberg dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
22043f2304f8SSagi Grimberg ctrl->opts->reconnect_delay);
22053f2304f8SSagi Grimberg queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
22063f2304f8SSagi Grimberg ctrl->opts->reconnect_delay * HZ);
22073f2304f8SSagi Grimberg } else {
2208adfde7edSHannes Reinecke dev_info(ctrl->device, "Removing controller (%d)...\n",
2209adfde7edSHannes Reinecke status);
22103f2304f8SSagi Grimberg nvme_delete_ctrl(ctrl);
22113f2304f8SSagi Grimberg }
22123f2304f8SSagi Grimberg }
22133f2304f8SSagi Grimberg
nvme_tcp_setup_ctrl(struct nvme_ctrl * ctrl,bool new)22143f2304f8SSagi Grimberg static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
22153f2304f8SSagi Grimberg {
22163f2304f8SSagi Grimberg struct nvmf_ctrl_options *opts = ctrl->opts;
2217312910f4SColin Ian King int ret;
22183f2304f8SSagi Grimberg
22193f2304f8SSagi Grimberg ret = nvme_tcp_configure_admin_queue(ctrl, new);
22203f2304f8SSagi Grimberg if (ret)
22213f2304f8SSagi Grimberg return ret;
22223f2304f8SSagi Grimberg
22233f2304f8SSagi Grimberg if (ctrl->icdoff) {
2224522af60cSDan Carpenter ret = -EOPNOTSUPP;
22253f2304f8SSagi Grimberg dev_err(ctrl->device, "icdoff is not supported!\n");
22263f2304f8SSagi Grimberg goto destroy_admin;
22273f2304f8SSagi Grimberg }
22283f2304f8SSagi Grimberg
22293b54064fSChaitanya Kulkarni if (!nvme_ctrl_sgl_supported(ctrl)) {
2230522af60cSDan Carpenter ret = -EOPNOTSUPP;
223173ffcefcSMax Gurtovoy dev_err(ctrl->device, "Mandatory sgls are not supported!\n");
223273ffcefcSMax Gurtovoy goto destroy_admin;
223373ffcefcSMax Gurtovoy }
223473ffcefcSMax Gurtovoy
22353f2304f8SSagi Grimberg if (opts->queue_size > ctrl->sqsize + 1)
22363f2304f8SSagi Grimberg dev_warn(ctrl->device,
22373f2304f8SSagi Grimberg "queue_size %zu > ctrl sqsize %u, clamping down\n",
22383f2304f8SSagi Grimberg opts->queue_size, ctrl->sqsize + 1);
22393f2304f8SSagi Grimberg
22403f2304f8SSagi Grimberg if (ctrl->sqsize + 1 > ctrl->maxcmd) {
22413f2304f8SSagi Grimberg dev_warn(ctrl->device,
22423f2304f8SSagi Grimberg "sqsize %u > ctrl maxcmd %u, clamping down\n",
22433f2304f8SSagi Grimberg ctrl->sqsize + 1, ctrl->maxcmd);
22443f2304f8SSagi Grimberg ctrl->sqsize = ctrl->maxcmd - 1;
22453f2304f8SSagi Grimberg }
22463f2304f8SSagi Grimberg
22473f2304f8SSagi Grimberg if (ctrl->queue_count > 1) {
22483f2304f8SSagi Grimberg ret = nvme_tcp_configure_io_queues(ctrl, new);
22493f2304f8SSagi Grimberg if (ret)
22503f2304f8SSagi Grimberg goto destroy_admin;
22513f2304f8SSagi Grimberg }
22523f2304f8SSagi Grimberg
22533f2304f8SSagi Grimberg if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
2254bea54ef5SIsrael Rukshin /*
2255ecca390eSSagi Grimberg * state change failure is ok if we started ctrl delete,
2256bea54ef5SIsrael Rukshin * unless we're during creation of a new controller to
2257bea54ef5SIsrael Rukshin * avoid races with teardown flow.
2258bea54ef5SIsrael Rukshin */
2259e6e7f7acSKeith Busch enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
2260e6e7f7acSKeith Busch
2261e6e7f7acSKeith Busch WARN_ON_ONCE(state != NVME_CTRL_DELETING &&
2262e6e7f7acSKeith Busch state != NVME_CTRL_DELETING_NOIO);
2263bea54ef5SIsrael Rukshin WARN_ON_ONCE(new);
22643f2304f8SSagi Grimberg ret = -EINVAL;
22653f2304f8SSagi Grimberg goto destroy_io;
22663f2304f8SSagi Grimberg }
22673f2304f8SSagi Grimberg
22683f2304f8SSagi Grimberg nvme_start_ctrl(ctrl);
22693f2304f8SSagi Grimberg return 0;
22703f2304f8SSagi Grimberg
22713f2304f8SSagi Grimberg destroy_io:
227270a99574SChao Leng if (ctrl->queue_count > 1) {
22739f27bd70SChristoph Hellwig nvme_quiesce_io_queues(ctrl);
227470a99574SChao Leng nvme_sync_io_queues(ctrl);
227570a99574SChao Leng nvme_tcp_stop_io_queues(ctrl);
227670a99574SChao Leng nvme_cancel_tagset(ctrl);
22773f2304f8SSagi Grimberg nvme_tcp_destroy_io_queues(ctrl, new);
227870a99574SChao Leng }
22793f2304f8SSagi Grimberg destroy_admin:
22803af755a4SHannes Reinecke nvme_stop_keep_alive(ctrl);
2281fd1418deSHannes Reinecke nvme_tcp_teardown_admin_queue(ctrl, false);
22823f2304f8SSagi Grimberg return ret;
22833f2304f8SSagi Grimberg }
22843f2304f8SSagi Grimberg
nvme_tcp_reconnect_ctrl_work(struct work_struct * work)22853f2304f8SSagi Grimberg static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
22863f2304f8SSagi Grimberg {
22873f2304f8SSagi Grimberg struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
22883f2304f8SSagi Grimberg struct nvme_tcp_ctrl, connect_work);
22893f2304f8SSagi Grimberg struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
2290adfde7edSHannes Reinecke int ret;
22913f2304f8SSagi Grimberg
22923f2304f8SSagi Grimberg ++ctrl->nr_reconnects;
22933f2304f8SSagi Grimberg
2294adfde7edSHannes Reinecke ret = nvme_tcp_setup_ctrl(ctrl, false);
2295adfde7edSHannes Reinecke if (ret)
22963f2304f8SSagi Grimberg goto requeue;
22973f2304f8SSagi Grimberg
229854a76c87STokunori Ikegami dev_info(ctrl->device, "Successfully reconnected (attempt %d/%d)\n",
229954a76c87STokunori Ikegami ctrl->nr_reconnects, ctrl->opts->max_reconnects);
23003f2304f8SSagi Grimberg
23013f2304f8SSagi Grimberg ctrl->nr_reconnects = 0;
23023f2304f8SSagi Grimberg
23033f2304f8SSagi Grimberg return;
23043f2304f8SSagi Grimberg
23053f2304f8SSagi Grimberg requeue:
230654a76c87STokunori Ikegami dev_info(ctrl->device, "Failed reconnect attempt %d/%d\n",
230754a76c87STokunori Ikegami ctrl->nr_reconnects, ctrl->opts->max_reconnects);
2308adfde7edSHannes Reinecke nvme_tcp_reconnect_or_remove(ctrl, ret);
23093f2304f8SSagi Grimberg }
23103f2304f8SSagi Grimberg
nvme_tcp_error_recovery_work(struct work_struct * work)23113f2304f8SSagi Grimberg static void nvme_tcp_error_recovery_work(struct work_struct *work)
23123f2304f8SSagi Grimberg {
23133f2304f8SSagi Grimberg struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
23143f2304f8SSagi Grimberg struct nvme_tcp_ctrl, err_work);
23153f2304f8SSagi Grimberg struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
23163f2304f8SSagi Grimberg
23173f2304f8SSagi Grimberg nvme_stop_keep_alive(ctrl);
2318ff9fc7ebSSagi Grimberg flush_work(&ctrl->async_event_work);
23193f2304f8SSagi Grimberg nvme_tcp_teardown_io_queues(ctrl, false);
23203f2304f8SSagi Grimberg /* unquiesce to fail fast pending requests */
23219f27bd70SChristoph Hellwig nvme_unquiesce_io_queues(ctrl);
23223f2304f8SSagi Grimberg nvme_tcp_teardown_admin_queue(ctrl, false);
23239f27bd70SChristoph Hellwig nvme_unquiesce_admin_queue(ctrl);
23241f1a4f89SSagi Grimberg nvme_auth_stop(ctrl);
23253f2304f8SSagi Grimberg
23263f2304f8SSagi Grimberg if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2327ecca390eSSagi Grimberg /* state change failure is ok if we started ctrl delete */
2328e6e7f7acSKeith Busch enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
2329e6e7f7acSKeith Busch
2330e6e7f7acSKeith Busch WARN_ON_ONCE(state != NVME_CTRL_DELETING &&
2331e6e7f7acSKeith Busch state != NVME_CTRL_DELETING_NOIO);
23323f2304f8SSagi Grimberg return;
23333f2304f8SSagi Grimberg }
23343f2304f8SSagi Grimberg
2335adfde7edSHannes Reinecke nvme_tcp_reconnect_or_remove(ctrl, 0);
23363f2304f8SSagi Grimberg }
23373f2304f8SSagi Grimberg
nvme_tcp_teardown_ctrl(struct nvme_ctrl * ctrl,bool shutdown)23383f2304f8SSagi Grimberg static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
23393f2304f8SSagi Grimberg {
23403f2304f8SSagi Grimberg nvme_tcp_teardown_io_queues(ctrl, shutdown);
23419f27bd70SChristoph Hellwig nvme_quiesce_admin_queue(ctrl);
2342285b6e9bSChristoph Hellwig nvme_disable_ctrl(ctrl, shutdown);
23433f2304f8SSagi Grimberg nvme_tcp_teardown_admin_queue(ctrl, shutdown);
23443f2304f8SSagi Grimberg }
23453f2304f8SSagi Grimberg
nvme_tcp_delete_ctrl(struct nvme_ctrl * ctrl)23463f2304f8SSagi Grimberg static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
23473f2304f8SSagi Grimberg {
23483f2304f8SSagi Grimberg nvme_tcp_teardown_ctrl(ctrl, true);
23493f2304f8SSagi Grimberg }
23503f2304f8SSagi Grimberg
nvme_reset_ctrl_work(struct work_struct * work)23513f2304f8SSagi Grimberg static void nvme_reset_ctrl_work(struct work_struct *work)
23523f2304f8SSagi Grimberg {
23533f2304f8SSagi Grimberg struct nvme_ctrl *ctrl =
23543f2304f8SSagi Grimberg container_of(work, struct nvme_ctrl, reset_work);
2355adfde7edSHannes Reinecke int ret;
23563f2304f8SSagi Grimberg
23573f2304f8SSagi Grimberg nvme_stop_ctrl(ctrl);
23583f2304f8SSagi Grimberg nvme_tcp_teardown_ctrl(ctrl, false);
23593f2304f8SSagi Grimberg
23603f2304f8SSagi Grimberg if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
2361ecca390eSSagi Grimberg /* state change failure is ok if we started ctrl delete */
2362e6e7f7acSKeith Busch enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
2363e6e7f7acSKeith Busch
2364e6e7f7acSKeith Busch WARN_ON_ONCE(state != NVME_CTRL_DELETING &&
2365e6e7f7acSKeith Busch state != NVME_CTRL_DELETING_NOIO);
23663f2304f8SSagi Grimberg return;
23673f2304f8SSagi Grimberg }
23683f2304f8SSagi Grimberg
2369adfde7edSHannes Reinecke ret = nvme_tcp_setup_ctrl(ctrl, false);
2370adfde7edSHannes Reinecke if (ret)
23713f2304f8SSagi Grimberg goto out_fail;
23723f2304f8SSagi Grimberg
23733f2304f8SSagi Grimberg return;
23743f2304f8SSagi Grimberg
23753f2304f8SSagi Grimberg out_fail:
23763f2304f8SSagi Grimberg ++ctrl->nr_reconnects;
2377adfde7edSHannes Reinecke nvme_tcp_reconnect_or_remove(ctrl, ret);
23783f2304f8SSagi Grimberg }
23793f2304f8SSagi Grimberg
nvme_tcp_stop_ctrl(struct nvme_ctrl * ctrl)2380f7f70f4aSRuozhu Li static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl)
2381f7f70f4aSRuozhu Li {
2382c4abd875SSagi Grimberg flush_work(&to_tcp_ctrl(ctrl)->err_work);
2383f7f70f4aSRuozhu Li cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
2384f7f70f4aSRuozhu Li }
2385f7f70f4aSRuozhu Li
nvme_tcp_free_ctrl(struct nvme_ctrl * nctrl)23863f2304f8SSagi Grimberg static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
23873f2304f8SSagi Grimberg {
23883f2304f8SSagi Grimberg struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
23893f2304f8SSagi Grimberg
23903f2304f8SSagi Grimberg if (list_empty(&ctrl->list))
23913f2304f8SSagi Grimberg goto free_ctrl;
23923f2304f8SSagi Grimberg
23933f2304f8SSagi Grimberg mutex_lock(&nvme_tcp_ctrl_mutex);
23943f2304f8SSagi Grimberg list_del(&ctrl->list);
23953f2304f8SSagi Grimberg mutex_unlock(&nvme_tcp_ctrl_mutex);
23963f2304f8SSagi Grimberg
23973f2304f8SSagi Grimberg nvmf_free_options(nctrl->opts);
23983f2304f8SSagi Grimberg free_ctrl:
23993f2304f8SSagi Grimberg kfree(ctrl->queues);
24003f2304f8SSagi Grimberg kfree(ctrl);
24013f2304f8SSagi Grimberg }
24023f2304f8SSagi Grimberg
nvme_tcp_set_sg_null(struct nvme_command * c)24033f2304f8SSagi Grimberg static void nvme_tcp_set_sg_null(struct nvme_command *c)
24043f2304f8SSagi Grimberg {
24053f2304f8SSagi Grimberg struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
24063f2304f8SSagi Grimberg
24073f2304f8SSagi Grimberg sg->addr = 0;
24083f2304f8SSagi Grimberg sg->length = 0;
24093f2304f8SSagi Grimberg sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
24103f2304f8SSagi Grimberg NVME_SGL_FMT_TRANSPORT_A;
24113f2304f8SSagi Grimberg }
24123f2304f8SSagi Grimberg
nvme_tcp_set_sg_inline(struct nvme_tcp_queue * queue,struct nvme_command * c,u32 data_len)24133f2304f8SSagi Grimberg static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
24143f2304f8SSagi Grimberg struct nvme_command *c, u32 data_len)
24153f2304f8SSagi Grimberg {
24163f2304f8SSagi Grimberg struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
24173f2304f8SSagi Grimberg
24183f2304f8SSagi Grimberg sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
24193f2304f8SSagi Grimberg sg->length = cpu_to_le32(data_len);
24203f2304f8SSagi Grimberg sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
24213f2304f8SSagi Grimberg }
24223f2304f8SSagi Grimberg
nvme_tcp_set_sg_host_data(struct nvme_command * c,u32 data_len)24233f2304f8SSagi Grimberg static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
24243f2304f8SSagi Grimberg u32 data_len)
24253f2304f8SSagi Grimberg {
24263f2304f8SSagi Grimberg struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
24273f2304f8SSagi Grimberg
24283f2304f8SSagi Grimberg sg->addr = 0;
24293f2304f8SSagi Grimberg sg->length = cpu_to_le32(data_len);
24303f2304f8SSagi Grimberg sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
24313f2304f8SSagi Grimberg NVME_SGL_FMT_TRANSPORT_A;
24323f2304f8SSagi Grimberg }
24333f2304f8SSagi Grimberg
nvme_tcp_submit_async_event(struct nvme_ctrl * arg)24343f2304f8SSagi Grimberg static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
24353f2304f8SSagi Grimberg {
24363f2304f8SSagi Grimberg struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
24373f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = &ctrl->queues[0];
24383f2304f8SSagi Grimberg struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
24393f2304f8SSagi Grimberg struct nvme_command *cmd = &pdu->cmd;
24403f2304f8SSagi Grimberg u8 hdgst = nvme_tcp_hdgst_len(queue);
24413f2304f8SSagi Grimberg
24423f2304f8SSagi Grimberg memset(pdu, 0, sizeof(*pdu));
24433f2304f8SSagi Grimberg pdu->hdr.type = nvme_tcp_cmd;
24443f2304f8SSagi Grimberg if (queue->hdr_digest)
24453f2304f8SSagi Grimberg pdu->hdr.flags |= NVME_TCP_F_HDGST;
24463f2304f8SSagi Grimberg pdu->hdr.hlen = sizeof(*pdu);
24473f2304f8SSagi Grimberg pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
24483f2304f8SSagi Grimberg
24493f2304f8SSagi Grimberg cmd->common.opcode = nvme_admin_async_event;
24503f2304f8SSagi Grimberg cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
24513f2304f8SSagi Grimberg cmd->common.flags |= NVME_CMD_SGL_METABUF;
24523f2304f8SSagi Grimberg nvme_tcp_set_sg_null(cmd);
24533f2304f8SSagi Grimberg
24543f2304f8SSagi Grimberg ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
24553f2304f8SSagi Grimberg ctrl->async_req.offset = 0;
24563f2304f8SSagi Grimberg ctrl->async_req.curr_bio = NULL;
24573f2304f8SSagi Grimberg ctrl->async_req.data_len = 0;
24583f2304f8SSagi Grimberg
245986f0348aSSagi Grimberg nvme_tcp_queue_request(&ctrl->async_req, true, true);
24603f2304f8SSagi Grimberg }
24613f2304f8SSagi Grimberg
nvme_tcp_complete_timed_out(struct request * rq)2462236187c4SSagi Grimberg static void nvme_tcp_complete_timed_out(struct request *rq)
2463236187c4SSagi Grimberg {
2464236187c4SSagi Grimberg struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2465236187c4SSagi Grimberg struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
2466236187c4SSagi Grimberg
2467236187c4SSagi Grimberg nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
246893ba75c9SChaitanya Kulkarni nvmf_complete_timed_out_request(rq);
2469236187c4SSagi Grimberg }
2470236187c4SSagi Grimberg
nvme_tcp_timeout(struct request * rq)24719bdb4833SJohn Garry static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq)
24723f2304f8SSagi Grimberg {
24733f2304f8SSagi Grimberg struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2474236187c4SSagi Grimberg struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
2475a3406352SSagi Grimberg struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
24767d23e836SCaleb Sander struct nvme_command *cmd = &pdu->cmd;
247799607843SAmit Engel int qid = nvme_tcp_queue_id(req->queue);
24783f2304f8SSagi Grimberg
2479236187c4SSagi Grimberg dev_warn(ctrl->device,
248045c36f04SMax Gurtovoy "I/O tag %d (%04x) type %d opcode %#x (%s) QID %d timeout\n",
24817d23e836SCaleb Sander rq->tag, nvme_cid(rq), pdu->hdr.type, cmd->common.opcode,
24827d23e836SCaleb Sander nvme_fabrics_opcode_str(qid, cmd), qid);
24833f2304f8SSagi Grimberg
2484e6e7f7acSKeith Busch if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) {
248539d57757SSagi Grimberg /*
2486236187c4SSagi Grimberg * If we are resetting, connecting or deleting we should
2487236187c4SSagi Grimberg * complete immediately because we may block controller
2488236187c4SSagi Grimberg * teardown or setup sequence
2489236187c4SSagi Grimberg * - ctrl disable/shutdown fabrics requests
2490236187c4SSagi Grimberg * - connect requests
2491236187c4SSagi Grimberg * - initialization admin requests
2492236187c4SSagi Grimberg * - I/O requests that entered after unquiescing and
2493236187c4SSagi Grimberg * the controller stopped responding
2494236187c4SSagi Grimberg *
2495236187c4SSagi Grimberg * All other requests should be cancelled by the error
2496236187c4SSagi Grimberg * recovery work, so it's fine that we fail it here.
249739d57757SSagi Grimberg */
2498236187c4SSagi Grimberg nvme_tcp_complete_timed_out(rq);
24993f2304f8SSagi Grimberg return BLK_EH_DONE;
25003f2304f8SSagi Grimberg }
25013f2304f8SSagi Grimberg
2502236187c4SSagi Grimberg /*
2503236187c4SSagi Grimberg * LIVE state should trigger the normal error recovery which will
2504236187c4SSagi Grimberg * handle completing this request.
2505236187c4SSagi Grimberg */
2506236187c4SSagi Grimberg nvme_tcp_error_recovery(ctrl);
25073f2304f8SSagi Grimberg return BLK_EH_RESET_TIMER;
25083f2304f8SSagi Grimberg }
25093f2304f8SSagi Grimberg
nvme_tcp_map_data(struct nvme_tcp_queue * queue,struct request * rq)25103f2304f8SSagi Grimberg static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
25113f2304f8SSagi Grimberg struct request *rq)
25123f2304f8SSagi Grimberg {
25133f2304f8SSagi Grimberg struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2514a3406352SSagi Grimberg struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
25153f2304f8SSagi Grimberg struct nvme_command *c = &pdu->cmd;
25163f2304f8SSagi Grimberg
25173f2304f8SSagi Grimberg c->common.flags |= NVME_CMD_SGL_METABUF;
25183f2304f8SSagi Grimberg
251925e5cb78SSagi Grimberg if (!blk_rq_nr_phys_segments(rq))
252025e5cb78SSagi Grimberg nvme_tcp_set_sg_null(c);
252125e5cb78SSagi Grimberg else if (rq_data_dir(rq) == WRITE &&
252253ee9e29SCaleb Sander req->data_len <= nvme_tcp_inline_data_size(req))
25233f2304f8SSagi Grimberg nvme_tcp_set_sg_inline(queue, c, req->data_len);
25243f2304f8SSagi Grimberg else
25253f2304f8SSagi Grimberg nvme_tcp_set_sg_host_data(c, req->data_len);
25263f2304f8SSagi Grimberg
25273f2304f8SSagi Grimberg return 0;
25283f2304f8SSagi Grimberg }
25293f2304f8SSagi Grimberg
nvme_tcp_setup_cmd_pdu(struct nvme_ns * ns,struct request * rq)25303f2304f8SSagi Grimberg static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
25313f2304f8SSagi Grimberg struct request *rq)
25323f2304f8SSagi Grimberg {
25333f2304f8SSagi Grimberg struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
2534a3406352SSagi Grimberg struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
25353f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = req->queue;
25363f2304f8SSagi Grimberg u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
25373f2304f8SSagi Grimberg blk_status_t ret;
25383f2304f8SSagi Grimberg
2539f4b9e6c9SKeith Busch ret = nvme_setup_cmd(ns, rq);
25403f2304f8SSagi Grimberg if (ret)
25413f2304f8SSagi Grimberg return ret;
25423f2304f8SSagi Grimberg
25433f2304f8SSagi Grimberg req->state = NVME_TCP_SEND_CMD_PDU;
25441ba2e507SDaniel Wagner req->status = cpu_to_le16(NVME_SC_SUCCESS);
25453f2304f8SSagi Grimberg req->offset = 0;
25463f2304f8SSagi Grimberg req->data_sent = 0;
25473f2304f8SSagi Grimberg req->pdu_len = 0;
25483f2304f8SSagi Grimberg req->pdu_sent = 0;
2549c2700d28SVarun Prakash req->h2cdata_left = 0;
255025e5cb78SSagi Grimberg req->data_len = blk_rq_nr_phys_segments(rq) ?
255125e5cb78SSagi Grimberg blk_rq_payload_bytes(rq) : 0;
25523f2304f8SSagi Grimberg req->curr_bio = rq->bio;
2553e11e5116SSagi Grimberg if (req->curr_bio && req->data_len)
2554cb9b870fSSagi Grimberg nvme_tcp_init_iter(req, rq_data_dir(rq));
25553f2304f8SSagi Grimberg
25563f2304f8SSagi Grimberg if (rq_data_dir(rq) == WRITE &&
255753ee9e29SCaleb Sander req->data_len <= nvme_tcp_inline_data_size(req))
25583f2304f8SSagi Grimberg req->pdu_len = req->data_len;
25593f2304f8SSagi Grimberg
25603f2304f8SSagi Grimberg pdu->hdr.type = nvme_tcp_cmd;
25613f2304f8SSagi Grimberg pdu->hdr.flags = 0;
25623f2304f8SSagi Grimberg if (queue->hdr_digest)
25633f2304f8SSagi Grimberg pdu->hdr.flags |= NVME_TCP_F_HDGST;
25643f2304f8SSagi Grimberg if (queue->data_digest && req->pdu_len) {
25653f2304f8SSagi Grimberg pdu->hdr.flags |= NVME_TCP_F_DDGST;
25663f2304f8SSagi Grimberg ddgst = nvme_tcp_ddgst_len(queue);
25673f2304f8SSagi Grimberg }
25683f2304f8SSagi Grimberg pdu->hdr.hlen = sizeof(*pdu);
25693f2304f8SSagi Grimberg pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
25703f2304f8SSagi Grimberg pdu->hdr.plen =
25713f2304f8SSagi Grimberg cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
25723f2304f8SSagi Grimberg
25733f2304f8SSagi Grimberg ret = nvme_tcp_map_data(queue, rq);
25743f2304f8SSagi Grimberg if (unlikely(ret)) {
257528a4cac4SMax Gurtovoy nvme_cleanup_cmd(rq);
25763f2304f8SSagi Grimberg dev_err(queue->ctrl->ctrl.device,
25773f2304f8SSagi Grimberg "Failed to map data (%d)\n", ret);
25783f2304f8SSagi Grimberg return ret;
25793f2304f8SSagi Grimberg }
25803f2304f8SSagi Grimberg
25813f2304f8SSagi Grimberg return 0;
25823f2304f8SSagi Grimberg }
25833f2304f8SSagi Grimberg
nvme_tcp_commit_rqs(struct blk_mq_hw_ctx * hctx)258486f0348aSSagi Grimberg static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx)
258586f0348aSSagi Grimberg {
258686f0348aSSagi Grimberg struct nvme_tcp_queue *queue = hctx->driver_data;
258786f0348aSSagi Grimberg
258886f0348aSSagi Grimberg if (!llist_empty(&queue->req_list))
258986f0348aSSagi Grimberg queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
259086f0348aSSagi Grimberg }
259186f0348aSSagi Grimberg
nvme_tcp_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)25923f2304f8SSagi Grimberg static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
25933f2304f8SSagi Grimberg const struct blk_mq_queue_data *bd)
25943f2304f8SSagi Grimberg {
25953f2304f8SSagi Grimberg struct nvme_ns *ns = hctx->queue->queuedata;
25963f2304f8SSagi Grimberg struct nvme_tcp_queue *queue = hctx->driver_data;
25973f2304f8SSagi Grimberg struct request *rq = bd->rq;
25983f2304f8SSagi Grimberg struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
25993f2304f8SSagi Grimberg bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
26003f2304f8SSagi Grimberg blk_status_t ret;
26013f2304f8SSagi Grimberg
2602a9715744STao Chiu if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
2603a9715744STao Chiu return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
26043f2304f8SSagi Grimberg
26053f2304f8SSagi Grimberg ret = nvme_tcp_setup_cmd_pdu(ns, rq);
26063f2304f8SSagi Grimberg if (unlikely(ret))
26073f2304f8SSagi Grimberg return ret;
26083f2304f8SSagi Grimberg
26096887fc64SSagi Grimberg nvme_start_request(rq);
26103f2304f8SSagi Grimberg
261186f0348aSSagi Grimberg nvme_tcp_queue_request(req, true, bd->last);
26123f2304f8SSagi Grimberg
26133f2304f8SSagi Grimberg return BLK_STS_OK;
26143f2304f8SSagi Grimberg }
26153f2304f8SSagi Grimberg
nvme_tcp_map_queues(struct blk_mq_tag_set * set)2616a4e1d0b7SBart Van Assche static void nvme_tcp_map_queues(struct blk_mq_tag_set *set)
2617873946f4SSagi Grimberg {
261806427ca0SChristoph Hellwig struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data);
2619873946f4SSagi Grimberg
2620a249d306SKeith Busch nvmf_map_queues(set, &ctrl->ctrl, ctrl->io_queues);
2621873946f4SSagi Grimberg }
2622873946f4SSagi Grimberg
nvme_tcp_poll(struct blk_mq_hw_ctx * hctx,struct io_comp_batch * iob)26235a72e899SJens Axboe static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
26241a9460ceSSagi Grimberg {
26251a9460ceSSagi Grimberg struct nvme_tcp_queue *queue = hctx->driver_data;
26261a9460ceSSagi Grimberg struct sock *sk = queue->sock->sk;
26271a9460ceSSagi Grimberg
2628f86e5bf8SSagi Grimberg if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2629f86e5bf8SSagi Grimberg return 0;
2630f86e5bf8SSagi Grimberg
263172e5d757SSagi Grimberg set_bit(NVME_TCP_Q_POLLING, &queue->flags);
26323f926af3SEric Dumazet if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue))
26331a9460ceSSagi Grimberg sk_busy_loop(sk, true);
26341a9460ceSSagi Grimberg nvme_tcp_try_recv(queue);
263572e5d757SSagi Grimberg clear_bit(NVME_TCP_Q_POLLING, &queue->flags);
26361a9460ceSSagi Grimberg return queue->nr_cqe;
26371a9460ceSSagi Grimberg }
26381a9460ceSSagi Grimberg
nvme_tcp_get_address(struct nvme_ctrl * ctrl,char * buf,int size)263902c57a82SMartin Belanger static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
264002c57a82SMartin Belanger {
264102c57a82SMartin Belanger struct nvme_tcp_queue *queue = &to_tcp_ctrl(ctrl)->queues[0];
264202c57a82SMartin Belanger struct sockaddr_storage src_addr;
264302c57a82SMartin Belanger int ret, len;
264402c57a82SMartin Belanger
264502c57a82SMartin Belanger len = nvmf_get_address(ctrl, buf, size);
264602c57a82SMartin Belanger
2647*782373baSHannes Reinecke if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
2648*782373baSHannes Reinecke return len;
2649*782373baSHannes Reinecke
265076d54bf2SAkinobu Mita mutex_lock(&queue->queue_lock);
265176d54bf2SAkinobu Mita
265202c57a82SMartin Belanger ret = kernel_getsockname(queue->sock, (struct sockaddr *)&src_addr);
265302c57a82SMartin Belanger if (ret > 0) {
265402c57a82SMartin Belanger if (len > 0)
265502c57a82SMartin Belanger len--; /* strip trailing newline */
265602c57a82SMartin Belanger len += scnprintf(buf + len, size - len, "%ssrc_addr=%pISc\n",
265702c57a82SMartin Belanger (len) ? "," : "", &src_addr);
265802c57a82SMartin Belanger }
2659*782373baSHannes Reinecke
266076d54bf2SAkinobu Mita mutex_unlock(&queue->queue_lock);
266102c57a82SMartin Belanger
266202c57a82SMartin Belanger return len;
266302c57a82SMartin Belanger }
266402c57a82SMartin Belanger
26656acbd961SRikard Falkeborn static const struct blk_mq_ops nvme_tcp_mq_ops = {
26663f2304f8SSagi Grimberg .queue_rq = nvme_tcp_queue_rq,
266786f0348aSSagi Grimberg .commit_rqs = nvme_tcp_commit_rqs,
26683f2304f8SSagi Grimberg .complete = nvme_complete_rq,
26693f2304f8SSagi Grimberg .init_request = nvme_tcp_init_request,
26703f2304f8SSagi Grimberg .exit_request = nvme_tcp_exit_request,
26713f2304f8SSagi Grimberg .init_hctx = nvme_tcp_init_hctx,
26723f2304f8SSagi Grimberg .timeout = nvme_tcp_timeout,
2673873946f4SSagi Grimberg .map_queues = nvme_tcp_map_queues,
26741a9460ceSSagi Grimberg .poll = nvme_tcp_poll,
26753f2304f8SSagi Grimberg };
26763f2304f8SSagi Grimberg
26776acbd961SRikard Falkeborn static const struct blk_mq_ops nvme_tcp_admin_mq_ops = {
26783f2304f8SSagi Grimberg .queue_rq = nvme_tcp_queue_rq,
26793f2304f8SSagi Grimberg .complete = nvme_complete_rq,
26803f2304f8SSagi Grimberg .init_request = nvme_tcp_init_request,
26813f2304f8SSagi Grimberg .exit_request = nvme_tcp_exit_request,
26823f2304f8SSagi Grimberg .init_hctx = nvme_tcp_init_admin_hctx,
26833f2304f8SSagi Grimberg .timeout = nvme_tcp_timeout,
26843f2304f8SSagi Grimberg };
26853f2304f8SSagi Grimberg
26863f2304f8SSagi Grimberg static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
26873f2304f8SSagi Grimberg .name = "tcp",
26883f2304f8SSagi Grimberg .module = THIS_MODULE,
2689db45e1a5SChristoph Hellwig .flags = NVME_F_FABRICS | NVME_F_BLOCKING,
26903f2304f8SSagi Grimberg .reg_read32 = nvmf_reg_read32,
26913f2304f8SSagi Grimberg .reg_read64 = nvmf_reg_read64,
26923f2304f8SSagi Grimberg .reg_write32 = nvmf_reg_write32,
2693210b1f65SKeith Busch .subsystem_reset = nvmf_subsystem_reset,
26943f2304f8SSagi Grimberg .free_ctrl = nvme_tcp_free_ctrl,
26953f2304f8SSagi Grimberg .submit_async_event = nvme_tcp_submit_async_event,
26963f2304f8SSagi Grimberg .delete_ctrl = nvme_tcp_delete_ctrl,
269702c57a82SMartin Belanger .get_address = nvme_tcp_get_address,
2698f7f70f4aSRuozhu Li .stop_ctrl = nvme_tcp_stop_ctrl,
26993f2304f8SSagi Grimberg };
27003f2304f8SSagi Grimberg
27013f2304f8SSagi Grimberg static bool
nvme_tcp_existing_controller(struct nvmf_ctrl_options * opts)27023f2304f8SSagi Grimberg nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
27033f2304f8SSagi Grimberg {
27043f2304f8SSagi Grimberg struct nvme_tcp_ctrl *ctrl;
27053f2304f8SSagi Grimberg bool found = false;
27063f2304f8SSagi Grimberg
27073f2304f8SSagi Grimberg mutex_lock(&nvme_tcp_ctrl_mutex);
27083f2304f8SSagi Grimberg list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
27093f2304f8SSagi Grimberg found = nvmf_ip_options_match(&ctrl->ctrl, opts);
27103f2304f8SSagi Grimberg if (found)
27113f2304f8SSagi Grimberg break;
27123f2304f8SSagi Grimberg }
27133f2304f8SSagi Grimberg mutex_unlock(&nvme_tcp_ctrl_mutex);
27143f2304f8SSagi Grimberg
27153f2304f8SSagi Grimberg return found;
27163f2304f8SSagi Grimberg }
27173f2304f8SSagi Grimberg
nvme_tcp_alloc_ctrl(struct device * dev,struct nvmf_ctrl_options * opts)271810fd7fb6SKeith Busch static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev,
27193f2304f8SSagi Grimberg struct nvmf_ctrl_options *opts)
27203f2304f8SSagi Grimberg {
27213f2304f8SSagi Grimberg struct nvme_tcp_ctrl *ctrl;
27223f2304f8SSagi Grimberg int ret;
27233f2304f8SSagi Grimberg
27243f2304f8SSagi Grimberg ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
27253f2304f8SSagi Grimberg if (!ctrl)
27263f2304f8SSagi Grimberg return ERR_PTR(-ENOMEM);
27273f2304f8SSagi Grimberg
27283f2304f8SSagi Grimberg INIT_LIST_HEAD(&ctrl->list);
27293f2304f8SSagi Grimberg ctrl->ctrl.opts = opts;
27301a9460ceSSagi Grimberg ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
27311a9460ceSSagi Grimberg opts->nr_poll_queues + 1;
27323f2304f8SSagi Grimberg ctrl->ctrl.sqsize = opts->queue_size - 1;
27333f2304f8SSagi Grimberg ctrl->ctrl.kato = opts->kato;
27343f2304f8SSagi Grimberg
27353f2304f8SSagi Grimberg INIT_DELAYED_WORK(&ctrl->connect_work,
27363f2304f8SSagi Grimberg nvme_tcp_reconnect_ctrl_work);
27373f2304f8SSagi Grimberg INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
27383f2304f8SSagi Grimberg INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
27393f2304f8SSagi Grimberg
27403f2304f8SSagi Grimberg if (!(opts->mask & NVMF_OPT_TRSVCID)) {
27413f2304f8SSagi Grimberg opts->trsvcid =
27423f2304f8SSagi Grimberg kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
27433f2304f8SSagi Grimberg if (!opts->trsvcid) {
27443f2304f8SSagi Grimberg ret = -ENOMEM;
27453f2304f8SSagi Grimberg goto out_free_ctrl;
27463f2304f8SSagi Grimberg }
27473f2304f8SSagi Grimberg opts->mask |= NVMF_OPT_TRSVCID;
27483f2304f8SSagi Grimberg }
27493f2304f8SSagi Grimberg
27503f2304f8SSagi Grimberg ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
27513f2304f8SSagi Grimberg opts->traddr, opts->trsvcid, &ctrl->addr);
27523f2304f8SSagi Grimberg if (ret) {
27533f2304f8SSagi Grimberg pr_err("malformed address passed: %s:%s\n",
27543f2304f8SSagi Grimberg opts->traddr, opts->trsvcid);
27553f2304f8SSagi Grimberg goto out_free_ctrl;
27563f2304f8SSagi Grimberg }
27573f2304f8SSagi Grimberg
27583f2304f8SSagi Grimberg if (opts->mask & NVMF_OPT_HOST_TRADDR) {
27593f2304f8SSagi Grimberg ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
27603f2304f8SSagi Grimberg opts->host_traddr, NULL, &ctrl->src_addr);
27613f2304f8SSagi Grimberg if (ret) {
27623f2304f8SSagi Grimberg pr_err("malformed src address passed: %s\n",
27633f2304f8SSagi Grimberg opts->host_traddr);
27643f2304f8SSagi Grimberg goto out_free_ctrl;
27653f2304f8SSagi Grimberg }
27663f2304f8SSagi Grimberg }
27673f2304f8SSagi Grimberg
27683ede8f72SMartin Belanger if (opts->mask & NVMF_OPT_HOST_IFACE) {
27698b43ced6SPrabhakar Kushwaha if (!__dev_get_by_name(&init_net, opts->host_iface)) {
27703ede8f72SMartin Belanger pr_err("invalid interface passed: %s\n",
27713ede8f72SMartin Belanger opts->host_iface);
27723ede8f72SMartin Belanger ret = -ENODEV;
27733ede8f72SMartin Belanger goto out_free_ctrl;
27743ede8f72SMartin Belanger }
27753ede8f72SMartin Belanger }
27763ede8f72SMartin Belanger
27773f2304f8SSagi Grimberg if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
27783f2304f8SSagi Grimberg ret = -EALREADY;
27793f2304f8SSagi Grimberg goto out_free_ctrl;
27803f2304f8SSagi Grimberg }
27813f2304f8SSagi Grimberg
2782873946f4SSagi Grimberg ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
27833f2304f8SSagi Grimberg GFP_KERNEL);
27843f2304f8SSagi Grimberg if (!ctrl->queues) {
27853f2304f8SSagi Grimberg ret = -ENOMEM;
27863f2304f8SSagi Grimberg goto out_free_ctrl;
27873f2304f8SSagi Grimberg }
27883f2304f8SSagi Grimberg
27893f2304f8SSagi Grimberg ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
27903f2304f8SSagi Grimberg if (ret)
27913f2304f8SSagi Grimberg goto out_kfree_queues;
27923f2304f8SSagi Grimberg
279310fd7fb6SKeith Busch return ctrl;
279410fd7fb6SKeith Busch out_kfree_queues:
279510fd7fb6SKeith Busch kfree(ctrl->queues);
279610fd7fb6SKeith Busch out_free_ctrl:
279710fd7fb6SKeith Busch kfree(ctrl);
279810fd7fb6SKeith Busch return ERR_PTR(ret);
279910fd7fb6SKeith Busch }
280010fd7fb6SKeith Busch
nvme_tcp_create_ctrl(struct device * dev,struct nvmf_ctrl_options * opts)280110fd7fb6SKeith Busch static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
280210fd7fb6SKeith Busch struct nvmf_ctrl_options *opts)
280310fd7fb6SKeith Busch {
280410fd7fb6SKeith Busch struct nvme_tcp_ctrl *ctrl;
280510fd7fb6SKeith Busch int ret;
280610fd7fb6SKeith Busch
280710fd7fb6SKeith Busch ctrl = nvme_tcp_alloc_ctrl(dev, opts);
280810fd7fb6SKeith Busch if (IS_ERR(ctrl))
280910fd7fb6SKeith Busch return ERR_CAST(ctrl);
281010fd7fb6SKeith Busch
28111a9e2181SKeith Busch ret = nvme_add_ctrl(&ctrl->ctrl);
28121a9e2181SKeith Busch if (ret)
28131a9e2181SKeith Busch goto out_put_ctrl;
28141a9e2181SKeith Busch
28153f2304f8SSagi Grimberg if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
28163f2304f8SSagi Grimberg WARN_ON_ONCE(1);
28173f2304f8SSagi Grimberg ret = -EINTR;
28183f2304f8SSagi Grimberg goto out_uninit_ctrl;
28193f2304f8SSagi Grimberg }
28203f2304f8SSagi Grimberg
28213f2304f8SSagi Grimberg ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
28223f2304f8SSagi Grimberg if (ret)
28233f2304f8SSagi Grimberg goto out_uninit_ctrl;
28243f2304f8SSagi Grimberg
2825524719b4SNitin U. Yewale dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp, hostnqn: %s\n",
2826524719b4SNitin U. Yewale nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr, opts->host->nqn);
28273f2304f8SSagi Grimberg
28283f2304f8SSagi Grimberg mutex_lock(&nvme_tcp_ctrl_mutex);
28293f2304f8SSagi Grimberg list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
28303f2304f8SSagi Grimberg mutex_unlock(&nvme_tcp_ctrl_mutex);
28313f2304f8SSagi Grimberg
28323f2304f8SSagi Grimberg return &ctrl->ctrl;
28333f2304f8SSagi Grimberg
28343f2304f8SSagi Grimberg out_uninit_ctrl:
28353f2304f8SSagi Grimberg nvme_uninit_ctrl(&ctrl->ctrl);
28361a9e2181SKeith Busch out_put_ctrl:
28373f2304f8SSagi Grimberg nvme_put_ctrl(&ctrl->ctrl);
28383f2304f8SSagi Grimberg if (ret > 0)
28393f2304f8SSagi Grimberg ret = -EIO;
28403f2304f8SSagi Grimberg return ERR_PTR(ret);
28413f2304f8SSagi Grimberg }
28423f2304f8SSagi Grimberg
28433f2304f8SSagi Grimberg static struct nvmf_transport_ops nvme_tcp_transport = {
28443f2304f8SSagi Grimberg .name = "tcp",
28453f2304f8SSagi Grimberg .module = THIS_MODULE,
28463f2304f8SSagi Grimberg .required_opts = NVMF_OPT_TRADDR,
28473f2304f8SSagi Grimberg .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
28483f2304f8SSagi Grimberg NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2849873946f4SSagi Grimberg NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
2850bb13985dSIsrael Rukshin NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2851adf22c52SHannes Reinecke NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS |
2852adf22c52SHannes Reinecke NVMF_OPT_KEYRING | NVMF_OPT_TLS_KEY,
28533f2304f8SSagi Grimberg .create_ctrl = nvme_tcp_create_ctrl,
28543f2304f8SSagi Grimberg };
28553f2304f8SSagi Grimberg
nvme_tcp_init_module(void)28563f2304f8SSagi Grimberg static int __init nvme_tcp_init_module(void)
28573f2304f8SSagi Grimberg {
28580c29f9faSLi Feng unsigned int wq_flags = WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_SYSFS;
28590c29f9faSLi Feng
28607e87965dSSagi Grimberg BUILD_BUG_ON(sizeof(struct nvme_tcp_hdr) != 8);
28617e87965dSSagi Grimberg BUILD_BUG_ON(sizeof(struct nvme_tcp_cmd_pdu) != 72);
28627e87965dSSagi Grimberg BUILD_BUG_ON(sizeof(struct nvme_tcp_data_pdu) != 24);
28637e87965dSSagi Grimberg BUILD_BUG_ON(sizeof(struct nvme_tcp_rsp_pdu) != 24);
28647e87965dSSagi Grimberg BUILD_BUG_ON(sizeof(struct nvme_tcp_r2t_pdu) != 24);
28657e87965dSSagi Grimberg BUILD_BUG_ON(sizeof(struct nvme_tcp_icreq_pdu) != 128);
28667e87965dSSagi Grimberg BUILD_BUG_ON(sizeof(struct nvme_tcp_icresp_pdu) != 128);
28677e87965dSSagi Grimberg BUILD_BUG_ON(sizeof(struct nvme_tcp_term_pdu) != 24);
28687e87965dSSagi Grimberg
28690c29f9faSLi Feng if (wq_unbound)
28700c29f9faSLi Feng wq_flags |= WQ_UNBOUND;
28710c29f9faSLi Feng
28720c29f9faSLi Feng nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq", wq_flags, 0);
28733f2304f8SSagi Grimberg if (!nvme_tcp_wq)
28743f2304f8SSagi Grimberg return -ENOMEM;
28753f2304f8SSagi Grimberg
28763f2304f8SSagi Grimberg nvmf_register_transport(&nvme_tcp_transport);
28773f2304f8SSagi Grimberg return 0;
28783f2304f8SSagi Grimberg }
28793f2304f8SSagi Grimberg
nvme_tcp_cleanup_module(void)28803f2304f8SSagi Grimberg static void __exit nvme_tcp_cleanup_module(void)
28813f2304f8SSagi Grimberg {
28823f2304f8SSagi Grimberg struct nvme_tcp_ctrl *ctrl;
28833f2304f8SSagi Grimberg
28843f2304f8SSagi Grimberg nvmf_unregister_transport(&nvme_tcp_transport);
28853f2304f8SSagi Grimberg
28863f2304f8SSagi Grimberg mutex_lock(&nvme_tcp_ctrl_mutex);
28873f2304f8SSagi Grimberg list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
28883f2304f8SSagi Grimberg nvme_delete_ctrl(&ctrl->ctrl);
28893f2304f8SSagi Grimberg mutex_unlock(&nvme_tcp_ctrl_mutex);
28903f2304f8SSagi Grimberg flush_workqueue(nvme_delete_wq);
28913f2304f8SSagi Grimberg
28923f2304f8SSagi Grimberg destroy_workqueue(nvme_tcp_wq);
28933f2304f8SSagi Grimberg }
28943f2304f8SSagi Grimberg
28953f2304f8SSagi Grimberg module_init(nvme_tcp_init_module);
28963f2304f8SSagi Grimberg module_exit(nvme_tcp_cleanup_module);
28973f2304f8SSagi Grimberg
289892b0b0ffSChaitanya Kulkarni MODULE_DESCRIPTION("NVMe host TCP transport driver");
28993f2304f8SSagi Grimberg MODULE_LICENSE("GPL v2");
2900