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