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 | |
| 18 | static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device) |
| 19 | { |
| 20 | return device->port->ha->lldd_ha; |
| 21 | } |
| 22 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 23 | static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx) |
| 24 | { |
| 25 | void *bitmap = hisi_hba->slot_index_tags; |
| 26 | |
| 27 | clear_bit(slot_idx, bitmap); |
| 28 | } |
| 29 | |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 30 | static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx) |
| 31 | { |
| 32 | hisi_sas_slot_index_clear(hisi_hba, slot_idx); |
| 33 | } |
| 34 | |
| 35 | static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx) |
| 36 | { |
| 37 | void *bitmap = hisi_hba->slot_index_tags; |
| 38 | |
| 39 | set_bit(slot_idx, bitmap); |
| 40 | } |
| 41 | |
| 42 | static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx) |
| 43 | { |
| 44 | unsigned int index; |
| 45 | void *bitmap = hisi_hba->slot_index_tags; |
| 46 | |
| 47 | index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count); |
| 48 | if (index >= hisi_hba->slot_index_count) |
| 49 | return -SAS_QUEUE_FULL; |
| 50 | hisi_sas_slot_index_set(hisi_hba, index); |
| 51 | *slot_idx = index; |
| 52 | return 0; |
| 53 | } |
| 54 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 55 | static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba) |
| 56 | { |
| 57 | int i; |
| 58 | |
| 59 | for (i = 0; i < hisi_hba->slot_index_count; ++i) |
| 60 | hisi_sas_slot_index_clear(hisi_hba, i); |
| 61 | } |
John Garry | 27a3f22 | 2015-11-18 00:50:50 +0800 | [diff] [blame] | 62 | |
| 63 | void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task, |
| 64 | struct hisi_sas_slot *slot) |
| 65 | { |
| 66 | struct device *dev = &hisi_hba->pdev->dev; |
| 67 | |
| 68 | if (!slot->task) |
| 69 | return; |
| 70 | |
| 71 | if (!sas_protocol_ata(task->task_proto)) |
| 72 | if (slot->n_elem) |
| 73 | dma_unmap_sg(dev, task->scatter, slot->n_elem, |
| 74 | task->data_dir); |
| 75 | |
| 76 | if (slot->command_table) |
| 77 | dma_pool_free(hisi_hba->command_table_pool, |
| 78 | slot->command_table, slot->command_table_dma); |
| 79 | |
| 80 | if (slot->status_buffer) |
| 81 | dma_pool_free(hisi_hba->status_buffer_pool, |
| 82 | slot->status_buffer, slot->status_buffer_dma); |
| 83 | |
| 84 | if (slot->sge_page) |
| 85 | dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page, |
| 86 | slot->sge_page_dma); |
| 87 | |
| 88 | list_del_init(&slot->entry); |
| 89 | task->lldd_task = NULL; |
| 90 | slot->task = NULL; |
| 91 | slot->port = NULL; |
| 92 | hisi_sas_slot_index_free(hisi_hba, slot->idx); |
| 93 | memset(slot, 0, sizeof(*slot)); |
| 94 | } |
| 95 | EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free); |
| 96 | |
John Garry | 66ee999 | 2015-11-18 00:50:54 +0800 | [diff] [blame] | 97 | static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba, |
| 98 | struct hisi_sas_slot *slot) |
| 99 | { |
| 100 | return hisi_hba->hw->prep_smp(hisi_hba, slot); |
| 101 | } |
| 102 | |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 103 | static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba, |
| 104 | struct hisi_sas_slot *slot, int is_tmf, |
| 105 | struct hisi_sas_tmf_task *tmf) |
| 106 | { |
| 107 | return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf); |
| 108 | } |
| 109 | |
| 110 | static int hisi_sas_task_prep(struct sas_task *task, struct hisi_hba *hisi_hba, |
| 111 | int is_tmf, struct hisi_sas_tmf_task *tmf, |
| 112 | int *pass) |
| 113 | { |
| 114 | struct domain_device *device = task->dev; |
| 115 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 116 | struct hisi_sas_port *port; |
| 117 | struct hisi_sas_slot *slot; |
| 118 | struct hisi_sas_cmd_hdr *cmd_hdr_base; |
| 119 | struct device *dev = &hisi_hba->pdev->dev; |
| 120 | int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx; |
| 121 | |
| 122 | if (!device->port) { |
| 123 | struct task_status_struct *ts = &task->task_status; |
| 124 | |
| 125 | ts->resp = SAS_TASK_UNDELIVERED; |
| 126 | ts->stat = SAS_PHY_DOWN; |
| 127 | /* |
| 128 | * libsas will use dev->port, should |
| 129 | * not call task_done for sata |
| 130 | */ |
| 131 | if (device->dev_type != SAS_SATA_DEV) |
| 132 | task->task_done(task); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | if (DEV_IS_GONE(sas_dev)) { |
| 137 | if (sas_dev) |
| 138 | dev_info(dev, "task prep: device %llu not ready\n", |
| 139 | sas_dev->device_id); |
| 140 | else |
| 141 | dev_info(dev, "task prep: device %016llx not ready\n", |
| 142 | SAS_ADDR(device->sas_addr)); |
| 143 | |
| 144 | rc = SAS_PHY_DOWN; |
| 145 | return rc; |
| 146 | } |
| 147 | port = device->port->lldd_port; |
| 148 | if (port && !port->port_attached && !tmf) { |
| 149 | if (sas_protocol_ata(task->task_proto)) { |
| 150 | struct task_status_struct *ts = &task->task_status; |
| 151 | |
| 152 | dev_info(dev, |
| 153 | "task prep: SATA/STP port%d not attach device\n", |
| 154 | device->port->id); |
| 155 | ts->resp = SAS_TASK_COMPLETE; |
| 156 | ts->stat = SAS_PHY_DOWN; |
| 157 | task->task_done(task); |
| 158 | } else { |
| 159 | struct task_status_struct *ts = &task->task_status; |
| 160 | |
| 161 | dev_info(dev, |
| 162 | "task prep: SAS port%d does not attach device\n", |
| 163 | device->port->id); |
| 164 | ts->resp = SAS_TASK_UNDELIVERED; |
| 165 | ts->stat = SAS_PHY_DOWN; |
| 166 | task->task_done(task); |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | if (!sas_protocol_ata(task->task_proto)) { |
| 172 | if (task->num_scatter) { |
| 173 | n_elem = dma_map_sg(dev, task->scatter, |
| 174 | task->num_scatter, task->data_dir); |
| 175 | if (!n_elem) { |
| 176 | rc = -ENOMEM; |
| 177 | goto prep_out; |
| 178 | } |
| 179 | } |
| 180 | } else |
| 181 | n_elem = task->num_scatter; |
| 182 | |
| 183 | rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx); |
| 184 | if (rc) |
| 185 | goto err_out; |
| 186 | rc = hisi_hba->hw->get_free_slot(hisi_hba, &dlvry_queue, |
| 187 | &dlvry_queue_slot); |
| 188 | if (rc) |
| 189 | goto err_out_tag; |
| 190 | |
| 191 | slot = &hisi_hba->slot_info[slot_idx]; |
| 192 | memset(slot, 0, sizeof(struct hisi_sas_slot)); |
| 193 | |
| 194 | slot->idx = slot_idx; |
| 195 | slot->n_elem = n_elem; |
| 196 | slot->dlvry_queue = dlvry_queue; |
| 197 | slot->dlvry_queue_slot = dlvry_queue_slot; |
| 198 | cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue]; |
| 199 | slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot]; |
| 200 | slot->task = task; |
| 201 | slot->port = port; |
| 202 | task->lldd_task = slot; |
| 203 | |
| 204 | slot->status_buffer = dma_pool_alloc(hisi_hba->status_buffer_pool, |
| 205 | GFP_ATOMIC, |
| 206 | &slot->status_buffer_dma); |
Dan Carpenter | 9c9d18e | 2015-12-09 13:48:36 +0300 | [diff] [blame] | 207 | if (!slot->status_buffer) { |
| 208 | rc = -ENOMEM; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 209 | goto err_out_slot_buf; |
Dan Carpenter | 9c9d18e | 2015-12-09 13:48:36 +0300 | [diff] [blame] | 210 | } |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 211 | memset(slot->status_buffer, 0, HISI_SAS_STATUS_BUF_SZ); |
| 212 | |
| 213 | slot->command_table = dma_pool_alloc(hisi_hba->command_table_pool, |
| 214 | GFP_ATOMIC, |
| 215 | &slot->command_table_dma); |
Dan Carpenter | 9c9d18e | 2015-12-09 13:48:36 +0300 | [diff] [blame] | 216 | if (!slot->command_table) { |
| 217 | rc = -ENOMEM; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 218 | goto err_out_status_buf; |
Dan Carpenter | 9c9d18e | 2015-12-09 13:48:36 +0300 | [diff] [blame] | 219 | } |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 220 | memset(slot->command_table, 0, HISI_SAS_COMMAND_TABLE_SZ); |
| 221 | memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr)); |
| 222 | |
| 223 | switch (task->task_proto) { |
John Garry | 66ee999 | 2015-11-18 00:50:54 +0800 | [diff] [blame] | 224 | case SAS_PROTOCOL_SMP: |
| 225 | rc = hisi_sas_task_prep_smp(hisi_hba, slot); |
| 226 | break; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 227 | case SAS_PROTOCOL_SSP: |
| 228 | rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf); |
| 229 | break; |
| 230 | case SAS_PROTOCOL_SATA: |
| 231 | case SAS_PROTOCOL_STP: |
| 232 | case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: |
| 233 | default: |
| 234 | dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n", |
| 235 | task->task_proto); |
| 236 | rc = -EINVAL; |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | if (rc) { |
| 241 | dev_err(dev, "task prep: rc = 0x%x\n", rc); |
| 242 | if (slot->sge_page) |
| 243 | goto err_out_sge; |
| 244 | goto err_out_command_table; |
| 245 | } |
| 246 | |
| 247 | list_add_tail(&slot->entry, &port->list); |
| 248 | spin_lock(&task->task_state_lock); |
| 249 | task->task_state_flags |= SAS_TASK_AT_INITIATOR; |
| 250 | spin_unlock(&task->task_state_lock); |
| 251 | |
| 252 | hisi_hba->slot_prep = slot; |
| 253 | |
| 254 | sas_dev->running_req++; |
| 255 | ++(*pass); |
| 256 | |
Dan Carpenter | 9c9d18e | 2015-12-09 13:48:36 +0300 | [diff] [blame] | 257 | return 0; |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 258 | |
| 259 | err_out_sge: |
| 260 | dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page, |
| 261 | slot->sge_page_dma); |
| 262 | err_out_command_table: |
| 263 | dma_pool_free(hisi_hba->command_table_pool, slot->command_table, |
| 264 | slot->command_table_dma); |
| 265 | err_out_status_buf: |
| 266 | dma_pool_free(hisi_hba->status_buffer_pool, slot->status_buffer, |
| 267 | slot->status_buffer_dma); |
| 268 | err_out_slot_buf: |
| 269 | /* Nothing to be done */ |
| 270 | err_out_tag: |
| 271 | hisi_sas_slot_index_free(hisi_hba, slot_idx); |
| 272 | err_out: |
| 273 | dev_err(dev, "task prep: failed[%d]!\n", rc); |
| 274 | if (!sas_protocol_ata(task->task_proto)) |
| 275 | if (n_elem) |
| 276 | dma_unmap_sg(dev, task->scatter, n_elem, |
| 277 | task->data_dir); |
| 278 | prep_out: |
| 279 | return rc; |
| 280 | } |
| 281 | |
| 282 | static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags, |
| 283 | int is_tmf, struct hisi_sas_tmf_task *tmf) |
| 284 | { |
| 285 | u32 rc; |
| 286 | u32 pass = 0; |
| 287 | unsigned long flags; |
| 288 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev); |
| 289 | struct device *dev = &hisi_hba->pdev->dev; |
| 290 | |
| 291 | /* protect task_prep and start_delivery sequence */ |
| 292 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 293 | rc = hisi_sas_task_prep(task, hisi_hba, is_tmf, tmf, &pass); |
| 294 | if (rc) |
| 295 | dev_err(dev, "task exec: failed[%d]!\n", rc); |
| 296 | |
| 297 | if (likely(pass)) |
| 298 | hisi_hba->hw->start_delivery(hisi_hba); |
| 299 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 300 | |
| 301 | return rc; |
| 302 | } |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 303 | |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 304 | static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no) |
| 305 | { |
| 306 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; |
| 307 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 308 | struct sas_ha_struct *sas_ha; |
| 309 | |
| 310 | if (!phy->phy_attached) |
| 311 | return; |
| 312 | |
| 313 | sas_ha = &hisi_hba->sha; |
| 314 | sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE); |
| 315 | |
| 316 | if (sas_phy->phy) { |
| 317 | struct sas_phy *sphy = sas_phy->phy; |
| 318 | |
| 319 | sphy->negotiated_linkrate = sas_phy->linkrate; |
| 320 | sphy->minimum_linkrate = phy->minimum_linkrate; |
| 321 | sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS; |
| 322 | sphy->maximum_linkrate = phy->maximum_linkrate; |
| 323 | } |
| 324 | |
| 325 | if (phy->phy_type & PORT_TYPE_SAS) { |
| 326 | struct sas_identify_frame *id; |
| 327 | |
| 328 | id = (struct sas_identify_frame *)phy->frame_rcvd; |
| 329 | id->dev_type = phy->identify.device_type; |
| 330 | id->initiator_bits = SAS_PROTOCOL_ALL; |
| 331 | id->target_bits = phy->identify.target_port_protocols; |
| 332 | } else if (phy->phy_type & PORT_TYPE_SATA) { |
| 333 | /*Nothing*/ |
| 334 | } |
| 335 | |
| 336 | sas_phy->frame_rcvd_size = phy->frame_rcvd_size; |
| 337 | sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED); |
| 338 | } |
| 339 | |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 340 | static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device) |
| 341 | { |
| 342 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
| 343 | struct hisi_sas_device *sas_dev = NULL; |
| 344 | int i; |
| 345 | |
| 346 | spin_lock(&hisi_hba->lock); |
| 347 | for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { |
| 348 | if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) { |
| 349 | hisi_hba->devices[i].device_id = i; |
| 350 | sas_dev = &hisi_hba->devices[i]; |
| 351 | sas_dev->dev_status = HISI_SAS_DEV_NORMAL; |
| 352 | sas_dev->dev_type = device->dev_type; |
| 353 | sas_dev->hisi_hba = hisi_hba; |
| 354 | sas_dev->sas_device = device; |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | spin_unlock(&hisi_hba->lock); |
| 359 | |
| 360 | return sas_dev; |
| 361 | } |
| 362 | |
| 363 | static int hisi_sas_dev_found(struct domain_device *device) |
| 364 | { |
| 365 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
| 366 | struct domain_device *parent_dev = device->parent; |
| 367 | struct hisi_sas_device *sas_dev; |
| 368 | struct device *dev = &hisi_hba->pdev->dev; |
| 369 | |
| 370 | sas_dev = hisi_sas_alloc_dev(device); |
| 371 | if (!sas_dev) { |
| 372 | dev_err(dev, "fail alloc dev: max support %d devices\n", |
| 373 | HISI_SAS_MAX_DEVICES); |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
| 377 | device->lldd_dev = sas_dev; |
| 378 | hisi_hba->hw->setup_itct(hisi_hba, sas_dev); |
| 379 | |
| 380 | if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) { |
| 381 | int phy_no; |
| 382 | u8 phy_num = parent_dev->ex_dev.num_phys; |
| 383 | struct ex_phy *phy; |
| 384 | |
| 385 | for (phy_no = 0; phy_no < phy_num; phy_no++) { |
| 386 | phy = &parent_dev->ex_dev.ex_phy[phy_no]; |
| 387 | if (SAS_ADDR(phy->attached_sas_addr) == |
| 388 | SAS_ADDR(device->sas_addr)) { |
| 389 | sas_dev->attached_phy = phy_no; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (phy_no == phy_num) { |
| 395 | dev_info(dev, "dev found: no attached " |
| 396 | "dev:%016llx at ex:%016llx\n", |
| 397 | SAS_ADDR(device->sas_addr), |
| 398 | SAS_ADDR(parent_dev->sas_addr)); |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
John Garry | 701f75e | 2015-11-18 00:50:55 +0800 | [diff] [blame] | 406 | static void hisi_sas_scan_start(struct Scsi_Host *shost) |
| 407 | { |
| 408 | struct hisi_hba *hisi_hba = shost_priv(shost); |
| 409 | int i; |
| 410 | |
| 411 | for (i = 0; i < hisi_hba->n_phy; ++i) |
| 412 | hisi_sas_bytes_dmaed(hisi_hba, i); |
| 413 | |
| 414 | hisi_hba->scan_finished = 1; |
| 415 | } |
| 416 | |
| 417 | static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time) |
| 418 | { |
| 419 | struct hisi_hba *hisi_hba = shost_priv(shost); |
| 420 | struct sas_ha_struct *sha = &hisi_hba->sha; |
| 421 | |
| 422 | if (hisi_hba->scan_finished == 0) |
| 423 | return 0; |
| 424 | |
| 425 | sas_drain_work(sha); |
| 426 | return 1; |
| 427 | } |
| 428 | |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 429 | static void hisi_sas_phyup_work(struct work_struct *work) |
| 430 | { |
| 431 | struct hisi_sas_phy *phy = |
| 432 | container_of(work, struct hisi_sas_phy, phyup_ws); |
| 433 | struct hisi_hba *hisi_hba = phy->hisi_hba; |
| 434 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 435 | int phy_no = sas_phy->id; |
| 436 | |
| 437 | hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */ |
| 438 | hisi_sas_bytes_dmaed(hisi_hba, phy_no); |
| 439 | } |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 440 | |
| 441 | static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no) |
| 442 | { |
| 443 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; |
| 444 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 445 | |
| 446 | phy->hisi_hba = hisi_hba; |
| 447 | phy->port = NULL; |
| 448 | init_timer(&phy->timer); |
| 449 | sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0; |
| 450 | sas_phy->class = SAS; |
| 451 | sas_phy->iproto = SAS_PROTOCOL_ALL; |
| 452 | sas_phy->tproto = 0; |
| 453 | sas_phy->type = PHY_TYPE_PHYSICAL; |
| 454 | sas_phy->role = PHY_ROLE_INITIATOR; |
| 455 | sas_phy->oob_mode = OOB_NOT_CONNECTED; |
| 456 | sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN; |
| 457 | sas_phy->id = phy_no; |
| 458 | sas_phy->sas_addr = &hisi_hba->sas_addr[0]; |
| 459 | sas_phy->frame_rcvd = &phy->frame_rcvd[0]; |
| 460 | sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata; |
| 461 | sas_phy->lldd_phy = phy; |
John Garry | 6613992 | 2015-11-18 00:50:48 +0800 | [diff] [blame] | 462 | |
| 463 | INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work); |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 464 | } |
| 465 | |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 466 | static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy) |
| 467 | { |
| 468 | struct sas_ha_struct *sas_ha = sas_phy->ha; |
| 469 | struct hisi_hba *hisi_hba = sas_ha->lldd_ha; |
| 470 | struct hisi_sas_phy *phy = sas_phy->lldd_phy; |
| 471 | struct asd_sas_port *sas_port = sas_phy->port; |
| 472 | struct hisi_sas_port *port = &hisi_hba->port[sas_phy->id]; |
| 473 | unsigned long flags; |
| 474 | |
| 475 | if (!sas_port) |
| 476 | return; |
| 477 | |
| 478 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 479 | port->port_attached = 1; |
| 480 | port->id = phy->port_id; |
| 481 | phy->port = port; |
| 482 | sas_port->lldd_port = port; |
| 483 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 484 | } |
| 485 | |
| 486 | static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, int phy_no, |
| 487 | struct domain_device *device) |
| 488 | { |
| 489 | struct hisi_sas_phy *phy; |
| 490 | struct hisi_sas_port *port; |
| 491 | struct hisi_sas_slot *slot, *slot2; |
| 492 | struct device *dev = &hisi_hba->pdev->dev; |
| 493 | |
| 494 | phy = &hisi_hba->phy[phy_no]; |
| 495 | port = phy->port; |
| 496 | if (!port) |
| 497 | return; |
| 498 | |
| 499 | list_for_each_entry_safe(slot, slot2, &port->list, entry) { |
| 500 | struct sas_task *task; |
| 501 | |
| 502 | task = slot->task; |
| 503 | if (device && task->dev != device) |
| 504 | continue; |
| 505 | |
| 506 | dev_info(dev, "Release slot [%d:%d], task [%p]:\n", |
| 507 | slot->dlvry_queue, slot->dlvry_queue_slot, task); |
| 508 | hisi_hba->hw->slot_complete(hisi_hba, slot, 1); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | static void hisi_sas_port_notify_deformed(struct asd_sas_phy *sas_phy) |
| 513 | { |
| 514 | struct domain_device *device; |
| 515 | struct hisi_sas_phy *phy = sas_phy->lldd_phy; |
| 516 | struct asd_sas_port *sas_port = sas_phy->port; |
| 517 | |
| 518 | list_for_each_entry(device, &sas_port->dev_list, dev_list_node) |
| 519 | hisi_sas_do_release_task(phy->hisi_hba, sas_phy->id, device); |
| 520 | } |
| 521 | |
| 522 | static void hisi_sas_release_task(struct hisi_hba *hisi_hba, |
| 523 | struct domain_device *device) |
| 524 | { |
| 525 | struct asd_sas_port *port = device->port; |
| 526 | struct asd_sas_phy *sas_phy; |
| 527 | |
| 528 | list_for_each_entry(sas_phy, &port->phy_list, port_phy_el) |
| 529 | hisi_sas_do_release_task(hisi_hba, sas_phy->id, device); |
| 530 | } |
| 531 | |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 532 | static void hisi_sas_dev_gone(struct domain_device *device) |
| 533 | { |
| 534 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 535 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
| 536 | struct device *dev = &hisi_hba->pdev->dev; |
| 537 | u64 dev_id = sas_dev->device_id; |
| 538 | |
| 539 | dev_info(dev, "found dev[%lld:%x] is gone\n", |
| 540 | sas_dev->device_id, sas_dev->dev_type); |
| 541 | |
| 542 | hisi_hba->hw->free_device(hisi_hba, sas_dev); |
| 543 | device->lldd_dev = NULL; |
| 544 | memset(sas_dev, 0, sizeof(*sas_dev)); |
| 545 | sas_dev->device_id = dev_id; |
| 546 | sas_dev->dev_type = SAS_PHY_UNUSED; |
| 547 | sas_dev->dev_status = HISI_SAS_DEV_NORMAL; |
| 548 | } |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 549 | |
| 550 | static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags) |
| 551 | { |
| 552 | return hisi_sas_task_exec(task, gfp_flags, 0, NULL); |
| 553 | } |
| 554 | |
John Garry | e4189d5 | 2015-11-18 00:50:57 +0800 | [diff] [blame] | 555 | static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func, |
| 556 | void *funcdata) |
| 557 | { |
| 558 | struct sas_ha_struct *sas_ha = sas_phy->ha; |
| 559 | struct hisi_hba *hisi_hba = sas_ha->lldd_ha; |
| 560 | int phy_no = sas_phy->id; |
| 561 | |
| 562 | switch (func) { |
| 563 | case PHY_FUNC_HARD_RESET: |
| 564 | hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no); |
| 565 | break; |
| 566 | |
| 567 | case PHY_FUNC_LINK_RESET: |
| 568 | hisi_hba->hw->phy_enable(hisi_hba, phy_no); |
| 569 | hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no); |
| 570 | break; |
| 571 | |
| 572 | case PHY_FUNC_DISABLE: |
| 573 | hisi_hba->hw->phy_disable(hisi_hba, phy_no); |
| 574 | break; |
| 575 | |
| 576 | case PHY_FUNC_SET_LINK_RATE: |
| 577 | case PHY_FUNC_RELEASE_SPINUP_HOLD: |
| 578 | default: |
| 579 | return -EOPNOTSUPP; |
| 580 | } |
| 581 | return 0; |
| 582 | } |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 583 | |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 584 | static void hisi_sas_task_done(struct sas_task *task) |
| 585 | { |
| 586 | if (!del_timer(&task->slow_task->timer)) |
| 587 | return; |
| 588 | complete(&task->slow_task->completion); |
| 589 | } |
| 590 | |
| 591 | static void hisi_sas_tmf_timedout(unsigned long data) |
| 592 | { |
| 593 | struct sas_task *task = (struct sas_task *)data; |
| 594 | |
| 595 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; |
| 596 | complete(&task->slow_task->completion); |
| 597 | } |
| 598 | |
| 599 | #define TASK_TIMEOUT 20 |
| 600 | #define TASK_RETRY 3 |
| 601 | static int hisi_sas_exec_internal_tmf_task(struct domain_device *device, |
| 602 | void *parameter, u32 para_len, |
| 603 | struct hisi_sas_tmf_task *tmf) |
| 604 | { |
| 605 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 606 | struct hisi_hba *hisi_hba = sas_dev->hisi_hba; |
| 607 | struct device *dev = &hisi_hba->pdev->dev; |
| 608 | struct sas_task *task; |
| 609 | int res, retry; |
| 610 | |
| 611 | for (retry = 0; retry < TASK_RETRY; retry++) { |
| 612 | task = sas_alloc_slow_task(GFP_KERNEL); |
| 613 | if (!task) |
| 614 | return -ENOMEM; |
| 615 | |
| 616 | task->dev = device; |
| 617 | task->task_proto = device->tproto; |
| 618 | |
| 619 | memcpy(&task->ssp_task, parameter, para_len); |
| 620 | task->task_done = hisi_sas_task_done; |
| 621 | |
| 622 | task->slow_task->timer.data = (unsigned long) task; |
| 623 | task->slow_task->timer.function = hisi_sas_tmf_timedout; |
| 624 | task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ; |
| 625 | add_timer(&task->slow_task->timer); |
| 626 | |
| 627 | res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf); |
| 628 | |
| 629 | if (res) { |
| 630 | del_timer(&task->slow_task->timer); |
| 631 | dev_err(dev, "abort tmf: executing internal task failed: %d\n", |
| 632 | res); |
| 633 | goto ex_err; |
| 634 | } |
| 635 | |
| 636 | wait_for_completion(&task->slow_task->completion); |
| 637 | res = TMF_RESP_FUNC_FAILED; |
| 638 | /* Even TMF timed out, return direct. */ |
| 639 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { |
| 640 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { |
| 641 | dev_err(dev, "abort tmf: TMF task[%d] timeout\n", |
| 642 | tmf->tag_of_task_to_be_managed); |
| 643 | if (task->lldd_task) { |
| 644 | struct hisi_sas_slot *slot = |
| 645 | task->lldd_task; |
| 646 | |
| 647 | hisi_sas_slot_task_free(hisi_hba, |
| 648 | task, slot); |
| 649 | } |
| 650 | |
| 651 | goto ex_err; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
| 656 | task->task_status.stat == SAM_STAT_GOOD) { |
| 657 | res = TMF_RESP_FUNC_COMPLETE; |
| 658 | break; |
| 659 | } |
| 660 | |
| 661 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
| 662 | task->task_status.stat == SAS_DATA_UNDERRUN) { |
| 663 | /* no error, but return the number of bytes of |
| 664 | * underrun |
| 665 | */ |
| 666 | dev_warn(dev, "abort tmf: task to dev %016llx " |
| 667 | "resp: 0x%x sts 0x%x underrun\n", |
| 668 | SAS_ADDR(device->sas_addr), |
| 669 | task->task_status.resp, |
| 670 | task->task_status.stat); |
| 671 | res = task->task_status.residual; |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
| 676 | task->task_status.stat == SAS_DATA_OVERRUN) { |
| 677 | dev_warn(dev, "abort tmf: blocked task error\n"); |
| 678 | res = -EMSGSIZE; |
| 679 | break; |
| 680 | } |
| 681 | |
| 682 | dev_warn(dev, "abort tmf: task to dev " |
| 683 | "%016llx resp: 0x%x status 0x%x\n", |
| 684 | SAS_ADDR(device->sas_addr), task->task_status.resp, |
| 685 | task->task_status.stat); |
| 686 | sas_free_task(task); |
| 687 | task = NULL; |
| 688 | } |
| 689 | ex_err: |
| 690 | WARN_ON(retry == TASK_RETRY); |
| 691 | sas_free_task(task); |
| 692 | return res; |
| 693 | } |
| 694 | |
| 695 | static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device, |
| 696 | u8 *lun, struct hisi_sas_tmf_task *tmf) |
| 697 | { |
| 698 | struct sas_ssp_task ssp_task; |
| 699 | |
| 700 | if (!(device->tproto & SAS_PROTOCOL_SSP)) |
| 701 | return TMF_RESP_FUNC_ESUPP; |
| 702 | |
| 703 | memcpy(ssp_task.LUN, lun, 8); |
| 704 | |
| 705 | return hisi_sas_exec_internal_tmf_task(device, &ssp_task, |
| 706 | sizeof(ssp_task), tmf); |
| 707 | } |
| 708 | |
| 709 | static int hisi_sas_abort_task(struct sas_task *task) |
| 710 | { |
| 711 | struct scsi_lun lun; |
| 712 | struct hisi_sas_tmf_task tmf_task; |
| 713 | struct domain_device *device = task->dev; |
| 714 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 715 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev); |
| 716 | struct device *dev = &hisi_hba->pdev->dev; |
| 717 | int rc = TMF_RESP_FUNC_FAILED; |
| 718 | unsigned long flags; |
| 719 | |
| 720 | if (!sas_dev) { |
| 721 | dev_warn(dev, "Device has been removed\n"); |
| 722 | return TMF_RESP_FUNC_FAILED; |
| 723 | } |
| 724 | |
| 725 | spin_lock_irqsave(&task->task_state_lock, flags); |
| 726 | if (task->task_state_flags & SAS_TASK_STATE_DONE) { |
| 727 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 728 | rc = TMF_RESP_FUNC_COMPLETE; |
| 729 | goto out; |
| 730 | } |
| 731 | |
| 732 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 733 | sas_dev->dev_status = HISI_SAS_DEV_EH; |
| 734 | if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) { |
| 735 | struct scsi_cmnd *cmnd = task->uldd_task; |
| 736 | struct hisi_sas_slot *slot = task->lldd_task; |
| 737 | u32 tag = slot->idx; |
| 738 | |
| 739 | int_to_scsilun(cmnd->device->lun, &lun); |
| 740 | tmf_task.tmf = TMF_ABORT_TASK; |
| 741 | tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag); |
| 742 | |
| 743 | rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, |
| 744 | &tmf_task); |
| 745 | |
| 746 | /* if successful, clear the task and callback forwards.*/ |
| 747 | if (rc == TMF_RESP_FUNC_COMPLETE) { |
| 748 | if (task->lldd_task) { |
| 749 | struct hisi_sas_slot *slot; |
| 750 | |
| 751 | slot = &hisi_hba->slot_info |
| 752 | [tmf_task.tag_of_task_to_be_managed]; |
| 753 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 754 | hisi_hba->hw->slot_complete(hisi_hba, slot, 1); |
| 755 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | } else if (task->task_proto & SAS_PROTOCOL_SATA || |
| 760 | task->task_proto & SAS_PROTOCOL_STP) { |
| 761 | if (task->dev->dev_type == SAS_SATA_DEV) { |
| 762 | struct hisi_slot_info *slot = task->lldd_task; |
| 763 | |
| 764 | dev_notice(dev, "abort task: hba=%p task=%p slot=%p\n", |
| 765 | hisi_hba, task, slot); |
| 766 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; |
| 767 | rc = TMF_RESP_FUNC_COMPLETE; |
| 768 | goto out; |
| 769 | } |
| 770 | |
| 771 | } |
| 772 | |
| 773 | out: |
| 774 | if (rc != TMF_RESP_FUNC_COMPLETE) |
| 775 | dev_notice(dev, "abort task: rc=%d\n", rc); |
| 776 | return rc; |
| 777 | } |
| 778 | |
| 779 | static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun) |
| 780 | { |
| 781 | struct hisi_sas_tmf_task tmf_task; |
| 782 | int rc = TMF_RESP_FUNC_FAILED; |
| 783 | |
| 784 | tmf_task.tmf = TMF_ABORT_TASK_SET; |
| 785 | rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); |
| 786 | |
| 787 | return rc; |
| 788 | } |
| 789 | |
| 790 | static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun) |
| 791 | { |
| 792 | int rc = TMF_RESP_FUNC_FAILED; |
| 793 | struct hisi_sas_tmf_task tmf_task; |
| 794 | |
| 795 | tmf_task.tmf = TMF_CLEAR_ACA; |
| 796 | rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); |
| 797 | |
| 798 | return rc; |
| 799 | } |
| 800 | |
| 801 | static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device) |
| 802 | { |
| 803 | struct sas_phy *phy = sas_get_local_phy(device); |
| 804 | int rc, reset_type = (device->dev_type == SAS_SATA_DEV || |
| 805 | (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1; |
| 806 | rc = sas_phy_reset(phy, reset_type); |
| 807 | sas_put_local_phy(phy); |
| 808 | msleep(2000); |
| 809 | return rc; |
| 810 | } |
| 811 | |
| 812 | static int hisi_sas_I_T_nexus_reset(struct domain_device *device) |
| 813 | { |
| 814 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 815 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
| 816 | unsigned long flags; |
| 817 | int rc = TMF_RESP_FUNC_FAILED; |
| 818 | |
| 819 | if (sas_dev->dev_status != HISI_SAS_DEV_EH) |
| 820 | return TMF_RESP_FUNC_FAILED; |
| 821 | sas_dev->dev_status = HISI_SAS_DEV_NORMAL; |
| 822 | |
| 823 | rc = hisi_sas_debug_I_T_nexus_reset(device); |
| 824 | |
| 825 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 826 | hisi_sas_release_task(hisi_hba, device); |
| 827 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 828 | |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun) |
| 833 | { |
| 834 | struct hisi_sas_tmf_task tmf_task; |
| 835 | struct hisi_sas_device *sas_dev = device->lldd_dev; |
| 836 | struct hisi_hba *hisi_hba = dev_to_hisi_hba(device); |
| 837 | struct device *dev = &hisi_hba->pdev->dev; |
| 838 | unsigned long flags; |
| 839 | int rc = TMF_RESP_FUNC_FAILED; |
| 840 | |
| 841 | tmf_task.tmf = TMF_LU_RESET; |
| 842 | sas_dev->dev_status = HISI_SAS_DEV_EH; |
| 843 | rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task); |
| 844 | if (rc == TMF_RESP_FUNC_COMPLETE) { |
| 845 | spin_lock_irqsave(&hisi_hba->lock, flags); |
| 846 | hisi_sas_release_task(hisi_hba, device); |
| 847 | spin_unlock_irqrestore(&hisi_hba->lock, flags); |
| 848 | } |
| 849 | |
| 850 | /* If failed, fall-through I_T_Nexus reset */ |
| 851 | dev_err(dev, "lu_reset: for device[%llx]:rc= %d\n", |
| 852 | sas_dev->device_id, rc); |
| 853 | return rc; |
| 854 | } |
| 855 | |
| 856 | static int hisi_sas_query_task(struct sas_task *task) |
| 857 | { |
| 858 | struct scsi_lun lun; |
| 859 | struct hisi_sas_tmf_task tmf_task; |
| 860 | int rc = TMF_RESP_FUNC_FAILED; |
| 861 | |
| 862 | if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) { |
| 863 | struct scsi_cmnd *cmnd = task->uldd_task; |
| 864 | struct domain_device *device = task->dev; |
| 865 | struct hisi_sas_slot *slot = task->lldd_task; |
| 866 | u32 tag = slot->idx; |
| 867 | |
| 868 | int_to_scsilun(cmnd->device->lun, &lun); |
| 869 | tmf_task.tmf = TMF_QUERY_TASK; |
| 870 | tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag); |
| 871 | |
| 872 | rc = hisi_sas_debug_issue_ssp_tmf(device, |
| 873 | lun.scsi_lun, |
| 874 | &tmf_task); |
| 875 | switch (rc) { |
| 876 | /* The task is still in Lun, release it then */ |
| 877 | case TMF_RESP_FUNC_SUCC: |
| 878 | /* The task is not in Lun or failed, reset the phy */ |
| 879 | case TMF_RESP_FUNC_FAILED: |
| 880 | case TMF_RESP_FUNC_COMPLETE: |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | return rc; |
| 885 | } |
| 886 | |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 887 | static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy) |
| 888 | { |
| 889 | hisi_sas_port_notify_formed(sas_phy); |
| 890 | } |
| 891 | |
| 892 | static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy) |
| 893 | { |
| 894 | hisi_sas_port_notify_deformed(sas_phy); |
| 895 | } |
| 896 | |
| 897 | static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy) |
| 898 | { |
| 899 | phy->phy_attached = 0; |
| 900 | phy->phy_type = 0; |
| 901 | phy->port = NULL; |
| 902 | } |
| 903 | |
| 904 | void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy) |
| 905 | { |
| 906 | struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no]; |
| 907 | struct asd_sas_phy *sas_phy = &phy->sas_phy; |
| 908 | struct sas_ha_struct *sas_ha = &hisi_hba->sha; |
| 909 | |
| 910 | if (rdy) { |
| 911 | /* Phy down but ready */ |
| 912 | hisi_sas_bytes_dmaed(hisi_hba, phy_no); |
| 913 | hisi_sas_port_notify_formed(sas_phy); |
| 914 | } else { |
| 915 | struct hisi_sas_port *port = phy->port; |
| 916 | |
| 917 | /* Phy down and not ready */ |
| 918 | sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL); |
| 919 | sas_phy_disconnected(sas_phy); |
| 920 | |
| 921 | if (port) { |
| 922 | if (phy->phy_type & PORT_TYPE_SAS) { |
| 923 | int port_id = port->id; |
| 924 | |
| 925 | if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba, |
| 926 | port_id)) |
| 927 | port->port_attached = 0; |
| 928 | } else if (phy->phy_type & PORT_TYPE_SATA) |
| 929 | port->port_attached = 0; |
| 930 | } |
| 931 | hisi_sas_phy_disconnected(phy); |
| 932 | } |
| 933 | } |
| 934 | EXPORT_SYMBOL_GPL(hisi_sas_phy_down); |
| 935 | |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 936 | static struct scsi_transport_template *hisi_sas_stt; |
| 937 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 938 | static struct scsi_host_template hisi_sas_sht = { |
| 939 | .module = THIS_MODULE, |
| 940 | .name = DRV_NAME, |
| 941 | .queuecommand = sas_queuecommand, |
| 942 | .target_alloc = sas_target_alloc, |
| 943 | .slave_configure = sas_slave_configure, |
John Garry | 701f75e | 2015-11-18 00:50:55 +0800 | [diff] [blame] | 944 | .scan_finished = hisi_sas_scan_finished, |
| 945 | .scan_start = hisi_sas_scan_start, |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 946 | .change_queue_depth = sas_change_queue_depth, |
| 947 | .bios_param = sas_bios_param, |
| 948 | .can_queue = 1, |
| 949 | .this_id = -1, |
| 950 | .sg_tablesize = SG_ALL, |
| 951 | .max_sectors = SCSI_DEFAULT_MAX_SECTORS, |
| 952 | .use_clustering = ENABLE_CLUSTERING, |
| 953 | .eh_device_reset_handler = sas_eh_device_reset_handler, |
| 954 | .eh_bus_reset_handler = sas_eh_bus_reset_handler, |
| 955 | .target_destroy = sas_target_destroy, |
| 956 | .ioctl = sas_ioctl, |
| 957 | }; |
| 958 | |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 959 | static struct sas_domain_function_template hisi_sas_transport_ops = { |
John Garry | abda97c | 2015-11-18 00:50:51 +0800 | [diff] [blame] | 960 | .lldd_dev_found = hisi_sas_dev_found, |
| 961 | .lldd_dev_gone = hisi_sas_dev_gone, |
John Garry | 42e7a69 | 2015-11-18 00:50:49 +0800 | [diff] [blame] | 962 | .lldd_execute_task = hisi_sas_queue_command, |
John Garry | e4189d5 | 2015-11-18 00:50:57 +0800 | [diff] [blame] | 963 | .lldd_control_phy = hisi_sas_control_phy, |
John Garry | 0efff30 | 2015-11-18 00:50:56 +0800 | [diff] [blame] | 964 | .lldd_abort_task = hisi_sas_abort_task, |
| 965 | .lldd_abort_task_set = hisi_sas_abort_task_set, |
| 966 | .lldd_clear_aca = hisi_sas_clear_aca, |
| 967 | .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset, |
| 968 | .lldd_lu_reset = hisi_sas_lu_reset, |
| 969 | .lldd_query_task = hisi_sas_query_task, |
John Garry | 184a463 | 2015-11-18 00:50:52 +0800 | [diff] [blame] | 970 | .lldd_port_formed = hisi_sas_port_formed, |
| 971 | .lldd_port_deformed = hisi_sas_port_deformed, |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 972 | }; |
| 973 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 974 | static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost) |
| 975 | { |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 976 | struct platform_device *pdev = hisi_hba->pdev; |
| 977 | struct device *dev = &pdev->dev; |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame^] | 978 | int i, s, max_command_entries = hisi_hba->hw->max_command_entries; |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 979 | |
John Garry | fa42d80 | 2015-11-18 00:50:43 +0800 | [diff] [blame] | 980 | spin_lock_init(&hisi_hba->lock); |
John Garry | 976867e | 2015-11-18 00:50:42 +0800 | [diff] [blame] | 981 | for (i = 0; i < hisi_hba->n_phy; i++) { |
| 982 | hisi_sas_phy_init(hisi_hba, i); |
| 983 | hisi_hba->port[i].port_attached = 0; |
| 984 | hisi_hba->port[i].id = -1; |
| 985 | INIT_LIST_HEAD(&hisi_hba->port[i].list); |
| 986 | } |
| 987 | |
John Garry | af740db | 2015-11-18 00:50:41 +0800 | [diff] [blame] | 988 | for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) { |
| 989 | hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED; |
| 990 | hisi_hba->devices[i].device_id = i; |
| 991 | hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL; |
| 992 | } |
| 993 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 994 | for (i = 0; i < hisi_hba->queue_count; i++) { |
John Garry | 9101a07 | 2015-11-18 00:50:37 +0800 | [diff] [blame] | 995 | struct hisi_sas_cq *cq = &hisi_hba->cq[i]; |
| 996 | |
| 997 | /* Completion queue structure */ |
| 998 | cq->id = i; |
| 999 | cq->hisi_hba = hisi_hba; |
| 1000 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1001 | /* Delivery queue */ |
| 1002 | s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; |
| 1003 | hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s, |
| 1004 | &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL); |
| 1005 | if (!hisi_hba->cmd_hdr[i]) |
| 1006 | goto err_out; |
| 1007 | memset(hisi_hba->cmd_hdr[i], 0, s); |
| 1008 | |
| 1009 | /* Completion queue */ |
| 1010 | s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; |
| 1011 | hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s, |
| 1012 | &hisi_hba->complete_hdr_dma[i], GFP_KERNEL); |
| 1013 | if (!hisi_hba->complete_hdr[i]) |
| 1014 | goto err_out; |
| 1015 | memset(hisi_hba->complete_hdr[i], 0, s); |
| 1016 | } |
| 1017 | |
| 1018 | s = HISI_SAS_STATUS_BUF_SZ; |
| 1019 | hisi_hba->status_buffer_pool = dma_pool_create("status_buffer", |
| 1020 | dev, s, 16, 0); |
| 1021 | if (!hisi_hba->status_buffer_pool) |
| 1022 | goto err_out; |
| 1023 | |
| 1024 | s = HISI_SAS_COMMAND_TABLE_SZ; |
| 1025 | hisi_hba->command_table_pool = dma_pool_create("command_table", |
| 1026 | dev, s, 16, 0); |
| 1027 | if (!hisi_hba->command_table_pool) |
| 1028 | goto err_out; |
| 1029 | |
| 1030 | s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); |
| 1031 | hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma, |
| 1032 | GFP_KERNEL); |
| 1033 | if (!hisi_hba->itct) |
| 1034 | goto err_out; |
| 1035 | |
| 1036 | memset(hisi_hba->itct, 0, s); |
| 1037 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame^] | 1038 | hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries, |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1039 | sizeof(struct hisi_sas_slot), |
| 1040 | GFP_KERNEL); |
| 1041 | if (!hisi_hba->slot_info) |
| 1042 | goto err_out; |
| 1043 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame^] | 1044 | s = max_command_entries * sizeof(struct hisi_sas_iost); |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1045 | hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma, |
| 1046 | GFP_KERNEL); |
| 1047 | if (!hisi_hba->iost) |
| 1048 | goto err_out; |
| 1049 | |
| 1050 | memset(hisi_hba->iost, 0, s); |
| 1051 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame^] | 1052 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint); |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1053 | hisi_hba->breakpoint = dma_alloc_coherent(dev, s, |
| 1054 | &hisi_hba->breakpoint_dma, GFP_KERNEL); |
| 1055 | if (!hisi_hba->breakpoint) |
| 1056 | goto err_out; |
| 1057 | |
| 1058 | memset(hisi_hba->breakpoint, 0, s); |
| 1059 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame^] | 1060 | hisi_hba->slot_index_count = max_command_entries; |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 1061 | s = hisi_hba->slot_index_count / sizeof(unsigned long); |
| 1062 | hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL); |
| 1063 | if (!hisi_hba->slot_index_tags) |
| 1064 | goto err_out; |
| 1065 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1066 | hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev, |
| 1067 | sizeof(struct hisi_sas_sge_page), 16, 0); |
| 1068 | if (!hisi_hba->sge_page_pool) |
| 1069 | goto err_out; |
| 1070 | |
| 1071 | s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; |
| 1072 | hisi_hba->initial_fis = dma_alloc_coherent(dev, s, |
| 1073 | &hisi_hba->initial_fis_dma, GFP_KERNEL); |
| 1074 | if (!hisi_hba->initial_fis) |
| 1075 | goto err_out; |
| 1076 | memset(hisi_hba->initial_fis, 0, s); |
| 1077 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame^] | 1078 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2; |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1079 | hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s, |
| 1080 | &hisi_hba->sata_breakpoint_dma, GFP_KERNEL); |
| 1081 | if (!hisi_hba->sata_breakpoint) |
| 1082 | goto err_out; |
| 1083 | memset(hisi_hba->sata_breakpoint, 0, s); |
| 1084 | |
John Garry | 257efd1 | 2015-11-18 00:50:36 +0800 | [diff] [blame] | 1085 | hisi_sas_slot_index_init(hisi_hba); |
| 1086 | |
John Garry | 7e9080e | 2015-11-18 00:50:40 +0800 | [diff] [blame] | 1087 | hisi_hba->wq = create_singlethread_workqueue(dev_name(dev)); |
| 1088 | if (!hisi_hba->wq) { |
| 1089 | dev_err(dev, "sas_alloc: failed to create workqueue\n"); |
| 1090 | goto err_out; |
| 1091 | } |
| 1092 | |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1093 | return 0; |
| 1094 | err_out: |
| 1095 | return -ENOMEM; |
| 1096 | } |
| 1097 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1098 | static void hisi_sas_free(struct hisi_hba *hisi_hba) |
| 1099 | { |
| 1100 | struct device *dev = &hisi_hba->pdev->dev; |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame^] | 1101 | int i, s, max_command_entries = hisi_hba->hw->max_command_entries; |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1102 | |
| 1103 | for (i = 0; i < hisi_hba->queue_count; i++) { |
| 1104 | s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS; |
| 1105 | if (hisi_hba->cmd_hdr[i]) |
| 1106 | dma_free_coherent(dev, s, |
| 1107 | hisi_hba->cmd_hdr[i], |
| 1108 | hisi_hba->cmd_hdr_dma[i]); |
| 1109 | |
| 1110 | s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS; |
| 1111 | if (hisi_hba->complete_hdr[i]) |
| 1112 | dma_free_coherent(dev, s, |
| 1113 | hisi_hba->complete_hdr[i], |
| 1114 | hisi_hba->complete_hdr_dma[i]); |
| 1115 | } |
| 1116 | |
| 1117 | dma_pool_destroy(hisi_hba->status_buffer_pool); |
| 1118 | dma_pool_destroy(hisi_hba->command_table_pool); |
| 1119 | dma_pool_destroy(hisi_hba->sge_page_pool); |
| 1120 | |
| 1121 | s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct); |
| 1122 | if (hisi_hba->itct) |
| 1123 | dma_free_coherent(dev, s, |
| 1124 | hisi_hba->itct, hisi_hba->itct_dma); |
| 1125 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame^] | 1126 | s = max_command_entries * sizeof(struct hisi_sas_iost); |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1127 | if (hisi_hba->iost) |
| 1128 | dma_free_coherent(dev, s, |
| 1129 | hisi_hba->iost, hisi_hba->iost_dma); |
| 1130 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame^] | 1131 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint); |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1132 | if (hisi_hba->breakpoint) |
| 1133 | dma_free_coherent(dev, s, |
| 1134 | hisi_hba->breakpoint, |
| 1135 | hisi_hba->breakpoint_dma); |
| 1136 | |
| 1137 | |
| 1138 | s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS; |
| 1139 | if (hisi_hba->initial_fis) |
| 1140 | dma_free_coherent(dev, s, |
| 1141 | hisi_hba->initial_fis, |
| 1142 | hisi_hba->initial_fis_dma); |
| 1143 | |
John Garry | a8d547b | 2016-01-26 02:47:03 +0800 | [diff] [blame^] | 1144 | s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2; |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1145 | if (hisi_hba->sata_breakpoint) |
| 1146 | dma_free_coherent(dev, s, |
| 1147 | hisi_hba->sata_breakpoint, |
| 1148 | hisi_hba->sata_breakpoint_dma); |
| 1149 | |
John Garry | 7e9080e | 2015-11-18 00:50:40 +0800 | [diff] [blame] | 1150 | if (hisi_hba->wq) |
| 1151 | destroy_workqueue(hisi_hba->wq); |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1152 | } |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1153 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1154 | static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev, |
| 1155 | const struct hisi_sas_hw *hw) |
| 1156 | { |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 1157 | struct resource *res; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1158 | struct Scsi_Host *shost; |
| 1159 | struct hisi_hba *hisi_hba; |
| 1160 | struct device *dev = &pdev->dev; |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 1161 | struct device_node *np = pdev->dev.of_node; |
| 1162 | struct property *sas_addr_prop; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1163 | |
| 1164 | shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba)); |
| 1165 | if (!shost) |
| 1166 | goto err_out; |
| 1167 | hisi_hba = shost_priv(shost); |
| 1168 | |
| 1169 | hisi_hba->hw = hw; |
| 1170 | hisi_hba->pdev = pdev; |
| 1171 | hisi_hba->shost = shost; |
| 1172 | SHOST_TO_SAS_HA(shost) = &hisi_hba->sha; |
| 1173 | |
John Garry | fa42d80 | 2015-11-18 00:50:43 +0800 | [diff] [blame] | 1174 | init_timer(&hisi_hba->timer); |
| 1175 | |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 1176 | sas_addr_prop = of_find_property(np, "sas-addr", NULL); |
| 1177 | if (!sas_addr_prop || (sas_addr_prop->length != SAS_ADDR_SIZE)) |
| 1178 | goto err_out; |
| 1179 | memcpy(hisi_hba->sas_addr, sas_addr_prop->value, SAS_ADDR_SIZE); |
| 1180 | |
| 1181 | if (of_property_read_u32(np, "ctrl-reset-reg", |
| 1182 | &hisi_hba->ctrl_reset_reg)) |
| 1183 | goto err_out; |
| 1184 | |
| 1185 | if (of_property_read_u32(np, "ctrl-reset-sts-reg", |
| 1186 | &hisi_hba->ctrl_reset_sts_reg)) |
| 1187 | goto err_out; |
| 1188 | |
| 1189 | if (of_property_read_u32(np, "ctrl-clock-ena-reg", |
| 1190 | &hisi_hba->ctrl_clock_ena_reg)) |
| 1191 | goto err_out; |
| 1192 | |
| 1193 | if (of_property_read_u32(np, "phy-count", &hisi_hba->n_phy)) |
| 1194 | goto err_out; |
| 1195 | |
| 1196 | if (of_property_read_u32(np, "queue-count", &hisi_hba->queue_count)) |
| 1197 | goto err_out; |
| 1198 | |
John Garry | e26b2f4 | 2015-11-18 00:50:32 +0800 | [diff] [blame] | 1199 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1200 | hisi_hba->regs = devm_ioremap_resource(dev, res); |
| 1201 | if (IS_ERR(hisi_hba->regs)) |
| 1202 | goto err_out; |
| 1203 | |
| 1204 | hisi_hba->ctrl = syscon_regmap_lookup_by_phandle( |
| 1205 | np, "hisilicon,sas-syscon"); |
| 1206 | if (IS_ERR(hisi_hba->ctrl)) |
| 1207 | goto err_out; |
| 1208 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1209 | if (hisi_sas_alloc(hisi_hba, shost)) { |
| 1210 | hisi_sas_free(hisi_hba); |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1211 | goto err_out; |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1212 | } |
John Garry | 6be6de1 | 2015-11-18 00:50:34 +0800 | [diff] [blame] | 1213 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1214 | return shost; |
| 1215 | err_out: |
| 1216 | dev_err(dev, "shost alloc failed\n"); |
| 1217 | return NULL; |
| 1218 | } |
| 1219 | |
John Garry | 5d74242 | 2015-11-18 00:50:38 +0800 | [diff] [blame] | 1220 | static void hisi_sas_init_add(struct hisi_hba *hisi_hba) |
| 1221 | { |
| 1222 | int i; |
| 1223 | |
| 1224 | for (i = 0; i < hisi_hba->n_phy; i++) |
| 1225 | memcpy(&hisi_hba->phy[i].dev_sas_addr, |
| 1226 | hisi_hba->sas_addr, |
| 1227 | SAS_ADDR_SIZE); |
| 1228 | } |
| 1229 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1230 | int hisi_sas_probe(struct platform_device *pdev, |
| 1231 | const struct hisi_sas_hw *hw) |
| 1232 | { |
| 1233 | struct Scsi_Host *shost; |
| 1234 | struct hisi_hba *hisi_hba; |
| 1235 | struct device *dev = &pdev->dev; |
| 1236 | struct asd_sas_phy **arr_phy; |
| 1237 | struct asd_sas_port **arr_port; |
| 1238 | struct sas_ha_struct *sha; |
| 1239 | int rc, phy_nr, port_nr, i; |
| 1240 | |
| 1241 | shost = hisi_sas_shost_alloc(pdev, hw); |
| 1242 | if (!shost) { |
| 1243 | rc = -ENOMEM; |
| 1244 | goto err_out_ha; |
| 1245 | } |
| 1246 | |
| 1247 | sha = SHOST_TO_SAS_HA(shost); |
| 1248 | hisi_hba = shost_priv(shost); |
| 1249 | platform_set_drvdata(pdev, sha); |
John Garry | 50cb916 | 2015-11-18 00:50:39 +0800 | [diff] [blame] | 1250 | |
| 1251 | if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) && |
| 1252 | dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) { |
| 1253 | dev_err(dev, "No usable DMA addressing method\n"); |
| 1254 | rc = -EIO; |
| 1255 | goto err_out_ha; |
| 1256 | } |
| 1257 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1258 | phy_nr = port_nr = hisi_hba->n_phy; |
| 1259 | |
| 1260 | arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL); |
| 1261 | arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL); |
| 1262 | if (!arr_phy || !arr_port) |
| 1263 | return -ENOMEM; |
| 1264 | |
| 1265 | sha->sas_phy = arr_phy; |
| 1266 | sha->sas_port = arr_port; |
| 1267 | sha->core.shost = shost; |
| 1268 | sha->lldd_ha = hisi_hba; |
| 1269 | |
| 1270 | shost->transportt = hisi_sas_stt; |
| 1271 | shost->max_id = HISI_SAS_MAX_DEVICES; |
| 1272 | shost->max_lun = ~0; |
| 1273 | shost->max_channel = 1; |
| 1274 | shost->max_cmd_len = 16; |
| 1275 | 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^] | 1276 | shost->can_queue = hisi_hba->hw->max_command_entries; |
| 1277 | shost->cmd_per_lun = hisi_hba->hw->max_command_entries; |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1278 | |
| 1279 | sha->sas_ha_name = DRV_NAME; |
| 1280 | sha->dev = &hisi_hba->pdev->dev; |
| 1281 | sha->lldd_module = THIS_MODULE; |
| 1282 | sha->sas_addr = &hisi_hba->sas_addr[0]; |
| 1283 | sha->num_phys = hisi_hba->n_phy; |
| 1284 | sha->core.shost = hisi_hba->shost; |
| 1285 | |
| 1286 | for (i = 0; i < hisi_hba->n_phy; i++) { |
| 1287 | sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy; |
| 1288 | sha->sas_port[i] = &hisi_hba->port[i].sas_port; |
| 1289 | } |
| 1290 | |
John Garry | 5d74242 | 2015-11-18 00:50:38 +0800 | [diff] [blame] | 1291 | hisi_sas_init_add(hisi_hba); |
| 1292 | |
John Garry | 8ff1d57 | 2015-11-18 00:50:46 +0800 | [diff] [blame] | 1293 | rc = hisi_hba->hw->hw_init(hisi_hba); |
| 1294 | if (rc) |
| 1295 | goto err_out_ha; |
| 1296 | |
John Garry | 7eb7869 | 2015-11-18 00:50:31 +0800 | [diff] [blame] | 1297 | rc = scsi_add_host(shost, &pdev->dev); |
| 1298 | if (rc) |
| 1299 | goto err_out_ha; |
| 1300 | |
| 1301 | rc = sas_register_ha(sha); |
| 1302 | if (rc) |
| 1303 | goto err_out_register_ha; |
| 1304 | |
| 1305 | scsi_scan_host(shost); |
| 1306 | |
| 1307 | return 0; |
| 1308 | |
| 1309 | err_out_register_ha: |
| 1310 | scsi_remove_host(shost); |
| 1311 | err_out_ha: |
| 1312 | kfree(shost); |
| 1313 | return rc; |
| 1314 | } |
| 1315 | EXPORT_SYMBOL_GPL(hisi_sas_probe); |
| 1316 | |
John Garry | 89d5332 | 2015-11-18 00:50:35 +0800 | [diff] [blame] | 1317 | int hisi_sas_remove(struct platform_device *pdev) |
| 1318 | { |
| 1319 | struct sas_ha_struct *sha = platform_get_drvdata(pdev); |
| 1320 | struct hisi_hba *hisi_hba = sha->lldd_ha; |
| 1321 | |
| 1322 | scsi_remove_host(sha->core.shost); |
| 1323 | sas_unregister_ha(sha); |
| 1324 | sas_remove_host(sha->core.shost); |
| 1325 | |
| 1326 | hisi_sas_free(hisi_hba); |
| 1327 | return 0; |
| 1328 | } |
| 1329 | EXPORT_SYMBOL_GPL(hisi_sas_remove); |
| 1330 | |
John Garry | e8899fa | 2015-11-18 00:50:30 +0800 | [diff] [blame] | 1331 | static __init int hisi_sas_init(void) |
| 1332 | { |
| 1333 | pr_info("hisi_sas: driver version %s\n", DRV_VERSION); |
| 1334 | |
| 1335 | hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops); |
| 1336 | if (!hisi_sas_stt) |
| 1337 | return -ENOMEM; |
| 1338 | |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | static __exit void hisi_sas_exit(void) |
| 1343 | { |
| 1344 | sas_release_transport(hisi_sas_stt); |
| 1345 | } |
| 1346 | |
| 1347 | module_init(hisi_sas_init); |
| 1348 | module_exit(hisi_sas_exit); |
| 1349 | |
| 1350 | MODULE_VERSION(DRV_VERSION); |
| 1351 | MODULE_LICENSE("GPL"); |
| 1352 | MODULE_AUTHOR("John Garry <john.garry@huawei.com>"); |
| 1353 | MODULE_DESCRIPTION("HISILICON SAS controller driver"); |
| 1354 | MODULE_ALIAS("platform:" DRV_NAME); |