blob: 6bba59c7d657eaaf0709df704c9ee73ca076a29f [file] [log] [blame]
jack wangdbf9bfe2009-10-14 16:19:21 +08001/*
Sakthivel Kf5860992013-04-17 16:37:02 +05302 * PMC-Sierra PM8001/8081/8088/8089 SAS/SATA based host adapters driver
jack wangdbf9bfe2009-10-14 16:19:21 +08003 *
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
jack wangdbf9bfe2009-10-14 16:19:21 +080042#include "pm8001_sas.h"
43
44/**
45 * pm8001_find_tag - from sas task to find out tag that belongs to this task
46 * @task: the task sent to the LLDD
47 * @tag: the found tag associated with the task
48 */
49static int pm8001_find_tag(struct sas_task *task, u32 *tag)
50{
51 if (task->lldd_task) {
52 struct pm8001_ccb_info *ccb;
53 ccb = task->lldd_task;
54 *tag = ccb->ccb_tag;
55 return 1;
56 }
57 return 0;
58}
59
60/**
61 * pm8001_tag_clear - clear the tags bitmap
62 * @pm8001_ha: our hba struct
63 * @tag: the found tag associated with the task
64 */
65static void pm8001_tag_clear(struct pm8001_hba_info *pm8001_ha, u32 tag)
66{
67 void *bitmap = pm8001_ha->tags;
68 clear_bit(tag, bitmap);
69}
70
71static void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
72{
73 pm8001_tag_clear(pm8001_ha, tag);
74}
75
76static void pm8001_tag_set(struct pm8001_hba_info *pm8001_ha, u32 tag)
77{
78 void *bitmap = pm8001_ha->tags;
79 set_bit(tag, bitmap);
80}
81
82/**
83 * pm8001_tag_alloc - allocate a empty tag for task used.
84 * @pm8001_ha: our hba struct
85 * @tag_out: the found empty tag .
86 */
87inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out)
88{
89 unsigned int index, tag;
90 void *bitmap = pm8001_ha->tags;
91
92 index = find_first_zero_bit(bitmap, pm8001_ha->tags_num);
93 tag = index;
94 if (tag >= pm8001_ha->tags_num)
95 return -SAS_QUEUE_FULL;
96 pm8001_tag_set(pm8001_ha, tag);
97 *tag_out = tag;
98 return 0;
99}
100
101void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha)
102{
103 int i;
104 for (i = 0; i < pm8001_ha->tags_num; ++i)
105 pm8001_tag_clear(pm8001_ha, i);
106}
107
108 /**
109 * pm8001_mem_alloc - allocate memory for pm8001.
110 * @pdev: pci device.
111 * @virt_addr: the allocated virtual address
112 * @pphys_addr_hi: the physical address high byte address.
113 * @pphys_addr_lo: the physical address low byte address.
114 * @mem_size: memory size.
115 */
116int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr,
117 dma_addr_t *pphys_addr, u32 *pphys_addr_hi,
118 u32 *pphys_addr_lo, u32 mem_size, u32 align)
119{
120 caddr_t mem_virt_alloc;
121 dma_addr_t mem_dma_handle;
122 u64 phys_align;
123 u64 align_offset = 0;
124 if (align)
125 align_offset = (dma_addr_t)align - 1;
126 mem_virt_alloc =
127 pci_alloc_consistent(pdev, mem_size + align, &mem_dma_handle);
128 if (!mem_virt_alloc) {
129 pm8001_printk("memory allocation error\n");
130 return -1;
131 }
132 memset((void *)mem_virt_alloc, 0, mem_size+align);
133 *pphys_addr = mem_dma_handle;
134 phys_align = (*pphys_addr + align_offset) & ~align_offset;
135 *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr;
136 *pphys_addr_hi = upper_32_bits(phys_align);
137 *pphys_addr_lo = lower_32_bits(phys_align);
138 return 0;
139}
140/**
141 * pm8001_find_ha_by_dev - from domain device which come from sas layer to
142 * find out our hba struct.
143 * @dev: the domain device which from sas layer.
144 */
145static
146struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev)
147{
148 struct sas_ha_struct *sha = dev->port->ha;
149 struct pm8001_hba_info *pm8001_ha = sha->lldd_ha;
150 return pm8001_ha;
151}
152
153/**
154 * pm8001_phy_control - this function should be registered to
155 * sas_domain_function_template to provide libsas used, note: this is just
156 * control the HBA phy rather than other expander phy if you want control
157 * other phy, you should use SMP command.
158 * @sas_phy: which phy in HBA phys.
159 * @func: the operation.
160 * @funcdata: always NULL.
161 */
162int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
163 void *funcdata)
164{
165 int rc = 0, phy_id = sas_phy->id;
166 struct pm8001_hba_info *pm8001_ha = NULL;
167 struct sas_phy_linkrates *rates;
168 DECLARE_COMPLETION_ONSTACK(completion);
Mark Salyzynd95d0002012-01-17 09:18:57 -0500169 unsigned long flags;
jack wangdbf9bfe2009-10-14 16:19:21 +0800170 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;
178 }
179 if (rates->maximum_linkrate) {
180 pm8001_ha->phy[phy_id].maximum_linkrate =
181 rates->maximum_linkrate;
182 }
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);
186 }
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);
194 }
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);
202 }
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;
Mark Salyzynd95d0002012-01-17 09:18:57 -0500213 case PHY_FUNC_GET_EVENTS:
214 spin_lock_irqsave(&pm8001_ha->lock, flags);
Sakthivel Kf5860992013-04-17 16:37:02 +0530215 if (pm8001_ha->chip_id == chip_8001) {
216 if (-1 == pm8001_bar4_shift(pm8001_ha,
Mark Salyzynd95d0002012-01-17 09:18:57 -0500217 (phy_id < 4) ? 0x30000 : 0x40000)) {
Sakthivel Kf5860992013-04-17 16:37:02 +0530218 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
219 return -EINVAL;
220 }
Mark Salyzynd95d0002012-01-17 09:18:57 -0500221 }
222 {
223 struct sas_phy *phy = sas_phy->phy;
224 uint32_t *qp = (uint32_t *)(((char *)
225 pm8001_ha->io_mem[2].memvirtaddr)
226 + 0x1034 + (0x4000 * (phy_id & 3)));
227
228 phy->invalid_dword_count = qp[0];
229 phy->running_disparity_error_count = qp[1];
230 phy->loss_of_dword_sync_count = qp[3];
231 phy->phy_reset_problem_count = qp[4];
232 }
Sakthivel Kf5860992013-04-17 16:37:02 +0530233 if (pm8001_ha->chip_id == chip_8001)
234 pm8001_bar4_shift(pm8001_ha, 0);
Mark Salyzynd95d0002012-01-17 09:18:57 -0500235 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
236 return 0;
jack wangdbf9bfe2009-10-14 16:19:21 +0800237 default:
Mark Salyzynd95d0002012-01-17 09:18:57 -0500238 rc = -EOPNOTSUPP;
jack wangdbf9bfe2009-10-14 16:19:21 +0800239 }
240 msleep(300);
241 return rc;
242}
243
jack wangdbf9bfe2009-10-14 16:19:21 +0800244/**
245 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
246 * command to HBA.
247 * @shost: the scsi host data.
248 */
249void pm8001_scan_start(struct Scsi_Host *shost)
250{
251 int i;
252 struct pm8001_hba_info *pm8001_ha;
253 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
254 pm8001_ha = sha->lldd_ha;
Sakthivel Kf5860992013-04-17 16:37:02 +0530255 /* SAS_RE_INITIALIZATION not available in SPCv/ve */
256 if (pm8001_ha->chip_id == chip_8001)
257 PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
jack wangdbf9bfe2009-10-14 16:19:21 +0800258 for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
259 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
260}
261
262int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
263{
Dan Williamsb1124cd2011-12-19 16:42:34 -0800264 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
265
jack wangdbf9bfe2009-10-14 16:19:21 +0800266 /* give the phy enabling interrupt event time to come in (1s
267 * is empirically about all it takes) */
268 if (time < HZ)
269 return 0;
270 /* Wait for discovery to finish */
Dan Williamsb1124cd2011-12-19 16:42:34 -0800271 sas_drain_work(ha);
jack wangdbf9bfe2009-10-14 16:19:21 +0800272 return 1;
273}
274
275/**
276 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
277 * @pm8001_ha: our hba card information
278 * @ccb: the ccb which attached to smp task
279 */
280static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
281 struct pm8001_ccb_info *ccb)
282{
283 return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
284}
285
286u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
287{
288 struct ata_queued_cmd *qc = task->uldd_task;
289 if (qc) {
290 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
291 qc->tf.command == ATA_CMD_FPDMA_READ) {
292 *tag = qc->tag;
293 return 1;
294 }
295 }
296 return 0;
297}
298
299/**
300 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
301 * @pm8001_ha: our hba card information
302 * @ccb: the ccb which attached to sata task
303 */
304static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
305 struct pm8001_ccb_info *ccb)
306{
307 return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
308}
309
310/**
311 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
312 * @pm8001_ha: our hba card information
313 * @ccb: the ccb which attached to TM
314 * @tmf: the task management IU
315 */
316static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
317 struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
318{
319 return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
320}
321
322/**
323 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
324 * @pm8001_ha: our hba card information
325 * @ccb: the ccb which attached to ssp task
326 */
327static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
328 struct pm8001_ccb_info *ccb)
329{
330 return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
331}
Dan Williams11e16362011-09-20 15:11:03 -0700332
jack wang1cc943a2009-12-07 17:22:42 +0800333 /* Find the local port id that's attached to this device */
334static int sas_find_local_port_id(struct domain_device *dev)
335{
336 struct domain_device *pdev = dev->parent;
337
338 /* Directly attached device */
339 if (!pdev)
340 return dev->port->id;
341 while (pdev) {
342 struct domain_device *pdev_p = pdev->parent;
343 if (!pdev_p)
344 return pdev->port->id;
345 pdev = pdev->parent;
346 }
347 return 0;
348}
349
jack wangdbf9bfe2009-10-14 16:19:21 +0800350/**
jack_wang97ee2082009-11-05 22:33:51 +0800351 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
jack wangdbf9bfe2009-10-14 16:19:21 +0800352 * @task: the task to be execute.
353 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
354 * we always execute one one time.
355 * @gfp_flags: gfp_flags.
jack_wang97ee2082009-11-05 22:33:51 +0800356 * @is_tmf: if it is task management task.
jack wangdbf9bfe2009-10-14 16:19:21 +0800357 * @tmf: the task management IU
358 */
359#define DEV_IS_GONE(pm8001_dev) \
360 ((!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE)))
361static int pm8001_task_exec(struct sas_task *task, const int num,
362 gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
363{
364 struct domain_device *dev = task->dev;
365 struct pm8001_hba_info *pm8001_ha;
366 struct pm8001_device *pm8001_dev;
jack wang1cc943a2009-12-07 17:22:42 +0800367 struct pm8001_port *port = NULL;
jack wangdbf9bfe2009-10-14 16:19:21 +0800368 struct sas_task *t = task;
369 struct pm8001_ccb_info *ccb;
370 u32 tag = 0xdeadbeef, rc, n_elem = 0;
371 u32 n = num;
Dan Williams312d3e52011-11-17 17:59:50 -0800372 unsigned long flags = 0;
jack wangdbf9bfe2009-10-14 16:19:21 +0800373
374 if (!dev->port) {
375 struct task_status_struct *tsm = &t->task_status;
376 tsm->resp = SAS_TASK_UNDELIVERED;
377 tsm->stat = SAS_PHY_DOWN;
378 if (dev->dev_type != SATA_DEV)
379 t->task_done(t);
380 return 0;
381 }
382 pm8001_ha = pm8001_find_ha_by_dev(task->dev);
383 PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n "));
384 spin_lock_irqsave(&pm8001_ha->lock, flags);
385 do {
386 dev = t->dev;
387 pm8001_dev = dev->lldd_dev;
jack wang1cc943a2009-12-07 17:22:42 +0800388 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
Mark Salyzynb90b3782011-09-22 08:50:01 -0700389 if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
jack wang1cc943a2009-12-07 17:22:42 +0800390 if (sas_protocol_ata(t->task_proto)) {
391 struct task_status_struct *ts = &t->task_status;
392 ts->resp = SAS_TASK_UNDELIVERED;
393 ts->stat = SAS_PHY_DOWN;
394
395 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
jack wang1cc943a2009-12-07 17:22:42 +0800396 t->task_done(t);
jack wang1cc943a2009-12-07 17:22:42 +0800397 spin_lock_irqsave(&pm8001_ha->lock, flags);
398 if (n > 1)
399 t = list_entry(t->list.next,
400 struct sas_task, list);
401 continue;
402 } else {
403 struct task_status_struct *ts = &t->task_status;
404 ts->resp = SAS_TASK_UNDELIVERED;
405 ts->stat = SAS_PHY_DOWN;
406 t->task_done(t);
407 if (n > 1)
408 t = list_entry(t->list.next,
409 struct sas_task, list);
410 continue;
411 }
412 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800413 rc = pm8001_tag_alloc(pm8001_ha, &tag);
414 if (rc)
415 goto err_out;
416 ccb = &pm8001_ha->ccb_info[tag];
417
418 if (!sas_protocol_ata(t->task_proto)) {
419 if (t->num_scatter) {
420 n_elem = dma_map_sg(pm8001_ha->dev,
421 t->scatter,
422 t->num_scatter,
423 t->data_dir);
424 if (!n_elem) {
425 rc = -ENOMEM;
jack_wang97ee2082009-11-05 22:33:51 +0800426 goto err_out_tag;
jack wangdbf9bfe2009-10-14 16:19:21 +0800427 }
428 }
429 } else {
430 n_elem = t->num_scatter;
431 }
432
jack_wang97ee2082009-11-05 22:33:51 +0800433 t->lldd_task = ccb;
jack wangdbf9bfe2009-10-14 16:19:21 +0800434 ccb->n_elem = n_elem;
435 ccb->ccb_tag = tag;
436 ccb->task = t;
437 switch (t->task_proto) {
438 case SAS_PROTOCOL_SMP:
439 rc = pm8001_task_prep_smp(pm8001_ha, ccb);
440 break;
441 case SAS_PROTOCOL_SSP:
442 if (is_tmf)
443 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
444 ccb, tmf);
445 else
446 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
447 break;
448 case SAS_PROTOCOL_SATA:
449 case SAS_PROTOCOL_STP:
450 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
451 rc = pm8001_task_prep_ata(pm8001_ha, ccb);
452 break;
453 default:
454 dev_printk(KERN_ERR, pm8001_ha->dev,
455 "unknown sas_task proto: 0x%x\n",
456 t->task_proto);
457 rc = -EINVAL;
458 break;
459 }
460
461 if (rc) {
462 PM8001_IO_DBG(pm8001_ha,
463 pm8001_printk("rc is %x\n", rc));
464 goto err_out_tag;
465 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800466 /* TODO: select normal or high priority */
467 spin_lock(&t->task_state_lock);
468 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
469 spin_unlock(&t->task_state_lock);
470 pm8001_dev->running_req++;
471 if (n > 1)
472 t = list_entry(t->list.next, struct sas_task, list);
473 } while (--n);
474 rc = 0;
475 goto out_done;
476
477err_out_tag:
478 pm8001_tag_free(pm8001_ha, tag);
479err_out:
480 dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
481 if (!sas_protocol_ata(t->task_proto))
482 if (n_elem)
483 dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem,
484 t->data_dir);
485out_done:
486 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
487 return rc;
488}
489
490/**
491 * pm8001_queue_command - register for upper layer used, all IO commands sent
492 * to HBA are from this interface.
493 * @task: the task to be execute.
494 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
495 * we always execute one one time
496 * @gfp_flags: gfp_flags
497 */
498int pm8001_queue_command(struct sas_task *task, const int num,
499 gfp_t gfp_flags)
500{
501 return pm8001_task_exec(task, num, gfp_flags, 0, NULL);
502}
503
504void pm8001_ccb_free(struct pm8001_hba_info *pm8001_ha, u32 ccb_idx)
505{
506 pm8001_tag_clear(pm8001_ha, ccb_idx);
507}
508
509/**
510 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
511 * @pm8001_ha: our hba card information
512 * @ccb: the ccb which attached to ssp task
513 * @task: the task to be free.
514 * @ccb_idx: ccb index.
515 */
516void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
517 struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
518{
519 if (!ccb->task)
520 return;
521 if (!sas_protocol_ata(task->task_proto))
522 if (ccb->n_elem)
523 dma_unmap_sg(pm8001_ha->dev, task->scatter,
524 task->num_scatter, task->data_dir);
525
526 switch (task->task_proto) {
527 case SAS_PROTOCOL_SMP:
528 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
529 PCI_DMA_FROMDEVICE);
530 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
531 PCI_DMA_TODEVICE);
532 break;
533
534 case SAS_PROTOCOL_SATA:
535 case SAS_PROTOCOL_STP:
536 case SAS_PROTOCOL_SSP:
537 default:
538 /* do nothing */
539 break;
540 }
541 task->lldd_task = NULL;
542 ccb->task = NULL;
543 ccb->ccb_tag = 0xFFFFFFFF;
Mark Salyzyn5954d732012-01-17 11:52:24 -0500544 ccb->open_retry = 0;
jack wangdbf9bfe2009-10-14 16:19:21 +0800545 pm8001_ccb_free(pm8001_ha, ccb_idx);
546}
547
548 /**
jack_wang97ee2082009-11-05 22:33:51 +0800549 * pm8001_alloc_dev - find a empty pm8001_device
jack wangdbf9bfe2009-10-14 16:19:21 +0800550 * @pm8001_ha: our hba card information
551 */
552struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
553{
554 u32 dev;
555 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
556 if (pm8001_ha->devices[dev].dev_type == NO_DEVICE) {
557 pm8001_ha->devices[dev].id = dev;
558 return &pm8001_ha->devices[dev];
559 }
560 }
561 if (dev == PM8001_MAX_DEVICES) {
562 PM8001_FAIL_DBG(pm8001_ha,
563 pm8001_printk("max support %d devices, ignore ..\n",
564 PM8001_MAX_DEVICES));
565 }
566 return NULL;
567}
568
569static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
570{
571 u32 id = pm8001_dev->id;
572 memset(pm8001_dev, 0, sizeof(*pm8001_dev));
573 pm8001_dev->id = id;
574 pm8001_dev->dev_type = NO_DEVICE;
575 pm8001_dev->device_id = PM8001_MAX_DEVICES;
576 pm8001_dev->sas_device = NULL;
577}
578
579/**
jack_wang97ee2082009-11-05 22:33:51 +0800580 * pm8001_dev_found_notify - libsas notify a device is found.
581 * @dev: the device structure which sas layer used.
582 *
583 * when libsas find a sas domain device, it should tell the LLDD that
584 * device is found, and then LLDD register this device to HBA firmware
585 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
586 * device ID(according to device's sas address) and returned it to LLDD. From
jack wangdbf9bfe2009-10-14 16:19:21 +0800587 * now on, we communicate with HBA FW with the device ID which HBA assigned
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800588 * rather than sas address. it is the necessary step for our HBA but it is
jack wangdbf9bfe2009-10-14 16:19:21 +0800589 * the optional for other HBA driver.
jack wangdbf9bfe2009-10-14 16:19:21 +0800590 */
591static int pm8001_dev_found_notify(struct domain_device *dev)
592{
593 unsigned long flags = 0;
594 int res = 0;
595 struct pm8001_hba_info *pm8001_ha = NULL;
596 struct domain_device *parent_dev = dev->parent;
597 struct pm8001_device *pm8001_device;
598 DECLARE_COMPLETION_ONSTACK(completion);
599 u32 flag = 0;
600 pm8001_ha = pm8001_find_ha_by_dev(dev);
601 spin_lock_irqsave(&pm8001_ha->lock, flags);
602
603 pm8001_device = pm8001_alloc_dev(pm8001_ha);
jack wangdbf9bfe2009-10-14 16:19:21 +0800604 if (!pm8001_device) {
605 res = -1;
606 goto found_out;
607 }
jack wangf01f4e62009-12-07 17:22:55 +0800608 pm8001_device->sas_device = dev;
jack wangdbf9bfe2009-10-14 16:19:21 +0800609 dev->lldd_dev = pm8001_device;
610 pm8001_device->dev_type = dev->dev_type;
611 pm8001_device->dcompletion = &completion;
612 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
613 int phy_id;
614 struct ex_phy *phy;
615 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
616 phy_id++) {
617 phy = &parent_dev->ex_dev.ex_phy[phy_id];
618 if (SAS_ADDR(phy->attached_sas_addr)
619 == SAS_ADDR(dev->sas_addr)) {
620 pm8001_device->attached_phy = phy_id;
621 break;
622 }
623 }
624 if (phy_id == parent_dev->ex_dev.num_phys) {
625 PM8001_FAIL_DBG(pm8001_ha,
626 pm8001_printk("Error: no attached dev:%016llx"
627 " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
628 SAS_ADDR(parent_dev->sas_addr)));
629 res = -1;
630 }
631 } else {
632 if (dev->dev_type == SATA_DEV) {
633 pm8001_device->attached_phy =
634 dev->rphy->identify.phy_identifier;
635 flag = 1; /* directly sata*/
636 }
637 } /*register this device to HBA*/
Mark Salyzyn6fbc7692011-09-26 07:57:36 -0700638 PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device\n"));
jack wangdbf9bfe2009-10-14 16:19:21 +0800639 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
640 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
641 wait_for_completion(&completion);
642 if (dev->dev_type == SAS_END_DEV)
643 msleep(50);
Mark Salyzyn5c4fb762012-01-17 12:56:45 -0500644 pm8001_ha->flags = PM8001F_RUN_TIME;
jack wangdbf9bfe2009-10-14 16:19:21 +0800645 return 0;
646found_out:
647 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
648 return res;
649}
650
651int pm8001_dev_found(struct domain_device *dev)
652{
653 return pm8001_dev_found_notify(dev);
654}
655
jack wangdbf9bfe2009-10-14 16:19:21 +0800656static void pm8001_task_done(struct sas_task *task)
657{
Dan Williamsf0bf7502012-06-21 23:36:30 -0700658 if (!del_timer(&task->slow_task->timer))
jack wangdbf9bfe2009-10-14 16:19:21 +0800659 return;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700660 complete(&task->slow_task->completion);
jack wangdbf9bfe2009-10-14 16:19:21 +0800661}
662
663static void pm8001_tmf_timedout(unsigned long data)
664{
665 struct sas_task *task = (struct sas_task *)data;
666
667 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700668 complete(&task->slow_task->completion);
jack wangdbf9bfe2009-10-14 16:19:21 +0800669}
670
671#define PM8001_TASK_TIMEOUT 20
672/**
jack_wang97ee2082009-11-05 22:33:51 +0800673 * pm8001_exec_internal_tmf_task - execute some task management commands.
jack wangdbf9bfe2009-10-14 16:19:21 +0800674 * @dev: the wanted device.
675 * @tmf: which task management wanted to be take.
676 * @para_len: para_len.
677 * @parameter: ssp task parameter.
jack_wang97ee2082009-11-05 22:33:51 +0800678 *
679 * when errors or exception happened, we may want to do something, for example
680 * abort the issued task which result in this execption, it is done by calling
681 * this function, note it is also with the task execute interface.
jack wangdbf9bfe2009-10-14 16:19:21 +0800682 */
683static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
684 void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
685{
686 int res, retry;
687 struct sas_task *task = NULL;
688 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
689
690 for (retry = 0; retry < 3; retry++) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700691 task = sas_alloc_slow_task(GFP_KERNEL);
jack wangdbf9bfe2009-10-14 16:19:21 +0800692 if (!task)
693 return -ENOMEM;
694
695 task->dev = dev;
696 task->task_proto = dev->tproto;
697 memcpy(&task->ssp_task, parameter, para_len);
698 task->task_done = pm8001_task_done;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700699 task->slow_task->timer.data = (unsigned long)task;
700 task->slow_task->timer.function = pm8001_tmf_timedout;
701 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
702 add_timer(&task->slow_task->timer);
jack wangdbf9bfe2009-10-14 16:19:21 +0800703
704 res = pm8001_task_exec(task, 1, GFP_KERNEL, 1, tmf);
705
706 if (res) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700707 del_timer(&task->slow_task->timer);
jack wangdbf9bfe2009-10-14 16:19:21 +0800708 PM8001_FAIL_DBG(pm8001_ha,
709 pm8001_printk("Executing internal task "
710 "failed\n"));
711 goto ex_err;
712 }
Dan Williamsf0bf7502012-06-21 23:36:30 -0700713 wait_for_completion(&task->slow_task->completion);
jack wangdbf9bfe2009-10-14 16:19:21 +0800714 res = -TMF_RESP_FUNC_FAILED;
715 /* Even TMF timed out, return direct. */
716 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
717 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
718 PM8001_FAIL_DBG(pm8001_ha,
719 pm8001_printk("TMF task[%x]timeout.\n",
720 tmf->tmf));
721 goto ex_err;
722 }
723 }
724
725 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -0500726 task->task_status.stat == SAM_STAT_GOOD) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800727 res = TMF_RESP_FUNC_COMPLETE;
728 break;
729 }
730
731 if (task->task_status.resp == SAS_TASK_COMPLETE &&
732 task->task_status.stat == SAS_DATA_UNDERRUN) {
733 /* no error, but return the number of bytes of
734 * underrun */
735 res = task->task_status.residual;
736 break;
737 }
738
739 if (task->task_status.resp == SAS_TASK_COMPLETE &&
740 task->task_status.stat == SAS_DATA_OVERRUN) {
741 PM8001_FAIL_DBG(pm8001_ha,
742 pm8001_printk("Blocked task error.\n"));
743 res = -EMSGSIZE;
744 break;
745 } else {
jack_wang97ee2082009-11-05 22:33:51 +0800746 PM8001_EH_DBG(pm8001_ha,
747 pm8001_printk(" Task to dev %016llx response:"
748 "0x%x status 0x%x\n",
jack wangdbf9bfe2009-10-14 16:19:21 +0800749 SAS_ADDR(dev->sas_addr),
750 task->task_status.resp,
751 task->task_status.stat));
Dan Williams4fcf8122011-07-29 17:26:39 -0700752 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800753 task = NULL;
754 }
755 }
756ex_err:
757 BUG_ON(retry == 3 && task != NULL);
Dan Williams4fcf8122011-07-29 17:26:39 -0700758 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800759 return res;
760}
761
762static int
763pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
764 struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
765 u32 task_tag)
766{
767 int res, retry;
jack_wang97ee2082009-11-05 22:33:51 +0800768 u32 ccb_tag;
jack wangdbf9bfe2009-10-14 16:19:21 +0800769 struct pm8001_ccb_info *ccb;
770 struct sas_task *task = NULL;
771
772 for (retry = 0; retry < 3; retry++) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700773 task = sas_alloc_slow_task(GFP_KERNEL);
jack wangdbf9bfe2009-10-14 16:19:21 +0800774 if (!task)
775 return -ENOMEM;
776
777 task->dev = dev;
778 task->task_proto = dev->tproto;
779 task->task_done = pm8001_task_done;
Dan Williamsf0bf7502012-06-21 23:36:30 -0700780 task->slow_task->timer.data = (unsigned long)task;
781 task->slow_task->timer.function = pm8001_tmf_timedout;
782 task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
783 add_timer(&task->slow_task->timer);
jack wangdbf9bfe2009-10-14 16:19:21 +0800784
jack_wang97ee2082009-11-05 22:33:51 +0800785 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
786 if (res)
787 return res;
jack wangdbf9bfe2009-10-14 16:19:21 +0800788 ccb = &pm8001_ha->ccb_info[ccb_tag];
789 ccb->device = pm8001_dev;
790 ccb->ccb_tag = ccb_tag;
791 ccb->task = task;
792
793 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
794 pm8001_dev, flag, task_tag, ccb_tag);
795
796 if (res) {
Dan Williamsf0bf7502012-06-21 23:36:30 -0700797 del_timer(&task->slow_task->timer);
jack wangdbf9bfe2009-10-14 16:19:21 +0800798 PM8001_FAIL_DBG(pm8001_ha,
799 pm8001_printk("Executing internal task "
800 "failed\n"));
801 goto ex_err;
802 }
Dan Williamsf0bf7502012-06-21 23:36:30 -0700803 wait_for_completion(&task->slow_task->completion);
jack wangdbf9bfe2009-10-14 16:19:21 +0800804 res = TMF_RESP_FUNC_FAILED;
805 /* Even TMF timed out, return direct. */
806 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
807 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
808 PM8001_FAIL_DBG(pm8001_ha,
809 pm8001_printk("TMF task timeout.\n"));
810 goto ex_err;
811 }
812 }
813
814 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -0500815 task->task_status.stat == SAM_STAT_GOOD) {
jack wangdbf9bfe2009-10-14 16:19:21 +0800816 res = TMF_RESP_FUNC_COMPLETE;
817 break;
818
819 } else {
jack_wang97ee2082009-11-05 22:33:51 +0800820 PM8001_EH_DBG(pm8001_ha,
jack wangdbf9bfe2009-10-14 16:19:21 +0800821 pm8001_printk(" Task to dev %016llx response: "
822 "0x%x status 0x%x\n",
823 SAS_ADDR(dev->sas_addr),
824 task->task_status.resp,
825 task->task_status.stat));
Dan Williams4fcf8122011-07-29 17:26:39 -0700826 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800827 task = NULL;
828 }
829 }
830ex_err:
831 BUG_ON(retry == 3 && task != NULL);
Dan Williams4fcf8122011-07-29 17:26:39 -0700832 sas_free_task(task);
jack wangdbf9bfe2009-10-14 16:19:21 +0800833 return res;
834}
835
836/**
837 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
838 * @dev: the device structure which sas layer used.
839 */
840static void pm8001_dev_gone_notify(struct domain_device *dev)
841{
842 unsigned long flags = 0;
843 u32 tag;
844 struct pm8001_hba_info *pm8001_ha;
845 struct pm8001_device *pm8001_dev = dev->lldd_dev;
Dan Carpenter2471b892010-04-23 14:01:04 +0200846
jack wangdbf9bfe2009-10-14 16:19:21 +0800847 pm8001_ha = pm8001_find_ha_by_dev(dev);
848 spin_lock_irqsave(&pm8001_ha->lock, flags);
849 pm8001_tag_alloc(pm8001_ha, &tag);
850 if (pm8001_dev) {
Dan Carpenter2471b892010-04-23 14:01:04 +0200851 u32 device_id = pm8001_dev->device_id;
852
jack wangdbf9bfe2009-10-14 16:19:21 +0800853 PM8001_DISC_DBG(pm8001_ha,
854 pm8001_printk("found dev[%d:%x] is gone.\n",
855 pm8001_dev->device_id, pm8001_dev->dev_type));
856 if (pm8001_dev->running_req) {
857 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
858 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
859 dev, 1, 0);
860 spin_lock_irqsave(&pm8001_ha->lock, flags);
861 }
862 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
863 pm8001_free_dev(pm8001_dev);
864 } else {
865 PM8001_DISC_DBG(pm8001_ha,
866 pm8001_printk("Found dev has gone.\n"));
867 }
868 dev->lldd_dev = NULL;
869 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
870}
871
872void pm8001_dev_gone(struct domain_device *dev)
873{
874 pm8001_dev_gone_notify(dev);
875}
876
877static int pm8001_issue_ssp_tmf(struct domain_device *dev,
878 u8 *lun, struct pm8001_tmf_task *tmf)
879{
880 struct sas_ssp_task ssp_task;
881 if (!(dev->tproto & SAS_PROTOCOL_SSP))
882 return TMF_RESP_FUNC_ESUPP;
883
884 strncpy((u8 *)&ssp_task.LUN, lun, 8);
885 return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
886 tmf);
887}
888
Mark Salyzyn5954d732012-01-17 11:52:24 -0500889/* retry commands by ha, by task and/or by device */
890void pm8001_open_reject_retry(
891 struct pm8001_hba_info *pm8001_ha,
892 struct sas_task *task_to_close,
893 struct pm8001_device *device_to_close)
894{
895 int i;
896 unsigned long flags;
897
898 if (pm8001_ha == NULL)
899 return;
900
901 spin_lock_irqsave(&pm8001_ha->lock, flags);
902
903 for (i = 0; i < PM8001_MAX_CCB; i++) {
904 struct sas_task *task;
905 struct task_status_struct *ts;
906 struct pm8001_device *pm8001_dev;
907 unsigned long flags1;
908 u32 tag;
909 struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i];
910
911 pm8001_dev = ccb->device;
912 if (!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE))
913 continue;
914 if (!device_to_close) {
915 uintptr_t d = (uintptr_t)pm8001_dev
916 - (uintptr_t)&pm8001_ha->devices;
917 if (((d % sizeof(*pm8001_dev)) != 0)
918 || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES))
919 continue;
920 } else if (pm8001_dev != device_to_close)
921 continue;
922 tag = ccb->ccb_tag;
923 if (!tag || (tag == 0xFFFFFFFF))
924 continue;
925 task = ccb->task;
926 if (!task || !task->task_done)
927 continue;
928 if (task_to_close && (task != task_to_close))
929 continue;
930 ts = &task->task_status;
931 ts->resp = SAS_TASK_COMPLETE;
932 /* Force the midlayer to retry */
933 ts->stat = SAS_OPEN_REJECT;
934 ts->open_rej_reason = SAS_OREJ_RSVD_RETRY;
935 if (pm8001_dev)
936 pm8001_dev->running_req--;
937 spin_lock_irqsave(&task->task_state_lock, flags1);
938 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
939 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
940 task->task_state_flags |= SAS_TASK_STATE_DONE;
941 if (unlikely((task->task_state_flags
942 & SAS_TASK_STATE_ABORTED))) {
943 spin_unlock_irqrestore(&task->task_state_lock,
944 flags1);
945 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
946 } else {
947 spin_unlock_irqrestore(&task->task_state_lock,
948 flags1);
949 pm8001_ccb_task_free(pm8001_ha, task, ccb, tag);
950 mb();/* in order to force CPU ordering */
951 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
952 task->task_done(task);
953 spin_lock_irqsave(&pm8001_ha->lock, flags);
954 }
955 }
956
957 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
958}
959
jack wangdbf9bfe2009-10-14 16:19:21 +0800960/**
961 * Standard mandates link reset for ATA (type 0) and hard reset for
962 * SSP (type 1) , only for RECOVERY
963 */
964int pm8001_I_T_nexus_reset(struct domain_device *dev)
965{
966 int rc = TMF_RESP_FUNC_FAILED;
967 struct pm8001_device *pm8001_dev;
968 struct pm8001_hba_info *pm8001_ha;
969 struct sas_phy *phy;
Dan Williamse7db8222012-06-21 23:30:58 -0700970
jack wangdbf9bfe2009-10-14 16:19:21 +0800971 if (!dev || !dev->lldd_dev)
Dan Williamse7db8222012-06-21 23:30:58 -0700972 return -ENODEV;
jack wangdbf9bfe2009-10-14 16:19:21 +0800973
974 pm8001_dev = dev->lldd_dev;
975 pm8001_ha = pm8001_find_ha_by_dev(dev);
Dan Williamsf41a0c42011-12-21 21:33:17 -0800976 phy = sas_get_local_phy(dev);
jack wangdbf9bfe2009-10-14 16:19:21 +0800977
978 if (dev_is_sata(dev)) {
979 DECLARE_COMPLETION_ONSTACK(completion_setstate);
Dan Williamsf41a0c42011-12-21 21:33:17 -0800980 if (scsi_is_sas_phy_local(phy)) {
981 rc = 0;
982 goto out;
983 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800984 rc = sas_phy_reset(phy, 1);
985 msleep(2000);
986 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
987 dev, 1, 0);
988 pm8001_dev->setds_completion = &completion_setstate;
989 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
990 pm8001_dev, 0x01);
991 wait_for_completion(&completion_setstate);
Dan Williamsf41a0c42011-12-21 21:33:17 -0800992 } else {
993 rc = sas_phy_reset(phy, 1);
994 msleep(2000);
jack wangdbf9bfe2009-10-14 16:19:21 +0800995 }
996 PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
997 pm8001_dev->device_id, rc));
Dan Williamsf41a0c42011-12-21 21:33:17 -0800998 out:
999 sas_put_local_phy(phy);
jack wangdbf9bfe2009-10-14 16:19:21 +08001000 return rc;
1001}
1002
1003/* mandatory SAM-3, the task reset the specified LUN*/
1004int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
1005{
1006 int rc = TMF_RESP_FUNC_FAILED;
1007 struct pm8001_tmf_task tmf_task;
1008 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1009 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1010 if (dev_is_sata(dev)) {
Dan Williamsf41a0c42011-12-21 21:33:17 -08001011 struct sas_phy *phy = sas_get_local_phy(dev);
jack wangdbf9bfe2009-10-14 16:19:21 +08001012 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
1013 dev, 1, 0);
1014 rc = sas_phy_reset(phy, 1);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001015 sas_put_local_phy(phy);
jack wangdbf9bfe2009-10-14 16:19:21 +08001016 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
1017 pm8001_dev, 0x01);
1018 msleep(2000);
1019 } else {
1020 tmf_task.tmf = TMF_LU_RESET;
1021 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1022 }
1023 /* If failed, fall-through I_T_Nexus reset */
1024 PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
1025 pm8001_dev->device_id, rc));
1026 return rc;
1027}
1028
1029/* optional SAM-3 */
1030int pm8001_query_task(struct sas_task *task)
1031{
1032 u32 tag = 0xdeadbeef;
1033 int i = 0;
1034 struct scsi_lun lun;
1035 struct pm8001_tmf_task tmf_task;
1036 int rc = TMF_RESP_FUNC_FAILED;
1037 if (unlikely(!task || !task->lldd_task || !task->dev))
1038 return rc;
1039
1040 if (task->task_proto & SAS_PROTOCOL_SSP) {
1041 struct scsi_cmnd *cmnd = task->uldd_task;
1042 struct domain_device *dev = task->dev;
1043 struct pm8001_hba_info *pm8001_ha =
1044 pm8001_find_ha_by_dev(dev);
1045
1046 int_to_scsilun(cmnd->device->lun, &lun);
1047 rc = pm8001_find_tag(task, &tag);
1048 if (rc == 0) {
1049 rc = TMF_RESP_FUNC_FAILED;
1050 return rc;
1051 }
1052 PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
1053 for (i = 0; i < 16; i++)
1054 printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
1055 printk(KERN_INFO "]\n");
1056 tmf_task.tmf = TMF_QUERY_TASK;
1057 tmf_task.tag_of_task_to_be_managed = tag;
1058
1059 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1060 switch (rc) {
1061 /* The task is still in Lun, release it then */
1062 case TMF_RESP_FUNC_SUCC:
1063 PM8001_EH_DBG(pm8001_ha,
Mark Salyzyn6fbc7692011-09-26 07:57:36 -07001064 pm8001_printk("The task is still in Lun\n"));
1065 break;
jack wangdbf9bfe2009-10-14 16:19:21 +08001066 /* The task is not in Lun or failed, reset the phy */
1067 case TMF_RESP_FUNC_FAILED:
1068 case TMF_RESP_FUNC_COMPLETE:
1069 PM8001_EH_DBG(pm8001_ha,
1070 pm8001_printk("The task is not in Lun or failed,"
Mark Salyzyn6fbc7692011-09-26 07:57:36 -07001071 " reset the phy\n"));
jack wangdbf9bfe2009-10-14 16:19:21 +08001072 break;
1073 }
1074 }
1075 pm8001_printk(":rc= %d\n", rc);
1076 return rc;
1077}
1078
1079/* mandatory SAM-3, still need free task/ccb info, abord the specified task */
1080int pm8001_abort_task(struct sas_task *task)
1081{
1082 unsigned long flags;
1083 u32 tag = 0xdeadbeef;
1084 u32 device_id;
1085 struct domain_device *dev ;
1086 struct pm8001_hba_info *pm8001_ha = NULL;
1087 struct pm8001_ccb_info *ccb;
1088 struct scsi_lun lun;
1089 struct pm8001_device *pm8001_dev;
1090 struct pm8001_tmf_task tmf_task;
1091 int rc = TMF_RESP_FUNC_FAILED;
1092 if (unlikely(!task || !task->lldd_task || !task->dev))
1093 return rc;
1094 spin_lock_irqsave(&task->task_state_lock, flags);
1095 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1096 spin_unlock_irqrestore(&task->task_state_lock, flags);
1097 rc = TMF_RESP_FUNC_COMPLETE;
1098 goto out;
1099 }
1100 spin_unlock_irqrestore(&task->task_state_lock, flags);
1101 if (task->task_proto & SAS_PROTOCOL_SSP) {
1102 struct scsi_cmnd *cmnd = task->uldd_task;
1103 dev = task->dev;
1104 ccb = task->lldd_task;
1105 pm8001_dev = dev->lldd_dev;
1106 pm8001_ha = pm8001_find_ha_by_dev(dev);
1107 int_to_scsilun(cmnd->device->lun, &lun);
1108 rc = pm8001_find_tag(task, &tag);
1109 if (rc == 0) {
1110 printk(KERN_INFO "No such tag in %s\n", __func__);
1111 rc = TMF_RESP_FUNC_FAILED;
1112 return rc;
1113 }
1114 device_id = pm8001_dev->device_id;
1115 PM8001_EH_DBG(pm8001_ha,
jack_wang97ee2082009-11-05 22:33:51 +08001116 pm8001_printk("abort io to deviceid= %d\n", device_id));
1117 tmf_task.tmf = TMF_ABORT_TASK;
jack wangdbf9bfe2009-10-14 16:19:21 +08001118 tmf_task.tag_of_task_to_be_managed = tag;
1119 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
jack_wang97ee2082009-11-05 22:33:51 +08001120 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
jack wangdbf9bfe2009-10-14 16:19:21 +08001121 pm8001_dev->sas_device, 0, tag);
1122 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1123 task->task_proto & SAS_PROTOCOL_STP) {
1124 dev = task->dev;
1125 pm8001_dev = dev->lldd_dev;
1126 pm8001_ha = pm8001_find_ha_by_dev(dev);
1127 rc = pm8001_find_tag(task, &tag);
1128 if (rc == 0) {
1129 printk(KERN_INFO "No such tag in %s\n", __func__);
1130 rc = TMF_RESP_FUNC_FAILED;
1131 return rc;
1132 }
1133 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1134 pm8001_dev->sas_device, 0, tag);
1135 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1136 /* SMP */
1137 dev = task->dev;
1138 pm8001_dev = dev->lldd_dev;
1139 pm8001_ha = pm8001_find_ha_by_dev(dev);
1140 rc = pm8001_find_tag(task, &tag);
1141 if (rc == 0) {
1142 printk(KERN_INFO "No such tag in %s\n", __func__);
1143 rc = TMF_RESP_FUNC_FAILED;
1144 return rc;
1145 }
1146 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1147 pm8001_dev->sas_device, 0, tag);
1148
1149 }
1150out:
1151 if (rc != TMF_RESP_FUNC_COMPLETE)
1152 pm8001_printk("rc= %d\n", rc);
1153 return rc;
1154}
1155
1156int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1157{
1158 int rc = TMF_RESP_FUNC_FAILED;
1159 struct pm8001_tmf_task tmf_task;
1160
1161 tmf_task.tmf = TMF_ABORT_TASK_SET;
1162 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1163 return rc;
1164}
1165
1166int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1167{
1168 int rc = TMF_RESP_FUNC_FAILED;
1169 struct pm8001_tmf_task tmf_task;
1170
1171 tmf_task.tmf = TMF_CLEAR_ACA;
1172 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1173
1174 return rc;
1175}
1176
1177int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1178{
1179 int rc = TMF_RESP_FUNC_FAILED;
1180 struct pm8001_tmf_task tmf_task;
1181 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1182 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1183
1184 PM8001_EH_DBG(pm8001_ha,
1185 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1186 pm8001_dev->device_id));
1187 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1188 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1189 return rc;
1190}
1191