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_debug_issue_ssp_tmf(struct domain_device *device,
14 u8 *lun, struct hisi_sas_tmf_task *tmf);
16 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
17 struct domain_device *device,
18 int abort_flag, int tag);
19 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
20 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
22 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
23 struct domain_device *device);
24 static void hisi_sas_dev_gone(struct domain_device *device);
26 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
28 switch (fis->command) {
29 case ATA_CMD_FPDMA_WRITE:
30 case ATA_CMD_FPDMA_READ:
31 case ATA_CMD_FPDMA_RECV:
32 case ATA_CMD_FPDMA_SEND:
33 case ATA_CMD_NCQ_NON_DATA:
34 return HISI_SAS_SATA_PROTOCOL_FPDMA;
36 case ATA_CMD_DOWNLOAD_MICRO:
38 case ATA_CMD_PMP_READ:
39 case ATA_CMD_READ_LOG_EXT:
40 case ATA_CMD_PIO_READ:
41 case ATA_CMD_PIO_READ_EXT:
42 case ATA_CMD_PMP_WRITE:
43 case ATA_CMD_WRITE_LOG_EXT:
44 case ATA_CMD_PIO_WRITE:
45 case ATA_CMD_PIO_WRITE_EXT:
46 return HISI_SAS_SATA_PROTOCOL_PIO;
49 case ATA_CMD_DOWNLOAD_MICRO_DMA:
50 case ATA_CMD_PMP_READ_DMA:
51 case ATA_CMD_PMP_WRITE_DMA:
53 case ATA_CMD_READ_EXT:
54 case ATA_CMD_READ_LOG_DMA_EXT:
55 case ATA_CMD_READ_STREAM_DMA_EXT:
56 case ATA_CMD_TRUSTED_RCV_DMA:
57 case ATA_CMD_TRUSTED_SND_DMA:
59 case ATA_CMD_WRITE_EXT:
60 case ATA_CMD_WRITE_FUA_EXT:
61 case ATA_CMD_WRITE_QUEUED:
62 case ATA_CMD_WRITE_LOG_DMA_EXT:
63 case ATA_CMD_WRITE_STREAM_DMA_EXT:
64 case ATA_CMD_ZAC_MGMT_IN:
65 return HISI_SAS_SATA_PROTOCOL_DMA;
67 case ATA_CMD_CHK_POWER:
68 case ATA_CMD_DEV_RESET:
71 case ATA_CMD_FLUSH_EXT:
73 case ATA_CMD_VERIFY_EXT:
74 case ATA_CMD_SET_FEATURES:
76 case ATA_CMD_STANDBYNOW1:
77 case ATA_CMD_ZAC_MGMT_OUT:
78 return HISI_SAS_SATA_PROTOCOL_NONDATA;
81 switch (fis->features) {
82 case ATA_SET_MAX_PASSWD:
83 case ATA_SET_MAX_LOCK:
84 return HISI_SAS_SATA_PROTOCOL_PIO;
86 case ATA_SET_MAX_PASSWD_DMA:
87 case ATA_SET_MAX_UNLOCK_DMA:
88 return HISI_SAS_SATA_PROTOCOL_DMA;
91 return HISI_SAS_SATA_PROTOCOL_NONDATA;
96 if (direction == DMA_NONE)
97 return HISI_SAS_SATA_PROTOCOL_NONDATA;
98 return HISI_SAS_SATA_PROTOCOL_PIO;
102 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
104 void hisi_sas_sata_done(struct sas_task *task,
105 struct hisi_sas_slot *slot)
107 struct task_status_struct *ts = &task->task_status;
108 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
109 struct hisi_sas_status_buffer *status_buf =
110 hisi_sas_status_buf_addr_mem(slot);
111 u8 *iu = &status_buf->iu[0];
112 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
114 resp->frame_len = sizeof(struct dev_to_host_fis);
115 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
117 ts->buf_valid_size = sizeof(*resp);
119 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
122 * This function assumes linkrate mask fits in 8 bits, which it
123 * does for all HW versions supported.
125 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
130 max -= SAS_LINK_RATE_1_5_GBPS;
131 for (i = 0; i <= max; i++)
132 rate |= 1 << (i * 2);
135 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
137 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
139 return device->port->ha->lldd_ha;
142 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
144 return container_of(sas_port, struct hisi_sas_port, sas_port);
146 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
148 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
152 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
153 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
155 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
157 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
159 void *bitmap = hisi_hba->slot_index_tags;
161 clear_bit(slot_idx, bitmap);
164 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
168 if (hisi_hba->hw->slot_index_alloc ||
169 slot_idx >= HISI_SAS_UNRESERVED_IPTT) {
170 spin_lock_irqsave(&hisi_hba->lock, flags);
171 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
172 spin_unlock_irqrestore(&hisi_hba->lock, flags);
176 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
178 void *bitmap = hisi_hba->slot_index_tags;
180 set_bit(slot_idx, bitmap);
183 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba,
184 struct scsi_cmnd *scsi_cmnd)
187 void *bitmap = hisi_hba->slot_index_tags;
191 return scsi_cmnd->request->tag;
193 spin_lock_irqsave(&hisi_hba->lock, flags);
194 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
195 hisi_hba->last_slot_index + 1);
196 if (index >= hisi_hba->slot_index_count) {
197 index = find_next_zero_bit(bitmap,
198 hisi_hba->slot_index_count,
199 HISI_SAS_UNRESERVED_IPTT);
200 if (index >= hisi_hba->slot_index_count) {
201 spin_unlock_irqrestore(&hisi_hba->lock, flags);
202 return -SAS_QUEUE_FULL;
205 hisi_sas_slot_index_set(hisi_hba, index);
206 hisi_hba->last_slot_index = index;
207 spin_unlock_irqrestore(&hisi_hba->lock, flags);
212 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
216 for (i = 0; i < hisi_hba->slot_index_count; ++i)
217 hisi_sas_slot_index_clear(hisi_hba, i);
220 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
221 struct hisi_sas_slot *slot)
224 int device_id = slot->device_id;
225 struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id];
228 struct device *dev = hisi_hba->dev;
230 if (!task->lldd_task)
233 task->lldd_task = NULL;
235 if (!sas_protocol_ata(task->task_proto)) {
236 struct sas_ssp_task *ssp_task = &task->ssp_task;
237 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
240 dma_unmap_sg(dev, task->scatter,
243 if (slot->n_elem_dif)
244 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
245 scsi_prot_sg_count(scsi_cmnd),
250 spin_lock_irqsave(&sas_dev->lock, flags);
251 list_del_init(&slot->entry);
252 spin_unlock_irqrestore(&sas_dev->lock, flags);
254 memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
256 hisi_sas_slot_index_free(hisi_hba, slot->idx);
258 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
260 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
261 struct hisi_sas_slot *slot)
263 hisi_hba->hw->prep_smp(hisi_hba, slot);
266 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
267 struct hisi_sas_slot *slot)
269 hisi_hba->hw->prep_ssp(hisi_hba, slot);
272 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
273 struct hisi_sas_slot *slot)
275 hisi_hba->hw->prep_stp(hisi_hba, slot);
278 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
279 struct hisi_sas_slot *slot,
280 int device_id, int abort_flag, int tag_to_abort)
282 hisi_hba->hw->prep_abort(hisi_hba, slot,
283 device_id, abort_flag, tag_to_abort);
286 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
287 struct sas_task *task, int n_elem,
290 struct device *dev = hisi_hba->dev;
292 if (!sas_protocol_ata(task->task_proto)) {
293 if (task->num_scatter) {
295 dma_unmap_sg(dev, task->scatter,
298 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
300 dma_unmap_sg(dev, &task->smp_task.smp_req,
306 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
307 struct sas_task *task, int *n_elem,
310 struct device *dev = hisi_hba->dev;
313 if (sas_protocol_ata(task->task_proto)) {
314 *n_elem = task->num_scatter;
316 unsigned int req_len;
318 if (task->num_scatter) {
319 *n_elem = dma_map_sg(dev, task->scatter,
320 task->num_scatter, task->data_dir);
325 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
326 *n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
332 req_len = sg_dma_len(&task->smp_task.smp_req);
335 goto err_out_dma_unmap;
340 if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
341 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
344 goto err_out_dma_unmap;
349 /* It would be better to call dma_unmap_sg() here, but it's messy */
350 hisi_sas_dma_unmap(hisi_hba, task, *n_elem,
356 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
357 struct sas_task *task, int n_elem_dif)
359 struct device *dev = hisi_hba->dev;
362 struct sas_ssp_task *ssp_task = &task->ssp_task;
363 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
365 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
366 scsi_prot_sg_count(scsi_cmnd),
371 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
372 int *n_elem_dif, struct sas_task *task)
374 struct device *dev = hisi_hba->dev;
375 struct sas_ssp_task *ssp_task;
376 struct scsi_cmnd *scsi_cmnd;
379 if (task->num_scatter) {
380 ssp_task = &task->ssp_task;
381 scsi_cmnd = ssp_task->cmd;
383 if (scsi_prot_sg_count(scsi_cmnd)) {
384 *n_elem_dif = dma_map_sg(dev,
385 scsi_prot_sglist(scsi_cmnd),
386 scsi_prot_sg_count(scsi_cmnd),
392 if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
393 dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
396 goto err_out_dif_dma_unmap;
403 err_out_dif_dma_unmap:
404 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
405 scsi_prot_sg_count(scsi_cmnd), task->data_dir);
409 static int hisi_sas_task_prep(struct sas_task *task,
410 struct hisi_sas_dq **dq_pointer,
411 bool is_tmf, struct hisi_sas_tmf_task *tmf,
414 struct domain_device *device = task->dev;
415 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
416 struct hisi_sas_device *sas_dev = device->lldd_dev;
417 struct hisi_sas_port *port;
418 struct hisi_sas_slot *slot;
419 struct hisi_sas_cmd_hdr *cmd_hdr_base;
420 struct asd_sas_port *sas_port = device->port;
421 struct device *dev = hisi_hba->dev;
422 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
423 int n_elem = 0, n_elem_dif = 0, n_elem_req = 0;
424 struct hisi_sas_dq *dq;
428 if (DEV_IS_GONE(sas_dev)) {
430 dev_info(dev, "task prep: device %d not ready\n",
433 dev_info(dev, "task prep: device %016llx not ready\n",
434 SAS_ADDR(device->sas_addr));
439 if (hisi_hba->reply_map) {
440 int cpu = raw_smp_processor_id();
441 unsigned int dq_index = hisi_hba->reply_map[cpu];
443 *dq_pointer = dq = &hisi_hba->dq[dq_index];
445 *dq_pointer = dq = sas_dev->dq;
448 port = to_hisi_sas_port(sas_port);
449 if (port && !port->port_attached) {
450 dev_info(dev, "task prep: %s port%d not attach device\n",
451 (dev_is_sata(device)) ?
458 rc = hisi_sas_dma_map(hisi_hba, task, &n_elem,
463 if (!sas_protocol_ata(task->task_proto)) {
464 rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
466 goto err_out_dma_unmap;
469 if (hisi_hba->hw->slot_index_alloc)
470 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
472 struct scsi_cmnd *scsi_cmnd = NULL;
474 if (task->uldd_task) {
475 struct ata_queued_cmd *qc;
477 if (dev_is_sata(device)) {
478 qc = task->uldd_task;
479 scsi_cmnd = qc->scsicmd;
481 scsi_cmnd = task->uldd_task;
484 rc = hisi_sas_slot_index_alloc(hisi_hba, scsi_cmnd);
487 goto err_out_dif_dma_unmap;
490 slot = &hisi_hba->slot_info[slot_idx];
492 spin_lock_irqsave(&dq->lock, flags);
493 wr_q_index = dq->wr_point;
494 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
495 list_add_tail(&slot->delivery, &dq->list);
496 spin_unlock_irqrestore(&dq->lock, flags);
497 spin_lock_irqsave(&sas_dev->lock, flags);
498 list_add_tail(&slot->entry, &sas_dev->list);
499 spin_unlock_irqrestore(&sas_dev->lock, flags);
501 dlvry_queue = dq->id;
502 dlvry_queue_slot = wr_q_index;
504 slot->device_id = sas_dev->device_id;
505 slot->n_elem = n_elem;
506 slot->n_elem_dif = n_elem_dif;
507 slot->dlvry_queue = dlvry_queue;
508 slot->dlvry_queue_slot = dlvry_queue_slot;
509 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
510 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
514 slot->is_internal = is_tmf;
515 task->lldd_task = slot;
517 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
518 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
519 memset(hisi_sas_status_buf_addr_mem(slot), 0,
520 sizeof(struct hisi_sas_err_record));
522 switch (task->task_proto) {
523 case SAS_PROTOCOL_SMP:
524 hisi_sas_task_prep_smp(hisi_hba, slot);
526 case SAS_PROTOCOL_SSP:
527 hisi_sas_task_prep_ssp(hisi_hba, slot);
529 case SAS_PROTOCOL_SATA:
530 case SAS_PROTOCOL_STP:
531 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
532 hisi_sas_task_prep_ata(hisi_hba, slot);
535 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
540 spin_lock_irqsave(&task->task_state_lock, flags);
541 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
542 spin_unlock_irqrestore(&task->task_state_lock, flags);
545 WRITE_ONCE(slot->ready, 1);
549 err_out_dif_dma_unmap:
550 if (!sas_protocol_ata(task->task_proto))
551 hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
553 hisi_sas_dma_unmap(hisi_hba, task, n_elem,
556 dev_err(dev, "task prep: failed[%d]!\n", rc);
560 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
561 bool is_tmf, struct hisi_sas_tmf_task *tmf)
566 struct hisi_hba *hisi_hba;
568 struct domain_device *device = task->dev;
569 struct asd_sas_port *sas_port = device->port;
570 struct hisi_sas_dq *dq = NULL;
573 struct task_status_struct *ts = &task->task_status;
575 ts->resp = SAS_TASK_UNDELIVERED;
576 ts->stat = SAS_PHY_DOWN;
578 * libsas will use dev->port, should
579 * not call task_done for sata
581 if (device->dev_type != SAS_SATA_DEV)
582 task->task_done(task);
586 hisi_hba = dev_to_hisi_hba(device);
589 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
591 * For IOs from upper layer, it may already disable preempt
592 * in the IO path, if disable preempt again in down(),
593 * function schedule() will report schedule_bug(), so check
594 * preemptible() before goto down().
599 down(&hisi_hba->sem);
603 /* protect task_prep and start_delivery sequence */
604 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
606 dev_err(dev, "task exec: failed[%d]!\n", rc);
609 spin_lock_irqsave(&dq->lock, flags);
610 hisi_hba->hw->start_delivery(dq);
611 spin_unlock_irqrestore(&dq->lock, flags);
617 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
619 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
620 struct asd_sas_phy *sas_phy = &phy->sas_phy;
621 struct sas_ha_struct *sas_ha;
623 if (!phy->phy_attached)
626 sas_ha = &hisi_hba->sha;
627 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
630 struct sas_phy *sphy = sas_phy->phy;
632 sphy->negotiated_linkrate = sas_phy->linkrate;
633 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
634 sphy->maximum_linkrate_hw =
635 hisi_hba->hw->phy_get_max_linkrate();
636 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
637 sphy->minimum_linkrate = phy->minimum_linkrate;
639 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
640 sphy->maximum_linkrate = phy->maximum_linkrate;
643 if (phy->phy_type & PORT_TYPE_SAS) {
644 struct sas_identify_frame *id;
646 id = (struct sas_identify_frame *)phy->frame_rcvd;
647 id->dev_type = phy->identify.device_type;
648 id->initiator_bits = SAS_PROTOCOL_ALL;
649 id->target_bits = phy->identify.target_port_protocols;
650 } else if (phy->phy_type & PORT_TYPE_SATA) {
654 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
655 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
658 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
660 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
661 struct hisi_sas_device *sas_dev = NULL;
663 int last = hisi_hba->last_dev_id;
664 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
667 spin_lock_irqsave(&hisi_hba->lock, flags);
668 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
669 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
670 int queue = i % hisi_hba->queue_count;
671 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
673 hisi_hba->devices[i].device_id = i;
674 sas_dev = &hisi_hba->devices[i];
675 sas_dev->dev_status = HISI_SAS_DEV_INIT;
676 sas_dev->dev_type = device->dev_type;
677 sas_dev->hisi_hba = hisi_hba;
678 sas_dev->sas_device = device;
680 spin_lock_init(&sas_dev->lock);
681 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
686 hisi_hba->last_dev_id = i;
687 spin_unlock_irqrestore(&hisi_hba->lock, flags);
692 #define HISI_SAS_DISK_RECOVER_CNT 3
693 static int hisi_sas_init_device(struct domain_device *device)
695 int rc = TMF_RESP_FUNC_COMPLETE;
697 struct hisi_sas_tmf_task tmf_task;
698 int retry = HISI_SAS_DISK_RECOVER_CNT;
699 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
700 struct device *dev = hisi_hba->dev;
701 struct sas_phy *local_phy;
703 switch (device->dev_type) {
705 int_to_scsilun(0, &lun);
707 tmf_task.tmf = TMF_CLEAR_TASK_SET;
708 while (retry-- > 0) {
709 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
711 if (rc == TMF_RESP_FUNC_COMPLETE) {
712 hisi_sas_release_task(hisi_hba, device);
719 case SAS_SATA_PM_PORT:
720 case SAS_SATA_PENDING:
722 * send HARD RESET to clear previous affiliation of
725 local_phy = sas_get_local_phy(device);
726 if (!scsi_is_sas_phy_local(local_phy) &&
727 !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
728 unsigned long deadline = ata_deadline(jiffies, 20000);
729 struct sata_device *sata_dev = &device->sata_dev;
730 struct ata_host *ata_host = sata_dev->ata_host;
731 struct ata_port_operations *ops = ata_host->ops;
732 struct ata_port *ap = sata_dev->ap;
733 struct ata_link *link;
734 unsigned int classes;
736 ata_for_each_link(link, ap, EDGE)
737 rc = ops->hardreset(link, &classes,
740 sas_put_local_phy(local_phy);
742 dev_warn(dev, "SATA disk hardreset fail: %d\n", rc);
746 while (retry-- > 0) {
747 rc = hisi_sas_softreset_ata_disk(device);
759 static int hisi_sas_dev_found(struct domain_device *device)
761 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
762 struct domain_device *parent_dev = device->parent;
763 struct hisi_sas_device *sas_dev;
764 struct device *dev = hisi_hba->dev;
767 if (hisi_hba->hw->alloc_dev)
768 sas_dev = hisi_hba->hw->alloc_dev(device);
770 sas_dev = hisi_sas_alloc_dev(device);
772 dev_err(dev, "fail alloc dev: max support %d devices\n",
773 HISI_SAS_MAX_DEVICES);
777 device->lldd_dev = sas_dev;
778 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
780 if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
782 u8 phy_num = parent_dev->ex_dev.num_phys;
785 for (phy_no = 0; phy_no < phy_num; phy_no++) {
786 phy = &parent_dev->ex_dev.ex_phy[phy_no];
787 if (SAS_ADDR(phy->attached_sas_addr) ==
788 SAS_ADDR(device->sas_addr))
792 if (phy_no == phy_num) {
793 dev_info(dev, "dev found: no attached "
794 "dev:%016llx at ex:%016llx\n",
795 SAS_ADDR(device->sas_addr),
796 SAS_ADDR(parent_dev->sas_addr));
802 dev_info(dev, "dev[%d:%x] found\n",
803 sas_dev->device_id, sas_dev->dev_type);
805 rc = hisi_sas_init_device(device);
808 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
812 hisi_sas_dev_gone(device);
816 int hisi_sas_slave_configure(struct scsi_device *sdev)
818 struct domain_device *dev = sdev_to_domain_dev(sdev);
819 int ret = sas_slave_configure(sdev);
823 if (!dev_is_sata(dev))
824 sas_change_queue_depth(sdev, 64);
828 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
830 void hisi_sas_scan_start(struct Scsi_Host *shost)
832 struct hisi_hba *hisi_hba = shost_priv(shost);
834 hisi_hba->hw->phys_init(hisi_hba);
836 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
838 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
840 struct hisi_hba *hisi_hba = shost_priv(shost);
841 struct sas_ha_struct *sha = &hisi_hba->sha;
843 /* Wait for PHY up interrupt to occur */
850 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
852 static void hisi_sas_phyup_work(struct work_struct *work)
854 struct hisi_sas_phy *phy =
855 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
856 struct hisi_hba *hisi_hba = phy->hisi_hba;
857 struct asd_sas_phy *sas_phy = &phy->sas_phy;
858 int phy_no = sas_phy->id;
860 if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
861 hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
862 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
865 static void hisi_sas_linkreset_work(struct work_struct *work)
867 struct hisi_sas_phy *phy =
868 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
869 struct asd_sas_phy *sas_phy = &phy->sas_phy;
871 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
874 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
875 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
876 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
879 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
880 enum hisi_sas_phy_event event)
882 struct hisi_hba *hisi_hba = phy->hisi_hba;
884 if (WARN_ON(event >= HISI_PHYES_NUM))
887 return queue_work(hisi_hba->wq, &phy->works[event]);
889 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
891 static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
893 struct hisi_sas_phy *phy = from_timer(phy, t, timer);
894 struct hisi_hba *hisi_hba = phy->hisi_hba;
895 struct device *dev = hisi_hba->dev;
896 int phy_no = phy->sas_phy.id;
898 dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
899 hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
902 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
904 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
905 struct device *dev = hisi_hba->dev;
907 dev_dbg(dev, "phy%d OOB ready\n", phy_no);
908 if (phy->phy_attached)
911 if (!timer_pending(&phy->timer)) {
912 phy->timer.expires = jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT * HZ;
913 add_timer(&phy->timer);
916 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
918 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
920 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
921 struct asd_sas_phy *sas_phy = &phy->sas_phy;
924 phy->hisi_hba = hisi_hba;
926 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
927 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
928 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
929 sas_phy->class = SAS;
930 sas_phy->iproto = SAS_PROTOCOL_ALL;
932 sas_phy->type = PHY_TYPE_PHYSICAL;
933 sas_phy->role = PHY_ROLE_INITIATOR;
934 sas_phy->oob_mode = OOB_NOT_CONNECTED;
935 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
936 sas_phy->id = phy_no;
937 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
938 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
939 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
940 sas_phy->lldd_phy = phy;
942 for (i = 0; i < HISI_PHYES_NUM; i++)
943 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
945 spin_lock_init(&phy->lock);
947 timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
950 /* Wrapper to ensure we track hisi_sas_phy.enable properly */
951 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable)
953 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
954 struct asd_sas_phy *aphy = &phy->sas_phy;
955 struct sas_phy *sphy = aphy->phy;
958 spin_lock_irqsave(&phy->lock, flags);
961 /* We may have been enabled already; if so, don't touch */
963 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
964 hisi_hba->hw->phy_start(hisi_hba, phy_no);
966 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
967 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
969 phy->enable = enable;
970 spin_unlock_irqrestore(&phy->lock, flags);
972 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable);
974 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
976 struct sas_ha_struct *sas_ha = sas_phy->ha;
977 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
978 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
979 struct asd_sas_port *sas_port = sas_phy->port;
980 struct hisi_sas_port *port;
986 port = to_hisi_sas_port(sas_port);
987 spin_lock_irqsave(&hisi_hba->lock, flags);
988 port->port_attached = 1;
989 port->id = phy->port_id;
991 sas_port->lldd_port = port;
992 spin_unlock_irqrestore(&hisi_hba->lock, flags);
995 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
996 struct hisi_sas_slot *slot)
1000 struct task_status_struct *ts;
1002 ts = &task->task_status;
1004 ts->resp = SAS_TASK_COMPLETE;
1005 ts->stat = SAS_ABORTED_TASK;
1006 spin_lock_irqsave(&task->task_state_lock, flags);
1007 task->task_state_flags &=
1008 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1009 if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
1010 task->task_state_flags |= SAS_TASK_STATE_DONE;
1011 spin_unlock_irqrestore(&task->task_state_lock, flags);
1014 hisi_sas_slot_task_free(hisi_hba, task, slot);
1017 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
1018 struct domain_device *device)
1020 struct hisi_sas_slot *slot, *slot2;
1021 struct hisi_sas_device *sas_dev = device->lldd_dev;
1023 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
1024 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
1027 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
1029 struct hisi_sas_device *sas_dev;
1030 struct domain_device *device;
1033 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1034 sas_dev = &hisi_hba->devices[i];
1035 device = sas_dev->sas_device;
1037 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
1041 hisi_sas_release_task(hisi_hba, device);
1044 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
1046 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
1047 struct domain_device *device)
1049 if (hisi_hba->hw->dereg_device)
1050 hisi_hba->hw->dereg_device(hisi_hba, device);
1053 static void hisi_sas_dev_gone(struct domain_device *device)
1055 struct hisi_sas_device *sas_dev = device->lldd_dev;
1056 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1057 struct device *dev = hisi_hba->dev;
1059 dev_info(dev, "dev[%d:%x] is gone\n",
1060 sas_dev->device_id, sas_dev->dev_type);
1062 down(&hisi_hba->sem);
1063 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
1064 hisi_sas_internal_task_abort(hisi_hba, device,
1065 HISI_SAS_INT_ABT_DEV, 0);
1067 hisi_sas_dereg_device(hisi_hba, device);
1069 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
1070 device->lldd_dev = NULL;
1073 if (hisi_hba->hw->free_device)
1074 hisi_hba->hw->free_device(sas_dev);
1075 sas_dev->dev_type = SAS_PHY_UNUSED;
1076 sas_dev->sas_device = NULL;
1080 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
1082 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
1085 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
1086 struct sas_phy_linkrates *r)
1088 struct sas_phy_linkrates _r;
1090 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1091 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1092 enum sas_linkrate min, max;
1094 if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
1097 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1098 max = sas_phy->phy->maximum_linkrate;
1099 min = r->minimum_linkrate;
1100 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1101 max = r->maximum_linkrate;
1102 min = sas_phy->phy->minimum_linkrate;
1106 _r.maximum_linkrate = max;
1107 _r.minimum_linkrate = min;
1109 sas_phy->phy->maximum_linkrate = max;
1110 sas_phy->phy->minimum_linkrate = min;
1112 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1114 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
1115 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1120 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
1123 struct sas_ha_struct *sas_ha = sas_phy->ha;
1124 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1125 int phy_no = sas_phy->id;
1128 case PHY_FUNC_HARD_RESET:
1129 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
1132 case PHY_FUNC_LINK_RESET:
1133 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1135 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1138 case PHY_FUNC_DISABLE:
1139 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1142 case PHY_FUNC_SET_LINK_RATE:
1143 return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1144 case PHY_FUNC_GET_EVENTS:
1145 if (hisi_hba->hw->get_events) {
1146 hisi_hba->hw->get_events(hisi_hba, phy_no);
1150 case PHY_FUNC_RELEASE_SPINUP_HOLD:
1157 static void hisi_sas_task_done(struct sas_task *task)
1159 del_timer(&task->slow_task->timer);
1160 complete(&task->slow_task->completion);
1163 static void hisi_sas_tmf_timedout(struct timer_list *t)
1165 struct sas_task_slow *slow = from_timer(slow, t, timer);
1166 struct sas_task *task = slow->task;
1167 unsigned long flags;
1168 bool is_completed = true;
1170 spin_lock_irqsave(&task->task_state_lock, flags);
1171 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1172 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1173 is_completed = false;
1175 spin_unlock_irqrestore(&task->task_state_lock, flags);
1178 complete(&task->slow_task->completion);
1181 #define TASK_TIMEOUT 20
1182 #define TASK_RETRY 3
1183 #define INTERNAL_ABORT_TIMEOUT 6
1184 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1185 void *parameter, u32 para_len,
1186 struct hisi_sas_tmf_task *tmf)
1188 struct hisi_sas_device *sas_dev = device->lldd_dev;
1189 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1190 struct device *dev = hisi_hba->dev;
1191 struct sas_task *task;
1194 for (retry = 0; retry < TASK_RETRY; retry++) {
1195 task = sas_alloc_slow_task(GFP_KERNEL);
1200 task->task_proto = device->tproto;
1202 if (dev_is_sata(device)) {
1203 task->ata_task.device_control_reg_update = 1;
1204 memcpy(&task->ata_task.fis, parameter, para_len);
1206 memcpy(&task->ssp_task, parameter, para_len);
1208 task->task_done = hisi_sas_task_done;
1210 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1211 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT * HZ;
1212 add_timer(&task->slow_task->timer);
1214 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1217 del_timer(&task->slow_task->timer);
1218 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1223 wait_for_completion(&task->slow_task->completion);
1224 res = TMF_RESP_FUNC_FAILED;
1225 /* Even TMF timed out, return direct. */
1226 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1227 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1228 struct hisi_sas_slot *slot = task->lldd_task;
1230 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1232 struct hisi_sas_cq *cq =
1233 &hisi_hba->cq[slot->dlvry_queue];
1235 * flush tasklet to avoid free'ing task
1236 * before using task in IO completion
1238 tasklet_kill(&cq->tasklet);
1244 dev_err(dev, "abort tmf: TMF task timeout\n");
1247 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1248 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1249 res = TMF_RESP_FUNC_COMPLETE;
1253 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1254 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1255 res = TMF_RESP_FUNC_SUCC;
1259 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1260 task->task_status.stat == SAS_DATA_UNDERRUN) {
1261 /* no error, but return the number of bytes of
1264 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x sts 0x%x underrun\n",
1265 SAS_ADDR(device->sas_addr),
1266 task->task_status.resp,
1267 task->task_status.stat);
1268 res = task->task_status.residual;
1272 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1273 task->task_status.stat == SAS_DATA_OVERRUN) {
1274 dev_warn(dev, "abort tmf: blocked task error\n");
1279 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1280 task->task_status.stat == SAS_OPEN_REJECT) {
1281 dev_warn(dev, "abort tmf: open reject failed\n");
1284 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x status 0x%x\n",
1285 SAS_ADDR(device->sas_addr),
1286 task->task_status.resp,
1287 task->task_status.stat);
1289 sas_free_task(task);
1293 if (retry == TASK_RETRY)
1294 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1295 sas_free_task(task);
1299 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1300 bool reset, int pmp, u8 *fis)
1302 struct ata_taskfile tf;
1304 ata_tf_init(dev, &tf);
1308 tf.ctl &= ~ATA_SRST;
1309 tf.command = ATA_CMD_DEV_RESET;
1310 ata_tf_to_fis(&tf, pmp, 0, fis);
1313 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1316 struct ata_port *ap = device->sata_dev.ap;
1317 struct ata_link *link;
1318 int rc = TMF_RESP_FUNC_FAILED;
1319 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1320 struct device *dev = hisi_hba->dev;
1321 int s = sizeof(struct host_to_dev_fis);
1323 ata_for_each_link(link, ap, EDGE) {
1324 int pmp = sata_srst_pmp(link);
1326 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1327 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1328 if (rc != TMF_RESP_FUNC_COMPLETE)
1332 if (rc == TMF_RESP_FUNC_COMPLETE) {
1333 ata_for_each_link(link, ap, EDGE) {
1334 int pmp = sata_srst_pmp(link);
1336 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1337 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1339 if (rc != TMF_RESP_FUNC_COMPLETE)
1340 dev_err(dev, "ata disk de-reset failed\n");
1343 dev_err(dev, "ata disk reset failed\n");
1346 if (rc == TMF_RESP_FUNC_COMPLETE)
1347 hisi_sas_release_task(hisi_hba, device);
1352 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1353 u8 *lun, struct hisi_sas_tmf_task *tmf)
1355 struct sas_ssp_task ssp_task;
1357 if (!(device->tproto & SAS_PROTOCOL_SSP))
1358 return TMF_RESP_FUNC_ESUPP;
1360 memcpy(ssp_task.LUN, lun, 8);
1362 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1363 sizeof(ssp_task), tmf);
1366 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1368 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1371 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1372 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1373 struct domain_device *device = sas_dev->sas_device;
1374 struct asd_sas_port *sas_port;
1375 struct hisi_sas_port *port;
1376 struct hisi_sas_phy *phy = NULL;
1377 struct asd_sas_phy *sas_phy;
1379 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1380 || !device || !device->port)
1383 sas_port = device->port;
1384 port = to_hisi_sas_port(sas_port);
1386 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1387 if (state & BIT(sas_phy->id)) {
1388 phy = sas_phy->lldd_phy;
1393 port->id = phy->port_id;
1395 /* Update linkrate of directly attached device. */
1396 if (!device->parent)
1397 device->linkrate = phy->sas_phy.linkrate;
1399 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1405 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state)
1407 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1408 struct asd_sas_port *_sas_port = NULL;
1411 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1412 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1413 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1414 struct asd_sas_port *sas_port = sas_phy->port;
1415 bool do_port_check = !!(_sas_port != sas_port);
1417 if (!sas_phy->phy->enabled)
1420 /* Report PHY state change to libsas */
1421 if (state & BIT(phy_no)) {
1422 if (do_port_check && sas_port && sas_port->port_dev) {
1423 struct domain_device *dev = sas_port->port_dev;
1425 _sas_port = sas_port;
1427 if (dev_is_expander(dev->dev_type))
1428 sas_ha->notify_port_event(sas_phy,
1429 PORTE_BROADCAST_RCVD);
1432 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1438 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1440 struct hisi_sas_device *sas_dev;
1441 struct domain_device *device;
1444 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1445 sas_dev = &hisi_hba->devices[i];
1446 device = sas_dev->sas_device;
1448 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1451 hisi_sas_init_device(device);
1455 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1456 struct asd_sas_port *sas_port,
1457 struct domain_device *device)
1459 struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1460 struct ata_port *ap = device->sata_dev.ap;
1461 struct device *dev = hisi_hba->dev;
1462 int s = sizeof(struct host_to_dev_fis);
1463 int rc = TMF_RESP_FUNC_FAILED;
1464 struct asd_sas_phy *sas_phy;
1465 struct ata_link *link;
1469 state = hisi_hba->hw->get_phys_state(hisi_hba);
1470 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1471 if (!(state & BIT(sas_phy->id)))
1474 ata_for_each_link(link, ap, EDGE) {
1475 int pmp = sata_srst_pmp(link);
1477 tmf_task.phy_id = sas_phy->id;
1478 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1479 rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1481 if (rc != TMF_RESP_FUNC_COMPLETE) {
1482 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1490 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1492 struct device *dev = hisi_hba->dev;
1495 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1496 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1497 struct domain_device *device = sas_dev->sas_device;
1499 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1502 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1503 HISI_SAS_INT_ABT_DEV, 0);
1505 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1508 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1509 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1510 struct asd_sas_port *sas_port = &port->sas_port;
1511 struct domain_device *port_dev = sas_port->port_dev;
1512 struct domain_device *device;
1514 if (!port_dev || !dev_is_expander(port_dev->dev_type))
1517 /* Try to find a SATA device */
1518 list_for_each_entry(device, &sas_port->dev_list,
1520 if (dev_is_sata(device)) {
1521 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1530 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1532 struct Scsi_Host *shost = hisi_hba->shost;
1534 down(&hisi_hba->sem);
1535 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1537 scsi_block_requests(shost);
1538 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1540 if (timer_pending(&hisi_hba->timer))
1541 del_timer_sync(&hisi_hba->timer);
1543 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1545 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1547 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1549 struct Scsi_Host *shost = hisi_hba->shost;
1552 /* Init and wait for PHYs to come up and all libsas event finished. */
1553 hisi_hba->hw->phys_init(hisi_hba);
1555 hisi_sas_refresh_port_id(hisi_hba);
1556 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1558 if (hisi_hba->reject_stp_links_msk)
1559 hisi_sas_terminate_stp_reject(hisi_hba);
1560 hisi_sas_reset_init_all_devices(hisi_hba);
1562 scsi_unblock_requests(shost);
1563 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1565 state = hisi_hba->hw->get_phys_state(hisi_hba);
1566 hisi_sas_rescan_topology(hisi_hba, state);
1568 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1570 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1572 struct device *dev = hisi_hba->dev;
1573 struct Scsi_Host *shost = hisi_hba->shost;
1576 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct)
1577 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
1579 if (!hisi_hba->hw->soft_reset)
1582 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1585 dev_info(dev, "controller resetting...\n");
1586 hisi_sas_controller_reset_prepare(hisi_hba);
1588 rc = hisi_hba->hw->soft_reset(hisi_hba);
1590 dev_warn(dev, "controller reset failed (%d)\n", rc);
1591 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1593 scsi_unblock_requests(shost);
1594 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1598 hisi_sas_controller_reset_done(hisi_hba);
1599 dev_info(dev, "controller reset complete\n");
1604 static int hisi_sas_abort_task(struct sas_task *task)
1606 struct scsi_lun lun;
1607 struct hisi_sas_tmf_task tmf_task;
1608 struct domain_device *device = task->dev;
1609 struct hisi_sas_device *sas_dev = device->lldd_dev;
1610 struct hisi_hba *hisi_hba;
1612 int rc = TMF_RESP_FUNC_FAILED;
1613 unsigned long flags;
1616 return TMF_RESP_FUNC_FAILED;
1618 hisi_hba = dev_to_hisi_hba(task->dev);
1619 dev = hisi_hba->dev;
1621 spin_lock_irqsave(&task->task_state_lock, flags);
1622 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1623 struct hisi_sas_slot *slot = task->lldd_task;
1624 struct hisi_sas_cq *cq;
1628 * flush tasklet to avoid free'ing task
1629 * before using task in IO completion
1631 cq = &hisi_hba->cq[slot->dlvry_queue];
1632 tasklet_kill(&cq->tasklet);
1634 spin_unlock_irqrestore(&task->task_state_lock, flags);
1635 rc = TMF_RESP_FUNC_COMPLETE;
1638 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1639 spin_unlock_irqrestore(&task->task_state_lock, flags);
1641 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1642 struct scsi_cmnd *cmnd = task->uldd_task;
1643 struct hisi_sas_slot *slot = task->lldd_task;
1644 u16 tag = slot->idx;
1647 int_to_scsilun(cmnd->device->lun, &lun);
1648 tmf_task.tmf = TMF_ABORT_TASK;
1649 tmf_task.tag_of_task_to_be_managed = tag;
1651 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1654 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1655 HISI_SAS_INT_ABT_CMD, tag);
1657 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1658 return TMF_RESP_FUNC_FAILED;
1662 * If the TMF finds that the IO is not in the device and also
1663 * the internal abort does not succeed, then it is safe to
1665 * Note: if the internal abort succeeds then the slot
1666 * will have already been completed
1668 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1669 if (task->lldd_task)
1670 hisi_sas_do_release_task(hisi_hba, task, slot);
1672 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1673 task->task_proto & SAS_PROTOCOL_STP) {
1674 if (task->dev->dev_type == SAS_SATA_DEV) {
1675 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1676 HISI_SAS_INT_ABT_DEV,
1679 dev_err(dev, "abort task: internal abort failed\n");
1682 hisi_sas_dereg_device(hisi_hba, device);
1683 rc = hisi_sas_softreset_ata_disk(device);
1685 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1687 struct hisi_sas_slot *slot = task->lldd_task;
1688 u32 tag = slot->idx;
1689 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1691 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1692 HISI_SAS_INT_ABT_CMD, tag);
1693 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1696 * flush tasklet to avoid free'ing task
1697 * before using task in IO completion
1699 tasklet_kill(&cq->tasklet);
1705 if (rc != TMF_RESP_FUNC_COMPLETE)
1706 dev_notice(dev, "abort task: rc=%d\n", rc);
1710 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1712 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1713 struct device *dev = hisi_hba->dev;
1714 struct hisi_sas_tmf_task tmf_task;
1717 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1718 HISI_SAS_INT_ABT_DEV, 0);
1720 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1721 return TMF_RESP_FUNC_FAILED;
1723 hisi_sas_dereg_device(hisi_hba, device);
1725 tmf_task.tmf = TMF_ABORT_TASK_SET;
1726 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1728 if (rc == TMF_RESP_FUNC_COMPLETE)
1729 hisi_sas_release_task(hisi_hba, device);
1734 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1736 struct hisi_sas_tmf_task tmf_task;
1739 tmf_task.tmf = TMF_CLEAR_ACA;
1740 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1745 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1747 struct sas_phy *local_phy = sas_get_local_phy(device);
1748 struct hisi_sas_device *sas_dev = device->lldd_dev;
1749 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1750 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1751 DECLARE_COMPLETION_ONSTACK(phyreset);
1754 if (!local_phy->enabled) {
1755 sas_put_local_phy(local_phy);
1759 if (scsi_is_sas_phy_local(local_phy)) {
1760 struct asd_sas_phy *sas_phy =
1761 sas_ha->sas_phy[local_phy->number];
1762 struct hisi_sas_phy *phy =
1763 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1765 phy->reset_completion = &phyreset;
1768 reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
1769 !dev_is_sata(device)) ? true : false;
1771 rc = sas_phy_reset(local_phy, reset_type);
1772 sas_put_local_phy(local_phy);
1774 if (scsi_is_sas_phy_local(local_phy)) {
1775 struct asd_sas_phy *sas_phy =
1776 sas_ha->sas_phy[local_phy->number];
1777 struct hisi_sas_phy *phy =
1778 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1779 int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1780 unsigned long flags;
1782 spin_lock_irqsave(&phy->lock, flags);
1783 phy->reset_completion = NULL;
1785 spin_unlock_irqrestore(&phy->lock, flags);
1787 /* report PHY down if timed out */
1789 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
1790 } else if (sas_dev->dev_status != HISI_SAS_DEV_INIT) {
1792 * If in init state, we rely on caller to wait for link to be
1793 * ready; otherwise, except phy reset is fail, delay.
1802 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1804 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1805 struct device *dev = hisi_hba->dev;
1808 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1809 HISI_SAS_INT_ABT_DEV, 0);
1811 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1812 return TMF_RESP_FUNC_FAILED;
1814 hisi_sas_dereg_device(hisi_hba, device);
1816 if (dev_is_sata(device)) {
1817 rc = hisi_sas_softreset_ata_disk(device);
1818 if (rc == TMF_RESP_FUNC_FAILED)
1819 return TMF_RESP_FUNC_FAILED;
1822 rc = hisi_sas_debug_I_T_nexus_reset(device);
1824 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1825 hisi_sas_release_task(hisi_hba, device);
1830 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1832 struct hisi_sas_device *sas_dev = device->lldd_dev;
1833 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1834 struct device *dev = hisi_hba->dev;
1835 int rc = TMF_RESP_FUNC_FAILED;
1837 /* Clear internal IO and then lu reset */
1838 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1839 HISI_SAS_INT_ABT_DEV, 0);
1841 dev_err(dev, "lu_reset: internal abort failed\n");
1844 hisi_sas_dereg_device(hisi_hba, device);
1846 if (dev_is_sata(device)) {
1847 struct sas_phy *phy;
1849 phy = sas_get_local_phy(device);
1851 rc = sas_phy_reset(phy, true);
1854 hisi_sas_release_task(hisi_hba, device);
1855 sas_put_local_phy(phy);
1857 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1859 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1860 if (rc == TMF_RESP_FUNC_COMPLETE)
1861 hisi_sas_release_task(hisi_hba, device);
1864 if (rc != TMF_RESP_FUNC_COMPLETE)
1865 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1866 sas_dev->device_id, rc);
1870 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1872 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1873 struct device *dev = hisi_hba->dev;
1874 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1877 queue_work(hisi_hba->wq, &r.work);
1878 wait_for_completion(r.completion);
1880 return TMF_RESP_FUNC_FAILED;
1882 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1883 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1884 struct domain_device *device = sas_dev->sas_device;
1886 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1887 dev_is_expander(device->dev_type))
1890 rc = hisi_sas_debug_I_T_nexus_reset(device);
1891 if (rc != TMF_RESP_FUNC_COMPLETE)
1892 dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
1893 sas_dev->device_id, rc);
1896 hisi_sas_release_tasks(hisi_hba);
1898 return TMF_RESP_FUNC_COMPLETE;
1901 static int hisi_sas_query_task(struct sas_task *task)
1903 struct scsi_lun lun;
1904 struct hisi_sas_tmf_task tmf_task;
1905 int rc = TMF_RESP_FUNC_FAILED;
1907 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1908 struct scsi_cmnd *cmnd = task->uldd_task;
1909 struct domain_device *device = task->dev;
1910 struct hisi_sas_slot *slot = task->lldd_task;
1911 u32 tag = slot->idx;
1913 int_to_scsilun(cmnd->device->lun, &lun);
1914 tmf_task.tmf = TMF_QUERY_TASK;
1915 tmf_task.tag_of_task_to_be_managed = tag;
1917 rc = hisi_sas_debug_issue_ssp_tmf(device,
1921 /* The task is still in Lun, release it then */
1922 case TMF_RESP_FUNC_SUCC:
1923 /* The task is not in Lun or failed, reset the phy */
1924 case TMF_RESP_FUNC_FAILED:
1925 case TMF_RESP_FUNC_COMPLETE:
1928 rc = TMF_RESP_FUNC_FAILED;
1936 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1937 struct sas_task *task, int abort_flag,
1938 int task_tag, struct hisi_sas_dq *dq)
1940 struct domain_device *device = task->dev;
1941 struct hisi_sas_device *sas_dev = device->lldd_dev;
1942 struct device *dev = hisi_hba->dev;
1943 struct hisi_sas_port *port;
1944 struct hisi_sas_slot *slot;
1945 struct asd_sas_port *sas_port = device->port;
1946 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1947 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1948 unsigned long flags;
1951 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1957 port = to_hisi_sas_port(sas_port);
1959 /* simply get a slot and send abort command */
1960 rc = hisi_sas_slot_index_alloc(hisi_hba, NULL);
1965 slot = &hisi_hba->slot_info[slot_idx];
1967 spin_lock_irqsave(&dq->lock, flags);
1968 wr_q_index = dq->wr_point;
1969 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
1970 list_add_tail(&slot->delivery, &dq->list);
1971 spin_unlock_irqrestore(&dq->lock, flags);
1972 spin_lock_irqsave(&sas_dev->lock, flags);
1973 list_add_tail(&slot->entry, &sas_dev->list);
1974 spin_unlock_irqrestore(&sas_dev->lock, flags);
1976 dlvry_queue = dq->id;
1977 dlvry_queue_slot = wr_q_index;
1979 slot->device_id = sas_dev->device_id;
1980 slot->n_elem = n_elem;
1981 slot->dlvry_queue = dlvry_queue;
1982 slot->dlvry_queue_slot = dlvry_queue_slot;
1983 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1984 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1987 slot->is_internal = true;
1988 task->lldd_task = slot;
1990 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1991 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1992 memset(hisi_sas_status_buf_addr_mem(slot), 0,
1993 sizeof(struct hisi_sas_err_record));
1995 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1996 abort_flag, task_tag);
1998 spin_lock_irqsave(&task->task_state_lock, flags);
1999 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
2000 spin_unlock_irqrestore(&task->task_state_lock, flags);
2001 WRITE_ONCE(slot->ready, 1);
2002 /* send abort command to the chip */
2003 spin_lock_irqsave(&dq->lock, flags);
2004 hisi_hba->hw->start_delivery(dq);
2005 spin_unlock_irqrestore(&dq->lock, flags);
2010 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
2016 * _hisi_sas_internal_task_abort -- execute an internal
2017 * abort command for single IO command or a device
2018 * @hisi_hba: host controller struct
2019 * @device: domain device
2020 * @abort_flag: mode of operation, device or single IO
2021 * @tag: tag of IO to be aborted (only relevant to single
2023 * @dq: delivery queue for this internal abort command
2026 _hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2027 struct domain_device *device, int abort_flag,
2028 int tag, struct hisi_sas_dq *dq)
2030 struct sas_task *task;
2031 struct hisi_sas_device *sas_dev = device->lldd_dev;
2032 struct device *dev = hisi_hba->dev;
2036 * The interface is not realized means this HW don't support internal
2037 * abort, or don't need to do internal abort. Then here, we return
2038 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
2039 * the internal abort has been executed and returned CQ.
2041 if (!hisi_hba->hw->prep_abort)
2042 return TMF_RESP_FUNC_FAILED;
2044 task = sas_alloc_slow_task(GFP_KERNEL);
2049 task->task_proto = device->tproto;
2050 task->task_done = hisi_sas_task_done;
2051 task->slow_task->timer.function = hisi_sas_tmf_timedout;
2052 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT * HZ;
2053 add_timer(&task->slow_task->timer);
2055 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
2056 task, abort_flag, tag, dq);
2058 del_timer(&task->slow_task->timer);
2059 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
2063 wait_for_completion(&task->slow_task->completion);
2064 res = TMF_RESP_FUNC_FAILED;
2066 /* Internal abort timed out */
2067 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
2068 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct)
2069 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
2071 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
2072 struct hisi_sas_slot *slot = task->lldd_task;
2075 struct hisi_sas_cq *cq =
2076 &hisi_hba->cq[slot->dlvry_queue];
2078 * flush tasklet to avoid free'ing task
2079 * before using task in IO completion
2081 tasklet_kill(&cq->tasklet);
2084 dev_err(dev, "internal task abort: timeout and not done.\n");
2089 dev_err(dev, "internal task abort: timeout.\n");
2092 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2093 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
2094 res = TMF_RESP_FUNC_COMPLETE;
2098 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2099 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
2100 res = TMF_RESP_FUNC_SUCC;
2105 dev_dbg(dev, "internal task abort: task to dev %016llx task=%pK resp: 0x%x sts 0x%x\n",
2106 SAS_ADDR(device->sas_addr), task,
2107 task->task_status.resp, /* 0 is complete, -1 is undelivered */
2108 task->task_status.stat);
2109 sas_free_task(task);
2115 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2116 struct domain_device *device,
2117 int abort_flag, int tag)
2119 struct hisi_sas_slot *slot;
2120 struct device *dev = hisi_hba->dev;
2121 struct hisi_sas_dq *dq;
2124 switch (abort_flag) {
2125 case HISI_SAS_INT_ABT_CMD:
2126 slot = &hisi_hba->slot_info[tag];
2127 dq = &hisi_hba->dq[slot->dlvry_queue];
2128 return _hisi_sas_internal_task_abort(hisi_hba, device,
2129 abort_flag, tag, dq);
2130 case HISI_SAS_INT_ABT_DEV:
2131 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2132 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2133 const struct cpumask *mask = cq->pci_irq_mask;
2135 if (mask && !cpumask_intersects(cpu_online_mask, mask))
2137 dq = &hisi_hba->dq[i];
2138 rc = _hisi_sas_internal_task_abort(hisi_hba, device,
2146 dev_err(dev, "Unrecognised internal abort flag (%d)\n",
2154 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
2156 hisi_sas_port_notify_formed(sas_phy);
2159 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
2160 u8 reg_index, u8 reg_count, u8 *write_data)
2162 struct hisi_hba *hisi_hba = sha->lldd_ha;
2164 if (!hisi_hba->hw->write_gpio)
2167 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
2168 reg_index, reg_count, write_data);
2171 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
2173 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2174 struct sas_phy *sphy = sas_phy->phy;
2175 unsigned long flags;
2177 phy->phy_attached = 0;
2181 spin_lock_irqsave(&phy->lock, flags);
2183 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
2185 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
2186 spin_unlock_irqrestore(&phy->lock, flags);
2189 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
2191 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2192 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2193 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
2194 struct device *dev = hisi_hba->dev;
2197 /* Phy down but ready */
2198 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
2199 hisi_sas_port_notify_formed(sas_phy);
2201 struct hisi_sas_port *port = phy->port;
2203 if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
2205 dev_info(dev, "ignore flutter phy%d down\n", phy_no);
2208 /* Phy down and not ready */
2209 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
2210 sas_phy_disconnected(sas_phy);
2213 if (phy->phy_type & PORT_TYPE_SAS) {
2214 int port_id = port->id;
2216 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2218 port->port_attached = 0;
2219 } else if (phy->phy_type & PORT_TYPE_SATA)
2220 port->port_attached = 0;
2222 hisi_sas_phy_disconnected(phy);
2225 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2227 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
2231 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2232 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2234 tasklet_kill(&cq->tasklet);
2237 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
2239 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type)
2241 struct hisi_hba *hisi_hba = shost_priv(shost);
2243 if (reset_type != SCSI_ADAPTER_RESET)
2246 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2250 EXPORT_SYMBOL_GPL(hisi_sas_host_reset);
2252 struct scsi_transport_template *hisi_sas_stt;
2253 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2255 static struct sas_domain_function_template hisi_sas_transport_ops = {
2256 .lldd_dev_found = hisi_sas_dev_found,
2257 .lldd_dev_gone = hisi_sas_dev_gone,
2258 .lldd_execute_task = hisi_sas_queue_command,
2259 .lldd_control_phy = hisi_sas_control_phy,
2260 .lldd_abort_task = hisi_sas_abort_task,
2261 .lldd_abort_task_set = hisi_sas_abort_task_set,
2262 .lldd_clear_aca = hisi_sas_clear_aca,
2263 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
2264 .lldd_lu_reset = hisi_sas_lu_reset,
2265 .lldd_query_task = hisi_sas_query_task,
2266 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
2267 .lldd_port_formed = hisi_sas_port_formed,
2268 .lldd_write_gpio = hisi_sas_write_gpio,
2271 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2273 int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS;
2274 struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
2276 for (i = 0; i < hisi_hba->queue_count; i++) {
2277 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2278 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2279 struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
2281 s = sizeof(struct hisi_sas_cmd_hdr);
2282 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2283 memset(&cmd_hdr[j], 0, s);
2287 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2288 memset(hisi_hba->complete_hdr[i], 0, s);
2292 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2293 memset(hisi_hba->initial_fis, 0, s);
2295 s = max_command_entries * sizeof(struct hisi_sas_iost);
2296 memset(hisi_hba->iost, 0, s);
2298 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2299 memset(hisi_hba->breakpoint, 0, s);
2301 s = sizeof(struct hisi_sas_sata_breakpoint);
2302 for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
2303 memset(&sata_breakpoint[j], 0, s);
2305 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2307 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2309 struct device *dev = hisi_hba->dev;
2310 int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS;
2311 int max_command_entries_ru, sz_slot_buf_ru;
2312 int blk_cnt, slots_per_blk;
2314 sema_init(&hisi_hba->sem, 1);
2315 spin_lock_init(&hisi_hba->lock);
2316 for (i = 0; i < hisi_hba->n_phy; i++) {
2317 hisi_sas_phy_init(hisi_hba, i);
2318 hisi_hba->port[i].port_attached = 0;
2319 hisi_hba->port[i].id = -1;
2322 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2323 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2324 hisi_hba->devices[i].device_id = i;
2325 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
2328 for (i = 0; i < hisi_hba->queue_count; i++) {
2329 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2330 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2332 /* Completion queue structure */
2334 cq->hisi_hba = hisi_hba;
2336 /* Delivery queue structure */
2337 spin_lock_init(&dq->lock);
2338 INIT_LIST_HEAD(&dq->list);
2340 dq->hisi_hba = hisi_hba;
2342 /* Delivery queue */
2343 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2344 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2345 &hisi_hba->cmd_hdr_dma[i],
2347 if (!hisi_hba->cmd_hdr[i])
2350 /* Completion queue */
2351 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2352 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2353 &hisi_hba->complete_hdr_dma[i],
2355 if (!hisi_hba->complete_hdr[i])
2359 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2360 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2362 if (!hisi_hba->itct)
2365 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2366 sizeof(struct hisi_sas_slot),
2368 if (!hisi_hba->slot_info)
2371 /* roundup to avoid overly large block size */
2372 max_command_entries_ru = roundup(max_command_entries, 64);
2373 if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
2374 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
2376 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2377 sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2378 s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
2379 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2380 slots_per_blk = s / sz_slot_buf_ru;
2382 for (i = 0; i < blk_cnt; i++) {
2383 int slot_index = i * slots_per_blk;
2387 buf = dmam_alloc_coherent(dev, s, &buf_dma,
2392 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2393 struct hisi_sas_slot *slot;
2395 slot = &hisi_hba->slot_info[slot_index];
2397 slot->buf_dma = buf_dma;
2398 slot->idx = slot_index;
2400 buf += sz_slot_buf_ru;
2401 buf_dma += sz_slot_buf_ru;
2405 s = max_command_entries * sizeof(struct hisi_sas_iost);
2406 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2408 if (!hisi_hba->iost)
2411 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2412 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2413 &hisi_hba->breakpoint_dma,
2415 if (!hisi_hba->breakpoint)
2418 hisi_hba->slot_index_count = max_command_entries;
2419 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2420 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2421 if (!hisi_hba->slot_index_tags)
2424 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2425 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2426 &hisi_hba->initial_fis_dma,
2428 if (!hisi_hba->initial_fis)
2431 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2432 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2433 &hisi_hba->sata_breakpoint_dma,
2435 if (!hisi_hba->sata_breakpoint)
2438 hisi_sas_slot_index_init(hisi_hba);
2439 hisi_hba->last_slot_index = HISI_SAS_UNRESERVED_IPTT;
2441 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2442 if (!hisi_hba->wq) {
2443 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2451 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2453 void hisi_sas_free(struct hisi_hba *hisi_hba)
2457 for (i = 0; i < hisi_hba->n_phy; i++) {
2458 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
2460 del_timer_sync(&phy->timer);
2464 destroy_workqueue(hisi_hba->wq);
2466 EXPORT_SYMBOL_GPL(hisi_sas_free);
2468 void hisi_sas_rst_work_handler(struct work_struct *work)
2470 struct hisi_hba *hisi_hba =
2471 container_of(work, struct hisi_hba, rst_work);
2473 hisi_sas_controller_reset(hisi_hba);
2475 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2477 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2479 struct hisi_sas_rst *rst =
2480 container_of(work, struct hisi_sas_rst, work);
2482 if (!hisi_sas_controller_reset(rst->hisi_hba))
2484 complete(rst->completion);
2486 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2488 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2490 struct device *dev = hisi_hba->dev;
2491 struct platform_device *pdev = hisi_hba->platform_dev;
2492 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2495 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2497 dev_err(dev, "could not get property sas-addr\n");
2503 * These properties are only required for platform device-based
2504 * controller with DT firmware.
2506 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2507 "hisilicon,sas-syscon");
2508 if (IS_ERR(hisi_hba->ctrl)) {
2509 dev_err(dev, "could not get syscon\n");
2513 if (device_property_read_u32(dev, "ctrl-reset-reg",
2514 &hisi_hba->ctrl_reset_reg)) {
2515 dev_err(dev, "could not get property ctrl-reset-reg\n");
2519 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2520 &hisi_hba->ctrl_reset_sts_reg)) {
2521 dev_err(dev, "could not get property ctrl-reset-sts-reg\n");
2525 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2526 &hisi_hba->ctrl_clock_ena_reg)) {
2527 dev_err(dev, "could not get property ctrl-clock-ena-reg\n");
2532 refclk = devm_clk_get(dev, NULL);
2534 dev_dbg(dev, "no ref clk property\n");
2536 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2538 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2539 dev_err(dev, "could not get property phy-count\n");
2543 if (device_property_read_u32(dev, "queue-count",
2544 &hisi_hba->queue_count)) {
2545 dev_err(dev, "could not get property queue-count\n");
2551 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2553 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2554 const struct hisi_sas_hw *hw)
2556 struct resource *res;
2557 struct Scsi_Host *shost;
2558 struct hisi_hba *hisi_hba;
2559 struct device *dev = &pdev->dev;
2562 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2564 dev_err(dev, "scsi host alloc failed\n");
2567 hisi_hba = shost_priv(shost);
2569 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2571 hisi_hba->dev = dev;
2572 hisi_hba->platform_dev = pdev;
2573 hisi_hba->shost = shost;
2574 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2576 timer_setup(&hisi_hba->timer, NULL, 0);
2578 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2581 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2583 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2586 dev_err(dev, "No usable DMA addressing method\n");
2590 hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0);
2591 if (IS_ERR(hisi_hba->regs))
2594 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2596 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2597 if (IS_ERR(hisi_hba->sgpio_regs))
2601 if (hisi_sas_alloc(hisi_hba)) {
2602 hisi_sas_free(hisi_hba);
2608 scsi_host_put(shost);
2609 dev_err(dev, "shost alloc failed\n");
2613 int hisi_sas_probe(struct platform_device *pdev,
2614 const struct hisi_sas_hw *hw)
2616 struct Scsi_Host *shost;
2617 struct hisi_hba *hisi_hba;
2618 struct device *dev = &pdev->dev;
2619 struct asd_sas_phy **arr_phy;
2620 struct asd_sas_port **arr_port;
2621 struct sas_ha_struct *sha;
2622 int rc, phy_nr, port_nr, i;
2624 shost = hisi_sas_shost_alloc(pdev, hw);
2628 sha = SHOST_TO_SAS_HA(shost);
2629 hisi_hba = shost_priv(shost);
2630 platform_set_drvdata(pdev, sha);
2632 phy_nr = port_nr = hisi_hba->n_phy;
2634 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2635 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2636 if (!arr_phy || !arr_port) {
2641 sha->sas_phy = arr_phy;
2642 sha->sas_port = arr_port;
2643 sha->lldd_ha = hisi_hba;
2645 shost->transportt = hisi_sas_stt;
2646 shost->max_id = HISI_SAS_MAX_DEVICES;
2647 shost->max_lun = ~0;
2648 shost->max_channel = 1;
2649 shost->max_cmd_len = 16;
2650 if (hisi_hba->hw->slot_index_alloc) {
2651 shost->can_queue = HISI_SAS_MAX_COMMANDS;
2652 shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;
2654 shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
2655 shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
2658 sha->sas_ha_name = DRV_NAME;
2659 sha->dev = hisi_hba->dev;
2660 sha->lldd_module = THIS_MODULE;
2661 sha->sas_addr = &hisi_hba->sas_addr[0];
2662 sha->num_phys = hisi_hba->n_phy;
2663 sha->core.shost = hisi_hba->shost;
2665 for (i = 0; i < hisi_hba->n_phy; i++) {
2666 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2667 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2670 rc = scsi_add_host(shost, &pdev->dev);
2674 rc = sas_register_ha(sha);
2676 goto err_out_register_ha;
2678 rc = hisi_hba->hw->hw_init(hisi_hba);
2680 goto err_out_register_ha;
2682 scsi_scan_host(shost);
2686 err_out_register_ha:
2687 scsi_remove_host(shost);
2689 hisi_sas_debugfs_exit(hisi_hba);
2690 hisi_sas_free(hisi_hba);
2691 scsi_host_put(shost);
2694 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2696 struct dentry *hisi_sas_debugfs_dir;
2698 static void hisi_sas_debugfs_snapshot_cq_reg(struct hisi_hba *hisi_hba)
2700 int queue_entry_size = hisi_hba->hw->complete_hdr_size;
2703 for (i = 0; i < hisi_hba->queue_count; i++)
2704 memcpy(hisi_hba->debugfs_complete_hdr[i],
2705 hisi_hba->complete_hdr[i],
2706 HISI_SAS_QUEUE_SLOTS * queue_entry_size);
2709 static void hisi_sas_debugfs_snapshot_dq_reg(struct hisi_hba *hisi_hba)
2711 int queue_entry_size = sizeof(struct hisi_sas_cmd_hdr);
2714 for (i = 0; i < hisi_hba->queue_count; i++) {
2715 struct hisi_sas_cmd_hdr *debugfs_cmd_hdr, *cmd_hdr;
2718 debugfs_cmd_hdr = hisi_hba->debugfs_cmd_hdr[i];
2719 cmd_hdr = hisi_hba->cmd_hdr[i];
2721 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2722 memcpy(&debugfs_cmd_hdr[j], &cmd_hdr[j],
2727 static void hisi_sas_debugfs_snapshot_port_reg(struct hisi_hba *hisi_hba)
2729 const struct hisi_sas_debugfs_reg *port =
2730 hisi_hba->hw->debugfs_reg_port;
2735 for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) {
2736 databuf = (u32 *)hisi_hba->debugfs_port_reg[phy_cnt];
2737 for (i = 0; i < port->count; i++, databuf++) {
2738 offset = port->base_off + 4 * i;
2739 *databuf = port->read_port_reg(hisi_hba, phy_cnt,
2745 static void hisi_sas_debugfs_snapshot_global_reg(struct hisi_hba *hisi_hba)
2747 u32 *databuf = hisi_hba->debugfs_regs[DEBUGFS_GLOBAL];
2748 const struct hisi_sas_hw *hw = hisi_hba->hw;
2749 const struct hisi_sas_debugfs_reg *global =
2750 hw->debugfs_reg_array[DEBUGFS_GLOBAL];
2753 for (i = 0; i < global->count; i++, databuf++)
2754 *databuf = global->read_global_reg(hisi_hba, 4 * i);
2757 static void hisi_sas_debugfs_snapshot_axi_reg(struct hisi_hba *hisi_hba)
2759 u32 *databuf = hisi_hba->debugfs_regs[DEBUGFS_AXI];
2760 const struct hisi_sas_hw *hw = hisi_hba->hw;
2761 const struct hisi_sas_debugfs_reg *axi =
2762 hw->debugfs_reg_array[DEBUGFS_AXI];
2765 for (i = 0; i < axi->count; i++, databuf++)
2766 *databuf = axi->read_global_reg(hisi_hba,
2767 4 * i + axi->base_off);
2770 static void hisi_sas_debugfs_snapshot_ras_reg(struct hisi_hba *hisi_hba)
2772 u32 *databuf = hisi_hba->debugfs_regs[DEBUGFS_RAS];
2773 const struct hisi_sas_hw *hw = hisi_hba->hw;
2774 const struct hisi_sas_debugfs_reg *ras =
2775 hw->debugfs_reg_array[DEBUGFS_RAS];
2778 for (i = 0; i < ras->count; i++, databuf++)
2779 *databuf = ras->read_global_reg(hisi_hba,
2780 4 * i + ras->base_off);
2783 static void hisi_sas_debugfs_snapshot_itct_reg(struct hisi_hba *hisi_hba)
2785 void *cachebuf = hisi_hba->debugfs_itct_cache;
2786 void *databuf = hisi_hba->debugfs_itct;
2787 struct hisi_sas_itct *itct;
2790 hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_ITCT_CACHE,
2793 itct = hisi_hba->itct;
2795 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
2796 memcpy(databuf, itct, sizeof(struct hisi_sas_itct));
2797 databuf += sizeof(struct hisi_sas_itct);
2801 static void hisi_sas_debugfs_snapshot_iost_reg(struct hisi_hba *hisi_hba)
2803 int max_command_entries = HISI_SAS_MAX_COMMANDS;
2804 void *cachebuf = hisi_hba->debugfs_iost_cache;
2805 void *databuf = hisi_hba->debugfs_iost;
2806 struct hisi_sas_iost *iost;
2809 hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_IOST_CACHE,
2812 iost = hisi_hba->iost;
2814 for (i = 0; i < max_command_entries; i++, iost++) {
2815 memcpy(databuf, iost, sizeof(struct hisi_sas_iost));
2816 databuf += sizeof(struct hisi_sas_iost);
2821 hisi_sas_debugfs_to_reg_name(int off, int base_off,
2822 const struct hisi_sas_debugfs_reg_lu *lu)
2824 for (; lu->name; lu++) {
2825 if (off == lu->off - base_off)
2832 static void hisi_sas_debugfs_print_reg(u32 *regs_val, const void *ptr,
2835 const struct hisi_sas_debugfs_reg *reg = ptr;
2838 for (i = 0; i < reg->count; i++) {
2842 name = hisi_sas_debugfs_to_reg_name(off, reg->base_off,
2846 seq_printf(s, "0x%08x 0x%08x %s\n", off,
2849 seq_printf(s, "0x%08x 0x%08x\n", off,
2854 static int hisi_sas_debugfs_global_show(struct seq_file *s, void *p)
2856 struct hisi_hba *hisi_hba = s->private;
2857 const struct hisi_sas_hw *hw = hisi_hba->hw;
2858 const void *reg_global = hw->debugfs_reg_array[DEBUGFS_GLOBAL];
2860 hisi_sas_debugfs_print_reg(hisi_hba->debugfs_regs[DEBUGFS_GLOBAL],
2866 static int hisi_sas_debugfs_global_open(struct inode *inode, struct file *filp)
2868 return single_open(filp, hisi_sas_debugfs_global_show,
2872 static const struct file_operations hisi_sas_debugfs_global_fops = {
2873 .open = hisi_sas_debugfs_global_open,
2875 .llseek = seq_lseek,
2876 .release = single_release,
2877 .owner = THIS_MODULE,
2880 static int hisi_sas_debugfs_axi_show(struct seq_file *s, void *p)
2882 struct hisi_hba *hisi_hba = s->private;
2883 const struct hisi_sas_hw *hw = hisi_hba->hw;
2884 const void *reg_axi = hw->debugfs_reg_array[DEBUGFS_AXI];
2886 hisi_sas_debugfs_print_reg(hisi_hba->debugfs_regs[DEBUGFS_AXI],
2892 static int hisi_sas_debugfs_axi_open(struct inode *inode, struct file *filp)
2894 return single_open(filp, hisi_sas_debugfs_axi_show,
2898 static const struct file_operations hisi_sas_debugfs_axi_fops = {
2899 .open = hisi_sas_debugfs_axi_open,
2901 .llseek = seq_lseek,
2902 .release = single_release,
2903 .owner = THIS_MODULE,
2906 static int hisi_sas_debugfs_ras_show(struct seq_file *s, void *p)
2908 struct hisi_hba *hisi_hba = s->private;
2909 const struct hisi_sas_hw *hw = hisi_hba->hw;
2910 const void *reg_ras = hw->debugfs_reg_array[DEBUGFS_RAS];
2912 hisi_sas_debugfs_print_reg(hisi_hba->debugfs_regs[DEBUGFS_RAS],
2918 static int hisi_sas_debugfs_ras_open(struct inode *inode, struct file *filp)
2920 return single_open(filp, hisi_sas_debugfs_ras_show,
2924 static const struct file_operations hisi_sas_debugfs_ras_fops = {
2925 .open = hisi_sas_debugfs_ras_open,
2927 .llseek = seq_lseek,
2928 .release = single_release,
2929 .owner = THIS_MODULE,
2932 static int hisi_sas_debugfs_port_show(struct seq_file *s, void *p)
2934 struct hisi_sas_phy *phy = s->private;
2935 struct hisi_hba *hisi_hba = phy->hisi_hba;
2936 const struct hisi_sas_hw *hw = hisi_hba->hw;
2937 const struct hisi_sas_debugfs_reg *reg_port = hw->debugfs_reg_port;
2938 u32 *databuf = hisi_hba->debugfs_port_reg[phy->sas_phy.id];
2940 hisi_sas_debugfs_print_reg(databuf, reg_port, s);
2945 static int hisi_sas_debugfs_port_open(struct inode *inode, struct file *filp)
2947 return single_open(filp, hisi_sas_debugfs_port_show, inode->i_private);
2950 static const struct file_operations hisi_sas_debugfs_port_fops = {
2951 .open = hisi_sas_debugfs_port_open,
2953 .llseek = seq_lseek,
2954 .release = single_release,
2955 .owner = THIS_MODULE,
2958 static void hisi_sas_show_row_64(struct seq_file *s, int index,
2959 int sz, __le64 *ptr)
2963 /* completion header size not fixed per HW version */
2964 seq_printf(s, "index %04d:\n\t", index);
2965 for (i = 1; i <= sz / 8; i++, ptr++) {
2966 seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr));
2968 seq_puts(s, "\n\t");
2974 static void hisi_sas_show_row_32(struct seq_file *s, int index,
2975 int sz, __le32 *ptr)
2979 /* completion header size not fixed per HW version */
2980 seq_printf(s, "index %04d:\n\t", index);
2981 for (i = 1; i <= sz / 4; i++, ptr++) {
2982 seq_printf(s, " 0x%08x", le32_to_cpu(*ptr));
2984 seq_puts(s, "\n\t");
2989 static void hisi_sas_cq_show_slot(struct seq_file *s, int slot, void *cq_ptr)
2991 struct hisi_sas_cq *cq = cq_ptr;
2992 struct hisi_hba *hisi_hba = cq->hisi_hba;
2993 void *complete_queue = hisi_hba->debugfs_complete_hdr[cq->id];
2994 __le32 *complete_hdr = complete_queue +
2995 (hisi_hba->hw->complete_hdr_size * slot);
2997 hisi_sas_show_row_32(s, slot,
2998 hisi_hba->hw->complete_hdr_size,
3002 static int hisi_sas_debugfs_cq_show(struct seq_file *s, void *p)
3004 struct hisi_sas_cq *cq = s->private;
3007 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
3008 hisi_sas_cq_show_slot(s, slot, cq);
3013 static int hisi_sas_debugfs_cq_open(struct inode *inode, struct file *filp)
3015 return single_open(filp, hisi_sas_debugfs_cq_show, inode->i_private);
3018 static const struct file_operations hisi_sas_debugfs_cq_fops = {
3019 .open = hisi_sas_debugfs_cq_open,
3021 .llseek = seq_lseek,
3022 .release = single_release,
3023 .owner = THIS_MODULE,
3026 static void hisi_sas_dq_show_slot(struct seq_file *s, int slot, void *dq_ptr)
3028 struct hisi_sas_dq *dq = dq_ptr;
3029 struct hisi_hba *hisi_hba = dq->hisi_hba;
3030 void *cmd_queue = hisi_hba->debugfs_cmd_hdr[dq->id];
3031 __le32 *cmd_hdr = cmd_queue +
3032 sizeof(struct hisi_sas_cmd_hdr) * slot;
3034 hisi_sas_show_row_32(s, slot, sizeof(struct hisi_sas_cmd_hdr), cmd_hdr);
3037 static int hisi_sas_debugfs_dq_show(struct seq_file *s, void *p)
3041 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
3042 hisi_sas_dq_show_slot(s, slot, s->private);
3047 static int hisi_sas_debugfs_dq_open(struct inode *inode, struct file *filp)
3049 return single_open(filp, hisi_sas_debugfs_dq_show, inode->i_private);
3052 static const struct file_operations hisi_sas_debugfs_dq_fops = {
3053 .open = hisi_sas_debugfs_dq_open,
3055 .llseek = seq_lseek,
3056 .release = single_release,
3057 .owner = THIS_MODULE,
3060 static int hisi_sas_debugfs_iost_show(struct seq_file *s, void *p)
3062 struct hisi_hba *hisi_hba = s->private;
3063 struct hisi_sas_iost *debugfs_iost = hisi_hba->debugfs_iost;
3064 int i, max_command_entries = HISI_SAS_MAX_COMMANDS;
3066 for (i = 0; i < max_command_entries; i++, debugfs_iost++) {
3067 __le64 *iost = &debugfs_iost->qw0;
3069 hisi_sas_show_row_64(s, i, sizeof(*debugfs_iost), iost);
3075 static int hisi_sas_debugfs_iost_open(struct inode *inode, struct file *filp)
3077 return single_open(filp, hisi_sas_debugfs_iost_show, inode->i_private);
3080 static const struct file_operations hisi_sas_debugfs_iost_fops = {
3081 .open = hisi_sas_debugfs_iost_open,
3083 .llseek = seq_lseek,
3084 .release = single_release,
3085 .owner = THIS_MODULE,
3088 static int hisi_sas_debugfs_iost_cache_show(struct seq_file *s, void *p)
3090 struct hisi_hba *hisi_hba = s->private;
3091 struct hisi_sas_iost_itct_cache *iost_cache =
3092 (struct hisi_sas_iost_itct_cache *)hisi_hba->debugfs_iost_cache;
3093 u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3097 for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, iost_cache++) {
3099 * Data struct of IOST cache:
3100 * Data[1]: BIT0~15: Table index
3102 * Data[2]~[9]: IOST table
3104 tab_idx = (iost_cache->data[1] & 0xffff);
3105 iost = (__le64 *)iost_cache;
3107 hisi_sas_show_row_64(s, tab_idx, cache_size, iost);
3113 static int hisi_sas_debugfs_iost_cache_open(struct inode *inode,
3116 return single_open(filp, hisi_sas_debugfs_iost_cache_show,
3120 static const struct file_operations hisi_sas_debugfs_iost_cache_fops = {
3121 .open = hisi_sas_debugfs_iost_cache_open,
3123 .llseek = seq_lseek,
3124 .release = single_release,
3125 .owner = THIS_MODULE,
3128 static int hisi_sas_debugfs_itct_show(struct seq_file *s, void *p)
3131 struct hisi_hba *hisi_hba = s->private;
3132 struct hisi_sas_itct *debugfs_itct = hisi_hba->debugfs_itct;
3134 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, debugfs_itct++) {
3135 __le64 *itct = &debugfs_itct->qw0;
3137 hisi_sas_show_row_64(s, i, sizeof(*debugfs_itct), itct);
3143 static int hisi_sas_debugfs_itct_open(struct inode *inode, struct file *filp)
3145 return single_open(filp, hisi_sas_debugfs_itct_show, inode->i_private);
3148 static const struct file_operations hisi_sas_debugfs_itct_fops = {
3149 .open = hisi_sas_debugfs_itct_open,
3151 .llseek = seq_lseek,
3152 .release = single_release,
3153 .owner = THIS_MODULE,
3156 static int hisi_sas_debugfs_itct_cache_show(struct seq_file *s, void *p)
3158 struct hisi_hba *hisi_hba = s->private;
3159 struct hisi_sas_iost_itct_cache *itct_cache =
3160 (struct hisi_sas_iost_itct_cache *)hisi_hba->debugfs_itct_cache;
3161 u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3165 for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, itct_cache++) {
3167 * Data struct of ITCT cache:
3168 * Data[1]: BIT0~15: Table index
3170 * Data[2]~[9]: ITCT table
3172 tab_idx = itct_cache->data[1] & 0xffff;
3173 itct = (__le64 *)itct_cache;
3175 hisi_sas_show_row_64(s, tab_idx, cache_size, itct);
3181 static int hisi_sas_debugfs_itct_cache_open(struct inode *inode,
3184 return single_open(filp, hisi_sas_debugfs_itct_cache_show,
3188 static const struct file_operations hisi_sas_debugfs_itct_cache_fops = {
3189 .open = hisi_sas_debugfs_itct_cache_open,
3191 .llseek = seq_lseek,
3192 .release = single_release,
3193 .owner = THIS_MODULE,
3196 static void hisi_sas_debugfs_create_files(struct hisi_hba *hisi_hba)
3198 struct dentry *dump_dentry;
3199 struct dentry *dentry;
3205 /* Create dump dir inside device dir */
3206 dump_dentry = debugfs_create_dir("dump", hisi_hba->debugfs_dir);
3207 hisi_hba->debugfs_dump_dentry = dump_dentry;
3209 debugfs_create_file("global", 0400, dump_dentry, hisi_hba,
3210 &hisi_sas_debugfs_global_fops);
3212 /* Create port dir and files */
3213 dentry = debugfs_create_dir("port", dump_dentry);
3214 for (p = 0; p < hisi_hba->n_phy; p++) {
3215 snprintf(name, 256, "%d", p);
3217 debugfs_create_file(name, 0400, dentry, &hisi_hba->phy[p],
3218 &hisi_sas_debugfs_port_fops);
3221 /* Create CQ dir and files */
3222 dentry = debugfs_create_dir("cq", dump_dentry);
3223 for (c = 0; c < hisi_hba->queue_count; c++) {
3224 snprintf(name, 256, "%d", c);
3226 debugfs_create_file(name, 0400, dentry, &hisi_hba->cq[c],
3227 &hisi_sas_debugfs_cq_fops);
3230 /* Create DQ dir and files */
3231 dentry = debugfs_create_dir("dq", dump_dentry);
3232 for (d = 0; d < hisi_hba->queue_count; d++) {
3233 snprintf(name, 256, "%d", d);
3235 debugfs_create_file(name, 0400, dentry, &hisi_hba->dq[d],
3236 &hisi_sas_debugfs_dq_fops);
3239 debugfs_create_file("iost", 0400, dump_dentry, hisi_hba,
3240 &hisi_sas_debugfs_iost_fops);
3242 debugfs_create_file("iost_cache", 0400, dump_dentry, hisi_hba,
3243 &hisi_sas_debugfs_iost_cache_fops);
3245 debugfs_create_file("itct", 0400, dump_dentry, hisi_hba,
3246 &hisi_sas_debugfs_itct_fops);
3248 debugfs_create_file("itct_cache", 0400, dump_dentry, hisi_hba,
3249 &hisi_sas_debugfs_itct_cache_fops);
3251 debugfs_create_file("axi", 0400, dump_dentry, hisi_hba,
3252 &hisi_sas_debugfs_axi_fops);
3254 debugfs_create_file("ras", 0400, dump_dentry, hisi_hba,
3255 &hisi_sas_debugfs_ras_fops);
3260 static void hisi_sas_debugfs_snapshot_regs(struct hisi_hba *hisi_hba)
3262 hisi_hba->hw->snapshot_prepare(hisi_hba);
3264 hisi_sas_debugfs_snapshot_global_reg(hisi_hba);
3265 hisi_sas_debugfs_snapshot_port_reg(hisi_hba);
3266 hisi_sas_debugfs_snapshot_axi_reg(hisi_hba);
3267 hisi_sas_debugfs_snapshot_ras_reg(hisi_hba);
3268 hisi_sas_debugfs_snapshot_cq_reg(hisi_hba);
3269 hisi_sas_debugfs_snapshot_dq_reg(hisi_hba);
3270 hisi_sas_debugfs_snapshot_itct_reg(hisi_hba);
3271 hisi_sas_debugfs_snapshot_iost_reg(hisi_hba);
3273 hisi_sas_debugfs_create_files(hisi_hba);
3275 hisi_hba->hw->snapshot_restore(hisi_hba);
3278 static ssize_t hisi_sas_debugfs_trigger_dump_write(struct file *file,
3279 const char __user *user_buf,
3280 size_t count, loff_t *ppos)
3282 struct hisi_hba *hisi_hba = file->f_inode->i_private;
3285 /* A bit racy, but don't care too much since it's only debugfs */
3286 if (hisi_hba->debugfs_snapshot)
3292 if (copy_from_user(buf, user_buf, count))
3298 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
3303 static const struct file_operations hisi_sas_debugfs_trigger_dump_fops = {
3304 .write = &hisi_sas_debugfs_trigger_dump_write,
3305 .owner = THIS_MODULE,
3309 HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL = 0,
3310 HISI_SAS_BIST_LOOPBACK_MODE_SERDES,
3311 HISI_SAS_BIST_LOOPBACK_MODE_REMOTE,
3315 HISI_SAS_BIST_CODE_MODE_PRBS7 = 0,
3316 HISI_SAS_BIST_CODE_MODE_PRBS23,
3317 HISI_SAS_BIST_CODE_MODE_PRBS31,
3318 HISI_SAS_BIST_CODE_MODE_JTPAT,
3319 HISI_SAS_BIST_CODE_MODE_CJTPAT,
3320 HISI_SAS_BIST_CODE_MODE_SCRAMBED_0,
3321 HISI_SAS_BIST_CODE_MODE_TRAIN,
3322 HISI_SAS_BIST_CODE_MODE_TRAIN_DONE,
3323 HISI_SAS_BIST_CODE_MODE_HFTP,
3324 HISI_SAS_BIST_CODE_MODE_MFTP,
3325 HISI_SAS_BIST_CODE_MODE_LFTP,
3326 HISI_SAS_BIST_CODE_MODE_FIXED_DATA,
3329 static const struct {
3332 } hisi_sas_debugfs_loop_linkrate[] = {
3333 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
3334 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
3335 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
3336 { SAS_LINK_RATE_12_0_GBPS, "12.0 Gbit" },
3339 static int hisi_sas_debugfs_bist_linkrate_show(struct seq_file *s, void *p)
3341 struct hisi_hba *hisi_hba = s->private;
3344 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
3345 int match = (hisi_hba->debugfs_bist_linkrate ==
3346 hisi_sas_debugfs_loop_linkrate[i].value);
3348 seq_printf(s, "%s%s%s ", match ? "[" : "",
3349 hisi_sas_debugfs_loop_linkrate[i].name,
3357 static ssize_t hisi_sas_debugfs_bist_linkrate_write(struct file *filp,
3358 const char __user *buf,
3359 size_t count, loff_t *ppos)
3361 struct seq_file *m = filp->private_data;
3362 struct hisi_hba *hisi_hba = m->private;
3363 char kbuf[16] = {}, *pkbuf;
3367 if (hisi_hba->debugfs_bist_enable)
3370 if (count >= sizeof(kbuf))
3373 if (copy_from_user(kbuf, buf, count))
3376 pkbuf = strstrip(kbuf);
3378 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
3379 if (!strncmp(hisi_sas_debugfs_loop_linkrate[i].name,
3381 hisi_hba->debugfs_bist_linkrate =
3382 hisi_sas_debugfs_loop_linkrate[i].value;
3394 static int hisi_sas_debugfs_bist_linkrate_open(struct inode *inode,
3397 return single_open(filp, hisi_sas_debugfs_bist_linkrate_show,
3401 static const struct file_operations hisi_sas_debugfs_bist_linkrate_ops = {
3402 .open = hisi_sas_debugfs_bist_linkrate_open,
3404 .write = hisi_sas_debugfs_bist_linkrate_write,
3405 .llseek = seq_lseek,
3406 .release = single_release,
3407 .owner = THIS_MODULE,
3410 static const struct {
3413 } hisi_sas_debugfs_loop_code_mode[] = {
3414 { HISI_SAS_BIST_CODE_MODE_PRBS7, "PRBS7" },
3415 { HISI_SAS_BIST_CODE_MODE_PRBS23, "PRBS23" },
3416 { HISI_SAS_BIST_CODE_MODE_PRBS31, "PRBS31" },
3417 { HISI_SAS_BIST_CODE_MODE_JTPAT, "JTPAT" },
3418 { HISI_SAS_BIST_CODE_MODE_CJTPAT, "CJTPAT" },
3419 { HISI_SAS_BIST_CODE_MODE_SCRAMBED_0, "SCRAMBED_0" },
3420 { HISI_SAS_BIST_CODE_MODE_TRAIN, "TRAIN" },
3421 { HISI_SAS_BIST_CODE_MODE_TRAIN_DONE, "TRAIN_DONE" },
3422 { HISI_SAS_BIST_CODE_MODE_HFTP, "HFTP" },
3423 { HISI_SAS_BIST_CODE_MODE_MFTP, "MFTP" },
3424 { HISI_SAS_BIST_CODE_MODE_LFTP, "LFTP" },
3425 { HISI_SAS_BIST_CODE_MODE_FIXED_DATA, "FIXED_DATA" },
3428 static int hisi_sas_debugfs_bist_code_mode_show(struct seq_file *s, void *p)
3430 struct hisi_hba *hisi_hba = s->private;
3433 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
3434 int match = (hisi_hba->debugfs_bist_code_mode ==
3435 hisi_sas_debugfs_loop_code_mode[i].value);
3437 seq_printf(s, "%s%s%s ", match ? "[" : "",
3438 hisi_sas_debugfs_loop_code_mode[i].name,
3446 static ssize_t hisi_sas_debugfs_bist_code_mode_write(struct file *filp,
3447 const char __user *buf,
3451 struct seq_file *m = filp->private_data;
3452 struct hisi_hba *hisi_hba = m->private;
3453 char kbuf[16] = {}, *pkbuf;
3457 if (hisi_hba->debugfs_bist_enable)
3460 if (count >= sizeof(kbuf))
3463 if (copy_from_user(kbuf, buf, count))
3466 pkbuf = strstrip(kbuf);
3468 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
3469 if (!strncmp(hisi_sas_debugfs_loop_code_mode[i].name,
3471 hisi_hba->debugfs_bist_code_mode =
3472 hisi_sas_debugfs_loop_code_mode[i].value;
3484 static int hisi_sas_debugfs_bist_code_mode_open(struct inode *inode,
3487 return single_open(filp, hisi_sas_debugfs_bist_code_mode_show,
3491 static const struct file_operations hisi_sas_debugfs_bist_code_mode_ops = {
3492 .open = hisi_sas_debugfs_bist_code_mode_open,
3494 .write = hisi_sas_debugfs_bist_code_mode_write,
3495 .llseek = seq_lseek,
3496 .release = single_release,
3497 .owner = THIS_MODULE,
3500 static ssize_t hisi_sas_debugfs_bist_phy_write(struct file *filp,
3501 const char __user *buf,
3502 size_t count, loff_t *ppos)
3504 struct seq_file *m = filp->private_data;
3505 struct hisi_hba *hisi_hba = m->private;
3506 unsigned int phy_no;
3509 if (hisi_hba->debugfs_bist_enable)
3512 val = kstrtouint_from_user(buf, count, 0, &phy_no);
3516 if (phy_no >= hisi_hba->n_phy)
3519 hisi_hba->debugfs_bist_phy_no = phy_no;
3524 static int hisi_sas_debugfs_bist_phy_show(struct seq_file *s, void *p)
3526 struct hisi_hba *hisi_hba = s->private;
3528 seq_printf(s, "%d\n", hisi_hba->debugfs_bist_phy_no);
3533 static int hisi_sas_debugfs_bist_phy_open(struct inode *inode,
3536 return single_open(filp, hisi_sas_debugfs_bist_phy_show,
3540 static const struct file_operations hisi_sas_debugfs_bist_phy_ops = {
3541 .open = hisi_sas_debugfs_bist_phy_open,
3543 .write = hisi_sas_debugfs_bist_phy_write,
3544 .llseek = seq_lseek,
3545 .release = single_release,
3546 .owner = THIS_MODULE,
3549 static const struct {
3552 } hisi_sas_debugfs_loop_modes[] = {
3553 { HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL, "digial" },
3554 { HISI_SAS_BIST_LOOPBACK_MODE_SERDES, "serdes" },
3555 { HISI_SAS_BIST_LOOPBACK_MODE_REMOTE, "remote" },
3558 static int hisi_sas_debugfs_bist_mode_show(struct seq_file *s, void *p)
3560 struct hisi_hba *hisi_hba = s->private;
3563 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
3564 int match = (hisi_hba->debugfs_bist_mode ==
3565 hisi_sas_debugfs_loop_modes[i].value);
3567 seq_printf(s, "%s%s%s ", match ? "[" : "",
3568 hisi_sas_debugfs_loop_modes[i].name,
3576 static ssize_t hisi_sas_debugfs_bist_mode_write(struct file *filp,
3577 const char __user *buf,
3578 size_t count, loff_t *ppos)
3580 struct seq_file *m = filp->private_data;
3581 struct hisi_hba *hisi_hba = m->private;
3582 char kbuf[16] = {}, *pkbuf;
3586 if (hisi_hba->debugfs_bist_enable)
3589 if (count >= sizeof(kbuf))
3592 if (copy_from_user(kbuf, buf, count))
3595 pkbuf = strstrip(kbuf);
3597 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
3598 if (!strncmp(hisi_sas_debugfs_loop_modes[i].name, pkbuf, 16)) {
3599 hisi_hba->debugfs_bist_mode =
3600 hisi_sas_debugfs_loop_modes[i].value;
3612 static int hisi_sas_debugfs_bist_mode_open(struct inode *inode,
3615 return single_open(filp, hisi_sas_debugfs_bist_mode_show,
3619 static const struct file_operations hisi_sas_debugfs_bist_mode_ops = {
3620 .open = hisi_sas_debugfs_bist_mode_open,
3622 .write = hisi_sas_debugfs_bist_mode_write,
3623 .llseek = seq_lseek,
3624 .release = single_release,
3625 .owner = THIS_MODULE,
3628 static ssize_t hisi_sas_debugfs_bist_enable_write(struct file *filp,
3629 const char __user *buf,
3630 size_t count, loff_t *ppos)
3632 struct seq_file *m = filp->private_data;
3633 struct hisi_hba *hisi_hba = m->private;
3634 unsigned int enable;
3637 val = kstrtouint_from_user(buf, count, 0, &enable);
3644 if (enable == hisi_hba->debugfs_bist_enable)
3647 if (!hisi_hba->hw->set_bist)
3650 val = hisi_hba->hw->set_bist(hisi_hba, enable);
3654 hisi_hba->debugfs_bist_enable = enable;
3659 static int hisi_sas_debugfs_bist_enable_show(struct seq_file *s, void *p)
3661 struct hisi_hba *hisi_hba = s->private;
3663 seq_printf(s, "%d\n", hisi_hba->debugfs_bist_enable);
3668 static int hisi_sas_debugfs_bist_enable_open(struct inode *inode,
3671 return single_open(filp, hisi_sas_debugfs_bist_enable_show,
3675 static const struct file_operations hisi_sas_debugfs_bist_enable_ops = {
3676 .open = hisi_sas_debugfs_bist_enable_open,
3678 .write = hisi_sas_debugfs_bist_enable_write,
3679 .llseek = seq_lseek,
3680 .release = single_release,
3681 .owner = THIS_MODULE,
3684 void hisi_sas_debugfs_work_handler(struct work_struct *work)
3686 struct hisi_hba *hisi_hba =
3687 container_of(work, struct hisi_hba, debugfs_work);
3689 if (hisi_hba->debugfs_snapshot)
3691 hisi_hba->debugfs_snapshot = true;
3693 hisi_sas_debugfs_snapshot_regs(hisi_hba);
3695 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_work_handler);
3697 static void hisi_sas_debugfs_release(struct hisi_hba *hisi_hba)
3699 struct device *dev = hisi_hba->dev;
3702 devm_kfree(dev, hisi_hba->debugfs_iost_cache);
3703 devm_kfree(dev, hisi_hba->debugfs_itct_cache);
3704 devm_kfree(dev, hisi_hba->debugfs_iost);
3706 for (i = 0; i < hisi_hba->queue_count; i++)
3707 devm_kfree(dev, hisi_hba->debugfs_cmd_hdr[i]);
3709 for (i = 0; i < hisi_hba->queue_count; i++)
3710 devm_kfree(dev, hisi_hba->debugfs_complete_hdr[i]);
3712 for (i = 0; i < DEBUGFS_REGS_NUM; i++)
3713 devm_kfree(dev, hisi_hba->debugfs_regs[i]);
3715 for (i = 0; i < hisi_hba->n_phy; i++)
3716 devm_kfree(dev, hisi_hba->debugfs_port_reg[i]);
3719 static int hisi_sas_debugfs_alloc(struct hisi_hba *hisi_hba)
3721 const struct hisi_sas_hw *hw = hisi_hba->hw;
3722 struct device *dev = hisi_hba->dev;
3726 sz = hw->debugfs_reg_array[DEBUGFS_GLOBAL]->count * 4;
3727 hisi_hba->debugfs_regs[DEBUGFS_GLOBAL] =
3728 devm_kmalloc(dev, sz, GFP_KERNEL);
3730 if (!hisi_hba->debugfs_regs[DEBUGFS_GLOBAL])
3733 sz = hw->debugfs_reg_port->count * 4;
3734 for (p = 0; p < hisi_hba->n_phy; p++) {
3735 hisi_hba->debugfs_port_reg[p] =
3736 devm_kmalloc(dev, sz, GFP_KERNEL);
3738 if (!hisi_hba->debugfs_port_reg[p])
3742 sz = hw->debugfs_reg_array[DEBUGFS_AXI]->count * 4;
3743 hisi_hba->debugfs_regs[DEBUGFS_AXI] =
3744 devm_kmalloc(dev, sz, GFP_KERNEL);
3746 if (!hisi_hba->debugfs_regs[DEBUGFS_AXI])
3749 sz = hw->debugfs_reg_array[DEBUGFS_RAS]->count * 4;
3750 hisi_hba->debugfs_regs[DEBUGFS_RAS] =
3751 devm_kmalloc(dev, sz, GFP_KERNEL);
3753 if (!hisi_hba->debugfs_regs[DEBUGFS_RAS])
3756 sz = hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
3757 for (c = 0; c < hisi_hba->queue_count; c++) {
3758 hisi_hba->debugfs_complete_hdr[c] =
3759 devm_kmalloc(dev, sz, GFP_KERNEL);
3761 if (!hisi_hba->debugfs_complete_hdr[c])
3765 sz = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
3766 for (d = 0; d < hisi_hba->queue_count; d++) {
3767 hisi_hba->debugfs_cmd_hdr[d] =
3768 devm_kmalloc(dev, sz, GFP_KERNEL);
3770 if (!hisi_hba->debugfs_cmd_hdr[d])
3774 sz = HISI_SAS_MAX_COMMANDS * sizeof(struct hisi_sas_iost);
3776 hisi_hba->debugfs_iost = devm_kmalloc(dev, sz, GFP_KERNEL);
3777 if (!hisi_hba->debugfs_iost)
3780 sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
3781 sizeof(struct hisi_sas_iost_itct_cache);
3783 hisi_hba->debugfs_iost_cache = devm_kmalloc(dev, sz, GFP_KERNEL);
3784 if (!hisi_hba->debugfs_iost_cache)
3787 sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
3788 sizeof(struct hisi_sas_iost_itct_cache);
3790 hisi_hba->debugfs_itct_cache = devm_kmalloc(dev, sz, GFP_KERNEL);
3791 if (!hisi_hba->debugfs_itct_cache)
3794 /* New memory allocation must be locate before itct */
3795 sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
3797 hisi_hba->debugfs_itct = devm_kmalloc(dev, sz, GFP_KERNEL);
3798 if (!hisi_hba->debugfs_itct)
3803 hisi_sas_debugfs_release(hisi_hba);
3807 static void hisi_sas_debugfs_bist_init(struct hisi_hba *hisi_hba)
3809 hisi_hba->debugfs_bist_dentry =
3810 debugfs_create_dir("bist", hisi_hba->debugfs_dir);
3811 debugfs_create_file("link_rate", 0600,
3812 hisi_hba->debugfs_bist_dentry, hisi_hba,
3813 &hisi_sas_debugfs_bist_linkrate_ops);
3815 debugfs_create_file("code_mode", 0600,
3816 hisi_hba->debugfs_bist_dentry, hisi_hba,
3817 &hisi_sas_debugfs_bist_code_mode_ops);
3819 debugfs_create_file("phy_id", 0600, hisi_hba->debugfs_bist_dentry,
3820 hisi_hba, &hisi_sas_debugfs_bist_phy_ops);
3822 debugfs_create_u32("cnt", 0600, hisi_hba->debugfs_bist_dentry,
3823 &hisi_hba->debugfs_bist_cnt);
3825 debugfs_create_file("loopback_mode", 0600,
3826 hisi_hba->debugfs_bist_dentry,
3827 hisi_hba, &hisi_sas_debugfs_bist_mode_ops);
3829 debugfs_create_file("enable", 0600, hisi_hba->debugfs_bist_dentry,
3830 hisi_hba, &hisi_sas_debugfs_bist_enable_ops);
3832 hisi_hba->debugfs_bist_linkrate = SAS_LINK_RATE_1_5_GBPS;
3835 void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba)
3837 struct device *dev = hisi_hba->dev;
3839 hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev),
3840 hisi_sas_debugfs_dir);
3841 debugfs_create_file("trigger_dump", 0600,
3842 hisi_hba->debugfs_dir,
3844 &hisi_sas_debugfs_trigger_dump_fops);
3846 /* create bist structures */
3847 hisi_sas_debugfs_bist_init(hisi_hba);
3849 if (hisi_sas_debugfs_alloc(hisi_hba)) {
3850 debugfs_remove_recursive(hisi_hba->debugfs_dir);
3851 dev_dbg(dev, "failed to init debugfs!\n");
3854 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_init);
3856 void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba)
3858 debugfs_remove_recursive(hisi_hba->debugfs_dir);
3860 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_exit);
3862 int hisi_sas_remove(struct platform_device *pdev)
3864 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
3865 struct hisi_hba *hisi_hba = sha->lldd_ha;
3866 struct Scsi_Host *shost = sha->core.shost;
3868 if (timer_pending(&hisi_hba->timer))
3869 del_timer(&hisi_hba->timer);
3871 sas_unregister_ha(sha);
3872 sas_remove_host(sha->core.shost);
3874 hisi_sas_free(hisi_hba);
3875 scsi_host_put(shost);
3878 EXPORT_SYMBOL_GPL(hisi_sas_remove);
3880 bool hisi_sas_debugfs_enable;
3881 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
3882 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
3883 MODULE_PARM_DESC(hisi_sas_debugfs_enable, "Enable driver debugfs (default disabled)");
3885 static __init int hisi_sas_init(void)
3887 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
3891 if (hisi_sas_debugfs_enable)
3892 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
3897 static __exit void hisi_sas_exit(void)
3899 sas_release_transport(hisi_sas_stt);
3901 debugfs_remove(hisi_sas_debugfs_dir);
3904 module_init(hisi_sas_init);
3905 module_exit(hisi_sas_exit);
3907 MODULE_LICENSE("GPL");
3908 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
3909 MODULE_DESCRIPTION("HISILICON SAS controller driver");
3910 MODULE_ALIAS("platform:" DRV_NAME);