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