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