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