2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
13 #define DRV_NAME "hisi_sas"
15 #define DEV_IS_GONE(dev) \
16 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
18 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device
*device
,
19 u8
*lun
, struct hisi_sas_tmf_task
*tmf
);
21 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
22 struct domain_device
*device
,
23 int abort_flag
, int tag
);
24 static int hisi_sas_softreset_ata_disk(struct domain_device
*device
);
25 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
28 u8
hisi_sas_get_ata_protocol(struct host_to_dev_fis
*fis
, int direction
)
30 switch (fis
->command
) {
31 case ATA_CMD_FPDMA_WRITE
:
32 case ATA_CMD_FPDMA_READ
:
33 case ATA_CMD_FPDMA_RECV
:
34 case ATA_CMD_FPDMA_SEND
:
35 case ATA_CMD_NCQ_NON_DATA
:
36 return HISI_SAS_SATA_PROTOCOL_FPDMA
;
38 case ATA_CMD_DOWNLOAD_MICRO
:
40 case ATA_CMD_PMP_READ
:
41 case ATA_CMD_READ_LOG_EXT
:
42 case ATA_CMD_PIO_READ
:
43 case ATA_CMD_PIO_READ_EXT
:
44 case ATA_CMD_PMP_WRITE
:
45 case ATA_CMD_WRITE_LOG_EXT
:
46 case ATA_CMD_PIO_WRITE
:
47 case ATA_CMD_PIO_WRITE_EXT
:
48 return HISI_SAS_SATA_PROTOCOL_PIO
;
51 case ATA_CMD_DOWNLOAD_MICRO_DMA
:
52 case ATA_CMD_PMP_READ_DMA
:
53 case ATA_CMD_PMP_WRITE_DMA
:
55 case ATA_CMD_READ_EXT
:
56 case ATA_CMD_READ_LOG_DMA_EXT
:
57 case ATA_CMD_READ_STREAM_DMA_EXT
:
58 case ATA_CMD_TRUSTED_RCV_DMA
:
59 case ATA_CMD_TRUSTED_SND_DMA
:
61 case ATA_CMD_WRITE_EXT
:
62 case ATA_CMD_WRITE_FUA_EXT
:
63 case ATA_CMD_WRITE_QUEUED
:
64 case ATA_CMD_WRITE_LOG_DMA_EXT
:
65 case ATA_CMD_WRITE_STREAM_DMA_EXT
:
66 case ATA_CMD_ZAC_MGMT_IN
:
67 return HISI_SAS_SATA_PROTOCOL_DMA
;
69 case ATA_CMD_CHK_POWER
:
70 case ATA_CMD_DEV_RESET
:
73 case ATA_CMD_FLUSH_EXT
:
75 case ATA_CMD_VERIFY_EXT
:
76 case ATA_CMD_SET_FEATURES
:
78 case ATA_CMD_STANDBYNOW1
:
79 case ATA_CMD_ZAC_MGMT_OUT
:
80 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
83 if (fis
->command
== ATA_CMD_SET_MAX
) {
84 switch (fis
->features
) {
85 case ATA_SET_MAX_PASSWD
:
86 case ATA_SET_MAX_LOCK
:
87 return HISI_SAS_SATA_PROTOCOL_PIO
;
89 case ATA_SET_MAX_PASSWD_DMA
:
90 case ATA_SET_MAX_UNLOCK_DMA
:
91 return HISI_SAS_SATA_PROTOCOL_DMA
;
94 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
97 if (direction
== DMA_NONE
)
98 return HISI_SAS_SATA_PROTOCOL_NONDATA
;
99 return HISI_SAS_SATA_PROTOCOL_PIO
;
103 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol
);
105 void hisi_sas_sata_done(struct sas_task
*task
,
106 struct hisi_sas_slot
*slot
)
108 struct task_status_struct
*ts
= &task
->task_status
;
109 struct ata_task_resp
*resp
= (struct ata_task_resp
*)ts
->buf
;
110 struct hisi_sas_status_buffer
*status_buf
=
111 hisi_sas_status_buf_addr_mem(slot
);
112 u8
*iu
= &status_buf
->iu
[0];
113 struct dev_to_host_fis
*d2h
= (struct dev_to_host_fis
*)iu
;
115 resp
->frame_len
= sizeof(struct dev_to_host_fis
);
116 memcpy(&resp
->ending_fis
[0], d2h
, sizeof(struct dev_to_host_fis
));
118 ts
->buf_valid_size
= sizeof(*resp
);
120 EXPORT_SYMBOL_GPL(hisi_sas_sata_done
);
122 int hisi_sas_get_ncq_tag(struct sas_task
*task
, u32
*tag
)
124 struct ata_queued_cmd
*qc
= task
->uldd_task
;
127 if (qc
->tf
.command
== ATA_CMD_FPDMA_WRITE
||
128 qc
->tf
.command
== ATA_CMD_FPDMA_READ
) {
135 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag
);
138 * This function assumes linkrate mask fits in 8 bits, which it
139 * does for all HW versions supported.
141 u8
hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max
)
146 max
-= SAS_LINK_RATE_1_5_GBPS
;
147 for (i
= 0; i
<= max
; i
++)
148 rate
|= 1 << (i
* 2);
151 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask
);
153 static struct hisi_hba
*dev_to_hisi_hba(struct domain_device
*device
)
155 return device
->port
->ha
->lldd_ha
;
158 struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
)
160 return container_of(sas_port
, struct hisi_sas_port
, sas_port
);
162 EXPORT_SYMBOL_GPL(to_hisi_sas_port
);
164 void hisi_sas_stop_phys(struct hisi_hba
*hisi_hba
)
168 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++)
169 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
171 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys
);
173 static void hisi_sas_slot_index_clear(struct hisi_hba
*hisi_hba
, int slot_idx
)
175 void *bitmap
= hisi_hba
->slot_index_tags
;
177 clear_bit(slot_idx
, bitmap
);
180 static void hisi_sas_slot_index_free(struct hisi_hba
*hisi_hba
, int slot_idx
)
182 hisi_sas_slot_index_clear(hisi_hba
, slot_idx
);
185 static void hisi_sas_slot_index_set(struct hisi_hba
*hisi_hba
, int slot_idx
)
187 void *bitmap
= hisi_hba
->slot_index_tags
;
189 set_bit(slot_idx
, bitmap
);
192 static int hisi_sas_slot_index_alloc(struct hisi_hba
*hisi_hba
, int *slot_idx
)
195 void *bitmap
= hisi_hba
->slot_index_tags
;
197 index
= find_first_zero_bit(bitmap
, hisi_hba
->slot_index_count
);
198 if (index
>= hisi_hba
->slot_index_count
)
199 return -SAS_QUEUE_FULL
;
200 hisi_sas_slot_index_set(hisi_hba
, index
);
205 static void hisi_sas_slot_index_init(struct hisi_hba
*hisi_hba
)
209 for (i
= 0; i
< hisi_hba
->slot_index_count
; ++i
)
210 hisi_sas_slot_index_clear(hisi_hba
, i
);
213 void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
214 struct hisi_sas_slot
*slot
)
218 struct device
*dev
= hisi_hba
->dev
;
220 if (!task
->lldd_task
)
223 task
->lldd_task
= NULL
;
225 if (!sas_protocol_ata(task
->task_proto
))
227 dma_unmap_sg(dev
, task
->scatter
,
233 dma_pool_free(hisi_hba
->buffer_pool
, slot
->buf
, slot
->buf_dma
);
235 list_del_init(&slot
->entry
);
239 hisi_sas_slot_index_free(hisi_hba
, slot
->idx
);
241 /* slot memory is fully zeroed when it is reused */
243 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free
);
245 static void hisi_sas_task_prep_smp(struct hisi_hba
*hisi_hba
,
246 struct hisi_sas_slot
*slot
)
248 hisi_hba
->hw
->prep_smp(hisi_hba
, slot
);
251 static void hisi_sas_task_prep_ssp(struct hisi_hba
*hisi_hba
,
252 struct hisi_sas_slot
*slot
, int is_tmf
,
253 struct hisi_sas_tmf_task
*tmf
)
255 hisi_hba
->hw
->prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
258 static void hisi_sas_task_prep_ata(struct hisi_hba
*hisi_hba
,
259 struct hisi_sas_slot
*slot
)
261 hisi_hba
->hw
->prep_stp(hisi_hba
, slot
);
264 static void hisi_sas_task_prep_abort(struct hisi_hba
*hisi_hba
,
265 struct hisi_sas_slot
*slot
,
266 int device_id
, int abort_flag
, int tag_to_abort
)
268 hisi_hba
->hw
->prep_abort(hisi_hba
, slot
,
269 device_id
, abort_flag
, tag_to_abort
);
273 * This function will issue an abort TMF regardless of whether the
274 * task is in the sdev or not. Then it will do the task complete
275 * cleanup and callbacks.
277 static void hisi_sas_slot_abort(struct work_struct
*work
)
279 struct hisi_sas_slot
*abort_slot
=
280 container_of(work
, struct hisi_sas_slot
, abort_slot
);
281 struct sas_task
*task
= abort_slot
->task
;
282 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
283 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
284 struct hisi_sas_tmf_task tmf_task
;
286 struct device
*dev
= hisi_hba
->dev
;
287 int tag
= abort_slot
->idx
;
290 if (!(task
->task_proto
& SAS_PROTOCOL_SSP
)) {
291 dev_err(dev
, "cannot abort slot for non-ssp task\n");
295 int_to_scsilun(cmnd
->device
->lun
, &lun
);
296 tmf_task
.tmf
= TMF_ABORT_TASK
;
297 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
299 hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
, &tmf_task
);
301 /* Do cleanup for this task */
302 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
303 hisi_sas_slot_task_free(hisi_hba
, task
, abort_slot
);
304 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
306 task
->task_done(task
);
309 static int hisi_sas_task_prep(struct sas_task
*task
, struct hisi_sas_dq
310 *dq
, int is_tmf
, struct hisi_sas_tmf_task
*tmf
,
313 struct hisi_hba
*hisi_hba
= dq
->hisi_hba
;
314 struct domain_device
*device
= task
->dev
;
315 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
316 struct hisi_sas_port
*port
;
317 struct hisi_sas_slot
*slot
;
318 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
319 struct asd_sas_port
*sas_port
= device
->port
;
320 struct device
*dev
= hisi_hba
->dev
;
321 int dlvry_queue_slot
, dlvry_queue
, rc
, slot_idx
;
322 int n_elem
= 0, n_elem_req
= 0, n_elem_resp
= 0;
326 struct task_status_struct
*ts
= &task
->task_status
;
328 ts
->resp
= SAS_TASK_UNDELIVERED
;
329 ts
->stat
= SAS_PHY_DOWN
;
331 * libsas will use dev->port, should
332 * not call task_done for sata
334 if (device
->dev_type
!= SAS_SATA_DEV
)
335 task
->task_done(task
);
339 if (DEV_IS_GONE(sas_dev
)) {
341 dev_info(dev
, "task prep: device %d not ready\n",
344 dev_info(dev
, "task prep: device %016llx not ready\n",
345 SAS_ADDR(device
->sas_addr
));
350 port
= to_hisi_sas_port(sas_port
);
351 if (port
&& !port
->port_attached
) {
352 dev_info(dev
, "task prep: %s port%d not attach device\n",
353 (dev_is_sata(device
)) ?
360 if (!sas_protocol_ata(task
->task_proto
)) {
361 unsigned int req_len
, resp_len
;
363 if (task
->num_scatter
) {
364 n_elem
= dma_map_sg(dev
, task
->scatter
,
365 task
->num_scatter
, task
->data_dir
);
370 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
371 n_elem_req
= dma_map_sg(dev
, &task
->smp_task
.smp_req
,
377 req_len
= sg_dma_len(&task
->smp_task
.smp_req
);
380 goto err_out_dma_unmap
;
382 n_elem_resp
= dma_map_sg(dev
, &task
->smp_task
.smp_resp
,
386 goto err_out_dma_unmap
;
388 resp_len
= sg_dma_len(&task
->smp_task
.smp_resp
);
389 if (resp_len
& 0x3) {
391 goto err_out_dma_unmap
;
395 n_elem
= task
->num_scatter
;
397 if (n_elem
> HISI_SAS_SGE_PAGE_CNT
) {
398 dev_err(dev
, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
401 goto err_out_dma_unmap
;
404 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
405 if (hisi_hba
->hw
->slot_index_alloc
)
406 rc
= hisi_hba
->hw
->slot_index_alloc(hisi_hba
, &slot_idx
,
409 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
410 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
412 goto err_out_dma_unmap
;
414 rc
= hisi_hba
->hw
->get_free_slot(hisi_hba
, dq
);
418 dlvry_queue
= dq
->id
;
419 dlvry_queue_slot
= dq
->wr_point
;
420 slot
= &hisi_hba
->slot_info
[slot_idx
];
421 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
423 slot
->idx
= slot_idx
;
424 slot
->n_elem
= n_elem
;
425 slot
->dlvry_queue
= dlvry_queue
;
426 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
427 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
428 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
432 slot
->is_internal
= true;
433 task
->lldd_task
= slot
;
434 INIT_WORK(&slot
->abort_slot
, hisi_sas_slot_abort
);
436 slot
->buf
= dma_pool_alloc(hisi_hba
->buffer_pool
,
437 GFP_ATOMIC
, &slot
->buf_dma
);
440 goto err_out_slot_buf
;
442 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
443 memset(hisi_sas_cmd_hdr_addr_mem(slot
), 0, HISI_SAS_COMMAND_TABLE_SZ
);
444 memset(hisi_sas_status_buf_addr_mem(slot
), 0, HISI_SAS_STATUS_BUF_SZ
);
446 switch (task
->task_proto
) {
447 case SAS_PROTOCOL_SMP
:
448 hisi_sas_task_prep_smp(hisi_hba
, slot
);
450 case SAS_PROTOCOL_SSP
:
451 hisi_sas_task_prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
453 case SAS_PROTOCOL_SATA
:
454 case SAS_PROTOCOL_STP
:
455 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:
456 hisi_sas_task_prep_ata(hisi_hba
, slot
);
459 dev_err(dev
, "task prep: unknown/unsupported proto (0x%x)\n",
464 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
465 list_add_tail(&slot
->entry
, &sas_dev
->list
);
466 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
467 spin_lock_irqsave(&task
->task_state_lock
, flags
);
468 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
469 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
471 dq
->slot_prep
= slot
;
477 /* Nothing to be done */
479 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
480 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
481 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
483 if (!sas_protocol_ata(task
->task_proto
)) {
484 if (task
->num_scatter
) {
485 dma_unmap_sg(dev
, task
->scatter
, task
->num_scatter
,
487 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
489 dma_unmap_sg(dev
, &task
->smp_task
.smp_req
,
492 dma_unmap_sg(dev
, &task
->smp_task
.smp_resp
,
497 dev_err(dev
, "task prep: failed[%d]!\n", rc
);
501 static int hisi_sas_task_exec(struct sas_task
*task
, gfp_t gfp_flags
,
502 int is_tmf
, struct hisi_sas_tmf_task
*tmf
)
507 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
508 struct device
*dev
= hisi_hba
->dev
;
509 struct domain_device
*device
= task
->dev
;
510 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
511 struct hisi_sas_dq
*dq
= sas_dev
->dq
;
513 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
)))
516 /* protect task_prep and start_delivery sequence */
517 spin_lock_irqsave(&dq
->lock
, flags
);
518 rc
= hisi_sas_task_prep(task
, dq
, is_tmf
, tmf
, &pass
);
520 dev_err(dev
, "task exec: failed[%d]!\n", rc
);
523 hisi_hba
->hw
->start_delivery(dq
);
524 spin_unlock_irqrestore(&dq
->lock
, flags
);
529 static void hisi_sas_bytes_dmaed(struct hisi_hba
*hisi_hba
, int phy_no
)
531 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
532 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
533 struct sas_ha_struct
*sas_ha
;
535 if (!phy
->phy_attached
)
538 sas_ha
= &hisi_hba
->sha
;
539 sas_ha
->notify_phy_event(sas_phy
, PHYE_OOB_DONE
);
542 struct sas_phy
*sphy
= sas_phy
->phy
;
544 sphy
->negotiated_linkrate
= sas_phy
->linkrate
;
545 sphy
->minimum_linkrate_hw
= SAS_LINK_RATE_1_5_GBPS
;
546 sphy
->maximum_linkrate_hw
=
547 hisi_hba
->hw
->phy_get_max_linkrate();
548 if (sphy
->minimum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
549 sphy
->minimum_linkrate
= phy
->minimum_linkrate
;
551 if (sphy
->maximum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
552 sphy
->maximum_linkrate
= phy
->maximum_linkrate
;
555 if (phy
->phy_type
& PORT_TYPE_SAS
) {
556 struct sas_identify_frame
*id
;
558 id
= (struct sas_identify_frame
*)phy
->frame_rcvd
;
559 id
->dev_type
= phy
->identify
.device_type
;
560 id
->initiator_bits
= SAS_PROTOCOL_ALL
;
561 id
->target_bits
= phy
->identify
.target_port_protocols
;
562 } else if (phy
->phy_type
& PORT_TYPE_SATA
) {
566 sas_phy
->frame_rcvd_size
= phy
->frame_rcvd_size
;
567 sas_ha
->notify_port_event(sas_phy
, PORTE_BYTES_DMAED
);
570 static struct hisi_sas_device
*hisi_sas_alloc_dev(struct domain_device
*device
)
572 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
573 struct hisi_sas_device
*sas_dev
= NULL
;
577 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
578 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
579 if (hisi_hba
->devices
[i
].dev_type
== SAS_PHY_UNUSED
) {
580 int queue
= i
% hisi_hba
->queue_count
;
581 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[queue
];
583 hisi_hba
->devices
[i
].device_id
= i
;
584 sas_dev
= &hisi_hba
->devices
[i
];
585 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
586 sas_dev
->dev_type
= device
->dev_type
;
587 sas_dev
->hisi_hba
= hisi_hba
;
588 sas_dev
->sas_device
= device
;
590 INIT_LIST_HEAD(&hisi_hba
->devices
[i
].list
);
594 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
599 static int hisi_sas_dev_found(struct domain_device
*device
)
601 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
602 struct domain_device
*parent_dev
= device
->parent
;
603 struct hisi_sas_device
*sas_dev
;
604 struct device
*dev
= hisi_hba
->dev
;
606 if (hisi_hba
->hw
->alloc_dev
)
607 sas_dev
= hisi_hba
->hw
->alloc_dev(device
);
609 sas_dev
= hisi_sas_alloc_dev(device
);
611 dev_err(dev
, "fail alloc dev: max support %d devices\n",
612 HISI_SAS_MAX_DEVICES
);
616 device
->lldd_dev
= sas_dev
;
617 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
619 if (parent_dev
&& DEV_IS_EXPANDER(parent_dev
->dev_type
)) {
621 u8 phy_num
= parent_dev
->ex_dev
.num_phys
;
624 for (phy_no
= 0; phy_no
< phy_num
; phy_no
++) {
625 phy
= &parent_dev
->ex_dev
.ex_phy
[phy_no
];
626 if (SAS_ADDR(phy
->attached_sas_addr
) ==
627 SAS_ADDR(device
->sas_addr
)) {
628 sas_dev
->attached_phy
= phy_no
;
633 if (phy_no
== phy_num
) {
634 dev_info(dev
, "dev found: no attached "
635 "dev:%016llx at ex:%016llx\n",
636 SAS_ADDR(device
->sas_addr
),
637 SAS_ADDR(parent_dev
->sas_addr
));
642 dev_info(dev
, "dev[%d:%x] found\n",
643 sas_dev
->device_id
, sas_dev
->dev_type
);
648 static int hisi_sas_slave_configure(struct scsi_device
*sdev
)
650 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
651 int ret
= sas_slave_configure(sdev
);
655 if (!dev_is_sata(dev
))
656 sas_change_queue_depth(sdev
, 64);
661 static void hisi_sas_scan_start(struct Scsi_Host
*shost
)
663 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
665 hisi_hba
->hw
->phys_init(hisi_hba
);
668 static int hisi_sas_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
670 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
671 struct sas_ha_struct
*sha
= &hisi_hba
->sha
;
673 /* Wait for PHY up interrupt to occur */
681 static void hisi_sas_phyup_work(struct work_struct
*work
)
683 struct hisi_sas_phy
*phy
=
684 container_of(work
, typeof(*phy
), works
[HISI_PHYE_PHY_UP
]);
685 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
686 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
687 int phy_no
= sas_phy
->id
;
689 hisi_hba
->hw
->sl_notify(hisi_hba
, phy_no
); /* This requires a sleep */
690 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
693 static void hisi_sas_linkreset_work(struct work_struct
*work
)
695 struct hisi_sas_phy
*phy
=
696 container_of(work
, typeof(*phy
), works
[HISI_PHYE_LINK_RESET
]);
697 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
699 hisi_sas_control_phy(sas_phy
, PHY_FUNC_LINK_RESET
, NULL
);
702 static const work_func_t hisi_sas_phye_fns
[HISI_PHYES_NUM
] = {
703 [HISI_PHYE_PHY_UP
] = hisi_sas_phyup_work
,
704 [HISI_PHYE_LINK_RESET
] = hisi_sas_linkreset_work
,
707 bool hisi_sas_notify_phy_event(struct hisi_sas_phy
*phy
,
708 enum hisi_sas_phy_event event
)
710 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
712 if (WARN_ON(event
>= HISI_PHYES_NUM
))
715 return queue_work(hisi_hba
->wq
, &phy
->works
[event
]);
717 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event
);
719 static void hisi_sas_phy_init(struct hisi_hba
*hisi_hba
, int phy_no
)
721 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
722 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
725 phy
->hisi_hba
= hisi_hba
;
727 phy
->minimum_linkrate
= SAS_LINK_RATE_1_5_GBPS
;
728 phy
->maximum_linkrate
= hisi_hba
->hw
->phy_get_max_linkrate();
729 sas_phy
->enabled
= (phy_no
< hisi_hba
->n_phy
) ? 1 : 0;
730 sas_phy
->class = SAS
;
731 sas_phy
->iproto
= SAS_PROTOCOL_ALL
;
733 sas_phy
->type
= PHY_TYPE_PHYSICAL
;
734 sas_phy
->role
= PHY_ROLE_INITIATOR
;
735 sas_phy
->oob_mode
= OOB_NOT_CONNECTED
;
736 sas_phy
->linkrate
= SAS_LINK_RATE_UNKNOWN
;
737 sas_phy
->id
= phy_no
;
738 sas_phy
->sas_addr
= &hisi_hba
->sas_addr
[0];
739 sas_phy
->frame_rcvd
= &phy
->frame_rcvd
[0];
740 sas_phy
->ha
= (struct sas_ha_struct
*)hisi_hba
->shost
->hostdata
;
741 sas_phy
->lldd_phy
= phy
;
743 for (i
= 0; i
< HISI_PHYES_NUM
; i
++)
744 INIT_WORK(&phy
->works
[i
], hisi_sas_phye_fns
[i
]);
747 static void hisi_sas_port_notify_formed(struct asd_sas_phy
*sas_phy
)
749 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
750 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
751 struct hisi_sas_phy
*phy
= sas_phy
->lldd_phy
;
752 struct asd_sas_port
*sas_port
= sas_phy
->port
;
753 struct hisi_sas_port
*port
= to_hisi_sas_port(sas_port
);
759 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
760 port
->port_attached
= 1;
761 port
->id
= phy
->port_id
;
763 sas_port
->lldd_port
= port
;
764 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
767 static void hisi_sas_do_release_task(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
768 struct hisi_sas_slot
*slot
)
772 struct task_status_struct
*ts
;
774 ts
= &task
->task_status
;
776 ts
->resp
= SAS_TASK_COMPLETE
;
777 ts
->stat
= SAS_ABORTED_TASK
;
778 spin_lock_irqsave(&task
->task_state_lock
, flags
);
779 task
->task_state_flags
&=
780 ~(SAS_TASK_STATE_PENDING
| SAS_TASK_AT_INITIATOR
);
781 task
->task_state_flags
|= SAS_TASK_STATE_DONE
;
782 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
785 hisi_sas_slot_task_free(hisi_hba
, task
, slot
);
788 /* hisi_hba.lock should be locked */
789 static void hisi_sas_release_task(struct hisi_hba
*hisi_hba
,
790 struct domain_device
*device
)
792 struct hisi_sas_slot
*slot
, *slot2
;
793 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
795 list_for_each_entry_safe(slot
, slot2
, &sas_dev
->list
, entry
)
796 hisi_sas_do_release_task(hisi_hba
, slot
->task
, slot
);
799 void hisi_sas_release_tasks(struct hisi_hba
*hisi_hba
)
801 struct hisi_sas_device
*sas_dev
;
802 struct domain_device
*device
;
805 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
806 sas_dev
= &hisi_hba
->devices
[i
];
807 device
= sas_dev
->sas_device
;
809 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
) ||
813 hisi_sas_release_task(hisi_hba
, device
);
816 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks
);
818 static void hisi_sas_dereg_device(struct hisi_hba
*hisi_hba
,
819 struct domain_device
*device
)
821 if (hisi_hba
->hw
->dereg_device
)
822 hisi_hba
->hw
->dereg_device(hisi_hba
, device
);
825 static void hisi_sas_dev_gone(struct domain_device
*device
)
827 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
828 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
829 struct device
*dev
= hisi_hba
->dev
;
831 dev_info(dev
, "dev[%d:%x] is gone\n",
832 sas_dev
->device_id
, sas_dev
->dev_type
);
834 if (!test_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
)) {
835 hisi_sas_internal_task_abort(hisi_hba
, device
,
836 HISI_SAS_INT_ABT_DEV
, 0);
838 hisi_sas_dereg_device(hisi_hba
, device
);
840 hisi_hba
->hw
->clear_itct(hisi_hba
, sas_dev
);
841 device
->lldd_dev
= NULL
;
844 if (hisi_hba
->hw
->free_device
)
845 hisi_hba
->hw
->free_device(sas_dev
);
846 sas_dev
->dev_type
= SAS_PHY_UNUSED
;
849 static int hisi_sas_queue_command(struct sas_task
*task
, gfp_t gfp_flags
)
851 return hisi_sas_task_exec(task
, gfp_flags
, 0, NULL
);
854 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
857 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
858 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
859 int phy_no
= sas_phy
->id
;
862 case PHY_FUNC_HARD_RESET
:
863 hisi_hba
->hw
->phy_hard_reset(hisi_hba
, phy_no
);
866 case PHY_FUNC_LINK_RESET
:
867 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
869 hisi_hba
->hw
->phy_start(hisi_hba
, phy_no
);
872 case PHY_FUNC_DISABLE
:
873 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
876 case PHY_FUNC_SET_LINK_RATE
:
877 hisi_hba
->hw
->phy_set_linkrate(hisi_hba
, phy_no
, funcdata
);
879 case PHY_FUNC_GET_EVENTS
:
880 if (hisi_hba
->hw
->get_events
) {
881 hisi_hba
->hw
->get_events(hisi_hba
, phy_no
);
885 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
892 static void hisi_sas_task_done(struct sas_task
*task
)
894 if (!del_timer(&task
->slow_task
->timer
))
896 complete(&task
->slow_task
->completion
);
899 static void hisi_sas_tmf_timedout(struct timer_list
*t
)
901 struct sas_task_slow
*slow
= from_timer(slow
, t
, timer
);
902 struct sas_task
*task
= slow
->task
;
905 spin_lock_irqsave(&task
->task_state_lock
, flags
);
906 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
907 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
908 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
910 complete(&task
->slow_task
->completion
);
913 #define TASK_TIMEOUT 20
915 #define INTERNAL_ABORT_TIMEOUT 6
916 static int hisi_sas_exec_internal_tmf_task(struct domain_device
*device
,
917 void *parameter
, u32 para_len
,
918 struct hisi_sas_tmf_task
*tmf
)
920 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
921 struct hisi_hba
*hisi_hba
= sas_dev
->hisi_hba
;
922 struct device
*dev
= hisi_hba
->dev
;
923 struct sas_task
*task
;
926 for (retry
= 0; retry
< TASK_RETRY
; retry
++) {
927 task
= sas_alloc_slow_task(GFP_KERNEL
);
932 task
->task_proto
= device
->tproto
;
934 if (dev_is_sata(device
)) {
935 task
->ata_task
.device_control_reg_update
= 1;
936 memcpy(&task
->ata_task
.fis
, parameter
, para_len
);
938 memcpy(&task
->ssp_task
, parameter
, para_len
);
940 task
->task_done
= hisi_sas_task_done
;
942 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
943 task
->slow_task
->timer
.expires
= jiffies
+ TASK_TIMEOUT
*HZ
;
944 add_timer(&task
->slow_task
->timer
);
946 res
= hisi_sas_task_exec(task
, GFP_KERNEL
, 1, tmf
);
949 del_timer(&task
->slow_task
->timer
);
950 dev_err(dev
, "abort tmf: executing internal task failed: %d\n",
955 wait_for_completion(&task
->slow_task
->completion
);
956 res
= TMF_RESP_FUNC_FAILED
;
957 /* Even TMF timed out, return direct. */
958 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
959 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
960 struct hisi_sas_slot
*slot
= task
->lldd_task
;
962 dev_err(dev
, "abort tmf: TMF task timeout and not done\n");
968 dev_err(dev
, "abort tmf: TMF task timeout\n");
971 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
972 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
973 res
= TMF_RESP_FUNC_COMPLETE
;
977 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
978 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
979 res
= TMF_RESP_FUNC_SUCC
;
983 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
984 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
985 /* no error, but return the number of bytes of
988 dev_warn(dev
, "abort tmf: task to dev %016llx "
989 "resp: 0x%x sts 0x%x underrun\n",
990 SAS_ADDR(device
->sas_addr
),
991 task
->task_status
.resp
,
992 task
->task_status
.stat
);
993 res
= task
->task_status
.residual
;
997 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
998 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
999 dev_warn(dev
, "abort tmf: blocked task error\n");
1004 dev_warn(dev
, "abort tmf: task to dev "
1005 "%016llx resp: 0x%x status 0x%x\n",
1006 SAS_ADDR(device
->sas_addr
), task
->task_status
.resp
,
1007 task
->task_status
.stat
);
1008 sas_free_task(task
);
1012 if (retry
== TASK_RETRY
)
1013 dev_warn(dev
, "abort tmf: executing internal task failed!\n");
1014 sas_free_task(task
);
1018 static void hisi_sas_fill_ata_reset_cmd(struct ata_device
*dev
,
1019 bool reset
, int pmp
, u8
*fis
)
1021 struct ata_taskfile tf
;
1023 ata_tf_init(dev
, &tf
);
1027 tf
.ctl
&= ~ATA_SRST
;
1028 tf
.command
= ATA_CMD_DEV_RESET
;
1029 ata_tf_to_fis(&tf
, pmp
, 0, fis
);
1032 static int hisi_sas_softreset_ata_disk(struct domain_device
*device
)
1035 struct ata_port
*ap
= device
->sata_dev
.ap
;
1036 struct ata_link
*link
;
1037 int rc
= TMF_RESP_FUNC_FAILED
;
1038 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1039 struct device
*dev
= hisi_hba
->dev
;
1040 int s
= sizeof(struct host_to_dev_fis
);
1041 unsigned long flags
;
1043 ata_for_each_link(link
, ap
, EDGE
) {
1044 int pmp
= sata_srst_pmp(link
);
1046 hisi_sas_fill_ata_reset_cmd(link
->device
, 1, pmp
, fis
);
1047 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
, s
, NULL
);
1048 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1052 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1053 ata_for_each_link(link
, ap
, EDGE
) {
1054 int pmp
= sata_srst_pmp(link
);
1056 hisi_sas_fill_ata_reset_cmd(link
->device
, 0, pmp
, fis
);
1057 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
,
1059 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1060 dev_err(dev
, "ata disk de-reset failed\n");
1063 dev_err(dev
, "ata disk reset failed\n");
1066 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1067 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1068 hisi_sas_release_task(hisi_hba
, device
);
1069 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1075 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device
*device
,
1076 u8
*lun
, struct hisi_sas_tmf_task
*tmf
)
1078 struct sas_ssp_task ssp_task
;
1080 if (!(device
->tproto
& SAS_PROTOCOL_SSP
))
1081 return TMF_RESP_FUNC_ESUPP
;
1083 memcpy(ssp_task
.LUN
, lun
, 8);
1085 return hisi_sas_exec_internal_tmf_task(device
, &ssp_task
,
1086 sizeof(ssp_task
), tmf
);
1089 static void hisi_sas_refresh_port_id(struct hisi_hba
*hisi_hba
)
1091 u32 state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1094 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1095 struct hisi_sas_device
*sas_dev
= &hisi_hba
->devices
[i
];
1096 struct domain_device
*device
= sas_dev
->sas_device
;
1097 struct asd_sas_port
*sas_port
;
1098 struct hisi_sas_port
*port
;
1099 struct hisi_sas_phy
*phy
= NULL
;
1100 struct asd_sas_phy
*sas_phy
;
1102 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
)
1103 || !device
|| !device
->port
)
1106 sas_port
= device
->port
;
1107 port
= to_hisi_sas_port(sas_port
);
1109 list_for_each_entry(sas_phy
, &sas_port
->phy_list
, port_phy_el
)
1110 if (state
& BIT(sas_phy
->id
)) {
1111 phy
= sas_phy
->lldd_phy
;
1116 port
->id
= phy
->port_id
;
1118 /* Update linkrate of directly attached device. */
1119 if (!device
->parent
)
1120 device
->linkrate
= phy
->sas_phy
.linkrate
;
1122 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
1128 static void hisi_sas_rescan_topology(struct hisi_hba
*hisi_hba
, u32 old_state
,
1131 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1132 struct asd_sas_port
*_sas_port
= NULL
;
1135 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++) {
1136 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1137 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1138 struct asd_sas_port
*sas_port
= sas_phy
->port
;
1139 bool do_port_check
= !!(_sas_port
!= sas_port
);
1141 if (!sas_phy
->phy
->enabled
)
1144 /* Report PHY state change to libsas */
1145 if (state
& BIT(phy_no
)) {
1146 if (do_port_check
&& sas_port
&& sas_port
->port_dev
) {
1147 struct domain_device
*dev
= sas_port
->port_dev
;
1149 _sas_port
= sas_port
;
1151 if (DEV_IS_EXPANDER(dev
->dev_type
))
1152 sas_ha
->notify_port_event(sas_phy
,
1153 PORTE_BROADCAST_RCVD
);
1155 } else if (old_state
& (1 << phy_no
))
1156 /* PHY down but was up before */
1157 hisi_sas_phy_down(hisi_hba
, phy_no
, 0);
1162 static int hisi_sas_controller_reset(struct hisi_hba
*hisi_hba
)
1164 struct device
*dev
= hisi_hba
->dev
;
1165 struct Scsi_Host
*shost
= hisi_hba
->shost
;
1166 u32 old_state
, state
;
1167 unsigned long flags
;
1170 if (!hisi_hba
->hw
->soft_reset
)
1173 if (test_and_set_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
))
1176 dev_info(dev
, "controller resetting...\n");
1177 old_state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1179 scsi_block_requests(shost
);
1180 if (timer_pending(&hisi_hba
->timer
))
1181 del_timer_sync(&hisi_hba
->timer
);
1183 set_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1184 rc
= hisi_hba
->hw
->soft_reset(hisi_hba
);
1186 dev_warn(dev
, "controller reset failed (%d)\n", rc
);
1187 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1188 scsi_unblock_requests(shost
);
1191 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1192 hisi_sas_release_tasks(hisi_hba
);
1193 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1195 clear_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
);
1197 /* Init and wait for PHYs to come up and all libsas event finished. */
1198 hisi_hba
->hw
->phys_init(hisi_hba
);
1200 hisi_sas_refresh_port_id(hisi_hba
);
1201 scsi_unblock_requests(shost
);
1203 state
= hisi_hba
->hw
->get_phys_state(hisi_hba
);
1204 hisi_sas_rescan_topology(hisi_hba
, old_state
, state
);
1205 dev_info(dev
, "controller reset complete\n");
1208 clear_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
);
1213 static int hisi_sas_abort_task(struct sas_task
*task
)
1215 struct scsi_lun lun
;
1216 struct hisi_sas_tmf_task tmf_task
;
1217 struct domain_device
*device
= task
->dev
;
1218 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1219 struct hisi_hba
*hisi_hba
;
1221 int rc
= TMF_RESP_FUNC_FAILED
;
1222 unsigned long flags
;
1225 return TMF_RESP_FUNC_FAILED
;
1227 hisi_hba
= dev_to_hisi_hba(task
->dev
);
1228 dev
= hisi_hba
->dev
;
1230 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1231 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
1232 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1233 rc
= TMF_RESP_FUNC_COMPLETE
;
1236 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
1237 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1239 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1240 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1241 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1242 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1243 u32 tag
= slot
->idx
;
1246 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1247 tmf_task
.tmf
= TMF_ABORT_TASK
;
1248 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1250 rc
= hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
,
1253 rc2
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1254 HISI_SAS_INT_ABT_CMD
, tag
);
1256 dev_err(dev
, "abort task: internal abort (%d)\n", rc2
);
1257 return TMF_RESP_FUNC_FAILED
;
1261 * If the TMF finds that the IO is not in the device and also
1262 * the internal abort does not succeed, then it is safe to
1264 * Note: if the internal abort succeeds then the slot
1265 * will have already been completed
1267 if (rc
== TMF_RESP_FUNC_COMPLETE
&& rc2
!= TMF_RESP_FUNC_SUCC
) {
1268 if (task
->lldd_task
) {
1269 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1270 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1271 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1274 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
1275 task
->task_proto
& SAS_PROTOCOL_STP
) {
1276 if (task
->dev
->dev_type
== SAS_SATA_DEV
) {
1277 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1278 HISI_SAS_INT_ABT_DEV
, 0);
1280 dev_err(dev
, "abort task: internal abort failed\n");
1283 hisi_sas_dereg_device(hisi_hba
, device
);
1284 rc
= hisi_sas_softreset_ata_disk(device
);
1286 } else if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SMP
) {
1288 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1289 u32 tag
= slot
->idx
;
1291 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1292 HISI_SAS_INT_ABT_CMD
, tag
);
1293 if (((rc
< 0) || (rc
== TMF_RESP_FUNC_FAILED
)) &&
1295 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1296 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
1297 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1302 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1303 dev_notice(dev
, "abort task: rc=%d\n", rc
);
1307 static int hisi_sas_abort_task_set(struct domain_device
*device
, u8
*lun
)
1309 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1310 struct device
*dev
= hisi_hba
->dev
;
1311 struct hisi_sas_tmf_task tmf_task
;
1312 int rc
= TMF_RESP_FUNC_FAILED
;
1313 unsigned long flags
;
1315 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1316 HISI_SAS_INT_ABT_DEV
, 0);
1318 dev_err(dev
, "abort task set: internal abort rc=%d\n", rc
);
1319 return TMF_RESP_FUNC_FAILED
;
1321 hisi_sas_dereg_device(hisi_hba
, device
);
1323 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
1324 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1326 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1327 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1328 hisi_sas_release_task(hisi_hba
, device
);
1329 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1335 static int hisi_sas_clear_aca(struct domain_device
*device
, u8
*lun
)
1337 int rc
= TMF_RESP_FUNC_FAILED
;
1338 struct hisi_sas_tmf_task tmf_task
;
1340 tmf_task
.tmf
= TMF_CLEAR_ACA
;
1341 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1346 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device
*device
)
1348 struct sas_phy
*phy
= sas_get_local_phy(device
);
1349 int rc
, reset_type
= (device
->dev_type
== SAS_SATA_DEV
||
1350 (device
->tproto
& SAS_PROTOCOL_STP
)) ? 0 : 1;
1351 rc
= sas_phy_reset(phy
, reset_type
);
1352 sas_put_local_phy(phy
);
1357 static int hisi_sas_I_T_nexus_reset(struct domain_device
*device
)
1359 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1360 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1361 struct device
*dev
= hisi_hba
->dev
;
1362 int rc
= TMF_RESP_FUNC_FAILED
;
1363 unsigned long flags
;
1365 if (sas_dev
->dev_status
!= HISI_SAS_DEV_EH
)
1366 return TMF_RESP_FUNC_FAILED
;
1367 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
1369 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1370 HISI_SAS_INT_ABT_DEV
, 0);
1372 dev_err(dev
, "I_T nexus reset: internal abort (%d)\n", rc
);
1373 return TMF_RESP_FUNC_FAILED
;
1375 hisi_sas_dereg_device(hisi_hba
, device
);
1377 rc
= hisi_sas_debug_I_T_nexus_reset(device
);
1379 if ((rc
== TMF_RESP_FUNC_COMPLETE
) || (rc
== -ENODEV
)) {
1380 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1381 hisi_sas_release_task(hisi_hba
, device
);
1382 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1387 static int hisi_sas_lu_reset(struct domain_device
*device
, u8
*lun
)
1389 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1390 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1391 struct device
*dev
= hisi_hba
->dev
;
1392 unsigned long flags
;
1393 int rc
= TMF_RESP_FUNC_FAILED
;
1395 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1396 if (dev_is_sata(device
)) {
1397 struct sas_phy
*phy
;
1399 /* Clear internal IO and then hardreset */
1400 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1401 HISI_SAS_INT_ABT_DEV
, 0);
1403 dev_err(dev
, "lu_reset: internal abort failed\n");
1406 hisi_sas_dereg_device(hisi_hba
, device
);
1408 phy
= sas_get_local_phy(device
);
1410 rc
= sas_phy_reset(phy
, 1);
1413 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1414 hisi_sas_release_task(hisi_hba
, device
);
1415 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1417 sas_put_local_phy(phy
);
1419 struct hisi_sas_tmf_task tmf_task
= { .tmf
= TMF_LU_RESET
};
1421 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1422 HISI_SAS_INT_ABT_DEV
, 0);
1424 dev_err(dev
, "lu_reset: internal abort failed\n");
1427 hisi_sas_dereg_device(hisi_hba
, device
);
1429 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1430 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1431 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1432 hisi_sas_release_task(hisi_hba
, device
);
1433 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1437 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1438 dev_err(dev
, "lu_reset: for device[%d]:rc= %d\n",
1439 sas_dev
->device_id
, rc
);
1443 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct
*sas_ha
)
1445 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
1446 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r
);
1448 queue_work(hisi_hba
->wq
, &r
.work
);
1449 wait_for_completion(r
.completion
);
1451 return TMF_RESP_FUNC_COMPLETE
;
1453 return TMF_RESP_FUNC_FAILED
;
1456 static int hisi_sas_query_task(struct sas_task
*task
)
1458 struct scsi_lun lun
;
1459 struct hisi_sas_tmf_task tmf_task
;
1460 int rc
= TMF_RESP_FUNC_FAILED
;
1462 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1463 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1464 struct domain_device
*device
= task
->dev
;
1465 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1466 u32 tag
= slot
->idx
;
1468 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1469 tmf_task
.tmf
= TMF_QUERY_TASK
;
1470 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1472 rc
= hisi_sas_debug_issue_ssp_tmf(device
,
1476 /* The task is still in Lun, release it then */
1477 case TMF_RESP_FUNC_SUCC
:
1478 /* The task is not in Lun or failed, reset the phy */
1479 case TMF_RESP_FUNC_FAILED
:
1480 case TMF_RESP_FUNC_COMPLETE
:
1483 rc
= TMF_RESP_FUNC_FAILED
;
1491 hisi_sas_internal_abort_task_exec(struct hisi_hba
*hisi_hba
, int device_id
,
1492 struct sas_task
*task
, int abort_flag
,
1495 struct domain_device
*device
= task
->dev
;
1496 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1497 struct device
*dev
= hisi_hba
->dev
;
1498 struct hisi_sas_port
*port
;
1499 struct hisi_sas_slot
*slot
;
1500 struct asd_sas_port
*sas_port
= device
->port
;
1501 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
1502 struct hisi_sas_dq
*dq
= sas_dev
->dq
;
1503 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
1504 unsigned long flags
, flags_dq
;
1506 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT
, &hisi_hba
->flags
)))
1512 port
= to_hisi_sas_port(sas_port
);
1514 /* simply get a slot and send abort command */
1515 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1516 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
1518 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1521 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1523 spin_lock_irqsave(&dq
->lock
, flags_dq
);
1524 rc
= hisi_hba
->hw
->get_free_slot(hisi_hba
, dq
);
1528 dlvry_queue
= dq
->id
;
1529 dlvry_queue_slot
= dq
->wr_point
;
1531 slot
= &hisi_hba
->slot_info
[slot_idx
];
1532 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
1534 slot
->idx
= slot_idx
;
1535 slot
->n_elem
= n_elem
;
1536 slot
->dlvry_queue
= dlvry_queue
;
1537 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
1538 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
1539 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
1542 slot
->is_internal
= true;
1543 task
->lldd_task
= slot
;
1545 slot
->buf
= dma_pool_alloc(hisi_hba
->buffer_pool
,
1546 GFP_ATOMIC
, &slot
->buf_dma
);
1552 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
1553 memset(hisi_sas_cmd_hdr_addr_mem(slot
), 0, HISI_SAS_COMMAND_TABLE_SZ
);
1554 memset(hisi_sas_status_buf_addr_mem(slot
), 0, HISI_SAS_STATUS_BUF_SZ
);
1556 hisi_sas_task_prep_abort(hisi_hba
, slot
, device_id
,
1557 abort_flag
, task_tag
);
1559 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1560 list_add_tail(&slot
->entry
, &sas_dev
->list
);
1561 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1562 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1563 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
1564 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1566 dq
->slot_prep
= slot
;
1568 /* send abort command to the chip */
1569 hisi_hba
->hw
->start_delivery(dq
);
1570 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
1575 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1576 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
1577 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1578 spin_unlock_irqrestore(&dq
->lock
, flags_dq
);
1580 dev_err(dev
, "internal abort task prep: failed[%d]!\n", rc
);
1586 * hisi_sas_internal_task_abort -- execute an internal
1587 * abort command for single IO command or a device
1588 * @hisi_hba: host controller struct
1589 * @device: domain device
1590 * @abort_flag: mode of operation, device or single IO
1591 * @tag: tag of IO to be aborted (only relevant to single
1595 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
1596 struct domain_device
*device
,
1597 int abort_flag
, int tag
)
1599 struct sas_task
*task
;
1600 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1601 struct device
*dev
= hisi_hba
->dev
;
1605 * The interface is not realized means this HW don't support internal
1606 * abort, or don't need to do internal abort. Then here, we return
1607 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1608 * the internal abort has been executed and returned CQ.
1610 if (!hisi_hba
->hw
->prep_abort
)
1611 return TMF_RESP_FUNC_FAILED
;
1613 task
= sas_alloc_slow_task(GFP_KERNEL
);
1618 task
->task_proto
= device
->tproto
;
1619 task
->task_done
= hisi_sas_task_done
;
1620 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
1621 task
->slow_task
->timer
.expires
= jiffies
+ INTERNAL_ABORT_TIMEOUT
*HZ
;
1622 add_timer(&task
->slow_task
->timer
);
1624 res
= hisi_sas_internal_abort_task_exec(hisi_hba
, sas_dev
->device_id
,
1625 task
, abort_flag
, tag
);
1627 del_timer(&task
->slow_task
->timer
);
1628 dev_err(dev
, "internal task abort: executing internal task failed: %d\n",
1632 wait_for_completion(&task
->slow_task
->completion
);
1633 res
= TMF_RESP_FUNC_FAILED
;
1635 /* Internal abort timed out */
1636 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
1637 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
1638 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1642 dev_err(dev
, "internal task abort: timeout and not done.\n");
1646 dev_err(dev
, "internal task abort: timeout.\n");
1649 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1650 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
1651 res
= TMF_RESP_FUNC_COMPLETE
;
1655 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1656 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
1657 res
= TMF_RESP_FUNC_SUCC
;
1662 dev_dbg(dev
, "internal task abort: task to dev %016llx task=%p "
1663 "resp: 0x%x sts 0x%x\n",
1664 SAS_ADDR(device
->sas_addr
),
1666 task
->task_status
.resp
, /* 0 is complete, -1 is undelivered */
1667 task
->task_status
.stat
);
1668 sas_free_task(task
);
1673 static void hisi_sas_port_formed(struct asd_sas_phy
*sas_phy
)
1675 hisi_sas_port_notify_formed(sas_phy
);
1678 static void hisi_sas_port_deformed(struct asd_sas_phy
*sas_phy
)
1682 static int hisi_sas_write_gpio(struct sas_ha_struct
*sha
, u8 reg_type
,
1683 u8 reg_index
, u8 reg_count
, u8
*write_data
)
1685 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
1687 if (!hisi_hba
->hw
->write_gpio
)
1690 return hisi_hba
->hw
->write_gpio(hisi_hba
, reg_type
,
1691 reg_index
, reg_count
, write_data
);
1694 static void hisi_sas_phy_disconnected(struct hisi_sas_phy
*phy
)
1696 phy
->phy_attached
= 0;
1701 void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
)
1703 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1704 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1705 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1708 /* Phy down but ready */
1709 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
1710 hisi_sas_port_notify_formed(sas_phy
);
1712 struct hisi_sas_port
*port
= phy
->port
;
1714 /* Phy down and not ready */
1715 sas_ha
->notify_phy_event(sas_phy
, PHYE_LOSS_OF_SIGNAL
);
1716 sas_phy_disconnected(sas_phy
);
1719 if (phy
->phy_type
& PORT_TYPE_SAS
) {
1720 int port_id
= port
->id
;
1722 if (!hisi_hba
->hw
->get_wideport_bitmap(hisi_hba
,
1724 port
->port_attached
= 0;
1725 } else if (phy
->phy_type
& PORT_TYPE_SATA
)
1726 port
->port_attached
= 0;
1728 hisi_sas_phy_disconnected(phy
);
1731 EXPORT_SYMBOL_GPL(hisi_sas_phy_down
);
1733 void hisi_sas_kill_tasklets(struct hisi_hba
*hisi_hba
)
1737 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1738 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1740 tasklet_kill(&cq
->tasklet
);
1743 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets
);
1745 struct scsi_transport_template
*hisi_sas_stt
;
1746 EXPORT_SYMBOL_GPL(hisi_sas_stt
);
1748 static struct device_attribute
*host_attrs
[] = {
1749 &dev_attr_phy_event_threshold
,
1753 static struct scsi_host_template _hisi_sas_sht
= {
1754 .module
= THIS_MODULE
,
1756 .queuecommand
= sas_queuecommand
,
1757 .target_alloc
= sas_target_alloc
,
1758 .slave_configure
= hisi_sas_slave_configure
,
1759 .scan_finished
= hisi_sas_scan_finished
,
1760 .scan_start
= hisi_sas_scan_start
,
1761 .change_queue_depth
= sas_change_queue_depth
,
1762 .bios_param
= sas_bios_param
,
1765 .sg_tablesize
= SG_ALL
,
1766 .max_sectors
= SCSI_DEFAULT_MAX_SECTORS
,
1767 .use_clustering
= ENABLE_CLUSTERING
,
1768 .eh_device_reset_handler
= sas_eh_device_reset_handler
,
1769 .eh_target_reset_handler
= sas_eh_target_reset_handler
,
1770 .target_destroy
= sas_target_destroy
,
1772 .shost_attrs
= host_attrs
,
1774 struct scsi_host_template
*hisi_sas_sht
= &_hisi_sas_sht
;
1775 EXPORT_SYMBOL_GPL(hisi_sas_sht
);
1777 static struct sas_domain_function_template hisi_sas_transport_ops
= {
1778 .lldd_dev_found
= hisi_sas_dev_found
,
1779 .lldd_dev_gone
= hisi_sas_dev_gone
,
1780 .lldd_execute_task
= hisi_sas_queue_command
,
1781 .lldd_control_phy
= hisi_sas_control_phy
,
1782 .lldd_abort_task
= hisi_sas_abort_task
,
1783 .lldd_abort_task_set
= hisi_sas_abort_task_set
,
1784 .lldd_clear_aca
= hisi_sas_clear_aca
,
1785 .lldd_I_T_nexus_reset
= hisi_sas_I_T_nexus_reset
,
1786 .lldd_lu_reset
= hisi_sas_lu_reset
,
1787 .lldd_query_task
= hisi_sas_query_task
,
1788 .lldd_clear_nexus_ha
= hisi_sas_clear_nexus_ha
,
1789 .lldd_port_formed
= hisi_sas_port_formed
,
1790 .lldd_port_deformed
= hisi_sas_port_deformed
,
1791 .lldd_write_gpio
= hisi_sas_write_gpio
,
1794 void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
)
1796 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1798 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1799 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1800 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1802 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1803 memset(hisi_hba
->cmd_hdr
[i
], 0, s
);
1806 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1807 memset(hisi_hba
->complete_hdr
[i
], 0, s
);
1811 s
= sizeof(struct hisi_sas_initial_fis
) * hisi_hba
->n_phy
;
1812 memset(hisi_hba
->initial_fis
, 0, s
);
1814 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1815 memset(hisi_hba
->iost
, 0, s
);
1817 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1818 memset(hisi_hba
->breakpoint
, 0, s
);
1820 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1821 memset(hisi_hba
->sata_breakpoint
, 0, s
);
1823 EXPORT_SYMBOL_GPL(hisi_sas_init_mem
);
1825 int hisi_sas_alloc(struct hisi_hba
*hisi_hba
, struct Scsi_Host
*shost
)
1827 struct device
*dev
= hisi_hba
->dev
;
1828 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1830 spin_lock_init(&hisi_hba
->lock
);
1831 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1832 hisi_sas_phy_init(hisi_hba
, i
);
1833 hisi_hba
->port
[i
].port_attached
= 0;
1834 hisi_hba
->port
[i
].id
= -1;
1837 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1838 hisi_hba
->devices
[i
].dev_type
= SAS_PHY_UNUSED
;
1839 hisi_hba
->devices
[i
].device_id
= i
;
1840 hisi_hba
->devices
[i
].dev_status
= HISI_SAS_DEV_NORMAL
;
1843 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1844 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1845 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1847 /* Completion queue structure */
1849 cq
->hisi_hba
= hisi_hba
;
1851 /* Delivery queue structure */
1852 spin_lock_init(&dq
->lock
);
1854 dq
->hisi_hba
= hisi_hba
;
1856 /* Delivery queue */
1857 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1858 hisi_hba
->cmd_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1859 &hisi_hba
->cmd_hdr_dma
[i
], GFP_KERNEL
);
1860 if (!hisi_hba
->cmd_hdr
[i
])
1863 /* Completion queue */
1864 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1865 hisi_hba
->complete_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1866 &hisi_hba
->complete_hdr_dma
[i
], GFP_KERNEL
);
1867 if (!hisi_hba
->complete_hdr
[i
])
1871 s
= sizeof(struct hisi_sas_slot_buf_table
);
1872 hisi_hba
->buffer_pool
= dma_pool_create("dma_buffer", dev
, s
, 16, 0);
1873 if (!hisi_hba
->buffer_pool
)
1876 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1877 hisi_hba
->itct
= dma_alloc_coherent(dev
, s
, &hisi_hba
->itct_dma
,
1879 if (!hisi_hba
->itct
)
1882 memset(hisi_hba
->itct
, 0, s
);
1884 hisi_hba
->slot_info
= devm_kcalloc(dev
, max_command_entries
,
1885 sizeof(struct hisi_sas_slot
),
1887 if (!hisi_hba
->slot_info
)
1890 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1891 hisi_hba
->iost
= dma_alloc_coherent(dev
, s
, &hisi_hba
->iost_dma
,
1893 if (!hisi_hba
->iost
)
1896 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1897 hisi_hba
->breakpoint
= dma_alloc_coherent(dev
, s
,
1898 &hisi_hba
->breakpoint_dma
, GFP_KERNEL
);
1899 if (!hisi_hba
->breakpoint
)
1902 hisi_hba
->slot_index_count
= max_command_entries
;
1903 s
= hisi_hba
->slot_index_count
/ BITS_PER_BYTE
;
1904 hisi_hba
->slot_index_tags
= devm_kzalloc(dev
, s
, GFP_KERNEL
);
1905 if (!hisi_hba
->slot_index_tags
)
1908 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1909 hisi_hba
->initial_fis
= dma_alloc_coherent(dev
, s
,
1910 &hisi_hba
->initial_fis_dma
, GFP_KERNEL
);
1911 if (!hisi_hba
->initial_fis
)
1914 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1915 hisi_hba
->sata_breakpoint
= dma_alloc_coherent(dev
, s
,
1916 &hisi_hba
->sata_breakpoint_dma
, GFP_KERNEL
);
1917 if (!hisi_hba
->sata_breakpoint
)
1919 hisi_sas_init_mem(hisi_hba
);
1921 hisi_sas_slot_index_init(hisi_hba
);
1923 hisi_hba
->wq
= create_singlethread_workqueue(dev_name(dev
));
1924 if (!hisi_hba
->wq
) {
1925 dev_err(dev
, "sas_alloc: failed to create workqueue\n");
1933 EXPORT_SYMBOL_GPL(hisi_sas_alloc
);
1935 void hisi_sas_free(struct hisi_hba
*hisi_hba
)
1937 struct device
*dev
= hisi_hba
->dev
;
1938 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1940 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1941 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1942 if (hisi_hba
->cmd_hdr
[i
])
1943 dma_free_coherent(dev
, s
,
1944 hisi_hba
->cmd_hdr
[i
],
1945 hisi_hba
->cmd_hdr_dma
[i
]);
1947 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1948 if (hisi_hba
->complete_hdr
[i
])
1949 dma_free_coherent(dev
, s
,
1950 hisi_hba
->complete_hdr
[i
],
1951 hisi_hba
->complete_hdr_dma
[i
]);
1954 dma_pool_destroy(hisi_hba
->buffer_pool
);
1956 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1958 dma_free_coherent(dev
, s
,
1959 hisi_hba
->itct
, hisi_hba
->itct_dma
);
1961 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1963 dma_free_coherent(dev
, s
,
1964 hisi_hba
->iost
, hisi_hba
->iost_dma
);
1966 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1967 if (hisi_hba
->breakpoint
)
1968 dma_free_coherent(dev
, s
,
1969 hisi_hba
->breakpoint
,
1970 hisi_hba
->breakpoint_dma
);
1973 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1974 if (hisi_hba
->initial_fis
)
1975 dma_free_coherent(dev
, s
,
1976 hisi_hba
->initial_fis
,
1977 hisi_hba
->initial_fis_dma
);
1979 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_sata_breakpoint
);
1980 if (hisi_hba
->sata_breakpoint
)
1981 dma_free_coherent(dev
, s
,
1982 hisi_hba
->sata_breakpoint
,
1983 hisi_hba
->sata_breakpoint_dma
);
1986 destroy_workqueue(hisi_hba
->wq
);
1988 EXPORT_SYMBOL_GPL(hisi_sas_free
);
1990 void hisi_sas_rst_work_handler(struct work_struct
*work
)
1992 struct hisi_hba
*hisi_hba
=
1993 container_of(work
, struct hisi_hba
, rst_work
);
1995 hisi_sas_controller_reset(hisi_hba
);
1997 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler
);
1999 void hisi_sas_sync_rst_work_handler(struct work_struct
*work
)
2001 struct hisi_sas_rst
*rst
=
2002 container_of(work
, struct hisi_sas_rst
, work
);
2004 if (!hisi_sas_controller_reset(rst
->hisi_hba
))
2006 complete(rst
->completion
);
2008 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler
);
2010 int hisi_sas_get_fw_info(struct hisi_hba
*hisi_hba
)
2012 struct device
*dev
= hisi_hba
->dev
;
2013 struct platform_device
*pdev
= hisi_hba
->platform_dev
;
2014 struct device_node
*np
= pdev
? pdev
->dev
.of_node
: NULL
;
2017 if (device_property_read_u8_array(dev
, "sas-addr", hisi_hba
->sas_addr
,
2019 dev_err(dev
, "could not get property sas-addr\n");
2025 * These properties are only required for platform device-based
2026 * controller with DT firmware.
2028 hisi_hba
->ctrl
= syscon_regmap_lookup_by_phandle(np
,
2029 "hisilicon,sas-syscon");
2030 if (IS_ERR(hisi_hba
->ctrl
)) {
2031 dev_err(dev
, "could not get syscon\n");
2035 if (device_property_read_u32(dev
, "ctrl-reset-reg",
2036 &hisi_hba
->ctrl_reset_reg
)) {
2038 "could not get property ctrl-reset-reg\n");
2042 if (device_property_read_u32(dev
, "ctrl-reset-sts-reg",
2043 &hisi_hba
->ctrl_reset_sts_reg
)) {
2045 "could not get property ctrl-reset-sts-reg\n");
2049 if (device_property_read_u32(dev
, "ctrl-clock-ena-reg",
2050 &hisi_hba
->ctrl_clock_ena_reg
)) {
2052 "could not get property ctrl-clock-ena-reg\n");
2057 refclk
= devm_clk_get(dev
, NULL
);
2059 dev_dbg(dev
, "no ref clk property\n");
2061 hisi_hba
->refclk_frequency_mhz
= clk_get_rate(refclk
) / 1000000;
2063 if (device_property_read_u32(dev
, "phy-count", &hisi_hba
->n_phy
)) {
2064 dev_err(dev
, "could not get property phy-count\n");
2068 if (device_property_read_u32(dev
, "queue-count",
2069 &hisi_hba
->queue_count
)) {
2070 dev_err(dev
, "could not get property queue-count\n");
2076 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info
);
2078 static struct Scsi_Host
*hisi_sas_shost_alloc(struct platform_device
*pdev
,
2079 const struct hisi_sas_hw
*hw
)
2081 struct resource
*res
;
2082 struct Scsi_Host
*shost
;
2083 struct hisi_hba
*hisi_hba
;
2084 struct device
*dev
= &pdev
->dev
;
2086 shost
= scsi_host_alloc(hisi_sas_sht
, sizeof(*hisi_hba
));
2088 dev_err(dev
, "scsi host alloc failed\n");
2091 hisi_hba
= shost_priv(shost
);
2093 INIT_WORK(&hisi_hba
->rst_work
, hisi_sas_rst_work_handler
);
2095 hisi_hba
->dev
= dev
;
2096 hisi_hba
->platform_dev
= pdev
;
2097 hisi_hba
->shost
= shost
;
2098 SHOST_TO_SAS_HA(shost
) = &hisi_hba
->sha
;
2100 timer_setup(&hisi_hba
->timer
, NULL
, 0);
2102 if (hisi_sas_get_fw_info(hisi_hba
) < 0)
2105 if (dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64)) &&
2106 dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32))) {
2107 dev_err(dev
, "No usable DMA addressing method\n");
2111 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2112 hisi_hba
->regs
= devm_ioremap_resource(dev
, res
);
2113 if (IS_ERR(hisi_hba
->regs
))
2116 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2118 hisi_hba
->sgpio_regs
= devm_ioremap_resource(dev
, res
);
2119 if (IS_ERR(hisi_hba
->sgpio_regs
))
2123 if (hisi_sas_alloc(hisi_hba
, shost
)) {
2124 hisi_sas_free(hisi_hba
);
2130 scsi_host_put(shost
);
2131 dev_err(dev
, "shost alloc failed\n");
2135 void hisi_sas_init_add(struct hisi_hba
*hisi_hba
)
2139 for (i
= 0; i
< hisi_hba
->n_phy
; i
++)
2140 memcpy(&hisi_hba
->phy
[i
].dev_sas_addr
,
2144 EXPORT_SYMBOL_GPL(hisi_sas_init_add
);
2146 int hisi_sas_probe(struct platform_device
*pdev
,
2147 const struct hisi_sas_hw
*hw
)
2149 struct Scsi_Host
*shost
;
2150 struct hisi_hba
*hisi_hba
;
2151 struct device
*dev
= &pdev
->dev
;
2152 struct asd_sas_phy
**arr_phy
;
2153 struct asd_sas_port
**arr_port
;
2154 struct sas_ha_struct
*sha
;
2155 int rc
, phy_nr
, port_nr
, i
;
2157 shost
= hisi_sas_shost_alloc(pdev
, hw
);
2161 sha
= SHOST_TO_SAS_HA(shost
);
2162 hisi_hba
= shost_priv(shost
);
2163 platform_set_drvdata(pdev
, sha
);
2165 phy_nr
= port_nr
= hisi_hba
->n_phy
;
2167 arr_phy
= devm_kcalloc(dev
, phy_nr
, sizeof(void *), GFP_KERNEL
);
2168 arr_port
= devm_kcalloc(dev
, port_nr
, sizeof(void *), GFP_KERNEL
);
2169 if (!arr_phy
|| !arr_port
) {
2174 sha
->sas_phy
= arr_phy
;
2175 sha
->sas_port
= arr_port
;
2176 sha
->lldd_ha
= hisi_hba
;
2178 shost
->transportt
= hisi_sas_stt
;
2179 shost
->max_id
= HISI_SAS_MAX_DEVICES
;
2180 shost
->max_lun
= ~0;
2181 shost
->max_channel
= 1;
2182 shost
->max_cmd_len
= 16;
2183 shost
->sg_tablesize
= min_t(u16
, SG_ALL
, HISI_SAS_SGE_PAGE_CNT
);
2184 shost
->can_queue
= hisi_hba
->hw
->max_command_entries
;
2185 shost
->cmd_per_lun
= hisi_hba
->hw
->max_command_entries
;
2187 sha
->sas_ha_name
= DRV_NAME
;
2188 sha
->dev
= hisi_hba
->dev
;
2189 sha
->lldd_module
= THIS_MODULE
;
2190 sha
->sas_addr
= &hisi_hba
->sas_addr
[0];
2191 sha
->num_phys
= hisi_hba
->n_phy
;
2192 sha
->core
.shost
= hisi_hba
->shost
;
2194 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
2195 sha
->sas_phy
[i
] = &hisi_hba
->phy
[i
].sas_phy
;
2196 sha
->sas_port
[i
] = &hisi_hba
->port
[i
].sas_port
;
2199 hisi_sas_init_add(hisi_hba
);
2201 rc
= scsi_add_host(shost
, &pdev
->dev
);
2205 rc
= sas_register_ha(sha
);
2207 goto err_out_register_ha
;
2209 rc
= hisi_hba
->hw
->hw_init(hisi_hba
);
2211 goto err_out_register_ha
;
2213 scsi_scan_host(shost
);
2217 err_out_register_ha
:
2218 scsi_remove_host(shost
);
2220 hisi_sas_free(hisi_hba
);
2221 scsi_host_put(shost
);
2224 EXPORT_SYMBOL_GPL(hisi_sas_probe
);
2226 int hisi_sas_remove(struct platform_device
*pdev
)
2228 struct sas_ha_struct
*sha
= platform_get_drvdata(pdev
);
2229 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
2230 struct Scsi_Host
*shost
= sha
->core
.shost
;
2232 if (timer_pending(&hisi_hba
->timer
))
2233 del_timer(&hisi_hba
->timer
);
2235 sas_unregister_ha(sha
);
2236 sas_remove_host(sha
->core
.shost
);
2238 hisi_sas_free(hisi_hba
);
2239 scsi_host_put(shost
);
2242 EXPORT_SYMBOL_GPL(hisi_sas_remove
);
2244 static __init
int hisi_sas_init(void)
2246 hisi_sas_stt
= sas_domain_attach_transport(&hisi_sas_transport_ops
);
2253 static __exit
void hisi_sas_exit(void)
2255 sas_release_transport(hisi_sas_stt
);
2258 module_init(hisi_sas_init
);
2259 module_exit(hisi_sas_exit
);
2261 MODULE_LICENSE("GPL");
2262 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2263 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2264 MODULE_ALIAS("platform:" DRV_NAME
);