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