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
;
74 struct domain_device
*device
= task
->dev
;
75 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
80 if (!sas_protocol_ata(task
->task_proto
))
82 dma_unmap_sg(dev
, task
->scatter
, slot
->n_elem
,
85 if (slot
->command_table
)
86 dma_pool_free(hisi_hba
->command_table_pool
,
87 slot
->command_table
, slot
->command_table_dma
);
89 if (slot
->status_buffer
)
90 dma_pool_free(hisi_hba
->status_buffer_pool
,
91 slot
->status_buffer
, slot
->status_buffer_dma
);
94 dma_pool_free(hisi_hba
->sge_page_pool
, slot
->sge_page
,
97 list_del_init(&slot
->entry
);
98 task
->lldd_task
= NULL
;
101 hisi_sas_slot_index_free(hisi_hba
, slot
->idx
);
103 atomic64_dec(&sas_dev
->running_req
);
104 /* slot memory is fully zeroed when it is reused */
106 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free
);
108 static int hisi_sas_task_prep_smp(struct hisi_hba
*hisi_hba
,
109 struct hisi_sas_slot
*slot
)
111 return hisi_hba
->hw
->prep_smp(hisi_hba
, slot
);
114 static int hisi_sas_task_prep_ssp(struct hisi_hba
*hisi_hba
,
115 struct hisi_sas_slot
*slot
, int is_tmf
,
116 struct hisi_sas_tmf_task
*tmf
)
118 return hisi_hba
->hw
->prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
121 static int hisi_sas_task_prep_ata(struct hisi_hba
*hisi_hba
,
122 struct hisi_sas_slot
*slot
)
124 return hisi_hba
->hw
->prep_stp(hisi_hba
, slot
);
127 static int hisi_sas_task_prep_abort(struct hisi_hba
*hisi_hba
,
128 struct hisi_sas_slot
*slot
,
129 int device_id
, int abort_flag
, int tag_to_abort
)
131 return hisi_hba
->hw
->prep_abort(hisi_hba
, slot
,
132 device_id
, abort_flag
, tag_to_abort
);
136 * This function will issue an abort TMF regardless of whether the
137 * task is in the sdev or not. Then it will do the task complete
138 * cleanup and callbacks.
140 static void hisi_sas_slot_abort(struct work_struct
*work
)
142 struct hisi_sas_slot
*abort_slot
=
143 container_of(work
, struct hisi_sas_slot
, abort_slot
);
144 struct sas_task
*task
= abort_slot
->task
;
145 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
146 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
147 struct hisi_sas_tmf_task tmf_task
;
149 struct device
*dev
= &hisi_hba
->pdev
->dev
;
150 int tag
= abort_slot
->idx
;
153 if (!(task
->task_proto
& SAS_PROTOCOL_SSP
)) {
154 dev_err(dev
, "cannot abort slot for non-ssp task\n");
158 int_to_scsilun(cmnd
->device
->lun
, &lun
);
159 tmf_task
.tmf
= TMF_ABORT_TASK
;
160 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
162 hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
, &tmf_task
);
164 /* Do cleanup for this task */
165 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
166 hisi_sas_slot_task_free(hisi_hba
, task
, abort_slot
);
167 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
169 task
->task_done(task
);
172 static int hisi_sas_task_prep(struct sas_task
*task
, struct hisi_hba
*hisi_hba
,
173 int is_tmf
, struct hisi_sas_tmf_task
*tmf
,
176 struct domain_device
*device
= task
->dev
;
177 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
178 struct hisi_sas_port
*port
;
179 struct hisi_sas_slot
*slot
;
180 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
181 struct device
*dev
= &hisi_hba
->pdev
->dev
;
182 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
185 struct task_status_struct
*ts
= &task
->task_status
;
187 ts
->resp
= SAS_TASK_UNDELIVERED
;
188 ts
->stat
= SAS_PHY_DOWN
;
190 * libsas will use dev->port, should
191 * not call task_done for sata
193 if (device
->dev_type
!= SAS_SATA_DEV
)
194 task
->task_done(task
);
198 if (DEV_IS_GONE(sas_dev
)) {
200 dev_info(dev
, "task prep: device %llu not ready\n",
203 dev_info(dev
, "task prep: device %016llx not ready\n",
204 SAS_ADDR(device
->sas_addr
));
209 port
= device
->port
->lldd_port
;
210 if (port
&& !port
->port_attached
) {
211 dev_info(dev
, "task prep: %s port%d not attach device\n",
212 (sas_protocol_ata(task
->task_proto
)) ?
219 if (!sas_protocol_ata(task
->task_proto
)) {
220 if (task
->num_scatter
) {
221 n_elem
= dma_map_sg(dev
, task
->scatter
,
222 task
->num_scatter
, task
->data_dir
);
229 n_elem
= task
->num_scatter
;
231 if (hisi_hba
->hw
->slot_index_alloc
)
232 rc
= hisi_hba
->hw
->slot_index_alloc(hisi_hba
, &slot_idx
,
235 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
238 rc
= hisi_hba
->hw
->get_free_slot(hisi_hba
, sas_dev
->device_id
,
239 &dlvry_queue
, &dlvry_queue_slot
);
243 slot
= &hisi_hba
->slot_info
[slot_idx
];
244 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
246 slot
->idx
= slot_idx
;
247 slot
->n_elem
= n_elem
;
248 slot
->dlvry_queue
= dlvry_queue
;
249 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
250 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
251 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
254 task
->lldd_task
= slot
;
255 INIT_WORK(&slot
->abort_slot
, hisi_sas_slot_abort
);
257 slot
->status_buffer
= dma_pool_alloc(hisi_hba
->status_buffer_pool
,
259 &slot
->status_buffer_dma
);
260 if (!slot
->status_buffer
) {
262 goto err_out_slot_buf
;
264 memset(slot
->status_buffer
, 0, HISI_SAS_STATUS_BUF_SZ
);
266 slot
->command_table
= dma_pool_alloc(hisi_hba
->command_table_pool
,
268 &slot
->command_table_dma
);
269 if (!slot
->command_table
) {
271 goto err_out_status_buf
;
273 memset(slot
->command_table
, 0, HISI_SAS_COMMAND_TABLE_SZ
);
274 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
276 switch (task
->task_proto
) {
277 case SAS_PROTOCOL_SMP
:
278 rc
= hisi_sas_task_prep_smp(hisi_hba
, slot
);
280 case SAS_PROTOCOL_SSP
:
281 rc
= hisi_sas_task_prep_ssp(hisi_hba
, slot
, is_tmf
, tmf
);
283 case SAS_PROTOCOL_SATA
:
284 case SAS_PROTOCOL_STP
:
285 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:
286 rc
= hisi_sas_task_prep_ata(hisi_hba
, slot
);
289 dev_err(dev
, "task prep: unknown/unsupported proto (0x%x)\n",
296 dev_err(dev
, "task prep: rc = 0x%x\n", rc
);
299 goto err_out_command_table
;
302 list_add_tail(&slot
->entry
, &port
->list
);
303 spin_lock(&task
->task_state_lock
);
304 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
305 spin_unlock(&task
->task_state_lock
);
307 hisi_hba
->slot_prep
= slot
;
309 atomic64_inc(&sas_dev
->running_req
);
315 dma_pool_free(hisi_hba
->sge_page_pool
, slot
->sge_page
,
317 err_out_command_table
:
318 dma_pool_free(hisi_hba
->command_table_pool
, slot
->command_table
,
319 slot
->command_table_dma
);
321 dma_pool_free(hisi_hba
->status_buffer_pool
, slot
->status_buffer
,
322 slot
->status_buffer_dma
);
324 /* Nothing to be done */
326 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
328 dev_err(dev
, "task prep: failed[%d]!\n", rc
);
329 if (!sas_protocol_ata(task
->task_proto
))
331 dma_unmap_sg(dev
, task
->scatter
, n_elem
,
337 static int hisi_sas_task_exec(struct sas_task
*task
, gfp_t gfp_flags
,
338 int is_tmf
, struct hisi_sas_tmf_task
*tmf
)
343 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
344 struct device
*dev
= &hisi_hba
->pdev
->dev
;
346 /* protect task_prep and start_delivery sequence */
347 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
348 rc
= hisi_sas_task_prep(task
, hisi_hba
, is_tmf
, tmf
, &pass
);
350 dev_err(dev
, "task exec: failed[%d]!\n", rc
);
353 hisi_hba
->hw
->start_delivery(hisi_hba
);
354 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
359 static void hisi_sas_bytes_dmaed(struct hisi_hba
*hisi_hba
, int phy_no
)
361 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
362 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
363 struct sas_ha_struct
*sas_ha
;
365 if (!phy
->phy_attached
)
368 sas_ha
= &hisi_hba
->sha
;
369 sas_ha
->notify_phy_event(sas_phy
, PHYE_OOB_DONE
);
372 struct sas_phy
*sphy
= sas_phy
->phy
;
374 sphy
->negotiated_linkrate
= sas_phy
->linkrate
;
375 sphy
->minimum_linkrate_hw
= SAS_LINK_RATE_1_5_GBPS
;
376 sphy
->maximum_linkrate_hw
=
377 hisi_hba
->hw
->phy_get_max_linkrate();
378 if (sphy
->minimum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
379 sphy
->minimum_linkrate
= phy
->minimum_linkrate
;
381 if (sphy
->maximum_linkrate
== SAS_LINK_RATE_UNKNOWN
)
382 sphy
->maximum_linkrate
= phy
->maximum_linkrate
;
385 if (phy
->phy_type
& PORT_TYPE_SAS
) {
386 struct sas_identify_frame
*id
;
388 id
= (struct sas_identify_frame
*)phy
->frame_rcvd
;
389 id
->dev_type
= phy
->identify
.device_type
;
390 id
->initiator_bits
= SAS_PROTOCOL_ALL
;
391 id
->target_bits
= phy
->identify
.target_port_protocols
;
392 } else if (phy
->phy_type
& PORT_TYPE_SATA
) {
396 sas_phy
->frame_rcvd_size
= phy
->frame_rcvd_size
;
397 sas_ha
->notify_port_event(sas_phy
, PORTE_BYTES_DMAED
);
400 static struct hisi_sas_device
*hisi_sas_alloc_dev(struct domain_device
*device
)
402 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
403 struct hisi_sas_device
*sas_dev
= NULL
;
406 spin_lock(&hisi_hba
->lock
);
407 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
408 if (hisi_hba
->devices
[i
].dev_type
== SAS_PHY_UNUSED
) {
409 hisi_hba
->devices
[i
].device_id
= i
;
410 sas_dev
= &hisi_hba
->devices
[i
];
411 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
412 sas_dev
->dev_type
= device
->dev_type
;
413 sas_dev
->hisi_hba
= hisi_hba
;
414 sas_dev
->sas_device
= device
;
418 spin_unlock(&hisi_hba
->lock
);
423 static int hisi_sas_dev_found(struct domain_device
*device
)
425 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
426 struct domain_device
*parent_dev
= device
->parent
;
427 struct hisi_sas_device
*sas_dev
;
428 struct device
*dev
= &hisi_hba
->pdev
->dev
;
430 if (hisi_hba
->hw
->alloc_dev
)
431 sas_dev
= hisi_hba
->hw
->alloc_dev(device
);
433 sas_dev
= hisi_sas_alloc_dev(device
);
435 dev_err(dev
, "fail alloc dev: max support %d devices\n",
436 HISI_SAS_MAX_DEVICES
);
440 device
->lldd_dev
= sas_dev
;
441 hisi_hba
->hw
->setup_itct(hisi_hba
, sas_dev
);
443 if (parent_dev
&& DEV_IS_EXPANDER(parent_dev
->dev_type
)) {
445 u8 phy_num
= parent_dev
->ex_dev
.num_phys
;
448 for (phy_no
= 0; phy_no
< phy_num
; phy_no
++) {
449 phy
= &parent_dev
->ex_dev
.ex_phy
[phy_no
];
450 if (SAS_ADDR(phy
->attached_sas_addr
) ==
451 SAS_ADDR(device
->sas_addr
)) {
452 sas_dev
->attached_phy
= phy_no
;
457 if (phy_no
== phy_num
) {
458 dev_info(dev
, "dev found: no attached "
459 "dev:%016llx at ex:%016llx\n",
460 SAS_ADDR(device
->sas_addr
),
461 SAS_ADDR(parent_dev
->sas_addr
));
469 static int hisi_sas_slave_configure(struct scsi_device
*sdev
)
471 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
472 int ret
= sas_slave_configure(sdev
);
476 if (!dev_is_sata(dev
))
477 sas_change_queue_depth(sdev
, 64);
482 static void hisi_sas_scan_start(struct Scsi_Host
*shost
)
484 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
487 for (i
= 0; i
< hisi_hba
->n_phy
; ++i
)
488 hisi_sas_bytes_dmaed(hisi_hba
, i
);
490 hisi_hba
->scan_finished
= 1;
493 static int hisi_sas_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
495 struct hisi_hba
*hisi_hba
= shost_priv(shost
);
496 struct sas_ha_struct
*sha
= &hisi_hba
->sha
;
498 if (hisi_hba
->scan_finished
== 0)
505 static void hisi_sas_phyup_work(struct work_struct
*work
)
507 struct hisi_sas_phy
*phy
=
508 container_of(work
, struct hisi_sas_phy
, phyup_ws
);
509 struct hisi_hba
*hisi_hba
= phy
->hisi_hba
;
510 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
511 int phy_no
= sas_phy
->id
;
513 hisi_hba
->hw
->sl_notify(hisi_hba
, phy_no
); /* This requires a sleep */
514 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
517 static void hisi_sas_phy_init(struct hisi_hba
*hisi_hba
, int phy_no
)
519 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
520 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
522 phy
->hisi_hba
= hisi_hba
;
524 init_timer(&phy
->timer
);
525 sas_phy
->enabled
= (phy_no
< hisi_hba
->n_phy
) ? 1 : 0;
526 sas_phy
->class = SAS
;
527 sas_phy
->iproto
= SAS_PROTOCOL_ALL
;
529 sas_phy
->type
= PHY_TYPE_PHYSICAL
;
530 sas_phy
->role
= PHY_ROLE_INITIATOR
;
531 sas_phy
->oob_mode
= OOB_NOT_CONNECTED
;
532 sas_phy
->linkrate
= SAS_LINK_RATE_UNKNOWN
;
533 sas_phy
->id
= phy_no
;
534 sas_phy
->sas_addr
= &hisi_hba
->sas_addr
[0];
535 sas_phy
->frame_rcvd
= &phy
->frame_rcvd
[0];
536 sas_phy
->ha
= (struct sas_ha_struct
*)hisi_hba
->shost
->hostdata
;
537 sas_phy
->lldd_phy
= phy
;
539 INIT_WORK(&phy
->phyup_ws
, hisi_sas_phyup_work
);
542 static void hisi_sas_port_notify_formed(struct asd_sas_phy
*sas_phy
)
544 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
545 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
546 struct hisi_sas_phy
*phy
= sas_phy
->lldd_phy
;
547 struct asd_sas_port
*sas_port
= sas_phy
->port
;
548 struct hisi_sas_port
*port
= &hisi_hba
->port
[phy
->port_id
];
554 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
555 port
->port_attached
= 1;
556 port
->id
= phy
->port_id
;
558 sas_port
->lldd_port
= port
;
559 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
562 static void hisi_sas_do_release_task(struct hisi_hba
*hisi_hba
, int phy_no
,
563 struct domain_device
*device
)
565 struct hisi_sas_phy
*phy
;
566 struct hisi_sas_port
*port
;
567 struct hisi_sas_slot
*slot
, *slot2
;
568 struct device
*dev
= &hisi_hba
->pdev
->dev
;
570 phy
= &hisi_hba
->phy
[phy_no
];
575 list_for_each_entry_safe(slot
, slot2
, &port
->list
, entry
) {
576 struct sas_task
*task
;
579 if (device
&& task
->dev
!= device
)
582 dev_info(dev
, "Release slot [%d:%d], task [%p]:\n",
583 slot
->dlvry_queue
, slot
->dlvry_queue_slot
, task
);
584 hisi_hba
->hw
->slot_complete(hisi_hba
, slot
, 1);
588 static void hisi_sas_port_notify_deformed(struct asd_sas_phy
*sas_phy
)
590 struct domain_device
*device
;
591 struct hisi_sas_phy
*phy
= sas_phy
->lldd_phy
;
592 struct asd_sas_port
*sas_port
= sas_phy
->port
;
594 list_for_each_entry(device
, &sas_port
->dev_list
, dev_list_node
)
595 hisi_sas_do_release_task(phy
->hisi_hba
, sas_phy
->id
, device
);
598 static void hisi_sas_release_task(struct hisi_hba
*hisi_hba
,
599 struct domain_device
*device
)
601 struct asd_sas_port
*port
= device
->port
;
602 struct asd_sas_phy
*sas_phy
;
604 list_for_each_entry(sas_phy
, &port
->phy_list
, port_phy_el
)
605 hisi_sas_do_release_task(hisi_hba
, sas_phy
->id
, device
);
608 static void hisi_sas_dev_gone(struct domain_device
*device
)
610 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
611 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
612 struct device
*dev
= &hisi_hba
->pdev
->dev
;
613 u64 dev_id
= sas_dev
->device_id
;
615 dev_info(dev
, "found dev[%lld:%x] is gone\n",
616 sas_dev
->device_id
, sas_dev
->dev_type
);
618 hisi_sas_internal_task_abort(hisi_hba
, device
,
619 HISI_SAS_INT_ABT_DEV
, 0);
621 hisi_hba
->hw
->free_device(hisi_hba
, sas_dev
);
622 device
->lldd_dev
= NULL
;
623 memset(sas_dev
, 0, sizeof(*sas_dev
));
624 sas_dev
->device_id
= dev_id
;
625 sas_dev
->dev_type
= SAS_PHY_UNUSED
;
626 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
629 static int hisi_sas_queue_command(struct sas_task
*task
, gfp_t gfp_flags
)
631 return hisi_sas_task_exec(task
, gfp_flags
, 0, NULL
);
634 static int hisi_sas_control_phy(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
637 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
638 struct hisi_hba
*hisi_hba
= sas_ha
->lldd_ha
;
639 int phy_no
= sas_phy
->id
;
642 case PHY_FUNC_HARD_RESET
:
643 hisi_hba
->hw
->phy_hard_reset(hisi_hba
, phy_no
);
646 case PHY_FUNC_LINK_RESET
:
647 hisi_hba
->hw
->phy_enable(hisi_hba
, phy_no
);
648 hisi_hba
->hw
->phy_hard_reset(hisi_hba
, phy_no
);
651 case PHY_FUNC_DISABLE
:
652 hisi_hba
->hw
->phy_disable(hisi_hba
, phy_no
);
655 case PHY_FUNC_SET_LINK_RATE
:
656 hisi_hba
->hw
->phy_set_linkrate(hisi_hba
, phy_no
, funcdata
);
659 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
666 static void hisi_sas_task_done(struct sas_task
*task
)
668 if (!del_timer(&task
->slow_task
->timer
))
670 complete(&task
->slow_task
->completion
);
673 static void hisi_sas_tmf_timedout(unsigned long data
)
675 struct sas_task
*task
= (struct sas_task
*)data
;
677 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
678 complete(&task
->slow_task
->completion
);
681 #define TASK_TIMEOUT 20
683 static int hisi_sas_exec_internal_tmf_task(struct domain_device
*device
,
684 void *parameter
, u32 para_len
,
685 struct hisi_sas_tmf_task
*tmf
)
687 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
688 struct hisi_hba
*hisi_hba
= sas_dev
->hisi_hba
;
689 struct device
*dev
= &hisi_hba
->pdev
->dev
;
690 struct sas_task
*task
;
693 for (retry
= 0; retry
< TASK_RETRY
; retry
++) {
694 task
= sas_alloc_slow_task(GFP_KERNEL
);
699 task
->task_proto
= device
->tproto
;
701 memcpy(&task
->ssp_task
, parameter
, para_len
);
702 task
->task_done
= hisi_sas_task_done
;
704 task
->slow_task
->timer
.data
= (unsigned long) task
;
705 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
706 task
->slow_task
->timer
.expires
= jiffies
+ TASK_TIMEOUT
*HZ
;
707 add_timer(&task
->slow_task
->timer
);
709 res
= hisi_sas_task_exec(task
, GFP_KERNEL
, 1, tmf
);
712 del_timer(&task
->slow_task
->timer
);
713 dev_err(dev
, "abort tmf: executing internal task failed: %d\n",
718 wait_for_completion(&task
->slow_task
->completion
);
719 res
= TMF_RESP_FUNC_FAILED
;
720 /* Even TMF timed out, return direct. */
721 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
722 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
723 dev_err(dev
, "abort tmf: TMF task[%d] timeout\n",
724 tmf
->tag_of_task_to_be_managed
);
725 if (task
->lldd_task
) {
726 struct hisi_sas_slot
*slot
=
729 hisi_sas_slot_task_free(hisi_hba
,
737 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
738 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
739 res
= TMF_RESP_FUNC_COMPLETE
;
743 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
744 task
->task_status
.stat
== TMF_RESP_FUNC_SUCC
) {
745 res
= TMF_RESP_FUNC_SUCC
;
749 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
750 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
751 /* no error, but return the number of bytes of
754 dev_warn(dev
, "abort tmf: task to dev %016llx "
755 "resp: 0x%x sts 0x%x underrun\n",
756 SAS_ADDR(device
->sas_addr
),
757 task
->task_status
.resp
,
758 task
->task_status
.stat
);
759 res
= task
->task_status
.residual
;
763 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
764 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
765 dev_warn(dev
, "abort tmf: blocked task error\n");
770 dev_warn(dev
, "abort tmf: task to dev "
771 "%016llx resp: 0x%x status 0x%x\n",
772 SAS_ADDR(device
->sas_addr
), task
->task_status
.resp
,
773 task
->task_status
.stat
);
778 if (retry
== TASK_RETRY
)
779 dev_warn(dev
, "abort tmf: executing internal task failed!\n");
784 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device
*device
,
785 u8
*lun
, struct hisi_sas_tmf_task
*tmf
)
787 struct sas_ssp_task ssp_task
;
789 if (!(device
->tproto
& SAS_PROTOCOL_SSP
))
790 return TMF_RESP_FUNC_ESUPP
;
792 memcpy(ssp_task
.LUN
, lun
, 8);
794 return hisi_sas_exec_internal_tmf_task(device
, &ssp_task
,
795 sizeof(ssp_task
), tmf
);
798 static int hisi_sas_abort_task(struct sas_task
*task
)
801 struct hisi_sas_tmf_task tmf_task
;
802 struct domain_device
*device
= task
->dev
;
803 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
804 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(task
->dev
);
805 struct device
*dev
= &hisi_hba
->pdev
->dev
;
806 int rc
= TMF_RESP_FUNC_FAILED
;
810 dev_warn(dev
, "Device has been removed\n");
811 return TMF_RESP_FUNC_FAILED
;
814 spin_lock_irqsave(&task
->task_state_lock
, flags
);
815 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
816 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
817 rc
= TMF_RESP_FUNC_COMPLETE
;
821 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
822 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
823 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
824 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
825 struct hisi_sas_slot
*slot
= task
->lldd_task
;
828 int_to_scsilun(cmnd
->device
->lun
, &lun
);
829 tmf_task
.tmf
= TMF_ABORT_TASK
;
830 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
832 rc
= hisi_sas_debug_issue_ssp_tmf(task
->dev
, lun
.scsi_lun
,
835 /* if successful, clear the task and callback forwards.*/
836 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
837 if (task
->lldd_task
) {
838 struct hisi_sas_slot
*slot
;
840 slot
= &hisi_hba
->slot_info
841 [tmf_task
.tag_of_task_to_be_managed
];
842 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
843 hisi_hba
->hw
->slot_complete(hisi_hba
, slot
, 1);
844 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
848 hisi_sas_internal_task_abort(hisi_hba
, device
,
849 HISI_SAS_INT_ABT_CMD
, tag
);
850 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
851 task
->task_proto
& SAS_PROTOCOL_STP
) {
852 if (task
->dev
->dev_type
== SAS_SATA_DEV
) {
853 hisi_sas_internal_task_abort(hisi_hba
, device
,
854 HISI_SAS_INT_ABT_DEV
, 0);
855 rc
= TMF_RESP_FUNC_COMPLETE
;
857 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
859 struct hisi_sas_slot
*slot
= task
->lldd_task
;
862 hisi_sas_internal_task_abort(hisi_hba
, device
,
863 HISI_SAS_INT_ABT_CMD
, tag
);
867 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
868 dev_notice(dev
, "abort task: rc=%d\n", rc
);
872 static int hisi_sas_abort_task_set(struct domain_device
*device
, u8
*lun
)
874 struct hisi_sas_tmf_task tmf_task
;
875 int rc
= TMF_RESP_FUNC_FAILED
;
877 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
878 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
883 static int hisi_sas_clear_aca(struct domain_device
*device
, u8
*lun
)
885 int rc
= TMF_RESP_FUNC_FAILED
;
886 struct hisi_sas_tmf_task tmf_task
;
888 tmf_task
.tmf
= TMF_CLEAR_ACA
;
889 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
894 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device
*device
)
896 struct sas_phy
*phy
= sas_get_local_phy(device
);
897 int rc
, reset_type
= (device
->dev_type
== SAS_SATA_DEV
||
898 (device
->tproto
& SAS_PROTOCOL_STP
)) ? 0 : 1;
899 rc
= sas_phy_reset(phy
, reset_type
);
900 sas_put_local_phy(phy
);
905 static int hisi_sas_I_T_nexus_reset(struct domain_device
*device
)
907 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
908 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
910 int rc
= TMF_RESP_FUNC_FAILED
;
912 if (sas_dev
->dev_status
!= HISI_SAS_DEV_EH
)
913 return TMF_RESP_FUNC_FAILED
;
914 sas_dev
->dev_status
= HISI_SAS_DEV_NORMAL
;
916 rc
= hisi_sas_debug_I_T_nexus_reset(device
);
918 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
919 hisi_sas_release_task(hisi_hba
, device
);
920 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
925 static int hisi_sas_lu_reset(struct domain_device
*device
, u8
*lun
)
927 struct hisi_sas_tmf_task tmf_task
;
928 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
929 struct hisi_hba
*hisi_hba
= dev_to_hisi_hba(device
);
930 struct device
*dev
= &hisi_hba
->pdev
->dev
;
932 int rc
= TMF_RESP_FUNC_FAILED
;
934 tmf_task
.tmf
= TMF_LU_RESET
;
935 sas_dev
->dev_status
= HISI_SAS_DEV_EH
;
936 rc
= hisi_sas_debug_issue_ssp_tmf(device
, lun
, &tmf_task
);
937 if (rc
== TMF_RESP_FUNC_COMPLETE
) {
938 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
939 hisi_sas_release_task(hisi_hba
, device
);
940 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
943 /* If failed, fall-through I_T_Nexus reset */
944 dev_err(dev
, "lu_reset: for device[%llx]:rc= %d\n",
945 sas_dev
->device_id
, rc
);
949 static int hisi_sas_query_task(struct sas_task
*task
)
952 struct hisi_sas_tmf_task tmf_task
;
953 int rc
= TMF_RESP_FUNC_FAILED
;
955 if (task
->lldd_task
&& task
->task_proto
& SAS_PROTOCOL_SSP
) {
956 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
957 struct domain_device
*device
= task
->dev
;
958 struct hisi_sas_slot
*slot
= task
->lldd_task
;
961 int_to_scsilun(cmnd
->device
->lun
, &lun
);
962 tmf_task
.tmf
= TMF_QUERY_TASK
;
963 tmf_task
.tag_of_task_to_be_managed
= cpu_to_le16(tag
);
965 rc
= hisi_sas_debug_issue_ssp_tmf(device
,
969 /* The task is still in Lun, release it then */
970 case TMF_RESP_FUNC_SUCC
:
971 /* The task is not in Lun or failed, reset the phy */
972 case TMF_RESP_FUNC_FAILED
:
973 case TMF_RESP_FUNC_COMPLETE
:
976 rc
= TMF_RESP_FUNC_FAILED
;
984 hisi_sas_internal_abort_task_exec(struct hisi_hba
*hisi_hba
, u64 device_id
,
985 struct sas_task
*task
, int abort_flag
,
988 struct domain_device
*device
= task
->dev
;
989 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
990 struct device
*dev
= &hisi_hba
->pdev
->dev
;
991 struct hisi_sas_port
*port
;
992 struct hisi_sas_slot
*slot
;
993 struct hisi_sas_cmd_hdr
*cmd_hdr_base
;
994 int dlvry_queue_slot
, dlvry_queue
, n_elem
= 0, rc
, slot_idx
;
999 port
= device
->port
->lldd_port
;
1001 /* simply get a slot and send abort command */
1002 rc
= hisi_sas_slot_index_alloc(hisi_hba
, &slot_idx
);
1005 rc
= hisi_hba
->hw
->get_free_slot(hisi_hba
, sas_dev
->device_id
,
1006 &dlvry_queue
, &dlvry_queue_slot
);
1010 slot
= &hisi_hba
->slot_info
[slot_idx
];
1011 memset(slot
, 0, sizeof(struct hisi_sas_slot
));
1013 slot
->idx
= slot_idx
;
1014 slot
->n_elem
= n_elem
;
1015 slot
->dlvry_queue
= dlvry_queue
;
1016 slot
->dlvry_queue_slot
= dlvry_queue_slot
;
1017 cmd_hdr_base
= hisi_hba
->cmd_hdr
[dlvry_queue
];
1018 slot
->cmd_hdr
= &cmd_hdr_base
[dlvry_queue_slot
];
1021 task
->lldd_task
= slot
;
1023 memset(slot
->cmd_hdr
, 0, sizeof(struct hisi_sas_cmd_hdr
));
1025 rc
= hisi_sas_task_prep_abort(hisi_hba
, slot
, device_id
,
1026 abort_flag
, task_tag
);
1030 /* Port structure is static for the HBA, so
1031 * even if the port is deformed it is ok
1034 list_add_tail(&slot
->entry
, &port
->list
);
1035 spin_lock(&task
->task_state_lock
);
1036 task
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
1037 spin_unlock(&task
->task_state_lock
);
1039 hisi_hba
->slot_prep
= slot
;
1041 atomic64_inc(&sas_dev
->running_req
);
1043 /* send abort command to our chip */
1044 hisi_hba
->hw
->start_delivery(hisi_hba
);
1049 hisi_sas_slot_index_free(hisi_hba
, slot_idx
);
1051 dev_err(dev
, "internal abort task prep: failed[%d]!\n", rc
);
1057 * hisi_sas_internal_task_abort -- execute an internal
1058 * abort command for single IO command or a device
1059 * @hisi_hba: host controller struct
1060 * @device: domain device
1061 * @abort_flag: mode of operation, device or single IO
1062 * @tag: tag of IO to be aborted (only relevant to single
1066 hisi_sas_internal_task_abort(struct hisi_hba
*hisi_hba
,
1067 struct domain_device
*device
,
1068 int abort_flag
, int tag
)
1070 struct sas_task
*task
;
1071 struct hisi_sas_device
*sas_dev
= device
->lldd_dev
;
1072 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1074 unsigned long flags
;
1076 if (!hisi_hba
->hw
->prep_abort
)
1079 task
= sas_alloc_slow_task(GFP_KERNEL
);
1084 task
->task_proto
= device
->tproto
;
1085 task
->task_done
= hisi_sas_task_done
;
1086 task
->slow_task
->timer
.data
= (unsigned long)task
;
1087 task
->slow_task
->timer
.function
= hisi_sas_tmf_timedout
;
1088 task
->slow_task
->timer
.expires
= jiffies
+ 20*HZ
;
1089 add_timer(&task
->slow_task
->timer
);
1091 /* Lock as we are alloc'ing a slot, which cannot be interrupted */
1092 spin_lock_irqsave(&hisi_hba
->lock
, flags
);
1093 res
= hisi_sas_internal_abort_task_exec(hisi_hba
, sas_dev
->device_id
,
1094 task
, abort_flag
, tag
);
1095 spin_unlock_irqrestore(&hisi_hba
->lock
, flags
);
1097 del_timer(&task
->slow_task
->timer
);
1098 dev_err(dev
, "internal task abort: executing internal task failed: %d\n",
1102 wait_for_completion(&task
->slow_task
->completion
);
1103 res
= TMF_RESP_FUNC_FAILED
;
1105 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
1106 task
->task_status
.stat
== TMF_RESP_FUNC_COMPLETE
) {
1107 res
= TMF_RESP_FUNC_COMPLETE
;
1111 /* TMF timed out, return direct. */
1112 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
1113 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
1114 dev_err(dev
, "internal task abort: timeout.\n");
1115 if (task
->lldd_task
) {
1116 struct hisi_sas_slot
*slot
= task
->lldd_task
;
1118 hisi_sas_slot_task_free(hisi_hba
, task
, slot
);
1124 dev_dbg(dev
, "internal task abort: task to dev %016llx task=%p "
1125 "resp: 0x%x sts 0x%x\n",
1126 SAS_ADDR(device
->sas_addr
),
1128 task
->task_status
.resp
, /* 0 is complete, -1 is undelivered */
1129 task
->task_status
.stat
);
1130 sas_free_task(task
);
1135 static void hisi_sas_port_formed(struct asd_sas_phy
*sas_phy
)
1137 hisi_sas_port_notify_formed(sas_phy
);
1140 static void hisi_sas_port_deformed(struct asd_sas_phy
*sas_phy
)
1142 hisi_sas_port_notify_deformed(sas_phy
);
1145 static void hisi_sas_phy_disconnected(struct hisi_sas_phy
*phy
)
1147 phy
->phy_attached
= 0;
1152 void hisi_sas_phy_down(struct hisi_hba
*hisi_hba
, int phy_no
, int rdy
)
1154 struct hisi_sas_phy
*phy
= &hisi_hba
->phy
[phy_no
];
1155 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1156 struct sas_ha_struct
*sas_ha
= &hisi_hba
->sha
;
1159 /* Phy down but ready */
1160 hisi_sas_bytes_dmaed(hisi_hba
, phy_no
);
1161 hisi_sas_port_notify_formed(sas_phy
);
1163 struct hisi_sas_port
*port
= phy
->port
;
1165 /* Phy down and not ready */
1166 sas_ha
->notify_phy_event(sas_phy
, PHYE_LOSS_OF_SIGNAL
);
1167 sas_phy_disconnected(sas_phy
);
1170 if (phy
->phy_type
& PORT_TYPE_SAS
) {
1171 int port_id
= port
->id
;
1173 if (!hisi_hba
->hw
->get_wideport_bitmap(hisi_hba
,
1175 port
->port_attached
= 0;
1176 } else if (phy
->phy_type
& PORT_TYPE_SATA
)
1177 port
->port_attached
= 0;
1179 hisi_sas_phy_disconnected(phy
);
1182 EXPORT_SYMBOL_GPL(hisi_sas_phy_down
);
1184 static struct scsi_transport_template
*hisi_sas_stt
;
1186 static struct scsi_host_template hisi_sas_sht
= {
1187 .module
= THIS_MODULE
,
1189 .queuecommand
= sas_queuecommand
,
1190 .target_alloc
= sas_target_alloc
,
1191 .slave_configure
= hisi_sas_slave_configure
,
1192 .scan_finished
= hisi_sas_scan_finished
,
1193 .scan_start
= hisi_sas_scan_start
,
1194 .change_queue_depth
= sas_change_queue_depth
,
1195 .bios_param
= sas_bios_param
,
1198 .sg_tablesize
= SG_ALL
,
1199 .max_sectors
= SCSI_DEFAULT_MAX_SECTORS
,
1200 .use_clustering
= ENABLE_CLUSTERING
,
1201 .eh_device_reset_handler
= sas_eh_device_reset_handler
,
1202 .eh_bus_reset_handler
= sas_eh_bus_reset_handler
,
1203 .target_destroy
= sas_target_destroy
,
1207 static struct sas_domain_function_template hisi_sas_transport_ops
= {
1208 .lldd_dev_found
= hisi_sas_dev_found
,
1209 .lldd_dev_gone
= hisi_sas_dev_gone
,
1210 .lldd_execute_task
= hisi_sas_queue_command
,
1211 .lldd_control_phy
= hisi_sas_control_phy
,
1212 .lldd_abort_task
= hisi_sas_abort_task
,
1213 .lldd_abort_task_set
= hisi_sas_abort_task_set
,
1214 .lldd_clear_aca
= hisi_sas_clear_aca
,
1215 .lldd_I_T_nexus_reset
= hisi_sas_I_T_nexus_reset
,
1216 .lldd_lu_reset
= hisi_sas_lu_reset
,
1217 .lldd_query_task
= hisi_sas_query_task
,
1218 .lldd_port_formed
= hisi_sas_port_formed
,
1219 .lldd_port_deformed
= hisi_sas_port_deformed
,
1222 static int hisi_sas_alloc(struct hisi_hba
*hisi_hba
, struct Scsi_Host
*shost
)
1224 struct platform_device
*pdev
= hisi_hba
->pdev
;
1225 struct device
*dev
= &pdev
->dev
;
1226 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1228 spin_lock_init(&hisi_hba
->lock
);
1229 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1230 hisi_sas_phy_init(hisi_hba
, i
);
1231 hisi_hba
->port
[i
].port_attached
= 0;
1232 hisi_hba
->port
[i
].id
= -1;
1233 INIT_LIST_HEAD(&hisi_hba
->port
[i
].list
);
1236 for (i
= 0; i
< HISI_SAS_MAX_DEVICES
; i
++) {
1237 hisi_hba
->devices
[i
].dev_type
= SAS_PHY_UNUSED
;
1238 hisi_hba
->devices
[i
].device_id
= i
;
1239 hisi_hba
->devices
[i
].dev_status
= HISI_SAS_DEV_NORMAL
;
1242 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1243 struct hisi_sas_cq
*cq
= &hisi_hba
->cq
[i
];
1244 struct hisi_sas_dq
*dq
= &hisi_hba
->dq
[i
];
1246 /* Completion queue structure */
1248 cq
->hisi_hba
= hisi_hba
;
1250 /* Delivery queue structure */
1252 dq
->hisi_hba
= hisi_hba
;
1254 /* Delivery queue */
1255 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1256 hisi_hba
->cmd_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1257 &hisi_hba
->cmd_hdr_dma
[i
], GFP_KERNEL
);
1258 if (!hisi_hba
->cmd_hdr
[i
])
1260 memset(hisi_hba
->cmd_hdr
[i
], 0, s
);
1262 /* Completion queue */
1263 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1264 hisi_hba
->complete_hdr
[i
] = dma_alloc_coherent(dev
, s
,
1265 &hisi_hba
->complete_hdr_dma
[i
], GFP_KERNEL
);
1266 if (!hisi_hba
->complete_hdr
[i
])
1268 memset(hisi_hba
->complete_hdr
[i
], 0, s
);
1271 s
= HISI_SAS_STATUS_BUF_SZ
;
1272 hisi_hba
->status_buffer_pool
= dma_pool_create("status_buffer",
1274 if (!hisi_hba
->status_buffer_pool
)
1277 s
= HISI_SAS_COMMAND_TABLE_SZ
;
1278 hisi_hba
->command_table_pool
= dma_pool_create("command_table",
1280 if (!hisi_hba
->command_table_pool
)
1283 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1284 hisi_hba
->itct
= dma_alloc_coherent(dev
, s
, &hisi_hba
->itct_dma
,
1286 if (!hisi_hba
->itct
)
1289 memset(hisi_hba
->itct
, 0, s
);
1291 hisi_hba
->slot_info
= devm_kcalloc(dev
, max_command_entries
,
1292 sizeof(struct hisi_sas_slot
),
1294 if (!hisi_hba
->slot_info
)
1297 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1298 hisi_hba
->iost
= dma_alloc_coherent(dev
, s
, &hisi_hba
->iost_dma
,
1300 if (!hisi_hba
->iost
)
1303 memset(hisi_hba
->iost
, 0, s
);
1305 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1306 hisi_hba
->breakpoint
= dma_alloc_coherent(dev
, s
,
1307 &hisi_hba
->breakpoint_dma
, GFP_KERNEL
);
1308 if (!hisi_hba
->breakpoint
)
1311 memset(hisi_hba
->breakpoint
, 0, s
);
1313 hisi_hba
->slot_index_count
= max_command_entries
;
1314 s
= hisi_hba
->slot_index_count
/ BITS_PER_BYTE
;
1315 hisi_hba
->slot_index_tags
= devm_kzalloc(dev
, s
, GFP_KERNEL
);
1316 if (!hisi_hba
->slot_index_tags
)
1319 hisi_hba
->sge_page_pool
= dma_pool_create("status_sge", dev
,
1320 sizeof(struct hisi_sas_sge_page
), 16, 0);
1321 if (!hisi_hba
->sge_page_pool
)
1324 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1325 hisi_hba
->initial_fis
= dma_alloc_coherent(dev
, s
,
1326 &hisi_hba
->initial_fis_dma
, GFP_KERNEL
);
1327 if (!hisi_hba
->initial_fis
)
1329 memset(hisi_hba
->initial_fis
, 0, s
);
1331 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
) * 2;
1332 hisi_hba
->sata_breakpoint
= dma_alloc_coherent(dev
, s
,
1333 &hisi_hba
->sata_breakpoint_dma
, GFP_KERNEL
);
1334 if (!hisi_hba
->sata_breakpoint
)
1336 memset(hisi_hba
->sata_breakpoint
, 0, s
);
1338 hisi_sas_slot_index_init(hisi_hba
);
1340 hisi_hba
->wq
= create_singlethread_workqueue(dev_name(dev
));
1341 if (!hisi_hba
->wq
) {
1342 dev_err(dev
, "sas_alloc: failed to create workqueue\n");
1351 static void hisi_sas_free(struct hisi_hba
*hisi_hba
)
1353 struct device
*dev
= &hisi_hba
->pdev
->dev
;
1354 int i
, s
, max_command_entries
= hisi_hba
->hw
->max_command_entries
;
1356 for (i
= 0; i
< hisi_hba
->queue_count
; i
++) {
1357 s
= sizeof(struct hisi_sas_cmd_hdr
) * HISI_SAS_QUEUE_SLOTS
;
1358 if (hisi_hba
->cmd_hdr
[i
])
1359 dma_free_coherent(dev
, s
,
1360 hisi_hba
->cmd_hdr
[i
],
1361 hisi_hba
->cmd_hdr_dma
[i
]);
1363 s
= hisi_hba
->hw
->complete_hdr_size
* HISI_SAS_QUEUE_SLOTS
;
1364 if (hisi_hba
->complete_hdr
[i
])
1365 dma_free_coherent(dev
, s
,
1366 hisi_hba
->complete_hdr
[i
],
1367 hisi_hba
->complete_hdr_dma
[i
]);
1370 dma_pool_destroy(hisi_hba
->status_buffer_pool
);
1371 dma_pool_destroy(hisi_hba
->command_table_pool
);
1372 dma_pool_destroy(hisi_hba
->sge_page_pool
);
1374 s
= HISI_SAS_MAX_ITCT_ENTRIES
* sizeof(struct hisi_sas_itct
);
1376 dma_free_coherent(dev
, s
,
1377 hisi_hba
->itct
, hisi_hba
->itct_dma
);
1379 s
= max_command_entries
* sizeof(struct hisi_sas_iost
);
1381 dma_free_coherent(dev
, s
,
1382 hisi_hba
->iost
, hisi_hba
->iost_dma
);
1384 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
);
1385 if (hisi_hba
->breakpoint
)
1386 dma_free_coherent(dev
, s
,
1387 hisi_hba
->breakpoint
,
1388 hisi_hba
->breakpoint_dma
);
1391 s
= sizeof(struct hisi_sas_initial_fis
) * HISI_SAS_MAX_PHYS
;
1392 if (hisi_hba
->initial_fis
)
1393 dma_free_coherent(dev
, s
,
1394 hisi_hba
->initial_fis
,
1395 hisi_hba
->initial_fis_dma
);
1397 s
= max_command_entries
* sizeof(struct hisi_sas_breakpoint
) * 2;
1398 if (hisi_hba
->sata_breakpoint
)
1399 dma_free_coherent(dev
, s
,
1400 hisi_hba
->sata_breakpoint
,
1401 hisi_hba
->sata_breakpoint_dma
);
1404 destroy_workqueue(hisi_hba
->wq
);
1407 static struct Scsi_Host
*hisi_sas_shost_alloc(struct platform_device
*pdev
,
1408 const struct hisi_sas_hw
*hw
)
1410 struct resource
*res
;
1411 struct Scsi_Host
*shost
;
1412 struct hisi_hba
*hisi_hba
;
1413 struct device
*dev
= &pdev
->dev
;
1414 struct device_node
*np
= pdev
->dev
.of_node
;
1417 shost
= scsi_host_alloc(&hisi_sas_sht
, sizeof(*hisi_hba
));
1419 dev_err(dev
, "scsi host alloc failed\n");
1422 hisi_hba
= shost_priv(shost
);
1425 hisi_hba
->pdev
= pdev
;
1426 hisi_hba
->shost
= shost
;
1427 SHOST_TO_SAS_HA(shost
) = &hisi_hba
->sha
;
1429 init_timer(&hisi_hba
->timer
);
1431 if (device_property_read_u8_array(dev
, "sas-addr", hisi_hba
->sas_addr
,
1436 hisi_hba
->ctrl
= syscon_regmap_lookup_by_phandle(np
,
1437 "hisilicon,sas-syscon");
1438 if (IS_ERR(hisi_hba
->ctrl
))
1441 if (device_property_read_u32(dev
, "ctrl-reset-reg",
1442 &hisi_hba
->ctrl_reset_reg
))
1445 if (device_property_read_u32(dev
, "ctrl-reset-sts-reg",
1446 &hisi_hba
->ctrl_reset_sts_reg
))
1449 if (device_property_read_u32(dev
, "ctrl-clock-ena-reg",
1450 &hisi_hba
->ctrl_clock_ena_reg
))
1454 refclk
= devm_clk_get(&pdev
->dev
, NULL
);
1456 dev_dbg(dev
, "no ref clk property\n");
1458 hisi_hba
->refclk_frequency_mhz
= clk_get_rate(refclk
) / 1000000;
1460 if (device_property_read_u32(dev
, "phy-count", &hisi_hba
->n_phy
))
1463 if (device_property_read_u32(dev
, "queue-count",
1464 &hisi_hba
->queue_count
))
1467 if (dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(64)) &&
1468 dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32))) {
1469 dev_err(dev
, "No usable DMA addressing method\n");
1473 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1474 hisi_hba
->regs
= devm_ioremap_resource(dev
, res
);
1475 if (IS_ERR(hisi_hba
->regs
))
1478 if (hisi_sas_alloc(hisi_hba
, shost
)) {
1479 hisi_sas_free(hisi_hba
);
1486 dev_err(dev
, "shost alloc failed\n");
1490 static void hisi_sas_init_add(struct hisi_hba
*hisi_hba
)
1494 for (i
= 0; i
< hisi_hba
->n_phy
; i
++)
1495 memcpy(&hisi_hba
->phy
[i
].dev_sas_addr
,
1500 int hisi_sas_probe(struct platform_device
*pdev
,
1501 const struct hisi_sas_hw
*hw
)
1503 struct Scsi_Host
*shost
;
1504 struct hisi_hba
*hisi_hba
;
1505 struct device
*dev
= &pdev
->dev
;
1506 struct asd_sas_phy
**arr_phy
;
1507 struct asd_sas_port
**arr_port
;
1508 struct sas_ha_struct
*sha
;
1509 int rc
, phy_nr
, port_nr
, i
;
1511 shost
= hisi_sas_shost_alloc(pdev
, hw
);
1515 sha
= SHOST_TO_SAS_HA(shost
);
1516 hisi_hba
= shost_priv(shost
);
1517 platform_set_drvdata(pdev
, sha
);
1519 phy_nr
= port_nr
= hisi_hba
->n_phy
;
1521 arr_phy
= devm_kcalloc(dev
, phy_nr
, sizeof(void *), GFP_KERNEL
);
1522 arr_port
= devm_kcalloc(dev
, port_nr
, sizeof(void *), GFP_KERNEL
);
1523 if (!arr_phy
|| !arr_port
) {
1528 sha
->sas_phy
= arr_phy
;
1529 sha
->sas_port
= arr_port
;
1530 sha
->lldd_ha
= hisi_hba
;
1532 shost
->transportt
= hisi_sas_stt
;
1533 shost
->max_id
= HISI_SAS_MAX_DEVICES
;
1534 shost
->max_lun
= ~0;
1535 shost
->max_channel
= 1;
1536 shost
->max_cmd_len
= 16;
1537 shost
->sg_tablesize
= min_t(u16
, SG_ALL
, HISI_SAS_SGE_PAGE_CNT
);
1538 shost
->can_queue
= hisi_hba
->hw
->max_command_entries
;
1539 shost
->cmd_per_lun
= hisi_hba
->hw
->max_command_entries
;
1541 sha
->sas_ha_name
= DRV_NAME
;
1542 sha
->dev
= &hisi_hba
->pdev
->dev
;
1543 sha
->lldd_module
= THIS_MODULE
;
1544 sha
->sas_addr
= &hisi_hba
->sas_addr
[0];
1545 sha
->num_phys
= hisi_hba
->n_phy
;
1546 sha
->core
.shost
= hisi_hba
->shost
;
1548 for (i
= 0; i
< hisi_hba
->n_phy
; i
++) {
1549 sha
->sas_phy
[i
] = &hisi_hba
->phy
[i
].sas_phy
;
1550 sha
->sas_port
[i
] = &hisi_hba
->port
[i
].sas_port
;
1553 hisi_sas_init_add(hisi_hba
);
1555 rc
= scsi_add_host(shost
, &pdev
->dev
);
1559 rc
= sas_register_ha(sha
);
1561 goto err_out_register_ha
;
1563 rc
= hisi_hba
->hw
->hw_init(hisi_hba
);
1565 goto err_out_register_ha
;
1567 scsi_scan_host(shost
);
1571 err_out_register_ha
:
1572 scsi_remove_host(shost
);
1574 hisi_sas_free(hisi_hba
);
1578 EXPORT_SYMBOL_GPL(hisi_sas_probe
);
1580 int hisi_sas_remove(struct platform_device
*pdev
)
1582 struct sas_ha_struct
*sha
= platform_get_drvdata(pdev
);
1583 struct hisi_hba
*hisi_hba
= sha
->lldd_ha
;
1584 struct Scsi_Host
*shost
= sha
->core
.shost
;
1586 scsi_remove_host(sha
->core
.shost
);
1587 sas_unregister_ha(sha
);
1588 sas_remove_host(sha
->core
.shost
);
1590 hisi_sas_free(hisi_hba
);
1594 EXPORT_SYMBOL_GPL(hisi_sas_remove
);
1596 static __init
int hisi_sas_init(void)
1598 pr_info("hisi_sas: driver version %s\n", DRV_VERSION
);
1600 hisi_sas_stt
= sas_domain_attach_transport(&hisi_sas_transport_ops
);
1607 static __exit
void hisi_sas_exit(void)
1609 sas_release_transport(hisi_sas_stt
);
1612 module_init(hisi_sas_init
);
1613 module_exit(hisi_sas_exit
);
1615 MODULE_VERSION(DRV_VERSION
);
1616 MODULE_LICENSE("GPL");
1617 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1618 MODULE_DESCRIPTION("HISILICON SAS controller driver");
1619 MODULE_ALIAS("platform:" DRV_NAME
);