[SCSI] pm8001: add SAS/SATA HBA driver
[linux-2.6.git] / drivers / scsi / pm8001 / pm8001_sas.c
1 /*
2  * PMC-Sierra SPC 8001 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 "pm8001_sas.h"
42
43 /**
44  * pm8001_find_tag - from sas task to find out  tag that belongs to this task
45  * @task: the task sent to the LLDD
46  * @tag: the found tag associated with the task
47  */
48 static int pm8001_find_tag(struct sas_task *task, u32 *tag)
49 {
50         if (task->lldd_task) {
51                 struct pm8001_ccb_info *ccb;
52                 ccb = task->lldd_task;
53                 *tag = ccb->ccb_tag;
54                 return 1;
55         }
56         return 0;
57 }
58
59 /**
60   * pm8001_tag_clear - clear the tags bitmap
61   * @pm8001_ha: our hba struct
62   * @tag: the found tag associated with the task
63   */
64 static void pm8001_tag_clear(struct pm8001_hba_info *pm8001_ha, u32 tag)
65 {
66         void *bitmap = pm8001_ha->tags;
67         clear_bit(tag, bitmap);
68 }
69
70 static void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
71 {
72         pm8001_tag_clear(pm8001_ha, tag);
73 }
74
75 static void pm8001_tag_set(struct pm8001_hba_info *pm8001_ha, u32 tag)
76 {
77         void *bitmap = pm8001_ha->tags;
78         set_bit(tag, bitmap);
79 }
80
81 /**
82   * pm8001_tag_alloc - allocate a empty tag for task used.
83   * @pm8001_ha: our hba struct
84   * @tag_out: the found empty tag .
85   */
86 inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
87 {
88         unsigned int index, tag;
89         void *bitmap = pm8001_ha->tags;
90
91         index = find_first_zero_bit(bitmap, pm8001_ha->tags_num);
92         tag = index;
93         if (tag >= pm8001_ha->tags_num)
94                 return -SAS_QUEUE_FULL;
95         pm8001_tag_set(pm8001_ha, tag);
96         *tag_out = tag;
97         return 0;
98 }
99
100 void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha)
101 {
102         int i;
103         for (i = 0; i < pm8001_ha->tags_num; ++i)
104                 pm8001_tag_clear(pm8001_ha, i);
105 }
106
107  /**
108   * pm8001_mem_alloc - allocate memory for pm8001.
109   * @pdev: pci device.
110   * @virt_addr: the allocated virtual address
111   * @pphys_addr_hi: the physical address high byte address.
112   * @pphys_addr_lo: the physical address low byte address.
113   * @mem_size: memory size.
114   */
115 int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr,
116         dma_addr_t *pphys_addr, u32 *pphys_addr_hi,
117         u32 *pphys_addr_lo, u32 mem_size, u32 align)
118 {
119         caddr_t mem_virt_alloc;
120         dma_addr_t mem_dma_handle;
121         u64 phys_align;
122         u64 align_offset = 0;
123         if (align)
124                 align_offset = (dma_addr_t)align - 1;
125         mem_virt_alloc =
126                 pci_alloc_consistent(pdev, mem_size + align, &mem_dma_handle);
127         if (!mem_virt_alloc) {
128                 pm8001_printk("memory allocation error\n");
129                 return -1;
130         }
131         memset((void *)mem_virt_alloc, 0, mem_size+align);
132         *pphys_addr = mem_dma_handle;
133         phys_align = (*pphys_addr + align_offset) & ~align_offset;
134         *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
135         *pphys_addr_hi = upper_32_bits(phys_align);
136         *pphys_addr_lo = lower_32_bits(phys_align);
137         return 0;
138 }
139 /**
140   * pm8001_find_ha_by_dev - from domain device which come from sas layer to
141   * find out our hba struct.
142   * @dev: the domain device which from sas layer.
143   */
144 static
145 struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
146 {
147         struct sas_ha_struct *sha = dev->port->ha;
148         struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
149         return pm8001_ha;
150 }
151
152 /**
153   * pm8001_phy_control - this function should be registered to
154   * sas_domain_function_template to provide libsas used, note: this is just
155   * control the HBA phy rather than other expander phy if you want control
156   * other phy, you should use SMP command.
157   * @sas_phy: which phy in HBA phys.
158   * @func: the operation.
159   * @funcdata: always NULL.
160   */
161 int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
162         void *funcdata)
163 {
164         int rc = 0, phy_id = sas_phy->id;
165         struct pm8001_hba_info *pm8001_ha = NULL;
166         struct sas_phy_linkrates *rates;
167         DECLARE_COMPLETION_ONSTACK(completion);
168         pm8001_ha = sas_phy->ha->lldd_ha;
169         pm8001_ha->phy[phy_id].enable_completion = &completion;
170         switch (func) {
171         case PHY_FUNC_SET_LINK_RATE:
172                 rates = funcdata;
173                 if (rates->minimum_linkrate) {
174                         pm8001_ha->phy[phy_id].minimum_linkrate =
175                                 rates->minimum_linkrate;
176                 }
177                 if (rates->maximum_linkrate) {
178                         pm8001_ha->phy[phy_id].maximum_linkrate =
179                                 rates->maximum_linkrate;
180                 }
181                 if (pm8001_ha->phy[phy_id].phy_state == 0) {
182                         PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
183                         wait_for_completion(&completion);
184                 }
185                 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
186                                               PHY_LINK_RESET);
187                 break;
188         case PHY_FUNC_HARD_RESET:
189                 if (pm8001_ha->phy[phy_id].phy_state == 0) {
190                         PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
191                         wait_for_completion(&completion);
192                 }
193                 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
194                                               PHY_HARD_RESET);
195                 break;
196         case PHY_FUNC_LINK_RESET:
197                 if (pm8001_ha->phy[phy_id].phy_state == 0) {
198                         PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
199                         wait_for_completion(&completion);
200                 }
201                 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
202                                               PHY_LINK_RESET);
203                 break;
204         case PHY_FUNC_RELEASE_SPINUP_HOLD:
205                 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
206                                               PHY_LINK_RESET);
207                 break;
208         case PHY_FUNC_DISABLE:
209                 PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
210                 break;
211         default:
212                 rc = -EOPNOTSUPP;
213         }
214         msleep(300);
215         return rc;
216 }
217
218 int pm8001_slave_alloc(struct scsi_device *scsi_dev)
219 {
220         struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
221         if (dev_is_sata(dev)) {
222                 /* We don't need to rescan targets
223                 * if REPORT_LUNS request is failed
224                 */
225                 if (scsi_dev->lun > 0)
226                         return -ENXIO;
227                 scsi_dev->tagged_supported = 1;
228         }
229         return sas_slave_alloc(scsi_dev);
230 }
231
232 /**
233   * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
234   * command to HBA.
235   * @shost: the scsi host data.
236   */
237 void pm8001_scan_start(struct Scsi_Host *shost)
238 {
239         int i;
240         struct pm8001_hba_info *pm8001_ha;
241         struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
242         pm8001_ha = sha->lldd_ha;
243         for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
244                 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
245 }
246
247 int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
248 {
249         /* give the phy enabling interrupt event time to come in (1s
250         * is empirically about all it takes) */
251         if (time < HZ)
252                 return 0;
253         /* Wait for discovery to finish */
254         scsi_flush_work(shost);
255         return 1;
256 }
257
258 /**
259   * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
260   * @pm8001_ha: our hba card information
261   * @ccb: the ccb which attached to smp task
262   */
263 static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
264         struct pm8001_ccb_info *ccb)
265 {
266         return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
267 }
268
269 u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
270 {
271         struct ata_queued_cmd *qc = task->uldd_task;
272         if (qc) {
273                 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
274                         qc->tf.command == ATA_CMD_FPDMA_READ) {
275                         *tag = qc->tag;
276                         return 1;
277                 }
278         }
279         return 0;
280 }
281
282 /**
283   * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
284   * @pm8001_ha: our hba card information
285   * @ccb: the ccb which attached to sata task
286   */
287 static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
288         struct pm8001_ccb_info *ccb)
289 {
290         return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
291 }
292
293 /**
294   * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
295   * @pm8001_ha: our hba card information
296   * @ccb: the ccb which attached to TM
297   * @tmf: the task management IU
298   */
299 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
300         struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
301 {
302         return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
303 }
304
305 /**
306   * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
307   * @pm8001_ha: our hba card information
308   * @ccb: the ccb which attached to ssp task
309   */
310 static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
311         struct pm8001_ccb_info *ccb)
312 {
313         return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
314 }
315 int pm8001_slave_configure(struct scsi_device *sdev)
316 {
317         struct domain_device *dev = sdev_to_domain_dev(sdev);
318         int ret = sas_slave_configure(sdev);
319         if (ret)
320                 return ret;
321         if (dev_is_sata(dev)) {
322         #ifdef PM8001_DISABLE_NCQ
323                 struct ata_port *ap = dev->sata_dev.ap;
324                 struct ata_device *adev = ap->link.device;
325                 adev->flags |= ATA_DFLAG_NCQ_OFF;
326                 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, 1);
327         #endif
328         }
329         return 0;
330 }
331 /**
332   * pm8001_task_exec -execute the task which come from upper level, send the
333   * command or data to DMA area and then increase CI,for queuecommand(ssp),
334   * it is from upper layer and for smp command,it is from libsas,
335   * for ata command it is from libata.
336   * @task: the task to be execute.
337   * @num: if can_queue great than 1, the task can be queued up. for SMP task,
338   * we always execute one one time.
339   * @gfp_flags: gfp_flags.
340   * @is tmf: if it is task management task.
341   * @tmf: the task management IU
342   */
343 #define DEV_IS_GONE(pm8001_dev) \
344         ((!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE)))
345 static int pm8001_task_exec(struct sas_task *task, const int num,
346         gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
347 {
348         struct domain_device *dev = task->dev;
349         struct pm8001_hba_info *pm8001_ha;
350         struct pm8001_device *pm8001_dev;
351         struct sas_task *t = task;
352         struct pm8001_ccb_info *ccb;
353         u32 tag = 0xdeadbeef, rc, n_elem = 0;
354         u32 n = num;
355         unsigned long flags = 0;
356
357         if (!dev->port) {
358                 struct task_status_struct *tsm = &t->task_status;
359                 tsm->resp = SAS_TASK_UNDELIVERED;
360                 tsm->stat = SAS_PHY_DOWN;
361                 if (dev->dev_type != SATA_DEV)
362                         t->task_done(t);
363                 return 0;
364         }
365         pm8001_ha = pm8001_find_ha_by_dev(task->dev);
366         PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n "));
367         spin_lock_irqsave(&pm8001_ha->lock, flags);
368         do {
369                 dev = t->dev;
370                 pm8001_dev = dev->lldd_dev;
371                 if (DEV_IS_GONE(pm8001_dev)) {
372                         if (pm8001_dev) {
373                                 PM8001_IO_DBG(pm8001_ha,
374                                         pm8001_printk("device %d not ready.\n",
375                                         pm8001_dev->device_id));
376                         } else {
377                                 PM8001_IO_DBG(pm8001_ha,
378                                         pm8001_printk("device %016llx not "
379                                         "ready.\n", SAS_ADDR(dev->sas_addr)));
380                         }
381                 rc = SAS_PHY_DOWN;
382                         goto out_done;
383                 }
384                 rc = pm8001_tag_alloc(pm8001_ha, &tag);
385                 if (rc)
386                         goto err_out;
387                 ccb = &pm8001_ha->ccb_info[tag];
388
389                 if (!sas_protocol_ata(t->task_proto)) {
390                         if (t->num_scatter) {
391                                 n_elem = dma_map_sg(pm8001_ha->dev,
392                                         t->scatter,
393                                         t->num_scatter,
394                                         t->data_dir);
395                                 if (!n_elem) {
396                                         rc = -ENOMEM;
397                                         goto err_out;
398                                 }
399                         }
400                 } else {
401                         n_elem = t->num_scatter;
402                 }
403
404                 t->lldd_task = NULL;
405                 ccb->n_elem = n_elem;
406                 ccb->ccb_tag = tag;
407                 ccb->task = t;
408                 switch (t->task_proto) {
409                 case SAS_PROTOCOL_SMP:
410                         rc = pm8001_task_prep_smp(pm8001_ha, ccb);
411                         break;
412                 case SAS_PROTOCOL_SSP:
413                         if (is_tmf)
414                                 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
415                                         ccb, tmf);
416                         else
417                                 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
418                         break;
419                 case SAS_PROTOCOL_SATA:
420                 case SAS_PROTOCOL_STP:
421                 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
422                         rc = pm8001_task_prep_ata(pm8001_ha, ccb);
423                         break;
424                 default:
425                         dev_printk(KERN_ERR, pm8001_ha->dev,
426                                 "unknown sas_task proto: 0x%x\n",
427                                 t->task_proto);
428                         rc = -EINVAL;
429                         break;
430                 }
431
432                 if (rc) {
433                         PM8001_IO_DBG(pm8001_ha,
434                                 pm8001_printk("rc is %x\n", rc));
435                         goto err_out_tag;
436                 }
437                 t->lldd_task = ccb;
438                 /* TODO: select normal or high priority */
439                 spin_lock(&t->task_state_lock);
440                 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
441                 spin_unlock(&t->task_state_lock);
442                 pm8001_dev->running_req++;
443                 if (n > 1)
444                         t = list_entry(t->list.next, struct sas_task, list);
445         } while (--n);
446         rc = 0;
447         goto out_done;
448
449 err_out_tag:
450         pm8001_tag_free(pm8001_ha, tag);
451 err_out:
452         dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
453         if (!sas_protocol_ata(t->task_proto))
454                 if (n_elem)
455                         dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem,
456                                 t->data_dir);
457 out_done:
458         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
459         return rc;
460 }
461
462 /**
463   * pm8001_queue_command - register for upper layer used, all IO commands sent
464   * to HBA are from this interface.
465   * @task: the task to be execute.
466   * @num: if can_queue great than 1, the task can be queued up. for SMP task,
467   * we always execute one one time
468   * @gfp_flags: gfp_flags
469   */
470 int pm8001_queue_command(struct sas_task *task, const int num,
471                 gfp_t gfp_flags)
472 {
473         return pm8001_task_exec(task, num, gfp_flags, 0, NULL);
474 }
475
476 void pm8001_ccb_free(struct pm8001_hba_info *pm8001_ha, u32 ccb_idx)
477 {
478         pm8001_tag_clear(pm8001_ha, ccb_idx);
479 }
480
481 /**
482   * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
483   * @pm8001_ha: our hba card information
484   * @ccb: the ccb which attached to ssp task
485   * @task: the task to be free.
486   * @ccb_idx: ccb index.
487   */
488 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
489         struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
490 {
491         if (!ccb->task)
492                 return;
493         if (!sas_protocol_ata(task->task_proto))
494                 if (ccb->n_elem)
495                         dma_unmap_sg(pm8001_ha->dev, task->scatter,
496                                 task->num_scatter, task->data_dir);
497
498         switch (task->task_proto) {
499         case SAS_PROTOCOL_SMP:
500                 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
501                         PCI_DMA_FROMDEVICE);
502                 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
503                         PCI_DMA_TODEVICE);
504                 break;
505
506         case SAS_PROTOCOL_SATA:
507         case SAS_PROTOCOL_STP:
508         case SAS_PROTOCOL_SSP:
509         default:
510                 /* do nothing */
511                 break;
512         }
513         task->lldd_task = NULL;
514         ccb->task = NULL;
515         ccb->ccb_tag = 0xFFFFFFFF;
516         pm8001_ccb_free(pm8001_ha, ccb_idx);
517 }
518
519  /**
520   * pm8001_alloc_dev - find the empty pm8001_device structure, allocate and
521   * return it for use.
522   * @pm8001_ha: our hba card information
523   */
524 struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
525 {
526         u32 dev;
527         for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
528                 if (pm8001_ha->devices[dev].dev_type == NO_DEVICE) {
529                         pm8001_ha->devices[dev].id = dev;
530                         return &pm8001_ha->devices[dev];
531                 }
532         }
533         if (dev == PM8001_MAX_DEVICES) {
534                 PM8001_FAIL_DBG(pm8001_ha,
535                         pm8001_printk("max support %d devices, ignore ..\n",
536                         PM8001_MAX_DEVICES));
537         }
538         return NULL;
539 }
540
541 static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
542 {
543         u32 id = pm8001_dev->id;
544         memset(pm8001_dev, 0, sizeof(*pm8001_dev));
545         pm8001_dev->id = id;
546         pm8001_dev->dev_type = NO_DEVICE;
547         pm8001_dev->device_id = PM8001_MAX_DEVICES;
548         pm8001_dev->sas_device = NULL;
549 }
550
551 /**
552   * pm8001_dev_found_notify - when libsas find a sas domain device, it should
553   * tell the LLDD that device is found, and then LLDD register this device to
554   * HBA FW by the command "OPC_INB_REG_DEV", after that the HBA will assign
555   * a device ID(according to device's sas address) and returned it to LLDD.from
556   * now on, we communicate with HBA FW with the device ID which HBA assigned
557   * rather than sas address. it is the neccessary step for our HBA but it is
558   * the optional for other HBA driver.
559   * @dev: the device structure which sas layer used.
560   */
561 static int pm8001_dev_found_notify(struct domain_device *dev)
562 {
563         unsigned long flags = 0;
564         int res = 0;
565         struct pm8001_hba_info *pm8001_ha = NULL;
566         struct domain_device *parent_dev = dev->parent;
567         struct pm8001_device *pm8001_device;
568         DECLARE_COMPLETION_ONSTACK(completion);
569         u32 flag = 0;
570         pm8001_ha = pm8001_find_ha_by_dev(dev);
571         spin_lock_irqsave(&pm8001_ha->lock, flags);
572
573         pm8001_device = pm8001_alloc_dev(pm8001_ha);
574         pm8001_device->sas_device = dev;
575         if (!pm8001_device) {
576                 res = -1;
577                 goto found_out;
578         }
579         dev->lldd_dev = pm8001_device;
580         pm8001_device->dev_type = dev->dev_type;
581         pm8001_device->dcompletion = &completion;
582         if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
583                 int phy_id;
584                 struct ex_phy *phy;
585                 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
586                 phy_id++) {
587                         phy = &parent_dev->ex_dev.ex_phy[phy_id];
588                         if (SAS_ADDR(phy->attached_sas_addr)
589                                 == SAS_ADDR(dev->sas_addr)) {
590                                 pm8001_device->attached_phy = phy_id;
591                                 break;
592                         }
593                 }
594                 if (phy_id == parent_dev->ex_dev.num_phys) {
595                         PM8001_FAIL_DBG(pm8001_ha,
596                         pm8001_printk("Error: no attached dev:%016llx"
597                         " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
598                                 SAS_ADDR(parent_dev->sas_addr)));
599                         res = -1;
600                 }
601         } else {
602                 if (dev->dev_type == SATA_DEV) {
603                         pm8001_device->attached_phy =
604                                 dev->rphy->identify.phy_identifier;
605                                 flag = 1; /* directly sata*/
606                 }
607         } /*register this device to HBA*/
608         PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device \n"));
609         PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
610         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
611         wait_for_completion(&completion);
612         if (dev->dev_type == SAS_END_DEV)
613                 msleep(50);
614         pm8001_ha->flags = PM8001F_RUN_TIME ;
615         return 0;
616 found_out:
617         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
618         return res;
619 }
620
621 int pm8001_dev_found(struct domain_device *dev)
622 {
623         return pm8001_dev_found_notify(dev);
624 }
625
626 /**
627   * pm8001_alloc_task - allocate a task structure for TMF
628   */
629 static struct sas_task *pm8001_alloc_task(void)
630 {
631         struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
632         if (task) {
633                 INIT_LIST_HEAD(&task->list);
634                 spin_lock_init(&task->task_state_lock);
635                 task->task_state_flags = SAS_TASK_STATE_PENDING;
636                 init_timer(&task->timer);
637                 init_completion(&task->completion);
638         }
639         return task;
640 }
641
642 static void pm8001_free_task(struct sas_task *task)
643 {
644         if (task) {
645                 BUG_ON(!list_empty(&task->list));
646                 kfree(task);
647         }
648 }
649
650 static void pm8001_task_done(struct sas_task *task)
651 {
652         if (!del_timer(&task->timer))
653                 return;
654         complete(&task->completion);
655 }
656
657 static void pm8001_tmf_timedout(unsigned long data)
658 {
659         struct sas_task *task = (struct sas_task *)data;
660
661         task->task_state_flags |= SAS_TASK_STATE_ABORTED;
662         complete(&task->completion);
663 }
664
665 #define PM8001_TASK_TIMEOUT 20
666 /**
667   * pm8001_exec_internal_tmf_task - when errors or exception happened, we may
668   * want to do something, for example abort issued task which result in this
669   * execption, this is by calling this function, note it is also with the task
670   * execute interface.
671   * @dev: the wanted device.
672   * @tmf: which task management wanted to be take.
673   * @para_len: para_len.
674   * @parameter: ssp task parameter.
675   */
676 static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
677         void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
678 {
679         int res, retry;
680         struct sas_task *task = NULL;
681         struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
682
683         for (retry = 0; retry < 3; retry++) {
684                 task = pm8001_alloc_task();
685                 if (!task)
686                         return -ENOMEM;
687
688                 task->dev = dev;
689                 task->task_proto = dev->tproto;
690                 memcpy(&task->ssp_task, parameter, para_len);
691                 task->task_done = pm8001_task_done;
692                 task->timer.data = (unsigned long)task;
693                 task->timer.function = pm8001_tmf_timedout;
694                 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
695                 add_timer(&task->timer);
696
697                 res = pm8001_task_exec(task, 1, GFP_KERNEL, 1, tmf);
698
699                 if (res) {
700                         del_timer(&task->timer);
701                         PM8001_FAIL_DBG(pm8001_ha,
702                                 pm8001_printk("Executing internal task "
703                                 "failed\n"));
704                         goto ex_err;
705                 }
706                 wait_for_completion(&task->completion);
707                 res = -TMF_RESP_FUNC_FAILED;
708                 /* Even TMF timed out, return direct. */
709                 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
710                         if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
711                                 PM8001_FAIL_DBG(pm8001_ha,
712                                         pm8001_printk("TMF task[%x]timeout.\n",
713                                         tmf->tmf));
714                                 goto ex_err;
715                         }
716                 }
717
718                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
719                         task->task_status.stat == SAM_GOOD) {
720                         res = TMF_RESP_FUNC_COMPLETE;
721                         break;
722                 }
723
724                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
725                 task->task_status.stat == SAS_DATA_UNDERRUN) {
726                         /* no error, but return the number of bytes of
727                         * underrun */
728                         res = task->task_status.residual;
729                         break;
730                 }
731
732                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
733                         task->task_status.stat == SAS_DATA_OVERRUN) {
734                         PM8001_FAIL_DBG(pm8001_ha,
735                                 pm8001_printk("Blocked task error.\n"));
736                         res = -EMSGSIZE;
737                         break;
738                 } else {
739                         PM8001_IO_DBG(pm8001_ha,
740                         pm8001_printk(" Task to dev %016llx response: 0x%x"
741                                 "status 0x%x\n",
742                                 SAS_ADDR(dev->sas_addr),
743                                 task->task_status.resp,
744                                 task->task_status.stat));
745                         pm8001_free_task(task);
746                         task = NULL;
747                 }
748         }
749 ex_err:
750         BUG_ON(retry == 3 && task != NULL);
751         if (task != NULL)
752                 pm8001_free_task(task);
753         return res;
754 }
755
756 static int
757 pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
758         struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
759         u32 task_tag)
760 {
761         int res, retry;
762         u32 rc, ccb_tag;
763         struct pm8001_ccb_info *ccb;
764         struct sas_task *task = NULL;
765
766         for (retry = 0; retry < 3; retry++) {
767                 task = pm8001_alloc_task();
768                 if (!task)
769                         return -ENOMEM;
770
771                 task->dev = dev;
772                 task->task_proto = dev->tproto;
773                 task->task_done = pm8001_task_done;
774                 task->timer.data = (unsigned long)task;
775                 task->timer.function = pm8001_tmf_timedout;
776                 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
777                 add_timer(&task->timer);
778
779                 rc = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
780                 if (rc)
781                         return rc;
782                 ccb = &pm8001_ha->ccb_info[ccb_tag];
783                 ccb->device = pm8001_dev;
784                 ccb->ccb_tag = ccb_tag;
785                 ccb->task = task;
786
787                 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
788                         pm8001_dev, flag, task_tag, ccb_tag);
789
790                 if (res) {
791                         del_timer(&task->timer);
792                         PM8001_FAIL_DBG(pm8001_ha,
793                                 pm8001_printk("Executing internal task "
794                                 "failed\n"));
795                         goto ex_err;
796                 }
797                 wait_for_completion(&task->completion);
798                 res = TMF_RESP_FUNC_FAILED;
799                 /* Even TMF timed out, return direct. */
800                 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
801                         if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
802                                 PM8001_FAIL_DBG(pm8001_ha,
803                                         pm8001_printk("TMF task timeout.\n"));
804                                 goto ex_err;
805                         }
806                 }
807
808                 if (task->task_status.resp == SAS_TASK_COMPLETE &&
809                         task->task_status.stat == SAM_GOOD) {
810                         res = TMF_RESP_FUNC_COMPLETE;
811                         break;
812
813                 } else {
814                         PM8001_IO_DBG(pm8001_ha,
815                                 pm8001_printk(" Task to dev %016llx response: "
816                                         "0x%x status 0x%x\n",
817                                 SAS_ADDR(dev->sas_addr),
818                                 task->task_status.resp,
819                                 task->task_status.stat));
820                         pm8001_free_task(task);
821                         task = NULL;
822                 }
823         }
824 ex_err:
825         BUG_ON(retry == 3 && task != NULL);
826         if (task != NULL)
827                 pm8001_free_task(task);
828         return res;
829 }
830
831 /**
832   * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
833   * @dev: the device structure which sas layer used.
834   */
835 static void pm8001_dev_gone_notify(struct domain_device *dev)
836 {
837         unsigned long flags = 0;
838         u32 tag;
839         struct pm8001_hba_info *pm8001_ha;
840         struct pm8001_device *pm8001_dev = dev->lldd_dev;
841         u32 device_id = pm8001_dev->device_id;
842         pm8001_ha = pm8001_find_ha_by_dev(dev);
843         spin_lock_irqsave(&pm8001_ha->lock, flags);
844         pm8001_tag_alloc(pm8001_ha, &tag);
845         if (pm8001_dev) {
846                 PM8001_DISC_DBG(pm8001_ha,
847                         pm8001_printk("found dev[%d:%x] is gone.\n",
848                         pm8001_dev->device_id, pm8001_dev->dev_type));
849                 if (pm8001_dev->running_req) {
850                         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
851                         pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
852                                 dev, 1, 0);
853                         spin_lock_irqsave(&pm8001_ha->lock, flags);
854                 }
855                 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
856                 pm8001_free_dev(pm8001_dev);
857         } else {
858                 PM8001_DISC_DBG(pm8001_ha,
859                         pm8001_printk("Found dev has gone.\n"));
860         }
861         dev->lldd_dev = NULL;
862         spin_unlock_irqrestore(&pm8001_ha->lock, flags);
863 }
864
865 void pm8001_dev_gone(struct domain_device *dev)
866 {
867         pm8001_dev_gone_notify(dev);
868 }
869
870 static int pm8001_issue_ssp_tmf(struct domain_device *dev,
871         u8 *lun, struct pm8001_tmf_task *tmf)
872 {
873         struct sas_ssp_task ssp_task;
874         if (!(dev->tproto & SAS_PROTOCOL_SSP))
875                 return TMF_RESP_FUNC_ESUPP;
876
877         strncpy((u8 *)&ssp_task.LUN, lun, 8);
878         return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
879                 tmf);
880 }
881
882 /**
883   * Standard mandates link reset for ATA  (type 0) and hard reset for
884   * SSP (type 1) , only for RECOVERY
885   */
886 int pm8001_I_T_nexus_reset(struct domain_device *dev)
887 {
888         int rc = TMF_RESP_FUNC_FAILED;
889         struct pm8001_device *pm8001_dev;
890         struct pm8001_hba_info *pm8001_ha;
891         struct sas_phy *phy;
892         if (!dev || !dev->lldd_dev)
893                 return -1;
894
895         pm8001_dev = dev->lldd_dev;
896         pm8001_ha = pm8001_find_ha_by_dev(dev);
897         phy = sas_find_local_phy(dev);
898
899         if (dev_is_sata(dev)) {
900                 DECLARE_COMPLETION_ONSTACK(completion_setstate);
901                 rc = sas_phy_reset(phy, 1);
902                 msleep(2000);
903                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
904                         dev, 1, 0);
905                 pm8001_dev->setds_completion = &completion_setstate;
906                 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
907                         pm8001_dev, 0x01);
908                 wait_for_completion(&completion_setstate);
909         } else{
910         rc = sas_phy_reset(phy, 1);
911         msleep(2000);
912         }
913         PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
914                 pm8001_dev->device_id, rc));
915         return rc;
916 }
917
918 /* mandatory SAM-3, the task reset the specified LUN*/
919 int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
920 {
921         int rc = TMF_RESP_FUNC_FAILED;
922         struct pm8001_tmf_task tmf_task;
923         struct pm8001_device *pm8001_dev = dev->lldd_dev;
924         struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
925         if (dev_is_sata(dev)) {
926                 struct sas_phy *phy = sas_find_local_phy(dev);
927                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
928                         dev, 1, 0);
929                 rc = sas_phy_reset(phy, 1);
930                 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
931                         pm8001_dev, 0x01);
932                 msleep(2000);
933         } else {
934                 tmf_task.tmf = TMF_LU_RESET;
935                 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
936         }
937         /* If failed, fall-through I_T_Nexus reset */
938         PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
939                 pm8001_dev->device_id, rc));
940         return rc;
941 }
942
943 /* optional SAM-3 */
944 int pm8001_query_task(struct sas_task *task)
945 {
946         u32 tag = 0xdeadbeef;
947         int i = 0;
948         struct scsi_lun lun;
949         struct pm8001_tmf_task tmf_task;
950         int rc = TMF_RESP_FUNC_FAILED;
951         if (unlikely(!task || !task->lldd_task || !task->dev))
952                 return rc;
953
954         if (task->task_proto & SAS_PROTOCOL_SSP) {
955                 struct scsi_cmnd *cmnd = task->uldd_task;
956                 struct domain_device *dev = task->dev;
957                 struct pm8001_hba_info *pm8001_ha =
958                         pm8001_find_ha_by_dev(dev);
959
960                 int_to_scsilun(cmnd->device->lun, &lun);
961                 rc = pm8001_find_tag(task, &tag);
962                 if (rc == 0) {
963                         rc = TMF_RESP_FUNC_FAILED;
964                         return rc;
965                 }
966                 PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
967                 for (i = 0; i < 16; i++)
968                         printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
969                 printk(KERN_INFO "]\n");
970                 tmf_task.tmf =  TMF_QUERY_TASK;
971                 tmf_task.tag_of_task_to_be_managed = tag;
972
973                 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
974                 switch (rc) {
975                 /* The task is still in Lun, release it then */
976                 case TMF_RESP_FUNC_SUCC:
977                         PM8001_EH_DBG(pm8001_ha,
978                                 pm8001_printk("The task is still in Lun \n"));
979                 /* The task is not in Lun or failed, reset the phy */
980                 case TMF_RESP_FUNC_FAILED:
981                 case TMF_RESP_FUNC_COMPLETE:
982                         PM8001_EH_DBG(pm8001_ha,
983                         pm8001_printk("The task is not in Lun or failed,"
984                         " reset the phy \n"));
985                         break;
986                 }
987         }
988         pm8001_printk(":rc= %d\n", rc);
989         return rc;
990 }
991
992 /*  mandatory SAM-3, still need free task/ccb info, abord the specified task */
993 int pm8001_abort_task(struct sas_task *task)
994 {
995         unsigned long flags;
996         u32 tag = 0xdeadbeef;
997         u32 device_id;
998         struct domain_device *dev ;
999         struct pm8001_hba_info *pm8001_ha = NULL;
1000         struct pm8001_ccb_info *ccb;
1001         struct scsi_lun lun;
1002         struct pm8001_device *pm8001_dev;
1003         struct pm8001_tmf_task tmf_task;
1004         int rc = TMF_RESP_FUNC_FAILED;
1005         if (unlikely(!task || !task->lldd_task || !task->dev))
1006                 return rc;
1007         spin_lock_irqsave(&task->task_state_lock, flags);
1008         if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1009                 spin_unlock_irqrestore(&task->task_state_lock, flags);
1010                 rc = TMF_RESP_FUNC_COMPLETE;
1011                 goto out;
1012         }
1013         spin_unlock_irqrestore(&task->task_state_lock, flags);
1014         if (task->task_proto & SAS_PROTOCOL_SSP) {
1015                 struct scsi_cmnd *cmnd = task->uldd_task;
1016                 dev = task->dev;
1017                 ccb = task->lldd_task;
1018                 pm8001_dev = dev->lldd_dev;
1019                 pm8001_ha = pm8001_find_ha_by_dev(dev);
1020                 int_to_scsilun(cmnd->device->lun, &lun);
1021                 rc = pm8001_find_tag(task, &tag);
1022                 if (rc == 0) {
1023                         printk(KERN_INFO "No such tag in %s\n", __func__);
1024                         rc = TMF_RESP_FUNC_FAILED;
1025                         return rc;
1026                 }
1027                 device_id = pm8001_dev->device_id;
1028                 PM8001_EH_DBG(pm8001_ha,
1029                 pm8001_printk("abort io to device_id = %d\n", device_id));
1030                 tmf_task.tmf =  TMF_ABORT_TASK;
1031                 tmf_task.tag_of_task_to_be_managed = tag;
1032                 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1033                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1034                         pm8001_dev->sas_device, 0, tag);
1035         } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1036                 task->task_proto & SAS_PROTOCOL_STP) {
1037                 dev = task->dev;
1038                 pm8001_dev = dev->lldd_dev;
1039                 pm8001_ha = pm8001_find_ha_by_dev(dev);
1040                 rc = pm8001_find_tag(task, &tag);
1041                 if (rc == 0) {
1042                         printk(KERN_INFO "No such tag in %s\n", __func__);
1043                         rc = TMF_RESP_FUNC_FAILED;
1044                         return rc;
1045                 }
1046                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1047                         pm8001_dev->sas_device, 0, tag);
1048         } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1049                 /* SMP */
1050                 dev = task->dev;
1051                 pm8001_dev = dev->lldd_dev;
1052                 pm8001_ha = pm8001_find_ha_by_dev(dev);
1053                 rc = pm8001_find_tag(task, &tag);
1054                 if (rc == 0) {
1055                         printk(KERN_INFO "No such tag in %s\n", __func__);
1056                         rc = TMF_RESP_FUNC_FAILED;
1057                         return rc;
1058                 }
1059                 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1060                         pm8001_dev->sas_device, 0, tag);
1061
1062         }
1063 out:
1064         if (rc != TMF_RESP_FUNC_COMPLETE)
1065                 pm8001_printk("rc= %d\n", rc);
1066         return rc;
1067 }
1068
1069 int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1070 {
1071         int rc = TMF_RESP_FUNC_FAILED;
1072         struct pm8001_tmf_task tmf_task;
1073
1074         tmf_task.tmf = TMF_ABORT_TASK_SET;
1075         rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1076         return rc;
1077 }
1078
1079 int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1080 {
1081         int rc = TMF_RESP_FUNC_FAILED;
1082         struct pm8001_tmf_task tmf_task;
1083
1084         tmf_task.tmf = TMF_CLEAR_ACA;
1085         rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1086
1087         return rc;
1088 }
1089
1090 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1091 {
1092         int rc = TMF_RESP_FUNC_FAILED;
1093         struct pm8001_tmf_task tmf_task;
1094         struct pm8001_device *pm8001_dev = dev->lldd_dev;
1095         struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1096
1097         PM8001_EH_DBG(pm8001_ha,
1098                 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1099                 pm8001_dev->device_id));
1100         tmf_task.tmf = TMF_CLEAR_TASK_SET;
1101         rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1102         return rc;
1103 }
1104