2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
13 #include "../libsas/sas_internal.h"
14 #define DRV_NAME "hisi_sas"
16 #define DEV_IS_GONE(dev) \
17 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
19 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
20 u8 *lun, struct hisi_sas_tmf_task *tmf);
22 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
23 struct domain_device *device,
24 int abort_flag, int tag);
25 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
26 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
28 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
29 struct domain_device *device);
30 static void hisi_sas_dev_gone(struct domain_device *device);
32 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
34 switch (fis->command) {
35 case ATA_CMD_FPDMA_WRITE:
36 case ATA_CMD_FPDMA_READ:
37 case ATA_CMD_FPDMA_RECV:
38 case ATA_CMD_FPDMA_SEND:
39 case ATA_CMD_NCQ_NON_DATA:
40 return HISI_SAS_SATA_PROTOCOL_FPDMA;
42 case ATA_CMD_DOWNLOAD_MICRO:
44 case ATA_CMD_PMP_READ:
45 case ATA_CMD_READ_LOG_EXT:
46 case ATA_CMD_PIO_READ:
47 case ATA_CMD_PIO_READ_EXT:
48 case ATA_CMD_PMP_WRITE:
49 case ATA_CMD_WRITE_LOG_EXT:
50 case ATA_CMD_PIO_WRITE:
51 case ATA_CMD_PIO_WRITE_EXT:
52 return HISI_SAS_SATA_PROTOCOL_PIO;
55 case ATA_CMD_DOWNLOAD_MICRO_DMA:
56 case ATA_CMD_PMP_READ_DMA:
57 case ATA_CMD_PMP_WRITE_DMA:
59 case ATA_CMD_READ_EXT:
60 case ATA_CMD_READ_LOG_DMA_EXT:
61 case ATA_CMD_READ_STREAM_DMA_EXT:
62 case ATA_CMD_TRUSTED_RCV_DMA:
63 case ATA_CMD_TRUSTED_SND_DMA:
65 case ATA_CMD_WRITE_EXT:
66 case ATA_CMD_WRITE_FUA_EXT:
67 case ATA_CMD_WRITE_QUEUED:
68 case ATA_CMD_WRITE_LOG_DMA_EXT:
69 case ATA_CMD_WRITE_STREAM_DMA_EXT:
70 case ATA_CMD_ZAC_MGMT_IN:
71 return HISI_SAS_SATA_PROTOCOL_DMA;
73 case ATA_CMD_CHK_POWER:
74 case ATA_CMD_DEV_RESET:
77 case ATA_CMD_FLUSH_EXT:
79 case ATA_CMD_VERIFY_EXT:
80 case ATA_CMD_SET_FEATURES:
82 case ATA_CMD_STANDBYNOW1:
83 case ATA_CMD_ZAC_MGMT_OUT:
84 return HISI_SAS_SATA_PROTOCOL_NONDATA;
87 switch (fis->features) {
88 case ATA_SET_MAX_PASSWD:
89 case ATA_SET_MAX_LOCK:
90 return HISI_SAS_SATA_PROTOCOL_PIO;
92 case ATA_SET_MAX_PASSWD_DMA:
93 case ATA_SET_MAX_UNLOCK_DMA:
94 return HISI_SAS_SATA_PROTOCOL_DMA;
97 return HISI_SAS_SATA_PROTOCOL_NONDATA;
102 if (direction == DMA_NONE)
103 return HISI_SAS_SATA_PROTOCOL_NONDATA;
104 return HISI_SAS_SATA_PROTOCOL_PIO;
108 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
110 void hisi_sas_sata_done(struct sas_task *task,
111 struct hisi_sas_slot *slot)
113 struct task_status_struct *ts = &task->task_status;
114 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
115 struct hisi_sas_status_buffer *status_buf =
116 hisi_sas_status_buf_addr_mem(slot);
117 u8 *iu = &status_buf->iu[0];
118 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
120 resp->frame_len = sizeof(struct dev_to_host_fis);
121 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
123 ts->buf_valid_size = sizeof(*resp);
125 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
127 int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
129 struct ata_queued_cmd *qc = task->uldd_task;
132 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
133 qc->tf.command == ATA_CMD_FPDMA_READ) {
140 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
143 * This function assumes linkrate mask fits in 8 bits, which it
144 * does for all HW versions supported.
146 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
151 max -= SAS_LINK_RATE_1_5_GBPS;
152 for (i = 0; i <= max; i++)
153 rate |= 1 << (i * 2);
156 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
158 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
160 return device->port->ha->lldd_ha;
163 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
165 return container_of(sas_port, struct hisi_sas_port, sas_port);
167 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
169 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
173 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
174 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
176 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
178 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
180 void *bitmap = hisi_hba->slot_index_tags;
182 clear_bit(slot_idx, bitmap);
185 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
189 if (hisi_hba->hw->slot_index_alloc || (slot_idx >=
190 hisi_hba->hw->max_command_entries - HISI_SAS_RESERVED_IPTT_CNT)) {
191 spin_lock_irqsave(&hisi_hba->lock, flags);
192 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
193 spin_unlock_irqrestore(&hisi_hba->lock, flags);
197 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
199 void *bitmap = hisi_hba->slot_index_tags;
201 set_bit(slot_idx, bitmap);
204 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba,
205 struct scsi_cmnd *scsi_cmnd)
208 void *bitmap = hisi_hba->slot_index_tags;
212 return scsi_cmnd->request->tag;
214 spin_lock_irqsave(&hisi_hba->lock, flags);
215 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
216 hisi_hba->last_slot_index + 1);
217 if (index >= hisi_hba->slot_index_count) {
218 index = find_next_zero_bit(bitmap,
219 hisi_hba->slot_index_count,
220 hisi_hba->hw->max_command_entries -
221 HISI_SAS_RESERVED_IPTT_CNT);
222 if (index >= hisi_hba->slot_index_count) {
223 spin_unlock_irqrestore(&hisi_hba->lock, flags);
224 return -SAS_QUEUE_FULL;
227 hisi_sas_slot_index_set(hisi_hba, index);
228 hisi_hba->last_slot_index = index;
229 spin_unlock_irqrestore(&hisi_hba->lock, flags);
234 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
238 for (i = 0; i < hisi_hba->slot_index_count; ++i)
239 hisi_sas_slot_index_clear(hisi_hba, i);
242 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
243 struct hisi_sas_slot *slot)
246 int device_id = slot->device_id;
247 struct hisi_sas_device *sas_dev = &hisi_hba->devices[device_id];
250 struct device *dev = hisi_hba->dev;
252 if (!task->lldd_task)
255 task->lldd_task = NULL;
257 if (!sas_protocol_ata(task->task_proto)) {
258 struct sas_ssp_task *ssp_task = &task->ssp_task;
259 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
262 dma_unmap_sg(dev, task->scatter,
265 if (slot->n_elem_dif)
266 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
267 scsi_prot_sg_count(scsi_cmnd),
272 spin_lock_irqsave(&sas_dev->lock, flags);
273 list_del_init(&slot->entry);
274 spin_unlock_irqrestore(&sas_dev->lock, flags);
276 memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
278 hisi_sas_slot_index_free(hisi_hba, slot->idx);
280 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
282 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
283 struct hisi_sas_slot *slot)
285 hisi_hba->hw->prep_smp(hisi_hba, slot);
288 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
289 struct hisi_sas_slot *slot)
291 hisi_hba->hw->prep_ssp(hisi_hba, slot);
294 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
295 struct hisi_sas_slot *slot)
297 hisi_hba->hw->prep_stp(hisi_hba, slot);
300 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
301 struct hisi_sas_slot *slot,
302 int device_id, int abort_flag, int tag_to_abort)
304 hisi_hba->hw->prep_abort(hisi_hba, slot,
305 device_id, abort_flag, tag_to_abort);
308 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
309 struct sas_task *task, int n_elem,
310 int n_elem_req, int n_elem_resp)
312 struct device *dev = hisi_hba->dev;
314 if (!sas_protocol_ata(task->task_proto)) {
315 if (task->num_scatter) {
317 dma_unmap_sg(dev, task->scatter,
320 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
322 dma_unmap_sg(dev, &task->smp_task.smp_req,
325 dma_unmap_sg(dev, &task->smp_task.smp_resp,
331 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
332 struct sas_task *task, int *n_elem,
333 int *n_elem_req, int *n_elem_resp)
335 struct device *dev = hisi_hba->dev;
338 if (sas_protocol_ata(task->task_proto)) {
339 *n_elem = task->num_scatter;
341 unsigned int req_len, resp_len;
343 if (task->num_scatter) {
344 *n_elem = dma_map_sg(dev, task->scatter,
345 task->num_scatter, task->data_dir);
350 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
351 *n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
357 req_len = sg_dma_len(&task->smp_task.smp_req);
360 goto err_out_dma_unmap;
362 *n_elem_resp = dma_map_sg(dev, &task->smp_task.smp_resp,
366 goto err_out_dma_unmap;
368 resp_len = sg_dma_len(&task->smp_task.smp_resp);
369 if (resp_len & 0x3) {
371 goto err_out_dma_unmap;
376 if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
377 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
380 goto err_out_dma_unmap;
385 /* It would be better to call dma_unmap_sg() here, but it's messy */
386 hisi_sas_dma_unmap(hisi_hba, task, *n_elem,
387 *n_elem_req, *n_elem_resp);
392 static void hisi_sas_dif_dma_unmap(struct hisi_hba *hisi_hba,
393 struct sas_task *task, int n_elem_dif)
395 struct device *dev = hisi_hba->dev;
398 struct sas_ssp_task *ssp_task = &task->ssp_task;
399 struct scsi_cmnd *scsi_cmnd = ssp_task->cmd;
401 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
402 scsi_prot_sg_count(scsi_cmnd),
407 static int hisi_sas_dif_dma_map(struct hisi_hba *hisi_hba,
408 int *n_elem_dif, struct sas_task *task)
410 struct device *dev = hisi_hba->dev;
411 struct sas_ssp_task *ssp_task;
412 struct scsi_cmnd *scsi_cmnd;
415 if (task->num_scatter) {
416 ssp_task = &task->ssp_task;
417 scsi_cmnd = ssp_task->cmd;
419 if (scsi_prot_sg_count(scsi_cmnd)) {
420 *n_elem_dif = dma_map_sg(dev,
421 scsi_prot_sglist(scsi_cmnd),
422 scsi_prot_sg_count(scsi_cmnd),
428 if (*n_elem_dif > HISI_SAS_SGE_DIF_PAGE_CNT) {
429 dev_err(dev, "task prep: n_elem_dif(%d) too large\n",
432 goto err_out_dif_dma_unmap;
439 err_out_dif_dma_unmap:
440 dma_unmap_sg(dev, scsi_prot_sglist(scsi_cmnd),
441 scsi_prot_sg_count(scsi_cmnd), task->data_dir);
445 static int hisi_sas_task_prep(struct sas_task *task,
446 struct hisi_sas_dq **dq_pointer,
447 bool is_tmf, struct hisi_sas_tmf_task *tmf,
450 struct domain_device *device = task->dev;
451 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
452 struct hisi_sas_device *sas_dev = device->lldd_dev;
453 struct hisi_sas_port *port;
454 struct hisi_sas_slot *slot;
455 struct hisi_sas_cmd_hdr *cmd_hdr_base;
456 struct asd_sas_port *sas_port = device->port;
457 struct device *dev = hisi_hba->dev;
458 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
459 int n_elem = 0, n_elem_dif = 0, n_elem_req = 0, n_elem_resp = 0;
460 struct hisi_sas_dq *dq;
464 if (DEV_IS_GONE(sas_dev)) {
466 dev_info(dev, "task prep: device %d not ready\n",
469 dev_info(dev, "task prep: device %016llx not ready\n",
470 SAS_ADDR(device->sas_addr));
475 if (hisi_hba->reply_map) {
476 int cpu = raw_smp_processor_id();
477 unsigned int dq_index = hisi_hba->reply_map[cpu];
479 *dq_pointer = dq = &hisi_hba->dq[dq_index];
481 *dq_pointer = dq = sas_dev->dq;
484 port = to_hisi_sas_port(sas_port);
485 if (port && !port->port_attached) {
486 dev_info(dev, "task prep: %s port%d not attach device\n",
487 (dev_is_sata(device)) ?
494 rc = hisi_sas_dma_map(hisi_hba, task, &n_elem,
495 &n_elem_req, &n_elem_resp);
499 if (!sas_protocol_ata(task->task_proto)) {
500 rc = hisi_sas_dif_dma_map(hisi_hba, &n_elem_dif, task);
502 goto err_out_dma_unmap;
505 if (hisi_hba->hw->slot_index_alloc)
506 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
508 struct scsi_cmnd *scsi_cmnd = NULL;
510 if (task->uldd_task) {
511 struct ata_queued_cmd *qc;
513 if (dev_is_sata(device)) {
514 qc = task->uldd_task;
515 scsi_cmnd = qc->scsicmd;
517 scsi_cmnd = task->uldd_task;
520 rc = hisi_sas_slot_index_alloc(hisi_hba, scsi_cmnd);
523 goto err_out_dif_dma_unmap;
526 slot = &hisi_hba->slot_info[slot_idx];
528 spin_lock_irqsave(&dq->lock, flags);
529 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
530 if (wr_q_index < 0) {
531 spin_unlock_irqrestore(&dq->lock, flags);
536 list_add_tail(&slot->delivery, &dq->list);
537 spin_unlock_irqrestore(&dq->lock, flags);
538 spin_lock_irqsave(&sas_dev->lock, flags);
539 list_add_tail(&slot->entry, &sas_dev->list);
540 spin_unlock_irqrestore(&sas_dev->lock, flags);
542 dlvry_queue = dq->id;
543 dlvry_queue_slot = wr_q_index;
545 slot->device_id = sas_dev->device_id;
546 slot->n_elem = n_elem;
547 slot->n_elem_dif = n_elem_dif;
548 slot->dlvry_queue = dlvry_queue;
549 slot->dlvry_queue_slot = dlvry_queue_slot;
550 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
551 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
555 slot->is_internal = is_tmf;
556 task->lldd_task = slot;
558 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
559 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
560 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
562 switch (task->task_proto) {
563 case SAS_PROTOCOL_SMP:
564 hisi_sas_task_prep_smp(hisi_hba, slot);
566 case SAS_PROTOCOL_SSP:
567 hisi_sas_task_prep_ssp(hisi_hba, slot);
569 case SAS_PROTOCOL_SATA:
570 case SAS_PROTOCOL_STP:
571 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
572 hisi_sas_task_prep_ata(hisi_hba, slot);
575 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
580 spin_lock_irqsave(&task->task_state_lock, flags);
581 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
582 spin_unlock_irqrestore(&task->task_state_lock, flags);
585 WRITE_ONCE(slot->ready, 1);
590 hisi_sas_slot_index_free(hisi_hba, slot_idx);
591 err_out_dif_dma_unmap:
592 if (!sas_protocol_ata(task->task_proto))
593 hisi_sas_dif_dma_unmap(hisi_hba, task, n_elem_dif);
595 hisi_sas_dma_unmap(hisi_hba, task, n_elem,
596 n_elem_req, n_elem_resp);
598 dev_err(dev, "task prep: failed[%d]!\n", rc);
602 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
603 bool is_tmf, struct hisi_sas_tmf_task *tmf)
608 struct hisi_hba *hisi_hba;
610 struct domain_device *device = task->dev;
611 struct asd_sas_port *sas_port = device->port;
612 struct hisi_sas_dq *dq = NULL;
615 struct task_status_struct *ts = &task->task_status;
617 ts->resp = SAS_TASK_UNDELIVERED;
618 ts->stat = SAS_PHY_DOWN;
620 * libsas will use dev->port, should
621 * not call task_done for sata
623 if (device->dev_type != SAS_SATA_DEV)
624 task->task_done(task);
628 hisi_hba = dev_to_hisi_hba(device);
631 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
635 down(&hisi_hba->sem);
639 /* protect task_prep and start_delivery sequence */
640 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
642 dev_err(dev, "task exec: failed[%d]!\n", rc);
645 spin_lock_irqsave(&dq->lock, flags);
646 hisi_hba->hw->start_delivery(dq);
647 spin_unlock_irqrestore(&dq->lock, flags);
653 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
655 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
656 struct asd_sas_phy *sas_phy = &phy->sas_phy;
657 struct sas_ha_struct *sas_ha;
659 if (!phy->phy_attached)
662 sas_ha = &hisi_hba->sha;
663 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
666 struct sas_phy *sphy = sas_phy->phy;
668 sphy->negotiated_linkrate = sas_phy->linkrate;
669 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
670 sphy->maximum_linkrate_hw =
671 hisi_hba->hw->phy_get_max_linkrate();
672 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
673 sphy->minimum_linkrate = phy->minimum_linkrate;
675 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
676 sphy->maximum_linkrate = phy->maximum_linkrate;
679 if (phy->phy_type & PORT_TYPE_SAS) {
680 struct sas_identify_frame *id;
682 id = (struct sas_identify_frame *)phy->frame_rcvd;
683 id->dev_type = phy->identify.device_type;
684 id->initiator_bits = SAS_PROTOCOL_ALL;
685 id->target_bits = phy->identify.target_port_protocols;
686 } else if (phy->phy_type & PORT_TYPE_SATA) {
690 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
691 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
694 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
696 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
697 struct hisi_sas_device *sas_dev = NULL;
699 int last = hisi_hba->last_dev_id;
700 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
703 spin_lock_irqsave(&hisi_hba->lock, flags);
704 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
705 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
706 int queue = i % hisi_hba->queue_count;
707 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
709 hisi_hba->devices[i].device_id = i;
710 sas_dev = &hisi_hba->devices[i];
711 sas_dev->dev_status = HISI_SAS_DEV_INIT;
712 sas_dev->dev_type = device->dev_type;
713 sas_dev->hisi_hba = hisi_hba;
714 sas_dev->sas_device = device;
716 spin_lock_init(&sas_dev->lock);
717 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
722 hisi_hba->last_dev_id = i;
723 spin_unlock_irqrestore(&hisi_hba->lock, flags);
728 #define HISI_SAS_SRST_ATA_DISK_CNT 3
729 static int hisi_sas_init_device(struct domain_device *device)
731 int rc = TMF_RESP_FUNC_COMPLETE;
733 struct hisi_sas_tmf_task tmf_task;
734 int retry = HISI_SAS_SRST_ATA_DISK_CNT;
735 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
736 struct device *dev = hisi_hba->dev;
737 struct sas_phy *local_phy;
739 switch (device->dev_type) {
741 int_to_scsilun(0, &lun);
743 tmf_task.tmf = TMF_CLEAR_TASK_SET;
744 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
746 if (rc == TMF_RESP_FUNC_COMPLETE)
747 hisi_sas_release_task(hisi_hba, device);
751 case SAS_SATA_PM_PORT:
752 case SAS_SATA_PENDING:
754 * send HARD RESET to clear previous affiliation of
757 local_phy = sas_get_local_phy(device);
758 if (!scsi_is_sas_phy_local(local_phy)) {
759 unsigned long deadline = ata_deadline(jiffies, 20000);
760 struct sata_device *sata_dev = &device->sata_dev;
761 struct ata_host *ata_host = sata_dev->ata_host;
762 struct ata_port_operations *ops = ata_host->ops;
763 struct ata_port *ap = sata_dev->ap;
764 struct ata_link *link;
765 unsigned int classes;
767 ata_for_each_link(link, ap, EDGE)
768 rc = ops->hardreset(link, &classes,
771 sas_put_local_phy(local_phy);
773 dev_warn(dev, "SATA disk hardreset fail: 0x%x\n",
778 while (retry-- > 0) {
779 rc = hisi_sas_softreset_ata_disk(device);
791 static int hisi_sas_dev_found(struct domain_device *device)
793 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
794 struct domain_device *parent_dev = device->parent;
795 struct hisi_sas_device *sas_dev;
796 struct device *dev = hisi_hba->dev;
799 if (hisi_hba->hw->alloc_dev)
800 sas_dev = hisi_hba->hw->alloc_dev(device);
802 sas_dev = hisi_sas_alloc_dev(device);
804 dev_err(dev, "fail alloc dev: max support %d devices\n",
805 HISI_SAS_MAX_DEVICES);
809 device->lldd_dev = sas_dev;
810 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
812 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
814 u8 phy_num = parent_dev->ex_dev.num_phys;
817 for (phy_no = 0; phy_no < phy_num; phy_no++) {
818 phy = &parent_dev->ex_dev.ex_phy[phy_no];
819 if (SAS_ADDR(phy->attached_sas_addr) ==
820 SAS_ADDR(device->sas_addr))
824 if (phy_no == phy_num) {
825 dev_info(dev, "dev found: no attached "
826 "dev:%016llx at ex:%016llx\n",
827 SAS_ADDR(device->sas_addr),
828 SAS_ADDR(parent_dev->sas_addr));
834 dev_info(dev, "dev[%d:%x] found\n",
835 sas_dev->device_id, sas_dev->dev_type);
837 rc = hisi_sas_init_device(device);
840 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
844 hisi_sas_dev_gone(device);
848 int hisi_sas_slave_configure(struct scsi_device *sdev)
850 struct domain_device *dev = sdev_to_domain_dev(sdev);
851 int ret = sas_slave_configure(sdev);
855 if (!dev_is_sata(dev))
856 sas_change_queue_depth(sdev, 64);
860 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
862 void hisi_sas_scan_start(struct Scsi_Host *shost)
864 struct hisi_hba *hisi_hba = shost_priv(shost);
866 hisi_hba->hw->phys_init(hisi_hba);
868 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
870 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
872 struct hisi_hba *hisi_hba = shost_priv(shost);
873 struct sas_ha_struct *sha = &hisi_hba->sha;
875 /* Wait for PHY up interrupt to occur */
882 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
884 static void hisi_sas_phyup_work(struct work_struct *work)
886 struct hisi_sas_phy *phy =
887 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
888 struct hisi_hba *hisi_hba = phy->hisi_hba;
889 struct asd_sas_phy *sas_phy = &phy->sas_phy;
890 int phy_no = sas_phy->id;
892 if (phy->identify.target_port_protocols == SAS_PROTOCOL_SSP)
893 hisi_hba->hw->sl_notify_ssp(hisi_hba, phy_no);
894 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
897 static void hisi_sas_linkreset_work(struct work_struct *work)
899 struct hisi_sas_phy *phy =
900 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
901 struct asd_sas_phy *sas_phy = &phy->sas_phy;
903 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
906 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
907 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
908 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
911 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
912 enum hisi_sas_phy_event event)
914 struct hisi_hba *hisi_hba = phy->hisi_hba;
916 if (WARN_ON(event >= HISI_PHYES_NUM))
919 return queue_work(hisi_hba->wq, &phy->works[event]);
921 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
923 static void hisi_sas_wait_phyup_timedout(struct timer_list *t)
925 struct hisi_sas_phy *phy = from_timer(phy, t, timer);
926 struct hisi_hba *hisi_hba = phy->hisi_hba;
927 struct device *dev = hisi_hba->dev;
928 int phy_no = phy->sas_phy.id;
930 dev_warn(dev, "phy%d wait phyup timeout, issuing link reset\n", phy_no);
931 hisi_sas_notify_phy_event(phy, HISI_PHYE_LINK_RESET);
934 void hisi_sas_phy_oob_ready(struct hisi_hba *hisi_hba, int phy_no)
936 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
937 struct device *dev = hisi_hba->dev;
939 if (!timer_pending(&phy->timer)) {
940 dev_dbg(dev, "phy%d OOB ready\n", phy_no);
941 phy->timer.expires = jiffies + HISI_SAS_WAIT_PHYUP_TIMEOUT * HZ;
942 add_timer(&phy->timer);
945 EXPORT_SYMBOL_GPL(hisi_sas_phy_oob_ready);
947 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
949 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
950 struct asd_sas_phy *sas_phy = &phy->sas_phy;
953 phy->hisi_hba = hisi_hba;
955 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
956 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
957 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
958 sas_phy->class = SAS;
959 sas_phy->iproto = SAS_PROTOCOL_ALL;
961 sas_phy->type = PHY_TYPE_PHYSICAL;
962 sas_phy->role = PHY_ROLE_INITIATOR;
963 sas_phy->oob_mode = OOB_NOT_CONNECTED;
964 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
965 sas_phy->id = phy_no;
966 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
967 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
968 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
969 sas_phy->lldd_phy = phy;
971 for (i = 0; i < HISI_PHYES_NUM; i++)
972 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
974 spin_lock_init(&phy->lock);
976 timer_setup(&phy->timer, hisi_sas_wait_phyup_timedout, 0);
979 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
981 struct sas_ha_struct *sas_ha = sas_phy->ha;
982 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
983 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
984 struct asd_sas_port *sas_port = sas_phy->port;
985 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
991 spin_lock_irqsave(&hisi_hba->lock, flags);
992 port->port_attached = 1;
993 port->id = phy->port_id;
995 sas_port->lldd_port = port;
996 spin_unlock_irqrestore(&hisi_hba->lock, flags);
999 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
1000 struct hisi_sas_slot *slot)
1003 unsigned long flags;
1004 struct task_status_struct *ts;
1006 ts = &task->task_status;
1008 ts->resp = SAS_TASK_COMPLETE;
1009 ts->stat = SAS_ABORTED_TASK;
1010 spin_lock_irqsave(&task->task_state_lock, flags);
1011 task->task_state_flags &=
1012 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1013 if (!slot->is_internal && task->task_proto != SAS_PROTOCOL_SMP)
1014 task->task_state_flags |= SAS_TASK_STATE_DONE;
1015 spin_unlock_irqrestore(&task->task_state_lock, flags);
1018 hisi_sas_slot_task_free(hisi_hba, task, slot);
1021 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
1022 struct domain_device *device)
1024 struct hisi_sas_slot *slot, *slot2;
1025 struct hisi_sas_device *sas_dev = device->lldd_dev;
1027 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
1028 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
1031 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
1033 struct hisi_sas_device *sas_dev;
1034 struct domain_device *device;
1037 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1038 sas_dev = &hisi_hba->devices[i];
1039 device = sas_dev->sas_device;
1041 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
1045 hisi_sas_release_task(hisi_hba, device);
1048 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
1050 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
1051 struct domain_device *device)
1053 if (hisi_hba->hw->dereg_device)
1054 hisi_hba->hw->dereg_device(hisi_hba, device);
1057 static void hisi_sas_dev_gone(struct domain_device *device)
1059 struct hisi_sas_device *sas_dev = device->lldd_dev;
1060 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1061 struct device *dev = hisi_hba->dev;
1063 dev_info(dev, "dev[%d:%x] is gone\n",
1064 sas_dev->device_id, sas_dev->dev_type);
1066 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
1067 hisi_sas_internal_task_abort(hisi_hba, device,
1068 HISI_SAS_INT_ABT_DEV, 0);
1070 hisi_sas_dereg_device(hisi_hba, device);
1072 down(&hisi_hba->sem);
1073 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
1075 device->lldd_dev = NULL;
1078 if (hisi_hba->hw->free_device)
1079 hisi_hba->hw->free_device(sas_dev);
1080 sas_dev->dev_type = SAS_PHY_UNUSED;
1083 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
1085 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
1088 static int hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
1089 struct sas_phy_linkrates *r)
1091 struct sas_phy_linkrates _r;
1093 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1094 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1095 enum sas_linkrate min, max;
1097 if (r->minimum_linkrate > SAS_LINK_RATE_1_5_GBPS)
1100 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1101 max = sas_phy->phy->maximum_linkrate;
1102 min = r->minimum_linkrate;
1103 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
1104 max = r->maximum_linkrate;
1105 min = sas_phy->phy->minimum_linkrate;
1109 _r.maximum_linkrate = max;
1110 _r.minimum_linkrate = min;
1112 sas_phy->phy->maximum_linkrate = max;
1113 sas_phy->phy->minimum_linkrate = min;
1115 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
1117 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
1118 hisi_hba->hw->phy_start(hisi_hba, phy_no);
1123 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
1126 struct sas_ha_struct *sas_ha = sas_phy->ha;
1127 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1128 int phy_no = sas_phy->id;
1131 case PHY_FUNC_HARD_RESET:
1132 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
1135 case PHY_FUNC_LINK_RESET:
1136 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
1138 hisi_hba->hw->phy_start(hisi_hba, phy_no);
1141 case PHY_FUNC_DISABLE:
1142 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
1145 case PHY_FUNC_SET_LINK_RATE:
1146 return hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1147 case PHY_FUNC_GET_EVENTS:
1148 if (hisi_hba->hw->get_events) {
1149 hisi_hba->hw->get_events(hisi_hba, phy_no);
1153 case PHY_FUNC_RELEASE_SPINUP_HOLD:
1160 static void hisi_sas_task_done(struct sas_task *task)
1162 del_timer(&task->slow_task->timer);
1163 complete(&task->slow_task->completion);
1166 static void hisi_sas_tmf_timedout(struct timer_list *t)
1168 struct sas_task_slow *slow = from_timer(slow, t, timer);
1169 struct sas_task *task = slow->task;
1170 unsigned long flags;
1171 bool is_completed = true;
1173 spin_lock_irqsave(&task->task_state_lock, flags);
1174 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1175 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1176 is_completed = false;
1178 spin_unlock_irqrestore(&task->task_state_lock, flags);
1181 complete(&task->slow_task->completion);
1184 #define TASK_TIMEOUT 20
1185 #define TASK_RETRY 3
1186 #define INTERNAL_ABORT_TIMEOUT 6
1187 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1188 void *parameter, u32 para_len,
1189 struct hisi_sas_tmf_task *tmf)
1191 struct hisi_sas_device *sas_dev = device->lldd_dev;
1192 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1193 struct device *dev = hisi_hba->dev;
1194 struct sas_task *task;
1197 for (retry = 0; retry < TASK_RETRY; retry++) {
1198 task = sas_alloc_slow_task(GFP_KERNEL);
1203 task->task_proto = device->tproto;
1205 if (dev_is_sata(device)) {
1206 task->ata_task.device_control_reg_update = 1;
1207 memcpy(&task->ata_task.fis, parameter, para_len);
1209 memcpy(&task->ssp_task, parameter, para_len);
1211 task->task_done = hisi_sas_task_done;
1213 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1214 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT * HZ;
1215 add_timer(&task->slow_task->timer);
1217 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1220 del_timer(&task->slow_task->timer);
1221 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1226 wait_for_completion(&task->slow_task->completion);
1227 res = TMF_RESP_FUNC_FAILED;
1228 /* Even TMF timed out, return direct. */
1229 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1230 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1231 struct hisi_sas_slot *slot = task->lldd_task;
1233 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1235 struct hisi_sas_cq *cq =
1236 &hisi_hba->cq[slot->dlvry_queue];
1238 * flush tasklet to avoid free'ing task
1239 * before using task in IO completion
1241 tasklet_kill(&cq->tasklet);
1247 dev_err(dev, "abort tmf: TMF task timeout\n");
1250 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1251 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1252 res = TMF_RESP_FUNC_COMPLETE;
1256 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1257 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1258 res = TMF_RESP_FUNC_SUCC;
1262 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1263 task->task_status.stat == SAS_DATA_UNDERRUN) {
1264 /* no error, but return the number of bytes of
1267 dev_warn(dev, "abort tmf: task to dev %016llx "
1268 "resp: 0x%x sts 0x%x underrun\n",
1269 SAS_ADDR(device->sas_addr),
1270 task->task_status.resp,
1271 task->task_status.stat);
1272 res = task->task_status.residual;
1276 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1277 task->task_status.stat == SAS_DATA_OVERRUN) {
1278 dev_warn(dev, "abort tmf: blocked task error\n");
1283 dev_warn(dev, "abort tmf: task to dev "
1284 "%016llx resp: 0x%x status 0x%x\n",
1285 SAS_ADDR(device->sas_addr), task->task_status.resp,
1286 task->task_status.stat);
1287 sas_free_task(task);
1291 if (retry == TASK_RETRY)
1292 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1293 sas_free_task(task);
1297 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1298 bool reset, int pmp, u8 *fis)
1300 struct ata_taskfile tf;
1302 ata_tf_init(dev, &tf);
1306 tf.ctl &= ~ATA_SRST;
1307 tf.command = ATA_CMD_DEV_RESET;
1308 ata_tf_to_fis(&tf, pmp, 0, fis);
1311 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1314 struct ata_port *ap = device->sata_dev.ap;
1315 struct ata_link *link;
1316 int rc = TMF_RESP_FUNC_FAILED;
1317 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1318 struct device *dev = hisi_hba->dev;
1319 int s = sizeof(struct host_to_dev_fis);
1321 ata_for_each_link(link, ap, EDGE) {
1322 int pmp = sata_srst_pmp(link);
1324 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1325 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1326 if (rc != TMF_RESP_FUNC_COMPLETE)
1330 if (rc == TMF_RESP_FUNC_COMPLETE) {
1331 ata_for_each_link(link, ap, EDGE) {
1332 int pmp = sata_srst_pmp(link);
1334 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1335 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1337 if (rc != TMF_RESP_FUNC_COMPLETE)
1338 dev_err(dev, "ata disk de-reset failed\n");
1341 dev_err(dev, "ata disk reset failed\n");
1344 if (rc == TMF_RESP_FUNC_COMPLETE)
1345 hisi_sas_release_task(hisi_hba, device);
1350 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1351 u8 *lun, struct hisi_sas_tmf_task *tmf)
1353 struct sas_ssp_task ssp_task;
1355 if (!(device->tproto & SAS_PROTOCOL_SSP))
1356 return TMF_RESP_FUNC_ESUPP;
1358 memcpy(ssp_task.LUN, lun, 8);
1360 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1361 sizeof(ssp_task), tmf);
1364 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1366 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1369 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1370 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1371 struct domain_device *device = sas_dev->sas_device;
1372 struct asd_sas_port *sas_port;
1373 struct hisi_sas_port *port;
1374 struct hisi_sas_phy *phy = NULL;
1375 struct asd_sas_phy *sas_phy;
1377 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1378 || !device || !device->port)
1381 sas_port = device->port;
1382 port = to_hisi_sas_port(sas_port);
1384 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1385 if (state & BIT(sas_phy->id)) {
1386 phy = sas_phy->lldd_phy;
1391 port->id = phy->port_id;
1393 /* Update linkrate of directly attached device. */
1394 if (!device->parent)
1395 device->linkrate = phy->sas_phy.linkrate;
1397 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1403 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1406 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1407 struct asd_sas_port *_sas_port = NULL;
1410 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1411 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1412 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1413 struct asd_sas_port *sas_port = sas_phy->port;
1414 bool do_port_check = !!(_sas_port != sas_port);
1416 if (!sas_phy->phy->enabled)
1419 /* Report PHY state change to libsas */
1420 if (state & BIT(phy_no)) {
1421 if (do_port_check && sas_port && sas_port->port_dev) {
1422 struct domain_device *dev = sas_port->port_dev;
1424 _sas_port = sas_port;
1426 if (DEV_IS_EXPANDER(dev->dev_type))
1427 sas_ha->notify_port_event(sas_phy,
1428 PORTE_BROADCAST_RCVD);
1430 } else if (old_state & (1 << phy_no))
1431 /* PHY down but was up before */
1432 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1437 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1439 struct hisi_sas_device *sas_dev;
1440 struct domain_device *device;
1443 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1444 sas_dev = &hisi_hba->devices[i];
1445 device = sas_dev->sas_device;
1447 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1450 hisi_sas_init_device(device);
1454 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1455 struct asd_sas_port *sas_port,
1456 struct domain_device *device)
1458 struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1459 struct ata_port *ap = device->sata_dev.ap;
1460 struct device *dev = hisi_hba->dev;
1461 int s = sizeof(struct host_to_dev_fis);
1462 int rc = TMF_RESP_FUNC_FAILED;
1463 struct asd_sas_phy *sas_phy;
1464 struct ata_link *link;
1468 state = hisi_hba->hw->get_phys_state(hisi_hba);
1469 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1470 if (!(state & BIT(sas_phy->id)))
1473 ata_for_each_link(link, ap, EDGE) {
1474 int pmp = sata_srst_pmp(link);
1476 tmf_task.phy_id = sas_phy->id;
1477 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1478 rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1480 if (rc != TMF_RESP_FUNC_COMPLETE) {
1481 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1489 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1491 struct device *dev = hisi_hba->dev;
1494 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1495 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1496 struct domain_device *device = sas_dev->sas_device;
1498 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1501 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1502 HISI_SAS_INT_ABT_DEV, 0);
1504 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1507 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1508 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1509 struct asd_sas_port *sas_port = &port->sas_port;
1510 struct domain_device *port_dev = sas_port->port_dev;
1511 struct domain_device *device;
1513 if (!port_dev || !DEV_IS_EXPANDER(port_dev->dev_type))
1516 /* Try to find a SATA device */
1517 list_for_each_entry(device, &sas_port->dev_list,
1519 if (dev_is_sata(device)) {
1520 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1529 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1531 struct Scsi_Host *shost = hisi_hba->shost;
1533 down(&hisi_hba->sem);
1534 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1536 scsi_block_requests(shost);
1537 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1539 if (timer_pending(&hisi_hba->timer))
1540 del_timer_sync(&hisi_hba->timer);
1542 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1544 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1546 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1548 struct Scsi_Host *shost = hisi_hba->shost;
1551 /* Init and wait for PHYs to come up and all libsas event finished. */
1552 hisi_hba->hw->phys_init(hisi_hba);
1554 hisi_sas_refresh_port_id(hisi_hba);
1555 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1558 if (hisi_hba->reject_stp_links_msk)
1559 hisi_sas_terminate_stp_reject(hisi_hba);
1560 hisi_sas_reset_init_all_devices(hisi_hba);
1561 scsi_unblock_requests(shost);
1562 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1564 state = hisi_hba->hw->get_phys_state(hisi_hba);
1565 hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state, state);
1567 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1569 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1571 struct device *dev = hisi_hba->dev;
1572 struct Scsi_Host *shost = hisi_hba->shost;
1575 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct)
1576 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
1578 if (!hisi_hba->hw->soft_reset)
1581 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1584 dev_info(dev, "controller resetting...\n");
1585 hisi_sas_controller_reset_prepare(hisi_hba);
1587 rc = hisi_hba->hw->soft_reset(hisi_hba);
1589 dev_warn(dev, "controller reset failed (%d)\n", rc);
1590 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1592 scsi_unblock_requests(shost);
1593 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1597 hisi_sas_controller_reset_done(hisi_hba);
1598 dev_info(dev, "controller reset complete\n");
1603 static int hisi_sas_abort_task(struct sas_task *task)
1605 struct scsi_lun lun;
1606 struct hisi_sas_tmf_task tmf_task;
1607 struct domain_device *device = task->dev;
1608 struct hisi_sas_device *sas_dev = device->lldd_dev;
1609 struct hisi_hba *hisi_hba;
1611 int rc = TMF_RESP_FUNC_FAILED;
1612 unsigned long flags;
1615 return TMF_RESP_FUNC_FAILED;
1617 hisi_hba = dev_to_hisi_hba(task->dev);
1618 dev = hisi_hba->dev;
1620 spin_lock_irqsave(&task->task_state_lock, flags);
1621 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1622 struct hisi_sas_slot *slot = task->lldd_task;
1623 struct hisi_sas_cq *cq;
1627 * flush tasklet to avoid free'ing task
1628 * before using task in IO completion
1630 cq = &hisi_hba->cq[slot->dlvry_queue];
1631 tasklet_kill(&cq->tasklet);
1633 spin_unlock_irqrestore(&task->task_state_lock, flags);
1634 rc = TMF_RESP_FUNC_COMPLETE;
1637 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1638 spin_unlock_irqrestore(&task->task_state_lock, flags);
1640 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1641 struct scsi_cmnd *cmnd = task->uldd_task;
1642 struct hisi_sas_slot *slot = task->lldd_task;
1643 u16 tag = slot->idx;
1646 int_to_scsilun(cmnd->device->lun, &lun);
1647 tmf_task.tmf = TMF_ABORT_TASK;
1648 tmf_task.tag_of_task_to_be_managed = tag;
1650 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1653 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1654 HISI_SAS_INT_ABT_CMD, tag);
1656 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1657 return TMF_RESP_FUNC_FAILED;
1661 * If the TMF finds that the IO is not in the device and also
1662 * the internal abort does not succeed, then it is safe to
1664 * Note: if the internal abort succeeds then the slot
1665 * will have already been completed
1667 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1668 if (task->lldd_task)
1669 hisi_sas_do_release_task(hisi_hba, task, slot);
1671 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1672 task->task_proto & SAS_PROTOCOL_STP) {
1673 if (task->dev->dev_type == SAS_SATA_DEV) {
1674 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1675 HISI_SAS_INT_ABT_DEV,
1678 dev_err(dev, "abort task: internal abort failed\n");
1681 hisi_sas_dereg_device(hisi_hba, device);
1682 rc = hisi_sas_softreset_ata_disk(device);
1684 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1686 struct hisi_sas_slot *slot = task->lldd_task;
1687 u32 tag = slot->idx;
1688 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1690 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1691 HISI_SAS_INT_ABT_CMD, tag);
1692 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1695 * flush tasklet to avoid free'ing task
1696 * before using task in IO completion
1698 tasklet_kill(&cq->tasklet);
1704 if (rc != TMF_RESP_FUNC_COMPLETE)
1705 dev_notice(dev, "abort task: rc=%d\n", rc);
1709 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1711 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1712 struct device *dev = hisi_hba->dev;
1713 struct hisi_sas_tmf_task tmf_task;
1714 int rc = TMF_RESP_FUNC_FAILED;
1716 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1717 HISI_SAS_INT_ABT_DEV, 0);
1719 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1720 return TMF_RESP_FUNC_FAILED;
1722 hisi_sas_dereg_device(hisi_hba, device);
1724 tmf_task.tmf = TMF_ABORT_TASK_SET;
1725 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1727 if (rc == TMF_RESP_FUNC_COMPLETE)
1728 hisi_sas_release_task(hisi_hba, device);
1733 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1735 struct hisi_sas_tmf_task tmf_task;
1738 tmf_task.tmf = TMF_CLEAR_ACA;
1739 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1744 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1746 struct sas_phy *local_phy = sas_get_local_phy(device);
1747 struct hisi_sas_device *sas_dev = device->lldd_dev;
1748 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1749 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1750 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[local_phy->number];
1751 struct hisi_sas_phy *phy = container_of(sas_phy,
1752 struct hisi_sas_phy, sas_phy);
1753 DECLARE_COMPLETION_ONSTACK(phyreset);
1756 if (scsi_is_sas_phy_local(local_phy)) {
1758 phy->reset_completion = &phyreset;
1761 reset_type = (sas_dev->dev_status == HISI_SAS_DEV_INIT ||
1762 !dev_is_sata(device)) ? 1 : 0;
1764 rc = sas_phy_reset(local_phy, reset_type);
1765 sas_put_local_phy(local_phy);
1767 if (scsi_is_sas_phy_local(local_phy)) {
1768 int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1769 unsigned long flags;
1771 spin_lock_irqsave(&phy->lock, flags);
1772 phy->reset_completion = NULL;
1774 spin_unlock_irqrestore(&phy->lock, flags);
1776 /* report PHY down if timed out */
1778 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
1779 } else if (sas_dev->dev_status != HISI_SAS_DEV_INIT) {
1781 * If in init state, we rely on caller to wait for link to be
1782 * ready; otherwise, delay.
1790 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1792 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1793 struct device *dev = hisi_hba->dev;
1796 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1797 HISI_SAS_INT_ABT_DEV, 0);
1799 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1800 return TMF_RESP_FUNC_FAILED;
1802 hisi_sas_dereg_device(hisi_hba, device);
1804 rc = hisi_sas_debug_I_T_nexus_reset(device);
1806 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1807 hisi_sas_release_task(hisi_hba, device);
1812 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1814 struct hisi_sas_device *sas_dev = device->lldd_dev;
1815 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1816 struct device *dev = hisi_hba->dev;
1817 int rc = TMF_RESP_FUNC_FAILED;
1819 if (dev_is_sata(device)) {
1820 struct sas_phy *phy;
1822 /* Clear internal IO and then hardreset */
1823 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1824 HISI_SAS_INT_ABT_DEV, 0);
1826 dev_err(dev, "lu_reset: internal abort failed\n");
1829 hisi_sas_dereg_device(hisi_hba, device);
1831 phy = sas_get_local_phy(device);
1833 rc = sas_phy_reset(phy, 1);
1836 hisi_sas_release_task(hisi_hba, device);
1837 sas_put_local_phy(phy);
1839 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1841 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1842 HISI_SAS_INT_ABT_DEV, 0);
1844 dev_err(dev, "lu_reset: internal abort failed\n");
1847 hisi_sas_dereg_device(hisi_hba, device);
1849 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1850 if (rc == TMF_RESP_FUNC_COMPLETE)
1851 hisi_sas_release_task(hisi_hba, device);
1854 if (rc != TMF_RESP_FUNC_COMPLETE)
1855 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1856 sas_dev->device_id, rc);
1860 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1862 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1863 struct device *dev = hisi_hba->dev;
1864 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1867 queue_work(hisi_hba->wq, &r.work);
1868 wait_for_completion(r.completion);
1870 return TMF_RESP_FUNC_FAILED;
1872 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1873 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1874 struct domain_device *device = sas_dev->sas_device;
1876 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1877 DEV_IS_EXPANDER(device->dev_type))
1880 rc = hisi_sas_debug_I_T_nexus_reset(device);
1881 if (rc != TMF_RESP_FUNC_COMPLETE)
1882 dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
1883 sas_dev->device_id, rc);
1886 hisi_sas_release_tasks(hisi_hba);
1888 return TMF_RESP_FUNC_COMPLETE;
1891 static int hisi_sas_query_task(struct sas_task *task)
1893 struct scsi_lun lun;
1894 struct hisi_sas_tmf_task tmf_task;
1895 int rc = TMF_RESP_FUNC_FAILED;
1897 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1898 struct scsi_cmnd *cmnd = task->uldd_task;
1899 struct domain_device *device = task->dev;
1900 struct hisi_sas_slot *slot = task->lldd_task;
1901 u32 tag = slot->idx;
1903 int_to_scsilun(cmnd->device->lun, &lun);
1904 tmf_task.tmf = TMF_QUERY_TASK;
1905 tmf_task.tag_of_task_to_be_managed = tag;
1907 rc = hisi_sas_debug_issue_ssp_tmf(device,
1911 /* The task is still in Lun, release it then */
1912 case TMF_RESP_FUNC_SUCC:
1913 /* The task is not in Lun or failed, reset the phy */
1914 case TMF_RESP_FUNC_FAILED:
1915 case TMF_RESP_FUNC_COMPLETE:
1918 rc = TMF_RESP_FUNC_FAILED;
1926 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1927 struct sas_task *task, int abort_flag,
1928 int task_tag, struct hisi_sas_dq *dq)
1930 struct domain_device *device = task->dev;
1931 struct hisi_sas_device *sas_dev = device->lldd_dev;
1932 struct device *dev = hisi_hba->dev;
1933 struct hisi_sas_port *port;
1934 struct hisi_sas_slot *slot;
1935 struct asd_sas_port *sas_port = device->port;
1936 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1937 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1938 unsigned long flags, flags_dq = 0;
1941 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1947 port = to_hisi_sas_port(sas_port);
1949 /* simply get a slot and send abort command */
1950 rc = hisi_sas_slot_index_alloc(hisi_hba, NULL);
1955 slot = &hisi_hba->slot_info[slot_idx];
1957 spin_lock_irqsave(&dq->lock, flags_dq);
1958 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
1959 if (wr_q_index < 0) {
1960 spin_unlock_irqrestore(&dq->lock, flags_dq);
1964 list_add_tail(&slot->delivery, &dq->list);
1965 spin_unlock_irqrestore(&dq->lock, flags_dq);
1966 spin_lock_irqsave(&sas_dev->lock, flags);
1967 list_add_tail(&slot->entry, &sas_dev->list);
1968 spin_unlock_irqrestore(&sas_dev->lock, flags);
1970 dlvry_queue = dq->id;
1971 dlvry_queue_slot = wr_q_index;
1973 slot->device_id = sas_dev->device_id;
1974 slot->n_elem = n_elem;
1975 slot->dlvry_queue = dlvry_queue;
1976 slot->dlvry_queue_slot = dlvry_queue_slot;
1977 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1978 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1981 slot->is_internal = true;
1982 task->lldd_task = slot;
1984 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1985 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1986 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
1988 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1989 abort_flag, task_tag);
1991 spin_lock_irqsave(&task->task_state_lock, flags);
1992 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1993 spin_unlock_irqrestore(&task->task_state_lock, flags);
1994 WRITE_ONCE(slot->ready, 1);
1995 /* send abort command to the chip */
1996 spin_lock_irqsave(&dq->lock, flags);
1997 hisi_hba->hw->start_delivery(dq);
1998 spin_unlock_irqrestore(&dq->lock, flags);
2003 hisi_sas_slot_index_free(hisi_hba, slot_idx);
2005 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
2011 * _hisi_sas_internal_task_abort -- execute an internal
2012 * abort command for single IO command or a device
2013 * @hisi_hba: host controller struct
2014 * @device: domain device
2015 * @abort_flag: mode of operation, device or single IO
2016 * @tag: tag of IO to be aborted (only relevant to single
2018 * @dq: delivery queue for this internal abort command
2021 _hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2022 struct domain_device *device, int abort_flag,
2023 int tag, struct hisi_sas_dq *dq)
2025 struct sas_task *task;
2026 struct hisi_sas_device *sas_dev = device->lldd_dev;
2027 struct device *dev = hisi_hba->dev;
2031 * The interface is not realized means this HW don't support internal
2032 * abort, or don't need to do internal abort. Then here, we return
2033 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
2034 * the internal abort has been executed and returned CQ.
2036 if (!hisi_hba->hw->prep_abort)
2037 return TMF_RESP_FUNC_FAILED;
2039 task = sas_alloc_slow_task(GFP_KERNEL);
2044 task->task_proto = device->tproto;
2045 task->task_done = hisi_sas_task_done;
2046 task->slow_task->timer.function = hisi_sas_tmf_timedout;
2047 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT * HZ;
2048 add_timer(&task->slow_task->timer);
2050 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
2051 task, abort_flag, tag, dq);
2053 del_timer(&task->slow_task->timer);
2054 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
2058 wait_for_completion(&task->slow_task->completion);
2059 res = TMF_RESP_FUNC_FAILED;
2061 /* Internal abort timed out */
2062 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
2063 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
2064 struct hisi_sas_slot *slot = task->lldd_task;
2067 struct hisi_sas_cq *cq =
2068 &hisi_hba->cq[slot->dlvry_queue];
2070 * flush tasklet to avoid free'ing task
2071 * before using task in IO completion
2073 tasklet_kill(&cq->tasklet);
2076 dev_err(dev, "internal task abort: timeout and not done.\n");
2081 dev_err(dev, "internal task abort: timeout.\n");
2084 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2085 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
2086 res = TMF_RESP_FUNC_COMPLETE;
2090 if (task->task_status.resp == SAS_TASK_COMPLETE &&
2091 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
2092 res = TMF_RESP_FUNC_SUCC;
2097 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
2098 "resp: 0x%x sts 0x%x\n",
2099 SAS_ADDR(device->sas_addr),
2101 task->task_status.resp, /* 0 is complete, -1 is undelivered */
2102 task->task_status.stat);
2103 sas_free_task(task);
2109 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
2110 struct domain_device *device,
2111 int abort_flag, int tag)
2113 struct hisi_sas_slot *slot;
2114 struct device *dev = hisi_hba->dev;
2115 struct hisi_sas_dq *dq;
2118 switch (abort_flag) {
2119 case HISI_SAS_INT_ABT_CMD:
2120 slot = &hisi_hba->slot_info[tag];
2121 dq = &hisi_hba->dq[slot->dlvry_queue];
2122 return _hisi_sas_internal_task_abort(hisi_hba, device,
2123 abort_flag, tag, dq);
2124 case HISI_SAS_INT_ABT_DEV:
2125 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2126 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2127 const struct cpumask *mask = cq->pci_irq_mask;
2129 if (mask && !cpumask_intersects(cpu_online_mask, mask))
2131 dq = &hisi_hba->dq[i];
2132 rc = _hisi_sas_internal_task_abort(hisi_hba, device,
2140 dev_err(dev, "Unrecognised internal abort flag (%d)\n",
2148 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
2150 hisi_sas_port_notify_formed(sas_phy);
2153 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
2154 u8 reg_index, u8 reg_count, u8 *write_data)
2156 struct hisi_hba *hisi_hba = sha->lldd_ha;
2158 if (!hisi_hba->hw->write_gpio)
2161 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
2162 reg_index, reg_count, write_data);
2165 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
2167 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2168 struct sas_phy *sphy = sas_phy->phy;
2169 struct sas_phy_data *d = sphy->hostdata;
2171 phy->phy_attached = 0;
2176 sphy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
2178 sphy->negotiated_linkrate = SAS_PHY_DISABLED;
2181 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
2183 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
2184 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2185 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
2186 struct device *dev = hisi_hba->dev;
2189 /* Phy down but ready */
2190 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
2191 hisi_sas_port_notify_formed(sas_phy);
2193 struct hisi_sas_port *port = phy->port;
2195 if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
2197 dev_info(dev, "ignore flutter phy%d down\n", phy_no);
2200 /* Phy down and not ready */
2201 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
2202 sas_phy_disconnected(sas_phy);
2205 if (phy->phy_type & PORT_TYPE_SAS) {
2206 int port_id = port->id;
2208 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2210 port->port_attached = 0;
2211 } else if (phy->phy_type & PORT_TYPE_SATA)
2212 port->port_attached = 0;
2214 hisi_sas_phy_disconnected(phy);
2217 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2219 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
2223 for (i = 0; i < hisi_hba->cq_nvecs; i++) {
2224 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2226 tasklet_kill(&cq->tasklet);
2229 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
2231 struct scsi_transport_template *hisi_sas_stt;
2232 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2234 static struct sas_domain_function_template hisi_sas_transport_ops = {
2235 .lldd_dev_found = hisi_sas_dev_found,
2236 .lldd_dev_gone = hisi_sas_dev_gone,
2237 .lldd_execute_task = hisi_sas_queue_command,
2238 .lldd_control_phy = hisi_sas_control_phy,
2239 .lldd_abort_task = hisi_sas_abort_task,
2240 .lldd_abort_task_set = hisi_sas_abort_task_set,
2241 .lldd_clear_aca = hisi_sas_clear_aca,
2242 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
2243 .lldd_lu_reset = hisi_sas_lu_reset,
2244 .lldd_query_task = hisi_sas_query_task,
2245 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
2246 .lldd_port_formed = hisi_sas_port_formed,
2247 .lldd_write_gpio = hisi_sas_write_gpio,
2250 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2252 int i, s, j, max_command_entries = hisi_hba->hw->max_command_entries;
2253 struct hisi_sas_breakpoint *sata_breakpoint = hisi_hba->sata_breakpoint;
2255 for (i = 0; i < hisi_hba->queue_count; i++) {
2256 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2257 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2258 struct hisi_sas_cmd_hdr *cmd_hdr = hisi_hba->cmd_hdr[i];
2260 s = sizeof(struct hisi_sas_cmd_hdr);
2261 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2262 memset(&cmd_hdr[j], 0, s);
2266 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2267 memset(hisi_hba->complete_hdr[i], 0, s);
2271 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2272 memset(hisi_hba->initial_fis, 0, s);
2274 s = max_command_entries * sizeof(struct hisi_sas_iost);
2275 memset(hisi_hba->iost, 0, s);
2277 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2278 memset(hisi_hba->breakpoint, 0, s);
2280 s = sizeof(struct hisi_sas_sata_breakpoint);
2281 for (j = 0; j < HISI_SAS_MAX_ITCT_ENTRIES; j++)
2282 memset(&sata_breakpoint[j], 0, s);
2284 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2286 int hisi_sas_alloc(struct hisi_hba *hisi_hba)
2288 struct device *dev = hisi_hba->dev;
2289 int i, j, s, max_command_entries = hisi_hba->hw->max_command_entries;
2290 int max_command_entries_ru, sz_slot_buf_ru;
2291 int blk_cnt, slots_per_blk;
2293 sema_init(&hisi_hba->sem, 1);
2294 spin_lock_init(&hisi_hba->lock);
2295 for (i = 0; i < hisi_hba->n_phy; i++) {
2296 hisi_sas_phy_init(hisi_hba, i);
2297 hisi_hba->port[i].port_attached = 0;
2298 hisi_hba->port[i].id = -1;
2301 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2302 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2303 hisi_hba->devices[i].device_id = i;
2304 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_INIT;
2307 for (i = 0; i < hisi_hba->queue_count; i++) {
2308 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2309 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2311 /* Completion queue structure */
2313 cq->hisi_hba = hisi_hba;
2315 /* Delivery queue structure */
2316 spin_lock_init(&dq->lock);
2317 INIT_LIST_HEAD(&dq->list);
2319 dq->hisi_hba = hisi_hba;
2321 /* Delivery queue */
2322 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2323 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2324 &hisi_hba->cmd_hdr_dma[i],
2326 if (!hisi_hba->cmd_hdr[i])
2329 /* Completion queue */
2330 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2331 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2332 &hisi_hba->complete_hdr_dma[i],
2334 if (!hisi_hba->complete_hdr[i])
2338 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2339 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2340 GFP_KERNEL | __GFP_ZERO);
2341 if (!hisi_hba->itct)
2344 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2345 sizeof(struct hisi_sas_slot),
2347 if (!hisi_hba->slot_info)
2350 /* roundup to avoid overly large block size */
2351 max_command_entries_ru = roundup(max_command_entries, 64);
2352 if (hisi_hba->prot_mask & HISI_SAS_DIX_PROT_MASK)
2353 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_dif_buf_table);
2355 sz_slot_buf_ru = sizeof(struct hisi_sas_slot_buf_table);
2356 sz_slot_buf_ru = roundup(sz_slot_buf_ru, 64);
2357 s = lcm(max_command_entries_ru, sz_slot_buf_ru);
2358 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2359 slots_per_blk = s / sz_slot_buf_ru;
2361 for (i = 0; i < blk_cnt; i++) {
2362 int slot_index = i * slots_per_blk;
2366 buf = dmam_alloc_coherent(dev, s, &buf_dma,
2367 GFP_KERNEL | __GFP_ZERO);
2371 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2372 struct hisi_sas_slot *slot;
2374 slot = &hisi_hba->slot_info[slot_index];
2376 slot->buf_dma = buf_dma;
2377 slot->idx = slot_index;
2379 buf += sz_slot_buf_ru;
2380 buf_dma += sz_slot_buf_ru;
2384 s = max_command_entries * sizeof(struct hisi_sas_iost);
2385 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2387 if (!hisi_hba->iost)
2390 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2391 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2392 &hisi_hba->breakpoint_dma,
2394 if (!hisi_hba->breakpoint)
2397 hisi_hba->slot_index_count = max_command_entries;
2398 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2399 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2400 if (!hisi_hba->slot_index_tags)
2403 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2404 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2405 &hisi_hba->initial_fis_dma,
2407 if (!hisi_hba->initial_fis)
2410 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2411 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2412 &hisi_hba->sata_breakpoint_dma,
2414 if (!hisi_hba->sata_breakpoint)
2416 hisi_sas_init_mem(hisi_hba);
2418 hisi_sas_slot_index_init(hisi_hba);
2419 hisi_hba->last_slot_index = hisi_hba->hw->max_command_entries -
2420 HISI_SAS_RESERVED_IPTT_CNT;
2422 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2423 if (!hisi_hba->wq) {
2424 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2432 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2434 void hisi_sas_free(struct hisi_hba *hisi_hba)
2437 destroy_workqueue(hisi_hba->wq);
2439 EXPORT_SYMBOL_GPL(hisi_sas_free);
2441 void hisi_sas_rst_work_handler(struct work_struct *work)
2443 struct hisi_hba *hisi_hba =
2444 container_of(work, struct hisi_hba, rst_work);
2446 hisi_sas_controller_reset(hisi_hba);
2448 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2450 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2452 struct hisi_sas_rst *rst =
2453 container_of(work, struct hisi_sas_rst, work);
2455 if (!hisi_sas_controller_reset(rst->hisi_hba))
2457 complete(rst->completion);
2459 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2461 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2463 struct device *dev = hisi_hba->dev;
2464 struct platform_device *pdev = hisi_hba->platform_dev;
2465 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2468 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2470 dev_err(dev, "could not get property sas-addr\n");
2476 * These properties are only required for platform device-based
2477 * controller with DT firmware.
2479 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2480 "hisilicon,sas-syscon");
2481 if (IS_ERR(hisi_hba->ctrl)) {
2482 dev_err(dev, "could not get syscon\n");
2486 if (device_property_read_u32(dev, "ctrl-reset-reg",
2487 &hisi_hba->ctrl_reset_reg)) {
2489 "could not get property ctrl-reset-reg\n");
2493 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2494 &hisi_hba->ctrl_reset_sts_reg)) {
2496 "could not get property ctrl-reset-sts-reg\n");
2500 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2501 &hisi_hba->ctrl_clock_ena_reg)) {
2503 "could not get property ctrl-clock-ena-reg\n");
2508 refclk = devm_clk_get(dev, NULL);
2510 dev_dbg(dev, "no ref clk property\n");
2512 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2514 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2515 dev_err(dev, "could not get property phy-count\n");
2519 if (device_property_read_u32(dev, "queue-count",
2520 &hisi_hba->queue_count)) {
2521 dev_err(dev, "could not get property queue-count\n");
2527 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2529 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2530 const struct hisi_sas_hw *hw)
2532 struct resource *res;
2533 struct Scsi_Host *shost;
2534 struct hisi_hba *hisi_hba;
2535 struct device *dev = &pdev->dev;
2538 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2540 dev_err(dev, "scsi host alloc failed\n");
2543 hisi_hba = shost_priv(shost);
2545 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2547 hisi_hba->dev = dev;
2548 hisi_hba->platform_dev = pdev;
2549 hisi_hba->shost = shost;
2550 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2552 timer_setup(&hisi_hba->timer, NULL, 0);
2554 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2557 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2559 error = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2562 dev_err(dev, "No usable DMA addressing method\n");
2566 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2567 hisi_hba->regs = devm_ioremap_resource(dev, res);
2568 if (IS_ERR(hisi_hba->regs))
2571 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2573 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2574 if (IS_ERR(hisi_hba->sgpio_regs))
2578 if (hisi_sas_alloc(hisi_hba)) {
2579 hisi_sas_free(hisi_hba);
2585 scsi_host_put(shost);
2586 dev_err(dev, "shost alloc failed\n");
2590 int hisi_sas_probe(struct platform_device *pdev,
2591 const struct hisi_sas_hw *hw)
2593 struct Scsi_Host *shost;
2594 struct hisi_hba *hisi_hba;
2595 struct device *dev = &pdev->dev;
2596 struct asd_sas_phy **arr_phy;
2597 struct asd_sas_port **arr_port;
2598 struct sas_ha_struct *sha;
2599 int rc, phy_nr, port_nr, i;
2601 shost = hisi_sas_shost_alloc(pdev, hw);
2605 sha = SHOST_TO_SAS_HA(shost);
2606 hisi_hba = shost_priv(shost);
2607 platform_set_drvdata(pdev, sha);
2609 phy_nr = port_nr = hisi_hba->n_phy;
2611 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2612 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2613 if (!arr_phy || !arr_port) {
2618 sha->sas_phy = arr_phy;
2619 sha->sas_port = arr_port;
2620 sha->lldd_ha = hisi_hba;
2622 shost->transportt = hisi_sas_stt;
2623 shost->max_id = HISI_SAS_MAX_DEVICES;
2624 shost->max_lun = ~0;
2625 shost->max_channel = 1;
2626 shost->max_cmd_len = 16;
2627 if (hisi_hba->hw->slot_index_alloc) {
2628 shost->can_queue = hisi_hba->hw->max_command_entries;
2629 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
2631 shost->can_queue = hisi_hba->hw->max_command_entries -
2632 HISI_SAS_RESERVED_IPTT_CNT;
2633 shost->cmd_per_lun = hisi_hba->hw->max_command_entries -
2634 HISI_SAS_RESERVED_IPTT_CNT;
2637 sha->sas_ha_name = DRV_NAME;
2638 sha->dev = hisi_hba->dev;
2639 sha->lldd_module = THIS_MODULE;
2640 sha->sas_addr = &hisi_hba->sas_addr[0];
2641 sha->num_phys = hisi_hba->n_phy;
2642 sha->core.shost = hisi_hba->shost;
2644 for (i = 0; i < hisi_hba->n_phy; i++) {
2645 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2646 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2649 rc = scsi_add_host(shost, &pdev->dev);
2653 rc = sas_register_ha(sha);
2655 goto err_out_register_ha;
2657 rc = hisi_hba->hw->hw_init(hisi_hba);
2659 goto err_out_register_ha;
2661 scsi_scan_host(shost);
2665 err_out_register_ha:
2666 scsi_remove_host(shost);
2668 hisi_sas_free(hisi_hba);
2669 scsi_host_put(shost);
2672 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2674 struct dentry *hisi_sas_debugfs_dir;
2676 static void hisi_sas_debugfs_snapshot_cq_reg(struct hisi_hba *hisi_hba)
2678 int queue_entry_size = hisi_hba->hw->complete_hdr_size;
2681 for (i = 0; i < hisi_hba->queue_count; i++)
2682 memcpy(hisi_hba->debugfs_complete_hdr[i],
2683 hisi_hba->complete_hdr[i],
2684 HISI_SAS_QUEUE_SLOTS * queue_entry_size);
2687 static void hisi_sas_debugfs_snapshot_dq_reg(struct hisi_hba *hisi_hba)
2689 int queue_entry_size = sizeof(struct hisi_sas_cmd_hdr);
2692 for (i = 0; i < hisi_hba->queue_count; i++) {
2693 struct hisi_sas_cmd_hdr *debugfs_cmd_hdr, *cmd_hdr;
2696 debugfs_cmd_hdr = hisi_hba->debugfs_cmd_hdr[i];
2697 cmd_hdr = hisi_hba->cmd_hdr[i];
2699 for (j = 0; j < HISI_SAS_QUEUE_SLOTS; j++)
2700 memcpy(&debugfs_cmd_hdr[j], &cmd_hdr[j],
2705 static void hisi_sas_debugfs_snapshot_port_reg(struct hisi_hba *hisi_hba)
2707 const struct hisi_sas_debugfs_reg *port =
2708 hisi_hba->hw->debugfs_reg_port;
2713 for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) {
2714 databuf = (u32 *)hisi_hba->debugfs_port_reg[phy_cnt];
2715 for (i = 0; i < port->count; i++, databuf++) {
2716 offset = port->base_off + 4 * i;
2717 *databuf = port->read_port_reg(hisi_hba, phy_cnt,
2723 static void hisi_sas_debugfs_snapshot_global_reg(struct hisi_hba *hisi_hba)
2725 u32 *databuf = (u32 *)hisi_hba->debugfs_global_reg;
2726 const struct hisi_sas_debugfs_reg *global =
2727 hisi_hba->hw->debugfs_reg_global;
2730 for (i = 0; i < global->count; i++, databuf++)
2731 *databuf = global->read_global_reg(hisi_hba, 4 * i);
2734 static void hisi_sas_debugfs_snapshot_itct_reg(struct hisi_hba *hisi_hba)
2736 void *databuf = hisi_hba->debugfs_itct;
2737 struct hisi_sas_itct *itct;
2740 itct = hisi_hba->itct;
2742 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
2743 memcpy(databuf, itct, sizeof(struct hisi_sas_itct));
2744 databuf += sizeof(struct hisi_sas_itct);
2748 static void hisi_sas_debugfs_snapshot_iost_reg(struct hisi_hba *hisi_hba)
2750 int max_command_entries = hisi_hba->hw->max_command_entries;
2751 void *databuf = hisi_hba->debugfs_iost;
2752 struct hisi_sas_iost *iost;
2755 iost = hisi_hba->iost;
2757 for (i = 0; i < max_command_entries; i++, iost++) {
2758 memcpy(databuf, iost, sizeof(struct hisi_sas_iost));
2759 databuf += sizeof(struct hisi_sas_iost);
2764 hisi_sas_debugfs_to_reg_name(int off, int base_off,
2765 const struct hisi_sas_debugfs_reg_lu *lu)
2767 for (; lu->name; lu++) {
2768 if (off == lu->off - base_off)
2775 static void hisi_sas_debugfs_print_reg(u32 *regs_val, const void *ptr,
2778 const struct hisi_sas_debugfs_reg *reg = ptr;
2781 for (i = 0; i < reg->count; i++) {
2785 name = hisi_sas_debugfs_to_reg_name(off, reg->base_off,
2789 seq_printf(s, "0x%08x 0x%08x %s\n", off,
2792 seq_printf(s, "0x%08x 0x%08x\n", off,
2797 static int hisi_sas_debugfs_global_show(struct seq_file *s, void *p)
2799 struct hisi_hba *hisi_hba = s->private;
2800 const struct hisi_sas_hw *hw = hisi_hba->hw;
2801 const struct hisi_sas_debugfs_reg *reg_global = hw->debugfs_reg_global;
2803 hisi_sas_debugfs_print_reg(hisi_hba->debugfs_global_reg,
2809 static int hisi_sas_debugfs_global_open(struct inode *inode, struct file *filp)
2811 return single_open(filp, hisi_sas_debugfs_global_show,
2815 static const struct file_operations hisi_sas_debugfs_global_fops = {
2816 .open = hisi_sas_debugfs_global_open,
2818 .llseek = seq_lseek,
2819 .release = single_release,
2820 .owner = THIS_MODULE,
2823 static int hisi_sas_debugfs_port_show(struct seq_file *s, void *p)
2825 struct hisi_sas_phy *phy = s->private;
2826 struct hisi_hba *hisi_hba = phy->hisi_hba;
2827 const struct hisi_sas_hw *hw = hisi_hba->hw;
2828 const struct hisi_sas_debugfs_reg *reg_port = hw->debugfs_reg_port;
2829 u32 *databuf = hisi_hba->debugfs_port_reg[phy->sas_phy.id];
2831 hisi_sas_debugfs_print_reg(databuf, reg_port, s);
2836 static int hisi_sas_debugfs_port_open(struct inode *inode, struct file *filp)
2838 return single_open(filp, hisi_sas_debugfs_port_show, inode->i_private);
2841 static const struct file_operations hisi_sas_debugfs_port_fops = {
2842 .open = hisi_sas_debugfs_port_open,
2844 .llseek = seq_lseek,
2845 .release = single_release,
2846 .owner = THIS_MODULE,
2849 static int hisi_sas_show_row_64(struct seq_file *s, int index,
2850 int sz, __le64 *ptr)
2854 /* completion header size not fixed per HW version */
2855 seq_printf(s, "index %04d:\n\t", index);
2856 for (i = 1; i <= sz / 8; i++, ptr++) {
2857 seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr));
2859 seq_puts(s, "\n\t");
2867 static int hisi_sas_show_row_32(struct seq_file *s, int index,
2868 int sz, __le32 *ptr)
2872 /* completion header size not fixed per HW version */
2873 seq_printf(s, "index %04d:\n\t", index);
2874 for (i = 1; i <= sz / 4; i++, ptr++) {
2875 seq_printf(s, " 0x%08x", le32_to_cpu(*ptr));
2877 seq_puts(s, "\n\t");
2884 static int hisi_sas_cq_show_slot(struct seq_file *s, int slot, void *cq_ptr)
2886 struct hisi_sas_cq *cq = cq_ptr;
2887 struct hisi_hba *hisi_hba = cq->hisi_hba;
2888 void *complete_queue = hisi_hba->debugfs_complete_hdr[cq->id];
2889 __le32 *complete_hdr = complete_queue +
2890 (hisi_hba->hw->complete_hdr_size * slot);
2892 return hisi_sas_show_row_32(s, slot,
2893 hisi_hba->hw->complete_hdr_size,
2897 static int hisi_sas_debugfs_cq_show(struct seq_file *s, void *p)
2899 struct hisi_sas_cq *cq = s->private;
2902 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
2903 ret = hisi_sas_cq_show_slot(s, slot, cq);
2910 static int hisi_sas_debugfs_cq_open(struct inode *inode, struct file *filp)
2912 return single_open(filp, hisi_sas_debugfs_cq_show, inode->i_private);
2915 static const struct file_operations hisi_sas_debugfs_cq_fops = {
2916 .open = hisi_sas_debugfs_cq_open,
2918 .llseek = seq_lseek,
2919 .release = single_release,
2920 .owner = THIS_MODULE,
2923 static int hisi_sas_dq_show_slot(struct seq_file *s, int slot, void *dq_ptr)
2925 struct hisi_sas_dq *dq = dq_ptr;
2926 struct hisi_hba *hisi_hba = dq->hisi_hba;
2927 void *cmd_queue = hisi_hba->debugfs_cmd_hdr[dq->id];
2928 __le32 *cmd_hdr = cmd_queue +
2929 sizeof(struct hisi_sas_cmd_hdr) * slot;
2931 return hisi_sas_show_row_32(s, slot, sizeof(struct hisi_sas_cmd_hdr),
2935 static int hisi_sas_debugfs_dq_show(struct seq_file *s, void *p)
2939 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
2940 ret = hisi_sas_dq_show_slot(s, slot, s->private);
2947 static int hisi_sas_debugfs_dq_open(struct inode *inode, struct file *filp)
2949 return single_open(filp, hisi_sas_debugfs_dq_show, inode->i_private);
2952 static const struct file_operations hisi_sas_debugfs_dq_fops = {
2953 .open = hisi_sas_debugfs_dq_open,
2955 .llseek = seq_lseek,
2956 .release = single_release,
2957 .owner = THIS_MODULE,
2960 static int hisi_sas_debugfs_iost_show(struct seq_file *s, void *p)
2962 struct hisi_hba *hisi_hba = s->private;
2963 struct hisi_sas_iost *debugfs_iost = hisi_hba->debugfs_iost;
2964 int i, ret, max_command_entries = hisi_hba->hw->max_command_entries;
2965 __le64 *iost = &debugfs_iost->qw0;
2967 for (i = 0; i < max_command_entries; i++, debugfs_iost++) {
2968 ret = hisi_sas_show_row_64(s, i, sizeof(*debugfs_iost),
2977 static int hisi_sas_debugfs_iost_open(struct inode *inode, struct file *filp)
2979 return single_open(filp, hisi_sas_debugfs_iost_show, inode->i_private);
2982 static const struct file_operations hisi_sas_debugfs_iost_fops = {
2983 .open = hisi_sas_debugfs_iost_open,
2985 .llseek = seq_lseek,
2986 .release = single_release,
2987 .owner = THIS_MODULE,
2990 static int hisi_sas_debugfs_itct_show(struct seq_file *s, void *p)
2993 struct hisi_hba *hisi_hba = s->private;
2994 struct hisi_sas_itct *debugfs_itct = hisi_hba->debugfs_itct;
2995 __le64 *itct = &debugfs_itct->qw0;
2997 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, debugfs_itct++) {
2998 ret = hisi_sas_show_row_64(s, i, sizeof(*debugfs_itct),
3007 static int hisi_sas_debugfs_itct_open(struct inode *inode, struct file *filp)
3009 return single_open(filp, hisi_sas_debugfs_itct_show, inode->i_private);
3012 static const struct file_operations hisi_sas_debugfs_itct_fops = {
3013 .open = hisi_sas_debugfs_itct_open,
3015 .llseek = seq_lseek,
3016 .release = single_release,
3017 .owner = THIS_MODULE,
3020 static void hisi_sas_debugfs_create_files(struct hisi_hba *hisi_hba)
3022 struct dentry *dump_dentry;
3023 struct dentry *dentry;
3029 /* Create dump dir inside device dir */
3030 dump_dentry = debugfs_create_dir("dump", hisi_hba->debugfs_dir);
3031 hisi_hba->debugfs_dump_dentry = dump_dentry;
3033 debugfs_create_file("global", 0400, dump_dentry, hisi_hba,
3034 &hisi_sas_debugfs_global_fops);
3036 /* Create port dir and files */
3037 dentry = debugfs_create_dir("port", dump_dentry);
3038 for (p = 0; p < hisi_hba->n_phy; p++) {
3039 snprintf(name, 256, "%d", p);
3041 debugfs_create_file(name, 0400, dentry, &hisi_hba->phy[p],
3042 &hisi_sas_debugfs_port_fops);
3045 /* Create CQ dir and files */
3046 dentry = debugfs_create_dir("cq", dump_dentry);
3047 for (c = 0; c < hisi_hba->queue_count; c++) {
3048 snprintf(name, 256, "%d", c);
3050 debugfs_create_file(name, 0400, dentry, &hisi_hba->cq[c],
3051 &hisi_sas_debugfs_cq_fops);
3054 /* Create DQ dir and files */
3055 dentry = debugfs_create_dir("dq", dump_dentry);
3056 for (d = 0; d < hisi_hba->queue_count; d++) {
3057 snprintf(name, 256, "%d", d);
3059 debugfs_create_file(name, 0400, dentry, &hisi_hba->dq[d],
3060 &hisi_sas_debugfs_dq_fops);
3063 debugfs_create_file("iost", 0400, dump_dentry, hisi_hba,
3064 &hisi_sas_debugfs_iost_fops);
3066 debugfs_create_file("itct", 0400, dump_dentry, hisi_hba,
3067 &hisi_sas_debugfs_itct_fops);
3072 static void hisi_sas_debugfs_snapshot_regs(struct hisi_hba *hisi_hba)
3074 hisi_hba->hw->snapshot_prepare(hisi_hba);
3076 hisi_sas_debugfs_snapshot_global_reg(hisi_hba);
3077 hisi_sas_debugfs_snapshot_port_reg(hisi_hba);
3078 hisi_sas_debugfs_snapshot_cq_reg(hisi_hba);
3079 hisi_sas_debugfs_snapshot_dq_reg(hisi_hba);
3080 hisi_sas_debugfs_snapshot_itct_reg(hisi_hba);
3081 hisi_sas_debugfs_snapshot_iost_reg(hisi_hba);
3083 hisi_sas_debugfs_create_files(hisi_hba);
3085 hisi_hba->hw->snapshot_restore(hisi_hba);
3088 static ssize_t hisi_sas_debugfs_trigger_dump_write(struct file *file,
3089 const char __user *user_buf,
3090 size_t count, loff_t *ppos)
3092 struct hisi_hba *hisi_hba = file->f_inode->i_private;
3095 /* A bit racy, but don't care too much since it's only debugfs */
3096 if (hisi_hba->debugfs_snapshot)
3102 if (copy_from_user(buf, user_buf, count))
3108 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
3113 static const struct file_operations hisi_sas_debugfs_trigger_dump_fops = {
3114 .write = &hisi_sas_debugfs_trigger_dump_write,
3115 .owner = THIS_MODULE,
3118 void hisi_sas_debugfs_work_handler(struct work_struct *work)
3120 struct hisi_hba *hisi_hba =
3121 container_of(work, struct hisi_hba, debugfs_work);
3123 if (hisi_hba->debugfs_snapshot)
3125 hisi_hba->debugfs_snapshot = true;
3127 hisi_sas_debugfs_snapshot_regs(hisi_hba);
3129 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_work_handler);
3131 void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba)
3133 int max_command_entries = hisi_hba->hw->max_command_entries;
3134 struct device *dev = hisi_hba->dev;
3138 hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev),
3139 hisi_sas_debugfs_dir);
3140 debugfs_create_file("trigger_dump", 0600,
3141 hisi_hba->debugfs_dir,
3143 &hisi_sas_debugfs_trigger_dump_fops);
3145 /* Alloc buffer for global */
3146 sz = hisi_hba->hw->debugfs_reg_global->count * 4;
3147 hisi_hba->debugfs_global_reg =
3148 devm_kmalloc(dev, sz, GFP_KERNEL);
3150 if (!hisi_hba->debugfs_global_reg)
3153 /* Alloc buffer for port */
3154 sz = hisi_hba->hw->debugfs_reg_port->count * 4;
3155 for (p = 0; p < hisi_hba->n_phy; p++) {
3156 hisi_hba->debugfs_port_reg[p] =
3157 devm_kmalloc(dev, sz, GFP_KERNEL);
3159 if (!hisi_hba->debugfs_port_reg[p])
3163 /* Alloc buffer for cq */
3164 sz = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
3165 for (c = 0; c < hisi_hba->queue_count; c++) {
3166 hisi_hba->debugfs_complete_hdr[c] =
3167 devm_kmalloc(dev, sz, GFP_KERNEL);
3169 if (!hisi_hba->debugfs_complete_hdr[c])
3173 /* Alloc buffer for dq */
3174 sz = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
3175 for (d = 0; d < hisi_hba->queue_count; d++) {
3176 hisi_hba->debugfs_cmd_hdr[d] =
3177 devm_kmalloc(dev, sz, GFP_KERNEL);
3179 if (!hisi_hba->debugfs_cmd_hdr[d])
3183 /* Alloc buffer for iost */
3184 sz = max_command_entries * sizeof(struct hisi_sas_iost);
3186 hisi_hba->debugfs_iost = devm_kmalloc(dev, sz, GFP_KERNEL);
3187 if (!hisi_hba->debugfs_iost)
3190 /* Alloc buffer for itct */
3191 /* New memory allocation must be locate before itct */
3192 sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
3194 hisi_hba->debugfs_itct = devm_kmalloc(dev, sz, GFP_KERNEL);
3195 if (!hisi_hba->debugfs_itct)
3200 devm_kfree(dev, hisi_hba->debugfs_iost);
3202 for (i = 0; i < d; i++)
3203 devm_kfree(dev, hisi_hba->debugfs_cmd_hdr[i]);
3205 for (i = 0; i < c; i++)
3206 devm_kfree(dev, hisi_hba->debugfs_complete_hdr[i]);
3208 for (i = 0; i < p; i++)
3209 devm_kfree(dev, hisi_hba->debugfs_port_reg[i]);
3210 devm_kfree(dev, hisi_hba->debugfs_global_reg);
3212 debugfs_remove_recursive(hisi_hba->debugfs_dir);
3213 dev_dbg(dev, "failed to init debugfs!\n");
3215 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_init);
3217 void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba)
3219 debugfs_remove_recursive(hisi_hba->debugfs_dir);
3221 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_exit);
3223 int hisi_sas_remove(struct platform_device *pdev)
3225 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
3226 struct hisi_hba *hisi_hba = sha->lldd_ha;
3227 struct Scsi_Host *shost = sha->core.shost;
3229 if (timer_pending(&hisi_hba->timer))
3230 del_timer(&hisi_hba->timer);
3232 sas_unregister_ha(sha);
3233 sas_remove_host(sha->core.shost);
3235 hisi_sas_free(hisi_hba);
3236 scsi_host_put(shost);
3239 EXPORT_SYMBOL_GPL(hisi_sas_remove);
3241 bool hisi_sas_debugfs_enable;
3242 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
3243 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
3244 MODULE_PARM_DESC(hisi_sas_debugfs_enable, "Enable driver debugfs (default disabled)");
3246 static __init int hisi_sas_init(void)
3248 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
3252 if (hisi_sas_debugfs_enable)
3253 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
3258 static __exit void hisi_sas_exit(void)
3260 sas_release_transport(hisi_sas_stt);
3262 debugfs_remove(hisi_sas_debugfs_dir);
3265 module_init(hisi_sas_init);
3266 module_exit(hisi_sas_exit);
3268 MODULE_LICENSE("GPL");
3269 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
3270 MODULE_DESCRIPTION("HISILICON SAS controller driver");
3271 MODULE_ALIAS("platform:" DRV_NAME);