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
);
26 static struct hisi_hba
*dev_to_hisi_hba(struct domain_device
*device
)
28 return device
->port
->ha
->lldd_ha
;
31 struct hisi_sas_port
*to_hisi_sas_port(struct asd_sas_port
*sas_port
)
33 return container_of(sas_port
, struct hisi_sas_port
, sas_port
);
35 EXPORT_SYMBOL_GPL(to_hisi_sas_port
);
37 static void hisi_sas_slot_index_clear(struct hisi_hba
*hisi_hba
, int slot_idx
)
39 void *bitmap
= hisi_hba
->slot_index_tags
;
41 clear_bit(slot_idx
, bitmap
);
44 static void hisi_sas_slot_index_free(struct hisi_hba
*hisi_hba
, int slot_idx
)
46 hisi_sas_slot_index_clear(hisi_hba
, slot_idx
);
49 static void hisi_sas_slot_index_set(struct hisi_hba
*hisi_hba
, int slot_idx
)
51 void *bitmap
= hisi_hba
->slot_index_tags
;
53 set_bit(slot_idx
, bitmap
);
56 static int hisi_sas_slot_index_alloc(struct hisi_hba
*hisi_hba
, int *slot_idx
)
59 void *bitmap
= hisi_hba
->slot_index_tags
;
61 index
= find_first_zero_bit(bitmap
, hisi_hba
->slot_index_count
);
62 if (index
>= hisi_hba
->slot_index_count
)
63 return -SAS_QUEUE_FULL
;
64 hisi_sas_slot_index_set(hisi_hba
, index
);
69 static void hisi_sas_slot_index_init(struct hisi_hba
*hisi_hba
)
73 for (i
= 0; i
< hisi_hba
->slot_index_count
; ++i
)
74 hisi_sas_slot_index_clear(hisi_hba
, i
);
77 void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
78 struct hisi_sas_slot
*slot
)
82 struct device
*dev
= &hisi_hba
->pdev
->dev
;
83 struct domain_device
*device
= task
->dev
;
84 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
86 if (!sas_protocol_ata(task
->task_proto
))
88 dma_unmap_sg(dev
, task
->scatter
, slot
->n_elem
,
91 task
->lldd_task
= NULL
;
94 atomic64_dec(&sas_dev
->running_req
);
97 if (slot
->command_table
)
98 dma_pool_free(hisi_hba
->command_table_pool
,
99 slot
->command_table
, slot
->command_table_dma
);
101 if (slot
->status_buffer
)
102 dma_pool_free(hisi_hba
->status_buffer_pool
,
103 slot
->status_buffer
, slot
->status_buffer_dma
);
106 dma_pool_free(hisi_hba
->sge_page_pool
, slot
->sge_page
,
109 list_del_init(&slot
->entry
);
112 hisi_sas_slot_index_free(hisi_hba
, slot
->idx
);
114 /* slot memory is fully zeroed when it is reused */
116 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free
);
118 static int hisi_sas_task_prep_smp(struct hisi_hba
*hisi_hba
,
119 struct hisi_sas_slot
*slot
)
121 return hisi_hba
->hw
->prep_smp(hisi_hba
, slot
);
124 static int hisi_sas_task_prep_ssp(struct hisi_hba
*hisi_hba
,
125 struct hisi_sas_slot
*slot
, int is_tmf
,
126 struct hisi_sas_tmf_task
*tmf
)
128 return hisi_hba
->hw
->prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
131 static int hisi_sas_task_prep_ata(struct hisi_hba
*hisi_hba
,
132 struct hisi_sas_slot
*slot
)
134 return hisi_hba
->hw
->prep_stp(hisi_hba
, slot
);
137 static int hisi_sas_task_prep_abort(struct hisi_hba
*hisi_hba
,
138 struct hisi_sas_slot
*slot
,
139 int device_id
, int abort_flag
, int tag_to_abort
)
141 return hisi_hba
->hw
->prep_abort(hisi_hba
, slot
,
142 device_id
, abort_flag
, tag_to_abort
);
146 * This function will issue an abort TMF regardless of whether the
147 * task is in the sdev or not. Then it will do the task complete
148 * cleanup and callbacks.
150 static void hisi_sas_slot_abort(struct work_struct
*work
)
152 struct hisi_sas_slot
*abort_slot
=
153 container_of(work
, struct hisi_sas_slot
, abort_slot
);
154 struct sas_task
*task
= abort_slot
->task
;
155 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
156 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
157 struct hisi_sas_tmf_task tmf_task
;
159 struct device
*dev
= &hisi_hba
->pdev
->dev
;
160 int tag
= abort_slot
->idx
;
163 if (!(task
->task_proto
& SAS_PROTOCOL_SSP
)) {
164 dev_err(dev
, "cannot abort slot for non-ssp task\n");
168 int_to_scsilun(cmnd
->device
->lun
, &lun
);
169 tmf_task
.tmf
= TMF_ABORT_TASK
;
170 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
172 hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
, &tmf_task
);
174 /* Do cleanup for this task */
175 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
176 hisi_sas_slot_task_free(hisi_hba
, task
, abort_slot
);
177 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
179 task
->task_done(task
);
182 static int hisi_sas_task_prep(struct sas_task
*task
, struct hisi_hba
*hisi_hba
,
183 int is_tmf
, struct hisi_sas_tmf_task
*tmf
,
186 struct domain_device
*device
= task
->dev
;
187 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
188 struct hisi_sas_port
*port
;
189 struct hisi_sas_slot
*slot
;
190 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
191 struct asd_sas_port
*sas_port
= device
->port
;
192 struct device
*dev
= &hisi_hba
->pdev
->dev
;
193 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
197 struct task_status_struct
*ts
= &task
->task_status
;
199 ts
->resp
= SAS_TASK_UNDELIVERED
;
200 ts
->stat
= SAS_PHY_DOWN
;
202 * libsas will use dev->port, should
203 * not call task_done for sata
205 if (device
->dev_type
!= SAS_SATA_DEV
)
206 task
->task_done(task
);
210 if (DEV_IS_GONE(sas_dev
)) {
212 dev_info(dev
, "task prep: device %llu not ready\n",
215 dev_info(dev
, "task prep: device %016llx not ready\n",
216 SAS_ADDR(device
->sas_addr
));
221 port
= to_hisi_sas_port(sas_port
);
222 if (port
&& !port
->port_attached
) {
223 dev_info(dev
, "task prep: %s port%d not attach device\n",
224 (dev_is_sata(device
)) ?
231 if (!sas_protocol_ata(task
->task_proto
)) {
232 if (task
->num_scatter
) {
233 n_elem
= dma_map_sg(dev
, task
->scatter
,
234 task
->num_scatter
, task
->data_dir
);
241 n_elem
= task
->num_scatter
;
243 if (hisi_hba
->hw
->slot_index_alloc
)
244 rc
= hisi_hba
->hw
->slot_index_alloc(hisi_hba
, &slot_idx
,
247 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
250 rc
= hisi_hba
->hw
->get_free_slot(hisi_hba
, sas_dev
->device_id
,
251 &dlvry_queue
, &dlvry_queue_slot
);
255 slot
= &hisi_hba
->slot_info
[slot_idx
];
256 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
258 slot
->idx
= slot_idx
;
259 slot
->n_elem
= n_elem
;
260 slot
->dlvry_queue
= dlvry_queue
;
261 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
262 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
263 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
266 task
->lldd_task
= slot
;
267 INIT_WORK(&slot
->abort_slot
, hisi_sas_slot_abort
);
269 slot
->status_buffer
= dma_pool_alloc(hisi_hba
->status_buffer_pool
,
271 &slot
->status_buffer_dma
);
272 if (!slot
->status_buffer
) {
274 goto err_out_slot_buf
;
276 memset(slot
->status_buffer
, 0, HISI_SAS_STATUS_BUF_SZ
);
278 slot
->command_table
= dma_pool_alloc(hisi_hba
->command_table_pool
,
280 &slot
->command_table_dma
);
281 if (!slot
->command_table
) {
283 goto err_out_status_buf
;
285 memset(slot
->command_table
, 0, HISI_SAS_COMMAND_TABLE_SZ
);
286 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
288 switch (task
->task_proto
) {
289 case SAS_PROTOCOL_SMP
:
290 rc
= hisi_sas_task_prep_smp(hisi_hba
, slot
);
292 case SAS_PROTOCOL_SSP
:
293 rc
= hisi_sas_task_prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
295 case SAS_PROTOCOL_SATA
:
296 case SAS_PROTOCOL_STP
:
297 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:
298 rc
= hisi_sas_task_prep_ata(hisi_hba
, slot
);
301 dev_err(dev
, "task prep: unknown/unsupported proto (0x%x)\n",
308 dev_err(dev
, "task prep: rc = 0x%x\n", rc
);
311 goto err_out_command_table
;
314 list_add_tail(&slot
->entry
, &sas_dev
->list
);
315 spin_lock_irqsave(&task
->task_state_lock
, flags
);
316 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
317 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
319 hisi_hba
->slot_prep
= slot
;
321 atomic64_inc(&sas_dev
->running_req
);
327 dma_pool_free(hisi_hba
->sge_page_pool
, slot
->sge_page
,
329 err_out_command_table
:
330 dma_pool_free(hisi_hba
->command_table_pool
, slot
->command_table
,
331 slot
->command_table_dma
);
333 dma_pool_free(hisi_hba
->status_buffer_pool
, slot
->status_buffer
,
334 slot
->status_buffer_dma
);
336 /* Nothing to be done */
338 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
340 dev_err(dev
, "task prep: failed[%d]!\n", rc
);
341 if (!sas_protocol_ata(task
->task_proto
))
343 dma_unmap_sg(dev
, task
->scatter
, n_elem
,
349 static int hisi_sas_task_exec(struct sas_task
*task
, gfp_t gfp_flags
,
350 int is_tmf
, struct hisi_sas_tmf_task
*tmf
)
355 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
356 struct device
*dev
= &hisi_hba
->pdev
->dev
;
358 if (unlikely(test_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
)))
361 /* protect task_prep and start_delivery sequence */
362 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
363 rc
= hisi_sas_task_prep(task
, hisi_hba
, is_tmf
, tmf
, &pass
);
365 dev_err(dev
, "task exec: failed[%d]!\n", rc
);
368 hisi_hba
->hw
->start_delivery(hisi_hba
);
369 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
374 static void hisi_sas_bytes_dmaed(struct hisi_hba
*hisi_hba
, int phy_no
)
376 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
377 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
378 struct sas_ha_struct
*sas_ha
;
380 if (!phy
->phy_attached
)
383 sas_ha
= &hisi_hba
->sha
;
384 sas_ha
->notify_phy_event(sas_phy
, PHYE_OOB_DONE
);
387 struct sas_phy
*sphy
= sas_phy
->phy
;
389 sphy
->negotiated_linkrate
= sas_phy
->linkrate
;
390 sphy
->minimum_linkrate_hw
= SAS_LINK_RATE_1_5_GBPS
;
391 sphy
->maximum_linkrate_hw
=
392 hisi_hba
->hw
->phy_get_max_linkrate();
393 if (sphy
->minimum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
394 sphy
->minimum_linkrate
= phy
->minimum_linkrate
;
396 if (sphy
->maximum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
397 sphy
->maximum_linkrate
= phy
->maximum_linkrate
;
400 if (phy
->phy_type
& PORT_TYPE_SAS
) {
401 struct sas_identify_frame
*id
;
403 id
= (struct sas_identify_frame
*)phy
->frame_rcvd
;
404 id
->dev_type
= phy
->identify
.device_type
;
405 id
->initiator_bits
= SAS_PROTOCOL_ALL
;
406 id
->target_bits
= phy
->identify
.target_port_protocols
;
407 } else if (phy
->phy_type
& PORT_TYPE_SATA
) {
411 sas_phy
->frame_rcvd_size
= phy
->frame_rcvd_size
;
412 sas_ha
->notify_port_event(sas_phy
, PORTE_BYTES_DMAED
);
415 static struct hisi_sas_device
*hisi_sas_alloc_dev(struct domain_device
*device
)
417 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
418 struct hisi_sas_device
*sas_dev
= NULL
;
421 spin_lock(&hisi_hba
->lock
);
422 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
423 if (hisi_hba
->devices
[i
].dev_type
== SAS_PHY_UNUSED
) {
424 hisi_hba
->devices
[i
].device_id
= i
;
425 sas_dev
= &hisi_hba
->devices
[i
];
426 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
427 sas_dev
->dev_type
= device
->dev_type
;
428 sas_dev
->hisi_hba
= hisi_hba
;
429 sas_dev
->sas_device
= device
;
430 INIT_LIST_HEAD(&hisi_hba
->devices
[i
].list
);
434 spin_unlock(&hisi_hba
->lock
);
439 static int hisi_sas_dev_found(struct domain_device
*device
)
441 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
442 struct domain_device
*parent_dev
= device
->parent
;
443 struct hisi_sas_device
*sas_dev
;
444 struct device
*dev
= &hisi_hba
->pdev
->dev
;
446 if (hisi_hba
->hw
->alloc_dev
)
447 sas_dev
= hisi_hba
->hw
->alloc_dev(device
);
449 sas_dev
= hisi_sas_alloc_dev(device
);
451 dev_err(dev
, "fail alloc dev: max support %d devices\n",
452 HISI_SAS_MAX_DEVICES
);
456 device
->lldd_dev
= sas_dev
;
457 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
459 if (parent_dev
&& DEV_IS_EXPANDER(parent_dev
->dev_type
)) {
461 u8 phy_num
= parent_dev
->ex_dev
.num_phys
;
464 for (phy_no
= 0; phy_no
< phy_num
; phy_no
++) {
465 phy
= &parent_dev
->ex_dev
.ex_phy
[phy_no
];
466 if (SAS_ADDR(phy
->attached_sas_addr
) ==
467 SAS_ADDR(device
->sas_addr
)) {
468 sas_dev
->attached_phy
= phy_no
;
473 if (phy_no
== phy_num
) {
474 dev_info(dev
, "dev found: no attached "
475 "dev:%016llx at ex:%016llx\n",
476 SAS_ADDR(device
->sas_addr
),
477 SAS_ADDR(parent_dev
->sas_addr
));
485 static int hisi_sas_slave_configure(struct scsi_device
*sdev
)
487 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
488 int ret
= sas_slave_configure(sdev
);
492 if (!dev_is_sata(dev
))
493 sas_change_queue_depth(sdev
, 64);
498 static void hisi_sas_scan_start(struct Scsi_Host
*shost
)
500 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
502 hisi_hba
->hw
->phys_init(hisi_hba
);
505 static int hisi_sas_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
507 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
508 struct sas_ha_struct
*sha
= &hisi_hba
->sha
;
510 /* Wait for PHY up interrupt to occur */
518 static void hisi_sas_phyup_work(struct work_struct
*work
)
520 struct hisi_sas_phy
*phy
=
521 container_of(work
, struct hisi_sas_phy
, phyup_ws
);
522 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
523 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
524 int phy_no
= sas_phy
->id
;
526 hisi_hba
->hw
->sl_notify(hisi_hba
, phy_no
); /* This requires a sleep */
527 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
530 static void hisi_sas_phy_init(struct hisi_hba
*hisi_hba
, int phy_no
)
532 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
533 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
535 phy
->hisi_hba
= hisi_hba
;
537 init_timer(&phy
->timer
);
538 sas_phy
->enabled
= (phy_no
< hisi_hba
->n_phy
) ? 1 : 0;
539 sas_phy
->class = SAS
;
540 sas_phy
->iproto
= SAS_PROTOCOL_ALL
;
542 sas_phy
->type
= PHY_TYPE_PHYSICAL
;
543 sas_phy
->role
= PHY_ROLE_INITIATOR
;
544 sas_phy
->oob_mode
= OOB_NOT_CONNECTED
;
545 sas_phy
->linkrate
= SAS_LINK_RATE_UNKNOWN
;
546 sas_phy
->id
= phy_no
;
547 sas_phy
->sas_addr
= &hisi_hba
->sas_addr
[0];
548 sas_phy
->frame_rcvd
= &phy
->frame_rcvd
[0];
549 sas_phy
->ha
= (struct sas_ha_struct
*)hisi_hba
->shost
->hostdata
;
550 sas_phy
->lldd_phy
= phy
;
552 INIT_WORK(&phy
->phyup_ws
, hisi_sas_phyup_work
);
555 static void hisi_sas_port_notify_formed(struct asd_sas_phy
*sas_phy
)
557 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
558 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
559 struct hisi_sas_phy
*phy
= sas_phy
->lldd_phy
;
560 struct asd_sas_port
*sas_port
= sas_phy
->port
;
561 struct hisi_sas_port
*port
= to_hisi_sas_port(sas_port
);
567 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
568 port
->port_attached
= 1;
569 port
->id
= phy
->port_id
;
571 sas_port
->lldd_port
= port
;
572 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
575 static void hisi_sas_do_release_task(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
576 struct hisi_sas_slot
*slot
)
580 struct task_status_struct
*ts
;
582 ts
= &task
->task_status
;
584 ts
->resp
= SAS_TASK_COMPLETE
;
585 ts
->stat
= SAS_ABORTED_TASK
;
586 spin_lock_irqsave(&task
->task_state_lock
, flags
);
587 task
->task_state_flags
&=
588 ~(SAS_TASK_STATE_PENDING
| SAS_TASK_AT_INITIATOR
);
589 task
->task_state_flags
|= SAS_TASK_STATE_DONE
;
590 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
593 hisi_sas_slot_task_free(hisi_hba
, task
, slot
);
596 /* hisi_hba.lock should be locked */
597 static void hisi_sas_release_task(struct hisi_hba
*hisi_hba
,
598 struct domain_device
*device
)
600 struct hisi_sas_slot
*slot
, *slot2
;
601 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
603 list_for_each_entry_safe(slot
, slot2
, &sas_dev
->list
, entry
)
604 hisi_sas_do_release_task(hisi_hba
, slot
->task
, slot
);
607 static void hisi_sas_release_tasks(struct hisi_hba
*hisi_hba
)
609 struct hisi_sas_device
*sas_dev
;
610 struct domain_device
*device
;
613 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
614 sas_dev
= &hisi_hba
->devices
[i
];
615 device
= sas_dev
->sas_device
;
617 if ((sas_dev
->dev_type
== SAS_PHY_UNUSED
) ||
621 hisi_sas_release_task(hisi_hba
, device
);
625 static void hisi_sas_dev_gone(struct domain_device
*device
)
627 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
628 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
629 struct device
*dev
= &hisi_hba
->pdev
->dev
;
630 u64 dev_id
= sas_dev
->device_id
;
632 dev_info(dev
, "found dev[%lld:%x] is gone\n",
633 sas_dev
->device_id
, sas_dev
->dev_type
);
635 hisi_sas_internal_task_abort(hisi_hba
, device
,
636 HISI_SAS_INT_ABT_DEV
, 0);
638 hisi_hba
->hw
->free_device(hisi_hba
, sas_dev
);
639 device
->lldd_dev
= NULL
;
640 memset(sas_dev
, 0, sizeof(*sas_dev
));
641 sas_dev
->device_id
= dev_id
;
642 sas_dev
->dev_type
= SAS_PHY_UNUSED
;
643 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
646 static int hisi_sas_queue_command(struct sas_task
*task
, gfp_t gfp_flags
)
648 return hisi_sas_task_exec(task
, gfp_flags
, 0, NULL
);
651 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
654 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
655 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
656 int phy_no
= sas_phy
->id
;
659 case PHY_FUNC_HARD_RESET
:
660 hisi_hba
->hw
->phy_hard_reset(hisi_hba
, phy_no
);
663 case PHY_FUNC_LINK_RESET
:
664 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
666 hisi_hba
->hw
->phy_enable(hisi_hba
, phy_no
);
669 case PHY_FUNC_DISABLE
:
670 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
673 case PHY_FUNC_SET_LINK_RATE
:
674 hisi_hba
->hw
->phy_set_linkrate(hisi_hba
, phy_no
, funcdata
);
677 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
684 static void hisi_sas_task_done(struct sas_task
*task
)
686 if (!del_timer(&task
->slow_task
->timer
))
688 complete(&task
->slow_task
->completion
);
691 static void hisi_sas_tmf_timedout(unsigned long data
)
693 struct sas_task
*task
= (struct sas_task
*)data
;
695 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
696 complete(&task
->slow_task
->completion
);
699 #define TASK_TIMEOUT 20
701 static int hisi_sas_exec_internal_tmf_task(struct domain_device
*device
,
702 void *parameter
, u32 para_len
,
703 struct hisi_sas_tmf_task
*tmf
)
705 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
706 struct hisi_hba
*hisi_hba
= sas_dev
->hisi_hba
;
707 struct device
*dev
= &hisi_hba
->pdev
->dev
;
708 struct sas_task
*task
;
711 for (retry
= 0; retry
< TASK_RETRY
; retry
++) {
712 task
= sas_alloc_slow_task(GFP_KERNEL
);
717 task
->task_proto
= device
->tproto
;
719 if (dev_is_sata(device
)) {
720 task
->ata_task
.device_control_reg_update
= 1;
721 memcpy(&task
->ata_task
.fis
, parameter
, para_len
);
723 memcpy(&task
->ssp_task
, parameter
, para_len
);
725 task
->task_done
= hisi_sas_task_done
;
727 task
->slow_task
->timer
.data
= (unsigned long) task
;
728 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
729 task
->slow_task
->timer
.expires
= jiffies
+ TASK_TIMEOUT
*HZ
;
730 add_timer(&task
->slow_task
->timer
);
732 res
= hisi_sas_task_exec(task
, GFP_KERNEL
, 1, tmf
);
735 del_timer(&task
->slow_task
->timer
);
736 dev_err(dev
, "abort tmf: executing internal task failed: %d\n",
741 wait_for_completion(&task
->slow_task
->completion
);
742 res
= TMF_RESP_FUNC_FAILED
;
743 /* Even TMF timed out, return direct. */
744 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
745 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
746 struct hisi_sas_slot
*slot
= task
->lldd_task
;
748 dev_err(dev
, "abort tmf: TMF task timeout\n");
756 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
757 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
758 res
= TMF_RESP_FUNC_COMPLETE
;
762 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
763 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
764 res
= TMF_RESP_FUNC_SUCC
;
768 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
769 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
770 /* no error, but return the number of bytes of
773 dev_warn(dev
, "abort tmf: task to dev %016llx "
774 "resp: 0x%x sts 0x%x underrun\n",
775 SAS_ADDR(device
->sas_addr
),
776 task
->task_status
.resp
,
777 task
->task_status
.stat
);
778 res
= task
->task_status
.residual
;
782 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
783 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
784 dev_warn(dev
, "abort tmf: blocked task error\n");
789 dev_warn(dev
, "abort tmf: task to dev "
790 "%016llx resp: 0x%x status 0x%x\n",
791 SAS_ADDR(device
->sas_addr
), task
->task_status
.resp
,
792 task
->task_status
.stat
);
797 if (retry
== TASK_RETRY
)
798 dev_warn(dev
, "abort tmf: executing internal task failed!\n");
803 static void hisi_sas_fill_ata_reset_cmd(struct ata_device
*dev
,
804 bool reset
, int pmp
, u8
*fis
)
806 struct ata_taskfile tf
;
808 ata_tf_init(dev
, &tf
);
813 tf
.command
= ATA_CMD_DEV_RESET
;
814 ata_tf_to_fis(&tf
, pmp
, 0, fis
);
817 static int hisi_sas_softreset_ata_disk(struct domain_device
*device
)
820 struct ata_port
*ap
= device
->sata_dev
.ap
;
821 struct ata_link
*link
;
822 int rc
= TMF_RESP_FUNC_FAILED
;
823 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
824 struct device
*dev
= &hisi_hba
->pdev
->dev
;
825 int s
= sizeof(struct host_to_dev_fis
);
828 ata_for_each_link(link
, ap
, EDGE
) {
829 int pmp
= sata_srst_pmp(link
);
831 hisi_sas_fill_ata_reset_cmd(link
->device
, 1, pmp
, fis
);
832 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
, s
, NULL
);
833 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
837 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
838 ata_for_each_link(link
, ap
, EDGE
) {
839 int pmp
= sata_srst_pmp(link
);
841 hisi_sas_fill_ata_reset_cmd(link
->device
, 0, pmp
, fis
);
842 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
,
844 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
845 dev_err(dev
, "ata disk de-reset failed\n");
848 dev_err(dev
, "ata disk reset failed\n");
851 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
852 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
853 hisi_sas_release_task(hisi_hba
, device
);
854 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
860 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device
*device
,
861 u8
*lun
, struct hisi_sas_tmf_task
*tmf
)
863 struct sas_ssp_task ssp_task
;
865 if (!(device
->tproto
& SAS_PROTOCOL_SSP
))
866 return TMF_RESP_FUNC_ESUPP
;
868 memcpy(ssp_task
.LUN
, lun
, 8);
870 return hisi_sas_exec_internal_tmf_task(device
, &ssp_task
,
871 sizeof(ssp_task
), tmf
);
874 static int hisi_sas_controller_reset(struct hisi_hba
*hisi_hba
)
878 if (!hisi_hba
->hw
->soft_reset
)
881 if (!test_and_set_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
)) {
882 struct device
*dev
= &hisi_hba
->pdev
->dev
;
883 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
886 dev_dbg(dev
, "controller reset begins!\n");
887 scsi_block_requests(hisi_hba
->shost
);
888 rc
= hisi_hba
->hw
->soft_reset(hisi_hba
);
890 dev_warn(dev
, "controller reset failed (%d)\n", rc
);
893 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
894 hisi_sas_release_tasks(hisi_hba
);
895 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
897 sas_ha
->notify_ha_event(sas_ha
, HAE_RESET
);
898 dev_dbg(dev
, "controller reset successful!\n");
903 scsi_unblock_requests(hisi_hba
->shost
);
904 clear_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
);
908 static int hisi_sas_abort_task(struct sas_task
*task
)
911 struct hisi_sas_tmf_task tmf_task
;
912 struct domain_device
*device
= task
->dev
;
913 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
914 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
915 struct device
*dev
= &hisi_hba
->pdev
->dev
;
916 int rc
= TMF_RESP_FUNC_FAILED
;
920 dev_warn(dev
, "Device has been removed\n");
921 return TMF_RESP_FUNC_FAILED
;
924 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
925 rc
= TMF_RESP_FUNC_COMPLETE
;
929 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
930 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
931 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
932 struct hisi_sas_slot
*slot
= task
->lldd_task
;
936 int_to_scsilun(cmnd
->device
->lun
, &lun
);
937 tmf_task
.tmf
= TMF_ABORT_TASK
;
938 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
940 rc
= hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
,
943 rc2
= hisi_sas_internal_task_abort(hisi_hba
, device
,
944 HISI_SAS_INT_ABT_CMD
, tag
);
946 * If the TMF finds that the IO is not in the device and also
947 * the internal abort does not succeed, then it is safe to
949 * Note: if the internal abort succeeds then the slot
950 * will have already been completed
952 if (rc
== TMF_RESP_FUNC_COMPLETE
&& rc2
!= TMF_RESP_FUNC_SUCC
) {
953 if (task
->lldd_task
) {
954 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
955 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
956 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
959 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
960 task
->task_proto
& SAS_PROTOCOL_STP
) {
961 if (task
->dev
->dev_type
== SAS_SATA_DEV
) {
962 hisi_sas_internal_task_abort(hisi_hba
, device
,
963 HISI_SAS_INT_ABT_DEV
, 0);
964 rc
= hisi_sas_softreset_ata_disk(device
);
966 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
968 struct hisi_sas_slot
*slot
= task
->lldd_task
;
971 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
972 HISI_SAS_INT_ABT_CMD
, tag
);
973 if (rc
== TMF_RESP_FUNC_FAILED
) {
974 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
975 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
976 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
981 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
982 dev_notice(dev
, "abort task: rc=%d\n", rc
);
986 static int hisi_sas_abort_task_set(struct domain_device
*device
, u8
*lun
)
988 struct hisi_sas_tmf_task tmf_task
;
989 int rc
= TMF_RESP_FUNC_FAILED
;
991 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
992 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
997 static int hisi_sas_clear_aca(struct domain_device
*device
, u8
*lun
)
999 int rc
= TMF_RESP_FUNC_FAILED
;
1000 struct hisi_sas_tmf_task tmf_task
;
1002 tmf_task
.tmf
= TMF_CLEAR_ACA
;
1003 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1008 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device
*device
)
1010 struct sas_phy
*phy
= sas_get_local_phy(device
);
1011 int rc
, reset_type
= (device
->dev_type
== SAS_SATA_DEV
||
1012 (device
->tproto
& SAS_PROTOCOL_STP
)) ? 0 : 1;
1013 rc
= sas_phy_reset(phy
, reset_type
);
1014 sas_put_local_phy(phy
);
1019 static int hisi_sas_I_T_nexus_reset(struct domain_device
*device
)
1021 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1022 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1023 unsigned long flags
;
1024 int rc
= TMF_RESP_FUNC_FAILED
;
1026 if (sas_dev
->dev_status
!= HISI_SAS_DEV_EH
)
1027 return TMF_RESP_FUNC_FAILED
;
1028 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
1030 rc
= hisi_sas_debug_I_T_nexus_reset(device
);
1032 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1033 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1034 hisi_sas_release_task(hisi_hba
, device
);
1035 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1040 static int hisi_sas_lu_reset(struct domain_device
*device
, u8
*lun
)
1042 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1043 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1044 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1045 unsigned long flags
;
1046 int rc
= TMF_RESP_FUNC_FAILED
;
1048 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1049 if (dev_is_sata(device
)) {
1050 struct sas_phy
*phy
;
1052 /* Clear internal IO and then hardreset */
1053 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1054 HISI_SAS_INT_ABT_DEV
, 0);
1055 if (rc
== TMF_RESP_FUNC_FAILED
)
1058 phy
= sas_get_local_phy(device
);
1060 rc
= sas_phy_reset(phy
, 1);
1063 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1064 hisi_sas_release_task(hisi_hba
, device
);
1065 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1067 sas_put_local_phy(phy
);
1069 struct hisi_sas_tmf_task tmf_task
= { .tmf
= TMF_LU_RESET
};
1071 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1072 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1073 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1074 hisi_sas_release_task(hisi_hba
, device
);
1075 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1079 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1080 dev_err(dev
, "lu_reset: for device[%llx]:rc= %d\n",
1081 sas_dev
->device_id
, rc
);
1085 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct
*sas_ha
)
1087 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
1089 return hisi_sas_controller_reset(hisi_hba
);
1092 static int hisi_sas_query_task(struct sas_task
*task
)
1094 struct scsi_lun lun
;
1095 struct hisi_sas_tmf_task tmf_task
;
1096 int rc
= TMF_RESP_FUNC_FAILED
;
1098 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1099 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1100 struct domain_device
*device
= task
->dev
;
1101 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1102 u32 tag
= slot
->idx
;
1104 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1105 tmf_task
.tmf
= TMF_QUERY_TASK
;
1106 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1108 rc
= hisi_sas_debug_issue_ssp_tmf(device
,
1112 /* The task is still in Lun, release it then */
1113 case TMF_RESP_FUNC_SUCC
:
1114 /* The task is not in Lun or failed, reset the phy */
1115 case TMF_RESP_FUNC_FAILED
:
1116 case TMF_RESP_FUNC_COMPLETE
:
1119 rc
= TMF_RESP_FUNC_FAILED
;
1127 hisi_sas_internal_abort_task_exec(struct hisi_hba
*hisi_hba
, u64 device_id
,
1128 struct sas_task
*task
, int abort_flag
,
1131 struct domain_device
*device
= task
->dev
;
1132 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1133 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1134 struct hisi_sas_port
*port
;
1135 struct hisi_sas_slot
*slot
;
1136 struct asd_sas_port
*sas_port
= device
->port
;
1137 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
1138 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
1139 unsigned long flags
;
1141 if (unlikely(test_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
)))
1147 port
= to_hisi_sas_port(sas_port
);
1149 /* simply get a slot and send abort command */
1150 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
1153 rc
= hisi_hba
->hw
->get_free_slot(hisi_hba
, sas_dev
->device_id
,
1154 &dlvry_queue
, &dlvry_queue_slot
);
1158 slot
= &hisi_hba
->slot_info
[slot_idx
];
1159 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
1161 slot
->idx
= slot_idx
;
1162 slot
->n_elem
= n_elem
;
1163 slot
->dlvry_queue
= dlvry_queue
;
1164 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
1165 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
1166 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
1169 task
->lldd_task
= slot
;
1171 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
1173 rc
= hisi_sas_task_prep_abort(hisi_hba
, slot
, device_id
,
1174 abort_flag
, task_tag
);
1179 list_add_tail(&slot
->entry
, &sas_dev
->list
);
1180 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1181 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
1182 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1184 hisi_hba
->slot_prep
= slot
;
1186 atomic64_inc(&sas_dev
->running_req
);
1188 /* send abort command to our chip */
1189 hisi_hba
->hw
->start_delivery(hisi_hba
);
1194 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
1196 dev_err(dev
, "internal abort task prep: failed[%d]!\n", rc
);
1202 * hisi_sas_internal_task_abort -- execute an internal
1203 * abort command for single IO command or a device
1204 * @hisi_hba: host controller struct
1205 * @device: domain device
1206 * @abort_flag: mode of operation, device or single IO
1207 * @tag: tag of IO to be aborted (only relevant to single
1211 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
1212 struct domain_device
*device
,
1213 int abort_flag
, int tag
)
1215 struct sas_task
*task
;
1216 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1217 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1219 unsigned long flags
;
1221 if (!hisi_hba
->hw
->prep_abort
)
1224 task
= sas_alloc_slow_task(GFP_KERNEL
);
1229 task
->task_proto
= device
->tproto
;
1230 task
->task_done
= hisi_sas_task_done
;
1231 task
->slow_task
->timer
.data
= (unsigned long)task
;
1232 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
1233 task
->slow_task
->timer
.expires
= jiffies
+ msecs_to_jiffies(110);
1234 add_timer(&task
->slow_task
->timer
);
1236 /* Lock as we are alloc'ing a slot, which cannot be interrupted */
1237 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1238 res
= hisi_sas_internal_abort_task_exec(hisi_hba
, sas_dev
->device_id
,
1239 task
, abort_flag
, tag
);
1240 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1242 del_timer(&task
->slow_task
->timer
);
1243 dev_err(dev
, "internal task abort: executing internal task failed: %d\n",
1247 wait_for_completion(&task
->slow_task
->completion
);
1248 res
= TMF_RESP_FUNC_FAILED
;
1250 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1251 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
1252 res
= TMF_RESP_FUNC_COMPLETE
;
1256 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1257 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
1258 res
= TMF_RESP_FUNC_SUCC
;
1262 /* Internal abort timed out */
1263 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
1264 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
1265 dev_err(dev
, "internal task abort: timeout.\n");
1270 dev_dbg(dev
, "internal task abort: task to dev %016llx task=%p "
1271 "resp: 0x%x sts 0x%x\n",
1272 SAS_ADDR(device
->sas_addr
),
1274 task
->task_status
.resp
, /* 0 is complete, -1 is undelivered */
1275 task
->task_status
.stat
);
1276 sas_free_task(task
);
1281 static void hisi_sas_port_formed(struct asd_sas_phy
*sas_phy
)
1283 hisi_sas_port_notify_formed(sas_phy
);
1286 static void hisi_sas_phy_disconnected(struct hisi_sas_phy
*phy
)
1288 phy
->phy_attached
= 0;
1293 void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
)
1295 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1296 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1297 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1300 /* Phy down but ready */
1301 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
1302 hisi_sas_port_notify_formed(sas_phy
);
1304 struct hisi_sas_port
*port
= phy
->port
;
1306 /* Phy down and not ready */
1307 sas_ha
->notify_phy_event(sas_phy
, PHYE_LOSS_OF_SIGNAL
);
1308 sas_phy_disconnected(sas_phy
);
1311 if (phy
->phy_type
& PORT_TYPE_SAS
) {
1312 int port_id
= port
->id
;
1314 if (!hisi_hba
->hw
->get_wideport_bitmap(hisi_hba
,
1316 port
->port_attached
= 0;
1317 } else if (phy
->phy_type
& PORT_TYPE_SATA
)
1318 port
->port_attached
= 0;
1320 hisi_sas_phy_disconnected(phy
);
1323 EXPORT_SYMBOL_GPL(hisi_sas_phy_down
);
1325 void hisi_sas_rescan_topology(struct hisi_hba
*hisi_hba
, u32 old_state
,
1328 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1331 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++) {
1332 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1333 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1334 struct asd_sas_port
*sas_port
= sas_phy
->port
;
1335 struct domain_device
*dev
;
1337 if (sas_phy
->enabled
) {
1338 /* Report PHY state change to libsas */
1339 if (state
& (1 << phy_no
))
1342 if (old_state
& (1 << phy_no
))
1343 /* PHY down but was up before */
1344 hisi_sas_phy_down(hisi_hba
, phy_no
, 0);
1348 dev
= sas_port
->port_dev
;
1350 if (DEV_IS_EXPANDER(dev
->dev_type
))
1351 sas_ha
->notify_phy_event(sas_phy
, PORTE_BROADCAST_RCVD
);
1354 EXPORT_SYMBOL_GPL(hisi_sas_rescan_topology
);
1356 static struct scsi_transport_template
*hisi_sas_stt
;
1358 static struct scsi_host_template hisi_sas_sht
= {
1359 .module
= THIS_MODULE
,
1361 .queuecommand
= sas_queuecommand
,
1362 .target_alloc
= sas_target_alloc
,
1363 .slave_configure
= hisi_sas_slave_configure
,
1364 .scan_finished
= hisi_sas_scan_finished
,
1365 .scan_start
= hisi_sas_scan_start
,
1366 .change_queue_depth
= sas_change_queue_depth
,
1367 .bios_param
= sas_bios_param
,
1370 .sg_tablesize
= SG_ALL
,
1371 .max_sectors
= SCSI_DEFAULT_MAX_SECTORS
,
1372 .use_clustering
= ENABLE_CLUSTERING
,
1373 .eh_device_reset_handler
= sas_eh_device_reset_handler
,
1374 .eh_bus_reset_handler
= sas_eh_bus_reset_handler
,
1375 .target_destroy
= sas_target_destroy
,
1379 static struct sas_domain_function_template hisi_sas_transport_ops
= {
1380 .lldd_dev_found
= hisi_sas_dev_found
,
1381 .lldd_dev_gone
= hisi_sas_dev_gone
,
1382 .lldd_execute_task
= hisi_sas_queue_command
,
1383 .lldd_control_phy
= hisi_sas_control_phy
,
1384 .lldd_abort_task
= hisi_sas_abort_task
,
1385 .lldd_abort_task_set
= hisi_sas_abort_task_set
,
1386 .lldd_clear_aca
= hisi_sas_clear_aca
,
1387 .lldd_I_T_nexus_reset
= hisi_sas_I_T_nexus_reset
,
1388 .lldd_lu_reset
= hisi_sas_lu_reset
,
1389 .lldd_query_task
= hisi_sas_query_task
,
1390 .lldd_clear_nexus_ha
= hisi_sas_clear_nexus_ha
,
1391 .lldd_port_formed
= hisi_sas_port_formed
,
1394 void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
)
1396 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1398 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1399 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1400 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1402 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1403 memset(hisi_hba
->cmd_hdr
[i
], 0, s
);
1406 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1407 memset(hisi_hba
->complete_hdr
[i
], 0, s
);
1411 s
= sizeof(struct hisi_sas_initial_fis
) * hisi_hba
->n_phy
;
1412 memset(hisi_hba
->initial_fis
, 0, s
);
1414 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1415 memset(hisi_hba
->iost
, 0, s
);
1417 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1418 memset(hisi_hba
->breakpoint
, 0, s
);
1420 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
) * 2;
1421 memset(hisi_hba
->sata_breakpoint
, 0, s
);
1423 EXPORT_SYMBOL_GPL(hisi_sas_init_mem
);
1425 static int hisi_sas_alloc(struct hisi_hba
*hisi_hba
, struct Scsi_Host
*shost
)
1427 struct platform_device
*pdev
= hisi_hba
->pdev
;
1428 struct device
*dev
= &pdev
->dev
;
1429 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1431 spin_lock_init(&hisi_hba
->lock
);
1432 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1433 hisi_sas_phy_init(hisi_hba
, i
);
1434 hisi_hba
->port
[i
].port_attached
= 0;
1435 hisi_hba
->port
[i
].id
= -1;
1438 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1439 hisi_hba
->devices
[i
].dev_type
= SAS_PHY_UNUSED
;
1440 hisi_hba
->devices
[i
].device_id
= i
;
1441 hisi_hba
->devices
[i
].dev_status
= HISI_SAS_DEV_NORMAL
;
1444 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1445 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1446 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1448 /* Completion queue structure */
1450 cq
->hisi_hba
= hisi_hba
;
1452 /* Delivery queue structure */
1454 dq
->hisi_hba
= hisi_hba
;
1456 /* Delivery queue */
1457 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1458 hisi_hba
->cmd_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1459 &hisi_hba
->cmd_hdr_dma
[i
], GFP_KERNEL
);
1460 if (!hisi_hba
->cmd_hdr
[i
])
1463 /* Completion queue */
1464 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1465 hisi_hba
->complete_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1466 &hisi_hba
->complete_hdr_dma
[i
], GFP_KERNEL
);
1467 if (!hisi_hba
->complete_hdr
[i
])
1471 s
= HISI_SAS_STATUS_BUF_SZ
;
1472 hisi_hba
->status_buffer_pool
= dma_pool_create("status_buffer",
1474 if (!hisi_hba
->status_buffer_pool
)
1477 s
= HISI_SAS_COMMAND_TABLE_SZ
;
1478 hisi_hba
->command_table_pool
= dma_pool_create("command_table",
1480 if (!hisi_hba
->command_table_pool
)
1483 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1484 hisi_hba
->itct
= dma_alloc_coherent(dev
, s
, &hisi_hba
->itct_dma
,
1486 if (!hisi_hba
->itct
)
1489 memset(hisi_hba
->itct
, 0, s
);
1491 hisi_hba
->slot_info
= devm_kcalloc(dev
, max_command_entries
,
1492 sizeof(struct hisi_sas_slot
),
1494 if (!hisi_hba
->slot_info
)
1497 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1498 hisi_hba
->iost
= dma_alloc_coherent(dev
, s
, &hisi_hba
->iost_dma
,
1500 if (!hisi_hba
->iost
)
1503 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1504 hisi_hba
->breakpoint
= dma_alloc_coherent(dev
, s
,
1505 &hisi_hba
->breakpoint_dma
, GFP_KERNEL
);
1506 if (!hisi_hba
->breakpoint
)
1509 hisi_hba
->slot_index_count
= max_command_entries
;
1510 s
= hisi_hba
->slot_index_count
/ BITS_PER_BYTE
;
1511 hisi_hba
->slot_index_tags
= devm_kzalloc(dev
, s
, GFP_KERNEL
);
1512 if (!hisi_hba
->slot_index_tags
)
1515 hisi_hba
->sge_page_pool
= dma_pool_create("status_sge", dev
,
1516 sizeof(struct hisi_sas_sge_page
), 16, 0);
1517 if (!hisi_hba
->sge_page_pool
)
1520 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1521 hisi_hba
->initial_fis
= dma_alloc_coherent(dev
, s
,
1522 &hisi_hba
->initial_fis_dma
, GFP_KERNEL
);
1523 if (!hisi_hba
->initial_fis
)
1526 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
) * 2;
1527 hisi_hba
->sata_breakpoint
= dma_alloc_coherent(dev
, s
,
1528 &hisi_hba
->sata_breakpoint_dma
, GFP_KERNEL
);
1529 if (!hisi_hba
->sata_breakpoint
)
1531 hisi_sas_init_mem(hisi_hba
);
1533 hisi_sas_slot_index_init(hisi_hba
);
1535 hisi_hba
->wq
= create_singlethread_workqueue(dev_name(dev
));
1536 if (!hisi_hba
->wq
) {
1537 dev_err(dev
, "sas_alloc: failed to create workqueue\n");
1546 static void hisi_sas_free(struct hisi_hba
*hisi_hba
)
1548 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1549 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1551 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1552 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1553 if (hisi_hba
->cmd_hdr
[i
])
1554 dma_free_coherent(dev
, s
,
1555 hisi_hba
->cmd_hdr
[i
],
1556 hisi_hba
->cmd_hdr_dma
[i
]);
1558 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1559 if (hisi_hba
->complete_hdr
[i
])
1560 dma_free_coherent(dev
, s
,
1561 hisi_hba
->complete_hdr
[i
],
1562 hisi_hba
->complete_hdr_dma
[i
]);
1565 dma_pool_destroy(hisi_hba
->status_buffer_pool
);
1566 dma_pool_destroy(hisi_hba
->command_table_pool
);
1567 dma_pool_destroy(hisi_hba
->sge_page_pool
);
1569 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1571 dma_free_coherent(dev
, s
,
1572 hisi_hba
->itct
, hisi_hba
->itct_dma
);
1574 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1576 dma_free_coherent(dev
, s
,
1577 hisi_hba
->iost
, hisi_hba
->iost_dma
);
1579 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1580 if (hisi_hba
->breakpoint
)
1581 dma_free_coherent(dev
, s
,
1582 hisi_hba
->breakpoint
,
1583 hisi_hba
->breakpoint_dma
);
1586 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1587 if (hisi_hba
->initial_fis
)
1588 dma_free_coherent(dev
, s
,
1589 hisi_hba
->initial_fis
,
1590 hisi_hba
->initial_fis_dma
);
1592 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
) * 2;
1593 if (hisi_hba
->sata_breakpoint
)
1594 dma_free_coherent(dev
, s
,
1595 hisi_hba
->sata_breakpoint
,
1596 hisi_hba
->sata_breakpoint_dma
);
1599 destroy_workqueue(hisi_hba
->wq
);
1602 static void hisi_sas_rst_work_handler(struct work_struct
*work
)
1604 struct hisi_hba
*hisi_hba
=
1605 container_of(work
, struct hisi_hba
, rst_work
);
1607 hisi_sas_controller_reset(hisi_hba
);
1610 static struct Scsi_Host
*hisi_sas_shost_alloc(struct platform_device
*pdev
,
1611 const struct hisi_sas_hw
*hw
)
1613 struct resource
*res
;
1614 struct Scsi_Host
*shost
;
1615 struct hisi_hba
*hisi_hba
;
1616 struct device
*dev
= &pdev
->dev
;
1617 struct device_node
*np
= pdev
->dev
.of_node
;
1620 shost
= scsi_host_alloc(&hisi_sas_sht
, sizeof(*hisi_hba
));
1622 dev_err(dev
, "scsi host alloc failed\n");
1625 hisi_hba
= shost_priv(shost
);
1627 INIT_WORK(&hisi_hba
->rst_work
, hisi_sas_rst_work_handler
);
1629 hisi_hba
->pdev
= pdev
;
1630 hisi_hba
->shost
= shost
;
1631 SHOST_TO_SAS_HA(shost
) = &hisi_hba
->sha
;
1633 init_timer(&hisi_hba
->timer
);
1635 if (device_property_read_u8_array(dev
, "sas-addr", hisi_hba
->sas_addr
,
1640 hisi_hba
->ctrl
= syscon_regmap_lookup_by_phandle(np
,
1641 "hisilicon,sas-syscon");
1642 if (IS_ERR(hisi_hba
->ctrl
))
1645 if (device_property_read_u32(dev
, "ctrl-reset-reg",
1646 &hisi_hba
->ctrl_reset_reg
))
1649 if (device_property_read_u32(dev
, "ctrl-reset-sts-reg",
1650 &hisi_hba
->ctrl_reset_sts_reg
))
1653 if (device_property_read_u32(dev
, "ctrl-clock-ena-reg",
1654 &hisi_hba
->ctrl_clock_ena_reg
))
1658 refclk
= devm_clk_get(&pdev
->dev
, NULL
);
1660 dev_dbg(dev
, "no ref clk property\n");
1662 hisi_hba
->refclk_frequency_mhz
= clk_get_rate(refclk
) / 1000000;
1664 if (device_property_read_u32(dev
, "phy-count", &hisi_hba
->n_phy
))
1667 if (device_property_read_u32(dev
, "queue-count",
1668 &hisi_hba
->queue_count
))
1671 if (dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64)) &&
1672 dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32))) {
1673 dev_err(dev
, "No usable DMA addressing method\n");
1677 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1678 hisi_hba
->regs
= devm_ioremap_resource(dev
, res
);
1679 if (IS_ERR(hisi_hba
->regs
))
1682 if (hisi_sas_alloc(hisi_hba
, shost
)) {
1683 hisi_sas_free(hisi_hba
);
1690 dev_err(dev
, "shost alloc failed\n");
1694 static void hisi_sas_init_add(struct hisi_hba
*hisi_hba
)
1698 for (i
= 0; i
< hisi_hba
->n_phy
; i
++)
1699 memcpy(&hisi_hba
->phy
[i
].dev_sas_addr
,
1704 int hisi_sas_probe(struct platform_device
*pdev
,
1705 const struct hisi_sas_hw
*hw
)
1707 struct Scsi_Host
*shost
;
1708 struct hisi_hba
*hisi_hba
;
1709 struct device
*dev
= &pdev
->dev
;
1710 struct asd_sas_phy
**arr_phy
;
1711 struct asd_sas_port
**arr_port
;
1712 struct sas_ha_struct
*sha
;
1713 int rc
, phy_nr
, port_nr
, i
;
1715 shost
= hisi_sas_shost_alloc(pdev
, hw
);
1719 sha
= SHOST_TO_SAS_HA(shost
);
1720 hisi_hba
= shost_priv(shost
);
1721 platform_set_drvdata(pdev
, sha
);
1723 phy_nr
= port_nr
= hisi_hba
->n_phy
;
1725 arr_phy
= devm_kcalloc(dev
, phy_nr
, sizeof(void *), GFP_KERNEL
);
1726 arr_port
= devm_kcalloc(dev
, port_nr
, sizeof(void *), GFP_KERNEL
);
1727 if (!arr_phy
|| !arr_port
) {
1732 sha
->sas_phy
= arr_phy
;
1733 sha
->sas_port
= arr_port
;
1734 sha
->lldd_ha
= hisi_hba
;
1736 shost
->transportt
= hisi_sas_stt
;
1737 shost
->max_id
= HISI_SAS_MAX_DEVICES
;
1738 shost
->max_lun
= ~0;
1739 shost
->max_channel
= 1;
1740 shost
->max_cmd_len
= 16;
1741 shost
->sg_tablesize
= min_t(u16
, SG_ALL
, HISI_SAS_SGE_PAGE_CNT
);
1742 shost
->can_queue
= hisi_hba
->hw
->max_command_entries
;
1743 shost
->cmd_per_lun
= hisi_hba
->hw
->max_command_entries
;
1745 sha
->sas_ha_name
= DRV_NAME
;
1746 sha
->dev
= &hisi_hba
->pdev
->dev
;
1747 sha
->lldd_module
= THIS_MODULE
;
1748 sha
->sas_addr
= &hisi_hba
->sas_addr
[0];
1749 sha
->num_phys
= hisi_hba
->n_phy
;
1750 sha
->core
.shost
= hisi_hba
->shost
;
1752 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1753 sha
->sas_phy
[i
] = &hisi_hba
->phy
[i
].sas_phy
;
1754 sha
->sas_port
[i
] = &hisi_hba
->port
[i
].sas_port
;
1757 hisi_sas_init_add(hisi_hba
);
1759 rc
= scsi_add_host(shost
, &pdev
->dev
);
1763 rc
= sas_register_ha(sha
);
1765 goto err_out_register_ha
;
1767 rc
= hisi_hba
->hw
->hw_init(hisi_hba
);
1769 goto err_out_register_ha
;
1771 scsi_scan_host(shost
);
1775 err_out_register_ha
:
1776 scsi_remove_host(shost
);
1778 hisi_sas_free(hisi_hba
);
1782 EXPORT_SYMBOL_GPL(hisi_sas_probe
);
1784 int hisi_sas_remove(struct platform_device
*pdev
)
1786 struct sas_ha_struct
*sha
= platform_get_drvdata(pdev
);
1787 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
1788 struct Scsi_Host
*shost
= sha
->core
.shost
;
1790 sas_unregister_ha(sha
);
1791 sas_remove_host(sha
->core
.shost
);
1793 hisi_sas_free(hisi_hba
);
1797 EXPORT_SYMBOL_GPL(hisi_sas_remove
);
1799 static __init
int hisi_sas_init(void)
1801 pr_info("hisi_sas: driver version %s\n", DRV_VERSION
);
1803 hisi_sas_stt
= sas_domain_attach_transport(&hisi_sas_transport_ops
);
1810 static __exit
void hisi_sas_exit(void)
1812 sas_release_transport(hisi_sas_stt
);
1815 module_init(hisi_sas_init
);
1816 module_exit(hisi_sas_exit
);
1818 MODULE_VERSION(DRV_VERSION
);
1819 MODULE_LICENSE("GPL");
1820 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1821 MODULE_DESCRIPTION("HISILICON SAS controller driver");
1822 MODULE_ALIAS("platform:" DRV_NAME
);