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