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