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
);
25 static struct hisi_hba
*dev_to_hisi_hba(struct domain_device
*device
)
27 return device
->port
->ha
->lldd_ha
;
30 static void hisi_sas_slot_index_clear(struct hisi_hba
*hisi_hba
, int slot_idx
)
32 void *bitmap
= hisi_hba
->slot_index_tags
;
34 clear_bit(slot_idx
, bitmap
);
37 static void hisi_sas_slot_index_free(struct hisi_hba
*hisi_hba
, int slot_idx
)
39 hisi_sas_slot_index_clear(hisi_hba
, slot_idx
);
42 static void hisi_sas_slot_index_set(struct hisi_hba
*hisi_hba
, int slot_idx
)
44 void *bitmap
= hisi_hba
->slot_index_tags
;
46 set_bit(slot_idx
, bitmap
);
49 static int hisi_sas_slot_index_alloc(struct hisi_hba
*hisi_hba
, int *slot_idx
)
52 void *bitmap
= hisi_hba
->slot_index_tags
;
54 index
= find_first_zero_bit(bitmap
, hisi_hba
->slot_index_count
);
55 if (index
>= hisi_hba
->slot_index_count
)
56 return -SAS_QUEUE_FULL
;
57 hisi_sas_slot_index_set(hisi_hba
, index
);
62 static void hisi_sas_slot_index_init(struct hisi_hba
*hisi_hba
)
66 for (i
= 0; i
< hisi_hba
->slot_index_count
; ++i
)
67 hisi_sas_slot_index_clear(hisi_hba
, i
);
70 void hisi_sas_slot_task_free(struct hisi_hba
*hisi_hba
, struct sas_task
*task
,
71 struct hisi_sas_slot
*slot
)
73 struct device
*dev
= &hisi_hba
->pdev
->dev
;
78 if (!sas_protocol_ata(task
->task_proto
))
80 dma_unmap_sg(dev
, task
->scatter
, slot
->n_elem
,
83 if (slot
->command_table
)
84 dma_pool_free(hisi_hba
->command_table_pool
,
85 slot
->command_table
, slot
->command_table_dma
);
87 if (slot
->status_buffer
)
88 dma_pool_free(hisi_hba
->status_buffer_pool
,
89 slot
->status_buffer
, slot
->status_buffer_dma
);
92 dma_pool_free(hisi_hba
->sge_page_pool
, slot
->sge_page
,
95 list_del_init(&slot
->entry
);
96 task
->lldd_task
= NULL
;
99 hisi_sas_slot_index_free(hisi_hba
, slot
->idx
);
100 /* slot memory is fully zeroed when it is reused */
102 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free
);
104 static int hisi_sas_task_prep_smp(struct hisi_hba
*hisi_hba
,
105 struct hisi_sas_slot
*slot
)
107 return hisi_hba
->hw
->prep_smp(hisi_hba
, slot
);
110 static int hisi_sas_task_prep_ssp(struct hisi_hba
*hisi_hba
,
111 struct hisi_sas_slot
*slot
, int is_tmf
,
112 struct hisi_sas_tmf_task
*tmf
)
114 return hisi_hba
->hw
->prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
117 static int hisi_sas_task_prep_ata(struct hisi_hba
*hisi_hba
,
118 struct hisi_sas_slot
*slot
)
120 return hisi_hba
->hw
->prep_stp(hisi_hba
, slot
);
123 static int hisi_sas_task_prep_abort(struct hisi_hba
*hisi_hba
,
124 struct hisi_sas_slot
*slot
,
125 int device_id
, int abort_flag
, int tag_to_abort
)
127 return hisi_hba
->hw
->prep_abort(hisi_hba
, slot
,
128 device_id
, abort_flag
, tag_to_abort
);
132 * This function will issue an abort TMF regardless of whether the
133 * task is in the sdev or not. Then it will do the task complete
134 * cleanup and callbacks.
136 static void hisi_sas_slot_abort(struct work_struct
*work
)
138 struct hisi_sas_slot
*abort_slot
=
139 container_of(work
, struct hisi_sas_slot
, abort_slot
);
140 struct sas_task
*task
= abort_slot
->task
;
141 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
142 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
143 struct hisi_sas_tmf_task tmf_task
;
144 struct domain_device
*device
= task
->dev
;
145 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
147 struct device
*dev
= &hisi_hba
->pdev
->dev
;
148 int tag
= abort_slot
->idx
;
150 if (!(task
->task_proto
& SAS_PROTOCOL_SSP
)) {
151 dev_err(dev
, "cannot abort slot for non-ssp task\n");
155 int_to_scsilun(cmnd
->device
->lun
, &lun
);
156 tmf_task
.tmf
= TMF_ABORT_TASK
;
157 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
159 hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
, &tmf_task
);
161 /* Do cleanup for this task */
162 hisi_sas_slot_task_free(hisi_hba
, task
, abort_slot
);
164 task
->task_done(task
);
166 atomic64_dec(&sas_dev
->running_req
);
169 static int hisi_sas_task_prep(struct sas_task
*task
, struct hisi_hba
*hisi_hba
,
170 int is_tmf
, struct hisi_sas_tmf_task
*tmf
,
173 struct domain_device
*device
= task
->dev
;
174 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
175 struct hisi_sas_port
*port
;
176 struct hisi_sas_slot
*slot
;
177 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
178 struct device
*dev
= &hisi_hba
->pdev
->dev
;
179 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
182 struct task_status_struct
*ts
= &task
->task_status
;
184 ts
->resp
= SAS_TASK_UNDELIVERED
;
185 ts
->stat
= SAS_PHY_DOWN
;
187 * libsas will use dev->port, should
188 * not call task_done for sata
190 if (device
->dev_type
!= SAS_SATA_DEV
)
191 task
->task_done(task
);
195 if (DEV_IS_GONE(sas_dev
)) {
197 dev_info(dev
, "task prep: device %llu not ready\n",
200 dev_info(dev
, "task prep: device %016llx not ready\n",
201 SAS_ADDR(device
->sas_addr
));
206 port
= device
->port
->lldd_port
;
207 if (port
&& !port
->port_attached
) {
208 dev_info(dev
, "task prep: %s port%d not attach device\n",
209 (sas_protocol_ata(task
->task_proto
)) ?
216 if (!sas_protocol_ata(task
->task_proto
)) {
217 if (task
->num_scatter
) {
218 n_elem
= dma_map_sg(dev
, task
->scatter
,
219 task
->num_scatter
, task
->data_dir
);
226 n_elem
= task
->num_scatter
;
228 if (hisi_hba
->hw
->slot_index_alloc
)
229 rc
= hisi_hba
->hw
->slot_index_alloc(hisi_hba
, &slot_idx
,
232 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
235 rc
= hisi_hba
->hw
->get_free_slot(hisi_hba
, sas_dev
->device_id
,
236 &dlvry_queue
, &dlvry_queue_slot
);
240 slot
= &hisi_hba
->slot_info
[slot_idx
];
241 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
243 slot
->idx
= slot_idx
;
244 slot
->n_elem
= n_elem
;
245 slot
->dlvry_queue
= dlvry_queue
;
246 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
247 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
248 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
251 task
->lldd_task
= slot
;
252 INIT_WORK(&slot
->abort_slot
, hisi_sas_slot_abort
);
254 slot
->status_buffer
= dma_pool_alloc(hisi_hba
->status_buffer_pool
,
256 &slot
->status_buffer_dma
);
257 if (!slot
->status_buffer
) {
259 goto err_out_slot_buf
;
261 memset(slot
->status_buffer
, 0, HISI_SAS_STATUS_BUF_SZ
);
263 slot
->command_table
= dma_pool_alloc(hisi_hba
->command_table_pool
,
265 &slot
->command_table_dma
);
266 if (!slot
->command_table
) {
268 goto err_out_status_buf
;
270 memset(slot
->command_table
, 0, HISI_SAS_COMMAND_TABLE_SZ
);
271 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
273 switch (task
->task_proto
) {
274 case SAS_PROTOCOL_SMP
:
275 rc
= hisi_sas_task_prep_smp(hisi_hba
, slot
);
277 case SAS_PROTOCOL_SSP
:
278 rc
= hisi_sas_task_prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
280 case SAS_PROTOCOL_SATA
:
281 case SAS_PROTOCOL_STP
:
282 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:
283 rc
= hisi_sas_task_prep_ata(hisi_hba
, slot
);
286 dev_err(dev
, "task prep: unknown/unsupported proto (0x%x)\n",
293 dev_err(dev
, "task prep: rc = 0x%x\n", rc
);
296 goto err_out_command_table
;
299 list_add_tail(&slot
->entry
, &port
->list
);
300 spin_lock(&task
->task_state_lock
);
301 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
302 spin_unlock(&task
->task_state_lock
);
304 hisi_hba
->slot_prep
= slot
;
306 atomic64_inc(&sas_dev
->running_req
);
312 dma_pool_free(hisi_hba
->sge_page_pool
, slot
->sge_page
,
314 err_out_command_table
:
315 dma_pool_free(hisi_hba
->command_table_pool
, slot
->command_table
,
316 slot
->command_table_dma
);
318 dma_pool_free(hisi_hba
->status_buffer_pool
, slot
->status_buffer
,
319 slot
->status_buffer_dma
);
321 /* Nothing to be done */
323 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
325 dev_err(dev
, "task prep: failed[%d]!\n", rc
);
326 if (!sas_protocol_ata(task
->task_proto
))
328 dma_unmap_sg(dev
, task
->scatter
, n_elem
,
334 static int hisi_sas_task_exec(struct sas_task
*task
, gfp_t gfp_flags
,
335 int is_tmf
, struct hisi_sas_tmf_task
*tmf
)
340 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
341 struct device
*dev
= &hisi_hba
->pdev
->dev
;
343 /* protect task_prep and start_delivery sequence */
344 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
345 rc
= hisi_sas_task_prep(task
, hisi_hba
, is_tmf
, tmf
, &pass
);
347 dev_err(dev
, "task exec: failed[%d]!\n", rc
);
350 hisi_hba
->hw
->start_delivery(hisi_hba
);
351 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
356 static void hisi_sas_bytes_dmaed(struct hisi_hba
*hisi_hba
, int phy_no
)
358 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
359 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
360 struct sas_ha_struct
*sas_ha
;
362 if (!phy
->phy_attached
)
365 sas_ha
= &hisi_hba
->sha
;
366 sas_ha
->notify_phy_event(sas_phy
, PHYE_OOB_DONE
);
369 struct sas_phy
*sphy
= sas_phy
->phy
;
371 sphy
->negotiated_linkrate
= sas_phy
->linkrate
;
372 sphy
->minimum_linkrate_hw
= SAS_LINK_RATE_1_5_GBPS
;
373 sphy
->maximum_linkrate_hw
=
374 hisi_hba
->hw
->phy_get_max_linkrate();
375 if (sphy
->minimum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
376 sphy
->minimum_linkrate
= phy
->minimum_linkrate
;
378 if (sphy
->maximum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
379 sphy
->maximum_linkrate
= phy
->maximum_linkrate
;
382 if (phy
->phy_type
& PORT_TYPE_SAS
) {
383 struct sas_identify_frame
*id
;
385 id
= (struct sas_identify_frame
*)phy
->frame_rcvd
;
386 id
->dev_type
= phy
->identify
.device_type
;
387 id
->initiator_bits
= SAS_PROTOCOL_ALL
;
388 id
->target_bits
= phy
->identify
.target_port_protocols
;
389 } else if (phy
->phy_type
& PORT_TYPE_SATA
) {
393 sas_phy
->frame_rcvd_size
= phy
->frame_rcvd_size
;
394 sas_ha
->notify_port_event(sas_phy
, PORTE_BYTES_DMAED
);
397 static struct hisi_sas_device
*hisi_sas_alloc_dev(struct domain_device
*device
)
399 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
400 struct hisi_sas_device
*sas_dev
= NULL
;
403 spin_lock(&hisi_hba
->lock
);
404 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
405 if (hisi_hba
->devices
[i
].dev_type
== SAS_PHY_UNUSED
) {
406 hisi_hba
->devices
[i
].device_id
= i
;
407 sas_dev
= &hisi_hba
->devices
[i
];
408 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
409 sas_dev
->dev_type
= device
->dev_type
;
410 sas_dev
->hisi_hba
= hisi_hba
;
411 sas_dev
->sas_device
= device
;
415 spin_unlock(&hisi_hba
->lock
);
420 static int hisi_sas_dev_found(struct domain_device
*device
)
422 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
423 struct domain_device
*parent_dev
= device
->parent
;
424 struct hisi_sas_device
*sas_dev
;
425 struct device
*dev
= &hisi_hba
->pdev
->dev
;
427 if (hisi_hba
->hw
->alloc_dev
)
428 sas_dev
= hisi_hba
->hw
->alloc_dev(device
);
430 sas_dev
= hisi_sas_alloc_dev(device
);
432 dev_err(dev
, "fail alloc dev: max support %d devices\n",
433 HISI_SAS_MAX_DEVICES
);
437 device
->lldd_dev
= sas_dev
;
438 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
440 if (parent_dev
&& DEV_IS_EXPANDER(parent_dev
->dev_type
)) {
442 u8 phy_num
= parent_dev
->ex_dev
.num_phys
;
445 for (phy_no
= 0; phy_no
< phy_num
; phy_no
++) {
446 phy
= &parent_dev
->ex_dev
.ex_phy
[phy_no
];
447 if (SAS_ADDR(phy
->attached_sas_addr
) ==
448 SAS_ADDR(device
->sas_addr
)) {
449 sas_dev
->attached_phy
= phy_no
;
454 if (phy_no
== phy_num
) {
455 dev_info(dev
, "dev found: no attached "
456 "dev:%016llx at ex:%016llx\n",
457 SAS_ADDR(device
->sas_addr
),
458 SAS_ADDR(parent_dev
->sas_addr
));
466 static int hisi_sas_slave_configure(struct scsi_device
*sdev
)
468 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
469 int ret
= sas_slave_configure(sdev
);
473 if (!dev_is_sata(dev
))
474 sas_change_queue_depth(sdev
, 64);
479 static void hisi_sas_scan_start(struct Scsi_Host
*shost
)
481 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
484 for (i
= 0; i
< hisi_hba
->n_phy
; ++i
)
485 hisi_sas_bytes_dmaed(hisi_hba
, i
);
487 hisi_hba
->scan_finished
= 1;
490 static int hisi_sas_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
492 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
493 struct sas_ha_struct
*sha
= &hisi_hba
->sha
;
495 if (hisi_hba
->scan_finished
== 0)
502 static void hisi_sas_phyup_work(struct work_struct
*work
)
504 struct hisi_sas_phy
*phy
=
505 container_of(work
, struct hisi_sas_phy
, phyup_ws
);
506 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
507 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
508 int phy_no
= sas_phy
->id
;
510 hisi_hba
->hw
->sl_notify(hisi_hba
, phy_no
); /* This requires a sleep */
511 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
514 static void hisi_sas_phy_init(struct hisi_hba
*hisi_hba
, int phy_no
)
516 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
517 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
519 phy
->hisi_hba
= hisi_hba
;
521 init_timer(&phy
->timer
);
522 sas_phy
->enabled
= (phy_no
< hisi_hba
->n_phy
) ? 1 : 0;
523 sas_phy
->class = SAS
;
524 sas_phy
->iproto
= SAS_PROTOCOL_ALL
;
526 sas_phy
->type
= PHY_TYPE_PHYSICAL
;
527 sas_phy
->role
= PHY_ROLE_INITIATOR
;
528 sas_phy
->oob_mode
= OOB_NOT_CONNECTED
;
529 sas_phy
->linkrate
= SAS_LINK_RATE_UNKNOWN
;
530 sas_phy
->id
= phy_no
;
531 sas_phy
->sas_addr
= &hisi_hba
->sas_addr
[0];
532 sas_phy
->frame_rcvd
= &phy
->frame_rcvd
[0];
533 sas_phy
->ha
= (struct sas_ha_struct
*)hisi_hba
->shost
->hostdata
;
534 sas_phy
->lldd_phy
= phy
;
536 INIT_WORK(&phy
->phyup_ws
, hisi_sas_phyup_work
);
539 static void hisi_sas_port_notify_formed(struct asd_sas_phy
*sas_phy
)
541 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
542 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
543 struct hisi_sas_phy
*phy
= sas_phy
->lldd_phy
;
544 struct asd_sas_port
*sas_port
= sas_phy
->port
;
545 struct hisi_sas_port
*port
= &hisi_hba
->port
[phy
->port_id
];
551 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
552 port
->port_attached
= 1;
553 port
->id
= phy
->port_id
;
555 sas_port
->lldd_port
= port
;
556 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
559 static void hisi_sas_do_release_task(struct hisi_hba
*hisi_hba
, int phy_no
,
560 struct domain_device
*device
)
562 struct hisi_sas_phy
*phy
;
563 struct hisi_sas_port
*port
;
564 struct hisi_sas_slot
*slot
, *slot2
;
565 struct device
*dev
= &hisi_hba
->pdev
->dev
;
567 phy
= &hisi_hba
->phy
[phy_no
];
572 list_for_each_entry_safe(slot
, slot2
, &port
->list
, entry
) {
573 struct sas_task
*task
;
576 if (device
&& task
->dev
!= device
)
579 dev_info(dev
, "Release slot [%d:%d], task [%p]:\n",
580 slot
->dlvry_queue
, slot
->dlvry_queue_slot
, task
);
581 hisi_hba
->hw
->slot_complete(hisi_hba
, slot
, 1);
585 static void hisi_sas_port_notify_deformed(struct asd_sas_phy
*sas_phy
)
587 struct domain_device
*device
;
588 struct hisi_sas_phy
*phy
= sas_phy
->lldd_phy
;
589 struct asd_sas_port
*sas_port
= sas_phy
->port
;
591 list_for_each_entry(device
, &sas_port
->dev_list
, dev_list_node
)
592 hisi_sas_do_release_task(phy
->hisi_hba
, sas_phy
->id
, device
);
595 static void hisi_sas_release_task(struct hisi_hba
*hisi_hba
,
596 struct domain_device
*device
)
598 struct asd_sas_port
*port
= device
->port
;
599 struct asd_sas_phy
*sas_phy
;
601 list_for_each_entry(sas_phy
, &port
->phy_list
, port_phy_el
)
602 hisi_sas_do_release_task(hisi_hba
, sas_phy
->id
, device
);
605 static void hisi_sas_dev_gone(struct domain_device
*device
)
607 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
608 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
609 struct device
*dev
= &hisi_hba
->pdev
->dev
;
610 u64 dev_id
= sas_dev
->device_id
;
612 dev_info(dev
, "found dev[%lld:%x] is gone\n",
613 sas_dev
->device_id
, sas_dev
->dev_type
);
615 hisi_sas_internal_task_abort(hisi_hba
, device
,
616 HISI_SAS_INT_ABT_DEV
, 0);
618 hisi_hba
->hw
->free_device(hisi_hba
, sas_dev
);
619 device
->lldd_dev
= NULL
;
620 memset(sas_dev
, 0, sizeof(*sas_dev
));
621 sas_dev
->device_id
= dev_id
;
622 sas_dev
->dev_type
= SAS_PHY_UNUSED
;
623 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
626 static int hisi_sas_queue_command(struct sas_task
*task
, gfp_t gfp_flags
)
628 return hisi_sas_task_exec(task
, gfp_flags
, 0, NULL
);
631 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
634 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
635 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
636 int phy_no
= sas_phy
->id
;
639 case PHY_FUNC_HARD_RESET
:
640 hisi_hba
->hw
->phy_hard_reset(hisi_hba
, phy_no
);
643 case PHY_FUNC_LINK_RESET
:
644 hisi_hba
->hw
->phy_enable(hisi_hba
, phy_no
);
645 hisi_hba
->hw
->phy_hard_reset(hisi_hba
, phy_no
);
648 case PHY_FUNC_DISABLE
:
649 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
652 case PHY_FUNC_SET_LINK_RATE
:
653 hisi_hba
->hw
->phy_set_linkrate(hisi_hba
, phy_no
, funcdata
);
656 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
663 static void hisi_sas_task_done(struct sas_task
*task
)
665 if (!del_timer(&task
->slow_task
->timer
))
667 complete(&task
->slow_task
->completion
);
670 static void hisi_sas_tmf_timedout(unsigned long data
)
672 struct sas_task
*task
= (struct sas_task
*)data
;
674 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
675 complete(&task
->slow_task
->completion
);
678 #define TASK_TIMEOUT 20
680 static int hisi_sas_exec_internal_tmf_task(struct domain_device
*device
,
681 void *parameter
, u32 para_len
,
682 struct hisi_sas_tmf_task
*tmf
)
684 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
685 struct hisi_hba
*hisi_hba
= sas_dev
->hisi_hba
;
686 struct device
*dev
= &hisi_hba
->pdev
->dev
;
687 struct sas_task
*task
;
690 for (retry
= 0; retry
< TASK_RETRY
; retry
++) {
691 task
= sas_alloc_slow_task(GFP_KERNEL
);
696 task
->task_proto
= device
->tproto
;
698 memcpy(&task
->ssp_task
, parameter
, para_len
);
699 task
->task_done
= hisi_sas_task_done
;
701 task
->slow_task
->timer
.data
= (unsigned long) task
;
702 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
703 task
->slow_task
->timer
.expires
= jiffies
+ TASK_TIMEOUT
*HZ
;
704 add_timer(&task
->slow_task
->timer
);
706 res
= hisi_sas_task_exec(task
, GFP_KERNEL
, 1, tmf
);
709 del_timer(&task
->slow_task
->timer
);
710 dev_err(dev
, "abort tmf: executing internal task failed: %d\n",
715 wait_for_completion(&task
->slow_task
->completion
);
716 res
= TMF_RESP_FUNC_FAILED
;
717 /* Even TMF timed out, return direct. */
718 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
719 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
720 dev_err(dev
, "abort tmf: TMF task[%d] timeout\n",
721 tmf
->tag_of_task_to_be_managed
);
722 if (task
->lldd_task
) {
723 struct hisi_sas_slot
*slot
=
726 hisi_sas_slot_task_free(hisi_hba
,
734 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
735 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
736 res
= TMF_RESP_FUNC_COMPLETE
;
740 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
741 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
742 res
= TMF_RESP_FUNC_SUCC
;
746 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
747 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
748 /* no error, but return the number of bytes of
751 dev_warn(dev
, "abort tmf: task to dev %016llx "
752 "resp: 0x%x sts 0x%x underrun\n",
753 SAS_ADDR(device
->sas_addr
),
754 task
->task_status
.resp
,
755 task
->task_status
.stat
);
756 res
= task
->task_status
.residual
;
760 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
761 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
762 dev_warn(dev
, "abort tmf: blocked task error\n");
767 dev_warn(dev
, "abort tmf: task to dev "
768 "%016llx resp: 0x%x status 0x%x\n",
769 SAS_ADDR(device
->sas_addr
), task
->task_status
.resp
,
770 task
->task_status
.stat
);
775 if (retry
== TASK_RETRY
)
776 dev_warn(dev
, "abort tmf: executing internal task failed!\n");
781 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device
*device
,
782 u8
*lun
, struct hisi_sas_tmf_task
*tmf
)
784 struct sas_ssp_task ssp_task
;
786 if (!(device
->tproto
& SAS_PROTOCOL_SSP
))
787 return TMF_RESP_FUNC_ESUPP
;
789 memcpy(ssp_task
.LUN
, lun
, 8);
791 return hisi_sas_exec_internal_tmf_task(device
, &ssp_task
,
792 sizeof(ssp_task
), tmf
);
795 static int hisi_sas_abort_task(struct sas_task
*task
)
798 struct hisi_sas_tmf_task tmf_task
;
799 struct domain_device
*device
= task
->dev
;
800 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
801 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
802 struct device
*dev
= &hisi_hba
->pdev
->dev
;
803 int rc
= TMF_RESP_FUNC_FAILED
;
807 dev_warn(dev
, "Device has been removed\n");
808 return TMF_RESP_FUNC_FAILED
;
811 spin_lock_irqsave(&task
->task_state_lock
, flags
);
812 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
813 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
814 rc
= TMF_RESP_FUNC_COMPLETE
;
818 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
819 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
820 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
821 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
822 struct hisi_sas_slot
*slot
= task
->lldd_task
;
825 int_to_scsilun(cmnd
->device
->lun
, &lun
);
826 tmf_task
.tmf
= TMF_ABORT_TASK
;
827 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
829 rc
= hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
,
832 /* if successful, clear the task and callback forwards.*/
833 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
834 if (task
->lldd_task
) {
835 struct hisi_sas_slot
*slot
;
837 slot
= &hisi_hba
->slot_info
838 [tmf_task
.tag_of_task_to_be_managed
];
839 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
840 hisi_hba
->hw
->slot_complete(hisi_hba
, slot
, 1);
841 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
845 hisi_sas_internal_task_abort(hisi_hba
, device
,
846 HISI_SAS_INT_ABT_CMD
, tag
);
847 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
848 task
->task_proto
& SAS_PROTOCOL_STP
) {
849 if (task
->dev
->dev_type
== SAS_SATA_DEV
) {
850 hisi_sas_internal_task_abort(hisi_hba
, device
,
851 HISI_SAS_INT_ABT_DEV
, 0);
852 rc
= TMF_RESP_FUNC_COMPLETE
;
854 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
856 struct hisi_sas_slot
*slot
= task
->lldd_task
;
859 hisi_sas_internal_task_abort(hisi_hba
, device
,
860 HISI_SAS_INT_ABT_CMD
, tag
);
864 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
865 dev_notice(dev
, "abort task: rc=%d\n", rc
);
869 static int hisi_sas_abort_task_set(struct domain_device
*device
, u8
*lun
)
871 struct hisi_sas_tmf_task tmf_task
;
872 int rc
= TMF_RESP_FUNC_FAILED
;
874 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
875 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
880 static int hisi_sas_clear_aca(struct domain_device
*device
, u8
*lun
)
882 int rc
= TMF_RESP_FUNC_FAILED
;
883 struct hisi_sas_tmf_task tmf_task
;
885 tmf_task
.tmf
= TMF_CLEAR_ACA
;
886 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
891 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device
*device
)
893 struct sas_phy
*phy
= sas_get_local_phy(device
);
894 int rc
, reset_type
= (device
->dev_type
== SAS_SATA_DEV
||
895 (device
->tproto
& SAS_PROTOCOL_STP
)) ? 0 : 1;
896 rc
= sas_phy_reset(phy
, reset_type
);
897 sas_put_local_phy(phy
);
902 static int hisi_sas_I_T_nexus_reset(struct domain_device
*device
)
904 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
905 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
907 int rc
= TMF_RESP_FUNC_FAILED
;
909 if (sas_dev
->dev_status
!= HISI_SAS_DEV_EH
)
910 return TMF_RESP_FUNC_FAILED
;
911 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
913 rc
= hisi_sas_debug_I_T_nexus_reset(device
);
915 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
916 hisi_sas_release_task(hisi_hba
, device
);
917 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
922 static int hisi_sas_lu_reset(struct domain_device
*device
, u8
*lun
)
924 struct hisi_sas_tmf_task tmf_task
;
925 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
926 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
927 struct device
*dev
= &hisi_hba
->pdev
->dev
;
929 int rc
= TMF_RESP_FUNC_FAILED
;
931 tmf_task
.tmf
= TMF_LU_RESET
;
932 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
933 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
934 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
935 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
936 hisi_sas_release_task(hisi_hba
, device
);
937 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
940 /* If failed, fall-through I_T_Nexus reset */
941 dev_err(dev
, "lu_reset: for device[%llx]:rc= %d\n",
942 sas_dev
->device_id
, rc
);
946 static int hisi_sas_query_task(struct sas_task
*task
)
949 struct hisi_sas_tmf_task tmf_task
;
950 int rc
= TMF_RESP_FUNC_FAILED
;
952 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
953 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
954 struct domain_device
*device
= task
->dev
;
955 struct hisi_sas_slot
*slot
= task
->lldd_task
;
958 int_to_scsilun(cmnd
->device
->lun
, &lun
);
959 tmf_task
.tmf
= TMF_QUERY_TASK
;
960 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
962 rc
= hisi_sas_debug_issue_ssp_tmf(device
,
966 /* The task is still in Lun, release it then */
967 case TMF_RESP_FUNC_SUCC
:
968 /* The task is not in Lun or failed, reset the phy */
969 case TMF_RESP_FUNC_FAILED
:
970 case TMF_RESP_FUNC_COMPLETE
:
973 rc
= TMF_RESP_FUNC_FAILED
;
981 hisi_sas_internal_abort_task_exec(struct hisi_hba
*hisi_hba
, u64 device_id
,
982 struct sas_task
*task
, int abort_flag
,
985 struct domain_device
*device
= task
->dev
;
986 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
987 struct device
*dev
= &hisi_hba
->pdev
->dev
;
988 struct hisi_sas_port
*port
;
989 struct hisi_sas_slot
*slot
;
990 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
991 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
996 port
= device
->port
->lldd_port
;
998 /* simply get a slot and send abort command */
999 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
1002 rc
= hisi_hba
->hw
->get_free_slot(hisi_hba
, sas_dev
->device_id
,
1003 &dlvry_queue
, &dlvry_queue_slot
);
1007 slot
= &hisi_hba
->slot_info
[slot_idx
];
1008 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
1010 slot
->idx
= slot_idx
;
1011 slot
->n_elem
= n_elem
;
1012 slot
->dlvry_queue
= dlvry_queue
;
1013 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
1014 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
1015 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
1018 task
->lldd_task
= slot
;
1020 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
1022 rc
= hisi_sas_task_prep_abort(hisi_hba
, slot
, device_id
,
1023 abort_flag
, task_tag
);
1027 /* Port structure is static for the HBA, so
1028 * even if the port is deformed it is ok
1031 list_add_tail(&slot
->entry
, &port
->list
);
1032 spin_lock(&task
->task_state_lock
);
1033 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
1034 spin_unlock(&task
->task_state_lock
);
1036 hisi_hba
->slot_prep
= slot
;
1038 atomic64_inc(&sas_dev
->running_req
);
1040 /* send abort command to our chip */
1041 hisi_hba
->hw
->start_delivery(hisi_hba
);
1046 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
1048 dev_err(dev
, "internal abort task prep: failed[%d]!\n", rc
);
1054 * hisi_sas_internal_task_abort -- execute an internal
1055 * abort command for single IO command or a device
1056 * @hisi_hba: host controller struct
1057 * @device: domain device
1058 * @abort_flag: mode of operation, device or single IO
1059 * @tag: tag of IO to be aborted (only relevant to single
1063 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
1064 struct domain_device
*device
,
1065 int abort_flag
, int tag
)
1067 struct sas_task
*task
;
1068 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1069 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1071 unsigned long flags
;
1073 if (!hisi_hba
->hw
->prep_abort
)
1076 task
= sas_alloc_slow_task(GFP_KERNEL
);
1081 task
->task_proto
= device
->tproto
;
1082 task
->task_done
= hisi_sas_task_done
;
1083 task
->slow_task
->timer
.data
= (unsigned long)task
;
1084 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
1085 task
->slow_task
->timer
.expires
= jiffies
+ 20*HZ
;
1086 add_timer(&task
->slow_task
->timer
);
1088 /* Lock as we are alloc'ing a slot, which cannot be interrupted */
1089 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1090 res
= hisi_sas_internal_abort_task_exec(hisi_hba
, sas_dev
->device_id
,
1091 task
, abort_flag
, tag
);
1092 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1094 del_timer(&task
->slow_task
->timer
);
1095 dev_err(dev
, "internal task abort: executing internal task failed: %d\n",
1099 wait_for_completion(&task
->slow_task
->completion
);
1100 res
= TMF_RESP_FUNC_FAILED
;
1102 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1103 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
1104 res
= TMF_RESP_FUNC_COMPLETE
;
1108 /* TMF timed out, return direct. */
1109 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
1110 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
1111 dev_err(dev
, "internal task abort: timeout.\n");
1112 if (task
->lldd_task
) {
1113 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1115 hisi_sas_slot_task_free(hisi_hba
, task
, slot
);
1121 dev_info(dev
, "internal task abort: task to dev %016llx task=%p "
1122 "resp: 0x%x sts 0x%x\n",
1123 SAS_ADDR(device
->sas_addr
),
1125 task
->task_status
.resp
, /* 0 is complete, -1 is undelivered */
1126 task
->task_status
.stat
);
1127 sas_free_task(task
);
1132 static void hisi_sas_port_formed(struct asd_sas_phy
*sas_phy
)
1134 hisi_sas_port_notify_formed(sas_phy
);
1137 static void hisi_sas_port_deformed(struct asd_sas_phy
*sas_phy
)
1139 hisi_sas_port_notify_deformed(sas_phy
);
1142 static void hisi_sas_phy_disconnected(struct hisi_sas_phy
*phy
)
1144 phy
->phy_attached
= 0;
1149 void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
)
1151 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1152 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1153 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1156 /* Phy down but ready */
1157 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
1158 hisi_sas_port_notify_formed(sas_phy
);
1160 struct hisi_sas_port
*port
= phy
->port
;
1162 /* Phy down and not ready */
1163 sas_ha
->notify_phy_event(sas_phy
, PHYE_LOSS_OF_SIGNAL
);
1164 sas_phy_disconnected(sas_phy
);
1167 if (phy
->phy_type
& PORT_TYPE_SAS
) {
1168 int port_id
= port
->id
;
1170 if (!hisi_hba
->hw
->get_wideport_bitmap(hisi_hba
,
1172 port
->port_attached
= 0;
1173 } else if (phy
->phy_type
& PORT_TYPE_SATA
)
1174 port
->port_attached
= 0;
1176 hisi_sas_phy_disconnected(phy
);
1179 EXPORT_SYMBOL_GPL(hisi_sas_phy_down
);
1181 static struct scsi_transport_template
*hisi_sas_stt
;
1183 static struct scsi_host_template hisi_sas_sht
= {
1184 .module
= THIS_MODULE
,
1186 .queuecommand
= sas_queuecommand
,
1187 .target_alloc
= sas_target_alloc
,
1188 .slave_configure
= hisi_sas_slave_configure
,
1189 .scan_finished
= hisi_sas_scan_finished
,
1190 .scan_start
= hisi_sas_scan_start
,
1191 .change_queue_depth
= sas_change_queue_depth
,
1192 .bios_param
= sas_bios_param
,
1195 .sg_tablesize
= SG_ALL
,
1196 .max_sectors
= SCSI_DEFAULT_MAX_SECTORS
,
1197 .use_clustering
= ENABLE_CLUSTERING
,
1198 .eh_device_reset_handler
= sas_eh_device_reset_handler
,
1199 .eh_bus_reset_handler
= sas_eh_bus_reset_handler
,
1200 .target_destroy
= sas_target_destroy
,
1204 static struct sas_domain_function_template hisi_sas_transport_ops
= {
1205 .lldd_dev_found
= hisi_sas_dev_found
,
1206 .lldd_dev_gone
= hisi_sas_dev_gone
,
1207 .lldd_execute_task
= hisi_sas_queue_command
,
1208 .lldd_control_phy
= hisi_sas_control_phy
,
1209 .lldd_abort_task
= hisi_sas_abort_task
,
1210 .lldd_abort_task_set
= hisi_sas_abort_task_set
,
1211 .lldd_clear_aca
= hisi_sas_clear_aca
,
1212 .lldd_I_T_nexus_reset
= hisi_sas_I_T_nexus_reset
,
1213 .lldd_lu_reset
= hisi_sas_lu_reset
,
1214 .lldd_query_task
= hisi_sas_query_task
,
1215 .lldd_port_formed
= hisi_sas_port_formed
,
1216 .lldd_port_deformed
= hisi_sas_port_deformed
,
1219 static int hisi_sas_alloc(struct hisi_hba
*hisi_hba
, struct Scsi_Host
*shost
)
1221 struct platform_device
*pdev
= hisi_hba
->pdev
;
1222 struct device
*dev
= &pdev
->dev
;
1223 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1225 spin_lock_init(&hisi_hba
->lock
);
1226 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1227 hisi_sas_phy_init(hisi_hba
, i
);
1228 hisi_hba
->port
[i
].port_attached
= 0;
1229 hisi_hba
->port
[i
].id
= -1;
1230 INIT_LIST_HEAD(&hisi_hba
->port
[i
].list
);
1233 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1234 hisi_hba
->devices
[i
].dev_type
= SAS_PHY_UNUSED
;
1235 hisi_hba
->devices
[i
].device_id
= i
;
1236 hisi_hba
->devices
[i
].dev_status
= HISI_SAS_DEV_NORMAL
;
1239 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1240 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1241 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1243 /* Completion queue structure */
1245 cq
->hisi_hba
= hisi_hba
;
1247 /* Delivery queue structure */
1249 dq
->hisi_hba
= hisi_hba
;
1251 /* Delivery queue */
1252 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1253 hisi_hba
->cmd_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1254 &hisi_hba
->cmd_hdr_dma
[i
], GFP_KERNEL
);
1255 if (!hisi_hba
->cmd_hdr
[i
])
1257 memset(hisi_hba
->cmd_hdr
[i
], 0, s
);
1259 /* Completion queue */
1260 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1261 hisi_hba
->complete_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1262 &hisi_hba
->complete_hdr_dma
[i
], GFP_KERNEL
);
1263 if (!hisi_hba
->complete_hdr
[i
])
1265 memset(hisi_hba
->complete_hdr
[i
], 0, s
);
1268 s
= HISI_SAS_STATUS_BUF_SZ
;
1269 hisi_hba
->status_buffer_pool
= dma_pool_create("status_buffer",
1271 if (!hisi_hba
->status_buffer_pool
)
1274 s
= HISI_SAS_COMMAND_TABLE_SZ
;
1275 hisi_hba
->command_table_pool
= dma_pool_create("command_table",
1277 if (!hisi_hba
->command_table_pool
)
1280 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1281 hisi_hba
->itct
= dma_alloc_coherent(dev
, s
, &hisi_hba
->itct_dma
,
1283 if (!hisi_hba
->itct
)
1286 memset(hisi_hba
->itct
, 0, s
);
1288 hisi_hba
->slot_info
= devm_kcalloc(dev
, max_command_entries
,
1289 sizeof(struct hisi_sas_slot
),
1291 if (!hisi_hba
->slot_info
)
1294 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1295 hisi_hba
->iost
= dma_alloc_coherent(dev
, s
, &hisi_hba
->iost_dma
,
1297 if (!hisi_hba
->iost
)
1300 memset(hisi_hba
->iost
, 0, s
);
1302 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1303 hisi_hba
->breakpoint
= dma_alloc_coherent(dev
, s
,
1304 &hisi_hba
->breakpoint_dma
, GFP_KERNEL
);
1305 if (!hisi_hba
->breakpoint
)
1308 memset(hisi_hba
->breakpoint
, 0, s
);
1310 hisi_hba
->slot_index_count
= max_command_entries
;
1311 s
= hisi_hba
->slot_index_count
/ BITS_PER_BYTE
;
1312 hisi_hba
->slot_index_tags
= devm_kzalloc(dev
, s
, GFP_KERNEL
);
1313 if (!hisi_hba
->slot_index_tags
)
1316 hisi_hba
->sge_page_pool
= dma_pool_create("status_sge", dev
,
1317 sizeof(struct hisi_sas_sge_page
), 16, 0);
1318 if (!hisi_hba
->sge_page_pool
)
1321 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1322 hisi_hba
->initial_fis
= dma_alloc_coherent(dev
, s
,
1323 &hisi_hba
->initial_fis_dma
, GFP_KERNEL
);
1324 if (!hisi_hba
->initial_fis
)
1326 memset(hisi_hba
->initial_fis
, 0, s
);
1328 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
) * 2;
1329 hisi_hba
->sata_breakpoint
= dma_alloc_coherent(dev
, s
,
1330 &hisi_hba
->sata_breakpoint_dma
, GFP_KERNEL
);
1331 if (!hisi_hba
->sata_breakpoint
)
1333 memset(hisi_hba
->sata_breakpoint
, 0, s
);
1335 hisi_sas_slot_index_init(hisi_hba
);
1337 hisi_hba
->wq
= create_singlethread_workqueue(dev_name(dev
));
1338 if (!hisi_hba
->wq
) {
1339 dev_err(dev
, "sas_alloc: failed to create workqueue\n");
1348 static void hisi_sas_free(struct hisi_hba
*hisi_hba
)
1350 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1351 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1353 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1354 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1355 if (hisi_hba
->cmd_hdr
[i
])
1356 dma_free_coherent(dev
, s
,
1357 hisi_hba
->cmd_hdr
[i
],
1358 hisi_hba
->cmd_hdr_dma
[i
]);
1360 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1361 if (hisi_hba
->complete_hdr
[i
])
1362 dma_free_coherent(dev
, s
,
1363 hisi_hba
->complete_hdr
[i
],
1364 hisi_hba
->complete_hdr_dma
[i
]);
1367 dma_pool_destroy(hisi_hba
->status_buffer_pool
);
1368 dma_pool_destroy(hisi_hba
->command_table_pool
);
1369 dma_pool_destroy(hisi_hba
->sge_page_pool
);
1371 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1373 dma_free_coherent(dev
, s
,
1374 hisi_hba
->itct
, hisi_hba
->itct_dma
);
1376 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1378 dma_free_coherent(dev
, s
,
1379 hisi_hba
->iost
, hisi_hba
->iost_dma
);
1381 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1382 if (hisi_hba
->breakpoint
)
1383 dma_free_coherent(dev
, s
,
1384 hisi_hba
->breakpoint
,
1385 hisi_hba
->breakpoint_dma
);
1388 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1389 if (hisi_hba
->initial_fis
)
1390 dma_free_coherent(dev
, s
,
1391 hisi_hba
->initial_fis
,
1392 hisi_hba
->initial_fis_dma
);
1394 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
) * 2;
1395 if (hisi_hba
->sata_breakpoint
)
1396 dma_free_coherent(dev
, s
,
1397 hisi_hba
->sata_breakpoint
,
1398 hisi_hba
->sata_breakpoint_dma
);
1401 destroy_workqueue(hisi_hba
->wq
);
1404 static struct Scsi_Host
*hisi_sas_shost_alloc(struct platform_device
*pdev
,
1405 const struct hisi_sas_hw
*hw
)
1407 struct resource
*res
;
1408 struct Scsi_Host
*shost
;
1409 struct hisi_hba
*hisi_hba
;
1410 struct device
*dev
= &pdev
->dev
;
1411 struct device_node
*np
= pdev
->dev
.of_node
;
1414 shost
= scsi_host_alloc(&hisi_sas_sht
, sizeof(*hisi_hba
));
1416 dev_err(dev
, "scsi host alloc failed\n");
1419 hisi_hba
= shost_priv(shost
);
1422 hisi_hba
->pdev
= pdev
;
1423 hisi_hba
->shost
= shost
;
1424 SHOST_TO_SAS_HA(shost
) = &hisi_hba
->sha
;
1426 init_timer(&hisi_hba
->timer
);
1428 if (device_property_read_u8_array(dev
, "sas-addr", hisi_hba
->sas_addr
,
1433 hisi_hba
->ctrl
= syscon_regmap_lookup_by_phandle(np
,
1434 "hisilicon,sas-syscon");
1435 if (IS_ERR(hisi_hba
->ctrl
))
1438 if (device_property_read_u32(dev
, "ctrl-reset-reg",
1439 &hisi_hba
->ctrl_reset_reg
))
1442 if (device_property_read_u32(dev
, "ctrl-reset-sts-reg",
1443 &hisi_hba
->ctrl_reset_sts_reg
))
1446 if (device_property_read_u32(dev
, "ctrl-clock-ena-reg",
1447 &hisi_hba
->ctrl_clock_ena_reg
))
1451 refclk
= devm_clk_get(&pdev
->dev
, NULL
);
1453 dev_info(dev
, "no ref clk property\n");
1455 hisi_hba
->refclk_frequency_mhz
= clk_get_rate(refclk
) / 1000000;
1457 if (device_property_read_u32(dev
, "phy-count", &hisi_hba
->n_phy
))
1460 if (device_property_read_u32(dev
, "queue-count",
1461 &hisi_hba
->queue_count
))
1464 if (dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64)) &&
1465 dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32))) {
1466 dev_err(dev
, "No usable DMA addressing method\n");
1470 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1471 hisi_hba
->regs
= devm_ioremap_resource(dev
, res
);
1472 if (IS_ERR(hisi_hba
->regs
))
1475 if (hisi_sas_alloc(hisi_hba
, shost
)) {
1476 hisi_sas_free(hisi_hba
);
1483 dev_err(dev
, "shost alloc failed\n");
1487 static void hisi_sas_init_add(struct hisi_hba
*hisi_hba
)
1491 for (i
= 0; i
< hisi_hba
->n_phy
; i
++)
1492 memcpy(&hisi_hba
->phy
[i
].dev_sas_addr
,
1497 int hisi_sas_probe(struct platform_device
*pdev
,
1498 const struct hisi_sas_hw
*hw
)
1500 struct Scsi_Host
*shost
;
1501 struct hisi_hba
*hisi_hba
;
1502 struct device
*dev
= &pdev
->dev
;
1503 struct asd_sas_phy
**arr_phy
;
1504 struct asd_sas_port
**arr_port
;
1505 struct sas_ha_struct
*sha
;
1506 int rc
, phy_nr
, port_nr
, i
;
1508 shost
= hisi_sas_shost_alloc(pdev
, hw
);
1512 sha
= SHOST_TO_SAS_HA(shost
);
1513 hisi_hba
= shost_priv(shost
);
1514 platform_set_drvdata(pdev
, sha
);
1516 phy_nr
= port_nr
= hisi_hba
->n_phy
;
1518 arr_phy
= devm_kcalloc(dev
, phy_nr
, sizeof(void *), GFP_KERNEL
);
1519 arr_port
= devm_kcalloc(dev
, port_nr
, sizeof(void *), GFP_KERNEL
);
1520 if (!arr_phy
|| !arr_port
) {
1525 sha
->sas_phy
= arr_phy
;
1526 sha
->sas_port
= arr_port
;
1527 sha
->lldd_ha
= hisi_hba
;
1529 shost
->transportt
= hisi_sas_stt
;
1530 shost
->max_id
= HISI_SAS_MAX_DEVICES
;
1531 shost
->max_lun
= ~0;
1532 shost
->max_channel
= 1;
1533 shost
->max_cmd_len
= 16;
1534 shost
->sg_tablesize
= min_t(u16
, SG_ALL
, HISI_SAS_SGE_PAGE_CNT
);
1535 shost
->can_queue
= hisi_hba
->hw
->max_command_entries
;
1536 shost
->cmd_per_lun
= hisi_hba
->hw
->max_command_entries
;
1538 sha
->sas_ha_name
= DRV_NAME
;
1539 sha
->dev
= &hisi_hba
->pdev
->dev
;
1540 sha
->lldd_module
= THIS_MODULE
;
1541 sha
->sas_addr
= &hisi_hba
->sas_addr
[0];
1542 sha
->num_phys
= hisi_hba
->n_phy
;
1543 sha
->core
.shost
= hisi_hba
->shost
;
1545 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1546 sha
->sas_phy
[i
] = &hisi_hba
->phy
[i
].sas_phy
;
1547 sha
->sas_port
[i
] = &hisi_hba
->port
[i
].sas_port
;
1550 hisi_sas_init_add(hisi_hba
);
1552 rc
= hisi_hba
->hw
->hw_init(hisi_hba
);
1556 rc
= scsi_add_host(shost
, &pdev
->dev
);
1560 rc
= sas_register_ha(sha
);
1562 goto err_out_register_ha
;
1564 scsi_scan_host(shost
);
1568 err_out_register_ha
:
1569 scsi_remove_host(shost
);
1571 hisi_sas_free(hisi_hba
);
1575 EXPORT_SYMBOL_GPL(hisi_sas_probe
);
1577 int hisi_sas_remove(struct platform_device
*pdev
)
1579 struct sas_ha_struct
*sha
= platform_get_drvdata(pdev
);
1580 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
1581 struct Scsi_Host
*shost
= sha
->core
.shost
;
1583 scsi_remove_host(sha
->core
.shost
);
1584 sas_unregister_ha(sha
);
1585 sas_remove_host(sha
->core
.shost
);
1587 hisi_sas_free(hisi_hba
);
1591 EXPORT_SYMBOL_GPL(hisi_sas_remove
);
1593 static __init
int hisi_sas_init(void)
1595 pr_info("hisi_sas: driver version %s\n", DRV_VERSION
);
1597 hisi_sas_stt
= sas_domain_attach_transport(&hisi_sas_transport_ops
);
1604 static __exit
void hisi_sas_exit(void)
1606 sas_release_transport(hisi_sas_stt
);
1609 module_init(hisi_sas_init
);
1610 module_exit(hisi_sas_exit
);
1612 MODULE_VERSION(DRV_VERSION
);
1613 MODULE_LICENSE("GPL");
1614 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1615 MODULE_DESCRIPTION("HISILICON SAS controller driver");
1616 MODULE_ALIAS("platform:" DRV_NAME
);