at commit c03c1a14
| 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/crc32.h> |
| 12 | #include <linux/nvme-tcp.h> |
| 13 | #include <linux/nvme-keyring.h> |
| 14 | #include <net/sock.h> |
| 15 | #include <net/tcp.h> |
| 16 | #include <net/tls.h> |
| 17 | #include <net/tls_prot.h> |
| 18 | #include <net/handshake.h> |
| 19 | #include <linux/blk-mq.h> |
| 20 | #include <net/busy_poll.h> |
| 21 | #include <trace/events/sock.h> |
| 22 | |
| 23 | #include "nvme.h" |
| 24 | #include "fabrics.h" |
| 25 | |
| 26 | struct nvme_tcp_queue; |
| 27 | |
| 28 | /* |
| 29 | * Define the socket priority to use for connections where it is desirable |
| 30 | * that the NIC consider performing optimized packet processing or filtering. |
| 31 | * A non-zero value being sufficient to indicate general consideration of any |
| 32 | * possible optimization. Making it a module param allows for alternative |
| 33 | * values that may be unique for some NIC implementations. |
| 34 | */ |
| 35 | static int so_priority; |
| 36 | module_param(so_priority, int, 0644); |
| 37 | MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority"); |
| 38 | |
| 39 | /* |
| 40 | * Use the unbound workqueue for nvme_tcp_wq, then we can set the cpu affinity |
| 41 | * from sysfs. |
| 42 | */ |
| 43 | static bool wq_unbound; |
| 44 | module_param(wq_unbound, bool, 0644); |
| 45 | MODULE_PARM_DESC(wq_unbound, "Use unbound workqueue for nvme-tcp IO context (default false)"); |
| 46 | |
| 47 | /* |
| 48 | * TLS handshake timeout |
| 49 | */ |
| 50 | static int tls_handshake_timeout = 10; |
| 51 | #ifdef CONFIG_NVME_TCP_TLS |
| 52 | module_param(tls_handshake_timeout, int, 0644); |
| 53 | MODULE_PARM_DESC(tls_handshake_timeout, |
| 54 | "nvme TLS handshake timeout in seconds (default 10)"); |
| 55 | #endif |
| 56 | |
| 57 | static atomic_t nvme_tcp_cpu_queues[NR_CPUS]; |
| 58 | |
| 59 | enum nvme_tcp_send_state { |
| 60 | NVME_TCP_SEND_CMD_PDU = 0, |
| 61 | NVME_TCP_SEND_H2C_PDU, |
| 62 | NVME_TCP_SEND_DATA, |
| 63 | NVME_TCP_SEND_DDGST, |
| 64 | }; |
| 65 | |
| 66 | struct nvme_tcp_request { |
| 67 | struct nvme_request req; |
| 68 | void *pdu; |
| 69 | struct nvme_tcp_queue *queue; |
| 70 | u32 data_len; |
| 71 | u32 pdu_len; |
| 72 | u32 pdu_sent; |
| 73 | u32 h2cdata_left; |
| 74 | u32 h2cdata_offset; |
| 75 | u16 ttag; |
| 76 | __le16 status; |
| 77 | struct list_head entry; |
| 78 | struct llist_node lentry; |
| 79 | __le32 ddgst; |
| 80 | |
| 81 | struct bio *curr_bio; |
| 82 | struct iov_iter iter; |
| 83 | u32 data_recvd; |
| 84 | |
| 85 | /* send state */ |
| 86 | size_t offset; |
| 87 | size_t data_sent; |
| 88 | enum nvme_tcp_send_state state; |
| 89 | }; |
| 90 | |
| 91 | enum nvme_tcp_queue_flags { |
| 92 | NVME_TCP_Q_ALLOCATED = 0, |
| 93 | NVME_TCP_Q_LIVE = 1, |
| 94 | NVME_TCP_Q_POLLING = 2, |
| 95 | NVME_TCP_Q_IO_CPU_SET = 3, |
| 96 | }; |
| 97 | |
| 98 | enum nvme_tcp_recv_state { |
| 99 | NVME_TCP_RECV_PDU = 0, |
| 100 | NVME_TCP_RECV_DATA, |
| 101 | NVME_TCP_RECV_DDGST, |
| 102 | }; |
| 103 | |
| 104 | struct nvme_tcp_ctrl; |
| 105 | struct nvme_tcp_queue { |
| 106 | struct socket *sock; |
| 107 | struct work_struct io_work; |
| 108 | int io_cpu; |
| 109 | |
| 110 | struct mutex queue_lock; |
| 111 | struct mutex send_mutex; |
| 112 | struct mutex pf_cache_lock; |
| 113 | struct llist_head req_list; |
| 114 | struct list_head send_list; |
| 115 | |
| 116 | /* recv state */ |
| 117 | void *pdu; |
| 118 | int pdu_remaining; |
| 119 | int pdu_offset; |
| 120 | size_t data_remaining; |
| 121 | size_t ddgst_remaining; |
| 122 | unsigned int nr_cqe; |
| 123 | |
| 124 | /* send state */ |
| 125 | struct nvme_tcp_request *request; |
| 126 | |
| 127 | u32 maxh2cdata; |
| 128 | size_t cmnd_capsule_len; |
| 129 | struct nvme_tcp_ctrl *ctrl; |
| 130 | unsigned long flags; |
| 131 | bool rd_enabled; |
| 132 | |
| 133 | bool hdr_digest; |
| 134 | bool data_digest; |
| 135 | bool tls_enabled; |
| 136 | u32 rcv_crc; |
| 137 | u32 snd_crc; |
| 138 | __le32 exp_ddgst; |
| 139 | __le32 recv_ddgst; |
| 140 | struct completion tls_complete; |
| 141 | int tls_err; |
| 142 | struct page_frag_cache pf_cache; |
| 143 | |
| 144 | void (*state_change)(struct sock *); |
| 145 | void (*data_ready)(struct sock *); |
| 146 | void (*write_space)(struct sock *); |
| 147 | }; |
| 148 | |
| 149 | static DEFINE_MUTEX(nvme_tcp_ctrl_mutex); |
| 150 | static LIST_HEAD_GUARDED(nvme_tcp_ctrl_list, nvme_tcp_ctrl_mutex); |
| 151 | |
| 152 | struct nvme_tcp_ctrl { |
| 153 | /* read only in the hot path */ |
| 154 | struct nvme_tcp_queue *queues; |
| 155 | struct blk_mq_tag_set tag_set; |
| 156 | |
| 157 | /* other member variables */ |
| 158 | struct list_head list |
| 159 | __guarded_by(&nvme_tcp_ctrl_mutex); |
| 160 | struct blk_mq_tag_set admin_tag_set; |
| 161 | struct sockaddr_storage addr; |
| 162 | struct sockaddr_storage src_addr; |
| 163 | struct nvme_ctrl ctrl; |
| 164 | |
| 165 | struct work_struct err_work; |
| 166 | struct delayed_work connect_work; |
| 167 | struct nvme_tcp_request async_req; |
| 168 | u32 io_queues[HCTX_MAX_TYPES]; |
| 169 | }; |
| 170 | |
| 171 | static struct workqueue_struct *nvme_tcp_wq; |
| 172 | static const struct blk_mq_ops nvme_tcp_mq_ops; |
| 173 | static const struct blk_mq_ops nvme_tcp_admin_mq_ops; |
| 174 | static int nvme_tcp_try_send(struct nvme_tcp_queue *queue); |
| 175 | |
| 176 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 177 | /* lockdep can detect a circular dependency of the form |
| 178 | * sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock |
| 179 | * because dependencies are tracked for both nvme-tcp and user contexts. Using |
| 180 | * a separate class prevents lockdep from conflating nvme-tcp socket use with |
| 181 | * user-space socket API use. |
| 182 | */ |
| 183 | static struct lock_class_key nvme_tcp_sk_key[2]; |
| 184 | static struct lock_class_key nvme_tcp_slock_key[2]; |
| 185 | |
| 186 | static void nvme_tcp_reclassify_socket(struct socket *sock) |
| 187 | { |
| 188 | struct sock *sk = sock->sk; |
| 189 | |
| 190 | if (WARN_ON_ONCE(!sock_allow_reclassification(sk))) |
| 191 | return; |
| 192 | |
| 193 | switch (sk->sk_family) { |
| 194 | case AF_INET: |
| 195 | sock_lock_init_class_and_name(sk, "slock-AF_INET-NVME", |
| 196 | &nvme_tcp_slock_key[0], |
| 197 | "sk_lock-AF_INET-NVME", |
| 198 | &nvme_tcp_sk_key[0]); |
| 199 | break; |
| 200 | case AF_INET6: |
| 201 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-NVME", |
| 202 | &nvme_tcp_slock_key[1], |
| 203 | "sk_lock-AF_INET6-NVME", |
| 204 | &nvme_tcp_sk_key[1]); |
| 205 | break; |
| 206 | default: |
| 207 | WARN_ON_ONCE(1); |
| 208 | } |
| 209 | } |
| 210 | #else |
| 211 | static void nvme_tcp_reclassify_socket(struct socket *sock) { } |
| 212 | #endif |
| 213 | |
| 214 | static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl) |
| 215 | { |
| 216 | return container_of(ctrl, struct nvme_tcp_ctrl, ctrl); |
| 217 | } |
| 218 | |
| 219 | static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue) |
| 220 | { |
| 221 | return queue - queue->ctrl->queues; |
| 222 | } |
| 223 | |
| 224 | static inline bool nvme_tcp_recv_pdu_supported(enum nvme_tcp_pdu_type type) |
| 225 | { |
| 226 | switch (type) { |
| 227 | case nvme_tcp_c2h_term: |
| 228 | case nvme_tcp_c2h_data: |
| 229 | case nvme_tcp_r2t: |
| 230 | case nvme_tcp_rsp: |
| 231 | return true; |
| 232 | default: |
| 233 | return false; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Check if the queue is TLS encrypted |
| 239 | */ |
| 240 | static inline bool nvme_tcp_queue_tls(struct nvme_tcp_queue *queue) |
| 241 | { |
| 242 | if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) |
| 243 | return 0; |
| 244 | |
| 245 | return queue->tls_enabled; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Check if TLS is configured for the controller. |
| 250 | */ |
| 251 | static inline bool nvme_tcp_tls_configured(struct nvme_ctrl *ctrl) |
| 252 | { |
| 253 | if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) |
| 254 | return 0; |
| 255 | |
| 256 | return ctrl->opts->tls || ctrl->opts->concat; |
| 257 | } |
| 258 | |
| 259 | static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue) |
| 260 | { |
| 261 | u32 queue_idx = nvme_tcp_queue_id(queue); |
| 262 | |
| 263 | if (queue_idx == 0) |
| 264 | return queue->ctrl->admin_tag_set.tags[queue_idx]; |
| 265 | return queue->ctrl->tag_set.tags[queue_idx - 1]; |
| 266 | } |
| 267 | |
| 268 | static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue) |
| 269 | { |
| 270 | return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0; |
| 271 | } |
| 272 | |
| 273 | static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue) |
| 274 | { |
| 275 | return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0; |
| 276 | } |
| 277 | |
| 278 | static inline void *nvme_tcp_req_cmd_pdu(struct nvme_tcp_request *req) |
| 279 | { |
| 280 | return req->pdu; |
| 281 | } |
| 282 | |
| 283 | static inline void *nvme_tcp_req_data_pdu(struct nvme_tcp_request *req) |
| 284 | { |
| 285 | /* use the pdu space in the back for the data pdu */ |
| 286 | return req->pdu + sizeof(struct nvme_tcp_cmd_pdu) - |
| 287 | sizeof(struct nvme_tcp_data_pdu); |
| 288 | } |
| 289 | |
| 290 | static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_request *req) |
| 291 | { |
| 292 | if (nvme_is_fabrics(req->req.cmd)) |
| 293 | return NVME_TCP_ADMIN_CCSZ; |
| 294 | return req->queue->cmnd_capsule_len - sizeof(struct nvme_command); |
| 295 | } |
| 296 | |
| 297 | static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req) |
| 298 | { |
| 299 | return req == &req->queue->ctrl->async_req; |
| 300 | } |
| 301 | |
| 302 | static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req) |
| 303 | { |
| 304 | struct request *rq; |
| 305 | |
| 306 | if (unlikely(nvme_tcp_async_req(req))) |
| 307 | return false; /* async events don't have a request */ |
| 308 | |
| 309 | rq = blk_mq_rq_from_pdu(req); |
| 310 | |
| 311 | return rq_data_dir(rq) == WRITE && req->data_len && |
| 312 | req->data_len <= nvme_tcp_inline_data_size(req); |
| 313 | } |
| 314 | |
| 315 | static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req) |
| 316 | { |
| 317 | return req->iter.bvec->bv_page; |
| 318 | } |
| 319 | |
| 320 | static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req) |
| 321 | { |
| 322 | return req->iter.bvec->bv_offset + req->iter.iov_offset; |
| 323 | } |
| 324 | |
| 325 | static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req) |
| 326 | { |
| 327 | return min_t(size_t, iov_iter_single_seg_count(&req->iter), |
| 328 | req->pdu_len - req->pdu_sent); |
| 329 | } |
| 330 | |
| 331 | static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req) |
| 332 | { |
| 333 | return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ? |
| 334 | req->pdu_len - req->pdu_sent : 0; |
| 335 | } |
| 336 | |
| 337 | static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req, |
| 338 | int len) |
| 339 | { |
| 340 | return nvme_tcp_pdu_data_left(req) <= len; |
| 341 | } |
| 342 | |
| 343 | static void nvme_tcp_init_iter(struct nvme_tcp_request *req, |
| 344 | unsigned int dir) |
| 345 | { |
| 346 | struct request *rq = blk_mq_rq_from_pdu(req); |
| 347 | |
| 348 | if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) { |
| 349 | iov_iter_bvec(&req->iter, dir, &rq->special_vec, 1, |
| 350 | blk_rq_payload_bytes(rq)); |
| 351 | req->iter.iov_offset = 0; |
| 352 | } else { |
| 353 | struct bio *bio = req->curr_bio; |
| 354 | struct bvec_iter bi; |
| 355 | struct bio_vec bv; |
| 356 | int nr_bvec = 0; |
| 357 | |
| 358 | bio_for_each_bvec(bv, bio, bi) |
| 359 | nr_bvec++; |
| 360 | |
| 361 | iov_iter_bvec(&req->iter, dir, |
| 362 | __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter), nr_bvec, |
| 363 | bio->bi_iter.bi_size); |
| 364 | req->iter.iov_offset = bio->bi_iter.bi_offset; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req, |
| 369 | int len) |
| 370 | { |
| 371 | req->data_sent += len; |
| 372 | req->pdu_sent += len; |
| 373 | iov_iter_advance(&req->iter, len); |
| 374 | if (!iov_iter_count(&req->iter) && |
| 375 | req->data_sent < req->data_len) { |
| 376 | req->curr_bio = req->curr_bio->bi_next; |
| 377 | nvme_tcp_init_iter(req, ITER_SOURCE); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | static inline void nvme_tcp_send_all(struct nvme_tcp_queue *queue) |
| 382 | { |
| 383 | int ret; |
| 384 | |
| 385 | /* drain the send queue as much as we can... */ |
| 386 | do { |
| 387 | ret = nvme_tcp_try_send(queue); |
| 388 | } while (ret > 0); |
| 389 | } |
| 390 | |
| 391 | static inline bool nvme_tcp_queue_has_pending(struct nvme_tcp_queue *queue) |
| 392 | { |
| 393 | return !list_empty(&queue->send_list) || |
| 394 | !llist_empty(&queue->req_list); |
| 395 | } |
| 396 | |
| 397 | static inline bool nvme_tcp_queue_more(struct nvme_tcp_queue *queue) |
| 398 | { |
| 399 | return !nvme_tcp_queue_tls(queue) && |
| 400 | nvme_tcp_queue_has_pending(queue); |
| 401 | } |
| 402 | |
| 403 | static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req, |
| 404 | bool last) |
| 405 | { |
| 406 | struct nvme_tcp_queue *queue = req->queue; |
| 407 | bool empty; |
| 408 | |
| 409 | empty = llist_add(&req->lentry, &queue->req_list) && |
| 410 | list_empty(&queue->send_list) && !queue->request; |
| 411 | |
| 412 | /* |
| 413 | * if we're the first on the send_list and we can try to send |
| 414 | * directly, otherwise queue io_work. Also, only do that if we |
| 415 | * are on the same cpu, so we don't introduce contention. |
| 416 | * |
| 417 | * TLS kTLS send takes ctx->tx_lock while blk_mq holds set->srcu. |
| 418 | * lockdep reports circular locking via elevator_lock. Defer TLS |
| 419 | * sends to the io workqueue instead of inline from this path. |
| 420 | */ |
| 421 | if (queue->io_cpu == raw_smp_processor_id() && |
| 422 | !nvme_tcp_queue_tls(queue) && |
| 423 | empty && mutex_trylock(&queue->send_mutex)) { |
| 424 | nvme_tcp_send_all(queue); |
| 425 | mutex_unlock(&queue->send_mutex); |
| 426 | } |
| 427 | |
| 428 | if (last && nvme_tcp_queue_has_pending(queue)) |
| 429 | queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); |
| 430 | } |
| 431 | |
| 432 | static void nvme_tcp_process_req_list(struct nvme_tcp_queue *queue) |
| 433 | { |
| 434 | struct nvme_tcp_request *req; |
| 435 | struct llist_node *node; |
| 436 | |
| 437 | for (node = llist_del_all(&queue->req_list); node; node = node->next) { |
| 438 | req = llist_entry(node, struct nvme_tcp_request, lentry); |
| 439 | list_add(&req->entry, &queue->send_list); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | static inline struct nvme_tcp_request * |
| 444 | nvme_tcp_fetch_request(struct nvme_tcp_queue *queue) |
| 445 | { |
| 446 | struct nvme_tcp_request *req; |
| 447 | |
| 448 | req = list_first_entry_or_null(&queue->send_list, |
| 449 | struct nvme_tcp_request, entry); |
| 450 | if (!req) { |
| 451 | nvme_tcp_process_req_list(queue); |
| 452 | req = list_first_entry_or_null(&queue->send_list, |
| 453 | struct nvme_tcp_request, entry); |
| 454 | if (unlikely(!req)) |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | list_del_init(&req->entry); |
| 459 | init_llist_node(&req->lentry); |
| 460 | return req; |
| 461 | } |
| 462 | |
| 463 | #define NVME_TCP_CRC_SEED (~0) |
| 464 | |
| 465 | static inline void nvme_tcp_ddgst_update(u32 *crcp, |
| 466 | struct page *page, size_t off, size_t len) |
| 467 | { |
| 468 | page += off / PAGE_SIZE; |
| 469 | off %= PAGE_SIZE; |
| 470 | while (len) { |
| 471 | const void *vaddr = kmap_local_page(page); |
| 472 | size_t n = min(len, (size_t)PAGE_SIZE - off); |
| 473 | |
| 474 | *crcp = crc32c(*crcp, vaddr + off, n); |
| 475 | kunmap_local(vaddr); |
| 476 | page++; |
| 477 | off = 0; |
| 478 | len -= n; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | static inline __le32 nvme_tcp_ddgst_final(u32 crc) |
| 483 | { |
| 484 | return cpu_to_le32(~crc); |
| 485 | } |
| 486 | |
| 487 | static inline __le32 nvme_tcp_hdgst(const void *pdu, size_t len) |
| 488 | { |
| 489 | return cpu_to_le32(~crc32c(NVME_TCP_CRC_SEED, pdu, len)); |
| 490 | } |
| 491 | |
| 492 | static inline void nvme_tcp_set_hdgst(void *pdu, size_t len) |
| 493 | { |
| 494 | *(__le32 *)(pdu + len) = nvme_tcp_hdgst(pdu, len); |
| 495 | } |
| 496 | |
| 497 | static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue, |
| 498 | void *pdu, size_t pdu_len) |
| 499 | { |
| 500 | struct nvme_tcp_hdr *hdr = pdu; |
| 501 | __le32 recv_digest; |
| 502 | __le32 exp_digest; |
| 503 | |
| 504 | if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) { |
| 505 | dev_err(queue->ctrl->ctrl.device, |
| 506 | "queue %d: header digest flag is cleared\n", |
| 507 | nvme_tcp_queue_id(queue)); |
| 508 | return -EPROTO; |
| 509 | } |
| 510 | |
| 511 | recv_digest = *(__le32 *)(pdu + hdr->hlen); |
| 512 | exp_digest = nvme_tcp_hdgst(pdu, pdu_len); |
| 513 | if (recv_digest != exp_digest) { |
| 514 | dev_err(queue->ctrl->ctrl.device, |
| 515 | "header digest error: recv %#x expected %#x\n", |
| 516 | le32_to_cpu(recv_digest), le32_to_cpu(exp_digest)); |
| 517 | return -EIO; |
| 518 | } |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu) |
| 524 | { |
| 525 | struct nvme_tcp_hdr *hdr = pdu; |
| 526 | u8 digest_len = nvme_tcp_hdgst_len(queue); |
| 527 | u32 len; |
| 528 | |
| 529 | len = le32_to_cpu(hdr->plen) - hdr->hlen - |
| 530 | ((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0); |
| 531 | |
| 532 | if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) { |
| 533 | dev_err(queue->ctrl->ctrl.device, |
| 534 | "queue %d: data digest flag is cleared\n", |
| 535 | nvme_tcp_queue_id(queue)); |
| 536 | return -EPROTO; |
| 537 | } |
| 538 | queue->rcv_crc = NVME_TCP_CRC_SEED; |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static void nvme_tcp_exit_request(struct blk_mq_tag_set *set, |
| 544 | struct request *rq, unsigned int hctx_idx) |
| 545 | { |
| 546 | struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); |
| 547 | |
| 548 | page_frag_free(req->pdu); |
| 549 | } |
| 550 | |
| 551 | static int nvme_tcp_init_request(struct blk_mq_tag_set *set, |
| 552 | struct request *rq, unsigned int hctx_idx, |
| 553 | int numa_node) |
| 554 | { |
| 555 | struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data); |
| 556 | struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); |
| 557 | struct nvme_tcp_cmd_pdu *pdu; |
| 558 | int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0; |
| 559 | struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx]; |
| 560 | u8 hdgst = nvme_tcp_hdgst_len(queue); |
| 561 | |
| 562 | mutex_lock(&queue->pf_cache_lock); |
| 563 | req->pdu = page_frag_alloc(&queue->pf_cache, |
| 564 | sizeof(struct nvme_tcp_cmd_pdu) + hdgst, |
| 565 | GFP_KERNEL | __GFP_ZERO); |
| 566 | mutex_unlock(&queue->pf_cache_lock); |
| 567 | if (!req->pdu) |
| 568 | return -ENOMEM; |
| 569 | |
| 570 | pdu = req->pdu; |
| 571 | req->queue = queue; |
| 572 | nvme_req(rq)->ctrl = &ctrl->ctrl; |
| 573 | nvme_req(rq)->cmd = &pdu->cmd; |
| 574 | init_llist_node(&req->lentry); |
| 575 | INIT_LIST_HEAD(&req->entry); |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 581 | unsigned int hctx_idx) |
| 582 | { |
| 583 | struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(data); |
| 584 | struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1]; |
| 585 | |
| 586 | hctx->driver_data = queue; |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data, |
| 591 | unsigned int hctx_idx) |
| 592 | { |
| 593 | struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(data); |
| 594 | struct nvme_tcp_queue *queue = &ctrl->queues[0]; |
| 595 | |
| 596 | hctx->driver_data = queue; |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static enum nvme_tcp_recv_state |
| 601 | nvme_tcp_recv_state(struct nvme_tcp_queue *queue) |
| 602 | { |
| 603 | return (queue->pdu_remaining) ? NVME_TCP_RECV_PDU : |
| 604 | (queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST : |
| 605 | NVME_TCP_RECV_DATA; |
| 606 | } |
| 607 | |
| 608 | static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue) |
| 609 | { |
| 610 | queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) + |
| 611 | nvme_tcp_hdgst_len(queue); |
| 612 | queue->pdu_offset = 0; |
| 613 | queue->data_remaining = -1; |
| 614 | queue->ddgst_remaining = 0; |
| 615 | } |
| 616 | |
| 617 | static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl) |
| 618 | { |
| 619 | if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING)) |
| 620 | return; |
| 621 | |
| 622 | dev_warn(ctrl->device, "starting error recovery\n"); |
| 623 | queue_work(nvme_reset_wq, &to_tcp_ctrl(ctrl)->err_work); |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * NVMe has no short read: a read that completes successfully must |
| 628 | * have transferred everything it asked for. |
| 629 | */ |
| 630 | static bool nvme_tcp_data_in_short(struct nvme_tcp_queue *queue, |
| 631 | struct request *rq) |
| 632 | { |
| 633 | struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); |
| 634 | |
| 635 | if (le16_to_cpu(req->status) >> 1) |
| 636 | return false; |
| 637 | if (req_op(rq) != REQ_OP_READ || !req->data_len) |
| 638 | return false; |
| 639 | if (likely(req->data_recvd == req->data_len)) |
| 640 | return false; |
| 641 | |
| 642 | dev_err(queue->ctrl->ctrl.device, |
| 643 | "queue %d tag %#x short data-in: got %u of %u\n", |
| 644 | nvme_tcp_queue_id(queue), rq->tag, |
| 645 | req->data_recvd, req->data_len); |
| 646 | return true; |
| 647 | } |
| 648 | |
| 649 | static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue, |
| 650 | struct nvme_completion *cqe) |
| 651 | { |
| 652 | struct nvme_tcp_request *req; |
| 653 | struct request *rq; |
| 654 | |
| 655 | rq = nvme_find_rq(nvme_tcp_tagset(queue), cqe->command_id); |
| 656 | if (!rq) { |
| 657 | dev_err(queue->ctrl->ctrl.device, |
| 658 | "got bad cqe.command_id %#x on queue %d\n", |
| 659 | cqe->command_id, nvme_tcp_queue_id(queue)); |
| 660 | nvme_tcp_error_recovery(&queue->ctrl->ctrl); |
| 661 | return -EINVAL; |
| 662 | } |
| 663 | |
| 664 | req = blk_mq_rq_to_pdu(rq); |
| 665 | if (req->status == cpu_to_le16(NVME_SC_SUCCESS)) |
| 666 | req->status = cqe->status; |
| 667 | |
| 668 | if (unlikely(nvme_tcp_data_in_short(queue, rq))) |
| 669 | return -EPROTO; |
| 670 | |
| 671 | if (!nvme_try_complete_req(rq, req->status, cqe->result)) |
| 672 | nvme_complete_rq(rq); |
| 673 | queue->nr_cqe++; |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue, |
| 679 | struct nvme_tcp_data_pdu *pdu) |
| 680 | { |
| 681 | struct nvme_tcp_request *req; |
| 682 | struct request *rq; |
| 683 | |
| 684 | rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id); |
| 685 | if (!rq) { |
| 686 | dev_err(queue->ctrl->ctrl.device, |
| 687 | "got bad c2hdata.command_id %#x on queue %d\n", |
| 688 | pdu->command_id, nvme_tcp_queue_id(queue)); |
| 689 | return -ENOENT; |
| 690 | } |
| 691 | |
| 692 | if (rq_data_dir(rq) != READ) { |
| 693 | dev_err(queue->ctrl->ctrl.device, |
| 694 | "queue %d tag %#x unexpected data for a write\n", |
| 695 | nvme_tcp_queue_id(queue), rq->tag); |
| 696 | return -EPROTO; |
| 697 | } |
| 698 | |
| 699 | req = blk_mq_rq_to_pdu(rq); |
| 700 | if (!blk_rq_payload_bytes(rq) || !req->curr_bio || !req->data_len) { |
| 701 | dev_err(queue->ctrl->ctrl.device, |
| 702 | "queue %d tag %#x unexpected data\n", |
| 703 | nvme_tcp_queue_id(queue), rq->tag); |
| 704 | return -EIO; |
| 705 | } |
| 706 | |
| 707 | queue->data_remaining = le32_to_cpu(pdu->data_length); |
| 708 | |
| 709 | if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS && |
| 710 | unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) { |
| 711 | dev_err(queue->ctrl->ctrl.device, |
| 712 | "queue %d tag %#x SUCCESS set but not last PDU\n", |
| 713 | nvme_tcp_queue_id(queue), rq->tag); |
| 714 | nvme_tcp_error_recovery(&queue->ctrl->ctrl); |
| 715 | return -EPROTO; |
| 716 | } |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue, |
| 722 | struct nvme_tcp_rsp_pdu *pdu) |
| 723 | { |
| 724 | struct nvme_completion *cqe = &pdu->cqe; |
| 725 | int ret = 0; |
| 726 | |
| 727 | /* |
| 728 | * AEN requests are special as they don't time out and can |
| 729 | * survive any kind of queue freeze and often don't respond to |
| 730 | * aborts. We don't even bother to allocate a struct request |
| 731 | * for them but rather special case them here. |
| 732 | */ |
| 733 | if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue), |
| 734 | cqe->command_id))) |
| 735 | nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status, |
| 736 | &cqe->result); |
| 737 | else |
| 738 | ret = nvme_tcp_process_nvme_cqe(queue, cqe); |
| 739 | |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | static void nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req) |
| 744 | { |
| 745 | struct nvme_tcp_data_pdu *data = nvme_tcp_req_data_pdu(req); |
| 746 | struct nvme_tcp_queue *queue = req->queue; |
| 747 | struct request *rq = blk_mq_rq_from_pdu(req); |
| 748 | u32 h2cdata_sent = req->pdu_len; |
| 749 | u8 hdgst = nvme_tcp_hdgst_len(queue); |
| 750 | u8 ddgst = nvme_tcp_ddgst_len(queue); |
| 751 | |
| 752 | req->state = NVME_TCP_SEND_H2C_PDU; |
| 753 | req->offset = 0; |
| 754 | req->pdu_len = min(req->h2cdata_left, queue->maxh2cdata); |
| 755 | req->pdu_sent = 0; |
| 756 | req->h2cdata_left -= req->pdu_len; |
| 757 | req->h2cdata_offset += h2cdata_sent; |
| 758 | |
| 759 | memset(data, 0, sizeof(*data)); |
| 760 | data->hdr.type = nvme_tcp_h2c_data; |
| 761 | if (!req->h2cdata_left) |
| 762 | data->hdr.flags = NVME_TCP_F_DATA_LAST; |
| 763 | if (queue->hdr_digest) |
| 764 | data->hdr.flags |= NVME_TCP_F_HDGST; |
| 765 | if (queue->data_digest) |
| 766 | data->hdr.flags |= NVME_TCP_F_DDGST; |
| 767 | data->hdr.hlen = sizeof(*data); |
| 768 | data->hdr.pdo = data->hdr.hlen + hdgst; |
| 769 | data->hdr.plen = |
| 770 | cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst); |
| 771 | data->ttag = req->ttag; |
| 772 | data->command_id = nvme_cid(rq); |
| 773 | data->data_offset = cpu_to_le32(req->h2cdata_offset); |
| 774 | data->data_length = cpu_to_le32(req->pdu_len); |
| 775 | } |
| 776 | |
| 777 | static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue, |
| 778 | struct nvme_tcp_r2t_pdu *pdu) |
| 779 | { |
| 780 | struct nvme_tcp_request *req; |
| 781 | struct request *rq; |
| 782 | u32 r2t_length = le32_to_cpu(pdu->r2t_length); |
| 783 | u32 r2t_offset = le32_to_cpu(pdu->r2t_offset); |
| 784 | |
| 785 | rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id); |
| 786 | if (!rq) { |
| 787 | dev_err(queue->ctrl->ctrl.device, |
| 788 | "got bad r2t.command_id %#x on queue %d\n", |
| 789 | pdu->command_id, nvme_tcp_queue_id(queue)); |
| 790 | return -ENOENT; |
| 791 | } |
| 792 | req = blk_mq_rq_to_pdu(rq); |
| 793 | |
| 794 | if (unlikely(rq_data_dir(rq) != WRITE)) { |
| 795 | dev_err(queue->ctrl->ctrl.device, |
| 796 | "req %d unexpected r2t for a non-write command\n", |
| 797 | rq->tag); |
| 798 | return -EPROTO; |
| 799 | } |
| 800 | |
| 801 | if (unlikely(!r2t_length)) { |
| 802 | dev_err(queue->ctrl->ctrl.device, |
| 803 | "req %d r2t len is %u, probably a bug...\n", |
| 804 | rq->tag, r2t_length); |
| 805 | return -EPROTO; |
| 806 | } |
| 807 | |
| 808 | if (unlikely(req->data_sent + r2t_length > req->data_len)) { |
| 809 | dev_err(queue->ctrl->ctrl.device, |
| 810 | "req %d r2t len %u exceeded data len %u (%zu sent)\n", |
| 811 | rq->tag, r2t_length, req->data_len, req->data_sent); |
| 812 | return -EPROTO; |
| 813 | } |
| 814 | |
| 815 | if (unlikely(r2t_offset < req->data_sent)) { |
| 816 | dev_err(queue->ctrl->ctrl.device, |
| 817 | "req %d unexpected r2t offset %u (expected %zu)\n", |
| 818 | rq->tag, r2t_offset, req->data_sent); |
| 819 | return -EPROTO; |
| 820 | } |
| 821 | |
| 822 | if (llist_on_list(&req->lentry) || |
| 823 | !list_empty(&req->entry)) { |
| 824 | dev_err(queue->ctrl->ctrl.device, |
| 825 | "req %d unexpected r2t while processing request\n", |
| 826 | rq->tag); |
| 827 | return -EPROTO; |
| 828 | } |
| 829 | |
| 830 | req->pdu_len = 0; |
| 831 | req->h2cdata_left = r2t_length; |
| 832 | req->h2cdata_offset = r2t_offset; |
| 833 | req->ttag = pdu->ttag; |
| 834 | |
| 835 | nvme_tcp_setup_h2c_data_pdu(req); |
| 836 | |
| 837 | llist_add(&req->lentry, &queue->req_list); |
| 838 | queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | static void nvme_tcp_handle_c2h_term(struct nvme_tcp_queue *queue, |
| 844 | struct nvme_tcp_term_pdu *pdu) |
| 845 | { |
| 846 | u16 fes; |
| 847 | const char *msg; |
| 848 | u32 plen = le32_to_cpu(pdu->hdr.plen); |
| 849 | |
| 850 | static const char * const msg_table[] = { |
| 851 | [NVME_TCP_FES_INVALID_PDU_HDR] = "Invalid PDU Header Field", |
| 852 | [NVME_TCP_FES_PDU_SEQ_ERR] = "PDU Sequence Error", |
| 853 | [NVME_TCP_FES_HDR_DIGEST_ERR] = "Header Digest Error", |
| 854 | [NVME_TCP_FES_DATA_OUT_OF_RANGE] = "Data Transfer Out Of Range", |
| 855 | [NVME_TCP_FES_DATA_LIMIT_EXCEEDED] = "Data Transfer Limit Exceeded", |
| 856 | [NVME_TCP_FES_UNSUPPORTED_PARAM] = "Unsupported Parameter", |
| 857 | }; |
| 858 | |
| 859 | if (plen < NVME_TCP_MIN_C2HTERM_PLEN || |
| 860 | plen > NVME_TCP_MAX_C2HTERM_PLEN) { |
| 861 | dev_err(queue->ctrl->ctrl.device, |
| 862 | "Received a malformed C2HTermReq PDU (plen = %u)\n", |
| 863 | plen); |
| 864 | return; |
| 865 | } |
| 866 | |
| 867 | fes = le16_to_cpu(pdu->fes); |
| 868 | if (fes && fes < ARRAY_SIZE(msg_table)) |
| 869 | msg = msg_table[fes]; |
| 870 | else |
| 871 | msg = "Unknown"; |
| 872 | |
| 873 | dev_err(queue->ctrl->ctrl.device, |
| 874 | "Received C2HTermReq (FES = %s)\n", msg); |
| 875 | } |
| 876 | |
| 877 | static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb, |
| 878 | unsigned int *offset, size_t *len) |
| 879 | { |
| 880 | struct nvme_tcp_hdr *hdr; |
| 881 | char *pdu = queue->pdu; |
| 882 | size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining); |
| 883 | int ret; |
| 884 | |
| 885 | ret = skb_copy_bits(skb, *offset, |
| 886 | &pdu[queue->pdu_offset], rcv_len); |
| 887 | if (unlikely(ret)) |
| 888 | return ret; |
| 889 | |
| 890 | queue->pdu_remaining -= rcv_len; |
| 891 | queue->pdu_offset += rcv_len; |
| 892 | *offset += rcv_len; |
| 893 | *len -= rcv_len; |
| 894 | if (queue->pdu_remaining) |
| 895 | return 0; |
| 896 | |
| 897 | hdr = queue->pdu; |
| 898 | if (unlikely(hdr->hlen != sizeof(struct nvme_tcp_rsp_pdu))) { |
| 899 | if (!nvme_tcp_recv_pdu_supported(hdr->type)) |
| 900 | goto unsupported_pdu; |
| 901 | |
| 902 | dev_err(queue->ctrl->ctrl.device, |
| 903 | "pdu type %d has unexpected header length (%d)\n", |
| 904 | hdr->type, hdr->hlen); |
| 905 | return -EPROTO; |
| 906 | } |
| 907 | |
| 908 | if (unlikely(hdr->type == nvme_tcp_c2h_term)) { |
| 909 | /* |
| 910 | * C2HTermReq never includes Header or Data digests. |
| 911 | * Skip the checks. |
| 912 | */ |
| 913 | nvme_tcp_handle_c2h_term(queue, (void *)queue->pdu); |
| 914 | return -EINVAL; |
| 915 | } |
| 916 | |
| 917 | if (queue->hdr_digest) { |
| 918 | ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen); |
| 919 | if (unlikely(ret)) |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | |
| 924 | if (queue->data_digest) { |
| 925 | ret = nvme_tcp_check_ddgst(queue, queue->pdu); |
| 926 | if (unlikely(ret)) |
| 927 | return ret; |
| 928 | } |
| 929 | |
| 930 | switch (hdr->type) { |
| 931 | case nvme_tcp_c2h_data: |
| 932 | return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu); |
| 933 | case nvme_tcp_rsp: |
| 934 | nvme_tcp_init_recv_ctx(queue); |
| 935 | return nvme_tcp_handle_comp(queue, (void *)queue->pdu); |
| 936 | case nvme_tcp_r2t: |
| 937 | nvme_tcp_init_recv_ctx(queue); |
| 938 | return nvme_tcp_handle_r2t(queue, (void *)queue->pdu); |
| 939 | default: |
| 940 | goto unsupported_pdu; |
| 941 | } |
| 942 | |
| 943 | unsupported_pdu: |
| 944 | dev_err(queue->ctrl->ctrl.device, |
| 945 | "unsupported pdu type (%d)\n", hdr->type); |
| 946 | return -EINVAL; |
| 947 | } |
| 948 | |
| 949 | static inline void nvme_tcp_end_request(struct request *rq, u16 status) |
| 950 | { |
| 951 | union nvme_result res = {}; |
| 952 | |
| 953 | if (!nvme_try_complete_req(rq, cpu_to_le16(status << 1), res)) |
| 954 | nvme_complete_rq(rq); |
| 955 | } |
| 956 | |
| 957 | static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb, |
| 958 | unsigned int *offset, size_t *len) |
| 959 | { |
| 960 | struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu; |
| 961 | struct request *rq = |
| 962 | nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id); |
| 963 | struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); |
| 964 | |
| 965 | while (true) { |
| 966 | int recv_len, ret; |
| 967 | |
| 968 | recv_len = min_t(size_t, *len, queue->data_remaining); |
| 969 | if (!recv_len) |
| 970 | break; |
| 971 | |
| 972 | if (!iov_iter_count(&req->iter)) { |
| 973 | req->curr_bio = req->curr_bio->bi_next; |
| 974 | |
| 975 | /* |
| 976 | * If we don't have any bios it means the controller |
| 977 | * sent more data than we requested, hence error |
| 978 | */ |
| 979 | if (!req->curr_bio) { |
| 980 | dev_err(queue->ctrl->ctrl.device, |
| 981 | "queue %d no space in request %#x", |
| 982 | nvme_tcp_queue_id(queue), rq->tag); |
| 983 | nvme_tcp_init_recv_ctx(queue); |
| 984 | return -EIO; |
| 985 | } |
| 986 | nvme_tcp_init_iter(req, ITER_DEST); |
| 987 | } |
| 988 | |
| 989 | /* we can read only from what is left in this bio */ |
| 990 | recv_len = min_t(size_t, recv_len, |
| 991 | iov_iter_count(&req->iter)); |
| 992 | |
| 993 | if (queue->data_digest) |
| 994 | ret = skb_copy_and_crc32c_datagram_iter(skb, *offset, |
| 995 | &req->iter, recv_len, &queue->rcv_crc); |
| 996 | else |
| 997 | ret = skb_copy_datagram_iter(skb, *offset, |
| 998 | &req->iter, recv_len); |
| 999 | if (ret) { |
| 1000 | dev_err(queue->ctrl->ctrl.device, |
| 1001 | "queue %d failed to copy request %#x data", |
| 1002 | nvme_tcp_queue_id(queue), rq->tag); |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
| 1006 | *len -= recv_len; |
| 1007 | *offset += recv_len; |
| 1008 | queue->data_remaining -= recv_len; |
| 1009 | req->data_recvd += recv_len; |
| 1010 | } |
| 1011 | |
| 1012 | if (!queue->data_remaining) { |
| 1013 | if (queue->data_digest) { |
| 1014 | queue->exp_ddgst = nvme_tcp_ddgst_final(queue->rcv_crc); |
| 1015 | queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH; |
| 1016 | } else { |
| 1017 | if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) { |
| 1018 | if (unlikely(nvme_tcp_data_in_short(queue, rq))) |
| 1019 | return -EPROTO; |
| 1020 | nvme_tcp_end_request(rq, |
| 1021 | le16_to_cpu(req->status)); |
| 1022 | queue->nr_cqe++; |
| 1023 | } |
| 1024 | nvme_tcp_init_recv_ctx(queue); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
| 1031 | static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue, |
| 1032 | struct sk_buff *skb, unsigned int *offset, size_t *len) |
| 1033 | { |
| 1034 | struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu; |
| 1035 | char *ddgst = (char *)&queue->recv_ddgst; |
| 1036 | size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining); |
| 1037 | off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining; |
| 1038 | int ret; |
| 1039 | |
| 1040 | ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len); |
| 1041 | if (unlikely(ret)) |
| 1042 | return ret; |
| 1043 | |
| 1044 | queue->ddgst_remaining -= recv_len; |
| 1045 | *offset += recv_len; |
| 1046 | *len -= recv_len; |
| 1047 | if (queue->ddgst_remaining) |
| 1048 | return 0; |
| 1049 | |
| 1050 | if (queue->recv_ddgst != queue->exp_ddgst) { |
| 1051 | struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue), |
| 1052 | pdu->command_id); |
| 1053 | struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); |
| 1054 | |
| 1055 | req->status = cpu_to_le16(NVME_SC_DATA_XFER_ERROR); |
| 1056 | |
| 1057 | dev_err(queue->ctrl->ctrl.device, |
| 1058 | "data digest error: recv %#x expected %#x\n", |
| 1059 | le32_to_cpu(queue->recv_ddgst), |
| 1060 | le32_to_cpu(queue->exp_ddgst)); |
| 1061 | } |
| 1062 | |
| 1063 | if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) { |
| 1064 | struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue), |
| 1065 | pdu->command_id); |
| 1066 | struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); |
| 1067 | |
| 1068 | if (unlikely(nvme_tcp_data_in_short(queue, rq))) |
| 1069 | return -EPROTO; |
| 1070 | |
| 1071 | nvme_tcp_end_request(rq, le16_to_cpu(req->status)); |
| 1072 | queue->nr_cqe++; |
| 1073 | } |
| 1074 | |
| 1075 | nvme_tcp_init_recv_ctx(queue); |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
| 1079 | static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb, |
| 1080 | unsigned int offset, size_t len) |
| 1081 | { |
| 1082 | struct nvme_tcp_queue *queue = desc->arg.data; |
| 1083 | size_t consumed = len; |
| 1084 | int result; |
| 1085 | |
| 1086 | if (unlikely(!queue->rd_enabled)) |
| 1087 | return -EFAULT; |
| 1088 | |
| 1089 | while (len) { |
| 1090 | switch (nvme_tcp_recv_state(queue)) { |
| 1091 | case NVME_TCP_RECV_PDU: |
| 1092 | result = nvme_tcp_recv_pdu(queue, skb, &offset, &len); |
| 1093 | break; |
| 1094 | case NVME_TCP_RECV_DATA: |
| 1095 | result = nvme_tcp_recv_data(queue, skb, &offset, &len); |
| 1096 | break; |
| 1097 | case NVME_TCP_RECV_DDGST: |
| 1098 | result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len); |
| 1099 | break; |
| 1100 | default: |
| 1101 | result = -EFAULT; |
| 1102 | } |
| 1103 | if (result) { |
| 1104 | dev_err(queue->ctrl->ctrl.device, |
| 1105 | "receive failed: %d\n", result); |
| 1106 | queue->rd_enabled = false; |
| 1107 | nvme_tcp_error_recovery(&queue->ctrl->ctrl); |
| 1108 | return result; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | return consumed; |
| 1113 | } |
| 1114 | |
| 1115 | static void nvme_tcp_data_ready(struct sock *sk) |
| 1116 | { |
| 1117 | struct nvme_tcp_queue *queue; |
| 1118 | |
| 1119 | trace_sk_data_ready(sk); |
| 1120 | |
| 1121 | read_lock_bh(&sk->sk_callback_lock); |
| 1122 | queue = sk->sk_user_data; |
| 1123 | if (likely(queue && queue->rd_enabled) && |
| 1124 | !test_bit(NVME_TCP_Q_POLLING, &queue->flags)) |
| 1125 | queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); |
| 1126 | read_unlock_bh(&sk->sk_callback_lock); |
| 1127 | } |
| 1128 | |
| 1129 | static void nvme_tcp_write_space(struct sock *sk) |
| 1130 | { |
| 1131 | struct nvme_tcp_queue *queue; |
| 1132 | |
| 1133 | read_lock_bh(&sk->sk_callback_lock); |
| 1134 | queue = sk->sk_user_data; |
| 1135 | if (likely(queue && sk_stream_is_writeable(sk))) { |
| 1136 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 1137 | /* Ensure pending TLS partial records are retried */ |
| 1138 | if (nvme_tcp_queue_tls(queue)) |
| 1139 | queue->write_space(sk); |
| 1140 | queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); |
| 1141 | } |
| 1142 | read_unlock_bh(&sk->sk_callback_lock); |
| 1143 | } |
| 1144 | |
| 1145 | static void nvme_tcp_state_change(struct sock *sk) |
| 1146 | { |
| 1147 | struct nvme_tcp_queue *queue; |
| 1148 | |
| 1149 | read_lock_bh(&sk->sk_callback_lock); |
| 1150 | queue = sk->sk_user_data; |
| 1151 | if (!queue) |
| 1152 | goto done; |
| 1153 | |
| 1154 | switch (sk->sk_state) { |
| 1155 | case TCP_CLOSE: |
| 1156 | case TCP_CLOSE_WAIT: |
| 1157 | case TCP_LAST_ACK: |
| 1158 | case TCP_FIN_WAIT1: |
| 1159 | case TCP_FIN_WAIT2: |
| 1160 | nvme_tcp_error_recovery(&queue->ctrl->ctrl); |
| 1161 | break; |
| 1162 | default: |
| 1163 | dev_info(queue->ctrl->ctrl.device, |
| 1164 | "queue %d socket state %d\n", |
| 1165 | nvme_tcp_queue_id(queue), sk->sk_state); |
| 1166 | } |
| 1167 | |
| 1168 | queue->state_change(sk); |
| 1169 | done: |
| 1170 | read_unlock_bh(&sk->sk_callback_lock); |
| 1171 | } |
| 1172 | |
| 1173 | static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue) |
| 1174 | { |
| 1175 | queue->request = NULL; |
| 1176 | } |
| 1177 | |
| 1178 | static void nvme_tcp_fail_request(struct nvme_tcp_request *req) |
| 1179 | { |
| 1180 | if (nvme_tcp_async_req(req)) { |
| 1181 | union nvme_result res = {}; |
| 1182 | |
| 1183 | nvme_complete_async_event(&req->queue->ctrl->ctrl, |
| 1184 | cpu_to_le16(NVME_SC_HOST_PATH_ERROR), &res); |
| 1185 | } else { |
| 1186 | nvme_tcp_end_request(blk_mq_rq_from_pdu(req), |
| 1187 | NVME_SC_HOST_PATH_ERROR); |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | static int nvme_tcp_try_send_data(struct nvme_tcp_request *req) |
| 1192 | { |
| 1193 | struct nvme_tcp_queue *queue = req->queue; |
| 1194 | int req_data_len = req->data_len; |
| 1195 | u32 h2cdata_left = req->h2cdata_left; |
| 1196 | |
| 1197 | while (true) { |
| 1198 | struct bio_vec bvec; |
| 1199 | struct msghdr msg = { |
| 1200 | .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES, |
| 1201 | }; |
| 1202 | struct page *page = nvme_tcp_req_cur_page(req); |
| 1203 | size_t offset = nvme_tcp_req_cur_offset(req); |
| 1204 | size_t len = nvme_tcp_req_cur_length(req); |
| 1205 | bool last = nvme_tcp_pdu_last_send(req, len); |
| 1206 | int req_data_sent = req->data_sent; |
| 1207 | int ret; |
| 1208 | |
| 1209 | if (last && !queue->data_digest && !nvme_tcp_queue_more(queue)) |
| 1210 | msg.msg_flags |= MSG_EOR; |
| 1211 | else |
| 1212 | msg.msg_flags |= MSG_MORE; |
| 1213 | |
| 1214 | if (!sendpages_ok(page, len, offset)) |
| 1215 | msg.msg_flags &= ~MSG_SPLICE_PAGES; |
| 1216 | |
| 1217 | bvec_set_page(&bvec, page, len, offset); |
| 1218 | iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len); |
| 1219 | ret = sock_sendmsg(queue->sock, &msg); |
| 1220 | if (ret <= 0) |
| 1221 | return ret; |
| 1222 | |
| 1223 | if (queue->data_digest) |
| 1224 | nvme_tcp_ddgst_update(&queue->snd_crc, page, |
| 1225 | offset, ret); |
| 1226 | |
| 1227 | /* |
| 1228 | * update the request iterator except for the last payload send |
| 1229 | * in the request where we don't want to modify it as we may |
| 1230 | * compete with the RX path completing the request. |
| 1231 | */ |
| 1232 | if (req_data_sent + ret < req_data_len) |
| 1233 | nvme_tcp_advance_req(req, ret); |
| 1234 | |
| 1235 | /* fully successful last send in current PDU */ |
| 1236 | if (last && ret == len) { |
| 1237 | if (queue->data_digest) { |
| 1238 | req->ddgst = |
| 1239 | nvme_tcp_ddgst_final(queue->snd_crc); |
| 1240 | req->state = NVME_TCP_SEND_DDGST; |
| 1241 | req->offset = 0; |
| 1242 | } else { |
| 1243 | if (h2cdata_left) |
| 1244 | nvme_tcp_setup_h2c_data_pdu(req); |
| 1245 | else |
| 1246 | nvme_tcp_done_send_req(queue); |
| 1247 | } |
| 1248 | return 1; |
| 1249 | } |
| 1250 | } |
| 1251 | return -EAGAIN; |
| 1252 | } |
| 1253 | |
| 1254 | static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req) |
| 1255 | { |
| 1256 | struct nvme_tcp_queue *queue = req->queue; |
| 1257 | struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); |
| 1258 | struct bio_vec bvec; |
| 1259 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES, }; |
| 1260 | bool inline_data = nvme_tcp_has_inline_data(req); |
| 1261 | u8 hdgst = nvme_tcp_hdgst_len(queue); |
| 1262 | int len = sizeof(*pdu) + hdgst - req->offset; |
| 1263 | int ret; |
| 1264 | |
| 1265 | if (inline_data || nvme_tcp_queue_more(queue)) |
| 1266 | msg.msg_flags |= MSG_MORE; |
| 1267 | else |
| 1268 | msg.msg_flags |= MSG_EOR; |
| 1269 | |
| 1270 | if (queue->hdr_digest && !req->offset) |
| 1271 | nvme_tcp_set_hdgst(pdu, sizeof(*pdu)); |
| 1272 | |
| 1273 | bvec_set_virt(&bvec, (void *)pdu + req->offset, len); |
| 1274 | iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len); |
| 1275 | ret = sock_sendmsg(queue->sock, &msg); |
| 1276 | if (unlikely(ret <= 0)) |
| 1277 | return ret; |
| 1278 | |
| 1279 | len -= ret; |
| 1280 | if (!len) { |
| 1281 | if (inline_data) { |
| 1282 | req->state = NVME_TCP_SEND_DATA; |
| 1283 | if (queue->data_digest) |
| 1284 | queue->snd_crc = NVME_TCP_CRC_SEED; |
| 1285 | } else { |
| 1286 | nvme_tcp_done_send_req(queue); |
| 1287 | } |
| 1288 | return 1; |
| 1289 | } |
| 1290 | req->offset += ret; |
| 1291 | |
| 1292 | return -EAGAIN; |
| 1293 | } |
| 1294 | |
| 1295 | static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req) |
| 1296 | { |
| 1297 | struct nvme_tcp_queue *queue = req->queue; |
| 1298 | struct nvme_tcp_data_pdu *pdu = nvme_tcp_req_data_pdu(req); |
| 1299 | struct bio_vec bvec; |
| 1300 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_MORE, }; |
| 1301 | u8 hdgst = nvme_tcp_hdgst_len(queue); |
| 1302 | int len = sizeof(*pdu) - req->offset + hdgst; |
| 1303 | int ret; |
| 1304 | |
| 1305 | if (queue->hdr_digest && !req->offset) |
| 1306 | nvme_tcp_set_hdgst(pdu, sizeof(*pdu)); |
| 1307 | |
| 1308 | if (!req->h2cdata_left) |
| 1309 | msg.msg_flags |= MSG_SPLICE_PAGES; |
| 1310 | |
| 1311 | bvec_set_virt(&bvec, (void *)pdu + req->offset, len); |
| 1312 | iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len); |
| 1313 | ret = sock_sendmsg(queue->sock, &msg); |
| 1314 | if (unlikely(ret <= 0)) |
| 1315 | return ret; |
| 1316 | |
| 1317 | len -= ret; |
| 1318 | if (!len) { |
| 1319 | req->state = NVME_TCP_SEND_DATA; |
| 1320 | if (queue->data_digest) |
| 1321 | queue->snd_crc = NVME_TCP_CRC_SEED; |
| 1322 | return 1; |
| 1323 | } |
| 1324 | req->offset += ret; |
| 1325 | |
| 1326 | return -EAGAIN; |
| 1327 | } |
| 1328 | |
| 1329 | static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req) |
| 1330 | { |
| 1331 | struct nvme_tcp_queue *queue = req->queue; |
| 1332 | size_t offset = req->offset; |
| 1333 | u32 h2cdata_left = req->h2cdata_left; |
| 1334 | int ret; |
| 1335 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT }; |
| 1336 | struct kvec iov = { |
| 1337 | .iov_base = (u8 *)&req->ddgst + req->offset, |
| 1338 | .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset |
| 1339 | }; |
| 1340 | |
| 1341 | if (nvme_tcp_queue_more(queue)) |
| 1342 | msg.msg_flags |= MSG_MORE; |
| 1343 | else |
| 1344 | msg.msg_flags |= MSG_EOR; |
| 1345 | |
| 1346 | ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len); |
| 1347 | if (unlikely(ret <= 0)) |
| 1348 | return ret; |
| 1349 | |
| 1350 | if (offset + ret == NVME_TCP_DIGEST_LENGTH) { |
| 1351 | if (h2cdata_left) |
| 1352 | nvme_tcp_setup_h2c_data_pdu(req); |
| 1353 | else |
| 1354 | nvme_tcp_done_send_req(queue); |
| 1355 | return 1; |
| 1356 | } |
| 1357 | |
| 1358 | req->offset += ret; |
| 1359 | return -EAGAIN; |
| 1360 | } |
| 1361 | |
| 1362 | static int nvme_tcp_try_send(struct nvme_tcp_queue *queue) |
| 1363 | { |
| 1364 | struct nvme_tcp_request *req; |
| 1365 | unsigned int noreclaim_flag; |
| 1366 | int ret = 1; |
| 1367 | |
| 1368 | if (!queue->request) { |
| 1369 | queue->request = nvme_tcp_fetch_request(queue); |
| 1370 | if (!queue->request) |
| 1371 | return 0; |
| 1372 | } |
| 1373 | req = queue->request; |
| 1374 | |
| 1375 | noreclaim_flag = memalloc_noreclaim_save(); |
| 1376 | if (req->state == NVME_TCP_SEND_CMD_PDU) { |
| 1377 | ret = nvme_tcp_try_send_cmd_pdu(req); |
| 1378 | if (ret <= 0) |
| 1379 | goto done; |
| 1380 | if (!nvme_tcp_has_inline_data(req)) |
| 1381 | goto out; |
| 1382 | } |
| 1383 | |
| 1384 | if (req->state == NVME_TCP_SEND_H2C_PDU) { |
| 1385 | ret = nvme_tcp_try_send_data_pdu(req); |
| 1386 | if (ret <= 0) |
| 1387 | goto done; |
| 1388 | } |
| 1389 | |
| 1390 | if (req->state == NVME_TCP_SEND_DATA) { |
| 1391 | ret = nvme_tcp_try_send_data(req); |
| 1392 | if (ret <= 0) |
| 1393 | goto done; |
| 1394 | } |
| 1395 | |
| 1396 | if (req->state == NVME_TCP_SEND_DDGST) |
| 1397 | ret = nvme_tcp_try_send_ddgst(req); |
| 1398 | done: |
| 1399 | if (ret == -EAGAIN) { |
| 1400 | ret = 0; |
| 1401 | } else if (ret < 0) { |
| 1402 | dev_err(queue->ctrl->ctrl.device, |
| 1403 | "failed to send request %d\n", ret); |
| 1404 | nvme_tcp_fail_request(queue->request); |
| 1405 | nvme_tcp_done_send_req(queue); |
| 1406 | } |
| 1407 | out: |
| 1408 | memalloc_noreclaim_restore(noreclaim_flag); |
| 1409 | return ret; |
| 1410 | } |
| 1411 | |
| 1412 | static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue) |
| 1413 | { |
| 1414 | struct socket *sock = queue->sock; |
| 1415 | struct sock *sk = sock->sk; |
| 1416 | read_descriptor_t rd_desc; |
| 1417 | int consumed; |
| 1418 | |
| 1419 | rd_desc.arg.data = queue; |
| 1420 | rd_desc.count = 1; |
| 1421 | lock_sock(sk); |
| 1422 | queue->nr_cqe = 0; |
| 1423 | consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb); |
| 1424 | release_sock(sk); |
| 1425 | return consumed == -EAGAIN ? 0 : consumed; |
| 1426 | } |
| 1427 | |
| 1428 | static void nvme_tcp_io_work(struct work_struct *w) |
| 1429 | { |
| 1430 | struct nvme_tcp_queue *queue = |
| 1431 | container_of(w, struct nvme_tcp_queue, io_work); |
| 1432 | unsigned long deadline = jiffies + msecs_to_jiffies(1); |
| 1433 | |
| 1434 | do { |
| 1435 | bool pending = false; |
| 1436 | int result; |
| 1437 | |
| 1438 | if (mutex_trylock(&queue->send_mutex)) { |
| 1439 | result = nvme_tcp_try_send(queue); |
| 1440 | mutex_unlock(&queue->send_mutex); |
| 1441 | if (result > 0) |
| 1442 | pending = true; |
| 1443 | else if (unlikely(result < 0)) |
| 1444 | break; |
| 1445 | } |
| 1446 | |
| 1447 | result = nvme_tcp_try_recv(queue); |
| 1448 | if (result > 0) |
| 1449 | pending = true; |
| 1450 | else if (unlikely(result < 0)) |
| 1451 | return; |
| 1452 | |
| 1453 | /* did we get some space after spending time in recv? */ |
| 1454 | if (nvme_tcp_queue_has_pending(queue) && |
| 1455 | sk_stream_is_writeable(queue->sock->sk)) |
| 1456 | pending = true; |
| 1457 | |
| 1458 | if (!pending || !queue->rd_enabled) |
| 1459 | return; |
| 1460 | |
| 1461 | } while (!time_after(jiffies, deadline)); /* quota is exhausted */ |
| 1462 | |
| 1463 | queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); |
| 1464 | } |
| 1465 | |
| 1466 | static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl) |
| 1467 | { |
| 1468 | struct nvme_tcp_request *async = &ctrl->async_req; |
| 1469 | |
| 1470 | page_frag_free(async->pdu); |
| 1471 | } |
| 1472 | |
| 1473 | static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl) |
| 1474 | { |
| 1475 | struct nvme_tcp_queue *queue = &ctrl->queues[0]; |
| 1476 | struct nvme_tcp_request *async = &ctrl->async_req; |
| 1477 | u8 hdgst = nvme_tcp_hdgst_len(queue); |
| 1478 | |
| 1479 | mutex_lock(&queue->pf_cache_lock); |
| 1480 | async->pdu = page_frag_alloc(&queue->pf_cache, |
| 1481 | sizeof(struct nvme_tcp_cmd_pdu) + hdgst, |
| 1482 | GFP_KERNEL | __GFP_ZERO); |
| 1483 | mutex_unlock(&queue->pf_cache_lock); |
| 1484 | if (!async->pdu) |
| 1485 | return -ENOMEM; |
| 1486 | |
| 1487 | async->queue = &ctrl->queues[0]; |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
| 1491 | static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid) |
| 1492 | { |
| 1493 | struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); |
| 1494 | struct nvme_tcp_queue *queue = &ctrl->queues[qid]; |
| 1495 | unsigned int noio_flag; |
| 1496 | |
| 1497 | if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) |
| 1498 | return; |
| 1499 | |
| 1500 | page_frag_cache_drain(&queue->pf_cache); |
| 1501 | |
| 1502 | /** |
| 1503 | * Prevent memory reclaim from triggering block I/O during socket |
| 1504 | * teardown. The socket release path fput -> tcp_close -> |
| 1505 | * tcp_disconnect -> tcp_send_active_reset may allocate memory, and |
| 1506 | * allowing reclaim to issue I/O could deadlock if we're being called |
| 1507 | * from block device teardown (e.g., del_gendisk -> elevator cleanup) |
| 1508 | * which holds locks that the I/O completion path needs. |
| 1509 | */ |
| 1510 | noio_flag = memalloc_noio_save(); |
| 1511 | |
| 1512 | /** |
| 1513 | * Release the socket synchronously. During reset in |
| 1514 | * nvme_reset_ctrl_work(), queue teardown is immediately followed by |
| 1515 | * re-allocation. fput() defers socket cleanup to delayed_fput_work |
| 1516 | * in workqueue context, which can race with new queue setup. |
| 1517 | */ |
| 1518 | __fput_sync(queue->sock->file); |
| 1519 | queue->sock = NULL; |
| 1520 | memalloc_noio_restore(noio_flag); |
| 1521 | |
| 1522 | kfree(queue->pdu); |
| 1523 | mutex_destroy(&queue->send_mutex); |
| 1524 | mutex_destroy(&queue->queue_lock); |
| 1525 | mutex_destroy(&queue->pf_cache_lock); |
| 1526 | } |
| 1527 | |
| 1528 | static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue) |
| 1529 | { |
| 1530 | struct nvme_tcp_icreq_pdu *icreq; |
| 1531 | struct nvme_tcp_icresp_pdu *icresp; |
| 1532 | char cbuf[CMSG_LEN(sizeof(char))] = {}; |
| 1533 | u8 ctype; |
| 1534 | struct msghdr msg = {}; |
| 1535 | struct kvec iov; |
| 1536 | bool ctrl_hdgst, ctrl_ddgst; |
| 1537 | u32 maxh2cdata; |
| 1538 | int ret; |
| 1539 | |
| 1540 | icreq = kzalloc_obj(*icreq); |
| 1541 | if (!icreq) |
| 1542 | return -ENOMEM; |
| 1543 | |
| 1544 | icresp = kzalloc_obj(*icresp); |
| 1545 | if (!icresp) { |
| 1546 | ret = -ENOMEM; |
| 1547 | goto free_icreq; |
| 1548 | } |
| 1549 | |
| 1550 | icreq->hdr.type = nvme_tcp_icreq; |
| 1551 | icreq->hdr.hlen = sizeof(*icreq); |
| 1552 | icreq->hdr.pdo = 0; |
| 1553 | icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen); |
| 1554 | icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0); |
| 1555 | icreq->maxr2t = 0; /* single inflight r2t supported */ |
| 1556 | icreq->hpda = 0; /* no alignment constraint */ |
| 1557 | if (queue->hdr_digest) |
| 1558 | icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE; |
| 1559 | if (queue->data_digest) |
| 1560 | icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE; |
| 1561 | |
| 1562 | iov.iov_base = icreq; |
| 1563 | iov.iov_len = sizeof(*icreq); |
| 1564 | ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len); |
| 1565 | if (ret < 0) { |
| 1566 | pr_warn("queue %d: failed to send icreq, error %d\n", |
| 1567 | nvme_tcp_queue_id(queue), ret); |
| 1568 | goto free_icresp; |
| 1569 | } |
| 1570 | |
| 1571 | memset(&msg, 0, sizeof(msg)); |
| 1572 | iov.iov_base = icresp; |
| 1573 | iov.iov_len = sizeof(*icresp); |
| 1574 | if (nvme_tcp_queue_tls(queue)) { |
| 1575 | msg.msg_control = cbuf; |
| 1576 | msg.msg_controllen = sizeof(cbuf); |
| 1577 | } |
| 1578 | msg.msg_flags = MSG_WAITALL; |
| 1579 | ret = kernel_recvmsg(queue->sock, &msg, &iov, 1, |
| 1580 | iov.iov_len, msg.msg_flags); |
| 1581 | if (ret >= 0 && ret < sizeof(*icresp)) |
| 1582 | ret = -ECONNRESET; |
| 1583 | if (ret < 0) { |
| 1584 | pr_warn("queue %d: failed to receive icresp, error %d\n", |
| 1585 | nvme_tcp_queue_id(queue), ret); |
| 1586 | goto free_icresp; |
| 1587 | } |
| 1588 | ret = -ENOTCONN; |
| 1589 | if (nvme_tcp_queue_tls(queue)) { |
| 1590 | ctype = tls_get_record_type(queue->sock->sk, |
| 1591 | (struct cmsghdr *)cbuf); |
| 1592 | if (ctype != TLS_RECORD_TYPE_DATA) { |
| 1593 | pr_err("queue %d: unhandled TLS record %d\n", |
| 1594 | nvme_tcp_queue_id(queue), ctype); |
| 1595 | goto free_icresp; |
| 1596 | } |
| 1597 | } |
| 1598 | ret = -EINVAL; |
| 1599 | if (icresp->hdr.type != nvme_tcp_icresp) { |
| 1600 | pr_err("queue %d: bad type returned %d\n", |
| 1601 | nvme_tcp_queue_id(queue), icresp->hdr.type); |
| 1602 | goto free_icresp; |
| 1603 | } |
| 1604 | |
| 1605 | if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) { |
| 1606 | pr_err("queue %d: bad pdu length returned %d\n", |
| 1607 | nvme_tcp_queue_id(queue), icresp->hdr.plen); |
| 1608 | goto free_icresp; |
| 1609 | } |
| 1610 | |
| 1611 | if (icresp->pfv != NVME_TCP_PFV_1_0) { |
| 1612 | pr_err("queue %d: bad pfv returned %d\n", |
| 1613 | nvme_tcp_queue_id(queue), icresp->pfv); |
| 1614 | goto free_icresp; |
| 1615 | } |
| 1616 | |
| 1617 | ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE); |
| 1618 | if ((queue->data_digest && !ctrl_ddgst) || |
| 1619 | (!queue->data_digest && ctrl_ddgst)) { |
| 1620 | pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n", |
| 1621 | nvme_tcp_queue_id(queue), |
| 1622 | queue->data_digest ? "enabled" : "disabled", |
| 1623 | ctrl_ddgst ? "enabled" : "disabled"); |
| 1624 | goto free_icresp; |
| 1625 | } |
| 1626 | |
| 1627 | ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE); |
| 1628 | if ((queue->hdr_digest && !ctrl_hdgst) || |
| 1629 | (!queue->hdr_digest && ctrl_hdgst)) { |
| 1630 | pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n", |
| 1631 | nvme_tcp_queue_id(queue), |
| 1632 | queue->hdr_digest ? "enabled" : "disabled", |
| 1633 | ctrl_hdgst ? "enabled" : "disabled"); |
| 1634 | goto free_icresp; |
| 1635 | } |
| 1636 | |
| 1637 | if (icresp->cpda != 0) { |
| 1638 | pr_err("queue %d: unsupported cpda returned %d\n", |
| 1639 | nvme_tcp_queue_id(queue), icresp->cpda); |
| 1640 | goto free_icresp; |
| 1641 | } |
| 1642 | |
| 1643 | maxh2cdata = le32_to_cpu(icresp->maxdata); |
| 1644 | if ((maxh2cdata % 4) || (maxh2cdata < NVME_TCP_MIN_MAXH2CDATA)) { |
| 1645 | pr_err("queue %d: invalid maxh2cdata returned %u\n", |
| 1646 | nvme_tcp_queue_id(queue), maxh2cdata); |
| 1647 | goto free_icresp; |
| 1648 | } |
| 1649 | queue->maxh2cdata = maxh2cdata; |
| 1650 | |
| 1651 | ret = 0; |
| 1652 | free_icresp: |
| 1653 | kfree(icresp); |
| 1654 | free_icreq: |
| 1655 | kfree(icreq); |
| 1656 | return ret; |
| 1657 | } |
| 1658 | |
| 1659 | static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue) |
| 1660 | { |
| 1661 | return nvme_tcp_queue_id(queue) == 0; |
| 1662 | } |
| 1663 | |
| 1664 | static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue) |
| 1665 | { |
| 1666 | struct nvme_tcp_ctrl *ctrl = queue->ctrl; |
| 1667 | int qid = nvme_tcp_queue_id(queue); |
| 1668 | |
| 1669 | return !nvme_tcp_admin_queue(queue) && |
| 1670 | qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT]; |
| 1671 | } |
| 1672 | |
| 1673 | static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue) |
| 1674 | { |
| 1675 | struct nvme_tcp_ctrl *ctrl = queue->ctrl; |
| 1676 | int qid = nvme_tcp_queue_id(queue); |
| 1677 | |
| 1678 | return !nvme_tcp_admin_queue(queue) && |
| 1679 | !nvme_tcp_default_queue(queue) && |
| 1680 | qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] + |
| 1681 | ctrl->io_queues[HCTX_TYPE_READ]; |
| 1682 | } |
| 1683 | |
| 1684 | static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue) |
| 1685 | { |
| 1686 | struct nvme_tcp_ctrl *ctrl = queue->ctrl; |
| 1687 | int qid = nvme_tcp_queue_id(queue); |
| 1688 | |
| 1689 | return !nvme_tcp_admin_queue(queue) && |
| 1690 | !nvme_tcp_default_queue(queue) && |
| 1691 | !nvme_tcp_read_queue(queue) && |
| 1692 | qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] + |
| 1693 | ctrl->io_queues[HCTX_TYPE_READ] + |
| 1694 | ctrl->io_queues[HCTX_TYPE_POLL]; |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * Track the number of queues assigned to each cpu using a global per-cpu |
| 1699 | * counter and select the least used cpu from the mq_map. Our goal is to spread |
| 1700 | * different controllers I/O threads across different cpu cores. |
| 1701 | * |
| 1702 | * Note that the accounting is not 100% perfect, but we don't need to be, we're |
| 1703 | * simply putting our best effort to select the best candidate cpu core that we |
| 1704 | * find at any given point. |
| 1705 | */ |
| 1706 | static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue) |
| 1707 | { |
| 1708 | struct nvme_tcp_ctrl *ctrl = queue->ctrl; |
| 1709 | struct blk_mq_tag_set *set = &ctrl->tag_set; |
| 1710 | int qid = nvme_tcp_queue_id(queue) - 1; |
| 1711 | unsigned int *mq_map = NULL; |
| 1712 | int cpu, min_queues = INT_MAX, io_cpu; |
| 1713 | |
| 1714 | if (wq_unbound) |
| 1715 | goto out; |
| 1716 | |
| 1717 | if (nvme_tcp_default_queue(queue)) |
| 1718 | mq_map = set->map[HCTX_TYPE_DEFAULT].mq_map; |
| 1719 | else if (nvme_tcp_read_queue(queue)) |
| 1720 | mq_map = set->map[HCTX_TYPE_READ].mq_map; |
| 1721 | else if (nvme_tcp_poll_queue(queue)) |
| 1722 | mq_map = set->map[HCTX_TYPE_POLL].mq_map; |
| 1723 | |
| 1724 | if (WARN_ON(!mq_map)) |
| 1725 | goto out; |
| 1726 | |
| 1727 | /* Search for the least used cpu from the mq_map */ |
| 1728 | io_cpu = WORK_CPU_UNBOUND; |
| 1729 | for_each_online_cpu(cpu) { |
| 1730 | int num_queues = atomic_read(&nvme_tcp_cpu_queues[cpu]); |
| 1731 | |
| 1732 | if (mq_map[cpu] != qid) |
| 1733 | continue; |
| 1734 | if (num_queues < min_queues) { |
| 1735 | io_cpu = cpu; |
| 1736 | min_queues = num_queues; |
| 1737 | } |
| 1738 | } |
| 1739 | if (io_cpu != WORK_CPU_UNBOUND) { |
| 1740 | queue->io_cpu = io_cpu; |
| 1741 | atomic_inc(&nvme_tcp_cpu_queues[io_cpu]); |
| 1742 | set_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags); |
| 1743 | } |
| 1744 | out: |
| 1745 | dev_dbg(ctrl->ctrl.device, "queue %d: using cpu %d\n", |
| 1746 | qid, queue->io_cpu); |
| 1747 | } |
| 1748 | |
| 1749 | static void nvme_tcp_tls_done(void *data, int status, key_serial_t pskid) |
| 1750 | { |
| 1751 | struct nvme_tcp_queue *queue = data; |
| 1752 | struct nvme_tcp_ctrl *ctrl = queue->ctrl; |
| 1753 | int qid = nvme_tcp_queue_id(queue); |
| 1754 | struct key *tls_key; |
| 1755 | |
| 1756 | dev_dbg(ctrl->ctrl.device, "queue %d: TLS handshake done, key %x, status %d\n", |
| 1757 | qid, pskid, status); |
| 1758 | |
| 1759 | if (status) { |
| 1760 | queue->tls_err = status; |
| 1761 | goto out_complete; |
| 1762 | } |
| 1763 | |
| 1764 | tls_key = nvme_tls_key_lookup(pskid); |
| 1765 | if (IS_ERR(tls_key)) { |
| 1766 | dev_warn(ctrl->ctrl.device, "queue %d: Invalid key %x\n", |
| 1767 | qid, pskid); |
| 1768 | queue->tls_err = -ENOKEY; |
| 1769 | } else { |
| 1770 | queue->tls_enabled = true; |
| 1771 | if (qid == 0) |
| 1772 | ctrl->ctrl.tls_pskid = key_serial(tls_key); |
| 1773 | key_put(tls_key); |
| 1774 | queue->tls_err = 0; |
| 1775 | } |
| 1776 | |
| 1777 | out_complete: |
| 1778 | complete(&queue->tls_complete); |
| 1779 | } |
| 1780 | |
| 1781 | static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl, |
| 1782 | struct nvme_tcp_queue *queue, |
| 1783 | key_serial_t pskid) |
| 1784 | { |
| 1785 | int qid = nvme_tcp_queue_id(queue); |
| 1786 | int ret; |
| 1787 | struct tls_handshake_args args; |
| 1788 | unsigned long tmo = tls_handshake_timeout * HZ; |
| 1789 | key_serial_t keyring = nvme_keyring_id(); |
| 1790 | |
| 1791 | dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n", |
| 1792 | qid, pskid); |
| 1793 | memset(&args, 0, sizeof(args)); |
| 1794 | args.ta_sock = queue->sock; |
| 1795 | args.ta_done = nvme_tcp_tls_done; |
| 1796 | args.ta_data = queue; |
| 1797 | args.ta_my_peerids[0] = pskid; |
| 1798 | args.ta_num_peerids = 1; |
| 1799 | if (nctrl->opts->keyring) |
| 1800 | keyring = key_serial(nctrl->opts->keyring); |
| 1801 | args.ta_keyring = keyring; |
| 1802 | args.ta_timeout_ms = tls_handshake_timeout * 1000; |
| 1803 | queue->tls_err = -EOPNOTSUPP; |
| 1804 | init_completion(&queue->tls_complete); |
| 1805 | ret = tls_client_hello_psk(&args, GFP_KERNEL); |
| 1806 | if (ret) { |
| 1807 | dev_err(nctrl->device, "queue %d: failed to start TLS: %d\n", |
| 1808 | qid, ret); |
| 1809 | return ret; |
| 1810 | } |
| 1811 | ret = wait_for_completion_interruptible_timeout(&queue->tls_complete, tmo); |
| 1812 | if (ret <= 0) { |
| 1813 | if (ret == 0) |
| 1814 | ret = -ETIMEDOUT; |
| 1815 | |
| 1816 | dev_err(nctrl->device, |
| 1817 | "queue %d: TLS handshake failed, error %d\n", |
| 1818 | qid, ret); |
| 1819 | tls_handshake_cancel(queue->sock->sk); |
| 1820 | } else { |
| 1821 | if (queue->tls_err) { |
| 1822 | dev_err(nctrl->device, |
| 1823 | "queue %d: TLS handshake complete, error %d\n", |
| 1824 | qid, queue->tls_err); |
| 1825 | } else { |
| 1826 | dev_dbg(nctrl->device, |
| 1827 | "queue %d: TLS handshake complete\n", qid); |
| 1828 | } |
| 1829 | ret = queue->tls_err; |
| 1830 | } |
| 1831 | return ret; |
| 1832 | } |
| 1833 | |
| 1834 | static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid, |
| 1835 | key_serial_t pskid) |
| 1836 | { |
| 1837 | struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); |
| 1838 | struct nvme_tcp_queue *queue = &ctrl->queues[qid]; |
| 1839 | int ret, rcv_pdu_size; |
| 1840 | struct file *sock_file; |
| 1841 | |
| 1842 | mutex_init(&queue->queue_lock); |
| 1843 | queue->ctrl = ctrl; |
| 1844 | init_llist_head(&queue->req_list); |
| 1845 | INIT_LIST_HEAD(&queue->send_list); |
| 1846 | mutex_init(&queue->send_mutex); |
| 1847 | INIT_WORK(&queue->io_work, nvme_tcp_io_work); |
| 1848 | mutex_init(&queue->pf_cache_lock); |
| 1849 | |
| 1850 | if (qid > 0) |
| 1851 | queue->cmnd_capsule_len = nctrl->ioccsz * 16; |
| 1852 | else |
| 1853 | queue->cmnd_capsule_len = sizeof(struct nvme_command) + |
| 1854 | NVME_TCP_ADMIN_CCSZ; |
| 1855 | |
| 1856 | ret = sock_create_kern(current->nsproxy->net_ns, |
| 1857 | ctrl->addr.ss_family, SOCK_STREAM, |
| 1858 | IPPROTO_TCP, &queue->sock); |
| 1859 | if (ret) { |
| 1860 | dev_err(nctrl->device, |
| 1861 | "failed to create socket: %d\n", ret); |
| 1862 | goto err_destroy_mutex; |
| 1863 | } |
| 1864 | |
| 1865 | sock_file = sock_alloc_file(queue->sock, O_CLOEXEC, NULL); |
| 1866 | if (IS_ERR(sock_file)) { |
| 1867 | ret = PTR_ERR(sock_file); |
| 1868 | goto err_destroy_mutex; |
| 1869 | } |
| 1870 | |
| 1871 | sk_net_refcnt_upgrade(queue->sock->sk); |
| 1872 | |
| 1873 | /* Single syn retry */ |
| 1874 | tcp_sock_set_syncnt(queue->sock->sk, 1); |
| 1875 | |
| 1876 | /* Set TCP no delay */ |
| 1877 | tcp_sock_set_nodelay(queue->sock->sk); |
| 1878 | |
| 1879 | /* |
| 1880 | * Cleanup whatever is sitting in the TCP transmit queue on socket |
| 1881 | * close. This is done to prevent stale data from being sent should |
| 1882 | * the network connection be restored before TCP times out. |
| 1883 | */ |
| 1884 | sock_no_linger(queue->sock->sk); |
| 1885 | |
| 1886 | if (so_priority > 0) |
| 1887 | sock_set_priority(queue->sock->sk, so_priority); |
| 1888 | |
| 1889 | /* Set socket type of service */ |
| 1890 | if (nctrl->opts->tos >= 0) |
| 1891 | ip_sock_set_tos(queue->sock->sk, nctrl->opts->tos); |
| 1892 | |
| 1893 | /* Set 10 seconds timeout for icresp recvmsg */ |
| 1894 | queue->sock->sk->sk_rcvtimeo = 10 * HZ; |
| 1895 | |
| 1896 | queue->sock->sk->sk_allocation = GFP_ATOMIC; |
| 1897 | queue->sock->sk->sk_use_task_frag = false; |
| 1898 | queue->io_cpu = WORK_CPU_UNBOUND; |
| 1899 | queue->request = NULL; |
| 1900 | queue->data_remaining = 0; |
| 1901 | queue->ddgst_remaining = 0; |
| 1902 | queue->pdu_remaining = 0; |
| 1903 | queue->pdu_offset = 0; |
| 1904 | sk_set_memalloc(queue->sock->sk); |
| 1905 | |
| 1906 | if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) { |
| 1907 | ret = kernel_bind(queue->sock, (struct sockaddr_unsized *)&ctrl->src_addr, |
| 1908 | sizeof(ctrl->src_addr)); |
| 1909 | if (ret) { |
| 1910 | dev_err(nctrl->device, |
| 1911 | "failed to bind queue %d socket %d\n", |
| 1912 | qid, ret); |
| 1913 | goto err_sock; |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | if (nctrl->opts->mask & NVMF_OPT_HOST_IFACE) { |
| 1918 | char *iface = nctrl->opts->host_iface; |
| 1919 | sockptr_t optval = KERNEL_SOCKPTR(iface); |
| 1920 | |
| 1921 | ret = sock_setsockopt(queue->sock, SOL_SOCKET, SO_BINDTODEVICE, |
| 1922 | optval, strlen(iface)); |
| 1923 | if (ret) { |
| 1924 | dev_err(nctrl->device, |
| 1925 | "failed to bind to interface %s queue %d err %d\n", |
| 1926 | iface, qid, ret); |
| 1927 | goto err_sock; |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | queue->hdr_digest = nctrl->opts->hdr_digest; |
| 1932 | queue->data_digest = nctrl->opts->data_digest; |
| 1933 | |
| 1934 | rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) + |
| 1935 | nvme_tcp_hdgst_len(queue); |
| 1936 | queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL); |
| 1937 | if (!queue->pdu) { |
| 1938 | ret = -ENOMEM; |
| 1939 | goto err_sock; |
| 1940 | } |
| 1941 | |
| 1942 | dev_dbg(nctrl->device, "connecting queue %d\n", |
| 1943 | nvme_tcp_queue_id(queue)); |
| 1944 | |
| 1945 | ret = kernel_connect(queue->sock, (struct sockaddr_unsized *)&ctrl->addr, |
| 1946 | sizeof(ctrl->addr), 0); |
| 1947 | if (ret) { |
| 1948 | dev_err(nctrl->device, |
| 1949 | "failed to connect socket: %d\n", ret); |
| 1950 | goto err_rcv_pdu; |
| 1951 | } |
| 1952 | |
| 1953 | /* If PSKs are configured try to start TLS */ |
| 1954 | if (nvme_tcp_tls_configured(nctrl) && pskid) { |
| 1955 | ret = nvme_tcp_start_tls(nctrl, queue, pskid); |
| 1956 | if (ret) |
| 1957 | goto err_init_connect; |
| 1958 | } |
| 1959 | |
| 1960 | nvme_tcp_reclassify_socket(queue->sock); |
| 1961 | |
| 1962 | ret = nvme_tcp_init_connection(queue); |
| 1963 | if (ret) |
| 1964 | goto err_init_connect; |
| 1965 | |
| 1966 | set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags); |
| 1967 | |
| 1968 | return 0; |
| 1969 | |
| 1970 | err_init_connect: |
| 1971 | kernel_sock_shutdown(queue->sock, SHUT_RDWR); |
| 1972 | err_rcv_pdu: |
| 1973 | kfree(queue->pdu); |
| 1974 | err_sock: |
| 1975 | /* Use sync variant - see nvme_tcp_free_queue() for explanation */ |
| 1976 | __fput_sync(queue->sock->file); |
| 1977 | queue->sock = NULL; |
| 1978 | err_destroy_mutex: |
| 1979 | mutex_destroy(&queue->send_mutex); |
| 1980 | mutex_destroy(&queue->queue_lock); |
| 1981 | mutex_destroy(&queue->pf_cache_lock); |
| 1982 | return ret; |
| 1983 | } |
| 1984 | |
| 1985 | static void nvme_tcp_restore_sock_ops(struct nvme_tcp_queue *queue) |
| 1986 | { |
| 1987 | struct socket *sock = queue->sock; |
| 1988 | |
| 1989 | write_lock_bh(&sock->sk->sk_callback_lock); |
| 1990 | sock->sk->sk_user_data = NULL; |
| 1991 | sock->sk->sk_data_ready = queue->data_ready; |
| 1992 | sock->sk->sk_state_change = queue->state_change; |
| 1993 | sock->sk->sk_write_space = queue->write_space; |
| 1994 | write_unlock_bh(&sock->sk->sk_callback_lock); |
| 1995 | } |
| 1996 | |
| 1997 | static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue) |
| 1998 | { |
| 1999 | kernel_sock_shutdown(queue->sock, SHUT_RDWR); |
| 2000 | nvme_tcp_restore_sock_ops(queue); |
| 2001 | cancel_work_sync(&queue->io_work); |
| 2002 | } |
| 2003 | |
| 2004 | static void nvme_tcp_stop_queue_nowait(struct nvme_ctrl *nctrl, int qid) |
| 2005 | { |
| 2006 | struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); |
| 2007 | struct nvme_tcp_queue *queue = &ctrl->queues[qid]; |
| 2008 | |
| 2009 | if (!test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) |
| 2010 | return; |
| 2011 | |
| 2012 | if (test_and_clear_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags)) |
| 2013 | atomic_dec(&nvme_tcp_cpu_queues[queue->io_cpu]); |
| 2014 | |
| 2015 | mutex_lock(&queue->queue_lock); |
| 2016 | if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags)) |
| 2017 | __nvme_tcp_stop_queue(queue); |
| 2018 | /* Stopping the queue will disable TLS */ |
| 2019 | queue->tls_enabled = false; |
| 2020 | mutex_unlock(&queue->queue_lock); |
| 2021 | } |
| 2022 | |
| 2023 | static void nvme_tcp_wait_queue(struct nvme_ctrl *nctrl, int qid) |
| 2024 | { |
| 2025 | struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); |
| 2026 | struct nvme_tcp_queue *queue = &ctrl->queues[qid]; |
| 2027 | int timeout = 100; |
| 2028 | |
| 2029 | while (timeout > 0) { |
| 2030 | if (!test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags) || |
| 2031 | !sk_wmem_alloc_get(queue->sock->sk)) |
| 2032 | return; |
| 2033 | msleep(2); |
| 2034 | timeout -= 2; |
| 2035 | } |
| 2036 | dev_warn(nctrl->device, |
| 2037 | "qid %d: timeout draining sock wmem allocation expired\n", |
| 2038 | qid); |
| 2039 | } |
| 2040 | |
| 2041 | static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid) |
| 2042 | { |
| 2043 | nvme_tcp_stop_queue_nowait(nctrl, qid); |
| 2044 | nvme_tcp_wait_queue(nctrl, qid); |
| 2045 | } |
| 2046 | |
| 2047 | |
| 2048 | static void nvme_tcp_setup_sock_ops(struct nvme_tcp_queue *queue) |
| 2049 | { |
| 2050 | write_lock_bh(&queue->sock->sk->sk_callback_lock); |
| 2051 | queue->sock->sk->sk_user_data = queue; |
| 2052 | queue->state_change = queue->sock->sk->sk_state_change; |
| 2053 | queue->data_ready = queue->sock->sk->sk_data_ready; |
| 2054 | queue->write_space = queue->sock->sk->sk_write_space; |
| 2055 | queue->sock->sk->sk_data_ready = nvme_tcp_data_ready; |
| 2056 | queue->sock->sk->sk_state_change = nvme_tcp_state_change; |
| 2057 | queue->sock->sk->sk_write_space = nvme_tcp_write_space; |
| 2058 | #ifdef CONFIG_NET_RX_BUSY_POLL |
| 2059 | queue->sock->sk->sk_ll_usec = 1; |
| 2060 | #endif |
| 2061 | write_unlock_bh(&queue->sock->sk->sk_callback_lock); |
| 2062 | } |
| 2063 | |
| 2064 | static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx) |
| 2065 | { |
| 2066 | struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); |
| 2067 | struct nvme_tcp_queue *queue = &ctrl->queues[idx]; |
| 2068 | int ret; |
| 2069 | |
| 2070 | queue->rd_enabled = true; |
| 2071 | nvme_tcp_init_recv_ctx(queue); |
| 2072 | nvme_tcp_setup_sock_ops(queue); |
| 2073 | |
| 2074 | if (idx) { |
| 2075 | nvme_tcp_set_queue_io_cpu(queue); |
| 2076 | ret = nvmf_connect_io_queue(nctrl, idx); |
| 2077 | } else |
| 2078 | ret = nvmf_connect_admin_queue(nctrl); |
| 2079 | |
| 2080 | if (!ret) { |
| 2081 | set_bit(NVME_TCP_Q_LIVE, &queue->flags); |
| 2082 | } else { |
| 2083 | if (test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags)) |
| 2084 | __nvme_tcp_stop_queue(queue); |
| 2085 | dev_err(nctrl->device, |
| 2086 | "failed to connect queue: %d ret=%d\n", idx, ret); |
| 2087 | } |
| 2088 | return ret; |
| 2089 | } |
| 2090 | |
| 2091 | static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl) |
| 2092 | { |
| 2093 | if (to_tcp_ctrl(ctrl)->async_req.pdu) { |
| 2094 | cancel_work_sync(&ctrl->async_event_work); |
| 2095 | nvme_tcp_free_async_req(to_tcp_ctrl(ctrl)); |
| 2096 | to_tcp_ctrl(ctrl)->async_req.pdu = NULL; |
| 2097 | } |
| 2098 | |
| 2099 | nvme_tcp_free_queue(ctrl, 0); |
| 2100 | } |
| 2101 | |
| 2102 | static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl) |
| 2103 | { |
| 2104 | int i; |
| 2105 | |
| 2106 | for (i = 1; i < ctrl->queue_count; i++) |
| 2107 | nvme_tcp_free_queue(ctrl, i); |
| 2108 | } |
| 2109 | |
| 2110 | static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl) |
| 2111 | { |
| 2112 | int i; |
| 2113 | |
| 2114 | for (i = 1; i < ctrl->queue_count; i++) |
| 2115 | nvme_tcp_stop_queue_nowait(ctrl, i); |
| 2116 | for (i = 1; i < ctrl->queue_count; i++) |
| 2117 | nvme_tcp_wait_queue(ctrl, i); |
| 2118 | } |
| 2119 | |
| 2120 | static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl, |
| 2121 | int first, int last) |
| 2122 | { |
| 2123 | int i, ret; |
| 2124 | |
| 2125 | for (i = first; i < last; i++) { |
| 2126 | ret = nvme_tcp_start_queue(ctrl, i); |
| 2127 | if (ret) |
| 2128 | goto out_stop_queues; |
| 2129 | } |
| 2130 | |
| 2131 | return 0; |
| 2132 | |
| 2133 | out_stop_queues: |
| 2134 | for (i--; i >= first; i--) |
| 2135 | nvme_tcp_stop_queue(ctrl, i); |
| 2136 | return ret; |
| 2137 | } |
| 2138 | |
| 2139 | static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) |
| 2140 | { |
| 2141 | int ret; |
| 2142 | key_serial_t pskid = 0; |
| 2143 | |
| 2144 | if (nvme_tcp_tls_configured(ctrl)) { |
| 2145 | if (ctrl->opts->tls_key) |
| 2146 | pskid = key_serial(ctrl->opts->tls_key); |
| 2147 | else if (ctrl->opts->tls) { |
| 2148 | pskid = nvme_tls_psk_default(ctrl->opts->keyring, |
| 2149 | ctrl->opts->host->nqn, |
| 2150 | ctrl->opts->subsysnqn); |
| 2151 | if (!pskid) { |
| 2152 | dev_err(ctrl->device, "no valid PSK found\n"); |
| 2153 | return -ENOKEY; |
| 2154 | } |
| 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | ret = nvme_tcp_alloc_queue(ctrl, 0, pskid); |
| 2159 | if (ret) |
| 2160 | return ret; |
| 2161 | |
| 2162 | ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl)); |
| 2163 | if (ret) |
| 2164 | goto out_free_queue; |
| 2165 | |
| 2166 | return 0; |
| 2167 | |
| 2168 | out_free_queue: |
| 2169 | nvme_tcp_free_queue(ctrl, 0); |
| 2170 | return ret; |
| 2171 | } |
| 2172 | |
| 2173 | static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl) |
| 2174 | { |
| 2175 | int i, ret; |
| 2176 | |
| 2177 | if (nvme_tcp_tls_configured(ctrl)) { |
| 2178 | if (ctrl->opts->concat) { |
| 2179 | /* |
| 2180 | * The generated PSK is stored in the |
| 2181 | * fabric options |
| 2182 | */ |
| 2183 | if (!ctrl->opts->tls_key) { |
| 2184 | dev_err(ctrl->device, "no PSK generated\n"); |
| 2185 | return -ENOKEY; |
| 2186 | } |
| 2187 | if (ctrl->tls_pskid && |
| 2188 | ctrl->tls_pskid != key_serial(ctrl->opts->tls_key)) { |
| 2189 | dev_err(ctrl->device, "Stale PSK id %08x\n", ctrl->tls_pskid); |
| 2190 | ctrl->tls_pskid = 0; |
| 2191 | } |
| 2192 | } else if (!ctrl->tls_pskid) { |
| 2193 | dev_err(ctrl->device, "no PSK negotiated\n"); |
| 2194 | return -ENOKEY; |
| 2195 | } |
| 2196 | } |
| 2197 | |
| 2198 | for (i = 1; i < ctrl->queue_count; i++) { |
| 2199 | ret = nvme_tcp_alloc_queue(ctrl, i, |
| 2200 | ctrl->tls_pskid); |
| 2201 | if (ret) |
| 2202 | goto out_free_queues; |
| 2203 | } |
| 2204 | |
| 2205 | return 0; |
| 2206 | |
| 2207 | out_free_queues: |
| 2208 | for (i--; i >= 1; i--) |
| 2209 | nvme_tcp_free_queue(ctrl, i); |
| 2210 | |
| 2211 | return ret; |
| 2212 | } |
| 2213 | |
| 2214 | static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl) |
| 2215 | { |
| 2216 | unsigned int nr_io_queues; |
| 2217 | int ret; |
| 2218 | |
| 2219 | nr_io_queues = nvmf_nr_io_queues(ctrl->opts); |
| 2220 | ret = nvme_set_queue_count(ctrl, &nr_io_queues); |
| 2221 | if (ret) |
| 2222 | return ret; |
| 2223 | |
| 2224 | if (nr_io_queues == 0) { |
| 2225 | dev_err(ctrl->device, |
| 2226 | "unable to set any I/O queues\n"); |
| 2227 | return -ENOMEM; |
| 2228 | } |
| 2229 | |
| 2230 | ctrl->queue_count = nr_io_queues + 1; |
| 2231 | dev_info(ctrl->device, |
| 2232 | "creating %d I/O queues.\n", nr_io_queues); |
| 2233 | |
| 2234 | nvmf_set_io_queues(ctrl->opts, nr_io_queues, |
| 2235 | to_tcp_ctrl(ctrl)->io_queues); |
| 2236 | return __nvme_tcp_alloc_io_queues(ctrl); |
| 2237 | } |
| 2238 | |
| 2239 | static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new) |
| 2240 | { |
| 2241 | int ret, nr_queues; |
| 2242 | |
| 2243 | ret = nvme_tcp_alloc_io_queues(ctrl); |
| 2244 | if (ret) |
| 2245 | return ret; |
| 2246 | |
| 2247 | if (new) { |
| 2248 | ret = nvme_alloc_io_tag_set(ctrl, &to_tcp_ctrl(ctrl)->tag_set, |
| 2249 | &nvme_tcp_mq_ops, |
| 2250 | ctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2, |
| 2251 | sizeof(struct nvme_tcp_request)); |
| 2252 | if (ret) |
| 2253 | goto out_free_io_queues; |
| 2254 | } |
| 2255 | |
| 2256 | /* |
| 2257 | * Only start IO queues for which we have allocated the tagset |
| 2258 | * and limited it to the available queues. On reconnects, the |
| 2259 | * queue number might have changed. |
| 2260 | */ |
| 2261 | nr_queues = min(ctrl->tagset->nr_hw_queues + 1, ctrl->queue_count); |
| 2262 | ret = nvme_tcp_start_io_queues(ctrl, 1, nr_queues); |
| 2263 | if (ret) |
| 2264 | goto out_cleanup_connect_q; |
| 2265 | |
| 2266 | if (!new) { |
| 2267 | nvme_start_freeze(ctrl); |
| 2268 | nvme_unquiesce_io_queues(ctrl); |
| 2269 | if (!nvme_wait_freeze_timeout(ctrl)) { |
| 2270 | /* |
| 2271 | * If we timed out waiting for freeze we are likely to |
| 2272 | * be stuck. Fail the controller initialization just |
| 2273 | * to be safe. |
| 2274 | */ |
| 2275 | ret = -ENODEV; |
| 2276 | nvme_unfreeze(ctrl); |
| 2277 | goto out_wait_freeze_timed_out; |
| 2278 | } |
| 2279 | blk_mq_update_nr_hw_queues(ctrl->tagset, |
| 2280 | ctrl->queue_count - 1); |
| 2281 | nvme_unfreeze(ctrl); |
| 2282 | } |
| 2283 | |
| 2284 | /* |
| 2285 | * If the number of queues has increased (reconnect case) |
| 2286 | * start all new queues now. |
| 2287 | */ |
| 2288 | ret = nvme_tcp_start_io_queues(ctrl, nr_queues, |
| 2289 | ctrl->tagset->nr_hw_queues + 1); |
| 2290 | if (ret) |
| 2291 | goto out_wait_freeze_timed_out; |
| 2292 | |
| 2293 | return 0; |
| 2294 | |
| 2295 | out_wait_freeze_timed_out: |
| 2296 | nvme_quiesce_io_queues(ctrl); |
| 2297 | nvme_sync_io_queues(ctrl); |
| 2298 | nvme_tcp_stop_io_queues(ctrl); |
| 2299 | out_cleanup_connect_q: |
| 2300 | nvme_cancel_tagset(ctrl); |
| 2301 | if (new) |
| 2302 | nvme_remove_io_tag_set(ctrl); |
| 2303 | out_free_io_queues: |
| 2304 | nvme_tcp_free_io_queues(ctrl); |
| 2305 | return ret; |
| 2306 | } |
| 2307 | |
| 2308 | static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new) |
| 2309 | { |
| 2310 | int error; |
| 2311 | |
| 2312 | error = nvme_tcp_alloc_admin_queue(ctrl); |
| 2313 | if (error) |
| 2314 | return error; |
| 2315 | |
| 2316 | if (new) { |
| 2317 | error = nvme_alloc_admin_tag_set(ctrl, |
| 2318 | &to_tcp_ctrl(ctrl)->admin_tag_set, |
| 2319 | &nvme_tcp_admin_mq_ops, |
| 2320 | sizeof(struct nvme_tcp_request)); |
| 2321 | if (error) |
| 2322 | goto out_free_queue; |
| 2323 | } |
| 2324 | |
| 2325 | error = nvme_tcp_start_queue(ctrl, 0); |
| 2326 | if (error) |
| 2327 | goto out_cleanup_tagset; |
| 2328 | |
| 2329 | if (ctrl->opts->concat && !ctrl->tls_pskid) |
| 2330 | return 0; |
| 2331 | |
| 2332 | error = nvme_enable_ctrl(ctrl); |
| 2333 | if (error) |
| 2334 | goto out_stop_queue; |
| 2335 | |
| 2336 | nvme_unquiesce_admin_queue(ctrl); |
| 2337 | |
| 2338 | error = nvme_init_ctrl_finish(ctrl, false); |
| 2339 | if (error) |
| 2340 | goto out_quiesce_queue; |
| 2341 | |
| 2342 | return 0; |
| 2343 | |
| 2344 | out_quiesce_queue: |
| 2345 | nvme_quiesce_admin_queue(ctrl); |
| 2346 | blk_sync_queue(ctrl->admin_q); |
| 2347 | out_stop_queue: |
| 2348 | nvme_tcp_stop_queue(ctrl, 0); |
| 2349 | nvme_cancel_admin_tagset(ctrl); |
| 2350 | out_cleanup_tagset: |
| 2351 | if (new) |
| 2352 | nvme_remove_admin_tag_set(ctrl); |
| 2353 | out_free_queue: |
| 2354 | nvme_tcp_free_admin_queue(ctrl); |
| 2355 | return error; |
| 2356 | } |
| 2357 | |
| 2358 | static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl, |
| 2359 | bool remove) |
| 2360 | { |
| 2361 | nvme_quiesce_admin_queue(ctrl); |
| 2362 | blk_sync_queue(ctrl->admin_q); |
| 2363 | nvme_tcp_stop_queue(ctrl, 0); |
| 2364 | nvme_cancel_admin_tagset(ctrl); |
| 2365 | if (remove) { |
| 2366 | nvme_unquiesce_admin_queue(ctrl); |
| 2367 | nvme_remove_admin_tag_set(ctrl); |
| 2368 | } |
| 2369 | nvme_tcp_free_admin_queue(ctrl); |
| 2370 | if (ctrl->tls_pskid) { |
| 2371 | dev_dbg(ctrl->device, "Wipe negotiated TLS_PSK %08x\n", |
| 2372 | ctrl->tls_pskid); |
| 2373 | ctrl->tls_pskid = 0; |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl, |
| 2378 | bool remove) |
| 2379 | { |
| 2380 | if (ctrl->queue_count <= 1) |
| 2381 | return; |
| 2382 | nvme_quiesce_io_queues(ctrl); |
| 2383 | nvme_sync_io_queues(ctrl); |
| 2384 | nvme_tcp_stop_io_queues(ctrl); |
| 2385 | nvme_cancel_tagset(ctrl); |
| 2386 | if (remove) { |
| 2387 | nvme_unquiesce_io_queues(ctrl); |
| 2388 | nvme_remove_io_tag_set(ctrl); |
| 2389 | } |
| 2390 | nvme_tcp_free_io_queues(ctrl); |
| 2391 | } |
| 2392 | |
| 2393 | static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl, |
| 2394 | int status) |
| 2395 | { |
| 2396 | enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); |
| 2397 | |
| 2398 | /* If we are resetting/deleting then do nothing */ |
| 2399 | if (state != NVME_CTRL_CONNECTING) { |
| 2400 | WARN_ON_ONCE(state == NVME_CTRL_NEW || state == NVME_CTRL_LIVE); |
| 2401 | return; |
| 2402 | } |
| 2403 | |
| 2404 | if (nvmf_should_reconnect(ctrl, status)) { |
| 2405 | dev_info(ctrl->device, "Reconnecting in %d seconds...\n", |
| 2406 | ctrl->opts->reconnect_delay); |
| 2407 | queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work, |
| 2408 | ctrl->opts->reconnect_delay * HZ); |
| 2409 | } else { |
| 2410 | dev_info(ctrl->device, "Removing controller (%d)...\n", |
| 2411 | status); |
| 2412 | nvme_delete_ctrl(ctrl); |
| 2413 | } |
| 2414 | } |
| 2415 | |
| 2416 | /* |
| 2417 | * The TLS key is set by secure concatenation after negotiation has been |
| 2418 | * completed on the admin queue. We need to revoke the key when: |
| 2419 | * - concatenation is enabled (otherwise it's a static key set by the user) |
| 2420 | * and |
| 2421 | * - the generated key is present in ctrl->tls_key (otherwise there's nothing |
| 2422 | * to revoke) |
| 2423 | * and |
| 2424 | * - a valid PSK key ID has been set in ctrl->tls_pskid (otherwise TLS |
| 2425 | * negotiation has not run). |
| 2426 | * |
| 2427 | * We cannot always revoke the key as nvme_tcp_alloc_admin_queue() is called |
| 2428 | * twice during secure concatenation, once on a 'normal' connection to run the |
| 2429 | * DH-HMAC-CHAP negotiation (which generates the key, so it _must not_ be set), |
| 2430 | * and once after the negotiation (which uses the key, so it _must_ be set). |
| 2431 | */ |
| 2432 | static bool nvme_tcp_key_revoke_needed(struct nvme_ctrl *ctrl) |
| 2433 | { |
| 2434 | return ctrl->opts->concat && ctrl->opts->tls_key && ctrl->tls_pskid; |
| 2435 | } |
| 2436 | |
| 2437 | static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new) |
| 2438 | { |
| 2439 | struct nvmf_ctrl_options *opts = ctrl->opts; |
| 2440 | int ret; |
| 2441 | |
| 2442 | ret = nvme_tcp_configure_admin_queue(ctrl, new); |
| 2443 | if (ret) |
| 2444 | return ret; |
| 2445 | |
| 2446 | if (ctrl->opts->concat && !ctrl->tls_pskid) { |
| 2447 | /* See comments for nvme_tcp_key_revoke_needed() */ |
| 2448 | dev_dbg(ctrl->device, "restart admin queue for secure concatenation\n"); |
| 2449 | nvme_stop_keep_alive(ctrl); |
| 2450 | nvme_tcp_teardown_admin_queue(ctrl, false); |
| 2451 | ret = nvme_tcp_configure_admin_queue(ctrl, false); |
| 2452 | if (ret) |
| 2453 | goto destroy_admin; |
| 2454 | } |
| 2455 | |
| 2456 | if (ctrl->icdoff) { |
| 2457 | ret = -EOPNOTSUPP; |
| 2458 | dev_err(ctrl->device, "icdoff is not supported!\n"); |
| 2459 | goto destroy_admin; |
| 2460 | } |
| 2461 | |
| 2462 | if (!nvme_ctrl_sgl_supported(ctrl)) { |
| 2463 | ret = -EOPNOTSUPP; |
| 2464 | dev_err(ctrl->device, "Mandatory sgls are not supported!\n"); |
| 2465 | goto destroy_admin; |
| 2466 | } |
| 2467 | |
| 2468 | if (opts->queue_size > ctrl->sqsize + 1) |
| 2469 | dev_warn(ctrl->device, |
| 2470 | "queue_size %zu > ctrl sqsize %u, clamping down\n", |
| 2471 | opts->queue_size, ctrl->sqsize + 1); |
| 2472 | |
| 2473 | if (ctrl->sqsize + 1 > ctrl->maxcmd) { |
| 2474 | dev_warn(ctrl->device, |
| 2475 | "sqsize %u > ctrl maxcmd %u, clamping down\n", |
| 2476 | ctrl->sqsize + 1, ctrl->maxcmd); |
| 2477 | ctrl->sqsize = ctrl->maxcmd - 1; |
| 2478 | } |
| 2479 | |
| 2480 | if (ctrl->queue_count > 1) { |
| 2481 | ret = nvme_tcp_configure_io_queues(ctrl, new); |
| 2482 | if (ret) |
| 2483 | goto destroy_admin; |
| 2484 | } |
| 2485 | |
| 2486 | if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) { |
| 2487 | /* |
| 2488 | * state change failure is ok if we started ctrl delete, |
| 2489 | * unless we're during creation of a new controller to |
| 2490 | * avoid races with teardown flow. |
| 2491 | */ |
| 2492 | enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); |
| 2493 | |
| 2494 | WARN_ON_ONCE(state != NVME_CTRL_DELETING && |
| 2495 | state != NVME_CTRL_DELETING_NOIO); |
| 2496 | WARN_ON_ONCE(new); |
| 2497 | ret = -EINVAL; |
| 2498 | goto destroy_io; |
| 2499 | } |
| 2500 | |
| 2501 | nvme_start_ctrl(ctrl); |
| 2502 | return 0; |
| 2503 | |
| 2504 | destroy_io: |
| 2505 | if (ctrl->queue_count > 1) { |
| 2506 | nvme_quiesce_io_queues(ctrl); |
| 2507 | nvme_sync_io_queues(ctrl); |
| 2508 | nvme_tcp_stop_io_queues(ctrl); |
| 2509 | nvme_cancel_tagset(ctrl); |
| 2510 | if (new) |
| 2511 | nvme_remove_io_tag_set(ctrl); |
| 2512 | nvme_tcp_free_io_queues(ctrl); |
| 2513 | } |
| 2514 | destroy_admin: |
| 2515 | nvme_stop_keep_alive(ctrl); |
| 2516 | nvme_tcp_teardown_admin_queue(ctrl, new); |
| 2517 | return ret; |
| 2518 | } |
| 2519 | |
| 2520 | static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work) |
| 2521 | { |
| 2522 | struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work), |
| 2523 | struct nvme_tcp_ctrl, connect_work); |
| 2524 | struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl; |
| 2525 | int ret; |
| 2526 | |
| 2527 | ++ctrl->nr_reconnects; |
| 2528 | |
| 2529 | ret = nvme_tcp_setup_ctrl(ctrl, false); |
| 2530 | if (ret) |
| 2531 | goto requeue; |
| 2532 | |
| 2533 | dev_info(ctrl->device, "Successfully reconnected (attempt %d/%d)\n", |
| 2534 | ctrl->nr_reconnects, ctrl->opts->max_reconnects); |
| 2535 | |
| 2536 | /* accumulate reconnect attempts before resetting it to zero */ |
| 2537 | atomic_long_add(ctrl->nr_reconnects, &ctrl->acc_reconnects); |
| 2538 | ctrl->nr_reconnects = 0; |
| 2539 | |
| 2540 | return; |
| 2541 | |
| 2542 | requeue: |
| 2543 | dev_info(ctrl->device, "Failed reconnect attempt %d/%d\n", |
| 2544 | ctrl->nr_reconnects, ctrl->opts->max_reconnects); |
| 2545 | nvme_tcp_reconnect_or_remove(ctrl, ret); |
| 2546 | } |
| 2547 | |
| 2548 | static void nvme_tcp_error_recovery_work(struct work_struct *work) |
| 2549 | { |
| 2550 | struct nvme_tcp_ctrl *tcp_ctrl = container_of(work, |
| 2551 | struct nvme_tcp_ctrl, err_work); |
| 2552 | struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl; |
| 2553 | |
| 2554 | if (nvme_tcp_key_revoke_needed(ctrl)) |
| 2555 | nvme_auth_revoke_tls_key(ctrl); |
| 2556 | nvme_stop_keep_alive(ctrl); |
| 2557 | flush_work(&ctrl->async_event_work); |
| 2558 | nvme_tcp_teardown_io_queues(ctrl, false); |
| 2559 | /* unquiesce to fail fast pending requests */ |
| 2560 | nvme_unquiesce_io_queues(ctrl); |
| 2561 | nvme_tcp_teardown_admin_queue(ctrl, false); |
| 2562 | nvme_unquiesce_admin_queue(ctrl); |
| 2563 | nvme_auth_stop(ctrl); |
| 2564 | |
| 2565 | if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) { |
| 2566 | /* state change failure is ok if we started ctrl delete */ |
| 2567 | enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); |
| 2568 | |
| 2569 | WARN_ON_ONCE(state != NVME_CTRL_DELETING && |
| 2570 | state != NVME_CTRL_DELETING_NOIO); |
| 2571 | return; |
| 2572 | } |
| 2573 | |
| 2574 | nvme_tcp_reconnect_or_remove(ctrl, 0); |
| 2575 | } |
| 2576 | |
| 2577 | static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown) |
| 2578 | { |
| 2579 | nvme_tcp_teardown_io_queues(ctrl, shutdown); |
| 2580 | nvme_quiesce_admin_queue(ctrl); |
| 2581 | nvme_disable_ctrl(ctrl, shutdown); |
| 2582 | nvme_tcp_teardown_admin_queue(ctrl, shutdown); |
| 2583 | } |
| 2584 | |
| 2585 | static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl) |
| 2586 | { |
| 2587 | nvme_tcp_teardown_ctrl(ctrl, true); |
| 2588 | } |
| 2589 | |
| 2590 | static void nvme_reset_ctrl_work(struct work_struct *work) |
| 2591 | { |
| 2592 | struct nvme_ctrl *ctrl = |
| 2593 | container_of(work, struct nvme_ctrl, reset_work); |
| 2594 | int ret; |
| 2595 | |
| 2596 | if (nvme_tcp_key_revoke_needed(ctrl)) |
| 2597 | nvme_auth_revoke_tls_key(ctrl); |
| 2598 | nvme_stop_ctrl(ctrl); |
| 2599 | nvme_tcp_teardown_ctrl(ctrl, false); |
| 2600 | |
| 2601 | if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) { |
| 2602 | /* state change failure is ok if we started ctrl delete */ |
| 2603 | enum nvme_ctrl_state state = nvme_ctrl_state(ctrl); |
| 2604 | |
| 2605 | WARN_ON_ONCE(state != NVME_CTRL_DELETING && |
| 2606 | state != NVME_CTRL_DELETING_NOIO); |
| 2607 | return; |
| 2608 | } |
| 2609 | |
| 2610 | ret = nvme_tcp_setup_ctrl(ctrl, false); |
| 2611 | if (ret) |
| 2612 | goto out_fail; |
| 2613 | |
| 2614 | return; |
| 2615 | |
| 2616 | out_fail: |
| 2617 | ++ctrl->nr_reconnects; |
| 2618 | nvme_tcp_reconnect_or_remove(ctrl, ret); |
| 2619 | } |
| 2620 | |
| 2621 | static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl) |
| 2622 | { |
| 2623 | flush_work(&to_tcp_ctrl(ctrl)->err_work); |
| 2624 | cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work); |
| 2625 | } |
| 2626 | |
| 2627 | static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl) |
| 2628 | { |
| 2629 | struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl); |
| 2630 | |
| 2631 | if (list_empty_careful(&ctrl->list)) |
| 2632 | goto free_ctrl; |
| 2633 | |
| 2634 | mutex_lock(&nvme_tcp_ctrl_mutex); |
| 2635 | list_del(&ctrl->list); |
| 2636 | mutex_unlock(&nvme_tcp_ctrl_mutex); |
| 2637 | |
| 2638 | nvmf_free_options(nctrl->opts); |
| 2639 | free_ctrl: |
| 2640 | kfree(ctrl->queues); |
| 2641 | kfree(ctrl); |
| 2642 | } |
| 2643 | |
| 2644 | static void nvme_tcp_set_sg_null(struct nvme_command *c) |
| 2645 | { |
| 2646 | struct nvme_sgl_desc *sg = &c->common.dptr.sgl; |
| 2647 | |
| 2648 | sg->addr = 0; |
| 2649 | sg->length = 0; |
| 2650 | sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) | |
| 2651 | NVME_SGL_FMT_TRANSPORT_A; |
| 2652 | } |
| 2653 | |
| 2654 | static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue, |
| 2655 | struct nvme_command *c, u32 data_len) |
| 2656 | { |
| 2657 | struct nvme_sgl_desc *sg = &c->common.dptr.sgl; |
| 2658 | |
| 2659 | sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff); |
| 2660 | sg->length = cpu_to_le32(data_len); |
| 2661 | sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET; |
| 2662 | } |
| 2663 | |
| 2664 | static void nvme_tcp_set_sg_host_data(struct nvme_command *c, |
| 2665 | u32 data_len) |
| 2666 | { |
| 2667 | struct nvme_sgl_desc *sg = &c->common.dptr.sgl; |
| 2668 | |
| 2669 | sg->addr = 0; |
| 2670 | sg->length = cpu_to_le32(data_len); |
| 2671 | sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) | |
| 2672 | NVME_SGL_FMT_TRANSPORT_A; |
| 2673 | } |
| 2674 | |
| 2675 | static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg) |
| 2676 | { |
| 2677 | struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg); |
| 2678 | struct nvme_tcp_queue *queue = &ctrl->queues[0]; |
| 2679 | struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu; |
| 2680 | struct nvme_command *cmd = &pdu->cmd; |
| 2681 | u8 hdgst = nvme_tcp_hdgst_len(queue); |
| 2682 | |
| 2683 | memset(pdu, 0, sizeof(*pdu)); |
| 2684 | pdu->hdr.type = nvme_tcp_cmd; |
| 2685 | if (queue->hdr_digest) |
| 2686 | pdu->hdr.flags |= NVME_TCP_F_HDGST; |
| 2687 | pdu->hdr.hlen = sizeof(*pdu); |
| 2688 | pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst); |
| 2689 | |
| 2690 | cmd->common.opcode = nvme_admin_async_event; |
| 2691 | cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH; |
| 2692 | cmd->common.flags |= NVME_CMD_SGL_METABUF; |
| 2693 | nvme_tcp_set_sg_null(cmd); |
| 2694 | |
| 2695 | ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU; |
| 2696 | ctrl->async_req.offset = 0; |
| 2697 | ctrl->async_req.curr_bio = NULL; |
| 2698 | ctrl->async_req.data_len = 0; |
| 2699 | init_llist_node(&ctrl->async_req.lentry); |
| 2700 | INIT_LIST_HEAD(&ctrl->async_req.entry); |
| 2701 | |
| 2702 | nvme_tcp_queue_request(&ctrl->async_req, true); |
| 2703 | } |
| 2704 | |
| 2705 | static void nvme_tcp_complete_timed_out(struct request *rq) |
| 2706 | { |
| 2707 | struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); |
| 2708 | struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl; |
| 2709 | |
| 2710 | nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue)); |
| 2711 | nvmf_complete_timed_out_request(rq); |
| 2712 | } |
| 2713 | |
| 2714 | static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq) |
| 2715 | { |
| 2716 | struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); |
| 2717 | struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl; |
| 2718 | struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); |
| 2719 | struct nvme_command *cmd = &pdu->cmd; |
| 2720 | int qid = nvme_tcp_queue_id(req->queue); |
| 2721 | |
| 2722 | dev_warn(ctrl->device, |
| 2723 | "I/O tag %d (%04x) type %d opcode %#x (%s) QID %d timeout\n", |
| 2724 | rq->tag, nvme_cid(rq), pdu->hdr.type, cmd->common.opcode, |
| 2725 | nvme_fabrics_opcode_str(qid, cmd), qid); |
| 2726 | |
| 2727 | if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) { |
| 2728 | /* |
| 2729 | * If we are resetting, connecting or deleting we should |
| 2730 | * complete immediately because we may block controller |
| 2731 | * teardown or setup sequence |
| 2732 | * - ctrl disable/shutdown fabrics requests |
| 2733 | * - connect requests |
| 2734 | * - initialization admin requests |
| 2735 | * - I/O requests that entered after unquiescing and |
| 2736 | * the controller stopped responding |
| 2737 | * |
| 2738 | * All other requests should be cancelled by the error |
| 2739 | * recovery work, so it's fine that we fail it here. |
| 2740 | */ |
| 2741 | nvme_tcp_complete_timed_out(rq); |
| 2742 | return BLK_EH_DONE; |
| 2743 | } |
| 2744 | |
| 2745 | /* |
| 2746 | * LIVE state should trigger the normal error recovery which will |
| 2747 | * handle completing this request. |
| 2748 | */ |
| 2749 | nvme_tcp_error_recovery(ctrl); |
| 2750 | return BLK_EH_RESET_TIMER; |
| 2751 | } |
| 2752 | |
| 2753 | static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue, |
| 2754 | struct request *rq) |
| 2755 | { |
| 2756 | struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); |
| 2757 | struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); |
| 2758 | struct nvme_command *c = &pdu->cmd; |
| 2759 | |
| 2760 | c->common.flags |= NVME_CMD_SGL_METABUF; |
| 2761 | |
| 2762 | if (!blk_rq_nr_phys_segments(rq)) |
| 2763 | nvme_tcp_set_sg_null(c); |
| 2764 | else if (rq_data_dir(rq) == WRITE && |
| 2765 | req->data_len <= nvme_tcp_inline_data_size(req)) |
| 2766 | nvme_tcp_set_sg_inline(queue, c, req->data_len); |
| 2767 | else |
| 2768 | nvme_tcp_set_sg_host_data(c, req->data_len); |
| 2769 | |
| 2770 | return 0; |
| 2771 | } |
| 2772 | |
| 2773 | static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns, |
| 2774 | struct request *rq) |
| 2775 | { |
| 2776 | struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); |
| 2777 | struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req); |
| 2778 | struct nvme_tcp_queue *queue = req->queue; |
| 2779 | u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0; |
| 2780 | blk_status_t ret; |
| 2781 | |
| 2782 | ret = nvme_setup_cmd(ns, rq); |
| 2783 | if (ret) |
| 2784 | return ret; |
| 2785 | |
| 2786 | req->state = NVME_TCP_SEND_CMD_PDU; |
| 2787 | req->status = cpu_to_le16(NVME_SC_SUCCESS); |
| 2788 | req->offset = 0; |
| 2789 | req->data_sent = 0; |
| 2790 | req->data_recvd = 0; |
| 2791 | req->pdu_len = 0; |
| 2792 | req->pdu_sent = 0; |
| 2793 | req->h2cdata_left = 0; |
| 2794 | req->data_len = blk_rq_nr_phys_segments(rq) ? |
| 2795 | blk_rq_payload_bytes(rq) : 0; |
| 2796 | req->curr_bio = rq->bio; |
| 2797 | if (req->curr_bio && req->data_len) |
| 2798 | nvme_tcp_init_iter(req, rq_data_dir(rq)); |
| 2799 | |
| 2800 | if (rq_data_dir(rq) == WRITE && |
| 2801 | req->data_len <= nvme_tcp_inline_data_size(req)) |
| 2802 | req->pdu_len = req->data_len; |
| 2803 | |
| 2804 | pdu->hdr.type = nvme_tcp_cmd; |
| 2805 | pdu->hdr.flags = 0; |
| 2806 | if (queue->hdr_digest) |
| 2807 | pdu->hdr.flags |= NVME_TCP_F_HDGST; |
| 2808 | if (queue->data_digest && req->pdu_len) { |
| 2809 | pdu->hdr.flags |= NVME_TCP_F_DDGST; |
| 2810 | ddgst = nvme_tcp_ddgst_len(queue); |
| 2811 | } |
| 2812 | pdu->hdr.hlen = sizeof(*pdu); |
| 2813 | pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0; |
| 2814 | pdu->hdr.plen = |
| 2815 | cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst); |
| 2816 | |
| 2817 | ret = nvme_tcp_map_data(queue, rq); |
| 2818 | if (unlikely(ret)) { |
| 2819 | nvme_cleanup_cmd(rq); |
| 2820 | dev_err(queue->ctrl->ctrl.device, |
| 2821 | "Failed to map data (%d)\n", ret); |
| 2822 | return ret; |
| 2823 | } |
| 2824 | |
| 2825 | return 0; |
| 2826 | } |
| 2827 | |
| 2828 | static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx) |
| 2829 | { |
| 2830 | struct nvme_tcp_queue *queue = hctx->driver_data; |
| 2831 | |
| 2832 | if (!llist_empty(&queue->req_list)) |
| 2833 | queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work); |
| 2834 | } |
| 2835 | |
| 2836 | static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 2837 | const struct blk_mq_queue_data *bd) |
| 2838 | { |
| 2839 | struct nvme_ns *ns = hctx->queue->queuedata; |
| 2840 | struct nvme_tcp_queue *queue = hctx->driver_data; |
| 2841 | struct request *rq = bd->rq; |
| 2842 | struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq); |
| 2843 | bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags); |
| 2844 | blk_status_t ret; |
| 2845 | |
| 2846 | if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready)) |
| 2847 | return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq); |
| 2848 | |
| 2849 | ret = nvme_tcp_setup_cmd_pdu(ns, rq); |
| 2850 | if (unlikely(ret)) |
| 2851 | return ret; |
| 2852 | |
| 2853 | nvme_start_request(rq); |
| 2854 | |
| 2855 | nvme_tcp_queue_request(req, bd->last); |
| 2856 | |
| 2857 | return BLK_STS_OK; |
| 2858 | } |
| 2859 | |
| 2860 | static void nvme_tcp_map_queues(struct blk_mq_tag_set *set) |
| 2861 | { |
| 2862 | struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data); |
| 2863 | |
| 2864 | nvmf_map_queues(set, &ctrl->ctrl, ctrl->io_queues); |
| 2865 | } |
| 2866 | |
| 2867 | static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob) |
| 2868 | { |
| 2869 | struct nvme_tcp_queue *queue = hctx->driver_data; |
| 2870 | struct sock *sk = queue->sock->sk; |
| 2871 | int ret; |
| 2872 | |
| 2873 | if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags)) |
| 2874 | return 0; |
| 2875 | |
| 2876 | set_bit(NVME_TCP_Q_POLLING, &queue->flags); |
| 2877 | if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue)) |
| 2878 | sk_busy_loop(sk, true); |
| 2879 | ret = nvme_tcp_try_recv(queue); |
| 2880 | clear_bit(NVME_TCP_Q_POLLING, &queue->flags); |
| 2881 | return ret < 0 ? ret : queue->nr_cqe; |
| 2882 | } |
| 2883 | |
| 2884 | static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size) |
| 2885 | { |
| 2886 | struct nvme_tcp_queue *queue = &to_tcp_ctrl(ctrl)->queues[0]; |
| 2887 | struct sockaddr_storage src_addr; |
| 2888 | int ret, len; |
| 2889 | |
| 2890 | len = nvmf_get_address(ctrl, buf, size); |
| 2891 | |
| 2892 | if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags)) |
| 2893 | return len; |
| 2894 | |
| 2895 | mutex_lock(&queue->queue_lock); |
| 2896 | |
| 2897 | ret = kernel_getsockname(queue->sock, (struct sockaddr *)&src_addr); |
| 2898 | if (ret > 0) { |
| 2899 | if (len > 0) |
| 2900 | len--; /* strip trailing newline */ |
| 2901 | len += scnprintf(buf + len, size - len, "%ssrc_addr=%pISc\n", |
| 2902 | (len) ? "," : "", &src_addr); |
| 2903 | } |
| 2904 | |
| 2905 | mutex_unlock(&queue->queue_lock); |
| 2906 | |
| 2907 | return len; |
| 2908 | } |
| 2909 | |
| 2910 | static const struct blk_mq_ops nvme_tcp_mq_ops = { |
| 2911 | .queue_rq = nvme_tcp_queue_rq, |
| 2912 | .commit_rqs = nvme_tcp_commit_rqs, |
| 2913 | .complete = nvme_complete_rq, |
| 2914 | .init_request = nvme_tcp_init_request, |
| 2915 | .exit_request = nvme_tcp_exit_request, |
| 2916 | .init_hctx = nvme_tcp_init_hctx, |
| 2917 | .timeout = nvme_tcp_timeout, |
| 2918 | .map_queues = nvme_tcp_map_queues, |
| 2919 | .poll = nvme_tcp_poll, |
| 2920 | }; |
| 2921 | |
| 2922 | static const struct blk_mq_ops nvme_tcp_admin_mq_ops = { |
| 2923 | .queue_rq = nvme_tcp_queue_rq, |
| 2924 | .complete = nvme_complete_rq, |
| 2925 | .init_request = nvme_tcp_init_request, |
| 2926 | .exit_request = nvme_tcp_exit_request, |
| 2927 | .init_hctx = nvme_tcp_init_admin_hctx, |
| 2928 | .timeout = nvme_tcp_timeout, |
| 2929 | }; |
| 2930 | |
| 2931 | static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = { |
| 2932 | .name = "tcp", |
| 2933 | .module = THIS_MODULE, |
| 2934 | .flags = NVME_F_FABRICS | NVME_F_BLOCKING, |
| 2935 | .reg_read32 = nvmf_reg_read32, |
| 2936 | .reg_read64 = nvmf_reg_read64, |
| 2937 | .reg_write32 = nvmf_reg_write32, |
| 2938 | .subsystem_reset = nvmf_subsystem_reset, |
| 2939 | .free_ctrl = nvme_tcp_free_ctrl, |
| 2940 | .submit_async_event = nvme_tcp_submit_async_event, |
| 2941 | .delete_ctrl = nvme_tcp_delete_ctrl, |
| 2942 | .get_address = nvme_tcp_get_address, |
| 2943 | .stop_ctrl = nvme_tcp_stop_ctrl, |
| 2944 | .get_virt_boundary = nvmf_get_virt_boundary, |
| 2945 | }; |
| 2946 | |
| 2947 | static bool |
| 2948 | nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts) |
| 2949 | { |
| 2950 | struct nvme_tcp_ctrl *ctrl; |
| 2951 | bool found = false; |
| 2952 | |
| 2953 | mutex_lock(&nvme_tcp_ctrl_mutex); |
| 2954 | list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) { |
| 2955 | found = nvmf_ip_options_match(&ctrl->ctrl, opts); |
| 2956 | if (found) |
| 2957 | break; |
| 2958 | } |
| 2959 | mutex_unlock(&nvme_tcp_ctrl_mutex); |
| 2960 | |
| 2961 | return found; |
| 2962 | } |
| 2963 | |
| 2964 | static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev, |
| 2965 | struct nvmf_ctrl_options *opts) |
| 2966 | { |
| 2967 | struct nvme_tcp_ctrl *ctrl; |
| 2968 | int ret; |
| 2969 | |
| 2970 | ctrl = kzalloc_obj(*ctrl); |
| 2971 | if (!ctrl) |
| 2972 | return ERR_PTR(-ENOMEM); |
| 2973 | |
| 2974 | /* |
| 2975 | * Safe to init list while allocating ctrl object. |
| 2976 | */ |
| 2977 | context_unsafe(INIT_LIST_HEAD(&ctrl->list)); |
| 2978 | ctrl->ctrl.opts = opts; |
| 2979 | ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues + |
| 2980 | opts->nr_poll_queues + 1; |
| 2981 | ctrl->ctrl.sqsize = opts->queue_size - 1; |
| 2982 | ctrl->ctrl.kato = opts->kato; |
| 2983 | |
| 2984 | INIT_DELAYED_WORK(&ctrl->connect_work, |
| 2985 | nvme_tcp_reconnect_ctrl_work); |
| 2986 | INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work); |
| 2987 | INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work); |
| 2988 | |
| 2989 | if (!(opts->mask & NVMF_OPT_TRSVCID)) { |
| 2990 | opts->trsvcid = |
| 2991 | kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL); |
| 2992 | if (!opts->trsvcid) { |
| 2993 | ret = -ENOMEM; |
| 2994 | goto out_free_ctrl; |
| 2995 | } |
| 2996 | opts->mask |= NVMF_OPT_TRSVCID; |
| 2997 | } |
| 2998 | |
| 2999 | ret = inet_pton_with_scope(&init_net, AF_UNSPEC, |
| 3000 | opts->traddr, opts->trsvcid, &ctrl->addr); |
| 3001 | if (ret) { |
| 3002 | pr_err("malformed address passed: %s:%s\n", |
| 3003 | opts->traddr, opts->trsvcid); |
| 3004 | goto out_free_ctrl; |
| 3005 | } |
| 3006 | |
| 3007 | if (opts->mask & NVMF_OPT_HOST_TRADDR) { |
| 3008 | ret = inet_pton_with_scope(&init_net, AF_UNSPEC, |
| 3009 | opts->host_traddr, NULL, &ctrl->src_addr); |
| 3010 | if (ret) { |
| 3011 | pr_err("malformed src address passed: %s\n", |
| 3012 | opts->host_traddr); |
| 3013 | goto out_free_ctrl; |
| 3014 | } |
| 3015 | } |
| 3016 | |
| 3017 | if (opts->mask & NVMF_OPT_HOST_IFACE) { |
| 3018 | if (!__dev_get_by_name(current->nsproxy->net_ns, |
| 3019 | opts->host_iface)) { |
| 3020 | pr_err("invalid interface passed: %s\n", |
| 3021 | opts->host_iface); |
| 3022 | ret = -ENODEV; |
| 3023 | goto out_free_ctrl; |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) { |
| 3028 | ret = -EALREADY; |
| 3029 | goto out_free_ctrl; |
| 3030 | } |
| 3031 | |
| 3032 | ctrl->queues = kzalloc_objs(*ctrl->queues, ctrl->ctrl.queue_count); |
| 3033 | if (!ctrl->queues) { |
| 3034 | ret = -ENOMEM; |
| 3035 | goto out_free_ctrl; |
| 3036 | } |
| 3037 | |
| 3038 | ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0); |
| 3039 | if (ret) |
| 3040 | goto out_kfree_queues; |
| 3041 | |
| 3042 | return ctrl; |
| 3043 | out_kfree_queues: |
| 3044 | kfree(ctrl->queues); |
| 3045 | out_free_ctrl: |
| 3046 | kfree(ctrl); |
| 3047 | return ERR_PTR(ret); |
| 3048 | } |
| 3049 | |
| 3050 | static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev, |
| 3051 | struct nvmf_ctrl_options *opts) |
| 3052 | { |
| 3053 | struct nvme_tcp_ctrl *ctrl; |
| 3054 | int ret; |
| 3055 | |
| 3056 | ctrl = nvme_tcp_alloc_ctrl(dev, opts); |
| 3057 | if (IS_ERR(ctrl)) |
| 3058 | return ERR_CAST(ctrl); |
| 3059 | |
| 3060 | ret = nvme_add_ctrl(&ctrl->ctrl); |
| 3061 | if (ret) |
| 3062 | goto out_put_ctrl; |
| 3063 | |
| 3064 | if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) { |
| 3065 | WARN_ON_ONCE(1); |
| 3066 | ret = -EINTR; |
| 3067 | goto out_uninit_ctrl; |
| 3068 | } |
| 3069 | |
| 3070 | ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true); |
| 3071 | if (ret) |
| 3072 | goto out_uninit_ctrl; |
| 3073 | |
| 3074 | dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp, hostnqn: %s\n", |
| 3075 | nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr, opts->host->nqn); |
| 3076 | |
| 3077 | mutex_lock(&nvme_tcp_ctrl_mutex); |
| 3078 | list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list); |
| 3079 | mutex_unlock(&nvme_tcp_ctrl_mutex); |
| 3080 | |
| 3081 | return &ctrl->ctrl; |
| 3082 | |
| 3083 | out_uninit_ctrl: |
| 3084 | nvme_uninit_ctrl(&ctrl->ctrl); |
| 3085 | out_put_ctrl: |
| 3086 | nvme_put_ctrl(&ctrl->ctrl); |
| 3087 | if (ret > 0) |
| 3088 | ret = -EIO; |
| 3089 | return ERR_PTR(ret); |
| 3090 | } |
| 3091 | |
| 3092 | static struct nvmf_transport_ops nvme_tcp_transport = { |
| 3093 | .name = "tcp", |
| 3094 | .module = THIS_MODULE, |
| 3095 | .required_opts = NVMF_OPT_TRADDR, |
| 3096 | .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY | |
| 3097 | NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO | |
| 3098 | NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST | |
| 3099 | NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES | |
| 3100 | NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS | |
| 3101 | NVMF_OPT_KEYRING | NVMF_OPT_TLS_KEY | NVMF_OPT_CONCAT, |
| 3102 | .create_ctrl = nvme_tcp_create_ctrl, |
| 3103 | }; |
| 3104 | |
| 3105 | static int __init nvme_tcp_init_module(void) |
| 3106 | { |
| 3107 | unsigned int wq_flags = WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_SYSFS; |
| 3108 | int cpu; |
| 3109 | |
| 3110 | BUILD_BUG_ON(sizeof(struct nvme_tcp_hdr) != 8); |
| 3111 | BUILD_BUG_ON(sizeof(struct nvme_tcp_cmd_pdu) != 72); |
| 3112 | BUILD_BUG_ON(sizeof(struct nvme_tcp_data_pdu) != 24); |
| 3113 | BUILD_BUG_ON(sizeof(struct nvme_tcp_rsp_pdu) != 24); |
| 3114 | BUILD_BUG_ON(sizeof(struct nvme_tcp_r2t_pdu) != 24); |
| 3115 | BUILD_BUG_ON(sizeof(struct nvme_tcp_icreq_pdu) != 128); |
| 3116 | BUILD_BUG_ON(sizeof(struct nvme_tcp_icresp_pdu) != 128); |
| 3117 | BUILD_BUG_ON(sizeof(struct nvme_tcp_term_pdu) != 24); |
| 3118 | |
| 3119 | if (wq_unbound) |
| 3120 | wq_flags |= WQ_UNBOUND; |
| 3121 | else |
| 3122 | wq_flags |= WQ_PERCPU; |
| 3123 | |
| 3124 | nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq", wq_flags, 0); |
| 3125 | if (!nvme_tcp_wq) |
| 3126 | return -ENOMEM; |
| 3127 | |
| 3128 | for_each_possible_cpu(cpu) |
| 3129 | atomic_set(&nvme_tcp_cpu_queues[cpu], 0); |
| 3130 | |
| 3131 | nvmf_register_transport(&nvme_tcp_transport); |
| 3132 | return 0; |
| 3133 | } |
| 3134 | |
| 3135 | static void __exit nvme_tcp_cleanup_module(void) |
| 3136 | { |
| 3137 | struct nvme_tcp_ctrl *ctrl; |
| 3138 | |
| 3139 | nvmf_unregister_transport(&nvme_tcp_transport); |
| 3140 | |
| 3141 | mutex_lock(&nvme_tcp_ctrl_mutex); |
| 3142 | list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) |
| 3143 | nvme_delete_ctrl(&ctrl->ctrl); |
| 3144 | mutex_unlock(&nvme_tcp_ctrl_mutex); |
| 3145 | flush_workqueue(nvme_delete_wq); |
| 3146 | |
| 3147 | destroy_workqueue(nvme_tcp_wq); |
| 3148 | } |
| 3149 | |
| 3150 | module_init(nvme_tcp_init_module); |
| 3151 | module_exit(nvme_tcp_cleanup_module); |
| 3152 | |
| 3153 | MODULE_DESCRIPTION("NVMe host TCP transport driver"); |
| 3154 | MODULE_LICENSE("GPL v2"); |
| 3155 | MODULE_ALIAS("nvme-tcp"); |