Linux-libre 4.4.222-gnu
[librecmc/linux-libre.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,
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         unsigned long flags = 0;
364
365         if (!dev->port) {
366                 struct task_status_struct *tsm = &t->task_status;
367                 tsm->resp = SAS_TASK_UNDELIVERED;
368                 tsm->stat = SAS_PHY_DOWN;
369                 if (dev->dev_type != SAS_SATA_DEV)
370                         t->task_done(t);
371                 return 0;
372         }
373         pm8001_ha = pm8001_find_ha_by_dev(task->dev);
374         if (pm8001_ha->controller_fatal_error) {
375                 struct task_status_struct *ts = &t->task_status;
376
377                 ts->resp = SAS_TASK_UNDELIVERED;
378                 t->task_done(t);
379                 return 0;
380         }
381         PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n "));
382         spin_lock_irqsave(&pm8001_ha->lock, flags);
383         do {
384                 dev = t->dev;
385                 pm8001_dev = dev->lldd_dev;
386                 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
387                 if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
388                         if (sas_protocol_ata(t->task_proto)) {
389                                 struct task_status_struct *ts = &t->task_status;
390                                 ts->resp = SAS_TASK_UNDELIVERED;
391                                 ts->stat = SAS_PHY_DOWN;
392
393                                 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
394                                 t->task_done(t);
395                                 spin_lock_irqsave(&pm8001_ha->lock, flags);
396                                 continue;
397                         } else {
398                                 struct task_status_struct *ts = &t->task_status;
399                                 ts->resp = SAS_TASK_UNDELIVERED;
400                                 ts->stat = SAS_PHY_DOWN;
401                                 t->task_done(t);
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         } while (0);
464         rc = 0;
465         goto out_done;
466
467 err_out_tag:
468         pm8001_tag_free(pm8001_ha, tag);
469 err_out:
470         dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
471         if (!sas_protocol_ata(t->task_proto))
472                 if (n_elem)
473                         dma_unmap_sg(pm8001_ha->dev, t->scatter, t->num_scatter,
474                                 t->data_dir);
475 out_done:
476         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
477         return rc;
478 }
479
480 /**
481   * pm8001_queue_command - register for upper layer used, all IO commands sent
482   * to HBA are from this interface.
483   * @task: the task to be execute.
484   * @gfp_flags: gfp_flags
485   */
486 int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags)
487 {
488         return pm8001_task_exec(task, gfp_flags, 0, NULL);
489 }
490
491 /**
492   * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
493   * @pm8001_ha: our hba card information
494   * @ccb: the ccb which attached to ssp task
495   * @task: the task to be free.
496   * @ccb_idx: ccb index.
497   */
498 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
499         struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
500 {
501         if (!ccb->task)
502                 return;
503         if (!sas_protocol_ata(task->task_proto))
504                 if (ccb->n_elem)
505                         dma_unmap_sg(pm8001_ha->dev, task->scatter,
506                                 task->num_scatter, task->data_dir);
507
508         switch (task->task_proto) {
509         case SAS_PROTOCOL_SMP:
510                 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
511                         PCI_DMA_FROMDEVICE);
512                 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
513                         PCI_DMA_TODEVICE);
514                 break;
515
516         case SAS_PROTOCOL_SATA:
517         case SAS_PROTOCOL_STP:
518         case SAS_PROTOCOL_SSP:
519         default:
520                 /* do nothing */
521                 break;
522         }
523         task->lldd_task = NULL;
524         ccb->task = NULL;
525         ccb->ccb_tag = 0xFFFFFFFF;
526         ccb->open_retry = 0;
527         pm8001_tag_free(pm8001_ha, ccb_idx);
528 }
529
530  /**
531   * pm8001_alloc_dev - find a empty pm8001_device
532   * @pm8001_ha: our hba card information
533   */
534 struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
535 {
536         u32 dev;
537         for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
538                 if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) {
539                         pm8001_ha->devices[dev].id = dev;
540                         return &pm8001_ha->devices[dev];
541                 }
542         }
543         if (dev == PM8001_MAX_DEVICES) {
544                 PM8001_FAIL_DBG(pm8001_ha,
545                         pm8001_printk("max support %d devices, ignore ..\n",
546                         PM8001_MAX_DEVICES));
547         }
548         return NULL;
549 }
550 /**
551   * pm8001_find_dev - find a matching pm8001_device
552   * @pm8001_ha: our hba card information
553   */
554 struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha,
555                                         u32 device_id)
556 {
557         u32 dev;
558         for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
559                 if (pm8001_ha->devices[dev].device_id == device_id)
560                         return &pm8001_ha->devices[dev];
561         }
562         if (dev == PM8001_MAX_DEVICES) {
563                 PM8001_FAIL_DBG(pm8001_ha, pm8001_printk("NO MATCHING "
564                                 "DEVICE FOUND !!!\n"));
565         }
566         return NULL;
567 }
568
569 static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
570 {
571         u32 id = pm8001_dev->id;
572         memset(pm8001_dev, 0, sizeof(*pm8001_dev));
573         pm8001_dev->id = id;
574         pm8001_dev->dev_type = SAS_PHY_UNUSED;
575         pm8001_dev->device_id = PM8001_MAX_DEVICES;
576         pm8001_dev->sas_device = NULL;
577 }
578
579 /**
580   * pm8001_dev_found_notify - libsas notify a device is found.
581   * @dev: the device structure which sas layer used.
582   *
583   * when libsas find a sas domain device, it should tell the LLDD that
584   * device is found, and then LLDD register this device to HBA firmware
585   * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
586   * device ID(according to device's sas address) and returned it to LLDD. From
587   * now on, we communicate with HBA FW with the device ID which HBA assigned
588   * rather than sas address. it is the necessary step for our HBA but it is
589   * the optional for other HBA driver.
590   */
591 static int pm8001_dev_found_notify(struct domain_device *dev)
592 {
593         unsigned long flags = 0;
594         int res = 0;
595         struct pm8001_hba_info *pm8001_ha = NULL;
596         struct domain_device *parent_dev = dev->parent;
597         struct pm8001_device *pm8001_device;
598         DECLARE_COMPLETION_ONSTACK(completion);
599         u32 flag = 0;
600         pm8001_ha = pm8001_find_ha_by_dev(dev);
601         spin_lock_irqsave(&pm8001_ha->lock, flags);
602
603         pm8001_device = pm8001_alloc_dev(pm8001_ha);
604         if (!pm8001_device) {
605                 res = -1;
606                 goto found_out;
607         }
608         pm8001_device->sas_device = dev;
609         dev->lldd_dev = pm8001_device;
610         pm8001_device->dev_type = dev->dev_type;
611         pm8001_device->dcompletion = &completion;
612         if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
613                 int phy_id;
614                 struct ex_phy *phy;
615                 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
616                 phy_id++) {
617                         phy = &parent_dev->ex_dev.ex_phy[phy_id];
618                         if (SAS_ADDR(phy->attached_sas_addr)
619                                 == SAS_ADDR(dev->sas_addr)) {
620                                 pm8001_device->attached_phy = phy_id;
621                                 break;
622                         }
623                 }
624                 if (phy_id == parent_dev->ex_dev.num_phys) {
625                         PM8001_FAIL_DBG(pm8001_ha,
626                         pm8001_printk("Error: no attached dev:%016llx"
627                         " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
628                                 SAS_ADDR(parent_dev->sas_addr)));
629                         res = -1;
630                 }
631         } else {
632                 if (dev->dev_type == SAS_SATA_DEV) {
633                         pm8001_device->attached_phy =
634                                 dev->rphy->identify.phy_identifier;
635                                 flag = 1; /* directly sata*/
636                 }
637         } /*register this device to HBA*/
638         PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device\n"));
639         PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
640         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
641         wait_for_completion(&completion);
642         if (dev->dev_type == SAS_END_DEVICE)
643                 msleep(50);
644         pm8001_ha->flags = PM8001F_RUN_TIME;
645         return 0;
646 found_out:
647         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
648         return res;
649 }
650
651 int pm8001_dev_found(struct domain_device *dev)
652 {
653         return pm8001_dev_found_notify(dev);
654 }
655
656 void pm8001_task_done(struct sas_task *task)
657 {
658         if (!del_timer(&task->slow_task->timer))
659                 return;
660         complete(&task->slow_task->completion);
661 }
662
663 static void pm8001_tmf_timedout(unsigned long data)
664 {
665         struct sas_task *task = (struct sas_task *)data;
666
667         task->task_state_flags |= SAS_TASK_STATE_ABORTED;
668         complete(&task->slow_task->completion);
669 }
670
671 #define PM8001_TASK_TIMEOUT 20
672 /**
673   * pm8001_exec_internal_tmf_task - execute some task management commands.
674   * @dev: the wanted device.
675   * @tmf: which task management wanted to be take.
676   * @para_len: para_len.
677   * @parameter: ssp task parameter.
678   *
679   * when errors or exception happened, we may want to do something, for example
680   * abort the issued task which result in this execption, it is done by calling
681   * this function, note it is also with the task execute interface.
682   */
683 static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
684         void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
685 {
686         int res, retry;
687         struct sas_task *task = NULL;
688         struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
689         struct pm8001_device *pm8001_dev = dev->lldd_dev;
690         DECLARE_COMPLETION_ONSTACK(completion_setstate);
691
692         for (retry = 0; retry < 3; retry++) {
693                 task = sas_alloc_slow_task(GFP_KERNEL);
694                 if (!task)
695                         return -ENOMEM;
696
697                 task->dev = dev;
698                 task->task_proto = dev->tproto;
699                 memcpy(&task->ssp_task, parameter, para_len);
700                 task->task_done = pm8001_task_done;
701                 task->slow_task->timer.data = (unsigned long)task;
702                 task->slow_task->timer.function = pm8001_tmf_timedout;
703                 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
704                 add_timer(&task->slow_task->timer);
705
706                 res = pm8001_task_exec(task, GFP_KERNEL, 1, tmf);
707
708                 if (res) {
709                         del_timer(&task->slow_task->timer);
710                         PM8001_FAIL_DBG(pm8001_ha,
711                                 pm8001_printk("Executing internal task "
712                                 "failed\n"));
713                         goto ex_err;
714                 }
715                 wait_for_completion(&task->slow_task->completion);
716                 if (pm8001_ha->chip_id != chip_8001) {
717                         pm8001_dev->setds_completion = &completion_setstate;
718                                 PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
719                                         pm8001_dev, 0x01);
720                         wait_for_completion(&completion_setstate);
721                 }
722                 res = -TMF_RESP_FUNC_FAILED;
723                 /* Even TMF timed out, return direct. */
724                 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
725                         if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
726                                 PM8001_FAIL_DBG(pm8001_ha,
727                                         pm8001_printk("TMF task[%x]timeout.\n",
728                                         tmf->tmf));
729                                 goto ex_err;
730                         }
731                 }
732
733                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
734                         task->task_status.stat == SAM_STAT_GOOD) {
735                         res = TMF_RESP_FUNC_COMPLETE;
736                         break;
737                 }
738
739                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
740                 task->task_status.stat == SAS_DATA_UNDERRUN) {
741                         /* no error, but return the number of bytes of
742                         * underrun */
743                         res = task->task_status.residual;
744                         break;
745                 }
746
747                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
748                         task->task_status.stat == SAS_DATA_OVERRUN) {
749                         PM8001_FAIL_DBG(pm8001_ha,
750                                 pm8001_printk("Blocked task error.\n"));
751                         res = -EMSGSIZE;
752                         break;
753                 } else {
754                         PM8001_EH_DBG(pm8001_ha,
755                                 pm8001_printk(" Task to dev %016llx response:"
756                                 "0x%x status 0x%x\n",
757                                 SAS_ADDR(dev->sas_addr),
758                                 task->task_status.resp,
759                                 task->task_status.stat));
760                         sas_free_task(task);
761                         task = NULL;
762                 }
763         }
764 ex_err:
765         BUG_ON(retry == 3 && task != NULL);
766         sas_free_task(task);
767         return res;
768 }
769
770 static int
771 pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
772         struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
773         u32 task_tag)
774 {
775         int res, retry;
776         u32 ccb_tag;
777         struct pm8001_ccb_info *ccb;
778         struct sas_task *task = NULL;
779
780         for (retry = 0; retry < 3; retry++) {
781                 task = sas_alloc_slow_task(GFP_KERNEL);
782                 if (!task)
783                         return -ENOMEM;
784
785                 task->dev = dev;
786                 task->task_proto = dev->tproto;
787                 task->task_done = pm8001_task_done;
788                 task->slow_task->timer.data = (unsigned long)task;
789                 task->slow_task->timer.function = pm8001_tmf_timedout;
790                 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
791                 add_timer(&task->slow_task->timer);
792
793                 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
794                 if (res)
795                         return res;
796                 ccb = &pm8001_ha->ccb_info[ccb_tag];
797                 ccb->device = pm8001_dev;
798                 ccb->ccb_tag = ccb_tag;
799                 ccb->task = task;
800                 ccb->n_elem = 0;
801
802                 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
803                         pm8001_dev, flag, task_tag, ccb_tag);
804
805                 if (res) {
806                         del_timer(&task->slow_task->timer);
807                         PM8001_FAIL_DBG(pm8001_ha,
808                                 pm8001_printk("Executing internal task "
809                                 "failed\n"));
810                         goto ex_err;
811                 }
812                 wait_for_completion(&task->slow_task->completion);
813                 res = TMF_RESP_FUNC_FAILED;
814                 /* Even TMF timed out, return direct. */
815                 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
816                         if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
817                                 PM8001_FAIL_DBG(pm8001_ha,
818                                         pm8001_printk("TMF task timeout.\n"));
819                                 goto ex_err;
820                         }
821                 }
822
823                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
824                         task->task_status.stat == SAM_STAT_GOOD) {
825                         res = TMF_RESP_FUNC_COMPLETE;
826                         break;
827
828                 } else {
829                         PM8001_EH_DBG(pm8001_ha,
830                                 pm8001_printk(" Task to dev %016llx response: "
831                                         "0x%x status 0x%x\n",
832                                 SAS_ADDR(dev->sas_addr),
833                                 task->task_status.resp,
834                                 task->task_status.stat));
835                         sas_free_task(task);
836                         task = NULL;
837                 }
838         }
839 ex_err:
840         BUG_ON(retry == 3 && task != NULL);
841         sas_free_task(task);
842         return res;
843 }
844
845 /**
846   * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
847   * @dev: the device structure which sas layer used.
848   */
849 static void pm8001_dev_gone_notify(struct domain_device *dev)
850 {
851         unsigned long flags = 0;
852         struct pm8001_hba_info *pm8001_ha;
853         struct pm8001_device *pm8001_dev = dev->lldd_dev;
854
855         pm8001_ha = pm8001_find_ha_by_dev(dev);
856         spin_lock_irqsave(&pm8001_ha->lock, flags);
857         if (pm8001_dev) {
858                 u32 device_id = pm8001_dev->device_id;
859
860                 PM8001_DISC_DBG(pm8001_ha,
861                         pm8001_printk("found dev[%d:%x] is gone.\n",
862                         pm8001_dev->device_id, pm8001_dev->dev_type));
863                 if (pm8001_dev->running_req) {
864                         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
865                         pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
866                                 dev, 1, 0);
867                         spin_lock_irqsave(&pm8001_ha->lock, flags);
868                 }
869                 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
870                 pm8001_free_dev(pm8001_dev);
871         } else {
872                 PM8001_DISC_DBG(pm8001_ha,
873                         pm8001_printk("Found dev has gone.\n"));
874         }
875         dev->lldd_dev = NULL;
876         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
877 }
878
879 void pm8001_dev_gone(struct domain_device *dev)
880 {
881         pm8001_dev_gone_notify(dev);
882 }
883
884 static int pm8001_issue_ssp_tmf(struct domain_device *dev,
885         u8 *lun, struct pm8001_tmf_task *tmf)
886 {
887         struct sas_ssp_task ssp_task;
888         if (!(dev->tproto & SAS_PROTOCOL_SSP))
889                 return TMF_RESP_FUNC_ESUPP;
890
891         strncpy((u8 *)&ssp_task.LUN, lun, 8);
892         return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
893                 tmf);
894 }
895
896 /* retry commands by ha, by task and/or by device */
897 void pm8001_open_reject_retry(
898         struct pm8001_hba_info *pm8001_ha,
899         struct sas_task *task_to_close,
900         struct pm8001_device *device_to_close)
901 {
902         int i;
903         unsigned long flags;
904
905         if (pm8001_ha == NULL)
906                 return;
907
908         spin_lock_irqsave(&pm8001_ha->lock, flags);
909
910         for (i = 0; i < PM8001_MAX_CCB; i++) {
911                 struct sas_task *task;
912                 struct task_status_struct *ts;
913                 struct pm8001_device *pm8001_dev;
914                 unsigned long flags1;
915                 u32 tag;
916                 struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
917
918                 pm8001_dev = ccb->device;
919                 if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))
920                         continue;
921                 if (!device_to_close) {
922                         uintptr_t d = (uintptr_t)pm8001_dev
923                                         - (uintptr_t)&pm8001_ha->devices;
924                         if (((d % sizeof(*pm8001_dev)) != 0)
925                          || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
926                                 continue;
927                 } else if (pm8001_dev != device_to_close)
928                         continue;
929                 tag = ccb->ccb_tag;
930                 if (!tag || (tag == 0xFFFFFFFF))
931                         continue;
932                 task = ccb->task;
933                 if (!task || !task->task_done)
934                         continue;
935                 if (task_to_close && (task != task_to_close))
936                         continue;
937                 ts = &task->task_status;
938                 ts->resp = SAS_TASK_COMPLETE;
939                 /* Force the midlayer to retry */
940                 ts->stat = SAS_OPEN_REJECT;
941                 ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
942                 if (pm8001_dev)
943                         pm8001_dev->running_req--;
944                 spin_lock_irqsave(&task->task_state_lock, flags1);
945                 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
946                 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
947                 task->task_state_flags |= SAS_TASK_STATE_DONE;
948                 if (unlikely((task->task_state_flags
949                                 & SAS_TASK_STATE_ABORTED))) {
950                         spin_unlock_irqrestore(&task->task_state_lock,
951                                 flags1);
952                         pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
953                 } else {
954                         spin_unlock_irqrestore(&task->task_state_lock,
955                                 flags1);
956                         pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
957                         mb();/* in order to force CPU ordering */
958                         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
959                         task->task_done(task);
960                         spin_lock_irqsave(&pm8001_ha->lock, flags);
961                 }
962         }
963
964         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
965 }
966
967 /**
968   * Standard mandates link reset for ATA  (type 0) and hard reset for
969   * SSP (type 1) , only for RECOVERY
970   */
971 int pm8001_I_T_nexus_reset(struct domain_device *dev)
972 {
973         int rc = TMF_RESP_FUNC_FAILED;
974         struct pm8001_device *pm8001_dev;
975         struct pm8001_hba_info *pm8001_ha;
976         struct sas_phy *phy;
977
978         if (!dev || !dev->lldd_dev)
979                 return -ENODEV;
980
981         pm8001_dev = dev->lldd_dev;
982         pm8001_ha = pm8001_find_ha_by_dev(dev);
983         phy = sas_get_local_phy(dev);
984
985         if (dev_is_sata(dev)) {
986                 if (scsi_is_sas_phy_local(phy)) {
987                         rc = 0;
988                         goto out;
989                 }
990                 rc = sas_phy_reset(phy, 1);
991                 if (rc) {
992                         PM8001_EH_DBG(pm8001_ha,
993                         pm8001_printk("phy reset failed for device %x\n"
994                         "with rc %d\n", pm8001_dev->device_id, rc));
995                         rc = TMF_RESP_FUNC_FAILED;
996                         goto out;
997                 }
998                 msleep(2000);
999                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1000                         dev, 1, 0);
1001                 if (rc) {
1002                         PM8001_EH_DBG(pm8001_ha,
1003                         pm8001_printk("task abort failed %x\n"
1004                         "with rc %d\n", pm8001_dev->device_id, rc));
1005                         rc = TMF_RESP_FUNC_FAILED;
1006                 }
1007         } else {
1008                 rc = sas_phy_reset(phy, 1);
1009                 msleep(2000);
1010         }
1011         PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
1012                 pm8001_dev->device_id, rc));
1013  out:
1014         sas_put_local_phy(phy);
1015         return rc;
1016 }
1017
1018 /*
1019 * This function handle the IT_NEXUS_XXX event or completion
1020 * status code for SSP/SATA/SMP I/O request.
1021 */
1022 int pm8001_I_T_nexus_event_handler(struct domain_device *dev)
1023 {
1024         int rc = TMF_RESP_FUNC_FAILED;
1025         struct pm8001_device *pm8001_dev;
1026         struct pm8001_hba_info *pm8001_ha;
1027         struct sas_phy *phy;
1028         u32 device_id = 0;
1029
1030         if (!dev || !dev->lldd_dev)
1031                 return -1;
1032
1033         pm8001_dev = dev->lldd_dev;
1034         device_id = pm8001_dev->device_id;
1035         pm8001_ha = pm8001_find_ha_by_dev(dev);
1036
1037         PM8001_EH_DBG(pm8001_ha,
1038                         pm8001_printk("I_T_Nexus handler invoked !!"));
1039
1040         phy = sas_get_local_phy(dev);
1041
1042         if (dev_is_sata(dev)) {
1043                 DECLARE_COMPLETION_ONSTACK(completion_setstate);
1044                 if (scsi_is_sas_phy_local(phy)) {
1045                         rc = 0;
1046                         goto out;
1047                 }
1048                 /* send internal ssp/sata/smp abort command to FW */
1049                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1050                                                         dev, 1, 0);
1051                 msleep(100);
1052
1053                 /* deregister the target device */
1054                 pm8001_dev_gone_notify(dev);
1055                 msleep(200);
1056
1057                 /*send phy reset to hard reset target */
1058                 rc = sas_phy_reset(phy, 1);
1059                 msleep(2000);
1060                 pm8001_dev->setds_completion = &completion_setstate;
1061
1062                 wait_for_completion(&completion_setstate);
1063         } else {
1064                 /* send internal ssp/sata/smp abort command to FW */
1065                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1066                                                         dev, 1, 0);
1067                 msleep(100);
1068
1069                 /* deregister the target device */
1070                 pm8001_dev_gone_notify(dev);
1071                 msleep(200);
1072
1073                 /*send phy reset to hard reset target */
1074                 rc = sas_phy_reset(phy, 1);
1075                 msleep(2000);
1076         }
1077         PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
1078                 pm8001_dev->device_id, rc));
1079 out:
1080         sas_put_local_phy(phy);
1081
1082         return rc;
1083 }
1084 /* mandatory SAM-3, the task reset the specified LUN*/
1085 int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
1086 {
1087         int rc = TMF_RESP_FUNC_FAILED;
1088         struct pm8001_tmf_task tmf_task;
1089         struct pm8001_device *pm8001_dev = dev->lldd_dev;
1090         struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1091         DECLARE_COMPLETION_ONSTACK(completion_setstate);
1092         if (dev_is_sata(dev)) {
1093                 struct sas_phy *phy = sas_get_local_phy(dev);
1094                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1095                         dev, 1, 0);
1096                 rc = sas_phy_reset(phy, 1);
1097                 sas_put_local_phy(phy);
1098                 pm8001_dev->setds_completion = &completion_setstate;
1099                 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1100                         pm8001_dev, 0x01);
1101                 wait_for_completion(&completion_setstate);
1102         } else {
1103                 tmf_task.tmf = TMF_LU_RESET;
1104                 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1105         }
1106         /* If failed, fall-through I_T_Nexus reset */
1107         PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
1108                 pm8001_dev->device_id, rc));
1109         return rc;
1110 }
1111
1112 /* optional SAM-3 */
1113 int pm8001_query_task(struct sas_task *task)
1114 {
1115         u32 tag = 0xdeadbeef;
1116         int i = 0;
1117         struct scsi_lun lun;
1118         struct pm8001_tmf_task tmf_task;
1119         int rc = TMF_RESP_FUNC_FAILED;
1120         if (unlikely(!task || !task->lldd_task || !task->dev))
1121                 return rc;
1122
1123         if (task->task_proto & SAS_PROTOCOL_SSP) {
1124                 struct scsi_cmnd *cmnd = task->uldd_task;
1125                 struct domain_device *dev = task->dev;
1126                 struct pm8001_hba_info *pm8001_ha =
1127                         pm8001_find_ha_by_dev(dev);
1128
1129                 int_to_scsilun(cmnd->device->lun, &lun);
1130                 rc = pm8001_find_tag(task, &tag);
1131                 if (rc == 0) {
1132                         rc = TMF_RESP_FUNC_FAILED;
1133                         return rc;
1134                 }
1135                 PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
1136                 for (i = 0; i < 16; i++)
1137                         printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
1138                 printk(KERN_INFO "]\n");
1139                 tmf_task.tmf =  TMF_QUERY_TASK;
1140                 tmf_task.tag_of_task_to_be_managed = tag;
1141
1142                 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1143                 switch (rc) {
1144                 /* The task is still in Lun, release it then */
1145                 case TMF_RESP_FUNC_SUCC:
1146                         PM8001_EH_DBG(pm8001_ha,
1147                                 pm8001_printk("The task is still in Lun\n"));
1148                         break;
1149                 /* The task is not in Lun or failed, reset the phy */
1150                 case TMF_RESP_FUNC_FAILED:
1151                 case TMF_RESP_FUNC_COMPLETE:
1152                         PM8001_EH_DBG(pm8001_ha,
1153                         pm8001_printk("The task is not in Lun or failed,"
1154                         " reset the phy\n"));
1155                         break;
1156                 }
1157         }
1158         pm8001_printk(":rc= %d\n", rc);
1159         return rc;
1160 }
1161
1162 /*  mandatory SAM-3, still need free task/ccb info, abord the specified task */
1163 int pm8001_abort_task(struct sas_task *task)
1164 {
1165         unsigned long flags;
1166         u32 tag = 0xdeadbeef;
1167         u32 device_id;
1168         struct domain_device *dev ;
1169         struct pm8001_hba_info *pm8001_ha = NULL;
1170         struct pm8001_ccb_info *ccb;
1171         struct scsi_lun lun;
1172         struct pm8001_device *pm8001_dev;
1173         struct pm8001_tmf_task tmf_task;
1174         int rc = TMF_RESP_FUNC_FAILED;
1175         if (unlikely(!task || !task->lldd_task || !task->dev))
1176                 return rc;
1177         spin_lock_irqsave(&task->task_state_lock, flags);
1178         if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1179                 spin_unlock_irqrestore(&task->task_state_lock, flags);
1180                 rc = TMF_RESP_FUNC_COMPLETE;
1181                 goto out;
1182         }
1183         spin_unlock_irqrestore(&task->task_state_lock, flags);
1184         if (task->task_proto & SAS_PROTOCOL_SSP) {
1185                 struct scsi_cmnd *cmnd = task->uldd_task;
1186                 dev = task->dev;
1187                 ccb = task->lldd_task;
1188                 pm8001_dev = dev->lldd_dev;
1189                 pm8001_ha = pm8001_find_ha_by_dev(dev);
1190                 int_to_scsilun(cmnd->device->lun, &lun);
1191                 rc = pm8001_find_tag(task, &tag);
1192                 if (rc == 0) {
1193                         printk(KERN_INFO "No such tag in %s\n", __func__);
1194                         rc = TMF_RESP_FUNC_FAILED;
1195                         return rc;
1196                 }
1197                 device_id = pm8001_dev->device_id;
1198                 PM8001_EH_DBG(pm8001_ha,
1199                         pm8001_printk("abort io to deviceid= %d\n", device_id));
1200                 tmf_task.tmf = TMF_ABORT_TASK;
1201                 tmf_task.tag_of_task_to_be_managed = tag;
1202                 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1203                 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1204                         pm8001_dev->sas_device, 0, tag);
1205         } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1206                 task->task_proto & SAS_PROTOCOL_STP) {
1207                 dev = task->dev;
1208                 pm8001_dev = dev->lldd_dev;
1209                 pm8001_ha = pm8001_find_ha_by_dev(dev);
1210                 rc = pm8001_find_tag(task, &tag);
1211                 if (rc == 0) {
1212                         printk(KERN_INFO "No such tag in %s\n", __func__);
1213                         rc = TMF_RESP_FUNC_FAILED;
1214                         return rc;
1215                 }
1216                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1217                         pm8001_dev->sas_device, 0, tag);
1218         } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1219                 /* SMP */
1220                 dev = task->dev;
1221                 pm8001_dev = dev->lldd_dev;
1222                 pm8001_ha = pm8001_find_ha_by_dev(dev);
1223                 rc = pm8001_find_tag(task, &tag);
1224                 if (rc == 0) {
1225                         printk(KERN_INFO "No such tag in %s\n", __func__);
1226                         rc = TMF_RESP_FUNC_FAILED;
1227                         return rc;
1228                 }
1229                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1230                         pm8001_dev->sas_device, 0, tag);
1231
1232         }
1233 out:
1234         if (rc != TMF_RESP_FUNC_COMPLETE)
1235                 pm8001_printk("rc= %d\n", rc);
1236         return rc;
1237 }
1238
1239 int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1240 {
1241         int rc = TMF_RESP_FUNC_FAILED;
1242         struct pm8001_tmf_task tmf_task;
1243
1244         tmf_task.tmf = TMF_ABORT_TASK_SET;
1245         rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1246         return rc;
1247 }
1248
1249 int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1250 {
1251         int rc = TMF_RESP_FUNC_FAILED;
1252         struct pm8001_tmf_task tmf_task;
1253
1254         tmf_task.tmf = TMF_CLEAR_ACA;
1255         rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1256
1257         return rc;
1258 }
1259
1260 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1261 {
1262         int rc = TMF_RESP_FUNC_FAILED;
1263         struct pm8001_tmf_task tmf_task;
1264         struct pm8001_device *pm8001_dev = dev->lldd_dev;
1265         struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1266
1267         PM8001_EH_DBG(pm8001_ha,
1268                 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1269                 pm8001_dev->device_id));
1270         tmf_task.tmf = TMF_CLEAR_TASK_SET;
1271         rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1272         return rc;
1273 }
1274