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;
119 mem_virt_alloc = dma_alloc_coherent(&pdev->dev, mem_size + align,
120 &mem_dma_handle, GFP_KERNEL);
121 if (!mem_virt_alloc) {
122 pr_err("pm80xx: memory allocation error\n");
125 *pphys_addr = mem_dma_handle;
126 phys_align = (*pphys_addr + align_offset) & ~align_offset;
127 *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
128 *pphys_addr_hi = upper_32_bits(phys_align);
129 *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 struct pm8001_phy *phy;
162 DECLARE_COMPLETION_ONSTACK(completion);
164 pm8001_ha = sas_phy->ha->lldd_ha;
165 phy = &pm8001_ha->phy[phy_id];
166 pm8001_ha->phy[phy_id].enable_completion = &completion;
168 case PHY_FUNC_SET_LINK_RATE:
170 if (rates->minimum_linkrate) {
171 pm8001_ha->phy[phy_id].minimum_linkrate =
172 rates->minimum_linkrate;
174 if (rates->maximum_linkrate) {
175 pm8001_ha->phy[phy_id].maximum_linkrate =
176 rates->maximum_linkrate;
178 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
179 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
180 wait_for_completion(&completion);
182 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
185 case PHY_FUNC_HARD_RESET:
186 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
187 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
188 wait_for_completion(&completion);
190 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
193 case PHY_FUNC_LINK_RESET:
194 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
195 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
196 wait_for_completion(&completion);
198 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
201 case PHY_FUNC_RELEASE_SPINUP_HOLD:
202 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
205 case PHY_FUNC_DISABLE:
206 if (pm8001_ha->chip_id != chip_8001) {
207 if (pm8001_ha->phy[phy_id].phy_state ==
208 PHY_STATE_LINK_UP_SPCV) {
209 sas_phy_disconnected(&phy->sas_phy);
210 sas_notify_phy_event(&phy->sas_phy,
211 PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
212 phy->phy_attached = 0;
215 if (pm8001_ha->phy[phy_id].phy_state ==
216 PHY_STATE_LINK_UP_SPC) {
217 sas_phy_disconnected(&phy->sas_phy);
218 sas_notify_phy_event(&phy->sas_phy,
219 PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
220 phy->phy_attached = 0;
223 PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
225 case PHY_FUNC_GET_EVENTS:
226 spin_lock_irqsave(&pm8001_ha->lock, flags);
227 if (pm8001_ha->chip_id == chip_8001) {
228 if (-1 == pm8001_bar4_shift(pm8001_ha,
229 (phy_id < 4) ? 0x30000 : 0x40000)) {
230 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
235 struct sas_phy *phy = sas_phy->phy;
236 uint32_t *qp = (uint32_t *)(((char *)
237 pm8001_ha->io_mem[2].memvirtaddr)
238 + 0x1034 + (0x4000 * (phy_id & 3)));
240 phy->invalid_dword_count = qp[0];
241 phy->running_disparity_error_count = qp[1];
242 phy->loss_of_dword_sync_count = qp[3];
243 phy->phy_reset_problem_count = qp[4];
245 if (pm8001_ha->chip_id == chip_8001)
246 pm8001_bar4_shift(pm8001_ha, 0);
247 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
250 pm8001_dbg(pm8001_ha, DEVIO, "func 0x%x\n", func);
258 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
260 * @shost: the scsi host data.
262 void pm8001_scan_start(struct Scsi_Host *shost)
265 struct pm8001_hba_info *pm8001_ha;
266 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
267 pm8001_ha = sha->lldd_ha;
268 /* SAS_RE_INITIALIZATION not available in SPCv/ve */
269 if (pm8001_ha->chip_id == chip_8001)
270 PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
271 for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
272 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
275 int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
277 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
279 /* give the phy enabling interrupt event time to come in (1s
280 * is empirically about all it takes) */
283 /* Wait for discovery to finish */
289 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
290 * @pm8001_ha: our hba card information
291 * @ccb: the ccb which attached to smp task
293 static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
294 struct pm8001_ccb_info *ccb)
296 return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
299 u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
301 struct ata_queued_cmd *qc = task->uldd_task;
303 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
304 qc->tf.command == ATA_CMD_FPDMA_READ ||
305 qc->tf.command == ATA_CMD_FPDMA_RECV ||
306 qc->tf.command == ATA_CMD_FPDMA_SEND ||
307 qc->tf.command == ATA_CMD_NCQ_NON_DATA) {
316 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
317 * @pm8001_ha: our hba card information
318 * @ccb: the ccb which attached to sata task
320 static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
321 struct pm8001_ccb_info *ccb)
323 return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
327 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
328 * @pm8001_ha: our hba card information
329 * @ccb: the ccb which attached to TM
330 * @tmf: the task management IU
332 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
333 struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
335 return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
339 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
340 * @pm8001_ha: our hba card information
341 * @ccb: the ccb which attached to ssp task
343 static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
344 struct pm8001_ccb_info *ccb)
346 return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
349 /* Find the local port id that's attached to this device */
350 static int sas_find_local_port_id(struct domain_device *dev)
352 struct domain_device *pdev = dev->parent;
354 /* Directly attached device */
356 return dev->port->id;
358 struct domain_device *pdev_p = pdev->parent;
360 return pdev->port->id;
366 #define DEV_IS_GONE(pm8001_dev) \
367 ((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
369 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
370 * @task: the task to be execute.
371 * @gfp_flags: gfp_flags.
372 * @is_tmf: if it is task management task.
373 * @tmf: the task management IU
375 static int pm8001_task_exec(struct sas_task *task,
376 gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
378 struct domain_device *dev = task->dev;
379 struct pm8001_hba_info *pm8001_ha;
380 struct pm8001_device *pm8001_dev;
381 struct pm8001_port *port = NULL;
382 struct sas_task *t = task;
383 struct pm8001_ccb_info *ccb;
384 u32 tag = 0xdeadbeef, rc = 0, n_elem = 0;
385 unsigned long flags = 0;
386 enum sas_protocol task_proto = t->task_proto;
389 struct task_status_struct *tsm = &t->task_status;
390 tsm->resp = SAS_TASK_UNDELIVERED;
391 tsm->stat = SAS_PHY_DOWN;
392 if (dev->dev_type != SAS_SATA_DEV)
396 pm8001_ha = pm8001_find_ha_by_dev(task->dev);
397 if (pm8001_ha->controller_fatal_error) {
398 struct task_status_struct *ts = &t->task_status;
400 ts->resp = SAS_TASK_UNDELIVERED;
404 pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec device\n");
405 spin_lock_irqsave(&pm8001_ha->lock, flags);
408 pm8001_dev = dev->lldd_dev;
409 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
410 if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
411 if (sas_protocol_ata(task_proto)) {
412 struct task_status_struct *ts = &t->task_status;
413 ts->resp = SAS_TASK_UNDELIVERED;
414 ts->stat = SAS_PHY_DOWN;
416 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
418 spin_lock_irqsave(&pm8001_ha->lock, flags);
421 struct task_status_struct *ts = &t->task_status;
422 ts->resp = SAS_TASK_UNDELIVERED;
423 ts->stat = SAS_PHY_DOWN;
428 rc = pm8001_tag_alloc(pm8001_ha, &tag);
431 ccb = &pm8001_ha->ccb_info[tag];
433 if (!sas_protocol_ata(task_proto)) {
434 if (t->num_scatter) {
435 n_elem = dma_map_sg(pm8001_ha->dev,
445 n_elem = t->num_scatter;
449 ccb->n_elem = n_elem;
452 ccb->device = pm8001_dev;
453 switch (task_proto) {
454 case SAS_PROTOCOL_SMP:
455 atomic_inc(&pm8001_dev->running_req);
456 rc = pm8001_task_prep_smp(pm8001_ha, ccb);
458 case SAS_PROTOCOL_SSP:
459 atomic_inc(&pm8001_dev->running_req);
461 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
464 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
466 case SAS_PROTOCOL_SATA:
467 case SAS_PROTOCOL_STP:
468 atomic_inc(&pm8001_dev->running_req);
469 rc = pm8001_task_prep_ata(pm8001_ha, ccb);
472 dev_printk(KERN_ERR, pm8001_ha->dev,
473 "unknown sas_task proto: 0x%x\n", task_proto);
479 pm8001_dbg(pm8001_ha, IO, "rc is %x\n", rc);
480 atomic_dec(&pm8001_dev->running_req);
483 /* TODO: select normal or high priority */
484 spin_lock(&t->task_state_lock);
485 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
486 spin_unlock(&t->task_state_lock);
492 pm8001_tag_free(pm8001_ha, tag);
494 dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
495 if (!sas_protocol_ata(task_proto))
497 dma_unmap_sg(pm8001_ha->dev, t->scatter, t->num_scatter,
500 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
505 * pm8001_queue_command - register for upper layer used, all IO commands sent
506 * to HBA are from this interface.
507 * @task: the task to be execute.
508 * @gfp_flags: gfp_flags
510 int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags)
512 return pm8001_task_exec(task, gfp_flags, 0, NULL);
516 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
517 * @pm8001_ha: our hba card information
518 * @ccb: the ccb which attached to ssp task
519 * @task: the task to be free.
520 * @ccb_idx: ccb index.
522 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
523 struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
527 if (!sas_protocol_ata(task->task_proto))
529 dma_unmap_sg(pm8001_ha->dev, task->scatter,
530 task->num_scatter, task->data_dir);
532 switch (task->task_proto) {
533 case SAS_PROTOCOL_SMP:
534 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
536 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
540 case SAS_PROTOCOL_SATA:
541 case SAS_PROTOCOL_STP:
542 case SAS_PROTOCOL_SSP:
547 task->lldd_task = NULL;
549 ccb->ccb_tag = 0xFFFFFFFF;
551 pm8001_tag_free(pm8001_ha, ccb_idx);
555 * pm8001_alloc_dev - find a empty pm8001_device
556 * @pm8001_ha: our hba card information
558 static struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
561 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
562 if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) {
563 pm8001_ha->devices[dev].id = dev;
564 return &pm8001_ha->devices[dev];
567 if (dev == PM8001_MAX_DEVICES) {
568 pm8001_dbg(pm8001_ha, FAIL,
569 "max support %d devices, ignore ..\n",
575 * pm8001_find_dev - find a matching pm8001_device
576 * @pm8001_ha: our hba card information
577 * @device_id: device ID to match against
579 struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
583 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
584 if (pm8001_ha->devices[dev].device_id == device_id)
585 return &pm8001_ha->devices[dev];
587 if (dev == PM8001_MAX_DEVICES) {
588 pm8001_dbg(pm8001_ha, FAIL, "NO MATCHING DEVICE FOUND !!!\n");
593 static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
595 u32 id = pm8001_dev->id;
596 memset(pm8001_dev, 0, sizeof(*pm8001_dev));
598 pm8001_dev->dev_type = SAS_PHY_UNUSED;
599 pm8001_dev->device_id = PM8001_MAX_DEVICES;
600 pm8001_dev->sas_device = NULL;
604 * pm8001_dev_found_notify - libsas notify a device is found.
605 * @dev: the device structure which sas layer used.
607 * when libsas find a sas domain device, it should tell the LLDD that
608 * device is found, and then LLDD register this device to HBA firmware
609 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
610 * device ID(according to device's sas address) and returned it to LLDD. From
611 * now on, we communicate with HBA FW with the device ID which HBA assigned
612 * rather than sas address. it is the necessary step for our HBA but it is
613 * the optional for other HBA driver.
615 static int pm8001_dev_found_notify(struct domain_device *dev)
617 unsigned long flags = 0;
619 struct pm8001_hba_info *pm8001_ha = NULL;
620 struct domain_device *parent_dev = dev->parent;
621 struct pm8001_device *pm8001_device;
622 DECLARE_COMPLETION_ONSTACK(completion);
624 pm8001_ha = pm8001_find_ha_by_dev(dev);
625 spin_lock_irqsave(&pm8001_ha->lock, flags);
627 pm8001_device = pm8001_alloc_dev(pm8001_ha);
628 if (!pm8001_device) {
632 pm8001_device->sas_device = dev;
633 dev->lldd_dev = pm8001_device;
634 pm8001_device->dev_type = dev->dev_type;
635 pm8001_device->dcompletion = &completion;
636 if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
639 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
641 phy = &parent_dev->ex_dev.ex_phy[phy_id];
642 if (SAS_ADDR(phy->attached_sas_addr)
643 == SAS_ADDR(dev->sas_addr)) {
644 pm8001_device->attached_phy = phy_id;
648 if (phy_id == parent_dev->ex_dev.num_phys) {
649 pm8001_dbg(pm8001_ha, FAIL,
650 "Error: no attached dev:%016llx at ex:%016llx.\n",
651 SAS_ADDR(dev->sas_addr),
652 SAS_ADDR(parent_dev->sas_addr));
656 if (dev->dev_type == SAS_SATA_DEV) {
657 pm8001_device->attached_phy =
658 dev->rphy->identify.phy_identifier;
659 flag = 1; /* directly sata */
661 } /*register this device to HBA*/
662 pm8001_dbg(pm8001_ha, DISC, "Found device\n");
663 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
664 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
665 wait_for_completion(&completion);
666 if (dev->dev_type == SAS_END_DEVICE)
668 pm8001_ha->flags = PM8001F_RUN_TIME;
671 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
675 int pm8001_dev_found(struct domain_device *dev)
677 return pm8001_dev_found_notify(dev);
680 void pm8001_task_done(struct sas_task *task)
682 if (!del_timer(&task->slow_task->timer))
684 complete(&task->slow_task->completion);
687 static void pm8001_tmf_timedout(struct timer_list *t)
689 struct sas_task_slow *slow = from_timer(slow, t, timer);
690 struct sas_task *task = slow->task;
692 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
693 complete(&task->slow_task->completion);
696 #define PM8001_TASK_TIMEOUT 20
698 * pm8001_exec_internal_tmf_task - execute some task management commands.
699 * @dev: the wanted device.
700 * @tmf: which task management wanted to be take.
701 * @para_len: para_len.
702 * @parameter: ssp task parameter.
704 * when errors or exception happened, we may want to do something, for example
705 * abort the issued task which result in this execption, it is done by calling
706 * this function, note it is also with the task execute interface.
708 static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
709 void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
712 struct sas_task *task = NULL;
713 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
714 struct pm8001_device *pm8001_dev = dev->lldd_dev;
715 DECLARE_COMPLETION_ONSTACK(completion_setstate);
717 for (retry = 0; retry < 3; retry++) {
718 task = sas_alloc_slow_task(GFP_KERNEL);
723 task->task_proto = dev->tproto;
724 memcpy(&task->ssp_task, parameter, para_len);
725 task->task_done = pm8001_task_done;
726 task->slow_task->timer.function = pm8001_tmf_timedout;
727 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
728 add_timer(&task->slow_task->timer);
730 res = pm8001_task_exec(task, GFP_KERNEL, 1, tmf);
733 del_timer(&task->slow_task->timer);
734 pm8001_dbg(pm8001_ha, FAIL, "Executing internal task failed\n");
737 wait_for_completion(&task->slow_task->completion);
738 if (pm8001_ha->chip_id != chip_8001) {
739 pm8001_dev->setds_completion = &completion_setstate;
740 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
742 wait_for_completion(&completion_setstate);
744 res = -TMF_RESP_FUNC_FAILED;
745 /* Even TMF timed out, return direct. */
746 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
747 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
748 pm8001_dbg(pm8001_ha, FAIL,
749 "TMF task[%x]timeout.\n",
755 if (task->task_status.resp == SAS_TASK_COMPLETE &&
756 task->task_status.stat == SAM_STAT_GOOD) {
757 res = TMF_RESP_FUNC_COMPLETE;
761 if (task->task_status.resp == SAS_TASK_COMPLETE &&
762 task->task_status.stat == SAS_DATA_UNDERRUN) {
763 /* no error, but return the number of bytes of
765 res = task->task_status.residual;
769 if (task->task_status.resp == SAS_TASK_COMPLETE &&
770 task->task_status.stat == SAS_DATA_OVERRUN) {
771 pm8001_dbg(pm8001_ha, FAIL, "Blocked task error.\n");
775 pm8001_dbg(pm8001_ha, EH,
776 " Task to dev %016llx response:0x%x status 0x%x\n",
777 SAS_ADDR(dev->sas_addr),
778 task->task_status.resp,
779 task->task_status.stat);
785 BUG_ON(retry == 3 && task != NULL);
791 pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
792 struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
797 struct pm8001_ccb_info *ccb;
798 struct sas_task *task = NULL;
800 for (retry = 0; retry < 3; retry++) {
801 task = sas_alloc_slow_task(GFP_KERNEL);
806 task->task_proto = dev->tproto;
807 task->task_done = pm8001_task_done;
808 task->slow_task->timer.function = pm8001_tmf_timedout;
809 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
810 add_timer(&task->slow_task->timer);
812 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
815 ccb = &pm8001_ha->ccb_info[ccb_tag];
816 ccb->device = pm8001_dev;
817 ccb->ccb_tag = ccb_tag;
821 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
822 pm8001_dev, flag, task_tag, ccb_tag);
825 del_timer(&task->slow_task->timer);
826 pm8001_dbg(pm8001_ha, FAIL, "Executing internal task failed\n");
829 wait_for_completion(&task->slow_task->completion);
830 res = TMF_RESP_FUNC_FAILED;
831 /* Even TMF timed out, return direct. */
832 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
833 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
834 pm8001_dbg(pm8001_ha, FAIL,
835 "TMF task timeout.\n");
840 if (task->task_status.resp == SAS_TASK_COMPLETE &&
841 task->task_status.stat == SAM_STAT_GOOD) {
842 res = TMF_RESP_FUNC_COMPLETE;
846 pm8001_dbg(pm8001_ha, EH,
847 " Task to dev %016llx response: 0x%x status 0x%x\n",
848 SAS_ADDR(dev->sas_addr),
849 task->task_status.resp,
850 task->task_status.stat);
856 BUG_ON(retry == 3 && task != NULL);
862 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
863 * @dev: the device structure which sas layer used.
865 static void pm8001_dev_gone_notify(struct domain_device *dev)
867 unsigned long flags = 0;
868 struct pm8001_hba_info *pm8001_ha;
869 struct pm8001_device *pm8001_dev = dev->lldd_dev;
871 pm8001_ha = pm8001_find_ha_by_dev(dev);
872 spin_lock_irqsave(&pm8001_ha->lock, flags);
874 u32 device_id = pm8001_dev->device_id;
876 pm8001_dbg(pm8001_ha, DISC, "found dev[%d:%x] is gone.\n",
877 pm8001_dev->device_id, pm8001_dev->dev_type);
878 if (atomic_read(&pm8001_dev->running_req)) {
879 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
880 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
882 while (atomic_read(&pm8001_dev->running_req))
884 spin_lock_irqsave(&pm8001_ha->lock, flags);
886 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
887 pm8001_free_dev(pm8001_dev);
889 pm8001_dbg(pm8001_ha, DISC, "Found dev has gone.\n");
891 dev->lldd_dev = NULL;
892 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
895 void pm8001_dev_gone(struct domain_device *dev)
897 pm8001_dev_gone_notify(dev);
900 static int pm8001_issue_ssp_tmf(struct domain_device *dev,
901 u8 *lun, struct pm8001_tmf_task *tmf)
903 struct sas_ssp_task ssp_task;
904 if (!(dev->tproto & SAS_PROTOCOL_SSP))
905 return TMF_RESP_FUNC_ESUPP;
907 memcpy((u8 *)&ssp_task.LUN, lun, 8);
908 return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
912 /* retry commands by ha, by task and/or by device */
913 void pm8001_open_reject_retry(
914 struct pm8001_hba_info *pm8001_ha,
915 struct sas_task *task_to_close,
916 struct pm8001_device *device_to_close)
921 if (pm8001_ha == NULL)
924 spin_lock_irqsave(&pm8001_ha->lock, flags);
926 for (i = 0; i < PM8001_MAX_CCB; i++) {
927 struct sas_task *task;
928 struct task_status_struct *ts;
929 struct pm8001_device *pm8001_dev;
930 unsigned long flags1;
932 struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
934 pm8001_dev = ccb->device;
935 if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
937 if (!device_to_close) {
938 uintptr_t d = (uintptr_t)pm8001_dev
939 - (uintptr_t)&pm8001_ha->devices;
940 if (((d % sizeof(*pm8001_dev)) != 0)
941 || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
943 } else if (pm8001_dev != device_to_close)
946 if (!tag || (tag == 0xFFFFFFFF))
949 if (!task || !task->task_done)
951 if (task_to_close && (task != task_to_close))
953 ts = &task->task_status;
954 ts->resp = SAS_TASK_COMPLETE;
955 /* Force the midlayer to retry */
956 ts->stat = SAS_OPEN_REJECT;
957 ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
959 atomic_dec(&pm8001_dev->running_req);
960 spin_lock_irqsave(&task->task_state_lock, flags1);
961 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
962 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
963 task->task_state_flags |= SAS_TASK_STATE_DONE;
964 if (unlikely((task->task_state_flags
965 & SAS_TASK_STATE_ABORTED))) {
966 spin_unlock_irqrestore(&task->task_state_lock,
968 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
970 spin_unlock_irqrestore(&task->task_state_lock,
972 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
973 mb();/* in order to force CPU ordering */
974 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
975 task->task_done(task);
976 spin_lock_irqsave(&pm8001_ha->lock, flags);
980 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
984 * Standard mandates link reset for ATA (type 0) and hard reset for
985 * SSP (type 1) , only for RECOVERY
986 * @dev: the device structure for the device to reset.
988 int pm8001_I_T_nexus_reset(struct domain_device *dev)
990 int rc = TMF_RESP_FUNC_FAILED;
991 struct pm8001_device *pm8001_dev;
992 struct pm8001_hba_info *pm8001_ha;
995 if (!dev || !dev->lldd_dev)
998 pm8001_dev = dev->lldd_dev;
999 pm8001_ha = pm8001_find_ha_by_dev(dev);
1000 phy = sas_get_local_phy(dev);
1002 if (dev_is_sata(dev)) {
1003 if (scsi_is_sas_phy_local(phy)) {
1007 rc = sas_phy_reset(phy, 1);
1009 pm8001_dbg(pm8001_ha, EH,
1010 "phy reset failed for device %x\n"
1011 "with rc %d\n", pm8001_dev->device_id, rc);
1012 rc = TMF_RESP_FUNC_FAILED;
1016 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1019 pm8001_dbg(pm8001_ha, EH, "task abort failed %x\n"
1020 "with rc %d\n", pm8001_dev->device_id, rc);
1021 rc = TMF_RESP_FUNC_FAILED;
1024 rc = sas_phy_reset(phy, 1);
1027 pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
1028 pm8001_dev->device_id, rc);
1030 sas_put_local_phy(phy);
1035 * This function handle the IT_NEXUS_XXX event or completion
1036 * status code for SSP/SATA/SMP I/O request.
1038 int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
1040 int rc = TMF_RESP_FUNC_FAILED;
1041 struct pm8001_device *pm8001_dev;
1042 struct pm8001_hba_info *pm8001_ha;
1043 struct sas_phy *phy;
1045 if (!dev || !dev->lldd_dev)
1048 pm8001_dev = dev->lldd_dev;
1049 pm8001_ha = pm8001_find_ha_by_dev(dev);
1051 pm8001_dbg(pm8001_ha, EH, "I_T_Nexus handler invoked !!\n");
1053 phy = sas_get_local_phy(dev);
1055 if (dev_is_sata(dev)) {
1056 DECLARE_COMPLETION_ONSTACK(completion_setstate);
1057 if (scsi_is_sas_phy_local(phy)) {
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);
1073 pm8001_dev->setds_completion = &completion_setstate;
1075 wait_for_completion(&completion_setstate);
1077 /* send internal ssp/sata/smp abort command to FW */
1078 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1082 /* deregister the target device */
1083 pm8001_dev_gone_notify(dev);
1086 /*send phy reset to hard reset target */
1087 rc = sas_phy_reset(phy, 1);
1090 pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
1091 pm8001_dev->device_id, rc);
1093 sas_put_local_phy(phy);
1097 /* mandatory SAM-3, the task reset the specified LUN*/
1098 int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
1100 int rc = TMF_RESP_FUNC_FAILED;
1101 struct pm8001_tmf_task tmf_task;
1102 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1103 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1104 DECLARE_COMPLETION_ONSTACK(completion_setstate);
1105 if (dev_is_sata(dev)) {
1106 struct sas_phy *phy = sas_get_local_phy(dev);
1107 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1109 rc = sas_phy_reset(phy, 1);
1110 sas_put_local_phy(phy);
1111 pm8001_dev->setds_completion = &completion_setstate;
1112 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1114 wait_for_completion(&completion_setstate);
1116 tmf_task.tmf = TMF_LU_RESET;
1117 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1119 /* If failed, fall-through I_T_Nexus reset */
1120 pm8001_dbg(pm8001_ha, EH, "for device[%x]:rc=%d\n",
1121 pm8001_dev->device_id, rc);
1125 /* optional SAM-3 */
1126 int pm8001_query_task(struct sas_task *task)
1128 u32 tag = 0xdeadbeef;
1129 struct scsi_lun lun;
1130 struct pm8001_tmf_task tmf_task;
1131 int rc = TMF_RESP_FUNC_FAILED;
1132 if (unlikely(!task || !task->lldd_task || !task->dev))
1135 if (task->task_proto & SAS_PROTOCOL_SSP) {
1136 struct scsi_cmnd *cmnd = task->uldd_task;
1137 struct domain_device *dev = task->dev;
1138 struct pm8001_hba_info *pm8001_ha =
1139 pm8001_find_ha_by_dev(dev);
1141 int_to_scsilun(cmnd->device->lun, &lun);
1142 rc = pm8001_find_tag(task, &tag);
1144 rc = TMF_RESP_FUNC_FAILED;
1147 pm8001_dbg(pm8001_ha, EH, "Query:[%16ph]\n", cmnd->cmnd);
1148 tmf_task.tmf = TMF_QUERY_TASK;
1149 tmf_task.tag_of_task_to_be_managed = tag;
1151 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1153 /* The task is still in Lun, release it then */
1154 case TMF_RESP_FUNC_SUCC:
1155 pm8001_dbg(pm8001_ha, EH,
1156 "The task is still in Lun\n");
1158 /* The task is not in Lun or failed, reset the phy */
1159 case TMF_RESP_FUNC_FAILED:
1160 case TMF_RESP_FUNC_COMPLETE:
1161 pm8001_dbg(pm8001_ha, EH,
1162 "The task is not in Lun or failed, reset the phy\n");
1166 pr_err("pm80xx: rc= %d\n", rc);
1170 /* mandatory SAM-3, still need free task/ccb info, abort the specified task */
1171 int pm8001_abort_task(struct sas_task *task)
1173 unsigned long flags;
1175 struct domain_device *dev ;
1176 struct pm8001_hba_info *pm8001_ha;
1177 struct scsi_lun lun;
1178 struct pm8001_device *pm8001_dev;
1179 struct pm8001_tmf_task tmf_task;
1180 int rc = TMF_RESP_FUNC_FAILED, ret;
1182 struct sas_task_slow slow_task;
1184 if (unlikely(!task || !task->lldd_task || !task->dev))
1185 return TMF_RESP_FUNC_FAILED;
1188 pm8001_dev = dev->lldd_dev;
1189 pm8001_ha = pm8001_find_ha_by_dev(dev);
1190 phy_id = pm8001_dev->attached_phy;
1192 if (PM8001_CHIP_DISP->fatal_errors(pm8001_ha)) {
1193 // If the controller is seeing fatal errors
1194 // abort task will not get a response from the controller
1195 return TMF_RESP_FUNC_FAILED;
1198 ret = pm8001_find_tag(task, &tag);
1200 pm8001_info(pm8001_ha, "no tag for task:%p\n", task);
1201 return TMF_RESP_FUNC_FAILED;
1203 spin_lock_irqsave(&task->task_state_lock, flags);
1204 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1205 spin_unlock_irqrestore(&task->task_state_lock, flags);
1206 return TMF_RESP_FUNC_COMPLETE;
1208 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1209 if (task->slow_task == NULL) {
1210 init_completion(&slow_task.completion);
1211 task->slow_task = &slow_task;
1213 spin_unlock_irqrestore(&task->task_state_lock, flags);
1214 if (task->task_proto & SAS_PROTOCOL_SSP) {
1215 struct scsi_cmnd *cmnd = task->uldd_task;
1216 int_to_scsilun(cmnd->device->lun, &lun);
1217 tmf_task.tmf = TMF_ABORT_TASK;
1218 tmf_task.tag_of_task_to_be_managed = tag;
1219 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1220 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1221 pm8001_dev->sas_device, 0, tag);
1222 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1223 task->task_proto & SAS_PROTOCOL_STP) {
1224 if (pm8001_ha->chip_id == chip_8006) {
1225 DECLARE_COMPLETION_ONSTACK(completion_reset);
1226 DECLARE_COMPLETION_ONSTACK(completion);
1227 struct pm8001_phy *phy = pm8001_ha->phy + phy_id;
1229 /* 1. Set Device state as Recovery */
1230 pm8001_dev->setds_completion = &completion;
1231 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1233 wait_for_completion(&completion);
1235 /* 2. Send Phy Control Hard Reset */
1236 reinit_completion(&completion);
1237 phy->port_reset_status = PORT_RESET_TMO;
1238 phy->reset_success = false;
1239 phy->enable_completion = &completion;
1240 phy->reset_completion = &completion_reset;
1241 ret = PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
1244 phy->enable_completion = NULL;
1245 phy->reset_completion = NULL;
1249 /* In the case of the reset timeout/fail we still
1250 * abort the command at the firmware. The assumption
1251 * here is that the drive is off doing something so
1252 * that it's not processing requests, and we want to
1253 * avoid getting a completion for this and either
1254 * leaking the task in libsas or losing the race and
1255 * getting a double free.
1257 pm8001_dbg(pm8001_ha, MSG,
1258 "Waiting for local phy ctl\n");
1259 ret = wait_for_completion_timeout(&completion,
1260 PM8001_TASK_TIMEOUT * HZ);
1261 if (!ret || !phy->reset_success) {
1262 phy->enable_completion = NULL;
1263 phy->reset_completion = NULL;
1265 /* 3. Wait for Port Reset complete or
1268 pm8001_dbg(pm8001_ha, MSG,
1269 "Waiting for Port reset\n");
1270 ret = wait_for_completion_timeout(
1272 PM8001_TASK_TIMEOUT * HZ);
1274 phy->reset_completion = NULL;
1275 WARN_ON(phy->port_reset_status ==
1277 if (phy->port_reset_status == PORT_RESET_TMO) {
1278 pm8001_dev_gone_notify(dev);
1285 * we wait for the task to be aborted so that the task
1286 * is removed from the ccb. on success the caller is
1287 * going to free the task.
1289 ret = pm8001_exec_internal_task_abort(pm8001_ha,
1290 pm8001_dev, pm8001_dev->sas_device, 1, tag);
1293 ret = wait_for_completion_timeout(
1294 &task->slow_task->completion,
1295 PM8001_TASK_TIMEOUT * HZ);
1299 /* 5. Set Device State as Operational */
1300 reinit_completion(&completion);
1301 pm8001_dev->setds_completion = &completion;
1302 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1304 wait_for_completion(&completion);
1306 rc = pm8001_exec_internal_task_abort(pm8001_ha,
1307 pm8001_dev, pm8001_dev->sas_device, 0, tag);
1309 rc = TMF_RESP_FUNC_COMPLETE;
1310 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1312 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1313 pm8001_dev->sas_device, 0, tag);
1317 spin_lock_irqsave(&task->task_state_lock, flags);
1318 if (task->slow_task == &slow_task)
1319 task->slow_task = NULL;
1320 spin_unlock_irqrestore(&task->task_state_lock, flags);
1321 if (rc != TMF_RESP_FUNC_COMPLETE)
1322 pm8001_info(pm8001_ha, "rc= %d\n", rc);
1326 int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1328 struct pm8001_tmf_task tmf_task;
1330 tmf_task.tmf = TMF_ABORT_TASK_SET;
1331 return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1334 int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1336 struct pm8001_tmf_task tmf_task;
1338 tmf_task.tmf = TMF_CLEAR_ACA;
1339 return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1342 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1344 struct pm8001_tmf_task tmf_task;
1345 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1346 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1348 pm8001_dbg(pm8001_ha, EH, "I_T_L_Q clear task set[%x]\n",
1349 pm8001_dev->device_id);
1350 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1351 return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);