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
,
27 static void hisi_sas_release_task(struct hisi_hba
*hisi_hba
,
28 struct domain_device
*device
);
29 static void hisi_sas_dev_gone(struct domain_device
*device
);
31 u8
hisi_sas_get_ata_protocol(struct host_to_dev_fis
*fis
, int direction
)
33 switch (fis
->command
) {
34 case ATA_CMD_FPDMA_WRITE
:
35 case ATA_CMD_FPDMA_READ
:
36 case ATA_CMD_FPDMA_RECV
:
37 case ATA_CMD_FPDMA_SEND
:
38 case ATA_CMD_NCQ_NON_DATA
:
39 return HISI_SAS_SATA_PROTOCOL_FPDMA
;
41 case ATA_CMD_DOWNLOAD_MICRO
:
43 case ATA_CMD_PMP_READ
:
44 case ATA_CMD_READ_LOG_EXT
:
45 case ATA_CMD_PIO_READ
:
46 case ATA_CMD_PIO_READ_EXT
:
47 case ATA_CMD_PMP_WRITE
:
48 case ATA_CMD_WRITE_LOG_EXT
:
49 case ATA_CMD_PIO_WRITE
:
50 case ATA_CMD_PIO_WRITE_EXT
:
51 return HISI_SAS_SATA_PROTOCOL_PIO
;
54 case ATA_CMD_DOWNLOAD_MICRO_DMA
:
55 case ATA_CMD_PMP_READ_DMA
:
56 case ATA_CMD_PMP_WRITE_DMA
:
58 case ATA_CMD_READ_EXT
:
59 case ATA_CMD_READ_LOG_DMA_EXT
:
60 case ATA_CMD_READ_STREAM_DMA_EXT
:
61 case ATA_CMD_TRUSTED_RCV_DMA
:
62 case ATA_CMD_TRUSTED_SND_DMA
:
64 case ATA_CMD_WRITE_EXT
:
65 case ATA_CMD_WRITE_FUA_EXT
:
66 case ATA_CMD_WRITE_QUEUED
:
67 case ATA_CMD_WRITE_LOG_DMA_EXT
:
68 case ATA_CMD_WRITE_STREAM_DMA_EXT
:
69 case ATA_CMD_ZAC_MGMT_IN
:
70 return HISI_SAS_SATA_PROTOCOL_DMA
;
72 case ATA_CMD_CHK_POWER
:
73 case ATA_CMD_DEV_RESET
:
76 case ATA_CMD_FLUSH_EXT
:
78 case ATA_CMD_VERIFY_EXT
:
79 case ATA_CMD_SET_FEATURES
:
81 case ATA_CMD_STANDBYNOW1
:
82 case ATA_CMD_ZAC_MGMT_OUT
:
83 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
86 if (fis
->command
== ATA_CMD_SET_MAX
) {
87 switch (fis
->features
) {
88 case ATA_SET_MAX_PASSWD
:
89 case ATA_SET_MAX_LOCK
:
90 return HISI_SAS_SATA_PROTOCOL_PIO
;
92 case ATA_SET_MAX_PASSWD_DMA
:
93 case ATA_SET_MAX_UNLOCK_DMA
:
94 return HISI_SAS_SATA_PROTOCOL_DMA
;
97 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
100 if (direction
== DMA_NONE
)
101 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
102 return HISI_SAS_SATA_PROTOCOL_PIO
;
106 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol
);
108 void hisi_sas_sata_done(struct sas_task
*task
,
109 struct hisi_sas_slot
*slot
)
111 struct task_status_struct
*ts
= &task
->task_status
;
112 struct ata_task_resp
*resp
= (struct ata_task_resp
*)ts
->buf
;
113 struct hisi_sas_status_buffer
*status_buf
=
114 hisi_sas_status_buf_addr_mem(slot
);
115 u8
*iu
= &status_buf
->iu
[0];
116 struct dev_to_host_fis
*d2h
= (struct dev_to_host_fis
*)iu
;
118 resp
->frame_len
= sizeof(struct dev_to_host_fis
);
119 memcpy(&resp
->ending_fis
[0], d2h
, sizeof(struct dev_to_host_fis
));
121 ts
->buf_valid_size
= sizeof(*resp
);
123 EXPORT_SYMBOL_GPL(hisi_sas_sata_done
);
125 int hisi_sas_get_ncq_tag(struct sas_task
*task
, u32
*tag
)
127 struct ata_queued_cmd
*qc
= task
->uldd_task
;
130 if (qc
->tf
.command
== ATA_CMD_FPDMA_WRITE
||
131 qc
->tf
.command
== ATA_CMD_FPDMA_READ
) {
138 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag
);
141 * This function assumes linkrate mask fits in 8 bits, which it
142 * does for all HW versions supported.
144 u8
hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max
)
149 max
-= SAS_LINK_RATE_1_5_GBPS
;
150 for (i
= 0; i
<= max
; i
++)
151 rate
|= 1 << (i
* 2);
154 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask
);
156 static struct hisi_hba
*dev_to_hisi_hba(struct domain_device
*device
)
158 return device
->port
->ha
->lldd_ha
;
161 struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
)
163 return container_of(sas_port
, struct hisi_sas_port
, sas_port
);
165 EXPORT_SYMBOL_GPL(to_hisi_sas_port
);
167 void hisi_sas_stop_phys(struct hisi_hba
*hisi_hba
)
171 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++)
172 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
174 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys
);
176 static void hisi_sas_slot_index_clear(struct hisi_hba
*hisi_hba
, int slot_idx
)
178 void *bitmap
= hisi_hba
->slot_index_tags
;
180 clear_bit(slot_idx
, bitmap
);
183 static void hisi_sas_slot_index_free(struct hisi_hba
*hisi_hba
, int slot_idx
)
185 hisi_sas_slot_index_clear(hisi_hba
, slot_idx
);
188 static void hisi_sas_slot_index_set(struct hisi_hba
*hisi_hba
, int slot_idx
)
190 void *bitmap
= hisi_hba
->slot_index_tags
;
192 set_bit(slot_idx
, bitmap
);
195 static int hisi_sas_slot_index_alloc(struct hisi_hba
*hisi_hba
, int *slot_idx
)
198 void *bitmap
= hisi_hba
->slot_index_tags
;
200 index
= find_next_zero_bit(bitmap
, hisi_hba
->slot_index_count
,
201 hisi_hba
->last_slot_index
+ 1);
202 if (index
>= hisi_hba
->slot_index_count
) {
203 index
= find_next_zero_bit(bitmap
, hisi_hba
->slot_index_count
,
205 if (index
>= hisi_hba
->slot_index_count
)
206 return -SAS_QUEUE_FULL
;
208 hisi_sas_slot_index_set(hisi_hba
, index
);
210 hisi_hba
->last_slot_index
= index
;
215 static void hisi_sas_slot_index_init(struct hisi_hba
*hisi_hba
)
219 for (i
= 0; i
< hisi_hba
->slot_index_count
; ++i
)
220 hisi_sas_slot_index_clear(hisi_hba
, i
);
223 void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
224 struct hisi_sas_slot
*slot
)
226 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[slot
->dlvry_queue
];
230 struct device
*dev
= hisi_hba
->dev
;
232 if (!task
->lldd_task
)
235 task
->lldd_task
= NULL
;
237 if (!sas_protocol_ata(task
->task_proto
))
239 dma_unmap_sg(dev
, task
->scatter
,
245 dma_pool_free(hisi_hba
->buffer_pool
, slot
->buf
, slot
->buf_dma
);
247 spin_lock_irqsave(&dq
->lock
, flags
);
248 list_del_init(&slot
->entry
);
249 spin_unlock_irqrestore(&dq
->lock
, flags
);
253 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
254 hisi_sas_slot_index_free(hisi_hba
, slot
->idx
);
255 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
257 /* slot memory is fully zeroed when it is reused */
259 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free
);
261 static void hisi_sas_task_prep_smp(struct hisi_hba
*hisi_hba
,
262 struct hisi_sas_slot
*slot
)
264 hisi_hba
->hw
->prep_smp(hisi_hba
, slot
);
267 static void hisi_sas_task_prep_ssp(struct hisi_hba
*hisi_hba
,
268 struct hisi_sas_slot
*slot
)
270 hisi_hba
->hw
->prep_ssp(hisi_hba
, slot
);
273 static void hisi_sas_task_prep_ata(struct hisi_hba
*hisi_hba
,
274 struct hisi_sas_slot
*slot
)
276 hisi_hba
->hw
->prep_stp(hisi_hba
, slot
);
279 static void hisi_sas_task_prep_abort(struct hisi_hba
*hisi_hba
,
280 struct hisi_sas_slot
*slot
,
281 int device_id
, int abort_flag
, int tag_to_abort
)
283 hisi_hba
->hw
->prep_abort(hisi_hba
, slot
,
284 device_id
, abort_flag
, tag_to_abort
);
288 * This function will issue an abort TMF regardless of whether the
289 * task is in the sdev or not. Then it will do the task complete
290 * cleanup and callbacks.
292 static void hisi_sas_slot_abort(struct work_struct
*work
)
294 struct hisi_sas_slot
*abort_slot
=
295 container_of(work
, struct hisi_sas_slot
, abort_slot
);
296 struct sas_task
*task
= abort_slot
->task
;
297 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
298 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
299 struct hisi_sas_tmf_task tmf_task
;
301 struct device
*dev
= hisi_hba
->dev
;
302 int tag
= abort_slot
->idx
;
304 if (!(task
->task_proto
& SAS_PROTOCOL_SSP
)) {
305 dev_err(dev
, "cannot abort slot for non-ssp task\n");
309 int_to_scsilun(cmnd
->device
->lun
, &lun
);
310 tmf_task
.tmf
= TMF_ABORT_TASK
;
311 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
313 hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
, &tmf_task
);
315 /* Do cleanup for this task */
316 hisi_sas_slot_task_free(hisi_hba
, task
, abort_slot
);
318 task
->task_done(task
);
321 static int hisi_sas_task_prep(struct sas_task
*task
,
322 struct hisi_sas_dq
**dq_pointer
,
323 bool is_tmf
, struct hisi_sas_tmf_task
*tmf
,
326 struct domain_device
*device
= task
->dev
;
327 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
328 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
329 struct hisi_sas_port
*port
;
330 struct hisi_sas_slot
*slot
;
331 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
332 struct asd_sas_port
*sas_port
= device
->port
;
333 struct device
*dev
= hisi_hba
->dev
;
334 int dlvry_queue_slot
, dlvry_queue
, rc
, slot_idx
;
335 int n_elem
= 0, n_elem_req
= 0, n_elem_resp
= 0;
336 unsigned long flags
, flags_dq
;
337 struct hisi_sas_dq
*dq
;
341 struct task_status_struct
*ts
= &task
->task_status
;
343 ts
->resp
= SAS_TASK_UNDELIVERED
;
344 ts
->stat
= SAS_PHY_DOWN
;
346 * libsas will use dev->port, should
347 * not call task_done for sata
349 if (device
->dev_type
!= SAS_SATA_DEV
)
350 task
->task_done(task
);
354 if (DEV_IS_GONE(sas_dev
)) {
356 dev_info(dev
, "task prep: device %d not ready\n",
359 dev_info(dev
, "task prep: device %016llx not ready\n",
360 SAS_ADDR(device
->sas_addr
));
365 *dq_pointer
= dq
= sas_dev
->dq
;
367 port
= to_hisi_sas_port(sas_port
);
368 if (port
&& !port
->port_attached
) {
369 dev_info(dev
, "task prep: %s port%d not attach device\n",
370 (dev_is_sata(device
)) ?
377 if (!sas_protocol_ata(task
->task_proto
)) {
378 unsigned int req_len
, resp_len
;
380 if (task
->num_scatter
) {
381 n_elem
= dma_map_sg(dev
, task
->scatter
,
382 task
->num_scatter
, task
->data_dir
);
387 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
388 n_elem_req
= dma_map_sg(dev
, &task
->smp_task
.smp_req
,
394 req_len
= sg_dma_len(&task
->smp_task
.smp_req
);
397 goto err_out_dma_unmap
;
399 n_elem_resp
= dma_map_sg(dev
, &task
->smp_task
.smp_resp
,
403 goto err_out_dma_unmap
;
405 resp_len
= sg_dma_len(&task
->smp_task
.smp_resp
);
406 if (resp_len
& 0x3) {
408 goto err_out_dma_unmap
;
412 n_elem
= task
->num_scatter
;
414 if (n_elem
> HISI_SAS_SGE_PAGE_CNT
) {
415 dev_err(dev
, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
418 goto err_out_dma_unmap
;
421 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
422 if (hisi_hba
->hw
->slot_index_alloc
)
423 rc
= hisi_hba
->hw
->slot_index_alloc(hisi_hba
, &slot_idx
,
426 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
427 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
429 goto err_out_dma_unmap
;
431 slot
= &hisi_hba
->slot_info
[slot_idx
];
432 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
434 slot
->buf
= dma_pool_alloc(hisi_hba
->buffer_pool
,
435 GFP_ATOMIC
, &slot
->buf_dma
);
441 spin_lock_irqsave(&dq
->lock
, flags_dq
);
442 wr_q_index
= hisi_hba
->hw
->get_free_slot(hisi_hba
, dq
);
443 if (wr_q_index
< 0) {
444 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
448 list_add_tail(&slot
->delivery
, &dq
->list
);
449 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
451 dlvry_queue
= dq
->id
;
452 dlvry_queue_slot
= wr_q_index
;
454 slot
->idx
= slot_idx
;
455 slot
->n_elem
= n_elem
;
456 slot
->dlvry_queue
= dlvry_queue
;
457 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
458 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
459 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
463 slot
->is_internal
= is_tmf
;
464 task
->lldd_task
= slot
;
465 INIT_WORK(&slot
->abort_slot
, hisi_sas_slot_abort
);
467 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
468 memset(hisi_sas_cmd_hdr_addr_mem(slot
), 0, HISI_SAS_COMMAND_TABLE_SZ
);
469 memset(hisi_sas_status_buf_addr_mem(slot
), 0, HISI_SAS_STATUS_BUF_SZ
);
471 switch (task
->task_proto
) {
472 case SAS_PROTOCOL_SMP
:
473 hisi_sas_task_prep_smp(hisi_hba
, slot
);
475 case SAS_PROTOCOL_SSP
:
476 hisi_sas_task_prep_ssp(hisi_hba
, slot
);
478 case SAS_PROTOCOL_SATA
:
479 case SAS_PROTOCOL_STP
:
480 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:
481 hisi_sas_task_prep_ata(hisi_hba
, slot
);
484 dev_err(dev
, "task prep: unknown/unsupported proto (0x%x)\n",
489 spin_lock_irqsave(&dq
->lock
, flags
);
490 list_add_tail(&slot
->entry
, &sas_dev
->list
);
491 spin_unlock_irqrestore(&dq
->lock
, flags
);
492 spin_lock_irqsave(&task
->task_state_lock
, flags
);
493 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
494 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
502 dma_pool_free(hisi_hba
->buffer_pool
, slot
->buf
,
505 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
506 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
507 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
509 if (!sas_protocol_ata(task
->task_proto
)) {
510 if (task
->num_scatter
) {
511 dma_unmap_sg(dev
, task
->scatter
, task
->num_scatter
,
513 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
515 dma_unmap_sg(dev
, &task
->smp_task
.smp_req
,
518 dma_unmap_sg(dev
, &task
->smp_task
.smp_resp
,
523 dev_err(dev
, "task prep: failed[%d]!\n", rc
);
527 static int hisi_sas_task_exec(struct sas_task
*task
, gfp_t gfp_flags
,
528 bool is_tmf
, struct hisi_sas_tmf_task
*tmf
)
533 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
534 struct device
*dev
= hisi_hba
->dev
;
535 struct hisi_sas_dq
*dq
= NULL
;
537 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
)))
540 /* protect task_prep and start_delivery sequence */
541 rc
= hisi_sas_task_prep(task
, &dq
, is_tmf
, tmf
, &pass
);
543 dev_err(dev
, "task exec: failed[%d]!\n", rc
);
546 spin_lock_irqsave(&dq
->lock
, flags
);
547 hisi_hba
->hw
->start_delivery(dq
);
548 spin_unlock_irqrestore(&dq
->lock
, flags
);
554 static void hisi_sas_bytes_dmaed(struct hisi_hba
*hisi_hba
, int phy_no
)
556 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
557 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
558 struct sas_ha_struct
*sas_ha
;
560 if (!phy
->phy_attached
)
563 sas_ha
= &hisi_hba
->sha
;
564 sas_ha
->notify_phy_event(sas_phy
, PHYE_OOB_DONE
);
567 struct sas_phy
*sphy
= sas_phy
->phy
;
569 sphy
->negotiated_linkrate
= sas_phy
->linkrate
;
570 sphy
->minimum_linkrate_hw
= SAS_LINK_RATE_1_5_GBPS
;
571 sphy
->maximum_linkrate_hw
=
572 hisi_hba
->hw
->phy_get_max_linkrate();
573 if (sphy
->minimum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
574 sphy
->minimum_linkrate
= phy
->minimum_linkrate
;
576 if (sphy
->maximum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
577 sphy
->maximum_linkrate
= phy
->maximum_linkrate
;
580 if (phy
->phy_type
& PORT_TYPE_SAS
) {
581 struct sas_identify_frame
*id
;
583 id
= (struct sas_identify_frame
*)phy
->frame_rcvd
;
584 id
->dev_type
= phy
->identify
.device_type
;
585 id
->initiator_bits
= SAS_PROTOCOL_ALL
;
586 id
->target_bits
= phy
->identify
.target_port_protocols
;
587 } else if (phy
->phy_type
& PORT_TYPE_SATA
) {
591 sas_phy
->frame_rcvd_size
= phy
->frame_rcvd_size
;
592 sas_ha
->notify_port_event(sas_phy
, PORTE_BYTES_DMAED
);
595 static struct hisi_sas_device
*hisi_sas_alloc_dev(struct domain_device
*device
)
597 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
598 struct hisi_sas_device
*sas_dev
= NULL
;
600 int last
= hisi_hba
->last_dev_id
;
601 int first
= (hisi_hba
->last_dev_id
+ 1) % HISI_SAS_MAX_DEVICES
;
604 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
605 for (i
= first
; i
!= last
; i
%= HISI_SAS_MAX_DEVICES
) {
606 if (hisi_hba
->devices
[i
].dev_type
== SAS_PHY_UNUSED
) {
607 int queue
= i
% hisi_hba
->queue_count
;
608 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[queue
];
610 hisi_hba
->devices
[i
].device_id
= i
;
611 sas_dev
= &hisi_hba
->devices
[i
];
612 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
613 sas_dev
->dev_type
= device
->dev_type
;
614 sas_dev
->hisi_hba
= hisi_hba
;
615 sas_dev
->sas_device
= device
;
617 INIT_LIST_HEAD(&hisi_hba
->devices
[i
].list
);
622 hisi_hba
->last_dev_id
= i
;
623 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
628 #define HISI_SAS_SRST_ATA_DISK_CNT 3
629 static int hisi_sas_init_device(struct domain_device
*device
)
631 int rc
= TMF_RESP_FUNC_COMPLETE
;
633 struct hisi_sas_tmf_task tmf_task
;
634 int retry
= HISI_SAS_SRST_ATA_DISK_CNT
;
635 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
637 switch (device
->dev_type
) {
639 int_to_scsilun(0, &lun
);
641 tmf_task
.tmf
= TMF_CLEAR_TASK_SET
;
642 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
.scsi_lun
,
644 if (rc
== TMF_RESP_FUNC_COMPLETE
)
645 hisi_sas_release_task(hisi_hba
, device
);
649 case SAS_SATA_PM_PORT
:
650 case SAS_SATA_PENDING
:
651 while (retry
-- > 0) {
652 rc
= hisi_sas_softreset_ata_disk(device
);
664 static int hisi_sas_dev_found(struct domain_device
*device
)
666 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
667 struct domain_device
*parent_dev
= device
->parent
;
668 struct hisi_sas_device
*sas_dev
;
669 struct device
*dev
= hisi_hba
->dev
;
672 if (hisi_hba
->hw
->alloc_dev
)
673 sas_dev
= hisi_hba
->hw
->alloc_dev(device
);
675 sas_dev
= hisi_sas_alloc_dev(device
);
677 dev_err(dev
, "fail alloc dev: max support %d devices\n",
678 HISI_SAS_MAX_DEVICES
);
682 device
->lldd_dev
= sas_dev
;
683 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
685 if (parent_dev
&& DEV_IS_EXPANDER(parent_dev
->dev_type
)) {
687 u8 phy_num
= parent_dev
->ex_dev
.num_phys
;
690 for (phy_no
= 0; phy_no
< phy_num
; phy_no
++) {
691 phy
= &parent_dev
->ex_dev
.ex_phy
[phy_no
];
692 if (SAS_ADDR(phy
->attached_sas_addr
) ==
693 SAS_ADDR(device
->sas_addr
)) {
694 sas_dev
->attached_phy
= phy_no
;
699 if (phy_no
== phy_num
) {
700 dev_info(dev
, "dev found: no attached "
701 "dev:%016llx at ex:%016llx\n",
702 SAS_ADDR(device
->sas_addr
),
703 SAS_ADDR(parent_dev
->sas_addr
));
709 dev_info(dev
, "dev[%d:%x] found\n",
710 sas_dev
->device_id
, sas_dev
->dev_type
);
712 rc
= hisi_sas_init_device(device
);
718 hisi_sas_dev_gone(device
);
722 int hisi_sas_slave_configure(struct scsi_device
*sdev
)
724 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
725 int ret
= sas_slave_configure(sdev
);
729 if (!dev_is_sata(dev
))
730 sas_change_queue_depth(sdev
, 64);
734 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure
);
736 void hisi_sas_scan_start(struct Scsi_Host
*shost
)
738 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
740 hisi_hba
->hw
->phys_init(hisi_hba
);
742 EXPORT_SYMBOL_GPL(hisi_sas_scan_start
);
744 int hisi_sas_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
746 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
747 struct sas_ha_struct
*sha
= &hisi_hba
->sha
;
749 /* Wait for PHY up interrupt to occur */
756 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished
);
758 static void hisi_sas_phyup_work(struct work_struct
*work
)
760 struct hisi_sas_phy
*phy
=
761 container_of(work
, typeof(*phy
), works
[HISI_PHYE_PHY_UP
]);
762 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
763 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
764 int phy_no
= sas_phy
->id
;
766 hisi_hba
->hw
->sl_notify(hisi_hba
, phy_no
); /* This requires a sleep */
767 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
770 static void hisi_sas_linkreset_work(struct work_struct
*work
)
772 struct hisi_sas_phy
*phy
=
773 container_of(work
, typeof(*phy
), works
[HISI_PHYE_LINK_RESET
]);
774 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
776 hisi_sas_control_phy(sas_phy
, PHY_FUNC_LINK_RESET
, NULL
);
779 static const work_func_t hisi_sas_phye_fns
[HISI_PHYES_NUM
] = {
780 [HISI_PHYE_PHY_UP
] = hisi_sas_phyup_work
,
781 [HISI_PHYE_LINK_RESET
] = hisi_sas_linkreset_work
,
784 bool hisi_sas_notify_phy_event(struct hisi_sas_phy
*phy
,
785 enum hisi_sas_phy_event event
)
787 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
789 if (WARN_ON(event
>= HISI_PHYES_NUM
))
792 return queue_work(hisi_hba
->wq
, &phy
->works
[event
]);
794 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event
);
796 static void hisi_sas_phy_init(struct hisi_hba
*hisi_hba
, int phy_no
)
798 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
799 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
802 phy
->hisi_hba
= hisi_hba
;
804 phy
->minimum_linkrate
= SAS_LINK_RATE_1_5_GBPS
;
805 phy
->maximum_linkrate
= hisi_hba
->hw
->phy_get_max_linkrate();
806 sas_phy
->enabled
= (phy_no
< hisi_hba
->n_phy
) ? 1 : 0;
807 sas_phy
->class = SAS
;
808 sas_phy
->iproto
= SAS_PROTOCOL_ALL
;
810 sas_phy
->type
= PHY_TYPE_PHYSICAL
;
811 sas_phy
->role
= PHY_ROLE_INITIATOR
;
812 sas_phy
->oob_mode
= OOB_NOT_CONNECTED
;
813 sas_phy
->linkrate
= SAS_LINK_RATE_UNKNOWN
;
814 sas_phy
->id
= phy_no
;
815 sas_phy
->sas_addr
= &hisi_hba
->sas_addr
[0];
816 sas_phy
->frame_rcvd
= &phy
->frame_rcvd
[0];
817 sas_phy
->ha
= (struct sas_ha_struct
*)hisi_hba
->shost
->hostdata
;
818 sas_phy
->lldd_phy
= phy
;
820 for (i
= 0; i
< HISI_PHYES_NUM
; i
++)
821 INIT_WORK(&phy
->works
[i
], hisi_sas_phye_fns
[i
]);
824 static void hisi_sas_port_notify_formed(struct asd_sas_phy
*sas_phy
)
826 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
827 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
828 struct hisi_sas_phy
*phy
= sas_phy
->lldd_phy
;
829 struct asd_sas_port
*sas_port
= sas_phy
->port
;
830 struct hisi_sas_port
*port
= to_hisi_sas_port(sas_port
);
836 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
837 port
->port_attached
= 1;
838 port
->id
= phy
->port_id
;
840 sas_port
->lldd_port
= port
;
841 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
844 static void hisi_sas_do_release_task(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
845 struct hisi_sas_slot
*slot
)
849 struct task_status_struct
*ts
;
851 ts
= &task
->task_status
;
853 ts
->resp
= SAS_TASK_COMPLETE
;
854 ts
->stat
= SAS_ABORTED_TASK
;
855 spin_lock_irqsave(&task
->task_state_lock
, flags
);
856 task
->task_state_flags
&=
857 ~(SAS_TASK_STATE_PENDING
| SAS_TASK_AT_INITIATOR
);
858 task
->task_state_flags
|= SAS_TASK_STATE_DONE
;
859 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
862 hisi_sas_slot_task_free(hisi_hba
, task
, slot
);
865 /* hisi_hba.lock should be locked */
866 static void hisi_sas_release_task(struct hisi_hba
*hisi_hba
,
867 struct domain_device
*device
)
869 struct hisi_sas_slot
*slot
, *slot2
;
870 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
872 list_for_each_entry_safe(slot
, slot2
, &sas_dev
->list
, entry
)
873 hisi_sas_do_release_task(hisi_hba
, slot
->task
, slot
);
876 void hisi_sas_release_tasks(struct hisi_hba
*hisi_hba
)
878 struct hisi_sas_device
*sas_dev
;
879 struct domain_device
*device
;
882 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
883 sas_dev
= &hisi_hba
->devices
[i
];
884 device
= sas_dev
->sas_device
;
886 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
) ||
890 hisi_sas_release_task(hisi_hba
, device
);
893 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks
);
895 static void hisi_sas_dereg_device(struct hisi_hba
*hisi_hba
,
896 struct domain_device
*device
)
898 if (hisi_hba
->hw
->dereg_device
)
899 hisi_hba
->hw
->dereg_device(hisi_hba
, device
);
902 static void hisi_sas_dev_gone(struct domain_device
*device
)
904 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
905 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
906 struct device
*dev
= hisi_hba
->dev
;
908 dev_info(dev
, "dev[%d:%x] is gone\n",
909 sas_dev
->device_id
, sas_dev
->dev_type
);
911 if (!test_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
)) {
912 hisi_sas_internal_task_abort(hisi_hba
, device
,
913 HISI_SAS_INT_ABT_DEV
, 0);
915 hisi_sas_dereg_device(hisi_hba
, device
);
917 hisi_hba
->hw
->clear_itct(hisi_hba
, sas_dev
);
918 device
->lldd_dev
= NULL
;
921 if (hisi_hba
->hw
->free_device
)
922 hisi_hba
->hw
->free_device(sas_dev
);
923 sas_dev
->dev_type
= SAS_PHY_UNUSED
;
926 static int hisi_sas_queue_command(struct sas_task
*task
, gfp_t gfp_flags
)
928 return hisi_sas_task_exec(task
, gfp_flags
, 0, NULL
);
931 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
934 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
935 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
936 int phy_no
= sas_phy
->id
;
939 case PHY_FUNC_HARD_RESET
:
940 hisi_hba
->hw
->phy_hard_reset(hisi_hba
, phy_no
);
943 case PHY_FUNC_LINK_RESET
:
944 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
946 hisi_hba
->hw
->phy_start(hisi_hba
, phy_no
);
949 case PHY_FUNC_DISABLE
:
950 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
953 case PHY_FUNC_SET_LINK_RATE
:
954 hisi_hba
->hw
->phy_set_linkrate(hisi_hba
, phy_no
, funcdata
);
956 case PHY_FUNC_GET_EVENTS
:
957 if (hisi_hba
->hw
->get_events
) {
958 hisi_hba
->hw
->get_events(hisi_hba
, phy_no
);
962 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
969 static void hisi_sas_task_done(struct sas_task
*task
)
971 if (!del_timer(&task
->slow_task
->timer
))
973 complete(&task
->slow_task
->completion
);
976 static void hisi_sas_tmf_timedout(struct timer_list
*t
)
978 struct sas_task_slow
*slow
= from_timer(slow
, t
, timer
);
979 struct sas_task
*task
= slow
->task
;
982 spin_lock_irqsave(&task
->task_state_lock
, flags
);
983 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
984 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
985 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
987 complete(&task
->slow_task
->completion
);
990 #define TASK_TIMEOUT 20
992 #define INTERNAL_ABORT_TIMEOUT 6
993 static int hisi_sas_exec_internal_tmf_task(struct domain_device
*device
,
994 void *parameter
, u32 para_len
,
995 struct hisi_sas_tmf_task
*tmf
)
997 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
998 struct hisi_hba
*hisi_hba
= sas_dev
->hisi_hba
;
999 struct device
*dev
= hisi_hba
->dev
;
1000 struct sas_task
*task
;
1003 for (retry
= 0; retry
< TASK_RETRY
; retry
++) {
1004 task
= sas_alloc_slow_task(GFP_KERNEL
);
1009 task
->task_proto
= device
->tproto
;
1011 if (dev_is_sata(device
)) {
1012 task
->ata_task
.device_control_reg_update
= 1;
1013 memcpy(&task
->ata_task
.fis
, parameter
, para_len
);
1015 memcpy(&task
->ssp_task
, parameter
, para_len
);
1017 task
->task_done
= hisi_sas_task_done
;
1019 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
1020 task
->slow_task
->timer
.expires
= jiffies
+ TASK_TIMEOUT
*HZ
;
1021 add_timer(&task
->slow_task
->timer
);
1023 res
= hisi_sas_task_exec(task
, GFP_KERNEL
, 1, tmf
);
1026 del_timer(&task
->slow_task
->timer
);
1027 dev_err(dev
, "abort tmf: executing internal task failed: %d\n",
1032 wait_for_completion(&task
->slow_task
->completion
);
1033 res
= TMF_RESP_FUNC_FAILED
;
1034 /* Even TMF timed out, return direct. */
1035 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
1036 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
1037 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1039 dev_err(dev
, "abort tmf: TMF task timeout and not done\n");
1045 dev_err(dev
, "abort tmf: TMF task timeout\n");
1048 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1049 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
1050 res
= TMF_RESP_FUNC_COMPLETE
;
1054 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1055 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
1056 res
= TMF_RESP_FUNC_SUCC
;
1060 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1061 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
1062 /* no error, but return the number of bytes of
1065 dev_warn(dev
, "abort tmf: task to dev %016llx "
1066 "resp: 0x%x sts 0x%x underrun\n",
1067 SAS_ADDR(device
->sas_addr
),
1068 task
->task_status
.resp
,
1069 task
->task_status
.stat
);
1070 res
= task
->task_status
.residual
;
1074 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1075 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
1076 dev_warn(dev
, "abort tmf: blocked task error\n");
1081 dev_warn(dev
, "abort tmf: task to dev "
1082 "%016llx resp: 0x%x status 0x%x\n",
1083 SAS_ADDR(device
->sas_addr
), task
->task_status
.resp
,
1084 task
->task_status
.stat
);
1085 sas_free_task(task
);
1089 if (retry
== TASK_RETRY
)
1090 dev_warn(dev
, "abort tmf: executing internal task failed!\n");
1091 sas_free_task(task
);
1095 static void hisi_sas_fill_ata_reset_cmd(struct ata_device
*dev
,
1096 bool reset
, int pmp
, u8
*fis
)
1098 struct ata_taskfile tf
;
1100 ata_tf_init(dev
, &tf
);
1104 tf
.ctl
&= ~ATA_SRST
;
1105 tf
.command
= ATA_CMD_DEV_RESET
;
1106 ata_tf_to_fis(&tf
, pmp
, 0, fis
);
1109 static int hisi_sas_softreset_ata_disk(struct domain_device
*device
)
1112 struct ata_port
*ap
= device
->sata_dev
.ap
;
1113 struct ata_link
*link
;
1114 int rc
= TMF_RESP_FUNC_FAILED
;
1115 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1116 struct device
*dev
= hisi_hba
->dev
;
1117 int s
= sizeof(struct host_to_dev_fis
);
1119 ata_for_each_link(link
, ap
, EDGE
) {
1120 int pmp
= sata_srst_pmp(link
);
1122 hisi_sas_fill_ata_reset_cmd(link
->device
, 1, pmp
, fis
);
1123 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
, s
, NULL
);
1124 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1128 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1129 ata_for_each_link(link
, ap
, EDGE
) {
1130 int pmp
= sata_srst_pmp(link
);
1132 hisi_sas_fill_ata_reset_cmd(link
->device
, 0, pmp
, fis
);
1133 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
,
1135 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1136 dev_err(dev
, "ata disk de-reset failed\n");
1139 dev_err(dev
, "ata disk reset failed\n");
1142 if (rc
== TMF_RESP_FUNC_COMPLETE
)
1143 hisi_sas_release_task(hisi_hba
, device
);
1148 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device
*device
,
1149 u8
*lun
, struct hisi_sas_tmf_task
*tmf
)
1151 struct sas_ssp_task ssp_task
;
1153 if (!(device
->tproto
& SAS_PROTOCOL_SSP
))
1154 return TMF_RESP_FUNC_ESUPP
;
1156 memcpy(ssp_task
.LUN
, lun
, 8);
1158 return hisi_sas_exec_internal_tmf_task(device
, &ssp_task
,
1159 sizeof(ssp_task
), tmf
);
1162 static void hisi_sas_refresh_port_id(struct hisi_hba
*hisi_hba
)
1164 u32 state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1167 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1168 struct hisi_sas_device
*sas_dev
= &hisi_hba
->devices
[i
];
1169 struct domain_device
*device
= sas_dev
->sas_device
;
1170 struct asd_sas_port
*sas_port
;
1171 struct hisi_sas_port
*port
;
1172 struct hisi_sas_phy
*phy
= NULL
;
1173 struct asd_sas_phy
*sas_phy
;
1175 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
)
1176 || !device
|| !device
->port
)
1179 sas_port
= device
->port
;
1180 port
= to_hisi_sas_port(sas_port
);
1182 list_for_each_entry(sas_phy
, &sas_port
->phy_list
, port_phy_el
)
1183 if (state
& BIT(sas_phy
->id
)) {
1184 phy
= sas_phy
->lldd_phy
;
1189 port
->id
= phy
->port_id
;
1191 /* Update linkrate of directly attached device. */
1192 if (!device
->parent
)
1193 device
->linkrate
= phy
->sas_phy
.linkrate
;
1195 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
1201 static void hisi_sas_rescan_topology(struct hisi_hba
*hisi_hba
, u32 old_state
,
1204 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1205 struct asd_sas_port
*_sas_port
= NULL
;
1208 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++) {
1209 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1210 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1211 struct asd_sas_port
*sas_port
= sas_phy
->port
;
1212 bool do_port_check
= !!(_sas_port
!= sas_port
);
1214 if (!sas_phy
->phy
->enabled
)
1217 /* Report PHY state change to libsas */
1218 if (state
& BIT(phy_no
)) {
1219 if (do_port_check
&& sas_port
&& sas_port
->port_dev
) {
1220 struct domain_device
*dev
= sas_port
->port_dev
;
1222 _sas_port
= sas_port
;
1224 if (DEV_IS_EXPANDER(dev
->dev_type
))
1225 sas_ha
->notify_port_event(sas_phy
,
1226 PORTE_BROADCAST_RCVD
);
1228 } else if (old_state
& (1 << phy_no
))
1229 /* PHY down but was up before */
1230 hisi_sas_phy_down(hisi_hba
, phy_no
, 0);
1235 static void hisi_sas_reset_init_all_devices(struct hisi_hba
*hisi_hba
)
1237 struct hisi_sas_device
*sas_dev
;
1238 struct domain_device
*device
;
1241 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1242 sas_dev
= &hisi_hba
->devices
[i
];
1243 device
= sas_dev
->sas_device
;
1245 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
) || !device
)
1248 hisi_sas_init_device(device
);
1252 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba
*hisi_hba
,
1253 struct asd_sas_port
*sas_port
,
1254 struct domain_device
*device
)
1256 struct hisi_sas_tmf_task tmf_task
= { .force_phy
= 1 };
1257 struct ata_port
*ap
= device
->sata_dev
.ap
;
1258 struct device
*dev
= hisi_hba
->dev
;
1259 int s
= sizeof(struct host_to_dev_fis
);
1260 int rc
= TMF_RESP_FUNC_FAILED
;
1261 struct asd_sas_phy
*sas_phy
;
1262 struct ata_link
*link
;
1266 state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1267 list_for_each_entry(sas_phy
, &sas_port
->phy_list
, port_phy_el
) {
1268 if (!(state
& BIT(sas_phy
->id
)))
1271 ata_for_each_link(link
, ap
, EDGE
) {
1272 int pmp
= sata_srst_pmp(link
);
1274 tmf_task
.phy_id
= sas_phy
->id
;
1275 hisi_sas_fill_ata_reset_cmd(link
->device
, 1, pmp
, fis
);
1276 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
, s
,
1278 if (rc
!= TMF_RESP_FUNC_COMPLETE
) {
1279 dev_err(dev
, "phy%d ata reset failed rc=%d\n",
1287 static void hisi_sas_terminate_stp_reject(struct hisi_hba
*hisi_hba
)
1289 struct device
*dev
= hisi_hba
->dev
;
1292 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1293 struct hisi_sas_device
*sas_dev
= &hisi_hba
->devices
[i
];
1294 struct domain_device
*device
= sas_dev
->sas_device
;
1296 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
) || !device
)
1299 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1300 HISI_SAS_INT_ABT_DEV
, 0);
1302 dev_err(dev
, "STP reject: abort dev failed %d\n", rc
);
1305 for (port_no
= 0; port_no
< hisi_hba
->n_phy
; port_no
++) {
1306 struct hisi_sas_port
*port
= &hisi_hba
->port
[port_no
];
1307 struct asd_sas_port
*sas_port
= &port
->sas_port
;
1308 struct domain_device
*port_dev
= sas_port
->port_dev
;
1309 struct domain_device
*device
;
1311 if (!port_dev
|| !DEV_IS_EXPANDER(port_dev
->dev_type
))
1314 /* Try to find a SATA device */
1315 list_for_each_entry(device
, &sas_port
->dev_list
,
1317 if (dev_is_sata(device
)) {
1318 hisi_sas_send_ata_reset_each_phy(hisi_hba
,
1327 static int hisi_sas_controller_reset(struct hisi_hba
*hisi_hba
)
1329 struct device
*dev
= hisi_hba
->dev
;
1330 struct Scsi_Host
*shost
= hisi_hba
->shost
;
1331 u32 old_state
, state
;
1334 if (!hisi_hba
->hw
->soft_reset
)
1337 if (test_and_set_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
))
1340 dev_info(dev
, "controller resetting...\n");
1341 old_state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1343 scsi_block_requests(shost
);
1344 hisi_hba
->hw
->wait_cmds_complete_timeout(hisi_hba
, 100, 5000);
1346 if (timer_pending(&hisi_hba
->timer
))
1347 del_timer_sync(&hisi_hba
->timer
);
1349 set_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1350 rc
= hisi_hba
->hw
->soft_reset(hisi_hba
);
1352 dev_warn(dev
, "controller reset failed (%d)\n", rc
);
1353 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1354 scsi_unblock_requests(shost
);
1358 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1360 /* Init and wait for PHYs to come up and all libsas event finished. */
1361 hisi_hba
->hw
->phys_init(hisi_hba
);
1363 hisi_sas_refresh_port_id(hisi_hba
);
1365 if (hisi_hba
->reject_stp_links_msk
)
1366 hisi_sas_terminate_stp_reject(hisi_hba
);
1367 hisi_sas_reset_init_all_devices(hisi_hba
);
1368 scsi_unblock_requests(shost
);
1370 state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1371 hisi_sas_rescan_topology(hisi_hba
, old_state
, state
);
1372 dev_info(dev
, "controller reset complete\n");
1375 clear_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
);
1380 static int hisi_sas_abort_task(struct sas_task
*task
)
1382 struct scsi_lun lun
;
1383 struct hisi_sas_tmf_task tmf_task
;
1384 struct domain_device
*device
= task
->dev
;
1385 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1386 struct hisi_hba
*hisi_hba
;
1388 int rc
= TMF_RESP_FUNC_FAILED
;
1389 unsigned long flags
;
1392 return TMF_RESP_FUNC_FAILED
;
1394 hisi_hba
= dev_to_hisi_hba(task
->dev
);
1395 dev
= hisi_hba
->dev
;
1397 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1398 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
1399 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1400 rc
= TMF_RESP_FUNC_COMPLETE
;
1403 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
1404 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1406 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1407 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1408 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1409 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1410 u32 tag
= slot
->idx
;
1413 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1414 tmf_task
.tmf
= TMF_ABORT_TASK
;
1415 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1417 rc
= hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
,
1420 rc2
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1421 HISI_SAS_INT_ABT_CMD
, tag
);
1423 dev_err(dev
, "abort task: internal abort (%d)\n", rc2
);
1424 return TMF_RESP_FUNC_FAILED
;
1428 * If the TMF finds that the IO is not in the device and also
1429 * the internal abort does not succeed, then it is safe to
1431 * Note: if the internal abort succeeds then the slot
1432 * will have already been completed
1434 if (rc
== TMF_RESP_FUNC_COMPLETE
&& rc2
!= TMF_RESP_FUNC_SUCC
) {
1435 if (task
->lldd_task
)
1436 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1438 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
1439 task
->task_proto
& SAS_PROTOCOL_STP
) {
1440 if (task
->dev
->dev_type
== SAS_SATA_DEV
) {
1441 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1442 HISI_SAS_INT_ABT_DEV
, 0);
1444 dev_err(dev
, "abort task: internal abort failed\n");
1447 hisi_sas_dereg_device(hisi_hba
, device
);
1448 rc
= hisi_sas_softreset_ata_disk(device
);
1450 } else if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SMP
) {
1452 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1453 u32 tag
= slot
->idx
;
1455 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1456 HISI_SAS_INT_ABT_CMD
, tag
);
1457 if (((rc
< 0) || (rc
== TMF_RESP_FUNC_FAILED
)) &&
1459 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1463 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1464 dev_notice(dev
, "abort task: rc=%d\n", rc
);
1468 static int hisi_sas_abort_task_set(struct domain_device
*device
, u8
*lun
)
1470 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1471 struct device
*dev
= hisi_hba
->dev
;
1472 struct hisi_sas_tmf_task tmf_task
;
1473 int rc
= TMF_RESP_FUNC_FAILED
;
1475 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1476 HISI_SAS_INT_ABT_DEV
, 0);
1478 dev_err(dev
, "abort task set: internal abort rc=%d\n", rc
);
1479 return TMF_RESP_FUNC_FAILED
;
1481 hisi_sas_dereg_device(hisi_hba
, device
);
1483 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
1484 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1486 if (rc
== TMF_RESP_FUNC_COMPLETE
)
1487 hisi_sas_release_task(hisi_hba
, device
);
1492 static int hisi_sas_clear_aca(struct domain_device
*device
, u8
*lun
)
1494 int rc
= TMF_RESP_FUNC_FAILED
;
1495 struct hisi_sas_tmf_task tmf_task
;
1497 tmf_task
.tmf
= TMF_CLEAR_ACA
;
1498 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1503 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device
*device
)
1505 struct sas_phy
*phy
= sas_get_local_phy(device
);
1506 int rc
, reset_type
= (device
->dev_type
== SAS_SATA_DEV
||
1507 (device
->tproto
& SAS_PROTOCOL_STP
)) ? 0 : 1;
1508 rc
= sas_phy_reset(phy
, reset_type
);
1509 sas_put_local_phy(phy
);
1514 static int hisi_sas_I_T_nexus_reset(struct domain_device
*device
)
1516 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1517 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1518 struct device
*dev
= hisi_hba
->dev
;
1519 int rc
= TMF_RESP_FUNC_FAILED
;
1521 if (sas_dev
->dev_status
!= HISI_SAS_DEV_EH
)
1522 return TMF_RESP_FUNC_FAILED
;
1523 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
1525 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1526 HISI_SAS_INT_ABT_DEV
, 0);
1528 dev_err(dev
, "I_T nexus reset: internal abort (%d)\n", rc
);
1529 return TMF_RESP_FUNC_FAILED
;
1531 hisi_sas_dereg_device(hisi_hba
, device
);
1533 rc
= hisi_sas_debug_I_T_nexus_reset(device
);
1535 if ((rc
== TMF_RESP_FUNC_COMPLETE
) || (rc
== -ENODEV
))
1536 hisi_sas_release_task(hisi_hba
, device
);
1541 static int hisi_sas_lu_reset(struct domain_device
*device
, u8
*lun
)
1543 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1544 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1545 struct device
*dev
= hisi_hba
->dev
;
1546 int rc
= TMF_RESP_FUNC_FAILED
;
1548 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1549 if (dev_is_sata(device
)) {
1550 struct sas_phy
*phy
;
1552 /* Clear internal IO and then hardreset */
1553 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1554 HISI_SAS_INT_ABT_DEV
, 0);
1556 dev_err(dev
, "lu_reset: internal abort failed\n");
1559 hisi_sas_dereg_device(hisi_hba
, device
);
1561 phy
= sas_get_local_phy(device
);
1563 rc
= sas_phy_reset(phy
, 1);
1566 hisi_sas_release_task(hisi_hba
, device
);
1567 sas_put_local_phy(phy
);
1569 struct hisi_sas_tmf_task tmf_task
= { .tmf
= TMF_LU_RESET
};
1571 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1572 HISI_SAS_INT_ABT_DEV
, 0);
1574 dev_err(dev
, "lu_reset: internal abort failed\n");
1577 hisi_sas_dereg_device(hisi_hba
, device
);
1579 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1580 if (rc
== TMF_RESP_FUNC_COMPLETE
)
1581 hisi_sas_release_task(hisi_hba
, device
);
1584 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1585 dev_err(dev
, "lu_reset: for device[%d]:rc= %d\n",
1586 sas_dev
->device_id
, rc
);
1590 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct
*sas_ha
)
1592 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
1593 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r
);
1595 queue_work(hisi_hba
->wq
, &r
.work
);
1596 wait_for_completion(r
.completion
);
1598 return TMF_RESP_FUNC_COMPLETE
;
1600 return TMF_RESP_FUNC_FAILED
;
1603 static int hisi_sas_query_task(struct sas_task
*task
)
1605 struct scsi_lun lun
;
1606 struct hisi_sas_tmf_task tmf_task
;
1607 int rc
= TMF_RESP_FUNC_FAILED
;
1609 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1610 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1611 struct domain_device
*device
= task
->dev
;
1612 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1613 u32 tag
= slot
->idx
;
1615 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1616 tmf_task
.tmf
= TMF_QUERY_TASK
;
1617 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1619 rc
= hisi_sas_debug_issue_ssp_tmf(device
,
1623 /* The task is still in Lun, release it then */
1624 case TMF_RESP_FUNC_SUCC
:
1625 /* The task is not in Lun or failed, reset the phy */
1626 case TMF_RESP_FUNC_FAILED
:
1627 case TMF_RESP_FUNC_COMPLETE
:
1630 rc
= TMF_RESP_FUNC_FAILED
;
1638 hisi_sas_internal_abort_task_exec(struct hisi_hba
*hisi_hba
, int device_id
,
1639 struct sas_task
*task
, int abort_flag
,
1642 struct domain_device
*device
= task
->dev
;
1643 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1644 struct device
*dev
= hisi_hba
->dev
;
1645 struct hisi_sas_port
*port
;
1646 struct hisi_sas_slot
*slot
;
1647 struct asd_sas_port
*sas_port
= device
->port
;
1648 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
1649 struct hisi_sas_dq
*dq
= sas_dev
->dq
;
1650 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
1651 unsigned long flags
, flags_dq
= 0;
1654 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
)))
1660 port
= to_hisi_sas_port(sas_port
);
1662 /* simply get a slot and send abort command */
1663 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1664 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
1666 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1669 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1671 slot
= &hisi_hba
->slot_info
[slot_idx
];
1672 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
1674 slot
->buf
= dma_pool_alloc(hisi_hba
->buffer_pool
,
1675 GFP_ATOMIC
, &slot
->buf_dma
);
1681 spin_lock_irqsave(&dq
->lock
, flags_dq
);
1682 wr_q_index
= hisi_hba
->hw
->get_free_slot(hisi_hba
, dq
);
1683 if (wr_q_index
< 0) {
1684 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
1687 list_add_tail(&slot
->delivery
, &dq
->list
);
1688 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
1690 dlvry_queue
= dq
->id
;
1691 dlvry_queue_slot
= wr_q_index
;
1693 slot
->idx
= slot_idx
;
1694 slot
->n_elem
= n_elem
;
1695 slot
->dlvry_queue
= dlvry_queue
;
1696 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
1697 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
1698 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
1701 slot
->is_internal
= true;
1702 task
->lldd_task
= slot
;
1704 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
1705 memset(hisi_sas_cmd_hdr_addr_mem(slot
), 0, HISI_SAS_COMMAND_TABLE_SZ
);
1706 memset(hisi_sas_status_buf_addr_mem(slot
), 0, HISI_SAS_STATUS_BUF_SZ
);
1708 hisi_sas_task_prep_abort(hisi_hba
, slot
, device_id
,
1709 abort_flag
, task_tag
);
1711 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1712 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
1713 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1716 /* send abort command to the chip */
1717 spin_lock_irqsave(&dq
->lock
, flags
);
1718 list_add_tail(&slot
->entry
, &sas_dev
->list
);
1719 hisi_hba
->hw
->start_delivery(dq
);
1720 spin_unlock_irqrestore(&dq
->lock
, flags
);
1725 dma_pool_free(hisi_hba
->buffer_pool
, slot
->buf
,
1728 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1729 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
1730 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1732 dev_err(dev
, "internal abort task prep: failed[%d]!\n", rc
);
1738 * hisi_sas_internal_task_abort -- execute an internal
1739 * abort command for single IO command or a device
1740 * @hisi_hba: host controller struct
1741 * @device: domain device
1742 * @abort_flag: mode of operation, device or single IO
1743 * @tag: tag of IO to be aborted (only relevant to single
1747 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
1748 struct domain_device
*device
,
1749 int abort_flag
, int tag
)
1751 struct sas_task
*task
;
1752 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1753 struct device
*dev
= hisi_hba
->dev
;
1757 * The interface is not realized means this HW don't support internal
1758 * abort, or don't need to do internal abort. Then here, we return
1759 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1760 * the internal abort has been executed and returned CQ.
1762 if (!hisi_hba
->hw
->prep_abort
)
1763 return TMF_RESP_FUNC_FAILED
;
1765 task
= sas_alloc_slow_task(GFP_KERNEL
);
1770 task
->task_proto
= device
->tproto
;
1771 task
->task_done
= hisi_sas_task_done
;
1772 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
1773 task
->slow_task
->timer
.expires
= jiffies
+ INTERNAL_ABORT_TIMEOUT
*HZ
;
1774 add_timer(&task
->slow_task
->timer
);
1776 res
= hisi_sas_internal_abort_task_exec(hisi_hba
, sas_dev
->device_id
,
1777 task
, abort_flag
, tag
);
1779 del_timer(&task
->slow_task
->timer
);
1780 dev_err(dev
, "internal task abort: executing internal task failed: %d\n",
1784 wait_for_completion(&task
->slow_task
->completion
);
1785 res
= TMF_RESP_FUNC_FAILED
;
1787 /* Internal abort timed out */
1788 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
1789 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
1790 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1794 dev_err(dev
, "internal task abort: timeout and not done.\n");
1798 dev_err(dev
, "internal task abort: timeout.\n");
1801 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1802 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
1803 res
= TMF_RESP_FUNC_COMPLETE
;
1807 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1808 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
1809 res
= TMF_RESP_FUNC_SUCC
;
1814 dev_dbg(dev
, "internal task abort: task to dev %016llx task=%p "
1815 "resp: 0x%x sts 0x%x\n",
1816 SAS_ADDR(device
->sas_addr
),
1818 task
->task_status
.resp
, /* 0 is complete, -1 is undelivered */
1819 task
->task_status
.stat
);
1820 sas_free_task(task
);
1825 static void hisi_sas_port_formed(struct asd_sas_phy
*sas_phy
)
1827 hisi_sas_port_notify_formed(sas_phy
);
1830 static void hisi_sas_port_deformed(struct asd_sas_phy
*sas_phy
)
1834 static int hisi_sas_write_gpio(struct sas_ha_struct
*sha
, u8 reg_type
,
1835 u8 reg_index
, u8 reg_count
, u8
*write_data
)
1837 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
1839 if (!hisi_hba
->hw
->write_gpio
)
1842 return hisi_hba
->hw
->write_gpio(hisi_hba
, reg_type
,
1843 reg_index
, reg_count
, write_data
);
1846 static void hisi_sas_phy_disconnected(struct hisi_sas_phy
*phy
)
1848 phy
->phy_attached
= 0;
1853 void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
)
1855 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1856 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1857 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1860 /* Phy down but ready */
1861 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
1862 hisi_sas_port_notify_formed(sas_phy
);
1864 struct hisi_sas_port
*port
= phy
->port
;
1866 /* Phy down and not ready */
1867 sas_ha
->notify_phy_event(sas_phy
, PHYE_LOSS_OF_SIGNAL
);
1868 sas_phy_disconnected(sas_phy
);
1871 if (phy
->phy_type
& PORT_TYPE_SAS
) {
1872 int port_id
= port
->id
;
1874 if (!hisi_hba
->hw
->get_wideport_bitmap(hisi_hba
,
1876 port
->port_attached
= 0;
1877 } else if (phy
->phy_type
& PORT_TYPE_SATA
)
1878 port
->port_attached
= 0;
1880 hisi_sas_phy_disconnected(phy
);
1883 EXPORT_SYMBOL_GPL(hisi_sas_phy_down
);
1885 void hisi_sas_kill_tasklets(struct hisi_hba
*hisi_hba
)
1889 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1890 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1892 tasklet_kill(&cq
->tasklet
);
1895 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets
);
1897 struct scsi_transport_template
*hisi_sas_stt
;
1898 EXPORT_SYMBOL_GPL(hisi_sas_stt
);
1900 struct device_attribute
*host_attrs
[] = {
1901 &dev_attr_phy_event_threshold
,
1904 EXPORT_SYMBOL_GPL(host_attrs
);
1906 static struct sas_domain_function_template hisi_sas_transport_ops
= {
1907 .lldd_dev_found
= hisi_sas_dev_found
,
1908 .lldd_dev_gone
= hisi_sas_dev_gone
,
1909 .lldd_execute_task
= hisi_sas_queue_command
,
1910 .lldd_control_phy
= hisi_sas_control_phy
,
1911 .lldd_abort_task
= hisi_sas_abort_task
,
1912 .lldd_abort_task_set
= hisi_sas_abort_task_set
,
1913 .lldd_clear_aca
= hisi_sas_clear_aca
,
1914 .lldd_I_T_nexus_reset
= hisi_sas_I_T_nexus_reset
,
1915 .lldd_lu_reset
= hisi_sas_lu_reset
,
1916 .lldd_query_task
= hisi_sas_query_task
,
1917 .lldd_clear_nexus_ha
= hisi_sas_clear_nexus_ha
,
1918 .lldd_port_formed
= hisi_sas_port_formed
,
1919 .lldd_port_deformed
= hisi_sas_port_deformed
,
1920 .lldd_write_gpio
= hisi_sas_write_gpio
,
1923 void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
)
1925 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1927 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1928 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1929 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1931 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1932 memset(hisi_hba
->cmd_hdr
[i
], 0, s
);
1935 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1936 memset(hisi_hba
->complete_hdr
[i
], 0, s
);
1940 s
= sizeof(struct hisi_sas_initial_fis
) * hisi_hba
->n_phy
;
1941 memset(hisi_hba
->initial_fis
, 0, s
);
1943 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1944 memset(hisi_hba
->iost
, 0, s
);
1946 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1947 memset(hisi_hba
->breakpoint
, 0, s
);
1949 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1950 memset(hisi_hba
->sata_breakpoint
, 0, s
);
1952 EXPORT_SYMBOL_GPL(hisi_sas_init_mem
);
1954 int hisi_sas_alloc(struct hisi_hba
*hisi_hba
, struct Scsi_Host
*shost
)
1956 struct device
*dev
= hisi_hba
->dev
;
1957 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1959 spin_lock_init(&hisi_hba
->lock
);
1960 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1961 hisi_sas_phy_init(hisi_hba
, i
);
1962 hisi_hba
->port
[i
].port_attached
= 0;
1963 hisi_hba
->port
[i
].id
= -1;
1966 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1967 hisi_hba
->devices
[i
].dev_type
= SAS_PHY_UNUSED
;
1968 hisi_hba
->devices
[i
].device_id
= i
;
1969 hisi_hba
->devices
[i
].dev_status
= HISI_SAS_DEV_NORMAL
;
1972 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1973 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1974 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1976 /* Completion queue structure */
1978 cq
->hisi_hba
= hisi_hba
;
1980 /* Delivery queue structure */
1981 spin_lock_init(&dq
->lock
);
1982 INIT_LIST_HEAD(&dq
->list
);
1984 dq
->hisi_hba
= hisi_hba
;
1986 /* Delivery queue */
1987 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1988 hisi_hba
->cmd_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1989 &hisi_hba
->cmd_hdr_dma
[i
], GFP_KERNEL
);
1990 if (!hisi_hba
->cmd_hdr
[i
])
1993 /* Completion queue */
1994 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1995 hisi_hba
->complete_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1996 &hisi_hba
->complete_hdr_dma
[i
], GFP_KERNEL
);
1997 if (!hisi_hba
->complete_hdr
[i
])
2001 s
= sizeof(struct hisi_sas_slot_buf_table
);
2002 hisi_hba
->buffer_pool
= dma_pool_create("dma_buffer", dev
, s
, 16, 0);
2003 if (!hisi_hba
->buffer_pool
)
2006 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
2007 hisi_hba
->itct
= dma_alloc_coherent(dev
, s
, &hisi_hba
->itct_dma
,
2009 if (!hisi_hba
->itct
)
2012 memset(hisi_hba
->itct
, 0, s
);
2014 hisi_hba
->slot_info
= devm_kcalloc(dev
, max_command_entries
,
2015 sizeof(struct hisi_sas_slot
),
2017 if (!hisi_hba
->slot_info
)
2020 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
2021 hisi_hba
->iost
= dma_alloc_coherent(dev
, s
, &hisi_hba
->iost_dma
,
2023 if (!hisi_hba
->iost
)
2026 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
2027 hisi_hba
->breakpoint
= dma_alloc_coherent(dev
, s
,
2028 &hisi_hba
->breakpoint_dma
, GFP_KERNEL
);
2029 if (!hisi_hba
->breakpoint
)
2032 hisi_hba
->slot_index_count
= max_command_entries
;
2033 s
= hisi_hba
->slot_index_count
/ BITS_PER_BYTE
;
2034 hisi_hba
->slot_index_tags
= devm_kzalloc(dev
, s
, GFP_KERNEL
);
2035 if (!hisi_hba
->slot_index_tags
)
2038 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
2039 hisi_hba
->initial_fis
= dma_alloc_coherent(dev
, s
,
2040 &hisi_hba
->initial_fis_dma
, GFP_KERNEL
);
2041 if (!hisi_hba
->initial_fis
)
2044 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
2045 hisi_hba
->sata_breakpoint
= dma_alloc_coherent(dev
, s
,
2046 &hisi_hba
->sata_breakpoint_dma
, GFP_KERNEL
);
2047 if (!hisi_hba
->sata_breakpoint
)
2049 hisi_sas_init_mem(hisi_hba
);
2051 hisi_sas_slot_index_init(hisi_hba
);
2053 hisi_hba
->wq
= create_singlethread_workqueue(dev_name(dev
));
2054 if (!hisi_hba
->wq
) {
2055 dev_err(dev
, "sas_alloc: failed to create workqueue\n");
2063 EXPORT_SYMBOL_GPL(hisi_sas_alloc
);
2065 void hisi_sas_free(struct hisi_hba
*hisi_hba
)
2067 struct device
*dev
= hisi_hba
->dev
;
2068 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
2070 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
2071 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
2072 if (hisi_hba
->cmd_hdr
[i
])
2073 dma_free_coherent(dev
, s
,
2074 hisi_hba
->cmd_hdr
[i
],
2075 hisi_hba
->cmd_hdr_dma
[i
]);
2077 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
2078 if (hisi_hba
->complete_hdr
[i
])
2079 dma_free_coherent(dev
, s
,
2080 hisi_hba
->complete_hdr
[i
],
2081 hisi_hba
->complete_hdr_dma
[i
]);
2084 dma_pool_destroy(hisi_hba
->buffer_pool
);
2086 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
2088 dma_free_coherent(dev
, s
,
2089 hisi_hba
->itct
, hisi_hba
->itct_dma
);
2091 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
2093 dma_free_coherent(dev
, s
,
2094 hisi_hba
->iost
, hisi_hba
->iost_dma
);
2096 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
2097 if (hisi_hba
->breakpoint
)
2098 dma_free_coherent(dev
, s
,
2099 hisi_hba
->breakpoint
,
2100 hisi_hba
->breakpoint_dma
);
2103 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
2104 if (hisi_hba
->initial_fis
)
2105 dma_free_coherent(dev
, s
,
2106 hisi_hba
->initial_fis
,
2107 hisi_hba
->initial_fis_dma
);
2109 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
2110 if (hisi_hba
->sata_breakpoint
)
2111 dma_free_coherent(dev
, s
,
2112 hisi_hba
->sata_breakpoint
,
2113 hisi_hba
->sata_breakpoint_dma
);
2116 destroy_workqueue(hisi_hba
->wq
);
2118 EXPORT_SYMBOL_GPL(hisi_sas_free
);
2120 void hisi_sas_rst_work_handler(struct work_struct
*work
)
2122 struct hisi_hba
*hisi_hba
=
2123 container_of(work
, struct hisi_hba
, rst_work
);
2125 hisi_sas_controller_reset(hisi_hba
);
2127 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler
);
2129 void hisi_sas_sync_rst_work_handler(struct work_struct
*work
)
2131 struct hisi_sas_rst
*rst
=
2132 container_of(work
, struct hisi_sas_rst
, work
);
2134 if (!hisi_sas_controller_reset(rst
->hisi_hba
))
2136 complete(rst
->completion
);
2138 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler
);
2140 int hisi_sas_get_fw_info(struct hisi_hba
*hisi_hba
)
2142 struct device
*dev
= hisi_hba
->dev
;
2143 struct platform_device
*pdev
= hisi_hba
->platform_dev
;
2144 struct device_node
*np
= pdev
? pdev
->dev
.of_node
: NULL
;
2147 if (device_property_read_u8_array(dev
, "sas-addr", hisi_hba
->sas_addr
,
2149 dev_err(dev
, "could not get property sas-addr\n");
2155 * These properties are only required for platform device-based
2156 * controller with DT firmware.
2158 hisi_hba
->ctrl
= syscon_regmap_lookup_by_phandle(np
,
2159 "hisilicon,sas-syscon");
2160 if (IS_ERR(hisi_hba
->ctrl
)) {
2161 dev_err(dev
, "could not get syscon\n");
2165 if (device_property_read_u32(dev
, "ctrl-reset-reg",
2166 &hisi_hba
->ctrl_reset_reg
)) {
2168 "could not get property ctrl-reset-reg\n");
2172 if (device_property_read_u32(dev
, "ctrl-reset-sts-reg",
2173 &hisi_hba
->ctrl_reset_sts_reg
)) {
2175 "could not get property ctrl-reset-sts-reg\n");
2179 if (device_property_read_u32(dev
, "ctrl-clock-ena-reg",
2180 &hisi_hba
->ctrl_clock_ena_reg
)) {
2182 "could not get property ctrl-clock-ena-reg\n");
2187 refclk
= devm_clk_get(dev
, NULL
);
2189 dev_dbg(dev
, "no ref clk property\n");
2191 hisi_hba
->refclk_frequency_mhz
= clk_get_rate(refclk
) / 1000000;
2193 if (device_property_read_u32(dev
, "phy-count", &hisi_hba
->n_phy
)) {
2194 dev_err(dev
, "could not get property phy-count\n");
2198 if (device_property_read_u32(dev
, "queue-count",
2199 &hisi_hba
->queue_count
)) {
2200 dev_err(dev
, "could not get property queue-count\n");
2206 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info
);
2208 static struct Scsi_Host
*hisi_sas_shost_alloc(struct platform_device
*pdev
,
2209 const struct hisi_sas_hw
*hw
)
2211 struct resource
*res
;
2212 struct Scsi_Host
*shost
;
2213 struct hisi_hba
*hisi_hba
;
2214 struct device
*dev
= &pdev
->dev
;
2216 shost
= scsi_host_alloc(hw
->sht
, sizeof(*hisi_hba
));
2218 dev_err(dev
, "scsi host alloc failed\n");
2221 hisi_hba
= shost_priv(shost
);
2223 INIT_WORK(&hisi_hba
->rst_work
, hisi_sas_rst_work_handler
);
2225 hisi_hba
->dev
= dev
;
2226 hisi_hba
->platform_dev
= pdev
;
2227 hisi_hba
->shost
= shost
;
2228 SHOST_TO_SAS_HA(shost
) = &hisi_hba
->sha
;
2230 timer_setup(&hisi_hba
->timer
, NULL
, 0);
2232 if (hisi_sas_get_fw_info(hisi_hba
) < 0)
2235 if (dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64)) &&
2236 dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32))) {
2237 dev_err(dev
, "No usable DMA addressing method\n");
2241 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2242 hisi_hba
->regs
= devm_ioremap_resource(dev
, res
);
2243 if (IS_ERR(hisi_hba
->regs
))
2246 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2248 hisi_hba
->sgpio_regs
= devm_ioremap_resource(dev
, res
);
2249 if (IS_ERR(hisi_hba
->sgpio_regs
))
2253 if (hisi_sas_alloc(hisi_hba
, shost
)) {
2254 hisi_sas_free(hisi_hba
);
2260 scsi_host_put(shost
);
2261 dev_err(dev
, "shost alloc failed\n");
2265 void hisi_sas_init_add(struct hisi_hba
*hisi_hba
)
2269 for (i
= 0; i
< hisi_hba
->n_phy
; i
++)
2270 memcpy(&hisi_hba
->phy
[i
].dev_sas_addr
,
2274 EXPORT_SYMBOL_GPL(hisi_sas_init_add
);
2276 int hisi_sas_probe(struct platform_device
*pdev
,
2277 const struct hisi_sas_hw
*hw
)
2279 struct Scsi_Host
*shost
;
2280 struct hisi_hba
*hisi_hba
;
2281 struct device
*dev
= &pdev
->dev
;
2282 struct asd_sas_phy
**arr_phy
;
2283 struct asd_sas_port
**arr_port
;
2284 struct sas_ha_struct
*sha
;
2285 int rc
, phy_nr
, port_nr
, i
;
2287 shost
= hisi_sas_shost_alloc(pdev
, hw
);
2291 sha
= SHOST_TO_SAS_HA(shost
);
2292 hisi_hba
= shost_priv(shost
);
2293 platform_set_drvdata(pdev
, sha
);
2295 phy_nr
= port_nr
= hisi_hba
->n_phy
;
2297 arr_phy
= devm_kcalloc(dev
, phy_nr
, sizeof(void *), GFP_KERNEL
);
2298 arr_port
= devm_kcalloc(dev
, port_nr
, sizeof(void *), GFP_KERNEL
);
2299 if (!arr_phy
|| !arr_port
) {
2304 sha
->sas_phy
= arr_phy
;
2305 sha
->sas_port
= arr_port
;
2306 sha
->lldd_ha
= hisi_hba
;
2308 shost
->transportt
= hisi_sas_stt
;
2309 shost
->max_id
= HISI_SAS_MAX_DEVICES
;
2310 shost
->max_lun
= ~0;
2311 shost
->max_channel
= 1;
2312 shost
->max_cmd_len
= 16;
2313 shost
->sg_tablesize
= min_t(u16
, SG_ALL
, HISI_SAS_SGE_PAGE_CNT
);
2314 shost
->can_queue
= hisi_hba
->hw
->max_command_entries
;
2315 shost
->cmd_per_lun
= hisi_hba
->hw
->max_command_entries
;
2317 sha
->sas_ha_name
= DRV_NAME
;
2318 sha
->dev
= hisi_hba
->dev
;
2319 sha
->lldd_module
= THIS_MODULE
;
2320 sha
->sas_addr
= &hisi_hba
->sas_addr
[0];
2321 sha
->num_phys
= hisi_hba
->n_phy
;
2322 sha
->core
.shost
= hisi_hba
->shost
;
2324 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
2325 sha
->sas_phy
[i
] = &hisi_hba
->phy
[i
].sas_phy
;
2326 sha
->sas_port
[i
] = &hisi_hba
->port
[i
].sas_port
;
2329 hisi_sas_init_add(hisi_hba
);
2331 rc
= scsi_add_host(shost
, &pdev
->dev
);
2335 rc
= sas_register_ha(sha
);
2337 goto err_out_register_ha
;
2339 rc
= hisi_hba
->hw
->hw_init(hisi_hba
);
2341 goto err_out_register_ha
;
2343 scsi_scan_host(shost
);
2347 err_out_register_ha
:
2348 scsi_remove_host(shost
);
2350 hisi_sas_free(hisi_hba
);
2351 scsi_host_put(shost
);
2354 EXPORT_SYMBOL_GPL(hisi_sas_probe
);
2356 int hisi_sas_remove(struct platform_device
*pdev
)
2358 struct sas_ha_struct
*sha
= platform_get_drvdata(pdev
);
2359 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
2360 struct Scsi_Host
*shost
= sha
->core
.shost
;
2362 if (timer_pending(&hisi_hba
->timer
))
2363 del_timer(&hisi_hba
->timer
);
2365 sas_unregister_ha(sha
);
2366 sas_remove_host(sha
->core
.shost
);
2368 hisi_sas_free(hisi_hba
);
2369 scsi_host_put(shost
);
2372 EXPORT_SYMBOL_GPL(hisi_sas_remove
);
2374 static __init
int hisi_sas_init(void)
2376 hisi_sas_stt
= sas_domain_attach_transport(&hisi_sas_transport_ops
);
2383 static __exit
void hisi_sas_exit(void)
2385 sas_release_transport(hisi_sas_stt
);
2388 module_init(hisi_sas_init
);
2389 module_exit(hisi_sas_exit
);
2391 MODULE_LICENSE("GPL");
2392 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2393 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2394 MODULE_ALIAS("platform:" DRV_NAME
);