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