2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
13 #define DRV_NAME "hisi_sas"
15 #define DEV_IS_GONE(dev) \
16 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
18 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device
*device
,
19 u8
*lun
, struct hisi_sas_tmf_task
*tmf
);
21 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
22 struct domain_device
*device
,
23 int abort_flag
, int tag
);
24 static int hisi_sas_softreset_ata_disk(struct domain_device
*device
);
25 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
28 u8
hisi_sas_get_ata_protocol(struct host_to_dev_fis
*fis
, int direction
)
30 switch (fis
->command
) {
31 case ATA_CMD_FPDMA_WRITE
:
32 case ATA_CMD_FPDMA_READ
:
33 case ATA_CMD_FPDMA_RECV
:
34 case ATA_CMD_FPDMA_SEND
:
35 case ATA_CMD_NCQ_NON_DATA
:
36 return HISI_SAS_SATA_PROTOCOL_FPDMA
;
38 case ATA_CMD_DOWNLOAD_MICRO
:
40 case ATA_CMD_PMP_READ
:
41 case ATA_CMD_READ_LOG_EXT
:
42 case ATA_CMD_PIO_READ
:
43 case ATA_CMD_PIO_READ_EXT
:
44 case ATA_CMD_PMP_WRITE
:
45 case ATA_CMD_WRITE_LOG_EXT
:
46 case ATA_CMD_PIO_WRITE
:
47 case ATA_CMD_PIO_WRITE_EXT
:
48 return HISI_SAS_SATA_PROTOCOL_PIO
;
51 case ATA_CMD_DOWNLOAD_MICRO_DMA
:
52 case ATA_CMD_PMP_READ_DMA
:
53 case ATA_CMD_PMP_WRITE_DMA
:
55 case ATA_CMD_READ_EXT
:
56 case ATA_CMD_READ_LOG_DMA_EXT
:
57 case ATA_CMD_READ_STREAM_DMA_EXT
:
58 case ATA_CMD_TRUSTED_RCV_DMA
:
59 case ATA_CMD_TRUSTED_SND_DMA
:
61 case ATA_CMD_WRITE_EXT
:
62 case ATA_CMD_WRITE_FUA_EXT
:
63 case ATA_CMD_WRITE_QUEUED
:
64 case ATA_CMD_WRITE_LOG_DMA_EXT
:
65 case ATA_CMD_WRITE_STREAM_DMA_EXT
:
66 case ATA_CMD_ZAC_MGMT_IN
:
67 return HISI_SAS_SATA_PROTOCOL_DMA
;
69 case ATA_CMD_CHK_POWER
:
70 case ATA_CMD_DEV_RESET
:
73 case ATA_CMD_FLUSH_EXT
:
75 case ATA_CMD_VERIFY_EXT
:
76 case ATA_CMD_SET_FEATURES
:
78 case ATA_CMD_STANDBYNOW1
:
79 case ATA_CMD_ZAC_MGMT_OUT
:
80 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
83 if (fis
->command
== ATA_CMD_SET_MAX
) {
84 switch (fis
->features
) {
85 case ATA_SET_MAX_PASSWD
:
86 case ATA_SET_MAX_LOCK
:
87 return HISI_SAS_SATA_PROTOCOL_PIO
;
89 case ATA_SET_MAX_PASSWD_DMA
:
90 case ATA_SET_MAX_UNLOCK_DMA
:
91 return HISI_SAS_SATA_PROTOCOL_DMA
;
94 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
97 if (direction
== DMA_NONE
)
98 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
99 return HISI_SAS_SATA_PROTOCOL_PIO
;
103 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol
);
105 void hisi_sas_sata_done(struct sas_task
*task
,
106 struct hisi_sas_slot
*slot
)
108 struct task_status_struct
*ts
= &task
->task_status
;
109 struct ata_task_resp
*resp
= (struct ata_task_resp
*)ts
->buf
;
110 struct hisi_sas_status_buffer
*status_buf
=
111 hisi_sas_status_buf_addr_mem(slot
);
112 u8
*iu
= &status_buf
->iu
[0];
113 struct dev_to_host_fis
*d2h
= (struct dev_to_host_fis
*)iu
;
115 resp
->frame_len
= sizeof(struct dev_to_host_fis
);
116 memcpy(&resp
->ending_fis
[0], d2h
, sizeof(struct dev_to_host_fis
));
118 ts
->buf_valid_size
= sizeof(*resp
);
120 EXPORT_SYMBOL_GPL(hisi_sas_sata_done
);
122 int hisi_sas_get_ncq_tag(struct sas_task
*task
, u32
*tag
)
124 struct ata_queued_cmd
*qc
= task
->uldd_task
;
127 if (qc
->tf
.command
== ATA_CMD_FPDMA_WRITE
||
128 qc
->tf
.command
== ATA_CMD_FPDMA_READ
) {
135 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag
);
138 * This function assumes linkrate mask fits in 8 bits, which it
139 * does for all HW versions supported.
141 u8
hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max
)
146 max
-= SAS_LINK_RATE_1_5_GBPS
;
147 for (i
= 0; i
<= max
; i
++)
148 rate
|= 1 << (i
* 2);
151 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask
);
153 static struct hisi_hba
*dev_to_hisi_hba(struct domain_device
*device
)
155 return device
->port
->ha
->lldd_ha
;
158 struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
)
160 return container_of(sas_port
, struct hisi_sas_port
, sas_port
);
162 EXPORT_SYMBOL_GPL(to_hisi_sas_port
);
164 void hisi_sas_stop_phys(struct hisi_hba
*hisi_hba
)
168 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++)
169 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
171 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys
);
173 static void hisi_sas_slot_index_clear(struct hisi_hba
*hisi_hba
, int slot_idx
)
175 void *bitmap
= hisi_hba
->slot_index_tags
;
177 clear_bit(slot_idx
, bitmap
);
180 static void hisi_sas_slot_index_free(struct hisi_hba
*hisi_hba
, int slot_idx
)
182 hisi_sas_slot_index_clear(hisi_hba
, slot_idx
);
185 static void hisi_sas_slot_index_set(struct hisi_hba
*hisi_hba
, int slot_idx
)
187 void *bitmap
= hisi_hba
->slot_index_tags
;
189 set_bit(slot_idx
, bitmap
);
192 static int hisi_sas_slot_index_alloc(struct hisi_hba
*hisi_hba
, int *slot_idx
)
195 void *bitmap
= hisi_hba
->slot_index_tags
;
197 index
= find_first_zero_bit(bitmap
, hisi_hba
->slot_index_count
);
198 if (index
>= hisi_hba
->slot_index_count
)
199 return -SAS_QUEUE_FULL
;
200 hisi_sas_slot_index_set(hisi_hba
, index
);
205 static void hisi_sas_slot_index_init(struct hisi_hba
*hisi_hba
)
209 for (i
= 0; i
< hisi_hba
->slot_index_count
; ++i
)
210 hisi_sas_slot_index_clear(hisi_hba
, i
);
213 void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
214 struct hisi_sas_slot
*slot
)
216 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[slot
->dlvry_queue
];
220 struct device
*dev
= hisi_hba
->dev
;
222 if (!task
->lldd_task
)
225 task
->lldd_task
= NULL
;
227 if (!sas_protocol_ata(task
->task_proto
))
229 dma_unmap_sg(dev
, task
->scatter
,
235 dma_pool_free(hisi_hba
->buffer_pool
, slot
->buf
, slot
->buf_dma
);
237 spin_lock_irqsave(&dq
->lock
, flags
);
238 list_del_init(&slot
->entry
);
239 spin_unlock_irqrestore(&dq
->lock
, flags
);
243 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
244 hisi_sas_slot_index_free(hisi_hba
, slot
->idx
);
245 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
247 /* slot memory is fully zeroed when it is reused */
249 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free
);
251 static void hisi_sas_task_prep_smp(struct hisi_hba
*hisi_hba
,
252 struct hisi_sas_slot
*slot
)
254 hisi_hba
->hw
->prep_smp(hisi_hba
, slot
);
257 static void hisi_sas_task_prep_ssp(struct hisi_hba
*hisi_hba
,
258 struct hisi_sas_slot
*slot
, int is_tmf
,
259 struct hisi_sas_tmf_task
*tmf
)
261 hisi_hba
->hw
->prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
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
,
272 int device_id
, int abort_flag
, int tag_to_abort
)
274 hisi_hba
->hw
->prep_abort(hisi_hba
, slot
,
275 device_id
, abort_flag
, tag_to_abort
);
279 * This function will issue an abort TMF regardless of whether the
280 * task is in the sdev or not. Then it will do the task complete
281 * cleanup and callbacks.
283 static void hisi_sas_slot_abort(struct work_struct
*work
)
285 struct hisi_sas_slot
*abort_slot
=
286 container_of(work
, struct hisi_sas_slot
, abort_slot
);
287 struct sas_task
*task
= abort_slot
->task
;
288 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
289 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
290 struct hisi_sas_tmf_task tmf_task
;
292 struct device
*dev
= hisi_hba
->dev
;
293 int tag
= abort_slot
->idx
;
295 if (!(task
->task_proto
& SAS_PROTOCOL_SSP
)) {
296 dev_err(dev
, "cannot abort slot for non-ssp task\n");
300 int_to_scsilun(cmnd
->device
->lun
, &lun
);
301 tmf_task
.tmf
= TMF_ABORT_TASK
;
302 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
304 hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
, &tmf_task
);
306 /* Do cleanup for this task */
307 hisi_sas_slot_task_free(hisi_hba
, task
, abort_slot
);
309 task
->task_done(task
);
312 static int hisi_sas_task_prep(struct sas_task
*task
,
313 struct hisi_sas_dq
**dq_pointer
,
314 int is_tmf
, struct hisi_sas_tmf_task
*tmf
,
317 struct domain_device
*device
= task
->dev
;
318 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
319 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
320 struct hisi_sas_port
*port
;
321 struct hisi_sas_slot
*slot
;
322 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
323 struct asd_sas_port
*sas_port
= device
->port
;
324 struct device
*dev
= hisi_hba
->dev
;
325 int dlvry_queue_slot
, dlvry_queue
, rc
, slot_idx
;
326 int n_elem
= 0, n_elem_req
= 0, n_elem_resp
= 0;
327 unsigned long flags
, flags_dq
;
328 struct hisi_sas_dq
*dq
;
332 struct task_status_struct
*ts
= &task
->task_status
;
334 ts
->resp
= SAS_TASK_UNDELIVERED
;
335 ts
->stat
= SAS_PHY_DOWN
;
337 * libsas will use dev->port, should
338 * not call task_done for sata
340 if (device
->dev_type
!= SAS_SATA_DEV
)
341 task
->task_done(task
);
345 if (DEV_IS_GONE(sas_dev
)) {
347 dev_info(dev
, "task prep: device %d not ready\n",
350 dev_info(dev
, "task prep: device %016llx not ready\n",
351 SAS_ADDR(device
->sas_addr
));
356 *dq_pointer
= dq
= sas_dev
->dq
;
358 port
= to_hisi_sas_port(sas_port
);
359 if (port
&& !port
->port_attached
) {
360 dev_info(dev
, "task prep: %s port%d not attach device\n",
361 (dev_is_sata(device
)) ?
368 if (!sas_protocol_ata(task
->task_proto
)) {
369 unsigned int req_len
, resp_len
;
371 if (task
->num_scatter
) {
372 n_elem
= dma_map_sg(dev
, task
->scatter
,
373 task
->num_scatter
, task
->data_dir
);
378 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
379 n_elem_req
= dma_map_sg(dev
, &task
->smp_task
.smp_req
,
385 req_len
= sg_dma_len(&task
->smp_task
.smp_req
);
388 goto err_out_dma_unmap
;
390 n_elem_resp
= dma_map_sg(dev
, &task
->smp_task
.smp_resp
,
394 goto err_out_dma_unmap
;
396 resp_len
= sg_dma_len(&task
->smp_task
.smp_resp
);
397 if (resp_len
& 0x3) {
399 goto err_out_dma_unmap
;
403 n_elem
= task
->num_scatter
;
405 if (n_elem
> HISI_SAS_SGE_PAGE_CNT
) {
406 dev_err(dev
, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
409 goto err_out_dma_unmap
;
412 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
413 if (hisi_hba
->hw
->slot_index_alloc
)
414 rc
= hisi_hba
->hw
->slot_index_alloc(hisi_hba
, &slot_idx
,
417 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
418 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
420 goto err_out_dma_unmap
;
422 slot
= &hisi_hba
->slot_info
[slot_idx
];
423 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
425 slot
->buf
= dma_pool_alloc(hisi_hba
->buffer_pool
,
426 GFP_ATOMIC
, &slot
->buf_dma
);
432 spin_lock_irqsave(&dq
->lock
, flags_dq
);
433 wr_q_index
= hisi_hba
->hw
->get_free_slot(hisi_hba
, dq
);
434 if (wr_q_index
< 0) {
435 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
439 list_add_tail(&slot
->delivery
, &dq
->list
);
440 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
442 dlvry_queue
= dq
->id
;
443 dlvry_queue_slot
= wr_q_index
;
445 slot
->idx
= slot_idx
;
446 slot
->n_elem
= n_elem
;
447 slot
->dlvry_queue
= dlvry_queue
;
448 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
449 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
450 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
454 slot
->is_internal
= true;
455 task
->lldd_task
= slot
;
456 INIT_WORK(&slot
->abort_slot
, hisi_sas_slot_abort
);
458 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
459 memset(hisi_sas_cmd_hdr_addr_mem(slot
), 0, HISI_SAS_COMMAND_TABLE_SZ
);
460 memset(hisi_sas_status_buf_addr_mem(slot
), 0, HISI_SAS_STATUS_BUF_SZ
);
462 switch (task
->task_proto
) {
463 case SAS_PROTOCOL_SMP
:
464 hisi_sas_task_prep_smp(hisi_hba
, slot
);
466 case SAS_PROTOCOL_SSP
:
467 hisi_sas_task_prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
469 case SAS_PROTOCOL_SATA
:
470 case SAS_PROTOCOL_STP
:
471 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:
472 hisi_sas_task_prep_ata(hisi_hba
, slot
);
475 dev_err(dev
, "task prep: unknown/unsupported proto (0x%x)\n",
480 spin_lock_irqsave(&dq
->lock
, flags
);
481 list_add_tail(&slot
->entry
, &sas_dev
->list
);
482 spin_unlock_irqrestore(&dq
->lock
, flags
);
483 spin_lock_irqsave(&task
->task_state_lock
, flags
);
484 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
485 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
493 dma_pool_free(hisi_hba
->buffer_pool
, slot
->buf
,
496 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
497 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
498 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
500 if (!sas_protocol_ata(task
->task_proto
)) {
501 if (task
->num_scatter
) {
502 dma_unmap_sg(dev
, task
->scatter
, task
->num_scatter
,
504 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
506 dma_unmap_sg(dev
, &task
->smp_task
.smp_req
,
509 dma_unmap_sg(dev
, &task
->smp_task
.smp_resp
,
514 dev_err(dev
, "task prep: failed[%d]!\n", rc
);
518 static int hisi_sas_task_exec(struct sas_task
*task
, gfp_t gfp_flags
,
519 int is_tmf
, struct hisi_sas_tmf_task
*tmf
)
524 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
525 struct device
*dev
= hisi_hba
->dev
;
526 struct hisi_sas_dq
*dq
= NULL
;
528 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
)))
531 /* protect task_prep and start_delivery sequence */
532 rc
= hisi_sas_task_prep(task
, &dq
, is_tmf
, tmf
, &pass
);
534 dev_err(dev
, "task exec: failed[%d]!\n", rc
);
537 spin_lock_irqsave(&dq
->lock
, flags
);
538 hisi_hba
->hw
->start_delivery(dq
);
539 spin_unlock_irqrestore(&dq
->lock
, flags
);
545 static void hisi_sas_bytes_dmaed(struct hisi_hba
*hisi_hba
, int phy_no
)
547 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
548 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
549 struct sas_ha_struct
*sas_ha
;
551 if (!phy
->phy_attached
)
554 sas_ha
= &hisi_hba
->sha
;
555 sas_ha
->notify_phy_event(sas_phy
, PHYE_OOB_DONE
);
558 struct sas_phy
*sphy
= sas_phy
->phy
;
560 sphy
->negotiated_linkrate
= sas_phy
->linkrate
;
561 sphy
->minimum_linkrate_hw
= SAS_LINK_RATE_1_5_GBPS
;
562 sphy
->maximum_linkrate_hw
=
563 hisi_hba
->hw
->phy_get_max_linkrate();
564 if (sphy
->minimum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
565 sphy
->minimum_linkrate
= phy
->minimum_linkrate
;
567 if (sphy
->maximum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
568 sphy
->maximum_linkrate
= phy
->maximum_linkrate
;
571 if (phy
->phy_type
& PORT_TYPE_SAS
) {
572 struct sas_identify_frame
*id
;
574 id
= (struct sas_identify_frame
*)phy
->frame_rcvd
;
575 id
->dev_type
= phy
->identify
.device_type
;
576 id
->initiator_bits
= SAS_PROTOCOL_ALL
;
577 id
->target_bits
= phy
->identify
.target_port_protocols
;
578 } else if (phy
->phy_type
& PORT_TYPE_SATA
) {
582 sas_phy
->frame_rcvd_size
= phy
->frame_rcvd_size
;
583 sas_ha
->notify_port_event(sas_phy
, PORTE_BYTES_DMAED
);
586 static struct hisi_sas_device
*hisi_sas_alloc_dev(struct domain_device
*device
)
588 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
589 struct hisi_sas_device
*sas_dev
= NULL
;
593 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
594 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
595 if (hisi_hba
->devices
[i
].dev_type
== SAS_PHY_UNUSED
) {
596 int queue
= i
% hisi_hba
->queue_count
;
597 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[queue
];
599 hisi_hba
->devices
[i
].device_id
= i
;
600 sas_dev
= &hisi_hba
->devices
[i
];
601 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
602 sas_dev
->dev_type
= device
->dev_type
;
603 sas_dev
->hisi_hba
= hisi_hba
;
604 sas_dev
->sas_device
= device
;
606 INIT_LIST_HEAD(&hisi_hba
->devices
[i
].list
);
610 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
615 static int hisi_sas_dev_found(struct domain_device
*device
)
617 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
618 struct domain_device
*parent_dev
= device
->parent
;
619 struct hisi_sas_device
*sas_dev
;
620 struct device
*dev
= hisi_hba
->dev
;
622 if (hisi_hba
->hw
->alloc_dev
)
623 sas_dev
= hisi_hba
->hw
->alloc_dev(device
);
625 sas_dev
= hisi_sas_alloc_dev(device
);
627 dev_err(dev
, "fail alloc dev: max support %d devices\n",
628 HISI_SAS_MAX_DEVICES
);
632 device
->lldd_dev
= sas_dev
;
633 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
635 if (parent_dev
&& DEV_IS_EXPANDER(parent_dev
->dev_type
)) {
637 u8 phy_num
= parent_dev
->ex_dev
.num_phys
;
640 for (phy_no
= 0; phy_no
< phy_num
; phy_no
++) {
641 phy
= &parent_dev
->ex_dev
.ex_phy
[phy_no
];
642 if (SAS_ADDR(phy
->attached_sas_addr
) ==
643 SAS_ADDR(device
->sas_addr
)) {
644 sas_dev
->attached_phy
= phy_no
;
649 if (phy_no
== phy_num
) {
650 dev_info(dev
, "dev found: no attached "
651 "dev:%016llx at ex:%016llx\n",
652 SAS_ADDR(device
->sas_addr
),
653 SAS_ADDR(parent_dev
->sas_addr
));
658 dev_info(dev
, "dev[%d:%x] found\n",
659 sas_dev
->device_id
, sas_dev
->dev_type
);
664 static int hisi_sas_slave_configure(struct scsi_device
*sdev
)
666 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
667 int ret
= sas_slave_configure(sdev
);
671 if (!dev_is_sata(dev
))
672 sas_change_queue_depth(sdev
, 64);
677 static void hisi_sas_scan_start(struct Scsi_Host
*shost
)
679 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
681 hisi_hba
->hw
->phys_init(hisi_hba
);
684 static int hisi_sas_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
686 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
687 struct sas_ha_struct
*sha
= &hisi_hba
->sha
;
689 /* Wait for PHY up interrupt to occur */
697 static void hisi_sas_phyup_work(struct work_struct
*work
)
699 struct hisi_sas_phy
*phy
=
700 container_of(work
, typeof(*phy
), works
[HISI_PHYE_PHY_UP
]);
701 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
702 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
703 int phy_no
= sas_phy
->id
;
705 hisi_hba
->hw
->sl_notify(hisi_hba
, phy_no
); /* This requires a sleep */
706 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
709 static void hisi_sas_linkreset_work(struct work_struct
*work
)
711 struct hisi_sas_phy
*phy
=
712 container_of(work
, typeof(*phy
), works
[HISI_PHYE_LINK_RESET
]);
713 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
715 hisi_sas_control_phy(sas_phy
, PHY_FUNC_LINK_RESET
, NULL
);
718 static const work_func_t hisi_sas_phye_fns
[HISI_PHYES_NUM
] = {
719 [HISI_PHYE_PHY_UP
] = hisi_sas_phyup_work
,
720 [HISI_PHYE_LINK_RESET
] = hisi_sas_linkreset_work
,
723 bool hisi_sas_notify_phy_event(struct hisi_sas_phy
*phy
,
724 enum hisi_sas_phy_event event
)
726 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
728 if (WARN_ON(event
>= HISI_PHYES_NUM
))
731 return queue_work(hisi_hba
->wq
, &phy
->works
[event
]);
733 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event
);
735 static void hisi_sas_phy_init(struct hisi_hba
*hisi_hba
, int phy_no
)
737 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
738 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
741 phy
->hisi_hba
= hisi_hba
;
743 phy
->minimum_linkrate
= SAS_LINK_RATE_1_5_GBPS
;
744 phy
->maximum_linkrate
= hisi_hba
->hw
->phy_get_max_linkrate();
745 sas_phy
->enabled
= (phy_no
< hisi_hba
->n_phy
) ? 1 : 0;
746 sas_phy
->class = SAS
;
747 sas_phy
->iproto
= SAS_PROTOCOL_ALL
;
749 sas_phy
->type
= PHY_TYPE_PHYSICAL
;
750 sas_phy
->role
= PHY_ROLE_INITIATOR
;
751 sas_phy
->oob_mode
= OOB_NOT_CONNECTED
;
752 sas_phy
->linkrate
= SAS_LINK_RATE_UNKNOWN
;
753 sas_phy
->id
= phy_no
;
754 sas_phy
->sas_addr
= &hisi_hba
->sas_addr
[0];
755 sas_phy
->frame_rcvd
= &phy
->frame_rcvd
[0];
756 sas_phy
->ha
= (struct sas_ha_struct
*)hisi_hba
->shost
->hostdata
;
757 sas_phy
->lldd_phy
= phy
;
759 for (i
= 0; i
< HISI_PHYES_NUM
; i
++)
760 INIT_WORK(&phy
->works
[i
], hisi_sas_phye_fns
[i
]);
763 static void hisi_sas_port_notify_formed(struct asd_sas_phy
*sas_phy
)
765 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
766 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
767 struct hisi_sas_phy
*phy
= sas_phy
->lldd_phy
;
768 struct asd_sas_port
*sas_port
= sas_phy
->port
;
769 struct hisi_sas_port
*port
= to_hisi_sas_port(sas_port
);
775 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
776 port
->port_attached
= 1;
777 port
->id
= phy
->port_id
;
779 sas_port
->lldd_port
= port
;
780 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
783 static void hisi_sas_do_release_task(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
784 struct hisi_sas_slot
*slot
)
788 struct task_status_struct
*ts
;
790 ts
= &task
->task_status
;
792 ts
->resp
= SAS_TASK_COMPLETE
;
793 ts
->stat
= SAS_ABORTED_TASK
;
794 spin_lock_irqsave(&task
->task_state_lock
, flags
);
795 task
->task_state_flags
&=
796 ~(SAS_TASK_STATE_PENDING
| SAS_TASK_AT_INITIATOR
);
797 task
->task_state_flags
|= SAS_TASK_STATE_DONE
;
798 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
801 hisi_sas_slot_task_free(hisi_hba
, task
, slot
);
804 /* hisi_hba.lock should be locked */
805 static void hisi_sas_release_task(struct hisi_hba
*hisi_hba
,
806 struct domain_device
*device
)
808 struct hisi_sas_slot
*slot
, *slot2
;
809 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
811 list_for_each_entry_safe(slot
, slot2
, &sas_dev
->list
, entry
)
812 hisi_sas_do_release_task(hisi_hba
, slot
->task
, slot
);
815 void hisi_sas_release_tasks(struct hisi_hba
*hisi_hba
)
817 struct hisi_sas_device
*sas_dev
;
818 struct domain_device
*device
;
821 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
822 sas_dev
= &hisi_hba
->devices
[i
];
823 device
= sas_dev
->sas_device
;
825 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
) ||
829 hisi_sas_release_task(hisi_hba
, device
);
832 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks
);
834 static void hisi_sas_dereg_device(struct hisi_hba
*hisi_hba
,
835 struct domain_device
*device
)
837 if (hisi_hba
->hw
->dereg_device
)
838 hisi_hba
->hw
->dereg_device(hisi_hba
, device
);
841 static void hisi_sas_dev_gone(struct domain_device
*device
)
843 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
844 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
845 struct device
*dev
= hisi_hba
->dev
;
847 dev_info(dev
, "dev[%d:%x] is gone\n",
848 sas_dev
->device_id
, sas_dev
->dev_type
);
850 if (!test_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
)) {
851 hisi_sas_internal_task_abort(hisi_hba
, device
,
852 HISI_SAS_INT_ABT_DEV
, 0);
854 hisi_sas_dereg_device(hisi_hba
, device
);
856 hisi_hba
->hw
->clear_itct(hisi_hba
, sas_dev
);
857 device
->lldd_dev
= NULL
;
860 if (hisi_hba
->hw
->free_device
)
861 hisi_hba
->hw
->free_device(sas_dev
);
862 sas_dev
->dev_type
= SAS_PHY_UNUSED
;
865 static int hisi_sas_queue_command(struct sas_task
*task
, gfp_t gfp_flags
)
867 return hisi_sas_task_exec(task
, gfp_flags
, 0, NULL
);
870 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
873 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
874 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
875 int phy_no
= sas_phy
->id
;
878 case PHY_FUNC_HARD_RESET
:
879 hisi_hba
->hw
->phy_hard_reset(hisi_hba
, phy_no
);
882 case PHY_FUNC_LINK_RESET
:
883 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
885 hisi_hba
->hw
->phy_start(hisi_hba
, phy_no
);
888 case PHY_FUNC_DISABLE
:
889 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
892 case PHY_FUNC_SET_LINK_RATE
:
893 hisi_hba
->hw
->phy_set_linkrate(hisi_hba
, phy_no
, funcdata
);
895 case PHY_FUNC_GET_EVENTS
:
896 if (hisi_hba
->hw
->get_events
) {
897 hisi_hba
->hw
->get_events(hisi_hba
, phy_no
);
901 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
908 static void hisi_sas_task_done(struct sas_task
*task
)
910 if (!del_timer(&task
->slow_task
->timer
))
912 complete(&task
->slow_task
->completion
);
915 static void hisi_sas_tmf_timedout(struct timer_list
*t
)
917 struct sas_task_slow
*slow
= from_timer(slow
, t
, timer
);
918 struct sas_task
*task
= slow
->task
;
921 spin_lock_irqsave(&task
->task_state_lock
, flags
);
922 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
923 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
924 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
926 complete(&task
->slow_task
->completion
);
929 #define TASK_TIMEOUT 20
931 #define INTERNAL_ABORT_TIMEOUT 6
932 static int hisi_sas_exec_internal_tmf_task(struct domain_device
*device
,
933 void *parameter
, u32 para_len
,
934 struct hisi_sas_tmf_task
*tmf
)
936 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
937 struct hisi_hba
*hisi_hba
= sas_dev
->hisi_hba
;
938 struct device
*dev
= hisi_hba
->dev
;
939 struct sas_task
*task
;
942 for (retry
= 0; retry
< TASK_RETRY
; retry
++) {
943 task
= sas_alloc_slow_task(GFP_KERNEL
);
948 task
->task_proto
= device
->tproto
;
950 if (dev_is_sata(device
)) {
951 task
->ata_task
.device_control_reg_update
= 1;
952 memcpy(&task
->ata_task
.fis
, parameter
, para_len
);
954 memcpy(&task
->ssp_task
, parameter
, para_len
);
956 task
->task_done
= hisi_sas_task_done
;
958 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
959 task
->slow_task
->timer
.expires
= jiffies
+ TASK_TIMEOUT
*HZ
;
960 add_timer(&task
->slow_task
->timer
);
962 res
= hisi_sas_task_exec(task
, GFP_KERNEL
, 1, tmf
);
965 del_timer(&task
->slow_task
->timer
);
966 dev_err(dev
, "abort tmf: executing internal task failed: %d\n",
971 wait_for_completion(&task
->slow_task
->completion
);
972 res
= TMF_RESP_FUNC_FAILED
;
973 /* Even TMF timed out, return direct. */
974 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
975 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
976 struct hisi_sas_slot
*slot
= task
->lldd_task
;
978 dev_err(dev
, "abort tmf: TMF task timeout and not done\n");
984 dev_err(dev
, "abort tmf: TMF task timeout\n");
987 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
988 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
989 res
= TMF_RESP_FUNC_COMPLETE
;
993 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
994 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
995 res
= TMF_RESP_FUNC_SUCC
;
999 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1000 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
1001 /* no error, but return the number of bytes of
1004 dev_warn(dev
, "abort tmf: task to dev %016llx "
1005 "resp: 0x%x sts 0x%x underrun\n",
1006 SAS_ADDR(device
->sas_addr
),
1007 task
->task_status
.resp
,
1008 task
->task_status
.stat
);
1009 res
= task
->task_status
.residual
;
1013 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1014 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
1015 dev_warn(dev
, "abort tmf: blocked task error\n");
1020 dev_warn(dev
, "abort tmf: task to dev "
1021 "%016llx resp: 0x%x status 0x%x\n",
1022 SAS_ADDR(device
->sas_addr
), task
->task_status
.resp
,
1023 task
->task_status
.stat
);
1024 sas_free_task(task
);
1028 if (retry
== TASK_RETRY
)
1029 dev_warn(dev
, "abort tmf: executing internal task failed!\n");
1030 sas_free_task(task
);
1034 static void hisi_sas_fill_ata_reset_cmd(struct ata_device
*dev
,
1035 bool reset
, int pmp
, u8
*fis
)
1037 struct ata_taskfile tf
;
1039 ata_tf_init(dev
, &tf
);
1043 tf
.ctl
&= ~ATA_SRST
;
1044 tf
.command
= ATA_CMD_DEV_RESET
;
1045 ata_tf_to_fis(&tf
, pmp
, 0, fis
);
1048 static int hisi_sas_softreset_ata_disk(struct domain_device
*device
)
1051 struct ata_port
*ap
= device
->sata_dev
.ap
;
1052 struct ata_link
*link
;
1053 int rc
= TMF_RESP_FUNC_FAILED
;
1054 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1055 struct device
*dev
= hisi_hba
->dev
;
1056 int s
= sizeof(struct host_to_dev_fis
);
1058 ata_for_each_link(link
, ap
, EDGE
) {
1059 int pmp
= sata_srst_pmp(link
);
1061 hisi_sas_fill_ata_reset_cmd(link
->device
, 1, pmp
, fis
);
1062 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
, s
, NULL
);
1063 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1067 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1068 ata_for_each_link(link
, ap
, EDGE
) {
1069 int pmp
= sata_srst_pmp(link
);
1071 hisi_sas_fill_ata_reset_cmd(link
->device
, 0, pmp
, fis
);
1072 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
,
1074 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1075 dev_err(dev
, "ata disk de-reset failed\n");
1078 dev_err(dev
, "ata disk reset failed\n");
1081 if (rc
== TMF_RESP_FUNC_COMPLETE
)
1082 hisi_sas_release_task(hisi_hba
, device
);
1087 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device
*device
,
1088 u8
*lun
, struct hisi_sas_tmf_task
*tmf
)
1090 struct sas_ssp_task ssp_task
;
1092 if (!(device
->tproto
& SAS_PROTOCOL_SSP
))
1093 return TMF_RESP_FUNC_ESUPP
;
1095 memcpy(ssp_task
.LUN
, lun
, 8);
1097 return hisi_sas_exec_internal_tmf_task(device
, &ssp_task
,
1098 sizeof(ssp_task
), tmf
);
1101 static void hisi_sas_refresh_port_id(struct hisi_hba
*hisi_hba
)
1103 u32 state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1106 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1107 struct hisi_sas_device
*sas_dev
= &hisi_hba
->devices
[i
];
1108 struct domain_device
*device
= sas_dev
->sas_device
;
1109 struct asd_sas_port
*sas_port
;
1110 struct hisi_sas_port
*port
;
1111 struct hisi_sas_phy
*phy
= NULL
;
1112 struct asd_sas_phy
*sas_phy
;
1114 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
)
1115 || !device
|| !device
->port
)
1118 sas_port
= device
->port
;
1119 port
= to_hisi_sas_port(sas_port
);
1121 list_for_each_entry(sas_phy
, &sas_port
->phy_list
, port_phy_el
)
1122 if (state
& BIT(sas_phy
->id
)) {
1123 phy
= sas_phy
->lldd_phy
;
1128 port
->id
= phy
->port_id
;
1130 /* Update linkrate of directly attached device. */
1131 if (!device
->parent
)
1132 device
->linkrate
= phy
->sas_phy
.linkrate
;
1134 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
1140 static void hisi_sas_rescan_topology(struct hisi_hba
*hisi_hba
, u32 old_state
,
1143 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1144 struct asd_sas_port
*_sas_port
= NULL
;
1147 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++) {
1148 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1149 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1150 struct asd_sas_port
*sas_port
= sas_phy
->port
;
1151 bool do_port_check
= !!(_sas_port
!= sas_port
);
1153 if (!sas_phy
->phy
->enabled
)
1156 /* Report PHY state change to libsas */
1157 if (state
& BIT(phy_no
)) {
1158 if (do_port_check
&& sas_port
&& sas_port
->port_dev
) {
1159 struct domain_device
*dev
= sas_port
->port_dev
;
1161 _sas_port
= sas_port
;
1163 if (DEV_IS_EXPANDER(dev
->dev_type
))
1164 sas_ha
->notify_port_event(sas_phy
,
1165 PORTE_BROADCAST_RCVD
);
1167 } else if (old_state
& (1 << phy_no
))
1168 /* PHY down but was up before */
1169 hisi_sas_phy_down(hisi_hba
, phy_no
, 0);
1174 static int hisi_sas_controller_reset(struct hisi_hba
*hisi_hba
)
1176 struct device
*dev
= hisi_hba
->dev
;
1177 struct Scsi_Host
*shost
= hisi_hba
->shost
;
1178 u32 old_state
, state
;
1181 if (!hisi_hba
->hw
->soft_reset
)
1184 if (test_and_set_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
))
1187 dev_info(dev
, "controller resetting...\n");
1188 old_state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1190 scsi_block_requests(shost
);
1191 if (timer_pending(&hisi_hba
->timer
))
1192 del_timer_sync(&hisi_hba
->timer
);
1194 set_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1195 rc
= hisi_hba
->hw
->soft_reset(hisi_hba
);
1197 dev_warn(dev
, "controller reset failed (%d)\n", rc
);
1198 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1199 scsi_unblock_requests(shost
);
1202 hisi_sas_release_tasks(hisi_hba
);
1204 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1206 /* Init and wait for PHYs to come up and all libsas event finished. */
1207 hisi_hba
->hw
->phys_init(hisi_hba
);
1209 hisi_sas_refresh_port_id(hisi_hba
);
1210 scsi_unblock_requests(shost
);
1212 state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1213 hisi_sas_rescan_topology(hisi_hba
, old_state
, state
);
1214 dev_info(dev
, "controller reset complete\n");
1217 clear_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
);
1222 static int hisi_sas_abort_task(struct sas_task
*task
)
1224 struct scsi_lun lun
;
1225 struct hisi_sas_tmf_task tmf_task
;
1226 struct domain_device
*device
= task
->dev
;
1227 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1228 struct hisi_hba
*hisi_hba
;
1230 int rc
= TMF_RESP_FUNC_FAILED
;
1231 unsigned long flags
;
1234 return TMF_RESP_FUNC_FAILED
;
1236 hisi_hba
= dev_to_hisi_hba(task
->dev
);
1237 dev
= hisi_hba
->dev
;
1239 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1240 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
1241 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1242 rc
= TMF_RESP_FUNC_COMPLETE
;
1245 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
1246 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1248 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1249 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1250 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1251 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1252 u32 tag
= slot
->idx
;
1255 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1256 tmf_task
.tmf
= TMF_ABORT_TASK
;
1257 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1259 rc
= hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
,
1262 rc2
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1263 HISI_SAS_INT_ABT_CMD
, tag
);
1265 dev_err(dev
, "abort task: internal abort (%d)\n", rc2
);
1266 return TMF_RESP_FUNC_FAILED
;
1270 * If the TMF finds that the IO is not in the device and also
1271 * the internal abort does not succeed, then it is safe to
1273 * Note: if the internal abort succeeds then the slot
1274 * will have already been completed
1276 if (rc
== TMF_RESP_FUNC_COMPLETE
&& rc2
!= TMF_RESP_FUNC_SUCC
) {
1277 if (task
->lldd_task
)
1278 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1280 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
1281 task
->task_proto
& SAS_PROTOCOL_STP
) {
1282 if (task
->dev
->dev_type
== SAS_SATA_DEV
) {
1283 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1284 HISI_SAS_INT_ABT_DEV
, 0);
1286 dev_err(dev
, "abort task: internal abort failed\n");
1289 hisi_sas_dereg_device(hisi_hba
, device
);
1290 rc
= hisi_sas_softreset_ata_disk(device
);
1292 } else if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SMP
) {
1294 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1295 u32 tag
= slot
->idx
;
1297 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1298 HISI_SAS_INT_ABT_CMD
, tag
);
1299 if (((rc
< 0) || (rc
== TMF_RESP_FUNC_FAILED
)) &&
1301 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1305 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1306 dev_notice(dev
, "abort task: rc=%d\n", rc
);
1310 static int hisi_sas_abort_task_set(struct domain_device
*device
, u8
*lun
)
1312 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1313 struct device
*dev
= hisi_hba
->dev
;
1314 struct hisi_sas_tmf_task tmf_task
;
1315 int rc
= TMF_RESP_FUNC_FAILED
;
1317 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1318 HISI_SAS_INT_ABT_DEV
, 0);
1320 dev_err(dev
, "abort task set: internal abort rc=%d\n", rc
);
1321 return TMF_RESP_FUNC_FAILED
;
1323 hisi_sas_dereg_device(hisi_hba
, device
);
1325 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
1326 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1328 if (rc
== TMF_RESP_FUNC_COMPLETE
)
1329 hisi_sas_release_task(hisi_hba
, device
);
1334 static int hisi_sas_clear_aca(struct domain_device
*device
, u8
*lun
)
1336 int rc
= TMF_RESP_FUNC_FAILED
;
1337 struct hisi_sas_tmf_task tmf_task
;
1339 tmf_task
.tmf
= TMF_CLEAR_ACA
;
1340 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1345 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device
*device
)
1347 struct sas_phy
*phy
= sas_get_local_phy(device
);
1348 int rc
, reset_type
= (device
->dev_type
== SAS_SATA_DEV
||
1349 (device
->tproto
& SAS_PROTOCOL_STP
)) ? 0 : 1;
1350 rc
= sas_phy_reset(phy
, reset_type
);
1351 sas_put_local_phy(phy
);
1356 static int hisi_sas_I_T_nexus_reset(struct domain_device
*device
)
1358 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1359 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1360 struct device
*dev
= hisi_hba
->dev
;
1361 int rc
= TMF_RESP_FUNC_FAILED
;
1363 if (sas_dev
->dev_status
!= HISI_SAS_DEV_EH
)
1364 return TMF_RESP_FUNC_FAILED
;
1365 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
1367 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1368 HISI_SAS_INT_ABT_DEV
, 0);
1370 dev_err(dev
, "I_T nexus reset: internal abort (%d)\n", rc
);
1371 return TMF_RESP_FUNC_FAILED
;
1373 hisi_sas_dereg_device(hisi_hba
, device
);
1375 rc
= hisi_sas_debug_I_T_nexus_reset(device
);
1377 if ((rc
== TMF_RESP_FUNC_COMPLETE
) || (rc
== -ENODEV
))
1378 hisi_sas_release_task(hisi_hba
, device
);
1383 static int hisi_sas_lu_reset(struct domain_device
*device
, u8
*lun
)
1385 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1386 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1387 struct device
*dev
= hisi_hba
->dev
;
1388 int rc
= TMF_RESP_FUNC_FAILED
;
1390 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1391 if (dev_is_sata(device
)) {
1392 struct sas_phy
*phy
;
1394 /* Clear internal IO and then hardreset */
1395 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1396 HISI_SAS_INT_ABT_DEV
, 0);
1398 dev_err(dev
, "lu_reset: internal abort failed\n");
1401 hisi_sas_dereg_device(hisi_hba
, device
);
1403 phy
= sas_get_local_phy(device
);
1405 rc
= sas_phy_reset(phy
, 1);
1408 hisi_sas_release_task(hisi_hba
, device
);
1409 sas_put_local_phy(phy
);
1411 struct hisi_sas_tmf_task tmf_task
= { .tmf
= TMF_LU_RESET
};
1413 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1414 HISI_SAS_INT_ABT_DEV
, 0);
1416 dev_err(dev
, "lu_reset: internal abort failed\n");
1419 hisi_sas_dereg_device(hisi_hba
, device
);
1421 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1422 if (rc
== TMF_RESP_FUNC_COMPLETE
)
1423 hisi_sas_release_task(hisi_hba
, device
);
1426 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1427 dev_err(dev
, "lu_reset: for device[%d]:rc= %d\n",
1428 sas_dev
->device_id
, rc
);
1432 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct
*sas_ha
)
1434 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
1435 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r
);
1437 queue_work(hisi_hba
->wq
, &r
.work
);
1438 wait_for_completion(r
.completion
);
1440 return TMF_RESP_FUNC_COMPLETE
;
1442 return TMF_RESP_FUNC_FAILED
;
1445 static int hisi_sas_query_task(struct sas_task
*task
)
1447 struct scsi_lun lun
;
1448 struct hisi_sas_tmf_task tmf_task
;
1449 int rc
= TMF_RESP_FUNC_FAILED
;
1451 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1452 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1453 struct domain_device
*device
= task
->dev
;
1454 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1455 u32 tag
= slot
->idx
;
1457 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1458 tmf_task
.tmf
= TMF_QUERY_TASK
;
1459 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1461 rc
= hisi_sas_debug_issue_ssp_tmf(device
,
1465 /* The task is still in Lun, release it then */
1466 case TMF_RESP_FUNC_SUCC
:
1467 /* The task is not in Lun or failed, reset the phy */
1468 case TMF_RESP_FUNC_FAILED
:
1469 case TMF_RESP_FUNC_COMPLETE
:
1472 rc
= TMF_RESP_FUNC_FAILED
;
1480 hisi_sas_internal_abort_task_exec(struct hisi_hba
*hisi_hba
, int device_id
,
1481 struct sas_task
*task
, int abort_flag
,
1484 struct domain_device
*device
= task
->dev
;
1485 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1486 struct device
*dev
= hisi_hba
->dev
;
1487 struct hisi_sas_port
*port
;
1488 struct hisi_sas_slot
*slot
;
1489 struct asd_sas_port
*sas_port
= device
->port
;
1490 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
1491 struct hisi_sas_dq
*dq
= sas_dev
->dq
;
1492 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
1493 unsigned long flags
, flags_dq
= 0;
1496 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
)))
1502 port
= to_hisi_sas_port(sas_port
);
1504 /* simply get a slot and send abort command */
1505 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1506 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
1508 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1511 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1513 slot
= &hisi_hba
->slot_info
[slot_idx
];
1514 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
1516 slot
->buf
= dma_pool_alloc(hisi_hba
->buffer_pool
,
1517 GFP_ATOMIC
, &slot
->buf_dma
);
1523 spin_lock_irqsave(&dq
->lock
, flags_dq
);
1524 wr_q_index
= hisi_hba
->hw
->get_free_slot(hisi_hba
, dq
);
1525 if (wr_q_index
< 0) {
1526 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
1529 list_add_tail(&slot
->delivery
, &dq
->list
);
1530 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
1532 dlvry_queue
= dq
->id
;
1533 dlvry_queue_slot
= wr_q_index
;
1535 slot
->idx
= slot_idx
;
1536 slot
->n_elem
= n_elem
;
1537 slot
->dlvry_queue
= dlvry_queue
;
1538 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
1539 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
1540 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
1543 slot
->is_internal
= true;
1544 task
->lldd_task
= slot
;
1546 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
1547 memset(hisi_sas_cmd_hdr_addr_mem(slot
), 0, HISI_SAS_COMMAND_TABLE_SZ
);
1548 memset(hisi_sas_status_buf_addr_mem(slot
), 0, HISI_SAS_STATUS_BUF_SZ
);
1550 hisi_sas_task_prep_abort(hisi_hba
, slot
, device_id
,
1551 abort_flag
, task_tag
);
1553 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1554 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
1555 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1558 /* send abort command to the chip */
1559 spin_lock_irqsave(&dq
->lock
, flags
);
1560 list_add_tail(&slot
->entry
, &sas_dev
->list
);
1561 hisi_hba
->hw
->start_delivery(dq
);
1562 spin_unlock_irqrestore(&dq
->lock
, flags
);
1567 dma_pool_free(hisi_hba
->buffer_pool
, slot
->buf
,
1570 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1571 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
1572 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1574 dev_err(dev
, "internal abort task prep: failed[%d]!\n", rc
);
1580 * hisi_sas_internal_task_abort -- execute an internal
1581 * abort command for single IO command or a device
1582 * @hisi_hba: host controller struct
1583 * @device: domain device
1584 * @abort_flag: mode of operation, device or single IO
1585 * @tag: tag of IO to be aborted (only relevant to single
1589 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
1590 struct domain_device
*device
,
1591 int abort_flag
, int tag
)
1593 struct sas_task
*task
;
1594 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1595 struct device
*dev
= hisi_hba
->dev
;
1599 * The interface is not realized means this HW don't support internal
1600 * abort, or don't need to do internal abort. Then here, we return
1601 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1602 * the internal abort has been executed and returned CQ.
1604 if (!hisi_hba
->hw
->prep_abort
)
1605 return TMF_RESP_FUNC_FAILED
;
1607 task
= sas_alloc_slow_task(GFP_KERNEL
);
1612 task
->task_proto
= device
->tproto
;
1613 task
->task_done
= hisi_sas_task_done
;
1614 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
1615 task
->slow_task
->timer
.expires
= jiffies
+ INTERNAL_ABORT_TIMEOUT
*HZ
;
1616 add_timer(&task
->slow_task
->timer
);
1618 res
= hisi_sas_internal_abort_task_exec(hisi_hba
, sas_dev
->device_id
,
1619 task
, abort_flag
, tag
);
1621 del_timer(&task
->slow_task
->timer
);
1622 dev_err(dev
, "internal task abort: executing internal task failed: %d\n",
1626 wait_for_completion(&task
->slow_task
->completion
);
1627 res
= TMF_RESP_FUNC_FAILED
;
1629 /* Internal abort timed out */
1630 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
1631 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
1632 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1636 dev_err(dev
, "internal task abort: timeout and not done.\n");
1640 dev_err(dev
, "internal task abort: timeout.\n");
1643 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1644 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
1645 res
= TMF_RESP_FUNC_COMPLETE
;
1649 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1650 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
1651 res
= TMF_RESP_FUNC_SUCC
;
1656 dev_dbg(dev
, "internal task abort: task to dev %016llx task=%p "
1657 "resp: 0x%x sts 0x%x\n",
1658 SAS_ADDR(device
->sas_addr
),
1660 task
->task_status
.resp
, /* 0 is complete, -1 is undelivered */
1661 task
->task_status
.stat
);
1662 sas_free_task(task
);
1667 static void hisi_sas_port_formed(struct asd_sas_phy
*sas_phy
)
1669 hisi_sas_port_notify_formed(sas_phy
);
1672 static void hisi_sas_port_deformed(struct asd_sas_phy
*sas_phy
)
1676 static int hisi_sas_write_gpio(struct sas_ha_struct
*sha
, u8 reg_type
,
1677 u8 reg_index
, u8 reg_count
, u8
*write_data
)
1679 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
1681 if (!hisi_hba
->hw
->write_gpio
)
1684 return hisi_hba
->hw
->write_gpio(hisi_hba
, reg_type
,
1685 reg_index
, reg_count
, write_data
);
1688 static void hisi_sas_phy_disconnected(struct hisi_sas_phy
*phy
)
1690 phy
->phy_attached
= 0;
1695 void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
)
1697 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1698 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1699 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1702 /* Phy down but ready */
1703 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
1704 hisi_sas_port_notify_formed(sas_phy
);
1706 struct hisi_sas_port
*port
= phy
->port
;
1708 /* Phy down and not ready */
1709 sas_ha
->notify_phy_event(sas_phy
, PHYE_LOSS_OF_SIGNAL
);
1710 sas_phy_disconnected(sas_phy
);
1713 if (phy
->phy_type
& PORT_TYPE_SAS
) {
1714 int port_id
= port
->id
;
1716 if (!hisi_hba
->hw
->get_wideport_bitmap(hisi_hba
,
1718 port
->port_attached
= 0;
1719 } else if (phy
->phy_type
& PORT_TYPE_SATA
)
1720 port
->port_attached
= 0;
1722 hisi_sas_phy_disconnected(phy
);
1725 EXPORT_SYMBOL_GPL(hisi_sas_phy_down
);
1727 void hisi_sas_kill_tasklets(struct hisi_hba
*hisi_hba
)
1731 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1732 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1734 tasklet_kill(&cq
->tasklet
);
1737 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets
);
1739 struct scsi_transport_template
*hisi_sas_stt
;
1740 EXPORT_SYMBOL_GPL(hisi_sas_stt
);
1742 static struct device_attribute
*host_attrs
[] = {
1743 &dev_attr_phy_event_threshold
,
1747 static struct scsi_host_template _hisi_sas_sht
= {
1748 .module
= THIS_MODULE
,
1750 .queuecommand
= sas_queuecommand
,
1751 .target_alloc
= sas_target_alloc
,
1752 .slave_configure
= hisi_sas_slave_configure
,
1753 .scan_finished
= hisi_sas_scan_finished
,
1754 .scan_start
= hisi_sas_scan_start
,
1755 .change_queue_depth
= sas_change_queue_depth
,
1756 .bios_param
= sas_bios_param
,
1759 .sg_tablesize
= SG_ALL
,
1760 .max_sectors
= SCSI_DEFAULT_MAX_SECTORS
,
1761 .use_clustering
= ENABLE_CLUSTERING
,
1762 .eh_device_reset_handler
= sas_eh_device_reset_handler
,
1763 .eh_target_reset_handler
= sas_eh_target_reset_handler
,
1764 .target_destroy
= sas_target_destroy
,
1766 .shost_attrs
= host_attrs
,
1768 struct scsi_host_template
*hisi_sas_sht
= &_hisi_sas_sht
;
1769 EXPORT_SYMBOL_GPL(hisi_sas_sht
);
1771 static struct sas_domain_function_template hisi_sas_transport_ops
= {
1772 .lldd_dev_found
= hisi_sas_dev_found
,
1773 .lldd_dev_gone
= hisi_sas_dev_gone
,
1774 .lldd_execute_task
= hisi_sas_queue_command
,
1775 .lldd_control_phy
= hisi_sas_control_phy
,
1776 .lldd_abort_task
= hisi_sas_abort_task
,
1777 .lldd_abort_task_set
= hisi_sas_abort_task_set
,
1778 .lldd_clear_aca
= hisi_sas_clear_aca
,
1779 .lldd_I_T_nexus_reset
= hisi_sas_I_T_nexus_reset
,
1780 .lldd_lu_reset
= hisi_sas_lu_reset
,
1781 .lldd_query_task
= hisi_sas_query_task
,
1782 .lldd_clear_nexus_ha
= hisi_sas_clear_nexus_ha
,
1783 .lldd_port_formed
= hisi_sas_port_formed
,
1784 .lldd_port_deformed
= hisi_sas_port_deformed
,
1785 .lldd_write_gpio
= hisi_sas_write_gpio
,
1788 void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
)
1790 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1792 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1793 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1794 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1796 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1797 memset(hisi_hba
->cmd_hdr
[i
], 0, s
);
1800 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1801 memset(hisi_hba
->complete_hdr
[i
], 0, s
);
1805 s
= sizeof(struct hisi_sas_initial_fis
) * hisi_hba
->n_phy
;
1806 memset(hisi_hba
->initial_fis
, 0, s
);
1808 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1809 memset(hisi_hba
->iost
, 0, s
);
1811 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1812 memset(hisi_hba
->breakpoint
, 0, s
);
1814 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1815 memset(hisi_hba
->sata_breakpoint
, 0, s
);
1817 EXPORT_SYMBOL_GPL(hisi_sas_init_mem
);
1819 int hisi_sas_alloc(struct hisi_hba
*hisi_hba
, struct Scsi_Host
*shost
)
1821 struct device
*dev
= hisi_hba
->dev
;
1822 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1824 spin_lock_init(&hisi_hba
->lock
);
1825 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1826 hisi_sas_phy_init(hisi_hba
, i
);
1827 hisi_hba
->port
[i
].port_attached
= 0;
1828 hisi_hba
->port
[i
].id
= -1;
1831 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1832 hisi_hba
->devices
[i
].dev_type
= SAS_PHY_UNUSED
;
1833 hisi_hba
->devices
[i
].device_id
= i
;
1834 hisi_hba
->devices
[i
].dev_status
= HISI_SAS_DEV_NORMAL
;
1837 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1838 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1839 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1841 /* Completion queue structure */
1843 cq
->hisi_hba
= hisi_hba
;
1845 /* Delivery queue structure */
1846 spin_lock_init(&dq
->lock
);
1847 INIT_LIST_HEAD(&dq
->list
);
1849 dq
->hisi_hba
= hisi_hba
;
1851 /* Delivery queue */
1852 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1853 hisi_hba
->cmd_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1854 &hisi_hba
->cmd_hdr_dma
[i
], GFP_KERNEL
);
1855 if (!hisi_hba
->cmd_hdr
[i
])
1858 /* Completion queue */
1859 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1860 hisi_hba
->complete_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1861 &hisi_hba
->complete_hdr_dma
[i
], GFP_KERNEL
);
1862 if (!hisi_hba
->complete_hdr
[i
])
1866 s
= sizeof(struct hisi_sas_slot_buf_table
);
1867 hisi_hba
->buffer_pool
= dma_pool_create("dma_buffer", dev
, s
, 16, 0);
1868 if (!hisi_hba
->buffer_pool
)
1871 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1872 hisi_hba
->itct
= dma_alloc_coherent(dev
, s
, &hisi_hba
->itct_dma
,
1874 if (!hisi_hba
->itct
)
1877 memset(hisi_hba
->itct
, 0, s
);
1879 hisi_hba
->slot_info
= devm_kcalloc(dev
, max_command_entries
,
1880 sizeof(struct hisi_sas_slot
),
1882 if (!hisi_hba
->slot_info
)
1885 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1886 hisi_hba
->iost
= dma_alloc_coherent(dev
, s
, &hisi_hba
->iost_dma
,
1888 if (!hisi_hba
->iost
)
1891 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1892 hisi_hba
->breakpoint
= dma_alloc_coherent(dev
, s
,
1893 &hisi_hba
->breakpoint_dma
, GFP_KERNEL
);
1894 if (!hisi_hba
->breakpoint
)
1897 hisi_hba
->slot_index_count
= max_command_entries
;
1898 s
= hisi_hba
->slot_index_count
/ BITS_PER_BYTE
;
1899 hisi_hba
->slot_index_tags
= devm_kzalloc(dev
, s
, GFP_KERNEL
);
1900 if (!hisi_hba
->slot_index_tags
)
1903 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1904 hisi_hba
->initial_fis
= dma_alloc_coherent(dev
, s
,
1905 &hisi_hba
->initial_fis_dma
, GFP_KERNEL
);
1906 if (!hisi_hba
->initial_fis
)
1909 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1910 hisi_hba
->sata_breakpoint
= dma_alloc_coherent(dev
, s
,
1911 &hisi_hba
->sata_breakpoint_dma
, GFP_KERNEL
);
1912 if (!hisi_hba
->sata_breakpoint
)
1914 hisi_sas_init_mem(hisi_hba
);
1916 hisi_sas_slot_index_init(hisi_hba
);
1918 hisi_hba
->wq
= create_singlethread_workqueue(dev_name(dev
));
1919 if (!hisi_hba
->wq
) {
1920 dev_err(dev
, "sas_alloc: failed to create workqueue\n");
1928 EXPORT_SYMBOL_GPL(hisi_sas_alloc
);
1930 void hisi_sas_free(struct hisi_hba
*hisi_hba
)
1932 struct device
*dev
= hisi_hba
->dev
;
1933 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1935 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1936 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1937 if (hisi_hba
->cmd_hdr
[i
])
1938 dma_free_coherent(dev
, s
,
1939 hisi_hba
->cmd_hdr
[i
],
1940 hisi_hba
->cmd_hdr_dma
[i
]);
1942 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1943 if (hisi_hba
->complete_hdr
[i
])
1944 dma_free_coherent(dev
, s
,
1945 hisi_hba
->complete_hdr
[i
],
1946 hisi_hba
->complete_hdr_dma
[i
]);
1949 dma_pool_destroy(hisi_hba
->buffer_pool
);
1951 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1953 dma_free_coherent(dev
, s
,
1954 hisi_hba
->itct
, hisi_hba
->itct_dma
);
1956 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1958 dma_free_coherent(dev
, s
,
1959 hisi_hba
->iost
, hisi_hba
->iost_dma
);
1961 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1962 if (hisi_hba
->breakpoint
)
1963 dma_free_coherent(dev
, s
,
1964 hisi_hba
->breakpoint
,
1965 hisi_hba
->breakpoint_dma
);
1968 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1969 if (hisi_hba
->initial_fis
)
1970 dma_free_coherent(dev
, s
,
1971 hisi_hba
->initial_fis
,
1972 hisi_hba
->initial_fis_dma
);
1974 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1975 if (hisi_hba
->sata_breakpoint
)
1976 dma_free_coherent(dev
, s
,
1977 hisi_hba
->sata_breakpoint
,
1978 hisi_hba
->sata_breakpoint_dma
);
1981 destroy_workqueue(hisi_hba
->wq
);
1983 EXPORT_SYMBOL_GPL(hisi_sas_free
);
1985 void hisi_sas_rst_work_handler(struct work_struct
*work
)
1987 struct hisi_hba
*hisi_hba
=
1988 container_of(work
, struct hisi_hba
, rst_work
);
1990 hisi_sas_controller_reset(hisi_hba
);
1992 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler
);
1994 void hisi_sas_sync_rst_work_handler(struct work_struct
*work
)
1996 struct hisi_sas_rst
*rst
=
1997 container_of(work
, struct hisi_sas_rst
, work
);
1999 if (!hisi_sas_controller_reset(rst
->hisi_hba
))
2001 complete(rst
->completion
);
2003 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler
);
2005 int hisi_sas_get_fw_info(struct hisi_hba
*hisi_hba
)
2007 struct device
*dev
= hisi_hba
->dev
;
2008 struct platform_device
*pdev
= hisi_hba
->platform_dev
;
2009 struct device_node
*np
= pdev
? pdev
->dev
.of_node
: NULL
;
2012 if (device_property_read_u8_array(dev
, "sas-addr", hisi_hba
->sas_addr
,
2014 dev_err(dev
, "could not get property sas-addr\n");
2020 * These properties are only required for platform device-based
2021 * controller with DT firmware.
2023 hisi_hba
->ctrl
= syscon_regmap_lookup_by_phandle(np
,
2024 "hisilicon,sas-syscon");
2025 if (IS_ERR(hisi_hba
->ctrl
)) {
2026 dev_err(dev
, "could not get syscon\n");
2030 if (device_property_read_u32(dev
, "ctrl-reset-reg",
2031 &hisi_hba
->ctrl_reset_reg
)) {
2033 "could not get property ctrl-reset-reg\n");
2037 if (device_property_read_u32(dev
, "ctrl-reset-sts-reg",
2038 &hisi_hba
->ctrl_reset_sts_reg
)) {
2040 "could not get property ctrl-reset-sts-reg\n");
2044 if (device_property_read_u32(dev
, "ctrl-clock-ena-reg",
2045 &hisi_hba
->ctrl_clock_ena_reg
)) {
2047 "could not get property ctrl-clock-ena-reg\n");
2052 refclk
= devm_clk_get(dev
, NULL
);
2054 dev_dbg(dev
, "no ref clk property\n");
2056 hisi_hba
->refclk_frequency_mhz
= clk_get_rate(refclk
) / 1000000;
2058 if (device_property_read_u32(dev
, "phy-count", &hisi_hba
->n_phy
)) {
2059 dev_err(dev
, "could not get property phy-count\n");
2063 if (device_property_read_u32(dev
, "queue-count",
2064 &hisi_hba
->queue_count
)) {
2065 dev_err(dev
, "could not get property queue-count\n");
2071 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info
);
2073 static struct Scsi_Host
*hisi_sas_shost_alloc(struct platform_device
*pdev
,
2074 const struct hisi_sas_hw
*hw
)
2076 struct resource
*res
;
2077 struct Scsi_Host
*shost
;
2078 struct hisi_hba
*hisi_hba
;
2079 struct device
*dev
= &pdev
->dev
;
2081 shost
= scsi_host_alloc(hisi_sas_sht
, sizeof(*hisi_hba
));
2083 dev_err(dev
, "scsi host alloc failed\n");
2086 hisi_hba
= shost_priv(shost
);
2088 INIT_WORK(&hisi_hba
->rst_work
, hisi_sas_rst_work_handler
);
2090 hisi_hba
->dev
= dev
;
2091 hisi_hba
->platform_dev
= pdev
;
2092 hisi_hba
->shost
= shost
;
2093 SHOST_TO_SAS_HA(shost
) = &hisi_hba
->sha
;
2095 timer_setup(&hisi_hba
->timer
, NULL
, 0);
2097 if (hisi_sas_get_fw_info(hisi_hba
) < 0)
2100 if (dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64)) &&
2101 dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32))) {
2102 dev_err(dev
, "No usable DMA addressing method\n");
2106 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2107 hisi_hba
->regs
= devm_ioremap_resource(dev
, res
);
2108 if (IS_ERR(hisi_hba
->regs
))
2111 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2113 hisi_hba
->sgpio_regs
= devm_ioremap_resource(dev
, res
);
2114 if (IS_ERR(hisi_hba
->sgpio_regs
))
2118 if (hisi_sas_alloc(hisi_hba
, shost
)) {
2119 hisi_sas_free(hisi_hba
);
2125 scsi_host_put(shost
);
2126 dev_err(dev
, "shost alloc failed\n");
2130 void hisi_sas_init_add(struct hisi_hba
*hisi_hba
)
2134 for (i
= 0; i
< hisi_hba
->n_phy
; i
++)
2135 memcpy(&hisi_hba
->phy
[i
].dev_sas_addr
,
2139 EXPORT_SYMBOL_GPL(hisi_sas_init_add
);
2141 int hisi_sas_probe(struct platform_device
*pdev
,
2142 const struct hisi_sas_hw
*hw
)
2144 struct Scsi_Host
*shost
;
2145 struct hisi_hba
*hisi_hba
;
2146 struct device
*dev
= &pdev
->dev
;
2147 struct asd_sas_phy
**arr_phy
;
2148 struct asd_sas_port
**arr_port
;
2149 struct sas_ha_struct
*sha
;
2150 int rc
, phy_nr
, port_nr
, i
;
2152 shost
= hisi_sas_shost_alloc(pdev
, hw
);
2156 sha
= SHOST_TO_SAS_HA(shost
);
2157 hisi_hba
= shost_priv(shost
);
2158 platform_set_drvdata(pdev
, sha
);
2160 phy_nr
= port_nr
= hisi_hba
->n_phy
;
2162 arr_phy
= devm_kcalloc(dev
, phy_nr
, sizeof(void *), GFP_KERNEL
);
2163 arr_port
= devm_kcalloc(dev
, port_nr
, sizeof(void *), GFP_KERNEL
);
2164 if (!arr_phy
|| !arr_port
) {
2169 sha
->sas_phy
= arr_phy
;
2170 sha
->sas_port
= arr_port
;
2171 sha
->lldd_ha
= hisi_hba
;
2173 shost
->transportt
= hisi_sas_stt
;
2174 shost
->max_id
= HISI_SAS_MAX_DEVICES
;
2175 shost
->max_lun
= ~0;
2176 shost
->max_channel
= 1;
2177 shost
->max_cmd_len
= 16;
2178 shost
->sg_tablesize
= min_t(u16
, SG_ALL
, HISI_SAS_SGE_PAGE_CNT
);
2179 shost
->can_queue
= hisi_hba
->hw
->max_command_entries
;
2180 shost
->cmd_per_lun
= hisi_hba
->hw
->max_command_entries
;
2182 sha
->sas_ha_name
= DRV_NAME
;
2183 sha
->dev
= hisi_hba
->dev
;
2184 sha
->lldd_module
= THIS_MODULE
;
2185 sha
->sas_addr
= &hisi_hba
->sas_addr
[0];
2186 sha
->num_phys
= hisi_hba
->n_phy
;
2187 sha
->core
.shost
= hisi_hba
->shost
;
2189 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
2190 sha
->sas_phy
[i
] = &hisi_hba
->phy
[i
].sas_phy
;
2191 sha
->sas_port
[i
] = &hisi_hba
->port
[i
].sas_port
;
2194 hisi_sas_init_add(hisi_hba
);
2196 rc
= scsi_add_host(shost
, &pdev
->dev
);
2200 rc
= sas_register_ha(sha
);
2202 goto err_out_register_ha
;
2204 rc
= hisi_hba
->hw
->hw_init(hisi_hba
);
2206 goto err_out_register_ha
;
2208 scsi_scan_host(shost
);
2212 err_out_register_ha
:
2213 scsi_remove_host(shost
);
2215 hisi_sas_free(hisi_hba
);
2216 scsi_host_put(shost
);
2219 EXPORT_SYMBOL_GPL(hisi_sas_probe
);
2221 int hisi_sas_remove(struct platform_device
*pdev
)
2223 struct sas_ha_struct
*sha
= platform_get_drvdata(pdev
);
2224 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
2225 struct Scsi_Host
*shost
= sha
->core
.shost
;
2227 if (timer_pending(&hisi_hba
->timer
))
2228 del_timer(&hisi_hba
->timer
);
2230 sas_unregister_ha(sha
);
2231 sas_remove_host(sha
->core
.shost
);
2233 hisi_sas_free(hisi_hba
);
2234 scsi_host_put(shost
);
2237 EXPORT_SYMBOL_GPL(hisi_sas_remove
);
2239 static __init
int hisi_sas_init(void)
2241 hisi_sas_stt
= sas_domain_attach_transport(&hisi_sas_transport_ops
);
2248 static __exit
void hisi_sas_exit(void)
2250 sas_release_transport(hisi_sas_stt
);
2253 module_init(hisi_sas_init
);
2254 module_exit(hisi_sas_exit
);
2256 MODULE_LICENSE("GPL");
2257 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2258 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2259 MODULE_ALIAS("platform:" DRV_NAME
);