Use dentry_path() to create full path to inode object
[pohmelfs.git] / drivers / scsi / pm8001 / pm8001_sas.c
blobfb3dc9978861284e575945331487122a56ddd6c3
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 = -ENOSYS;
215 msleep(300);
216 return rc;
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.
224 void pm8001_scan_start(struct Scsi_Host *shost)
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);
235 int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
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;
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
251 static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
252 struct pm8001_ccb_info *ccb)
254 return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
257 u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
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;
267 return 0;
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
275 static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
276 struct pm8001_ccb_info *ccb)
278 return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
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
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)
290 return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
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
298 static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
299 struct pm8001_ccb_info *ccb)
301 return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
304 /* Find the local port id that's attached to this device */
305 static int sas_find_local_port_id(struct domain_device *dev)
307 struct domain_device *pdev = dev->parent;
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;
318 return 0;
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
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)
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;
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;
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 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
360 if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
361 if (sas_protocol_ata(t->task_proto)) {
362 struct task_status_struct *ts = &t->task_status;
363 ts->resp = SAS_TASK_UNDELIVERED;
364 ts->stat = SAS_PHY_DOWN;
366 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
367 spin_unlock_irqrestore(dev->sata_dev.ap->lock,
368 flags_libsas);
369 t->task_done(t);
370 spin_lock_irqsave(dev->sata_dev.ap->lock,
371 flags_libsas);
372 spin_lock_irqsave(&pm8001_ha->lock, flags);
373 if (n > 1)
374 t = list_entry(t->list.next,
375 struct sas_task, list);
376 continue;
377 } else {
378 struct task_status_struct *ts = &t->task_status;
379 ts->resp = SAS_TASK_UNDELIVERED;
380 ts->stat = SAS_PHY_DOWN;
381 t->task_done(t);
382 if (n > 1)
383 t = list_entry(t->list.next,
384 struct sas_task, list);
385 continue;
388 rc = pm8001_tag_alloc(pm8001_ha, &tag);
389 if (rc)
390 goto err_out;
391 ccb = &pm8001_ha->ccb_info[tag];
393 if (!sas_protocol_ata(t->task_proto)) {
394 if (t->num_scatter) {
395 n_elem = dma_map_sg(pm8001_ha->dev,
396 t->scatter,
397 t->num_scatter,
398 t->data_dir);
399 if (!n_elem) {
400 rc = -ENOMEM;
401 goto err_out_tag;
404 } else {
405 n_elem = t->num_scatter;
408 t->lldd_task = ccb;
409 ccb->n_elem = n_elem;
410 ccb->ccb_tag = tag;
411 ccb->task = t;
412 switch (t->task_proto) {
413 case SAS_PROTOCOL_SMP:
414 rc = pm8001_task_prep_smp(pm8001_ha, ccb);
415 break;
416 case SAS_PROTOCOL_SSP:
417 if (is_tmf)
418 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
419 ccb, tmf);
420 else
421 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
422 break;
423 case SAS_PROTOCOL_SATA:
424 case SAS_PROTOCOL_STP:
425 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
426 rc = pm8001_task_prep_ata(pm8001_ha, ccb);
427 break;
428 default:
429 dev_printk(KERN_ERR, pm8001_ha->dev,
430 "unknown sas_task proto: 0x%x\n",
431 t->task_proto);
432 rc = -EINVAL;
433 break;
436 if (rc) {
437 PM8001_IO_DBG(pm8001_ha,
438 pm8001_printk("rc is %x\n", rc));
439 goto err_out_tag;
441 /* TODO: select normal or high priority */
442 spin_lock(&t->task_state_lock);
443 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
444 spin_unlock(&t->task_state_lock);
445 pm8001_dev->running_req++;
446 if (n > 1)
447 t = list_entry(t->list.next, struct sas_task, list);
448 } while (--n);
449 rc = 0;
450 goto out_done;
452 err_out_tag:
453 pm8001_tag_free(pm8001_ha, tag);
454 err_out:
455 dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
456 if (!sas_protocol_ata(t->task_proto))
457 if (n_elem)
458 dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem,
459 t->data_dir);
460 out_done:
461 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
462 return rc;
466 * pm8001_queue_command - register for upper layer used, all IO commands sent
467 * to HBA are from this interface.
468 * @task: the task to be execute.
469 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
470 * we always execute one one time
471 * @gfp_flags: gfp_flags
473 int pm8001_queue_command(struct sas_task *task, const int num,
474 gfp_t gfp_flags)
476 return pm8001_task_exec(task, num, gfp_flags, 0, NULL);
479 void pm8001_ccb_free(struct pm8001_hba_info *pm8001_ha, u32 ccb_idx)
481 pm8001_tag_clear(pm8001_ha, ccb_idx);
485 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
486 * @pm8001_ha: our hba card information
487 * @ccb: the ccb which attached to ssp task
488 * @task: the task to be free.
489 * @ccb_idx: ccb index.
491 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
492 struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
494 if (!ccb->task)
495 return;
496 if (!sas_protocol_ata(task->task_proto))
497 if (ccb->n_elem)
498 dma_unmap_sg(pm8001_ha->dev, task->scatter,
499 task->num_scatter, task->data_dir);
501 switch (task->task_proto) {
502 case SAS_PROTOCOL_SMP:
503 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
504 PCI_DMA_FROMDEVICE);
505 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
506 PCI_DMA_TODEVICE);
507 break;
509 case SAS_PROTOCOL_SATA:
510 case SAS_PROTOCOL_STP:
511 case SAS_PROTOCOL_SSP:
512 default:
513 /* do nothing */
514 break;
516 task->lldd_task = NULL;
517 ccb->task = NULL;
518 ccb->ccb_tag = 0xFFFFFFFF;
519 pm8001_ccb_free(pm8001_ha, ccb_idx);
523 * pm8001_alloc_dev - find a empty pm8001_device
524 * @pm8001_ha: our hba card information
526 struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
528 u32 dev;
529 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
530 if (pm8001_ha->devices[dev].dev_type == NO_DEVICE) {
531 pm8001_ha->devices[dev].id = dev;
532 return &pm8001_ha->devices[dev];
535 if (dev == PM8001_MAX_DEVICES) {
536 PM8001_FAIL_DBG(pm8001_ha,
537 pm8001_printk("max support %d devices, ignore ..\n",
538 PM8001_MAX_DEVICES));
540 return NULL;
543 static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
545 u32 id = pm8001_dev->id;
546 memset(pm8001_dev, 0, sizeof(*pm8001_dev));
547 pm8001_dev->id = id;
548 pm8001_dev->dev_type = NO_DEVICE;
549 pm8001_dev->device_id = PM8001_MAX_DEVICES;
550 pm8001_dev->sas_device = NULL;
554 * pm8001_dev_found_notify - libsas notify a device is found.
555 * @dev: the device structure which sas layer used.
557 * when libsas find a sas domain device, it should tell the LLDD that
558 * device is found, and then LLDD register this device to HBA firmware
559 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
560 * device ID(according to device's sas address) and returned it to LLDD. From
561 * now on, we communicate with HBA FW with the device ID which HBA assigned
562 * rather than sas address. it is the necessary step for our HBA but it is
563 * the optional for other HBA driver.
565 static int pm8001_dev_found_notify(struct domain_device *dev)
567 unsigned long flags = 0;
568 int res = 0;
569 struct pm8001_hba_info *pm8001_ha = NULL;
570 struct domain_device *parent_dev = dev->parent;
571 struct pm8001_device *pm8001_device;
572 DECLARE_COMPLETION_ONSTACK(completion);
573 u32 flag = 0;
574 pm8001_ha = pm8001_find_ha_by_dev(dev);
575 spin_lock_irqsave(&pm8001_ha->lock, flags);
577 pm8001_device = pm8001_alloc_dev(pm8001_ha);
578 if (!pm8001_device) {
579 res = -1;
580 goto found_out;
582 pm8001_device->sas_device = dev;
583 dev->lldd_dev = pm8001_device;
584 pm8001_device->dev_type = dev->dev_type;
585 pm8001_device->dcompletion = &completion;
586 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
587 int phy_id;
588 struct ex_phy *phy;
589 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
590 phy_id++) {
591 phy = &parent_dev->ex_dev.ex_phy[phy_id];
592 if (SAS_ADDR(phy->attached_sas_addr)
593 == SAS_ADDR(dev->sas_addr)) {
594 pm8001_device->attached_phy = phy_id;
595 break;
598 if (phy_id == parent_dev->ex_dev.num_phys) {
599 PM8001_FAIL_DBG(pm8001_ha,
600 pm8001_printk("Error: no attached dev:%016llx"
601 " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
602 SAS_ADDR(parent_dev->sas_addr)));
603 res = -1;
605 } else {
606 if (dev->dev_type == SATA_DEV) {
607 pm8001_device->attached_phy =
608 dev->rphy->identify.phy_identifier;
609 flag = 1; /* directly sata*/
611 } /*register this device to HBA*/
612 PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device\n"));
613 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
614 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
615 wait_for_completion(&completion);
616 if (dev->dev_type == SAS_END_DEV)
617 msleep(50);
618 pm8001_ha->flags |= PM8001F_RUN_TIME ;
619 return 0;
620 found_out:
621 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
622 return res;
625 int pm8001_dev_found(struct domain_device *dev)
627 return pm8001_dev_found_notify(dev);
630 static void pm8001_task_done(struct sas_task *task)
632 if (!del_timer(&task->timer))
633 return;
634 complete(&task->completion);
637 static void pm8001_tmf_timedout(unsigned long data)
639 struct sas_task *task = (struct sas_task *)data;
641 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
642 complete(&task->completion);
645 #define PM8001_TASK_TIMEOUT 20
647 * pm8001_exec_internal_tmf_task - execute some task management commands.
648 * @dev: the wanted device.
649 * @tmf: which task management wanted to be take.
650 * @para_len: para_len.
651 * @parameter: ssp task parameter.
653 * when errors or exception happened, we may want to do something, for example
654 * abort the issued task which result in this execption, it is done by calling
655 * this function, note it is also with the task execute interface.
657 static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
658 void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
660 int res, retry;
661 struct sas_task *task = NULL;
662 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
664 for (retry = 0; retry < 3; retry++) {
665 task = sas_alloc_task(GFP_KERNEL);
666 if (!task)
667 return -ENOMEM;
669 task->dev = dev;
670 task->task_proto = dev->tproto;
671 memcpy(&task->ssp_task, parameter, para_len);
672 task->task_done = pm8001_task_done;
673 task->timer.data = (unsigned long)task;
674 task->timer.function = pm8001_tmf_timedout;
675 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
676 add_timer(&task->timer);
678 res = pm8001_task_exec(task, 1, GFP_KERNEL, 1, tmf);
680 if (res) {
681 del_timer(&task->timer);
682 PM8001_FAIL_DBG(pm8001_ha,
683 pm8001_printk("Executing internal task "
684 "failed\n"));
685 goto ex_err;
687 wait_for_completion(&task->completion);
688 res = -TMF_RESP_FUNC_FAILED;
689 /* Even TMF timed out, return direct. */
690 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
691 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
692 PM8001_FAIL_DBG(pm8001_ha,
693 pm8001_printk("TMF task[%x]timeout.\n",
694 tmf->tmf));
695 goto ex_err;
699 if (task->task_status.resp == SAS_TASK_COMPLETE &&
700 task->task_status.stat == SAM_STAT_GOOD) {
701 res = TMF_RESP_FUNC_COMPLETE;
702 break;
705 if (task->task_status.resp == SAS_TASK_COMPLETE &&
706 task->task_status.stat == SAS_DATA_UNDERRUN) {
707 /* no error, but return the number of bytes of
708 * underrun */
709 res = task->task_status.residual;
710 break;
713 if (task->task_status.resp == SAS_TASK_COMPLETE &&
714 task->task_status.stat == SAS_DATA_OVERRUN) {
715 PM8001_FAIL_DBG(pm8001_ha,
716 pm8001_printk("Blocked task error.\n"));
717 res = -EMSGSIZE;
718 break;
719 } else {
720 PM8001_EH_DBG(pm8001_ha,
721 pm8001_printk(" Task to dev %016llx response:"
722 "0x%x status 0x%x\n",
723 SAS_ADDR(dev->sas_addr),
724 task->task_status.resp,
725 task->task_status.stat));
726 sas_free_task(task);
727 task = NULL;
730 ex_err:
731 BUG_ON(retry == 3 && task != NULL);
732 sas_free_task(task);
733 return res;
736 static int
737 pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
738 struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
739 u32 task_tag)
741 int res, retry;
742 u32 ccb_tag;
743 struct pm8001_ccb_info *ccb;
744 struct sas_task *task = NULL;
746 for (retry = 0; retry < 3; retry++) {
747 task = sas_alloc_task(GFP_KERNEL);
748 if (!task)
749 return -ENOMEM;
751 task->dev = dev;
752 task->task_proto = dev->tproto;
753 task->task_done = pm8001_task_done;
754 task->timer.data = (unsigned long)task;
755 task->timer.function = pm8001_tmf_timedout;
756 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
757 add_timer(&task->timer);
759 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
760 if (res)
761 return res;
762 ccb = &pm8001_ha->ccb_info[ccb_tag];
763 ccb->device = pm8001_dev;
764 ccb->ccb_tag = ccb_tag;
765 ccb->task = task;
767 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
768 pm8001_dev, flag, task_tag, ccb_tag);
770 if (res) {
771 del_timer(&task->timer);
772 PM8001_FAIL_DBG(pm8001_ha,
773 pm8001_printk("Executing internal task "
774 "failed\n"));
775 goto ex_err;
777 wait_for_completion(&task->completion);
778 res = TMF_RESP_FUNC_FAILED;
779 /* Even TMF timed out, return direct. */
780 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
781 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
782 PM8001_FAIL_DBG(pm8001_ha,
783 pm8001_printk("TMF task timeout.\n"));
784 goto ex_err;
788 if (task->task_status.resp == SAS_TASK_COMPLETE &&
789 task->task_status.stat == SAM_STAT_GOOD) {
790 res = TMF_RESP_FUNC_COMPLETE;
791 break;
793 } else {
794 PM8001_EH_DBG(pm8001_ha,
795 pm8001_printk(" Task to dev %016llx response: "
796 "0x%x status 0x%x\n",
797 SAS_ADDR(dev->sas_addr),
798 task->task_status.resp,
799 task->task_status.stat));
800 sas_free_task(task);
801 task = NULL;
804 ex_err:
805 BUG_ON(retry == 3 && task != NULL);
806 sas_free_task(task);
807 return res;
811 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
812 * @dev: the device structure which sas layer used.
814 static void pm8001_dev_gone_notify(struct domain_device *dev)
816 unsigned long flags = 0;
817 u32 tag;
818 struct pm8001_hba_info *pm8001_ha;
819 struct pm8001_device *pm8001_dev = dev->lldd_dev;
821 pm8001_ha = pm8001_find_ha_by_dev(dev);
822 spin_lock_irqsave(&pm8001_ha->lock, flags);
823 pm8001_tag_alloc(pm8001_ha, &tag);
824 if (pm8001_dev) {
825 u32 device_id = pm8001_dev->device_id;
827 PM8001_DISC_DBG(pm8001_ha,
828 pm8001_printk("found dev[%d:%x] is gone.\n",
829 pm8001_dev->device_id, pm8001_dev->dev_type));
830 if (pm8001_dev->running_req) {
831 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
832 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
833 dev, 1, 0);
834 spin_lock_irqsave(&pm8001_ha->lock, flags);
836 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
837 pm8001_free_dev(pm8001_dev);
838 } else {
839 PM8001_DISC_DBG(pm8001_ha,
840 pm8001_printk("Found dev has gone.\n"));
842 dev->lldd_dev = NULL;
843 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
846 void pm8001_dev_gone(struct domain_device *dev)
848 pm8001_dev_gone_notify(dev);
851 static int pm8001_issue_ssp_tmf(struct domain_device *dev,
852 u8 *lun, struct pm8001_tmf_task *tmf)
854 struct sas_ssp_task ssp_task;
855 if (!(dev->tproto & SAS_PROTOCOL_SSP))
856 return TMF_RESP_FUNC_ESUPP;
858 strncpy((u8 *)&ssp_task.LUN, lun, 8);
859 return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
860 tmf);
864 * Standard mandates link reset for ATA (type 0) and hard reset for
865 * SSP (type 1) , only for RECOVERY
867 int pm8001_I_T_nexus_reset(struct domain_device *dev)
869 int rc = TMF_RESP_FUNC_FAILED;
870 struct pm8001_device *pm8001_dev;
871 struct pm8001_hba_info *pm8001_ha;
872 struct sas_phy *phy;
873 if (!dev || !dev->lldd_dev)
874 return -1;
876 pm8001_dev = dev->lldd_dev;
877 pm8001_ha = pm8001_find_ha_by_dev(dev);
878 phy = sas_find_local_phy(dev);
880 if (dev_is_sata(dev)) {
881 DECLARE_COMPLETION_ONSTACK(completion_setstate);
882 if (scsi_is_sas_phy_local(phy))
883 return 0;
884 rc = sas_phy_reset(phy, 1);
885 msleep(2000);
886 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
887 dev, 1, 0);
888 pm8001_dev->setds_completion = &completion_setstate;
889 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
890 pm8001_dev, 0x01);
891 wait_for_completion(&completion_setstate);
892 } else{
893 rc = sas_phy_reset(phy, 1);
894 msleep(2000);
896 PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
897 pm8001_dev->device_id, rc));
898 return rc;
901 /* mandatory SAM-3, the task reset the specified LUN*/
902 int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
904 int rc = TMF_RESP_FUNC_FAILED;
905 struct pm8001_tmf_task tmf_task;
906 struct pm8001_device *pm8001_dev = dev->lldd_dev;
907 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
908 if (dev_is_sata(dev)) {
909 struct sas_phy *phy = sas_find_local_phy(dev);
910 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
911 dev, 1, 0);
912 rc = sas_phy_reset(phy, 1);
913 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
914 pm8001_dev, 0x01);
915 msleep(2000);
916 } else {
917 tmf_task.tmf = TMF_LU_RESET;
918 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
920 /* If failed, fall-through I_T_Nexus reset */
921 PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
922 pm8001_dev->device_id, rc));
923 return rc;
926 /* optional SAM-3 */
927 int pm8001_query_task(struct sas_task *task)
929 u32 tag = 0xdeadbeef;
930 int i = 0;
931 struct scsi_lun lun;
932 struct pm8001_tmf_task tmf_task;
933 int rc = TMF_RESP_FUNC_FAILED;
934 if (unlikely(!task || !task->lldd_task || !task->dev))
935 return rc;
937 if (task->task_proto & SAS_PROTOCOL_SSP) {
938 struct scsi_cmnd *cmnd = task->uldd_task;
939 struct domain_device *dev = task->dev;
940 struct pm8001_hba_info *pm8001_ha =
941 pm8001_find_ha_by_dev(dev);
943 int_to_scsilun(cmnd->device->lun, &lun);
944 rc = pm8001_find_tag(task, &tag);
945 if (rc == 0) {
946 rc = TMF_RESP_FUNC_FAILED;
947 return rc;
949 PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
950 for (i = 0; i < 16; i++)
951 printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
952 printk(KERN_INFO "]\n");
953 tmf_task.tmf = TMF_QUERY_TASK;
954 tmf_task.tag_of_task_to_be_managed = tag;
956 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
957 switch (rc) {
958 /* The task is still in Lun, release it then */
959 case TMF_RESP_FUNC_SUCC:
960 PM8001_EH_DBG(pm8001_ha,
961 pm8001_printk("The task is still in Lun\n"));
962 break;
963 /* The task is not in Lun or failed, reset the phy */
964 case TMF_RESP_FUNC_FAILED:
965 case TMF_RESP_FUNC_COMPLETE:
966 PM8001_EH_DBG(pm8001_ha,
967 pm8001_printk("The task is not in Lun or failed,"
968 " reset the phy\n"));
969 break;
972 pm8001_printk(":rc= %d\n", rc);
973 return rc;
976 /* mandatory SAM-3, still need free task/ccb info, abord the specified task */
977 int pm8001_abort_task(struct sas_task *task)
979 unsigned long flags;
980 u32 tag = 0xdeadbeef;
981 u32 device_id;
982 struct domain_device *dev ;
983 struct pm8001_hba_info *pm8001_ha = NULL;
984 struct pm8001_ccb_info *ccb;
985 struct scsi_lun lun;
986 struct pm8001_device *pm8001_dev;
987 struct pm8001_tmf_task tmf_task;
988 int rc = TMF_RESP_FUNC_FAILED;
989 if (unlikely(!task || !task->lldd_task || !task->dev))
990 return rc;
991 spin_lock_irqsave(&task->task_state_lock, flags);
992 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
993 spin_unlock_irqrestore(&task->task_state_lock, flags);
994 rc = TMF_RESP_FUNC_COMPLETE;
995 goto out;
997 spin_unlock_irqrestore(&task->task_state_lock, flags);
998 if (task->task_proto & SAS_PROTOCOL_SSP) {
999 struct scsi_cmnd *cmnd = task->uldd_task;
1000 dev = task->dev;
1001 ccb = task->lldd_task;
1002 pm8001_dev = dev->lldd_dev;
1003 pm8001_ha = pm8001_find_ha_by_dev(dev);
1004 int_to_scsilun(cmnd->device->lun, &lun);
1005 rc = pm8001_find_tag(task, &tag);
1006 if (rc == 0) {
1007 printk(KERN_INFO "No such tag in %s\n", __func__);
1008 rc = TMF_RESP_FUNC_FAILED;
1009 return rc;
1011 device_id = pm8001_dev->device_id;
1012 PM8001_EH_DBG(pm8001_ha,
1013 pm8001_printk("abort io to deviceid= %d\n", device_id));
1014 tmf_task.tmf = TMF_ABORT_TASK;
1015 tmf_task.tag_of_task_to_be_managed = tag;
1016 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1017 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1018 pm8001_dev->sas_device, 0, tag);
1019 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1020 task->task_proto & SAS_PROTOCOL_STP) {
1021 dev = task->dev;
1022 pm8001_dev = dev->lldd_dev;
1023 pm8001_ha = pm8001_find_ha_by_dev(dev);
1024 rc = pm8001_find_tag(task, &tag);
1025 if (rc == 0) {
1026 printk(KERN_INFO "No such tag in %s\n", __func__);
1027 rc = TMF_RESP_FUNC_FAILED;
1028 return rc;
1030 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1031 pm8001_dev->sas_device, 0, tag);
1032 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1033 /* SMP */
1034 dev = task->dev;
1035 pm8001_dev = dev->lldd_dev;
1036 pm8001_ha = pm8001_find_ha_by_dev(dev);
1037 rc = pm8001_find_tag(task, &tag);
1038 if (rc == 0) {
1039 printk(KERN_INFO "No such tag in %s\n", __func__);
1040 rc = TMF_RESP_FUNC_FAILED;
1041 return rc;
1043 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1044 pm8001_dev->sas_device, 0, tag);
1047 out:
1048 if (rc != TMF_RESP_FUNC_COMPLETE)
1049 pm8001_printk("rc= %d\n", rc);
1050 return rc;
1053 int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1055 int rc = TMF_RESP_FUNC_FAILED;
1056 struct pm8001_tmf_task tmf_task;
1058 tmf_task.tmf = TMF_ABORT_TASK_SET;
1059 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1060 return rc;
1063 int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1065 int rc = TMF_RESP_FUNC_FAILED;
1066 struct pm8001_tmf_task tmf_task;
1068 tmf_task.tmf = TMF_CLEAR_ACA;
1069 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1071 return rc;
1074 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1076 int rc = TMF_RESP_FUNC_FAILED;
1077 struct pm8001_tmf_task tmf_task;
1078 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1079 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1081 PM8001_EH_DBG(pm8001_ha,
1082 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1083 pm8001_dev->device_id));
1084 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1085 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1086 return rc;