1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2015 Linaro Ltd.
4 * Copyright (c) 2015 Hisilicon Limited.
8 #define DRV_NAME "hisi_sas"
10 #define DEV_IS_GONE(dev) \
11 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
13 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
14 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
16 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
17 struct domain_device *device);
18 static void hisi_sas_dev_gone(struct domain_device *device);
20 struct hisi_sas_internal_abort_data {
21 bool rst_ha_timeout; /* reset the HA for timeout */
24 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
26 switch (fis->command) {
27 case ATA_CMD_FPDMA_WRITE:
28 case ATA_CMD_FPDMA_READ:
29 case ATA_CMD_FPDMA_RECV:
30 case ATA_CMD_FPDMA_SEND:
31 case ATA_CMD_NCQ_NON_DATA:
32 return HISI_SAS_SATA_PROTOCOL_FPDMA;
34 case ATA_CMD_DOWNLOAD_MICRO:
36 case ATA_CMD_PMP_READ:
37 case ATA_CMD_READ_LOG_EXT:
38 case ATA_CMD_PIO_READ:
39 case ATA_CMD_PIO_READ_EXT:
40 case ATA_CMD_PMP_WRITE:
41 case ATA_CMD_WRITE_LOG_EXT:
42 case ATA_CMD_PIO_WRITE:
43 case ATA_CMD_PIO_WRITE_EXT:
44 return HISI_SAS_SATA_PROTOCOL_PIO;
47 case ATA_CMD_DOWNLOAD_MICRO_DMA:
48 case ATA_CMD_PMP_READ_DMA:
49 case ATA_CMD_PMP_WRITE_DMA:
51 case ATA_CMD_READ_EXT:
52 case ATA_CMD_READ_LOG_DMA_EXT:
53 case ATA_CMD_READ_STREAM_DMA_EXT:
54 case ATA_CMD_TRUSTED_RCV_DMA:
55 case ATA_CMD_TRUSTED_SND_DMA:
57 case ATA_CMD_WRITE_EXT:
58 case ATA_CMD_WRITE_FUA_EXT:
59 case ATA_CMD_WRITE_QUEUED:
60 case ATA_CMD_WRITE_LOG_DMA_EXT:
61 case ATA_CMD_WRITE_STREAM_DMA_EXT:
62 case ATA_CMD_ZAC_MGMT_IN:
63 return HISI_SAS_SATA_PROTOCOL_DMA;
65 case ATA_CMD_CHK_POWER:
66 case ATA_CMD_DEV_RESET:
69 case ATA_CMD_FLUSH_EXT:
71 case ATA_CMD_VERIFY_EXT:
72 case ATA_CMD_SET_FEATURES:
74 case ATA_CMD_STANDBYNOW1:
75 case ATA_CMD_ZAC_MGMT_OUT:
76 return HISI_SAS_SATA_PROTOCOL_NONDATA;
79 switch (fis->features) {
80 case ATA_SET_MAX_PASSWD:
81 case ATA_SET_MAX_LOCK:
82 return HISI_SAS_SATA_PROTOCOL_PIO;
84 case ATA_SET_MAX_PASSWD_DMA:
85 case ATA_SET_MAX_UNLOCK_DMA:
86 return HISI_SAS_SATA_PROTOCOL_DMA;
89 return HISI_SAS_SATA_PROTOCOL_NONDATA;
94 if (direction == DMA_NONE)
95 return HISI_SAS_SATA_PROTOCOL_NONDATA;
96 return HISI_SAS_SATA_PROTOCOL_PIO;
100 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
102 void hisi_sas_sata_done(struct sas_task *task,
103 struct hisi_sas_slot *slot)
105 struct task_status_struct *ts = &task->task_status;
106 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
107 struct hisi_sas_status_buffer *status_buf =
108 hisi_sas_status_buf_addr_mem(slot);
109 u8 *iu = &status_buf->iu[0];
110 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
112 resp->frame_len = sizeof(struct dev_to_host_fis);
113 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
115 ts->buf_valid_size = sizeof(*resp);
117 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
120 * This function assumes linkrate mask fits in 8 bits, which it
121 * does for all HW versions supported.
123 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
128 max -= SAS_LINK_RATE_1_5_GBPS;
129 for (i = 0; i <= max; i++)
130 rate |= 1 << (i * 2);
133 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
135 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
137 return device->port->ha->lldd_ha;
140 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
142 return container_of(sas_port, struct hisi_sas_port, sas_port);
144 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
146 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
150 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
151 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
153 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
155 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
157 void *bitmap = hisi_hba->slot_index_tags;
159 __clear_bit(slot_idx, bitmap);
162 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
164 if (hisi_hba->hw->slot_index_alloc ||
165 slot_idx < HISI_SAS_RESERVED_IPTT) {
166 spin_lock(&hisi_hba->lock);
167 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
168 spin_unlock(&hisi_hba->lock);
172 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
174 void *bitmap = hisi_hba->slot_index_tags;
176 __set_bit(slot_idx, bitmap);
179 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba,
183 void *bitmap = hisi_hba->slot_index_tags;
186 return rq->tag + HISI_SAS_RESERVED_IPTT;
188 spin_lock(&hisi_hba->lock);
189 index = find_next_zero_bit(bitmap, HISI_SAS_RESERVED_IPTT,
190 hisi_hba->last_slot_index + 1);
191 if (index >= HISI_SAS_RESERVED_IPTT) {
192 index = find_next_zero_bit(bitmap,
193 HISI_SAS_RESERVED_IPTT,
195 if (index >= HISI_SAS_RESERVED_IPTT) {
196 spin_unlock(&hisi_hba->lock);
197 return -SAS_QUEUE_FULL;
200 hisi_sas_slot_index_set(hisi_hba, index);
201 hisi_hba->last_slot_index = index;
202 spin_unlock(&hisi_hba->lock);
207 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
208 struct hisi_sas_slot *slot, bool need_lock)
210 int device_id = slot->device_id;
211 struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id];
214 struct device *dev = hisi_hba->dev;
216 if (!task->lldd_task)
219 task->lldd_task = NULL;
221 if (!sas_protocol_ata(task->task_proto)) {
223 if (task->task_proto & SAS_PROTOCOL_SSP)
224 dma_unmap_sg(dev, task->scatter,
228 dma_unmap_sg(dev, &task->smp_task.smp_req,
231 if (slot->n_elem_dif) {
232 struct sas_ssp_task *ssp_task = &task->ssp_task;
233 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
235 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
236 scsi_prot_sg_count(scsi_cmnd),
243 spin_lock(&sas_dev->lock);
244 list_del_init(&slot->entry);
245 spin_unlock(&sas_dev->lock);
247 list_del_init(&slot->entry);
250 memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
252 hisi_sas_slot_index_free(hisi_hba, slot->idx);
254 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
256 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
257 struct hisi_sas_slot *slot)
259 hisi_hba->hw->prep_smp(hisi_hba, slot);
262 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
263 struct hisi_sas_slot *slot)
265 hisi_hba->hw->prep_ssp(hisi_hba, slot);
268 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
269 struct hisi_sas_slot *slot)
271 hisi_hba->hw->prep_stp(hisi_hba, slot);
274 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
275 struct hisi_sas_slot *slot)
277 hisi_hba->hw->prep_abort(hisi_hba, slot);
280 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
281 struct sas_task *task, int n_elem)
283 struct device *dev = hisi_hba->dev;
285 if (!sas_protocol_ata(task->task_proto) && n_elem) {
286 if (task->num_scatter) {
287 dma_unmap_sg(dev, task->scatter, task->num_scatter,
289 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
290 dma_unmap_sg(dev, &task->smp_task.smp_req,
296 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
297 struct sas_task *task, int *n_elem)
299 struct device *dev = hisi_hba->dev;
302 if (sas_protocol_ata(task->task_proto)) {
303 *n_elem = task->num_scatter;
305 unsigned int req_len;
307 if (task->num_scatter) {
308 *n_elem = dma_map_sg(dev, task->scatter,
309 task->num_scatter, task->data_dir);
314 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
315 *n_elem = dma_map_sg(dev, &task->smp_task.smp_req,
321 req_len = sg_dma_len(&task->smp_task.smp_req);
324 goto err_out_dma_unmap;
329 if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
330 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT\n",
333 goto err_out_dma_unmap;
338 /* It would be better to call dma_unmap_sg() here, but it's messy */
339 hisi_sas_dma_unmap(hisi_hba, task, *n_elem);
344 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
345 struct sas_task *task, int n_elem_dif)
347 struct device *dev = hisi_hba->dev;
350 struct sas_ssp_task *ssp_task = &task->ssp_task;
351 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
353 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
354 scsi_prot_sg_count(scsi_cmnd),
359 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
360 int *n_elem_dif, struct sas_task *task)
362 struct device *dev = hisi_hba->dev;
363 struct sas_ssp_task *ssp_task;
364 struct scsi_cmnd *scsi_cmnd;
367 if (task->num_scatter) {
368 ssp_task = &task->ssp_task;
369 scsi_cmnd = ssp_task->cmd;
371 if (scsi_prot_sg_count(scsi_cmnd)) {
372 *n_elem_dif = dma_map_sg(dev,
373 scsi_prot_sglist(scsi_cmnd),
374 scsi_prot_sg_count(scsi_cmnd),
380 if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
381 dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
384 goto err_out_dif_dma_unmap;
391 err_out_dif_dma_unmap:
392 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
393 scsi_prot_sg_count(scsi_cmnd), task->data_dir);
398 void hisi_sas_task_deliver(struct hisi_hba *hisi_hba,
399 struct hisi_sas_slot *slot,
400 struct hisi_sas_dq *dq,
401 struct hisi_sas_device *sas_dev)
403 struct hisi_sas_cmd_hdr *cmd_hdr_base;
404 int dlvry_queue_slot, dlvry_queue;
405 struct sas_task *task = slot->task;
408 spin_lock(&dq->lock);
409 wr_q_index = dq->wr_point;
410 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
411 list_add_tail(&slot->delivery, &dq->list);
412 spin_unlock(&dq->lock);
413 spin_lock(&sas_dev->lock);
414 list_add_tail(&slot->entry, &sas_dev->list);
415 spin_unlock(&sas_dev->lock);
417 dlvry_queue = dq->id;
418 dlvry_queue_slot = wr_q_index;
420 slot->device_id = sas_dev->device_id;
421 slot->dlvry_queue = dlvry_queue;
422 slot->dlvry_queue_slot = dlvry_queue_slot;
423 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
424 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
426 task->lldd_task = slot;
428 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
429 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
430 memset(hisi_sas_status_buf_addr_mem(slot), 0,
431 sizeof(struct hisi_sas_err_record));
433 switch (task->task_proto) {
434 case SAS_PROTOCOL_SMP:
435 hisi_sas_task_prep_smp(hisi_hba, slot);
437 case SAS_PROTOCOL_SSP:
438 hisi_sas_task_prep_ssp(hisi_hba, slot);
440 case SAS_PROTOCOL_SATA:
441 case SAS_PROTOCOL_STP:
442 case SAS_PROTOCOL_STP_ALL:
443 hisi_sas_task_prep_ata(hisi_hba, slot);
445 case SAS_PROTOCOL_INTERNAL_ABORT:
446 hisi_sas_task_prep_abort(hisi_hba, slot);
452 /* Make slot memories observable before marking as ready */
454 WRITE_ONCE(slot->ready, 1);
456 spin_lock(&dq->lock);
457 hisi_hba->hw->start_delivery(dq);
458 spin_unlock(&dq->lock);
461 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
463 int n_elem = 0, n_elem_dif = 0;
464 struct domain_device *device = task->dev;
465 struct asd_sas_port *sas_port = device->port;
466 struct hisi_sas_device *sas_dev = device->lldd_dev;
467 bool internal_abort = sas_is_internal_abort(task);
468 struct hisi_sas_dq *dq = NULL;
469 struct hisi_sas_port *port;
470 struct hisi_hba *hisi_hba;
471 struct hisi_sas_slot *slot;
472 struct request *rq = NULL;
477 struct task_status_struct *ts = &task->task_status;
479 ts->resp = SAS_TASK_UNDELIVERED;
480 ts->stat = SAS_PHY_DOWN;
482 * libsas will use dev->port, should
483 * not call task_done for sata
485 if (device->dev_type != SAS_SATA_DEV && !internal_abort)
486 task->task_done(task);
490 hisi_hba = dev_to_hisi_hba(device);
493 switch (task->task_proto) {
494 case SAS_PROTOCOL_SSP:
495 case SAS_PROTOCOL_SMP:
496 case SAS_PROTOCOL_SATA:
497 case SAS_PROTOCOL_STP:
498 case SAS_PROTOCOL_STP_ALL:
499 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
500 if (!gfpflags_allow_blocking(gfp_flags))
503 down(&hisi_hba->sem);
507 if (DEV_IS_GONE(sas_dev)) {
509 dev_info(dev, "task prep: device %d not ready\n",
512 dev_info(dev, "task prep: device %016llx not ready\n",
513 SAS_ADDR(device->sas_addr));
518 port = to_hisi_sas_port(sas_port);
519 if (!port->port_attached) {
520 dev_info(dev, "task prep: %s port%d not attach device\n",
521 dev_is_sata(device) ? "SATA/STP" : "SAS",
527 rq = sas_task_find_rq(task);
529 unsigned int dq_index;
532 blk_tag = blk_mq_unique_tag(rq);
533 dq_index = blk_mq_unique_tag_to_hwq(blk_tag);
534 dq = &hisi_hba->dq[dq_index];
538 if (hisi_hba->iopoll_q_cnt) {
540 * Use interrupt queue (queue 0) to deliver and complete
541 * internal IOs of libsas or libata when there is at least
546 struct Scsi_Host *shost = hisi_hba->shost;
547 struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
549 queue = qmap->mq_map[raw_smp_processor_id()];
551 dq = &hisi_hba->dq[queue];
554 case SAS_PROTOCOL_INTERNAL_ABORT:
555 if (!hisi_hba->hw->prep_abort)
556 return TMF_RESP_FUNC_FAILED;
558 if (test_bit(HISI_SAS_HW_FAULT_BIT, &hisi_hba->flags))
561 hisi_hba = dev_to_hisi_hba(device);
563 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
566 port = to_hisi_sas_port(sas_port);
567 dq = &hisi_hba->dq[task->abort_task.qid];
570 dev_err(hisi_hba->dev, "task prep: unknown/unsupported proto (0x%x)\n",
575 rc = hisi_sas_dma_map(hisi_hba, task, &n_elem);
579 if (!sas_protocol_ata(task->task_proto)) {
580 rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
582 goto err_out_dma_unmap;
585 if (!internal_abort && hisi_hba->hw->slot_index_alloc)
586 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
588 rc = hisi_sas_slot_index_alloc(hisi_hba, rq);
591 goto err_out_dif_dma_unmap;
593 slot = &hisi_hba->slot_info[rc];
594 slot->n_elem = n_elem;
595 slot->n_elem_dif = n_elem_dif;
599 slot->tmf = task->tmf;
600 slot->is_internal = !!task->tmf || internal_abort;
602 /* protect task_prep and start_delivery sequence */
603 hisi_sas_task_deliver(hisi_hba, slot, dq, sas_dev);
607 err_out_dif_dma_unmap:
608 if (!sas_protocol_ata(task->task_proto))
609 hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
611 hisi_sas_dma_unmap(hisi_hba, task, n_elem);
613 dev_err(dev, "task exec: failed[%d]!\n", rc);
617 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no,
620 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
621 struct asd_sas_phy *sas_phy = &phy->sas_phy;
623 if (!phy->phy_attached)
626 sas_notify_phy_event(sas_phy, PHYE_OOB_DONE, gfp_flags);
629 struct sas_phy *sphy = sas_phy->phy;
631 sphy->negotiated_linkrate = sas_phy->linkrate;
632 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
633 sphy->maximum_linkrate_hw =
634 hisi_hba->hw->phy_get_max_linkrate();
635 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
636 sphy->minimum_linkrate = phy->minimum_linkrate;
638 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
639 sphy->maximum_linkrate = phy->maximum_linkrate;
642 if (phy->phy_type & PORT_TYPE_SAS) {
643 struct sas_identify_frame *id;
645 id = (struct sas_identify_frame *)phy->frame_rcvd;
646 id->dev_type = phy->identify.device_type;
647 id->initiator_bits = SAS_PROTOCOL_ALL;
648 id->target_bits = phy->identify.target_port_protocols;
649 } else if (phy->phy_type & PORT_TYPE_SATA) {
653 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
654 sas_notify_port_event(sas_phy, PORTE_BYTES_DMAED, gfp_flags);
657 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
659 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
660 struct hisi_sas_device *sas_dev = NULL;
661 int last = hisi_hba->last_dev_id;
662 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
665 spin_lock(&hisi_hba->lock);
666 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
667 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
668 int queue = i % hisi_hba->queue_count;
669 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
671 hisi_hba->devices[i].device_id = i;
672 sas_dev = &hisi_hba->devices[i];
673 sas_dev->dev_status = HISI_SAS_DEV_INIT;
674 sas_dev->dev_type = device->dev_type;
675 sas_dev->hisi_hba = hisi_hba;
676 sas_dev->sas_device = device;
678 spin_lock_init(&sas_dev->lock);
679 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
684 hisi_hba->last_dev_id = i;
685 spin_unlock(&hisi_hba->lock);
690 static void hisi_sas_sync_poll_cq(struct hisi_sas_cq *cq)
692 /* make sure CQ entries being processed are processed to completion */
693 spin_lock(&cq->poll_lock);
694 spin_unlock(&cq->poll_lock);
697 static bool hisi_sas_queue_is_poll(struct hisi_sas_cq *cq)
699 struct hisi_hba *hisi_hba = cq->hisi_hba;
701 if (cq->id < hisi_hba->queue_count - hisi_hba->iopoll_q_cnt)
706 static void hisi_sas_sync_cq(struct hisi_sas_cq *cq)
708 if (hisi_sas_queue_is_poll(cq))
709 hisi_sas_sync_poll_cq(cq);
711 synchronize_irq(cq->irq_no);
714 void hisi_sas_sync_poll_cqs(struct hisi_hba *hisi_hba)
718 for (i = 0; i < hisi_hba->queue_count; i++) {
719 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
721 if (hisi_sas_queue_is_poll(cq))
722 hisi_sas_sync_poll_cq(cq);
725 EXPORT_SYMBOL_GPL(hisi_sas_sync_poll_cqs);
727 void hisi_sas_sync_cqs(struct hisi_hba *hisi_hba)
731 for (i = 0; i < hisi_hba->queue_count; i++) {
732 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
734 hisi_sas_sync_cq(cq);
737 EXPORT_SYMBOL_GPL(hisi_sas_sync_cqs);
739 static void hisi_sas_tmf_aborted(struct sas_task *task)
741 struct hisi_sas_slot *slot = task->lldd_task;
742 struct domain_device *device = task->dev;
743 struct hisi_sas_device *sas_dev = device->lldd_dev;
744 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
747 struct hisi_sas_cq *cq =
748 &hisi_hba->cq[slot->dlvry_queue];
750 * sync irq or poll queue to avoid free'ing task
751 * before using task in IO completion
753 hisi_sas_sync_cq(cq);
758 #define HISI_SAS_DISK_RECOVER_CNT 3
759 static int hisi_sas_init_device(struct domain_device *device)
761 int rc = TMF_RESP_FUNC_COMPLETE;
763 int retry = HISI_SAS_DISK_RECOVER_CNT;
764 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
766 switch (device->dev_type) {
768 int_to_scsilun(0, &lun);
770 while (retry-- > 0) {
771 rc = sas_abort_task_set(device, lun.scsi_lun);
772 if (rc == TMF_RESP_FUNC_COMPLETE) {
773 hisi_sas_release_task(hisi_hba, device);
780 case SAS_SATA_PM_PORT:
781 case SAS_SATA_PENDING:
783 * If an expander is swapped when a SATA disk is attached then
784 * we should issue a hard reset to clear previous affiliation
785 * of STP target port, see SPL (chapter 6.19.4).
787 * However we don't need to issue a hard reset here for these
789 * a. When probing the device, libsas/libata already issues a
790 * hard reset in sas_probe_sata() -> ata_port_probe().
791 * Note that in hisi_sas_debug_I_T_nexus_reset() we take care
792 * to issue a hard reset by checking the dev status (== INIT).
793 * b. When resetting the controller, this is simply unnecessary.
795 while (retry-- > 0) {
796 rc = hisi_sas_softreset_ata_disk(device);
808 int hisi_sas_slave_alloc(struct scsi_device *sdev)
810 struct domain_device *ddev = sdev_to_domain_dev(sdev);
811 struct hisi_sas_device *sas_dev = ddev->lldd_dev;
814 rc = sas_slave_alloc(sdev);
818 rc = hisi_sas_init_device(ddev);
821 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
824 EXPORT_SYMBOL_GPL(hisi_sas_slave_alloc);
826 static int hisi_sas_dev_found(struct domain_device *device)
828 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
829 struct domain_device *parent_dev = device->parent;
830 struct hisi_sas_device *sas_dev;
831 struct device *dev = hisi_hba->dev;
834 if (hisi_hba->hw->alloc_dev)
835 sas_dev = hisi_hba->hw->alloc_dev(device);
837 sas_dev = hisi_sas_alloc_dev(device);
839 dev_err(dev, "fail alloc dev: max support %d devices\n",
840 HISI_SAS_MAX_DEVICES);
844 device->lldd_dev = sas_dev;
845 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
847 if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
850 phy_no = sas_find_attached_phy_id(&parent_dev->ex_dev, device);
852 dev_info(dev, "dev found: no attached "
853 "dev:%016llx at ex:%016llx\n",
854 SAS_ADDR(device->sas_addr),
855 SAS_ADDR(parent_dev->sas_addr));
861 dev_info(dev, "dev[%d:%x] found\n",
862 sas_dev->device_id, sas_dev->dev_type);
867 hisi_sas_dev_gone(device);
871 int hisi_sas_slave_configure(struct scsi_device *sdev)
873 struct domain_device *dev = sdev_to_domain_dev(sdev);
874 int ret = sas_slave_configure(sdev);
878 if (!dev_is_sata(dev))
879 sas_change_queue_depth(sdev, 64);
883 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
885 void hisi_sas_scan_start(struct Scsi_Host *shost)
887 struct hisi_hba *hisi_hba = shost_priv(shost);
889 hisi_hba->hw->phys_init(hisi_hba);
891 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
893 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
895 struct hisi_hba *hisi_hba = shost_priv(shost);
896 struct sas_ha_struct *sha = &hisi_hba->sha;
898 /* Wait for PHY up interrupt to occur */
905 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
907 static void hisi_sas_phyup_work_common(struct work_struct *work,
908 enum hisi_sas_phy_event event)
910 struct hisi_sas_phy *phy =
911 container_of(work, typeof(*phy), works[event]);
912 struct hisi_hba *hisi_hba = phy->hisi_hba;
913 struct asd_sas_phy *sas_phy = &phy->sas_phy;
914 int phy_no = sas_phy->id;
916 phy->wait_phyup_cnt = 0;
917 if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
918 hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
919 hisi_sas_bytes_dmaed(hisi_hba, phy_no, GFP_KERNEL);
922 static void hisi_sas_phyup_work(struct work_struct *work)
924 hisi_sas_phyup_work_common(work, HISI_PHYE_PHY_UP);
927 static void hisi_sas_linkreset_work(struct work_struct *work)
929 struct hisi_sas_phy *phy =
930 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
931 struct asd_sas_phy *sas_phy = &phy->sas_phy;
933 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
936 static void hisi_sas_phyup_pm_work(struct work_struct *work)
938 struct hisi_sas_phy *phy =
939 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP_PM]);
940 struct hisi_hba *hisi_hba = phy->hisi_hba;
941 struct device *dev = hisi_hba->dev;
943 hisi_sas_phyup_work_common(work, HISI_PHYE_PHY_UP_PM);
944 pm_runtime_put_sync(dev);
947 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
948 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
949 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
950 [HISI_PHYE_PHY_UP_PM] = hisi_sas_phyup_pm_work,
953 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
954 enum hisi_sas_phy_event event)
956 struct hisi_hba *hisi_hba = phy->hisi_hba;
958 if (WARN_ON(event >= HISI_PHYES_NUM))
961 return queue_work(hisi_hba->wq, &phy->works[event]);
963 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
965 static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
967 struct hisi_sas_phy *phy = from_timer(phy, t, timer);
968 struct hisi_hba *hisi_hba = phy->hisi_hba;
969 struct device *dev = hisi_hba->dev;
970 int phy_no = phy->sas_phy.id;
972 dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
973 hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
976 #define HISI_SAS_WAIT_PHYUP_RETRIES 10
978 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
980 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
981 struct device *dev = hisi_hba->dev;
984 dev_dbg(dev, "phy%d OOB ready\n", phy_no);
985 spin_lock_irqsave(&phy->lock, flags);
986 if (phy->phy_attached) {
987 spin_unlock_irqrestore(&phy->lock, flags);
991 if (!timer_pending(&phy->timer)) {
992 if (phy->wait_phyup_cnt < HISI_SAS_WAIT_PHYUP_RETRIES) {
993 phy->wait_phyup_cnt++;
994 phy->timer.expires = jiffies +
995 HISI_SAS_WAIT_PHYUP_TIMEOUT;
996 add_timer(&phy->timer);
997 spin_unlock_irqrestore(&phy->lock, flags);
1001 dev_warn(dev, "phy%d failed to come up %d times, giving up\n",
1002 phy_no, phy->wait_phyup_cnt);
1003 phy->wait_phyup_cnt = 0;
1005 spin_unlock_irqrestore(&phy->lock, flags);
1008 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
1010 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
1012 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1013 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1016 phy->hisi_hba = hisi_hba;
1018 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
1019 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
1020 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
1021 sas_phy->class = SAS;
1022 sas_phy->iproto = SAS_PROTOCOL_ALL;
1023 sas_phy->tproto = 0;
1024 sas_phy->type = PHY_TYPE_PHYSICAL;
1025 sas_phy->role = PHY_ROLE_INITIATOR;
1026 sas_phy->oob_mode = OOB_NOT_CONNECTED;
1027 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
1028 sas_phy->id = phy_no;
1029 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
1030 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
1031 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
1032 sas_phy->lldd_phy = phy;
1034 for (i = 0; i < HISI_PHYES_NUM; i++)
1035 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
1037 spin_lock_init(&phy->lock);
1039 timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
1042 /* Wrapper to ensure we track hisi_sas_phy.enable properly */
1043 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable)
1045 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1046 struct asd_sas_phy *aphy = &phy->sas_phy;
1047 struct sas_phy *sphy = aphy->phy;
1048 unsigned long flags;
1050 spin_lock_irqsave(&phy->lock, flags);
1053 /* We may have been enabled already; if so, don't touch */
1055 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
1056 hisi_hba->hw->phy_start(hisi_hba, phy_no);
1058 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
1059 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
1061 phy->enable = enable;
1062 spin_unlock_irqrestore(&phy->lock, flags);
1064 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable);
1066 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
1068 struct sas_ha_struct *sas_ha = sas_phy->ha;
1069 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1070 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
1071 struct asd_sas_port *sas_port = sas_phy->port;
1072 struct hisi_sas_port *port;
1073 unsigned long flags;
1078 port = to_hisi_sas_port(sas_port);
1079 spin_lock_irqsave(&hisi_hba->lock, flags);
1080 port->port_attached = 1;
1081 port->id = phy->port_id;
1083 sas_port->lldd_port = port;
1084 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1087 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
1088 struct hisi_sas_slot *slot, bool need_lock)
1091 unsigned long flags;
1092 struct task_status_struct *ts;
1094 ts = &task->task_status;
1096 ts->resp = SAS_TASK_COMPLETE;
1097 ts->stat = SAS_ABORTED_TASK;
1098 spin_lock_irqsave(&task->task_state_lock, flags);
1099 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
1100 if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
1101 task->task_state_flags |= SAS_TASK_STATE_DONE;
1102 spin_unlock_irqrestore(&task->task_state_lock, flags);
1105 hisi_sas_slot_task_free(hisi_hba, task, slot, need_lock);
1108 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
1109 struct domain_device *device)
1111 struct hisi_sas_slot *slot, *slot2;
1112 struct hisi_sas_device *sas_dev = device->lldd_dev;
1114 spin_lock(&sas_dev->lock);
1115 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
1116 hisi_sas_do_release_task(hisi_hba, slot->task, slot, false);
1118 spin_unlock(&sas_dev->lock);
1121 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
1123 struct hisi_sas_device *sas_dev;
1124 struct domain_device *device;
1127 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1128 sas_dev = &hisi_hba->devices[i];
1129 device = sas_dev->sas_device;
1131 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
1135 hisi_sas_release_task(hisi_hba, device);
1138 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
1140 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
1141 struct domain_device *device)
1143 if (hisi_hba->hw->dereg_device)
1144 hisi_hba->hw->dereg_device(hisi_hba, device);
1148 hisi_sas_internal_task_abort_dev(struct hisi_sas_device *sas_dev,
1149 bool rst_ha_timeout)
1151 struct hisi_sas_internal_abort_data data = { rst_ha_timeout };
1152 struct domain_device *device = sas_dev->sas_device;
1153 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1156 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
1157 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1158 const struct cpumask *mask = cq->irq_mask;
1160 if (mask && !cpumask_intersects(cpu_online_mask, mask))
1162 rc = sas_execute_internal_abort_dev(device, i, &data);
1170 static void hisi_sas_dev_gone(struct domain_device *device)
1172 struct hisi_sas_device *sas_dev = device->lldd_dev;
1173 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1174 struct device *dev = hisi_hba->dev;
1177 dev_info(dev, "dev[%d:%x] is gone\n",
1178 sas_dev->device_id, sas_dev->dev_type);
1180 down(&hisi_hba->sem);
1181 if (!test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags)) {
1182 hisi_sas_internal_task_abort_dev(sas_dev, true);
1184 hisi_sas_dereg_device(hisi_hba, device);
1186 ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
1187 device->lldd_dev = NULL;
1190 if (hisi_hba->hw->free_device)
1191 hisi_hba->hw->free_device(sas_dev);
1193 /* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */
1195 sas_dev->dev_type = SAS_PHY_UNUSED;
1196 sas_dev->sas_device = NULL;
1200 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
1201 struct sas_phy_linkrates *r)
1203 struct sas_phy_linkrates _r;
1205 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1206 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1207 enum sas_linkrate min, max;
1209 if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
1212 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1213 max = sas_phy->phy->maximum_linkrate;
1214 min = r->minimum_linkrate;
1215 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1216 max = r->maximum_linkrate;
1217 min = sas_phy->phy->minimum_linkrate;
1221 _r.maximum_linkrate = max;
1222 _r.minimum_linkrate = min;
1224 sas_phy->phy->maximum_linkrate = max;
1225 sas_phy->phy->minimum_linkrate = min;
1227 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1229 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
1230 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1235 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
1238 struct hisi_sas_phy *phy = container_of(sas_phy,
1239 struct hisi_sas_phy, sas_phy);
1240 struct sas_ha_struct *sas_ha = sas_phy->ha;
1241 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1242 struct device *dev = hisi_hba->dev;
1243 DECLARE_COMPLETION_ONSTACK(completion);
1244 int phy_no = sas_phy->id;
1245 u8 sts = phy->phy_attached;
1248 down(&hisi_hba->sem);
1249 phy->reset_completion = &completion;
1252 case PHY_FUNC_HARD_RESET:
1253 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
1256 case PHY_FUNC_LINK_RESET:
1257 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1259 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1262 case PHY_FUNC_DISABLE:
1263 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1266 case PHY_FUNC_SET_LINK_RATE:
1267 ret = hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1270 case PHY_FUNC_GET_EVENTS:
1271 if (hisi_hba->hw->get_events) {
1272 hisi_hba->hw->get_events(hisi_hba, phy_no);
1276 case PHY_FUNC_RELEASE_SPINUP_HOLD:
1282 if (sts && !wait_for_completion_timeout(&completion,
1283 HISI_SAS_WAIT_PHYUP_TIMEOUT)) {
1284 dev_warn(dev, "phy%d wait phyup timed out for func %d\n",
1291 phy->reset_completion = NULL;
1297 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1298 bool reset, int pmp, u8 *fis)
1300 struct ata_taskfile tf;
1302 ata_tf_init(dev, &tf);
1306 tf.ctl &= ~ATA_SRST;
1307 tf.command = ATA_CMD_DEV_RESET;
1308 ata_tf_to_fis(&tf, pmp, 0, fis);
1311 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1314 struct ata_port *ap = device->sata_dev.ap;
1315 struct ata_link *link;
1316 int rc = TMF_RESP_FUNC_FAILED;
1317 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1318 struct device *dev = hisi_hba->dev;
1320 ata_for_each_link(link, ap, EDGE) {
1321 int pmp = sata_srst_pmp(link);
1323 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1324 rc = sas_execute_ata_cmd(device, fis, -1);
1325 if (rc != TMF_RESP_FUNC_COMPLETE)
1329 if (rc == TMF_RESP_FUNC_COMPLETE) {
1330 ata_for_each_link(link, ap, EDGE) {
1331 int pmp = sata_srst_pmp(link);
1333 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1334 rc = sas_execute_ata_cmd(device, fis, -1);
1335 if (rc != TMF_RESP_FUNC_COMPLETE)
1336 dev_err(dev, "ata disk %016llx de-reset failed\n",
1337 SAS_ADDR(device->sas_addr));
1340 dev_err(dev, "ata disk %016llx reset failed\n",
1341 SAS_ADDR(device->sas_addr));
1344 if (rc == TMF_RESP_FUNC_COMPLETE)
1345 hisi_sas_release_task(hisi_hba, device);
1350 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1352 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1355 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1356 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1357 struct domain_device *device = sas_dev->sas_device;
1358 struct asd_sas_port *sas_port;
1359 struct hisi_sas_port *port;
1360 struct hisi_sas_phy *phy = NULL;
1361 struct asd_sas_phy *sas_phy;
1363 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1364 || !device || !device->port)
1367 sas_port = device->port;
1368 port = to_hisi_sas_port(sas_port);
1370 spin_lock(&sas_port->phy_list_lock);
1371 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1372 if (state & BIT(sas_phy->id)) {
1373 phy = sas_phy->lldd_phy;
1376 spin_unlock(&sas_port->phy_list_lock);
1379 port->id = phy->port_id;
1381 /* Update linkrate of directly attached device. */
1382 if (!device->parent)
1383 device->linkrate = phy->sas_phy.linkrate;
1385 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1386 } else if (!port->port_attached)
1391 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state)
1393 struct asd_sas_port *_sas_port = NULL;
1396 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1397 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1398 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1399 struct asd_sas_port *sas_port = sas_phy->port;
1400 bool do_port_check = _sas_port != sas_port;
1402 if (!sas_phy->phy->enabled)
1405 /* Report PHY state change to libsas */
1406 if (state & BIT(phy_no)) {
1407 if (do_port_check && sas_port && sas_port->port_dev) {
1408 struct domain_device *dev = sas_port->port_dev;
1410 _sas_port = sas_port;
1412 if (dev_is_expander(dev->dev_type))
1413 sas_notify_port_event(sas_phy,
1414 PORTE_BROADCAST_RCVD,
1418 hisi_sas_phy_down(hisi_hba, phy_no, 0, GFP_KERNEL);
1423 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1425 struct hisi_sas_device *sas_dev;
1426 struct domain_device *device;
1429 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1430 sas_dev = &hisi_hba->devices[i];
1431 device = sas_dev->sas_device;
1433 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1436 hisi_sas_init_device(device);
1440 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1441 struct asd_sas_port *sas_port,
1442 struct domain_device *device)
1444 struct ata_port *ap = device->sata_dev.ap;
1445 struct device *dev = hisi_hba->dev;
1446 int rc = TMF_RESP_FUNC_FAILED;
1447 struct ata_link *link;
1451 for (i = 0; i < hisi_hba->n_phy; i++) {
1452 if (!(sas_port->phy_mask & BIT(i)))
1455 ata_for_each_link(link, ap, EDGE) {
1456 int pmp = sata_srst_pmp(link);
1458 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1459 rc = sas_execute_ata_cmd(device, fis, i);
1460 if (rc != TMF_RESP_FUNC_COMPLETE) {
1461 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1469 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1471 struct device *dev = hisi_hba->dev;
1474 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1475 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1476 struct domain_device *device = sas_dev->sas_device;
1478 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1481 rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1483 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1486 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1487 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1488 struct asd_sas_port *sas_port = &port->sas_port;
1489 struct domain_device *port_dev = sas_port->port_dev;
1490 struct domain_device *device;
1492 if (!port_dev || !dev_is_expander(port_dev->dev_type))
1495 /* Try to find a SATA device */
1496 list_for_each_entry(device, &sas_port->dev_list,
1498 if (dev_is_sata(device)) {
1499 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1508 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1510 struct Scsi_Host *shost = hisi_hba->shost;
1512 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1514 scsi_block_requests(shost);
1515 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1517 del_timer_sync(&hisi_hba->timer);
1519 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1521 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1523 static void hisi_sas_async_init_wait_phyup(void *data, async_cookie_t cookie)
1525 struct hisi_sas_phy *phy = data;
1526 struct hisi_hba *hisi_hba = phy->hisi_hba;
1527 struct device *dev = hisi_hba->dev;
1528 DECLARE_COMPLETION_ONSTACK(completion);
1529 int phy_no = phy->sas_phy.id;
1531 phy->reset_completion = &completion;
1532 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1533 if (!wait_for_completion_timeout(&completion,
1534 HISI_SAS_WAIT_PHYUP_TIMEOUT))
1535 dev_warn(dev, "phy%d wait phyup timed out\n", phy_no);
1537 phy->reset_completion = NULL;
1540 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1542 struct Scsi_Host *shost = hisi_hba->shost;
1543 ASYNC_DOMAIN_EXCLUSIVE(async);
1546 /* Init and wait for PHYs to come up and all libsas event finished. */
1547 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1548 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1550 if (!(hisi_hba->phy_state & BIT(phy_no)))
1553 async_schedule_domain(hisi_sas_async_init_wait_phyup,
1557 async_synchronize_full_domain(&async);
1558 hisi_sas_refresh_port_id(hisi_hba);
1559 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1561 if (hisi_hba->reject_stp_links_msk)
1562 hisi_sas_terminate_stp_reject(hisi_hba);
1563 hisi_sas_reset_init_all_devices(hisi_hba);
1564 scsi_unblock_requests(shost);
1565 clear_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags);
1568 hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state);
1570 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1572 static int hisi_sas_controller_prereset(struct hisi_hba *hisi_hba)
1574 if (!hisi_hba->hw->soft_reset)
1577 down(&hisi_hba->sem);
1578 if (test_and_set_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags)) {
1583 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
1584 hisi_hba->hw->debugfs_snapshot_regs(hisi_hba);
1589 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1591 struct device *dev = hisi_hba->dev;
1592 struct Scsi_Host *shost = hisi_hba->shost;
1595 dev_info(dev, "controller resetting...\n");
1596 hisi_sas_controller_reset_prepare(hisi_hba);
1598 rc = hisi_hba->hw->soft_reset(hisi_hba);
1600 dev_warn(dev, "controller reset failed (%d)\n", rc);
1601 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1603 scsi_unblock_requests(shost);
1604 clear_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags);
1607 clear_bit(HISI_SAS_HW_FAULT_BIT, &hisi_hba->flags);
1609 hisi_sas_controller_reset_done(hisi_hba);
1610 dev_info(dev, "controller reset complete\n");
1615 static int hisi_sas_abort_task(struct sas_task *task)
1617 struct hisi_sas_internal_abort_data internal_abort_data = { false };
1618 struct domain_device *device = task->dev;
1619 struct hisi_sas_device *sas_dev = device->lldd_dev;
1620 struct hisi_sas_slot *slot = task->lldd_task;
1621 struct hisi_hba *hisi_hba;
1623 int rc = TMF_RESP_FUNC_FAILED;
1624 unsigned long flags;
1627 return TMF_RESP_FUNC_FAILED;
1629 hisi_hba = dev_to_hisi_hba(task->dev);
1630 dev = hisi_hba->dev;
1632 spin_lock_irqsave(&task->task_state_lock, flags);
1633 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1634 struct hisi_sas_cq *cq;
1638 * sync irq or poll queue to avoid free'ing task
1639 * before using task in IO completion
1641 cq = &hisi_hba->cq[slot->dlvry_queue];
1642 hisi_sas_sync_cq(cq);
1644 spin_unlock_irqrestore(&task->task_state_lock, flags);
1645 rc = TMF_RESP_FUNC_COMPLETE;
1648 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1649 spin_unlock_irqrestore(&task->task_state_lock, flags);
1651 if (slot && task->task_proto & SAS_PROTOCOL_SSP) {
1652 u16 tag = slot->idx;
1655 rc = sas_abort_task(task, tag);
1656 rc2 = sas_execute_internal_abort_single(device, tag,
1657 slot->dlvry_queue, &internal_abort_data);
1659 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1660 return TMF_RESP_FUNC_FAILED;
1664 * If the TMF finds that the IO is not in the device and also
1665 * the internal abort does not succeed, then it is safe to
1667 * Note: if the internal abort succeeds then the slot
1668 * will have already been completed
1670 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1671 if (task->lldd_task)
1672 hisi_sas_do_release_task(hisi_hba, task, slot, true);
1674 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1675 task->task_proto & SAS_PROTOCOL_STP) {
1676 if (task->dev->dev_type == SAS_SATA_DEV) {
1677 struct ata_queued_cmd *qc = task->uldd_task;
1679 rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1681 dev_err(dev, "abort task: internal abort failed\n");
1684 hisi_sas_dereg_device(hisi_hba, device);
1687 * If an ATA internal command times out in ATA EH, it
1688 * need to execute soft reset, so check the scsicmd
1690 if ((sas_dev->dev_status == HISI_SAS_DEV_NCQ_ERR) &&
1691 qc && qc->scsicmd) {
1692 hisi_sas_do_release_task(hisi_hba, task, slot, true);
1693 rc = TMF_RESP_FUNC_COMPLETE;
1695 rc = hisi_sas_softreset_ata_disk(device);
1698 } else if (slot && task->task_proto & SAS_PROTOCOL_SMP) {
1700 u32 tag = slot->idx;
1701 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1703 rc = sas_execute_internal_abort_single(device,
1704 tag, slot->dlvry_queue,
1705 &internal_abort_data);
1706 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1709 * sync irq or poll queue to avoid free'ing task
1710 * before using task in IO completion
1712 hisi_sas_sync_cq(cq);
1718 if (rc != TMF_RESP_FUNC_COMPLETE)
1719 dev_notice(dev, "abort task: rc=%d\n", rc);
1723 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1725 struct hisi_sas_device *sas_dev = device->lldd_dev;
1726 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1727 struct device *dev = hisi_hba->dev;
1730 rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1732 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1733 return TMF_RESP_FUNC_FAILED;
1735 hisi_sas_dereg_device(hisi_hba, device);
1737 rc = sas_abort_task_set(device, lun);
1738 if (rc == TMF_RESP_FUNC_COMPLETE)
1739 hisi_sas_release_task(hisi_hba, device);
1744 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1746 struct sas_phy *local_phy = sas_get_local_phy(device);
1747 struct hisi_sas_device *sas_dev = device->lldd_dev;
1748 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1749 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1752 if (!local_phy->enabled) {
1753 sas_put_local_phy(local_phy);
1757 if (scsi_is_sas_phy_local(local_phy)) {
1758 struct asd_sas_phy *sas_phy =
1759 sas_ha->sas_phy[local_phy->number];
1760 struct hisi_sas_phy *phy =
1761 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1762 unsigned long flags;
1764 spin_lock_irqsave(&phy->lock, flags);
1766 spin_unlock_irqrestore(&phy->lock, flags);
1769 reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
1770 !dev_is_sata(device)) ? true : false;
1772 rc = sas_phy_reset(local_phy, reset_type);
1773 sas_put_local_phy(local_phy);
1775 if (scsi_is_sas_phy_local(local_phy)) {
1776 struct asd_sas_phy *sas_phy =
1777 sas_ha->sas_phy[local_phy->number];
1778 struct hisi_sas_phy *phy =
1779 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1780 unsigned long flags;
1782 spin_lock_irqsave(&phy->lock, flags);
1784 spin_unlock_irqrestore(&phy->lock, flags);
1786 /* report PHY down if timed out */
1787 if (rc == -ETIMEDOUT)
1788 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0, GFP_KERNEL);
1796 if (dev_is_sata(device)) {
1797 struct ata_link *link = &device->sata_dev.ap->link;
1799 rc = ata_wait_after_reset(link, HISI_SAS_WAIT_PHYUP_TIMEOUT,
1800 smp_ata_check_ready_type);
1808 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1810 struct hisi_sas_device *sas_dev = device->lldd_dev;
1811 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1812 struct device *dev = hisi_hba->dev;
1815 if (sas_dev->dev_status == HISI_SAS_DEV_NCQ_ERR)
1816 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1818 rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1820 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1821 return TMF_RESP_FUNC_FAILED;
1823 hisi_sas_dereg_device(hisi_hba, device);
1825 rc = hisi_sas_debug_I_T_nexus_reset(device);
1826 if (rc == TMF_RESP_FUNC_COMPLETE && dev_is_sata(device)) {
1827 struct sas_phy *local_phy;
1829 rc = hisi_sas_softreset_ata_disk(device);
1834 case TMF_RESP_FUNC_FAILED:
1837 local_phy = sas_get_local_phy(device);
1838 rc = sas_phy_enable(local_phy, 0);
1840 local_phy->enabled = 0;
1841 dev_err(dev, "Disabled local phy of ATA disk %016llx due to softreset fail (%d)\n",
1842 SAS_ADDR(device->sas_addr), rc);
1845 sas_put_local_phy(local_phy);
1852 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1853 hisi_sas_release_task(hisi_hba, device);
1858 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1860 struct hisi_sas_device *sas_dev = device->lldd_dev;
1861 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1862 struct device *dev = hisi_hba->dev;
1863 int rc = TMF_RESP_FUNC_FAILED;
1865 /* Clear internal IO and then lu reset */
1866 rc = hisi_sas_internal_task_abort_dev(sas_dev, false);
1868 dev_err(dev, "lu_reset: internal abort failed\n");
1871 hisi_sas_dereg_device(hisi_hba, device);
1873 if (dev_is_sata(device)) {
1874 struct sas_phy *phy;
1876 phy = sas_get_local_phy(device);
1878 rc = sas_phy_reset(phy, true);
1881 hisi_sas_release_task(hisi_hba, device);
1882 sas_put_local_phy(phy);
1884 rc = sas_lu_reset(device, lun);
1885 if (rc == TMF_RESP_FUNC_COMPLETE)
1886 hisi_sas_release_task(hisi_hba, device);
1889 if (rc != TMF_RESP_FUNC_COMPLETE)
1890 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1891 sas_dev->device_id, rc);
1895 static void hisi_sas_async_I_T_nexus_reset(void *data, async_cookie_t cookie)
1897 struct domain_device *device = data;
1898 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1901 rc = hisi_sas_debug_I_T_nexus_reset(device);
1902 if (rc != TMF_RESP_FUNC_COMPLETE)
1903 dev_info(hisi_hba->dev, "I_T_nexus reset fail for dev:%016llx rc=%d\n",
1904 SAS_ADDR(device->sas_addr), rc);
1907 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1909 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1910 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1911 ASYNC_DOMAIN_EXCLUSIVE(async);
1914 queue_work(hisi_hba->wq, &r.work);
1915 wait_for_completion(r.completion);
1917 return TMF_RESP_FUNC_FAILED;
1919 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1920 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1921 struct domain_device *device = sas_dev->sas_device;
1923 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1924 dev_is_expander(device->dev_type))
1927 async_schedule_domain(hisi_sas_async_I_T_nexus_reset,
1931 async_synchronize_full_domain(&async);
1932 hisi_sas_release_tasks(hisi_hba);
1934 return TMF_RESP_FUNC_COMPLETE;
1937 static int hisi_sas_query_task(struct sas_task *task)
1939 int rc = TMF_RESP_FUNC_FAILED;
1941 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1942 struct hisi_sas_slot *slot = task->lldd_task;
1943 u32 tag = slot->idx;
1945 rc = sas_query_task(task, tag);
1947 /* The task is still in Lun, release it then */
1948 case TMF_RESP_FUNC_SUCC:
1949 /* The task is not in Lun or failed, reset the phy */
1950 case TMF_RESP_FUNC_FAILED:
1951 case TMF_RESP_FUNC_COMPLETE:
1954 rc = TMF_RESP_FUNC_FAILED;
1961 static bool hisi_sas_internal_abort_timeout(struct sas_task *task,
1964 struct domain_device *device = task->dev;
1965 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1966 struct hisi_sas_internal_abort_data *timeout = data;
1968 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
1969 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
1971 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1972 pr_err("Internal abort: timeout %016llx\n",
1973 SAS_ADDR(device->sas_addr));
1975 struct hisi_sas_slot *slot = task->lldd_task;
1977 set_bit(HISI_SAS_HW_FAULT_BIT, &hisi_hba->flags);
1980 struct hisi_sas_cq *cq =
1981 &hisi_hba->cq[slot->dlvry_queue];
1983 * sync irq or poll queue to avoid free'ing task
1984 * before using task in IO completion
1986 hisi_sas_sync_cq(cq);
1990 if (timeout->rst_ha_timeout) {
1991 pr_err("Internal abort: timeout and not done %016llx. Queuing reset.\n",
1992 SAS_ADDR(device->sas_addr));
1993 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
1995 pr_err("Internal abort: timeout and not done %016llx.\n",
1996 SAS_ADDR(device->sas_addr));
2005 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
2007 hisi_sas_port_notify_formed(sas_phy);
2010 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
2011 u8 reg_index, u8 reg_count, u8 *write_data)
2013 struct hisi_hba *hisi_hba = sha->lldd_ha;
2015 if (!hisi_hba->hw->write_gpio)
2018 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
2019 reg_index, reg_count, write_data);
2022 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
2024 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2025 struct sas_phy *sphy = sas_phy->phy;
2026 unsigned long flags;
2028 phy->phy_attached = 0;
2032 spin_lock_irqsave(&phy->lock, flags);
2034 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
2036 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
2037 spin_unlock_irqrestore(&phy->lock, flags);
2040 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy,
2043 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2044 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2045 struct device *dev = hisi_hba->dev;
2048 /* Phy down but ready */
2049 hisi_sas_bytes_dmaed(hisi_hba, phy_no, gfp_flags);
2050 hisi_sas_port_notify_formed(sas_phy);
2052 struct hisi_sas_port *port = phy->port;
2054 if (test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags) ||
2056 dev_info(dev, "ignore flutter phy%d down\n", phy_no);
2059 /* Phy down and not ready */
2060 sas_notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL, gfp_flags);
2061 sas_phy_disconnected(sas_phy);
2064 if (phy->phy_type & PORT_TYPE_SAS) {
2065 int port_id = port->id;
2067 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2069 port->port_attached = 0;
2070 } else if (phy->phy_type & PORT_TYPE_SATA)
2071 port->port_attached = 0;
2073 hisi_sas_phy_disconnected(phy);
2076 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2078 void hisi_sas_phy_bcast(struct hisi_sas_phy *phy)
2080 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2081 struct hisi_hba *hisi_hba = phy->hisi_hba;
2083 if (test_bit(HISI_SAS_RESETTING_BIT, &hisi_hba->flags))
2086 sas_notify_port_event(sas_phy, PORTE_BROADCAST_RCVD, GFP_ATOMIC);
2088 EXPORT_SYMBOL_GPL(hisi_sas_phy_bcast);
2090 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type)
2092 struct hisi_hba *hisi_hba = shost_priv(shost);
2094 if (reset_type != SCSI_ADAPTER_RESET)
2097 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2101 EXPORT_SYMBOL_GPL(hisi_sas_host_reset);
2103 struct scsi_transport_template *hisi_sas_stt;
2104 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2106 static struct sas_domain_function_template hisi_sas_transport_ops = {
2107 .lldd_dev_found = hisi_sas_dev_found,
2108 .lldd_dev_gone = hisi_sas_dev_gone,
2109 .lldd_execute_task = hisi_sas_queue_command,
2110 .lldd_control_phy = hisi_sas_control_phy,
2111 .lldd_abort_task = hisi_sas_abort_task,
2112 .lldd_abort_task_set = hisi_sas_abort_task_set,
2113 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
2114 .lldd_lu_reset = hisi_sas_lu_reset,
2115 .lldd_query_task = hisi_sas_query_task,
2116 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
2117 .lldd_port_formed = hisi_sas_port_formed,
2118 .lldd_write_gpio = hisi_sas_write_gpio,
2119 .lldd_tmf_aborted = hisi_sas_tmf_aborted,
2120 .lldd_abort_timeout = hisi_sas_internal_abort_timeout,
2123 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2125 int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS;
2126 struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
2128 for (i = 0; i < hisi_hba->queue_count; i++) {
2129 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2130 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2131 struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
2133 s = sizeof(struct hisi_sas_cmd_hdr);
2134 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2135 memset(&cmd_hdr[j], 0, s);
2139 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2140 memset(hisi_hba->complete_hdr[i], 0, s);
2144 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2145 memset(hisi_hba->initial_fis, 0, s);
2147 s = max_command_entries * sizeof(struct hisi_sas_iost);
2148 memset(hisi_hba->iost, 0, s);
2150 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2151 memset(hisi_hba->breakpoint, 0, s);
2153 s = sizeof(struct hisi_sas_sata_breakpoint);
2154 for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
2155 memset(&sata_breakpoint[j], 0, s);
2157 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2159 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2161 struct device *dev = hisi_hba->dev;
2162 int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS;
2163 int max_command_entries_ru, sz_slot_buf_ru;
2164 int blk_cnt, slots_per_blk;
2166 sema_init(&hisi_hba->sem, 1);
2167 spin_lock_init(&hisi_hba->lock);
2168 for (i = 0; i < hisi_hba->n_phy; i++) {
2169 hisi_sas_phy_init(hisi_hba, i);
2170 hisi_hba->port[i].port_attached = 0;
2171 hisi_hba->port[i].id = -1;
2174 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2175 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2176 hisi_hba->devices[i].device_id = i;
2177 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
2180 for (i = 0; i < hisi_hba->queue_count; i++) {
2181 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2182 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2184 /* Completion queue structure */
2186 cq->hisi_hba = hisi_hba;
2187 spin_lock_init(&cq->poll_lock);
2189 /* Delivery queue structure */
2190 spin_lock_init(&dq->lock);
2191 INIT_LIST_HEAD(&dq->list);
2193 dq->hisi_hba = hisi_hba;
2195 /* Delivery queue */
2196 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2197 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2198 &hisi_hba->cmd_hdr_dma[i],
2200 if (!hisi_hba->cmd_hdr[i])
2203 /* Completion queue */
2204 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2205 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2206 &hisi_hba->complete_hdr_dma[i],
2208 if (!hisi_hba->complete_hdr[i])
2212 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2213 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2215 if (!hisi_hba->itct)
2218 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2219 sizeof(struct hisi_sas_slot),
2221 if (!hisi_hba->slot_info)
2224 /* roundup to avoid overly large block size */
2225 max_command_entries_ru = roundup(max_command_entries, 64);
2226 if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
2227 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
2229 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2230 sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2231 s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
2232 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2233 slots_per_blk = s / sz_slot_buf_ru;
2235 for (i = 0; i < blk_cnt; i++) {
2236 int slot_index = i * slots_per_blk;
2240 buf = dmam_alloc_coherent(dev, s, &buf_dma,
2245 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2246 struct hisi_sas_slot *slot;
2248 slot = &hisi_hba->slot_info[slot_index];
2250 slot->buf_dma = buf_dma;
2251 slot->idx = slot_index;
2253 buf += sz_slot_buf_ru;
2254 buf_dma += sz_slot_buf_ru;
2258 s = max_command_entries * sizeof(struct hisi_sas_iost);
2259 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2261 if (!hisi_hba->iost)
2264 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2265 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2266 &hisi_hba->breakpoint_dma,
2268 if (!hisi_hba->breakpoint)
2271 s = hisi_hba->slot_index_count = max_command_entries;
2272 hisi_hba->slot_index_tags = devm_bitmap_zalloc(dev, s, GFP_KERNEL);
2273 if (!hisi_hba->slot_index_tags)
2276 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2277 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2278 &hisi_hba->initial_fis_dma,
2280 if (!hisi_hba->initial_fis)
2283 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2284 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2285 &hisi_hba->sata_breakpoint_dma,
2287 if (!hisi_hba->sata_breakpoint)
2290 hisi_hba->last_slot_index = 0;
2292 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2293 if (!hisi_hba->wq) {
2294 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2302 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2304 void hisi_sas_free(struct hisi_hba *hisi_hba)
2308 for (i = 0; i < hisi_hba->n_phy; i++) {
2309 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
2311 del_timer_sync(&phy->timer);
2315 destroy_workqueue(hisi_hba->wq);
2317 EXPORT_SYMBOL_GPL(hisi_sas_free);
2319 void hisi_sas_rst_work_handler(struct work_struct *work)
2321 struct hisi_hba *hisi_hba =
2322 container_of(work, struct hisi_hba, rst_work);
2324 if (hisi_sas_controller_prereset(hisi_hba))
2327 hisi_sas_controller_reset(hisi_hba);
2329 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2331 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2333 struct hisi_sas_rst *rst =
2334 container_of(work, struct hisi_sas_rst, work);
2336 if (hisi_sas_controller_prereset(rst->hisi_hba))
2339 if (!hisi_sas_controller_reset(rst->hisi_hba))
2342 complete(rst->completion);
2344 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2346 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2348 struct device *dev = hisi_hba->dev;
2349 struct platform_device *pdev = hisi_hba->platform_dev;
2350 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2353 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2355 dev_err(dev, "could not get property sas-addr\n");
2361 * These properties are only required for platform device-based
2362 * controller with DT firmware.
2364 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2365 "hisilicon,sas-syscon");
2366 if (IS_ERR(hisi_hba->ctrl)) {
2367 dev_err(dev, "could not get syscon\n");
2371 if (device_property_read_u32(dev, "ctrl-reset-reg",
2372 &hisi_hba->ctrl_reset_reg)) {
2373 dev_err(dev, "could not get property ctrl-reset-reg\n");
2377 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2378 &hisi_hba->ctrl_reset_sts_reg)) {
2379 dev_err(dev, "could not get property ctrl-reset-sts-reg\n");
2383 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2384 &hisi_hba->ctrl_clock_ena_reg)) {
2385 dev_err(dev, "could not get property ctrl-clock-ena-reg\n");
2390 refclk = devm_clk_get(dev, NULL);
2392 dev_dbg(dev, "no ref clk property\n");
2394 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2396 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2397 dev_err(dev, "could not get property phy-count\n");
2401 if (device_property_read_u32(dev, "queue-count",
2402 &hisi_hba->queue_count)) {
2403 dev_err(dev, "could not get property queue-count\n");
2409 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2411 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2412 const struct hisi_sas_hw *hw)
2414 struct resource *res;
2415 struct Scsi_Host *shost;
2416 struct hisi_hba *hisi_hba;
2417 struct device *dev = &pdev->dev;
2420 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2422 dev_err(dev, "scsi host alloc failed\n");
2425 hisi_hba = shost_priv(shost);
2427 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2429 hisi_hba->dev = dev;
2430 hisi_hba->platform_dev = pdev;
2431 hisi_hba->shost = shost;
2432 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2434 timer_setup(&hisi_hba->timer, NULL, 0);
2436 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2439 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2441 dev_err(dev, "No usable DMA addressing method\n");
2445 hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0);
2446 if (IS_ERR(hisi_hba->regs))
2449 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2451 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2452 if (IS_ERR(hisi_hba->sgpio_regs))
2456 if (hisi_sas_alloc(hisi_hba)) {
2457 hisi_sas_free(hisi_hba);
2463 scsi_host_put(shost);
2464 dev_err(dev, "shost alloc failed\n");
2468 static int hisi_sas_interrupt_preinit(struct hisi_hba *hisi_hba)
2470 if (hisi_hba->hw->interrupt_preinit)
2471 return hisi_hba->hw->interrupt_preinit(hisi_hba);
2475 int hisi_sas_probe(struct platform_device *pdev,
2476 const struct hisi_sas_hw *hw)
2478 struct Scsi_Host *shost;
2479 struct hisi_hba *hisi_hba;
2480 struct device *dev = &pdev->dev;
2481 struct asd_sas_phy **arr_phy;
2482 struct asd_sas_port **arr_port;
2483 struct sas_ha_struct *sha;
2484 int rc, phy_nr, port_nr, i;
2486 shost = hisi_sas_shost_alloc(pdev, hw);
2490 sha = SHOST_TO_SAS_HA(shost);
2491 hisi_hba = shost_priv(shost);
2492 platform_set_drvdata(pdev, sha);
2494 phy_nr = port_nr = hisi_hba->n_phy;
2496 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2497 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2498 if (!arr_phy || !arr_port) {
2503 sha->sas_phy = arr_phy;
2504 sha->sas_port = arr_port;
2505 sha->lldd_ha = hisi_hba;
2507 shost->transportt = hisi_sas_stt;
2508 shost->max_id = HISI_SAS_MAX_DEVICES;
2509 shost->max_lun = ~0;
2510 shost->max_channel = 1;
2511 shost->max_cmd_len = 16;
2512 if (hisi_hba->hw->slot_index_alloc) {
2513 shost->can_queue = HISI_SAS_MAX_COMMANDS;
2514 shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;
2516 shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
2517 shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
2520 sha->sas_ha_name = DRV_NAME;
2521 sha->dev = hisi_hba->dev;
2522 sha->lldd_module = THIS_MODULE;
2523 sha->sas_addr = &hisi_hba->sas_addr[0];
2524 sha->num_phys = hisi_hba->n_phy;
2525 sha->core.shost = hisi_hba->shost;
2527 for (i = 0; i < hisi_hba->n_phy; i++) {
2528 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2529 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2532 rc = hisi_sas_interrupt_preinit(hisi_hba);
2536 rc = scsi_add_host(shost, &pdev->dev);
2540 rc = sas_register_ha(sha);
2542 goto err_out_register_ha;
2544 rc = hisi_hba->hw->hw_init(hisi_hba);
2546 goto err_out_hw_init;
2548 scsi_scan_host(shost);
2553 sas_unregister_ha(sha);
2554 err_out_register_ha:
2555 scsi_remove_host(shost);
2557 hisi_sas_free(hisi_hba);
2558 scsi_host_put(shost);
2561 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2563 void hisi_sas_remove(struct platform_device *pdev)
2565 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2566 struct hisi_hba *hisi_hba = sha->lldd_ha;
2567 struct Scsi_Host *shost = sha->core.shost;
2569 del_timer_sync(&hisi_hba->timer);
2571 sas_unregister_ha(sha);
2572 sas_remove_host(sha->core.shost);
2574 hisi_sas_free(hisi_hba);
2575 scsi_host_put(shost);
2577 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2579 #if IS_ENABLED(CONFIG_SCSI_HISI_SAS_DEBUGFS_DEFAULT_ENABLE)
2580 #define DEBUGFS_ENABLE_DEFAULT "enabled"
2581 bool hisi_sas_debugfs_enable = true;
2582 u32 hisi_sas_debugfs_dump_count = 50;
2584 #define DEBUGFS_ENABLE_DEFAULT "disabled"
2585 bool hisi_sas_debugfs_enable;
2586 u32 hisi_sas_debugfs_dump_count = 1;
2589 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
2590 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
2591 MODULE_PARM_DESC(hisi_sas_debugfs_enable,
2592 "Enable driver debugfs (default "DEBUGFS_ENABLE_DEFAULT")");
2594 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count);
2595 module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444);
2596 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow");
2598 struct dentry *hisi_sas_debugfs_dir;
2599 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dir);
2601 static __init int hisi_sas_init(void)
2603 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2607 if (hisi_sas_debugfs_enable) {
2608 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
2609 if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) {
2610 pr_info("hisi_sas: Limiting debugfs dump count\n");
2611 hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP;
2618 static __exit void hisi_sas_exit(void)
2620 sas_release_transport(hisi_sas_stt);
2622 debugfs_remove(hisi_sas_debugfs_dir);
2625 module_init(hisi_sas_init);
2626 module_exit(hisi_sas_exit);
2628 MODULE_LICENSE("GPL");
2629 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2630 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2631 MODULE_ALIAS("platform:" DRV_NAME);