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