1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2015 Linaro Ltd.
4 * Copyright (c) 2015 Hisilicon Limited.
8 #define DRV_NAME "hisi_sas"
10 #define DEV_IS_GONE(dev) \
11 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
13 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
14 u8 *lun, struct hisi_sas_tmf_task *tmf);
16 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
17 struct domain_device *device,
18 int abort_flag, int tag);
19 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
20 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
22 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
23 struct domain_device *device);
24 static void hisi_sas_dev_gone(struct domain_device *device);
26 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
28 switch (fis->command) {
29 case ATA_CMD_FPDMA_WRITE:
30 case ATA_CMD_FPDMA_READ:
31 case ATA_CMD_FPDMA_RECV:
32 case ATA_CMD_FPDMA_SEND:
33 case ATA_CMD_NCQ_NON_DATA:
34 return HISI_SAS_SATA_PROTOCOL_FPDMA;
36 case ATA_CMD_DOWNLOAD_MICRO:
38 case ATA_CMD_PMP_READ:
39 case ATA_CMD_READ_LOG_EXT:
40 case ATA_CMD_PIO_READ:
41 case ATA_CMD_PIO_READ_EXT:
42 case ATA_CMD_PMP_WRITE:
43 case ATA_CMD_WRITE_LOG_EXT:
44 case ATA_CMD_PIO_WRITE:
45 case ATA_CMD_PIO_WRITE_EXT:
46 return HISI_SAS_SATA_PROTOCOL_PIO;
49 case ATA_CMD_DOWNLOAD_MICRO_DMA:
50 case ATA_CMD_PMP_READ_DMA:
51 case ATA_CMD_PMP_WRITE_DMA:
53 case ATA_CMD_READ_EXT:
54 case ATA_CMD_READ_LOG_DMA_EXT:
55 case ATA_CMD_READ_STREAM_DMA_EXT:
56 case ATA_CMD_TRUSTED_RCV_DMA:
57 case ATA_CMD_TRUSTED_SND_DMA:
59 case ATA_CMD_WRITE_EXT:
60 case ATA_CMD_WRITE_FUA_EXT:
61 case ATA_CMD_WRITE_QUEUED:
62 case ATA_CMD_WRITE_LOG_DMA_EXT:
63 case ATA_CMD_WRITE_STREAM_DMA_EXT:
64 case ATA_CMD_ZAC_MGMT_IN:
65 return HISI_SAS_SATA_PROTOCOL_DMA;
67 case ATA_CMD_CHK_POWER:
68 case ATA_CMD_DEV_RESET:
71 case ATA_CMD_FLUSH_EXT:
73 case ATA_CMD_VERIFY_EXT:
74 case ATA_CMD_SET_FEATURES:
76 case ATA_CMD_STANDBYNOW1:
77 case ATA_CMD_ZAC_MGMT_OUT:
78 return HISI_SAS_SATA_PROTOCOL_NONDATA;
81 switch (fis->features) {
82 case ATA_SET_MAX_PASSWD:
83 case ATA_SET_MAX_LOCK:
84 return HISI_SAS_SATA_PROTOCOL_PIO;
86 case ATA_SET_MAX_PASSWD_DMA:
87 case ATA_SET_MAX_UNLOCK_DMA:
88 return HISI_SAS_SATA_PROTOCOL_DMA;
91 return HISI_SAS_SATA_PROTOCOL_NONDATA;
96 if (direction == DMA_NONE)
97 return HISI_SAS_SATA_PROTOCOL_NONDATA;
98 return HISI_SAS_SATA_PROTOCOL_PIO;
102 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
104 void hisi_sas_sata_done(struct sas_task *task,
105 struct hisi_sas_slot *slot)
107 struct task_status_struct *ts = &task->task_status;
108 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
109 struct hisi_sas_status_buffer *status_buf =
110 hisi_sas_status_buf_addr_mem(slot);
111 u8 *iu = &status_buf->iu[0];
112 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
114 resp->frame_len = sizeof(struct dev_to_host_fis);
115 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
117 ts->buf_valid_size = sizeof(*resp);
119 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
122 * This function assumes linkrate mask fits in 8 bits, which it
123 * does for all HW versions supported.
125 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
130 max -= SAS_LINK_RATE_1_5_GBPS;
131 for (i = 0; i <= max; i++)
132 rate |= 1 << (i * 2);
135 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
137 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
139 return device->port->ha->lldd_ha;
142 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
144 return container_of(sas_port, struct hisi_sas_port, sas_port);
146 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
148 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
152 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
153 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
155 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
157 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
159 void *bitmap = hisi_hba->slot_index_tags;
161 clear_bit(slot_idx, bitmap);
164 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
166 if (hisi_hba->hw->slot_index_alloc ||
167 slot_idx >= HISI_SAS_UNRESERVED_IPTT) {
168 spin_lock(&hisi_hba->lock);
169 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
170 spin_unlock(&hisi_hba->lock);
174 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
176 void *bitmap = hisi_hba->slot_index_tags;
178 set_bit(slot_idx, bitmap);
181 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba,
182 struct scsi_cmnd *scsi_cmnd)
185 void *bitmap = hisi_hba->slot_index_tags;
188 return scsi_cmnd->request->tag;
190 spin_lock(&hisi_hba->lock);
191 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
192 hisi_hba->last_slot_index + 1);
193 if (index >= hisi_hba->slot_index_count) {
194 index = find_next_zero_bit(bitmap,
195 hisi_hba->slot_index_count,
196 HISI_SAS_UNRESERVED_IPTT);
197 if (index >= hisi_hba->slot_index_count) {
198 spin_unlock(&hisi_hba->lock);
199 return -SAS_QUEUE_FULL;
202 hisi_sas_slot_index_set(hisi_hba, index);
203 hisi_hba->last_slot_index = index;
204 spin_unlock(&hisi_hba->lock);
209 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
213 for (i = 0; i < hisi_hba->slot_index_count; ++i)
214 hisi_sas_slot_index_clear(hisi_hba, i);
217 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
218 struct hisi_sas_slot *slot)
220 int device_id = slot->device_id;
221 struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id];
224 struct device *dev = hisi_hba->dev;
226 if (!task->lldd_task)
229 task->lldd_task = NULL;
231 if (!sas_protocol_ata(task->task_proto)) {
233 dma_unmap_sg(dev, task->scatter,
236 if (slot->n_elem_dif) {
237 struct sas_ssp_task *ssp_task = &task->ssp_task;
238 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
240 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
241 scsi_prot_sg_count(scsi_cmnd),
247 spin_lock(&sas_dev->lock);
248 list_del_init(&slot->entry);
249 spin_unlock(&sas_dev->lock);
251 memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
253 hisi_sas_slot_index_free(hisi_hba, slot->idx);
255 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
257 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
258 struct hisi_sas_slot *slot)
260 hisi_hba->hw->prep_smp(hisi_hba, slot);
263 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
264 struct hisi_sas_slot *slot)
266 hisi_hba->hw->prep_ssp(hisi_hba, slot);
269 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
270 struct hisi_sas_slot *slot)
272 hisi_hba->hw->prep_stp(hisi_hba, slot);
275 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
276 struct hisi_sas_slot *slot,
277 int device_id, int abort_flag, int tag_to_abort)
279 hisi_hba->hw->prep_abort(hisi_hba, slot,
280 device_id, abort_flag, tag_to_abort);
283 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
284 struct sas_task *task, int n_elem,
287 struct device *dev = hisi_hba->dev;
289 if (!sas_protocol_ata(task->task_proto)) {
290 if (task->num_scatter) {
292 dma_unmap_sg(dev, task->scatter,
295 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
297 dma_unmap_sg(dev, &task->smp_task.smp_req,
303 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
304 struct sas_task *task, int *n_elem,
307 struct device *dev = hisi_hba->dev;
310 if (sas_protocol_ata(task->task_proto)) {
311 *n_elem = task->num_scatter;
313 unsigned int req_len;
315 if (task->num_scatter) {
316 *n_elem = dma_map_sg(dev, task->scatter,
317 task->num_scatter, task->data_dir);
322 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
323 *n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
329 req_len = sg_dma_len(&task->smp_task.smp_req);
332 goto err_out_dma_unmap;
337 if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
338 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT\n",
341 goto err_out_dma_unmap;
346 /* It would be better to call dma_unmap_sg() here, but it's messy */
347 hisi_sas_dma_unmap(hisi_hba, task, *n_elem,
353 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
354 struct sas_task *task, int n_elem_dif)
356 struct device *dev = hisi_hba->dev;
359 struct sas_ssp_task *ssp_task = &task->ssp_task;
360 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
362 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
363 scsi_prot_sg_count(scsi_cmnd),
368 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
369 int *n_elem_dif, struct sas_task *task)
371 struct device *dev = hisi_hba->dev;
372 struct sas_ssp_task *ssp_task;
373 struct scsi_cmnd *scsi_cmnd;
376 if (task->num_scatter) {
377 ssp_task = &task->ssp_task;
378 scsi_cmnd = ssp_task->cmd;
380 if (scsi_prot_sg_count(scsi_cmnd)) {
381 *n_elem_dif = dma_map_sg(dev,
382 scsi_prot_sglist(scsi_cmnd),
383 scsi_prot_sg_count(scsi_cmnd),
389 if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
390 dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
393 goto err_out_dif_dma_unmap;
400 err_out_dif_dma_unmap:
401 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
402 scsi_prot_sg_count(scsi_cmnd), task->data_dir);
406 static int hisi_sas_task_prep(struct sas_task *task,
407 struct hisi_sas_dq **dq_pointer,
408 bool is_tmf, struct hisi_sas_tmf_task *tmf,
411 struct domain_device *device = task->dev;
412 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
413 struct hisi_sas_device *sas_dev = device->lldd_dev;
414 struct hisi_sas_port *port;
415 struct hisi_sas_slot *slot;
416 struct hisi_sas_cmd_hdr *cmd_hdr_base;
417 struct asd_sas_port *sas_port = device->port;
418 struct device *dev = hisi_hba->dev;
419 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
420 int n_elem = 0, n_elem_dif = 0, n_elem_req = 0;
421 struct scsi_cmnd *scmd = NULL;
422 struct hisi_sas_dq *dq;
426 if (DEV_IS_GONE(sas_dev)) {
428 dev_info(dev, "task prep: device %d not ready\n",
431 dev_info(dev, "task prep: device %016llx not ready\n",
432 SAS_ADDR(device->sas_addr));
437 if (task->uldd_task) {
438 struct ata_queued_cmd *qc;
440 if (dev_is_sata(device)) {
441 qc = task->uldd_task;
444 scmd = task->uldd_task;
448 if (scmd && hisi_hba->shost->nr_hw_queues) {
449 unsigned int dq_index;
452 blk_tag = blk_mq_unique_tag(scmd->request);
453 dq_index = blk_mq_unique_tag_to_hwq(blk_tag);
454 *dq_pointer = dq = &hisi_hba->dq[dq_index];
455 } else if (hisi_hba->shost->nr_hw_queues) {
456 struct Scsi_Host *shost = hisi_hba->shost;
457 struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
458 int queue = qmap->mq_map[raw_smp_processor_id()];
460 *dq_pointer = dq = &hisi_hba->dq[queue];
462 *dq_pointer = dq = sas_dev->dq;
465 port = to_hisi_sas_port(sas_port);
466 if (port && !port->port_attached) {
467 dev_info(dev, "task prep: %s port%d not attach device\n",
468 (dev_is_sata(device)) ?
475 rc = hisi_sas_dma_map(hisi_hba, task, &n_elem,
480 if (!sas_protocol_ata(task->task_proto)) {
481 rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
483 goto err_out_dma_unmap;
486 if (hisi_hba->hw->slot_index_alloc)
487 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
489 rc = hisi_sas_slot_index_alloc(hisi_hba, scmd);
492 goto err_out_dif_dma_unmap;
495 slot = &hisi_hba->slot_info[slot_idx];
497 spin_lock(&dq->lock);
498 wr_q_index = dq->wr_point;
499 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
500 list_add_tail(&slot->delivery, &dq->list);
501 spin_unlock(&dq->lock);
502 spin_lock(&sas_dev->lock);
503 list_add_tail(&slot->entry, &sas_dev->list);
504 spin_unlock(&sas_dev->lock);
506 dlvry_queue = dq->id;
507 dlvry_queue_slot = wr_q_index;
509 slot->device_id = sas_dev->device_id;
510 slot->n_elem = n_elem;
511 slot->n_elem_dif = n_elem_dif;
512 slot->dlvry_queue = dlvry_queue;
513 slot->dlvry_queue_slot = dlvry_queue_slot;
514 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
515 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
519 slot->is_internal = is_tmf;
520 task->lldd_task = slot;
522 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
523 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
524 memset(hisi_sas_status_buf_addr_mem(slot), 0,
525 sizeof(struct hisi_sas_err_record));
527 switch (task->task_proto) {
528 case SAS_PROTOCOL_SMP:
529 hisi_sas_task_prep_smp(hisi_hba, slot);
531 case SAS_PROTOCOL_SSP:
532 hisi_sas_task_prep_ssp(hisi_hba, slot);
534 case SAS_PROTOCOL_SATA:
535 case SAS_PROTOCOL_STP:
536 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
537 hisi_sas_task_prep_ata(hisi_hba, slot);
540 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
545 spin_lock_irqsave(&task->task_state_lock, flags);
546 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
547 spin_unlock_irqrestore(&task->task_state_lock, flags);
550 WRITE_ONCE(slot->ready, 1);
554 err_out_dif_dma_unmap:
555 if (!sas_protocol_ata(task->task_proto))
556 hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
558 hisi_sas_dma_unmap(hisi_hba, task, n_elem,
561 dev_err(dev, "task prep: failed[%d]!\n", rc);
565 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
566 bool is_tmf, struct hisi_sas_tmf_task *tmf)
570 struct hisi_hba *hisi_hba;
572 struct domain_device *device = task->dev;
573 struct asd_sas_port *sas_port = device->port;
574 struct hisi_sas_dq *dq = NULL;
577 struct task_status_struct *ts = &task->task_status;
579 ts->resp = SAS_TASK_UNDELIVERED;
580 ts->stat = SAS_PHY_DOWN;
582 * libsas will use dev->port, should
583 * not call task_done for sata
585 if (device->dev_type != SAS_SATA_DEV)
586 task->task_done(task);
590 hisi_hba = dev_to_hisi_hba(device);
593 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
595 * For IOs from upper layer, it may already disable preempt
596 * in the IO path, if disable preempt again in down(),
597 * function schedule() will report schedule_bug(), so check
598 * preemptible() before goto down().
603 down(&hisi_hba->sem);
607 /* protect task_prep and start_delivery sequence */
608 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
610 dev_err(dev, "task exec: failed[%d]!\n", rc);
613 spin_lock(&dq->lock);
614 hisi_hba->hw->start_delivery(dq);
615 spin_unlock(&dq->lock);
621 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
623 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
624 struct asd_sas_phy *sas_phy = &phy->sas_phy;
626 if (!phy->phy_attached)
629 if (test_bit(HISI_SAS_PM_BIT, &hisi_hba->flags) &&
630 !sas_phy->suspended) {
631 dev_warn(hisi_hba->dev, "phy%d during suspend filtered out\n", phy_no);
635 sas_notify_phy_event(sas_phy, PHYE_OOB_DONE);
638 struct sas_phy *sphy = sas_phy->phy;
640 sphy->negotiated_linkrate = sas_phy->linkrate;
641 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
642 sphy->maximum_linkrate_hw =
643 hisi_hba->hw->phy_get_max_linkrate();
644 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
645 sphy->minimum_linkrate = phy->minimum_linkrate;
647 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
648 sphy->maximum_linkrate = phy->maximum_linkrate;
651 if (phy->phy_type & PORT_TYPE_SAS) {
652 struct sas_identify_frame *id;
654 id = (struct sas_identify_frame *)phy->frame_rcvd;
655 id->dev_type = phy->identify.device_type;
656 id->initiator_bits = SAS_PROTOCOL_ALL;
657 id->target_bits = phy->identify.target_port_protocols;
658 } else if (phy->phy_type & PORT_TYPE_SATA) {
662 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
663 sas_notify_port_event(sas_phy, PORTE_BYTES_DMAED);
666 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
668 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
669 struct hisi_sas_device *sas_dev = NULL;
670 int last = hisi_hba->last_dev_id;
671 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
674 spin_lock(&hisi_hba->lock);
675 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
676 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
677 int queue = i % hisi_hba->queue_count;
678 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
680 hisi_hba->devices[i].device_id = i;
681 sas_dev = &hisi_hba->devices[i];
682 sas_dev->dev_status = HISI_SAS_DEV_INIT;
683 sas_dev->dev_type = device->dev_type;
684 sas_dev->hisi_hba = hisi_hba;
685 sas_dev->sas_device = device;
687 spin_lock_init(&sas_dev->lock);
688 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
693 hisi_hba->last_dev_id = i;
694 spin_unlock(&hisi_hba->lock);
699 #define HISI_SAS_DISK_RECOVER_CNT 3
700 static int hisi_sas_init_device(struct domain_device *device)
702 int rc = TMF_RESP_FUNC_COMPLETE;
704 struct hisi_sas_tmf_task tmf_task;
705 int retry = HISI_SAS_DISK_RECOVER_CNT;
706 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
707 struct device *dev = hisi_hba->dev;
708 struct sas_phy *local_phy;
710 switch (device->dev_type) {
712 int_to_scsilun(0, &lun);
714 tmf_task.tmf = TMF_CLEAR_TASK_SET;
715 while (retry-- > 0) {
716 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
718 if (rc == TMF_RESP_FUNC_COMPLETE) {
719 hisi_sas_release_task(hisi_hba, device);
726 case SAS_SATA_PM_PORT:
727 case SAS_SATA_PENDING:
729 * send HARD RESET to clear previous affiliation of
732 local_phy = sas_get_local_phy(device);
733 if (!scsi_is_sas_phy_local(local_phy) &&
734 !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
735 unsigned long deadline = ata_deadline(jiffies, 20000);
736 struct sata_device *sata_dev = &device->sata_dev;
737 struct ata_host *ata_host = sata_dev->ata_host;
738 struct ata_port_operations *ops = ata_host->ops;
739 struct ata_port *ap = sata_dev->ap;
740 struct ata_link *link;
741 unsigned int classes;
743 ata_for_each_link(link, ap, EDGE)
744 rc = ops->hardreset(link, &classes,
747 sas_put_local_phy(local_phy);
749 dev_warn(dev, "SATA disk hardreset fail: %d\n", rc);
753 while (retry-- > 0) {
754 rc = hisi_sas_softreset_ata_disk(device);
766 static int hisi_sas_dev_found(struct domain_device *device)
768 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
769 struct domain_device *parent_dev = device->parent;
770 struct hisi_sas_device *sas_dev;
771 struct device *dev = hisi_hba->dev;
774 if (hisi_hba->hw->alloc_dev)
775 sas_dev = hisi_hba->hw->alloc_dev(device);
777 sas_dev = hisi_sas_alloc_dev(device);
779 dev_err(dev, "fail alloc dev: max support %d devices\n",
780 HISI_SAS_MAX_DEVICES);
784 device->lldd_dev = sas_dev;
785 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
787 if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
789 u8 phy_num = parent_dev->ex_dev.num_phys;
792 for (phy_no = 0; phy_no < phy_num; phy_no++) {
793 phy = &parent_dev->ex_dev.ex_phy[phy_no];
794 if (SAS_ADDR(phy->attached_sas_addr) ==
795 SAS_ADDR(device->sas_addr))
799 if (phy_no == phy_num) {
800 dev_info(dev, "dev found: no attached "
801 "dev:%016llx at ex:%016llx\n",
802 SAS_ADDR(device->sas_addr),
803 SAS_ADDR(parent_dev->sas_addr));
809 dev_info(dev, "dev[%d:%x] found\n",
810 sas_dev->device_id, sas_dev->dev_type);
812 rc = hisi_sas_init_device(device);
815 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
819 hisi_sas_dev_gone(device);
823 int hisi_sas_slave_configure(struct scsi_device *sdev)
825 struct domain_device *dev = sdev_to_domain_dev(sdev);
826 int ret = sas_slave_configure(sdev);
830 if (!dev_is_sata(dev))
831 sas_change_queue_depth(sdev, 64);
835 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
837 void hisi_sas_scan_start(struct Scsi_Host *shost)
839 struct hisi_hba *hisi_hba = shost_priv(shost);
841 hisi_hba->hw->phys_init(hisi_hba);
843 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
845 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
847 struct hisi_hba *hisi_hba = shost_priv(shost);
848 struct sas_ha_struct *sha = &hisi_hba->sha;
850 /* Wait for PHY up interrupt to occur */
857 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
859 static void hisi_sas_phyup_work(struct work_struct *work)
861 struct hisi_sas_phy *phy =
862 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
863 struct hisi_hba *hisi_hba = phy->hisi_hba;
864 struct asd_sas_phy *sas_phy = &phy->sas_phy;
865 int phy_no = sas_phy->id;
867 if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
868 hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
869 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
872 static void hisi_sas_linkreset_work(struct work_struct *work)
874 struct hisi_sas_phy *phy =
875 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
876 struct asd_sas_phy *sas_phy = &phy->sas_phy;
878 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
881 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
882 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
883 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
886 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
887 enum hisi_sas_phy_event event)
889 struct hisi_hba *hisi_hba = phy->hisi_hba;
891 if (WARN_ON(event >= HISI_PHYES_NUM))
894 return queue_work(hisi_hba->wq, &phy->works[event]);
896 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
898 static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
900 struct hisi_sas_phy *phy = from_timer(phy, t, timer);
901 struct hisi_hba *hisi_hba = phy->hisi_hba;
902 struct device *dev = hisi_hba->dev;
903 int phy_no = phy->sas_phy.id;
905 dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
906 hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
909 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
911 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
912 struct device *dev = hisi_hba->dev;
914 dev_dbg(dev, "phy%d OOB ready\n", phy_no);
915 if (phy->phy_attached)
918 if (!timer_pending(&phy->timer)) {
919 phy->timer.expires = jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT * HZ;
920 add_timer(&phy->timer);
923 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
925 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
927 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
928 struct asd_sas_phy *sas_phy = &phy->sas_phy;
931 phy->hisi_hba = hisi_hba;
933 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
934 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
935 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
936 sas_phy->class = SAS;
937 sas_phy->iproto = SAS_PROTOCOL_ALL;
939 sas_phy->type = PHY_TYPE_PHYSICAL;
940 sas_phy->role = PHY_ROLE_INITIATOR;
941 sas_phy->oob_mode = OOB_NOT_CONNECTED;
942 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
943 sas_phy->id = phy_no;
944 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
945 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
946 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
947 sas_phy->lldd_phy = phy;
949 for (i = 0; i < HISI_PHYES_NUM; i++)
950 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
952 spin_lock_init(&phy->lock);
954 timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
957 /* Wrapper to ensure we track hisi_sas_phy.enable properly */
958 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable)
960 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
961 struct asd_sas_phy *aphy = &phy->sas_phy;
962 struct sas_phy *sphy = aphy->phy;
965 spin_lock_irqsave(&phy->lock, flags);
968 /* We may have been enabled already; if so, don't touch */
970 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
971 hisi_hba->hw->phy_start(hisi_hba, phy_no);
973 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
974 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
976 phy->enable = enable;
977 spin_unlock_irqrestore(&phy->lock, flags);
979 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable);
981 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
983 struct sas_ha_struct *sas_ha = sas_phy->ha;
984 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
985 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
986 struct asd_sas_port *sas_port = sas_phy->port;
987 struct hisi_sas_port *port;
993 port = to_hisi_sas_port(sas_port);
994 spin_lock_irqsave(&hisi_hba->lock, flags);
995 port->port_attached = 1;
996 port->id = phy->port_id;
998 sas_port->lldd_port = port;
999 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1002 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
1003 struct hisi_sas_slot *slot)
1006 unsigned long flags;
1007 struct task_status_struct *ts;
1009 ts = &task->task_status;
1011 ts->resp = SAS_TASK_COMPLETE;
1012 ts->stat = SAS_ABORTED_TASK;
1013 spin_lock_irqsave(&task->task_state_lock, flags);
1014 task->task_state_flags &=
1015 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1016 if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
1017 task->task_state_flags |= SAS_TASK_STATE_DONE;
1018 spin_unlock_irqrestore(&task->task_state_lock, flags);
1021 hisi_sas_slot_task_free(hisi_hba, task, slot);
1024 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
1025 struct domain_device *device)
1027 struct hisi_sas_slot *slot, *slot2;
1028 struct hisi_sas_device *sas_dev = device->lldd_dev;
1030 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
1031 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
1034 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
1036 struct hisi_sas_device *sas_dev;
1037 struct domain_device *device;
1040 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1041 sas_dev = &hisi_hba->devices[i];
1042 device = sas_dev->sas_device;
1044 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
1048 hisi_sas_release_task(hisi_hba, device);
1051 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
1053 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
1054 struct domain_device *device)
1056 if (hisi_hba->hw->dereg_device)
1057 hisi_hba->hw->dereg_device(hisi_hba, device);
1060 static void hisi_sas_dev_gone(struct domain_device *device)
1062 struct hisi_sas_device *sas_dev = device->lldd_dev;
1063 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1064 struct device *dev = hisi_hba->dev;
1067 dev_info(dev, "dev[%d:%x] is gone\n",
1068 sas_dev->device_id, sas_dev->dev_type);
1070 down(&hisi_hba->sem);
1071 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
1072 hisi_sas_internal_task_abort(hisi_hba, device,
1073 HISI_SAS_INT_ABT_DEV, 0);
1075 hisi_sas_dereg_device(hisi_hba, device);
1077 ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
1078 device->lldd_dev = NULL;
1081 if (hisi_hba->hw->free_device)
1082 hisi_hba->hw->free_device(sas_dev);
1084 /* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */
1086 sas_dev->dev_type = SAS_PHY_UNUSED;
1087 sas_dev->sas_device = NULL;
1091 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
1093 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
1096 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
1097 struct sas_phy_linkrates *r)
1099 struct sas_phy_linkrates _r;
1101 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1102 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1103 enum sas_linkrate min, max;
1105 if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
1108 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1109 max = sas_phy->phy->maximum_linkrate;
1110 min = r->minimum_linkrate;
1111 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1112 max = r->maximum_linkrate;
1113 min = sas_phy->phy->minimum_linkrate;
1117 _r.maximum_linkrate = max;
1118 _r.minimum_linkrate = min;
1120 sas_phy->phy->maximum_linkrate = max;
1121 sas_phy->phy->minimum_linkrate = min;
1123 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1125 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
1126 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1131 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
1134 struct sas_ha_struct *sas_ha = sas_phy->ha;
1135 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1136 int phy_no = sas_phy->id;
1139 case PHY_FUNC_HARD_RESET:
1140 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
1143 case PHY_FUNC_LINK_RESET:
1144 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1146 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1149 case PHY_FUNC_DISABLE:
1150 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1153 case PHY_FUNC_SET_LINK_RATE:
1154 return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1155 case PHY_FUNC_GET_EVENTS:
1156 if (hisi_hba->hw->get_events) {
1157 hisi_hba->hw->get_events(hisi_hba, phy_no);
1161 case PHY_FUNC_RELEASE_SPINUP_HOLD:
1168 static void hisi_sas_task_done(struct sas_task *task)
1170 del_timer(&task->slow_task->timer);
1171 complete(&task->slow_task->completion);
1174 static void hisi_sas_tmf_timedout(struct timer_list *t)
1176 struct sas_task_slow *slow = from_timer(slow, t, timer);
1177 struct sas_task *task = slow->task;
1178 unsigned long flags;
1179 bool is_completed = true;
1181 spin_lock_irqsave(&task->task_state_lock, flags);
1182 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1183 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1184 is_completed = false;
1186 spin_unlock_irqrestore(&task->task_state_lock, flags);
1189 complete(&task->slow_task->completion);
1192 #define TASK_TIMEOUT 20
1193 #define TASK_RETRY 3
1194 #define INTERNAL_ABORT_TIMEOUT 6
1195 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1196 void *parameter, u32 para_len,
1197 struct hisi_sas_tmf_task *tmf)
1199 struct hisi_sas_device *sas_dev = device->lldd_dev;
1200 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1201 struct device *dev = hisi_hba->dev;
1202 struct sas_task *task;
1205 for (retry = 0; retry < TASK_RETRY; retry++) {
1206 task = sas_alloc_slow_task(GFP_KERNEL);
1211 task->task_proto = device->tproto;
1213 if (dev_is_sata(device)) {
1214 task->ata_task.device_control_reg_update = 1;
1215 memcpy(&task->ata_task.fis, parameter, para_len);
1217 memcpy(&task->ssp_task, parameter, para_len);
1219 task->task_done = hisi_sas_task_done;
1221 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1222 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT * HZ;
1223 add_timer(&task->slow_task->timer);
1225 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1228 del_timer(&task->slow_task->timer);
1229 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1234 wait_for_completion(&task->slow_task->completion);
1235 res = TMF_RESP_FUNC_FAILED;
1236 /* Even TMF timed out, return direct. */
1237 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1238 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1239 struct hisi_sas_slot *slot = task->lldd_task;
1241 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1243 struct hisi_sas_cq *cq =
1244 &hisi_hba->cq[slot->dlvry_queue];
1246 * sync irq to avoid free'ing task
1247 * before using task in IO completion
1249 synchronize_irq(cq->irq_no);
1255 dev_err(dev, "abort tmf: TMF task timeout\n");
1258 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1259 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1260 res = TMF_RESP_FUNC_COMPLETE;
1264 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1265 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1266 res = TMF_RESP_FUNC_SUCC;
1270 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1271 task->task_status.stat == SAS_DATA_UNDERRUN) {
1272 /* no error, but return the number of bytes of
1275 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x sts 0x%x underrun\n",
1276 SAS_ADDR(device->sas_addr),
1277 task->task_status.resp,
1278 task->task_status.stat);
1279 res = task->task_status.residual;
1283 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1284 task->task_status.stat == SAS_DATA_OVERRUN) {
1285 dev_warn(dev, "abort tmf: blocked task error\n");
1290 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1291 task->task_status.stat == SAS_OPEN_REJECT) {
1292 dev_warn(dev, "abort tmf: open reject failed\n");
1295 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x status 0x%x\n",
1296 SAS_ADDR(device->sas_addr),
1297 task->task_status.resp,
1298 task->task_status.stat);
1300 sas_free_task(task);
1304 if (retry == TASK_RETRY)
1305 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1306 sas_free_task(task);
1310 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1311 bool reset, int pmp, u8 *fis)
1313 struct ata_taskfile tf;
1315 ata_tf_init(dev, &tf);
1319 tf.ctl &= ~ATA_SRST;
1320 tf.command = ATA_CMD_DEV_RESET;
1321 ata_tf_to_fis(&tf, pmp, 0, fis);
1324 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1327 struct ata_port *ap = device->sata_dev.ap;
1328 struct ata_link *link;
1329 int rc = TMF_RESP_FUNC_FAILED;
1330 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1331 struct device *dev = hisi_hba->dev;
1332 int s = sizeof(struct host_to_dev_fis);
1334 ata_for_each_link(link, ap, EDGE) {
1335 int pmp = sata_srst_pmp(link);
1337 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1338 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1339 if (rc != TMF_RESP_FUNC_COMPLETE)
1343 if (rc == TMF_RESP_FUNC_COMPLETE) {
1344 ata_for_each_link(link, ap, EDGE) {
1345 int pmp = sata_srst_pmp(link);
1347 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1348 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1350 if (rc != TMF_RESP_FUNC_COMPLETE)
1351 dev_err(dev, "ata disk de-reset failed\n");
1354 dev_err(dev, "ata disk reset failed\n");
1357 if (rc == TMF_RESP_FUNC_COMPLETE)
1358 hisi_sas_release_task(hisi_hba, device);
1363 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1364 u8 *lun, struct hisi_sas_tmf_task *tmf)
1366 struct sas_ssp_task ssp_task;
1368 if (!(device->tproto & SAS_PROTOCOL_SSP))
1369 return TMF_RESP_FUNC_ESUPP;
1371 memcpy(ssp_task.LUN, lun, 8);
1373 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1374 sizeof(ssp_task), tmf);
1377 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1379 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1382 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1383 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1384 struct domain_device *device = sas_dev->sas_device;
1385 struct asd_sas_port *sas_port;
1386 struct hisi_sas_port *port;
1387 struct hisi_sas_phy *phy = NULL;
1388 struct asd_sas_phy *sas_phy;
1390 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1391 || !device || !device->port)
1394 sas_port = device->port;
1395 port = to_hisi_sas_port(sas_port);
1397 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1398 if (state & BIT(sas_phy->id)) {
1399 phy = sas_phy->lldd_phy;
1404 port->id = phy->port_id;
1406 /* Update linkrate of directly attached device. */
1407 if (!device->parent)
1408 device->linkrate = phy->sas_phy.linkrate;
1410 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1411 } else if (!port->port_attached)
1416 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state)
1418 struct asd_sas_port *_sas_port = NULL;
1421 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1422 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1423 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1424 struct asd_sas_port *sas_port = sas_phy->port;
1425 bool do_port_check = _sas_port != sas_port;
1427 if (!sas_phy->phy->enabled)
1430 /* Report PHY state change to libsas */
1431 if (state & BIT(phy_no)) {
1432 if (do_port_check && sas_port && sas_port->port_dev) {
1433 struct domain_device *dev = sas_port->port_dev;
1435 _sas_port = sas_port;
1437 if (dev_is_expander(dev->dev_type))
1438 sas_notify_port_event(sas_phy,
1439 PORTE_BROADCAST_RCVD);
1442 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1447 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1449 struct hisi_sas_device *sas_dev;
1450 struct domain_device *device;
1453 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1454 sas_dev = &hisi_hba->devices[i];
1455 device = sas_dev->sas_device;
1457 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1460 hisi_sas_init_device(device);
1464 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1465 struct asd_sas_port *sas_port,
1466 struct domain_device *device)
1468 struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1469 struct ata_port *ap = device->sata_dev.ap;
1470 struct device *dev = hisi_hba->dev;
1471 int s = sizeof(struct host_to_dev_fis);
1472 int rc = TMF_RESP_FUNC_FAILED;
1473 struct asd_sas_phy *sas_phy;
1474 struct ata_link *link;
1478 state = hisi_hba->hw->get_phys_state(hisi_hba);
1479 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1480 if (!(state & BIT(sas_phy->id)))
1483 ata_for_each_link(link, ap, EDGE) {
1484 int pmp = sata_srst_pmp(link);
1486 tmf_task.phy_id = sas_phy->id;
1487 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1488 rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1490 if (rc != TMF_RESP_FUNC_COMPLETE) {
1491 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1499 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1501 struct device *dev = hisi_hba->dev;
1504 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1505 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1506 struct domain_device *device = sas_dev->sas_device;
1508 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1511 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1512 HISI_SAS_INT_ABT_DEV, 0);
1514 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1517 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1518 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1519 struct asd_sas_port *sas_port = &port->sas_port;
1520 struct domain_device *port_dev = sas_port->port_dev;
1521 struct domain_device *device;
1523 if (!port_dev || !dev_is_expander(port_dev->dev_type))
1526 /* Try to find a SATA device */
1527 list_for_each_entry(device, &sas_port->dev_list,
1529 if (dev_is_sata(device)) {
1530 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1539 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1541 struct Scsi_Host *shost = hisi_hba->shost;
1543 down(&hisi_hba->sem);
1544 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1546 scsi_block_requests(shost);
1547 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1549 if (timer_pending(&hisi_hba->timer))
1550 del_timer_sync(&hisi_hba->timer);
1552 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1554 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1556 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1558 struct Scsi_Host *shost = hisi_hba->shost;
1560 /* Init and wait for PHYs to come up and all libsas event finished. */
1561 hisi_hba->hw->phys_init(hisi_hba);
1563 hisi_sas_refresh_port_id(hisi_hba);
1564 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1566 if (hisi_hba->reject_stp_links_msk)
1567 hisi_sas_terminate_stp_reject(hisi_hba);
1568 hisi_sas_reset_init_all_devices(hisi_hba);
1570 scsi_unblock_requests(shost);
1571 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1573 hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state);
1575 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1577 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1579 struct device *dev = hisi_hba->dev;
1580 struct Scsi_Host *shost = hisi_hba->shost;
1583 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
1584 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
1586 if (!hisi_hba->hw->soft_reset)
1589 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1592 dev_info(dev, "controller resetting...\n");
1593 hisi_sas_controller_reset_prepare(hisi_hba);
1595 rc = hisi_hba->hw->soft_reset(hisi_hba);
1597 dev_warn(dev, "controller reset failed (%d)\n", rc);
1598 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1600 scsi_unblock_requests(shost);
1601 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1605 hisi_sas_controller_reset_done(hisi_hba);
1606 dev_info(dev, "controller reset complete\n");
1611 static int hisi_sas_abort_task(struct sas_task *task)
1613 struct scsi_lun lun;
1614 struct hisi_sas_tmf_task tmf_task;
1615 struct domain_device *device = task->dev;
1616 struct hisi_sas_device *sas_dev = device->lldd_dev;
1617 struct hisi_hba *hisi_hba;
1619 int rc = TMF_RESP_FUNC_FAILED;
1620 unsigned long flags;
1623 return TMF_RESP_FUNC_FAILED;
1625 hisi_hba = dev_to_hisi_hba(task->dev);
1626 dev = hisi_hba->dev;
1628 spin_lock_irqsave(&task->task_state_lock, flags);
1629 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1630 struct hisi_sas_slot *slot = task->lldd_task;
1631 struct hisi_sas_cq *cq;
1635 * sync irq to avoid free'ing task
1636 * before using task in IO completion
1638 cq = &hisi_hba->cq[slot->dlvry_queue];
1639 synchronize_irq(cq->irq_no);
1641 spin_unlock_irqrestore(&task->task_state_lock, flags);
1642 rc = TMF_RESP_FUNC_COMPLETE;
1645 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1646 spin_unlock_irqrestore(&task->task_state_lock, flags);
1648 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1649 struct scsi_cmnd *cmnd = task->uldd_task;
1650 struct hisi_sas_slot *slot = task->lldd_task;
1651 u16 tag = slot->idx;
1654 int_to_scsilun(cmnd->device->lun, &lun);
1655 tmf_task.tmf = TMF_ABORT_TASK;
1656 tmf_task.tag_of_task_to_be_managed = tag;
1658 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1661 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1662 HISI_SAS_INT_ABT_CMD, tag);
1664 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1665 return TMF_RESP_FUNC_FAILED;
1669 * If the TMF finds that the IO is not in the device and also
1670 * the internal abort does not succeed, then it is safe to
1672 * Note: if the internal abort succeeds then the slot
1673 * will have already been completed
1675 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1676 if (task->lldd_task)
1677 hisi_sas_do_release_task(hisi_hba, task, slot);
1679 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1680 task->task_proto & SAS_PROTOCOL_STP) {
1681 if (task->dev->dev_type == SAS_SATA_DEV) {
1682 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1683 HISI_SAS_INT_ABT_DEV,
1686 dev_err(dev, "abort task: internal abort failed\n");
1689 hisi_sas_dereg_device(hisi_hba, device);
1690 rc = hisi_sas_softreset_ata_disk(device);
1692 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1694 struct hisi_sas_slot *slot = task->lldd_task;
1695 u32 tag = slot->idx;
1696 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1698 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1699 HISI_SAS_INT_ABT_CMD, tag);
1700 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1703 * sync irq to avoid free'ing task
1704 * before using task in IO completion
1706 synchronize_irq(cq->irq_no);
1712 if (rc != TMF_RESP_FUNC_COMPLETE)
1713 dev_notice(dev, "abort task: rc=%d\n", rc);
1717 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1719 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1720 struct device *dev = hisi_hba->dev;
1721 struct hisi_sas_tmf_task tmf_task;
1724 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1725 HISI_SAS_INT_ABT_DEV, 0);
1727 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1728 return TMF_RESP_FUNC_FAILED;
1730 hisi_sas_dereg_device(hisi_hba, device);
1732 tmf_task.tmf = TMF_ABORT_TASK_SET;
1733 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1735 if (rc == TMF_RESP_FUNC_COMPLETE)
1736 hisi_sas_release_task(hisi_hba, device);
1741 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1743 struct hisi_sas_tmf_task tmf_task;
1746 tmf_task.tmf = TMF_CLEAR_ACA;
1747 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1752 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1754 struct sas_phy *local_phy = sas_get_local_phy(device);
1755 struct hisi_sas_device *sas_dev = device->lldd_dev;
1756 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1757 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1758 DECLARE_COMPLETION_ONSTACK(phyreset);
1761 if (!local_phy->enabled) {
1762 sas_put_local_phy(local_phy);
1766 if (scsi_is_sas_phy_local(local_phy)) {
1767 struct asd_sas_phy *sas_phy =
1768 sas_ha->sas_phy[local_phy->number];
1769 struct hisi_sas_phy *phy =
1770 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1772 phy->reset_completion = &phyreset;
1775 reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
1776 !dev_is_sata(device)) ? true : false;
1778 rc = sas_phy_reset(local_phy, reset_type);
1779 sas_put_local_phy(local_phy);
1781 if (scsi_is_sas_phy_local(local_phy)) {
1782 struct asd_sas_phy *sas_phy =
1783 sas_ha->sas_phy[local_phy->number];
1784 struct hisi_sas_phy *phy =
1785 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1786 int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1787 unsigned long flags;
1789 spin_lock_irqsave(&phy->lock, flags);
1790 phy->reset_completion = NULL;
1792 spin_unlock_irqrestore(&phy->lock, flags);
1794 /* report PHY down if timed out */
1796 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
1797 } else if (sas_dev->dev_status != HISI_SAS_DEV_INIT) {
1799 * If in init state, we rely on caller to wait for link to be
1800 * ready; otherwise, except phy reset is fail, delay.
1809 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1811 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1812 struct device *dev = hisi_hba->dev;
1815 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1816 HISI_SAS_INT_ABT_DEV, 0);
1818 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1819 return TMF_RESP_FUNC_FAILED;
1821 hisi_sas_dereg_device(hisi_hba, device);
1823 if (dev_is_sata(device)) {
1824 rc = hisi_sas_softreset_ata_disk(device);
1825 if (rc == TMF_RESP_FUNC_FAILED)
1826 return TMF_RESP_FUNC_FAILED;
1829 rc = hisi_sas_debug_I_T_nexus_reset(device);
1831 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1832 hisi_sas_release_task(hisi_hba, device);
1837 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1839 struct hisi_sas_device *sas_dev = device->lldd_dev;
1840 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1841 struct device *dev = hisi_hba->dev;
1842 int rc = TMF_RESP_FUNC_FAILED;
1844 /* Clear internal IO and then lu reset */
1845 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1846 HISI_SAS_INT_ABT_DEV, 0);
1848 dev_err(dev, "lu_reset: internal abort failed\n");
1851 hisi_sas_dereg_device(hisi_hba, device);
1853 if (dev_is_sata(device)) {
1854 struct sas_phy *phy;
1856 phy = sas_get_local_phy(device);
1858 rc = sas_phy_reset(phy, true);
1861 hisi_sas_release_task(hisi_hba, device);
1862 sas_put_local_phy(phy);
1864 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1866 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1867 if (rc == TMF_RESP_FUNC_COMPLETE)
1868 hisi_sas_release_task(hisi_hba, device);
1871 if (rc != TMF_RESP_FUNC_COMPLETE)
1872 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1873 sas_dev->device_id, rc);
1877 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1879 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1880 struct device *dev = hisi_hba->dev;
1881 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1884 queue_work(hisi_hba->wq, &r.work);
1885 wait_for_completion(r.completion);
1887 return TMF_RESP_FUNC_FAILED;
1889 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1890 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1891 struct domain_device *device = sas_dev->sas_device;
1893 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1894 dev_is_expander(device->dev_type))
1897 rc = hisi_sas_debug_I_T_nexus_reset(device);
1898 if (rc != TMF_RESP_FUNC_COMPLETE)
1899 dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
1900 sas_dev->device_id, rc);
1903 hisi_sas_release_tasks(hisi_hba);
1905 return TMF_RESP_FUNC_COMPLETE;
1908 static int hisi_sas_query_task(struct sas_task *task)
1910 struct scsi_lun lun;
1911 struct hisi_sas_tmf_task tmf_task;
1912 int rc = TMF_RESP_FUNC_FAILED;
1914 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1915 struct scsi_cmnd *cmnd = task->uldd_task;
1916 struct domain_device *device = task->dev;
1917 struct hisi_sas_slot *slot = task->lldd_task;
1918 u32 tag = slot->idx;
1920 int_to_scsilun(cmnd->device->lun, &lun);
1921 tmf_task.tmf = TMF_QUERY_TASK;
1922 tmf_task.tag_of_task_to_be_managed = tag;
1924 rc = hisi_sas_debug_issue_ssp_tmf(device,
1928 /* The task is still in Lun, release it then */
1929 case TMF_RESP_FUNC_SUCC:
1930 /* The task is not in Lun or failed, reset the phy */
1931 case TMF_RESP_FUNC_FAILED:
1932 case TMF_RESP_FUNC_COMPLETE:
1935 rc = TMF_RESP_FUNC_FAILED;
1943 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1944 struct sas_task *task, int abort_flag,
1945 int task_tag, struct hisi_sas_dq *dq)
1947 struct domain_device *device = task->dev;
1948 struct hisi_sas_device *sas_dev = device->lldd_dev;
1949 struct device *dev = hisi_hba->dev;
1950 struct hisi_sas_port *port;
1951 struct hisi_sas_slot *slot;
1952 struct asd_sas_port *sas_port = device->port;
1953 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1954 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1955 unsigned long flags;
1958 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1964 port = to_hisi_sas_port(sas_port);
1966 /* simply get a slot and send abort command */
1967 rc = hisi_sas_slot_index_alloc(hisi_hba, NULL);
1972 slot = &hisi_hba->slot_info[slot_idx];
1974 spin_lock(&dq->lock);
1975 wr_q_index = dq->wr_point;
1976 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
1977 list_add_tail(&slot->delivery, &dq->list);
1978 spin_unlock(&dq->lock);
1979 spin_lock(&sas_dev->lock);
1980 list_add_tail(&slot->entry, &sas_dev->list);
1981 spin_unlock(&sas_dev->lock);
1983 dlvry_queue = dq->id;
1984 dlvry_queue_slot = wr_q_index;
1986 slot->device_id = sas_dev->device_id;
1987 slot->n_elem = n_elem;
1988 slot->dlvry_queue = dlvry_queue;
1989 slot->dlvry_queue_slot = dlvry_queue_slot;
1990 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1991 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1994 slot->is_internal = true;
1995 task->lldd_task = slot;
1997 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1998 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1999 memset(hisi_sas_status_buf_addr_mem(slot), 0,
2000 sizeof(struct hisi_sas_err_record));
2002 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
2003 abort_flag, task_tag);
2005 spin_lock_irqsave(&task->task_state_lock, flags);
2006 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
2007 spin_unlock_irqrestore(&task->task_state_lock, flags);
2008 WRITE_ONCE(slot->ready, 1);
2009 /* send abort command to the chip */
2010 spin_lock(&dq->lock);
2011 hisi_hba->hw->start_delivery(dq);
2012 spin_unlock(&dq->lock);
2017 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
2023 * _hisi_sas_internal_task_abort -- execute an internal
2024 * abort command for single IO command or a device
2025 * @hisi_hba: host controller struct
2026 * @device: domain device
2027 * @abort_flag: mode of operation, device or single IO
2028 * @tag: tag of IO to be aborted (only relevant to single
2030 * @dq: delivery queue for this internal abort command
2033 _hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2034 struct domain_device *device, int abort_flag,
2035 int tag, struct hisi_sas_dq *dq)
2037 struct sas_task *task;
2038 struct hisi_sas_device *sas_dev = device->lldd_dev;
2039 struct device *dev = hisi_hba->dev;
2043 * The interface is not realized means this HW don't support internal
2044 * abort, or don't need to do internal abort. Then here, we return
2045 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
2046 * the internal abort has been executed and returned CQ.
2048 if (!hisi_hba->hw->prep_abort)
2049 return TMF_RESP_FUNC_FAILED;
2051 task = sas_alloc_slow_task(GFP_KERNEL);
2056 task->task_proto = device->tproto;
2057 task->task_done = hisi_sas_task_done;
2058 task->slow_task->timer.function = hisi_sas_tmf_timedout;
2059 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT * HZ;
2060 add_timer(&task->slow_task->timer);
2062 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
2063 task, abort_flag, tag, dq);
2065 del_timer(&task->slow_task->timer);
2066 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
2070 wait_for_completion(&task->slow_task->completion);
2071 res = TMF_RESP_FUNC_FAILED;
2073 /* Internal abort timed out */
2074 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
2075 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
2076 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
2078 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
2079 struct hisi_sas_slot *slot = task->lldd_task;
2082 struct hisi_sas_cq *cq =
2083 &hisi_hba->cq[slot->dlvry_queue];
2085 * sync irq to avoid free'ing task
2086 * before using task in IO completion
2088 synchronize_irq(cq->irq_no);
2091 dev_err(dev, "internal task abort: timeout and not done.\n");
2096 dev_err(dev, "internal task abort: timeout.\n");
2099 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2100 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
2101 res = TMF_RESP_FUNC_COMPLETE;
2105 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2106 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
2107 res = TMF_RESP_FUNC_SUCC;
2112 dev_dbg(dev, "internal task abort: task to dev %016llx task=%pK resp: 0x%x sts 0x%x\n",
2113 SAS_ADDR(device->sas_addr), task,
2114 task->task_status.resp, /* 0 is complete, -1 is undelivered */
2115 task->task_status.stat);
2116 sas_free_task(task);
2122 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2123 struct domain_device *device,
2124 int abort_flag, int tag)
2126 struct hisi_sas_slot *slot;
2127 struct device *dev = hisi_hba->dev;
2128 struct hisi_sas_dq *dq;
2131 switch (abort_flag) {
2132 case HISI_SAS_INT_ABT_CMD:
2133 slot = &hisi_hba->slot_info[tag];
2134 dq = &hisi_hba->dq[slot->dlvry_queue];
2135 return _hisi_sas_internal_task_abort(hisi_hba, device,
2136 abort_flag, tag, dq);
2137 case HISI_SAS_INT_ABT_DEV:
2138 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2139 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2140 const struct cpumask *mask = cq->irq_mask;
2142 if (mask && !cpumask_intersects(cpu_online_mask, mask))
2144 dq = &hisi_hba->dq[i];
2145 rc = _hisi_sas_internal_task_abort(hisi_hba, device,
2153 dev_err(dev, "Unrecognised internal abort flag (%d)\n",
2161 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
2163 hisi_sas_port_notify_formed(sas_phy);
2166 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
2167 u8 reg_index, u8 reg_count, u8 *write_data)
2169 struct hisi_hba *hisi_hba = sha->lldd_ha;
2171 if (!hisi_hba->hw->write_gpio)
2174 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
2175 reg_index, reg_count, write_data);
2178 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
2180 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2181 struct sas_phy *sphy = sas_phy->phy;
2182 unsigned long flags;
2184 phy->phy_attached = 0;
2188 spin_lock_irqsave(&phy->lock, flags);
2190 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
2192 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
2193 spin_unlock_irqrestore(&phy->lock, flags);
2196 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
2198 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2199 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2200 struct device *dev = hisi_hba->dev;
2203 /* Phy down but ready */
2204 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
2205 hisi_sas_port_notify_formed(sas_phy);
2207 struct hisi_sas_port *port = phy->port;
2209 if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
2211 dev_info(dev, "ignore flutter phy%d down\n", phy_no);
2214 /* Phy down and not ready */
2215 sas_notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
2216 sas_phy_disconnected(sas_phy);
2219 if (phy->phy_type & PORT_TYPE_SAS) {
2220 int port_id = port->id;
2222 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2224 port->port_attached = 0;
2225 } else if (phy->phy_type & PORT_TYPE_SATA)
2226 port->port_attached = 0;
2228 hisi_sas_phy_disconnected(phy);
2231 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2233 void hisi_sas_sync_irqs(struct hisi_hba *hisi_hba)
2237 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2238 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2240 synchronize_irq(cq->irq_no);
2243 EXPORT_SYMBOL_GPL(hisi_sas_sync_irqs);
2245 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type)
2247 struct hisi_hba *hisi_hba = shost_priv(shost);
2249 if (reset_type != SCSI_ADAPTER_RESET)
2252 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2256 EXPORT_SYMBOL_GPL(hisi_sas_host_reset);
2258 struct scsi_transport_template *hisi_sas_stt;
2259 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2261 static struct sas_domain_function_template hisi_sas_transport_ops = {
2262 .lldd_dev_found = hisi_sas_dev_found,
2263 .lldd_dev_gone = hisi_sas_dev_gone,
2264 .lldd_execute_task = hisi_sas_queue_command,
2265 .lldd_control_phy = hisi_sas_control_phy,
2266 .lldd_abort_task = hisi_sas_abort_task,
2267 .lldd_abort_task_set = hisi_sas_abort_task_set,
2268 .lldd_clear_aca = hisi_sas_clear_aca,
2269 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
2270 .lldd_lu_reset = hisi_sas_lu_reset,
2271 .lldd_query_task = hisi_sas_query_task,
2272 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
2273 .lldd_port_formed = hisi_sas_port_formed,
2274 .lldd_write_gpio = hisi_sas_write_gpio,
2277 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2279 int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS;
2280 struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
2282 for (i = 0; i < hisi_hba->queue_count; i++) {
2283 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2284 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2285 struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
2287 s = sizeof(struct hisi_sas_cmd_hdr);
2288 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2289 memset(&cmd_hdr[j], 0, s);
2293 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2294 memset(hisi_hba->complete_hdr[i], 0, s);
2298 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2299 memset(hisi_hba->initial_fis, 0, s);
2301 s = max_command_entries * sizeof(struct hisi_sas_iost);
2302 memset(hisi_hba->iost, 0, s);
2304 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2305 memset(hisi_hba->breakpoint, 0, s);
2307 s = sizeof(struct hisi_sas_sata_breakpoint);
2308 for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
2309 memset(&sata_breakpoint[j], 0, s);
2311 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2313 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2315 struct device *dev = hisi_hba->dev;
2316 int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS;
2317 int max_command_entries_ru, sz_slot_buf_ru;
2318 int blk_cnt, slots_per_blk;
2320 sema_init(&hisi_hba->sem, 1);
2321 spin_lock_init(&hisi_hba->lock);
2322 for (i = 0; i < hisi_hba->n_phy; i++) {
2323 hisi_sas_phy_init(hisi_hba, i);
2324 hisi_hba->port[i].port_attached = 0;
2325 hisi_hba->port[i].id = -1;
2328 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2329 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2330 hisi_hba->devices[i].device_id = i;
2331 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
2334 for (i = 0; i < hisi_hba->queue_count; i++) {
2335 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2336 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2338 /* Completion queue structure */
2340 cq->hisi_hba = hisi_hba;
2342 /* Delivery queue structure */
2343 spin_lock_init(&dq->lock);
2344 INIT_LIST_HEAD(&dq->list);
2346 dq->hisi_hba = hisi_hba;
2348 /* Delivery queue */
2349 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2350 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2351 &hisi_hba->cmd_hdr_dma[i],
2353 if (!hisi_hba->cmd_hdr[i])
2356 /* Completion queue */
2357 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2358 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2359 &hisi_hba->complete_hdr_dma[i],
2361 if (!hisi_hba->complete_hdr[i])
2365 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2366 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2368 if (!hisi_hba->itct)
2371 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2372 sizeof(struct hisi_sas_slot),
2374 if (!hisi_hba->slot_info)
2377 /* roundup to avoid overly large block size */
2378 max_command_entries_ru = roundup(max_command_entries, 64);
2379 if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
2380 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
2382 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2383 sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2384 s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
2385 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2386 slots_per_blk = s / sz_slot_buf_ru;
2388 for (i = 0; i < blk_cnt; i++) {
2389 int slot_index = i * slots_per_blk;
2393 buf = dmam_alloc_coherent(dev, s, &buf_dma,
2398 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2399 struct hisi_sas_slot *slot;
2401 slot = &hisi_hba->slot_info[slot_index];
2403 slot->buf_dma = buf_dma;
2404 slot->idx = slot_index;
2406 buf += sz_slot_buf_ru;
2407 buf_dma += sz_slot_buf_ru;
2411 s = max_command_entries * sizeof(struct hisi_sas_iost);
2412 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2414 if (!hisi_hba->iost)
2417 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2418 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2419 &hisi_hba->breakpoint_dma,
2421 if (!hisi_hba->breakpoint)
2424 hisi_hba->slot_index_count = max_command_entries;
2425 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2426 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2427 if (!hisi_hba->slot_index_tags)
2430 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2431 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2432 &hisi_hba->initial_fis_dma,
2434 if (!hisi_hba->initial_fis)
2437 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2438 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2439 &hisi_hba->sata_breakpoint_dma,
2441 if (!hisi_hba->sata_breakpoint)
2444 hisi_sas_slot_index_init(hisi_hba);
2445 hisi_hba->last_slot_index = HISI_SAS_UNRESERVED_IPTT;
2447 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2448 if (!hisi_hba->wq) {
2449 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2457 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2459 void hisi_sas_free(struct hisi_hba *hisi_hba)
2463 for (i = 0; i < hisi_hba->n_phy; i++) {
2464 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
2466 del_timer_sync(&phy->timer);
2470 destroy_workqueue(hisi_hba->wq);
2472 EXPORT_SYMBOL_GPL(hisi_sas_free);
2474 void hisi_sas_rst_work_handler(struct work_struct *work)
2476 struct hisi_hba *hisi_hba =
2477 container_of(work, struct hisi_hba, rst_work);
2479 hisi_sas_controller_reset(hisi_hba);
2481 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2483 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2485 struct hisi_sas_rst *rst =
2486 container_of(work, struct hisi_sas_rst, work);
2488 if (!hisi_sas_controller_reset(rst->hisi_hba))
2490 complete(rst->completion);
2492 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2494 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2496 struct device *dev = hisi_hba->dev;
2497 struct platform_device *pdev = hisi_hba->platform_dev;
2498 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2501 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2503 dev_err(dev, "could not get property sas-addr\n");
2509 * These properties are only required for platform device-based
2510 * controller with DT firmware.
2512 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2513 "hisilicon,sas-syscon");
2514 if (IS_ERR(hisi_hba->ctrl)) {
2515 dev_err(dev, "could not get syscon\n");
2519 if (device_property_read_u32(dev, "ctrl-reset-reg",
2520 &hisi_hba->ctrl_reset_reg)) {
2521 dev_err(dev, "could not get property ctrl-reset-reg\n");
2525 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2526 &hisi_hba->ctrl_reset_sts_reg)) {
2527 dev_err(dev, "could not get property ctrl-reset-sts-reg\n");
2531 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2532 &hisi_hba->ctrl_clock_ena_reg)) {
2533 dev_err(dev, "could not get property ctrl-clock-ena-reg\n");
2538 refclk = devm_clk_get(dev, NULL);
2540 dev_dbg(dev, "no ref clk property\n");
2542 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2544 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2545 dev_err(dev, "could not get property phy-count\n");
2549 if (device_property_read_u32(dev, "queue-count",
2550 &hisi_hba->queue_count)) {
2551 dev_err(dev, "could not get property queue-count\n");
2557 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2559 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2560 const struct hisi_sas_hw *hw)
2562 struct resource *res;
2563 struct Scsi_Host *shost;
2564 struct hisi_hba *hisi_hba;
2565 struct device *dev = &pdev->dev;
2568 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2570 dev_err(dev, "scsi host alloc failed\n");
2573 hisi_hba = shost_priv(shost);
2575 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2577 hisi_hba->dev = dev;
2578 hisi_hba->platform_dev = pdev;
2579 hisi_hba->shost = shost;
2580 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2582 timer_setup(&hisi_hba->timer, NULL, 0);
2584 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2587 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2589 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2592 dev_err(dev, "No usable DMA addressing method\n");
2596 hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0);
2597 if (IS_ERR(hisi_hba->regs))
2600 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2602 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2603 if (IS_ERR(hisi_hba->sgpio_regs))
2607 if (hisi_sas_alloc(hisi_hba)) {
2608 hisi_sas_free(hisi_hba);
2614 scsi_host_put(shost);
2615 dev_err(dev, "shost alloc failed\n");
2619 int hisi_sas_probe(struct platform_device *pdev,
2620 const struct hisi_sas_hw *hw)
2622 struct Scsi_Host *shost;
2623 struct hisi_hba *hisi_hba;
2624 struct device *dev = &pdev->dev;
2625 struct asd_sas_phy **arr_phy;
2626 struct asd_sas_port **arr_port;
2627 struct sas_ha_struct *sha;
2628 int rc, phy_nr, port_nr, i;
2630 shost = hisi_sas_shost_alloc(pdev, hw);
2634 sha = SHOST_TO_SAS_HA(shost);
2635 hisi_hba = shost_priv(shost);
2636 platform_set_drvdata(pdev, sha);
2638 phy_nr = port_nr = hisi_hba->n_phy;
2640 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2641 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2642 if (!arr_phy || !arr_port) {
2647 sha->sas_phy = arr_phy;
2648 sha->sas_port = arr_port;
2649 sha->lldd_ha = hisi_hba;
2651 shost->transportt = hisi_sas_stt;
2652 shost->max_id = HISI_SAS_MAX_DEVICES;
2653 shost->max_lun = ~0;
2654 shost->max_channel = 1;
2655 shost->max_cmd_len = 16;
2656 if (hisi_hba->hw->slot_index_alloc) {
2657 shost->can_queue = HISI_SAS_MAX_COMMANDS;
2658 shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;
2660 shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
2661 shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
2664 sha->sas_ha_name = DRV_NAME;
2665 sha->dev = hisi_hba->dev;
2666 sha->lldd_module = THIS_MODULE;
2667 sha->sas_addr = &hisi_hba->sas_addr[0];
2668 sha->num_phys = hisi_hba->n_phy;
2669 sha->core.shost = hisi_hba->shost;
2671 for (i = 0; i < hisi_hba->n_phy; i++) {
2672 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2673 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2676 rc = scsi_add_host(shost, &pdev->dev);
2680 rc = sas_register_ha(sha);
2682 goto err_out_register_ha;
2684 rc = hisi_hba->hw->hw_init(hisi_hba);
2686 goto err_out_register_ha;
2688 scsi_scan_host(shost);
2692 err_out_register_ha:
2693 scsi_remove_host(shost);
2695 hisi_sas_debugfs_exit(hisi_hba);
2696 hisi_sas_free(hisi_hba);
2697 scsi_host_put(shost);
2700 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2702 struct dentry *hisi_sas_debugfs_dir;
2704 static void hisi_sas_debugfs_snapshot_cq_reg(struct hisi_hba *hisi_hba)
2706 int queue_entry_size = hisi_hba->hw->complete_hdr_size;
2707 int dump_index = hisi_hba->debugfs_dump_index;
2710 for (i = 0; i < hisi_hba->queue_count; i++)
2711 memcpy(hisi_hba->debugfs_cq[dump_index][i].complete_hdr,
2712 hisi_hba->complete_hdr[i],
2713 HISI_SAS_QUEUE_SLOTS * queue_entry_size);
2716 static void hisi_sas_debugfs_snapshot_dq_reg(struct hisi_hba *hisi_hba)
2718 int queue_entry_size = sizeof(struct hisi_sas_cmd_hdr);
2719 int dump_index = hisi_hba->debugfs_dump_index;
2722 for (i = 0; i < hisi_hba->queue_count; i++) {
2723 struct hisi_sas_cmd_hdr *debugfs_cmd_hdr, *cmd_hdr;
2726 debugfs_cmd_hdr = hisi_hba->debugfs_dq[dump_index][i].hdr;
2727 cmd_hdr = hisi_hba->cmd_hdr[i];
2729 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2730 memcpy(&debugfs_cmd_hdr[j], &cmd_hdr[j],
2735 static void hisi_sas_debugfs_snapshot_port_reg(struct hisi_hba *hisi_hba)
2737 int dump_index = hisi_hba->debugfs_dump_index;
2738 const struct hisi_sas_debugfs_reg *port =
2739 hisi_hba->hw->debugfs_reg_port;
2744 for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) {
2745 databuf = hisi_hba->debugfs_port_reg[dump_index][phy_cnt].data;
2746 for (i = 0; i < port->count; i++, databuf++) {
2747 offset = port->base_off + 4 * i;
2748 *databuf = port->read_port_reg(hisi_hba, phy_cnt,
2754 static void hisi_sas_debugfs_snapshot_global_reg(struct hisi_hba *hisi_hba)
2756 int dump_index = hisi_hba->debugfs_dump_index;
2757 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL].data;
2758 const struct hisi_sas_hw *hw = hisi_hba->hw;
2759 const struct hisi_sas_debugfs_reg *global =
2760 hw->debugfs_reg_array[DEBUGFS_GLOBAL];
2763 for (i = 0; i < global->count; i++, databuf++)
2764 *databuf = global->read_global_reg(hisi_hba, 4 * i);
2767 static void hisi_sas_debugfs_snapshot_axi_reg(struct hisi_hba *hisi_hba)
2769 int dump_index = hisi_hba->debugfs_dump_index;
2770 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI].data;
2771 const struct hisi_sas_hw *hw = hisi_hba->hw;
2772 const struct hisi_sas_debugfs_reg *axi =
2773 hw->debugfs_reg_array[DEBUGFS_AXI];
2776 for (i = 0; i < axi->count; i++, databuf++)
2777 *databuf = axi->read_global_reg(hisi_hba,
2778 4 * i + axi->base_off);
2781 static void hisi_sas_debugfs_snapshot_ras_reg(struct hisi_hba *hisi_hba)
2783 int dump_index = hisi_hba->debugfs_dump_index;
2784 u32 *databuf = hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS].data;
2785 const struct hisi_sas_hw *hw = hisi_hba->hw;
2786 const struct hisi_sas_debugfs_reg *ras =
2787 hw->debugfs_reg_array[DEBUGFS_RAS];
2790 for (i = 0; i < ras->count; i++, databuf++)
2791 *databuf = ras->read_global_reg(hisi_hba,
2792 4 * i + ras->base_off);
2795 static void hisi_sas_debugfs_snapshot_itct_reg(struct hisi_hba *hisi_hba)
2797 int dump_index = hisi_hba->debugfs_dump_index;
2798 void *cachebuf = hisi_hba->debugfs_itct_cache[dump_index].cache;
2799 void *databuf = hisi_hba->debugfs_itct[dump_index].itct;
2800 struct hisi_sas_itct *itct;
2803 hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_ITCT_CACHE,
2806 itct = hisi_hba->itct;
2808 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
2809 memcpy(databuf, itct, sizeof(struct hisi_sas_itct));
2810 databuf += sizeof(struct hisi_sas_itct);
2814 static void hisi_sas_debugfs_snapshot_iost_reg(struct hisi_hba *hisi_hba)
2816 int dump_index = hisi_hba->debugfs_dump_index;
2817 int max_command_entries = HISI_SAS_MAX_COMMANDS;
2818 void *cachebuf = hisi_hba->debugfs_iost_cache[dump_index].cache;
2819 void *databuf = hisi_hba->debugfs_iost[dump_index].iost;
2820 struct hisi_sas_iost *iost;
2823 hisi_hba->hw->read_iost_itct_cache(hisi_hba, HISI_SAS_IOST_CACHE,
2826 iost = hisi_hba->iost;
2828 for (i = 0; i < max_command_entries; i++, iost++) {
2829 memcpy(databuf, iost, sizeof(struct hisi_sas_iost));
2830 databuf += sizeof(struct hisi_sas_iost);
2835 hisi_sas_debugfs_to_reg_name(int off, int base_off,
2836 const struct hisi_sas_debugfs_reg_lu *lu)
2838 for (; lu->name; lu++) {
2839 if (off == lu->off - base_off)
2846 static void hisi_sas_debugfs_print_reg(u32 *regs_val, const void *ptr,
2849 const struct hisi_sas_debugfs_reg *reg = ptr;
2852 for (i = 0; i < reg->count; i++) {
2856 name = hisi_sas_debugfs_to_reg_name(off, reg->base_off,
2860 seq_printf(s, "0x%08x 0x%08x %s\n", off,
2863 seq_printf(s, "0x%08x 0x%08x\n", off,
2868 static int hisi_sas_debugfs_global_show(struct seq_file *s, void *p)
2870 struct hisi_sas_debugfs_regs *global = s->private;
2871 struct hisi_hba *hisi_hba = global->hisi_hba;
2872 const struct hisi_sas_hw *hw = hisi_hba->hw;
2873 const void *reg_global = hw->debugfs_reg_array[DEBUGFS_GLOBAL];
2875 hisi_sas_debugfs_print_reg(global->data,
2881 static int hisi_sas_debugfs_global_open(struct inode *inode, struct file *filp)
2883 return single_open(filp, hisi_sas_debugfs_global_show,
2887 static const struct file_operations hisi_sas_debugfs_global_fops = {
2888 .open = hisi_sas_debugfs_global_open,
2890 .llseek = seq_lseek,
2891 .release = single_release,
2892 .owner = THIS_MODULE,
2895 static int hisi_sas_debugfs_axi_show(struct seq_file *s, void *p)
2897 struct hisi_sas_debugfs_regs *axi = s->private;
2898 struct hisi_hba *hisi_hba = axi->hisi_hba;
2899 const struct hisi_sas_hw *hw = hisi_hba->hw;
2900 const void *reg_axi = hw->debugfs_reg_array[DEBUGFS_AXI];
2902 hisi_sas_debugfs_print_reg(axi->data,
2908 static int hisi_sas_debugfs_axi_open(struct inode *inode, struct file *filp)
2910 return single_open(filp, hisi_sas_debugfs_axi_show,
2914 static const struct file_operations hisi_sas_debugfs_axi_fops = {
2915 .open = hisi_sas_debugfs_axi_open,
2917 .llseek = seq_lseek,
2918 .release = single_release,
2919 .owner = THIS_MODULE,
2922 static int hisi_sas_debugfs_ras_show(struct seq_file *s, void *p)
2924 struct hisi_sas_debugfs_regs *ras = s->private;
2925 struct hisi_hba *hisi_hba = ras->hisi_hba;
2926 const struct hisi_sas_hw *hw = hisi_hba->hw;
2927 const void *reg_ras = hw->debugfs_reg_array[DEBUGFS_RAS];
2929 hisi_sas_debugfs_print_reg(ras->data,
2935 static int hisi_sas_debugfs_ras_open(struct inode *inode, struct file *filp)
2937 return single_open(filp, hisi_sas_debugfs_ras_show,
2941 static const struct file_operations hisi_sas_debugfs_ras_fops = {
2942 .open = hisi_sas_debugfs_ras_open,
2944 .llseek = seq_lseek,
2945 .release = single_release,
2946 .owner = THIS_MODULE,
2949 static int hisi_sas_debugfs_port_show(struct seq_file *s, void *p)
2951 struct hisi_sas_debugfs_port *port = s->private;
2952 struct hisi_sas_phy *phy = port->phy;
2953 struct hisi_hba *hisi_hba = phy->hisi_hba;
2954 const struct hisi_sas_hw *hw = hisi_hba->hw;
2955 const struct hisi_sas_debugfs_reg *reg_port = hw->debugfs_reg_port;
2957 hisi_sas_debugfs_print_reg(port->data, reg_port, s);
2962 static int hisi_sas_debugfs_port_open(struct inode *inode, struct file *filp)
2964 return single_open(filp, hisi_sas_debugfs_port_show, inode->i_private);
2967 static const struct file_operations hisi_sas_debugfs_port_fops = {
2968 .open = hisi_sas_debugfs_port_open,
2970 .llseek = seq_lseek,
2971 .release = single_release,
2972 .owner = THIS_MODULE,
2975 static void hisi_sas_show_row_64(struct seq_file *s, int index,
2976 int sz, __le64 *ptr)
2980 /* completion header size not fixed per HW version */
2981 seq_printf(s, "index %04d:\n\t", index);
2982 for (i = 1; i <= sz / 8; i++, ptr++) {
2983 seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr));
2985 seq_puts(s, "\n\t");
2991 static void hisi_sas_show_row_32(struct seq_file *s, int index,
2992 int sz, __le32 *ptr)
2996 /* completion header size not fixed per HW version */
2997 seq_printf(s, "index %04d:\n\t", index);
2998 for (i = 1; i <= sz / 4; i++, ptr++) {
2999 seq_printf(s, " 0x%08x", le32_to_cpu(*ptr));
3001 seq_puts(s, "\n\t");
3006 static void hisi_sas_cq_show_slot(struct seq_file *s, int slot,
3007 struct hisi_sas_debugfs_cq *debugfs_cq)
3009 struct hisi_sas_cq *cq = debugfs_cq->cq;
3010 struct hisi_hba *hisi_hba = cq->hisi_hba;
3011 __le32 *complete_hdr = debugfs_cq->complete_hdr +
3012 (hisi_hba->hw->complete_hdr_size * slot);
3014 hisi_sas_show_row_32(s, slot,
3015 hisi_hba->hw->complete_hdr_size,
3019 static int hisi_sas_debugfs_cq_show(struct seq_file *s, void *p)
3021 struct hisi_sas_debugfs_cq *debugfs_cq = s->private;
3024 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
3025 hisi_sas_cq_show_slot(s, slot, debugfs_cq);
3030 static int hisi_sas_debugfs_cq_open(struct inode *inode, struct file *filp)
3032 return single_open(filp, hisi_sas_debugfs_cq_show, inode->i_private);
3035 static const struct file_operations hisi_sas_debugfs_cq_fops = {
3036 .open = hisi_sas_debugfs_cq_open,
3038 .llseek = seq_lseek,
3039 .release = single_release,
3040 .owner = THIS_MODULE,
3043 static void hisi_sas_dq_show_slot(struct seq_file *s, int slot, void *dq_ptr)
3045 struct hisi_sas_debugfs_dq *debugfs_dq = dq_ptr;
3046 void *cmd_queue = debugfs_dq->hdr;
3047 __le32 *cmd_hdr = cmd_queue +
3048 sizeof(struct hisi_sas_cmd_hdr) * slot;
3050 hisi_sas_show_row_32(s, slot, sizeof(struct hisi_sas_cmd_hdr), cmd_hdr);
3053 static int hisi_sas_debugfs_dq_show(struct seq_file *s, void *p)
3057 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
3058 hisi_sas_dq_show_slot(s, slot, s->private);
3063 static int hisi_sas_debugfs_dq_open(struct inode *inode, struct file *filp)
3065 return single_open(filp, hisi_sas_debugfs_dq_show, inode->i_private);
3068 static const struct file_operations hisi_sas_debugfs_dq_fops = {
3069 .open = hisi_sas_debugfs_dq_open,
3071 .llseek = seq_lseek,
3072 .release = single_release,
3073 .owner = THIS_MODULE,
3076 static int hisi_sas_debugfs_iost_show(struct seq_file *s, void *p)
3078 struct hisi_sas_debugfs_iost *debugfs_iost = s->private;
3079 struct hisi_sas_iost *iost = debugfs_iost->iost;
3080 int i, max_command_entries = HISI_SAS_MAX_COMMANDS;
3082 for (i = 0; i < max_command_entries; i++, iost++) {
3083 __le64 *data = &iost->qw0;
3085 hisi_sas_show_row_64(s, i, sizeof(*iost), data);
3091 static int hisi_sas_debugfs_iost_open(struct inode *inode, struct file *filp)
3093 return single_open(filp, hisi_sas_debugfs_iost_show, inode->i_private);
3096 static const struct file_operations hisi_sas_debugfs_iost_fops = {
3097 .open = hisi_sas_debugfs_iost_open,
3099 .llseek = seq_lseek,
3100 .release = single_release,
3101 .owner = THIS_MODULE,
3104 static int hisi_sas_debugfs_iost_cache_show(struct seq_file *s, void *p)
3106 struct hisi_sas_debugfs_iost_cache *debugfs_iost_cache = s->private;
3107 struct hisi_sas_iost_itct_cache *iost_cache = debugfs_iost_cache->cache;
3108 u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3112 for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, iost_cache++) {
3114 * Data struct of IOST cache:
3115 * Data[1]: BIT0~15: Table index
3117 * Data[2]~[9]: IOST table
3119 tab_idx = (iost_cache->data[1] & 0xffff);
3120 iost = (__le64 *)iost_cache;
3122 hisi_sas_show_row_64(s, tab_idx, cache_size, iost);
3128 static int hisi_sas_debugfs_iost_cache_open(struct inode *inode,
3131 return single_open(filp, hisi_sas_debugfs_iost_cache_show,
3135 static const struct file_operations hisi_sas_debugfs_iost_cache_fops = {
3136 .open = hisi_sas_debugfs_iost_cache_open,
3138 .llseek = seq_lseek,
3139 .release = single_release,
3140 .owner = THIS_MODULE,
3143 static int hisi_sas_debugfs_itct_show(struct seq_file *s, void *p)
3146 struct hisi_sas_debugfs_itct *debugfs_itct = s->private;
3147 struct hisi_sas_itct *itct = debugfs_itct->itct;
3149 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
3150 __le64 *data = &itct->qw0;
3152 hisi_sas_show_row_64(s, i, sizeof(*itct), data);
3158 static int hisi_sas_debugfs_itct_open(struct inode *inode, struct file *filp)
3160 return single_open(filp, hisi_sas_debugfs_itct_show, inode->i_private);
3163 static const struct file_operations hisi_sas_debugfs_itct_fops = {
3164 .open = hisi_sas_debugfs_itct_open,
3166 .llseek = seq_lseek,
3167 .release = single_release,
3168 .owner = THIS_MODULE,
3171 static int hisi_sas_debugfs_itct_cache_show(struct seq_file *s, void *p)
3173 struct hisi_sas_debugfs_itct_cache *debugfs_itct_cache = s->private;
3174 struct hisi_sas_iost_itct_cache *itct_cache = debugfs_itct_cache->cache;
3175 u32 cache_size = HISI_SAS_IOST_ITCT_CACHE_DW_SZ * 4;
3179 for (i = 0; i < HISI_SAS_IOST_ITCT_CACHE_NUM; i++, itct_cache++) {
3181 * Data struct of ITCT cache:
3182 * Data[1]: BIT0~15: Table index
3184 * Data[2]~[9]: ITCT table
3186 tab_idx = itct_cache->data[1] & 0xffff;
3187 itct = (__le64 *)itct_cache;
3189 hisi_sas_show_row_64(s, tab_idx, cache_size, itct);
3195 static int hisi_sas_debugfs_itct_cache_open(struct inode *inode,
3198 return single_open(filp, hisi_sas_debugfs_itct_cache_show,
3202 static const struct file_operations hisi_sas_debugfs_itct_cache_fops = {
3203 .open = hisi_sas_debugfs_itct_cache_open,
3205 .llseek = seq_lseek,
3206 .release = single_release,
3207 .owner = THIS_MODULE,
3210 static void hisi_sas_debugfs_create_files(struct hisi_hba *hisi_hba)
3212 u64 *debugfs_timestamp;
3213 int dump_index = hisi_hba->debugfs_dump_index;
3214 struct dentry *dump_dentry;
3215 struct dentry *dentry;
3221 snprintf(name, 256, "%d", dump_index);
3223 dump_dentry = debugfs_create_dir(name, hisi_hba->debugfs_dump_dentry);
3225 debugfs_timestamp = &hisi_hba->debugfs_timestamp[dump_index];
3227 debugfs_create_u64("timestamp", 0400, dump_dentry,
3230 debugfs_create_file("global", 0400, dump_dentry,
3231 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_GLOBAL],
3232 &hisi_sas_debugfs_global_fops);
3234 /* Create port dir and files */
3235 dentry = debugfs_create_dir("port", dump_dentry);
3236 for (p = 0; p < hisi_hba->n_phy; p++) {
3237 snprintf(name, 256, "%d", p);
3239 debugfs_create_file(name, 0400, dentry,
3240 &hisi_hba->debugfs_port_reg[dump_index][p],
3241 &hisi_sas_debugfs_port_fops);
3244 /* Create CQ dir and files */
3245 dentry = debugfs_create_dir("cq", dump_dentry);
3246 for (c = 0; c < hisi_hba->queue_count; c++) {
3247 snprintf(name, 256, "%d", c);
3249 debugfs_create_file(name, 0400, dentry,
3250 &hisi_hba->debugfs_cq[dump_index][c],
3251 &hisi_sas_debugfs_cq_fops);
3254 /* Create DQ dir and files */
3255 dentry = debugfs_create_dir("dq", dump_dentry);
3256 for (d = 0; d < hisi_hba->queue_count; d++) {
3257 snprintf(name, 256, "%d", d);
3259 debugfs_create_file(name, 0400, dentry,
3260 &hisi_hba->debugfs_dq[dump_index][d],
3261 &hisi_sas_debugfs_dq_fops);
3264 debugfs_create_file("iost", 0400, dump_dentry,
3265 &hisi_hba->debugfs_iost[dump_index],
3266 &hisi_sas_debugfs_iost_fops);
3268 debugfs_create_file("iost_cache", 0400, dump_dentry,
3269 &hisi_hba->debugfs_iost_cache[dump_index],
3270 &hisi_sas_debugfs_iost_cache_fops);
3272 debugfs_create_file("itct", 0400, dump_dentry,
3273 &hisi_hba->debugfs_itct[dump_index],
3274 &hisi_sas_debugfs_itct_fops);
3276 debugfs_create_file("itct_cache", 0400, dump_dentry,
3277 &hisi_hba->debugfs_itct_cache[dump_index],
3278 &hisi_sas_debugfs_itct_cache_fops);
3280 debugfs_create_file("axi", 0400, dump_dentry,
3281 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_AXI],
3282 &hisi_sas_debugfs_axi_fops);
3284 debugfs_create_file("ras", 0400, dump_dentry,
3285 &hisi_hba->debugfs_regs[dump_index][DEBUGFS_RAS],
3286 &hisi_sas_debugfs_ras_fops);
3291 static void hisi_sas_debugfs_snapshot_regs(struct hisi_hba *hisi_hba)
3293 hisi_hba->hw->snapshot_prepare(hisi_hba);
3295 hisi_sas_debugfs_snapshot_global_reg(hisi_hba);
3296 hisi_sas_debugfs_snapshot_port_reg(hisi_hba);
3297 hisi_sas_debugfs_snapshot_axi_reg(hisi_hba);
3298 hisi_sas_debugfs_snapshot_ras_reg(hisi_hba);
3299 hisi_sas_debugfs_snapshot_cq_reg(hisi_hba);
3300 hisi_sas_debugfs_snapshot_dq_reg(hisi_hba);
3301 hisi_sas_debugfs_snapshot_itct_reg(hisi_hba);
3302 hisi_sas_debugfs_snapshot_iost_reg(hisi_hba);
3304 hisi_sas_debugfs_create_files(hisi_hba);
3306 hisi_hba->hw->snapshot_restore(hisi_hba);
3309 static ssize_t hisi_sas_debugfs_trigger_dump_write(struct file *file,
3310 const char __user *user_buf,
3311 size_t count, loff_t *ppos)
3313 struct hisi_hba *hisi_hba = file->f_inode->i_private;
3316 if (hisi_hba->debugfs_dump_index >= hisi_sas_debugfs_dump_count)
3322 if (copy_from_user(buf, user_buf, count))
3328 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
3333 static const struct file_operations hisi_sas_debugfs_trigger_dump_fops = {
3334 .write = &hisi_sas_debugfs_trigger_dump_write,
3335 .owner = THIS_MODULE,
3339 HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL = 0,
3340 HISI_SAS_BIST_LOOPBACK_MODE_SERDES,
3341 HISI_SAS_BIST_LOOPBACK_MODE_REMOTE,
3344 static const struct {
3347 } hisi_sas_debugfs_loop_linkrate[] = {
3348 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
3349 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
3350 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
3351 { SAS_LINK_RATE_12_0_GBPS, "12.0 Gbit" },
3354 static int hisi_sas_debugfs_bist_linkrate_show(struct seq_file *s, void *p)
3356 struct hisi_hba *hisi_hba = s->private;
3359 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
3360 int match = (hisi_hba->debugfs_bist_linkrate ==
3361 hisi_sas_debugfs_loop_linkrate[i].value);
3363 seq_printf(s, "%s%s%s ", match ? "[" : "",
3364 hisi_sas_debugfs_loop_linkrate[i].name,
3372 static ssize_t hisi_sas_debugfs_bist_linkrate_write(struct file *filp,
3373 const char __user *buf,
3374 size_t count, loff_t *ppos)
3376 struct seq_file *m = filp->private_data;
3377 struct hisi_hba *hisi_hba = m->private;
3378 char kbuf[16] = {}, *pkbuf;
3382 if (hisi_hba->debugfs_bist_enable)
3385 if (count >= sizeof(kbuf))
3388 if (copy_from_user(kbuf, buf, count))
3391 pkbuf = strstrip(kbuf);
3393 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_linkrate); i++) {
3394 if (!strncmp(hisi_sas_debugfs_loop_linkrate[i].name,
3396 hisi_hba->debugfs_bist_linkrate =
3397 hisi_sas_debugfs_loop_linkrate[i].value;
3409 static int hisi_sas_debugfs_bist_linkrate_open(struct inode *inode,
3412 return single_open(filp, hisi_sas_debugfs_bist_linkrate_show,
3416 static const struct file_operations hisi_sas_debugfs_bist_linkrate_ops = {
3417 .open = hisi_sas_debugfs_bist_linkrate_open,
3419 .write = hisi_sas_debugfs_bist_linkrate_write,
3420 .llseek = seq_lseek,
3421 .release = single_release,
3422 .owner = THIS_MODULE,
3425 static const struct {
3428 } hisi_sas_debugfs_loop_code_mode[] = {
3429 { HISI_SAS_BIST_CODE_MODE_PRBS7, "PRBS7" },
3430 { HISI_SAS_BIST_CODE_MODE_PRBS23, "PRBS23" },
3431 { HISI_SAS_BIST_CODE_MODE_PRBS31, "PRBS31" },
3432 { HISI_SAS_BIST_CODE_MODE_JTPAT, "JTPAT" },
3433 { HISI_SAS_BIST_CODE_MODE_CJTPAT, "CJTPAT" },
3434 { HISI_SAS_BIST_CODE_MODE_SCRAMBED_0, "SCRAMBED_0" },
3435 { HISI_SAS_BIST_CODE_MODE_TRAIN, "TRAIN" },
3436 { HISI_SAS_BIST_CODE_MODE_TRAIN_DONE, "TRAIN_DONE" },
3437 { HISI_SAS_BIST_CODE_MODE_HFTP, "HFTP" },
3438 { HISI_SAS_BIST_CODE_MODE_MFTP, "MFTP" },
3439 { HISI_SAS_BIST_CODE_MODE_LFTP, "LFTP" },
3440 { HISI_SAS_BIST_CODE_MODE_FIXED_DATA, "FIXED_DATA" },
3443 static int hisi_sas_debugfs_bist_code_mode_show(struct seq_file *s, void *p)
3445 struct hisi_hba *hisi_hba = s->private;
3448 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
3449 int match = (hisi_hba->debugfs_bist_code_mode ==
3450 hisi_sas_debugfs_loop_code_mode[i].value);
3452 seq_printf(s, "%s%s%s ", match ? "[" : "",
3453 hisi_sas_debugfs_loop_code_mode[i].name,
3461 static ssize_t hisi_sas_debugfs_bist_code_mode_write(struct file *filp,
3462 const char __user *buf,
3466 struct seq_file *m = filp->private_data;
3467 struct hisi_hba *hisi_hba = m->private;
3468 char kbuf[16] = {}, *pkbuf;
3472 if (hisi_hba->debugfs_bist_enable)
3475 if (count >= sizeof(kbuf))
3478 if (copy_from_user(kbuf, buf, count))
3481 pkbuf = strstrip(kbuf);
3483 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_code_mode); i++) {
3484 if (!strncmp(hisi_sas_debugfs_loop_code_mode[i].name,
3486 hisi_hba->debugfs_bist_code_mode =
3487 hisi_sas_debugfs_loop_code_mode[i].value;
3499 static int hisi_sas_debugfs_bist_code_mode_open(struct inode *inode,
3502 return single_open(filp, hisi_sas_debugfs_bist_code_mode_show,
3506 static const struct file_operations hisi_sas_debugfs_bist_code_mode_ops = {
3507 .open = hisi_sas_debugfs_bist_code_mode_open,
3509 .write = hisi_sas_debugfs_bist_code_mode_write,
3510 .llseek = seq_lseek,
3511 .release = single_release,
3512 .owner = THIS_MODULE,
3515 static ssize_t hisi_sas_debugfs_bist_phy_write(struct file *filp,
3516 const char __user *buf,
3517 size_t count, loff_t *ppos)
3519 struct seq_file *m = filp->private_data;
3520 struct hisi_hba *hisi_hba = m->private;
3521 unsigned int phy_no;
3524 if (hisi_hba->debugfs_bist_enable)
3527 val = kstrtouint_from_user(buf, count, 0, &phy_no);
3531 if (phy_no >= hisi_hba->n_phy)
3534 hisi_hba->debugfs_bist_phy_no = phy_no;
3539 static int hisi_sas_debugfs_bist_phy_show(struct seq_file *s, void *p)
3541 struct hisi_hba *hisi_hba = s->private;
3543 seq_printf(s, "%d\n", hisi_hba->debugfs_bist_phy_no);
3548 static int hisi_sas_debugfs_bist_phy_open(struct inode *inode,
3551 return single_open(filp, hisi_sas_debugfs_bist_phy_show,
3555 static const struct file_operations hisi_sas_debugfs_bist_phy_ops = {
3556 .open = hisi_sas_debugfs_bist_phy_open,
3558 .write = hisi_sas_debugfs_bist_phy_write,
3559 .llseek = seq_lseek,
3560 .release = single_release,
3561 .owner = THIS_MODULE,
3564 static const struct {
3567 } hisi_sas_debugfs_loop_modes[] = {
3568 { HISI_SAS_BIST_LOOPBACK_MODE_DIGITAL, "digital" },
3569 { HISI_SAS_BIST_LOOPBACK_MODE_SERDES, "serdes" },
3570 { HISI_SAS_BIST_LOOPBACK_MODE_REMOTE, "remote" },
3573 static int hisi_sas_debugfs_bist_mode_show(struct seq_file *s, void *p)
3575 struct hisi_hba *hisi_hba = s->private;
3578 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
3579 int match = (hisi_hba->debugfs_bist_mode ==
3580 hisi_sas_debugfs_loop_modes[i].value);
3582 seq_printf(s, "%s%s%s ", match ? "[" : "",
3583 hisi_sas_debugfs_loop_modes[i].name,
3591 static ssize_t hisi_sas_debugfs_bist_mode_write(struct file *filp,
3592 const char __user *buf,
3593 size_t count, loff_t *ppos)
3595 struct seq_file *m = filp->private_data;
3596 struct hisi_hba *hisi_hba = m->private;
3597 char kbuf[16] = {}, *pkbuf;
3601 if (hisi_hba->debugfs_bist_enable)
3604 if (count >= sizeof(kbuf))
3607 if (copy_from_user(kbuf, buf, count))
3610 pkbuf = strstrip(kbuf);
3612 for (i = 0; i < ARRAY_SIZE(hisi_sas_debugfs_loop_modes); i++) {
3613 if (!strncmp(hisi_sas_debugfs_loop_modes[i].name, pkbuf, 16)) {
3614 hisi_hba->debugfs_bist_mode =
3615 hisi_sas_debugfs_loop_modes[i].value;
3627 static int hisi_sas_debugfs_bist_mode_open(struct inode *inode,
3630 return single_open(filp, hisi_sas_debugfs_bist_mode_show,
3634 static const struct file_operations hisi_sas_debugfs_bist_mode_ops = {
3635 .open = hisi_sas_debugfs_bist_mode_open,
3637 .write = hisi_sas_debugfs_bist_mode_write,
3638 .llseek = seq_lseek,
3639 .release = single_release,
3640 .owner = THIS_MODULE,
3643 static ssize_t hisi_sas_debugfs_bist_enable_write(struct file *filp,
3644 const char __user *buf,
3645 size_t count, loff_t *ppos)
3647 struct seq_file *m = filp->private_data;
3648 struct hisi_hba *hisi_hba = m->private;
3649 unsigned int enable;
3652 val = kstrtouint_from_user(buf, count, 0, &enable);
3659 if (enable == hisi_hba->debugfs_bist_enable)
3662 if (!hisi_hba->hw->set_bist)
3665 val = hisi_hba->hw->set_bist(hisi_hba, enable);
3669 hisi_hba->debugfs_bist_enable = enable;
3674 static int hisi_sas_debugfs_bist_enable_show(struct seq_file *s, void *p)
3676 struct hisi_hba *hisi_hba = s->private;
3678 seq_printf(s, "%d\n", hisi_hba->debugfs_bist_enable);
3683 static int hisi_sas_debugfs_bist_enable_open(struct inode *inode,
3686 return single_open(filp, hisi_sas_debugfs_bist_enable_show,
3690 static const struct file_operations hisi_sas_debugfs_bist_enable_ops = {
3691 .open = hisi_sas_debugfs_bist_enable_open,
3693 .write = hisi_sas_debugfs_bist_enable_write,
3694 .llseek = seq_lseek,
3695 .release = single_release,
3696 .owner = THIS_MODULE,
3699 static const struct {
3701 } hisi_sas_debugfs_ffe_name[FFE_CFG_MAX] = {
3705 { "SAS_12_0_GBPS" },
3707 { "SATA_1_5_GBPS" },
3708 { "SATA_3_0_GBPS" },
3709 { "SATA_6_0_GBPS" },
3712 static ssize_t hisi_sas_debugfs_write(struct file *filp,
3713 const char __user *buf,
3714 size_t count, loff_t *ppos)
3716 struct seq_file *m = filp->private_data;
3717 u32 *val = m->private;
3720 res = kstrtouint_from_user(buf, count, 0, val);
3727 static int hisi_sas_debugfs_show(struct seq_file *s, void *p)
3729 u32 *val = s->private;
3731 seq_printf(s, "0x%x\n", *val);
3736 static int hisi_sas_debugfs_open(struct inode *inode, struct file *filp)
3738 return single_open(filp, hisi_sas_debugfs_show,
3742 static const struct file_operations hisi_sas_debugfs_ops = {
3743 .open = hisi_sas_debugfs_open,
3745 .write = hisi_sas_debugfs_write,
3746 .llseek = seq_lseek,
3747 .release = single_release,
3748 .owner = THIS_MODULE,
3751 static ssize_t hisi_sas_debugfs_phy_down_cnt_write(struct file *filp,
3752 const char __user *buf,
3753 size_t count, loff_t *ppos)
3755 struct seq_file *s = filp->private_data;
3756 struct hisi_sas_phy *phy = s->private;
3757 unsigned int set_val;
3760 res = kstrtouint_from_user(buf, count, 0, &set_val);
3767 atomic_set(&phy->down_cnt, 0);
3772 static int hisi_sas_debugfs_phy_down_cnt_show(struct seq_file *s, void *p)
3774 struct hisi_sas_phy *phy = s->private;
3776 seq_printf(s, "%d\n", atomic_read(&phy->down_cnt));
3781 static int hisi_sas_debugfs_phy_down_cnt_open(struct inode *inode,
3784 return single_open(filp, hisi_sas_debugfs_phy_down_cnt_show,
3788 static const struct file_operations hisi_sas_debugfs_phy_down_cnt_ops = {
3789 .open = hisi_sas_debugfs_phy_down_cnt_open,
3791 .write = hisi_sas_debugfs_phy_down_cnt_write,
3792 .llseek = seq_lseek,
3793 .release = single_release,
3794 .owner = THIS_MODULE,
3797 void hisi_sas_debugfs_work_handler(struct work_struct *work)
3799 struct hisi_hba *hisi_hba =
3800 container_of(work, struct hisi_hba, debugfs_work);
3801 int debugfs_dump_index = hisi_hba->debugfs_dump_index;
3802 struct device *dev = hisi_hba->dev;
3803 u64 timestamp = local_clock();
3805 if (debugfs_dump_index >= hisi_sas_debugfs_dump_count) {
3806 dev_warn(dev, "dump count exceeded!\n");
3810 do_div(timestamp, NSEC_PER_MSEC);
3811 hisi_hba->debugfs_timestamp[debugfs_dump_index] = timestamp;
3813 hisi_sas_debugfs_snapshot_regs(hisi_hba);
3814 hisi_hba->debugfs_dump_index++;
3816 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_work_handler);
3818 static void hisi_sas_debugfs_release(struct hisi_hba *hisi_hba, int dump_index)
3820 struct device *dev = hisi_hba->dev;
3823 devm_kfree(dev, hisi_hba->debugfs_iost_cache[dump_index].cache);
3824 devm_kfree(dev, hisi_hba->debugfs_itct_cache[dump_index].cache);
3825 devm_kfree(dev, hisi_hba->debugfs_iost[dump_index].iost);
3826 devm_kfree(dev, hisi_hba->debugfs_itct[dump_index].itct);
3828 for (i = 0; i < hisi_hba->queue_count; i++)
3829 devm_kfree(dev, hisi_hba->debugfs_dq[dump_index][i].hdr);
3831 for (i = 0; i < hisi_hba->queue_count; i++)
3833 hisi_hba->debugfs_cq[dump_index][i].complete_hdr);
3835 for (i = 0; i < DEBUGFS_REGS_NUM; i++)
3836 devm_kfree(dev, hisi_hba->debugfs_regs[dump_index][i].data);
3838 for (i = 0; i < hisi_hba->n_phy; i++)
3839 devm_kfree(dev, hisi_hba->debugfs_port_reg[dump_index][i].data);
3842 static int hisi_sas_debugfs_alloc(struct hisi_hba *hisi_hba, int dump_index)
3844 const struct hisi_sas_hw *hw = hisi_hba->hw;
3845 struct device *dev = hisi_hba->dev;
3849 for (r = 0; r < DEBUGFS_REGS_NUM; r++) {
3850 struct hisi_sas_debugfs_regs *regs =
3851 &hisi_hba->debugfs_regs[dump_index][r];
3853 sz = hw->debugfs_reg_array[r]->count * 4;
3854 regs->data = devm_kmalloc(dev, sz, GFP_KERNEL);
3857 regs->hisi_hba = hisi_hba;
3860 sz = hw->debugfs_reg_port->count * 4;
3861 for (p = 0; p < hisi_hba->n_phy; p++) {
3862 struct hisi_sas_debugfs_port *port =
3863 &hisi_hba->debugfs_port_reg[dump_index][p];
3865 port->data = devm_kmalloc(dev, sz, GFP_KERNEL);
3868 port->phy = &hisi_hba->phy[p];
3871 sz = hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
3872 for (c = 0; c < hisi_hba->queue_count; c++) {
3873 struct hisi_sas_debugfs_cq *cq =
3874 &hisi_hba->debugfs_cq[dump_index][c];
3876 cq->complete_hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
3877 if (!cq->complete_hdr)
3879 cq->cq = &hisi_hba->cq[c];
3882 sz = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
3883 for (d = 0; d < hisi_hba->queue_count; d++) {
3884 struct hisi_sas_debugfs_dq *dq =
3885 &hisi_hba->debugfs_dq[dump_index][d];
3887 dq->hdr = devm_kmalloc(dev, sz, GFP_KERNEL);
3890 dq->dq = &hisi_hba->dq[d];
3893 sz = HISI_SAS_MAX_COMMANDS * sizeof(struct hisi_sas_iost);
3895 hisi_hba->debugfs_iost[dump_index].iost =
3896 devm_kmalloc(dev, sz, GFP_KERNEL);
3897 if (!hisi_hba->debugfs_iost[dump_index].iost)
3900 sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
3901 sizeof(struct hisi_sas_iost_itct_cache);
3903 hisi_hba->debugfs_iost_cache[dump_index].cache =
3904 devm_kmalloc(dev, sz, GFP_KERNEL);
3905 if (!hisi_hba->debugfs_iost_cache[dump_index].cache)
3908 sz = HISI_SAS_IOST_ITCT_CACHE_NUM *
3909 sizeof(struct hisi_sas_iost_itct_cache);
3911 hisi_hba->debugfs_itct_cache[dump_index].cache =
3912 devm_kmalloc(dev, sz, GFP_KERNEL);
3913 if (!hisi_hba->debugfs_itct_cache[dump_index].cache)
3916 /* New memory allocation must be locate before itct */
3917 sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
3919 hisi_hba->debugfs_itct[dump_index].itct =
3920 devm_kmalloc(dev, sz, GFP_KERNEL);
3921 if (!hisi_hba->debugfs_itct[dump_index].itct)
3926 for (i = 0; i < hisi_sas_debugfs_dump_count; i++)
3927 hisi_sas_debugfs_release(hisi_hba, i);
3931 static void hisi_sas_debugfs_phy_down_cnt_init(struct hisi_hba *hisi_hba)
3933 struct dentry *dir = debugfs_create_dir("phy_down_cnt",
3934 hisi_hba->debugfs_dir);
3938 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
3939 snprintf(name, 16, "%d", phy_no);
3940 debugfs_create_file(name, 0600, dir,
3941 &hisi_hba->phy[phy_no],
3942 &hisi_sas_debugfs_phy_down_cnt_ops);
3946 static void hisi_sas_debugfs_bist_init(struct hisi_hba *hisi_hba)
3948 struct dentry *ports_dentry;
3951 hisi_hba->debugfs_bist_dentry =
3952 debugfs_create_dir("bist", hisi_hba->debugfs_dir);
3953 debugfs_create_file("link_rate", 0600,
3954 hisi_hba->debugfs_bist_dentry, hisi_hba,
3955 &hisi_sas_debugfs_bist_linkrate_ops);
3957 debugfs_create_file("code_mode", 0600,
3958 hisi_hba->debugfs_bist_dentry, hisi_hba,
3959 &hisi_sas_debugfs_bist_code_mode_ops);
3961 debugfs_create_file("fixed_code", 0600,
3962 hisi_hba->debugfs_bist_dentry,
3963 &hisi_hba->debugfs_bist_fixed_code[0],
3964 &hisi_sas_debugfs_ops);
3966 debugfs_create_file("fixed_code_1", 0600,
3967 hisi_hba->debugfs_bist_dentry,
3968 &hisi_hba->debugfs_bist_fixed_code[1],
3969 &hisi_sas_debugfs_ops);
3971 debugfs_create_file("phy_id", 0600, hisi_hba->debugfs_bist_dentry,
3972 hisi_hba, &hisi_sas_debugfs_bist_phy_ops);
3974 debugfs_create_u32("cnt", 0600, hisi_hba->debugfs_bist_dentry,
3975 &hisi_hba->debugfs_bist_cnt);
3977 debugfs_create_file("loopback_mode", 0600,
3978 hisi_hba->debugfs_bist_dentry,
3979 hisi_hba, &hisi_sas_debugfs_bist_mode_ops);
3981 debugfs_create_file("enable", 0600, hisi_hba->debugfs_bist_dentry,
3982 hisi_hba, &hisi_sas_debugfs_bist_enable_ops);
3984 ports_dentry = debugfs_create_dir("port", hisi_hba->debugfs_bist_dentry);
3986 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
3987 struct dentry *port_dentry;
3988 struct dentry *ffe_dentry;
3992 snprintf(name, 256, "%d", phy_no);
3993 port_dentry = debugfs_create_dir(name, ports_dentry);
3994 ffe_dentry = debugfs_create_dir("ffe", port_dentry);
3995 for (i = 0; i < FFE_CFG_MAX; i++) {
3998 debugfs_create_file(hisi_sas_debugfs_ffe_name[i].name,
4000 &hisi_hba->debugfs_bist_ffe[phy_no][i],
4001 &hisi_sas_debugfs_ops);
4005 hisi_hba->debugfs_bist_linkrate = SAS_LINK_RATE_1_5_GBPS;
4008 void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba)
4010 struct device *dev = hisi_hba->dev;
4013 hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev),
4014 hisi_sas_debugfs_dir);
4015 debugfs_create_file("trigger_dump", 0200,
4016 hisi_hba->debugfs_dir,
4018 &hisi_sas_debugfs_trigger_dump_fops);
4020 /* create bist structures */
4021 hisi_sas_debugfs_bist_init(hisi_hba);
4023 hisi_hba->debugfs_dump_dentry =
4024 debugfs_create_dir("dump", hisi_hba->debugfs_dir);
4026 hisi_sas_debugfs_phy_down_cnt_init(hisi_hba);
4028 for (i = 0; i < hisi_sas_debugfs_dump_count; i++) {
4029 if (hisi_sas_debugfs_alloc(hisi_hba, i)) {
4030 debugfs_remove_recursive(hisi_hba->debugfs_dir);
4031 dev_dbg(dev, "failed to init debugfs!\n");
4036 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_init);
4038 void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba)
4040 debugfs_remove_recursive(hisi_hba->debugfs_dir);
4042 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_exit);
4044 int hisi_sas_remove(struct platform_device *pdev)
4046 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
4047 struct hisi_hba *hisi_hba = sha->lldd_ha;
4048 struct Scsi_Host *shost = sha->core.shost;
4050 if (timer_pending(&hisi_hba->timer))
4051 del_timer(&hisi_hba->timer);
4053 sas_unregister_ha(sha);
4054 sas_remove_host(sha->core.shost);
4056 hisi_sas_free(hisi_hba);
4057 scsi_host_put(shost);
4060 EXPORT_SYMBOL_GPL(hisi_sas_remove);
4062 bool hisi_sas_debugfs_enable;
4063 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
4064 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
4065 MODULE_PARM_DESC(hisi_sas_debugfs_enable, "Enable driver debugfs (default disabled)");
4067 u32 hisi_sas_debugfs_dump_count = 1;
4068 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count);
4069 module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444);
4070 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow");
4072 static __init int hisi_sas_init(void)
4074 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
4078 if (hisi_sas_debugfs_enable) {
4079 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
4080 if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) {
4081 pr_info("hisi_sas: Limiting debugfs dump count\n");
4082 hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP;
4089 static __exit void hisi_sas_exit(void)
4091 sas_release_transport(hisi_sas_stt);
4093 debugfs_remove(hisi_sas_debugfs_dir);
4096 module_init(hisi_sas_init);
4097 module_exit(hisi_sas_exit);
4099 MODULE_LICENSE("GPL");
4100 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
4101 MODULE_DESCRIPTION("HISILICON SAS controller driver");
4102 MODULE_ALIAS("platform:" DRV_NAME);