1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2015 Linaro Ltd.
4 * Copyright (c) 2015 Hisilicon Limited.
8 #define DRV_NAME "hisi_sas"
10 #define DEV_IS_GONE(dev) \
11 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
13 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
14 u8 *lun, struct hisi_sas_tmf_task *tmf);
16 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
17 struct domain_device *device,
18 int abort_flag, int tag);
19 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
20 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
22 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
23 struct domain_device *device);
24 static void hisi_sas_dev_gone(struct domain_device *device);
26 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
28 switch (fis->command) {
29 case ATA_CMD_FPDMA_WRITE:
30 case ATA_CMD_FPDMA_READ:
31 case ATA_CMD_FPDMA_RECV:
32 case ATA_CMD_FPDMA_SEND:
33 case ATA_CMD_NCQ_NON_DATA:
34 return HISI_SAS_SATA_PROTOCOL_FPDMA;
36 case ATA_CMD_DOWNLOAD_MICRO:
38 case ATA_CMD_PMP_READ:
39 case ATA_CMD_READ_LOG_EXT:
40 case ATA_CMD_PIO_READ:
41 case ATA_CMD_PIO_READ_EXT:
42 case ATA_CMD_PMP_WRITE:
43 case ATA_CMD_WRITE_LOG_EXT:
44 case ATA_CMD_PIO_WRITE:
45 case ATA_CMD_PIO_WRITE_EXT:
46 return HISI_SAS_SATA_PROTOCOL_PIO;
49 case ATA_CMD_DOWNLOAD_MICRO_DMA:
50 case ATA_CMD_PMP_READ_DMA:
51 case ATA_CMD_PMP_WRITE_DMA:
53 case ATA_CMD_READ_EXT:
54 case ATA_CMD_READ_LOG_DMA_EXT:
55 case ATA_CMD_READ_STREAM_DMA_EXT:
56 case ATA_CMD_TRUSTED_RCV_DMA:
57 case ATA_CMD_TRUSTED_SND_DMA:
59 case ATA_CMD_WRITE_EXT:
60 case ATA_CMD_WRITE_FUA_EXT:
61 case ATA_CMD_WRITE_QUEUED:
62 case ATA_CMD_WRITE_LOG_DMA_EXT:
63 case ATA_CMD_WRITE_STREAM_DMA_EXT:
64 case ATA_CMD_ZAC_MGMT_IN:
65 return HISI_SAS_SATA_PROTOCOL_DMA;
67 case ATA_CMD_CHK_POWER:
68 case ATA_CMD_DEV_RESET:
71 case ATA_CMD_FLUSH_EXT:
73 case ATA_CMD_VERIFY_EXT:
74 case ATA_CMD_SET_FEATURES:
76 case ATA_CMD_STANDBYNOW1:
77 case ATA_CMD_ZAC_MGMT_OUT:
78 return HISI_SAS_SATA_PROTOCOL_NONDATA;
81 switch (fis->features) {
82 case ATA_SET_MAX_PASSWD:
83 case ATA_SET_MAX_LOCK:
84 return HISI_SAS_SATA_PROTOCOL_PIO;
86 case ATA_SET_MAX_PASSWD_DMA:
87 case ATA_SET_MAX_UNLOCK_DMA:
88 return HISI_SAS_SATA_PROTOCOL_DMA;
91 return HISI_SAS_SATA_PROTOCOL_NONDATA;
96 if (direction == DMA_NONE)
97 return HISI_SAS_SATA_PROTOCOL_NONDATA;
98 return HISI_SAS_SATA_PROTOCOL_PIO;
102 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
104 void hisi_sas_sata_done(struct sas_task *task,
105 struct hisi_sas_slot *slot)
107 struct task_status_struct *ts = &task->task_status;
108 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
109 struct hisi_sas_status_buffer *status_buf =
110 hisi_sas_status_buf_addr_mem(slot);
111 u8 *iu = &status_buf->iu[0];
112 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
114 resp->frame_len = sizeof(struct dev_to_host_fis);
115 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
117 ts->buf_valid_size = sizeof(*resp);
119 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
122 * This function assumes linkrate mask fits in 8 bits, which it
123 * does for all HW versions supported.
125 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
130 max -= SAS_LINK_RATE_1_5_GBPS;
131 for (i = 0; i <= max; i++)
132 rate |= 1 << (i * 2);
135 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
137 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
139 return device->port->ha->lldd_ha;
142 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
144 return container_of(sas_port, struct hisi_sas_port, sas_port);
146 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
148 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
152 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
153 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
155 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
157 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
159 void *bitmap = hisi_hba->slot_index_tags;
161 clear_bit(slot_idx, bitmap);
164 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
166 if (hisi_hba->hw->slot_index_alloc ||
167 slot_idx >= HISI_SAS_UNRESERVED_IPTT) {
168 spin_lock(&hisi_hba->lock);
169 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
170 spin_unlock(&hisi_hba->lock);
174 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
176 void *bitmap = hisi_hba->slot_index_tags;
178 set_bit(slot_idx, bitmap);
181 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba,
182 struct scsi_cmnd *scsi_cmnd)
185 void *bitmap = hisi_hba->slot_index_tags;
188 return scsi_cmnd->request->tag;
190 spin_lock(&hisi_hba->lock);
191 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
192 hisi_hba->last_slot_index + 1);
193 if (index >= hisi_hba->slot_index_count) {
194 index = find_next_zero_bit(bitmap,
195 hisi_hba->slot_index_count,
196 HISI_SAS_UNRESERVED_IPTT);
197 if (index >= hisi_hba->slot_index_count) {
198 spin_unlock(&hisi_hba->lock);
199 return -SAS_QUEUE_FULL;
202 hisi_sas_slot_index_set(hisi_hba, index);
203 hisi_hba->last_slot_index = index;
204 spin_unlock(&hisi_hba->lock);
209 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
213 for (i = 0; i < hisi_hba->slot_index_count; ++i)
214 hisi_sas_slot_index_clear(hisi_hba, i);
217 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
218 struct hisi_sas_slot *slot)
220 int device_id = slot->device_id;
221 struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id];
224 struct device *dev = hisi_hba->dev;
226 if (!task->lldd_task)
229 task->lldd_task = NULL;
231 if (!sas_protocol_ata(task->task_proto)) {
233 dma_unmap_sg(dev, task->scatter,
236 if (slot->n_elem_dif) {
237 struct sas_ssp_task *ssp_task = &task->ssp_task;
238 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
240 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
241 scsi_prot_sg_count(scsi_cmnd),
247 spin_lock(&sas_dev->lock);
248 list_del_init(&slot->entry);
249 spin_unlock(&sas_dev->lock);
251 memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
253 hisi_sas_slot_index_free(hisi_hba, slot->idx);
255 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
257 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
258 struct hisi_sas_slot *slot)
260 hisi_hba->hw->prep_smp(hisi_hba, slot);
263 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
264 struct hisi_sas_slot *slot)
266 hisi_hba->hw->prep_ssp(hisi_hba, slot);
269 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
270 struct hisi_sas_slot *slot)
272 hisi_hba->hw->prep_stp(hisi_hba, slot);
275 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
276 struct hisi_sas_slot *slot,
277 int device_id, int abort_flag, int tag_to_abort)
279 hisi_hba->hw->prep_abort(hisi_hba, slot,
280 device_id, abort_flag, tag_to_abort);
283 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
284 struct sas_task *task, int n_elem,
287 struct device *dev = hisi_hba->dev;
289 if (!sas_protocol_ata(task->task_proto)) {
290 if (task->num_scatter) {
292 dma_unmap_sg(dev, task->scatter,
295 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
297 dma_unmap_sg(dev, &task->smp_task.smp_req,
303 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
304 struct sas_task *task, int *n_elem,
307 struct device *dev = hisi_hba->dev;
310 if (sas_protocol_ata(task->task_proto)) {
311 *n_elem = task->num_scatter;
313 unsigned int req_len;
315 if (task->num_scatter) {
316 *n_elem = dma_map_sg(dev, task->scatter,
317 task->num_scatter, task->data_dir);
322 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
323 *n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
329 req_len = sg_dma_len(&task->smp_task.smp_req);
332 goto err_out_dma_unmap;
337 if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
338 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT\n",
341 goto err_out_dma_unmap;
346 /* It would be better to call dma_unmap_sg() here, but it's messy */
347 hisi_sas_dma_unmap(hisi_hba, task, *n_elem,
353 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
354 struct sas_task *task, int n_elem_dif)
356 struct device *dev = hisi_hba->dev;
359 struct sas_ssp_task *ssp_task = &task->ssp_task;
360 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
362 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
363 scsi_prot_sg_count(scsi_cmnd),
368 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
369 int *n_elem_dif, struct sas_task *task)
371 struct device *dev = hisi_hba->dev;
372 struct sas_ssp_task *ssp_task;
373 struct scsi_cmnd *scsi_cmnd;
376 if (task->num_scatter) {
377 ssp_task = &task->ssp_task;
378 scsi_cmnd = ssp_task->cmd;
380 if (scsi_prot_sg_count(scsi_cmnd)) {
381 *n_elem_dif = dma_map_sg(dev,
382 scsi_prot_sglist(scsi_cmnd),
383 scsi_prot_sg_count(scsi_cmnd),
389 if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
390 dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
393 goto err_out_dif_dma_unmap;
400 err_out_dif_dma_unmap:
401 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
402 scsi_prot_sg_count(scsi_cmnd), task->data_dir);
406 static int hisi_sas_task_prep(struct sas_task *task,
407 struct hisi_sas_dq **dq_pointer,
408 bool is_tmf, struct hisi_sas_tmf_task *tmf,
411 struct domain_device *device = task->dev;
412 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
413 struct hisi_sas_device *sas_dev = device->lldd_dev;
414 struct hisi_sas_port *port;
415 struct hisi_sas_slot *slot;
416 struct hisi_sas_cmd_hdr *cmd_hdr_base;
417 struct asd_sas_port *sas_port = device->port;
418 struct device *dev = hisi_hba->dev;
419 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
420 int n_elem = 0, n_elem_dif = 0, n_elem_req = 0;
421 struct scsi_cmnd *scmd = NULL;
422 struct hisi_sas_dq *dq;
426 if (DEV_IS_GONE(sas_dev)) {
428 dev_info(dev, "task prep: device %d not ready\n",
431 dev_info(dev, "task prep: device %016llx not ready\n",
432 SAS_ADDR(device->sas_addr));
437 if (task->uldd_task) {
438 struct ata_queued_cmd *qc;
440 if (dev_is_sata(device)) {
441 qc = task->uldd_task;
444 scmd = task->uldd_task;
449 unsigned int dq_index;
452 blk_tag = blk_mq_unique_tag(scmd->request);
453 dq_index = blk_mq_unique_tag_to_hwq(blk_tag);
454 *dq_pointer = dq = &hisi_hba->dq[dq_index];
456 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];
463 port = to_hisi_sas_port(sas_port);
464 if (port && !port->port_attached) {
465 dev_info(dev, "task prep: %s port%d not attach device\n",
466 (dev_is_sata(device)) ?
473 rc = hisi_sas_dma_map(hisi_hba, task, &n_elem,
478 if (!sas_protocol_ata(task->task_proto)) {
479 rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
481 goto err_out_dma_unmap;
484 if (hisi_hba->hw->slot_index_alloc)
485 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
487 rc = hisi_sas_slot_index_alloc(hisi_hba, scmd);
490 goto err_out_dif_dma_unmap;
493 slot = &hisi_hba->slot_info[slot_idx];
495 spin_lock(&dq->lock);
496 wr_q_index = dq->wr_point;
497 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
498 list_add_tail(&slot->delivery, &dq->list);
499 spin_unlock(&dq->lock);
500 spin_lock(&sas_dev->lock);
501 list_add_tail(&slot->entry, &sas_dev->list);
502 spin_unlock(&sas_dev->lock);
504 dlvry_queue = dq->id;
505 dlvry_queue_slot = wr_q_index;
507 slot->device_id = sas_dev->device_id;
508 slot->n_elem = n_elem;
509 slot->n_elem_dif = n_elem_dif;
510 slot->dlvry_queue = dlvry_queue;
511 slot->dlvry_queue_slot = dlvry_queue_slot;
512 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
513 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
517 slot->is_internal = is_tmf;
518 task->lldd_task = slot;
520 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
521 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
522 memset(hisi_sas_status_buf_addr_mem(slot), 0,
523 sizeof(struct hisi_sas_err_record));
525 switch (task->task_proto) {
526 case SAS_PROTOCOL_SMP:
527 hisi_sas_task_prep_smp(hisi_hba, slot);
529 case SAS_PROTOCOL_SSP:
530 hisi_sas_task_prep_ssp(hisi_hba, slot);
532 case SAS_PROTOCOL_SATA:
533 case SAS_PROTOCOL_STP:
534 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
535 hisi_sas_task_prep_ata(hisi_hba, slot);
538 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
543 spin_lock_irqsave(&task->task_state_lock, flags);
544 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
545 spin_unlock_irqrestore(&task->task_state_lock, flags);
548 WRITE_ONCE(slot->ready, 1);
552 err_out_dif_dma_unmap:
553 if (!sas_protocol_ata(task->task_proto))
554 hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
556 hisi_sas_dma_unmap(hisi_hba, task, n_elem,
559 dev_err(dev, "task prep: failed[%d]!\n", rc);
563 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
564 bool is_tmf, struct hisi_sas_tmf_task *tmf)
568 struct hisi_hba *hisi_hba;
570 struct domain_device *device = task->dev;
571 struct asd_sas_port *sas_port = device->port;
572 struct hisi_sas_dq *dq = NULL;
575 struct task_status_struct *ts = &task->task_status;
577 ts->resp = SAS_TASK_UNDELIVERED;
578 ts->stat = SAS_PHY_DOWN;
580 * libsas will use dev->port, should
581 * not call task_done for sata
583 if (device->dev_type != SAS_SATA_DEV)
584 task->task_done(task);
588 hisi_hba = dev_to_hisi_hba(device);
591 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
592 if (!gfpflags_allow_blocking(gfp_flags))
595 down(&hisi_hba->sem);
599 /* protect task_prep and start_delivery sequence */
600 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
602 dev_err(dev, "task exec: failed[%d]!\n", rc);
605 spin_lock(&dq->lock);
606 hisi_hba->hw->start_delivery(dq);
607 spin_unlock(&dq->lock);
613 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no,
616 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
617 struct asd_sas_phy *sas_phy = &phy->sas_phy;
619 if (!phy->phy_attached)
622 if (test_bit(HISI_SAS_PM_BIT, &hisi_hba->flags) &&
623 !sas_phy->suspended) {
624 dev_warn(hisi_hba->dev, "phy%d during suspend filtered out\n", phy_no);
628 sas_notify_phy_event(sas_phy, PHYE_OOB_DONE, gfp_flags);
631 struct sas_phy *sphy = sas_phy->phy;
633 sphy->negotiated_linkrate = sas_phy->linkrate;
634 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
635 sphy->maximum_linkrate_hw =
636 hisi_hba->hw->phy_get_max_linkrate();
637 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
638 sphy->minimum_linkrate = phy->minimum_linkrate;
640 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
641 sphy->maximum_linkrate = phy->maximum_linkrate;
644 if (phy->phy_type & PORT_TYPE_SAS) {
645 struct sas_identify_frame *id;
647 id = (struct sas_identify_frame *)phy->frame_rcvd;
648 id->dev_type = phy->identify.device_type;
649 id->initiator_bits = SAS_PROTOCOL_ALL;
650 id->target_bits = phy->identify.target_port_protocols;
651 } else if (phy->phy_type & PORT_TYPE_SATA) {
655 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
656 sas_notify_port_event(sas_phy, PORTE_BYTES_DMAED, gfp_flags);
659 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
661 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
662 struct hisi_sas_device *sas_dev = NULL;
663 int last = hisi_hba->last_dev_id;
664 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
667 spin_lock(&hisi_hba->lock);
668 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
669 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
670 int queue = i % hisi_hba->queue_count;
671 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
673 hisi_hba->devices[i].device_id = i;
674 sas_dev = &hisi_hba->devices[i];
675 sas_dev->dev_status = HISI_SAS_DEV_INIT;
676 sas_dev->dev_type = device->dev_type;
677 sas_dev->hisi_hba = hisi_hba;
678 sas_dev->sas_device = device;
680 spin_lock_init(&sas_dev->lock);
681 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
686 hisi_hba->last_dev_id = i;
687 spin_unlock(&hisi_hba->lock);
692 #define HISI_SAS_DISK_RECOVER_CNT 3
693 static int hisi_sas_init_device(struct domain_device *device)
695 int rc = TMF_RESP_FUNC_COMPLETE;
697 struct hisi_sas_tmf_task tmf_task;
698 int retry = HISI_SAS_DISK_RECOVER_CNT;
699 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
700 struct device *dev = hisi_hba->dev;
701 struct sas_phy *local_phy;
703 switch (device->dev_type) {
705 int_to_scsilun(0, &lun);
707 tmf_task.tmf = TMF_CLEAR_TASK_SET;
708 while (retry-- > 0) {
709 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
711 if (rc == TMF_RESP_FUNC_COMPLETE) {
712 hisi_sas_release_task(hisi_hba, device);
719 case SAS_SATA_PM_PORT:
720 case SAS_SATA_PENDING:
722 * send HARD RESET to clear previous affiliation of
725 local_phy = sas_get_local_phy(device);
726 if (!scsi_is_sas_phy_local(local_phy) &&
727 !test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
728 unsigned long deadline = ata_deadline(jiffies, 20000);
729 struct sata_device *sata_dev = &device->sata_dev;
730 struct ata_host *ata_host = sata_dev->ata_host;
731 struct ata_port_operations *ops = ata_host->ops;
732 struct ata_port *ap = sata_dev->ap;
733 struct ata_link *link;
734 unsigned int classes;
736 ata_for_each_link(link, ap, EDGE)
737 rc = ops->hardreset(link, &classes,
740 sas_put_local_phy(local_phy);
742 dev_warn(dev, "SATA disk hardreset fail: %d\n", rc);
746 while (retry-- > 0) {
747 rc = hisi_sas_softreset_ata_disk(device);
759 static int hisi_sas_dev_found(struct domain_device *device)
761 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
762 struct domain_device *parent_dev = device->parent;
763 struct hisi_sas_device *sas_dev;
764 struct device *dev = hisi_hba->dev;
767 if (hisi_hba->hw->alloc_dev)
768 sas_dev = hisi_hba->hw->alloc_dev(device);
770 sas_dev = hisi_sas_alloc_dev(device);
772 dev_err(dev, "fail alloc dev: max support %d devices\n",
773 HISI_SAS_MAX_DEVICES);
777 device->lldd_dev = sas_dev;
778 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
780 if (parent_dev && dev_is_expander(parent_dev->dev_type)) {
782 u8 phy_num = parent_dev->ex_dev.num_phys;
785 for (phy_no = 0; phy_no < phy_num; phy_no++) {
786 phy = &parent_dev->ex_dev.ex_phy[phy_no];
787 if (SAS_ADDR(phy->attached_sas_addr) ==
788 SAS_ADDR(device->sas_addr))
792 if (phy_no == phy_num) {
793 dev_info(dev, "dev found: no attached "
794 "dev:%016llx at ex:%016llx\n",
795 SAS_ADDR(device->sas_addr),
796 SAS_ADDR(parent_dev->sas_addr));
802 dev_info(dev, "dev[%d:%x] found\n",
803 sas_dev->device_id, sas_dev->dev_type);
805 rc = hisi_sas_init_device(device);
808 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
812 hisi_sas_dev_gone(device);
816 int hisi_sas_slave_configure(struct scsi_device *sdev)
818 struct domain_device *dev = sdev_to_domain_dev(sdev);
819 int ret = sas_slave_configure(sdev);
823 if (!dev_is_sata(dev))
824 sas_change_queue_depth(sdev, 64);
828 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
830 void hisi_sas_scan_start(struct Scsi_Host *shost)
832 struct hisi_hba *hisi_hba = shost_priv(shost);
834 hisi_hba->hw->phys_init(hisi_hba);
836 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
838 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
840 struct hisi_hba *hisi_hba = shost_priv(shost);
841 struct sas_ha_struct *sha = &hisi_hba->sha;
843 /* Wait for PHY up interrupt to occur */
850 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
852 static void hisi_sas_phyup_work(struct work_struct *work)
854 struct hisi_sas_phy *phy =
855 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
856 struct hisi_hba *hisi_hba = phy->hisi_hba;
857 struct asd_sas_phy *sas_phy = &phy->sas_phy;
858 int phy_no = sas_phy->id;
860 phy->wait_phyup_cnt = 0;
861 if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
862 hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
863 hisi_sas_bytes_dmaed(hisi_hba, phy_no, GFP_KERNEL);
866 static void hisi_sas_linkreset_work(struct work_struct *work)
868 struct hisi_sas_phy *phy =
869 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
870 struct asd_sas_phy *sas_phy = &phy->sas_phy;
872 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
875 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
876 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
877 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
880 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
881 enum hisi_sas_phy_event event)
883 struct hisi_hba *hisi_hba = phy->hisi_hba;
885 if (WARN_ON(event >= HISI_PHYES_NUM))
888 return queue_work(hisi_hba->wq, &phy->works[event]);
890 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
892 static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
894 struct hisi_sas_phy *phy = from_timer(phy, t, timer);
895 struct hisi_hba *hisi_hba = phy->hisi_hba;
896 struct device *dev = hisi_hba->dev;
897 int phy_no = phy->sas_phy.id;
899 dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
900 hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
903 #define HISI_SAS_WAIT_PHYUP_RETRIES 10
905 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
907 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
908 struct device *dev = hisi_hba->dev;
910 dev_dbg(dev, "phy%d OOB ready\n", phy_no);
911 if (phy->phy_attached)
914 if (!timer_pending(&phy->timer)) {
915 if (phy->wait_phyup_cnt < HISI_SAS_WAIT_PHYUP_RETRIES) {
916 phy->wait_phyup_cnt++;
917 phy->timer.expires = jiffies +
918 HISI_SAS_WAIT_PHYUP_TIMEOUT * HZ;
919 add_timer(&phy->timer);
921 dev_warn(dev, "phy%d failed to come up %d times, giving up\n",
922 phy_no, phy->wait_phyup_cnt);
923 phy->wait_phyup_cnt = 0;
927 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
929 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
931 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
932 struct asd_sas_phy *sas_phy = &phy->sas_phy;
935 phy->hisi_hba = hisi_hba;
937 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
938 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
939 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
940 sas_phy->class = SAS;
941 sas_phy->iproto = SAS_PROTOCOL_ALL;
943 sas_phy->type = PHY_TYPE_PHYSICAL;
944 sas_phy->role = PHY_ROLE_INITIATOR;
945 sas_phy->oob_mode = OOB_NOT_CONNECTED;
946 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
947 sas_phy->id = phy_no;
948 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
949 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
950 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
951 sas_phy->lldd_phy = phy;
953 for (i = 0; i < HISI_PHYES_NUM; i++)
954 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
956 spin_lock_init(&phy->lock);
958 timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
961 /* Wrapper to ensure we track hisi_sas_phy.enable properly */
962 void hisi_sas_phy_enable(struct hisi_hba *hisi_hba, int phy_no, int enable)
964 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
965 struct asd_sas_phy *aphy = &phy->sas_phy;
966 struct sas_phy *sphy = aphy->phy;
969 spin_lock_irqsave(&phy->lock, flags);
972 /* We may have been enabled already; if so, don't touch */
974 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
975 hisi_hba->hw->phy_start(hisi_hba, phy_no);
977 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
978 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
980 phy->enable = enable;
981 spin_unlock_irqrestore(&phy->lock, flags);
983 EXPORT_SYMBOL_GPL(hisi_sas_phy_enable);
985 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
987 struct sas_ha_struct *sas_ha = sas_phy->ha;
988 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
989 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
990 struct asd_sas_port *sas_port = sas_phy->port;
991 struct hisi_sas_port *port;
997 port = to_hisi_sas_port(sas_port);
998 spin_lock_irqsave(&hisi_hba->lock, flags);
999 port->port_attached = 1;
1000 port->id = phy->port_id;
1002 sas_port->lldd_port = port;
1003 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1006 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
1007 struct hisi_sas_slot *slot)
1010 unsigned long flags;
1011 struct task_status_struct *ts;
1013 ts = &task->task_status;
1015 ts->resp = SAS_TASK_COMPLETE;
1016 ts->stat = SAS_ABORTED_TASK;
1017 spin_lock_irqsave(&task->task_state_lock, flags);
1018 task->task_state_flags &=
1019 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1020 if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
1021 task->task_state_flags |= SAS_TASK_STATE_DONE;
1022 spin_unlock_irqrestore(&task->task_state_lock, flags);
1025 hisi_sas_slot_task_free(hisi_hba, task, slot);
1028 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
1029 struct domain_device *device)
1031 struct hisi_sas_slot *slot, *slot2;
1032 struct hisi_sas_device *sas_dev = device->lldd_dev;
1034 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
1035 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
1038 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
1040 struct hisi_sas_device *sas_dev;
1041 struct domain_device *device;
1044 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1045 sas_dev = &hisi_hba->devices[i];
1046 device = sas_dev->sas_device;
1048 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
1052 hisi_sas_release_task(hisi_hba, device);
1055 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
1057 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
1058 struct domain_device *device)
1060 if (hisi_hba->hw->dereg_device)
1061 hisi_hba->hw->dereg_device(hisi_hba, device);
1064 static void hisi_sas_dev_gone(struct domain_device *device)
1066 struct hisi_sas_device *sas_dev = device->lldd_dev;
1067 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1068 struct device *dev = hisi_hba->dev;
1071 dev_info(dev, "dev[%d:%x] is gone\n",
1072 sas_dev->device_id, sas_dev->dev_type);
1074 down(&hisi_hba->sem);
1075 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
1076 hisi_sas_internal_task_abort(hisi_hba, device,
1077 HISI_SAS_INT_ABT_DEV, 0);
1079 hisi_sas_dereg_device(hisi_hba, device);
1081 ret = hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
1082 device->lldd_dev = NULL;
1085 if (hisi_hba->hw->free_device)
1086 hisi_hba->hw->free_device(sas_dev);
1088 /* Don't mark it as SAS_PHY_UNUSED if failed to clear ITCT */
1090 sas_dev->dev_type = SAS_PHY_UNUSED;
1091 sas_dev->sas_device = NULL;
1095 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
1097 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
1100 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
1101 struct sas_phy_linkrates *r)
1103 struct sas_phy_linkrates _r;
1105 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1106 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1107 enum sas_linkrate min, max;
1109 if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
1112 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1113 max = sas_phy->phy->maximum_linkrate;
1114 min = r->minimum_linkrate;
1115 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1116 max = r->maximum_linkrate;
1117 min = sas_phy->phy->minimum_linkrate;
1121 _r.maximum_linkrate = max;
1122 _r.minimum_linkrate = min;
1124 sas_phy->phy->maximum_linkrate = max;
1125 sas_phy->phy->minimum_linkrate = min;
1127 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1129 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
1130 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1135 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
1138 struct sas_ha_struct *sas_ha = sas_phy->ha;
1139 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1140 int phy_no = sas_phy->id;
1143 case PHY_FUNC_HARD_RESET:
1144 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
1147 case PHY_FUNC_LINK_RESET:
1148 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1150 hisi_sas_phy_enable(hisi_hba, phy_no, 1);
1153 case PHY_FUNC_DISABLE:
1154 hisi_sas_phy_enable(hisi_hba, phy_no, 0);
1157 case PHY_FUNC_SET_LINK_RATE:
1158 return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1159 case PHY_FUNC_GET_EVENTS:
1160 if (hisi_hba->hw->get_events) {
1161 hisi_hba->hw->get_events(hisi_hba, phy_no);
1165 case PHY_FUNC_RELEASE_SPINUP_HOLD:
1172 static void hisi_sas_task_done(struct sas_task *task)
1174 del_timer(&task->slow_task->timer);
1175 complete(&task->slow_task->completion);
1178 static void hisi_sas_tmf_timedout(struct timer_list *t)
1180 struct sas_task_slow *slow = from_timer(slow, t, timer);
1181 struct sas_task *task = slow->task;
1182 unsigned long flags;
1183 bool is_completed = true;
1185 spin_lock_irqsave(&task->task_state_lock, flags);
1186 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1187 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1188 is_completed = false;
1190 spin_unlock_irqrestore(&task->task_state_lock, flags);
1193 complete(&task->slow_task->completion);
1196 #define TASK_TIMEOUT 20
1197 #define TASK_RETRY 3
1198 #define INTERNAL_ABORT_TIMEOUT 6
1199 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1200 void *parameter, u32 para_len,
1201 struct hisi_sas_tmf_task *tmf)
1203 struct hisi_sas_device *sas_dev = device->lldd_dev;
1204 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1205 struct device *dev = hisi_hba->dev;
1206 struct sas_task *task;
1209 for (retry = 0; retry < TASK_RETRY; retry++) {
1210 task = sas_alloc_slow_task(GFP_KERNEL);
1215 task->task_proto = device->tproto;
1217 if (dev_is_sata(device)) {
1218 task->ata_task.device_control_reg_update = 1;
1219 memcpy(&task->ata_task.fis, parameter, para_len);
1221 memcpy(&task->ssp_task, parameter, para_len);
1223 task->task_done = hisi_sas_task_done;
1225 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1226 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT * HZ;
1227 add_timer(&task->slow_task->timer);
1229 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1232 del_timer(&task->slow_task->timer);
1233 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1238 wait_for_completion(&task->slow_task->completion);
1239 res = TMF_RESP_FUNC_FAILED;
1240 /* Even TMF timed out, return direct. */
1241 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1242 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1243 struct hisi_sas_slot *slot = task->lldd_task;
1245 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1247 struct hisi_sas_cq *cq =
1248 &hisi_hba->cq[slot->dlvry_queue];
1250 * sync irq to avoid free'ing task
1251 * before using task in IO completion
1253 synchronize_irq(cq->irq_no);
1259 dev_err(dev, "abort tmf: TMF task timeout\n");
1262 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1263 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1264 res = TMF_RESP_FUNC_COMPLETE;
1268 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1269 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1270 res = TMF_RESP_FUNC_SUCC;
1274 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1275 task->task_status.stat == SAS_DATA_UNDERRUN) {
1276 /* no error, but return the number of bytes of
1279 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x sts 0x%x underrun\n",
1280 SAS_ADDR(device->sas_addr),
1281 task->task_status.resp,
1282 task->task_status.stat);
1283 res = task->task_status.residual;
1287 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1288 task->task_status.stat == SAS_DATA_OVERRUN) {
1289 dev_warn(dev, "abort tmf: blocked task error\n");
1294 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1295 task->task_status.stat == SAS_OPEN_REJECT) {
1296 dev_warn(dev, "abort tmf: open reject failed\n");
1299 dev_warn(dev, "abort tmf: task to dev %016llx resp: 0x%x status 0x%x\n",
1300 SAS_ADDR(device->sas_addr),
1301 task->task_status.resp,
1302 task->task_status.stat);
1304 sas_free_task(task);
1308 if (retry == TASK_RETRY)
1309 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1310 sas_free_task(task);
1314 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1315 bool reset, int pmp, u8 *fis)
1317 struct ata_taskfile tf;
1319 ata_tf_init(dev, &tf);
1323 tf.ctl &= ~ATA_SRST;
1324 tf.command = ATA_CMD_DEV_RESET;
1325 ata_tf_to_fis(&tf, pmp, 0, fis);
1328 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1331 struct ata_port *ap = device->sata_dev.ap;
1332 struct ata_link *link;
1333 int rc = TMF_RESP_FUNC_FAILED;
1334 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1335 struct device *dev = hisi_hba->dev;
1336 int s = sizeof(struct host_to_dev_fis);
1338 ata_for_each_link(link, ap, EDGE) {
1339 int pmp = sata_srst_pmp(link);
1341 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1342 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1343 if (rc != TMF_RESP_FUNC_COMPLETE)
1347 if (rc == TMF_RESP_FUNC_COMPLETE) {
1348 ata_for_each_link(link, ap, EDGE) {
1349 int pmp = sata_srst_pmp(link);
1351 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1352 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1354 if (rc != TMF_RESP_FUNC_COMPLETE)
1355 dev_err(dev, "ata disk %016llx de-reset failed\n",
1356 SAS_ADDR(device->sas_addr));
1359 dev_err(dev, "ata disk %016llx reset failed\n",
1360 SAS_ADDR(device->sas_addr));
1363 if (rc == TMF_RESP_FUNC_COMPLETE)
1364 hisi_sas_release_task(hisi_hba, device);
1369 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1370 u8 *lun, struct hisi_sas_tmf_task *tmf)
1372 struct sas_ssp_task ssp_task;
1374 if (!(device->tproto & SAS_PROTOCOL_SSP))
1375 return TMF_RESP_FUNC_ESUPP;
1377 memcpy(ssp_task.LUN, lun, 8);
1379 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1380 sizeof(ssp_task), tmf);
1383 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1385 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1388 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1389 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1390 struct domain_device *device = sas_dev->sas_device;
1391 struct asd_sas_port *sas_port;
1392 struct hisi_sas_port *port;
1393 struct hisi_sas_phy *phy = NULL;
1394 struct asd_sas_phy *sas_phy;
1396 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1397 || !device || !device->port)
1400 sas_port = device->port;
1401 port = to_hisi_sas_port(sas_port);
1403 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1404 if (state & BIT(sas_phy->id)) {
1405 phy = sas_phy->lldd_phy;
1410 port->id = phy->port_id;
1412 /* Update linkrate of directly attached device. */
1413 if (!device->parent)
1414 device->linkrate = phy->sas_phy.linkrate;
1416 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1422 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 state)
1424 struct asd_sas_port *_sas_port = NULL;
1427 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1428 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1429 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1430 struct asd_sas_port *sas_port = sas_phy->port;
1431 bool do_port_check = _sas_port != sas_port;
1433 if (!sas_phy->phy->enabled)
1436 /* Report PHY state change to libsas */
1437 if (state & BIT(phy_no)) {
1438 if (do_port_check && sas_port && sas_port->port_dev) {
1439 struct domain_device *dev = sas_port->port_dev;
1441 _sas_port = sas_port;
1443 if (dev_is_expander(dev->dev_type))
1444 sas_notify_port_event(sas_phy,
1445 PORTE_BROADCAST_RCVD,
1449 hisi_sas_phy_down(hisi_hba, phy_no, 0, GFP_KERNEL);
1454 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1456 struct hisi_sas_device *sas_dev;
1457 struct domain_device *device;
1460 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1461 sas_dev = &hisi_hba->devices[i];
1462 device = sas_dev->sas_device;
1464 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1467 hisi_sas_init_device(device);
1471 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1472 struct asd_sas_port *sas_port,
1473 struct domain_device *device)
1475 struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1476 struct ata_port *ap = device->sata_dev.ap;
1477 struct device *dev = hisi_hba->dev;
1478 int s = sizeof(struct host_to_dev_fis);
1479 int rc = TMF_RESP_FUNC_FAILED;
1480 struct asd_sas_phy *sas_phy;
1481 struct ata_link *link;
1485 state = hisi_hba->hw->get_phys_state(hisi_hba);
1486 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1487 if (!(state & BIT(sas_phy->id)))
1490 ata_for_each_link(link, ap, EDGE) {
1491 int pmp = sata_srst_pmp(link);
1493 tmf_task.phy_id = sas_phy->id;
1494 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1495 rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1497 if (rc != TMF_RESP_FUNC_COMPLETE) {
1498 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1506 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1508 struct device *dev = hisi_hba->dev;
1511 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1512 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1513 struct domain_device *device = sas_dev->sas_device;
1515 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1518 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1519 HISI_SAS_INT_ABT_DEV, 0);
1521 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1524 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1525 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1526 struct asd_sas_port *sas_port = &port->sas_port;
1527 struct domain_device *port_dev = sas_port->port_dev;
1528 struct domain_device *device;
1530 if (!port_dev || !dev_is_expander(port_dev->dev_type))
1533 /* Try to find a SATA device */
1534 list_for_each_entry(device, &sas_port->dev_list,
1536 if (dev_is_sata(device)) {
1537 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1546 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1548 struct Scsi_Host *shost = hisi_hba->shost;
1550 down(&hisi_hba->sem);
1551 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1553 scsi_block_requests(shost);
1554 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1556 if (timer_pending(&hisi_hba->timer))
1557 del_timer_sync(&hisi_hba->timer);
1559 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1561 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1563 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1565 struct Scsi_Host *shost = hisi_hba->shost;
1567 /* Init and wait for PHYs to come up and all libsas event finished. */
1568 hisi_hba->hw->phys_init(hisi_hba);
1570 hisi_sas_refresh_port_id(hisi_hba);
1571 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1573 if (hisi_hba->reject_stp_links_msk)
1574 hisi_sas_terminate_stp_reject(hisi_hba);
1575 hisi_sas_reset_init_all_devices(hisi_hba);
1577 scsi_unblock_requests(shost);
1578 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1580 hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state);
1582 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1584 static int hisi_sas_controller_prereset(struct hisi_hba *hisi_hba)
1586 if (!hisi_hba->hw->soft_reset)
1589 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1592 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
1593 hisi_hba->hw->debugfs_snapshot_regs(hisi_hba);
1598 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1600 struct device *dev = hisi_hba->dev;
1601 struct Scsi_Host *shost = hisi_hba->shost;
1604 dev_info(dev, "controller resetting...\n");
1605 hisi_sas_controller_reset_prepare(hisi_hba);
1607 rc = hisi_hba->hw->soft_reset(hisi_hba);
1609 dev_warn(dev, "controller reset failed (%d)\n", rc);
1610 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1612 scsi_unblock_requests(shost);
1613 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1617 hisi_sas_controller_reset_done(hisi_hba);
1618 dev_info(dev, "controller reset complete\n");
1623 static int hisi_sas_abort_task(struct sas_task *task)
1625 struct scsi_lun lun;
1626 struct hisi_sas_tmf_task tmf_task;
1627 struct domain_device *device = task->dev;
1628 struct hisi_sas_device *sas_dev = device->lldd_dev;
1629 struct hisi_hba *hisi_hba;
1631 int rc = TMF_RESP_FUNC_FAILED;
1632 unsigned long flags;
1635 return TMF_RESP_FUNC_FAILED;
1637 hisi_hba = dev_to_hisi_hba(task->dev);
1638 dev = hisi_hba->dev;
1640 spin_lock_irqsave(&task->task_state_lock, flags);
1641 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1642 struct hisi_sas_slot *slot = task->lldd_task;
1643 struct hisi_sas_cq *cq;
1647 * sync irq to avoid free'ing task
1648 * before using task in IO completion
1650 cq = &hisi_hba->cq[slot->dlvry_queue];
1651 synchronize_irq(cq->irq_no);
1653 spin_unlock_irqrestore(&task->task_state_lock, flags);
1654 rc = TMF_RESP_FUNC_COMPLETE;
1657 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1658 spin_unlock_irqrestore(&task->task_state_lock, flags);
1660 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1661 struct scsi_cmnd *cmnd = task->uldd_task;
1662 struct hisi_sas_slot *slot = task->lldd_task;
1663 u16 tag = slot->idx;
1666 int_to_scsilun(cmnd->device->lun, &lun);
1667 tmf_task.tmf = TMF_ABORT_TASK;
1668 tmf_task.tag_of_task_to_be_managed = tag;
1670 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1673 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1674 HISI_SAS_INT_ABT_CMD, tag);
1676 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1677 return TMF_RESP_FUNC_FAILED;
1681 * If the TMF finds that the IO is not in the device and also
1682 * the internal abort does not succeed, then it is safe to
1684 * Note: if the internal abort succeeds then the slot
1685 * will have already been completed
1687 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1688 if (task->lldd_task)
1689 hisi_sas_do_release_task(hisi_hba, task, slot);
1691 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1692 task->task_proto & SAS_PROTOCOL_STP) {
1693 if (task->dev->dev_type == SAS_SATA_DEV) {
1694 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1695 HISI_SAS_INT_ABT_DEV,
1698 dev_err(dev, "abort task: internal abort failed\n");
1701 hisi_sas_dereg_device(hisi_hba, device);
1702 rc = hisi_sas_softreset_ata_disk(device);
1704 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1706 struct hisi_sas_slot *slot = task->lldd_task;
1707 u32 tag = slot->idx;
1708 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1710 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1711 HISI_SAS_INT_ABT_CMD, tag);
1712 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1715 * sync irq to avoid free'ing task
1716 * before using task in IO completion
1718 synchronize_irq(cq->irq_no);
1724 if (rc != TMF_RESP_FUNC_COMPLETE)
1725 dev_notice(dev, "abort task: rc=%d\n", rc);
1729 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1731 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1732 struct device *dev = hisi_hba->dev;
1733 struct hisi_sas_tmf_task tmf_task;
1736 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1737 HISI_SAS_INT_ABT_DEV, 0);
1739 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1740 return TMF_RESP_FUNC_FAILED;
1742 hisi_sas_dereg_device(hisi_hba, device);
1744 tmf_task.tmf = TMF_ABORT_TASK_SET;
1745 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1747 if (rc == TMF_RESP_FUNC_COMPLETE)
1748 hisi_sas_release_task(hisi_hba, device);
1753 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1755 struct hisi_sas_tmf_task tmf_task;
1758 tmf_task.tmf = TMF_CLEAR_ACA;
1759 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1764 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1766 struct sas_phy *local_phy = sas_get_local_phy(device);
1767 struct hisi_sas_device *sas_dev = device->lldd_dev;
1768 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1769 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1770 DECLARE_COMPLETION_ONSTACK(phyreset);
1773 if (!local_phy->enabled) {
1774 sas_put_local_phy(local_phy);
1778 if (scsi_is_sas_phy_local(local_phy)) {
1779 struct asd_sas_phy *sas_phy =
1780 sas_ha->sas_phy[local_phy->number];
1781 struct hisi_sas_phy *phy =
1782 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1784 phy->reset_completion = &phyreset;
1787 reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
1788 !dev_is_sata(device)) ? true : false;
1790 rc = sas_phy_reset(local_phy, reset_type);
1791 sas_put_local_phy(local_phy);
1793 if (scsi_is_sas_phy_local(local_phy)) {
1794 struct asd_sas_phy *sas_phy =
1795 sas_ha->sas_phy[local_phy->number];
1796 struct hisi_sas_phy *phy =
1797 container_of(sas_phy, struct hisi_sas_phy, sas_phy);
1798 int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1799 unsigned long flags;
1801 spin_lock_irqsave(&phy->lock, flags);
1802 phy->reset_completion = NULL;
1804 spin_unlock_irqrestore(&phy->lock, flags);
1806 /* report PHY down if timed out */
1808 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0, GFP_KERNEL);
1809 } else if (sas_dev->dev_status != HISI_SAS_DEV_INIT) {
1811 * If in init state, we rely on caller to wait for link to be
1812 * ready; otherwise, except phy reset is fail, delay.
1821 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1823 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1824 struct device *dev = hisi_hba->dev;
1827 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1828 HISI_SAS_INT_ABT_DEV, 0);
1830 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1831 return TMF_RESP_FUNC_FAILED;
1833 hisi_sas_dereg_device(hisi_hba, device);
1835 if (dev_is_sata(device)) {
1836 rc = hisi_sas_softreset_ata_disk(device);
1837 if (rc == TMF_RESP_FUNC_FAILED)
1838 return TMF_RESP_FUNC_FAILED;
1841 rc = hisi_sas_debug_I_T_nexus_reset(device);
1843 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1844 hisi_sas_release_task(hisi_hba, device);
1849 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1851 struct hisi_sas_device *sas_dev = device->lldd_dev;
1852 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1853 struct device *dev = hisi_hba->dev;
1854 int rc = TMF_RESP_FUNC_FAILED;
1856 /* Clear internal IO and then lu reset */
1857 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1858 HISI_SAS_INT_ABT_DEV, 0);
1860 dev_err(dev, "lu_reset: internal abort failed\n");
1863 hisi_sas_dereg_device(hisi_hba, device);
1865 if (dev_is_sata(device)) {
1866 struct sas_phy *phy;
1868 phy = sas_get_local_phy(device);
1870 rc = sas_phy_reset(phy, true);
1873 hisi_sas_release_task(hisi_hba, device);
1874 sas_put_local_phy(phy);
1876 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1878 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1879 if (rc == TMF_RESP_FUNC_COMPLETE)
1880 hisi_sas_release_task(hisi_hba, device);
1883 if (rc != TMF_RESP_FUNC_COMPLETE)
1884 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1885 sas_dev->device_id, rc);
1889 static void hisi_sas_async_I_T_nexus_reset(void *data, async_cookie_t cookie)
1891 struct domain_device *device = data;
1892 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1895 rc = hisi_sas_debug_I_T_nexus_reset(device);
1896 if (rc != TMF_RESP_FUNC_COMPLETE)
1897 dev_info(hisi_hba->dev, "I_T_nexus reset fail for dev:%016llx rc=%d\n",
1898 SAS_ADDR(device->sas_addr), rc);
1901 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1903 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1904 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1905 ASYNC_DOMAIN_EXCLUSIVE(async);
1908 queue_work(hisi_hba->wq, &r.work);
1909 wait_for_completion(r.completion);
1911 return TMF_RESP_FUNC_FAILED;
1913 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1914 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1915 struct domain_device *device = sas_dev->sas_device;
1917 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1918 dev_is_expander(device->dev_type))
1921 async_schedule_domain(hisi_sas_async_I_T_nexus_reset,
1925 async_synchronize_full_domain(&async);
1926 hisi_sas_release_tasks(hisi_hba);
1928 return TMF_RESP_FUNC_COMPLETE;
1931 static int hisi_sas_query_task(struct sas_task *task)
1933 struct scsi_lun lun;
1934 struct hisi_sas_tmf_task tmf_task;
1935 int rc = TMF_RESP_FUNC_FAILED;
1937 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1938 struct scsi_cmnd *cmnd = task->uldd_task;
1939 struct domain_device *device = task->dev;
1940 struct hisi_sas_slot *slot = task->lldd_task;
1941 u32 tag = slot->idx;
1943 int_to_scsilun(cmnd->device->lun, &lun);
1944 tmf_task.tmf = TMF_QUERY_TASK;
1945 tmf_task.tag_of_task_to_be_managed = tag;
1947 rc = hisi_sas_debug_issue_ssp_tmf(device,
1951 /* The task is still in Lun, release it then */
1952 case TMF_RESP_FUNC_SUCC:
1953 /* The task is not in Lun or failed, reset the phy */
1954 case TMF_RESP_FUNC_FAILED:
1955 case TMF_RESP_FUNC_COMPLETE:
1958 rc = TMF_RESP_FUNC_FAILED;
1966 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1967 struct sas_task *task, int abort_flag,
1968 int task_tag, struct hisi_sas_dq *dq)
1970 struct domain_device *device = task->dev;
1971 struct hisi_sas_device *sas_dev = device->lldd_dev;
1972 struct device *dev = hisi_hba->dev;
1973 struct hisi_sas_port *port;
1974 struct hisi_sas_slot *slot;
1975 struct asd_sas_port *sas_port = device->port;
1976 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1977 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1978 unsigned long flags;
1981 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1987 port = to_hisi_sas_port(sas_port);
1989 /* simply get a slot and send abort command */
1990 rc = hisi_sas_slot_index_alloc(hisi_hba, NULL);
1995 slot = &hisi_hba->slot_info[slot_idx];
1997 spin_lock(&dq->lock);
1998 wr_q_index = dq->wr_point;
1999 dq->wr_point = (dq->wr_point + 1) % HISI_SAS_QUEUE_SLOTS;
2000 list_add_tail(&slot->delivery, &dq->list);
2001 spin_unlock(&dq->lock);
2002 spin_lock(&sas_dev->lock);
2003 list_add_tail(&slot->entry, &sas_dev->list);
2004 spin_unlock(&sas_dev->lock);
2006 dlvry_queue = dq->id;
2007 dlvry_queue_slot = wr_q_index;
2009 slot->device_id = sas_dev->device_id;
2010 slot->n_elem = n_elem;
2011 slot->dlvry_queue = dlvry_queue;
2012 slot->dlvry_queue_slot = dlvry_queue_slot;
2013 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
2014 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
2017 slot->is_internal = true;
2018 task->lldd_task = slot;
2020 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
2021 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
2022 memset(hisi_sas_status_buf_addr_mem(slot), 0,
2023 sizeof(struct hisi_sas_err_record));
2025 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
2026 abort_flag, task_tag);
2028 spin_lock_irqsave(&task->task_state_lock, flags);
2029 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
2030 spin_unlock_irqrestore(&task->task_state_lock, flags);
2031 WRITE_ONCE(slot->ready, 1);
2032 /* send abort command to the chip */
2033 spin_lock(&dq->lock);
2034 hisi_hba->hw->start_delivery(dq);
2035 spin_unlock(&dq->lock);
2040 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
2046 * _hisi_sas_internal_task_abort -- execute an internal
2047 * abort command for single IO command or a device
2048 * @hisi_hba: host controller struct
2049 * @device: domain device
2050 * @abort_flag: mode of operation, device or single IO
2051 * @tag: tag of IO to be aborted (only relevant to single
2053 * @dq: delivery queue for this internal abort command
2056 _hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2057 struct domain_device *device, int abort_flag,
2058 int tag, struct hisi_sas_dq *dq)
2060 struct sas_task *task;
2061 struct hisi_sas_device *sas_dev = device->lldd_dev;
2062 struct device *dev = hisi_hba->dev;
2066 * The interface is not realized means this HW don't support internal
2067 * abort, or don't need to do internal abort. Then here, we return
2068 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
2069 * the internal abort has been executed and returned CQ.
2071 if (!hisi_hba->hw->prep_abort)
2072 return TMF_RESP_FUNC_FAILED;
2074 task = sas_alloc_slow_task(GFP_KERNEL);
2079 task->task_proto = device->tproto;
2080 task->task_done = hisi_sas_task_done;
2081 task->slow_task->timer.function = hisi_sas_tmf_timedout;
2082 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT * HZ;
2083 add_timer(&task->slow_task->timer);
2085 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
2086 task, abort_flag, tag, dq);
2088 del_timer(&task->slow_task->timer);
2089 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
2093 wait_for_completion(&task->slow_task->completion);
2094 res = TMF_RESP_FUNC_FAILED;
2096 /* Internal abort timed out */
2097 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
2098 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct[0].itct)
2099 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
2101 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
2102 struct hisi_sas_slot *slot = task->lldd_task;
2105 struct hisi_sas_cq *cq =
2106 &hisi_hba->cq[slot->dlvry_queue];
2108 * sync irq to avoid free'ing task
2109 * before using task in IO completion
2111 synchronize_irq(cq->irq_no);
2114 dev_err(dev, "internal task abort: timeout and not done.\n");
2119 dev_err(dev, "internal task abort: timeout.\n");
2122 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2123 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
2124 res = TMF_RESP_FUNC_COMPLETE;
2128 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2129 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
2130 res = TMF_RESP_FUNC_SUCC;
2135 dev_dbg(dev, "internal task abort: task to dev %016llx task=%pK resp: 0x%x sts 0x%x\n",
2136 SAS_ADDR(device->sas_addr), task,
2137 task->task_status.resp, /* 0 is complete, -1 is undelivered */
2138 task->task_status.stat);
2139 sas_free_task(task);
2145 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2146 struct domain_device *device,
2147 int abort_flag, int tag)
2149 struct hisi_sas_slot *slot;
2150 struct device *dev = hisi_hba->dev;
2151 struct hisi_sas_dq *dq;
2154 switch (abort_flag) {
2155 case HISI_SAS_INT_ABT_CMD:
2156 slot = &hisi_hba->slot_info[tag];
2157 dq = &hisi_hba->dq[slot->dlvry_queue];
2158 return _hisi_sas_internal_task_abort(hisi_hba, device,
2159 abort_flag, tag, dq);
2160 case HISI_SAS_INT_ABT_DEV:
2161 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2162 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2163 const struct cpumask *mask = cq->irq_mask;
2165 if (mask && !cpumask_intersects(cpu_online_mask, mask))
2167 dq = &hisi_hba->dq[i];
2168 rc = _hisi_sas_internal_task_abort(hisi_hba, device,
2176 dev_err(dev, "Unrecognised internal abort flag (%d)\n",
2184 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
2186 hisi_sas_port_notify_formed(sas_phy);
2189 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
2190 u8 reg_index, u8 reg_count, u8 *write_data)
2192 struct hisi_hba *hisi_hba = sha->lldd_ha;
2194 if (!hisi_hba->hw->write_gpio)
2197 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
2198 reg_index, reg_count, write_data);
2201 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
2203 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2204 struct sas_phy *sphy = sas_phy->phy;
2205 unsigned long flags;
2207 phy->phy_attached = 0;
2211 spin_lock_irqsave(&phy->lock, flags);
2213 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
2215 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
2216 spin_unlock_irqrestore(&phy->lock, flags);
2219 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy,
2222 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2223 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2224 struct device *dev = hisi_hba->dev;
2227 /* Phy down but ready */
2228 hisi_sas_bytes_dmaed(hisi_hba, phy_no, gfp_flags);
2229 hisi_sas_port_notify_formed(sas_phy);
2231 struct hisi_sas_port *port = phy->port;
2233 if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
2235 dev_info(dev, "ignore flutter phy%d down\n", phy_no);
2238 /* Phy down and not ready */
2239 sas_notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL, gfp_flags);
2240 sas_phy_disconnected(sas_phy);
2243 if (phy->phy_type & PORT_TYPE_SAS) {
2244 int port_id = port->id;
2246 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2248 port->port_attached = 0;
2249 } else if (phy->phy_type & PORT_TYPE_SATA)
2250 port->port_attached = 0;
2252 hisi_sas_phy_disconnected(phy);
2255 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2257 void hisi_sas_sync_irqs(struct hisi_hba *hisi_hba)
2261 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2262 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2264 synchronize_irq(cq->irq_no);
2267 EXPORT_SYMBOL_GPL(hisi_sas_sync_irqs);
2269 int hisi_sas_host_reset(struct Scsi_Host *shost, int reset_type)
2271 struct hisi_hba *hisi_hba = shost_priv(shost);
2273 if (reset_type != SCSI_ADAPTER_RESET)
2276 queue_work(hisi_hba->wq, &hisi_hba->rst_work);
2280 EXPORT_SYMBOL_GPL(hisi_sas_host_reset);
2282 struct scsi_transport_template *hisi_sas_stt;
2283 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2285 static struct sas_domain_function_template hisi_sas_transport_ops = {
2286 .lldd_dev_found = hisi_sas_dev_found,
2287 .lldd_dev_gone = hisi_sas_dev_gone,
2288 .lldd_execute_task = hisi_sas_queue_command,
2289 .lldd_control_phy = hisi_sas_control_phy,
2290 .lldd_abort_task = hisi_sas_abort_task,
2291 .lldd_abort_task_set = hisi_sas_abort_task_set,
2292 .lldd_clear_aca = hisi_sas_clear_aca,
2293 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
2294 .lldd_lu_reset = hisi_sas_lu_reset,
2295 .lldd_query_task = hisi_sas_query_task,
2296 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
2297 .lldd_port_formed = hisi_sas_port_formed,
2298 .lldd_write_gpio = hisi_sas_write_gpio,
2301 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2303 int i, s, j, max_command_entries = HISI_SAS_MAX_COMMANDS;
2304 struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
2306 for (i = 0; i < hisi_hba->queue_count; i++) {
2307 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2308 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2309 struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
2311 s = sizeof(struct hisi_sas_cmd_hdr);
2312 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2313 memset(&cmd_hdr[j], 0, s);
2317 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2318 memset(hisi_hba->complete_hdr[i], 0, s);
2322 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2323 memset(hisi_hba->initial_fis, 0, s);
2325 s = max_command_entries * sizeof(struct hisi_sas_iost);
2326 memset(hisi_hba->iost, 0, s);
2328 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2329 memset(hisi_hba->breakpoint, 0, s);
2331 s = sizeof(struct hisi_sas_sata_breakpoint);
2332 for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
2333 memset(&sata_breakpoint[j], 0, s);
2335 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2337 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2339 struct device *dev = hisi_hba->dev;
2340 int i, j, s, max_command_entries = HISI_SAS_MAX_COMMANDS;
2341 int max_command_entries_ru, sz_slot_buf_ru;
2342 int blk_cnt, slots_per_blk;
2344 sema_init(&hisi_hba->sem, 1);
2345 spin_lock_init(&hisi_hba->lock);
2346 for (i = 0; i < hisi_hba->n_phy; i++) {
2347 hisi_sas_phy_init(hisi_hba, i);
2348 hisi_hba->port[i].port_attached = 0;
2349 hisi_hba->port[i].id = -1;
2352 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2353 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2354 hisi_hba->devices[i].device_id = i;
2355 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
2358 for (i = 0; i < hisi_hba->queue_count; i++) {
2359 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2360 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2362 /* Completion queue structure */
2364 cq->hisi_hba = hisi_hba;
2366 /* Delivery queue structure */
2367 spin_lock_init(&dq->lock);
2368 INIT_LIST_HEAD(&dq->list);
2370 dq->hisi_hba = hisi_hba;
2372 /* Delivery queue */
2373 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2374 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2375 &hisi_hba->cmd_hdr_dma[i],
2377 if (!hisi_hba->cmd_hdr[i])
2380 /* Completion queue */
2381 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2382 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2383 &hisi_hba->complete_hdr_dma[i],
2385 if (!hisi_hba->complete_hdr[i])
2389 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2390 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2392 if (!hisi_hba->itct)
2395 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2396 sizeof(struct hisi_sas_slot),
2398 if (!hisi_hba->slot_info)
2401 /* roundup to avoid overly large block size */
2402 max_command_entries_ru = roundup(max_command_entries, 64);
2403 if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
2404 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
2406 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2407 sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2408 s = max(lcm(max_command_entries_ru, sz_slot_buf_ru), PAGE_SIZE);
2409 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2410 slots_per_blk = s / sz_slot_buf_ru;
2412 for (i = 0; i < blk_cnt; i++) {
2413 int slot_index = i * slots_per_blk;
2417 buf = dmam_alloc_coherent(dev, s, &buf_dma,
2422 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2423 struct hisi_sas_slot *slot;
2425 slot = &hisi_hba->slot_info[slot_index];
2427 slot->buf_dma = buf_dma;
2428 slot->idx = slot_index;
2430 buf += sz_slot_buf_ru;
2431 buf_dma += sz_slot_buf_ru;
2435 s = max_command_entries * sizeof(struct hisi_sas_iost);
2436 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2438 if (!hisi_hba->iost)
2441 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2442 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2443 &hisi_hba->breakpoint_dma,
2445 if (!hisi_hba->breakpoint)
2448 hisi_hba->slot_index_count = max_command_entries;
2449 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2450 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2451 if (!hisi_hba->slot_index_tags)
2454 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2455 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2456 &hisi_hba->initial_fis_dma,
2458 if (!hisi_hba->initial_fis)
2461 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2462 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2463 &hisi_hba->sata_breakpoint_dma,
2465 if (!hisi_hba->sata_breakpoint)
2468 hisi_sas_slot_index_init(hisi_hba);
2469 hisi_hba->last_slot_index = HISI_SAS_UNRESERVED_IPTT;
2471 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2472 if (!hisi_hba->wq) {
2473 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2481 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2483 void hisi_sas_free(struct hisi_hba *hisi_hba)
2487 for (i = 0; i < hisi_hba->n_phy; i++) {
2488 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
2490 del_timer_sync(&phy->timer);
2494 destroy_workqueue(hisi_hba->wq);
2496 EXPORT_SYMBOL_GPL(hisi_sas_free);
2498 void hisi_sas_rst_work_handler(struct work_struct *work)
2500 struct hisi_hba *hisi_hba =
2501 container_of(work, struct hisi_hba, rst_work);
2503 if (hisi_sas_controller_prereset(hisi_hba))
2506 hisi_sas_controller_reset(hisi_hba);
2508 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2510 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2512 struct hisi_sas_rst *rst =
2513 container_of(work, struct hisi_sas_rst, work);
2515 if (hisi_sas_controller_prereset(rst->hisi_hba))
2518 if (!hisi_sas_controller_reset(rst->hisi_hba))
2521 complete(rst->completion);
2523 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2525 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2527 struct device *dev = hisi_hba->dev;
2528 struct platform_device *pdev = hisi_hba->platform_dev;
2529 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2532 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2534 dev_err(dev, "could not get property sas-addr\n");
2540 * These properties are only required for platform device-based
2541 * controller with DT firmware.
2543 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2544 "hisilicon,sas-syscon");
2545 if (IS_ERR(hisi_hba->ctrl)) {
2546 dev_err(dev, "could not get syscon\n");
2550 if (device_property_read_u32(dev, "ctrl-reset-reg",
2551 &hisi_hba->ctrl_reset_reg)) {
2552 dev_err(dev, "could not get property ctrl-reset-reg\n");
2556 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2557 &hisi_hba->ctrl_reset_sts_reg)) {
2558 dev_err(dev, "could not get property ctrl-reset-sts-reg\n");
2562 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2563 &hisi_hba->ctrl_clock_ena_reg)) {
2564 dev_err(dev, "could not get property ctrl-clock-ena-reg\n");
2569 refclk = devm_clk_get(dev, NULL);
2571 dev_dbg(dev, "no ref clk property\n");
2573 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2575 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2576 dev_err(dev, "could not get property phy-count\n");
2580 if (device_property_read_u32(dev, "queue-count",
2581 &hisi_hba->queue_count)) {
2582 dev_err(dev, "could not get property queue-count\n");
2588 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2590 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2591 const struct hisi_sas_hw *hw)
2593 struct resource *res;
2594 struct Scsi_Host *shost;
2595 struct hisi_hba *hisi_hba;
2596 struct device *dev = &pdev->dev;
2599 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2601 dev_err(dev, "scsi host alloc failed\n");
2604 hisi_hba = shost_priv(shost);
2606 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2608 hisi_hba->dev = dev;
2609 hisi_hba->platform_dev = pdev;
2610 hisi_hba->shost = shost;
2611 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2613 timer_setup(&hisi_hba->timer, NULL, 0);
2615 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2618 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2620 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2623 dev_err(dev, "No usable DMA addressing method\n");
2627 hisi_hba->regs = devm_platform_ioremap_resource(pdev, 0);
2628 if (IS_ERR(hisi_hba->regs))
2631 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2633 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2634 if (IS_ERR(hisi_hba->sgpio_regs))
2638 if (hisi_sas_alloc(hisi_hba)) {
2639 hisi_sas_free(hisi_hba);
2645 scsi_host_put(shost);
2646 dev_err(dev, "shost alloc failed\n");
2650 static int hisi_sas_interrupt_preinit(struct hisi_hba *hisi_hba)
2652 if (hisi_hba->hw->interrupt_preinit)
2653 return hisi_hba->hw->interrupt_preinit(hisi_hba);
2657 int hisi_sas_probe(struct platform_device *pdev,
2658 const struct hisi_sas_hw *hw)
2660 struct Scsi_Host *shost;
2661 struct hisi_hba *hisi_hba;
2662 struct device *dev = &pdev->dev;
2663 struct asd_sas_phy **arr_phy;
2664 struct asd_sas_port **arr_port;
2665 struct sas_ha_struct *sha;
2666 int rc, phy_nr, port_nr, i;
2668 shost = hisi_sas_shost_alloc(pdev, hw);
2672 sha = SHOST_TO_SAS_HA(shost);
2673 hisi_hba = shost_priv(shost);
2674 platform_set_drvdata(pdev, sha);
2676 phy_nr = port_nr = hisi_hba->n_phy;
2678 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2679 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2680 if (!arr_phy || !arr_port) {
2685 sha->sas_phy = arr_phy;
2686 sha->sas_port = arr_port;
2687 sha->lldd_ha = hisi_hba;
2689 shost->transportt = hisi_sas_stt;
2690 shost->max_id = HISI_SAS_MAX_DEVICES;
2691 shost->max_lun = ~0;
2692 shost->max_channel = 1;
2693 shost->max_cmd_len = 16;
2694 if (hisi_hba->hw->slot_index_alloc) {
2695 shost->can_queue = HISI_SAS_MAX_COMMANDS;
2696 shost->cmd_per_lun = HISI_SAS_MAX_COMMANDS;
2698 shost->can_queue = HISI_SAS_UNRESERVED_IPTT;
2699 shost->cmd_per_lun = HISI_SAS_UNRESERVED_IPTT;
2702 sha->sas_ha_name = DRV_NAME;
2703 sha->dev = hisi_hba->dev;
2704 sha->lldd_module = THIS_MODULE;
2705 sha->sas_addr = &hisi_hba->sas_addr[0];
2706 sha->num_phys = hisi_hba->n_phy;
2707 sha->core.shost = hisi_hba->shost;
2709 for (i = 0; i < hisi_hba->n_phy; i++) {
2710 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2711 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2714 rc = hisi_sas_interrupt_preinit(hisi_hba);
2718 rc = scsi_add_host(shost, &pdev->dev);
2722 rc = sas_register_ha(sha);
2724 goto err_out_register_ha;
2726 rc = hisi_hba->hw->hw_init(hisi_hba);
2728 goto err_out_hw_init;
2730 scsi_scan_host(shost);
2735 sas_unregister_ha(sha);
2736 err_out_register_ha:
2737 scsi_remove_host(shost);
2739 hisi_sas_free(hisi_hba);
2740 scsi_host_put(shost);
2743 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2745 int hisi_sas_remove(struct platform_device *pdev)
2747 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2748 struct hisi_hba *hisi_hba = sha->lldd_ha;
2749 struct Scsi_Host *shost = sha->core.shost;
2751 if (timer_pending(&hisi_hba->timer))
2752 del_timer(&hisi_hba->timer);
2754 sas_unregister_ha(sha);
2755 sas_remove_host(sha->core.shost);
2757 hisi_sas_free(hisi_hba);
2758 scsi_host_put(shost);
2761 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2763 #if IS_ENABLED(CONFIG_SCSI_HISI_SAS_DEBUGFS_DEFAULT_ENABLE)
2764 #define DEBUGFS_ENABLE_DEFAULT "enabled"
2765 bool hisi_sas_debugfs_enable = true;
2766 u32 hisi_sas_debugfs_dump_count = 50;
2768 #define DEBUGFS_ENABLE_DEFAULT "disabled"
2769 bool hisi_sas_debugfs_enable;
2770 u32 hisi_sas_debugfs_dump_count = 1;
2773 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
2774 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
2775 MODULE_PARM_DESC(hisi_sas_debugfs_enable,
2776 "Enable driver debugfs (default "DEBUGFS_ENABLE_DEFAULT")");
2778 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dump_count);
2779 module_param_named(debugfs_dump_count, hisi_sas_debugfs_dump_count, uint, 0444);
2780 MODULE_PARM_DESC(hisi_sas_debugfs_dump_count, "Number of debugfs dumps to allow");
2782 struct dentry *hisi_sas_debugfs_dir;
2783 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_dir);
2785 static __init int hisi_sas_init(void)
2787 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2791 if (hisi_sas_debugfs_enable) {
2792 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
2793 if (hisi_sas_debugfs_dump_count > HISI_SAS_MAX_DEBUGFS_DUMP) {
2794 pr_info("hisi_sas: Limiting debugfs dump count\n");
2795 hisi_sas_debugfs_dump_count = HISI_SAS_MAX_DEBUGFS_DUMP;
2802 static __exit void hisi_sas_exit(void)
2804 sas_release_transport(hisi_sas_stt);
2806 debugfs_remove(hisi_sas_debugfs_dir);
2809 module_init(hisi_sas_init);
2810 module_exit(hisi_sas_exit);
2812 MODULE_LICENSE("GPL");
2813 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2814 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2815 MODULE_ALIAS("platform:" DRV_NAME);