Christoph Hellwig | a4b74fc | 2019-02-18 11:35:19 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Avago Technologies. All rights reserved. |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 4 | */ |
| 5 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/parser.h> |
| 8 | #include <uapi/scsi/fc/fc_fs.h> |
| 9 | |
| 10 | #include "../host/nvme.h" |
| 11 | #include "../target/nvmet.h" |
| 12 | #include <linux/nvme-fc-driver.h> |
| 13 | #include <linux/nvme-fc.h> |
| 14 | |
| 15 | |
| 16 | enum { |
| 17 | NVMF_OPT_ERR = 0, |
| 18 | NVMF_OPT_WWNN = 1 << 0, |
| 19 | NVMF_OPT_WWPN = 1 << 1, |
| 20 | NVMF_OPT_ROLES = 1 << 2, |
| 21 | NVMF_OPT_FCADDR = 1 << 3, |
| 22 | NVMF_OPT_LPWWNN = 1 << 4, |
| 23 | NVMF_OPT_LPWWPN = 1 << 5, |
| 24 | }; |
| 25 | |
| 26 | struct fcloop_ctrl_options { |
| 27 | int mask; |
| 28 | u64 wwnn; |
| 29 | u64 wwpn; |
| 30 | u32 roles; |
| 31 | u32 fcaddr; |
| 32 | u64 lpwwnn; |
| 33 | u64 lpwwpn; |
| 34 | }; |
| 35 | |
| 36 | static const match_table_t opt_tokens = { |
| 37 | { NVMF_OPT_WWNN, "wwnn=%s" }, |
| 38 | { NVMF_OPT_WWPN, "wwpn=%s" }, |
| 39 | { NVMF_OPT_ROLES, "roles=%d" }, |
| 40 | { NVMF_OPT_FCADDR, "fcaddr=%x" }, |
| 41 | { NVMF_OPT_LPWWNN, "lpwwnn=%s" }, |
| 42 | { NVMF_OPT_LPWWPN, "lpwwpn=%s" }, |
| 43 | { NVMF_OPT_ERR, NULL } |
| 44 | }; |
| 45 | |
Dongli Zhang | ca8f4be | 2020-05-25 21:21:18 -0700 | [diff] [blame] | 46 | static int fcloop_verify_addr(substring_t *s) |
| 47 | { |
| 48 | size_t blen = s->to - s->from + 1; |
| 49 | |
| 50 | if (strnlen(s->from, blen) != NVME_FC_TRADDR_HEXNAMELEN + 2 || |
| 51 | strncmp(s->from, "0x", 2)) |
| 52 | return -EINVAL; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 57 | static int |
| 58 | fcloop_parse_options(struct fcloop_ctrl_options *opts, |
| 59 | const char *buf) |
| 60 | { |
| 61 | substring_t args[MAX_OPT_ARGS]; |
| 62 | char *options, *o, *p; |
| 63 | int token, ret = 0; |
| 64 | u64 token64; |
| 65 | |
| 66 | options = o = kstrdup(buf, GFP_KERNEL); |
| 67 | if (!options) |
| 68 | return -ENOMEM; |
| 69 | |
| 70 | while ((p = strsep(&o, ",\n")) != NULL) { |
| 71 | if (!*p) |
| 72 | continue; |
| 73 | |
| 74 | token = match_token(p, opt_tokens, args); |
| 75 | opts->mask |= token; |
| 76 | switch (token) { |
| 77 | case NVMF_OPT_WWNN: |
Dongli Zhang | ca8f4be | 2020-05-25 21:21:18 -0700 | [diff] [blame] | 78 | if (fcloop_verify_addr(args) || |
| 79 | match_u64(args, &token64)) { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 80 | ret = -EINVAL; |
| 81 | goto out_free_options; |
| 82 | } |
| 83 | opts->wwnn = token64; |
| 84 | break; |
| 85 | case NVMF_OPT_WWPN: |
Dongli Zhang | ca8f4be | 2020-05-25 21:21:18 -0700 | [diff] [blame] | 86 | if (fcloop_verify_addr(args) || |
| 87 | match_u64(args, &token64)) { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 88 | ret = -EINVAL; |
| 89 | goto out_free_options; |
| 90 | } |
| 91 | opts->wwpn = token64; |
| 92 | break; |
| 93 | case NVMF_OPT_ROLES: |
| 94 | if (match_int(args, &token)) { |
| 95 | ret = -EINVAL; |
| 96 | goto out_free_options; |
| 97 | } |
| 98 | opts->roles = token; |
| 99 | break; |
| 100 | case NVMF_OPT_FCADDR: |
| 101 | if (match_hex(args, &token)) { |
| 102 | ret = -EINVAL; |
| 103 | goto out_free_options; |
| 104 | } |
| 105 | opts->fcaddr = token; |
| 106 | break; |
| 107 | case NVMF_OPT_LPWWNN: |
Dongli Zhang | ca8f4be | 2020-05-25 21:21:18 -0700 | [diff] [blame] | 108 | if (fcloop_verify_addr(args) || |
| 109 | match_u64(args, &token64)) { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 110 | ret = -EINVAL; |
| 111 | goto out_free_options; |
| 112 | } |
| 113 | opts->lpwwnn = token64; |
| 114 | break; |
| 115 | case NVMF_OPT_LPWWPN: |
Dongli Zhang | ca8f4be | 2020-05-25 21:21:18 -0700 | [diff] [blame] | 116 | if (fcloop_verify_addr(args) || |
| 117 | match_u64(args, &token64)) { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 118 | ret = -EINVAL; |
| 119 | goto out_free_options; |
| 120 | } |
| 121 | opts->lpwwpn = token64; |
| 122 | break; |
| 123 | default: |
| 124 | pr_warn("unknown parameter or missing value '%s'\n", p); |
| 125 | ret = -EINVAL; |
| 126 | goto out_free_options; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | out_free_options: |
| 131 | kfree(options); |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | static int |
| 137 | fcloop_parse_nm_options(struct device *dev, u64 *nname, u64 *pname, |
| 138 | const char *buf) |
| 139 | { |
| 140 | substring_t args[MAX_OPT_ARGS]; |
| 141 | char *options, *o, *p; |
| 142 | int token, ret = 0; |
| 143 | u64 token64; |
| 144 | |
| 145 | *nname = -1; |
| 146 | *pname = -1; |
| 147 | |
| 148 | options = o = kstrdup(buf, GFP_KERNEL); |
| 149 | if (!options) |
| 150 | return -ENOMEM; |
| 151 | |
| 152 | while ((p = strsep(&o, ",\n")) != NULL) { |
| 153 | if (!*p) |
| 154 | continue; |
| 155 | |
| 156 | token = match_token(p, opt_tokens, args); |
| 157 | switch (token) { |
| 158 | case NVMF_OPT_WWNN: |
Dongli Zhang | ca8f4be | 2020-05-25 21:21:18 -0700 | [diff] [blame] | 159 | if (fcloop_verify_addr(args) || |
| 160 | match_u64(args, &token64)) { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 161 | ret = -EINVAL; |
| 162 | goto out_free_options; |
| 163 | } |
| 164 | *nname = token64; |
| 165 | break; |
| 166 | case NVMF_OPT_WWPN: |
Dongli Zhang | ca8f4be | 2020-05-25 21:21:18 -0700 | [diff] [blame] | 167 | if (fcloop_verify_addr(args) || |
| 168 | match_u64(args, &token64)) { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 169 | ret = -EINVAL; |
| 170 | goto out_free_options; |
| 171 | } |
| 172 | *pname = token64; |
| 173 | break; |
| 174 | default: |
| 175 | pr_warn("unknown parameter or missing value '%s'\n", p); |
| 176 | ret = -EINVAL; |
| 177 | goto out_free_options; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | out_free_options: |
| 182 | kfree(options); |
| 183 | |
| 184 | if (!ret) { |
| 185 | if (*nname == -1) |
| 186 | return -EINVAL; |
| 187 | if (*pname == -1) |
| 188 | return -EINVAL; |
| 189 | } |
| 190 | |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | #define LPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN) |
| 196 | |
| 197 | #define RPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN | \ |
| 198 | NVMF_OPT_LPWWNN | NVMF_OPT_LPWWPN) |
| 199 | |
| 200 | #define TGTPORT_OPTS (NVMF_OPT_WWNN | NVMF_OPT_WWPN) |
| 201 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 202 | |
| 203 | static DEFINE_SPINLOCK(fcloop_lock); |
| 204 | static LIST_HEAD(fcloop_lports); |
| 205 | static LIST_HEAD(fcloop_nports); |
| 206 | |
| 207 | struct fcloop_lport { |
| 208 | struct nvme_fc_local_port *localport; |
| 209 | struct list_head lport_list; |
| 210 | struct completion unreg_done; |
| 211 | }; |
| 212 | |
James Smart | 6fda202 | 2017-11-29 16:47:31 -0800 | [diff] [blame] | 213 | struct fcloop_lport_priv { |
| 214 | struct fcloop_lport *lport; |
| 215 | }; |
| 216 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 217 | struct fcloop_rport { |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 218 | struct nvme_fc_remote_port *remoteport; |
| 219 | struct nvmet_fc_target_port *targetport; |
| 220 | struct fcloop_nport *nport; |
| 221 | struct fcloop_lport *lport; |
| 222 | spinlock_t lock; |
| 223 | struct list_head ls_list; |
| 224 | struct work_struct ls_work; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | struct fcloop_tport { |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 228 | struct nvmet_fc_target_port *targetport; |
| 229 | struct nvme_fc_remote_port *remoteport; |
| 230 | struct fcloop_nport *nport; |
| 231 | struct fcloop_lport *lport; |
| 232 | spinlock_t lock; |
| 233 | struct list_head ls_list; |
| 234 | struct work_struct ls_work; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | struct fcloop_nport { |
| 238 | struct fcloop_rport *rport; |
| 239 | struct fcloop_tport *tport; |
| 240 | struct fcloop_lport *lport; |
| 241 | struct list_head nport_list; |
| 242 | struct kref ref; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 243 | u64 node_name; |
| 244 | u64 port_name; |
| 245 | u32 port_role; |
| 246 | u32 port_id; |
| 247 | }; |
| 248 | |
| 249 | struct fcloop_lsreq { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 250 | struct nvmefc_ls_req *lsreq; |
James Smart | 72e6329 | 2020-03-31 09:49:47 -0700 | [diff] [blame] | 251 | struct nvmefc_ls_rsp ls_rsp; |
James Smart | ea39765 | 2020-03-31 09:50:00 -0700 | [diff] [blame] | 252 | int lsdir; /* H2T or T2H */ |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 253 | int status; |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 254 | struct list_head ls_list; /* fcloop_rport->ls_list */ |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 255 | }; |
| 256 | |
James Smart | 4cf7c36 | 2019-05-14 14:58:04 -0700 | [diff] [blame] | 257 | struct fcloop_rscn { |
| 258 | struct fcloop_tport *tport; |
| 259 | struct work_struct work; |
| 260 | }; |
| 261 | |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 262 | enum { |
| 263 | INI_IO_START = 0, |
| 264 | INI_IO_ACTIVE = 1, |
| 265 | INI_IO_ABORTED = 2, |
| 266 | INI_IO_COMPLETED = 3, |
| 267 | }; |
| 268 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 269 | struct fcloop_fcpreq { |
| 270 | struct fcloop_tport *tport; |
| 271 | struct nvmefc_fcp_req *fcpreq; |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 272 | spinlock_t reqlock; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 273 | u16 status; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 274 | u32 inistate; |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 275 | bool active; |
| 276 | bool aborted; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 277 | struct kref ref; |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 278 | struct work_struct fcp_rcv_work; |
| 279 | struct work_struct abort_rcv_work; |
| 280 | struct work_struct tio_done_work; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 281 | struct nvmefc_tgt_fcp_req tgt_fcp_req; |
| 282 | }; |
| 283 | |
James Smart | ce79bfc | 2017-04-11 11:32:30 -0700 | [diff] [blame] | 284 | struct fcloop_ini_fcpreq { |
| 285 | struct nvmefc_fcp_req *fcpreq; |
| 286 | struct fcloop_fcpreq *tfcp_req; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 287 | spinlock_t inilock; |
James Smart | ce79bfc | 2017-04-11 11:32:30 -0700 | [diff] [blame] | 288 | }; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 289 | |
| 290 | static inline struct fcloop_lsreq * |
James Smart | 72e6329 | 2020-03-31 09:49:47 -0700 | [diff] [blame] | 291 | ls_rsp_to_lsreq(struct nvmefc_ls_rsp *lsrsp) |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 292 | { |
James Smart | 72e6329 | 2020-03-31 09:49:47 -0700 | [diff] [blame] | 293 | return container_of(lsrsp, struct fcloop_lsreq, ls_rsp); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static inline struct fcloop_fcpreq * |
| 297 | tgt_fcp_req_to_fcpreq(struct nvmefc_tgt_fcp_req *tgt_fcpreq) |
| 298 | { |
| 299 | return container_of(tgt_fcpreq, struct fcloop_fcpreq, tgt_fcp_req); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | static int |
| 304 | fcloop_create_queue(struct nvme_fc_local_port *localport, |
| 305 | unsigned int qidx, u16 qsize, |
| 306 | void **handle) |
| 307 | { |
| 308 | *handle = localport; |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static void |
| 313 | fcloop_delete_queue(struct nvme_fc_local_port *localport, |
| 314 | unsigned int idx, void *handle) |
| 315 | { |
| 316 | } |
| 317 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 318 | static void |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 319 | fcloop_rport_lsrqst_work(struct work_struct *work) |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 320 | { |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 321 | struct fcloop_rport *rport = |
| 322 | container_of(work, struct fcloop_rport, ls_work); |
| 323 | struct fcloop_lsreq *tls_req; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 324 | |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 325 | spin_lock(&rport->lock); |
| 326 | for (;;) { |
| 327 | tls_req = list_first_entry_or_null(&rport->ls_list, |
| 328 | struct fcloop_lsreq, ls_list); |
| 329 | if (!tls_req) |
| 330 | break; |
| 331 | |
| 332 | list_del(&tls_req->ls_list); |
| 333 | spin_unlock(&rport->lock); |
| 334 | |
| 335 | tls_req->lsreq->done(tls_req->lsreq, tls_req->status); |
| 336 | /* |
| 337 | * callee may free memory containing tls_req. |
| 338 | * do not reference lsreq after this. |
| 339 | */ |
| 340 | |
| 341 | spin_lock(&rport->lock); |
| 342 | } |
| 343 | spin_unlock(&rport->lock); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static int |
James Smart | ea39765 | 2020-03-31 09:50:00 -0700 | [diff] [blame] | 347 | fcloop_h2t_ls_req(struct nvme_fc_local_port *localport, |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 348 | struct nvme_fc_remote_port *remoteport, |
| 349 | struct nvmefc_ls_req *lsreq) |
| 350 | { |
| 351 | struct fcloop_lsreq *tls_req = lsreq->private; |
| 352 | struct fcloop_rport *rport = remoteport->private; |
| 353 | int ret = 0; |
| 354 | |
| 355 | tls_req->lsreq = lsreq; |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 356 | INIT_LIST_HEAD(&tls_req->ls_list); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 357 | |
| 358 | if (!rport->targetport) { |
| 359 | tls_req->status = -ECONNREFUSED; |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 360 | spin_lock(&rport->lock); |
Daniel Wagner | dcfad4a | 2024-01-31 09:51:02 +0100 | [diff] [blame] | 361 | list_add_tail(&tls_req->ls_list, &rport->ls_list); |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 362 | spin_unlock(&rport->lock); |
Sagi Grimberg | 8832cf9 | 2022-03-21 13:57:27 +0200 | [diff] [blame] | 363 | queue_work(nvmet_wq, &rport->ls_work); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | tls_req->status = 0; |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 368 | ret = nvmet_fc_rcv_ls_req(rport->targetport, rport, |
| 369 | &tls_req->ls_rsp, |
| 370 | lsreq->rqstaddr, lsreq->rqstlen); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 371 | |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | static int |
James Smart | ea39765 | 2020-03-31 09:50:00 -0700 | [diff] [blame] | 376 | fcloop_h2t_xmt_ls_rsp(struct nvmet_fc_target_port *targetport, |
James Smart | 72e6329 | 2020-03-31 09:49:47 -0700 | [diff] [blame] | 377 | struct nvmefc_ls_rsp *lsrsp) |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 378 | { |
James Smart | 72e6329 | 2020-03-31 09:49:47 -0700 | [diff] [blame] | 379 | struct fcloop_lsreq *tls_req = ls_rsp_to_lsreq(lsrsp); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 380 | struct nvmefc_ls_req *lsreq = tls_req->lsreq; |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 381 | struct fcloop_tport *tport = targetport->private; |
| 382 | struct nvme_fc_remote_port *remoteport = tport->remoteport; |
| 383 | struct fcloop_rport *rport; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 384 | |
James Smart | 72e6329 | 2020-03-31 09:49:47 -0700 | [diff] [blame] | 385 | memcpy(lsreq->rspaddr, lsrsp->rspbuf, |
| 386 | ((lsreq->rsplen < lsrsp->rsplen) ? |
| 387 | lsreq->rsplen : lsrsp->rsplen)); |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 388 | |
James Smart | 72e6329 | 2020-03-31 09:49:47 -0700 | [diff] [blame] | 389 | lsrsp->done(lsrsp); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 390 | |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 391 | if (remoteport) { |
| 392 | rport = remoteport->private; |
| 393 | spin_lock(&rport->lock); |
Daniel Wagner | dcfad4a | 2024-01-31 09:51:02 +0100 | [diff] [blame] | 394 | list_add_tail(&tls_req->ls_list, &rport->ls_list); |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 395 | spin_unlock(&rport->lock); |
Sagi Grimberg | 8832cf9 | 2022-03-21 13:57:27 +0200 | [diff] [blame] | 396 | queue_work(nvmet_wq, &rport->ls_work); |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 397 | } |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 402 | static void |
| 403 | fcloop_tport_lsrqst_work(struct work_struct *work) |
| 404 | { |
| 405 | struct fcloop_tport *tport = |
| 406 | container_of(work, struct fcloop_tport, ls_work); |
| 407 | struct fcloop_lsreq *tls_req; |
| 408 | |
| 409 | spin_lock(&tport->lock); |
| 410 | for (;;) { |
| 411 | tls_req = list_first_entry_or_null(&tport->ls_list, |
| 412 | struct fcloop_lsreq, ls_list); |
| 413 | if (!tls_req) |
| 414 | break; |
| 415 | |
| 416 | list_del(&tls_req->ls_list); |
| 417 | spin_unlock(&tport->lock); |
| 418 | |
| 419 | tls_req->lsreq->done(tls_req->lsreq, tls_req->status); |
| 420 | /* |
| 421 | * callee may free memory containing tls_req. |
| 422 | * do not reference lsreq after this. |
| 423 | */ |
| 424 | |
| 425 | spin_lock(&tport->lock); |
| 426 | } |
| 427 | spin_unlock(&tport->lock); |
| 428 | } |
| 429 | |
| 430 | static int |
| 431 | fcloop_t2h_ls_req(struct nvmet_fc_target_port *targetport, void *hosthandle, |
| 432 | struct nvmefc_ls_req *lsreq) |
| 433 | { |
| 434 | struct fcloop_lsreq *tls_req = lsreq->private; |
| 435 | struct fcloop_tport *tport = targetport->private; |
| 436 | int ret = 0; |
| 437 | |
| 438 | /* |
| 439 | * hosthandle should be the dst.rport value. |
| 440 | * hosthandle ignored as fcloop currently is |
| 441 | * 1:1 tgtport vs remoteport |
| 442 | */ |
| 443 | tls_req->lsreq = lsreq; |
| 444 | INIT_LIST_HEAD(&tls_req->ls_list); |
| 445 | |
| 446 | if (!tport->remoteport) { |
| 447 | tls_req->status = -ECONNREFUSED; |
| 448 | spin_lock(&tport->lock); |
Daniel Wagner | dcfad4a | 2024-01-31 09:51:02 +0100 | [diff] [blame] | 449 | list_add_tail(&tls_req->ls_list, &tport->ls_list); |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 450 | spin_unlock(&tport->lock); |
Sagi Grimberg | 8832cf9 | 2022-03-21 13:57:27 +0200 | [diff] [blame] | 451 | queue_work(nvmet_wq, &tport->ls_work); |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | tls_req->status = 0; |
| 456 | ret = nvme_fc_rcv_ls_req(tport->remoteport, &tls_req->ls_rsp, |
| 457 | lsreq->rqstaddr, lsreq->rqstlen); |
| 458 | |
| 459 | return ret; |
| 460 | } |
| 461 | |
| 462 | static int |
| 463 | fcloop_t2h_xmt_ls_rsp(struct nvme_fc_local_port *localport, |
| 464 | struct nvme_fc_remote_port *remoteport, |
| 465 | struct nvmefc_ls_rsp *lsrsp) |
| 466 | { |
| 467 | struct fcloop_lsreq *tls_req = ls_rsp_to_lsreq(lsrsp); |
| 468 | struct nvmefc_ls_req *lsreq = tls_req->lsreq; |
| 469 | struct fcloop_rport *rport = remoteport->private; |
| 470 | struct nvmet_fc_target_port *targetport = rport->targetport; |
| 471 | struct fcloop_tport *tport; |
| 472 | |
| 473 | memcpy(lsreq->rspaddr, lsrsp->rspbuf, |
| 474 | ((lsreq->rsplen < lsrsp->rsplen) ? |
| 475 | lsreq->rsplen : lsrsp->rsplen)); |
| 476 | lsrsp->done(lsrsp); |
| 477 | |
| 478 | if (targetport) { |
| 479 | tport = targetport->private; |
| 480 | spin_lock(&tport->lock); |
| 481 | list_add_tail(&tport->ls_list, &tls_req->ls_list); |
| 482 | spin_unlock(&tport->lock); |
Sagi Grimberg | 8832cf9 | 2022-03-21 13:57:27 +0200 | [diff] [blame] | 483 | queue_work(nvmet_wq, &tport->ls_work); |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static void |
| 490 | fcloop_t2h_host_release(void *hosthandle) |
| 491 | { |
| 492 | /* host handle ignored for now */ |
| 493 | } |
| 494 | |
James Smart | 4cf7c36 | 2019-05-14 14:58:04 -0700 | [diff] [blame] | 495 | /* |
| 496 | * Simulate reception of RSCN and converting it to a initiator transport |
| 497 | * call to rescan a remote port. |
| 498 | */ |
| 499 | static void |
| 500 | fcloop_tgt_rscn_work(struct work_struct *work) |
| 501 | { |
| 502 | struct fcloop_rscn *tgt_rscn = |
| 503 | container_of(work, struct fcloop_rscn, work); |
| 504 | struct fcloop_tport *tport = tgt_rscn->tport; |
| 505 | |
| 506 | if (tport->remoteport) |
| 507 | nvme_fc_rescan_remoteport(tport->remoteport); |
| 508 | kfree(tgt_rscn); |
| 509 | } |
| 510 | |
| 511 | static void |
| 512 | fcloop_tgt_discovery_evt(struct nvmet_fc_target_port *tgtport) |
| 513 | { |
| 514 | struct fcloop_rscn *tgt_rscn; |
| 515 | |
| 516 | tgt_rscn = kzalloc(sizeof(*tgt_rscn), GFP_KERNEL); |
| 517 | if (!tgt_rscn) |
| 518 | return; |
| 519 | |
| 520 | tgt_rscn->tport = tgtport->private; |
| 521 | INIT_WORK(&tgt_rscn->work, fcloop_tgt_rscn_work); |
| 522 | |
Sagi Grimberg | 8832cf9 | 2022-03-21 13:57:27 +0200 | [diff] [blame] | 523 | queue_work(nvmet_wq, &tgt_rscn->work); |
James Smart | 4cf7c36 | 2019-05-14 14:58:04 -0700 | [diff] [blame] | 524 | } |
| 525 | |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 526 | static void |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 527 | fcloop_tfcp_req_free(struct kref *ref) |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 528 | { |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 529 | struct fcloop_fcpreq *tfcp_req = |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 530 | container_of(ref, struct fcloop_fcpreq, ref); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 531 | |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 532 | kfree(tfcp_req); |
| 533 | } |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 534 | |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 535 | static void |
| 536 | fcloop_tfcp_req_put(struct fcloop_fcpreq *tfcp_req) |
| 537 | { |
| 538 | kref_put(&tfcp_req->ref, fcloop_tfcp_req_free); |
| 539 | } |
| 540 | |
| 541 | static int |
| 542 | fcloop_tfcp_req_get(struct fcloop_fcpreq *tfcp_req) |
| 543 | { |
| 544 | return kref_get_unless_zero(&tfcp_req->ref); |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static void |
| 548 | fcloop_call_host_done(struct nvmefc_fcp_req *fcpreq, |
| 549 | struct fcloop_fcpreq *tfcp_req, int status) |
| 550 | { |
| 551 | struct fcloop_ini_fcpreq *inireq = NULL; |
| 552 | |
| 553 | if (fcpreq) { |
| 554 | inireq = fcpreq->private; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 555 | spin_lock(&inireq->inilock); |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 556 | inireq->tfcp_req = NULL; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 557 | spin_unlock(&inireq->inilock); |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 558 | |
| 559 | fcpreq->status = status; |
| 560 | fcpreq->done(fcpreq); |
| 561 | } |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 562 | |
| 563 | /* release original io reference on tgt struct */ |
| 564 | fcloop_tfcp_req_put(tfcp_req); |
| 565 | } |
| 566 | |
James Smart | 03d99e5 | 2020-10-16 14:28:38 -0700 | [diff] [blame] | 567 | static bool drop_fabric_opcode; |
| 568 | #define DROP_OPCODE_MASK 0x00FF |
| 569 | /* fabrics opcode will have a bit set above 1st byte */ |
| 570 | static int drop_opcode = -1; |
| 571 | static int drop_instance; |
| 572 | static int drop_amount; |
| 573 | static int drop_current_cnt; |
| 574 | |
| 575 | /* |
| 576 | * Routine to parse io and determine if the io is to be dropped. |
| 577 | * Returns: |
| 578 | * 0 if io is not obstructed |
| 579 | * 1 if io was dropped |
| 580 | */ |
| 581 | static int check_for_drop(struct fcloop_fcpreq *tfcp_req) |
| 582 | { |
| 583 | struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq; |
| 584 | struct nvme_fc_cmd_iu *cmdiu = fcpreq->cmdaddr; |
| 585 | struct nvme_command *sqe = &cmdiu->sqe; |
| 586 | |
| 587 | if (drop_opcode == -1) |
| 588 | return 0; |
| 589 | |
| 590 | pr_info("%s: seq opcd x%02x fctype x%02x: drop F %s op x%02x " |
| 591 | "inst %d start %d amt %d\n", |
| 592 | __func__, sqe->common.opcode, sqe->fabrics.fctype, |
| 593 | drop_fabric_opcode ? "y" : "n", |
| 594 | drop_opcode, drop_current_cnt, drop_instance, drop_amount); |
| 595 | |
| 596 | if ((drop_fabric_opcode && |
| 597 | (sqe->common.opcode != nvme_fabrics_command || |
| 598 | sqe->fabrics.fctype != drop_opcode)) || |
| 599 | (!drop_fabric_opcode && sqe->common.opcode != drop_opcode)) |
| 600 | return 0; |
| 601 | |
| 602 | if (++drop_current_cnt >= drop_instance) { |
| 603 | if (drop_current_cnt >= drop_instance + drop_amount) |
| 604 | drop_opcode = -1; |
| 605 | return 1; |
| 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 611 | static void |
| 612 | fcloop_fcp_recv_work(struct work_struct *work) |
| 613 | { |
| 614 | struct fcloop_fcpreq *tfcp_req = |
| 615 | container_of(work, struct fcloop_fcpreq, fcp_rcv_work); |
| 616 | struct nvmefc_fcp_req *fcpreq = tfcp_req->fcpreq; |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 617 | unsigned long flags; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 618 | int ret = 0; |
| 619 | bool aborted = false; |
| 620 | |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 621 | spin_lock_irqsave(&tfcp_req->reqlock, flags); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 622 | switch (tfcp_req->inistate) { |
| 623 | case INI_IO_START: |
| 624 | tfcp_req->inistate = INI_IO_ACTIVE; |
| 625 | break; |
| 626 | case INI_IO_ABORTED: |
| 627 | aborted = true; |
| 628 | break; |
| 629 | default: |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 630 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 631 | WARN_ON(1); |
| 632 | return; |
| 633 | } |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 634 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 635 | |
| 636 | if (unlikely(aborted)) |
| 637 | ret = -ECANCELED; |
James Smart | 03d99e5 | 2020-10-16 14:28:38 -0700 | [diff] [blame] | 638 | else { |
| 639 | if (likely(!check_for_drop(tfcp_req))) |
| 640 | ret = nvmet_fc_rcv_fcp_req(tfcp_req->tport->targetport, |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 641 | &tfcp_req->tgt_fcp_req, |
| 642 | fcpreq->cmdaddr, fcpreq->cmdlen); |
James Smart | 03d99e5 | 2020-10-16 14:28:38 -0700 | [diff] [blame] | 643 | else |
| 644 | pr_info("%s: dropped command ********\n", __func__); |
| 645 | } |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 646 | if (ret) |
| 647 | fcloop_call_host_done(fcpreq, tfcp_req, ret); |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static void |
| 651 | fcloop_fcp_abort_recv_work(struct work_struct *work) |
| 652 | { |
| 653 | struct fcloop_fcpreq *tfcp_req = |
| 654 | container_of(work, struct fcloop_fcpreq, abort_rcv_work); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 655 | struct nvmefc_fcp_req *fcpreq; |
| 656 | bool completed = false; |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 657 | unsigned long flags; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 658 | |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 659 | spin_lock_irqsave(&tfcp_req->reqlock, flags); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 660 | fcpreq = tfcp_req->fcpreq; |
| 661 | switch (tfcp_req->inistate) { |
| 662 | case INI_IO_ABORTED: |
| 663 | break; |
| 664 | case INI_IO_COMPLETED: |
| 665 | completed = true; |
| 666 | break; |
| 667 | default: |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 668 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 669 | WARN_ON(1); |
| 670 | return; |
| 671 | } |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 672 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 673 | |
| 674 | if (unlikely(completed)) { |
| 675 | /* remove reference taken in original abort downcall */ |
| 676 | fcloop_tfcp_req_put(tfcp_req); |
| 677 | return; |
| 678 | } |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 679 | |
| 680 | if (tfcp_req->tport->targetport) |
| 681 | nvmet_fc_rcv_fcp_abort(tfcp_req->tport->targetport, |
| 682 | &tfcp_req->tgt_fcp_req); |
| 683 | |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 684 | spin_lock_irqsave(&tfcp_req->reqlock, flags); |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 685 | tfcp_req->fcpreq = NULL; |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 686 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 687 | |
| 688 | fcloop_call_host_done(fcpreq, tfcp_req, -ECANCELED); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 689 | /* call_host_done releases reference for abort downcall */ |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /* |
| 693 | * FCP IO operation done by target completion. |
| 694 | * call back up initiator "done" flows. |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 695 | */ |
| 696 | static void |
| 697 | fcloop_tgt_fcprqst_done_work(struct work_struct *work) |
| 698 | { |
| 699 | struct fcloop_fcpreq *tfcp_req = |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 700 | container_of(work, struct fcloop_fcpreq, tio_done_work); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 701 | struct nvmefc_fcp_req *fcpreq; |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 702 | unsigned long flags; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 703 | |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 704 | spin_lock_irqsave(&tfcp_req->reqlock, flags); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 705 | fcpreq = tfcp_req->fcpreq; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 706 | tfcp_req->inistate = INI_IO_COMPLETED; |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 707 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 708 | |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 709 | fcloop_call_host_done(fcpreq, tfcp_req, tfcp_req->status); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | |
| 713 | static int |
| 714 | fcloop_fcp_req(struct nvme_fc_local_port *localport, |
| 715 | struct nvme_fc_remote_port *remoteport, |
| 716 | void *hw_queue_handle, |
| 717 | struct nvmefc_fcp_req *fcpreq) |
| 718 | { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 719 | struct fcloop_rport *rport = remoteport->private; |
James Smart | ce79bfc | 2017-04-11 11:32:30 -0700 | [diff] [blame] | 720 | struct fcloop_ini_fcpreq *inireq = fcpreq->private; |
| 721 | struct fcloop_fcpreq *tfcp_req; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 722 | |
James Smart | ce79bfc | 2017-04-11 11:32:30 -0700 | [diff] [blame] | 723 | if (!rport->targetport) |
| 724 | return -ECONNREFUSED; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 725 | |
James Smart | e0620bf | 2019-06-20 13:17:01 -0700 | [diff] [blame] | 726 | tfcp_req = kzalloc(sizeof(*tfcp_req), GFP_ATOMIC); |
James Smart | ce79bfc | 2017-04-11 11:32:30 -0700 | [diff] [blame] | 727 | if (!tfcp_req) |
| 728 | return -ENOMEM; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 729 | |
James Smart | ce79bfc | 2017-04-11 11:32:30 -0700 | [diff] [blame] | 730 | inireq->fcpreq = fcpreq; |
| 731 | inireq->tfcp_req = tfcp_req; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 732 | spin_lock_init(&inireq->inilock); |
| 733 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 734 | tfcp_req->fcpreq = fcpreq; |
| 735 | tfcp_req->tport = rport->targetport->private; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 736 | tfcp_req->inistate = INI_IO_START; |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 737 | spin_lock_init(&tfcp_req->reqlock); |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 738 | INIT_WORK(&tfcp_req->fcp_rcv_work, fcloop_fcp_recv_work); |
| 739 | INIT_WORK(&tfcp_req->abort_rcv_work, fcloop_fcp_abort_recv_work); |
| 740 | INIT_WORK(&tfcp_req->tio_done_work, fcloop_tgt_fcprqst_done_work); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 741 | kref_init(&tfcp_req->ref); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 742 | |
Sagi Grimberg | 8832cf9 | 2022-03-21 13:57:27 +0200 | [diff] [blame] | 743 | queue_work(nvmet_wq, &tfcp_req->fcp_rcv_work); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 744 | |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 745 | return 0; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | static void |
| 749 | fcloop_fcp_copy_data(u8 op, struct scatterlist *data_sg, |
| 750 | struct scatterlist *io_sg, u32 offset, u32 length) |
| 751 | { |
| 752 | void *data_p, *io_p; |
| 753 | u32 data_len, io_len, tlen; |
| 754 | |
| 755 | io_p = sg_virt(io_sg); |
| 756 | io_len = io_sg->length; |
| 757 | |
| 758 | for ( ; offset; ) { |
| 759 | tlen = min_t(u32, offset, io_len); |
| 760 | offset -= tlen; |
| 761 | io_len -= tlen; |
| 762 | if (!io_len) { |
| 763 | io_sg = sg_next(io_sg); |
| 764 | io_p = sg_virt(io_sg); |
| 765 | io_len = io_sg->length; |
| 766 | } else |
| 767 | io_p += tlen; |
| 768 | } |
| 769 | |
| 770 | data_p = sg_virt(data_sg); |
| 771 | data_len = data_sg->length; |
| 772 | |
| 773 | for ( ; length; ) { |
| 774 | tlen = min_t(u32, io_len, data_len); |
| 775 | tlen = min_t(u32, tlen, length); |
| 776 | |
| 777 | if (op == NVMET_FCOP_WRITEDATA) |
| 778 | memcpy(data_p, io_p, tlen); |
| 779 | else |
| 780 | memcpy(io_p, data_p, tlen); |
| 781 | |
| 782 | length -= tlen; |
| 783 | |
| 784 | io_len -= tlen; |
| 785 | if ((!io_len) && (length)) { |
| 786 | io_sg = sg_next(io_sg); |
| 787 | io_p = sg_virt(io_sg); |
| 788 | io_len = io_sg->length; |
| 789 | } else |
| 790 | io_p += tlen; |
| 791 | |
| 792 | data_len -= tlen; |
| 793 | if ((!data_len) && (length)) { |
| 794 | data_sg = sg_next(data_sg); |
| 795 | data_p = sg_virt(data_sg); |
| 796 | data_len = data_sg->length; |
| 797 | } else |
| 798 | data_p += tlen; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | static int |
| 803 | fcloop_fcp_op(struct nvmet_fc_target_port *tgtport, |
| 804 | struct nvmefc_tgt_fcp_req *tgt_fcpreq) |
| 805 | { |
| 806 | struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 807 | struct nvmefc_fcp_req *fcpreq; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 808 | u32 rsplen = 0, xfrlen = 0; |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 809 | int fcp_err = 0, active, aborted; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 810 | u8 op = tgt_fcpreq->op; |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 811 | unsigned long flags; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 812 | |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 813 | spin_lock_irqsave(&tfcp_req->reqlock, flags); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 814 | fcpreq = tfcp_req->fcpreq; |
| 815 | active = tfcp_req->active; |
| 816 | aborted = tfcp_req->aborted; |
| 817 | tfcp_req->active = true; |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 818 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 819 | |
| 820 | if (unlikely(active)) |
| 821 | /* illegal - call while i/o active */ |
| 822 | return -EALREADY; |
| 823 | |
| 824 | if (unlikely(aborted)) { |
| 825 | /* target transport has aborted i/o prior */ |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 826 | spin_lock_irqsave(&tfcp_req->reqlock, flags); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 827 | tfcp_req->active = false; |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 828 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 829 | tgt_fcpreq->transferred_length = 0; |
| 830 | tgt_fcpreq->fcp_error = -ECANCELED; |
| 831 | tgt_fcpreq->done(tgt_fcpreq); |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * if fcpreq is NULL, the I/O has been aborted (from |
| 837 | * initiator side). For the target side, act as if all is well |
| 838 | * but don't actually move data. |
| 839 | */ |
| 840 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 841 | switch (op) { |
| 842 | case NVMET_FCOP_WRITEDATA: |
| 843 | xfrlen = tgt_fcpreq->transfer_length; |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 844 | if (fcpreq) { |
| 845 | fcloop_fcp_copy_data(op, tgt_fcpreq->sg, |
| 846 | fcpreq->first_sgl, tgt_fcpreq->offset, |
| 847 | xfrlen); |
| 848 | fcpreq->transferred_length += xfrlen; |
| 849 | } |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 850 | break; |
| 851 | |
| 852 | case NVMET_FCOP_READDATA: |
| 853 | case NVMET_FCOP_READDATA_RSP: |
| 854 | xfrlen = tgt_fcpreq->transfer_length; |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 855 | if (fcpreq) { |
| 856 | fcloop_fcp_copy_data(op, tgt_fcpreq->sg, |
| 857 | fcpreq->first_sgl, tgt_fcpreq->offset, |
| 858 | xfrlen); |
| 859 | fcpreq->transferred_length += xfrlen; |
| 860 | } |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 861 | if (op == NVMET_FCOP_READDATA) |
| 862 | break; |
| 863 | |
| 864 | /* Fall-Thru to RSP handling */ |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 865 | fallthrough; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 866 | |
| 867 | case NVMET_FCOP_RSP: |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 868 | if (fcpreq) { |
| 869 | rsplen = ((fcpreq->rsplen < tgt_fcpreq->rsplen) ? |
| 870 | fcpreq->rsplen : tgt_fcpreq->rsplen); |
| 871 | memcpy(fcpreq->rspaddr, tgt_fcpreq->rspaddr, rsplen); |
| 872 | if (rsplen < tgt_fcpreq->rsplen) |
| 873 | fcp_err = -E2BIG; |
| 874 | fcpreq->rcv_rsplen = rsplen; |
| 875 | fcpreq->status = 0; |
| 876 | } |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 877 | tfcp_req->status = 0; |
| 878 | break; |
| 879 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 880 | default: |
| 881 | fcp_err = -EINVAL; |
| 882 | break; |
| 883 | } |
| 884 | |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 885 | spin_lock_irqsave(&tfcp_req->reqlock, flags); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 886 | tfcp_req->active = false; |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 887 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 888 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 889 | tgt_fcpreq->transferred_length = xfrlen; |
| 890 | tgt_fcpreq->fcp_error = fcp_err; |
| 891 | tgt_fcpreq->done(tgt_fcpreq); |
| 892 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | static void |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 897 | fcloop_tgt_fcp_abort(struct nvmet_fc_target_port *tgtport, |
| 898 | struct nvmefc_tgt_fcp_req *tgt_fcpreq) |
| 899 | { |
| 900 | struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq); |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 901 | unsigned long flags; |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 902 | |
| 903 | /* |
| 904 | * mark aborted only in case there were 2 threads in transport |
| 905 | * (one doing io, other doing abort) and only kills ops posted |
| 906 | * after the abort request |
| 907 | */ |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 908 | spin_lock_irqsave(&tfcp_req->reqlock, flags); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 909 | tfcp_req->aborted = true; |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 910 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 911 | |
James Smart | fc9608e | 2017-09-07 16:27:28 -0700 | [diff] [blame] | 912 | tfcp_req->status = NVME_SC_INTERNAL; |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 913 | |
| 914 | /* |
| 915 | * nothing more to do. If io wasn't active, the transport should |
| 916 | * immediately call the req_release. If it was active, the op |
| 917 | * will complete, and the lldd should call req_release. |
| 918 | */ |
| 919 | } |
| 920 | |
| 921 | static void |
James Smart | 19b58d9 | 2017-04-11 11:32:29 -0700 | [diff] [blame] | 922 | fcloop_fcp_req_release(struct nvmet_fc_target_port *tgtport, |
| 923 | struct nvmefc_tgt_fcp_req *tgt_fcpreq) |
| 924 | { |
| 925 | struct fcloop_fcpreq *tfcp_req = tgt_fcp_req_to_fcpreq(tgt_fcpreq); |
| 926 | |
Sagi Grimberg | 8832cf9 | 2022-03-21 13:57:27 +0200 | [diff] [blame] | 927 | queue_work(nvmet_wq, &tfcp_req->tio_done_work); |
James Smart | 19b58d9 | 2017-04-11 11:32:29 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | static void |
James Smart | ea39765 | 2020-03-31 09:50:00 -0700 | [diff] [blame] | 931 | fcloop_h2t_ls_abort(struct nvme_fc_local_port *localport, |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 932 | struct nvme_fc_remote_port *remoteport, |
| 933 | struct nvmefc_ls_req *lsreq) |
| 934 | { |
| 935 | } |
| 936 | |
| 937 | static void |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 938 | fcloop_t2h_ls_abort(struct nvmet_fc_target_port *targetport, |
| 939 | void *hosthandle, struct nvmefc_ls_req *lsreq) |
| 940 | { |
| 941 | } |
| 942 | |
| 943 | static void |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 944 | fcloop_fcp_abort(struct nvme_fc_local_port *localport, |
| 945 | struct nvme_fc_remote_port *remoteport, |
| 946 | void *hw_queue_handle, |
| 947 | struct nvmefc_fcp_req *fcpreq) |
| 948 | { |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 949 | struct fcloop_ini_fcpreq *inireq = fcpreq->private; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 950 | struct fcloop_fcpreq *tfcp_req; |
| 951 | bool abortio = true; |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 952 | unsigned long flags; |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 953 | |
| 954 | spin_lock(&inireq->inilock); |
| 955 | tfcp_req = inireq->tfcp_req; |
| 956 | if (tfcp_req) |
| 957 | fcloop_tfcp_req_get(tfcp_req); |
| 958 | spin_unlock(&inireq->inilock); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 959 | |
| 960 | if (!tfcp_req) |
| 961 | /* abort has already been called */ |
| 962 | return; |
| 963 | |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 964 | /* break initiator/target relationship for io */ |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 965 | spin_lock_irqsave(&tfcp_req->reqlock, flags); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 966 | switch (tfcp_req->inistate) { |
| 967 | case INI_IO_START: |
| 968 | case INI_IO_ACTIVE: |
| 969 | tfcp_req->inistate = INI_IO_ABORTED; |
| 970 | break; |
| 971 | case INI_IO_COMPLETED: |
| 972 | abortio = false; |
| 973 | break; |
| 974 | default: |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 975 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 976 | WARN_ON(1); |
| 977 | return; |
| 978 | } |
Ming Lei | 4f86a6f | 2023-04-12 16:49:04 +0800 | [diff] [blame] | 979 | spin_unlock_irqrestore(&tfcp_req->reqlock, flags); |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 980 | |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 981 | if (abortio) |
| 982 | /* leave the reference while the work item is scheduled */ |
Sagi Grimberg | 8832cf9 | 2022-03-21 13:57:27 +0200 | [diff] [blame] | 983 | WARN_ON(!queue_work(nvmet_wq, &tfcp_req->abort_rcv_work)); |
James Smart | b6f8077 | 2017-11-29 16:47:33 -0800 | [diff] [blame] | 984 | else { |
| 985 | /* |
| 986 | * as the io has already had the done callback made, |
| 987 | * nothing more to do. So release the reference taken above |
| 988 | */ |
| 989 | fcloop_tfcp_req_put(tfcp_req); |
| 990 | } |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | static void |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 994 | fcloop_nport_free(struct kref *ref) |
| 995 | { |
| 996 | struct fcloop_nport *nport = |
| 997 | container_of(ref, struct fcloop_nport, ref); |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 998 | |
| 999 | kfree(nport); |
| 1000 | } |
| 1001 | |
| 1002 | static void |
| 1003 | fcloop_nport_put(struct fcloop_nport *nport) |
| 1004 | { |
| 1005 | kref_put(&nport->ref, fcloop_nport_free); |
| 1006 | } |
| 1007 | |
| 1008 | static int |
| 1009 | fcloop_nport_get(struct fcloop_nport *nport) |
| 1010 | { |
| 1011 | return kref_get_unless_zero(&nport->ref); |
| 1012 | } |
| 1013 | |
| 1014 | static void |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1015 | fcloop_localport_delete(struct nvme_fc_local_port *localport) |
| 1016 | { |
James Smart | 6fda202 | 2017-11-29 16:47:31 -0800 | [diff] [blame] | 1017 | struct fcloop_lport_priv *lport_priv = localport->private; |
| 1018 | struct fcloop_lport *lport = lport_priv->lport; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1019 | |
| 1020 | /* release any threads waiting for the unreg to complete */ |
| 1021 | complete(&lport->unreg_done); |
| 1022 | } |
| 1023 | |
| 1024 | static void |
| 1025 | fcloop_remoteport_delete(struct nvme_fc_remote_port *remoteport) |
| 1026 | { |
| 1027 | struct fcloop_rport *rport = remoteport->private; |
| 1028 | |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 1029 | flush_work(&rport->ls_work); |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1030 | fcloop_nport_put(rport->nport); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static void |
| 1034 | fcloop_targetport_delete(struct nvmet_fc_target_port *targetport) |
| 1035 | { |
| 1036 | struct fcloop_tport *tport = targetport->private; |
| 1037 | |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 1038 | flush_work(&tport->ls_work); |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1039 | fcloop_nport_put(tport->nport); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | #define FCLOOP_HW_QUEUES 4 |
| 1043 | #define FCLOOP_SGL_SEGS 256 |
| 1044 | #define FCLOOP_DMABOUND_4G 0xFFFFFFFF |
| 1045 | |
Christoph Hellwig | 36b8890 | 2017-04-21 10:37:25 +0200 | [diff] [blame] | 1046 | static struct nvme_fc_port_template fctemplate = { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1047 | .localport_delete = fcloop_localport_delete, |
| 1048 | .remoteport_delete = fcloop_remoteport_delete, |
| 1049 | .create_queue = fcloop_create_queue, |
| 1050 | .delete_queue = fcloop_delete_queue, |
James Smart | ea39765 | 2020-03-31 09:50:00 -0700 | [diff] [blame] | 1051 | .ls_req = fcloop_h2t_ls_req, |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1052 | .fcp_io = fcloop_fcp_req, |
James Smart | ea39765 | 2020-03-31 09:50:00 -0700 | [diff] [blame] | 1053 | .ls_abort = fcloop_h2t_ls_abort, |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1054 | .fcp_abort = fcloop_fcp_abort, |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 1055 | .xmt_ls_rsp = fcloop_t2h_xmt_ls_rsp, |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1056 | .max_hw_queues = FCLOOP_HW_QUEUES, |
| 1057 | .max_sgl_segments = FCLOOP_SGL_SEGS, |
| 1058 | .max_dif_sgl_segments = FCLOOP_SGL_SEGS, |
| 1059 | .dma_boundary = FCLOOP_DMABOUND_4G, |
| 1060 | /* sizes of additional private data for data structures */ |
James Smart | 6fda202 | 2017-11-29 16:47:31 -0800 | [diff] [blame] | 1061 | .local_priv_sz = sizeof(struct fcloop_lport_priv), |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1062 | .remote_priv_sz = sizeof(struct fcloop_rport), |
| 1063 | .lsrqst_priv_sz = sizeof(struct fcloop_lsreq), |
James Smart | ce79bfc | 2017-04-11 11:32:30 -0700 | [diff] [blame] | 1064 | .fcprqst_priv_sz = sizeof(struct fcloop_ini_fcpreq), |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1065 | }; |
| 1066 | |
Christoph Hellwig | 36b8890 | 2017-04-21 10:37:25 +0200 | [diff] [blame] | 1067 | static struct nvmet_fc_target_template tgttemplate = { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1068 | .targetport_delete = fcloop_targetport_delete, |
James Smart | ea39765 | 2020-03-31 09:50:00 -0700 | [diff] [blame] | 1069 | .xmt_ls_rsp = fcloop_h2t_xmt_ls_rsp, |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1070 | .fcp_op = fcloop_fcp_op, |
James Smart | a97ec51b | 2017-04-11 11:32:31 -0700 | [diff] [blame] | 1071 | .fcp_abort = fcloop_tgt_fcp_abort, |
James Smart | 19b58d9 | 2017-04-11 11:32:29 -0700 | [diff] [blame] | 1072 | .fcp_req_release = fcloop_fcp_req_release, |
James Smart | 4cf7c36 | 2019-05-14 14:58:04 -0700 | [diff] [blame] | 1073 | .discovery_event = fcloop_tgt_discovery_evt, |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 1074 | .ls_req = fcloop_t2h_ls_req, |
| 1075 | .ls_abort = fcloop_t2h_ls_abort, |
| 1076 | .host_release = fcloop_t2h_host_release, |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1077 | .max_hw_queues = FCLOOP_HW_QUEUES, |
| 1078 | .max_sgl_segments = FCLOOP_SGL_SEGS, |
| 1079 | .max_dif_sgl_segments = FCLOOP_SGL_SEGS, |
| 1080 | .dma_boundary = FCLOOP_DMABOUND_4G, |
| 1081 | /* optional features */ |
James Smart | 24431d6 | 2017-11-29 16:47:32 -0800 | [diff] [blame] | 1082 | .target_features = 0, |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1083 | /* sizes of additional private data for data structures */ |
| 1084 | .target_priv_sz = sizeof(struct fcloop_tport), |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 1085 | .lsrqst_priv_sz = sizeof(struct fcloop_lsreq), |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1086 | }; |
| 1087 | |
| 1088 | static ssize_t |
| 1089 | fcloop_create_local_port(struct device *dev, struct device_attribute *attr, |
| 1090 | const char *buf, size_t count) |
| 1091 | { |
| 1092 | struct nvme_fc_port_info pinfo; |
| 1093 | struct fcloop_ctrl_options *opts; |
| 1094 | struct nvme_fc_local_port *localport; |
| 1095 | struct fcloop_lport *lport; |
James Smart | 6fda202 | 2017-11-29 16:47:31 -0800 | [diff] [blame] | 1096 | struct fcloop_lport_priv *lport_priv; |
| 1097 | unsigned long flags; |
| 1098 | int ret = -ENOMEM; |
| 1099 | |
| 1100 | lport = kzalloc(sizeof(*lport), GFP_KERNEL); |
| 1101 | if (!lport) |
| 1102 | return -ENOMEM; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1103 | |
| 1104 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 1105 | if (!opts) |
James Smart | 6fda202 | 2017-11-29 16:47:31 -0800 | [diff] [blame] | 1106 | goto out_free_lport; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1107 | |
| 1108 | ret = fcloop_parse_options(opts, buf); |
| 1109 | if (ret) |
| 1110 | goto out_free_opts; |
| 1111 | |
| 1112 | /* everything there ? */ |
| 1113 | if ((opts->mask & LPORT_OPTS) != LPORT_OPTS) { |
| 1114 | ret = -EINVAL; |
| 1115 | goto out_free_opts; |
| 1116 | } |
| 1117 | |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1118 | memset(&pinfo, 0, sizeof(pinfo)); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1119 | pinfo.node_name = opts->wwnn; |
| 1120 | pinfo.port_name = opts->wwpn; |
| 1121 | pinfo.port_role = opts->roles; |
| 1122 | pinfo.port_id = opts->fcaddr; |
| 1123 | |
| 1124 | ret = nvme_fc_register_localport(&pinfo, &fctemplate, NULL, &localport); |
| 1125 | if (!ret) { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1126 | /* success */ |
James Smart | 6fda202 | 2017-11-29 16:47:31 -0800 | [diff] [blame] | 1127 | lport_priv = localport->private; |
| 1128 | lport_priv->lport = lport; |
| 1129 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1130 | lport->localport = localport; |
| 1131 | INIT_LIST_HEAD(&lport->lport_list); |
| 1132 | |
| 1133 | spin_lock_irqsave(&fcloop_lock, flags); |
| 1134 | list_add_tail(&lport->lport_list, &fcloop_lports); |
| 1135 | spin_unlock_irqrestore(&fcloop_lock, flags); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | out_free_opts: |
| 1139 | kfree(opts); |
James Smart | 6fda202 | 2017-11-29 16:47:31 -0800 | [diff] [blame] | 1140 | out_free_lport: |
| 1141 | /* free only if we're going to fail */ |
| 1142 | if (ret) |
| 1143 | kfree(lport); |
| 1144 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1145 | return ret ? ret : count; |
| 1146 | } |
| 1147 | |
| 1148 | |
| 1149 | static void |
| 1150 | __unlink_local_port(struct fcloop_lport *lport) |
| 1151 | { |
| 1152 | list_del(&lport->lport_list); |
| 1153 | } |
| 1154 | |
| 1155 | static int |
| 1156 | __wait_localport_unreg(struct fcloop_lport *lport) |
| 1157 | { |
| 1158 | int ret; |
| 1159 | |
| 1160 | init_completion(&lport->unreg_done); |
| 1161 | |
| 1162 | ret = nvme_fc_unregister_localport(lport->localport); |
| 1163 | |
Daniel Wagner | d97b411 | 2023-06-06 14:24:11 +0200 | [diff] [blame] | 1164 | if (!ret) |
| 1165 | wait_for_completion(&lport->unreg_done); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1166 | |
James Smart | 6fda202 | 2017-11-29 16:47:31 -0800 | [diff] [blame] | 1167 | kfree(lport); |
| 1168 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1169 | return ret; |
| 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | static ssize_t |
| 1174 | fcloop_delete_local_port(struct device *dev, struct device_attribute *attr, |
| 1175 | const char *buf, size_t count) |
| 1176 | { |
| 1177 | struct fcloop_lport *tlport, *lport = NULL; |
| 1178 | u64 nodename, portname; |
| 1179 | unsigned long flags; |
| 1180 | int ret; |
| 1181 | |
| 1182 | ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf); |
| 1183 | if (ret) |
| 1184 | return ret; |
| 1185 | |
| 1186 | spin_lock_irqsave(&fcloop_lock, flags); |
| 1187 | |
| 1188 | list_for_each_entry(tlport, &fcloop_lports, lport_list) { |
| 1189 | if (tlport->localport->node_name == nodename && |
| 1190 | tlport->localport->port_name == portname) { |
| 1191 | lport = tlport; |
| 1192 | __unlink_local_port(lport); |
| 1193 | break; |
| 1194 | } |
| 1195 | } |
| 1196 | spin_unlock_irqrestore(&fcloop_lock, flags); |
| 1197 | |
| 1198 | if (!lport) |
| 1199 | return -ENOENT; |
| 1200 | |
| 1201 | ret = __wait_localport_unreg(lport); |
| 1202 | |
| 1203 | return ret ? ret : count; |
| 1204 | } |
| 1205 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1206 | static struct fcloop_nport * |
| 1207 | fcloop_alloc_nport(const char *buf, size_t count, bool remoteport) |
| 1208 | { |
| 1209 | struct fcloop_nport *newnport, *nport = NULL; |
| 1210 | struct fcloop_lport *tmplport, *lport = NULL; |
| 1211 | struct fcloop_ctrl_options *opts; |
| 1212 | unsigned long flags; |
| 1213 | u32 opts_mask = (remoteport) ? RPORT_OPTS : TGTPORT_OPTS; |
| 1214 | int ret; |
| 1215 | |
| 1216 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 1217 | if (!opts) |
| 1218 | return NULL; |
| 1219 | |
| 1220 | ret = fcloop_parse_options(opts, buf); |
| 1221 | if (ret) |
| 1222 | goto out_free_opts; |
| 1223 | |
| 1224 | /* everything there ? */ |
| 1225 | if ((opts->mask & opts_mask) != opts_mask) { |
| 1226 | ret = -EINVAL; |
| 1227 | goto out_free_opts; |
| 1228 | } |
| 1229 | |
| 1230 | newnport = kzalloc(sizeof(*newnport), GFP_KERNEL); |
| 1231 | if (!newnport) |
| 1232 | goto out_free_opts; |
| 1233 | |
| 1234 | INIT_LIST_HEAD(&newnport->nport_list); |
| 1235 | newnport->node_name = opts->wwnn; |
| 1236 | newnport->port_name = opts->wwpn; |
| 1237 | if (opts->mask & NVMF_OPT_ROLES) |
| 1238 | newnport->port_role = opts->roles; |
| 1239 | if (opts->mask & NVMF_OPT_FCADDR) |
| 1240 | newnport->port_id = opts->fcaddr; |
| 1241 | kref_init(&newnport->ref); |
| 1242 | |
| 1243 | spin_lock_irqsave(&fcloop_lock, flags); |
| 1244 | |
| 1245 | list_for_each_entry(tmplport, &fcloop_lports, lport_list) { |
| 1246 | if (tmplport->localport->node_name == opts->wwnn && |
| 1247 | tmplport->localport->port_name == opts->wwpn) |
| 1248 | goto out_invalid_opts; |
| 1249 | |
| 1250 | if (tmplport->localport->node_name == opts->lpwwnn && |
| 1251 | tmplport->localport->port_name == opts->lpwwpn) |
| 1252 | lport = tmplport; |
| 1253 | } |
| 1254 | |
| 1255 | if (remoteport) { |
| 1256 | if (!lport) |
| 1257 | goto out_invalid_opts; |
| 1258 | newnport->lport = lport; |
| 1259 | } |
| 1260 | |
| 1261 | list_for_each_entry(nport, &fcloop_nports, nport_list) { |
| 1262 | if (nport->node_name == opts->wwnn && |
| 1263 | nport->port_name == opts->wwpn) { |
| 1264 | if ((remoteport && nport->rport) || |
| 1265 | (!remoteport && nport->tport)) { |
| 1266 | nport = NULL; |
| 1267 | goto out_invalid_opts; |
| 1268 | } |
| 1269 | |
| 1270 | fcloop_nport_get(nport); |
| 1271 | |
| 1272 | spin_unlock_irqrestore(&fcloop_lock, flags); |
| 1273 | |
| 1274 | if (remoteport) |
| 1275 | nport->lport = lport; |
| 1276 | if (opts->mask & NVMF_OPT_ROLES) |
| 1277 | nport->port_role = opts->roles; |
| 1278 | if (opts->mask & NVMF_OPT_FCADDR) |
| 1279 | nport->port_id = opts->fcaddr; |
| 1280 | goto out_free_newnport; |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | list_add_tail(&newnport->nport_list, &fcloop_nports); |
| 1285 | |
| 1286 | spin_unlock_irqrestore(&fcloop_lock, flags); |
| 1287 | |
| 1288 | kfree(opts); |
| 1289 | return newnport; |
| 1290 | |
| 1291 | out_invalid_opts: |
| 1292 | spin_unlock_irqrestore(&fcloop_lock, flags); |
| 1293 | out_free_newnport: |
| 1294 | kfree(newnport); |
| 1295 | out_free_opts: |
| 1296 | kfree(opts); |
| 1297 | return nport; |
| 1298 | } |
| 1299 | |
| 1300 | static ssize_t |
| 1301 | fcloop_create_remote_port(struct device *dev, struct device_attribute *attr, |
| 1302 | const char *buf, size_t count) |
| 1303 | { |
| 1304 | struct nvme_fc_remote_port *remoteport; |
| 1305 | struct fcloop_nport *nport; |
| 1306 | struct fcloop_rport *rport; |
| 1307 | struct nvme_fc_port_info pinfo; |
| 1308 | int ret; |
| 1309 | |
| 1310 | nport = fcloop_alloc_nport(buf, count, true); |
| 1311 | if (!nport) |
| 1312 | return -EIO; |
| 1313 | |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1314 | memset(&pinfo, 0, sizeof(pinfo)); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1315 | pinfo.node_name = nport->node_name; |
| 1316 | pinfo.port_name = nport->port_name; |
| 1317 | pinfo.port_role = nport->port_role; |
| 1318 | pinfo.port_id = nport->port_id; |
| 1319 | |
| 1320 | ret = nvme_fc_register_remoteport(nport->lport->localport, |
| 1321 | &pinfo, &remoteport); |
| 1322 | if (ret || !remoteport) { |
| 1323 | fcloop_nport_put(nport); |
| 1324 | return ret; |
| 1325 | } |
| 1326 | |
| 1327 | /* success */ |
| 1328 | rport = remoteport->private; |
| 1329 | rport->remoteport = remoteport; |
| 1330 | rport->targetport = (nport->tport) ? nport->tport->targetport : NULL; |
| 1331 | if (nport->tport) { |
| 1332 | nport->tport->remoteport = remoteport; |
| 1333 | nport->tport->lport = nport->lport; |
| 1334 | } |
| 1335 | rport->nport = nport; |
| 1336 | rport->lport = nport->lport; |
| 1337 | nport->rport = rport; |
James Smart | 38803fc | 2020-03-18 14:41:12 -0700 | [diff] [blame] | 1338 | spin_lock_init(&rport->lock); |
| 1339 | INIT_WORK(&rport->ls_work, fcloop_rport_lsrqst_work); |
| 1340 | INIT_LIST_HEAD(&rport->ls_list); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1341 | |
Colin Ian King | 7c3a23b | 2016-12-09 14:59:47 +0000 | [diff] [blame] | 1342 | return count; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | |
| 1346 | static struct fcloop_rport * |
| 1347 | __unlink_remote_port(struct fcloop_nport *nport) |
| 1348 | { |
| 1349 | struct fcloop_rport *rport = nport->rport; |
| 1350 | |
| 1351 | if (rport && nport->tport) |
| 1352 | nport->tport->remoteport = NULL; |
| 1353 | nport->rport = NULL; |
| 1354 | |
Daniel Wagner | f644d21 | 2023-12-18 16:30:53 +0100 | [diff] [blame] | 1355 | list_del(&nport->nport_list); |
| 1356 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1357 | return rport; |
| 1358 | } |
| 1359 | |
| 1360 | static int |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1361 | __remoteport_unreg(struct fcloop_nport *nport, struct fcloop_rport *rport) |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1362 | { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1363 | if (!rport) |
| 1364 | return -EALREADY; |
| 1365 | |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1366 | return nvme_fc_unregister_remoteport(rport->remoteport); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | static ssize_t |
| 1370 | fcloop_delete_remote_port(struct device *dev, struct device_attribute *attr, |
| 1371 | const char *buf, size_t count) |
| 1372 | { |
| 1373 | struct fcloop_nport *nport = NULL, *tmpport; |
| 1374 | static struct fcloop_rport *rport; |
| 1375 | u64 nodename, portname; |
| 1376 | unsigned long flags; |
| 1377 | int ret; |
| 1378 | |
| 1379 | ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf); |
| 1380 | if (ret) |
| 1381 | return ret; |
| 1382 | |
| 1383 | spin_lock_irqsave(&fcloop_lock, flags); |
| 1384 | |
| 1385 | list_for_each_entry(tmpport, &fcloop_nports, nport_list) { |
| 1386 | if (tmpport->node_name == nodename && |
| 1387 | tmpport->port_name == portname && tmpport->rport) { |
| 1388 | nport = tmpport; |
| 1389 | rport = __unlink_remote_port(nport); |
| 1390 | break; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | spin_unlock_irqrestore(&fcloop_lock, flags); |
| 1395 | |
| 1396 | if (!nport) |
| 1397 | return -ENOENT; |
| 1398 | |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1399 | ret = __remoteport_unreg(nport, rport); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1400 | |
| 1401 | return ret ? ret : count; |
| 1402 | } |
| 1403 | |
| 1404 | static ssize_t |
| 1405 | fcloop_create_target_port(struct device *dev, struct device_attribute *attr, |
| 1406 | const char *buf, size_t count) |
| 1407 | { |
| 1408 | struct nvmet_fc_target_port *targetport; |
| 1409 | struct fcloop_nport *nport; |
| 1410 | struct fcloop_tport *tport; |
| 1411 | struct nvmet_fc_port_info tinfo; |
| 1412 | int ret; |
| 1413 | |
| 1414 | nport = fcloop_alloc_nport(buf, count, false); |
| 1415 | if (!nport) |
| 1416 | return -EIO; |
| 1417 | |
| 1418 | tinfo.node_name = nport->node_name; |
| 1419 | tinfo.port_name = nport->port_name; |
| 1420 | tinfo.port_id = nport->port_id; |
| 1421 | |
| 1422 | ret = nvmet_fc_register_targetport(&tinfo, &tgttemplate, NULL, |
| 1423 | &targetport); |
| 1424 | if (ret) { |
| 1425 | fcloop_nport_put(nport); |
| 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | /* success */ |
| 1430 | tport = targetport->private; |
| 1431 | tport->targetport = targetport; |
| 1432 | tport->remoteport = (nport->rport) ? nport->rport->remoteport : NULL; |
| 1433 | if (nport->rport) |
| 1434 | nport->rport->targetport = targetport; |
| 1435 | tport->nport = nport; |
| 1436 | tport->lport = nport->lport; |
| 1437 | nport->tport = tport; |
James Smart | 437c0b8 | 2020-03-31 09:50:01 -0700 | [diff] [blame] | 1438 | spin_lock_init(&tport->lock); |
| 1439 | INIT_WORK(&tport->ls_work, fcloop_tport_lsrqst_work); |
| 1440 | INIT_LIST_HEAD(&tport->ls_list); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1441 | |
Colin Ian King | 7c3a23b | 2016-12-09 14:59:47 +0000 | [diff] [blame] | 1442 | return count; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | |
| 1446 | static struct fcloop_tport * |
| 1447 | __unlink_target_port(struct fcloop_nport *nport) |
| 1448 | { |
| 1449 | struct fcloop_tport *tport = nport->tport; |
| 1450 | |
| 1451 | if (tport && nport->rport) |
| 1452 | nport->rport->targetport = NULL; |
| 1453 | nport->tport = NULL; |
| 1454 | |
| 1455 | return tport; |
| 1456 | } |
| 1457 | |
| 1458 | static int |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1459 | __targetport_unreg(struct fcloop_nport *nport, struct fcloop_tport *tport) |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1460 | { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1461 | if (!tport) |
| 1462 | return -EALREADY; |
| 1463 | |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1464 | return nvmet_fc_unregister_targetport(tport->targetport); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | static ssize_t |
| 1468 | fcloop_delete_target_port(struct device *dev, struct device_attribute *attr, |
| 1469 | const char *buf, size_t count) |
| 1470 | { |
| 1471 | struct fcloop_nport *nport = NULL, *tmpport; |
James Smart | 254beb8 | 2017-12-21 14:15:47 -0800 | [diff] [blame] | 1472 | struct fcloop_tport *tport = NULL; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1473 | u64 nodename, portname; |
| 1474 | unsigned long flags; |
| 1475 | int ret; |
| 1476 | |
| 1477 | ret = fcloop_parse_nm_options(dev, &nodename, &portname, buf); |
| 1478 | if (ret) |
| 1479 | return ret; |
| 1480 | |
| 1481 | spin_lock_irqsave(&fcloop_lock, flags); |
| 1482 | |
| 1483 | list_for_each_entry(tmpport, &fcloop_nports, nport_list) { |
| 1484 | if (tmpport->node_name == nodename && |
| 1485 | tmpport->port_name == portname && tmpport->tport) { |
| 1486 | nport = tmpport; |
| 1487 | tport = __unlink_target_port(nport); |
| 1488 | break; |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | spin_unlock_irqrestore(&fcloop_lock, flags); |
| 1493 | |
| 1494 | if (!nport) |
| 1495 | return -ENOENT; |
| 1496 | |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1497 | ret = __targetport_unreg(nport, tport); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1498 | |
| 1499 | return ret ? ret : count; |
| 1500 | } |
| 1501 | |
James Smart | 03d99e5 | 2020-10-16 14:28:38 -0700 | [diff] [blame] | 1502 | static ssize_t |
| 1503 | fcloop_set_cmd_drop(struct device *dev, struct device_attribute *attr, |
| 1504 | const char *buf, size_t count) |
| 1505 | { |
James Smart | 2b54996b7 | 2020-12-07 12:29:40 -0800 | [diff] [blame] | 1506 | unsigned int opcode; |
| 1507 | int starting, amount; |
James Smart | 03d99e5 | 2020-10-16 14:28:38 -0700 | [diff] [blame] | 1508 | |
| 1509 | if (sscanf(buf, "%x:%d:%d", &opcode, &starting, &amount) != 3) |
| 1510 | return -EBADRQC; |
| 1511 | |
| 1512 | drop_current_cnt = 0; |
| 1513 | drop_fabric_opcode = (opcode & ~DROP_OPCODE_MASK) ? true : false; |
| 1514 | drop_opcode = (opcode & DROP_OPCODE_MASK); |
| 1515 | drop_instance = starting; |
| 1516 | /* the check to drop routine uses instance + count to know when |
| 1517 | * to end. Thus, if dropping 1 instance, count should be 0. |
| 1518 | * so subtract 1 from the count. |
| 1519 | */ |
| 1520 | drop_amount = amount - 1; |
| 1521 | |
| 1522 | pr_info("%s: DROP: Starting at instance %d of%s opcode x%x drop +%d " |
| 1523 | "instances\n", |
| 1524 | __func__, drop_instance, drop_fabric_opcode ? " fabric" : "", |
| 1525 | drop_opcode, drop_amount); |
| 1526 | |
| 1527 | return count; |
| 1528 | } |
| 1529 | |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1530 | |
| 1531 | static DEVICE_ATTR(add_local_port, 0200, NULL, fcloop_create_local_port); |
| 1532 | static DEVICE_ATTR(del_local_port, 0200, NULL, fcloop_delete_local_port); |
| 1533 | static DEVICE_ATTR(add_remote_port, 0200, NULL, fcloop_create_remote_port); |
| 1534 | static DEVICE_ATTR(del_remote_port, 0200, NULL, fcloop_delete_remote_port); |
| 1535 | static DEVICE_ATTR(add_target_port, 0200, NULL, fcloop_create_target_port); |
| 1536 | static DEVICE_ATTR(del_target_port, 0200, NULL, fcloop_delete_target_port); |
James Smart | 03d99e5 | 2020-10-16 14:28:38 -0700 | [diff] [blame] | 1537 | static DEVICE_ATTR(set_cmd_drop, 0200, NULL, fcloop_set_cmd_drop); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1538 | |
| 1539 | static struct attribute *fcloop_dev_attrs[] = { |
| 1540 | &dev_attr_add_local_port.attr, |
| 1541 | &dev_attr_del_local_port.attr, |
| 1542 | &dev_attr_add_remote_port.attr, |
| 1543 | &dev_attr_del_remote_port.attr, |
| 1544 | &dev_attr_add_target_port.attr, |
| 1545 | &dev_attr_del_target_port.attr, |
James Smart | 03d99e5 | 2020-10-16 14:28:38 -0700 | [diff] [blame] | 1546 | &dev_attr_set_cmd_drop.attr, |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1547 | NULL |
| 1548 | }; |
| 1549 | |
Rikard Falkeborn | 60b152a | 2021-01-09 00:41:47 +0100 | [diff] [blame] | 1550 | static const struct attribute_group fclopp_dev_attrs_group = { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1551 | .attrs = fcloop_dev_attrs, |
| 1552 | }; |
| 1553 | |
| 1554 | static const struct attribute_group *fcloop_dev_attr_groups[] = { |
| 1555 | &fclopp_dev_attrs_group, |
| 1556 | NULL, |
| 1557 | }; |
| 1558 | |
Ricardo B. Marliere | 800bb2b | 2024-03-05 10:15:58 -0300 | [diff] [blame] | 1559 | static const struct class fcloop_class = { |
| 1560 | .name = "fcloop", |
| 1561 | }; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1562 | static struct device *fcloop_device; |
| 1563 | |
| 1564 | |
| 1565 | static int __init fcloop_init(void) |
| 1566 | { |
| 1567 | int ret; |
| 1568 | |
Ricardo B. Marliere | 800bb2b | 2024-03-05 10:15:58 -0300 | [diff] [blame] | 1569 | ret = class_register(&fcloop_class); |
| 1570 | if (ret) { |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1571 | pr_err("couldn't register class fcloop\n"); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1572 | return ret; |
| 1573 | } |
| 1574 | |
| 1575 | fcloop_device = device_create_with_groups( |
Ricardo B. Marliere | 800bb2b | 2024-03-05 10:15:58 -0300 | [diff] [blame] | 1576 | &fcloop_class, NULL, MKDEV(0, 0), NULL, |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1577 | fcloop_dev_attr_groups, "ctl"); |
| 1578 | if (IS_ERR(fcloop_device)) { |
| 1579 | pr_err("couldn't create ctl device!\n"); |
| 1580 | ret = PTR_ERR(fcloop_device); |
| 1581 | goto out_destroy_class; |
| 1582 | } |
| 1583 | |
| 1584 | get_device(fcloop_device); |
| 1585 | |
| 1586 | return 0; |
| 1587 | |
| 1588 | out_destroy_class: |
Ricardo B. Marliere | 800bb2b | 2024-03-05 10:15:58 -0300 | [diff] [blame] | 1589 | class_unregister(&fcloop_class); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1590 | return ret; |
| 1591 | } |
| 1592 | |
| 1593 | static void __exit fcloop_exit(void) |
| 1594 | { |
James Smart | 2b54996b7 | 2020-12-07 12:29:40 -0800 | [diff] [blame] | 1595 | struct fcloop_lport *lport = NULL; |
| 1596 | struct fcloop_nport *nport = NULL; |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1597 | struct fcloop_tport *tport; |
| 1598 | struct fcloop_rport *rport; |
| 1599 | unsigned long flags; |
| 1600 | int ret; |
| 1601 | |
| 1602 | spin_lock_irqsave(&fcloop_lock, flags); |
| 1603 | |
| 1604 | for (;;) { |
| 1605 | nport = list_first_entry_or_null(&fcloop_nports, |
| 1606 | typeof(*nport), nport_list); |
| 1607 | if (!nport) |
| 1608 | break; |
| 1609 | |
| 1610 | tport = __unlink_target_port(nport); |
| 1611 | rport = __unlink_remote_port(nport); |
| 1612 | |
| 1613 | spin_unlock_irqrestore(&fcloop_lock, flags); |
| 1614 | |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1615 | ret = __targetport_unreg(nport, tport); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1616 | if (ret) |
| 1617 | pr_warn("%s: Failed deleting target port\n", __func__); |
| 1618 | |
James Smart | fddc992 | 2017-09-19 14:01:50 -0700 | [diff] [blame] | 1619 | ret = __remoteport_unreg(nport, rport); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1620 | if (ret) |
| 1621 | pr_warn("%s: Failed deleting remote port\n", __func__); |
| 1622 | |
| 1623 | spin_lock_irqsave(&fcloop_lock, flags); |
| 1624 | } |
| 1625 | |
| 1626 | for (;;) { |
| 1627 | lport = list_first_entry_or_null(&fcloop_lports, |
| 1628 | typeof(*lport), lport_list); |
| 1629 | if (!lport) |
| 1630 | break; |
| 1631 | |
| 1632 | __unlink_local_port(lport); |
| 1633 | |
| 1634 | spin_unlock_irqrestore(&fcloop_lock, flags); |
| 1635 | |
| 1636 | ret = __wait_localport_unreg(lport); |
| 1637 | if (ret) |
| 1638 | pr_warn("%s: Failed deleting local port\n", __func__); |
| 1639 | |
| 1640 | spin_lock_irqsave(&fcloop_lock, flags); |
| 1641 | } |
| 1642 | |
| 1643 | spin_unlock_irqrestore(&fcloop_lock, flags); |
| 1644 | |
| 1645 | put_device(fcloop_device); |
| 1646 | |
Ricardo B. Marliere | 800bb2b | 2024-03-05 10:15:58 -0300 | [diff] [blame] | 1647 | device_destroy(&fcloop_class, MKDEV(0, 0)); |
| 1648 | class_unregister(&fcloop_class); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | module_init(fcloop_init); |
| 1652 | module_exit(fcloop_exit); |
| 1653 | |
Chaitanya Kulkarni | 41951f8 | 2024-01-23 14:13:41 -0800 | [diff] [blame] | 1654 | MODULE_DESCRIPTION("NVMe target FC loop transport driver"); |
James Smart | 475d0fe | 2016-12-02 00:28:44 -0800 | [diff] [blame] | 1655 | MODULE_LICENSE("GPL v2"); |