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