John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Linaro Ltd. |
| 3 | * Copyright (c) 2015 Hisilicon Limited. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include "hisi_sas.h" |
| 13 | #define DRV_NAME "hisi_sas" |
| 14 | |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 15 | #define DEV_IS_GONE(dev) \ |
| 16 | ((!dev) || (dev->dev_type == SAS_PHY_UNUSED)) |
| 17 | |
John Garry | cac9b2a | 2016-02-25 17:42:11 +0800 | [diff] [blame] | 18 | static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device, |
| 19 | u8 *lun, struct hisi_sas_tmf_task *tmf); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 20 | static int |
| 21 | hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba, |
| 22 | struct domain_device *device, |
| 23 | int abort_flag, int tag); |
Xiang Chen | 7c594f0 | 2017-03-23 01:25:20 +0800 | [diff] [blame] | 24 | static int hisi_sas_softreset_ata_disk(struct domain_device *device); |
John Garry | cac9b2a | 2016-02-25 17:42:11 +0800 | [diff] [blame] | 25 | |
Xiang Chen | 6c7bb8a | 2017-06-14 23:33:14 +0800 | [diff] [blame] | 26 | u8 hisi_sas_get_ata_protocol(u8 cmd, int direction) |
| 27 | { |
| 28 | switch (cmd) { |
| 29 | case ATA_CMD_FPDMA_WRITE: |
| 30 | case ATA_CMD_FPDMA_READ: |
| 31 | case ATA_CMD_FPDMA_RECV: |
| 32 | case ATA_CMD_FPDMA_SEND: |
| 33 | case ATA_CMD_NCQ_NON_DATA: |
| 34 | return HISI_SAS_SATA_PROTOCOL_FPDMA; |
| 35 | |
| 36 | case ATA_CMD_DOWNLOAD_MICRO: |
| 37 | case ATA_CMD_ID_ATA: |
| 38 | case ATA_CMD_PMP_READ: |
| 39 | case ATA_CMD_READ_LOG_EXT: |
| 40 | case ATA_CMD_PIO_READ: |
| 41 | case ATA_CMD_PIO_READ_EXT: |
| 42 | case ATA_CMD_PMP_WRITE: |
| 43 | case ATA_CMD_WRITE_LOG_EXT: |
| 44 | case ATA_CMD_PIO_WRITE: |
| 45 | case ATA_CMD_PIO_WRITE_EXT: |
| 46 | return HISI_SAS_SATA_PROTOCOL_PIO; |
| 47 | |
| 48 | case ATA_CMD_DSM: |
| 49 | case ATA_CMD_DOWNLOAD_MICRO_DMA: |
| 50 | case ATA_CMD_PMP_READ_DMA: |
| 51 | case ATA_CMD_PMP_WRITE_DMA: |
| 52 | case ATA_CMD_READ: |
| 53 | case ATA_CMD_READ_EXT: |
| 54 | case ATA_CMD_READ_LOG_DMA_EXT: |
| 55 | case ATA_CMD_READ_STREAM_DMA_EXT: |
| 56 | case ATA_CMD_TRUSTED_RCV_DMA: |
| 57 | case ATA_CMD_TRUSTED_SND_DMA: |
| 58 | case ATA_CMD_WRITE: |
| 59 | case ATA_CMD_WRITE_EXT: |
| 60 | case ATA_CMD_WRITE_FUA_EXT: |
| 61 | case ATA_CMD_WRITE_QUEUED: |
| 62 | case ATA_CMD_WRITE_LOG_DMA_EXT: |
| 63 | case ATA_CMD_WRITE_STREAM_DMA_EXT: |
| 64 | return HISI_SAS_SATA_PROTOCOL_DMA; |
| 65 | |
| 66 | case ATA_CMD_CHK_POWER: |
| 67 | case ATA_CMD_DEV_RESET: |
| 68 | case ATA_CMD_EDD: |
| 69 | case ATA_CMD_FLUSH: |
| 70 | case ATA_CMD_FLUSH_EXT: |
| 71 | case ATA_CMD_VERIFY: |
| 72 | case ATA_CMD_VERIFY_EXT: |
| 73 | case ATA_CMD_SET_FEATURES: |
| 74 | case ATA_CMD_STANDBY: |
| 75 | case ATA_CMD_STANDBYNOW1: |
| 76 | return HISI_SAS_SATA_PROTOCOL_NONDATA; |
| 77 | default: |
| 78 | if (direction == DMA_NONE) |
| 79 | return HISI_SAS_SATA_PROTOCOL_NONDATA; |
| 80 | return HISI_SAS_SATA_PROTOCOL_PIO; |
| 81 | } |
| 82 | } |
| 83 | EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol); |
| 84 | |
Xiang Chen | 7590407 | 2017-06-14 23:33:15 +0800 | [diff] [blame] | 85 | void hisi_sas_sata_done(struct sas_task *task, |
| 86 | struct hisi_sas_slot *slot) |
| 87 | { |
| 88 | struct task_status_struct *ts = &task->task_status; |
| 89 | struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf; |
Xiaofei Tan | f557e32 | 2017-06-29 21:02:14 +0800 | [diff] [blame] | 90 | struct hisi_sas_status_buffer *status_buf = |
| 91 | hisi_sas_status_buf_addr_mem(slot); |
| 92 | u8 *iu = &status_buf->iu[0]; |
| 93 | struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu; |
Xiang Chen | 7590407 | 2017-06-14 23:33:15 +0800 | [diff] [blame] | 94 | |
| 95 | resp->frame_len = sizeof(struct dev_to_host_fis); |
| 96 | memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis)); |
| 97 | |
| 98 | ts->buf_valid_size = sizeof(*resp); |
| 99 | } |
| 100 | EXPORT_SYMBOL_GPL(hisi_sas_sata_done); |
| 101 | |
Xiang Chen | 318913c | 2017-06-14 23:33:16 +0800 | [diff] [blame] | 102 | int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag) |
| 103 | { |
| 104 | struct ata_queued_cmd *qc = task->uldd_task; |
| 105 | |
| 106 | if (qc) { |
| 107 | if (qc->tf.command == ATA_CMD_FPDMA_WRITE || |
| 108 | qc->tf.command == ATA_CMD_FPDMA_READ) { |
| 109 | *tag = qc->tag; |
| 110 | return 1; |
| 111 | } |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag); |
| 116 | |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 117 | static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device) |
| 118 | { |
| 119 | return device->port->ha->lldd_ha; |
| 120 | } |
| 121 | |
John Garry | 2e244f0 | 2017-03-23 01:25:17 +0800 | [diff] [blame] | 122 | struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port) |
| 123 | { |
| 124 | return container_of(sas_port, struct hisi_sas_port, sas_port); |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(to_hisi_sas_port); |
| 127 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 128 | static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx) |
| 129 | { |
| 130 | void *bitmap = hisi_hba->slot_index_tags; |
| 131 | |
| 132 | clear_bit(slot_idx, bitmap); |
| 133 | } |
| 134 | |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 135 | static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx) |
| 136 | { |
| 137 | hisi_sas_slot_index_clear(hisi_hba, slot_idx); |
| 138 | } |
| 139 | |
| 140 | static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx) |
| 141 | { |
| 142 | void *bitmap = hisi_hba->slot_index_tags; |
| 143 | |
| 144 | set_bit(slot_idx, bitmap); |
| 145 | } |
| 146 | |
| 147 | static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx) |
| 148 | { |
| 149 | unsigned int index; |
| 150 | void *bitmap = hisi_hba->slot_index_tags; |
| 151 | |
| 152 | index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count); |
| 153 | if (index >= hisi_hba->slot_index_count) |
| 154 | return -SAS_QUEUE_FULL; |
| 155 | hisi_sas_slot_index_set(hisi_hba, index); |
| 156 | *slot_idx = index; |
| 157 | return 0; |
| 158 | } |
| 159 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 160 | static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba) |
| 161 | { |
| 162 | int i; |
| 163 | |
| 164 | for (i = 0; i < hisi_hba->slot_index_count; ++i) |
| 165 | hisi_sas_slot_index_clear(hisi_hba, i); |
| 166 | } |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 167 | |
| 168 | void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task, |
| 169 | struct hisi_sas_slot *slot) |
| 170 | { |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 171 | |
John Garry | d3c4dd4 | 2017-04-10 21:22:00 +0800 | [diff] [blame] | 172 | if (task) { |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 173 | struct device *dev = hisi_hba->dev; |
John Garry | d3c4dd4 | 2017-04-10 21:22:00 +0800 | [diff] [blame] | 174 | struct domain_device *device = task->dev; |
| 175 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 176 | |
John Garry | d3c4dd4 | 2017-04-10 21:22:00 +0800 | [diff] [blame] | 177 | if (!sas_protocol_ata(task->task_proto)) |
| 178 | if (slot->n_elem) |
| 179 | dma_unmap_sg(dev, task->scatter, slot->n_elem, |
| 180 | task->data_dir); |
| 181 | |
| 182 | task->lldd_task = NULL; |
| 183 | |
| 184 | if (sas_dev) |
| 185 | atomic64_dec(&sas_dev->running_req); |
| 186 | } |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 187 | |
Xiaofei Tan | f557e32 | 2017-06-29 21:02:14 +0800 | [diff] [blame] | 188 | if (slot->buf) |
| 189 | dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma); |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 190 | |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 191 | |
| 192 | list_del_init(&slot->entry); |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 193 | slot->task = NULL; |
| 194 | slot->port = NULL; |
| 195 | hisi_sas_slot_index_free(hisi_hba, slot->idx); |
John Garry | d3c4dd4 | 2017-04-10 21:22:00 +0800 | [diff] [blame] | 196 | |
John Garry | 59ba49f | 2016-09-06 23:36:14 +0800 | [diff] [blame] | 197 | /* slot memory is fully zeroed when it is reused */ |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 198 | } |
| 199 | EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free); |
| 200 | |
John Garry | 66ee999 | 2015-11-18 00:50:54 +0800 | [diff] [blame] | 201 | static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba, |
| 202 | struct hisi_sas_slot *slot) |
| 203 | { |
| 204 | return hisi_hba->hw->prep_smp(hisi_hba, slot); |
| 205 | } |
| 206 | |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 207 | static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba, |
| 208 | struct hisi_sas_slot *slot, int is_tmf, |
| 209 | struct hisi_sas_tmf_task *tmf) |
| 210 | { |
| 211 | return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf); |
| 212 | } |
| 213 | |
John Garry | 6f2ff1a | 2016-01-26 02:47:20 +0800 | [diff] [blame] | 214 | static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba, |
| 215 | struct hisi_sas_slot *slot) |
| 216 | { |
| 217 | return hisi_hba->hw->prep_stp(hisi_hba, slot); |
| 218 | } |
| 219 | |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 220 | static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba, |
| 221 | struct hisi_sas_slot *slot, |
| 222 | int device_id, int abort_flag, int tag_to_abort) |
| 223 | { |
| 224 | return hisi_hba->hw->prep_abort(hisi_hba, slot, |
| 225 | device_id, abort_flag, tag_to_abort); |
| 226 | } |
| 227 | |
John Garry | cac9b2a | 2016-02-25 17:42:11 +0800 | [diff] [blame] | 228 | /* |
| 229 | * This function will issue an abort TMF regardless of whether the |
| 230 | * task is in the sdev or not. Then it will do the task complete |
| 231 | * cleanup and callbacks. |
| 232 | */ |
| 233 | static void hisi_sas_slot_abort(struct work_struct *work) |
| 234 | { |
| 235 | struct hisi_sas_slot *abort_slot = |
| 236 | container_of(work, struct hisi_sas_slot, abort_slot); |
| 237 | struct sas_task *task = abort_slot->task; |
| 238 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev); |
| 239 | struct scsi_cmnd *cmnd = task->uldd_task; |
| 240 | struct hisi_sas_tmf_task tmf_task; |
John Garry | cac9b2a | 2016-02-25 17:42:11 +0800 | [diff] [blame] | 241 | struct scsi_lun lun; |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 242 | struct device *dev = hisi_hba->dev; |
John Garry | cac9b2a | 2016-02-25 17:42:11 +0800 | [diff] [blame] | 243 | int tag = abort_slot->idx; |
John Garry | da7b66e | 2017-01-03 20:24:50 +0800 | [diff] [blame] | 244 | unsigned long flags; |
John Garry | cac9b2a | 2016-02-25 17:42:11 +0800 | [diff] [blame] | 245 | |
| 246 | if (!(task->task_proto & SAS_PROTOCOL_SSP)) { |
| 247 | dev_err(dev, "cannot abort slot for non-ssp task\n"); |
| 248 | goto out; |
| 249 | } |
| 250 | |
| 251 | int_to_scsilun(cmnd->device->lun, &lun); |
| 252 | tmf_task.tmf = TMF_ABORT_TASK; |
| 253 | tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag); |
| 254 | |
| 255 | hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task); |
| 256 | out: |
| 257 | /* Do cleanup for this task */ |
John Garry | da7b66e | 2017-01-03 20:24:50 +0800 | [diff] [blame] | 258 | spin_lock_irqsave(&hisi_hba->lock, flags); |
John Garry | cac9b2a | 2016-02-25 17:42:11 +0800 | [diff] [blame] | 259 | hisi_sas_slot_task_free(hisi_hba, task, abort_slot); |
John Garry | da7b66e | 2017-01-03 20:24:50 +0800 | [diff] [blame] | 260 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
John Garry | cac9b2a | 2016-02-25 17:42:11 +0800 | [diff] [blame] | 261 | if (task->task_done) |
| 262 | task->task_done(task); |
John Garry | cac9b2a | 2016-02-25 17:42:11 +0800 | [diff] [blame] | 263 | } |
| 264 | |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 265 | static int hisi_sas_task_prep(struct sas_task *task, struct hisi_sas_dq |
| 266 | *dq, int is_tmf, struct hisi_sas_tmf_task *tmf, |
| 267 | int *pass) |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 268 | { |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 269 | struct hisi_hba *hisi_hba = dq->hisi_hba; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 270 | struct domain_device *device = task->dev; |
| 271 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 272 | struct hisi_sas_port *port; |
| 273 | struct hisi_sas_slot *slot; |
| 274 | struct hisi_sas_cmd_hdr *cmd_hdr_base; |
John Garry | 2e244f0 | 2017-03-23 01:25:17 +0800 | [diff] [blame] | 275 | struct asd_sas_port *sas_port = device->port; |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 276 | struct device *dev = hisi_hba->dev; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 277 | int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx; |
John Garry | 54c9dd2 | 2017-03-23 01:25:29 +0800 | [diff] [blame] | 278 | unsigned long flags; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 279 | |
John Garry | 2e244f0 | 2017-03-23 01:25:17 +0800 | [diff] [blame] | 280 | if (!sas_port) { |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 281 | struct task_status_struct *ts = &task->task_status; |
| 282 | |
| 283 | ts->resp = SAS_TASK_UNDELIVERED; |
| 284 | ts->stat = SAS_PHY_DOWN; |
| 285 | /* |
| 286 | * libsas will use dev->port, should |
| 287 | * not call task_done for sata |
| 288 | */ |
| 289 | if (device->dev_type != SAS_SATA_DEV) |
| 290 | task->task_done(task); |
John Garry | ddabca2 | 2017-03-23 01:25:22 +0800 | [diff] [blame] | 291 | return SAS_PHY_DOWN; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | if (DEV_IS_GONE(sas_dev)) { |
| 295 | if (sas_dev) |
John Garry | ad60483 | 2017-06-14 23:33:12 +0800 | [diff] [blame] | 296 | dev_info(dev, "task prep: device %d not ready\n", |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 297 | sas_dev->device_id); |
| 298 | else |
| 299 | dev_info(dev, "task prep: device %016llx not ready\n", |
| 300 | SAS_ADDR(device->sas_addr)); |
| 301 | |
John Garry | ddabca2 | 2017-03-23 01:25:22 +0800 | [diff] [blame] | 302 | return SAS_PHY_DOWN; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 303 | } |
John Garry | 2e244f0 | 2017-03-23 01:25:17 +0800 | [diff] [blame] | 304 | |
| 305 | port = to_hisi_sas_port(sas_port); |
John Garry | 9859f24 | 2016-08-24 19:05:52 +0800 | [diff] [blame] | 306 | if (port && !port->port_attached) { |
John Garry | 09fe9ec | 2016-09-06 23:36:18 +0800 | [diff] [blame] | 307 | dev_info(dev, "task prep: %s port%d not attach device\n", |
Xiang Chen | 6073b77 | 2017-03-23 01:25:38 +0800 | [diff] [blame] | 308 | (dev_is_sata(device)) ? |
John Garry | 09fe9ec | 2016-09-06 23:36:18 +0800 | [diff] [blame] | 309 | "SATA/STP" : "SAS", |
| 310 | device->port->id); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 311 | |
John Garry | 09fe9ec | 2016-09-06 23:36:18 +0800 | [diff] [blame] | 312 | return SAS_PHY_DOWN; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | if (!sas_protocol_ata(task->task_proto)) { |
| 316 | if (task->num_scatter) { |
| 317 | n_elem = dma_map_sg(dev, task->scatter, |
| 318 | task->num_scatter, task->data_dir); |
| 319 | if (!n_elem) { |
| 320 | rc = -ENOMEM; |
| 321 | goto prep_out; |
| 322 | } |
| 323 | } |
| 324 | } else |
| 325 | n_elem = task->num_scatter; |
| 326 | |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 327 | spin_lock_irqsave(&hisi_hba->lock, flags); |
John Garry | 685b6d6 | 2016-04-15 21:36:36 +0800 | [diff] [blame] | 328 | if (hisi_hba->hw->slot_index_alloc) |
| 329 | rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx, |
| 330 | device); |
| 331 | else |
| 332 | rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx); |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 333 | if (rc) { |
| 334 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 335 | goto err_out; |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 336 | } |
| 337 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 338 | |
| 339 | rc = hisi_hba->hw->get_free_slot(hisi_hba, dq); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 340 | if (rc) |
| 341 | goto err_out_tag; |
| 342 | |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 343 | dlvry_queue = dq->id; |
| 344 | dlvry_queue_slot = dq->wr_point; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 345 | slot = &hisi_hba->slot_info[slot_idx]; |
| 346 | memset(slot, 0, sizeof(struct hisi_sas_slot)); |
| 347 | |
| 348 | slot->idx = slot_idx; |
| 349 | slot->n_elem = n_elem; |
| 350 | slot->dlvry_queue = dlvry_queue; |
| 351 | slot->dlvry_queue_slot = dlvry_queue_slot; |
| 352 | cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue]; |
| 353 | slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot]; |
| 354 | slot->task = task; |
| 355 | slot->port = port; |
| 356 | task->lldd_task = slot; |
John Garry | cac9b2a | 2016-02-25 17:42:11 +0800 | [diff] [blame] | 357 | INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 358 | |
Xiaofei Tan | f557e32 | 2017-06-29 21:02:14 +0800 | [diff] [blame] | 359 | slot->buf = dma_pool_alloc(hisi_hba->buffer_pool, |
| 360 | GFP_ATOMIC, &slot->buf_dma); |
| 361 | if (!slot->buf) { |
Dan Carpenter | 9c9d18e | 2015-12-09 13:48:36 +0300 | [diff] [blame] | 362 | rc = -ENOMEM; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 363 | goto err_out_slot_buf; |
Dan Carpenter | 9c9d18e | 2015-12-09 13:48:36 +0300 | [diff] [blame] | 364 | } |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 365 | memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr)); |
Xiaofei Tan | f557e32 | 2017-06-29 21:02:14 +0800 | [diff] [blame] | 366 | memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ); |
| 367 | memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 368 | |
| 369 | switch (task->task_proto) { |
John Garry | 66ee999 | 2015-11-18 00:50:54 +0800 | [diff] [blame] | 370 | case SAS_PROTOCOL_SMP: |
| 371 | rc = hisi_sas_task_prep_smp(hisi_hba, slot); |
| 372 | break; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 373 | case SAS_PROTOCOL_SSP: |
| 374 | rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf); |
| 375 | break; |
| 376 | case SAS_PROTOCOL_SATA: |
| 377 | case SAS_PROTOCOL_STP: |
| 378 | case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: |
John Garry | 6f2ff1a | 2016-01-26 02:47:20 +0800 | [diff] [blame] | 379 | rc = hisi_sas_task_prep_ata(hisi_hba, slot); |
| 380 | break; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 381 | default: |
| 382 | dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n", |
| 383 | task->task_proto); |
| 384 | rc = -EINVAL; |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | if (rc) { |
| 389 | dev_err(dev, "task prep: rc = 0x%x\n", rc); |
Xiaofei Tan | f557e32 | 2017-06-29 21:02:14 +0800 | [diff] [blame] | 390 | goto err_out_buf; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 391 | } |
| 392 | |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 393 | list_add_tail(&slot->entry, &sas_dev->list); |
John Garry | 54c9dd2 | 2017-03-23 01:25:29 +0800 | [diff] [blame] | 394 | spin_lock_irqsave(&task->task_state_lock, flags); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 395 | task->task_state_flags |= SAS_TASK_AT_INITIATOR; |
John Garry | 54c9dd2 | 2017-03-23 01:25:29 +0800 | [diff] [blame] | 396 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 397 | |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 398 | dq->slot_prep = slot; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 399 | |
John Garry | f696cc3 | 2016-11-07 20:48:39 +0800 | [diff] [blame] | 400 | atomic64_inc(&sas_dev->running_req); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 401 | ++(*pass); |
| 402 | |
Dan Carpenter | 9c9d18e | 2015-12-09 13:48:36 +0300 | [diff] [blame] | 403 | return 0; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 404 | |
Xiaofei Tan | f557e32 | 2017-06-29 21:02:14 +0800 | [diff] [blame] | 405 | err_out_buf: |
| 406 | dma_pool_free(hisi_hba->buffer_pool, slot->buf, |
| 407 | slot->buf_dma); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 408 | err_out_slot_buf: |
| 409 | /* Nothing to be done */ |
| 410 | err_out_tag: |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 411 | spin_lock_irqsave(&hisi_hba->lock, flags); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 412 | hisi_sas_slot_index_free(hisi_hba, slot_idx); |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 413 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 414 | err_out: |
| 415 | dev_err(dev, "task prep: failed[%d]!\n", rc); |
| 416 | if (!sas_protocol_ata(task->task_proto)) |
| 417 | if (n_elem) |
| 418 | dma_unmap_sg(dev, task->scatter, n_elem, |
| 419 | task->data_dir); |
| 420 | prep_out: |
| 421 | return rc; |
| 422 | } |
| 423 | |
| 424 | static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags, |
| 425 | int is_tmf, struct hisi_sas_tmf_task *tmf) |
| 426 | { |
| 427 | u32 rc; |
| 428 | u32 pass = 0; |
| 429 | unsigned long flags; |
| 430 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev); |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 431 | struct device *dev = hisi_hba->dev; |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 432 | struct domain_device *device = task->dev; |
| 433 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 434 | struct hisi_sas_dq *dq = sas_dev->dq; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 435 | |
Xiaofei Tan | 917d3bd | 2017-08-11 00:09:26 +0800 | [diff] [blame^] | 436 | if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 437 | return -EINVAL; |
| 438 | |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 439 | /* protect task_prep and start_delivery sequence */ |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 440 | spin_lock_irqsave(&dq->lock, flags); |
| 441 | rc = hisi_sas_task_prep(task, dq, is_tmf, tmf, &pass); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 442 | if (rc) |
| 443 | dev_err(dev, "task exec: failed[%d]!\n", rc); |
| 444 | |
| 445 | if (likely(pass)) |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 446 | hisi_hba->hw->start_delivery(dq); |
| 447 | spin_unlock_irqrestore(&dq->lock, flags); |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 448 | |
| 449 | return rc; |
| 450 | } |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 451 | |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 452 | static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no) |
| 453 | { |
| 454 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; |
| 455 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 456 | struct sas_ha_struct *sas_ha; |
| 457 | |
| 458 | if (!phy->phy_attached) |
| 459 | return; |
| 460 | |
| 461 | sas_ha = &hisi_hba->sha; |
| 462 | sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE); |
| 463 | |
| 464 | if (sas_phy->phy) { |
| 465 | struct sas_phy *sphy = sas_phy->phy; |
| 466 | |
| 467 | sphy->negotiated_linkrate = sas_phy->linkrate; |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 468 | sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS; |
Xiang Chen | 2ae7578 | 2016-11-07 20:48:40 +0800 | [diff] [blame] | 469 | sphy->maximum_linkrate_hw = |
| 470 | hisi_hba->hw->phy_get_max_linkrate(); |
| 471 | if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) |
| 472 | sphy->minimum_linkrate = phy->minimum_linkrate; |
| 473 | |
| 474 | if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) |
| 475 | sphy->maximum_linkrate = phy->maximum_linkrate; |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | if (phy->phy_type & PORT_TYPE_SAS) { |
| 479 | struct sas_identify_frame *id; |
| 480 | |
| 481 | id = (struct sas_identify_frame *)phy->frame_rcvd; |
| 482 | id->dev_type = phy->identify.device_type; |
| 483 | id->initiator_bits = SAS_PROTOCOL_ALL; |
| 484 | id->target_bits = phy->identify.target_port_protocols; |
| 485 | } else if (phy->phy_type & PORT_TYPE_SATA) { |
| 486 | /*Nothing*/ |
| 487 | } |
| 488 | |
| 489 | sas_phy->frame_rcvd_size = phy->frame_rcvd_size; |
| 490 | sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED); |
| 491 | } |
| 492 | |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 493 | static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device) |
| 494 | { |
| 495 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
| 496 | struct hisi_sas_device *sas_dev = NULL; |
| 497 | int i; |
| 498 | |
| 499 | spin_lock(&hisi_hba->lock); |
| 500 | for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { |
| 501 | if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) { |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 502 | int queue = i % hisi_hba->queue_count; |
| 503 | struct hisi_sas_dq *dq = &hisi_hba->dq[queue]; |
| 504 | |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 505 | hisi_hba->devices[i].device_id = i; |
| 506 | sas_dev = &hisi_hba->devices[i]; |
| 507 | sas_dev->dev_status = HISI_SAS_DEV_NORMAL; |
| 508 | sas_dev->dev_type = device->dev_type; |
| 509 | sas_dev->hisi_hba = hisi_hba; |
| 510 | sas_dev->sas_device = device; |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 511 | sas_dev->dq = dq; |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 512 | INIT_LIST_HEAD(&hisi_hba->devices[i].list); |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 513 | break; |
| 514 | } |
| 515 | } |
| 516 | spin_unlock(&hisi_hba->lock); |
| 517 | |
| 518 | return sas_dev; |
| 519 | } |
| 520 | |
| 521 | static int hisi_sas_dev_found(struct domain_device *device) |
| 522 | { |
| 523 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
| 524 | struct domain_device *parent_dev = device->parent; |
| 525 | struct hisi_sas_device *sas_dev; |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 526 | struct device *dev = hisi_hba->dev; |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 527 | |
John Garry | 685b6d6 | 2016-04-15 21:36:36 +0800 | [diff] [blame] | 528 | if (hisi_hba->hw->alloc_dev) |
| 529 | sas_dev = hisi_hba->hw->alloc_dev(device); |
| 530 | else |
| 531 | sas_dev = hisi_sas_alloc_dev(device); |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 532 | if (!sas_dev) { |
| 533 | dev_err(dev, "fail alloc dev: max support %d devices\n", |
| 534 | HISI_SAS_MAX_DEVICES); |
| 535 | return -EINVAL; |
| 536 | } |
| 537 | |
| 538 | device->lldd_dev = sas_dev; |
| 539 | hisi_hba->hw->setup_itct(hisi_hba, sas_dev); |
| 540 | |
| 541 | if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) { |
| 542 | int phy_no; |
| 543 | u8 phy_num = parent_dev->ex_dev.num_phys; |
| 544 | struct ex_phy *phy; |
| 545 | |
| 546 | for (phy_no = 0; phy_no < phy_num; phy_no++) { |
| 547 | phy = &parent_dev->ex_dev.ex_phy[phy_no]; |
| 548 | if (SAS_ADDR(phy->attached_sas_addr) == |
| 549 | SAS_ADDR(device->sas_addr)) { |
| 550 | sas_dev->attached_phy = phy_no; |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | if (phy_no == phy_num) { |
| 556 | dev_info(dev, "dev found: no attached " |
| 557 | "dev:%016llx at ex:%016llx\n", |
| 558 | SAS_ADDR(device->sas_addr), |
| 559 | SAS_ADDR(parent_dev->sas_addr)); |
| 560 | return -EINVAL; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
John Garry | 31eec8a | 2016-02-25 17:42:14 +0800 | [diff] [blame] | 567 | static int hisi_sas_slave_configure(struct scsi_device *sdev) |
| 568 | { |
| 569 | struct domain_device *dev = sdev_to_domain_dev(sdev); |
| 570 | int ret = sas_slave_configure(sdev); |
| 571 | |
| 572 | if (ret) |
| 573 | return ret; |
| 574 | if (!dev_is_sata(dev)) |
| 575 | sas_change_queue_depth(sdev, 64); |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
John Garry | 701f75e | 2015-11-18 00:50:55 +0800 | [diff] [blame] | 580 | static void hisi_sas_scan_start(struct Scsi_Host *shost) |
| 581 | { |
| 582 | struct hisi_hba *hisi_hba = shost_priv(shost); |
John Garry | 701f75e | 2015-11-18 00:50:55 +0800 | [diff] [blame] | 583 | |
John Garry | 396b804 | 2017-03-23 01:25:19 +0800 | [diff] [blame] | 584 | hisi_hba->hw->phys_init(hisi_hba); |
John Garry | 701f75e | 2015-11-18 00:50:55 +0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time) |
| 588 | { |
| 589 | struct hisi_hba *hisi_hba = shost_priv(shost); |
| 590 | struct sas_ha_struct *sha = &hisi_hba->sha; |
| 591 | |
John Garry | 396b804 | 2017-03-23 01:25:19 +0800 | [diff] [blame] | 592 | /* Wait for PHY up interrupt to occur */ |
| 593 | if (time < HZ) |
John Garry | 701f75e | 2015-11-18 00:50:55 +0800 | [diff] [blame] | 594 | return 0; |
| 595 | |
| 596 | sas_drain_work(sha); |
| 597 | return 1; |
| 598 | } |
| 599 | |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 600 | static void hisi_sas_phyup_work(struct work_struct *work) |
| 601 | { |
| 602 | struct hisi_sas_phy *phy = |
| 603 | container_of(work, struct hisi_sas_phy, phyup_ws); |
| 604 | struct hisi_hba *hisi_hba = phy->hisi_hba; |
| 605 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 606 | int phy_no = sas_phy->id; |
| 607 | |
| 608 | hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */ |
| 609 | hisi_sas_bytes_dmaed(hisi_hba, phy_no); |
| 610 | } |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 611 | |
| 612 | static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no) |
| 613 | { |
| 614 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; |
| 615 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 616 | |
| 617 | phy->hisi_hba = hisi_hba; |
| 618 | phy->port = NULL; |
| 619 | init_timer(&phy->timer); |
| 620 | sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0; |
| 621 | sas_phy->class = SAS; |
| 622 | sas_phy->iproto = SAS_PROTOCOL_ALL; |
| 623 | sas_phy->tproto = 0; |
| 624 | sas_phy->type = PHY_TYPE_PHYSICAL; |
| 625 | sas_phy->role = PHY_ROLE_INITIATOR; |
| 626 | sas_phy->oob_mode = OOB_NOT_CONNECTED; |
| 627 | sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN; |
| 628 | sas_phy->id = phy_no; |
| 629 | sas_phy->sas_addr = &hisi_hba->sas_addr[0]; |
| 630 | sas_phy->frame_rcvd = &phy->frame_rcvd[0]; |
| 631 | sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata; |
| 632 | sas_phy->lldd_phy = phy; |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 633 | |
| 634 | INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work); |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 635 | } |
| 636 | |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 637 | static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy) |
| 638 | { |
| 639 | struct sas_ha_struct *sas_ha = sas_phy->ha; |
| 640 | struct hisi_hba *hisi_hba = sas_ha->lldd_ha; |
| 641 | struct hisi_sas_phy *phy = sas_phy->lldd_phy; |
| 642 | struct asd_sas_port *sas_port = sas_phy->port; |
John Garry | 2e244f0 | 2017-03-23 01:25:17 +0800 | [diff] [blame] | 643 | struct hisi_sas_port *port = to_hisi_sas_port(sas_port); |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 644 | unsigned long flags; |
| 645 | |
| 646 | if (!sas_port) |
| 647 | return; |
| 648 | |
| 649 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 650 | port->port_attached = 1; |
| 651 | port->id = phy->port_id; |
| 652 | phy->port = port; |
| 653 | sas_port->lldd_port = port; |
| 654 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 655 | } |
| 656 | |
John Garry | d3c4dd4 | 2017-04-10 21:22:00 +0800 | [diff] [blame] | 657 | static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task, |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 658 | struct hisi_sas_slot *slot) |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 659 | { |
John Garry | d3c4dd4 | 2017-04-10 21:22:00 +0800 | [diff] [blame] | 660 | if (task) { |
| 661 | unsigned long flags; |
| 662 | struct task_status_struct *ts; |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 663 | |
John Garry | d3c4dd4 | 2017-04-10 21:22:00 +0800 | [diff] [blame] | 664 | ts = &task->task_status; |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 665 | |
John Garry | d3c4dd4 | 2017-04-10 21:22:00 +0800 | [diff] [blame] | 666 | ts->resp = SAS_TASK_COMPLETE; |
| 667 | ts->stat = SAS_ABORTED_TASK; |
| 668 | spin_lock_irqsave(&task->task_state_lock, flags); |
| 669 | task->task_state_flags &= |
| 670 | ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR); |
| 671 | task->task_state_flags |= SAS_TASK_STATE_DONE; |
| 672 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 673 | } |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 674 | |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 675 | hisi_sas_slot_task_free(hisi_hba, task, slot); |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 676 | } |
| 677 | |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 678 | /* hisi_hba.lock should be locked */ |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 679 | static void hisi_sas_release_task(struct hisi_hba *hisi_hba, |
| 680 | struct domain_device *device) |
| 681 | { |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 682 | struct hisi_sas_slot *slot, *slot2; |
| 683 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 684 | |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 685 | list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry) |
| 686 | hisi_sas_do_release_task(hisi_hba, slot->task, slot); |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 687 | } |
| 688 | |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 689 | static void hisi_sas_release_tasks(struct hisi_hba *hisi_hba) |
| 690 | { |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 691 | struct hisi_sas_device *sas_dev; |
| 692 | struct domain_device *device; |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 693 | int i; |
| 694 | |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 695 | for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { |
| 696 | sas_dev = &hisi_hba->devices[i]; |
| 697 | device = sas_dev->sas_device; |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 698 | |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 699 | if ((sas_dev->dev_type == SAS_PHY_UNUSED) || |
| 700 | !device) |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 701 | continue; |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 702 | |
| 703 | hisi_sas_release_task(hisi_hba, device); |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
Xiang Chen | d30ff26 | 2017-06-14 23:33:32 +0800 | [diff] [blame] | 707 | static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba, |
| 708 | struct domain_device *device) |
| 709 | { |
| 710 | if (hisi_hba->hw->dereg_device) |
| 711 | hisi_hba->hw->dereg_device(hisi_hba, device); |
| 712 | } |
| 713 | |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 714 | static void hisi_sas_dev_gone(struct domain_device *device) |
| 715 | { |
| 716 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 717 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 718 | struct device *dev = hisi_hba->dev; |
John Garry | ad60483 | 2017-06-14 23:33:12 +0800 | [diff] [blame] | 719 | int dev_id = sas_dev->device_id; |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 720 | |
John Garry | ad60483 | 2017-06-14 23:33:12 +0800 | [diff] [blame] | 721 | dev_info(dev, "found dev[%d:%x] is gone\n", |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 722 | sas_dev->device_id, sas_dev->dev_type); |
| 723 | |
John Garry | 40f2702 | 2016-08-24 19:05:48 +0800 | [diff] [blame] | 724 | hisi_sas_internal_task_abort(hisi_hba, device, |
| 725 | HISI_SAS_INT_ABT_DEV, 0); |
| 726 | |
Xiang Chen | d30ff26 | 2017-06-14 23:33:32 +0800 | [diff] [blame] | 727 | hisi_sas_dereg_device(hisi_hba, device); |
| 728 | |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 729 | hisi_hba->hw->free_device(hisi_hba, sas_dev); |
| 730 | device->lldd_dev = NULL; |
| 731 | memset(sas_dev, 0, sizeof(*sas_dev)); |
| 732 | sas_dev->device_id = dev_id; |
| 733 | sas_dev->dev_type = SAS_PHY_UNUSED; |
| 734 | sas_dev->dev_status = HISI_SAS_DEV_NORMAL; |
| 735 | } |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 736 | |
| 737 | static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags) |
| 738 | { |
| 739 | return hisi_sas_task_exec(task, gfp_flags, 0, NULL); |
| 740 | } |
| 741 | |
John Garry | e4189d5 | 2015-11-18 00:50:57 +0800 | [diff] [blame] | 742 | static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func, |
| 743 | void *funcdata) |
| 744 | { |
| 745 | struct sas_ha_struct *sas_ha = sas_phy->ha; |
| 746 | struct hisi_hba *hisi_hba = sas_ha->lldd_ha; |
| 747 | int phy_no = sas_phy->id; |
| 748 | |
| 749 | switch (func) { |
| 750 | case PHY_FUNC_HARD_RESET: |
| 751 | hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no); |
| 752 | break; |
| 753 | |
| 754 | case PHY_FUNC_LINK_RESET: |
John Garry | b4c67a6 | 2017-03-23 01:25:23 +0800 | [diff] [blame] | 755 | hisi_hba->hw->phy_disable(hisi_hba, phy_no); |
| 756 | msleep(100); |
John Garry | e4189d5 | 2015-11-18 00:50:57 +0800 | [diff] [blame] | 757 | hisi_hba->hw->phy_enable(hisi_hba, phy_no); |
John Garry | e4189d5 | 2015-11-18 00:50:57 +0800 | [diff] [blame] | 758 | break; |
| 759 | |
| 760 | case PHY_FUNC_DISABLE: |
| 761 | hisi_hba->hw->phy_disable(hisi_hba, phy_no); |
| 762 | break; |
| 763 | |
| 764 | case PHY_FUNC_SET_LINK_RATE: |
Xiang Chen | 2ae7578 | 2016-11-07 20:48:40 +0800 | [diff] [blame] | 765 | hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata); |
| 766 | break; |
| 767 | |
John Garry | e4189d5 | 2015-11-18 00:50:57 +0800 | [diff] [blame] | 768 | case PHY_FUNC_RELEASE_SPINUP_HOLD: |
| 769 | default: |
| 770 | return -EOPNOTSUPP; |
| 771 | } |
| 772 | return 0; |
| 773 | } |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 774 | |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 775 | static void hisi_sas_task_done(struct sas_task *task) |
| 776 | { |
| 777 | if (!del_timer(&task->slow_task->timer)) |
| 778 | return; |
| 779 | complete(&task->slow_task->completion); |
| 780 | } |
| 781 | |
| 782 | static void hisi_sas_tmf_timedout(unsigned long data) |
| 783 | { |
| 784 | struct sas_task *task = (struct sas_task *)data; |
Xiang Chen | f64a698 | 2017-06-14 23:33:11 +0800 | [diff] [blame] | 785 | unsigned long flags; |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 786 | |
Xiang Chen | f64a698 | 2017-06-14 23:33:11 +0800 | [diff] [blame] | 787 | spin_lock_irqsave(&task->task_state_lock, flags); |
| 788 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) |
| 789 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; |
| 790 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 791 | |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 792 | complete(&task->slow_task->completion); |
| 793 | } |
| 794 | |
| 795 | #define TASK_TIMEOUT 20 |
| 796 | #define TASK_RETRY 3 |
| 797 | static int hisi_sas_exec_internal_tmf_task(struct domain_device *device, |
| 798 | void *parameter, u32 para_len, |
| 799 | struct hisi_sas_tmf_task *tmf) |
| 800 | { |
| 801 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 802 | struct hisi_hba *hisi_hba = sas_dev->hisi_hba; |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 803 | struct device *dev = hisi_hba->dev; |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 804 | struct sas_task *task; |
| 805 | int res, retry; |
| 806 | |
| 807 | for (retry = 0; retry < TASK_RETRY; retry++) { |
| 808 | task = sas_alloc_slow_task(GFP_KERNEL); |
| 809 | if (!task) |
| 810 | return -ENOMEM; |
| 811 | |
| 812 | task->dev = device; |
| 813 | task->task_proto = device->tproto; |
| 814 | |
Xiang Chen | 7c594f0 | 2017-03-23 01:25:20 +0800 | [diff] [blame] | 815 | if (dev_is_sata(device)) { |
| 816 | task->ata_task.device_control_reg_update = 1; |
| 817 | memcpy(&task->ata_task.fis, parameter, para_len); |
| 818 | } else { |
| 819 | memcpy(&task->ssp_task, parameter, para_len); |
| 820 | } |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 821 | task->task_done = hisi_sas_task_done; |
| 822 | |
| 823 | task->slow_task->timer.data = (unsigned long) task; |
| 824 | task->slow_task->timer.function = hisi_sas_tmf_timedout; |
| 825 | task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ; |
| 826 | add_timer(&task->slow_task->timer); |
| 827 | |
| 828 | res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf); |
| 829 | |
| 830 | if (res) { |
| 831 | del_timer(&task->slow_task->timer); |
| 832 | dev_err(dev, "abort tmf: executing internal task failed: %d\n", |
| 833 | res); |
| 834 | goto ex_err; |
| 835 | } |
| 836 | |
| 837 | wait_for_completion(&task->slow_task->completion); |
| 838 | res = TMF_RESP_FUNC_FAILED; |
| 839 | /* Even TMF timed out, return direct. */ |
| 840 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { |
| 841 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { |
John Garry | d3c4dd4 | 2017-04-10 21:22:00 +0800 | [diff] [blame] | 842 | struct hisi_sas_slot *slot = task->lldd_task; |
| 843 | |
Xiang Chen | 7c594f0 | 2017-03-23 01:25:20 +0800 | [diff] [blame] | 844 | dev_err(dev, "abort tmf: TMF task timeout\n"); |
John Garry | d3c4dd4 | 2017-04-10 21:22:00 +0800 | [diff] [blame] | 845 | if (slot) |
| 846 | slot->task = NULL; |
| 847 | |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 848 | goto ex_err; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
John Garry | 1af1b80 | 2016-02-25 17:42:10 +0800 | [diff] [blame] | 853 | task->task_status.stat == TMF_RESP_FUNC_COMPLETE) { |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 854 | res = TMF_RESP_FUNC_COMPLETE; |
| 855 | break; |
| 856 | } |
| 857 | |
| 858 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
John Garry | 4ffde48 | 2016-08-24 19:05:53 +0800 | [diff] [blame] | 859 | task->task_status.stat == TMF_RESP_FUNC_SUCC) { |
| 860 | res = TMF_RESP_FUNC_SUCC; |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 865 | task->task_status.stat == SAS_DATA_UNDERRUN) { |
| 866 | /* no error, but return the number of bytes of |
| 867 | * underrun |
| 868 | */ |
| 869 | dev_warn(dev, "abort tmf: task to dev %016llx " |
| 870 | "resp: 0x%x sts 0x%x underrun\n", |
| 871 | SAS_ADDR(device->sas_addr), |
| 872 | task->task_status.resp, |
| 873 | task->task_status.stat); |
| 874 | res = task->task_status.residual; |
| 875 | break; |
| 876 | } |
| 877 | |
| 878 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
| 879 | task->task_status.stat == SAS_DATA_OVERRUN) { |
| 880 | dev_warn(dev, "abort tmf: blocked task error\n"); |
| 881 | res = -EMSGSIZE; |
| 882 | break; |
| 883 | } |
| 884 | |
| 885 | dev_warn(dev, "abort tmf: task to dev " |
| 886 | "%016llx resp: 0x%x status 0x%x\n", |
| 887 | SAS_ADDR(device->sas_addr), task->task_status.resp, |
| 888 | task->task_status.stat); |
| 889 | sas_free_task(task); |
| 890 | task = NULL; |
| 891 | } |
| 892 | ex_err: |
Xiang Chen | d2d7e7a | 2016-11-07 20:48:34 +0800 | [diff] [blame] | 893 | if (retry == TASK_RETRY) |
| 894 | dev_warn(dev, "abort tmf: executing internal task failed!\n"); |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 895 | sas_free_task(task); |
| 896 | return res; |
| 897 | } |
| 898 | |
Xiang Chen | 7c594f0 | 2017-03-23 01:25:20 +0800 | [diff] [blame] | 899 | static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev, |
| 900 | bool reset, int pmp, u8 *fis) |
| 901 | { |
| 902 | struct ata_taskfile tf; |
| 903 | |
| 904 | ata_tf_init(dev, &tf); |
| 905 | if (reset) |
| 906 | tf.ctl |= ATA_SRST; |
| 907 | else |
| 908 | tf.ctl &= ~ATA_SRST; |
| 909 | tf.command = ATA_CMD_DEV_RESET; |
| 910 | ata_tf_to_fis(&tf, pmp, 0, fis); |
| 911 | } |
| 912 | |
| 913 | static int hisi_sas_softreset_ata_disk(struct domain_device *device) |
| 914 | { |
| 915 | u8 fis[20] = {0}; |
| 916 | struct ata_port *ap = device->sata_dev.ap; |
| 917 | struct ata_link *link; |
| 918 | int rc = TMF_RESP_FUNC_FAILED; |
| 919 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 920 | struct device *dev = hisi_hba->dev; |
Xiang Chen | 7c594f0 | 2017-03-23 01:25:20 +0800 | [diff] [blame] | 921 | int s = sizeof(struct host_to_dev_fis); |
| 922 | unsigned long flags; |
| 923 | |
| 924 | ata_for_each_link(link, ap, EDGE) { |
| 925 | int pmp = sata_srst_pmp(link); |
| 926 | |
| 927 | hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis); |
| 928 | rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL); |
| 929 | if (rc != TMF_RESP_FUNC_COMPLETE) |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | if (rc == TMF_RESP_FUNC_COMPLETE) { |
| 934 | ata_for_each_link(link, ap, EDGE) { |
| 935 | int pmp = sata_srst_pmp(link); |
| 936 | |
| 937 | hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis); |
| 938 | rc = hisi_sas_exec_internal_tmf_task(device, fis, |
| 939 | s, NULL); |
| 940 | if (rc != TMF_RESP_FUNC_COMPLETE) |
| 941 | dev_err(dev, "ata disk de-reset failed\n"); |
| 942 | } |
| 943 | } else { |
| 944 | dev_err(dev, "ata disk reset failed\n"); |
| 945 | } |
| 946 | |
| 947 | if (rc == TMF_RESP_FUNC_COMPLETE) { |
| 948 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 949 | hisi_sas_release_task(hisi_hba, device); |
| 950 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 951 | } |
| 952 | |
| 953 | return rc; |
| 954 | } |
| 955 | |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 956 | static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device, |
| 957 | u8 *lun, struct hisi_sas_tmf_task *tmf) |
| 958 | { |
| 959 | struct sas_ssp_task ssp_task; |
| 960 | |
| 961 | if (!(device->tproto & SAS_PROTOCOL_SSP)) |
| 962 | return TMF_RESP_FUNC_ESUPP; |
| 963 | |
| 964 | memcpy(ssp_task.LUN, lun, 8); |
| 965 | |
| 966 | return hisi_sas_exec_internal_tmf_task(device, &ssp_task, |
| 967 | sizeof(ssp_task), tmf); |
| 968 | } |
| 969 | |
Xiaofei Tan | 917d3bd | 2017-08-11 00:09:26 +0800 | [diff] [blame^] | 970 | static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba, |
| 971 | struct asd_sas_port *sas_port, enum sas_linkrate linkrate) |
| 972 | { |
| 973 | struct hisi_sas_device *sas_dev; |
| 974 | struct domain_device *device; |
| 975 | int i; |
| 976 | |
| 977 | for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { |
| 978 | sas_dev = &hisi_hba->devices[i]; |
| 979 | device = sas_dev->sas_device; |
| 980 | if ((sas_dev->dev_type == SAS_PHY_UNUSED) |
| 981 | || !device || (device->port != sas_port)) |
| 982 | continue; |
| 983 | |
| 984 | hisi_hba->hw->free_device(hisi_hba, sas_dev); |
| 985 | |
| 986 | /* Update linkrate of directly attached device. */ |
| 987 | if (!device->parent) |
| 988 | device->linkrate = linkrate; |
| 989 | |
| 990 | hisi_hba->hw->setup_itct(hisi_hba, sas_dev); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state, |
| 995 | u32 state) |
| 996 | { |
| 997 | struct sas_ha_struct *sas_ha = &hisi_hba->sha; |
| 998 | struct asd_sas_port *_sas_port = NULL; |
| 999 | int phy_no; |
| 1000 | |
| 1001 | for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) { |
| 1002 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; |
| 1003 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 1004 | struct asd_sas_port *sas_port = sas_phy->port; |
| 1005 | struct hisi_sas_port *port = to_hisi_sas_port(sas_port); |
| 1006 | bool do_port_check = !!(_sas_port != sas_port); |
| 1007 | |
| 1008 | if (!sas_phy->phy->enabled) |
| 1009 | continue; |
| 1010 | |
| 1011 | /* Report PHY state change to libsas */ |
| 1012 | if (state & (1 << phy_no)) { |
| 1013 | if (do_port_check && sas_port) { |
| 1014 | struct domain_device *dev = sas_port->port_dev; |
| 1015 | |
| 1016 | _sas_port = sas_port; |
| 1017 | port->id = phy->port_id; |
| 1018 | hisi_sas_refresh_port_id(hisi_hba, |
| 1019 | sas_port, sas_phy->linkrate); |
| 1020 | |
| 1021 | if (DEV_IS_EXPANDER(dev->dev_type)) |
| 1022 | sas_ha->notify_port_event(sas_phy, |
| 1023 | PORTE_BROADCAST_RCVD); |
| 1024 | } |
| 1025 | } else if (old_state & (1 << phy_no)) |
| 1026 | /* PHY down but was up before */ |
| 1027 | hisi_sas_phy_down(hisi_hba, phy_no, 0); |
| 1028 | |
| 1029 | } |
| 1030 | |
| 1031 | drain_workqueue(hisi_hba->shost->work_q); |
| 1032 | } |
| 1033 | |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1034 | static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba) |
| 1035 | { |
Xiaofei Tan | 917d3bd | 2017-08-11 00:09:26 +0800 | [diff] [blame^] | 1036 | struct sas_ha_struct *sas_ha = &hisi_hba->sha; |
| 1037 | struct device *dev = hisi_hba->dev; |
| 1038 | struct Scsi_Host *shost = hisi_hba->shost; |
| 1039 | u32 old_state, state; |
| 1040 | unsigned long flags; |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1041 | int rc; |
| 1042 | |
| 1043 | if (!hisi_hba->hw->soft_reset) |
| 1044 | return -1; |
| 1045 | |
Xiaofei Tan | 917d3bd | 2017-08-11 00:09:26 +0800 | [diff] [blame^] | 1046 | if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1047 | return -1; |
| 1048 | |
Xiaofei Tan | 917d3bd | 2017-08-11 00:09:26 +0800 | [diff] [blame^] | 1049 | dev_dbg(dev, "controller resetting...\n"); |
| 1050 | old_state = hisi_hba->hw->get_phys_state(hisi_hba); |
| 1051 | |
| 1052 | scsi_block_requests(shost); |
| 1053 | set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags); |
| 1054 | rc = hisi_hba->hw->soft_reset(hisi_hba); |
| 1055 | if (rc) { |
| 1056 | dev_warn(dev, "controller reset failed (%d)\n", rc); |
| 1057 | clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags); |
| 1058 | goto out; |
| 1059 | } |
| 1060 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 1061 | hisi_sas_release_tasks(hisi_hba); |
| 1062 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 1063 | |
| 1064 | sas_ha->notify_ha_event(sas_ha, HAE_RESET); |
| 1065 | clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags); |
| 1066 | |
| 1067 | /* Init and wait for PHYs to come up and all libsas event finished. */ |
| 1068 | hisi_hba->hw->phys_init(hisi_hba); |
| 1069 | msleep(1000); |
| 1070 | drain_workqueue(hisi_hba->wq); |
| 1071 | drain_workqueue(shost->work_q); |
| 1072 | |
| 1073 | state = hisi_hba->hw->get_phys_state(hisi_hba); |
| 1074 | hisi_sas_rescan_topology(hisi_hba, old_state, state); |
| 1075 | dev_dbg(dev, "controller reset complete\n"); |
| 1076 | |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1077 | out: |
Xiaofei Tan | 917d3bd | 2017-08-11 00:09:26 +0800 | [diff] [blame^] | 1078 | scsi_unblock_requests(shost); |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1079 | clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags); |
Xiaofei Tan | 917d3bd | 2017-08-11 00:09:26 +0800 | [diff] [blame^] | 1080 | |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1081 | return rc; |
| 1082 | } |
| 1083 | |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1084 | static int hisi_sas_abort_task(struct sas_task *task) |
| 1085 | { |
| 1086 | struct scsi_lun lun; |
| 1087 | struct hisi_sas_tmf_task tmf_task; |
| 1088 | struct domain_device *device = task->dev; |
| 1089 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 1090 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev); |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 1091 | struct device *dev = hisi_hba->dev; |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1092 | int rc = TMF_RESP_FUNC_FAILED; |
| 1093 | unsigned long flags; |
| 1094 | |
| 1095 | if (!sas_dev) { |
| 1096 | dev_warn(dev, "Device has been removed\n"); |
| 1097 | return TMF_RESP_FUNC_FAILED; |
| 1098 | } |
| 1099 | |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1100 | if (task->task_state_flags & SAS_TASK_STATE_DONE) { |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1101 | rc = TMF_RESP_FUNC_COMPLETE; |
| 1102 | goto out; |
| 1103 | } |
| 1104 | |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1105 | sas_dev->dev_status = HISI_SAS_DEV_EH; |
| 1106 | if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) { |
| 1107 | struct scsi_cmnd *cmnd = task->uldd_task; |
| 1108 | struct hisi_sas_slot *slot = task->lldd_task; |
| 1109 | u32 tag = slot->idx; |
John Garry | c35279f | 2017-03-23 01:25:25 +0800 | [diff] [blame] | 1110 | int rc2; |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1111 | |
| 1112 | int_to_scsilun(cmnd->device->lun, &lun); |
| 1113 | tmf_task.tmf = TMF_ABORT_TASK; |
| 1114 | tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag); |
| 1115 | |
| 1116 | rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, |
| 1117 | &tmf_task); |
| 1118 | |
John Garry | c35279f | 2017-03-23 01:25:25 +0800 | [diff] [blame] | 1119 | rc2 = hisi_sas_internal_task_abort(hisi_hba, device, |
| 1120 | HISI_SAS_INT_ABT_CMD, tag); |
| 1121 | /* |
| 1122 | * If the TMF finds that the IO is not in the device and also |
| 1123 | * the internal abort does not succeed, then it is safe to |
| 1124 | * free the slot. |
| 1125 | * Note: if the internal abort succeeds then the slot |
| 1126 | * will have already been completed |
| 1127 | */ |
| 1128 | if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) { |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1129 | if (task->lldd_task) { |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1130 | spin_lock_irqsave(&hisi_hba->lock, flags); |
John Garry | c35279f | 2017-03-23 01:25:25 +0800 | [diff] [blame] | 1131 | hisi_sas_do_release_task(hisi_hba, task, slot); |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1132 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 1133 | } |
| 1134 | } |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1135 | } else if (task->task_proto & SAS_PROTOCOL_SATA || |
| 1136 | task->task_proto & SAS_PROTOCOL_STP) { |
| 1137 | if (task->dev->dev_type == SAS_SATA_DEV) { |
John Garry | dc8a49c | 2016-08-24 19:05:49 +0800 | [diff] [blame] | 1138 | hisi_sas_internal_task_abort(hisi_hba, device, |
| 1139 | HISI_SAS_INT_ABT_DEV, 0); |
Xiang Chen | d30ff26 | 2017-06-14 23:33:32 +0800 | [diff] [blame] | 1140 | hisi_sas_dereg_device(hisi_hba, device); |
Xiang Chen | 7c594f0 | 2017-03-23 01:25:20 +0800 | [diff] [blame] | 1141 | rc = hisi_sas_softreset_ata_disk(device); |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1142 | } |
Gustavo A. R. Silva | eb045e0 | 2017-05-22 13:00:29 -0500 | [diff] [blame] | 1143 | } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) { |
John Garry | dc8a49c | 2016-08-24 19:05:49 +0800 | [diff] [blame] | 1144 | /* SMP */ |
| 1145 | struct hisi_sas_slot *slot = task->lldd_task; |
| 1146 | u32 tag = slot->idx; |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1147 | |
Xiang Chen | ccbfe5a | 2017-03-23 01:25:36 +0800 | [diff] [blame] | 1148 | rc = hisi_sas_internal_task_abort(hisi_hba, device, |
| 1149 | HISI_SAS_INT_ABT_CMD, tag); |
| 1150 | if (rc == TMF_RESP_FUNC_FAILED) { |
| 1151 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 1152 | hisi_sas_do_release_task(hisi_hba, task, slot); |
| 1153 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 1154 | } |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | out: |
| 1158 | if (rc != TMF_RESP_FUNC_COMPLETE) |
| 1159 | dev_notice(dev, "abort task: rc=%d\n", rc); |
| 1160 | return rc; |
| 1161 | } |
| 1162 | |
| 1163 | static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun) |
| 1164 | { |
| 1165 | struct hisi_sas_tmf_task tmf_task; |
| 1166 | int rc = TMF_RESP_FUNC_FAILED; |
| 1167 | |
| 1168 | tmf_task.tmf = TMF_ABORT_TASK_SET; |
| 1169 | rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); |
| 1170 | |
| 1171 | return rc; |
| 1172 | } |
| 1173 | |
| 1174 | static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun) |
| 1175 | { |
| 1176 | int rc = TMF_RESP_FUNC_FAILED; |
| 1177 | struct hisi_sas_tmf_task tmf_task; |
| 1178 | |
| 1179 | tmf_task.tmf = TMF_CLEAR_ACA; |
| 1180 | rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); |
| 1181 | |
| 1182 | return rc; |
| 1183 | } |
| 1184 | |
| 1185 | static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device) |
| 1186 | { |
| 1187 | struct sas_phy *phy = sas_get_local_phy(device); |
| 1188 | int rc, reset_type = (device->dev_type == SAS_SATA_DEV || |
| 1189 | (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1; |
| 1190 | rc = sas_phy_reset(phy, reset_type); |
| 1191 | sas_put_local_phy(phy); |
| 1192 | msleep(2000); |
| 1193 | return rc; |
| 1194 | } |
| 1195 | |
| 1196 | static int hisi_sas_I_T_nexus_reset(struct domain_device *device) |
| 1197 | { |
| 1198 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 1199 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
| 1200 | unsigned long flags; |
| 1201 | int rc = TMF_RESP_FUNC_FAILED; |
| 1202 | |
| 1203 | if (sas_dev->dev_status != HISI_SAS_DEV_EH) |
| 1204 | return TMF_RESP_FUNC_FAILED; |
| 1205 | sas_dev->dev_status = HISI_SAS_DEV_NORMAL; |
| 1206 | |
Xiang Chen | d30ff26 | 2017-06-14 23:33:32 +0800 | [diff] [blame] | 1207 | hisi_sas_internal_task_abort(hisi_hba, device, |
| 1208 | HISI_SAS_INT_ABT_DEV, 0); |
| 1209 | hisi_sas_dereg_device(hisi_hba, device); |
| 1210 | |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1211 | rc = hisi_sas_debug_I_T_nexus_reset(device); |
| 1212 | |
Xiang Chen | 6131243 | 2017-03-23 01:25:28 +0800 | [diff] [blame] | 1213 | if (rc == TMF_RESP_FUNC_COMPLETE) { |
| 1214 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 1215 | hisi_sas_release_task(hisi_hba, device); |
| 1216 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 1217 | } |
| 1218 | return rc; |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun) |
| 1222 | { |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1223 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 1224 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 1225 | struct device *dev = hisi_hba->dev; |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1226 | unsigned long flags; |
| 1227 | int rc = TMF_RESP_FUNC_FAILED; |
| 1228 | |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1229 | sas_dev->dev_status = HISI_SAS_DEV_EH; |
John Garry | 055945d | 2017-03-23 01:25:26 +0800 | [diff] [blame] | 1230 | if (dev_is_sata(device)) { |
| 1231 | struct sas_phy *phy; |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1232 | |
John Garry | 055945d | 2017-03-23 01:25:26 +0800 | [diff] [blame] | 1233 | /* Clear internal IO and then hardreset */ |
| 1234 | rc = hisi_sas_internal_task_abort(hisi_hba, device, |
| 1235 | HISI_SAS_INT_ABT_DEV, 0); |
| 1236 | if (rc == TMF_RESP_FUNC_FAILED) |
| 1237 | goto out; |
Xiang Chen | d30ff26 | 2017-06-14 23:33:32 +0800 | [diff] [blame] | 1238 | hisi_sas_dereg_device(hisi_hba, device); |
John Garry | 055945d | 2017-03-23 01:25:26 +0800 | [diff] [blame] | 1239 | |
| 1240 | phy = sas_get_local_phy(device); |
| 1241 | |
| 1242 | rc = sas_phy_reset(phy, 1); |
| 1243 | |
| 1244 | if (rc == 0) { |
| 1245 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 1246 | hisi_sas_release_task(hisi_hba, device); |
| 1247 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 1248 | } |
| 1249 | sas_put_local_phy(phy); |
| 1250 | } else { |
| 1251 | struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET }; |
| 1252 | |
| 1253 | rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); |
| 1254 | if (rc == TMF_RESP_FUNC_COMPLETE) { |
| 1255 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 1256 | hisi_sas_release_task(hisi_hba, device); |
| 1257 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 1258 | } |
| 1259 | } |
| 1260 | out: |
John Garry | 14d3f39 | 2017-03-23 01:25:37 +0800 | [diff] [blame] | 1261 | if (rc != TMF_RESP_FUNC_COMPLETE) |
John Garry | ad60483 | 2017-06-14 23:33:12 +0800 | [diff] [blame] | 1262 | dev_err(dev, "lu_reset: for device[%d]:rc= %d\n", |
John Garry | 14d3f39 | 2017-03-23 01:25:37 +0800 | [diff] [blame] | 1263 | sas_dev->device_id, rc); |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1264 | return rc; |
| 1265 | } |
| 1266 | |
John Garry | 8b05ad6 | 2017-03-23 01:25:35 +0800 | [diff] [blame] | 1267 | static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha) |
| 1268 | { |
| 1269 | struct hisi_hba *hisi_hba = sas_ha->lldd_ha; |
| 1270 | |
| 1271 | return hisi_sas_controller_reset(hisi_hba); |
| 1272 | } |
| 1273 | |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1274 | static int hisi_sas_query_task(struct sas_task *task) |
| 1275 | { |
| 1276 | struct scsi_lun lun; |
| 1277 | struct hisi_sas_tmf_task tmf_task; |
| 1278 | int rc = TMF_RESP_FUNC_FAILED; |
| 1279 | |
| 1280 | if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) { |
| 1281 | struct scsi_cmnd *cmnd = task->uldd_task; |
| 1282 | struct domain_device *device = task->dev; |
| 1283 | struct hisi_sas_slot *slot = task->lldd_task; |
| 1284 | u32 tag = slot->idx; |
| 1285 | |
| 1286 | int_to_scsilun(cmnd->device->lun, &lun); |
| 1287 | tmf_task.tmf = TMF_QUERY_TASK; |
| 1288 | tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag); |
| 1289 | |
| 1290 | rc = hisi_sas_debug_issue_ssp_tmf(device, |
| 1291 | lun.scsi_lun, |
| 1292 | &tmf_task); |
| 1293 | switch (rc) { |
| 1294 | /* The task is still in Lun, release it then */ |
| 1295 | case TMF_RESP_FUNC_SUCC: |
| 1296 | /* The task is not in Lun or failed, reset the phy */ |
| 1297 | case TMF_RESP_FUNC_FAILED: |
| 1298 | case TMF_RESP_FUNC_COMPLETE: |
| 1299 | break; |
Xiang Chen | 997ee43 | 2016-11-07 20:48:35 +0800 | [diff] [blame] | 1300 | default: |
| 1301 | rc = TMF_RESP_FUNC_FAILED; |
| 1302 | break; |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1303 | } |
| 1304 | } |
| 1305 | return rc; |
| 1306 | } |
| 1307 | |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1308 | static int |
John Garry | ad60483 | 2017-06-14 23:33:12 +0800 | [diff] [blame] | 1309 | hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id, |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1310 | struct sas_task *task, int abort_flag, |
| 1311 | int task_tag) |
| 1312 | { |
| 1313 | struct domain_device *device = task->dev; |
| 1314 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 1315 | struct device *dev = hisi_hba->dev; |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1316 | struct hisi_sas_port *port; |
| 1317 | struct hisi_sas_slot *slot; |
John Garry | 2e244f0 | 2017-03-23 01:25:17 +0800 | [diff] [blame] | 1318 | struct asd_sas_port *sas_port = device->port; |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1319 | struct hisi_sas_cmd_hdr *cmd_hdr_base; |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 1320 | struct hisi_sas_dq *dq = sas_dev->dq; |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1321 | int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx; |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 1322 | unsigned long flags, flags_dq; |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1323 | |
Xiaofei Tan | 917d3bd | 2017-08-11 00:09:26 +0800 | [diff] [blame^] | 1324 | if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1325 | return -EINVAL; |
| 1326 | |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1327 | if (!device->port) |
| 1328 | return -1; |
| 1329 | |
John Garry | 2e244f0 | 2017-03-23 01:25:17 +0800 | [diff] [blame] | 1330 | port = to_hisi_sas_port(sas_port); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1331 | |
| 1332 | /* simply get a slot and send abort command */ |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 1333 | spin_lock_irqsave(&hisi_hba->lock, flags); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1334 | rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx); |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 1335 | if (rc) { |
| 1336 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1337 | goto err_out; |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 1338 | } |
| 1339 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 1340 | |
| 1341 | spin_lock_irqsave(&dq->lock, flags_dq); |
| 1342 | rc = hisi_hba->hw->get_free_slot(hisi_hba, dq); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1343 | if (rc) |
| 1344 | goto err_out_tag; |
| 1345 | |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 1346 | dlvry_queue = dq->id; |
| 1347 | dlvry_queue_slot = dq->wr_point; |
| 1348 | |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1349 | slot = &hisi_hba->slot_info[slot_idx]; |
| 1350 | memset(slot, 0, sizeof(struct hisi_sas_slot)); |
| 1351 | |
| 1352 | slot->idx = slot_idx; |
| 1353 | slot->n_elem = n_elem; |
| 1354 | slot->dlvry_queue = dlvry_queue; |
| 1355 | slot->dlvry_queue_slot = dlvry_queue_slot; |
| 1356 | cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue]; |
| 1357 | slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot]; |
| 1358 | slot->task = task; |
| 1359 | slot->port = port; |
| 1360 | task->lldd_task = slot; |
| 1361 | |
| 1362 | memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr)); |
| 1363 | |
| 1364 | rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id, |
| 1365 | abort_flag, task_tag); |
| 1366 | if (rc) |
| 1367 | goto err_out_tag; |
| 1368 | |
John Garry | 405314d | 2017-03-23 01:25:21 +0800 | [diff] [blame] | 1369 | |
| 1370 | list_add_tail(&slot->entry, &sas_dev->list); |
John Garry | 54c9dd2 | 2017-03-23 01:25:29 +0800 | [diff] [blame] | 1371 | spin_lock_irqsave(&task->task_state_lock, flags); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1372 | task->task_state_flags |= SAS_TASK_AT_INITIATOR; |
John Garry | 54c9dd2 | 2017-03-23 01:25:29 +0800 | [diff] [blame] | 1373 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1374 | |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 1375 | dq->slot_prep = slot; |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1376 | |
John Garry | f696cc3 | 2016-11-07 20:48:39 +0800 | [diff] [blame] | 1377 | atomic64_inc(&sas_dev->running_req); |
| 1378 | |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 1379 | /* send abort command to the chip */ |
| 1380 | hisi_hba->hw->start_delivery(dq); |
| 1381 | spin_unlock_irqrestore(&dq->lock, flags_dq); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1382 | |
| 1383 | return 0; |
| 1384 | |
| 1385 | err_out_tag: |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 1386 | spin_lock_irqsave(&hisi_hba->lock, flags); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1387 | hisi_sas_slot_index_free(hisi_hba, slot_idx); |
Xiang Chen | b1a4941 | 2017-06-14 23:33:13 +0800 | [diff] [blame] | 1388 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 1389 | spin_unlock_irqrestore(&dq->lock, flags_dq); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1390 | err_out: |
| 1391 | dev_err(dev, "internal abort task prep: failed[%d]!\n", rc); |
| 1392 | |
| 1393 | return rc; |
| 1394 | } |
| 1395 | |
| 1396 | /** |
| 1397 | * hisi_sas_internal_task_abort -- execute an internal |
| 1398 | * abort command for single IO command or a device |
| 1399 | * @hisi_hba: host controller struct |
| 1400 | * @device: domain device |
| 1401 | * @abort_flag: mode of operation, device or single IO |
| 1402 | * @tag: tag of IO to be aborted (only relevant to single |
| 1403 | * IO mode) |
| 1404 | */ |
| 1405 | static int |
| 1406 | hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba, |
| 1407 | struct domain_device *device, |
| 1408 | int abort_flag, int tag) |
| 1409 | { |
| 1410 | struct sas_task *task; |
| 1411 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 1412 | struct device *dev = hisi_hba->dev; |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1413 | int res; |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1414 | |
| 1415 | if (!hisi_hba->hw->prep_abort) |
| 1416 | return -EOPNOTSUPP; |
| 1417 | |
| 1418 | task = sas_alloc_slow_task(GFP_KERNEL); |
| 1419 | if (!task) |
| 1420 | return -ENOMEM; |
| 1421 | |
| 1422 | task->dev = device; |
| 1423 | task->task_proto = device->tproto; |
| 1424 | task->task_done = hisi_sas_task_done; |
| 1425 | task->slow_task->timer.data = (unsigned long)task; |
| 1426 | task->slow_task->timer.function = hisi_sas_tmf_timedout; |
John Garry | 0844a3f | 2017-04-10 21:21:59 +0800 | [diff] [blame] | 1427 | task->slow_task->timer.expires = jiffies + msecs_to_jiffies(110); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1428 | add_timer(&task->slow_task->timer); |
| 1429 | |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1430 | res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id, |
| 1431 | task, abort_flag, tag); |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1432 | if (res) { |
| 1433 | del_timer(&task->slow_task->timer); |
| 1434 | dev_err(dev, "internal task abort: executing internal task failed: %d\n", |
| 1435 | res); |
| 1436 | goto exit; |
| 1437 | } |
| 1438 | wait_for_completion(&task->slow_task->completion); |
| 1439 | res = TMF_RESP_FUNC_FAILED; |
| 1440 | |
Xiang Chen | f64a698 | 2017-06-14 23:33:11 +0800 | [diff] [blame] | 1441 | /* Internal abort timed out */ |
| 1442 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { |
| 1443 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { |
| 1444 | struct hisi_sas_slot *slot = task->lldd_task; |
| 1445 | |
| 1446 | if (slot) |
| 1447 | slot->task = NULL; |
| 1448 | dev_err(dev, "internal task abort: timeout.\n"); |
| 1449 | } |
| 1450 | } |
| 1451 | |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1452 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
| 1453 | task->task_status.stat == TMF_RESP_FUNC_COMPLETE) { |
| 1454 | res = TMF_RESP_FUNC_COMPLETE; |
| 1455 | goto exit; |
| 1456 | } |
| 1457 | |
John Garry | c35279f | 2017-03-23 01:25:25 +0800 | [diff] [blame] | 1458 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
| 1459 | task->task_status.stat == TMF_RESP_FUNC_SUCC) { |
| 1460 | res = TMF_RESP_FUNC_SUCC; |
| 1461 | goto exit; |
| 1462 | } |
| 1463 | |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1464 | exit: |
John Garry | 297d730 | 2017-01-20 20:45:22 +0800 | [diff] [blame] | 1465 | dev_dbg(dev, "internal task abort: task to dev %016llx task=%p " |
John Garry | 441c274 | 2016-08-24 19:05:47 +0800 | [diff] [blame] | 1466 | "resp: 0x%x sts 0x%x\n", |
| 1467 | SAS_ADDR(device->sas_addr), |
| 1468 | task, |
| 1469 | task->task_status.resp, /* 0 is complete, -1 is undelivered */ |
| 1470 | task->task_status.stat); |
| 1471 | sas_free_task(task); |
| 1472 | |
| 1473 | return res; |
| 1474 | } |
| 1475 | |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 1476 | static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy) |
| 1477 | { |
| 1478 | hisi_sas_port_notify_formed(sas_phy); |
| 1479 | } |
| 1480 | |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 1481 | static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy) |
| 1482 | { |
| 1483 | phy->phy_attached = 0; |
| 1484 | phy->phy_type = 0; |
| 1485 | phy->port = NULL; |
| 1486 | } |
| 1487 | |
| 1488 | void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy) |
| 1489 | { |
| 1490 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; |
| 1491 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 1492 | struct sas_ha_struct *sas_ha = &hisi_hba->sha; |
| 1493 | |
| 1494 | if (rdy) { |
| 1495 | /* Phy down but ready */ |
| 1496 | hisi_sas_bytes_dmaed(hisi_hba, phy_no); |
| 1497 | hisi_sas_port_notify_formed(sas_phy); |
| 1498 | } else { |
| 1499 | struct hisi_sas_port *port = phy->port; |
| 1500 | |
| 1501 | /* Phy down and not ready */ |
| 1502 | sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL); |
| 1503 | sas_phy_disconnected(sas_phy); |
| 1504 | |
| 1505 | if (port) { |
| 1506 | if (phy->phy_type & PORT_TYPE_SAS) { |
| 1507 | int port_id = port->id; |
| 1508 | |
| 1509 | if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba, |
| 1510 | port_id)) |
| 1511 | port->port_attached = 0; |
| 1512 | } else if (phy->phy_type & PORT_TYPE_SATA) |
| 1513 | port->port_attached = 0; |
| 1514 | } |
| 1515 | hisi_sas_phy_disconnected(phy); |
| 1516 | } |
| 1517 | } |
| 1518 | EXPORT_SYMBOL_GPL(hisi_sas_phy_down); |
| 1519 | |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1520 | |
John Garry | e21fe3a | 2017-06-14 23:33:20 +0800 | [diff] [blame] | 1521 | struct scsi_transport_template *hisi_sas_stt; |
| 1522 | EXPORT_SYMBOL_GPL(hisi_sas_stt); |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 1523 | |
John Garry | e21fe3a | 2017-06-14 23:33:20 +0800 | [diff] [blame] | 1524 | static struct scsi_host_template _hisi_sas_sht = { |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1525 | .module = THIS_MODULE, |
| 1526 | .name = DRV_NAME, |
| 1527 | .queuecommand = sas_queuecommand, |
| 1528 | .target_alloc = sas_target_alloc, |
John Garry | 31eec8a | 2016-02-25 17:42:14 +0800 | [diff] [blame] | 1529 | .slave_configure = hisi_sas_slave_configure, |
John Garry | 701f75e | 2015-11-18 00:50:55 +0800 | [diff] [blame] | 1530 | .scan_finished = hisi_sas_scan_finished, |
| 1531 | .scan_start = hisi_sas_scan_start, |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1532 | .change_queue_depth = sas_change_queue_depth, |
| 1533 | .bios_param = sas_bios_param, |
| 1534 | .can_queue = 1, |
| 1535 | .this_id = -1, |
| 1536 | .sg_tablesize = SG_ALL, |
| 1537 | .max_sectors = SCSI_DEFAULT_MAX_SECTORS, |
| 1538 | .use_clustering = ENABLE_CLUSTERING, |
| 1539 | .eh_device_reset_handler = sas_eh_device_reset_handler, |
| 1540 | .eh_bus_reset_handler = sas_eh_bus_reset_handler, |
| 1541 | .target_destroy = sas_target_destroy, |
| 1542 | .ioctl = sas_ioctl, |
| 1543 | }; |
John Garry | e21fe3a | 2017-06-14 23:33:20 +0800 | [diff] [blame] | 1544 | struct scsi_host_template *hisi_sas_sht = &_hisi_sas_sht; |
| 1545 | EXPORT_SYMBOL_GPL(hisi_sas_sht); |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1546 | |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 1547 | static struct sas_domain_function_template hisi_sas_transport_ops = { |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 1548 | .lldd_dev_found = hisi_sas_dev_found, |
| 1549 | .lldd_dev_gone = hisi_sas_dev_gone, |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 1550 | .lldd_execute_task = hisi_sas_queue_command, |
John Garry | e4189d5 | 2015-11-18 00:50:57 +0800 | [diff] [blame] | 1551 | .lldd_control_phy = hisi_sas_control_phy, |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 1552 | .lldd_abort_task = hisi_sas_abort_task, |
| 1553 | .lldd_abort_task_set = hisi_sas_abort_task_set, |
| 1554 | .lldd_clear_aca = hisi_sas_clear_aca, |
| 1555 | .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset, |
| 1556 | .lldd_lu_reset = hisi_sas_lu_reset, |
| 1557 | .lldd_query_task = hisi_sas_query_task, |
John Garry | 8b05ad6 | 2017-03-23 01:25:35 +0800 | [diff] [blame] | 1558 | .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha, |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 1559 | .lldd_port_formed = hisi_sas_port_formed, |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 1560 | }; |
| 1561 | |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1562 | void hisi_sas_init_mem(struct hisi_hba *hisi_hba) |
| 1563 | { |
| 1564 | int i, s, max_command_entries = hisi_hba->hw->max_command_entries; |
| 1565 | |
| 1566 | for (i = 0; i < hisi_hba->queue_count; i++) { |
| 1567 | struct hisi_sas_cq *cq = &hisi_hba->cq[i]; |
| 1568 | struct hisi_sas_dq *dq = &hisi_hba->dq[i]; |
| 1569 | |
| 1570 | s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; |
| 1571 | memset(hisi_hba->cmd_hdr[i], 0, s); |
| 1572 | dq->wr_point = 0; |
| 1573 | |
| 1574 | s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; |
| 1575 | memset(hisi_hba->complete_hdr[i], 0, s); |
| 1576 | cq->rd_point = 0; |
| 1577 | } |
| 1578 | |
| 1579 | s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy; |
| 1580 | memset(hisi_hba->initial_fis, 0, s); |
| 1581 | |
| 1582 | s = max_command_entries * sizeof(struct hisi_sas_iost); |
| 1583 | memset(hisi_hba->iost, 0, s); |
| 1584 | |
| 1585 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint); |
| 1586 | memset(hisi_hba->breakpoint, 0, s); |
| 1587 | |
| 1588 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2; |
| 1589 | memset(hisi_hba->sata_breakpoint, 0, s); |
| 1590 | } |
| 1591 | EXPORT_SYMBOL_GPL(hisi_sas_init_mem); |
| 1592 | |
John Garry | e21fe3a | 2017-06-14 23:33:20 +0800 | [diff] [blame] | 1593 | int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost) |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1594 | { |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 1595 | struct device *dev = hisi_hba->dev; |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 1596 | int i, s, max_command_entries = hisi_hba->hw->max_command_entries; |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1597 | |
John Garry | fa42d80 | 2015-11-18 00:50:43 +0800 | [diff] [blame] | 1598 | spin_lock_init(&hisi_hba->lock); |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 1599 | for (i = 0; i < hisi_hba->n_phy; i++) { |
| 1600 | hisi_sas_phy_init(hisi_hba, i); |
| 1601 | hisi_hba->port[i].port_attached = 0; |
| 1602 | hisi_hba->port[i].id = -1; |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 1603 | } |
| 1604 | |
John Garry | af740db | 2015-11-18 00:50:41 +0800 | [diff] [blame] | 1605 | for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { |
| 1606 | hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED; |
| 1607 | hisi_hba->devices[i].device_id = i; |
| 1608 | hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL; |
| 1609 | } |
| 1610 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1611 | for (i = 0; i < hisi_hba->queue_count; i++) { |
John Garry | 9101a07 | 2015-11-18 00:50:37 +0800 | [diff] [blame] | 1612 | struct hisi_sas_cq *cq = &hisi_hba->cq[i]; |
John Garry | 4fde02a | 2016-09-06 23:36:12 +0800 | [diff] [blame] | 1613 | struct hisi_sas_dq *dq = &hisi_hba->dq[i]; |
John Garry | 9101a07 | 2015-11-18 00:50:37 +0800 | [diff] [blame] | 1614 | |
| 1615 | /* Completion queue structure */ |
| 1616 | cq->id = i; |
| 1617 | cq->hisi_hba = hisi_hba; |
| 1618 | |
John Garry | 4fde02a | 2016-09-06 23:36:12 +0800 | [diff] [blame] | 1619 | /* Delivery queue structure */ |
| 1620 | dq->id = i; |
| 1621 | dq->hisi_hba = hisi_hba; |
| 1622 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1623 | /* Delivery queue */ |
| 1624 | s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; |
| 1625 | hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s, |
| 1626 | &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL); |
| 1627 | if (!hisi_hba->cmd_hdr[i]) |
| 1628 | goto err_out; |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1629 | |
| 1630 | /* Completion queue */ |
| 1631 | s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; |
| 1632 | hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s, |
| 1633 | &hisi_hba->complete_hdr_dma[i], GFP_KERNEL); |
| 1634 | if (!hisi_hba->complete_hdr[i]) |
| 1635 | goto err_out; |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1636 | } |
| 1637 | |
Xiaofei Tan | f557e32 | 2017-06-29 21:02:14 +0800 | [diff] [blame] | 1638 | s = sizeof(struct hisi_sas_slot_buf_table); |
| 1639 | hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0); |
| 1640 | if (!hisi_hba->buffer_pool) |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1641 | goto err_out; |
| 1642 | |
| 1643 | s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); |
| 1644 | hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma, |
| 1645 | GFP_KERNEL); |
| 1646 | if (!hisi_hba->itct) |
| 1647 | goto err_out; |
| 1648 | |
| 1649 | memset(hisi_hba->itct, 0, s); |
| 1650 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 1651 | hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries, |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1652 | sizeof(struct hisi_sas_slot), |
| 1653 | GFP_KERNEL); |
| 1654 | if (!hisi_hba->slot_info) |
| 1655 | goto err_out; |
| 1656 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 1657 | s = max_command_entries * sizeof(struct hisi_sas_iost); |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1658 | hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma, |
| 1659 | GFP_KERNEL); |
| 1660 | if (!hisi_hba->iost) |
| 1661 | goto err_out; |
| 1662 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 1663 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint); |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1664 | hisi_hba->breakpoint = dma_alloc_coherent(dev, s, |
| 1665 | &hisi_hba->breakpoint_dma, GFP_KERNEL); |
| 1666 | if (!hisi_hba->breakpoint) |
| 1667 | goto err_out; |
| 1668 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 1669 | hisi_hba->slot_index_count = max_command_entries; |
John Garry | 433f569 | 2016-09-06 23:36:15 +0800 | [diff] [blame] | 1670 | s = hisi_hba->slot_index_count / BITS_PER_BYTE; |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 1671 | hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL); |
| 1672 | if (!hisi_hba->slot_index_tags) |
| 1673 | goto err_out; |
| 1674 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1675 | s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; |
| 1676 | hisi_hba->initial_fis = dma_alloc_coherent(dev, s, |
| 1677 | &hisi_hba->initial_fis_dma, GFP_KERNEL); |
| 1678 | if (!hisi_hba->initial_fis) |
| 1679 | goto err_out; |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1680 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 1681 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2; |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1682 | hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s, |
| 1683 | &hisi_hba->sata_breakpoint_dma, GFP_KERNEL); |
| 1684 | if (!hisi_hba->sata_breakpoint) |
| 1685 | goto err_out; |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1686 | hisi_sas_init_mem(hisi_hba); |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1687 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 1688 | hisi_sas_slot_index_init(hisi_hba); |
| 1689 | |
John Garry | 7e9080e | 2015-11-18 00:50:40 +0800 | [diff] [blame] | 1690 | hisi_hba->wq = create_singlethread_workqueue(dev_name(dev)); |
| 1691 | if (!hisi_hba->wq) { |
| 1692 | dev_err(dev, "sas_alloc: failed to create workqueue\n"); |
| 1693 | goto err_out; |
| 1694 | } |
| 1695 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1696 | return 0; |
| 1697 | err_out: |
| 1698 | return -ENOMEM; |
| 1699 | } |
John Garry | e21fe3a | 2017-06-14 23:33:20 +0800 | [diff] [blame] | 1700 | EXPORT_SYMBOL_GPL(hisi_sas_alloc); |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1701 | |
John Garry | e21fe3a | 2017-06-14 23:33:20 +0800 | [diff] [blame] | 1702 | void hisi_sas_free(struct hisi_hba *hisi_hba) |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1703 | { |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 1704 | struct device *dev = hisi_hba->dev; |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 1705 | int i, s, max_command_entries = hisi_hba->hw->max_command_entries; |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1706 | |
| 1707 | for (i = 0; i < hisi_hba->queue_count; i++) { |
| 1708 | s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; |
| 1709 | if (hisi_hba->cmd_hdr[i]) |
| 1710 | dma_free_coherent(dev, s, |
| 1711 | hisi_hba->cmd_hdr[i], |
| 1712 | hisi_hba->cmd_hdr_dma[i]); |
| 1713 | |
| 1714 | s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; |
| 1715 | if (hisi_hba->complete_hdr[i]) |
| 1716 | dma_free_coherent(dev, s, |
| 1717 | hisi_hba->complete_hdr[i], |
| 1718 | hisi_hba->complete_hdr_dma[i]); |
| 1719 | } |
| 1720 | |
Xiaofei Tan | f557e32 | 2017-06-29 21:02:14 +0800 | [diff] [blame] | 1721 | dma_pool_destroy(hisi_hba->buffer_pool); |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1722 | |
| 1723 | s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); |
| 1724 | if (hisi_hba->itct) |
| 1725 | dma_free_coherent(dev, s, |
| 1726 | hisi_hba->itct, hisi_hba->itct_dma); |
| 1727 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 1728 | s = max_command_entries * sizeof(struct hisi_sas_iost); |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1729 | if (hisi_hba->iost) |
| 1730 | dma_free_coherent(dev, s, |
| 1731 | hisi_hba->iost, hisi_hba->iost_dma); |
| 1732 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 1733 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint); |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1734 | if (hisi_hba->breakpoint) |
| 1735 | dma_free_coherent(dev, s, |
| 1736 | hisi_hba->breakpoint, |
| 1737 | hisi_hba->breakpoint_dma); |
| 1738 | |
| 1739 | |
| 1740 | s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; |
| 1741 | if (hisi_hba->initial_fis) |
| 1742 | dma_free_coherent(dev, s, |
| 1743 | hisi_hba->initial_fis, |
| 1744 | hisi_hba->initial_fis_dma); |
| 1745 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 1746 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2; |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1747 | if (hisi_hba->sata_breakpoint) |
| 1748 | dma_free_coherent(dev, s, |
| 1749 | hisi_hba->sata_breakpoint, |
| 1750 | hisi_hba->sata_breakpoint_dma); |
| 1751 | |
John Garry | 7e9080e | 2015-11-18 00:50:40 +0800 | [diff] [blame] | 1752 | if (hisi_hba->wq) |
| 1753 | destroy_workqueue(hisi_hba->wq); |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1754 | } |
John Garry | e21fe3a | 2017-06-14 23:33:20 +0800 | [diff] [blame] | 1755 | EXPORT_SYMBOL_GPL(hisi_sas_free); |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1756 | |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1757 | static void hisi_sas_rst_work_handler(struct work_struct *work) |
| 1758 | { |
| 1759 | struct hisi_hba *hisi_hba = |
| 1760 | container_of(work, struct hisi_hba, rst_work); |
| 1761 | |
| 1762 | hisi_sas_controller_reset(hisi_hba); |
| 1763 | } |
| 1764 | |
John Garry | 0fa24c1 | 2017-06-14 23:33:18 +0800 | [diff] [blame] | 1765 | int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba) |
| 1766 | { |
| 1767 | struct device *dev = hisi_hba->dev; |
| 1768 | struct platform_device *pdev = hisi_hba->platform_dev; |
| 1769 | struct device_node *np = pdev ? pdev->dev.of_node : NULL; |
| 1770 | struct clk *refclk; |
| 1771 | |
| 1772 | if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr, |
| 1773 | SAS_ADDR_SIZE)) { |
| 1774 | dev_err(dev, "could not get property sas-addr\n"); |
| 1775 | return -ENOENT; |
| 1776 | } |
| 1777 | |
| 1778 | if (np) { |
| 1779 | /* |
| 1780 | * These properties are only required for platform device-based |
| 1781 | * controller with DT firmware. |
| 1782 | */ |
| 1783 | hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np, |
| 1784 | "hisilicon,sas-syscon"); |
| 1785 | if (IS_ERR(hisi_hba->ctrl)) { |
| 1786 | dev_err(dev, "could not get syscon\n"); |
| 1787 | return -ENOENT; |
| 1788 | } |
| 1789 | |
| 1790 | if (device_property_read_u32(dev, "ctrl-reset-reg", |
| 1791 | &hisi_hba->ctrl_reset_reg)) { |
| 1792 | dev_err(dev, |
| 1793 | "could not get property ctrl-reset-reg\n"); |
| 1794 | return -ENOENT; |
| 1795 | } |
| 1796 | |
| 1797 | if (device_property_read_u32(dev, "ctrl-reset-sts-reg", |
| 1798 | &hisi_hba->ctrl_reset_sts_reg)) { |
| 1799 | dev_err(dev, |
| 1800 | "could not get property ctrl-reset-sts-reg\n"); |
| 1801 | return -ENOENT; |
| 1802 | } |
| 1803 | |
| 1804 | if (device_property_read_u32(dev, "ctrl-clock-ena-reg", |
| 1805 | &hisi_hba->ctrl_clock_ena_reg)) { |
| 1806 | dev_err(dev, |
| 1807 | "could not get property ctrl-clock-ena-reg\n"); |
| 1808 | return -ENOENT; |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | refclk = devm_clk_get(dev, NULL); |
| 1813 | if (IS_ERR(refclk)) |
| 1814 | dev_dbg(dev, "no ref clk property\n"); |
| 1815 | else |
| 1816 | hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000; |
| 1817 | |
| 1818 | if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) { |
| 1819 | dev_err(dev, "could not get property phy-count\n"); |
| 1820 | return -ENOENT; |
| 1821 | } |
| 1822 | |
| 1823 | if (device_property_read_u32(dev, "queue-count", |
| 1824 | &hisi_hba->queue_count)) { |
| 1825 | dev_err(dev, "could not get property queue-count\n"); |
| 1826 | return -ENOENT; |
| 1827 | } |
| 1828 | |
| 1829 | return 0; |
| 1830 | } |
| 1831 | EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info); |
| 1832 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1833 | static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev, |
| 1834 | const struct hisi_sas_hw *hw) |
| 1835 | { |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 1836 | struct resource *res; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1837 | struct Scsi_Host *shost; |
| 1838 | struct hisi_hba *hisi_hba; |
| 1839 | struct device *dev = &pdev->dev; |
| 1840 | |
John Garry | e21fe3a | 2017-06-14 23:33:20 +0800 | [diff] [blame] | 1841 | shost = scsi_host_alloc(hisi_sas_sht, sizeof(*hisi_hba)); |
Xiaofei Tan | d37a008 | 2016-11-29 23:45:57 +0800 | [diff] [blame] | 1842 | if (!shost) { |
| 1843 | dev_err(dev, "scsi host alloc failed\n"); |
| 1844 | return NULL; |
| 1845 | } |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1846 | hisi_hba = shost_priv(shost); |
| 1847 | |
Xiang Chen | 06ec0fb | 2017-03-23 01:25:18 +0800 | [diff] [blame] | 1848 | INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler); |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1849 | hisi_hba->hw = hw; |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 1850 | hisi_hba->dev = dev; |
John Garry | 0fa24c1 | 2017-06-14 23:33:18 +0800 | [diff] [blame] | 1851 | hisi_hba->platform_dev = pdev; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1852 | hisi_hba->shost = shost; |
| 1853 | SHOST_TO_SAS_HA(shost) = &hisi_hba->sha; |
| 1854 | |
John Garry | fa42d80 | 2015-11-18 00:50:43 +0800 | [diff] [blame] | 1855 | init_timer(&hisi_hba->timer); |
| 1856 | |
John Garry | 0fa24c1 | 2017-06-14 23:33:18 +0800 | [diff] [blame] | 1857 | if (hisi_sas_get_fw_info(hisi_hba) < 0) |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 1858 | goto err_out; |
| 1859 | |
John Garry | a6f2c7f | 2016-09-06 23:36:19 +0800 | [diff] [blame] | 1860 | if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) && |
| 1861 | dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) { |
| 1862 | dev_err(dev, "No usable DMA addressing method\n"); |
| 1863 | goto err_out; |
| 1864 | } |
| 1865 | |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 1866 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1867 | hisi_hba->regs = devm_ioremap_resource(dev, res); |
| 1868 | if (IS_ERR(hisi_hba->regs)) |
| 1869 | goto err_out; |
| 1870 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1871 | if (hisi_sas_alloc(hisi_hba, shost)) { |
| 1872 | hisi_sas_free(hisi_hba); |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1873 | goto err_out; |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1874 | } |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1875 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1876 | return shost; |
| 1877 | err_out: |
Xiaofei Tan | d37a008 | 2016-11-29 23:45:57 +0800 | [diff] [blame] | 1878 | kfree(shost); |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1879 | dev_err(dev, "shost alloc failed\n"); |
| 1880 | return NULL; |
| 1881 | } |
| 1882 | |
John Garry | e21fe3a | 2017-06-14 23:33:20 +0800 | [diff] [blame] | 1883 | void hisi_sas_init_add(struct hisi_hba *hisi_hba) |
John Garry | 5d74242 | 2015-11-18 00:50:38 +0800 | [diff] [blame] | 1884 | { |
| 1885 | int i; |
| 1886 | |
| 1887 | for (i = 0; i < hisi_hba->n_phy; i++) |
| 1888 | memcpy(&hisi_hba->phy[i].dev_sas_addr, |
| 1889 | hisi_hba->sas_addr, |
| 1890 | SAS_ADDR_SIZE); |
| 1891 | } |
John Garry | e21fe3a | 2017-06-14 23:33:20 +0800 | [diff] [blame] | 1892 | EXPORT_SYMBOL_GPL(hisi_sas_init_add); |
John Garry | 5d74242 | 2015-11-18 00:50:38 +0800 | [diff] [blame] | 1893 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1894 | int hisi_sas_probe(struct platform_device *pdev, |
| 1895 | const struct hisi_sas_hw *hw) |
| 1896 | { |
| 1897 | struct Scsi_Host *shost; |
| 1898 | struct hisi_hba *hisi_hba; |
| 1899 | struct device *dev = &pdev->dev; |
| 1900 | struct asd_sas_phy **arr_phy; |
| 1901 | struct asd_sas_port **arr_port; |
| 1902 | struct sas_ha_struct *sha; |
| 1903 | int rc, phy_nr, port_nr, i; |
| 1904 | |
| 1905 | shost = hisi_sas_shost_alloc(pdev, hw); |
Xiaofei Tan | d37a008 | 2016-11-29 23:45:57 +0800 | [diff] [blame] | 1906 | if (!shost) |
| 1907 | return -ENOMEM; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1908 | |
| 1909 | sha = SHOST_TO_SAS_HA(shost); |
| 1910 | hisi_hba = shost_priv(shost); |
| 1911 | platform_set_drvdata(pdev, sha); |
John Garry | 50cb916 | 2015-11-18 00:50:39 +0800 | [diff] [blame] | 1912 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1913 | phy_nr = port_nr = hisi_hba->n_phy; |
| 1914 | |
| 1915 | arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL); |
| 1916 | arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL); |
Xiaofei Tan | d37a008 | 2016-11-29 23:45:57 +0800 | [diff] [blame] | 1917 | if (!arr_phy || !arr_port) { |
| 1918 | rc = -ENOMEM; |
| 1919 | goto err_out_ha; |
| 1920 | } |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1921 | |
| 1922 | sha->sas_phy = arr_phy; |
| 1923 | sha->sas_port = arr_port; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1924 | sha->lldd_ha = hisi_hba; |
| 1925 | |
| 1926 | shost->transportt = hisi_sas_stt; |
| 1927 | shost->max_id = HISI_SAS_MAX_DEVICES; |
| 1928 | shost->max_lun = ~0; |
| 1929 | shost->max_channel = 1; |
| 1930 | shost->max_cmd_len = 16; |
| 1931 | shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT); |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame] | 1932 | shost->can_queue = hisi_hba->hw->max_command_entries; |
| 1933 | shost->cmd_per_lun = hisi_hba->hw->max_command_entries; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1934 | |
| 1935 | sha->sas_ha_name = DRV_NAME; |
John Garry | 11b7524 | 2017-06-14 23:33:17 +0800 | [diff] [blame] | 1936 | sha->dev = hisi_hba->dev; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1937 | sha->lldd_module = THIS_MODULE; |
| 1938 | sha->sas_addr = &hisi_hba->sas_addr[0]; |
| 1939 | sha->num_phys = hisi_hba->n_phy; |
| 1940 | sha->core.shost = hisi_hba->shost; |
| 1941 | |
| 1942 | for (i = 0; i < hisi_hba->n_phy; i++) { |
| 1943 | sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy; |
| 1944 | sha->sas_port[i] = &hisi_hba->port[i].sas_port; |
| 1945 | } |
| 1946 | |
John Garry | 5d74242 | 2015-11-18 00:50:38 +0800 | [diff] [blame] | 1947 | hisi_sas_init_add(hisi_hba); |
| 1948 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1949 | rc = scsi_add_host(shost, &pdev->dev); |
| 1950 | if (rc) |
| 1951 | goto err_out_ha; |
| 1952 | |
| 1953 | rc = sas_register_ha(sha); |
| 1954 | if (rc) |
| 1955 | goto err_out_register_ha; |
| 1956 | |
Xiang Chen | 0757f04 | 2017-01-20 20:45:23 +0800 | [diff] [blame] | 1957 | rc = hisi_hba->hw->hw_init(hisi_hba); |
| 1958 | if (rc) |
| 1959 | goto err_out_register_ha; |
| 1960 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1961 | scsi_scan_host(shost); |
| 1962 | |
| 1963 | return 0; |
| 1964 | |
| 1965 | err_out_register_ha: |
| 1966 | scsi_remove_host(shost); |
| 1967 | err_out_ha: |
Xiaofei Tan | d37a008 | 2016-11-29 23:45:57 +0800 | [diff] [blame] | 1968 | hisi_sas_free(hisi_hba); |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1969 | kfree(shost); |
| 1970 | return rc; |
| 1971 | } |
| 1972 | EXPORT_SYMBOL_GPL(hisi_sas_probe); |
| 1973 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1974 | int hisi_sas_remove(struct platform_device *pdev) |
| 1975 | { |
| 1976 | struct sas_ha_struct *sha = platform_get_drvdata(pdev); |
| 1977 | struct hisi_hba *hisi_hba = sha->lldd_ha; |
Xiaofei Tan | d37a008 | 2016-11-29 23:45:57 +0800 | [diff] [blame] | 1978 | struct Scsi_Host *shost = sha->core.shost; |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1979 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1980 | sas_unregister_ha(sha); |
| 1981 | sas_remove_host(sha->core.shost); |
| 1982 | |
| 1983 | hisi_sas_free(hisi_hba); |
Xiaofei Tan | d37a008 | 2016-11-29 23:45:57 +0800 | [diff] [blame] | 1984 | kfree(shost); |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1985 | return 0; |
| 1986 | } |
| 1987 | EXPORT_SYMBOL_GPL(hisi_sas_remove); |
| 1988 | |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 1989 | static __init int hisi_sas_init(void) |
| 1990 | { |
| 1991 | pr_info("hisi_sas: driver version %s\n", DRV_VERSION); |
| 1992 | |
| 1993 | hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops); |
| 1994 | if (!hisi_sas_stt) |
| 1995 | return -ENOMEM; |
| 1996 | |
| 1997 | return 0; |
| 1998 | } |
| 1999 | |
| 2000 | static __exit void hisi_sas_exit(void) |
| 2001 | { |
| 2002 | sas_release_transport(hisi_sas_stt); |
| 2003 | } |
| 2004 | |
| 2005 | module_init(hisi_sas_init); |
| 2006 | module_exit(hisi_sas_exit); |
| 2007 | |
| 2008 | MODULE_VERSION(DRV_VERSION); |
| 2009 | MODULE_LICENSE("GPL"); |
| 2010 | MODULE_AUTHOR("John Garry <john.garry@huawei.com>"); |
| 2011 | MODULE_DESCRIPTION("HISILICON SAS controller driver"); |
| 2012 | MODULE_ALIAS("platform:" DRV_NAME); |