2 * PMC-Sierra PM8001/8081/8088/8089 SAS/SATA based host adapters driver
4 * Copyright (c) 2008-2009 USI Co., Ltd.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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.
41 #include <linux/slab.h>
42 #include "pm8001_sas.h"
45 * pm8001_find_tag - from sas task to find out tag that belongs to this task
46 * @task: the task sent to the LLDD
47 * @tag: the found tag associated with the task
49 static int pm8001_find_tag(struct sas_task *task, u32 *tag)
51 if (task->lldd_task) {
52 struct pm8001_ccb_info *ccb;
53 ccb = task->lldd_task;
61 * pm8001_tag_free - free the no more needed tag
62 * @pm8001_ha: our hba struct
63 * @tag: the found tag associated with the task
65 void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
67 void *bitmap = pm8001_ha->tags;
68 clear_bit(tag, bitmap);
72 * pm8001_tag_alloc - allocate a empty tag for task used.
73 * @pm8001_ha: our hba struct
74 * @tag_out: the found empty tag .
76 inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
79 void *bitmap = pm8001_ha->tags;
82 spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags);
83 tag = find_first_zero_bit(bitmap, pm8001_ha->tags_num);
84 if (tag >= pm8001_ha->tags_num) {
85 spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
86 return -SAS_QUEUE_FULL;
89 spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
94 void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha)
97 for (i = 0; i < pm8001_ha->tags_num; ++i)
98 pm8001_tag_free(pm8001_ha, i);
102 * pm8001_mem_alloc - allocate memory for pm8001.
104 * @virt_addr: the allocated virtual address
105 * @pphys_addr_hi: the physical address high byte address.
106 * @pphys_addr_lo: the physical address low byte address.
107 * @mem_size: memory size.
109 int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr,
110 dma_addr_t *pphys_addr, u32 *pphys_addr_hi,
111 u32 *pphys_addr_lo, u32 mem_size, u32 align)
113 caddr_t mem_virt_alloc;
114 dma_addr_t mem_dma_handle;
116 u64 align_offset = 0;
118 align_offset = (dma_addr_t)align - 1;
120 pci_alloc_consistent(pdev, mem_size + align, &mem_dma_handle);
121 if (!mem_virt_alloc) {
122 pm8001_printk("memory allocation error\n");
125 memset((void *)mem_virt_alloc, 0, mem_size+align);
126 *pphys_addr = mem_dma_handle;
127 phys_align = (*pphys_addr + align_offset) & ~align_offset;
128 *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
129 *pphys_addr_hi = upper_32_bits(phys_align);
130 *pphys_addr_lo = lower_32_bits(phys_align);
134 * pm8001_find_ha_by_dev - from domain device which come from sas layer to
135 * find out our hba struct.
136 * @dev: the domain device which from sas layer.
139 struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
141 struct sas_ha_struct *sha = dev->port->ha;
142 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
147 * pm8001_phy_control - this function should be registered to
148 * sas_domain_function_template to provide libsas used, note: this is just
149 * control the HBA phy rather than other expander phy if you want control
150 * other phy, you should use SMP command.
151 * @sas_phy: which phy in HBA phys.
152 * @func: the operation.
153 * @funcdata: always NULL.
155 int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
158 int rc = 0, phy_id = sas_phy->id;
159 struct pm8001_hba_info *pm8001_ha = NULL;
160 struct sas_phy_linkrates *rates;
161 DECLARE_COMPLETION_ONSTACK(completion);
163 pm8001_ha = sas_phy->ha->lldd_ha;
164 pm8001_ha->phy[phy_id].enable_completion = &completion;
166 case PHY_FUNC_SET_LINK_RATE:
168 if (rates->minimum_linkrate) {
169 pm8001_ha->phy[phy_id].minimum_linkrate =
170 rates->minimum_linkrate;
172 if (rates->maximum_linkrate) {
173 pm8001_ha->phy[phy_id].maximum_linkrate =
174 rates->maximum_linkrate;
176 if (pm8001_ha->phy[phy_id].phy_state == 0) {
177 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
178 wait_for_completion(&completion);
180 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
183 case PHY_FUNC_HARD_RESET:
184 if (pm8001_ha->phy[phy_id].phy_state == 0) {
185 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
186 wait_for_completion(&completion);
188 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
191 case PHY_FUNC_LINK_RESET:
192 if (pm8001_ha->phy[phy_id].phy_state == 0) {
193 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
194 wait_for_completion(&completion);
196 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
199 case PHY_FUNC_RELEASE_SPINUP_HOLD:
200 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
203 case PHY_FUNC_DISABLE:
204 PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
206 case PHY_FUNC_GET_EVENTS:
207 spin_lock_irqsave(&pm8001_ha->lock, flags);
208 if (pm8001_ha->chip_id == chip_8001) {
209 if (-1 == pm8001_bar4_shift(pm8001_ha,
210 (phy_id < 4) ? 0x30000 : 0x40000)) {
211 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
216 struct sas_phy *phy = sas_phy->phy;
217 uint32_t *qp = (uint32_t *)(((char *)
218 pm8001_ha->io_mem[2].memvirtaddr)
219 + 0x1034 + (0x4000 * (phy_id & 3)));
221 phy->invalid_dword_count = qp[0];
222 phy->running_disparity_error_count = qp[1];
223 phy->loss_of_dword_sync_count = qp[3];
224 phy->phy_reset_problem_count = qp[4];
226 if (pm8001_ha->chip_id == chip_8001)
227 pm8001_bar4_shift(pm8001_ha, 0);
228 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
238 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
240 * @shost: the scsi host data.
242 void pm8001_scan_start(struct Scsi_Host *shost)
245 struct pm8001_hba_info *pm8001_ha;
246 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
247 pm8001_ha = sha->lldd_ha;
248 /* SAS_RE_INITIALIZATION not available in SPCv/ve */
249 if (pm8001_ha->chip_id == chip_8001)
250 PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
251 for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
252 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
255 int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
257 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
259 /* give the phy enabling interrupt event time to come in (1s
260 * is empirically about all it takes) */
263 /* Wait for discovery to finish */
269 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
270 * @pm8001_ha: our hba card information
271 * @ccb: the ccb which attached to smp task
273 static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
274 struct pm8001_ccb_info *ccb)
276 return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
279 u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
281 struct ata_queued_cmd *qc = task->uldd_task;
283 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
284 qc->tf.command == ATA_CMD_FPDMA_READ) {
293 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
294 * @pm8001_ha: our hba card information
295 * @ccb: the ccb which attached to sata task
297 static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
298 struct pm8001_ccb_info *ccb)
300 return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
304 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
305 * @pm8001_ha: our hba card information
306 * @ccb: the ccb which attached to TM
307 * @tmf: the task management IU
309 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
310 struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
312 return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
316 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
317 * @pm8001_ha: our hba card information
318 * @ccb: the ccb which attached to ssp task
320 static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
321 struct pm8001_ccb_info *ccb)
323 return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
326 /* Find the local port id that's attached to this device */
327 static int sas_find_local_port_id(struct domain_device *dev)
329 struct domain_device *pdev = dev->parent;
331 /* Directly attached device */
333 return dev->port->id;
335 struct domain_device *pdev_p = pdev->parent;
337 return pdev->port->id;
344 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
345 * @task: the task to be execute.
346 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
347 * we always execute one one time.
348 * @gfp_flags: gfp_flags.
349 * @is_tmf: if it is task management task.
350 * @tmf: the task management IU
352 #define DEV_IS_GONE(pm8001_dev) \
353 ((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
354 static int pm8001_task_exec(struct sas_task *task, const int num,
355 gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
357 struct domain_device *dev = task->dev;
358 struct pm8001_hba_info *pm8001_ha;
359 struct pm8001_device *pm8001_dev;
360 struct pm8001_port *port = NULL;
361 struct sas_task *t = task;
362 struct pm8001_ccb_info *ccb;
363 u32 tag = 0xdeadbeef, rc, n_elem = 0;
365 unsigned long flags = 0;
368 struct task_status_struct *tsm = &t->task_status;
369 tsm->resp = SAS_TASK_UNDELIVERED;
370 tsm->stat = SAS_PHY_DOWN;
371 if (dev->dev_type != SAS_SATA_DEV)
375 pm8001_ha = pm8001_find_ha_by_dev(task->dev);
376 PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n "));
377 spin_lock_irqsave(&pm8001_ha->lock, flags);
380 pm8001_dev = dev->lldd_dev;
381 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
382 if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
383 if (sas_protocol_ata(t->task_proto)) {
384 struct task_status_struct *ts = &t->task_status;
385 ts->resp = SAS_TASK_UNDELIVERED;
386 ts->stat = SAS_PHY_DOWN;
388 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
390 spin_lock_irqsave(&pm8001_ha->lock, flags);
392 t = list_entry(t->list.next,
393 struct sas_task, list);
396 struct task_status_struct *ts = &t->task_status;
397 ts->resp = SAS_TASK_UNDELIVERED;
398 ts->stat = SAS_PHY_DOWN;
401 t = list_entry(t->list.next,
402 struct sas_task, list);
406 rc = pm8001_tag_alloc(pm8001_ha, &tag);
409 ccb = &pm8001_ha->ccb_info[tag];
411 if (!sas_protocol_ata(t->task_proto)) {
412 if (t->num_scatter) {
413 n_elem = dma_map_sg(pm8001_ha->dev,
423 n_elem = t->num_scatter;
427 ccb->n_elem = n_elem;
430 ccb->device = pm8001_dev;
431 switch (t->task_proto) {
432 case SAS_PROTOCOL_SMP:
433 rc = pm8001_task_prep_smp(pm8001_ha, ccb);
435 case SAS_PROTOCOL_SSP:
437 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
440 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
442 case SAS_PROTOCOL_SATA:
443 case SAS_PROTOCOL_STP:
444 rc = pm8001_task_prep_ata(pm8001_ha, ccb);
447 dev_printk(KERN_ERR, pm8001_ha->dev,
448 "unknown sas_task proto: 0x%x\n",
455 PM8001_IO_DBG(pm8001_ha,
456 pm8001_printk("rc is %x\n", rc));
459 /* TODO: select normal or high priority */
460 spin_lock(&t->task_state_lock);
461 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
462 spin_unlock(&t->task_state_lock);
463 pm8001_dev->running_req++;
465 t = list_entry(t->list.next, struct sas_task, list);
471 pm8001_tag_free(pm8001_ha, tag);
473 dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
474 if (!sas_protocol_ata(t->task_proto))
476 dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem,
479 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
484 * pm8001_queue_command - register for upper layer used, all IO commands sent
485 * to HBA are from this interface.
486 * @task: the task to be execute.
487 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
488 * we always execute one one time
489 * @gfp_flags: gfp_flags
491 int pm8001_queue_command(struct sas_task *task, const int num,
494 return pm8001_task_exec(task, num, gfp_flags, 0, NULL);
498 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
499 * @pm8001_ha: our hba card information
500 * @ccb: the ccb which attached to ssp task
501 * @task: the task to be free.
502 * @ccb_idx: ccb index.
504 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
505 struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
509 if (!sas_protocol_ata(task->task_proto))
511 dma_unmap_sg(pm8001_ha->dev, task->scatter,
512 task->num_scatter, task->data_dir);
514 switch (task->task_proto) {
515 case SAS_PROTOCOL_SMP:
516 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
518 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
522 case SAS_PROTOCOL_SATA:
523 case SAS_PROTOCOL_STP:
524 case SAS_PROTOCOL_SSP:
529 task->lldd_task = NULL;
531 ccb->ccb_tag = 0xFFFFFFFF;
533 pm8001_tag_free(pm8001_ha, ccb_idx);
537 * pm8001_alloc_dev - find a empty pm8001_device
538 * @pm8001_ha: our hba card information
540 struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
543 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
544 if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) {
545 pm8001_ha->devices[dev].id = dev;
546 return &pm8001_ha->devices[dev];
549 if (dev == PM8001_MAX_DEVICES) {
550 PM8001_FAIL_DBG(pm8001_ha,
551 pm8001_printk("max support %d devices, ignore ..\n",
552 PM8001_MAX_DEVICES));
557 * pm8001_find_dev - find a matching pm8001_device
558 * @pm8001_ha: our hba card information
560 struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
564 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
565 if (pm8001_ha->devices[dev].device_id == device_id)
566 return &pm8001_ha->devices[dev];
568 if (dev == PM8001_MAX_DEVICES) {
569 PM8001_FAIL_DBG(pm8001_ha, pm8001_printk("NO MATCHING "
570 "DEVICE FOUND !!!\n"));
575 static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
577 u32 id = pm8001_dev->id;
578 memset(pm8001_dev, 0, sizeof(*pm8001_dev));
580 pm8001_dev->dev_type = SAS_PHY_UNUSED;
581 pm8001_dev->device_id = PM8001_MAX_DEVICES;
582 pm8001_dev->sas_device = NULL;
586 * pm8001_dev_found_notify - libsas notify a device is found.
587 * @dev: the device structure which sas layer used.
589 * when libsas find a sas domain device, it should tell the LLDD that
590 * device is found, and then LLDD register this device to HBA firmware
591 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
592 * device ID(according to device's sas address) and returned it to LLDD. From
593 * now on, we communicate with HBA FW with the device ID which HBA assigned
594 * rather than sas address. it is the necessary step for our HBA but it is
595 * the optional for other HBA driver.
597 static int pm8001_dev_found_notify(struct domain_device *dev)
599 unsigned long flags = 0;
601 struct pm8001_hba_info *pm8001_ha = NULL;
602 struct domain_device *parent_dev = dev->parent;
603 struct pm8001_device *pm8001_device;
604 DECLARE_COMPLETION_ONSTACK(completion);
606 pm8001_ha = pm8001_find_ha_by_dev(dev);
607 spin_lock_irqsave(&pm8001_ha->lock, flags);
609 pm8001_device = pm8001_alloc_dev(pm8001_ha);
610 if (!pm8001_device) {
614 pm8001_device->sas_device = dev;
615 dev->lldd_dev = pm8001_device;
616 pm8001_device->dev_type = dev->dev_type;
617 pm8001_device->dcompletion = &completion;
618 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
621 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
623 phy = &parent_dev->ex_dev.ex_phy[phy_id];
624 if (SAS_ADDR(phy->attached_sas_addr)
625 == SAS_ADDR(dev->sas_addr)) {
626 pm8001_device->attached_phy = phy_id;
630 if (phy_id == parent_dev->ex_dev.num_phys) {
631 PM8001_FAIL_DBG(pm8001_ha,
632 pm8001_printk("Error: no attached dev:%016llx"
633 " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
634 SAS_ADDR(parent_dev->sas_addr)));
638 if (dev->dev_type == SAS_SATA_DEV) {
639 pm8001_device->attached_phy =
640 dev->rphy->identify.phy_identifier;
641 flag = 1; /* directly sata*/
643 } /*register this device to HBA*/
644 PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device\n"));
645 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
646 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
647 wait_for_completion(&completion);
648 if (dev->dev_type == SAS_END_DEVICE)
650 pm8001_ha->flags = PM8001F_RUN_TIME;
653 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
657 int pm8001_dev_found(struct domain_device *dev)
659 return pm8001_dev_found_notify(dev);
662 void pm8001_task_done(struct sas_task *task)
664 if (!del_timer(&task->slow_task->timer))
666 complete(&task->slow_task->completion);
669 static void pm8001_tmf_timedout(unsigned long data)
671 struct sas_task *task = (struct sas_task *)data;
673 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
674 complete(&task->slow_task->completion);
677 #define PM8001_TASK_TIMEOUT 20
679 * pm8001_exec_internal_tmf_task - execute some task management commands.
680 * @dev: the wanted device.
681 * @tmf: which task management wanted to be take.
682 * @para_len: para_len.
683 * @parameter: ssp task parameter.
685 * when errors or exception happened, we may want to do something, for example
686 * abort the issued task which result in this execption, it is done by calling
687 * this function, note it is also with the task execute interface.
689 static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
690 void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
693 struct sas_task *task = NULL;
694 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
695 struct pm8001_device *pm8001_dev = dev->lldd_dev;
696 DECLARE_COMPLETION_ONSTACK(completion_setstate);
698 for (retry = 0; retry < 3; retry++) {
699 task = sas_alloc_slow_task(GFP_KERNEL);
704 task->task_proto = dev->tproto;
705 memcpy(&task->ssp_task, parameter, para_len);
706 task->task_done = pm8001_task_done;
707 task->slow_task->timer.data = (unsigned long)task;
708 task->slow_task->timer.function = pm8001_tmf_timedout;
709 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
710 add_timer(&task->slow_task->timer);
712 res = pm8001_task_exec(task, 1, GFP_KERNEL, 1, tmf);
715 del_timer(&task->slow_task->timer);
716 PM8001_FAIL_DBG(pm8001_ha,
717 pm8001_printk("Executing internal task "
721 wait_for_completion(&task->slow_task->completion);
722 if (pm8001_ha->chip_id != chip_8001) {
723 pm8001_dev->setds_completion = &completion_setstate;
724 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
726 wait_for_completion(&completion_setstate);
728 res = -TMF_RESP_FUNC_FAILED;
729 /* Even TMF timed out, return direct. */
730 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
731 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
732 PM8001_FAIL_DBG(pm8001_ha,
733 pm8001_printk("TMF task[%x]timeout.\n",
739 if (task->task_status.resp == SAS_TASK_COMPLETE &&
740 task->task_status.stat == SAM_STAT_GOOD) {
741 res = TMF_RESP_FUNC_COMPLETE;
745 if (task->task_status.resp == SAS_TASK_COMPLETE &&
746 task->task_status.stat == SAS_DATA_UNDERRUN) {
747 /* no error, but return the number of bytes of
749 res = task->task_status.residual;
753 if (task->task_status.resp == SAS_TASK_COMPLETE &&
754 task->task_status.stat == SAS_DATA_OVERRUN) {
755 PM8001_FAIL_DBG(pm8001_ha,
756 pm8001_printk("Blocked task error.\n"));
760 PM8001_EH_DBG(pm8001_ha,
761 pm8001_printk(" Task to dev %016llx response:"
762 "0x%x status 0x%x\n",
763 SAS_ADDR(dev->sas_addr),
764 task->task_status.resp,
765 task->task_status.stat));
771 BUG_ON(retry == 3 && task != NULL);
777 pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
778 struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
783 struct pm8001_ccb_info *ccb;
784 struct sas_task *task = NULL;
786 for (retry = 0; retry < 3; retry++) {
787 task = sas_alloc_slow_task(GFP_KERNEL);
792 task->task_proto = dev->tproto;
793 task->task_done = pm8001_task_done;
794 task->slow_task->timer.data = (unsigned long)task;
795 task->slow_task->timer.function = pm8001_tmf_timedout;
796 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
797 add_timer(&task->slow_task->timer);
799 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
802 ccb = &pm8001_ha->ccb_info[ccb_tag];
803 ccb->device = pm8001_dev;
804 ccb->ccb_tag = ccb_tag;
807 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
808 pm8001_dev, flag, task_tag, ccb_tag);
811 del_timer(&task->slow_task->timer);
812 PM8001_FAIL_DBG(pm8001_ha,
813 pm8001_printk("Executing internal task "
817 wait_for_completion(&task->slow_task->completion);
818 res = TMF_RESP_FUNC_FAILED;
819 /* Even TMF timed out, return direct. */
820 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
821 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
822 PM8001_FAIL_DBG(pm8001_ha,
823 pm8001_printk("TMF task timeout.\n"));
828 if (task->task_status.resp == SAS_TASK_COMPLETE &&
829 task->task_status.stat == SAM_STAT_GOOD) {
830 res = TMF_RESP_FUNC_COMPLETE;
834 PM8001_EH_DBG(pm8001_ha,
835 pm8001_printk(" Task to dev %016llx response: "
836 "0x%x status 0x%x\n",
837 SAS_ADDR(dev->sas_addr),
838 task->task_status.resp,
839 task->task_status.stat));
845 BUG_ON(retry == 3 && task != NULL);
851 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
852 * @dev: the device structure which sas layer used.
854 static void pm8001_dev_gone_notify(struct domain_device *dev)
856 unsigned long flags = 0;
857 struct pm8001_hba_info *pm8001_ha;
858 struct pm8001_device *pm8001_dev = dev->lldd_dev;
860 pm8001_ha = pm8001_find_ha_by_dev(dev);
861 spin_lock_irqsave(&pm8001_ha->lock, flags);
863 u32 device_id = pm8001_dev->device_id;
865 PM8001_DISC_DBG(pm8001_ha,
866 pm8001_printk("found dev[%d:%x] is gone.\n",
867 pm8001_dev->device_id, pm8001_dev->dev_type));
868 if (pm8001_dev->running_req) {
869 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
870 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
872 spin_lock_irqsave(&pm8001_ha->lock, flags);
874 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
875 pm8001_free_dev(pm8001_dev);
877 PM8001_DISC_DBG(pm8001_ha,
878 pm8001_printk("Found dev has gone.\n"));
880 dev->lldd_dev = NULL;
881 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
884 void pm8001_dev_gone(struct domain_device *dev)
886 pm8001_dev_gone_notify(dev);
889 static int pm8001_issue_ssp_tmf(struct domain_device *dev,
890 u8 *lun, struct pm8001_tmf_task *tmf)
892 struct sas_ssp_task ssp_task;
893 if (!(dev->tproto & SAS_PROTOCOL_SSP))
894 return TMF_RESP_FUNC_ESUPP;
896 strncpy((u8 *)&ssp_task.LUN, lun, 8);
897 return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
901 /* retry commands by ha, by task and/or by device */
902 void pm8001_open_reject_retry(
903 struct pm8001_hba_info *pm8001_ha,
904 struct sas_task *task_to_close,
905 struct pm8001_device *device_to_close)
910 if (pm8001_ha == NULL)
913 spin_lock_irqsave(&pm8001_ha->lock, flags);
915 for (i = 0; i < PM8001_MAX_CCB; i++) {
916 struct sas_task *task;
917 struct task_status_struct *ts;
918 struct pm8001_device *pm8001_dev;
919 unsigned long flags1;
921 struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
923 pm8001_dev = ccb->device;
924 if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
926 if (!device_to_close) {
927 uintptr_t d = (uintptr_t)pm8001_dev
928 - (uintptr_t)&pm8001_ha->devices;
929 if (((d % sizeof(*pm8001_dev)) != 0)
930 || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
932 } else if (pm8001_dev != device_to_close)
935 if (!tag || (tag == 0xFFFFFFFF))
938 if (!task || !task->task_done)
940 if (task_to_close && (task != task_to_close))
942 ts = &task->task_status;
943 ts->resp = SAS_TASK_COMPLETE;
944 /* Force the midlayer to retry */
945 ts->stat = SAS_OPEN_REJECT;
946 ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
948 pm8001_dev->running_req--;
949 spin_lock_irqsave(&task->task_state_lock, flags1);
950 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
951 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
952 task->task_state_flags |= SAS_TASK_STATE_DONE;
953 if (unlikely((task->task_state_flags
954 & SAS_TASK_STATE_ABORTED))) {
955 spin_unlock_irqrestore(&task->task_state_lock,
957 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
959 spin_unlock_irqrestore(&task->task_state_lock,
961 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
962 mb();/* in order to force CPU ordering */
963 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
964 task->task_done(task);
965 spin_lock_irqsave(&pm8001_ha->lock, flags);
969 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
973 * Standard mandates link reset for ATA (type 0) and hard reset for
974 * SSP (type 1) , only for RECOVERY
976 int pm8001_I_T_nexus_reset(struct domain_device *dev)
978 int rc = TMF_RESP_FUNC_FAILED;
979 struct pm8001_device *pm8001_dev;
980 struct pm8001_hba_info *pm8001_ha;
983 if (!dev || !dev->lldd_dev)
986 pm8001_dev = dev->lldd_dev;
987 pm8001_ha = pm8001_find_ha_by_dev(dev);
988 phy = sas_get_local_phy(dev);
990 if (dev_is_sata(dev)) {
991 DECLARE_COMPLETION_ONSTACK(completion_setstate);
992 if (scsi_is_sas_phy_local(phy)) {
996 rc = sas_phy_reset(phy, 1);
998 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1000 pm8001_dev->setds_completion = &completion_setstate;
1001 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1003 wait_for_completion(&completion_setstate);
1005 rc = sas_phy_reset(phy, 1);
1008 PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
1009 pm8001_dev->device_id, rc));
1011 sas_put_local_phy(phy);
1016 * This function handle the IT_NEXUS_XXX event or completion
1017 * status code for SSP/SATA/SMP I/O request.
1019 int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
1021 int rc = TMF_RESP_FUNC_FAILED;
1022 struct pm8001_device *pm8001_dev;
1023 struct pm8001_hba_info *pm8001_ha;
1024 struct sas_phy *phy;
1027 if (!dev || !dev->lldd_dev)
1030 pm8001_dev = dev->lldd_dev;
1031 device_id = pm8001_dev->device_id;
1032 pm8001_ha = pm8001_find_ha_by_dev(dev);
1034 PM8001_EH_DBG(pm8001_ha,
1035 pm8001_printk("I_T_Nexus handler invoked !!"));
1037 phy = sas_get_local_phy(dev);
1039 if (dev_is_sata(dev)) {
1040 DECLARE_COMPLETION_ONSTACK(completion_setstate);
1041 if (scsi_is_sas_phy_local(phy)) {
1045 /* send internal ssp/sata/smp abort command to FW */
1046 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1050 /* deregister the target device */
1051 pm8001_dev_gone_notify(dev);
1054 /*send phy reset to hard reset target */
1055 rc = sas_phy_reset(phy, 1);
1057 pm8001_dev->setds_completion = &completion_setstate;
1059 wait_for_completion(&completion_setstate);
1061 /* send internal ssp/sata/smp abort command to FW */
1062 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1066 /* deregister the target device */
1067 pm8001_dev_gone_notify(dev);
1070 /*send phy reset to hard reset target */
1071 rc = sas_phy_reset(phy, 1);
1074 PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
1075 pm8001_dev->device_id, rc));
1077 sas_put_local_phy(phy);
1081 /* mandatory SAM-3, the task reset the specified LUN*/
1082 int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
1084 int rc = TMF_RESP_FUNC_FAILED;
1085 struct pm8001_tmf_task tmf_task;
1086 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1087 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1088 DECLARE_COMPLETION_ONSTACK(completion_setstate);
1089 if (dev_is_sata(dev)) {
1090 struct sas_phy *phy = sas_get_local_phy(dev);
1091 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1093 rc = sas_phy_reset(phy, 1);
1094 sas_put_local_phy(phy);
1095 pm8001_dev->setds_completion = &completion_setstate;
1096 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1098 wait_for_completion(&completion_setstate);
1100 tmf_task.tmf = TMF_LU_RESET;
1101 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1103 /* If failed, fall-through I_T_Nexus reset */
1104 PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
1105 pm8001_dev->device_id, rc));
1109 /* optional SAM-3 */
1110 int pm8001_query_task(struct sas_task *task)
1112 u32 tag = 0xdeadbeef;
1114 struct scsi_lun lun;
1115 struct pm8001_tmf_task tmf_task;
1116 int rc = TMF_RESP_FUNC_FAILED;
1117 if (unlikely(!task || !task->lldd_task || !task->dev))
1120 if (task->task_proto & SAS_PROTOCOL_SSP) {
1121 struct scsi_cmnd *cmnd = task->uldd_task;
1122 struct domain_device *dev = task->dev;
1123 struct pm8001_hba_info *pm8001_ha =
1124 pm8001_find_ha_by_dev(dev);
1126 int_to_scsilun(cmnd->device->lun, &lun);
1127 rc = pm8001_find_tag(task, &tag);
1129 rc = TMF_RESP_FUNC_FAILED;
1132 PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
1133 for (i = 0; i < 16; i++)
1134 printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
1135 printk(KERN_INFO "]\n");
1136 tmf_task.tmf = TMF_QUERY_TASK;
1137 tmf_task.tag_of_task_to_be_managed = tag;
1139 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1141 /* The task is still in Lun, release it then */
1142 case TMF_RESP_FUNC_SUCC:
1143 PM8001_EH_DBG(pm8001_ha,
1144 pm8001_printk("The task is still in Lun\n"));
1146 /* The task is not in Lun or failed, reset the phy */
1147 case TMF_RESP_FUNC_FAILED:
1148 case TMF_RESP_FUNC_COMPLETE:
1149 PM8001_EH_DBG(pm8001_ha,
1150 pm8001_printk("The task is not in Lun or failed,"
1151 " reset the phy\n"));
1155 pm8001_printk(":rc= %d\n", rc);
1159 /* mandatory SAM-3, still need free task/ccb info, abord the specified task */
1160 int pm8001_abort_task(struct sas_task *task)
1162 unsigned long flags;
1163 u32 tag = 0xdeadbeef;
1165 struct domain_device *dev ;
1166 struct pm8001_hba_info *pm8001_ha = NULL;
1167 struct pm8001_ccb_info *ccb;
1168 struct scsi_lun lun;
1169 struct pm8001_device *pm8001_dev;
1170 struct pm8001_tmf_task tmf_task;
1171 int rc = TMF_RESP_FUNC_FAILED;
1172 if (unlikely(!task || !task->lldd_task || !task->dev))
1174 spin_lock_irqsave(&task->task_state_lock, flags);
1175 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1176 spin_unlock_irqrestore(&task->task_state_lock, flags);
1177 rc = TMF_RESP_FUNC_COMPLETE;
1180 spin_unlock_irqrestore(&task->task_state_lock, flags);
1181 if (task->task_proto & SAS_PROTOCOL_SSP) {
1182 struct scsi_cmnd *cmnd = task->uldd_task;
1184 ccb = task->lldd_task;
1185 pm8001_dev = dev->lldd_dev;
1186 pm8001_ha = pm8001_find_ha_by_dev(dev);
1187 int_to_scsilun(cmnd->device->lun, &lun);
1188 rc = pm8001_find_tag(task, &tag);
1190 printk(KERN_INFO "No such tag in %s\n", __func__);
1191 rc = TMF_RESP_FUNC_FAILED;
1194 device_id = pm8001_dev->device_id;
1195 PM8001_EH_DBG(pm8001_ha,
1196 pm8001_printk("abort io to deviceid= %d\n", device_id));
1197 tmf_task.tmf = TMF_ABORT_TASK;
1198 tmf_task.tag_of_task_to_be_managed = tag;
1199 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1200 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1201 pm8001_dev->sas_device, 0, tag);
1202 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1203 task->task_proto & SAS_PROTOCOL_STP) {
1205 pm8001_dev = dev->lldd_dev;
1206 pm8001_ha = pm8001_find_ha_by_dev(dev);
1207 rc = pm8001_find_tag(task, &tag);
1209 printk(KERN_INFO "No such tag in %s\n", __func__);
1210 rc = TMF_RESP_FUNC_FAILED;
1213 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1214 pm8001_dev->sas_device, 0, tag);
1215 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1218 pm8001_dev = dev->lldd_dev;
1219 pm8001_ha = pm8001_find_ha_by_dev(dev);
1220 rc = pm8001_find_tag(task, &tag);
1222 printk(KERN_INFO "No such tag in %s\n", __func__);
1223 rc = TMF_RESP_FUNC_FAILED;
1226 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1227 pm8001_dev->sas_device, 0, tag);
1231 if (rc != TMF_RESP_FUNC_COMPLETE)
1232 pm8001_printk("rc= %d\n", rc);
1236 int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1238 int rc = TMF_RESP_FUNC_FAILED;
1239 struct pm8001_tmf_task tmf_task;
1241 tmf_task.tmf = TMF_ABORT_TASK_SET;
1242 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1246 int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1248 int rc = TMF_RESP_FUNC_FAILED;
1249 struct pm8001_tmf_task tmf_task;
1251 tmf_task.tmf = TMF_CLEAR_ACA;
1252 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1257 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1259 int rc = TMF_RESP_FUNC_FAILED;
1260 struct pm8001_tmf_task tmf_task;
1261 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1262 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1264 PM8001_EH_DBG(pm8001_ha,
1265 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1266 pm8001_dev->device_id));
1267 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1268 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);