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