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
;
696 spin_lock_irqsave(&task
->task_state_lock
, flags
);
697 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
698 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
699 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
701 complete(&task
->slow_task
->completion
);
704 #define TASK_TIMEOUT 20
706 static int hisi_sas_exec_internal_tmf_task(struct domain_device
*device
,
707 void *parameter
, u32 para_len
,
708 struct hisi_sas_tmf_task
*tmf
)
710 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
711 struct hisi_hba
*hisi_hba
= sas_dev
->hisi_hba
;
712 struct device
*dev
= &hisi_hba
->pdev
->dev
;
713 struct sas_task
*task
;
716 for (retry
= 0; retry
< TASK_RETRY
; retry
++) {
717 task
= sas_alloc_slow_task(GFP_KERNEL
);
722 task
->task_proto
= device
->tproto
;
724 if (dev_is_sata(device
)) {
725 task
->ata_task
.device_control_reg_update
= 1;
726 memcpy(&task
->ata_task
.fis
, parameter
, para_len
);
728 memcpy(&task
->ssp_task
, parameter
, para_len
);
730 task
->task_done
= hisi_sas_task_done
;
732 task
->slow_task
->timer
.data
= (unsigned long) task
;
733 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
734 task
->slow_task
->timer
.expires
= jiffies
+ TASK_TIMEOUT
*HZ
;
735 add_timer(&task
->slow_task
->timer
);
737 res
= hisi_sas_task_exec(task
, GFP_KERNEL
, 1, tmf
);
740 del_timer(&task
->slow_task
->timer
);
741 dev_err(dev
, "abort tmf: executing internal task failed: %d\n",
746 wait_for_completion(&task
->slow_task
->completion
);
747 res
= TMF_RESP_FUNC_FAILED
;
748 /* Even TMF timed out, return direct. */
749 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
750 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
751 struct hisi_sas_slot
*slot
= task
->lldd_task
;
753 dev_err(dev
, "abort tmf: TMF task timeout\n");
761 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
762 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
763 res
= TMF_RESP_FUNC_COMPLETE
;
767 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
768 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
769 res
= TMF_RESP_FUNC_SUCC
;
773 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
774 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
775 /* no error, but return the number of bytes of
778 dev_warn(dev
, "abort tmf: task to dev %016llx "
779 "resp: 0x%x sts 0x%x underrun\n",
780 SAS_ADDR(device
->sas_addr
),
781 task
->task_status
.resp
,
782 task
->task_status
.stat
);
783 res
= task
->task_status
.residual
;
787 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
788 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
789 dev_warn(dev
, "abort tmf: blocked task error\n");
794 dev_warn(dev
, "abort tmf: task to dev "
795 "%016llx resp: 0x%x status 0x%x\n",
796 SAS_ADDR(device
->sas_addr
), task
->task_status
.resp
,
797 task
->task_status
.stat
);
802 if (retry
== TASK_RETRY
)
803 dev_warn(dev
, "abort tmf: executing internal task failed!\n");
808 static void hisi_sas_fill_ata_reset_cmd(struct ata_device
*dev
,
809 bool reset
, int pmp
, u8
*fis
)
811 struct ata_taskfile tf
;
813 ata_tf_init(dev
, &tf
);
818 tf
.command
= ATA_CMD_DEV_RESET
;
819 ata_tf_to_fis(&tf
, pmp
, 0, fis
);
822 static int hisi_sas_softreset_ata_disk(struct domain_device
*device
)
825 struct ata_port
*ap
= device
->sata_dev
.ap
;
826 struct ata_link
*link
;
827 int rc
= TMF_RESP_FUNC_FAILED
;
828 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
829 struct device
*dev
= &hisi_hba
->pdev
->dev
;
830 int s
= sizeof(struct host_to_dev_fis
);
833 ata_for_each_link(link
, ap
, EDGE
) {
834 int pmp
= sata_srst_pmp(link
);
836 hisi_sas_fill_ata_reset_cmd(link
->device
, 1, pmp
, fis
);
837 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
, s
, NULL
);
838 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
842 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
843 ata_for_each_link(link
, ap
, EDGE
) {
844 int pmp
= sata_srst_pmp(link
);
846 hisi_sas_fill_ata_reset_cmd(link
->device
, 0, pmp
, fis
);
847 rc
= hisi_sas_exec_internal_tmf_task(device
, fis
,
849 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
850 dev_err(dev
, "ata disk de-reset failed\n");
853 dev_err(dev
, "ata disk reset failed\n");
856 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
857 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
858 hisi_sas_release_task(hisi_hba
, device
);
859 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
865 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device
*device
,
866 u8
*lun
, struct hisi_sas_tmf_task
*tmf
)
868 struct sas_ssp_task ssp_task
;
870 if (!(device
->tproto
& SAS_PROTOCOL_SSP
))
871 return TMF_RESP_FUNC_ESUPP
;
873 memcpy(ssp_task
.LUN
, lun
, 8);
875 return hisi_sas_exec_internal_tmf_task(device
, &ssp_task
,
876 sizeof(ssp_task
), tmf
);
879 static int hisi_sas_controller_reset(struct hisi_hba
*hisi_hba
)
883 if (!hisi_hba
->hw
->soft_reset
)
886 if (!test_and_set_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
)) {
887 struct device
*dev
= &hisi_hba
->pdev
->dev
;
888 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
891 dev_dbg(dev
, "controller reset begins!\n");
892 scsi_block_requests(hisi_hba
->shost
);
893 rc
= hisi_hba
->hw
->soft_reset(hisi_hba
);
895 dev_warn(dev
, "controller reset failed (%d)\n", rc
);
898 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
899 hisi_sas_release_tasks(hisi_hba
);
900 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
902 sas_ha
->notify_ha_event(sas_ha
, HAE_RESET
);
903 dev_dbg(dev
, "controller reset successful!\n");
908 scsi_unblock_requests(hisi_hba
->shost
);
909 clear_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
);
913 static int hisi_sas_abort_task(struct sas_task
*task
)
916 struct hisi_sas_tmf_task tmf_task
;
917 struct domain_device
*device
= task
->dev
;
918 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
919 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
920 struct device
*dev
= &hisi_hba
->pdev
->dev
;
921 int rc
= TMF_RESP_FUNC_FAILED
;
925 dev_warn(dev
, "Device has been removed\n");
926 return TMF_RESP_FUNC_FAILED
;
929 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
930 rc
= TMF_RESP_FUNC_COMPLETE
;
934 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
935 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
936 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
937 struct hisi_sas_slot
*slot
= task
->lldd_task
;
941 int_to_scsilun(cmnd
->device
->lun
, &lun
);
942 tmf_task
.tmf
= TMF_ABORT_TASK
;
943 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
945 rc
= hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
,
948 rc2
= hisi_sas_internal_task_abort(hisi_hba
, device
,
949 HISI_SAS_INT_ABT_CMD
, tag
);
951 * If the TMF finds that the IO is not in the device and also
952 * the internal abort does not succeed, then it is safe to
954 * Note: if the internal abort succeeds then the slot
955 * will have already been completed
957 if (rc
== TMF_RESP_FUNC_COMPLETE
&& rc2
!= TMF_RESP_FUNC_SUCC
) {
958 if (task
->lldd_task
) {
959 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
960 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
961 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
964 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
965 task
->task_proto
& SAS_PROTOCOL_STP
) {
966 if (task
->dev
->dev_type
== SAS_SATA_DEV
) {
967 hisi_sas_internal_task_abort(hisi_hba
, device
,
968 HISI_SAS_INT_ABT_DEV
, 0);
969 rc
= hisi_sas_softreset_ata_disk(device
);
971 } else if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SMP
) {
973 struct hisi_sas_slot
*slot
= task
->lldd_task
;
976 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
977 HISI_SAS_INT_ABT_CMD
, tag
);
978 if (rc
== TMF_RESP_FUNC_FAILED
) {
979 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
980 hisi_sas_do_release_task(hisi_hba
, task
, slot
);
981 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
986 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
987 dev_notice(dev
, "abort task: rc=%d\n", rc
);
991 static int hisi_sas_abort_task_set(struct domain_device
*device
, u8
*lun
)
993 struct hisi_sas_tmf_task tmf_task
;
994 int rc
= TMF_RESP_FUNC_FAILED
;
996 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
997 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1002 static int hisi_sas_clear_aca(struct domain_device
*device
, u8
*lun
)
1004 int rc
= TMF_RESP_FUNC_FAILED
;
1005 struct hisi_sas_tmf_task tmf_task
;
1007 tmf_task
.tmf
= TMF_CLEAR_ACA
;
1008 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1013 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device
*device
)
1015 struct sas_phy
*phy
= sas_get_local_phy(device
);
1016 int rc
, reset_type
= (device
->dev_type
== SAS_SATA_DEV
||
1017 (device
->tproto
& SAS_PROTOCOL_STP
)) ? 0 : 1;
1018 rc
= sas_phy_reset(phy
, reset_type
);
1019 sas_put_local_phy(phy
);
1024 static int hisi_sas_I_T_nexus_reset(struct domain_device
*device
)
1026 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1027 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1028 unsigned long flags
;
1029 int rc
= TMF_RESP_FUNC_FAILED
;
1031 if (sas_dev
->dev_status
!= HISI_SAS_DEV_EH
)
1032 return TMF_RESP_FUNC_FAILED
;
1033 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
1035 rc
= hisi_sas_debug_I_T_nexus_reset(device
);
1037 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1038 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1039 hisi_sas_release_task(hisi_hba
, device
);
1040 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1045 static int hisi_sas_lu_reset(struct domain_device
*device
, u8
*lun
)
1047 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1048 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
1049 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1050 unsigned long flags
;
1051 int rc
= TMF_RESP_FUNC_FAILED
;
1053 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
1054 if (dev_is_sata(device
)) {
1055 struct sas_phy
*phy
;
1057 /* Clear internal IO and then hardreset */
1058 rc
= hisi_sas_internal_task_abort(hisi_hba
, device
,
1059 HISI_SAS_INT_ABT_DEV
, 0);
1060 if (rc
== TMF_RESP_FUNC_FAILED
)
1063 phy
= sas_get_local_phy(device
);
1065 rc
= sas_phy_reset(phy
, 1);
1068 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1069 hisi_sas_release_task(hisi_hba
, device
);
1070 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1072 sas_put_local_phy(phy
);
1074 struct hisi_sas_tmf_task tmf_task
= { .tmf
= TMF_LU_RESET
};
1076 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
1077 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
1078 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1079 hisi_sas_release_task(hisi_hba
, device
);
1080 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1084 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1085 dev_err(dev
, "lu_reset: for device[%llx]:rc= %d\n",
1086 sas_dev
->device_id
, rc
);
1090 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct
*sas_ha
)
1092 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
1094 return hisi_sas_controller_reset(hisi_hba
);
1097 static int hisi_sas_query_task(struct sas_task
*task
)
1099 struct scsi_lun lun
;
1100 struct hisi_sas_tmf_task tmf_task
;
1101 int rc
= TMF_RESP_FUNC_FAILED
;
1103 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
1104 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1105 struct domain_device
*device
= task
->dev
;
1106 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1107 u32 tag
= slot
->idx
;
1109 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1110 tmf_task
.tmf
= TMF_QUERY_TASK
;
1111 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
1113 rc
= hisi_sas_debug_issue_ssp_tmf(device
,
1117 /* The task is still in Lun, release it then */
1118 case TMF_RESP_FUNC_SUCC
:
1119 /* The task is not in Lun or failed, reset the phy */
1120 case TMF_RESP_FUNC_FAILED
:
1121 case TMF_RESP_FUNC_COMPLETE
:
1124 rc
= TMF_RESP_FUNC_FAILED
;
1132 hisi_sas_internal_abort_task_exec(struct hisi_hba
*hisi_hba
, u64 device_id
,
1133 struct sas_task
*task
, int abort_flag
,
1136 struct domain_device
*device
= task
->dev
;
1137 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1138 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1139 struct hisi_sas_port
*port
;
1140 struct hisi_sas_slot
*slot
;
1141 struct asd_sas_port
*sas_port
= device
->port
;
1142 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
1143 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
1144 unsigned long flags
;
1146 if (unlikely(test_bit(HISI_SAS_RESET_BIT
, &hisi_hba
->flags
)))
1152 port
= to_hisi_sas_port(sas_port
);
1154 /* simply get a slot and send abort command */
1155 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
1158 rc
= hisi_hba
->hw
->get_free_slot(hisi_hba
, sas_dev
->device_id
,
1159 &dlvry_queue
, &dlvry_queue_slot
);
1163 slot
= &hisi_hba
->slot_info
[slot_idx
];
1164 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
1166 slot
->idx
= slot_idx
;
1167 slot
->n_elem
= n_elem
;
1168 slot
->dlvry_queue
= dlvry_queue
;
1169 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
1170 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
1171 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
1174 task
->lldd_task
= slot
;
1176 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
1178 rc
= hisi_sas_task_prep_abort(hisi_hba
, slot
, device_id
,
1179 abort_flag
, task_tag
);
1184 list_add_tail(&slot
->entry
, &sas_dev
->list
);
1185 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1186 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
1187 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1189 hisi_hba
->slot_prep
= slot
;
1191 atomic64_inc(&sas_dev
->running_req
);
1193 /* send abort command to our chip */
1194 hisi_hba
->hw
->start_delivery(hisi_hba
);
1199 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
1201 dev_err(dev
, "internal abort task prep: failed[%d]!\n", rc
);
1207 * hisi_sas_internal_task_abort -- execute an internal
1208 * abort command for single IO command or a device
1209 * @hisi_hba: host controller struct
1210 * @device: domain device
1211 * @abort_flag: mode of operation, device or single IO
1212 * @tag: tag of IO to be aborted (only relevant to single
1216 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
1217 struct domain_device
*device
,
1218 int abort_flag
, int tag
)
1220 struct sas_task
*task
;
1221 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1222 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1224 unsigned long flags
;
1226 if (!hisi_hba
->hw
->prep_abort
)
1229 task
= sas_alloc_slow_task(GFP_KERNEL
);
1234 task
->task_proto
= device
->tproto
;
1235 task
->task_done
= hisi_sas_task_done
;
1236 task
->slow_task
->timer
.data
= (unsigned long)task
;
1237 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
1238 task
->slow_task
->timer
.expires
= jiffies
+ msecs_to_jiffies(110);
1239 add_timer(&task
->slow_task
->timer
);
1241 /* Lock as we are alloc'ing a slot, which cannot be interrupted */
1242 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1243 res
= hisi_sas_internal_abort_task_exec(hisi_hba
, sas_dev
->device_id
,
1244 task
, abort_flag
, tag
);
1245 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1247 del_timer(&task
->slow_task
->timer
);
1248 dev_err(dev
, "internal task abort: executing internal task failed: %d\n",
1252 wait_for_completion(&task
->slow_task
->completion
);
1253 res
= TMF_RESP_FUNC_FAILED
;
1255 /* Internal abort timed out */
1256 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
1257 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
1258 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1262 dev_err(dev
, "internal task abort: timeout.\n");
1266 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1267 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
1268 res
= TMF_RESP_FUNC_COMPLETE
;
1272 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1273 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
1274 res
= TMF_RESP_FUNC_SUCC
;
1279 dev_dbg(dev
, "internal task abort: task to dev %016llx task=%p "
1280 "resp: 0x%x sts 0x%x\n",
1281 SAS_ADDR(device
->sas_addr
),
1283 task
->task_status
.resp
, /* 0 is complete, -1 is undelivered */
1284 task
->task_status
.stat
);
1285 sas_free_task(task
);
1290 static void hisi_sas_port_formed(struct asd_sas_phy
*sas_phy
)
1292 hisi_sas_port_notify_formed(sas_phy
);
1295 static void hisi_sas_phy_disconnected(struct hisi_sas_phy
*phy
)
1297 phy
->phy_attached
= 0;
1302 void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
)
1304 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1305 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1306 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1309 /* Phy down but ready */
1310 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
1311 hisi_sas_port_notify_formed(sas_phy
);
1313 struct hisi_sas_port
*port
= phy
->port
;
1315 /* Phy down and not ready */
1316 sas_ha
->notify_phy_event(sas_phy
, PHYE_LOSS_OF_SIGNAL
);
1317 sas_phy_disconnected(sas_phy
);
1320 if (phy
->phy_type
& PORT_TYPE_SAS
) {
1321 int port_id
= port
->id
;
1323 if (!hisi_hba
->hw
->get_wideport_bitmap(hisi_hba
,
1325 port
->port_attached
= 0;
1326 } else if (phy
->phy_type
& PORT_TYPE_SATA
)
1327 port
->port_attached
= 0;
1329 hisi_sas_phy_disconnected(phy
);
1332 EXPORT_SYMBOL_GPL(hisi_sas_phy_down
);
1334 void hisi_sas_rescan_topology(struct hisi_hba
*hisi_hba
, u32 old_state
,
1337 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1340 for (phy_no
= 0; phy_no
< hisi_hba
->n_phy
; phy_no
++) {
1341 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1342 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1343 struct asd_sas_port
*sas_port
= sas_phy
->port
;
1344 struct domain_device
*dev
;
1346 if (sas_phy
->enabled
) {
1347 /* Report PHY state change to libsas */
1348 if (state
& (1 << phy_no
))
1351 if (old_state
& (1 << phy_no
))
1352 /* PHY down but was up before */
1353 hisi_sas_phy_down(hisi_hba
, phy_no
, 0);
1357 dev
= sas_port
->port_dev
;
1359 if (DEV_IS_EXPANDER(dev
->dev_type
))
1360 sas_ha
->notify_phy_event(sas_phy
, PORTE_BROADCAST_RCVD
);
1363 EXPORT_SYMBOL_GPL(hisi_sas_rescan_topology
);
1365 static struct scsi_transport_template
*hisi_sas_stt
;
1367 static struct scsi_host_template hisi_sas_sht
= {
1368 .module
= THIS_MODULE
,
1370 .queuecommand
= sas_queuecommand
,
1371 .target_alloc
= sas_target_alloc
,
1372 .slave_configure
= hisi_sas_slave_configure
,
1373 .scan_finished
= hisi_sas_scan_finished
,
1374 .scan_start
= hisi_sas_scan_start
,
1375 .change_queue_depth
= sas_change_queue_depth
,
1376 .bios_param
= sas_bios_param
,
1379 .sg_tablesize
= SG_ALL
,
1380 .max_sectors
= SCSI_DEFAULT_MAX_SECTORS
,
1381 .use_clustering
= ENABLE_CLUSTERING
,
1382 .eh_device_reset_handler
= sas_eh_device_reset_handler
,
1383 .eh_bus_reset_handler
= sas_eh_bus_reset_handler
,
1384 .target_destroy
= sas_target_destroy
,
1388 static struct sas_domain_function_template hisi_sas_transport_ops
= {
1389 .lldd_dev_found
= hisi_sas_dev_found
,
1390 .lldd_dev_gone
= hisi_sas_dev_gone
,
1391 .lldd_execute_task
= hisi_sas_queue_command
,
1392 .lldd_control_phy
= hisi_sas_control_phy
,
1393 .lldd_abort_task
= hisi_sas_abort_task
,
1394 .lldd_abort_task_set
= hisi_sas_abort_task_set
,
1395 .lldd_clear_aca
= hisi_sas_clear_aca
,
1396 .lldd_I_T_nexus_reset
= hisi_sas_I_T_nexus_reset
,
1397 .lldd_lu_reset
= hisi_sas_lu_reset
,
1398 .lldd_query_task
= hisi_sas_query_task
,
1399 .lldd_clear_nexus_ha
= hisi_sas_clear_nexus_ha
,
1400 .lldd_port_formed
= hisi_sas_port_formed
,
1403 void hisi_sas_init_mem(struct hisi_hba
*hisi_hba
)
1405 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1407 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1408 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1409 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1411 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1412 memset(hisi_hba
->cmd_hdr
[i
], 0, s
);
1415 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1416 memset(hisi_hba
->complete_hdr
[i
], 0, s
);
1420 s
= sizeof(struct hisi_sas_initial_fis
) * hisi_hba
->n_phy
;
1421 memset(hisi_hba
->initial_fis
, 0, s
);
1423 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1424 memset(hisi_hba
->iost
, 0, s
);
1426 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1427 memset(hisi_hba
->breakpoint
, 0, s
);
1429 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
) * 2;
1430 memset(hisi_hba
->sata_breakpoint
, 0, s
);
1432 EXPORT_SYMBOL_GPL(hisi_sas_init_mem
);
1434 static int hisi_sas_alloc(struct hisi_hba
*hisi_hba
, struct Scsi_Host
*shost
)
1436 struct platform_device
*pdev
= hisi_hba
->pdev
;
1437 struct device
*dev
= &pdev
->dev
;
1438 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1440 spin_lock_init(&hisi_hba
->lock
);
1441 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1442 hisi_sas_phy_init(hisi_hba
, i
);
1443 hisi_hba
->port
[i
].port_attached
= 0;
1444 hisi_hba
->port
[i
].id
= -1;
1447 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1448 hisi_hba
->devices
[i
].dev_type
= SAS_PHY_UNUSED
;
1449 hisi_hba
->devices
[i
].device_id
= i
;
1450 hisi_hba
->devices
[i
].dev_status
= HISI_SAS_DEV_NORMAL
;
1453 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1454 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1455 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1457 /* Completion queue structure */
1459 cq
->hisi_hba
= hisi_hba
;
1461 /* Delivery queue structure */
1463 dq
->hisi_hba
= hisi_hba
;
1465 /* Delivery queue */
1466 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1467 hisi_hba
->cmd_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1468 &hisi_hba
->cmd_hdr_dma
[i
], GFP_KERNEL
);
1469 if (!hisi_hba
->cmd_hdr
[i
])
1472 /* Completion queue */
1473 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1474 hisi_hba
->complete_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1475 &hisi_hba
->complete_hdr_dma
[i
], GFP_KERNEL
);
1476 if (!hisi_hba
->complete_hdr
[i
])
1480 s
= HISI_SAS_STATUS_BUF_SZ
;
1481 hisi_hba
->status_buffer_pool
= dma_pool_create("status_buffer",
1483 if (!hisi_hba
->status_buffer_pool
)
1486 s
= HISI_SAS_COMMAND_TABLE_SZ
;
1487 hisi_hba
->command_table_pool
= dma_pool_create("command_table",
1489 if (!hisi_hba
->command_table_pool
)
1492 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1493 hisi_hba
->itct
= dma_alloc_coherent(dev
, s
, &hisi_hba
->itct_dma
,
1495 if (!hisi_hba
->itct
)
1498 memset(hisi_hba
->itct
, 0, s
);
1500 hisi_hba
->slot_info
= devm_kcalloc(dev
, max_command_entries
,
1501 sizeof(struct hisi_sas_slot
),
1503 if (!hisi_hba
->slot_info
)
1506 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1507 hisi_hba
->iost
= dma_alloc_coherent(dev
, s
, &hisi_hba
->iost_dma
,
1509 if (!hisi_hba
->iost
)
1512 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1513 hisi_hba
->breakpoint
= dma_alloc_coherent(dev
, s
,
1514 &hisi_hba
->breakpoint_dma
, GFP_KERNEL
);
1515 if (!hisi_hba
->breakpoint
)
1518 hisi_hba
->slot_index_count
= max_command_entries
;
1519 s
= hisi_hba
->slot_index_count
/ BITS_PER_BYTE
;
1520 hisi_hba
->slot_index_tags
= devm_kzalloc(dev
, s
, GFP_KERNEL
);
1521 if (!hisi_hba
->slot_index_tags
)
1524 hisi_hba
->sge_page_pool
= dma_pool_create("status_sge", dev
,
1525 sizeof(struct hisi_sas_sge_page
), 16, 0);
1526 if (!hisi_hba
->sge_page_pool
)
1529 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1530 hisi_hba
->initial_fis
= dma_alloc_coherent(dev
, s
,
1531 &hisi_hba
->initial_fis_dma
, GFP_KERNEL
);
1532 if (!hisi_hba
->initial_fis
)
1535 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
) * 2;
1536 hisi_hba
->sata_breakpoint
= dma_alloc_coherent(dev
, s
,
1537 &hisi_hba
->sata_breakpoint_dma
, GFP_KERNEL
);
1538 if (!hisi_hba
->sata_breakpoint
)
1540 hisi_sas_init_mem(hisi_hba
);
1542 hisi_sas_slot_index_init(hisi_hba
);
1544 hisi_hba
->wq
= create_singlethread_workqueue(dev_name(dev
));
1545 if (!hisi_hba
->wq
) {
1546 dev_err(dev
, "sas_alloc: failed to create workqueue\n");
1555 static void hisi_sas_free(struct hisi_hba
*hisi_hba
)
1557 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1558 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1560 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1561 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1562 if (hisi_hba
->cmd_hdr
[i
])
1563 dma_free_coherent(dev
, s
,
1564 hisi_hba
->cmd_hdr
[i
],
1565 hisi_hba
->cmd_hdr_dma
[i
]);
1567 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1568 if (hisi_hba
->complete_hdr
[i
])
1569 dma_free_coherent(dev
, s
,
1570 hisi_hba
->complete_hdr
[i
],
1571 hisi_hba
->complete_hdr_dma
[i
]);
1574 dma_pool_destroy(hisi_hba
->status_buffer_pool
);
1575 dma_pool_destroy(hisi_hba
->command_table_pool
);
1576 dma_pool_destroy(hisi_hba
->sge_page_pool
);
1578 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1580 dma_free_coherent(dev
, s
,
1581 hisi_hba
->itct
, hisi_hba
->itct_dma
);
1583 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1585 dma_free_coherent(dev
, s
,
1586 hisi_hba
->iost
, hisi_hba
->iost_dma
);
1588 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1589 if (hisi_hba
->breakpoint
)
1590 dma_free_coherent(dev
, s
,
1591 hisi_hba
->breakpoint
,
1592 hisi_hba
->breakpoint_dma
);
1595 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1596 if (hisi_hba
->initial_fis
)
1597 dma_free_coherent(dev
, s
,
1598 hisi_hba
->initial_fis
,
1599 hisi_hba
->initial_fis_dma
);
1601 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
) * 2;
1602 if (hisi_hba
->sata_breakpoint
)
1603 dma_free_coherent(dev
, s
,
1604 hisi_hba
->sata_breakpoint
,
1605 hisi_hba
->sata_breakpoint_dma
);
1608 destroy_workqueue(hisi_hba
->wq
);
1611 static void hisi_sas_rst_work_handler(struct work_struct
*work
)
1613 struct hisi_hba
*hisi_hba
=
1614 container_of(work
, struct hisi_hba
, rst_work
);
1616 hisi_sas_controller_reset(hisi_hba
);
1619 static struct Scsi_Host
*hisi_sas_shost_alloc(struct platform_device
*pdev
,
1620 const struct hisi_sas_hw
*hw
)
1622 struct resource
*res
;
1623 struct Scsi_Host
*shost
;
1624 struct hisi_hba
*hisi_hba
;
1625 struct device
*dev
= &pdev
->dev
;
1626 struct device_node
*np
= pdev
->dev
.of_node
;
1629 shost
= scsi_host_alloc(&hisi_sas_sht
, sizeof(*hisi_hba
));
1631 dev_err(dev
, "scsi host alloc failed\n");
1634 hisi_hba
= shost_priv(shost
);
1636 INIT_WORK(&hisi_hba
->rst_work
, hisi_sas_rst_work_handler
);
1638 hisi_hba
->pdev
= pdev
;
1639 hisi_hba
->shost
= shost
;
1640 SHOST_TO_SAS_HA(shost
) = &hisi_hba
->sha
;
1642 init_timer(&hisi_hba
->timer
);
1644 if (device_property_read_u8_array(dev
, "sas-addr", hisi_hba
->sas_addr
,
1649 hisi_hba
->ctrl
= syscon_regmap_lookup_by_phandle(np
,
1650 "hisilicon,sas-syscon");
1651 if (IS_ERR(hisi_hba
->ctrl
))
1654 if (device_property_read_u32(dev
, "ctrl-reset-reg",
1655 &hisi_hba
->ctrl_reset_reg
))
1658 if (device_property_read_u32(dev
, "ctrl-reset-sts-reg",
1659 &hisi_hba
->ctrl_reset_sts_reg
))
1662 if (device_property_read_u32(dev
, "ctrl-clock-ena-reg",
1663 &hisi_hba
->ctrl_clock_ena_reg
))
1667 refclk
= devm_clk_get(&pdev
->dev
, NULL
);
1669 dev_dbg(dev
, "no ref clk property\n");
1671 hisi_hba
->refclk_frequency_mhz
= clk_get_rate(refclk
) / 1000000;
1673 if (device_property_read_u32(dev
, "phy-count", &hisi_hba
->n_phy
))
1676 if (device_property_read_u32(dev
, "queue-count",
1677 &hisi_hba
->queue_count
))
1680 if (dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64)) &&
1681 dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32))) {
1682 dev_err(dev
, "No usable DMA addressing method\n");
1686 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1687 hisi_hba
->regs
= devm_ioremap_resource(dev
, res
);
1688 if (IS_ERR(hisi_hba
->regs
))
1691 if (hisi_sas_alloc(hisi_hba
, shost
)) {
1692 hisi_sas_free(hisi_hba
);
1699 dev_err(dev
, "shost alloc failed\n");
1703 static void hisi_sas_init_add(struct hisi_hba
*hisi_hba
)
1707 for (i
= 0; i
< hisi_hba
->n_phy
; i
++)
1708 memcpy(&hisi_hba
->phy
[i
].dev_sas_addr
,
1713 int hisi_sas_probe(struct platform_device
*pdev
,
1714 const struct hisi_sas_hw
*hw
)
1716 struct Scsi_Host
*shost
;
1717 struct hisi_hba
*hisi_hba
;
1718 struct device
*dev
= &pdev
->dev
;
1719 struct asd_sas_phy
**arr_phy
;
1720 struct asd_sas_port
**arr_port
;
1721 struct sas_ha_struct
*sha
;
1722 int rc
, phy_nr
, port_nr
, i
;
1724 shost
= hisi_sas_shost_alloc(pdev
, hw
);
1728 sha
= SHOST_TO_SAS_HA(shost
);
1729 hisi_hba
= shost_priv(shost
);
1730 platform_set_drvdata(pdev
, sha
);
1732 phy_nr
= port_nr
= hisi_hba
->n_phy
;
1734 arr_phy
= devm_kcalloc(dev
, phy_nr
, sizeof(void *), GFP_KERNEL
);
1735 arr_port
= devm_kcalloc(dev
, port_nr
, sizeof(void *), GFP_KERNEL
);
1736 if (!arr_phy
|| !arr_port
) {
1741 sha
->sas_phy
= arr_phy
;
1742 sha
->sas_port
= arr_port
;
1743 sha
->lldd_ha
= hisi_hba
;
1745 shost
->transportt
= hisi_sas_stt
;
1746 shost
->max_id
= HISI_SAS_MAX_DEVICES
;
1747 shost
->max_lun
= ~0;
1748 shost
->max_channel
= 1;
1749 shost
->max_cmd_len
= 16;
1750 shost
->sg_tablesize
= min_t(u16
, SG_ALL
, HISI_SAS_SGE_PAGE_CNT
);
1751 shost
->can_queue
= hisi_hba
->hw
->max_command_entries
;
1752 shost
->cmd_per_lun
= hisi_hba
->hw
->max_command_entries
;
1754 sha
->sas_ha_name
= DRV_NAME
;
1755 sha
->dev
= &hisi_hba
->pdev
->dev
;
1756 sha
->lldd_module
= THIS_MODULE
;
1757 sha
->sas_addr
= &hisi_hba
->sas_addr
[0];
1758 sha
->num_phys
= hisi_hba
->n_phy
;
1759 sha
->core
.shost
= hisi_hba
->shost
;
1761 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1762 sha
->sas_phy
[i
] = &hisi_hba
->phy
[i
].sas_phy
;
1763 sha
->sas_port
[i
] = &hisi_hba
->port
[i
].sas_port
;
1766 hisi_sas_init_add(hisi_hba
);
1768 rc
= scsi_add_host(shost
, &pdev
->dev
);
1772 rc
= sas_register_ha(sha
);
1774 goto err_out_register_ha
;
1776 rc
= hisi_hba
->hw
->hw_init(hisi_hba
);
1778 goto err_out_register_ha
;
1780 scsi_scan_host(shost
);
1784 err_out_register_ha
:
1785 scsi_remove_host(shost
);
1787 hisi_sas_free(hisi_hba
);
1791 EXPORT_SYMBOL_GPL(hisi_sas_probe
);
1793 int hisi_sas_remove(struct platform_device
*pdev
)
1795 struct sas_ha_struct
*sha
= platform_get_drvdata(pdev
);
1796 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
1797 struct Scsi_Host
*shost
= sha
->core
.shost
;
1799 sas_unregister_ha(sha
);
1800 sas_remove_host(sha
->core
.shost
);
1802 hisi_sas_free(hisi_hba
);
1806 EXPORT_SYMBOL_GPL(hisi_sas_remove
);
1808 static __init
int hisi_sas_init(void)
1810 pr_info("hisi_sas: driver version %s\n", DRV_VERSION
);
1812 hisi_sas_stt
= sas_domain_attach_transport(&hisi_sas_transport_ops
);
1819 static __exit
void hisi_sas_exit(void)
1821 sas_release_transport(hisi_sas_stt
);
1824 module_init(hisi_sas_init
);
1825 module_exit(hisi_sas_exit
);
1827 MODULE_VERSION(DRV_VERSION
);
1828 MODULE_LICENSE("GPL");
1829 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1830 MODULE_DESCRIPTION("HISILICON SAS controller driver");
1831 MODULE_ALIAS("platform:" DRV_NAME
);