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 int hisi_sas_controller_reset(struct hisi_hba
*hisi_hba
)
1254 struct device
*dev
= hisi_hba
->dev
;
1255 struct Scsi_Host
*shost
= hisi_hba
->shost
;
1256 u32 old_state
, state
;
1259 if (!hisi_hba
->hw
->soft_reset
)
1262 if (test_and_set_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
))
1265 dev_info(dev
, "controller resetting...\n");
1266 old_state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1268 scsi_block_requests(shost
);
1269 hisi_hba
->hw
->wait_cmds_complete_timeout(hisi_hba
, 100, 5000);
1271 if (timer_pending(&hisi_hba
->timer
))
1272 del_timer_sync(&hisi_hba
->timer
);
1274 set_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1275 rc
= hisi_hba
->hw
->soft_reset(hisi_hba
);
1277 dev_warn(dev
, "controller reset failed (%d)\n", rc
);
1278 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1279 scsi_unblock_requests(shost
);
1283 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1285 /* Init and wait for PHYs to come up and all libsas event finished. */
1286 hisi_hba
->hw
->phys_init(hisi_hba
);
1288 hisi_sas_refresh_port_id(hisi_hba
);
1289 hisi_sas_reset_init_all_devices(hisi_hba
);
1290 scsi_unblock_requests(shost
);
1292 state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1293 hisi_sas_rescan_topology(hisi_hba
, old_state
, state
);
1294 dev_info(dev
, "controller reset complete\n");
1297 clear_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
);
1302 static int hisi_sas_abort_task(struct sas_task
*task
)
1304 struct scsi_lun lun
;
1305 struct hisi_sas_tmf_task tmf_task
;
1306 struct domain_device
*device
= task
->dev
;
1307 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1308 struct hisi_hba
*hisi_hba
;
1310 int rc
= TMF_RESP_FUNC_FAILED
;
1311 unsigned long flags
;
1314 return TMF_RESP_FUNC_FAILED
;
1316 hisi_hba
= dev_to_hisi_hba(task
->dev
);
1317 dev
= hisi_hba
->dev
;
1319 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1320 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
1321 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1322 rc
= TMF_RESP_FUNC_COMPLETE
;
1325 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
1326 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1328 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1329 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1330 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1331 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1332 u32 tag
= slot
->idx
;
1335 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1336 tmf_task
.tmf
= TMF_ABORT_TASK
;
1337 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1339 rc
= hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
,
1342 rc2
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1343 HISI_SAS_INT_ABT_CMD
, tag
);
1345 dev_err(dev
, "abort task: internal abort (%d)\n", rc2
);
1346 return TMF_RESP_FUNC_FAILED
;
1350 * If the TMF finds that the IO is not in the device and also
1351 * the internal abort does not succeed, then it is safe to
1353 * Note: if the internal abort succeeds then the slot
1354 * will have already been completed
1356 if (rc
== TMF_RESP_FUNC_COMPLETE
&& rc2
!= TMF_RESP_FUNC_SUCC
) {
1357 if (task
->lldd_task
)
1358 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1360 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
1361 task
->task_proto
& SAS_PROTOCOL_STP
) {
1362 if (task
->dev
->dev_type
== SAS_SATA_DEV
) {
1363 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1364 HISI_SAS_INT_ABT_DEV
, 0);
1366 dev_err(dev
, "abort task: internal abort failed\n");
1369 hisi_sas_dereg_device(hisi_hba
, device
);
1370 rc
= hisi_sas_softreset_ata_disk(device
);
1372 } else if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SMP
) {
1374 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1375 u32 tag
= slot
->idx
;
1377 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1378 HISI_SAS_INT_ABT_CMD
, tag
);
1379 if (((rc
< 0) || (rc
== TMF_RESP_FUNC_FAILED
)) &&
1381 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1385 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1386 dev_notice(dev
, "abort task: rc=%d\n", rc
);
1390 static int hisi_sas_abort_task_set(struct domain_device
*device
, u8
*lun
)
1392 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1393 struct device
*dev
= hisi_hba
->dev
;
1394 struct hisi_sas_tmf_task tmf_task
;
1395 int rc
= TMF_RESP_FUNC_FAILED
;
1397 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1398 HISI_SAS_INT_ABT_DEV
, 0);
1400 dev_err(dev
, "abort task set: internal abort rc=%d\n", rc
);
1401 return TMF_RESP_FUNC_FAILED
;
1403 hisi_sas_dereg_device(hisi_hba
, device
);
1405 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
1406 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1408 if (rc
== TMF_RESP_FUNC_COMPLETE
)
1409 hisi_sas_release_task(hisi_hba
, device
);
1414 static int hisi_sas_clear_aca(struct domain_device
*device
, u8
*lun
)
1416 int rc
= TMF_RESP_FUNC_FAILED
;
1417 struct hisi_sas_tmf_task tmf_task
;
1419 tmf_task
.tmf
= TMF_CLEAR_ACA
;
1420 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1425 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device
*device
)
1427 struct sas_phy
*phy
= sas_get_local_phy(device
);
1428 int rc
, reset_type
= (device
->dev_type
== SAS_SATA_DEV
||
1429 (device
->tproto
& SAS_PROTOCOL_STP
)) ? 0 : 1;
1430 rc
= sas_phy_reset(phy
, reset_type
);
1431 sas_put_local_phy(phy
);
1436 static int hisi_sas_I_T_nexus_reset(struct domain_device
*device
)
1438 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1439 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1440 struct device
*dev
= hisi_hba
->dev
;
1441 int rc
= TMF_RESP_FUNC_FAILED
;
1443 if (sas_dev
->dev_status
!= HISI_SAS_DEV_EH
)
1444 return TMF_RESP_FUNC_FAILED
;
1445 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
1447 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1448 HISI_SAS_INT_ABT_DEV
, 0);
1450 dev_err(dev
, "I_T nexus reset: internal abort (%d)\n", rc
);
1451 return TMF_RESP_FUNC_FAILED
;
1453 hisi_sas_dereg_device(hisi_hba
, device
);
1455 rc
= hisi_sas_debug_I_T_nexus_reset(device
);
1457 if ((rc
== TMF_RESP_FUNC_COMPLETE
) || (rc
== -ENODEV
))
1458 hisi_sas_release_task(hisi_hba
, device
);
1463 static int hisi_sas_lu_reset(struct domain_device
*device
, u8
*lun
)
1465 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1466 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1467 struct device
*dev
= hisi_hba
->dev
;
1468 int rc
= TMF_RESP_FUNC_FAILED
;
1470 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1471 if (dev_is_sata(device
)) {
1472 struct sas_phy
*phy
;
1474 /* Clear internal IO and then hardreset */
1475 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1476 HISI_SAS_INT_ABT_DEV
, 0);
1478 dev_err(dev
, "lu_reset: internal abort failed\n");
1481 hisi_sas_dereg_device(hisi_hba
, device
);
1483 phy
= sas_get_local_phy(device
);
1485 rc
= sas_phy_reset(phy
, 1);
1488 hisi_sas_release_task(hisi_hba
, device
);
1489 sas_put_local_phy(phy
);
1491 struct hisi_sas_tmf_task tmf_task
= { .tmf
= TMF_LU_RESET
};
1493 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1494 HISI_SAS_INT_ABT_DEV
, 0);
1496 dev_err(dev
, "lu_reset: internal abort failed\n");
1499 hisi_sas_dereg_device(hisi_hba
, device
);
1501 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1502 if (rc
== TMF_RESP_FUNC_COMPLETE
)
1503 hisi_sas_release_task(hisi_hba
, device
);
1506 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1507 dev_err(dev
, "lu_reset: for device[%d]:rc= %d\n",
1508 sas_dev
->device_id
, rc
);
1512 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct
*sas_ha
)
1514 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
1515 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r
);
1517 queue_work(hisi_hba
->wq
, &r
.work
);
1518 wait_for_completion(r
.completion
);
1520 return TMF_RESP_FUNC_COMPLETE
;
1522 return TMF_RESP_FUNC_FAILED
;
1525 static int hisi_sas_query_task(struct sas_task
*task
)
1527 struct scsi_lun lun
;
1528 struct hisi_sas_tmf_task tmf_task
;
1529 int rc
= TMF_RESP_FUNC_FAILED
;
1531 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1532 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1533 struct domain_device
*device
= task
->dev
;
1534 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1535 u32 tag
= slot
->idx
;
1537 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1538 tmf_task
.tmf
= TMF_QUERY_TASK
;
1539 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1541 rc
= hisi_sas_debug_issue_ssp_tmf(device
,
1545 /* The task is still in Lun, release it then */
1546 case TMF_RESP_FUNC_SUCC
:
1547 /* The task is not in Lun or failed, reset the phy */
1548 case TMF_RESP_FUNC_FAILED
:
1549 case TMF_RESP_FUNC_COMPLETE
:
1552 rc
= TMF_RESP_FUNC_FAILED
;
1560 hisi_sas_internal_abort_task_exec(struct hisi_hba
*hisi_hba
, int device_id
,
1561 struct sas_task
*task
, int abort_flag
,
1564 struct domain_device
*device
= task
->dev
;
1565 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1566 struct device
*dev
= hisi_hba
->dev
;
1567 struct hisi_sas_port
*port
;
1568 struct hisi_sas_slot
*slot
;
1569 struct asd_sas_port
*sas_port
= device
->port
;
1570 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
1571 struct hisi_sas_dq
*dq
= sas_dev
->dq
;
1572 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
1573 unsigned long flags
, flags_dq
= 0;
1576 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
)))
1582 port
= to_hisi_sas_port(sas_port
);
1584 /* simply get a slot and send abort command */
1585 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1586 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
1588 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1591 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1593 slot
= &hisi_hba
->slot_info
[slot_idx
];
1594 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
1596 slot
->buf
= dma_pool_alloc(hisi_hba
->buffer_pool
,
1597 GFP_ATOMIC
, &slot
->buf_dma
);
1603 spin_lock_irqsave(&dq
->lock
, flags_dq
);
1604 wr_q_index
= hisi_hba
->hw
->get_free_slot(hisi_hba
, dq
);
1605 if (wr_q_index
< 0) {
1606 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
1609 list_add_tail(&slot
->delivery
, &dq
->list
);
1610 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
1612 dlvry_queue
= dq
->id
;
1613 dlvry_queue_slot
= wr_q_index
;
1615 slot
->idx
= slot_idx
;
1616 slot
->n_elem
= n_elem
;
1617 slot
->dlvry_queue
= dlvry_queue
;
1618 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
1619 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
1620 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
1623 slot
->is_internal
= true;
1624 task
->lldd_task
= slot
;
1626 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
1627 memset(hisi_sas_cmd_hdr_addr_mem(slot
), 0, HISI_SAS_COMMAND_TABLE_SZ
);
1628 memset(hisi_sas_status_buf_addr_mem(slot
), 0, HISI_SAS_STATUS_BUF_SZ
);
1630 hisi_sas_task_prep_abort(hisi_hba
, slot
, device_id
,
1631 abort_flag
, task_tag
);
1633 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1634 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
1635 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1638 /* send abort command to the chip */
1639 spin_lock_irqsave(&dq
->lock
, flags
);
1640 list_add_tail(&slot
->entry
, &sas_dev
->list
);
1641 hisi_hba
->hw
->start_delivery(dq
);
1642 spin_unlock_irqrestore(&dq
->lock
, flags
);
1647 dma_pool_free(hisi_hba
->buffer_pool
, slot
->buf
,
1650 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1651 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
1652 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1654 dev_err(dev
, "internal abort task prep: failed[%d]!\n", rc
);
1660 * hisi_sas_internal_task_abort -- execute an internal
1661 * abort command for single IO command or a device
1662 * @hisi_hba: host controller struct
1663 * @device: domain device
1664 * @abort_flag: mode of operation, device or single IO
1665 * @tag: tag of IO to be aborted (only relevant to single
1669 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
1670 struct domain_device
*device
,
1671 int abort_flag
, int tag
)
1673 struct sas_task
*task
;
1674 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1675 struct device
*dev
= hisi_hba
->dev
;
1679 * The interface is not realized means this HW don't support internal
1680 * abort, or don't need to do internal abort. Then here, we return
1681 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1682 * the internal abort has been executed and returned CQ.
1684 if (!hisi_hba
->hw
->prep_abort
)
1685 return TMF_RESP_FUNC_FAILED
;
1687 task
= sas_alloc_slow_task(GFP_KERNEL
);
1692 task
->task_proto
= device
->tproto
;
1693 task
->task_done
= hisi_sas_task_done
;
1694 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
1695 task
->slow_task
->timer
.expires
= jiffies
+ INTERNAL_ABORT_TIMEOUT
*HZ
;
1696 add_timer(&task
->slow_task
->timer
);
1698 res
= hisi_sas_internal_abort_task_exec(hisi_hba
, sas_dev
->device_id
,
1699 task
, abort_flag
, tag
);
1701 del_timer(&task
->slow_task
->timer
);
1702 dev_err(dev
, "internal task abort: executing internal task failed: %d\n",
1706 wait_for_completion(&task
->slow_task
->completion
);
1707 res
= TMF_RESP_FUNC_FAILED
;
1709 /* Internal abort timed out */
1710 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
1711 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
1712 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1716 dev_err(dev
, "internal task abort: timeout and not done.\n");
1720 dev_err(dev
, "internal task abort: timeout.\n");
1723 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1724 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
1725 res
= TMF_RESP_FUNC_COMPLETE
;
1729 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1730 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
1731 res
= TMF_RESP_FUNC_SUCC
;
1736 dev_dbg(dev
, "internal task abort: task to dev %016llx task=%p "
1737 "resp: 0x%x sts 0x%x\n",
1738 SAS_ADDR(device
->sas_addr
),
1740 task
->task_status
.resp
, /* 0 is complete, -1 is undelivered */
1741 task
->task_status
.stat
);
1742 sas_free_task(task
);
1747 static void hisi_sas_port_formed(struct asd_sas_phy
*sas_phy
)
1749 hisi_sas_port_notify_formed(sas_phy
);
1752 static void hisi_sas_port_deformed(struct asd_sas_phy
*sas_phy
)
1756 static int hisi_sas_write_gpio(struct sas_ha_struct
*sha
, u8 reg_type
,
1757 u8 reg_index
, u8 reg_count
, u8
*write_data
)
1759 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
1761 if (!hisi_hba
->hw
->write_gpio
)
1764 return hisi_hba
->hw
->write_gpio(hisi_hba
, reg_type
,
1765 reg_index
, reg_count
, write_data
);
1768 static void hisi_sas_phy_disconnected(struct hisi_sas_phy
*phy
)
1770 phy
->phy_attached
= 0;
1775 void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
)
1777 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1778 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1779 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1782 /* Phy down but ready */
1783 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
1784 hisi_sas_port_notify_formed(sas_phy
);
1786 struct hisi_sas_port
*port
= phy
->port
;
1788 /* Phy down and not ready */
1789 sas_ha
->notify_phy_event(sas_phy
, PHYE_LOSS_OF_SIGNAL
);
1790 sas_phy_disconnected(sas_phy
);
1793 if (phy
->phy_type
& PORT_TYPE_SAS
) {
1794 int port_id
= port
->id
;
1796 if (!hisi_hba
->hw
->get_wideport_bitmap(hisi_hba
,
1798 port
->port_attached
= 0;
1799 } else if (phy
->phy_type
& PORT_TYPE_SATA
)
1800 port
->port_attached
= 0;
1802 hisi_sas_phy_disconnected(phy
);
1805 EXPORT_SYMBOL_GPL(hisi_sas_phy_down
);
1807 void hisi_sas_kill_tasklets(struct hisi_hba
*hisi_hba
)
1811 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1812 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1814 tasklet_kill(&cq
->tasklet
);
1817 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets
);
1819 struct scsi_transport_template
*hisi_sas_stt
;
1820 EXPORT_SYMBOL_GPL(hisi_sas_stt
);
1822 struct device_attribute
*host_attrs
[] = {
1823 &dev_attr_phy_event_threshold
,
1826 EXPORT_SYMBOL_GPL(host_attrs
);
1828 static struct sas_domain_function_template hisi_sas_transport_ops
= {
1829 .lldd_dev_found
= hisi_sas_dev_found
,
1830 .lldd_dev_gone
= hisi_sas_dev_gone
,
1831 .lldd_execute_task
= hisi_sas_queue_command
,
1832 .lldd_control_phy
= hisi_sas_control_phy
,
1833 .lldd_abort_task
= hisi_sas_abort_task
,
1834 .lldd_abort_task_set
= hisi_sas_abort_task_set
,
1835 .lldd_clear_aca
= hisi_sas_clear_aca
,
1836 .lldd_I_T_nexus_reset
= hisi_sas_I_T_nexus_reset
,
1837 .lldd_lu_reset
= hisi_sas_lu_reset
,
1838 .lldd_query_task
= hisi_sas_query_task
,
1839 .lldd_clear_nexus_ha
= hisi_sas_clear_nexus_ha
,
1840 .lldd_port_formed
= hisi_sas_port_formed
,
1841 .lldd_port_deformed
= hisi_sas_port_deformed
,
1842 .lldd_write_gpio
= hisi_sas_write_gpio
,
1845 void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
)
1847 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1849 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1850 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1851 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1853 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1854 memset(hisi_hba
->cmd_hdr
[i
], 0, s
);
1857 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1858 memset(hisi_hba
->complete_hdr
[i
], 0, s
);
1862 s
= sizeof(struct hisi_sas_initial_fis
) * hisi_hba
->n_phy
;
1863 memset(hisi_hba
->initial_fis
, 0, s
);
1865 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1866 memset(hisi_hba
->iost
, 0, s
);
1868 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1869 memset(hisi_hba
->breakpoint
, 0, s
);
1871 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1872 memset(hisi_hba
->sata_breakpoint
, 0, s
);
1874 EXPORT_SYMBOL_GPL(hisi_sas_init_mem
);
1876 int hisi_sas_alloc(struct hisi_hba
*hisi_hba
, struct Scsi_Host
*shost
)
1878 struct device
*dev
= hisi_hba
->dev
;
1879 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1881 spin_lock_init(&hisi_hba
->lock
);
1882 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1883 hisi_sas_phy_init(hisi_hba
, i
);
1884 hisi_hba
->port
[i
].port_attached
= 0;
1885 hisi_hba
->port
[i
].id
= -1;
1888 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1889 hisi_hba
->devices
[i
].dev_type
= SAS_PHY_UNUSED
;
1890 hisi_hba
->devices
[i
].device_id
= i
;
1891 hisi_hba
->devices
[i
].dev_status
= HISI_SAS_DEV_NORMAL
;
1894 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1895 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1896 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1898 /* Completion queue structure */
1900 cq
->hisi_hba
= hisi_hba
;
1902 /* Delivery queue structure */
1903 spin_lock_init(&dq
->lock
);
1904 INIT_LIST_HEAD(&dq
->list
);
1906 dq
->hisi_hba
= hisi_hba
;
1908 /* Delivery queue */
1909 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1910 hisi_hba
->cmd_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1911 &hisi_hba
->cmd_hdr_dma
[i
], GFP_KERNEL
);
1912 if (!hisi_hba
->cmd_hdr
[i
])
1915 /* Completion queue */
1916 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1917 hisi_hba
->complete_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1918 &hisi_hba
->complete_hdr_dma
[i
], GFP_KERNEL
);
1919 if (!hisi_hba
->complete_hdr
[i
])
1923 s
= sizeof(struct hisi_sas_slot_buf_table
);
1924 hisi_hba
->buffer_pool
= dma_pool_create("dma_buffer", dev
, s
, 16, 0);
1925 if (!hisi_hba
->buffer_pool
)
1928 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1929 hisi_hba
->itct
= dma_alloc_coherent(dev
, s
, &hisi_hba
->itct_dma
,
1931 if (!hisi_hba
->itct
)
1934 memset(hisi_hba
->itct
, 0, s
);
1936 hisi_hba
->slot_info
= devm_kcalloc(dev
, max_command_entries
,
1937 sizeof(struct hisi_sas_slot
),
1939 if (!hisi_hba
->slot_info
)
1942 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1943 hisi_hba
->iost
= dma_alloc_coherent(dev
, s
, &hisi_hba
->iost_dma
,
1945 if (!hisi_hba
->iost
)
1948 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1949 hisi_hba
->breakpoint
= dma_alloc_coherent(dev
, s
,
1950 &hisi_hba
->breakpoint_dma
, GFP_KERNEL
);
1951 if (!hisi_hba
->breakpoint
)
1954 hisi_hba
->slot_index_count
= max_command_entries
;
1955 s
= hisi_hba
->slot_index_count
/ BITS_PER_BYTE
;
1956 hisi_hba
->slot_index_tags
= devm_kzalloc(dev
, s
, GFP_KERNEL
);
1957 if (!hisi_hba
->slot_index_tags
)
1960 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1961 hisi_hba
->initial_fis
= dma_alloc_coherent(dev
, s
,
1962 &hisi_hba
->initial_fis_dma
, GFP_KERNEL
);
1963 if (!hisi_hba
->initial_fis
)
1966 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1967 hisi_hba
->sata_breakpoint
= dma_alloc_coherent(dev
, s
,
1968 &hisi_hba
->sata_breakpoint_dma
, GFP_KERNEL
);
1969 if (!hisi_hba
->sata_breakpoint
)
1971 hisi_sas_init_mem(hisi_hba
);
1973 hisi_sas_slot_index_init(hisi_hba
);
1975 hisi_hba
->wq
= create_singlethread_workqueue(dev_name(dev
));
1976 if (!hisi_hba
->wq
) {
1977 dev_err(dev
, "sas_alloc: failed to create workqueue\n");
1985 EXPORT_SYMBOL_GPL(hisi_sas_alloc
);
1987 void hisi_sas_free(struct hisi_hba
*hisi_hba
)
1989 struct device
*dev
= hisi_hba
->dev
;
1990 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1992 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1993 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1994 if (hisi_hba
->cmd_hdr
[i
])
1995 dma_free_coherent(dev
, s
,
1996 hisi_hba
->cmd_hdr
[i
],
1997 hisi_hba
->cmd_hdr_dma
[i
]);
1999 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
2000 if (hisi_hba
->complete_hdr
[i
])
2001 dma_free_coherent(dev
, s
,
2002 hisi_hba
->complete_hdr
[i
],
2003 hisi_hba
->complete_hdr_dma
[i
]);
2006 dma_pool_destroy(hisi_hba
->buffer_pool
);
2008 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
2010 dma_free_coherent(dev
, s
,
2011 hisi_hba
->itct
, hisi_hba
->itct_dma
);
2013 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
2015 dma_free_coherent(dev
, s
,
2016 hisi_hba
->iost
, hisi_hba
->iost_dma
);
2018 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
2019 if (hisi_hba
->breakpoint
)
2020 dma_free_coherent(dev
, s
,
2021 hisi_hba
->breakpoint
,
2022 hisi_hba
->breakpoint_dma
);
2025 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
2026 if (hisi_hba
->initial_fis
)
2027 dma_free_coherent(dev
, s
,
2028 hisi_hba
->initial_fis
,
2029 hisi_hba
->initial_fis_dma
);
2031 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
2032 if (hisi_hba
->sata_breakpoint
)
2033 dma_free_coherent(dev
, s
,
2034 hisi_hba
->sata_breakpoint
,
2035 hisi_hba
->sata_breakpoint_dma
);
2038 destroy_workqueue(hisi_hba
->wq
);
2040 EXPORT_SYMBOL_GPL(hisi_sas_free
);
2042 void hisi_sas_rst_work_handler(struct work_struct
*work
)
2044 struct hisi_hba
*hisi_hba
=
2045 container_of(work
, struct hisi_hba
, rst_work
);
2047 hisi_sas_controller_reset(hisi_hba
);
2049 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler
);
2051 void hisi_sas_sync_rst_work_handler(struct work_struct
*work
)
2053 struct hisi_sas_rst
*rst
=
2054 container_of(work
, struct hisi_sas_rst
, work
);
2056 if (!hisi_sas_controller_reset(rst
->hisi_hba
))
2058 complete(rst
->completion
);
2060 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler
);
2062 int hisi_sas_get_fw_info(struct hisi_hba
*hisi_hba
)
2064 struct device
*dev
= hisi_hba
->dev
;
2065 struct platform_device
*pdev
= hisi_hba
->platform_dev
;
2066 struct device_node
*np
= pdev
? pdev
->dev
.of_node
: NULL
;
2069 if (device_property_read_u8_array(dev
, "sas-addr", hisi_hba
->sas_addr
,
2071 dev_err(dev
, "could not get property sas-addr\n");
2077 * These properties are only required for platform device-based
2078 * controller with DT firmware.
2080 hisi_hba
->ctrl
= syscon_regmap_lookup_by_phandle(np
,
2081 "hisilicon,sas-syscon");
2082 if (IS_ERR(hisi_hba
->ctrl
)) {
2083 dev_err(dev
, "could not get syscon\n");
2087 if (device_property_read_u32(dev
, "ctrl-reset-reg",
2088 &hisi_hba
->ctrl_reset_reg
)) {
2090 "could not get property ctrl-reset-reg\n");
2094 if (device_property_read_u32(dev
, "ctrl-reset-sts-reg",
2095 &hisi_hba
->ctrl_reset_sts_reg
)) {
2097 "could not get property ctrl-reset-sts-reg\n");
2101 if (device_property_read_u32(dev
, "ctrl-clock-ena-reg",
2102 &hisi_hba
->ctrl_clock_ena_reg
)) {
2104 "could not get property ctrl-clock-ena-reg\n");
2109 refclk
= devm_clk_get(dev
, NULL
);
2111 dev_dbg(dev
, "no ref clk property\n");
2113 hisi_hba
->refclk_frequency_mhz
= clk_get_rate(refclk
) / 1000000;
2115 if (device_property_read_u32(dev
, "phy-count", &hisi_hba
->n_phy
)) {
2116 dev_err(dev
, "could not get property phy-count\n");
2120 if (device_property_read_u32(dev
, "queue-count",
2121 &hisi_hba
->queue_count
)) {
2122 dev_err(dev
, "could not get property queue-count\n");
2128 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info
);
2130 static struct Scsi_Host
*hisi_sas_shost_alloc(struct platform_device
*pdev
,
2131 const struct hisi_sas_hw
*hw
)
2133 struct resource
*res
;
2134 struct Scsi_Host
*shost
;
2135 struct hisi_hba
*hisi_hba
;
2136 struct device
*dev
= &pdev
->dev
;
2138 shost
= scsi_host_alloc(hw
->sht
, sizeof(*hisi_hba
));
2140 dev_err(dev
, "scsi host alloc failed\n");
2143 hisi_hba
= shost_priv(shost
);
2145 INIT_WORK(&hisi_hba
->rst_work
, hisi_sas_rst_work_handler
);
2147 hisi_hba
->dev
= dev
;
2148 hisi_hba
->platform_dev
= pdev
;
2149 hisi_hba
->shost
= shost
;
2150 SHOST_TO_SAS_HA(shost
) = &hisi_hba
->sha
;
2152 timer_setup(&hisi_hba
->timer
, NULL
, 0);
2154 if (hisi_sas_get_fw_info(hisi_hba
) < 0)
2157 if (dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64)) &&
2158 dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32))) {
2159 dev_err(dev
, "No usable DMA addressing method\n");
2163 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2164 hisi_hba
->regs
= devm_ioremap_resource(dev
, res
);
2165 if (IS_ERR(hisi_hba
->regs
))
2168 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2170 hisi_hba
->sgpio_regs
= devm_ioremap_resource(dev
, res
);
2171 if (IS_ERR(hisi_hba
->sgpio_regs
))
2175 if (hisi_sas_alloc(hisi_hba
, shost
)) {
2176 hisi_sas_free(hisi_hba
);
2182 scsi_host_put(shost
);
2183 dev_err(dev
, "shost alloc failed\n");
2187 void hisi_sas_init_add(struct hisi_hba
*hisi_hba
)
2191 for (i
= 0; i
< hisi_hba
->n_phy
; i
++)
2192 memcpy(&hisi_hba
->phy
[i
].dev_sas_addr
,
2196 EXPORT_SYMBOL_GPL(hisi_sas_init_add
);
2198 int hisi_sas_probe(struct platform_device
*pdev
,
2199 const struct hisi_sas_hw
*hw
)
2201 struct Scsi_Host
*shost
;
2202 struct hisi_hba
*hisi_hba
;
2203 struct device
*dev
= &pdev
->dev
;
2204 struct asd_sas_phy
**arr_phy
;
2205 struct asd_sas_port
**arr_port
;
2206 struct sas_ha_struct
*sha
;
2207 int rc
, phy_nr
, port_nr
, i
;
2209 shost
= hisi_sas_shost_alloc(pdev
, hw
);
2213 sha
= SHOST_TO_SAS_HA(shost
);
2214 hisi_hba
= shost_priv(shost
);
2215 platform_set_drvdata(pdev
, sha
);
2217 phy_nr
= port_nr
= hisi_hba
->n_phy
;
2219 arr_phy
= devm_kcalloc(dev
, phy_nr
, sizeof(void *), GFP_KERNEL
);
2220 arr_port
= devm_kcalloc(dev
, port_nr
, sizeof(void *), GFP_KERNEL
);
2221 if (!arr_phy
|| !arr_port
) {
2226 sha
->sas_phy
= arr_phy
;
2227 sha
->sas_port
= arr_port
;
2228 sha
->lldd_ha
= hisi_hba
;
2230 shost
->transportt
= hisi_sas_stt
;
2231 shost
->max_id
= HISI_SAS_MAX_DEVICES
;
2232 shost
->max_lun
= ~0;
2233 shost
->max_channel
= 1;
2234 shost
->max_cmd_len
= 16;
2235 shost
->sg_tablesize
= min_t(u16
, SG_ALL
, HISI_SAS_SGE_PAGE_CNT
);
2236 shost
->can_queue
= hisi_hba
->hw
->max_command_entries
;
2237 shost
->cmd_per_lun
= hisi_hba
->hw
->max_command_entries
;
2239 sha
->sas_ha_name
= DRV_NAME
;
2240 sha
->dev
= hisi_hba
->dev
;
2241 sha
->lldd_module
= THIS_MODULE
;
2242 sha
->sas_addr
= &hisi_hba
->sas_addr
[0];
2243 sha
->num_phys
= hisi_hba
->n_phy
;
2244 sha
->core
.shost
= hisi_hba
->shost
;
2246 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
2247 sha
->sas_phy
[i
] = &hisi_hba
->phy
[i
].sas_phy
;
2248 sha
->sas_port
[i
] = &hisi_hba
->port
[i
].sas_port
;
2251 hisi_sas_init_add(hisi_hba
);
2253 rc
= scsi_add_host(shost
, &pdev
->dev
);
2257 rc
= sas_register_ha(sha
);
2259 goto err_out_register_ha
;
2261 rc
= hisi_hba
->hw
->hw_init(hisi_hba
);
2263 goto err_out_register_ha
;
2265 scsi_scan_host(shost
);
2269 err_out_register_ha
:
2270 scsi_remove_host(shost
);
2272 hisi_sas_free(hisi_hba
);
2273 scsi_host_put(shost
);
2276 EXPORT_SYMBOL_GPL(hisi_sas_probe
);
2278 int hisi_sas_remove(struct platform_device
*pdev
)
2280 struct sas_ha_struct
*sha
= platform_get_drvdata(pdev
);
2281 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
2282 struct Scsi_Host
*shost
= sha
->core
.shost
;
2284 if (timer_pending(&hisi_hba
->timer
))
2285 del_timer(&hisi_hba
->timer
);
2287 sas_unregister_ha(sha
);
2288 sas_remove_host(sha
->core
.shost
);
2290 hisi_sas_free(hisi_hba
);
2291 scsi_host_put(shost
);
2294 EXPORT_SYMBOL_GPL(hisi_sas_remove
);
2296 static __init
int hisi_sas_init(void)
2298 hisi_sas_stt
= sas_domain_attach_transport(&hisi_sas_transport_ops
);
2305 static __exit
void hisi_sas_exit(void)
2307 sas_release_transport(hisi_sas_stt
);
2310 module_init(hisi_sas_init
);
2311 module_exit(hisi_sas_exit
);
2313 MODULE_LICENSE("GPL");
2314 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2315 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2316 MODULE_ALIAS("platform:" DRV_NAME
);