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 #define DRV_NAME "hisi_sas"
15 #define DEV_IS_GONE(dev) \
16 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
18 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
19 u8 *lun, struct hisi_sas_tmf_task *tmf);
21 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
22 struct domain_device *device,
23 int abort_flag, int tag);
24 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
25 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
27 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
28 struct domain_device *device);
29 static void hisi_sas_dev_gone(struct domain_device *device);
31 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
33 switch (fis->command) {
34 case ATA_CMD_FPDMA_WRITE:
35 case ATA_CMD_FPDMA_READ:
36 case ATA_CMD_FPDMA_RECV:
37 case ATA_CMD_FPDMA_SEND:
38 case ATA_CMD_NCQ_NON_DATA:
39 return HISI_SAS_SATA_PROTOCOL_FPDMA;
41 case ATA_CMD_DOWNLOAD_MICRO:
43 case ATA_CMD_PMP_READ:
44 case ATA_CMD_READ_LOG_EXT:
45 case ATA_CMD_PIO_READ:
46 case ATA_CMD_PIO_READ_EXT:
47 case ATA_CMD_PMP_WRITE:
48 case ATA_CMD_WRITE_LOG_EXT:
49 case ATA_CMD_PIO_WRITE:
50 case ATA_CMD_PIO_WRITE_EXT:
51 return HISI_SAS_SATA_PROTOCOL_PIO;
54 case ATA_CMD_DOWNLOAD_MICRO_DMA:
55 case ATA_CMD_PMP_READ_DMA:
56 case ATA_CMD_PMP_WRITE_DMA:
58 case ATA_CMD_READ_EXT:
59 case ATA_CMD_READ_LOG_DMA_EXT:
60 case ATA_CMD_READ_STREAM_DMA_EXT:
61 case ATA_CMD_TRUSTED_RCV_DMA:
62 case ATA_CMD_TRUSTED_SND_DMA:
64 case ATA_CMD_WRITE_EXT:
65 case ATA_CMD_WRITE_FUA_EXT:
66 case ATA_CMD_WRITE_QUEUED:
67 case ATA_CMD_WRITE_LOG_DMA_EXT:
68 case ATA_CMD_WRITE_STREAM_DMA_EXT:
69 case ATA_CMD_ZAC_MGMT_IN:
70 return HISI_SAS_SATA_PROTOCOL_DMA;
72 case ATA_CMD_CHK_POWER:
73 case ATA_CMD_DEV_RESET:
76 case ATA_CMD_FLUSH_EXT:
78 case ATA_CMD_VERIFY_EXT:
79 case ATA_CMD_SET_FEATURES:
81 case ATA_CMD_STANDBYNOW1:
82 case ATA_CMD_ZAC_MGMT_OUT:
83 return HISI_SAS_SATA_PROTOCOL_NONDATA;
86 switch (fis->features) {
87 case ATA_SET_MAX_PASSWD:
88 case ATA_SET_MAX_LOCK:
89 return HISI_SAS_SATA_PROTOCOL_PIO;
91 case ATA_SET_MAX_PASSWD_DMA:
92 case ATA_SET_MAX_UNLOCK_DMA:
93 return HISI_SAS_SATA_PROTOCOL_DMA;
96 return HISI_SAS_SATA_PROTOCOL_NONDATA;
101 if (direction == DMA_NONE)
102 return HISI_SAS_SATA_PROTOCOL_NONDATA;
103 return HISI_SAS_SATA_PROTOCOL_PIO;
107 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
109 void hisi_sas_sata_done(struct sas_task *task,
110 struct hisi_sas_slot *slot)
112 struct task_status_struct *ts = &task->task_status;
113 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
114 struct hisi_sas_status_buffer *status_buf =
115 hisi_sas_status_buf_addr_mem(slot);
116 u8 *iu = &status_buf->iu[0];
117 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
119 resp->frame_len = sizeof(struct dev_to_host_fis);
120 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
122 ts->buf_valid_size = sizeof(*resp);
124 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
126 int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
128 struct ata_queued_cmd *qc = task->uldd_task;
131 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
132 qc->tf.command == ATA_CMD_FPDMA_READ) {
139 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
142 * This function assumes linkrate mask fits in 8 bits, which it
143 * does for all HW versions supported.
145 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
150 max -= SAS_LINK_RATE_1_5_GBPS;
151 for (i = 0; i <= max; i++)
152 rate |= 1 << (i * 2);
155 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
157 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
159 return device->port->ha->lldd_ha;
162 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
164 return container_of(sas_port, struct hisi_sas_port, sas_port);
166 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
168 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
172 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
173 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
175 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
177 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
179 void *bitmap = hisi_hba->slot_index_tags;
181 clear_bit(slot_idx, bitmap);
184 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
188 if (hisi_hba->hw->slot_index_alloc || (slot_idx >=
189 hisi_hba->hw->max_command_entries - HISI_SAS_RESERVED_IPTT_CNT)) {
190 spin_lock_irqsave(&hisi_hba->lock, flags);
191 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
192 spin_unlock_irqrestore(&hisi_hba->lock, flags);
196 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
198 void *bitmap = hisi_hba->slot_index_tags;
200 set_bit(slot_idx, bitmap);
203 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba,
204 struct scsi_cmnd *scsi_cmnd)
207 void *bitmap = hisi_hba->slot_index_tags;
211 return scsi_cmnd->request->tag;
213 spin_lock_irqsave(&hisi_hba->lock, flags);
214 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
215 hisi_hba->last_slot_index + 1);
216 if (index >= hisi_hba->slot_index_count) {
217 index = find_next_zero_bit(bitmap,
218 hisi_hba->slot_index_count,
219 hisi_hba->hw->max_command_entries -
220 HISI_SAS_RESERVED_IPTT_CNT);
221 if (index >= hisi_hba->slot_index_count) {
222 spin_unlock_irqrestore(&hisi_hba->lock, flags);
223 return -SAS_QUEUE_FULL;
226 hisi_sas_slot_index_set(hisi_hba, index);
227 hisi_hba->last_slot_index = index;
228 spin_unlock_irqrestore(&hisi_hba->lock, flags);
233 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
237 for (i = 0; i < hisi_hba->slot_index_count; ++i)
238 hisi_sas_slot_index_clear(hisi_hba, i);
241 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
242 struct hisi_sas_slot *slot)
244 struct hisi_sas_dq *dq = &hisi_hba->dq[slot->dlvry_queue];
248 struct device *dev = hisi_hba->dev;
250 if (!task->lldd_task)
253 task->lldd_task = NULL;
255 if (!sas_protocol_ata(task->task_proto))
257 dma_unmap_sg(dev, task->scatter,
263 spin_lock_irqsave(&dq->lock, flags);
264 list_del_init(&slot->entry);
265 spin_unlock_irqrestore(&dq->lock, flags);
267 memset(slot, 0, offsetof(struct hisi_sas_slot, buf));
269 hisi_sas_slot_index_free(hisi_hba, slot->idx);
271 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
273 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
274 struct hisi_sas_slot *slot)
276 hisi_hba->hw->prep_smp(hisi_hba, slot);
279 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
280 struct hisi_sas_slot *slot)
282 hisi_hba->hw->prep_ssp(hisi_hba, slot);
285 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
286 struct hisi_sas_slot *slot)
288 hisi_hba->hw->prep_stp(hisi_hba, slot);
291 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
292 struct hisi_sas_slot *slot,
293 int device_id, int abort_flag, int tag_to_abort)
295 hisi_hba->hw->prep_abort(hisi_hba, slot,
296 device_id, abort_flag, tag_to_abort);
299 static void hisi_sas_dma_unmap(struct hisi_hba *hisi_hba,
300 struct sas_task *task, int n_elem,
301 int n_elem_req, int n_elem_resp)
303 struct device *dev = hisi_hba->dev;
305 if (!sas_protocol_ata(task->task_proto)) {
306 if (task->num_scatter) {
308 dma_unmap_sg(dev, task->scatter,
311 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
313 dma_unmap_sg(dev, &task->smp_task.smp_req,
316 dma_unmap_sg(dev, &task->smp_task.smp_resp,
322 static int hisi_sas_dma_map(struct hisi_hba *hisi_hba,
323 struct sas_task *task, int *n_elem,
324 int *n_elem_req, int *n_elem_resp)
326 struct device *dev = hisi_hba->dev;
329 if (sas_protocol_ata(task->task_proto)) {
330 *n_elem = task->num_scatter;
332 unsigned int req_len, resp_len;
334 if (task->num_scatter) {
335 *n_elem = dma_map_sg(dev, task->scatter,
336 task->num_scatter, task->data_dir);
341 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
342 *n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
348 req_len = sg_dma_len(&task->smp_task.smp_req);
351 goto err_out_dma_unmap;
353 *n_elem_resp = dma_map_sg(dev, &task->smp_task.smp_resp,
357 goto err_out_dma_unmap;
359 resp_len = sg_dma_len(&task->smp_task.smp_resp);
360 if (resp_len & 0x3) {
362 goto err_out_dma_unmap;
367 if (*n_elem > HISI_SAS_SGE_PAGE_CNT) {
368 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
371 goto err_out_dma_unmap;
376 /* It would be better to call dma_unmap_sg() here, but it's messy */
377 hisi_sas_dma_unmap(hisi_hba, task, *n_elem,
378 *n_elem_req, *n_elem_resp);
383 static int hisi_sas_task_prep(struct sas_task *task,
384 struct hisi_sas_dq **dq_pointer,
385 bool is_tmf, struct hisi_sas_tmf_task *tmf,
388 struct domain_device *device = task->dev;
389 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
390 struct hisi_sas_device *sas_dev = device->lldd_dev;
391 struct hisi_sas_port *port;
392 struct hisi_sas_slot *slot;
393 struct hisi_sas_cmd_hdr *cmd_hdr_base;
394 struct asd_sas_port *sas_port = device->port;
395 struct device *dev = hisi_hba->dev;
396 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
397 int n_elem = 0, n_elem_req = 0, n_elem_resp = 0;
398 struct hisi_sas_dq *dq;
402 if (DEV_IS_GONE(sas_dev)) {
404 dev_info(dev, "task prep: device %d not ready\n",
407 dev_info(dev, "task prep: device %016llx not ready\n",
408 SAS_ADDR(device->sas_addr));
413 *dq_pointer = dq = sas_dev->dq;
415 port = to_hisi_sas_port(sas_port);
416 if (port && !port->port_attached) {
417 dev_info(dev, "task prep: %s port%d not attach device\n",
418 (dev_is_sata(device)) ?
425 rc = hisi_sas_dma_map(hisi_hba, task, &n_elem,
426 &n_elem_req, &n_elem_resp);
430 if (hisi_hba->hw->slot_index_alloc)
431 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, device);
433 struct scsi_cmnd *scsi_cmnd = NULL;
435 if (task->uldd_task) {
436 struct ata_queued_cmd *qc;
438 if (dev_is_sata(device)) {
439 qc = task->uldd_task;
440 scsi_cmnd = qc->scsicmd;
442 scsi_cmnd = task->uldd_task;
445 rc = hisi_sas_slot_index_alloc(hisi_hba, scsi_cmnd);
448 goto err_out_dma_unmap;
451 slot = &hisi_hba->slot_info[slot_idx];
453 spin_lock_irqsave(&dq->lock, flags);
454 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
455 if (wr_q_index < 0) {
456 spin_unlock_irqrestore(&dq->lock, flags);
461 list_add_tail(&slot->delivery, &dq->list);
462 list_add_tail(&slot->entry, &sas_dev->list);
463 spin_unlock_irqrestore(&dq->lock, flags);
465 dlvry_queue = dq->id;
466 dlvry_queue_slot = wr_q_index;
468 slot->n_elem = n_elem;
469 slot->dlvry_queue = dlvry_queue;
470 slot->dlvry_queue_slot = dlvry_queue_slot;
471 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
472 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
476 slot->is_internal = is_tmf;
477 task->lldd_task = slot;
479 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
480 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
481 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
483 switch (task->task_proto) {
484 case SAS_PROTOCOL_SMP:
485 hisi_sas_task_prep_smp(hisi_hba, slot);
487 case SAS_PROTOCOL_SSP:
488 hisi_sas_task_prep_ssp(hisi_hba, slot);
490 case SAS_PROTOCOL_SATA:
491 case SAS_PROTOCOL_STP:
492 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
493 hisi_sas_task_prep_ata(hisi_hba, slot);
496 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
501 spin_lock_irqsave(&task->task_state_lock, flags);
502 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
503 spin_unlock_irqrestore(&task->task_state_lock, flags);
506 WRITE_ONCE(slot->ready, 1);
511 hisi_sas_slot_index_free(hisi_hba, slot_idx);
513 hisi_sas_dma_unmap(hisi_hba, task, n_elem,
514 n_elem_req, n_elem_resp);
516 dev_err(dev, "task prep: failed[%d]!\n", rc);
520 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
521 bool is_tmf, struct hisi_sas_tmf_task *tmf)
526 struct hisi_hba *hisi_hba;
528 struct domain_device *device = task->dev;
529 struct asd_sas_port *sas_port = device->port;
530 struct hisi_sas_dq *dq = NULL;
533 struct task_status_struct *ts = &task->task_status;
535 ts->resp = SAS_TASK_UNDELIVERED;
536 ts->stat = SAS_PHY_DOWN;
538 * libsas will use dev->port, should
539 * not call task_done for sata
541 if (device->dev_type != SAS_SATA_DEV)
542 task->task_done(task);
546 hisi_hba = dev_to_hisi_hba(device);
549 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags))) {
553 down(&hisi_hba->sem);
557 /* protect task_prep and start_delivery sequence */
558 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
560 dev_err(dev, "task exec: failed[%d]!\n", rc);
563 spin_lock_irqsave(&dq->lock, flags);
564 hisi_hba->hw->start_delivery(dq);
565 spin_unlock_irqrestore(&dq->lock, flags);
571 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
573 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
574 struct asd_sas_phy *sas_phy = &phy->sas_phy;
575 struct sas_ha_struct *sas_ha;
577 if (!phy->phy_attached)
580 sas_ha = &hisi_hba->sha;
581 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
584 struct sas_phy *sphy = sas_phy->phy;
586 sphy->negotiated_linkrate = sas_phy->linkrate;
587 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
588 sphy->maximum_linkrate_hw =
589 hisi_hba->hw->phy_get_max_linkrate();
590 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
591 sphy->minimum_linkrate = phy->minimum_linkrate;
593 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
594 sphy->maximum_linkrate = phy->maximum_linkrate;
597 if (phy->phy_type & PORT_TYPE_SAS) {
598 struct sas_identify_frame *id;
600 id = (struct sas_identify_frame *)phy->frame_rcvd;
601 id->dev_type = phy->identify.device_type;
602 id->initiator_bits = SAS_PROTOCOL_ALL;
603 id->target_bits = phy->identify.target_port_protocols;
604 } else if (phy->phy_type & PORT_TYPE_SATA) {
608 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
609 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
612 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
614 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
615 struct hisi_sas_device *sas_dev = NULL;
617 int last = hisi_hba->last_dev_id;
618 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
621 spin_lock_irqsave(&hisi_hba->lock, flags);
622 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
623 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
624 int queue = i % hisi_hba->queue_count;
625 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
627 hisi_hba->devices[i].device_id = i;
628 sas_dev = &hisi_hba->devices[i];
629 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
630 sas_dev->dev_type = device->dev_type;
631 sas_dev->hisi_hba = hisi_hba;
632 sas_dev->sas_device = device;
634 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
639 hisi_hba->last_dev_id = i;
640 spin_unlock_irqrestore(&hisi_hba->lock, flags);
645 #define HISI_SAS_SRST_ATA_DISK_CNT 3
646 static int hisi_sas_init_device(struct domain_device *device)
648 int rc = TMF_RESP_FUNC_COMPLETE;
650 struct hisi_sas_tmf_task tmf_task;
651 int retry = HISI_SAS_SRST_ATA_DISK_CNT;
652 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
654 switch (device->dev_type) {
656 int_to_scsilun(0, &lun);
658 tmf_task.tmf = TMF_CLEAR_TASK_SET;
659 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
661 if (rc == TMF_RESP_FUNC_COMPLETE)
662 hisi_sas_release_task(hisi_hba, device);
666 case SAS_SATA_PM_PORT:
667 case SAS_SATA_PENDING:
668 while (retry-- > 0) {
669 rc = hisi_sas_softreset_ata_disk(device);
681 static int hisi_sas_dev_found(struct domain_device *device)
683 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
684 struct domain_device *parent_dev = device->parent;
685 struct hisi_sas_device *sas_dev;
686 struct device *dev = hisi_hba->dev;
689 if (hisi_hba->hw->alloc_dev)
690 sas_dev = hisi_hba->hw->alloc_dev(device);
692 sas_dev = hisi_sas_alloc_dev(device);
694 dev_err(dev, "fail alloc dev: max support %d devices\n",
695 HISI_SAS_MAX_DEVICES);
699 device->lldd_dev = sas_dev;
700 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
702 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
704 u8 phy_num = parent_dev->ex_dev.num_phys;
707 for (phy_no = 0; phy_no < phy_num; phy_no++) {
708 phy = &parent_dev->ex_dev.ex_phy[phy_no];
709 if (SAS_ADDR(phy->attached_sas_addr) ==
710 SAS_ADDR(device->sas_addr))
714 if (phy_no == phy_num) {
715 dev_info(dev, "dev found: no attached "
716 "dev:%016llx at ex:%016llx\n",
717 SAS_ADDR(device->sas_addr),
718 SAS_ADDR(parent_dev->sas_addr));
724 dev_info(dev, "dev[%d:%x] found\n",
725 sas_dev->device_id, sas_dev->dev_type);
727 rc = hisi_sas_init_device(device);
733 hisi_sas_dev_gone(device);
737 int hisi_sas_slave_configure(struct scsi_device *sdev)
739 struct domain_device *dev = sdev_to_domain_dev(sdev);
740 int ret = sas_slave_configure(sdev);
744 if (!dev_is_sata(dev))
745 sas_change_queue_depth(sdev, 64);
749 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
751 void hisi_sas_scan_start(struct Scsi_Host *shost)
753 struct hisi_hba *hisi_hba = shost_priv(shost);
755 hisi_hba->hw->phys_init(hisi_hba);
757 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
759 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
761 struct hisi_hba *hisi_hba = shost_priv(shost);
762 struct sas_ha_struct *sha = &hisi_hba->sha;
764 /* Wait for PHY up interrupt to occur */
771 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
773 static void hisi_sas_phyup_work(struct work_struct *work)
775 struct hisi_sas_phy *phy =
776 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
777 struct hisi_hba *hisi_hba = phy->hisi_hba;
778 struct asd_sas_phy *sas_phy = &phy->sas_phy;
779 int phy_no = sas_phy->id;
781 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
782 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
785 static void hisi_sas_linkreset_work(struct work_struct *work)
787 struct hisi_sas_phy *phy =
788 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
789 struct asd_sas_phy *sas_phy = &phy->sas_phy;
791 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
794 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
795 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
796 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
799 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
800 enum hisi_sas_phy_event event)
802 struct hisi_hba *hisi_hba = phy->hisi_hba;
804 if (WARN_ON(event >= HISI_PHYES_NUM))
807 return queue_work(hisi_hba->wq, &phy->works[event]);
809 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
811 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
813 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
814 struct asd_sas_phy *sas_phy = &phy->sas_phy;
817 phy->hisi_hba = hisi_hba;
819 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
820 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
821 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
822 sas_phy->class = SAS;
823 sas_phy->iproto = SAS_PROTOCOL_ALL;
825 sas_phy->type = PHY_TYPE_PHYSICAL;
826 sas_phy->role = PHY_ROLE_INITIATOR;
827 sas_phy->oob_mode = OOB_NOT_CONNECTED;
828 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
829 sas_phy->id = phy_no;
830 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
831 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
832 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
833 sas_phy->lldd_phy = phy;
835 for (i = 0; i < HISI_PHYES_NUM; i++)
836 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
838 spin_lock_init(&phy->lock);
841 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
843 struct sas_ha_struct *sas_ha = sas_phy->ha;
844 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
845 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
846 struct asd_sas_port *sas_port = sas_phy->port;
847 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
853 spin_lock_irqsave(&hisi_hba->lock, flags);
854 port->port_attached = 1;
855 port->id = phy->port_id;
857 sas_port->lldd_port = port;
858 spin_unlock_irqrestore(&hisi_hba->lock, flags);
861 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
862 struct hisi_sas_slot *slot)
866 struct task_status_struct *ts;
868 ts = &task->task_status;
870 ts->resp = SAS_TASK_COMPLETE;
871 ts->stat = SAS_ABORTED_TASK;
872 spin_lock_irqsave(&task->task_state_lock, flags);
873 task->task_state_flags &=
874 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
875 task->task_state_flags |= SAS_TASK_STATE_DONE;
876 spin_unlock_irqrestore(&task->task_state_lock, flags);
879 hisi_sas_slot_task_free(hisi_hba, task, slot);
882 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
883 struct domain_device *device)
885 struct hisi_sas_slot *slot, *slot2;
886 struct hisi_sas_device *sas_dev = device->lldd_dev;
888 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
889 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
892 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
894 struct hisi_sas_device *sas_dev;
895 struct domain_device *device;
898 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
899 sas_dev = &hisi_hba->devices[i];
900 device = sas_dev->sas_device;
902 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
906 hisi_sas_release_task(hisi_hba, device);
909 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
911 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
912 struct domain_device *device)
914 if (hisi_hba->hw->dereg_device)
915 hisi_hba->hw->dereg_device(hisi_hba, device);
918 static void hisi_sas_dev_gone(struct domain_device *device)
920 struct hisi_sas_device *sas_dev = device->lldd_dev;
921 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
922 struct device *dev = hisi_hba->dev;
924 dev_info(dev, "dev[%d:%x] is gone\n",
925 sas_dev->device_id, sas_dev->dev_type);
927 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
928 hisi_sas_internal_task_abort(hisi_hba, device,
929 HISI_SAS_INT_ABT_DEV, 0);
931 hisi_sas_dereg_device(hisi_hba, device);
933 down(&hisi_hba->sem);
934 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
936 device->lldd_dev = NULL;
939 if (hisi_hba->hw->free_device)
940 hisi_hba->hw->free_device(sas_dev);
941 sas_dev->dev_type = SAS_PHY_UNUSED;
944 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
946 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
949 static void hisi_sas_phy_set_linkrate(struct hisi_hba *hisi_hba, int phy_no,
950 struct sas_phy_linkrates *r)
952 struct sas_phy_linkrates _r;
954 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
955 struct asd_sas_phy *sas_phy = &phy->sas_phy;
956 enum sas_linkrate min, max;
958 if (r->maximum_linkrate == SAS_LINK_RATE_UNKNOWN) {
959 max = sas_phy->phy->maximum_linkrate;
960 min = r->minimum_linkrate;
961 } else if (r->minimum_linkrate == SAS_LINK_RATE_UNKNOWN) {
962 max = r->maximum_linkrate;
963 min = sas_phy->phy->minimum_linkrate;
967 _r.maximum_linkrate = max;
968 _r.minimum_linkrate = min;
970 sas_phy->phy->maximum_linkrate = max;
971 sas_phy->phy->minimum_linkrate = min;
973 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
975 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, &_r);
976 hisi_hba->hw->phy_start(hisi_hba, phy_no);
979 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
982 struct sas_ha_struct *sas_ha = sas_phy->ha;
983 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
984 int phy_no = sas_phy->id;
987 case PHY_FUNC_HARD_RESET:
988 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
991 case PHY_FUNC_LINK_RESET:
992 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
994 hisi_hba->hw->phy_start(hisi_hba, phy_no);
997 case PHY_FUNC_DISABLE:
998 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
1001 case PHY_FUNC_SET_LINK_RATE:
1002 hisi_sas_phy_set_linkrate(hisi_hba, phy_no, funcdata);
1004 case PHY_FUNC_GET_EVENTS:
1005 if (hisi_hba->hw->get_events) {
1006 hisi_hba->hw->get_events(hisi_hba, phy_no);
1010 case PHY_FUNC_RELEASE_SPINUP_HOLD:
1017 static void hisi_sas_task_done(struct sas_task *task)
1019 del_timer(&task->slow_task->timer);
1020 complete(&task->slow_task->completion);
1023 static void hisi_sas_tmf_timedout(struct timer_list *t)
1025 struct sas_task_slow *slow = from_timer(slow, t, timer);
1026 struct sas_task *task = slow->task;
1027 unsigned long flags;
1028 bool is_completed = true;
1030 spin_lock_irqsave(&task->task_state_lock, flags);
1031 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1032 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1033 is_completed = false;
1035 spin_unlock_irqrestore(&task->task_state_lock, flags);
1038 complete(&task->slow_task->completion);
1041 #define TASK_TIMEOUT 20
1042 #define TASK_RETRY 3
1043 #define INTERNAL_ABORT_TIMEOUT 6
1044 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
1045 void *parameter, u32 para_len,
1046 struct hisi_sas_tmf_task *tmf)
1048 struct hisi_sas_device *sas_dev = device->lldd_dev;
1049 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
1050 struct device *dev = hisi_hba->dev;
1051 struct sas_task *task;
1054 for (retry = 0; retry < TASK_RETRY; retry++) {
1055 task = sas_alloc_slow_task(GFP_KERNEL);
1060 task->task_proto = device->tproto;
1062 if (dev_is_sata(device)) {
1063 task->ata_task.device_control_reg_update = 1;
1064 memcpy(&task->ata_task.fis, parameter, para_len);
1066 memcpy(&task->ssp_task, parameter, para_len);
1068 task->task_done = hisi_sas_task_done;
1070 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1071 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
1072 add_timer(&task->slow_task->timer);
1074 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1077 del_timer(&task->slow_task->timer);
1078 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1083 wait_for_completion(&task->slow_task->completion);
1084 res = TMF_RESP_FUNC_FAILED;
1085 /* Even TMF timed out, return direct. */
1086 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1087 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1088 struct hisi_sas_slot *slot = task->lldd_task;
1090 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1092 struct hisi_sas_cq *cq =
1093 &hisi_hba->cq[slot->dlvry_queue];
1095 * flush tasklet to avoid free'ing task
1096 * before using task in IO completion
1098 tasklet_kill(&cq->tasklet);
1104 dev_err(dev, "abort tmf: TMF task timeout\n");
1107 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1108 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1109 res = TMF_RESP_FUNC_COMPLETE;
1113 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1114 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1115 res = TMF_RESP_FUNC_SUCC;
1119 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1120 task->task_status.stat == SAS_DATA_UNDERRUN) {
1121 /* no error, but return the number of bytes of
1124 dev_warn(dev, "abort tmf: task to dev %016llx "
1125 "resp: 0x%x sts 0x%x underrun\n",
1126 SAS_ADDR(device->sas_addr),
1127 task->task_status.resp,
1128 task->task_status.stat);
1129 res = task->task_status.residual;
1133 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1134 task->task_status.stat == SAS_DATA_OVERRUN) {
1135 dev_warn(dev, "abort tmf: blocked task error\n");
1140 dev_warn(dev, "abort tmf: task to dev "
1141 "%016llx resp: 0x%x status 0x%x\n",
1142 SAS_ADDR(device->sas_addr), task->task_status.resp,
1143 task->task_status.stat);
1144 sas_free_task(task);
1148 if (retry == TASK_RETRY)
1149 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1150 sas_free_task(task);
1154 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1155 bool reset, int pmp, u8 *fis)
1157 struct ata_taskfile tf;
1159 ata_tf_init(dev, &tf);
1163 tf.ctl &= ~ATA_SRST;
1164 tf.command = ATA_CMD_DEV_RESET;
1165 ata_tf_to_fis(&tf, pmp, 0, fis);
1168 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1171 struct ata_port *ap = device->sata_dev.ap;
1172 struct ata_link *link;
1173 int rc = TMF_RESP_FUNC_FAILED;
1174 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1175 struct device *dev = hisi_hba->dev;
1176 int s = sizeof(struct host_to_dev_fis);
1178 ata_for_each_link(link, ap, EDGE) {
1179 int pmp = sata_srst_pmp(link);
1181 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1182 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1183 if (rc != TMF_RESP_FUNC_COMPLETE)
1187 if (rc == TMF_RESP_FUNC_COMPLETE) {
1188 ata_for_each_link(link, ap, EDGE) {
1189 int pmp = sata_srst_pmp(link);
1191 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1192 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1194 if (rc != TMF_RESP_FUNC_COMPLETE)
1195 dev_err(dev, "ata disk de-reset failed\n");
1198 dev_err(dev, "ata disk reset failed\n");
1201 if (rc == TMF_RESP_FUNC_COMPLETE)
1202 hisi_sas_release_task(hisi_hba, device);
1207 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1208 u8 *lun, struct hisi_sas_tmf_task *tmf)
1210 struct sas_ssp_task ssp_task;
1212 if (!(device->tproto & SAS_PROTOCOL_SSP))
1213 return TMF_RESP_FUNC_ESUPP;
1215 memcpy(ssp_task.LUN, lun, 8);
1217 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1218 sizeof(ssp_task), tmf);
1221 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1223 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1226 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1227 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1228 struct domain_device *device = sas_dev->sas_device;
1229 struct asd_sas_port *sas_port;
1230 struct hisi_sas_port *port;
1231 struct hisi_sas_phy *phy = NULL;
1232 struct asd_sas_phy *sas_phy;
1234 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1235 || !device || !device->port)
1238 sas_port = device->port;
1239 port = to_hisi_sas_port(sas_port);
1241 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1242 if (state & BIT(sas_phy->id)) {
1243 phy = sas_phy->lldd_phy;
1248 port->id = phy->port_id;
1250 /* Update linkrate of directly attached device. */
1251 if (!device->parent)
1252 device->linkrate = phy->sas_phy.linkrate;
1254 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1260 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1263 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1264 struct asd_sas_port *_sas_port = NULL;
1267 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1268 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1269 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1270 struct asd_sas_port *sas_port = sas_phy->port;
1271 bool do_port_check = !!(_sas_port != sas_port);
1273 if (!sas_phy->phy->enabled)
1276 /* Report PHY state change to libsas */
1277 if (state & BIT(phy_no)) {
1278 if (do_port_check && sas_port && sas_port->port_dev) {
1279 struct domain_device *dev = sas_port->port_dev;
1281 _sas_port = sas_port;
1283 if (DEV_IS_EXPANDER(dev->dev_type))
1284 sas_ha->notify_port_event(sas_phy,
1285 PORTE_BROADCAST_RCVD);
1287 } else if (old_state & (1 << phy_no))
1288 /* PHY down but was up before */
1289 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1294 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1296 struct hisi_sas_device *sas_dev;
1297 struct domain_device *device;
1300 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1301 sas_dev = &hisi_hba->devices[i];
1302 device = sas_dev->sas_device;
1304 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1307 hisi_sas_init_device(device);
1311 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1312 struct asd_sas_port *sas_port,
1313 struct domain_device *device)
1315 struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1316 struct ata_port *ap = device->sata_dev.ap;
1317 struct device *dev = hisi_hba->dev;
1318 int s = sizeof(struct host_to_dev_fis);
1319 int rc = TMF_RESP_FUNC_FAILED;
1320 struct asd_sas_phy *sas_phy;
1321 struct ata_link *link;
1325 state = hisi_hba->hw->get_phys_state(hisi_hba);
1326 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1327 if (!(state & BIT(sas_phy->id)))
1330 ata_for_each_link(link, ap, EDGE) {
1331 int pmp = sata_srst_pmp(link);
1333 tmf_task.phy_id = sas_phy->id;
1334 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1335 rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1337 if (rc != TMF_RESP_FUNC_COMPLETE) {
1338 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1346 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1348 struct device *dev = hisi_hba->dev;
1351 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1352 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1353 struct domain_device *device = sas_dev->sas_device;
1355 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1358 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1359 HISI_SAS_INT_ABT_DEV, 0);
1361 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1364 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1365 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1366 struct asd_sas_port *sas_port = &port->sas_port;
1367 struct domain_device *port_dev = sas_port->port_dev;
1368 struct domain_device *device;
1370 if (!port_dev || !DEV_IS_EXPANDER(port_dev->dev_type))
1373 /* Try to find a SATA device */
1374 list_for_each_entry(device, &sas_port->dev_list,
1376 if (dev_is_sata(device)) {
1377 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1386 void hisi_sas_controller_reset_prepare(struct hisi_hba *hisi_hba)
1388 struct Scsi_Host *shost = hisi_hba->shost;
1390 down(&hisi_hba->sem);
1391 hisi_hba->phy_state = hisi_hba->hw->get_phys_state(hisi_hba);
1393 scsi_block_requests(shost);
1394 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1396 if (timer_pending(&hisi_hba->timer))
1397 del_timer_sync(&hisi_hba->timer);
1399 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1401 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_prepare);
1403 void hisi_sas_controller_reset_done(struct hisi_hba *hisi_hba)
1405 struct Scsi_Host *shost = hisi_hba->shost;
1408 /* Init and wait for PHYs to come up and all libsas event finished. */
1409 hisi_hba->hw->phys_init(hisi_hba);
1411 hisi_sas_refresh_port_id(hisi_hba);
1412 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1415 if (hisi_hba->reject_stp_links_msk)
1416 hisi_sas_terminate_stp_reject(hisi_hba);
1417 hisi_sas_reset_init_all_devices(hisi_hba);
1418 scsi_unblock_requests(shost);
1419 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1421 state = hisi_hba->hw->get_phys_state(hisi_hba);
1422 hisi_sas_rescan_topology(hisi_hba, hisi_hba->phy_state, state);
1424 EXPORT_SYMBOL_GPL(hisi_sas_controller_reset_done);
1426 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1428 struct device *dev = hisi_hba->dev;
1429 struct Scsi_Host *shost = hisi_hba->shost;
1432 if (hisi_sas_debugfs_enable && hisi_hba->debugfs_itct)
1433 queue_work(hisi_hba->wq, &hisi_hba->debugfs_work);
1435 if (!hisi_hba->hw->soft_reset)
1438 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1441 dev_info(dev, "controller resetting...\n");
1442 hisi_sas_controller_reset_prepare(hisi_hba);
1444 rc = hisi_hba->hw->soft_reset(hisi_hba);
1446 dev_warn(dev, "controller reset failed (%d)\n", rc);
1447 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1449 scsi_unblock_requests(shost);
1450 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1454 hisi_sas_controller_reset_done(hisi_hba);
1455 dev_info(dev, "controller reset complete\n");
1460 static int hisi_sas_abort_task(struct sas_task *task)
1462 struct scsi_lun lun;
1463 struct hisi_sas_tmf_task tmf_task;
1464 struct domain_device *device = task->dev;
1465 struct hisi_sas_device *sas_dev = device->lldd_dev;
1466 struct hisi_hba *hisi_hba;
1468 int rc = TMF_RESP_FUNC_FAILED;
1469 unsigned long flags;
1472 return TMF_RESP_FUNC_FAILED;
1474 hisi_hba = dev_to_hisi_hba(task->dev);
1475 dev = hisi_hba->dev;
1477 spin_lock_irqsave(&task->task_state_lock, flags);
1478 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1479 struct hisi_sas_slot *slot = task->lldd_task;
1480 struct hisi_sas_cq *cq;
1484 * flush tasklet to avoid free'ing task
1485 * before using task in IO completion
1487 cq = &hisi_hba->cq[slot->dlvry_queue];
1488 tasklet_kill(&cq->tasklet);
1490 spin_unlock_irqrestore(&task->task_state_lock, flags);
1491 rc = TMF_RESP_FUNC_COMPLETE;
1494 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1495 spin_unlock_irqrestore(&task->task_state_lock, flags);
1497 sas_dev->dev_status = HISI_SAS_DEV_EH;
1498 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1499 struct scsi_cmnd *cmnd = task->uldd_task;
1500 struct hisi_sas_slot *slot = task->lldd_task;
1501 u16 tag = slot->idx;
1504 int_to_scsilun(cmnd->device->lun, &lun);
1505 tmf_task.tmf = TMF_ABORT_TASK;
1506 tmf_task.tag_of_task_to_be_managed = tag;
1508 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1511 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1512 HISI_SAS_INT_ABT_CMD, tag);
1514 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1515 return TMF_RESP_FUNC_FAILED;
1519 * If the TMF finds that the IO is not in the device and also
1520 * the internal abort does not succeed, then it is safe to
1522 * Note: if the internal abort succeeds then the slot
1523 * will have already been completed
1525 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1526 if (task->lldd_task)
1527 hisi_sas_do_release_task(hisi_hba, task, slot);
1529 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1530 task->task_proto & SAS_PROTOCOL_STP) {
1531 if (task->dev->dev_type == SAS_SATA_DEV) {
1532 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1533 HISI_SAS_INT_ABT_DEV, 0);
1535 dev_err(dev, "abort task: internal abort failed\n");
1538 hisi_sas_dereg_device(hisi_hba, device);
1539 rc = hisi_sas_softreset_ata_disk(device);
1541 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1543 struct hisi_sas_slot *slot = task->lldd_task;
1544 u32 tag = slot->idx;
1545 struct hisi_sas_cq *cq = &hisi_hba->cq[slot->dlvry_queue];
1547 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1548 HISI_SAS_INT_ABT_CMD, tag);
1549 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1552 * flush tasklet to avoid free'ing task
1553 * before using task in IO completion
1555 tasklet_kill(&cq->tasklet);
1561 if (rc != TMF_RESP_FUNC_COMPLETE)
1562 dev_notice(dev, "abort task: rc=%d\n", rc);
1566 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1568 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1569 struct device *dev = hisi_hba->dev;
1570 struct hisi_sas_tmf_task tmf_task;
1571 int rc = TMF_RESP_FUNC_FAILED;
1573 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1574 HISI_SAS_INT_ABT_DEV, 0);
1576 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1577 return TMF_RESP_FUNC_FAILED;
1579 hisi_sas_dereg_device(hisi_hba, device);
1581 tmf_task.tmf = TMF_ABORT_TASK_SET;
1582 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1584 if (rc == TMF_RESP_FUNC_COMPLETE)
1585 hisi_sas_release_task(hisi_hba, device);
1590 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1592 int rc = TMF_RESP_FUNC_FAILED;
1593 struct hisi_sas_tmf_task tmf_task;
1595 tmf_task.tmf = TMF_CLEAR_ACA;
1596 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1601 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1603 struct sas_phy *local_phy = sas_get_local_phy(device);
1604 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1605 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1606 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1607 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1608 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[local_phy->number];
1609 struct hisi_sas_phy *phy = container_of(sas_phy,
1610 struct hisi_sas_phy, sas_phy);
1611 DECLARE_COMPLETION_ONSTACK(phyreset);
1613 if (scsi_is_sas_phy_local(local_phy)) {
1615 phy->reset_completion = &phyreset;
1618 rc = sas_phy_reset(local_phy, reset_type);
1619 sas_put_local_phy(local_phy);
1621 if (scsi_is_sas_phy_local(local_phy)) {
1622 int ret = wait_for_completion_timeout(&phyreset, 2 * HZ);
1623 unsigned long flags;
1625 spin_lock_irqsave(&phy->lock, flags);
1626 phy->reset_completion = NULL;
1628 spin_unlock_irqrestore(&phy->lock, flags);
1630 /* report PHY down if timed out */
1632 hisi_sas_phy_down(hisi_hba, sas_phy->id, 0);
1639 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1641 struct hisi_sas_device *sas_dev = device->lldd_dev;
1642 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1643 struct device *dev = hisi_hba->dev;
1644 int rc = TMF_RESP_FUNC_FAILED;
1646 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1647 return TMF_RESP_FUNC_FAILED;
1648 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1650 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1651 HISI_SAS_INT_ABT_DEV, 0);
1653 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1654 return TMF_RESP_FUNC_FAILED;
1656 hisi_sas_dereg_device(hisi_hba, device);
1658 rc = hisi_sas_debug_I_T_nexus_reset(device);
1660 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1661 hisi_sas_release_task(hisi_hba, device);
1666 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1668 struct hisi_sas_device *sas_dev = device->lldd_dev;
1669 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1670 struct device *dev = hisi_hba->dev;
1671 int rc = TMF_RESP_FUNC_FAILED;
1673 sas_dev->dev_status = HISI_SAS_DEV_EH;
1674 if (dev_is_sata(device)) {
1675 struct sas_phy *phy;
1677 /* Clear internal IO and then hardreset */
1678 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1679 HISI_SAS_INT_ABT_DEV, 0);
1681 dev_err(dev, "lu_reset: internal abort failed\n");
1684 hisi_sas_dereg_device(hisi_hba, device);
1686 phy = sas_get_local_phy(device);
1688 rc = sas_phy_reset(phy, 1);
1691 hisi_sas_release_task(hisi_hba, device);
1692 sas_put_local_phy(phy);
1694 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1696 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1697 HISI_SAS_INT_ABT_DEV, 0);
1699 dev_err(dev, "lu_reset: internal abort failed\n");
1702 hisi_sas_dereg_device(hisi_hba, device);
1704 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1705 if (rc == TMF_RESP_FUNC_COMPLETE)
1706 hisi_sas_release_task(hisi_hba, device);
1709 if (rc != TMF_RESP_FUNC_COMPLETE)
1710 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1711 sas_dev->device_id, rc);
1715 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1717 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1718 struct device *dev = hisi_hba->dev;
1719 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1722 queue_work(hisi_hba->wq, &r.work);
1723 wait_for_completion(r.completion);
1725 return TMF_RESP_FUNC_FAILED;
1727 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1728 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1729 struct domain_device *device = sas_dev->sas_device;
1731 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device ||
1732 DEV_IS_EXPANDER(device->dev_type))
1735 rc = hisi_sas_debug_I_T_nexus_reset(device);
1736 if (rc != TMF_RESP_FUNC_COMPLETE)
1737 dev_info(dev, "clear nexus ha: for device[%d] rc=%d\n",
1738 sas_dev->device_id, rc);
1741 hisi_sas_release_tasks(hisi_hba);
1743 return TMF_RESP_FUNC_COMPLETE;
1746 static int hisi_sas_query_task(struct sas_task *task)
1748 struct scsi_lun lun;
1749 struct hisi_sas_tmf_task tmf_task;
1750 int rc = TMF_RESP_FUNC_FAILED;
1752 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1753 struct scsi_cmnd *cmnd = task->uldd_task;
1754 struct domain_device *device = task->dev;
1755 struct hisi_sas_slot *slot = task->lldd_task;
1756 u32 tag = slot->idx;
1758 int_to_scsilun(cmnd->device->lun, &lun);
1759 tmf_task.tmf = TMF_QUERY_TASK;
1760 tmf_task.tag_of_task_to_be_managed = tag;
1762 rc = hisi_sas_debug_issue_ssp_tmf(device,
1766 /* The task is still in Lun, release it then */
1767 case TMF_RESP_FUNC_SUCC:
1768 /* The task is not in Lun or failed, reset the phy */
1769 case TMF_RESP_FUNC_FAILED:
1770 case TMF_RESP_FUNC_COMPLETE:
1773 rc = TMF_RESP_FUNC_FAILED;
1781 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1782 struct sas_task *task, int abort_flag,
1785 struct domain_device *device = task->dev;
1786 struct hisi_sas_device *sas_dev = device->lldd_dev;
1787 struct device *dev = hisi_hba->dev;
1788 struct hisi_sas_port *port;
1789 struct hisi_sas_slot *slot;
1790 struct asd_sas_port *sas_port = device->port;
1791 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1792 struct hisi_sas_dq *dq = sas_dev->dq;
1793 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1794 unsigned long flags, flags_dq = 0;
1797 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1803 port = to_hisi_sas_port(sas_port);
1805 /* simply get a slot and send abort command */
1806 rc = hisi_sas_slot_index_alloc(hisi_hba, NULL);
1811 slot = &hisi_hba->slot_info[slot_idx];
1813 spin_lock_irqsave(&dq->lock, flags_dq);
1814 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
1815 if (wr_q_index < 0) {
1816 spin_unlock_irqrestore(&dq->lock, flags_dq);
1820 list_add_tail(&slot->delivery, &dq->list);
1821 spin_unlock_irqrestore(&dq->lock, flags_dq);
1823 dlvry_queue = dq->id;
1824 dlvry_queue_slot = wr_q_index;
1826 slot->n_elem = n_elem;
1827 slot->dlvry_queue = dlvry_queue;
1828 slot->dlvry_queue_slot = dlvry_queue_slot;
1829 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1830 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1833 slot->is_internal = true;
1834 task->lldd_task = slot;
1836 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1837 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1838 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
1840 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1841 abort_flag, task_tag);
1843 spin_lock_irqsave(&task->task_state_lock, flags);
1844 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1845 spin_unlock_irqrestore(&task->task_state_lock, flags);
1846 WRITE_ONCE(slot->ready, 1);
1847 /* send abort command to the chip */
1848 spin_lock_irqsave(&dq->lock, flags);
1849 list_add_tail(&slot->entry, &sas_dev->list);
1850 hisi_hba->hw->start_delivery(dq);
1851 spin_unlock_irqrestore(&dq->lock, flags);
1856 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1858 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1864 * hisi_sas_internal_task_abort -- execute an internal
1865 * abort command for single IO command or a device
1866 * @hisi_hba: host controller struct
1867 * @device: domain device
1868 * @abort_flag: mode of operation, device or single IO
1869 * @tag: tag of IO to be aborted (only relevant to single
1873 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1874 struct domain_device *device,
1875 int abort_flag, int tag)
1877 struct sas_task *task;
1878 struct hisi_sas_device *sas_dev = device->lldd_dev;
1879 struct device *dev = hisi_hba->dev;
1883 * The interface is not realized means this HW don't support internal
1884 * abort, or don't need to do internal abort. Then here, we return
1885 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1886 * the internal abort has been executed and returned CQ.
1888 if (!hisi_hba->hw->prep_abort)
1889 return TMF_RESP_FUNC_FAILED;
1891 task = sas_alloc_slow_task(GFP_KERNEL);
1896 task->task_proto = device->tproto;
1897 task->task_done = hisi_sas_task_done;
1898 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1899 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT*HZ;
1900 add_timer(&task->slow_task->timer);
1902 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1903 task, abort_flag, tag);
1905 del_timer(&task->slow_task->timer);
1906 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1910 wait_for_completion(&task->slow_task->completion);
1911 res = TMF_RESP_FUNC_FAILED;
1913 /* Internal abort timed out */
1914 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1915 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1916 struct hisi_sas_slot *slot = task->lldd_task;
1919 struct hisi_sas_cq *cq =
1920 &hisi_hba->cq[slot->dlvry_queue];
1922 * flush tasklet to avoid free'ing task
1923 * before using task in IO completion
1925 tasklet_kill(&cq->tasklet);
1928 dev_err(dev, "internal task abort: timeout and not done.\n");
1933 dev_err(dev, "internal task abort: timeout.\n");
1936 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1937 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1938 res = TMF_RESP_FUNC_COMPLETE;
1942 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1943 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1944 res = TMF_RESP_FUNC_SUCC;
1949 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
1950 "resp: 0x%x sts 0x%x\n",
1951 SAS_ADDR(device->sas_addr),
1953 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1954 task->task_status.stat);
1955 sas_free_task(task);
1960 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1962 hisi_sas_port_notify_formed(sas_phy);
1965 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
1966 u8 reg_index, u8 reg_count, u8 *write_data)
1968 struct hisi_hba *hisi_hba = sha->lldd_ha;
1970 if (!hisi_hba->hw->write_gpio)
1973 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
1974 reg_index, reg_count, write_data);
1977 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1979 phy->phy_attached = 0;
1984 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1986 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1987 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1988 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1989 struct device *dev = hisi_hba->dev;
1992 /* Phy down but ready */
1993 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1994 hisi_sas_port_notify_formed(sas_phy);
1996 struct hisi_sas_port *port = phy->port;
1998 if (test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags) ||
2000 dev_info(dev, "ignore flutter phy%d down\n", phy_no);
2003 /* Phy down and not ready */
2004 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
2005 sas_phy_disconnected(sas_phy);
2008 if (phy->phy_type & PORT_TYPE_SAS) {
2009 int port_id = port->id;
2011 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
2013 port->port_attached = 0;
2014 } else if (phy->phy_type & PORT_TYPE_SATA)
2015 port->port_attached = 0;
2017 hisi_sas_phy_disconnected(phy);
2020 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
2022 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
2026 for (i = 0; i < hisi_hba->queue_count; i++) {
2027 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2029 tasklet_kill(&cq->tasklet);
2032 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
2034 struct scsi_transport_template *hisi_sas_stt;
2035 EXPORT_SYMBOL_GPL(hisi_sas_stt);
2037 static struct sas_domain_function_template hisi_sas_transport_ops = {
2038 .lldd_dev_found = hisi_sas_dev_found,
2039 .lldd_dev_gone = hisi_sas_dev_gone,
2040 .lldd_execute_task = hisi_sas_queue_command,
2041 .lldd_control_phy = hisi_sas_control_phy,
2042 .lldd_abort_task = hisi_sas_abort_task,
2043 .lldd_abort_task_set = hisi_sas_abort_task_set,
2044 .lldd_clear_aca = hisi_sas_clear_aca,
2045 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
2046 .lldd_lu_reset = hisi_sas_lu_reset,
2047 .lldd_query_task = hisi_sas_query_task,
2048 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
2049 .lldd_port_formed = hisi_sas_port_formed,
2050 .lldd_write_gpio = hisi_sas_write_gpio,
2053 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
2055 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
2057 for (i = 0; i < hisi_hba->queue_count; i++) {
2058 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2059 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2061 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2062 memset(hisi_hba->cmd_hdr[i], 0, s);
2065 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2066 memset(hisi_hba->complete_hdr[i], 0, s);
2070 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
2071 memset(hisi_hba->initial_fis, 0, s);
2073 s = max_command_entries * sizeof(struct hisi_sas_iost);
2074 memset(hisi_hba->iost, 0, s);
2076 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2077 memset(hisi_hba->breakpoint, 0, s);
2079 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2080 memset(hisi_hba->sata_breakpoint, 0, s);
2082 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
2084 int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
2086 struct device *dev = hisi_hba->dev;
2087 int i, j, s, max_command_entries = hisi_hba->hw->max_command_entries;
2088 int max_command_entries_ru, sz_slot_buf_ru;
2089 int blk_cnt, slots_per_blk;
2091 sema_init(&hisi_hba->sem, 1);
2092 spin_lock_init(&hisi_hba->lock);
2093 for (i = 0; i < hisi_hba->n_phy; i++) {
2094 hisi_sas_phy_init(hisi_hba, i);
2095 hisi_hba->port[i].port_attached = 0;
2096 hisi_hba->port[i].id = -1;
2099 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
2100 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
2101 hisi_hba->devices[i].device_id = i;
2102 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
2105 for (i = 0; i < hisi_hba->queue_count; i++) {
2106 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
2107 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
2109 /* Completion queue structure */
2111 cq->hisi_hba = hisi_hba;
2113 /* Delivery queue structure */
2114 spin_lock_init(&dq->lock);
2115 INIT_LIST_HEAD(&dq->list);
2117 dq->hisi_hba = hisi_hba;
2119 /* Delivery queue */
2120 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2121 hisi_hba->cmd_hdr[i] = dmam_alloc_coherent(dev, s,
2122 &hisi_hba->cmd_hdr_dma[i],
2124 if (!hisi_hba->cmd_hdr[i])
2127 /* Completion queue */
2128 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2129 hisi_hba->complete_hdr[i] = dmam_alloc_coherent(dev, s,
2130 &hisi_hba->complete_hdr_dma[i],
2132 if (!hisi_hba->complete_hdr[i])
2136 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2137 hisi_hba->itct = dmam_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2139 if (!hisi_hba->itct)
2141 memset(hisi_hba->itct, 0, s);
2143 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2144 sizeof(struct hisi_sas_slot),
2146 if (!hisi_hba->slot_info)
2149 /* roundup to avoid overly large block size */
2150 max_command_entries_ru = roundup(max_command_entries, 64);
2151 sz_slot_buf_ru = roundup(sizeof(struct hisi_sas_slot_buf_table), 64);
2152 s = lcm(max_command_entries_ru, sz_slot_buf_ru);
2153 blk_cnt = (max_command_entries_ru * sz_slot_buf_ru) / s;
2154 slots_per_blk = s / sz_slot_buf_ru;
2155 for (i = 0; i < blk_cnt; i++) {
2156 struct hisi_sas_slot_buf_table *buf;
2158 int slot_index = i * slots_per_blk;
2160 buf = dmam_alloc_coherent(dev, s, &buf_dma, GFP_KERNEL);
2165 for (j = 0; j < slots_per_blk; j++, slot_index++) {
2166 struct hisi_sas_slot *slot;
2168 slot = &hisi_hba->slot_info[slot_index];
2170 slot->buf_dma = buf_dma;
2171 slot->idx = slot_index;
2174 buf_dma += sizeof(*buf);
2178 s = max_command_entries * sizeof(struct hisi_sas_iost);
2179 hisi_hba->iost = dmam_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2181 if (!hisi_hba->iost)
2184 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2185 hisi_hba->breakpoint = dmam_alloc_coherent(dev, s,
2186 &hisi_hba->breakpoint_dma,
2188 if (!hisi_hba->breakpoint)
2191 hisi_hba->slot_index_count = max_command_entries;
2192 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2193 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2194 if (!hisi_hba->slot_index_tags)
2197 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2198 hisi_hba->initial_fis = dmam_alloc_coherent(dev, s,
2199 &hisi_hba->initial_fis_dma,
2201 if (!hisi_hba->initial_fis)
2204 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2205 hisi_hba->sata_breakpoint = dmam_alloc_coherent(dev, s,
2206 &hisi_hba->sata_breakpoint_dma,
2208 if (!hisi_hba->sata_breakpoint)
2210 hisi_sas_init_mem(hisi_hba);
2212 hisi_sas_slot_index_init(hisi_hba);
2213 hisi_hba->last_slot_index = hisi_hba->hw->max_command_entries -
2214 HISI_SAS_RESERVED_IPTT_CNT;
2216 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2217 if (!hisi_hba->wq) {
2218 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2226 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2228 void hisi_sas_free(struct hisi_hba *hisi_hba)
2231 destroy_workqueue(hisi_hba->wq);
2233 EXPORT_SYMBOL_GPL(hisi_sas_free);
2235 void hisi_sas_rst_work_handler(struct work_struct *work)
2237 struct hisi_hba *hisi_hba =
2238 container_of(work, struct hisi_hba, rst_work);
2240 hisi_sas_controller_reset(hisi_hba);
2242 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2244 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2246 struct hisi_sas_rst *rst =
2247 container_of(work, struct hisi_sas_rst, work);
2249 if (!hisi_sas_controller_reset(rst->hisi_hba))
2251 complete(rst->completion);
2253 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2255 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2257 struct device *dev = hisi_hba->dev;
2258 struct platform_device *pdev = hisi_hba->platform_dev;
2259 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2262 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2264 dev_err(dev, "could not get property sas-addr\n");
2270 * These properties are only required for platform device-based
2271 * controller with DT firmware.
2273 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2274 "hisilicon,sas-syscon");
2275 if (IS_ERR(hisi_hba->ctrl)) {
2276 dev_err(dev, "could not get syscon\n");
2280 if (device_property_read_u32(dev, "ctrl-reset-reg",
2281 &hisi_hba->ctrl_reset_reg)) {
2283 "could not get property ctrl-reset-reg\n");
2287 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2288 &hisi_hba->ctrl_reset_sts_reg)) {
2290 "could not get property ctrl-reset-sts-reg\n");
2294 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2295 &hisi_hba->ctrl_clock_ena_reg)) {
2297 "could not get property ctrl-clock-ena-reg\n");
2302 refclk = devm_clk_get(dev, NULL);
2304 dev_dbg(dev, "no ref clk property\n");
2306 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2308 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2309 dev_err(dev, "could not get property phy-count\n");
2313 if (device_property_read_u32(dev, "queue-count",
2314 &hisi_hba->queue_count)) {
2315 dev_err(dev, "could not get property queue-count\n");
2321 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2323 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2324 const struct hisi_sas_hw *hw)
2326 struct resource *res;
2327 struct Scsi_Host *shost;
2328 struct hisi_hba *hisi_hba;
2329 struct device *dev = &pdev->dev;
2331 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2333 dev_err(dev, "scsi host alloc failed\n");
2336 hisi_hba = shost_priv(shost);
2338 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2340 hisi_hba->dev = dev;
2341 hisi_hba->platform_dev = pdev;
2342 hisi_hba->shost = shost;
2343 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2345 timer_setup(&hisi_hba->timer, NULL, 0);
2347 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2350 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
2351 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
2352 dev_err(dev, "No usable DMA addressing method\n");
2356 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2357 hisi_hba->regs = devm_ioremap_resource(dev, res);
2358 if (IS_ERR(hisi_hba->regs))
2361 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2363 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2364 if (IS_ERR(hisi_hba->sgpio_regs))
2368 if (hisi_sas_alloc(hisi_hba, shost)) {
2369 hisi_sas_free(hisi_hba);
2375 scsi_host_put(shost);
2376 dev_err(dev, "shost alloc failed\n");
2380 int hisi_sas_probe(struct platform_device *pdev,
2381 const struct hisi_sas_hw *hw)
2383 struct Scsi_Host *shost;
2384 struct hisi_hba *hisi_hba;
2385 struct device *dev = &pdev->dev;
2386 struct asd_sas_phy **arr_phy;
2387 struct asd_sas_port **arr_port;
2388 struct sas_ha_struct *sha;
2389 int rc, phy_nr, port_nr, i;
2391 shost = hisi_sas_shost_alloc(pdev, hw);
2395 sha = SHOST_TO_SAS_HA(shost);
2396 hisi_hba = shost_priv(shost);
2397 platform_set_drvdata(pdev, sha);
2399 phy_nr = port_nr = hisi_hba->n_phy;
2401 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2402 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2403 if (!arr_phy || !arr_port) {
2408 sha->sas_phy = arr_phy;
2409 sha->sas_port = arr_port;
2410 sha->lldd_ha = hisi_hba;
2412 shost->transportt = hisi_sas_stt;
2413 shost->max_id = HISI_SAS_MAX_DEVICES;
2414 shost->max_lun = ~0;
2415 shost->max_channel = 1;
2416 shost->max_cmd_len = 16;
2417 if (hisi_hba->hw->slot_index_alloc) {
2418 shost->can_queue = hisi_hba->hw->max_command_entries;
2419 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
2421 shost->can_queue = hisi_hba->hw->max_command_entries -
2422 HISI_SAS_RESERVED_IPTT_CNT;
2423 shost->cmd_per_lun = hisi_hba->hw->max_command_entries -
2424 HISI_SAS_RESERVED_IPTT_CNT;
2427 sha->sas_ha_name = DRV_NAME;
2428 sha->dev = hisi_hba->dev;
2429 sha->lldd_module = THIS_MODULE;
2430 sha->sas_addr = &hisi_hba->sas_addr[0];
2431 sha->num_phys = hisi_hba->n_phy;
2432 sha->core.shost = hisi_hba->shost;
2434 for (i = 0; i < hisi_hba->n_phy; i++) {
2435 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2436 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2439 rc = scsi_add_host(shost, &pdev->dev);
2443 rc = sas_register_ha(sha);
2445 goto err_out_register_ha;
2447 rc = hisi_hba->hw->hw_init(hisi_hba);
2449 goto err_out_register_ha;
2451 scsi_scan_host(shost);
2455 err_out_register_ha:
2456 scsi_remove_host(shost);
2458 hisi_sas_free(hisi_hba);
2459 scsi_host_put(shost);
2462 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2464 struct dentry *hisi_sas_debugfs_dir;
2466 static void hisi_sas_debugfs_snapshot_cq_reg(struct hisi_hba *hisi_hba)
2468 int queue_entry_size = hisi_hba->hw->complete_hdr_size;
2471 for (i = 0; i < hisi_hba->queue_count; i++)
2472 memcpy(hisi_hba->debugfs_complete_hdr[i],
2473 hisi_hba->complete_hdr[i],
2474 HISI_SAS_QUEUE_SLOTS * queue_entry_size);
2477 static void hisi_sas_debugfs_snapshot_dq_reg(struct hisi_hba *hisi_hba)
2479 int queue_entry_size = hisi_hba->hw->complete_hdr_size;
2482 for (i = 0; i < hisi_hba->queue_count; i++)
2483 memcpy(hisi_hba->debugfs_cmd_hdr[i],
2484 hisi_hba->cmd_hdr[i],
2485 HISI_SAS_QUEUE_SLOTS * queue_entry_size);
2488 static void hisi_sas_debugfs_snapshot_port_reg(struct hisi_hba *hisi_hba)
2490 const struct hisi_sas_debugfs_reg *port =
2491 hisi_hba->hw->debugfs_reg_port;
2496 for (phy_cnt = 0; phy_cnt < hisi_hba->n_phy; phy_cnt++) {
2497 databuf = (u32 *)hisi_hba->debugfs_port_reg[phy_cnt];
2498 for (i = 0; i < port->count; i++, databuf++) {
2499 offset = port->base_off + 4 * i;
2500 *databuf = port->read_port_reg(hisi_hba, phy_cnt,
2506 static void hisi_sas_debugfs_snapshot_global_reg(struct hisi_hba *hisi_hba)
2508 u32 *databuf = (u32 *)hisi_hba->debugfs_global_reg;
2509 const struct hisi_sas_debugfs_reg *global =
2510 hisi_hba->hw->debugfs_reg_global;
2513 for (i = 0; i < global->count; i++, databuf++)
2514 *databuf = global->read_global_reg(hisi_hba, 4 * i);
2517 static void hisi_sas_debugfs_snapshot_itct_reg(struct hisi_hba *hisi_hba)
2519 void *databuf = hisi_hba->debugfs_itct;
2520 struct hisi_sas_itct *itct;
2523 itct = hisi_hba->itct;
2525 for (i = 0; i < HISI_SAS_MAX_ITCT_ENTRIES; i++, itct++) {
2526 memcpy(databuf, itct, sizeof(struct hisi_sas_itct));
2527 databuf += sizeof(struct hisi_sas_itct);
2531 static void hisi_sas_debugfs_snapshot_iost_reg(struct hisi_hba *hisi_hba)
2533 int max_command_entries = hisi_hba->hw->max_command_entries;
2534 void *databuf = hisi_hba->debugfs_iost;
2535 struct hisi_sas_iost *iost;
2538 iost = hisi_hba->iost;
2540 for (i = 0; i < max_command_entries; i++, iost++) {
2541 memcpy(databuf, iost, sizeof(struct hisi_sas_iost));
2542 databuf += sizeof(struct hisi_sas_iost);
2547 hisi_sas_debugfs_to_reg_name(int off, int base_off,
2548 const struct hisi_sas_debugfs_reg_lu *lu)
2550 for (; lu->name; lu++) {
2551 if (off == lu->off - base_off)
2558 static void hisi_sas_debugfs_print_reg(u32 *regs_val, const void *ptr,
2561 const struct hisi_sas_debugfs_reg *reg = ptr;
2564 for (i = 0; i < reg->count; i++) {
2568 name = hisi_sas_debugfs_to_reg_name(off, reg->base_off,
2572 seq_printf(s, "0x%08x 0x%08x %s\n", off,
2573 le32_to_cpu(regs_val[i]), name);
2575 seq_printf(s, "0x%08x 0x%08x\n", off,
2576 le32_to_cpu(regs_val[i]));
2580 static int hisi_sas_debugfs_global_show(struct seq_file *s, void *p)
2582 struct hisi_hba *hisi_hba = s->private;
2583 const struct hisi_sas_hw *hw = hisi_hba->hw;
2584 const struct hisi_sas_debugfs_reg *reg_global = hw->debugfs_reg_global;
2586 hisi_sas_debugfs_print_reg((u32 *)hisi_hba->debugfs_global_reg,
2592 static int hisi_sas_debugfs_global_open(struct inode *inode, struct file *filp)
2594 return single_open(filp, hisi_sas_debugfs_global_show,
2598 static const struct file_operations hisi_sas_debugfs_global_fops = {
2599 .open = hisi_sas_debugfs_global_open,
2601 .llseek = seq_lseek,
2602 .release = single_release,
2603 .owner = THIS_MODULE,
2606 static int hisi_sas_debugfs_port_show(struct seq_file *s, void *p)
2608 struct hisi_sas_phy *phy = s->private;
2609 struct hisi_hba *hisi_hba = phy->hisi_hba;
2610 const struct hisi_sas_hw *hw = hisi_hba->hw;
2611 const struct hisi_sas_debugfs_reg *reg_port = hw->debugfs_reg_port;
2612 u32 *databuf = hisi_hba->debugfs_port_reg[phy->sas_phy.id];
2614 hisi_sas_debugfs_print_reg(databuf, reg_port, s);
2619 static int hisi_sas_debugfs_port_open(struct inode *inode, struct file *filp)
2621 return single_open(filp, hisi_sas_debugfs_port_show, inode->i_private);
2624 static const struct file_operations hisi_sas_debugfs_port_fops = {
2625 .open = hisi_sas_debugfs_port_open,
2627 .llseek = seq_lseek,
2628 .release = single_release,
2629 .owner = THIS_MODULE,
2632 static int hisi_sas_show_row_64(struct seq_file *s, int index,
2637 /* completion header size not fixed per HW version */
2638 seq_printf(s, "index %04d:\n\t", index);
2639 for (i = 1; i <= sz / 8; i++, ptr++) {
2640 seq_printf(s, " 0x%016llx", le64_to_cpu(*ptr));
2642 seq_puts(s, "\n\t");
2650 static int hisi_sas_show_row_32(struct seq_file *s, int index,
2655 /* completion header size not fixed per HW version */
2656 seq_printf(s, "index %04d:\n\t", index);
2657 for (i = 1; i <= sz / 4; i++, ptr++) {
2659 seq_puts(s, "\n\t");
2666 static int hisi_sas_cq_show_slot(struct seq_file *s, int slot, void *cq_ptr)
2668 struct hisi_sas_cq *cq = cq_ptr;
2669 struct hisi_hba *hisi_hba = cq->hisi_hba;
2670 void *complete_queue = hisi_hba->debugfs_complete_hdr[cq->id];
2671 void *complete_hdr = complete_queue +
2672 (hisi_hba->hw->complete_hdr_size * slot);
2674 return hisi_sas_show_row_32(s, slot,
2675 hisi_hba->hw->complete_hdr_size,
2679 static int hisi_sas_debugfs_cq_show(struct seq_file *s, void *p)
2681 struct hisi_sas_cq *cq = s->private;
2684 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
2685 ret = hisi_sas_cq_show_slot(s, slot, cq);
2692 static int hisi_sas_debugfs_cq_open(struct inode *inode, struct file *filp)
2694 return single_open(filp, hisi_sas_debugfs_cq_show, inode->i_private);
2697 static const struct file_operations hisi_sas_debugfs_cq_fops = {
2698 .open = hisi_sas_debugfs_cq_open,
2700 .llseek = seq_lseek,
2701 .release = single_release,
2702 .owner = THIS_MODULE,
2705 static int hisi_sas_dq_show_slot(struct seq_file *s, int slot, void *dq_ptr)
2707 struct hisi_sas_dq *dq = dq_ptr;
2708 struct hisi_hba *hisi_hba = dq->hisi_hba;
2709 void *cmd_queue = hisi_hba->debugfs_cmd_hdr[dq->id];
2710 void *cmd_hdr = cmd_queue +
2711 hisi_hba->hw->complete_hdr_size * slot;
2713 return hisi_sas_show_row_32(s, slot, sizeof(struct hisi_sas_cmd_hdr),
2717 static int hisi_sas_debugfs_dq_show(struct seq_file *s, void *p)
2721 for (slot = 0; slot < HISI_SAS_QUEUE_SLOTS; slot++) {
2722 ret = hisi_sas_dq_show_slot(s, slot, s->private);
2729 static int hisi_sas_debugfs_dq_open(struct inode *inode, struct file *filp)
2731 return single_open(filp, hisi_sas_debugfs_dq_show, inode->i_private);
2734 static const struct file_operations hisi_sas_debugfs_dq_fops = {
2735 .open = hisi_sas_debugfs_dq_open,
2737 .llseek = seq_lseek,
2738 .release = single_release,
2739 .owner = THIS_MODULE,
2742 static int hisi_sas_debugfs_iost_show(struct seq_file *s, void *p)
2744 struct hisi_hba *hisi_hba = s->private;
2745 struct hisi_sas_iost *debugfs_iost = hisi_hba->debugfs_iost;
2746 int i, ret, max_command_entries = hisi_hba->hw->max_command_entries;
2748 for (i = 0; i < max_command_entries; i++, debugfs_iost++) {
2749 ret = hisi_sas_show_row_64(s, i, sizeof(*debugfs_iost),
2750 (u64 *)debugfs_iost);
2758 static int hisi_sas_debugfs_iost_open(struct inode *inode, struct file *filp)
2760 return single_open(filp, hisi_sas_debugfs_iost_show, inode->i_private);
2763 static const struct file_operations hisi_sas_debugfs_iost_fops = {
2764 .open = hisi_sas_debugfs_iost_open,
2766 .llseek = seq_lseek,
2767 .release = single_release,
2768 .owner = THIS_MODULE,
2771 static void hisi_sas_debugfs_create_files(struct hisi_hba *hisi_hba)
2773 struct dentry *dump_dentry;
2774 struct dentry *dentry;
2780 /* Create dump dir inside device dir */
2781 dump_dentry = debugfs_create_dir("dump", hisi_hba->debugfs_dir);
2782 hisi_hba->debugfs_dump_dentry = dump_dentry;
2784 debugfs_create_file("global", 0400, dump_dentry, hisi_hba,
2785 &hisi_sas_debugfs_global_fops);
2787 /* Create port dir and files */
2788 dentry = debugfs_create_dir("port", dump_dentry);
2789 for (p = 0; p < hisi_hba->n_phy; p++) {
2790 snprintf(name, 256, "%d", p);
2792 debugfs_create_file(name, 0400, dentry, &hisi_hba->phy[p],
2793 &hisi_sas_debugfs_port_fops);
2796 /* Create CQ dir and files */
2797 dentry = debugfs_create_dir("cq", dump_dentry);
2798 for (c = 0; c < hisi_hba->queue_count; c++) {
2799 snprintf(name, 256, "%d", c);
2801 debugfs_create_file(name, 0400, dentry, &hisi_hba->cq[c],
2802 &hisi_sas_debugfs_cq_fops);
2805 /* Create DQ dir and files */
2806 dentry = debugfs_create_dir("dq", dump_dentry);
2807 for (d = 0; d < hisi_hba->queue_count; d++) {
2808 snprintf(name, 256, "%d", d);
2810 debugfs_create_file(name, 0400, dentry, &hisi_hba->dq[d],
2811 &hisi_sas_debugfs_dq_fops);
2814 debugfs_create_file("iost", 0400, dump_dentry, hisi_hba,
2815 &hisi_sas_debugfs_iost_fops);
2820 static void hisi_sas_debugfs_snapshot_regs(struct hisi_hba *hisi_hba)
2822 hisi_hba->hw->snapshot_prepare(hisi_hba);
2824 hisi_sas_debugfs_snapshot_global_reg(hisi_hba);
2825 hisi_sas_debugfs_snapshot_port_reg(hisi_hba);
2826 hisi_sas_debugfs_snapshot_cq_reg(hisi_hba);
2827 hisi_sas_debugfs_snapshot_dq_reg(hisi_hba);
2828 hisi_sas_debugfs_snapshot_itct_reg(hisi_hba);
2829 hisi_sas_debugfs_snapshot_iost_reg(hisi_hba);
2831 hisi_sas_debugfs_create_files(hisi_hba);
2833 hisi_hba->hw->snapshot_restore(hisi_hba);
2836 void hisi_sas_debugfs_work_handler(struct work_struct *work)
2838 struct hisi_hba *hisi_hba =
2839 container_of(work, struct hisi_hba, debugfs_work);
2841 if (hisi_hba->debugfs_snapshot)
2843 hisi_hba->debugfs_snapshot = true;
2845 hisi_sas_debugfs_snapshot_regs(hisi_hba);
2847 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_work_handler);
2849 void hisi_sas_debugfs_init(struct hisi_hba *hisi_hba)
2851 int max_command_entries = hisi_hba->hw->max_command_entries;
2852 struct device *dev = hisi_hba->dev;
2856 hisi_hba->debugfs_dir = debugfs_create_dir(dev_name(dev),
2857 hisi_sas_debugfs_dir);
2859 /* Alloc buffer for global */
2860 sz = hisi_hba->hw->debugfs_reg_global->count * 4;
2861 hisi_hba->debugfs_global_reg =
2862 devm_kmalloc(dev, sz, GFP_KERNEL);
2864 if (!hisi_hba->debugfs_global_reg)
2867 /* Alloc buffer for port */
2868 sz = hisi_hba->hw->debugfs_reg_port->count * 4;
2869 for (p = 0; p < hisi_hba->n_phy; p++) {
2870 hisi_hba->debugfs_port_reg[p] =
2871 devm_kmalloc(dev, sz, GFP_KERNEL);
2873 if (!hisi_hba->debugfs_port_reg[p])
2877 /* Alloc buffer for cq */
2878 sz = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2879 for (c = 0; c < hisi_hba->queue_count; c++) {
2880 hisi_hba->debugfs_complete_hdr[c] =
2881 devm_kmalloc(dev, sz, GFP_KERNEL);
2883 if (!hisi_hba->debugfs_complete_hdr[c])
2887 /* Alloc buffer for dq */
2888 sz = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2889 for (d = 0; d < hisi_hba->queue_count; d++) {
2890 hisi_hba->debugfs_cmd_hdr[d] =
2891 devm_kmalloc(dev, sz, GFP_KERNEL);
2893 if (!hisi_hba->debugfs_cmd_hdr[d])
2897 /* Alloc buffer for iost */
2898 sz = max_command_entries * sizeof(struct hisi_sas_iost);
2900 hisi_hba->debugfs_iost = devm_kmalloc(dev, sz, GFP_KERNEL);
2901 if (!hisi_hba->debugfs_iost)
2904 /* Alloc buffer for itct */
2905 /* New memory allocation must be locate before itct */
2906 sz = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2908 hisi_hba->debugfs_itct = devm_kmalloc(dev, sz, GFP_KERNEL);
2909 if (!hisi_hba->debugfs_itct)
2914 devm_kfree(dev, hisi_hba->debugfs_iost);
2916 for (i = 0; i < d; i++)
2917 devm_kfree(dev, hisi_hba->debugfs_cmd_hdr[i]);
2919 for (i = 0; i < c; i++)
2920 devm_kfree(dev, hisi_hba->debugfs_complete_hdr[i]);
2922 for (i = 0; i < p; i++)
2923 devm_kfree(dev, hisi_hba->debugfs_port_reg[i]);
2924 devm_kfree(dev, hisi_hba->debugfs_global_reg);
2926 debugfs_remove_recursive(hisi_hba->debugfs_dir);
2927 dev_dbg(dev, "failed to init debugfs!\n");
2929 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_init);
2931 void hisi_sas_debugfs_exit(struct hisi_hba *hisi_hba)
2933 debugfs_remove_recursive(hisi_hba->debugfs_dir);
2935 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_exit);
2937 int hisi_sas_remove(struct platform_device *pdev)
2939 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2940 struct hisi_hba *hisi_hba = sha->lldd_ha;
2941 struct Scsi_Host *shost = sha->core.shost;
2943 if (timer_pending(&hisi_hba->timer))
2944 del_timer(&hisi_hba->timer);
2946 sas_unregister_ha(sha);
2947 sas_remove_host(sha->core.shost);
2949 hisi_sas_free(hisi_hba);
2950 scsi_host_put(shost);
2953 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2955 bool hisi_sas_debugfs_enable;
2956 EXPORT_SYMBOL_GPL(hisi_sas_debugfs_enable);
2957 module_param_named(debugfs_enable, hisi_sas_debugfs_enable, bool, 0444);
2958 MODULE_PARM_DESC(hisi_sas_debugfs_enable, "Enable driver debugfs (default disabled)");
2960 static __init int hisi_sas_init(void)
2962 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2966 if (hisi_sas_debugfs_enable)
2967 hisi_sas_debugfs_dir = debugfs_create_dir("hisi_sas", NULL);
2972 static __exit void hisi_sas_exit(void)
2974 sas_release_transport(hisi_sas_stt);
2976 debugfs_remove(hisi_sas_debugfs_dir);
2979 module_init(hisi_sas_init);
2980 module_exit(hisi_sas_exit);
2982 MODULE_LICENSE("GPL");
2983 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2984 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2985 MODULE_ALIAS("platform:" DRV_NAME);