1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2015 Linaro Ltd.
4 * Copyright (c) 2015 Hisilicon Limited.
8 #define DRV_NAME "hisi_sas"
10 #define DEV_IS_GONE(dev) \
11 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
13 static int hisi_sas_softreset_ata_disk(struct domain_device
*device
);
14 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
16 static void hisi_sas_release_task(struct hisi_hba
*hisi_hba
,
17 struct domain_device
*device
);
18 static void hisi_sas_dev_gone(struct domain_device
*device
);
20 struct hisi_sas_internal_abort_data
{
21 bool rst_ha_timeout
; /* reset the HA for timeout */
24 u8
hisi_sas_get_ata_protocol(struct host_to_dev_fis
*fis
, int direction
)
26 switch (fis
->command
) {
27 case ATA_CMD_FPDMA_WRITE
:
28 case ATA_CMD_FPDMA_READ
:
29 case ATA_CMD_FPDMA_RECV
:
30 case ATA_CMD_FPDMA_SEND
:
31 case ATA_CMD_NCQ_NON_DATA
:
32 return HISI_SAS_SATA_PROTOCOL_FPDMA
;
34 case ATA_CMD_DOWNLOAD_MICRO
:
36 case ATA_CMD_PMP_READ
:
37 case ATA_CMD_READ_LOG_EXT
:
38 case ATA_CMD_PIO_READ
:
39 case ATA_CMD_PIO_READ_EXT
:
40 case ATA_CMD_PMP_WRITE
:
41 case ATA_CMD_WRITE_LOG_EXT
:
42 case ATA_CMD_PIO_WRITE
:
43 case ATA_CMD_PIO_WRITE_EXT
:
44 return HISI_SAS_SATA_PROTOCOL_PIO
;
47 case ATA_CMD_DOWNLOAD_MICRO_DMA
:
48 case ATA_CMD_PMP_READ_DMA
:
49 case ATA_CMD_PMP_WRITE_DMA
:
51 case ATA_CMD_READ_EXT
:
52 case ATA_CMD_READ_LOG_DMA_EXT
:
53 case ATA_CMD_READ_STREAM_DMA_EXT
:
54 case ATA_CMD_TRUSTED_RCV_DMA
:
55 case ATA_CMD_TRUSTED_SND_DMA
:
57 case ATA_CMD_WRITE_EXT
:
58 case ATA_CMD_WRITE_FUA_EXT
:
59 case ATA_CMD_WRITE_QUEUED
:
60 case ATA_CMD_WRITE_LOG_DMA_EXT
:
61 case ATA_CMD_WRITE_STREAM_DMA_EXT
:
62 case ATA_CMD_ZAC_MGMT_IN
:
63 return HISI_SAS_SATA_PROTOCOL_DMA
;
65 case ATA_CMD_CHK_POWER
:
66 case ATA_CMD_DEV_RESET
:
69 case ATA_CMD_FLUSH_EXT
:
71 case ATA_CMD_VERIFY_EXT
:
72 case ATA_CMD_SET_FEATURES
:
74 case ATA_CMD_STANDBYNOW1
:
75 case ATA_CMD_ZAC_MGMT_OUT
:
76 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
79 switch (fis
->features
) {
80 case ATA_SET_MAX_PASSWD
:
81 case ATA_SET_MAX_LOCK
:
82 return HISI_SAS_SATA_PROTOCOL_PIO
;
84 case ATA_SET_MAX_PASSWD_DMA
:
85 case ATA_SET_MAX_UNLOCK_DMA
:
86 return HISI_SAS_SATA_PROTOCOL_DMA
;
89 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
94 if (direction
== DMA_NONE
)
95 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
96 return HISI_SAS_SATA_PROTOCOL_PIO
;
100 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol
);
102 void hisi_sas_sata_done(struct sas_task
*task
,
103 struct hisi_sas_slot
*slot
)
105 struct task_status_struct
*ts
= &task
->task_status
;
106 struct ata_task_resp
*resp
= (struct ata_task_resp
*)ts
->buf
;
107 struct hisi_sas_status_buffer
*status_buf
=
108 hisi_sas_status_buf_addr_mem(slot
);
109 u8
*iu
= &status_buf
->iu
[0];
110 struct dev_to_host_fis
*d2h
= (struct dev_to_host_fis
*)iu
;
112 resp
->frame_len
= sizeof(struct dev_to_host_fis
);
113 memcpy(&resp
->ending_fis
[0], d2h
, sizeof(struct dev_to_host_fis
));
115 ts
->buf_valid_size
= sizeof(*resp
);
117 EXPORT_SYMBOL_GPL(hisi_sas_sata_done
);
120 * This function assumes linkrate mask fits in 8 bits, which it
121 * does for all HW versions supported.
123 u8
hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max
)
128 max
-= SAS_LINK_RATE_1_5_GBPS
;
129 for (i
= 0; i
<= max
; i
++)
130 rate
|= 1 << (i
* 2);
133 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask
);
135 static struct hisi_hba
*dev_to_hisi_hba(struct domain_device
*device
)
137 return device
->port
->ha
->lldd_ha
;
140 struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
)
142 return container_of(sas_port
, struct hisi_sas_port
, sas_port
);
144 EXPORT_SYMBOL_GPL(to_hisi_sas_port
);
146 void hisi_sas_stop_phys(struct hisi_hba
*hisi_hba
)
150 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++)
151 hisi_sas_phy_enable(hisi_hba
, phy_no
, 0);
153 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys
);
155 static void hisi_sas_slot_index_clear(struct hisi_hba
*hisi_hba
, int slot_idx
)
157 void *bitmap
= hisi_hba
->slot_index_tags
;
159 __clear_bit(slot_idx
, bitmap
);
162 static void hisi_sas_slot_index_free(struct hisi_hba
*hisi_hba
, int slot_idx
)
164 if (hisi_hba
->hw
->slot_index_alloc
||
165 slot_idx
< HISI_SAS_RESERVED_IPTT
) {
166 spin_lock(&hisi_hba
->lock
);
167 hisi_sas_slot_index_clear(hisi_hba
, slot_idx
);
168 spin_unlock(&hisi_hba
->lock
);
172 static void hisi_sas_slot_index_set(struct hisi_hba
*hisi_hba
, int slot_idx
)
174 void *bitmap
= hisi_hba
->slot_index_tags
;
176 __set_bit(slot_idx
, bitmap
);
179 static int hisi_sas_slot_index_alloc(struct hisi_hba
*hisi_hba
,
183 void *bitmap
= hisi_hba
->slot_index_tags
;
186 return rq
->tag
+ HISI_SAS_RESERVED_IPTT
;
188 spin_lock(&hisi_hba
->lock
);
189 index
= find_next_zero_bit(bitmap
, HISI_SAS_RESERVED_IPTT
,
190 hisi_hba
->last_slot_index
+ 1);
191 if (index
>= HISI_SAS_RESERVED_IPTT
) {
192 index
= find_next_zero_bit(bitmap
,
193 HISI_SAS_RESERVED_IPTT
,
195 if (index
>= HISI_SAS_RESERVED_IPTT
) {
196 spin_unlock(&hisi_hba
->lock
);
197 return -SAS_QUEUE_FULL
;
200 hisi_sas_slot_index_set(hisi_hba
, index
);
201 hisi_hba
->last_slot_index
= index
;
202 spin_unlock(&hisi_hba
->lock
);
207 void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
208 struct hisi_sas_slot
*slot
)
210 int device_id
= slot
->device_id
;
211 struct hisi_sas_device
*sas_dev
= &hisi_hba
->devices
[device_id
];
214 struct device
*dev
= hisi_hba
->dev
;
216 if (!task
->lldd_task
)
219 task
->lldd_task
= NULL
;
221 if (!sas_protocol_ata(task
->task_proto
)) {
223 if (task
->task_proto
& SAS_PROTOCOL_SSP
)
224 dma_unmap_sg(dev
, task
->scatter
,
228 dma_unmap_sg(dev
, &task
->smp_task
.smp_req
,
231 if (slot
->n_elem_dif
) {
232 struct sas_ssp_task
*ssp_task
= &task
->ssp_task
;
233 struct scsi_cmnd
*scsi_cmnd
= ssp_task
->cmd
;
235 dma_unmap_sg(dev
, scsi_prot_sglist(scsi_cmnd
),
236 scsi_prot_sg_count(scsi_cmnd
),
242 spin_lock(&sas_dev
->lock
);
243 list_del_init(&slot
->entry
);
244 spin_unlock(&sas_dev
->lock
);
246 memset(slot
, 0, offsetof(struct hisi_sas_slot
, buf
));
248 hisi_sas_slot_index_free(hisi_hba
, slot
->idx
);
250 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free
);
252 static void hisi_sas_task_prep_smp(struct hisi_hba
*hisi_hba
,
253 struct hisi_sas_slot
*slot
)
255 hisi_hba
->hw
->prep_smp(hisi_hba
, slot
);
258 static void hisi_sas_task_prep_ssp(struct hisi_hba
*hisi_hba
,
259 struct hisi_sas_slot
*slot
)
261 hisi_hba
->hw
->prep_ssp(hisi_hba
, slot
);
264 static void hisi_sas_task_prep_ata(struct hisi_hba
*hisi_hba
,
265 struct hisi_sas_slot
*slot
)
267 hisi_hba
->hw
->prep_stp(hisi_hba
, slot
);
270 static void hisi_sas_task_prep_abort(struct hisi_hba
*hisi_hba
,
271 struct hisi_sas_slot
*slot
)
273 hisi_hba
->hw
->prep_abort(hisi_hba
, slot
);
276 static void hisi_sas_dma_unmap(struct hisi_hba
*hisi_hba
,
277 struct sas_task
*task
, int n_elem
)
279 struct device
*dev
= hisi_hba
->dev
;
281 if (!sas_protocol_ata(task
->task_proto
) && n_elem
) {
282 if (task
->num_scatter
) {
283 dma_unmap_sg(dev
, task
->scatter
, task
->num_scatter
,
285 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
286 dma_unmap_sg(dev
, &task
->smp_task
.smp_req
,
292 static int hisi_sas_dma_map(struct hisi_hba
*hisi_hba
,
293 struct sas_task
*task
, int *n_elem
)
295 struct device
*dev
= hisi_hba
->dev
;
298 if (sas_protocol_ata(task
->task_proto
)) {
299 *n_elem
= task
->num_scatter
;
301 unsigned int req_len
;
303 if (task
->num_scatter
) {
304 *n_elem
= dma_map_sg(dev
, task
->scatter
,
305 task
->num_scatter
, task
->data_dir
);
310 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
311 *n_elem
= dma_map_sg(dev
, &task
->smp_task
.smp_req
,
317 req_len
= sg_dma_len(&task
->smp_task
.smp_req
);
320 goto err_out_dma_unmap
;
325 if (*n_elem
> HISI_SAS_SGE_PAGE_CNT
) {
326 dev_err(dev
, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT\n",
329 goto err_out_dma_unmap
;
334 /* It would be better to call dma_unmap_sg() here, but it's messy */
335 hisi_sas_dma_unmap(hisi_hba
, task
, *n_elem
);
340 static void hisi_sas_dif_dma_unmap(struct hisi_hba
*hisi_hba
,
341 struct sas_task
*task
, int n_elem_dif
)
343 struct device
*dev
= hisi_hba
->dev
;
346 struct sas_ssp_task
*ssp_task
= &task
->ssp_task
;
347 struct scsi_cmnd
*scsi_cmnd
= ssp_task
->cmd
;
349 dma_unmap_sg(dev
, scsi_prot_sglist(scsi_cmnd
),
350 scsi_prot_sg_count(scsi_cmnd
),
355 static int hisi_sas_dif_dma_map(struct hisi_hba
*hisi_hba
,
356 int *n_elem_dif
, struct sas_task
*task
)
358 struct device
*dev
= hisi_hba
->dev
;
359 struct sas_ssp_task
*ssp_task
;
360 struct scsi_cmnd
*scsi_cmnd
;
363 if (task
->num_scatter
) {
364 ssp_task
= &task
->ssp_task
;
365 scsi_cmnd
= ssp_task
->cmd
;
367 if (scsi_prot_sg_count(scsi_cmnd
)) {
368 *n_elem_dif
= dma_map_sg(dev
,
369 scsi_prot_sglist(scsi_cmnd
),
370 scsi_prot_sg_count(scsi_cmnd
),
376 if (*n_elem_dif
> HISI_SAS_SGE_DIF_PAGE_CNT
) {
377 dev_err(dev
, "task prep: n_elem_dif(%d) too large\n",
380 goto err_out_dif_dma_unmap
;
387 err_out_dif_dma_unmap
:
388 dma_unmap_sg(dev
, scsi_prot_sglist(scsi_cmnd
),
389 scsi_prot_sg_count(scsi_cmnd
), task
->data_dir
);
394 void hisi_sas_task_deliver(struct hisi_hba
*hisi_hba
,
395 struct hisi_sas_slot
*slot
,
396 struct hisi_sas_dq
*dq
,
397 struct hisi_sas_device
*sas_dev
)
399 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
400 int dlvry_queue_slot
, dlvry_queue
;
401 struct sas_task
*task
= slot
->task
;
404 spin_lock(&dq
->lock
);
405 wr_q_index
= dq
->wr_point
;
406 dq
->wr_point
= (dq
->wr_point
+ 1) % HISI_SAS_QUEUE_SLOTS
;
407 list_add_tail(&slot
->delivery
, &dq
->list
);
408 spin_unlock(&dq
->lock
);
409 spin_lock(&sas_dev
->lock
);
410 list_add_tail(&slot
->entry
, &sas_dev
->list
);
411 spin_unlock(&sas_dev
->lock
);
413 dlvry_queue
= dq
->id
;
414 dlvry_queue_slot
= wr_q_index
;
416 slot
->device_id
= sas_dev
->device_id
;
417 slot
->dlvry_queue
= dlvry_queue
;
418 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
419 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
420 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
422 task
->lldd_task
= slot
;
424 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
425 memset(hisi_sas_cmd_hdr_addr_mem(slot
), 0, HISI_SAS_COMMAND_TABLE_SZ
);
426 memset(hisi_sas_status_buf_addr_mem(slot
), 0,
427 sizeof(struct hisi_sas_err_record
));
429 switch (task
->task_proto
) {
430 case SAS_PROTOCOL_SMP
:
431 hisi_sas_task_prep_smp(hisi_hba
, slot
);
433 case SAS_PROTOCOL_SSP
:
434 hisi_sas_task_prep_ssp(hisi_hba
, slot
);
436 case SAS_PROTOCOL_SATA
:
437 case SAS_PROTOCOL_STP
:
438 case SAS_PROTOCOL_STP_ALL
:
439 hisi_sas_task_prep_ata(hisi_hba
, slot
);
441 case SAS_PROTOCOL_INTERNAL_ABORT
:
442 hisi_sas_task_prep_abort(hisi_hba
, slot
);
448 /* Make slot memories observable before marking as ready */
450 WRITE_ONCE(slot
->ready
, 1);
452 spin_lock(&dq
->lock
);
453 hisi_hba
->hw
->start_delivery(dq
);
454 spin_unlock(&dq
->lock
);
457 static int hisi_sas_queue_command(struct sas_task
*task
, gfp_t gfp_flags
)
459 int n_elem
= 0, n_elem_dif
= 0;
460 struct domain_device
*device
= task
->dev
;
461 struct asd_sas_port
*sas_port
= device
->port
;
462 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
463 bool internal_abort
= sas_is_internal_abort(task
);
464 struct hisi_sas_dq
*dq
= NULL
;
465 struct hisi_sas_port
*port
;
466 struct hisi_hba
*hisi_hba
;
467 struct hisi_sas_slot
*slot
;
468 struct request
*rq
= NULL
;
473 struct task_status_struct
*ts
= &task
->task_status
;
475 ts
->resp
= SAS_TASK_UNDELIVERED
;
476 ts
->stat
= SAS_PHY_DOWN
;
478 * libsas will use dev->port, should
479 * not call task_done for sata
481 if (device
->dev_type
!= SAS_SATA_DEV
&& !internal_abort
)
482 task
->task_done(task
);
486 hisi_hba
= dev_to_hisi_hba(device
);
489 switch (task
->task_proto
) {
490 case SAS_PROTOCOL_SSP
:
491 case SAS_PROTOCOL_SMP
:
492 case SAS_PROTOCOL_SATA
:
493 case SAS_PROTOCOL_STP
:
494 case SAS_PROTOCOL_STP_ALL
:
495 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
))) {
496 if (!gfpflags_allow_blocking(gfp_flags
))
499 down(&hisi_hba
->sem
);
503 if (DEV_IS_GONE(sas_dev
)) {
505 dev_info(dev
, "task prep: device %d not ready\n",
508 dev_info(dev
, "task prep: device %016llx not ready\n",
509 SAS_ADDR(device
->sas_addr
));
514 port
= to_hisi_sas_port(sas_port
);
515 if (!port
->port_attached
) {
516 dev_info(dev
, "task prep: %s port%d not attach device\n",
517 dev_is_sata(device
) ? "SATA/STP" : "SAS",
523 rq
= sas_task_find_rq(task
);
525 unsigned int dq_index
;
528 blk_tag
= blk_mq_unique_tag(rq
);
529 dq_index
= blk_mq_unique_tag_to_hwq(blk_tag
);
530 dq
= &hisi_hba
->dq
[dq_index
];
532 struct Scsi_Host
*shost
= hisi_hba
->shost
;
533 struct blk_mq_queue_map
*qmap
= &shost
->tag_set
.map
[HCTX_TYPE_DEFAULT
];
534 int queue
= qmap
->mq_map
[raw_smp_processor_id()];
536 dq
= &hisi_hba
->dq
[queue
];
539 case SAS_PROTOCOL_INTERNAL_ABORT
:
540 if (!hisi_hba
->hw
->prep_abort
)
541 return TMF_RESP_FUNC_FAILED
;
543 if (test_bit(HISI_SAS_HW_FAULT_BIT
, &hisi_hba
->flags
))
546 hisi_hba
= dev_to_hisi_hba(device
);
548 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
)))
551 port
= to_hisi_sas_port(sas_port
);
552 dq
= &hisi_hba
->dq
[task
->abort_task
.qid
];
555 dev_err(hisi_hba
->dev
, "task prep: unknown/unsupported proto (0x%x)\n",
560 rc
= hisi_sas_dma_map(hisi_hba
, task
, &n_elem
);
564 if (!sas_protocol_ata(task
->task_proto
)) {
565 rc
= hisi_sas_dif_dma_map(hisi_hba
, &n_elem_dif
, task
);
567 goto err_out_dma_unmap
;
570 if (!internal_abort
&& hisi_hba
->hw
->slot_index_alloc
)
571 rc
= hisi_hba
->hw
->slot_index_alloc(hisi_hba
, device
);
573 rc
= hisi_sas_slot_index_alloc(hisi_hba
, rq
);
576 goto err_out_dif_dma_unmap
;
578 slot
= &hisi_hba
->slot_info
[rc
];
579 slot
->n_elem
= n_elem
;
580 slot
->n_elem_dif
= n_elem_dif
;
584 slot
->tmf
= task
->tmf
;
585 slot
->is_internal
= !!task
->tmf
|| internal_abort
;
587 /* protect task_prep and start_delivery sequence */
588 hisi_sas_task_deliver(hisi_hba
, slot
, dq
, sas_dev
);
592 err_out_dif_dma_unmap
:
593 if (!sas_protocol_ata(task
->task_proto
))
594 hisi_sas_dif_dma_unmap(hisi_hba
, task
, n_elem_dif
);
596 hisi_sas_dma_unmap(hisi_hba
, task
, n_elem
);
598 dev_err(dev
, "task exec: failed[%d]!\n", rc
);
602 static void hisi_sas_bytes_dmaed(struct hisi_hba
*hisi_hba
, int phy_no
,
605 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
606 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
608 if (!phy
->phy_attached
)
611 sas_notify_phy_event(sas_phy
, PHYE_OOB_DONE
, gfp_flags
);
614 struct sas_phy
*sphy
= sas_phy
->phy
;
616 sphy
->negotiated_linkrate
= sas_phy
->linkrate
;
617 sphy
->minimum_linkrate_hw
= SAS_LINK_RATE_1_5_GBPS
;
618 sphy
->maximum_linkrate_hw
=
619 hisi_hba
->hw
->phy_get_max_linkrate();
620 if (sphy
->minimum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
621 sphy
->minimum_linkrate
= phy
->minimum_linkrate
;
623 if (sphy
->maximum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
624 sphy
->maximum_linkrate
= phy
->maximum_linkrate
;
627 if (phy
->phy_type
& PORT_TYPE_SAS
) {
628 struct sas_identify_frame
*id
;
630 id
= (struct sas_identify_frame
*)phy
->frame_rcvd
;
631 id
->dev_type
= phy
->identify
.device_type
;
632 id
->initiator_bits
= SAS_PROTOCOL_ALL
;
633 id
->target_bits
= phy
->identify
.target_port_protocols
;
634 } else if (phy
->phy_type
& PORT_TYPE_SATA
) {
638 sas_phy
->frame_rcvd_size
= phy
->frame_rcvd_size
;
639 sas_notify_port_event(sas_phy
, PORTE_BYTES_DMAED
, gfp_flags
);
642 static struct hisi_sas_device
*hisi_sas_alloc_dev(struct domain_device
*device
)
644 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
645 struct hisi_sas_device
*sas_dev
= NULL
;
646 int last
= hisi_hba
->last_dev_id
;
647 int first
= (hisi_hba
->last_dev_id
+ 1) % HISI_SAS_MAX_DEVICES
;
650 spin_lock(&hisi_hba
->lock
);
651 for (i
= first
; i
!= last
; i
%= HISI_SAS_MAX_DEVICES
) {
652 if (hisi_hba
->devices
[i
].dev_type
== SAS_PHY_UNUSED
) {
653 int queue
= i
% hisi_hba
->queue_count
;
654 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[queue
];
656 hisi_hba
->devices
[i
].device_id
= i
;
657 sas_dev
= &hisi_hba
->devices
[i
];
658 sas_dev
->dev_status
= HISI_SAS_DEV_INIT
;
659 sas_dev
->dev_type
= device
->dev_type
;
660 sas_dev
->hisi_hba
= hisi_hba
;
661 sas_dev
->sas_device
= device
;
663 spin_lock_init(&sas_dev
->lock
);
664 INIT_LIST_HEAD(&hisi_hba
->devices
[i
].list
);
669 hisi_hba
->last_dev_id
= i
;
670 spin_unlock(&hisi_hba
->lock
);
675 static void hisi_sas_tmf_aborted(struct sas_task
*task
)
677 struct hisi_sas_slot
*slot
= task
->lldd_task
;
678 struct domain_device
*device
= task
->dev
;
679 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
680 struct hisi_hba
*hisi_hba
= sas_dev
->hisi_hba
;
683 struct hisi_sas_cq
*cq
=
684 &hisi_hba
->cq
[slot
->dlvry_queue
];
686 * sync irq to avoid free'ing task
687 * before using task in IO completion
689 synchronize_irq(cq
->irq_no
);
694 #define HISI_SAS_DISK_RECOVER_CNT 3
695 static int hisi_sas_init_device(struct domain_device
*device
)
697 int rc
= TMF_RESP_FUNC_COMPLETE
;
699 int retry
= HISI_SAS_DISK_RECOVER_CNT
;
700 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
702 switch (device
->dev_type
) {
704 int_to_scsilun(0, &lun
);
706 while (retry
-- > 0) {
707 rc
= sas_abort_task_set(device
, lun
.scsi_lun
);
708 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
709 hisi_sas_release_task(hisi_hba
, device
);
716 case SAS_SATA_PM_PORT
:
717 case SAS_SATA_PENDING
:
719 * If an expander is swapped when a SATA disk is attached then
720 * we should issue a hard reset to clear previous affiliation
721 * of STP target port, see SPL (chapter 6.19.4).
723 * However we don't need to issue a hard reset here for these
725 * a. When probing the device, libsas/libata already issues a
726 * hard reset in sas_probe_sata() -> ata_sas_async_probe().
727 * Note that in hisi_sas_debug_I_T_nexus_reset() we take care
728 * to issue a hard reset by checking the dev status (== INIT).
729 * b. When resetting the controller, this is simply unnecessary.
731 while (retry
-- > 0) {
732 rc
= hisi_sas_softreset_ata_disk(device
);
744 int hisi_sas_slave_alloc(struct scsi_device
*sdev
)
746 struct domain_device
*ddev
= sdev_to_domain_dev(sdev
);
747 struct hisi_sas_device
*sas_dev
= ddev
->lldd_dev
;
750 rc
= sas_slave_alloc(sdev
);
754 rc
= hisi_sas_init_device(ddev
);
757 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
760 EXPORT_SYMBOL_GPL(hisi_sas_slave_alloc
);
762 static int hisi_sas_dev_found(struct domain_device
*device
)
764 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
765 struct domain_device
*parent_dev
= device
->parent
;
766 struct hisi_sas_device
*sas_dev
;
767 struct device
*dev
= hisi_hba
->dev
;
770 if (hisi_hba
->hw
->alloc_dev
)
771 sas_dev
= hisi_hba
->hw
->alloc_dev(device
);
773 sas_dev
= hisi_sas_alloc_dev(device
);
775 dev_err(dev
, "fail alloc dev: max support %d devices\n",
776 HISI_SAS_MAX_DEVICES
);
780 device
->lldd_dev
= sas_dev
;
781 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
783 if (parent_dev
&& dev_is_expander(parent_dev
->dev_type
)) {
786 phy_no
= sas_find_attached_phy_id(&parent_dev
->ex_dev
, device
);
788 dev_info(dev
, "dev found: no attached "
789 "dev:%016llx at ex:%016llx\n",
790 SAS_ADDR(device
->sas_addr
),
791 SAS_ADDR(parent_dev
->sas_addr
));
797 dev_info(dev
, "dev[%d:%x] found\n",
798 sas_dev
->device_id
, sas_dev
->dev_type
);
803 hisi_sas_dev_gone(device
);
807 int hisi_sas_slave_configure(struct scsi_device
*sdev
)
809 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
810 int ret
= sas_slave_configure(sdev
);
814 if (!dev_is_sata(dev
))
815 sas_change_queue_depth(sdev
, 64);
819 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure
);
821 void hisi_sas_scan_start(struct Scsi_Host
*shost
)
823 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
825 hisi_hba
->hw
->phys_init(hisi_hba
);
827 EXPORT_SYMBOL_GPL(hisi_sas_scan_start
);
829 int hisi_sas_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
831 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
832 struct sas_ha_struct
*sha
= &hisi_hba
->sha
;
834 /* Wait for PHY up interrupt to occur */
841 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished
);
843 static void hisi_sas_phyup_work_common(struct work_struct
*work
,
844 enum hisi_sas_phy_event event
)
846 struct hisi_sas_phy
*phy
=
847 container_of(work
, typeof(*phy
), works
[event
]);
848 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
849 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
850 int phy_no
= sas_phy
->id
;
852 phy
->wait_phyup_cnt
= 0;
853 if (phy
->identify
.target_port_protocols
== SAS_PROTOCOL_SSP
)
854 hisi_hba
->hw
->sl_notify_ssp(hisi_hba
, phy_no
);
855 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
, GFP_KERNEL
);
858 static void hisi_sas_phyup_work(struct work_struct
*work
)
860 hisi_sas_phyup_work_common(work
, HISI_PHYE_PHY_UP
);
863 static void hisi_sas_linkreset_work(struct work_struct
*work
)
865 struct hisi_sas_phy
*phy
=
866 container_of(work
, typeof(*phy
), works
[HISI_PHYE_LINK_RESET
]);
867 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
869 hisi_sas_control_phy(sas_phy
, PHY_FUNC_LINK_RESET
, NULL
);
872 static void hisi_sas_phyup_pm_work(struct work_struct
*work
)
874 struct hisi_sas_phy
*phy
=
875 container_of(work
, typeof(*phy
), works
[HISI_PHYE_PHY_UP_PM
]);
876 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
877 struct device
*dev
= hisi_hba
->dev
;
879 hisi_sas_phyup_work_common(work
, HISI_PHYE_PHY_UP_PM
);
880 pm_runtime_put_sync(dev
);
883 static const work_func_t hisi_sas_phye_fns
[HISI_PHYES_NUM
] = {
884 [HISI_PHYE_PHY_UP
] = hisi_sas_phyup_work
,
885 [HISI_PHYE_LINK_RESET
] = hisi_sas_linkreset_work
,
886 [HISI_PHYE_PHY_UP_PM
] = hisi_sas_phyup_pm_work
,
889 bool hisi_sas_notify_phy_event(struct hisi_sas_phy
*phy
,
890 enum hisi_sas_phy_event event
)
892 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
894 if (WARN_ON(event
>= HISI_PHYES_NUM
))
897 return queue_work(hisi_hba
->wq
, &phy
->works
[event
]);
899 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event
);
901 static void hisi_sas_wait_phyup_timedout(struct timer_list
*t
)
903 struct hisi_sas_phy
*phy
= from_timer(phy
, t
, timer
);
904 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
905 struct device
*dev
= hisi_hba
->dev
;
906 int phy_no
= phy
->sas_phy
.id
;
908 dev_warn(dev
, "phy%d wait phyup timeout, issuing link reset\n", phy_no
);
909 hisi_sas_notify_phy_event(phy
, HISI_PHYE_LINK_RESET
);
912 #define HISI_SAS_WAIT_PHYUP_RETRIES 10
914 void hisi_sas_phy_oob_ready(struct hisi_hba
*hisi_hba
, int phy_no
)
916 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
917 struct device
*dev
= hisi_hba
->dev
;
920 dev_dbg(dev
, "phy%d OOB ready\n", phy_no
);
921 spin_lock_irqsave(&phy
->lock
, flags
);
922 if (phy
->phy_attached
) {
923 spin_unlock_irqrestore(&phy
->lock
, flags
);
927 if (!timer_pending(&phy
->timer
)) {
928 if (phy
->wait_phyup_cnt
< HISI_SAS_WAIT_PHYUP_RETRIES
) {
929 phy
->wait_phyup_cnt
++;
930 phy
->timer
.expires
= jiffies
+
931 HISI_SAS_WAIT_PHYUP_TIMEOUT
;
932 add_timer(&phy
->timer
);
933 spin_unlock_irqrestore(&phy
->lock
, flags
);
937 dev_warn(dev
, "phy%d failed to come up %d times, giving up\n",
938 phy_no
, phy
->wait_phyup_cnt
);
939 phy
->wait_phyup_cnt
= 0;
941 spin_unlock_irqrestore(&phy
->lock
, flags
);
944 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready
);
946 static void hisi_sas_phy_init(struct hisi_hba
*hisi_hba
, int phy_no
)
948 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
949 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
952 phy
->hisi_hba
= hisi_hba
;
954 phy
->minimum_linkrate
= SAS_LINK_RATE_1_5_GBPS
;
955 phy
->maximum_linkrate
= hisi_hba
->hw
->phy_get_max_linkrate();
956 sas_phy
->enabled
= (phy_no
< hisi_hba
->n_phy
) ? 1 : 0;
957 sas_phy
->class = SAS
;
958 sas_phy
->iproto
= SAS_PROTOCOL_ALL
;
960 sas_phy
->type
= PHY_TYPE_PHYSICAL
;
961 sas_phy
->role
= PHY_ROLE_INITIATOR
;
962 sas_phy
->oob_mode
= OOB_NOT_CONNECTED
;
963 sas_phy
->linkrate
= SAS_LINK_RATE_UNKNOWN
;
964 sas_phy
->id
= phy_no
;
965 sas_phy
->sas_addr
= &hisi_hba
->sas_addr
[0];
966 sas_phy
->frame_rcvd
= &phy
->frame_rcvd
[0];
967 sas_phy
->ha
= (struct sas_ha_struct
*)hisi_hba
->shost
->hostdata
;
968 sas_phy
->lldd_phy
= phy
;
970 for (i
= 0; i
< HISI_PHYES_NUM
; i
++)
971 INIT_WORK(&phy
->works
[i
], hisi_sas_phye_fns
[i
]);
973 spin_lock_init(&phy
->lock
);
975 timer_setup(&phy
->timer
, hisi_sas_wait_phyup_timedout
, 0);
978 /* Wrapper to ensure we track hisi_sas_phy.enable properly */
979 void hisi_sas_phy_enable(struct hisi_hba
*hisi_hba
, int phy_no
, int enable
)
981 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
982 struct asd_sas_phy
*aphy
= &phy
->sas_phy
;
983 struct sas_phy
*sphy
= aphy
->phy
;
986 spin_lock_irqsave(&phy
->lock
, flags
);
989 /* We may have been enabled already; if so, don't touch */
991 sphy
->negotiated_linkrate
= SAS_LINK_RATE_UNKNOWN
;
992 hisi_hba
->hw
->phy_start(hisi_hba
, phy_no
);
994 sphy
->negotiated_linkrate
= SAS_PHY_DISABLED
;
995 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
997 phy
->enable
= enable
;
998 spin_unlock_irqrestore(&phy
->lock
, flags
);
1000 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable
);
1002 static void hisi_sas_port_notify_formed(struct asd_sas_phy
*sas_phy
)
1004 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
1005 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
1006 struct hisi_sas_phy
*phy
= sas_phy
->lldd_phy
;
1007 struct asd_sas_port
*sas_port
= sas_phy
->port
;
1008 struct hisi_sas_port
*port
;
1009 unsigned long flags
;
1014 port
= to_hisi_sas_port(sas_port
);
1015 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1016 port
->port_attached
= 1;
1017 port
->id
= phy
->port_id
;
1019 sas_port
->lldd_port
= port
;
1020 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1023 static void hisi_sas_do_release_task(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
1024 struct hisi_sas_slot
*slot
)
1027 unsigned long flags
;
1028 struct task_status_struct
*ts
;
1030 ts
= &task
->task_status
;
1032 ts
->resp
= SAS_TASK_COMPLETE
;
1033 ts
->stat
= SAS_ABORTED_TASK
;
1034 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1035 task
->task_state_flags
&= ~SAS_TASK_STATE_PENDING
;
1036 if (!slot
->is_internal
&& task
->task_proto
!= SAS_PROTOCOL_SMP
)
1037 task
->task_state_flags
|= SAS_TASK_STATE_DONE
;
1038 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1041 hisi_sas_slot_task_free(hisi_hba
, task
, slot
);
1044 static void hisi_sas_release_task(struct hisi_hba
*hisi_hba
,
1045 struct domain_device
*device
)
1047 struct hisi_sas_slot
*slot
, *slot2
;
1048 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1050 list_for_each_entry_safe(slot
, slot2
, &sas_dev
->list
, entry
)
1051 hisi_sas_do_release_task(hisi_hba
, slot
->task
, slot
);
1054 void hisi_sas_release_tasks(struct hisi_hba
*hisi_hba
)
1056 struct hisi_sas_device
*sas_dev
;
1057 struct domain_device
*device
;
1060 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1061 sas_dev
= &hisi_hba
->devices
[i
];
1062 device
= sas_dev
->sas_device
;
1064 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
) ||
1068 hisi_sas_release_task(hisi_hba
, device
);
1071 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks
);
1073 static void hisi_sas_dereg_device(struct hisi_hba
*hisi_hba
,
1074 struct domain_device
*device
)
1076 if (hisi_hba
->hw
->dereg_device
)
1077 hisi_hba
->hw
->dereg_device(hisi_hba
, device
);
1081 hisi_sas_internal_task_abort_dev(struct hisi_sas_device
*sas_dev
,
1082 bool rst_ha_timeout
)
1084 struct hisi_sas_internal_abort_data data
= { rst_ha_timeout
};
1085 struct domain_device
*device
= sas_dev
->sas_device
;
1086 struct hisi_hba
*hisi_hba
= sas_dev
->hisi_hba
;
1089 for (i
= 0; i
< hisi_hba
->cq_nvecs
; i
++) {
1090 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1091 const struct cpumask
*mask
= cq
->irq_mask
;
1093 if (mask
&& !cpumask_intersects(cpu_online_mask
, mask
))
1095 rc
= sas_execute_internal_abort_dev(device
, i
, &data
);
1103 static void hisi_sas_dev_gone(struct domain_device
*device
)
1105 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1106 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1107 struct device
*dev
= hisi_hba
->dev
;
1110 dev_info(dev
, "dev[%d:%x] is gone\n",
1111 sas_dev
->device_id
, sas_dev
->dev_type
);
1113 down(&hisi_hba
->sem
);
1114 if (!test_bit(HISI_SAS_RESETTING_BIT
, &hisi_hba
->flags
)) {
1115 hisi_sas_internal_task_abort_dev(sas_dev
, true);
1117 hisi_sas_dereg_device(hisi_hba
, device
);
1119 ret
= hisi_hba
->hw
->clear_itct(hisi_hba
, sas_dev
);
1120 device
->lldd_dev
= NULL
;
1123 if (hisi_hba
->hw
->free_device
)
1124 hisi_hba
->hw
->free_device(sas_dev
);
1126 /* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */
1128 sas_dev
->dev_type
= SAS_PHY_UNUSED
;
1129 sas_dev
->sas_device
= NULL
;
1133 static int hisi_sas_phy_set_linkrate(struct hisi_hba
*hisi_hba
, int phy_no
,
1134 struct sas_phy_linkrates
*r
)
1136 struct sas_phy_linkrates _r
;
1138 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1139 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1140 enum sas_linkrate min
, max
;
1142 if (r
->minimum_linkrate
> SAS_LINK_RATE_1_5_GBPS
)
1145 if (r
->maximum_linkrate
== SAS_LINK_RATE_UNKNOWN
) {
1146 max
= sas_phy
->phy
->maximum_linkrate
;
1147 min
= r
->minimum_linkrate
;
1148 } else if (r
->minimum_linkrate
== SAS_LINK_RATE_UNKNOWN
) {
1149 max
= r
->maximum_linkrate
;
1150 min
= sas_phy
->phy
->minimum_linkrate
;
1154 _r
.maximum_linkrate
= max
;
1155 _r
.minimum_linkrate
= min
;
1157 sas_phy
->phy
->maximum_linkrate
= max
;
1158 sas_phy
->phy
->minimum_linkrate
= min
;
1160 hisi_sas_phy_enable(hisi_hba
, phy_no
, 0);
1162 hisi_hba
->hw
->phy_set_linkrate(hisi_hba
, phy_no
, &_r
);
1163 hisi_sas_phy_enable(hisi_hba
, phy_no
, 1);
1168 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
1171 struct hisi_sas_phy
*phy
= container_of(sas_phy
,
1172 struct hisi_sas_phy
, sas_phy
);
1173 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
1174 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
1175 struct device
*dev
= hisi_hba
->dev
;
1176 DECLARE_COMPLETION_ONSTACK(completion
);
1177 int phy_no
= sas_phy
->id
;
1178 u8 sts
= phy
->phy_attached
;
1181 down(&hisi_hba
->sem
);
1182 phy
->reset_completion
= &completion
;
1185 case PHY_FUNC_HARD_RESET
:
1186 hisi_hba
->hw
->phy_hard_reset(hisi_hba
, phy_no
);
1189 case PHY_FUNC_LINK_RESET
:
1190 hisi_sas_phy_enable(hisi_hba
, phy_no
, 0);
1192 hisi_sas_phy_enable(hisi_hba
, phy_no
, 1);
1195 case PHY_FUNC_DISABLE
:
1196 hisi_sas_phy_enable(hisi_hba
, phy_no
, 0);
1199 case PHY_FUNC_SET_LINK_RATE
:
1200 ret
= hisi_sas_phy_set_linkrate(hisi_hba
, phy_no
, funcdata
);
1203 case PHY_FUNC_GET_EVENTS
:
1204 if (hisi_hba
->hw
->get_events
) {
1205 hisi_hba
->hw
->get_events(hisi_hba
, phy_no
);
1209 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
1215 if (sts
&& !wait_for_completion_timeout(&completion
,
1216 HISI_SAS_WAIT_PHYUP_TIMEOUT
)) {
1217 dev_warn(dev
, "phy%d wait phyup timed out for func %d\n",
1224 phy
->reset_completion
= NULL
;
1230 static void hisi_sas_fill_ata_reset_cmd(struct ata_device
*dev
,
1231 bool reset
, int pmp
, u8
*fis
)
1233 struct ata_taskfile tf
;
1235 ata_tf_init(dev
, &tf
);
1239 tf
.ctl
&= ~ATA_SRST
;
1240 tf
.command
= ATA_CMD_DEV_RESET
;
1241 ata_tf_to_fis(&tf
, pmp
, 0, fis
);
1244 static int hisi_sas_softreset_ata_disk(struct domain_device
*device
)
1247 struct ata_port
*ap
= device
->sata_dev
.ap
;
1248 struct ata_link
*link
;
1249 int rc
= TMF_RESP_FUNC_FAILED
;
1250 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1251 struct device
*dev
= hisi_hba
->dev
;
1253 ata_for_each_link(link
, ap
, EDGE
) {
1254 int pmp
= sata_srst_pmp(link
);
1256 hisi_sas_fill_ata_reset_cmd(link
->device
, 1, pmp
, fis
);
1257 rc
= sas_execute_ata_cmd(device
, fis
, -1);
1258 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1262 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1263 ata_for_each_link(link
, ap
, EDGE
) {
1264 int pmp
= sata_srst_pmp(link
);
1266 hisi_sas_fill_ata_reset_cmd(link
->device
, 0, pmp
, fis
);
1267 rc
= sas_execute_ata_cmd(device
, fis
, -1);
1268 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1269 dev_err(dev
, "ata disk %016llx de-reset failed\n",
1270 SAS_ADDR(device
->sas_addr
));
1273 dev_err(dev
, "ata disk %016llx reset failed\n",
1274 SAS_ADDR(device
->sas_addr
));
1277 if (rc
== TMF_RESP_FUNC_COMPLETE
)
1278 hisi_sas_release_task(hisi_hba
, device
);
1283 static void hisi_sas_refresh_port_id(struct hisi_hba
*hisi_hba
)
1285 u32 state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1288 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1289 struct hisi_sas_device
*sas_dev
= &hisi_hba
->devices
[i
];
1290 struct domain_device
*device
= sas_dev
->sas_device
;
1291 struct asd_sas_port
*sas_port
;
1292 struct hisi_sas_port
*port
;
1293 struct hisi_sas_phy
*phy
= NULL
;
1294 struct asd_sas_phy
*sas_phy
;
1296 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
)
1297 || !device
|| !device
->port
)
1300 sas_port
= device
->port
;
1301 port
= to_hisi_sas_port(sas_port
);
1303 spin_lock(&sas_port
->phy_list_lock
);
1304 list_for_each_entry(sas_phy
, &sas_port
->phy_list
, port_phy_el
)
1305 if (state
& BIT(sas_phy
->id
)) {
1306 phy
= sas_phy
->lldd_phy
;
1309 spin_unlock(&sas_port
->phy_list_lock
);
1312 port
->id
= phy
->port_id
;
1314 /* Update linkrate of directly attached device. */
1315 if (!device
->parent
)
1316 device
->linkrate
= phy
->sas_phy
.linkrate
;
1318 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
1319 } else if (!port
->port_attached
)
1324 static void hisi_sas_rescan_topology(struct hisi_hba
*hisi_hba
, u32 state
)
1326 struct asd_sas_port
*_sas_port
= NULL
;
1329 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++) {
1330 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1331 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1332 struct asd_sas_port
*sas_port
= sas_phy
->port
;
1333 bool do_port_check
= _sas_port
!= sas_port
;
1335 if (!sas_phy
->phy
->enabled
)
1338 /* Report PHY state change to libsas */
1339 if (state
& BIT(phy_no
)) {
1340 if (do_port_check
&& sas_port
&& sas_port
->port_dev
) {
1341 struct domain_device
*dev
= sas_port
->port_dev
;
1343 _sas_port
= sas_port
;
1345 if (dev_is_expander(dev
->dev_type
))
1346 sas_notify_port_event(sas_phy
,
1347 PORTE_BROADCAST_RCVD
,
1351 hisi_sas_phy_down(hisi_hba
, phy_no
, 0, GFP_KERNEL
);
1356 static void hisi_sas_reset_init_all_devices(struct hisi_hba
*hisi_hba
)
1358 struct hisi_sas_device
*sas_dev
;
1359 struct domain_device
*device
;
1362 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1363 sas_dev
= &hisi_hba
->devices
[i
];
1364 device
= sas_dev
->sas_device
;
1366 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
) || !device
)
1369 hisi_sas_init_device(device
);
1373 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba
*hisi_hba
,
1374 struct asd_sas_port
*sas_port
,
1375 struct domain_device
*device
)
1377 struct ata_port
*ap
= device
->sata_dev
.ap
;
1378 struct device
*dev
= hisi_hba
->dev
;
1379 int rc
= TMF_RESP_FUNC_FAILED
;
1380 struct ata_link
*link
;
1384 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1385 if (!(sas_port
->phy_mask
& BIT(i
)))
1388 ata_for_each_link(link
, ap
, EDGE
) {
1389 int pmp
= sata_srst_pmp(link
);
1391 hisi_sas_fill_ata_reset_cmd(link
->device
, 1, pmp
, fis
);
1392 rc
= sas_execute_ata_cmd(device
, fis
, i
);
1393 if (rc
!= TMF_RESP_FUNC_COMPLETE
) {
1394 dev_err(dev
, "phy%d ata reset failed rc=%d\n",
1402 static void hisi_sas_terminate_stp_reject(struct hisi_hba
*hisi_hba
)
1404 struct device
*dev
= hisi_hba
->dev
;
1407 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1408 struct hisi_sas_device
*sas_dev
= &hisi_hba
->devices
[i
];
1409 struct domain_device
*device
= sas_dev
->sas_device
;
1411 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
) || !device
)
1414 rc
= hisi_sas_internal_task_abort_dev(sas_dev
, false);
1416 dev_err(dev
, "STP reject: abort dev failed %d\n", rc
);
1419 for (port_no
= 0; port_no
< hisi_hba
->n_phy
; port_no
++) {
1420 struct hisi_sas_port
*port
= &hisi_hba
->port
[port_no
];
1421 struct asd_sas_port
*sas_port
= &port
->sas_port
;
1422 struct domain_device
*port_dev
= sas_port
->port_dev
;
1423 struct domain_device
*device
;
1425 if (!port_dev
|| !dev_is_expander(port_dev
->dev_type
))
1428 /* Try to find a SATA device */
1429 list_for_each_entry(device
, &sas_port
->dev_list
,
1431 if (dev_is_sata(device
)) {
1432 hisi_sas_send_ata_reset_each_phy(hisi_hba
,
1441 void hisi_sas_controller_reset_prepare(struct hisi_hba
*hisi_hba
)
1443 struct Scsi_Host
*shost
= hisi_hba
->shost
;
1445 hisi_hba
->phy_state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1447 scsi_block_requests(shost
);
1448 hisi_hba
->hw
->wait_cmds_complete_timeout(hisi_hba
, 100, 5000);
1450 del_timer_sync(&hisi_hba
->timer
);
1452 set_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1454 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare
);
1456 void hisi_sas_controller_reset_done(struct hisi_hba
*hisi_hba
)
1458 struct Scsi_Host
*shost
= hisi_hba
->shost
;
1460 /* Init and wait for PHYs to come up and all libsas event finished. */
1461 hisi_hba
->hw
->phys_init(hisi_hba
);
1463 hisi_sas_refresh_port_id(hisi_hba
);
1464 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1466 if (hisi_hba
->reject_stp_links_msk
)
1467 hisi_sas_terminate_stp_reject(hisi_hba
);
1468 hisi_sas_reset_init_all_devices(hisi_hba
);
1469 scsi_unblock_requests(shost
);
1470 clear_bit(HISI_SAS_RESETTING_BIT
, &hisi_hba
->flags
);
1473 hisi_sas_rescan_topology(hisi_hba
, hisi_hba
->phy_state
);
1475 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done
);
1477 static int hisi_sas_controller_prereset(struct hisi_hba
*hisi_hba
)
1479 if (!hisi_hba
->hw
->soft_reset
)
1482 down(&hisi_hba
->sem
);
1483 if (test_and_set_bit(HISI_SAS_RESETTING_BIT
, &hisi_hba
->flags
)) {
1488 if (hisi_sas_debugfs_enable
&& hisi_hba
->debugfs_itct
[0].itct
)
1489 hisi_hba
->hw
->debugfs_snapshot_regs(hisi_hba
);
1494 static int hisi_sas_controller_reset(struct hisi_hba
*hisi_hba
)
1496 struct device
*dev
= hisi_hba
->dev
;
1497 struct Scsi_Host
*shost
= hisi_hba
->shost
;
1500 dev_info(dev
, "controller resetting...\n");
1501 hisi_sas_controller_reset_prepare(hisi_hba
);
1503 rc
= hisi_hba
->hw
->soft_reset(hisi_hba
);
1505 dev_warn(dev
, "controller reset failed (%d)\n", rc
);
1506 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1508 scsi_unblock_requests(shost
);
1509 clear_bit(HISI_SAS_RESETTING_BIT
, &hisi_hba
->flags
);
1512 clear_bit(HISI_SAS_HW_FAULT_BIT
, &hisi_hba
->flags
);
1514 hisi_sas_controller_reset_done(hisi_hba
);
1515 dev_info(dev
, "controller reset complete\n");
1520 static int hisi_sas_abort_task(struct sas_task
*task
)
1522 struct hisi_sas_internal_abort_data internal_abort_data
= { false };
1523 struct domain_device
*device
= task
->dev
;
1524 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1525 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1526 struct hisi_hba
*hisi_hba
;
1528 int rc
= TMF_RESP_FUNC_FAILED
;
1529 unsigned long flags
;
1532 return TMF_RESP_FUNC_FAILED
;
1534 hisi_hba
= dev_to_hisi_hba(task
->dev
);
1535 dev
= hisi_hba
->dev
;
1537 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1538 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
1539 struct hisi_sas_cq
*cq
;
1543 * sync irq to avoid free'ing task
1544 * before using task in IO completion
1546 cq
= &hisi_hba
->cq
[slot
->dlvry_queue
];
1547 synchronize_irq(cq
->irq_no
);
1549 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1550 rc
= TMF_RESP_FUNC_COMPLETE
;
1553 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
1554 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1556 if (slot
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1557 u16 tag
= slot
->idx
;
1560 rc
= sas_abort_task(task
, tag
);
1561 rc2
= sas_execute_internal_abort_single(device
, tag
,
1562 slot
->dlvry_queue
, &internal_abort_data
);
1564 dev_err(dev
, "abort task: internal abort (%d)\n", rc2
);
1565 return TMF_RESP_FUNC_FAILED
;
1569 * If the TMF finds that the IO is not in the device and also
1570 * the internal abort does not succeed, then it is safe to
1572 * Note: if the internal abort succeeds then the slot
1573 * will have already been completed
1575 if (rc
== TMF_RESP_FUNC_COMPLETE
&& rc2
!= TMF_RESP_FUNC_SUCC
) {
1576 if (task
->lldd_task
)
1577 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1579 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
1580 task
->task_proto
& SAS_PROTOCOL_STP
) {
1581 if (task
->dev
->dev_type
== SAS_SATA_DEV
) {
1582 struct ata_queued_cmd
*qc
= task
->uldd_task
;
1584 rc
= hisi_sas_internal_task_abort_dev(sas_dev
, false);
1586 dev_err(dev
, "abort task: internal abort failed\n");
1589 hisi_sas_dereg_device(hisi_hba
, device
);
1592 * If an ATA internal command times out in ATA EH, it
1593 * need to execute soft reset, so check the scsicmd
1595 if ((sas_dev
->dev_status
== HISI_SAS_DEV_NCQ_ERR
) &&
1596 qc
&& qc
->scsicmd
) {
1597 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1598 rc
= TMF_RESP_FUNC_COMPLETE
;
1600 rc
= hisi_sas_softreset_ata_disk(device
);
1603 } else if (slot
&& task
->task_proto
& SAS_PROTOCOL_SMP
) {
1605 u32 tag
= slot
->idx
;
1606 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[slot
->dlvry_queue
];
1608 rc
= sas_execute_internal_abort_single(device
,
1609 tag
, slot
->dlvry_queue
,
1610 &internal_abort_data
);
1611 if (((rc
< 0) || (rc
== TMF_RESP_FUNC_FAILED
)) &&
1614 * sync irq to avoid free'ing task
1615 * before using task in IO completion
1617 synchronize_irq(cq
->irq_no
);
1623 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1624 dev_notice(dev
, "abort task: rc=%d\n", rc
);
1628 static int hisi_sas_abort_task_set(struct domain_device
*device
, u8
*lun
)
1630 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1631 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1632 struct device
*dev
= hisi_hba
->dev
;
1635 rc
= hisi_sas_internal_task_abort_dev(sas_dev
, false);
1637 dev_err(dev
, "abort task set: internal abort rc=%d\n", rc
);
1638 return TMF_RESP_FUNC_FAILED
;
1640 hisi_sas_dereg_device(hisi_hba
, device
);
1642 rc
= sas_abort_task_set(device
, lun
);
1643 if (rc
== TMF_RESP_FUNC_COMPLETE
)
1644 hisi_sas_release_task(hisi_hba
, device
);
1649 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device
*device
)
1651 struct sas_phy
*local_phy
= sas_get_local_phy(device
);
1652 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1653 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1654 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1657 if (!local_phy
->enabled
) {
1658 sas_put_local_phy(local_phy
);
1662 if (scsi_is_sas_phy_local(local_phy
)) {
1663 struct asd_sas_phy
*sas_phy
=
1664 sas_ha
->sas_phy
[local_phy
->number
];
1665 struct hisi_sas_phy
*phy
=
1666 container_of(sas_phy
, struct hisi_sas_phy
, sas_phy
);
1667 unsigned long flags
;
1669 spin_lock_irqsave(&phy
->lock
, flags
);
1671 spin_unlock_irqrestore(&phy
->lock
, flags
);
1674 reset_type
= (sas_dev
->dev_status
== HISI_SAS_DEV_INIT
||
1675 !dev_is_sata(device
)) ? true : false;
1677 rc
= sas_phy_reset(local_phy
, reset_type
);
1678 sas_put_local_phy(local_phy
);
1680 if (scsi_is_sas_phy_local(local_phy
)) {
1681 struct asd_sas_phy
*sas_phy
=
1682 sas_ha
->sas_phy
[local_phy
->number
];
1683 struct hisi_sas_phy
*phy
=
1684 container_of(sas_phy
, struct hisi_sas_phy
, sas_phy
);
1685 unsigned long flags
;
1687 spin_lock_irqsave(&phy
->lock
, flags
);
1689 spin_unlock_irqrestore(&phy
->lock
, flags
);
1691 /* report PHY down if timed out */
1692 if (rc
== -ETIMEDOUT
)
1693 hisi_sas_phy_down(hisi_hba
, sas_phy
->id
, 0, GFP_KERNEL
);
1701 if (dev_is_sata(device
)) {
1702 struct ata_link
*link
= &device
->sata_dev
.ap
->link
;
1704 rc
= ata_wait_after_reset(link
, HISI_SAS_WAIT_PHYUP_TIMEOUT
,
1705 smp_ata_check_ready_type
);
1713 static int hisi_sas_I_T_nexus_reset(struct domain_device
*device
)
1715 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1716 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1717 struct device
*dev
= hisi_hba
->dev
;
1720 if (sas_dev
->dev_status
== HISI_SAS_DEV_NCQ_ERR
)
1721 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
1723 rc
= hisi_sas_internal_task_abort_dev(sas_dev
, false);
1725 dev_err(dev
, "I_T nexus reset: internal abort (%d)\n", rc
);
1726 return TMF_RESP_FUNC_FAILED
;
1728 hisi_sas_dereg_device(hisi_hba
, device
);
1730 rc
= hisi_sas_debug_I_T_nexus_reset(device
);
1731 if (rc
== TMF_RESP_FUNC_COMPLETE
&& dev_is_sata(device
)) {
1732 struct sas_phy
*local_phy
;
1734 rc
= hisi_sas_softreset_ata_disk(device
);
1739 case TMF_RESP_FUNC_FAILED
:
1742 local_phy
= sas_get_local_phy(device
);
1743 rc
= sas_phy_enable(local_phy
, 0);
1745 local_phy
->enabled
= 0;
1746 dev_err(dev
, "Disabled local phy of ATA disk %016llx due to softreset fail (%d)\n",
1747 SAS_ADDR(device
->sas_addr
), rc
);
1750 sas_put_local_phy(local_phy
);
1757 if ((rc
== TMF_RESP_FUNC_COMPLETE
) || (rc
== -ENODEV
))
1758 hisi_sas_release_task(hisi_hba
, device
);
1763 static int hisi_sas_lu_reset(struct domain_device
*device
, u8
*lun
)
1765 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1766 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1767 struct device
*dev
= hisi_hba
->dev
;
1768 int rc
= TMF_RESP_FUNC_FAILED
;
1770 /* Clear internal IO and then lu reset */
1771 rc
= hisi_sas_internal_task_abort_dev(sas_dev
, false);
1773 dev_err(dev
, "lu_reset: internal abort failed\n");
1776 hisi_sas_dereg_device(hisi_hba
, device
);
1778 if (dev_is_sata(device
)) {
1779 struct sas_phy
*phy
;
1781 phy
= sas_get_local_phy(device
);
1783 rc
= sas_phy_reset(phy
, true);
1786 hisi_sas_release_task(hisi_hba
, device
);
1787 sas_put_local_phy(phy
);
1789 rc
= sas_lu_reset(device
, lun
);
1790 if (rc
== TMF_RESP_FUNC_COMPLETE
)
1791 hisi_sas_release_task(hisi_hba
, device
);
1794 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1795 dev_err(dev
, "lu_reset: for device[%d]:rc= %d\n",
1796 sas_dev
->device_id
, rc
);
1800 static void hisi_sas_async_I_T_nexus_reset(void *data
, async_cookie_t cookie
)
1802 struct domain_device
*device
= data
;
1803 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1806 rc
= hisi_sas_debug_I_T_nexus_reset(device
);
1807 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1808 dev_info(hisi_hba
->dev
, "I_T_nexus reset fail for dev:%016llx rc=%d\n",
1809 SAS_ADDR(device
->sas_addr
), rc
);
1812 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct
*sas_ha
)
1814 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
1815 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r
);
1816 ASYNC_DOMAIN_EXCLUSIVE(async
);
1819 queue_work(hisi_hba
->wq
, &r
.work
);
1820 wait_for_completion(r
.completion
);
1822 return TMF_RESP_FUNC_FAILED
;
1824 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1825 struct hisi_sas_device
*sas_dev
= &hisi_hba
->devices
[i
];
1826 struct domain_device
*device
= sas_dev
->sas_device
;
1828 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
) || !device
||
1829 dev_is_expander(device
->dev_type
))
1832 async_schedule_domain(hisi_sas_async_I_T_nexus_reset
,
1836 async_synchronize_full_domain(&async
);
1837 hisi_sas_release_tasks(hisi_hba
);
1839 return TMF_RESP_FUNC_COMPLETE
;
1842 static int hisi_sas_query_task(struct sas_task
*task
)
1844 int rc
= TMF_RESP_FUNC_FAILED
;
1846 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1847 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1848 u32 tag
= slot
->idx
;
1850 rc
= sas_query_task(task
, tag
);
1852 /* The task is still in Lun, release it then */
1853 case TMF_RESP_FUNC_SUCC
:
1854 /* The task is not in Lun or failed, reset the phy */
1855 case TMF_RESP_FUNC_FAILED
:
1856 case TMF_RESP_FUNC_COMPLETE
:
1859 rc
= TMF_RESP_FUNC_FAILED
;
1866 static bool hisi_sas_internal_abort_timeout(struct sas_task
*task
,
1869 struct domain_device
*device
= task
->dev
;
1870 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1871 struct hisi_sas_internal_abort_data
*timeout
= data
;
1873 if (hisi_sas_debugfs_enable
&& hisi_hba
->debugfs_itct
[0].itct
)
1874 queue_work(hisi_hba
->wq
, &hisi_hba
->debugfs_work
);
1876 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
1877 pr_err("Internal abort: timeout %016llx\n",
1878 SAS_ADDR(device
->sas_addr
));
1880 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1882 set_bit(HISI_SAS_HW_FAULT_BIT
, &hisi_hba
->flags
);
1885 struct hisi_sas_cq
*cq
=
1886 &hisi_hba
->cq
[slot
->dlvry_queue
];
1888 * sync irq to avoid free'ing task
1889 * before using task in IO completion
1891 synchronize_irq(cq
->irq_no
);
1895 if (timeout
->rst_ha_timeout
) {
1896 pr_err("Internal abort: timeout and not done %016llx. Queuing reset.\n",
1897 SAS_ADDR(device
->sas_addr
));
1898 queue_work(hisi_hba
->wq
, &hisi_hba
->rst_work
);
1900 pr_err("Internal abort: timeout and not done %016llx.\n",
1901 SAS_ADDR(device
->sas_addr
));
1910 static void hisi_sas_port_formed(struct asd_sas_phy
*sas_phy
)
1912 hisi_sas_port_notify_formed(sas_phy
);
1915 static int hisi_sas_write_gpio(struct sas_ha_struct
*sha
, u8 reg_type
,
1916 u8 reg_index
, u8 reg_count
, u8
*write_data
)
1918 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
1920 if (!hisi_hba
->hw
->write_gpio
)
1923 return hisi_hba
->hw
->write_gpio(hisi_hba
, reg_type
,
1924 reg_index
, reg_count
, write_data
);
1927 static void hisi_sas_phy_disconnected(struct hisi_sas_phy
*phy
)
1929 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1930 struct sas_phy
*sphy
= sas_phy
->phy
;
1931 unsigned long flags
;
1933 phy
->phy_attached
= 0;
1937 spin_lock_irqsave(&phy
->lock
, flags
);
1939 sphy
->negotiated_linkrate
= SAS_LINK_RATE_UNKNOWN
;
1941 sphy
->negotiated_linkrate
= SAS_PHY_DISABLED
;
1942 spin_unlock_irqrestore(&phy
->lock
, flags
);
1945 void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
,
1948 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1949 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1950 struct device
*dev
= hisi_hba
->dev
;
1953 /* Phy down but ready */
1954 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
, gfp_flags
);
1955 hisi_sas_port_notify_formed(sas_phy
);
1957 struct hisi_sas_port
*port
= phy
->port
;
1959 if (test_bit(HISI_SAS_RESETTING_BIT
, &hisi_hba
->flags
) ||
1961 dev_info(dev
, "ignore flutter phy%d down\n", phy_no
);
1964 /* Phy down and not ready */
1965 sas_notify_phy_event(sas_phy
, PHYE_LOSS_OF_SIGNAL
, gfp_flags
);
1966 sas_phy_disconnected(sas_phy
);
1969 if (phy
->phy_type
& PORT_TYPE_SAS
) {
1970 int port_id
= port
->id
;
1972 if (!hisi_hba
->hw
->get_wideport_bitmap(hisi_hba
,
1974 port
->port_attached
= 0;
1975 } else if (phy
->phy_type
& PORT_TYPE_SATA
)
1976 port
->port_attached
= 0;
1978 hisi_sas_phy_disconnected(phy
);
1981 EXPORT_SYMBOL_GPL(hisi_sas_phy_down
);
1983 void hisi_sas_phy_bcast(struct hisi_sas_phy
*phy
)
1985 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1986 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
1988 if (test_bit(HISI_SAS_RESETTING_BIT
, &hisi_hba
->flags
))
1991 sas_notify_port_event(sas_phy
, PORTE_BROADCAST_RCVD
, GFP_ATOMIC
);
1993 EXPORT_SYMBOL_GPL(hisi_sas_phy_bcast
);
1995 void hisi_sas_sync_irqs(struct hisi_hba
*hisi_hba
)
1999 for (i
= 0; i
< hisi_hba
->cq_nvecs
; i
++) {
2000 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
2002 synchronize_irq(cq
->irq_no
);
2005 EXPORT_SYMBOL_GPL(hisi_sas_sync_irqs
);
2007 int hisi_sas_host_reset(struct Scsi_Host
*shost
, int reset_type
)
2009 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
2011 if (reset_type
!= SCSI_ADAPTER_RESET
)
2014 queue_work(hisi_hba
->wq
, &hisi_hba
->rst_work
);
2018 EXPORT_SYMBOL_GPL(hisi_sas_host_reset
);
2020 struct scsi_transport_template
*hisi_sas_stt
;
2021 EXPORT_SYMBOL_GPL(hisi_sas_stt
);
2023 static struct sas_domain_function_template hisi_sas_transport_ops
= {
2024 .lldd_dev_found
= hisi_sas_dev_found
,
2025 .lldd_dev_gone
= hisi_sas_dev_gone
,
2026 .lldd_execute_task
= hisi_sas_queue_command
,
2027 .lldd_control_phy
= hisi_sas_control_phy
,
2028 .lldd_abort_task
= hisi_sas_abort_task
,
2029 .lldd_abort_task_set
= hisi_sas_abort_task_set
,
2030 .lldd_I_T_nexus_reset
= hisi_sas_I_T_nexus_reset
,
2031 .lldd_lu_reset
= hisi_sas_lu_reset
,
2032 .lldd_query_task
= hisi_sas_query_task
,
2033 .lldd_clear_nexus_ha
= hisi_sas_clear_nexus_ha
,
2034 .lldd_port_formed
= hisi_sas_port_formed
,
2035 .lldd_write_gpio
= hisi_sas_write_gpio
,
2036 .lldd_tmf_aborted
= hisi_sas_tmf_aborted
,
2037 .lldd_abort_timeout
= hisi_sas_internal_abort_timeout
,
2040 void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
)
2042 int i
, s
, j
, max_command_entries
= HISI_SAS_MAX_COMMANDS
;
2043 struct hisi_sas_breakpoint
*sata_breakpoint
= hisi_hba
->sata_breakpoint
;
2045 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
2046 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
2047 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
2048 struct hisi_sas_cmd_hdr
*cmd_hdr
= hisi_hba
->cmd_hdr
[i
];
2050 s
= sizeof(struct hisi_sas_cmd_hdr
);
2051 for (j
= 0; j
< HISI_SAS_QUEUE_SLOTS
; j
++)
2052 memset(&cmd_hdr
[j
], 0, s
);
2056 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
2057 memset(hisi_hba
->complete_hdr
[i
], 0, s
);
2061 s
= sizeof(struct hisi_sas_initial_fis
) * hisi_hba
->n_phy
;
2062 memset(hisi_hba
->initial_fis
, 0, s
);
2064 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
2065 memset(hisi_hba
->iost
, 0, s
);
2067 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
2068 memset(hisi_hba
->breakpoint
, 0, s
);
2070 s
= sizeof(struct hisi_sas_sata_breakpoint
);
2071 for (j
= 0; j
< HISI_SAS_MAX_ITCT_ENTRIES
; j
++)
2072 memset(&sata_breakpoint
[j
], 0, s
);
2074 EXPORT_SYMBOL_GPL(hisi_sas_init_mem
);
2076 int hisi_sas_alloc(struct hisi_hba
*hisi_hba
)
2078 struct device
*dev
= hisi_hba
->dev
;
2079 int i
, j
, s
, max_command_entries
= HISI_SAS_MAX_COMMANDS
;
2080 int max_command_entries_ru
, sz_slot_buf_ru
;
2081 int blk_cnt
, slots_per_blk
;
2083 sema_init(&hisi_hba
->sem
, 1);
2084 spin_lock_init(&hisi_hba
->lock
);
2085 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
2086 hisi_sas_phy_init(hisi_hba
, i
);
2087 hisi_hba
->port
[i
].port_attached
= 0;
2088 hisi_hba
->port
[i
].id
= -1;
2091 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
2092 hisi_hba
->devices
[i
].dev_type
= SAS_PHY_UNUSED
;
2093 hisi_hba
->devices
[i
].device_id
= i
;
2094 hisi_hba
->devices
[i
].dev_status
= HISI_SAS_DEV_INIT
;
2097 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
2098 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
2099 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
2101 /* Completion queue structure */
2103 cq
->hisi_hba
= hisi_hba
;
2105 /* Delivery queue structure */
2106 spin_lock_init(&dq
->lock
);
2107 INIT_LIST_HEAD(&dq
->list
);
2109 dq
->hisi_hba
= hisi_hba
;
2111 /* Delivery queue */
2112 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
2113 hisi_hba
->cmd_hdr
[i
] = dmam_alloc_coherent(dev
, s
,
2114 &hisi_hba
->cmd_hdr_dma
[i
],
2116 if (!hisi_hba
->cmd_hdr
[i
])
2119 /* Completion queue */
2120 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
2121 hisi_hba
->complete_hdr
[i
] = dmam_alloc_coherent(dev
, s
,
2122 &hisi_hba
->complete_hdr_dma
[i
],
2124 if (!hisi_hba
->complete_hdr
[i
])
2128 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
2129 hisi_hba
->itct
= dmam_alloc_coherent(dev
, s
, &hisi_hba
->itct_dma
,
2131 if (!hisi_hba
->itct
)
2134 hisi_hba
->slot_info
= devm_kcalloc(dev
, max_command_entries
,
2135 sizeof(struct hisi_sas_slot
),
2137 if (!hisi_hba
->slot_info
)
2140 /* roundup to avoid overly large block size */
2141 max_command_entries_ru
= roundup(max_command_entries
, 64);
2142 if (hisi_hba
->prot_mask
& HISI_SAS_DIX_PROT_MASK
)
2143 sz_slot_buf_ru
= sizeof(struct hisi_sas_slot_dif_buf_table
);
2145 sz_slot_buf_ru
= sizeof(struct hisi_sas_slot_buf_table
);
2146 sz_slot_buf_ru
= roundup(sz_slot_buf_ru
, 64);
2147 s
= max(lcm(max_command_entries_ru
, sz_slot_buf_ru
), PAGE_SIZE
);
2148 blk_cnt
= (max_command_entries_ru
* sz_slot_buf_ru
) / s
;
2149 slots_per_blk
= s
/ sz_slot_buf_ru
;
2151 for (i
= 0; i
< blk_cnt
; i
++) {
2152 int slot_index
= i
* slots_per_blk
;
2156 buf
= dmam_alloc_coherent(dev
, s
, &buf_dma
,
2161 for (j
= 0; j
< slots_per_blk
; j
++, slot_index
++) {
2162 struct hisi_sas_slot
*slot
;
2164 slot
= &hisi_hba
->slot_info
[slot_index
];
2166 slot
->buf_dma
= buf_dma
;
2167 slot
->idx
= slot_index
;
2169 buf
+= sz_slot_buf_ru
;
2170 buf_dma
+= sz_slot_buf_ru
;
2174 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
2175 hisi_hba
->iost
= dmam_alloc_coherent(dev
, s
, &hisi_hba
->iost_dma
,
2177 if (!hisi_hba
->iost
)
2180 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
2181 hisi_hba
->breakpoint
= dmam_alloc_coherent(dev
, s
,
2182 &hisi_hba
->breakpoint_dma
,
2184 if (!hisi_hba
->breakpoint
)
2187 s
= hisi_hba
->slot_index_count
= max_command_entries
;
2188 hisi_hba
->slot_index_tags
= devm_bitmap_zalloc(dev
, s
, GFP_KERNEL
);
2189 if (!hisi_hba
->slot_index_tags
)
2192 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
2193 hisi_hba
->initial_fis
= dmam_alloc_coherent(dev
, s
,
2194 &hisi_hba
->initial_fis_dma
,
2196 if (!hisi_hba
->initial_fis
)
2199 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
2200 hisi_hba
->sata_breakpoint
= dmam_alloc_coherent(dev
, s
,
2201 &hisi_hba
->sata_breakpoint_dma
,
2203 if (!hisi_hba
->sata_breakpoint
)
2206 hisi_hba
->last_slot_index
= 0;
2208 hisi_hba
->wq
= create_singlethread_workqueue(dev_name(dev
));
2209 if (!hisi_hba
->wq
) {
2210 dev_err(dev
, "sas_alloc: failed to create workqueue\n");
2218 EXPORT_SYMBOL_GPL(hisi_sas_alloc
);
2220 void hisi_sas_free(struct hisi_hba
*hisi_hba
)
2224 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
2225 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[i
];
2227 del_timer_sync(&phy
->timer
);
2231 destroy_workqueue(hisi_hba
->wq
);
2233 EXPORT_SYMBOL_GPL(hisi_sas_free
);
2235 void hisi_sas_rst_work_handler(struct work_struct
*work
)
2237 struct hisi_hba
*hisi_hba
=
2238 container_of(work
, struct hisi_hba
, rst_work
);
2240 if (hisi_sas_controller_prereset(hisi_hba
))
2243 hisi_sas_controller_reset(hisi_hba
);
2245 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler
);
2247 void hisi_sas_sync_rst_work_handler(struct work_struct
*work
)
2249 struct hisi_sas_rst
*rst
=
2250 container_of(work
, struct hisi_sas_rst
, work
);
2252 if (hisi_sas_controller_prereset(rst
->hisi_hba
))
2255 if (!hisi_sas_controller_reset(rst
->hisi_hba
))
2258 complete(rst
->completion
);
2260 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler
);
2262 int hisi_sas_get_fw_info(struct hisi_hba
*hisi_hba
)
2264 struct device
*dev
= hisi_hba
->dev
;
2265 struct platform_device
*pdev
= hisi_hba
->platform_dev
;
2266 struct device_node
*np
= pdev
? pdev
->dev
.of_node
: NULL
;
2269 if (device_property_read_u8_array(dev
, "sas-addr", hisi_hba
->sas_addr
,
2271 dev_err(dev
, "could not get property sas-addr\n");
2277 * These properties are only required for platform device-based
2278 * controller with DT firmware.
2280 hisi_hba
->ctrl
= syscon_regmap_lookup_by_phandle(np
,
2281 "hisilicon,sas-syscon");
2282 if (IS_ERR(hisi_hba
->ctrl
)) {
2283 dev_err(dev
, "could not get syscon\n");
2287 if (device_property_read_u32(dev
, "ctrl-reset-reg",
2288 &hisi_hba
->ctrl_reset_reg
)) {
2289 dev_err(dev
, "could not get property ctrl-reset-reg\n");
2293 if (device_property_read_u32(dev
, "ctrl-reset-sts-reg",
2294 &hisi_hba
->ctrl_reset_sts_reg
)) {
2295 dev_err(dev
, "could not get property ctrl-reset-sts-reg\n");
2299 if (device_property_read_u32(dev
, "ctrl-clock-ena-reg",
2300 &hisi_hba
->ctrl_clock_ena_reg
)) {
2301 dev_err(dev
, "could not get property ctrl-clock-ena-reg\n");
2306 refclk
= devm_clk_get(dev
, NULL
);
2308 dev_dbg(dev
, "no ref clk property\n");
2310 hisi_hba
->refclk_frequency_mhz
= clk_get_rate(refclk
) / 1000000;
2312 if (device_property_read_u32(dev
, "phy-count", &hisi_hba
->n_phy
)) {
2313 dev_err(dev
, "could not get property phy-count\n");
2317 if (device_property_read_u32(dev
, "queue-count",
2318 &hisi_hba
->queue_count
)) {
2319 dev_err(dev
, "could not get property queue-count\n");
2325 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info
);
2327 static struct Scsi_Host
*hisi_sas_shost_alloc(struct platform_device
*pdev
,
2328 const struct hisi_sas_hw
*hw
)
2330 struct resource
*res
;
2331 struct Scsi_Host
*shost
;
2332 struct hisi_hba
*hisi_hba
;
2333 struct device
*dev
= &pdev
->dev
;
2336 shost
= scsi_host_alloc(hw
->sht
, sizeof(*hisi_hba
));
2338 dev_err(dev
, "scsi host alloc failed\n");
2341 hisi_hba
= shost_priv(shost
);
2343 INIT_WORK(&hisi_hba
->rst_work
, hisi_sas_rst_work_handler
);
2345 hisi_hba
->dev
= dev
;
2346 hisi_hba
->platform_dev
= pdev
;
2347 hisi_hba
->shost
= shost
;
2348 SHOST_TO_SAS_HA(shost
) = &hisi_hba
->sha
;
2350 timer_setup(&hisi_hba
->timer
, NULL
, 0);
2352 if (hisi_sas_get_fw_info(hisi_hba
) < 0)
2355 error
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64));
2357 dev_err(dev
, "No usable DMA addressing method\n");
2361 hisi_hba
->regs
= devm_platform_ioremap_resource(pdev
, 0);
2362 if (IS_ERR(hisi_hba
->regs
))
2365 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2367 hisi_hba
->sgpio_regs
= devm_ioremap_resource(dev
, res
);
2368 if (IS_ERR(hisi_hba
->sgpio_regs
))
2372 if (hisi_sas_alloc(hisi_hba
)) {
2373 hisi_sas_free(hisi_hba
);
2379 scsi_host_put(shost
);
2380 dev_err(dev
, "shost alloc failed\n");
2384 static int hisi_sas_interrupt_preinit(struct hisi_hba
*hisi_hba
)
2386 if (hisi_hba
->hw
->interrupt_preinit
)
2387 return hisi_hba
->hw
->interrupt_preinit(hisi_hba
);
2391 int hisi_sas_probe(struct platform_device
*pdev
,
2392 const struct hisi_sas_hw
*hw
)
2394 struct Scsi_Host
*shost
;
2395 struct hisi_hba
*hisi_hba
;
2396 struct device
*dev
= &pdev
->dev
;
2397 struct asd_sas_phy
**arr_phy
;
2398 struct asd_sas_port
**arr_port
;
2399 struct sas_ha_struct
*sha
;
2400 int rc
, phy_nr
, port_nr
, i
;
2402 shost
= hisi_sas_shost_alloc(pdev
, hw
);
2406 sha
= SHOST_TO_SAS_HA(shost
);
2407 hisi_hba
= shost_priv(shost
);
2408 platform_set_drvdata(pdev
, sha
);
2410 phy_nr
= port_nr
= hisi_hba
->n_phy
;
2412 arr_phy
= devm_kcalloc(dev
, phy_nr
, sizeof(void *), GFP_KERNEL
);
2413 arr_port
= devm_kcalloc(dev
, port_nr
, sizeof(void *), GFP_KERNEL
);
2414 if (!arr_phy
|| !arr_port
) {
2419 sha
->sas_phy
= arr_phy
;
2420 sha
->sas_port
= arr_port
;
2421 sha
->lldd_ha
= hisi_hba
;
2423 shost
->transportt
= hisi_sas_stt
;
2424 shost
->max_id
= HISI_SAS_MAX_DEVICES
;
2425 shost
->max_lun
= ~0;
2426 shost
->max_channel
= 1;
2427 shost
->max_cmd_len
= 16;
2428 if (hisi_hba
->hw
->slot_index_alloc
) {
2429 shost
->can_queue
= HISI_SAS_MAX_COMMANDS
;
2430 shost
->cmd_per_lun
= HISI_SAS_MAX_COMMANDS
;
2432 shost
->can_queue
= HISI_SAS_UNRESERVED_IPTT
;
2433 shost
->cmd_per_lun
= HISI_SAS_UNRESERVED_IPTT
;
2436 sha
->sas_ha_name
= DRV_NAME
;
2437 sha
->dev
= hisi_hba
->dev
;
2438 sha
->lldd_module
= THIS_MODULE
;
2439 sha
->sas_addr
= &hisi_hba
->sas_addr
[0];
2440 sha
->num_phys
= hisi_hba
->n_phy
;
2441 sha
->core
.shost
= hisi_hba
->shost
;
2443 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
2444 sha
->sas_phy
[i
] = &hisi_hba
->phy
[i
].sas_phy
;
2445 sha
->sas_port
[i
] = &hisi_hba
->port
[i
].sas_port
;
2448 rc
= hisi_sas_interrupt_preinit(hisi_hba
);
2452 rc
= scsi_add_host(shost
, &pdev
->dev
);
2456 rc
= sas_register_ha(sha
);
2458 goto err_out_register_ha
;
2460 rc
= hisi_hba
->hw
->hw_init(hisi_hba
);
2462 goto err_out_hw_init
;
2464 scsi_scan_host(shost
);
2469 sas_unregister_ha(sha
);
2470 err_out_register_ha
:
2471 scsi_remove_host(shost
);
2473 hisi_sas_free(hisi_hba
);
2474 scsi_host_put(shost
);
2477 EXPORT_SYMBOL_GPL(hisi_sas_probe
);
2479 int hisi_sas_remove(struct platform_device
*pdev
)
2481 struct sas_ha_struct
*sha
= platform_get_drvdata(pdev
);
2482 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
2483 struct Scsi_Host
*shost
= sha
->core
.shost
;
2485 del_timer_sync(&hisi_hba
->timer
);
2487 sas_unregister_ha(sha
);
2488 sas_remove_host(sha
->core
.shost
);
2490 hisi_sas_free(hisi_hba
);
2491 scsi_host_put(shost
);
2494 EXPORT_SYMBOL_GPL(hisi_sas_remove
);
2496 #if IS_ENABLED(CONFIG_SCSI_HISI_SAS_DEBUGFS_DEFAULT_ENABLE)
2497 #define DEBUGFS_ENABLE_DEFAULT "enabled"
2498 bool hisi_sas_debugfs_enable
= true;
2499 u32 hisi_sas_debugfs_dump_count
= 50;
2501 #define DEBUGFS_ENABLE_DEFAULT "disabled"
2502 bool hisi_sas_debugfs_enable
;
2503 u32 hisi_sas_debugfs_dump_count
= 1;
2506 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable
);
2507 module_param_named(debugfs_enable
, hisi_sas_debugfs_enable
, bool, 0444);
2508 MODULE_PARM_DESC(hisi_sas_debugfs_enable
,
2509 "Enable driver debugfs (default "DEBUGFS_ENABLE_DEFAULT
")");
2511 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count
);
2512 module_param_named(debugfs_dump_count
, hisi_sas_debugfs_dump_count
, uint
, 0444);
2513 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count
, "Number of debugfs dumps to allow");
2515 struct dentry
*hisi_sas_debugfs_dir
;
2516 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dir
);
2518 static __init
int hisi_sas_init(void)
2520 hisi_sas_stt
= sas_domain_attach_transport(&hisi_sas_transport_ops
);
2524 if (hisi_sas_debugfs_enable
) {
2525 hisi_sas_debugfs_dir
= debugfs_create_dir("hisi_sas", NULL
);
2526 if (hisi_sas_debugfs_dump_count
> HISI_SAS_MAX_DEBUGFS_DUMP
) {
2527 pr_info("hisi_sas: Limiting debugfs dump count\n");
2528 hisi_sas_debugfs_dump_count
= HISI_SAS_MAX_DEBUGFS_DUMP
;
2535 static __exit
void hisi_sas_exit(void)
2537 sas_release_transport(hisi_sas_stt
);
2539 debugfs_remove(hisi_sas_debugfs_dir
);
2542 module_init(hisi_sas_init
);
2543 module_exit(hisi_sas_exit
);
2545 MODULE_LICENSE("GPL");
2546 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2547 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2548 MODULE_ALIAS("platform:" DRV_NAME
);