6f33d821e5453d14076b9f6561d6d3e774b5266c
[linux-2.6-microblaze.git] / drivers / scsi / pm8001 / pm8001_sas.c
1 /*
2  * PMC-Sierra PM8001/8081/8088/8089 SAS/SATA based host adapters driver
3  *
4  * Copyright (c) 2008-2009 USI Co., Ltd.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    substantially similar to the "NO WARRANTY" disclaimer below
15  *    ("Disclaimer") and any redistribution must be conditioned upon
16  *    including a substantially similar Disclaimer requirement for further
17  *    binary redistribution.
18  * 3. Neither the names of the above-listed copyright holders nor the names
19  *    of any contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * Alternatively, this software may be distributed under the terms of the
23  * GNU General Public License ("GPL") version 2 as published by the Free
24  * Software Foundation.
25  *
26  * NO WARRANTY
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGES.
38  *
39  */
40
41 #include <linux/slab.h>
42 #include "pm8001_sas.h"
43
44 /**
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
48  */
49 static int pm8001_find_tag(struct sas_task *task, u32 *tag)
50 {
51         if (task->lldd_task) {
52                 struct pm8001_ccb_info *ccb;
53                 ccb = task->lldd_task;
54                 *tag = ccb->ccb_tag;
55                 return 1;
56         }
57         return 0;
58 }
59
60 /**
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
64   */
65 void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
66 {
67         void *bitmap = pm8001_ha->tags;
68         clear_bit(tag, bitmap);
69 }
70
71 /**
72   * pm8001_tag_alloc - allocate a empty tag for task used.
73   * @pm8001_ha: our hba struct
74   * @tag_out: the found empty tag .
75   */
76 inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
77 {
78         unsigned int tag;
79         void *bitmap = pm8001_ha->tags;
80         unsigned long flags;
81
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;
87         }
88         set_bit(tag, bitmap);
89         spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags);
90         *tag_out = tag;
91         return 0;
92 }
93
94 void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha)
95 {
96         int i;
97         for (i = 0; i < pm8001_ha->tags_num; ++i)
98                 pm8001_tag_free(pm8001_ha, i);
99 }
100
101  /**
102   * pm8001_mem_alloc - allocate memory for pm8001.
103   * @pdev: pci device.
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.
108   */
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)
112 {
113         caddr_t mem_virt_alloc;
114         dma_addr_t mem_dma_handle;
115         u64 phys_align;
116         u64 align_offset = 0;
117         if (align)
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                 return -ENOMEM;
123         *pphys_addr = mem_dma_handle;
124         phys_align = (*pphys_addr + align_offset) & ~align_offset;
125         *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
126         *pphys_addr_hi = upper_32_bits(phys_align);
127         *pphys_addr_lo = lower_32_bits(phys_align);
128         return 0;
129 }
130
131 /**
132   * pm8001_find_ha_by_dev - from domain device which come from sas layer to
133   * find out our hba struct.
134   * @dev: the domain device which from sas layer.
135   */
136 static
137 struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
138 {
139         struct sas_ha_struct *sha = dev->port->ha;
140         struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
141         return pm8001_ha;
142 }
143
144 /**
145   * pm8001_phy_control - this function should be registered to
146   * sas_domain_function_template to provide libsas used, note: this is just
147   * control the HBA phy rather than other expander phy if you want control
148   * other phy, you should use SMP command.
149   * @sas_phy: which phy in HBA phys.
150   * @func: the operation.
151   * @funcdata: always NULL.
152   */
153 int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
154         void *funcdata)
155 {
156         int rc = 0, phy_id = sas_phy->id;
157         struct pm8001_hba_info *pm8001_ha = NULL;
158         struct sas_phy_linkrates *rates;
159         struct pm8001_phy *phy;
160         DECLARE_COMPLETION_ONSTACK(completion);
161         unsigned long flags;
162         pm8001_ha = sas_phy->ha->lldd_ha;
163         phy = &pm8001_ha->phy[phy_id];
164         pm8001_ha->phy[phy_id].enable_completion = &completion;
165         switch (func) {
166         case PHY_FUNC_SET_LINK_RATE:
167                 rates = funcdata;
168                 if (rates->minimum_linkrate) {
169                         pm8001_ha->phy[phy_id].minimum_linkrate =
170                                 rates->minimum_linkrate;
171                 }
172                 if (rates->maximum_linkrate) {
173                         pm8001_ha->phy[phy_id].maximum_linkrate =
174                                 rates->maximum_linkrate;
175                 }
176                 if (pm8001_ha->phy[phy_id].phy_state ==  PHY_LINK_DISABLE) {
177                         PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
178                         wait_for_completion(&completion);
179                 }
180                 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
181                                               PHY_LINK_RESET);
182                 break;
183         case PHY_FUNC_HARD_RESET:
184                 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
185                         PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
186                         wait_for_completion(&completion);
187                 }
188                 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
189                                               PHY_HARD_RESET);
190                 break;
191         case PHY_FUNC_LINK_RESET:
192                 if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) {
193                         PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
194                         wait_for_completion(&completion);
195                 }
196                 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
197                                               PHY_LINK_RESET);
198                 break;
199         case PHY_FUNC_RELEASE_SPINUP_HOLD:
200                 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
201                                               PHY_LINK_RESET);
202                 break;
203         case PHY_FUNC_DISABLE:
204                 if (pm8001_ha->chip_id != chip_8001) {
205                         if (pm8001_ha->phy[phy_id].phy_state ==
206                                 PHY_STATE_LINK_UP_SPCV) {
207                                 sas_phy_disconnected(&phy->sas_phy);
208                                 sas_notify_phy_event(&phy->sas_phy,
209                                         PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
210                                 phy->phy_attached = 0;
211                         }
212                 } else {
213                         if (pm8001_ha->phy[phy_id].phy_state ==
214                                 PHY_STATE_LINK_UP_SPC) {
215                                 sas_phy_disconnected(&phy->sas_phy);
216                                 sas_notify_phy_event(&phy->sas_phy,
217                                         PHYE_LOSS_OF_SIGNAL, GFP_KERNEL);
218                                 phy->phy_attached = 0;
219                         }
220                 }
221                 PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
222                 break;
223         case PHY_FUNC_GET_EVENTS:
224                 spin_lock_irqsave(&pm8001_ha->lock, flags);
225                 if (pm8001_ha->chip_id == chip_8001) {
226                         if (-1 == pm8001_bar4_shift(pm8001_ha,
227                                         (phy_id < 4) ? 0x30000 : 0x40000)) {
228                                 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
229                                 return -EINVAL;
230                         }
231                 }
232                 {
233                         struct sas_phy *phy = sas_phy->phy;
234                         uint32_t *qp = (uint32_t *)(((char *)
235                                 pm8001_ha->io_mem[2].memvirtaddr)
236                                 + 0x1034 + (0x4000 * (phy_id & 3)));
237
238                         phy->invalid_dword_count = qp[0];
239                         phy->running_disparity_error_count = qp[1];
240                         phy->loss_of_dword_sync_count = qp[3];
241                         phy->phy_reset_problem_count = qp[4];
242                 }
243                 if (pm8001_ha->chip_id == chip_8001)
244                         pm8001_bar4_shift(pm8001_ha, 0);
245                 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
246                 return 0;
247         default:
248                 pm8001_dbg(pm8001_ha, DEVIO, "func 0x%x\n", func);
249                 rc = -EOPNOTSUPP;
250         }
251         msleep(300);
252         return rc;
253 }
254
255 /**
256   * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
257   * command to HBA.
258   * @shost: the scsi host data.
259   */
260 void pm8001_scan_start(struct Scsi_Host *shost)
261 {
262         int i;
263         struct pm8001_hba_info *pm8001_ha;
264         struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
265         DECLARE_COMPLETION_ONSTACK(completion);
266         pm8001_ha = sha->lldd_ha;
267         /* SAS_RE_INITIALIZATION not available in SPCv/ve */
268         if (pm8001_ha->chip_id == chip_8001)
269                 PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
270         for (i = 0; i < pm8001_ha->chip->n_phy; ++i) {
271                 pm8001_ha->phy[i].enable_completion = &completion;
272                 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
273                 wait_for_completion(&completion);
274                 msleep(300);
275         }
276 }
277
278 int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
279 {
280         struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
281
282         /* give the phy enabling interrupt event time to come in (1s
283         * is empirically about all it takes) */
284         if (time < HZ)
285                 return 0;
286         /* Wait for discovery to finish */
287         sas_drain_work(ha);
288         return 1;
289 }
290
291 /**
292   * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
293   * @pm8001_ha: our hba card information
294   * @ccb: the ccb which attached to smp task
295   */
296 static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
297         struct pm8001_ccb_info *ccb)
298 {
299         return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
300 }
301
302 u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
303 {
304         struct ata_queued_cmd *qc = task->uldd_task;
305         if (qc) {
306                 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
307                     qc->tf.command == ATA_CMD_FPDMA_READ ||
308                     qc->tf.command == ATA_CMD_FPDMA_RECV ||
309                     qc->tf.command == ATA_CMD_FPDMA_SEND ||
310                     qc->tf.command == ATA_CMD_NCQ_NON_DATA) {
311                         *tag = qc->tag;
312                         return 1;
313                 }
314         }
315         return 0;
316 }
317
318 /**
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
322   */
323 static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
324         struct pm8001_ccb_info *ccb)
325 {
326         return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
327 }
328
329 /**
330   * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
331   * @pm8001_ha: our hba card information
332   * @ccb: the ccb which attached to TM
333   * @tmf: the task management IU
334   */
335 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
336         struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
337 {
338         return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
339 }
340
341 /**
342   * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
343   * @pm8001_ha: our hba card information
344   * @ccb: the ccb which attached to ssp task
345   */
346 static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
347         struct pm8001_ccb_info *ccb)
348 {
349         return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
350 }
351
352  /* Find the local port id that's attached to this device */
353 static int sas_find_local_port_id(struct domain_device *dev)
354 {
355         struct domain_device *pdev = dev->parent;
356
357         /* Directly attached device */
358         if (!pdev)
359                 return dev->port->id;
360         while (pdev) {
361                 struct domain_device *pdev_p = pdev->parent;
362                 if (!pdev_p)
363                         return pdev->port->id;
364                 pdev = pdev->parent;
365         }
366         return 0;
367 }
368
369 #define DEV_IS_GONE(pm8001_dev) \
370         ((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
371 /**
372   * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
373   * @task: the task to be execute.
374   * @gfp_flags: gfp_flags.
375   * @is_tmf: if it is task management task.
376   * @tmf: the task management IU
377   */
378 static int pm8001_task_exec(struct sas_task *task,
379         gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
380 {
381         struct domain_device *dev = task->dev;
382         struct pm8001_hba_info *pm8001_ha;
383         struct pm8001_device *pm8001_dev;
384         struct pm8001_port *port = NULL;
385         struct sas_task *t = task;
386         struct pm8001_ccb_info *ccb;
387         u32 tag = 0xdeadbeef, rc = 0, n_elem = 0;
388         unsigned long flags = 0;
389         enum sas_protocol task_proto = t->task_proto;
390
391         if (!dev->port) {
392                 struct task_status_struct *tsm = &t->task_status;
393                 tsm->resp = SAS_TASK_UNDELIVERED;
394                 tsm->stat = SAS_PHY_DOWN;
395                 if (dev->dev_type != SAS_SATA_DEV)
396                         t->task_done(t);
397                 return 0;
398         }
399         pm8001_ha = pm8001_find_ha_by_dev(task->dev);
400         if (pm8001_ha->controller_fatal_error) {
401                 struct task_status_struct *ts = &t->task_status;
402
403                 ts->resp = SAS_TASK_UNDELIVERED;
404                 t->task_done(t);
405                 return 0;
406         }
407         pm8001_dbg(pm8001_ha, IO, "pm8001_task_exec device\n");
408         spin_lock_irqsave(&pm8001_ha->lock, flags);
409         do {
410                 dev = t->dev;
411                 pm8001_dev = dev->lldd_dev;
412                 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
413                 if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
414                         if (sas_protocol_ata(task_proto)) {
415                                 struct task_status_struct *ts = &t->task_status;
416                                 ts->resp = SAS_TASK_UNDELIVERED;
417                                 ts->stat = SAS_PHY_DOWN;
418
419                                 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
420                                 t->task_done(t);
421                                 spin_lock_irqsave(&pm8001_ha->lock, flags);
422                                 continue;
423                         } else {
424                                 struct task_status_struct *ts = &t->task_status;
425                                 ts->resp = SAS_TASK_UNDELIVERED;
426                                 ts->stat = SAS_PHY_DOWN;
427                                 t->task_done(t);
428                                 continue;
429                         }
430                 }
431                 rc = pm8001_tag_alloc(pm8001_ha, &tag);
432                 if (rc)
433                         goto err_out;
434                 ccb = &pm8001_ha->ccb_info[tag];
435
436                 if (!sas_protocol_ata(task_proto)) {
437                         if (t->num_scatter) {
438                                 n_elem = dma_map_sg(pm8001_ha->dev,
439                                         t->scatter,
440                                         t->num_scatter,
441                                         t->data_dir);
442                                 if (!n_elem) {
443                                         rc = -ENOMEM;
444                                         goto err_out_tag;
445                                 }
446                         }
447                 } else {
448                         n_elem = t->num_scatter;
449                 }
450
451                 t->lldd_task = ccb;
452                 ccb->n_elem = n_elem;
453                 ccb->ccb_tag = tag;
454                 ccb->task = t;
455                 ccb->device = pm8001_dev;
456                 switch (task_proto) {
457                 case SAS_PROTOCOL_SMP:
458                         atomic_inc(&pm8001_dev->running_req);
459                         rc = pm8001_task_prep_smp(pm8001_ha, ccb);
460                         break;
461                 case SAS_PROTOCOL_SSP:
462                         atomic_inc(&pm8001_dev->running_req);
463                         if (is_tmf)
464                                 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
465                                         ccb, tmf);
466                         else
467                                 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
468                         break;
469                 case SAS_PROTOCOL_SATA:
470                 case SAS_PROTOCOL_STP:
471                         atomic_inc(&pm8001_dev->running_req);
472                         rc = pm8001_task_prep_ata(pm8001_ha, ccb);
473                         break;
474                 default:
475                         dev_printk(KERN_ERR, pm8001_ha->dev,
476                                 "unknown sas_task proto: 0x%x\n", task_proto);
477                         rc = -EINVAL;
478                         break;
479                 }
480
481                 if (rc) {
482                         pm8001_dbg(pm8001_ha, IO, "rc is %x\n", rc);
483                         atomic_dec(&pm8001_dev->running_req);
484                         goto err_out_tag;
485                 }
486                 /* TODO: select normal or high priority */
487                 spin_lock(&t->task_state_lock);
488                 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
489                 spin_unlock(&t->task_state_lock);
490         } while (0);
491         rc = 0;
492         goto out_done;
493
494 err_out_tag:
495         pm8001_tag_free(pm8001_ha, tag);
496 err_out:
497         dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
498         if (!sas_protocol_ata(task_proto))
499                 if (n_elem)
500                         dma_unmap_sg(pm8001_ha->dev, t->scatter, t->num_scatter,
501                                 t->data_dir);
502 out_done:
503         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
504         return rc;
505 }
506
507 /**
508   * pm8001_queue_command - register for upper layer used, all IO commands sent
509   * to HBA are from this interface.
510   * @task: the task to be execute.
511   * @gfp_flags: gfp_flags
512   */
513 int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags)
514 {
515         return pm8001_task_exec(task, gfp_flags, 0, NULL);
516 }
517
518 /**
519   * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
520   * @pm8001_ha: our hba card information
521   * @ccb: the ccb which attached to ssp task
522   * @task: the task to be free.
523   * @ccb_idx: ccb index.
524   */
525 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
526         struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
527 {
528         if (!ccb->task)
529                 return;
530         if (!sas_protocol_ata(task->task_proto))
531                 if (ccb->n_elem)
532                         dma_unmap_sg(pm8001_ha->dev, task->scatter,
533                                 task->num_scatter, task->data_dir);
534
535         switch (task->task_proto) {
536         case SAS_PROTOCOL_SMP:
537                 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
538                         DMA_FROM_DEVICE);
539                 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
540                         DMA_TO_DEVICE);
541                 break;
542
543         case SAS_PROTOCOL_SATA:
544         case SAS_PROTOCOL_STP:
545         case SAS_PROTOCOL_SSP:
546         default:
547                 /* do nothing */
548                 break;
549         }
550         task->lldd_task = NULL;
551         ccb->task = NULL;
552         ccb->ccb_tag = 0xFFFFFFFF;
553         ccb->open_retry = 0;
554         pm8001_tag_free(pm8001_ha, ccb_idx);
555 }
556
557  /**
558   * pm8001_alloc_dev - find a empty pm8001_device
559   * @pm8001_ha: our hba card information
560   */
561 static struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
562 {
563         u32 dev;
564         for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
565                 if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) {
566                         pm8001_ha->devices[dev].id = dev;
567                         return &pm8001_ha->devices[dev];
568                 }
569         }
570         if (dev == PM8001_MAX_DEVICES) {
571                 pm8001_dbg(pm8001_ha, FAIL,
572                            "max support %d devices, ignore ..\n",
573                            PM8001_MAX_DEVICES);
574         }
575         return NULL;
576 }
577 /**
578   * pm8001_find_dev - find a matching pm8001_device
579   * @pm8001_ha: our hba card information
580   * @device_id: device ID to match against
581   */
582 struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
583                                         u32 device_id)
584 {
585         u32 dev;
586         for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
587                 if (pm8001_ha->devices[dev].device_id == device_id)
588                         return &pm8001_ha->devices[dev];
589         }
590         if (dev == PM8001_MAX_DEVICES) {
591                 pm8001_dbg(pm8001_ha, FAIL, "NO MATCHING DEVICE FOUND !!!\n");
592         }
593         return NULL;
594 }
595
596 void pm8001_free_dev(struct pm8001_device *pm8001_dev)
597 {
598         u32 id = pm8001_dev->id;
599         memset(pm8001_dev, 0, sizeof(*pm8001_dev));
600         pm8001_dev->id = id;
601         pm8001_dev->dev_type = SAS_PHY_UNUSED;
602         pm8001_dev->device_id = PM8001_MAX_DEVICES;
603         pm8001_dev->sas_device = NULL;
604 }
605
606 /**
607   * pm8001_dev_found_notify - libsas notify a device is found.
608   * @dev: the device structure which sas layer used.
609   *
610   * when libsas find a sas domain device, it should tell the LLDD that
611   * device is found, and then LLDD register this device to HBA firmware
612   * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
613   * device ID(according to device's sas address) and returned it to LLDD. From
614   * now on, we communicate with HBA FW with the device ID which HBA assigned
615   * rather than sas address. it is the necessary step for our HBA but it is
616   * the optional for other HBA driver.
617   */
618 static int pm8001_dev_found_notify(struct domain_device *dev)
619 {
620         unsigned long flags = 0;
621         int res = 0;
622         struct pm8001_hba_info *pm8001_ha = NULL;
623         struct domain_device *parent_dev = dev->parent;
624         struct pm8001_device *pm8001_device;
625         DECLARE_COMPLETION_ONSTACK(completion);
626         u32 flag = 0;
627         pm8001_ha = pm8001_find_ha_by_dev(dev);
628         spin_lock_irqsave(&pm8001_ha->lock, flags);
629
630         pm8001_device = pm8001_alloc_dev(pm8001_ha);
631         if (!pm8001_device) {
632                 res = -1;
633                 goto found_out;
634         }
635         pm8001_device->sas_device = dev;
636         dev->lldd_dev = pm8001_device;
637         pm8001_device->dev_type = dev->dev_type;
638         pm8001_device->dcompletion = &completion;
639         if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
640                 int phy_id;
641                 struct ex_phy *phy;
642                 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
643                 phy_id++) {
644                         phy = &parent_dev->ex_dev.ex_phy[phy_id];
645                         if (SAS_ADDR(phy->attached_sas_addr)
646                                 == SAS_ADDR(dev->sas_addr)) {
647                                 pm8001_device->attached_phy = phy_id;
648                                 break;
649                         }
650                 }
651                 if (phy_id == parent_dev->ex_dev.num_phys) {
652                         pm8001_dbg(pm8001_ha, FAIL,
653                                    "Error: no attached dev:%016llx at ex:%016llx.\n",
654                                    SAS_ADDR(dev->sas_addr),
655                                    SAS_ADDR(parent_dev->sas_addr));
656                         res = -1;
657                 }
658         } else {
659                 if (dev->dev_type == SAS_SATA_DEV) {
660                         pm8001_device->attached_phy =
661                                 dev->rphy->identify.phy_identifier;
662                         flag = 1; /* directly sata */
663                 }
664         } /*register this device to HBA*/
665         pm8001_dbg(pm8001_ha, DISC, "Found device\n");
666         PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
667         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
668         wait_for_completion(&completion);
669         if (dev->dev_type == SAS_END_DEVICE)
670                 msleep(50);
671         pm8001_ha->flags = PM8001F_RUN_TIME;
672         return 0;
673 found_out:
674         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
675         return res;
676 }
677
678 int pm8001_dev_found(struct domain_device *dev)
679 {
680         return pm8001_dev_found_notify(dev);
681 }
682
683 void pm8001_task_done(struct sas_task *task)
684 {
685         if (!del_timer(&task->slow_task->timer))
686                 return;
687         complete(&task->slow_task->completion);
688 }
689
690 static void pm8001_tmf_timedout(struct timer_list *t)
691 {
692         struct sas_task_slow *slow = from_timer(slow, t, timer);
693         struct sas_task *task = slow->task;
694
695         task->task_state_flags |= SAS_TASK_STATE_ABORTED;
696         complete(&task->slow_task->completion);
697 }
698
699 #define PM8001_TASK_TIMEOUT 20
700 /**
701   * pm8001_exec_internal_tmf_task - execute some task management commands.
702   * @dev: the wanted device.
703   * @tmf: which task management wanted to be take.
704   * @para_len: para_len.
705   * @parameter: ssp task parameter.
706   *
707   * when errors or exception happened, we may want to do something, for example
708   * abort the issued task which result in this execption, it is done by calling
709   * this function, note it is also with the task execute interface.
710   */
711 static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
712         void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
713 {
714         int res, retry;
715         struct sas_task *task = NULL;
716         struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
717         struct pm8001_device *pm8001_dev = dev->lldd_dev;
718         DECLARE_COMPLETION_ONSTACK(completion_setstate);
719
720         for (retry = 0; retry < 3; retry++) {
721                 task = sas_alloc_slow_task(GFP_KERNEL);
722                 if (!task)
723                         return -ENOMEM;
724
725                 task->dev = dev;
726                 task->task_proto = dev->tproto;
727                 memcpy(&task->ssp_task, parameter, para_len);
728                 task->task_done = pm8001_task_done;
729                 task->slow_task->timer.function = pm8001_tmf_timedout;
730                 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
731                 add_timer(&task->slow_task->timer);
732
733                 res = pm8001_task_exec(task, GFP_KERNEL, 1, tmf);
734
735                 if (res) {
736                         del_timer(&task->slow_task->timer);
737                         pm8001_dbg(pm8001_ha, FAIL, "Executing internal task failed\n");
738                         goto ex_err;
739                 }
740                 wait_for_completion(&task->slow_task->completion);
741                 if (pm8001_ha->chip_id != chip_8001) {
742                         pm8001_dev->setds_completion = &completion_setstate;
743                         PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
744                                 pm8001_dev, DS_OPERATIONAL);
745                         wait_for_completion(&completion_setstate);
746                 }
747                 res = -TMF_RESP_FUNC_FAILED;
748                 /* Even TMF timed out, return direct. */
749                 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
750                         if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
751                                 pm8001_dbg(pm8001_ha, FAIL,
752                                            "TMF task[%x]timeout.\n",
753                                            tmf->tmf);
754                                 goto ex_err;
755                         }
756                 }
757
758                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
759                         task->task_status.stat == SAS_SAM_STAT_GOOD) {
760                         res = TMF_RESP_FUNC_COMPLETE;
761                         break;
762                 }
763
764                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
765                 task->task_status.stat == SAS_DATA_UNDERRUN) {
766                         /* no error, but return the number of bytes of
767                         * underrun */
768                         res = task->task_status.residual;
769                         break;
770                 }
771
772                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
773                         task->task_status.stat == SAS_DATA_OVERRUN) {
774                         pm8001_dbg(pm8001_ha, FAIL, "Blocked task error.\n");
775                         res = -EMSGSIZE;
776                         break;
777                 } else {
778                         pm8001_dbg(pm8001_ha, EH,
779                                    " Task to dev %016llx response:0x%x status 0x%x\n",
780                                    SAS_ADDR(dev->sas_addr),
781                                    task->task_status.resp,
782                                    task->task_status.stat);
783                         sas_free_task(task);
784                         task = NULL;
785                 }
786         }
787 ex_err:
788         BUG_ON(retry == 3 && task != NULL);
789         sas_free_task(task);
790         return res;
791 }
792
793 static int
794 pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
795         struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
796         u32 task_tag)
797 {
798         int res, retry;
799         u32 ccb_tag;
800         struct pm8001_ccb_info *ccb;
801         struct sas_task *task = NULL;
802
803         for (retry = 0; retry < 3; retry++) {
804                 task = sas_alloc_slow_task(GFP_KERNEL);
805                 if (!task)
806                         return -ENOMEM;
807
808                 task->dev = dev;
809                 task->task_proto = dev->tproto;
810                 task->task_done = pm8001_task_done;
811                 task->slow_task->timer.function = pm8001_tmf_timedout;
812                 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
813                 add_timer(&task->slow_task->timer);
814
815                 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
816                 if (res)
817                         goto ex_err;
818                 ccb = &pm8001_ha->ccb_info[ccb_tag];
819                 ccb->device = pm8001_dev;
820                 ccb->ccb_tag = ccb_tag;
821                 ccb->task = task;
822                 ccb->n_elem = 0;
823
824                 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
825                         pm8001_dev, flag, task_tag, ccb_tag);
826
827                 if (res) {
828                         del_timer(&task->slow_task->timer);
829                         pm8001_dbg(pm8001_ha, FAIL, "Executing internal task failed\n");
830                         goto ex_err;
831                 }
832                 wait_for_completion(&task->slow_task->completion);
833                 res = TMF_RESP_FUNC_FAILED;
834                 /* Even TMF timed out, return direct. */
835                 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
836                         if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
837                                 pm8001_dbg(pm8001_ha, FAIL,
838                                            "TMF task timeout.\n");
839                                 goto ex_err;
840                         }
841                 }
842
843                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
844                         task->task_status.stat == SAS_SAM_STAT_GOOD) {
845                         res = TMF_RESP_FUNC_COMPLETE;
846                         break;
847
848                 } else {
849                         pm8001_dbg(pm8001_ha, EH,
850                                    " Task to dev %016llx response: 0x%x status 0x%x\n",
851                                    SAS_ADDR(dev->sas_addr),
852                                    task->task_status.resp,
853                                    task->task_status.stat);
854                         sas_free_task(task);
855                         task = NULL;
856                 }
857         }
858 ex_err:
859         BUG_ON(retry == 3 && task != NULL);
860         sas_free_task(task);
861         return res;
862 }
863
864 /**
865   * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
866   * @dev: the device structure which sas layer used.
867   */
868 static void pm8001_dev_gone_notify(struct domain_device *dev)
869 {
870         unsigned long flags = 0;
871         struct pm8001_hba_info *pm8001_ha;
872         struct pm8001_device *pm8001_dev = dev->lldd_dev;
873
874         pm8001_ha = pm8001_find_ha_by_dev(dev);
875         spin_lock_irqsave(&pm8001_ha->lock, flags);
876         if (pm8001_dev) {
877                 u32 device_id = pm8001_dev->device_id;
878
879                 pm8001_dbg(pm8001_ha, DISC, "found dev[%d:%x] is gone.\n",
880                            pm8001_dev->device_id, pm8001_dev->dev_type);
881                 if (atomic_read(&pm8001_dev->running_req)) {
882                         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
883                         pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
884                                                         dev, 1, 0);
885                         while (atomic_read(&pm8001_dev->running_req))
886                                 msleep(20);
887                         spin_lock_irqsave(&pm8001_ha->lock, flags);
888                 }
889                 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
890                 pm8001_free_dev(pm8001_dev);
891         } else {
892                 pm8001_dbg(pm8001_ha, DISC, "Found dev has gone.\n");
893         }
894         dev->lldd_dev = NULL;
895         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
896 }
897
898 void pm8001_dev_gone(struct domain_device *dev)
899 {
900         pm8001_dev_gone_notify(dev);
901 }
902
903 static int pm8001_issue_ssp_tmf(struct domain_device *dev,
904         u8 *lun, struct pm8001_tmf_task *tmf)
905 {
906         struct sas_ssp_task ssp_task;
907         if (!(dev->tproto & SAS_PROTOCOL_SSP))
908                 return TMF_RESP_FUNC_ESUPP;
909
910         memcpy((u8 *)&ssp_task.LUN, lun, 8);
911         return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
912                 tmf);
913 }
914
915 /* retry commands by ha, by task and/or by device */
916 void pm8001_open_reject_retry(
917         struct pm8001_hba_info *pm8001_ha,
918         struct sas_task *task_to_close,
919         struct pm8001_device *device_to_close)
920 {
921         int i;
922         unsigned long flags;
923
924         if (pm8001_ha == NULL)
925                 return;
926
927         spin_lock_irqsave(&pm8001_ha->lock, flags);
928
929         for (i = 0; i < PM8001_MAX_CCB; i++) {
930                 struct sas_task *task;
931                 struct task_status_struct *ts;
932                 struct pm8001_device *pm8001_dev;
933                 unsigned long flags1;
934                 u32 tag;
935                 struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
936
937                 pm8001_dev = ccb->device;
938                 if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
939                         continue;
940                 if (!device_to_close) {
941                         uintptr_t d = (uintptr_t)pm8001_dev
942                                         - (uintptr_t)&pm8001_ha->devices;
943                         if (((d % sizeof(*pm8001_dev)) != 0)
944                          || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
945                                 continue;
946                 } else if (pm8001_dev != device_to_close)
947                         continue;
948                 tag = ccb->ccb_tag;
949                 if (!tag || (tag == 0xFFFFFFFF))
950                         continue;
951                 task = ccb->task;
952                 if (!task || !task->task_done)
953                         continue;
954                 if (task_to_close && (task != task_to_close))
955                         continue;
956                 ts = &task->task_status;
957                 ts->resp = SAS_TASK_COMPLETE;
958                 /* Force the midlayer to retry */
959                 ts->stat = SAS_OPEN_REJECT;
960                 ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
961                 if (pm8001_dev)
962                         atomic_dec(&pm8001_dev->running_req);
963                 spin_lock_irqsave(&task->task_state_lock, flags1);
964                 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
965                 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
966                 task->task_state_flags |= SAS_TASK_STATE_DONE;
967                 if (unlikely((task->task_state_flags
968                                 & SAS_TASK_STATE_ABORTED))) {
969                         spin_unlock_irqrestore(&task->task_state_lock,
970                                 flags1);
971                         pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
972                 } else {
973                         spin_unlock_irqrestore(&task->task_state_lock,
974                                 flags1);
975                         pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
976                         mb();/* in order to force CPU ordering */
977                         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
978                         task->task_done(task);
979                         spin_lock_irqsave(&pm8001_ha->lock, flags);
980                 }
981         }
982
983         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
984 }
985
986 /**
987  * pm8001_I_T_nexus_reset()
988   * Standard mandates link reset for ATA  (type 0) and hard reset for
989   * SSP (type 1) , only for RECOVERY
990   * @dev: the device structure for the device to reset.
991   */
992 int pm8001_I_T_nexus_reset(struct domain_device *dev)
993 {
994         int rc = TMF_RESP_FUNC_FAILED;
995         struct pm8001_device *pm8001_dev;
996         struct pm8001_hba_info *pm8001_ha;
997         struct sas_phy *phy;
998
999         if (!dev || !dev->lldd_dev)
1000                 return -ENODEV;
1001
1002         pm8001_dev = dev->lldd_dev;
1003         pm8001_ha = pm8001_find_ha_by_dev(dev);
1004         phy = sas_get_local_phy(dev);
1005
1006         if (dev_is_sata(dev)) {
1007                 if (scsi_is_sas_phy_local(phy)) {
1008                         rc = 0;
1009                         goto out;
1010                 }
1011                 rc = sas_phy_reset(phy, 1);
1012                 if (rc) {
1013                         pm8001_dbg(pm8001_ha, EH,
1014                                    "phy reset failed for device %x\n"
1015                                    "with rc %d\n", pm8001_dev->device_id, rc);
1016                         rc = TMF_RESP_FUNC_FAILED;
1017                         goto out;
1018                 }
1019                 msleep(2000);
1020                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1021                                                      dev, 1, 0);
1022                 if (rc) {
1023                         pm8001_dbg(pm8001_ha, EH, "task abort failed %x\n"
1024                                    "with rc %d\n", pm8001_dev->device_id, rc);
1025                         rc = TMF_RESP_FUNC_FAILED;
1026                 }
1027         } else {
1028                 rc = sas_phy_reset(phy, 1);
1029                 msleep(2000);
1030         }
1031         pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
1032                    pm8001_dev->device_id, rc);
1033  out:
1034         sas_put_local_phy(phy);
1035         return rc;
1036 }
1037
1038 /*
1039 * This function handle the IT_NEXUS_XXX event or completion
1040 * status code for SSP/SATA/SMP I/O request.
1041 */
1042 int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
1043 {
1044         int rc = TMF_RESP_FUNC_FAILED;
1045         struct pm8001_device *pm8001_dev;
1046         struct pm8001_hba_info *pm8001_ha;
1047         struct sas_phy *phy;
1048
1049         if (!dev || !dev->lldd_dev)
1050                 return -1;
1051
1052         pm8001_dev = dev->lldd_dev;
1053         pm8001_ha = pm8001_find_ha_by_dev(dev);
1054
1055         pm8001_dbg(pm8001_ha, EH, "I_T_Nexus handler invoked !!\n");
1056
1057         phy = sas_get_local_phy(dev);
1058
1059         if (dev_is_sata(dev)) {
1060                 DECLARE_COMPLETION_ONSTACK(completion_setstate);
1061                 if (scsi_is_sas_phy_local(phy)) {
1062                         rc = 0;
1063                         goto out;
1064                 }
1065                 /* send internal ssp/sata/smp abort command to FW */
1066                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1067                                                      dev, 1, 0);
1068                 msleep(100);
1069
1070                 /* deregister the target device */
1071                 pm8001_dev_gone_notify(dev);
1072                 msleep(200);
1073
1074                 /*send phy reset to hard reset target */
1075                 rc = sas_phy_reset(phy, 1);
1076                 msleep(2000);
1077                 pm8001_dev->setds_completion = &completion_setstate;
1078
1079                 wait_for_completion(&completion_setstate);
1080         } else {
1081                 /* send internal ssp/sata/smp abort command to FW */
1082                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1083                                                      dev, 1, 0);
1084                 msleep(100);
1085
1086                 /* deregister the target device */
1087                 pm8001_dev_gone_notify(dev);
1088                 msleep(200);
1089
1090                 /*send phy reset to hard reset target */
1091                 rc = sas_phy_reset(phy, 1);
1092                 msleep(2000);
1093         }
1094         pm8001_dbg(pm8001_ha, EH, " for device[%x]:rc=%d\n",
1095                    pm8001_dev->device_id, rc);
1096 out:
1097         sas_put_local_phy(phy);
1098
1099         return rc;
1100 }
1101 /* mandatory SAM-3, the task reset the specified LUN*/
1102 int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
1103 {
1104         int rc = TMF_RESP_FUNC_FAILED;
1105         struct pm8001_tmf_task tmf_task;
1106         struct pm8001_device *pm8001_dev = dev->lldd_dev;
1107         struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1108         DECLARE_COMPLETION_ONSTACK(completion_setstate);
1109         if (dev_is_sata(dev)) {
1110                 struct sas_phy *phy = sas_get_local_phy(dev);
1111                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1112                                                      dev, 1, 0);
1113                 rc = sas_phy_reset(phy, 1);
1114                 sas_put_local_phy(phy);
1115                 pm8001_dev->setds_completion = &completion_setstate;
1116                 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1117                         pm8001_dev, DS_OPERATIONAL);
1118                 wait_for_completion(&completion_setstate);
1119         } else {
1120                 tmf_task.tmf = TMF_LU_RESET;
1121                 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1122         }
1123         /* If failed, fall-through I_T_Nexus reset */
1124         pm8001_dbg(pm8001_ha, EH, "for device[%x]:rc=%d\n",
1125                    pm8001_dev->device_id, rc);
1126         return rc;
1127 }
1128
1129 /* optional SAM-3 */
1130 int pm8001_query_task(struct sas_task *task)
1131 {
1132         u32 tag = 0xdeadbeef;
1133         struct scsi_lun lun;
1134         struct pm8001_tmf_task tmf_task;
1135         int rc = TMF_RESP_FUNC_FAILED;
1136         if (unlikely(!task || !task->lldd_task || !task->dev))
1137                 return rc;
1138
1139         if (task->task_proto & SAS_PROTOCOL_SSP) {
1140                 struct scsi_cmnd *cmnd = task->uldd_task;
1141                 struct domain_device *dev = task->dev;
1142                 struct pm8001_hba_info *pm8001_ha =
1143                         pm8001_find_ha_by_dev(dev);
1144
1145                 int_to_scsilun(cmnd->device->lun, &lun);
1146                 rc = pm8001_find_tag(task, &tag);
1147                 if (rc == 0) {
1148                         rc = TMF_RESP_FUNC_FAILED;
1149                         return rc;
1150                 }
1151                 pm8001_dbg(pm8001_ha, EH, "Query:[%16ph]\n", cmnd->cmnd);
1152                 tmf_task.tmf =  TMF_QUERY_TASK;
1153                 tmf_task.tag_of_task_to_be_managed = tag;
1154
1155                 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1156                 switch (rc) {
1157                 /* The task is still in Lun, release it then */
1158                 case TMF_RESP_FUNC_SUCC:
1159                         pm8001_dbg(pm8001_ha, EH,
1160                                    "The task is still in Lun\n");
1161                         break;
1162                 /* The task is not in Lun or failed, reset the phy */
1163                 case TMF_RESP_FUNC_FAILED:
1164                 case TMF_RESP_FUNC_COMPLETE:
1165                         pm8001_dbg(pm8001_ha, EH,
1166                                    "The task is not in Lun or failed, reset the phy\n");
1167                         break;
1168                 }
1169         }
1170         pr_err("pm80xx: rc= %d\n", rc);
1171         return rc;
1172 }
1173
1174 /*  mandatory SAM-3, still need free task/ccb info, abort the specified task */
1175 int pm8001_abort_task(struct sas_task *task)
1176 {
1177         unsigned long flags;
1178         u32 tag;
1179         struct domain_device *dev ;
1180         struct pm8001_hba_info *pm8001_ha;
1181         struct scsi_lun lun;
1182         struct pm8001_device *pm8001_dev;
1183         struct pm8001_tmf_task tmf_task;
1184         int rc = TMF_RESP_FUNC_FAILED, ret;
1185         u32 phy_id;
1186         struct sas_task_slow slow_task;
1187
1188         if (unlikely(!task || !task->lldd_task || !task->dev))
1189                 return TMF_RESP_FUNC_FAILED;
1190
1191         dev = task->dev;
1192         pm8001_dev = dev->lldd_dev;
1193         pm8001_ha = pm8001_find_ha_by_dev(dev);
1194         phy_id = pm8001_dev->attached_phy;
1195
1196         if (PM8001_CHIP_DISP->fatal_errors(pm8001_ha)) {
1197                 // If the controller is seeing fatal errors
1198                 // abort task will not get a response from the controller
1199                 return TMF_RESP_FUNC_FAILED;
1200         }
1201
1202         ret = pm8001_find_tag(task, &tag);
1203         if (ret == 0) {
1204                 pm8001_info(pm8001_ha, "no tag for task:%p\n", task);
1205                 return TMF_RESP_FUNC_FAILED;
1206         }
1207         spin_lock_irqsave(&task->task_state_lock, flags);
1208         if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1209                 spin_unlock_irqrestore(&task->task_state_lock, flags);
1210                 return TMF_RESP_FUNC_COMPLETE;
1211         }
1212         task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1213         if (task->slow_task == NULL) {
1214                 init_completion(&slow_task.completion);
1215                 task->slow_task = &slow_task;
1216         }
1217         spin_unlock_irqrestore(&task->task_state_lock, flags);
1218         if (task->task_proto & SAS_PROTOCOL_SSP) {
1219                 struct scsi_cmnd *cmnd = task->uldd_task;
1220                 int_to_scsilun(cmnd->device->lun, &lun);
1221                 tmf_task.tmf = TMF_ABORT_TASK;
1222                 tmf_task.tag_of_task_to_be_managed = tag;
1223                 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1224                 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1225                         pm8001_dev->sas_device, 0, tag);
1226         } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1227                 task->task_proto & SAS_PROTOCOL_STP) {
1228                 if (pm8001_ha->chip_id == chip_8006) {
1229                         DECLARE_COMPLETION_ONSTACK(completion_reset);
1230                         DECLARE_COMPLETION_ONSTACK(completion);
1231                         struct pm8001_phy *phy = pm8001_ha->phy + phy_id;
1232
1233                         /* 1. Set Device state as Recovery */
1234                         pm8001_dev->setds_completion = &completion;
1235                         PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1236                                 pm8001_dev, DS_IN_RECOVERY);
1237                         wait_for_completion(&completion);
1238
1239                         /* 2. Send Phy Control Hard Reset */
1240                         reinit_completion(&completion);
1241                         phy->port_reset_status = PORT_RESET_TMO;
1242                         phy->reset_success = false;
1243                         phy->enable_completion = &completion;
1244                         phy->reset_completion = &completion_reset;
1245                         ret = PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
1246                                 PHY_HARD_RESET);
1247                         if (ret) {
1248                                 phy->enable_completion = NULL;
1249                                 phy->reset_completion = NULL;
1250                                 goto out;
1251                         }
1252
1253                         /* In the case of the reset timeout/fail we still
1254                          * abort the command at the firmware. The assumption
1255                          * here is that the drive is off doing something so
1256                          * that it's not processing requests, and we want to
1257                          * avoid getting a completion for this and either
1258                          * leaking the task in libsas or losing the race and
1259                          * getting a double free.
1260                          */
1261                         pm8001_dbg(pm8001_ha, MSG,
1262                                    "Waiting for local phy ctl\n");
1263                         ret = wait_for_completion_timeout(&completion,
1264                                         PM8001_TASK_TIMEOUT * HZ);
1265                         if (!ret || !phy->reset_success) {
1266                                 phy->enable_completion = NULL;
1267                                 phy->reset_completion = NULL;
1268                         } else {
1269                                 /* 3. Wait for Port Reset complete or
1270                                  * Port reset TMO
1271                                  */
1272                                 pm8001_dbg(pm8001_ha, MSG,
1273                                            "Waiting for Port reset\n");
1274                                 ret = wait_for_completion_timeout(
1275                                         &completion_reset,
1276                                         PM8001_TASK_TIMEOUT * HZ);
1277                                 if (!ret)
1278                                         phy->reset_completion = NULL;
1279                                 WARN_ON(phy->port_reset_status ==
1280                                                 PORT_RESET_TMO);
1281                                 if (phy->port_reset_status == PORT_RESET_TMO) {
1282                                         pm8001_dev_gone_notify(dev);
1283                                         goto out;
1284                                 }
1285                         }
1286
1287                         /*
1288                          * 4. SATA Abort ALL
1289                          * we wait for the task to be aborted so that the task
1290                          * is removed from the ccb. on success the caller is
1291                          * going to free the task.
1292                          */
1293                         ret = pm8001_exec_internal_task_abort(pm8001_ha,
1294                                 pm8001_dev, pm8001_dev->sas_device, 1, tag);
1295                         if (ret)
1296                                 goto out;
1297                         ret = wait_for_completion_timeout(
1298                                 &task->slow_task->completion,
1299                                 PM8001_TASK_TIMEOUT * HZ);
1300                         if (!ret)
1301                                 goto out;
1302
1303                         /* 5. Set Device State as Operational */
1304                         reinit_completion(&completion);
1305                         pm8001_dev->setds_completion = &completion;
1306                         PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1307                                 pm8001_dev, DS_OPERATIONAL);
1308                         wait_for_completion(&completion);
1309                 } else {
1310                         rc = pm8001_exec_internal_task_abort(pm8001_ha,
1311                                 pm8001_dev, pm8001_dev->sas_device, 0, tag);
1312                 }
1313                 rc = TMF_RESP_FUNC_COMPLETE;
1314         } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1315                 /* SMP */
1316                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1317                         pm8001_dev->sas_device, 0, tag);
1318
1319         }
1320 out:
1321         spin_lock_irqsave(&task->task_state_lock, flags);
1322         if (task->slow_task == &slow_task)
1323                 task->slow_task = NULL;
1324         spin_unlock_irqrestore(&task->task_state_lock, flags);
1325         if (rc != TMF_RESP_FUNC_COMPLETE)
1326                 pm8001_info(pm8001_ha, "rc= %d\n", rc);
1327         return rc;
1328 }
1329
1330 int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1331 {
1332         struct pm8001_tmf_task tmf_task;
1333
1334         tmf_task.tmf = TMF_ABORT_TASK_SET;
1335         return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1336 }
1337
1338 int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1339 {
1340         struct pm8001_tmf_task tmf_task;
1341
1342         tmf_task.tmf = TMF_CLEAR_ACA;
1343         return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1344 }
1345
1346 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1347 {
1348         struct pm8001_tmf_task tmf_task;
1349         struct pm8001_device *pm8001_dev = dev->lldd_dev;
1350         struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1351
1352         pm8001_dbg(pm8001_ha, EH, "I_T_L_Q clear task set[%x]\n",
1353                    pm8001_dev->device_id);
1354         tmf_task.tmf = TMF_CLEAR_TASK_SET;
1355         return pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1356 }