2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
13 #define DRV_NAME "hisi_sas"
15 #define DEV_IS_GONE(dev) \
16 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
18 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device
*device
,
19 u8
*lun
, struct hisi_sas_tmf_task
*tmf
);
21 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
22 struct domain_device
*device
,
23 int abort_flag
, int tag
);
24 static int hisi_sas_softreset_ata_disk(struct domain_device
*device
);
25 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
28 u8
hisi_sas_get_ata_protocol(struct host_to_dev_fis
*fis
, int direction
)
30 switch (fis
->command
) {
31 case ATA_CMD_FPDMA_WRITE
:
32 case ATA_CMD_FPDMA_READ
:
33 case ATA_CMD_FPDMA_RECV
:
34 case ATA_CMD_FPDMA_SEND
:
35 case ATA_CMD_NCQ_NON_DATA
:
36 return HISI_SAS_SATA_PROTOCOL_FPDMA
;
38 case ATA_CMD_DOWNLOAD_MICRO
:
40 case ATA_CMD_PMP_READ
:
41 case ATA_CMD_READ_LOG_EXT
:
42 case ATA_CMD_PIO_READ
:
43 case ATA_CMD_PIO_READ_EXT
:
44 case ATA_CMD_PMP_WRITE
:
45 case ATA_CMD_WRITE_LOG_EXT
:
46 case ATA_CMD_PIO_WRITE
:
47 case ATA_CMD_PIO_WRITE_EXT
:
48 return HISI_SAS_SATA_PROTOCOL_PIO
;
51 case ATA_CMD_DOWNLOAD_MICRO_DMA
:
52 case ATA_CMD_PMP_READ_DMA
:
53 case ATA_CMD_PMP_WRITE_DMA
:
55 case ATA_CMD_READ_EXT
:
56 case ATA_CMD_READ_LOG_DMA_EXT
:
57 case ATA_CMD_READ_STREAM_DMA_EXT
:
58 case ATA_CMD_TRUSTED_RCV_DMA
:
59 case ATA_CMD_TRUSTED_SND_DMA
:
61 case ATA_CMD_WRITE_EXT
:
62 case ATA_CMD_WRITE_FUA_EXT
:
63 case ATA_CMD_WRITE_QUEUED
:
64 case ATA_CMD_WRITE_LOG_DMA_EXT
:
65 case ATA_CMD_WRITE_STREAM_DMA_EXT
:
66 case ATA_CMD_ZAC_MGMT_IN
:
67 return HISI_SAS_SATA_PROTOCOL_DMA
;
69 case ATA_CMD_CHK_POWER
:
70 case ATA_CMD_DEV_RESET
:
73 case ATA_CMD_FLUSH_EXT
:
75 case ATA_CMD_VERIFY_EXT
:
76 case ATA_CMD_SET_FEATURES
:
78 case ATA_CMD_STANDBYNOW1
:
79 case ATA_CMD_ZAC_MGMT_OUT
:
80 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
83 if (fis
->command
== ATA_CMD_SET_MAX
) {
84 switch (fis
->features
) {
85 case ATA_SET_MAX_PASSWD
:
86 case ATA_SET_MAX_LOCK
:
87 return HISI_SAS_SATA_PROTOCOL_PIO
;
89 case ATA_SET_MAX_PASSWD_DMA
:
90 case ATA_SET_MAX_UNLOCK_DMA
:
91 return HISI_SAS_SATA_PROTOCOL_DMA
;
94 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
97 if (direction
== DMA_NONE
)
98 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
99 return HISI_SAS_SATA_PROTOCOL_PIO
;
103 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol
);
105 void hisi_sas_sata_done(struct sas_task
*task
,
106 struct hisi_sas_slot
*slot
)
108 struct task_status_struct
*ts
= &task
->task_status
;
109 struct ata_task_resp
*resp
= (struct ata_task_resp
*)ts
->buf
;
110 struct hisi_sas_status_buffer
*status_buf
=
111 hisi_sas_status_buf_addr_mem(slot
);
112 u8
*iu
= &status_buf
->iu
[0];
113 struct dev_to_host_fis
*d2h
= (struct dev_to_host_fis
*)iu
;
115 resp
->frame_len
= sizeof(struct dev_to_host_fis
);
116 memcpy(&resp
->ending_fis
[0], d2h
, sizeof(struct dev_to_host_fis
));
118 ts
->buf_valid_size
= sizeof(*resp
);
120 EXPORT_SYMBOL_GPL(hisi_sas_sata_done
);
122 int hisi_sas_get_ncq_tag(struct sas_task
*task
, u32
*tag
)
124 struct ata_queued_cmd
*qc
= task
->uldd_task
;
127 if (qc
->tf
.command
== ATA_CMD_FPDMA_WRITE
||
128 qc
->tf
.command
== ATA_CMD_FPDMA_READ
) {
135 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag
);
138 * This function assumes linkrate mask fits in 8 bits, which it
139 * does for all HW versions supported.
141 u8
hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max
)
146 max
-= SAS_LINK_RATE_1_5_GBPS
;
147 for (i
= 0; i
<= max
; i
++)
148 rate
|= 1 << (i
* 2);
151 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask
);
153 static struct hisi_hba
*dev_to_hisi_hba(struct domain_device
*device
)
155 return device
->port
->ha
->lldd_ha
;
158 struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
)
160 return container_of(sas_port
, struct hisi_sas_port
, sas_port
);
162 EXPORT_SYMBOL_GPL(to_hisi_sas_port
);
164 void hisi_sas_stop_phys(struct hisi_hba
*hisi_hba
)
168 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++)
169 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
171 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys
);
173 static void hisi_sas_slot_index_clear(struct hisi_hba
*hisi_hba
, int slot_idx
)
175 void *bitmap
= hisi_hba
->slot_index_tags
;
177 clear_bit(slot_idx
, bitmap
);
180 static void hisi_sas_slot_index_free(struct hisi_hba
*hisi_hba
, int slot_idx
)
182 hisi_sas_slot_index_clear(hisi_hba
, slot_idx
);
185 static void hisi_sas_slot_index_set(struct hisi_hba
*hisi_hba
, int slot_idx
)
187 void *bitmap
= hisi_hba
->slot_index_tags
;
189 set_bit(slot_idx
, bitmap
);
192 static int hisi_sas_slot_index_alloc(struct hisi_hba
*hisi_hba
, int *slot_idx
)
195 void *bitmap
= hisi_hba
->slot_index_tags
;
197 index
= find_first_zero_bit(bitmap
, hisi_hba
->slot_index_count
);
198 if (index
>= hisi_hba
->slot_index_count
)
199 return -SAS_QUEUE_FULL
;
200 hisi_sas_slot_index_set(hisi_hba
, index
);
205 static void hisi_sas_slot_index_init(struct hisi_hba
*hisi_hba
)
209 for (i
= 0; i
< hisi_hba
->slot_index_count
; ++i
)
210 hisi_sas_slot_index_clear(hisi_hba
, i
);
213 void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
214 struct hisi_sas_slot
*slot
)
218 struct device
*dev
= hisi_hba
->dev
;
220 if (!task
->lldd_task
)
223 task
->lldd_task
= NULL
;
225 if (!sas_protocol_ata(task
->task_proto
))
227 dma_unmap_sg(dev
, task
->scatter
,
233 dma_pool_free(hisi_hba
->buffer_pool
, slot
->buf
, slot
->buf_dma
);
235 list_del_init(&slot
->entry
);
239 hisi_sas_slot_index_free(hisi_hba
, slot
->idx
);
241 /* slot memory is fully zeroed when it is reused */
243 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free
);
245 static void hisi_sas_task_prep_smp(struct hisi_hba
*hisi_hba
,
246 struct hisi_sas_slot
*slot
)
248 hisi_hba
->hw
->prep_smp(hisi_hba
, slot
);
251 static void hisi_sas_task_prep_ssp(struct hisi_hba
*hisi_hba
,
252 struct hisi_sas_slot
*slot
, int is_tmf
,
253 struct hisi_sas_tmf_task
*tmf
)
255 hisi_hba
->hw
->prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
258 static void hisi_sas_task_prep_ata(struct hisi_hba
*hisi_hba
,
259 struct hisi_sas_slot
*slot
)
261 hisi_hba
->hw
->prep_stp(hisi_hba
, slot
);
264 static void hisi_sas_task_prep_abort(struct hisi_hba
*hisi_hba
,
265 struct hisi_sas_slot
*slot
,
266 int device_id
, int abort_flag
, int tag_to_abort
)
268 hisi_hba
->hw
->prep_abort(hisi_hba
, slot
,
269 device_id
, abort_flag
, tag_to_abort
);
273 * This function will issue an abort TMF regardless of whether the
274 * task is in the sdev or not. Then it will do the task complete
275 * cleanup and callbacks.
277 static void hisi_sas_slot_abort(struct work_struct
*work
)
279 struct hisi_sas_slot
*abort_slot
=
280 container_of(work
, struct hisi_sas_slot
, abort_slot
);
281 struct sas_task
*task
= abort_slot
->task
;
282 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
283 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
284 struct hisi_sas_tmf_task tmf_task
;
286 struct device
*dev
= hisi_hba
->dev
;
287 int tag
= abort_slot
->idx
;
290 if (!(task
->task_proto
& SAS_PROTOCOL_SSP
)) {
291 dev_err(dev
, "cannot abort slot for non-ssp task\n");
295 int_to_scsilun(cmnd
->device
->lun
, &lun
);
296 tmf_task
.tmf
= TMF_ABORT_TASK
;
297 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
299 hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
, &tmf_task
);
301 /* Do cleanup for this task */
302 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
303 hisi_sas_slot_task_free(hisi_hba
, task
, abort_slot
);
304 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
306 task
->task_done(task
);
309 static int hisi_sas_task_prep(struct sas_task
*task
, struct hisi_sas_dq
310 *dq
, int is_tmf
, struct hisi_sas_tmf_task
*tmf
,
313 struct hisi_hba
*hisi_hba
= dq
->hisi_hba
;
314 struct domain_device
*device
= task
->dev
;
315 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
316 struct hisi_sas_port
*port
;
317 struct hisi_sas_slot
*slot
;
318 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
319 struct asd_sas_port
*sas_port
= device
->port
;
320 struct device
*dev
= hisi_hba
->dev
;
321 int dlvry_queue_slot
, dlvry_queue
, rc
, slot_idx
;
322 int n_elem
= 0, n_elem_req
= 0, n_elem_resp
= 0;
326 struct task_status_struct
*ts
= &task
->task_status
;
328 ts
->resp
= SAS_TASK_UNDELIVERED
;
329 ts
->stat
= SAS_PHY_DOWN
;
331 * libsas will use dev->port, should
332 * not call task_done for sata
334 if (device
->dev_type
!= SAS_SATA_DEV
)
335 task
->task_done(task
);
339 if (DEV_IS_GONE(sas_dev
)) {
341 dev_info(dev
, "task prep: device %d not ready\n",
344 dev_info(dev
, "task prep: device %016llx not ready\n",
345 SAS_ADDR(device
->sas_addr
));
350 port
= to_hisi_sas_port(sas_port
);
351 if (port
&& !port
->port_attached
) {
352 dev_info(dev
, "task prep: %s port%d not attach device\n",
353 (dev_is_sata(device
)) ?
360 if (!sas_protocol_ata(task
->task_proto
)) {
361 unsigned int req_len
, resp_len
;
363 if (task
->num_scatter
) {
364 n_elem
= dma_map_sg(dev
, task
->scatter
,
365 task
->num_scatter
, task
->data_dir
);
370 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
371 n_elem_req
= dma_map_sg(dev
, &task
->smp_task
.smp_req
,
377 req_len
= sg_dma_len(&task
->smp_task
.smp_req
);
380 goto err_out_dma_unmap
;
382 n_elem_resp
= dma_map_sg(dev
, &task
->smp_task
.smp_resp
,
386 goto err_out_dma_unmap
;
388 resp_len
= sg_dma_len(&task
->smp_task
.smp_resp
);
389 if (resp_len
& 0x3) {
391 goto err_out_dma_unmap
;
395 n_elem
= task
->num_scatter
;
397 if (n_elem
> HISI_SAS_SGE_PAGE_CNT
) {
398 dev_err(dev
, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
401 goto err_out_dma_unmap
;
404 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
405 if (hisi_hba
->hw
->slot_index_alloc
)
406 rc
= hisi_hba
->hw
->slot_index_alloc(hisi_hba
, &slot_idx
,
409 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
410 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
412 goto err_out_dma_unmap
;
414 slot
= &hisi_hba
->slot_info
[slot_idx
];
415 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
417 slot
->buf
= dma_pool_alloc(hisi_hba
->buffer_pool
,
418 GFP_ATOMIC
, &slot
->buf_dma
);
424 rc
= hisi_hba
->hw
->get_free_slot(hisi_hba
, dq
);
428 dlvry_queue
= dq
->id
;
429 dlvry_queue_slot
= dq
->wr_point
;
431 slot
->idx
= slot_idx
;
432 slot
->n_elem
= n_elem
;
433 slot
->dlvry_queue
= dlvry_queue
;
434 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
435 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
436 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
440 slot
->is_internal
= true;
441 task
->lldd_task
= slot
;
442 INIT_WORK(&slot
->abort_slot
, hisi_sas_slot_abort
);
444 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
445 memset(hisi_sas_cmd_hdr_addr_mem(slot
), 0, HISI_SAS_COMMAND_TABLE_SZ
);
446 memset(hisi_sas_status_buf_addr_mem(slot
), 0, HISI_SAS_STATUS_BUF_SZ
);
448 switch (task
->task_proto
) {
449 case SAS_PROTOCOL_SMP
:
450 hisi_sas_task_prep_smp(hisi_hba
, slot
);
452 case SAS_PROTOCOL_SSP
:
453 hisi_sas_task_prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
455 case SAS_PROTOCOL_SATA
:
456 case SAS_PROTOCOL_STP
:
457 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:
458 hisi_sas_task_prep_ata(hisi_hba
, slot
);
461 dev_err(dev
, "task prep: unknown/unsupported proto (0x%x)\n",
466 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
467 list_add_tail(&slot
->entry
, &sas_dev
->list
);
468 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
469 spin_lock_irqsave(&task
->task_state_lock
, flags
);
470 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
471 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
473 dq
->slot_prep
= slot
;
479 dma_pool_free(hisi_hba
->buffer_pool
, slot
->buf
,
482 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
483 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
484 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
486 if (!sas_protocol_ata(task
->task_proto
)) {
487 if (task
->num_scatter
) {
488 dma_unmap_sg(dev
, task
->scatter
, task
->num_scatter
,
490 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
492 dma_unmap_sg(dev
, &task
->smp_task
.smp_req
,
495 dma_unmap_sg(dev
, &task
->smp_task
.smp_resp
,
500 dev_err(dev
, "task prep: failed[%d]!\n", rc
);
504 static int hisi_sas_task_exec(struct sas_task
*task
, gfp_t gfp_flags
,
505 int is_tmf
, struct hisi_sas_tmf_task
*tmf
)
510 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
511 struct device
*dev
= hisi_hba
->dev
;
512 struct domain_device
*device
= task
->dev
;
513 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
514 struct hisi_sas_dq
*dq
= sas_dev
->dq
;
516 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
)))
519 /* protect task_prep and start_delivery sequence */
520 spin_lock_irqsave(&dq
->lock
, flags
);
521 rc
= hisi_sas_task_prep(task
, dq
, is_tmf
, tmf
, &pass
);
523 dev_err(dev
, "task exec: failed[%d]!\n", rc
);
526 hisi_hba
->hw
->start_delivery(dq
);
527 spin_unlock_irqrestore(&dq
->lock
, flags
);
532 static void hisi_sas_bytes_dmaed(struct hisi_hba
*hisi_hba
, int phy_no
)
534 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
535 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
536 struct sas_ha_struct
*sas_ha
;
538 if (!phy
->phy_attached
)
541 sas_ha
= &hisi_hba
->sha
;
542 sas_ha
->notify_phy_event(sas_phy
, PHYE_OOB_DONE
);
545 struct sas_phy
*sphy
= sas_phy
->phy
;
547 sphy
->negotiated_linkrate
= sas_phy
->linkrate
;
548 sphy
->minimum_linkrate_hw
= SAS_LINK_RATE_1_5_GBPS
;
549 sphy
->maximum_linkrate_hw
=
550 hisi_hba
->hw
->phy_get_max_linkrate();
551 if (sphy
->minimum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
552 sphy
->minimum_linkrate
= phy
->minimum_linkrate
;
554 if (sphy
->maximum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
555 sphy
->maximum_linkrate
= phy
->maximum_linkrate
;
558 if (phy
->phy_type
& PORT_TYPE_SAS
) {
559 struct sas_identify_frame
*id
;
561 id
= (struct sas_identify_frame
*)phy
->frame_rcvd
;
562 id
->dev_type
= phy
->identify
.device_type
;
563 id
->initiator_bits
= SAS_PROTOCOL_ALL
;
564 id
->target_bits
= phy
->identify
.target_port_protocols
;
565 } else if (phy
->phy_type
& PORT_TYPE_SATA
) {
569 sas_phy
->frame_rcvd_size
= phy
->frame_rcvd_size
;
570 sas_ha
->notify_port_event(sas_phy
, PORTE_BYTES_DMAED
);
573 static struct hisi_sas_device
*hisi_sas_alloc_dev(struct domain_device
*device
)
575 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
576 struct hisi_sas_device
*sas_dev
= NULL
;
580 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
581 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
582 if (hisi_hba
->devices
[i
].dev_type
== SAS_PHY_UNUSED
) {
583 int queue
= i
% hisi_hba
->queue_count
;
584 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[queue
];
586 hisi_hba
->devices
[i
].device_id
= i
;
587 sas_dev
= &hisi_hba
->devices
[i
];
588 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
589 sas_dev
->dev_type
= device
->dev_type
;
590 sas_dev
->hisi_hba
= hisi_hba
;
591 sas_dev
->sas_device
= device
;
593 INIT_LIST_HEAD(&hisi_hba
->devices
[i
].list
);
597 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
602 static int hisi_sas_dev_found(struct domain_device
*device
)
604 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
605 struct domain_device
*parent_dev
= device
->parent
;
606 struct hisi_sas_device
*sas_dev
;
607 struct device
*dev
= hisi_hba
->dev
;
609 if (hisi_hba
->hw
->alloc_dev
)
610 sas_dev
= hisi_hba
->hw
->alloc_dev(device
);
612 sas_dev
= hisi_sas_alloc_dev(device
);
614 dev_err(dev
, "fail alloc dev: max support %d devices\n",
615 HISI_SAS_MAX_DEVICES
);
619 device
->lldd_dev
= sas_dev
;
620 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
622 if (parent_dev
&& DEV_IS_EXPANDER(parent_dev
->dev_type
)) {
624 u8 phy_num
= parent_dev
->ex_dev
.num_phys
;
627 for (phy_no
= 0; phy_no
< phy_num
; phy_no
++) {
628 phy
= &parent_dev
->ex_dev
.ex_phy
[phy_no
];
629 if (SAS_ADDR(phy
->attached_sas_addr
) ==
630 SAS_ADDR(device
->sas_addr
)) {
631 sas_dev
->attached_phy
= phy_no
;
636 if (phy_no
== phy_num
) {
637 dev_info(dev
, "dev found: no attached "
638 "dev:%016llx at ex:%016llx\n",
639 SAS_ADDR(device
->sas_addr
),
640 SAS_ADDR(parent_dev
->sas_addr
));
645 dev_info(dev
, "dev[%d:%x] found\n",
646 sas_dev
->device_id
, sas_dev
->dev_type
);
651 static int hisi_sas_slave_configure(struct scsi_device
*sdev
)
653 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
654 int ret
= sas_slave_configure(sdev
);
658 if (!dev_is_sata(dev
))
659 sas_change_queue_depth(sdev
, 64);
664 static void hisi_sas_scan_start(struct Scsi_Host
*shost
)
666 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
668 hisi_hba
->hw
->phys_init(hisi_hba
);
671 static int hisi_sas_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
673 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
674 struct sas_ha_struct
*sha
= &hisi_hba
->sha
;
676 /* Wait for PHY up interrupt to occur */
684 static void hisi_sas_phyup_work(struct work_struct
*work
)
686 struct hisi_sas_phy
*phy
=
687 container_of(work
, typeof(*phy
), works
[HISI_PHYE_PHY_UP
]);
688 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
689 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
690 int phy_no
= sas_phy
->id
;
692 hisi_hba
->hw
->sl_notify(hisi_hba
, phy_no
); /* This requires a sleep */
693 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
696 static void hisi_sas_linkreset_work(struct work_struct
*work
)
698 struct hisi_sas_phy
*phy
=
699 container_of(work
, typeof(*phy
), works
[HISI_PHYE_LINK_RESET
]);
700 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
702 hisi_sas_control_phy(sas_phy
, PHY_FUNC_LINK_RESET
, NULL
);
705 static const work_func_t hisi_sas_phye_fns
[HISI_PHYES_NUM
] = {
706 [HISI_PHYE_PHY_UP
] = hisi_sas_phyup_work
,
707 [HISI_PHYE_LINK_RESET
] = hisi_sas_linkreset_work
,
710 bool hisi_sas_notify_phy_event(struct hisi_sas_phy
*phy
,
711 enum hisi_sas_phy_event event
)
713 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
715 if (WARN_ON(event
>= HISI_PHYES_NUM
))
718 return queue_work(hisi_hba
->wq
, &phy
->works
[event
]);
720 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event
);
722 static void hisi_sas_phy_init(struct hisi_hba
*hisi_hba
, int phy_no
)
724 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
725 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
728 phy
->hisi_hba
= hisi_hba
;
730 phy
->minimum_linkrate
= SAS_LINK_RATE_1_5_GBPS
;
731 phy
->maximum_linkrate
= hisi_hba
->hw
->phy_get_max_linkrate();
732 sas_phy
->enabled
= (phy_no
< hisi_hba
->n_phy
) ? 1 : 0;
733 sas_phy
->class = SAS
;
734 sas_phy
->iproto
= SAS_PROTOCOL_ALL
;
736 sas_phy
->type
= PHY_TYPE_PHYSICAL
;
737 sas_phy
->role
= PHY_ROLE_INITIATOR
;
738 sas_phy
->oob_mode
= OOB_NOT_CONNECTED
;
739 sas_phy
->linkrate
= SAS_LINK_RATE_UNKNOWN
;
740 sas_phy
->id
= phy_no
;
741 sas_phy
->sas_addr
= &hisi_hba
->sas_addr
[0];
742 sas_phy
->frame_rcvd
= &phy
->frame_rcvd
[0];
743 sas_phy
->ha
= (struct sas_ha_struct
*)hisi_hba
->shost
->hostdata
;
744 sas_phy
->lldd_phy
= phy
;
746 for (i
= 0; i
< HISI_PHYES_NUM
; i
++)
747 INIT_WORK(&phy
->works
[i
], hisi_sas_phye_fns
[i
]);
750 static void hisi_sas_port_notify_formed(struct asd_sas_phy
*sas_phy
)
752 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
753 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
754 struct hisi_sas_phy
*phy
= sas_phy
->lldd_phy
;
755 struct asd_sas_port
*sas_port
= sas_phy
->port
;
756 struct hisi_sas_port
*port
= to_hisi_sas_port(sas_port
);
762 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
763 port
->port_attached
= 1;
764 port
->id
= phy
->port_id
;
766 sas_port
->lldd_port
= port
;
767 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
770 static void hisi_sas_do_release_task(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
771 struct hisi_sas_slot
*slot
)
775 struct task_status_struct
*ts
;
777 ts
= &task
->task_status
;
779 ts
->resp
= SAS_TASK_COMPLETE
;
780 ts
->stat
= SAS_ABORTED_TASK
;
781 spin_lock_irqsave(&task
->task_state_lock
, flags
);
782 task
->task_state_flags
&=
783 ~(SAS_TASK_STATE_PENDING
| SAS_TASK_AT_INITIATOR
);
784 task
->task_state_flags
|= SAS_TASK_STATE_DONE
;
785 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
788 hisi_sas_slot_task_free(hisi_hba
, task
, slot
);
791 /* hisi_hba.lock should be locked */
792 static void hisi_sas_release_task(struct hisi_hba
*hisi_hba
,
793 struct domain_device
*device
)
795 struct hisi_sas_slot
*slot
, *slot2
;
796 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
798 list_for_each_entry_safe(slot
, slot2
, &sas_dev
->list
, entry
)
799 hisi_sas_do_release_task(hisi_hba
, slot
->task
, slot
);
802 void hisi_sas_release_tasks(struct hisi_hba
*hisi_hba
)
804 struct hisi_sas_device
*sas_dev
;
805 struct domain_device
*device
;
808 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
809 sas_dev
= &hisi_hba
->devices
[i
];
810 device
= sas_dev
->sas_device
;
812 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
) ||
816 hisi_sas_release_task(hisi_hba
, device
);
819 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks
);
821 static void hisi_sas_dereg_device(struct hisi_hba
*hisi_hba
,
822 struct domain_device
*device
)
824 if (hisi_hba
->hw
->dereg_device
)
825 hisi_hba
->hw
->dereg_device(hisi_hba
, device
);
828 static void hisi_sas_dev_gone(struct domain_device
*device
)
830 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
831 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
832 struct device
*dev
= hisi_hba
->dev
;
834 dev_info(dev
, "dev[%d:%x] is gone\n",
835 sas_dev
->device_id
, sas_dev
->dev_type
);
837 if (!test_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
)) {
838 hisi_sas_internal_task_abort(hisi_hba
, device
,
839 HISI_SAS_INT_ABT_DEV
, 0);
841 hisi_sas_dereg_device(hisi_hba
, device
);
843 hisi_hba
->hw
->clear_itct(hisi_hba
, sas_dev
);
844 device
->lldd_dev
= NULL
;
847 if (hisi_hba
->hw
->free_device
)
848 hisi_hba
->hw
->free_device(sas_dev
);
849 sas_dev
->dev_type
= SAS_PHY_UNUSED
;
852 static int hisi_sas_queue_command(struct sas_task
*task
, gfp_t gfp_flags
)
854 return hisi_sas_task_exec(task
, gfp_flags
, 0, NULL
);
857 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
860 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
861 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
862 int phy_no
= sas_phy
->id
;
865 case PHY_FUNC_HARD_RESET
:
866 hisi_hba
->hw
->phy_hard_reset(hisi_hba
, phy_no
);
869 case PHY_FUNC_LINK_RESET
:
870 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
872 hisi_hba
->hw
->phy_start(hisi_hba
, phy_no
);
875 case PHY_FUNC_DISABLE
:
876 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
879 case PHY_FUNC_SET_LINK_RATE
:
880 hisi_hba
->hw
->phy_set_linkrate(hisi_hba
, phy_no
, funcdata
);
882 case PHY_FUNC_GET_EVENTS
:
883 if (hisi_hba
->hw
->get_events
) {
884 hisi_hba
->hw
->get_events(hisi_hba
, phy_no
);
888 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
895 static void hisi_sas_task_done(struct sas_task
*task
)
897 if (!del_timer(&task
->slow_task
->timer
))
899 complete(&task
->slow_task
->completion
);
902 static void hisi_sas_tmf_timedout(struct timer_list
*t
)
904 struct sas_task_slow
*slow
= from_timer(slow
, t
, timer
);
905 struct sas_task
*task
= slow
->task
;
908 spin_lock_irqsave(&task
->task_state_lock
, flags
);
909 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
910 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
911 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
913 complete(&task
->slow_task
->completion
);
916 #define TASK_TIMEOUT 20
918 #define INTERNAL_ABORT_TIMEOUT 6
919 static int hisi_sas_exec_internal_tmf_task(struct domain_device
*device
,
920 void *parameter
, u32 para_len
,
921 struct hisi_sas_tmf_task
*tmf
)
923 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
924 struct hisi_hba
*hisi_hba
= sas_dev
->hisi_hba
;
925 struct device
*dev
= hisi_hba
->dev
;
926 struct sas_task
*task
;
929 for (retry
= 0; retry
< TASK_RETRY
; retry
++) {
930 task
= sas_alloc_slow_task(GFP_KERNEL
);
935 task
->task_proto
= device
->tproto
;
937 if (dev_is_sata(device
)) {
938 task
->ata_task
.device_control_reg_update
= 1;
939 memcpy(&task
->ata_task
.fis
, parameter
, para_len
);
941 memcpy(&task
->ssp_task
, parameter
, para_len
);
943 task
->task_done
= hisi_sas_task_done
;
945 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
946 task
->slow_task
->timer
.expires
= jiffies
+ TASK_TIMEOUT
*HZ
;
947 add_timer(&task
->slow_task
->timer
);
949 res
= hisi_sas_task_exec(task
, GFP_KERNEL
, 1, tmf
);
952 del_timer(&task
->slow_task
->timer
);
953 dev_err(dev
, "abort tmf: executing internal task failed: %d\n",
958 wait_for_completion(&task
->slow_task
->completion
);
959 res
= TMF_RESP_FUNC_FAILED
;
960 /* Even TMF timed out, return direct. */
961 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
962 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
963 struct hisi_sas_slot
*slot
= task
->lldd_task
;
965 dev_err(dev
, "abort tmf: TMF task timeout and not done\n");
971 dev_err(dev
, "abort tmf: TMF task timeout\n");
974 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
975 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
976 res
= TMF_RESP_FUNC_COMPLETE
;
980 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
981 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
982 res
= TMF_RESP_FUNC_SUCC
;
986 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
987 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
988 /* no error, but return the number of bytes of
991 dev_warn(dev
, "abort tmf: task to dev %016llx "
992 "resp: 0x%x sts 0x%x underrun\n",
993 SAS_ADDR(device
->sas_addr
),
994 task
->task_status
.resp
,
995 task
->task_status
.stat
);
996 res
= task
->task_status
.residual
;
1000 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1001 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
1002 dev_warn(dev
, "abort tmf: blocked task error\n");
1007 dev_warn(dev
, "abort tmf: task to dev "
1008 "%016llx resp: 0x%x status 0x%x\n",
1009 SAS_ADDR(device
->sas_addr
), task
->task_status
.resp
,
1010 task
->task_status
.stat
);
1011 sas_free_task(task
);
1015 if (retry
== TASK_RETRY
)
1016 dev_warn(dev
, "abort tmf: executing internal task failed!\n");
1017 sas_free_task(task
);
1021 static void hisi_sas_fill_ata_reset_cmd(struct ata_device
*dev
,
1022 bool reset
, int pmp
, u8
*fis
)
1024 struct ata_taskfile tf
;
1026 ata_tf_init(dev
, &tf
);
1030 tf
.ctl
&= ~ATA_SRST
;
1031 tf
.command
= ATA_CMD_DEV_RESET
;
1032 ata_tf_to_fis(&tf
, pmp
, 0, fis
);
1035 static int hisi_sas_softreset_ata_disk(struct domain_device
*device
)
1038 struct ata_port
*ap
= device
->sata_dev
.ap
;
1039 struct ata_link
*link
;
1040 int rc
= TMF_RESP_FUNC_FAILED
;
1041 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1042 struct device
*dev
= hisi_hba
->dev
;
1043 int s
= sizeof(struct host_to_dev_fis
);
1044 unsigned long flags
;
1046 ata_for_each_link(link
, ap
, EDGE
) {
1047 int pmp
= sata_srst_pmp(link
);
1049 hisi_sas_fill_ata_reset_cmd(link
->device
, 1, pmp
, fis
);
1050 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
, s
, NULL
);
1051 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1055 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1056 ata_for_each_link(link
, ap
, EDGE
) {
1057 int pmp
= sata_srst_pmp(link
);
1059 hisi_sas_fill_ata_reset_cmd(link
->device
, 0, pmp
, fis
);
1060 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
,
1062 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1063 dev_err(dev
, "ata disk de-reset failed\n");
1066 dev_err(dev
, "ata disk reset failed\n");
1069 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1070 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1071 hisi_sas_release_task(hisi_hba
, device
);
1072 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1078 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device
*device
,
1079 u8
*lun
, struct hisi_sas_tmf_task
*tmf
)
1081 struct sas_ssp_task ssp_task
;
1083 if (!(device
->tproto
& SAS_PROTOCOL_SSP
))
1084 return TMF_RESP_FUNC_ESUPP
;
1086 memcpy(ssp_task
.LUN
, lun
, 8);
1088 return hisi_sas_exec_internal_tmf_task(device
, &ssp_task
,
1089 sizeof(ssp_task
), tmf
);
1092 static void hisi_sas_refresh_port_id(struct hisi_hba
*hisi_hba
)
1094 u32 state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1097 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1098 struct hisi_sas_device
*sas_dev
= &hisi_hba
->devices
[i
];
1099 struct domain_device
*device
= sas_dev
->sas_device
;
1100 struct asd_sas_port
*sas_port
;
1101 struct hisi_sas_port
*port
;
1102 struct hisi_sas_phy
*phy
= NULL
;
1103 struct asd_sas_phy
*sas_phy
;
1105 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
)
1106 || !device
|| !device
->port
)
1109 sas_port
= device
->port
;
1110 port
= to_hisi_sas_port(sas_port
);
1112 list_for_each_entry(sas_phy
, &sas_port
->phy_list
, port_phy_el
)
1113 if (state
& BIT(sas_phy
->id
)) {
1114 phy
= sas_phy
->lldd_phy
;
1119 port
->id
= phy
->port_id
;
1121 /* Update linkrate of directly attached device. */
1122 if (!device
->parent
)
1123 device
->linkrate
= phy
->sas_phy
.linkrate
;
1125 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
1131 static void hisi_sas_rescan_topology(struct hisi_hba
*hisi_hba
, u32 old_state
,
1134 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1135 struct asd_sas_port
*_sas_port
= NULL
;
1138 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++) {
1139 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1140 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1141 struct asd_sas_port
*sas_port
= sas_phy
->port
;
1142 bool do_port_check
= !!(_sas_port
!= sas_port
);
1144 if (!sas_phy
->phy
->enabled
)
1147 /* Report PHY state change to libsas */
1148 if (state
& BIT(phy_no
)) {
1149 if (do_port_check
&& sas_port
&& sas_port
->port_dev
) {
1150 struct domain_device
*dev
= sas_port
->port_dev
;
1152 _sas_port
= sas_port
;
1154 if (DEV_IS_EXPANDER(dev
->dev_type
))
1155 sas_ha
->notify_port_event(sas_phy
,
1156 PORTE_BROADCAST_RCVD
);
1158 } else if (old_state
& (1 << phy_no
))
1159 /* PHY down but was up before */
1160 hisi_sas_phy_down(hisi_hba
, phy_no
, 0);
1165 static int hisi_sas_controller_reset(struct hisi_hba
*hisi_hba
)
1167 struct device
*dev
= hisi_hba
->dev
;
1168 struct Scsi_Host
*shost
= hisi_hba
->shost
;
1169 u32 old_state
, state
;
1170 unsigned long flags
;
1173 if (!hisi_hba
->hw
->soft_reset
)
1176 if (test_and_set_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
))
1179 dev_info(dev
, "controller resetting...\n");
1180 old_state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1182 scsi_block_requests(shost
);
1183 if (timer_pending(&hisi_hba
->timer
))
1184 del_timer_sync(&hisi_hba
->timer
);
1186 set_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1187 rc
= hisi_hba
->hw
->soft_reset(hisi_hba
);
1189 dev_warn(dev
, "controller reset failed (%d)\n", rc
);
1190 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1191 scsi_unblock_requests(shost
);
1194 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1195 hisi_sas_release_tasks(hisi_hba
);
1196 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1198 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1200 /* Init and wait for PHYs to come up and all libsas event finished. */
1201 hisi_hba
->hw
->phys_init(hisi_hba
);
1203 hisi_sas_refresh_port_id(hisi_hba
);
1204 scsi_unblock_requests(shost
);
1206 state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1207 hisi_sas_rescan_topology(hisi_hba
, old_state
, state
);
1208 dev_info(dev
, "controller reset complete\n");
1211 clear_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
);
1216 static int hisi_sas_abort_task(struct sas_task
*task
)
1218 struct scsi_lun lun
;
1219 struct hisi_sas_tmf_task tmf_task
;
1220 struct domain_device
*device
= task
->dev
;
1221 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1222 struct hisi_hba
*hisi_hba
;
1224 int rc
= TMF_RESP_FUNC_FAILED
;
1225 unsigned long flags
;
1228 return TMF_RESP_FUNC_FAILED
;
1230 hisi_hba
= dev_to_hisi_hba(task
->dev
);
1231 dev
= hisi_hba
->dev
;
1233 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1234 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
1235 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1236 rc
= TMF_RESP_FUNC_COMPLETE
;
1239 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
1240 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1242 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1243 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1244 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1245 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1246 u32 tag
= slot
->idx
;
1249 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1250 tmf_task
.tmf
= TMF_ABORT_TASK
;
1251 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1253 rc
= hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
,
1256 rc2
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1257 HISI_SAS_INT_ABT_CMD
, tag
);
1259 dev_err(dev
, "abort task: internal abort (%d)\n", rc2
);
1260 return TMF_RESP_FUNC_FAILED
;
1264 * If the TMF finds that the IO is not in the device and also
1265 * the internal abort does not succeed, then it is safe to
1267 * Note: if the internal abort succeeds then the slot
1268 * will have already been completed
1270 if (rc
== TMF_RESP_FUNC_COMPLETE
&& rc2
!= TMF_RESP_FUNC_SUCC
) {
1271 if (task
->lldd_task
) {
1272 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1273 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1274 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1277 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
1278 task
->task_proto
& SAS_PROTOCOL_STP
) {
1279 if (task
->dev
->dev_type
== SAS_SATA_DEV
) {
1280 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1281 HISI_SAS_INT_ABT_DEV
, 0);
1283 dev_err(dev
, "abort task: internal abort failed\n");
1286 hisi_sas_dereg_device(hisi_hba
, device
);
1287 rc
= hisi_sas_softreset_ata_disk(device
);
1289 } else if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SMP
) {
1291 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1292 u32 tag
= slot
->idx
;
1294 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1295 HISI_SAS_INT_ABT_CMD
, tag
);
1296 if (((rc
< 0) || (rc
== TMF_RESP_FUNC_FAILED
)) &&
1298 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1299 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1300 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1305 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1306 dev_notice(dev
, "abort task: rc=%d\n", rc
);
1310 static int hisi_sas_abort_task_set(struct domain_device
*device
, u8
*lun
)
1312 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1313 struct device
*dev
= hisi_hba
->dev
;
1314 struct hisi_sas_tmf_task tmf_task
;
1315 int rc
= TMF_RESP_FUNC_FAILED
;
1316 unsigned long flags
;
1318 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1319 HISI_SAS_INT_ABT_DEV
, 0);
1321 dev_err(dev
, "abort task set: internal abort rc=%d\n", rc
);
1322 return TMF_RESP_FUNC_FAILED
;
1324 hisi_sas_dereg_device(hisi_hba
, device
);
1326 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
1327 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1329 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1330 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1331 hisi_sas_release_task(hisi_hba
, device
);
1332 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1338 static int hisi_sas_clear_aca(struct domain_device
*device
, u8
*lun
)
1340 int rc
= TMF_RESP_FUNC_FAILED
;
1341 struct hisi_sas_tmf_task tmf_task
;
1343 tmf_task
.tmf
= TMF_CLEAR_ACA
;
1344 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1349 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device
*device
)
1351 struct sas_phy
*phy
= sas_get_local_phy(device
);
1352 int rc
, reset_type
= (device
->dev_type
== SAS_SATA_DEV
||
1353 (device
->tproto
& SAS_PROTOCOL_STP
)) ? 0 : 1;
1354 rc
= sas_phy_reset(phy
, reset_type
);
1355 sas_put_local_phy(phy
);
1360 static int hisi_sas_I_T_nexus_reset(struct domain_device
*device
)
1362 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1363 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1364 struct device
*dev
= hisi_hba
->dev
;
1365 int rc
= TMF_RESP_FUNC_FAILED
;
1366 unsigned long flags
;
1368 if (sas_dev
->dev_status
!= HISI_SAS_DEV_EH
)
1369 return TMF_RESP_FUNC_FAILED
;
1370 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
1372 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1373 HISI_SAS_INT_ABT_DEV
, 0);
1375 dev_err(dev
, "I_T nexus reset: internal abort (%d)\n", rc
);
1376 return TMF_RESP_FUNC_FAILED
;
1378 hisi_sas_dereg_device(hisi_hba
, device
);
1380 rc
= hisi_sas_debug_I_T_nexus_reset(device
);
1382 if ((rc
== TMF_RESP_FUNC_COMPLETE
) || (rc
== -ENODEV
)) {
1383 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1384 hisi_sas_release_task(hisi_hba
, device
);
1385 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1390 static int hisi_sas_lu_reset(struct domain_device
*device
, u8
*lun
)
1392 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1393 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1394 struct device
*dev
= hisi_hba
->dev
;
1395 unsigned long flags
;
1396 int rc
= TMF_RESP_FUNC_FAILED
;
1398 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1399 if (dev_is_sata(device
)) {
1400 struct sas_phy
*phy
;
1402 /* Clear internal IO and then hardreset */
1403 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1404 HISI_SAS_INT_ABT_DEV
, 0);
1406 dev_err(dev
, "lu_reset: internal abort failed\n");
1409 hisi_sas_dereg_device(hisi_hba
, device
);
1411 phy
= sas_get_local_phy(device
);
1413 rc
= sas_phy_reset(phy
, 1);
1416 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1417 hisi_sas_release_task(hisi_hba
, device
);
1418 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1420 sas_put_local_phy(phy
);
1422 struct hisi_sas_tmf_task tmf_task
= { .tmf
= TMF_LU_RESET
};
1424 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1425 HISI_SAS_INT_ABT_DEV
, 0);
1427 dev_err(dev
, "lu_reset: internal abort failed\n");
1430 hisi_sas_dereg_device(hisi_hba
, device
);
1432 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1433 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1434 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1435 hisi_sas_release_task(hisi_hba
, device
);
1436 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1440 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1441 dev_err(dev
, "lu_reset: for device[%d]:rc= %d\n",
1442 sas_dev
->device_id
, rc
);
1446 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct
*sas_ha
)
1448 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
1449 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r
);
1451 queue_work(hisi_hba
->wq
, &r
.work
);
1452 wait_for_completion(r
.completion
);
1454 return TMF_RESP_FUNC_COMPLETE
;
1456 return TMF_RESP_FUNC_FAILED
;
1459 static int hisi_sas_query_task(struct sas_task
*task
)
1461 struct scsi_lun lun
;
1462 struct hisi_sas_tmf_task tmf_task
;
1463 int rc
= TMF_RESP_FUNC_FAILED
;
1465 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1466 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1467 struct domain_device
*device
= task
->dev
;
1468 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1469 u32 tag
= slot
->idx
;
1471 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1472 tmf_task
.tmf
= TMF_QUERY_TASK
;
1473 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1475 rc
= hisi_sas_debug_issue_ssp_tmf(device
,
1479 /* The task is still in Lun, release it then */
1480 case TMF_RESP_FUNC_SUCC
:
1481 /* The task is not in Lun or failed, reset the phy */
1482 case TMF_RESP_FUNC_FAILED
:
1483 case TMF_RESP_FUNC_COMPLETE
:
1486 rc
= TMF_RESP_FUNC_FAILED
;
1494 hisi_sas_internal_abort_task_exec(struct hisi_hba
*hisi_hba
, int device_id
,
1495 struct sas_task
*task
, int abort_flag
,
1498 struct domain_device
*device
= task
->dev
;
1499 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1500 struct device
*dev
= hisi_hba
->dev
;
1501 struct hisi_sas_port
*port
;
1502 struct hisi_sas_slot
*slot
;
1503 struct asd_sas_port
*sas_port
= device
->port
;
1504 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
1505 struct hisi_sas_dq
*dq
= sas_dev
->dq
;
1506 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
1507 unsigned long flags
, flags_dq
;
1509 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
)))
1515 port
= to_hisi_sas_port(sas_port
);
1517 /* simply get a slot and send abort command */
1518 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1519 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
1521 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1524 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1526 slot
= &hisi_hba
->slot_info
[slot_idx
];
1527 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
1529 slot
->buf
= dma_pool_alloc(hisi_hba
->buffer_pool
,
1530 GFP_ATOMIC
, &slot
->buf_dma
);
1535 spin_lock_irqsave(&dq
->lock
, flags_dq
);
1536 rc
= hisi_hba
->hw
->get_free_slot(hisi_hba
, dq
);
1539 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
1543 dlvry_queue
= dq
->id
;
1544 dlvry_queue_slot
= dq
->wr_point
;
1546 slot
->idx
= slot_idx
;
1547 slot
->n_elem
= n_elem
;
1548 slot
->dlvry_queue
= dlvry_queue
;
1549 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
1550 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
1551 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
1554 slot
->is_internal
= true;
1555 task
->lldd_task
= slot
;
1557 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
1558 memset(hisi_sas_cmd_hdr_addr_mem(slot
), 0, HISI_SAS_COMMAND_TABLE_SZ
);
1559 memset(hisi_sas_status_buf_addr_mem(slot
), 0, HISI_SAS_STATUS_BUF_SZ
);
1561 hisi_sas_task_prep_abort(hisi_hba
, slot
, device_id
,
1562 abort_flag
, task_tag
);
1564 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1565 list_add_tail(&slot
->entry
, &sas_dev
->list
);
1566 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1567 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1568 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
1569 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1571 dq
->slot_prep
= slot
;
1573 /* send abort command to the chip */
1574 hisi_hba
->hw
->start_delivery(dq
);
1575 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
1580 dma_pool_free(hisi_hba
->buffer_pool
, slot
->buf
,
1583 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1584 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
1585 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1587 dev_err(dev
, "internal abort task prep: failed[%d]!\n", rc
);
1593 * hisi_sas_internal_task_abort -- execute an internal
1594 * abort command for single IO command or a device
1595 * @hisi_hba: host controller struct
1596 * @device: domain device
1597 * @abort_flag: mode of operation, device or single IO
1598 * @tag: tag of IO to be aborted (only relevant to single
1602 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
1603 struct domain_device
*device
,
1604 int abort_flag
, int tag
)
1606 struct sas_task
*task
;
1607 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1608 struct device
*dev
= hisi_hba
->dev
;
1612 * The interface is not realized means this HW don't support internal
1613 * abort, or don't need to do internal abort. Then here, we return
1614 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1615 * the internal abort has been executed and returned CQ.
1617 if (!hisi_hba
->hw
->prep_abort
)
1618 return TMF_RESP_FUNC_FAILED
;
1620 task
= sas_alloc_slow_task(GFP_KERNEL
);
1625 task
->task_proto
= device
->tproto
;
1626 task
->task_done
= hisi_sas_task_done
;
1627 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
1628 task
->slow_task
->timer
.expires
= jiffies
+ INTERNAL_ABORT_TIMEOUT
*HZ
;
1629 add_timer(&task
->slow_task
->timer
);
1631 res
= hisi_sas_internal_abort_task_exec(hisi_hba
, sas_dev
->device_id
,
1632 task
, abort_flag
, tag
);
1634 del_timer(&task
->slow_task
->timer
);
1635 dev_err(dev
, "internal task abort: executing internal task failed: %d\n",
1639 wait_for_completion(&task
->slow_task
->completion
);
1640 res
= TMF_RESP_FUNC_FAILED
;
1642 /* Internal abort timed out */
1643 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
1644 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
1645 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1649 dev_err(dev
, "internal task abort: timeout and not done.\n");
1653 dev_err(dev
, "internal task abort: timeout.\n");
1656 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1657 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
1658 res
= TMF_RESP_FUNC_COMPLETE
;
1662 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1663 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
1664 res
= TMF_RESP_FUNC_SUCC
;
1669 dev_dbg(dev
, "internal task abort: task to dev %016llx task=%p "
1670 "resp: 0x%x sts 0x%x\n",
1671 SAS_ADDR(device
->sas_addr
),
1673 task
->task_status
.resp
, /* 0 is complete, -1 is undelivered */
1674 task
->task_status
.stat
);
1675 sas_free_task(task
);
1680 static void hisi_sas_port_formed(struct asd_sas_phy
*sas_phy
)
1682 hisi_sas_port_notify_formed(sas_phy
);
1685 static void hisi_sas_port_deformed(struct asd_sas_phy
*sas_phy
)
1689 static int hisi_sas_write_gpio(struct sas_ha_struct
*sha
, u8 reg_type
,
1690 u8 reg_index
, u8 reg_count
, u8
*write_data
)
1692 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
1694 if (!hisi_hba
->hw
->write_gpio
)
1697 return hisi_hba
->hw
->write_gpio(hisi_hba
, reg_type
,
1698 reg_index
, reg_count
, write_data
);
1701 static void hisi_sas_phy_disconnected(struct hisi_sas_phy
*phy
)
1703 phy
->phy_attached
= 0;
1708 void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
)
1710 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1711 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1712 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1715 /* Phy down but ready */
1716 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
1717 hisi_sas_port_notify_formed(sas_phy
);
1719 struct hisi_sas_port
*port
= phy
->port
;
1721 /* Phy down and not ready */
1722 sas_ha
->notify_phy_event(sas_phy
, PHYE_LOSS_OF_SIGNAL
);
1723 sas_phy_disconnected(sas_phy
);
1726 if (phy
->phy_type
& PORT_TYPE_SAS
) {
1727 int port_id
= port
->id
;
1729 if (!hisi_hba
->hw
->get_wideport_bitmap(hisi_hba
,
1731 port
->port_attached
= 0;
1732 } else if (phy
->phy_type
& PORT_TYPE_SATA
)
1733 port
->port_attached
= 0;
1735 hisi_sas_phy_disconnected(phy
);
1738 EXPORT_SYMBOL_GPL(hisi_sas_phy_down
);
1740 void hisi_sas_kill_tasklets(struct hisi_hba
*hisi_hba
)
1744 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1745 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1747 tasklet_kill(&cq
->tasklet
);
1750 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets
);
1752 struct scsi_transport_template
*hisi_sas_stt
;
1753 EXPORT_SYMBOL_GPL(hisi_sas_stt
);
1755 static struct device_attribute
*host_attrs
[] = {
1756 &dev_attr_phy_event_threshold
,
1760 static struct scsi_host_template _hisi_sas_sht
= {
1761 .module
= THIS_MODULE
,
1763 .queuecommand
= sas_queuecommand
,
1764 .target_alloc
= sas_target_alloc
,
1765 .slave_configure
= hisi_sas_slave_configure
,
1766 .scan_finished
= hisi_sas_scan_finished
,
1767 .scan_start
= hisi_sas_scan_start
,
1768 .change_queue_depth
= sas_change_queue_depth
,
1769 .bios_param
= sas_bios_param
,
1772 .sg_tablesize
= SG_ALL
,
1773 .max_sectors
= SCSI_DEFAULT_MAX_SECTORS
,
1774 .use_clustering
= ENABLE_CLUSTERING
,
1775 .eh_device_reset_handler
= sas_eh_device_reset_handler
,
1776 .eh_target_reset_handler
= sas_eh_target_reset_handler
,
1777 .target_destroy
= sas_target_destroy
,
1779 .shost_attrs
= host_attrs
,
1781 struct scsi_host_template
*hisi_sas_sht
= &_hisi_sas_sht
;
1782 EXPORT_SYMBOL_GPL(hisi_sas_sht
);
1784 static struct sas_domain_function_template hisi_sas_transport_ops
= {
1785 .lldd_dev_found
= hisi_sas_dev_found
,
1786 .lldd_dev_gone
= hisi_sas_dev_gone
,
1787 .lldd_execute_task
= hisi_sas_queue_command
,
1788 .lldd_control_phy
= hisi_sas_control_phy
,
1789 .lldd_abort_task
= hisi_sas_abort_task
,
1790 .lldd_abort_task_set
= hisi_sas_abort_task_set
,
1791 .lldd_clear_aca
= hisi_sas_clear_aca
,
1792 .lldd_I_T_nexus_reset
= hisi_sas_I_T_nexus_reset
,
1793 .lldd_lu_reset
= hisi_sas_lu_reset
,
1794 .lldd_query_task
= hisi_sas_query_task
,
1795 .lldd_clear_nexus_ha
= hisi_sas_clear_nexus_ha
,
1796 .lldd_port_formed
= hisi_sas_port_formed
,
1797 .lldd_port_deformed
= hisi_sas_port_deformed
,
1798 .lldd_write_gpio
= hisi_sas_write_gpio
,
1801 void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
)
1803 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1805 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1806 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1807 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1809 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1810 memset(hisi_hba
->cmd_hdr
[i
], 0, s
);
1813 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1814 memset(hisi_hba
->complete_hdr
[i
], 0, s
);
1818 s
= sizeof(struct hisi_sas_initial_fis
) * hisi_hba
->n_phy
;
1819 memset(hisi_hba
->initial_fis
, 0, s
);
1821 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1822 memset(hisi_hba
->iost
, 0, s
);
1824 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1825 memset(hisi_hba
->breakpoint
, 0, s
);
1827 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1828 memset(hisi_hba
->sata_breakpoint
, 0, s
);
1830 EXPORT_SYMBOL_GPL(hisi_sas_init_mem
);
1832 int hisi_sas_alloc(struct hisi_hba
*hisi_hba
, struct Scsi_Host
*shost
)
1834 struct device
*dev
= hisi_hba
->dev
;
1835 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1837 spin_lock_init(&hisi_hba
->lock
);
1838 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1839 hisi_sas_phy_init(hisi_hba
, i
);
1840 hisi_hba
->port
[i
].port_attached
= 0;
1841 hisi_hba
->port
[i
].id
= -1;
1844 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1845 hisi_hba
->devices
[i
].dev_type
= SAS_PHY_UNUSED
;
1846 hisi_hba
->devices
[i
].device_id
= i
;
1847 hisi_hba
->devices
[i
].dev_status
= HISI_SAS_DEV_NORMAL
;
1850 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1851 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1852 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1854 /* Completion queue structure */
1856 cq
->hisi_hba
= hisi_hba
;
1858 /* Delivery queue structure */
1859 spin_lock_init(&dq
->lock
);
1861 dq
->hisi_hba
= hisi_hba
;
1863 /* Delivery queue */
1864 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1865 hisi_hba
->cmd_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1866 &hisi_hba
->cmd_hdr_dma
[i
], GFP_KERNEL
);
1867 if (!hisi_hba
->cmd_hdr
[i
])
1870 /* Completion queue */
1871 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1872 hisi_hba
->complete_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1873 &hisi_hba
->complete_hdr_dma
[i
], GFP_KERNEL
);
1874 if (!hisi_hba
->complete_hdr
[i
])
1878 s
= sizeof(struct hisi_sas_slot_buf_table
);
1879 hisi_hba
->buffer_pool
= dma_pool_create("dma_buffer", dev
, s
, 16, 0);
1880 if (!hisi_hba
->buffer_pool
)
1883 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1884 hisi_hba
->itct
= dma_alloc_coherent(dev
, s
, &hisi_hba
->itct_dma
,
1886 if (!hisi_hba
->itct
)
1889 memset(hisi_hba
->itct
, 0, s
);
1891 hisi_hba
->slot_info
= devm_kcalloc(dev
, max_command_entries
,
1892 sizeof(struct hisi_sas_slot
),
1894 if (!hisi_hba
->slot_info
)
1897 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1898 hisi_hba
->iost
= dma_alloc_coherent(dev
, s
, &hisi_hba
->iost_dma
,
1900 if (!hisi_hba
->iost
)
1903 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1904 hisi_hba
->breakpoint
= dma_alloc_coherent(dev
, s
,
1905 &hisi_hba
->breakpoint_dma
, GFP_KERNEL
);
1906 if (!hisi_hba
->breakpoint
)
1909 hisi_hba
->slot_index_count
= max_command_entries
;
1910 s
= hisi_hba
->slot_index_count
/ BITS_PER_BYTE
;
1911 hisi_hba
->slot_index_tags
= devm_kzalloc(dev
, s
, GFP_KERNEL
);
1912 if (!hisi_hba
->slot_index_tags
)
1915 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1916 hisi_hba
->initial_fis
= dma_alloc_coherent(dev
, s
,
1917 &hisi_hba
->initial_fis_dma
, GFP_KERNEL
);
1918 if (!hisi_hba
->initial_fis
)
1921 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1922 hisi_hba
->sata_breakpoint
= dma_alloc_coherent(dev
, s
,
1923 &hisi_hba
->sata_breakpoint_dma
, GFP_KERNEL
);
1924 if (!hisi_hba
->sata_breakpoint
)
1926 hisi_sas_init_mem(hisi_hba
);
1928 hisi_sas_slot_index_init(hisi_hba
);
1930 hisi_hba
->wq
= create_singlethread_workqueue(dev_name(dev
));
1931 if (!hisi_hba
->wq
) {
1932 dev_err(dev
, "sas_alloc: failed to create workqueue\n");
1940 EXPORT_SYMBOL_GPL(hisi_sas_alloc
);
1942 void hisi_sas_free(struct hisi_hba
*hisi_hba
)
1944 struct device
*dev
= hisi_hba
->dev
;
1945 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1947 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1948 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1949 if (hisi_hba
->cmd_hdr
[i
])
1950 dma_free_coherent(dev
, s
,
1951 hisi_hba
->cmd_hdr
[i
],
1952 hisi_hba
->cmd_hdr_dma
[i
]);
1954 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1955 if (hisi_hba
->complete_hdr
[i
])
1956 dma_free_coherent(dev
, s
,
1957 hisi_hba
->complete_hdr
[i
],
1958 hisi_hba
->complete_hdr_dma
[i
]);
1961 dma_pool_destroy(hisi_hba
->buffer_pool
);
1963 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1965 dma_free_coherent(dev
, s
,
1966 hisi_hba
->itct
, hisi_hba
->itct_dma
);
1968 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1970 dma_free_coherent(dev
, s
,
1971 hisi_hba
->iost
, hisi_hba
->iost_dma
);
1973 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1974 if (hisi_hba
->breakpoint
)
1975 dma_free_coherent(dev
, s
,
1976 hisi_hba
->breakpoint
,
1977 hisi_hba
->breakpoint_dma
);
1980 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1981 if (hisi_hba
->initial_fis
)
1982 dma_free_coherent(dev
, s
,
1983 hisi_hba
->initial_fis
,
1984 hisi_hba
->initial_fis_dma
);
1986 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1987 if (hisi_hba
->sata_breakpoint
)
1988 dma_free_coherent(dev
, s
,
1989 hisi_hba
->sata_breakpoint
,
1990 hisi_hba
->sata_breakpoint_dma
);
1993 destroy_workqueue(hisi_hba
->wq
);
1995 EXPORT_SYMBOL_GPL(hisi_sas_free
);
1997 void hisi_sas_rst_work_handler(struct work_struct
*work
)
1999 struct hisi_hba
*hisi_hba
=
2000 container_of(work
, struct hisi_hba
, rst_work
);
2002 hisi_sas_controller_reset(hisi_hba
);
2004 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler
);
2006 void hisi_sas_sync_rst_work_handler(struct work_struct
*work
)
2008 struct hisi_sas_rst
*rst
=
2009 container_of(work
, struct hisi_sas_rst
, work
);
2011 if (!hisi_sas_controller_reset(rst
->hisi_hba
))
2013 complete(rst
->completion
);
2015 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler
);
2017 int hisi_sas_get_fw_info(struct hisi_hba
*hisi_hba
)
2019 struct device
*dev
= hisi_hba
->dev
;
2020 struct platform_device
*pdev
= hisi_hba
->platform_dev
;
2021 struct device_node
*np
= pdev
? pdev
->dev
.of_node
: NULL
;
2024 if (device_property_read_u8_array(dev
, "sas-addr", hisi_hba
->sas_addr
,
2026 dev_err(dev
, "could not get property sas-addr\n");
2032 * These properties are only required for platform device-based
2033 * controller with DT firmware.
2035 hisi_hba
->ctrl
= syscon_regmap_lookup_by_phandle(np
,
2036 "hisilicon,sas-syscon");
2037 if (IS_ERR(hisi_hba
->ctrl
)) {
2038 dev_err(dev
, "could not get syscon\n");
2042 if (device_property_read_u32(dev
, "ctrl-reset-reg",
2043 &hisi_hba
->ctrl_reset_reg
)) {
2045 "could not get property ctrl-reset-reg\n");
2049 if (device_property_read_u32(dev
, "ctrl-reset-sts-reg",
2050 &hisi_hba
->ctrl_reset_sts_reg
)) {
2052 "could not get property ctrl-reset-sts-reg\n");
2056 if (device_property_read_u32(dev
, "ctrl-clock-ena-reg",
2057 &hisi_hba
->ctrl_clock_ena_reg
)) {
2059 "could not get property ctrl-clock-ena-reg\n");
2064 refclk
= devm_clk_get(dev
, NULL
);
2066 dev_dbg(dev
, "no ref clk property\n");
2068 hisi_hba
->refclk_frequency_mhz
= clk_get_rate(refclk
) / 1000000;
2070 if (device_property_read_u32(dev
, "phy-count", &hisi_hba
->n_phy
)) {
2071 dev_err(dev
, "could not get property phy-count\n");
2075 if (device_property_read_u32(dev
, "queue-count",
2076 &hisi_hba
->queue_count
)) {
2077 dev_err(dev
, "could not get property queue-count\n");
2083 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info
);
2085 static struct Scsi_Host
*hisi_sas_shost_alloc(struct platform_device
*pdev
,
2086 const struct hisi_sas_hw
*hw
)
2088 struct resource
*res
;
2089 struct Scsi_Host
*shost
;
2090 struct hisi_hba
*hisi_hba
;
2091 struct device
*dev
= &pdev
->dev
;
2093 shost
= scsi_host_alloc(hisi_sas_sht
, sizeof(*hisi_hba
));
2095 dev_err(dev
, "scsi host alloc failed\n");
2098 hisi_hba
= shost_priv(shost
);
2100 INIT_WORK(&hisi_hba
->rst_work
, hisi_sas_rst_work_handler
);
2102 hisi_hba
->dev
= dev
;
2103 hisi_hba
->platform_dev
= pdev
;
2104 hisi_hba
->shost
= shost
;
2105 SHOST_TO_SAS_HA(shost
) = &hisi_hba
->sha
;
2107 timer_setup(&hisi_hba
->timer
, NULL
, 0);
2109 if (hisi_sas_get_fw_info(hisi_hba
) < 0)
2112 if (dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64)) &&
2113 dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32))) {
2114 dev_err(dev
, "No usable DMA addressing method\n");
2118 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2119 hisi_hba
->regs
= devm_ioremap_resource(dev
, res
);
2120 if (IS_ERR(hisi_hba
->regs
))
2123 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2125 hisi_hba
->sgpio_regs
= devm_ioremap_resource(dev
, res
);
2126 if (IS_ERR(hisi_hba
->sgpio_regs
))
2130 if (hisi_sas_alloc(hisi_hba
, shost
)) {
2131 hisi_sas_free(hisi_hba
);
2137 scsi_host_put(shost
);
2138 dev_err(dev
, "shost alloc failed\n");
2142 void hisi_sas_init_add(struct hisi_hba
*hisi_hba
)
2146 for (i
= 0; i
< hisi_hba
->n_phy
; i
++)
2147 memcpy(&hisi_hba
->phy
[i
].dev_sas_addr
,
2151 EXPORT_SYMBOL_GPL(hisi_sas_init_add
);
2153 int hisi_sas_probe(struct platform_device
*pdev
,
2154 const struct hisi_sas_hw
*hw
)
2156 struct Scsi_Host
*shost
;
2157 struct hisi_hba
*hisi_hba
;
2158 struct device
*dev
= &pdev
->dev
;
2159 struct asd_sas_phy
**arr_phy
;
2160 struct asd_sas_port
**arr_port
;
2161 struct sas_ha_struct
*sha
;
2162 int rc
, phy_nr
, port_nr
, i
;
2164 shost
= hisi_sas_shost_alloc(pdev
, hw
);
2168 sha
= SHOST_TO_SAS_HA(shost
);
2169 hisi_hba
= shost_priv(shost
);
2170 platform_set_drvdata(pdev
, sha
);
2172 phy_nr
= port_nr
= hisi_hba
->n_phy
;
2174 arr_phy
= devm_kcalloc(dev
, phy_nr
, sizeof(void *), GFP_KERNEL
);
2175 arr_port
= devm_kcalloc(dev
, port_nr
, sizeof(void *), GFP_KERNEL
);
2176 if (!arr_phy
|| !arr_port
) {
2181 sha
->sas_phy
= arr_phy
;
2182 sha
->sas_port
= arr_port
;
2183 sha
->lldd_ha
= hisi_hba
;
2185 shost
->transportt
= hisi_sas_stt
;
2186 shost
->max_id
= HISI_SAS_MAX_DEVICES
;
2187 shost
->max_lun
= ~0;
2188 shost
->max_channel
= 1;
2189 shost
->max_cmd_len
= 16;
2190 shost
->sg_tablesize
= min_t(u16
, SG_ALL
, HISI_SAS_SGE_PAGE_CNT
);
2191 shost
->can_queue
= hisi_hba
->hw
->max_command_entries
;
2192 shost
->cmd_per_lun
= hisi_hba
->hw
->max_command_entries
;
2194 sha
->sas_ha_name
= DRV_NAME
;
2195 sha
->dev
= hisi_hba
->dev
;
2196 sha
->lldd_module
= THIS_MODULE
;
2197 sha
->sas_addr
= &hisi_hba
->sas_addr
[0];
2198 sha
->num_phys
= hisi_hba
->n_phy
;
2199 sha
->core
.shost
= hisi_hba
->shost
;
2201 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
2202 sha
->sas_phy
[i
] = &hisi_hba
->phy
[i
].sas_phy
;
2203 sha
->sas_port
[i
] = &hisi_hba
->port
[i
].sas_port
;
2206 hisi_sas_init_add(hisi_hba
);
2208 rc
= scsi_add_host(shost
, &pdev
->dev
);
2212 rc
= sas_register_ha(sha
);
2214 goto err_out_register_ha
;
2216 rc
= hisi_hba
->hw
->hw_init(hisi_hba
);
2218 goto err_out_register_ha
;
2220 scsi_scan_host(shost
);
2224 err_out_register_ha
:
2225 scsi_remove_host(shost
);
2227 hisi_sas_free(hisi_hba
);
2228 scsi_host_put(shost
);
2231 EXPORT_SYMBOL_GPL(hisi_sas_probe
);
2233 int hisi_sas_remove(struct platform_device
*pdev
)
2235 struct sas_ha_struct
*sha
= platform_get_drvdata(pdev
);
2236 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
2237 struct Scsi_Host
*shost
= sha
->core
.shost
;
2239 if (timer_pending(&hisi_hba
->timer
))
2240 del_timer(&hisi_hba
->timer
);
2242 sas_unregister_ha(sha
);
2243 sas_remove_host(sha
->core
.shost
);
2245 hisi_sas_free(hisi_hba
);
2246 scsi_host_put(shost
);
2249 EXPORT_SYMBOL_GPL(hisi_sas_remove
);
2251 static __init
int hisi_sas_init(void)
2253 hisi_sas_stt
= sas_domain_attach_transport(&hisi_sas_transport_ops
);
2260 static __exit
void hisi_sas_exit(void)
2262 sas_release_transport(hisi_sas_stt
);
2265 module_init(hisi_sas_init
);
2266 module_exit(hisi_sas_exit
);
2268 MODULE_LICENSE("GPL");
2269 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2270 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2271 MODULE_ALIAS("platform:" DRV_NAME
);