blob: 1f767a0e727a9b46d5fc4e073576c1422e3b9eea [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/**
jack_wang97ee2082009-11-05 22:33:51 +0800333 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
jack wangdbf9bfe2009-10-14 16:19:21 +0800334 * @task: the task to be execute.
335 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
336 * we always execute one one time.
337 * @gfp_flags: gfp_flags.
jack_wang97ee2082009-11-05 22:33:51 +0800338 * @is_tmf: if it is task management task.
jack wangdbf9bfe2009-10-14 16:19:21 +0800339 * @tmf: the task management IU
340 */
341#define DEV_IS_GONE(pm8001_dev) \
342 ((!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE)))
343static int pm8001_task_exec(struct sas_task *task, const int num,
344 gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
345{
346 struct domain_device *dev = task->dev;
347 struct pm8001_hba_info *pm8001_ha;
348 struct pm8001_device *pm8001_dev;
349 struct sas_task *t = task;
350 struct pm8001_ccb_info *ccb;
351 u32 tag = 0xdeadbeef, rc, n_elem = 0;
352 u32 n = num;
353 unsigned long flags = 0;
354
355 if (!dev->port) {
356 struct task_status_struct *tsm = &t->task_status;
357 tsm->resp = SAS_TASK_UNDELIVERED;
358 tsm->stat = SAS_PHY_DOWN;
359 if (dev->dev_type != SATA_DEV)
360 t->task_done(t);
361 return 0;
362 }
363 pm8001_ha = pm8001_find_ha_by_dev(task->dev);
364 PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n "));
365 spin_lock_irqsave(&pm8001_ha->lock, flags);
366 do {
367 dev = t->dev;
368 pm8001_dev = dev->lldd_dev;
369 if (DEV_IS_GONE(pm8001_dev)) {
370 if (pm8001_dev) {
371 PM8001_IO_DBG(pm8001_ha,
372 pm8001_printk("device %d not ready.\n",
373 pm8001_dev->device_id));
374 } else {
375 PM8001_IO_DBG(pm8001_ha,
376 pm8001_printk("device %016llx not "
377 "ready.\n", SAS_ADDR(dev->sas_addr)));
378 }
jack_wang97ee2082009-11-05 22:33:51 +0800379 rc = SAS_PHY_DOWN;
jack wangdbf9bfe2009-10-14 16:19:21 +0800380 goto out_done;
381 }
382 rc = pm8001_tag_alloc(pm8001_ha, &tag);
383 if (rc)
384 goto err_out;
385 ccb = &pm8001_ha->ccb_info[tag];
386
387 if (!sas_protocol_ata(t->task_proto)) {
388 if (t->num_scatter) {
389 n_elem = dma_map_sg(pm8001_ha->dev,
390 t->scatter,
391 t->num_scatter,
392 t->data_dir);
393 if (!n_elem) {
394 rc = -ENOMEM;
jack_wang97ee2082009-11-05 22:33:51 +0800395 goto err_out_tag;
jack wangdbf9bfe2009-10-14 16:19:21 +0800396 }
397 }
398 } else {
399 n_elem = t->num_scatter;
400 }
401
jack_wang97ee2082009-11-05 22:33:51 +0800402 t->lldd_task = ccb;
jack wangdbf9bfe2009-10-14 16:19:21 +0800403 ccb->n_elem = n_elem;
404 ccb->ccb_tag = tag;
405 ccb->task = t;
406 switch (t->task_proto) {
407 case SAS_PROTOCOL_SMP:
408 rc = pm8001_task_prep_smp(pm8001_ha, ccb);
409 break;
410 case SAS_PROTOCOL_SSP:
411 if (is_tmf)
412 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
413 ccb, tmf);
414 else
415 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
416 break;
417 case SAS_PROTOCOL_SATA:
418 case SAS_PROTOCOL_STP:
419 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
420 rc = pm8001_task_prep_ata(pm8001_ha, ccb);
421 break;
422 default:
423 dev_printk(KERN_ERR, pm8001_ha->dev,
424 "unknown sas_task proto: 0x%x\n",
425 t->task_proto);
426 rc = -EINVAL;
427 break;
428 }
429
430 if (rc) {
431 PM8001_IO_DBG(pm8001_ha,
432 pm8001_printk("rc is %x\n", rc));
433 goto err_out_tag;
434 }
jack wangdbf9bfe2009-10-14 16:19:21 +0800435 /* TODO: select normal or high priority */
436 spin_lock(&t->task_state_lock);
437 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
438 spin_unlock(&t->task_state_lock);
439 pm8001_dev->running_req++;
440 if (n > 1)
441 t = list_entry(t->list.next, struct sas_task, list);
442 } while (--n);
443 rc = 0;
444 goto out_done;
445
446err_out_tag:
447 pm8001_tag_free(pm8001_ha, tag);
448err_out:
449 dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
450 if (!sas_protocol_ata(t->task_proto))
451 if (n_elem)
452 dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem,
453 t->data_dir);
454out_done:
455 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
456 return rc;
457}
458
459/**
460 * pm8001_queue_command - register for upper layer used, all IO commands sent
461 * to HBA are from this interface.
462 * @task: the task to be execute.
463 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
464 * we always execute one one time
465 * @gfp_flags: gfp_flags
466 */
467int pm8001_queue_command(struct sas_task *task, const int num,
468 gfp_t gfp_flags)
469{
470 return pm8001_task_exec(task, num, gfp_flags, 0, NULL);
471}
472
473void pm8001_ccb_free(struct pm8001_hba_info *pm8001_ha, u32 ccb_idx)
474{
475 pm8001_tag_clear(pm8001_ha, ccb_idx);
476}
477
478/**
479 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
480 * @pm8001_ha: our hba card information
481 * @ccb: the ccb which attached to ssp task
482 * @task: the task to be free.
483 * @ccb_idx: ccb index.
484 */
485void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
486 struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
487{
488 if (!ccb->task)
489 return;
490 if (!sas_protocol_ata(task->task_proto))
491 if (ccb->n_elem)
492 dma_unmap_sg(pm8001_ha->dev, task->scatter,
493 task->num_scatter, task->data_dir);
494
495 switch (task->task_proto) {
496 case SAS_PROTOCOL_SMP:
497 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
498 PCI_DMA_FROMDEVICE);
499 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
500 PCI_DMA_TODEVICE);
501 break;
502
503 case SAS_PROTOCOL_SATA:
504 case SAS_PROTOCOL_STP:
505 case SAS_PROTOCOL_SSP:
506 default:
507 /* do nothing */
508 break;
509 }
510 task->lldd_task = NULL;
511 ccb->task = NULL;
512 ccb->ccb_tag = 0xFFFFFFFF;
513 pm8001_ccb_free(pm8001_ha, ccb_idx);
514}
515
516 /**
jack_wang97ee2082009-11-05 22:33:51 +0800517 * pm8001_alloc_dev - find a empty pm8001_device
jack wangdbf9bfe2009-10-14 16:19:21 +0800518 * @pm8001_ha: our hba card information
519 */
520struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
521{
522 u32 dev;
523 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
524 if (pm8001_ha->devices[dev].dev_type == NO_DEVICE) {
525 pm8001_ha->devices[dev].id = dev;
526 return &pm8001_ha->devices[dev];
527 }
528 }
529 if (dev == PM8001_MAX_DEVICES) {
530 PM8001_FAIL_DBG(pm8001_ha,
531 pm8001_printk("max support %d devices, ignore ..\n",
532 PM8001_MAX_DEVICES));
533 }
534 return NULL;
535}
536
537static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
538{
539 u32 id = pm8001_dev->id;
540 memset(pm8001_dev, 0, sizeof(*pm8001_dev));
541 pm8001_dev->id = id;
542 pm8001_dev->dev_type = NO_DEVICE;
543 pm8001_dev->device_id = PM8001_MAX_DEVICES;
544 pm8001_dev->sas_device = NULL;
545}
546
547/**
jack_wang97ee2082009-11-05 22:33:51 +0800548 * pm8001_dev_found_notify - libsas notify a device is found.
549 * @dev: the device structure which sas layer used.
550 *
551 * when libsas find a sas domain device, it should tell the LLDD that
552 * device is found, and then LLDD register this device to HBA firmware
553 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
554 * device ID(according to device's sas address) and returned it to LLDD. From
jack wangdbf9bfe2009-10-14 16:19:21 +0800555 * now on, we communicate with HBA FW with the device ID which HBA assigned
556 * rather than sas address. it is the neccessary step for our HBA but it is
557 * the optional for other HBA driver.
jack wangdbf9bfe2009-10-14 16:19:21 +0800558 */
559static int pm8001_dev_found_notify(struct domain_device *dev)
560{
561 unsigned long flags = 0;
562 int res = 0;
563 struct pm8001_hba_info *pm8001_ha = NULL;
564 struct domain_device *parent_dev = dev->parent;
565 struct pm8001_device *pm8001_device;
566 DECLARE_COMPLETION_ONSTACK(completion);
567 u32 flag = 0;
568 pm8001_ha = pm8001_find_ha_by_dev(dev);
569 spin_lock_irqsave(&pm8001_ha->lock, flags);
570
571 pm8001_device = pm8001_alloc_dev(pm8001_ha);
572 pm8001_device->sas_device = dev;
573 if (!pm8001_device) {
574 res = -1;
575 goto found_out;
576 }
577 dev->lldd_dev = pm8001_device;
578 pm8001_device->dev_type = dev->dev_type;
579 pm8001_device->dcompletion = &completion;
580 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
581 int phy_id;
582 struct ex_phy *phy;
583 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
584 phy_id++) {
585 phy = &parent_dev->ex_dev.ex_phy[phy_id];
586 if (SAS_ADDR(phy->attached_sas_addr)
587 == SAS_ADDR(dev->sas_addr)) {
588 pm8001_device->attached_phy = phy_id;
589 break;
590 }
591 }
592 if (phy_id == parent_dev->ex_dev.num_phys) {
593 PM8001_FAIL_DBG(pm8001_ha,
594 pm8001_printk("Error: no attached dev:%016llx"
595 " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
596 SAS_ADDR(parent_dev->sas_addr)));
597 res = -1;
598 }
599 } else {
600 if (dev->dev_type == SATA_DEV) {
601 pm8001_device->attached_phy =
602 dev->rphy->identify.phy_identifier;
603 flag = 1; /* directly sata*/
604 }
605 } /*register this device to HBA*/
606 PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device \n"));
607 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
608 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
609 wait_for_completion(&completion);
610 if (dev->dev_type == SAS_END_DEV)
611 msleep(50);
612 pm8001_ha->flags = PM8001F_RUN_TIME ;
613 return 0;
614found_out:
615 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
616 return res;
617}
618
619int pm8001_dev_found(struct domain_device *dev)
620{
621 return pm8001_dev_found_notify(dev);
622}
623
624/**
625 * pm8001_alloc_task - allocate a task structure for TMF
626 */
627static struct sas_task *pm8001_alloc_task(void)
628{
629 struct sas_task *task = kzalloc(sizeof(*task), GFP_KERNEL);
630 if (task) {
631 INIT_LIST_HEAD(&task->list);
632 spin_lock_init(&task->task_state_lock);
633 task->task_state_flags = SAS_TASK_STATE_PENDING;
634 init_timer(&task->timer);
635 init_completion(&task->completion);
636 }
637 return task;
638}
639
640static void pm8001_free_task(struct sas_task *task)
641{
642 if (task) {
643 BUG_ON(!list_empty(&task->list));
644 kfree(task);
645 }
646}
647
648static void pm8001_task_done(struct sas_task *task)
649{
650 if (!del_timer(&task->timer))
651 return;
652 complete(&task->completion);
653}
654
655static void pm8001_tmf_timedout(unsigned long data)
656{
657 struct sas_task *task = (struct sas_task *)data;
658
659 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
660 complete(&task->completion);
661}
662
663#define PM8001_TASK_TIMEOUT 20
664/**
jack_wang97ee2082009-11-05 22:33:51 +0800665 * pm8001_exec_internal_tmf_task - execute some task management commands.
jack wangdbf9bfe2009-10-14 16:19:21 +0800666 * @dev: the wanted device.
667 * @tmf: which task management wanted to be take.
668 * @para_len: para_len.
669 * @parameter: ssp task parameter.
jack_wang97ee2082009-11-05 22:33:51 +0800670 *
671 * when errors or exception happened, we may want to do something, for example
672 * abort the issued task which result in this execption, it is done by calling
673 * this function, note it is also with the task execute interface.
jack wangdbf9bfe2009-10-14 16:19:21 +0800674 */
675static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
676 void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
677{
678 int res, retry;
679 struct sas_task *task = NULL;
680 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
681
682 for (retry = 0; retry < 3; retry++) {
683 task = pm8001_alloc_task();
684 if (!task)
685 return -ENOMEM;
686
687 task->dev = dev;
688 task->task_proto = dev->tproto;
689 memcpy(&task->ssp_task, parameter, para_len);
690 task->task_done = pm8001_task_done;
691 task->timer.data = (unsigned long)task;
692 task->timer.function = pm8001_tmf_timedout;
693 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
694 add_timer(&task->timer);
695
696 res = pm8001_task_exec(task, 1, GFP_KERNEL, 1, tmf);
697
698 if (res) {
699 del_timer(&task->timer);
700 PM8001_FAIL_DBG(pm8001_ha,
701 pm8001_printk("Executing internal task "
702 "failed\n"));
703 goto ex_err;
704 }
705 wait_for_completion(&task->completion);
706 res = -TMF_RESP_FUNC_FAILED;
707 /* Even TMF timed out, return direct. */
708 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
709 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
710 PM8001_FAIL_DBG(pm8001_ha,
711 pm8001_printk("TMF task[%x]timeout.\n",
712 tmf->tmf));
713 goto ex_err;
714 }
715 }
716
717 if (task->task_status.resp == SAS_TASK_COMPLETE &&
718 task->task_status.stat == SAM_GOOD) {
719 res = TMF_RESP_FUNC_COMPLETE;
720 break;
721 }
722
723 if (task->task_status.resp == SAS_TASK_COMPLETE &&
724 task->task_status.stat == SAS_DATA_UNDERRUN) {
725 /* no error, but return the number of bytes of
726 * underrun */
727 res = task->task_status.residual;
728 break;
729 }
730
731 if (task->task_status.resp == SAS_TASK_COMPLETE &&
732 task->task_status.stat == SAS_DATA_OVERRUN) {
733 PM8001_FAIL_DBG(pm8001_ha,
734 pm8001_printk("Blocked task error.\n"));
735 res = -EMSGSIZE;
736 break;
737 } else {
jack_wang97ee2082009-11-05 22:33:51 +0800738 PM8001_EH_DBG(pm8001_ha,
739 pm8001_printk(" Task to dev %016llx response:"
740 "0x%x status 0x%x\n",
jack wangdbf9bfe2009-10-14 16:19:21 +0800741 SAS_ADDR(dev->sas_addr),
742 task->task_status.resp,
743 task->task_status.stat));
744 pm8001_free_task(task);
745 task = NULL;
746 }
747 }
748ex_err:
749 BUG_ON(retry == 3 && task != NULL);
750 if (task != NULL)
751 pm8001_free_task(task);
752 return res;
753}
754
755static int
756pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
757 struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
758 u32 task_tag)
759{
760 int res, retry;
jack_wang97ee2082009-11-05 22:33:51 +0800761 u32 ccb_tag;
jack wangdbf9bfe2009-10-14 16:19:21 +0800762 struct pm8001_ccb_info *ccb;
763 struct sas_task *task = NULL;
764
765 for (retry = 0; retry < 3; retry++) {
766 task = pm8001_alloc_task();
767 if (!task)
768 return -ENOMEM;
769
770 task->dev = dev;
771 task->task_proto = dev->tproto;
772 task->task_done = pm8001_task_done;
773 task->timer.data = (unsigned long)task;
774 task->timer.function = pm8001_tmf_timedout;
775 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
776 add_timer(&task->timer);
777
jack_wang97ee2082009-11-05 22:33:51 +0800778 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
779 if (res)
780 return res;
jack wangdbf9bfe2009-10-14 16:19:21 +0800781 ccb = &pm8001_ha->ccb_info[ccb_tag];
782 ccb->device = pm8001_dev;
783 ccb->ccb_tag = ccb_tag;
784 ccb->task = task;
785
786 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
787 pm8001_dev, flag, task_tag, ccb_tag);
788
789 if (res) {
790 del_timer(&task->timer);
791 PM8001_FAIL_DBG(pm8001_ha,
792 pm8001_printk("Executing internal task "
793 "failed\n"));
794 goto ex_err;
795 }
796 wait_for_completion(&task->completion);
797 res = TMF_RESP_FUNC_FAILED;
798 /* Even TMF timed out, return direct. */
799 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
800 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
801 PM8001_FAIL_DBG(pm8001_ha,
802 pm8001_printk("TMF task timeout.\n"));
803 goto ex_err;
804 }
805 }
806
807 if (task->task_status.resp == SAS_TASK_COMPLETE &&
808 task->task_status.stat == SAM_GOOD) {
809 res = TMF_RESP_FUNC_COMPLETE;
810 break;
811
812 } else {
jack_wang97ee2082009-11-05 22:33:51 +0800813 PM8001_EH_DBG(pm8001_ha,
jack wangdbf9bfe2009-10-14 16:19:21 +0800814 pm8001_printk(" Task to dev %016llx response: "
815 "0x%x status 0x%x\n",
816 SAS_ADDR(dev->sas_addr),
817 task->task_status.resp,
818 task->task_status.stat));
819 pm8001_free_task(task);
820 task = NULL;
821 }
822 }
823ex_err:
824 BUG_ON(retry == 3 && task != NULL);
825 if (task != NULL)
826 pm8001_free_task(task);
827 return res;
828}
829
830/**
831 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
832 * @dev: the device structure which sas layer used.
833 */
834static void pm8001_dev_gone_notify(struct domain_device *dev)
835{
836 unsigned long flags = 0;
837 u32 tag;
838 struct pm8001_hba_info *pm8001_ha;
839 struct pm8001_device *pm8001_dev = dev->lldd_dev;
840 u32 device_id = pm8001_dev->device_id;
841 pm8001_ha = pm8001_find_ha_by_dev(dev);
842 spin_lock_irqsave(&pm8001_ha->lock, flags);
843 pm8001_tag_alloc(pm8001_ha, &tag);
844 if (pm8001_dev) {
845 PM8001_DISC_DBG(pm8001_ha,
846 pm8001_printk("found dev[%d:%x] is gone.\n",
847 pm8001_dev->device_id, pm8001_dev->dev_type));
848 if (pm8001_dev->running_req) {
849 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
850 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
851 dev, 1, 0);
852 spin_lock_irqsave(&pm8001_ha->lock, flags);
853 }
854 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
855 pm8001_free_dev(pm8001_dev);
856 } else {
857 PM8001_DISC_DBG(pm8001_ha,
858 pm8001_printk("Found dev has gone.\n"));
859 }
860 dev->lldd_dev = NULL;
861 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
862}
863
864void pm8001_dev_gone(struct domain_device *dev)
865{
866 pm8001_dev_gone_notify(dev);
867}
868
869static int pm8001_issue_ssp_tmf(struct domain_device *dev,
870 u8 *lun, struct pm8001_tmf_task *tmf)
871{
872 struct sas_ssp_task ssp_task;
873 if (!(dev->tproto & SAS_PROTOCOL_SSP))
874 return TMF_RESP_FUNC_ESUPP;
875
876 strncpy((u8 *)&ssp_task.LUN, lun, 8);
877 return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
878 tmf);
879}
880
881/**
882 * Standard mandates link reset for ATA (type 0) and hard reset for
883 * SSP (type 1) , only for RECOVERY
884 */
885int pm8001_I_T_nexus_reset(struct domain_device *dev)
886{
887 int rc = TMF_RESP_FUNC_FAILED;
888 struct pm8001_device *pm8001_dev;
889 struct pm8001_hba_info *pm8001_ha;
890 struct sas_phy *phy;
891 if (!dev || !dev->lldd_dev)
892 return -1;
893
894 pm8001_dev = dev->lldd_dev;
895 pm8001_ha = pm8001_find_ha_by_dev(dev);
896 phy = sas_find_local_phy(dev);
897
898 if (dev_is_sata(dev)) {
899 DECLARE_COMPLETION_ONSTACK(completion_setstate);
900 rc = sas_phy_reset(phy, 1);
901 msleep(2000);
902 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
903 dev, 1, 0);
904 pm8001_dev->setds_completion = &completion_setstate;
905 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
906 pm8001_dev, 0x01);
907 wait_for_completion(&completion_setstate);
908 } else{
909 rc = sas_phy_reset(phy, 1);
910 msleep(2000);
911 }
912 PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
913 pm8001_dev->device_id, rc));
914 return rc;
915}
916
917/* mandatory SAM-3, the task reset the specified LUN*/
918int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
919{
920 int rc = TMF_RESP_FUNC_FAILED;
921 struct pm8001_tmf_task tmf_task;
922 struct pm8001_device *pm8001_dev = dev->lldd_dev;
923 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
924 if (dev_is_sata(dev)) {
925 struct sas_phy *phy = sas_find_local_phy(dev);
926 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
927 dev, 1, 0);
928 rc = sas_phy_reset(phy, 1);
929 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
930 pm8001_dev, 0x01);
931 msleep(2000);
932 } else {
933 tmf_task.tmf = TMF_LU_RESET;
934 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
935 }
936 /* If failed, fall-through I_T_Nexus reset */
937 PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
938 pm8001_dev->device_id, rc));
939 return rc;
940}
941
942/* optional SAM-3 */
943int pm8001_query_task(struct sas_task *task)
944{
945 u32 tag = 0xdeadbeef;
946 int i = 0;
947 struct scsi_lun lun;
948 struct pm8001_tmf_task tmf_task;
949 int rc = TMF_RESP_FUNC_FAILED;
950 if (unlikely(!task || !task->lldd_task || !task->dev))
951 return rc;
952
953 if (task->task_proto & SAS_PROTOCOL_SSP) {
954 struct scsi_cmnd *cmnd = task->uldd_task;
955 struct domain_device *dev = task->dev;
956 struct pm8001_hba_info *pm8001_ha =
957 pm8001_find_ha_by_dev(dev);
958
959 int_to_scsilun(cmnd->device->lun, &lun);
960 rc = pm8001_find_tag(task, &tag);
961 if (rc == 0) {
962 rc = TMF_RESP_FUNC_FAILED;
963 return rc;
964 }
965 PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
966 for (i = 0; i < 16; i++)
967 printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
968 printk(KERN_INFO "]\n");
969 tmf_task.tmf = TMF_QUERY_TASK;
970 tmf_task.tag_of_task_to_be_managed = tag;
971
972 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
973 switch (rc) {
974 /* The task is still in Lun, release it then */
975 case TMF_RESP_FUNC_SUCC:
976 PM8001_EH_DBG(pm8001_ha,
977 pm8001_printk("The task is still in Lun \n"));
978 /* The task is not in Lun or failed, reset the phy */
979 case TMF_RESP_FUNC_FAILED:
980 case TMF_RESP_FUNC_COMPLETE:
981 PM8001_EH_DBG(pm8001_ha,
982 pm8001_printk("The task is not in Lun or failed,"
983 " reset the phy \n"));
984 break;
985 }
986 }
987 pm8001_printk(":rc= %d\n", rc);
988 return rc;
989}
990
991/* mandatory SAM-3, still need free task/ccb info, abord the specified task */
992int pm8001_abort_task(struct sas_task *task)
993{
994 unsigned long flags;
995 u32 tag = 0xdeadbeef;
996 u32 device_id;
997 struct domain_device *dev ;
998 struct pm8001_hba_info *pm8001_ha = NULL;
999 struct pm8001_ccb_info *ccb;
1000 struct scsi_lun lun;
1001 struct pm8001_device *pm8001_dev;
1002 struct pm8001_tmf_task tmf_task;
1003 int rc = TMF_RESP_FUNC_FAILED;
1004 if (unlikely(!task || !task->lldd_task || !task->dev))
1005 return rc;
1006 spin_lock_irqsave(&task->task_state_lock, flags);
1007 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1008 spin_unlock_irqrestore(&task->task_state_lock, flags);
1009 rc = TMF_RESP_FUNC_COMPLETE;
1010 goto out;
1011 }
1012 spin_unlock_irqrestore(&task->task_state_lock, flags);
1013 if (task->task_proto & SAS_PROTOCOL_SSP) {
1014 struct scsi_cmnd *cmnd = task->uldd_task;
1015 dev = task->dev;
1016 ccb = task->lldd_task;
1017 pm8001_dev = dev->lldd_dev;
1018 pm8001_ha = pm8001_find_ha_by_dev(dev);
1019 int_to_scsilun(cmnd->device->lun, &lun);
1020 rc = pm8001_find_tag(task, &tag);
1021 if (rc == 0) {
1022 printk(KERN_INFO "No such tag in %s\n", __func__);
1023 rc = TMF_RESP_FUNC_FAILED;
1024 return rc;
1025 }
1026 device_id = pm8001_dev->device_id;
1027 PM8001_EH_DBG(pm8001_ha,
jack_wang97ee2082009-11-05 22:33:51 +08001028 pm8001_printk("abort io to deviceid= %d\n", device_id));
1029 tmf_task.tmf = TMF_ABORT_TASK;
jack wangdbf9bfe2009-10-14 16:19:21 +08001030 tmf_task.tag_of_task_to_be_managed = tag;
1031 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
jack_wang97ee2082009-11-05 22:33:51 +08001032 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
jack wangdbf9bfe2009-10-14 16:19:21 +08001033 pm8001_dev->sas_device, 0, tag);
1034 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1035 task->task_proto & SAS_PROTOCOL_STP) {
1036 dev = task->dev;
1037 pm8001_dev = dev->lldd_dev;
1038 pm8001_ha = pm8001_find_ha_by_dev(dev);
1039 rc = pm8001_find_tag(task, &tag);
1040 if (rc == 0) {
1041 printk(KERN_INFO "No such tag in %s\n", __func__);
1042 rc = TMF_RESP_FUNC_FAILED;
1043 return rc;
1044 }
1045 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1046 pm8001_dev->sas_device, 0, tag);
1047 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1048 /* SMP */
1049 dev = task->dev;
1050 pm8001_dev = dev->lldd_dev;
1051 pm8001_ha = pm8001_find_ha_by_dev(dev);
1052 rc = pm8001_find_tag(task, &tag);
1053 if (rc == 0) {
1054 printk(KERN_INFO "No such tag in %s\n", __func__);
1055 rc = TMF_RESP_FUNC_FAILED;
1056 return rc;
1057 }
1058 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1059 pm8001_dev->sas_device, 0, tag);
1060
1061 }
1062out:
1063 if (rc != TMF_RESP_FUNC_COMPLETE)
1064 pm8001_printk("rc= %d\n", rc);
1065 return rc;
1066}
1067
1068int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1069{
1070 int rc = TMF_RESP_FUNC_FAILED;
1071 struct pm8001_tmf_task tmf_task;
1072
1073 tmf_task.tmf = TMF_ABORT_TASK_SET;
1074 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1075 return rc;
1076}
1077
1078int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1079{
1080 int rc = TMF_RESP_FUNC_FAILED;
1081 struct pm8001_tmf_task tmf_task;
1082
1083 tmf_task.tmf = TMF_CLEAR_ACA;
1084 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1085
1086 return rc;
1087}
1088
1089int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1090{
1091 int rc = TMF_RESP_FUNC_FAILED;
1092 struct pm8001_tmf_task tmf_task;
1093 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1094 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1095
1096 PM8001_EH_DBG(pm8001_ha,
1097 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1098 pm8001_dev->device_id));
1099 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1100 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1101 return rc;
1102}
1103