1 /* 2 * PMC-Sierra SPC 8001 SAS/SATA based host adapters driver 3 * 4 * Copyright (c) 2008-2009 USI Co., Ltd. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions, and the following disclaimer, 12 * without modification. 13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 14 * substantially similar to the "NO WARRANTY" disclaimer below 15 * ("Disclaimer") and any redistribution must be conditioned upon 16 * including a substantially similar Disclaimer requirement for further 17 * binary redistribution. 18 * 3. Neither the names of the above-listed copyright holders nor the names 19 * of any contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * Alternatively, this software may be distributed under the terms of the 23 * GNU General Public License ("GPL") version 2 as published by the Free 24 * Software Foundation. 25 * 26 * NO WARRANTY 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGES. 38 * 39 */ 40 41 #include "pm8001_sas.h" 42 43 /** 44 * pm8001_find_tag - from sas task to find out tag that belongs to this task 45 * @task: the task sent to the LLDD 46 * @tag: the found tag associated with the task 47 */ 48 static int pm8001_find_tag(struct sas_task *task, u32 *tag) 49 { 50 if (task->lldd_task) { 51 struct pm8001_ccb_info *ccb; 52 ccb = task->lldd_task; 53 *tag = ccb->ccb_tag; 54 return 1; 55 } 56 return 0; 57 } 58 59 /** 60 * pm8001_tag_clear - clear the tags bitmap 61 * @pm8001_ha: our hba struct 62 * @tag: the found tag associated with the task 63 */ 64 static void pm8001_tag_clear(struct pm8001_hba_info *pm8001_ha, u32 tag) 65 { 66 void *bitmap = pm8001_ha->tags; 67 clear_bit(tag, bitmap); 68 } 69 70 static void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag) 71 { 72 pm8001_tag_clear(pm8001_ha, tag); 73 } 74 75 static void pm8001_tag_set(struct pm8001_hba_info *pm8001_ha, u32 tag) 76 { 77 void *bitmap = pm8001_ha->tags; 78 set_bit(tag, bitmap); 79 } 80 81 /** 82 * pm8001_tag_alloc - allocate a empty tag for task used. 83 * @pm8001_ha: our hba struct 84 * @tag_out: the found empty tag . 85 */ 86 inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out) 87 { 88 unsigned int index, tag; 89 void *bitmap = pm8001_ha->tags; 90 91 index = find_first_zero_bit(bitmap, pm8001_ha->tags_num); 92 tag = index; 93 if (tag >= pm8001_ha->tags_num) 94 return -SAS_QUEUE_FULL; 95 pm8001_tag_set(pm8001_ha, tag); 96 *tag_out = tag; 97 return 0; 98 } 99 100 void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha) 101 { 102 int i; 103 for (i = 0; i < pm8001_ha->tags_num; ++i) 104 pm8001_tag_clear(pm8001_ha, i); 105 } 106 107 /** 108 * pm8001_mem_alloc - allocate memory for pm8001. 109 * @pdev: pci device. 110 * @virt_addr: the allocated virtual address 111 * @pphys_addr_hi: the physical address high byte address. 112 * @pphys_addr_lo: the physical address low byte address. 113 * @mem_size: memory size. 114 */ 115 int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr, 116 dma_addr_t *pphys_addr, u32 *pphys_addr_hi, 117 u32 *pphys_addr_lo, u32 mem_size, u32 align) 118 { 119 caddr_t mem_virt_alloc; 120 dma_addr_t mem_dma_handle; 121 u64 phys_align; 122 u64 align_offset = 0; 123 if (align) 124 align_offset = (dma_addr_t)align - 1; 125 mem_virt_alloc = 126 pci_alloc_consistent(pdev, mem_size + align, &mem_dma_handle); 127 if (!mem_virt_alloc) { 128 pm8001_printk("memory allocation error\n"); 129 return -1; 130 } 131 memset((void *)mem_virt_alloc, 0, mem_size+align); 132 *pphys_addr = mem_dma_handle; 133 phys_align = (*pphys_addr + align_offset) & ~align_offset; 134 *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr; 135 *pphys_addr_hi = upper_32_bits(phys_align); 136 *pphys_addr_lo = lower_32_bits(phys_align); 137 return 0; 138 } 139 /** 140 * pm8001_find_ha_by_dev - from domain device which come from sas layer to 141 * find out our hba struct. 142 * @dev: the domain device which from sas layer. 143 */ 144 static 145 struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev) 146 { 147 struct sas_ha_struct *sha = dev->port->ha; 148 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha; 149 return pm8001_ha; 150 } 151 152 /** 153 * pm8001_phy_control - this function should be registered to 154 * sas_domain_function_template to provide libsas used, note: this is just 155 * control the HBA phy rather than other expander phy if you want control 156 * other phy, you should use SMP command. 157 * @sas_phy: which phy in HBA phys. 158 * @func: the operation. 159 * @funcdata: always NULL. 160 */ 161 int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func, 162 void *funcdata) 163 { 164 int rc = 0, phy_id = sas_phy->id; 165 struct pm8001_hba_info *pm8001_ha = NULL; 166 struct sas_phy_linkrates *rates; 167 DECLARE_COMPLETION_ONSTACK(completion); 168 pm8001_ha = sas_phy->ha->lldd_ha; 169 pm8001_ha->phy[phy_id].enable_completion = &completion; 170 switch (func) { 171 case PHY_FUNC_SET_LINK_RATE: 172 rates = funcdata; 173 if (rates->minimum_linkrate) { 174 pm8001_ha->phy[phy_id].minimum_linkrate = 175 rates->minimum_linkrate; 176 } 177 if (rates->maximum_linkrate) { 178 pm8001_ha->phy[phy_id].maximum_linkrate = 179 rates->maximum_linkrate; 180 } 181 if (pm8001_ha->phy[phy_id].phy_state == 0) { 182 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id); 183 wait_for_completion(&completion); 184 } 185 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, 186 PHY_LINK_RESET); 187 break; 188 case PHY_FUNC_HARD_RESET: 189 if (pm8001_ha->phy[phy_id].phy_state == 0) { 190 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id); 191 wait_for_completion(&completion); 192 } 193 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, 194 PHY_HARD_RESET); 195 break; 196 case PHY_FUNC_LINK_RESET: 197 if (pm8001_ha->phy[phy_id].phy_state == 0) { 198 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id); 199 wait_for_completion(&completion); 200 } 201 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, 202 PHY_LINK_RESET); 203 break; 204 case PHY_FUNC_RELEASE_SPINUP_HOLD: 205 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, 206 PHY_LINK_RESET); 207 break; 208 case PHY_FUNC_DISABLE: 209 PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id); 210 break; 211 default: 212 rc = -EOPNOTSUPP; 213 } 214 msleep(300); 215 return rc; 216 } 217 218 int pm8001_slave_alloc(struct scsi_device *scsi_dev) 219 { 220 struct domain_device *dev = sdev_to_domain_dev(scsi_dev); 221 if (dev_is_sata(dev)) { 222 /* We don't need to rescan targets 223 * if REPORT_LUNS request is failed 224 */ 225 if (scsi_dev->lun > 0) 226 return -ENXIO; 227 scsi_dev->tagged_supported = 1; 228 } 229 return sas_slave_alloc(scsi_dev); 230 } 231 232 /** 233 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start 234 * command to HBA. 235 * @shost: the scsi host data. 236 */ 237 void pm8001_scan_start(struct Scsi_Host *shost) 238 { 239 int i; 240 struct pm8001_hba_info *pm8001_ha; 241 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost); 242 pm8001_ha = sha->lldd_ha; 243 PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha); 244 for (i = 0; i < pm8001_ha->chip->n_phy; ++i) 245 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i); 246 } 247 248 int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time) 249 { 250 /* give the phy enabling interrupt event time to come in (1s 251 * is empirically about all it takes) */ 252 if (time < HZ) 253 return 0; 254 /* Wait for discovery to finish */ 255 scsi_flush_work(shost); 256 return 1; 257 } 258 259 /** 260 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task 261 * @pm8001_ha: our hba card information 262 * @ccb: the ccb which attached to smp task 263 */ 264 static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha, 265 struct pm8001_ccb_info *ccb) 266 { 267 return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb); 268 } 269 270 u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag) 271 { 272 struct ata_queued_cmd *qc = task->uldd_task; 273 if (qc) { 274 if (qc->tf.command == ATA_CMD_FPDMA_WRITE || 275 qc->tf.command == ATA_CMD_FPDMA_READ) { 276 *tag = qc->tag; 277 return 1; 278 } 279 } 280 return 0; 281 } 282 283 /** 284 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task 285 * @pm8001_ha: our hba card information 286 * @ccb: the ccb which attached to sata task 287 */ 288 static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha, 289 struct pm8001_ccb_info *ccb) 290 { 291 return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb); 292 } 293 294 /** 295 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data 296 * @pm8001_ha: our hba card information 297 * @ccb: the ccb which attached to TM 298 * @tmf: the task management IU 299 */ 300 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha, 301 struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf) 302 { 303 return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf); 304 } 305 306 /** 307 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task 308 * @pm8001_ha: our hba card information 309 * @ccb: the ccb which attached to ssp task 310 */ 311 static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha, 312 struct pm8001_ccb_info *ccb) 313 { 314 return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb); 315 } 316 int pm8001_slave_configure(struct scsi_device *sdev) 317 { 318 struct domain_device *dev = sdev_to_domain_dev(sdev); 319 int ret = sas_slave_configure(sdev); 320 if (ret) 321 return ret; 322 if (dev_is_sata(dev)) { 323 #ifdef PM8001_DISABLE_NCQ 324 struct ata_port *ap = dev->sata_dev.ap; 325 struct ata_device *adev = ap->link.device; 326 adev->flags |= ATA_DFLAG_NCQ_OFF; 327 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, 1); 328 #endif 329 } 330 return 0; 331 } 332 /* Find the local port id that's attached to this device */ 333 static int sas_find_local_port_id(struct domain_device *dev) 334 { 335 struct domain_device *pdev = dev->parent; 336 337 /* Directly attached device */ 338 if (!pdev) 339 return dev->port->id; 340 while (pdev) { 341 struct domain_device *pdev_p = pdev->parent; 342 if (!pdev_p) 343 return pdev->port->id; 344 pdev = pdev->parent; 345 } 346 return 0; 347 } 348 349 /** 350 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware. 351 * @task: the task to be execute. 352 * @num: if can_queue great than 1, the task can be queued up. for SMP task, 353 * we always execute one one time. 354 * @gfp_flags: gfp_flags. 355 * @is_tmf: if it is task management task. 356 * @tmf: the task management IU 357 */ 358 #define DEV_IS_GONE(pm8001_dev) \ 359 ((!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE))) 360 static int pm8001_task_exec(struct sas_task *task, const int num, 361 gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf) 362 { 363 struct domain_device *dev = task->dev; 364 struct pm8001_hba_info *pm8001_ha; 365 struct pm8001_device *pm8001_dev; 366 struct pm8001_port *port = NULL; 367 struct sas_task *t = task; 368 struct pm8001_ccb_info *ccb; 369 u32 tag = 0xdeadbeef, rc, n_elem = 0; 370 u32 n = num; 371 unsigned long flags = 0, flags_libsas = 0; 372 373 if (!dev->port) { 374 struct task_status_struct *tsm = &t->task_status; 375 tsm->resp = SAS_TASK_UNDELIVERED; 376 tsm->stat = SAS_PHY_DOWN; 377 if (dev->dev_type != SATA_DEV) 378 t->task_done(t); 379 return 0; 380 } 381 pm8001_ha = pm8001_find_ha_by_dev(task->dev); 382 PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n ")); 383 spin_lock_irqsave(&pm8001_ha->lock, flags); 384 do { 385 dev = t->dev; 386 pm8001_dev = dev->lldd_dev; 387 if (DEV_IS_GONE(pm8001_dev)) { 388 if (pm8001_dev) { 389 PM8001_IO_DBG(pm8001_ha, 390 pm8001_printk("device %d not ready.\n", 391 pm8001_dev->device_id)); 392 } else { 393 PM8001_IO_DBG(pm8001_ha, 394 pm8001_printk("device %016llx not " 395 "ready.\n", SAS_ADDR(dev->sas_addr))); 396 } 397 rc = SAS_PHY_DOWN; 398 goto out_done; 399 } 400 port = &pm8001_ha->port[sas_find_local_port_id(dev)]; 401 if (!port->port_attached) { 402 if (sas_protocol_ata(t->task_proto)) { 403 struct task_status_struct *ts = &t->task_status; 404 ts->resp = SAS_TASK_UNDELIVERED; 405 ts->stat = SAS_PHY_DOWN; 406 407 spin_unlock_irqrestore(&pm8001_ha->lock, flags); 408 spin_unlock_irqrestore(dev->sata_dev.ap->lock, 409 flags_libsas); 410 t->task_done(t); 411 spin_lock_irqsave(dev->sata_dev.ap->lock, 412 flags_libsas); 413 spin_lock_irqsave(&pm8001_ha->lock, flags); 414 if (n > 1) 415 t = list_entry(t->list.next, 416 struct sas_task, list); 417 continue; 418 } else { 419 struct task_status_struct *ts = &t->task_status; 420 ts->resp = SAS_TASK_UNDELIVERED; 421 ts->stat = SAS_PHY_DOWN; 422 t->task_done(t); 423 if (n > 1) 424 t = list_entry(t->list.next, 425 struct sas_task, list); 426 continue; 427 } 428 } 429 rc = pm8001_tag_alloc(pm8001_ha, &tag); 430 if (rc) 431 goto err_out; 432 ccb = &pm8001_ha->ccb_info[tag]; 433 434 if (!sas_protocol_ata(t->task_proto)) { 435 if (t->num_scatter) { 436 n_elem = dma_map_sg(pm8001_ha->dev, 437 t->scatter, 438 t->num_scatter, 439 t->data_dir); 440 if (!n_elem) { 441 rc = -ENOMEM; 442 goto err_out_tag; 443 } 444 } 445 } else { 446 n_elem = t->num_scatter; 447 } 448 449 t->lldd_task = ccb; 450 ccb->n_elem = n_elem; 451 ccb->ccb_tag = tag; 452 ccb->task = t; 453 switch (t->task_proto) { 454 case SAS_PROTOCOL_SMP: 455 rc = pm8001_task_prep_smp(pm8001_ha, ccb); 456 break; 457 case SAS_PROTOCOL_SSP: 458 if (is_tmf) 459 rc = pm8001_task_prep_ssp_tm(pm8001_ha, 460 ccb, tmf); 461 else 462 rc = pm8001_task_prep_ssp(pm8001_ha, ccb); 463 break; 464 case SAS_PROTOCOL_SATA: 465 case SAS_PROTOCOL_STP: 466 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: 467 rc = pm8001_task_prep_ata(pm8001_ha, ccb); 468 break; 469 default: 470 dev_printk(KERN_ERR, pm8001_ha->dev, 471 "unknown sas_task proto: 0x%x\n", 472 t->task_proto); 473 rc = -EINVAL; 474 break; 475 } 476 477 if (rc) { 478 PM8001_IO_DBG(pm8001_ha, 479 pm8001_printk("rc is %x\n", rc)); 480 goto err_out_tag; 481 } 482 /* TODO: select normal or high priority */ 483 spin_lock(&t->task_state_lock); 484 t->task_state_flags |= SAS_TASK_AT_INITIATOR; 485 spin_unlock(&t->task_state_lock); 486 pm8001_dev->running_req++; 487 if (n > 1) 488 t = list_entry(t->list.next, struct sas_task, list); 489 } while (--n); 490 rc = 0; 491 goto out_done; 492 493 err_out_tag: 494 pm8001_tag_free(pm8001_ha, tag); 495 err_out: 496 dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc); 497 if (!sas_protocol_ata(t->task_proto)) 498 if (n_elem) 499 dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem, 500 t->data_dir); 501 out_done: 502 spin_unlock_irqrestore(&pm8001_ha->lock, flags); 503 return rc; 504 } 505 506 /** 507 * pm8001_queue_command - register for upper layer used, all IO commands sent 508 * to HBA are from this interface. 509 * @task: the task to be execute. 510 * @num: if can_queue great than 1, the task can be queued up. for SMP task, 511 * we always execute one one time 512 * @gfp_flags: gfp_flags 513 */ 514 int pm8001_queue_command(struct sas_task *task, const int num, 515 gfp_t gfp_flags) 516 { 517 return pm8001_task_exec(task, num, gfp_flags, 0, NULL); 518 } 519 520 void pm8001_ccb_free(struct pm8001_hba_info *pm8001_ha, u32 ccb_idx) 521 { 522 pm8001_tag_clear(pm8001_ha, ccb_idx); 523 } 524 525 /** 526 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb. 527 * @pm8001_ha: our hba card information 528 * @ccb: the ccb which attached to ssp task 529 * @task: the task to be free. 530 * @ccb_idx: ccb index. 531 */ 532 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha, 533 struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx) 534 { 535 if (!ccb->task) 536 return; 537 if (!sas_protocol_ata(task->task_proto)) 538 if (ccb->n_elem) 539 dma_unmap_sg(pm8001_ha->dev, task->scatter, 540 task->num_scatter, task->data_dir); 541 542 switch (task->task_proto) { 543 case SAS_PROTOCOL_SMP: 544 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1, 545 PCI_DMA_FROMDEVICE); 546 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1, 547 PCI_DMA_TODEVICE); 548 break; 549 550 case SAS_PROTOCOL_SATA: 551 case SAS_PROTOCOL_STP: 552 case SAS_PROTOCOL_SSP: 553 default: 554 /* do nothing */ 555 break; 556 } 557 task->lldd_task = NULL; 558 ccb->task = NULL; 559 ccb->ccb_tag = 0xFFFFFFFF; 560 pm8001_ccb_free(pm8001_ha, ccb_idx); 561 } 562 563 /** 564 * pm8001_alloc_dev - find a empty pm8001_device 565 * @pm8001_ha: our hba card information 566 */ 567 struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha) 568 { 569 u32 dev; 570 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) { 571 if (pm8001_ha->devices[dev].dev_type == NO_DEVICE) { 572 pm8001_ha->devices[dev].id = dev; 573 return &pm8001_ha->devices[dev]; 574 } 575 } 576 if (dev == PM8001_MAX_DEVICES) { 577 PM8001_FAIL_DBG(pm8001_ha, 578 pm8001_printk("max support %d devices, ignore ..\n", 579 PM8001_MAX_DEVICES)); 580 } 581 return NULL; 582 } 583 584 static void pm8001_free_dev(struct pm8001_device *pm8001_dev) 585 { 586 u32 id = pm8001_dev->id; 587 memset(pm8001_dev, 0, sizeof(*pm8001_dev)); 588 pm8001_dev->id = id; 589 pm8001_dev->dev_type = NO_DEVICE; 590 pm8001_dev->device_id = PM8001_MAX_DEVICES; 591 pm8001_dev->sas_device = NULL; 592 } 593 594 /** 595 * pm8001_dev_found_notify - libsas notify a device is found. 596 * @dev: the device structure which sas layer used. 597 * 598 * when libsas find a sas domain device, it should tell the LLDD that 599 * device is found, and then LLDD register this device to HBA firmware 600 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a 601 * device ID(according to device's sas address) and returned it to LLDD. From 602 * now on, we communicate with HBA FW with the device ID which HBA assigned 603 * rather than sas address. it is the neccessary step for our HBA but it is 604 * the optional for other HBA driver. 605 */ 606 static int pm8001_dev_found_notify(struct domain_device *dev) 607 { 608 unsigned long flags = 0; 609 int res = 0; 610 struct pm8001_hba_info *pm8001_ha = NULL; 611 struct domain_device *parent_dev = dev->parent; 612 struct pm8001_device *pm8001_device; 613 DECLARE_COMPLETION_ONSTACK(completion); 614 u32 flag = 0; 615 pm8001_ha = pm8001_find_ha_by_dev(dev); 616 spin_lock_irqsave(&pm8001_ha->lock, flags); 617 618 pm8001_device = pm8001_alloc_dev(pm8001_ha); 619 if (!pm8001_device) { 620 res = -1; 621 goto found_out; 622 } 623 pm8001_device->sas_device = dev; 624 dev->lldd_dev = pm8001_device; 625 pm8001_device->dev_type = dev->dev_type; 626 pm8001_device->dcompletion = &completion; 627 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) { 628 int phy_id; 629 struct ex_phy *phy; 630 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys; 631 phy_id++) { 632 phy = &parent_dev->ex_dev.ex_phy[phy_id]; 633 if (SAS_ADDR(phy->attached_sas_addr) 634 == SAS_ADDR(dev->sas_addr)) { 635 pm8001_device->attached_phy = phy_id; 636 break; 637 } 638 } 639 if (phy_id == parent_dev->ex_dev.num_phys) { 640 PM8001_FAIL_DBG(pm8001_ha, 641 pm8001_printk("Error: no attached dev:%016llx" 642 " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr), 643 SAS_ADDR(parent_dev->sas_addr))); 644 res = -1; 645 } 646 } else { 647 if (dev->dev_type == SATA_DEV) { 648 pm8001_device->attached_phy = 649 dev->rphy->identify.phy_identifier; 650 flag = 1; /* directly sata*/ 651 } 652 } /*register this device to HBA*/ 653 PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device \n")); 654 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag); 655 spin_unlock_irqrestore(&pm8001_ha->lock, flags); 656 wait_for_completion(&completion); 657 if (dev->dev_type == SAS_END_DEV) 658 msleep(50); 659 pm8001_ha->flags |= PM8001F_RUN_TIME ; 660 return 0; 661 found_out: 662 spin_unlock_irqrestore(&pm8001_ha->lock, flags); 663 return res; 664 } 665 666 int pm8001_dev_found(struct domain_device *dev) 667 { 668 return pm8001_dev_found_notify(dev); 669 } 670 671 /** 672 * pm8001_alloc_task - allocate a task structure for TMF 673 */ 674 static struct sas_task *pm8001_alloc_task(void) 675 { 676 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL); 677 if (task) { 678 INIT_LIST_HEAD(&task->list); 679 spin_lock_init(&task->task_state_lock); 680 task->task_state_flags = SAS_TASK_STATE_PENDING; 681 init_timer(&task->timer); 682 init_completion(&task->completion); 683 } 684 return task; 685 } 686 687 static void pm8001_free_task(struct sas_task *task) 688 { 689 if (task) { 690 BUG_ON(!list_empty(&task->list)); 691 kfree(task); 692 } 693 } 694 695 static void pm8001_task_done(struct sas_task *task) 696 { 697 if (!del_timer(&task->timer)) 698 return; 699 complete(&task->completion); 700 } 701 702 static void pm8001_tmf_timedout(unsigned long data) 703 { 704 struct sas_task *task = (struct sas_task *)data; 705 706 task->task_state_flags |= SAS_TASK_STATE_ABORTED; 707 complete(&task->completion); 708 } 709 710 #define PM8001_TASK_TIMEOUT 20 711 /** 712 * pm8001_exec_internal_tmf_task - execute some task management commands. 713 * @dev: the wanted device. 714 * @tmf: which task management wanted to be take. 715 * @para_len: para_len. 716 * @parameter: ssp task parameter. 717 * 718 * when errors or exception happened, we may want to do something, for example 719 * abort the issued task which result in this execption, it is done by calling 720 * this function, note it is also with the task execute interface. 721 */ 722 static int pm8001_exec_internal_tmf_task(struct domain_device *dev, 723 void *parameter, u32 para_len, struct pm8001_tmf_task *tmf) 724 { 725 int res, retry; 726 struct sas_task *task = NULL; 727 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev); 728 729 for (retry = 0; retry < 3; retry++) { 730 task = pm8001_alloc_task(); 731 if (!task) 732 return -ENOMEM; 733 734 task->dev = dev; 735 task->task_proto = dev->tproto; 736 memcpy(&task->ssp_task, parameter, para_len); 737 task->task_done = pm8001_task_done; 738 task->timer.data = (unsigned long)task; 739 task->timer.function = pm8001_tmf_timedout; 740 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ; 741 add_timer(&task->timer); 742 743 res = pm8001_task_exec(task, 1, GFP_KERNEL, 1, tmf); 744 745 if (res) { 746 del_timer(&task->timer); 747 PM8001_FAIL_DBG(pm8001_ha, 748 pm8001_printk("Executing internal task " 749 "failed\n")); 750 goto ex_err; 751 } 752 wait_for_completion(&task->completion); 753 res = -TMF_RESP_FUNC_FAILED; 754 /* Even TMF timed out, return direct. */ 755 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { 756 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { 757 PM8001_FAIL_DBG(pm8001_ha, 758 pm8001_printk("TMF task[%x]timeout.\n", 759 tmf->tmf)); 760 goto ex_err; 761 } 762 } 763 764 if (task->task_status.resp == SAS_TASK_COMPLETE && 765 task->task_status.stat == SAM_GOOD) { 766 res = TMF_RESP_FUNC_COMPLETE; 767 break; 768 } 769 770 if (task->task_status.resp == SAS_TASK_COMPLETE && 771 task->task_status.stat == SAS_DATA_UNDERRUN) { 772 /* no error, but return the number of bytes of 773 * underrun */ 774 res = task->task_status.residual; 775 break; 776 } 777 778 if (task->task_status.resp == SAS_TASK_COMPLETE && 779 task->task_status.stat == SAS_DATA_OVERRUN) { 780 PM8001_FAIL_DBG(pm8001_ha, 781 pm8001_printk("Blocked task error.\n")); 782 res = -EMSGSIZE; 783 break; 784 } else { 785 PM8001_EH_DBG(pm8001_ha, 786 pm8001_printk(" Task to dev %016llx response:" 787 "0x%x status 0x%x\n", 788 SAS_ADDR(dev->sas_addr), 789 task->task_status.resp, 790 task->task_status.stat)); 791 pm8001_free_task(task); 792 task = NULL; 793 } 794 } 795 ex_err: 796 BUG_ON(retry == 3 && task != NULL); 797 if (task != NULL) 798 pm8001_free_task(task); 799 return res; 800 } 801 802 static int 803 pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha, 804 struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag, 805 u32 task_tag) 806 { 807 int res, retry; 808 u32 ccb_tag; 809 struct pm8001_ccb_info *ccb; 810 struct sas_task *task = NULL; 811 812 for (retry = 0; retry < 3; retry++) { 813 task = pm8001_alloc_task(); 814 if (!task) 815 return -ENOMEM; 816 817 task->dev = dev; 818 task->task_proto = dev->tproto; 819 task->task_done = pm8001_task_done; 820 task->timer.data = (unsigned long)task; 821 task->timer.function = pm8001_tmf_timedout; 822 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ; 823 add_timer(&task->timer); 824 825 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag); 826 if (res) 827 return res; 828 ccb = &pm8001_ha->ccb_info[ccb_tag]; 829 ccb->device = pm8001_dev; 830 ccb->ccb_tag = ccb_tag; 831 ccb->task = task; 832 833 res = PM8001_CHIP_DISP->task_abort(pm8001_ha, 834 pm8001_dev, flag, task_tag, ccb_tag); 835 836 if (res) { 837 del_timer(&task->timer); 838 PM8001_FAIL_DBG(pm8001_ha, 839 pm8001_printk("Executing internal task " 840 "failed\n")); 841 goto ex_err; 842 } 843 wait_for_completion(&task->completion); 844 res = TMF_RESP_FUNC_FAILED; 845 /* Even TMF timed out, return direct. */ 846 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { 847 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { 848 PM8001_FAIL_DBG(pm8001_ha, 849 pm8001_printk("TMF task timeout.\n")); 850 goto ex_err; 851 } 852 } 853 854 if (task->task_status.resp == SAS_TASK_COMPLETE && 855 task->task_status.stat == SAM_GOOD) { 856 res = TMF_RESP_FUNC_COMPLETE; 857 break; 858 859 } else { 860 PM8001_EH_DBG(pm8001_ha, 861 pm8001_printk(" Task to dev %016llx response: " 862 "0x%x status 0x%x\n", 863 SAS_ADDR(dev->sas_addr), 864 task->task_status.resp, 865 task->task_status.stat)); 866 pm8001_free_task(task); 867 task = NULL; 868 } 869 } 870 ex_err: 871 BUG_ON(retry == 3 && task != NULL); 872 if (task != NULL) 873 pm8001_free_task(task); 874 return res; 875 } 876 877 /** 878 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify" 879 * @dev: the device structure which sas layer used. 880 */ 881 static void pm8001_dev_gone_notify(struct domain_device *dev) 882 { 883 unsigned long flags = 0; 884 u32 tag; 885 struct pm8001_hba_info *pm8001_ha; 886 struct pm8001_device *pm8001_dev = dev->lldd_dev; 887 u32 device_id = pm8001_dev->device_id; 888 pm8001_ha = pm8001_find_ha_by_dev(dev); 889 spin_lock_irqsave(&pm8001_ha->lock, flags); 890 pm8001_tag_alloc(pm8001_ha, &tag); 891 if (pm8001_dev) { 892 PM8001_DISC_DBG(pm8001_ha, 893 pm8001_printk("found dev[%d:%x] is gone.\n", 894 pm8001_dev->device_id, pm8001_dev->dev_type)); 895 if (pm8001_dev->running_req) { 896 spin_unlock_irqrestore(&pm8001_ha->lock, flags); 897 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , 898 dev, 1, 0); 899 spin_lock_irqsave(&pm8001_ha->lock, flags); 900 } 901 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id); 902 pm8001_free_dev(pm8001_dev); 903 } else { 904 PM8001_DISC_DBG(pm8001_ha, 905 pm8001_printk("Found dev has gone.\n")); 906 } 907 dev->lldd_dev = NULL; 908 spin_unlock_irqrestore(&pm8001_ha->lock, flags); 909 } 910 911 void pm8001_dev_gone(struct domain_device *dev) 912 { 913 pm8001_dev_gone_notify(dev); 914 } 915 916 static int pm8001_issue_ssp_tmf(struct domain_device *dev, 917 u8 *lun, struct pm8001_tmf_task *tmf) 918 { 919 struct sas_ssp_task ssp_task; 920 if (!(dev->tproto & SAS_PROTOCOL_SSP)) 921 return TMF_RESP_FUNC_ESUPP; 922 923 strncpy((u8 *)&ssp_task.LUN, lun, 8); 924 return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task), 925 tmf); 926 } 927 928 /** 929 * Standard mandates link reset for ATA (type 0) and hard reset for 930 * SSP (type 1) , only for RECOVERY 931 */ 932 int pm8001_I_T_nexus_reset(struct domain_device *dev) 933 { 934 int rc = TMF_RESP_FUNC_FAILED; 935 struct pm8001_device *pm8001_dev; 936 struct pm8001_hba_info *pm8001_ha; 937 struct sas_phy *phy; 938 if (!dev || !dev->lldd_dev) 939 return -1; 940 941 pm8001_dev = dev->lldd_dev; 942 pm8001_ha = pm8001_find_ha_by_dev(dev); 943 phy = sas_find_local_phy(dev); 944 945 if (dev_is_sata(dev)) { 946 DECLARE_COMPLETION_ONSTACK(completion_setstate); 947 if (scsi_is_sas_phy_local(phy)) 948 return 0; 949 rc = sas_phy_reset(phy, 1); 950 msleep(2000); 951 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , 952 dev, 1, 0); 953 pm8001_dev->setds_completion = &completion_setstate; 954 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha, 955 pm8001_dev, 0x01); 956 wait_for_completion(&completion_setstate); 957 } else{ 958 rc = sas_phy_reset(phy, 1); 959 msleep(2000); 960 } 961 PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n", 962 pm8001_dev->device_id, rc)); 963 return rc; 964 } 965 966 /* mandatory SAM-3, the task reset the specified LUN*/ 967 int pm8001_lu_reset(struct domain_device *dev, u8 *lun) 968 { 969 int rc = TMF_RESP_FUNC_FAILED; 970 struct pm8001_tmf_task tmf_task; 971 struct pm8001_device *pm8001_dev = dev->lldd_dev; 972 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev); 973 if (dev_is_sata(dev)) { 974 struct sas_phy *phy = sas_find_local_phy(dev); 975 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , 976 dev, 1, 0); 977 rc = sas_phy_reset(phy, 1); 978 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha, 979 pm8001_dev, 0x01); 980 msleep(2000); 981 } else { 982 tmf_task.tmf = TMF_LU_RESET; 983 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); 984 } 985 /* If failed, fall-through I_T_Nexus reset */ 986 PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n", 987 pm8001_dev->device_id, rc)); 988 return rc; 989 } 990 991 /* optional SAM-3 */ 992 int pm8001_query_task(struct sas_task *task) 993 { 994 u32 tag = 0xdeadbeef; 995 int i = 0; 996 struct scsi_lun lun; 997 struct pm8001_tmf_task tmf_task; 998 int rc = TMF_RESP_FUNC_FAILED; 999 if (unlikely(!task || !task->lldd_task || !task->dev)) 1000 return rc; 1001 1002 if (task->task_proto & SAS_PROTOCOL_SSP) { 1003 struct scsi_cmnd *cmnd = task->uldd_task; 1004 struct domain_device *dev = task->dev; 1005 struct pm8001_hba_info *pm8001_ha = 1006 pm8001_find_ha_by_dev(dev); 1007 1008 int_to_scsilun(cmnd->device->lun, &lun); 1009 rc = pm8001_find_tag(task, &tag); 1010 if (rc == 0) { 1011 rc = TMF_RESP_FUNC_FAILED; 1012 return rc; 1013 } 1014 PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:[")); 1015 for (i = 0; i < 16; i++) 1016 printk(KERN_INFO "%02x ", cmnd->cmnd[i]); 1017 printk(KERN_INFO "]\n"); 1018 tmf_task.tmf = TMF_QUERY_TASK; 1019 tmf_task.tag_of_task_to_be_managed = tag; 1020 1021 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task); 1022 switch (rc) { 1023 /* The task is still in Lun, release it then */ 1024 case TMF_RESP_FUNC_SUCC: 1025 PM8001_EH_DBG(pm8001_ha, 1026 pm8001_printk("The task is still in Lun \n")); 1027 /* The task is not in Lun or failed, reset the phy */ 1028 case TMF_RESP_FUNC_FAILED: 1029 case TMF_RESP_FUNC_COMPLETE: 1030 PM8001_EH_DBG(pm8001_ha, 1031 pm8001_printk("The task is not in Lun or failed," 1032 " reset the phy \n")); 1033 break; 1034 } 1035 } 1036 pm8001_printk(":rc= %d\n", rc); 1037 return rc; 1038 } 1039 1040 /* mandatory SAM-3, still need free task/ccb info, abord the specified task */ 1041 int pm8001_abort_task(struct sas_task *task) 1042 { 1043 unsigned long flags; 1044 u32 tag = 0xdeadbeef; 1045 u32 device_id; 1046 struct domain_device *dev ; 1047 struct pm8001_hba_info *pm8001_ha = NULL; 1048 struct pm8001_ccb_info *ccb; 1049 struct scsi_lun lun; 1050 struct pm8001_device *pm8001_dev; 1051 struct pm8001_tmf_task tmf_task; 1052 int rc = TMF_RESP_FUNC_FAILED; 1053 if (unlikely(!task || !task->lldd_task || !task->dev)) 1054 return rc; 1055 spin_lock_irqsave(&task->task_state_lock, flags); 1056 if (task->task_state_flags & SAS_TASK_STATE_DONE) { 1057 spin_unlock_irqrestore(&task->task_state_lock, flags); 1058 rc = TMF_RESP_FUNC_COMPLETE; 1059 goto out; 1060 } 1061 spin_unlock_irqrestore(&task->task_state_lock, flags); 1062 if (task->task_proto & SAS_PROTOCOL_SSP) { 1063 struct scsi_cmnd *cmnd = task->uldd_task; 1064 dev = task->dev; 1065 ccb = task->lldd_task; 1066 pm8001_dev = dev->lldd_dev; 1067 pm8001_ha = pm8001_find_ha_by_dev(dev); 1068 int_to_scsilun(cmnd->device->lun, &lun); 1069 rc = pm8001_find_tag(task, &tag); 1070 if (rc == 0) { 1071 printk(KERN_INFO "No such tag in %s\n", __func__); 1072 rc = TMF_RESP_FUNC_FAILED; 1073 return rc; 1074 } 1075 device_id = pm8001_dev->device_id; 1076 PM8001_EH_DBG(pm8001_ha, 1077 pm8001_printk("abort io to deviceid= %d\n", device_id)); 1078 tmf_task.tmf = TMF_ABORT_TASK; 1079 tmf_task.tag_of_task_to_be_managed = tag; 1080 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task); 1081 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev, 1082 pm8001_dev->sas_device, 0, tag); 1083 } else if (task->task_proto & SAS_PROTOCOL_SATA || 1084 task->task_proto & SAS_PROTOCOL_STP) { 1085 dev = task->dev; 1086 pm8001_dev = dev->lldd_dev; 1087 pm8001_ha = pm8001_find_ha_by_dev(dev); 1088 rc = pm8001_find_tag(task, &tag); 1089 if (rc == 0) { 1090 printk(KERN_INFO "No such tag in %s\n", __func__); 1091 rc = TMF_RESP_FUNC_FAILED; 1092 return rc; 1093 } 1094 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev, 1095 pm8001_dev->sas_device, 0, tag); 1096 } else if (task->task_proto & SAS_PROTOCOL_SMP) { 1097 /* SMP */ 1098 dev = task->dev; 1099 pm8001_dev = dev->lldd_dev; 1100 pm8001_ha = pm8001_find_ha_by_dev(dev); 1101 rc = pm8001_find_tag(task, &tag); 1102 if (rc == 0) { 1103 printk(KERN_INFO "No such tag in %s\n", __func__); 1104 rc = TMF_RESP_FUNC_FAILED; 1105 return rc; 1106 } 1107 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev, 1108 pm8001_dev->sas_device, 0, tag); 1109 1110 } 1111 out: 1112 if (rc != TMF_RESP_FUNC_COMPLETE) 1113 pm8001_printk("rc= %d\n", rc); 1114 return rc; 1115 } 1116 1117 int pm8001_abort_task_set(struct domain_device *dev, u8 *lun) 1118 { 1119 int rc = TMF_RESP_FUNC_FAILED; 1120 struct pm8001_tmf_task tmf_task; 1121 1122 tmf_task.tmf = TMF_ABORT_TASK_SET; 1123 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); 1124 return rc; 1125 } 1126 1127 int pm8001_clear_aca(struct domain_device *dev, u8 *lun) 1128 { 1129 int rc = TMF_RESP_FUNC_FAILED; 1130 struct pm8001_tmf_task tmf_task; 1131 1132 tmf_task.tmf = TMF_CLEAR_ACA; 1133 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); 1134 1135 return rc; 1136 } 1137 1138 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun) 1139 { 1140 int rc = TMF_RESP_FUNC_FAILED; 1141 struct pm8001_tmf_task tmf_task; 1142 struct pm8001_device *pm8001_dev = dev->lldd_dev; 1143 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev); 1144 1145 PM8001_EH_DBG(pm8001_ha, 1146 pm8001_printk("I_T_L_Q clear task set[%x]\n", 1147 pm8001_dev->device_id)); 1148 tmf_task.tmf = TMF_CLEAR_TASK_SET; 1149 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); 1150 return rc; 1151 } 1152 1153