]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/scsi/pm8001/pm8001_sas.c
[SCSI] pm8001: Add FUNC_GET_EVENTS
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / pm8001 / pm8001_sas.c
1 /*
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 <linux/slab.h>
42 #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 */
49 static 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 */
65 static 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
71 static void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag)
72 {
73 pm8001_tag_clear(pm8001_ha, tag);
74 }
75
76 static 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 */
87 inline 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
101 void 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 */
116 int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr,
117 dma_addr_t *pphys_addr, u32 *pphys_addr_hi,
118 u32 *pphys_addr_lo, u32 mem_size, u32 align)
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 */
145 static
146 struct 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 */
162 int 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);
169 unsigned long flags;
170 pm8001_ha = sas_phy->ha->lldd_ha;
171 pm8001_ha->phy[phy_id].enable_completion = &completion;
172 switch (func) {
173 case PHY_FUNC_SET_LINK_RATE:
174 rates = funcdata;
175 if (rates->minimum_linkrate) {
176 pm8001_ha->phy[phy_id].minimum_linkrate =
177 rates->minimum_linkrate;
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;
213 case PHY_FUNC_GET_EVENTS:
214 spin_lock_irqsave(&pm8001_ha->lock, flags);
215 if (-1 == pm8001_bar4_shift(pm8001_ha,
216 (phy_id < 4) ? 0x30000 : 0x40000)) {
217 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
218 return -EINVAL;
219 }
220 {
221 struct sas_phy *phy = sas_phy->phy;
222 uint32_t *qp = (uint32_t *)(((char *)
223 pm8001_ha->io_mem[2].memvirtaddr)
224 + 0x1034 + (0x4000 * (phy_id & 3)));
225
226 phy->invalid_dword_count = qp[0];
227 phy->running_disparity_error_count = qp[1];
228 phy->loss_of_dword_sync_count = qp[3];
229 phy->phy_reset_problem_count = qp[4];
230 }
231 pm8001_bar4_shift(pm8001_ha, 0);
232 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
233 return 0;
234 default:
235 rc = -EOPNOTSUPP;
236 }
237 msleep(300);
238 return rc;
239 }
240
241 /**
242 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
243 * command to HBA.
244 * @shost: the scsi host data.
245 */
246 void pm8001_scan_start(struct Scsi_Host *shost)
247 {
248 int i;
249 struct pm8001_hba_info *pm8001_ha;
250 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
251 pm8001_ha = sha->lldd_ha;
252 PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha);
253 for (i = 0; i < pm8001_ha->chip->n_phy; ++i)
254 PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i);
255 }
256
257 int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time)
258 {
259 /* give the phy enabling interrupt event time to come in (1s
260 * is empirically about all it takes) */
261 if (time < HZ)
262 return 0;
263 /* Wait for discovery to finish */
264 scsi_flush_work(shost);
265 return 1;
266 }
267
268 /**
269 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
270 * @pm8001_ha: our hba card information
271 * @ccb: the ccb which attached to smp task
272 */
273 static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha,
274 struct pm8001_ccb_info *ccb)
275 {
276 return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb);
277 }
278
279 u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag)
280 {
281 struct ata_queued_cmd *qc = task->uldd_task;
282 if (qc) {
283 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
284 qc->tf.command == ATA_CMD_FPDMA_READ) {
285 *tag = qc->tag;
286 return 1;
287 }
288 }
289 return 0;
290 }
291
292 /**
293 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
294 * @pm8001_ha: our hba card information
295 * @ccb: the ccb which attached to sata task
296 */
297 static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha,
298 struct pm8001_ccb_info *ccb)
299 {
300 return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb);
301 }
302
303 /**
304 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
305 * @pm8001_ha: our hba card information
306 * @ccb: the ccb which attached to TM
307 * @tmf: the task management IU
308 */
309 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha,
310 struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf)
311 {
312 return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf);
313 }
314
315 /**
316 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
317 * @pm8001_ha: our hba card information
318 * @ccb: the ccb which attached to ssp task
319 */
320 static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha,
321 struct pm8001_ccb_info *ccb)
322 {
323 return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb);
324 }
325
326 /* Find the local port id that's attached to this device */
327 static int sas_find_local_port_id(struct domain_device *dev)
328 {
329 struct domain_device *pdev = dev->parent;
330
331 /* Directly attached device */
332 if (!pdev)
333 return dev->port->id;
334 while (pdev) {
335 struct domain_device *pdev_p = pdev->parent;
336 if (!pdev_p)
337 return pdev->port->id;
338 pdev = pdev->parent;
339 }
340 return 0;
341 }
342
343 /**
344 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
345 * @task: the task to be execute.
346 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
347 * we always execute one one time.
348 * @gfp_flags: gfp_flags.
349 * @is_tmf: if it is task management task.
350 * @tmf: the task management IU
351 */
352 #define DEV_IS_GONE(pm8001_dev) \
353 ((!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE)))
354 static int pm8001_task_exec(struct sas_task *task, const int num,
355 gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf)
356 {
357 struct domain_device *dev = task->dev;
358 struct pm8001_hba_info *pm8001_ha;
359 struct pm8001_device *pm8001_dev;
360 struct pm8001_port *port = NULL;
361 struct sas_task *t = task;
362 struct pm8001_ccb_info *ccb;
363 u32 tag = 0xdeadbeef, rc, n_elem = 0;
364 u32 n = num;
365 unsigned long flags = 0, flags_libsas = 0;
366
367 if (!dev->port) {
368 struct task_status_struct *tsm = &t->task_status;
369 tsm->resp = SAS_TASK_UNDELIVERED;
370 tsm->stat = SAS_PHY_DOWN;
371 if (dev->dev_type != SATA_DEV)
372 t->task_done(t);
373 return 0;
374 }
375 pm8001_ha = pm8001_find_ha_by_dev(task->dev);
376 PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n "));
377 spin_lock_irqsave(&pm8001_ha->lock, flags);
378 do {
379 dev = t->dev;
380 pm8001_dev = dev->lldd_dev;
381 port = &pm8001_ha->port[sas_find_local_port_id(dev)];
382 if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) {
383 if (sas_protocol_ata(t->task_proto)) {
384 struct task_status_struct *ts = &t->task_status;
385 ts->resp = SAS_TASK_UNDELIVERED;
386 ts->stat = SAS_PHY_DOWN;
387
388 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
389 spin_unlock_irqrestore(dev->sata_dev.ap->lock,
390 flags_libsas);
391 t->task_done(t);
392 spin_lock_irqsave(dev->sata_dev.ap->lock,
393 flags_libsas);
394 spin_lock_irqsave(&pm8001_ha->lock, flags);
395 if (n > 1)
396 t = list_entry(t->list.next,
397 struct sas_task, list);
398 continue;
399 } else {
400 struct task_status_struct *ts = &t->task_status;
401 ts->resp = SAS_TASK_UNDELIVERED;
402 ts->stat = SAS_PHY_DOWN;
403 t->task_done(t);
404 if (n > 1)
405 t = list_entry(t->list.next,
406 struct sas_task, list);
407 continue;
408 }
409 }
410 rc = pm8001_tag_alloc(pm8001_ha, &tag);
411 if (rc)
412 goto err_out;
413 ccb = &pm8001_ha->ccb_info[tag];
414
415 if (!sas_protocol_ata(t->task_proto)) {
416 if (t->num_scatter) {
417 n_elem = dma_map_sg(pm8001_ha->dev,
418 t->scatter,
419 t->num_scatter,
420 t->data_dir);
421 if (!n_elem) {
422 rc = -ENOMEM;
423 goto err_out_tag;
424 }
425 }
426 } else {
427 n_elem = t->num_scatter;
428 }
429
430 t->lldd_task = ccb;
431 ccb->n_elem = n_elem;
432 ccb->ccb_tag = tag;
433 ccb->task = t;
434 switch (t->task_proto) {
435 case SAS_PROTOCOL_SMP:
436 rc = pm8001_task_prep_smp(pm8001_ha, ccb);
437 break;
438 case SAS_PROTOCOL_SSP:
439 if (is_tmf)
440 rc = pm8001_task_prep_ssp_tm(pm8001_ha,
441 ccb, tmf);
442 else
443 rc = pm8001_task_prep_ssp(pm8001_ha, ccb);
444 break;
445 case SAS_PROTOCOL_SATA:
446 case SAS_PROTOCOL_STP:
447 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
448 rc = pm8001_task_prep_ata(pm8001_ha, ccb);
449 break;
450 default:
451 dev_printk(KERN_ERR, pm8001_ha->dev,
452 "unknown sas_task proto: 0x%x\n",
453 t->task_proto);
454 rc = -EINVAL;
455 break;
456 }
457
458 if (rc) {
459 PM8001_IO_DBG(pm8001_ha,
460 pm8001_printk("rc is %x\n", rc));
461 goto err_out_tag;
462 }
463 /* TODO: select normal or high priority */
464 spin_lock(&t->task_state_lock);
465 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
466 spin_unlock(&t->task_state_lock);
467 pm8001_dev->running_req++;
468 if (n > 1)
469 t = list_entry(t->list.next, struct sas_task, list);
470 } while (--n);
471 rc = 0;
472 goto out_done;
473
474 err_out_tag:
475 pm8001_tag_free(pm8001_ha, tag);
476 err_out:
477 dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc);
478 if (!sas_protocol_ata(t->task_proto))
479 if (n_elem)
480 dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem,
481 t->data_dir);
482 out_done:
483 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
484 return rc;
485 }
486
487 /**
488 * pm8001_queue_command - register for upper layer used, all IO commands sent
489 * to HBA are from this interface.
490 * @task: the task to be execute.
491 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
492 * we always execute one one time
493 * @gfp_flags: gfp_flags
494 */
495 int pm8001_queue_command(struct sas_task *task, const int num,
496 gfp_t gfp_flags)
497 {
498 return pm8001_task_exec(task, num, gfp_flags, 0, NULL);
499 }
500
501 void pm8001_ccb_free(struct pm8001_hba_info *pm8001_ha, u32 ccb_idx)
502 {
503 pm8001_tag_clear(pm8001_ha, ccb_idx);
504 }
505
506 /**
507 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
508 * @pm8001_ha: our hba card information
509 * @ccb: the ccb which attached to ssp task
510 * @task: the task to be free.
511 * @ccb_idx: ccb index.
512 */
513 void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha,
514 struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx)
515 {
516 if (!ccb->task)
517 return;
518 if (!sas_protocol_ata(task->task_proto))
519 if (ccb->n_elem)
520 dma_unmap_sg(pm8001_ha->dev, task->scatter,
521 task->num_scatter, task->data_dir);
522
523 switch (task->task_proto) {
524 case SAS_PROTOCOL_SMP:
525 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1,
526 PCI_DMA_FROMDEVICE);
527 dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1,
528 PCI_DMA_TODEVICE);
529 break;
530
531 case SAS_PROTOCOL_SATA:
532 case SAS_PROTOCOL_STP:
533 case SAS_PROTOCOL_SSP:
534 default:
535 /* do nothing */
536 break;
537 }
538 task->lldd_task = NULL;
539 ccb->task = NULL;
540 ccb->ccb_tag = 0xFFFFFFFF;
541 pm8001_ccb_free(pm8001_ha, ccb_idx);
542 }
543
544 /**
545 * pm8001_alloc_dev - find a empty pm8001_device
546 * @pm8001_ha: our hba card information
547 */
548 struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha)
549 {
550 u32 dev;
551 for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) {
552 if (pm8001_ha->devices[dev].dev_type == NO_DEVICE) {
553 pm8001_ha->devices[dev].id = dev;
554 return &pm8001_ha->devices[dev];
555 }
556 }
557 if (dev == PM8001_MAX_DEVICES) {
558 PM8001_FAIL_DBG(pm8001_ha,
559 pm8001_printk("max support %d devices, ignore ..\n",
560 PM8001_MAX_DEVICES));
561 }
562 return NULL;
563 }
564
565 static void pm8001_free_dev(struct pm8001_device *pm8001_dev)
566 {
567 u32 id = pm8001_dev->id;
568 memset(pm8001_dev, 0, sizeof(*pm8001_dev));
569 pm8001_dev->id = id;
570 pm8001_dev->dev_type = NO_DEVICE;
571 pm8001_dev->device_id = PM8001_MAX_DEVICES;
572 pm8001_dev->sas_device = NULL;
573 }
574
575 /**
576 * pm8001_dev_found_notify - libsas notify a device is found.
577 * @dev: the device structure which sas layer used.
578 *
579 * when libsas find a sas domain device, it should tell the LLDD that
580 * device is found, and then LLDD register this device to HBA firmware
581 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
582 * device ID(according to device's sas address) and returned it to LLDD. From
583 * now on, we communicate with HBA FW with the device ID which HBA assigned
584 * rather than sas address. it is the necessary step for our HBA but it is
585 * the optional for other HBA driver.
586 */
587 static int pm8001_dev_found_notify(struct domain_device *dev)
588 {
589 unsigned long flags = 0;
590 int res = 0;
591 struct pm8001_hba_info *pm8001_ha = NULL;
592 struct domain_device *parent_dev = dev->parent;
593 struct pm8001_device *pm8001_device;
594 DECLARE_COMPLETION_ONSTACK(completion);
595 u32 flag = 0;
596 pm8001_ha = pm8001_find_ha_by_dev(dev);
597 spin_lock_irqsave(&pm8001_ha->lock, flags);
598
599 pm8001_device = pm8001_alloc_dev(pm8001_ha);
600 if (!pm8001_device) {
601 res = -1;
602 goto found_out;
603 }
604 pm8001_device->sas_device = dev;
605 dev->lldd_dev = pm8001_device;
606 pm8001_device->dev_type = dev->dev_type;
607 pm8001_device->dcompletion = &completion;
608 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
609 int phy_id;
610 struct ex_phy *phy;
611 for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys;
612 phy_id++) {
613 phy = &parent_dev->ex_dev.ex_phy[phy_id];
614 if (SAS_ADDR(phy->attached_sas_addr)
615 == SAS_ADDR(dev->sas_addr)) {
616 pm8001_device->attached_phy = phy_id;
617 break;
618 }
619 }
620 if (phy_id == parent_dev->ex_dev.num_phys) {
621 PM8001_FAIL_DBG(pm8001_ha,
622 pm8001_printk("Error: no attached dev:%016llx"
623 " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr),
624 SAS_ADDR(parent_dev->sas_addr)));
625 res = -1;
626 }
627 } else {
628 if (dev->dev_type == SATA_DEV) {
629 pm8001_device->attached_phy =
630 dev->rphy->identify.phy_identifier;
631 flag = 1; /* directly sata*/
632 }
633 } /*register this device to HBA*/
634 PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device\n"));
635 PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag);
636 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
637 wait_for_completion(&completion);
638 if (dev->dev_type == SAS_END_DEV)
639 msleep(50);
640 pm8001_ha->flags = PM8001F_RUN_TIME;
641 return 0;
642 found_out:
643 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
644 return res;
645 }
646
647 int pm8001_dev_found(struct domain_device *dev)
648 {
649 return pm8001_dev_found_notify(dev);
650 }
651
652 static void pm8001_task_done(struct sas_task *task)
653 {
654 if (!del_timer(&task->timer))
655 return;
656 complete(&task->completion);
657 }
658
659 static void pm8001_tmf_timedout(unsigned long data)
660 {
661 struct sas_task *task = (struct sas_task *)data;
662
663 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
664 complete(&task->completion);
665 }
666
667 #define PM8001_TASK_TIMEOUT 20
668 /**
669 * pm8001_exec_internal_tmf_task - execute some task management commands.
670 * @dev: the wanted device.
671 * @tmf: which task management wanted to be take.
672 * @para_len: para_len.
673 * @parameter: ssp task parameter.
674 *
675 * when errors or exception happened, we may want to do something, for example
676 * abort the issued task which result in this execption, it is done by calling
677 * this function, note it is also with the task execute interface.
678 */
679 static int pm8001_exec_internal_tmf_task(struct domain_device *dev,
680 void *parameter, u32 para_len, struct pm8001_tmf_task *tmf)
681 {
682 int res, retry;
683 struct sas_task *task = NULL;
684 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
685
686 for (retry = 0; retry < 3; retry++) {
687 task = sas_alloc_task(GFP_KERNEL);
688 if (!task)
689 return -ENOMEM;
690
691 task->dev = dev;
692 task->task_proto = dev->tproto;
693 memcpy(&task->ssp_task, parameter, para_len);
694 task->task_done = pm8001_task_done;
695 task->timer.data = (unsigned long)task;
696 task->timer.function = pm8001_tmf_timedout;
697 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ;
698 add_timer(&task->timer);
699
700 res = pm8001_task_exec(task, 1, GFP_KERNEL, 1, tmf);
701
702 if (res) {
703 del_timer(&task->timer);
704 PM8001_FAIL_DBG(pm8001_ha,
705 pm8001_printk("Executing internal task "
706 "failed\n"));
707 goto ex_err;
708 }
709 wait_for_completion(&task->completion);
710 res = -TMF_RESP_FUNC_FAILED;
711 /* Even TMF timed out, return direct. */
712 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
713 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
714 PM8001_FAIL_DBG(pm8001_ha,
715 pm8001_printk("TMF task[%x]timeout.\n",
716 tmf->tmf));
717 goto ex_err;
718 }
719 }
720
721 if (task->task_status.resp == SAS_TASK_COMPLETE &&
722 task->task_status.stat == SAM_STAT_GOOD) {
723 res = TMF_RESP_FUNC_COMPLETE;
724 break;
725 }
726
727 if (task->task_status.resp == SAS_TASK_COMPLETE &&
728 task->task_status.stat == SAS_DATA_UNDERRUN) {
729 /* no error, but return the number of bytes of
730 * underrun */
731 res = task->task_status.residual;
732 break;
733 }
734
735 if (task->task_status.resp == SAS_TASK_COMPLETE &&
736 task->task_status.stat == SAS_DATA_OVERRUN) {
737 PM8001_FAIL_DBG(pm8001_ha,
738 pm8001_printk("Blocked task error.\n"));
739 res = -EMSGSIZE;
740 break;
741 } else {
742 PM8001_EH_DBG(pm8001_ha,
743 pm8001_printk(" Task to dev %016llx response:"
744 "0x%x status 0x%x\n",
745 SAS_ADDR(dev->sas_addr),
746 task->task_status.resp,
747 task->task_status.stat));
748 sas_free_task(task);
749 task = NULL;
750 }
751 }
752 ex_err:
753 BUG_ON(retry == 3 && task != NULL);
754 sas_free_task(task);
755 return res;
756 }
757
758 static int
759 pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha,
760 struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag,
761 u32 task_tag)
762 {
763 int res, retry;
764 u32 ccb_tag;
765 struct pm8001_ccb_info *ccb;
766 struct sas_task *task = NULL;
767
768 for (retry = 0; retry < 3; retry++) {
769 task = sas_alloc_task(GFP_KERNEL);
770 if (!task)
771 return -ENOMEM;
772
773 task->dev = dev;
774 task->task_proto = dev->tproto;
775 task->task_done = pm8001_task_done;
776 task->timer.data = (unsigned long)task;
777 task->timer.function = pm8001_tmf_timedout;
778 task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ;
779 add_timer(&task->timer);
780
781 res = pm8001_tag_alloc(pm8001_ha, &ccb_tag);
782 if (res)
783 return res;
784 ccb = &pm8001_ha->ccb_info[ccb_tag];
785 ccb->device = pm8001_dev;
786 ccb->ccb_tag = ccb_tag;
787 ccb->task = task;
788
789 res = PM8001_CHIP_DISP->task_abort(pm8001_ha,
790 pm8001_dev, flag, task_tag, ccb_tag);
791
792 if (res) {
793 del_timer(&task->timer);
794 PM8001_FAIL_DBG(pm8001_ha,
795 pm8001_printk("Executing internal task "
796 "failed\n"));
797 goto ex_err;
798 }
799 wait_for_completion(&task->completion);
800 res = TMF_RESP_FUNC_FAILED;
801 /* Even TMF timed out, return direct. */
802 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
803 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
804 PM8001_FAIL_DBG(pm8001_ha,
805 pm8001_printk("TMF task timeout.\n"));
806 goto ex_err;
807 }
808 }
809
810 if (task->task_status.resp == SAS_TASK_COMPLETE &&
811 task->task_status.stat == SAM_STAT_GOOD) {
812 res = TMF_RESP_FUNC_COMPLETE;
813 break;
814
815 } else {
816 PM8001_EH_DBG(pm8001_ha,
817 pm8001_printk(" Task to dev %016llx response: "
818 "0x%x status 0x%x\n",
819 SAS_ADDR(dev->sas_addr),
820 task->task_status.resp,
821 task->task_status.stat));
822 sas_free_task(task);
823 task = NULL;
824 }
825 }
826 ex_err:
827 BUG_ON(retry == 3 && task != NULL);
828 sas_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 */
836 static 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
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 u32 device_id = pm8001_dev->device_id;
848
849 PM8001_DISC_DBG(pm8001_ha,
850 pm8001_printk("found dev[%d:%x] is gone.\n",
851 pm8001_dev->device_id, pm8001_dev->dev_type));
852 if (pm8001_dev->running_req) {
853 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
854 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
855 dev, 1, 0);
856 spin_lock_irqsave(&pm8001_ha->lock, flags);
857 }
858 PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id);
859 pm8001_free_dev(pm8001_dev);
860 } else {
861 PM8001_DISC_DBG(pm8001_ha,
862 pm8001_printk("Found dev has gone.\n"));
863 }
864 dev->lldd_dev = NULL;
865 spin_unlock_irqrestore(&pm8001_ha->lock, flags);
866 }
867
868 void pm8001_dev_gone(struct domain_device *dev)
869 {
870 pm8001_dev_gone_notify(dev);
871 }
872
873 static int pm8001_issue_ssp_tmf(struct domain_device *dev,
874 u8 *lun, struct pm8001_tmf_task *tmf)
875 {
876 struct sas_ssp_task ssp_task;
877 if (!(dev->tproto & SAS_PROTOCOL_SSP))
878 return TMF_RESP_FUNC_ESUPP;
879
880 strncpy((u8 *)&ssp_task.LUN, lun, 8);
881 return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task),
882 tmf);
883 }
884
885 /**
886 * Standard mandates link reset for ATA (type 0) and hard reset for
887 * SSP (type 1) , only for RECOVERY
888 */
889 int pm8001_I_T_nexus_reset(struct domain_device *dev)
890 {
891 int rc = TMF_RESP_FUNC_FAILED;
892 struct pm8001_device *pm8001_dev;
893 struct pm8001_hba_info *pm8001_ha;
894 struct sas_phy *phy;
895 if (!dev || !dev->lldd_dev)
896 return -1;
897
898 pm8001_dev = dev->lldd_dev;
899 pm8001_ha = pm8001_find_ha_by_dev(dev);
900 phy = sas_find_local_phy(dev);
901
902 if (dev_is_sata(dev)) {
903 DECLARE_COMPLETION_ONSTACK(completion_setstate);
904 if (scsi_is_sas_phy_local(phy))
905 return 0;
906 rc = sas_phy_reset(phy, 1);
907 msleep(2000);
908 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
909 dev, 1, 0);
910 pm8001_dev->setds_completion = &completion_setstate;
911 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
912 pm8001_dev, 0x01);
913 wait_for_completion(&completion_setstate);
914 } else{
915 rc = sas_phy_reset(phy, 1);
916 msleep(2000);
917 }
918 PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n",
919 pm8001_dev->device_id, rc));
920 return rc;
921 }
922
923 /* mandatory SAM-3, the task reset the specified LUN*/
924 int pm8001_lu_reset(struct domain_device *dev, u8 *lun)
925 {
926 int rc = TMF_RESP_FUNC_FAILED;
927 struct pm8001_tmf_task tmf_task;
928 struct pm8001_device *pm8001_dev = dev->lldd_dev;
929 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
930 if (dev_is_sata(dev)) {
931 struct sas_phy *phy = sas_find_local_phy(dev);
932 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev ,
933 dev, 1, 0);
934 rc = sas_phy_reset(phy, 1);
935 rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha,
936 pm8001_dev, 0x01);
937 msleep(2000);
938 } else {
939 tmf_task.tmf = TMF_LU_RESET;
940 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
941 }
942 /* If failed, fall-through I_T_Nexus reset */
943 PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n",
944 pm8001_dev->device_id, rc));
945 return rc;
946 }
947
948 /* optional SAM-3 */
949 int pm8001_query_task(struct sas_task *task)
950 {
951 u32 tag = 0xdeadbeef;
952 int i = 0;
953 struct scsi_lun lun;
954 struct pm8001_tmf_task tmf_task;
955 int rc = TMF_RESP_FUNC_FAILED;
956 if (unlikely(!task || !task->lldd_task || !task->dev))
957 return rc;
958
959 if (task->task_proto & SAS_PROTOCOL_SSP) {
960 struct scsi_cmnd *cmnd = task->uldd_task;
961 struct domain_device *dev = task->dev;
962 struct pm8001_hba_info *pm8001_ha =
963 pm8001_find_ha_by_dev(dev);
964
965 int_to_scsilun(cmnd->device->lun, &lun);
966 rc = pm8001_find_tag(task, &tag);
967 if (rc == 0) {
968 rc = TMF_RESP_FUNC_FAILED;
969 return rc;
970 }
971 PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:["));
972 for (i = 0; i < 16; i++)
973 printk(KERN_INFO "%02x ", cmnd->cmnd[i]);
974 printk(KERN_INFO "]\n");
975 tmf_task.tmf = TMF_QUERY_TASK;
976 tmf_task.tag_of_task_to_be_managed = tag;
977
978 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
979 switch (rc) {
980 /* The task is still in Lun, release it then */
981 case TMF_RESP_FUNC_SUCC:
982 PM8001_EH_DBG(pm8001_ha,
983 pm8001_printk("The task is still in Lun\n"));
984 break;
985 /* The task is not in Lun or failed, reset the phy */
986 case TMF_RESP_FUNC_FAILED:
987 case TMF_RESP_FUNC_COMPLETE:
988 PM8001_EH_DBG(pm8001_ha,
989 pm8001_printk("The task is not in Lun or failed,"
990 " reset the phy\n"));
991 break;
992 }
993 }
994 pm8001_printk(":rc= %d\n", rc);
995 return rc;
996 }
997
998 /* mandatory SAM-3, still need free task/ccb info, abord the specified task */
999 int pm8001_abort_task(struct sas_task *task)
1000 {
1001 unsigned long flags;
1002 u32 tag = 0xdeadbeef;
1003 u32 device_id;
1004 struct domain_device *dev ;
1005 struct pm8001_hba_info *pm8001_ha = NULL;
1006 struct pm8001_ccb_info *ccb;
1007 struct scsi_lun lun;
1008 struct pm8001_device *pm8001_dev;
1009 struct pm8001_tmf_task tmf_task;
1010 int rc = TMF_RESP_FUNC_FAILED;
1011 if (unlikely(!task || !task->lldd_task || !task->dev))
1012 return rc;
1013 spin_lock_irqsave(&task->task_state_lock, flags);
1014 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1015 spin_unlock_irqrestore(&task->task_state_lock, flags);
1016 rc = TMF_RESP_FUNC_COMPLETE;
1017 goto out;
1018 }
1019 spin_unlock_irqrestore(&task->task_state_lock, flags);
1020 if (task->task_proto & SAS_PROTOCOL_SSP) {
1021 struct scsi_cmnd *cmnd = task->uldd_task;
1022 dev = task->dev;
1023 ccb = task->lldd_task;
1024 pm8001_dev = dev->lldd_dev;
1025 pm8001_ha = pm8001_find_ha_by_dev(dev);
1026 int_to_scsilun(cmnd->device->lun, &lun);
1027 rc = pm8001_find_tag(task, &tag);
1028 if (rc == 0) {
1029 printk(KERN_INFO "No such tag in %s\n", __func__);
1030 rc = TMF_RESP_FUNC_FAILED;
1031 return rc;
1032 }
1033 device_id = pm8001_dev->device_id;
1034 PM8001_EH_DBG(pm8001_ha,
1035 pm8001_printk("abort io to deviceid= %d\n", device_id));
1036 tmf_task.tmf = TMF_ABORT_TASK;
1037 tmf_task.tag_of_task_to_be_managed = tag;
1038 rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1039 pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1040 pm8001_dev->sas_device, 0, tag);
1041 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1042 task->task_proto & SAS_PROTOCOL_STP) {
1043 dev = task->dev;
1044 pm8001_dev = dev->lldd_dev;
1045 pm8001_ha = pm8001_find_ha_by_dev(dev);
1046 rc = pm8001_find_tag(task, &tag);
1047 if (rc == 0) {
1048 printk(KERN_INFO "No such tag in %s\n", __func__);
1049 rc = TMF_RESP_FUNC_FAILED;
1050 return rc;
1051 }
1052 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1053 pm8001_dev->sas_device, 0, tag);
1054 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
1055 /* SMP */
1056 dev = task->dev;
1057 pm8001_dev = dev->lldd_dev;
1058 pm8001_ha = pm8001_find_ha_by_dev(dev);
1059 rc = pm8001_find_tag(task, &tag);
1060 if (rc == 0) {
1061 printk(KERN_INFO "No such tag in %s\n", __func__);
1062 rc = TMF_RESP_FUNC_FAILED;
1063 return rc;
1064 }
1065 rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev,
1066 pm8001_dev->sas_device, 0, tag);
1067
1068 }
1069 out:
1070 if (rc != TMF_RESP_FUNC_COMPLETE)
1071 pm8001_printk("rc= %d\n", rc);
1072 return rc;
1073 }
1074
1075 int pm8001_abort_task_set(struct domain_device *dev, u8 *lun)
1076 {
1077 int rc = TMF_RESP_FUNC_FAILED;
1078 struct pm8001_tmf_task tmf_task;
1079
1080 tmf_task.tmf = TMF_ABORT_TASK_SET;
1081 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1082 return rc;
1083 }
1084
1085 int pm8001_clear_aca(struct domain_device *dev, u8 *lun)
1086 {
1087 int rc = TMF_RESP_FUNC_FAILED;
1088 struct pm8001_tmf_task tmf_task;
1089
1090 tmf_task.tmf = TMF_CLEAR_ACA;
1091 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1092
1093 return rc;
1094 }
1095
1096 int pm8001_clear_task_set(struct domain_device *dev, u8 *lun)
1097 {
1098 int rc = TMF_RESP_FUNC_FAILED;
1099 struct pm8001_tmf_task tmf_task;
1100 struct pm8001_device *pm8001_dev = dev->lldd_dev;
1101 struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev);
1102
1103 PM8001_EH_DBG(pm8001_ha,
1104 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1105 pm8001_dev->device_id));
1106 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1107 rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task);
1108 return rc;
1109 }
1110