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"
43 #include "pm80xx_tracepoints.h"
46 * pm8001_find_tag - from sas task to find out tag that belongs to this task
47 * @task: the task sent to the LLDD
48 * @tag: the found tag associated with the task
50 static int pm8001_find_tag(struct sas_task *task, u32 *tag)
52 if (task->lldd_task) {
53 struct pm8001_ccb_info *ccb;
54 ccb = task->lldd_task;
62 * pm8001_tag_free - free the no more needed tag
63 * @pm8001_ha: our hba struct
64 * @tag: the found tag associated with the task
66 void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
68 void *bitmap = pm8001_ha->rsvd_tags;
71 if (tag >= PM8001_RESERVE_SLOT)
74 spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags);
75 __clear_bit(tag, bitmap);
76 spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
80 * pm8001_tag_alloc - allocate a empty tag for task used.
81 * @pm8001_ha: our hba struct
82 * @tag_out: the found empty tag .
84 int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
86 void *bitmap = pm8001_ha->rsvd_tags;
90 spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags);
91 tag = find_first_zero_bit(bitmap, PM8001_RESERVE_SLOT);
92 if (tag >= PM8001_RESERVE_SLOT) {
93 spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
94 return -SAS_QUEUE_FULL;
96 __set_bit(tag, bitmap);
97 spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
99 /* reserved tags are in the lower region of the tagset */
105 * pm8001_mem_alloc - allocate memory for pm8001.
107 * @virt_addr: the allocated virtual address
108 * @pphys_addr: DMA address for this device
109 * @pphys_addr_hi: the physical address high byte address.
110 * @pphys_addr_lo: the physical address low byte address.
111 * @mem_size: memory size.
112 * @align: requested byte alignment
114 int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr,
115 dma_addr_t *pphys_addr, u32 *pphys_addr_hi,
116 u32 *pphys_addr_lo, u32 mem_size, u32 align)
118 caddr_t mem_virt_alloc;
119 dma_addr_t mem_dma_handle;
121 u64 align_offset = 0;
123 align_offset = (dma_addr_t)align - 1;
124 mem_virt_alloc = dma_alloc_coherent(&pdev->dev, mem_size + align,
125 &mem_dma_handle, GFP_KERNEL);
128 *pphys_addr = mem_dma_handle;
129 phys_align = (*pphys_addr + align_offset) & ~align_offset;
130 *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
131 *pphys_addr_hi = upper_32_bits(phys_align);
132 *pphys_addr_lo = lower_32_bits(phys_align);
137 * pm8001_find_ha_by_dev - from domain device which come from sas layer to
138 * find out our hba struct.
139 * @dev: the domain device which from sas layer.
142 struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
144 struct sas_ha_struct *sha = dev->port->ha;
145 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
150 * pm8001_phy_control - this function should be registered to
151 * sas_domain_function_template to provide libsas used, note: this is just
152 * control the HBA phy rather than other expander phy if you want control
153 * other phy, you should use SMP command.
154 * @sas_phy: which phy in HBA phys.
155 * @func: the operation.
156 * @funcdata: always NULL.
158 int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
161 int rc = 0, phy_id = sas_phy->id;
162 struct pm8001_hba_info *pm8001_ha = NULL;
163 struct sas_phy_linkrates *rates;
164 struct pm8001_phy *phy;
165 DECLARE_COMPLETION_ONSTACK(completion);
167 pm8001_ha = sas_phy->ha->lldd_ha;
168 phy = &pm8001_ha->phy[phy_id];
169 pm8001_ha->phy[phy_id].enable_completion = &completion;
171 case PHY_FUNC_SET_LINK_RATE:
173 if (rates->minimum_linkrate) {
174 pm8001_ha->phy[phy_id].minimum_linkrate =
175 rates->minimum_linkrate;
177 if (rates->maximum_linkrate) {
178 pm8001_ha->phy[phy_id].maximum_linkrate =
179 rates->maximum_linkrate;
181 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
182 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
183 wait_for_completion(&completion);
185 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
188 case PHY_FUNC_HARD_RESET:
189 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
190 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
191 wait_for_completion(&completion);
193 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
196 case PHY_FUNC_LINK_RESET:
197 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
198 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
199 wait_for_completion(&completion);
201 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
204 case PHY_FUNC_RELEASE_SPINUP_HOLD:
205 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
208 case PHY_FUNC_DISABLE:
209 if (pm8001_ha->chip_id != chip_8001) {
210 if (pm8001_ha->phy[phy_id].phy_state ==
211 PHY_STATE_LINK_UP_SPCV) {
212 sas_phy_disconnected(&phy->sas_phy);
213 sas_notify_phy_event(&phy->sas_phy,
214 PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
215 phy->phy_attached = 0;
218 if (pm8001_ha->phy[phy_id].phy_state ==
219 PHY_STATE_LINK_UP_SPC) {
220 sas_phy_disconnected(&phy->sas_phy);
221 sas_notify_phy_event(&phy->sas_phy,
222 PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
223 phy->phy_attached = 0;
226 PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
228 case PHY_FUNC_GET_EVENTS:
229 spin_lock_irqsave(&pm8001_ha->lock, flags);
230 if (pm8001_ha->chip_id == chip_8001) {
231 if (-1 == pm8001_bar4_shift(pm8001_ha,
232 (phy_id < 4) ? 0x30000 : 0x40000)) {
233 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
238 struct sas_phy *phy = sas_phy->phy;
239 u32 __iomem *qp = pm8001_ha->io_mem[2].memvirtaddr
240 + 0x1034 + (0x4000 * (phy_id & 3));
242 phy->invalid_dword_count = readl(qp);
243 phy->running_disparity_error_count = readl(&qp[1]);
244 phy->loss_of_dword_sync_count = readl(&qp[3]);
245 phy->phy_reset_problem_count = readl(&qp[4]);
247 if (pm8001_ha->chip_id == chip_8001)
248 pm8001_bar4_shift(pm8001_ha, 0);
249 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
252 pm8001_dbg(pm8001_ha, DEVIO, "func 0x%x\n", func);
260 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
262 * @shost: the scsi host data.
264 void pm8001_scan_start(struct Scsi_Host *shost)
267 struct pm8001_hba_info *pm8001_ha;
268 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
269 DECLARE_COMPLETION_ONSTACK(completion);
270 pm8001_ha = sha->lldd_ha;
271 /* SAS_RE_INITIALIZATION not available in SPCv/ve */
272 if (pm8001_ha->chip_id == chip_8001)
273 PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
274 for (i = 0; i < pm8001_ha->chip->n_phy; ++i) {
275 pm8001_ha->phy[i].enable_completion = &completion;
276 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
277 wait_for_completion(&completion);
282 int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
284 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
286 /* give the phy enabling interrupt event time to come in (1s
287 * is empirically about all it takes) */
290 /* Wait for discovery to finish */
296 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
297 * @pm8001_ha: our hba card information
298 * @ccb: the ccb which attached to smp task
300 static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
301 struct pm8001_ccb_info *ccb)
303 return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
306 u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
308 struct ata_queued_cmd *qc = task->uldd_task;
310 if (qc && ata_is_ncq(qc->tf.protocol)) {
319 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
320 * @pm8001_ha: our hba card information
321 * @ccb: the ccb which attached to sata task
323 static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
324 struct pm8001_ccb_info *ccb)
326 return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
330 * pm8001_task_prep_internal_abort - the dispatcher function, prepare data
331 * for internal abort task
332 * @pm8001_ha: our hba card information
333 * @ccb: the ccb which attached to sata task
335 static int pm8001_task_prep_internal_abort(struct pm8001_hba_info *pm8001_ha,
336 struct pm8001_ccb_info *ccb)
338 return PM8001_CHIP_DISP->task_abort(pm8001_ha, ccb);
342 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
343 * @pm8001_ha: our hba card information
344 * @ccb: the ccb which attached to TM
345 * @tmf: the task management IU
347 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
348 struct pm8001_ccb_info *ccb, struct sas_tmf_task *tmf)
350 return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
354 * pm8001_task_prep_ssp - the dispatcher function, prepare ssp data for ssp task
355 * @pm8001_ha: our hba card information
356 * @ccb: the ccb which attached to ssp task
358 static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
359 struct pm8001_ccb_info *ccb)
361 return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
364 /* Find the local port id that's attached to this device */
365 static int sas_find_local_port_id(struct domain_device *dev)
367 struct domain_device *pdev = dev->parent;
369 /* Directly attached device */
371 return dev->port->id;
373 struct domain_device *pdev_p = pdev->parent;
375 return pdev->port->id;
381 #define DEV_IS_GONE(pm8001_dev) \
382 ((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
385 static int pm8001_deliver_command(struct pm8001_hba_info *pm8001_ha,
386 struct pm8001_ccb_info *ccb)
388 struct sas_task *task = ccb->task;
389 enum sas_protocol task_proto = task->task_proto;
390 struct sas_tmf_task *tmf = task->tmf;
393 switch (task_proto) {
394 case SAS_PROTOCOL_SMP:
395 return pm8001_task_prep_smp(pm8001_ha, ccb);
396 case SAS_PROTOCOL_SSP:
398 return pm8001_task_prep_ssp_tm(pm8001_ha, ccb, tmf);
399 return pm8001_task_prep_ssp(pm8001_ha, ccb);
400 case SAS_PROTOCOL_SATA:
401 case SAS_PROTOCOL_STP:
402 return pm8001_task_prep_ata(pm8001_ha, ccb);
403 case SAS_PROTOCOL_INTERNAL_ABORT:
404 return pm8001_task_prep_internal_abort(pm8001_ha, ccb);
406 dev_err(pm8001_ha->dev, "unknown sas_task proto: 0x%x\n",
414 * pm8001_queue_command - register for upper layer used, all IO commands sent
415 * to HBA are from this interface.
416 * @task: the task to be execute.
417 * @gfp_flags: gfp_flags
419 int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags)
421 struct task_status_struct *ts = &task->task_status;
422 enum sas_protocol task_proto = task->task_proto;
423 struct domain_device *dev = task->dev;
424 struct pm8001_device *pm8001_dev = dev->lldd_dev;
425 bool internal_abort = sas_is_internal_abort(task);
426 struct pm8001_hba_info *pm8001_ha;
427 struct pm8001_port *port = NULL;
428 struct pm8001_ccb_info *ccb;
433 if (!internal_abort && !dev->port) {
434 ts->resp = SAS_TASK_UNDELIVERED;
435 ts->stat = SAS_PHY_DOWN;
436 if (dev->dev_type != SAS_SATA_DEV)
437 task->task_done(task);
441 pm8001_ha = pm8001_find_ha_by_dev(dev);
442 if (pm8001_ha->controller_fatal_error) {
443 ts->resp = SAS_TASK_UNDELIVERED;
444 task->task_done(task);
448 pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec device\n");
450 spin_lock_irqsave(&pm8001_ha->lock, flags);
452 pm8001_dev = dev->lldd_dev;
453 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
455 if (!internal_abort &&
456 (DEV_IS_GONE(pm8001_dev) || !port->port_attached)) {
457 ts->resp = SAS_TASK_UNDELIVERED;
458 ts->stat = SAS_PHY_DOWN;
459 if (sas_protocol_ata(task_proto)) {
460 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
461 task->task_done(task);
462 spin_lock_irqsave(&pm8001_ha->lock, flags);
464 task->task_done(task);
470 ccb = pm8001_ccb_alloc(pm8001_ha, pm8001_dev, task);
472 rc = -SAS_QUEUE_FULL;
476 if (!sas_protocol_ata(task_proto)) {
477 if (task->num_scatter) {
478 n_elem = dma_map_sg(pm8001_ha->dev, task->scatter,
479 task->num_scatter, task->data_dir);
486 n_elem = task->num_scatter;
489 task->lldd_task = ccb;
490 ccb->n_elem = n_elem;
492 atomic_inc(&pm8001_dev->running_req);
494 rc = pm8001_deliver_command(pm8001_ha, ccb);
496 atomic_dec(&pm8001_dev->running_req);
497 if (!sas_protocol_ata(task_proto) && n_elem)
498 dma_unmap_sg(pm8001_ha->dev, task->scatter,
499 task->num_scatter, task->data_dir);
501 pm8001_ccb_free(pm8001_ha, ccb);
504 pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec failed[%d]!\n", rc);
507 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
513 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
514 * @pm8001_ha: our hba card information
515 * @ccb: the ccb which attached to ssp task to free
517 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
518 struct pm8001_ccb_info *ccb)
520 struct sas_task *task = ccb->task;
521 struct ata_queued_cmd *qc;
522 struct pm8001_device *pm8001_dev;
527 if (!sas_protocol_ata(task->task_proto) && ccb->n_elem)
528 dma_unmap_sg(pm8001_ha->dev, task->scatter,
529 task->num_scatter, task->data_dir);
531 switch (task->task_proto) {
532 case SAS_PROTOCOL_SMP:
533 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
535 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
539 case SAS_PROTOCOL_SATA:
540 case SAS_PROTOCOL_STP:
541 case SAS_PROTOCOL_SSP:
547 if (sas_protocol_ata(task->task_proto)) {
548 /* For SCSI/ATA commands uldd_task points to ata_queued_cmd */
549 qc = task->uldd_task;
550 pm8001_dev = ccb->device;
551 trace_pm80xx_request_complete(pm8001_ha->id,
552 pm8001_dev ? pm8001_dev->attached_phy : PM8001_MAX_PHYS,
553 ccb->ccb_tag, 0 /* ctlr_opcode not known */,
554 qc ? qc->tf.command : 0, // ata opcode
555 pm8001_dev ? atomic_read(&pm8001_dev->running_req) : -1);
558 task->lldd_task = NULL;
559 pm8001_ccb_free(pm8001_ha, ccb);
563 * pm8001_alloc_dev - find a empty pm8001_device
564 * @pm8001_ha: our hba card information
566 static struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
569 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
570 if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) {
571 pm8001_ha->devices[dev].id = dev;
572 return &pm8001_ha->devices[dev];
575 if (dev == PM8001_MAX_DEVICES) {
576 pm8001_dbg(pm8001_ha, FAIL,
577 "max support %d devices, ignore ..\n",
583 * pm8001_find_dev - find a matching pm8001_device
584 * @pm8001_ha: our hba card information
585 * @device_id: device ID to match against
587 struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
591 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
592 if (pm8001_ha->devices[dev].device_id == device_id)
593 return &pm8001_ha->devices[dev];
595 if (dev == PM8001_MAX_DEVICES) {
596 pm8001_dbg(pm8001_ha, FAIL, "NO MATCHING DEVICE FOUND !!!\n");
601 void pm8001_free_dev(struct pm8001_device *pm8001_dev)
603 u32 id = pm8001_dev->id;
604 memset(pm8001_dev, 0, sizeof(*pm8001_dev));
606 pm8001_dev->dev_type = SAS_PHY_UNUSED;
607 pm8001_dev->device_id = PM8001_MAX_DEVICES;
608 pm8001_dev->sas_device = NULL;
612 * pm8001_dev_found_notify - libsas notify a device is found.
613 * @dev: the device structure which sas layer used.
615 * when libsas find a sas domain device, it should tell the LLDD that
616 * device is found, and then LLDD register this device to HBA firmware
617 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
618 * device ID(according to device's sas address) and returned it to LLDD. From
619 * now on, we communicate with HBA FW with the device ID which HBA assigned
620 * rather than sas address. it is the necessary step for our HBA but it is
621 * the optional for other HBA driver.
623 static int pm8001_dev_found_notify(struct domain_device *dev)
625 unsigned long flags = 0;
627 struct pm8001_hba_info *pm8001_ha = NULL;
628 struct domain_device *parent_dev = dev->parent;
629 struct pm8001_device *pm8001_device;
630 DECLARE_COMPLETION_ONSTACK(completion);
632 pm8001_ha = pm8001_find_ha_by_dev(dev);
633 spin_lock_irqsave(&pm8001_ha->lock, flags);
635 pm8001_device = pm8001_alloc_dev(pm8001_ha);
636 if (!pm8001_device) {
640 pm8001_device->sas_device = dev;
641 dev->lldd_dev = pm8001_device;
642 pm8001_device->dev_type = dev->dev_type;
643 pm8001_device->dcompletion = &completion;
644 if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
647 phy_id = sas_find_attached_phy_id(&parent_dev->ex_dev, dev);
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));
655 pm8001_device->attached_phy = phy_id;
658 if (dev->dev_type == SAS_SATA_DEV) {
659 pm8001_device->attached_phy =
660 dev->rphy->identify.phy_identifier;
661 flag = 1; /* directly sata */
663 } /*register this device to HBA*/
664 pm8001_dbg(pm8001_ha, DISC, "Found device\n");
665 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
666 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
667 wait_for_completion(&completion);
668 if (dev->dev_type == SAS_END_DEVICE)
670 pm8001_ha->flags = PM8001F_RUN_TIME;
673 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
677 int pm8001_dev_found(struct domain_device *dev)
679 return pm8001_dev_found_notify(dev);
682 #define PM8001_TASK_TIMEOUT 20
685 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
686 * @dev: the device structure which sas layer used.
688 static void pm8001_dev_gone_notify(struct domain_device *dev)
690 unsigned long flags = 0;
691 struct pm8001_hba_info *pm8001_ha;
692 struct pm8001_device *pm8001_dev = dev->lldd_dev;
694 pm8001_ha = pm8001_find_ha_by_dev(dev);
695 spin_lock_irqsave(&pm8001_ha->lock, flags);
697 u32 device_id = pm8001_dev->device_id;
699 pm8001_dbg(pm8001_ha, DISC, "found dev[%d:%x] is gone.\n",
700 pm8001_dev->device_id, pm8001_dev->dev_type);
701 if (atomic_read(&pm8001_dev->running_req)) {
702 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
703 sas_execute_internal_abort_dev(dev, 0, NULL);
704 while (atomic_read(&pm8001_dev->running_req))
706 spin_lock_irqsave(&pm8001_ha->lock, flags);
708 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
709 pm8001_free_dev(pm8001_dev);
711 pm8001_dbg(pm8001_ha, DISC, "Found dev has gone.\n");
713 dev->lldd_dev = NULL;
714 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
717 void pm8001_dev_gone(struct domain_device *dev)
719 pm8001_dev_gone_notify(dev);
722 /* retry commands by ha, by task and/or by device */
723 void pm8001_open_reject_retry(
724 struct pm8001_hba_info *pm8001_ha,
725 struct sas_task *task_to_close,
726 struct pm8001_device *device_to_close)
731 if (pm8001_ha == NULL)
734 spin_lock_irqsave(&pm8001_ha->lock, flags);
736 for (i = 0; i < PM8001_MAX_CCB; i++) {
737 struct sas_task *task;
738 struct task_status_struct *ts;
739 struct pm8001_device *pm8001_dev;
740 unsigned long flags1;
741 struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
743 if (ccb->ccb_tag == PM8001_INVALID_TAG)
746 pm8001_dev = ccb->device;
747 if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
749 if (!device_to_close) {
750 uintptr_t d = (uintptr_t)pm8001_dev
751 - (uintptr_t)&pm8001_ha->devices;
752 if (((d % sizeof(*pm8001_dev)) != 0)
753 || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
755 } else if (pm8001_dev != device_to_close)
758 if (!task || !task->task_done)
760 if (task_to_close && (task != task_to_close))
762 ts = &task->task_status;
763 ts->resp = SAS_TASK_COMPLETE;
764 /* Force the midlayer to retry */
765 ts->stat = SAS_OPEN_REJECT;
766 ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
768 atomic_dec(&pm8001_dev->running_req);
769 spin_lock_irqsave(&task->task_state_lock, flags1);
770 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
771 task->task_state_flags |= SAS_TASK_STATE_DONE;
772 if (unlikely((task->task_state_flags
773 & SAS_TASK_STATE_ABORTED))) {
774 spin_unlock_irqrestore(&task->task_state_lock,
776 pm8001_ccb_task_free(pm8001_ha, ccb);
778 spin_unlock_irqrestore(&task->task_state_lock,
780 pm8001_ccb_task_free(pm8001_ha, ccb);
781 mb();/* in order to force CPU ordering */
782 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
783 task->task_done(task);
784 spin_lock_irqsave(&pm8001_ha->lock, flags);
788 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
792 * pm8001_I_T_nexus_reset() - reset the initiator/target connection
793 * @dev: the device structure for the device to reset.
795 * Standard mandates link reset for ATA (type 0) and hard reset for
796 * SSP (type 1), only for RECOVERY
798 int pm8001_I_T_nexus_reset(struct domain_device *dev)
800 int rc = TMF_RESP_FUNC_FAILED;
801 struct pm8001_device *pm8001_dev;
802 struct pm8001_hba_info *pm8001_ha;
805 if (!dev || !dev->lldd_dev)
808 pm8001_dev = dev->lldd_dev;
809 pm8001_ha = pm8001_find_ha_by_dev(dev);
810 phy = sas_get_local_phy(dev);
812 if (dev_is_sata(dev)) {
813 if (scsi_is_sas_phy_local(phy)) {
817 rc = sas_phy_reset(phy, 1);
819 pm8001_dbg(pm8001_ha, EH,
820 "phy reset failed for device %x\n"
821 "with rc %d\n", pm8001_dev->device_id, rc);
822 rc = TMF_RESP_FUNC_FAILED;
826 rc = sas_execute_internal_abort_dev(dev, 0, NULL);
828 pm8001_dbg(pm8001_ha, EH, "task abort failed %x\n"
829 "with rc %d\n", pm8001_dev->device_id, rc);
830 rc = TMF_RESP_FUNC_FAILED;
833 rc = sas_phy_reset(phy, 1);
836 pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
837 pm8001_dev->device_id, rc);
839 sas_put_local_phy(phy);
844 * This function handle the IT_NEXUS_XXX event or completion
845 * status code for SSP/SATA/SMP I/O request.
847 int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
849 int rc = TMF_RESP_FUNC_FAILED;
850 struct pm8001_device *pm8001_dev;
851 struct pm8001_hba_info *pm8001_ha;
854 if (!dev || !dev->lldd_dev)
857 pm8001_dev = dev->lldd_dev;
858 pm8001_ha = pm8001_find_ha_by_dev(dev);
860 pm8001_dbg(pm8001_ha, EH, "I_T_Nexus handler invoked !!\n");
862 phy = sas_get_local_phy(dev);
864 if (dev_is_sata(dev)) {
865 DECLARE_COMPLETION_ONSTACK(completion_setstate);
866 if (scsi_is_sas_phy_local(phy)) {
870 /* send internal ssp/sata/smp abort command to FW */
871 sas_execute_internal_abort_dev(dev, 0, NULL);
874 /* deregister the target device */
875 pm8001_dev_gone_notify(dev);
878 /*send phy reset to hard reset target */
879 rc = sas_phy_reset(phy, 1);
881 pm8001_dev->setds_completion = &completion_setstate;
883 wait_for_completion(&completion_setstate);
885 /* send internal ssp/sata/smp abort command to FW */
886 sas_execute_internal_abort_dev(dev, 0, NULL);
889 /* deregister the target device */
890 pm8001_dev_gone_notify(dev);
893 /*send phy reset to hard reset target */
894 rc = sas_phy_reset(phy, 1);
897 pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
898 pm8001_dev->device_id, rc);
900 sas_put_local_phy(phy);
904 /* mandatory SAM-3, the task reset the specified LUN*/
905 int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
907 int rc = TMF_RESP_FUNC_FAILED;
908 struct pm8001_device *pm8001_dev = dev->lldd_dev;
909 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
910 DECLARE_COMPLETION_ONSTACK(completion_setstate);
911 if (dev_is_sata(dev)) {
912 struct sas_phy *phy = sas_get_local_phy(dev);
913 sas_execute_internal_abort_dev(dev, 0, NULL);
914 rc = sas_phy_reset(phy, 1);
915 sas_put_local_phy(phy);
916 pm8001_dev->setds_completion = &completion_setstate;
917 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
918 pm8001_dev, DS_OPERATIONAL);
919 wait_for_completion(&completion_setstate);
921 rc = sas_lu_reset(dev, lun);
923 /* If failed, fall-through I_T_Nexus reset */
924 pm8001_dbg(pm8001_ha, EH, "for device[%x]:rc=%d\n",
925 pm8001_dev->device_id, rc);
930 int pm8001_query_task(struct sas_task *task)
932 u32 tag = 0xdeadbeef;
933 int rc = TMF_RESP_FUNC_FAILED;
934 if (unlikely(!task || !task->lldd_task || !task->dev))
937 if (task->task_proto & SAS_PROTOCOL_SSP) {
938 struct scsi_cmnd *cmnd = task->uldd_task;
939 struct domain_device *dev = task->dev;
940 struct pm8001_hba_info *pm8001_ha =
941 pm8001_find_ha_by_dev(dev);
943 rc = pm8001_find_tag(task, &tag);
945 rc = TMF_RESP_FUNC_FAILED;
948 pm8001_dbg(pm8001_ha, EH, "Query:[%16ph]\n", cmnd->cmnd);
950 rc = sas_query_task(task, tag);
952 /* The task is still in Lun, release it then */
953 case TMF_RESP_FUNC_SUCC:
954 pm8001_dbg(pm8001_ha, EH,
955 "The task is still in Lun\n");
957 /* The task is not in Lun or failed, reset the phy */
958 case TMF_RESP_FUNC_FAILED:
959 case TMF_RESP_FUNC_COMPLETE:
960 pm8001_dbg(pm8001_ha, EH,
961 "The task is not in Lun or failed, reset the phy\n");
965 pr_err("pm80xx: rc= %d\n", rc);
969 /* mandatory SAM-3, still need free task/ccb info, abort the specified task */
970 int pm8001_abort_task(struct sas_task *task)
972 struct pm8001_ccb_info *ccb = task->lldd_task;
975 struct domain_device *dev ;
976 struct pm8001_hba_info *pm8001_ha;
977 struct pm8001_device *pm8001_dev;
978 int rc = TMF_RESP_FUNC_FAILED, ret;
980 struct sas_task_slow slow_task;
982 if (!task->lldd_task || !task->dev)
983 return TMF_RESP_FUNC_FAILED;
986 pm8001_dev = dev->lldd_dev;
987 pm8001_ha = pm8001_find_ha_by_dev(dev);
988 phy_id = pm8001_dev->attached_phy;
990 if (PM8001_CHIP_DISP->fatal_errors(pm8001_ha)) {
991 // If the controller is seeing fatal errors
992 // abort task will not get a response from the controller
993 return TMF_RESP_FUNC_FAILED;
996 ret = pm8001_find_tag(task, &tag);
998 pm8001_info(pm8001_ha, "no tag for task:%p\n", task);
999 return TMF_RESP_FUNC_FAILED;
1001 spin_lock_irqsave(&task->task_state_lock, flags);
1002 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1003 spin_unlock_irqrestore(&task->task_state_lock, flags);
1004 return TMF_RESP_FUNC_COMPLETE;
1006 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1007 if (task->slow_task == NULL) {
1008 init_completion(&slow_task.completion);
1009 task->slow_task = &slow_task;
1011 spin_unlock_irqrestore(&task->task_state_lock, flags);
1012 if (task->task_proto & SAS_PROTOCOL_SSP) {
1013 rc = sas_abort_task(task, tag);
1014 sas_execute_internal_abort_single(dev, tag, 0, NULL);
1015 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1016 task->task_proto & SAS_PROTOCOL_STP) {
1017 if (pm8001_ha->chip_id == chip_8006) {
1018 DECLARE_COMPLETION_ONSTACK(completion_reset);
1019 DECLARE_COMPLETION_ONSTACK(completion);
1020 struct pm8001_phy *phy = pm8001_ha->phy + phy_id;
1021 port_id = phy->port->port_id;
1023 /* 1. Set Device state as Recovery */
1024 pm8001_dev->setds_completion = &completion;
1025 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1026 pm8001_dev, DS_IN_RECOVERY);
1027 wait_for_completion(&completion);
1029 /* 2. Send Phy Control Hard Reset */
1030 reinit_completion(&completion);
1031 phy->port_reset_status = PORT_RESET_TMO;
1032 phy->reset_success = false;
1033 phy->enable_completion = &completion;
1034 phy->reset_completion = &completion_reset;
1035 ret = PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
1038 phy->enable_completion = NULL;
1039 phy->reset_completion = NULL;
1043 /* In the case of the reset timeout/fail we still
1044 * abort the command at the firmware. The assumption
1045 * here is that the drive is off doing something so
1046 * that it's not processing requests, and we want to
1047 * avoid getting a completion for this and either
1048 * leaking the task in libsas or losing the race and
1049 * getting a double free.
1051 pm8001_dbg(pm8001_ha, MSG,
1052 "Waiting for local phy ctl\n");
1053 ret = wait_for_completion_timeout(&completion,
1054 PM8001_TASK_TIMEOUT * HZ);
1055 if (!ret || !phy->reset_success) {
1056 phy->enable_completion = NULL;
1057 phy->reset_completion = NULL;
1059 /* 3. Wait for Port Reset complete or
1062 pm8001_dbg(pm8001_ha, MSG,
1063 "Waiting for Port reset\n");
1064 ret = wait_for_completion_timeout(
1066 PM8001_TASK_TIMEOUT * HZ);
1068 phy->reset_completion = NULL;
1069 WARN_ON(phy->port_reset_status ==
1071 if (phy->port_reset_status == PORT_RESET_TMO) {
1072 pm8001_dev_gone_notify(dev);
1073 PM8001_CHIP_DISP->hw_event_ack_req(
1075 0x07, /*HW_EVENT_PHY_DOWN ack*/
1076 port_id, phy_id, 0, 0);
1083 * we wait for the task to be aborted so that the task
1084 * is removed from the ccb. on success the caller is
1085 * going to free the task.
1087 ret = sas_execute_internal_abort_dev(dev, 0, NULL);
1090 ret = wait_for_completion_timeout(
1091 &task->slow_task->completion,
1092 PM8001_TASK_TIMEOUT * HZ);
1096 /* 5. Set Device State as Operational */
1097 reinit_completion(&completion);
1098 pm8001_dev->setds_completion = &completion;
1099 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1100 pm8001_dev, DS_OPERATIONAL);
1101 wait_for_completion(&completion);
1104 * Ensure that if we see a completion for the ccb
1105 * associated with the task which we are trying to
1106 * abort then we should not touch the sas_task as it
1107 * may race with libsas freeing it when return here.
1110 ret = sas_execute_internal_abort_single(dev, tag, 0, NULL);
1112 rc = TMF_RESP_FUNC_COMPLETE;
1113 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1115 rc = sas_execute_internal_abort_single(dev, tag, 0, NULL);
1119 spin_lock_irqsave(&task->task_state_lock, flags);
1120 if (task->slow_task == &slow_task)
1121 task->slow_task = NULL;
1122 spin_unlock_irqrestore(&task->task_state_lock, flags);
1123 if (rc != TMF_RESP_FUNC_COMPLETE)
1124 pm8001_info(pm8001_ha, "rc= %d\n", rc);
1128 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1130 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1131 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1133 pm8001_dbg(pm8001_ha, EH, "I_T_L_Q clear task set[%x]\n",
1134 pm8001_dev->device_id);
1135 return sas_clear_task_set(dev, lun);
1138 void pm8001_port_formed(struct asd_sas_phy *sas_phy)
1140 struct sas_ha_struct *sas_ha = sas_phy->ha;
1141 struct pm8001_hba_info *pm8001_ha = sas_ha->lldd_ha;
1142 struct pm8001_phy *phy = sas_phy->lldd_phy;
1143 struct asd_sas_port *sas_port = sas_phy->port;
1144 struct pm8001_port *port = phy->port;
1147 pm8001_dbg(pm8001_ha, FAIL, "Received null port\n");
1150 sas_port->lldd_port = port;
1153 void pm8001_setds_completion(struct domain_device *dev)
1155 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1156 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1157 DECLARE_COMPLETION_ONSTACK(completion_setstate);
1159 if (pm8001_ha->chip_id != chip_8001) {
1160 pm8001_dev->setds_completion = &completion_setstate;
1161 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1162 pm8001_dev, DS_OPERATIONAL);
1163 wait_for_completion(&completion_setstate);
1167 void pm8001_tmf_aborted(struct sas_task *task)
1169 struct pm8001_ccb_info *ccb = task->lldd_task;