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