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