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)
166 if (hisi_hba->hw->slot_index_alloc ||
167 slot_idx >= HISI_SAS_UNRESERVED_IPTT) {
168 spin_lock(&hisi_hba->lock);
169 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
170 spin_unlock(&hisi_hba->lock);
174 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
176 void *bitmap = hisi_hba->slot_index_tags;
178 set_bit(slot_idx, bitmap);
181 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba,
182 struct scsi_cmnd *scsi_cmnd)
185 void *bitmap = hisi_hba->slot_index_tags;
188 return scsi_cmnd->request->tag;
190 spin_lock(&hisi_hba->lock);
191 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
192 hisi_hba->last_slot_index + 1);
193 if (index >= hisi_hba->slot_index_count) {
194 index = find_next_zero_bit(bitmap,
195 hisi_hba->slot_index_count,
196 HISI_SAS_UNRESERVED_IPTT);
197 if (index >= hisi_hba->slot_index_count) {
198 spin_unlock(&hisi_hba->lock);
199 return -SAS_QUEUE_FULL;
202 hisi_sas_slot_index_set(hisi_hba, index);
203 hisi_hba->last_slot_index = index;
204 spin_unlock(&hisi_hba->lock);
209 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
213 for (i = 0; i < hisi_hba->slot_index_count; ++i)
214 hisi_sas_slot_index_clear(hisi_hba, i);
217 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
218 struct hisi_sas_slot *slot)
220 int device_id = slot->device_id;
221 struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id];
224 struct device *dev = hisi_hba->dev;
226 if (!task->lldd_task)
229 task->lldd_task = NULL;
231 if (!sas_protocol_ata(task->task_proto)) {
233 dma_unmap_sg(dev, task->scatter,
236 if (slot->n_elem_dif) {
237 struct sas_ssp_task *ssp_task = &task->ssp_task;
238 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
240 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
241 scsi_prot_sg_count(scsi_cmnd),
247 spin_lock(&sas_dev->lock);
248 list_del_init(&slot->entry);
249 spin_unlock(&sas_dev->lock);
251 memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
253 hisi_sas_slot_index_free(hisi_hba, slot->idx);
255 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
257 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
258 struct hisi_sas_slot *slot)
260 hisi_hba->hw->prep_smp(hisi_hba, slot);
263 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
264 struct hisi_sas_slot *slot)
266 hisi_hba->hw->prep_ssp(hisi_hba, slot);
269 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
270 struct hisi_sas_slot *slot)
272 hisi_hba->hw->prep_stp(hisi_hba, slot);
275 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
276 struct hisi_sas_slot *slot,
277 int device_id, int abort_flag, int tag_to_abort)
279 hisi_hba->hw->prep_abort(hisi_hba, slot,
280 device_id, abort_flag, tag_to_abort);
283 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
284 struct sas_task *task, int n_elem,
287 struct device *dev = hisi_hba->dev;
289 if (!sas_protocol_ata(task->task_proto)) {
290 if (task->num_scatter) {
292 dma_unmap_sg(dev, task->scatter,
295 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
297 dma_unmap_sg(dev, &task->smp_task.smp_req,
303 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
304 struct sas_task *task, int *n_elem,
307 struct device *dev = hisi_hba->dev;
310 if (sas_protocol_ata(task->task_proto)) {
311 *n_elem = task->num_scatter;
313 unsigned int req_len;
315 if (task->num_scatter) {
316 *n_elem = dma_map_sg(dev, task->scatter,
317 task->num_scatter, task->data_dir);
322 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
323 *n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
329 req_len = sg_dma_len(&task->smp_task.smp_req);
332 goto err_out_dma_unmap;
337 if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
338 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT\n",
341 goto err_out_dma_unmap;
346 /* It would be better to call dma_unmap_sg() here, but it's messy */
347 hisi_sas_dma_unmap(hisi_hba, task, *n_elem,
353 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
354 struct sas_task *task, int n_elem_dif)
356 struct device *dev = hisi_hba->dev;
359 struct sas_ssp_task *ssp_task = &task->ssp_task;
360 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
362 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
363 scsi_prot_sg_count(scsi_cmnd),
368 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
369 int *n_elem_dif, struct sas_task *task)
371 struct device *dev = hisi_hba->dev;
372 struct sas_ssp_task *ssp_task;
373 struct scsi_cmnd *scsi_cmnd;
376 if (task->num_scatter) {
377 ssp_task = &task->ssp_task;
378 scsi_cmnd = ssp_task->cmd;
380 if (scsi_prot_sg_count(scsi_cmnd)) {
381 *n_elem_dif = dma_map_sg(dev,
382 scsi_prot_sglist(scsi_cmnd),
383 scsi_prot_sg_count(scsi_cmnd),
389 if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
390 dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
393 goto err_out_dif_dma_unmap;
400 err_out_dif_dma_unmap:
401 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
402 scsi_prot_sg_count(scsi_cmnd), task->data_dir);
406 static int hisi_sas_task_prep(struct sas_task *task,
407 struct hisi_sas_dq **dq_pointer,
408 bool is_tmf, struct hisi_sas_tmf_task *tmf,
411 struct domain_device *device = task->dev;
412 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
413 struct hisi_sas_device *sas_dev = device->lldd_dev;
414 struct hisi_sas_port *port;
415 struct hisi_sas_slot *slot;
416 struct hisi_sas_cmd_hdr *cmd_hdr_base;
417 struct asd_sas_port *sas_port = device->port;
418 struct device *dev = hisi_hba->dev;
419 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
420 int n_elem = 0, n_elem_dif = 0, n_elem_req = 0;
421 struct scsi_cmnd *scmd = NULL;
422 struct hisi_sas_dq *dq;
426 if (DEV_IS_GONE(sas_dev)) {
428 dev_info(dev, "task prep: device %d not ready\n",
431 dev_info(dev, "task prep: device %016llx not ready\n",
432 SAS_ADDR(device->sas_addr));
437 if (task->uldd_task) {
438 struct ata_queued_cmd *qc;
440 if (dev_is_sata(device)) {
441 qc = task->uldd_task;
444 scmd = task->uldd_task;
449 unsigned int dq_index;
452 blk_tag = blk_mq_unique_tag(scmd->request);
453 dq_index = blk_mq_unique_tag_to_hwq(blk_tag);
454 *dq_pointer = dq = &hisi_hba->dq[dq_index];
456 *dq_pointer = dq = sas_dev->dq;
459 port = to_hisi_sas_port(sas_port);
460 if (port && !port->port_attached) {
461 dev_info(dev, "task prep: %s port%d not attach device\n",
462 (dev_is_sata(device)) ?
469 rc = hisi_sas_dma_map(hisi_hba, task, &n_elem,
474 if (!sas_protocol_ata(task->task_proto)) {
475 rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
477 goto err_out_dma_unmap;
480 if (hisi_hba->hw->slot_index_alloc)
481 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
483 rc = hisi_sas_slot_index_alloc(hisi_hba, scmd);
486 goto err_out_dif_dma_unmap;
489 slot = &hisi_hba->slot_info[slot_idx];
491 spin_lock(&dq->lock);
492 wr_q_index = dq->wr_point;
493 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
494 list_add_tail(&slot->delivery, &dq->list);
495 spin_unlock(&dq->lock);
496 spin_lock(&sas_dev->lock);
497 list_add_tail(&slot->entry, &sas_dev->list);
498 spin_unlock(&sas_dev->lock);
500 dlvry_queue = dq->id;
501 dlvry_queue_slot = wr_q_index;
503 slot->device_id = sas_dev->device_id;
504 slot->n_elem = n_elem;
505 slot->n_elem_dif = n_elem_dif;
506 slot->dlvry_queue = dlvry_queue;
507 slot->dlvry_queue_slot = dlvry_queue_slot;
508 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
509 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
513 slot->is_internal = is_tmf;
514 task->lldd_task = slot;
516 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
517 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
518 memset(hisi_sas_status_buf_addr_mem(slot), 0,
519 sizeof(struct hisi_sas_err_record));
521 switch (task->task_proto) {
522 case SAS_PROTOCOL_SMP:
523 hisi_sas_task_prep_smp(hisi_hba, slot);
525 case SAS_PROTOCOL_SSP:
526 hisi_sas_task_prep_ssp(hisi_hba, slot);
528 case SAS_PROTOCOL_SATA:
529 case SAS_PROTOCOL_STP:
530 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
531 hisi_sas_task_prep_ata(hisi_hba, slot);
534 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
539 spin_lock_irqsave(&task->task_state_lock, flags);
540 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
541 spin_unlock_irqrestore(&task->task_state_lock, flags);
544 WRITE_ONCE(slot->ready, 1);
548 err_out_dif_dma_unmap:
549 if (!sas_protocol_ata(task->task_proto))
550 hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
552 hisi_sas_dma_unmap(hisi_hba, task, n_elem,
555 dev_err(dev, "task prep: failed[%d]!\n", rc);
559 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
560 bool is_tmf, struct hisi_sas_tmf_task *tmf)
564 struct hisi_hba *hisi_hba;
566 struct domain_device *device = task->dev;
567 struct asd_sas_port *sas_port = device->port;
568 struct hisi_sas_dq *dq = NULL;
571 struct task_status_struct *ts = &task->task_status;
573 ts->resp = SAS_TASK_UNDELIVERED;
574 ts->stat = SAS_PHY_DOWN;
576 * libsas will use dev->port, should
577 * not call task_done for sata
579 if (device->dev_type != SAS_SATA_DEV)
580 task->task_done(task);
584 hisi_hba = dev_to_hisi_hba(device);
587 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
589 * For IOs from upper layer, it may already disable preempt
590 * in the IO path, if disable preempt again in down(),
591 * function schedule() will report schedule_bug(), so check
592 * preemptible() before goto down().
597 down(&hisi_hba->sem);
601 /* protect task_prep and start_delivery sequence */
602 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
604 dev_err(dev, "task exec: failed[%d]!\n", rc);
607 spin_lock(&dq->lock);
608 hisi_hba->hw->start_delivery(dq);
609 spin_unlock(&dq->lock);
615 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
617 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
618 struct asd_sas_phy *sas_phy = &phy->sas_phy;
619 struct sas_ha_struct *sas_ha;
621 if (!phy->phy_attached)
624 if (test_bit(HISI_SAS_PM_BIT, &hisi_hba->flags) &&
625 !sas_phy->suspended) {
626 dev_warn(hisi_hba->dev, "phy%d during suspend filtered out\n", phy_no);
630 sas_ha = &hisi_hba->sha;
631 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
634 struct sas_phy *sphy = sas_phy->phy;
636 sphy->negotiated_linkrate = sas_phy->linkrate;
637 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
638 sphy->maximum_linkrate_hw =
639 hisi_hba->hw->phy_get_max_linkrate();
640 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
641 sphy->minimum_linkrate = phy->minimum_linkrate;
643 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
644 sphy->maximum_linkrate = phy->maximum_linkrate;
647 if (phy->phy_type & PORT_TYPE_SAS) {
648 struct sas_identify_frame *id;
650 id = (struct sas_identify_frame *)phy->frame_rcvd;
651 id->dev_type = phy->identify.device_type;
652 id->initiator_bits = SAS_PROTOCOL_ALL;
653 id->target_bits = phy->identify.target_port_protocols;
654 } else if (phy->phy_type & PORT_TYPE_SATA) {
658 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
659 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
662 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
664 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
665 struct hisi_sas_device *sas_dev = NULL;
666 int last = hisi_hba->last_dev_id;
667 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
670 spin_lock(&hisi_hba->lock);
671 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
672 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
673 int queue = i % hisi_hba->queue_count;
674 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
676 hisi_hba->devices[i].device_id = i;
677 sas_dev = &hisi_hba->devices[i];
678 sas_dev->dev_status = HISI_SAS_DEV_INIT;
679 sas_dev->dev_type = device->dev_type;
680 sas_dev->hisi_hba = hisi_hba;
681 sas_dev->sas_device = device;
683 spin_lock_init(&sas_dev->lock);
684 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
689 hisi_hba->last_dev_id = i;
690 spin_unlock(&hisi_hba->lock);
695 #define HISI_SAS_DISK_RECOVER_CNT 3
696 static int hisi_sas_init_device(struct domain_device *device)
698 int rc = TMF_RESP_FUNC_COMPLETE;
700 struct hisi_sas_tmf_task tmf_task;
701 int retry = HISI_SAS_DISK_RECOVER_CNT;
702 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
703 struct device *dev = hisi_hba->dev;
704 struct sas_phy *local_phy;
706 switch (device->dev_type) {
708 int_to_scsilun(0, &lun);
710 tmf_task.tmf = TMF_CLEAR_TASK_SET;
711 while (retry-- > 0) {
712 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
714 if (rc == TMF_RESP_FUNC_COMPLETE) {
715 hisi_sas_release_task(hisi_hba, device);
722 case SAS_SATA_PM_PORT:
723 case SAS_SATA_PENDING:
725 * send HARD RESET to clear previous affiliation of
728 local_phy = sas_get_local_phy(device);
729 if (!scsi_is_sas_phy_local(local_phy) &&
730 !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
731 unsigned long deadline = ata_deadline(jiffies, 20000);
732 struct sata_device *sata_dev = &device->sata_dev;
733 struct ata_host *ata_host = sata_dev->ata_host;
734 struct ata_port_operations *ops = ata_host->ops;
735 struct ata_port *ap = sata_dev->ap;
736 struct ata_link *link;
737 unsigned int classes;
739 ata_for_each_link(link, ap, EDGE)
740 rc = ops->hardreset(link, &classes,
743 sas_put_local_phy(local_phy);
745 dev_warn(dev, "SATA disk hardreset fail: %d\n", rc);
749 while (retry-- > 0) {
750 rc = hisi_sas_softreset_ata_disk(device);
762 static int hisi_sas_dev_found(struct domain_device *device)
764 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
765 struct domain_device *parent_dev = device->parent;
766 struct hisi_sas_device *sas_dev;
767 struct device *dev = hisi_hba->dev;
770 if (hisi_hba->hw->alloc_dev)
771 sas_dev = hisi_hba->hw->alloc_dev(device);
773 sas_dev = hisi_sas_alloc_dev(device);
775 dev_err(dev, "fail alloc dev: max support %d devices\n",
776 HISI_SAS_MAX_DEVICES);
780 device->lldd_dev = sas_dev;
781 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
783 if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
785 u8 phy_num = parent_dev->ex_dev.num_phys;
788 for (phy_no = 0; phy_no < phy_num; phy_no++) {
789 phy = &parent_dev->ex_dev.ex_phy[phy_no];
790 if (SAS_ADDR(phy->attached_sas_addr) ==
791 SAS_ADDR(device->sas_addr))
795 if (phy_no == phy_num) {
796 dev_info(dev, "dev found: no attached "
797 "dev:%016llx at ex:%016llx\n",
798 SAS_ADDR(device->sas_addr),
799 SAS_ADDR(parent_dev->sas_addr));
805 dev_info(dev, "dev[%d:%x] found\n",
806 sas_dev->device_id, sas_dev->dev_type);
808 rc = hisi_sas_init_device(device);
811 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
815 hisi_sas_dev_gone(device);
819 int hisi_sas_slave_configure(struct scsi_device *sdev)
821 struct domain_device *dev = sdev_to_domain_dev(sdev);
822 int ret = sas_slave_configure(sdev);
826 if (!dev_is_sata(dev))
827 sas_change_queue_depth(sdev, 64);
831 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
833 void hisi_sas_scan_start(struct Scsi_Host *shost)
835 struct hisi_hba *hisi_hba = shost_priv(shost);
837 hisi_hba->hw->phys_init(hisi_hba);
839 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
841 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
843 struct hisi_hba *hisi_hba = shost_priv(shost);
844 struct sas_ha_struct *sha = &hisi_hba->sha;
846 /* Wait for PHY up interrupt to occur */
853 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
855 static void hisi_sas_phyup_work(struct work_struct *work)
857 struct hisi_sas_phy *phy =
858 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
859 struct hisi_hba *hisi_hba = phy->hisi_hba;
860 struct asd_sas_phy *sas_phy = &phy->sas_phy;
861 int phy_no = sas_phy->id;
863 if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
864 hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
865 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
868 static void hisi_sas_linkreset_work(struct work_struct *work)
870 struct hisi_sas_phy *phy =
871 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
872 struct asd_sas_phy *sas_phy = &phy->sas_phy;
874 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
877 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
878 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
879 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
882 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
883 enum hisi_sas_phy_event event)
885 struct hisi_hba *hisi_hba = phy->hisi_hba;
887 if (WARN_ON(event >= HISI_PHYES_NUM))
890 return queue_work(hisi_hba->wq, &phy->works[event]);
892 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
894 static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
896 struct hisi_sas_phy *phy = from_timer(phy, t, timer);
897 struct hisi_hba *hisi_hba = phy->hisi_hba;
898 struct device *dev = hisi_hba->dev;
899 int phy_no = phy->sas_phy.id;
901 dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
902 hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
905 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
907 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
908 struct device *dev = hisi_hba->dev;
910 dev_dbg(dev, "phy%d OOB ready\n", phy_no);
911 if (phy->phy_attached)
914 if (!timer_pending(&phy->timer)) {
915 phy->timer.expires = jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT * HZ;
916 add_timer(&phy->timer);
919 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
921 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
923 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
924 struct asd_sas_phy *sas_phy = &phy->sas_phy;
927 phy->hisi_hba = hisi_hba;
929 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
930 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
931 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
932 sas_phy->class = SAS;
933 sas_phy->iproto = SAS_PROTOCOL_ALL;
935 sas_phy->type = PHY_TYPE_PHYSICAL;
936 sas_phy->role = PHY_ROLE_INITIATOR;
937 sas_phy->oob_mode = OOB_NOT_CONNECTED;
938 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
939 sas_phy->id = phy_no;
940 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
941 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
942 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
943 sas_phy->lldd_phy = phy;
945 for (i = 0; i < HISI_PHYES_NUM; i++)
946 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
948 spin_lock_init(&phy->lock);
950 timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
953 /* Wrapper to ensure we track hisi_sas_phy.enable properly */
954 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable)
956 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
957 struct asd_sas_phy *aphy = &phy->sas_phy;
958 struct sas_phy *sphy = aphy->phy;
961 spin_lock_irqsave(&phy->lock, flags);
964 /* We may have been enabled already; if so, don't touch */
966 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
967 hisi_hba->hw->phy_start(hisi_hba, phy_no);
969 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
970 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
972 phy->enable = enable;
973 spin_unlock_irqrestore(&phy->lock, flags);
975 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable);
977 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
979 struct sas_ha_struct *sas_ha = sas_phy->ha;
980 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
981 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
982 struct asd_sas_port *sas_port = sas_phy->port;
983 struct hisi_sas_port *port;
989 port = to_hisi_sas_port(sas_port);
990 spin_lock_irqsave(&hisi_hba->lock, flags);
991 port->port_attached = 1;
992 port->id = phy->port_id;
994 sas_port->lldd_port = port;
995 spin_unlock_irqrestore(&hisi_hba->lock, flags);
998 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
999 struct hisi_sas_slot *slot)
1002 unsigned long flags;
1003 struct task_status_struct *ts;
1005 ts = &task->task_status;
1007 ts->resp = SAS_TASK_COMPLETE;
1008 ts->stat = SAS_ABORTED_TASK;
1009 spin_lock_irqsave(&task->task_state_lock, flags);
1010 task->task_state_flags &=
1011 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1012 if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
1013 task->task_state_flags |= SAS_TASK_STATE_DONE;
1014 spin_unlock_irqrestore(&task->task_state_lock, flags);
1017 hisi_sas_slot_task_free(hisi_hba, task, slot);
1020 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
1021 struct domain_device *device)
1023 struct hisi_sas_slot *slot, *slot2;
1024 struct hisi_sas_device *sas_dev = device->lldd_dev;
1026 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
1027 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
1030 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
1032 struct hisi_sas_device *sas_dev;
1033 struct domain_device *device;
1036 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1037 sas_dev = &hisi_hba->devices[i];
1038 device = sas_dev->sas_device;
1040 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
1044 hisi_sas_release_task(hisi_hba, device);
1047 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
1049 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
1050 struct domain_device *device)
1052 if (hisi_hba->hw->dereg_device)
1053 hisi_hba->hw->dereg_device(hisi_hba, device);
1056 static void hisi_sas_dev_gone(struct domain_device *device)
1058 struct hisi_sas_device *sas_dev = device->lldd_dev;
1059 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1060 struct device *dev = hisi_hba->dev;
1063 dev_info(dev, "dev[%d:%x] is gone\n",
1064 sas_dev->device_id, sas_dev->dev_type);
1066 down(&hisi_hba->sem);
1067 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
1068 hisi_sas_internal_task_abort(hisi_hba, device,
1069 HISI_SAS_INT_ABT_DEV, 0);
1071 hisi_sas_dereg_device(hisi_hba, device);
1073 ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
1074 device->lldd_dev = NULL;
1077 if (hisi_hba->hw->free_device)
1078 hisi_hba->hw->free_device(sas_dev);
1080 /* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */
1082 sas_dev->dev_type = SAS_PHY_UNUSED;
1083 sas_dev->sas_device = NULL;
1087 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
1089 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
1092 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
1093 struct sas_phy_linkrates *r)
1095 struct sas_phy_linkrates _r;
1097 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1098 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1099 enum sas_linkrate min, max;
1101 if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
1104 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1105 max = sas_phy->phy->maximum_linkrate;
1106 min = r->minimum_linkrate;
1107 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1108 max = r->maximum_linkrate;
1109 min = sas_phy->phy->minimum_linkrate;
1113 _r.maximum_linkrate = max;
1114 _r.minimum_linkrate = min;
1116 sas_phy->phy->maximum_linkrate = max;
1117 sas_phy->phy->minimum_linkrate = min;
1119 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1121 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
1122 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1127 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
1130 struct sas_ha_struct *sas_ha = sas_phy->ha;
1131 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1132 int phy_no = sas_phy->id;
1135 case PHY_FUNC_HARD_RESET:
1136 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
1139 case PHY_FUNC_LINK_RESET:
1140 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1142 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1145 case PHY_FUNC_DISABLE:
1146 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1149 case PHY_FUNC_SET_LINK_RATE:
1150 return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1151 case PHY_FUNC_GET_EVENTS:
1152 if (hisi_hba->hw->get_events) {
1153 hisi_hba->hw->get_events(hisi_hba, phy_no);
1157 case PHY_FUNC_RELEASE_SPINUP_HOLD:
1164 static void hisi_sas_task_done(struct sas_task *task)
1166 del_timer(&task->slow_task->timer);
1167 complete(&task->slow_task->completion);
1170 static void hisi_sas_tmf_timedout(struct timer_list *t)
1172 struct sas_task_slow *slow = from_timer(slow, t, timer);
1173 struct sas_task *task = slow->task;
1174 unsigned long flags;
1175 bool is_completed = true;
1177 spin_lock_irqsave(&task->task_state_lock, flags);
1178 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1179 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1180 is_completed = false;
1182 spin_unlock_irqrestore(&task->task_state_lock, flags);
1185 complete(&task->slow_task->completion);
1188 #define TASK_TIMEOUT 20
1189 #define TASK_RETRY 3
1190 #define INTERNAL_ABORT_TIMEOUT 6
1191 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1192 void *parameter, u32 para_len,
1193 struct hisi_sas_tmf_task *tmf)
1195 struct hisi_sas_device *sas_dev = device->lldd_dev;
1196 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1197 struct device *dev = hisi_hba->dev;
1198 struct sas_task *task;
1201 for (retry = 0; retry < TASK_RETRY; retry++) {
1202 task = sas_alloc_slow_task(GFP_KERNEL);
1207 task->task_proto = device->tproto;
1209 if (dev_is_sata(device)) {
1210 task->ata_task.device_control_reg_update = 1;
1211 memcpy(&task->ata_task.fis, parameter, para_len);
1213 memcpy(&task->ssp_task, parameter, para_len);
1215 task->task_done = hisi_sas_task_done;
1217 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1218 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT * HZ;
1219 add_timer(&task->slow_task->timer);
1221 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1224 del_timer(&task->slow_task->timer);
1225 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1230 wait_for_completion(&task->slow_task->completion);
1231 res = TMF_RESP_FUNC_FAILED;
1232 /* Even TMF timed out, return direct. */
1233 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1234 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1235 struct hisi_sas_slot *slot = task->lldd_task;
1237 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1239 struct hisi_sas_cq *cq =
1240 &hisi_hba->cq[slot->dlvry_queue];
1242 * sync irq to avoid free'ing task
1243 * before using task in IO completion
1245 synchronize_irq(cq->irq_no);
1251 dev_err(dev, "abort tmf: TMF task timeout\n");
1254 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1255 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1256 res = TMF_RESP_FUNC_COMPLETE;
1260 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1261 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1262 res = TMF_RESP_FUNC_SUCC;
1266 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1267 task->task_status.stat == SAS_DATA_UNDERRUN) {
1268 /* no error, but return the number of bytes of
1271 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x sts 0x%x underrun\n",
1272 SAS_ADDR(device->sas_addr),
1273 task->task_status.resp,
1274 task->task_status.stat);
1275 res = task->task_status.residual;
1279 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1280 task->task_status.stat == SAS_DATA_OVERRUN) {
1281 dev_warn(dev, "abort tmf: blocked task error\n");
1286 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1287 task->task_status.stat == SAS_OPEN_REJECT) {
1288 dev_warn(dev, "abort tmf: open reject failed\n");
1291 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x status 0x%x\n",
1292 SAS_ADDR(device->sas_addr),
1293 task->task_status.resp,
1294 task->task_status.stat);
1296 sas_free_task(task);
1300 if (retry == TASK_RETRY)
1301 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1302 sas_free_task(task);
1306 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1307 bool reset, int pmp, u8 *fis)
1309 struct ata_taskfile tf;
1311 ata_tf_init(dev, &tf);
1315 tf.ctl &= ~ATA_SRST;
1316 tf.command = ATA_CMD_DEV_RESET;
1317 ata_tf_to_fis(&tf, pmp, 0, fis);
1320 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1323 struct ata_port *ap = device->sata_dev.ap;
1324 struct ata_link *link;
1325 int rc = TMF_RESP_FUNC_FAILED;
1326 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1327 struct device *dev = hisi_hba->dev;
1328 int s = sizeof(struct host_to_dev_fis);
1330 ata_for_each_link(link, ap, EDGE) {
1331 int pmp = sata_srst_pmp(link);
1333 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1334 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1335 if (rc != TMF_RESP_FUNC_COMPLETE)
1339 if (rc == TMF_RESP_FUNC_COMPLETE) {
1340 ata_for_each_link(link, ap, EDGE) {
1341 int pmp = sata_srst_pmp(link);
1343 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1344 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1346 if (rc != TMF_RESP_FUNC_COMPLETE)
1347 dev_err(dev, "ata disk de-reset failed\n");
1350 dev_err(dev, "ata disk reset failed\n");
1353 if (rc == TMF_RESP_FUNC_COMPLETE)
1354 hisi_sas_release_task(hisi_hba, device);
1359 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1360 u8 *lun, struct hisi_sas_tmf_task *tmf)
1362 struct sas_ssp_task ssp_task;
1364 if (!(device->tproto & SAS_PROTOCOL_SSP))
1365 return TMF_RESP_FUNC_ESUPP;
1367 memcpy(ssp_task.LUN, lun, 8);
1369 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1370 sizeof(ssp_task), tmf);
1373 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1375 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1378 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1379 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1380 struct domain_device *device = sas_dev->sas_device;
1381 struct asd_sas_port *sas_port;
1382 struct hisi_sas_port *port;
1383 struct hisi_sas_phy *phy = NULL;
1384 struct asd_sas_phy *sas_phy;
1386 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1387 || !device || !device->port)
1390 sas_port = device->port;
1391 port = to_hisi_sas_port(sas_port);
1393 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1394 if (state & BIT(sas_phy->id)) {
1395 phy = sas_phy->lldd_phy;
1400 port->id = phy->port_id;
1402 /* Update linkrate of directly attached device. */
1403 if (!device->parent)
1404 device->linkrate = phy->sas_phy.linkrate;
1406 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1412 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state)
1414 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1415 struct asd_sas_port *_sas_port = NULL;
1418 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1419 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1420 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1421 struct asd_sas_port *sas_port = sas_phy->port;
1422 bool do_port_check = _sas_port != sas_port;
1424 if (!sas_phy->phy->enabled)
1427 /* Report PHY state change to libsas */
1428 if (state & BIT(phy_no)) {
1429 if (do_port_check && sas_port && sas_port->port_dev) {
1430 struct domain_device *dev = sas_port->port_dev;
1432 _sas_port = sas_port;
1434 if (dev_is_expander(dev->dev_type))
1435 sas_ha->notify_port_event(sas_phy,
1436 PORTE_BROADCAST_RCVD);
1439 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1444 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1446 struct hisi_sas_device *sas_dev;
1447 struct domain_device *device;
1450 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1451 sas_dev = &hisi_hba->devices[i];
1452 device = sas_dev->sas_device;
1454 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1457 hisi_sas_init_device(device);
1461 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1462 struct asd_sas_port *sas_port,
1463 struct domain_device *device)
1465 struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1466 struct ata_port *ap = device->sata_dev.ap;
1467 struct device *dev = hisi_hba->dev;
1468 int s = sizeof(struct host_to_dev_fis);
1469 int rc = TMF_RESP_FUNC_FAILED;
1470 struct asd_sas_phy *sas_phy;
1471 struct ata_link *link;
1475 state = hisi_hba->hw->get_phys_state(hisi_hba);
1476 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1477 if (!(state & BIT(sas_phy->id)))
1480 ata_for_each_link(link, ap, EDGE) {
1481 int pmp = sata_srst_pmp(link);
1483 tmf_task.phy_id = sas_phy->id;
1484 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1485 rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1487 if (rc != TMF_RESP_FUNC_COMPLETE) {
1488 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1496 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1498 struct device *dev = hisi_hba->dev;
1501 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1502 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1503 struct domain_device *device = sas_dev->sas_device;
1505 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1508 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1509 HISI_SAS_INT_ABT_DEV, 0);
1511 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1514 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1515 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1516 struct asd_sas_port *sas_port = &port->sas_port;
1517 struct domain_device *port_dev = sas_port->port_dev;
1518 struct domain_device *device;
1520 if (!port_dev || !dev_is_expander(port_dev->dev_type))
1523 /* Try to find a SATA device */
1524 list_for_each_entry(device, &sas_port->dev_list,
1526 if (dev_is_sata(device)) {
1527 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1536 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1538 struct Scsi_Host *shost = hisi_hba->shost;
1540 down(&hisi_hba->sem);
1541 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1543 scsi_block_requests(shost);
1544 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1546 if (timer_pending(&hisi_hba->timer))
1547 del_timer_sync(&hisi_hba->timer);
1549 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1551 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1553 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1555 struct Scsi_Host *shost = hisi_hba->shost;
1557 /* Init and wait for PHYs to come up and all libsas event finished. */
1558 hisi_hba->hw->phys_init(hisi_hba);
1560 hisi_sas_refresh_port_id(hisi_hba);
1561 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1563 if (hisi_hba->reject_stp_links_msk)
1564 hisi_sas_terminate_stp_reject(hisi_hba);
1565 hisi_sas_reset_init_all_devices(hisi_hba);
1567 scsi_unblock_requests(shost);
1568 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1570 hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state);
1572 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1574 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1576 struct device *dev = hisi_hba->dev;
1577 struct Scsi_Host *shost = hisi_hba->shost;
1580 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
1581 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
1583 if (!hisi_hba->hw->soft_reset)
1586 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1589 dev_info(dev, "controller resetting...\n");
1590 hisi_sas_controller_reset_prepare(hisi_hba);
1592 rc = hisi_hba->hw->soft_reset(hisi_hba);
1594 dev_warn(dev, "controller reset failed (%d)\n", rc);
1595 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1597 scsi_unblock_requests(shost);
1598 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1602 hisi_sas_controller_reset_done(hisi_hba);
1603 dev_info(dev, "controller reset complete\n");
1608 static int hisi_sas_abort_task(struct sas_task *task)
1610 struct scsi_lun lun;
1611 struct hisi_sas_tmf_task tmf_task;
1612 struct domain_device *device = task->dev;
1613 struct hisi_sas_device *sas_dev = device->lldd_dev;
1614 struct hisi_hba *hisi_hba;
1616 int rc = TMF_RESP_FUNC_FAILED;
1617 unsigned long flags;
1620 return TMF_RESP_FUNC_FAILED;
1622 hisi_hba = dev_to_hisi_hba(task->dev);
1623 dev = hisi_hba->dev;
1625 spin_lock_irqsave(&task->task_state_lock, flags);
1626 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1627 struct hisi_sas_slot *slot = task->lldd_task;
1628 struct hisi_sas_cq *cq;
1632 * sync irq to avoid free'ing task
1633 * before using task in IO completion
1635 cq = &hisi_hba->cq[slot->dlvry_queue];
1636 synchronize_irq(cq->irq_no);
1638 spin_unlock_irqrestore(&task->task_state_lock, flags);
1639 rc = TMF_RESP_FUNC_COMPLETE;
1642 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1643 spin_unlock_irqrestore(&task->task_state_lock, flags);
1645 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1646 struct scsi_cmnd *cmnd = task->uldd_task;
1647 struct hisi_sas_slot *slot = task->lldd_task;
1648 u16 tag = slot->idx;
1651 int_to_scsilun(cmnd->device->lun, &lun);
1652 tmf_task.tmf = TMF_ABORT_TASK;
1653 tmf_task.tag_of_task_to_be_managed = tag;
1655 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1658 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1659 HISI_SAS_INT_ABT_CMD, tag);
1661 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1662 return TMF_RESP_FUNC_FAILED;
1666 * If the TMF finds that the IO is not in the device and also
1667 * the internal abort does not succeed, then it is safe to
1669 * Note: if the internal abort succeeds then the slot
1670 * will have already been completed
1672 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1673 if (task->lldd_task)
1674 hisi_sas_do_release_task(hisi_hba, task, slot);
1676 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1677 task->task_proto & SAS_PROTOCOL_STP) {
1678 if (task->dev->dev_type == SAS_SATA_DEV) {
1679 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1680 HISI_SAS_INT_ABT_DEV,
1683 dev_err(dev, "abort task: internal abort failed\n");
1686 hisi_sas_dereg_device(hisi_hba, device);
1687 rc = hisi_sas_softreset_ata_disk(device);
1689 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1691 struct hisi_sas_slot *slot = task->lldd_task;
1692 u32 tag = slot->idx;
1693 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1695 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1696 HISI_SAS_INT_ABT_CMD, tag);
1697 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1700 * sync irq to avoid free'ing task
1701 * before using task in IO completion
1703 synchronize_irq(cq->irq_no);
1709 if (rc != TMF_RESP_FUNC_COMPLETE)
1710 dev_notice(dev, "abort task: rc=%d\n", rc);
1714 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1716 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1717 struct device *dev = hisi_hba->dev;
1718 struct hisi_sas_tmf_task tmf_task;
1721 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1722 HISI_SAS_INT_ABT_DEV, 0);
1724 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1725 return TMF_RESP_FUNC_FAILED;
1727 hisi_sas_dereg_device(hisi_hba, device);
1729 tmf_task.tmf = TMF_ABORT_TASK_SET;
1730 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1732 if (rc == TMF_RESP_FUNC_COMPLETE)
1733 hisi_sas_release_task(hisi_hba, device);
1738 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1740 struct hisi_sas_tmf_task tmf_task;
1743 tmf_task.tmf = TMF_CLEAR_ACA;
1744 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1749 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1751 struct sas_phy *local_phy = sas_get_local_phy(device);
1752 struct hisi_sas_device *sas_dev = device->lldd_dev;
1753 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1754 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1755 DECLARE_COMPLETION_ONSTACK(phyreset);
1758 if (!local_phy->enabled) {
1759 sas_put_local_phy(local_phy);
1763 if (scsi_is_sas_phy_local(local_phy)) {
1764 struct asd_sas_phy *sas_phy =
1765 sas_ha->sas_phy[local_phy->number];
1766 struct hisi_sas_phy *phy =
1767 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1769 phy->reset_completion = &phyreset;
1772 reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
1773 !dev_is_sata(device)) ? true : false;
1775 rc = sas_phy_reset(local_phy, reset_type);
1776 sas_put_local_phy(local_phy);
1778 if (scsi_is_sas_phy_local(local_phy)) {
1779 struct asd_sas_phy *sas_phy =
1780 sas_ha->sas_phy[local_phy->number];
1781 struct hisi_sas_phy *phy =
1782 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1783 int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1784 unsigned long flags;
1786 spin_lock_irqsave(&phy->lock, flags);
1787 phy->reset_completion = NULL;
1789 spin_unlock_irqrestore(&phy->lock, flags);
1791 /* report PHY down if timed out */
1793 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
1794 } else if (sas_dev->dev_status != HISI_SAS_DEV_INIT) {
1796 * If in init state, we rely on caller to wait for link to be
1797 * ready; otherwise, except phy reset is fail, delay.
1806 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1808 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1809 struct device *dev = hisi_hba->dev;
1812 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1813 HISI_SAS_INT_ABT_DEV, 0);
1815 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1816 return TMF_RESP_FUNC_FAILED;
1818 hisi_sas_dereg_device(hisi_hba, device);
1820 if (dev_is_sata(device)) {
1821 rc = hisi_sas_softreset_ata_disk(device);
1822 if (rc == TMF_RESP_FUNC_FAILED)
1823 return TMF_RESP_FUNC_FAILED;
1826 rc = hisi_sas_debug_I_T_nexus_reset(device);
1828 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1829 hisi_sas_release_task(hisi_hba, device);
1834 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1836 struct hisi_sas_device *sas_dev = device->lldd_dev;
1837 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1838 struct device *dev = hisi_hba->dev;
1839 int rc = TMF_RESP_FUNC_FAILED;
1841 /* Clear internal IO and then lu reset */
1842 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1843 HISI_SAS_INT_ABT_DEV, 0);
1845 dev_err(dev, "lu_reset: internal abort failed\n");
1848 hisi_sas_dereg_device(hisi_hba, device);
1850 if (dev_is_sata(device)) {
1851 struct sas_phy *phy;
1853 phy = sas_get_local_phy(device);
1855 rc = sas_phy_reset(phy, true);
1858 hisi_sas_release_task(hisi_hba, device);
1859 sas_put_local_phy(phy);
1861 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1863 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1864 if (rc == TMF_RESP_FUNC_COMPLETE)
1865 hisi_sas_release_task(hisi_hba, device);
1868 if (rc != TMF_RESP_FUNC_COMPLETE)
1869 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1870 sas_dev->device_id, rc);
1874 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1876 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1877 struct device *dev = hisi_hba->dev;
1878 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1881 queue_work(hisi_hba->wq, &r.work);
1882 wait_for_completion(r.completion);
1884 return TMF_RESP_FUNC_FAILED;
1886 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1887 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1888 struct domain_device *device = sas_dev->sas_device;
1890 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1891 dev_is_expander(device->dev_type))
1894 rc = hisi_sas_debug_I_T_nexus_reset(device);
1895 if (rc != TMF_RESP_FUNC_COMPLETE)
1896 dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
1897 sas_dev->device_id, rc);
1900 hisi_sas_release_tasks(hisi_hba);
1902 return TMF_RESP_FUNC_COMPLETE;
1905 static int hisi_sas_query_task(struct sas_task *task)
1907 struct scsi_lun lun;
1908 struct hisi_sas_tmf_task tmf_task;
1909 int rc = TMF_RESP_FUNC_FAILED;
1911 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1912 struct scsi_cmnd *cmnd = task->uldd_task;
1913 struct domain_device *device = task->dev;
1914 struct hisi_sas_slot *slot = task->lldd_task;
1915 u32 tag = slot->idx;
1917 int_to_scsilun(cmnd->device->lun, &lun);
1918 tmf_task.tmf = TMF_QUERY_TASK;
1919 tmf_task.tag_of_task_to_be_managed = tag;
1921 rc = hisi_sas_debug_issue_ssp_tmf(device,
1925 /* The task is still in Lun, release it then */
1926 case TMF_RESP_FUNC_SUCC:
1927 /* The task is not in Lun or failed, reset the phy */
1928 case TMF_RESP_FUNC_FAILED:
1929 case TMF_RESP_FUNC_COMPLETE:
1932 rc = TMF_RESP_FUNC_FAILED;
1940 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1941 struct sas_task *task, int abort_flag,
1942 int task_tag, struct hisi_sas_dq *dq)
1944 struct domain_device *device = task->dev;
1945 struct hisi_sas_device *sas_dev = device->lldd_dev;
1946 struct device *dev = hisi_hba->dev;
1947 struct hisi_sas_port *port;
1948 struct hisi_sas_slot *slot;
1949 struct asd_sas_port *sas_port = device->port;
1950 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1951 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1952 unsigned long flags;
1955 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1961 port = to_hisi_sas_port(sas_port);
1963 /* simply get a slot and send abort command */
1964 rc = hisi_sas_slot_index_alloc(hisi_hba, NULL);
1969 slot = &hisi_hba->slot_info[slot_idx];
1971 spin_lock(&dq->lock);
1972 wr_q_index = dq->wr_point;
1973 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
1974 list_add_tail(&slot->delivery, &dq->list);
1975 spin_unlock(&dq->lock);
1976 spin_lock(&sas_dev->lock);
1977 list_add_tail(&slot->entry, &sas_dev->list);
1978 spin_unlock(&sas_dev->lock);
1980 dlvry_queue = dq->id;
1981 dlvry_queue_slot = wr_q_index;
1983 slot->device_id = sas_dev->device_id;
1984 slot->n_elem = n_elem;
1985 slot->dlvry_queue = dlvry_queue;
1986 slot->dlvry_queue_slot = dlvry_queue_slot;
1987 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1988 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1991 slot->is_internal = true;
1992 task->lldd_task = slot;
1994 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1995 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1996 memset(hisi_sas_status_buf_addr_mem(slot), 0,
1997 sizeof(struct hisi_sas_err_record));
1999 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
2000 abort_flag, task_tag);
2002 spin_lock_irqsave(&task->task_state_lock, flags);
2003 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
2004 spin_unlock_irqrestore(&task->task_state_lock, flags);
2005 WRITE_ONCE(slot->ready, 1);
2006 /* send abort command to the chip */
2007 spin_lock(&dq->lock);
2008 hisi_hba->hw->start_delivery(dq);
2009 spin_unlock(&dq->lock);
2014 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
2020 * _hisi_sas_internal_task_abort -- execute an internal
2021 * abort command for single IO command or a device
2022 * @hisi_hba: host controller struct
2023 * @device: domain device
2024 * @abort_flag: mode of operation, device or single IO
2025 * @tag: tag of IO to be aborted (only relevant to single
2027 * @dq: delivery queue for this internal abort command
2030 _hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2031 struct domain_device *device, int abort_flag,
2032 int tag, struct hisi_sas_dq *dq)
2034 struct sas_task *task;
2035 struct hisi_sas_device *sas_dev = device->lldd_dev;
2036 struct device *dev = hisi_hba->dev;
2040 * The interface is not realized means this HW don't support internal
2041 * abort, or don't need to do internal abort. Then here, we return
2042 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
2043 * the internal abort has been executed and returned CQ.
2045 if (!hisi_hba->hw->prep_abort)
2046 return TMF_RESP_FUNC_FAILED;
2048 task = sas_alloc_slow_task(GFP_KERNEL);
2053 task->task_proto = device->tproto;
2054 task->task_done = hisi_sas_task_done;
2055 task->slow_task->timer.function = hisi_sas_tmf_timedout;
2056 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT * HZ;
2057 add_timer(&task->slow_task->timer);
2059 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
2060 task, abort_flag, tag, dq);
2062 del_timer(&task->slow_task->timer);
2063 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
2067 wait_for_completion(&task->slow_task->completion);
2068 res = TMF_RESP_FUNC_FAILED;
2070 /* Internal abort timed out */
2071 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
2072 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
2073 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
2075 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
2076 struct hisi_sas_slot *slot = task->lldd_task;
2079 struct hisi_sas_cq *cq =
2080 &hisi_hba->cq[slot->dlvry_queue];
2082 * sync irq to avoid free'ing task
2083 * before using task in IO completion
2085 synchronize_irq(cq->irq_no);
2088 dev_err(dev, "internal task abort: timeout and not done.\n");
2093 dev_err(dev, "internal task abort: timeout.\n");
2096 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2097 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
2098 res = TMF_RESP_FUNC_COMPLETE;
2102 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2103 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
2104 res = TMF_RESP_FUNC_SUCC;
2109 dev_dbg(dev, "internal task abort: task to dev %016llx task=%pK resp: 0x%x sts 0x%x\n",
2110 SAS_ADDR(device->sas_addr), task,
2111 task->task_status.resp, /* 0 is complete, -1 is undelivered */
2112 task->task_status.stat);
2113 sas_free_task(task);
2119 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2120 struct domain_device *device,
2121 int abort_flag, int tag)
2123 struct hisi_sas_slot *slot;
2124 struct device *dev = hisi_hba->dev;
2125 struct hisi_sas_dq *dq;
2128 switch (abort_flag) {
2129 case HISI_SAS_INT_ABT_CMD:
2130 slot = &hisi_hba->slot_info[tag];
2131 dq = &hisi_hba->dq[slot->dlvry_queue];
2132 return _hisi_sas_internal_task_abort(hisi_hba, device,
2133 abort_flag, tag, dq);
2134 case HISI_SAS_INT_ABT_DEV:
2135 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2136 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2137 const struct cpumask *mask = cq->irq_mask;
2139 if (mask && !cpumask_intersects(cpu_online_mask, mask))
2141 dq = &hisi_hba->dq[i];
2142 rc = _hisi_sas_internal_task_abort(hisi_hba, device,
2150 dev_err(dev, "Unrecognised internal abort flag (%d)\n",
2158 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
2160 hisi_sas_port_notify_formed(sas_phy);
2163 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
2164 u8 reg_index, u8 reg_count, u8 *write_data)
2166 struct hisi_hba *hisi_hba = sha->lldd_ha;
2168 if (!hisi_hba->hw->write_gpio)
2171 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
2172 reg_index, reg_count, write_data);
2175 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
2177 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2178 struct sas_phy *sphy = sas_phy->phy;
2179 unsigned long flags;
2181 phy->phy_attached = 0;
2185 spin_lock_irqsave(&phy->lock, flags);
2187 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
2189 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
2190 spin_unlock_irqrestore(&phy->lock, flags);
2193 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
2195 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2196 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2197 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
2198 struct device *dev = hisi_hba->dev;
2201 /* Phy down but ready */
2202 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
2203 hisi_sas_port_notify_formed(sas_phy);
2205 struct hisi_sas_port *port = phy->port;
2207 if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
2209 dev_info(dev, "ignore flutter phy%d down\n", phy_no);
2212 /* Phy down and not ready */
2213 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
2214 sas_phy_disconnected(sas_phy);
2217 if (phy->phy_type & PORT_TYPE_SAS) {
2218 int port_id = port->id;
2220 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2222 port->port_attached = 0;
2223 } else if (phy->phy_type & PORT_TYPE_SATA)
2224 port->port_attached = 0;
2226 hisi_sas_phy_disconnected(phy);
2229 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2231 void hisi_sas_sync_irqs(struct hisi_hba *hisi_hba)
2235 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2236 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2238 synchronize_irq(cq->irq_no);
2241 EXPORT_SYMBOL_GPL(hisi_sas_sync_irqs);
2243 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type)
2245 struct hisi_hba *hisi_hba = shost_priv(shost);
2247 if (reset_type != SCSI_ADAPTER_RESET)
2250 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2254 EXPORT_SYMBOL_GPL(hisi_sas_host_reset);
2256 struct scsi_transport_template *hisi_sas_stt;
2257 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2259 static struct sas_domain_function_template hisi_sas_transport_ops = {
2260 .lldd_dev_found = hisi_sas_dev_found,
2261 .lldd_dev_gone = hisi_sas_dev_gone,
2262 .lldd_execute_task = hisi_sas_queue_command,
2263 .lldd_control_phy = hisi_sas_control_phy,
2264 .lldd_abort_task = hisi_sas_abort_task,
2265 .lldd_abort_task_set = hisi_sas_abort_task_set,
2266 .lldd_clear_aca = hisi_sas_clear_aca,
2267 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
2268 .lldd_lu_reset = hisi_sas_lu_reset,
2269 .lldd_query_task = hisi_sas_query_task,
2270 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
2271 .lldd_port_formed = hisi_sas_port_formed,
2272 .lldd_write_gpio = hisi_sas_write_gpio,
2275 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2277 int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS;
2278 struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
2280 for (i = 0; i < hisi_hba->queue_count; i++) {
2281 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2282 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2283 struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
2285 s = sizeof(struct hisi_sas_cmd_hdr);
2286 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2287 memset(&cmd_hdr[j], 0, s);
2291 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2292 memset(hisi_hba->complete_hdr[i], 0, s);
2296 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2297 memset(hisi_hba->initial_fis, 0, s);
2299 s = max_command_entries * sizeof(struct hisi_sas_iost);
2300 memset(hisi_hba->iost, 0, s);
2302 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2303 memset(hisi_hba->breakpoint, 0, s);
2305 s = sizeof(struct hisi_sas_sata_breakpoint);
2306 for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
2307 memset(&sata_breakpoint[j], 0, s);
2309 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2311 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2313 struct device *dev = hisi_hba->dev;
2314 int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS;
2315 int max_command_entries_ru, sz_slot_buf_ru;
2316 int blk_cnt, slots_per_blk;
2318 sema_init(&hisi_hba->sem, 1);
2319 spin_lock_init(&hisi_hba->lock);
2320 for (i = 0; i < hisi_hba->n_phy; i++) {
2321 hisi_sas_phy_init(hisi_hba, i);
2322 hisi_hba->port[i].port_attached = 0;
2323 hisi_hba->port[i].id = -1;
2326 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2327 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2328 hisi_hba->devices[i].device_id = i;
2329 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
2332 for (i = 0; i < hisi_hba->queue_count; i++) {
2333 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2334 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2336 /* Completion queue structure */
2338 cq->hisi_hba = hisi_hba;
2340 /* Delivery queue structure */
2341 spin_lock_init(&dq->lock);
2342 INIT_LIST_HEAD(&dq->list);
2344 dq->hisi_hba = hisi_hba;
2346 /* Delivery queue */
2347 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2348 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2349 &hisi_hba->cmd_hdr_dma[i],
2351 if (!hisi_hba->cmd_hdr[i])
2354 /* Completion queue */
2355 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2356 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2357 &hisi_hba->complete_hdr_dma[i],
2359 if (!hisi_hba->complete_hdr[i])
2363 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2364 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2366 if (!hisi_hba->itct)
2369 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2370 sizeof(struct hisi_sas_slot),
2372 if (!hisi_hba->slot_info)
2375 /* roundup to avoid overly large block size */
2376 max_command_entries_ru = roundup(max_command_entries, 64);
2377 if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
2378 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
2380 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2381 sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2382 s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
2383 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2384 slots_per_blk = s / sz_slot_buf_ru;
2386 for (i = 0; i < blk_cnt; i++) {
2387 int slot_index = i * slots_per_blk;
2391 buf = dmam_alloc_coherent(dev, s, &buf_dma,
2396 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2397 struct hisi_sas_slot *slot;
2399 slot = &hisi_hba->slot_info[slot_index];
2401 slot->buf_dma = buf_dma;
2402 slot->idx = slot_index;
2404 buf += sz_slot_buf_ru;
2405 buf_dma += sz_slot_buf_ru;
2409 s = max_command_entries * sizeof(struct hisi_sas_iost);
2410 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2412 if (!hisi_hba->iost)
2415 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2416 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2417 &hisi_hba->breakpoint_dma,
2419 if (!hisi_hba->breakpoint)
2422 hisi_hba->slot_index_count = max_command_entries;
2423 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2424 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2425 if (!hisi_hba->slot_index_tags)
2428 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2429 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2430 &hisi_hba->initial_fis_dma,
2432 if (!hisi_hba->initial_fis)
2435 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2436 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2437 &hisi_hba->sata_breakpoint_dma,
2439 if (!hisi_hba->sata_breakpoint)
2442 hisi_sas_slot_index_init(hisi_hba);
2443 hisi_hba->last_slot_index = HISI_SAS_UNRESERVED_IPTT;
2445 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2446 if (!hisi_hba->wq) {
2447 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2455 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2457 void hisi_sas_free(struct hisi_hba *hisi_hba)
2461 for (i = 0; i < hisi_hba->n_phy; i++) {
2462 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
2464 del_timer_sync(&phy->timer);
2468 destroy_workqueue(hisi_hba->wq);
2470 EXPORT_SYMBOL_GPL(hisi_sas_free);
2472 void hisi_sas_rst_work_handler(struct work_struct *work)
2474 struct hisi_hba *hisi_hba =
2475 container_of(work, struct hisi_hba, rst_work);
2477 hisi_sas_controller_reset(hisi_hba);
2479 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2481 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2483 struct hisi_sas_rst *rst =
2484 container_of(work, struct hisi_sas_rst, work);
2486 if (!hisi_sas_controller_reset(rst->hisi_hba))
2488 complete(rst->completion);
2490 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2492 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2494 struct device *dev = hisi_hba->dev;
2495 struct platform_device *pdev = hisi_hba->platform_dev;
2496 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2499 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2501 dev_err(dev, "could not get property sas-addr\n");
2507 * These properties are only required for platform device-based
2508 * controller with DT firmware.
2510 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2511 "hisilicon,sas-syscon");
2512 if (IS_ERR(hisi_hba->ctrl)) {
2513 dev_err(dev, "could not get syscon\n");
2517 if (device_property_read_u32(dev, "ctrl-reset-reg",
2518 &hisi_hba->ctrl_reset_reg)) {
2519 dev_err(dev, "could not get property ctrl-reset-reg\n");
2523 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2524 &hisi_hba->ctrl_reset_sts_reg)) {
2525 dev_err(dev, "could not get property ctrl-reset-sts-reg\n");
2529 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2530 &hisi_hba->ctrl_clock_ena_reg)) {
2531 dev_err(dev, "could not get property ctrl-clock-ena-reg\n");
2536 refclk = devm_clk_get(dev, NULL);
2538 dev_dbg(dev, "no ref clk property\n");
2540 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2542 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2543 dev_err(dev, "could not get property phy-count\n");
2547 if (device_property_read_u32(dev, "queue-count",
2548 &hisi_hba->queue_count)) {
2549 dev_err(dev, "could not get property queue-count\n");
2555 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2557 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2558 const struct hisi_sas_hw *hw)
2560 struct resource *res;
2561 struct Scsi_Host *shost;
2562 struct hisi_hba *hisi_hba;
2563 struct device *dev = &pdev->dev;
2566 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2568 dev_err(dev, "scsi host alloc failed\n");
2571 hisi_hba = shost_priv(shost);
2573 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2575 hisi_hba->dev = dev;
2576 hisi_hba->platform_dev = pdev;
2577 hisi_hba->shost = shost;
2578 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2580 timer_setup(&hisi_hba->timer, NULL, 0);
2582 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2585 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2587 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2590 dev_err(dev, "No usable DMA addressing method\n");
2594 hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0);
2595 if (IS_ERR(hisi_hba->regs))
2598 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2600 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2601 if (IS_ERR(hisi_hba->sgpio_regs))
2605 if (hisi_sas_alloc(hisi_hba)) {
2606 hisi_sas_free(hisi_hba);
2612 scsi_host_put(shost);
2613 dev_err(dev, "shost alloc failed\n");
2617 int hisi_sas_probe(struct platform_device *pdev,
2618 const struct hisi_sas_hw *hw)
2620 struct Scsi_Host *shost;
2621 struct hisi_hba *hisi_hba;
2622 struct device *dev = &pdev->dev;
2623 struct asd_sas_phy **arr_phy;
2624 struct asd_sas_port **arr_port;
2625 struct sas_ha_struct *sha;
2626 int rc, phy_nr, port_nr, i;
2628 shost = hisi_sas_shost_alloc(pdev, hw);
2632 sha = SHOST_TO_SAS_HA(shost);
2633 hisi_hba = shost_priv(shost);
2634 platform_set_drvdata(pdev, sha);
2636 phy_nr = port_nr = hisi_hba->n_phy;
2638 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2639 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2640 if (!arr_phy || !arr_port) {
2645 sha->sas_phy = arr_phy;
2646 sha->sas_port = arr_port;
2647 sha->lldd_ha = hisi_hba;
2649 shost->transportt = hisi_sas_stt;
2650 shost->max_id = HISI_SAS_MAX_DEVICES;
2651 shost->max_lun = ~0;
2652 shost->max_channel = 1;
2653 shost->max_cmd_len = 16;
2654 if (hisi_hba->hw->slot_index_alloc) {
2655 shost->can_queue = HISI_SAS_MAX_COMMANDS;
2656 shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;
2658 shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
2659 shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
2662 sha->sas_ha_name = DRV_NAME;
2663 sha->dev = hisi_hba->dev;
2664 sha->lldd_module = THIS_MODULE;
2665 sha->sas_addr = &hisi_hba->sas_addr[0];
2666 sha->num_phys = hisi_hba->n_phy;
2667 sha->core.shost = hisi_hba->shost;
2669 for (i = 0; i < hisi_hba->n_phy; i++) {
2670 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2671 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2674 rc = scsi_add_host(shost, &pdev->dev);
2678 rc = sas_register_ha(sha);
2680 goto err_out_register_ha;
2682 rc = hisi_hba->hw->hw_init(hisi_hba);
2684 goto err_out_register_ha;
2686 scsi_scan_host(shost);
2690 err_out_register_ha:
2691 scsi_remove_host(shost);
2693 hisi_sas_debugfs_exit(hisi_hba);
2694 hisi_sas_free(hisi_hba);
2695 scsi_host_put(shost);
2698 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2700 struct dentry *hisi_sas_debugfs_dir;
2702 static void hisi_sas_debugfs_snapshot_cq_reg(struct hisi_hba *hisi_hba)
2704 int queue_entry_size = hisi_hba->hw->complete_hdr_size;
2705 int dump_index = hisi_hba->debugfs_dump_index;
2708 for (i = 0; i < hisi_hba->queue_count; i++)
2709 memcpy(hisi_hba->debugfs_cq[dump_index][i].complete_hdr,
2710 hisi_hba->complete_hdr[i],
2711 HISI_SAS_QUEUE_SLOTS * queue_entry_size);
2714 static void hisi_sas_debugfs_snapshot_dq_reg(struct hisi_hba *hisi_hba)
2716 int queue_entry_size = sizeof(struct hisi_sas_cmd_hdr);
2717 int dump_index = hisi_hba->debugfs_dump_index;
2720 for (i = 0; i < hisi_hba->queue_count; i++) {
2721 struct hisi_sas_cmd_hdr *debugfs_cmd_hdr, *cmd_hdr;
2724 debugfs_cmd_hdr = hisi_hba->debugfs_dq[dump_index][i].hdr;
2725 cmd_hdr = hisi_hba->cmd_hdr[i];
2727 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2728 memcpy(&debugfs_cmd_hdr[j], &cmd_hdr[j],
2733 static void hisi_sas_debugfs_snapshot_port_reg(struct hisi_hba *hisi_hba)
2735 int dump_index = hisi_hba->debugfs_dump_index;
2736 const struct hisi_sas_debugfs_reg *port =
2737 hisi_hba->hw->debugfs_reg_port;
2742 for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) {
2743 databuf = hisi_hba->debugfs_port_reg[dump_index][phy_cnt].data;
2744 for (i = 0; i < port->count; i++, databuf++) {
2745 offset = port->base_off + 4 * i;
2746 *databuf = port->read_port_reg(hisi_hba, phy_cnt,
2752 static void hisi_sas_debugfs_snapshot_global_reg(struct hisi_hba *hisi_hba)
2754 int dump_index = hisi_hba->debugfs_dump_index;
2755 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL].data;
2756 const struct hisi_sas_hw *hw = hisi_hba->hw;
2757 const struct hisi_sas_debugfs_reg *global =
2758 hw->debugfs_reg_array[DEBUGFS_GLOBAL];
2761 for (i = 0; i < global->count; i++, databuf++)
2762 *databuf = global->read_global_reg(hisi_hba, 4 * i);
2765 static void hisi_sas_debugfs_snapshot_axi_reg(struct hisi_hba *hisi_hba)
2767 int dump_index = hisi_hba->debugfs_dump_index;
2768 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI].data;
2769 const struct hisi_sas_hw *hw = hisi_hba->hw;
2770 const struct hisi_sas_debugfs_reg *axi =
2771 hw->debugfs_reg_array[DEBUGFS_AXI];
2774 for (i = 0; i < axi->count; i++, databuf++)
2775 *databuf = axi->read_global_reg(hisi_hba,
2776 4 * i + axi->base_off);
2779 static void hisi_sas_debugfs_snapshot_ras_reg(struct hisi_hba *hisi_hba)
2781 int dump_index = hisi_hba->debugfs_dump_index;
2782 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS].data;
2783 const struct hisi_sas_hw *hw = hisi_hba->hw;
2784 const struct hisi_sas_debugfs_reg *ras =
2785 hw->debugfs_reg_array[DEBUGFS_RAS];
2788 for (i = 0; i < ras->count; i++, databuf++)
2789 *databuf = ras->read_global_reg(hisi_hba,
2790 4 * i + ras->base_off);
2793 static void hisi_sas_debugfs_snapshot_itct_reg(struct hisi_hba *hisi_hba)
2795 int dump_index = hisi_hba->debugfs_dump_index;
2796 void *cachebuf = hisi_hba->debugfs_itct_cache[dump_index].cache;
2797 void *databuf = hisi_hba->debugfs_itct[dump_index].itct;
2798 struct hisi_sas_itct *itct;
2801 hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_ITCT_CACHE,
2804 itct = hisi_hba->itct;
2806 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
2807 memcpy(databuf, itct, sizeof(struct hisi_sas_itct));
2808 databuf += sizeof(struct hisi_sas_itct);
2812 static void hisi_sas_debugfs_snapshot_iost_reg(struct hisi_hba *hisi_hba)
2814 int dump_index = hisi_hba->debugfs_dump_index;
2815 int max_command_entries = HISI_SAS_MAX_COMMANDS;
2816 void *cachebuf = hisi_hba->debugfs_iost_cache[dump_index].cache;
2817 void *databuf = hisi_hba->debugfs_iost[dump_index].iost;
2818 struct hisi_sas_iost *iost;
2821 hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_IOST_CACHE,
2824 iost = hisi_hba->iost;
2826 for (i = 0; i < max_command_entries; i++, iost++) {
2827 memcpy(databuf, iost, sizeof(struct hisi_sas_iost));
2828 databuf += sizeof(struct hisi_sas_iost);
2833 hisi_sas_debugfs_to_reg_name(int off, int base_off,
2834 const struct hisi_sas_debugfs_reg_lu *lu)
2836 for (; lu->name; lu++) {
2837 if (off == lu->off - base_off)
2844 static void hisi_sas_debugfs_print_reg(u32 *regs_val, const void *ptr,
2847 const struct hisi_sas_debugfs_reg *reg = ptr;
2850 for (i = 0; i < reg->count; i++) {
2854 name = hisi_sas_debugfs_to_reg_name(off, reg->base_off,
2858 seq_printf(s, "0x%08x 0x%08x %s\n", off,
2861 seq_printf(s, "0x%08x 0x%08x\n", off,
2866 static int hisi_sas_debugfs_global_show(struct seq_file *s, void *p)
2868 struct hisi_sas_debugfs_regs *global = s->private;
2869 struct hisi_hba *hisi_hba = global->hisi_hba;
2870 const struct hisi_sas_hw *hw = hisi_hba->hw;
2871 const void *reg_global = hw->debugfs_reg_array[DEBUGFS_GLOBAL];
2873 hisi_sas_debugfs_print_reg(global->data,
2879 static int hisi_sas_debugfs_global_open(struct inode *inode, struct file *filp)
2881 return single_open(filp, hisi_sas_debugfs_global_show,
2885 static const struct file_operations hisi_sas_debugfs_global_fops = {
2886 .open = hisi_sas_debugfs_global_open,
2888 .llseek = seq_lseek,
2889 .release = single_release,
2890 .owner = THIS_MODULE,
2893 static int hisi_sas_debugfs_axi_show(struct seq_file *s, void *p)
2895 struct hisi_sas_debugfs_regs *axi = s->private;
2896 struct hisi_hba *hisi_hba = axi->hisi_hba;
2897 const struct hisi_sas_hw *hw = hisi_hba->hw;
2898 const void *reg_axi = hw->debugfs_reg_array[DEBUGFS_AXI];
2900 hisi_sas_debugfs_print_reg(axi->data,
2906 static int hisi_sas_debugfs_axi_open(struct inode *inode, struct file *filp)
2908 return single_open(filp, hisi_sas_debugfs_axi_show,
2912 static const struct file_operations hisi_sas_debugfs_axi_fops = {
2913 .open = hisi_sas_debugfs_axi_open,
2915 .llseek = seq_lseek,
2916 .release = single_release,
2917 .owner = THIS_MODULE,
2920 static int hisi_sas_debugfs_ras_show(struct seq_file *s, void *p)
2922 struct hisi_sas_debugfs_regs *ras = s->private;
2923 struct hisi_hba *hisi_hba = ras->hisi_hba;
2924 const struct hisi_sas_hw *hw = hisi_hba->hw;
2925 const void *reg_ras = hw->debugfs_reg_array[DEBUGFS_RAS];
2927 hisi_sas_debugfs_print_reg(ras->data,
2933 static int hisi_sas_debugfs_ras_open(struct inode *inode, struct file *filp)
2935 return single_open(filp, hisi_sas_debugfs_ras_show,
2939 static const struct file_operations hisi_sas_debugfs_ras_fops = {
2940 .open = hisi_sas_debugfs_ras_open,
2942 .llseek = seq_lseek,
2943 .release = single_release,
2944 .owner = THIS_MODULE,
2947 static int hisi_sas_debugfs_port_show(struct seq_file *s, void *p)
2949 struct hisi_sas_debugfs_port *port = s->private;
2950 struct hisi_sas_phy *phy = port->phy;
2951 struct hisi_hba *hisi_hba = phy->hisi_hba;
2952 const struct hisi_sas_hw *hw = hisi_hba->hw;
2953 const struct hisi_sas_debugfs_reg *reg_port = hw->debugfs_reg_port;
2955 hisi_sas_debugfs_print_reg(port->data, reg_port, s);
2960 static int hisi_sas_debugfs_port_open(struct inode *inode, struct file *filp)
2962 return single_open(filp, hisi_sas_debugfs_port_show, inode->i_private);
2965 static const struct file_operations hisi_sas_debugfs_port_fops = {
2966 .open = hisi_sas_debugfs_port_open,
2968 .llseek = seq_lseek,
2969 .release = single_release,
2970 .owner = THIS_MODULE,
2973 static void hisi_sas_show_row_64(struct seq_file *s, int index,
2974 int sz, __le64 *ptr)
2978 /* completion header size not fixed per HW version */
2979 seq_printf(s, "index %04d:\n\t", index);
2980 for (i = 1; i <= sz / 8; i++, ptr++) {
2981 seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr));
2983 seq_puts(s, "\n\t");
2989 static void hisi_sas_show_row_32(struct seq_file *s, int index,
2990 int sz, __le32 *ptr)
2994 /* completion header size not fixed per HW version */
2995 seq_printf(s, "index %04d:\n\t", index);
2996 for (i = 1; i <= sz / 4; i++, ptr++) {
2997 seq_printf(s, " 0x%08x", le32_to_cpu(*ptr));
2999 seq_puts(s, "\n\t");
3004 static void hisi_sas_cq_show_slot(struct seq_file *s, int slot,
3005 struct hisi_sas_debugfs_cq *debugfs_cq)
3007 struct hisi_sas_cq *cq = debugfs_cq->cq;
3008 struct hisi_hba *hisi_hba = cq->hisi_hba;
3009 __le32 *complete_hdr = debugfs_cq->complete_hdr +
3010 (hisi_hba->hw->complete_hdr_size * slot);
3012 hisi_sas_show_row_32(s, slot,
3013 hisi_hba->hw->complete_hdr_size,
3017 static int hisi_sas_debugfs_cq_show(struct seq_file *s, void *p)
3019 struct hisi_sas_debugfs_cq *debugfs_cq = s->private;
3022 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
3023 hisi_sas_cq_show_slot(s, slot, debugfs_cq);
3028 static int hisi_sas_debugfs_cq_open(struct inode *inode, struct file *filp)
3030 return single_open(filp, hisi_sas_debugfs_cq_show, inode->i_private);
3033 static const struct file_operations hisi_sas_debugfs_cq_fops = {
3034 .open = hisi_sas_debugfs_cq_open,
3036 .llseek = seq_lseek,
3037 .release = single_release,
3038 .owner = THIS_MODULE,
3041 static void hisi_sas_dq_show_slot(struct seq_file *s, int slot, void *dq_ptr)
3043 struct hisi_sas_debugfs_dq *debugfs_dq = dq_ptr;
3044 void *cmd_queue = debugfs_dq->hdr;
3045 __le32 *cmd_hdr = cmd_queue +
3046 sizeof(struct hisi_sas_cmd_hdr) * slot;
3048 hisi_sas_show_row_32(s, slot, sizeof(struct hisi_sas_cmd_hdr), cmd_hdr);
3051 static int hisi_sas_debugfs_dq_show(struct seq_file *s, void *p)
3055 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
3056 hisi_sas_dq_show_slot(s, slot, s->private);
3061 static int hisi_sas_debugfs_dq_open(struct inode *inode, struct file *filp)
3063 return single_open(filp, hisi_sas_debugfs_dq_show, inode->i_private);
3066 static const struct file_operations hisi_sas_debugfs_dq_fops = {
3067 .open = hisi_sas_debugfs_dq_open,
3069 .llseek = seq_lseek,
3070 .release = single_release,
3071 .owner = THIS_MODULE,
3074 static int hisi_sas_debugfs_iost_show(struct seq_file *s, void *p)
3076 struct hisi_sas_debugfs_iost *debugfs_iost = s->private;
3077 struct hisi_sas_iost *iost = debugfs_iost->iost;
3078 int i, max_command_entries = HISI_SAS_MAX_COMMANDS;
3080 for (i = 0; i < max_command_entries; i++, iost++) {
3081 __le64 *data = &iost->qw0;
3083 hisi_sas_show_row_64(s, i, sizeof(*iost), data);
3089 static int hisi_sas_debugfs_iost_open(struct inode *inode, struct file *filp)
3091 return single_open(filp, hisi_sas_debugfs_iost_show, inode->i_private);
3094 static const struct file_operations hisi_sas_debugfs_iost_fops = {
3095 .open = hisi_sas_debugfs_iost_open,
3097 .llseek = seq_lseek,
3098 .release = single_release,
3099 .owner = THIS_MODULE,
3102 static int hisi_sas_debugfs_iost_cache_show(struct seq_file *s, void *p)
3104 struct hisi_sas_debugfs_iost_cache *debugfs_iost_cache = s->private;
3105 struct hisi_sas_iost_itct_cache *iost_cache = debugfs_iost_cache->cache;
3106 u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3110 for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, iost_cache++) {
3112 * Data struct of IOST cache:
3113 * Data[1]: BIT0~15: Table index
3115 * Data[2]~[9]: IOST table
3117 tab_idx = (iost_cache->data[1] & 0xffff);
3118 iost = (__le64 *)iost_cache;
3120 hisi_sas_show_row_64(s, tab_idx, cache_size, iost);
3126 static int hisi_sas_debugfs_iost_cache_open(struct inode *inode,
3129 return single_open(filp, hisi_sas_debugfs_iost_cache_show,
3133 static const struct file_operations hisi_sas_debugfs_iost_cache_fops = {
3134 .open = hisi_sas_debugfs_iost_cache_open,
3136 .llseek = seq_lseek,
3137 .release = single_release,
3138 .owner = THIS_MODULE,
3141 static int hisi_sas_debugfs_itct_show(struct seq_file *s, void *p)
3144 struct hisi_sas_debugfs_itct *debugfs_itct = s->private;
3145 struct hisi_sas_itct *itct = debugfs_itct->itct;
3147 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
3148 __le64 *data = &itct->qw0;
3150 hisi_sas_show_row_64(s, i, sizeof(*itct), data);
3156 static int hisi_sas_debugfs_itct_open(struct inode *inode, struct file *filp)
3158 return single_open(filp, hisi_sas_debugfs_itct_show, inode->i_private);
3161 static const struct file_operations hisi_sas_debugfs_itct_fops = {
3162 .open = hisi_sas_debugfs_itct_open,
3164 .llseek = seq_lseek,
3165 .release = single_release,
3166 .owner = THIS_MODULE,
3169 static int hisi_sas_debugfs_itct_cache_show(struct seq_file *s, void *p)
3171 struct hisi_sas_debugfs_itct_cache *debugfs_itct_cache = s->private;
3172 struct hisi_sas_iost_itct_cache *itct_cache = debugfs_itct_cache->cache;
3173 u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3177 for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, itct_cache++) {
3179 * Data struct of ITCT cache:
3180 * Data[1]: BIT0~15: Table index
3182 * Data[2]~[9]: ITCT table
3184 tab_idx = itct_cache->data[1] & 0xffff;
3185 itct = (__le64 *)itct_cache;
3187 hisi_sas_show_row_64(s, tab_idx, cache_size, itct);
3193 static int hisi_sas_debugfs_itct_cache_open(struct inode *inode,
3196 return single_open(filp, hisi_sas_debugfs_itct_cache_show,
3200 static const struct file_operations hisi_sas_debugfs_itct_cache_fops = {
3201 .open = hisi_sas_debugfs_itct_cache_open,
3203 .llseek = seq_lseek,
3204 .release = single_release,
3205 .owner = THIS_MODULE,
3208 static void hisi_sas_debugfs_create_files(struct hisi_hba *hisi_hba)
3210 u64 *debugfs_timestamp;
3211 int dump_index = hisi_hba->debugfs_dump_index;
3212 struct dentry *dump_dentry;
3213 struct dentry *dentry;
3219 snprintf(name, 256, "%d", dump_index);
3221 dump_dentry = debugfs_create_dir(name, hisi_hba->debugfs_dump_dentry);
3223 debugfs_timestamp = &hisi_hba->debugfs_timestamp[dump_index];
3225 debugfs_create_u64("timestamp", 0400, dump_dentry,
3228 debugfs_create_file("global", 0400, dump_dentry,
3229 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL],
3230 &hisi_sas_debugfs_global_fops);
3232 /* Create port dir and files */
3233 dentry = debugfs_create_dir("port", dump_dentry);
3234 for (p = 0; p < hisi_hba->n_phy; p++) {
3235 snprintf(name, 256, "%d", p);
3237 debugfs_create_file(name, 0400, dentry,
3238 &hisi_hba->debugfs_port_reg[dump_index][p],
3239 &hisi_sas_debugfs_port_fops);
3242 /* Create CQ dir and files */
3243 dentry = debugfs_create_dir("cq", dump_dentry);
3244 for (c = 0; c < hisi_hba->queue_count; c++) {
3245 snprintf(name, 256, "%d", c);
3247 debugfs_create_file(name, 0400, dentry,
3248 &hisi_hba->debugfs_cq[dump_index][c],
3249 &hisi_sas_debugfs_cq_fops);
3252 /* Create DQ dir and files */
3253 dentry = debugfs_create_dir("dq", dump_dentry);
3254 for (d = 0; d < hisi_hba->queue_count; d++) {
3255 snprintf(name, 256, "%d", d);
3257 debugfs_create_file(name, 0400, dentry,
3258 &hisi_hba->debugfs_dq[dump_index][d],
3259 &hisi_sas_debugfs_dq_fops);
3262 debugfs_create_file("iost", 0400, dump_dentry,
3263 &hisi_hba->debugfs_iost[dump_index],
3264 &hisi_sas_debugfs_iost_fops);
3266 debugfs_create_file("iost_cache", 0400, dump_dentry,
3267 &hisi_hba->debugfs_iost_cache[dump_index],
3268 &hisi_sas_debugfs_iost_cache_fops);
3270 debugfs_create_file("itct", 0400, dump_dentry,
3271 &hisi_hba->debugfs_itct[dump_index],
3272 &hisi_sas_debugfs_itct_fops);
3274 debugfs_create_file("itct_cache", 0400, dump_dentry,
3275 &hisi_hba->debugfs_itct_cache[dump_index],
3276 &hisi_sas_debugfs_itct_cache_fops);
3278 debugfs_create_file("axi", 0400, dump_dentry,
3279 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI],
3280 &hisi_sas_debugfs_axi_fops);
3282 debugfs_create_file("ras", 0400, dump_dentry,
3283 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS],
3284 &hisi_sas_debugfs_ras_fops);
3289 static void hisi_sas_debugfs_snapshot_regs(struct hisi_hba *hisi_hba)
3291 hisi_hba->hw->snapshot_prepare(hisi_hba);
3293 hisi_sas_debugfs_snapshot_global_reg(hisi_hba);
3294 hisi_sas_debugfs_snapshot_port_reg(hisi_hba);
3295 hisi_sas_debugfs_snapshot_axi_reg(hisi_hba);
3296 hisi_sas_debugfs_snapshot_ras_reg(hisi_hba);
3297 hisi_sas_debugfs_snapshot_cq_reg(hisi_hba);
3298 hisi_sas_debugfs_snapshot_dq_reg(hisi_hba);
3299 hisi_sas_debugfs_snapshot_itct_reg(hisi_hba);
3300 hisi_sas_debugfs_snapshot_iost_reg(hisi_hba);
3302 hisi_sas_debugfs_create_files(hisi_hba);
3304 hisi_hba->hw->snapshot_restore(hisi_hba);
3307 static ssize_t hisi_sas_debugfs_trigger_dump_write(struct file *file,
3308 const char __user *user_buf,
3309 size_t count, loff_t *ppos)
3311 struct hisi_hba *hisi_hba = file->f_inode->i_private;
3314 if (hisi_hba->debugfs_dump_index >= hisi_sas_debugfs_dump_count)
3320 if (copy_from_user(buf, user_buf, count))
3326 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
3331 static const struct file_operations hisi_sas_debugfs_trigger_dump_fops = {
3332 .write = &hisi_sas_debugfs_trigger_dump_write,
3333 .owner = THIS_MODULE,
3337 HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL = 0,
3338 HISI_SAS_BIST_LOOPBACK_MODE_SERDES,
3339 HISI_SAS_BIST_LOOPBACK_MODE_REMOTE,
3342 static const struct {
3345 } hisi_sas_debugfs_loop_linkrate[] = {
3346 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
3347 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
3348 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
3349 { SAS_LINK_RATE_12_0_GBPS, "12.0 Gbit" },
3352 static int hisi_sas_debugfs_bist_linkrate_show(struct seq_file *s, void *p)
3354 struct hisi_hba *hisi_hba = s->private;
3357 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
3358 int match = (hisi_hba->debugfs_bist_linkrate ==
3359 hisi_sas_debugfs_loop_linkrate[i].value);
3361 seq_printf(s, "%s%s%s ", match ? "[" : "",
3362 hisi_sas_debugfs_loop_linkrate[i].name,
3370 static ssize_t hisi_sas_debugfs_bist_linkrate_write(struct file *filp,
3371 const char __user *buf,
3372 size_t count, loff_t *ppos)
3374 struct seq_file *m = filp->private_data;
3375 struct hisi_hba *hisi_hba = m->private;
3376 char kbuf[16] = {}, *pkbuf;
3380 if (hisi_hba->debugfs_bist_enable)
3383 if (count >= sizeof(kbuf))
3386 if (copy_from_user(kbuf, buf, count))
3389 pkbuf = strstrip(kbuf);
3391 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
3392 if (!strncmp(hisi_sas_debugfs_loop_linkrate[i].name,
3394 hisi_hba->debugfs_bist_linkrate =
3395 hisi_sas_debugfs_loop_linkrate[i].value;
3407 static int hisi_sas_debugfs_bist_linkrate_open(struct inode *inode,
3410 return single_open(filp, hisi_sas_debugfs_bist_linkrate_show,
3414 static const struct file_operations hisi_sas_debugfs_bist_linkrate_ops = {
3415 .open = hisi_sas_debugfs_bist_linkrate_open,
3417 .write = hisi_sas_debugfs_bist_linkrate_write,
3418 .llseek = seq_lseek,
3419 .release = single_release,
3420 .owner = THIS_MODULE,
3423 static const struct {
3426 } hisi_sas_debugfs_loop_code_mode[] = {
3427 { HISI_SAS_BIST_CODE_MODE_PRBS7, "PRBS7" },
3428 { HISI_SAS_BIST_CODE_MODE_PRBS23, "PRBS23" },
3429 { HISI_SAS_BIST_CODE_MODE_PRBS31, "PRBS31" },
3430 { HISI_SAS_BIST_CODE_MODE_JTPAT, "JTPAT" },
3431 { HISI_SAS_BIST_CODE_MODE_CJTPAT, "CJTPAT" },
3432 { HISI_SAS_BIST_CODE_MODE_SCRAMBED_0, "SCRAMBED_0" },
3433 { HISI_SAS_BIST_CODE_MODE_TRAIN, "TRAIN" },
3434 { HISI_SAS_BIST_CODE_MODE_TRAIN_DONE, "TRAIN_DONE" },
3435 { HISI_SAS_BIST_CODE_MODE_HFTP, "HFTP" },
3436 { HISI_SAS_BIST_CODE_MODE_MFTP, "MFTP" },
3437 { HISI_SAS_BIST_CODE_MODE_LFTP, "LFTP" },
3438 { HISI_SAS_BIST_CODE_MODE_FIXED_DATA, "FIXED_DATA" },
3441 static int hisi_sas_debugfs_bist_code_mode_show(struct seq_file *s, void *p)
3443 struct hisi_hba *hisi_hba = s->private;
3446 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
3447 int match = (hisi_hba->debugfs_bist_code_mode ==
3448 hisi_sas_debugfs_loop_code_mode[i].value);
3450 seq_printf(s, "%s%s%s ", match ? "[" : "",
3451 hisi_sas_debugfs_loop_code_mode[i].name,
3459 static ssize_t hisi_sas_debugfs_bist_code_mode_write(struct file *filp,
3460 const char __user *buf,
3464 struct seq_file *m = filp->private_data;
3465 struct hisi_hba *hisi_hba = m->private;
3466 char kbuf[16] = {}, *pkbuf;
3470 if (hisi_hba->debugfs_bist_enable)
3473 if (count >= sizeof(kbuf))
3476 if (copy_from_user(kbuf, buf, count))
3479 pkbuf = strstrip(kbuf);
3481 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
3482 if (!strncmp(hisi_sas_debugfs_loop_code_mode[i].name,
3484 hisi_hba->debugfs_bist_code_mode =
3485 hisi_sas_debugfs_loop_code_mode[i].value;
3497 static int hisi_sas_debugfs_bist_code_mode_open(struct inode *inode,
3500 return single_open(filp, hisi_sas_debugfs_bist_code_mode_show,
3504 static const struct file_operations hisi_sas_debugfs_bist_code_mode_ops = {
3505 .open = hisi_sas_debugfs_bist_code_mode_open,
3507 .write = hisi_sas_debugfs_bist_code_mode_write,
3508 .llseek = seq_lseek,
3509 .release = single_release,
3510 .owner = THIS_MODULE,
3513 static ssize_t hisi_sas_debugfs_bist_phy_write(struct file *filp,
3514 const char __user *buf,
3515 size_t count, loff_t *ppos)
3517 struct seq_file *m = filp->private_data;
3518 struct hisi_hba *hisi_hba = m->private;
3519 unsigned int phy_no;
3522 if (hisi_hba->debugfs_bist_enable)
3525 val = kstrtouint_from_user(buf, count, 0, &phy_no);
3529 if (phy_no >= hisi_hba->n_phy)
3532 hisi_hba->debugfs_bist_phy_no = phy_no;
3537 static int hisi_sas_debugfs_bist_phy_show(struct seq_file *s, void *p)
3539 struct hisi_hba *hisi_hba = s->private;
3541 seq_printf(s, "%d\n", hisi_hba->debugfs_bist_phy_no);
3546 static int hisi_sas_debugfs_bist_phy_open(struct inode *inode,
3549 return single_open(filp, hisi_sas_debugfs_bist_phy_show,
3553 static const struct file_operations hisi_sas_debugfs_bist_phy_ops = {
3554 .open = hisi_sas_debugfs_bist_phy_open,
3556 .write = hisi_sas_debugfs_bist_phy_write,
3557 .llseek = seq_lseek,
3558 .release = single_release,
3559 .owner = THIS_MODULE,
3562 static const struct {
3565 } hisi_sas_debugfs_loop_modes[] = {
3566 { HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL, "digital" },
3567 { HISI_SAS_BIST_LOOPBACK_MODE_SERDES, "serdes" },
3568 { HISI_SAS_BIST_LOOPBACK_MODE_REMOTE, "remote" },
3571 static int hisi_sas_debugfs_bist_mode_show(struct seq_file *s, void *p)
3573 struct hisi_hba *hisi_hba = s->private;
3576 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
3577 int match = (hisi_hba->debugfs_bist_mode ==
3578 hisi_sas_debugfs_loop_modes[i].value);
3580 seq_printf(s, "%s%s%s ", match ? "[" : "",
3581 hisi_sas_debugfs_loop_modes[i].name,
3589 static ssize_t hisi_sas_debugfs_bist_mode_write(struct file *filp,
3590 const char __user *buf,
3591 size_t count, loff_t *ppos)
3593 struct seq_file *m = filp->private_data;
3594 struct hisi_hba *hisi_hba = m->private;
3595 char kbuf[16] = {}, *pkbuf;
3599 if (hisi_hba->debugfs_bist_enable)
3602 if (count >= sizeof(kbuf))
3605 if (copy_from_user(kbuf, buf, count))
3608 pkbuf = strstrip(kbuf);
3610 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
3611 if (!strncmp(hisi_sas_debugfs_loop_modes[i].name, pkbuf, 16)) {
3612 hisi_hba->debugfs_bist_mode =
3613 hisi_sas_debugfs_loop_modes[i].value;
3625 static int hisi_sas_debugfs_bist_mode_open(struct inode *inode,
3628 return single_open(filp, hisi_sas_debugfs_bist_mode_show,
3632 static const struct file_operations hisi_sas_debugfs_bist_mode_ops = {
3633 .open = hisi_sas_debugfs_bist_mode_open,
3635 .write = hisi_sas_debugfs_bist_mode_write,
3636 .llseek = seq_lseek,
3637 .release = single_release,
3638 .owner = THIS_MODULE,
3641 static ssize_t hisi_sas_debugfs_bist_enable_write(struct file *filp,
3642 const char __user *buf,
3643 size_t count, loff_t *ppos)
3645 struct seq_file *m = filp->private_data;
3646 struct hisi_hba *hisi_hba = m->private;
3647 unsigned int enable;
3650 val = kstrtouint_from_user(buf, count, 0, &enable);
3657 if (enable == hisi_hba->debugfs_bist_enable)
3660 if (!hisi_hba->hw->set_bist)
3663 val = hisi_hba->hw->set_bist(hisi_hba, enable);
3667 hisi_hba->debugfs_bist_enable = enable;
3672 static int hisi_sas_debugfs_bist_enable_show(struct seq_file *s, void *p)
3674 struct hisi_hba *hisi_hba = s->private;
3676 seq_printf(s, "%d\n", hisi_hba->debugfs_bist_enable);
3681 static int hisi_sas_debugfs_bist_enable_open(struct inode *inode,
3684 return single_open(filp, hisi_sas_debugfs_bist_enable_show,
3688 static const struct file_operations hisi_sas_debugfs_bist_enable_ops = {
3689 .open = hisi_sas_debugfs_bist_enable_open,
3691 .write = hisi_sas_debugfs_bist_enable_write,
3692 .llseek = seq_lseek,
3693 .release = single_release,
3694 .owner = THIS_MODULE,
3697 static const struct {
3699 } hisi_sas_debugfs_ffe_name[FFE_CFG_MAX] = {
3703 { "SAS_12_0_GBPS" },
3705 { "SATA_1_5_GBPS" },
3706 { "SATA_3_0_GBPS" },
3707 { "SATA_6_0_GBPS" },
3710 static ssize_t hisi_sas_debugfs_write(struct file *filp,
3711 const char __user *buf,
3712 size_t count, loff_t *ppos)
3714 struct seq_file *m = filp->private_data;
3715 u32 *val = m->private;
3718 res = kstrtouint_from_user(buf, count, 0, val);
3725 static int hisi_sas_debugfs_show(struct seq_file *s, void *p)
3727 u32 *val = s->private;
3729 seq_printf(s, "0x%x\n", *val);
3734 static int hisi_sas_debugfs_open(struct inode *inode, struct file *filp)
3736 return single_open(filp, hisi_sas_debugfs_show,
3740 static const struct file_operations hisi_sas_debugfs_ops = {
3741 .open = hisi_sas_debugfs_open,
3743 .write = hisi_sas_debugfs_write,
3744 .llseek = seq_lseek,
3745 .release = single_release,
3746 .owner = THIS_MODULE,
3749 static ssize_t hisi_sas_debugfs_phy_down_cnt_write(struct file *filp,
3750 const char __user *buf,
3751 size_t count, loff_t *ppos)
3753 struct seq_file *s = filp->private_data;
3754 struct hisi_sas_phy *phy = s->private;
3755 unsigned int set_val;
3758 res = kstrtouint_from_user(buf, count, 0, &set_val);
3765 atomic_set(&phy->down_cnt, 0);
3770 static int hisi_sas_debugfs_phy_down_cnt_show(struct seq_file *s, void *p)
3772 struct hisi_sas_phy *phy = s->private;
3774 seq_printf(s, "%d\n", atomic_read(&phy->down_cnt));
3779 static int hisi_sas_debugfs_phy_down_cnt_open(struct inode *inode,
3782 return single_open(filp, hisi_sas_debugfs_phy_down_cnt_show,
3786 static const struct file_operations hisi_sas_debugfs_phy_down_cnt_ops = {
3787 .open = hisi_sas_debugfs_phy_down_cnt_open,
3789 .write = hisi_sas_debugfs_phy_down_cnt_write,
3790 .llseek = seq_lseek,
3791 .release = single_release,
3792 .owner = THIS_MODULE,
3795 void hisi_sas_debugfs_work_handler(struct work_struct *work)
3797 struct hisi_hba *hisi_hba =
3798 container_of(work, struct hisi_hba, debugfs_work);
3799 int debugfs_dump_index = hisi_hba->debugfs_dump_index;
3800 struct device *dev = hisi_hba->dev;
3801 u64 timestamp = local_clock();
3803 if (debugfs_dump_index >= hisi_sas_debugfs_dump_count) {
3804 dev_warn(dev, "dump count exceeded!\n");
3808 do_div(timestamp, NSEC_PER_MSEC);
3809 hisi_hba->debugfs_timestamp[debugfs_dump_index] = timestamp;
3811 hisi_sas_debugfs_snapshot_regs(hisi_hba);
3812 hisi_hba->debugfs_dump_index++;
3814 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_work_handler);
3816 static void hisi_sas_debugfs_release(struct hisi_hba *hisi_hba, int dump_index)
3818 struct device *dev = hisi_hba->dev;
3821 devm_kfree(dev, hisi_hba->debugfs_iost_cache[dump_index].cache);
3822 devm_kfree(dev, hisi_hba->debugfs_itct_cache[dump_index].cache);
3823 devm_kfree(dev, hisi_hba->debugfs_iost[dump_index].iost);
3824 devm_kfree(dev, hisi_hba->debugfs_itct[dump_index].itct);
3826 for (i = 0; i < hisi_hba->queue_count; i++)
3827 devm_kfree(dev, hisi_hba->debugfs_dq[dump_index][i].hdr);
3829 for (i = 0; i < hisi_hba->queue_count; i++)
3831 hisi_hba->debugfs_cq[dump_index][i].complete_hdr);
3833 for (i = 0; i < DEBUGFS_REGS_NUM; i++)
3834 devm_kfree(dev, hisi_hba->debugfs_regs[dump_index][i].data);
3836 for (i = 0; i < hisi_hba->n_phy; i++)
3837 devm_kfree(dev, hisi_hba->debugfs_port_reg[dump_index][i].data);
3840 static int hisi_sas_debugfs_alloc(struct hisi_hba *hisi_hba, int dump_index)
3842 const struct hisi_sas_hw *hw = hisi_hba->hw;
3843 struct device *dev = hisi_hba->dev;
3847 for (r = 0; r < DEBUGFS_REGS_NUM; r++) {
3848 struct hisi_sas_debugfs_regs *regs =
3849 &hisi_hba->debugfs_regs[dump_index][r];
3851 sz = hw->debugfs_reg_array[r]->count * 4;
3852 regs->data = devm_kmalloc(dev, sz, GFP_KERNEL);
3855 regs->hisi_hba = hisi_hba;
3858 sz = hw->debugfs_reg_port->count * 4;
3859 for (p = 0; p < hisi_hba->n_phy; p++) {
3860 struct hisi_sas_debugfs_port *port =
3861 &hisi_hba->debugfs_port_reg[dump_index][p];
3863 port->data = devm_kmalloc(dev, sz, GFP_KERNEL);
3866 port->phy = &hisi_hba->phy[p];
3869 sz = hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
3870 for (c = 0; c < hisi_hba->queue_count; c++) {
3871 struct hisi_sas_debugfs_cq *cq =
3872 &hisi_hba->debugfs_cq[dump_index][c];
3874 cq->complete_hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
3875 if (!cq->complete_hdr)
3877 cq->cq = &hisi_hba->cq[c];
3880 sz = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
3881 for (d = 0; d < hisi_hba->queue_count; d++) {
3882 struct hisi_sas_debugfs_dq *dq =
3883 &hisi_hba->debugfs_dq[dump_index][d];
3885 dq->hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
3888 dq->dq = &hisi_hba->dq[d];
3891 sz = HISI_SAS_MAX_COMMANDS * sizeof(struct hisi_sas_iost);
3893 hisi_hba->debugfs_iost[dump_index].iost =
3894 devm_kmalloc(dev, sz, GFP_KERNEL);
3895 if (!hisi_hba->debugfs_iost[dump_index].iost)
3898 sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
3899 sizeof(struct hisi_sas_iost_itct_cache);
3901 hisi_hba->debugfs_iost_cache[dump_index].cache =
3902 devm_kmalloc(dev, sz, GFP_KERNEL);
3903 if (!hisi_hba->debugfs_iost_cache[dump_index].cache)
3906 sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
3907 sizeof(struct hisi_sas_iost_itct_cache);
3909 hisi_hba->debugfs_itct_cache[dump_index].cache =
3910 devm_kmalloc(dev, sz, GFP_KERNEL);
3911 if (!hisi_hba->debugfs_itct_cache[dump_index].cache)
3914 /* New memory allocation must be locate before itct */
3915 sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
3917 hisi_hba->debugfs_itct[dump_index].itct =
3918 devm_kmalloc(dev, sz, GFP_KERNEL);
3919 if (!hisi_hba->debugfs_itct[dump_index].itct)
3924 for (i = 0; i < hisi_sas_debugfs_dump_count; i++)
3925 hisi_sas_debugfs_release(hisi_hba, i);
3929 static void hisi_sas_debugfs_phy_down_cnt_init(struct hisi_hba *hisi_hba)
3931 struct dentry *dir = debugfs_create_dir("phy_down_cnt",
3932 hisi_hba->debugfs_dir);
3936 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
3937 snprintf(name, 16, "%d", phy_no);
3938 debugfs_create_file(name, 0600, dir,
3939 &hisi_hba->phy[phy_no],
3940 &hisi_sas_debugfs_phy_down_cnt_ops);
3944 static void hisi_sas_debugfs_bist_init(struct hisi_hba *hisi_hba)
3946 struct dentry *ports_dentry;
3949 hisi_hba->debugfs_bist_dentry =
3950 debugfs_create_dir("bist", hisi_hba->debugfs_dir);
3951 debugfs_create_file("link_rate", 0600,
3952 hisi_hba->debugfs_bist_dentry, hisi_hba,
3953 &hisi_sas_debugfs_bist_linkrate_ops);
3955 debugfs_create_file("code_mode", 0600,
3956 hisi_hba->debugfs_bist_dentry, hisi_hba,
3957 &hisi_sas_debugfs_bist_code_mode_ops);
3959 debugfs_create_file("fixed_code", 0600,
3960 hisi_hba->debugfs_bist_dentry,
3961 &hisi_hba->debugfs_bist_fixed_code[0],
3962 &hisi_sas_debugfs_ops);
3964 debugfs_create_file("fixed_code_1", 0600,
3965 hisi_hba->debugfs_bist_dentry,
3966 &hisi_hba->debugfs_bist_fixed_code[1],
3967 &hisi_sas_debugfs_ops);
3969 debugfs_create_file("phy_id", 0600, hisi_hba->debugfs_bist_dentry,
3970 hisi_hba, &hisi_sas_debugfs_bist_phy_ops);
3972 debugfs_create_u32("cnt", 0600, hisi_hba->debugfs_bist_dentry,
3973 &hisi_hba->debugfs_bist_cnt);
3975 debugfs_create_file("loopback_mode", 0600,
3976 hisi_hba->debugfs_bist_dentry,
3977 hisi_hba, &hisi_sas_debugfs_bist_mode_ops);
3979 debugfs_create_file("enable", 0600, hisi_hba->debugfs_bist_dentry,
3980 hisi_hba, &hisi_sas_debugfs_bist_enable_ops);
3982 ports_dentry = debugfs_create_dir("port", hisi_hba->debugfs_bist_dentry);
3984 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
3985 struct dentry *port_dentry;
3986 struct dentry *ffe_dentry;
3990 snprintf(name, 256, "%d", phy_no);
3991 port_dentry = debugfs_create_dir(name, ports_dentry);
3992 ffe_dentry = debugfs_create_dir("ffe", port_dentry);
3993 for (i = 0; i < FFE_CFG_MAX; i++) {
3996 debugfs_create_file(hisi_sas_debugfs_ffe_name[i].name,
3998 &hisi_hba->debugfs_bist_ffe[phy_no][i],
3999 &hisi_sas_debugfs_ops);
4003 hisi_hba->debugfs_bist_linkrate = SAS_LINK_RATE_1_5_GBPS;
4006 void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba)
4008 struct device *dev = hisi_hba->dev;
4011 hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev),
4012 hisi_sas_debugfs_dir);
4013 debugfs_create_file("trigger_dump", 0200,
4014 hisi_hba->debugfs_dir,
4016 &hisi_sas_debugfs_trigger_dump_fops);
4018 /* create bist structures */
4019 hisi_sas_debugfs_bist_init(hisi_hba);
4021 hisi_hba->debugfs_dump_dentry =
4022 debugfs_create_dir("dump", hisi_hba->debugfs_dir);
4024 hisi_sas_debugfs_phy_down_cnt_init(hisi_hba);
4026 for (i = 0; i < hisi_sas_debugfs_dump_count; i++) {
4027 if (hisi_sas_debugfs_alloc(hisi_hba, i)) {
4028 debugfs_remove_recursive(hisi_hba->debugfs_dir);
4029 dev_dbg(dev, "failed to init debugfs!\n");
4034 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_init);
4036 void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba)
4038 debugfs_remove_recursive(hisi_hba->debugfs_dir);
4040 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_exit);
4042 int hisi_sas_remove(struct platform_device *pdev)
4044 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
4045 struct hisi_hba *hisi_hba = sha->lldd_ha;
4046 struct Scsi_Host *shost = sha->core.shost;
4048 if (timer_pending(&hisi_hba->timer))
4049 del_timer(&hisi_hba->timer);
4051 sas_unregister_ha(sha);
4052 sas_remove_host(sha->core.shost);
4054 hisi_sas_free(hisi_hba);
4055 scsi_host_put(shost);
4058 EXPORT_SYMBOL_GPL(hisi_sas_remove);
4060 bool hisi_sas_debugfs_enable;
4061 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
4062 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
4063 MODULE_PARM_DESC(hisi_sas_debugfs_enable, "Enable driver debugfs (default disabled)");
4065 u32 hisi_sas_debugfs_dump_count = 1;
4066 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count);
4067 module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444);
4068 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow");
4070 static __init int hisi_sas_init(void)
4072 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
4076 if (hisi_sas_debugfs_enable) {
4077 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
4078 if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) {
4079 pr_info("hisi_sas: Limiting debugfs dump count\n");
4080 hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP;
4087 static __exit void hisi_sas_exit(void)
4089 sas_release_transport(hisi_sas_stt);
4091 debugfs_remove(hisi_sas_debugfs_dir);
4094 module_init(hisi_sas_init);
4095 module_exit(hisi_sas_exit);
4097 MODULE_LICENSE("GPL");
4098 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
4099 MODULE_DESCRIPTION("HISILICON SAS controller driver");
4100 MODULE_ALIAS("platform:" DRV_NAME);