drm/tilcdc: Fix an incorrect condition
[linux-2.6/btrfs-unstable.git] / drivers / scsi / pm8001 / pm8001_sas.c
blobb961112395d5e053145ba6f83ed4bf7e5058c5c5
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 unsigned long flags;
170 pm8001_ha = sas_phy->ha->lldd_ha;
171 pm8001_ha->phy[phy_id].enable_completion = &completion;
172 switch (func) {
173 case PHY_FUNC_SET_LINK_RATE:
174 rates = funcdata;
175 if (rates->minimum_linkrate) {
176 pm8001_ha->phy[phy_id].minimum_linkrate =
177 rates->minimum_linkrate;
179 if (rates->maximum_linkrate) {
180 pm8001_ha->phy[phy_id].maximum_linkrate =
181 rates->maximum_linkrate;
183 if (pm8001_ha->phy[phy_id].phy_state == 0) {
184 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
185 wait_for_completion(&completion);
187 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
188 PHY_LINK_RESET);
189 break;
190 case PHY_FUNC_HARD_RESET:
191 if (pm8001_ha->phy[phy_id].phy_state == 0) {
192 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
193 wait_for_completion(&completion);
195 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
196 PHY_HARD_RESET);
197 break;
198 case PHY_FUNC_LINK_RESET:
199 if (pm8001_ha->phy[phy_id].phy_state == 0) {
200 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id);
201 wait_for_completion(&completion);
203 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
204 PHY_LINK_RESET);
205 break;
206 case PHY_FUNC_RELEASE_SPINUP_HOLD:
207 PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id,
208 PHY_LINK_RESET);
209 break;
210 case PHY_FUNC_DISABLE:
211 PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id);
212 break;
213 case PHY_FUNC_GET_EVENTS:
214 spin_lock_irqsave(&pm8001_ha->lock, flags);
215 if (-1 == pm8001_bar4_shift(pm8001_ha,
216 (phy_id < 4) ? 0x30000 : 0x40000)) {
217 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
218 return -EINVAL;
221 struct sas_phy *phy = sas_phy->phy;
222 uint32_t *qp = (uint32_t *)(((char *)
223 pm8001_ha->io_mem[2].memvirtaddr)
224 + 0x1034 + (0x4000 * (phy_id & 3)));
226 phy->invalid_dword_count = qp[0];
227 phy->running_disparity_error_count = qp[1];
228 phy->loss_of_dword_sync_count = qp[3];
229 phy->phy_reset_problem_count = qp[4];
231 pm8001_bar4_shift(pm8001_ha, 0);
232 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
233 return 0;
234 default:
235 rc = -EOPNOTSUPP;
237 msleep(300);
238 return rc;
242 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
243 * command to HBA.
244 * @shost: the scsi host data.
246 void pm8001_scan_start(struct Scsi_Host *shost)
248 int i;
249 struct pm8001_hba_info *pm8001_ha;
250 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
251 pm8001_ha = sha->lldd_ha;
252 PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
253 for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
254 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
257 int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
259 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
261 /* give the phy enabling interrupt event time to come in (1s
262 * is empirically about all it takes) */
263 if (time < HZ)
264 return 0;
265 /* Wait for discovery to finish */
266 sas_drain_work(ha);
267 return 1;
271 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
272 * @pm8001_ha: our hba card information
273 * @ccb: the ccb which attached to smp task
275 static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
276 struct pm8001_ccb_info *ccb)
278 return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
281 u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
283 struct ata_queued_cmd *qc = task->uldd_task;
284 if (qc) {
285 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
286 qc->tf.command == ATA_CMD_FPDMA_READ) {
287 *tag = qc->tag;
288 return 1;
291 return 0;
295 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
296 * @pm8001_ha: our hba card information
297 * @ccb: the ccb which attached to sata task
299 static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
300 struct pm8001_ccb_info *ccb)
302 return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
306 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
307 * @pm8001_ha: our hba card information
308 * @ccb: the ccb which attached to TM
309 * @tmf: the task management IU
311 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
312 struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
314 return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
318 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
319 * @pm8001_ha: our hba card information
320 * @ccb: the ccb which attached to ssp task
322 static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
323 struct pm8001_ccb_info *ccb)
325 return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
328 /* Find the local port id that's attached to this device */
329 static int sas_find_local_port_id(struct domain_device *dev)
331 struct domain_device *pdev = dev->parent;
333 /* Directly attached device */
334 if (!pdev)
335 return dev->port->id;
336 while (pdev) {
337 struct domain_device *pdev_p = pdev->parent;
338 if (!pdev_p)
339 return pdev->port->id;
340 pdev = pdev->parent;
342 return 0;
346 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
347 * @task: the task to be execute.
348 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
349 * we always execute one one time.
350 * @gfp_flags: gfp_flags.
351 * @is_tmf: if it is task management task.
352 * @tmf: the task management IU
354 #define DEV_IS_GONE(pm8001_dev) \
355 ((!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE)))
356 static int pm8001_task_exec(struct sas_task *task, const int num,
357 gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
359 struct domain_device *dev = task->dev;
360 struct pm8001_hba_info *pm8001_ha;
361 struct pm8001_device *pm8001_dev;
362 struct pm8001_port *port = NULL;
363 struct sas_task *t = task;
364 struct pm8001_ccb_info *ccb;
365 u32 tag = 0xdeadbeef, rc, n_elem = 0;
366 u32 n = num;
367 unsigned long flags = 0;
369 if (!dev->port) {
370 struct task_status_struct *tsm = &t->task_status;
371 tsm->resp = SAS_TASK_UNDELIVERED;
372 tsm->stat = SAS_PHY_DOWN;
373 if (dev->dev_type != SATA_DEV)
374 t->task_done(t);
375 return 0;
377 pm8001_ha = pm8001_find_ha_by_dev(task->dev);
378 PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n "));
379 spin_lock_irqsave(&pm8001_ha->lock, flags);
380 do {
381 dev = t->dev;
382 pm8001_dev = dev->lldd_dev;
383 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
384 if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
385 if (sas_protocol_ata(t->task_proto)) {
386 struct task_status_struct *ts = &t->task_status;
387 ts->resp = SAS_TASK_UNDELIVERED;
388 ts->stat = SAS_PHY_DOWN;
390 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
391 t->task_done(t);
392 spin_lock_irqsave(&pm8001_ha->lock, flags);
393 if (n > 1)
394 t = list_entry(t->list.next,
395 struct sas_task, list);
396 continue;
397 } else {
398 struct task_status_struct *ts = &t->task_status;
399 ts->resp = SAS_TASK_UNDELIVERED;
400 ts->stat = SAS_PHY_DOWN;
401 t->task_done(t);
402 if (n > 1)
403 t = list_entry(t->list.next,
404 struct sas_task, list);
405 continue;
408 rc = pm8001_tag_alloc(pm8001_ha, &tag);
409 if (rc)
410 goto err_out;
411 ccb = &pm8001_ha->ccb_info[tag];
413 if (!sas_protocol_ata(t->task_proto)) {
414 if (t->num_scatter) {
415 n_elem = dma_map_sg(pm8001_ha->dev,
416 t->scatter,
417 t->num_scatter,
418 t->data_dir);
419 if (!n_elem) {
420 rc = -ENOMEM;
421 goto err_out_tag;
424 } else {
425 n_elem = t->num_scatter;
428 t->lldd_task = ccb;
429 ccb->n_elem = n_elem;
430 ccb->ccb_tag = tag;
431 ccb->task = t;
432 switch (t->task_proto) {
433 case SAS_PROTOCOL_SMP:
434 rc = pm8001_task_prep_smp(pm8001_ha, ccb);
435 break;
436 case SAS_PROTOCOL_SSP:
437 if (is_tmf)
438 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
439 ccb, tmf);
440 else
441 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
442 break;
443 case SAS_PROTOCOL_SATA:
444 case SAS_PROTOCOL_STP:
445 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
446 rc = pm8001_task_prep_ata(pm8001_ha, ccb);
447 break;
448 default:
449 dev_printk(KERN_ERR, pm8001_ha->dev,
450 "unknown sas_task proto: 0x%x\n",
451 t->task_proto);
452 rc = -EINVAL;
453 break;
456 if (rc) {
457 PM8001_IO_DBG(pm8001_ha,
458 pm8001_printk("rc is %x\n", rc));
459 goto err_out_tag;
461 /* TODO: select normal or high priority */
462 spin_lock(&t->task_state_lock);
463 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
464 spin_unlock(&t->task_state_lock);
465 pm8001_dev->running_req++;
466 if (n > 1)
467 t = list_entry(t->list.next, struct sas_task, list);
468 } while (--n);
469 rc = 0;
470 goto out_done;
472 err_out_tag:
473 pm8001_tag_free(pm8001_ha, tag);
474 err_out:
475 dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
476 if (!sas_protocol_ata(t->task_proto))
477 if (n_elem)
478 dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem,
479 t->data_dir);
480 out_done:
481 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
482 return rc;
486 * pm8001_queue_command - register for upper layer used, all IO commands sent
487 * to HBA are from this interface.
488 * @task: the task to be execute.
489 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
490 * we always execute one one time
491 * @gfp_flags: gfp_flags
493 int pm8001_queue_command(struct sas_task *task, const int num,
494 gfp_t gfp_flags)
496 return pm8001_task_exec(task, num, gfp_flags, 0, NULL);
499 void pm8001_ccb_free(struct pm8001_hba_info *pm8001_ha, u32 ccb_idx)
501 pm8001_tag_clear(pm8001_ha, ccb_idx);
505 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
506 * @pm8001_ha: our hba card information
507 * @ccb: the ccb which attached to ssp task
508 * @task: the task to be free.
509 * @ccb_idx: ccb index.
511 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
512 struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
514 if (!ccb->task)
515 return;
516 if (!sas_protocol_ata(task->task_proto))
517 if (ccb->n_elem)
518 dma_unmap_sg(pm8001_ha->dev, task->scatter,
519 task->num_scatter, task->data_dir);
521 switch (task->task_proto) {
522 case SAS_PROTOCOL_SMP:
523 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
524 PCI_DMA_FROMDEVICE);
525 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
526 PCI_DMA_TODEVICE);
527 break;
529 case SAS_PROTOCOL_SATA:
530 case SAS_PROTOCOL_STP:
531 case SAS_PROTOCOL_SSP:
532 default:
533 /* do nothing */
534 break;
536 task->lldd_task = NULL;
537 ccb->task = NULL;
538 ccb->ccb_tag = 0xFFFFFFFF;
539 ccb->open_retry = 0;
540 pm8001_ccb_free(pm8001_ha, ccb_idx);
544 * pm8001_alloc_dev - find a empty pm8001_device
545 * @pm8001_ha: our hba card information
547 struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
549 u32 dev;
550 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
551 if (pm8001_ha->devices[dev].dev_type == NO_DEVICE) {
552 pm8001_ha->devices[dev].id = dev;
553 return &pm8001_ha->devices[dev];
556 if (dev == PM8001_MAX_DEVICES) {
557 PM8001_FAIL_DBG(pm8001_ha,
558 pm8001_printk("max support %d devices, ignore ..\n",
559 PM8001_MAX_DEVICES));
561 return NULL;
564 static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
566 u32 id = pm8001_dev->id;
567 memset(pm8001_dev, 0, sizeof(*pm8001_dev));
568 pm8001_dev->id = id;
569 pm8001_dev->dev_type = NO_DEVICE;
570 pm8001_dev->device_id = PM8001_MAX_DEVICES;
571 pm8001_dev->sas_device = NULL;
575 * pm8001_dev_found_notify - libsas notify a device is found.
576 * @dev: the device structure which sas layer used.
578 * when libsas find a sas domain device, it should tell the LLDD that
579 * device is found, and then LLDD register this device to HBA firmware
580 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
581 * device ID(according to device's sas address) and returned it to LLDD. From
582 * now on, we communicate with HBA FW with the device ID which HBA assigned
583 * rather than sas address. it is the necessary step for our HBA but it is
584 * the optional for other HBA driver.
586 static int pm8001_dev_found_notify(struct domain_device *dev)
588 unsigned long flags = 0;
589 int res = 0;
590 struct pm8001_hba_info *pm8001_ha = NULL;
591 struct domain_device *parent_dev = dev->parent;
592 struct pm8001_device *pm8001_device;
593 DECLARE_COMPLETION_ONSTACK(completion);
594 u32 flag = 0;
595 pm8001_ha = pm8001_find_ha_by_dev(dev);
596 spin_lock_irqsave(&pm8001_ha->lock, flags);
598 pm8001_device = pm8001_alloc_dev(pm8001_ha);
599 if (!pm8001_device) {
600 res = -1;
601 goto found_out;
603 pm8001_device->sas_device = dev;
604 dev->lldd_dev = pm8001_device;
605 pm8001_device->dev_type = dev->dev_type;
606 pm8001_device->dcompletion = &completion;
607 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
608 int phy_id;
609 struct ex_phy *phy;
610 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
611 phy_id++) {
612 phy = &parent_dev->ex_dev.ex_phy[phy_id];
613 if (SAS_ADDR(phy->attached_sas_addr)
614 == SAS_ADDR(dev->sas_addr)) {
615 pm8001_device->attached_phy = phy_id;
616 break;
619 if (phy_id == parent_dev->ex_dev.num_phys) {
620 PM8001_FAIL_DBG(pm8001_ha,
621 pm8001_printk("Error: no attached dev:%016llx"
622 " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
623 SAS_ADDR(parent_dev->sas_addr)));
624 res = -1;
626 } else {
627 if (dev->dev_type == SATA_DEV) {
628 pm8001_device->attached_phy =
629 dev->rphy->identify.phy_identifier;
630 flag = 1; /* directly sata*/
632 } /*register this device to HBA*/
633 PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device\n"));
634 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
635 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
636 wait_for_completion(&completion);
637 if (dev->dev_type == SAS_END_DEV)
638 msleep(50);
639 pm8001_ha->flags = PM8001F_RUN_TIME;
640 return 0;
641 found_out:
642 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
643 return res;
646 int pm8001_dev_found(struct domain_device *dev)
648 return pm8001_dev_found_notify(dev);
651 static void pm8001_task_done(struct sas_task *task)
653 if (!del_timer(&task->slow_task->timer))
654 return;
655 complete(&task->slow_task->completion);
658 static void pm8001_tmf_timedout(unsigned long data)
660 struct sas_task *task = (struct sas_task *)data;
662 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
663 complete(&task->slow_task->completion);
666 #define PM8001_TASK_TIMEOUT 20
668 * pm8001_exec_internal_tmf_task - execute some task management commands.
669 * @dev: the wanted device.
670 * @tmf: which task management wanted to be take.
671 * @para_len: para_len.
672 * @parameter: ssp task parameter.
674 * when errors or exception happened, we may want to do something, for example
675 * abort the issued task which result in this execption, it is done by calling
676 * this function, note it is also with the task execute interface.
678 static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
679 void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
681 int res, retry;
682 struct sas_task *task = NULL;
683 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
685 for (retry = 0; retry < 3; retry++) {
686 task = sas_alloc_slow_task(GFP_KERNEL);
687 if (!task)
688 return -ENOMEM;
690 task->dev = dev;
691 task->task_proto = dev->tproto;
692 memcpy(&task->ssp_task, parameter, para_len);
693 task->task_done = pm8001_task_done;
694 task->slow_task->timer.data = (unsigned long)task;
695 task->slow_task->timer.function = pm8001_tmf_timedout;
696 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
697 add_timer(&task->slow_task->timer);
699 res = pm8001_task_exec(task, 1, GFP_KERNEL, 1, tmf);
701 if (res) {
702 del_timer(&task->slow_task->timer);
703 PM8001_FAIL_DBG(pm8001_ha,
704 pm8001_printk("Executing internal task "
705 "failed\n"));
706 goto ex_err;
708 wait_for_completion(&task->slow_task->completion);
709 res = -TMF_RESP_FUNC_FAILED;
710 /* Even TMF timed out, return direct. */
711 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
712 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
713 PM8001_FAIL_DBG(pm8001_ha,
714 pm8001_printk("TMF task[%x]timeout.\n",
715 tmf->tmf));
716 goto ex_err;
720 if (task->task_status.resp == SAS_TASK_COMPLETE &&
721 task->task_status.stat == SAM_STAT_GOOD) {
722 res = TMF_RESP_FUNC_COMPLETE;
723 break;
726 if (task->task_status.resp == SAS_TASK_COMPLETE &&
727 task->task_status.stat == SAS_DATA_UNDERRUN) {
728 /* no error, but return the number of bytes of
729 * underrun */
730 res = task->task_status.residual;
731 break;
734 if (task->task_status.resp == SAS_TASK_COMPLETE &&
735 task->task_status.stat == SAS_DATA_OVERRUN) {
736 PM8001_FAIL_DBG(pm8001_ha,
737 pm8001_printk("Blocked task error.\n"));
738 res = -EMSGSIZE;
739 break;
740 } else {
741 PM8001_EH_DBG(pm8001_ha,
742 pm8001_printk(" Task to dev %016llx response:"
743 "0x%x status 0x%x\n",
744 SAS_ADDR(dev->sas_addr),
745 task->task_status.resp,
746 task->task_status.stat));
747 sas_free_task(task);
748 task = NULL;
751 ex_err:
752 BUG_ON(retry == 3 && task != NULL);
753 sas_free_task(task);
754 return res;
757 static int
758 pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
759 struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
760 u32 task_tag)
762 int res, retry;
763 u32 ccb_tag;
764 struct pm8001_ccb_info *ccb;
765 struct sas_task *task = NULL;
767 for (retry = 0; retry < 3; retry++) {
768 task = sas_alloc_slow_task(GFP_KERNEL);
769 if (!task)
770 return -ENOMEM;
772 task->dev = dev;
773 task->task_proto = dev->tproto;
774 task->task_done = pm8001_task_done;
775 task->slow_task->timer.data = (unsigned long)task;
776 task->slow_task->timer.function = pm8001_tmf_timedout;
777 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
778 add_timer(&task->slow_task->timer);
780 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
781 if (res)
782 return res;
783 ccb = &pm8001_ha->ccb_info[ccb_tag];
784 ccb->device = pm8001_dev;
785 ccb->ccb_tag = ccb_tag;
786 ccb->task = task;
788 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
789 pm8001_dev, flag, task_tag, ccb_tag);
791 if (res) {
792 del_timer(&task->slow_task->timer);
793 PM8001_FAIL_DBG(pm8001_ha,
794 pm8001_printk("Executing internal task "
795 "failed\n"));
796 goto ex_err;
798 wait_for_completion(&task->slow_task->completion);
799 res = TMF_RESP_FUNC_FAILED;
800 /* Even TMF timed out, return direct. */
801 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
802 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
803 PM8001_FAIL_DBG(pm8001_ha,
804 pm8001_printk("TMF task timeout.\n"));
805 goto ex_err;
809 if (task->task_status.resp == SAS_TASK_COMPLETE &&
810 task->task_status.stat == SAM_STAT_GOOD) {
811 res = TMF_RESP_FUNC_COMPLETE;
812 break;
814 } else {
815 PM8001_EH_DBG(pm8001_ha,
816 pm8001_printk(" Task to dev %016llx response: "
817 "0x%x status 0x%x\n",
818 SAS_ADDR(dev->sas_addr),
819 task->task_status.resp,
820 task->task_status.stat));
821 sas_free_task(task);
822 task = NULL;
825 ex_err:
826 BUG_ON(retry == 3 && task != NULL);
827 sas_free_task(task);
828 return res;
832 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
833 * @dev: the device structure which sas layer used.
835 static void pm8001_dev_gone_notify(struct domain_device *dev)
837 unsigned long flags = 0;
838 u32 tag;
839 struct pm8001_hba_info *pm8001_ha;
840 struct pm8001_device *pm8001_dev = dev->lldd_dev;
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 u32 device_id = pm8001_dev->device_id;
848 PM8001_DISC_DBG(pm8001_ha,
849 pm8001_printk("found dev[%d:%x] is gone.\n",
850 pm8001_dev->device_id, pm8001_dev->dev_type));
851 if (pm8001_dev->running_req) {
852 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
853 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
854 dev, 1, 0);
855 spin_lock_irqsave(&pm8001_ha->lock, flags);
857 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
858 pm8001_free_dev(pm8001_dev);
859 } else {
860 PM8001_DISC_DBG(pm8001_ha,
861 pm8001_printk("Found dev has gone.\n"));
863 dev->lldd_dev = NULL;
864 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
867 void pm8001_dev_gone(struct domain_device *dev)
869 pm8001_dev_gone_notify(dev);
872 static int pm8001_issue_ssp_tmf(struct domain_device *dev,
873 u8 *lun, struct pm8001_tmf_task *tmf)
875 struct sas_ssp_task ssp_task;
876 if (!(dev->tproto & SAS_PROTOCOL_SSP))
877 return TMF_RESP_FUNC_ESUPP;
879 strncpy((u8 *)&ssp_task.LUN, lun, 8);
880 return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
881 tmf);
884 /* retry commands by ha, by task and/or by device */
885 void pm8001_open_reject_retry(
886 struct pm8001_hba_info *pm8001_ha,
887 struct sas_task *task_to_close,
888 struct pm8001_device *device_to_close)
890 int i;
891 unsigned long flags;
893 if (pm8001_ha == NULL)
894 return;
896 spin_lock_irqsave(&pm8001_ha->lock, flags);
898 for (i = 0; i < PM8001_MAX_CCB; i++) {
899 struct sas_task *task;
900 struct task_status_struct *ts;
901 struct pm8001_device *pm8001_dev;
902 unsigned long flags1;
903 u32 tag;
904 struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
906 pm8001_dev = ccb->device;
907 if (!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE))
908 continue;
909 if (!device_to_close) {
910 uintptr_t d = (uintptr_t)pm8001_dev
911 - (uintptr_t)&pm8001_ha->devices;
912 if (((d % sizeof(*pm8001_dev)) != 0)
913 || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
914 continue;
915 } else if (pm8001_dev != device_to_close)
916 continue;
917 tag = ccb->ccb_tag;
918 if (!tag || (tag == 0xFFFFFFFF))
919 continue;
920 task = ccb->task;
921 if (!task || !task->task_done)
922 continue;
923 if (task_to_close && (task != task_to_close))
924 continue;
925 ts = &task->task_status;
926 ts->resp = SAS_TASK_COMPLETE;
927 /* Force the midlayer to retry */
928 ts->stat = SAS_OPEN_REJECT;
929 ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
930 if (pm8001_dev)
931 pm8001_dev->running_req--;
932 spin_lock_irqsave(&task->task_state_lock, flags1);
933 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
934 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
935 task->task_state_flags |= SAS_TASK_STATE_DONE;
936 if (unlikely((task->task_state_flags
937 & SAS_TASK_STATE_ABORTED))) {
938 spin_unlock_irqrestore(&task->task_state_lock,
939 flags1);
940 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
941 } else {
942 spin_unlock_irqrestore(&task->task_state_lock,
943 flags1);
944 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
945 mb();/* in order to force CPU ordering */
946 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
947 task->task_done(task);
948 spin_lock_irqsave(&pm8001_ha->lock, flags);
952 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
956 * Standard mandates link reset for ATA (type 0) and hard reset for
957 * SSP (type 1) , only for RECOVERY
959 int pm8001_I_T_nexus_reset(struct domain_device *dev)
961 int rc = TMF_RESP_FUNC_FAILED;
962 struct pm8001_device *pm8001_dev;
963 struct pm8001_hba_info *pm8001_ha;
964 struct sas_phy *phy;
966 if (!dev || !dev->lldd_dev)
967 return -ENODEV;
969 pm8001_dev = dev->lldd_dev;
970 pm8001_ha = pm8001_find_ha_by_dev(dev);
971 phy = sas_get_local_phy(dev);
973 if (dev_is_sata(dev)) {
974 DECLARE_COMPLETION_ONSTACK(completion_setstate);
975 if (scsi_is_sas_phy_local(phy)) {
976 rc = 0;
977 goto out;
979 rc = sas_phy_reset(phy, 1);
980 msleep(2000);
981 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
982 dev, 1, 0);
983 pm8001_dev->setds_completion = &completion_setstate;
984 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
985 pm8001_dev, 0x01);
986 wait_for_completion(&completion_setstate);
987 } else {
988 rc = sas_phy_reset(phy, 1);
989 msleep(2000);
991 PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
992 pm8001_dev->device_id, rc));
993 out:
994 sas_put_local_phy(phy);
995 return rc;
998 /* mandatory SAM-3, the task reset the specified LUN*/
999 int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
1001 int rc = TMF_RESP_FUNC_FAILED;
1002 struct pm8001_tmf_task tmf_task;
1003 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1004 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1005 if (dev_is_sata(dev)) {
1006 struct sas_phy *phy = sas_get_local_phy(dev);
1007 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1008 dev, 1, 0);
1009 rc = sas_phy_reset(phy, 1);
1010 sas_put_local_phy(phy);
1011 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1012 pm8001_dev, 0x01);
1013 msleep(2000);
1014 } else {
1015 tmf_task.tmf = TMF_LU_RESET;
1016 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1018 /* If failed, fall-through I_T_Nexus reset */
1019 PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
1020 pm8001_dev->device_id, rc));
1021 return rc;
1024 /* optional SAM-3 */
1025 int pm8001_query_task(struct sas_task *task)
1027 u32 tag = 0xdeadbeef;
1028 int i = 0;
1029 struct scsi_lun lun;
1030 struct pm8001_tmf_task tmf_task;
1031 int rc = TMF_RESP_FUNC_FAILED;
1032 if (unlikely(!task || !task->lldd_task || !task->dev))
1033 return rc;
1035 if (task->task_proto & SAS_PROTOCOL_SSP) {
1036 struct scsi_cmnd *cmnd = task->uldd_task;
1037 struct domain_device *dev = task->dev;
1038 struct pm8001_hba_info *pm8001_ha =
1039 pm8001_find_ha_by_dev(dev);
1041 int_to_scsilun(cmnd->device->lun, &lun);
1042 rc = pm8001_find_tag(task, &tag);
1043 if (rc == 0) {
1044 rc = TMF_RESP_FUNC_FAILED;
1045 return rc;
1047 PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
1048 for (i = 0; i < 16; i++)
1049 printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
1050 printk(KERN_INFO "]\n");
1051 tmf_task.tmf = TMF_QUERY_TASK;
1052 tmf_task.tag_of_task_to_be_managed = tag;
1054 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1055 switch (rc) {
1056 /* The task is still in Lun, release it then */
1057 case TMF_RESP_FUNC_SUCC:
1058 PM8001_EH_DBG(pm8001_ha,
1059 pm8001_printk("The task is still in Lun\n"));
1060 break;
1061 /* The task is not in Lun or failed, reset the phy */
1062 case TMF_RESP_FUNC_FAILED:
1063 case TMF_RESP_FUNC_COMPLETE:
1064 PM8001_EH_DBG(pm8001_ha,
1065 pm8001_printk("The task is not in Lun or failed,"
1066 " reset the phy\n"));
1067 break;
1070 pm8001_printk(":rc= %d\n", rc);
1071 return rc;
1074 /* mandatory SAM-3, still need free task/ccb info, abord the specified task */
1075 int pm8001_abort_task(struct sas_task *task)
1077 unsigned long flags;
1078 u32 tag = 0xdeadbeef;
1079 u32 device_id;
1080 struct domain_device *dev ;
1081 struct pm8001_hba_info *pm8001_ha = NULL;
1082 struct pm8001_ccb_info *ccb;
1083 struct scsi_lun lun;
1084 struct pm8001_device *pm8001_dev;
1085 struct pm8001_tmf_task tmf_task;
1086 int rc = TMF_RESP_FUNC_FAILED;
1087 if (unlikely(!task || !task->lldd_task || !task->dev))
1088 return rc;
1089 spin_lock_irqsave(&task->task_state_lock, flags);
1090 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1091 spin_unlock_irqrestore(&task->task_state_lock, flags);
1092 rc = TMF_RESP_FUNC_COMPLETE;
1093 goto out;
1095 spin_unlock_irqrestore(&task->task_state_lock, flags);
1096 if (task->task_proto & SAS_PROTOCOL_SSP) {
1097 struct scsi_cmnd *cmnd = task->uldd_task;
1098 dev = task->dev;
1099 ccb = task->lldd_task;
1100 pm8001_dev = dev->lldd_dev;
1101 pm8001_ha = pm8001_find_ha_by_dev(dev);
1102 int_to_scsilun(cmnd->device->lun, &lun);
1103 rc = pm8001_find_tag(task, &tag);
1104 if (rc == 0) {
1105 printk(KERN_INFO "No such tag in %s\n", __func__);
1106 rc = TMF_RESP_FUNC_FAILED;
1107 return rc;
1109 device_id = pm8001_dev->device_id;
1110 PM8001_EH_DBG(pm8001_ha,
1111 pm8001_printk("abort io to deviceid= %d\n", device_id));
1112 tmf_task.tmf = TMF_ABORT_TASK;
1113 tmf_task.tag_of_task_to_be_managed = tag;
1114 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1115 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1116 pm8001_dev->sas_device, 0, tag);
1117 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1118 task->task_proto & SAS_PROTOCOL_STP) {
1119 dev = task->dev;
1120 pm8001_dev = dev->lldd_dev;
1121 pm8001_ha = pm8001_find_ha_by_dev(dev);
1122 rc = pm8001_find_tag(task, &tag);
1123 if (rc == 0) {
1124 printk(KERN_INFO "No such tag in %s\n", __func__);
1125 rc = TMF_RESP_FUNC_FAILED;
1126 return rc;
1128 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1129 pm8001_dev->sas_device, 0, tag);
1130 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1131 /* SMP */
1132 dev = task->dev;
1133 pm8001_dev = dev->lldd_dev;
1134 pm8001_ha = pm8001_find_ha_by_dev(dev);
1135 rc = pm8001_find_tag(task, &tag);
1136 if (rc == 0) {
1137 printk(KERN_INFO "No such tag in %s\n", __func__);
1138 rc = TMF_RESP_FUNC_FAILED;
1139 return rc;
1141 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1142 pm8001_dev->sas_device, 0, tag);
1145 out:
1146 if (rc != TMF_RESP_FUNC_COMPLETE)
1147 pm8001_printk("rc= %d\n", rc);
1148 return rc;
1151 int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1153 int rc = TMF_RESP_FUNC_FAILED;
1154 struct pm8001_tmf_task tmf_task;
1156 tmf_task.tmf = TMF_ABORT_TASK_SET;
1157 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1158 return rc;
1161 int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1163 int rc = TMF_RESP_FUNC_FAILED;
1164 struct pm8001_tmf_task tmf_task;
1166 tmf_task.tmf = TMF_CLEAR_ACA;
1167 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1169 return rc;
1172 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1174 int rc = TMF_RESP_FUNC_FAILED;
1175 struct pm8001_tmf_task tmf_task;
1176 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1177 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1179 PM8001_EH_DBG(pm8001_ha,
1180 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1181 pm8001_dev->device_id));
1182 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1183 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1184 return rc;