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