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