Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 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/list.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/pci.h> |
| 19 | |
| 20 | #include <asm/unaligned.h> |
| 21 | |
| 22 | #include <misc/cxl.h> |
| 23 | |
| 24 | #include <scsi/scsi_cmnd.h> |
| 25 | #include <scsi/scsi_host.h> |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 26 | #include <uapi/scsi/cxlflash_ioctl.h> |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 27 | |
| 28 | #include "main.h" |
| 29 | #include "sislite.h" |
| 30 | #include "common.h" |
| 31 | |
| 32 | MODULE_DESCRIPTION(CXLFLASH_ADAPTER_NAME); |
| 33 | MODULE_AUTHOR("Manoj N. Kumar <manoj@linux.vnet.ibm.com>"); |
| 34 | MODULE_AUTHOR("Matthew R. Ochs <mrochs@linux.vnet.ibm.com>"); |
| 35 | MODULE_LICENSE("GPL"); |
| 36 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 37 | /** |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 38 | * cmd_checkout() - checks out an AFU command |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 39 | * @afu: AFU to checkout from. |
| 40 | * |
| 41 | * Commands are checked out in a round-robin fashion. Note that since |
| 42 | * the command pool is larger than the hardware queue, the majority of |
| 43 | * times we will only loop once or twice before getting a command. The |
Matthew R. Ochs | e7ab2d40 | 2016-11-28 18:42:01 -0600 | [diff] [blame] | 44 | * CDB within the command is initialized (zeroed) prior to returning. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 45 | * |
| 46 | * Return: The checked out command or NULL when command pool is empty. |
| 47 | */ |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 48 | static struct afu_cmd *cmd_checkout(struct afu *afu) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 49 | { |
| 50 | int k, dec = CXLFLASH_NUM_CMDS; |
| 51 | struct afu_cmd *cmd; |
| 52 | |
| 53 | while (dec--) { |
| 54 | k = (afu->cmd_couts++ & (CXLFLASH_NUM_CMDS - 1)); |
| 55 | |
| 56 | cmd = &afu->cmd[k]; |
| 57 | |
| 58 | if (!atomic_dec_if_positive(&cmd->free)) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 59 | pr_devel("%s: returning found index=%d cmd=%p\n", |
| 60 | __func__, cmd->slot, cmd); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 61 | memset(cmd->rcb.cdb, 0, sizeof(cmd->rcb.cdb)); |
| 62 | return cmd; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | /** |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 70 | * cmd_checkin() - checks in an AFU command |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 71 | * @cmd: AFU command to checkin. |
| 72 | * |
| 73 | * Safe to pass commands that have already been checked in. Several |
| 74 | * internal tracking fields are reset as part of the checkin. Note |
| 75 | * that these are intentionally reset prior to toggling the free bit |
| 76 | * to avoid clobbering values in the event that the command is checked |
| 77 | * out right away. |
| 78 | */ |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 79 | static void cmd_checkin(struct afu_cmd *cmd) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 80 | { |
| 81 | cmd->rcb.scp = NULL; |
| 82 | cmd->rcb.timeout = 0; |
| 83 | cmd->sa.ioasc = 0; |
| 84 | cmd->cmd_tmf = false; |
| 85 | cmd->sa.host_use[0] = 0; /* clears both completion and retry bytes */ |
| 86 | |
| 87 | if (unlikely(atomic_inc_return(&cmd->free) != 1)) { |
| 88 | pr_err("%s: Freeing cmd (%d) that is not in use!\n", |
| 89 | __func__, cmd->slot); |
| 90 | return; |
| 91 | } |
| 92 | |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 93 | pr_devel("%s: released cmd %p index=%d\n", __func__, cmd, cmd->slot); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * process_cmd_err() - command error handler |
| 98 | * @cmd: AFU command that experienced the error. |
| 99 | * @scp: SCSI command associated with the AFU command in error. |
| 100 | * |
| 101 | * Translates error bits from AFU command to SCSI command results. |
| 102 | */ |
| 103 | static void process_cmd_err(struct afu_cmd *cmd, struct scsi_cmnd *scp) |
| 104 | { |
| 105 | struct sisl_ioarcb *ioarcb; |
| 106 | struct sisl_ioasa *ioasa; |
Matthew R. Ochs | 8396012 | 2015-10-21 15:13:29 -0500 | [diff] [blame] | 107 | u32 resid; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 108 | |
| 109 | if (unlikely(!cmd)) |
| 110 | return; |
| 111 | |
| 112 | ioarcb = &(cmd->rcb); |
| 113 | ioasa = &(cmd->sa); |
| 114 | |
| 115 | if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) { |
Matthew R. Ochs | 8396012 | 2015-10-21 15:13:29 -0500 | [diff] [blame] | 116 | resid = ioasa->resid; |
| 117 | scsi_set_resid(scp, resid); |
| 118 | pr_debug("%s: cmd underrun cmd = %p scp = %p, resid = %d\n", |
| 119 | __func__, cmd, scp, resid); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) { |
| 123 | pr_debug("%s: cmd underrun cmd = %p scp = %p\n", |
| 124 | __func__, cmd, scp); |
| 125 | scp->result = (DID_ERROR << 16); |
| 126 | } |
| 127 | |
| 128 | pr_debug("%s: cmd failed afu_rc=%d scsi_rc=%d fc_rc=%d " |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 129 | "afu_extra=0x%X, scsi_extra=0x%X, fc_extra=0x%X\n", |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 130 | __func__, ioasa->rc.afu_rc, ioasa->rc.scsi_rc, |
| 131 | ioasa->rc.fc_rc, ioasa->afu_extra, ioasa->scsi_extra, |
| 132 | ioasa->fc_extra); |
| 133 | |
| 134 | if (ioasa->rc.scsi_rc) { |
| 135 | /* We have a SCSI status */ |
| 136 | if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) { |
| 137 | memcpy(scp->sense_buffer, ioasa->sense_data, |
| 138 | SISL_SENSE_DATA_LEN); |
| 139 | scp->result = ioasa->rc.scsi_rc; |
| 140 | } else |
| 141 | scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * We encountered an error. Set scp->result based on nature |
| 146 | * of error. |
| 147 | */ |
| 148 | if (ioasa->rc.fc_rc) { |
| 149 | /* We have an FC status */ |
| 150 | switch (ioasa->rc.fc_rc) { |
| 151 | case SISL_FC_RC_LINKDOWN: |
| 152 | scp->result = (DID_REQUEUE << 16); |
| 153 | break; |
| 154 | case SISL_FC_RC_RESID: |
| 155 | /* This indicates an FCP resid underrun */ |
| 156 | if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) { |
| 157 | /* If the SISL_RC_FLAGS_OVERRUN flag was set, |
| 158 | * then we will handle this error else where. |
| 159 | * If not then we must handle it here. |
Matthew R. Ochs | 8396012 | 2015-10-21 15:13:29 -0500 | [diff] [blame] | 160 | * This is probably an AFU bug. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 161 | */ |
| 162 | scp->result = (DID_ERROR << 16); |
| 163 | } |
| 164 | break; |
| 165 | case SISL_FC_RC_RESIDERR: |
| 166 | /* Resid mismatch between adapter and device */ |
| 167 | case SISL_FC_RC_TGTABORT: |
| 168 | case SISL_FC_RC_ABORTOK: |
| 169 | case SISL_FC_RC_ABORTFAIL: |
| 170 | case SISL_FC_RC_NOLOGI: |
| 171 | case SISL_FC_RC_ABORTPEND: |
| 172 | case SISL_FC_RC_WRABORTPEND: |
| 173 | case SISL_FC_RC_NOEXP: |
| 174 | case SISL_FC_RC_INUSE: |
| 175 | scp->result = (DID_ERROR << 16); |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (ioasa->rc.afu_rc) { |
| 181 | /* We have an AFU error */ |
| 182 | switch (ioasa->rc.afu_rc) { |
| 183 | case SISL_AFU_RC_NO_CHANNELS: |
Matthew R. Ochs | 8396012 | 2015-10-21 15:13:29 -0500 | [diff] [blame] | 184 | scp->result = (DID_NO_CONNECT << 16); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 185 | break; |
| 186 | case SISL_AFU_RC_DATA_DMA_ERR: |
| 187 | switch (ioasa->afu_extra) { |
| 188 | case SISL_AFU_DMA_ERR_PAGE_IN: |
| 189 | /* Retry */ |
| 190 | scp->result = (DID_IMM_RETRY << 16); |
| 191 | break; |
| 192 | case SISL_AFU_DMA_ERR_INVALID_EA: |
| 193 | default: |
| 194 | scp->result = (DID_ERROR << 16); |
| 195 | } |
| 196 | break; |
| 197 | case SISL_AFU_RC_OUT_OF_DATA_BUFS: |
| 198 | /* Retry */ |
| 199 | scp->result = (DID_ALLOC_FAILURE << 16); |
| 200 | break; |
| 201 | default: |
| 202 | scp->result = (DID_ERROR << 16); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * cmd_complete() - command completion handler |
| 209 | * @cmd: AFU command that has completed. |
| 210 | * |
| 211 | * Prepares and submits command that has either completed or timed out to |
| 212 | * the SCSI stack. Checks AFU command back into command pool for non-internal |
| 213 | * (rcb.scp populated) commands. |
| 214 | */ |
| 215 | static void cmd_complete(struct afu_cmd *cmd) |
| 216 | { |
| 217 | struct scsi_cmnd *scp; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 218 | ulong lock_flags; |
| 219 | struct afu *afu = cmd->parent; |
| 220 | struct cxlflash_cfg *cfg = afu->parent; |
| 221 | bool cmd_is_tmf; |
| 222 | |
| 223 | spin_lock_irqsave(&cmd->slock, lock_flags); |
| 224 | cmd->sa.host_use_b[0] |= B_DONE; |
| 225 | spin_unlock_irqrestore(&cmd->slock, lock_flags); |
| 226 | |
| 227 | if (cmd->rcb.scp) { |
| 228 | scp = cmd->rcb.scp; |
Matthew R. Ochs | 8396012 | 2015-10-21 15:13:29 -0500 | [diff] [blame] | 229 | if (unlikely(cmd->sa.ioasc)) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 230 | process_cmd_err(cmd, scp); |
| 231 | else |
| 232 | scp->result = (DID_OK << 16); |
| 233 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 234 | cmd_is_tmf = cmd->cmd_tmf; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 235 | cmd_checkin(cmd); /* Don't use cmd after here */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 236 | |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 237 | pr_debug_ratelimited("%s: calling scsi_done scp=%p result=%X " |
| 238 | "ioasc=%d\n", __func__, scp, scp->result, |
| 239 | cmd->sa.ioasc); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 240 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 241 | scsi_dma_unmap(scp); |
| 242 | scp->scsi_done(scp); |
| 243 | |
| 244 | if (cmd_is_tmf) { |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 245 | spin_lock_irqsave(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 246 | cfg->tmf_active = false; |
| 247 | wake_up_all_locked(&cfg->tmf_waitq); |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 248 | spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 249 | } |
| 250 | } else |
| 251 | complete(&cmd->cevent); |
| 252 | } |
| 253 | |
| 254 | /** |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 255 | * context_reset() - timeout handler for AFU commands |
| 256 | * @cmd: AFU command that timed out. |
| 257 | * |
| 258 | * Sends a reset to the AFU. |
| 259 | */ |
| 260 | static void context_reset(struct afu_cmd *cmd) |
| 261 | { |
| 262 | int nretry = 0; |
| 263 | u64 rrin = 0x1; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 264 | struct afu *afu = cmd->parent; |
Uma Krishnan | 3d2f617 | 2016-11-28 18:41:36 -0600 | [diff] [blame] | 265 | struct cxlflash_cfg *cfg = afu->parent; |
| 266 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 267 | ulong lock_flags; |
| 268 | |
| 269 | pr_debug("%s: cmd=%p\n", __func__, cmd); |
| 270 | |
| 271 | spin_lock_irqsave(&cmd->slock, lock_flags); |
| 272 | |
| 273 | /* Already completed? */ |
| 274 | if (cmd->sa.host_use_b[0] & B_DONE) { |
| 275 | spin_unlock_irqrestore(&cmd->slock, lock_flags); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | cmd->sa.host_use_b[0] |= (B_DONE | B_ERROR | B_TIMEOUT); |
| 280 | spin_unlock_irqrestore(&cmd->slock, lock_flags); |
| 281 | |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 282 | writeq_be(rrin, &afu->host_map->ioarrin); |
| 283 | do { |
| 284 | rrin = readq_be(&afu->host_map->ioarrin); |
| 285 | if (rrin != 0x1) |
| 286 | break; |
| 287 | /* Double delay each time */ |
Manoj N. Kumar | ea76543 | 2016-03-25 14:26:49 -0500 | [diff] [blame] | 288 | udelay(1 << nretry); |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 289 | } while (nretry++ < MC_ROOM_RETRY_CNT); |
Uma Krishnan | 3d2f617 | 2016-11-28 18:41:36 -0600 | [diff] [blame] | 290 | |
| 291 | dev_dbg(dev, "%s: returning rrin=0x%016llX nretry=%d\n", |
| 292 | __func__, rrin, nretry); |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
| 296 | * send_cmd() - sends an AFU command |
| 297 | * @afu: AFU associated with the host. |
| 298 | * @cmd: AFU command to send. |
| 299 | * |
| 300 | * Return: |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 301 | * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 302 | */ |
| 303 | static int send_cmd(struct afu *afu, struct afu_cmd *cmd) |
| 304 | { |
| 305 | struct cxlflash_cfg *cfg = afu->parent; |
| 306 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 307 | int rc = 0; |
Uma Krishnan | 11f7b18 | 2016-11-28 18:41:45 -0600 | [diff] [blame] | 308 | s64 room; |
| 309 | ulong lock_flags; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 310 | |
| 311 | /* |
Uma Krishnan | 11f7b18 | 2016-11-28 18:41:45 -0600 | [diff] [blame] | 312 | * To avoid the performance penalty of MMIO, spread the update of |
| 313 | * 'room' over multiple commands. |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 314 | */ |
Uma Krishnan | 11f7b18 | 2016-11-28 18:41:45 -0600 | [diff] [blame] | 315 | spin_lock_irqsave(&afu->rrin_slock, lock_flags); |
| 316 | if (--afu->room < 0) { |
| 317 | room = readq_be(&afu->host_map->cmd_room); |
| 318 | if (room <= 0) { |
| 319 | dev_dbg_ratelimited(dev, "%s: no cmd_room to send " |
| 320 | "0x%02X, room=0x%016llX\n", |
| 321 | __func__, cmd->rcb.cdb[0], room); |
| 322 | afu->room = 0; |
| 323 | rc = SCSI_MLQUEUE_HOST_BUSY; |
| 324 | goto out; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 325 | } |
Uma Krishnan | 11f7b18 | 2016-11-28 18:41:45 -0600 | [diff] [blame] | 326 | afu->room = room - 1; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 327 | } |
| 328 | |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 329 | writeq_be((u64)&cmd->rcb, &afu->host_map->ioarrin); |
| 330 | out: |
Uma Krishnan | 11f7b18 | 2016-11-28 18:41:45 -0600 | [diff] [blame] | 331 | spin_unlock_irqrestore(&afu->rrin_slock, lock_flags); |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 332 | pr_devel("%s: cmd=%p len=%d ea=%p rc=%d\n", __func__, cmd, |
| 333 | cmd->rcb.data_len, (void *)cmd->rcb.data_ea, rc); |
| 334 | return rc; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /** |
| 338 | * wait_resp() - polls for a response or timeout to a sent AFU command |
| 339 | * @afu: AFU associated with the host. |
| 340 | * @cmd: AFU command that was sent. |
| 341 | */ |
| 342 | static void wait_resp(struct afu *afu, struct afu_cmd *cmd) |
| 343 | { |
| 344 | ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000); |
| 345 | |
| 346 | timeout = wait_for_completion_timeout(&cmd->cevent, timeout); |
| 347 | if (!timeout) |
| 348 | context_reset(cmd); |
| 349 | |
| 350 | if (unlikely(cmd->sa.ioasc != 0)) |
| 351 | pr_err("%s: CMD 0x%X failed, IOASC: flags 0x%X, afu_rc 0x%X, " |
| 352 | "scsi_rc 0x%X, fc_rc 0x%X\n", __func__, cmd->rcb.cdb[0], |
| 353 | cmd->sa.rc.flags, cmd->sa.rc.afu_rc, cmd->sa.rc.scsi_rc, |
| 354 | cmd->sa.rc.fc_rc); |
| 355 | } |
| 356 | |
| 357 | /** |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 358 | * send_tmf() - sends a Task Management Function (TMF) |
| 359 | * @afu: AFU to checkout from. |
| 360 | * @scp: SCSI command from stack. |
| 361 | * @tmfcmd: TMF command to send. |
| 362 | * |
| 363 | * Return: |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 364 | * 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 365 | */ |
| 366 | static int send_tmf(struct afu *afu, struct scsi_cmnd *scp, u64 tmfcmd) |
| 367 | { |
| 368 | struct afu_cmd *cmd; |
| 369 | |
| 370 | u32 port_sel = scp->device->channel + 1; |
| 371 | short lflag = 0; |
| 372 | struct Scsi_Host *host = scp->device->host; |
| 373 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata; |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 374 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 375 | ulong lock_flags; |
| 376 | int rc = 0; |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 377 | ulong to; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 378 | |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 379 | cmd = cmd_checkout(afu); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 380 | if (unlikely(!cmd)) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 381 | dev_err(dev, "%s: could not get a free command\n", __func__); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 382 | rc = SCSI_MLQUEUE_HOST_BUSY; |
| 383 | goto out; |
| 384 | } |
| 385 | |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 386 | /* When Task Management Function is active do not send another */ |
| 387 | spin_lock_irqsave(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 388 | if (cfg->tmf_active) |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 389 | wait_event_interruptible_lock_irq(cfg->tmf_waitq, |
| 390 | !cfg->tmf_active, |
| 391 | cfg->tmf_slock); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 392 | cfg->tmf_active = true; |
| 393 | cmd->cmd_tmf = true; |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 394 | spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 395 | |
| 396 | cmd->rcb.ctx_id = afu->ctx_hndl; |
| 397 | cmd->rcb.port_sel = port_sel; |
| 398 | cmd->rcb.lun_id = lun_to_lunid(scp->device->lun); |
| 399 | |
| 400 | lflag = SISL_REQ_FLAGS_TMF_CMD; |
| 401 | |
| 402 | cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID | |
| 403 | SISL_REQ_FLAGS_SUP_UNDERRUN | lflag); |
| 404 | |
| 405 | /* Stash the scp in the reserved field, for reuse during interrupt */ |
| 406 | cmd->rcb.scp = scp; |
| 407 | |
| 408 | /* Copy the CDB from the cmd passed in */ |
| 409 | memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd)); |
| 410 | |
| 411 | /* Send the command */ |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 412 | rc = send_cmd(afu, cmd); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 413 | if (unlikely(rc)) { |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 414 | cmd_checkin(cmd); |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 415 | spin_lock_irqsave(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 416 | cfg->tmf_active = false; |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 417 | spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 418 | goto out; |
| 419 | } |
| 420 | |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 421 | spin_lock_irqsave(&cfg->tmf_slock, lock_flags); |
| 422 | to = msecs_to_jiffies(5000); |
| 423 | to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq, |
| 424 | !cfg->tmf_active, |
| 425 | cfg->tmf_slock, |
| 426 | to); |
| 427 | if (!to) { |
| 428 | cfg->tmf_active = false; |
| 429 | dev_err(dev, "%s: TMF timed out!\n", __func__); |
| 430 | rc = -1; |
| 431 | } |
| 432 | spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 433 | out: |
| 434 | return rc; |
| 435 | } |
| 436 | |
Manoj Kumar | b45cdbaf | 2015-12-14 15:07:23 -0600 | [diff] [blame] | 437 | static void afu_unmap(struct kref *ref) |
| 438 | { |
| 439 | struct afu *afu = container_of(ref, struct afu, mapcount); |
| 440 | |
| 441 | if (likely(afu->afu_map)) { |
| 442 | cxl_psa_unmap((void __iomem *)afu->afu_map); |
| 443 | afu->afu_map = NULL; |
| 444 | } |
| 445 | } |
| 446 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 447 | /** |
| 448 | * cxlflash_driver_info() - information handler for this host driver |
| 449 | * @host: SCSI host associated with device. |
| 450 | * |
| 451 | * Return: A string describing the device. |
| 452 | */ |
| 453 | static const char *cxlflash_driver_info(struct Scsi_Host *host) |
| 454 | { |
| 455 | return CXLFLASH_ADAPTER_NAME; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * cxlflash_queuecommand() - sends a mid-layer request |
| 460 | * @host: SCSI host associated with device. |
| 461 | * @scp: SCSI command to send. |
| 462 | * |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 463 | * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 464 | */ |
| 465 | static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp) |
| 466 | { |
| 467 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata; |
| 468 | struct afu *afu = cfg->afu; |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 469 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 470 | struct afu_cmd *cmd; |
| 471 | u32 port_sel = scp->device->channel + 1; |
| 472 | int nseg, i, ncount; |
| 473 | struct scatterlist *sg; |
| 474 | ulong lock_flags; |
| 475 | short lflag = 0; |
| 476 | int rc = 0; |
Manoj Kumar | b45cdbaf | 2015-12-14 15:07:23 -0600 | [diff] [blame] | 477 | int kref_got = 0; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 478 | |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 479 | dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu " |
| 480 | "cdb=(%08X-%08X-%08X-%08X)\n", |
| 481 | __func__, scp, host->host_no, scp->device->channel, |
| 482 | scp->device->id, scp->device->lun, |
| 483 | get_unaligned_be32(&((u32 *)scp->cmnd)[0]), |
| 484 | get_unaligned_be32(&((u32 *)scp->cmnd)[1]), |
| 485 | get_unaligned_be32(&((u32 *)scp->cmnd)[2]), |
| 486 | get_unaligned_be32(&((u32 *)scp->cmnd)[3])); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 487 | |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 488 | /* |
| 489 | * If a Task Management Function is active, wait for it to complete |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 490 | * before continuing with regular commands. |
| 491 | */ |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 492 | spin_lock_irqsave(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 493 | if (cfg->tmf_active) { |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 494 | spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 495 | rc = SCSI_MLQUEUE_HOST_BUSY; |
| 496 | goto out; |
| 497 | } |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 498 | spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 499 | |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 500 | switch (cfg->state) { |
Matthew R. Ochs | 439e85c | 2015-10-21 15:12:00 -0500 | [diff] [blame] | 501 | case STATE_RESET: |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 502 | dev_dbg_ratelimited(dev, "%s: device is in reset!\n", __func__); |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 503 | rc = SCSI_MLQUEUE_HOST_BUSY; |
| 504 | goto out; |
| 505 | case STATE_FAILTERM: |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 506 | dev_dbg_ratelimited(dev, "%s: device has failed!\n", __func__); |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 507 | scp->result = (DID_NO_CONNECT << 16); |
| 508 | scp->scsi_done(scp); |
| 509 | rc = 0; |
| 510 | goto out; |
| 511 | default: |
| 512 | break; |
| 513 | } |
| 514 | |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 515 | cmd = cmd_checkout(afu); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 516 | if (unlikely(!cmd)) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 517 | dev_err(dev, "%s: could not get a free command\n", __func__); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 518 | rc = SCSI_MLQUEUE_HOST_BUSY; |
| 519 | goto out; |
| 520 | } |
| 521 | |
Manoj Kumar | b45cdbaf | 2015-12-14 15:07:23 -0600 | [diff] [blame] | 522 | kref_get(&cfg->afu->mapcount); |
| 523 | kref_got = 1; |
| 524 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 525 | cmd->rcb.ctx_id = afu->ctx_hndl; |
| 526 | cmd->rcb.port_sel = port_sel; |
| 527 | cmd->rcb.lun_id = lun_to_lunid(scp->device->lun); |
| 528 | |
| 529 | if (scp->sc_data_direction == DMA_TO_DEVICE) |
| 530 | lflag = SISL_REQ_FLAGS_HOST_WRITE; |
| 531 | else |
| 532 | lflag = SISL_REQ_FLAGS_HOST_READ; |
| 533 | |
| 534 | cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID | |
| 535 | SISL_REQ_FLAGS_SUP_UNDERRUN | lflag); |
| 536 | |
| 537 | /* Stash the scp in the reserved field, for reuse during interrupt */ |
| 538 | cmd->rcb.scp = scp; |
| 539 | |
| 540 | nseg = scsi_dma_map(scp); |
| 541 | if (unlikely(nseg < 0)) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 542 | dev_err(dev, "%s: Fail DMA map! nseg=%d\n", |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 543 | __func__, nseg); |
| 544 | rc = SCSI_MLQUEUE_HOST_BUSY; |
| 545 | goto out; |
| 546 | } |
| 547 | |
| 548 | ncount = scsi_sg_count(scp); |
| 549 | scsi_for_each_sg(scp, sg, ncount, i) { |
| 550 | cmd->rcb.data_len = sg_dma_len(sg); |
| 551 | cmd->rcb.data_ea = sg_dma_address(sg); |
| 552 | } |
| 553 | |
| 554 | /* Copy the CDB from the scsi_cmnd passed in */ |
| 555 | memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb)); |
| 556 | |
| 557 | /* Send the command */ |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 558 | rc = send_cmd(afu, cmd); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 559 | if (unlikely(rc)) { |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 560 | cmd_checkin(cmd); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 561 | scsi_dma_unmap(scp); |
| 562 | } |
| 563 | |
| 564 | out: |
Manoj Kumar | b45cdbaf | 2015-12-14 15:07:23 -0600 | [diff] [blame] | 565 | if (kref_got) |
| 566 | kref_put(&afu->mapcount, afu_unmap); |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 567 | pr_devel("%s: returning rc=%d\n", __func__, rc); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 568 | return rc; |
| 569 | } |
| 570 | |
| 571 | /** |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 572 | * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 573 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 574 | */ |
| 575 | static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg) |
| 576 | { |
| 577 | struct pci_dev *pdev = cfg->dev; |
| 578 | |
| 579 | if (pci_channel_offline(pdev)) |
Matthew R. Ochs | 439e85c | 2015-10-21 15:12:00 -0500 | [diff] [blame] | 580 | wait_event_timeout(cfg->reset_waitq, |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 581 | !pci_channel_offline(pdev), |
| 582 | CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT); |
| 583 | } |
| 584 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 585 | /** |
| 586 | * free_mem() - free memory associated with the AFU |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 587 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 588 | */ |
| 589 | static void free_mem(struct cxlflash_cfg *cfg) |
| 590 | { |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 591 | struct afu *afu = cfg->afu; |
| 592 | |
| 593 | if (cfg->afu) { |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 594 | free_pages((ulong)afu, get_order(sizeof(struct afu))); |
| 595 | cfg->afu = NULL; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * stop_afu() - stops the AFU command timers and unmaps the MMIO space |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 601 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 602 | * |
| 603 | * Safe to call with AFU in a partially allocated/initialized state. |
Manoj Kumar | ee91e33 | 2015-12-14 15:07:02 -0600 | [diff] [blame] | 604 | * |
| 605 | * Cleans up all state associated with the command queue, and unmaps |
| 606 | * the MMIO space. |
| 607 | * |
| 608 | * - complete() will take care of commands we initiated (they'll be checked |
| 609 | * in as part of the cleanup that occurs after the completion) |
| 610 | * |
| 611 | * - cmd_checkin() will take care of entries that we did not initiate and that |
| 612 | * have not (and will not) complete because they are sitting on a [now stale] |
| 613 | * hardware queue |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 614 | */ |
| 615 | static void stop_afu(struct cxlflash_cfg *cfg) |
| 616 | { |
| 617 | int i; |
| 618 | struct afu *afu = cfg->afu; |
Manoj Kumar | ee91e33 | 2015-12-14 15:07:02 -0600 | [diff] [blame] | 619 | struct afu_cmd *cmd; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 620 | |
| 621 | if (likely(afu)) { |
Manoj Kumar | ee91e33 | 2015-12-14 15:07:02 -0600 | [diff] [blame] | 622 | for (i = 0; i < CXLFLASH_NUM_CMDS; i++) { |
| 623 | cmd = &afu->cmd[i]; |
| 624 | complete(&cmd->cevent); |
| 625 | if (!atomic_read(&cmd->free)) |
| 626 | cmd_checkin(cmd); |
| 627 | } |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 628 | |
| 629 | if (likely(afu->afu_map)) { |
Matthew R. Ochs | 1786f4a | 2015-10-21 15:14:48 -0500 | [diff] [blame] | 630 | cxl_psa_unmap((void __iomem *)afu->afu_map); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 631 | afu->afu_map = NULL; |
| 632 | } |
Manoj Kumar | b45cdbaf | 2015-12-14 15:07:23 -0600 | [diff] [blame] | 633 | kref_put(&afu->mapcount, afu_unmap); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
| 637 | /** |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 638 | * term_intr() - disables all AFU interrupts |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 639 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 640 | * @level: Depth of allocation, where to begin waterfall tear down. |
| 641 | * |
| 642 | * Safe to call with AFU/MC in partially allocated/initialized state. |
| 643 | */ |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 644 | static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 645 | { |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 646 | struct afu *afu = cfg->afu; |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 647 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 648 | |
| 649 | if (!afu || !cfg->mcctx) { |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 650 | dev_err(dev, "%s: returning with NULL afu or MC\n", __func__); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | |
| 654 | switch (level) { |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 655 | case UNMAP_THREE: |
| 656 | cxl_unmap_afu_irq(cfg->mcctx, 3, afu); |
| 657 | case UNMAP_TWO: |
| 658 | cxl_unmap_afu_irq(cfg->mcctx, 2, afu); |
| 659 | case UNMAP_ONE: |
| 660 | cxl_unmap_afu_irq(cfg->mcctx, 1, afu); |
| 661 | case FREE_IRQ: |
| 662 | cxl_free_afu_irqs(cfg->mcctx); |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 663 | /* fall through */ |
| 664 | case UNDO_NOOP: |
| 665 | /* No action required */ |
| 666 | break; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
| 670 | /** |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 671 | * term_mc() - terminates the master context |
| 672 | * @cfg: Internal structure associated with the host. |
| 673 | * @level: Depth of allocation, where to begin waterfall tear down. |
| 674 | * |
| 675 | * Safe to call with AFU/MC in partially allocated/initialized state. |
| 676 | */ |
| 677 | static void term_mc(struct cxlflash_cfg *cfg) |
| 678 | { |
| 679 | int rc = 0; |
| 680 | struct afu *afu = cfg->afu; |
| 681 | struct device *dev = &cfg->dev->dev; |
| 682 | |
| 683 | if (!afu || !cfg->mcctx) { |
| 684 | dev_err(dev, "%s: returning with NULL afu or MC\n", __func__); |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | rc = cxl_stop_context(cfg->mcctx); |
| 689 | WARN_ON(rc); |
| 690 | cfg->mcctx = NULL; |
| 691 | } |
| 692 | |
| 693 | /** |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 694 | * term_afu() - terminates the AFU |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 695 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 696 | * |
| 697 | * Safe to call with AFU/MC in partially allocated/initialized state. |
| 698 | */ |
| 699 | static void term_afu(struct cxlflash_cfg *cfg) |
| 700 | { |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 701 | /* |
| 702 | * Tear down is carefully orchestrated to ensure |
| 703 | * no interrupts can come in when the problem state |
| 704 | * area is unmapped. |
| 705 | * |
| 706 | * 1) Disable all AFU interrupts |
| 707 | * 2) Unmap the problem state area |
| 708 | * 3) Stop the master context |
| 709 | */ |
| 710 | term_intr(cfg, UNMAP_THREE); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 711 | if (cfg->afu) |
| 712 | stop_afu(cfg); |
| 713 | |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 714 | term_mc(cfg); |
Uma Krishnan | 6ded8b3 | 2016-03-04 15:55:15 -0600 | [diff] [blame] | 715 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 716 | pr_debug("%s: returning\n", __func__); |
| 717 | } |
| 718 | |
| 719 | /** |
Uma Krishnan | 704c4b0 | 2016-06-15 18:49:57 -0500 | [diff] [blame] | 720 | * notify_shutdown() - notifies device of pending shutdown |
| 721 | * @cfg: Internal structure associated with the host. |
| 722 | * @wait: Whether to wait for shutdown processing to complete. |
| 723 | * |
| 724 | * This function will notify the AFU that the adapter is being shutdown |
| 725 | * and will wait for shutdown processing to complete if wait is true. |
| 726 | * This notification should flush pending I/Os to the device and halt |
| 727 | * further I/Os until the next AFU reset is issued and device restarted. |
| 728 | */ |
| 729 | static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait) |
| 730 | { |
| 731 | struct afu *afu = cfg->afu; |
| 732 | struct device *dev = &cfg->dev->dev; |
Uma Krishnan | 1bd2b28 | 2016-07-21 15:44:04 -0500 | [diff] [blame] | 733 | struct sisl_global_map __iomem *global; |
Uma Krishnan | 704c4b0 | 2016-06-15 18:49:57 -0500 | [diff] [blame] | 734 | struct dev_dependent_vals *ddv; |
| 735 | u64 reg, status; |
| 736 | int i, retry_cnt = 0; |
| 737 | |
| 738 | ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data; |
| 739 | if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN)) |
| 740 | return; |
| 741 | |
Uma Krishnan | 1bd2b28 | 2016-07-21 15:44:04 -0500 | [diff] [blame] | 742 | if (!afu || !afu->afu_map) { |
| 743 | dev_dbg(dev, "%s: The problem state area is not mapped\n", |
| 744 | __func__); |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | global = &afu->afu_map->global; |
| 749 | |
Uma Krishnan | 704c4b0 | 2016-06-15 18:49:57 -0500 | [diff] [blame] | 750 | /* Notify AFU */ |
| 751 | for (i = 0; i < NUM_FC_PORTS; i++) { |
| 752 | reg = readq_be(&global->fc_regs[i][FC_CONFIG2 / 8]); |
| 753 | reg |= SISL_FC_SHUTDOWN_NORMAL; |
| 754 | writeq_be(reg, &global->fc_regs[i][FC_CONFIG2 / 8]); |
| 755 | } |
| 756 | |
| 757 | if (!wait) |
| 758 | return; |
| 759 | |
| 760 | /* Wait up to 1.5 seconds for shutdown processing to complete */ |
| 761 | for (i = 0; i < NUM_FC_PORTS; i++) { |
| 762 | retry_cnt = 0; |
| 763 | while (true) { |
| 764 | status = readq_be(&global->fc_regs[i][FC_STATUS / 8]); |
| 765 | if (status & SISL_STATUS_SHUTDOWN_COMPLETE) |
| 766 | break; |
| 767 | if (++retry_cnt >= MC_RETRY_CNT) { |
| 768 | dev_dbg(dev, "%s: port %d shutdown processing " |
| 769 | "not yet completed\n", __func__, i); |
| 770 | break; |
| 771 | } |
| 772 | msleep(100 * retry_cnt); |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | /** |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 778 | * cxlflash_remove() - PCI entry point to tear down host |
| 779 | * @pdev: PCI device associated with the host. |
| 780 | * |
| 781 | * Safe to use as a cleanup in partially allocated/initialized state. |
| 782 | */ |
| 783 | static void cxlflash_remove(struct pci_dev *pdev) |
| 784 | { |
| 785 | struct cxlflash_cfg *cfg = pci_get_drvdata(pdev); |
| 786 | ulong lock_flags; |
| 787 | |
Uma Krishnan | babf985 | 2016-09-02 15:39:16 -0500 | [diff] [blame] | 788 | if (!pci_is_enabled(pdev)) { |
| 789 | pr_debug("%s: Device is disabled\n", __func__); |
| 790 | return; |
| 791 | } |
| 792 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 793 | /* If a Task Management Function is active, wait for it to complete |
| 794 | * before continuing with remove. |
| 795 | */ |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 796 | spin_lock_irqsave(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 797 | if (cfg->tmf_active) |
Matthew R. Ochs | 018d1dc95 | 2015-10-21 15:13:21 -0500 | [diff] [blame] | 798 | wait_event_interruptible_lock_irq(cfg->tmf_waitq, |
| 799 | !cfg->tmf_active, |
| 800 | cfg->tmf_slock); |
| 801 | spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 802 | |
Uma Krishnan | 704c4b0 | 2016-06-15 18:49:57 -0500 | [diff] [blame] | 803 | /* Notify AFU and wait for shutdown processing to complete */ |
| 804 | notify_shutdown(cfg, true); |
| 805 | |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 806 | cfg->state = STATE_FAILTERM; |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 807 | cxlflash_stop_term_user_contexts(cfg); |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 808 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 809 | switch (cfg->init_state) { |
| 810 | case INIT_STATE_SCSI: |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 811 | cxlflash_term_local_luns(cfg); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 812 | scsi_remove_host(cfg->host); |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 813 | /* fall through */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 814 | case INIT_STATE_AFU: |
Matthew R. Ochs | d804621 | 2015-10-21 15:14:17 -0500 | [diff] [blame] | 815 | cancel_work_sync(&cfg->work_q); |
Manoj Kumar | b45cdbaf | 2015-12-14 15:07:23 -0600 | [diff] [blame] | 816 | term_afu(cfg); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 817 | case INIT_STATE_PCI: |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 818 | pci_disable_device(pdev); |
| 819 | case INIT_STATE_NONE: |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 820 | free_mem(cfg); |
Matthew R. Ochs | 8b5b1e8 | 2015-10-21 15:14:09 -0500 | [diff] [blame] | 821 | scsi_host_put(cfg->host); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 822 | break; |
| 823 | } |
| 824 | |
| 825 | pr_debug("%s: returning\n", __func__); |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * alloc_mem() - allocates the AFU and its command pool |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 830 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 831 | * |
| 832 | * A partially allocated state remains on failure. |
| 833 | * |
| 834 | * Return: |
| 835 | * 0 on success |
| 836 | * -ENOMEM on failure to allocate memory |
| 837 | */ |
| 838 | static int alloc_mem(struct cxlflash_cfg *cfg) |
| 839 | { |
| 840 | int rc = 0; |
| 841 | int i; |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 842 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 843 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 844 | /* AFU is ~12k, i.e. only one 64k page or up to four 4k pages */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 845 | cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 846 | get_order(sizeof(struct afu))); |
| 847 | if (unlikely(!cfg->afu)) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 848 | dev_err(dev, "%s: cannot get %d free pages\n", |
| 849 | __func__, get_order(sizeof(struct afu))); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 850 | rc = -ENOMEM; |
| 851 | goto out; |
| 852 | } |
| 853 | cfg->afu->parent = cfg; |
| 854 | cfg->afu->afu_map = NULL; |
| 855 | |
Matthew R. Ochs | e7ab2d40 | 2016-11-28 18:42:01 -0600 | [diff] [blame] | 856 | for (i = 0; i < CXLFLASH_NUM_CMDS; i++) { |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 857 | atomic_set(&cfg->afu->cmd[i].free, 1); |
| 858 | cfg->afu->cmd[i].slot = i; |
| 859 | } |
| 860 | |
| 861 | out: |
| 862 | return rc; |
| 863 | } |
| 864 | |
| 865 | /** |
| 866 | * init_pci() - initializes the host as a PCI device |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 867 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 868 | * |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 869 | * Return: 0 on success, -errno on failure |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 870 | */ |
| 871 | static int init_pci(struct cxlflash_cfg *cfg) |
| 872 | { |
| 873 | struct pci_dev *pdev = cfg->dev; |
| 874 | int rc = 0; |
| 875 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 876 | rc = pci_enable_device(pdev); |
| 877 | if (rc || pci_channel_offline(pdev)) { |
| 878 | if (pci_channel_offline(pdev)) { |
| 879 | cxlflash_wait_for_pci_err_recovery(cfg); |
| 880 | rc = pci_enable_device(pdev); |
| 881 | } |
| 882 | |
| 883 | if (rc) { |
| 884 | dev_err(&pdev->dev, "%s: Cannot enable adapter\n", |
| 885 | __func__); |
| 886 | cxlflash_wait_for_pci_err_recovery(cfg); |
Manoj N. Kumar | 961487e | 2016-03-04 15:55:14 -0600 | [diff] [blame] | 887 | goto out; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 891 | out: |
| 892 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 893 | return rc; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /** |
| 897 | * init_scsi() - adds the host to the SCSI stack and kicks off host scan |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 898 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 899 | * |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 900 | * Return: 0 on success, -errno on failure |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 901 | */ |
| 902 | static int init_scsi(struct cxlflash_cfg *cfg) |
| 903 | { |
| 904 | struct pci_dev *pdev = cfg->dev; |
| 905 | int rc = 0; |
| 906 | |
| 907 | rc = scsi_add_host(cfg->host, &pdev->dev); |
| 908 | if (rc) { |
| 909 | dev_err(&pdev->dev, "%s: scsi_add_host failed (rc=%d)\n", |
| 910 | __func__, rc); |
| 911 | goto out; |
| 912 | } |
| 913 | |
| 914 | scsi_scan_host(cfg->host); |
| 915 | |
| 916 | out: |
| 917 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 918 | return rc; |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * set_port_online() - transitions the specified host FC port to online state |
| 923 | * @fc_regs: Top of MMIO region defined for specified port. |
| 924 | * |
| 925 | * The provided MMIO region must be mapped prior to call. Online state means |
| 926 | * that the FC link layer has synced, completed the handshaking process, and |
| 927 | * is ready for login to start. |
| 928 | */ |
Matthew R. Ochs | 1786f4a | 2015-10-21 15:14:48 -0500 | [diff] [blame] | 929 | static void set_port_online(__be64 __iomem *fc_regs) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 930 | { |
| 931 | u64 cmdcfg; |
| 932 | |
| 933 | cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]); |
| 934 | cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE); /* clear OFF_LINE */ |
| 935 | cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE); /* set ON_LINE */ |
| 936 | writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]); |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * set_port_offline() - transitions the specified host FC port to offline state |
| 941 | * @fc_regs: Top of MMIO region defined for specified port. |
| 942 | * |
| 943 | * The provided MMIO region must be mapped prior to call. |
| 944 | */ |
Matthew R. Ochs | 1786f4a | 2015-10-21 15:14:48 -0500 | [diff] [blame] | 945 | static void set_port_offline(__be64 __iomem *fc_regs) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 946 | { |
| 947 | u64 cmdcfg; |
| 948 | |
| 949 | cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]); |
| 950 | cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE); /* clear ON_LINE */ |
| 951 | cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE); /* set OFF_LINE */ |
| 952 | writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]); |
| 953 | } |
| 954 | |
| 955 | /** |
| 956 | * wait_port_online() - waits for the specified host FC port come online |
| 957 | * @fc_regs: Top of MMIO region defined for specified port. |
| 958 | * @delay_us: Number of microseconds to delay between reading port status. |
| 959 | * @nretry: Number of cycles to retry reading port status. |
| 960 | * |
| 961 | * The provided MMIO region must be mapped prior to call. This will timeout |
| 962 | * when the cable is not plugged in. |
| 963 | * |
| 964 | * Return: |
| 965 | * TRUE (1) when the specified port is online |
| 966 | * FALSE (0) when the specified port fails to come online after timeout |
| 967 | * -EINVAL when @delay_us is less than 1000 |
| 968 | */ |
Matthew R. Ochs | 1786f4a | 2015-10-21 15:14:48 -0500 | [diff] [blame] | 969 | static int wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 970 | { |
| 971 | u64 status; |
| 972 | |
| 973 | if (delay_us < 1000) { |
| 974 | pr_err("%s: invalid delay specified %d\n", __func__, delay_us); |
| 975 | return -EINVAL; |
| 976 | } |
| 977 | |
| 978 | do { |
| 979 | msleep(delay_us / 1000); |
| 980 | status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]); |
Matthew R. Ochs | 05dab43 | 2016-09-02 15:40:03 -0500 | [diff] [blame] | 981 | if (status == U64_MAX) |
| 982 | nretry /= 2; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 983 | } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE && |
| 984 | nretry--); |
| 985 | |
| 986 | return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE); |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * wait_port_offline() - waits for the specified host FC port go offline |
| 991 | * @fc_regs: Top of MMIO region defined for specified port. |
| 992 | * @delay_us: Number of microseconds to delay between reading port status. |
| 993 | * @nretry: Number of cycles to retry reading port status. |
| 994 | * |
| 995 | * The provided MMIO region must be mapped prior to call. |
| 996 | * |
| 997 | * Return: |
| 998 | * TRUE (1) when the specified port is offline |
| 999 | * FALSE (0) when the specified port fails to go offline after timeout |
| 1000 | * -EINVAL when @delay_us is less than 1000 |
| 1001 | */ |
Matthew R. Ochs | 1786f4a | 2015-10-21 15:14:48 -0500 | [diff] [blame] | 1002 | static int wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1003 | { |
| 1004 | u64 status; |
| 1005 | |
| 1006 | if (delay_us < 1000) { |
| 1007 | pr_err("%s: invalid delay specified %d\n", __func__, delay_us); |
| 1008 | return -EINVAL; |
| 1009 | } |
| 1010 | |
| 1011 | do { |
| 1012 | msleep(delay_us / 1000); |
| 1013 | status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]); |
Matthew R. Ochs | 05dab43 | 2016-09-02 15:40:03 -0500 | [diff] [blame] | 1014 | if (status == U64_MAX) |
| 1015 | nretry /= 2; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1016 | } while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE && |
| 1017 | nretry--); |
| 1018 | |
| 1019 | return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE); |
| 1020 | } |
| 1021 | |
| 1022 | /** |
| 1023 | * afu_set_wwpn() - configures the WWPN for the specified host FC port |
| 1024 | * @afu: AFU associated with the host that owns the specified FC port. |
| 1025 | * @port: Port number being configured. |
| 1026 | * @fc_regs: Top of MMIO region defined for specified port. |
| 1027 | * @wwpn: The world-wide-port-number previously discovered for port. |
| 1028 | * |
| 1029 | * The provided MMIO region must be mapped prior to call. As part of the |
| 1030 | * sequence to configure the WWPN, the port is toggled offline and then back |
| 1031 | * online. This toggling action can cause this routine to delay up to a few |
| 1032 | * seconds. When configured to use the internal LUN feature of the AFU, a |
| 1033 | * failure to come online is overridden. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1034 | */ |
Matthew R. Ochs | f801326 | 2016-09-02 15:40:20 -0500 | [diff] [blame] | 1035 | static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs, |
| 1036 | u64 wwpn) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1037 | { |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1038 | set_port_offline(fc_regs); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1039 | if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US, |
| 1040 | FC_PORT_STATUS_RETRY_CNT)) { |
| 1041 | pr_debug("%s: wait on port %d to go offline timed out\n", |
| 1042 | __func__, port); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1043 | } |
| 1044 | |
Matthew R. Ochs | f801326 | 2016-09-02 15:40:20 -0500 | [diff] [blame] | 1045 | writeq_be(wwpn, &fc_regs[FC_PNAME / 8]); |
Matthew R. Ochs | 964497b | 2015-10-21 15:13:54 -0500 | [diff] [blame] | 1046 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1047 | set_port_online(fc_regs); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1048 | if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US, |
| 1049 | FC_PORT_STATUS_RETRY_CNT)) { |
Matthew R. Ochs | f801326 | 2016-09-02 15:40:20 -0500 | [diff] [blame] | 1050 | pr_debug("%s: wait on port %d to go online timed out\n", |
| 1051 | __func__, port); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1052 | } |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * afu_link_reset() - resets the specified host FC port |
| 1057 | * @afu: AFU associated with the host that owns the specified FC port. |
| 1058 | * @port: Port number being configured. |
| 1059 | * @fc_regs: Top of MMIO region defined for specified port. |
| 1060 | * |
| 1061 | * The provided MMIO region must be mapped prior to call. The sequence to |
| 1062 | * reset the port involves toggling it offline and then back online. This |
| 1063 | * action can cause this routine to delay up to a few seconds. An effort |
| 1064 | * is made to maintain link with the device by switching to host to use |
| 1065 | * the alternate port exclusively while the reset takes place. |
| 1066 | * failure to come online is overridden. |
| 1067 | */ |
Matthew R. Ochs | 1786f4a | 2015-10-21 15:14:48 -0500 | [diff] [blame] | 1068 | static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1069 | { |
| 1070 | u64 port_sel; |
| 1071 | |
| 1072 | /* first switch the AFU to the other links, if any */ |
| 1073 | port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel); |
Dan Carpenter | 4da74db | 2015-08-18 11:57:43 +0300 | [diff] [blame] | 1074 | port_sel &= ~(1ULL << port); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1075 | writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel); |
| 1076 | cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC); |
| 1077 | |
| 1078 | set_port_offline(fc_regs); |
| 1079 | if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US, |
| 1080 | FC_PORT_STATUS_RETRY_CNT)) |
| 1081 | pr_err("%s: wait on port %d to go offline timed out\n", |
| 1082 | __func__, port); |
| 1083 | |
| 1084 | set_port_online(fc_regs); |
| 1085 | if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US, |
| 1086 | FC_PORT_STATUS_RETRY_CNT)) |
| 1087 | pr_err("%s: wait on port %d to go online timed out\n", |
| 1088 | __func__, port); |
| 1089 | |
| 1090 | /* switch back to include this port */ |
Dan Carpenter | 4da74db | 2015-08-18 11:57:43 +0300 | [diff] [blame] | 1091 | port_sel |= (1ULL << port); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1092 | writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel); |
| 1093 | cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC); |
| 1094 | |
| 1095 | pr_debug("%s: returning port_sel=%lld\n", __func__, port_sel); |
| 1096 | } |
| 1097 | |
| 1098 | /* |
| 1099 | * Asynchronous interrupt information table |
| 1100 | */ |
| 1101 | static const struct asyc_intr_info ainfo[] = { |
| 1102 | {SISL_ASTATUS_FC0_OTHER, "other error", 0, CLR_FC_ERROR | LINK_RESET}, |
| 1103 | {SISL_ASTATUS_FC0_LOGO, "target initiated LOGO", 0, 0}, |
| 1104 | {SISL_ASTATUS_FC0_CRC_T, "CRC threshold exceeded", 0, LINK_RESET}, |
Manoj Kumar | e6e6df3 | 2015-10-21 15:16:07 -0500 | [diff] [blame] | 1105 | {SISL_ASTATUS_FC0_LOGI_R, "login timed out, retrying", 0, LINK_RESET}, |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1106 | {SISL_ASTATUS_FC0_LOGI_F, "login failed", 0, CLR_FC_ERROR}, |
Matthew R. Ochs | ef51074 | 2015-10-21 15:13:37 -0500 | [diff] [blame] | 1107 | {SISL_ASTATUS_FC0_LOGI_S, "login succeeded", 0, SCAN_HOST}, |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1108 | {SISL_ASTATUS_FC0_LINK_DN, "link down", 0, 0}, |
Uma Krishnan | bbbfae9 | 2016-09-02 15:38:48 -0500 | [diff] [blame] | 1109 | {SISL_ASTATUS_FC0_LINK_UP, "link up", 0, 0}, |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1110 | {SISL_ASTATUS_FC1_OTHER, "other error", 1, CLR_FC_ERROR | LINK_RESET}, |
| 1111 | {SISL_ASTATUS_FC1_LOGO, "target initiated LOGO", 1, 0}, |
| 1112 | {SISL_ASTATUS_FC1_CRC_T, "CRC threshold exceeded", 1, LINK_RESET}, |
Manoj Kumar | a9be294 | 2015-12-14 14:55:09 -0600 | [diff] [blame] | 1113 | {SISL_ASTATUS_FC1_LOGI_R, "login timed out, retrying", 1, LINK_RESET}, |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1114 | {SISL_ASTATUS_FC1_LOGI_F, "login failed", 1, CLR_FC_ERROR}, |
Matthew R. Ochs | ef51074 | 2015-10-21 15:13:37 -0500 | [diff] [blame] | 1115 | {SISL_ASTATUS_FC1_LOGI_S, "login succeeded", 1, SCAN_HOST}, |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1116 | {SISL_ASTATUS_FC1_LINK_DN, "link down", 1, 0}, |
Uma Krishnan | bbbfae9 | 2016-09-02 15:38:48 -0500 | [diff] [blame] | 1117 | {SISL_ASTATUS_FC1_LINK_UP, "link up", 1, 0}, |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1118 | {0x0, "", 0, 0} /* terminator */ |
| 1119 | }; |
| 1120 | |
| 1121 | /** |
| 1122 | * find_ainfo() - locates and returns asynchronous interrupt information |
| 1123 | * @status: Status code set by AFU on error. |
| 1124 | * |
| 1125 | * Return: The located information or NULL when the status code is invalid. |
| 1126 | */ |
| 1127 | static const struct asyc_intr_info *find_ainfo(u64 status) |
| 1128 | { |
| 1129 | const struct asyc_intr_info *info; |
| 1130 | |
| 1131 | for (info = &ainfo[0]; info->status; info++) |
| 1132 | if (info->status == status) |
| 1133 | return info; |
| 1134 | |
| 1135 | return NULL; |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * afu_err_intr_init() - clears and initializes the AFU for error interrupts |
| 1140 | * @afu: AFU associated with the host. |
| 1141 | */ |
| 1142 | static void afu_err_intr_init(struct afu *afu) |
| 1143 | { |
| 1144 | int i; |
| 1145 | u64 reg; |
| 1146 | |
| 1147 | /* global async interrupts: AFU clears afu_ctrl on context exit |
| 1148 | * if async interrupts were sent to that context. This prevents |
| 1149 | * the AFU form sending further async interrupts when |
| 1150 | * there is |
| 1151 | * nobody to receive them. |
| 1152 | */ |
| 1153 | |
| 1154 | /* mask all */ |
| 1155 | writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask); |
| 1156 | /* set LISN# to send and point to master context */ |
| 1157 | reg = ((u64) (((afu->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40); |
| 1158 | |
| 1159 | if (afu->internal_lun) |
| 1160 | reg |= 1; /* Bit 63 indicates local lun */ |
| 1161 | writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl); |
| 1162 | /* clear all */ |
| 1163 | writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear); |
| 1164 | /* unmask bits that are of interest */ |
| 1165 | /* note: afu can send an interrupt after this step */ |
| 1166 | writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask); |
| 1167 | /* clear again in case a bit came on after previous clear but before */ |
| 1168 | /* unmask */ |
| 1169 | writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear); |
| 1170 | |
| 1171 | /* Clear/Set internal lun bits */ |
| 1172 | reg = readq_be(&afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]); |
| 1173 | reg &= SISL_FC_INTERNAL_MASK; |
| 1174 | if (afu->internal_lun) |
| 1175 | reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT); |
| 1176 | writeq_be(reg, &afu->afu_map->global.fc_regs[0][FC_CONFIG2 / 8]); |
| 1177 | |
| 1178 | /* now clear FC errors */ |
| 1179 | for (i = 0; i < NUM_FC_PORTS; i++) { |
| 1180 | writeq_be(0xFFFFFFFFU, |
| 1181 | &afu->afu_map->global.fc_regs[i][FC_ERROR / 8]); |
| 1182 | writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRCAP / 8]); |
| 1183 | } |
| 1184 | |
| 1185 | /* sync interrupts for master's IOARRIN write */ |
| 1186 | /* note that unlike asyncs, there can be no pending sync interrupts */ |
| 1187 | /* at this time (this is a fresh context and master has not written */ |
| 1188 | /* IOARRIN yet), so there is nothing to clear. */ |
| 1189 | |
| 1190 | /* set LISN#, it is always sent to the context that wrote IOARRIN */ |
| 1191 | writeq_be(SISL_MSI_SYNC_ERROR, &afu->host_map->ctx_ctrl); |
| 1192 | writeq_be(SISL_ISTATUS_MASK, &afu->host_map->intr_mask); |
| 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * cxlflash_sync_err_irq() - interrupt handler for synchronous errors |
| 1197 | * @irq: Interrupt number. |
| 1198 | * @data: Private data provided at interrupt registration, the AFU. |
| 1199 | * |
| 1200 | * Return: Always return IRQ_HANDLED. |
| 1201 | */ |
| 1202 | static irqreturn_t cxlflash_sync_err_irq(int irq, void *data) |
| 1203 | { |
| 1204 | struct afu *afu = (struct afu *)data; |
| 1205 | u64 reg; |
| 1206 | u64 reg_unmasked; |
| 1207 | |
| 1208 | reg = readq_be(&afu->host_map->intr_status); |
| 1209 | reg_unmasked = (reg & SISL_ISTATUS_UNMASK); |
| 1210 | |
| 1211 | if (reg_unmasked == 0UL) { |
| 1212 | pr_err("%s: %llX: spurious interrupt, intr_status %016llX\n", |
| 1213 | __func__, (u64)afu, reg); |
| 1214 | goto cxlflash_sync_err_irq_exit; |
| 1215 | } |
| 1216 | |
| 1217 | pr_err("%s: %llX: unexpected interrupt, intr_status %016llX\n", |
| 1218 | __func__, (u64)afu, reg); |
| 1219 | |
| 1220 | writeq_be(reg_unmasked, &afu->host_map->intr_clear); |
| 1221 | |
| 1222 | cxlflash_sync_err_irq_exit: |
| 1223 | pr_debug("%s: returning rc=%d\n", __func__, IRQ_HANDLED); |
| 1224 | return IRQ_HANDLED; |
| 1225 | } |
| 1226 | |
| 1227 | /** |
| 1228 | * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path) |
| 1229 | * @irq: Interrupt number. |
| 1230 | * @data: Private data provided at interrupt registration, the AFU. |
| 1231 | * |
| 1232 | * Return: Always return IRQ_HANDLED. |
| 1233 | */ |
| 1234 | static irqreturn_t cxlflash_rrq_irq(int irq, void *data) |
| 1235 | { |
| 1236 | struct afu *afu = (struct afu *)data; |
| 1237 | struct afu_cmd *cmd; |
| 1238 | bool toggle = afu->toggle; |
| 1239 | u64 entry, |
| 1240 | *hrrq_start = afu->hrrq_start, |
| 1241 | *hrrq_end = afu->hrrq_end, |
| 1242 | *hrrq_curr = afu->hrrq_curr; |
| 1243 | |
| 1244 | /* Process however many RRQ entries that are ready */ |
| 1245 | while (true) { |
| 1246 | entry = *hrrq_curr; |
| 1247 | |
| 1248 | if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle) |
| 1249 | break; |
| 1250 | |
| 1251 | cmd = (struct afu_cmd *)(entry & ~SISL_RESP_HANDLE_T_BIT); |
| 1252 | cmd_complete(cmd); |
| 1253 | |
| 1254 | /* Advance to next entry or wrap and flip the toggle bit */ |
| 1255 | if (hrrq_curr < hrrq_end) |
| 1256 | hrrq_curr++; |
| 1257 | else { |
| 1258 | hrrq_curr = hrrq_start; |
| 1259 | toggle ^= SISL_RESP_HANDLE_T_BIT; |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | afu->hrrq_curr = hrrq_curr; |
| 1264 | afu->toggle = toggle; |
| 1265 | |
| 1266 | return IRQ_HANDLED; |
| 1267 | } |
| 1268 | |
| 1269 | /** |
| 1270 | * cxlflash_async_err_irq() - interrupt handler for asynchronous errors |
| 1271 | * @irq: Interrupt number. |
| 1272 | * @data: Private data provided at interrupt registration, the AFU. |
| 1273 | * |
| 1274 | * Return: Always return IRQ_HANDLED. |
| 1275 | */ |
| 1276 | static irqreturn_t cxlflash_async_err_irq(int irq, void *data) |
| 1277 | { |
| 1278 | struct afu *afu = (struct afu *)data; |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1279 | struct cxlflash_cfg *cfg = afu->parent; |
| 1280 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1281 | u64 reg_unmasked; |
| 1282 | const struct asyc_intr_info *info; |
Matthew R. Ochs | 1786f4a | 2015-10-21 15:14:48 -0500 | [diff] [blame] | 1283 | struct sisl_global_map __iomem *global = &afu->afu_map->global; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1284 | u64 reg; |
| 1285 | u8 port; |
| 1286 | int i; |
| 1287 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1288 | reg = readq_be(&global->regs.aintr_status); |
| 1289 | reg_unmasked = (reg & SISL_ASTATUS_UNMASK); |
| 1290 | |
| 1291 | if (reg_unmasked == 0) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1292 | dev_err(dev, "%s: spurious interrupt, aintr_status 0x%016llX\n", |
| 1293 | __func__, reg); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1294 | goto out; |
| 1295 | } |
| 1296 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1297 | /* FYI, it is 'okay' to clear AFU status before FC_ERROR */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1298 | writeq_be(reg_unmasked, &global->regs.aintr_clear); |
| 1299 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1300 | /* Check each bit that is on */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1301 | for (i = 0; reg_unmasked; i++, reg_unmasked = (reg_unmasked >> 1)) { |
| 1302 | info = find_ainfo(1ULL << i); |
Matthew R. Ochs | 16798d3 | 2015-10-21 15:13:45 -0500 | [diff] [blame] | 1303 | if (((reg_unmasked & 0x1) == 0) || !info) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1304 | continue; |
| 1305 | |
| 1306 | port = info->port; |
| 1307 | |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1308 | dev_err(dev, "%s: FC Port %d -> %s, fc_status 0x%08llX\n", |
| 1309 | __func__, port, info->desc, |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1310 | readq_be(&global->fc_regs[port][FC_STATUS / 8])); |
| 1311 | |
| 1312 | /* |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1313 | * Do link reset first, some OTHER errors will set FC_ERROR |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1314 | * again if cleared before or w/o a reset |
| 1315 | */ |
| 1316 | if (info->action & LINK_RESET) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1317 | dev_err(dev, "%s: FC Port %d: resetting link\n", |
| 1318 | __func__, port); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1319 | cfg->lr_state = LINK_RESET_REQUIRED; |
| 1320 | cfg->lr_port = port; |
Manoj Kumar | b45cdbaf | 2015-12-14 15:07:23 -0600 | [diff] [blame] | 1321 | kref_get(&cfg->afu->mapcount); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1322 | schedule_work(&cfg->work_q); |
| 1323 | } |
| 1324 | |
| 1325 | if (info->action & CLR_FC_ERROR) { |
| 1326 | reg = readq_be(&global->fc_regs[port][FC_ERROR / 8]); |
| 1327 | |
| 1328 | /* |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1329 | * Since all errors are unmasked, FC_ERROR and FC_ERRCAP |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1330 | * should be the same and tracing one is sufficient. |
| 1331 | */ |
| 1332 | |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1333 | dev_err(dev, "%s: fc %d: clearing fc_error 0x%08llX\n", |
| 1334 | __func__, port, reg); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1335 | |
| 1336 | writeq_be(reg, &global->fc_regs[port][FC_ERROR / 8]); |
| 1337 | writeq_be(0, &global->fc_regs[port][FC_ERRCAP / 8]); |
| 1338 | } |
Matthew R. Ochs | ef51074 | 2015-10-21 15:13:37 -0500 | [diff] [blame] | 1339 | |
| 1340 | if (info->action & SCAN_HOST) { |
| 1341 | atomic_inc(&cfg->scan_host_needed); |
Manoj Kumar | b45cdbaf | 2015-12-14 15:07:23 -0600 | [diff] [blame] | 1342 | kref_get(&cfg->afu->mapcount); |
Matthew R. Ochs | ef51074 | 2015-10-21 15:13:37 -0500 | [diff] [blame] | 1343 | schedule_work(&cfg->work_q); |
| 1344 | } |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | out: |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1348 | dev_dbg(dev, "%s: returning IRQ_HANDLED, afu=%p\n", __func__, afu); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1349 | return IRQ_HANDLED; |
| 1350 | } |
| 1351 | |
| 1352 | /** |
| 1353 | * start_context() - starts the master context |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 1354 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1355 | * |
| 1356 | * Return: A success or failure value from CXL services. |
| 1357 | */ |
| 1358 | static int start_context(struct cxlflash_cfg *cfg) |
| 1359 | { |
| 1360 | int rc = 0; |
| 1361 | |
| 1362 | rc = cxl_start_context(cfg->mcctx, |
| 1363 | cfg->afu->work.work_element_descriptor, |
| 1364 | NULL); |
| 1365 | |
| 1366 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 1367 | return rc; |
| 1368 | } |
| 1369 | |
| 1370 | /** |
| 1371 | * read_vpd() - obtains the WWPNs from VPD |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 1372 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1373 | * @wwpn: Array of size NUM_FC_PORTS to pass back WWPNs |
| 1374 | * |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 1375 | * Return: 0 on success, -errno on failure |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1376 | */ |
| 1377 | static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[]) |
| 1378 | { |
Frederic Barrat | ca946d4e | 2016-03-04 12:26:43 +0100 | [diff] [blame] | 1379 | struct pci_dev *dev = cfg->dev; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1380 | int rc = 0; |
| 1381 | int ro_start, ro_size, i, j, k; |
| 1382 | ssize_t vpd_size; |
| 1383 | char vpd_data[CXLFLASH_VPD_LEN]; |
| 1384 | char tmp_buf[WWPN_BUF_LEN] = { 0 }; |
| 1385 | char *wwpn_vpd_tags[NUM_FC_PORTS] = { "V5", "V6" }; |
| 1386 | |
| 1387 | /* Get the VPD data from the device */ |
Frederic Barrat | ca946d4e | 2016-03-04 12:26:43 +0100 | [diff] [blame] | 1388 | vpd_size = cxl_read_adapter_vpd(dev, vpd_data, sizeof(vpd_data)); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1389 | if (unlikely(vpd_size <= 0)) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1390 | dev_err(&dev->dev, "%s: Unable to read VPD (size = %ld)\n", |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1391 | __func__, vpd_size); |
| 1392 | rc = -ENODEV; |
| 1393 | goto out; |
| 1394 | } |
| 1395 | |
| 1396 | /* Get the read only section offset */ |
| 1397 | ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, |
| 1398 | PCI_VPD_LRDT_RO_DATA); |
| 1399 | if (unlikely(ro_start < 0)) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1400 | dev_err(&dev->dev, "%s: VPD Read-only data not found\n", |
| 1401 | __func__); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1402 | rc = -ENODEV; |
| 1403 | goto out; |
| 1404 | } |
| 1405 | |
| 1406 | /* Get the read only section size, cap when extends beyond read VPD */ |
| 1407 | ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]); |
| 1408 | j = ro_size; |
| 1409 | i = ro_start + PCI_VPD_LRDT_TAG_SIZE; |
| 1410 | if (unlikely((i + j) > vpd_size)) { |
| 1411 | pr_debug("%s: Might need to read more VPD (%d > %ld)\n", |
| 1412 | __func__, (i + j), vpd_size); |
| 1413 | ro_size = vpd_size - i; |
| 1414 | } |
| 1415 | |
| 1416 | /* |
| 1417 | * Find the offset of the WWPN tag within the read only |
| 1418 | * VPD data and validate the found field (partials are |
| 1419 | * no good to us). Convert the ASCII data to an integer |
| 1420 | * value. Note that we must copy to a temporary buffer |
| 1421 | * because the conversion service requires that the ASCII |
| 1422 | * string be terminated. |
| 1423 | */ |
| 1424 | for (k = 0; k < NUM_FC_PORTS; k++) { |
| 1425 | j = ro_size; |
| 1426 | i = ro_start + PCI_VPD_LRDT_TAG_SIZE; |
| 1427 | |
| 1428 | i = pci_vpd_find_info_keyword(vpd_data, i, j, wwpn_vpd_tags[k]); |
| 1429 | if (unlikely(i < 0)) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1430 | dev_err(&dev->dev, "%s: Port %d WWPN not found " |
| 1431 | "in VPD\n", __func__, k); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1432 | rc = -ENODEV; |
| 1433 | goto out; |
| 1434 | } |
| 1435 | |
| 1436 | j = pci_vpd_info_field_size(&vpd_data[i]); |
| 1437 | i += PCI_VPD_INFO_FLD_HDR_SIZE; |
| 1438 | if (unlikely((i + j > vpd_size) || (j != WWPN_LEN))) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1439 | dev_err(&dev->dev, "%s: Port %d WWPN incomplete or " |
| 1440 | "VPD corrupt\n", |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1441 | __func__, k); |
| 1442 | rc = -ENODEV; |
| 1443 | goto out; |
| 1444 | } |
| 1445 | |
| 1446 | memcpy(tmp_buf, &vpd_data[i], WWPN_LEN); |
| 1447 | rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]); |
| 1448 | if (unlikely(rc)) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1449 | dev_err(&dev->dev, "%s: Fail to convert port %d WWPN " |
| 1450 | "to integer\n", __func__, k); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1451 | rc = -ENODEV; |
| 1452 | goto out; |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | out: |
| 1457 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 1458 | return rc; |
| 1459 | } |
| 1460 | |
| 1461 | /** |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1462 | * init_pcr() - initialize the provisioning and control registers |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 1463 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1464 | * |
| 1465 | * Also sets up fast access to the mapped registers and initializes AFU |
| 1466 | * command fields that never change. |
| 1467 | */ |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 1468 | static void init_pcr(struct cxlflash_cfg *cfg) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1469 | { |
| 1470 | struct afu *afu = cfg->afu; |
Matthew R. Ochs | 1786f4a | 2015-10-21 15:14:48 -0500 | [diff] [blame] | 1471 | struct sisl_ctrl_map __iomem *ctrl_map; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1472 | int i; |
| 1473 | |
| 1474 | for (i = 0; i < MAX_CONTEXT; i++) { |
| 1475 | ctrl_map = &afu->afu_map->ctrls[i].ctrl; |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1476 | /* Disrupt any clients that could be running */ |
| 1477 | /* e.g. clients that survived a master restart */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1478 | writeq_be(0, &ctrl_map->rht_start); |
| 1479 | writeq_be(0, &ctrl_map->rht_cnt_id); |
| 1480 | writeq_be(0, &ctrl_map->ctx_cap); |
| 1481 | } |
| 1482 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1483 | /* Copy frequently used fields into afu */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1484 | afu->ctx_hndl = (u16) cxl_process_element(cfg->mcctx); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1485 | afu->host_map = &afu->afu_map->hosts[afu->ctx_hndl].host; |
| 1486 | afu->ctrl_map = &afu->afu_map->ctrls[afu->ctx_hndl].ctrl; |
| 1487 | |
| 1488 | /* Program the Endian Control for the master context */ |
| 1489 | writeq_be(SISL_ENDIAN_CTRL, &afu->host_map->endian_ctrl); |
| 1490 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1491 | /* Initialize cmd fields that never change */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1492 | for (i = 0; i < CXLFLASH_NUM_CMDS; i++) { |
| 1493 | afu->cmd[i].rcb.ctx_id = afu->ctx_hndl; |
| 1494 | afu->cmd[i].rcb.msi = SISL_MSI_RRQ_UPDATED; |
| 1495 | afu->cmd[i].rcb.rrq = 0x0; |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | /** |
| 1500 | * init_global() - initialize AFU global registers |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 1501 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1502 | */ |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 1503 | static int init_global(struct cxlflash_cfg *cfg) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1504 | { |
| 1505 | struct afu *afu = cfg->afu; |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1506 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1507 | u64 wwpn[NUM_FC_PORTS]; /* wwpn of AFU ports */ |
| 1508 | int i = 0, num_ports = 0; |
| 1509 | int rc = 0; |
| 1510 | u64 reg; |
| 1511 | |
| 1512 | rc = read_vpd(cfg, &wwpn[0]); |
| 1513 | if (rc) { |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1514 | dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1515 | goto out; |
| 1516 | } |
| 1517 | |
| 1518 | pr_debug("%s: wwpn0=0x%llX wwpn1=0x%llX\n", __func__, wwpn[0], wwpn[1]); |
| 1519 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1520 | /* Set up RRQ in AFU for master issued cmds */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1521 | writeq_be((u64) afu->hrrq_start, &afu->host_map->rrq_start); |
| 1522 | writeq_be((u64) afu->hrrq_end, &afu->host_map->rrq_end); |
| 1523 | |
| 1524 | /* AFU configuration */ |
| 1525 | reg = readq_be(&afu->afu_map->global.regs.afu_config); |
| 1526 | reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN; |
| 1527 | /* enable all auto retry options and control endianness */ |
| 1528 | /* leave others at default: */ |
| 1529 | /* CTX_CAP write protected, mbox_r does not clear on read and */ |
| 1530 | /* checker on if dual afu */ |
| 1531 | writeq_be(reg, &afu->afu_map->global.regs.afu_config); |
| 1532 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1533 | /* Global port select: select either port */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1534 | if (afu->internal_lun) { |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1535 | /* Only use port 0 */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1536 | writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel); |
| 1537 | num_ports = NUM_FC_PORTS - 1; |
| 1538 | } else { |
| 1539 | writeq_be(BOTH_PORTS, &afu->afu_map->global.regs.afu_port_sel); |
| 1540 | num_ports = NUM_FC_PORTS; |
| 1541 | } |
| 1542 | |
| 1543 | for (i = 0; i < num_ports; i++) { |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1544 | /* Unmask all errors (but they are still masked at AFU) */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1545 | writeq_be(0, &afu->afu_map->global.fc_regs[i][FC_ERRMSK / 8]); |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1546 | /* Clear CRC error cnt & set a threshold */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1547 | (void)readq_be(&afu->afu_map->global. |
| 1548 | fc_regs[i][FC_CNT_CRCERR / 8]); |
| 1549 | writeq_be(MC_CRC_THRESH, &afu->afu_map->global.fc_regs[i] |
| 1550 | [FC_CRC_THRESH / 8]); |
| 1551 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1552 | /* Set WWPNs. If already programmed, wwpn[i] is 0 */ |
Matthew R. Ochs | f801326 | 2016-09-02 15:40:20 -0500 | [diff] [blame] | 1553 | if (wwpn[i] != 0) |
| 1554 | afu_set_wwpn(afu, i, |
| 1555 | &afu->afu_map->global.fc_regs[i][0], |
| 1556 | wwpn[i]); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1557 | /* Programming WWPN back to back causes additional |
| 1558 | * offline/online transitions and a PLOGI |
| 1559 | */ |
| 1560 | msleep(100); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1561 | } |
| 1562 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1563 | /* Set up master's own CTX_CAP to allow real mode, host translation */ |
| 1564 | /* tables, afu cmds and read/write GSCSI cmds. */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1565 | /* First, unlock ctx_cap write by reading mbox */ |
| 1566 | (void)readq_be(&afu->ctrl_map->mbox_r); /* unlock ctx_cap */ |
| 1567 | writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE | |
| 1568 | SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD | |
| 1569 | SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD), |
| 1570 | &afu->ctrl_map->ctx_cap); |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1571 | /* Initialize heartbeat */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1572 | afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb); |
| 1573 | |
| 1574 | out: |
| 1575 | return rc; |
| 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | * start_afu() - initializes and starts the AFU |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 1580 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1581 | */ |
| 1582 | static int start_afu(struct cxlflash_cfg *cfg) |
| 1583 | { |
| 1584 | struct afu *afu = cfg->afu; |
| 1585 | struct afu_cmd *cmd; |
| 1586 | |
| 1587 | int i = 0; |
| 1588 | int rc = 0; |
| 1589 | |
| 1590 | for (i = 0; i < CXLFLASH_NUM_CMDS; i++) { |
| 1591 | cmd = &afu->cmd[i]; |
| 1592 | |
| 1593 | init_completion(&cmd->cevent); |
| 1594 | spin_lock_init(&cmd->slock); |
| 1595 | cmd->parent = afu; |
| 1596 | } |
| 1597 | |
| 1598 | init_pcr(cfg); |
| 1599 | |
Matthew R. Ochs | af10483 | 2015-10-21 15:15:14 -0500 | [diff] [blame] | 1600 | /* After an AFU reset, RRQ entries are stale, clear them */ |
| 1601 | memset(&afu->rrq_entry, 0, sizeof(afu->rrq_entry)); |
| 1602 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1603 | /* Initialize RRQ pointers */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1604 | afu->hrrq_start = &afu->rrq_entry[0]; |
| 1605 | afu->hrrq_end = &afu->rrq_entry[NUM_RRQ_ENTRY - 1]; |
| 1606 | afu->hrrq_curr = afu->hrrq_start; |
| 1607 | afu->toggle = 1; |
| 1608 | |
| 1609 | rc = init_global(cfg); |
| 1610 | |
| 1611 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 1612 | return rc; |
| 1613 | } |
| 1614 | |
| 1615 | /** |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 1616 | * init_intr() - setup interrupt handlers for the master context |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 1617 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1618 | * |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 1619 | * Return: 0 on success, -errno on failure |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1620 | */ |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 1621 | static enum undo_level init_intr(struct cxlflash_cfg *cfg, |
| 1622 | struct cxl_context *ctx) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1623 | { |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1624 | struct afu *afu = cfg->afu; |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 1625 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1626 | int rc = 0; |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 1627 | enum undo_level level = UNDO_NOOP; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1628 | |
| 1629 | rc = cxl_allocate_afu_irqs(ctx, 3); |
| 1630 | if (unlikely(rc)) { |
| 1631 | dev_err(dev, "%s: call to allocate_afu_irqs failed rc=%d!\n", |
| 1632 | __func__, rc); |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 1633 | level = UNDO_NOOP; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1634 | goto out; |
| 1635 | } |
| 1636 | |
| 1637 | rc = cxl_map_afu_irq(ctx, 1, cxlflash_sync_err_irq, afu, |
| 1638 | "SISL_MSI_SYNC_ERROR"); |
| 1639 | if (unlikely(rc <= 0)) { |
| 1640 | dev_err(dev, "%s: IRQ 1 (SISL_MSI_SYNC_ERROR) map failed!\n", |
| 1641 | __func__); |
| 1642 | level = FREE_IRQ; |
| 1643 | goto out; |
| 1644 | } |
| 1645 | |
| 1646 | rc = cxl_map_afu_irq(ctx, 2, cxlflash_rrq_irq, afu, |
| 1647 | "SISL_MSI_RRQ_UPDATED"); |
| 1648 | if (unlikely(rc <= 0)) { |
| 1649 | dev_err(dev, "%s: IRQ 2 (SISL_MSI_RRQ_UPDATED) map failed!\n", |
| 1650 | __func__); |
| 1651 | level = UNMAP_ONE; |
| 1652 | goto out; |
| 1653 | } |
| 1654 | |
| 1655 | rc = cxl_map_afu_irq(ctx, 3, cxlflash_async_err_irq, afu, |
| 1656 | "SISL_MSI_ASYNC_ERROR"); |
| 1657 | if (unlikely(rc <= 0)) { |
| 1658 | dev_err(dev, "%s: IRQ 3 (SISL_MSI_ASYNC_ERROR) map failed!\n", |
| 1659 | __func__); |
| 1660 | level = UNMAP_TWO; |
| 1661 | goto out; |
| 1662 | } |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 1663 | out: |
| 1664 | return level; |
| 1665 | } |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1666 | |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 1667 | /** |
| 1668 | * init_mc() - create and register as the master context |
| 1669 | * @cfg: Internal structure associated with the host. |
| 1670 | * |
| 1671 | * Return: 0 on success, -errno on failure |
| 1672 | */ |
| 1673 | static int init_mc(struct cxlflash_cfg *cfg) |
| 1674 | { |
| 1675 | struct cxl_context *ctx; |
| 1676 | struct device *dev = &cfg->dev->dev; |
| 1677 | int rc = 0; |
| 1678 | enum undo_level level; |
| 1679 | |
| 1680 | ctx = cxl_get_context(cfg->dev); |
| 1681 | if (unlikely(!ctx)) { |
| 1682 | rc = -ENOMEM; |
| 1683 | goto ret; |
| 1684 | } |
| 1685 | cfg->mcctx = ctx; |
| 1686 | |
| 1687 | /* Set it up as a master with the CXL */ |
| 1688 | cxl_set_master(ctx); |
| 1689 | |
| 1690 | /* During initialization reset the AFU to start from a clean slate */ |
| 1691 | rc = cxl_afu_reset(cfg->mcctx); |
| 1692 | if (unlikely(rc)) { |
| 1693 | dev_err(dev, "%s: initial AFU reset failed rc=%d\n", |
| 1694 | __func__, rc); |
| 1695 | goto ret; |
| 1696 | } |
| 1697 | |
| 1698 | level = init_intr(cfg, ctx); |
| 1699 | if (unlikely(level)) { |
| 1700 | dev_err(dev, "%s: setting up interrupts failed rc=%d\n", |
| 1701 | __func__, rc); |
| 1702 | goto out; |
| 1703 | } |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1704 | |
| 1705 | /* This performs the equivalent of the CXL_IOCTL_START_WORK. |
| 1706 | * The CXL_IOCTL_GET_PROCESS_ELEMENT is implicit in the process |
| 1707 | * element (pe) that is embedded in the context (ctx) |
| 1708 | */ |
| 1709 | rc = start_context(cfg); |
| 1710 | if (unlikely(rc)) { |
| 1711 | dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc); |
| 1712 | level = UNMAP_THREE; |
| 1713 | goto out; |
| 1714 | } |
| 1715 | ret: |
| 1716 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 1717 | return rc; |
| 1718 | out: |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 1719 | term_intr(cfg, level); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1720 | goto ret; |
| 1721 | } |
| 1722 | |
| 1723 | /** |
| 1724 | * init_afu() - setup as master context and start AFU |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 1725 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1726 | * |
| 1727 | * This routine is a higher level of control for configuring the |
| 1728 | * AFU on probe and reset paths. |
| 1729 | * |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 1730 | * Return: 0 on success, -errno on failure |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1731 | */ |
| 1732 | static int init_afu(struct cxlflash_cfg *cfg) |
| 1733 | { |
| 1734 | u64 reg; |
| 1735 | int rc = 0; |
| 1736 | struct afu *afu = cfg->afu; |
| 1737 | struct device *dev = &cfg->dev->dev; |
| 1738 | |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 1739 | cxl_perst_reloads_same_image(cfg->cxl_afu, true); |
| 1740 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1741 | rc = init_mc(cfg); |
| 1742 | if (rc) { |
| 1743 | dev_err(dev, "%s: call to init_mc failed, rc=%d!\n", |
| 1744 | __func__, rc); |
Matthew R. Ochs | ee3491b | 2015-10-21 15:16:00 -0500 | [diff] [blame] | 1745 | goto out; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1746 | } |
| 1747 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1748 | /* Map the entire MMIO space of the AFU */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1749 | afu->afu_map = cxl_psa_map(cfg->mcctx); |
| 1750 | if (!afu->afu_map) { |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1751 | dev_err(dev, "%s: call to cxl_psa_map failed!\n", __func__); |
Matthew R. Ochs | ee3491b | 2015-10-21 15:16:00 -0500 | [diff] [blame] | 1752 | rc = -ENOMEM; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1753 | goto err1; |
| 1754 | } |
Manoj Kumar | b45cdbaf | 2015-12-14 15:07:23 -0600 | [diff] [blame] | 1755 | kref_init(&afu->mapcount); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1756 | |
Matthew R. Ochs | e5ce067 | 2015-10-21 15:14:01 -0500 | [diff] [blame] | 1757 | /* No byte reverse on reading afu_version or string will be backwards */ |
| 1758 | reg = readq(&afu->afu_map->global.regs.afu_version); |
| 1759 | memcpy(afu->version, ®, sizeof(reg)); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1760 | afu->interface_version = |
| 1761 | readq_be(&afu->afu_map->global.regs.interface_version); |
Matthew R. Ochs | e5ce067 | 2015-10-21 15:14:01 -0500 | [diff] [blame] | 1762 | if ((afu->interface_version + 1) == 0) { |
| 1763 | pr_err("Back level AFU, please upgrade. AFU version %s " |
| 1764 | "interface version 0x%llx\n", afu->version, |
| 1765 | afu->interface_version); |
| 1766 | rc = -EINVAL; |
Matthew R. Ochs | ee3491b | 2015-10-21 15:16:00 -0500 | [diff] [blame] | 1767 | goto err2; |
| 1768 | } |
| 1769 | |
| 1770 | pr_debug("%s: afu version %s, interface version 0x%llX\n", __func__, |
| 1771 | afu->version, afu->interface_version); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1772 | |
| 1773 | rc = start_afu(cfg); |
| 1774 | if (rc) { |
| 1775 | dev_err(dev, "%s: call to start_afu failed, rc=%d!\n", |
| 1776 | __func__, rc); |
Matthew R. Ochs | ee3491b | 2015-10-21 15:16:00 -0500 | [diff] [blame] | 1777 | goto err2; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | afu_err_intr_init(cfg->afu); |
Uma Krishnan | 11f7b18 | 2016-11-28 18:41:45 -0600 | [diff] [blame] | 1781 | spin_lock_init(&afu->rrin_slock); |
| 1782 | afu->room = readq_be(&afu->host_map->cmd_room); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1783 | |
Matthew R. Ochs | 2cb7926 | 2015-08-13 21:47:53 -0500 | [diff] [blame] | 1784 | /* Restore the LUN mappings */ |
| 1785 | cxlflash_restore_luntable(cfg); |
Matthew R. Ochs | ee3491b | 2015-10-21 15:16:00 -0500 | [diff] [blame] | 1786 | out: |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1787 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 1788 | return rc; |
Matthew R. Ochs | ee3491b | 2015-10-21 15:16:00 -0500 | [diff] [blame] | 1789 | |
| 1790 | err2: |
Manoj Kumar | b45cdbaf | 2015-12-14 15:07:23 -0600 | [diff] [blame] | 1791 | kref_put(&afu->mapcount, afu_unmap); |
Matthew R. Ochs | ee3491b | 2015-10-21 15:16:00 -0500 | [diff] [blame] | 1792 | err1: |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 1793 | term_intr(cfg, UNMAP_THREE); |
| 1794 | term_mc(cfg); |
Matthew R. Ochs | ee3491b | 2015-10-21 15:16:00 -0500 | [diff] [blame] | 1795 | goto out; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | /** |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1799 | * cxlflash_afu_sync() - builds and sends an AFU sync command |
| 1800 | * @afu: AFU associated with the host. |
| 1801 | * @ctx_hndl_u: Identifies context requesting sync. |
| 1802 | * @res_hndl_u: Identifies resource requesting sync. |
| 1803 | * @mode: Type of sync to issue (lightweight, heavyweight, global). |
| 1804 | * |
| 1805 | * The AFU can only take 1 sync command at a time. This routine enforces this |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1806 | * limitation by using a mutex to provide exclusive access to the AFU during |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1807 | * the sync. This design point requires calling threads to not be on interrupt |
| 1808 | * context due to the possibility of sleeping during concurrent sync operations. |
| 1809 | * |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 1810 | * AFU sync operations are only necessary and allowed when the device is |
| 1811 | * operating normally. When not operating normally, sync requests can occur as |
| 1812 | * part of cleaning up resources associated with an adapter prior to removal. |
| 1813 | * In this scenario, these requests are simply ignored (safe due to the AFU |
| 1814 | * going away). |
| 1815 | * |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1816 | * Return: |
| 1817 | * 0 on success |
| 1818 | * -1 on failure |
| 1819 | */ |
| 1820 | int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx_hndl_u, |
| 1821 | res_hndl_t res_hndl_u, u8 mode) |
| 1822 | { |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 1823 | struct cxlflash_cfg *cfg = afu->parent; |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 1824 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1825 | struct afu_cmd *cmd = NULL; |
Matthew R. Ochs | 350bb47 | 2016-11-28 18:42:11 -0600 | [diff] [blame^] | 1826 | char *buf = NULL; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1827 | int rc = 0; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1828 | static DEFINE_MUTEX(sync_active); |
| 1829 | |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 1830 | if (cfg->state != STATE_NORMAL) { |
| 1831 | pr_debug("%s: Sync not required! (%u)\n", __func__, cfg->state); |
| 1832 | return 0; |
| 1833 | } |
| 1834 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1835 | mutex_lock(&sync_active); |
Matthew R. Ochs | 350bb47 | 2016-11-28 18:42:11 -0600 | [diff] [blame^] | 1836 | buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL); |
| 1837 | if (unlikely(!buf)) { |
| 1838 | dev_err(dev, "%s: no memory for command\n", __func__); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1839 | rc = -1; |
| 1840 | goto out; |
| 1841 | } |
| 1842 | |
Matthew R. Ochs | 350bb47 | 2016-11-28 18:42:11 -0600 | [diff] [blame^] | 1843 | cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd)); |
| 1844 | init_completion(&cmd->cevent); |
| 1845 | spin_lock_init(&cmd->slock); |
| 1846 | cmd->parent = afu; |
| 1847 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1848 | pr_debug("%s: afu=%p cmd=%p %d\n", __func__, afu, cmd, ctx_hndl_u); |
| 1849 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1850 | cmd->rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD; |
Matthew R. Ochs | 350bb47 | 2016-11-28 18:42:11 -0600 | [diff] [blame^] | 1851 | cmd->rcb.ctx_id = afu->ctx_hndl; |
| 1852 | cmd->rcb.msi = SISL_MSI_RRQ_UPDATED; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1853 | cmd->rcb.port_sel = 0x0; /* NA */ |
| 1854 | cmd->rcb.lun_id = 0x0; /* NA */ |
| 1855 | cmd->rcb.data_len = 0x0; |
| 1856 | cmd->rcb.data_ea = 0x0; |
| 1857 | cmd->rcb.timeout = MC_AFU_SYNC_TIMEOUT; |
| 1858 | |
| 1859 | cmd->rcb.cdb[0] = 0xC0; /* AFU Sync */ |
| 1860 | cmd->rcb.cdb[1] = mode; |
| 1861 | |
| 1862 | /* The cdb is aligned, no unaligned accessors required */ |
Matthew R. Ochs | 1786f4a | 2015-10-21 15:14:48 -0500 | [diff] [blame] | 1863 | *((__be16 *)&cmd->rcb.cdb[2]) = cpu_to_be16(ctx_hndl_u); |
| 1864 | *((__be32 *)&cmd->rcb.cdb[4]) = cpu_to_be32(res_hndl_u); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1865 | |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 1866 | rc = send_cmd(afu, cmd); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1867 | if (unlikely(rc)) |
| 1868 | goto out; |
| 1869 | |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 1870 | wait_resp(afu, cmd); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1871 | |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 1872 | /* Set on timeout */ |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1873 | if (unlikely((cmd->sa.ioasc != 0) || |
| 1874 | (cmd->sa.host_use_b[0] & B_ERROR))) |
| 1875 | rc = -1; |
| 1876 | out: |
| 1877 | mutex_unlock(&sync_active); |
Matthew R. Ochs | 350bb47 | 2016-11-28 18:42:11 -0600 | [diff] [blame^] | 1878 | kfree(buf); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1879 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 1880 | return rc; |
| 1881 | } |
| 1882 | |
| 1883 | /** |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 1884 | * afu_reset() - resets the AFU |
| 1885 | * @cfg: Internal structure associated with the host. |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1886 | * |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 1887 | * Return: 0 on success, -errno on failure |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1888 | */ |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 1889 | static int afu_reset(struct cxlflash_cfg *cfg) |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 1890 | { |
| 1891 | int rc = 0; |
| 1892 | /* Stop the context before the reset. Since the context is |
| 1893 | * no longer available restart it after the reset is complete |
| 1894 | */ |
| 1895 | |
| 1896 | term_afu(cfg); |
| 1897 | |
| 1898 | rc = init_afu(cfg); |
| 1899 | |
| 1900 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 1901 | return rc; |
| 1902 | } |
| 1903 | |
| 1904 | /** |
Manoj N. Kumar | f411396 | 2016-06-15 18:49:20 -0500 | [diff] [blame] | 1905 | * drain_ioctls() - wait until all currently executing ioctls have completed |
| 1906 | * @cfg: Internal structure associated with the host. |
| 1907 | * |
| 1908 | * Obtain write access to read/write semaphore that wraps ioctl |
| 1909 | * handling to 'drain' ioctls currently executing. |
| 1910 | */ |
| 1911 | static void drain_ioctls(struct cxlflash_cfg *cfg) |
| 1912 | { |
| 1913 | down_write(&cfg->ioctl_rwsem); |
| 1914 | up_write(&cfg->ioctl_rwsem); |
| 1915 | } |
| 1916 | |
| 1917 | /** |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 1918 | * cxlflash_eh_device_reset_handler() - reset a single LUN |
| 1919 | * @scp: SCSI command to send. |
| 1920 | * |
| 1921 | * Return: |
| 1922 | * SUCCESS as defined in scsi/scsi.h |
| 1923 | * FAILED as defined in scsi/scsi.h |
| 1924 | */ |
| 1925 | static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp) |
| 1926 | { |
| 1927 | int rc = SUCCESS; |
| 1928 | struct Scsi_Host *host = scp->device->host; |
| 1929 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata; |
| 1930 | struct afu *afu = cfg->afu; |
| 1931 | int rcr = 0; |
| 1932 | |
| 1933 | pr_debug("%s: (scp=%p) %d/%d/%d/%llu " |
| 1934 | "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp, |
| 1935 | host->host_no, scp->device->channel, |
| 1936 | scp->device->id, scp->device->lun, |
| 1937 | get_unaligned_be32(&((u32 *)scp->cmnd)[0]), |
| 1938 | get_unaligned_be32(&((u32 *)scp->cmnd)[1]), |
| 1939 | get_unaligned_be32(&((u32 *)scp->cmnd)[2]), |
| 1940 | get_unaligned_be32(&((u32 *)scp->cmnd)[3])); |
| 1941 | |
Matthew R. Ochs | ed486da | 2015-10-21 15:14:24 -0500 | [diff] [blame] | 1942 | retry: |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 1943 | switch (cfg->state) { |
| 1944 | case STATE_NORMAL: |
| 1945 | rcr = send_tmf(afu, scp, TMF_LUN_RESET); |
| 1946 | if (unlikely(rcr)) |
| 1947 | rc = FAILED; |
| 1948 | break; |
| 1949 | case STATE_RESET: |
| 1950 | wait_event(cfg->reset_waitq, cfg->state != STATE_RESET); |
Matthew R. Ochs | ed486da | 2015-10-21 15:14:24 -0500 | [diff] [blame] | 1951 | goto retry; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 1952 | default: |
| 1953 | rc = FAILED; |
| 1954 | break; |
| 1955 | } |
| 1956 | |
| 1957 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 1958 | return rc; |
| 1959 | } |
| 1960 | |
| 1961 | /** |
| 1962 | * cxlflash_eh_host_reset_handler() - reset the host adapter |
| 1963 | * @scp: SCSI command from stack identifying host. |
| 1964 | * |
Matthew R. Ochs | 1d3324c | 2016-09-02 15:39:30 -0500 | [diff] [blame] | 1965 | * Following a reset, the state is evaluated again in case an EEH occurred |
| 1966 | * during the reset. In such a scenario, the host reset will either yield |
| 1967 | * until the EEH recovery is complete or return success or failure based |
| 1968 | * upon the current device state. |
| 1969 | * |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 1970 | * Return: |
| 1971 | * SUCCESS as defined in scsi/scsi.h |
| 1972 | * FAILED as defined in scsi/scsi.h |
| 1973 | */ |
| 1974 | static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp) |
| 1975 | { |
| 1976 | int rc = SUCCESS; |
| 1977 | int rcr = 0; |
| 1978 | struct Scsi_Host *host = scp->device->host; |
| 1979 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)host->hostdata; |
| 1980 | |
| 1981 | pr_debug("%s: (scp=%p) %d/%d/%d/%llu " |
| 1982 | "cdb=(%08X-%08X-%08X-%08X)\n", __func__, scp, |
| 1983 | host->host_no, scp->device->channel, |
| 1984 | scp->device->id, scp->device->lun, |
| 1985 | get_unaligned_be32(&((u32 *)scp->cmnd)[0]), |
| 1986 | get_unaligned_be32(&((u32 *)scp->cmnd)[1]), |
| 1987 | get_unaligned_be32(&((u32 *)scp->cmnd)[2]), |
| 1988 | get_unaligned_be32(&((u32 *)scp->cmnd)[3])); |
| 1989 | |
| 1990 | switch (cfg->state) { |
| 1991 | case STATE_NORMAL: |
| 1992 | cfg->state = STATE_RESET; |
Manoj N. Kumar | f411396 | 2016-06-15 18:49:20 -0500 | [diff] [blame] | 1993 | drain_ioctls(cfg); |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 1994 | cxlflash_mark_contexts_error(cfg); |
| 1995 | rcr = afu_reset(cfg); |
| 1996 | if (rcr) { |
| 1997 | rc = FAILED; |
| 1998 | cfg->state = STATE_FAILTERM; |
| 1999 | } else |
| 2000 | cfg->state = STATE_NORMAL; |
| 2001 | wake_up_all(&cfg->reset_waitq); |
Matthew R. Ochs | 1d3324c | 2016-09-02 15:39:30 -0500 | [diff] [blame] | 2002 | ssleep(1); |
| 2003 | /* fall through */ |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2004 | case STATE_RESET: |
| 2005 | wait_event(cfg->reset_waitq, cfg->state != STATE_RESET); |
| 2006 | if (cfg->state == STATE_NORMAL) |
| 2007 | break; |
| 2008 | /* fall through */ |
| 2009 | default: |
| 2010 | rc = FAILED; |
| 2011 | break; |
| 2012 | } |
| 2013 | |
| 2014 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 2015 | return rc; |
| 2016 | } |
| 2017 | |
| 2018 | /** |
| 2019 | * cxlflash_change_queue_depth() - change the queue depth for the device |
| 2020 | * @sdev: SCSI device destined for queue depth change. |
| 2021 | * @qdepth: Requested queue depth value to set. |
| 2022 | * |
| 2023 | * The requested queue depth is capped to the maximum supported value. |
| 2024 | * |
| 2025 | * Return: The actual queue depth set. |
| 2026 | */ |
| 2027 | static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth) |
| 2028 | { |
| 2029 | |
| 2030 | if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN) |
| 2031 | qdepth = CXLFLASH_MAX_CMDS_PER_LUN; |
| 2032 | |
| 2033 | scsi_change_queue_depth(sdev, qdepth); |
| 2034 | return sdev->queue_depth; |
| 2035 | } |
| 2036 | |
| 2037 | /** |
| 2038 | * cxlflash_show_port_status() - queries and presents the current port status |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2039 | * @port: Desired port for status reporting. |
| 2040 | * @afu: AFU owning the specified port. |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2041 | * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII. |
| 2042 | * |
| 2043 | * Return: The size of the ASCII string returned in @buf. |
| 2044 | */ |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2045 | static ssize_t cxlflash_show_port_status(u32 port, struct afu *afu, char *buf) |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2046 | { |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2047 | char *disp_status; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2048 | u64 status; |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2049 | __be64 __iomem *fc_regs; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2050 | |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2051 | if (port >= NUM_FC_PORTS) |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2052 | return 0; |
| 2053 | |
| 2054 | fc_regs = &afu->afu_map->global.fc_regs[port][0]; |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2055 | status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]); |
| 2056 | status &= FC_MTIP_STATUS_MASK; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2057 | |
| 2058 | if (status == FC_MTIP_STATUS_ONLINE) |
| 2059 | disp_status = "online"; |
| 2060 | else if (status == FC_MTIP_STATUS_OFFLINE) |
| 2061 | disp_status = "offline"; |
| 2062 | else |
| 2063 | disp_status = "unknown"; |
| 2064 | |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2065 | return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status); |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | /** |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2069 | * port0_show() - queries and presents the current status of port 0 |
| 2070 | * @dev: Generic device associated with the host owning the port. |
| 2071 | * @attr: Device attribute representing the port. |
| 2072 | * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII. |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2073 | * |
| 2074 | * Return: The size of the ASCII string returned in @buf. |
| 2075 | */ |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2076 | static ssize_t port0_show(struct device *dev, |
| 2077 | struct device_attribute *attr, |
| 2078 | char *buf) |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2079 | { |
| 2080 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2081 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata; |
| 2082 | struct afu *afu = cfg->afu; |
| 2083 | |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2084 | return cxlflash_show_port_status(0, afu, buf); |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | /** |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2088 | * port1_show() - queries and presents the current status of port 1 |
| 2089 | * @dev: Generic device associated with the host owning the port. |
| 2090 | * @attr: Device attribute representing the port. |
| 2091 | * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII. |
| 2092 | * |
| 2093 | * Return: The size of the ASCII string returned in @buf. |
| 2094 | */ |
| 2095 | static ssize_t port1_show(struct device *dev, |
| 2096 | struct device_attribute *attr, |
| 2097 | char *buf) |
| 2098 | { |
| 2099 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2100 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata; |
| 2101 | struct afu *afu = cfg->afu; |
| 2102 | |
| 2103 | return cxlflash_show_port_status(1, afu, buf); |
| 2104 | } |
| 2105 | |
| 2106 | /** |
| 2107 | * lun_mode_show() - presents the current LUN mode of the host |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2108 | * @dev: Generic device associated with the host. |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2109 | * @attr: Device attribute representing the LUN mode. |
| 2110 | * @buf: Buffer of length PAGE_SIZE to report back the LUN mode in ASCII. |
| 2111 | * |
| 2112 | * Return: The size of the ASCII string returned in @buf. |
| 2113 | */ |
| 2114 | static ssize_t lun_mode_show(struct device *dev, |
| 2115 | struct device_attribute *attr, char *buf) |
| 2116 | { |
| 2117 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2118 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata; |
| 2119 | struct afu *afu = cfg->afu; |
| 2120 | |
| 2121 | return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun); |
| 2122 | } |
| 2123 | |
| 2124 | /** |
| 2125 | * lun_mode_store() - sets the LUN mode of the host |
| 2126 | * @dev: Generic device associated with the host. |
| 2127 | * @attr: Device attribute representing the LUN mode. |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2128 | * @buf: Buffer of length PAGE_SIZE containing the LUN mode in ASCII. |
| 2129 | * @count: Length of data resizing in @buf. |
| 2130 | * |
| 2131 | * The CXL Flash AFU supports a dummy LUN mode where the external |
| 2132 | * links and storage are not required. Space on the FPGA is used |
| 2133 | * to create 1 or 2 small LUNs which are presented to the system |
| 2134 | * as if they were a normal storage device. This feature is useful |
| 2135 | * during development and also provides manufacturing with a way |
| 2136 | * to test the AFU without an actual device. |
| 2137 | * |
| 2138 | * 0 = external LUN[s] (default) |
| 2139 | * 1 = internal LUN (1 x 64K, 512B blocks, id 0) |
| 2140 | * 2 = internal LUN (1 x 64K, 4K blocks, id 0) |
| 2141 | * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1) |
| 2142 | * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1) |
| 2143 | * |
| 2144 | * Return: The size of the ASCII string returned in @buf. |
| 2145 | */ |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2146 | static ssize_t lun_mode_store(struct device *dev, |
| 2147 | struct device_attribute *attr, |
| 2148 | const char *buf, size_t count) |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2149 | { |
| 2150 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2151 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata; |
| 2152 | struct afu *afu = cfg->afu; |
| 2153 | int rc; |
| 2154 | u32 lun_mode; |
| 2155 | |
| 2156 | rc = kstrtouint(buf, 10, &lun_mode); |
| 2157 | if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) { |
| 2158 | afu->internal_lun = lun_mode; |
Manoj N. Kumar | 603ecce | 2016-03-04 15:55:19 -0600 | [diff] [blame] | 2159 | |
| 2160 | /* |
| 2161 | * When configured for internal LUN, there is only one channel, |
| 2162 | * channel number 0, else there will be 2 (default). |
| 2163 | */ |
| 2164 | if (afu->internal_lun) |
| 2165 | shost->max_channel = 0; |
| 2166 | else |
| 2167 | shost->max_channel = NUM_FC_PORTS - 1; |
| 2168 | |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2169 | afu_reset(cfg); |
| 2170 | scsi_scan_host(cfg->host); |
| 2171 | } |
| 2172 | |
| 2173 | return count; |
| 2174 | } |
| 2175 | |
| 2176 | /** |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2177 | * ioctl_version_show() - presents the current ioctl version of the host |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2178 | * @dev: Generic device associated with the host. |
| 2179 | * @attr: Device attribute representing the ioctl version. |
| 2180 | * @buf: Buffer of length PAGE_SIZE to report back the ioctl version. |
| 2181 | * |
| 2182 | * Return: The size of the ASCII string returned in @buf. |
| 2183 | */ |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2184 | static ssize_t ioctl_version_show(struct device *dev, |
| 2185 | struct device_attribute *attr, char *buf) |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2186 | { |
| 2187 | return scnprintf(buf, PAGE_SIZE, "%u\n", DK_CXLFLASH_VERSION_0); |
| 2188 | } |
| 2189 | |
| 2190 | /** |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2191 | * cxlflash_show_port_lun_table() - queries and presents the port LUN table |
| 2192 | * @port: Desired port for status reporting. |
| 2193 | * @afu: AFU owning the specified port. |
| 2194 | * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII. |
| 2195 | * |
| 2196 | * Return: The size of the ASCII string returned in @buf. |
| 2197 | */ |
| 2198 | static ssize_t cxlflash_show_port_lun_table(u32 port, |
| 2199 | struct afu *afu, |
| 2200 | char *buf) |
| 2201 | { |
| 2202 | int i; |
| 2203 | ssize_t bytes = 0; |
| 2204 | __be64 __iomem *fc_port; |
| 2205 | |
| 2206 | if (port >= NUM_FC_PORTS) |
| 2207 | return 0; |
| 2208 | |
| 2209 | fc_port = &afu->afu_map->global.fc_port[port][0]; |
| 2210 | |
| 2211 | for (i = 0; i < CXLFLASH_NUM_VLUNS; i++) |
| 2212 | bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes, |
| 2213 | "%03d: %016llX\n", i, readq_be(&fc_port[i])); |
| 2214 | return bytes; |
| 2215 | } |
| 2216 | |
| 2217 | /** |
| 2218 | * port0_lun_table_show() - presents the current LUN table of port 0 |
| 2219 | * @dev: Generic device associated with the host owning the port. |
| 2220 | * @attr: Device attribute representing the port. |
| 2221 | * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII. |
| 2222 | * |
| 2223 | * Return: The size of the ASCII string returned in @buf. |
| 2224 | */ |
| 2225 | static ssize_t port0_lun_table_show(struct device *dev, |
| 2226 | struct device_attribute *attr, |
| 2227 | char *buf) |
| 2228 | { |
| 2229 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2230 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata; |
| 2231 | struct afu *afu = cfg->afu; |
| 2232 | |
| 2233 | return cxlflash_show_port_lun_table(0, afu, buf); |
| 2234 | } |
| 2235 | |
| 2236 | /** |
| 2237 | * port1_lun_table_show() - presents the current LUN table of port 1 |
| 2238 | * @dev: Generic device associated with the host owning the port. |
| 2239 | * @attr: Device attribute representing the port. |
| 2240 | * @buf: Buffer of length PAGE_SIZE to report back port status in ASCII. |
| 2241 | * |
| 2242 | * Return: The size of the ASCII string returned in @buf. |
| 2243 | */ |
| 2244 | static ssize_t port1_lun_table_show(struct device *dev, |
| 2245 | struct device_attribute *attr, |
| 2246 | char *buf) |
| 2247 | { |
| 2248 | struct Scsi_Host *shost = class_to_shost(dev); |
| 2249 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)shost->hostdata; |
| 2250 | struct afu *afu = cfg->afu; |
| 2251 | |
| 2252 | return cxlflash_show_port_lun_table(1, afu, buf); |
| 2253 | } |
| 2254 | |
| 2255 | /** |
| 2256 | * mode_show() - presents the current mode of the device |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2257 | * @dev: Generic device associated with the device. |
| 2258 | * @attr: Device attribute representing the device mode. |
| 2259 | * @buf: Buffer of length PAGE_SIZE to report back the dev mode in ASCII. |
| 2260 | * |
| 2261 | * Return: The size of the ASCII string returned in @buf. |
| 2262 | */ |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2263 | static ssize_t mode_show(struct device *dev, |
| 2264 | struct device_attribute *attr, char *buf) |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2265 | { |
| 2266 | struct scsi_device *sdev = to_scsi_device(dev); |
| 2267 | |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2268 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 2269 | sdev->hostdata ? "superpipe" : "legacy"); |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | /* |
| 2273 | * Host attributes |
| 2274 | */ |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2275 | static DEVICE_ATTR_RO(port0); |
| 2276 | static DEVICE_ATTR_RO(port1); |
| 2277 | static DEVICE_ATTR_RW(lun_mode); |
| 2278 | static DEVICE_ATTR_RO(ioctl_version); |
| 2279 | static DEVICE_ATTR_RO(port0_lun_table); |
| 2280 | static DEVICE_ATTR_RO(port1_lun_table); |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2281 | |
| 2282 | static struct device_attribute *cxlflash_host_attrs[] = { |
| 2283 | &dev_attr_port0, |
| 2284 | &dev_attr_port1, |
| 2285 | &dev_attr_lun_mode, |
| 2286 | &dev_attr_ioctl_version, |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2287 | &dev_attr_port0_lun_table, |
| 2288 | &dev_attr_port1_lun_table, |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2289 | NULL |
| 2290 | }; |
| 2291 | |
| 2292 | /* |
| 2293 | * Device attributes |
| 2294 | */ |
Matthew R. Ochs | e0f01a2 | 2015-10-21 15:12:39 -0500 | [diff] [blame] | 2295 | static DEVICE_ATTR_RO(mode); |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2296 | |
| 2297 | static struct device_attribute *cxlflash_dev_attrs[] = { |
| 2298 | &dev_attr_mode, |
| 2299 | NULL |
| 2300 | }; |
| 2301 | |
| 2302 | /* |
| 2303 | * Host template |
| 2304 | */ |
| 2305 | static struct scsi_host_template driver_template = { |
| 2306 | .module = THIS_MODULE, |
| 2307 | .name = CXLFLASH_ADAPTER_NAME, |
| 2308 | .info = cxlflash_driver_info, |
| 2309 | .ioctl = cxlflash_ioctl, |
| 2310 | .proc_name = CXLFLASH_NAME, |
| 2311 | .queuecommand = cxlflash_queuecommand, |
| 2312 | .eh_device_reset_handler = cxlflash_eh_device_reset_handler, |
| 2313 | .eh_host_reset_handler = cxlflash_eh_host_reset_handler, |
| 2314 | .change_queue_depth = cxlflash_change_queue_depth, |
Manoj N. Kumar | 8343083 | 2016-03-04 15:55:20 -0600 | [diff] [blame] | 2315 | .cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN, |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2316 | .can_queue = CXLFLASH_MAX_CMDS, |
| 2317 | .this_id = -1, |
Uma Krishnan | 68ab2d7 | 2016-11-28 18:41:06 -0600 | [diff] [blame] | 2318 | .sg_tablesize = 1, /* No scatter gather support */ |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2319 | .max_sectors = CXLFLASH_MAX_SECTORS, |
| 2320 | .use_clustering = ENABLE_CLUSTERING, |
| 2321 | .shost_attrs = cxlflash_host_attrs, |
| 2322 | .sdev_attrs = cxlflash_dev_attrs, |
| 2323 | }; |
| 2324 | |
| 2325 | /* |
| 2326 | * Device dependent values |
| 2327 | */ |
Uma Krishnan | 96e1b66 | 2016-06-15 18:49:38 -0500 | [diff] [blame] | 2328 | static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS, |
| 2329 | 0ULL }; |
| 2330 | static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS, |
Uma Krishnan | 704c4b0 | 2016-06-15 18:49:57 -0500 | [diff] [blame] | 2331 | CXLFLASH_NOTIFY_SHUTDOWN }; |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2332 | |
| 2333 | /* |
| 2334 | * PCI device binding table |
| 2335 | */ |
| 2336 | static struct pci_device_id cxlflash_pci_table[] = { |
| 2337 | {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA, |
| 2338 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals}, |
Manoj Kumar | a2746fb | 2015-12-14 15:07:43 -0600 | [diff] [blame] | 2339 | {PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT, |
| 2340 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals}, |
Matthew R. Ochs | 1530551 | 2015-10-21 15:12:10 -0500 | [diff] [blame] | 2341 | {} |
| 2342 | }; |
| 2343 | |
| 2344 | MODULE_DEVICE_TABLE(pci, cxlflash_pci_table); |
| 2345 | |
| 2346 | /** |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2347 | * cxlflash_worker_thread() - work thread handler for the AFU |
| 2348 | * @work: Work structure contained within cxlflash associated with host. |
| 2349 | * |
| 2350 | * Handles the following events: |
| 2351 | * - Link reset which cannot be performed on interrupt context due to |
| 2352 | * blocking up to a few seconds |
Matthew R. Ochs | ef51074 | 2015-10-21 15:13:37 -0500 | [diff] [blame] | 2353 | * - Rescan the host |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2354 | */ |
| 2355 | static void cxlflash_worker_thread(struct work_struct *work) |
| 2356 | { |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 2357 | struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg, |
| 2358 | work_q); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2359 | struct afu *afu = cfg->afu; |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 2360 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2361 | int port; |
| 2362 | ulong lock_flags; |
| 2363 | |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 2364 | /* Avoid MMIO if the device has failed */ |
| 2365 | |
| 2366 | if (cfg->state != STATE_NORMAL) |
| 2367 | return; |
| 2368 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2369 | spin_lock_irqsave(cfg->host->host_lock, lock_flags); |
| 2370 | |
| 2371 | if (cfg->lr_state == LINK_RESET_REQUIRED) { |
| 2372 | port = cfg->lr_port; |
| 2373 | if (port < 0) |
Matthew R. Ochs | 4392ba4 | 2015-10-21 15:13:11 -0500 | [diff] [blame] | 2374 | dev_err(dev, "%s: invalid port index %d\n", |
| 2375 | __func__, port); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2376 | else { |
| 2377 | spin_unlock_irqrestore(cfg->host->host_lock, |
| 2378 | lock_flags); |
| 2379 | |
| 2380 | /* The reset can block... */ |
| 2381 | afu_link_reset(afu, port, |
Matthew R. Ochs | f15fbf8 | 2015-10-21 15:15:06 -0500 | [diff] [blame] | 2382 | &afu->afu_map->global.fc_regs[port][0]); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2383 | spin_lock_irqsave(cfg->host->host_lock, lock_flags); |
| 2384 | } |
| 2385 | |
| 2386 | cfg->lr_state = LINK_RESET_COMPLETE; |
| 2387 | } |
| 2388 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2389 | spin_unlock_irqrestore(cfg->host->host_lock, lock_flags); |
Matthew R. Ochs | ef51074 | 2015-10-21 15:13:37 -0500 | [diff] [blame] | 2390 | |
| 2391 | if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0) |
| 2392 | scsi_scan_host(cfg->host); |
Manoj Kumar | b45cdbaf | 2015-12-14 15:07:23 -0600 | [diff] [blame] | 2393 | kref_put(&afu->mapcount, afu_unmap); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | /** |
| 2397 | * cxlflash_probe() - PCI entry point to add host |
| 2398 | * @pdev: PCI device associated with the host. |
| 2399 | * @dev_id: PCI device id associated with device. |
| 2400 | * |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 2401 | * Return: 0 on success, -errno on failure |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2402 | */ |
| 2403 | static int cxlflash_probe(struct pci_dev *pdev, |
| 2404 | const struct pci_device_id *dev_id) |
| 2405 | { |
| 2406 | struct Scsi_Host *host; |
| 2407 | struct cxlflash_cfg *cfg = NULL; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2408 | struct dev_dependent_vals *ddv; |
| 2409 | int rc = 0; |
| 2410 | |
| 2411 | dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n", |
| 2412 | __func__, pdev->irq); |
| 2413 | |
| 2414 | ddv = (struct dev_dependent_vals *)dev_id->driver_data; |
| 2415 | driver_template.max_sectors = ddv->max_sectors; |
| 2416 | |
| 2417 | host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg)); |
| 2418 | if (!host) { |
| 2419 | dev_err(&pdev->dev, "%s: call to scsi_host_alloc failed!\n", |
| 2420 | __func__); |
| 2421 | rc = -ENOMEM; |
| 2422 | goto out; |
| 2423 | } |
| 2424 | |
| 2425 | host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS; |
| 2426 | host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET; |
| 2427 | host->max_channel = NUM_FC_PORTS - 1; |
| 2428 | host->unique_id = host->host_no; |
| 2429 | host->max_cmd_len = CXLFLASH_MAX_CDB_LEN; |
| 2430 | |
| 2431 | cfg = (struct cxlflash_cfg *)host->hostdata; |
| 2432 | cfg->host = host; |
| 2433 | rc = alloc_mem(cfg); |
| 2434 | if (rc) { |
Matthew R. Ochs | fa3f2c6 | 2015-10-21 15:15:45 -0500 | [diff] [blame] | 2435 | dev_err(&pdev->dev, "%s: call to alloc_mem failed!\n", |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2436 | __func__); |
| 2437 | rc = -ENOMEM; |
Matthew R. Ochs | 8b5b1e8 | 2015-10-21 15:14:09 -0500 | [diff] [blame] | 2438 | scsi_host_put(cfg->host); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2439 | goto out; |
| 2440 | } |
| 2441 | |
| 2442 | cfg->init_state = INIT_STATE_NONE; |
| 2443 | cfg->dev = pdev; |
Matthew R. Ochs | 17ead26 | 2015-10-21 15:15:37 -0500 | [diff] [blame] | 2444 | cfg->cxl_fops = cxlflash_cxl_fops; |
Matthew R. Ochs | 2cb7926 | 2015-08-13 21:47:53 -0500 | [diff] [blame] | 2445 | |
| 2446 | /* |
| 2447 | * The promoted LUNs move to the top of the LUN table. The rest stay |
| 2448 | * on the bottom half. The bottom half grows from the end |
| 2449 | * (index = 255), whereas the top half grows from the beginning |
| 2450 | * (index = 0). |
| 2451 | */ |
| 2452 | cfg->promote_lun_index = 0; |
| 2453 | cfg->last_lun_index[0] = CXLFLASH_NUM_VLUNS/2 - 1; |
| 2454 | cfg->last_lun_index[1] = CXLFLASH_NUM_VLUNS/2 - 1; |
| 2455 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2456 | cfg->dev_id = (struct pci_device_id *)dev_id; |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2457 | |
| 2458 | init_waitqueue_head(&cfg->tmf_waitq); |
Matthew R. Ochs | 439e85c | 2015-10-21 15:12:00 -0500 | [diff] [blame] | 2459 | init_waitqueue_head(&cfg->reset_waitq); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2460 | |
| 2461 | INIT_WORK(&cfg->work_q, cxlflash_worker_thread); |
| 2462 | cfg->lr_state = LINK_RESET_INVALID; |
| 2463 | cfg->lr_port = -1; |
Matthew R. Ochs | 0d73122 | 2015-10-21 15:16:24 -0500 | [diff] [blame] | 2464 | spin_lock_init(&cfg->tmf_slock); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 2465 | mutex_init(&cfg->ctx_tbl_list_mutex); |
| 2466 | mutex_init(&cfg->ctx_recovery_mutex); |
Matthew R. Ochs | 0a27ae5 | 2015-10-21 15:11:52 -0500 | [diff] [blame] | 2467 | init_rwsem(&cfg->ioctl_rwsem); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 2468 | INIT_LIST_HEAD(&cfg->ctx_err_recovery); |
| 2469 | INIT_LIST_HEAD(&cfg->lluns); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2470 | |
| 2471 | pci_set_drvdata(pdev, cfg); |
| 2472 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2473 | cfg->cxl_afu = cxl_pci_to_afu(pdev); |
| 2474 | |
| 2475 | rc = init_pci(cfg); |
| 2476 | if (rc) { |
| 2477 | dev_err(&pdev->dev, "%s: call to init_pci " |
| 2478 | "failed rc=%d!\n", __func__, rc); |
| 2479 | goto out_remove; |
| 2480 | } |
| 2481 | cfg->init_state = INIT_STATE_PCI; |
| 2482 | |
| 2483 | rc = init_afu(cfg); |
| 2484 | if (rc) { |
| 2485 | dev_err(&pdev->dev, "%s: call to init_afu " |
| 2486 | "failed rc=%d!\n", __func__, rc); |
| 2487 | goto out_remove; |
| 2488 | } |
| 2489 | cfg->init_state = INIT_STATE_AFU; |
| 2490 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2491 | rc = init_scsi(cfg); |
| 2492 | if (rc) { |
| 2493 | dev_err(&pdev->dev, "%s: call to init_scsi " |
| 2494 | "failed rc=%d!\n", __func__, rc); |
| 2495 | goto out_remove; |
| 2496 | } |
| 2497 | cfg->init_state = INIT_STATE_SCSI; |
| 2498 | |
| 2499 | out: |
| 2500 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 2501 | return rc; |
| 2502 | |
| 2503 | out_remove: |
| 2504 | cxlflash_remove(pdev); |
| 2505 | goto out; |
| 2506 | } |
| 2507 | |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 2508 | /** |
| 2509 | * cxlflash_pci_error_detected() - called when a PCI error is detected |
| 2510 | * @pdev: PCI device struct. |
| 2511 | * @state: PCI channel state. |
| 2512 | * |
Matthew R. Ochs | 1d3324c | 2016-09-02 15:39:30 -0500 | [diff] [blame] | 2513 | * When an EEH occurs during an active reset, wait until the reset is |
| 2514 | * complete and then take action based upon the device state. |
| 2515 | * |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 2516 | * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT |
| 2517 | */ |
| 2518 | static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev, |
| 2519 | pci_channel_state_t state) |
| 2520 | { |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 2521 | int rc = 0; |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 2522 | struct cxlflash_cfg *cfg = pci_get_drvdata(pdev); |
| 2523 | struct device *dev = &cfg->dev->dev; |
| 2524 | |
| 2525 | dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state); |
| 2526 | |
| 2527 | switch (state) { |
| 2528 | case pci_channel_io_frozen: |
Matthew R. Ochs | 1d3324c | 2016-09-02 15:39:30 -0500 | [diff] [blame] | 2529 | wait_event(cfg->reset_waitq, cfg->state != STATE_RESET); |
| 2530 | if (cfg->state == STATE_FAILTERM) |
| 2531 | return PCI_ERS_RESULT_DISCONNECT; |
| 2532 | |
Matthew R. Ochs | 439e85c | 2015-10-21 15:12:00 -0500 | [diff] [blame] | 2533 | cfg->state = STATE_RESET; |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 2534 | scsi_block_requests(cfg->host); |
Matthew R. Ochs | 0a27ae5 | 2015-10-21 15:11:52 -0500 | [diff] [blame] | 2535 | drain_ioctls(cfg); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 2536 | rc = cxlflash_mark_contexts_error(cfg); |
| 2537 | if (unlikely(rc)) |
| 2538 | dev_err(dev, "%s: Failed to mark user contexts!(%d)\n", |
| 2539 | __func__, rc); |
Manoj N. Kumar | 9526f36 | 2016-03-25 14:26:34 -0500 | [diff] [blame] | 2540 | term_afu(cfg); |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 2541 | return PCI_ERS_RESULT_NEED_RESET; |
| 2542 | case pci_channel_io_perm_failure: |
| 2543 | cfg->state = STATE_FAILTERM; |
Matthew R. Ochs | 439e85c | 2015-10-21 15:12:00 -0500 | [diff] [blame] | 2544 | wake_up_all(&cfg->reset_waitq); |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 2545 | scsi_unblock_requests(cfg->host); |
| 2546 | return PCI_ERS_RESULT_DISCONNECT; |
| 2547 | default: |
| 2548 | break; |
| 2549 | } |
| 2550 | return PCI_ERS_RESULT_NEED_RESET; |
| 2551 | } |
| 2552 | |
| 2553 | /** |
| 2554 | * cxlflash_pci_slot_reset() - called when PCI slot has been reset |
| 2555 | * @pdev: PCI device struct. |
| 2556 | * |
| 2557 | * This routine is called by the pci error recovery code after the PCI |
| 2558 | * slot has been reset, just before we should resume normal operations. |
| 2559 | * |
| 2560 | * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT |
| 2561 | */ |
| 2562 | static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev) |
| 2563 | { |
| 2564 | int rc = 0; |
| 2565 | struct cxlflash_cfg *cfg = pci_get_drvdata(pdev); |
| 2566 | struct device *dev = &cfg->dev->dev; |
| 2567 | |
| 2568 | dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev); |
| 2569 | |
| 2570 | rc = init_afu(cfg); |
| 2571 | if (unlikely(rc)) { |
| 2572 | dev_err(dev, "%s: EEH recovery failed! (%d)\n", __func__, rc); |
| 2573 | return PCI_ERS_RESULT_DISCONNECT; |
| 2574 | } |
| 2575 | |
| 2576 | return PCI_ERS_RESULT_RECOVERED; |
| 2577 | } |
| 2578 | |
| 2579 | /** |
| 2580 | * cxlflash_pci_resume() - called when normal operation can resume |
| 2581 | * @pdev: PCI device struct |
| 2582 | */ |
| 2583 | static void cxlflash_pci_resume(struct pci_dev *pdev) |
| 2584 | { |
| 2585 | struct cxlflash_cfg *cfg = pci_get_drvdata(pdev); |
| 2586 | struct device *dev = &cfg->dev->dev; |
| 2587 | |
| 2588 | dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev); |
| 2589 | |
| 2590 | cfg->state = STATE_NORMAL; |
Matthew R. Ochs | 439e85c | 2015-10-21 15:12:00 -0500 | [diff] [blame] | 2591 | wake_up_all(&cfg->reset_waitq); |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 2592 | scsi_unblock_requests(cfg->host); |
| 2593 | } |
| 2594 | |
| 2595 | static const struct pci_error_handlers cxlflash_err_handler = { |
| 2596 | .error_detected = cxlflash_pci_error_detected, |
| 2597 | .slot_reset = cxlflash_pci_slot_reset, |
| 2598 | .resume = cxlflash_pci_resume, |
| 2599 | }; |
| 2600 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2601 | /* |
| 2602 | * PCI device structure |
| 2603 | */ |
| 2604 | static struct pci_driver cxlflash_driver = { |
| 2605 | .name = CXLFLASH_NAME, |
| 2606 | .id_table = cxlflash_pci_table, |
| 2607 | .probe = cxlflash_probe, |
| 2608 | .remove = cxlflash_remove, |
Uma Krishnan | babf985 | 2016-09-02 15:39:16 -0500 | [diff] [blame] | 2609 | .shutdown = cxlflash_remove, |
Matthew R. Ochs | 5cdac81 | 2015-08-13 21:47:34 -0500 | [diff] [blame] | 2610 | .err_handler = &cxlflash_err_handler, |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2611 | }; |
| 2612 | |
| 2613 | /** |
| 2614 | * init_cxlflash() - module entry point |
| 2615 | * |
Matthew R. Ochs | 1284fb0 | 2015-10-21 15:14:40 -0500 | [diff] [blame] | 2616 | * Return: 0 on success, -errno on failure |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2617 | */ |
| 2618 | static int __init init_cxlflash(void) |
| 2619 | { |
Uma Krishnan | 8559921 | 2015-12-14 15:06:33 -0600 | [diff] [blame] | 2620 | pr_info("%s: %s\n", __func__, CXLFLASH_ADAPTER_NAME); |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2621 | |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 2622 | cxlflash_list_init(); |
| 2623 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2624 | return pci_register_driver(&cxlflash_driver); |
| 2625 | } |
| 2626 | |
| 2627 | /** |
| 2628 | * exit_cxlflash() - module exit point |
| 2629 | */ |
| 2630 | static void __exit exit_cxlflash(void) |
| 2631 | { |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 2632 | cxlflash_term_global_luns(); |
| 2633 | cxlflash_free_errpage(); |
| 2634 | |
Matthew R. Ochs | c21e0bb | 2015-06-09 17:15:52 -0500 | [diff] [blame] | 2635 | pci_unregister_driver(&cxlflash_driver); |
| 2636 | } |
| 2637 | |
| 2638 | module_init(init_cxlflash); |
| 2639 | module_exit(exit_cxlflash); |