linux/drivers/scsi/cxlflash/superpipe.c
<<
>>
Prefs
   1/*
   2 * CXL Flash Device Driver
   3 *
   4 * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
   5 *             Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
   6 *
   7 * Copyright (C) 2015 IBM Corporation
   8 *
   9 * This program is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU General Public License
  11 * as published by the Free Software Foundation; either version
  12 * 2 of the License, or (at your option) any later version.
  13 */
  14
  15#include <linux/delay.h>
  16#include <linux/file.h>
  17#include <linux/syscalls.h>
  18#include <misc/cxl.h>
  19#include <asm/unaligned.h>
  20
  21#include <scsi/scsi.h>
  22#include <scsi/scsi_host.h>
  23#include <scsi/scsi_cmnd.h>
  24#include <scsi/scsi_eh.h>
  25#include <uapi/scsi/cxlflash_ioctl.h>
  26
  27#include "sislite.h"
  28#include "common.h"
  29#include "vlun.h"
  30#include "superpipe.h"
  31
  32struct cxlflash_global global;
  33
  34/**
  35 * marshal_rele_to_resize() - translate release to resize structure
  36 * @rele:       Source structure from which to translate/copy.
  37 * @resize:     Destination structure for the translate/copy.
  38 */
  39static void marshal_rele_to_resize(struct dk_cxlflash_release *release,
  40                                   struct dk_cxlflash_resize *resize)
  41{
  42        resize->hdr = release->hdr;
  43        resize->context_id = release->context_id;
  44        resize->rsrc_handle = release->rsrc_handle;
  45}
  46
  47/**
  48 * marshal_det_to_rele() - translate detach to release structure
  49 * @detach:     Destination structure for the translate/copy.
  50 * @rele:       Source structure from which to translate/copy.
  51 */
  52static void marshal_det_to_rele(struct dk_cxlflash_detach *detach,
  53                                struct dk_cxlflash_release *release)
  54{
  55        release->hdr = detach->hdr;
  56        release->context_id = detach->context_id;
  57}
  58
  59/**
  60 * cxlflash_free_errpage() - frees resources associated with global error page
  61 */
  62void cxlflash_free_errpage(void)
  63{
  64
  65        mutex_lock(&global.mutex);
  66        if (global.err_page) {
  67                __free_page(global.err_page);
  68                global.err_page = NULL;
  69        }
  70        mutex_unlock(&global.mutex);
  71}
  72
  73/**
  74 * cxlflash_stop_term_user_contexts() - stops/terminates known user contexts
  75 * @cfg:        Internal structure associated with the host.
  76 *
  77 * When the host needs to go down, all users must be quiesced and their
  78 * memory freed. This is accomplished by putting the contexts in error
  79 * state which will notify the user and let them 'drive' the tear down.
  80 * Meanwhile, this routine camps until all user contexts have been removed.
  81 */
  82void cxlflash_stop_term_user_contexts(struct cxlflash_cfg *cfg)
  83{
  84        struct device *dev = &cfg->dev->dev;
  85        int i, found;
  86
  87        cxlflash_mark_contexts_error(cfg);
  88
  89        while (true) {
  90                found = false;
  91
  92                for (i = 0; i < MAX_CONTEXT; i++)
  93                        if (cfg->ctx_tbl[i]) {
  94                                found = true;
  95                                break;
  96                        }
  97
  98                if (!found && list_empty(&cfg->ctx_err_recovery))
  99                        return;
 100
 101                dev_dbg(dev, "%s: Wait for user contexts to quiesce...\n",
 102                        __func__);
 103                wake_up_all(&cfg->reset_waitq);
 104                ssleep(1);
 105        }
 106}
 107
 108/**
 109 * find_error_context() - locates a context by cookie on the error recovery list
 110 * @cfg:        Internal structure associated with the host.
 111 * @rctxid:     Desired context by id.
 112 * @file:       Desired context by file.
 113 *
 114 * Return: Found context on success, NULL on failure
 115 */
 116static struct ctx_info *find_error_context(struct cxlflash_cfg *cfg, u64 rctxid,
 117                                           struct file *file)
 118{
 119        struct ctx_info *ctxi;
 120
 121        list_for_each_entry(ctxi, &cfg->ctx_err_recovery, list)
 122                if ((ctxi->ctxid == rctxid) || (ctxi->file == file))
 123                        return ctxi;
 124
 125        return NULL;
 126}
 127
 128/**
 129 * get_context() - obtains a validated and locked context reference
 130 * @cfg:        Internal structure associated with the host.
 131 * @rctxid:     Desired context (raw, un-decoded format).
 132 * @arg:        LUN information or file associated with request.
 133 * @ctx_ctrl:   Control information to 'steer' desired lookup.
 134 *
 135 * NOTE: despite the name pid, in linux, current->pid actually refers
 136 * to the lightweight process id (tid) and can change if the process is
 137 * multi threaded. The tgid remains constant for the process and only changes
 138 * when the process of fork. For all intents and purposes, think of tgid
 139 * as a pid in the traditional sense.
 140 *
 141 * Return: Validated context on success, NULL on failure
 142 */
 143struct ctx_info *get_context(struct cxlflash_cfg *cfg, u64 rctxid,
 144                             void *arg, enum ctx_ctrl ctx_ctrl)
 145{
 146        struct device *dev = &cfg->dev->dev;
 147        struct ctx_info *ctxi = NULL;
 148        struct lun_access *lun_access = NULL;
 149        struct file *file = NULL;
 150        struct llun_info *lli = arg;
 151        u64 ctxid = DECODE_CTXID(rctxid);
 152        int rc;
 153        pid_t pid = current->tgid, ctxpid = 0;
 154
 155        if (ctx_ctrl & CTX_CTRL_FILE) {
 156                lli = NULL;
 157                file = (struct file *)arg;
 158        }
 159
 160        if (ctx_ctrl & CTX_CTRL_CLONE)
 161                pid = current->parent->tgid;
 162
 163        if (likely(ctxid < MAX_CONTEXT)) {
 164                while (true) {
 165                        mutex_lock(&cfg->ctx_tbl_list_mutex);
 166                        ctxi = cfg->ctx_tbl[ctxid];
 167                        if (ctxi)
 168                                if ((file && (ctxi->file != file)) ||
 169                                    (!file && (ctxi->ctxid != rctxid)))
 170                                        ctxi = NULL;
 171
 172                        if ((ctx_ctrl & CTX_CTRL_ERR) ||
 173                            (!ctxi && (ctx_ctrl & CTX_CTRL_ERR_FALLBACK)))
 174                                ctxi = find_error_context(cfg, rctxid, file);
 175                        if (!ctxi) {
 176                                mutex_unlock(&cfg->ctx_tbl_list_mutex);
 177                                goto out;
 178                        }
 179
 180                        /*
 181                         * Need to acquire ownership of the context while still
 182                         * under the table/list lock to serialize with a remove
 183                         * thread. Use the 'try' to avoid stalling the
 184                         * table/list lock for a single context.
 185                         *
 186                         * Note that the lock order is:
 187                         *
 188                         *      cfg->ctx_tbl_list_mutex -> ctxi->mutex
 189                         *
 190                         * Therefore release ctx_tbl_list_mutex before retrying.
 191                         */
 192                        rc = mutex_trylock(&ctxi->mutex);
 193                        mutex_unlock(&cfg->ctx_tbl_list_mutex);
 194                        if (rc)
 195                                break; /* got the context's lock! */
 196                }
 197
 198                if (ctxi->unavail)
 199                        goto denied;
 200
 201                ctxpid = ctxi->pid;
 202                if (likely(!(ctx_ctrl & CTX_CTRL_NOPID)))
 203                        if (pid != ctxpid)
 204                                goto denied;
 205
 206                if (lli) {
 207                        list_for_each_entry(lun_access, &ctxi->luns, list)
 208                                if (lun_access->lli == lli)
 209                                        goto out;
 210                        goto denied;
 211                }
 212        }
 213
 214out:
 215        dev_dbg(dev, "%s: rctxid=%016llx ctxinfo=%p ctxpid=%u pid=%u "
 216                "ctx_ctrl=%u\n", __func__, rctxid, ctxi, ctxpid, pid,
 217                ctx_ctrl);
 218
 219        return ctxi;
 220
 221denied:
 222        mutex_unlock(&ctxi->mutex);
 223        ctxi = NULL;
 224        goto out;
 225}
 226
 227/**
 228 * put_context() - release a context that was retrieved from get_context()
 229 * @ctxi:       Context to release.
 230 *
 231 * For now, releasing the context equates to unlocking it's mutex.
 232 */
 233void put_context(struct ctx_info *ctxi)
 234{
 235        mutex_unlock(&ctxi->mutex);
 236}
 237
 238/**
 239 * afu_attach() - attach a context to the AFU
 240 * @cfg:        Internal structure associated with the host.
 241 * @ctxi:       Context to attach.
 242 *
 243 * Upon setting the context capabilities, they must be confirmed with
 244 * a read back operation as the context might have been closed since
 245 * the mailbox was unlocked. When this occurs, registration is failed.
 246 *
 247 * Return: 0 on success, -errno on failure
 248 */
 249static int afu_attach(struct cxlflash_cfg *cfg, struct ctx_info *ctxi)
 250{
 251        struct device *dev = &cfg->dev->dev;
 252        struct afu *afu = cfg->afu;
 253        struct sisl_ctrl_map __iomem *ctrl_map = ctxi->ctrl_map;
 254        int rc = 0;
 255        u64 val;
 256
 257        /* Unlock cap and restrict user to read/write cmds in translated mode */
 258        readq_be(&ctrl_map->mbox_r);
 259        val = (SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD);
 260        writeq_be(val, &ctrl_map->ctx_cap);
 261        val = readq_be(&ctrl_map->ctx_cap);
 262        if (val != (SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD)) {
 263                dev_err(dev, "%s: ctx may be closed val=%016llx\n",
 264                        __func__, val);
 265                rc = -EAGAIN;
 266                goto out;
 267        }
 268
 269        /* Set up MMIO registers pointing to the RHT */
 270        writeq_be((u64)ctxi->rht_start, &ctrl_map->rht_start);
 271        val = SISL_RHT_CNT_ID((u64)MAX_RHT_PER_CONTEXT, (u64)(afu->ctx_hndl));
 272        writeq_be(val, &ctrl_map->rht_cnt_id);
 273out:
 274        dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
 275        return rc;
 276}
 277
 278/**
 279 * read_cap16() - issues a SCSI READ_CAP16 command
 280 * @sdev:       SCSI device associated with LUN.
 281 * @lli:        LUN destined for capacity request.
 282 *
 283 * The READ_CAP16 can take quite a while to complete. Should an EEH occur while
 284 * in scsi_execute(), the EEH handler will attempt to recover. As part of the
 285 * recovery, the handler drains all currently running ioctls, waiting until they
 286 * have completed before proceeding with a reset. As this routine is used on the
 287 * ioctl path, this can create a condition where the EEH handler becomes stuck,
 288 * infinitely waiting for this ioctl thread. To avoid this behavior, temporarily
 289 * unmark this thread as an ioctl thread by releasing the ioctl read semaphore.
 290 * This will allow the EEH handler to proceed with a recovery while this thread
 291 * is still running. Once the scsi_execute() returns, reacquire the ioctl read
 292 * semaphore and check the adapter state in case it changed while inside of
 293 * scsi_execute(). The state check will wait if the adapter is still being
 294 * recovered or return a failure if the recovery failed. In the event that the
 295 * adapter reset failed, simply return the failure as the ioctl would be unable
 296 * to continue.
 297 *
 298 * Note that the above puts a requirement on this routine to only be called on
 299 * an ioctl thread.
 300 *
 301 * Return: 0 on success, -errno on failure
 302 */
 303static int read_cap16(struct scsi_device *sdev, struct llun_info *lli)
 304{
 305        struct cxlflash_cfg *cfg = shost_priv(sdev->host);
 306        struct device *dev = &cfg->dev->dev;
 307        struct glun_info *gli = lli->parent;
 308        struct scsi_sense_hdr sshdr;
 309        u8 *cmd_buf = NULL;
 310        u8 *scsi_cmd = NULL;
 311        u8 *sense_buf = NULL;
 312        int rc = 0;
 313        int result = 0;
 314        int retry_cnt = 0;
 315        u32 to = CMD_TIMEOUT * HZ;
 316
 317retry:
 318        cmd_buf = kzalloc(CMD_BUFSIZE, GFP_KERNEL);
 319        scsi_cmd = kzalloc(MAX_COMMAND_SIZE, GFP_KERNEL);
 320        sense_buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
 321        if (unlikely(!cmd_buf || !scsi_cmd || !sense_buf)) {
 322                rc = -ENOMEM;
 323                goto out;
 324        }
 325
 326        scsi_cmd[0] = SERVICE_ACTION_IN_16;     /* read cap(16) */
 327        scsi_cmd[1] = SAI_READ_CAPACITY_16;     /* service action */
 328        put_unaligned_be32(CMD_BUFSIZE, &scsi_cmd[10]);
 329
 330        dev_dbg(dev, "%s: %ssending cmd(%02x)\n", __func__,
 331                retry_cnt ? "re" : "", scsi_cmd[0]);
 332
 333        /* Drop the ioctl read semahpore across lengthy call */
 334        up_read(&cfg->ioctl_rwsem);
 335        result = scsi_execute(sdev, scsi_cmd, DMA_FROM_DEVICE, cmd_buf,
 336                              CMD_BUFSIZE, sense_buf, &sshdr, to, CMD_RETRIES,
 337                              0, 0, NULL);
 338        down_read(&cfg->ioctl_rwsem);
 339        rc = check_state(cfg);
 340        if (rc) {
 341                dev_err(dev, "%s: Failed state result=%08x\n",
 342                        __func__, result);
 343                rc = -ENODEV;
 344                goto out;
 345        }
 346
 347        if (driver_byte(result) == DRIVER_SENSE) {
 348                result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
 349                if (result & SAM_STAT_CHECK_CONDITION) {
 350                        switch (sshdr.sense_key) {
 351                        case NO_SENSE:
 352                        case RECOVERED_ERROR:
 353                                /* fall through */
 354                        case NOT_READY:
 355                                result &= ~SAM_STAT_CHECK_CONDITION;
 356                                break;
 357                        case UNIT_ATTENTION:
 358                                switch (sshdr.asc) {
 359                                case 0x29: /* Power on Reset or Device Reset */
 360                                        /* fall through */
 361                                case 0x2A: /* Device capacity changed */
 362                                case 0x3F: /* Report LUNs changed */
 363                                        /* Retry the command once more */
 364                                        if (retry_cnt++ < 1) {
 365                                                kfree(cmd_buf);
 366                                                kfree(scsi_cmd);
 367                                                kfree(sense_buf);
 368                                                goto retry;
 369                                        }
 370                                }
 371                                break;
 372                        default:
 373                                break;
 374                        }
 375                }
 376        }
 377
 378        if (result) {
 379                dev_err(dev, "%s: command failed, result=%08x\n",
 380                        __func__, result);
 381                rc = -EIO;
 382                goto out;
 383        }
 384
 385        /*
 386         * Read cap was successful, grab values from the buffer;
 387         * note that we don't need to worry about unaligned access
 388         * as the buffer is allocated on an aligned boundary.
 389         */
 390        mutex_lock(&gli->mutex);
 391        gli->max_lba = be64_to_cpu(*((__be64 *)&cmd_buf[0]));
 392        gli->blk_len = be32_to_cpu(*((__be32 *)&cmd_buf[8]));
 393        mutex_unlock(&gli->mutex);
 394
 395out:
 396        kfree(cmd_buf);
 397        kfree(scsi_cmd);
 398        kfree(sense_buf);
 399
 400        dev_dbg(dev, "%s: maxlba=%lld blklen=%d rc=%d\n",
 401                __func__, gli->max_lba, gli->blk_len, rc);
 402        return rc;
 403}
 404
 405/**
 406 * get_rhte() - obtains validated resource handle table entry reference
 407 * @ctxi:       Context owning the resource handle.
 408 * @rhndl:      Resource handle associated with entry.
 409 * @lli:        LUN associated with request.
 410 *
 411 * Return: Validated RHTE on success, NULL on failure
 412 */
 413struct sisl_rht_entry *get_rhte(struct ctx_info *ctxi, res_hndl_t rhndl,
 414                                struct llun_info *lli)
 415{
 416        struct cxlflash_cfg *cfg = ctxi->cfg;
 417        struct device *dev = &cfg->dev->dev;
 418        struct sisl_rht_entry *rhte = NULL;
 419
 420        if (unlikely(!ctxi->rht_start)) {
 421                dev_dbg(dev, "%s: Context does not have allocated RHT\n",
 422                         __func__);
 423                goto out;
 424        }
 425
 426        if (unlikely(rhndl >= MAX_RHT_PER_CONTEXT)) {
 427                dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
 428                        __func__, rhndl);
 429                goto out;
 430        }
 431
 432        if (unlikely(ctxi->rht_lun[rhndl] != lli)) {
 433                dev_dbg(dev, "%s: Bad resource handle LUN rhndl=%d\n",
 434                        __func__, rhndl);
 435                goto out;
 436        }
 437
 438        rhte = &ctxi->rht_start[rhndl];
 439        if (unlikely(rhte->nmask == 0)) {
 440                dev_dbg(dev, "%s: Unopened resource handle rhndl=%d\n",
 441                        __func__, rhndl);
 442                rhte = NULL;
 443                goto out;
 444        }
 445
 446out:
 447        return rhte;
 448}
 449
 450/**
 451 * rhte_checkout() - obtains free/empty resource handle table entry
 452 * @ctxi:       Context owning the resource handle.
 453 * @lli:        LUN associated with request.
 454 *
 455 * Return: Free RHTE on success, NULL on failure
 456 */
 457struct sisl_rht_entry *rhte_checkout(struct ctx_info *ctxi,
 458                                     struct llun_info *lli)
 459{
 460        struct cxlflash_cfg *cfg = ctxi->cfg;
 461        struct device *dev = &cfg->dev->dev;
 462        struct sisl_rht_entry *rhte = NULL;
 463        int i;
 464
 465        /* Find a free RHT entry */
 466        for (i = 0; i < MAX_RHT_PER_CONTEXT; i++)
 467                if (ctxi->rht_start[i].nmask == 0) {
 468                        rhte = &ctxi->rht_start[i];
 469                        ctxi->rht_out++;
 470                        break;
 471                }
 472
 473        if (likely(rhte))
 474                ctxi->rht_lun[i] = lli;
 475
 476        dev_dbg(dev, "%s: returning rhte=%p index=%d\n", __func__, rhte, i);
 477        return rhte;
 478}
 479
 480/**
 481 * rhte_checkin() - releases a resource handle table entry
 482 * @ctxi:       Context owning the resource handle.
 483 * @rhte:       RHTE to release.
 484 */
 485void rhte_checkin(struct ctx_info *ctxi,
 486                  struct sisl_rht_entry *rhte)
 487{
 488        u32 rsrc_handle = rhte - ctxi->rht_start;
 489
 490        rhte->nmask = 0;
 491        rhte->fp = 0;
 492        ctxi->rht_out--;
 493        ctxi->rht_lun[rsrc_handle] = NULL;
 494        ctxi->rht_needs_ws[rsrc_handle] = false;
 495}
 496
 497/**
 498 * rhte_format1() - populates a RHTE for format 1
 499 * @rhte:       RHTE to populate.
 500 * @lun_id:     LUN ID of LUN associated with RHTE.
 501 * @perm:       Desired permissions for RHTE.
 502 * @port_sel:   Port selection mask
 503 */
 504static void rht_format1(struct sisl_rht_entry *rhte, u64 lun_id, u32 perm,
 505                        u32 port_sel)
 506{
 507        /*
 508         * Populate the Format 1 RHT entry for direct access (physical
 509         * LUN) using the synchronization sequence defined in the
 510         * SISLite specification.
 511         */
 512        struct sisl_rht_entry_f1 dummy = { 0 };
 513        struct sisl_rht_entry_f1 *rhte_f1 = (struct sisl_rht_entry_f1 *)rhte;
 514
 515        memset(rhte_f1, 0, sizeof(*rhte_f1));
 516        rhte_f1->fp = SISL_RHT_FP(1U, 0);
 517        dma_wmb(); /* Make setting of format bit visible */
 518
 519        rhte_f1->lun_id = lun_id;
 520        dma_wmb(); /* Make setting of LUN id visible */
 521
 522        /*
 523         * Use a dummy RHT Format 1 entry to build the second dword
 524         * of the entry that must be populated in a single write when
 525         * enabled (valid bit set to TRUE).
 526         */
 527        dummy.valid = 0x80;
 528        dummy.fp = SISL_RHT_FP(1U, perm);
 529        dummy.port_sel = port_sel;
 530        rhte_f1->dw = dummy.dw;
 531
 532        dma_wmb(); /* Make remaining RHT entry fields visible */
 533}
 534
 535/**
 536 * cxlflash_lun_attach() - attaches a user to a LUN and manages the LUN's mode
 537 * @gli:        LUN to attach.
 538 * @mode:       Desired mode of the LUN.
 539 * @locked:     Mutex status on current thread.
 540 *
 541 * Return: 0 on success, -errno on failure
 542 */
 543int cxlflash_lun_attach(struct glun_info *gli, enum lun_mode mode, bool locked)
 544{
 545        int rc = 0;
 546
 547        if (!locked)
 548                mutex_lock(&gli->mutex);
 549
 550        if (gli->mode == MODE_NONE)
 551                gli->mode = mode;
 552        else if (gli->mode != mode) {
 553                pr_debug("%s: gli_mode=%d requested_mode=%d\n",
 554                         __func__, gli->mode, mode);
 555                rc = -EINVAL;
 556                goto out;
 557        }
 558
 559        gli->users++;
 560        WARN_ON(gli->users <= 0);
 561out:
 562        pr_debug("%s: Returning rc=%d gli->mode=%u gli->users=%u\n",
 563                 __func__, rc, gli->mode, gli->users);
 564        if (!locked)
 565                mutex_unlock(&gli->mutex);
 566        return rc;
 567}
 568
 569/**
 570 * cxlflash_lun_detach() - detaches a user from a LUN and resets the LUN's mode
 571 * @gli:        LUN to detach.
 572 *
 573 * When resetting the mode, terminate block allocation resources as they
 574 * are no longer required (service is safe to call even when block allocation
 575 * resources were not present - such as when transitioning from physical mode).
 576 * These resources will be reallocated when needed (subsequent transition to
 577 * virtual mode).
 578 */
 579void cxlflash_lun_detach(struct glun_info *gli)
 580{
 581        mutex_lock(&gli->mutex);
 582        WARN_ON(gli->mode == MODE_NONE);
 583        if (--gli->users == 0) {
 584                gli->mode = MODE_NONE;
 585                cxlflash_ba_terminate(&gli->blka.ba_lun);
 586        }
 587        pr_debug("%s: gli->users=%u\n", __func__, gli->users);
 588        WARN_ON(gli->users < 0);
 589        mutex_unlock(&gli->mutex);
 590}
 591
 592/**
 593 * _cxlflash_disk_release() - releases the specified resource entry
 594 * @sdev:       SCSI device associated with LUN.
 595 * @ctxi:       Context owning resources.
 596 * @release:    Release ioctl data structure.
 597 *
 598 * For LUNs in virtual mode, the virtual LUN associated with the specified
 599 * resource handle is resized to 0 prior to releasing the RHTE. Note that the
 600 * AFU sync should _not_ be performed when the context is sitting on the error
 601 * recovery list. A context on the error recovery list is not known to the AFU
 602 * due to reset. When the context is recovered, it will be reattached and made
 603 * known again to the AFU.
 604 *
 605 * Return: 0 on success, -errno on failure
 606 */
 607int _cxlflash_disk_release(struct scsi_device *sdev,
 608                           struct ctx_info *ctxi,
 609                           struct dk_cxlflash_release *release)
 610{
 611        struct cxlflash_cfg *cfg = shost_priv(sdev->host);
 612        struct device *dev = &cfg->dev->dev;
 613        struct llun_info *lli = sdev->hostdata;
 614        struct glun_info *gli = lli->parent;
 615        struct afu *afu = cfg->afu;
 616        bool put_ctx = false;
 617
 618        struct dk_cxlflash_resize size;
 619        res_hndl_t rhndl = release->rsrc_handle;
 620
 621        int rc = 0;
 622        u64 ctxid = DECODE_CTXID(release->context_id),
 623            rctxid = release->context_id;
 624
 625        struct sisl_rht_entry *rhte;
 626        struct sisl_rht_entry_f1 *rhte_f1;
 627
 628        dev_dbg(dev, "%s: ctxid=%llu rhndl=%llu gli->mode=%u gli->users=%u\n",
 629                __func__, ctxid, release->rsrc_handle, gli->mode, gli->users);
 630
 631        if (!ctxi) {
 632                ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
 633                if (unlikely(!ctxi)) {
 634                        dev_dbg(dev, "%s: Bad context ctxid=%llu\n",
 635                                __func__, ctxid);
 636                        rc = -EINVAL;
 637                        goto out;
 638                }
 639
 640                put_ctx = true;
 641        }
 642
 643        rhte = get_rhte(ctxi, rhndl, lli);
 644        if (unlikely(!rhte)) {
 645                dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
 646                        __func__, rhndl);
 647                rc = -EINVAL;
 648                goto out;
 649        }
 650
 651        /*
 652         * Resize to 0 for virtual LUNS by setting the size
 653         * to 0. This will clear LXT_START and LXT_CNT fields
 654         * in the RHT entry and properly sync with the AFU.
 655         *
 656         * Afterwards we clear the remaining fields.
 657         */
 658        switch (gli->mode) {
 659        case MODE_VIRTUAL:
 660                marshal_rele_to_resize(release, &size);
 661                size.req_size = 0;
 662                rc = _cxlflash_vlun_resize(sdev, ctxi, &size);
 663                if (rc) {
 664                        dev_dbg(dev, "%s: resize failed rc %d\n", __func__, rc);
 665                        goto out;
 666                }
 667
 668                break;
 669        case MODE_PHYSICAL:
 670                /*
 671                 * Clear the Format 1 RHT entry for direct access
 672                 * (physical LUN) using the synchronization sequence
 673                 * defined in the SISLite specification.
 674                 */
 675                rhte_f1 = (struct sisl_rht_entry_f1 *)rhte;
 676
 677                rhte_f1->valid = 0;
 678                dma_wmb(); /* Make revocation of RHT entry visible */
 679
 680                rhte_f1->lun_id = 0;
 681                dma_wmb(); /* Make clearing of LUN id visible */
 682
 683                rhte_f1->dw = 0;
 684                dma_wmb(); /* Make RHT entry bottom-half clearing visible */
 685
 686                if (!ctxi->err_recovery_active)
 687                        cxlflash_afu_sync(afu, ctxid, rhndl, AFU_HW_SYNC);
 688                break;
 689        default:
 690                WARN(1, "Unsupported LUN mode!");
 691                goto out;
 692        }
 693
 694        rhte_checkin(ctxi, rhte);
 695        cxlflash_lun_detach(gli);
 696
 697out:
 698        if (put_ctx)
 699                put_context(ctxi);
 700        dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
 701        return rc;
 702}
 703
 704int cxlflash_disk_release(struct scsi_device *sdev,
 705                          struct dk_cxlflash_release *release)
 706{
 707        return _cxlflash_disk_release(sdev, NULL, release);
 708}
 709
 710/**
 711 * destroy_context() - releases a context
 712 * @cfg:        Internal structure associated with the host.
 713 * @ctxi:       Context to release.
 714 *
 715 * This routine is safe to be called with a a non-initialized context.
 716 * Also note that the routine conditionally checks for the existence
 717 * of the context control map before clearing the RHT registers and
 718 * context capabilities because it is possible to destroy a context
 719 * while the context is in the error state (previous mapping was
 720 * removed [so there is no need to worry about clearing] and context
 721 * is waiting for a new mapping).
 722 */
 723static void destroy_context(struct cxlflash_cfg *cfg,
 724                            struct ctx_info *ctxi)
 725{
 726        struct afu *afu = cfg->afu;
 727
 728        if (ctxi->initialized) {
 729                WARN_ON(!list_empty(&ctxi->luns));
 730
 731                /* Clear RHT registers and drop all capabilities for context */
 732                if (afu->afu_map && ctxi->ctrl_map) {
 733                        writeq_be(0, &ctxi->ctrl_map->rht_start);
 734                        writeq_be(0, &ctxi->ctrl_map->rht_cnt_id);
 735                        writeq_be(0, &ctxi->ctrl_map->ctx_cap);
 736                }
 737        }
 738
 739        /* Free memory associated with context */
 740        free_page((ulong)ctxi->rht_start);
 741        kfree(ctxi->rht_needs_ws);
 742        kfree(ctxi->rht_lun);
 743        kfree(ctxi);
 744}
 745
 746/**
 747 * create_context() - allocates and initializes a context
 748 * @cfg:        Internal structure associated with the host.
 749 *
 750 * Return: Allocated context on success, NULL on failure
 751 */
 752static struct ctx_info *create_context(struct cxlflash_cfg *cfg)
 753{
 754        struct device *dev = &cfg->dev->dev;
 755        struct ctx_info *ctxi = NULL;
 756        struct llun_info **lli = NULL;
 757        u8 *ws = NULL;
 758        struct sisl_rht_entry *rhte;
 759
 760        ctxi = kzalloc(sizeof(*ctxi), GFP_KERNEL);
 761        lli = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*lli)), GFP_KERNEL);
 762        ws = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*ws)), GFP_KERNEL);
 763        if (unlikely(!ctxi || !lli || !ws)) {
 764                dev_err(dev, "%s: Unable to allocate context\n", __func__);
 765                goto err;
 766        }
 767
 768        rhte = (struct sisl_rht_entry *)get_zeroed_page(GFP_KERNEL);
 769        if (unlikely(!rhte)) {
 770                dev_err(dev, "%s: Unable to allocate RHT\n", __func__);
 771                goto err;
 772        }
 773
 774        ctxi->rht_lun = lli;
 775        ctxi->rht_needs_ws = ws;
 776        ctxi->rht_start = rhte;
 777out:
 778        return ctxi;
 779
 780err:
 781        kfree(ws);
 782        kfree(lli);
 783        kfree(ctxi);
 784        ctxi = NULL;
 785        goto out;
 786}
 787
 788/**
 789 * init_context() - initializes a previously allocated context
 790 * @ctxi:       Previously allocated context
 791 * @cfg:        Internal structure associated with the host.
 792 * @ctx:        Previously obtained CXL context reference.
 793 * @ctxid:      Previously obtained process element associated with CXL context.
 794 * @file:       Previously obtained file associated with CXL context.
 795 * @perms:      User-specified permissions.
 796 */
 797static void init_context(struct ctx_info *ctxi, struct cxlflash_cfg *cfg,
 798                         struct cxl_context *ctx, int ctxid, struct file *file,
 799                         u32 perms)
 800{
 801        struct afu *afu = cfg->afu;
 802
 803        ctxi->rht_perms = perms;
 804        ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
 805        ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
 806        ctxi->pid = current->tgid; /* tgid = pid */
 807        ctxi->ctx = ctx;
 808        ctxi->cfg = cfg;
 809        ctxi->file = file;
 810        ctxi->initialized = true;
 811        mutex_init(&ctxi->mutex);
 812        kref_init(&ctxi->kref);
 813        INIT_LIST_HEAD(&ctxi->luns);
 814        INIT_LIST_HEAD(&ctxi->list); /* initialize for list_empty() */
 815}
 816
 817/**
 818 * remove_context() - context kref release handler
 819 * @kref:       Kernel reference associated with context to be removed.
 820 *
 821 * When a context no longer has any references it can safely be removed
 822 * from global access and destroyed. Note that it is assumed the thread
 823 * relinquishing access to the context holds its mutex.
 824 */
 825static void remove_context(struct kref *kref)
 826{
 827        struct ctx_info *ctxi = container_of(kref, struct ctx_info, kref);
 828        struct cxlflash_cfg *cfg = ctxi->cfg;
 829        u64 ctxid = DECODE_CTXID(ctxi->ctxid);
 830
 831        /* Remove context from table/error list */
 832        WARN_ON(!mutex_is_locked(&ctxi->mutex));
 833        ctxi->unavail = true;
 834        mutex_unlock(&ctxi->mutex);
 835        mutex_lock(&cfg->ctx_tbl_list_mutex);
 836        mutex_lock(&ctxi->mutex);
 837
 838        if (!list_empty(&ctxi->list))
 839                list_del(&ctxi->list);
 840        cfg->ctx_tbl[ctxid] = NULL;
 841        mutex_unlock(&cfg->ctx_tbl_list_mutex);
 842        mutex_unlock(&ctxi->mutex);
 843
 844        /* Context now completely uncoupled/unreachable */
 845        destroy_context(cfg, ctxi);
 846}
 847
 848/**
 849 * _cxlflash_disk_detach() - detaches a LUN from a context
 850 * @sdev:       SCSI device associated with LUN.
 851 * @ctxi:       Context owning resources.
 852 * @detach:     Detach ioctl data structure.
 853 *
 854 * As part of the detach, all per-context resources associated with the LUN
 855 * are cleaned up. When detaching the last LUN for a context, the context
 856 * itself is cleaned up and released.
 857 *
 858 * Return: 0 on success, -errno on failure
 859 */
 860static int _cxlflash_disk_detach(struct scsi_device *sdev,
 861                                 struct ctx_info *ctxi,
 862                                 struct dk_cxlflash_detach *detach)
 863{
 864        struct cxlflash_cfg *cfg = shost_priv(sdev->host);
 865        struct device *dev = &cfg->dev->dev;
 866        struct llun_info *lli = sdev->hostdata;
 867        struct lun_access *lun_access, *t;
 868        struct dk_cxlflash_release rel;
 869        bool put_ctx = false;
 870
 871        int i;
 872        int rc = 0;
 873        u64 ctxid = DECODE_CTXID(detach->context_id),
 874            rctxid = detach->context_id;
 875
 876        dev_dbg(dev, "%s: ctxid=%llu\n", __func__, ctxid);
 877
 878        if (!ctxi) {
 879                ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
 880                if (unlikely(!ctxi)) {
 881                        dev_dbg(dev, "%s: Bad context ctxid=%llu\n",
 882                                __func__, ctxid);
 883                        rc = -EINVAL;
 884                        goto out;
 885                }
 886
 887                put_ctx = true;
 888        }
 889
 890        /* Cleanup outstanding resources tied to this LUN */
 891        if (ctxi->rht_out) {
 892                marshal_det_to_rele(detach, &rel);
 893                for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) {
 894                        if (ctxi->rht_lun[i] == lli) {
 895                                rel.rsrc_handle = i;
 896                                _cxlflash_disk_release(sdev, ctxi, &rel);
 897                        }
 898
 899                        /* No need to loop further if we're done */
 900                        if (ctxi->rht_out == 0)
 901                                break;
 902                }
 903        }
 904
 905        /* Take our LUN out of context, free the node */
 906        list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
 907                if (lun_access->lli == lli) {
 908                        list_del(&lun_access->list);
 909                        kfree(lun_access);
 910                        lun_access = NULL;
 911                        break;
 912                }
 913
 914        /*
 915         * Release the context reference and the sdev reference that
 916         * bound this LUN to the context.
 917         */
 918        if (kref_put(&ctxi->kref, remove_context))
 919                put_ctx = false;
 920        scsi_device_put(sdev);
 921out:
 922        if (put_ctx)
 923                put_context(ctxi);
 924        dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
 925        return rc;
 926}
 927
 928static int cxlflash_disk_detach(struct scsi_device *sdev,
 929                                struct dk_cxlflash_detach *detach)
 930{
 931        return _cxlflash_disk_detach(sdev, NULL, detach);
 932}
 933
 934/**
 935 * cxlflash_cxl_release() - release handler for adapter file descriptor
 936 * @inode:      File-system inode associated with fd.
 937 * @file:       File installed with adapter file descriptor.
 938 *
 939 * This routine is the release handler for the fops registered with
 940 * the CXL services on an initial attach for a context. It is called
 941 * when a close (explicity by the user or as part of a process tear
 942 * down) is performed on the adapter file descriptor returned to the
 943 * user. The user should be aware that explicitly performing a close
 944 * considered catastrophic and subsequent usage of the superpipe API
 945 * with previously saved off tokens will fail.
 946 *
 947 * This routine derives the context reference and calls detach for
 948 * each LUN associated with the context.The final detach operation
 949 * causes the context itself to be freed. With exception to when the
 950 * CXL process element (context id) lookup fails (a case that should
 951 * theoretically never occur), every call into this routine results
 952 * in a complete freeing of a context.
 953 *
 954 * Return: 0 on success
 955 */
 956static int cxlflash_cxl_release(struct inode *inode, struct file *file)
 957{
 958        struct cxl_context *ctx = cxl_fops_get_context(file);
 959        struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
 960                                                cxl_fops);
 961        struct device *dev = &cfg->dev->dev;
 962        struct ctx_info *ctxi = NULL;
 963        struct dk_cxlflash_detach detach = { { 0 }, 0 };
 964        struct lun_access *lun_access, *t;
 965        enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
 966        int ctxid;
 967
 968        ctxid = cxl_process_element(ctx);
 969        if (unlikely(ctxid < 0)) {
 970                dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
 971                        __func__, ctx, ctxid);
 972                goto out;
 973        }
 974
 975        ctxi = get_context(cfg, ctxid, file, ctrl);
 976        if (unlikely(!ctxi)) {
 977                ctxi = get_context(cfg, ctxid, file, ctrl | CTX_CTRL_CLONE);
 978                if (!ctxi) {
 979                        dev_dbg(dev, "%s: ctxid=%d already free\n",
 980                                __func__, ctxid);
 981                        goto out_release;
 982                }
 983
 984                dev_dbg(dev, "%s: Another process owns ctxid=%d\n",
 985                        __func__, ctxid);
 986                put_context(ctxi);
 987                goto out;
 988        }
 989
 990        dev_dbg(dev, "%s: close for ctxid=%d\n", __func__, ctxid);
 991
 992        detach.context_id = ctxi->ctxid;
 993        list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
 994                _cxlflash_disk_detach(lun_access->sdev, ctxi, &detach);
 995out_release:
 996        cxl_fd_release(inode, file);
 997out:
 998        dev_dbg(dev, "%s: returning\n", __func__);
 999        return 0;
