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)) {
232 struct sas_ssp_task *ssp_task = &task->ssp_task;
233 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
236 dma_unmap_sg(dev, task->scatter,
239 if (slot->n_elem_dif)
240 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
241 scsi_prot_sg_count(scsi_cmnd),
246 spin_lock(&sas_dev->lock);
247 list_del_init(&slot->entry);
248 spin_unlock(&sas_dev->lock);
250 memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
252 hisi_sas_slot_index_free(hisi_hba, slot->idx);
254 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
256 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
257 struct hisi_sas_slot *slot)
259 hisi_hba->hw->prep_smp(hisi_hba, slot);
262 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
263 struct hisi_sas_slot *slot)
265 hisi_hba->hw->prep_ssp(hisi_hba, slot);
268 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
269 struct hisi_sas_slot *slot)
271 hisi_hba->hw->prep_stp(hisi_hba, slot);
274 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
275 struct hisi_sas_slot *slot,
276 int device_id, int abort_flag, int tag_to_abort)
278 hisi_hba->hw->prep_abort(hisi_hba, slot,
279 device_id, abort_flag, tag_to_abort);
282 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
283 struct sas_task *task, int n_elem,
286 struct device *dev = hisi_hba->dev;
288 if (!sas_protocol_ata(task->task_proto)) {
289 if (task->num_scatter) {
291 dma_unmap_sg(dev, task->scatter,
294 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
296 dma_unmap_sg(dev, &task->smp_task.smp_req,
302 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
303 struct sas_task *task, int *n_elem,
306 struct device *dev = hisi_hba->dev;
309 if (sas_protocol_ata(task->task_proto)) {
310 *n_elem = task->num_scatter;
312 unsigned int req_len;
314 if (task->num_scatter) {
315 *n_elem = dma_map_sg(dev, task->scatter,
316 task->num_scatter, task->data_dir);
321 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
322 *n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
328 req_len = sg_dma_len(&task->smp_task.smp_req);
331 goto err_out_dma_unmap;
336 if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
337 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
340 goto err_out_dma_unmap;
345 /* It would be better to call dma_unmap_sg() here, but it's messy */
346 hisi_sas_dma_unmap(hisi_hba, task, *n_elem,
352 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
353 struct sas_task *task, int n_elem_dif)
355 struct device *dev = hisi_hba->dev;
358 struct sas_ssp_task *ssp_task = &task->ssp_task;
359 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
361 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
362 scsi_prot_sg_count(scsi_cmnd),
367 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
368 int *n_elem_dif, struct sas_task *task)
370 struct device *dev = hisi_hba->dev;
371 struct sas_ssp_task *ssp_task;
372 struct scsi_cmnd *scsi_cmnd;
375 if (task->num_scatter) {
376 ssp_task = &task->ssp_task;
377 scsi_cmnd = ssp_task->cmd;
379 if (scsi_prot_sg_count(scsi_cmnd)) {
380 *n_elem_dif = dma_map_sg(dev,
381 scsi_prot_sglist(scsi_cmnd),
382 scsi_prot_sg_count(scsi_cmnd),
388 if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
389 dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
392 goto err_out_dif_dma_unmap;
399 err_out_dif_dma_unmap:
400 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
401 scsi_prot_sg_count(scsi_cmnd), task->data_dir);
405 static int hisi_sas_task_prep(struct sas_task *task,
406 struct hisi_sas_dq **dq_pointer,
407 bool is_tmf, struct hisi_sas_tmf_task *tmf,
410 struct domain_device *device = task->dev;
411 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
412 struct hisi_sas_device *sas_dev = device->lldd_dev;
413 struct hisi_sas_port *port;
414 struct hisi_sas_slot *slot;
415 struct hisi_sas_cmd_hdr *cmd_hdr_base;
416 struct asd_sas_port *sas_port = device->port;
417 struct device *dev = hisi_hba->dev;
418 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
419 int n_elem = 0, n_elem_dif = 0, n_elem_req = 0;
420 struct hisi_sas_dq *dq;
424 if (DEV_IS_GONE(sas_dev)) {
426 dev_info(dev, "task prep: device %d not ready\n",
429 dev_info(dev, "task prep: device %016llx not ready\n",
430 SAS_ADDR(device->sas_addr));
435 if (hisi_hba->reply_map) {
436 int cpu = raw_smp_processor_id();
437 unsigned int dq_index = hisi_hba->reply_map[cpu];
439 *dq_pointer = dq = &hisi_hba->dq[dq_index];
441 *dq_pointer = dq = sas_dev->dq;
444 port = to_hisi_sas_port(sas_port);
445 if (port && !port->port_attached) {
446 dev_info(dev, "task prep: %s port%d not attach device\n",
447 (dev_is_sata(device)) ?
454 rc = hisi_sas_dma_map(hisi_hba, task, &n_elem,
459 if (!sas_protocol_ata(task->task_proto)) {
460 rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
462 goto err_out_dma_unmap;
465 if (hisi_hba->hw->slot_index_alloc)
466 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
468 struct scsi_cmnd *scsi_cmnd = NULL;
470 if (task->uldd_task) {
471 struct ata_queued_cmd *qc;
473 if (dev_is_sata(device)) {
474 qc = task->uldd_task;
475 scsi_cmnd = qc->scsicmd;
477 scsi_cmnd = task->uldd_task;
480 rc = hisi_sas_slot_index_alloc(hisi_hba, scsi_cmnd);
483 goto err_out_dif_dma_unmap;
486 slot = &hisi_hba->slot_info[slot_idx];
488 spin_lock(&dq->lock);
489 wr_q_index = dq->wr_point;
490 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
491 list_add_tail(&slot->delivery, &dq->list);
492 spin_unlock(&dq->lock);
493 spin_lock(&sas_dev->lock);
494 list_add_tail(&slot->entry, &sas_dev->list);
495 spin_unlock(&sas_dev->lock);
497 dlvry_queue = dq->id;
498 dlvry_queue_slot = wr_q_index;
500 slot->device_id = sas_dev->device_id;
501 slot->n_elem = n_elem;
502 slot->n_elem_dif = n_elem_dif;
503 slot->dlvry_queue = dlvry_queue;
504 slot->dlvry_queue_slot = dlvry_queue_slot;
505 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
506 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
510 slot->is_internal = is_tmf;
511 task->lldd_task = slot;
513 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
514 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
515 memset(hisi_sas_status_buf_addr_mem(slot), 0,
516 sizeof(struct hisi_sas_err_record));
518 switch (task->task_proto) {
519 case SAS_PROTOCOL_SMP:
520 hisi_sas_task_prep_smp(hisi_hba, slot);
522 case SAS_PROTOCOL_SSP:
523 hisi_sas_task_prep_ssp(hisi_hba, slot);
525 case SAS_PROTOCOL_SATA:
526 case SAS_PROTOCOL_STP:
527 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
528 hisi_sas_task_prep_ata(hisi_hba, slot);
531 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
536 spin_lock_irqsave(&task->task_state_lock, flags);
537 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
538 spin_unlock_irqrestore(&task->task_state_lock, flags);
541 WRITE_ONCE(slot->ready, 1);
545 err_out_dif_dma_unmap:
546 if (!sas_protocol_ata(task->task_proto))
547 hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
549 hisi_sas_dma_unmap(hisi_hba, task, n_elem,
552 dev_err(dev, "task prep: failed[%d]!\n", rc);
556 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
557 bool is_tmf, struct hisi_sas_tmf_task *tmf)
561 struct hisi_hba *hisi_hba;
563 struct domain_device *device = task->dev;
564 struct asd_sas_port *sas_port = device->port;
565 struct hisi_sas_dq *dq = NULL;
568 struct task_status_struct *ts = &task->task_status;
570 ts->resp = SAS_TASK_UNDELIVERED;
571 ts->stat = SAS_PHY_DOWN;
573 * libsas will use dev->port, should
574 * not call task_done for sata
576 if (device->dev_type != SAS_SATA_DEV)
577 task->task_done(task);
581 hisi_hba = dev_to_hisi_hba(device);
584 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
586 * For IOs from upper layer, it may already disable preempt
587 * in the IO path, if disable preempt again in down(),
588 * function schedule() will report schedule_bug(), so check
589 * preemptible() before goto down().
594 down(&hisi_hba->sem);
598 /* protect task_prep and start_delivery sequence */
599 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
601 dev_err(dev, "task exec: failed[%d]!\n", rc);
604 spin_lock(&dq->lock);
605 hisi_hba->hw->start_delivery(dq);
606 spin_unlock(&dq->lock);
612 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
614 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
615 struct asd_sas_phy *sas_phy = &phy->sas_phy;
616 struct sas_ha_struct *sas_ha;
618 if (!phy->phy_attached)
621 sas_ha = &hisi_hba->sha;
622 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
625 struct sas_phy *sphy = sas_phy->phy;
627 sphy->negotiated_linkrate = sas_phy->linkrate;
628 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
629 sphy->maximum_linkrate_hw =
630 hisi_hba->hw->phy_get_max_linkrate();
631 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
632 sphy->minimum_linkrate = phy->minimum_linkrate;
634 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
635 sphy->maximum_linkrate = phy->maximum_linkrate;
638 if (phy->phy_type & PORT_TYPE_SAS) {
639 struct sas_identify_frame *id;
641 id = (struct sas_identify_frame *)phy->frame_rcvd;
642 id->dev_type = phy->identify.device_type;
643 id->initiator_bits = SAS_PROTOCOL_ALL;
644 id->target_bits = phy->identify.target_port_protocols;
645 } else if (phy->phy_type & PORT_TYPE_SATA) {
649 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
650 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
653 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
655 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
656 struct hisi_sas_device *sas_dev = NULL;
657 int last = hisi_hba->last_dev_id;
658 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
661 spin_lock(&hisi_hba->lock);
662 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
663 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
664 int queue = i % hisi_hba->queue_count;
665 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
667 hisi_hba->devices[i].device_id = i;
668 sas_dev = &hisi_hba->devices[i];
669 sas_dev->dev_status = HISI_SAS_DEV_INIT;
670 sas_dev->dev_type = device->dev_type;
671 sas_dev->hisi_hba = hisi_hba;
672 sas_dev->sas_device = device;
674 spin_lock_init(&sas_dev->lock);
675 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
680 hisi_hba->last_dev_id = i;
681 spin_unlock(&hisi_hba->lock);
686 #define HISI_SAS_DISK_RECOVER_CNT 3
687 static int hisi_sas_init_device(struct domain_device *device)
689 int rc = TMF_RESP_FUNC_COMPLETE;
691 struct hisi_sas_tmf_task tmf_task;
692 int retry = HISI_SAS_DISK_RECOVER_CNT;
693 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
694 struct device *dev = hisi_hba->dev;
695 struct sas_phy *local_phy;
697 switch (device->dev_type) {
699 int_to_scsilun(0, &lun);
701 tmf_task.tmf = TMF_CLEAR_TASK_SET;
702 while (retry-- > 0) {
703 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
705 if (rc == TMF_RESP_FUNC_COMPLETE) {
706 hisi_sas_release_task(hisi_hba, device);
713 case SAS_SATA_PM_PORT:
714 case SAS_SATA_PENDING:
716 * send HARD RESET to clear previous affiliation of
719 local_phy = sas_get_local_phy(device);
720 if (!scsi_is_sas_phy_local(local_phy) &&
721 !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
722 unsigned long deadline = ata_deadline(jiffies, 20000);
723 struct sata_device *sata_dev = &device->sata_dev;
724 struct ata_host *ata_host = sata_dev->ata_host;
725 struct ata_port_operations *ops = ata_host->ops;
726 struct ata_port *ap = sata_dev->ap;
727 struct ata_link *link;
728 unsigned int classes;
730 ata_for_each_link(link, ap, EDGE)
731 rc = ops->hardreset(link, &classes,
734 sas_put_local_phy(local_phy);
736 dev_warn(dev, "SATA disk hardreset fail: %d\n", rc);
740 while (retry-- > 0) {
741 rc = hisi_sas_softreset_ata_disk(device);
753 static int hisi_sas_dev_found(struct domain_device *device)
755 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
756 struct domain_device *parent_dev = device->parent;
757 struct hisi_sas_device *sas_dev;
758 struct device *dev = hisi_hba->dev;
761 if (hisi_hba->hw->alloc_dev)
762 sas_dev = hisi_hba->hw->alloc_dev(device);
764 sas_dev = hisi_sas_alloc_dev(device);
766 dev_err(dev, "fail alloc dev: max support %d devices\n",
767 HISI_SAS_MAX_DEVICES);
771 device->lldd_dev = sas_dev;
772 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
774 if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
776 u8 phy_num = parent_dev->ex_dev.num_phys;
779 for (phy_no = 0; phy_no < phy_num; phy_no++) {
780 phy = &parent_dev->ex_dev.ex_phy[phy_no];
781 if (SAS_ADDR(phy->attached_sas_addr) ==
782 SAS_ADDR(device->sas_addr))
786 if (phy_no == phy_num) {
787 dev_info(dev, "dev found: no attached "
788 "dev:%016llx at ex:%016llx\n",
789 SAS_ADDR(device->sas_addr),
790 SAS_ADDR(parent_dev->sas_addr));
796 dev_info(dev, "dev[%d:%x] found\n",
797 sas_dev->device_id, sas_dev->dev_type);
799 rc = hisi_sas_init_device(device);
802 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
806 hisi_sas_dev_gone(device);
810 int hisi_sas_slave_configure(struct scsi_device *sdev)
812 struct domain_device *dev = sdev_to_domain_dev(sdev);
813 int ret = sas_slave_configure(sdev);
817 if (!dev_is_sata(dev))
818 sas_change_queue_depth(sdev, 64);
822 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
824 void hisi_sas_scan_start(struct Scsi_Host *shost)
826 struct hisi_hba *hisi_hba = shost_priv(shost);
828 hisi_hba->hw->phys_init(hisi_hba);
830 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
832 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
834 struct hisi_hba *hisi_hba = shost_priv(shost);
835 struct sas_ha_struct *sha = &hisi_hba->sha;
837 /* Wait for PHY up interrupt to occur */
844 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
846 static void hisi_sas_phyup_work(struct work_struct *work)
848 struct hisi_sas_phy *phy =
849 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
850 struct hisi_hba *hisi_hba = phy->hisi_hba;
851 struct asd_sas_phy *sas_phy = &phy->sas_phy;
852 int phy_no = sas_phy->id;
854 if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
855 hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
856 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
859 static void hisi_sas_linkreset_work(struct work_struct *work)
861 struct hisi_sas_phy *phy =
862 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
863 struct asd_sas_phy *sas_phy = &phy->sas_phy;
865 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
868 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
869 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
870 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
873 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
874 enum hisi_sas_phy_event event)
876 struct hisi_hba *hisi_hba = phy->hisi_hba;
878 if (WARN_ON(event >= HISI_PHYES_NUM))
881 return queue_work(hisi_hba->wq, &phy->works[event]);
883 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
885 static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
887 struct hisi_sas_phy *phy = from_timer(phy, t, timer);
888 struct hisi_hba *hisi_hba = phy->hisi_hba;
889 struct device *dev = hisi_hba->dev;
890 int phy_no = phy->sas_phy.id;
892 dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
893 hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
896 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
898 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
899 struct device *dev = hisi_hba->dev;
901 dev_dbg(dev, "phy%d OOB ready\n", phy_no);
902 if (phy->phy_attached)
905 if (!timer_pending(&phy->timer)) {
906 phy->timer.expires = jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT * HZ;
907 add_timer(&phy->timer);
910 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
912 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
914 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
915 struct asd_sas_phy *sas_phy = &phy->sas_phy;
918 phy->hisi_hba = hisi_hba;
920 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
921 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
922 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
923 sas_phy->class = SAS;
924 sas_phy->iproto = SAS_PROTOCOL_ALL;
926 sas_phy->type = PHY_TYPE_PHYSICAL;
927 sas_phy->role = PHY_ROLE_INITIATOR;
928 sas_phy->oob_mode = OOB_NOT_CONNECTED;
929 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
930 sas_phy->id = phy_no;
931 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
932 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
933 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
934 sas_phy->lldd_phy = phy;
936 for (i = 0; i < HISI_PHYES_NUM; i++)
937 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
939 spin_lock_init(&phy->lock);
941 timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
944 /* Wrapper to ensure we track hisi_sas_phy.enable properly */
945 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable)
947 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
948 struct asd_sas_phy *aphy = &phy->sas_phy;
949 struct sas_phy *sphy = aphy->phy;
952 spin_lock_irqsave(&phy->lock, flags);
955 /* We may have been enabled already; if so, don't touch */
957 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
958 hisi_hba->hw->phy_start(hisi_hba, phy_no);
960 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
961 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
963 phy->enable = enable;
964 spin_unlock_irqrestore(&phy->lock, flags);
966 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable);
968 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
970 struct sas_ha_struct *sas_ha = sas_phy->ha;
971 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
972 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
973 struct asd_sas_port *sas_port = sas_phy->port;
974 struct hisi_sas_port *port;
980 port = to_hisi_sas_port(sas_port);
981 spin_lock_irqsave(&hisi_hba->lock, flags);
982 port->port_attached = 1;
983 port->id = phy->port_id;
985 sas_port->lldd_port = port;
986 spin_unlock_irqrestore(&hisi_hba->lock, flags);
989 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
990 struct hisi_sas_slot *slot)
994 struct task_status_struct *ts;
996 ts = &task->task_status;
998 ts->resp = SAS_TASK_COMPLETE;
999 ts->stat = SAS_ABORTED_TASK;
1000 spin_lock_irqsave(&task->task_state_lock, flags);
1001 task->task_state_flags &=
1002 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1003 if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
1004 task->task_state_flags |= SAS_TASK_STATE_DONE;
1005 spin_unlock_irqrestore(&task->task_state_lock, flags);
1008 hisi_sas_slot_task_free(hisi_hba, task, slot);
1011 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
1012 struct domain_device *device)
1014 struct hisi_sas_slot *slot, *slot2;
1015 struct hisi_sas_device *sas_dev = device->lldd_dev;
1017 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
1018 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
1021 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
1023 struct hisi_sas_device *sas_dev;
1024 struct domain_device *device;
1027 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1028 sas_dev = &hisi_hba->devices[i];
1029 device = sas_dev->sas_device;
1031 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
1035 hisi_sas_release_task(hisi_hba, device);
1038 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
1040 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
1041 struct domain_device *device)
1043 if (hisi_hba->hw->dereg_device)
1044 hisi_hba->hw->dereg_device(hisi_hba, device);
1047 static void hisi_sas_dev_gone(struct domain_device *device)
1049 struct hisi_sas_device *sas_dev = device->lldd_dev;
1050 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1051 struct device *dev = hisi_hba->dev;
1054 dev_info(dev, "dev[%d:%x] is gone\n",
1055 sas_dev->device_id, sas_dev->dev_type);
1057 down(&hisi_hba->sem);
1058 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
1059 hisi_sas_internal_task_abort(hisi_hba, device,
1060 HISI_SAS_INT_ABT_DEV, 0);
1062 hisi_sas_dereg_device(hisi_hba, device);
1064 ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
1065 device->lldd_dev = NULL;
1068 if (hisi_hba->hw->free_device)
1069 hisi_hba->hw->free_device(sas_dev);
1071 /* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */
1073 sas_dev->dev_type = SAS_PHY_UNUSED;
1074 sas_dev->sas_device = NULL;
1078 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
1080 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
1083 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
1084 struct sas_phy_linkrates *r)
1086 struct sas_phy_linkrates _r;
1088 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1089 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1090 enum sas_linkrate min, max;
1092 if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
1095 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1096 max = sas_phy->phy->maximum_linkrate;
1097 min = r->minimum_linkrate;
1098 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1099 max = r->maximum_linkrate;
1100 min = sas_phy->phy->minimum_linkrate;
1104 _r.maximum_linkrate = max;
1105 _r.minimum_linkrate = min;
1107 sas_phy->phy->maximum_linkrate = max;
1108 sas_phy->phy->minimum_linkrate = min;
1110 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1112 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
1113 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1118 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
1121 struct sas_ha_struct *sas_ha = sas_phy->ha;
1122 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1123 int phy_no = sas_phy->id;
1126 case PHY_FUNC_HARD_RESET:
1127 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
1130 case PHY_FUNC_LINK_RESET:
1131 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1133 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1136 case PHY_FUNC_DISABLE:
1137 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1140 case PHY_FUNC_SET_LINK_RATE:
1141 return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1142 case PHY_FUNC_GET_EVENTS:
1143 if (hisi_hba->hw->get_events) {
1144 hisi_hba->hw->get_events(hisi_hba, phy_no);
1148 case PHY_FUNC_RELEASE_SPINUP_HOLD:
1155 static void hisi_sas_task_done(struct sas_task *task)
1157 del_timer(&task->slow_task->timer);
1158 complete(&task->slow_task->completion);
1161 static void hisi_sas_tmf_timedout(struct timer_list *t)
1163 struct sas_task_slow *slow = from_timer(slow, t, timer);
1164 struct sas_task *task = slow->task;
1165 unsigned long flags;
1166 bool is_completed = true;
1168 spin_lock_irqsave(&task->task_state_lock, flags);
1169 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1170 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1171 is_completed = false;
1173 spin_unlock_irqrestore(&task->task_state_lock, flags);
1176 complete(&task->slow_task->completion);
1179 #define TASK_TIMEOUT 20
1180 #define TASK_RETRY 3
1181 #define INTERNAL_ABORT_TIMEOUT 6
1182 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1183 void *parameter, u32 para_len,
1184 struct hisi_sas_tmf_task *tmf)
1186 struct hisi_sas_device *sas_dev = device->lldd_dev;
1187 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1188 struct device *dev = hisi_hba->dev;
1189 struct sas_task *task;
1192 for (retry = 0; retry < TASK_RETRY; retry++) {
1193 task = sas_alloc_slow_task(GFP_KERNEL);
1198 task->task_proto = device->tproto;
1200 if (dev_is_sata(device)) {
1201 task->ata_task.device_control_reg_update = 1;
1202 memcpy(&task->ata_task.fis, parameter, para_len);
1204 memcpy(&task->ssp_task, parameter, para_len);
1206 task->task_done = hisi_sas_task_done;
1208 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1209 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT * HZ;
1210 add_timer(&task->slow_task->timer);
1212 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1215 del_timer(&task->slow_task->timer);
1216 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1221 wait_for_completion(&task->slow_task->completion);
1222 res = TMF_RESP_FUNC_FAILED;
1223 /* Even TMF timed out, return direct. */
1224 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1225 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1226 struct hisi_sas_slot *slot = task->lldd_task;
1228 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1230 struct hisi_sas_cq *cq =
1231 &hisi_hba->cq[slot->dlvry_queue];
1233 * sync irq to avoid free'ing task
1234 * before using task in IO completion
1236 synchronize_irq(cq->irq_no);
1242 dev_err(dev, "abort tmf: TMF task timeout\n");
1245 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1246 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1247 res = TMF_RESP_FUNC_COMPLETE;
1251 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1252 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1253 res = TMF_RESP_FUNC_SUCC;
1257 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1258 task->task_status.stat == SAS_DATA_UNDERRUN) {
1259 /* no error, but return the number of bytes of
1262 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x sts 0x%x underrun\n",
1263 SAS_ADDR(device->sas_addr),
1264 task->task_status.resp,
1265 task->task_status.stat);
1266 res = task->task_status.residual;
1270 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1271 task->task_status.stat == SAS_DATA_OVERRUN) {
1272 dev_warn(dev, "abort tmf: blocked task error\n");
1277 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1278 task->task_status.stat == SAS_OPEN_REJECT) {
1279 dev_warn(dev, "abort tmf: open reject failed\n");
1282 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x status 0x%x\n",
1283 SAS_ADDR(device->sas_addr),
1284 task->task_status.resp,
1285 task->task_status.stat);
1287 sas_free_task(task);
1291 if (retry == TASK_RETRY)
1292 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1293 sas_free_task(task);
1297 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1298 bool reset, int pmp, u8 *fis)
1300 struct ata_taskfile tf;
1302 ata_tf_init(dev, &tf);
1306 tf.ctl &= ~ATA_SRST;
1307 tf.command = ATA_CMD_DEV_RESET;
1308 ata_tf_to_fis(&tf, pmp, 0, fis);
1311 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1314 struct ata_port *ap = device->sata_dev.ap;
1315 struct ata_link *link;
1316 int rc = TMF_RESP_FUNC_FAILED;
1317 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1318 struct device *dev = hisi_hba->dev;
1319 int s = sizeof(struct host_to_dev_fis);
1321 ata_for_each_link(link, ap, EDGE) {
1322 int pmp = sata_srst_pmp(link);
1324 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1325 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1326 if (rc != TMF_RESP_FUNC_COMPLETE)
1330 if (rc == TMF_RESP_FUNC_COMPLETE) {
1331 ata_for_each_link(link, ap, EDGE) {
1332 int pmp = sata_srst_pmp(link);
1334 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1335 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1337 if (rc != TMF_RESP_FUNC_COMPLETE)
1338 dev_err(dev, "ata disk de-reset failed\n");
1341 dev_err(dev, "ata disk reset failed\n");
1344 if (rc == TMF_RESP_FUNC_COMPLETE)
1345 hisi_sas_release_task(hisi_hba, device);
1350 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1351 u8 *lun, struct hisi_sas_tmf_task *tmf)
1353 struct sas_ssp_task ssp_task;
1355 if (!(device->tproto & SAS_PROTOCOL_SSP))
1356 return TMF_RESP_FUNC_ESUPP;
1358 memcpy(ssp_task.LUN, lun, 8);
1360 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1361 sizeof(ssp_task), tmf);
1364 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1366 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1369 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1370 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1371 struct domain_device *device = sas_dev->sas_device;
1372 struct asd_sas_port *sas_port;
1373 struct hisi_sas_port *port;
1374 struct hisi_sas_phy *phy = NULL;
1375 struct asd_sas_phy *sas_phy;
1377 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1378 || !device || !device->port)
1381 sas_port = device->port;
1382 port = to_hisi_sas_port(sas_port);
1384 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1385 if (state & BIT(sas_phy->id)) {
1386 phy = sas_phy->lldd_phy;
1391 port->id = phy->port_id;
1393 /* Update linkrate of directly attached device. */
1394 if (!device->parent)
1395 device->linkrate = phy->sas_phy.linkrate;
1397 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1403 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state)
1405 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1406 struct asd_sas_port *_sas_port = NULL;
1409 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1410 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1411 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1412 struct asd_sas_port *sas_port = sas_phy->port;
1413 bool do_port_check = _sas_port != sas_port;
1415 if (!sas_phy->phy->enabled)
1418 /* Report PHY state change to libsas */
1419 if (state & BIT(phy_no)) {
1420 if (do_port_check && sas_port && sas_port->port_dev) {
1421 struct domain_device *dev = sas_port->port_dev;
1423 _sas_port = sas_port;
1425 if (dev_is_expander(dev->dev_type))
1426 sas_ha->notify_port_event(sas_phy,
1427 PORTE_BROADCAST_RCVD);
1430 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1436 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1438 struct hisi_sas_device *sas_dev;
1439 struct domain_device *device;
1442 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1443 sas_dev = &hisi_hba->devices[i];
1444 device = sas_dev->sas_device;
1446 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1449 hisi_sas_init_device(device);
1453 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1454 struct asd_sas_port *sas_port,
1455 struct domain_device *device)
1457 struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1458 struct ata_port *ap = device->sata_dev.ap;
1459 struct device *dev = hisi_hba->dev;
1460 int s = sizeof(struct host_to_dev_fis);
1461 int rc = TMF_RESP_FUNC_FAILED;
1462 struct asd_sas_phy *sas_phy;
1463 struct ata_link *link;
1467 state = hisi_hba->hw->get_phys_state(hisi_hba);
1468 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1469 if (!(state & BIT(sas_phy->id)))
1472 ata_for_each_link(link, ap, EDGE) {
1473 int pmp = sata_srst_pmp(link);
1475 tmf_task.phy_id = sas_phy->id;
1476 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1477 rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1479 if (rc != TMF_RESP_FUNC_COMPLETE) {
1480 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1488 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1490 struct device *dev = hisi_hba->dev;
1493 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1494 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1495 struct domain_device *device = sas_dev->sas_device;
1497 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1500 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1501 HISI_SAS_INT_ABT_DEV, 0);
1503 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1506 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1507 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1508 struct asd_sas_port *sas_port = &port->sas_port;
1509 struct domain_device *port_dev = sas_port->port_dev;
1510 struct domain_device *device;
1512 if (!port_dev || !dev_is_expander(port_dev->dev_type))
1515 /* Try to find a SATA device */
1516 list_for_each_entry(device, &sas_port->dev_list,
1518 if (dev_is_sata(device)) {
1519 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1528 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1530 struct Scsi_Host *shost = hisi_hba->shost;
1532 down(&hisi_hba->sem);
1533 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1535 scsi_block_requests(shost);
1536 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1538 if (timer_pending(&hisi_hba->timer))
1539 del_timer_sync(&hisi_hba->timer);
1541 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1543 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1545 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1547 struct Scsi_Host *shost = hisi_hba->shost;
1550 /* Init and wait for PHYs to come up and all libsas event finished. */
1551 hisi_hba->hw->phys_init(hisi_hba);
1553 hisi_sas_refresh_port_id(hisi_hba);
1554 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1556 if (hisi_hba->reject_stp_links_msk)
1557 hisi_sas_terminate_stp_reject(hisi_hba);
1558 hisi_sas_reset_init_all_devices(hisi_hba);
1560 scsi_unblock_requests(shost);
1561 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1563 state = hisi_hba->hw->get_phys_state(hisi_hba);
1564 hisi_sas_rescan_topology(hisi_hba, state);
1566 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1568 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1570 struct device *dev = hisi_hba->dev;
1571 struct Scsi_Host *shost = hisi_hba->shost;
1574 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
1575 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
1577 if (!hisi_hba->hw->soft_reset)
1580 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1583 dev_info(dev, "controller resetting...\n");
1584 hisi_sas_controller_reset_prepare(hisi_hba);
1586 rc = hisi_hba->hw->soft_reset(hisi_hba);
1588 dev_warn(dev, "controller reset failed (%d)\n", rc);
1589 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1591 scsi_unblock_requests(shost);
1592 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1596 hisi_sas_controller_reset_done(hisi_hba);
1597 dev_info(dev, "controller reset complete\n");
1602 static int hisi_sas_abort_task(struct sas_task *task)
1604 struct scsi_lun lun;
1605 struct hisi_sas_tmf_task tmf_task;
1606 struct domain_device *device = task->dev;
1607 struct hisi_sas_device *sas_dev = device->lldd_dev;
1608 struct hisi_hba *hisi_hba;
1610 int rc = TMF_RESP_FUNC_FAILED;
1611 unsigned long flags;
1614 return TMF_RESP_FUNC_FAILED;
1616 hisi_hba = dev_to_hisi_hba(task->dev);
1617 dev = hisi_hba->dev;
1619 spin_lock_irqsave(&task->task_state_lock, flags);
1620 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1621 struct hisi_sas_slot *slot = task->lldd_task;
1622 struct hisi_sas_cq *cq;
1626 * sync irq to avoid free'ing task
1627 * before using task in IO completion
1629 cq = &hisi_hba->cq[slot->dlvry_queue];
1630 synchronize_irq(cq->irq_no);
1632 spin_unlock_irqrestore(&task->task_state_lock, flags);
1633 rc = TMF_RESP_FUNC_COMPLETE;
1636 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1637 spin_unlock_irqrestore(&task->task_state_lock, flags);
1639 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1640 struct scsi_cmnd *cmnd = task->uldd_task;
1641 struct hisi_sas_slot *slot = task->lldd_task;
1642 u16 tag = slot->idx;
1645 int_to_scsilun(cmnd->device->lun, &lun);
1646 tmf_task.tmf = TMF_ABORT_TASK;
1647 tmf_task.tag_of_task_to_be_managed = tag;
1649 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1652 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1653 HISI_SAS_INT_ABT_CMD, tag);
1655 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1656 return TMF_RESP_FUNC_FAILED;
1660 * If the TMF finds that the IO is not in the device and also
1661 * the internal abort does not succeed, then it is safe to
1663 * Note: if the internal abort succeeds then the slot
1664 * will have already been completed
1666 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1667 if (task->lldd_task)
1668 hisi_sas_do_release_task(hisi_hba, task, slot);
1670 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1671 task->task_proto & SAS_PROTOCOL_STP) {
1672 if (task->dev->dev_type == SAS_SATA_DEV) {
1673 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1674 HISI_SAS_INT_ABT_DEV,
1677 dev_err(dev, "abort task: internal abort failed\n");
1680 hisi_sas_dereg_device(hisi_hba, device);
1681 rc = hisi_sas_softreset_ata_disk(device);
1683 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1685 struct hisi_sas_slot *slot = task->lldd_task;
1686 u32 tag = slot->idx;
1687 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1689 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1690 HISI_SAS_INT_ABT_CMD, tag);
1691 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1694 * sync irq to avoid free'ing task
1695 * before using task in IO completion
1697 synchronize_irq(cq->irq_no);
1703 if (rc != TMF_RESP_FUNC_COMPLETE)
1704 dev_notice(dev, "abort task: rc=%d\n", rc);
1708 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1710 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1711 struct device *dev = hisi_hba->dev;
1712 struct hisi_sas_tmf_task tmf_task;
1715 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1716 HISI_SAS_INT_ABT_DEV, 0);
1718 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1719 return TMF_RESP_FUNC_FAILED;
1721 hisi_sas_dereg_device(hisi_hba, device);
1723 tmf_task.tmf = TMF_ABORT_TASK_SET;
1724 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1726 if (rc == TMF_RESP_FUNC_COMPLETE)
1727 hisi_sas_release_task(hisi_hba, device);
1732 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1734 struct hisi_sas_tmf_task tmf_task;
1737 tmf_task.tmf = TMF_CLEAR_ACA;
1738 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1743 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1745 struct sas_phy *local_phy = sas_get_local_phy(device);
1746 struct hisi_sas_device *sas_dev = device->lldd_dev;
1747 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1748 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1749 DECLARE_COMPLETION_ONSTACK(phyreset);
1752 if (!local_phy->enabled) {
1753 sas_put_local_phy(local_phy);
1757 if (scsi_is_sas_phy_local(local_phy)) {
1758 struct asd_sas_phy *sas_phy =
1759 sas_ha->sas_phy[local_phy->number];
1760 struct hisi_sas_phy *phy =
1761 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1763 phy->reset_completion = &phyreset;
1766 reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
1767 !dev_is_sata(device)) ? true : false;
1769 rc = sas_phy_reset(local_phy, reset_type);
1770 sas_put_local_phy(local_phy);
1772 if (scsi_is_sas_phy_local(local_phy)) {
1773 struct asd_sas_phy *sas_phy =
1774 sas_ha->sas_phy[local_phy->number];
1775 struct hisi_sas_phy *phy =
1776 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1777 int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1778 unsigned long flags;
1780 spin_lock_irqsave(&phy->lock, flags);
1781 phy->reset_completion = NULL;
1783 spin_unlock_irqrestore(&phy->lock, flags);
1785 /* report PHY down if timed out */
1787 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
1788 } else if (sas_dev->dev_status != HISI_SAS_DEV_INIT) {
1790 * If in init state, we rely on caller to wait for link to be
1791 * ready; otherwise, except phy reset is fail, delay.
1800 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1802 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1803 struct device *dev = hisi_hba->dev;
1806 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1807 HISI_SAS_INT_ABT_DEV, 0);
1809 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1810 return TMF_RESP_FUNC_FAILED;
1812 hisi_sas_dereg_device(hisi_hba, device);
1814 if (dev_is_sata(device)) {
1815 rc = hisi_sas_softreset_ata_disk(device);
1816 if (rc == TMF_RESP_FUNC_FAILED)
1817 return TMF_RESP_FUNC_FAILED;
1820 rc = hisi_sas_debug_I_T_nexus_reset(device);
1822 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1823 hisi_sas_release_task(hisi_hba, device);
1828 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1830 struct hisi_sas_device *sas_dev = device->lldd_dev;
1831 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1832 struct device *dev = hisi_hba->dev;
1833 int rc = TMF_RESP_FUNC_FAILED;
1835 /* Clear internal IO and then lu reset */
1836 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1837 HISI_SAS_INT_ABT_DEV, 0);
1839 dev_err(dev, "lu_reset: internal abort failed\n");
1842 hisi_sas_dereg_device(hisi_hba, device);
1844 if (dev_is_sata(device)) {
1845 struct sas_phy *phy;
1847 phy = sas_get_local_phy(device);
1849 rc = sas_phy_reset(phy, true);
1852 hisi_sas_release_task(hisi_hba, device);
1853 sas_put_local_phy(phy);
1855 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1857 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1858 if (rc == TMF_RESP_FUNC_COMPLETE)
1859 hisi_sas_release_task(hisi_hba, device);
1862 if (rc != TMF_RESP_FUNC_COMPLETE)
1863 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1864 sas_dev->device_id, rc);
1868 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1870 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1871 struct device *dev = hisi_hba->dev;
1872 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1875 queue_work(hisi_hba->wq, &r.work);
1876 wait_for_completion(r.completion);
1878 return TMF_RESP_FUNC_FAILED;
1880 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1881 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1882 struct domain_device *device = sas_dev->sas_device;
1884 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1885 dev_is_expander(device->dev_type))
1888 rc = hisi_sas_debug_I_T_nexus_reset(device);
1889 if (rc != TMF_RESP_FUNC_COMPLETE)
1890 dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
1891 sas_dev->device_id, rc);
1894 hisi_sas_release_tasks(hisi_hba);
1896 return TMF_RESP_FUNC_COMPLETE;
1899 static int hisi_sas_query_task(struct sas_task *task)
1901 struct scsi_lun lun;
1902 struct hisi_sas_tmf_task tmf_task;
1903 int rc = TMF_RESP_FUNC_FAILED;
1905 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1906 struct scsi_cmnd *cmnd = task->uldd_task;
1907 struct domain_device *device = task->dev;
1908 struct hisi_sas_slot *slot = task->lldd_task;
1909 u32 tag = slot->idx;
1911 int_to_scsilun(cmnd->device->lun, &lun);
1912 tmf_task.tmf = TMF_QUERY_TASK;
1913 tmf_task.tag_of_task_to_be_managed = tag;
1915 rc = hisi_sas_debug_issue_ssp_tmf(device,
1919 /* The task is still in Lun, release it then */
1920 case TMF_RESP_FUNC_SUCC:
1921 /* The task is not in Lun or failed, reset the phy */
1922 case TMF_RESP_FUNC_FAILED:
1923 case TMF_RESP_FUNC_COMPLETE:
1926 rc = TMF_RESP_FUNC_FAILED;
1934 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1935 struct sas_task *task, int abort_flag,
1936 int task_tag, struct hisi_sas_dq *dq)
1938 struct domain_device *device = task->dev;
1939 struct hisi_sas_device *sas_dev = device->lldd_dev;
1940 struct device *dev = hisi_hba->dev;
1941 struct hisi_sas_port *port;
1942 struct hisi_sas_slot *slot;
1943 struct asd_sas_port *sas_port = device->port;
1944 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1945 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1946 unsigned long flags;
1949 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1955 port = to_hisi_sas_port(sas_port);
1957 /* simply get a slot and send abort command */
1958 rc = hisi_sas_slot_index_alloc(hisi_hba, NULL);
1963 slot = &hisi_hba->slot_info[slot_idx];
1965 spin_lock(&dq->lock);
1966 wr_q_index = dq->wr_point;
1967 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
1968 list_add_tail(&slot->delivery, &dq->list);
1969 spin_unlock(&dq->lock);
1970 spin_lock(&sas_dev->lock);
1971 list_add_tail(&slot->entry, &sas_dev->list);
1972 spin_unlock(&sas_dev->lock);
1974 dlvry_queue = dq->id;
1975 dlvry_queue_slot = wr_q_index;
1977 slot->device_id = sas_dev->device_id;
1978 slot->n_elem = n_elem;
1979 slot->dlvry_queue = dlvry_queue;
1980 slot->dlvry_queue_slot = dlvry_queue_slot;
1981 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1982 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1985 slot->is_internal = true;
1986 task->lldd_task = slot;
1988 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1989 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1990 memset(hisi_sas_status_buf_addr_mem(slot), 0,
1991 sizeof(struct hisi_sas_err_record));
1993 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1994 abort_flag, task_tag);
1996 spin_lock_irqsave(&task->task_state_lock, flags);
1997 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1998 spin_unlock_irqrestore(&task->task_state_lock, flags);
1999 WRITE_ONCE(slot->ready, 1);
2000 /* send abort command to the chip */
2001 spin_lock(&dq->lock);
2002 hisi_hba->hw->start_delivery(dq);
2003 spin_unlock(&dq->lock);
2008 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
2014 * _hisi_sas_internal_task_abort -- execute an internal
2015 * abort command for single IO command or a device
2016 * @hisi_hba: host controller struct
2017 * @device: domain device
2018 * @abort_flag: mode of operation, device or single IO
2019 * @tag: tag of IO to be aborted (only relevant to single
2021 * @dq: delivery queue for this internal abort command
2024 _hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2025 struct domain_device *device, int abort_flag,
2026 int tag, struct hisi_sas_dq *dq)
2028 struct sas_task *task;
2029 struct hisi_sas_device *sas_dev = device->lldd_dev;
2030 struct device *dev = hisi_hba->dev;
2034 * The interface is not realized means this HW don't support internal
2035 * abort, or don't need to do internal abort. Then here, we return
2036 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
2037 * the internal abort has been executed and returned CQ.
2039 if (!hisi_hba->hw->prep_abort)
2040 return TMF_RESP_FUNC_FAILED;
2042 task = sas_alloc_slow_task(GFP_KERNEL);
2047 task->task_proto = device->tproto;
2048 task->task_done = hisi_sas_task_done;
2049 task->slow_task->timer.function = hisi_sas_tmf_timedout;
2050 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT * HZ;
2051 add_timer(&task->slow_task->timer);
2053 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
2054 task, abort_flag, tag, dq);
2056 del_timer(&task->slow_task->timer);
2057 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
2061 wait_for_completion(&task->slow_task->completion);
2062 res = TMF_RESP_FUNC_FAILED;
2064 /* Internal abort timed out */
2065 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
2066 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
2067 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
2069 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
2070 struct hisi_sas_slot *slot = task->lldd_task;
2073 struct hisi_sas_cq *cq =
2074 &hisi_hba->cq[slot->dlvry_queue];
2076 * sync irq to avoid free'ing task
2077 * before using task in IO completion
2079 synchronize_irq(cq->irq_no);
2082 dev_err(dev, "internal task abort: timeout and not done.\n");
2087 dev_err(dev, "internal task abort: timeout.\n");
2090 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2091 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
2092 res = TMF_RESP_FUNC_COMPLETE;
2096 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2097 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
2098 res = TMF_RESP_FUNC_SUCC;
2103 dev_dbg(dev, "internal task abort: task to dev %016llx task=%pK resp: 0x%x sts 0x%x\n",
2104 SAS_ADDR(device->sas_addr), task,
2105 task->task_status.resp, /* 0 is complete, -1 is undelivered */
2106 task->task_status.stat);
2107 sas_free_task(task);
2113 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2114 struct domain_device *device,
2115 int abort_flag, int tag)
2117 struct hisi_sas_slot *slot;
2118 struct device *dev = hisi_hba->dev;
2119 struct hisi_sas_dq *dq;
2122 switch (abort_flag) {
2123 case HISI_SAS_INT_ABT_CMD:
2124 slot = &hisi_hba->slot_info[tag];
2125 dq = &hisi_hba->dq[slot->dlvry_queue];
2126 return _hisi_sas_internal_task_abort(hisi_hba, device,
2127 abort_flag, tag, dq);
2128 case HISI_SAS_INT_ABT_DEV:
2129 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2130 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2131 const struct cpumask *mask = cq->irq_mask;
2133 if (mask && !cpumask_intersects(cpu_online_mask, mask))
2135 dq = &hisi_hba->dq[i];
2136 rc = _hisi_sas_internal_task_abort(hisi_hba, device,
2144 dev_err(dev, "Unrecognised internal abort flag (%d)\n",
2152 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
2154 hisi_sas_port_notify_formed(sas_phy);
2157 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
2158 u8 reg_index, u8 reg_count, u8 *write_data)
2160 struct hisi_hba *hisi_hba = sha->lldd_ha;
2162 if (!hisi_hba->hw->write_gpio)
2165 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
2166 reg_index, reg_count, write_data);
2169 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
2171 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2172 struct sas_phy *sphy = sas_phy->phy;
2173 unsigned long flags;
2175 phy->phy_attached = 0;
2179 spin_lock_irqsave(&phy->lock, flags);
2181 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
2183 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
2184 spin_unlock_irqrestore(&phy->lock, flags);
2187 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
2189 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2190 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2191 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
2192 struct device *dev = hisi_hba->dev;
2195 /* Phy down but ready */
2196 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
2197 hisi_sas_port_notify_formed(sas_phy);
2199 struct hisi_sas_port *port = phy->port;
2201 if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
2203 dev_info(dev, "ignore flutter phy%d down\n", phy_no);
2206 /* Phy down and not ready */
2207 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
2208 sas_phy_disconnected(sas_phy);
2211 if (phy->phy_type & PORT_TYPE_SAS) {
2212 int port_id = port->id;
2214 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2216 port->port_attached = 0;
2217 } else if (phy->phy_type & PORT_TYPE_SATA)
2218 port->port_attached = 0;
2220 hisi_sas_phy_disconnected(phy);
2223 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2225 void hisi_sas_sync_irqs(struct hisi_hba *hisi_hba)
2229 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2230 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2232 synchronize_irq(cq->irq_no);
2235 EXPORT_SYMBOL_GPL(hisi_sas_sync_irqs);
2237 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type)
2239 struct hisi_hba *hisi_hba = shost_priv(shost);
2241 if (reset_type != SCSI_ADAPTER_RESET)
2244 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2248 EXPORT_SYMBOL_GPL(hisi_sas_host_reset);
2250 struct scsi_transport_template *hisi_sas_stt;
2251 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2253 static struct sas_domain_function_template hisi_sas_transport_ops = {
2254 .lldd_dev_found = hisi_sas_dev_found,
2255 .lldd_dev_gone = hisi_sas_dev_gone,
2256 .lldd_execute_task = hisi_sas_queue_command,
2257 .lldd_control_phy = hisi_sas_control_phy,
2258 .lldd_abort_task = hisi_sas_abort_task,
2259 .lldd_abort_task_set = hisi_sas_abort_task_set,
2260 .lldd_clear_aca = hisi_sas_clear_aca,
2261 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
2262 .lldd_lu_reset = hisi_sas_lu_reset,
2263 .lldd_query_task = hisi_sas_query_task,
2264 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
2265 .lldd_port_formed = hisi_sas_port_formed,
2266 .lldd_write_gpio = hisi_sas_write_gpio,
2269 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2271 int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS;
2272 struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
2274 for (i = 0; i < hisi_hba->queue_count; i++) {
2275 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2276 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2277 struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
2279 s = sizeof(struct hisi_sas_cmd_hdr);
2280 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2281 memset(&cmd_hdr[j], 0, s);
2285 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2286 memset(hisi_hba->complete_hdr[i], 0, s);
2290 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2291 memset(hisi_hba->initial_fis, 0, s);
2293 s = max_command_entries * sizeof(struct hisi_sas_iost);
2294 memset(hisi_hba->iost, 0, s);
2296 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2297 memset(hisi_hba->breakpoint, 0, s);
2299 s = sizeof(struct hisi_sas_sata_breakpoint);
2300 for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
2301 memset(&sata_breakpoint[j], 0, s);
2303 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2305 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2307 struct device *dev = hisi_hba->dev;
2308 int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS;
2309 int max_command_entries_ru, sz_slot_buf_ru;
2310 int blk_cnt, slots_per_blk;
2312 sema_init(&hisi_hba->sem, 1);
2313 spin_lock_init(&hisi_hba->lock);
2314 for (i = 0; i < hisi_hba->n_phy; i++) {
2315 hisi_sas_phy_init(hisi_hba, i);
2316 hisi_hba->port[i].port_attached = 0;
2317 hisi_hba->port[i].id = -1;
2320 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2321 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2322 hisi_hba->devices[i].device_id = i;
2323 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
2326 for (i = 0; i < hisi_hba->queue_count; i++) {
2327 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2328 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2330 /* Completion queue structure */
2332 cq->hisi_hba = hisi_hba;
2334 /* Delivery queue structure */
2335 spin_lock_init(&dq->lock);
2336 INIT_LIST_HEAD(&dq->list);
2338 dq->hisi_hba = hisi_hba;
2340 /* Delivery queue */
2341 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2342 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2343 &hisi_hba->cmd_hdr_dma[i],
2345 if (!hisi_hba->cmd_hdr[i])
2348 /* Completion queue */
2349 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2350 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2351 &hisi_hba->complete_hdr_dma[i],
2353 if (!hisi_hba->complete_hdr[i])
2357 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2358 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2360 if (!hisi_hba->itct)
2363 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2364 sizeof(struct hisi_sas_slot),
2366 if (!hisi_hba->slot_info)
2369 /* roundup to avoid overly large block size */
2370 max_command_entries_ru = roundup(max_command_entries, 64);
2371 if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
2372 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
2374 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2375 sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2376 s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
2377 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2378 slots_per_blk = s / sz_slot_buf_ru;
2380 for (i = 0; i < blk_cnt; i++) {
2381 int slot_index = i * slots_per_blk;
2385 buf = dmam_alloc_coherent(dev, s, &buf_dma,
2390 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2391 struct hisi_sas_slot *slot;
2393 slot = &hisi_hba->slot_info[slot_index];
2395 slot->buf_dma = buf_dma;
2396 slot->idx = slot_index;
2398 buf += sz_slot_buf_ru;
2399 buf_dma += sz_slot_buf_ru;
2403 s = max_command_entries * sizeof(struct hisi_sas_iost);
2404 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2406 if (!hisi_hba->iost)
2409 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2410 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2411 &hisi_hba->breakpoint_dma,
2413 if (!hisi_hba->breakpoint)
2416 hisi_hba->slot_index_count = max_command_entries;
2417 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2418 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2419 if (!hisi_hba->slot_index_tags)
2422 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2423 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2424 &hisi_hba->initial_fis_dma,
2426 if (!hisi_hba->initial_fis)
2429 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2430 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2431 &hisi_hba->sata_breakpoint_dma,
2433 if (!hisi_hba->sata_breakpoint)
2436 hisi_sas_slot_index_init(hisi_hba);
2437 hisi_hba->last_slot_index = HISI_SAS_UNRESERVED_IPTT;
2439 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2440 if (!hisi_hba->wq) {
2441 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2449 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2451 void hisi_sas_free(struct hisi_hba *hisi_hba)
2455 for (i = 0; i < hisi_hba->n_phy; i++) {
2456 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
2458 del_timer_sync(&phy->timer);
2462 destroy_workqueue(hisi_hba->wq);
2464 EXPORT_SYMBOL_GPL(hisi_sas_free);
2466 void hisi_sas_rst_work_handler(struct work_struct *work)
2468 struct hisi_hba *hisi_hba =
2469 container_of(work, struct hisi_hba, rst_work);
2471 hisi_sas_controller_reset(hisi_hba);
2473 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2475 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2477 struct hisi_sas_rst *rst =
2478 container_of(work, struct hisi_sas_rst, work);
2480 if (!hisi_sas_controller_reset(rst->hisi_hba))
2482 complete(rst->completion);
2484 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2486 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2488 struct device *dev = hisi_hba->dev;
2489 struct platform_device *pdev = hisi_hba->platform_dev;
2490 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2493 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2495 dev_err(dev, "could not get property sas-addr\n");
2501 * These properties are only required for platform device-based
2502 * controller with DT firmware.
2504 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2505 "hisilicon,sas-syscon");
2506 if (IS_ERR(hisi_hba->ctrl)) {
2507 dev_err(dev, "could not get syscon\n");
2511 if (device_property_read_u32(dev, "ctrl-reset-reg",
2512 &hisi_hba->ctrl_reset_reg)) {
2513 dev_err(dev, "could not get property ctrl-reset-reg\n");
2517 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2518 &hisi_hba->ctrl_reset_sts_reg)) {
2519 dev_err(dev, "could not get property ctrl-reset-sts-reg\n");
2523 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2524 &hisi_hba->ctrl_clock_ena_reg)) {
2525 dev_err(dev, "could not get property ctrl-clock-ena-reg\n");
2530 refclk = devm_clk_get(dev, NULL);
2532 dev_dbg(dev, "no ref clk property\n");
2534 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2536 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2537 dev_err(dev, "could not get property phy-count\n");
2541 if (device_property_read_u32(dev, "queue-count",
2542 &hisi_hba->queue_count)) {
2543 dev_err(dev, "could not get property queue-count\n");
2549 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2551 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2552 const struct hisi_sas_hw *hw)
2554 struct resource *res;
2555 struct Scsi_Host *shost;
2556 struct hisi_hba *hisi_hba;
2557 struct device *dev = &pdev->dev;
2560 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2562 dev_err(dev, "scsi host alloc failed\n");
2565 hisi_hba = shost_priv(shost);
2567 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2569 hisi_hba->dev = dev;
2570 hisi_hba->platform_dev = pdev;
2571 hisi_hba->shost = shost;
2572 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2574 timer_setup(&hisi_hba->timer, NULL, 0);
2576 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2579 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2581 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2584 dev_err(dev, "No usable DMA addressing method\n");
2588 hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0);
2589 if (IS_ERR(hisi_hba->regs))
2592 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2594 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2595 if (IS_ERR(hisi_hba->sgpio_regs))
2599 if (hisi_sas_alloc(hisi_hba)) {
2600 hisi_sas_free(hisi_hba);
2606 scsi_host_put(shost);
2607 dev_err(dev, "shost alloc failed\n");
2611 int hisi_sas_probe(struct platform_device *pdev,
2612 const struct hisi_sas_hw *hw)
2614 struct Scsi_Host *shost;
2615 struct hisi_hba *hisi_hba;
2616 struct device *dev = &pdev->dev;
2617 struct asd_sas_phy **arr_phy;
2618 struct asd_sas_port **arr_port;
2619 struct sas_ha_struct *sha;
2620 int rc, phy_nr, port_nr, i;
2622 shost = hisi_sas_shost_alloc(pdev, hw);
2626 sha = SHOST_TO_SAS_HA(shost);
2627 hisi_hba = shost_priv(shost);
2628 platform_set_drvdata(pdev, sha);
2630 phy_nr = port_nr = hisi_hba->n_phy;
2632 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2633 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2634 if (!arr_phy || !arr_port) {
2639 sha->sas_phy = arr_phy;
2640 sha->sas_port = arr_port;
2641 sha->lldd_ha = hisi_hba;
2643 shost->transportt = hisi_sas_stt;
2644 shost->max_id = HISI_SAS_MAX_DEVICES;
2645 shost->max_lun = ~0;
2646 shost->max_channel = 1;
2647 shost->max_cmd_len = 16;
2648 if (hisi_hba->hw->slot_index_alloc) {
2649 shost->can_queue = HISI_SAS_MAX_COMMANDS;
2650 shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;
2652 shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
2653 shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
2656 sha->sas_ha_name = DRV_NAME;
2657 sha->dev = hisi_hba->dev;
2658 sha->lldd_module = THIS_MODULE;
2659 sha->sas_addr = &hisi_hba->sas_addr[0];
2660 sha->num_phys = hisi_hba->n_phy;
2661 sha->core.shost = hisi_hba->shost;
2663 for (i = 0; i < hisi_hba->n_phy; i++) {
2664 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2665 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2668 rc = scsi_add_host(shost, &pdev->dev);
2672 rc = sas_register_ha(sha);
2674 goto err_out_register_ha;
2676 rc = hisi_hba->hw->hw_init(hisi_hba);
2678 goto err_out_register_ha;
2680 scsi_scan_host(shost);
2684 err_out_register_ha:
2685 scsi_remove_host(shost);
2687 hisi_sas_debugfs_exit(hisi_hba);
2688 hisi_sas_free(hisi_hba);
2689 scsi_host_put(shost);
2692 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2694 struct dentry *hisi_sas_debugfs_dir;
2696 static void hisi_sas_debugfs_snapshot_cq_reg(struct hisi_hba *hisi_hba)
2698 int queue_entry_size = hisi_hba->hw->complete_hdr_size;
2699 int dump_index = hisi_hba->debugfs_dump_index;
2702 for (i = 0; i < hisi_hba->queue_count; i++)
2703 memcpy(hisi_hba->debugfs_cq[dump_index][i].complete_hdr,
2704 hisi_hba->complete_hdr[i],
2705 HISI_SAS_QUEUE_SLOTS * queue_entry_size);
2708 static void hisi_sas_debugfs_snapshot_dq_reg(struct hisi_hba *hisi_hba)
2710 int queue_entry_size = sizeof(struct hisi_sas_cmd_hdr);
2711 int dump_index = hisi_hba->debugfs_dump_index;
2714 for (i = 0; i < hisi_hba->queue_count; i++) {
2715 struct hisi_sas_cmd_hdr *debugfs_cmd_hdr, *cmd_hdr;
2718 debugfs_cmd_hdr = hisi_hba->debugfs_dq[dump_index][i].hdr;
2719 cmd_hdr = hisi_hba->cmd_hdr[i];
2721 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2722 memcpy(&debugfs_cmd_hdr[j], &cmd_hdr[j],
2727 static void hisi_sas_debugfs_snapshot_port_reg(struct hisi_hba *hisi_hba)
2729 int dump_index = hisi_hba->debugfs_dump_index;
2730 const struct hisi_sas_debugfs_reg *port =
2731 hisi_hba->hw->debugfs_reg_port;
2736 for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) {
2737 databuf = hisi_hba->debugfs_port_reg[dump_index][phy_cnt].data;
2738 for (i = 0; i < port->count; i++, databuf++) {
2739 offset = port->base_off + 4 * i;
2740 *databuf = port->read_port_reg(hisi_hba, phy_cnt,
2746 static void hisi_sas_debugfs_snapshot_global_reg(struct hisi_hba *hisi_hba)
2748 int dump_index = hisi_hba->debugfs_dump_index;
2749 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL].data;
2750 const struct hisi_sas_hw *hw = hisi_hba->hw;
2751 const struct hisi_sas_debugfs_reg *global =
2752 hw->debugfs_reg_array[DEBUGFS_GLOBAL];
2755 for (i = 0; i < global->count; i++, databuf++)
2756 *databuf = global->read_global_reg(hisi_hba, 4 * i);
2759 static void hisi_sas_debugfs_snapshot_axi_reg(struct hisi_hba *hisi_hba)
2761 int dump_index = hisi_hba->debugfs_dump_index;
2762 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI].data;
2763 const struct hisi_sas_hw *hw = hisi_hba->hw;
2764 const struct hisi_sas_debugfs_reg *axi =
2765 hw->debugfs_reg_array[DEBUGFS_AXI];
2768 for (i = 0; i < axi->count; i++, databuf++)
2769 *databuf = axi->read_global_reg(hisi_hba,
2770 4 * i + axi->base_off);
2773 static void hisi_sas_debugfs_snapshot_ras_reg(struct hisi_hba *hisi_hba)
2775 int dump_index = hisi_hba->debugfs_dump_index;
2776 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS].data;
2777 const struct hisi_sas_hw *hw = hisi_hba->hw;
2778 const struct hisi_sas_debugfs_reg *ras =
2779 hw->debugfs_reg_array[DEBUGFS_RAS];
2782 for (i = 0; i < ras->count; i++, databuf++)
2783 *databuf = ras->read_global_reg(hisi_hba,
2784 4 * i + ras->base_off);
2787 static void hisi_sas_debugfs_snapshot_itct_reg(struct hisi_hba *hisi_hba)
2789 int dump_index = hisi_hba->debugfs_dump_index;
2790 void *cachebuf = hisi_hba->debugfs_itct_cache[dump_index].cache;
2791 void *databuf = hisi_hba->debugfs_itct[dump_index].itct;
2792 struct hisi_sas_itct *itct;
2795 hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_ITCT_CACHE,
2798 itct = hisi_hba->itct;
2800 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
2801 memcpy(databuf, itct, sizeof(struct hisi_sas_itct));
2802 databuf += sizeof(struct hisi_sas_itct);
2806 static void hisi_sas_debugfs_snapshot_iost_reg(struct hisi_hba *hisi_hba)
2808 int dump_index = hisi_hba->debugfs_dump_index;
2809 int max_command_entries = HISI_SAS_MAX_COMMANDS;
2810 void *cachebuf = hisi_hba->debugfs_iost_cache[dump_index].cache;
2811 void *databuf = hisi_hba->debugfs_iost[dump_index].iost;
2812 struct hisi_sas_iost *iost;
2815 hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_IOST_CACHE,
2818 iost = hisi_hba->iost;
2820 for (i = 0; i < max_command_entries; i++, iost++) {
2821 memcpy(databuf, iost, sizeof(struct hisi_sas_iost));
2822 databuf += sizeof(struct hisi_sas_iost);
2827 hisi_sas_debugfs_to_reg_name(int off, int base_off,
2828 const struct hisi_sas_debugfs_reg_lu *lu)
2830 for (; lu->name; lu++) {
2831 if (off == lu->off - base_off)
2838 static void hisi_sas_debugfs_print_reg(u32 *regs_val, const void *ptr,
2841 const struct hisi_sas_debugfs_reg *reg = ptr;
2844 for (i = 0; i < reg->count; i++) {
2848 name = hisi_sas_debugfs_to_reg_name(off, reg->base_off,
2852 seq_printf(s, "0x%08x 0x%08x %s\n", off,
2855 seq_printf(s, "0x%08x 0x%08x\n", off,
2860 static int hisi_sas_debugfs_global_show(struct seq_file *s, void *p)
2862 struct hisi_sas_debugfs_regs *global = s->private;
2863 struct hisi_hba *hisi_hba = global->hisi_hba;
2864 const struct hisi_sas_hw *hw = hisi_hba->hw;
2865 const void *reg_global = hw->debugfs_reg_array[DEBUGFS_GLOBAL];
2867 hisi_sas_debugfs_print_reg(global->data,
2873 static int hisi_sas_debugfs_global_open(struct inode *inode, struct file *filp)
2875 return single_open(filp, hisi_sas_debugfs_global_show,
2879 static const struct file_operations hisi_sas_debugfs_global_fops = {
2880 .open = hisi_sas_debugfs_global_open,
2882 .llseek = seq_lseek,
2883 .release = single_release,
2884 .owner = THIS_MODULE,
2887 static int hisi_sas_debugfs_axi_show(struct seq_file *s, void *p)
2889 struct hisi_sas_debugfs_regs *axi = s->private;
2890 struct hisi_hba *hisi_hba = axi->hisi_hba;
2891 const struct hisi_sas_hw *hw = hisi_hba->hw;
2892 const void *reg_axi = hw->debugfs_reg_array[DEBUGFS_AXI];
2894 hisi_sas_debugfs_print_reg(axi->data,
2900 static int hisi_sas_debugfs_axi_open(struct inode *inode, struct file *filp)
2902 return single_open(filp, hisi_sas_debugfs_axi_show,
2906 static const struct file_operations hisi_sas_debugfs_axi_fops = {
2907 .open = hisi_sas_debugfs_axi_open,
2909 .llseek = seq_lseek,
2910 .release = single_release,
2911 .owner = THIS_MODULE,
2914 static int hisi_sas_debugfs_ras_show(struct seq_file *s, void *p)
2916 struct hisi_sas_debugfs_regs *ras = s->private;
2917 struct hisi_hba *hisi_hba = ras->hisi_hba;
2918 const struct hisi_sas_hw *hw = hisi_hba->hw;
2919 const void *reg_ras = hw->debugfs_reg_array[DEBUGFS_RAS];
2921 hisi_sas_debugfs_print_reg(ras->data,
2927 static int hisi_sas_debugfs_ras_open(struct inode *inode, struct file *filp)
2929 return single_open(filp, hisi_sas_debugfs_ras_show,
2933 static const struct file_operations hisi_sas_debugfs_ras_fops = {
2934 .open = hisi_sas_debugfs_ras_open,
2936 .llseek = seq_lseek,
2937 .release = single_release,
2938 .owner = THIS_MODULE,
2941 static int hisi_sas_debugfs_port_show(struct seq_file *s, void *p)
2943 struct hisi_sas_debugfs_port *port = s->private;
2944 struct hisi_sas_phy *phy = port->phy;
2945 struct hisi_hba *hisi_hba = phy->hisi_hba;
2946 const struct hisi_sas_hw *hw = hisi_hba->hw;
2947 const struct hisi_sas_debugfs_reg *reg_port = hw->debugfs_reg_port;
2949 hisi_sas_debugfs_print_reg(port->data, reg_port, s);
2954 static int hisi_sas_debugfs_port_open(struct inode *inode, struct file *filp)
2956 return single_open(filp, hisi_sas_debugfs_port_show, inode->i_private);
2959 static const struct file_operations hisi_sas_debugfs_port_fops = {
2960 .open = hisi_sas_debugfs_port_open,
2962 .llseek = seq_lseek,
2963 .release = single_release,
2964 .owner = THIS_MODULE,
2967 static void hisi_sas_show_row_64(struct seq_file *s, int index,
2968 int sz, __le64 *ptr)
2972 /* completion header size not fixed per HW version */
2973 seq_printf(s, "index %04d:\n\t", index);
2974 for (i = 1; i <= sz / 8; i++, ptr++) {
2975 seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr));
2977 seq_puts(s, "\n\t");
2983 static void hisi_sas_show_row_32(struct seq_file *s, int index,
2984 int sz, __le32 *ptr)
2988 /* completion header size not fixed per HW version */
2989 seq_printf(s, "index %04d:\n\t", index);
2990 for (i = 1; i <= sz / 4; i++, ptr++) {
2991 seq_printf(s, " 0x%08x", le32_to_cpu(*ptr));
2993 seq_puts(s, "\n\t");
2998 static void hisi_sas_cq_show_slot(struct seq_file *s, int slot,
2999 struct hisi_sas_debugfs_cq *debugfs_cq)
3001 struct hisi_sas_cq *cq = debugfs_cq->cq;
3002 struct hisi_hba *hisi_hba = cq->hisi_hba;
3003 __le32 *complete_hdr = debugfs_cq->complete_hdr +
3004 (hisi_hba->hw->complete_hdr_size * slot);
3006 hisi_sas_show_row_32(s, slot,
3007 hisi_hba->hw->complete_hdr_size,
3011 static int hisi_sas_debugfs_cq_show(struct seq_file *s, void *p)
3013 struct hisi_sas_debugfs_cq *debugfs_cq = s->private;
3016 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
3017 hisi_sas_cq_show_slot(s, slot, debugfs_cq);
3022 static int hisi_sas_debugfs_cq_open(struct inode *inode, struct file *filp)
3024 return single_open(filp, hisi_sas_debugfs_cq_show, inode->i_private);
3027 static const struct file_operations hisi_sas_debugfs_cq_fops = {
3028 .open = hisi_sas_debugfs_cq_open,
3030 .llseek = seq_lseek,
3031 .release = single_release,
3032 .owner = THIS_MODULE,
3035 static void hisi_sas_dq_show_slot(struct seq_file *s, int slot, void *dq_ptr)
3037 struct hisi_sas_debugfs_dq *debugfs_dq = dq_ptr;
3038 void *cmd_queue = debugfs_dq->hdr;
3039 __le32 *cmd_hdr = cmd_queue +
3040 sizeof(struct hisi_sas_cmd_hdr) * slot;
3042 hisi_sas_show_row_32(s, slot, sizeof(struct hisi_sas_cmd_hdr), cmd_hdr);
3045 static int hisi_sas_debugfs_dq_show(struct seq_file *s, void *p)
3049 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
3050 hisi_sas_dq_show_slot(s, slot, s->private);
3055 static int hisi_sas_debugfs_dq_open(struct inode *inode, struct file *filp)
3057 return single_open(filp, hisi_sas_debugfs_dq_show, inode->i_private);
3060 static const struct file_operations hisi_sas_debugfs_dq_fops = {
3061 .open = hisi_sas_debugfs_dq_open,
3063 .llseek = seq_lseek,
3064 .release = single_release,
3065 .owner = THIS_MODULE,
3068 static int hisi_sas_debugfs_iost_show(struct seq_file *s, void *p)
3070 struct hisi_sas_debugfs_iost *debugfs_iost = s->private;
3071 struct hisi_sas_iost *iost = debugfs_iost->iost;
3072 int i, max_command_entries = HISI_SAS_MAX_COMMANDS;
3074 for (i = 0; i < max_command_entries; i++, iost++) {
3075 __le64 *data = &iost->qw0;
3077 hisi_sas_show_row_64(s, i, sizeof(*iost), data);
3083 static int hisi_sas_debugfs_iost_open(struct inode *inode, struct file *filp)
3085 return single_open(filp, hisi_sas_debugfs_iost_show, inode->i_private);
3088 static const struct file_operations hisi_sas_debugfs_iost_fops = {
3089 .open = hisi_sas_debugfs_iost_open,
3091 .llseek = seq_lseek,
3092 .release = single_release,
3093 .owner = THIS_MODULE,
3096 static int hisi_sas_debugfs_iost_cache_show(struct seq_file *s, void *p)
3098 struct hisi_sas_debugfs_iost_cache *debugfs_iost_cache = s->private;
3099 struct hisi_sas_iost_itct_cache *iost_cache = debugfs_iost_cache->cache;
3100 u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3104 for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, iost_cache++) {
3106 * Data struct of IOST cache:
3107 * Data[1]: BIT0~15: Table index
3109 * Data[2]~[9]: IOST table
3111 tab_idx = (iost_cache->data[1] & 0xffff);
3112 iost = (__le64 *)iost_cache;
3114 hisi_sas_show_row_64(s, tab_idx, cache_size, iost);
3120 static int hisi_sas_debugfs_iost_cache_open(struct inode *inode,
3123 return single_open(filp, hisi_sas_debugfs_iost_cache_show,
3127 static const struct file_operations hisi_sas_debugfs_iost_cache_fops = {
3128 .open = hisi_sas_debugfs_iost_cache_open,
3130 .llseek = seq_lseek,
3131 .release = single_release,
3132 .owner = THIS_MODULE,
3135 static int hisi_sas_debugfs_itct_show(struct seq_file *s, void *p)
3138 struct hisi_sas_debugfs_itct *debugfs_itct = s->private;
3139 struct hisi_sas_itct *itct = debugfs_itct->itct;
3141 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
3142 __le64 *data = &itct->qw0;
3144 hisi_sas_show_row_64(s, i, sizeof(*itct), data);
3150 static int hisi_sas_debugfs_itct_open(struct inode *inode, struct file *filp)
3152 return single_open(filp, hisi_sas_debugfs_itct_show, inode->i_private);
3155 static const struct file_operations hisi_sas_debugfs_itct_fops = {
3156 .open = hisi_sas_debugfs_itct_open,
3158 .llseek = seq_lseek,
3159 .release = single_release,
3160 .owner = THIS_MODULE,
3163 static int hisi_sas_debugfs_itct_cache_show(struct seq_file *s, void *p)
3165 struct hisi_sas_debugfs_itct_cache *debugfs_itct_cache = s->private;
3166 struct hisi_sas_iost_itct_cache *itct_cache = debugfs_itct_cache->cache;
3167 u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3171 for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, itct_cache++) {
3173 * Data struct of ITCT cache:
3174 * Data[1]: BIT0~15: Table index
3176 * Data[2]~[9]: ITCT table
3178 tab_idx = itct_cache->data[1] & 0xffff;
3179 itct = (__le64 *)itct_cache;
3181 hisi_sas_show_row_64(s, tab_idx, cache_size, itct);
3187 static int hisi_sas_debugfs_itct_cache_open(struct inode *inode,
3190 return single_open(filp, hisi_sas_debugfs_itct_cache_show,
3194 static const struct file_operations hisi_sas_debugfs_itct_cache_fops = {
3195 .open = hisi_sas_debugfs_itct_cache_open,
3197 .llseek = seq_lseek,
3198 .release = single_release,
3199 .owner = THIS_MODULE,
3202 static void hisi_sas_debugfs_create_files(struct hisi_hba *hisi_hba)
3204 u64 *debugfs_timestamp;
3205 int dump_index = hisi_hba->debugfs_dump_index;
3206 struct dentry *dump_dentry;
3207 struct dentry *dentry;
3213 snprintf(name, 256, "%d", dump_index);
3215 dump_dentry = debugfs_create_dir(name, hisi_hba->debugfs_dump_dentry);
3217 debugfs_timestamp = &hisi_hba->debugfs_timestamp[dump_index];
3219 debugfs_create_u64("timestamp", 0400, dump_dentry,
3222 debugfs_create_file("global", 0400, dump_dentry,
3223 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL],
3224 &hisi_sas_debugfs_global_fops);
3226 /* Create port dir and files */
3227 dentry = debugfs_create_dir("port", dump_dentry);
3228 for (p = 0; p < hisi_hba->n_phy; p++) {
3229 snprintf(name, 256, "%d", p);
3231 debugfs_create_file(name, 0400, dentry,
3232 &hisi_hba->debugfs_port_reg[dump_index][p],
3233 &hisi_sas_debugfs_port_fops);
3236 /* Create CQ dir and files */
3237 dentry = debugfs_create_dir("cq", dump_dentry);
3238 for (c = 0; c < hisi_hba->queue_count; c++) {
3239 snprintf(name, 256, "%d", c);
3241 debugfs_create_file(name, 0400, dentry,
3242 &hisi_hba->debugfs_cq[dump_index][c],
3243 &hisi_sas_debugfs_cq_fops);
3246 /* Create DQ dir and files */
3247 dentry = debugfs_create_dir("dq", dump_dentry);
3248 for (d = 0; d < hisi_hba->queue_count; d++) {
3249 snprintf(name, 256, "%d", d);
3251 debugfs_create_file(name, 0400, dentry,
3252 &hisi_hba->debugfs_dq[dump_index][d],
3253 &hisi_sas_debugfs_dq_fops);
3256 debugfs_create_file("iost", 0400, dump_dentry,
3257 &hisi_hba->debugfs_iost[dump_index],
3258 &hisi_sas_debugfs_iost_fops);
3260 debugfs_create_file("iost_cache", 0400, dump_dentry,
3261 &hisi_hba->debugfs_iost_cache[dump_index],
3262 &hisi_sas_debugfs_iost_cache_fops);
3264 debugfs_create_file("itct", 0400, dump_dentry,
3265 &hisi_hba->debugfs_itct[dump_index],
3266 &hisi_sas_debugfs_itct_fops);
3268 debugfs_create_file("itct_cache", 0400, dump_dentry,
3269 &hisi_hba->debugfs_itct_cache[dump_index],
3270 &hisi_sas_debugfs_itct_cache_fops);
3272 debugfs_create_file("axi", 0400, dump_dentry,
3273 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI],
3274 &hisi_sas_debugfs_axi_fops);
3276 debugfs_create_file("ras", 0400, dump_dentry,
3277 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS],
3278 &hisi_sas_debugfs_ras_fops);
3283 static void hisi_sas_debugfs_snapshot_regs(struct hisi_hba *hisi_hba)
3285 hisi_hba->hw->snapshot_prepare(hisi_hba);
3287 hisi_sas_debugfs_snapshot_global_reg(hisi_hba);
3288 hisi_sas_debugfs_snapshot_port_reg(hisi_hba);
3289 hisi_sas_debugfs_snapshot_axi_reg(hisi_hba);
3290 hisi_sas_debugfs_snapshot_ras_reg(hisi_hba);
3291 hisi_sas_debugfs_snapshot_cq_reg(hisi_hba);
3292 hisi_sas_debugfs_snapshot_dq_reg(hisi_hba);
3293 hisi_sas_debugfs_snapshot_itct_reg(hisi_hba);
3294 hisi_sas_debugfs_snapshot_iost_reg(hisi_hba);
3296 hisi_sas_debugfs_create_files(hisi_hba);
3298 hisi_hba->hw->snapshot_restore(hisi_hba);
3301 static ssize_t hisi_sas_debugfs_trigger_dump_write(struct file *file,
3302 const char __user *user_buf,
3303 size_t count, loff_t *ppos)
3305 struct hisi_hba *hisi_hba = file->f_inode->i_private;
3308 if (hisi_hba->debugfs_dump_index >= hisi_sas_debugfs_dump_count)
3314 if (copy_from_user(buf, user_buf, count))
3320 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
3325 static const struct file_operations hisi_sas_debugfs_trigger_dump_fops = {
3326 .write = &hisi_sas_debugfs_trigger_dump_write,
3327 .owner = THIS_MODULE,
3331 HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL = 0,
3332 HISI_SAS_BIST_LOOPBACK_MODE_SERDES,
3333 HISI_SAS_BIST_LOOPBACK_MODE_REMOTE,
3337 HISI_SAS_BIST_CODE_MODE_PRBS7 = 0,
3338 HISI_SAS_BIST_CODE_MODE_PRBS23,
3339 HISI_SAS_BIST_CODE_MODE_PRBS31,
3340 HISI_SAS_BIST_CODE_MODE_JTPAT,
3341 HISI_SAS_BIST_CODE_MODE_CJTPAT,
3342 HISI_SAS_BIST_CODE_MODE_SCRAMBED_0,
3343 HISI_SAS_BIST_CODE_MODE_TRAIN,
3344 HISI_SAS_BIST_CODE_MODE_TRAIN_DONE,
3345 HISI_SAS_BIST_CODE_MODE_HFTP,
3346 HISI_SAS_BIST_CODE_MODE_MFTP,
3347 HISI_SAS_BIST_CODE_MODE_LFTP,
3348 HISI_SAS_BIST_CODE_MODE_FIXED_DATA,
3351 static const struct {
3354 } hisi_sas_debugfs_loop_linkrate[] = {
3355 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
3356 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
3357 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
3358 { SAS_LINK_RATE_12_0_GBPS, "12.0 Gbit" },
3361 static int hisi_sas_debugfs_bist_linkrate_show(struct seq_file *s, void *p)
3363 struct hisi_hba *hisi_hba = s->private;
3366 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
3367 int match = (hisi_hba->debugfs_bist_linkrate ==
3368 hisi_sas_debugfs_loop_linkrate[i].value);
3370 seq_printf(s, "%s%s%s ", match ? "[" : "",
3371 hisi_sas_debugfs_loop_linkrate[i].name,
3379 static ssize_t hisi_sas_debugfs_bist_linkrate_write(struct file *filp,
3380 const char __user *buf,
3381 size_t count, loff_t *ppos)
3383 struct seq_file *m = filp->private_data;
3384 struct hisi_hba *hisi_hba = m->private;
3385 char kbuf[16] = {}, *pkbuf;
3389 if (hisi_hba->debugfs_bist_enable)
3392 if (count >= sizeof(kbuf))
3395 if (copy_from_user(kbuf, buf, count))
3398 pkbuf = strstrip(kbuf);
3400 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
3401 if (!strncmp(hisi_sas_debugfs_loop_linkrate[i].name,
3403 hisi_hba->debugfs_bist_linkrate =
3404 hisi_sas_debugfs_loop_linkrate[i].value;
3416 static int hisi_sas_debugfs_bist_linkrate_open(struct inode *inode,
3419 return single_open(filp, hisi_sas_debugfs_bist_linkrate_show,
3423 static const struct file_operations hisi_sas_debugfs_bist_linkrate_ops = {
3424 .open = hisi_sas_debugfs_bist_linkrate_open,
3426 .write = hisi_sas_debugfs_bist_linkrate_write,
3427 .llseek = seq_lseek,
3428 .release = single_release,
3429 .owner = THIS_MODULE,
3432 static const struct {
3435 } hisi_sas_debugfs_loop_code_mode[] = {
3436 { HISI_SAS_BIST_CODE_MODE_PRBS7, "PRBS7" },
3437 { HISI_SAS_BIST_CODE_MODE_PRBS23, "PRBS23" },
3438 { HISI_SAS_BIST_CODE_MODE_PRBS31, "PRBS31" },
3439 { HISI_SAS_BIST_CODE_MODE_JTPAT, "JTPAT" },
3440 { HISI_SAS_BIST_CODE_MODE_CJTPAT, "CJTPAT" },
3441 { HISI_SAS_BIST_CODE_MODE_SCRAMBED_0, "SCRAMBED_0" },
3442 { HISI_SAS_BIST_CODE_MODE_TRAIN, "TRAIN" },
3443 { HISI_SAS_BIST_CODE_MODE_TRAIN_DONE, "TRAIN_DONE" },
3444 { HISI_SAS_BIST_CODE_MODE_HFTP, "HFTP" },
3445 { HISI_SAS_BIST_CODE_MODE_MFTP, "MFTP" },
3446 { HISI_SAS_BIST_CODE_MODE_LFTP, "LFTP" },
3447 { HISI_SAS_BIST_CODE_MODE_FIXED_DATA, "FIXED_DATA" },
3450 static int hisi_sas_debugfs_bist_code_mode_show(struct seq_file *s, void *p)
3452 struct hisi_hba *hisi_hba = s->private;
3455 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
3456 int match = (hisi_hba->debugfs_bist_code_mode ==
3457 hisi_sas_debugfs_loop_code_mode[i].value);
3459 seq_printf(s, "%s%s%s ", match ? "[" : "",
3460 hisi_sas_debugfs_loop_code_mode[i].name,
3468 static ssize_t hisi_sas_debugfs_bist_code_mode_write(struct file *filp,
3469 const char __user *buf,
3473 struct seq_file *m = filp->private_data;
3474 struct hisi_hba *hisi_hba = m->private;
3475 char kbuf[16] = {}, *pkbuf;
3479 if (hisi_hba->debugfs_bist_enable)
3482 if (count >= sizeof(kbuf))
3485 if (copy_from_user(kbuf, buf, count))
3488 pkbuf = strstrip(kbuf);
3490 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
3491 if (!strncmp(hisi_sas_debugfs_loop_code_mode[i].name,
3493 hisi_hba->debugfs_bist_code_mode =
3494 hisi_sas_debugfs_loop_code_mode[i].value;
3506 static int hisi_sas_debugfs_bist_code_mode_open(struct inode *inode,
3509 return single_open(filp, hisi_sas_debugfs_bist_code_mode_show,
3513 static const struct file_operations hisi_sas_debugfs_bist_code_mode_ops = {
3514 .open = hisi_sas_debugfs_bist_code_mode_open,
3516 .write = hisi_sas_debugfs_bist_code_mode_write,
3517 .llseek = seq_lseek,
3518 .release = single_release,
3519 .owner = THIS_MODULE,
3522 static ssize_t hisi_sas_debugfs_bist_phy_write(struct file *filp,
3523 const char __user *buf,
3524 size_t count, loff_t *ppos)
3526 struct seq_file *m = filp->private_data;
3527 struct hisi_hba *hisi_hba = m->private;
3528 unsigned int phy_no;
3531 if (hisi_hba->debugfs_bist_enable)
3534 val = kstrtouint_from_user(buf, count, 0, &phy_no);
3538 if (phy_no >= hisi_hba->n_phy)
3541 hisi_hba->debugfs_bist_phy_no = phy_no;
3546 static int hisi_sas_debugfs_bist_phy_show(struct seq_file *s, void *p)
3548 struct hisi_hba *hisi_hba = s->private;
3550 seq_printf(s, "%d\n", hisi_hba->debugfs_bist_phy_no);
3555 static int hisi_sas_debugfs_bist_phy_open(struct inode *inode,
3558 return single_open(filp, hisi_sas_debugfs_bist_phy_show,
3562 static const struct file_operations hisi_sas_debugfs_bist_phy_ops = {
3563 .open = hisi_sas_debugfs_bist_phy_open,
3565 .write = hisi_sas_debugfs_bist_phy_write,
3566 .llseek = seq_lseek,
3567 .release = single_release,
3568 .owner = THIS_MODULE,
3571 static const struct {
3574 } hisi_sas_debugfs_loop_modes[] = {
3575 { HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL, "digital" },
3576 { HISI_SAS_BIST_LOOPBACK_MODE_SERDES, "serdes" },
3577 { HISI_SAS_BIST_LOOPBACK_MODE_REMOTE, "remote" },
3580 static int hisi_sas_debugfs_bist_mode_show(struct seq_file *s, void *p)
3582 struct hisi_hba *hisi_hba = s->private;
3585 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
3586 int match = (hisi_hba->debugfs_bist_mode ==
3587 hisi_sas_debugfs_loop_modes[i].value);
3589 seq_printf(s, "%s%s%s ", match ? "[" : "",
3590 hisi_sas_debugfs_loop_modes[i].name,
3598 static ssize_t hisi_sas_debugfs_bist_mode_write(struct file *filp,
3599 const char __user *buf,
3600 size_t count, loff_t *ppos)
3602 struct seq_file *m = filp->private_data;
3603 struct hisi_hba *hisi_hba = m->private;
3604 char kbuf[16] = {}, *pkbuf;
3608 if (hisi_hba->debugfs_bist_enable)
3611 if (count >= sizeof(kbuf))
3614 if (copy_from_user(kbuf, buf, count))
3617 pkbuf = strstrip(kbuf);
3619 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
3620 if (!strncmp(hisi_sas_debugfs_loop_modes[i].name, pkbuf, 16)) {
3621 hisi_hba->debugfs_bist_mode =
3622 hisi_sas_debugfs_loop_modes[i].value;
3634 static int hisi_sas_debugfs_bist_mode_open(struct inode *inode,
3637 return single_open(filp, hisi_sas_debugfs_bist_mode_show,
3641 static const struct file_operations hisi_sas_debugfs_bist_mode_ops = {
3642 .open = hisi_sas_debugfs_bist_mode_open,
3644 .write = hisi_sas_debugfs_bist_mode_write,
3645 .llseek = seq_lseek,
3646 .release = single_release,
3647 .owner = THIS_MODULE,
3650 static ssize_t hisi_sas_debugfs_bist_enable_write(struct file *filp,
3651 const char __user *buf,
3652 size_t count, loff_t *ppos)
3654 struct seq_file *m = filp->private_data;
3655 struct hisi_hba *hisi_hba = m->private;
3656 unsigned int enable;
3659 val = kstrtouint_from_user(buf, count, 0, &enable);
3666 if (enable == hisi_hba->debugfs_bist_enable)
3669 if (!hisi_hba->hw->set_bist)
3672 val = hisi_hba->hw->set_bist(hisi_hba, enable);
3676 hisi_hba->debugfs_bist_enable = enable;
3681 static int hisi_sas_debugfs_bist_enable_show(struct seq_file *s, void *p)
3683 struct hisi_hba *hisi_hba = s->private;
3685 seq_printf(s, "%d\n", hisi_hba->debugfs_bist_enable);
3690 static int hisi_sas_debugfs_bist_enable_open(struct inode *inode,
3693 return single_open(filp, hisi_sas_debugfs_bist_enable_show,
3697 static const struct file_operations hisi_sas_debugfs_bist_enable_ops = {
3698 .open = hisi_sas_debugfs_bist_enable_open,
3700 .write = hisi_sas_debugfs_bist_enable_write,
3701 .llseek = seq_lseek,
3702 .release = single_release,
3703 .owner = THIS_MODULE,
3706 static ssize_t hisi_sas_debugfs_phy_down_cnt_write(struct file *filp,
3707 const char __user *buf,
3708 size_t count, loff_t *ppos)
3710 struct seq_file *s = filp->private_data;
3711 struct hisi_sas_phy *phy = s->private;
3712 unsigned int set_val;
3715 res = kstrtouint_from_user(buf, count, 0, &set_val);
3722 atomic_set(&phy->down_cnt, 0);
3727 static int hisi_sas_debugfs_phy_down_cnt_show(struct seq_file *s, void *p)
3729 struct hisi_sas_phy *phy = s->private;
3731 seq_printf(s, "%d\n", atomic_read(&phy->down_cnt));
3736 static int hisi_sas_debugfs_phy_down_cnt_open(struct inode *inode,
3739 return single_open(filp, hisi_sas_debugfs_phy_down_cnt_show,
3743 static const struct file_operations hisi_sas_debugfs_phy_down_cnt_ops = {
3744 .open = hisi_sas_debugfs_phy_down_cnt_open,
3746 .write = hisi_sas_debugfs_phy_down_cnt_write,
3747 .llseek = seq_lseek,
3748 .release = single_release,
3749 .owner = THIS_MODULE,
3752 void hisi_sas_debugfs_work_handler(struct work_struct *work)
3754 struct hisi_hba *hisi_hba =
3755 container_of(work, struct hisi_hba, debugfs_work);
3756 int debugfs_dump_index = hisi_hba->debugfs_dump_index;
3757 struct device *dev = hisi_hba->dev;
3758 u64 timestamp = local_clock();
3760 if (debugfs_dump_index >= hisi_sas_debugfs_dump_count) {
3761 dev_warn(dev, "dump count exceeded!\n");
3765 do_div(timestamp, NSEC_PER_MSEC);
3766 hisi_hba->debugfs_timestamp[debugfs_dump_index] = timestamp;
3768 hisi_sas_debugfs_snapshot_regs(hisi_hba);
3769 hisi_hba->debugfs_dump_index++;
3771 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_work_handler);
3773 static void hisi_sas_debugfs_release(struct hisi_hba *hisi_hba, int dump_index)
3775 struct device *dev = hisi_hba->dev;
3778 devm_kfree(dev, hisi_hba->debugfs_iost_cache[dump_index].cache);
3779 devm_kfree(dev, hisi_hba->debugfs_itct_cache[dump_index].cache);
3780 devm_kfree(dev, hisi_hba->debugfs_iost[dump_index].iost);
3781 devm_kfree(dev, hisi_hba->debugfs_itct[dump_index].itct);
3783 for (i = 0; i < hisi_hba->queue_count; i++)
3784 devm_kfree(dev, hisi_hba->debugfs_dq[dump_index][i].hdr);
3786 for (i = 0; i < hisi_hba->queue_count; i++)
3788 hisi_hba->debugfs_cq[dump_index][i].complete_hdr);
3790 for (i = 0; i < DEBUGFS_REGS_NUM; i++)
3791 devm_kfree(dev, hisi_hba->debugfs_regs[dump_index][i].data);
3793 for (i = 0; i < hisi_hba->n_phy; i++)
3794 devm_kfree(dev, hisi_hba->debugfs_port_reg[dump_index][i].data);
3797 static int hisi_sas_debugfs_alloc(struct hisi_hba *hisi_hba, int dump_index)
3799 const struct hisi_sas_hw *hw = hisi_hba->hw;
3800 struct device *dev = hisi_hba->dev;
3804 for (r = 0; r < DEBUGFS_REGS_NUM; r++) {
3805 struct hisi_sas_debugfs_regs *regs =
3806 &hisi_hba->debugfs_regs[dump_index][r];
3808 sz = hw->debugfs_reg_array[r]->count * 4;
3809 regs->data = devm_kmalloc(dev, sz, GFP_KERNEL);
3812 regs->hisi_hba = hisi_hba;
3815 sz = hw->debugfs_reg_port->count * 4;
3816 for (p = 0; p < hisi_hba->n_phy; p++) {
3817 struct hisi_sas_debugfs_port *port =
3818 &hisi_hba->debugfs_port_reg[dump_index][p];
3820 port->data = devm_kmalloc(dev, sz, GFP_KERNEL);
3823 port->phy = &hisi_hba->phy[p];
3826 sz = hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
3827 for (c = 0; c < hisi_hba->queue_count; c++) {
3828 struct hisi_sas_debugfs_cq *cq =
3829 &hisi_hba->debugfs_cq[dump_index][c];
3831 cq->complete_hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
3832 if (!cq->complete_hdr)
3834 cq->cq = &hisi_hba->cq[c];
3837 sz = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
3838 for (d = 0; d < hisi_hba->queue_count; d++) {
3839 struct hisi_sas_debugfs_dq *dq =
3840 &hisi_hba->debugfs_dq[dump_index][d];
3842 dq->hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
3845 dq->dq = &hisi_hba->dq[d];
3848 sz = HISI_SAS_MAX_COMMANDS * sizeof(struct hisi_sas_iost);
3850 hisi_hba->debugfs_iost[dump_index].iost =
3851 devm_kmalloc(dev, sz, GFP_KERNEL);
3852 if (!hisi_hba->debugfs_iost[dump_index].iost)
3855 sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
3856 sizeof(struct hisi_sas_iost_itct_cache);
3858 hisi_hba->debugfs_iost_cache[dump_index].cache =
3859 devm_kmalloc(dev, sz, GFP_KERNEL);
3860 if (!hisi_hba->debugfs_iost_cache[dump_index].cache)
3863 sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
3864 sizeof(struct hisi_sas_iost_itct_cache);
3866 hisi_hba->debugfs_itct_cache[dump_index].cache =
3867 devm_kmalloc(dev, sz, GFP_KERNEL);
3868 if (!hisi_hba->debugfs_itct_cache[dump_index].cache)
3871 /* New memory allocation must be locate before itct */
3872 sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
3874 hisi_hba->debugfs_itct[dump_index].itct =
3875 devm_kmalloc(dev, sz, GFP_KERNEL);
3876 if (!hisi_hba->debugfs_itct[dump_index].itct)
3881 for (i = 0; i < hisi_sas_debugfs_dump_count; i++)
3882 hisi_sas_debugfs_release(hisi_hba, i);
3886 static void hisi_sas_debugfs_phy_down_cnt_init(struct hisi_hba *hisi_hba)
3888 struct dentry *dir = debugfs_create_dir("phy_down_cnt",
3889 hisi_hba->debugfs_dir);
3893 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
3894 snprintf(name, 16, "%d", phy_no);
3895 debugfs_create_file(name, 0600, dir,
3896 &hisi_hba->phy[phy_no],
3897 &hisi_sas_debugfs_phy_down_cnt_ops);
3901 static void hisi_sas_debugfs_bist_init(struct hisi_hba *hisi_hba)
3903 hisi_hba->debugfs_bist_dentry =
3904 debugfs_create_dir("bist", hisi_hba->debugfs_dir);
3905 debugfs_create_file("link_rate", 0600,
3906 hisi_hba->debugfs_bist_dentry, hisi_hba,
3907 &hisi_sas_debugfs_bist_linkrate_ops);
3909 debugfs_create_file("code_mode", 0600,
3910 hisi_hba->debugfs_bist_dentry, hisi_hba,
3911 &hisi_sas_debugfs_bist_code_mode_ops);
3913 debugfs_create_file("phy_id", 0600, hisi_hba->debugfs_bist_dentry,
3914 hisi_hba, &hisi_sas_debugfs_bist_phy_ops);
3916 debugfs_create_u32("cnt", 0600, hisi_hba->debugfs_bist_dentry,
3917 &hisi_hba->debugfs_bist_cnt);
3919 debugfs_create_file("loopback_mode", 0600,
3920 hisi_hba->debugfs_bist_dentry,
3921 hisi_hba, &hisi_sas_debugfs_bist_mode_ops);
3923 debugfs_create_file("enable", 0600, hisi_hba->debugfs_bist_dentry,
3924 hisi_hba, &hisi_sas_debugfs_bist_enable_ops);
3926 hisi_hba->debugfs_bist_linkrate = SAS_LINK_RATE_1_5_GBPS;
3929 void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba)
3931 struct device *dev = hisi_hba->dev;
3934 hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev),
3935 hisi_sas_debugfs_dir);
3936 debugfs_create_file("trigger_dump", 0200,
3937 hisi_hba->debugfs_dir,
3939 &hisi_sas_debugfs_trigger_dump_fops);
3941 /* create bist structures */
3942 hisi_sas_debugfs_bist_init(hisi_hba);
3944 hisi_hba->debugfs_dump_dentry =
3945 debugfs_create_dir("dump", hisi_hba->debugfs_dir);
3947 hisi_sas_debugfs_phy_down_cnt_init(hisi_hba);
3949 for (i = 0; i < hisi_sas_debugfs_dump_count; i++) {
3950 if (hisi_sas_debugfs_alloc(hisi_hba, i)) {
3951 debugfs_remove_recursive(hisi_hba->debugfs_dir);
3952 dev_dbg(dev, "failed to init debugfs!\n");
3957 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_init);
3959 void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba)
3961 debugfs_remove_recursive(hisi_hba->debugfs_dir);
3963 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_exit);
3965 int hisi_sas_remove(struct platform_device *pdev)
3967 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
3968 struct hisi_hba *hisi_hba = sha->lldd_ha;
3969 struct Scsi_Host *shost = sha->core.shost;
3971 if (timer_pending(&hisi_hba->timer))
3972 del_timer(&hisi_hba->timer);
3974 sas_unregister_ha(sha);
3975 sas_remove_host(sha->core.shost);
3977 hisi_sas_free(hisi_hba);
3978 scsi_host_put(shost);
3981 EXPORT_SYMBOL_GPL(hisi_sas_remove);
3983 bool hisi_sas_debugfs_enable;
3984 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
3985 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
3986 MODULE_PARM_DESC(hisi_sas_debugfs_enable, "Enable driver debugfs (default disabled)");
3988 u32 hisi_sas_debugfs_dump_count = 1;
3989 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count);
3990 module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444);
3991 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow");
3993 static __init int hisi_sas_init(void)
3995 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
3999 if (hisi_sas_debugfs_enable) {
4000 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
4001 if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) {
4002 pr_info("hisi_sas: Limiting debugfs dump count\n");
4003 hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP;
4010 static __exit void hisi_sas_exit(void)
4012 sas_release_transport(hisi_sas_stt);
4014 debugfs_remove(hisi_sas_debugfs_dir);
4017 module_init(hisi_sas_init);
4018 module_exit(hisi_sas_exit);
4020 MODULE_LICENSE("GPL");
4021 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
4022 MODULE_DESCRIPTION("HISILICON SAS controller driver");
4023 MODULE_ALIAS("platform:" DRV_NAME);