Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / scsi / pm8001 / pm8001_sas.c
blob7dbbf8bb4d885c21891f9e3f4a6d74a2676b0da5
1 /*
2 * PMC-Sierra SPC 8001 SAS/SATA based host adapters driver
4 * Copyright (c) 2008-2009 USI Co., Ltd.
5 * All rights reserved.
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.
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.
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.
41 #include <linux/slab.h>
42 #include "pm8001_sas.h"
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
49 static int pm8001_find_tag(struct sas_task *task, u32 *tag)
51 if (task->lldd_task) {
52 struct pm8001_ccb_info *ccb;
53 ccb = task->lldd_task;
54 *tag = ccb->ccb_tag;
55 return 1;
57 return 0;
60 /**
61 * pm8001_tag_clear - clear the tags bitmap
62 * @pm8001_ha: our hba struct
63 * @tag: the found tag associated with the task
65 static void pm8001_tag_clear(struct pm8001_hba_info *pm8001_ha, u32 tag)
67 void *bitmap = pm8001_ha->tags;
68 clear_bit(tag, bitmap);
71 static void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
73 pm8001_tag_clear(pm8001_ha, tag);
76 static void pm8001_tag_set(struct pm8001_hba_info *pm8001_ha, u32 tag)
78 void *bitmap = pm8001_ha->tags;
79 set_bit(tag, bitmap);
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 .
87 inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
89 unsigned int index, tag;
90 void *bitmap = pm8001_ha->tags;
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;
101 void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha)
103 int i;
104 for (i = 0; i < pm8001_ha->tags_num; ++i)
105 pm8001_tag_clear(pm8001_ha, i);
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.
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)
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;
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;
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.
145 static
146 struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
148 struct sas_ha_struct *sha = dev->port->ha;
149 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
150 return pm8001_ha;
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.
162 int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
163 void *funcdata)
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;
178 if (rates->maximum_linkrate) {
179 pm8001_ha->phy[phy_id].maximum_linkrate =
180 rates->maximum_linkrate;
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);
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);
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);
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;
215 msleep(300);
216 return rc;
219 int pm8001_slave_alloc(struct scsi_device *scsi_dev)
221 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
222 if (dev_is_sata(dev)) {
223 /* We don't need to rescan targets
224 * if REPORT_LUNS request is failed
226 if (scsi_dev->lun > 0)
227 return -ENXIO;
228 scsi_dev->tagged_supported = 1;
230 return sas_slave_alloc(scsi_dev);
234 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
235 * command to HBA.
236 * @shost: the scsi host data.
238 void pm8001_scan_start(struct Scsi_Host *shost)
240 int i;
241 struct pm8001_hba_info *pm8001_ha;
242 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
243 pm8001_ha = sha->lldd_ha;
244 PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
245 for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
246 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
249 int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
251 /* give the phy enabling interrupt event time to come in (1s
252 * is empirically about all it takes) */
253 if (time < HZ)
254 return 0;
255 /* Wait for discovery to finish */
256 scsi_flush_work(shost);
257 return 1;
261 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
262 * @pm8001_ha: our hba card information
263 * @ccb: the ccb which attached to smp task
265 static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
266 struct pm8001_ccb_info *ccb)
268 return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
271 u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
273 struct ata_queued_cmd *qc = task->uldd_task;
274 if (qc) {
275 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
276 qc->tf.command == ATA_CMD_FPDMA_READ) {
277 *tag = qc->tag;
278 return 1;
281 return 0;
285 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
286 * @pm8001_ha: our hba card information
287 * @ccb: the ccb which attached to sata task
289 static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
290 struct pm8001_ccb_info *ccb)
292 return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
296 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
297 * @pm8001_ha: our hba card information
298 * @ccb: the ccb which attached to TM
299 * @tmf: the task management IU
301 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
302 struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
304 return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
308 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
309 * @pm8001_ha: our hba card information
310 * @ccb: the ccb which attached to ssp task
312 static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
313 struct pm8001_ccb_info *ccb)
315 return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
317 int pm8001_slave_configure(struct scsi_device *sdev)
319 struct domain_device *dev = sdev_to_domain_dev(sdev);
320 int ret = sas_slave_configure(sdev);
321 if (ret)
322 return ret;
323 if (dev_is_sata(dev)) {
324 #ifdef PM8001_DISABLE_NCQ
325 struct ata_port *ap = dev->sata_dev.ap;
326 struct ata_device *adev = ap->link.device;
327 adev->flags |= ATA_DFLAG_NCQ_OFF;
328 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, 1);
329 #endif
331 return 0;
333 /* Find the local port id that's attached to this device */
334 static int sas_find_local_port_id(struct domain_device *dev)
336 struct domain_device *pdev = dev->parent;
338 /* Directly attached device */
339 if (!pdev)
340 return dev->port->id;
341 while (pdev) {
342 struct domain_device *pdev_p = pdev->parent;
343 if (!pdev_p)
344 return pdev->port->id;
345 pdev = pdev->parent;
347 return 0;
351 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
352 * @task: the task to be execute.
353 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
354 * we always execute one one time.
355 * @gfp_flags: gfp_flags.
356 * @is_tmf: if it is task management task.
357 * @tmf: the task management IU
359 #define DEV_IS_GONE(pm8001_dev) \
360 ((!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE)))
361 static int pm8001_task_exec(struct sas_task *task, const int num,
362 gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
364 struct domain_device *dev = task->dev;
365 struct pm8001_hba_info *pm8001_ha;
366 struct pm8001_device *pm8001_dev;
367 struct pm8001_port *port = NULL;
368 struct sas_task *t = task;
369 struct pm8001_ccb_info *ccb;
370 u32 tag = 0xdeadbeef, rc, n_elem = 0;
371 u32 n = num;
372 unsigned long flags = 0, flags_libsas = 0;
374 if (!dev->port) {
375 struct task_status_struct *tsm = &t->task_status;
376 tsm->resp = SAS_TASK_UNDELIVERED;
377 tsm->stat = SAS_PHY_DOWN;
378 if (dev->dev_type != SATA_DEV)
379 t->task_done(t);
380 return 0;
382 pm8001_ha = pm8001_find_ha_by_dev(task->dev);
383 PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n "));
384 spin_lock_irqsave(&pm8001_ha->lock, flags);
385 do {
386 dev = t->dev;
387 pm8001_dev = dev->lldd_dev;
388 if (DEV_IS_GONE(pm8001_dev)) {
389 if (pm8001_dev) {
390 PM8001_IO_DBG(pm8001_ha,
391 pm8001_printk("device %d not ready.\n",
392 pm8001_dev->device_id));
393 } else {
394 PM8001_IO_DBG(pm8001_ha,
395 pm8001_printk("device %016llx not "
396 "ready.\n", SAS_ADDR(dev->sas_addr)));
398 rc = SAS_PHY_DOWN;
399 goto out_done;
401 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
402 if (!port->port_attached) {
403 if (sas_protocol_ata(t->task_proto)) {
404 struct task_status_struct *ts = &t->task_status;
405 ts->resp = SAS_TASK_UNDELIVERED;
406 ts->stat = SAS_PHY_DOWN;
408 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
409 spin_unlock_irqrestore(dev->sata_dev.ap->lock,
410 flags_libsas);
411 t->task_done(t);
412 spin_lock_irqsave(dev->sata_dev.ap->lock,
413 flags_libsas);
414 spin_lock_irqsave(&pm8001_ha->lock, flags);
415 if (n > 1)
416 t = list_entry(t->list.next,
417 struct sas_task, list);
418 continue;
419 } else {
420 struct task_status_struct *ts = &t->task_status;
421 ts->resp = SAS_TASK_UNDELIVERED;
422 ts->stat = SAS_PHY_DOWN;
423 t->task_done(t);
424 if (n > 1)
425 t = list_entry(t->list.next,
426 struct sas_task, list);
427 continue;
430 rc = pm8001_tag_alloc(pm8001_ha, &tag);
431 if (rc)
432 goto err_out;
433 ccb = &pm8001_ha->ccb_info[tag];
435 if (!sas_protocol_ata(t->task_proto)) {
436 if (t->num_scatter) {
437 n_elem = dma_map_sg(pm8001_ha->dev,
438 t->scatter,
439 t->num_scatter,
440 t->data_dir);
441 if (!n_elem) {
442 rc = -ENOMEM;
443 goto err_out_tag;
446 } else {
447 n_elem = t->num_scatter;
450 t->lldd_task = ccb;
451 ccb->n_elem = n_elem;
452 ccb->ccb_tag = tag;
453 ccb->task = t;
454 switch (t->task_proto) {
455 case SAS_PROTOCOL_SMP:
456 rc = pm8001_task_prep_smp(pm8001_ha, ccb);
457 break;
458 case SAS_PROTOCOL_SSP:
459 if (is_tmf)
460 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
461 ccb, tmf);
462 else
463 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
464 break;
465 case SAS_PROTOCOL_SATA:
466 case SAS_PROTOCOL_STP:
467 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
468 rc = pm8001_task_prep_ata(pm8001_ha, ccb);
469 break;
470 default:
471 dev_printk(KERN_ERR, pm8001_ha->dev,
472 "unknown sas_task proto: 0x%x\n",
473 t->task_proto);
474 rc = -EINVAL;
475 break;
478 if (rc) {
479 PM8001_IO_DBG(pm8001_ha,
480 pm8001_printk("rc is %x\n", rc));
481 goto err_out_tag;
483 /* TODO: select normal or high priority */
484 spin_lock(&t->task_state_lock);
485 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
486 spin_unlock(&t->task_state_lock);
487 pm8001_dev->running_req++;
488 if (n > 1)
489 t = list_entry(t->list.next, struct sas_task, list);
490 } while (--n);
491 rc = 0;
492 goto out_done;
494 err_out_tag:
495 pm8001_tag_free(pm8001_ha, tag);
496 err_out:
497 dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
498 if (!sas_protocol_ata(t->task_proto))
499 if (n_elem)
500 dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem,
501 t->data_dir);
502 out_done:
503 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
504 return rc;
508 * pm8001_queue_command - register for upper layer used, all IO commands sent
509 * to HBA are from this interface.
510 * @task: the task to be execute.
511 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
512 * we always execute one one time
513 * @gfp_flags: gfp_flags
515 int pm8001_queue_command(struct sas_task *task, const int num,
516 gfp_t gfp_flags)
518 return pm8001_task_exec(task, num, gfp_flags, 0, NULL);
521 void pm8001_ccb_free(struct pm8001_hba_info *pm8001_ha, u32 ccb_idx)
523 pm8001_tag_clear(pm8001_ha, ccb_idx);
527 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
528 * @pm8001_ha: our hba card information
529 * @ccb: the ccb which attached to ssp task
530 * @task: the task to be free.
531 * @ccb_idx: ccb index.
533 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
534 struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
536 if (!ccb->task)
537 return;
538 if (!sas_protocol_ata(task->task_proto))
539 if (ccb->n_elem)
540 dma_unmap_sg(pm8001_ha->dev, task->scatter,
541 task->num_scatter, task->data_dir);
543 switch (task->task_proto) {
544 case SAS_PROTOCOL_SMP:
545 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
546 PCI_DMA_FROMDEVICE);
547 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
548 PCI_DMA_TODEVICE);
549 break;
551 case SAS_PROTOCOL_SATA:
552 case SAS_PROTOCOL_STP:
553 case SAS_PROTOCOL_SSP:
554 default:
555 /* do nothing */
556 break;
558 task->lldd_task = NULL;
559 ccb->task = NULL;
560 ccb->ccb_tag = 0xFFFFFFFF;
561 pm8001_ccb_free(pm8001_ha, ccb_idx);
565 * pm8001_alloc_dev - find a empty pm8001_device
566 * @pm8001_ha: our hba card information
568 struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
570 u32 dev;
571 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
572 if (pm8001_ha->devices[dev].dev_type == NO_DEVICE) {
573 pm8001_ha->devices[dev].id = dev;
574 return &pm8001_ha->devices[dev];
577 if (dev == PM8001_MAX_DEVICES) {
578 PM8001_FAIL_DBG(pm8001_ha,
579 pm8001_printk("max support %d devices, ignore ..\n",
580 PM8001_MAX_DEVICES));
582 return NULL;
585 static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
587 u32 id = pm8001_dev->id;
588 memset(pm8001_dev, 0, sizeof(*pm8001_dev));
589 pm8001_dev->id = id;
590 pm8001_dev->dev_type = NO_DEVICE;
591 pm8001_dev->device_id = PM8001_MAX_DEVICES;
592 pm8001_dev->sas_device = NULL;
596 * pm8001_dev_found_notify - libsas notify a device is found.
597 * @dev: the device structure which sas layer used.
599 * when libsas find a sas domain device, it should tell the LLDD that
600 * device is found, and then LLDD register this device to HBA firmware
601 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
602 * device ID(according to device's sas address) and returned it to LLDD. From
603 * now on, we communicate with HBA FW with the device ID which HBA assigned
604 * rather than sas address. it is the necessary step for our HBA but it is
605 * the optional for other HBA driver.
607 static int pm8001_dev_found_notify(struct domain_device *dev)
609 unsigned long flags = 0;
610 int res = 0;
611 struct pm8001_hba_info *pm8001_ha = NULL;
612 struct domain_device *parent_dev = dev->parent;
613 struct pm8001_device *pm8001_device;
614 DECLARE_COMPLETION_ONSTACK(completion);
615 u32 flag = 0;
616 pm8001_ha = pm8001_find_ha_by_dev(dev);
617 spin_lock_irqsave(&pm8001_ha->lock, flags);
619 pm8001_device = pm8001_alloc_dev(pm8001_ha);
620 if (!pm8001_device) {
621 res = -1;
622 goto found_out;
624 pm8001_device->sas_device = dev;
625 dev->lldd_dev = pm8001_device;
626 pm8001_device->dev_type = dev->dev_type;
627 pm8001_device->dcompletion = &completion;
628 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
629 int phy_id;
630 struct ex_phy *phy;
631 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
632 phy_id++) {
633 phy = &parent_dev->ex_dev.ex_phy[phy_id];
634 if (SAS_ADDR(phy->attached_sas_addr)
635 == SAS_ADDR(dev->sas_addr)) {
636 pm8001_device->attached_phy = phy_id;
637 break;
640 if (phy_id == parent_dev->ex_dev.num_phys) {
641 PM8001_FAIL_DBG(pm8001_ha,
642 pm8001_printk("Error: no attached dev:%016llx"
643 " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
644 SAS_ADDR(parent_dev->sas_addr)));
645 res = -1;
647 } else {
648 if (dev->dev_type == SATA_DEV) {
649 pm8001_device->attached_phy =
650 dev->rphy->identify.phy_identifier;
651 flag = 1; /* directly sata*/
653 } /*register this device to HBA*/
654 PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device \n"));
655 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
656 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
657 wait_for_completion(&completion);
658 if (dev->dev_type == SAS_END_DEV)
659 msleep(50);
660 pm8001_ha->flags |= PM8001F_RUN_TIME ;
661 return 0;
662 found_out:
663 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
664 return res;
667 int pm8001_dev_found(struct domain_device *dev)
669 return pm8001_dev_found_notify(dev);
672 static void pm8001_task_done(struct sas_task *task)
674 if (!del_timer(&task->timer))
675 return;
676 complete(&task->completion);
679 static void pm8001_tmf_timedout(unsigned long data)
681 struct sas_task *task = (struct sas_task *)data;
683 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
684 complete(&task->completion);
687 #define PM8001_TASK_TIMEOUT 20
689 * pm8001_exec_internal_tmf_task - execute some task management commands.
690 * @dev: the wanted device.
691 * @tmf: which task management wanted to be take.
692 * @para_len: para_len.
693 * @parameter: ssp task parameter.
695 * when errors or exception happened, we may want to do something, for example
696 * abort the issued task which result in this execption, it is done by calling
697 * this function, note it is also with the task execute interface.
699 static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
700 void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
702 int res, retry;
703 struct sas_task *task = NULL;
704 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
706 for (retry = 0; retry < 3; retry++) {
707 task = sas_alloc_task(GFP_KERNEL);
708 if (!task)
709 return -ENOMEM;
711 task->dev = dev;
712 task->task_proto = dev->tproto;
713 memcpy(&task->ssp_task, parameter, para_len);
714 task->task_done = pm8001_task_done;
715 task->timer.data = (unsigned long)task;
716 task->timer.function = pm8001_tmf_timedout;
717 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
718 add_timer(&task->timer);
720 res = pm8001_task_exec(task, 1, GFP_KERNEL, 1, tmf);
722 if (res) {
723 del_timer(&task->timer);
724 PM8001_FAIL_DBG(pm8001_ha,
725 pm8001_printk("Executing internal task "
726 "failed\n"));
727 goto ex_err;
729 wait_for_completion(&task->completion);
730 res = -TMF_RESP_FUNC_FAILED;
731 /* Even TMF timed out, return direct. */
732 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
733 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
734 PM8001_FAIL_DBG(pm8001_ha,
735 pm8001_printk("TMF task[%x]timeout.\n",
736 tmf->tmf));
737 goto ex_err;
741 if (task->task_status.resp == SAS_TASK_COMPLETE &&
742 task->task_status.stat == SAM_STAT_GOOD) {
743 res = TMF_RESP_FUNC_COMPLETE;
744 break;
747 if (task->task_status.resp == SAS_TASK_COMPLETE &&
748 task->task_status.stat == SAS_DATA_UNDERRUN) {
749 /* no error, but return the number of bytes of
750 * underrun */
751 res = task->task_status.residual;
752 break;
755 if (task->task_status.resp == SAS_TASK_COMPLETE &&
756 task->task_status.stat == SAS_DATA_OVERRUN) {
757 PM8001_FAIL_DBG(pm8001_ha,
758 pm8001_printk("Blocked task error.\n"));
759 res = -EMSGSIZE;
760 break;
761 } else {
762 PM8001_EH_DBG(pm8001_ha,
763 pm8001_printk(" Task to dev %016llx response:"
764 "0x%x status 0x%x\n",
765 SAS_ADDR(dev->sas_addr),
766 task->task_status.resp,
767 task->task_status.stat));
768 sas_free_task(task);
769 task = NULL;
772 ex_err:
773 BUG_ON(retry == 3 && task != NULL);
774 sas_free_task(task);
775 return res;
778 static int
779 pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
780 struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
781 u32 task_tag)
783 int res, retry;
784 u32 ccb_tag;
785 struct pm8001_ccb_info *ccb;
786 struct sas_task *task = NULL;
788 for (retry = 0; retry < 3; retry++) {
789 task = sas_alloc_task(GFP_KERNEL);
790 if (!task)
791 return -ENOMEM;
793 task->dev = dev;
794 task->task_proto = dev->tproto;
795 task->task_done = pm8001_task_done;
796 task->timer.data = (unsigned long)task;
797 task->timer.function = pm8001_tmf_timedout;
798 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
799 add_timer(&task->timer);
801 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
802 if (res)
803 return res;
804 ccb = &pm8001_ha->ccb_info[ccb_tag];
805 ccb->device = pm8001_dev;
806 ccb->ccb_tag = ccb_tag;
807 ccb->task = task;
809 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
810 pm8001_dev, flag, task_tag, ccb_tag);
812 if (res) {
813 del_timer(&task->timer);
814 PM8001_FAIL_DBG(pm8001_ha,
815 pm8001_printk("Executing internal task "
816 "failed\n"));
817 goto ex_err;
819 wait_for_completion(&task->completion);
820 res = TMF_RESP_FUNC_FAILED;
821 /* Even TMF timed out, return direct. */
822 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
823 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
824 PM8001_FAIL_DBG(pm8001_ha,
825 pm8001_printk("TMF task timeout.\n"));
826 goto ex_err;
830 if (task->task_status.resp == SAS_TASK_COMPLETE &&
831 task->task_status.stat == SAM_STAT_GOOD) {
832 res = TMF_RESP_FUNC_COMPLETE;
833 break;
835 } else {
836 PM8001_EH_DBG(pm8001_ha,
837 pm8001_printk(" Task to dev %016llx response: "
838 "0x%x status 0x%x\n",
839 SAS_ADDR(dev->sas_addr),
840 task->task_status.resp,
841 task->task_status.stat));
842 sas_free_task(task);
843 task = NULL;
846 ex_err:
847 BUG_ON(retry == 3 && task != NULL);
848 sas_free_task(task);
849 return res;
853 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
854 * @dev: the device structure which sas layer used.
856 static void pm8001_dev_gone_notify(struct domain_device *dev)
858 unsigned long flags = 0;
859 u32 tag;
860 struct pm8001_hba_info *pm8001_ha;
861 struct pm8001_device *pm8001_dev = dev->lldd_dev;
863 pm8001_ha = pm8001_find_ha_by_dev(dev);
864 spin_lock_irqsave(&pm8001_ha->lock, flags);
865 pm8001_tag_alloc(pm8001_ha, &tag);
866 if (pm8001_dev) {
867 u32 device_id = pm8001_dev->device_id;
869 PM8001_DISC_DBG(pm8001_ha,
870 pm8001_printk("found dev[%d:%x] is gone.\n",
871 pm8001_dev->device_id, pm8001_dev->dev_type));
872 if (pm8001_dev->running_req) {
873 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
874 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
875 dev, 1, 0);
876 spin_lock_irqsave(&pm8001_ha->lock, flags);
878 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
879 pm8001_free_dev(pm8001_dev);
880 } else {
881 PM8001_DISC_DBG(pm8001_ha,
882 pm8001_printk("Found dev has gone.\n"));
884 dev->lldd_dev = NULL;
885 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
888 void pm8001_dev_gone(struct domain_device *dev)
890 pm8001_dev_gone_notify(dev);
893 static int pm8001_issue_ssp_tmf(struct domain_device *dev,
894 u8 *lun, struct pm8001_tmf_task *tmf)
896 struct sas_ssp_task ssp_task;
897 if (!(dev->tproto & SAS_PROTOCOL_SSP))
898 return TMF_RESP_FUNC_ESUPP;
900 strncpy((u8 *)&ssp_task.LUN, lun, 8);
901 return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
902 tmf);
906 * Standard mandates link reset for ATA (type 0) and hard reset for
907 * SSP (type 1) , only for RECOVERY
909 int pm8001_I_T_nexus_reset(struct domain_device *dev)
911 int rc = TMF_RESP_FUNC_FAILED;
912 struct pm8001_device *pm8001_dev;
913 struct pm8001_hba_info *pm8001_ha;
914 struct sas_phy *phy;
915 if (!dev || !dev->lldd_dev)
916 return -1;
918 pm8001_dev = dev->lldd_dev;
919 pm8001_ha = pm8001_find_ha_by_dev(dev);
920 phy = sas_find_local_phy(dev);
922 if (dev_is_sata(dev)) {
923 DECLARE_COMPLETION_ONSTACK(completion_setstate);
924 if (scsi_is_sas_phy_local(phy))
925 return 0;
926 rc = sas_phy_reset(phy, 1);
927 msleep(2000);
928 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
929 dev, 1, 0);
930 pm8001_dev->setds_completion = &completion_setstate;
931 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
932 pm8001_dev, 0x01);
933 wait_for_completion(&completion_setstate);
934 } else{
935 rc = sas_phy_reset(phy, 1);
936 msleep(2000);
938 PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
939 pm8001_dev->device_id, rc));
940 return rc;
943 /* mandatory SAM-3, the task reset the specified LUN*/
944 int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
946 int rc = TMF_RESP_FUNC_FAILED;
947 struct pm8001_tmf_task tmf_task;
948 struct pm8001_device *pm8001_dev = dev->lldd_dev;
949 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
950 if (dev_is_sata(dev)) {
951 struct sas_phy *phy = sas_find_local_phy(dev);
952 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
953 dev, 1, 0);
954 rc = sas_phy_reset(phy, 1);
955 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
956 pm8001_dev, 0x01);
957 msleep(2000);
958 } else {
959 tmf_task.tmf = TMF_LU_RESET;
960 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
962 /* If failed, fall-through I_T_Nexus reset */
963 PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
964 pm8001_dev->device_id, rc));
965 return rc;
968 /* optional SAM-3 */
969 int pm8001_query_task(struct sas_task *task)
971 u32 tag = 0xdeadbeef;
972 int i = 0;
973 struct scsi_lun lun;
974 struct pm8001_tmf_task tmf_task;
975 int rc = TMF_RESP_FUNC_FAILED;
976 if (unlikely(!task || !task->lldd_task || !task->dev))
977 return rc;
979 if (task->task_proto & SAS_PROTOCOL_SSP) {
980 struct scsi_cmnd *cmnd = task->uldd_task;
981 struct domain_device *dev = task->dev;
982 struct pm8001_hba_info *pm8001_ha =
983 pm8001_find_ha_by_dev(dev);
985 int_to_scsilun(cmnd->device->lun, &lun);
986 rc = pm8001_find_tag(task, &tag);
987 if (rc == 0) {
988 rc = TMF_RESP_FUNC_FAILED;
989 return rc;
991 PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
992 for (i = 0; i < 16; i++)
993 printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
994 printk(KERN_INFO "]\n");
995 tmf_task.tmf = TMF_QUERY_TASK;
996 tmf_task.tag_of_task_to_be_managed = tag;
998 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
999 switch (rc) {
1000 /* The task is still in Lun, release it then */
1001 case TMF_RESP_FUNC_SUCC:
1002 PM8001_EH_DBG(pm8001_ha,
1003 pm8001_printk("The task is still in Lun \n"));
1004 /* The task is not in Lun or failed, reset the phy */
1005 case TMF_RESP_FUNC_FAILED:
1006 case TMF_RESP_FUNC_COMPLETE:
1007 PM8001_EH_DBG(pm8001_ha,
1008 pm8001_printk("The task is not in Lun or failed,"
1009 " reset the phy \n"));
1010 break;
1013 pm8001_printk(":rc= %d\n", rc);
1014 return rc;
1017 /* mandatory SAM-3, still need free task/ccb info, abord the specified task */
1018 int pm8001_abort_task(struct sas_task *task)
1020 unsigned long flags;
1021 u32 tag = 0xdeadbeef;
1022 u32 device_id;
1023 struct domain_device *dev ;
1024 struct pm8001_hba_info *pm8001_ha = NULL;
1025 struct pm8001_ccb_info *ccb;
1026 struct scsi_lun lun;
1027 struct pm8001_device *pm8001_dev;
1028 struct pm8001_tmf_task tmf_task;
1029 int rc = TMF_RESP_FUNC_FAILED;
1030 if (unlikely(!task || !task->lldd_task || !task->dev))
1031 return rc;
1032 spin_lock_irqsave(&task->task_state_lock, flags);
1033 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1034 spin_unlock_irqrestore(&task->task_state_lock, flags);
1035 rc = TMF_RESP_FUNC_COMPLETE;
1036 goto out;
1038 spin_unlock_irqrestore(&task->task_state_lock, flags);
1039 if (task->task_proto & SAS_PROTOCOL_SSP) {
1040 struct scsi_cmnd *cmnd = task->uldd_task;
1041 dev = task->dev;
1042 ccb = task->lldd_task;
1043 pm8001_dev = dev->lldd_dev;
1044 pm8001_ha = pm8001_find_ha_by_dev(dev);
1045 int_to_scsilun(cmnd->device->lun, &lun);
1046 rc = pm8001_find_tag(task, &tag);
1047 if (rc == 0) {
1048 printk(KERN_INFO "No such tag in %s\n", __func__);
1049 rc = TMF_RESP_FUNC_FAILED;
1050 return rc;
1052 device_id = pm8001_dev->device_id;
1053 PM8001_EH_DBG(pm8001_ha,
1054 pm8001_printk("abort io to deviceid= %d\n", device_id));
1055 tmf_task.tmf = TMF_ABORT_TASK;
1056 tmf_task.tag_of_task_to_be_managed = tag;
1057 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1058 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1059 pm8001_dev->sas_device, 0, tag);
1060 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1061 task->task_proto & SAS_PROTOCOL_STP) {
1062 dev = task->dev;
1063 pm8001_dev = dev->lldd_dev;
1064 pm8001_ha = pm8001_find_ha_by_dev(dev);
1065 rc = pm8001_find_tag(task, &tag);
1066 if (rc == 0) {
1067 printk(KERN_INFO "No such tag in %s\n", __func__);
1068 rc = TMF_RESP_FUNC_FAILED;
1069 return rc;
1071 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1072 pm8001_dev->sas_device, 0, tag);
1073 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1074 /* SMP */
1075 dev = task->dev;
1076 pm8001_dev = dev->lldd_dev;
1077 pm8001_ha = pm8001_find_ha_by_dev(dev);
1078 rc = pm8001_find_tag(task, &tag);
1079 if (rc == 0) {
1080 printk(KERN_INFO "No such tag in %s\n", __func__);
1081 rc = TMF_RESP_FUNC_FAILED;
1082 return rc;
1084 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1085 pm8001_dev->sas_device, 0, tag);
1088 out:
1089 if (rc != TMF_RESP_FUNC_COMPLETE)
1090 pm8001_printk("rc= %d\n", rc);
1091 return rc;
1094 int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1096 int rc = TMF_RESP_FUNC_FAILED;
1097 struct pm8001_tmf_task tmf_task;
1099 tmf_task.tmf = TMF_ABORT_TASK_SET;
1100 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1101 return rc;
1104 int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1106 int rc = TMF_RESP_FUNC_FAILED;
1107 struct pm8001_tmf_task tmf_task;
1109 tmf_task.tmf = TMF_CLEAR_ACA;
1110 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1112 return rc;
1115 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1117 int rc = TMF_RESP_FUNC_FAILED;
1118 struct pm8001_tmf_task tmf_task;
1119 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1120 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1122 PM8001_EH_DBG(pm8001_ha,
1123 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1124 pm8001_dev->device_id));
1125 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1126 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1127 return rc;