1000}
1001
1002/**
1003 * unmap_context() - clears a previously established mapping
1004 * @ctxi:       Context owning the mapping.
1005 *
1006 * This routine is used to switch between the error notification page
1007 * (dummy page of all 1's) and the real mapping (established by the CXL
1008 * fault handler).
1009 */
1010static void unmap_context(struct ctx_info *ctxi)
1011{
1012        unmap_mapping_range(ctxi->file->f_mapping, 0, 0, 1);
1013}
1014
1015/**
1016 * get_err_page() - obtains and allocates the error notification page
1017 * @cfg:        Internal structure associated with the host.
1018 *
1019 * Return: error notification page on success, NULL on failure
1020 */
1021static struct page *get_err_page(struct cxlflash_cfg *cfg)
1022{
1023        struct page *err_page = global.err_page;
1024        struct device *dev = &cfg->dev->dev;
1025
1026        if (unlikely(!err_page)) {
1027                err_page = alloc_page(GFP_KERNEL);
1028                if (unlikely(!err_page)) {
1029                        dev_err(dev, "%s: Unable to allocate err_page\n",
1030                                __func__);
1031                        goto out;
1032                }
1033
1034                memset(page_address(err_page), -1, PAGE_SIZE);
1035
1036                /* Serialize update w/ other threads to avoid a leak */
1037                mutex_lock(&global.mutex);
1038                if (likely(!global.err_page))
1039                        global.err_page = err_page;
1040                else {
1041                        __free_page(err_page);
1042                        err_page = global.err_page;
1043                }
1044                mutex_unlock(&global.mutex);
1045        }
1046
1047out:
1048        dev_dbg(dev, "%s: returning err_page=%p\n", __func__, err_page);
1049        return err_page;
1050}
1051
1052/**
1053 * cxlflash_mmap_fault() - mmap fault handler for adapter file descriptor
1054 * @vmf:        VM fault associated with current fault.
1055 *
1056 * To support error notification via MMIO, faults are 'caught' by this routine
1057 * that was inserted before passing back the adapter file descriptor on attach.
1058 * When a fault occurs, this routine evaluates if error recovery is active and
1059 * if so, installs the error page to 'notify' the user about the error state.
1060 * During normal operation, the fault is simply handled by the original fault
1061 * handler that was installed by CXL services as part of initializing the
1062 * adapter file descriptor. The VMA's page protection bits are toggled to
1063 * indicate cached/not-cached depending on the memory backing the fault.
1064 *
1065 * Return: 0 on success, VM_FAULT_SIGBUS on failure
1066 */
1067static int cxlflash_mmap_fault(struct vm_fault *vmf)
1068{
1069        struct vm_area_struct *vma = vmf->vma;
1070        struct file *file = vma->vm_file;
1071        struct cxl_context *ctx = cxl_fops_get_context(file);
1072        struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
1073                                                cxl_fops);
1074        struct device *dev = &cfg->dev->dev;
1075        struct ctx_info *ctxi = NULL;
1076        struct page *err_page = NULL;
1077        enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
1078        int rc = 0;
1079        int ctxid;
1080
1081        ctxid = cxl_process_element(ctx);
1082        if (unlikely(ctxid < 0)) {
1083                dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
1084                        __func__, ctx, ctxid);
1085                goto err;
1086        }
1087
1088        ctxi = get_context(cfg, ctxid, file, ctrl);
1089        if (unlikely(!ctxi)) {
1090                dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid);
1091                goto err;
1092        }
1093
1094        dev_dbg(dev, "%s: fault for context %d\n", __func__, ctxid);
1095
1096        if (likely(!ctxi->err_recovery_active)) {
1097                vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1098                rc = ctxi->cxl_mmap_vmops->fault(vmf);
1099        } else {
1100                dev_dbg(dev, "%s: err recovery active, use err_page\n",
1101                        __func__);
1102
1103                err_page = get_err_page(cfg);
1104                if (unlikely(!err_page)) {
1105                        dev_err(dev, "%s: Could not get err_page\n", __func__);
1106                        rc = VM_FAULT_RETRY;
1107                        goto out;
1108                }
1109
1110                get_page(err_page);
1111                vmf->page = err_page;
1112                vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
1113        }
1114
1115out:
1116        if (likely(ctxi))
1117                put_context(ctxi);
1118        dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
1119        return rc;
1120
1121err:
1122        rc = VM_FAULT_SIGBUS;
1123        goto out;
1124}
1125
1126/*
1127 * Local MMAP vmops to 'catch' faults
1128 */
1129static const struct vm_operations_struct cxlflash_mmap_vmops = {
1130        .fault = cxlflash_mmap_fault,
1131};
1132
1133/**
1134 * cxlflash_cxl_mmap() - mmap handler for adapter file descriptor
1135 * @file:       File installed with adapter file descriptor.
1136 * @vma:        VM area associated with mapping.
1137 *
1138 * Installs local mmap vmops to 'catch' faults for error notification support.
1139 *
1140 * Return: 0 on success, -errno on failure
1141 */
1142static int cxlflash_cxl_mmap(struct file *file, struct vm_area_struct *vma)
1143{
1144        struct cxl_context *ctx = cxl_fops_get_context(file);
1145        struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
1146                                                cxl_fops);
1147        struct device *dev = &cfg->dev->dev;
1148        struct ctx_info *ctxi = NULL;
1149        enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
1150        int ctxid;
1151        int rc = 0;
1152
1153        ctxid = cxl_process_element(ctx);
1154        if (unlikely(ctxid < 0)) {
1155                dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
1156                        __func__, ctx, ctxid);
1157                rc = -EIO;
1158                goto out;
1159        }
1160
1161        ctxi = get_context(cfg, ctxid, file, ctrl);
1162        if (unlikely(!ctxi)) {
1163                dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid);
1164                rc = -EIO;
1165                goto out;
1166        }
1167
1168        dev_dbg(dev, "%s: mmap for context %d\n", __func__, ctxid);
1169
1170        rc = cxl_fd_mmap(file, vma);
1171        if (likely(!rc)) {
1172                /* Insert ourself in the mmap fault handler path */
1173                ctxi->cxl_mmap_vmops = vma->vm_ops;
1174                vma->vm_ops = &cxlflash_mmap_vmops;
1175        }
1176
1177out:
1178        if (likely(ctxi))
1179                put_context(ctxi);
1180        return rc;
1181}
1182
1183const struct file_operations cxlflash_cxl_fops = {
1184        .owner = THIS_MODULE,
1185        .mmap = cxlflash_cxl_mmap,
1186        .release = cxlflash_cxl_release,
1187};
1188
1189/**
1190 * cxlflash_mark_contexts_error() - move contexts to error state and list
1191 * @cfg:        Internal structure associated with the host.
1192 *
1193 * A context is only moved over to the error list when there are no outstanding
1194 * references to it. This ensures that a running operation has completed.
1195 *
1196 * Return: 0 on success, -errno on failure
1197 */
1198int cxlflash_mark_contexts_error(struct cxlflash_cfg *cfg)
1199{
1200        int i, rc = 0;
1201        struct ctx_info *ctxi = NULL;
1202
1203        mutex_lock(&cfg->ctx_tbl_list_mutex);
1204
1205        for (i = 0; i < MAX_CONTEXT; i++) {
1206                ctxi = cfg->ctx_tbl[i];
1207                if (ctxi) {
1208                        mutex_lock(&ctxi->mutex);
1209                        cfg->ctx_tbl[i] = NULL;
1210                        list_add(&ctxi->list, &cfg->ctx_err_recovery);
1211                        ctxi->err_recovery_active = true;
1212                        ctxi->ctrl_map = NULL;
1213                        unmap_context(ctxi);
1214                        mutex_unlock(&ctxi->mutex);
1215                }
1216        }
1217
1218        mutex_unlock(&cfg->ctx_tbl_list_mutex);
1219        return rc;
1220}
1221
1222/*
1223 * Dummy NULL fops
1224 */
1225static const struct file_operations null_fops = {
1226        .owner = THIS_MODULE,
1227};
1228
1229/**
1230 * check_state() - checks and responds to the current adapter state
1231 * @cfg:        Internal structure associated with the host.
1232 *
1233 * This routine can block and should only be used on process context.
1234 * It assumes that the caller is an ioctl thread and holding the ioctl
1235 * read semaphore. This is temporarily let up across the wait to allow
1236 * for draining actively running ioctls. Also note that when waking up
1237 * from waiting in reset, the state is unknown and must be checked again
1238 * before proceeding.
1239 *
1240 * Return: 0 on success, -errno on failure
1241 */
1242int check_state(struct cxlflash_cfg *cfg)
1243{
1244        struct device *dev = &cfg->dev->dev;
1245        int rc = 0;
1246
1247retry:
1248        switch (cfg->state) {
1249        case STATE_RESET:
1250                dev_dbg(dev, "%s: Reset state, going to wait...\n", __func__);
1251                up_read(&cfg->ioctl_rwsem);
1252                rc = wait_event_interruptible(cfg->reset_waitq,
1253                                              cfg->state != STATE_RESET);
1254                down_read(&cfg->ioctl_rwsem);
1255                if (unlikely(rc))
1256                        break;
1257                goto retry;
1258        case STATE_FAILTERM:
1259                dev_dbg(dev, "%s: Failed/Terminating\n", __func__);
1260                rc = -ENODEV;
1261                break;
1262        default:
1263                break;
1264        }
1265
1266        return rc;
1267}
1268
1269/**
1270 * cxlflash_disk_attach() - attach a LUN to a context
1271 * @sdev:       SCSI device associated with LUN.
1272 * @attach:     Attach ioctl data structure.
1273 *
1274 * Creates a context and attaches LUN to it. A LUN can only be attached
1275 * one time to a context (subsequent attaches for the same context/LUN pair
1276 * are not supported). Additional LUNs can be attached to a context by
1277 * specifying the 'reuse' flag defined in the cxlflash_ioctl.h header.
1278 *
1279 * Return: 0 on success, -errno on failure
1280 */
1281static int cxlflash_disk_attach(struct scsi_device *sdev,
1282                                struct dk_cxlflash_attach *attach)
1283{
1284        struct cxlflash_cfg *cfg = shost_priv(sdev->host);
1285        struct device *dev = &cfg->dev->dev;
1286        struct afu *afu = cfg->afu;
1287        struct llun_info *lli = sdev->hostdata;
1288        struct glun_info *gli = lli->parent;
1289        struct cxl_ioctl_start_work *work;
1290        struct ctx_info *ctxi = NULL;
1291        struct lun_access *lun_access = NULL;
1292        int rc = 0;
1293        u32 perms;
1294        int ctxid = -1;
1295        u64 flags = 0UL;
1296        u64 rctxid = 0UL;
1297        struct file *file = NULL;
1298
1299        struct cxl_context *ctx = NULL;
1300
1301        int fd = -1;
1302
1303        if (attach->num_interrupts > 4) {
1304                dev_dbg(dev, "%s: Cannot support this many interrupts %llu\n",
1305                        __func__, attach->num_interrupts);
1306                rc = -EINVAL;
1307                goto out;
1308        }
1309
1310        if (gli->max_lba == 0) {
1311                dev_dbg(dev, "%s: No capacity info for LUN=%016llx\n",
1312                        __func__, lli->lun_id[sdev->channel]);
1313                rc = read_cap16(sdev, lli);
1314                if (rc) {
1315                        dev_err(dev, "%s: Invalid device rc=%d\n",
1316                                __func__, rc);
1317                        rc = -ENODEV;
1318                        goto out;
1319                }
1320                dev_dbg(dev, "%s: LBA = %016llx\n", __func__, gli->max_lba);
1321                dev_dbg(dev, "%s: BLK_LEN = %08x\n", __func__, gli->blk_len);
1322        }
1323
1324        if (attach->hdr.flags & DK_CXLFLASH_ATTACH_REUSE_CONTEXT) {
1325                rctxid = attach->context_id;
1326                ctxi = get_context(cfg, rctxid, NULL, 0);
1327                if (!ctxi) {
1328                        dev_dbg(dev, "%s: Bad context rctxid=%016llx\n",
1329                                __func__, rctxid);
1330                        rc = -EINVAL;
1331                        goto out;
1332                }
1333
1334                list_for_each_entry(lun_access, &ctxi->luns, list)
1335                        if (lun_access->lli == lli) {
1336                                dev_dbg(dev, "%s: Already attached\n",
1337                                        __func__);
1338                                rc = -EINVAL;
1339                                goto out;
1340                        }
1341        }
1342
1343        rc = scsi_device_get(sdev);
1344        if (unlikely(rc)) {
1345                dev_err(dev, "%s: Unable to get sdev reference\n", __func__);
1346                goto out;
1347        }
1348
1349        lun_access = kzalloc(sizeof(*lun_access), GFP_KERNEL);
1350        if (unlikely(!lun_access)) {
1351                dev_err(dev, "%s: Unable to allocate lun_access\n", __func__);
1352                rc = -ENOMEM;
1353                goto err;
1354        }
1355
1356        lun_access->lli = lli;
1357        lun_access->sdev = sdev;
1358
1359        /* Non-NULL context indicates reuse (another context reference) */
1360        if (ctxi) {
1361                dev_dbg(dev, "%s: Reusing context for LUN rctxid=%016llx\n",
1362                        __func__, rctxid);
1363                kref_get(&ctxi->kref);
1364                list_add(&lun_access->list, &ctxi->luns);
1365                goto out_attach;
1366        }
1367
1368        ctxi = create_context(cfg);
1369        if (unlikely(!ctxi)) {
1370                dev_err(dev, "%s: Failed to create context ctxid=%d\n",
1371                        __func__, ctxid);
1372                goto err;
1373        }
1374
1375        ctx = cxl_dev_context_init(cfg->dev);
1376        if (IS_ERR_OR_NULL(ctx)) {
1377                dev_err(dev, "%s: Could not initialize context %p\n",
1378                        __func__, ctx);
1379                rc = -ENODEV;
1380                goto err;
1381        }
1382
1383        work = &ctxi->work;
1384        work->num_interrupts = attach->num_interrupts;
1385        work->flags = CXL_START_WORK_NUM_IRQS;
1386
1387        rc = cxl_start_work(ctx, work);
1388        if (unlikely(rc)) {
1389                dev_dbg(dev, "%s: Could not start context rc=%d\n",
1390                        __func__, rc);
1391                goto err;
1392        }
1393
1394        ctxid = cxl_process_element(ctx);
1395        if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
1396                dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid);
1397                rc = -EPERM;
1398                goto err;
1399        }
1400
1401        file = cxl_get_fd(ctx, &cfg->cxl_fops, &fd);
1402        if (unlikely(fd < 0)) {
1403                rc = -ENODEV;
1404                dev_err(dev, "%s: Could not get file descriptor\n", __func__);
1405                goto err;
1406        }
1407
1408        /* Translate read/write O_* flags from fcntl.h to AFU permission bits */
1409        perms = SISL_RHT_PERM(attach->hdr.flags + 1);
1410
1411        /* Context mutex is locked upon return */
1412        init_context(ctxi, cfg, ctx, ctxid, file, perms);
1413
1414        rc = afu_attach(cfg, ctxi);
1415        if (unlikely(rc)) {
1416                dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
1417                goto err;
1418        }
1419
1420        /*
1421         * No error paths after this point. Once the fd is installed it's
1422         * visible to user space and can't be undone safely on this thread.
1423         * There is no need to worry about a deadlock here because no one
1424         * knows about us yet; we can be the only one holding our mutex.
1425         */
1426        list_add(&lun_access->list, &ctxi->luns);
1427        mutex_lock(&cfg->ctx_tbl_list_mutex);
1428        mutex_lock(&ctxi->mutex);
1429        cfg->ctx_tbl[ctxid] = ctxi;
1430        mutex_unlock(&cfg->ctx_tbl_list_mutex);
1431        fd_install(fd, file);
1432
1433out_attach:
1434        if (fd != -1)
1435                flags |= DK_CXLFLASH_APP_CLOSE_ADAP_FD;
1436        if (afu_is_sq_cmd_mode(afu))
1437                flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE;
1438
1439        attach->hdr.return_flags = flags;
1440        attach->context_id = ctxi->ctxid;
1441        attach->block_size = gli->blk_len;
1442        attach->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
1443        attach->last_lba = gli->max_lba;
1444        attach->max_xfer = sdev->host->max_sectors * MAX_SECTOR_UNIT;
1445        attach->max_xfer /= gli->blk_len;
1446
1447out:
1448        attach->adap_fd = fd;
1449
1450        if (ctxi)
1451                put_context(ctxi);
1452
1453        dev_dbg(dev, "%s: returning ctxid=%d fd=%d bs=%lld rc=%d llba=%lld\n",
1454                __func__, ctxid, fd, attach->block_size, rc, attach->last_lba);
1455        return rc;
1456
1457err:
1458        /* Cleanup CXL context; okay to 'stop' even if it was not started */
1459        if (!IS_ERR_OR_NULL(ctx)) {
1460                cxl_stop_context(ctx);
1461                cxl_release_context(ctx);
1462                ctx = NULL;
1463        }
1464
1465        /*
1466         * Here, we're overriding the fops with a dummy all-NULL fops because
1467         * fput() calls the release fop, which will cause us to mistakenly
1468         * call into the CXL code. Rather than try to add yet more complexity
1469         * to that routine (cxlflash_cxl_release) we should try to fix the
1470         * issue here.
1471         */
1472        if (fd > 0) {
1473                file->f_op = &null_fops;
1474                fput(file);
1475                put_unused_fd(fd);
1476                fd = -1;
1477                file = NULL;
1478        }
1479
1480        /* Cleanup our context */
1481        if (ctxi) {
1482                destroy_context(cfg, ctxi);
1483                ctxi = NULL;
1484        }
1485
1486        kfree(lun_access);
1487        scsi_device_put(sdev);
1488        goto out;
1489}
1490
1491/**
1492 * recover_context() - recovers a context in error
1493 * @cfg:        Internal structure associated with the host.
1494 * @ctxi:       Context to release.
1495 * @adap_fd:    Adapter file descriptor associated with new/recovered context.
1496 *
1497 * Restablishes the state for a context-in-error.
1498 *
1499 * Return: 0 on success, -errno on failure
1500 */
1501static int recover_context(struct cxlflash_cfg *cfg,
1502                           struct ctx_info *ctxi,
1503                           int *adap_fd)
1504{
1505        struct device *dev = &cfg->dev->dev;
1506        int rc = 0;
1507        int fd = -1;
1508        int ctxid = -1;
1509        struct file *file;
1510        struct cxl_context *ctx;
1511        struct afu *afu = cfg->afu;
1512
1513        ctx = cxl_dev_context_init(cfg->dev);
1514        if (IS_ERR_OR_NULL(ctx)) {
1515                dev_err(dev, "%s: Could not initialize context %p\n",
1516                        __func__, ctx);
1517                rc = -ENODEV;
1518                goto out;
1519        }
1520
1521        rc = cxl_start_work(ctx, &ctxi->work);
1522        if (unlikely(rc)) {
1523                dev_dbg(dev, "%s: Could not start context rc=%d\n",
1524                        __func__, rc);
1525                goto err1;
1526        }
1527
1528        ctxid = cxl_process_element(ctx);
1529        if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
1530                dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid);
1531                rc = -EPERM;
1532                goto err2;
1533        }
1534
1535        file = cxl_get_fd(ctx, &cfg->cxl_fops, &fd);
1536        if (unlikely(fd < 0)) {
1537                rc = -ENODEV;
1538                dev_err(dev, "%s: Could not get file descriptor\n", __func__);
1539                goto err2;
1540        }
1541
1542        /* Update with new MMIO area based on updated context id */
1543        ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
1544
1545        rc = afu_attach(cfg, ctxi);
1546        if (rc) {
1547                dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
1548                goto err3;
1549        }
1550
1551        /*
1552         * No error paths after this point. Once the fd is installed it's
1553         * visible to user space and can't be undone safely on this thread.
1554         */
1555        ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
1556        ctxi->ctx = ctx;
1557        ctxi->file = file;
1558
1559        /*
1560         * Put context back in table (note the reinit of the context list);
1561         * we must first drop the context's mutex and then acquire it in
1562         * order with the table/list mutex to avoid a deadlock - safe to do
1563         * here because no one can find us at this moment in time.
1564         */
1565        mutex_unlock(&ctxi->mutex);
1566        mutex_lock(&cfg->ctx_tbl_list_mutex);
1567        mutex_lock(&ctxi->mutex);
1568        list_del_init(&ctxi->list);
1569        cfg->ctx_tbl[ctxid] = ctxi;
1570        mutex_unlock(&cfg->ctx_tbl_list_mutex);
1571        fd_install(fd, file);
1572        *adap_fd = fd;
1573out:
1574        dev_dbg(dev, "%s: returning ctxid=%d fd=%d rc=%d\n",
1575                __func__, ctxid, fd, rc);
1576        return rc;
1577
1578err3:
1579        fput(file);
1580        put_unused_fd(fd);
1581err2:
1582        cxl_stop_context(ctx);
1583err1:
1584        cxl_release_context(ctx);
1585        goto out;
1586}
1587
1588/**
1589 * cxlflash_afu_recover() - initiates AFU recovery
1590 * @sdev:       SCSI device associated with LUN.
1591 * @recover:    Recover ioctl data structure.
1592 *
1593 * Only a single recovery is allowed at a time to avoid exhausting CXL
1594 * resources (leading to recovery failure) in the event that we're up
1595 * against the maximum number of contexts limit. For similar reasons,
1596 * a context recovery is retried if there are multiple recoveries taking
1597 * place at the same time and the failure was due to CXL services being
1598 * unable to keep up.
1599 *
1600 * As this routine is called on ioctl context, it holds the ioctl r/w
1601 * semaphore that is used to drain ioctls in recovery scenarios. The
1602 * implementation to achieve the pacing described above (a local mutex)
1603 * requires that the ioctl r/w semaphore be dropped and reacquired to
1604 * avoid a 3-way deadlock when multiple process recoveries operate in
1605 * parallel.
1606 *
1607 * Because a user can detect an error condition before the kernel, it is
1608 * quite possible for this routine to act as the kernel's EEH detection
1609 * source (MMIO read of mbox_r). Because of this, there is a window of
1610 * time where an EEH might have been detected but not yet 'serviced'
1611 * (callback invoked, causing the device to enter reset state). To avoid
1612 * looping in this routine during that window, a 1 second sleep is in place
1613 * between the time the MMIO failure is detected and the time a wait on the
1614 * reset wait queue is attempted via check_state().
1615 *
1616 * Return: 0 on success, -errno on failure
1617 */
1618static int cxlflash_afu_recover(struct scsi_device *sdev,
1619                                struct dk_cxlflash_recover_afu *recover)
1620{
1621        struct cxlflash_cfg *cfg = shost_priv(sdev->host);
1622        struct device *dev = &cfg->dev->dev;
1623        struct llun_info *lli = sdev->hostdata;
1624        struct afu *afu = cfg->afu;
1625        struct ctx_info *ctxi = NULL;
1626        struct mutex *mutex = &cfg->ctx_recovery_mutex;
1627        u64 flags;
1628        u64 ctxid = DECODE_CTXID(recover->context_id),
1629            rctxid = recover->context_id;
1630        long reg;
1631        int lretry = 20; /* up to 2 seconds */
1632        int new_adap_fd = -1;
1633        int rc = 0;
1634
1635        atomic_inc(&cfg->recovery_threads);
1636        up_read(&cfg->ioctl_rwsem);
1637        rc = mutex_lock_interruptible(mutex);
1638        down_read(&cfg->ioctl_rwsem);
1639        if (rc)
1640                goto out;
1641        rc = check_state(cfg);
1642        if (rc) {
1643                dev_err(dev, "%s: Failed state rc=%d\n", __func__, rc);
1644                rc = -ENODEV;
1645                goto out;
1646        }
1647
1648        dev_dbg(dev, "%s: reason=%016llx rctxid=%016llx\n",
1649                __func__, recover->reason, rctxid);
1650
1651retry:
1652        /* Ensure that this process is attached to the context */
1653        ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
1654        if (unlikely(!ctxi)) {
1655                dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
1656                rc = -EINVAL;
1657                goto out;
1658        }
1659
1660        if (ctxi->err_recovery_active) {
1661retry_recover:
1662                rc = recover_context(cfg, ctxi, &new_adap_fd);
1663                if (unlikely(rc)) {
1664                        dev_err(dev, "%s: Recovery failed ctxid=%llu rc=%d\n",
1665                                __func__, ctxid, rc);
1666                        if ((rc == -ENODEV) &&
1667                            ((atomic_read(&cfg->recovery_threads) > 1) ||
1668                             (lretry--))) {
1669                                dev_dbg(dev, "%s: Going to try again\n",
1670                                        __func__);
1671                                mutex_unlock(mutex);
1672                                msleep(100);
1673                                rc = mutex_lock_interruptible(mutex);
1674                                if (rc)
1675                                        goto out;
1676                                goto retry_recover;
1677                        }
1678
1679                        goto out;
1680                }
1681
1682                ctxi->err_recovery_active = false;
1683
1684                flags = DK_CXLFLASH_APP_CLOSE_ADAP_FD |
1685                        DK_CXLFLASH_RECOVER_AFU_CONTEXT_RESET;
1686                if (afu_is_sq_cmd_mode(afu))
1687                        flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE;
1688
1689                recover->hdr.return_flags = flags;
1690                recover->context_id = ctxi->ctxid;
1691                recover->adap_fd = new_adap_fd;
1692                recover->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
1693                goto out;
1694        }
1695
1696        /* Test if in error state */
1697        reg = readq_be(&afu->ctrl_map->mbox_r);
1698        if (reg == -1) {
1699                dev_dbg(dev, "%s: MMIO fail, wait for recovery.\n", __func__);
1700
1701                /*
1702                 * Before checking the state, put back the context obtained with
1703                 * get_context() as it is no longer needed and sleep for a short
1704                 * period of time (see prolog notes).
1705                 */
1706                put_context(ctxi);
1707                ctxi = NULL;
1708                ssleep(1);
1709                rc = check_state(cfg);
1710                if (unlikely(rc))
1711                        goto out;
1712                goto retry;
1713        }
1714
1715        dev_dbg(dev, "%s: MMIO working, no recovery required\n", __func__);
1716out:
1717        if (likely(ctxi))
1718                put_context(ctxi);
1719        mutex_unlock(mutex);
1720        atomic_dec_if_positive(&cfg->recovery_threads);
1721        return rc;
1722}
1723
1724/**
1725 * process_sense() - evaluates and processes sense data
1726 * @sdev:       SCSI device associated with LUN.
1727 * @verify:     Verify ioctl data structure.
1728 *
1729 * Return: 0 on success, -errno on failure
1730 */
1731static int process_sense(struct scsi_device *sdev,
1732                         struct dk_cxlflash_verify *verify)
1733{
1734        struct cxlflash_cfg *cfg = shost_priv(sdev->host);
1735        struct device *dev = &cfg->dev->dev;
1736        struct llun_info *lli = sdev->hostdata;
1737        struct glun_info *gli = lli->parent;
1738        u64 prev_lba = gli->max_lba;
1739        struct scsi_sense_hdr sshdr = { 0 };
1740        int rc = 0;
1741
1742        rc = scsi_normalize_sense((const u8 *)&verify->sense_data,
1743                                  DK_CXLFLASH_VERIFY_SENSE_LEN, &sshdr);
1744        if (!rc) {
1745                dev_err(dev, "%s: Failed to normalize sense data\n", __func__);
1746                rc = -EINVAL;
1747                goto out;
1748        }
1749
1750        switch (sshdr.sense_key) {
1751        case NO_SENSE:
1752        case RECOVERED_ERROR:
1753                /* fall through */
1754        case NOT_READY:
1755                break;
1756        case UNIT_ATTENTION:
1757                switch (sshdr.asc) {
1758                case 0x29: /* Power on Reset or Device Reset */
1759                        /* fall through */
1760                case 0x2A: /* Device settings/capacity changed */
1761                        rc = read_cap16(sdev, lli);
1762                        if (rc) {
1763                                rc = -ENODEV;
1764                                break;
1765                        }
1766                        if (prev_lba != gli->max_lba)
1767                                dev_dbg(dev, "%s: Capacity changed old=%lld "
1768                                        "new=%lld\n", __func__, prev_lba,
1769                                        gli->max_lba);
1770                        break;
1771                case 0x3F: /* Report LUNs changed, Rescan. */
1772                        scsi_scan_host(cfg->host);
1773                        break;
1774                default:
1775                        rc = -EIO;
1776                        break;
1777                }
1778                break;
1779        default:
1780                rc = -EIO;
1781                break;
1782        }
1783out:
1784        dev_dbg(dev, "%s: sense_key %x asc %x ascq %x rc %d\n", __func__,
1785                sshdr.sense_key, sshdr.asc, sshdr.ascq, rc);
1786        return rc;
1787}
1788
1789/**
1790 * cxlflash_disk_verify() - verifies a LUN is the same and handle size changes
1791 * @sdev:       SCSI device associated with LUN.
1792 * @verify:     Verify ioctl data structure.
1793 *
1794 * Return: 0 on success, -errno on failure
1795 */
1796static int cxlflash_disk_verify(struct scsi_device *sdev,
1797                                struct dk_cxlflash_verify *verify)
1798{
1799        int rc = 0;
1800        struct ctx_info *ctxi = NULL;
1801        struct cxlflash_cfg *cfg = shost_priv(sdev->host);
1802        struct device *dev = &cfg->dev->dev;
1803        struct llun_info *lli = sdev->hostdata;
1804        struct glun_info *gli = lli->parent;
1805        struct sisl_rht_entry *rhte = NULL;
1806        res_hndl_t rhndl = verify->rsrc_handle;
1807        u64 ctxid = DECODE_CTXID(verify->context_id),
1808            rctxid = verify->context_id;
1809        u64 last_lba = 0;
1810
1811        dev_dbg(dev, "%s: ctxid=%llu rhndl=%016llx, hint=%016llx, "
1812                "flags=%016llx\n", __func__, ctxid, verify->rsrc_handle,
1813                verify->hint, verify->hdr.flags);
1814
1815        ctxi = get_context(cfg, rctxid, lli, 0);
1816        if (unlikely(!ctxi)) {
1817                dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
1818                rc = -EINVAL;
1819                goto out;
1820        }
1821
1822        rhte = get_rhte(ctxi, rhndl, lli);
1823        if (unlikely(!rhte)) {
1824                dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
1825                        __func__, rhndl);
1826                rc = -EINVAL;
1827                goto out;
1828        }
1829
1830        /*
1831         * Look at the hint/sense to see if it requires us to redrive
1832         * inquiry (i.e. the Unit attention is due to the WWN changing).
1833         */
1834        if (verify->hint & DK_CXLFLASH_VERIFY_HINT_SENSE) {
1835                /* Can't hold mutex across process_sense/read_cap16,
1836                 * since we could have an intervening EEH event.
1837                 */
1838                ctxi->unavail = true;
1839                mutex_unlock(&ctxi->mutex);
1840                rc = process_sense(sdev, verify);
1841                if (unlikely(rc)) {
1842                        dev_err(dev, "%s: Failed to validate sense data (%d)\n",
1843                                __func__, rc);
1844                        mutex_lock(&ctxi->mutex);
1845                        ctxi->unavail = false;
1846                        goto out;
1847                }
1848                mutex_lock(&ctxi->mutex);
1849                ctxi->unavail = false;
1850        }
1851
1852        switch (gli->mode) {
1853        case MODE_PHYSICAL:
1854                last_lba = gli->max_lba;
1855                break;
1856        case MODE_VIRTUAL:
1857                /* Cast lxt_cnt to u64 for multiply to be treated as 64bit op */
1858                last_lba = ((u64)rhte->lxt_cnt * MC_CHUNK_SIZE * gli->blk_len);
1859                last_lba /= CXLFLASH_BLOCK_SIZE;
1860                last_lba--;
1861                break;
1862        default:
1863                WARN(1, "Unsupported LUN mode!");
1864        }
1865
1866        verify->last_lba = last_lba;
1867
1868out:
1869        if (likely(ctxi))
1870                put_context(ctxi);
1871        dev_dbg(dev, "%s: returning rc=%d llba=%llx\n",
1872                __func__, rc, verify->last_lba);
1873        return rc;
1874}
1875
1876/**
1877 * decode_ioctl() - translates an encoded ioctl to an easily identifiable string
1878 * @cmd:        The ioctl command to decode.
1879 *
1880 * Return: A string identifying the decoded ioctl.
1881 */
1882static char *decode_ioctl(int cmd)
1883{
1884        switch (cmd) {
1885        case DK_CXLFLASH_ATTACH:
1886                return __stringify_1(DK_CXLFLASH_ATTACH);
1887        case DK_CXLFLASH_USER_DIRECT:
1888                return __stringify_1(DK_CXLFLASH_USER_DIRECT);
1889        case DK_CXLFLASH_USER_VIRTUAL:
1890                return __stringify_1(DK_CXLFLASH_USER_VIRTUAL);
1891        case DK_CXLFLASH_VLUN_RESIZE:
1892                return __stringify_1(DK_CXLFLASH_VLUN_RESIZE);
1893        case DK_CXLFLASH_RELEASE:
1894                return __stringify_1(DK_CXLFLASH_RELEASE);
1895        case DK_CXLFLASH_DETACH:
1896                return __stringify_1(DK_CXLFLASH_DETACH);
1897        case DK_CXLFLASH_VERIFY:
1898                return __stringify_1(DK_CXLFLASH_VERIFY);
1899        case DK_CXLFLASH_VLUN_CLONE:
1900                return __stringify_1(DK_CXLFLASH_VLUN_CLONE);
1901        case DK_CXLFLASH_RECOVER_AFU:
1902                return __stringify_1(DK_CXLFLASH_RECOVER_AFU);
1903        case DK_CXLFLASH_MANAGE_LUN:
1904                return __stringify_1(DK_CXLFLASH_MANAGE_LUN);
1905        }
1906
1907        return "UNKNOWN";
1908}
1909
1910/**
1911 * cxlflash_disk_direct_open() - opens a direct (physical) disk
1912 * @sdev:       SCSI device associated with LUN.
1913 * @arg:        UDirect ioctl data structure.
1914 *
1915 * On successful return, the user is informed of the resource handle
1916 * to be used to identify the direct lun and the size (in blocks) of
1917 * the direct lun in last LBA format.
1918 *
1919 * Return: 0 on success, -errno on failure
1920 */
1921static int cxlflash_disk_direct_open(struct scsi_device *sdev, void *arg)
1922{
1923        struct cxlflash_cfg *cfg = shost_priv(sdev->host);
1924        struct device *dev = &cfg->dev->dev;
1925        struct afu *afu = cfg->afu;
1926        struct llun_info *lli = sdev->hostdata;
1927        struct glun_info *gli = lli->parent;
1928
1929        struct dk_cxlflash_udirect *pphys = (struct dk_cxlflash_udirect *)arg;
1930
1931        u64 ctxid = DECODE_CTXID(pphys->context_id),
1932            rctxid = pphys->context_id;
1933        u64 lun_size = 0;
1934        u64 last_lba = 0;
1935        u64 rsrc_handle = -1;
1936        u32 port = CHAN2PORT(sdev->channel);
1937
1938        int rc = 0;
1939
1940        struct ctx_info *ctxi = NULL;
1941        struct sisl_rht_entry *rhte = NULL;
1942
1943        dev_dbg(dev, "%s: ctxid=%llu ls=%llu\n", __func__, ctxid, lun_size);
1944
1945        rc = cxlflash_lun_attach(gli, MODE_PHYSICAL, false);
1946        if (unlikely(rc)) {
1947                dev_dbg(dev, "%s: Failed attach to LUN (PHYSICAL)\n", __func__);
1948                goto out;
1949        }
1950
1951        ctxi = get_context(cfg, rctxid, lli, 0);
1952        if (unlikely(!ctxi)) {
1953                dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
1954                rc = -EINVAL;
1955                goto err1;
1956        }
1957
1958        rhte = rhte_checkout(ctxi, lli);
1959        if (unlikely(!rhte)) {
1960                dev_dbg(dev, "%s: Too many opens ctxid=%lld\n",
1961                        __func__, ctxid);
1962                rc = -EMFILE;   /* too many opens  */
1963                goto err1;
1964        }
1965
1966        rsrc_handle = (rhte - ctxi->rht_start);
1967
1968        rht_format1(rhte, lli->lun_id[sdev->channel], ctxi->rht_perms, port);
1969        cxlflash_afu_sync(afu, ctxid, rsrc_handle, AFU_LW_SYNC);
1970
1971        last_lba = gli->max_lba;
1972        pphys->hdr.return_flags = 0;
1973        pphys->last_lba = last_lba;
1974        pphys->rsrc_handle = rsrc_handle;
1975
1976out:
1977        if (likely(ctxi))
1978                put_context(ctxi);
1979        dev_dbg(dev, "%s: returning handle=%llu rc=%d llba=%llu\n",
1980                __func__, rsrc_handle, rc, last_lba);
1981        return rc;
1982
1983err1:
1984        cxlflash_lun_detach(gli);
1985        goto out;
1986}
1987
1988/**
1989 * ioctl_common() - common IOCTL handler for driver
1990 * @sdev:       SCSI device associated with LUN.
1991 * @cmd:        IOCTL command.
1992 *
1993 * Handles common fencing operations that are valid for multiple ioctls. Always
1994 * allow through ioctls that are cleanup oriented in nature, even when operating
1995 * in a failed/terminating state.
1996 *
1997 * Return: 0 on success, -errno on failure
1998 */
1999static int ioctl_common(struct scsi_device *sdev, int cmd)
2000{
2001        struct cxlflash_cfg *cfg = shost_priv(sdev->host);
2002        struct device *dev = &cfg->dev->dev;
2003        struct llun_info *lli = sdev->hostdata;
2004        int rc = 0;
2005
2006        if (unlikely(!lli)) {
2007                dev_dbg(dev, "%s: Unknown LUN\n", __func__);
2008                rc = -EINVAL;
2009                goto out;
2010        }
2011
2012        rc = check_state(cfg);
2013        if (unlikely(rc) && (cfg->state == STATE_FAILTERM)) {
2014                switch (cmd) {
2015                case DK_CXLFLASH_VLUN_RESIZE:
2016                case DK_CXLFLASH_RELEASE:
2017                case DK_CXLFLASH_DETACH:
2018                        dev_dbg(dev, "%s: Command override rc=%d\n",
2019                                __func__, rc);
2020                        rc = 0;
2021                        break;
2022                }
2023        }
2024out:
2025        return rc;
2026}
2027
2028/**
2029 * cxlflash_ioctl() - IOCTL handler for driver
2030 * @sdev:       SCSI device associated with LUN.
2031 * @cmd:        IOCTL command.
2032 * @arg:        Userspace ioctl data structure.
2033 *
2034 * A read/write semaphore is used to implement a 'drain' of currently
2035 * running ioctls. The read semaphore is taken at the beginning of each
2036 * ioctl thread and released upon concluding execution. Additionally the
2037 * semaphore should be released and then reacquired in any ioctl execution
2038 * path which will wait for an event to occur that is outside the scope of
2039 * the ioctl (i.e. an adapter reset). To drain the ioctls currently running,
2040 * a thread simply needs to acquire the write semaphore.
2041 *
2042 * Return: 0 on success, -errno on failure
2043 */
2044int cxlflash_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
2045{
2046        typedef int (*sioctl) (struct scsi_device *, void *);
2047
2048        struct cxlflash_cfg *cfg = shost_priv(sdev->host);
2049        struct device *dev = &cfg->dev->dev;
2050        struct afu *afu = cfg->afu;
2051        struct dk_cxlflash_hdr *hdr;
2052        char buf[sizeof(union cxlflash_ioctls)];
2053        size_t size = 0;
2054        bool known_ioctl = false;
2055        int idx;
2056        int rc = 0;
2057        struct Scsi_Host *shost = sdev->host;
2058        sioctl do_ioctl = NULL;
2059
2060        static const struct {
2061                size_t size;
2062                sioctl ioctl;
2063        } ioctl_tbl[] = {       /* NOTE: order matters here */
2064        {sizeof(struct dk_cxlflash_attach), (sioctl)cxlflash_disk_attach},
2065        {sizeof(struct dk_cxlflash_udirect), cxlflash_disk_direct_open},
2066        {sizeof(struct dk_cxlflash_release), (sioctl)cxlflash_disk_release},
2067        {sizeof(struct dk_cxlflash_detach), (sioctl)cxlflash_disk_detach},
2068        {sizeof(struct dk_cxlflash_verify), (sioctl)cxlflash_disk_verify},
2069        {sizeof(struct dk_cxlflash_recover_afu), (sioctl)cxlflash_afu_recover},
2070        {sizeof(struct dk_cxlflash_manage_lun), (sioctl)cxlflash_manage_lun},
2071        {sizeof(struct dk_cxlflash_uvirtual), cxlflash_disk_virtual_open},
2072        {sizeof(struct dk_cxlflash_resize), (sioctl)cxlflash_vlun_resize},
2073        {sizeof(struct dk_cxlflash_clone), (sioctl)cxlflash_disk_clone},
2074        };
2075
2076        /* Hold read semaphore so we can drain if needed */
2077        down_read(&cfg->ioctl_rwsem);
2078
2079        /* Restrict command set to physical support only for internal LUN */
2080        if (afu->internal_lun)
2081                switch (cmd) {
2082                case DK_CXLFLASH_RELEASE:
2083                case DK_CXLFLASH_USER_VIRTUAL:
2084                case DK_CXLFLASH_VLUN_RESIZE:
2085                case DK_CXLFLASH_VLUN_CLONE:
2086                        dev_dbg(dev, "%s: %s not supported for lun_mode=%d\n",
2087                                __func__, decode_ioctl(cmd), afu->internal_lun);
2088                        rc = -EINVAL;
2089                        goto cxlflash_ioctl_exit;
2090                }
2091
2092        switch (cmd) {
2093        case DK_CXLFLASH_ATTACH:
2094        case DK_CXLFLASH_USER_DIRECT:
2095        case DK_CXLFLASH_RELEASE:
2096        case DK_CXLFLASH_DETACH:
2097        case DK_CXLFLASH_VERIFY:
2098        case DK_CXLFLASH_RECOVER_AFU:
2099        case DK_CXLFLASH_USER_VIRTUAL:
2100        case DK_CXLFLASH_VLUN_RESIZE:
2101        case DK_CXLFLASH_VLUN_CLONE:
2102                dev_dbg(dev, "%s: %s (%08X) on dev(%d/%d/%d/%llu)\n",
2103                        __func__, decode_ioctl(cmd), cmd, shost->host_no,
2104                        sdev->channel, sdev->id, sdev->lun);
2105                rc = ioctl_common(sdev, cmd);
2106                if (unlikely(rc))
2107                        goto cxlflash_ioctl_exit;
2108
2109                /* fall through */
2110
2111        case DK_CXLFLASH_MANAGE_LUN:
2112                known_ioctl = true;
2113                idx = _IOC_NR(cmd) - _IOC_NR(DK_CXLFLASH_ATTACH);
2114                size = ioctl_tbl[idx].size;
2115                do_ioctl = ioctl_tbl[idx].ioctl;
2116
2117                if (likely(do_ioctl))
2118                        break;
2119
2120                /* fall through */
2121        default:
2122                rc = -EINVAL;
2123                goto cxlflash_ioctl_exit;
2124        }
2125
2126        if (unlikely(copy_from_user(&buf, arg, size))) {
2127                dev_err(dev, "%s: copy_from_user() fail "
2128                        "size=%lu cmd=%d (%s) arg=%p\n",
2129                        __func__, size, cmd, decode_ioctl(cmd), arg);
2130                rc = -EFAULT;
2131                goto cxlflash_ioctl_exit;
2132        }
2133
2134        hdr = (struct dk_cxlflash_hdr *)&buf;
2135        if (hdr->version != DK_CXLFLASH_VERSION_0) {
2136                dev_dbg(dev, "%s: Version %u not supported for %s\n",
2137                        __func__, hdr->version, decode_ioctl(cmd));
2138                rc = -EINVAL;
2139                goto cxlflash_ioctl_exit;
2140        }
2141
2142        if (hdr->rsvd[0] || hdr->rsvd[1] || hdr->rsvd[2] || hdr->return_flags) {
2143                dev_dbg(dev, "%s: Reserved/rflags populated\n", __func__);
2144                rc = -EINVAL;
2145                goto cxlflash_ioctl_exit;
2146        }
2147
2148        rc = do_ioctl(sdev, (void *)&buf);
2149        if (likely(!rc))
2150                if (unlikely(copy_to_user(arg, &buf, size))) {
2151                        dev_err(dev, "%s: copy_to_user() fail "
2152                                "size=%lu cmd=%d (%s) arg=%p\n",
2153                                __func__, size, cmd, decode_ioctl(cmd), arg);
2154                        rc = -EFAULT;
2155                }
2156
2157        /* fall through to exit */
2158
2159cxlflash_ioctl_exit:
2160        up_read(&cfg->ioctl_rwsem);
2161        if (unlikely(rc && known_ioctl))
2162                dev_err(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
2163                        "returned rc %d\n", __func__,
2164                        decode_ioctl(cmd), cmd, shost->host_no,
2165                        sdev->channel, sdev->id, sdev->lun, rc);
2166        else
2167                dev_dbg(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
2168                        "returned rc %d\n", __func__, decode_ioctl(cmd),
2169                        cmd, shost->host_no, sdev->channel, sdev->id,
2170                        sdev->lun, rc);
2171        return rc;
2172}
2173